xref: /netbsd-src/external/gpl3/gcc/dist/gcc/cp/parser.cc (revision 0a3071956a3a9fdebdbf7f338cf2d439b45fc728)
1 /* -*- C++ -*- Parser.
2    Copyright (C) 2000-2022 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49 
50 
51 /* The lexer.  */
52 
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54    and c-lex.cc) and the C++ parser.  */
55 
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant {
58   NIC_NONE,
59   /* floating-point literal */
60   NIC_FLOAT,
61   /* %<this%> */
62   NIC_THIS,
63   /* %<__FUNCTION__%> */
64   NIC_FUNC_NAME,
65   /* %<__PRETTY_FUNCTION__%> */
66   NIC_PRETTY_FUNC,
67   /* %<__func__%> */
68   NIC_C99_FUNC,
69   /* "%<va_arg%> */
70   NIC_VA_ARG,
71   /* a cast */
72   NIC_CAST,
73   /* %<typeid%> operator */
74   NIC_TYPEID,
75   /* non-constant compound literals */
76   NIC_NCC,
77   /* a function call */
78   NIC_FUNC_CALL,
79   /* an increment */
80   NIC_INC,
81   /* an decrement */
82   NIC_DEC,
83   /* an array reference */
84   NIC_ARRAY_REF,
85   /* %<->%> */
86   NIC_ARROW,
87   /* %<.%> */
88   NIC_POINT,
89   /* the address of a label */
90   NIC_ADDR_LABEL,
91   /* %<*%> */
92   NIC_STAR,
93   /* %<&%> */
94   NIC_ADDR,
95   /* %<++%> */
96   NIC_PREINCREMENT,
97   /* %<--%> */
98   NIC_PREDECREMENT,
99   /* %<new%> */
100   NIC_NEW,
101   /* %<delete%> */
102   NIC_DEL,
103   /* calls to overloaded operators */
104   NIC_OVERLOADED,
105   /* an assignment */
106   NIC_ASSIGNMENT,
107   /* a comma operator */
108   NIC_COMMA,
109   /* a call to a constructor */
110   NIC_CONSTRUCTOR,
111   /* a transaction expression */
112   NIC_TRANSACTION
113 };
114 
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error {
117   /* NULL */
118   NLE_NULL,
119   /* is not a type */
120   NLE_TYPE,
121   /* is not a class or namespace */
122   NLE_CXX98,
123   /* is not a class, namespace, or enumeration */
124   NLE_NOT_CXX98
125 };
126 
127 /* The various kinds of required token */
128 enum required_token {
129   RT_NONE,
130   RT_SEMICOLON,  /* ';' */
131   RT_OPEN_PAREN, /* '(' */
132   RT_CLOSE_BRACE, /* '}' */
133   RT_OPEN_BRACE,  /* '{' */
134   RT_CLOSE_SQUARE, /* ']' */
135   RT_OPEN_SQUARE,  /* '[' */
136   RT_COMMA, /* ',' */
137   RT_SCOPE, /* '::' */
138   RT_LESS, /* '<' */
139   RT_GREATER, /* '>' */
140   RT_EQ, /* '=' */
141   RT_ELLIPSIS, /* '...' */
142   RT_MULT, /* '*' */
143   RT_COMPL, /* '~' */
144   RT_COLON, /* ':' */
145   RT_COLON_SCOPE, /* ':' or '::' */
146   RT_CLOSE_PAREN, /* ')' */
147   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
148   RT_PRAGMA_EOL, /* end of line */
149   RT_NAME, /* identifier */
150 
151   /* The type is CPP_KEYWORD */
152   RT_NEW, /* new */
153   RT_DELETE, /* delete */
154   RT_RETURN, /* return */
155   RT_WHILE, /* while */
156   RT_EXTERN, /* extern */
157   RT_STATIC_ASSERT, /* static_assert */
158   RT_DECLTYPE, /* decltype */
159   RT_OPERATOR, /* operator */
160   RT_CLASS, /* class */
161   RT_TEMPLATE, /* template */
162   RT_NAMESPACE, /* namespace */
163   RT_USING, /* using */
164   RT_ASM, /* asm */
165   RT_TRY, /* try */
166   RT_CATCH, /* catch */
167   RT_THROW, /* throw */
168   RT_AUTO, /* auto */
169   RT_LABEL, /* __label__ */
170   RT_AT_TRY, /* @try */
171   RT_AT_SYNCHRONIZED, /* @synchronized */
172   RT_AT_THROW, /* @throw */
173 
174   RT_SELECT,  /* selection-statement */
175   RT_ITERATION, /* iteration-statement */
176   RT_JUMP, /* jump-statement */
177   RT_CLASS_KEY, /* class-key */
178   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
179   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
180   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
181   RT_TRANSACTION_CANCEL, /* __transaction_cancel */
182 
183   RT_CO_YIELD /* co_yield */
184 };
185 
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187    reverting it on destruction.  */
188 
189 class type_id_in_expr_sentinel
190 {
191   cp_parser *parser;
192   bool saved;
193 public:
type_id_in_expr_sentinel(cp_parser * parser,bool set=true)194   type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195     : parser (parser),
196       saved (parser->in_type_id_in_expr_p)
197   { parser->in_type_id_in_expr_p = set; }
~type_id_in_expr_sentinel()198   ~type_id_in_expr_sentinel ()
199   { parser->in_type_id_in_expr_p = saved; }
200 };
201 
202 /* Prototypes.  */
203 
204 static cp_lexer *cp_lexer_new_main
205   (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207   (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209   (cp_lexer *);
210 static int cp_lexer_saving_tokens
211   (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213   (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215   (unsigned, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217   (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219   (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221   (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223   (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225   (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227   (cp_lexer *);
228 static void cp_lexer_purge_token
229   (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231   (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233   (cp_lexer *);
234 static void cp_lexer_commit_tokens
235   (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237   (cp_lexer *);
238 static void cp_lexer_print_token
239   (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241   (cp_lexer *);
242 static void cp_lexer_start_debugging
243   (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245   (cp_lexer *) ATTRIBUTE_UNUSED;
246 
247 static cp_token_cache *cp_token_cache_new
248   (cp_token *, cp_token *);
249 static tree cp_parser_late_noexcept_specifier
250   (cp_parser *, tree);
251 static void noexcept_override_late_checks
252   (tree);
253 
254 static void cp_parser_initial_pragma
255   (cp_token *);
256 
257 static bool cp_parser_omp_declare_reduction_exprs
258   (tree, cp_parser *);
259 static void cp_finalize_oacc_routine
260   (cp_parser *, tree, bool);
261 
262 /* Manifest constants.  */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
265 
266 /* Variables.  */
267 
268 /* The stream to which debugging output should be written.  */
269 static FILE *cp_lexer_debug_stream;
270 
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272    sizeof, typeof, or alignof.  */
273 int cp_unevaluated_operand;
274 
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
277    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
278    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279    highlighted by surrounding it in [[ ]].  */
280 
281 static void
cp_lexer_dump_tokens(FILE * file,vec<cp_token,va_gc> * buffer,cp_token * start_token,unsigned num,cp_token * curr_token)282 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
283 		      cp_token *start_token, unsigned num,
284 		      cp_token *curr_token)
285 {
286   unsigned i, nprinted;
287   cp_token *token;
288   bool do_print;
289 
290   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
291 
292   if (buffer == NULL)
293     return;
294 
295   if (num == 0)
296     num = buffer->length ();
297 
298   if (start_token == NULL)
299     start_token = buffer->address ();
300 
301   if (start_token > buffer->address ())
302     {
303       cp_lexer_print_token (file, &(*buffer)[0]);
304       fprintf (file, " ... ");
305     }
306 
307   do_print = false;
308   nprinted = 0;
309   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
310     {
311       if (token == start_token)
312 	do_print = true;
313 
314       if (!do_print)
315 	continue;
316 
317       nprinted++;
318       if (token == curr_token)
319 	fprintf (file, "[[");
320 
321       cp_lexer_print_token (file, token);
322 
323       if (token == curr_token)
324 	fprintf (file, "]]");
325 
326       switch (token->type)
327 	{
328 	  case CPP_SEMICOLON:
329 	  case CPP_OPEN_BRACE:
330 	  case CPP_CLOSE_BRACE:
331 	  case CPP_EOF:
332 	    fputc ('\n', file);
333 	    break;
334 
335 	  default:
336 	    fputc (' ', file);
337 	}
338     }
339 
340   if (i == num && i < buffer->length ())
341     {
342       fprintf (file, " ... ");
343       cp_lexer_print_token (file, &buffer->last ());
344     }
345 
346   fprintf (file, "\n");
347 }
348 
349 
350 /* Dump all tokens in BUFFER to stderr.  */
351 
352 void
cp_lexer_debug_tokens(vec<cp_token,va_gc> * buffer)353 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
354 {
355   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
356 }
357 
358 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> & ref)359 debug (vec<cp_token, va_gc> &ref)
360 {
361   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
362 }
363 
364 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> * ptr)365 debug (vec<cp_token, va_gc> *ptr)
366 {
367   if (ptr)
368     debug (*ptr);
369   else
370     fprintf (stderr, "<nil>\n");
371 }
372 
373 
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
375    description for T.  */
376 
377 static void
cp_debug_print_tree_if_set(FILE * file,const char * desc,tree t)378 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
379 {
380   if (t)
381     {
382       fprintf (file, "%s: ", desc);
383       print_node_brief (file, "", t, 0);
384     }
385 }
386 
387 
388 /* Dump parser context C to FILE.  */
389 
390 static void
cp_debug_print_context(FILE * file,cp_parser_context * c)391 cp_debug_print_context (FILE *file, cp_parser_context *c)
392 {
393   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
394   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
395   print_node_brief (file, "", c->object_type, 0);
396   fprintf (file, "}\n");
397 }
398 
399 
400 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
401 
402 static void
cp_debug_print_context_stack(FILE * file,cp_parser_context * first)403 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
404 {
405   unsigned i;
406   cp_parser_context *c;
407 
408   fprintf (file, "Parsing context stack:\n");
409   for (i = 0, c = first; c; c = c->next, i++)
410     {
411       fprintf (file, "\t#%u: ", i);
412       cp_debug_print_context (file, c);
413     }
414 }
415 
416 
417 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
418 
419 static void
cp_debug_print_flag(FILE * file,const char * desc,bool flag)420 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
421 {
422   if (flag)
423     fprintf (file, "%s: true\n", desc);
424 }
425 
426 
427 /* Print an unparsed function entry UF to FILE.  */
428 
429 static void
cp_debug_print_unparsed_function(FILE * file,cp_unparsed_functions_entry * uf)430 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
431 {
432   unsigned i;
433   cp_default_arg_entry *default_arg_fn;
434   tree fn;
435 
436   fprintf (file, "\tFunctions with default args:\n");
437   for (i = 0;
438        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
439        i++)
440     {
441       fprintf (file, "\t\tClass type: ");
442       print_node_brief (file, "", default_arg_fn->class_type, 0);
443       fprintf (file, "\t\tDeclaration: ");
444       print_node_brief (file, "", default_arg_fn->decl, 0);
445       fprintf (file, "\n");
446     }
447 
448   fprintf (file, "\n\tFunctions with definitions that require "
449 	   "post-processing\n\t\t");
450   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
451     {
452       print_node_brief (file, "", fn, 0);
453       fprintf (file, " ");
454     }
455   fprintf (file, "\n");
456 
457   fprintf (file, "\n\tNon-static data members with initializers that require "
458            "post-processing\n\t\t");
459   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
460     {
461       print_node_brief (file, "", fn, 0);
462       fprintf (file, " ");
463     }
464   fprintf (file, "\n");
465 }
466 
467 
468 /* Print the stack of unparsed member functions S to FILE.  */
469 
470 static void
cp_debug_print_unparsed_queues(FILE * file,vec<cp_unparsed_functions_entry,va_gc> * s)471 cp_debug_print_unparsed_queues (FILE *file,
472 				vec<cp_unparsed_functions_entry, va_gc> *s)
473 {
474   unsigned i;
475   cp_unparsed_functions_entry *uf;
476 
477   fprintf (file, "Unparsed functions\n");
478   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
479     {
480       fprintf (file, "#%u:\n", i);
481       cp_debug_print_unparsed_function (file, uf);
482     }
483 }
484 
485 
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
488 
489 static void
cp_debug_parser_tokens(FILE * file,cp_parser * parser,int window_size)490 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
491 {
492   cp_token *next_token, *first_token, *start_token;
493 
494   if (file == NULL)
495     file = stderr;
496 
497   next_token = parser->lexer->next_token;
498   first_token = parser->lexer->buffer->address ();
499   start_token = (next_token > first_token + window_size / 2)
500 		? next_token - window_size / 2
501 		: first_token;
502   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
503 			next_token);
504 }
505 
506 
507 /* Dump debugging information for the given PARSER.  If FILE is NULL,
508    the output is printed on stderr.  */
509 
510 void
cp_debug_parser(FILE * file,cp_parser * parser)511 cp_debug_parser (FILE *file, cp_parser *parser)
512 {
513   const size_t window_size = 20;
514   cp_token *token;
515   expanded_location eloc;
516 
517   if (file == NULL)
518     file = stderr;
519 
520   fprintf (file, "Parser state\n\n");
521   fprintf (file, "Number of tokens: %u\n",
522 	   vec_safe_length (parser->lexer->buffer));
523   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
524   cp_debug_print_tree_if_set (file, "Object scope",
525 				     parser->object_scope);
526   cp_debug_print_tree_if_set (file, "Qualifying scope",
527 				     parser->qualifying_scope);
528   cp_debug_print_context_stack (file, parser->context);
529   cp_debug_print_flag (file, "Allow GNU extensions",
530 			      parser->allow_gnu_extensions_p);
531   cp_debug_print_flag (file, "'>' token is greater-than",
532 			      parser->greater_than_is_operator_p);
533   cp_debug_print_flag (file, "Default args allowed in current "
534 			      "parameter list", parser->default_arg_ok_p);
535   cp_debug_print_flag (file, "Parsing integral constant-expression",
536 			      parser->integral_constant_expression_p);
537   cp_debug_print_flag (file, "Allow non-constant expression in current "
538 			      "constant-expression",
539 			      parser->allow_non_integral_constant_expression_p);
540   cp_debug_print_flag (file, "Seen non-constant expression",
541 			      parser->non_integral_constant_expression_p);
542   cp_debug_print_flag (file, "Local names forbidden in current context",
543 			      (parser->local_variables_forbidden_p
544 			       & LOCAL_VARS_FORBIDDEN));
545   cp_debug_print_flag (file, "'this' forbidden in current context",
546 			      (parser->local_variables_forbidden_p
547 			       & THIS_FORBIDDEN));
548   cp_debug_print_flag (file, "In unbraced linkage specification",
549 			      parser->in_unbraced_linkage_specification_p);
550   cp_debug_print_flag (file, "Parsing a declarator",
551 			      parser->in_declarator_p);
552   cp_debug_print_flag (file, "In template argument list",
553 			      parser->in_template_argument_list_p);
554   cp_debug_print_flag (file, "Parsing an iteration statement",
555 			      parser->in_statement & IN_ITERATION_STMT);
556   cp_debug_print_flag (file, "Parsing a switch statement",
557 			      parser->in_statement & IN_SWITCH_STMT);
558   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 			      parser->in_statement & IN_OMP_BLOCK);
560   cp_debug_print_flag (file, "Parsing an OpenMP loop",
561 			      parser->in_statement & IN_OMP_FOR);
562   cp_debug_print_flag (file, "Parsing an if statement",
563 			      parser->in_statement & IN_IF_STMT);
564   cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 			      "context", parser->in_type_id_in_expr_p);
566   cp_debug_print_flag (file, "String expressions should be translated "
567 			      "to execution character set",
568 			      parser->translate_strings_p);
569   cp_debug_print_flag (file, "Parsing function body outside of a "
570 			      "local class", parser->in_function_body);
571   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
572 			      parser->colon_corrects_to_scope_p);
573   cp_debug_print_flag (file, "Colon doesn't start a class definition",
574 			      parser->colon_doesnt_start_class_def_p);
575   cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
576 			      parser->objective_c_message_context_p);
577   if (parser->type_definition_forbidden_message)
578     fprintf (file, "Error message for forbidden type definitions: %s %s\n",
579 	     parser->type_definition_forbidden_message,
580 	     parser->type_definition_forbidden_message_arg
581 	     ? parser->type_definition_forbidden_message_arg : "<none>");
582   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583   fprintf (file, "Number of class definitions in progress: %u\n",
584 	   parser->num_classes_being_defined);
585   fprintf (file, "Number of template parameter lists for the current "
586 	   "declaration: %u\n", parser->num_template_parameter_lists);
587   cp_debug_parser_tokens (file, parser, window_size);
588   token = parser->lexer->next_token;
589   fprintf (file, "Next token to parse:\n");
590   fprintf (file, "\tToken:  ");
591   cp_lexer_print_token (file, token);
592   eloc = expand_location (token->location);
593   fprintf (file, "\n\tFile:   %s\n", eloc.file);
594   fprintf (file, "\tLine:   %d\n", eloc.line);
595   fprintf (file, "\tColumn: %d\n", eloc.column);
596 }
597 
598 DEBUG_FUNCTION void
debug(cp_parser & ref)599 debug (cp_parser &ref)
600 {
601   cp_debug_parser (stderr, &ref);
602 }
603 
604 DEBUG_FUNCTION void
debug(cp_parser * ptr)605 debug (cp_parser *ptr)
606 {
607   if (ptr)
608     debug (*ptr);
609   else
610     fprintf (stderr, "<nil>\n");
611 }
612 
613 /* Allocate memory for a new lexer object and return it.  */
614 
615 static cp_lexer *
cp_lexer_alloc(void)616 cp_lexer_alloc (void)
617 {
618   /* Allocate the memory.  */
619   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
620 
621   /* Initially we are not debugging.  */
622   lexer->debugging_p = false;
623 
624   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
625 
626   /* Create the buffer.  */
627   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
628 
629   return lexer;
630 }
631 
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
633    preprocessor.  */
634 
635 static cp_lexer *
cp_lexer_new_main(void)636 cp_lexer_new_main (void)
637 {
638   cp_token token;
639 
640   /* It's possible that parsing the first pragma will load a PCH file,
641      which is a GC collection point.  So we have to do that before
642      allocating any memory.  */
643   cp_lexer_get_preprocessor_token (0, &token);
644   cp_parser_initial_pragma (&token);
645   c_common_no_more_pch ();
646 
647   cp_lexer *lexer = cp_lexer_alloc ();
648   /* Put the first token in the buffer.  */
649   cp_token *tok = lexer->buffer->quick_push (token);
650 
651   uintptr_t filter = 0;
652   if (modules_p ())
653     filter = module_token_cdtor (parse_in, filter);
654 
655   /* Get the remaining tokens from the preprocessor.  */
656   while (tok->type != CPP_EOF)
657     {
658       if (filter)
659 	/* Process the previous token.  */
660 	module_token_lang (tok->type, tok->keyword, tok->u.value,
661 			   tok->location, filter);
662       tok = vec_safe_push (lexer->buffer, cp_token ());
663       cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
664     }
665 
666   lexer->next_token = lexer->buffer->address ();
667   lexer->last_token = lexer->next_token
668                       + lexer->buffer->length ()
669 		      - 1;
670 
671   if (lexer->buffer->length () != 1)
672     {
673       /* Set the EOF token's location to be the just after the previous
674          token's range.  That way 'at-eof' diagnostics point at something
675 	 meaninful.  */
676       auto range = get_range_from_loc (line_table, tok[-1].location);
677       tok[0].location
678 	= linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
679     }
680 
681   if (filter)
682     module_token_cdtor (parse_in, filter);
683 
684   /* Subsequent preprocessor diagnostics should use compiler
685      diagnostic functions to get the compiler source location.  */
686   done_lexing = true;
687 
688   maybe_check_all_macros (parse_in);
689 
690   gcc_assert (!lexer->next_token->purged_p);
691   return lexer;
692 }
693 
694 /* Create a new lexer whose token stream is primed with the tokens in
695    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
696 
697 static cp_lexer *
cp_lexer_new_from_tokens(cp_token_cache * cache)698 cp_lexer_new_from_tokens (cp_token_cache *cache)
699 {
700   cp_token *first = cache->first;
701   cp_token *last = cache->last;
702   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
703 
704   /* We do not own the buffer.  */
705   lexer->buffer = NULL;
706 
707   /* Insert an EOF token.  */
708   lexer->saved_type = last->type;
709   lexer->saved_keyword = last->keyword;
710   last->type = CPP_EOF;
711   last->keyword = RID_MAX;
712 
713   lexer->next_token = first;
714   lexer->last_token = last;
715 
716   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
717 
718   /* Initially we are not debugging.  */
719   lexer->debugging_p = false;
720 
721   gcc_assert (!lexer->next_token->purged_p
722 	      && !lexer->last_token->purged_p);
723   return lexer;
724 }
725 
726 /* Frees all resources associated with LEXER.  */
727 
728 static void
cp_lexer_destroy(cp_lexer * lexer)729 cp_lexer_destroy (cp_lexer *lexer)
730 {
731   if (lexer->buffer)
732     vec_free (lexer->buffer);
733   else
734     {
735       /* Restore the token we overwrite with EOF.  */
736       lexer->last_token->type = lexer->saved_type;
737       lexer->last_token->keyword = lexer->saved_keyword;
738     }
739   lexer->saved_tokens.release ();
740   ggc_free (lexer);
741 }
742 
743 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
744    be used.  The point of this flag is to help the compiler to fold away calls
745    to cp_lexer_debugging_p within this source file at compile time, when the
746    lexer is not being debugged.  */
747 
748 #define LEXER_DEBUGGING_ENABLED_P false
749 
750 /* Returns nonzero if debugging information should be output.  */
751 
752 static inline bool
cp_lexer_debugging_p(cp_lexer * lexer)753 cp_lexer_debugging_p (cp_lexer *lexer)
754 {
755   if (!LEXER_DEBUGGING_ENABLED_P)
756     return false;
757 
758   return lexer->debugging_p;
759 }
760 
761 
762 static inline cp_token_position
cp_lexer_token_position(cp_lexer * lexer,bool previous_p)763 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
764 {
765   return lexer->next_token - previous_p;
766 }
767 
768 static inline cp_token *
cp_lexer_token_at(cp_lexer *,cp_token_position pos)769 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
770 {
771   return pos;
772 }
773 
774 static inline void
cp_lexer_set_token_position(cp_lexer * lexer,cp_token_position pos)775 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
776 {
777   lexer->next_token = cp_lexer_token_at (lexer, pos);
778 }
779 
780 static inline cp_token_position
cp_lexer_previous_token_position(cp_lexer * lexer)781 cp_lexer_previous_token_position (cp_lexer *lexer)
782 {
783   return cp_lexer_token_position (lexer, true);
784 }
785 
786 static inline cp_token *
cp_lexer_previous_token(cp_lexer * lexer)787 cp_lexer_previous_token (cp_lexer *lexer)
788 {
789   cp_token_position tp = cp_lexer_previous_token_position (lexer);
790 
791   /* Skip past purged tokens.  */
792   while (tp->purged_p)
793     {
794       gcc_assert (tp != vec_safe_address (lexer->buffer));
795       tp--;
796     }
797 
798   return cp_lexer_token_at (lexer, tp);
799 }
800 
801 /* Same as above, but return NULL when the lexer doesn't own the token
802    buffer or if the next_token is at the start of the token
803    vector or if all previous tokens are purged.  */
804 
805 static cp_token *
cp_lexer_safe_previous_token(cp_lexer * lexer)806 cp_lexer_safe_previous_token (cp_lexer *lexer)
807 {
808   if (lexer->buffer
809       && lexer->next_token != lexer->buffer->address ())
810     {
811       cp_token_position tp = cp_lexer_previous_token_position (lexer);
812 
813       /* Skip past purged tokens.  */
814       while (tp->purged_p)
815 	{
816 	  if (tp == lexer->buffer->address ())
817 	    return NULL;
818 	  tp--;
819 	}
820       return cp_lexer_token_at (lexer, tp);
821     }
822 
823   return NULL;
824 }
825 
826 /* Overload for make_location, taking the lexer to mean the location of the
827    previous token.  */
828 
829 static inline location_t
make_location(location_t caret,location_t start,cp_lexer * lexer)830 make_location (location_t caret, location_t start, cp_lexer *lexer)
831 {
832   cp_token *t = cp_lexer_previous_token (lexer);
833   return make_location (caret, start, t->location);
834 }
835 
836 /* Overload for make_location taking tokens instead of locations.  */
837 
838 static inline location_t
make_location(cp_token * caret,cp_token * start,cp_token * end)839 make_location (cp_token *caret, cp_token *start, cp_token *end)
840 {
841   return make_location (caret->location, start->location, end->location);
842 }
843 
844 /* nonzero if we are presently saving tokens.  */
845 
846 static inline int
cp_lexer_saving_tokens(const cp_lexer * lexer)847 cp_lexer_saving_tokens (const cp_lexer* lexer)
848 {
849   return lexer->saved_tokens.length () != 0;
850 }
851 
852 /* Store the next token from the preprocessor in *TOKEN.  Return true
853    if we reach EOF.  If LEXER is NULL, assume we are handling an
854    initial #pragma pch_preprocess, and thus want the lexer to return
855    processed strings.  */
856 
857 static void
cp_lexer_get_preprocessor_token(unsigned flags,cp_token * token)858 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
859 {
860   static int is_extern_c = 0;
861 
862    /* Get a new token from the preprocessor.  */
863   token->type
864     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
865 			flags);
866   token->keyword = RID_MAX;
867   token->purged_p = false;
868   token->error_reported = false;
869   token->tree_check_p = false;
870   /* Usually never see a zero, but just in case ... */
871   token->main_source_p = line_table->depth <= 1;
872 
873   /* On some systems, some header files are surrounded by an
874      implicit extern "C" block.  Set a flag in the token if it
875      comes from such a header.  */
876   is_extern_c += pending_lang_change;
877   pending_lang_change = 0;
878   token->implicit_extern_c = is_extern_c > 0;
879 
880   /* Check to see if this token is a keyword.  */
881   if (token->type == CPP_NAME)
882     {
883       if (IDENTIFIER_KEYWORD_P (token->u.value))
884 	{
885 	  /* Mark this token as a keyword.  */
886 	  token->type = CPP_KEYWORD;
887 	  /* Record which keyword.  */
888 	  token->keyword = C_RID_CODE (token->u.value);
889 	}
890       else
891 	{
892           if (warn_cxx11_compat
893               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
894               && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
895             {
896               /* Warn about the C++0x keyword (but still treat it as
897                  an identifier).  */
898 	      warning_at (token->location, OPT_Wc__11_compat,
899 			  "identifier %qE is a keyword in C++11",
900 			  token->u.value);
901 
902               /* Clear out the C_RID_CODE so we don't warn about this
903                  particular identifier-turned-keyword again.  */
904               C_SET_RID_CODE (token->u.value, RID_MAX);
905             }
906 	  if (warn_cxx20_compat
907 	      && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
908 	      && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
909 	    {
910 	      /* Warn about the C++20 keyword (but still treat it as
911 		 an identifier).  */
912 	      warning_at (token->location, OPT_Wc__20_compat,
913 			  "identifier %qE is a keyword in C++20",
914 			  token->u.value);
915 
916 	      /* Clear out the C_RID_CODE so we don't warn about this
917 		 particular identifier-turned-keyword again.  */
918 	      C_SET_RID_CODE (token->u.value, RID_MAX);
919 	    }
920 
921 	  token->keyword = RID_MAX;
922 	}
923     }
924   else if (token->type == CPP_AT_NAME)
925     {
926       /* This only happens in Objective-C++; it must be a keyword.  */
927       token->type = CPP_KEYWORD;
928       switch (C_RID_CODE (token->u.value))
929 	{
930 	  /* Replace 'class' with '@class', 'private' with '@private',
931 	     etc.  This prevents confusion with the C++ keyword
932 	     'class', and makes the tokens consistent with other
933 	     Objective-C 'AT' keywords.  For example '@class' is
934 	     reported as RID_AT_CLASS which is consistent with
935 	     '@synchronized', which is reported as
936 	     RID_AT_SYNCHRONIZED.
937 	  */
938 	case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
939 	case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
940 	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
941 	case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
942 	case RID_THROW:     token->keyword = RID_AT_THROW; break;
943 	case RID_TRY:       token->keyword = RID_AT_TRY; break;
944 	case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
945 	case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
946 	default:            token->keyword = C_RID_CODE (token->u.value);
947 	}
948     }
949 }
950 
951 /* Update the globals input_location and the input file stack from TOKEN.  */
952 static inline void
cp_lexer_set_source_position_from_token(cp_token * token)953 cp_lexer_set_source_position_from_token (cp_token *token)
954 {
955   input_location = token->location;
956 }
957 
958 /* Update the globals input_location and the input file stack from LEXER.  */
959 static inline void
cp_lexer_set_source_position(cp_lexer * lexer)960 cp_lexer_set_source_position (cp_lexer *lexer)
961 {
962   cp_token *token = cp_lexer_peek_token (lexer);
963   cp_lexer_set_source_position_from_token (token);
964 }
965 
966 /* Return a pointer to the next token in the token stream, but do not
967    consume it.  */
968 
969 static inline cp_token *
cp_lexer_peek_token(cp_lexer * lexer)970 cp_lexer_peek_token (cp_lexer *lexer)
971 {
972   if (cp_lexer_debugging_p (lexer))
973     {
974       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
975       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
976       putc ('\n', cp_lexer_debug_stream);
977     }
978   return lexer->next_token;
979 }
980 
981 /* Return true if the next token has the indicated TYPE.  */
982 
983 static inline bool
cp_lexer_next_token_is(cp_lexer * lexer,enum cpp_ttype type)984 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
985 {
986   return cp_lexer_peek_token (lexer)->type == type;
987 }
988 
989 /* Return true if the next token does not have the indicated TYPE.  */
990 
991 static inline bool
cp_lexer_next_token_is_not(cp_lexer * lexer,enum cpp_ttype type)992 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
993 {
994   return !cp_lexer_next_token_is (lexer, type);
995 }
996 
997 /* Return true if the next token is the indicated KEYWORD.  */
998 
999 static inline bool
cp_lexer_next_token_is_keyword(cp_lexer * lexer,enum rid keyword)1000 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1001 {
1002   return cp_lexer_peek_token (lexer)->keyword == keyword;
1003 }
1004 
1005 static inline bool
cp_lexer_nth_token_is(cp_lexer * lexer,size_t n,enum cpp_ttype type)1006 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1007 {
1008   return cp_lexer_peek_nth_token (lexer, n)->type == type;
1009 }
1010 
1011 static inline bool
cp_lexer_nth_token_is_keyword(cp_lexer * lexer,size_t n,enum rid keyword)1012 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1013 {
1014   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1015 }
1016 
1017 /* Return true if KEYWORD can start a decl-specifier.  */
1018 
1019 bool
cp_keyword_starts_decl_specifier_p(enum rid keyword)1020 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1021 {
1022   switch (keyword)
1023     {
1024       /* auto specifier: storage-class-specifier in C++,
1025          simple-type-specifier in C++0x.  */
1026     case RID_AUTO:
1027       /* Storage classes.  */
1028     case RID_REGISTER:
1029     case RID_STATIC:
1030     case RID_EXTERN:
1031     case RID_MUTABLE:
1032     case RID_THREAD:
1033       /* Elaborated type specifiers.  */
1034     case RID_ENUM:
1035     case RID_CLASS:
1036     case RID_STRUCT:
1037     case RID_UNION:
1038     case RID_TYPENAME:
1039       /* Simple type specifiers.  */
1040     case RID_CHAR:
1041     case RID_CHAR8:
1042     case RID_CHAR16:
1043     case RID_CHAR32:
1044     case RID_WCHAR:
1045     case RID_BOOL:
1046     case RID_SHORT:
1047     case RID_INT:
1048     case RID_LONG:
1049     case RID_SIGNED:
1050     case RID_UNSIGNED:
1051     case RID_FLOAT:
1052     case RID_DOUBLE:
1053     case RID_VOID:
1054       /* CV qualifiers.  */
1055     case RID_CONST:
1056     case RID_VOLATILE:
1057       /* Function specifiers.  */
1058     case RID_EXPLICIT:
1059     case RID_VIRTUAL:
1060       /* friend/typdef/inline specifiers.  */
1061     case RID_FRIEND:
1062     case RID_TYPEDEF:
1063     case RID_INLINE:
1064       /* GNU extensions.  */
1065     case RID_TYPEOF:
1066       /* C++11 extensions.  */
1067     case RID_DECLTYPE:
1068     case RID_UNDERLYING_TYPE:
1069     case RID_CONSTEXPR:
1070       /* C++20 extensions.  */
1071     case RID_CONSTINIT:
1072     case RID_CONSTEVAL:
1073       return true;
1074 
1075     default:
1076       if (keyword >= RID_FIRST_INT_N
1077 	  && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1078 	  && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1079 	return true;
1080       return false;
1081     }
1082 }
1083 
1084 /* Return true if the next token is a keyword for a decl-specifier.  */
1085 
1086 static bool
cp_lexer_next_token_is_decl_specifier_keyword(cp_lexer * lexer)1087 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1088 {
1089   cp_token *token;
1090 
1091   token = cp_lexer_peek_token (lexer);
1092   return cp_keyword_starts_decl_specifier_p (token->keyword);
1093 }
1094 
1095 /* Returns TRUE iff the token T begins a decltype type.  */
1096 
1097 static bool
token_is_decltype(cp_token * t)1098 token_is_decltype (cp_token *t)
1099 {
1100   return (t->keyword == RID_DECLTYPE
1101 	  || t->type == CPP_DECLTYPE);
1102 }
1103 
1104 /* Returns TRUE iff the next token begins a decltype type.  */
1105 
1106 static bool
cp_lexer_next_token_is_decltype(cp_lexer * lexer)1107 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1108 {
1109   cp_token *t = cp_lexer_peek_token (lexer);
1110   return token_is_decltype (t);
1111 }
1112 
1113 /* Called when processing a token with tree_check_value; perform or defer the
1114    associated checks and return the value.  */
1115 
1116 static tree
saved_checks_value(struct tree_check * check_value)1117 saved_checks_value (struct tree_check *check_value)
1118 {
1119   /* Perform any access checks that were deferred.  */
1120   vec<deferred_access_check, va_gc> *checks;
1121   deferred_access_check *chk;
1122   checks = check_value->checks;
1123   if (checks)
1124     {
1125       int i;
1126       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1127 	perform_or_defer_access_check (chk->binfo,
1128 				       chk->decl,
1129 				       chk->diag_decl, tf_warning_or_error);
1130     }
1131   /* Return the stored value.  */
1132   return check_value->value;
1133 }
1134 
1135 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1136    then this is precisely equivalent to cp_lexer_peek_token (except
1137    that it is not inline).  One would like to disallow that case, but
1138    there is one case (cp_parser_nth_token_starts_template_id) where
1139    the caller passes a variable for N and it might be 1.  */
1140 
1141 static cp_token *
cp_lexer_peek_nth_token(cp_lexer * lexer,size_t n)1142 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1143 {
1144   cp_token *token;
1145 
1146   /* N is 1-based, not zero-based.  */
1147   gcc_assert (n > 0);
1148 
1149   if (cp_lexer_debugging_p (lexer))
1150     fprintf (cp_lexer_debug_stream,
1151 	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
1152 
1153   --n;
1154   token = lexer->next_token;
1155   while (n && token->type != CPP_EOF)
1156     {
1157       ++token;
1158       if (!token->purged_p)
1159 	--n;
1160     }
1161 
1162   if (cp_lexer_debugging_p (lexer))
1163     {
1164       cp_lexer_print_token (cp_lexer_debug_stream, token);
1165       putc ('\n', cp_lexer_debug_stream);
1166     }
1167 
1168   return token;
1169 }
1170 
1171 /* Return the next token, and advance the lexer's next_token pointer
1172    to point to the next non-purged token.  */
1173 
1174 static cp_token *
cp_lexer_consume_token(cp_lexer * lexer)1175 cp_lexer_consume_token (cp_lexer* lexer)
1176 {
1177   cp_token *token = lexer->next_token;
1178 
1179   do
1180     {
1181       gcc_assert (token->type != CPP_EOF);
1182       lexer->next_token++;
1183     }
1184   while (lexer->next_token->purged_p);
1185 
1186   cp_lexer_set_source_position_from_token (token);
1187 
1188   /* Provide debugging output.  */
1189   if (cp_lexer_debugging_p (lexer))
1190     {
1191       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1192       cp_lexer_print_token (cp_lexer_debug_stream, token);
1193       putc ('\n', cp_lexer_debug_stream);
1194     }
1195 
1196   return token;
1197 }
1198 
1199 /* Permanently remove the next token from the token stream, and
1200    advance the next_token pointer to refer to the next non-purged
1201    token.  */
1202 
1203 static void
cp_lexer_purge_token(cp_lexer * lexer)1204 cp_lexer_purge_token (cp_lexer *lexer)
1205 {
1206   cp_token *tok = lexer->next_token;
1207 
1208   gcc_assert (tok->type != CPP_EOF);
1209   tok->purged_p = true;
1210   tok->location = UNKNOWN_LOCATION;
1211   tok->u.value = NULL_TREE;
1212   tok->keyword = RID_MAX;
1213 
1214   do
1215     tok++;
1216   while (tok->purged_p);
1217   lexer->next_token = tok;
1218 }
1219 
1220 /* Permanently remove all tokens after TOK, up to, but not
1221    including, the token that will be returned next by
1222    cp_lexer_peek_token.  */
1223 
1224 static void
cp_lexer_purge_tokens_after(cp_lexer * lexer,cp_token * tok)1225 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1226 {
1227   cp_token *peek = lexer->next_token;
1228 
1229   gcc_assert (tok < peek);
1230 
1231   for (tok++; tok != peek; tok++)
1232     {
1233       tok->purged_p = true;
1234       tok->location = UNKNOWN_LOCATION;
1235       tok->u.value = NULL_TREE;
1236       tok->keyword = RID_MAX;
1237     }
1238 }
1239 
1240 /* Begin saving tokens.  All tokens consumed after this point will be
1241    preserved.  */
1242 
1243 static void
cp_lexer_save_tokens(cp_lexer * lexer)1244 cp_lexer_save_tokens (cp_lexer* lexer)
1245 {
1246   /* Provide debugging output.  */
1247   if (cp_lexer_debugging_p (lexer))
1248     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1249 
1250   lexer->saved_tokens.safe_push (lexer->next_token);
1251 }
1252 
1253 /* Commit to the portion of the token stream most recently saved.  */
1254 
1255 static void
cp_lexer_commit_tokens(cp_lexer * lexer)1256 cp_lexer_commit_tokens (cp_lexer* lexer)
1257 {
1258   /* Provide debugging output.  */
1259   if (cp_lexer_debugging_p (lexer))
1260     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1261 
1262   lexer->saved_tokens.pop ();
1263 }
1264 
1265 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1266    to the token stream.  Stop saving tokens.  */
1267 
1268 static void
cp_lexer_rollback_tokens(cp_lexer * lexer)1269 cp_lexer_rollback_tokens (cp_lexer* lexer)
1270 {
1271   /* Provide debugging output.  */
1272   if (cp_lexer_debugging_p (lexer))
1273     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1274 
1275   lexer->next_token = lexer->saved_tokens.pop ();
1276 }
1277 
1278 /* Determines what saved_token_sentinel does when going out of scope.  */
1279 
1280 enum saved_token_sentinel_mode {
1281   STS_COMMIT,
1282   STS_ROLLBACK,
1283   STS_DONOTHING
1284 };
1285 
1286 /* RAII wrapper around the above functions, with sanity checking (the token
1287    stream should be the same at the point of instantiation as it is at the
1288    point of destruction).
1289 
1290    Creating a variable saves tokens.  MODE determines what happens when the
1291    object is destroyed.  STS_COMMIT commits tokens (default),
1292    STS_ROLLBACK rolls-back and STS_DONOTHING does nothing.  Calling
1293    rollback() will immediately roll-back tokens and set MODE to
1294    STS_DONOTHING.  */
1295 
1296 struct saved_token_sentinel
1297 {
1298   cp_lexer *lexer;
1299   unsigned len;
1300   saved_token_sentinel_mode mode;
saved_token_sentinelsaved_token_sentinel1301   saved_token_sentinel (cp_lexer *_lexer,
1302 			saved_token_sentinel_mode _mode = STS_COMMIT)
1303     : lexer (_lexer), mode (_mode)
1304   {
1305     len = lexer->saved_tokens.length ();
1306     cp_lexer_save_tokens (lexer);
1307   }
rollbacksaved_token_sentinel1308   void rollback ()
1309   {
1310     cp_lexer_rollback_tokens (lexer);
1311     cp_lexer_set_source_position_from_token
1312       (cp_lexer_previous_token (lexer));
1313     mode = STS_DONOTHING;
1314   }
~saved_token_sentinelsaved_token_sentinel1315   ~saved_token_sentinel ()
1316   {
1317     if (mode == STS_COMMIT)
1318       cp_lexer_commit_tokens (lexer);
1319     else if (mode == STS_ROLLBACK)
1320       rollback ();
1321 
1322     gcc_assert (lexer->saved_tokens.length () == len);
1323   }
1324 };
1325 
1326 /* Print a representation of the TOKEN on the STREAM.  */
1327 
1328 static void
cp_lexer_print_token(FILE * stream,cp_token * token)1329 cp_lexer_print_token (FILE * stream, cp_token *token)
1330 {
1331   /* We don't use cpp_type2name here because the parser defines
1332      a few tokens of its own.  */
1333   static const char *const token_names[] = {
1334     /* cpplib-defined token types */
1335 #define OP(e, s) #e,
1336 #define TK(e, s) #e,
1337     TTYPE_TABLE
1338 #undef OP
1339 #undef TK
1340     /* C++ parser token types - see "Manifest constants", above.  */
1341     "KEYWORD",
1342     "TEMPLATE_ID",
1343     "NESTED_NAME_SPECIFIER",
1344   };
1345 
1346   /* For some tokens, print the associated data.  */
1347   switch (token->type)
1348     {
1349     case CPP_KEYWORD:
1350       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1351 	 For example, `struct' is mapped to an INTEGER_CST.  */
1352       if (!identifier_p (token->u.value))
1353 	break;
1354       /* fall through */
1355     case CPP_NAME:
1356       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1357       break;
1358 
1359     case CPP_STRING:
1360     case CPP_STRING16:
1361     case CPP_STRING32:
1362     case CPP_WSTRING:
1363     case CPP_UTF8STRING:
1364       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1365       break;
1366 
1367     case CPP_NUMBER:
1368       print_generic_expr (stream, token->u.value);
1369       break;
1370 
1371     default:
1372       /* If we have a name for the token, print it out.  Otherwise, we
1373 	 simply give the numeric code.  */
1374       if (token->type < ARRAY_SIZE(token_names))
1375 	fputs (token_names[token->type], stream);
1376       else
1377 	fprintf (stream, "[%d]", token->type);
1378       break;
1379     }
1380 }
1381 
1382 DEBUG_FUNCTION void
debug(cp_token & ref)1383 debug (cp_token &ref)
1384 {
1385   cp_lexer_print_token (stderr, &ref);
1386   fprintf (stderr, "\n");
1387 }
1388 
1389 DEBUG_FUNCTION void
debug(cp_token * ptr)1390 debug (cp_token *ptr)
1391 {
1392   if (ptr)
1393     debug (*ptr);
1394   else
1395     fprintf (stderr, "<nil>\n");
1396 }
1397 
1398 
1399 /* Start emitting debugging information.  */
1400 
1401 static void
cp_lexer_start_debugging(cp_lexer * lexer)1402 cp_lexer_start_debugging (cp_lexer* lexer)
1403 {
1404   if (!LEXER_DEBUGGING_ENABLED_P)
1405     fatal_error (input_location,
1406 		 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1407 
1408   lexer->debugging_p = true;
1409   cp_lexer_debug_stream = stderr;
1410 }
1411 
1412 /* Stop emitting debugging information.  */
1413 
1414 static void
cp_lexer_stop_debugging(cp_lexer * lexer)1415 cp_lexer_stop_debugging (cp_lexer* lexer)
1416 {
1417   if (!LEXER_DEBUGGING_ENABLED_P)
1418     fatal_error (input_location,
1419 		 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1420 
1421   lexer->debugging_p = false;
1422   cp_lexer_debug_stream = NULL;
1423 }
1424 
1425 /* Create a new cp_token_cache, representing a range of tokens.  */
1426 
1427 static cp_token_cache *
cp_token_cache_new(cp_token * first,cp_token * last)1428 cp_token_cache_new (cp_token *first, cp_token *last)
1429 {
1430   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1431   cache->first = first;
1432   cache->last = last;
1433   return cache;
1434 }
1435 
1436 /* Diagnose if #pragma omp declare simd isn't followed immediately
1437    by function declaration or definition.  */
1438 
1439 static inline void
cp_ensure_no_omp_declare_simd(cp_parser * parser)1440 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1441 {
1442   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1443     {
1444       error ("%<#pragma omp declare %s%> not immediately followed by "
1445 	     "function declaration or definition",
1446 	     parser->omp_declare_simd->variant_p ? "variant" : "simd");
1447       parser->omp_declare_simd = NULL;
1448     }
1449 }
1450 
1451 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1452    and put that into "omp declare simd" attribute.  */
1453 
1454 static inline void
cp_finalize_omp_declare_simd(cp_parser * parser,tree fndecl)1455 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1456 {
1457   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1458     {
1459       if (fndecl == error_mark_node)
1460 	{
1461 	  parser->omp_declare_simd = NULL;
1462 	  return;
1463 	}
1464       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1465 	{
1466 	  cp_ensure_no_omp_declare_simd (parser);
1467 	  return;
1468 	}
1469     }
1470 }
1471 
1472 /* Similarly, but for use in declaration parsing functions
1473    which call cp_parser_handle_directive_omp_attributes.  */
1474 
1475 static inline void
cp_finalize_omp_declare_simd(cp_parser * parser,cp_omp_declare_simd_data * data)1476 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1477 {
1478   if (parser->omp_declare_simd != data)
1479     return;
1480 
1481   if (!parser->omp_declare_simd->error_seen
1482       && !parser->omp_declare_simd->fndecl_seen)
1483     error_at (parser->omp_declare_simd->loc,
1484 	      "%<declare %s%> directive not immediately followed by "
1485 	      "function declaration or definition",
1486 	      parser->omp_declare_simd->variant_p ? "variant" : "simd");
1487   parser->omp_declare_simd = NULL;
1488 }
1489 
1490 /* Diagnose if #pragma acc routine isn't followed immediately by function
1491    declaration or definition.  */
1492 
1493 static inline void
cp_ensure_no_oacc_routine(cp_parser * parser)1494 cp_ensure_no_oacc_routine (cp_parser *parser)
1495 {
1496   if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1497     {
1498       error_at (parser->oacc_routine->loc,
1499 		"%<#pragma acc routine%> not immediately followed by "
1500 		"function declaration or definition");
1501       parser->oacc_routine = NULL;
1502     }
1503 }
1504 
1505 /* Decl-specifiers.  */
1506 
1507 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1508 
1509 static void
clear_decl_specs(cp_decl_specifier_seq * decl_specs)1510 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1511 {
1512   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1513 }
1514 
1515 /* Declarators.  */
1516 
1517 /* Nothing other than the parser should be creating declarators;
1518    declarators are a semi-syntactic representation of C++ entities.
1519    Other parts of the front end that need to create entities (like
1520    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1521 
1522 static cp_declarator *make_call_declarator
1523   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1524    tree, tree, tree, tree, location_t);
1525 static cp_declarator *make_array_declarator
1526   (cp_declarator *, tree);
1527 static cp_declarator *make_pointer_declarator
1528   (cp_cv_quals, cp_declarator *, tree);
1529 static cp_declarator *make_reference_declarator
1530   (cp_cv_quals, cp_declarator *, bool, tree);
1531 static cp_declarator *make_ptrmem_declarator
1532   (cp_cv_quals, tree, cp_declarator *, tree);
1533 
1534 /* An erroneous declarator.  */
1535 static cp_declarator *cp_error_declarator;
1536 
1537 /* The obstack on which declarators and related data structures are
1538    allocated.  */
1539 static struct obstack declarator_obstack;
1540 
1541 /* Alloc BYTES from the declarator memory pool.  */
1542 
1543 static inline void *
alloc_declarator(size_t bytes)1544 alloc_declarator (size_t bytes)
1545 {
1546   return obstack_alloc (&declarator_obstack, bytes);
1547 }
1548 
1549 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1550    common to all declarators.  */
1551 
1552 static cp_declarator *
make_declarator(cp_declarator_kind kind)1553 make_declarator (cp_declarator_kind kind)
1554 {
1555   cp_declarator *declarator;
1556 
1557   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1558   declarator->kind = kind;
1559   declarator->parenthesized = UNKNOWN_LOCATION;
1560   declarator->attributes = NULL_TREE;
1561   declarator->std_attributes = NULL_TREE;
1562   declarator->declarator = NULL;
1563   declarator->parameter_pack_p = false;
1564   declarator->id_loc = UNKNOWN_LOCATION;
1565   declarator->init_loc = UNKNOWN_LOCATION;
1566 
1567   return declarator;
1568 }
1569 
1570 /* Make a declarator for a generalized identifier.  If
1571    QUALIFYING_SCOPE is non-NULL, the identifier is
1572    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1573    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1574    is, if any.   */
1575 
1576 static cp_declarator *
make_id_declarator(tree qualifying_scope,tree unqualified_name,special_function_kind sfk,location_t id_location)1577 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1578 		    special_function_kind sfk, location_t id_location)
1579 {
1580   cp_declarator *declarator;
1581 
1582   /* It is valid to write:
1583 
1584        class C { void f(); };
1585        typedef C D;
1586        void D::f();
1587 
1588      The standard is not clear about whether `typedef const C D' is
1589      legal; as of 2002-09-15 the committee is considering that
1590      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1591      well.  */
1592   if (qualifying_scope && TYPE_P (qualifying_scope))
1593     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1594 
1595   gcc_assert (identifier_p (unqualified_name)
1596 	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1597 	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1598 
1599   declarator = make_declarator (cdk_id);
1600   declarator->u.id.qualifying_scope = qualifying_scope;
1601   declarator->u.id.unqualified_name = unqualified_name;
1602   declarator->u.id.sfk = sfk;
1603   declarator->id_loc = id_location;
1604 
1605   return declarator;
1606 }
1607 
1608 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1609    of modifiers such as const or volatile to apply to the pointer
1610    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1611    appertain to the pointer or reference.  */
1612 
1613 cp_declarator *
make_pointer_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,tree attributes)1614 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1615 			 tree attributes)
1616 {
1617   cp_declarator *declarator;
1618 
1619   declarator = make_declarator (cdk_pointer);
1620   declarator->declarator = target;
1621   declarator->u.pointer.qualifiers = cv_qualifiers;
1622   declarator->u.pointer.class_type = NULL_TREE;
1623   if (target)
1624     {
1625       declarator->id_loc = target->id_loc;
1626       declarator->parameter_pack_p = target->parameter_pack_p;
1627       target->parameter_pack_p = false;
1628     }
1629   else
1630     declarator->parameter_pack_p = false;
1631 
1632   declarator->std_attributes = attributes;
1633 
1634   return declarator;
1635 }
1636 
1637 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1638    represent the attributes that appertain to the pointer or
1639    reference.  */
1640 
1641 cp_declarator *
make_reference_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,bool rvalue_ref,tree attributes)1642 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1643 			   bool rvalue_ref, tree attributes)
1644 {
1645   cp_declarator *declarator;
1646 
1647   declarator = make_declarator (cdk_reference);
1648   declarator->declarator = target;
1649   declarator->u.reference.qualifiers = cv_qualifiers;
1650   declarator->u.reference.rvalue_ref = rvalue_ref;
1651   if (target)
1652     {
1653       declarator->id_loc = target->id_loc;
1654       declarator->parameter_pack_p = target->parameter_pack_p;
1655       target->parameter_pack_p = false;
1656     }
1657   else
1658     declarator->parameter_pack_p = false;
1659 
1660   declarator->std_attributes = attributes;
1661 
1662   return declarator;
1663 }
1664 
1665 /* Like make_pointer_declarator -- but for a pointer to a non-static
1666    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1667    appertain to the pointer or reference.  */
1668 
1669 cp_declarator *
make_ptrmem_declarator(cp_cv_quals cv_qualifiers,tree class_type,cp_declarator * pointee,tree attributes)1670 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1671 			cp_declarator *pointee,
1672 			tree attributes)
1673 {
1674   cp_declarator *declarator;
1675 
1676   declarator = make_declarator (cdk_ptrmem);
1677   declarator->declarator = pointee;
1678   declarator->u.pointer.qualifiers = cv_qualifiers;
1679   declarator->u.pointer.class_type = class_type;
1680 
1681   if (pointee)
1682     {
1683       declarator->parameter_pack_p = pointee->parameter_pack_p;
1684       pointee->parameter_pack_p = false;
1685     }
1686   else
1687     declarator->parameter_pack_p = false;
1688 
1689   declarator->std_attributes = attributes;
1690 
1691   return declarator;
1692 }
1693 
1694 /* Make a declarator for the function given by TARGET, with the
1695    indicated PARMS.  The CV_QUALIFIERS apply to the function, as in
1696    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1697    indicates what exceptions can be thrown.  */
1698 
1699 cp_declarator *
make_call_declarator(cp_declarator * target,tree parms,cp_cv_quals cv_qualifiers,cp_virt_specifiers virt_specifiers,cp_ref_qualifier ref_qualifier,tree tx_qualifier,tree exception_specification,tree late_return_type,tree requires_clause,location_t parens_loc)1700 make_call_declarator (cp_declarator *target,
1701 		      tree parms,
1702 		      cp_cv_quals cv_qualifiers,
1703 		      cp_virt_specifiers virt_specifiers,
1704 		      cp_ref_qualifier ref_qualifier,
1705 		      tree tx_qualifier,
1706 		      tree exception_specification,
1707 		      tree late_return_type,
1708 		      tree requires_clause,
1709 		      location_t parens_loc)
1710 {
1711   cp_declarator *declarator;
1712 
1713   declarator = make_declarator (cdk_function);
1714   declarator->declarator = target;
1715   declarator->u.function.parameters = parms;
1716   declarator->u.function.qualifiers = cv_qualifiers;
1717   declarator->u.function.virt_specifiers = virt_specifiers;
1718   declarator->u.function.ref_qualifier = ref_qualifier;
1719   declarator->u.function.tx_qualifier = tx_qualifier;
1720   declarator->u.function.exception_specification = exception_specification;
1721   declarator->u.function.late_return_type = late_return_type;
1722   declarator->u.function.requires_clause = requires_clause;
1723   declarator->u.function.parens_loc = parens_loc;
1724   if (target)
1725     {
1726       declarator->id_loc = target->id_loc;
1727       declarator->parameter_pack_p = target->parameter_pack_p;
1728       target->parameter_pack_p = false;
1729     }
1730   else
1731     declarator->parameter_pack_p = false;
1732 
1733   return declarator;
1734 }
1735 
1736 /* Make a declarator for an array of BOUNDS elements, each of which is
1737    defined by ELEMENT.  */
1738 
1739 cp_declarator *
make_array_declarator(cp_declarator * element,tree bounds)1740 make_array_declarator (cp_declarator *element, tree bounds)
1741 {
1742   cp_declarator *declarator;
1743 
1744   declarator = make_declarator (cdk_array);
1745   declarator->declarator = element;
1746   declarator->u.array.bounds = bounds;
1747   if (element)
1748     {
1749       declarator->id_loc = element->id_loc;
1750       declarator->parameter_pack_p = element->parameter_pack_p;
1751       element->parameter_pack_p = false;
1752     }
1753   else
1754     declarator->parameter_pack_p = false;
1755 
1756   return declarator;
1757 }
1758 
1759 /* Determine whether the declarator we've seen so far can be a
1760    parameter pack, when followed by an ellipsis.  */
1761 static bool
declarator_can_be_parameter_pack(cp_declarator * declarator)1762 declarator_can_be_parameter_pack (cp_declarator *declarator)
1763 {
1764   if (declarator && declarator->parameter_pack_p)
1765     /* We already saw an ellipsis.  */
1766     return false;
1767 
1768   /* Search for a declarator name, or any other declarator that goes
1769      after the point where the ellipsis could appear in a parameter
1770      pack. If we find any of these, then this declarator cannot be
1771      made into a parameter pack.  */
1772   bool found = false;
1773   while (declarator && !found)
1774     {
1775       switch ((int)declarator->kind)
1776 	{
1777 	case cdk_id:
1778 	case cdk_array:
1779 	case cdk_decomp:
1780 	  found = true;
1781 	  break;
1782 
1783 	case cdk_error:
1784 	  return true;
1785 
1786 	default:
1787 	  declarator = declarator->declarator;
1788 	  break;
1789 	}
1790     }
1791 
1792   return !found;
1793 }
1794 
1795 cp_parameter_declarator *no_parameters;
1796 
1797 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1798    DECLARATOR and DEFAULT_ARGUMENT.  */
1799 
1800 cp_parameter_declarator *
make_parameter_declarator(cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree default_argument,location_t loc,bool template_parameter_pack_p=false)1801 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1802 			   cp_declarator *declarator,
1803 			   tree default_argument,
1804 			   location_t loc,
1805 			   bool template_parameter_pack_p = false)
1806 {
1807   cp_parameter_declarator *parameter;
1808 
1809   parameter = ((cp_parameter_declarator *)
1810 	       alloc_declarator (sizeof (cp_parameter_declarator)));
1811   parameter->next = NULL;
1812   if (decl_specifiers)
1813     parameter->decl_specifiers = *decl_specifiers;
1814   else
1815     clear_decl_specs (&parameter->decl_specifiers);
1816   parameter->declarator = declarator;
1817   parameter->default_argument = default_argument;
1818   parameter->template_parameter_pack_p = template_parameter_pack_p;
1819   parameter->loc = loc;
1820 
1821   return parameter;
1822 }
1823 
1824 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1825 
1826 static bool
function_declarator_p(const cp_declarator * declarator)1827 function_declarator_p (const cp_declarator *declarator)
1828 {
1829   while (declarator)
1830     {
1831       if (declarator->kind == cdk_function
1832 	  && declarator->declarator->kind == cdk_id)
1833 	return true;
1834       if (declarator->kind == cdk_id
1835 	  || declarator->kind == cdk_decomp
1836 	  || declarator->kind == cdk_error)
1837 	return false;
1838       declarator = declarator->declarator;
1839     }
1840   return false;
1841 }
1842 
1843 /* The parser.  */
1844 
1845 /* Overview
1846    --------
1847 
1848    A cp_parser parses the token stream as specified by the C++
1849    grammar.  Its job is purely parsing, not semantic analysis.  For
1850    example, the parser breaks the token stream into declarators,
1851    expressions, statements, and other similar syntactic constructs.
1852    It does not check that the types of the expressions on either side
1853    of an assignment-statement are compatible, or that a function is
1854    not declared with a parameter of type `void'.
1855 
1856    The parser invokes routines elsewhere in the compiler to perform
1857    semantic analysis and to build up the abstract syntax tree for the
1858    code processed.
1859 
1860    The parser (and the template instantiation code, which is, in a
1861    way, a close relative of parsing) are the only parts of the
1862    compiler that should be calling push_scope and pop_scope, or
1863    related functions.  The parser (and template instantiation code)
1864    keeps track of what scope is presently active; everything else
1865    should simply honor that.  (The code that generates static
1866    initializers may also need to set the scope, in order to check
1867    access control correctly when emitting the initializers.)
1868 
1869    Methodology
1870    -----------
1871 
1872    The parser is of the standard recursive-descent variety.  Upcoming
1873    tokens in the token stream are examined in order to determine which
1874    production to use when parsing a non-terminal.  Some C++ constructs
1875    require arbitrary look ahead to disambiguate.  For example, it is
1876    impossible, in the general case, to tell whether a statement is an
1877    expression or declaration without scanning the entire statement.
1878    Therefore, the parser is capable of "parsing tentatively."  When the
1879    parser is not sure what construct comes next, it enters this mode.
1880    Then, while we attempt to parse the construct, the parser queues up
1881    error messages, rather than issuing them immediately, and saves the
1882    tokens it consumes.  If the construct is parsed successfully, the
1883    parser "commits", i.e., it issues any queued error messages and
1884    the tokens that were being preserved are permanently discarded.
1885    If, however, the construct is not parsed successfully, the parser
1886    rolls back its state completely so that it can resume parsing using
1887    a different alternative.
1888 
1889    Future Improvements
1890    -------------------
1891 
1892    The performance of the parser could probably be improved substantially.
1893    We could often eliminate the need to parse tentatively by looking ahead
1894    a little bit.  In some places, this approach might not entirely eliminate
1895    the need to parse tentatively, but it might still speed up the average
1896    case.  */
1897 
1898 /* Flags that are passed to some parsing functions.  These values can
1899    be bitwise-ored together.  */
1900 
1901 enum
1902 {
1903   /* No flags.  */
1904   CP_PARSER_FLAGS_NONE = 0x0,
1905   /* The construct is optional.  If it is not present, then no error
1906      should be issued.  */
1907   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1908   /* When parsing a type-specifier, treat user-defined type-names
1909      as non-type identifiers.  */
1910   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1911   /* When parsing a type-specifier, do not try to parse a class-specifier
1912      or enum-specifier.  */
1913   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1914   /* When parsing a decl-specifier-seq, only allow type-specifier or
1915      constexpr.  */
1916   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1917   /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1918      for C++20 consteval.  */
1919   CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1920   /* When parsing a decl-specifier-seq, allow missing typename.  */
1921   CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
1922   /* When parsing of the noexcept-specifier should be delayed.  */
1923   CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
1924   /* When parsing a consteval declarator.  */
1925   CP_PARSER_FLAGS_CONSTEVAL = 0x80
1926 };
1927 
1928 /* This type is used for parameters and variables which hold
1929    combinations of the above flags.  */
1930 typedef int cp_parser_flags;
1931 
1932 /* The different kinds of declarators we want to parse.  */
1933 
1934 enum cp_parser_declarator_kind
1935 {
1936   /* We want an abstract declarator.  */
1937   CP_PARSER_DECLARATOR_ABSTRACT,
1938   /* We want a named declarator.  */
1939   CP_PARSER_DECLARATOR_NAMED,
1940   /* We don't mind, but the name must be an unqualified-id.  */
1941   CP_PARSER_DECLARATOR_EITHER
1942 };
1943 
1944 /* The precedence values used to parse binary expressions.  The minimum value
1945    of PREC must be 1, because zero is reserved to quickly discriminate
1946    binary operators from other tokens.  */
1947 
1948 enum cp_parser_prec
1949 {
1950   PREC_NOT_OPERATOR,
1951   PREC_LOGICAL_OR_EXPRESSION,
1952   PREC_LOGICAL_AND_EXPRESSION,
1953   PREC_INCLUSIVE_OR_EXPRESSION,
1954   PREC_EXCLUSIVE_OR_EXPRESSION,
1955   PREC_AND_EXPRESSION,
1956   PREC_EQUALITY_EXPRESSION,
1957   PREC_RELATIONAL_EXPRESSION,
1958   PREC_SPACESHIP_EXPRESSION,
1959   PREC_SHIFT_EXPRESSION,
1960   PREC_ADDITIVE_EXPRESSION,
1961   PREC_MULTIPLICATIVE_EXPRESSION,
1962   PREC_PM_EXPRESSION,
1963   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1964 };
1965 
1966 /* A mapping from a token type to a corresponding tree node type, with a
1967    precedence value.  */
1968 
1969 struct cp_parser_binary_operations_map_node
1970 {
1971   /* The token type.  */
1972   enum cpp_ttype token_type;
1973   /* The corresponding tree code.  */
1974   enum tree_code tree_type;
1975   /* The precedence of this operator.  */
1976   enum cp_parser_prec prec;
1977 };
1978 
1979 struct cp_parser_expression_stack_entry
1980 {
1981   /* Left hand side of the binary operation we are currently
1982      parsing.  */
1983   cp_expr lhs;
1984   /* Original tree code for left hand side, if it was a binary
1985      expression itself (used for -Wparentheses).  */
1986   enum tree_code lhs_type;
1987   /* Tree code for the binary operation we are parsing.  */
1988   enum tree_code tree_type;
1989   /* Precedence of the binary operation we are parsing.  */
1990   enum cp_parser_prec prec;
1991   /* Location of the binary operation we are parsing.  */
1992   location_t loc;
1993 };
1994 
1995 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1996    entries because precedence levels on the stack are monotonically
1997    increasing.  */
1998 typedef struct cp_parser_expression_stack_entry
1999   cp_parser_expression_stack[NUM_PREC_VALUES];
2000 
2001 /* Prototypes.  */
2002 
2003 /* Constructors and destructors.  */
2004 
2005 static cp_parser_context *cp_parser_context_new
2006   (cp_parser_context *);
2007 
2008 /* Class variables.  */
2009 
2010 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2011 
2012 /* The operator-precedence table used by cp_parser_binary_expression.
2013    Transformed into an associative array (binops_by_token) by
2014    cp_parser_new.  */
2015 
2016 static const cp_parser_binary_operations_map_node binops[] = {
2017   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2018   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2019 
2020   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2021   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2022   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2023 
2024   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2025   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2026 
2027   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2028   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2029 
2030   { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2031 
2032   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2033   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2034   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2035   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2036 
2037   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2038   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2039 
2040   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2041 
2042   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2043 
2044   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2045 
2046   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2047 
2048   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2049 };
2050 
2051 /* The same as binops, but initialized by cp_parser_new so that
2052    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
2053    for speed.  */
2054 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2055 
2056 /* Constructors and destructors.  */
2057 
2058 /* Construct a new context.  The context below this one on the stack
2059    is given by NEXT.  */
2060 
2061 static cp_parser_context *
cp_parser_context_new(cp_parser_context * next)2062 cp_parser_context_new (cp_parser_context* next)
2063 {
2064   cp_parser_context *context;
2065 
2066   /* Allocate the storage.  */
2067   if (cp_parser_context_free_list != NULL)
2068     {
2069       /* Pull the first entry from the free list.  */
2070       context = cp_parser_context_free_list;
2071       cp_parser_context_free_list = context->next;
2072       memset (context, 0, sizeof (*context));
2073     }
2074   else
2075     context = ggc_cleared_alloc<cp_parser_context> ();
2076 
2077   /* No errors have occurred yet in this context.  */
2078   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2079   /* If this is not the bottommost context, copy information that we
2080      need from the previous context.  */
2081   if (next)
2082     {
2083       /* If, in the NEXT context, we are parsing an `x->' or `x.'
2084 	 expression, then we are parsing one in this context, too.  */
2085       context->object_type = next->object_type;
2086       /* Thread the stack.  */
2087       context->next = next;
2088     }
2089 
2090   return context;
2091 }
2092 
2093 /* Managing the unparsed function queues.  */
2094 
2095 #define unparsed_funs_with_default_args \
2096   parser->unparsed_queues->last ().funs_with_default_args
2097 #define unparsed_funs_with_definitions \
2098   parser->unparsed_queues->last ().funs_with_definitions
2099 #define unparsed_nsdmis \
2100   parser->unparsed_queues->last ().nsdmis
2101 #define unparsed_noexcepts \
2102   parser->unparsed_queues->last ().noexcepts
2103 
2104 static void
push_unparsed_function_queues(cp_parser * parser)2105 push_unparsed_function_queues (cp_parser *parser)
2106 {
2107   cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2108   vec_safe_push (parser->unparsed_queues, e);
2109 }
2110 
2111 static void
pop_unparsed_function_queues(cp_parser * parser)2112 pop_unparsed_function_queues (cp_parser *parser)
2113 {
2114   release_tree_vector (unparsed_funs_with_definitions);
2115   parser->unparsed_queues->pop ();
2116 }
2117 
2118 /* Prototypes.  */
2119 
2120 /* Constructors and destructors.  */
2121 
2122 static cp_parser *cp_parser_new
2123   (cp_lexer *);
2124 
2125 /* Routines to parse various constructs.
2126 
2127    Those that return `tree' will return the error_mark_node (rather
2128    than NULL_TREE) if a parse error occurs, unless otherwise noted.
2129    Sometimes, they will return an ordinary node if error-recovery was
2130    attempted, even though a parse error occurred.  So, to check
2131    whether or not a parse error occurred, you should always use
2132    cp_parser_error_occurred.  If the construct is optional (indicated
2133    either by an `_opt' in the name of the function that does the
2134    parsing or via a FLAGS parameter), then NULL_TREE is returned if
2135    the construct is not present.  */
2136 
2137 /* Lexical conventions [gram.lex]  */
2138 
2139 static cp_expr cp_parser_identifier
2140   (cp_parser *);
2141 static cp_expr cp_parser_string_literal
2142   (cp_parser *, bool, bool, bool);
2143 static cp_expr cp_parser_userdef_char_literal
2144   (cp_parser *);
2145 static tree cp_parser_userdef_string_literal
2146   (tree);
2147 static cp_expr cp_parser_userdef_numeric_literal
2148   (cp_parser *);
2149 
2150 /* Basic concepts [gram.basic]  */
2151 
2152 static void cp_parser_translation_unit (cp_parser *);
2153 
2154 /* Expressions [gram.expr]  */
2155 
2156 static cp_expr cp_parser_primary_expression
2157   (cp_parser *, bool, bool, bool, cp_id_kind *);
2158 static cp_expr cp_parser_id_expression
2159   (cp_parser *, bool, bool, bool *, bool, bool);
2160 static cp_expr cp_parser_unqualified_id
2161   (cp_parser *, bool, bool, bool, bool);
2162 static tree cp_parser_nested_name_specifier_opt
2163   (cp_parser *, bool, bool, bool, bool, bool = false);
2164 static tree cp_parser_nested_name_specifier
2165   (cp_parser *, bool, bool, bool, bool);
2166 static tree cp_parser_qualifying_entity
2167   (cp_parser *, bool, bool, bool, bool, bool);
2168 static cp_expr cp_parser_postfix_expression
2169   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2170 static tree cp_parser_postfix_open_square_expression
2171   (cp_parser *, tree, bool, bool);
2172 static tree cp_parser_postfix_dot_deref_expression
2173   (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2174 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2175   (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2176    bool = false);
2177 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2178 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2179 static void cp_parser_pseudo_destructor_name
2180   (cp_parser *, tree, tree *, tree *);
2181 static cp_expr cp_parser_unary_expression
2182   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2183 static enum tree_code cp_parser_unary_operator
2184   (cp_token *);
2185 static tree cp_parser_has_attribute_expression
2186   (cp_parser *);
2187 static tree cp_parser_new_expression
2188   (cp_parser *);
2189 static vec<tree, va_gc> *cp_parser_new_placement
2190   (cp_parser *);
2191 static tree cp_parser_new_type_id
2192   (cp_parser *, tree *);
2193 static cp_declarator *cp_parser_new_declarator_opt
2194   (cp_parser *);
2195 static cp_declarator *cp_parser_direct_new_declarator
2196   (cp_parser *);
2197 static vec<tree, va_gc> *cp_parser_new_initializer
2198   (cp_parser *);
2199 static tree cp_parser_delete_expression
2200   (cp_parser *);
2201 static cp_expr cp_parser_cast_expression
2202   (cp_parser *, bool, bool, bool, cp_id_kind *);
2203 static cp_expr cp_parser_binary_expression
2204   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2205 static tree cp_parser_question_colon_clause
2206   (cp_parser *, cp_expr);
2207 static cp_expr cp_parser_assignment_expression
2208   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2209 static enum tree_code cp_parser_assignment_operator_opt
2210   (cp_parser *);
2211 static cp_expr cp_parser_expression
2212   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2213 static cp_expr cp_parser_constant_expression
2214   (cp_parser *, int = 0, bool * = NULL, bool = false);
2215 static cp_expr cp_parser_builtin_offsetof
2216   (cp_parser *);
2217 static cp_expr cp_parser_lambda_expression
2218   (cp_parser *);
2219 static void cp_parser_lambda_introducer
2220   (cp_parser *, tree);
2221 static bool cp_parser_lambda_declarator_opt
2222   (cp_parser *, tree);
2223 static void cp_parser_lambda_body
2224   (cp_parser *, tree);
2225 
2226 /* Statements [gram.stmt.stmt]  */
2227 
2228 static void cp_parser_statement
2229   (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2230 static void cp_parser_label_for_labeled_statement
2231 (cp_parser *, tree);
2232 static tree cp_parser_expression_statement
2233   (cp_parser *, tree);
2234 static tree cp_parser_compound_statement
2235   (cp_parser *, tree, int, bool);
2236 static void cp_parser_statement_seq_opt
2237   (cp_parser *, tree);
2238 static tree cp_parser_selection_statement
2239   (cp_parser *, bool *, vec<tree> *);
2240 static tree cp_parser_condition
2241   (cp_parser *);
2242 static tree cp_parser_iteration_statement
2243   (cp_parser *, bool *, bool, unsigned short);
2244 static bool cp_parser_init_statement
2245   (cp_parser *, tree *decl);
2246 static tree cp_parser_for
2247   (cp_parser *, bool, unsigned short);
2248 static tree cp_parser_c_for
2249   (cp_parser *, tree, tree, bool, unsigned short);
2250 static tree cp_parser_range_for
2251   (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2252 static void do_range_for_auto_deduction
2253   (tree, tree);
2254 static tree cp_parser_perform_range_for_lookup
2255   (tree, tree *, tree *);
2256 static tree cp_parser_range_for_member_function
2257   (tree, tree);
2258 static tree cp_parser_jump_statement
2259   (cp_parser *);
2260 static void cp_parser_declaration_statement
2261   (cp_parser *);
2262 
2263 static tree cp_parser_implicitly_scoped_statement
2264   (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2265 static void cp_parser_already_scoped_statement
2266   (cp_parser *, bool *, const token_indent_info &);
2267 
2268 /* State of module-declaration parsing.  */
2269 enum module_parse
2270 {
2271   MP_NOT_MODULE,	/* Not a module.  */
2272 
2273   _MP_UNUSED,
2274 
2275   MP_FIRST,	/* First declaration of TU.  */
2276   MP_GLOBAL,	/* Global Module Fragment.  */
2277 
2278   MP_PURVIEW_IMPORTS,   /* Imports of a module.  */
2279   MP_PURVIEW,	/* Purview of a named module.  */
2280 
2281   MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment.  */
2282   MP_PRIVATE,   /* Private Module Fragment.  */
2283 };
2284 
2285 static module_parse cp_parser_module_declaration
2286   (cp_parser *parser, module_parse, bool exporting);
2287 static void cp_parser_import_declaration
2288   (cp_parser *parser, module_parse, bool exporting);
2289 
2290 /* Declarations [gram.dcl.dcl] */
2291 
2292 static void cp_parser_declaration_seq_opt
2293   (cp_parser *);
2294 static void cp_parser_declaration
2295   (cp_parser *, tree);
2296 static void cp_parser_toplevel_declaration
2297   (cp_parser *);
2298 static void cp_parser_block_declaration
2299   (cp_parser *, bool);
2300 static void cp_parser_simple_declaration
2301   (cp_parser *, bool, tree *);
2302 static void cp_parser_decl_specifier_seq
2303   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2304 static tree cp_parser_storage_class_specifier_opt
2305   (cp_parser *);
2306 static tree cp_parser_function_specifier_opt
2307   (cp_parser *, cp_decl_specifier_seq *);
2308 static tree cp_parser_type_specifier
2309   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2310    int *, bool *);
2311 static tree cp_parser_simple_type_specifier
2312   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2313 static tree cp_parser_placeholder_type_specifier
2314   (cp_parser *, location_t, tree, bool);
2315 static tree cp_parser_type_name
2316   (cp_parser *, bool);
2317 static tree cp_parser_nonclass_name
2318   (cp_parser* parser);
2319 static tree cp_parser_elaborated_type_specifier
2320   (cp_parser *, bool, bool);
2321 static tree cp_parser_enum_specifier
2322   (cp_parser *);
2323 static void cp_parser_enumerator_list
2324   (cp_parser *, tree);
2325 static void cp_parser_enumerator_definition
2326   (cp_parser *, tree);
2327 static tree cp_parser_namespace_name
2328   (cp_parser *);
2329 static void cp_parser_namespace_definition
2330   (cp_parser *);
2331 static void cp_parser_namespace_body
2332   (cp_parser *);
2333 static tree cp_parser_qualified_namespace_specifier
2334   (cp_parser *);
2335 static void cp_parser_namespace_alias_definition
2336   (cp_parser *);
2337 static bool cp_parser_using_declaration
2338   (cp_parser *, bool);
2339 static void cp_parser_using_directive
2340   (cp_parser *);
2341 static void cp_parser_using_enum
2342   (cp_parser *);
2343 static tree cp_parser_alias_declaration
2344   (cp_parser *);
2345 static void cp_parser_asm_definition
2346   (cp_parser *);
2347 static void cp_parser_linkage_specification
2348   (cp_parser *, tree);
2349 static void cp_parser_static_assert
2350   (cp_parser *, bool);
2351 static tree cp_parser_decltype
2352   (cp_parser *);
2353 static tree cp_parser_decomposition_declaration
2354   (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2355 
2356 /* Declarators [gram.dcl.decl] */
2357 
2358 static tree cp_parser_init_declarator
2359   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2360    vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2361    location_t *, tree *);
2362 static cp_declarator *cp_parser_declarator
2363   (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2364    bool, bool, bool);
2365 static cp_declarator *cp_parser_direct_declarator
2366   (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2367    bool);
2368 static enum tree_code cp_parser_ptr_operator
2369   (cp_parser *, tree *, cp_cv_quals *, tree *);
2370 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2371   (cp_parser *);
2372 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2373   (cp_parser *);
2374 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2375   (cp_parser *);
2376 static tree cp_parser_tx_qualifier_opt
2377   (cp_parser *);
2378 static tree cp_parser_late_return_type_opt
2379   (cp_parser *, cp_declarator *, tree &);
2380 static tree cp_parser_declarator_id
2381   (cp_parser *, bool);
2382 static tree cp_parser_type_id
2383   (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2384 static tree cp_parser_template_type_arg
2385   (cp_parser *);
2386 static tree cp_parser_trailing_type_id (cp_parser *);
2387 static tree cp_parser_type_id_1
2388   (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2389 static void cp_parser_type_specifier_seq
2390   (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2391 static tree cp_parser_parameter_declaration_clause
2392   (cp_parser *, cp_parser_flags);
2393 static tree cp_parser_parameter_declaration_list
2394   (cp_parser *, cp_parser_flags);
2395 static cp_parameter_declarator *cp_parser_parameter_declaration
2396   (cp_parser *, cp_parser_flags, bool, bool *);
2397 static tree cp_parser_default_argument
2398   (cp_parser *, bool);
2399 static void cp_parser_function_body
2400   (cp_parser *, bool);
2401 static tree cp_parser_initializer
2402   (cp_parser *, bool *, bool *, bool = false);
2403 static cp_expr cp_parser_initializer_clause
2404   (cp_parser *, bool *);
2405 static cp_expr cp_parser_braced_list
2406   (cp_parser*, bool*);
2407 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2408   (cp_parser *, bool *, bool *);
2409 
2410 static void cp_parser_ctor_initializer_opt_and_function_body
2411   (cp_parser *, bool);
2412 
2413 static tree cp_parser_late_parsing_omp_declare_simd
2414   (cp_parser *, tree);
2415 
2416 static tree cp_parser_late_parsing_oacc_routine
2417   (cp_parser *, tree);
2418 
2419 static tree synthesize_implicit_template_parm
2420   (cp_parser *, tree);
2421 static tree finish_fully_implicit_template
2422   (cp_parser *, tree);
2423 static void abort_fully_implicit_template
2424   (cp_parser *);
2425 
2426 /* Classes [gram.class] */
2427 
2428 static tree cp_parser_class_name
2429   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2430 static tree cp_parser_class_specifier
2431   (cp_parser *);
2432 static tree cp_parser_class_head
2433   (cp_parser *, bool *);
2434 static enum tag_types cp_parser_class_key
2435   (cp_parser *);
2436 static void cp_parser_type_parameter_key
2437   (cp_parser* parser);
2438 static void cp_parser_member_specification_opt
2439   (cp_parser *);
2440 static void cp_parser_member_declaration
2441   (cp_parser *);
2442 static tree cp_parser_pure_specifier
2443   (cp_parser *);
2444 static tree cp_parser_constant_initializer
2445   (cp_parser *);
2446 
2447 /* Derived classes [gram.class.derived] */
2448 
2449 static tree cp_parser_base_clause
2450   (cp_parser *);
2451 static tree cp_parser_base_specifier
2452   (cp_parser *);
2453 
2454 /* Special member functions [gram.special] */
2455 
2456 static tree cp_parser_conversion_function_id
2457   (cp_parser *);
2458 static tree cp_parser_conversion_type_id
2459   (cp_parser *);
2460 static cp_declarator *cp_parser_conversion_declarator_opt
2461   (cp_parser *);
2462 static void cp_parser_ctor_initializer_opt
2463   (cp_parser *);
2464 static void cp_parser_mem_initializer_list
2465   (cp_parser *);
2466 static tree cp_parser_mem_initializer
2467   (cp_parser *);
2468 static tree cp_parser_mem_initializer_id
2469   (cp_parser *);
2470 
2471 /* Overloading [gram.over] */
2472 
2473 static cp_expr cp_parser_operator_function_id
2474   (cp_parser *);
2475 static cp_expr cp_parser_operator
2476   (cp_parser *, location_t);
2477 
2478 /* Templates [gram.temp] */
2479 
2480 static void cp_parser_template_declaration
2481   (cp_parser *, bool);
2482 static tree cp_parser_template_parameter_list
2483   (cp_parser *);
2484 static tree cp_parser_template_parameter
2485   (cp_parser *, bool *, bool *);
2486 static tree cp_parser_type_parameter
2487   (cp_parser *, bool *);
2488 static tree cp_parser_template_id
2489   (cp_parser *, bool, bool, enum tag_types, bool);
2490 static tree cp_parser_template_id_expr
2491   (cp_parser *, bool, bool, bool);
2492 static tree cp_parser_template_name
2493   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2494 static tree cp_parser_template_argument_list
2495   (cp_parser *);
2496 static tree cp_parser_template_argument
2497   (cp_parser *);
2498 static void cp_parser_explicit_instantiation
2499   (cp_parser *);
2500 static void cp_parser_explicit_specialization
2501   (cp_parser *);
2502 
2503 /* Exception handling [gram.except] */
2504 
2505 static tree cp_parser_try_block
2506   (cp_parser *);
2507 static void cp_parser_function_try_block
2508   (cp_parser *);
2509 static void cp_parser_handler_seq
2510   (cp_parser *);
2511 static void cp_parser_handler
2512   (cp_parser *);
2513 static tree cp_parser_exception_declaration
2514   (cp_parser *);
2515 static tree cp_parser_throw_expression
2516   (cp_parser *);
2517 static tree cp_parser_exception_specification_opt
2518   (cp_parser *, cp_parser_flags);
2519 static tree cp_parser_type_id_list
2520   (cp_parser *);
2521 static tree cp_parser_noexcept_specification_opt
2522   (cp_parser *, cp_parser_flags, bool, bool *, bool);
2523 
2524 /* GNU Extensions */
2525 
2526 static tree cp_parser_asm_specification_opt
2527   (cp_parser *);
2528 static tree cp_parser_asm_operand_list
2529   (cp_parser *);
2530 static tree cp_parser_asm_clobber_list
2531   (cp_parser *);
2532 static tree cp_parser_asm_label_list
2533   (cp_parser *);
2534 static bool cp_next_tokens_can_be_attribute_p
2535   (cp_parser *);
2536 static bool cp_next_tokens_can_be_gnu_attribute_p
2537   (cp_parser *);
2538 static bool cp_next_tokens_can_be_std_attribute_p
2539   (cp_parser *);
2540 static bool cp_nth_tokens_can_be_std_attribute_p
2541   (cp_parser *, size_t);
2542 static bool cp_nth_tokens_can_be_gnu_attribute_p
2543   (cp_parser *, size_t);
2544 static bool cp_nth_tokens_can_be_attribute_p
2545   (cp_parser *, size_t);
2546 static tree cp_parser_attributes_opt
2547   (cp_parser *);
2548 static tree cp_parser_gnu_attributes_opt
2549   (cp_parser *);
2550 static tree cp_parser_gnu_attribute_list
2551   (cp_parser *, bool = false);
2552 static tree cp_parser_std_attribute
2553   (cp_parser *, tree);
2554 static tree cp_parser_std_attribute_spec
2555   (cp_parser *);
2556 static tree cp_parser_std_attribute_spec_seq
2557   (cp_parser *);
2558 static size_t cp_parser_skip_std_attribute_spec_seq
2559   (cp_parser *, size_t);
2560 static size_t cp_parser_skip_attributes_opt
2561   (cp_parser *, size_t);
2562 static bool cp_parser_extension_opt
2563   (cp_parser *, int *);
2564 static void cp_parser_label_declaration
2565   (cp_parser *);
2566 
2567 /* Concept Extensions */
2568 
2569 static tree cp_parser_concept_definition
2570   (cp_parser *);
2571 static tree cp_parser_constraint_expression
2572   (cp_parser *);
2573 static tree cp_parser_requires_clause_opt
2574   (cp_parser *, bool);
2575 static tree cp_parser_requires_expression
2576   (cp_parser *);
2577 static tree cp_parser_requirement_parameter_list
2578   (cp_parser *);
2579 static tree cp_parser_requirement_body
2580   (cp_parser *);
2581 static tree cp_parser_requirement_seq
2582   (cp_parser *);
2583 static tree cp_parser_requirement
2584   (cp_parser *);
2585 static tree cp_parser_simple_requirement
2586   (cp_parser *);
2587 static tree cp_parser_compound_requirement
2588   (cp_parser *);
2589 static tree cp_parser_type_requirement
2590   (cp_parser *);
2591 static tree cp_parser_nested_requirement
2592   (cp_parser *);
2593 
2594 /* Transactional Memory Extensions */
2595 
2596 static tree cp_parser_transaction
2597   (cp_parser *, cp_token *);
2598 static tree cp_parser_transaction_expression
2599   (cp_parser *, enum rid);
2600 static void cp_parser_function_transaction
2601   (cp_parser *, enum rid);
2602 static tree cp_parser_transaction_cancel
2603   (cp_parser *);
2604 
2605 /* Coroutine extensions.  */
2606 
2607 static tree cp_parser_yield_expression
2608   (cp_parser *);
2609 
2610 
2611 enum pragma_context {
2612   pragma_external,
2613   pragma_member,
2614   pragma_objc_icode,
2615   pragma_stmt,
2616   pragma_compound
2617 };
2618 static bool cp_parser_pragma
2619   (cp_parser *, enum pragma_context, bool *);
2620 
2621 /* Objective-C++ Productions */
2622 
2623 static tree cp_parser_objc_message_receiver
2624   (cp_parser *);
2625 static tree cp_parser_objc_message_args
2626   (cp_parser *);
2627 static tree cp_parser_objc_message_expression
2628   (cp_parser *);
2629 static cp_expr cp_parser_objc_encode_expression
2630   (cp_parser *);
2631 static tree cp_parser_objc_defs_expression
2632   (cp_parser *);
2633 static tree cp_parser_objc_protocol_expression
2634   (cp_parser *);
2635 static tree cp_parser_objc_selector_expression
2636   (cp_parser *);
2637 static cp_expr cp_parser_objc_expression
2638   (cp_parser *);
2639 static bool cp_parser_objc_selector_p
2640   (enum cpp_ttype);
2641 static tree cp_parser_objc_selector
2642   (cp_parser *);
2643 static tree cp_parser_objc_protocol_refs_opt
2644   (cp_parser *);
2645 static void cp_parser_objc_declaration
2646   (cp_parser *, tree);
2647 static tree cp_parser_objc_statement
2648   (cp_parser *);
2649 static bool cp_parser_objc_valid_prefix_attributes
2650   (cp_parser *, tree *);
2651 static void cp_parser_objc_at_property_declaration
2652   (cp_parser *) ;
2653 static void cp_parser_objc_at_synthesize_declaration
2654   (cp_parser *) ;
2655 static void cp_parser_objc_at_dynamic_declaration
2656   (cp_parser *) ;
2657 static tree cp_parser_objc_struct_declaration
2658   (cp_parser *) ;
2659 
2660 /* Utility Routines */
2661 
2662 static cp_expr cp_parser_lookup_name
2663   (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2664 static tree cp_parser_lookup_name_simple
2665   (cp_parser *, tree, location_t);
2666 static tree cp_parser_maybe_treat_template_as_class
2667   (tree, bool);
2668 static bool cp_parser_check_declarator_template_parameters
2669   (cp_parser *, cp_declarator *, location_t);
2670 static bool cp_parser_check_template_parameters
2671   (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2672 static cp_expr cp_parser_simple_cast_expression
2673   (cp_parser *);
2674 static tree cp_parser_global_scope_opt
2675   (cp_parser *, bool);
2676 static bool cp_parser_constructor_declarator_p
2677   (cp_parser *, cp_parser_flags, bool);
2678 static tree cp_parser_function_definition_from_specifiers_and_declarator
2679   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2680 static tree cp_parser_function_definition_after_declarator
2681   (cp_parser *, bool);
2682 static bool cp_parser_template_declaration_after_export
2683   (cp_parser *, bool);
2684 static void cp_parser_perform_template_parameter_access_checks
2685   (vec<deferred_access_check, va_gc> *);
2686 static tree cp_parser_single_declaration
2687   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2688 static cp_expr cp_parser_functional_cast
2689   (cp_parser *, tree);
2690 static tree cp_parser_save_member_function_body
2691   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2692 static tree cp_parser_save_nsdmi
2693   (cp_parser *);
2694 static tree cp_parser_enclosed_template_argument_list
2695   (cp_parser *);
2696 static void cp_parser_save_default_args
2697   (cp_parser *, tree);
2698 static void cp_parser_late_parsing_for_member
2699   (cp_parser *, tree);
2700 static tree cp_parser_late_parse_one_default_arg
2701   (cp_parser *, tree, tree, tree);
2702 static void cp_parser_late_parsing_nsdmi
2703   (cp_parser *, tree);
2704 static void cp_parser_late_parsing_default_args
2705   (cp_parser *, tree);
2706 static tree cp_parser_sizeof_operand
2707   (cp_parser *, enum rid);
2708 static cp_expr cp_parser_trait_expr
2709   (cp_parser *, enum rid);
2710 static bool cp_parser_declares_only_class_p
2711   (cp_parser *);
2712 static void cp_parser_set_storage_class
2713   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2714 static void cp_parser_set_decl_spec_type
2715   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2716 static void set_and_check_decl_spec_loc
2717   (cp_decl_specifier_seq *decl_specs,
2718    cp_decl_spec ds, cp_token *);
2719 static bool cp_parser_friend_p
2720   (const cp_decl_specifier_seq *);
2721 static void cp_parser_required_error
2722   (cp_parser *, required_token, bool, location_t);
2723 static cp_token *cp_parser_require
2724   (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2725 static cp_token *cp_parser_require_keyword
2726   (cp_parser *, enum rid, required_token);
2727 static bool cp_parser_token_starts_function_definition_p
2728   (cp_token *);
2729 static bool cp_parser_next_token_starts_class_definition_p
2730   (cp_parser *);
2731 static bool cp_parser_next_token_ends_template_argument_p
2732   (cp_parser *);
2733 static bool cp_parser_nth_token_starts_template_argument_list_p
2734   (cp_parser *, size_t);
2735 static enum tag_types cp_parser_token_is_class_key
2736   (cp_token *);
2737 static enum tag_types cp_parser_token_is_type_parameter_key
2738   (cp_token *);
2739 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2740 static void cp_parser_check_class_key
2741 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2742 static void cp_parser_check_access_in_redeclaration
2743   (tree type, location_t location);
2744 static bool cp_parser_optional_template_keyword
2745   (cp_parser *);
2746 static void cp_parser_pre_parsed_nested_name_specifier
2747   (cp_parser *);
2748 static bool cp_parser_cache_group
2749   (cp_parser *, enum cpp_ttype, unsigned);
2750 static tree cp_parser_cache_defarg
2751   (cp_parser *parser, bool nsdmi);
2752 static void cp_parser_parse_tentatively
2753   (cp_parser *);
2754 static void cp_parser_commit_to_tentative_parse
2755   (cp_parser *);
2756 static void cp_parser_commit_to_topmost_tentative_parse
2757   (cp_parser *);
2758 static void cp_parser_abort_tentative_parse
2759   (cp_parser *);
2760 static bool cp_parser_parse_definitely
2761   (cp_parser *);
2762 static inline bool cp_parser_parsing_tentatively
2763   (cp_parser *);
2764 static bool cp_parser_uncommitted_to_tentative_parse_p
2765   (cp_parser *);
2766 static void cp_parser_error
2767   (cp_parser *, const char *);
2768 static void cp_parser_name_lookup_error
2769   (cp_parser *, tree, tree, name_lookup_error, location_t);
2770 static bool cp_parser_simulate_error
2771   (cp_parser *);
2772 static bool cp_parser_check_type_definition
2773   (cp_parser *);
2774 static void cp_parser_check_for_definition_in_return_type
2775   (cp_declarator *, tree, location_t type_location);
2776 static void cp_parser_check_for_invalid_template_id
2777   (cp_parser *, tree, enum tag_types, location_t location);
2778 static bool cp_parser_non_integral_constant_expression
2779   (cp_parser *, non_integral_constant);
2780 static void cp_parser_diagnose_invalid_type_name
2781   (cp_parser *, tree, location_t);
2782 static bool cp_parser_parse_and_diagnose_invalid_type_name
2783   (cp_parser *);
2784 static int cp_parser_skip_to_closing_parenthesis
2785   (cp_parser *, bool, bool, bool);
2786 static void cp_parser_skip_to_end_of_statement
2787   (cp_parser *);
2788 static void cp_parser_consume_semicolon_at_end_of_statement
2789   (cp_parser *);
2790 static void cp_parser_skip_to_end_of_block_or_statement
2791   (cp_parser *);
2792 static bool cp_parser_skip_to_closing_brace
2793   (cp_parser *);
2794 static bool cp_parser_skip_entire_template_parameter_list
2795   (cp_parser *);
2796 static void cp_parser_require_end_of_template_parameter_list
2797   (cp_parser *);
2798 static bool cp_parser_skip_to_end_of_template_parameter_list
2799   (cp_parser *);
2800 static void cp_parser_skip_to_pragma_eol
2801   (cp_parser*, cp_token *);
2802 static bool cp_parser_error_occurred
2803   (cp_parser *);
2804 static bool cp_parser_allow_gnu_extensions_p
2805   (cp_parser *);
2806 static bool cp_parser_is_pure_string_literal
2807   (cp_token *);
2808 static bool cp_parser_is_string_literal
2809   (cp_token *);
2810 static bool cp_parser_is_keyword
2811   (cp_token *, enum rid);
2812 static tree cp_parser_make_typename_type
2813   (cp_parser *, tree, location_t location);
2814 static cp_declarator * cp_parser_make_indirect_declarator
2815   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2816 static bool cp_parser_compound_literal_p
2817   (cp_parser *);
2818 static bool cp_parser_array_designator_p
2819   (cp_parser *);
2820 static bool cp_parser_init_statement_p
2821   (cp_parser *);
2822 static bool cp_parser_skip_to_closing_square_bracket
2823   (cp_parser *);
2824 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2825 
2826 // -------------------------------------------------------------------------- //
2827 // Unevaluated Operand Guard
2828 //
2829 // Implementation of an RAII helper for unevaluated operand parsing.
cp_unevaluated()2830 cp_unevaluated::cp_unevaluated ()
2831 {
2832   ++cp_unevaluated_operand;
2833   ++c_inhibit_evaluation_warnings;
2834 }
2835 
~cp_unevaluated()2836 cp_unevaluated::~cp_unevaluated ()
2837 {
2838   --c_inhibit_evaluation_warnings;
2839   --cp_unevaluated_operand;
2840 }
2841 
2842 // -------------------------------------------------------------------------- //
2843 // Tentative Parsing
2844 
2845 /* Returns nonzero if we are parsing tentatively.  */
2846 
2847 static inline bool
cp_parser_parsing_tentatively(cp_parser * parser)2848 cp_parser_parsing_tentatively (cp_parser* parser)
2849 {
2850   return parser->context->next != NULL;
2851 }
2852 
2853 /* Returns nonzero if TOKEN is a string literal.  */
2854 
2855 static bool
cp_parser_is_pure_string_literal(cp_token * token)2856 cp_parser_is_pure_string_literal (cp_token* token)
2857 {
2858   return (token->type == CPP_STRING ||
2859 	  token->type == CPP_STRING16 ||
2860 	  token->type == CPP_STRING32 ||
2861 	  token->type == CPP_WSTRING ||
2862 	  token->type == CPP_UTF8STRING);
2863 }
2864 
2865 /* Returns nonzero if TOKEN is a string literal
2866    of a user-defined string literal.  */
2867 
2868 static bool
cp_parser_is_string_literal(cp_token * token)2869 cp_parser_is_string_literal (cp_token* token)
2870 {
2871   return (cp_parser_is_pure_string_literal (token) ||
2872 	  token->type == CPP_STRING_USERDEF ||
2873 	  token->type == CPP_STRING16_USERDEF ||
2874 	  token->type == CPP_STRING32_USERDEF ||
2875 	  token->type == CPP_WSTRING_USERDEF ||
2876 	  token->type == CPP_UTF8STRING_USERDEF);
2877 }
2878 
2879 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2880 
2881 static bool
cp_parser_is_keyword(cp_token * token,enum rid keyword)2882 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2883 {
2884   return token->keyword == keyword;
2885 }
2886 
2887 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2888    PRAGMA_NONE.  */
2889 
2890 static enum pragma_kind
cp_parser_pragma_kind(cp_token * token)2891 cp_parser_pragma_kind (cp_token *token)
2892 {
2893   if (token->type != CPP_PRAGMA)
2894     return PRAGMA_NONE;
2895   /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2896   return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2897 }
2898 
2899 /* Helper function for cp_parser_error.
2900    Having peeked a token of kind TOK1_KIND that might signify
2901    a conflict marker, peek successor tokens to determine
2902    if we actually do have a conflict marker.
2903    Specifically, we consider a run of 7 '<', '=' or '>' characters
2904    at the start of a line as a conflict marker.
2905    These come through the lexer as three pairs and a single,
2906    e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2907    If it returns true, *OUT_LOC is written to with the location/range
2908    of the marker.  */
2909 
2910 static bool
cp_lexer_peek_conflict_marker(cp_lexer * lexer,enum cpp_ttype tok1_kind,location_t * out_loc)2911 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2912 			       location_t *out_loc)
2913 {
2914   cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2915   if (token2->type != tok1_kind)
2916     return false;
2917   cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2918   if (token3->type != tok1_kind)
2919     return false;
2920   cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2921   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2922     return false;
2923 
2924   /* It must be at the start of the line.  */
2925   location_t start_loc = cp_lexer_peek_token (lexer)->location;
2926   if (LOCATION_COLUMN (start_loc) != 1)
2927     return false;
2928 
2929   /* We have a conflict marker.  Construct a location of the form:
2930        <<<<<<<
2931        ^~~~~~~
2932      with start == caret, finishing at the end of the marker.  */
2933   location_t finish_loc = get_finish (token4->location);
2934   *out_loc = make_location (start_loc, start_loc, finish_loc);
2935 
2936   return true;
2937 }
2938 
2939 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2940    RT_CLOSE_PAREN.  */
2941 
2942 static const char *
get_matching_symbol(required_token token_desc)2943 get_matching_symbol (required_token token_desc)
2944 {
2945   switch (token_desc)
2946     {
2947     default:
2948       gcc_unreachable ();
2949       return "";
2950     case RT_CLOSE_BRACE:
2951       return "{";
2952     case RT_CLOSE_PAREN:
2953       return "(";
2954     }
2955 }
2956 
2957 /* Attempt to convert TOKEN_DESC from a required_token to an
2958    enum cpp_ttype, returning CPP_EOF if there is no good conversion.  */
2959 
2960 static enum cpp_ttype
get_required_cpp_ttype(required_token token_desc)2961 get_required_cpp_ttype (required_token token_desc)
2962 {
2963   switch (token_desc)
2964     {
2965     case RT_SEMICOLON:
2966       return CPP_SEMICOLON;
2967     case RT_OPEN_PAREN:
2968       return CPP_OPEN_PAREN;
2969     case RT_CLOSE_BRACE:
2970       return CPP_CLOSE_BRACE;
2971     case RT_OPEN_BRACE:
2972       return CPP_OPEN_BRACE;
2973     case RT_CLOSE_SQUARE:
2974       return CPP_CLOSE_SQUARE;
2975     case RT_OPEN_SQUARE:
2976       return CPP_OPEN_SQUARE;
2977     case RT_COMMA:
2978       return CPP_COMMA;
2979     case RT_COLON:
2980       return CPP_COLON;
2981     case RT_CLOSE_PAREN:
2982       return CPP_CLOSE_PAREN;
2983 
2984     default:
2985       /* Use CPP_EOF as a "no completions possible" code.  */
2986       return CPP_EOF;
2987     }
2988 }
2989 
2990 
2991 /* Subroutine of cp_parser_error and cp_parser_required_error.
2992 
2993    Issue a diagnostic of the form
2994       FILE:LINE: MESSAGE before TOKEN
2995    where TOKEN is the next token in the input stream.  MESSAGE
2996    (specified by the caller) is usually of the form "expected
2997    OTHER-TOKEN".
2998 
2999    This bypasses the check for tentative passing, and potentially
3000    adds material needed by cp_parser_required_error.
3001 
3002    If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3003    suggesting insertion of the missing token.
3004 
3005    Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3006    have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3007    location.  */
3008 
3009 static void
cp_parser_error_1(cp_parser * parser,const char * gmsgid,required_token missing_token_desc,location_t matching_location)3010 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3011 		   required_token missing_token_desc,
3012 		   location_t matching_location)
3013 {
3014   cp_token *token = cp_lexer_peek_token (parser->lexer);
3015   /* This diagnostic makes more sense if it is tagged to the line
3016      of the token we just peeked at.  */
3017   cp_lexer_set_source_position_from_token (token);
3018 
3019   if (token->type == CPP_PRAGMA)
3020     {
3021       error_at (token->location,
3022 		"%<#pragma%> is not allowed here");
3023       cp_parser_skip_to_pragma_eol (parser, token);
3024       return;
3025     }
3026 
3027   /* If this is actually a conflict marker, report it as such.  */
3028   if (token->type == CPP_LSHIFT
3029       || token->type == CPP_RSHIFT
3030       || token->type == CPP_EQ_EQ)
3031     {
3032       location_t loc;
3033       if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3034 	{
3035 	  error_at (loc, "version control conflict marker in file");
3036 	  expanded_location token_exploc = expand_location (token->location);
3037 	  /* Consume tokens until the end of the source line.  */
3038 	  for (;;)
3039 	    {
3040 	      cp_lexer_consume_token (parser->lexer);
3041 	      cp_token *next = cp_lexer_peek_token (parser->lexer);
3042 	      if (next->type == CPP_EOF)
3043 		break;
3044 	      if (next->location == UNKNOWN_LOCATION
3045 		  || loc == UNKNOWN_LOCATION)
3046 		break;
3047 
3048 	      expanded_location next_exploc = expand_location (next->location);
3049 	      if (next_exploc.file != token_exploc.file)
3050 		break;
3051 	      if (next_exploc.line != token_exploc.line)
3052 		break;
3053 	    }
3054 	  return;
3055 	}
3056     }
3057 
3058   auto_diagnostic_group d;
3059   gcc_rich_location richloc (input_location);
3060 
3061   bool added_matching_location = false;
3062 
3063   if (missing_token_desc != RT_NONE)
3064     if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3065       {
3066 	/* Potentially supply a fix-it hint, suggesting to add the
3067 	   missing token immediately after the *previous* token.
3068 	   This may move the primary location within richloc.  */
3069 	enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3070 	location_t prev_token_loc = prev_token->location;
3071 	maybe_suggest_missing_token_insertion (&richloc, ttype,
3072 					       prev_token_loc);
3073 
3074 	/* If matching_location != UNKNOWN_LOCATION, highlight it.
3075 	   Attempt to consolidate diagnostics by printing it as a
3076 	   secondary range within the main diagnostic.  */
3077 	if (matching_location != UNKNOWN_LOCATION)
3078 	  added_matching_location
3079 	    = richloc.add_location_if_nearby (matching_location);
3080       }
3081 
3082   /* If we were parsing a string-literal and there is an unknown name
3083      token right after, then check to see if that could also have been
3084      a literal string by checking the name against a list of known
3085      standard string literal constants defined in header files. If
3086      there is one, then add that as an hint to the error message. */
3087   name_hint h;
3088   if (token->type == CPP_NAME)
3089     if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3090       if (cp_parser_is_string_literal (prev_token))
3091 	{
3092 	  tree name = token->u.value;
3093 	  const char *token_name = IDENTIFIER_POINTER (name);
3094 	  const char *header_hint
3095 	    = get_cp_stdlib_header_for_string_macro_name (token_name);
3096 	  if (header_hint != NULL)
3097 	    h = name_hint (NULL, new suggest_missing_header (token->location,
3098 							     token_name,
3099 							     header_hint));
3100 	}
3101 
3102   /* Actually emit the error.  */
3103   c_parse_error (gmsgid,
3104 		 /* Because c_parser_error does not understand
3105 		    CPP_KEYWORD, keywords are treated like
3106 		    identifiers.  */
3107 		 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3108 		 token->u.value, token->flags, &richloc);
3109 
3110   if (missing_token_desc != RT_NONE)
3111     {
3112       /* If we weren't able to consolidate matching_location, then
3113 	 print it as a secondary diagnostic.  */
3114       if (matching_location != UNKNOWN_LOCATION
3115 	  && !added_matching_location)
3116 	inform (matching_location, "to match this %qs",
3117 		get_matching_symbol (missing_token_desc));
3118     }
3119 }
3120 
3121 /* If not parsing tentatively, issue a diagnostic of the form
3122       FILE:LINE: MESSAGE before TOKEN
3123    where TOKEN is the next token in the input stream.  MESSAGE
3124    (specified by the caller) is usually of the form "expected
3125    OTHER-TOKEN".  */
3126 
3127 static void
cp_parser_error(cp_parser * parser,const char * gmsgid)3128 cp_parser_error (cp_parser* parser, const char* gmsgid)
3129 {
3130   if (!cp_parser_simulate_error (parser))
3131     cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3132 }
3133 
3134 /* Issue an error about name-lookup failing.  NAME is the
3135    IDENTIFIER_NODE DECL is the result of
3136    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
3137    the thing that we hoped to find.  */
3138 
3139 static void
cp_parser_name_lookup_error(cp_parser * parser,tree name,tree decl,name_lookup_error desired,location_t location)3140 cp_parser_name_lookup_error (cp_parser* parser,
3141 			     tree name,
3142 			     tree decl,
3143 			     name_lookup_error desired,
3144 			     location_t location)
3145 {
3146   /* If name lookup completely failed, tell the user that NAME was not
3147      declared.  */
3148   if (decl == error_mark_node)
3149     {
3150       if (parser->scope && parser->scope != global_namespace)
3151 	error_at (location, "%<%E::%E%> has not been declared",
3152 		  parser->scope, name);
3153       else if (parser->scope == global_namespace)
3154 	error_at (location, "%<::%E%> has not been declared", name);
3155       else if (parser->object_scope
3156 	       && !CLASS_TYPE_P (parser->object_scope))
3157 	error_at (location, "request for member %qE in non-class type %qT",
3158 		  name, parser->object_scope);
3159       else if (parser->object_scope)
3160 	error_at (location, "%<%T::%E%> has not been declared",
3161 		  parser->object_scope, name);
3162       else
3163 	error_at (location, "%qE has not been declared", name);
3164     }
3165   else if (parser->scope && parser->scope != global_namespace)
3166     {
3167       switch (desired)
3168 	{
3169 	  case NLE_TYPE:
3170 	    error_at (location, "%<%E::%E%> is not a type",
3171 	    			parser->scope, name);
3172 	    break;
3173 	  case NLE_CXX98:
3174 	    error_at (location, "%<%E::%E%> is not a class or namespace",
3175 	    			parser->scope, name);
3176 	    break;
3177 	  case NLE_NOT_CXX98:
3178 	    error_at (location,
3179 	    	      "%<%E::%E%> is not a class, namespace, or enumeration",
3180 		      parser->scope, name);
3181 	    break;
3182 	  default:
3183 	    gcc_unreachable ();
3184 
3185 	}
3186     }
3187   else if (parser->scope == global_namespace)
3188     {
3189       switch (desired)
3190 	{
3191 	  case NLE_TYPE:
3192 	    error_at (location, "%<::%E%> is not a type", name);
3193 	    break;
3194 	  case NLE_CXX98:
3195 	    error_at (location, "%<::%E%> is not a class or namespace", name);
3196 	    break;
3197 	  case NLE_NOT_CXX98:
3198 	    error_at (location,
3199 		      "%<::%E%> is not a class, namespace, or enumeration",
3200 		      name);
3201 	    break;
3202 	  default:
3203 	    gcc_unreachable ();
3204 	}
3205     }
3206   else
3207     {
3208       switch (desired)
3209 	{
3210 	  case NLE_TYPE:
3211 	    error_at (location, "%qE is not a type", name);
3212 	    break;
3213 	  case NLE_CXX98:
3214 	    error_at (location, "%qE is not a class or namespace", name);
3215 	    break;
3216 	  case NLE_NOT_CXX98:
3217 	    error_at (location,
3218 		      "%qE is not a class, namespace, or enumeration", name);
3219 	    break;
3220 	  default:
3221 	    gcc_unreachable ();
3222 	}
3223     }
3224 }
3225 
3226 /* If we are parsing tentatively, remember that an error has occurred
3227    during this tentative parse.  Returns true if the error was
3228    simulated; false if a message should be issued by the caller.  */
3229 
3230 static bool
cp_parser_simulate_error(cp_parser * parser)3231 cp_parser_simulate_error (cp_parser* parser)
3232 {
3233   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3234     {
3235       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3236       return true;
3237     }
3238   return false;
3239 }
3240 
3241 /* This function is called when a type is defined.  If type
3242    definitions are forbidden at this point, an error message is
3243    issued.  */
3244 
3245 static bool
cp_parser_check_type_definition(cp_parser * parser)3246 cp_parser_check_type_definition (cp_parser* parser)
3247 {
3248   /* If types are forbidden here, issue a message.  */
3249   if (parser->type_definition_forbidden_message)
3250     {
3251       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3252 	 or %qs in the message need to be interpreted.  */
3253       error (parser->type_definition_forbidden_message,
3254 	     parser->type_definition_forbidden_message_arg);
3255       return false;
3256     }
3257   return true;
3258 }
3259 
3260 /* This function is called when the DECLARATOR is processed.  The TYPE
3261    was a type defined in the decl-specifiers.  If it is invalid to
3262    define a type in the decl-specifiers for DECLARATOR, an error is
3263    issued. TYPE_LOCATION is the location of TYPE and is used
3264    for error reporting.  */
3265 
3266 static void
cp_parser_check_for_definition_in_return_type(cp_declarator * declarator,tree type,location_t type_location)3267 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3268 					       tree type, location_t type_location)
3269 {
3270   /* [dcl.fct] forbids type definitions in return types.
3271      Unfortunately, it's not easy to know whether or not we are
3272      processing a return type until after the fact.  */
3273   while (declarator
3274 	 && (declarator->kind == cdk_pointer
3275 	     || declarator->kind == cdk_reference
3276 	     || declarator->kind == cdk_ptrmem))
3277     declarator = declarator->declarator;
3278   if (declarator
3279       && declarator->kind == cdk_function)
3280     {
3281       error_at (type_location,
3282 		"new types may not be defined in a return type");
3283       inform (type_location,
3284 	      "(perhaps a semicolon is missing after the definition of %qT)",
3285 	      type);
3286     }
3287 }
3288 
3289 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3290    "<" in any valid C++ program.  If the next token is indeed "<",
3291    issue a message warning the user about what appears to be an
3292    invalid attempt to form a template-id. LOCATION is the location
3293    of the type-specifier (TYPE) */
3294 
3295 static void
cp_parser_check_for_invalid_template_id(cp_parser * parser,tree type,enum tag_types tag_type,location_t location)3296 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3297 					 tree type,
3298 					 enum tag_types tag_type,
3299 					 location_t location)
3300 {
3301   cp_token_position start = 0;
3302 
3303   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3304     {
3305       if (TREE_CODE (type) == TYPE_DECL)
3306 	type = TREE_TYPE (type);
3307       if (TYPE_P (type) && !template_placeholder_p (type))
3308 	error_at (location, "%qT is not a template", type);
3309       else if (identifier_p (type))
3310 	{
3311 	  if (tag_type != none_type)
3312 	    error_at (location, "%qE is not a class template", type);
3313 	  else
3314 	    error_at (location, "%qE is not a template", type);
3315 	}
3316       else
3317 	error_at (location, "invalid template-id");
3318       /* Remember the location of the invalid "<".  */
3319       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3320 	start = cp_lexer_token_position (parser->lexer, true);
3321       /* Consume the "<".  */
3322       cp_lexer_consume_token (parser->lexer);
3323       /* Parse the template arguments.  */
3324       cp_parser_enclosed_template_argument_list (parser);
3325       /* Permanently remove the invalid template arguments so that
3326 	 this error message is not issued again.  */
3327       if (start)
3328 	cp_lexer_purge_tokens_after (parser->lexer, start);
3329     }
3330 }
3331 
3332 /* If parsing an integral constant-expression, issue an error message
3333    about the fact that THING appeared and return true.  Otherwise,
3334    return false.  In either case, set
3335    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3336 
3337 static bool
cp_parser_non_integral_constant_expression(cp_parser * parser,non_integral_constant thing)3338 cp_parser_non_integral_constant_expression (cp_parser  *parser,
3339 					    non_integral_constant thing)
3340 {
3341   parser->non_integral_constant_expression_p = true;
3342   if (parser->integral_constant_expression_p)
3343     {
3344       if (!parser->allow_non_integral_constant_expression_p)
3345 	{
3346 	  const char *msg = NULL;
3347 	  switch (thing)
3348 	    {
3349   	      case NIC_FLOAT:
3350 		pedwarn (input_location, OPT_Wpedantic,
3351 			 "ISO C++ forbids using a floating-point literal "
3352 			 "in a constant-expression");
3353 		return true;
3354 	      case NIC_CAST:
3355 		error ("a cast to a type other than an integral or "
3356 		       "enumeration type cannot appear in a "
3357 		       "constant-expression");
3358 		return true;
3359 	      case NIC_TYPEID:
3360 		error ("%<typeid%> operator "
3361 		       "cannot appear in a constant-expression");
3362 		return true;
3363 	      case NIC_NCC:
3364 		error ("non-constant compound literals "
3365 		       "cannot appear in a constant-expression");
3366 		return true;
3367 	      case NIC_FUNC_CALL:
3368 		error ("a function call "
3369 		       "cannot appear in a constant-expression");
3370 		return true;
3371 	      case NIC_INC:
3372 		error ("an increment "
3373 		       "cannot appear in a constant-expression");
3374 		return true;
3375 	      case NIC_DEC:
3376 		error ("an decrement "
3377 		       "cannot appear in a constant-expression");
3378 		return true;
3379 	      case NIC_ARRAY_REF:
3380 		error ("an array reference "
3381 		       "cannot appear in a constant-expression");
3382 		return true;
3383 	      case NIC_ADDR_LABEL:
3384 		error ("the address of a label "
3385 		       "cannot appear in a constant-expression");
3386 		return true;
3387 	      case NIC_OVERLOADED:
3388 		error ("calls to overloaded operators "
3389 		       "cannot appear in a constant-expression");
3390 		return true;
3391 	      case NIC_ASSIGNMENT:
3392 		error ("an assignment cannot appear in a constant-expression");
3393 		return true;
3394 	      case NIC_COMMA:
3395 		error ("a comma operator "
3396 		       "cannot appear in a constant-expression");
3397 		return true;
3398 	      case NIC_CONSTRUCTOR:
3399 		error ("a call to a constructor "
3400 		       "cannot appear in a constant-expression");
3401 		return true;
3402 	      case NIC_TRANSACTION:
3403 		error ("a transaction expression "
3404 		       "cannot appear in a constant-expression");
3405 		return true;
3406 	      case NIC_THIS:
3407 		msg = "this";
3408 		break;
3409 	      case NIC_FUNC_NAME:
3410 		msg = "__FUNCTION__";
3411 		break;
3412   	      case NIC_PRETTY_FUNC:
3413 		msg = "__PRETTY_FUNCTION__";
3414 		break;
3415 	      case NIC_C99_FUNC:
3416 		msg = "__func__";
3417 		break;
3418 	      case NIC_VA_ARG:
3419 		msg = "va_arg";
3420 		break;
3421 	      case NIC_ARROW:
3422 		msg = "->";
3423 		break;
3424 	      case NIC_POINT:
3425 		msg = ".";
3426 		break;
3427 	      case NIC_STAR:
3428 		msg = "*";
3429 		break;
3430 	      case NIC_ADDR:
3431 		msg = "&";
3432 		break;
3433 	      case NIC_PREINCREMENT:
3434 		msg = "++";
3435 		break;
3436 	      case NIC_PREDECREMENT:
3437 		msg = "--";
3438 		break;
3439 	      case NIC_NEW:
3440 		msg = "new";
3441 		break;
3442 	      case NIC_DEL:
3443 		msg = "delete";
3444 		break;
3445 	      default:
3446 		gcc_unreachable ();
3447 	    }
3448 	  if (msg)
3449 	    error ("%qs cannot appear in a constant-expression", msg);
3450 	  return true;
3451 	}
3452     }
3453   return false;
3454 }
3455 
3456 /* Emit a diagnostic for an invalid type name.  This function commits
3457    to the current active tentative parse, if any.  (Otherwise, the
3458    problematic construct might be encountered again later, resulting
3459    in duplicate error messages.) LOCATION is the location of ID.  */
3460 
3461 static void
cp_parser_diagnose_invalid_type_name(cp_parser * parser,tree id,location_t location)3462 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3463 				      location_t location)
3464 {
3465   tree decl, ambiguous_decls;
3466   cp_parser_commit_to_tentative_parse (parser);
3467   /* Try to lookup the identifier.  */
3468   decl = cp_parser_lookup_name (parser, id, none_type,
3469 				/*is_template=*/false,
3470 				/*is_namespace=*/false,
3471 				/*check_dependency=*/true,
3472 				&ambiguous_decls, location);
3473   if (ambiguous_decls)
3474     /* If the lookup was ambiguous, an error will already have
3475        been issued.  */
3476     return;
3477   /* If the lookup found a template-name, it means that the user forgot
3478   to specify an argument list. Emit a useful error message.  */
3479   if (DECL_TYPE_TEMPLATE_P (decl))
3480     {
3481       auto_diagnostic_group d;
3482       error_at (location,
3483 		"invalid use of template-name %qE without an argument list",
3484 		decl);
3485       if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3486 	inform (location, "class template argument deduction is only available "
3487 		"with %<-std=c++17%> or %<-std=gnu++17%>");
3488       inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3489     }
3490   else if (TREE_CODE (id) == BIT_NOT_EXPR)
3491     error_at (location, "invalid use of destructor %qD as a type", id);
3492   else if (TREE_CODE (decl) == TYPE_DECL)
3493     /* Something like 'unsigned A a;'  */
3494     error_at (location, "invalid combination of multiple type-specifiers");
3495   else if (!parser->scope)
3496     {
3497       /* Issue an error message.  */
3498       auto_diagnostic_group d;
3499       name_hint hint;
3500       if (TREE_CODE (id) == IDENTIFIER_NODE)
3501 	hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3502       if (const char *suggestion = hint.suggestion ())
3503 	{
3504 	  gcc_rich_location richloc (location);
3505 	  richloc.add_fixit_replace (suggestion);
3506 	  error_at (&richloc,
3507 		    "%qE does not name a type; did you mean %qs?",
3508 		    id, suggestion);
3509 	}
3510       else
3511 	error_at (location, "%qE does not name a type", id);
3512       /* If we're in a template class, it's possible that the user was
3513 	 referring to a type from a base class.  For example:
3514 
3515 	   template <typename T> struct A { typedef T X; };
3516 	   template <typename T> struct B : public A<T> { X x; };
3517 
3518 	 The user should have said "typename A<T>::X".  */
3519       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3520 	inform (location, "C++11 %<constexpr%> only available with "
3521 		"%<-std=c++11%> or %<-std=gnu++11%>");
3522       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3523 	inform (location, "C++11 %<noexcept%> only available with "
3524 		"%<-std=c++11%> or %<-std=gnu++11%>");
3525       else if (TREE_CODE (id) == IDENTIFIER_NODE
3526 	       && (id_equal (id, "module") || id_equal (id, "import")))
3527 	{
3528 	  if (modules_p ())
3529 	    inform (location, "%qE is not recognized as a module control-line",
3530 		    id);
3531 	  else if (cxx_dialect < cxx20)
3532 	    inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3533 		    id);
3534 	  else
3535 	    inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3536 		    ", which is not yet enabled with %<-std=c++20%>", id);
3537 	}
3538       else if (cxx_dialect < cxx11
3539 	       && TREE_CODE (id) == IDENTIFIER_NODE
3540 	       && id_equal (id, "thread_local"))
3541 	inform (location, "C++11 %<thread_local%> only available with "
3542 		"%<-std=c++11%> or %<-std=gnu++11%>");
3543       else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3544 	inform (location, "C++20 %<constinit%> only available with "
3545 		"%<-std=c++20%> or %<-std=gnu++20%>");
3546       else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3547 	inform (location, "%<concept%> only available with %<-std=c++20%> or "
3548 		"%<-fconcepts%>");
3549       else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3550 	inform (location, "%<requires%> only available with %<-std=c++20%> or "
3551 		"%<-fconcepts%>");
3552       else if (processing_template_decl && current_class_type
3553 	       && TYPE_BINFO (current_class_type))
3554 	{
3555 	  for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3556 	       b; b = TREE_CHAIN (b))
3557 	    {
3558 	      tree base_type = BINFO_TYPE (b);
3559 	      if (CLASS_TYPE_P (base_type)
3560 		  && dependent_type_p (base_type))
3561 		{
3562 		  /* Go from a particular instantiation of the
3563 		     template (which will have an empty TYPE_FIELDs),
3564 		     to the main version.  */
3565 		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3566 		  for (tree field = TYPE_FIELDS (base_type);
3567 		       field; field = DECL_CHAIN (field))
3568 		    if (TREE_CODE (field) == TYPE_DECL
3569 			&& DECL_NAME (field) == id)
3570 		      {
3571 			inform (location,
3572 				"(perhaps %<typename %T::%E%> was intended)",
3573 				BINFO_TYPE (b), id);
3574 			goto found;
3575 		      }
3576 		}
3577 	    }
3578 	found:;
3579 	}
3580     }
3581   /* Here we diagnose qualified-ids where the scope is actually correct,
3582      but the identifier does not resolve to a valid type name.  */
3583   else if (parser->scope != error_mark_node)
3584     {
3585       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3586 	{
3587 	  auto_diagnostic_group d;
3588 	  name_hint hint;
3589 	  if (decl == error_mark_node)
3590 	    hint = suggest_alternative_in_explicit_scope (location, id,
3591 							  parser->scope);
3592 	  const char *suggestion = hint.suggestion ();
3593 	  gcc_rich_location richloc (location_of (id));
3594 	  if (suggestion)
3595 	    richloc.add_fixit_replace (suggestion);
3596 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3597 	    {
3598 	      if (suggestion)
3599 		error_at (&richloc,
3600 			  "%qE in namespace %qE does not name a template"
3601 			  " type; did you mean %qs?",
3602 			  id, parser->scope, suggestion);
3603 	      else
3604 		error_at (&richloc,
3605 			  "%qE in namespace %qE does not name a template type",
3606 			  id, parser->scope);
3607 	    }
3608 	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3609 	    {
3610 	      if (suggestion)
3611 		error_at (&richloc,
3612 			  "%qE in namespace %qE does not name a template"
3613 			  " type; did you mean %qs?",
3614 			  TREE_OPERAND (id, 0), parser->scope, suggestion);
3615 	      else
3616 		error_at (&richloc,
3617 			  "%qE in namespace %qE does not name a template"
3618 			  " type",
3619 			  TREE_OPERAND (id, 0), parser->scope);
3620 	    }
3621 	  else
3622 	    {
3623 	      if (suggestion)
3624 		error_at (&richloc,
3625 			  "%qE in namespace %qE does not name a type"
3626 			  "; did you mean %qs?",
3627 			  id, parser->scope, suggestion);
3628 	      else
3629 		error_at (&richloc,
3630 			  "%qE in namespace %qE does not name a type",
3631 			  id, parser->scope);
3632 	    }
3633 	  if (DECL_P (decl))
3634 	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3635 	}
3636       else if (CLASS_TYPE_P (parser->scope)
3637 	       && constructor_name_p (id, parser->scope))
3638 	{
3639 	  /* A<T>::A<T>() */
3640 	  auto_diagnostic_group d;
3641 	  error_at (location, "%<%T::%E%> names the constructor, not"
3642 		    " the type", parser->scope, id);
3643 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3644 	    error_at (location, "and %qT has no template constructors",
3645 		      parser->scope);
3646 	}
3647       else if (TYPE_P (parser->scope)
3648 	       && dependent_scope_p (parser->scope))
3649 	{
3650 	  gcc_rich_location richloc (location);
3651 	  richloc.add_fixit_insert_before ("typename ");
3652 	  if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3653 	    error_at (&richloc,
3654 		      "need %<typename%> before %<%T::%D::%E%> because "
3655 		      "%<%T::%D%> is a dependent scope",
3656 		      TYPE_CONTEXT (parser->scope),
3657 		      TYPENAME_TYPE_FULLNAME (parser->scope),
3658 		      id,
3659 		      TYPE_CONTEXT (parser->scope),
3660 		      TYPENAME_TYPE_FULLNAME (parser->scope));
3661 	  else
3662 	    error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3663 		      "%qT is a dependent scope",
3664 		      parser->scope, id, parser->scope);
3665 	}
3666       else if (TYPE_P (parser->scope))
3667 	{
3668 	  auto_diagnostic_group d;
3669 	  if (!COMPLETE_TYPE_P (parser->scope))
3670 	    cxx_incomplete_type_error (location_of (id), NULL_TREE,
3671 				       parser->scope);
3672 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3673 	    error_at (location_of (id),
3674 		      "%qE in %q#T does not name a template type",
3675 		      id, parser->scope);
3676 	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3677 	    error_at (location_of (id),
3678 		      "%qE in %q#T does not name a template type",
3679 		      TREE_OPERAND (id, 0), parser->scope);
3680 	  else
3681 	    error_at (location_of (id),
3682 		      "%qE in %q#T does not name a type",
3683 		      id, parser->scope);
3684 	  if (DECL_P (decl))
3685 	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3686 	}
3687       else
3688 	gcc_unreachable ();
3689     }
3690 }
3691 
3692 /* Check for a common situation where a type-name should be present,
3693    but is not, and issue a sensible error message.  Returns true if an
3694    invalid type-name was detected.
3695 
3696    The situation handled by this function are variable declarations of the
3697    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3698    Usually, `ID' should name a type, but if we got here it means that it
3699    does not. We try to emit the best possible error message depending on
3700    how exactly the id-expression looks like.  */
3701 
3702 static bool
cp_parser_parse_and_diagnose_invalid_type_name(cp_parser * parser)3703 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3704 {
3705   tree id;
3706   cp_token *token = cp_lexer_peek_token (parser->lexer);
3707 
3708   /* Avoid duplicate error about ambiguous lookup.  */
3709   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3710     {
3711       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3712       if (next->type == CPP_NAME && next->error_reported)
3713 	goto out;
3714     }
3715 
3716   cp_parser_parse_tentatively (parser);
3717   id = cp_parser_id_expression (parser,
3718 				/*template_keyword_p=*/false,
3719 				/*check_dependency_p=*/true,
3720 				/*template_p=*/NULL,
3721 				/*declarator_p=*/true,
3722 				/*optional_p=*/false);
3723   /* If the next token is a (, this is a function with no explicit return
3724      type, i.e. constructor, destructor or conversion op.  */
3725   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3726       || TREE_CODE (id) == TYPE_DECL)
3727     {
3728       cp_parser_abort_tentative_parse (parser);
3729       return false;
3730     }
3731   if (!cp_parser_parse_definitely (parser))
3732     return false;
3733 
3734   /* Emit a diagnostic for the invalid type.  */
3735   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3736  out:
3737   /* If we aren't in the middle of a declarator (i.e. in a
3738      parameter-declaration-clause), skip to the end of the declaration;
3739      there's no point in trying to process it.  */
3740   if (!parser->in_declarator_p)
3741     cp_parser_skip_to_end_of_block_or_statement (parser);
3742   return true;
3743 }
3744 
3745 /* Consume tokens up to, and including, the next non-nested closing `)'.
3746    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3747    are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3748    found an unnested token of that type.  */
3749 
3750 static int
cp_parser_skip_to_closing_parenthesis_1(cp_parser * parser,bool recovering,cpp_ttype or_ttype,bool consume_paren)3751 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3752 					 bool recovering,
3753 					 cpp_ttype or_ttype,
3754 					 bool consume_paren)
3755 {
3756   unsigned paren_depth = 0;
3757   unsigned brace_depth = 0;
3758   unsigned square_depth = 0;
3759   unsigned condop_depth = 0;
3760 
3761   if (recovering && or_ttype == CPP_EOF
3762       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3763     return 0;
3764 
3765   while (true)
3766     {
3767       cp_token * token = cp_lexer_peek_token (parser->lexer);
3768 
3769       /* Have we found what we're looking for before the closing paren?  */
3770       if (token->type == or_ttype && or_ttype != CPP_EOF
3771 	  && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3772 	return -1;
3773 
3774       switch (token->type)
3775 	{
3776 	case CPP_PRAGMA_EOL:
3777 	  if (!parser->lexer->in_pragma)
3778 	    break;
3779 	  /* FALLTHRU */
3780 	case CPP_EOF:
3781 	  /* If we've run out of tokens, then there is no closing `)'.  */
3782 	  return 0;
3783 
3784         /* This is good for lambda expression capture-lists.  */
3785         case CPP_OPEN_SQUARE:
3786           ++square_depth;
3787           break;
3788         case CPP_CLOSE_SQUARE:
3789           if (!square_depth--)
3790             return 0;
3791           break;
3792 
3793 	case CPP_SEMICOLON:
3794 	  /* This matches the processing in skip_to_end_of_statement.  */
3795 	  if (!brace_depth)
3796 	    return 0;
3797 	  break;
3798 
3799 	case CPP_OPEN_BRACE:
3800 	  ++brace_depth;
3801 	  break;
3802 	case CPP_CLOSE_BRACE:
3803 	  if (!brace_depth--)
3804 	    return 0;
3805 	  break;
3806 
3807 	case CPP_OPEN_PAREN:
3808 	  if (!brace_depth)
3809 	    ++paren_depth;
3810 	  break;
3811 
3812 	case CPP_CLOSE_PAREN:
3813 	  if (!brace_depth && !paren_depth--)
3814 	    {
3815 	      if (consume_paren)
3816 		cp_lexer_consume_token (parser->lexer);
3817 	      return 1;
3818 	    }
3819 	  break;
3820 
3821 	case CPP_QUERY:
3822 	  if (!brace_depth && !paren_depth && !square_depth)
3823 	    ++condop_depth;
3824 	  break;
3825 
3826 	case CPP_COLON:
3827 	  if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3828 	    condop_depth--;
3829 	  break;
3830 
3831 	case CPP_KEYWORD:
3832 	  if (token->keyword != RID__EXPORT
3833 	      && token->keyword != RID__MODULE
3834 	      && token->keyword != RID__IMPORT)
3835 	    break;
3836 	  /* FALLTHROUGH  */
3837 
3838 	case CPP_PRAGMA:
3839 	  /* We fell into a pragma.  Skip it, and continue. */
3840 	  cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3841 	  continue;
3842 
3843 	default:
3844 	  break;
3845 	}
3846 
3847       /* Consume the token.  */
3848       cp_lexer_consume_token (parser->lexer);
3849     }
3850 }
3851 
3852 /* Consume tokens up to, and including, the next non-nested closing `)'.
3853    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3854    are doing error recovery. Returns -1 if OR_COMMA is true and we
3855    found an unnested token of that type.  */
3856 
3857 static int
cp_parser_skip_to_closing_parenthesis(cp_parser * parser,bool recovering,bool or_comma,bool consume_paren)3858 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3859 				       bool recovering,
3860 				       bool or_comma,
3861 				       bool consume_paren)
3862 {
3863   cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3864   return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3865 						  ttype, consume_paren);
3866 }
3867 
3868 /* Consume tokens until we reach the end of the current statement.
3869    Normally, that will be just before consuming a `;'.  However, if a
3870    non-nested `}' comes first, then we stop before consuming that.  */
3871 
3872 static void
cp_parser_skip_to_end_of_statement(cp_parser * parser)3873 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3874 {
3875   unsigned nesting_depth = 0;
3876 
3877   /* Unwind generic function template scope if necessary.  */
3878   if (parser->fully_implicit_function_template_p)
3879     abort_fully_implicit_template (parser);
3880 
3881   while (true)
3882     {
3883       cp_token *token = cp_lexer_peek_token (parser->lexer);
3884 
3885       switch (token->type)
3886 	{
3887 	case CPP_PRAGMA_EOL:
3888 	  if (!parser->lexer->in_pragma)
3889 	    break;
3890 	  /* FALLTHRU */
3891 	case CPP_EOF:
3892 	  /* If we've run out of tokens, stop.  */
3893 	  return;
3894 
3895 	case CPP_SEMICOLON:
3896 	  /* If the next token is a `;', we have reached the end of the
3897 	     statement.  */
3898 	  if (!nesting_depth)
3899 	    return;
3900 	  break;
3901 
3902 	case CPP_CLOSE_BRACE:
3903 	  /* If this is a non-nested '}', stop before consuming it.
3904 	     That way, when confronted with something like:
3905 
3906 	       { 3 + }
3907 
3908 	     we stop before consuming the closing '}', even though we
3909 	     have not yet reached a `;'.  */
3910 	  if (nesting_depth == 0)
3911 	    return;
3912 
3913 	  /* If it is the closing '}' for a block that we have
3914 	     scanned, stop -- but only after consuming the token.
3915 	     That way given:
3916 
3917 		void f g () { ... }
3918 		typedef int I;
3919 
3920 	     we will stop after the body of the erroneously declared
3921 	     function, but before consuming the following `typedef'
3922 	     declaration.  */
3923 	  if (--nesting_depth == 0)
3924 	    {
3925 	      cp_lexer_consume_token (parser->lexer);
3926 	      return;
3927 	    }
3928 	  break;
3929 
3930 	case CPP_OPEN_BRACE:
3931 	  ++nesting_depth;
3932 	  break;
3933 
3934 	case CPP_KEYWORD:
3935 	  if (token->keyword != RID__EXPORT
3936 	      && token->keyword != RID__MODULE
3937 	      && token->keyword != RID__IMPORT)
3938 	    break;
3939 	  /* FALLTHROUGH  */
3940 
3941 	case CPP_PRAGMA:
3942 	  /* We fell into a pragma.  Skip it, and continue or return. */
3943 	  cp_parser_skip_to_pragma_eol (parser, token);
3944 	  if (!nesting_depth)
3945 	    return;
3946 	  continue;
3947 
3948 	default:
3949 	  break;
3950 	}
3951 
3952       /* Consume the token.  */
3953       cp_lexer_consume_token (parser->lexer);
3954     }
3955 }
3956 
3957 /* This function is called at the end of a statement or declaration.
3958    If the next token is a semicolon, it is consumed; otherwise, error
3959    recovery is attempted.  */
3960 
3961 static void
cp_parser_consume_semicolon_at_end_of_statement(cp_parser * parser)3962 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3963 {
3964   /* Look for the trailing `;'.  */
3965   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3966     {
3967       /* If there is additional (erroneous) input, skip to the end of
3968 	 the statement.  */
3969       cp_parser_skip_to_end_of_statement (parser);
3970       /* If the next token is now a `;', consume it.  */
3971       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3972 	cp_lexer_consume_token (parser->lexer);
3973     }
3974 }
3975 
3976 /* Skip tokens until we have consumed an entire block, or until we
3977    have consumed a non-nested `;'.  */
3978 
3979 static void
cp_parser_skip_to_end_of_block_or_statement(cp_parser * parser)3980 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3981 {
3982   int nesting_depth = 0;
3983 
3984   /* Unwind generic function template scope if necessary.  */
3985   if (parser->fully_implicit_function_template_p)
3986     abort_fully_implicit_template (parser);
3987 
3988   while (nesting_depth >= 0)
3989     {
3990       cp_token *token = cp_lexer_peek_token (parser->lexer);
3991 
3992       switch (token->type)
3993 	{
3994 	case CPP_PRAGMA_EOL:
3995 	  if (!parser->lexer->in_pragma)
3996 	    break;
3997 	  /* FALLTHRU */
3998 	case CPP_EOF:
3999 	  /* If we've run out of tokens, stop.  */
4000 	  return;
4001 
4002 	case CPP_SEMICOLON:
4003 	  /* Stop if this is an unnested ';'. */
4004 	  if (!nesting_depth)
4005 	    nesting_depth = -1;
4006 	  break;
4007 
4008 	case CPP_CLOSE_BRACE:
4009 	  /* Stop if this is an unnested '}', or closes the outermost
4010 	     nesting level.  */
4011 	  nesting_depth--;
4012 	  if (nesting_depth < 0)
4013 	    return;
4014 	  if (!nesting_depth)
4015 	    nesting_depth = -1;
4016 	  break;
4017 
4018 	case CPP_OPEN_BRACE:
4019 	  /* Nest. */
4020 	  nesting_depth++;
4021 	  break;
4022 
4023 	case CPP_KEYWORD:
4024 	  if (token->keyword != RID__EXPORT
4025 	      && token->keyword != RID__MODULE
4026 	      && token->keyword != RID__IMPORT)
4027 	    break;
4028 	  /* FALLTHROUGH  */
4029 
4030 	case CPP_PRAGMA:
4031 	  /* Skip it, and continue or return. */
4032 	  cp_parser_skip_to_pragma_eol (parser, token);
4033 	  if (!nesting_depth)
4034 	    return;
4035 	  continue;
4036 
4037 	default:
4038 	  break;
4039 	}
4040 
4041       /* Consume the token.  */
4042       cp_lexer_consume_token (parser->lexer);
4043     }
4044 }
4045 
4046 /* Skip tokens until a non-nested closing curly brace is the next
4047    token, or there are no more tokens. Return true in the first case,
4048    false otherwise.  */
4049 
4050 static bool
cp_parser_skip_to_closing_brace(cp_parser * parser)4051 cp_parser_skip_to_closing_brace (cp_parser *parser)
4052 {
4053   unsigned nesting_depth = 0;
4054 
4055   while (true)
4056     {
4057       cp_token *token = cp_lexer_peek_token (parser->lexer);
4058 
4059       switch (token->type)
4060 	{
4061 	case CPP_PRAGMA_EOL:
4062 	  if (!parser->lexer->in_pragma)
4063 	    break;
4064 	  /* FALLTHRU */
4065 	case CPP_EOF:
4066 	  /* If we've run out of tokens, stop.  */
4067 	  return false;
4068 
4069 	case CPP_CLOSE_BRACE:
4070 	  /* If the next token is a non-nested `}', then we have reached
4071 	     the end of the current block.  */
4072 	  if (nesting_depth-- == 0)
4073 	    return true;
4074 	  break;
4075 
4076 	case CPP_OPEN_BRACE:
4077 	  /* If it the next token is a `{', then we are entering a new
4078 	     block.  Consume the entire block.  */
4079 	  ++nesting_depth;
4080 	  break;
4081 
4082 	default:
4083 	  break;
4084 	}
4085 
4086       /* Consume the token.  */
4087       cp_lexer_consume_token (parser->lexer);
4088     }
4089 }
4090 
4091 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
4092    parameter is the PRAGMA token, allowing us to purge the entire pragma
4093    sequence.  PRAGMA_TOK can be NULL, if we're speculatively scanning
4094    forwards (not error recovery).  */
4095 
4096 static void
cp_parser_skip_to_pragma_eol(cp_parser * parser,cp_token * pragma_tok)4097 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4098 {
4099   cp_token *token;
4100 
4101   do
4102     {
4103       /* The preprocessor makes sure that a PRAGMA_EOL token appears
4104          before an EOF token, even when the EOF is on the pragma line.
4105          We should never get here without being inside a deferred
4106          pragma.  */
4107       gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4108       token = cp_lexer_consume_token (parser->lexer);
4109     }
4110   while (token->type != CPP_PRAGMA_EOL);
4111 
4112   if (pragma_tok)
4113     {
4114       parser->lexer->in_pragma = false;
4115       if (parser->lexer->in_omp_attribute_pragma
4116 	  && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4117 	{
4118 	  parser->lexer = parser->lexer->next;
4119 	  /* Put the current source position back where it was before this
4120 	     lexer was pushed.  */
4121 	  cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4122 	}
4123     }
4124 }
4125 
4126 /* Require pragma end of line, resyncing with it as necessary.  The
4127    arguments are as for cp_parser_skip_to_pragma_eol.  */
4128 
4129 static void
cp_parser_require_pragma_eol(cp_parser * parser,cp_token * pragma_tok)4130 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4131 {
4132   parser->lexer->in_pragma = false;
4133   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4134     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4135   else if (parser->lexer->in_omp_attribute_pragma
4136 	   && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4137     {
4138       parser->lexer = parser->lexer->next;
4139       /* Put the current source position back where it was before this
4140 	 lexer was pushed.  */
4141       cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4142     }
4143 }
4144 
4145 /* This is a simple wrapper around make_typename_type. When the id is
4146    an unresolved identifier node, we can provide a superior diagnostic
4147    using cp_parser_diagnose_invalid_type_name.  */
4148 
4149 static tree
cp_parser_make_typename_type(cp_parser * parser,tree id,location_t id_location)4150 cp_parser_make_typename_type (cp_parser *parser, tree id,
4151 			      location_t id_location)
4152 {
4153   tree result;
4154   if (identifier_p (id))
4155     {
4156       result = make_typename_type (parser->scope, id, typename_type,
4157 				   /*complain=*/tf_none);
4158       if (result == error_mark_node)
4159 	cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4160       return result;
4161     }
4162   return make_typename_type (parser->scope, id, typename_type, tf_error);
4163 }
4164 
4165 /* This is a wrapper around the
4166    make_{pointer,ptrmem,reference}_declarator functions that decides
4167    which one to call based on the CODE and CLASS_TYPE arguments. The
4168    CODE argument should be one of the values returned by
4169    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
4170    appertain to the pointer or reference.  */
4171 
4172 static cp_declarator *
cp_parser_make_indirect_declarator(enum tree_code code,tree class_type,cp_cv_quals cv_qualifiers,cp_declarator * target,tree attributes)4173 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4174 				    cp_cv_quals cv_qualifiers,
4175 				    cp_declarator *target,
4176 				    tree attributes)
4177 {
4178   if (code == ERROR_MARK || target == cp_error_declarator)
4179     return cp_error_declarator;
4180 
4181   if (code == INDIRECT_REF)
4182     if (class_type == NULL_TREE)
4183       return make_pointer_declarator (cv_qualifiers, target, attributes);
4184     else
4185       return make_ptrmem_declarator (cv_qualifiers, class_type,
4186 				     target, attributes);
4187   else if (code == ADDR_EXPR && class_type == NULL_TREE)
4188     return make_reference_declarator (cv_qualifiers, target,
4189 				      false, attributes);
4190   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4191     return make_reference_declarator (cv_qualifiers, target,
4192 				      true, attributes);
4193   gcc_unreachable ();
4194 }
4195 
4196 /* Create a new C++ parser.  */
4197 
4198 static cp_parser *
cp_parser_new(cp_lexer * lexer)4199 cp_parser_new (cp_lexer *lexer)
4200 {
4201   /* Initialize the binops_by_token so that we can get the tree
4202      directly from the token.  */
4203   for (unsigned i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
4204     binops_by_token[binops[i].token_type] = binops[i];
4205 
4206   cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4207   parser->lexer = lexer;
4208   parser->context = cp_parser_context_new (NULL);
4209 
4210   /* For now, we always accept GNU extensions.  */
4211   parser->allow_gnu_extensions_p = 1;
4212 
4213   /* The `>' token is a greater-than operator, not the end of a
4214      template-id.  */
4215   parser->greater_than_is_operator_p = true;
4216 
4217   parser->default_arg_ok_p = true;
4218 
4219   /* We are not parsing a constant-expression.  */
4220   parser->integral_constant_expression_p = false;
4221   parser->allow_non_integral_constant_expression_p = false;
4222   parser->non_integral_constant_expression_p = false;
4223 
4224   /* Local variable names are not forbidden.  */
4225   parser->local_variables_forbidden_p = 0;
4226 
4227   /* We are not processing an `extern "C"' declaration.  */
4228   parser->in_unbraced_linkage_specification_p = false;
4229 
4230   /* We are not processing a declarator.  */
4231   parser->in_declarator_p = false;
4232 
4233   /* We are not processing a template-argument-list.  */
4234   parser->in_template_argument_list_p = false;
4235 
4236   /* We are not in an iteration statement.  */
4237   parser->in_statement = 0;
4238 
4239   /* We are not in a switch statement.  */
4240   parser->in_switch_statement_p = false;
4241 
4242   /* We are not parsing a type-id inside an expression.  */
4243   parser->in_type_id_in_expr_p = false;
4244 
4245   /* String literals should be translated to the execution character set.  */
4246   parser->translate_strings_p = true;
4247 
4248   /* We are not parsing a function body.  */
4249   parser->in_function_body = false;
4250 
4251   /* We can correct until told otherwise.  */
4252   parser->colon_corrects_to_scope_p = true;
4253 
4254   /* The unparsed function queue is empty.  */
4255   push_unparsed_function_queues (parser);
4256 
4257   /* There are no classes being defined.  */
4258   parser->num_classes_being_defined = 0;
4259 
4260   /* No template parameters apply.  */
4261   parser->num_template_parameter_lists = 0;
4262 
4263   /* Special parsing data structures.  */
4264   parser->omp_declare_simd = NULL;
4265   parser->oacc_routine = NULL;
4266 
4267   /* Not declaring an implicit function template.  */
4268   parser->auto_is_implicit_function_template_parm_p = false;
4269   parser->fully_implicit_function_template_p = false;
4270   parser->implicit_template_parms = 0;
4271   parser->implicit_template_scope = 0;
4272 
4273   /* Allow constrained-type-specifiers. */
4274   parser->prevent_constrained_type_specifiers = 0;
4275 
4276   /* We haven't yet seen an 'extern "C"'.  */
4277   parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4278 
4279   return parser;
4280 }
4281 
4282 /* Create a cp_lexer structure which will emit the tokens in CACHE
4283    and push it onto the parser's lexer stack.  This is used for delayed
4284    parsing of in-class method bodies and default arguments, and should
4285    not be confused with tentative parsing.  */
4286 static void
cp_parser_push_lexer_for_tokens(cp_parser * parser,cp_token_cache * cache)4287 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4288 {
4289   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4290   lexer->next = parser->lexer;
4291   parser->lexer = lexer;
4292 
4293   /* Move the current source position to that of the first token in the
4294      new lexer.  */
4295   cp_lexer_set_source_position_from_token (lexer->next_token);
4296 }
4297 
4298 /* Pop the top lexer off the parser stack.  This is never used for the
4299    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
4300 static void
cp_parser_pop_lexer(cp_parser * parser)4301 cp_parser_pop_lexer (cp_parser *parser)
4302 {
4303   cp_lexer *lexer = parser->lexer;
4304   parser->lexer = lexer->next;
4305   cp_lexer_destroy (lexer);
4306 
4307   /* Put the current source position back where it was before this
4308      lexer was pushed.  */
4309   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4310 }
4311 
4312 /* Lexical conventions [gram.lex]  */
4313 
4314 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
4315    identifier.  */
4316 
4317 static cp_expr
cp_parser_identifier(cp_parser * parser)4318 cp_parser_identifier (cp_parser* parser)
4319 {
4320   cp_token *token;
4321 
4322   /* Look for the identifier.  */
4323   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4324   /* Return the value.  */
4325   if (token)
4326     return cp_expr (token->u.value, token->location);
4327   else
4328     return error_mark_node;
4329 }
4330 
4331 /* Parse a sequence of adjacent string constants.  Returns a
4332    TREE_STRING representing the combined, nul-terminated string
4333    constant.  If TRANSLATE is true, translate the string to the
4334    execution character set.  If WIDE_OK is true, a wide string is
4335    invalid here.
4336 
4337    C++98 [lex.string] says that if a narrow string literal token is
4338    adjacent to a wide string literal token, the behavior is undefined.
4339    However, C99 6.4.5p4 says that this results in a wide string literal.
4340    We follow C99 here, for consistency with the C front end.
4341 
4342    This code is largely lifted from lex_string() in c-lex.cc.
4343 
4344    FUTURE: ObjC++ will need to handle @-strings here.  */
4345 static cp_expr
cp_parser_string_literal(cp_parser * parser,bool translate,bool wide_ok,bool lookup_udlit=true)4346 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4347 			  bool lookup_udlit = true)
4348 {
4349   tree value;
4350   size_t count;
4351   struct obstack str_ob;
4352   struct obstack loc_ob;
4353   cpp_string str, istr, *strs;
4354   cp_token *tok;
4355   enum cpp_ttype type, curr_type;
4356   int have_suffix_p = 0;
4357   tree string_tree;
4358   tree suffix_id = NULL_TREE;
4359   bool curr_tok_is_userdef_p = false;
4360 
4361   tok = cp_lexer_peek_token (parser->lexer);
4362   if (!cp_parser_is_string_literal (tok))
4363     {
4364       cp_parser_error (parser, "expected string-literal");
4365       return error_mark_node;
4366     }
4367 
4368   location_t loc = tok->location;
4369 
4370   if (cpp_userdef_string_p (tok->type))
4371     {
4372       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4373       curr_type = cpp_userdef_string_remove_type (tok->type);
4374       curr_tok_is_userdef_p = true;
4375     }
4376   else
4377     {
4378       string_tree = tok->u.value;
4379       curr_type = tok->type;
4380     }
4381   type = curr_type;
4382 
4383   /* Try to avoid the overhead of creating and destroying an obstack
4384      for the common case of just one string.  */
4385   if (!cp_parser_is_string_literal
4386       (cp_lexer_peek_nth_token (parser->lexer, 2)))
4387     {
4388       cp_lexer_consume_token (parser->lexer);
4389 
4390       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4391       str.len = TREE_STRING_LENGTH (string_tree);
4392       count = 1;
4393 
4394       if (curr_tok_is_userdef_p)
4395 	{
4396 	  suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4397 	  have_suffix_p = 1;
4398 	  curr_type = cpp_userdef_string_remove_type (tok->type);
4399 	}
4400       else
4401 	curr_type = tok->type;
4402 
4403       strs = &str;
4404     }
4405   else
4406     {
4407       location_t last_tok_loc = tok->location;
4408       gcc_obstack_init (&str_ob);
4409       gcc_obstack_init (&loc_ob);
4410       count = 0;
4411 
4412       do
4413 	{
4414 	  cp_lexer_consume_token (parser->lexer);
4415 	  count++;
4416 	  str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4417 	  str.len = TREE_STRING_LENGTH (string_tree);
4418 
4419 	  if (curr_tok_is_userdef_p)
4420 	    {
4421 	      tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4422 	      if (have_suffix_p == 0)
4423 		{
4424 		  suffix_id = curr_suffix_id;
4425 		  have_suffix_p = 1;
4426 		}
4427 	      else if (have_suffix_p == 1
4428 		       && curr_suffix_id != suffix_id)
4429 		{
4430 		  error ("inconsistent user-defined literal suffixes"
4431 			 " %qD and %qD in string literal",
4432 			 suffix_id, curr_suffix_id);
4433 		  have_suffix_p = -1;
4434 		}
4435 	      curr_type = cpp_userdef_string_remove_type (tok->type);
4436 	    }
4437 	  else
4438 	    curr_type = tok->type;
4439 
4440 	  if (type != curr_type)
4441 	    {
4442 	      if (type == CPP_STRING)
4443 		type = curr_type;
4444 	      else if (curr_type != CPP_STRING)
4445 		{
4446 		  rich_location rich_loc (line_table, tok->location);
4447 		  rich_loc.add_range (last_tok_loc);
4448 		  error_at (&rich_loc,
4449 			    "concatenation of string literals with "
4450 			    "conflicting encoding prefixes");
4451 		}
4452 	    }
4453 
4454 	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
4455 	  obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4456 
4457 	  last_tok_loc = tok->location;
4458 
4459 	  tok = cp_lexer_peek_token (parser->lexer);
4460 	  if (cpp_userdef_string_p (tok->type))
4461 	    {
4462 	      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4463 	      curr_type = cpp_userdef_string_remove_type (tok->type);
4464 	      curr_tok_is_userdef_p = true;
4465 	    }
4466 	  else
4467 	    {
4468 	      string_tree = tok->u.value;
4469 	      curr_type = tok->type;
4470 	      curr_tok_is_userdef_p = false;
4471 	    }
4472 	}
4473       while (cp_parser_is_string_literal (tok));
4474 
4475       /* A string literal built by concatenation has its caret=start at
4476 	 the start of the initial string, and its finish at the finish of
4477 	 the final string literal.  */
4478       loc = make_location (loc, loc, get_finish (last_tok_loc));
4479 
4480       strs = (cpp_string *) obstack_finish (&str_ob);
4481     }
4482 
4483   if (type != CPP_STRING && !wide_ok)
4484     {
4485       cp_parser_error (parser, "a wide string is invalid in this context");
4486       type = CPP_STRING;
4487     }
4488 
4489   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4490       (parse_in, strs, count, &istr, type))
4491     {
4492       value = build_string (istr.len, (const char *)istr.text);
4493       free (CONST_CAST (unsigned char *, istr.text));
4494       if (count > 1)
4495 	{
4496 	  location_t *locs = (location_t *)obstack_finish (&loc_ob);
4497 	  gcc_assert (g_string_concat_db);
4498 	  g_string_concat_db->record_string_concatenation (count, locs);
4499 	}
4500 
4501       switch (type)
4502 	{
4503 	default:
4504 	case CPP_STRING:
4505 	  TREE_TYPE (value) = char_array_type_node;
4506 	  break;
4507 	case CPP_UTF8STRING:
4508 	  if (flag_char8_t)
4509 	    TREE_TYPE (value) = char8_array_type_node;
4510 	  else
4511 	    TREE_TYPE (value) = char_array_type_node;
4512 	  break;
4513 	case CPP_STRING16:
4514 	  TREE_TYPE (value) = char16_array_type_node;
4515 	  break;
4516 	case CPP_STRING32:
4517 	  TREE_TYPE (value) = char32_array_type_node;
4518 	  break;
4519 	case CPP_WSTRING:
4520 	  TREE_TYPE (value) = wchar_array_type_node;
4521 	  break;
4522 	}
4523 
4524       value = fix_string_type (value);
4525 
4526       if (have_suffix_p)
4527 	{
4528 	  tree literal = build_userdef_literal (suffix_id, value,
4529 						OT_NONE, NULL_TREE);
4530 	  if (lookup_udlit)
4531 	    value = cp_parser_userdef_string_literal (literal);
4532 	  else
4533 	    value = literal;
4534 	}
4535     }
4536   else
4537     /* cpp_interpret_string has issued an error.  */
4538     value = error_mark_node;
4539 
4540   if (count > 1)
4541     {
4542       obstack_free (&str_ob, 0);
4543       obstack_free (&loc_ob, 0);
4544     }
4545 
4546   return cp_expr (value, loc);
4547 }
4548 
4549 /* Look up a literal operator with the name and the exact arguments.  */
4550 
4551 static tree
lookup_literal_operator(tree name,vec<tree,va_gc> * args)4552 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4553 {
4554   tree decl = lookup_name (name);
4555   if (!decl || !is_overloaded_fn (decl))
4556     return error_mark_node;
4557 
4558   for (lkp_iterator iter (decl); iter; ++iter)
4559     {
4560       tree fn = *iter;
4561 
4562       if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4563 	{
4564 	  unsigned int ix;
4565 	  bool found = true;
4566 
4567 	  for (ix = 0;
4568 	       found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4569 	       ++ix, parmtypes = TREE_CHAIN (parmtypes))
4570 	    {
4571 	      tree tparm = TREE_VALUE (parmtypes);
4572 	      tree targ = TREE_TYPE ((*args)[ix]);
4573 	      bool ptr = TYPE_PTR_P (tparm);
4574 	      bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4575 	      if ((ptr || arr || !same_type_p (tparm, targ))
4576 		  && (!ptr || !arr
4577 		      || !same_type_p (TREE_TYPE (tparm),
4578 				       TREE_TYPE (targ))))
4579 		found = false;
4580 	    }
4581 
4582 	  if (found
4583 	      && ix == vec_safe_length (args)
4584 	      /* May be this should be sufficient_parms_p instead,
4585 		 depending on how exactly should user-defined literals
4586 		 work in presence of default arguments on the literal
4587 		 operator parameters.  */
4588 	      && parmtypes == void_list_node)
4589 	    return decl;
4590 	}
4591     }
4592 
4593   return error_mark_node;
4594 }
4595 
4596 /* Parse a user-defined char constant.  Returns a call to a user-defined
4597    literal operator taking the character as an argument.  */
4598 
4599 static cp_expr
cp_parser_userdef_char_literal(cp_parser * parser)4600 cp_parser_userdef_char_literal (cp_parser *parser)
4601 {
4602   cp_token *token = cp_lexer_consume_token (parser->lexer);
4603   tree literal = token->u.value;
4604   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4605   tree value = USERDEF_LITERAL_VALUE (literal);
4606   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4607   tree decl, result;
4608 
4609   /* Build up a call to the user-defined operator  */
4610   /* Lookup the name we got back from the id-expression.  */
4611   releasing_vec args;
4612   vec_safe_push (args, value);
4613   decl = lookup_literal_operator (name, args);
4614   if (!decl || decl == error_mark_node)
4615     {
4616       error ("unable to find character literal operator %qD with %qT argument",
4617 	     name, TREE_TYPE (value));
4618       return error_mark_node;
4619     }
4620   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4621   return result;
4622 }
4623 
4624 /* A subroutine of cp_parser_userdef_numeric_literal to
4625    create a char... template parameter pack from a string node.  */
4626 
4627 static tree
make_char_string_pack(tree value)4628 make_char_string_pack (tree value)
4629 {
4630   tree charvec;
4631   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4632   const unsigned char *str
4633     = (const unsigned char *) TREE_STRING_POINTER (value);
4634   int i, len = TREE_STRING_LENGTH (value) - 1;
4635   tree argvec = make_tree_vec (1);
4636 
4637   /* Fill in CHARVEC with all of the parameters.  */
4638   charvec = make_tree_vec (len);
4639   for (i = 0; i < len; ++i)
4640     {
4641       unsigned char s[3] = { '\'', str[i], '\'' };
4642       cpp_string in = { 3, s };
4643       cpp_string out = { 0, 0 };
4644       if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4645 	return NULL_TREE;
4646       gcc_assert (out.len == 2);
4647       TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4648 						 out.text[0]);
4649     }
4650 
4651   /* Build the argument packs.  */
4652   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4653 
4654   TREE_VEC_ELT (argvec, 0) = argpack;
4655 
4656   return argvec;
4657 }
4658 
4659 /* A subroutine of cp_parser_userdef_numeric_literal to
4660    create a char... template parameter pack from a string node.  */
4661 
4662 static tree
make_string_pack(tree value)4663 make_string_pack (tree value)
4664 {
4665   tree charvec;
4666   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4667   const unsigned char *str
4668     = (const unsigned char *) TREE_STRING_POINTER (value);
4669   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4670   int len = TREE_STRING_LENGTH (value) / sz - 1;
4671   tree argvec = make_tree_vec (2);
4672 
4673   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4674   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4675 
4676   /* First template parm is character type.  */
4677   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4678 
4679   /* Fill in CHARVEC with all of the parameters.  */
4680   charvec = make_tree_vec (len);
4681   for (int i = 0; i < len; ++i)
4682     TREE_VEC_ELT (charvec, i)
4683       = double_int_to_tree (str_char_type_node,
4684 			    double_int::from_buffer (str + i * sz, sz));
4685 
4686   /* Build the argument packs.  */
4687   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4688 
4689   TREE_VEC_ELT (argvec, 1) = argpack;
4690 
4691   return argvec;
4692 }
4693 
4694 /* Parse a user-defined numeric constant.  returns a call to a user-defined
4695    literal operator.  */
4696 
4697 static cp_expr
cp_parser_userdef_numeric_literal(cp_parser * parser)4698 cp_parser_userdef_numeric_literal (cp_parser *parser)
4699 {
4700   cp_token *token = cp_lexer_consume_token (parser->lexer);
4701   tree literal = token->u.value;
4702   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4703   tree value = USERDEF_LITERAL_VALUE (literal);
4704   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4705   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4706   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4707   tree decl, result;
4708 
4709   /* Look for a literal operator taking the exact type of numeric argument
4710      as the literal value.  */
4711   releasing_vec args;
4712   vec_safe_push (args, value);
4713   decl = lookup_literal_operator (name, args);
4714   if (decl && decl != error_mark_node)
4715     {
4716       result = finish_call_expr (decl, &args, false, true,
4717 				 tf_warning_or_error);
4718 
4719       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4720 	{
4721 	  warning_at (token->location, OPT_Woverflow,
4722 		      "integer literal exceeds range of %qT type",
4723 		      long_long_unsigned_type_node);
4724 	}
4725       else
4726 	{
4727 	  if (overflow > 0)
4728 	    warning_at (token->location, OPT_Woverflow,
4729 			"floating literal exceeds range of %qT type",
4730 			long_double_type_node);
4731 	  else if (overflow < 0)
4732 	    warning_at (token->location, OPT_Woverflow,
4733 			"floating literal truncated to zero");
4734 	}
4735 
4736       return result;
4737     }
4738 
4739   /* If the numeric argument didn't work, look for a raw literal
4740      operator taking a const char* argument consisting of the number
4741      in string format.  */
4742   args->truncate (0);
4743   vec_safe_push (args, num_string);
4744   decl = lookup_literal_operator (name, args);
4745   if (decl && decl != error_mark_node)
4746     {
4747       result = finish_call_expr (decl, &args, false, true,
4748 				 tf_warning_or_error);
4749       return result;
4750     }
4751 
4752   /* If the raw literal didn't work, look for a non-type template
4753      function with parameter pack char....  Call the function with
4754      template parameter characters representing the number.  */
4755   args->truncate (0);
4756   decl = lookup_literal_operator (name, args);
4757   if (decl && decl != error_mark_node)
4758     {
4759       tree tmpl_args = make_char_string_pack (num_string);
4760       if (tmpl_args == NULL_TREE)
4761 	{
4762 	  error ("failed to translate literal to execution character set %qT",
4763 		 num_string);
4764 	  return error_mark_node;
4765 	}
4766       decl = lookup_template_function (decl, tmpl_args);
4767       result = finish_call_expr (decl, &args, false, true,
4768 				 tf_warning_or_error);
4769       return result;
4770     }
4771 
4772   /* In C++14 the standard library defines complex number suffixes that
4773      conflict with GNU extensions.  Prefer them if <complex> is #included.  */
4774   bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4775   bool i14 = (cxx_dialect > cxx11
4776 	      && (id_equal (suffix_id, "i")
4777 		  || id_equal (suffix_id, "if")
4778 		  || id_equal (suffix_id, "il")));
4779   diagnostic_t kind = DK_ERROR;
4780   int opt = 0;
4781 
4782   if (i14 && ext)
4783     {
4784       tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4785 					  LOOK_want::NORMAL, false);
4786       if (cxlit == error_mark_node)
4787 	{
4788 	  /* No <complex>, so pedwarn and use GNU semantics.  */
4789 	  kind = DK_PEDWARN;
4790 	  opt = OPT_Wpedantic;
4791 	}
4792     }
4793 
4794   bool complained
4795     = emit_diagnostic (kind, input_location, opt,
4796 		       "unable to find numeric literal operator %qD", name);
4797 
4798   if (!complained)
4799     /* Don't inform either.  */;
4800   else if (i14)
4801     {
4802       inform (token->location, "add %<using namespace std::complex_literals%> "
4803 	      "(from %<<complex>%>) to enable the C++14 user-defined literal "
4804 	      "suffixes");
4805       if (ext)
4806 	inform (token->location, "or use %<j%> instead of %<i%> for the "
4807 		"GNU built-in suffix");
4808     }
4809   else if (!ext)
4810     inform (token->location, "use %<-fext-numeric-literals%> "
4811 	    "to enable more built-in suffixes");
4812 
4813   if (kind == DK_ERROR)
4814     value = error_mark_node;
4815   else
4816     {
4817       /* Use the built-in semantics.  */
4818       tree type;
4819       if (id_equal (suffix_id, "i"))
4820 	{
4821 	  if (TREE_CODE (value) == INTEGER_CST)
4822 	    type = integer_type_node;
4823 	  else
4824 	    type = double_type_node;
4825 	}
4826       else if (id_equal (suffix_id, "if"))
4827 	type = float_type_node;
4828       else /* if (id_equal (suffix_id, "il")) */
4829 	type = long_double_type_node;
4830 
4831       value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4832 			   build_zero_cst (type), fold_convert (type, value));
4833     }
4834 
4835   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4836     /* Avoid repeated diagnostics.  */
4837     token->u.value = value;
4838   return value;
4839 }
4840 
4841 /* Parse a user-defined string constant.  Returns a call to a user-defined
4842    literal operator taking a character pointer and the length of the string
4843    as arguments.  */
4844 
4845 static tree
cp_parser_userdef_string_literal(tree literal)4846 cp_parser_userdef_string_literal (tree literal)
4847 {
4848   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4849   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4850   tree value = USERDEF_LITERAL_VALUE (literal);
4851   int len = TREE_STRING_LENGTH (value)
4852 	/ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4853   tree decl;
4854 
4855   /* Build up a call to the user-defined operator.  */
4856   /* Lookup the name we got back from the id-expression.  */
4857   releasing_vec args;
4858   vec_safe_push (args, value);
4859   vec_safe_push (args, build_int_cst (size_type_node, len));
4860   decl = lookup_literal_operator (name, args);
4861 
4862   if (decl && decl != error_mark_node)
4863     return finish_call_expr (decl, &args, false, true,
4864 			     tf_warning_or_error);
4865 
4866   /* Look for a suitable template function, either (C++20) with a single
4867      parameter of class type, or (N3599) with typename parameter CharT and
4868      parameter pack CharT...  */
4869   args->truncate (0);
4870   decl = lookup_literal_operator (name, args);
4871   if (decl && decl != error_mark_node)
4872     {
4873       /* Use resolve_nondeduced_context to try to choose one form of template
4874 	 or the other.  */
4875       tree tmpl_args = make_tree_vec (1);
4876       TREE_VEC_ELT (tmpl_args, 0) = value;
4877       decl = lookup_template_function (decl, tmpl_args);
4878       tree res = resolve_nondeduced_context (decl, tf_none);
4879       if (DECL_P (res))
4880 	decl = res;
4881       else
4882 	{
4883 	  TREE_OPERAND (decl, 1) = make_string_pack (value);
4884 	  res = resolve_nondeduced_context (decl, tf_none);
4885 	  if (DECL_P (res))
4886 	    decl = res;
4887 	}
4888       if (!DECL_P (decl) && cxx_dialect > cxx17)
4889 	TREE_OPERAND (decl, 1) = tmpl_args;
4890       return finish_call_expr (decl, &args, false, true,
4891 			       tf_warning_or_error);
4892     }
4893 
4894   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4895 	 name, TREE_TYPE (value), size_type_node);
4896   return error_mark_node;
4897 }
4898 
4899 
4900 /* Basic concepts [gram.basic]  */
4901 
4902 /* Parse a translation-unit.
4903 
4904    translation-unit:
4905      declaration-seq [opt]  */
4906 
4907 static void
cp_parser_translation_unit(cp_parser * parser)4908 cp_parser_translation_unit (cp_parser* parser)
4909 {
4910   gcc_checking_assert (!cp_error_declarator);
4911 
4912   /* Create the declarator obstack.  */
4913   gcc_obstack_init (&declarator_obstack);
4914   /* Create the error declarator.  */
4915   cp_error_declarator = make_declarator (cdk_error);
4916   /* Create the empty parameter list.  */
4917   no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4918 					     UNKNOWN_LOCATION);
4919   /* Remember where the base of the declarator obstack lies.  */
4920   void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4921 
4922   push_deferring_access_checks (flag_access_control
4923 				? dk_no_deferred : dk_no_check);
4924 
4925   module_parse mp_state = MP_NOT_MODULE;
4926   if (modules_p () && !header_module_p ())
4927     mp_state = MP_FIRST;
4928 
4929   bool implicit_extern_c = false;
4930 
4931   /* Parse until EOF.  */
4932   for (;;)
4933     {
4934       cp_token *token = cp_lexer_peek_token (parser->lexer);
4935 
4936       /* If we're entering or exiting a region that's implicitly
4937 	 extern "C", modify the lang context appropriately.  This is
4938 	 so horrible.  Please die.   */
4939       if (implicit_extern_c
4940 	  != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4941 	{
4942 	  implicit_extern_c = !implicit_extern_c;
4943 	  if (implicit_extern_c)
4944 	    push_lang_context (lang_name_c);
4945 	  else
4946 	    pop_lang_context ();
4947 	}
4948 
4949       if (token->type == CPP_EOF)
4950 	break;
4951 
4952       if (modules_p ())
4953 	{
4954 	  /* Top-level module declarations are ok, and change the
4955 	     portion of file we're in.  Top-level import declarations
4956 	     are significant for the import portions.  */
4957 
4958 	  cp_token *next = token;
4959 	  bool exporting = token->keyword == RID__EXPORT;
4960 	  if (exporting)
4961 	    {
4962 	      cp_lexer_consume_token (parser->lexer);
4963 	      next = cp_lexer_peek_token (parser->lexer);
4964 	    }
4965 	  if (next->keyword == RID__MODULE)
4966 	    {
4967 	      mp_state
4968 		= cp_parser_module_declaration (parser, mp_state, exporting);
4969 	      continue;
4970 	    }
4971 	  else if (next->keyword == RID__IMPORT)
4972 	    {
4973 	      if (mp_state == MP_FIRST)
4974 		mp_state = MP_NOT_MODULE;
4975 	      cp_parser_import_declaration (parser, mp_state, exporting);
4976 	      continue;
4977 	    }
4978 	  else
4979 	    gcc_checking_assert (!exporting);
4980 
4981 	  if (mp_state == MP_GLOBAL && token->main_source_p)
4982 	    {
4983 	      static bool warned = false;
4984 	      if (!warned)
4985 		{
4986 		  warned = true;
4987 		  error_at (token->location,
4988 			    "global module fragment contents must be"
4989 			    " from preprocessor inclusion");
4990 		}
4991 	    }
4992 	}
4993 
4994       /* This relies on the ordering of module_parse values.  */
4995       if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
4996 	/* We're no longer in the import portion of a named module.  */
4997 	mp_state = module_parse (mp_state + 1);
4998       else if (mp_state == MP_FIRST)
4999 	mp_state = MP_NOT_MODULE;
5000 
5001       if (token->type == CPP_CLOSE_BRACE)
5002 	{
5003 	  cp_parser_error (parser, "expected declaration");
5004 	  cp_lexer_consume_token (parser->lexer);
5005 	  /* If the next token is now a `;', consume it.  */
5006 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5007 	    cp_lexer_consume_token (parser->lexer);
5008 	}
5009       else
5010 	cp_parser_toplevel_declaration (parser);
5011     }
5012 
5013   /* Get rid of the token array; we don't need it any more.  */
5014   cp_lexer_destroy (parser->lexer);
5015   parser->lexer = NULL;
5016 
5017   /* The EOF should have reset this. */
5018   gcc_checking_assert (!implicit_extern_c);
5019 
5020   /* Make sure the declarator obstack was fully cleaned up.  */
5021   gcc_assert (obstack_next_free (&declarator_obstack)
5022 	      == declarator_obstack_base);
5023 }
5024 
5025 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5026    decltype context.  */
5027 
5028 static inline tsubst_flags_t
complain_flags(bool decltype_p)5029 complain_flags (bool decltype_p)
5030 {
5031   tsubst_flags_t complain = tf_warning_or_error;
5032   if (decltype_p)
5033     complain |= tf_decltype;
5034   return complain;
5035 }
5036 
5037 /* We're about to parse a collection of statements.  If we're currently
5038    parsing tentatively, set up a firewall so that any nested
5039    cp_parser_commit_to_tentative_parse won't affect the current context.  */
5040 
5041 static cp_token_position
cp_parser_start_tentative_firewall(cp_parser * parser)5042 cp_parser_start_tentative_firewall (cp_parser *parser)
5043 {
5044   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5045     return 0;
5046 
5047   cp_parser_parse_tentatively (parser);
5048   cp_parser_commit_to_topmost_tentative_parse (parser);
5049   return cp_lexer_token_position (parser->lexer, false);
5050 }
5051 
5052 /* We've finished parsing the collection of statements.  Wrap up the
5053    firewall and replace the relevant tokens with the parsed form.  */
5054 
5055 static void
cp_parser_end_tentative_firewall(cp_parser * parser,cp_token_position start,tree expr)5056 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5057 				  tree expr)
5058 {
5059   if (!start)
5060     return;
5061 
5062   /* Finish the firewall level.  */
5063   cp_parser_parse_definitely (parser);
5064   /* And remember the result of the parse for when we try again.  */
5065   cp_token *token = cp_lexer_token_at (parser->lexer, start);
5066   token->type = CPP_PREPARSED_EXPR;
5067   token->u.value = expr;
5068   token->keyword = RID_MAX;
5069   cp_lexer_purge_tokens_after (parser->lexer, start);
5070 }
5071 
5072 /* Like the above functions, but let the user modify the tokens.  Used by
5073    CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5074    later parses, so it makes sense to localize the effects of
5075    cp_parser_commit_to_tentative_parse.  */
5076 
5077 struct tentative_firewall
5078 {
5079   cp_parser *parser;
5080   bool set;
5081 
tentative_firewalltentative_firewall5082   tentative_firewall (cp_parser *p): parser(p)
5083   {
5084     /* If we're currently parsing tentatively, start a committed level as a
5085        firewall and then an inner tentative parse.  */
5086     if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5087       {
5088 	cp_parser_parse_tentatively (parser);
5089 	cp_parser_commit_to_topmost_tentative_parse (parser);
5090 	cp_parser_parse_tentatively (parser);
5091       }
5092   }
5093 
~tentative_firewalltentative_firewall5094   ~tentative_firewall()
5095   {
5096     if (set)
5097       {
5098 	/* Finish the inner tentative parse and the firewall, propagating any
5099 	   uncommitted error state to the outer tentative parse.  */
5100 	bool err = cp_parser_error_occurred (parser);
5101 	cp_parser_parse_definitely (parser);
5102 	cp_parser_parse_definitely (parser);
5103 	if (err)
5104 	  cp_parser_simulate_error (parser);
5105       }
5106   }
5107 };
5108 
5109 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5110    This class is for tracking such a matching pair of symbols.
5111    In particular, it tracks the location of the first token,
5112    so that if the second token is missing, we can highlight the
5113    location of the first token when notifying the user about the
5114    problem.  */
5115 
5116 template <typename traits_t>
5117 class token_pair
5118 {
5119  public:
5120   /* token_pair's ctor.  */
token_pair()5121   token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5122 
5123   /* If the next token is the opening symbol for this pair, consume it and
5124      return true.
5125      Otherwise, issue an error and return false.
5126      In either case, record the location of the opening token.  */
5127 
require_open(cp_parser * parser)5128   bool require_open (cp_parser *parser)
5129   {
5130     m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5131     return cp_parser_require (parser, traits_t::open_token_type,
5132 			      traits_t::required_token_open);
5133   }
5134 
5135   /* Consume the next token from PARSER, recording its location as
5136      that of the opening token within the pair.  */
5137 
consume_open(cp_parser * parser)5138   cp_token * consume_open (cp_parser *parser)
5139   {
5140     cp_token *tok = cp_lexer_consume_token (parser->lexer);
5141     gcc_assert (tok->type == traits_t::open_token_type);
5142     m_open_loc = tok->location;
5143     return tok;
5144   }
5145 
5146   /* If the next token is the closing symbol for this pair, consume it
5147      and return it.
5148      Otherwise, issue an error, highlighting the location of the
5149      corresponding opening token, and return NULL.  */
5150 
require_close(cp_parser * parser) const5151   cp_token *require_close (cp_parser *parser) const
5152   {
5153     return cp_parser_require (parser, traits_t::close_token_type,
5154 			      traits_t::required_token_close,
5155 			      m_open_loc);
5156   }
5157 
open_location() const5158   location_t open_location () const { return m_open_loc; }
5159 
5160  private:
5161   location_t m_open_loc;
5162 };
5163 
5164 /* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
5165 
5166 struct matching_paren_traits
5167 {
5168   static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5169   static const enum required_token required_token_open  = RT_OPEN_PAREN;
5170   static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5171   static const enum required_token required_token_close = RT_CLOSE_PAREN;
5172 };
5173 
5174 /* "matching_parens" is a token_pair<T> class for tracking matching
5175    pairs of parentheses.  */
5176 
5177 typedef token_pair<matching_paren_traits> matching_parens;
5178 
5179 /* Traits for token_pair<T> for tracking matching pairs of braces.  */
5180 
5181 struct matching_brace_traits
5182 {
5183   static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5184   static const enum required_token required_token_open = RT_OPEN_BRACE;
5185   static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5186   static const enum required_token required_token_close = RT_CLOSE_BRACE;
5187 };
5188 
5189 /* "matching_braces" is a token_pair<T> class for tracking matching
5190    pairs of braces.  */
5191 
5192 typedef token_pair<matching_brace_traits> matching_braces;
5193 
5194 
5195 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5196    enclosing parentheses.  */
5197 
5198 static cp_expr
cp_parser_statement_expr(cp_parser * parser)5199 cp_parser_statement_expr (cp_parser *parser)
5200 {
5201   cp_token_position start = cp_parser_start_tentative_firewall (parser);
5202 
5203   /* Consume the '('.  */
5204   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5205   matching_parens parens;
5206   parens.consume_open (parser);
5207   /* Start the statement-expression.  */
5208   tree expr = begin_stmt_expr ();
5209   /* Parse the compound-statement.  */
5210   cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
5211   /* Finish up.  */
5212   expr = finish_stmt_expr (expr, false);
5213   /* Consume the ')'.  */
5214   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5215   if (!parens.require_close (parser))
5216     cp_parser_skip_to_end_of_statement (parser);
5217 
5218   cp_parser_end_tentative_firewall (parser, start, expr);
5219   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5220   return cp_expr (expr, combined_loc);
5221 }
5222 
5223 /* Expressions [gram.expr] */
5224 
5225 /* Parse a fold-operator.
5226 
5227     fold-operator:
5228         -  *  /  %  ^  &  |  =  <  >  <<  >>
5229       =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
5230       ==  !=  <=  >=  &&  ||  ,  .*  ->*
5231 
5232    This returns the tree code corresponding to the matched operator
5233    as an int. When the current token matches a compound assignment
5234    operator, the resulting tree code is the negative value of the
5235    non-assignment operator. */
5236 
5237 static int
cp_parser_fold_operator(cp_token * token)5238 cp_parser_fold_operator (cp_token *token)
5239 {
5240   switch (token->type)
5241     {
5242     case CPP_PLUS: return PLUS_EXPR;
5243     case CPP_MINUS: return MINUS_EXPR;
5244     case CPP_MULT: return MULT_EXPR;
5245     case CPP_DIV: return TRUNC_DIV_EXPR;
5246     case CPP_MOD: return TRUNC_MOD_EXPR;
5247     case CPP_XOR: return BIT_XOR_EXPR;
5248     case CPP_AND: return BIT_AND_EXPR;
5249     case CPP_OR: return BIT_IOR_EXPR;
5250     case CPP_LSHIFT: return LSHIFT_EXPR;
5251     case CPP_RSHIFT: return RSHIFT_EXPR;
5252 
5253     case CPP_EQ: return -NOP_EXPR;
5254     case CPP_PLUS_EQ: return -PLUS_EXPR;
5255     case CPP_MINUS_EQ: return -MINUS_EXPR;
5256     case CPP_MULT_EQ: return -MULT_EXPR;
5257     case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5258     case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5259     case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5260     case CPP_AND_EQ: return -BIT_AND_EXPR;
5261     case CPP_OR_EQ: return -BIT_IOR_EXPR;
5262     case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5263     case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5264 
5265     case CPP_EQ_EQ: return EQ_EXPR;
5266     case CPP_NOT_EQ: return NE_EXPR;
5267     case CPP_LESS: return LT_EXPR;
5268     case CPP_GREATER: return GT_EXPR;
5269     case CPP_LESS_EQ: return LE_EXPR;
5270     case CPP_GREATER_EQ: return GE_EXPR;
5271 
5272     case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5273     case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5274 
5275     case CPP_COMMA: return COMPOUND_EXPR;
5276 
5277     case CPP_DOT_STAR: return DOTSTAR_EXPR;
5278     case CPP_DEREF_STAR: return MEMBER_REF;
5279 
5280     default: return ERROR_MARK;
5281     }
5282 }
5283 
5284 /* Returns true if CODE indicates a binary expression, which is not allowed in
5285    the LHS of a fold-expression.  More codes will need to be added to use this
5286    function in other contexts.  */
5287 
5288 static bool
is_binary_op(tree_code code)5289 is_binary_op (tree_code code)
5290 {
5291   switch (code)
5292     {
5293     case PLUS_EXPR:
5294     case POINTER_PLUS_EXPR:
5295     case MINUS_EXPR:
5296     case MULT_EXPR:
5297     case TRUNC_DIV_EXPR:
5298     case TRUNC_MOD_EXPR:
5299     case BIT_XOR_EXPR:
5300     case BIT_AND_EXPR:
5301     case BIT_IOR_EXPR:
5302     case LSHIFT_EXPR:
5303     case RSHIFT_EXPR:
5304 
5305     case MODOP_EXPR:
5306 
5307     case EQ_EXPR:
5308     case NE_EXPR:
5309     case LE_EXPR:
5310     case GE_EXPR:
5311     case LT_EXPR:
5312     case GT_EXPR:
5313 
5314     case TRUTH_ANDIF_EXPR:
5315     case TRUTH_ORIF_EXPR:
5316 
5317     case COMPOUND_EXPR:
5318 
5319     case DOTSTAR_EXPR:
5320     case MEMBER_REF:
5321       return true;
5322 
5323     default:
5324       return false;
5325     }
5326 }
5327 
5328 /* If the next token is a suitable fold operator, consume it and return as
5329    the function above.  */
5330 
5331 static int
cp_parser_fold_operator(cp_parser * parser)5332 cp_parser_fold_operator (cp_parser *parser)
5333 {
5334   cp_token* token = cp_lexer_peek_token (parser->lexer);
5335   int code = cp_parser_fold_operator (token);
5336   if (code != ERROR_MARK)
5337     cp_lexer_consume_token (parser->lexer);
5338   return code;
5339 }
5340 
5341 /* Parse a fold-expression.
5342 
5343      fold-expression:
5344        ( ... folding-operator cast-expression)
5345        ( cast-expression folding-operator ... )
5346        ( cast-expression folding operator ... folding-operator cast-expression)
5347 
5348    Note that the '(' and ')' are matched in primary expression. */
5349 
5350 static cp_expr
cp_parser_fold_expression(cp_parser * parser,tree expr1)5351 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5352 {
5353   cp_id_kind pidk;
5354 
5355   // Left fold.
5356   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5357     {
5358       if (expr1)
5359 	return error_mark_node;
5360       cp_lexer_consume_token (parser->lexer);
5361       int op = cp_parser_fold_operator (parser);
5362       if (op == ERROR_MARK)
5363         {
5364           cp_parser_error (parser, "expected binary operator");
5365           return error_mark_node;
5366         }
5367 
5368       tree expr = cp_parser_cast_expression (parser, false, false,
5369 					     false, &pidk);
5370       if (expr == error_mark_node)
5371         return error_mark_node;
5372       return finish_left_unary_fold_expr (expr, op);
5373     }
5374 
5375   const cp_token* token = cp_lexer_peek_token (parser->lexer);
5376   int op = cp_parser_fold_operator (parser);
5377   if (op == ERROR_MARK)
5378     {
5379       cp_parser_error (parser, "expected binary operator");
5380       return error_mark_node;
5381     }
5382 
5383   if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5384     {
5385       cp_parser_error (parser, "expected ...");
5386       return error_mark_node;
5387     }
5388   cp_lexer_consume_token (parser->lexer);
5389 
5390   /* The operands of a fold-expression are cast-expressions, so binary or
5391      conditional expressions are not allowed.  We check this here to avoid
5392      tentative parsing.  */
5393   if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5394     /* OK, the expression was parenthesized.  */;
5395   else if (is_binary_op (TREE_CODE (expr1)))
5396     error_at (location_of (expr1),
5397 	      "binary expression in operand of fold-expression");
5398   else if (TREE_CODE (expr1) == COND_EXPR
5399 	   || (REFERENCE_REF_P (expr1)
5400 	       && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5401     error_at (location_of (expr1),
5402 	      "conditional expression in operand of fold-expression");
5403 
5404   // Right fold.
5405   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5406     return finish_right_unary_fold_expr (expr1, op);
5407 
5408   if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5409     {
5410       cp_parser_error (parser, "mismatched operator in fold-expression");
5411       return error_mark_node;
5412     }
5413   cp_lexer_consume_token (parser->lexer);
5414 
5415   // Binary left or right fold.
5416   tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5417   if (expr2 == error_mark_node)
5418     return error_mark_node;
5419   return finish_binary_fold_expr (expr1, expr2, op);
5420 }
5421 
5422 /* Parse a primary-expression.
5423 
5424    primary-expression:
5425      literal
5426      this
5427      ( expression )
5428      id-expression
5429      lambda-expression (C++11)
5430 
5431    GNU Extensions:
5432 
5433    primary-expression:
5434      ( compound-statement )
5435      __builtin_va_arg ( assignment-expression , type-id )
5436      __builtin_offsetof ( type-id , offsetof-expression )
5437 
5438    C++ Extensions:
5439      __has_nothrow_assign ( type-id )
5440      __has_nothrow_constructor ( type-id )
5441      __has_nothrow_copy ( type-id )
5442      __has_trivial_assign ( type-id )
5443      __has_trivial_constructor ( type-id )
5444      __has_trivial_copy ( type-id )
5445      __has_trivial_destructor ( type-id )
5446      __has_virtual_destructor ( type-id )
5447      __is_abstract ( type-id )
5448      __is_base_of ( type-id , type-id )
5449      __is_class ( type-id )
5450      __is_empty ( type-id )
5451      __is_enum ( type-id )
5452      __is_final ( type-id )
5453      __is_literal_type ( type-id )
5454      __is_pod ( type-id )
5455      __is_polymorphic ( type-id )
5456      __is_std_layout ( type-id )
5457      __is_trivial ( type-id )
5458      __is_union ( type-id )
5459 
5460    Objective-C++ Extension:
5461 
5462    primary-expression:
5463      objc-expression
5464 
5465    literal:
5466      __null
5467 
5468    ADDRESS_P is true iff this expression was immediately preceded by
5469    "&" and therefore might denote a pointer-to-member.  CAST_P is true
5470    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
5471    true iff this expression is a template argument.
5472 
5473    Returns a representation of the expression.  Upon return, *IDK
5474    indicates what kind of id-expression (if any) was present.  */
5475 
5476 static cp_expr
cp_parser_primary_expression(cp_parser * parser,bool address_p,bool cast_p,bool template_arg_p,bool decltype_p,cp_id_kind * idk)5477 cp_parser_primary_expression (cp_parser *parser,
5478 			      bool address_p,
5479 			      bool cast_p,
5480 			      bool template_arg_p,
5481 			      bool decltype_p,
5482 			      cp_id_kind *idk)
5483 {
5484   cp_token *token = NULL;
5485 
5486   /* Assume the primary expression is not an id-expression.  */
5487   *idk = CP_ID_KIND_NONE;
5488 
5489   /* Peek at the next token.  */
5490   token = cp_lexer_peek_token (parser->lexer);
5491   switch ((int) token->type)
5492     {
5493       /* literal:
5494 	   integer-literal
5495 	   character-literal
5496 	   floating-literal
5497 	   string-literal
5498 	   boolean-literal
5499 	   pointer-literal
5500 	   user-defined-literal  */
5501     case CPP_CHAR:
5502     case CPP_CHAR16:
5503     case CPP_CHAR32:
5504     case CPP_WCHAR:
5505     case CPP_UTF8CHAR:
5506     case CPP_NUMBER:
5507     case CPP_PREPARSED_EXPR:
5508       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5509 	return cp_parser_userdef_numeric_literal (parser);
5510       token = cp_lexer_consume_token (parser->lexer);
5511       if (TREE_CODE (token->u.value) == FIXED_CST)
5512 	{
5513 	  error_at (token->location,
5514 		    "fixed-point types not supported in C++");
5515 	  return error_mark_node;
5516 	}
5517       /* Floating-point literals are only allowed in an integral
5518 	 constant expression if they are cast to an integral or
5519 	 enumeration type.  */
5520       if (TREE_CODE (token->u.value) == REAL_CST
5521 	  && parser->integral_constant_expression_p
5522 	  && pedantic)
5523 	{
5524 	  /* CAST_P will be set even in invalid code like "int(2.7 +
5525 	     ...)".   Therefore, we have to check that the next token
5526 	     is sure to end the cast.  */
5527 	  if (cast_p)
5528 	    {
5529 	      cp_token *next_token;
5530 
5531 	      next_token = cp_lexer_peek_token (parser->lexer);
5532 	      if (/* The comma at the end of an
5533 		     enumerator-definition.  */
5534 		  next_token->type != CPP_COMMA
5535 		  /* The curly brace at the end of an enum-specifier.  */
5536 		  && next_token->type != CPP_CLOSE_BRACE
5537 		  /* The end of a statement.  */
5538 		  && next_token->type != CPP_SEMICOLON
5539 		  /* The end of the cast-expression.  */
5540 		  && next_token->type != CPP_CLOSE_PAREN
5541 		  /* The end of an array bound.  */
5542 		  && next_token->type != CPP_CLOSE_SQUARE
5543 		  /* The closing ">" in a template-argument-list.  */
5544 		  && (next_token->type != CPP_GREATER
5545 		      || parser->greater_than_is_operator_p)
5546 		  /* C++0x only: A ">>" treated like two ">" tokens,
5547                      in a template-argument-list.  */
5548 		  && (next_token->type != CPP_RSHIFT
5549                       || (cxx_dialect == cxx98)
5550 		      || parser->greater_than_is_operator_p))
5551 		cast_p = false;
5552 	    }
5553 
5554 	  /* If we are within a cast, then the constraint that the
5555 	     cast is to an integral or enumeration type will be
5556 	     checked at that point.  If we are not within a cast, then
5557 	     this code is invalid.  */
5558 	  if (!cast_p)
5559 	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5560 	}
5561       return (cp_expr (token->u.value, token->location)
5562 	      .maybe_add_location_wrapper ());
5563 
5564     case CPP_CHAR_USERDEF:
5565     case CPP_CHAR16_USERDEF:
5566     case CPP_CHAR32_USERDEF:
5567     case CPP_WCHAR_USERDEF:
5568     case CPP_UTF8CHAR_USERDEF:
5569       return cp_parser_userdef_char_literal (parser);
5570 
5571     case CPP_STRING:
5572     case CPP_STRING16:
5573     case CPP_STRING32:
5574     case CPP_WSTRING:
5575     case CPP_UTF8STRING:
5576     case CPP_STRING_USERDEF:
5577     case CPP_STRING16_USERDEF:
5578     case CPP_STRING32_USERDEF:
5579     case CPP_WSTRING_USERDEF:
5580     case CPP_UTF8STRING_USERDEF:
5581       /* ??? Should wide strings be allowed when parser->translate_strings_p
5582 	 is false (i.e. in attributes)?  If not, we can kill the third
5583 	 argument to cp_parser_string_literal.  */
5584       return (cp_parser_string_literal (parser,
5585 					parser->translate_strings_p,
5586 					true)
5587 	      .maybe_add_location_wrapper ());
5588 
5589     case CPP_OPEN_PAREN:
5590       /* If we see `( { ' then we are looking at the beginning of
5591 	 a GNU statement-expression.  */
5592       if (cp_parser_allow_gnu_extensions_p (parser)
5593 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5594 	{
5595 	  /* Statement-expressions are not allowed by the standard.  */
5596 	  pedwarn (token->location, OPT_Wpedantic,
5597 		   "ISO C++ forbids braced-groups within expressions");
5598 
5599 	  /* And they're not allowed outside of a function-body; you
5600 	     cannot, for example, write:
5601 
5602 	     int i = ({ int j = 3; j + 1; });
5603 
5604 	     at class or namespace scope.  */
5605 	  if (!parser->in_function_body
5606 	      || parser->in_template_argument_list_p)
5607 	    {
5608 	      error_at (token->location,
5609 			"statement-expressions are not allowed outside "
5610 			"functions nor in template-argument lists");
5611 	      cp_parser_skip_to_end_of_block_or_statement (parser);
5612 	      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5613 		cp_lexer_consume_token (parser->lexer);
5614 	      return error_mark_node;
5615 	    }
5616 	  else
5617 	    return cp_parser_statement_expr (parser);
5618 	}
5619       /* Otherwise it's a normal parenthesized expression.  */
5620       {
5621 	cp_expr expr;
5622 	bool saved_greater_than_is_operator_p;
5623 
5624 	location_t open_paren_loc = token->location;
5625 
5626 	/* Consume the `('.  */
5627 	matching_parens parens;
5628 	parens.consume_open (parser);
5629 	/* Within a parenthesized expression, a `>' token is always
5630 	   the greater-than operator.  */
5631 	saved_greater_than_is_operator_p
5632 	  = parser->greater_than_is_operator_p;
5633 	parser->greater_than_is_operator_p = true;
5634 
5635 	if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5636 	  /* Left fold expression. */
5637 	  expr = NULL_TREE;
5638 	else
5639 	  /* Parse the parenthesized expression.  */
5640 	  expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5641 
5642 	token = cp_lexer_peek_token (parser->lexer);
5643 	if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5644 	  {
5645 	    expr = cp_parser_fold_expression (parser, expr);
5646 	    if (expr != error_mark_node
5647 		&& cxx_dialect < cxx17)
5648 	      pedwarn (input_location, OPT_Wc__17_extensions,
5649 		       "fold-expressions only available with %<-std=c++17%> "
5650 		       "or %<-std=gnu++17%>");
5651 	  }
5652 	else
5653 	  /* Let the front end know that this expression was
5654 	     enclosed in parentheses. This matters in case, for
5655 	     example, the expression is of the form `A::B', since
5656 	     `&A::B' might be a pointer-to-member, but `&(A::B)' is
5657 	     not.  */
5658 	  expr = finish_parenthesized_expr (expr);
5659 
5660 	/* DR 705: Wrapping an unqualified name in parentheses
5661 	   suppresses arg-dependent lookup.  We want to pass back
5662 	   CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5663 	   (c++/37862), but none of the others.  */
5664 	if (*idk != CP_ID_KIND_QUALIFIED)
5665 	  *idk = CP_ID_KIND_NONE;
5666 
5667 	/* The `>' token might be the end of a template-id or
5668 	   template-parameter-list now.  */
5669 	parser->greater_than_is_operator_p
5670 	  = saved_greater_than_is_operator_p;
5671 
5672 	/* Consume the `)'.  */
5673 	token = cp_lexer_peek_token (parser->lexer);
5674 	location_t close_paren_loc = token->location;
5675 	bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5676 	expr.set_range (open_paren_loc, close_paren_loc);
5677 	if (no_wparens)
5678 	  suppress_warning (expr, OPT_Wparentheses);
5679 	if (!parens.require_close (parser)
5680 	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5681 	  cp_parser_skip_to_end_of_statement (parser);
5682 
5683 	return expr;
5684       }
5685 
5686     case CPP_OPEN_SQUARE:
5687       {
5688 	if (c_dialect_objc ())
5689 	  {
5690 	    /* We might have an Objective-C++ message. */
5691 	    cp_parser_parse_tentatively (parser);
5692 	    tree msg = cp_parser_objc_message_expression (parser);
5693 	    /* If that works out, we're done ... */
5694 	    if (cp_parser_parse_definitely (parser))
5695 	      return msg;
5696 	    /* ... else, fall though to see if it's a lambda.  */
5697 	  }
5698 	cp_expr lam = cp_parser_lambda_expression (parser);
5699 	/* Don't warn about a failed tentative parse.  */
5700 	if (cp_parser_error_occurred (parser))
5701 	  return error_mark_node;
5702 	maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5703 	return lam;
5704       }
5705 
5706     case CPP_OBJC_STRING:
5707       if (c_dialect_objc ())
5708 	/* We have an Objective-C++ string literal. */
5709         return cp_parser_objc_expression (parser);
5710       cp_parser_error (parser, "expected primary-expression");
5711       return error_mark_node;
5712 
5713     case CPP_KEYWORD:
5714       switch (token->keyword)
5715 	{
5716 	  /* These two are the boolean literals.  */
5717 	case RID_TRUE:
5718 	  cp_lexer_consume_token (parser->lexer);
5719 	  return cp_expr (boolean_true_node, token->location);
5720 	case RID_FALSE:
5721 	  cp_lexer_consume_token (parser->lexer);
5722 	  return cp_expr (boolean_false_node, token->location);
5723 
5724 	  /* The `__null' literal.  */
5725 	case RID_NULL:
5726 	  cp_lexer_consume_token (parser->lexer);
5727 	  return cp_expr (null_node, token->location);
5728 
5729 	  /* The `nullptr' literal.  */
5730 	case RID_NULLPTR:
5731 	  cp_lexer_consume_token (parser->lexer);
5732 	  return cp_expr (nullptr_node, token->location);
5733 
5734 	  /* Recognize the `this' keyword.  */
5735 	case RID_THIS:
5736 	  cp_lexer_consume_token (parser->lexer);
5737 	  if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5738 	    {
5739 	      error_at (token->location,
5740 			"%<this%> may not be used in this context");
5741 	      return error_mark_node;
5742 	    }
5743 	  /* Pointers cannot appear in constant-expressions.  */
5744 	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5745 	    return error_mark_node;
5746 	  return cp_expr (finish_this_expr (), token->location);
5747 
5748 	  /* The `operator' keyword can be the beginning of an
5749 	     id-expression.  */
5750 	case RID_OPERATOR:
5751 	  goto id_expression;
5752 
5753 	case RID_FUNCTION_NAME:
5754 	case RID_PRETTY_FUNCTION_NAME:
5755 	case RID_C99_FUNCTION_NAME:
5756 	  {
5757 	    non_integral_constant name;
5758 
5759 	    /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5760 	       __func__ are the names of variables -- but they are
5761 	       treated specially.  Therefore, they are handled here,
5762 	       rather than relying on the generic id-expression logic
5763 	       below.  Grammatically, these names are id-expressions.
5764 
5765 	       Consume the token.  */
5766 	    token = cp_lexer_consume_token (parser->lexer);
5767 
5768 	    switch (token->keyword)
5769 	      {
5770 	      case RID_FUNCTION_NAME:
5771 		name = NIC_FUNC_NAME;
5772 		break;
5773 	      case RID_PRETTY_FUNCTION_NAME:
5774 		name = NIC_PRETTY_FUNC;
5775 		break;
5776 	      case RID_C99_FUNCTION_NAME:
5777 		name = NIC_C99_FUNC;
5778 		break;
5779 	      default:
5780 		gcc_unreachable ();
5781 	      }
5782 
5783 	    if (cp_parser_non_integral_constant_expression (parser, name))
5784 	      return error_mark_node;
5785 
5786 	    /* Look up the name.  */
5787 	    return finish_fname (token->u.value);
5788 	  }
5789 
5790 	case RID_VA_ARG:
5791 	  {
5792 	    tree expression;
5793 	    tree type;
5794 	    location_t type_location;
5795 	    location_t start_loc
5796 	      = cp_lexer_peek_token (parser->lexer)->location;
5797 	    /* The `__builtin_va_arg' construct is used to handle
5798 	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
5799 	    cp_lexer_consume_token (parser->lexer);
5800 	    /* Look for the opening `('.  */
5801 	    matching_parens parens;
5802 	    parens.require_open (parser);
5803 	    /* Now, parse the assignment-expression.  */
5804 	    expression = cp_parser_assignment_expression (parser);
5805 	    /* Look for the `,'.  */
5806 	    cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5807 	    type_location = cp_lexer_peek_token (parser->lexer)->location;
5808 	    /* Parse the type-id.  */
5809 	    {
5810 	      type_id_in_expr_sentinel s (parser);
5811 	      type = cp_parser_type_id (parser);
5812 	    }
5813 	    /* Look for the closing `)'.  */
5814 	    location_t finish_loc
5815 	      = cp_lexer_peek_token (parser->lexer)->location;
5816 	    parens.require_close (parser);
5817 	    /* Using `va_arg' in a constant-expression is not
5818 	       allowed.  */
5819 	    if (cp_parser_non_integral_constant_expression (parser,
5820 							    NIC_VA_ARG))
5821 	      return error_mark_node;
5822 	    /* Construct a location of the form:
5823 		 __builtin_va_arg (v, int)
5824 		 ~~~~~~~~~~~~~~~~~~~~~^~~~
5825 	       with the caret at the type, ranging from the start of the
5826 	       "__builtin_va_arg" token to the close paren.  */
5827 	    location_t combined_loc
5828 	      = make_location (type_location, start_loc, finish_loc);
5829 	    return build_x_va_arg (combined_loc, expression, type);
5830 	  }
5831 
5832 	case RID_OFFSETOF:
5833 	  return cp_parser_builtin_offsetof (parser);
5834 
5835 	case RID_HAS_NOTHROW_ASSIGN:
5836 	case RID_HAS_NOTHROW_CONSTRUCTOR:
5837 	case RID_HAS_NOTHROW_COPY:
5838 	case RID_HAS_TRIVIAL_ASSIGN:
5839 	case RID_HAS_TRIVIAL_CONSTRUCTOR:
5840 	case RID_HAS_TRIVIAL_COPY:
5841 	case RID_HAS_TRIVIAL_DESTRUCTOR:
5842 	case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5843 	case RID_HAS_VIRTUAL_DESTRUCTOR:
5844 	case RID_IS_ABSTRACT:
5845 	case RID_IS_AGGREGATE:
5846 	case RID_IS_BASE_OF:
5847 	case RID_IS_CLASS:
5848 	case RID_IS_EMPTY:
5849 	case RID_IS_ENUM:
5850 	case RID_IS_FINAL:
5851 	case RID_IS_LAYOUT_COMPATIBLE:
5852 	case RID_IS_LITERAL_TYPE:
5853 	case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
5854 	case RID_IS_POD:
5855 	case RID_IS_POLYMORPHIC:
5856 	case RID_IS_SAME_AS:
5857 	case RID_IS_STD_LAYOUT:
5858 	case RID_IS_TRIVIAL:
5859 	case RID_IS_TRIVIALLY_ASSIGNABLE:
5860 	case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5861 	case RID_IS_TRIVIALLY_COPYABLE:
5862 	case RID_IS_UNION:
5863 	case RID_IS_ASSIGNABLE:
5864 	case RID_IS_CONSTRUCTIBLE:
5865 	case RID_IS_NOTHROW_ASSIGNABLE:
5866 	case RID_IS_NOTHROW_CONSTRUCTIBLE:
5867 	  return cp_parser_trait_expr (parser, token->keyword);
5868 
5869 	// C++ concepts
5870 	case RID_REQUIRES:
5871 	  return cp_parser_requires_expression (parser);
5872 
5873 	/* Objective-C++ expressions.  */
5874 	case RID_AT_ENCODE:
5875 	case RID_AT_PROTOCOL:
5876 	case RID_AT_SELECTOR:
5877 	  return cp_parser_objc_expression (parser);
5878 
5879 	case RID_TEMPLATE:
5880 	  if (parser->in_function_body
5881 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5882 	      	  == CPP_LESS))
5883 	    {
5884 	      error_at (token->location,
5885 			"a template declaration cannot appear at block scope");
5886 	      cp_parser_skip_to_end_of_block_or_statement (parser);
5887 	      return error_mark_node;
5888 	    }
5889 	  /* FALLTHRU */
5890 	default:
5891 	  cp_parser_error (parser, "expected primary-expression");
5892 	  return error_mark_node;
5893 	}
5894 
5895       /* An id-expression can start with either an identifier, a
5896 	 `::' as the beginning of a qualified-id, or the "operator"
5897 	 keyword.  */
5898     case CPP_NAME:
5899     case CPP_SCOPE:
5900     case CPP_TEMPLATE_ID:
5901     case CPP_NESTED_NAME_SPECIFIER:
5902       {
5903       id_expression:
5904 	cp_expr id_expression;
5905 	cp_expr decl;
5906 	const char *error_msg;
5907 	bool template_p;
5908 	bool done;
5909 	cp_token *id_expr_token;
5910 
5911 	/* Parse the id-expression.  */
5912 	id_expression
5913 	  = cp_parser_id_expression (parser,
5914 				     /*template_keyword_p=*/false,
5915 				     /*check_dependency_p=*/true,
5916 				     &template_p,
5917 				     /*declarator_p=*/false,
5918 				     /*optional_p=*/false);
5919 	if (id_expression == error_mark_node)
5920 	  return error_mark_node;
5921 	id_expr_token = token;
5922 	token = cp_lexer_peek_token (parser->lexer);
5923 	done = (token->type != CPP_OPEN_SQUARE
5924 		&& token->type != CPP_OPEN_PAREN
5925 		&& token->type != CPP_DOT
5926 		&& token->type != CPP_DEREF
5927 		&& token->type != CPP_PLUS_PLUS
5928 		&& token->type != CPP_MINUS_MINUS);
5929 	/* If we have a template-id, then no further lookup is
5930 	   required.  If the template-id was for a template-class, we
5931 	   will sometimes have a TYPE_DECL at this point.  */
5932 	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5933 		 || TREE_CODE (id_expression) == TYPE_DECL)
5934 	  decl = id_expression;
5935 	/* Look up the name.  */
5936 	else
5937 	  {
5938 	    tree ambiguous_decls;
5939 
5940 	    /* If we already know that this lookup is ambiguous, then
5941 	       we've already issued an error message; there's no reason
5942 	       to check again.  */
5943 	    if (id_expr_token->type == CPP_NAME
5944 		&& id_expr_token->error_reported)
5945 	      {
5946 		cp_parser_simulate_error (parser);
5947 		return error_mark_node;
5948 	      }
5949 
5950 	    decl = cp_parser_lookup_name (parser, id_expression,
5951 					  none_type,
5952 					  template_p,
5953 					  /*is_namespace=*/false,
5954 					  /*check_dependency=*/true,
5955 					  &ambiguous_decls,
5956 					  id_expression.get_location ());
5957 	    /* If the lookup was ambiguous, an error will already have
5958 	       been issued.  */
5959 	    if (ambiguous_decls)
5960 	      return error_mark_node;
5961 
5962 	    /* In Objective-C++, we may have an Objective-C 2.0
5963 	       dot-syntax for classes here.  */
5964 	    if (c_dialect_objc ()
5965 		&& cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5966 		&& TREE_CODE (decl) == TYPE_DECL
5967 		&& objc_is_class_name (decl))
5968 	      {
5969 		tree component;
5970 		cp_lexer_consume_token (parser->lexer);
5971 		component = cp_parser_identifier (parser);
5972 		if (component == error_mark_node)
5973 		  return error_mark_node;
5974 
5975 		tree result = objc_build_class_component_ref (id_expression,
5976 							      component);
5977 		/* Build a location of the form:
5978 		     expr.component
5979 		     ~~~~~^~~~~~~~~
5980 		   with caret at the start of the component name (at
5981 		   input_location), ranging from the start of the id_expression
5982 		   to the end of the component name.  */
5983 		location_t combined_loc
5984 		  = make_location (input_location, id_expression.get_start (),
5985 				   get_finish (input_location));
5986 		protected_set_expr_location (result, combined_loc);
5987 		return result;
5988 	      }
5989 
5990 	    /* In Objective-C++, an instance variable (ivar) may be preferred
5991 	       to whatever cp_parser_lookup_name() found.
5992 	       Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5993 	       rest of c-family, we have to do a little extra work to preserve
5994 	       any location information in cp_expr "decl".  Given that
5995 	       objc_lookup_ivar is implemented in "c-family" and "objc", we
5996 	       have a trip through the pure "tree" type, rather than cp_expr.
5997 	       Naively copying it back to "decl" would implicitly give the
5998 	       new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5999 	       store an EXPR_LOCATION.  Hence we only update "decl" (and
6000 	       hence its location_t) if we get back a different tree node.  */
6001 	    tree decl_tree = objc_lookup_ivar (decl.get_value (),
6002 					       id_expression);
6003 	    if (decl_tree != decl.get_value ())
6004 	      decl = cp_expr (decl_tree);
6005 
6006 	    /* If name lookup gives us a SCOPE_REF, then the
6007 	       qualifying scope was dependent.  */
6008 	    if (TREE_CODE (decl) == SCOPE_REF)
6009 	      {
6010 		/* At this point, we do not know if DECL is a valid
6011 		   integral constant expression.  We assume that it is
6012 		   in fact such an expression, so that code like:
6013 
6014 		      template <int N> struct A {
6015 			int a[B<N>::i];
6016 		      };
6017 
6018 		   is accepted.  At template-instantiation time, we
6019 		   will check that B<N>::i is actually a constant.  */
6020 		return decl;
6021 	      }
6022 	    /* Check to see if DECL is a local variable in a context
6023 	       where that is forbidden.  */
6024 	    if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6025 		&& local_variable_p (decl)
6026 		/* DR 2082 permits local variables in unevaluated contexts
6027 		   within a default argument.  */
6028 		&& !cp_unevaluated_operand)
6029 	      {
6030 		const char *msg
6031 		  = (TREE_CODE (decl) == PARM_DECL
6032 		     ? _("parameter %qD may not appear in this context")
6033 		     : _("local variable %qD may not appear in this context"));
6034 		error_at (id_expression.get_location (), msg,
6035 			  decl.get_value ());
6036 		return error_mark_node;
6037 	      }
6038 	  }
6039 
6040 	decl = (finish_id_expression
6041 		(id_expression, decl, parser->scope,
6042 		 idk,
6043 		 parser->integral_constant_expression_p,
6044 		 parser->allow_non_integral_constant_expression_p,
6045 		 &parser->non_integral_constant_expression_p,
6046 		 template_p, done, address_p,
6047 		 template_arg_p,
6048 		 &error_msg,
6049 		 id_expression.get_location ()));
6050 	if (error_msg)
6051 	  cp_parser_error (parser, error_msg);
6052 	/* Build a location for an id-expression of the form:
6053 	     ::ns::id
6054              ~~~~~~^~
6055 	  or:
6056 	     id
6057 	     ^~
6058 	   i.e. from the start of the first token to the end of the final
6059 	   token, with the caret at the start of the unqualified-id.  */
6060 	location_t caret_loc = get_pure_location (id_expression.get_location ());
6061 	location_t start_loc = get_start (id_expr_token->location);
6062 	location_t finish_loc = get_finish (id_expression.get_location ());
6063 	location_t combined_loc
6064 	  = make_location (caret_loc, start_loc, finish_loc);
6065 
6066 	decl.set_location (combined_loc);
6067 	return decl;
6068       }
6069 
6070       /* Anything else is an error.  */
6071     default:
6072       cp_parser_error (parser, "expected primary-expression");
6073       return error_mark_node;
6074     }
6075 }
6076 
6077 static inline cp_expr
cp_parser_primary_expression(cp_parser * parser,bool address_p,bool cast_p,bool template_arg_p,cp_id_kind * idk)6078 cp_parser_primary_expression (cp_parser *parser,
6079 			      bool address_p,
6080 			      bool cast_p,
6081 			      bool template_arg_p,
6082 			      cp_id_kind *idk)
6083 {
6084   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6085 				       /*decltype*/false, idk);
6086 }
6087 
6088 /* Parse an id-expression.
6089 
6090    id-expression:
6091      unqualified-id
6092      qualified-id
6093 
6094    qualified-id:
6095      :: [opt] nested-name-specifier template [opt] unqualified-id
6096      :: identifier
6097      :: operator-function-id
6098      :: template-id
6099 
6100    Return a representation of the unqualified portion of the
6101    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
6102    a `::' or nested-name-specifier.
6103 
6104    Often, if the id-expression was a qualified-id, the caller will
6105    want to make a SCOPE_REF to represent the qualified-id.  This
6106    function does not do this in order to avoid wastefully creating
6107    SCOPE_REFs when they are not required.
6108 
6109    If TEMPLATE_KEYWORD_P is true, then we have just seen the
6110    `template' keyword.
6111 
6112    If CHECK_DEPENDENCY_P is false, then names are looked up inside
6113    uninstantiated templates.
6114 
6115    If *TEMPLATE_P is non-NULL, it is set to true iff the
6116    `template' keyword is used to explicitly indicate that the entity
6117    named is a template.
6118 
6119    If DECLARATOR_P is true, the id-expression is appearing as part of
6120    a declarator, rather than as part of an expression.  */
6121 
6122 static cp_expr
cp_parser_id_expression(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool * template_p,bool declarator_p,bool optional_p)6123 cp_parser_id_expression (cp_parser *parser,
6124 			 bool template_keyword_p,
6125 			 bool check_dependency_p,
6126 			 bool *template_p,
6127 			 bool declarator_p,
6128 			 bool optional_p)
6129 {
6130   bool global_scope_p;
6131   bool nested_name_specifier_p;
6132 
6133   /* Assume the `template' keyword was not used.  */
6134   if (template_p)
6135     *template_p = template_keyword_p;
6136 
6137   /* Look for the optional `::' operator.  */
6138   global_scope_p
6139     = (!template_keyword_p
6140        && (cp_parser_global_scope_opt (parser,
6141 				       /*current_scope_valid_p=*/false)
6142 	   != NULL_TREE));
6143 
6144   /* Look for the optional nested-name-specifier.  */
6145   nested_name_specifier_p
6146     = (cp_parser_nested_name_specifier_opt (parser,
6147 					    /*typename_keyword_p=*/false,
6148 					    check_dependency_p,
6149 					    /*type_p=*/false,
6150 					    declarator_p,
6151 					    template_keyword_p)
6152        != NULL_TREE);
6153 
6154   cp_expr id = NULL_TREE;
6155   tree scope = parser->scope;
6156 
6157   /* Peek at the next token.  */
6158   cp_token *token = cp_lexer_peek_token (parser->lexer);
6159 
6160   /* If there is a nested-name-specifier, then we are looking at
6161      the first qualified-id production.  */
6162   if (nested_name_specifier_p)
6163     {
6164       tree saved_object_scope;
6165       tree saved_qualifying_scope;
6166 
6167       /* See if the next token is the `template' keyword.  */
6168       if (!template_p)
6169 	template_p = &template_keyword_p;
6170       *template_p = cp_parser_optional_template_keyword (parser);
6171       /* Name lookup we do during the processing of the
6172 	 unqualified-id might obliterate SCOPE.  */
6173       saved_object_scope = parser->object_scope;
6174       saved_qualifying_scope = parser->qualifying_scope;
6175       /* Process the final unqualified-id.  */
6176       id = cp_parser_unqualified_id (parser, *template_p,
6177 				     check_dependency_p,
6178 				     declarator_p,
6179 				     /*optional_p=*/false);
6180       /* Restore the SAVED_SCOPE for our caller.  */
6181       parser->scope = scope;
6182       parser->object_scope = saved_object_scope;
6183       parser->qualifying_scope = saved_qualifying_scope;
6184     }
6185   /* Otherwise, if we are in global scope, then we are looking at one
6186      of the other qualified-id productions.  */
6187   else if (global_scope_p)
6188     {
6189       /* If it's an identifier, and the next token is not a "<", then
6190 	 we can avoid the template-id case.  This is an optimization
6191 	 for this common case.  */
6192       if (token->type == CPP_NAME
6193 	  && !cp_parser_nth_token_starts_template_argument_list_p
6194 	       (parser, 2))
6195 	return cp_parser_identifier (parser);
6196 
6197       cp_parser_parse_tentatively (parser);
6198       /* Try a template-id.  */
6199       id = cp_parser_template_id_expr (parser,
6200 				       /*template_keyword_p=*/false,
6201 				       /*check_dependency_p=*/true,
6202 				       declarator_p);
6203       /* If that worked, we're done.  */
6204       if (cp_parser_parse_definitely (parser))
6205 	return id;
6206 
6207       /* Peek at the next token.  (Changes in the token buffer may
6208 	 have invalidated the pointer obtained above.)  */
6209       token = cp_lexer_peek_token (parser->lexer);
6210 
6211       switch (token->type)
6212 	{
6213 	case CPP_NAME:
6214 	  id = cp_parser_identifier (parser);
6215 	  break;
6216 
6217 	case CPP_KEYWORD:
6218 	  if (token->keyword == RID_OPERATOR)
6219 	    {
6220 	      id = cp_parser_operator_function_id (parser);
6221 	      break;
6222 	    }
6223 	  /* Fall through.  */
6224 
6225 	default:
6226 	  cp_parser_error (parser, "expected id-expression");
6227 	  return error_mark_node;
6228 	}
6229     }
6230   else
6231     {
6232       if (!scope)
6233 	scope = parser->context->object_type;
6234       id = cp_parser_unqualified_id (parser, template_keyword_p,
6235 				     /*check_dependency_p=*/true,
6236 				     declarator_p,
6237 				     optional_p);
6238     }
6239 
6240   if (id && TREE_CODE (id) == IDENTIFIER_NODE
6241       && warn_missing_template_keyword
6242       && !template_keyword_p
6243       /* Don't warn if we're looking inside templates.  */
6244       && check_dependency_p
6245       /* In a template argument list a > could be closing
6246 	 the enclosing targs.  */
6247       && !parser->in_template_argument_list_p
6248       && scope && dependentish_scope_p (scope)
6249       /* Don't confuse an ill-formed constructor declarator for a missing
6250 	 template keyword in a return type.  */
6251       && !(declarator_p && constructor_name_p (id, scope))
6252       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6253       && warning_enabled_at (token->location,
6254 			     OPT_Wmissing_template_keyword))
6255     {
6256       saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6257       if (cp_parser_skip_entire_template_parameter_list (parser)
6258 	  /* An operator after the > suggests that the > ends a
6259 	     template-id; a name or literal suggests that the > is an
6260 	     operator.  */
6261 	  && (cp_lexer_peek_token (parser->lexer)->type
6262 	      <= CPP_LAST_PUNCTUATOR))
6263 	warning_at (token->location, OPT_Wmissing_template_keyword,
6264 		    "expected %qs keyword before dependent "
6265 		    "template name", "template");
6266     }
6267 
6268   return id;
6269 }
6270 
6271 /* Parse an unqualified-id.
6272 
6273    unqualified-id:
6274      identifier
6275      operator-function-id
6276      conversion-function-id
6277      ~ class-name
6278      template-id
6279 
6280    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6281    keyword, in a construct like `A::template ...'.
6282 
6283    Returns a representation of unqualified-id.  For the `identifier'
6284    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
6285    production a BIT_NOT_EXPR is returned; the operand of the
6286    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
6287    other productions, see the documentation accompanying the
6288    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
6289    names are looked up in uninstantiated templates.  If DECLARATOR_P
6290    is true, the unqualified-id is appearing as part of a declarator,
6291    rather than as part of an expression.  */
6292 
6293 static cp_expr
cp_parser_unqualified_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool declarator_p,bool optional_p)6294 cp_parser_unqualified_id (cp_parser* parser,
6295 			  bool template_keyword_p,
6296 			  bool check_dependency_p,
6297 			  bool declarator_p,
6298 			  bool optional_p)
6299 {
6300   cp_token *token;
6301 
6302   /* Peek at the next token.  */
6303   token = cp_lexer_peek_token (parser->lexer);
6304 
6305   switch ((int) token->type)
6306     {
6307     case CPP_NAME:
6308       {
6309 	tree id;
6310 
6311 	/* We don't know yet whether or not this will be a
6312 	   template-id.  */
6313 	cp_parser_parse_tentatively (parser);
6314 	/* Try a template-id.  */
6315 	id = cp_parser_template_id_expr (parser, template_keyword_p,
6316 					 check_dependency_p,
6317 					 declarator_p);
6318 	/* If it worked, we're done.  */
6319 	if (cp_parser_parse_definitely (parser))
6320 	  return id;
6321 	/* Otherwise, it's an ordinary identifier.  */
6322 	return cp_parser_identifier (parser);
6323       }
6324 
6325     case CPP_TEMPLATE_ID:
6326       return cp_parser_template_id_expr (parser, template_keyword_p,
6327 					 check_dependency_p,
6328 					 declarator_p);
6329 
6330     case CPP_COMPL:
6331       {
6332 	tree type_decl;
6333 	tree qualifying_scope;
6334 	tree object_scope;
6335 	tree scope;
6336 	bool done;
6337 	location_t tilde_loc = token->location;
6338 
6339 	/* Consume the `~' token.  */
6340 	cp_lexer_consume_token (parser->lexer);
6341 	/* Parse the class-name.  The standard, as written, seems to
6342 	   say that:
6343 
6344 	     template <typename T> struct S { ~S (); };
6345 	     template <typename T> S<T>::~S() {}
6346 
6347 	   is invalid, since `~' must be followed by a class-name, but
6348 	   `S<T>' is dependent, and so not known to be a class.
6349 	   That's not right; we need to look in uninstantiated
6350 	   templates.  A further complication arises from:
6351 
6352 	     template <typename T> void f(T t) {
6353 	       t.T::~T();
6354 	     }
6355 
6356 	   Here, it is not possible to look up `T' in the scope of `T'
6357 	   itself.  We must look in both the current scope, and the
6358 	   scope of the containing complete expression.
6359 
6360 	   Yet another issue is:
6361 
6362 	     struct S {
6363 	       int S;
6364 	       ~S();
6365 	     };
6366 
6367 	     S::~S() {}
6368 
6369 	   The standard does not seem to say that the `S' in `~S'
6370 	   should refer to the type `S' and not the data member
6371 	   `S::S'.  */
6372 
6373 	/* DR 244 says that we look up the name after the "~" in the
6374 	   same scope as we looked up the qualifying name.  That idea
6375 	   isn't fully worked out; it's more complicated than that.  */
6376 	scope = parser->scope;
6377 	object_scope = parser->object_scope;
6378 	qualifying_scope = parser->qualifying_scope;
6379 
6380 	/* Check for invalid scopes.  */
6381 	if (scope == error_mark_node)
6382 	  {
6383 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6384 	      cp_lexer_consume_token (parser->lexer);
6385 	    return error_mark_node;
6386 	  }
6387 	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6388 	  {
6389 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6390 	      error_at (token->location,
6391 			"scope %qT before %<~%> is not a class-name",
6392 			scope);
6393 	    cp_parser_simulate_error (parser);
6394 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6395 	      cp_lexer_consume_token (parser->lexer);
6396 	    return error_mark_node;
6397 	  }
6398 	if (template_keyword_p)
6399 	  {
6400 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6401 	      error_at (tilde_loc, "%<template%> keyword not permitted in "
6402 			"destructor name");
6403 	    cp_parser_simulate_error (parser);
6404 	    return error_mark_node;
6405 	  }
6406 
6407 	gcc_assert (!scope || TYPE_P (scope));
6408 
6409 	token = cp_lexer_peek_token (parser->lexer);
6410 
6411 	/* Create a location with caret == start at the tilde,
6412 	   finishing at the end of the peeked token, e.g:
6413 	   ~token
6414 	   ^~~~~~.  */
6415 	location_t loc
6416 	  = make_location (tilde_loc, tilde_loc, token->location);
6417 
6418 	/* If the name is of the form "X::~X" it's OK even if X is a
6419 	   typedef.  */
6420 
6421 	if (scope
6422 	    && token->type == CPP_NAME
6423 	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6424 		!= CPP_LESS)
6425 	    && (token->u.value == TYPE_IDENTIFIER (scope)
6426 		|| (CLASS_TYPE_P (scope)
6427 		    && constructor_name_p (token->u.value, scope))))
6428 	  {
6429 	    cp_lexer_consume_token (parser->lexer);
6430 	    return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6431 	  }
6432 
6433 	/* ~auto means the destructor of whatever the object is.  */
6434 	if (cp_parser_is_keyword (token, RID_AUTO))
6435 	  {
6436 	    if (cxx_dialect < cxx14)
6437 	      pedwarn (loc, OPT_Wc__14_extensions,
6438 		       "%<~auto%> only available with "
6439 		       "%<-std=c++14%> or %<-std=gnu++14%>");
6440 	    cp_lexer_consume_token (parser->lexer);
6441 	    return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6442 	  }
6443 
6444 	/* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6445 	   declarator-id of a constructor or destructor.  */
6446 	if (token->type == CPP_TEMPLATE_ID && declarator_p
6447 	    && cxx_dialect >= cxx20)
6448 	  {
6449 	    if (!cp_parser_simulate_error (parser))
6450 	      error_at (tilde_loc, "template-id not allowed for destructor");
6451 	    return error_mark_node;
6452 	  }
6453 
6454 	/* If there was an explicit qualification (S::~T), first look
6455 	   in the scope given by the qualification (i.e., S).
6456 
6457 	   Note: in the calls to cp_parser_class_name below we pass
6458 	   typename_type so that lookup finds the injected-class-name
6459 	   rather than the constructor.  */
6460 	done = false;
6461 	type_decl = NULL_TREE;
6462 	if (scope)
6463 	  {
6464 	    cp_parser_parse_tentatively (parser);
6465 	    type_decl = cp_parser_class_name (parser,
6466 					      /*typename_keyword_p=*/false,
6467 					      /*template_keyword_p=*/false,
6468 					      typename_type,
6469 					      /*check_dependency=*/false,
6470 					      /*class_head_p=*/false,
6471 					      declarator_p);
6472 	    if (cp_parser_parse_definitely (parser))
6473 	      done = true;
6474 	  }
6475 	/* In "N::S::~S", look in "N" as well.  */
6476 	if (!done && scope && qualifying_scope)
6477 	  {
6478 	    cp_parser_parse_tentatively (parser);
6479 	    parser->scope = qualifying_scope;
6480 	    parser->object_scope = NULL_TREE;
6481 	    parser->qualifying_scope = NULL_TREE;
6482 	    type_decl
6483 	      = cp_parser_class_name (parser,
6484 				      /*typename_keyword_p=*/false,
6485 				      /*template_keyword_p=*/false,
6486 				      typename_type,
6487 				      /*check_dependency=*/false,
6488 				      /*class_head_p=*/false,
6489 				      declarator_p);
6490 	    if (cp_parser_parse_definitely (parser))
6491 	      done = true;
6492 	  }
6493 	/* In "p->S::~T", look in the scope given by "*p" as well.  */
6494 	else if (!done && object_scope)
6495 	  {
6496 	    cp_parser_parse_tentatively (parser);
6497 	    parser->scope = object_scope;
6498 	    parser->object_scope = NULL_TREE;
6499 	    parser->qualifying_scope = NULL_TREE;
6500 	    type_decl
6501 	      = cp_parser_class_name (parser,
6502 				      /*typename_keyword_p=*/false,
6503 				      /*template_keyword_p=*/false,
6504 				      typename_type,
6505 				      /*check_dependency=*/false,
6506 				      /*class_head_p=*/false,
6507 				      declarator_p);
6508 	    if (cp_parser_parse_definitely (parser))
6509 	      done = true;
6510 	  }
6511 	/* Look in the surrounding context.  */
6512 	if (!done)
6513 	  {
6514 	    parser->scope = NULL_TREE;
6515 	    parser->object_scope = NULL_TREE;
6516 	    parser->qualifying_scope = NULL_TREE;
6517 	    if (processing_template_decl)
6518 	      cp_parser_parse_tentatively (parser);
6519 	    type_decl
6520 	      = cp_parser_class_name (parser,
6521 				      /*typename_keyword_p=*/false,
6522 				      /*template_keyword_p=*/false,
6523 				      typename_type,
6524 				      /*check_dependency=*/false,
6525 				      /*class_head_p=*/false,
6526 				      declarator_p);
6527 	    if (processing_template_decl
6528 		&& ! cp_parser_parse_definitely (parser))
6529 	      {
6530 		/* We couldn't find a type with this name.  If we're parsing
6531 		   tentatively, fail and try something else.  */
6532 		if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6533 		  {
6534 		    cp_parser_simulate_error (parser);
6535 		    return error_mark_node;
6536 		  }
6537 		/* Otherwise, accept it and check for a match at instantiation
6538 		   time.  */
6539 		type_decl = cp_parser_identifier (parser);
6540 		if (type_decl != error_mark_node)
6541 		  type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6542 		return type_decl;
6543 	      }
6544 	  }
6545 	/* If an error occurred, assume that the name of the
6546 	   destructor is the same as the name of the qualifying
6547 	   class.  That allows us to keep parsing after running
6548 	   into ill-formed destructor names.  */
6549 	if (type_decl == error_mark_node && scope)
6550 	  return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6551 	else if (type_decl == error_mark_node)
6552 	  return error_mark_node;
6553 
6554 	/* Check that destructor name and scope match.  */
6555 	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6556 	  {
6557 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6558 	      error_at (loc,
6559 			"declaration of %<~%T%> as member of %qT",
6560 			type_decl, scope);
6561 	    cp_parser_simulate_error (parser);
6562 	    return error_mark_node;
6563 	  }
6564 
6565 	/* [class.dtor]
6566 
6567 	   A typedef-name that names a class shall not be used as the
6568 	   identifier in the declarator for a destructor declaration.  */
6569 	if (declarator_p
6570 	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6571 	    && !DECL_SELF_REFERENCE_P (type_decl)
6572 	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6573 	  error_at (loc,
6574 		    "typedef-name %qD used as destructor declarator",
6575 		    type_decl);
6576 
6577 	return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6578       }
6579 
6580     case CPP_KEYWORD:
6581       if (token->keyword == RID_OPERATOR)
6582 	{
6583 	  cp_expr id;
6584 
6585 	  /* This could be a template-id, so we try that first.  */
6586 	  cp_parser_parse_tentatively (parser);
6587 	  /* Try a template-id.  */
6588 	  id = cp_parser_template_id_expr (parser, template_keyword_p,
6589 					   /*check_dependency_p=*/true,
6590 					   declarator_p);
6591 	  /* If that worked, we're done.  */
6592 	  if (cp_parser_parse_definitely (parser))
6593 	    return id;
6594 	  /* We still don't know whether we're looking at an
6595 	     operator-function-id or a conversion-function-id.  */
6596 	  cp_parser_parse_tentatively (parser);
6597 	  /* Try an operator-function-id.  */
6598 	  id = cp_parser_operator_function_id (parser);
6599 	  /* If that didn't work, try a conversion-function-id.  */
6600 	  if (!cp_parser_parse_definitely (parser))
6601 	    id = cp_parser_conversion_function_id (parser);
6602 
6603 	  return id;
6604 	}
6605       /* Fall through.  */
6606 
6607     default:
6608       if (optional_p)
6609 	return NULL_TREE;
6610       cp_parser_error (parser, "expected unqualified-id");
6611       return error_mark_node;
6612     }
6613 }
6614 
6615 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6616    be a template-id or the name shall refer to a class template or an
6617    alias template.  */
6618 
6619 static void
check_template_keyword_in_nested_name_spec(tree name)6620 check_template_keyword_in_nested_name_spec (tree name)
6621 {
6622   if (CLASS_TYPE_P (name)
6623       && ((CLASSTYPE_USE_TEMPLATE (name)
6624 	   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6625 	  || CLASSTYPE_IS_TEMPLATE (name)))
6626     return;
6627 
6628   if (TREE_CODE (name) == TYPENAME_TYPE
6629       && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6630     return;
6631   /* Alias templates are also OK.  */
6632   else if (alias_template_specialization_p (name, nt_opaque))
6633     return;
6634 
6635   permerror (input_location, TYPE_P (name)
6636 	     ? G_("%qT is not a template")
6637 	     : G_("%qD is not a template"),
6638 	     name);
6639 }
6640 
6641 /* Parse an (optional) nested-name-specifier.
6642 
6643    nested-name-specifier: [C++98]
6644      class-or-namespace-name :: nested-name-specifier [opt]
6645      class-or-namespace-name :: template nested-name-specifier [opt]
6646 
6647    nested-name-specifier: [C++0x]
6648      type-name ::
6649      namespace-name ::
6650      nested-name-specifier identifier ::
6651      nested-name-specifier template [opt] simple-template-id ::
6652 
6653    PARSER->SCOPE should be set appropriately before this function is
6654    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6655    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
6656    in name lookups.
6657 
6658    Sets PARSER->SCOPE to the class (TYPE) or namespace
6659    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6660    it unchanged if there is no nested-name-specifier.  Returns the new
6661    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6662 
6663    If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6664 
6665    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6666    part of a declaration and/or decl-specifier.  */
6667 
6668 static tree
cp_parser_nested_name_specifier_opt(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration,bool template_keyword_p)6669 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6670 				     bool typename_keyword_p,
6671 				     bool check_dependency_p,
6672 				     bool type_p,
6673 				     bool is_declaration,
6674 				     bool template_keyword_p /* = false */)
6675 {
6676   bool success = false;
6677   cp_token_position start = 0;
6678   cp_token *token;
6679 
6680   /* Remember where the nested-name-specifier starts.  */
6681   if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6682       && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6683     {
6684       start = cp_lexer_token_position (parser->lexer, false);
6685       push_deferring_access_checks (dk_deferred);
6686     }
6687 
6688   while (true)
6689     {
6690       tree new_scope;
6691       tree old_scope;
6692       tree saved_qualifying_scope;
6693 
6694       /* Spot cases that cannot be the beginning of a
6695 	 nested-name-specifier.  */
6696       token = cp_lexer_peek_token (parser->lexer);
6697 
6698       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6699 	 the already parsed nested-name-specifier.  */
6700       if (token->type == CPP_NESTED_NAME_SPECIFIER)
6701 	{
6702 	  /* Grab the nested-name-specifier and continue the loop.  */
6703 	  cp_parser_pre_parsed_nested_name_specifier (parser);
6704 	  /* If we originally encountered this nested-name-specifier
6705 	     with CHECK_DEPENDENCY_P set to true, we will not have
6706 	     resolved TYPENAME_TYPEs, so we must do so here.  */
6707 	  if (is_declaration
6708 	      && !check_dependency_p
6709 	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6710 	    {
6711 	      new_scope = resolve_typename_type (parser->scope,
6712 						 /*only_current_p=*/false);
6713 	      if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6714 		parser->scope = new_scope;
6715 	    }
6716 	  success = true;
6717 	  continue;
6718 	}
6719 
6720       /* Spot cases that cannot be the beginning of a
6721 	 nested-name-specifier.  On the second and subsequent times
6722 	 through the loop, we look for the `template' keyword.  */
6723       if (success && token->keyword == RID_TEMPLATE)
6724 	;
6725       /* A template-id can start a nested-name-specifier.  */
6726       else if (token->type == CPP_TEMPLATE_ID)
6727 	;
6728       /* DR 743: decltype can be used in a nested-name-specifier.  */
6729       else if (token_is_decltype (token))
6730 	;
6731       else
6732 	{
6733 	  /* If the next token is not an identifier, then it is
6734 	     definitely not a type-name or namespace-name.  */
6735 	  if (token->type != CPP_NAME)
6736 	    break;
6737 	  /* If the following token is neither a `<' (to begin a
6738 	     template-id), nor a `::', then we are not looking at a
6739 	     nested-name-specifier.  */
6740 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
6741 
6742 	  if (token->type == CPP_COLON
6743 	      && parser->colon_corrects_to_scope_p
6744 	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6745 	      /* name:name is a valid sequence in an Objective C message.  */
6746 	      && !parser->objective_c_message_context_p)
6747 	    {
6748 	      gcc_rich_location richloc (token->location);
6749 	      richloc.add_fixit_replace ("::");
6750 	      error_at (&richloc,
6751 			"found %<:%> in nested-name-specifier, "
6752 			"expected %<::%>");
6753 	      token->type = CPP_SCOPE;
6754 	    }
6755 
6756 	  if (token->type != CPP_SCOPE
6757 	      && !cp_parser_nth_token_starts_template_argument_list_p
6758 		  (parser, 2))
6759 	    break;
6760 	}
6761 
6762       /* The nested-name-specifier is optional, so we parse
6763 	 tentatively.  */
6764       cp_parser_parse_tentatively (parser);
6765 
6766       /* Look for the optional `template' keyword, if this isn't the
6767 	 first time through the loop.  */
6768       if (success)
6769 	{
6770 	  template_keyword_p = cp_parser_optional_template_keyword (parser);
6771 	  /* DR1710: "In a qualified-id used as the name in
6772 	     a typename-specifier, elaborated-type-specifier, using-declaration,
6773 	     or class-or-decltype, an optional keyword template appearing at
6774 	     the top level is ignored."  */
6775 	  if (!template_keyword_p
6776 	      && typename_keyword_p
6777 	      && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6778 	    template_keyword_p = true;
6779 	}
6780 
6781       /* Save the old scope since the name lookup we are about to do
6782 	 might destroy it.  */
6783       old_scope = parser->scope;
6784       saved_qualifying_scope = parser->qualifying_scope;
6785       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6786 	 look up names in "X<T>::I" in order to determine that "Y" is
6787 	 a template.  So, if we have a typename at this point, we make
6788 	 an effort to look through it.  */
6789       if (is_declaration
6790 	  && !check_dependency_p
6791 	  && !typename_keyword_p
6792 	  && parser->scope
6793 	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6794 	parser->scope = resolve_typename_type (parser->scope,
6795 					       /*only_current_p=*/false);
6796       /* Parse the qualifying entity.  */
6797       new_scope
6798 	= cp_parser_qualifying_entity (parser,
6799                                        typename_keyword_p,
6800                                        template_keyword_p,
6801                                        check_dependency_p,
6802                                        type_p,
6803                                        is_declaration);
6804       /* Look for the `::' token.  */
6805       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6806 
6807       /* If we found what we wanted, we keep going; otherwise, we're
6808 	 done.  */
6809       if (!cp_parser_parse_definitely (parser))
6810 	{
6811 	  bool error_p = false;
6812 
6813 	  /* Restore the OLD_SCOPE since it was valid before the
6814 	     failed attempt at finding the last
6815 	     class-or-namespace-name.  */
6816 	  parser->scope = old_scope;
6817 	  parser->qualifying_scope = saved_qualifying_scope;
6818 
6819 	  /* If the next token is a decltype, and the one after that is a
6820 	     `::', then the decltype has failed to resolve to a class or
6821 	     enumeration type.  Give this error even when parsing
6822 	     tentatively since it can't possibly be valid--and we're going
6823 	     to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6824 	     won't get another chance.*/
6825 	  if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6826 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6827 		  == CPP_SCOPE))
6828 	    {
6829 	      token = cp_lexer_consume_token (parser->lexer);
6830 	      tree dtype = token->u.tree_check_value->value;
6831 	      if (dtype != error_mark_node)
6832 		error_at (token->location, "%<decltype%> evaluates to %qT, "
6833 			  "which is not a class or enumeration type",
6834 			  dtype);
6835 	      parser->scope = error_mark_node;
6836 	      error_p = true;
6837 	      /* As below.  */
6838 	      success = true;
6839 	      cp_lexer_consume_token (parser->lexer);
6840 	    }
6841 
6842 	  if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6843 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6844 	    {
6845 	      /* If we have a non-type template-id followed by ::, it can't
6846 		 possibly be valid.  */
6847 	      token = cp_lexer_peek_token (parser->lexer);
6848 	      tree tid = token->u.tree_check_value->value;
6849 	      if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6850 		  && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6851 		{
6852 		  tree tmpl = NULL_TREE;
6853 		  if (is_overloaded_fn (tid))
6854 		    {
6855 		      tree fns = get_fns (tid);
6856 		      if (OVL_SINGLE_P (fns))
6857 			tmpl = OVL_FIRST (fns);
6858 		      if (function_concept_p (fns))
6859 			error_at (token->location, "concept-id %qD "
6860 				  "in nested-name-specifier", tid);
6861 		      else
6862 			error_at (token->location, "function template-id "
6863 				  "%qD in nested-name-specifier", tid);
6864 		    }
6865 		  else
6866 		    {
6867 		      tmpl = TREE_OPERAND (tid, 0);
6868 		      if (variable_concept_p (tmpl)
6869 			  || standard_concept_p (tmpl))
6870 			error_at (token->location, "concept-id %qD "
6871 				  "in nested-name-specifier", tid);
6872 		      else
6873 			{
6874 			  /* Variable template.  */
6875 			  gcc_assert (variable_template_p (tmpl));
6876 			  error_at (token->location, "variable template-id "
6877 				    "%qD in nested-name-specifier", tid);
6878 			}
6879 		    }
6880 		  if (tmpl)
6881 		    inform (DECL_SOURCE_LOCATION (tmpl),
6882 			    "%qD declared here", tmpl);
6883 
6884 		  parser->scope = error_mark_node;
6885 		  error_p = true;
6886 		  /* As below.  */
6887 		  success = true;
6888 		  cp_lexer_consume_token (parser->lexer);
6889 		  cp_lexer_consume_token (parser->lexer);
6890 		}
6891 	    }
6892 
6893 	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6894 	    break;
6895 	  /* If the next token is an identifier, and the one after
6896 	     that is a `::', then any valid interpretation would have
6897 	     found a class-or-namespace-name.  */
6898 	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6899 		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6900 		     == CPP_SCOPE)
6901 		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6902 		     != CPP_COMPL))
6903 	    {
6904 	      token = cp_lexer_consume_token (parser->lexer);
6905 	      if (!error_p)
6906 		{
6907 		  if (!token->error_reported)
6908 		    {
6909 		      tree decl;
6910 		      tree ambiguous_decls;
6911 
6912 		      decl = cp_parser_lookup_name (parser, token->u.value,
6913 						    none_type,
6914 						    /*is_template=*/false,
6915 						    /*is_namespace=*/false,
6916 						    /*check_dependency=*/true,
6917 						    &ambiguous_decls,
6918 						    token->location);
6919 		      if (TREE_CODE (decl) == TEMPLATE_DECL)
6920 			error_at (token->location,
6921 				  "%qD used without template arguments",
6922 				  decl);
6923 		      else if (ambiguous_decls)
6924 			{
6925 			  // cp_parser_lookup_name has the same diagnostic,
6926 			  // thus make sure to emit it at most once.
6927 			  if (cp_parser_uncommitted_to_tentative_parse_p
6928 			      (parser))
6929 			    {
6930 			      error_at (token->location,
6931 					"reference to %qD is ambiguous",
6932 					token->u.value);
6933 			      print_candidates (ambiguous_decls);
6934 			    }
6935 			  decl = error_mark_node;
6936 			}
6937 		      else
6938                         {
6939                           if (cxx_dialect != cxx98)
6940                             cp_parser_name_lookup_error
6941                             (parser, token->u.value, decl, NLE_NOT_CXX98,
6942 	  		     token->location);
6943 			  else
6944 			    cp_parser_name_lookup_error
6945 			    (parser, token->u.value, decl, NLE_CXX98,
6946 			     token->location);
6947                         }
6948 		    }
6949 		  parser->scope = error_mark_node;
6950 		  error_p = true;
6951 		  /* Treat this as a successful nested-name-specifier
6952 		     due to:
6953 
6954 		     [basic.lookup.qual]
6955 
6956 		     If the name found is not a class-name (clause
6957 		     _class_) or namespace-name (_namespace.def_), the
6958 		     program is ill-formed.  */
6959 		  success = true;
6960 		}
6961 	      cp_lexer_consume_token (parser->lexer);
6962 	    }
6963 	  break;
6964 	}
6965       /* We've found one valid nested-name-specifier.  */
6966       success = true;
6967       /* Name lookup always gives us a DECL.  */
6968       if (TREE_CODE (new_scope) == TYPE_DECL)
6969 	new_scope = TREE_TYPE (new_scope);
6970       /* Uses of "template" must be followed by actual templates.  */
6971       if (template_keyword_p)
6972 	check_template_keyword_in_nested_name_spec (new_scope);
6973       /* If it is a class scope, try to complete it; we are about to
6974 	 be looking up names inside the class.  */
6975       if (TYPE_P (new_scope)
6976 	  /* Since checking types for dependency can be expensive,
6977 	     avoid doing it if the type is already complete.  */
6978 	  && !COMPLETE_TYPE_P (new_scope)
6979 	  /* Do not try to complete dependent types.  */
6980 	  && !dependent_type_p (new_scope))
6981 	{
6982 	  new_scope = complete_type (new_scope);
6983 	  /* If it is a typedef to current class, use the current
6984 	     class instead, as the typedef won't have any names inside
6985 	     it yet.  */
6986 	  if (!COMPLETE_TYPE_P (new_scope)
6987 	      && currently_open_class (new_scope))
6988 	    new_scope = TYPE_MAIN_VARIANT (new_scope);
6989 	}
6990       /* Make sure we look in the right scope the next time through
6991 	 the loop.  */
6992       parser->scope = new_scope;
6993     }
6994 
6995   /* If parsing tentatively, replace the sequence of tokens that makes
6996      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6997      token.  That way, should we re-parse the token stream, we will
6998      not have to repeat the effort required to do the parse, nor will
6999      we issue duplicate error messages.  */
7000   if (success && start)
7001     {
7002       cp_token *token;
7003 
7004       token = cp_lexer_token_at (parser->lexer, start);
7005       /* Reset the contents of the START token.  */
7006       token->type = CPP_NESTED_NAME_SPECIFIER;
7007       /* Retrieve any deferred checks.  Do not pop this access checks yet
7008 	 so the memory will not be reclaimed during token replacing below.  */
7009       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7010       token->tree_check_p = true;
7011       token->u.tree_check_value->value = parser->scope;
7012       token->u.tree_check_value->checks = get_deferred_access_checks ();
7013       token->u.tree_check_value->qualifying_scope =
7014 	parser->qualifying_scope;
7015       token->keyword = RID_MAX;
7016 
7017       /* Purge all subsequent tokens.  */
7018       cp_lexer_purge_tokens_after (parser->lexer, start);
7019     }
7020 
7021   if (start)
7022     pop_to_parent_deferring_access_checks ();
7023 
7024   return success ? parser->scope : NULL_TREE;
7025 }
7026 
7027 /* Parse a nested-name-specifier.  See
7028    cp_parser_nested_name_specifier_opt for details.  This function
7029    behaves identically, except that it will an issue an error if no
7030    nested-name-specifier is present.  */
7031 
7032 static tree
cp_parser_nested_name_specifier(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)7033 cp_parser_nested_name_specifier (cp_parser *parser,
7034 				 bool typename_keyword_p,
7035 				 bool check_dependency_p,
7036 				 bool type_p,
7037 				 bool is_declaration)
7038 {
7039   tree scope;
7040 
7041   /* Look for the nested-name-specifier.  */
7042   scope = cp_parser_nested_name_specifier_opt (parser,
7043 					       typename_keyword_p,
7044 					       check_dependency_p,
7045 					       type_p,
7046 					       is_declaration);
7047   /* If it was not present, issue an error message.  */
7048   if (!scope)
7049     {
7050       cp_parser_error (parser, "expected nested-name-specifier");
7051       parser->scope = NULL_TREE;
7052     }
7053 
7054   return scope;
7055 }
7056 
7057 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7058    this is either a class-name or a namespace-name (which corresponds
7059    to the class-or-namespace-name production in the grammar). For
7060    C++0x, it can also be a type-name that refers to an enumeration
7061    type or a simple-template-id.
7062 
7063    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7064    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7065    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7066    TYPE_P is TRUE iff the next name should be taken as a class-name,
7067    even the same name is declared to be another entity in the same
7068    scope.
7069 
7070    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7071    specified by the class-or-namespace-name.  If neither is found the
7072    ERROR_MARK_NODE is returned.  */
7073 
7074 static tree
cp_parser_qualifying_entity(cp_parser * parser,bool typename_keyword_p,bool template_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)7075 cp_parser_qualifying_entity (cp_parser *parser,
7076 			     bool typename_keyword_p,
7077 			     bool template_keyword_p,
7078 			     bool check_dependency_p,
7079 			     bool type_p,
7080 			     bool is_declaration)
7081 {
7082   tree saved_scope;
7083   tree saved_qualifying_scope;
7084   tree saved_object_scope;
7085   tree scope;
7086   bool only_class_p;
7087   bool successful_parse_p;
7088 
7089   /* DR 743: decltype can appear in a nested-name-specifier.  */
7090   if (cp_lexer_next_token_is_decltype (parser->lexer))
7091     {
7092       scope = cp_parser_decltype (parser);
7093       if (TREE_CODE (scope) != ENUMERAL_TYPE
7094 	  && !MAYBE_CLASS_TYPE_P (scope))
7095 	{
7096 	  cp_parser_simulate_error (parser);
7097 	  return error_mark_node;
7098 	}
7099       if (TYPE_NAME (scope))
7100 	scope = TYPE_NAME (scope);
7101       return scope;
7102     }
7103 
7104   /* Before we try to parse the class-name, we must save away the
7105      current PARSER->SCOPE since cp_parser_class_name will destroy
7106      it.  */
7107   saved_scope = parser->scope;
7108   saved_qualifying_scope = parser->qualifying_scope;
7109   saved_object_scope = parser->object_scope;
7110   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
7111      there is no need to look for a namespace-name.  */
7112   only_class_p = template_keyword_p
7113     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7114   if (!only_class_p)
7115     cp_parser_parse_tentatively (parser);
7116   scope = cp_parser_class_name (parser,
7117 				typename_keyword_p,
7118 				template_keyword_p,
7119 				type_p ? class_type : none_type,
7120 				check_dependency_p,
7121 				/*class_head_p=*/false,
7122 				is_declaration,
7123 				/*enum_ok=*/cxx_dialect > cxx98);
7124   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7125   /* If that didn't work, try for a namespace-name.  */
7126   if (!only_class_p && !successful_parse_p)
7127     {
7128       /* Restore the saved scope.  */
7129       parser->scope = saved_scope;
7130       parser->qualifying_scope = saved_qualifying_scope;
7131       parser->object_scope = saved_object_scope;
7132       /* If we are not looking at an identifier followed by the scope
7133 	 resolution operator, then this is not part of a
7134 	 nested-name-specifier.  (Note that this function is only used
7135 	 to parse the components of a nested-name-specifier.)  */
7136       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7137 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7138 	return error_mark_node;
7139       scope = cp_parser_namespace_name (parser);
7140     }
7141 
7142   return scope;
7143 }
7144 
7145 /* Return true if we are looking at a compound-literal, false otherwise.  */
7146 
7147 static bool
cp_parser_compound_literal_p(cp_parser * parser)7148 cp_parser_compound_literal_p (cp_parser *parser)
7149 {
7150   cp_lexer_save_tokens (parser->lexer);
7151 
7152   /* Skip tokens until the next token is a closing parenthesis.
7153      If we find the closing `)', and the next token is a `{', then
7154      we are looking at a compound-literal.  */
7155   bool compound_literal_p
7156     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7157 					      /*consume_paren=*/true)
7158        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7159 
7160   /* Roll back the tokens we skipped.  */
7161   cp_lexer_rollback_tokens (parser->lexer);
7162 
7163   return compound_literal_p;
7164 }
7165 
7166 /* Return true if EXPR is the integer constant zero or a complex constant
7167    of zero, without any folding, but ignoring location wrappers.  */
7168 
7169 bool
literal_integer_zerop(const_tree expr)7170 literal_integer_zerop (const_tree expr)
7171 {
7172   return (location_wrapper_p (expr)
7173 	  && integer_zerop (TREE_OPERAND (expr, 0)));
7174 }
7175 
7176 /* Parse a postfix-expression.
7177 
7178    postfix-expression:
7179      primary-expression
7180      postfix-expression [ expression ]
7181      postfix-expression ( expression-list [opt] )
7182      simple-type-specifier ( expression-list [opt] )
7183      typename :: [opt] nested-name-specifier identifier
7184        ( expression-list [opt] )
7185      typename :: [opt] nested-name-specifier template [opt] template-id
7186        ( expression-list [opt] )
7187      postfix-expression . template [opt] id-expression
7188      postfix-expression -> template [opt] id-expression
7189      postfix-expression . pseudo-destructor-name
7190      postfix-expression -> pseudo-destructor-name
7191      postfix-expression ++
7192      postfix-expression --
7193      dynamic_cast < type-id > ( expression )
7194      static_cast < type-id > ( expression )
7195      reinterpret_cast < type-id > ( expression )
7196      const_cast < type-id > ( expression )
7197      typeid ( expression )
7198      typeid ( type-id )
7199 
7200    GNU Extension:
7201 
7202    postfix-expression:
7203      ( type-id ) { initializer-list , [opt] }
7204 
7205    This extension is a GNU version of the C99 compound-literal
7206    construct.  (The C99 grammar uses `type-name' instead of `type-id',
7207    but they are essentially the same concept.)
7208 
7209    If ADDRESS_P is true, the postfix expression is the operand of the
7210    `&' operator.  CAST_P is true if this expression is the target of a
7211    cast.
7212 
7213    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7214    class member access expressions [expr.ref].
7215 
7216    Returns a representation of the expression.  */
7217 
7218 static cp_expr
cp_parser_postfix_expression(cp_parser * parser,bool address_p,bool cast_p,bool member_access_only_p,bool decltype_p,cp_id_kind * pidk_return)7219 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7220                               bool member_access_only_p, bool decltype_p,
7221 			      cp_id_kind * pidk_return)
7222 {
7223   cp_token *token;
7224   location_t loc;
7225   enum rid keyword;
7226   cp_id_kind idk = CP_ID_KIND_NONE;
7227   cp_expr postfix_expression = NULL_TREE;
7228   bool is_member_access = false;
7229 
7230   /* Peek at the next token.  */
7231   token = cp_lexer_peek_token (parser->lexer);
7232   loc = token->location;
7233   location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7234 
7235   /* Some of the productions are determined by keywords.  */
7236   keyword = token->keyword;
7237   switch (keyword)
7238     {
7239     case RID_DYNCAST:
7240     case RID_STATCAST:
7241     case RID_REINTCAST:
7242     case RID_CONSTCAST:
7243       {
7244 	tree type;
7245 	cp_expr expression;
7246 	const char *saved_message;
7247 	bool saved_in_type_id_in_expr_p;
7248 
7249 	/* All of these can be handled in the same way from the point
7250 	   of view of parsing.  Begin by consuming the token
7251 	   identifying the cast.  */
7252 	cp_lexer_consume_token (parser->lexer);
7253 
7254 	/* New types cannot be defined in the cast.  */
7255 	saved_message = parser->type_definition_forbidden_message;
7256 	parser->type_definition_forbidden_message
7257 	  = G_("types may not be defined in casts");
7258 
7259 	/* Look for the opening `<'.  */
7260 	cp_parser_require (parser, CPP_LESS, RT_LESS);
7261 	/* Parse the type to which we are casting.  */
7262 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7263 	parser->in_type_id_in_expr_p = true;
7264 	type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7265 				  NULL);
7266 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7267 	/* Look for the closing `>'.  */
7268 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7269 	/* Restore the old message.  */
7270 	parser->type_definition_forbidden_message = saved_message;
7271 
7272 	bool saved_greater_than_is_operator_p
7273 	  = parser->greater_than_is_operator_p;
7274 	parser->greater_than_is_operator_p = true;
7275 
7276 	/* And the expression which is being cast.  */
7277 	matching_parens parens;
7278 	parens.require_open (parser);
7279 	expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7280 	cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7281 						   RT_CLOSE_PAREN);
7282 	location_t end_loc = close_paren ?
7283 	  close_paren->location : UNKNOWN_LOCATION;
7284 
7285 	parser->greater_than_is_operator_p
7286 	  = saved_greater_than_is_operator_p;
7287 
7288 	/* Only type conversions to integral or enumeration types
7289 	   can be used in constant-expressions.  */
7290 	if (!cast_valid_in_integral_constant_expression_p (type)
7291 	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7292 	  {
7293 	    postfix_expression = error_mark_node;
7294 	    break;
7295 	  }
7296 
7297 	/* Construct a location e.g. :
7298 	     reinterpret_cast <int *> (expr)
7299 	     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7300 	   ranging from the start of the "*_cast" token to the final closing
7301 	   paren, with the caret at the start.  */
7302 	location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7303 
7304 	switch (keyword)
7305 	  {
7306 	  case RID_DYNCAST:
7307 	    postfix_expression
7308 	      = build_dynamic_cast (cp_cast_loc, type, expression,
7309 				    tf_warning_or_error);
7310 	    break;
7311 	  case RID_STATCAST:
7312 	    postfix_expression
7313 	      = build_static_cast (cp_cast_loc, type, expression,
7314 				   tf_warning_or_error);
7315 	    break;
7316 	  case RID_REINTCAST:
7317 	    postfix_expression
7318 	      = build_reinterpret_cast (cp_cast_loc, type, expression,
7319                                         tf_warning_or_error);
7320 	    break;
7321 	  case RID_CONSTCAST:
7322 	    postfix_expression
7323 	      = build_const_cast (cp_cast_loc, type, expression,
7324 				  tf_warning_or_error);
7325 	    break;
7326 	  default:
7327 	    gcc_unreachable ();
7328 	  }
7329       }
7330       break;
7331 
7332     case RID_TYPEID:
7333       {
7334 	tree type;
7335 	const char *saved_message;
7336 	bool saved_in_type_id_in_expr_p;
7337 
7338 	/* Consume the `typeid' token.  */
7339 	cp_lexer_consume_token (parser->lexer);
7340 	/* Look for the `(' token.  */
7341 	matching_parens parens;
7342 	parens.require_open (parser);
7343 	/* Types cannot be defined in a `typeid' expression.  */
7344 	saved_message = parser->type_definition_forbidden_message;
7345 	parser->type_definition_forbidden_message
7346 	  = G_("types may not be defined in a %<typeid%> expression");
7347 	/* We can't be sure yet whether we're looking at a type-id or an
7348 	   expression.  */
7349 	cp_parser_parse_tentatively (parser);
7350 	/* Try a type-id first.  */
7351 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7352 	parser->in_type_id_in_expr_p = true;
7353 	type = cp_parser_type_id (parser);
7354 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7355 	/* Look for the `)' token.  Otherwise, we can't be sure that
7356 	   we're not looking at an expression: consider `typeid (int
7357 	   (3))', for example.  */
7358 	cp_token *close_paren = parens.require_close (parser);
7359 	/* If all went well, simply lookup the type-id.  */
7360 	if (cp_parser_parse_definitely (parser))
7361 	  postfix_expression = get_typeid (type, tf_warning_or_error);
7362 	/* Otherwise, fall back to the expression variant.  */
7363 	else
7364 	  {
7365 	    tree expression;
7366 
7367 	    /* Look for an expression.  */
7368 	    expression = cp_parser_expression (parser, & idk);
7369 	    /* Compute its typeid.  */
7370 	    postfix_expression = build_typeid (expression, tf_warning_or_error);
7371 	    /* Look for the `)' token.  */
7372 	    close_paren = parens.require_close (parser);
7373 	  }
7374 	/* Restore the saved message.  */
7375 	parser->type_definition_forbidden_message = saved_message;
7376 	/* `typeid' may not appear in an integral constant expression.  */
7377 	if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7378 	  postfix_expression = error_mark_node;
7379 
7380 	/* Construct a location e.g. :
7381 	     typeid (expr)
7382 	     ^~~~~~~~~~~~~
7383 	   ranging from the start of the "typeid" token to the final closing
7384 	   paren, with the caret at the start.  */
7385 	if (close_paren)
7386 	  {
7387 	    location_t typeid_loc
7388 	      = make_location (start_loc, start_loc, close_paren->location);
7389 	    postfix_expression.set_location (typeid_loc);
7390 	    postfix_expression.maybe_add_location_wrapper ();
7391 	  }
7392       }
7393       break;
7394 
7395     case RID_TYPENAME:
7396       {
7397 	tree type;
7398 	/* The syntax permitted here is the same permitted for an
7399 	   elaborated-type-specifier.  */
7400         ++parser->prevent_constrained_type_specifiers;
7401 	type = cp_parser_elaborated_type_specifier (parser,
7402 						    /*is_friend=*/false,
7403 						    /*is_declaration=*/false);
7404         --parser->prevent_constrained_type_specifiers;
7405 	postfix_expression = cp_parser_functional_cast (parser, type);
7406       }
7407       break;
7408 
7409     case RID_ADDRESSOF:
7410     case RID_BUILTIN_SHUFFLE:
7411     case RID_BUILTIN_SHUFFLEVECTOR:
7412     case RID_BUILTIN_LAUNDER:
7413     case RID_BUILTIN_ASSOC_BARRIER:
7414       {
7415 	vec<tree, va_gc> *vec;
7416 
7417 	cp_lexer_consume_token (parser->lexer);
7418 	vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7419 		    /*cast_p=*/false, /*allow_expansion_p=*/true,
7420 		    /*non_constant_p=*/NULL);
7421 	if (vec == NULL)
7422 	  {
7423 	    postfix_expression = error_mark_node;
7424 	    break;
7425 	  }
7426 
7427 	for (tree p : *vec)
7428 	  mark_exp_read (p);
7429 
7430 	switch (keyword)
7431 	  {
7432 	  case RID_ADDRESSOF:
7433 	    if (vec->length () == 1)
7434 	      postfix_expression
7435 		= cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7436 	    else
7437 	      {
7438 		error_at (loc, "wrong number of arguments to "
7439 			       "%<__builtin_addressof%>");
7440 		postfix_expression = error_mark_node;
7441 	      }
7442 	    break;
7443 
7444 	  case RID_BUILTIN_LAUNDER:
7445 	    if (vec->length () == 1)
7446 	      postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7447 							   tf_warning_or_error);
7448 	    else
7449 	      {
7450 		error_at (loc, "wrong number of arguments to "
7451 			       "%<__builtin_launder%>");
7452 		postfix_expression = error_mark_node;
7453 	      }
7454 	    break;
7455 
7456 	  case RID_BUILTIN_ASSOC_BARRIER:
7457 	    if (vec->length () == 1)
7458 	      postfix_expression = build1_loc (loc, PAREN_EXPR,
7459 					       TREE_TYPE ((*vec)[0]),
7460 					       (*vec)[0]);
7461 	    else
7462 	      {
7463 		error_at (loc, "wrong number of arguments to "
7464 			       "%<__builtin_assoc_barrier%>");
7465 		postfix_expression = error_mark_node;
7466 	      }
7467 	    break;
7468 
7469 	  case RID_BUILTIN_SHUFFLE:
7470 	    if (vec->length () == 2)
7471 	      postfix_expression
7472 		= build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7473 					 (*vec)[1], tf_warning_or_error);
7474 	    else if (vec->length () == 3)
7475 	      postfix_expression
7476 		= build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7477 					 (*vec)[2], tf_warning_or_error);
7478 	    else
7479 	      {
7480 		error_at (loc, "wrong number of arguments to "
7481 			       "%<__builtin_shuffle%>");
7482 		postfix_expression = error_mark_node;
7483 	      }
7484 	    break;
7485 
7486 	  case RID_BUILTIN_SHUFFLEVECTOR:
7487 	    if (vec->length () < 3)
7488 	      {
7489 		error_at (loc, "wrong number of arguments to "
7490 			       "%<__builtin_shufflevector%>");
7491 		postfix_expression = error_mark_node;
7492 	      }
7493 	    else
7494 	      {
7495 		postfix_expression
7496 		  = build_x_shufflevector (loc, vec, tf_warning_or_error);
7497 	      }
7498 	    break;
7499 
7500 	  default:
7501 	    gcc_unreachable ();
7502 	  }
7503 	break;
7504       }
7505 
7506     case RID_BUILTIN_CONVERTVECTOR:
7507       {
7508 	tree expression;
7509 	tree type;
7510 	/* Consume the `__builtin_convertvector' token.  */
7511 	cp_lexer_consume_token (parser->lexer);
7512 	/* Look for the opening `('.  */
7513 	matching_parens parens;
7514 	parens.require_open (parser);
7515 	/* Now, parse the assignment-expression.  */
7516 	expression = cp_parser_assignment_expression (parser);
7517 	/* Look for the `,'.  */
7518 	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7519 	location_t type_location
7520 	  = cp_lexer_peek_token (parser->lexer)->location;
7521 	/* Parse the type-id.  */
7522 	{
7523 	  type_id_in_expr_sentinel s (parser);
7524 	  type = cp_parser_type_id (parser);
7525 	}
7526 	/* Look for the closing `)'.  */
7527 	parens.require_close (parser);
7528 	postfix_expression
7529 	  = cp_build_vec_convert (expression, type_location, type,
7530 				  tf_warning_or_error);
7531 	break;
7532       }
7533 
7534     case RID_BUILTIN_BIT_CAST:
7535       {
7536 	tree expression;
7537 	tree type;
7538 	/* Consume the `__builtin_bit_cast' token.  */
7539 	cp_lexer_consume_token (parser->lexer);
7540 	/* Look for the opening `('.  */
7541 	matching_parens parens;
7542 	parens.require_open (parser);
7543 	location_t type_location
7544 	  = cp_lexer_peek_token (parser->lexer)->location;
7545 	/* Parse the type-id.  */
7546 	{
7547 	  type_id_in_expr_sentinel s (parser);
7548 	  type = cp_parser_type_id (parser);
7549 	}
7550 	/* Look for the `,'.  */
7551 	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7552 	/* Now, parse the assignment-expression.  */
7553 	expression = cp_parser_assignment_expression (parser);
7554 	/* Look for the closing `)'.  */
7555 	parens.require_close (parser);
7556 	postfix_expression
7557 	  = cp_build_bit_cast (type_location, type, expression,
7558 			       tf_warning_or_error);
7559 	break;
7560       }
7561 
7562     default:
7563       {
7564 	tree type;
7565 
7566 	/* If the next thing is a simple-type-specifier, we may be
7567 	   looking at a functional cast.  We could also be looking at
7568 	   an id-expression.  So, we try the functional cast, and if
7569 	   that doesn't work we fall back to the primary-expression.  */
7570 	cp_parser_parse_tentatively (parser);
7571 	/* Look for the simple-type-specifier.  */
7572         ++parser->prevent_constrained_type_specifiers;
7573 	type = cp_parser_simple_type_specifier (parser,
7574 						/*decl_specs=*/NULL,
7575 						CP_PARSER_FLAGS_NONE);
7576         --parser->prevent_constrained_type_specifiers;
7577 	/* Parse the cast itself.  */
7578 	if (!cp_parser_error_occurred (parser))
7579 	  postfix_expression
7580 	    = cp_parser_functional_cast (parser, type);
7581 	/* If that worked, we're done.  */
7582 	if (cp_parser_parse_definitely (parser))
7583 	  break;
7584 
7585 	/* If the functional-cast didn't work out, try a
7586 	   compound-literal.  */
7587 	if (cp_parser_allow_gnu_extensions_p (parser)
7588 	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7589 	  {
7590 	    cp_expr initializer = NULL_TREE;
7591 
7592 	    cp_parser_parse_tentatively (parser);
7593 
7594 	    matching_parens parens;
7595 	    parens.consume_open (parser);
7596 
7597 	    /* Avoid calling cp_parser_type_id pointlessly, see comment
7598 	       in cp_parser_cast_expression about c++/29234.  */
7599 	    if (!cp_parser_compound_literal_p (parser))
7600 	      cp_parser_simulate_error (parser);
7601 	    else
7602 	      {
7603 		/* Parse the type.  */
7604 		bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7605 		parser->in_type_id_in_expr_p = true;
7606 		type = cp_parser_type_id (parser);
7607 		parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7608 		parens.require_close (parser);
7609 	      }
7610 
7611 	    /* If things aren't going well, there's no need to
7612 	       keep going.  */
7613 	    if (!cp_parser_error_occurred (parser))
7614 	      {
7615 		bool non_constant_p;
7616 		/* Parse the brace-enclosed initializer list.  */
7617 		initializer = cp_parser_braced_list (parser,
7618 						     &non_constant_p);
7619 	      }
7620 	    /* If that worked, we're definitely looking at a
7621 	       compound-literal expression.  */
7622 	    if (cp_parser_parse_definitely (parser))
7623 	      {
7624 		/* Warn the user that a compound literal is not
7625 		   allowed in standard C++.  */
7626 		pedwarn (input_location, OPT_Wpedantic,
7627 			 "ISO C++ forbids compound-literals");
7628 		/* For simplicity, we disallow compound literals in
7629 		   constant-expressions.  We could
7630 		   allow compound literals of integer type, whose
7631 		   initializer was a constant, in constant
7632 		   expressions.  Permitting that usage, as a further
7633 		   extension, would not change the meaning of any
7634 		   currently accepted programs.  (Of course, as
7635 		   compound literals are not part of ISO C++, the
7636 		   standard has nothing to say.)  */
7637 		if (cp_parser_non_integral_constant_expression (parser,
7638 								NIC_NCC))
7639 		  {
7640 		    postfix_expression = error_mark_node;
7641 		    break;
7642 		  }
7643 		/* Form the representation of the compound-literal.  */
7644 		postfix_expression
7645 		  = finish_compound_literal (type, initializer,
7646 					     tf_warning_or_error, fcl_c99);
7647 		postfix_expression.set_location (initializer.get_location ());
7648 		break;
7649 	      }
7650 	  }
7651 
7652 	/* It must be a primary-expression.  */
7653 	postfix_expression
7654 	  = cp_parser_primary_expression (parser, address_p, cast_p,
7655 					  /*template_arg_p=*/false,
7656 					  decltype_p,
7657 					  &idk);
7658       }
7659       break;
7660     }
7661 
7662   /* Note that we don't need to worry about calling build_cplus_new on a
7663      class-valued CALL_EXPR in decltype when it isn't the end of the
7664      postfix-expression; unary_complex_lvalue will take care of that for
7665      all these cases.  */
7666 
7667   /* Keep looping until the postfix-expression is complete.  */
7668   while (true)
7669     {
7670       if (idk == CP_ID_KIND_UNQUALIFIED
7671 	  && identifier_p (postfix_expression)
7672 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7673 	/* It is not a Koenig lookup function call.  */
7674 	postfix_expression
7675 	  = unqualified_name_lookup_error (postfix_expression);
7676 
7677       /* Peek at the next token.  */
7678       token = cp_lexer_peek_token (parser->lexer);
7679 
7680       switch (token->type)
7681 	{
7682 	case CPP_OPEN_SQUARE:
7683 	  if (cp_next_tokens_can_be_std_attribute_p (parser))
7684 	    {
7685 	      cp_parser_error (parser,
7686 			       "two consecutive %<[%> shall "
7687 			       "only introduce an attribute");
7688 	      return error_mark_node;
7689 	    }
7690 	  postfix_expression
7691 	    = cp_parser_postfix_open_square_expression (parser,
7692 							postfix_expression,
7693 							false,
7694 							decltype_p);
7695 	  postfix_expression.set_range (start_loc,
7696 					postfix_expression.get_location ());
7697 
7698 	  idk = CP_ID_KIND_NONE;
7699           is_member_access = false;
7700 	  break;
7701 
7702 	case CPP_OPEN_PAREN:
7703 	  /* postfix-expression ( expression-list [opt] ) */
7704 	  {
7705 	    bool koenig_p;
7706 	    bool is_builtin_constant_p;
7707 	    bool saved_integral_constant_expression_p = false;
7708 	    bool saved_non_integral_constant_expression_p = false;
7709 	    tsubst_flags_t complain = complain_flags (decltype_p);
7710 	    vec<tree, va_gc> *args;
7711 	    location_t close_paren_loc = UNKNOWN_LOCATION;
7712 	    location_t combined_loc = UNKNOWN_LOCATION;
7713 
7714             is_member_access = false;
7715 
7716 	    tree stripped_expression
7717 	      = tree_strip_any_location_wrapper (postfix_expression);
7718 	    is_builtin_constant_p
7719 	      = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7720 	    if (is_builtin_constant_p)
7721 	      {
7722 		/* The whole point of __builtin_constant_p is to allow
7723 		   non-constant expressions to appear as arguments.  */
7724 		saved_integral_constant_expression_p
7725 		  = parser->integral_constant_expression_p;
7726 		saved_non_integral_constant_expression_p
7727 		  = parser->non_integral_constant_expression_p;
7728 		parser->integral_constant_expression_p = false;
7729 	      }
7730 	    args = (cp_parser_parenthesized_expression_list
7731 		    (parser, non_attr,
7732 		     /*cast_p=*/false, /*allow_expansion_p=*/true,
7733 		     /*non_constant_p=*/NULL,
7734 		     /*close_paren_loc=*/&close_paren_loc,
7735 		     /*wrap_locations_p=*/true));
7736 	    if (is_builtin_constant_p)
7737 	      {
7738 		parser->integral_constant_expression_p
7739 		  = saved_integral_constant_expression_p;
7740 		parser->non_integral_constant_expression_p
7741 		  = saved_non_integral_constant_expression_p;
7742 	      }
7743 
7744 	    if (args == NULL)
7745 	      {
7746 		postfix_expression = error_mark_node;
7747 		break;
7748 	      }
7749 
7750 	    /* Function calls are not permitted in
7751 	       constant-expressions.  */
7752 	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
7753 		&& cp_parser_non_integral_constant_expression (parser,
7754 							       NIC_FUNC_CALL))
7755 	      {
7756 		postfix_expression = error_mark_node;
7757 		release_tree_vector (args);
7758 		break;
7759 	      }
7760 
7761 	    koenig_p = false;
7762 	    if (idk == CP_ID_KIND_UNQUALIFIED
7763 		|| idk == CP_ID_KIND_TEMPLATE_ID)
7764 	      {
7765 		if (identifier_p (postfix_expression)
7766 		    /* In C++20, we may need to perform ADL for a template
7767 		       name.  */
7768 		    || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7769 			&& identifier_p (TREE_OPERAND (postfix_expression, 0))))
7770 		  {
7771 		    if (!args->is_empty ())
7772 		      {
7773 			koenig_p = true;
7774 			if (!any_type_dependent_arguments_p (args))
7775 			  postfix_expression
7776 			    = perform_koenig_lookup (postfix_expression, args,
7777 						     complain);
7778 		      }
7779 		    else
7780 		      postfix_expression
7781 			= unqualified_fn_lookup_error (postfix_expression);
7782 		  }
7783 		/* We do not perform argument-dependent lookup if
7784 		   normal lookup finds a non-function, in accordance
7785 		   with the expected resolution of DR 218.  */
7786 		else if (!args->is_empty ()
7787 			 && is_overloaded_fn (postfix_expression))
7788 		  {
7789 		    /* Do not do argument dependent lookup if regular
7790 		       lookup finds a member function or a block-scope
7791 		       function declaration.  [basic.lookup.argdep]/3  */
7792 		    bool do_adl_p = true;
7793 		    tree fns = get_fns (postfix_expression);
7794 		    for (lkp_iterator iter (fns); iter; ++iter)
7795 		      {
7796 			tree fn = STRIP_TEMPLATE (*iter);
7797 			if ((TREE_CODE (fn) == USING_DECL
7798 			     && DECL_DEPENDENT_P (fn))
7799 			    || DECL_FUNCTION_MEMBER_P (fn)
7800 			    || DECL_LOCAL_DECL_P (fn))
7801 			  {
7802 			    do_adl_p = false;
7803 			    break;
7804 			  }
7805 		      }
7806 
7807 		    if (do_adl_p)
7808 		      {
7809 			koenig_p = true;
7810 			if (!any_type_dependent_arguments_p (args))
7811 			  postfix_expression
7812 			    = perform_koenig_lookup (postfix_expression, args,
7813 						     complain);
7814 		      }
7815 		  }
7816 	      }
7817 
7818 	    /* Temporarily set input_location to the combined location
7819 	       with call expression range, as e.g. build_out_target_exprs
7820 	       called from convert_default_arg relies on input_location,
7821 	       so updating it only when the call is fully built results
7822 	       in inconsistencies between location handling in templates
7823 	       and outside of templates.  */
7824 	    if (close_paren_loc != UNKNOWN_LOCATION)
7825 	      combined_loc = make_location (token->location, start_loc,
7826 					    close_paren_loc);
7827 	    iloc_sentinel ils (combined_loc);
7828 
7829 	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7830 	      {
7831 		tree instance = TREE_OPERAND (postfix_expression, 0);
7832 		tree fn = TREE_OPERAND (postfix_expression, 1);
7833 
7834 		if (processing_template_decl
7835 		    && (type_dependent_object_expression_p (instance)
7836 			|| (!BASELINK_P (fn)
7837 			    && TREE_CODE (fn) != FIELD_DECL)
7838 			|| type_dependent_expression_p (fn)
7839 			|| any_type_dependent_arguments_p (args)))
7840 		  {
7841 		    maybe_generic_this_capture (instance, fn);
7842 		    postfix_expression
7843 		      = build_min_nt_call_vec (postfix_expression, args);
7844 		  }
7845 		else if (BASELINK_P (fn))
7846 		  {
7847 		  postfix_expression
7848 		    = (build_new_method_call
7849 		       (instance, fn, &args, NULL_TREE,
7850 			(idk == CP_ID_KIND_QUALIFIED
7851 			 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7852 			 : LOOKUP_NORMAL),
7853 			/*fn_p=*/NULL,
7854 			complain));
7855 		  }
7856 		else
7857 		  postfix_expression
7858 		    = finish_call_expr (postfix_expression, &args,
7859 					/*disallow_virtual=*/false,
7860 					/*koenig_p=*/false,
7861 					complain);
7862 	      }
7863 	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
7864 		     || TREE_CODE (postfix_expression) == MEMBER_REF
7865 		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7866 	      postfix_expression = (build_offset_ref_call_from_tree
7867 				    (postfix_expression, &args,
7868 				     complain));
7869 	    else if (idk == CP_ID_KIND_QUALIFIED)
7870 	      /* A call to a static class member, or a namespace-scope
7871 		 function.  */
7872 	      postfix_expression
7873 		= finish_call_expr (postfix_expression, &args,
7874 				    /*disallow_virtual=*/true,
7875 				    koenig_p,
7876 				    complain);
7877 	    else
7878 	      /* All other function calls.  */
7879 	      postfix_expression
7880 		= finish_call_expr (postfix_expression, &args,
7881 				    /*disallow_virtual=*/false,
7882 				    koenig_p,
7883 				    complain);
7884 
7885 	    if (close_paren_loc != UNKNOWN_LOCATION)
7886 	      postfix_expression.set_location (combined_loc);
7887 
7888 	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
7889 	    idk = CP_ID_KIND_NONE;
7890 
7891 	    release_tree_vector (args);
7892 	  }
7893 	  break;
7894 
7895 	case CPP_DOT:
7896 	case CPP_DEREF:
7897 	  /* postfix-expression . template [opt] id-expression
7898 	     postfix-expression . pseudo-destructor-name
7899 	     postfix-expression -> template [opt] id-expression
7900 	     postfix-expression -> pseudo-destructor-name */
7901 
7902 	  /* Consume the `.' or `->' operator.  */
7903 	  cp_lexer_consume_token (parser->lexer);
7904 
7905 	  postfix_expression
7906 	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
7907 						      postfix_expression,
7908 						      false, &idk, loc);
7909 
7910           is_member_access = true;
7911 	  break;
7912 
7913 	case CPP_PLUS_PLUS:
7914 	  /* postfix-expression ++  */
7915 	  /* Consume the `++' token.  */
7916 	  cp_lexer_consume_token (parser->lexer);
7917 	  /* Generate a representation for the complete expression.  */
7918 	  postfix_expression
7919 	    = finish_increment_expr (postfix_expression,
7920 				     POSTINCREMENT_EXPR);
7921 	  /* Increments may not appear in constant-expressions.  */
7922 	  if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7923 	    postfix_expression = error_mark_node;
7924 	  idk = CP_ID_KIND_NONE;
7925           is_member_access = false;
7926 	  break;
7927 
7928 	case CPP_MINUS_MINUS:
7929 	  /* postfix-expression -- */
7930 	  /* Consume the `--' token.  */
7931 	  cp_lexer_consume_token (parser->lexer);
7932 	  /* Generate a representation for the complete expression.  */
7933 	  postfix_expression
7934 	    = finish_increment_expr (postfix_expression,
7935 				     POSTDECREMENT_EXPR);
7936 	  /* Decrements may not appear in constant-expressions.  */
7937 	  if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7938 	    postfix_expression = error_mark_node;
7939 	  idk = CP_ID_KIND_NONE;
7940           is_member_access = false;
7941 	  break;
7942 
7943 	default:
7944 	  if (pidk_return != NULL)
7945 	    * pidk_return = idk;
7946           if (member_access_only_p)
7947             return is_member_access
7948               ? postfix_expression
7949               : cp_expr (error_mark_node);
7950           else
7951             return postfix_expression;
7952 	}
7953     }
7954 }
7955 
7956 /* Helper function for cp_parser_parenthesized_expression_list and
7957    cp_parser_postfix_open_square_expression.  Parse a single element
7958    of parenthesized expression list.  */
7959 
7960 static cp_expr
cp_parser_parenthesized_expression_list_elt(cp_parser * parser,bool cast_p,bool allow_expansion_p,bool * non_constant_p)7961 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
7962 					     bool allow_expansion_p,
7963 					     bool *non_constant_p)
7964 {
7965   cp_expr expr (NULL_TREE);
7966   bool expr_non_constant_p;
7967 
7968   /* Parse the next assignment-expression.  */
7969   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7970     {
7971       /* A braced-init-list.  */
7972       cp_lexer_set_source_position (parser->lexer);
7973       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7974       expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7975       if (non_constant_p && expr_non_constant_p)
7976 	*non_constant_p = true;
7977     }
7978   else if (non_constant_p)
7979     {
7980       expr = cp_parser_constant_expression (parser,
7981 					    /*allow_non_constant_p=*/true,
7982 					    &expr_non_constant_p);
7983       if (expr_non_constant_p)
7984 	*non_constant_p = true;
7985     }
7986   else
7987     expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
7988 
7989   /* If we have an ellipsis, then this is an expression expansion.  */
7990   if (allow_expansion_p
7991       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7992     {
7993       /* Consume the `...'.  */
7994       cp_lexer_consume_token (parser->lexer);
7995 
7996       /* Build the argument pack.  */
7997       expr = make_pack_expansion (expr);
7998     }
7999   return expr;
8000 }
8001 
8002 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8003    by cp_parser_builtin_offsetof.  We're looking for
8004 
8005      postfix-expression [ expression ]
8006      postfix-expression [ braced-init-list ] (C++11)
8007      postfix-expression [ expression-list[opt] ] (C++23)
8008 
8009    FOR_OFFSETOF is set if we're being called in that context, which
8010    changes how we deal with integer constant expressions.  */
8011 
8012 static tree
cp_parser_postfix_open_square_expression(cp_parser * parser,tree postfix_expression,bool for_offsetof,bool decltype_p)8013 cp_parser_postfix_open_square_expression (cp_parser *parser,
8014 					  tree postfix_expression,
8015 					  bool for_offsetof,
8016 					  bool decltype_p)
8017 {
8018   tree index = NULL_TREE;
8019   releasing_vec expression_list = NULL;
8020   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8021   bool saved_greater_than_is_operator_p;
8022 
8023   /* Consume the `[' token.  */
8024   cp_lexer_consume_token (parser->lexer);
8025 
8026   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8027   parser->greater_than_is_operator_p = true;
8028 
8029   /* Parse the index expression.  */
8030   /* ??? For offsetof, there is a question of what to allow here.  If
8031      offsetof is not being used in an integral constant expression context,
8032      then we *could* get the right answer by computing the value at runtime.
8033      If we are in an integral constant expression context, then we might
8034      could accept any constant expression; hard to say without analysis.
8035      Rather than open the barn door too wide right away, allow only integer
8036      constant expressions here.  */
8037   if (for_offsetof)
8038     index = cp_parser_constant_expression (parser);
8039   else
8040     {
8041       if (cxx_dialect >= cxx23
8042 	  && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8043 	*&expression_list = make_tree_vector ();
8044       else if (cxx_dialect >= cxx23)
8045 	{
8046 	  while (true)
8047 	    {
8048 	      cp_expr expr
8049 		= cp_parser_parenthesized_expression_list_elt (parser,
8050 							       /*cast_p=*/
8051 							       false,
8052 							       /*allow_exp_p=*/
8053 							       true,
8054 							       /*non_cst_p=*/
8055 							       NULL);
8056 
8057 	      if (expr == error_mark_node)
8058 		index = error_mark_node;
8059 	      else if (expression_list.get () == NULL
8060 		       && !PACK_EXPANSION_P (expr.get_value ()))
8061 		index = expr.get_value ();
8062 	      else
8063 		vec_safe_push (expression_list, expr.get_value ());
8064 
8065 	      /* If the next token isn't a `,', then we are done.  */
8066 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8067 		break;
8068 
8069 	      if (expression_list.get () == NULL && index != error_mark_node)
8070 		{
8071 		  *&expression_list = make_tree_vector_single (index);
8072 		  index = NULL_TREE;
8073 		}
8074 
8075 	      /* Otherwise, consume the `,' and keep going.  */
8076 	      cp_lexer_consume_token (parser->lexer);
8077 	    }
8078 	  if (expression_list.get () && index == error_mark_node)
8079 	    expression_list.release ();
8080 	}
8081       else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8082 	{
8083 	  bool expr_nonconst_p;
8084 	  cp_lexer_set_source_position (parser->lexer);
8085 	  maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8086 	  index = cp_parser_braced_list (parser, &expr_nonconst_p);
8087 	}
8088       else
8089 	index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8090 				      /*decltype_p=*/false,
8091 				      /*warn_comma_p=*/warn_comma_subscript);
8092     }
8093 
8094   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8095 
8096   /* Look for the closing `]'.  */
8097   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8098 
8099   /* Build the ARRAY_REF.  */
8100   postfix_expression = grok_array_decl (loc, postfix_expression,
8101 					index, &expression_list,
8102 					tf_warning_or_error
8103 					| (decltype_p ? tf_decltype : 0));
8104 
8105   /* When not doing offsetof, array references are not permitted in
8106      constant-expressions.  */
8107   if (!for_offsetof
8108       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8109     postfix_expression = error_mark_node;
8110 
8111   return postfix_expression;
8112 }
8113 
8114 /* A subroutine of cp_parser_postfix_dot_deref_expression.  Handle dot
8115    dereference of incomplete type, returns true if error_mark_node should
8116    be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8117    and *DEPENDENT_P.  */
8118 
8119 bool
cp_parser_dot_deref_incomplete(tree * scope,cp_expr * postfix_expression,bool * dependent_p)8120 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8121 				bool *dependent_p)
8122 {
8123   /* In a template, be permissive by treating an object expression
8124      of incomplete type as dependent (after a pedwarn).  */
8125   diagnostic_t kind = (processing_template_decl
8126 		       && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8127 
8128   switch (TREE_CODE (*postfix_expression))
8129     {
8130     case CAST_EXPR:
8131     case REINTERPRET_CAST_EXPR:
8132     case CONST_CAST_EXPR:
8133     case STATIC_CAST_EXPR:
8134     case DYNAMIC_CAST_EXPR:
8135     case IMPLICIT_CONV_EXPR:
8136     case VIEW_CONVERT_EXPR:
8137     case NON_LVALUE_EXPR:
8138       kind = DK_ERROR;
8139       break;
8140     case OVERLOAD:
8141       /* Don't emit any diagnostic for OVERLOADs.  */
8142       kind = DK_IGNORED;
8143       break;
8144     default:
8145       /* Avoid clobbering e.g. DECLs.  */
8146       if (!EXPR_P (*postfix_expression))
8147 	kind = DK_ERROR;
8148       break;
8149     }
8150 
8151   if (kind == DK_IGNORED)
8152     return false;
8153 
8154   location_t exploc = location_of (*postfix_expression);
8155   cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8156   if (!MAYBE_CLASS_TYPE_P (*scope))
8157     return true;
8158   if (kind == DK_ERROR)
8159     *scope = *postfix_expression = error_mark_node;
8160   else if (processing_template_decl)
8161     {
8162       *dependent_p = true;
8163       *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8164     }
8165   return false;
8166 }
8167 
8168 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8169    by cp_parser_builtin_offsetof.  We're looking for
8170 
8171      postfix-expression . template [opt] id-expression
8172      postfix-expression . pseudo-destructor-name
8173      postfix-expression -> template [opt] id-expression
8174      postfix-expression -> pseudo-destructor-name
8175 
8176    FOR_OFFSETOF is set if we're being called in that context.  That sorta
8177    limits what of the above we'll actually accept, but nevermind.
8178    TOKEN_TYPE is the "." or "->" token, which will already have been
8179    removed from the stream.  */
8180 
8181 static tree
cp_parser_postfix_dot_deref_expression(cp_parser * parser,enum cpp_ttype token_type,cp_expr postfix_expression,bool for_offsetof,cp_id_kind * idk,location_t location)8182 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8183 					enum cpp_ttype token_type,
8184 					cp_expr postfix_expression,
8185 					bool for_offsetof, cp_id_kind *idk,
8186 					location_t location)
8187 {
8188   tree name;
8189   bool dependent_p;
8190   bool pseudo_destructor_p;
8191   tree scope = NULL_TREE;
8192   location_t start_loc = postfix_expression.get_start ();
8193 
8194   /* If this is a `->' operator, dereference the pointer.  */
8195   if (token_type == CPP_DEREF)
8196     postfix_expression = build_x_arrow (location, postfix_expression,
8197 					tf_warning_or_error);
8198   /* Check to see whether or not the expression is type-dependent and
8199      not the current instantiation.  */
8200   dependent_p = type_dependent_object_expression_p (postfix_expression);
8201   /* The identifier following the `->' or `.' is not qualified.  */
8202   parser->scope = NULL_TREE;
8203   parser->qualifying_scope = NULL_TREE;
8204   parser->object_scope = NULL_TREE;
8205   *idk = CP_ID_KIND_NONE;
8206 
8207   /* Enter the scope corresponding to the type of the object
8208      given by the POSTFIX_EXPRESSION.  */
8209   if (!dependent_p)
8210     {
8211       scope = TREE_TYPE (postfix_expression);
8212       /* According to the standard, no expression should ever have
8213 	 reference type.  Unfortunately, we do not currently match
8214 	 the standard in this respect in that our internal representation
8215 	 of an expression may have reference type even when the standard
8216 	 says it does not.  Therefore, we have to manually obtain the
8217 	 underlying type here.  */
8218       scope = non_reference (scope);
8219       /* The type of the POSTFIX_EXPRESSION must be complete.  */
8220       /* Unlike the object expression in other contexts, *this is not
8221 	 required to be of complete type for purposes of class member
8222 	 access (5.2.5) outside the member function body.  */
8223       if (postfix_expression != current_class_ref
8224 	  && scope != error_mark_node
8225 	  && !currently_open_class (scope))
8226 	{
8227 	  scope = complete_type (scope);
8228 	  if (!COMPLETE_TYPE_P (scope)
8229 	      && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8230 						 &dependent_p))
8231 	    return error_mark_node;
8232 	}
8233 
8234       if (!dependent_p)
8235 	{
8236 	  /* Let the name lookup machinery know that we are processing a
8237 	     class member access expression.  */
8238 	  parser->context->object_type = scope;
8239 	  /* If something went wrong, we want to be able to discern that case,
8240 	     as opposed to the case where there was no SCOPE due to the type
8241 	     of expression being dependent.  */
8242 	  if (!scope)
8243 	    scope = error_mark_node;
8244 	  /* If the SCOPE was erroneous, make the various semantic analysis
8245 	     functions exit quickly -- and without issuing additional error
8246 	     messages.  */
8247 	  if (scope == error_mark_node)
8248 	    postfix_expression = error_mark_node;
8249 	}
8250     }
8251 
8252   if (dependent_p)
8253     {
8254       tree type = TREE_TYPE (postfix_expression);
8255       /* If we don't have a (type-dependent) object of class type, use
8256 	 typeof to figure out the type of the object.  */
8257       if (type == NULL_TREE || is_auto (type))
8258 	type = finish_typeof (postfix_expression);
8259       parser->context->object_type = type;
8260     }
8261 
8262   /* Assume this expression is not a pseudo-destructor access.  */
8263   pseudo_destructor_p = false;
8264 
8265   /* If the SCOPE is a scalar type, then, if this is a valid program,
8266      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
8267      is type dependent, it can be pseudo-destructor-name or something else.
8268      Try to parse it as pseudo-destructor-name first.  */
8269   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8270     {
8271       tree s;
8272       tree type;
8273 
8274       cp_parser_parse_tentatively (parser);
8275       /* Parse the pseudo-destructor-name.  */
8276       s = NULL_TREE;
8277       cp_parser_pseudo_destructor_name (parser, postfix_expression,
8278 					&s, &type);
8279       if (dependent_p
8280 	  && (cp_parser_error_occurred (parser)
8281 	      || !SCALAR_TYPE_P (type)))
8282 	cp_parser_abort_tentative_parse (parser);
8283       else if (cp_parser_parse_definitely (parser))
8284 	{
8285 	  pseudo_destructor_p = true;
8286 	  postfix_expression
8287 	    = finish_pseudo_destructor_expr (postfix_expression,
8288 					     s, type, location);
8289 	}
8290     }
8291 
8292   if (!pseudo_destructor_p)
8293     {
8294       /* If the SCOPE is not a scalar type, we are looking at an
8295 	 ordinary class member access expression, rather than a
8296 	 pseudo-destructor-name.  */
8297       bool template_p;
8298       cp_token *token = cp_lexer_peek_token (parser->lexer);
8299       /* Parse the id-expression.  */
8300       name = (cp_parser_id_expression
8301 	      (parser,
8302 	       cp_parser_optional_template_keyword (parser),
8303 	       /*check_dependency_p=*/true,
8304 	       &template_p,
8305 	       /*declarator_p=*/false,
8306 	       /*optional_p=*/false));
8307       /* In general, build a SCOPE_REF if the member name is qualified.
8308 	 However, if the name was not dependent and has already been
8309 	 resolved; there is no need to build the SCOPE_REF.  For example;
8310 
8311 	     struct X { void f(); };
8312 	     template <typename T> void f(T* t) { t->X::f(); }
8313 
8314 	 Even though "t" is dependent, "X::f" is not and has been resolved
8315 	 to a BASELINK; there is no need to include scope information.  */
8316 
8317       /* But we do need to remember that there was an explicit scope for
8318 	 virtual function calls.  */
8319       if (parser->scope)
8320 	*idk = CP_ID_KIND_QUALIFIED;
8321 
8322       /* If the name is a template-id that names a type, we will get a
8323 	 TYPE_DECL here.  That is invalid code.  */
8324       if (TREE_CODE (name) == TYPE_DECL)
8325 	{
8326 	  error_at (token->location, "invalid use of %qD", name);
8327 	  postfix_expression = error_mark_node;
8328 	}
8329       else
8330 	{
8331 	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8332 	    {
8333 	      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8334 		{
8335 		  error_at (token->location, "%<%D::%D%> is not a class member",
8336 			    parser->scope, name);
8337 		  postfix_expression = error_mark_node;
8338 		}
8339 	      else
8340 		name = build_qualified_name (/*type=*/NULL_TREE,
8341 					     parser->scope,
8342 					     name,
8343 					     template_p);
8344 	      parser->scope = NULL_TREE;
8345 	      parser->qualifying_scope = NULL_TREE;
8346 	      parser->object_scope = NULL_TREE;
8347 	    }
8348 	  if (parser->scope && name && BASELINK_P (name))
8349 	    adjust_result_of_qualified_name_lookup
8350 	      (name, parser->scope, scope);
8351 	  postfix_expression
8352 	    = finish_class_member_access_expr (postfix_expression, name,
8353 					       template_p,
8354 					       tf_warning_or_error);
8355 	  /* Build a location e.g.:
8356 	       ptr->access_expr
8357 	       ~~~^~~~~~~~~~~~~
8358 	     where the caret is at the deref token, ranging from
8359 	     the start of postfix_expression to the end of the access expr.  */
8360 	  location_t combined_loc
8361 	    = make_location (input_location, start_loc, parser->lexer);
8362 	  protected_set_expr_location (postfix_expression, combined_loc);
8363 	}
8364     }
8365 
8366   /* We no longer need to look up names in the scope of the object on
8367      the left-hand side of the `.' or `->' operator.  */
8368   parser->context->object_type = NULL_TREE;
8369 
8370   /* Outside of offsetof, these operators may not appear in
8371      constant-expressions.  */
8372   if (!for_offsetof
8373       && (cp_parser_non_integral_constant_expression
8374 	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8375     postfix_expression = error_mark_node;
8376 
8377   return postfix_expression;
8378 }
8379 
8380 /* Parse a parenthesized expression-list.
8381 
8382    expression-list:
8383      assignment-expression
8384      expression-list, assignment-expression
8385 
8386    attribute-list:
8387      expression-list
8388      identifier
8389      identifier, expression-list
8390 
8391    CAST_P is true if this expression is the target of a cast.
8392 
8393    ALLOW_EXPANSION_P is true if this expression allows expansion of an
8394    argument pack.
8395 
8396    WRAP_LOCATIONS_P is true if expressions within this list for which
8397    CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8398    their source locations.
8399 
8400    Returns a vector of trees.  Each element is a representation of an
8401    assignment-expression.  NULL is returned if the ( and or ) are
8402    missing.  An empty, but allocated, vector is returned on no
8403    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
8404    if we are parsing an attribute list for an attribute that wants a
8405    plain identifier argument, normal_attr for an attribute that wants
8406    an expression, or non_attr if we aren't parsing an attribute list.  If
8407    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8408    not all of the expressions in the list were constant.
8409    If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8410    will be written to with the location of the closing parenthesis.  If
8411    an error occurs, it may or may not be written to.  */
8412 
8413 static vec<tree, va_gc> *
cp_parser_parenthesized_expression_list(cp_parser * parser,int is_attribute_list,bool cast_p,bool allow_expansion_p,bool * non_constant_p,location_t * close_paren_loc,bool wrap_locations_p)8414 cp_parser_parenthesized_expression_list (cp_parser* parser,
8415 					 int is_attribute_list,
8416 					 bool cast_p,
8417                                          bool allow_expansion_p,
8418 					 bool *non_constant_p,
8419 					 location_t *close_paren_loc,
8420 					 bool wrap_locations_p)
8421 {
8422   vec<tree, va_gc> *expression_list;
8423   tree identifier = NULL_TREE;
8424   bool saved_greater_than_is_operator_p;
8425 
8426   /* Assume all the expressions will be constant.  */
8427   if (non_constant_p)
8428     *non_constant_p = false;
8429 
8430   matching_parens parens;
8431   if (!parens.require_open (parser))
8432     return NULL;
8433 
8434   expression_list = make_tree_vector ();
8435 
8436   /* Within a parenthesized expression, a `>' token is always
8437      the greater-than operator.  */
8438   saved_greater_than_is_operator_p
8439     = parser->greater_than_is_operator_p;
8440   parser->greater_than_is_operator_p = true;
8441 
8442   cp_expr expr (NULL_TREE);
8443 
8444   /* Consume expressions until there are no more.  */
8445   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8446     while (true)
8447       {
8448 	/* At the beginning of attribute lists, check to see if the
8449 	   next token is an identifier.  */
8450 	if (is_attribute_list == id_attr
8451 	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8452 	  {
8453 	    cp_token *token;
8454 
8455 	    /* Consume the identifier.  */
8456 	    token = cp_lexer_consume_token (parser->lexer);
8457 	    /* Save the identifier.  */
8458 	    identifier = token->u.value;
8459 	  }
8460 	else
8461 	  {
8462 	    expr
8463 	      = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8464 							     allow_expansion_p,
8465 							     non_constant_p);
8466 
8467 	    if (wrap_locations_p)
8468 	      expr.maybe_add_location_wrapper ();
8469 
8470 	     /* Add it to the list.  We add error_mark_node
8471 		expressions to the list, so that we can still tell if
8472 		the correct form for a parenthesized expression-list
8473 		is found. That gives better errors.  */
8474 	    vec_safe_push (expression_list, expr.get_value ());
8475 
8476 	    if (expr == error_mark_node)
8477 	      goto skip_comma;
8478 	  }
8479 
8480 	/* After the first item, attribute lists look the same as
8481 	   expression lists.  */
8482 	is_attribute_list = non_attr;
8483 
8484       get_comma:;
8485 	/* If the next token isn't a `,', then we are done.  */
8486 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8487 	  break;
8488 
8489 	/* Otherwise, consume the `,' and keep going.  */
8490 	cp_lexer_consume_token (parser->lexer);
8491       }
8492 
8493   if (close_paren_loc)
8494     *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8495 
8496   if (!parens.require_close (parser))
8497     {
8498       int ending;
8499 
8500     skip_comma:;
8501       /* We try and resync to an unnested comma, as that will give the
8502 	 user better diagnostics.  */
8503       ending = cp_parser_skip_to_closing_parenthesis (parser,
8504 						      /*recovering=*/true,
8505 						      /*or_comma=*/true,
8506 						      /*consume_paren=*/true);
8507       if (ending < 0)
8508 	goto get_comma;
8509       if (!ending)
8510 	{
8511 	  parser->greater_than_is_operator_p
8512 	    = saved_greater_than_is_operator_p;
8513 	  return NULL;
8514 	}
8515     }
8516 
8517   parser->greater_than_is_operator_p
8518     = saved_greater_than_is_operator_p;
8519 
8520   if (identifier)
8521     vec_safe_insert (expression_list, 0, identifier);
8522 
8523   return expression_list;
8524 }
8525 
8526 /* Parse a pseudo-destructor-name.
8527 
8528    pseudo-destructor-name:
8529      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8530      :: [opt] nested-name-specifier template template-id :: ~ type-name
8531      :: [opt] nested-name-specifier [opt] ~ type-name
8532 
8533    If either of the first two productions is used, sets *SCOPE to the
8534    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
8535    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
8536    or ERROR_MARK_NODE if the parse fails.  */
8537 
8538 static void
cp_parser_pseudo_destructor_name(cp_parser * parser,tree object,tree * scope,tree * type)8539 cp_parser_pseudo_destructor_name (cp_parser* parser,
8540 				  tree object,
8541 				  tree* scope,
8542 				  tree* type)
8543 {
8544   bool nested_name_specifier_p;
8545 
8546   /* Handle ~auto.  */
8547   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8548       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8549       && !type_dependent_expression_p (object))
8550     {
8551       if (cxx_dialect < cxx14)
8552 	pedwarn (input_location, OPT_Wc__14_extensions,
8553 		 "%<~auto%> only available with "
8554 		 "%<-std=c++14%> or %<-std=gnu++14%>");
8555       cp_lexer_consume_token (parser->lexer);
8556       cp_lexer_consume_token (parser->lexer);
8557       *scope = NULL_TREE;
8558       *type = TREE_TYPE (object);
8559       return;
8560     }
8561 
8562   /* Assume that things will not work out.  */
8563   *type = error_mark_node;
8564 
8565   /* Look for the optional `::' operator.  */
8566   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8567   /* Look for the optional nested-name-specifier.  */
8568   nested_name_specifier_p
8569     = (cp_parser_nested_name_specifier_opt (parser,
8570 					    /*typename_keyword_p=*/false,
8571 					    /*check_dependency_p=*/true,
8572 					    /*type_p=*/false,
8573 					    /*is_declaration=*/false)
8574        != NULL_TREE);
8575   /* Now, if we saw a nested-name-specifier, we might be doing the
8576      second production.  */
8577   if (nested_name_specifier_p
8578       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8579     {
8580       /* Consume the `template' keyword.  */
8581       cp_lexer_consume_token (parser->lexer);
8582       /* Parse the template-id.  */
8583       cp_parser_template_id (parser,
8584 			     /*template_keyword_p=*/true,
8585 			     /*check_dependency_p=*/false,
8586 			     class_type,
8587 			     /*is_declaration=*/true);
8588       /* Look for the `::' token.  */
8589       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8590     }
8591   /* If the next token is not a `~', then there might be some
8592      additional qualification.  */
8593   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8594     {
8595       /* At this point, we're looking for "type-name :: ~".  The type-name
8596 	 must not be a class-name, since this is a pseudo-destructor.  So,
8597 	 it must be either an enum-name, or a typedef-name -- both of which
8598 	 are just identifiers.  So, we peek ahead to check that the "::"
8599 	 and "~" tokens are present; if they are not, then we can avoid
8600 	 calling type_name.  */
8601       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8602 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8603 	  || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8604 	{
8605 	  cp_parser_error (parser, "non-scalar type");
8606 	  return;
8607 	}
8608 
8609       /* Look for the type-name.  */
8610       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8611       if (*scope == error_mark_node)
8612 	return;
8613 
8614       /* Look for the `::' token.  */
8615       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8616     }
8617   else
8618     *scope = NULL_TREE;
8619 
8620   /* Look for the `~'.  */
8621   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8622 
8623   /* Once we see the ~, this has to be a pseudo-destructor.  */
8624   if (!processing_template_decl && !cp_parser_error_occurred (parser))
8625     cp_parser_commit_to_topmost_tentative_parse (parser);
8626 
8627   /* Look for the type-name again.  We are not responsible for
8628      checking that it matches the first type-name.  */
8629   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8630 }
8631 
8632 /* Parse a unary-expression.
8633 
8634    unary-expression:
8635      postfix-expression
8636      ++ cast-expression
8637      -- cast-expression
8638      await-expression
8639      unary-operator cast-expression
8640      sizeof unary-expression
8641      sizeof ( type-id )
8642      alignof ( type-id )  [C++0x]
8643      new-expression
8644      delete-expression
8645 
8646    GNU Extensions:
8647 
8648    unary-expression:
8649      __extension__ cast-expression
8650      __alignof__ unary-expression
8651      __alignof__ ( type-id )
8652      alignof unary-expression  [C++0x]
8653      __real__ cast-expression
8654      __imag__ cast-expression
8655      && identifier
8656      sizeof ( type-id ) { initializer-list , [opt] }
8657      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8658      __alignof__ ( type-id ) { initializer-list , [opt] }
8659 
8660    ADDRESS_P is true iff the unary-expression is appearing as the
8661    operand of the `&' operator.   CAST_P is true if this expression is
8662    the target of a cast.
8663 
8664    Returns a representation of the expression.  */
8665 
8666 static cp_expr
cp_parser_unary_expression(cp_parser * parser,cp_id_kind * pidk,bool address_p,bool cast_p,bool decltype_p)8667 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8668 			    bool address_p, bool cast_p, bool decltype_p)
8669 {
8670   cp_token *token;
8671   enum tree_code unary_operator;
8672 
8673   /* Peek at the next token.  */
8674   token = cp_lexer_peek_token (parser->lexer);
8675   /* Some keywords give away the kind of expression.  */
8676   if (token->type == CPP_KEYWORD)
8677     {
8678       enum rid keyword = token->keyword;
8679 
8680       switch (keyword)
8681 	{
8682 	case RID_ALIGNOF:
8683 	case RID_SIZEOF:
8684 	  {
8685 	    tree operand, ret;
8686 	    enum tree_code op;
8687 	    location_t start_loc = token->location;
8688 
8689 	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8690 	    bool std_alignof = id_equal (token->u.value, "alignof");
8691 
8692 	    /* Consume the token.  */
8693 	    cp_lexer_consume_token (parser->lexer);
8694 	    /* Parse the operand.  */
8695 	    operand = cp_parser_sizeof_operand (parser, keyword);
8696 
8697 	    /* Construct a location e.g. :
8698               alignof (expr)
8699               ^~~~~~~~~~~~~~
8700               with start == caret at the start of the "alignof"/"sizeof"
8701               token, with the endpoint at the final closing paren.  */
8702 	    location_t compound_loc
8703 	      = make_location (start_loc, start_loc, parser->lexer);
8704 
8705 	    if (TYPE_P (operand))
8706 	      ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8707 						std_alignof, true);
8708 	    else
8709 	      {
8710 		/* ISO C++ defines alignof only with types, not with
8711 		   expressions. So pedwarn if alignof is used with a non-
8712 		   type expression. However, __alignof__ is ok.  */
8713 		if (std_alignof)
8714 		  pedwarn (token->location, OPT_Wpedantic,
8715 			   "ISO C++ does not allow %<alignof%> "
8716 			   "with a non-type");
8717 
8718 		ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8719 						  std_alignof, true);
8720 	      }
8721 	    /* For SIZEOF_EXPR, just issue diagnostics, but keep
8722 	       SIZEOF_EXPR with the original operand.  */
8723 	    if (op == SIZEOF_EXPR && ret != error_mark_node)
8724 	      {
8725 		if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8726 		  {
8727 		    if (!processing_template_decl && TYPE_P (operand))
8728 		      {
8729 			ret = build_min (SIZEOF_EXPR, size_type_node,
8730 					 build1 (NOP_EXPR, operand,
8731 						 error_mark_node));
8732 			SIZEOF_EXPR_TYPE_P (ret) = 1;
8733 		      }
8734 		    else
8735 		      ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8736 		    TREE_SIDE_EFFECTS (ret) = 0;
8737 		    TREE_READONLY (ret) = 1;
8738 		    SET_EXPR_LOCATION (ret, compound_loc);
8739 		  }
8740 	      }
8741 
8742 	    cp_expr ret_expr (ret, compound_loc);
8743 	    ret_expr = ret_expr.maybe_add_location_wrapper ();
8744 	    return ret_expr;
8745 	  }
8746 
8747 	case RID_BUILTIN_HAS_ATTRIBUTE:
8748 	  return cp_parser_has_attribute_expression (parser);
8749 
8750 	case RID_NEW:
8751 	  return cp_parser_new_expression (parser);
8752 
8753 	case RID_DELETE:
8754 	  return cp_parser_delete_expression (parser);
8755 
8756 	case RID_EXTENSION:
8757 	  {
8758 	    /* The saved value of the PEDANTIC flag.  */
8759 	    int saved_pedantic;
8760 	    tree expr;
8761 
8762 	    /* Save away the PEDANTIC flag.  */
8763 	    cp_parser_extension_opt (parser, &saved_pedantic);
8764 	    /* Parse the cast-expression.  */
8765 	    expr = cp_parser_simple_cast_expression (parser);
8766 	    /* Restore the PEDANTIC flag.  */
8767 	    pedantic = saved_pedantic;
8768 
8769 	    return expr;
8770 	  }
8771 
8772 	case RID_REALPART:
8773 	case RID_IMAGPART:
8774 	  {
8775 	    tree expression;
8776 
8777 	    /* Consume the `__real__' or `__imag__' token.  */
8778 	    cp_lexer_consume_token (parser->lexer);
8779 	    /* Parse the cast-expression.  */
8780 	    expression = cp_parser_simple_cast_expression (parser);
8781 	    /* Create the complete representation.  */
8782 	    return build_x_unary_op (token->location,
8783 				     (keyword == RID_REALPART
8784 				      ? REALPART_EXPR : IMAGPART_EXPR),
8785 				     expression, NULL_TREE,
8786                                      tf_warning_or_error);
8787 	  }
8788 	  break;
8789 
8790 	case RID_TRANSACTION_ATOMIC:
8791 	case RID_TRANSACTION_RELAXED:
8792 	  return cp_parser_transaction_expression (parser, keyword);
8793 
8794 	case RID_NOEXCEPT:
8795 	  {
8796 	    tree expr;
8797 	    const char *saved_message;
8798 	    bool saved_integral_constant_expression_p;
8799 	    bool saved_non_integral_constant_expression_p;
8800 	    bool saved_greater_than_is_operator_p;
8801 
8802 	    location_t start_loc = token->location;
8803 
8804 	    cp_lexer_consume_token (parser->lexer);
8805 	    matching_parens parens;
8806 	    parens.require_open (parser);
8807 
8808 	    saved_message = parser->type_definition_forbidden_message;
8809 	    parser->type_definition_forbidden_message
8810 	      = G_("types may not be defined in %<noexcept%> expressions");
8811 
8812 	    saved_integral_constant_expression_p
8813 	      = parser->integral_constant_expression_p;
8814 	    saved_non_integral_constant_expression_p
8815 	      = parser->non_integral_constant_expression_p;
8816 	    parser->integral_constant_expression_p = false;
8817 
8818 	    saved_greater_than_is_operator_p
8819 	      = parser->greater_than_is_operator_p;
8820 	    parser->greater_than_is_operator_p = true;
8821 
8822 	    ++cp_unevaluated_operand;
8823 	    ++c_inhibit_evaluation_warnings;
8824 	    ++cp_noexcept_operand;
8825 	    expr = cp_parser_expression (parser);
8826 	    --cp_noexcept_operand;
8827 	    --c_inhibit_evaluation_warnings;
8828 	    --cp_unevaluated_operand;
8829 
8830 	    parser->greater_than_is_operator_p
8831 	      = saved_greater_than_is_operator_p;
8832 
8833 	    parser->integral_constant_expression_p
8834 	      = saved_integral_constant_expression_p;
8835 	    parser->non_integral_constant_expression_p
8836 	      = saved_non_integral_constant_expression_p;
8837 
8838 	    parser->type_definition_forbidden_message = saved_message;
8839 
8840 	    parens.require_close (parser);
8841 
8842 	    /* Construct a location of the form:
8843 	       noexcept (expr)
8844 	       ^~~~~~~~~~~~~~~
8845 	       with start == caret, finishing at the close-paren.  */
8846 	    location_t noexcept_loc
8847 	      = make_location (start_loc, start_loc, parser->lexer);
8848 
8849 	    return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8850 			    noexcept_loc);
8851 	  }
8852 
8853 	case RID_CO_AWAIT:
8854 	  {
8855 	    tree expr;
8856 	    location_t kw_loc = token->location;
8857 
8858 	    /* Consume the `co_await' token.  */
8859 	    cp_lexer_consume_token (parser->lexer);
8860 	    /* Parse its cast-expression.  */
8861 	    expr = cp_parser_simple_cast_expression (parser);
8862 	    if (expr == error_mark_node)
8863 	      return error_mark_node;
8864 
8865 	    /* Handle [expr.await].  */
8866 	    return cp_expr (finish_co_await_expr (kw_loc, expr));
8867 	  }
8868 
8869 	default:
8870 	  break;
8871 	}
8872     }
8873 
8874   /* Look for the `:: new' and `:: delete', which also signal the
8875      beginning of a new-expression, or delete-expression,
8876      respectively.  If the next token is `::', then it might be one of
8877      these.  */
8878   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8879     {
8880       enum rid keyword;
8881 
8882       /* See if the token after the `::' is one of the keywords in
8883 	 which we're interested.  */
8884       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8885       /* If it's `new', we have a new-expression.  */
8886       if (keyword == RID_NEW)
8887 	return cp_parser_new_expression (parser);
8888       /* Similarly, for `delete'.  */
8889       else if (keyword == RID_DELETE)
8890 	return cp_parser_delete_expression (parser);
8891     }
8892 
8893   /* Look for a unary operator.  */
8894   unary_operator = cp_parser_unary_operator (token);
8895   /* The `++' and `--' operators can be handled similarly, even though
8896      they are not technically unary-operators in the grammar.  */
8897   if (unary_operator == ERROR_MARK)
8898     {
8899       if (token->type == CPP_PLUS_PLUS)
8900 	unary_operator = PREINCREMENT_EXPR;
8901       else if (token->type == CPP_MINUS_MINUS)
8902 	unary_operator = PREDECREMENT_EXPR;
8903       /* Handle the GNU address-of-label extension.  */
8904       else if (cp_parser_allow_gnu_extensions_p (parser)
8905 	       && token->type == CPP_AND_AND)
8906 	{
8907 	  tree identifier;
8908 	  tree expression;
8909 	  location_t start_loc = token->location;
8910 
8911 	  /* Consume the '&&' token.  */
8912 	  cp_lexer_consume_token (parser->lexer);
8913 	  /* Look for the identifier.  */
8914 	  identifier = cp_parser_identifier (parser);
8915 	  /* Construct a location of the form:
8916 	       &&label
8917 	       ^~~~~~~
8918 	     with caret==start at the "&&", finish at the end of the label.  */
8919 	  location_t combined_loc
8920 	    = make_location (start_loc, start_loc, parser->lexer);
8921 	  /* Create an expression representing the address.  */
8922 	  expression = finish_label_address_expr (identifier, combined_loc);
8923 	  if (cp_parser_non_integral_constant_expression (parser,
8924 							  NIC_ADDR_LABEL))
8925 	    expression = error_mark_node;
8926 	  return expression;
8927 	}
8928     }
8929   if (unary_operator != ERROR_MARK)
8930     {
8931       cp_expr cast_expression;
8932       cp_expr expression = error_mark_node;
8933       non_integral_constant non_constant_p = NIC_NONE;
8934       location_t loc = token->location;
8935       tsubst_flags_t complain = complain_flags (decltype_p);
8936 
8937       /* Consume the operator token.  */
8938       token = cp_lexer_consume_token (parser->lexer);
8939       enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8940 
8941       /* Parse the cast-expression.  */
8942       cast_expression
8943 	= cp_parser_cast_expression (parser,
8944 				     unary_operator == ADDR_EXPR,
8945 				     /*cast_p=*/false,
8946 				     /*decltype*/false,
8947 				     pidk);
8948 
8949       /* Make a location:
8950 	    OP_TOKEN  CAST_EXPRESSION
8951 	    ^~~~~~~~~~~~~~~~~~~~~~~~~
8952 	 with start==caret at the operator token, and
8953 	 extending to the end of the cast_expression.  */
8954       loc = make_location (loc, loc, cast_expression.get_finish ());
8955 
8956       /* Now, build an appropriate representation.  */
8957       switch (unary_operator)
8958 	{
8959 	case INDIRECT_REF:
8960 	  non_constant_p = NIC_STAR;
8961 	  expression = build_x_indirect_ref (loc, cast_expression,
8962 					     RO_UNARY_STAR, NULL_TREE,
8963                                              complain);
8964           /* TODO: build_x_indirect_ref does not always honor the
8965              location, so ensure it is set.  */
8966           expression.set_location (loc);
8967 	  break;
8968 
8969 	case ADDR_EXPR:
8970 	   non_constant_p = NIC_ADDR;
8971 	  /* Fall through.  */
8972 	case BIT_NOT_EXPR:
8973 	  expression = build_x_unary_op (loc, unary_operator,
8974 					 cast_expression,
8975 					 NULL_TREE, complain);
8976           /* TODO: build_x_unary_op does not always honor the location,
8977              so ensure it is set.  */
8978           expression.set_location (loc);
8979 	  break;
8980 
8981 	case PREINCREMENT_EXPR:
8982 	case PREDECREMENT_EXPR:
8983 	  non_constant_p = unary_operator == PREINCREMENT_EXPR
8984 			   ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8985 	  /* Fall through.  */
8986 	case NEGATE_EXPR:
8987 	  /* Immediately fold negation of a constant, unless the constant is 0
8988 	     (since -0 == 0) or it would overflow.  */
8989 	  if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8990 	    {
8991 	      tree stripped_expr
8992 		= tree_strip_any_location_wrapper (cast_expression);
8993 	      if (CONSTANT_CLASS_P (stripped_expr)
8994 		  && !integer_zerop (stripped_expr)
8995 		  && !TREE_OVERFLOW (stripped_expr))
8996 		{
8997 		  tree folded = fold_build1 (unary_operator,
8998 					     TREE_TYPE (stripped_expr),
8999 					     stripped_expr);
9000 		  if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9001 		    {
9002 		      expression = maybe_wrap_with_location (folded, loc);
9003 		      break;
9004 		    }
9005 		}
9006 	    }
9007 	  /* Fall through.  */
9008 	case UNARY_PLUS_EXPR:
9009 	case TRUTH_NOT_EXPR:
9010 	  expression = finish_unary_op_expr (loc, unary_operator,
9011 					     cast_expression, complain);
9012 	  break;
9013 
9014 	default:
9015 	  gcc_unreachable ();
9016 	}
9017 
9018       if (non_constant_p != NIC_NONE
9019 	  && cp_parser_non_integral_constant_expression (parser,
9020 							 non_constant_p))
9021 	expression = error_mark_node;
9022 
9023       return expression;
9024     }
9025 
9026   return cp_parser_postfix_expression (parser, address_p, cast_p,
9027                                        /*member_access_only_p=*/false,
9028 				       decltype_p,
9029 				       pidk);
9030 }
9031 
9032 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
9033    unary-operator, the corresponding tree code is returned.  */
9034 
9035 static enum tree_code
cp_parser_unary_operator(cp_token * token)9036 cp_parser_unary_operator (cp_token* token)
9037 {
9038   switch (token->type)
9039     {
9040     case CPP_MULT:
9041       return INDIRECT_REF;
9042 
9043     case CPP_AND:
9044       return ADDR_EXPR;
9045 
9046     case CPP_PLUS:
9047       return UNARY_PLUS_EXPR;
9048 
9049     case CPP_MINUS:
9050       return NEGATE_EXPR;
9051 
9052     case CPP_NOT:
9053       return TRUTH_NOT_EXPR;
9054 
9055     case CPP_COMPL:
9056       return BIT_NOT_EXPR;
9057 
9058     default:
9059       return ERROR_MARK;
9060     }
9061 }
9062 
9063 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9064    Returns a representation of the expression.  */
9065 
9066 static tree
cp_parser_has_attribute_expression(cp_parser * parser)9067 cp_parser_has_attribute_expression (cp_parser *parser)
9068 {
9069   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9070 
9071   /* Consume the __builtin_has_attribute token.  */
9072   cp_lexer_consume_token (parser->lexer);
9073 
9074   matching_parens parens;
9075   if (!parens.require_open (parser))
9076     return error_mark_node;
9077 
9078   /* Types cannot be defined in a `sizeof' expression.  Save away the
9079      old message.  */
9080   const char *saved_message = parser->type_definition_forbidden_message;
9081   const char *saved_message_arg
9082     = parser->type_definition_forbidden_message_arg;
9083   parser->type_definition_forbidden_message
9084     = G_("types may not be defined in %qs expressions");
9085   parser->type_definition_forbidden_message_arg
9086     = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9087 
9088   /* The restrictions on constant-expressions do not apply inside
9089      sizeof expressions.  */
9090   bool saved_integral_constant_expression_p
9091     = parser->integral_constant_expression_p;
9092   bool saved_non_integral_constant_expression_p
9093     = parser->non_integral_constant_expression_p;
9094   parser->integral_constant_expression_p = false;
9095 
9096   /* Do not actually evaluate the expression.  */
9097   ++cp_unevaluated_operand;
9098   ++c_inhibit_evaluation_warnings;
9099 
9100   tree oper = NULL_TREE;
9101 
9102   /* We can't be sure yet whether we're looking at a type-id or an
9103      expression.  */
9104   cp_parser_parse_tentatively (parser);
9105 
9106   bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9107   parser->in_type_id_in_expr_p = true;
9108   /* Look for the type-id.  */
9109   oper = cp_parser_type_id (parser);
9110   parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9111 
9112   cp_parser_parse_definitely (parser);
9113 
9114   /* If the type-id production did not work out, then we must be
9115      looking at an expression.  */
9116   if (!oper || oper == error_mark_node)
9117     oper = cp_parser_assignment_expression (parser);
9118 
9119   STRIP_ANY_LOCATION_WRAPPER (oper);
9120 
9121   /* Go back to evaluating expressions.  */
9122   --cp_unevaluated_operand;
9123   --c_inhibit_evaluation_warnings;
9124 
9125   /* And restore the old one.  */
9126   parser->type_definition_forbidden_message = saved_message;
9127   parser->type_definition_forbidden_message_arg = saved_message_arg;
9128   parser->integral_constant_expression_p
9129     = saved_integral_constant_expression_p;
9130   parser->non_integral_constant_expression_p
9131     = saved_non_integral_constant_expression_p;
9132 
9133   /* Consume the comma if it's there.  */
9134   if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9135     {
9136       cp_parser_skip_to_closing_parenthesis (parser, false, false,
9137 					     /*consume_paren=*/true);
9138       return error_mark_node;
9139     }
9140 
9141   /* Parse the attribute specification.  */
9142   bool ret = false;
9143   location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9144   if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9145     {
9146       if (oper == error_mark_node)
9147 	/* Nothing.  */;
9148       else if (processing_template_decl && uses_template_parms (oper))
9149 	sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9150 		  "not supported yet");
9151       else
9152 	{
9153 	  /* Fold constant expressions used in attributes first.  */
9154 	  cp_check_const_attributes (attr);
9155 
9156 	  /* Finally, see if OPER has been declared with ATTR.  */
9157 	  ret = has_attribute (atloc, oper, attr, default_conversion);
9158 	}
9159 
9160       parens.require_close (parser);
9161     }
9162   else
9163     {
9164       error_at (atloc, "expected identifier");
9165       cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9166     }
9167 
9168   /* Construct a location e.g. :
9169      __builtin_has_attribute (oper, attr)
9170      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9171      with start == caret at the start of the built-in token,
9172      and with the endpoint at the final closing paren.  */
9173   location_t compound_loc
9174     = make_location (start_loc, start_loc, parser->lexer);
9175 
9176   cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9177   ret_expr.set_location (compound_loc);
9178   ret_expr = ret_expr.maybe_add_location_wrapper ();
9179   return ret_expr;
9180 }
9181 
9182 /* Parse a new-expression.
9183 
9184    new-expression:
9185      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9186      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9187 
9188    Returns a representation of the expression.  */
9189 
9190 static tree
cp_parser_new_expression(cp_parser * parser)9191 cp_parser_new_expression (cp_parser* parser)
9192 {
9193   bool global_scope_p;
9194   vec<tree, va_gc> *placement;
9195   tree type;
9196   vec<tree, va_gc> *initializer;
9197   tree nelts = NULL_TREE;
9198   tree ret;
9199 
9200   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9201 
9202   /* Look for the optional `::' operator.  */
9203   global_scope_p
9204     = (cp_parser_global_scope_opt (parser,
9205 				   /*current_scope_valid_p=*/false)
9206        != NULL_TREE);
9207   /* Look for the `new' operator.  */
9208   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9209   /* There's no easy way to tell a new-placement from the
9210      `( type-id )' construct.  */
9211   cp_parser_parse_tentatively (parser);
9212   /* Look for a new-placement.  */
9213   placement = cp_parser_new_placement (parser);
9214   /* If that didn't work out, there's no new-placement.  */
9215   if (!cp_parser_parse_definitely (parser))
9216     {
9217       if (placement != NULL)
9218 	release_tree_vector (placement);
9219       placement = NULL;
9220     }
9221 
9222   /* If the next token is a `(', then we have a parenthesized
9223      type-id.  */
9224   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9225     {
9226       cp_token *token;
9227       const char *saved_message = parser->type_definition_forbidden_message;
9228 
9229       /* Consume the `('.  */
9230       matching_parens parens;
9231       parens.consume_open (parser);
9232 
9233       /* Parse the type-id.  */
9234       parser->type_definition_forbidden_message
9235 	= G_("types may not be defined in a new-expression");
9236       {
9237 	type_id_in_expr_sentinel s (parser);
9238 	type = cp_parser_type_id (parser);
9239       }
9240       parser->type_definition_forbidden_message = saved_message;
9241 
9242       /* Look for the closing `)'.  */
9243       parens.require_close (parser);
9244       token = cp_lexer_peek_token (parser->lexer);
9245       /* There should not be a direct-new-declarator in this production,
9246 	 but GCC used to allowed this, so we check and emit a sensible error
9247 	 message for this case.  */
9248       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9249 	{
9250 	  error_at (token->location,
9251 		    "array bound forbidden after parenthesized type-id");
9252 	  inform (token->location,
9253 		  "try removing the parentheses around the type-id");
9254 	  cp_parser_direct_new_declarator (parser);
9255 	}
9256     }
9257   /* Otherwise, there must be a new-type-id.  */
9258   else
9259     type = cp_parser_new_type_id (parser, &nelts);
9260 
9261   /* If the next token is a `(' or '{', then we have a new-initializer.  */
9262   cp_token *token = cp_lexer_peek_token (parser->lexer);
9263   if (token->type == CPP_OPEN_PAREN
9264       || token->type == CPP_OPEN_BRACE)
9265     initializer = cp_parser_new_initializer (parser);
9266   else
9267     initializer = NULL;
9268 
9269   /* A new-expression may not appear in an integral constant
9270      expression.  */
9271   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9272     ret = error_mark_node;
9273   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9274      of a new-type-id or type-id of a new-expression, the new-expression shall
9275      contain a new-initializer of the form ( assignment-expression )".
9276      Additionally, consistently with the spirit of DR 1467, we want to accept
9277      'new auto { 2 }' too.  */
9278   else if ((ret = type_uses_auto (type))
9279 	   && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9280 	   && (vec_safe_length (initializer) != 1
9281 	       || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9282 		   && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9283     {
9284       error_at (token->location,
9285 		"initialization of new-expression for type %<auto%> "
9286 		"requires exactly one element");
9287       ret = error_mark_node;
9288     }
9289   else
9290     {
9291       /* Construct a location e.g.:
9292            ptr = new int[100]
9293                  ^~~~~~~~~~~~
9294          with caret == start at the start of the "new" token, and the end
9295          at the end of the final token we consumed.  */
9296       location_t combined_loc = make_location (start_loc, start_loc,
9297 					       parser->lexer);
9298       /* Create a representation of the new-expression.  */
9299       ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9300 		       global_scope_p, tf_warning_or_error);
9301     }
9302 
9303   if (placement != NULL)
9304     release_tree_vector (placement);
9305   if (initializer != NULL)
9306     release_tree_vector (initializer);
9307 
9308   return ret;
9309 }
9310 
9311 /* Parse a new-placement.
9312 
9313    new-placement:
9314      ( expression-list )
9315 
9316    Returns the same representation as for an expression-list.  */
9317 
9318 static vec<tree, va_gc> *
cp_parser_new_placement(cp_parser * parser)9319 cp_parser_new_placement (cp_parser* parser)
9320 {
9321   vec<tree, va_gc> *expression_list;
9322 
9323   /* Parse the expression-list.  */
9324   expression_list = (cp_parser_parenthesized_expression_list
9325 		     (parser, non_attr, /*cast_p=*/false,
9326 		      /*allow_expansion_p=*/true,
9327 		      /*non_constant_p=*/NULL));
9328 
9329   if (expression_list && expression_list->is_empty ())
9330     error ("expected expression-list or type-id");
9331 
9332   return expression_list;
9333 }
9334 
9335 /* Parse a new-type-id.
9336 
9337    new-type-id:
9338      type-specifier-seq new-declarator [opt]
9339 
9340    Returns the TYPE allocated.  If the new-type-id indicates an array
9341    type, *NELTS is set to the number of elements in the last array
9342    bound; the TYPE will not include the last array bound.  */
9343 
9344 static tree
cp_parser_new_type_id(cp_parser * parser,tree * nelts)9345 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9346 {
9347   cp_decl_specifier_seq type_specifier_seq;
9348   cp_declarator *new_declarator;
9349   cp_declarator *declarator;
9350   cp_declarator *outer_declarator;
9351   const char *saved_message;
9352 
9353   /* The type-specifier sequence must not contain type definitions.
9354      (It cannot contain declarations of new types either, but if they
9355      are not definitions we will catch that because they are not
9356      complete.)  */
9357   saved_message = parser->type_definition_forbidden_message;
9358   parser->type_definition_forbidden_message
9359     = G_("types may not be defined in a new-type-id");
9360   /* Parse the type-specifier-seq.  */
9361   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9362 				/*is_declaration=*/false,
9363 				/*is_trailing_return=*/false,
9364 				&type_specifier_seq);
9365   /* Restore the old message.  */
9366   parser->type_definition_forbidden_message = saved_message;
9367 
9368   if (type_specifier_seq.type == error_mark_node)
9369     return error_mark_node;
9370 
9371   /* Parse the new-declarator.  */
9372   new_declarator = cp_parser_new_declarator_opt (parser);
9373 
9374   /* Determine the number of elements in the last array dimension, if
9375      any.  */
9376   *nelts = NULL_TREE;
9377   /* Skip down to the last array dimension.  */
9378   declarator = new_declarator;
9379   outer_declarator = NULL;
9380   while (declarator && (declarator->kind == cdk_pointer
9381 			|| declarator->kind == cdk_ptrmem))
9382     {
9383       outer_declarator = declarator;
9384       declarator = declarator->declarator;
9385     }
9386   while (declarator
9387 	 && declarator->kind == cdk_array
9388 	 && declarator->declarator
9389 	 && declarator->declarator->kind == cdk_array)
9390     {
9391       outer_declarator = declarator;
9392       declarator = declarator->declarator;
9393     }
9394 
9395   if (declarator && declarator->kind == cdk_array)
9396     {
9397       *nelts = declarator->u.array.bounds;
9398       if (*nelts == error_mark_node)
9399 	*nelts = integer_one_node;
9400 
9401       if (*nelts == NULL_TREE)
9402 	/* Leave [] in the declarator.  */;
9403       else if (outer_declarator)
9404 	outer_declarator->declarator = declarator->declarator;
9405       else
9406 	new_declarator = NULL;
9407     }
9408 
9409   return groktypename (&type_specifier_seq, new_declarator, false);
9410 }
9411 
9412 /* Parse an (optional) new-declarator.
9413 
9414    new-declarator:
9415      ptr-operator new-declarator [opt]
9416      direct-new-declarator
9417 
9418    Returns the declarator.  */
9419 
9420 static cp_declarator *
cp_parser_new_declarator_opt(cp_parser * parser)9421 cp_parser_new_declarator_opt (cp_parser* parser)
9422 {
9423   enum tree_code code;
9424   tree type, std_attributes = NULL_TREE;
9425   cp_cv_quals cv_quals;
9426 
9427   /* We don't know if there's a ptr-operator next, or not.  */
9428   cp_parser_parse_tentatively (parser);
9429   /* Look for a ptr-operator.  */
9430   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9431   /* If that worked, look for more new-declarators.  */
9432   if (cp_parser_parse_definitely (parser))
9433     {
9434       cp_declarator *declarator;
9435 
9436       /* Parse another optional declarator.  */
9437       declarator = cp_parser_new_declarator_opt (parser);
9438 
9439       declarator = cp_parser_make_indirect_declarator
9440 	(code, type, cv_quals, declarator, std_attributes);
9441 
9442       return declarator;
9443     }
9444 
9445   /* If the next token is a `[', there is a direct-new-declarator.  */
9446   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9447     return cp_parser_direct_new_declarator (parser);
9448 
9449   return NULL;
9450 }
9451 
9452 /* Parse a direct-new-declarator.
9453 
9454    direct-new-declarator:
9455      [ expression ]
9456      direct-new-declarator [constant-expression]
9457 
9458    */
9459 
9460 static cp_declarator *
cp_parser_direct_new_declarator(cp_parser * parser)9461 cp_parser_direct_new_declarator (cp_parser* parser)
9462 {
9463   cp_declarator *declarator = NULL;
9464   bool first_p = true;
9465 
9466   while (true)
9467     {
9468       tree expression;
9469       cp_token *token;
9470 
9471       /* Look for the opening `['.  */
9472       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9473 
9474       token = cp_lexer_peek_token (parser->lexer);
9475       if (token->type == CPP_CLOSE_SQUARE && first_p)
9476 	expression = NULL_TREE;
9477       else
9478 	expression = cp_parser_expression (parser);
9479       /* The standard requires that the expression have integral
9480 	 type.  DR 74 adds enumeration types.  We believe that the
9481 	 real intent is that these expressions be handled like the
9482 	 expression in a `switch' condition, which also allows
9483 	 classes with a single conversion to integral or
9484 	 enumeration type.  */
9485       if (expression && !processing_template_decl)
9486 	{
9487 	  expression
9488 	    = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9489 					  expression,
9490 					  /*complain=*/true);
9491 	  if (!expression)
9492 	    {
9493 	      error_at (token->location,
9494 			"expression in new-declarator must have integral "
9495 			"or enumeration type");
9496 	      expression = error_mark_node;
9497 	    }
9498 	}
9499 
9500       /* Look for the closing `]'.  */
9501       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9502 
9503       /* Add this bound to the declarator.  */
9504       declarator = make_array_declarator (declarator, expression);
9505 
9506       /* If the next token is not a `[', then there are no more
9507 	 bounds.  */
9508       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9509 	break;
9510       first_p = false;
9511     }
9512 
9513   return declarator;
9514 }
9515 
9516 /* Parse a new-initializer.
9517 
9518    new-initializer:
9519      ( expression-list [opt] )
9520      braced-init-list
9521 
9522    Returns a representation of the expression-list.  */
9523 
9524 static vec<tree, va_gc> *
cp_parser_new_initializer(cp_parser * parser)9525 cp_parser_new_initializer (cp_parser* parser)
9526 {
9527   vec<tree, va_gc> *expression_list;
9528 
9529   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9530     {
9531       tree t;
9532       bool expr_non_constant_p;
9533       cp_lexer_set_source_position (parser->lexer);
9534       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9535       t = cp_parser_braced_list (parser, &expr_non_constant_p);
9536       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9537       expression_list = make_tree_vector_single (t);
9538     }
9539   else
9540     expression_list = (cp_parser_parenthesized_expression_list
9541 		       (parser, non_attr, /*cast_p=*/false,
9542 			/*allow_expansion_p=*/true,
9543 			/*non_constant_p=*/NULL));
9544 
9545   return expression_list;
9546 }
9547 
9548 /* Parse a delete-expression.
9549 
9550    delete-expression:
9551      :: [opt] delete cast-expression
9552      :: [opt] delete [ ] cast-expression
9553 
9554    Returns a representation of the expression.  */
9555 
9556 static tree
cp_parser_delete_expression(cp_parser * parser)9557 cp_parser_delete_expression (cp_parser* parser)
9558 {
9559   bool global_scope_p;
9560   bool array_p;
9561   tree expression;
9562   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9563 
9564   /* Look for the optional `::' operator.  */
9565   global_scope_p
9566     = (cp_parser_global_scope_opt (parser,
9567 				   /*current_scope_valid_p=*/false)
9568        != NULL_TREE);
9569   /* Look for the `delete' keyword.  */
9570   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9571   /* See if the array syntax is in use.  */
9572   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9573     {
9574       /* Consume the `[' token.  */
9575       cp_lexer_consume_token (parser->lexer);
9576       /* Look for the `]' token.  */
9577       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9578       /* Remember that this is the `[]' construct.  */
9579       array_p = true;
9580     }
9581   else
9582     array_p = false;
9583 
9584   /* Parse the cast-expression.  */
9585   expression = cp_parser_simple_cast_expression (parser);
9586 
9587   /* A delete-expression may not appear in an integral constant
9588      expression.  */
9589   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9590     return error_mark_node;
9591 
9592   /* Construct a location e.g.:
9593        delete [ ] ptr
9594        ^~~~~~~~~~~~~~
9595      with caret == start at the start of the "delete" token, and
9596      the end at the end of the final token we consumed.  */
9597   location_t combined_loc = make_location (start_loc, start_loc,
9598 					   parser->lexer);
9599   expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9600 			      global_scope_p, tf_warning_or_error);
9601 
9602   return expression;
9603 }
9604 
9605 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9606    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9607    0 otherwise.  */
9608 
9609 static int
cp_parser_tokens_start_cast_expression(cp_parser * parser)9610 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9611 {
9612   cp_token *token = cp_lexer_peek_token (parser->lexer);
9613   switch (token->type)
9614     {
9615     case CPP_COMMA:
9616     case CPP_SEMICOLON:
9617     case CPP_QUERY:
9618     case CPP_COLON:
9619     case CPP_CLOSE_SQUARE:
9620     case CPP_CLOSE_PAREN:
9621     case CPP_CLOSE_BRACE:
9622     case CPP_OPEN_BRACE:
9623     case CPP_DOT:
9624     case CPP_DOT_STAR:
9625     case CPP_DEREF:
9626     case CPP_DEREF_STAR:
9627     case CPP_DIV:
9628     case CPP_MOD:
9629     case CPP_LSHIFT:
9630     case CPP_RSHIFT:
9631     case CPP_LESS:
9632     case CPP_GREATER:
9633     case CPP_LESS_EQ:
9634     case CPP_GREATER_EQ:
9635     case CPP_EQ_EQ:
9636     case CPP_NOT_EQ:
9637     case CPP_EQ:
9638     case CPP_MULT_EQ:
9639     case CPP_DIV_EQ:
9640     case CPP_MOD_EQ:
9641     case CPP_PLUS_EQ:
9642     case CPP_MINUS_EQ:
9643     case CPP_RSHIFT_EQ:
9644     case CPP_LSHIFT_EQ:
9645     case CPP_AND_EQ:
9646     case CPP_XOR_EQ:
9647     case CPP_OR_EQ:
9648     case CPP_XOR:
9649     case CPP_OR:
9650     case CPP_OR_OR:
9651     case CPP_EOF:
9652     case CPP_ELLIPSIS:
9653       return 0;
9654 
9655     case CPP_OPEN_PAREN:
9656       /* In ((type ()) () the last () isn't a valid cast-expression,
9657 	 so the whole must be parsed as postfix-expression.  */
9658       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9659 	     != CPP_CLOSE_PAREN;
9660 
9661     case CPP_OPEN_SQUARE:
9662       /* '[' may start a primary-expression in obj-c++ and in C++11,
9663 	 as a lambda-expression, eg, '(void)[]{}'.  */
9664       if (cxx_dialect >= cxx11)
9665 	return -1;
9666       return c_dialect_objc ();
9667 
9668     case CPP_PLUS_PLUS:
9669     case CPP_MINUS_MINUS:
9670       /* '++' and '--' may or may not start a cast-expression:
9671 
9672 	 struct T { void operator++(int); };
9673 	 void f() { (T())++; }
9674 
9675 	 vs
9676 
9677 	 int a;
9678 	 (int)++a;  */
9679       return -1;
9680 
9681     default:
9682       return 1;
9683     }
9684 }
9685 
9686 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9687    in the order: const_cast, static_cast, reinterpret_cast.
9688 
9689    Don't suggest dynamic_cast.
9690 
9691    Return the first legal cast kind found, or NULL otherwise.  */
9692 
9693 static const char *
get_cast_suggestion(tree dst_type,tree orig_expr)9694 get_cast_suggestion (tree dst_type, tree orig_expr)
9695 {
9696   tree trial;
9697 
9698   /* Reuse the parser logic by attempting to build the various kinds of
9699      cast, with "complain" disabled.
9700      Identify the first such cast that is valid.  */
9701 
9702   /* Don't attempt to run such logic within template processing.  */
9703   if (processing_template_decl)
9704     return NULL;
9705 
9706   /* First try const_cast.  */
9707   trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9708   if (trial != error_mark_node)
9709     return "const_cast";
9710 
9711   /* If that fails, try static_cast.  */
9712   trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9713   if (trial != error_mark_node)
9714     return "static_cast";
9715 
9716   /* Finally, try reinterpret_cast.  */
9717   trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9718 				  tf_none);
9719   if (trial != error_mark_node)
9720     return "reinterpret_cast";
9721 
9722   /* No such cast possible.  */
9723   return NULL;
9724 }
9725 
9726 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9727    suggesting how to convert a C-style cast of the form:
9728 
9729      (DST_TYPE)ORIG_EXPR
9730 
9731    to a C++-style cast.
9732 
9733    The primary range of RICHLOC is asssumed to be that of the original
9734    expression.  OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9735    of the parens in the C-style cast.  */
9736 
9737 static void
maybe_add_cast_fixit(rich_location * rich_loc,location_t open_paren_loc,location_t close_paren_loc,tree orig_expr,tree dst_type)9738 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9739 		      location_t close_paren_loc, tree orig_expr,
9740 		      tree dst_type)
9741 {
9742   /* This function is non-trivial, so bail out now if the warning isn't
9743      going to be emitted.  */
9744   if (!warn_old_style_cast)
9745     return;
9746 
9747   /* Try to find a legal C++ cast, trying them in order:
9748      const_cast, static_cast, reinterpret_cast.  */
9749   const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9750   if (!cast_suggestion)
9751     return;
9752 
9753   /* Replace the open paren with "CAST_SUGGESTION<".  */
9754   pretty_printer pp;
9755   pp_string (&pp, cast_suggestion);
9756   pp_less (&pp);
9757   rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9758 
9759   /* Replace the close paren with "> (".  */
9760   rich_loc->add_fixit_replace (close_paren_loc, "> (");
9761 
9762   /* Add a closing paren after the expr (the primary range of RICH_LOC).  */
9763   rich_loc->add_fixit_insert_after (")");
9764 }
9765 
9766 
9767 /* Parse a cast-expression.
9768 
9769    cast-expression:
9770      unary-expression
9771      ( type-id ) cast-expression
9772 
9773    ADDRESS_P is true iff the unary-expression is appearing as the
9774    operand of the `&' operator.   CAST_P is true if this expression is
9775    the target of a cast.
9776 
9777    Returns a representation of the expression.  */
9778 
9779 static cp_expr
cp_parser_cast_expression(cp_parser * parser,bool address_p,bool cast_p,bool decltype_p,cp_id_kind * pidk)9780 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9781 			   bool decltype_p, cp_id_kind * pidk)
9782 {
9783   /* If it's a `(', then we might be looking at a cast.  */
9784   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9785     {
9786       tree type = NULL_TREE;
9787       cp_expr expr (NULL_TREE);
9788       int cast_expression = 0;
9789       const char *saved_message;
9790 
9791       /* There's no way to know yet whether or not this is a cast.
9792 	 For example, `(int (3))' is a unary-expression, while `(int)
9793 	 3' is a cast.  So, we resort to parsing tentatively.  */
9794       cp_parser_parse_tentatively (parser);
9795       /* Types may not be defined in a cast.  */
9796       saved_message = parser->type_definition_forbidden_message;
9797       parser->type_definition_forbidden_message
9798 	= G_("types may not be defined in casts");
9799       /* Consume the `('.  */
9800       matching_parens parens;
9801       cp_token *open_paren = parens.consume_open (parser);
9802       location_t open_paren_loc = open_paren->location;
9803       location_t close_paren_loc = UNKNOWN_LOCATION;
9804 
9805       /* A very tricky bit is that `(struct S) { 3 }' is a
9806 	 compound-literal (which we permit in C++ as an extension).
9807 	 But, that construct is not a cast-expression -- it is a
9808 	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
9809 	 is legal; if the compound-literal were a cast-expression,
9810 	 you'd need an extra set of parentheses.)  But, if we parse
9811 	 the type-id, and it happens to be a class-specifier, then we
9812 	 will commit to the parse at that point, because we cannot
9813 	 undo the action that is done when creating a new class.  So,
9814 	 then we cannot back up and do a postfix-expression.
9815 
9816 	 Another tricky case is the following (c++/29234):
9817 
9818          struct S { void operator () (); };
9819 
9820          void foo ()
9821          {
9822            ( S()() );
9823          }
9824 
9825 	 As a type-id we parse the parenthesized S()() as a function
9826 	 returning a function, groktypename complains and we cannot
9827 	 back up in this case either.
9828 
9829 	 Therefore, we scan ahead to the closing `)', and check to see
9830 	 if the tokens after the `)' can start a cast-expression.  Otherwise
9831 	 we are dealing with an unary-expression, a postfix-expression
9832 	 or something else.
9833 
9834 	 Yet another tricky case, in C++11, is the following (c++/54891):
9835 
9836 	 (void)[]{};
9837 
9838          The issue is that usually, besides the case of lambda-expressions,
9839 	 the parenthesized type-id cannot be followed by '[', and, eg, we
9840 	 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9841 	 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9842 	 we don't commit, we try a cast-expression, then an unary-expression.
9843 
9844 	 Save tokens so that we can put them back.  */
9845       cp_lexer_save_tokens (parser->lexer);
9846 
9847       /* We may be looking at a cast-expression.  */
9848       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9849 						 /*consume_paren=*/true))
9850 	cast_expression
9851 	  = cp_parser_tokens_start_cast_expression (parser);
9852 
9853       /* Roll back the tokens we skipped.  */
9854       cp_lexer_rollback_tokens (parser->lexer);
9855       /* If we aren't looking at a cast-expression, simulate an error so
9856 	 that the call to cp_parser_error_occurred below returns true.  */
9857       if (!cast_expression)
9858 	cp_parser_simulate_error (parser);
9859       else
9860 	{
9861 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9862 	  parser->in_type_id_in_expr_p = true;
9863 	  /* Look for the type-id.  */
9864 	  type = cp_parser_type_id (parser);
9865 	  /* Look for the closing `)'.  */
9866 	  cp_token *close_paren = parens.require_close (parser);
9867 	  if (close_paren)
9868 	    close_paren_loc = close_paren->location;
9869 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9870 	}
9871 
9872       /* Restore the saved message.  */
9873       parser->type_definition_forbidden_message = saved_message;
9874 
9875       /* At this point this can only be either a cast or a
9876 	 parenthesized ctor such as `(T ())' that looks like a cast to
9877 	 function returning T.  */
9878       if (!cp_parser_error_occurred (parser))
9879 	{
9880 	  /* Only commit if the cast-expression doesn't start with
9881 	     '++', '--', or '[' in C++11.  */
9882 	  if (cast_expression > 0)
9883 	    cp_parser_commit_to_topmost_tentative_parse (parser);
9884 
9885 	  expr = cp_parser_cast_expression (parser,
9886 					    /*address_p=*/false,
9887 					    /*cast_p=*/true,
9888 					    /*decltype_p=*/false,
9889 					    pidk);
9890 
9891 	  if (cp_parser_parse_definitely (parser))
9892 	    {
9893 	      /* Warn about old-style casts, if so requested.  */
9894 	      if (warn_old_style_cast
9895 		  && !in_system_header_at (input_location)
9896 		  && !VOID_TYPE_P (type)
9897 		  && current_lang_name != lang_name_c)
9898 		{
9899 		  gcc_rich_location rich_loc (input_location);
9900 		  maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9901 					expr, type);
9902 		  warning_at (&rich_loc, OPT_Wold_style_cast,
9903 			      "use of old-style cast to %q#T", type);
9904 		}
9905 
9906 	      /* Only type conversions to integral or enumeration types
9907 		 can be used in constant-expressions.  */
9908 	      if (!cast_valid_in_integral_constant_expression_p (type)
9909 		  && cp_parser_non_integral_constant_expression (parser,
9910 								 NIC_CAST))
9911 		return error_mark_node;
9912 
9913 	      /* Perform the cast.  */
9914 	      /* Make a location:
9915 		   (TYPE) EXPR
9916 		   ^~~~~~~~~~~
9917 		 with start==caret at the open paren, extending to the
9918 		 end of "expr".  */
9919 	      location_t cast_loc = make_location (open_paren_loc,
9920 						   open_paren_loc,
9921 						   expr.get_finish ());
9922 	      expr = build_c_cast (cast_loc, type, expr);
9923 	      return expr;
9924 	    }
9925 	}
9926       else
9927         cp_parser_abort_tentative_parse (parser);
9928     }
9929 
9930   /* If we get here, then it's not a cast, so it must be a
9931      unary-expression.  */
9932   return cp_parser_unary_expression (parser, pidk, address_p,
9933 				     cast_p, decltype_p);
9934 }
9935 
9936 /* Parse a binary expression of the general form:
9937 
9938    pm-expression:
9939      cast-expression
9940      pm-expression .* cast-expression
9941      pm-expression ->* cast-expression
9942 
9943    multiplicative-expression:
9944      pm-expression
9945      multiplicative-expression * pm-expression
9946      multiplicative-expression / pm-expression
9947      multiplicative-expression % pm-expression
9948 
9949    additive-expression:
9950      multiplicative-expression
9951      additive-expression + multiplicative-expression
9952      additive-expression - multiplicative-expression
9953 
9954    shift-expression:
9955      additive-expression
9956      shift-expression << additive-expression
9957      shift-expression >> additive-expression
9958 
9959    relational-expression:
9960      shift-expression
9961      relational-expression < shift-expression
9962      relational-expression > shift-expression
9963      relational-expression <= shift-expression
9964      relational-expression >= shift-expression
9965 
9966   GNU Extension:
9967 
9968    relational-expression:
9969      relational-expression <? shift-expression
9970      relational-expression >? shift-expression
9971 
9972    equality-expression:
9973      relational-expression
9974      equality-expression == relational-expression
9975      equality-expression != relational-expression
9976 
9977    and-expression:
9978      equality-expression
9979      and-expression & equality-expression
9980 
9981    exclusive-or-expression:
9982      and-expression
9983      exclusive-or-expression ^ and-expression
9984 
9985    inclusive-or-expression:
9986      exclusive-or-expression
9987      inclusive-or-expression | exclusive-or-expression
9988 
9989    logical-and-expression:
9990      inclusive-or-expression
9991      logical-and-expression && inclusive-or-expression
9992 
9993    logical-or-expression:
9994      logical-and-expression
9995      logical-or-expression || logical-and-expression
9996 
9997    All these are implemented with a single function like:
9998 
9999    binary-expression:
10000      simple-cast-expression
10001      binary-expression <token> binary-expression
10002 
10003    CAST_P is true if this expression is the target of a cast.
10004 
10005    The binops_by_token map is used to get the tree codes for each <token> type.
10006    binary-expressions are associated according to a precedence table.  */
10007 
10008 #define TOKEN_PRECEDENCE(token)				     \
10009 (((token->type == CPP_GREATER				     \
10010    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10011   && !parser->greater_than_is_operator_p)		     \
10012  ? PREC_NOT_OPERATOR					     \
10013  : binops_by_token[token->type].prec)
10014 
10015 static cp_expr
cp_parser_binary_expression(cp_parser * parser,bool cast_p,bool no_toplevel_fold_p,bool decltype_p,enum cp_parser_prec prec,cp_id_kind * pidk)10016 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10017 			     bool no_toplevel_fold_p,
10018 			     bool decltype_p,
10019 			     enum cp_parser_prec prec,
10020 			     cp_id_kind * pidk)
10021 {
10022   cp_parser_expression_stack stack;
10023   cp_parser_expression_stack_entry *sp = &stack[0];
10024   cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10025   cp_parser_expression_stack_entry current;
10026   cp_expr rhs;
10027   cp_token *token;
10028   enum tree_code rhs_type;
10029   enum cp_parser_prec new_prec, lookahead_prec;
10030   tree overload;
10031 
10032   /* Parse the first expression.  */
10033   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10034 		      ? TRUTH_NOT_EXPR : ERROR_MARK);
10035   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10036 					   cast_p, decltype_p, pidk);
10037   current.prec = prec;
10038 
10039   if (cp_parser_error_occurred (parser))
10040     return error_mark_node;
10041 
10042   for (;;)
10043     {
10044       /* Get an operator token.  */
10045       token = cp_lexer_peek_token (parser->lexer);
10046 
10047       if (warn_cxx11_compat
10048           && token->type == CPP_RSHIFT
10049           && !parser->greater_than_is_operator_p)
10050         {
10051           if (warning_at (token->location, OPT_Wc__11_compat,
10052 			  "%<>>%> operator is treated"
10053 			  " as two right angle brackets in C++11"))
10054 	    inform (token->location,
10055 		    "suggest parentheses around %<>>%> expression");
10056         }
10057 
10058       new_prec = TOKEN_PRECEDENCE (token);
10059       if (new_prec != PREC_NOT_OPERATOR
10060 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10061 	/* This is a fold-expression; handle it later.  */
10062 	new_prec = PREC_NOT_OPERATOR;
10063 
10064       /* Popping an entry off the stack means we completed a subexpression:
10065 	 - either we found a token which is not an operator (`>' where it is not
10066 	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10067 	   will happen repeatedly;
10068 	 - or, we found an operator which has lower priority.  This is the case
10069 	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
10070 	   parsing `3 * 4'.  */
10071       if (new_prec <= current.prec)
10072 	{
10073 	  if (sp == stack)
10074 	    break;
10075 	  else
10076 	    goto pop;
10077 	}
10078 
10079      get_rhs:
10080       current.tree_type = binops_by_token[token->type].tree_type;
10081       current.loc = token->location;
10082 
10083       /* We used the operator token.  */
10084       cp_lexer_consume_token (parser->lexer);
10085 
10086       /* For "false && x" or "true || x", x will never be executed;
10087 	 disable warnings while evaluating it.  */
10088       if ((current.tree_type == TRUTH_ANDIF_EXPR
10089 	   && cp_fully_fold (current.lhs) == truthvalue_false_node)
10090 	  || (current.tree_type == TRUTH_ORIF_EXPR
10091 	      && cp_fully_fold (current.lhs) == truthvalue_true_node))
10092 	{
10093 	  disable_warnings_sp = sp;
10094 	  ++c_inhibit_evaluation_warnings;
10095 	}
10096 
10097       /* Extract another operand.  It may be the RHS of this expression
10098 	 or the LHS of a new, higher priority expression.  */
10099       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10100 		  ? TRUTH_NOT_EXPR : ERROR_MARK);
10101       rhs = cp_parser_simple_cast_expression (parser);
10102 
10103       /* Get another operator token.  Look up its precedence to avoid
10104 	 building a useless (immediately popped) stack entry for common
10105 	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
10106       token = cp_lexer_peek_token (parser->lexer);
10107       lookahead_prec = TOKEN_PRECEDENCE (token);
10108       if (lookahead_prec != PREC_NOT_OPERATOR
10109 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10110 	lookahead_prec = PREC_NOT_OPERATOR;
10111       if (lookahead_prec > new_prec)
10112 	{
10113 	  /* ... and prepare to parse the RHS of the new, higher priority
10114 	     expression.  Since precedence levels on the stack are
10115 	     monotonically increasing, we do not have to care about
10116 	     stack overflows.  */
10117 	  *sp = current;
10118 	  ++sp;
10119 	  current.lhs = rhs;
10120 	  current.lhs_type = rhs_type;
10121 	  current.prec = new_prec;
10122 	  new_prec = lookahead_prec;
10123 	  goto get_rhs;
10124 
10125 	 pop:
10126 	  lookahead_prec = new_prec;
10127 	  /* If the stack is not empty, we have parsed into LHS the right side
10128 	     (`4' in the example above) of an expression we had suspended.
10129 	     We can use the information on the stack to recover the LHS (`3')
10130 	     from the stack together with the tree code (`MULT_EXPR'), and
10131 	     the precedence of the higher level subexpression
10132 	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
10133 	     which will be used to actually build the additive expression.  */
10134 	  rhs = current.lhs;
10135 	  rhs_type = current.lhs_type;
10136 	  --sp;
10137 	  current = *sp;
10138 	}
10139 
10140       /* Undo the disabling of warnings done above.  */
10141       if (sp == disable_warnings_sp)
10142 	{
10143 	  disable_warnings_sp = NULL;
10144 	  --c_inhibit_evaluation_warnings;
10145 	}
10146 
10147       if (warn_logical_not_paren
10148 	  && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10149 	  && current.lhs_type == TRUTH_NOT_EXPR
10150 	  /* Avoid warning for !!x == y.  */
10151 	  && (TREE_CODE (current.lhs) != NE_EXPR
10152 	      || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10153 	  && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10154 	      || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10155 		  /* Avoid warning for !b == y where b is boolean.  */
10156 		  && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10157 		      || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10158 			  != BOOLEAN_TYPE))))
10159 	  /* Avoid warning for !!b == y where b is boolean.  */
10160 	  && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10161 		|| (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10162 		    && DECL_P (tree_strip_any_location_wrapper
10163 					    (TREE_OPERAND (current.lhs, 0)))))
10164 	      || TREE_TYPE (current.lhs) == NULL_TREE
10165 	      || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10166 	warn_logical_not_parentheses (current.loc, current.tree_type,
10167 				      current.lhs, maybe_constant_value (rhs));
10168 
10169       overload = NULL;
10170 
10171       location_t combined_loc = make_location (current.loc,
10172 					       current.lhs.get_start (),
10173 					       rhs.get_finish ());
10174 
10175       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10176 	 ERROR_MARK for everything that is not a binary expression.
10177 	 This makes warn_about_parentheses miss some warnings that
10178 	 involve unary operators.  For unary expressions we should
10179 	 pass the correct tree_code unless the unary expression was
10180 	 surrounded by parentheses.
10181       */
10182       if (no_toplevel_fold_p
10183 	  && lookahead_prec <= current.prec
10184 	  && sp == stack)
10185 	{
10186 	  if (current.lhs == error_mark_node || rhs == error_mark_node)
10187 	    current.lhs = error_mark_node;
10188 	  else
10189 	    {
10190 	      current.lhs.maybe_add_location_wrapper ();
10191 	      rhs.maybe_add_location_wrapper ();
10192 	      current.lhs
10193 		= build_min (current.tree_type,
10194 			     TREE_CODE_CLASS (current.tree_type)
10195 			     == tcc_comparison
10196 			     ? boolean_type_node : TREE_TYPE (current.lhs),
10197 			     current.lhs.get_value (), rhs.get_value ());
10198 	      SET_EXPR_LOCATION (current.lhs, combined_loc);
10199 	    }
10200 	}
10201       else
10202         {
10203 	  op_location_t op_loc (current.loc, combined_loc);
10204 	  current.lhs = build_x_binary_op (op_loc, current.tree_type,
10205                                            current.lhs, current.lhs_type,
10206 					   rhs, rhs_type, NULL_TREE, &overload,
10207                                            complain_flags (decltype_p));
10208           /* TODO: build_x_binary_op doesn't always honor the location.  */
10209           current.lhs.set_location (combined_loc);
10210         }
10211       current.lhs_type = current.tree_type;
10212 
10213       /* If the binary operator required the use of an overloaded operator,
10214 	 then this expression cannot be an integral constant-expression.
10215 	 An overloaded operator can be used even if both operands are
10216 	 otherwise permissible in an integral constant-expression if at
10217 	 least one of the operands is of enumeration type.  */
10218 
10219       if (overload
10220 	  && cp_parser_non_integral_constant_expression (parser,
10221 							 NIC_OVERLOADED))
10222 	return error_mark_node;
10223     }
10224 
10225   return current.lhs;
10226 }
10227 
10228 static cp_expr
cp_parser_binary_expression(cp_parser * parser,bool cast_p,bool no_toplevel_fold_p,enum cp_parser_prec prec,cp_id_kind * pidk)10229 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10230 			     bool no_toplevel_fold_p,
10231 			     enum cp_parser_prec prec,
10232 			     cp_id_kind * pidk)
10233 {
10234   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10235 				      /*decltype*/false, prec, pidk);
10236 }
10237 
10238 /* Parse the `? expression : assignment-expression' part of a
10239    conditional-expression.  The LOGICAL_OR_EXPR is the
10240    logical-or-expression that started the conditional-expression.
10241    Returns a representation of the entire conditional-expression.
10242 
10243    This routine is used by cp_parser_assignment_expression.
10244 
10245      ? expression : assignment-expression
10246 
10247    GNU Extensions:
10248 
10249      ? : assignment-expression */
10250 
10251 static tree
cp_parser_question_colon_clause(cp_parser * parser,cp_expr logical_or_expr)10252 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10253 {
10254   tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10255   cp_expr assignment_expr;
10256   struct cp_token *token;
10257   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10258 
10259   /* Consume the `?' token.  */
10260   cp_lexer_consume_token (parser->lexer);
10261   token = cp_lexer_peek_token (parser->lexer);
10262   if (cp_parser_allow_gnu_extensions_p (parser)
10263       && token->type == CPP_COLON)
10264     {
10265       pedwarn (token->location, OPT_Wpedantic,
10266 	       "ISO C++ does not allow %<?:%> with omitted middle operand");
10267       /* Implicit true clause.  */
10268       expr = NULL_TREE;
10269       c_inhibit_evaluation_warnings +=
10270 	folded_logical_or_expr == truthvalue_true_node;
10271       warn_for_omitted_condop (token->location, logical_or_expr);
10272     }
10273   else
10274     {
10275       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10276       parser->colon_corrects_to_scope_p = false;
10277       /* Parse the expression.  */
10278       c_inhibit_evaluation_warnings +=
10279 	folded_logical_or_expr == truthvalue_false_node;
10280       expr = cp_parser_expression (parser);
10281       c_inhibit_evaluation_warnings +=
10282 	((folded_logical_or_expr == truthvalue_true_node)
10283 	 - (folded_logical_or_expr == truthvalue_false_node));
10284       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10285     }
10286 
10287   /* The next token should be a `:'.  */
10288   cp_parser_require (parser, CPP_COLON, RT_COLON);
10289   /* Parse the assignment-expression.  */
10290   assignment_expr = cp_parser_assignment_expression (parser);
10291   c_inhibit_evaluation_warnings -=
10292     folded_logical_or_expr == truthvalue_true_node;
10293 
10294   /* Make a location:
10295        LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10296        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10297      with the caret at the "?", ranging from the start of
10298      the logical_or_expr to the end of the assignment_expr.  */
10299   loc = make_location (loc,
10300 		       logical_or_expr.get_start (),
10301 		       assignment_expr.get_finish ());
10302 
10303   /* Build the conditional-expression.  */
10304   return build_x_conditional_expr (loc, logical_or_expr,
10305 				   expr,
10306 				   assignment_expr,
10307                                    tf_warning_or_error);
10308 }
10309 
10310 /* Parse an assignment-expression.
10311 
10312    assignment-expression:
10313      conditional-expression
10314      logical-or-expression assignment-operator assignment_expression
10315      throw-expression
10316      yield-expression
10317 
10318    CAST_P is true if this expression is the target of a cast.
10319    DECLTYPE_P is true if this expression is the operand of decltype.
10320 
10321    Returns a representation for the expression.  */
10322 
10323 static cp_expr
cp_parser_assignment_expression(cp_parser * parser,cp_id_kind * pidk,bool cast_p,bool decltype_p)10324 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10325 				 bool cast_p, bool decltype_p)
10326 {
10327   cp_expr expr;
10328 
10329   /* If the next token is the `throw' keyword, then we're looking at
10330      a throw-expression.  */
10331   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10332     expr = cp_parser_throw_expression (parser);
10333   /* If the next token is the `co_yield' keyword, then we're looking at
10334      a yield-expression.  */
10335   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10336     expr = cp_parser_yield_expression (parser);
10337   /* Otherwise, it must be that we are looking at a
10338      logical-or-expression.  */
10339   else
10340     {
10341       /* Parse the binary expressions (logical-or-expression).  */
10342       expr = cp_parser_binary_expression (parser, cast_p, false,
10343 					  decltype_p,
10344 					  PREC_NOT_OPERATOR, pidk);
10345       /* If the next token is a `?' then we're actually looking at a
10346 	 conditional-expression.  */
10347       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10348 	return cp_parser_question_colon_clause (parser, expr);
10349       else
10350 	{
10351 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10352 
10353 	  /* If it's an assignment-operator, we're using the second
10354 	     production.  */
10355 	  enum tree_code assignment_operator
10356 	    = cp_parser_assignment_operator_opt (parser);
10357 	  if (assignment_operator != ERROR_MARK)
10358 	    {
10359 	      bool non_constant_p;
10360 
10361 	      /* Parse the right-hand side of the assignment.  */
10362 	      cp_expr rhs = cp_parser_initializer_clause (parser,
10363 							  &non_constant_p);
10364 
10365 	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10366 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10367 
10368 	      /* An assignment may not appear in a
10369 		 constant-expression.  */
10370 	      if (cp_parser_non_integral_constant_expression (parser,
10371 							      NIC_ASSIGNMENT))
10372 		return error_mark_node;
10373 	      /* Build the assignment expression.  Its default
10374 		 location:
10375 		   LHS = RHS
10376 		   ~~~~^~~~~
10377 		 is the location of the '=' token as the
10378 		 caret, ranging from the start of the lhs to the
10379 		 end of the rhs.  */
10380 	      loc = make_location (loc,
10381 				   expr.get_start (),
10382 				   rhs.get_finish ());
10383 	      expr = build_x_modify_expr (loc, expr,
10384 					  assignment_operator,
10385 					  rhs, NULL_TREE,
10386 					  complain_flags (decltype_p));
10387               /* TODO: build_x_modify_expr doesn't honor the location,
10388                  so we must set it here.  */
10389               expr.set_location (loc);
10390 	    }
10391 	}
10392     }
10393 
10394   return expr;
10395 }
10396 
10397 /* Parse an (optional) assignment-operator.
10398 
10399    assignment-operator: one of
10400      = *= /= %= += -= >>= <<= &= ^= |=
10401 
10402    GNU Extension:
10403 
10404    assignment-operator: one of
10405      <?= >?=
10406 
10407    If the next token is an assignment operator, the corresponding tree
10408    code is returned, and the token is consumed.  For example, for
10409    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
10410    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
10411    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
10412    operator, ERROR_MARK is returned.  */
10413 
10414 static enum tree_code
cp_parser_assignment_operator_opt(cp_parser * parser)10415 cp_parser_assignment_operator_opt (cp_parser* parser)
10416 {
10417   enum tree_code op;
10418   cp_token *token;
10419 
10420   /* Peek at the next token.  */
10421   token = cp_lexer_peek_token (parser->lexer);
10422 
10423   switch (token->type)
10424     {
10425     case CPP_EQ:
10426       op = NOP_EXPR;
10427       break;
10428 
10429     case CPP_MULT_EQ:
10430       op = MULT_EXPR;
10431       break;
10432 
10433     case CPP_DIV_EQ:
10434       op = TRUNC_DIV_EXPR;
10435       break;
10436 
10437     case CPP_MOD_EQ:
10438       op = TRUNC_MOD_EXPR;
10439       break;
10440 
10441     case CPP_PLUS_EQ:
10442       op = PLUS_EXPR;
10443       break;
10444 
10445     case CPP_MINUS_EQ:
10446       op = MINUS_EXPR;
10447       break;
10448 
10449     case CPP_RSHIFT_EQ:
10450       op = RSHIFT_EXPR;
10451       break;
10452 
10453     case CPP_LSHIFT_EQ:
10454       op = LSHIFT_EXPR;
10455       break;
10456 
10457     case CPP_AND_EQ:
10458       op = BIT_AND_EXPR;
10459       break;
10460 
10461     case CPP_XOR_EQ:
10462       op = BIT_XOR_EXPR;
10463       break;
10464 
10465     case CPP_OR_EQ:
10466       op = BIT_IOR_EXPR;
10467       break;
10468 
10469     default:
10470       /* Nothing else is an assignment operator.  */
10471       op = ERROR_MARK;
10472     }
10473 
10474   /* An operator followed by ... is a fold-expression, handled elsewhere.  */
10475   if (op != ERROR_MARK
10476       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10477     op = ERROR_MARK;
10478 
10479   /* If it was an assignment operator, consume it.  */
10480   if (op != ERROR_MARK)
10481     cp_lexer_consume_token (parser->lexer);
10482 
10483   return op;
10484 }
10485 
10486 /* Parse an expression.
10487 
10488    expression:
10489      assignment-expression
10490      expression , assignment-expression
10491 
10492    CAST_P is true if this expression is the target of a cast.
10493    DECLTYPE_P is true if this expression is the immediate operand of decltype,
10494      except possibly parenthesized or on the RHS of a comma (N3276).
10495    WARN_COMMA_P is true if a comma should be diagnosed.
10496 
10497    Returns a representation of the expression.  */
10498 
10499 static cp_expr
cp_parser_expression(cp_parser * parser,cp_id_kind * pidk,bool cast_p,bool decltype_p,bool warn_comma_p)10500 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10501 		      bool cast_p, bool decltype_p, bool warn_comma_p)
10502 {
10503   cp_expr expression = NULL_TREE;
10504   location_t loc = UNKNOWN_LOCATION;
10505 
10506   while (true)
10507     {
10508       cp_expr assignment_expression;
10509 
10510       /* Parse the next assignment-expression.  */
10511       assignment_expression
10512 	= cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10513 
10514       /* We don't create a temporary for a call that is the immediate operand
10515 	 of decltype or on the RHS of a comma.  But when we see a comma, we
10516 	 need to create a temporary for a call on the LHS.  */
10517       if (decltype_p && !processing_template_decl
10518 	  && TREE_CODE (assignment_expression) == CALL_EXPR
10519 	  && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10520 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10521 	assignment_expression
10522 	  = build_cplus_new (TREE_TYPE (assignment_expression),
10523 			     assignment_expression, tf_warning_or_error);
10524 
10525       /* If this is the first assignment-expression, we can just
10526 	 save it away.  */
10527       if (!expression)
10528 	expression = assignment_expression;
10529       else
10530 	{
10531 	  /* Create a location with caret at the comma, ranging
10532 	     from the start of the LHS to the end of the RHS.  */
10533 	  loc = make_location (loc,
10534 			       expression.get_start (),
10535 			       assignment_expression.get_finish ());
10536 	  expression = build_x_compound_expr (loc, expression,
10537 					      assignment_expression, NULL_TREE,
10538 					      complain_flags (decltype_p));
10539 	  expression.set_location (loc);
10540 	}
10541       /* If the next token is not a comma, or we're in a fold-expression, then
10542 	 we are done with the expression.  */
10543       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10544 	  || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10545 	break;
10546       /* Consume the `,'.  */
10547       loc = cp_lexer_peek_token (parser->lexer)->location;
10548       if (warn_comma_p)
10549 	{
10550 	  /* [depr.comma.subscript]: A comma expression appearing as
10551 	     the expr-or-braced-init-list of a subscripting expression
10552 	     is deprecated.  A parenthesized comma expression is not
10553 	     deprecated.  */
10554 	  warning_at (loc, OPT_Wcomma_subscript,
10555 		      "top-level comma expression in array subscript "
10556 		      "is deprecated");
10557 	  warn_comma_p = false;
10558 	}
10559       cp_lexer_consume_token (parser->lexer);
10560       /* A comma operator cannot appear in a constant-expression.  */
10561       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10562 	expression = error_mark_node;
10563     }
10564 
10565   return expression;
10566 }
10567 
10568 /* Parse a constant-expression.
10569 
10570    constant-expression:
10571      conditional-expression
10572 
10573   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10574   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
10575   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
10576   is false, NON_CONSTANT_P should be NULL.  If ALLOW_NON_CONSTANT_P is
10577   greater than 1, this isn't really a constant-expression, only a
10578   potentially constant-evaluated expression.  If STRICT_P is true,
10579   only parse a conditional-expression, otherwise parse an
10580   assignment-expression.  See below for rationale.  */
10581 
10582 static cp_expr
cp_parser_constant_expression(cp_parser * parser,int allow_non_constant_p,bool * non_constant_p,bool strict_p)10583 cp_parser_constant_expression (cp_parser* parser,
10584 			       int allow_non_constant_p,
10585 			       bool *non_constant_p,
10586 			       bool strict_p)
10587 {
10588   bool saved_integral_constant_expression_p;
10589   bool saved_allow_non_integral_constant_expression_p;
10590   bool saved_non_integral_constant_expression_p;
10591   cp_expr expression;
10592 
10593   /* It might seem that we could simply parse the
10594      conditional-expression, and then check to see if it were
10595      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
10596      one that the compiler can figure out is constant, possibly after
10597      doing some simplifications or optimizations.  The standard has a
10598      precise definition of constant-expression, and we must honor
10599      that, even though it is somewhat more restrictive.
10600 
10601      For example:
10602 
10603        int i[(2, 3)];
10604 
10605      is not a legal declaration, because `(2, 3)' is not a
10606      constant-expression.  The `,' operator is forbidden in a
10607      constant-expression.  However, GCC's constant-folding machinery
10608      will fold this operation to an INTEGER_CST for `3'.  */
10609 
10610   /* Save the old settings.  */
10611   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10612   saved_allow_non_integral_constant_expression_p
10613     = parser->allow_non_integral_constant_expression_p;
10614   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10615   /* We are now parsing a constant-expression.  */
10616   parser->integral_constant_expression_p = true;
10617   parser->allow_non_integral_constant_expression_p
10618     = (allow_non_constant_p || cxx_dialect >= cxx11);
10619   parser->non_integral_constant_expression_p = false;
10620 
10621   /* A manifestly constant-evaluated expression is evaluated even in an
10622      unevaluated operand.  */
10623   cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10624 
10625   /* Although the grammar says "conditional-expression", when not STRICT_P,
10626      we parse an "assignment-expression", which also permits
10627      "throw-expression" and the use of assignment operators.  In the case
10628      that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10629      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
10630      actually essential that we look for an assignment-expression.
10631      For example, cp_parser_initializer_clauses uses this function to
10632      determine whether a particular assignment-expression is in fact
10633      constant.  */
10634   if (strict_p)
10635     {
10636       /* Parse the binary expressions (logical-or-expression).  */
10637       expression = cp_parser_binary_expression (parser, false, false, false,
10638 						PREC_NOT_OPERATOR, NULL);
10639       /* If the next token is a `?' then we're actually looking at
10640 	 a conditional-expression; otherwise we're done.  */
10641       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10642 	expression = cp_parser_question_colon_clause (parser, expression);
10643     }
10644   else
10645     expression = cp_parser_assignment_expression (parser);
10646   /* Restore the old settings.  */
10647   parser->integral_constant_expression_p
10648     = saved_integral_constant_expression_p;
10649   parser->allow_non_integral_constant_expression_p
10650     = saved_allow_non_integral_constant_expression_p;
10651   if (cxx_dialect >= cxx11)
10652     {
10653       /* Require an rvalue constant expression here; that's what our
10654 	 callers expect.  Reference constant expressions are handled
10655 	 separately in e.g. cp_parser_template_argument.  */
10656       tree decay = expression;
10657       if (TREE_TYPE (expression)
10658 	  && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10659 	decay = build_address (expression);
10660       bool is_const = is_rvalue_constant_expression (decay);
10661       parser->non_integral_constant_expression_p = !is_const;
10662       if (!is_const && !allow_non_constant_p)
10663 	require_rvalue_constant_expression (decay);
10664     }
10665   if (allow_non_constant_p)
10666     *non_constant_p = parser->non_integral_constant_expression_p;
10667   parser->non_integral_constant_expression_p
10668     = saved_non_integral_constant_expression_p;
10669 
10670   return expression;
10671 }
10672 
10673 /* Parse __builtin_offsetof.
10674 
10675    offsetof-expression:
10676      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10677 
10678    offsetof-member-designator:
10679      id-expression
10680      | offsetof-member-designator "." id-expression
10681      | offsetof-member-designator "[" expression "]"
10682      | offsetof-member-designator "->" id-expression  */
10683 
10684 static cp_expr
cp_parser_builtin_offsetof(cp_parser * parser)10685 cp_parser_builtin_offsetof (cp_parser *parser)
10686 {
10687   int save_ice_p, save_non_ice_p;
10688   tree type;
10689   cp_expr expr;
10690   cp_id_kind dummy;
10691   cp_token *token;
10692   location_t finish_loc;
10693 
10694   /* We're about to accept non-integral-constant things, but will
10695      definitely yield an integral constant expression.  Save and
10696      restore these values around our local parsing.  */
10697   save_ice_p = parser->integral_constant_expression_p;
10698   save_non_ice_p = parser->non_integral_constant_expression_p;
10699 
10700   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10701 
10702   /* Consume the "__builtin_offsetof" token.  */
10703   cp_lexer_consume_token (parser->lexer);
10704   /* Consume the opening `('.  */
10705   matching_parens parens;
10706   parens.require_open (parser);
10707   /* Parse the type-id.  */
10708   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10709   {
10710     const char *saved_message = parser->type_definition_forbidden_message;
10711     parser->type_definition_forbidden_message
10712       = G_("types may not be defined within %<__builtin_offsetof%>");
10713     type = cp_parser_type_id (parser);
10714     parser->type_definition_forbidden_message = saved_message;
10715   }
10716   /* Look for the `,'.  */
10717   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10718   token = cp_lexer_peek_token (parser->lexer);
10719 
10720   /* Build the (type *)null that begins the traditional offsetof macro.  */
10721   tree object_ptr
10722     = build_static_cast (input_location, build_pointer_type (type),
10723 			 null_pointer_node, tf_warning_or_error);
10724 
10725   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
10726   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10727 						 true, &dummy, token->location);
10728   while (true)
10729     {
10730       token = cp_lexer_peek_token (parser->lexer);
10731       switch (token->type)
10732 	{
10733 	case CPP_OPEN_SQUARE:
10734 	  /* offsetof-member-designator "[" expression "]" */
10735 	  expr = cp_parser_postfix_open_square_expression (parser, expr,
10736 							   true, false);
10737 	  break;
10738 
10739 	case CPP_DEREF:
10740 	  /* offsetof-member-designator "->" identifier */
10741 	  expr = grok_array_decl (token->location, expr, integer_zero_node,
10742 				  NULL, tf_warning_or_error);
10743 	  /* FALLTHRU */
10744 
10745 	case CPP_DOT:
10746 	  /* offsetof-member-designator "." identifier */
10747 	  cp_lexer_consume_token (parser->lexer);
10748 	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10749 							 expr, true, &dummy,
10750 							 token->location);
10751 	  break;
10752 
10753 	case CPP_CLOSE_PAREN:
10754 	  /* Consume the ")" token.  */
10755 	  finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10756 	  cp_lexer_consume_token (parser->lexer);
10757 	  goto success;
10758 
10759 	default:
10760 	  /* Error.  We know the following require will fail, but
10761 	     that gives the proper error message.  */
10762 	  parens.require_close (parser);
10763 	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10764 	  expr = error_mark_node;
10765 	  goto failure;
10766 	}
10767     }
10768 
10769  success:
10770   /* Make a location of the form:
10771        __builtin_offsetof (struct s, f)
10772        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10773      with caret at the type-id, ranging from the start of the
10774      "_builtin_offsetof" token to the close paren.  */
10775   loc = make_location (loc, start_loc, finish_loc);
10776   /* The result will be an INTEGER_CST, so we need to explicitly
10777      preserve the location.  */
10778   expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10779 
10780  failure:
10781   parser->integral_constant_expression_p = save_ice_p;
10782   parser->non_integral_constant_expression_p = save_non_ice_p;
10783 
10784   expr = expr.maybe_add_location_wrapper ();
10785   return expr;
10786 }
10787 
10788 /* Parse a trait expression.
10789 
10790    Returns a representation of the expression, the underlying type
10791    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
10792 
10793 static cp_expr
cp_parser_trait_expr(cp_parser * parser,enum rid keyword)10794 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10795 {
10796   cp_trait_kind kind;
10797   tree type1, type2 = NULL_TREE;
10798   bool binary = false;
10799   bool variadic = false;
10800 
10801   switch (keyword)
10802     {
10803     case RID_HAS_NOTHROW_ASSIGN:
10804       kind = CPTK_HAS_NOTHROW_ASSIGN;
10805       break;
10806     case RID_HAS_NOTHROW_CONSTRUCTOR:
10807       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10808       break;
10809     case RID_HAS_NOTHROW_COPY:
10810       kind = CPTK_HAS_NOTHROW_COPY;
10811       break;
10812     case RID_HAS_TRIVIAL_ASSIGN:
10813       kind = CPTK_HAS_TRIVIAL_ASSIGN;
10814       break;
10815     case RID_HAS_TRIVIAL_CONSTRUCTOR:
10816       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10817       break;
10818     case RID_HAS_TRIVIAL_COPY:
10819       kind = CPTK_HAS_TRIVIAL_COPY;
10820       break;
10821     case RID_HAS_TRIVIAL_DESTRUCTOR:
10822       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10823       break;
10824     case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10825       kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10826       break;
10827     case RID_HAS_VIRTUAL_DESTRUCTOR:
10828       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10829       break;
10830     case RID_IS_ABSTRACT:
10831       kind = CPTK_IS_ABSTRACT;
10832       break;
10833     case RID_IS_AGGREGATE:
10834       kind = CPTK_IS_AGGREGATE;
10835       break;
10836     case RID_IS_BASE_OF:
10837       kind = CPTK_IS_BASE_OF;
10838       binary = true;
10839       break;
10840     case RID_IS_CLASS:
10841       kind = CPTK_IS_CLASS;
10842       break;
10843     case RID_IS_EMPTY:
10844       kind = CPTK_IS_EMPTY;
10845       break;
10846     case RID_IS_ENUM:
10847       kind = CPTK_IS_ENUM;
10848       break;
10849     case RID_IS_FINAL:
10850       kind = CPTK_IS_FINAL;
10851       break;
10852     case RID_IS_LAYOUT_COMPATIBLE:
10853       kind = CPTK_IS_LAYOUT_COMPATIBLE;
10854       binary = true;
10855       break;
10856     case RID_IS_LITERAL_TYPE:
10857       kind = CPTK_IS_LITERAL_TYPE;
10858       break;
10859     case RID_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
10860       kind = CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF;
10861       binary = true;
10862       break;
10863     case RID_IS_POD:
10864       kind = CPTK_IS_POD;
10865       break;
10866     case RID_IS_POLYMORPHIC:
10867       kind = CPTK_IS_POLYMORPHIC;
10868       break;
10869     case RID_IS_SAME_AS:
10870       kind = CPTK_IS_SAME_AS;
10871       binary = true;
10872       break;
10873     case RID_IS_STD_LAYOUT:
10874       kind = CPTK_IS_STD_LAYOUT;
10875       break;
10876     case RID_IS_TRIVIAL:
10877       kind = CPTK_IS_TRIVIAL;
10878       break;
10879     case RID_IS_TRIVIALLY_ASSIGNABLE:
10880       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10881       binary = true;
10882       break;
10883     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10884       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10885       variadic = true;
10886       break;
10887     case RID_IS_TRIVIALLY_COPYABLE:
10888       kind = CPTK_IS_TRIVIALLY_COPYABLE;
10889       break;
10890     case RID_IS_UNION:
10891       kind = CPTK_IS_UNION;
10892       break;
10893     case RID_UNDERLYING_TYPE:
10894       kind = CPTK_UNDERLYING_TYPE;
10895       break;
10896     case RID_BASES:
10897       kind = CPTK_BASES;
10898       break;
10899     case RID_DIRECT_BASES:
10900       kind = CPTK_DIRECT_BASES;
10901       break;
10902     case RID_IS_ASSIGNABLE:
10903       kind = CPTK_IS_ASSIGNABLE;
10904       binary = true;
10905       break;
10906     case RID_IS_CONSTRUCTIBLE:
10907       kind = CPTK_IS_CONSTRUCTIBLE;
10908       variadic = true;
10909       break;
10910     case RID_IS_NOTHROW_ASSIGNABLE:
10911       kind = CPTK_IS_NOTHROW_ASSIGNABLE;
10912       binary = true;
10913       break;
10914     case RID_IS_NOTHROW_CONSTRUCTIBLE:
10915       kind = CPTK_IS_NOTHROW_CONSTRUCTIBLE;
10916       variadic = true;
10917       break;
10918     default:
10919       gcc_unreachable ();
10920     }
10921 
10922   /* Get location of initial token.  */
10923   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10924 
10925   /* Consume the token.  */
10926   cp_lexer_consume_token (parser->lexer);
10927 
10928   matching_parens parens;
10929   parens.require_open (parser);
10930 
10931   {
10932     type_id_in_expr_sentinel s (parser);
10933     type1 = cp_parser_type_id (parser);
10934   }
10935 
10936   if (type1 == error_mark_node)
10937     return error_mark_node;
10938 
10939   if (binary)
10940     {
10941       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10942 
10943       {
10944 	type_id_in_expr_sentinel s (parser);
10945 	type2 = cp_parser_type_id (parser);
10946       }
10947 
10948       if (type2 == error_mark_node)
10949 	return error_mark_node;
10950     }
10951   else if (variadic)
10952     {
10953       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10954 	{
10955 	  cp_lexer_consume_token (parser->lexer);
10956 	  tree elt = cp_parser_type_id (parser);
10957 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10958 	    {
10959 	      cp_lexer_consume_token (parser->lexer);
10960 	      elt = make_pack_expansion (elt);
10961 	    }
10962 	  if (elt == error_mark_node)
10963 	    return error_mark_node;
10964 	  type2 = tree_cons (NULL_TREE, elt, type2);
10965 	}
10966       type2 = nreverse (type2);
10967     }
10968 
10969   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10970   parens.require_close (parser);
10971 
10972   /* Construct a location of the form:
10973        __is_trivially_copyable(_Tp)
10974        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10975      with start == caret, finishing at the close-paren.  */
10976   location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10977 
10978   /* Complete the trait expression, which may mean either processing
10979      the trait expr now or saving it for template instantiation.  */
10980   switch (kind)
10981     {
10982     case CPTK_UNDERLYING_TYPE:
10983       return cp_expr (finish_underlying_type (type1), trait_loc);
10984     case CPTK_BASES:
10985       return cp_expr (finish_bases (type1, false), trait_loc);
10986     case CPTK_DIRECT_BASES:
10987       return cp_expr (finish_bases (type1, true), trait_loc);
10988     default:
10989       return finish_trait_expr (trait_loc, kind, type1, type2);
10990     }
10991 }
10992 
10993 /* Parse a lambda expression.
10994 
10995    lambda-expression:
10996      lambda-introducer lambda-declarator [opt] compound-statement
10997      lambda-introducer < template-parameter-list > requires-clause [opt]
10998        lambda-declarator [opt] compound-statement
10999 
11000    Returns a representation of the expression.  */
11001 
11002 static cp_expr
cp_parser_lambda_expression(cp_parser * parser)11003 cp_parser_lambda_expression (cp_parser* parser)
11004 {
11005   tree lambda_expr = build_lambda_expr ();
11006   tree type;
11007   bool ok = true;
11008   cp_token *token = cp_lexer_peek_token (parser->lexer);
11009   cp_token_position start = 0;
11010 
11011   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11012 
11013   if (cxx_dialect >= cxx20)
11014     {
11015       /* C++20 allows lambdas in unevaluated context, but one in the type of a
11016 	 non-type parameter is nonsensical.
11017 
11018 	 Distinguish a lambda in the parameter type from a lambda in the
11019 	 default argument by looking at local_variables_forbidden_p, which is
11020 	 only set in default arguments.  */
11021       if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11022 	{
11023 	  error_at (token->location,
11024 		    "lambda-expression in template parameter type");
11025 	  token->error_reported = true;
11026 	  ok = false;
11027 	}
11028     }
11029   else if (cp_unevaluated_operand)
11030     {
11031       if (!token->error_reported)
11032 	{
11033 	  error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11034 		    "lambda-expression in unevaluated context"
11035 		    " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11036 	  token->error_reported = true;
11037 	}
11038       ok = false;
11039     }
11040   else if (parser->in_template_argument_list_p || processing_template_parmlist)
11041     {
11042       if (!token->error_reported)
11043 	{
11044 	  error_at (token->location, "lambda-expression in template-argument"
11045 		    " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11046 	  token->error_reported = true;
11047 	}
11048       ok = false;
11049     }
11050 
11051   /* We may be in the middle of deferred access check.  Disable
11052      it now.  */
11053   push_deferring_access_checks (dk_no_deferred);
11054 
11055   cp_parser_lambda_introducer (parser, lambda_expr);
11056   if (cp_parser_error_occurred (parser))
11057     return error_mark_node;
11058 
11059   type = begin_lambda_type (lambda_expr);
11060   if (type == error_mark_node)
11061     return error_mark_node;
11062 
11063   record_lambda_scope (lambda_expr);
11064 
11065   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
11066   determine_visibility (TYPE_NAME (type));
11067 
11068   /* Now that we've started the type, add the capture fields for any
11069      explicit captures.  */
11070   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11071 
11072   {
11073     /* Inside the class, surrounding template-parameter-lists do not apply.  */
11074     unsigned int saved_num_template_parameter_lists
11075         = parser->num_template_parameter_lists;
11076     unsigned char in_statement = parser->in_statement;
11077     bool in_switch_statement_p = parser->in_switch_statement_p;
11078     bool fully_implicit_function_template_p
11079         = parser->fully_implicit_function_template_p;
11080     tree implicit_template_parms = parser->implicit_template_parms;
11081     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11082     bool auto_is_implicit_function_template_parm_p
11083         = parser->auto_is_implicit_function_template_parm_p;
11084 
11085     parser->num_template_parameter_lists = 0;
11086     parser->in_statement = 0;
11087     parser->in_switch_statement_p = false;
11088     parser->fully_implicit_function_template_p = false;
11089     parser->implicit_template_parms = 0;
11090     parser->implicit_template_scope = 0;
11091     parser->auto_is_implicit_function_template_parm_p = false;
11092 
11093     /* The body of a lambda in a discarded statement is not discarded.  */
11094     bool discarded = in_discarded_stmt;
11095     in_discarded_stmt = 0;
11096 
11097     /* Similarly the body of a lambda in immediate function context is not
11098        in immediate function context.  */
11099     bool save_in_consteval_if_p = in_consteval_if_p;
11100     in_consteval_if_p = false;
11101 
11102     /* By virtue of defining a local class, a lambda expression has access to
11103        the private variables of enclosing classes.  */
11104 
11105     if (cp_parser_start_tentative_firewall (parser))
11106       start = token;
11107 
11108     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11109 
11110     if (ok && cp_parser_error_occurred (parser))
11111       ok = false;
11112 
11113     if (ok)
11114       {
11115 	cp_parser_lambda_body (parser, lambda_expr);
11116       }
11117     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11118       {
11119 	if (cp_parser_skip_to_closing_brace (parser))
11120 	  cp_lexer_consume_token (parser->lexer);
11121       }
11122 
11123     /* The capture list was built up in reverse order; fix that now.  */
11124     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11125       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11126 
11127     if (ok)
11128       maybe_add_lambda_conv_op (type);
11129 
11130     finish_struct (type, /*attributes=*/NULL_TREE);
11131 
11132     in_consteval_if_p = save_in_consteval_if_p;
11133     in_discarded_stmt = discarded;
11134 
11135     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11136     parser->in_statement = in_statement;
11137     parser->in_switch_statement_p = in_switch_statement_p;
11138     parser->fully_implicit_function_template_p
11139 	= fully_implicit_function_template_p;
11140     parser->implicit_template_parms = implicit_template_parms;
11141     parser->implicit_template_scope = implicit_template_scope;
11142     parser->auto_is_implicit_function_template_parm_p
11143 	= auto_is_implicit_function_template_parm_p;
11144   }
11145 
11146   /* This field is only used during parsing of the lambda.  */
11147   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11148 
11149   /* This lambda shouldn't have any proxies left at this point.  */
11150   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11151   /* And now that we're done, push proxies for an enclosing lambda.  */
11152   insert_pending_capture_proxies ();
11153 
11154   /* Update the lambda expression to a range.  */
11155   LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11156 						      token->location,
11157 						      parser->lexer);
11158 
11159   if (ok)
11160     lambda_expr = build_lambda_object (lambda_expr);
11161   else
11162     lambda_expr = error_mark_node;
11163 
11164   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11165 
11166   pop_deferring_access_checks ();
11167 
11168   return lambda_expr;
11169 }
11170 
11171 /* Parse the beginning of a lambda expression.
11172 
11173    lambda-introducer:
11174      [ lambda-capture [opt] ]
11175 
11176    LAMBDA_EXPR is the current representation of the lambda expression.  */
11177 
11178 static void
cp_parser_lambda_introducer(cp_parser * parser,tree lambda_expr)11179 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11180 {
11181   /* Need commas after the first capture.  */
11182   bool first = true;
11183 
11184   /* Eat the leading `['.  */
11185   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11186 
11187   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
11188   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11189       && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11190       && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11191       && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11192     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11193   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11194     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11195 
11196   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11197     {
11198       cp_lexer_consume_token (parser->lexer);
11199       first = false;
11200 
11201       if (!(at_function_scope_p () || parsing_nsdmi ()))
11202 	error ("non-local lambda expression cannot have a capture-default");
11203     }
11204 
11205   hash_set<tree, true> ids;
11206   tree first_capture_id = NULL_TREE;
11207   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11208     {
11209       cp_token* capture_token;
11210       tree capture_id;
11211       tree capture_init_expr;
11212       cp_id_kind idk = CP_ID_KIND_NONE;
11213       bool explicit_init_p = false;
11214 
11215       enum capture_kind_type
11216       {
11217 	BY_COPY,
11218 	BY_REFERENCE
11219       };
11220       enum capture_kind_type capture_kind = BY_COPY;
11221 
11222       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11223 	{
11224 	  error ("expected end of capture-list");
11225 	  return;
11226 	}
11227 
11228       if (first)
11229 	first = false;
11230       else
11231 	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11232 
11233       /* Possibly capture `this'.  */
11234       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11235 	{
11236 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11237 	  if (cxx_dialect < cxx20 && pedantic
11238 	      && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11239 	    pedwarn (loc, OPT_Wc__20_extensions,
11240 		     "explicit by-copy capture of %<this%> "
11241 		     "with by-copy capture default only available with "
11242 		     "%<-std=c++20%> or %<-std=gnu++20%>");
11243 	  cp_lexer_consume_token (parser->lexer);
11244 	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11245 	    pedwarn (input_location, 0,
11246 		     "already captured %qD in lambda expression",
11247 		     this_identifier);
11248 	  else
11249 	    add_capture (lambda_expr, /*id=*/this_identifier,
11250 			 /*initializer=*/finish_this_expr (),
11251 			 /*by_reference_p=*/true, explicit_init_p);
11252 	  continue;
11253 	}
11254 
11255       /* Possibly capture `*this'.  */
11256       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11257 	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11258 	{
11259 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11260 	  if (cxx_dialect < cxx17)
11261 	    pedwarn (loc, OPT_Wc__17_extensions,
11262 		     "%<*this%> capture only available with "
11263 		     "%<-std=c++17%> or %<-std=gnu++17%>");
11264 	  cp_lexer_consume_token (parser->lexer);
11265 	  cp_lexer_consume_token (parser->lexer);
11266 	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11267 	    pedwarn (input_location, 0,
11268 		     "already captured %qD in lambda expression",
11269 		     this_identifier);
11270 	  else
11271 	    add_capture (lambda_expr, /*id=*/this_identifier,
11272 			 /*initializer=*/finish_this_expr (),
11273 			 /*by_reference_p=*/false, explicit_init_p);
11274 	  continue;
11275 	}
11276 
11277       /* But reject `&this'.  */
11278       if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11279 	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11280 	{
11281 	  error_at (cp_lexer_peek_token (parser->lexer)->location,
11282 		    "%<this%> cannot be captured by reference");
11283 	  cp_lexer_consume_token (parser->lexer);
11284 	  cp_lexer_consume_token (parser->lexer);
11285 	  continue;
11286 	}
11287 
11288       /* Remember whether we want to capture as a reference or not.  */
11289       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11290 	{
11291 	  capture_kind = BY_REFERENCE;
11292 	  cp_lexer_consume_token (parser->lexer);
11293 	}
11294 
11295       bool init_pack_expansion = false;
11296       location_t ellipsis_loc = UNKNOWN_LOCATION;
11297       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11298 	{
11299 	  ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11300 	  if (cxx_dialect < cxx20)
11301 	    pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11302 		     "pack init-capture only available with "
11303 		     "%<-std=c++20%> or %<-std=gnu++20%>");
11304 	  cp_lexer_consume_token (parser->lexer);
11305 	  init_pack_expansion = true;
11306 	}
11307 
11308       /* Early C++20 drafts had ...& instead of &...; be forgiving.  */
11309       if (init_pack_expansion && capture_kind != BY_REFERENCE
11310 	  && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11311 	{
11312 	  pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11313 		   0, "%<&%> should come before %<...%>");
11314 	  capture_kind = BY_REFERENCE;
11315 	  cp_lexer_consume_token (parser->lexer);
11316 	}
11317 
11318       /* Get the identifier.  */
11319       capture_token = cp_lexer_peek_token (parser->lexer);
11320       capture_id = cp_parser_identifier (parser);
11321 
11322       if (capture_id == error_mark_node)
11323 	/* Would be nice to have a cp_parser_skip_to_closing_x for general
11324            delimiters, but I modified this to stop on unnested ']' as well.  It
11325            was already changed to stop on unnested '}', so the
11326            "closing_parenthesis" name is no more misleading with my change.  */
11327 	{
11328 	  cp_parser_skip_to_closing_parenthesis (parser,
11329 						 /*recovering=*/true,
11330 						 /*or_comma=*/true,
11331 						 /*consume_paren=*/true);
11332 	  break;
11333 	}
11334 
11335       /* Find the initializer for this capture.  */
11336       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11337 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11338 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11339 	{
11340 	  bool direct, non_constant;
11341 	  /* An explicit initializer exists.  */
11342 	  if (cxx_dialect < cxx14)
11343 	    pedwarn (input_location, OPT_Wc__14_extensions,
11344 		     "lambda capture initializers "
11345 		     "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11346 	  capture_init_expr = cp_parser_initializer (parser, &direct,
11347 						     &non_constant, true);
11348 	  explicit_init_p = true;
11349 	  if (capture_init_expr == NULL_TREE)
11350 	    {
11351 	      error ("empty initializer for lambda init-capture");
11352 	      capture_init_expr = error_mark_node;
11353 	    }
11354 	  if (init_pack_expansion)
11355 	    capture_init_expr = make_pack_expansion (capture_init_expr);
11356 	}
11357       else
11358 	{
11359 	  const char* error_msg;
11360 
11361 	  /* Turn the identifier into an id-expression.  */
11362 	  capture_init_expr
11363 	    = cp_parser_lookup_name_simple (parser, capture_id,
11364 					    capture_token->location);
11365 
11366 	  if (capture_init_expr == error_mark_node)
11367 	    {
11368 	      unqualified_name_lookup_error (capture_id);
11369 	      continue;
11370 	    }
11371 	  else if (!VAR_P (capture_init_expr)
11372 		   && TREE_CODE (capture_init_expr) != PARM_DECL)
11373 	    {
11374 	      error_at (capture_token->location,
11375 			"capture of non-variable %qE",
11376 			capture_init_expr);
11377 	      if (DECL_P (capture_init_expr))
11378 		inform (DECL_SOURCE_LOCATION (capture_init_expr),
11379 			"%q#D declared here", capture_init_expr);
11380 	      continue;
11381 	    }
11382 	  if (VAR_P (capture_init_expr)
11383 	      && decl_storage_duration (capture_init_expr) != dk_auto)
11384 	    {
11385 	      if (pedwarn (capture_token->location, 0, "capture of variable "
11386 			   "%qD with non-automatic storage duration",
11387 			   capture_init_expr))
11388 		inform (DECL_SOURCE_LOCATION (capture_init_expr),
11389 			"%q#D declared here", capture_init_expr);
11390 	      continue;
11391 	    }
11392 
11393 	  capture_init_expr
11394             = finish_id_expression
11395                 (capture_id,
11396 		 capture_init_expr,
11397                  parser->scope,
11398                  &idk,
11399                  /*integral_constant_expression_p=*/false,
11400                  /*allow_non_integral_constant_expression_p=*/false,
11401                  /*non_integral_constant_expression_p=*/NULL,
11402                  /*template_p=*/false,
11403                  /*done=*/true,
11404                  /*address_p=*/false,
11405                  /*template_arg_p=*/false,
11406                  &error_msg,
11407                  capture_token->location);
11408 
11409 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11410 	    {
11411 	      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11412 	      cp_lexer_consume_token (parser->lexer);
11413 	      capture_init_expr = make_pack_expansion (capture_init_expr);
11414 	      if (init_pack_expansion)
11415 		{
11416 		  /* If what follows is an initializer, the second '...' is
11417 		     invalid.  But for cases like [...xs...], the first one
11418 		     is invalid.  */
11419 		  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11420 		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11421 		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11422 		    ellipsis_loc = loc;
11423 		  error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11424 		  continue;
11425 		}
11426 	    }
11427 	}
11428 
11429       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11430 	  && !explicit_init_p)
11431 	{
11432 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11433 	      && capture_kind == BY_COPY)
11434 	    pedwarn (capture_token->location, 0, "explicit by-copy capture "
11435 		     "of %qD redundant with by-copy capture default",
11436 		     capture_id);
11437 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11438 	      && capture_kind == BY_REFERENCE)
11439 	    pedwarn (capture_token->location, 0, "explicit by-reference "
11440 		     "capture of %qD redundant with by-reference capture "
11441 		     "default", capture_id);
11442 	}
11443 
11444       /* Check for duplicates.
11445 	 Optimize for the zero or one explicit captures cases and only create
11446 	 the hash_set after adding second capture.  */
11447       bool found = false;
11448       if (!ids.is_empty ())
11449 	found = ids.add (capture_id);
11450       else if (first_capture_id == NULL_TREE)
11451 	first_capture_id = capture_id;
11452       else if (capture_id == first_capture_id)
11453 	found = true;
11454       else
11455 	{
11456 	  ids.add (first_capture_id);
11457 	  ids.add (capture_id);
11458 	}
11459       if (found)
11460 	pedwarn (input_location, 0,
11461 		 "already captured %qD in lambda expression", capture_id);
11462       else
11463 	add_capture (lambda_expr, capture_id, capture_init_expr,
11464 		     /*by_reference_p=*/capture_kind == BY_REFERENCE,
11465 		     explicit_init_p);
11466 
11467       /* If there is any qualification still in effect, clear it
11468 	 now; we will be starting fresh with the next capture.  */
11469       parser->scope = NULL_TREE;
11470       parser->qualifying_scope = NULL_TREE;
11471       parser->object_scope = NULL_TREE;
11472     }
11473 
11474   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11475 }
11476 
11477 /* Parse the (optional) middle of a lambda expression.
11478 
11479    lambda-declarator:
11480      ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11481      lambda-specifiers (C++23)
11482 
11483    lambda-specifiers:
11484      decl-specifier-seq [opt] noexcept-specifier [opt]
11485        attribute-specifier-seq [opt] trailing-return-type [opt]
11486 
11487    LAMBDA_EXPR is the current representation of the lambda expression.  */
11488 
11489 static bool
cp_parser_lambda_declarator_opt(cp_parser * parser,tree lambda_expr)11490 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11491 {
11492   /* 5.1.1.4 of the standard says:
11493        If a lambda-expression does not include a lambda-declarator, it is as if
11494        the lambda-declarator were ().
11495      This means an empty parameter list, no attributes, and no exception
11496      specification.  */
11497   tree param_list = void_list_node;
11498   tree std_attrs = NULL_TREE;
11499   tree gnu_attrs = NULL_TREE;
11500   tree exception_spec = NULL_TREE;
11501   tree template_param_list = NULL_TREE;
11502   tree tx_qual = NULL_TREE;
11503   tree return_type = NULL_TREE;
11504   tree trailing_requires_clause = NULL_TREE;
11505   bool has_param_list = false;
11506   location_t omitted_parms_loc = UNKNOWN_LOCATION;
11507   cp_decl_specifier_seq lambda_specs;
11508   clear_decl_specs (&lambda_specs);
11509   /* A lambda op() is const unless explicitly 'mutable'.  */
11510   cp_cv_quals quals = TYPE_QUAL_CONST;
11511 
11512   /* The template-parameter-list is optional, but must begin with
11513      an opening angle if present.  */
11514   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11515     {
11516       if (cxx_dialect < cxx20
11517 	  && (pedantic || cxx_dialect < cxx14))
11518 	pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11519 		 "lambda templates are only available with "
11520 		 "%<-std=c++20%> or %<-std=gnu++20%>");
11521 
11522       cp_lexer_consume_token (parser->lexer);
11523 
11524       template_param_list = cp_parser_template_parameter_list (parser);
11525       cp_parser_require_end_of_template_parameter_list (parser);
11526 
11527       /* We may have a constrained generic lambda; parse the requires-clause
11528 	 immediately after the template-parameter-list and combine with any
11529 	 shorthand constraints present.  */
11530       tree dreqs = cp_parser_requires_clause_opt (parser, true);
11531       if (flag_concepts)
11532 	{
11533 	  tree reqs = get_shorthand_constraints (current_template_parms);
11534 	  if (dreqs)
11535 	    reqs = combine_constraint_expressions (reqs, dreqs);
11536 	  TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11537 	}
11538 
11539       /* We just processed one more parameter list.  */
11540       ++parser->num_template_parameter_lists;
11541     }
11542 
11543   /* Committee discussion supports allowing attributes here.  */
11544   lambda_specs.attributes = cp_parser_attributes_opt (parser);
11545 
11546   /* The parameter-declaration-clause is optional (unless
11547      template-parameter-list was given), but must begin with an
11548      opening parenthesis if present.  */
11549   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11550     {
11551       bool is_consteval = false;
11552       /* For C++20, before parsing the parameter list check if there is
11553 	 a consteval specifier in the corresponding decl-specifier-seq.  */
11554       if (cxx_dialect >= cxx20)
11555 	{
11556 	  for (size_t n = cp_parser_skip_balanced_tokens (parser, 1);
11557 	       cp_lexer_nth_token_is (parser->lexer, n, CPP_KEYWORD); n++)
11558 	    {
11559 	      if (cp_lexer_peek_nth_token (parser->lexer, n)->keyword
11560 		  == RID_CONSTEVAL)
11561 		{
11562 		  is_consteval = true;
11563 		  break;
11564 		}
11565 	    }
11566 	}
11567 
11568       matching_parens parens;
11569       parens.consume_open (parser);
11570 
11571       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11572 
11573       if (is_consteval)
11574 	current_binding_level->immediate_fn_ctx_p = true;
11575 
11576       /* Parse parameters.  */
11577       param_list
11578 	= cp_parser_parameter_declaration_clause
11579 	    (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11580 
11581       /* Default arguments shall not be specified in the
11582 	 parameter-declaration-clause of a lambda-declarator.  */
11583       if (pedantic && cxx_dialect < cxx14)
11584 	for (tree t = param_list; t; t = TREE_CHAIN (t))
11585 	  if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11586 	    pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11587 		     OPT_Wc__14_extensions,
11588 		     "default argument specified for lambda parameter");
11589 
11590       parens.require_close (parser);
11591       has_param_list = true;
11592     }
11593   else if (cxx_dialect < cxx23)
11594     omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11595 
11596   /* In the decl-specifier-seq of the lambda-declarator, each
11597      decl-specifier shall either be mutable or constexpr.  */
11598   int declares_class_or_enum;
11599   if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11600     cp_parser_decl_specifier_seq (parser,
11601 				  CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11602 				  &lambda_specs, &declares_class_or_enum);
11603 
11604   if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11605     {
11606       pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11607 	       "parameter declaration before lambda declaration "
11608 	       "specifiers only optional with %<-std=c++2b%> or "
11609 	       "%<-std=gnu++2b%>");
11610       omitted_parms_loc = UNKNOWN_LOCATION;
11611     }
11612 
11613   if (lambda_specs.storage_class == sc_mutable)
11614     {
11615       LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11616       quals = TYPE_UNQUALIFIED;
11617       if (lambda_specs.conflicting_specifiers_p)
11618 	error_at (lambda_specs.locations[ds_storage_class],
11619 		  "duplicate %<mutable%>");
11620     }
11621 
11622   tx_qual = cp_parser_tx_qualifier_opt (parser);
11623   if (omitted_parms_loc && tx_qual)
11624     {
11625       pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11626 	       "parameter declaration before lambda transaction "
11627 	       "qualifier only optional with %<-std=c++2b%> or "
11628 	       "%<-std=gnu++2b%>");
11629       omitted_parms_loc = UNKNOWN_LOCATION;
11630     }
11631 
11632   /* Parse optional exception specification.  */
11633   exception_spec
11634     = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11635 
11636   if (omitted_parms_loc && exception_spec)
11637     {
11638       pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11639 	       "parameter declaration before lambda exception "
11640 	       "specification only optional with %<-std=c++2b%> or "
11641 	       "%<-std=gnu++2b%>");
11642       omitted_parms_loc = UNKNOWN_LOCATION;
11643     }
11644 
11645   /* GCC 8 accepted attributes here, and this is the place for standard C++11
11646      attributes that appertain to the function type.  */
11647   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11648     gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11649   else
11650     std_attrs = cp_parser_std_attribute_spec_seq (parser);
11651 
11652   /* Parse optional trailing return type.  */
11653   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11654     {
11655       if (omitted_parms_loc)
11656 	pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11657 		 "parameter declaration before lambda trailing "
11658 		 "return type only optional with %<-std=c++2b%> or "
11659 		 "%<-std=gnu++2b%>");
11660       cp_lexer_consume_token (parser->lexer);
11661       return_type = cp_parser_trailing_type_id (parser);
11662     }
11663 
11664   /* Also allow GNU attributes at the very end of the declaration, the usual
11665      place for GNU attributes.  */
11666   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11667     gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11668 
11669   if (has_param_list)
11670     {
11671       /* Parse optional trailing requires clause.  */
11672       trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11673 
11674       /* The function parameters must be in scope all the way until after the
11675          trailing-return-type in case of decltype.  */
11676       pop_bindings_and_leave_scope ();
11677     }
11678 
11679   /* Create the function call operator.
11680 
11681      Messing with declarators like this is no uglier than building up the
11682      FUNCTION_DECL by hand, and this is less likely to get out of sync with
11683      other code.  */
11684   {
11685     cp_decl_specifier_seq return_type_specs;
11686     cp_declarator* declarator;
11687     tree fco;
11688     void *p;
11689 
11690     clear_decl_specs (&return_type_specs);
11691     return_type_specs.type = make_auto ();
11692 
11693     if (lambda_specs.locations[ds_constexpr])
11694       {
11695 	if (cxx_dialect >= cxx17)
11696 	  return_type_specs.locations[ds_constexpr]
11697 	    = lambda_specs.locations[ds_constexpr];
11698 	else
11699 	  error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11700 		    "lambda only available with %<-std=c++17%> or "
11701 		    "%<-std=gnu++17%>");
11702       }
11703     if (lambda_specs.locations[ds_consteval])
11704       return_type_specs.locations[ds_consteval]
11705 	= lambda_specs.locations[ds_consteval];
11706 
11707     p = obstack_alloc (&declarator_obstack, 0);
11708 
11709     declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11710 				     LAMBDA_EXPR_LOCATION (lambda_expr));
11711 
11712     declarator = make_call_declarator (declarator, param_list, quals,
11713 				       VIRT_SPEC_UNSPECIFIED,
11714                                        REF_QUAL_NONE,
11715 				       tx_qual,
11716 				       exception_spec,
11717                                        return_type,
11718 				       trailing_requires_clause,
11719 				       UNKNOWN_LOCATION);
11720     declarator->std_attributes = std_attrs;
11721 
11722     fco = grokmethod (&return_type_specs,
11723 		      declarator,
11724 		      chainon (gnu_attrs, lambda_specs.attributes));
11725     if (fco != error_mark_node)
11726       {
11727 	DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11728 	DECL_ARTIFICIAL (fco) = 1;
11729 	/* Give the object parameter a different name.  */
11730 	DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11731 	DECL_SET_LAMBDA_FUNCTION (fco, true);
11732       }
11733     if (template_param_list)
11734       {
11735 	fco = finish_member_template_decl (fco);
11736 	finish_template_decl (template_param_list);
11737 	--parser->num_template_parameter_lists;
11738       }
11739     else if (parser->fully_implicit_function_template_p)
11740       fco = finish_fully_implicit_template (parser, fco);
11741 
11742     finish_member_declaration (fco);
11743 
11744     obstack_free (&declarator_obstack, p);
11745 
11746     return (fco != error_mark_node);
11747   }
11748 }
11749 
11750 /* Parse the body of a lambda expression, which is simply
11751 
11752    compound-statement
11753 
11754    but which requires special handling.
11755    LAMBDA_EXPR is the current representation of the lambda expression.  */
11756 
11757 static void
cp_parser_lambda_body(cp_parser * parser,tree lambda_expr)11758 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11759 {
11760   bool nested = (current_function_decl != NULL_TREE);
11761   unsigned char local_variables_forbidden_p
11762     = parser->local_variables_forbidden_p;
11763   bool in_function_body = parser->in_function_body;
11764 
11765   /* The body of a lambda-expression is not a subexpression of the enclosing
11766      expression.  */
11767   cp_evaluated ev;
11768 
11769   if (nested)
11770     push_function_context ();
11771   else
11772     /* Still increment function_depth so that we don't GC in the
11773        middle of an expression.  */
11774     ++function_depth;
11775 
11776   auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11777   auto ord = make_temp_override (parser->oacc_routine, NULL);
11778   auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11779   vec<tree> omp_privatization_save;
11780   save_omp_privatization_clauses (omp_privatization_save);
11781   /* Clear this in case we're in the middle of a default argument.  */
11782   parser->local_variables_forbidden_p = 0;
11783   parser->in_function_body = true;
11784 
11785   {
11786     local_specialization_stack s (lss_copy);
11787     tree fco = lambda_function (lambda_expr);
11788     tree body = start_lambda_function (fco, lambda_expr);
11789 
11790     /* Originally C++11 required us to peek for 'return expr'; and
11791        process it specially here to deduce the return type.  N3638
11792        removed the need for that.  */
11793     cp_parser_function_body (parser, false);
11794 
11795     finish_lambda_function (body);
11796   }
11797 
11798   restore_omp_privatization_clauses (omp_privatization_save);
11799   parser->local_variables_forbidden_p = local_variables_forbidden_p;
11800   parser->in_function_body = in_function_body;
11801   if (nested)
11802     pop_function_context();
11803   else
11804     --function_depth;
11805 }
11806 
11807 /* Statements [gram.stmt.stmt]  */
11808 
11809 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
11810 
11811 static void
add_debug_begin_stmt(location_t loc)11812 add_debug_begin_stmt (location_t loc)
11813 {
11814   if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11815     return;
11816   if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11817     /* A concept is never expanded normally.  */
11818     return;
11819 
11820   tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11821   SET_EXPR_LOCATION (stmt, loc);
11822   add_stmt (stmt);
11823 }
11824 
11825 struct cp_omp_attribute_data
11826 {
11827   cp_token_cache *tokens;
11828   const c_omp_directive *dir;
11829   c_omp_directive_kind kind;
11830 };
11831 
11832 /* Handle omp::directive and omp::sequence attributes in ATTRS
11833    (if any) at the start of a statement or in attribute-declaration.  */
11834 
11835 static tree
cp_parser_handle_statement_omp_attributes(cp_parser * parser,tree attrs)11836 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11837 {
11838   if (!flag_openmp && !flag_openmp_simd)
11839     return attrs;
11840 
11841   auto_vec<cp_omp_attribute_data, 16> vec;
11842   int cnt = 0;
11843   int tokens = 0;
11844   bool bad = false;
11845   for (tree *pa = &attrs; *pa; )
11846     if (get_attribute_namespace (*pa) == omp_identifier
11847 	&& is_attribute_p ("directive", get_attribute_name (*pa)))
11848       {
11849 	cnt++;
11850 	for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11851 	  {
11852 	    tree d = TREE_VALUE (a);
11853 	    gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11854 	    cp_token *first = DEFPARSE_TOKENS (d)->first;
11855 	    cp_token *last = DEFPARSE_TOKENS (d)->last;
11856 	    if (parser->omp_attrs_forbidden_p)
11857 	      {
11858 		error_at (first->location,
11859 			  "mixing OpenMP directives with attribute and pragma "
11860 			  "syntax on the same statement");
11861 		parser->omp_attrs_forbidden_p = false;
11862 		bad = true;
11863 	      }
11864 	    const char *directive[3] = {};
11865 	    for (int i = 0; i < 3; i++)
11866 	      {
11867 		tree id = NULL_TREE;
11868 		if (first + i == last)
11869 		  break;
11870 		if (first[i].type == CPP_NAME)
11871 		  id = first[i].u.value;
11872 		else if (first[i].type == CPP_KEYWORD)
11873 		  id = ridpointers[(int) first[i].keyword];
11874 		else
11875 		  break;
11876 		directive[i] = IDENTIFIER_POINTER (id);
11877 	      }
11878 	    const c_omp_directive *dir = NULL;
11879 	    if (directive[0])
11880 	      dir = c_omp_categorize_directive (directive[0], directive[1],
11881 						directive[2]);
11882 	    if (dir == NULL)
11883 	      {
11884 		error_at (first->location,
11885 			  "unknown OpenMP directive name in %<omp::directive%>"
11886 			  " attribute argument");
11887 		continue;
11888 	      }
11889 	    c_omp_directive_kind kind = dir->kind;
11890 	    if (dir->id == PRAGMA_OMP_ORDERED)
11891 	      {
11892 		/* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11893 		   depend clause.  */
11894 		if (directive[1] && strcmp (directive[1], "depend") == 0)
11895 		  kind = C_OMP_DIR_STANDALONE;
11896 		else if (first + 2 < last
11897 			 && first[1].type == CPP_COMMA
11898 			 && first[2].type == CPP_NAME
11899 			 && strcmp (IDENTIFIER_POINTER (first[2].u.value),
11900 				    "depend") == 0)
11901 		  kind = C_OMP_DIR_STANDALONE;
11902 	      }
11903 	    else if (dir->id == PRAGMA_OMP_ERROR)
11904 	      {
11905 		/* error with at(execution) clause is C_OMP_DIR_STANDALONE.  */
11906 		int paren_depth = 0;
11907 		for (int i = 1; first + i < last; i++)
11908 		  if (first[i].type == CPP_OPEN_PAREN)
11909 		    paren_depth++;
11910 		  else if (first[i].type == CPP_CLOSE_PAREN)
11911 		    paren_depth--;
11912 		  else if (paren_depth == 0
11913 			   && first + i + 2 < last
11914 			   && first[i].type == CPP_NAME
11915 			   && first[i + 1].type == CPP_OPEN_PAREN
11916 			   && first[i + 2].type == CPP_NAME
11917 			   && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11918 				       "at")
11919 			   && !strcmp (IDENTIFIER_POINTER (first[i
11920 								 + 2].u.value),
11921 				       "execution"))
11922 		    {
11923 		      kind = C_OMP_DIR_STANDALONE;
11924 		      break;
11925 		    }
11926 	      }
11927 	    cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
11928 	    vec.safe_push (v);
11929 	    if (flag_openmp || dir->simd)
11930 	      tokens += (last - first) + 1;
11931 	  }
11932 	cp_omp_attribute_data v = {};
11933 	vec.safe_push (v);
11934 	*pa = TREE_CHAIN (*pa);
11935       }
11936     else
11937       pa = &TREE_CHAIN (*pa);
11938 
11939   if (bad)
11940     return attrs;
11941 
11942   unsigned int i;
11943   cp_omp_attribute_data *v;
11944   cp_omp_attribute_data *construct_seen = nullptr;
11945   cp_omp_attribute_data *standalone_seen = nullptr;
11946   cp_omp_attribute_data *prev_standalone_seen = nullptr;
11947   FOR_EACH_VEC_ELT (vec, i, v)
11948     if (v->tokens)
11949       {
11950 	if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
11951 	  construct_seen = v;
11952 	else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
11953 	  standalone_seen = v;
11954       }
11955     else
11956       {
11957 	if (standalone_seen && !prev_standalone_seen)
11958 	  {
11959 	    prev_standalone_seen = standalone_seen;
11960 	    standalone_seen = nullptr;
11961 	  }
11962       }
11963 
11964   if (cnt > 1 && construct_seen)
11965     {
11966       error_at (construct_seen->tokens->first->location,
11967 		"OpenMP construct among %<omp::directive%> attributes"
11968 		" requires all %<omp::directive%> attributes on the"
11969 		" same statement to be in the same %<omp::sequence%>");
11970       return attrs;
11971     }
11972   if (cnt > 1 && standalone_seen && prev_standalone_seen)
11973     {
11974       error_at (standalone_seen->tokens->first->location,
11975 		"multiple OpenMP standalone directives among"
11976 		" %<omp::directive%> attributes must be all within the"
11977 		" same %<omp::sequence%>");
11978       return attrs;
11979     }
11980 
11981   if (prev_standalone_seen)
11982     standalone_seen = prev_standalone_seen;
11983   if (standalone_seen
11984       && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11985     {
11986       error_at (standalone_seen->tokens->first->location,
11987 		"standalone OpenMP directives in %<omp::directive%> attribute"
11988 		" can only appear on an empty statement");
11989       return attrs;
11990     }
11991   if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
11992     {
11993       cp_token *token = cp_lexer_peek_token (parser->lexer);
11994       enum pragma_kind kind = cp_parser_pragma_kind (token);
11995       if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
11996 	{
11997 	  error_at (token->location,
11998 		    "mixing OpenMP directives with attribute and pragma "
11999 		    "syntax on the same statement");
12000 	  return attrs;
12001 	}
12002     }
12003 
12004   if (!tokens)
12005     return attrs;
12006   tokens++;
12007   cp_lexer *lexer = cp_lexer_alloc ();
12008   lexer->debugging_p = parser->lexer->debugging_p;
12009   vec_safe_reserve (lexer->buffer, tokens, true);
12010   FOR_EACH_VEC_ELT (vec, i, v)
12011     {
12012       if (!v->tokens)
12013 	continue;
12014       if (!flag_openmp && !v->dir->simd)
12015 	continue;
12016       cp_token *first = v->tokens->first;
12017       cp_token *last = v->tokens->last;
12018       cp_token tok = {};
12019       tok.type = CPP_PRAGMA;
12020       tok.keyword = RID_MAX;
12021       tok.u.value = build_int_cst (NULL, v->dir->id);
12022       tok.location = first->location;
12023       lexer->buffer->quick_push (tok);
12024       while (++first < last)
12025 	lexer->buffer->quick_push (*first);
12026       tok = {};
12027       tok.type = CPP_PRAGMA_EOL;
12028       tok.keyword = RID_MAX;
12029       tok.location = last->location;
12030       lexer->buffer->quick_push (tok);
12031     }
12032   cp_token tok = {};
12033   tok.type = CPP_EOF;
12034   tok.keyword = RID_MAX;
12035   tok.location = lexer->buffer->last ().location;
12036   lexer->buffer->quick_push (tok);
12037   lexer->next = parser->lexer;
12038   lexer->next_token = lexer->buffer->address ();
12039   lexer->last_token = lexer->next_token
12040 		      + lexer->buffer->length ()
12041 		      - 1;
12042   lexer->in_omp_attribute_pragma = true;
12043   parser->lexer = lexer;
12044   /* Move the current source position to that of the first token in the
12045      new lexer.  */
12046   cp_lexer_set_source_position_from_token (lexer->next_token);
12047   return attrs;
12048 }
12049 
12050 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12051    (if any) at the start or after declaration-id of a declaration.  */
12052 
12053 static void
cp_parser_handle_directive_omp_attributes(cp_parser * parser,tree * pattrs,cp_omp_declare_simd_data * data,bool start)12054 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12055 					   cp_omp_declare_simd_data *data,
12056 					   bool start)
12057 {
12058   if (!flag_openmp && !flag_openmp_simd)
12059     return;
12060 
12061   int cnt = 0;
12062   bool bad = false;
12063   bool variant_p = false;
12064   location_t loc = UNKNOWN_LOCATION;
12065   for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12066     if (get_attribute_namespace (pa) == omp_identifier
12067 	&& is_attribute_p ("directive", get_attribute_name (pa)))
12068       {
12069 	for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12070 	  {
12071 	    tree d = TREE_VALUE (a);
12072 	    gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12073 	    cp_token *first = DEFPARSE_TOKENS (d)->first;
12074 	    cp_token *last = DEFPARSE_TOKENS (d)->last;
12075 	    const char *directive[3] = {};
12076 	    for (int i = 0; i < 3; i++)
12077 	      {
12078 		tree id = NULL_TREE;
12079 		if (first + i == last)
12080 		  break;
12081 		if (first[i].type == CPP_NAME)
12082 		  id = first[i].u.value;
12083 		else if (first[i].type == CPP_KEYWORD)
12084 		  id = ridpointers[(int) first[i].keyword];
12085 		else
12086 		  break;
12087 		directive[i] = IDENTIFIER_POINTER (id);
12088 	      }
12089 	    const c_omp_directive *dir = NULL;
12090 	    if (directive[0])
12091 	      dir = c_omp_categorize_directive (directive[0], directive[1],
12092 						directive[2]);
12093 	    if (dir == NULL)
12094 	      continue;
12095 	    if (dir->id == PRAGMA_OMP_DECLARE
12096 		&& (strcmp (directive[1], "simd") == 0
12097 		    || strcmp (directive[1], "variant") == 0))
12098 	      {
12099 		if (cnt++ == 0)
12100 		  {
12101 		    variant_p = strcmp (directive[1], "variant") == 0;
12102 		    loc = first->location;
12103 		  }
12104 		if (start && parser->omp_declare_simd && !bad)
12105 		  {
12106 		    error_at (first->location,
12107 			      "mixing OpenMP directives with attribute and "
12108 			      "pragma syntax on the same declaration");
12109 		    bad = true;
12110 		  }
12111 	      }
12112 	  }
12113       }
12114 
12115   if (bad)
12116     {
12117       for (tree *pa = pattrs; *pa; )
12118 	if (get_attribute_namespace (*pa) == omp_identifier
12119 	    && is_attribute_p ("directive", get_attribute_name (*pa)))
12120 	  *pa = TREE_CHAIN (*pa);
12121 	else
12122 	  pa = &TREE_CHAIN (*pa);
12123       return;
12124     }
12125   if (cnt == 0)
12126     return;
12127 
12128   if (parser->omp_declare_simd == NULL)
12129     {
12130       data->error_seen = false;
12131       data->fndecl_seen = false;
12132       data->variant_p = variant_p;
12133       data->loc = loc;
12134       data->tokens = vNULL;
12135       data->attribs[0] = NULL;
12136       data->attribs[1] = NULL;
12137       parser->omp_declare_simd = data;
12138     }
12139   parser->omp_declare_simd->attribs[!start] = pattrs;
12140 }
12141 
12142 /* Parse a statement.
12143 
12144    statement:
12145      labeled-statement
12146      expression-statement
12147      compound-statement
12148      selection-statement
12149      iteration-statement
12150      jump-statement
12151      declaration-statement
12152      try-block
12153 
12154   C++11:
12155 
12156   statement:
12157     labeled-statement
12158     attribute-specifier-seq (opt) expression-statement
12159     attribute-specifier-seq (opt) compound-statement
12160     attribute-specifier-seq (opt) selection-statement
12161     attribute-specifier-seq (opt) iteration-statement
12162     attribute-specifier-seq (opt) jump-statement
12163     declaration-statement
12164     attribute-specifier-seq (opt) try-block
12165 
12166   init-statement:
12167     expression-statement
12168     simple-declaration
12169     alias-declaration
12170 
12171   TM Extension:
12172 
12173    statement:
12174      atomic-statement
12175 
12176   IN_COMPOUND is true when the statement is nested inside a
12177   cp_parser_compound_statement; this matters for certain pragmas.
12178 
12179   If IF_P is not NULL, *IF_P is set to indicate whether the statement
12180   is a (possibly labeled) if statement which is not enclosed in braces
12181   and has an else clause.  This is used to implement -Wparentheses.
12182 
12183   CHAIN is a vector of if-else-if conditions.  */
12184 
12185 static void
cp_parser_statement(cp_parser * parser,tree in_statement_expr,bool in_compound,bool * if_p,vec<tree> * chain,location_t * loc_after_labels)12186 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12187 		     bool in_compound, bool *if_p, vec<tree> *chain,
12188 		     location_t *loc_after_labels)
12189 {
12190   tree statement, std_attrs = NULL_TREE;
12191   cp_token *token;
12192   location_t statement_location, attrs_loc;
12193   bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12194   bool has_std_attrs;
12195 
12196  restart:
12197   if (if_p != NULL)
12198     *if_p = false;
12199   /* There is no statement yet.  */
12200   statement = NULL_TREE;
12201 
12202   saved_token_sentinel saved_tokens (parser->lexer);
12203   token = cp_lexer_peek_token (parser->lexer);
12204   attrs_loc = token->location;
12205   if (c_dialect_objc ())
12206     /* In obj-c++, seeing '[[' might be the either the beginning of
12207        c++11 attributes, or a nested objc-message-expression.  So
12208        let's parse the c++11 attributes tentatively.  */
12209     cp_parser_parse_tentatively (parser);
12210   std_attrs = cp_parser_std_attribute_spec_seq (parser);
12211   if (std_attrs)
12212     attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12213   if (c_dialect_objc ())
12214     {
12215       if (!cp_parser_parse_definitely (parser))
12216 	std_attrs = NULL_TREE;
12217     }
12218   has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12219 
12220   /* Peek at the next token.  */
12221   token = cp_lexer_peek_token (parser->lexer);
12222   bool omp_attrs_forbidden_p;
12223   omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12224 
12225   if (std_attrs && (flag_openmp || flag_openmp_simd))
12226     {
12227       bool handle_omp_attribs = false;
12228       if (token->type == CPP_KEYWORD)
12229 	switch (token->keyword)
12230 	  {
12231 	  case RID_IF:
12232 	  case RID_SWITCH:
12233 	  case RID_WHILE:
12234 	  case RID_DO:
12235 	  case RID_FOR:
12236 	  case RID_BREAK:
12237 	  case RID_CONTINUE:
12238 	  case RID_RETURN:
12239 	  case RID_CO_RETURN:
12240 	  case RID_GOTO:
12241 	  case RID_AT_TRY:
12242 	  case RID_AT_CATCH:
12243 	  case RID_AT_FINALLY:
12244 	  case RID_AT_SYNCHRONIZED:
12245 	  case RID_AT_THROW:
12246 	  case RID_TRY:
12247 	  case RID_TRANSACTION_ATOMIC:
12248 	  case RID_TRANSACTION_RELAXED:
12249 	  case RID_SYNCHRONIZED:
12250 	  case RID_ATOMIC_NOEXCEPT:
12251 	  case RID_ATOMIC_CANCEL:
12252 	  case RID_TRANSACTION_CANCEL:
12253 	    handle_omp_attribs = true;
12254 	    break;
12255 	  default:
12256 	    break;
12257 	  }
12258       else if (token->type == CPP_SEMICOLON
12259 	       || token->type == CPP_OPEN_BRACE
12260 	       || token->type == CPP_PRAGMA)
12261 	handle_omp_attribs = true;
12262       if (handle_omp_attribs)
12263 	{
12264 	  std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12265 								 std_attrs);
12266 	  token = cp_lexer_peek_token (parser->lexer);
12267 	}
12268     }
12269   parser->omp_attrs_forbidden_p = false;
12270 
12271   /* Remember the location of the first token in the statement.  */
12272   cp_token *statement_token = token;
12273   statement_location = token->location;
12274   add_debug_begin_stmt (statement_location);
12275   /* If this is a keyword, then that will often determine what kind of
12276      statement we have.  */
12277   if (token->type == CPP_KEYWORD)
12278     {
12279       enum rid keyword = token->keyword;
12280 
12281       switch (keyword)
12282 	{
12283 	case RID_CASE:
12284 	case RID_DEFAULT:
12285 	  /* Looks like a labeled-statement with a case label.
12286 	     Parse the label, and then use tail recursion to parse
12287 	     the statement.  */
12288 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
12289 	  in_compound = false;
12290 	  in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12291 	  goto restart;
12292 
12293 	case RID_IF:
12294 	case RID_SWITCH:
12295 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12296 	  statement = cp_parser_selection_statement (parser, if_p, chain);
12297 	  break;
12298 
12299 	case RID_WHILE:
12300 	case RID_DO:
12301 	case RID_FOR:
12302 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12303 	  statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12304 	  break;
12305 
12306 	case RID_BREAK:
12307 	case RID_CONTINUE:
12308 	case RID_RETURN:
12309 	case RID_CO_RETURN:
12310 	case RID_GOTO:
12311 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12312 	  statement = cp_parser_jump_statement (parser);
12313 	  break;
12314 
12315 	  /* Objective-C++ exception-handling constructs.  */
12316 	case RID_AT_TRY:
12317 	case RID_AT_CATCH:
12318 	case RID_AT_FINALLY:
12319 	case RID_AT_SYNCHRONIZED:
12320 	case RID_AT_THROW:
12321 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12322 	  statement = cp_parser_objc_statement (parser);
12323 	  break;
12324 
12325 	case RID_TRY:
12326 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12327 	  statement = cp_parser_try_block (parser);
12328 	  break;
12329 
12330 	case RID_NAMESPACE:
12331 	  /* This must be a namespace alias definition.  */
12332 	  if (has_std_attrs)
12333 	    {
12334 	      /* Attributes should be parsed as part of the
12335 		 declaration, so let's un-parse them.  */
12336 	      saved_tokens.rollback();
12337 	      std_attrs = NULL_TREE;
12338 	    }
12339 	  cp_parser_declaration_statement (parser);
12340 	  return;
12341 
12342 	case RID_TRANSACTION_ATOMIC:
12343 	case RID_TRANSACTION_RELAXED:
12344 	case RID_SYNCHRONIZED:
12345 	case RID_ATOMIC_NOEXCEPT:
12346 	case RID_ATOMIC_CANCEL:
12347 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12348 	  statement = cp_parser_transaction (parser, token);
12349 	  break;
12350 	case RID_TRANSACTION_CANCEL:
12351 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12352 	  statement = cp_parser_transaction_cancel (parser);
12353 	  break;
12354 
12355 	default:
12356 	  /* It might be a keyword like `int' that can start a
12357 	     declaration-statement.  */
12358 	  break;
12359 	}
12360     }
12361   else if (token->type == CPP_NAME)
12362     {
12363       /* If the next token is a `:', then we are looking at a
12364 	 labeled-statement.  */
12365       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12366       if (token->type == CPP_COLON)
12367 	{
12368 	  /* Looks like a labeled-statement with an ordinary label.
12369 	     Parse the label, and then use tail recursion to parse
12370 	     the statement.  */
12371 
12372 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
12373 	  in_compound = false;
12374 	  in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12375 	  goto restart;
12376 	}
12377     }
12378   /* Anything that starts with a `{' must be a compound-statement.  */
12379   else if (token->type == CPP_OPEN_BRACE)
12380     {
12381       std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12382       statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12383     }
12384   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12385      a statement all its own.  */
12386   else if (token->type == CPP_PRAGMA)
12387     {
12388      do_pragma:;
12389       cp_lexer *lexer = parser->lexer;
12390       bool do_restart = false;
12391       /* Only certain OpenMP pragmas are attached to statements, and thus
12392 	 are considered statements themselves.  All others are not.  In
12393 	 the context of a compound, accept the pragma as a "statement" and
12394 	 return so that we can check for a close brace.  Otherwise we
12395 	 require a real statement and must go back and read one.  */
12396       if (in_compound)
12397 	cp_parser_pragma (parser, pragma_compound, if_p);
12398       else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12399 	do_restart = true;
12400       if (parser->lexer != lexer
12401 	  && lexer->in_omp_attribute_pragma
12402 	  && (!in_omp_attribute_pragma || lexer->orphan_p))
12403 	{
12404 	  if (saved_tokens.lexer == lexer)
12405 	    {
12406 	      if (saved_tokens.mode == STS_COMMIT)
12407 		cp_lexer_commit_tokens (lexer);
12408 	      gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12409 	      saved_tokens.lexer = parser->lexer;
12410 	      saved_tokens.mode = STS_DONOTHING;
12411 	      saved_tokens.len = parser->lexer->saved_tokens.length ();
12412 	    }
12413 	  cp_lexer_destroy (lexer);
12414 	  lexer = parser->lexer;
12415 	}
12416       if (do_restart)
12417 	goto restart;
12418       if (parser->lexer == lexer
12419 	  && lexer->in_omp_attribute_pragma
12420 	  && !in_omp_attribute_pragma)
12421 	parser->lexer->orphan_p = true;
12422       return;
12423     }
12424   else if (token->type == CPP_EOF)
12425     {
12426       cp_parser_error (parser, "expected statement");
12427       return;
12428     }
12429 
12430   /* Everything else must be a declaration-statement or an
12431      expression-statement.  Try for the declaration-statement
12432      first, unless we are looking at a `;', in which case we know that
12433      we have an expression-statement.  */
12434   if (!statement)
12435     {
12436       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12437 	{
12438 	  if (has_std_attrs)
12439 	    /* Attributes should be parsed as part of the declaration,
12440 	       so let's un-parse them.  */
12441 	    saved_tokens.rollback();
12442 
12443 	  parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12444 	  cp_parser_parse_tentatively (parser);
12445 	  /* Try to parse the declaration-statement.  */
12446 	  cp_parser_declaration_statement (parser);
12447 	  parser->omp_attrs_forbidden_p = false;
12448 	  /* If that worked, we're done.  */
12449 	  if (cp_parser_parse_definitely (parser))
12450 	    return;
12451 	  /* It didn't work, restore the post-attribute position.  */
12452 	  if (has_std_attrs)
12453 	    {
12454 	      cp_lexer_set_token_position (parser->lexer, statement_token);
12455 	      if (flag_openmp || flag_openmp_simd)
12456 		{
12457 		  size_t i = 1;
12458 		  bool handle_omp_attribs = true;
12459 		  while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12460 			 == RID_EXTENSION)
12461 		    i++;
12462 		  switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12463 		    {
12464 		    case RID_ASM:
12465 		    case RID_NAMESPACE:
12466 		    case RID_USING:
12467 		    case RID_LABEL:
12468 		    case RID_STATIC_ASSERT:
12469 		      /* Don't handle OpenMP attribs on keywords that
12470 			 always start a declaration statement but don't
12471 			 accept attribute before it and therefore
12472 			 the tentative cp_parser_declaration_statement
12473 			 fails to parse because of that.  */
12474 		      handle_omp_attribs = false;
12475 		      break;
12476 		    default:
12477 		      break;
12478 		    }
12479 
12480 		  if (handle_omp_attribs)
12481 		    {
12482 		      parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12483 		      std_attrs
12484 			= cp_parser_handle_statement_omp_attributes
12485 					(parser, std_attrs);
12486 		      parser->omp_attrs_forbidden_p = false;
12487 		      token = cp_lexer_peek_token (parser->lexer);
12488 		      if (token->type == CPP_PRAGMA)
12489 			goto do_pragma;
12490 		    }
12491 		}
12492 	    }
12493 	}
12494       /* All preceding labels have been parsed at this point.  */
12495       if (loc_after_labels != NULL)
12496 	*loc_after_labels = statement_location;
12497 
12498       std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12499 
12500       /* Look for an expression-statement instead.  */
12501       statement = cp_parser_expression_statement (parser, in_statement_expr);
12502 
12503       /* Handle [[fallthrough]];.  */
12504       if (attribute_fallthrough_p (std_attrs))
12505 	{
12506 	  /* The next token after the fallthrough attribute is ';'.  */
12507 	  if (statement == NULL_TREE)
12508 	    {
12509 	      /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
12510 	      statement = build_call_expr_internal_loc (statement_location,
12511 							IFN_FALLTHROUGH,
12512 							void_type_node, 0);
12513 	      finish_expr_stmt (statement);
12514 	    }
12515 	  else
12516 	    warning_at (statement_location, OPT_Wattributes,
12517 			"%<fallthrough%> attribute not followed by %<;%>");
12518 	  std_attrs = NULL_TREE;
12519 	}
12520     }
12521 
12522   /* Set the line number for the statement.  */
12523   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12524     SET_EXPR_LOCATION (statement, statement_location);
12525 
12526   /* Allow "[[fallthrough]];", but warn otherwise.  */
12527   if (std_attrs != NULL_TREE)
12528     warning_at (attrs_loc,
12529 		OPT_Wattributes,
12530 		"attributes at the beginning of statement are ignored");
12531 }
12532 
12533 /* Append ATTR to attribute list ATTRS.  */
12534 
12535 static tree
attr_chainon(tree attrs,tree attr)12536 attr_chainon (tree attrs, tree attr)
12537 {
12538   if (attrs == error_mark_node)
12539     return error_mark_node;
12540   if (attr == error_mark_node)
12541     return error_mark_node;
12542   return chainon (attrs, attr);
12543 }
12544 
12545 /* Parse the label for a labeled-statement, i.e.
12546 
12547    identifier :
12548    case constant-expression :
12549    default :
12550 
12551    GNU Extension:
12552    case constant-expression ... constant-expression : statement
12553 
12554    When a label is parsed without errors, the label is added to the
12555    parse tree by the finish_* functions, so this function doesn't
12556    have to return the label.  */
12557 
12558 static void
cp_parser_label_for_labeled_statement(cp_parser * parser,tree attributes)12559 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12560 {
12561   cp_token *token;
12562   tree label = NULL_TREE;
12563   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12564 
12565   /* The next token should be an identifier.  */
12566   token = cp_lexer_peek_token (parser->lexer);
12567   if (token->type != CPP_NAME
12568       && token->type != CPP_KEYWORD)
12569     {
12570       cp_parser_error (parser, "expected labeled-statement");
12571       return;
12572     }
12573 
12574   /* Remember whether this case or a user-defined label is allowed to fall
12575      through to.  */
12576   bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12577 
12578   parser->colon_corrects_to_scope_p = false;
12579   switch (token->keyword)
12580     {
12581     case RID_CASE:
12582       {
12583 	tree expr, expr_hi;
12584 	cp_token *ellipsis;
12585 
12586 	/* Consume the `case' token.  */
12587 	cp_lexer_consume_token (parser->lexer);
12588 	/* Parse the constant-expression.  */
12589 	expr = cp_parser_constant_expression (parser);
12590 	if (check_for_bare_parameter_packs (expr))
12591 	  expr = error_mark_node;
12592 
12593 	ellipsis = cp_lexer_peek_token (parser->lexer);
12594 	if (ellipsis->type == CPP_ELLIPSIS)
12595 	  {
12596 	    /* Consume the `...' token.  */
12597 	    cp_lexer_consume_token (parser->lexer);
12598 	    expr_hi = cp_parser_constant_expression (parser);
12599 	    if (check_for_bare_parameter_packs (expr_hi))
12600 	      expr_hi = error_mark_node;
12601 
12602 	    /* We don't need to emit warnings here, as the common code
12603 	       will do this for us.  */
12604 	  }
12605 	else
12606 	  expr_hi = NULL_TREE;
12607 
12608 	if (parser->in_switch_statement_p)
12609 	  {
12610 	    tree l = finish_case_label (token->location, expr, expr_hi);
12611 	    if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12612 	      {
12613 		label = CASE_LABEL (l);
12614 		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12615 	      }
12616 	  }
12617 	else
12618 	  error_at (token->location,
12619 		    "case label %qE not within a switch statement",
12620 		    expr);
12621       }
12622       break;
12623 
12624     case RID_DEFAULT:
12625       /* Consume the `default' token.  */
12626       cp_lexer_consume_token (parser->lexer);
12627 
12628       if (parser->in_switch_statement_p)
12629 	{
12630 	  tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12631 	  if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12632 	      {
12633 		label = CASE_LABEL (l);
12634 		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12635 	      }
12636 	}
12637       else
12638 	error_at (token->location, "case label not within a switch statement");
12639       break;
12640 
12641     default:
12642       /* Anything else must be an ordinary label.  */
12643       label = finish_label_stmt (cp_parser_identifier (parser));
12644       if (label && TREE_CODE (label) == LABEL_DECL)
12645 	FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12646       break;
12647     }
12648 
12649   /* Require the `:' token.  */
12650   cp_parser_require (parser, CPP_COLON, RT_COLON);
12651 
12652   /* An ordinary label may optionally be followed by attributes.
12653      However, this is only permitted if the attributes are then
12654      followed by a semicolon.  This is because, for backward
12655      compatibility, when parsing
12656        lab: __attribute__ ((unused)) int i;
12657      we want the attribute to attach to "i", not "lab".  */
12658   if (label != NULL_TREE
12659       && cp_next_tokens_can_be_gnu_attribute_p (parser))
12660     {
12661       tree attrs;
12662       cp_parser_parse_tentatively (parser);
12663       attrs = cp_parser_gnu_attributes_opt (parser);
12664       if (attrs == NULL_TREE
12665 	  /* And fallthrough always binds to the expression-statement.  */
12666 	  || attribute_fallthrough_p (attrs)
12667 	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12668 	cp_parser_abort_tentative_parse (parser);
12669       else if (!cp_parser_parse_definitely (parser))
12670 	;
12671       else
12672 	attributes = attr_chainon (attributes, attrs);
12673     }
12674 
12675   if (attributes != NULL_TREE)
12676     cplus_decl_attributes (&label, attributes, 0);
12677 
12678   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12679 }
12680 
12681 /* Parse an expression-statement.
12682 
12683    expression-statement:
12684      expression [opt] ;
12685 
12686    Returns the new EXPR_STMT -- or NULL_TREE if the expression
12687    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12688    indicates whether this expression-statement is part of an
12689    expression statement.  */
12690 
12691 static tree
cp_parser_expression_statement(cp_parser * parser,tree in_statement_expr)12692 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12693 {
12694   tree statement = NULL_TREE;
12695   cp_token *token = cp_lexer_peek_token (parser->lexer);
12696   location_t loc = token->location;
12697 
12698   /* There might be attribute fallthrough.  */
12699   tree attr = cp_parser_gnu_attributes_opt (parser);
12700 
12701   /* If the next token is a ';', then there is no expression
12702      statement.  */
12703   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12704     {
12705       statement = cp_parser_expression (parser);
12706       if (statement == error_mark_node
12707 	  && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12708 	{
12709 	  cp_parser_skip_to_end_of_block_or_statement (parser);
12710 	  return error_mark_node;
12711 	}
12712     }
12713 
12714   /* Handle [[fallthrough]];.  */
12715   if (attribute_fallthrough_p (attr))
12716     {
12717       /* The next token after the fallthrough attribute is ';'.  */
12718       if (statement == NULL_TREE)
12719 	/* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
12720 	statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12721 						  void_type_node, 0);
12722       else
12723 	warning_at (loc, OPT_Wattributes,
12724 		    "%<fallthrough%> attribute not followed by %<;%>");
12725       attr = NULL_TREE;
12726     }
12727 
12728   /* Allow "[[fallthrough]];", but warn otherwise.  */
12729   if (attr != NULL_TREE)
12730     warning_at (loc, OPT_Wattributes,
12731 		"attributes at the beginning of statement are ignored");
12732 
12733   /* Give a helpful message for "A<T>::type t;" and the like.  */
12734   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12735       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12736     {
12737       if (TREE_CODE (statement) == SCOPE_REF)
12738 	error_at (token->location, "need %<typename%> before %qE because "
12739 		  "%qT is a dependent scope",
12740 		  statement, TREE_OPERAND (statement, 0));
12741       else if (is_overloaded_fn (statement)
12742 	       && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12743 	{
12744 	  /* A::A a; */
12745 	  tree fn = get_first_fn (statement);
12746 	  error_at (token->location,
12747 		    "%<%T::%D%> names the constructor, not the type",
12748 		    DECL_CONTEXT (fn), DECL_NAME (fn));
12749 	}
12750     }
12751 
12752   /* Consume the final `;'.  */
12753   cp_parser_consume_semicolon_at_end_of_statement (parser);
12754 
12755   if (in_statement_expr
12756       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12757     /* This is the final expression statement of a statement
12758        expression.  */
12759     statement = finish_stmt_expr_expr (statement, in_statement_expr);
12760   else if (statement)
12761     statement = finish_expr_stmt (statement);
12762 
12763   return statement;
12764 }
12765 
12766 /* Parse a compound-statement.
12767 
12768    compound-statement:
12769      { statement-seq [opt] }
12770 
12771    GNU extension:
12772 
12773    compound-statement:
12774      { label-declaration-seq [opt] statement-seq [opt] }
12775 
12776    label-declaration-seq:
12777      label-declaration
12778      label-declaration-seq label-declaration
12779 
12780    Returns a tree representing the statement.  */
12781 
12782 static tree
cp_parser_compound_statement(cp_parser * parser,tree in_statement_expr,int bcs_flags,bool function_body)12783 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12784 			      int bcs_flags, bool function_body)
12785 {
12786   tree compound_stmt;
12787   matching_braces braces;
12788 
12789   /* Consume the `{'.  */
12790   if (!braces.require_open (parser))
12791     return error_mark_node;
12792   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12793       && !function_body && cxx_dialect < cxx14)
12794     pedwarn (input_location, OPT_Wpedantic,
12795 	     "compound-statement in %<constexpr%> function");
12796   /* Begin the compound-statement.  */
12797   compound_stmt = begin_compound_stmt (bcs_flags);
12798   /* If the next keyword is `__label__' we have a label declaration.  */
12799   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12800     cp_parser_label_declaration (parser);
12801   /* Parse an (optional) statement-seq.  */
12802   cp_parser_statement_seq_opt (parser, in_statement_expr);
12803 
12804   /* Consume the `}'.  */
12805   braces.require_close (parser);
12806 
12807   /* Finish the compound-statement.  */
12808   finish_compound_stmt (compound_stmt);
12809 
12810   return compound_stmt;
12811 }
12812 
12813 /* Parse an (optional) statement-seq.
12814 
12815    statement-seq:
12816      statement
12817      statement-seq [opt] statement  */
12818 
12819 static void
cp_parser_statement_seq_opt(cp_parser * parser,tree in_statement_expr)12820 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12821 {
12822   /* Scan statements until there aren't any more.  */
12823   while (true)
12824     {
12825       cp_token *token = cp_lexer_peek_token (parser->lexer);
12826 
12827       /* If we are looking at a `}', then we have run out of
12828 	 statements; the same is true if we have reached the end
12829 	 of file, or have stumbled upon a stray '@end'.  */
12830       if (token->type == CPP_CLOSE_BRACE
12831 	  || token->type == CPP_EOF
12832 	  || token->type == CPP_PRAGMA_EOL
12833 	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12834 	break;
12835 
12836       /* If we are in a compound statement and find 'else' then
12837 	 something went wrong.  */
12838       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12839 	{
12840 	  if (parser->in_statement & IN_IF_STMT)
12841 	    break;
12842 	  else
12843 	    {
12844 	      token = cp_lexer_consume_token (parser->lexer);
12845 	      error_at (token->location, "%<else%> without a previous %<if%>");
12846 	    }
12847 	}
12848 
12849       /* Parse the statement.  */
12850       cp_parser_statement (parser, in_statement_expr, true, NULL);
12851     }
12852 }
12853 
12854 /* Return true if this is the C++20 version of range-based-for with
12855    init-statement.  */
12856 
12857 static bool
cp_parser_range_based_for_with_init_p(cp_parser * parser)12858 cp_parser_range_based_for_with_init_p (cp_parser *parser)
12859 {
12860   bool r = false;
12861 
12862   /* Save tokens so that we can put them back.  */
12863   cp_lexer_save_tokens (parser->lexer);
12864 
12865   /* There has to be an unnested ; followed by an unnested :.  */
12866   if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12867 					       /*recovering=*/false,
12868 					       CPP_SEMICOLON,
12869 					       /*consume_paren=*/false) != -1)
12870     goto out;
12871 
12872   /* We found the semicolon, eat it now.  */
12873   cp_lexer_consume_token (parser->lexer);
12874 
12875   /* Now look for ':' that is not nested in () or {}.  */
12876   r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12877 						/*recovering=*/false,
12878 						CPP_COLON,
12879 						/*consume_paren=*/false) == -1);
12880 
12881 out:
12882   /* Roll back the tokens we skipped.  */
12883   cp_lexer_rollback_tokens (parser->lexer);
12884 
12885   return r;
12886 }
12887 
12888 /* Return true if we're looking at (init; cond), false otherwise.  */
12889 
12890 static bool
cp_parser_init_statement_p(cp_parser * parser)12891 cp_parser_init_statement_p (cp_parser *parser)
12892 {
12893   /* Save tokens so that we can put them back.  */
12894   cp_lexer_save_tokens (parser->lexer);
12895 
12896   /* Look for ';' that is not nested in () or {}.  */
12897   int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12898 						     /*recovering=*/false,
12899 						     CPP_SEMICOLON,
12900 						     /*consume_paren=*/false);
12901 
12902   /* Roll back the tokens we skipped.  */
12903   cp_lexer_rollback_tokens (parser->lexer);
12904 
12905   return ret == -1;
12906 }
12907 
12908 /* Parse a selection-statement.
12909 
12910    selection-statement:
12911      if ( init-statement [opt] condition ) statement
12912      if ( init-statement [opt] condition ) statement else statement
12913      switch ( init-statement [opt] condition ) statement
12914 
12915    Returns the new IF_STMT or SWITCH_STMT.
12916 
12917    If IF_P is not NULL, *IF_P is set to indicate whether the statement
12918    is a (possibly labeled) if statement which is not enclosed in
12919    braces and has an else clause.  This is used to implement
12920    -Wparentheses.
12921 
12922    CHAIN is a vector of if-else-if conditions.  This is used to implement
12923    -Wduplicated-cond.  */
12924 
12925 static tree
cp_parser_selection_statement(cp_parser * parser,bool * if_p,vec<tree> * chain)12926 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12927 			       vec<tree> *chain)
12928 {
12929   cp_token *token;
12930   enum rid keyword;
12931   token_indent_info guard_tinfo;
12932 
12933   if (if_p != NULL)
12934     *if_p = false;
12935 
12936   /* Peek at the next token.  */
12937   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12938   guard_tinfo = get_token_indent_info (token);
12939 
12940   /* See what kind of keyword it is.  */
12941   keyword = token->keyword;
12942   switch (keyword)
12943     {
12944     case RID_IF:
12945     case RID_SWITCH:
12946       {
12947 	tree statement;
12948 	tree condition;
12949 
12950 	bool cx = false;
12951 	if (keyword == RID_IF
12952 	    && cp_lexer_next_token_is_keyword (parser->lexer,
12953 					       RID_CONSTEXPR))
12954 	  {
12955 	    cx = true;
12956 	    cp_token *tok = cp_lexer_consume_token (parser->lexer);
12957 	    if (cxx_dialect < cxx17)
12958 	      pedwarn (tok->location, OPT_Wc__17_extensions,
12959 		       "%<if constexpr%> only available with "
12960 		       "%<-std=c++17%> or %<-std=gnu++17%>");
12961 	  }
12962 	int ce = 0;
12963 	if (keyword == RID_IF && !cx)
12964 	  {
12965 	    if (cp_lexer_next_token_is_keyword (parser->lexer,
12966 						RID_CONSTEVAL))
12967 	      ce = 1;
12968 	    else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
12969 		     && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
12970 						       RID_CONSTEVAL))
12971 	      {
12972 		ce = -1;
12973 		cp_lexer_consume_token (parser->lexer);
12974 	      }
12975 	  }
12976 	if (ce)
12977 	  {
12978 	    cp_token *tok = cp_lexer_consume_token (parser->lexer);
12979 	    if (cxx_dialect < cxx23)
12980 	      pedwarn (tok->location, OPT_Wc__23_extensions,
12981 		       "%<if consteval%> only available with "
12982 		       "%<-std=c++2b%> or %<-std=gnu++2b%>");
12983 
12984 	    bool save_in_consteval_if_p = in_consteval_if_p;
12985 	    statement = begin_if_stmt ();
12986 	    IF_STMT_CONSTEVAL_P (statement) = true;
12987 	    condition = finish_if_stmt_cond (boolean_false_node, statement);
12988 
12989 	    gcc_rich_location richloc (tok->location);
12990 	    bool non_compound_stmt_p = false;
12991 	    if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12992 	      {
12993 		non_compound_stmt_p = true;
12994 		richloc.add_fixit_insert_after (tok->location, "{");
12995 	      }
12996 
12997 	    in_consteval_if_p |= ce > 0;
12998 	    cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12999 
13000 	    if (non_compound_stmt_p)
13001 	      {
13002 		location_t before_loc
13003 		  = cp_lexer_peek_token (parser->lexer)->location;
13004 		richloc.add_fixit_insert_before (before_loc, "}");
13005 		error_at (&richloc,
13006 			  "%<if consteval%> requires compound statement");
13007 		non_compound_stmt_p = false;
13008 	      }
13009 
13010 	    finish_then_clause (statement);
13011 
13012 	    /* If the next token is `else', parse the else-clause.  */
13013 	    if (cp_lexer_next_token_is_keyword (parser->lexer,
13014 						RID_ELSE))
13015 	      {
13016 		cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13017 		gcc_rich_location else_richloc (else_tok->location);
13018 		guard_tinfo = get_token_indent_info (else_tok);
13019 		/* Consume the `else' keyword.  */
13020 		cp_lexer_consume_token (parser->lexer);
13021 
13022 		begin_else_clause (statement);
13023 
13024 		if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13025 		  {
13026 		    non_compound_stmt_p = true;
13027 		    else_richloc.add_fixit_insert_after (else_tok->location,
13028 							 "{");
13029 		  }
13030 
13031 		in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13032 		cp_parser_implicitly_scoped_statement (parser, NULL,
13033 						       guard_tinfo);
13034 
13035 		if (non_compound_stmt_p)
13036 		  {
13037 		    location_t before_loc
13038 		      = cp_lexer_peek_token (parser->lexer)->location;
13039 		    else_richloc.add_fixit_insert_before (before_loc, "}");
13040 		    error_at (&else_richloc,
13041 			      "%<if consteval%> requires compound statement");
13042 		  }
13043 
13044 		finish_else_clause (statement);
13045 	      }
13046 
13047 	    in_consteval_if_p = save_in_consteval_if_p;
13048 	    if (ce < 0)
13049 	      {
13050 		std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13051 		if (THEN_CLAUSE (statement) == NULL_TREE)
13052 		  THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13053 	      }
13054 
13055 	    finish_if_stmt (statement);
13056 	    return statement;
13057 	  }
13058 
13059 	/* Look for the `('.  */
13060 	matching_parens parens;
13061 	if (!parens.require_open (parser))
13062 	  {
13063 	    cp_parser_skip_to_end_of_statement (parser);
13064 	    return error_mark_node;
13065 	  }
13066 
13067 	/* Begin the selection-statement.  */
13068 	if (keyword == RID_IF)
13069 	  {
13070 	    statement = begin_if_stmt ();
13071 	    IF_STMT_CONSTEXPR_P (statement) = cx;
13072 	  }
13073 	else
13074 	  statement = begin_switch_stmt ();
13075 
13076 	/* Parse the optional init-statement.  */
13077 	if (cp_parser_init_statement_p (parser))
13078 	  {
13079 	    tree decl;
13080 	    if (cxx_dialect < cxx17)
13081 	      pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13082 		       OPT_Wc__17_extensions,
13083 		       "init-statement in selection statements only available "
13084 		       "with %<-std=c++17%> or %<-std=gnu++17%>");
13085 	    if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13086 	      /* A non-empty init-statement can have arbitrary side
13087 		 effects.  */
13088 	      vec_free (chain);
13089 	    cp_parser_init_statement (parser, &decl);
13090 	  }
13091 
13092 	/* Parse the condition.  */
13093 	condition = cp_parser_condition (parser);
13094 	/* Look for the `)'.  */
13095 	if (!parens.require_close (parser))
13096 	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
13097 						 /*consume_paren=*/true);
13098 
13099 	if (keyword == RID_IF)
13100 	  {
13101 	    bool nested_if;
13102 	    unsigned char in_statement;
13103 
13104 	    /* Add the condition.  */
13105 	    condition = finish_if_stmt_cond (condition, statement);
13106 
13107 	    if (warn_duplicated_cond)
13108 	      warn_duplicated_cond_add_or_warn (token->location, condition,
13109 						&chain);
13110 
13111 	    /* Parse the then-clause.  */
13112 	    in_statement = parser->in_statement;
13113 	    parser->in_statement |= IN_IF_STMT;
13114 
13115 	    /* Outside a template, the non-selected branch of a constexpr
13116 	       if is a 'discarded statement', i.e. unevaluated.  */
13117 	    bool was_discarded = in_discarded_stmt;
13118 	    bool discard_then = (cx && !processing_template_decl
13119 				 && integer_zerop (condition));
13120 	    if (discard_then)
13121 	      {
13122 		in_discarded_stmt = true;
13123 		++c_inhibit_evaluation_warnings;
13124 	      }
13125 
13126 	    cp_parser_implicitly_scoped_statement (parser, &nested_if,
13127 						   guard_tinfo);
13128 
13129 	    parser->in_statement = in_statement;
13130 
13131 	    finish_then_clause (statement);
13132 
13133 	    if (discard_then)
13134 	      {
13135 		THEN_CLAUSE (statement) = NULL_TREE;
13136 		in_discarded_stmt = was_discarded;
13137 		--c_inhibit_evaluation_warnings;
13138 	      }
13139 
13140 	    /* If the next token is `else', parse the else-clause.  */
13141 	    if (cp_lexer_next_token_is_keyword (parser->lexer,
13142 						RID_ELSE))
13143 	      {
13144 		bool discard_else = (cx && !processing_template_decl
13145 				     && integer_nonzerop (condition));
13146 		if (discard_else)
13147 		  {
13148 		    in_discarded_stmt = true;
13149 		    ++c_inhibit_evaluation_warnings;
13150 		  }
13151 
13152 		guard_tinfo
13153 		  = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13154 		/* Consume the `else' keyword.  */
13155 		cp_lexer_consume_token (parser->lexer);
13156 		if (warn_duplicated_cond)
13157 		  {
13158 		    if (cp_lexer_next_token_is_keyword (parser->lexer,
13159 							RID_IF)
13160 			&& chain == NULL)
13161 		      {
13162 			/* We've got "if (COND) else if (COND2)".  Start
13163 			   the condition chain and add COND as the first
13164 			   element.  */
13165 			chain = new vec<tree> ();
13166 			if (!CONSTANT_CLASS_P (condition)
13167 			    && !TREE_SIDE_EFFECTS (condition))
13168 			{
13169 			  /* Wrap it in a NOP_EXPR so that we can set the
13170 			     location of the condition.  */
13171 			  tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13172 					   condition);
13173 			  SET_EXPR_LOCATION (e, token->location);
13174 			  chain->safe_push (e);
13175 			}
13176 		      }
13177 		    else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13178 							      RID_IF))
13179 		      /* This is if-else without subsequent if.  Zap the
13180 			 condition chain; we would have already warned at
13181 			 this point.  */
13182 		      vec_free (chain);
13183 		  }
13184 		begin_else_clause (statement);
13185 		/* Parse the else-clause.  */
13186 		cp_parser_implicitly_scoped_statement (parser, NULL,
13187 						       guard_tinfo, chain);
13188 
13189 		finish_else_clause (statement);
13190 
13191 		/* If we are currently parsing a then-clause, then
13192 		   IF_P will not be NULL.  We set it to true to
13193 		   indicate that this if statement has an else clause.
13194 		   This may trigger the Wparentheses warning below
13195 		   when we get back up to the parent if statement.  */
13196 		if (if_p != NULL)
13197 		  *if_p = true;
13198 
13199 		if (discard_else)
13200 		  {
13201 		    ELSE_CLAUSE (statement) = NULL_TREE;
13202 		    in_discarded_stmt = was_discarded;
13203 		    --c_inhibit_evaluation_warnings;
13204 		  }
13205 	      }
13206 	    else
13207 	      {
13208 		/* This if statement does not have an else clause.  If
13209 		   NESTED_IF is true, then the then-clause has an if
13210 		   statement which does have an else clause.  We warn
13211 		   about the potential ambiguity.  */
13212 		if (nested_if)
13213 		  warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13214 			      "suggest explicit braces to avoid ambiguous"
13215 			      " %<else%>");
13216 		if (warn_duplicated_cond)
13217 		  /* We don't need the condition chain anymore.  */
13218 		  vec_free (chain);
13219 	      }
13220 
13221 	    /* Now we're all done with the if-statement.  */
13222 	    finish_if_stmt (statement);
13223 	  }
13224 	else
13225 	  {
13226 	    bool in_switch_statement_p;
13227 	    unsigned char in_statement;
13228 
13229 	    /* Add the condition.  */
13230 	    finish_switch_cond (condition, statement);
13231 
13232 	    /* Parse the body of the switch-statement.  */
13233 	    in_switch_statement_p = parser->in_switch_statement_p;
13234 	    in_statement = parser->in_statement;
13235 	    parser->in_switch_statement_p = true;
13236 	    parser->in_statement |= IN_SWITCH_STMT;
13237 	    cp_parser_implicitly_scoped_statement (parser, if_p,
13238 						   guard_tinfo);
13239 	    parser->in_switch_statement_p = in_switch_statement_p;
13240 	    parser->in_statement = in_statement;
13241 
13242 	    /* Now we're all done with the switch-statement.  */
13243 	    finish_switch_stmt (statement);
13244 	  }
13245 
13246 	return statement;
13247       }
13248       break;
13249 
13250     default:
13251       cp_parser_error (parser, "expected selection-statement");
13252       return error_mark_node;
13253     }
13254 }
13255 
13256 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13257    If we have seen at least one decl-specifier, and the next token is not
13258    a parenthesis (after "int (" we might be looking at a functional cast)
13259    neither we are dealing with a concept-check expression then we must be
13260    looking at a declaration.  */
13261 
13262 static void
cp_parser_maybe_commit_to_declaration(cp_parser * parser,cp_decl_specifier_seq * decl_specs)13263 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13264 				       cp_decl_specifier_seq *decl_specs)
13265 {
13266   if (decl_specs->any_specifiers_p
13267       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13268       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13269       && !cp_parser_error_occurred (parser)
13270       && !(decl_specs->type
13271 	   && TREE_CODE (decl_specs->type) == TYPE_DECL
13272 	   && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13273     cp_parser_commit_to_tentative_parse (parser);
13274 }
13275 
13276 /* Helper function for cp_parser_condition.  Enforces [stmt.stmt]/2:
13277    The declarator shall not specify a function or an array.  Returns
13278    TRUE if the declarator is valid, FALSE otherwise.  */
13279 
13280 static bool
cp_parser_check_condition_declarator(cp_parser * parser,cp_declarator * declarator,location_t loc)13281 cp_parser_check_condition_declarator (cp_parser* parser,
13282                                      cp_declarator *declarator,
13283                                      location_t loc)
13284 {
13285   if (declarator == cp_error_declarator
13286       || function_declarator_p (declarator)
13287       || declarator->kind == cdk_array)
13288     {
13289       if (declarator == cp_error_declarator)
13290 	/* Already complained.  */;
13291       else if (declarator->kind == cdk_array)
13292        error_at (loc, "condition declares an array");
13293       else
13294        error_at (loc, "condition declares a function");
13295       if (parser->fully_implicit_function_template_p)
13296        abort_fully_implicit_template (parser);
13297       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13298                                             /*or_comma=*/false,
13299                                             /*consume_paren=*/false);
13300       return false;
13301     }
13302   else
13303     return true;
13304 }
13305 
13306 /* Parse a condition.
13307 
13308    condition:
13309      expression
13310      type-specifier-seq declarator = initializer-clause
13311      type-specifier-seq declarator braced-init-list
13312 
13313    GNU Extension:
13314 
13315    condition:
13316      type-specifier-seq declarator asm-specification [opt]
13317        attributes [opt] = assignment-expression
13318 
13319    Returns the expression that should be tested.  */
13320 
13321 static tree
cp_parser_condition(cp_parser * parser)13322 cp_parser_condition (cp_parser* parser)
13323 {
13324   cp_decl_specifier_seq type_specifiers;
13325   const char *saved_message;
13326   int declares_class_or_enum;
13327 
13328   /* Try the declaration first.  */
13329   cp_parser_parse_tentatively (parser);
13330   /* New types are not allowed in the type-specifier-seq for a
13331      condition.  */
13332   saved_message = parser->type_definition_forbidden_message;
13333   parser->type_definition_forbidden_message
13334     = G_("types may not be defined in conditions");
13335   /* Parse the type-specifier-seq.  */
13336   cp_parser_decl_specifier_seq (parser,
13337 				CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13338 				&type_specifiers,
13339 				&declares_class_or_enum);
13340   /* Restore the saved message.  */
13341   parser->type_definition_forbidden_message = saved_message;
13342 
13343   /* Gather the attributes that were provided with the
13344      decl-specifiers.  */
13345   tree prefix_attributes = type_specifiers.attributes;
13346 
13347   cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13348 
13349   /* If all is well, we might be looking at a declaration.  */
13350   if (!cp_parser_error_occurred (parser))
13351     {
13352       tree decl;
13353       tree asm_specification;
13354       tree attributes;
13355       cp_declarator *declarator;
13356       tree initializer = NULL_TREE;
13357       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13358 
13359       /* Parse the declarator.  */
13360       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13361 					 CP_PARSER_FLAGS_NONE,
13362 					 /*ctor_dtor_or_conv_p=*/NULL,
13363 					 /*parenthesized_p=*/NULL,
13364 					 /*member_p=*/false,
13365 					 /*friend_p=*/false,
13366 					 /*static_p=*/false);
13367       /* Parse the attributes.  */
13368       attributes = cp_parser_attributes_opt (parser);
13369       /* Parse the asm-specification.  */
13370       asm_specification = cp_parser_asm_specification_opt (parser);
13371       /* If the next token is not an `=' or '{', then we might still be
13372 	 looking at an expression.  For example:
13373 
13374 	   if (A(a).x)
13375 
13376 	 looks like a decl-specifier-seq and a declarator -- but then
13377 	 there is no `=', so this is an expression.  */
13378       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13379 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13380 	cp_parser_simulate_error (parser);
13381 
13382       /* If we did see an `=' or '{', then we are looking at a declaration
13383 	 for sure.  */
13384       if (cp_parser_parse_definitely (parser))
13385 	{
13386 	  tree pushed_scope;
13387 	  bool non_constant_p = false;
13388 	  int flags = LOOKUP_ONLYCONVERTING;
13389 
13390 	  if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13391 	    return error_mark_node;
13392 
13393 	  /* Create the declaration.  */
13394 	  decl = start_decl (declarator, &type_specifiers,
13395 			     /*initialized_p=*/true,
13396 			     attributes, prefix_attributes,
13397 			     &pushed_scope);
13398 
13399 	  declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13400 	  /* Parse the initializer.  */
13401 	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13402 	    {
13403 	      initializer = cp_parser_braced_list (parser, &non_constant_p);
13404 	      CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13405 	      flags = 0;
13406 	    }
13407 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13408 	    {
13409 	      /* Consume the `='.  */
13410 	      cp_lexer_consume_token (parser->lexer);
13411 	      initializer = cp_parser_initializer_clause (parser,
13412 							  &non_constant_p);
13413 	    }
13414 	  else
13415 	    {
13416 	      cp_parser_error (parser, "expected initializer");
13417 	      initializer = error_mark_node;
13418 	    }
13419 	  if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13420 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13421 
13422 	  /* Process the initializer.  */
13423 	  cp_finish_decl (decl,
13424 			  initializer, !non_constant_p,
13425 			  asm_specification,
13426 			  flags);
13427 
13428 	  if (pushed_scope)
13429 	    pop_scope (pushed_scope);
13430 
13431 	  return convert_from_reference (decl);
13432 	}
13433     }
13434   /* If we didn't even get past the declarator successfully, we are
13435      definitely not looking at a declaration.  */
13436   else
13437     cp_parser_abort_tentative_parse (parser);
13438 
13439   /* Otherwise, we are looking at an expression.  */
13440   return cp_parser_expression (parser);
13441 }
13442 
13443 /* Parses a for-statement or range-for-statement until the closing ')',
13444    not included. */
13445 
13446 static tree
cp_parser_for(cp_parser * parser,bool ivdep,unsigned short unroll)13447 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13448 {
13449   tree init, scope, decl;
13450   bool is_range_for;
13451 
13452   /* Begin the for-statement.  */
13453   scope = begin_for_scope (&init);
13454 
13455   /* Maybe parse the optional init-statement in a range-based for loop.  */
13456   if (cp_parser_range_based_for_with_init_p (parser)
13457       /* Checked for diagnostic purposes only.  */
13458       && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13459     {
13460       tree dummy;
13461       cp_parser_init_statement (parser, &dummy);
13462       if (cxx_dialect < cxx20)
13463 	{
13464 	  pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13465 		   OPT_Wc__20_extensions,
13466 		   "range-based %<for%> loops with initializer only "
13467 		   "available with %<-std=c++20%> or %<-std=gnu++20%>");
13468 	  decl = error_mark_node;
13469 	}
13470     }
13471 
13472   /* Parse the initialization.  */
13473   is_range_for = cp_parser_init_statement (parser, &decl);
13474 
13475   if (is_range_for)
13476     return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13477 				false);
13478   else
13479     return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13480 }
13481 
13482 static tree
cp_parser_c_for(cp_parser * parser,tree scope,tree init,bool ivdep,unsigned short unroll)13483 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13484 		 unsigned short unroll)
13485 {
13486   /* Normal for loop */
13487   tree condition = NULL_TREE;
13488   tree expression = NULL_TREE;
13489   tree stmt;
13490 
13491   stmt = begin_for_stmt (scope, init);
13492   /* The init-statement has already been parsed in
13493      cp_parser_init_statement, so no work is needed here.  */
13494   finish_init_stmt (stmt);
13495 
13496   /* If there's a condition, process it.  */
13497   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13498     condition = cp_parser_condition (parser);
13499   else if (ivdep)
13500     {
13501       cp_parser_error (parser, "missing loop condition in loop with "
13502 		       "%<GCC ivdep%> pragma");
13503       condition = error_mark_node;
13504     }
13505   else if (unroll)
13506     {
13507       cp_parser_error (parser, "missing loop condition in loop with "
13508 		       "%<GCC unroll%> pragma");
13509       condition = error_mark_node;
13510     }
13511   finish_for_cond (condition, stmt, ivdep, unroll);
13512   /* Look for the `;'.  */
13513   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13514 
13515   /* If there's an expression, process it.  */
13516   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13517     expression = cp_parser_expression (parser);
13518   finish_for_expr (expression, stmt);
13519 
13520   return stmt;
13521 }
13522 
13523 /* Tries to parse a range-based for-statement:
13524 
13525   range-based-for:
13526     decl-specifier-seq declarator : expression
13527 
13528   The decl-specifier-seq declarator and the `:' are already parsed by
13529   cp_parser_init_statement.  If processing_template_decl it returns a
13530   newly created RANGE_FOR_STMT; if not, it is converted to a
13531   regular FOR_STMT.  */
13532 
13533 static tree
cp_parser_range_for(cp_parser * parser,tree scope,tree init,tree range_decl,bool ivdep,unsigned short unroll,bool is_omp)13534 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13535 		     bool ivdep, unsigned short unroll, bool is_omp)
13536 {
13537   tree stmt, range_expr;
13538   auto_vec <cxx_binding *, 16> bindings;
13539   auto_vec <tree, 16> names;
13540   tree decomp_first_name = NULL_TREE;
13541   unsigned int decomp_cnt = 0;
13542 
13543   /* Get the range declaration momentarily out of the way so that
13544      the range expression doesn't clash with it. */
13545   if (range_decl != error_mark_node)
13546     {
13547       if (DECL_HAS_VALUE_EXPR_P (range_decl))
13548 	{
13549 	  tree v = DECL_VALUE_EXPR (range_decl);
13550 	  /* For decomposition declaration get all of the corresponding
13551 	     declarations out of the way.  */
13552 	  if (TREE_CODE (v) == ARRAY_REF
13553 	      && VAR_P (TREE_OPERAND (v, 0))
13554 	      && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13555 	    {
13556 	      tree d = range_decl;
13557 	      range_decl = TREE_OPERAND (v, 0);
13558 	      decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13559 	      decomp_first_name = d;
13560 	      for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13561 		{
13562 		  tree name = DECL_NAME (d);
13563 		  names.safe_push (name);
13564 		  bindings.safe_push (IDENTIFIER_BINDING (name));
13565 		  IDENTIFIER_BINDING (name)
13566 		    = IDENTIFIER_BINDING (name)->previous;
13567 		}
13568 	    }
13569 	}
13570       if (names.is_empty ())
13571 	{
13572 	  tree name = DECL_NAME (range_decl);
13573 	  names.safe_push (name);
13574 	  bindings.safe_push (IDENTIFIER_BINDING (name));
13575 	  IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13576 	}
13577     }
13578 
13579   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13580     {
13581       bool expr_non_constant_p;
13582       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13583     }
13584   else
13585     range_expr = cp_parser_expression (parser);
13586 
13587   /* Put the range declaration(s) back into scope. */
13588   for (unsigned int i = 0; i < names.length (); i++)
13589     {
13590       cxx_binding *binding = bindings[i];
13591       binding->previous = IDENTIFIER_BINDING (names[i]);
13592       IDENTIFIER_BINDING (names[i]) = binding;
13593     }
13594 
13595   /* finish_omp_for has its own code for the following, so just
13596      return the range_expr instead.  */
13597   if (is_omp)
13598     return range_expr;
13599 
13600   /* If in template, STMT is converted to a normal for-statement
13601      at instantiation. If not, it is done just ahead. */
13602   if (processing_template_decl)
13603     {
13604       if (check_for_bare_parameter_packs (range_expr))
13605 	range_expr = error_mark_node;
13606       stmt = begin_range_for_stmt (scope, init);
13607       if (ivdep)
13608 	RANGE_FOR_IVDEP (stmt) = 1;
13609       if (unroll)
13610 	RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13611       finish_range_for_decl (stmt, range_decl, range_expr);
13612       if (!type_dependent_expression_p (range_expr)
13613 	  /* do_auto_deduction doesn't mess with template init-lists.  */
13614 	  && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13615 	do_range_for_auto_deduction (range_decl, range_expr);
13616     }
13617   else
13618     {
13619       stmt = begin_for_stmt (scope, init);
13620       stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13621 				   decomp_first_name, decomp_cnt, ivdep,
13622 				   unroll);
13623     }
13624   return stmt;
13625 }
13626 
13627 /* Subroutine of cp_convert_range_for: given the initializer expression,
13628    builds up the range temporary.  */
13629 
13630 static tree
build_range_temp(tree range_expr)13631 build_range_temp (tree range_expr)
13632 {
13633   /* Find out the type deduced by the declaration
13634      `auto &&__range = range_expr'.  */
13635   tree auto_node = make_auto ();
13636   tree range_type = cp_build_reference_type (auto_node, true);
13637   range_type = do_auto_deduction (range_type, range_expr, auto_node);
13638 
13639   /* Create the __range variable.  */
13640   tree range_temp = build_decl (input_location, VAR_DECL,
13641 				for_range__identifier, range_type);
13642   TREE_USED (range_temp) = 1;
13643   DECL_ARTIFICIAL (range_temp) = 1;
13644 
13645   return range_temp;
13646 }
13647 
13648 /* Used by cp_parser_range_for in template context: we aren't going to
13649    do a full conversion yet, but we still need to resolve auto in the
13650    type of the for-range-declaration if present.  This is basically
13651    a shortcut version of cp_convert_range_for.  */
13652 
13653 static void
do_range_for_auto_deduction(tree decl,tree range_expr)13654 do_range_for_auto_deduction (tree decl, tree range_expr)
13655 {
13656   tree auto_node = type_uses_auto (TREE_TYPE (decl));
13657   if (auto_node)
13658     {
13659       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13660       range_temp = convert_from_reference (build_range_temp (range_expr));
13661       iter_type = (cp_parser_perform_range_for_lookup
13662 		   (range_temp, &begin_dummy, &end_dummy));
13663       if (iter_type)
13664 	{
13665 	  iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13666 				  iter_type);
13667 	  iter_decl = build_x_indirect_ref (input_location, iter_decl,
13668 					    RO_UNARY_STAR, NULL_TREE,
13669 					    tf_warning_or_error);
13670 	  TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13671 						iter_decl, auto_node,
13672 						tf_warning_or_error,
13673 						adc_variable_type);
13674 	}
13675     }
13676 }
13677 
13678 /* Warns when the loop variable should be changed to a reference type to
13679    avoid unnecessary copying.  I.e., from
13680 
13681      for (const auto x : range)
13682 
13683    where range returns a reference, to
13684 
13685      for (const auto &x : range)
13686 
13687    if this version doesn't make a copy.
13688 
13689   This function also warns when the loop variable is initialized with
13690   a value of a different type resulting in a copy:
13691 
13692      int arr[10];
13693      for (const double &x : arr)
13694 
13695    DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13696    This function is never called when processing_template_decl is on.  */
13697 
13698 static void
warn_for_range_copy(tree decl,tree expr)13699 warn_for_range_copy (tree decl, tree expr)
13700 {
13701   if (!warn_range_loop_construct
13702       || decl == error_mark_node)
13703     return;
13704 
13705   location_t loc = DECL_SOURCE_LOCATION (decl);
13706   tree type = TREE_TYPE (decl);
13707 
13708   if (from_macro_expansion_at (loc))
13709     return;
13710 
13711   if (TYPE_REF_P (type))
13712     {
13713       if (glvalue_p (expr) && !ref_conv_binds_directly_p (type, expr))
13714 	{
13715 	  auto_diagnostic_group d;
13716 	  if (warning_at (loc, OPT_Wrange_loop_construct,
13717 			  "loop variable %qD of type %qT binds to a temporary "
13718 			  "constructed from type %qT", decl, type,
13719 			  TREE_TYPE (expr)))
13720 	    {
13721 	      tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13722 						  TYPE_QUAL_CONST);
13723 	      ref = cp_build_reference_type (ref, /*rval*/false);
13724 	      inform (loc, "use non-reference type %qT to make the copy "
13725 		      "explicit or %qT to prevent copying",
13726 		      non_reference (type), ref);
13727 	    }
13728 	}
13729       return;
13730     }
13731   else if (!CP_TYPE_CONST_P (type))
13732     return;
13733 
13734   /* Since small trivially copyable types are cheap to copy, we suppress the
13735      warning for them.  64B is a common size of a cache line.  */
13736   if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13737       || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13738 	  && trivially_copyable_p (type)))
13739     return;
13740 
13741   tree rtype = cp_build_reference_type (type, /*rval*/false);
13742   /* If we could initialize the reference directly, it wouldn't involve any
13743      copies.  */
13744   if (!ref_conv_binds_directly_p (rtype, expr))
13745     return;
13746 
13747   auto_diagnostic_group d;
13748   if (warning_at (loc, OPT_Wrange_loop_construct,
13749 		  "loop variable %qD creates a copy from type %qT",
13750 		  decl, type))
13751     {
13752       gcc_rich_location richloc (loc);
13753       richloc.add_fixit_insert_before ("&");
13754       inform (&richloc, "use reference type to prevent copying");
13755     }
13756 }
13757 
13758 /* Converts a range-based for-statement into a normal
13759    for-statement, as per the definition.
13760 
13761       for (RANGE_DECL : RANGE_EXPR)
13762 	BLOCK
13763 
13764    should be equivalent to:
13765 
13766       {
13767 	auto &&__range = RANGE_EXPR;
13768 	for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13769 	      __begin != __end;
13770 	      ++__begin)
13771 	  {
13772 	      RANGE_DECL = *__begin;
13773 	      BLOCK
13774 	  }
13775       }
13776 
13777    If RANGE_EXPR is an array:
13778 	BEGIN_EXPR = __range
13779 	END_EXPR = __range + ARRAY_SIZE(__range)
13780    Else if RANGE_EXPR has a member 'begin' or 'end':
13781 	BEGIN_EXPR = __range.begin()
13782 	END_EXPR = __range.end()
13783    Else:
13784 	BEGIN_EXPR = begin(__range)
13785 	END_EXPR = end(__range);
13786 
13787    If __range has a member 'begin' but not 'end', or vice versa, we must
13788    still use the second alternative (it will surely fail, however).
13789    When calling begin()/end() in the third alternative we must use
13790    argument dependent lookup, but always considering 'std' as an associated
13791    namespace.  */
13792 
13793 tree
cp_convert_range_for(tree statement,tree range_decl,tree range_expr,tree decomp_first_name,unsigned int decomp_cnt,bool ivdep,unsigned short unroll)13794 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13795 		      tree decomp_first_name, unsigned int decomp_cnt,
13796 		      bool ivdep, unsigned short unroll)
13797 {
13798   tree begin, end;
13799   tree iter_type, begin_expr, end_expr;
13800   tree condition, expression;
13801 
13802   range_expr = mark_lvalue_use (range_expr);
13803 
13804   if (range_decl == error_mark_node || range_expr == error_mark_node)
13805     /* If an error happened previously do nothing or else a lot of
13806        unhelpful errors would be issued.  */
13807     begin_expr = end_expr = iter_type = error_mark_node;
13808   else
13809     {
13810       tree range_temp;
13811 
13812       if (VAR_P (range_expr)
13813 	  && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13814 	/* Can't bind a reference to an array of runtime bound.  */
13815 	range_temp = range_expr;
13816       else
13817 	{
13818 	  range_temp = build_range_temp (range_expr);
13819 	  pushdecl (range_temp);
13820 	  cp_finish_decl (range_temp, range_expr,
13821 			  /*is_constant_init*/false, NULL_TREE,
13822 			  LOOKUP_ONLYCONVERTING);
13823 	  range_temp = convert_from_reference (range_temp);
13824 	}
13825       iter_type = cp_parser_perform_range_for_lookup (range_temp,
13826 						      &begin_expr, &end_expr);
13827     }
13828 
13829   /* The new for initialization statement.  */
13830   begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13831 		      iter_type);
13832   TREE_USED (begin) = 1;
13833   DECL_ARTIFICIAL (begin) = 1;
13834   pushdecl (begin);
13835   cp_finish_decl (begin, begin_expr,
13836 		  /*is_constant_init*/false, NULL_TREE,
13837 		  LOOKUP_ONLYCONVERTING);
13838 
13839   if (cxx_dialect >= cxx17)
13840     iter_type = cv_unqualified (TREE_TYPE (end_expr));
13841   end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13842   TREE_USED (end) = 1;
13843   DECL_ARTIFICIAL (end) = 1;
13844   pushdecl (end);
13845   cp_finish_decl (end, end_expr,
13846 		  /*is_constant_init*/false, NULL_TREE,
13847 		  LOOKUP_ONLYCONVERTING);
13848 
13849   finish_init_stmt (statement);
13850 
13851   /* The new for condition.  */
13852   condition = build_x_binary_op (input_location, NE_EXPR,
13853 				 begin, ERROR_MARK,
13854 				 end, ERROR_MARK,
13855 				 NULL_TREE, NULL, tf_warning_or_error);
13856   finish_for_cond (condition, statement, ivdep, unroll);
13857 
13858   /* The new increment expression.  */
13859   expression = finish_unary_op_expr (input_location,
13860 				     PREINCREMENT_EXPR, begin,
13861 				     tf_warning_or_error);
13862   finish_for_expr (expression, statement);
13863 
13864   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13865     cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13866 
13867   /* The declaration is initialized with *__begin inside the loop body.  */
13868   tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13869 					   NULL_TREE, tf_warning_or_error);
13870   cp_finish_decl (range_decl, deref_begin,
13871 		  /*is_constant_init*/false, NULL_TREE,
13872 		  LOOKUP_ONLYCONVERTING);
13873   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13874     cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13875 
13876   warn_for_range_copy (range_decl, deref_begin);
13877 
13878   return statement;
13879 }
13880 
13881 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13882    We need to solve both at the same time because the method used
13883    depends on the existence of members begin or end.
13884    Returns the type deduced for the iterator expression.  */
13885 
13886 static tree
cp_parser_perform_range_for_lookup(tree range,tree * begin,tree * end)13887 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13888 {
13889   if (error_operand_p (range))
13890     {
13891       *begin = *end = error_mark_node;
13892       return error_mark_node;
13893     }
13894 
13895   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13896     {
13897       error ("range-based %<for%> expression of type %qT "
13898 	     "has incomplete type", TREE_TYPE (range));
13899       *begin = *end = error_mark_node;
13900       return error_mark_node;
13901     }
13902   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13903     {
13904       /* If RANGE is an array, we will use pointer arithmetic.  */
13905       *begin = decay_conversion (range, tf_warning_or_error);
13906       *end = build_binary_op (input_location, PLUS_EXPR,
13907 			      range,
13908 			      array_type_nelts_top (TREE_TYPE (range)),
13909 			      false);
13910       return TREE_TYPE (*begin);
13911     }
13912   else
13913     {
13914       /* If it is not an array, we must do a bit of magic.  */
13915       tree id_begin, id_end;
13916       tree member_begin, member_end;
13917 
13918       *begin = *end = error_mark_node;
13919 
13920       id_begin = get_identifier ("begin");
13921       id_end = get_identifier ("end");
13922       member_begin = lookup_member (TREE_TYPE (range), id_begin,
13923 				    /*protect=*/2, /*want_type=*/false,
13924 				    tf_warning_or_error);
13925       member_end = lookup_member (TREE_TYPE (range), id_end,
13926 				  /*protect=*/2, /*want_type=*/false,
13927 				  tf_warning_or_error);
13928 
13929       if (member_begin != NULL_TREE && member_end != NULL_TREE)
13930 	{
13931 	  /* Use the member functions.  */
13932 	  *begin = cp_parser_range_for_member_function (range, id_begin);
13933 	  *end = cp_parser_range_for_member_function (range, id_end);
13934 	}
13935       else
13936 	{
13937 	  /* Use global functions with ADL.  */
13938 	  releasing_vec vec;
13939 
13940 	  vec_safe_push (vec, range);
13941 
13942 	  member_begin = perform_koenig_lookup (id_begin, vec,
13943 						tf_warning_or_error);
13944 	  *begin = finish_call_expr (member_begin, &vec, false, true,
13945 				     tf_warning_or_error);
13946 	  member_end = perform_koenig_lookup (id_end, vec,
13947 					      tf_warning_or_error);
13948 	  *end = finish_call_expr (member_end, &vec, false, true,
13949 				   tf_warning_or_error);
13950 	}
13951 
13952       /* Last common checks.  */
13953       if (*begin == error_mark_node || *end == error_mark_node)
13954 	{
13955 	  /* If one of the expressions is an error do no more checks.  */
13956 	  *begin = *end = error_mark_node;
13957 	  return error_mark_node;
13958 	}
13959       else if (type_dependent_expression_p (*begin)
13960 	       || type_dependent_expression_p (*end))
13961 	/* Can happen, when, eg, in a template context, Koenig lookup
13962 	   can't resolve begin/end (c++/58503).  */
13963 	return NULL_TREE;
13964       else
13965 	{
13966 	  tree iter_type = cv_unqualified (TREE_TYPE (*begin));
13967 	  /* The unqualified type of the __begin and __end temporaries should
13968 	     be the same, as required by the multiple auto declaration.  */
13969 	  if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
13970 	    {
13971 	      if (cxx_dialect >= cxx17
13972 		  && (build_x_binary_op (input_location, NE_EXPR,
13973 					 *begin, ERROR_MARK,
13974 					 *end, ERROR_MARK,
13975 					 NULL_TREE, NULL, tf_none)
13976 		      != error_mark_node))
13977 		/* P0184R0 allows __begin and __end to have different types,
13978 		   but make sure they are comparable so we can give a better
13979 		   diagnostic.  */;
13980 	      else
13981 		error ("inconsistent begin/end types in range-based %<for%> "
13982 		       "statement: %qT and %qT",
13983 		       TREE_TYPE (*begin), TREE_TYPE (*end));
13984 	    }
13985 	  return iter_type;
13986 	}
13987     }
13988 }
13989 
13990 /* Helper function for cp_parser_perform_range_for_lookup.
13991    Builds a tree for RANGE.IDENTIFIER().  */
13992 
13993 static tree
cp_parser_range_for_member_function(tree range,tree identifier)13994 cp_parser_range_for_member_function (tree range, tree identifier)
13995 {
13996   tree member, res;
13997 
13998   member = finish_class_member_access_expr (range, identifier,
13999 					    false, tf_warning_or_error);
14000   if (member == error_mark_node)
14001     return error_mark_node;
14002 
14003   releasing_vec vec;
14004   res = finish_call_expr (member, &vec,
14005 			  /*disallow_virtual=*/false,
14006 			  /*koenig_p=*/false,
14007 			  tf_warning_or_error);
14008   return res;
14009 }
14010 
14011 /* Parse an iteration-statement.
14012 
14013    iteration-statement:
14014      while ( condition ) statement
14015      do statement while ( expression ) ;
14016      for ( init-statement condition [opt] ; expression [opt] )
14017        statement
14018 
14019    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
14020 
14021 static tree
cp_parser_iteration_statement(cp_parser * parser,bool * if_p,bool ivdep,unsigned short unroll)14022 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14023 			       unsigned short unroll)
14024 {
14025   cp_token *token;
14026   enum rid keyword;
14027   tree statement;
14028   unsigned char in_statement;
14029   token_indent_info guard_tinfo;
14030 
14031   /* Peek at the next token.  */
14032   token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14033   if (!token)
14034     return error_mark_node;
14035 
14036   guard_tinfo = get_token_indent_info (token);
14037 
14038   /* Remember whether or not we are already within an iteration
14039      statement.  */
14040   in_statement = parser->in_statement;
14041 
14042   /* See what kind of keyword it is.  */
14043   keyword = token->keyword;
14044   switch (keyword)
14045     {
14046     case RID_WHILE:
14047       {
14048 	tree condition;
14049 
14050 	/* Begin the while-statement.  */
14051 	statement = begin_while_stmt ();
14052 	/* Look for the `('.  */
14053 	matching_parens parens;
14054 	parens.require_open (parser);
14055 	/* Parse the condition.  */
14056 	condition = cp_parser_condition (parser);
14057 	finish_while_stmt_cond (condition, statement, ivdep, unroll);
14058 	/* Look for the `)'.  */
14059 	parens.require_close (parser);
14060 	/* Parse the dependent statement.  */
14061 	parser->in_statement = IN_ITERATION_STMT;
14062 	bool prev = note_iteration_stmt_body_start ();
14063 	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14064 	note_iteration_stmt_body_end (prev);
14065 	parser->in_statement = in_statement;
14066 	/* We're done with the while-statement.  */
14067 	finish_while_stmt (statement);
14068       }
14069       break;
14070 
14071     case RID_DO:
14072       {
14073 	tree expression;
14074 
14075 	/* Begin the do-statement.  */
14076 	statement = begin_do_stmt ();
14077 	/* Parse the body of the do-statement.  */
14078 	parser->in_statement = IN_ITERATION_STMT;
14079 	bool prev = note_iteration_stmt_body_start ();
14080 	cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14081 	note_iteration_stmt_body_end (prev);
14082 	parser->in_statement = in_statement;
14083 	finish_do_body (statement);
14084 	/* Look for the `while' keyword.  */
14085 	cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14086 	/* Look for the `('.  */
14087 	matching_parens parens;
14088 	parens.require_open (parser);
14089 	/* Parse the expression.  */
14090 	expression = cp_parser_expression (parser);
14091 	/* We're done with the do-statement.  */
14092 	finish_do_stmt (expression, statement, ivdep, unroll);
14093 	/* Look for the `)'.  */
14094 	parens.require_close (parser);
14095 	/* Look for the `;'.  */
14096 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14097       }
14098       break;
14099 
14100     case RID_FOR:
14101       {
14102 	/* Look for the `('.  */
14103 	matching_parens parens;
14104 	parens.require_open (parser);
14105 
14106 	statement = cp_parser_for (parser, ivdep, unroll);
14107 
14108 	/* Look for the `)'.  */
14109 	parens.require_close (parser);
14110 
14111 	/* Parse the body of the for-statement.  */
14112 	parser->in_statement = IN_ITERATION_STMT;
14113 	bool prev = note_iteration_stmt_body_start ();
14114 	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14115 	note_iteration_stmt_body_end (prev);
14116 	parser->in_statement = in_statement;
14117 
14118 	/* We're done with the for-statement.  */
14119 	finish_for_stmt (statement);
14120       }
14121       break;
14122 
14123     default:
14124       cp_parser_error (parser, "expected iteration-statement");
14125       statement = error_mark_node;
14126       break;
14127     }
14128 
14129   return statement;
14130 }
14131 
14132 /* Parse an init-statement or the declarator of a range-based-for.
14133    Returns true if a range-based-for declaration is seen.
14134 
14135    init-statement:
14136      expression-statement
14137      simple-declaration
14138      alias-declaration  */
14139 
14140 static bool
cp_parser_init_statement(cp_parser * parser,tree * decl)14141 cp_parser_init_statement (cp_parser *parser, tree *decl)
14142 {
14143   /* If the next token is a `;', then we have an empty
14144      expression-statement.  Grammatically, this is also a
14145      simple-declaration, but an invalid one, because it does not
14146      declare anything.  Therefore, if we did not handle this case
14147      specially, we would issue an error message about an invalid
14148      declaration.  */
14149   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14150     {
14151       bool is_range_for = false;
14152       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14153 
14154       /* A colon is used in range-based for.  */
14155       parser->colon_corrects_to_scope_p = false;
14156 
14157       /* We're going to speculatively look for a declaration, falling back
14158 	 to an expression, if necessary.  */
14159       cp_parser_parse_tentatively (parser);
14160       bool expect_semicolon_p = true;
14161       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14162 	{
14163 	  cp_parser_alias_declaration (parser);
14164 	  expect_semicolon_p = false;
14165 	  if (cxx_dialect < cxx23
14166 	      && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14167 	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14168 		     OPT_Wc__23_extensions,
14169 		     "alias-declaration in init-statement only "
14170 		     "available with %<-std=c++23%> or %<-std=gnu++23%>");
14171 	}
14172       else
14173 	/* Parse the declaration.  */
14174 	cp_parser_simple_declaration (parser,
14175 				      /*function_definition_allowed_p=*/false,
14176 				      decl);
14177       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14178       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14179 	{
14180 	  /* It is a range-for, consume the ':'.  */
14181 	  cp_lexer_consume_token (parser->lexer);
14182 	  is_range_for = true;
14183 	  if (cxx_dialect < cxx11)
14184 	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14185 		     OPT_Wc__11_extensions,
14186 		     "range-based %<for%> loops only available with "
14187 		     "%<-std=c++11%> or %<-std=gnu++11%>");
14188 	}
14189       else if (expect_semicolon_p)
14190 	/* The ';' is not consumed yet because we told
14191 	   cp_parser_simple_declaration not to.  */
14192 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14193 
14194       if (cp_parser_parse_definitely (parser))
14195 	return is_range_for;
14196       /* If the tentative parse failed, then we shall need to look for an
14197 	 expression-statement.  */
14198     }
14199   /* If we are here, it is an expression-statement.  */
14200   cp_parser_expression_statement (parser, NULL_TREE);
14201   return false;
14202 }
14203 
14204 /* Parse a jump-statement.
14205 
14206    jump-statement:
14207      break ;
14208      continue ;
14209      return expression [opt] ;
14210      return braced-init-list ;
14211      coroutine-return-statement;
14212      goto identifier ;
14213 
14214    GNU extension:
14215 
14216    jump-statement:
14217      goto * expression ;
14218 
14219    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
14220 
14221 static tree
cp_parser_jump_statement(cp_parser * parser)14222 cp_parser_jump_statement (cp_parser* parser)
14223 {
14224   tree statement = error_mark_node;
14225   cp_token *token;
14226   enum rid keyword;
14227   unsigned char in_statement;
14228 
14229   /* Peek at the next token.  */
14230   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14231   if (!token)
14232     return error_mark_node;
14233 
14234   /* See what kind of keyword it is.  */
14235   keyword = token->keyword;
14236   switch (keyword)
14237     {
14238     case RID_BREAK:
14239       in_statement = parser->in_statement & ~IN_IF_STMT;
14240       switch (in_statement)
14241 	{
14242 	case 0:
14243 	  error_at (token->location, "break statement not within loop or switch");
14244 	  break;
14245 	default:
14246 	  gcc_assert ((in_statement & IN_SWITCH_STMT)
14247 		      || in_statement == IN_ITERATION_STMT);
14248 	  statement = finish_break_stmt ();
14249 	  if (in_statement == IN_ITERATION_STMT)
14250 	    break_maybe_infinite_loop ();
14251 	  break;
14252 	case IN_OMP_BLOCK:
14253 	  error_at (token->location, "invalid exit from OpenMP structured block");
14254 	  break;
14255 	case IN_OMP_FOR:
14256 	  error_at (token->location, "break statement used with OpenMP for loop");
14257 	  break;
14258 	}
14259       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14260       break;
14261 
14262     case RID_CONTINUE:
14263       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14264 	{
14265 	case 0:
14266 	  error_at (token->location, "continue statement not within a loop");
14267 	  break;
14268 	  /* Fall through.  */
14269 	case IN_ITERATION_STMT:
14270 	case IN_OMP_FOR:
14271 	  statement = finish_continue_stmt ();
14272 	  break;
14273 	case IN_OMP_BLOCK:
14274 	  error_at (token->location, "invalid exit from OpenMP structured block");
14275 	  break;
14276 	default:
14277 	  gcc_unreachable ();
14278 	}
14279       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14280       break;
14281 
14282     case RID_CO_RETURN:
14283     case RID_RETURN:
14284       {
14285 	tree expr;
14286 	bool expr_non_constant_p;
14287 
14288 	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14289 	  {
14290 	    cp_lexer_set_source_position (parser->lexer);
14291 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14292 	    expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14293 	  }
14294 	else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14295 	  expr = cp_parser_expression (parser);
14296 	else
14297 	  /* If the next token is a `;', then there is no
14298 	     expression.  */
14299 	  expr = NULL_TREE;
14300 	/* Build the return-statement, check co-return first, since type
14301 	   deduction is not valid there.  */
14302 	if (keyword == RID_CO_RETURN)
14303 	  statement = finish_co_return_stmt (token->location, expr);
14304 	else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14305 	  /* Don't deduce from a discarded return statement.  */;
14306 	else
14307 	  statement = finish_return_stmt (expr);
14308 	/* Look for the final `;'.  */
14309 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14310       }
14311       break;
14312 
14313     case RID_GOTO:
14314       if (parser->in_function_body
14315 	  && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14316 	  && cxx_dialect < cxx23)
14317 	{
14318 	  error ("%<goto%> in %<constexpr%> function only available with "
14319 		 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14320 	  cp_function_chain->invalid_constexpr = true;
14321 	}
14322 
14323       /* Create the goto-statement.  */
14324       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14325 	{
14326 	  /* Issue a warning about this use of a GNU extension.  */
14327 	  pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14328 	  /* Consume the '*' token.  */
14329 	  cp_lexer_consume_token (parser->lexer);
14330 	  /* Parse the dependent expression.  */
14331 	  finish_goto_stmt (cp_parser_expression (parser));
14332 	}
14333       else
14334 	finish_goto_stmt (cp_parser_identifier (parser));
14335       /* Look for the final `;'.  */
14336       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14337       break;
14338 
14339     default:
14340       cp_parser_error (parser, "expected jump-statement");
14341       break;
14342     }
14343 
14344   return statement;
14345 }
14346 
14347 /* Parse a declaration-statement.
14348 
14349    declaration-statement:
14350      block-declaration  */
14351 
14352 static void
cp_parser_declaration_statement(cp_parser * parser)14353 cp_parser_declaration_statement (cp_parser* parser)
14354 {
14355   void *p;
14356 
14357   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
14358   p = obstack_alloc (&declarator_obstack, 0);
14359 
14360  /* Parse the block-declaration.  */
14361   cp_parser_block_declaration (parser, /*statement_p=*/true);
14362 
14363   /* Free any declarators allocated.  */
14364   obstack_free (&declarator_obstack, p);
14365 }
14366 
14367 /* Some dependent statements (like `if (cond) statement'), are
14368    implicitly in their own scope.  In other words, if the statement is
14369    a single statement (as opposed to a compound-statement), it is
14370    none-the-less treated as if it were enclosed in braces.  Any
14371    declarations appearing in the dependent statement are out of scope
14372    after control passes that point.  This function parses a statement,
14373    but ensures that is in its own scope, even if it is not a
14374    compound-statement.
14375 
14376    If IF_P is not NULL, *IF_P is set to indicate whether the statement
14377    is a (possibly labeled) if statement which is not enclosed in
14378    braces and has an else clause.  This is used to implement
14379    -Wparentheses.
14380 
14381    CHAIN is a vector of if-else-if conditions.  This is used to implement
14382    -Wduplicated-cond.
14383 
14384    Returns the new statement.  */
14385 
14386 static tree
cp_parser_implicitly_scoped_statement(cp_parser * parser,bool * if_p,const token_indent_info & guard_tinfo,vec<tree> * chain)14387 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14388 				       const token_indent_info &guard_tinfo,
14389 				       vec<tree> *chain)
14390 {
14391   tree statement;
14392   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14393   location_t body_loc_after_labels = UNKNOWN_LOCATION;
14394   token_indent_info body_tinfo
14395     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14396 
14397   if (if_p != NULL)
14398     *if_p = false;
14399 
14400   /* Mark if () ; with a special NOP_EXPR.  */
14401   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14402     {
14403       cp_lexer_consume_token (parser->lexer);
14404       statement = add_stmt (build_empty_stmt (body_loc));
14405 
14406       if (guard_tinfo.keyword == RID_IF
14407 	  && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14408 	warning_at (body_loc, OPT_Wempty_body,
14409 		    "suggest braces around empty body in an %<if%> statement");
14410       else if (guard_tinfo.keyword == RID_ELSE)
14411 	warning_at (body_loc, OPT_Wempty_body,
14412 		    "suggest braces around empty body in an %<else%> statement");
14413     }
14414   /* if a compound is opened, we simply parse the statement directly.  */
14415   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14416     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14417   /* If the token is not a `{', then we must take special action.  */
14418   else
14419     {
14420       /* Create a compound-statement.  */
14421       statement = begin_compound_stmt (0);
14422       /* Parse the dependent-statement.  */
14423       cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14424 			   &body_loc_after_labels);
14425       /* Finish the dummy compound-statement.  */
14426       finish_compound_stmt (statement);
14427     }
14428 
14429   token_indent_info next_tinfo
14430     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14431   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14432 
14433   if (body_loc_after_labels != UNKNOWN_LOCATION
14434       && next_tinfo.type != CPP_SEMICOLON)
14435     warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14436 				    guard_tinfo.location, guard_tinfo.keyword);
14437 
14438   /* Return the statement.  */
14439   return statement;
14440 }
14441 
14442 /* For some dependent statements (like `while (cond) statement'), we
14443    have already created a scope.  Therefore, even if the dependent
14444    statement is a compound-statement, we do not want to create another
14445    scope.  */
14446 
14447 static void
cp_parser_already_scoped_statement(cp_parser * parser,bool * if_p,const token_indent_info & guard_tinfo)14448 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14449 				    const token_indent_info &guard_tinfo)
14450 {
14451   /* If the token is a `{', then we must take special action.  */
14452   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14453     {
14454       token_indent_info body_tinfo
14455 	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14456       location_t loc_after_labels = UNKNOWN_LOCATION;
14457 
14458       cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14459 			   &loc_after_labels);
14460       token_indent_info next_tinfo
14461 	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14462       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14463 
14464       if (loc_after_labels != UNKNOWN_LOCATION
14465 	  && next_tinfo.type != CPP_SEMICOLON)
14466 	warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14467 					guard_tinfo.location,
14468 					guard_tinfo.keyword);
14469     }
14470   else
14471     {
14472       /* Avoid calling cp_parser_compound_statement, so that we
14473 	 don't create a new scope.  Do everything else by hand.  */
14474       matching_braces braces;
14475       braces.require_open (parser);
14476       /* If the next keyword is `__label__' we have a label declaration.  */
14477       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14478 	cp_parser_label_declaration (parser);
14479       /* Parse an (optional) statement-seq.  */
14480       cp_parser_statement_seq_opt (parser, NULL_TREE);
14481       braces.require_close (parser);
14482     }
14483 }
14484 
14485 /* Modules */
14486 
14487 /* Parse a module-name,
14488    identifier
14489    module-name . identifier
14490    header-name
14491 
14492    Returns a pointer to module object, NULL.   */
14493 
14494 static module_state *
cp_parser_module_name(cp_parser * parser)14495 cp_parser_module_name (cp_parser *parser)
14496 {
14497   cp_token *token = cp_lexer_peek_token (parser->lexer);
14498   if (token->type == CPP_HEADER_NAME)
14499     {
14500       cp_lexer_consume_token (parser->lexer);
14501 
14502       return get_module (token->u.value);
14503     }
14504 
14505   module_state *parent = NULL;
14506   bool partitioned = false;
14507   if (token->type == CPP_COLON && named_module_p ())
14508     {
14509       partitioned = true;
14510       cp_lexer_consume_token (parser->lexer);
14511     }
14512 
14513   for (;;)
14514     {
14515       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14516 	{
14517 	  cp_parser_error (parser, "expected module-name");
14518 	  break;
14519 	}
14520 
14521       tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14522       parent = get_module (name, parent, partitioned);
14523       token = cp_lexer_peek_token (parser->lexer);
14524       if (!partitioned && token->type == CPP_COLON)
14525 	partitioned = true;
14526       else if (token->type != CPP_DOT)
14527 	break;
14528 
14529       cp_lexer_consume_token (parser->lexer);
14530    }
14531 
14532   return parent;
14533 }
14534 
14535 /* Named module-declaration
14536      __module ; PRAGMA_EOL
14537      __module private ; PRAGMA_EOL (unimplemented)
14538      [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14539 */
14540 
14541 static module_parse
cp_parser_module_declaration(cp_parser * parser,module_parse mp_state,bool exporting)14542 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14543 			      bool exporting)
14544 {
14545   /* We're a pseudo pragma.  */
14546   parser->lexer->in_pragma = true;
14547   cp_token *token = cp_lexer_consume_token (parser->lexer);
14548 
14549   if (flag_header_unit)
14550     {
14551       error_at (token->location,
14552 		"module-declaration not permitted in header-unit");
14553       goto skip_eol;
14554     }
14555   else if (mp_state == MP_FIRST && !exporting
14556       && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14557     {
14558       /* Start global module fragment.  */
14559       cp_lexer_consume_token (parser->lexer);
14560       module_kind |= MK_GLOBAL;
14561       mp_state = MP_GLOBAL;
14562       cp_parser_require_pragma_eol (parser, token);
14563     }
14564   else if (!exporting
14565 	   && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14566 	   && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14567 	   && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14568     {
14569       cp_lexer_consume_token (parser->lexer);
14570       cp_lexer_consume_token (parser->lexer);
14571       cp_lexer_consume_token (parser->lexer);
14572       cp_parser_require_pragma_eol (parser, token);
14573 
14574       if (!(mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14575 	  || !module_interface_p () || module_partition_p ())
14576 	error_at (token->location,
14577 		  "private module fragment only permitted in purview"
14578 		  " of module interface or partition");
14579       else
14580 	{
14581 	  mp_state = MP_PRIVATE_IMPORTS;
14582 	  sorry_at (token->location, "private module fragment");
14583 	}
14584     }
14585   else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14586     {
14587       /* Neither the first declaration, nor in a GMF.  */
14588       error_at (token->location, "module-declaration only permitted as first"
14589 		" declaration, or ending a global module fragment");
14590     skip_eol:
14591       cp_parser_skip_to_pragma_eol (parser, token);
14592     }
14593   else
14594     {
14595       module_state *mod = cp_parser_module_name (parser);
14596       tree attrs = cp_parser_attributes_opt (parser);
14597 
14598       mp_state = MP_PURVIEW_IMPORTS;
14599       if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14600 	goto skip_eol;
14601 
14602       declare_module (mod, token->location, exporting, attrs, parse_in);
14603       cp_parser_require_pragma_eol (parser, token);
14604     }
14605 
14606   return mp_state;
14607 }
14608 
14609 /* Import-declaration
14610    [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14611 
14612 static void
cp_parser_import_declaration(cp_parser * parser,module_parse mp_state,bool exporting)14613 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14614 			      bool exporting)
14615 {
14616   /* We're a pseudo pragma.  */
14617   parser->lexer->in_pragma = true;
14618   cp_token *token = cp_lexer_consume_token (parser->lexer);
14619 
14620   if (mp_state != MP_PURVIEW_IMPORTS
14621       && mp_state != MP_PRIVATE_IMPORTS
14622       && module_purview_p ()
14623       && !global_purview_p ())
14624     {
14625       error_at (token->location, "post-module-declaration"
14626 		" imports must be contiguous");
14627     note_lexer:
14628       inform (token->location, "perhaps insert a line break, or other"
14629 	      " disambiguation, to prevent this being considered a"
14630 	      " module control-line");
14631     skip_eol:
14632       cp_parser_skip_to_pragma_eol (parser, token);
14633     }
14634   else if (current_scope () != global_namespace)
14635     {
14636       error_at (token->location, "import-declaration must be at global scope");
14637       goto note_lexer;
14638     }
14639   else
14640     {
14641       module_state *mod = cp_parser_module_name (parser);
14642       tree attrs = cp_parser_attributes_opt (parser);
14643 
14644       if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14645 	goto skip_eol;
14646       cp_parser_require_pragma_eol (parser, token);
14647 
14648       if (parser->in_unbraced_linkage_specification_p)
14649 	error_at (token->location, "import cannot appear directly in"
14650 		  " a linkage-specification");
14651 
14652       /* Module-purview imports must not be from source inclusion
14653 	 [cpp.import]/7  */
14654       if (attrs && module_purview_p () && !global_purview_p ()
14655 	  && private_lookup_attribute ("__translated",
14656 				       strlen ("__translated"), attrs))
14657 	error_at (token->location, "post-module-declaration imports"
14658 		  " must not be include-translated");
14659       else if ((mp_state == MP_PURVIEW_IMPORTS
14660 		|| mp_state == MP_PRIVATE_IMPORTS)
14661 	       && !token->main_source_p)
14662 	error_at (token->location, "post-module-declaration imports"
14663 		  " must not be from header inclusion");
14664 
14665       import_module (mod, token->location, exporting, attrs, parse_in);
14666     }
14667 }
14668 
14669 /*  export-declaration.
14670 
14671     export declaration
14672     export { declaration-seq-opt }  */
14673 
14674 static void
cp_parser_module_export(cp_parser * parser)14675 cp_parser_module_export (cp_parser *parser)
14676 {
14677   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14678   cp_token *token = cp_lexer_consume_token (parser->lexer);
14679 
14680   if (!module_interface_p ())
14681     error_at (token->location,
14682 	      "%qE may only occur after a module interface declaration",
14683 	      token->u.value);
14684 
14685   bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14686 
14687   unsigned mk = module_kind;
14688   if (module_exporting_p ())
14689     error_at (token->location,
14690 	      "%qE may only occur once in an export declaration",
14691 	      token->u.value);
14692   module_kind |= MK_EXPORTING;
14693 
14694   if (braced)
14695     {
14696       cp_ensure_no_omp_declare_simd (parser);
14697       cp_ensure_no_oacc_routine (parser);
14698 
14699       cp_lexer_consume_token (parser->lexer);
14700       cp_parser_declaration_seq_opt (parser);
14701       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14702     }
14703   else
14704     {
14705       /* Explicitly check if the next tokens might be a
14706          module-directive line, so we can give a clearer error message
14707          about why the directive will be rejected.  */
14708       if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14709 	  || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14710 	  || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14711 	error_at (token->location, "%<export%> not part of following"
14712 		  " module-directive");
14713       cp_parser_declaration (parser, NULL_TREE);
14714     }
14715 
14716   module_kind = mk;
14717 }
14718 
14719 /* Declarations [gram.dcl.dcl] */
14720 
14721 /* Parse an optional declaration-sequence.  TOP_LEVEL is true, if this
14722    is the top-level declaration sequence.  That affects whether we
14723    deal with module-preamble.
14724 
14725    declaration-seq:
14726      declaration
14727      declaration-seq declaration  */
14728 
14729 static void
cp_parser_declaration_seq_opt(cp_parser * parser)14730 cp_parser_declaration_seq_opt (cp_parser* parser)
14731 {
14732   while (true)
14733     {
14734       cp_token *token = cp_lexer_peek_token (parser->lexer);
14735 
14736       if (token->type == CPP_CLOSE_BRACE
14737 	  || token->type == CPP_EOF)
14738 	break;
14739       else
14740 	cp_parser_toplevel_declaration (parser);
14741     }
14742 }
14743 
14744 /* Parse a declaration.
14745 
14746    declaration:
14747      block-declaration
14748      function-definition
14749      template-declaration
14750      explicit-instantiation
14751      explicit-specialization
14752      linkage-specification
14753      namespace-definition
14754 
14755    C++17:
14756      deduction-guide
14757 
14758    modules:
14759      (all these are only allowed at the outermost level, check
14760    	that semantically, for better diagnostics)
14761      module-declaration
14762      module-export-declaration
14763      module-import-declaration
14764      export-declaration
14765 
14766    GNU extension:
14767 
14768    declaration:
14769       __extension__ declaration */
14770 
14771 static void
cp_parser_declaration(cp_parser * parser,tree prefix_attrs)14772 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14773 {
14774   int saved_pedantic;
14775 
14776   /* Check for the `__extension__' keyword.  */
14777   if (cp_parser_extension_opt (parser, &saved_pedantic))
14778     {
14779       /* Parse the qualified declaration.  */
14780       cp_parser_declaration (parser, prefix_attrs);
14781       /* Restore the PEDANTIC flag.  */
14782       pedantic = saved_pedantic;
14783 
14784       return;
14785     }
14786 
14787   /* Try to figure out what kind of declaration is present.  */
14788   cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14789   cp_token *token2 = (token1->type == CPP_EOF
14790 		      ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14791 
14792   if (token1->type == CPP_SEMICOLON)
14793     {
14794       cp_lexer_consume_token (parser->lexer);
14795       /* A declaration consisting of a single semicolon is invalid
14796        * before C++11.  Allow it unless we're being pedantic.  */
14797       if (cxx_dialect < cxx11)
14798 	pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14799       return;
14800     }
14801   else if (cp_lexer_nth_token_is (parser->lexer,
14802 				  cp_parser_skip_std_attribute_spec_seq (parser,
14803 									 1),
14804 				  CPP_SEMICOLON))
14805     {
14806       location_t attrs_loc = token1->location;
14807       tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14808 
14809       if (std_attrs && (flag_openmp || flag_openmp_simd))
14810 	{
14811 	  gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14812 	  std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14813 								 std_attrs);
14814 	  if (parser->lexer->in_omp_attribute_pragma)
14815 	    {
14816 	      cp_lexer *lexer = parser->lexer;
14817 	      while (parser->lexer->in_omp_attribute_pragma)
14818 		{
14819 		  gcc_assert (cp_lexer_next_token_is (parser->lexer,
14820 						      CPP_PRAGMA));
14821 		  cp_parser_pragma (parser, pragma_external, NULL);
14822 		}
14823 	      cp_lexer_destroy (lexer);
14824 	    }
14825 	}
14826 
14827       if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
14828 	warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14829 		    OPT_Wattributes, "attribute ignored");
14830       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14831 	cp_lexer_consume_token (parser->lexer);
14832       return;
14833     }
14834 
14835   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
14836   void *p = obstack_alloc (&declarator_obstack, 0);
14837 
14838   tree attributes = NULL_TREE;
14839 
14840   /* Conditionally, allow attributes to precede a linkage specification.  */
14841   if (token1->keyword == RID_ATTRIBUTE)
14842     {
14843       cp_lexer_save_tokens (parser->lexer);
14844       attributes = cp_parser_attributes_opt (parser);
14845       cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14846       cp_token *t2 = (t1->type == CPP_EOF
14847 		      ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14848       if (t1->keyword == RID_EXTERN
14849 	  && cp_parser_is_pure_string_literal (t2))
14850 	{
14851 	  cp_lexer_commit_tokens (parser->lexer);
14852 	  /* We might have already been here.  */
14853 	  if (!c_dialect_objc ())
14854 	    {
14855 	      location_t where = get_finish (t2->location);
14856 	      warning_at (token1->location, OPT_Wattributes, "attributes are"
14857 			  " not permitted in this position");
14858 	      where = linemap_position_for_loc_and_offset (line_table,
14859 							   where, 1);
14860 	      inform (where, "attributes may be inserted here");
14861 	      attributes = NULL_TREE;
14862 	    }
14863 	  token1 = t1;
14864 	  token2 = t2;
14865 	}
14866       else
14867 	{
14868 	  cp_lexer_rollback_tokens (parser->lexer);
14869 	  attributes = NULL_TREE;
14870 	}
14871     }
14872   /* If we already had some attributes, and we've added more, then prepend.
14873      Otherwise attributes just contains any that we just read.  */
14874   if (prefix_attrs)
14875     {
14876       if (attributes)
14877 	TREE_CHAIN (prefix_attrs) = attributes;
14878       attributes = prefix_attrs;
14879     }
14880 
14881   /* If the next token is `extern' and the following token is a string
14882      literal, then we have a linkage specification.  */
14883   if (token1->keyword == RID_EXTERN
14884       && cp_parser_is_pure_string_literal (token2))
14885     cp_parser_linkage_specification (parser, attributes);
14886   /* If the next token is `template', then we have either a template
14887      declaration, an explicit instantiation, or an explicit
14888      specialization.  */
14889   else if (token1->keyword == RID_TEMPLATE)
14890     {
14891       /* `template <>' indicates a template specialization.  */
14892       if (token2->type == CPP_LESS
14893 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14894 	cp_parser_explicit_specialization (parser);
14895       /* `template <' indicates a template declaration.  */
14896       else if (token2->type == CPP_LESS)
14897 	cp_parser_template_declaration (parser, /*member_p=*/false);
14898       /* Anything else must be an explicit instantiation.  */
14899       else
14900 	cp_parser_explicit_instantiation (parser);
14901     }
14902   /* If the next token is `export', it's new-style modules or
14903      old-style template.  */
14904   else if (token1->keyword == RID_EXPORT)
14905     {
14906       if (!modules_p ())
14907 	cp_parser_template_declaration (parser, /*member_p=*/false);
14908       else
14909 	cp_parser_module_export (parser);
14910     }
14911   else if (token1->keyword == RID__EXPORT
14912 	   || token1->keyword == RID__IMPORT
14913 	   || token1->keyword == RID__MODULE)
14914     {
14915       bool exporting = token1->keyword == RID__EXPORT;
14916       cp_token *next = exporting ? token2 : token1;
14917       if (exporting)
14918 	cp_lexer_consume_token (parser->lexer);
14919       if (next->keyword == RID__MODULE)
14920 	cp_parser_module_declaration (parser, MP_NOT_MODULE, exporting);
14921       else
14922 	cp_parser_import_declaration (parser, MP_NOT_MODULE, exporting);
14923     }
14924   /* If the next token is `extern', 'static' or 'inline' and the one
14925      after that is `template', we have a GNU extended explicit
14926      instantiation directive.  */
14927   else if (cp_parser_allow_gnu_extensions_p (parser)
14928 	   && token2->keyword == RID_TEMPLATE
14929 	   && (token1->keyword == RID_EXTERN
14930 	       || token1->keyword == RID_STATIC
14931 	       || token1->keyword == RID_INLINE))
14932     cp_parser_explicit_instantiation (parser);
14933   /* If the next token is `namespace', check for a named or unnamed
14934      namespace definition.  */
14935   else if (token1->keyword == RID_NAMESPACE
14936 	   && (/* A named namespace definition.  */
14937 	       (token2->type == CPP_NAME
14938 		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
14939 		    != CPP_EQ))
14940                || (token2->type == CPP_OPEN_SQUARE
14941                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
14942                    == CPP_OPEN_SQUARE)
14943 	       /* An unnamed namespace definition.  */
14944 	       || token2->type == CPP_OPEN_BRACE
14945 	       || token2->keyword == RID_ATTRIBUTE))
14946     cp_parser_namespace_definition (parser);
14947   /* An inline (associated) namespace definition.  */
14948   else if (token2->keyword == RID_NAMESPACE
14949 	   && token1->keyword == RID_INLINE)
14950     cp_parser_namespace_definition (parser);
14951   /* Objective-C++ declaration/definition.  */
14952   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
14953     cp_parser_objc_declaration (parser, attributes);
14954   else if (c_dialect_objc ()
14955 	   && token1->keyword == RID_ATTRIBUTE
14956 	   && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
14957     cp_parser_objc_declaration (parser, attributes);
14958   /* At this point we may have a template declared by a concept
14959      introduction.  */
14960   else if (flag_concepts
14961 	   && cp_parser_template_declaration_after_export (parser,
14962 							   /*member_p=*/false))
14963     /* We did.  */;
14964   else
14965     /* Try to parse a block-declaration, or a function-definition.  */
14966     cp_parser_block_declaration (parser, /*statement_p=*/false);
14967 
14968   /* Free any declarators allocated.  */
14969   obstack_free (&declarator_obstack, p);
14970 }
14971 
14972 /* Parse a namespace-scope declaration.  */
14973 
14974 static void
cp_parser_toplevel_declaration(cp_parser * parser)14975 cp_parser_toplevel_declaration (cp_parser* parser)
14976 {
14977   cp_token *token = cp_lexer_peek_token (parser->lexer);
14978 
14979   if (token->type == CPP_PRAGMA)
14980     /* A top-level declaration can consist solely of a #pragma.  A
14981        nested declaration cannot, so this is done here and not in
14982        cp_parser_declaration.  (A #pragma at block scope is
14983        handled in cp_parser_statement.)  */
14984     cp_parser_pragma (parser, pragma_external, NULL);
14985   else
14986     /* Parse the declaration itself.  */
14987     cp_parser_declaration (parser, NULL_TREE);
14988 }
14989 
14990 /* Parse a block-declaration.
14991 
14992    block-declaration:
14993      simple-declaration
14994      asm-definition
14995      namespace-alias-definition
14996      using-declaration
14997      using-directive
14998 
14999    GNU Extension:
15000 
15001    block-declaration:
15002      __extension__ block-declaration
15003 
15004    C++0x Extension:
15005 
15006    block-declaration:
15007      static_assert-declaration
15008 
15009    If STATEMENT_P is TRUE, then this block-declaration is occurring as
15010    part of a declaration-statement.  */
15011 
15012 static void
cp_parser_block_declaration(cp_parser * parser,bool statement_p)15013 cp_parser_block_declaration (cp_parser *parser,
15014 			     bool      statement_p)
15015 {
15016   int saved_pedantic;
15017 
15018   /* Check for the `__extension__' keyword.  */
15019   if (cp_parser_extension_opt (parser, &saved_pedantic))
15020     {
15021       /* Parse the qualified declaration.  */
15022       cp_parser_block_declaration (parser, statement_p);
15023       /* Restore the PEDANTIC flag.  */
15024       pedantic = saved_pedantic;
15025 
15026       return;
15027     }
15028 
15029   /* Peek at the next token to figure out which kind of declaration is
15030      present.  */
15031   cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15032   size_t attr_idx;
15033 
15034   /* If the next keyword is `asm', we have an asm-definition.  */
15035   if (token1->keyword == RID_ASM)
15036     {
15037       if (statement_p)
15038 	cp_parser_commit_to_tentative_parse (parser);
15039       cp_parser_asm_definition (parser);
15040     }
15041   /* If the next keyword is `namespace', we have a
15042      namespace-alias-definition.  */
15043   else if (token1->keyword == RID_NAMESPACE)
15044     cp_parser_namespace_alias_definition (parser);
15045   /* If the next keyword is `using', we have a
15046      using-declaration, a using-directive, or an alias-declaration.  */
15047   else if (token1->keyword == RID_USING)
15048     {
15049       cp_token *token2;
15050 
15051       if (statement_p)
15052 	cp_parser_commit_to_tentative_parse (parser);
15053       /* If the token after `using' is `namespace', then we have a
15054 	 using-directive.  */
15055       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15056       if (token2->keyword == RID_NAMESPACE)
15057 	cp_parser_using_directive (parser);
15058       else if (token2->keyword == RID_ENUM)
15059 	cp_parser_using_enum (parser);
15060       /* If the second token after 'using' is '=', then we have an
15061 	 alias-declaration.  */
15062       else if (cxx_dialect >= cxx11
15063 	       && token2->type == CPP_NAME
15064 	       && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15065 		   || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15066 	cp_parser_alias_declaration (parser);
15067       /* Otherwise, it's a using-declaration.  */
15068       else
15069 	cp_parser_using_declaration (parser,
15070 				     /*access_declaration_p=*/false);
15071     }
15072   /* If the next keyword is `__label__' we have a misplaced label
15073      declaration.  */
15074   else if (token1->keyword == RID_LABEL)
15075     {
15076       cp_lexer_consume_token (parser->lexer);
15077       error_at (token1->location, "%<__label__%> not at the beginning of a block");
15078       cp_parser_skip_to_end_of_statement (parser);
15079       /* If the next token is now a `;', consume it.  */
15080       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15081 	cp_lexer_consume_token (parser->lexer);
15082     }
15083   /* If the next token is `static_assert' we have a static assertion.  */
15084   else if (token1->keyword == RID_STATIC_ASSERT)
15085     cp_parser_static_assert (parser, /*member_p=*/false);
15086   /* If the next tokens after attributes is `using namespace', then we have
15087      a using-directive.  */
15088   else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15089 	   && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15090 					     RID_USING)
15091 	   && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15092 					     RID_NAMESPACE))
15093     {
15094       if (statement_p)
15095 	cp_parser_commit_to_tentative_parse (parser);
15096       cp_parser_using_directive (parser);
15097     }
15098   /* Anything else must be a simple-declaration.  */
15099   else
15100     cp_parser_simple_declaration (parser, !statement_p,
15101 				  /*maybe_range_for_decl*/NULL);
15102 }
15103 
15104 /* Parse a simple-declaration.
15105 
15106    simple-declaration:
15107      decl-specifier-seq [opt] init-declarator-list [opt] ;
15108      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15109        brace-or-equal-initializer ;
15110 
15111    init-declarator-list:
15112      init-declarator
15113      init-declarator-list , init-declarator
15114 
15115    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15116    function-definition as a simple-declaration.
15117 
15118    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15119    parsed declaration if it is an uninitialized single declarator not followed
15120    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15121    if present, will not be consumed.  */
15122 
15123 static void
cp_parser_simple_declaration(cp_parser * parser,bool function_definition_allowed_p,tree * maybe_range_for_decl)15124 cp_parser_simple_declaration (cp_parser* parser,
15125 			      bool function_definition_allowed_p,
15126 			      tree *maybe_range_for_decl)
15127 {
15128   cp_decl_specifier_seq decl_specifiers;
15129   int declares_class_or_enum;
15130   bool saw_declarator;
15131   location_t comma_loc = UNKNOWN_LOCATION;
15132   location_t init_loc = UNKNOWN_LOCATION;
15133 
15134   if (maybe_range_for_decl)
15135     *maybe_range_for_decl = NULL_TREE;
15136 
15137   /* Defer access checks until we know what is being declared; the
15138      checks for names appearing in the decl-specifier-seq should be
15139      done as if we were in the scope of the thing being declared.  */
15140   push_deferring_access_checks (dk_deferred);
15141 
15142   /* Parse the decl-specifier-seq.  We have to keep track of whether
15143      or not the decl-specifier-seq declares a named class or
15144      enumeration type, since that is the only case in which the
15145      init-declarator-list is allowed to be empty.
15146 
15147      [dcl.dcl]
15148 
15149      In a simple-declaration, the optional init-declarator-list can be
15150      omitted only when declaring a class or enumeration, that is when
15151      the decl-specifier-seq contains either a class-specifier, an
15152      elaborated-type-specifier, or an enum-specifier.  */
15153   cp_parser_decl_specifier_seq (parser,
15154 				CP_PARSER_FLAGS_OPTIONAL,
15155 				&decl_specifiers,
15156 				&declares_class_or_enum);
15157   /* We no longer need to defer access checks.  */
15158   stop_deferring_access_checks ();
15159 
15160   cp_omp_declare_simd_data odsd;
15161   if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15162     cp_parser_handle_directive_omp_attributes (parser,
15163 					       &decl_specifiers.attributes,
15164 					       &odsd, true);
15165 
15166   /* In a block scope, a valid declaration must always have a
15167      decl-specifier-seq.  By not trying to parse declarators, we can
15168      resolve the declaration/expression ambiguity more quickly.  */
15169   if (!function_definition_allowed_p
15170       && !decl_specifiers.any_specifiers_p)
15171     {
15172       cp_parser_error (parser, "expected declaration");
15173       goto done;
15174     }
15175 
15176   /* If the next two tokens are both identifiers, the code is
15177      erroneous. The usual cause of this situation is code like:
15178 
15179        T t;
15180 
15181      where "T" should name a type -- but does not.  */
15182   if (!decl_specifiers.any_type_specifiers_p
15183       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15184     {
15185       /* If parsing tentatively, we should commit; we really are
15186 	 looking at a declaration.  */
15187       cp_parser_commit_to_tentative_parse (parser);
15188       /* Give up.  */
15189       goto done;
15190     }
15191 
15192   cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15193 
15194   /* Look for C++17 decomposition declaration.  */
15195   for (size_t n = 1; ; n++)
15196     if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15197 	|| cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15198       continue;
15199     else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15200 	     && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15201 	     && decl_specifiers.any_specifiers_p)
15202       {
15203 	tree decl
15204 	  = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15205 						 maybe_range_for_decl,
15206 						 &init_loc);
15207 
15208 	/* The next token should be either a `,' or a `;'.  */
15209 	cp_token *token = cp_lexer_peek_token (parser->lexer);
15210 	/* If it's a `;', we are done.  */
15211 	if (token->type == CPP_SEMICOLON)
15212 	  goto finish;
15213 	else if (maybe_range_for_decl)
15214 	  {
15215 	    if (*maybe_range_for_decl == NULL_TREE)
15216 	      *maybe_range_for_decl = error_mark_node;
15217 	    goto finish;
15218 	  }
15219 	/* Anything else is an error.  */
15220 	else
15221 	  {
15222 	    /* If we have already issued an error message we don't need
15223 	       to issue another one.  */
15224 	    if ((decl != error_mark_node
15225 		 && DECL_INITIAL (decl) != error_mark_node)
15226 		|| cp_parser_uncommitted_to_tentative_parse_p (parser))
15227 	      cp_parser_error (parser, "expected %<;%>");
15228 	    /* Skip tokens until we reach the end of the statement.  */
15229 	    cp_parser_skip_to_end_of_statement (parser);
15230 	    /* If the next token is now a `;', consume it.  */
15231 	    if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15232 	      cp_lexer_consume_token (parser->lexer);
15233 	    goto done;
15234 	  }
15235       }
15236     else
15237       break;
15238 
15239   tree last_type;
15240   bool auto_specifier_p;
15241   /* NULL_TREE if both variable and function declaration are allowed,
15242      error_mark_node if function declaration are not allowed and
15243      a FUNCTION_DECL that should be diagnosed if it is followed by
15244      variable declarations.  */
15245   tree auto_function_declaration;
15246 
15247   last_type = NULL_TREE;
15248   auto_specifier_p
15249     = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15250   auto_function_declaration = NULL_TREE;
15251 
15252   /* Keep going until we hit the `;' at the end of the simple
15253      declaration.  */
15254   saw_declarator = false;
15255   while (cp_lexer_next_token_is_not (parser->lexer,
15256 				     CPP_SEMICOLON))
15257     {
15258       cp_token *token;
15259       bool function_definition_p;
15260       tree decl;
15261       tree auto_result = NULL_TREE;
15262 
15263       if (saw_declarator)
15264 	{
15265 	  /* If we are processing next declarator, comma is expected */
15266 	  token = cp_lexer_peek_token (parser->lexer);
15267 	  gcc_assert (token->type == CPP_COMMA);
15268 	  cp_lexer_consume_token (parser->lexer);
15269 	  if (maybe_range_for_decl)
15270 	    {
15271 	      *maybe_range_for_decl = error_mark_node;
15272 	      if (comma_loc == UNKNOWN_LOCATION)
15273 		comma_loc = token->location;
15274 	    }
15275 	}
15276       else
15277 	saw_declarator = true;
15278 
15279       /* Parse the init-declarator.  */
15280       decl = cp_parser_init_declarator (parser,
15281 					CP_PARSER_FLAGS_NONE,
15282 					&decl_specifiers,
15283 					/*checks=*/NULL,
15284 					function_definition_allowed_p,
15285 					/*member_p=*/false,
15286 					declares_class_or_enum,
15287 					&function_definition_p,
15288 					maybe_range_for_decl,
15289 					&init_loc,
15290 					&auto_result);
15291       /* If an error occurred while parsing tentatively, exit quickly.
15292 	 (That usually happens when in the body of a function; each
15293 	 statement is treated as a declaration-statement until proven
15294 	 otherwise.)  */
15295       if (cp_parser_error_occurred (parser))
15296 	goto done;
15297 
15298       if (auto_specifier_p && cxx_dialect >= cxx14)
15299 	{
15300 	  /* If the init-declarator-list contains more than one
15301 	     init-declarator, they shall all form declarations of
15302 	     variables.  */
15303 	  if (auto_function_declaration == NULL_TREE)
15304 	    auto_function_declaration
15305 	      = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15306 	  else if (TREE_CODE (decl) == FUNCTION_DECL
15307 		   || auto_function_declaration != error_mark_node)
15308 	    {
15309 	      error_at (decl_specifiers.locations[ds_type_spec],
15310 			"non-variable %qD in declaration with more than one "
15311 			"declarator with placeholder type",
15312 			TREE_CODE (decl) == FUNCTION_DECL
15313 			? decl : auto_function_declaration);
15314 	      auto_function_declaration = error_mark_node;
15315 	    }
15316 	}
15317 
15318       if (auto_result
15319 	  && (!processing_template_decl || !type_uses_auto (auto_result)))
15320 	{
15321 	  if (last_type
15322 	      && last_type != error_mark_node
15323 	      && !same_type_p (auto_result, last_type))
15324 	    {
15325 	      /* If the list of declarators contains more than one declarator,
15326 		 the type of each declared variable is determined as described
15327 		 above. If the type deduced for the template parameter U is not
15328 		 the same in each deduction, the program is ill-formed.  */
15329 	      error_at (decl_specifiers.locations[ds_type_spec],
15330 			"inconsistent deduction for %qT: %qT and then %qT",
15331 			decl_specifiers.type, last_type, auto_result);
15332 	      last_type = error_mark_node;
15333 	    }
15334 	  else
15335 	    last_type = auto_result;
15336 	}
15337 
15338       /* Handle function definitions specially.  */
15339       if (function_definition_p)
15340 	{
15341 	  /* If the next token is a `,', then we are probably
15342 	     processing something like:
15343 
15344 	       void f() {}, *p;
15345 
15346 	     which is erroneous.  */
15347 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15348 	    {
15349 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
15350 	      error_at (token->location,
15351 			"mixing"
15352 			" declarations and function-definitions is forbidden");
15353 	    }
15354 	  /* Otherwise, we're done with the list of declarators.  */
15355 	  else
15356 	    {
15357 	      pop_deferring_access_checks ();
15358 	      cp_finalize_omp_declare_simd (parser, &odsd);
15359 	      return;
15360 	    }
15361 	}
15362       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15363 	*maybe_range_for_decl = decl;
15364       /* The next token should be either a `,' or a `;'.  */
15365       token = cp_lexer_peek_token (parser->lexer);
15366       /* If it's a `,', there are more declarators to come.  */
15367       if (token->type == CPP_COMMA)
15368 	/* will be consumed next time around */;
15369       /* If it's a `;', we are done.  */
15370       else if (token->type == CPP_SEMICOLON)
15371 	break;
15372       else if (maybe_range_for_decl)
15373 	{
15374 	  if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15375 	    permerror (decl_specifiers.locations[ds_type_spec],
15376 		       "types may not be defined in a for-range-declaration");
15377 	  break;
15378 	}
15379       /* Anything else is an error.  */
15380       else
15381 	{
15382 	  /* If we have already issued an error message we don't need
15383 	     to issue another one.  */
15384 	  if ((decl != error_mark_node
15385 	       && DECL_INITIAL (decl) != error_mark_node)
15386 	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
15387 	    cp_parser_error (parser, "expected %<,%> or %<;%>");
15388 	  /* Skip tokens until we reach the end of the statement.  */
15389 	  cp_parser_skip_to_end_of_statement (parser);
15390 	  /* If the next token is now a `;', consume it.  */
15391 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15392 	    cp_lexer_consume_token (parser->lexer);
15393 	  goto done;
15394 	}
15395       /* After the first time around, a function-definition is not
15396 	 allowed -- even if it was OK at first.  For example:
15397 
15398 	   int i, f() {}
15399 
15400 	 is not valid.  */
15401       function_definition_allowed_p = false;
15402     }
15403 
15404   /* Issue an error message if no declarators are present, and the
15405      decl-specifier-seq does not itself declare a class or
15406      enumeration: [dcl.dcl]/3.  */
15407   if (!saw_declarator)
15408     {
15409       if (cp_parser_declares_only_class_p (parser))
15410 	{
15411 	  if (!declares_class_or_enum
15412 	      && decl_specifiers.type
15413 	      && OVERLOAD_TYPE_P (decl_specifiers.type))
15414 	    /* Ensure an error is issued anyway when finish_decltype_type,
15415 	       called via cp_parser_decl_specifier_seq, returns a class or
15416 	       an enumeration (c++/51786).  */
15417 	    decl_specifiers.type = NULL_TREE;
15418 	  shadow_tag (&decl_specifiers);
15419 	}
15420       /* Perform any deferred access checks.  */
15421       perform_deferred_access_checks (tf_warning_or_error);
15422     }
15423 
15424   /* Consume the `;'.  */
15425  finish:
15426   if (!maybe_range_for_decl)
15427     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15428   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15429     {
15430       if (init_loc != UNKNOWN_LOCATION)
15431 	error_at (init_loc, "initializer in range-based %<for%> loop");
15432       if (comma_loc != UNKNOWN_LOCATION)
15433 	error_at (comma_loc,
15434 		  "multiple declarations in range-based %<for%> loop");
15435     }
15436 
15437  done:
15438   pop_deferring_access_checks ();
15439   cp_finalize_omp_declare_simd (parser, &odsd);
15440 }
15441 
15442 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15443      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15444        initializer ;  */
15445 
15446 static tree
cp_parser_decomposition_declaration(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,tree * maybe_range_for_decl,location_t * init_loc)15447 cp_parser_decomposition_declaration (cp_parser *parser,
15448 				     cp_decl_specifier_seq *decl_specifiers,
15449 				     tree *maybe_range_for_decl,
15450 				     location_t *init_loc)
15451 {
15452   cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15453   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15454   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15455 
15456   /* Parse the identifier-list.  */
15457   auto_vec<cp_expr, 10> v;
15458   if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15459     while (true)
15460       {
15461 	cp_expr e = cp_parser_identifier (parser);
15462 	if (e.get_value () == error_mark_node)
15463 	  break;
15464 	v.safe_push (e);
15465 	if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15466 	  break;
15467 	cp_lexer_consume_token (parser->lexer);
15468       }
15469 
15470   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15471   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15472     {
15473       end_loc = UNKNOWN_LOCATION;
15474       cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15475 					       false);
15476       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15477 	cp_lexer_consume_token (parser->lexer);
15478       else
15479 	{
15480 	  cp_parser_skip_to_end_of_statement (parser);
15481 	  return error_mark_node;
15482 	}
15483     }
15484 
15485   if (cxx_dialect < cxx17)
15486     pedwarn (loc, OPT_Wc__17_extensions,
15487 	     "structured bindings only available with "
15488 	     "%<-std=c++17%> or %<-std=gnu++17%>");
15489 
15490   tree pushed_scope;
15491   cp_declarator *declarator = make_declarator (cdk_decomp);
15492   loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15493   declarator->id_loc = loc;
15494   if (ref_qual != REF_QUAL_NONE)
15495     declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15496 					    ref_qual == REF_QUAL_RVALUE,
15497 					    NULL_TREE);
15498   tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15499 			  NULL_TREE, decl_specifiers->attributes,
15500 			  &pushed_scope);
15501   tree orig_decl = decl;
15502 
15503   unsigned int i;
15504   cp_expr e;
15505   cp_decl_specifier_seq decl_specs;
15506   clear_decl_specs (&decl_specs);
15507   decl_specs.type = make_auto ();
15508   tree prev = decl;
15509   FOR_EACH_VEC_ELT (v, i, e)
15510     {
15511       if (i == 0)
15512 	declarator = make_id_declarator (NULL_TREE, e.get_value (),
15513 					 sfk_none, e.get_location ());
15514       else
15515 	{
15516 	  declarator->u.id.unqualified_name = e.get_value ();
15517 	  declarator->id_loc = e.get_location ();
15518 	}
15519       tree elt_pushed_scope;
15520       tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15521 			       NULL_TREE, NULL_TREE, &elt_pushed_scope);
15522       if (decl2 == error_mark_node)
15523 	decl = error_mark_node;
15524       else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15525 	{
15526 	  /* Ensure we've diagnosed redeclaration if we aren't creating
15527 	     a new VAR_DECL.  */
15528 	  gcc_assert (errorcount);
15529 	  decl = error_mark_node;
15530 	}
15531       else
15532 	prev = decl2;
15533       if (elt_pushed_scope)
15534 	pop_scope (elt_pushed_scope);
15535     }
15536 
15537   if (v.is_empty ())
15538     {
15539       error_at (loc, "empty structured binding declaration");
15540       decl = error_mark_node;
15541     }
15542 
15543   if (maybe_range_for_decl == NULL
15544       || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15545     {
15546       bool non_constant_p = false, is_direct_init = false;
15547       *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15548       tree initializer = cp_parser_initializer (parser, &is_direct_init,
15549 						&non_constant_p);
15550       if (initializer == NULL_TREE
15551 	  || (TREE_CODE (initializer) == TREE_LIST
15552 	      && TREE_CHAIN (initializer))
15553 	  || (is_direct_init
15554 	      && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15555 	      && CONSTRUCTOR_NELTS (initializer) != 1))
15556 	{
15557 	  error_at (loc, "invalid initializer for structured binding "
15558 		    "declaration");
15559 	  initializer = error_mark_node;
15560 	}
15561 
15562       if (decl != error_mark_node)
15563 	{
15564 	  cp_maybe_mangle_decomp (decl, prev, v.length ());
15565 	  cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15566 			  (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15567 	  cp_finish_decomp (decl, prev, v.length ());
15568 	}
15569     }
15570   else if (decl != error_mark_node)
15571     {
15572       *maybe_range_for_decl = prev;
15573       /* Ensure DECL_VALUE_EXPR is created for all the decls but
15574 	 the underlying DECL.  */
15575       cp_finish_decomp (decl, prev, v.length ());
15576     }
15577 
15578   if (pushed_scope)
15579     pop_scope (pushed_scope);
15580 
15581   if (decl == error_mark_node && DECL_P (orig_decl))
15582     {
15583       if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15584 	SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15585     }
15586 
15587   return decl;
15588 }
15589 
15590 /* Parse a decl-specifier-seq.
15591 
15592    decl-specifier-seq:
15593      decl-specifier-seq [opt] decl-specifier
15594      decl-specifier attribute-specifier-seq [opt] (C++11)
15595 
15596    decl-specifier:
15597      storage-class-specifier
15598      type-specifier
15599      function-specifier
15600      friend
15601      typedef
15602 
15603    GNU Extension:
15604 
15605    decl-specifier:
15606      attributes
15607 
15608    Concepts Extension:
15609 
15610    decl-specifier:
15611      concept
15612 
15613    Set *DECL_SPECS to a representation of the decl-specifier-seq.
15614 
15615    The parser flags FLAGS is used to control type-specifier parsing.
15616 
15617    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15618    flags:
15619 
15620      1: one of the decl-specifiers is an elaborated-type-specifier
15621 	(i.e., a type declaration)
15622      2: one of the decl-specifiers is an enum-specifier or a
15623 	class-specifier (i.e., a type definition)
15624 
15625    */
15626 
15627 static void
cp_parser_decl_specifier_seq(cp_parser * parser,cp_parser_flags flags,cp_decl_specifier_seq * decl_specs,int * declares_class_or_enum)15628 cp_parser_decl_specifier_seq (cp_parser* parser,
15629 			      cp_parser_flags flags,
15630 			      cp_decl_specifier_seq *decl_specs,
15631 			      int* declares_class_or_enum)
15632 {
15633   bool constructor_possible_p = !parser->in_declarator_p;
15634   bool found_decl_spec = false;
15635   cp_token *start_token = NULL;
15636   cp_decl_spec ds;
15637 
15638   /* Clear DECL_SPECS.  */
15639   clear_decl_specs (decl_specs);
15640 
15641   /* Assume no class or enumeration type is declared.  */
15642   *declares_class_or_enum = 0;
15643 
15644   /* Keep reading specifiers until there are no more to read.  */
15645   while (true)
15646     {
15647       bool constructor_p;
15648       cp_token *token;
15649       ds = ds_last;
15650 
15651       /* Peek at the next token.  */
15652       token = cp_lexer_peek_token (parser->lexer);
15653 
15654       /* Save the first token of the decl spec list for error
15655          reporting.  */
15656       if (!start_token)
15657 	start_token = token;
15658       /* Handle attributes.  */
15659       if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15660 	  && cp_next_tokens_can_be_attribute_p (parser))
15661 	{
15662 	  /* Parse the attributes.  */
15663 	  tree attrs = cp_parser_attributes_opt (parser);
15664 
15665 	  /* In a sequence of declaration specifiers, c++11 attributes
15666 	     appertain to the type that precede them. In that case
15667 	     [dcl.spec]/1 says:
15668 
15669 	         The attribute-specifier-seq affects the type only for
15670 		 the declaration it appears in, not other declarations
15671 		 involving the same type.
15672 
15673              But for now let's force the user to position the
15674              attribute either at the beginning of the declaration or
15675              after the declarator-id, which would clearly mean that it
15676              applies to the declarator.  */
15677 	  if (cxx11_attribute_p (attrs))
15678 	    {
15679 	      if (!found_decl_spec)
15680 		/* The c++11 attribute is at the beginning of the
15681 		   declaration.  It appertains to the entity being
15682 		   declared.  */;
15683 	      else
15684 		{
15685 		  if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15686 		    {
15687 		      /*  This is an attribute following a
15688 			  class-specifier.  */
15689 		      if (decl_specs->type_definition_p)
15690 			warn_misplaced_attr_for_class_type (token->location,
15691 							    decl_specs->type);
15692 		      attrs = NULL_TREE;
15693 		    }
15694 		  else
15695 		    {
15696 		      decl_specs->std_attributes
15697 			= attr_chainon (decl_specs->std_attributes, attrs);
15698 		      if (decl_specs->locations[ds_std_attribute] == 0)
15699 			decl_specs->locations[ds_std_attribute] = token->location;
15700 		    }
15701 		  continue;
15702 		}
15703 	    }
15704 
15705 	  decl_specs->attributes
15706 	    = attr_chainon (decl_specs->attributes, attrs);
15707 	  if (decl_specs->locations[ds_attribute] == 0)
15708 	    decl_specs->locations[ds_attribute] = token->location;
15709 	  continue;
15710 	}
15711       /* Assume we will find a decl-specifier keyword.  */
15712       found_decl_spec = true;
15713       /* If the next token is an appropriate keyword, we can simply
15714 	 add it to the list.  */
15715       switch (token->keyword)
15716 	{
15717 	  /* decl-specifier:
15718 	       friend
15719 	       constexpr
15720 	       constinit */
15721 	case RID_FRIEND:
15722 	  if (!at_class_scope_p ())
15723 	    {
15724 	      gcc_rich_location richloc (token->location);
15725 	      richloc.add_fixit_remove ();
15726 	      error_at (&richloc, "%<friend%> used outside of class");
15727 	      cp_lexer_purge_token (parser->lexer);
15728 	    }
15729 	  else
15730 	    {
15731 	      ds = ds_friend;
15732 	      /* Consume the token.  */
15733 	      cp_lexer_consume_token (parser->lexer);
15734 	    }
15735 	  break;
15736 
15737         case RID_CONSTEXPR:
15738 	  ds = ds_constexpr;
15739           cp_lexer_consume_token (parser->lexer);
15740           break;
15741 
15742 	case RID_CONSTINIT:
15743 	  ds = ds_constinit;
15744 	  cp_lexer_consume_token (parser->lexer);
15745 	  break;
15746 
15747 	case RID_CONSTEVAL:
15748 	  ds = ds_consteval;
15749 	  cp_lexer_consume_token (parser->lexer);
15750 	  break;
15751 
15752         case RID_CONCEPT:
15753           ds = ds_concept;
15754           cp_lexer_consume_token (parser->lexer);
15755 
15756 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15757 	    break;
15758 
15759           /* Warn for concept as a decl-specifier. We'll rewrite these as
15760              concept declarations later.  */
15761           if (!flag_concepts_ts)
15762             {
15763 	      cp_token *next = cp_lexer_peek_token (parser->lexer);
15764 	      if (next->keyword == RID_BOOL)
15765 		pedwarn (next->location, 0, "the %<bool%> keyword is not "
15766 			 "allowed in a C++20 concept definition");
15767 	      else
15768 		pedwarn (token->location, 0, "C++20 concept definition syntax "
15769 			 "is %<concept <name> = <expr>%>");
15770             }
15771 
15772 	  /* In C++20 a concept definition is just 'concept name = expr;'
15773 	     Support that syntax as a TS extension by pretending we've seen
15774 	     the 'bool' specifier.  */
15775 	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15776 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15777 	      && !decl_specs->any_type_specifiers_p)
15778 	    {
15779 	      cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15780 					    token, /*type_definition*/false);
15781 	      decl_specs->any_type_specifiers_p = true;
15782 	    }
15783           break;
15784 
15785 	  /* function-specifier:
15786 	       inline
15787 	       virtual
15788 	       explicit  */
15789 	case RID_INLINE:
15790 	case RID_VIRTUAL:
15791 	case RID_EXPLICIT:
15792 	  cp_parser_function_specifier_opt (parser, decl_specs);
15793 	  break;
15794 
15795 	  /* decl-specifier:
15796 	       typedef  */
15797 	case RID_TYPEDEF:
15798 	  ds = ds_typedef;
15799 	  /* Consume the token.  */
15800 	  cp_lexer_consume_token (parser->lexer);
15801 
15802 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15803 	    break;
15804 
15805 	  /* A constructor declarator cannot appear in a typedef.  */
15806 	  constructor_possible_p = false;
15807 	  /* The "typedef" keyword can only occur in a declaration; we
15808 	     may as well commit at this point.  */
15809 	  cp_parser_commit_to_tentative_parse (parser);
15810 
15811           if (decl_specs->storage_class != sc_none)
15812             decl_specs->conflicting_specifiers_p = true;
15813 	  break;
15814 
15815 	  /* storage-class-specifier:
15816 	       auto
15817 	       register
15818 	       static
15819 	       extern
15820 	       mutable
15821 
15822 	     GNU Extension:
15823 	       thread  */
15824 	case RID_AUTO:
15825           if (cxx_dialect == cxx98)
15826             {
15827 	      /* Consume the token.  */
15828 	      cp_lexer_consume_token (parser->lexer);
15829 
15830 	      /* Complain about `auto' as a storage specifier, if
15831 		 we're complaining about C++0x compatibility.  */
15832 	      gcc_rich_location richloc (token->location);
15833 	      richloc.add_fixit_remove ();
15834 	      warning_at (&richloc, OPT_Wc__11_compat,
15835 			  "%<auto%> changes meaning in C++11; "
15836 			  "please remove it");
15837 
15838               /* Set the storage class anyway.  */
15839               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
15840 					   token);
15841             }
15842           else
15843 	    /* C++0x auto type-specifier.  */
15844 	    found_decl_spec = false;
15845           break;
15846 
15847 	case RID_REGISTER:
15848 	case RID_STATIC:
15849 	case RID_EXTERN:
15850 	case RID_MUTABLE:
15851 	  /* Consume the token.  */
15852 	  cp_lexer_consume_token (parser->lexer);
15853           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
15854 				       token);
15855 	  break;
15856 	case RID_THREAD:
15857 	  /* Consume the token.  */
15858 	  ds = ds_thread;
15859 	  cp_lexer_consume_token (parser->lexer);
15860 	  break;
15861 
15862 	default:
15863 	  /* We did not yet find a decl-specifier yet.  */
15864 	  found_decl_spec = false;
15865 	  break;
15866 	}
15867 
15868       if (found_decl_spec
15869 	  && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
15870 	  && token->keyword != RID_CONSTEXPR)
15871 	error ("%qD invalid in condition", ridpointers[token->keyword]);
15872 
15873       if (found_decl_spec
15874 	  && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15875 	  && token->keyword != RID_MUTABLE
15876 	  && token->keyword != RID_CONSTEXPR
15877 	  && token->keyword != RID_CONSTEVAL)
15878 	error_at (token->location, "%qD invalid in lambda",
15879 		  ridpointers[token->keyword]);
15880 
15881       if (ds != ds_last)
15882 	set_and_check_decl_spec_loc (decl_specs, ds, token);
15883 
15884       /* Constructors are a special case.  The `S' in `S()' is not a
15885 	 decl-specifier; it is the beginning of the declarator.  */
15886       constructor_p
15887 	= (!found_decl_spec
15888 	   && constructor_possible_p
15889 	   && (cp_parser_constructor_declarator_p
15890 	       (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
15891 							 ds_friend))));
15892 
15893       /* If we don't have a DECL_SPEC yet, then we must be looking at
15894 	 a type-specifier.  */
15895       if (!found_decl_spec && !constructor_p)
15896 	{
15897 	  int decl_spec_declares_class_or_enum;
15898 	  bool is_cv_qualifier;
15899 	  tree type_spec;
15900 
15901 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15902 	    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15903 
15904 	  type_spec
15905 	    = cp_parser_type_specifier (parser, flags,
15906 					decl_specs,
15907 					/*is_declaration=*/true,
15908 					&decl_spec_declares_class_or_enum,
15909 					&is_cv_qualifier);
15910 	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
15911 
15912 	  /* If this type-specifier referenced a user-defined type
15913 	     (a typedef, class-name, etc.), then we can't allow any
15914 	     more such type-specifiers henceforth.
15915 
15916 	     [dcl.spec]
15917 
15918 	     The longest sequence of decl-specifiers that could
15919 	     possibly be a type name is taken as the
15920 	     decl-specifier-seq of a declaration.  The sequence shall
15921 	     be self-consistent as described below.
15922 
15923 	     [dcl.type]
15924 
15925 	     As a general rule, at most one type-specifier is allowed
15926 	     in the complete decl-specifier-seq of a declaration.  The
15927 	     only exceptions are the following:
15928 
15929 	     -- const or volatile can be combined with any other
15930 		type-specifier.
15931 
15932 	     -- signed or unsigned can be combined with char, long,
15933 		short, or int.
15934 
15935 	     -- ..
15936 
15937 	     Example:
15938 
15939 	       typedef char* Pc;
15940 	       void g (const int Pc);
15941 
15942 	     Here, Pc is *not* part of the decl-specifier seq; it's
15943 	     the declarator.  Therefore, once we see a type-specifier
15944 	     (other than a cv-qualifier), we forbid any additional
15945 	     user-defined types.  We *do* still allow things like `int
15946 	     int' to be considered a decl-specifier-seq, and issue the
15947 	     error message later.  */
15948 	  if (type_spec && !is_cv_qualifier)
15949 	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15950 	  /* A constructor declarator cannot follow a type-specifier.  */
15951 	  if (type_spec)
15952 	    {
15953 	      constructor_possible_p = false;
15954 	      found_decl_spec = true;
15955 	      if (!is_cv_qualifier)
15956 		decl_specs->any_type_specifiers_p = true;
15957 
15958 	      if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
15959 		error_at (token->location, "type-specifier invalid in lambda");
15960 	    }
15961 	}
15962 
15963       /* If we still do not have a DECL_SPEC, then there are no more
15964 	 decl-specifiers.  */
15965       if (!found_decl_spec)
15966 	break;
15967 
15968       if (decl_specs->std_attributes)
15969 	{
15970 	  error_at (decl_specs->locations[ds_std_attribute],
15971 		    "standard attributes in middle of decl-specifiers");
15972 	  inform (decl_specs->locations[ds_std_attribute],
15973 		  "standard attributes must precede the decl-specifiers to "
15974 		  "apply to the declaration, or follow them to apply to "
15975 		  "the type");
15976 	}
15977 
15978       decl_specs->any_specifiers_p = true;
15979       /* After we see one decl-specifier, further decl-specifiers are
15980 	 always optional.  */
15981       flags |= CP_PARSER_FLAGS_OPTIONAL;
15982     }
15983 
15984   /* Don't allow a friend specifier with a class definition.  */
15985   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
15986       && (*declares_class_or_enum & 2))
15987     error_at (decl_specs->locations[ds_friend],
15988 	      "class definition may not be declared a friend");
15989 }
15990 
15991 /* Parse an (optional) storage-class-specifier.
15992 
15993    storage-class-specifier:
15994      auto
15995      register
15996      static
15997      extern
15998      mutable
15999 
16000    GNU Extension:
16001 
16002    storage-class-specifier:
16003      thread
16004 
16005    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
16006 
16007 static tree
cp_parser_storage_class_specifier_opt(cp_parser * parser)16008 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16009 {
16010   switch (cp_lexer_peek_token (parser->lexer)->keyword)
16011     {
16012     case RID_AUTO:
16013       if (cxx_dialect != cxx98)
16014         return NULL_TREE;
16015       /* Fall through for C++98.  */
16016       gcc_fallthrough ();
16017 
16018     case RID_REGISTER:
16019     case RID_STATIC:
16020     case RID_EXTERN:
16021     case RID_MUTABLE:
16022     case RID_THREAD:
16023       /* Consume the token.  */
16024       return cp_lexer_consume_token (parser->lexer)->u.value;
16025 
16026     default:
16027       return NULL_TREE;
16028     }
16029 }
16030 
16031 /* Parse an (optional) function-specifier.
16032 
16033    function-specifier:
16034      inline
16035      virtual
16036      explicit
16037 
16038    C++20 Extension:
16039      explicit(constant-expression)
16040 
16041    Returns an IDENTIFIER_NODE corresponding to the keyword used.
16042    Updates DECL_SPECS, if it is non-NULL.  */
16043 
16044 static tree
cp_parser_function_specifier_opt(cp_parser * parser,cp_decl_specifier_seq * decl_specs)16045 cp_parser_function_specifier_opt (cp_parser* parser,
16046 				  cp_decl_specifier_seq *decl_specs)
16047 {
16048   cp_token *token = cp_lexer_peek_token (parser->lexer);
16049   switch (token->keyword)
16050     {
16051     case RID_INLINE:
16052       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16053       break;
16054 
16055     case RID_VIRTUAL:
16056       /* 14.5.2.3 [temp.mem]
16057 
16058 	 A member function template shall not be virtual.  */
16059       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16060 	  && current_class_type)
16061 	error_at (token->location, "templates may not be %<virtual%>");
16062       else
16063 	set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16064       break;
16065 
16066     case RID_EXPLICIT:
16067       {
16068 	tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16069 	/* If we see '(', it's C++20 explicit(bool).  */
16070 	tree expr;
16071 	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16072 	  {
16073 	    matching_parens parens;
16074 	    parens.consume_open (parser);
16075 
16076 	    /* New types are not allowed in an explicit-specifier.  */
16077 	    const char *saved_message
16078 	      = parser->type_definition_forbidden_message;
16079 	    parser->type_definition_forbidden_message
16080 	      = G_("types may not be defined in explicit-specifier");
16081 
16082 	    if (cxx_dialect < cxx20)
16083 	      pedwarn (token->location, OPT_Wc__20_extensions,
16084 		       "%<explicit(bool)%> only available with %<-std=c++20%> "
16085 		       "or %<-std=gnu++20%>");
16086 
16087 	    /* Parse the constant-expression.  */
16088 	    expr = cp_parser_constant_expression (parser);
16089 
16090 	    /* Restore the saved message.  */
16091 	    parser->type_definition_forbidden_message = saved_message;
16092 	    parens.require_close (parser);
16093 	  }
16094 	else
16095 	  /* The explicit-specifier explicit without a constant-expression is
16096 	     equivalent to the explicit-specifier explicit(true).  */
16097 	  expr = boolean_true_node;
16098 
16099 	/* [dcl.fct.spec]
16100 	   "the constant-expression, if supplied, shall be a contextually
16101 	   converted constant expression of type bool."  */
16102 	expr = build_explicit_specifier (expr, tf_warning_or_error);
16103 	/* We could evaluate it -- mark the decl as appropriate.  */
16104 	if (expr == boolean_true_node)
16105 	  set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16106 	else if (expr == boolean_false_node)
16107 	  /* Don't mark the decl as explicit.  */;
16108 	else if (decl_specs)
16109 	  /* The expression was value-dependent.  Remember it so that we can
16110 	     substitute it later.  */
16111 	  decl_specs->explicit_specifier = expr;
16112 	return id;
16113       }
16114 
16115     default:
16116       return NULL_TREE;
16117     }
16118 
16119   /* Consume the token.  */
16120   return cp_lexer_consume_token (parser->lexer)->u.value;
16121 }
16122 
16123 /* Parse a linkage-specification.
16124 
16125    linkage-specification:
16126      extern string-literal { declaration-seq [opt] }
16127      extern string-literal declaration  */
16128 
16129 static void
cp_parser_linkage_specification(cp_parser * parser,tree prefix_attr)16130 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16131 {
16132   tree linkage;
16133 
16134   /* Look for the `extern' keyword.  */
16135   cp_token *extern_token
16136     = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16137 
16138   /* Look for the string-literal.  */
16139   cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16140   linkage = cp_parser_string_literal (parser, false, false);
16141 
16142   /* Transform the literal into an identifier.  If the literal is a
16143      wide-character string, or contains embedded NULs, then we can't
16144      handle it as the user wants.  */
16145   if (linkage == error_mark_node
16146       || strlen (TREE_STRING_POINTER (linkage))
16147 	 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16148     {
16149       cp_parser_error (parser, "invalid linkage-specification");
16150       /* Assume C++ linkage.  */
16151       linkage = lang_name_cplusplus;
16152     }
16153   else
16154     linkage = get_identifier (TREE_STRING_POINTER (linkage));
16155 
16156   /* We're now using the new linkage.  */
16157   push_lang_context (linkage);
16158 
16159   /* Preserve the location of the innermost linkage specification,
16160      tracking the locations of nested specifications via a local.  */
16161   location_t saved_location
16162     = parser->innermost_linkage_specification_location;
16163   /* Construct a location ranging from the start of the "extern" to
16164      the end of the string-literal, with the caret at the start, e.g.:
16165        extern "C" {
16166        ^~~~~~~~~~
16167   */
16168   parser->innermost_linkage_specification_location
16169     = make_location (extern_token->location,
16170 		     extern_token->location,
16171 		     get_finish (string_token->location));
16172 
16173   /* If the next token is a `{', then we're using the first
16174      production.  */
16175   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16176     {
16177       cp_ensure_no_omp_declare_simd (parser);
16178       cp_ensure_no_oacc_routine (parser);
16179 
16180       /* Consume the `{' token.  */
16181       matching_braces braces;
16182       braces.consume_open (parser);
16183       /* Parse the declarations.  */
16184       cp_parser_declaration_seq_opt (parser);
16185       /* Look for the closing `}'.  */
16186       braces.require_close (parser);
16187     }
16188   /* Otherwise, there's just one declaration.  */
16189   else
16190     {
16191       bool saved_in_unbraced_linkage_specification_p;
16192 
16193       saved_in_unbraced_linkage_specification_p
16194 	= parser->in_unbraced_linkage_specification_p;
16195       parser->in_unbraced_linkage_specification_p = true;
16196       cp_parser_declaration (parser, prefix_attr);
16197       parser->in_unbraced_linkage_specification_p
16198 	= saved_in_unbraced_linkage_specification_p;
16199     }
16200 
16201   /* We're done with the linkage-specification.  */
16202   pop_lang_context ();
16203 
16204   /* Restore location of parent linkage specification, if any.  */
16205   parser->innermost_linkage_specification_location = saved_location;
16206 }
16207 
16208 /* Parse a static_assert-declaration.
16209 
16210    static_assert-declaration:
16211      static_assert ( constant-expression , string-literal ) ;
16212      static_assert ( constant-expression ) ; (C++17)
16213 
16214    If MEMBER_P, this static_assert is a class member.  */
16215 
16216 static void
cp_parser_static_assert(cp_parser * parser,bool member_p)16217 cp_parser_static_assert(cp_parser *parser, bool member_p)
16218 {
16219   cp_expr condition;
16220   location_t token_loc;
16221   tree message;
16222   bool dummy;
16223 
16224   /* Peek at the `static_assert' token so we can keep track of exactly
16225      where the static assertion started.  */
16226   token_loc = cp_lexer_peek_token (parser->lexer)->location;
16227 
16228   /* Look for the `static_assert' keyword.  */
16229   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16230                                   RT_STATIC_ASSERT))
16231     return;
16232 
16233   /*  We know we are in a static assertion; commit to any tentative
16234       parse.  */
16235   if (cp_parser_parsing_tentatively (parser))
16236     cp_parser_commit_to_tentative_parse (parser);
16237 
16238   /* Parse the `(' starting the static assertion condition.  */
16239   matching_parens parens;
16240   parens.require_open (parser);
16241 
16242   /* Parse the constant-expression.  Allow a non-constant expression
16243      here in order to give better diagnostics in finish_static_assert.  */
16244   condition =
16245     cp_parser_constant_expression (parser,
16246                                    /*allow_non_constant_p=*/true,
16247                                    /*non_constant_p=*/&dummy);
16248 
16249   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16250     {
16251       if (pedantic && cxx_dialect < cxx17)
16252 	pedwarn (input_location, OPT_Wc__17_extensions,
16253 		 "%<static_assert%> without a message "
16254 		 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16255       /* Eat the ')'  */
16256       cp_lexer_consume_token (parser->lexer);
16257       message = build_string (1, "");
16258       TREE_TYPE (message) = char_array_type_node;
16259       fix_string_type (message);
16260     }
16261   else
16262     {
16263       /* Parse the separating `,'.  */
16264       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16265 
16266       /* Parse the string-literal message.  */
16267       message = cp_parser_string_literal (parser,
16268                                 	  /*translate=*/false,
16269                                 	  /*wide_ok=*/true);
16270 
16271       /* A `)' completes the static assertion.  */
16272       if (!parens.require_close (parser))
16273 	cp_parser_skip_to_closing_parenthesis (parser,
16274                                                /*recovering=*/true,
16275                                                /*or_comma=*/false,
16276 					       /*consume_paren=*/true);
16277     }
16278 
16279   /* A semicolon terminates the declaration.  */
16280   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16281 
16282   /* Get the location for the static assertion.  Use that of the
16283      condition if available, otherwise, use that of the "static_assert"
16284      token.  */
16285   location_t assert_loc = condition.get_location ();
16286   if (assert_loc == UNKNOWN_LOCATION)
16287     assert_loc = token_loc;
16288 
16289   /* Complete the static assertion, which may mean either processing
16290      the static assert now or saving it for template instantiation.  */
16291   finish_static_assert (condition, message, assert_loc, member_p,
16292 			/*show_expr_p=*/false);
16293 }
16294 
16295 /* Parse the expression in decltype ( expression ).  */
16296 
16297 static tree
cp_parser_decltype_expr(cp_parser * parser,bool & id_expression_or_member_access_p)16298 cp_parser_decltype_expr (cp_parser *parser,
16299 			 bool &id_expression_or_member_access_p)
16300 {
16301   cp_token *id_expr_start_token;
16302   tree expr;
16303 
16304   /* First, try parsing an id-expression.  */
16305   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16306   cp_parser_parse_tentatively (parser);
16307   expr = cp_parser_id_expression (parser,
16308                                   /*template_keyword_p=*/false,
16309                                   /*check_dependency_p=*/true,
16310                                   /*template_p=*/NULL,
16311                                   /*declarator_p=*/false,
16312                                   /*optional_p=*/false);
16313 
16314   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16315     {
16316       bool non_integral_constant_expression_p = false;
16317       tree id_expression = expr;
16318       cp_id_kind idk;
16319       const char *error_msg;
16320 
16321       if (identifier_p (expr))
16322 	/* Lookup the name we got back from the id-expression.  */
16323 	expr = cp_parser_lookup_name_simple (parser, expr,
16324 					     id_expr_start_token->location);
16325 
16326       if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16327 	/* A template without args is not a complete id-expression.  */
16328 	expr = error_mark_node;
16329 
16330       if (expr
16331           && expr != error_mark_node
16332           && TREE_CODE (expr) != TYPE_DECL
16333 	  && (TREE_CODE (expr) != BIT_NOT_EXPR
16334 	      || !TYPE_P (TREE_OPERAND (expr, 0)))
16335           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16336         {
16337           /* Complete lookup of the id-expression.  */
16338           expr = (finish_id_expression
16339                   (id_expression, expr, parser->scope, &idk,
16340                    /*integral_constant_expression_p=*/false,
16341                    /*allow_non_integral_constant_expression_p=*/true,
16342                    &non_integral_constant_expression_p,
16343                    /*template_p=*/false,
16344                    /*done=*/true,
16345                    /*address_p=*/false,
16346                    /*template_arg_p=*/false,
16347                    &error_msg,
16348 		   id_expr_start_token->location));
16349 
16350           if (expr == error_mark_node)
16351             /* We found an id-expression, but it was something that we
16352                should not have found. This is an error, not something
16353                we can recover from, so note that we found an
16354                id-expression and we'll recover as gracefully as
16355                possible.  */
16356             id_expression_or_member_access_p = true;
16357         }
16358 
16359       if (expr
16360           && expr != error_mark_node
16361           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16362         /* We have an id-expression.  */
16363         id_expression_or_member_access_p = true;
16364     }
16365 
16366   if (!id_expression_or_member_access_p)
16367     {
16368       /* Abort the id-expression parse.  */
16369       cp_parser_abort_tentative_parse (parser);
16370 
16371       /* Parsing tentatively, again.  */
16372       cp_parser_parse_tentatively (parser);
16373 
16374       /* Parse a class member access.  */
16375       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16376                                            /*cast_p=*/false, /*decltype*/true,
16377                                            /*member_access_only_p=*/true, NULL);
16378 
16379       if (expr
16380           && expr != error_mark_node
16381           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16382         /* We have an id-expression.  */
16383         id_expression_or_member_access_p = true;
16384     }
16385 
16386   if (id_expression_or_member_access_p)
16387     /* We have parsed the complete id-expression or member access.  */
16388     cp_parser_parse_definitely (parser);
16389   else
16390     {
16391       /* Abort our attempt to parse an id-expression or member access
16392          expression.  */
16393       cp_parser_abort_tentative_parse (parser);
16394 
16395       /* Parse a full expression.  */
16396       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16397 				   /*decltype_p=*/true);
16398     }
16399 
16400   return expr;
16401 }
16402 
16403 /* Parse a `decltype' type.  Returns the type.
16404 
16405    decltype-specifier:
16406      decltype ( expression )
16407    C++14:
16408      decltype ( auto )  */
16409 
16410 static tree
cp_parser_decltype(cp_parser * parser)16411 cp_parser_decltype (cp_parser *parser)
16412 {
16413   bool id_expression_or_member_access_p = false;
16414   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16415 
16416   if (start_token->type == CPP_DECLTYPE)
16417     {
16418       /* Already parsed.  */
16419       cp_lexer_consume_token (parser->lexer);
16420       return saved_checks_value (start_token->u.tree_check_value);
16421     }
16422 
16423   /* Look for the `decltype' token.  */
16424   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16425     return error_mark_node;
16426 
16427   /* Parse the opening `('.  */
16428   matching_parens parens;
16429   if (!parens.require_open (parser))
16430     return error_mark_node;
16431 
16432   /* Since we're going to preserve any side-effects from this parse, set up a
16433      firewall to protect our callers from cp_parser_commit_to_tentative_parse
16434      in the expression.  */
16435   tentative_firewall firewall (parser);
16436 
16437   /* If in_declarator_p, a reparse as an expression might succeed (60361).
16438      Otherwise, commit now for better diagnostics.  */
16439   if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16440       && !parser->in_declarator_p)
16441     cp_parser_commit_to_topmost_tentative_parse (parser);
16442 
16443   push_deferring_access_checks (dk_deferred);
16444 
16445   tree expr = NULL_TREE;
16446 
16447   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16448       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16449     {
16450       /* decltype (auto) */
16451       cp_lexer_consume_token (parser->lexer);
16452       if (cxx_dialect < cxx14)
16453 	{
16454 	  error_at (start_token->location,
16455 		    "%<decltype(auto)%> type specifier only available with "
16456 		    "%<-std=c++14%> or %<-std=gnu++14%>");
16457 	  expr = error_mark_node;
16458 	}
16459     }
16460   else
16461     {
16462       /* decltype (expression)  */
16463 
16464       /* Types cannot be defined in a `decltype' expression.  Save away the
16465 	 old message and set the new one.  */
16466       const char *saved_message = parser->type_definition_forbidden_message;
16467       parser->type_definition_forbidden_message
16468 	= G_("types may not be defined in %<decltype%> expressions");
16469 
16470       /* The restrictions on constant-expressions do not apply inside
16471 	 decltype expressions.  */
16472       bool saved_integral_constant_expression_p
16473 	= parser->integral_constant_expression_p;
16474       bool saved_non_integral_constant_expression_p
16475 	= parser->non_integral_constant_expression_p;
16476       parser->integral_constant_expression_p = false;
16477 
16478       /* Within a parenthesized expression, a `>' token is always
16479 	 the greater-than operator.  */
16480       bool saved_greater_than_is_operator_p
16481 	= parser->greater_than_is_operator_p;
16482       parser->greater_than_is_operator_p = true;
16483 
16484       /* Don't synthesize an implicit template type parameter here.  This
16485 	 could happen with C++23 code like
16486 
16487 	   void f(decltype(new auto{0}));
16488 
16489 	 where we want to deduce the auto right away so that the parameter
16490 	 is of type 'int *'.  */
16491       auto cleanup = make_temp_override
16492 	(parser->auto_is_implicit_function_template_parm_p, false);
16493 
16494       /* Do not actually evaluate the expression.  */
16495       ++cp_unevaluated_operand;
16496 
16497       /* Do not warn about problems with the expression.  */
16498       ++c_inhibit_evaluation_warnings;
16499 
16500       expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16501       STRIP_ANY_LOCATION_WRAPPER (expr);
16502 
16503       /* Go back to evaluating expressions.  */
16504       --cp_unevaluated_operand;
16505       --c_inhibit_evaluation_warnings;
16506 
16507       /* The `>' token might be the end of a template-id or
16508 	 template-parameter-list now.  */
16509       parser->greater_than_is_operator_p
16510 	= saved_greater_than_is_operator_p;
16511 
16512       /* Restore the old message and the integral constant expression
16513 	 flags.  */
16514       parser->type_definition_forbidden_message = saved_message;
16515       parser->integral_constant_expression_p
16516 	= saved_integral_constant_expression_p;
16517       parser->non_integral_constant_expression_p
16518 	= saved_non_integral_constant_expression_p;
16519     }
16520 
16521   /* Parse to the closing `)'.  */
16522   if (expr == error_mark_node || !parens.require_close (parser))
16523     {
16524       cp_parser_skip_to_closing_parenthesis (parser, true, false,
16525 					     /*consume_paren=*/true);
16526       expr = error_mark_node;
16527     }
16528 
16529   /* If we got a parse error while tentative, bail out now.  */
16530   if (cp_parser_error_occurred (parser))
16531     {
16532       pop_deferring_access_checks ();
16533       return error_mark_node;
16534     }
16535 
16536   if (!expr)
16537     /* Build auto.  */
16538     expr = make_decltype_auto ();
16539   else
16540     expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16541 				 tf_warning_or_error);
16542 
16543   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16544      it again.  */
16545   start_token->type = CPP_DECLTYPE;
16546   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16547   start_token->tree_check_p = true;
16548   start_token->u.tree_check_value->value = expr;
16549   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16550   start_token->keyword = RID_MAX;
16551 
16552   location_t loc = start_token->location;
16553   loc = make_location (loc, loc, parser->lexer);
16554   start_token->location = loc;
16555 
16556   cp_lexer_purge_tokens_after (parser->lexer, start_token);
16557 
16558   pop_to_parent_deferring_access_checks ();
16559 
16560   return expr;
16561 }
16562 
16563 /* Special member functions [gram.special] */
16564 
16565 /* Parse a conversion-function-id.
16566 
16567    conversion-function-id:
16568      operator conversion-type-id
16569 
16570    Returns an IDENTIFIER_NODE representing the operator.  */
16571 
16572 static tree
cp_parser_conversion_function_id(cp_parser * parser)16573 cp_parser_conversion_function_id (cp_parser* parser)
16574 {
16575   tree type;
16576   tree saved_scope;
16577   tree saved_qualifying_scope;
16578   tree saved_object_scope;
16579   tree pushed_scope = NULL_TREE;
16580 
16581   /* Look for the `operator' token.  */
16582   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16583     return error_mark_node;
16584   /* When we parse the conversion-type-id, the current scope will be
16585      reset.  However, we need that information in able to look up the
16586      conversion function later, so we save it here.  */
16587   saved_scope = parser->scope;
16588   saved_qualifying_scope = parser->qualifying_scope;
16589   saved_object_scope = parser->object_scope;
16590   /* We must enter the scope of the class so that the names of
16591      entities declared within the class are available in the
16592      conversion-type-id.  For example, consider:
16593 
16594        struct S {
16595 	 typedef int I;
16596 	 operator I();
16597        };
16598 
16599        S::operator I() { ... }
16600 
16601      In order to see that `I' is a type-name in the definition, we
16602      must be in the scope of `S'.  */
16603   if (saved_scope)
16604     pushed_scope = push_scope (saved_scope);
16605   /* Parse the conversion-type-id.  */
16606   type = cp_parser_conversion_type_id (parser);
16607   /* Leave the scope of the class, if any.  */
16608   if (pushed_scope)
16609     pop_scope (pushed_scope);
16610   /* Restore the saved scope.  */
16611   parser->scope = saved_scope;
16612   parser->qualifying_scope = saved_qualifying_scope;
16613   parser->object_scope = saved_object_scope;
16614   /* If the TYPE is invalid, indicate failure.  */
16615   if (type == error_mark_node)
16616     return error_mark_node;
16617   return make_conv_op_name (type);
16618 }
16619 
16620 /* Parse a conversion-type-id:
16621 
16622    conversion-type-id:
16623      type-specifier-seq conversion-declarator [opt]
16624 
16625    Returns the TYPE specified.  */
16626 
16627 static tree
cp_parser_conversion_type_id(cp_parser * parser)16628 cp_parser_conversion_type_id (cp_parser* parser)
16629 {
16630   tree attributes;
16631   cp_decl_specifier_seq type_specifiers;
16632   cp_declarator *declarator;
16633   tree type_specified;
16634   const char *saved_message;
16635 
16636   /* Parse the attributes.  */
16637   attributes = cp_parser_attributes_opt (parser);
16638 
16639   saved_message = parser->type_definition_forbidden_message;
16640   parser->type_definition_forbidden_message
16641     = G_("types may not be defined in a conversion-type-id");
16642 
16643   /* Parse the type-specifiers.  DR 2413 clarifies that `typename' is
16644      optional in conversion-type-id.  */
16645   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16646 				/*is_declaration=*/false,
16647 				/*is_trailing_return=*/false,
16648 				&type_specifiers);
16649 
16650   parser->type_definition_forbidden_message = saved_message;
16651 
16652   /* If that didn't work, stop.  */
16653   if (type_specifiers.type == error_mark_node)
16654     return error_mark_node;
16655   /* Parse the conversion-declarator.  */
16656   declarator = cp_parser_conversion_declarator_opt (parser);
16657 
16658   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
16659 				    /*initialized=*/0, &attributes);
16660   if (attributes)
16661     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16662 
16663   /* Don't give this error when parsing tentatively.  This happens to
16664      work because we always parse this definitively once.  */
16665   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16666       && type_uses_auto (type_specified))
16667     {
16668       if (cxx_dialect < cxx14)
16669 	{
16670 	  error ("invalid use of %<auto%> in conversion operator");
16671 	  return error_mark_node;
16672 	}
16673       else if (template_parm_scope_p ())
16674 	warning (0, "use of %<auto%> in member template "
16675 		 "conversion operator can never be deduced");
16676     }
16677 
16678   return type_specified;
16679 }
16680 
16681 /* Parse an (optional) conversion-declarator.
16682 
16683    conversion-declarator:
16684      ptr-operator conversion-declarator [opt]
16685 
16686    */
16687 
16688 static cp_declarator *
cp_parser_conversion_declarator_opt(cp_parser * parser)16689 cp_parser_conversion_declarator_opt (cp_parser* parser)
16690 {
16691   enum tree_code code;
16692   tree class_type, std_attributes = NULL_TREE;
16693   cp_cv_quals cv_quals;
16694 
16695   /* We don't know if there's a ptr-operator next, or not.  */
16696   cp_parser_parse_tentatively (parser);
16697   /* Try the ptr-operator.  */
16698   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16699 				 &std_attributes);
16700   /* If it worked, look for more conversion-declarators.  */
16701   if (cp_parser_parse_definitely (parser))
16702     {
16703       cp_declarator *declarator;
16704 
16705       /* Parse another optional declarator.  */
16706       declarator = cp_parser_conversion_declarator_opt (parser);
16707 
16708       declarator = cp_parser_make_indirect_declarator
16709 	(code, class_type, cv_quals, declarator, std_attributes);
16710 
16711       return declarator;
16712    }
16713 
16714   return NULL;
16715 }
16716 
16717 /* Parse an (optional) ctor-initializer.
16718 
16719    ctor-initializer:
16720      : mem-initializer-list  */
16721 
16722 static void
cp_parser_ctor_initializer_opt(cp_parser * parser)16723 cp_parser_ctor_initializer_opt (cp_parser* parser)
16724 {
16725   /* If the next token is not a `:', then there is no
16726      ctor-initializer.  */
16727   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16728     {
16729       /* Do default initialization of any bases and members.  */
16730       if (DECL_CONSTRUCTOR_P (current_function_decl))
16731 	finish_mem_initializers (NULL_TREE);
16732       return;
16733     }
16734 
16735   /* Consume the `:' token.  */
16736   cp_lexer_consume_token (parser->lexer);
16737   /* And the mem-initializer-list.  */
16738   cp_parser_mem_initializer_list (parser);
16739 }
16740 
16741 /* Parse a mem-initializer-list.
16742 
16743    mem-initializer-list:
16744      mem-initializer ... [opt]
16745      mem-initializer ... [opt] , mem-initializer-list  */
16746 
16747 static void
cp_parser_mem_initializer_list(cp_parser * parser)16748 cp_parser_mem_initializer_list (cp_parser* parser)
16749 {
16750   tree mem_initializer_list = NULL_TREE;
16751   tree target_ctor = error_mark_node;
16752   cp_token *token = cp_lexer_peek_token (parser->lexer);
16753 
16754   /* Let the semantic analysis code know that we are starting the
16755      mem-initializer-list.  */
16756   if (!DECL_CONSTRUCTOR_P (current_function_decl))
16757     error_at (token->location,
16758 	      "only constructors take member initializers");
16759 
16760   /* Loop through the list.  */
16761   while (true)
16762     {
16763       tree mem_initializer;
16764 
16765       token = cp_lexer_peek_token (parser->lexer);
16766       /* Parse the mem-initializer.  */
16767       mem_initializer = cp_parser_mem_initializer (parser);
16768       /* If the next token is a `...', we're expanding member initializers. */
16769       bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16770       if (ellipsis
16771 	  || (mem_initializer != error_mark_node
16772 	      && check_for_bare_parameter_packs (TREE_PURPOSE
16773 						 (mem_initializer))))
16774         {
16775           /* Consume the `...'. */
16776 	  if (ellipsis)
16777 	    cp_lexer_consume_token (parser->lexer);
16778 
16779           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16780              can be expanded but members cannot. */
16781           if (mem_initializer != error_mark_node
16782               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16783             {
16784               error_at (token->location,
16785 			"cannot expand initializer for member %qD",
16786 			TREE_PURPOSE (mem_initializer));
16787               mem_initializer = error_mark_node;
16788             }
16789 
16790           /* Construct the pack expansion type. */
16791           if (mem_initializer != error_mark_node)
16792             mem_initializer = make_pack_expansion (mem_initializer);
16793         }
16794       if (target_ctor != error_mark_node
16795 	  && mem_initializer != error_mark_node)
16796 	{
16797 	  error ("mem-initializer for %qD follows constructor delegation",
16798 		 TREE_PURPOSE (mem_initializer));
16799 	  mem_initializer = error_mark_node;
16800 	}
16801       /* Look for a target constructor. */
16802       if (mem_initializer != error_mark_node
16803 	  && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16804 	  && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16805 	{
16806 	  maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16807 	  if (mem_initializer_list)
16808 	    {
16809 	      error ("constructor delegation follows mem-initializer for %qD",
16810 		     TREE_PURPOSE (mem_initializer_list));
16811 	      mem_initializer = error_mark_node;
16812 	    }
16813 	  target_ctor = mem_initializer;
16814 	}
16815       /* Add it to the list, unless it was erroneous.  */
16816       if (mem_initializer != error_mark_node)
16817 	{
16818 	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
16819 	  mem_initializer_list = mem_initializer;
16820 	}
16821       /* If the next token is not a `,', we're done.  */
16822       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16823 	break;
16824       /* Consume the `,' token.  */
16825       cp_lexer_consume_token (parser->lexer);
16826     }
16827 
16828   /* Perform semantic analysis.  */
16829   if (DECL_CONSTRUCTOR_P (current_function_decl))
16830     finish_mem_initializers (mem_initializer_list);
16831 }
16832 
16833 /* Parse a mem-initializer.
16834 
16835    mem-initializer:
16836      mem-initializer-id ( expression-list [opt] )
16837      mem-initializer-id braced-init-list
16838 
16839    GNU extension:
16840 
16841    mem-initializer:
16842      ( expression-list [opt] )
16843 
16844    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
16845    class) or FIELD_DECL (for a non-static data member) to initialize;
16846    the TREE_VALUE is the expression-list.  An empty initialization
16847    list is represented by void_list_node.  */
16848 
16849 static tree
cp_parser_mem_initializer(cp_parser * parser)16850 cp_parser_mem_initializer (cp_parser* parser)
16851 {
16852   tree mem_initializer_id;
16853   tree expression_list;
16854   tree member;
16855   cp_token *token = cp_lexer_peek_token (parser->lexer);
16856 
16857   /* Find out what is being initialized.  */
16858   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16859     {
16860       permerror (token->location,
16861 		 "anachronistic old-style base class initializer");
16862       mem_initializer_id = NULL_TREE;
16863     }
16864   else
16865     {
16866       mem_initializer_id = cp_parser_mem_initializer_id (parser);
16867       if (mem_initializer_id == error_mark_node)
16868 	return mem_initializer_id;
16869     }
16870   member = expand_member_init (mem_initializer_id);
16871   if (member && !DECL_P (member))
16872     in_base_initializer = 1;
16873 
16874   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16875     {
16876       bool expr_non_constant_p;
16877       cp_lexer_set_source_position (parser->lexer);
16878       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16879       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
16880       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
16881       expression_list = build_tree_list (NULL_TREE, expression_list);
16882     }
16883   else
16884     {
16885       vec<tree, va_gc> *vec;
16886       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16887 						     /*cast_p=*/false,
16888 						     /*allow_expansion_p=*/true,
16889 						     /*non_constant_p=*/NULL,
16890 						     /*close_paren_loc=*/NULL,
16891 						     /*wrap_locations_p=*/true);
16892       if (vec == NULL)
16893 	return error_mark_node;
16894       expression_list = build_tree_list_vec (vec);
16895       release_tree_vector (vec);
16896     }
16897 
16898   if (expression_list == error_mark_node)
16899     return error_mark_node;
16900   if (!expression_list)
16901     expression_list = void_type_node;
16902 
16903   in_base_initializer = 0;
16904 
16905   if (!member)
16906     return error_mark_node;
16907   tree node = build_tree_list (member, expression_list);
16908 
16909   /* We can't attach the source location of this initializer directly to
16910      the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
16911      within the TREE_TYPE of the list node.  */
16912   location_t loc
16913     = make_location (token->location, token->location, parser->lexer);
16914   tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
16915   SET_EXPR_LOCATION (dummy, loc);
16916   TREE_TYPE (node) = dummy;
16917 
16918   return node;
16919 }
16920 
16921 /* Parse a mem-initializer-id.
16922 
16923    mem-initializer-id:
16924      :: [opt] nested-name-specifier [opt] class-name
16925      decltype-specifier (C++11)
16926      identifier
16927 
16928    Returns a TYPE indicating the class to be initialized for the first
16929    production (and the second in C++11).  Returns an IDENTIFIER_NODE
16930    indicating the data member to be initialized for the last production.  */
16931 
16932 static tree
cp_parser_mem_initializer_id(cp_parser * parser)16933 cp_parser_mem_initializer_id (cp_parser* parser)
16934 {
16935   bool global_scope_p;
16936   bool nested_name_specifier_p;
16937   bool template_p = false;
16938   tree id;
16939 
16940   cp_token *token = cp_lexer_peek_token (parser->lexer);
16941 
16942   /* `typename' is not allowed in this context ([temp.res]).  */
16943   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16944     {
16945       error_at (token->location,
16946 		"keyword %<typename%> not allowed in this context (a qualified "
16947 		"member initializer is implicitly a type)");
16948       cp_lexer_consume_token (parser->lexer);
16949     }
16950   /* Look for the optional `::' operator.  */
16951   global_scope_p
16952     = (cp_parser_global_scope_opt (parser,
16953 				   /*current_scope_valid_p=*/false)
16954        != NULL_TREE);
16955   /* Look for the optional nested-name-specifier.  The simplest way to
16956      implement:
16957 
16958        [temp.res]
16959 
16960        The keyword `typename' is not permitted in a base-specifier or
16961        mem-initializer; in these contexts a qualified name that
16962        depends on a template-parameter is implicitly assumed to be a
16963        type name.
16964 
16965      is to assume that we have seen the `typename' keyword at this
16966      point.  */
16967   nested_name_specifier_p
16968     = (cp_parser_nested_name_specifier_opt (parser,
16969 					    /*typename_keyword_p=*/true,
16970 					    /*check_dependency_p=*/true,
16971 					    /*type_p=*/true,
16972 					    /*is_declaration=*/true)
16973        != NULL_TREE);
16974   if (nested_name_specifier_p)
16975     template_p = cp_parser_optional_template_keyword (parser);
16976   /* If there is a `::' operator or a nested-name-specifier, then we
16977      are definitely looking for a class-name.  */
16978   if (global_scope_p || nested_name_specifier_p)
16979     return cp_parser_class_name (parser,
16980 				 /*typename_keyword_p=*/true,
16981 				 /*template_keyword_p=*/template_p,
16982 				 typename_type,
16983 				 /*check_dependency_p=*/true,
16984 				 /*class_head_p=*/false,
16985 				 /*is_declaration=*/true);
16986   /* Otherwise, we could also be looking for an ordinary identifier.  */
16987   cp_parser_parse_tentatively (parser);
16988   if (cp_lexer_next_token_is_decltype (parser->lexer))
16989     /* Try a decltype-specifier.  */
16990     id = cp_parser_decltype (parser);
16991   else
16992     /* Otherwise, try a class-name.  */
16993     id = cp_parser_class_name (parser,
16994 			       /*typename_keyword_p=*/true,
16995 			       /*template_keyword_p=*/false,
16996 			       none_type,
16997 			       /*check_dependency_p=*/true,
16998 			       /*class_head_p=*/false,
16999 			       /*is_declaration=*/true);
17000   /* If we found one, we're done.  */
17001   if (cp_parser_parse_definitely (parser))
17002     return id;
17003   /* Otherwise, look for an ordinary identifier.  */
17004   return cp_parser_identifier (parser);
17005 }
17006 
17007 /* Overloading [gram.over] */
17008 
17009 /* Parse an operator-function-id.
17010 
17011    operator-function-id:
17012      operator operator
17013 
17014    Returns an IDENTIFIER_NODE for the operator which is a
17015    human-readable spelling of the identifier, e.g., `operator +'.  */
17016 
17017 static cp_expr
cp_parser_operator_function_id(cp_parser * parser)17018 cp_parser_operator_function_id (cp_parser* parser)
17019 {
17020   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17021   /* Look for the `operator' keyword.  */
17022   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17023     return error_mark_node;
17024   /* And then the name of the operator itself.  */
17025   return cp_parser_operator (parser, start_loc);
17026 }
17027 
17028 /* Return an identifier node for a user-defined literal operator.
17029    The suffix identifier is chained to the operator name identifier.  */
17030 
17031 tree
cp_literal_operator_id(const char * name)17032 cp_literal_operator_id (const char* name)
17033 {
17034   tree identifier;
17035   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17036 			      + strlen (name) + 10);
17037   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17038   identifier = get_identifier (buffer);
17039   XDELETEVEC (buffer);
17040 
17041   return identifier;
17042 }
17043 
17044 /* Parse an operator.
17045 
17046    operator:
17047      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17048      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17049      || ++ -- , ->* -> () []
17050 
17051    GNU Extensions:
17052 
17053    operator:
17054      <? >? <?= >?=
17055 
17056    Returns an IDENTIFIER_NODE for the operator which is a
17057    human-readable spelling of the identifier, e.g., `operator +'.  */
17058 
17059 static cp_expr
cp_parser_operator(cp_parser * parser,location_t start_loc)17060 cp_parser_operator (cp_parser* parser, location_t start_loc)
17061 {
17062   tree id = NULL_TREE;
17063   cp_token *token;
17064   bool utf8 = false;
17065 
17066   /* Peek at the next token.  */
17067   token = cp_lexer_peek_token (parser->lexer);
17068 
17069   location_t end_loc = token->location;
17070 
17071   /* Figure out which operator we have.  */
17072   enum tree_code op = ERROR_MARK;
17073   bool assop = false;
17074   bool consumed = false;
17075   switch (token->type)
17076     {
17077     case CPP_KEYWORD:
17078       {
17079 	/* The keyword should be either `new', `delete' or `co_await'.  */
17080 	if (token->keyword == RID_NEW)
17081 	  op = NEW_EXPR;
17082 	else if (token->keyword == RID_DELETE)
17083 	  op = DELETE_EXPR;
17084 	else if (token->keyword == RID_CO_AWAIT)
17085 	  op = CO_AWAIT_EXPR;
17086 	else
17087 	  break;
17088 
17089 	/* Consume the `new', `delete' or co_await token.  */
17090 	end_loc = cp_lexer_consume_token (parser->lexer)->location;
17091 
17092 	/* Peek at the next token.  */
17093 	token = cp_lexer_peek_token (parser->lexer);
17094 	/* If it's a `[' token then this is the array variant of the
17095 	   operator.  */
17096 	if (token->type == CPP_OPEN_SQUARE
17097 	    && op != CO_AWAIT_EXPR)
17098 	  {
17099 	    /* Consume the `[' token.  */
17100 	    cp_lexer_consume_token (parser->lexer);
17101 	    /* Look for the `]' token.  */
17102 	    if (cp_token *close_token
17103 		= cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17104 	      end_loc = close_token->location;
17105 	    op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17106 	  }
17107 	consumed = true;
17108 	break;
17109       }
17110 
17111     case CPP_PLUS:
17112       op = PLUS_EXPR;
17113       break;
17114 
17115     case CPP_MINUS:
17116       op = MINUS_EXPR;
17117       break;
17118 
17119     case CPP_MULT:
17120       op = MULT_EXPR;
17121       break;
17122 
17123     case CPP_DIV:
17124       op = TRUNC_DIV_EXPR;
17125       break;
17126 
17127     case CPP_MOD:
17128       op = TRUNC_MOD_EXPR;
17129       break;
17130 
17131     case CPP_XOR:
17132       op = BIT_XOR_EXPR;
17133       break;
17134 
17135     case CPP_AND:
17136       op = BIT_AND_EXPR;
17137       break;
17138 
17139     case CPP_OR:
17140       op = BIT_IOR_EXPR;
17141       break;
17142 
17143     case CPP_COMPL:
17144       op = BIT_NOT_EXPR;
17145       break;
17146 
17147     case CPP_NOT:
17148       op = TRUTH_NOT_EXPR;
17149       break;
17150 
17151     case CPP_EQ:
17152       assop = true;
17153       op = NOP_EXPR;
17154       break;
17155 
17156     case CPP_LESS:
17157       op = LT_EXPR;
17158       break;
17159 
17160     case CPP_GREATER:
17161       op = GT_EXPR;
17162       break;
17163 
17164     case CPP_PLUS_EQ:
17165       assop = true;
17166       op = PLUS_EXPR;
17167       break;
17168 
17169     case CPP_MINUS_EQ:
17170       assop = true;
17171       op = MINUS_EXPR;
17172       break;
17173 
17174     case CPP_MULT_EQ:
17175       assop = true;
17176       op = MULT_EXPR;
17177       break;
17178 
17179     case CPP_DIV_EQ:
17180       assop = true;
17181       op = TRUNC_DIV_EXPR;
17182       break;
17183 
17184     case CPP_MOD_EQ:
17185       assop = true;
17186       op = TRUNC_MOD_EXPR;
17187       break;
17188 
17189     case CPP_XOR_EQ:
17190       assop = true;
17191       op = BIT_XOR_EXPR;
17192       break;
17193 
17194     case CPP_AND_EQ:
17195       assop = true;
17196       op = BIT_AND_EXPR;
17197       break;
17198 
17199     case CPP_OR_EQ:
17200       assop = true;
17201       op = BIT_IOR_EXPR;
17202       break;
17203 
17204     case CPP_LSHIFT:
17205       op = LSHIFT_EXPR;
17206       break;
17207 
17208     case CPP_RSHIFT:
17209       op = RSHIFT_EXPR;
17210       break;
17211 
17212     case CPP_LSHIFT_EQ:
17213       assop = true;
17214       op = LSHIFT_EXPR;
17215       break;
17216 
17217     case CPP_RSHIFT_EQ:
17218       assop = true;
17219       op = RSHIFT_EXPR;
17220       break;
17221 
17222     case CPP_EQ_EQ:
17223       op = EQ_EXPR;
17224       break;
17225 
17226     case CPP_NOT_EQ:
17227       op = NE_EXPR;
17228       break;
17229 
17230     case CPP_LESS_EQ:
17231       op = LE_EXPR;
17232       break;
17233 
17234     case CPP_GREATER_EQ:
17235       op = GE_EXPR;
17236       break;
17237 
17238     case CPP_SPACESHIP:
17239       op = SPACESHIP_EXPR;
17240       break;
17241 
17242     case CPP_AND_AND:
17243       op = TRUTH_ANDIF_EXPR;
17244       break;
17245 
17246     case CPP_OR_OR:
17247       op = TRUTH_ORIF_EXPR;
17248       break;
17249 
17250     case CPP_PLUS_PLUS:
17251       op = POSTINCREMENT_EXPR;
17252       break;
17253 
17254     case CPP_MINUS_MINUS:
17255       op = PREDECREMENT_EXPR;
17256       break;
17257 
17258     case CPP_COMMA:
17259       op = COMPOUND_EXPR;
17260       break;
17261 
17262     case CPP_DEREF_STAR:
17263       op = MEMBER_REF;
17264       break;
17265 
17266     case CPP_DEREF:
17267       op = COMPONENT_REF;
17268       break;
17269 
17270     case CPP_QUERY:
17271       op = COND_EXPR;
17272       /* Consume the `?'.  */
17273       cp_lexer_consume_token (parser->lexer);
17274       /* Look for the matching `:'.  */
17275       cp_parser_require (parser, CPP_COLON, RT_COLON);
17276       consumed = true;
17277       break;
17278 
17279     case CPP_OPEN_PAREN:
17280       {
17281         /* Consume the `('.  */
17282         matching_parens parens;
17283         parens.consume_open (parser);
17284         /* Look for the matching `)'.  */
17285         token = parens.require_close (parser);
17286         if (token)
17287 	  end_loc = token->location;
17288 	op = CALL_EXPR;
17289 	consumed = true;
17290 	break;
17291       }
17292 
17293     case CPP_OPEN_SQUARE:
17294       /* Consume the `['.  */
17295       cp_lexer_consume_token (parser->lexer);
17296       /* Look for the matching `]'.  */
17297       token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17298       if (token)
17299 	end_loc = token->location;
17300       op = ARRAY_REF;
17301       consumed = true;
17302       break;
17303 
17304     case CPP_UTF8STRING:
17305     case CPP_UTF8STRING_USERDEF:
17306       utf8 = true;
17307       /* FALLTHRU */
17308     case CPP_STRING:
17309     case CPP_WSTRING:
17310     case CPP_STRING16:
17311     case CPP_STRING32:
17312     case CPP_STRING_USERDEF:
17313     case CPP_WSTRING_USERDEF:
17314     case CPP_STRING16_USERDEF:
17315     case CPP_STRING32_USERDEF:
17316       {
17317 	cp_expr str;
17318 	tree string_tree;
17319 	int sz, len;
17320 
17321 	if (cxx_dialect == cxx98)
17322 	  maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17323 
17324 	/* Consume the string.  */
17325 	str = cp_parser_string_literal (parser, /*translate=*/true,
17326 				      /*wide_ok=*/true, /*lookup_udlit=*/false);
17327 	if (str == error_mark_node)
17328 	  return error_mark_node;
17329 	else if (TREE_CODE (str) == USERDEF_LITERAL)
17330 	  {
17331 	    string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17332 	    id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17333 	    end_loc = str.get_location ();
17334 	  }
17335 	else
17336 	  {
17337 	    string_tree = str;
17338 	    /* Look for the suffix identifier.  */
17339 	    token = cp_lexer_peek_token (parser->lexer);
17340 	    if (token->type == CPP_NAME)
17341 	      {
17342 		id = cp_parser_identifier (parser);
17343 		end_loc = token->location;
17344 	      }
17345 	    else if (token->type == CPP_KEYWORD)
17346 	      {
17347 		error ("unexpected keyword;"
17348 		       " remove space between quotes and suffix identifier");
17349 		return error_mark_node;
17350 	      }
17351 	    else
17352 	      {
17353 		error ("expected suffix identifier");
17354 		return error_mark_node;
17355 	      }
17356 	  }
17357 	sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17358 			       (TREE_TYPE (TREE_TYPE (string_tree))));
17359 	len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17360 	if (len != 0)
17361 	  {
17362 	    error ("expected empty string after %<operator%> keyword");
17363 	    return error_mark_node;
17364 	  }
17365 	if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17366 	    != char_type_node)
17367 	  {
17368 	    error ("invalid encoding prefix in literal operator");
17369 	    return error_mark_node;
17370 	  }
17371 	if (id != error_mark_node)
17372 	  {
17373 	    const char *name = IDENTIFIER_POINTER (id);
17374 	    id = cp_literal_operator_id (name);
17375 	  }
17376 	/* Generate a location of the form:
17377 	     "" _suffix_identifier
17378 	     ^~~~~~~~~~~~~~~~~~~~~
17379 	   with caret == start at the start token, finish at the end of the
17380 	   suffix identifier.  */
17381 	location_t combined_loc
17382 	  = make_location (start_loc, start_loc, parser->lexer);
17383 	return cp_expr (id, combined_loc);
17384       }
17385 
17386     default:
17387       /* Anything else is an error.  */
17388       break;
17389     }
17390 
17391   /* If we have selected an identifier, we need to consume the
17392      operator token.  */
17393   if (op != ERROR_MARK)
17394     {
17395       id = ovl_op_identifier (assop, op);
17396       if (!consumed)
17397 	cp_lexer_consume_token (parser->lexer);
17398     }
17399   /* Otherwise, no valid operator name was present.  */
17400   else
17401     {
17402       cp_parser_error (parser, "expected operator");
17403       id = error_mark_node;
17404     }
17405 
17406   start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17407   return cp_expr (id, start_loc);
17408 }
17409 
17410 /* Parse a template-declaration.
17411 
17412    template-declaration:
17413      export [opt] template < template-parameter-list > declaration
17414 
17415    If MEMBER_P is TRUE, this template-declaration occurs within a
17416    class-specifier.
17417 
17418    The grammar rule given by the standard isn't correct.  What
17419    is really meant is:
17420 
17421    template-declaration:
17422      export [opt] template-parameter-list-seq
17423        decl-specifier-seq [opt] init-declarator [opt] ;
17424      export [opt] template-parameter-list-seq
17425        function-definition
17426 
17427    template-parameter-list-seq:
17428      template-parameter-list-seq [opt]
17429      template < template-parameter-list >
17430 
17431    Concept Extensions:
17432 
17433    template-parameter-list-seq:
17434      template < template-parameter-list > requires-clause [opt]
17435 
17436    requires-clause:
17437      requires logical-or-expression  */
17438 
17439 static void
cp_parser_template_declaration(cp_parser * parser,bool member_p)17440 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17441 {
17442   /* Check for `export'.  */
17443   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17444     {
17445       /* Consume the `export' token.  */
17446       cp_lexer_consume_token (parser->lexer);
17447       /* Warn that this use of export is deprecated.  */
17448       if (cxx_dialect < cxx11)
17449 	warning (0, "keyword %<export%> not implemented, and will be ignored");
17450       else if (cxx_dialect < cxx20)
17451 	warning (0, "keyword %<export%> is deprecated, and is ignored");
17452       else
17453 	warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17454     }
17455 
17456   cp_parser_template_declaration_after_export (parser, member_p);
17457 }
17458 
17459 /* Parse a template-parameter-list.
17460 
17461    template-parameter-list:
17462      template-parameter
17463      template-parameter-list , template-parameter
17464 
17465    Returns a TREE_LIST.  Each node represents a template parameter.
17466    The nodes are connected via their TREE_CHAINs.  */
17467 
17468 static tree
cp_parser_template_parameter_list(cp_parser * parser)17469 cp_parser_template_parameter_list (cp_parser* parser)
17470 {
17471   tree parameter_list = NULL_TREE;
17472 
17473   /* Don't create wrapper nodes within a template-parameter-list,
17474      since we don't want to have different types based on the
17475      spelling location of constants and decls within them.  */
17476   auto_suppress_location_wrappers sentinel;
17477 
17478   begin_template_parm_list ();
17479 
17480   /* The loop below parses the template parms.  We first need to know
17481      the total number of template parms to be able to compute proper
17482      canonical types of each dependent type. So after the loop, when
17483      we know the total number of template parms,
17484      end_template_parm_list computes the proper canonical types and
17485      fixes up the dependent types accordingly.  */
17486   while (true)
17487     {
17488       tree parameter;
17489       bool is_non_type;
17490       bool is_parameter_pack;
17491       location_t parm_loc;
17492 
17493       /* Parse the template-parameter.  */
17494       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17495       parameter = cp_parser_template_parameter (parser,
17496                                                 &is_non_type,
17497                                                 &is_parameter_pack);
17498       /* Add it to the list.  */
17499       if (parameter != error_mark_node)
17500 	parameter_list = process_template_parm (parameter_list,
17501 						parm_loc,
17502 						parameter,
17503 						is_non_type,
17504 						is_parameter_pack);
17505       else
17506        {
17507          tree err_parm = build_tree_list (parameter, parameter);
17508          parameter_list = chainon (parameter_list, err_parm);
17509        }
17510 
17511       /* If the next token is not a `,', we're done.  */
17512       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17513 	break;
17514       /* Otherwise, consume the `,' token.  */
17515       cp_lexer_consume_token (parser->lexer);
17516     }
17517 
17518   return end_template_parm_list (parameter_list);
17519 }
17520 
17521 /* Parse a introduction-list.
17522 
17523    introduction-list:
17524      introduced-parameter
17525      introduction-list , introduced-parameter
17526 
17527    introduced-parameter:
17528      ...[opt] identifier
17529 
17530    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
17531    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
17532    WILDCARD_DECL will also have DECL_NAME set and token location in
17533    DECL_SOURCE_LOCATION.  */
17534 
17535 static tree
cp_parser_introduction_list(cp_parser * parser)17536 cp_parser_introduction_list (cp_parser *parser)
17537 {
17538   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17539 
17540   while (true)
17541     {
17542       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17543       if (is_pack)
17544 	cp_lexer_consume_token (parser->lexer);
17545 
17546       tree identifier = cp_parser_identifier (parser);
17547       if (identifier == error_mark_node)
17548 	break;
17549 
17550       /* Build placeholder. */
17551       tree parm = build_nt (WILDCARD_DECL);
17552       DECL_SOURCE_LOCATION (parm)
17553 	= cp_lexer_peek_token (parser->lexer)->location;
17554       DECL_NAME (parm) = identifier;
17555       WILDCARD_PACK_P (parm) = is_pack;
17556       vec_safe_push (introduction_vec, parm);
17557 
17558       /* If the next token is not a `,', we're done.  */
17559       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17560 	break;
17561       /* Otherwise, consume the `,' token.  */
17562       cp_lexer_consume_token (parser->lexer);
17563     }
17564 
17565   /* Convert the vec into a TREE_VEC.  */
17566   tree introduction_list = make_tree_vec (introduction_vec->length ());
17567   unsigned int n;
17568   tree parm;
17569   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17570     TREE_VEC_ELT (introduction_list, n) = parm;
17571 
17572   release_tree_vector (introduction_vec);
17573   return introduction_list;
17574 }
17575 
17576 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17577    is an abstract declarator. */
17578 
17579 static inline cp_declarator*
get_id_declarator(cp_declarator * declarator)17580 get_id_declarator (cp_declarator *declarator)
17581 {
17582   cp_declarator *d = declarator;
17583   while (d && d->kind != cdk_id)
17584     d = d->declarator;
17585   return d;
17586 }
17587 
17588 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17589    is an abstract declarator. */
17590 
17591 static inline tree
get_unqualified_id(cp_declarator * declarator)17592 get_unqualified_id (cp_declarator *declarator)
17593 {
17594   declarator = get_id_declarator (declarator);
17595   if (declarator)
17596     return declarator->u.id.unqualified_name;
17597   else
17598     return NULL_TREE;
17599 }
17600 
17601 /* Returns true if TYPE would declare a constrained constrained-parameter.  */
17602 
17603 static inline bool
is_constrained_parameter(tree type)17604 is_constrained_parameter (tree type)
17605 {
17606   return (type
17607           && TREE_CODE (type) == TYPE_DECL
17608           && CONSTRAINED_PARM_CONCEPT (type)
17609           && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17610 }
17611 
17612 /* Returns true if PARM declares a constrained-parameter. */
17613 
17614 static inline bool
is_constrained_parameter(cp_parameter_declarator * parm)17615 is_constrained_parameter (cp_parameter_declarator *parm)
17616 {
17617   return is_constrained_parameter (parm->decl_specifiers.type);
17618 }
17619 
17620 /* Check that the type parameter is only a declarator-id, and that its
17621    type is not cv-qualified. */
17622 
17623 bool
cp_parser_check_constrained_type_parm(cp_parser * parser,cp_parameter_declarator * parm)17624 cp_parser_check_constrained_type_parm (cp_parser *parser,
17625 				       cp_parameter_declarator *parm)
17626 {
17627   if (!parm->declarator)
17628     return true;
17629 
17630   if (parm->declarator->kind != cdk_id)
17631     {
17632       cp_parser_error (parser, "invalid constrained type parameter");
17633       return false;
17634     }
17635 
17636   /* Don't allow cv-qualified type parameters.  */
17637   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17638       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17639     {
17640       cp_parser_error (parser, "cv-qualified type parameter");
17641       return false;
17642     }
17643 
17644   return true;
17645 }
17646 
17647 /* Finish parsing/processing a template type parameter and checking
17648    various restrictions. */
17649 
17650 static inline tree
cp_parser_constrained_type_template_parm(cp_parser * parser,tree id,cp_parameter_declarator * parmdecl)17651 cp_parser_constrained_type_template_parm (cp_parser *parser,
17652                                           tree id,
17653                                           cp_parameter_declarator* parmdecl)
17654 {
17655   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17656     return finish_template_type_parm (class_type_node, id);
17657   else
17658     return error_mark_node;
17659 }
17660 
17661 static tree
finish_constrained_template_template_parm(tree proto,tree id)17662 finish_constrained_template_template_parm (tree proto, tree id)
17663 {
17664   /* FIXME: This should probably be copied, and we may need to adjust
17665      the template parameter depths.  */
17666   tree saved_parms = current_template_parms;
17667   begin_template_parm_list ();
17668   current_template_parms = DECL_TEMPLATE_PARMS (proto);
17669   end_template_parm_list ();
17670 
17671   tree parm = finish_template_template_parm (class_type_node, id);
17672   current_template_parms = saved_parms;
17673 
17674   return parm;
17675 }
17676 
17677 /* Finish parsing/processing a template template parameter by borrowing
17678    the template parameter list from the prototype parameter.  */
17679 
17680 static tree
cp_parser_constrained_template_template_parm(cp_parser * parser,tree proto,tree id,cp_parameter_declarator * parmdecl)17681 cp_parser_constrained_template_template_parm (cp_parser *parser,
17682                                               tree proto,
17683                                               tree id,
17684                                               cp_parameter_declarator *parmdecl)
17685 {
17686   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17687     return error_mark_node;
17688   return finish_constrained_template_template_parm (proto, id);
17689 }
17690 
17691 /* Create a new non-type template parameter from the given PARM
17692    declarator.  */
17693 
17694 static tree
cp_parser_constrained_non_type_template_parm(bool * is_non_type,cp_parameter_declarator * parm)17695 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17696 					      cp_parameter_declarator *parm)
17697 {
17698   *is_non_type = true;
17699   cp_declarator *decl = parm->declarator;
17700   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17701   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17702   return grokdeclarator (decl, specs, TPARM, 0, NULL);
17703 }
17704 
17705 /* Build a constrained template parameter based on the PARMDECL
17706    declarator. The type of PARMDECL is the constrained type, which
17707    refers to the prototype template parameter that ultimately
17708    specifies the type of the declared parameter. */
17709 
17710 static tree
finish_constrained_parameter(cp_parser * parser,cp_parameter_declarator * parmdecl,bool * is_non_type)17711 finish_constrained_parameter (cp_parser *parser,
17712                               cp_parameter_declarator *parmdecl,
17713                               bool *is_non_type)
17714 {
17715   tree decl = parmdecl->decl_specifiers.type;
17716   tree id = get_unqualified_id (parmdecl->declarator);
17717   tree def = parmdecl->default_argument;
17718   tree proto = DECL_INITIAL (decl);
17719 
17720   /* Build the parameter. Return an error if the declarator was invalid. */
17721   tree parm;
17722   if (TREE_CODE (proto) == TYPE_DECL)
17723     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17724   else if (TREE_CODE (proto) == TEMPLATE_DECL)
17725     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17726 							 parmdecl);
17727   else
17728     parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17729   if (parm == error_mark_node)
17730     return error_mark_node;
17731 
17732   /* Finish the parameter decl and create a node attaching the
17733      default argument and constraint.  */
17734   parm = build_tree_list (def, parm);
17735   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17736 
17737   return parm;
17738 }
17739 
17740 /* Returns true if the parsed type actually represents the declaration
17741    of a type template-parameter.  */
17742 
17743 static bool
declares_constrained_type_template_parameter(tree type)17744 declares_constrained_type_template_parameter (tree type)
17745 {
17746   return (is_constrained_parameter (type)
17747 	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17748 }
17749 
17750 /* Returns true if the parsed type actually represents the declaration of
17751    a template template-parameter.  */
17752 
17753 static bool
declares_constrained_template_template_parameter(tree type)17754 declares_constrained_template_template_parameter (tree type)
17755 {
17756   return (is_constrained_parameter (type)
17757 	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17758 }
17759 
17760 /* Parse a default argument for a type template-parameter.
17761    Note that diagnostics are handled in cp_parser_template_parameter.  */
17762 
17763 static tree
cp_parser_default_type_template_argument(cp_parser * parser)17764 cp_parser_default_type_template_argument (cp_parser *parser)
17765 {
17766   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17767 
17768   /* Consume the `=' token.  */
17769   cp_lexer_consume_token (parser->lexer);
17770 
17771   cp_token *token = cp_lexer_peek_token (parser->lexer);
17772 
17773   /* Tell cp_parser_lambda_expression this is a default argument.  */
17774   auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17775   parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17776 
17777   /* Parse the default-argument.  */
17778   push_deferring_access_checks (dk_no_deferred);
17779   tree default_argument = cp_parser_type_id (parser,
17780 					     CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17781 					     NULL);
17782   pop_deferring_access_checks ();
17783 
17784   if (flag_concepts && type_uses_auto (default_argument))
17785     {
17786       error_at (token->location,
17787 		"invalid use of %<auto%> in default template argument");
17788       return error_mark_node;
17789     }
17790 
17791   return default_argument;
17792 }
17793 
17794 /* Parse a default argument for a template template-parameter.  */
17795 
17796 static tree
cp_parser_default_template_template_argument(cp_parser * parser)17797 cp_parser_default_template_template_argument (cp_parser *parser)
17798 {
17799   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17800 
17801   bool is_template;
17802 
17803   /* Consume the `='.  */
17804   cp_lexer_consume_token (parser->lexer);
17805   /* Parse the id-expression.  */
17806   push_deferring_access_checks (dk_no_deferred);
17807   /* save token before parsing the id-expression, for error
17808      reporting */
17809   const cp_token* token = cp_lexer_peek_token (parser->lexer);
17810   tree default_argument
17811     = cp_parser_id_expression (parser,
17812                                /*template_keyword_p=*/false,
17813                                /*check_dependency_p=*/true,
17814                                /*template_p=*/&is_template,
17815                                /*declarator_p=*/false,
17816                                /*optional_p=*/false);
17817   if (TREE_CODE (default_argument) == TYPE_DECL)
17818     /* If the id-expression was a template-id that refers to
17819        a template-class, we already have the declaration here,
17820        so no further lookup is needed.  */
17821     ;
17822   else
17823     /* Look up the name.  */
17824     default_argument
17825       = cp_parser_lookup_name (parser, default_argument,
17826                                none_type,
17827                                /*is_template=*/is_template,
17828                                /*is_namespace=*/false,
17829                                /*check_dependency=*/true,
17830                                /*ambiguous_decls=*/NULL,
17831                                token->location);
17832   /* See if the default argument is valid.  */
17833   default_argument = check_template_template_default_arg (default_argument);
17834   pop_deferring_access_checks ();
17835   return default_argument;
17836 }
17837 
17838 /* Parse a template-parameter.
17839 
17840    template-parameter:
17841      type-parameter
17842      parameter-declaration
17843 
17844    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
17845    the parameter.  The TREE_PURPOSE is the default value, if any.
17846    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
17847    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
17848    set to true iff this parameter is a parameter pack. */
17849 
17850 static tree
cp_parser_template_parameter(cp_parser * parser,bool * is_non_type,bool * is_parameter_pack)17851 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
17852                               bool *is_parameter_pack)
17853 {
17854   cp_token *token;
17855   cp_parameter_declarator *parameter_declarator;
17856   tree parm;
17857 
17858   /* Assume it is a type parameter or a template parameter.  */
17859   *is_non_type = false;
17860   /* Assume it not a parameter pack. */
17861   *is_parameter_pack = false;
17862   /* Peek at the next token.  */
17863   token = cp_lexer_peek_token (parser->lexer);
17864   /* If it is `template', we have a type-parameter.  */
17865   if (token->keyword == RID_TEMPLATE)
17866     return cp_parser_type_parameter (parser, is_parameter_pack);
17867   /* If it is `class' or `typename' we do not know yet whether it is a
17868      type parameter or a non-type parameter.  Consider:
17869 
17870        template <typename T, typename T::X X> ...
17871 
17872      or:
17873 
17874        template <class C, class D*> ...
17875 
17876      Here, the first parameter is a type parameter, and the second is
17877      a non-type parameter.  We can tell by looking at the token after
17878      the identifier -- if it is a `,', `=', or `>' then we have a type
17879      parameter.  */
17880   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
17881     {
17882       /* Peek at the token after `class' or `typename'.  */
17883       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17884       /* If it's an ellipsis, we have a template type parameter
17885          pack. */
17886       if (token->type == CPP_ELLIPSIS)
17887         return cp_parser_type_parameter (parser, is_parameter_pack);
17888       /* If it's an identifier, skip it.  */
17889       if (token->type == CPP_NAME)
17890 	token = cp_lexer_peek_nth_token (parser->lexer, 3);
17891       /* Now, see if the token looks like the end of a template
17892 	 parameter.  */
17893       if (token->type == CPP_COMMA
17894 	  || token->type == CPP_EQ
17895 	  || token->type == CPP_GREATER)
17896 	return cp_parser_type_parameter (parser, is_parameter_pack);
17897     }
17898 
17899   /* Otherwise, it is a non-type parameter or a constrained parameter.
17900 
17901      [temp.param]
17902 
17903      When parsing a default template-argument for a non-type
17904      template-parameter, the first non-nested `>' is taken as the end
17905      of the template parameter-list rather than a greater-than
17906      operator.  */
17907   parameter_declarator
17908      = cp_parser_parameter_declaration (parser,
17909 					CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17910 					/*template_parm_p=*/true,
17911 					/*parenthesized_p=*/NULL);
17912 
17913   if (!parameter_declarator)
17914     return error_mark_node;
17915 
17916   /* If the parameter declaration is marked as a parameter pack, set
17917    *IS_PARAMETER_PACK to notify the caller.  */
17918   if (parameter_declarator->template_parameter_pack_p)
17919     *is_parameter_pack = true;
17920 
17921   if (parameter_declarator->default_argument)
17922     {
17923       /* Can happen in some cases of erroneous input (c++/34892).  */
17924       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17925 	/* Consume the `...' for better error recovery.  */
17926 	cp_lexer_consume_token (parser->lexer);
17927     }
17928 
17929   /* The parameter may have been constrained type parameter.  */
17930   if (is_constrained_parameter (parameter_declarator))
17931     return finish_constrained_parameter (parser,
17932                                          parameter_declarator,
17933                                          is_non_type);
17934 
17935   // Now we're sure that the parameter is a non-type parameter.
17936   *is_non_type = true;
17937 
17938   parm = grokdeclarator (parameter_declarator->declarator,
17939 			 &parameter_declarator->decl_specifiers,
17940 			 TPARM, /*initialized=*/0,
17941 			 /*attrlist=*/NULL);
17942   if (parm == error_mark_node)
17943     return error_mark_node;
17944 
17945   return build_tree_list (parameter_declarator->default_argument, parm);
17946 }
17947 
17948 /* Parse a type-parameter.
17949 
17950    type-parameter:
17951      class identifier [opt]
17952      class identifier [opt] = type-id
17953      typename identifier [opt]
17954      typename identifier [opt] = type-id
17955      template < template-parameter-list > class identifier [opt]
17956      template < template-parameter-list > class identifier [opt]
17957        = id-expression
17958 
17959    GNU Extension (variadic templates):
17960 
17961    type-parameter:
17962      class ... identifier [opt]
17963      typename ... identifier [opt]
17964 
17965    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
17966    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
17967    the declaration of the parameter.
17968 
17969    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
17970 
17971 static tree
cp_parser_type_parameter(cp_parser * parser,bool * is_parameter_pack)17972 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
17973 {
17974   cp_token *token;
17975   tree parameter;
17976 
17977   /* Look for a keyword to tell us what kind of parameter this is.  */
17978   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
17979   if (!token)
17980     return error_mark_node;
17981 
17982   switch (token->keyword)
17983     {
17984     case RID_CLASS:
17985     case RID_TYPENAME:
17986       {
17987 	tree identifier;
17988 	tree default_argument;
17989 
17990         /* If the next token is an ellipsis, we have a template
17991            argument pack. */
17992         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17993           {
17994             /* Consume the `...' token. */
17995             cp_lexer_consume_token (parser->lexer);
17996             maybe_warn_variadic_templates ();
17997 
17998             *is_parameter_pack = true;
17999           }
18000 
18001 	/* If the next token is an identifier, then it names the
18002 	   parameter.  */
18003 	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18004 	  identifier = cp_parser_identifier (parser);
18005 	else
18006 	  identifier = NULL_TREE;
18007 
18008 	/* Create the parameter.  */
18009 	parameter = finish_template_type_parm (class_type_node, identifier);
18010 
18011 	/* If the next token is an `=', we have a default argument.  */
18012 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18013 	  {
18014 	    default_argument
18015 	      = cp_parser_default_type_template_argument (parser);
18016 
18017             /* Template parameter packs cannot have default
18018                arguments. */
18019             if (*is_parameter_pack)
18020               {
18021                 if (identifier)
18022                   error_at (token->location,
18023 			    "template parameter pack %qD cannot have a "
18024 			    "default argument", identifier);
18025                 else
18026                   error_at (token->location,
18027 			    "template parameter packs cannot have "
18028 			    "default arguments");
18029                 default_argument = NULL_TREE;
18030               }
18031 	    else if (check_for_bare_parameter_packs (default_argument))
18032 	      default_argument = error_mark_node;
18033 	  }
18034 	else
18035 	  default_argument = NULL_TREE;
18036 
18037 	/* Create the combined representation of the parameter and the
18038 	   default argument.  */
18039 	parameter = build_tree_list (default_argument, parameter);
18040       }
18041       break;
18042 
18043     case RID_TEMPLATE:
18044       {
18045 	tree identifier;
18046 	tree default_argument;
18047 
18048 	/* Look for the `<'.  */
18049 	cp_parser_require (parser, CPP_LESS, RT_LESS);
18050 	/* Parse the template-parameter-list.  */
18051 	cp_parser_template_parameter_list (parser);
18052 	/* Look for the `>'.  */
18053 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18054 
18055 	/* If template requirements are present, parse them.  */
18056 	if (flag_concepts)
18057           {
18058 	    tree reqs = get_shorthand_constraints (current_template_parms);
18059 	    if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18060               reqs = combine_constraint_expressions (reqs, dreqs);
18061 	    TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18062           }
18063 
18064 	/* Look for the `class' or 'typename' keywords.  */
18065 	cp_parser_type_parameter_key (parser);
18066         /* If the next token is an ellipsis, we have a template
18067            argument pack. */
18068         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18069           {
18070             /* Consume the `...' token. */
18071             cp_lexer_consume_token (parser->lexer);
18072             maybe_warn_variadic_templates ();
18073 
18074             *is_parameter_pack = true;
18075           }
18076 	/* If the next token is an `=', then there is a
18077 	   default-argument.  If the next token is a `>', we are at
18078 	   the end of the parameter-list.  If the next token is a `,',
18079 	   then we are at the end of this parameter.  */
18080 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18081 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18082 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18083 	  {
18084 	    identifier = cp_parser_identifier (parser);
18085 	    /* Treat invalid names as if the parameter were nameless.  */
18086 	    if (identifier == error_mark_node)
18087 	      identifier = NULL_TREE;
18088 	  }
18089 	else
18090 	  identifier = NULL_TREE;
18091 
18092 	/* Create the template parameter.  */
18093 	parameter = finish_template_template_parm (class_type_node,
18094 						   identifier);
18095 
18096 	/* If the next token is an `=', then there is a
18097 	   default-argument.  */
18098 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18099 	  {
18100 	    default_argument
18101 	      = cp_parser_default_template_template_argument (parser);
18102 
18103             /* Template parameter packs cannot have default
18104                arguments. */
18105             if (*is_parameter_pack)
18106               {
18107                 if (identifier)
18108                   error_at (token->location,
18109 			    "template parameter pack %qD cannot "
18110 			    "have a default argument",
18111 			    identifier);
18112                 else
18113                   error_at (token->location, "template parameter packs cannot "
18114 			    "have default arguments");
18115                 default_argument = NULL_TREE;
18116               }
18117 	  }
18118 	else
18119 	  default_argument = NULL_TREE;
18120 
18121 	/* Create the combined representation of the parameter and the
18122 	   default argument.  */
18123 	parameter = build_tree_list (default_argument, parameter);
18124       }
18125       break;
18126 
18127     default:
18128       gcc_unreachable ();
18129       break;
18130     }
18131 
18132   return parameter;
18133 }
18134 
18135 /* Parse a template-id.
18136 
18137    template-id:
18138      template-name < template-argument-list [opt] >
18139 
18140    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18141    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
18142    returned.  Otherwise, if the template-name names a function, or set
18143    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
18144    names a class, returns a TYPE_DECL for the specialization.
18145 
18146    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18147    uninstantiated templates.  */
18148 
18149 static tree
cp_parser_template_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,enum tag_types tag_type,bool is_declaration)18150 cp_parser_template_id (cp_parser *parser,
18151 		       bool template_keyword_p,
18152 		       bool check_dependency_p,
18153 		       enum tag_types tag_type,
18154 		       bool is_declaration)
18155 {
18156   tree templ;
18157   tree arguments;
18158   tree template_id;
18159   cp_token_position start_of_id = 0;
18160   cp_token *next_token = NULL, *next_token_2 = NULL;
18161   bool is_identifier;
18162 
18163   /* If the next token corresponds to a template-id, there is no need
18164      to reparse it.  */
18165   cp_token *token = cp_lexer_peek_token (parser->lexer);
18166 
18167   if (token->type == CPP_TEMPLATE_ID)
18168     {
18169       cp_lexer_consume_token (parser->lexer);
18170       return saved_checks_value (token->u.tree_check_value);
18171     }
18172 
18173   /* Avoid performing name lookup if there is no possibility of
18174      finding a template-id.  */
18175   if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18176       || (token->type == CPP_NAME
18177 	  && !cp_parser_nth_token_starts_template_argument_list_p
18178 	       (parser, 2)))
18179     {
18180       cp_parser_error (parser, "expected template-id");
18181       return error_mark_node;
18182     }
18183 
18184   /* Remember where the template-id starts.  */
18185   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18186     start_of_id = cp_lexer_token_position (parser->lexer, false);
18187 
18188   push_deferring_access_checks (dk_deferred);
18189 
18190   /* Parse the template-name.  */
18191   is_identifier = false;
18192   templ = cp_parser_template_name (parser, template_keyword_p,
18193 				   check_dependency_p,
18194 				   is_declaration,
18195 				   tag_type,
18196 				   &is_identifier);
18197 
18198   /* Push any access checks inside the firewall we're about to create.  */
18199   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18200   pop_deferring_access_checks ();
18201   if (templ == error_mark_node || is_identifier)
18202     return templ;
18203 
18204   /* Since we're going to preserve any side-effects from this parse, set up a
18205      firewall to protect our callers from cp_parser_commit_to_tentative_parse
18206      in the template arguments.  */
18207   tentative_firewall firewall (parser);
18208   reopen_deferring_access_checks (checks);
18209 
18210   /* If we find the sequence `[:' after a template-name, it's probably
18211      a digraph-typo for `< ::'. Substitute the tokens and check if we can
18212      parse correctly the argument list.  */
18213   if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18214        == CPP_OPEN_SQUARE)
18215       && next_token->flags & DIGRAPH
18216       && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18217 	  == CPP_COLON)
18218       && !(next_token_2->flags & PREV_WHITE))
18219     {
18220       cp_parser_parse_tentatively (parser);
18221       /* Change `:' into `::'.  */
18222       next_token_2->type = CPP_SCOPE;
18223       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18224 	 CPP_LESS.  */
18225       cp_lexer_consume_token (parser->lexer);
18226 
18227       /* Parse the arguments.  */
18228       arguments = cp_parser_enclosed_template_argument_list (parser);
18229       if (!cp_parser_parse_definitely (parser))
18230 	{
18231 	  /* If we couldn't parse an argument list, then we revert our changes
18232 	     and return simply an error. Maybe this is not a template-id
18233 	     after all.  */
18234 	  next_token_2->type = CPP_COLON;
18235 	  cp_parser_error (parser, "expected %<<%>");
18236 	  pop_deferring_access_checks ();
18237 	  return error_mark_node;
18238 	}
18239       /* Otherwise, emit an error about the invalid digraph, but continue
18240 	 parsing because we got our argument list.  */
18241       if (permerror (next_token->location,
18242 		     "%<<::%> cannot begin a template-argument list"))
18243 	{
18244 	  static bool hint = false;
18245 	  inform (next_token->location,
18246 		  "%<<:%> is an alternate spelling for %<[%>."
18247 		  " Insert whitespace between %<<%> and %<::%>");
18248 	  if (!hint && !flag_permissive)
18249 	    {
18250 	      inform (next_token->location, "(if you use %<-fpermissive%> "
18251 		      "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18252 		      "accept your code)");
18253 	      hint = true;
18254 	    }
18255 	}
18256     }
18257   else
18258     {
18259       /* Look for the `<' that starts the template-argument-list.  */
18260       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18261 	{
18262 	  pop_deferring_access_checks ();
18263 	  return error_mark_node;
18264 	}
18265       /* Parse the arguments.  */
18266       arguments = cp_parser_enclosed_template_argument_list (parser);
18267 
18268       if ((cxx_dialect > cxx17)
18269 	  && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18270 	  && !template_keyword_p
18271 	  && (cp_parser_error_occurred (parser)
18272 	      || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18273 	{
18274 	  /* This didn't go well.  */
18275 	  if (TREE_CODE (templ) == FUNCTION_DECL)
18276 	    {
18277 	      /* C++20 says that "function-name < a;" is now ill-formed.  */
18278 	      if (cp_parser_error_occurred (parser))
18279 		{
18280 		  error_at (token->location, "invalid template-argument-list");
18281 		  inform (token->location, "function name as the left hand "
18282 			  "operand of %<<%> is ill-formed in C++20; wrap the "
18283 			  "function name in %<()%>");
18284 		}
18285 	      else
18286 		/* We expect "f<targs>" to be followed by "(args)".  */
18287 		error_at (cp_lexer_peek_token (parser->lexer)->location,
18288 			  "expected %<(%> after template-argument-list");
18289 	      if (start_of_id)
18290 		/* Purge all subsequent tokens.  */
18291 		cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18292 	    }
18293 	  else
18294 	    cp_parser_simulate_error (parser);
18295 	  pop_deferring_access_checks ();
18296 	  return error_mark_node;
18297 	}
18298     }
18299 
18300   /* Set the location to be of the form:
18301      template-name < template-argument-list [opt] >
18302      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18303      with caret == start at the start of the template-name,
18304      ranging until the closing '>'.  */
18305   location_t combined_loc
18306     = make_location (token->location, token->location, parser->lexer);
18307 
18308   /* Check for concepts autos where they don't belong.  We could
18309      identify types in some cases of identifier TEMPL, looking ahead
18310      for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18311      types.  We reject them in functions, but if what we have is an
18312      identifier, even with none_type we can't conclude it's NOT a
18313      type, we have to wait for template substitution.  */
18314   if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18315     template_id = error_mark_node;
18316   /* Build a representation of the specialization.  */
18317   else if (identifier_p (templ))
18318     template_id = build_min_nt_loc (combined_loc,
18319 				    TEMPLATE_ID_EXPR,
18320 				    templ, arguments);
18321   else if (DECL_TYPE_TEMPLATE_P (templ)
18322 	   || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18323     {
18324       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18325 	 template (rather than some instantiation thereof) only if
18326 	 is not nested within some other construct.  For example, in
18327 	 "template <typename T> void f(T) { A<T>::", A<T> is just an
18328 	 instantiation of A.  */
18329       bool entering_scope
18330 	= (template_parm_scope_p ()
18331 	   && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18332       template_id
18333 	= finish_template_type (templ, arguments, entering_scope);
18334     }
18335   else if (concept_definition_p (templ))
18336     {
18337       /* The caller will decide whether this is a concept check or type
18338 	 constraint.  */
18339       template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18340 				boolean_type_node, templ, arguments);
18341     }
18342   else if (variable_template_p (templ))
18343     {
18344       template_id = lookup_template_variable (templ, arguments);
18345       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18346 	SET_EXPR_LOCATION (template_id, combined_loc);
18347     }
18348   else if (TREE_CODE (templ) == TYPE_DECL
18349 	   && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18350     {
18351       /* Some type template in dependent scope.  */
18352       tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18353       name = build_min_nt_loc (combined_loc,
18354 			       TEMPLATE_ID_EXPR,
18355 			       name, arguments);
18356       template_id = templ;
18357     }
18358   else
18359     {
18360       /* If it's not a class-template or a template-template, it should be
18361 	 a function-template.  */
18362       gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18363 
18364       template_id = lookup_template_function (templ, arguments);
18365       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18366 	SET_EXPR_LOCATION (template_id, combined_loc);
18367     }
18368 
18369   /* If parsing tentatively, replace the sequence of tokens that makes
18370      up the template-id with a CPP_TEMPLATE_ID token.  That way,
18371      should we re-parse the token stream, we will not have to repeat
18372      the effort required to do the parse, nor will we issue duplicate
18373      error messages about problems during instantiation of the
18374      template.  */
18375   if (start_of_id
18376       /* Don't do this if we had a parse error in a declarator; re-parsing
18377 	 might succeed if a name changes meaning (60361).  */
18378       && !(cp_parser_error_occurred (parser)
18379 	   && cp_parser_parsing_tentatively (parser)
18380 	   && parser->in_declarator_p))
18381     {
18382       /* Reset the contents of the START_OF_ID token.  */
18383       token->type = CPP_TEMPLATE_ID;
18384       token->location = combined_loc;
18385 
18386       /* Retrieve any deferred checks.  Do not pop this access checks yet
18387 	 so the memory will not be reclaimed during token replacing below.  */
18388       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18389       token->tree_check_p = true;
18390       token->u.tree_check_value->value = template_id;
18391       token->u.tree_check_value->checks = get_deferred_access_checks ();
18392       token->keyword = RID_MAX;
18393 
18394       /* Purge all subsequent tokens.  */
18395       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18396 
18397       /* ??? Can we actually assume that, if template_id ==
18398 	 error_mark_node, we will have issued a diagnostic to the
18399 	 user, as opposed to simply marking the tentative parse as
18400 	 failed?  */
18401       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18402 	error_at (token->location, "parse error in template argument list");
18403     }
18404 
18405   pop_to_parent_deferring_access_checks ();
18406   return template_id;
18407 }
18408 
18409 /* Like cp_parser_template_id, called in non-type context.  */
18410 
18411 static tree
cp_parser_template_id_expr(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool is_declaration)18412 cp_parser_template_id_expr (cp_parser *parser,
18413 			    bool template_keyword_p,
18414 			    bool check_dependency_p,
18415 			    bool is_declaration)
18416 {
18417   tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18418 				  none_type, is_declaration);
18419   if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18420       && concept_check_p (x))
18421     /* We didn't check the arguments in cp_parser_template_id; do that now.  */
18422     return build_concept_id (x);
18423   return x;
18424 }
18425 
18426 /* Parse a template-name.
18427 
18428    template-name:
18429      identifier
18430 
18431    The standard should actually say:
18432 
18433    template-name:
18434      identifier
18435      operator-function-id
18436 
18437    A defect report has been filed about this issue.
18438 
18439    A conversion-function-id cannot be a template name because they cannot
18440    be part of a template-id. In fact, looking at this code:
18441 
18442    a.operator K<int>()
18443 
18444    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18445    It is impossible to call a templated conversion-function-id with an
18446    explicit argument list, since the only allowed template parameter is
18447    the type to which it is converting.
18448 
18449    If TEMPLATE_KEYWORD_P is true, then we have just seen the
18450    `template' keyword, in a construction like:
18451 
18452      T::template f<3>()
18453 
18454    In that case `f' is taken to be a template-name, even though there
18455    is no way of knowing for sure.
18456 
18457    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18458    name refers to a set of overloaded functions, at least one of which
18459    is a template, or an IDENTIFIER_NODE with the name of the template,
18460    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
18461    names are looked up inside uninstantiated templates.  */
18462 
18463 static tree
cp_parser_template_name(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool is_declaration,enum tag_types tag_type,bool * is_identifier)18464 cp_parser_template_name (cp_parser* parser,
18465 			 bool template_keyword_p,
18466 			 bool check_dependency_p,
18467 			 bool is_declaration,
18468 			 enum tag_types tag_type,
18469 			 bool *is_identifier)
18470 {
18471   tree identifier;
18472   tree decl;
18473   cp_token *token = cp_lexer_peek_token (parser->lexer);
18474 
18475   /* If the next token is `operator', then we have either an
18476      operator-function-id or a conversion-function-id.  */
18477   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18478     {
18479       /* We don't know whether we're looking at an
18480 	 operator-function-id or a conversion-function-id.  */
18481       cp_parser_parse_tentatively (parser);
18482       /* Try an operator-function-id.  */
18483       identifier = cp_parser_operator_function_id (parser);
18484       /* If that didn't work, try a conversion-function-id.  */
18485       if (!cp_parser_parse_definitely (parser))
18486 	{
18487 	  cp_parser_error (parser, "expected template-name");
18488 	  return error_mark_node;
18489 	}
18490     }
18491   /* Look for the identifier.  */
18492   else
18493     identifier = cp_parser_identifier (parser);
18494 
18495   /* If we didn't find an identifier, we don't have a template-id.  */
18496   if (identifier == error_mark_node)
18497     return error_mark_node;
18498 
18499   /* If the name immediately followed the `template' keyword, then it
18500      is a template-name.  However, if the next token is not `<', then
18501      we do not treat it as a template-name, since it is not being used
18502      as part of a template-id.  This enables us to handle constructs
18503      like:
18504 
18505        template <typename T> struct S { S(); };
18506        template <typename T> S<T>::S();
18507 
18508      correctly.  We would treat `S' as a template -- if it were `S<T>'
18509      -- but we do not if there is no `<'.  */
18510 
18511   if (processing_template_decl
18512       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18513     {
18514       /* In a declaration, in a dependent context, we pretend that the
18515 	 "template" keyword was present in order to improve error
18516 	 recovery.  For example, given:
18517 
18518 	   template <typename T> void f(T::X<int>);
18519 
18520 	 we want to treat "X<int>" as a template-id.  */
18521       if (is_declaration
18522 	  && !template_keyword_p
18523 	  && parser->scope && TYPE_P (parser->scope)
18524 	  && check_dependency_p
18525 	  && dependent_scope_p (parser->scope)
18526 	  /* Do not do this for dtors (or ctors), since they never
18527 	     need the template keyword before their name.  */
18528 	  && !constructor_name_p (identifier, parser->scope))
18529 	{
18530 	  cp_token_position start = 0;
18531 
18532 	  /* Explain what went wrong.  */
18533 	  error_at (token->location, "non-template %qD used as template",
18534 		    identifier);
18535 	  inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18536 		  parser->scope, identifier);
18537 	  /* If parsing tentatively, find the location of the "<" token.  */
18538 	  if (cp_parser_simulate_error (parser))
18539 	    start = cp_lexer_token_position (parser->lexer, true);
18540 	  /* Parse the template arguments so that we can issue error
18541 	     messages about them.  */
18542 	  cp_lexer_consume_token (parser->lexer);
18543 	  cp_parser_enclosed_template_argument_list (parser);
18544 	  /* Skip tokens until we find a good place from which to
18545 	     continue parsing.  */
18546 	  cp_parser_skip_to_closing_parenthesis (parser,
18547 						 /*recovering=*/true,
18548 						 /*or_comma=*/true,
18549 						 /*consume_paren=*/false);
18550 	  /* If parsing tentatively, permanently remove the
18551 	     template argument list.  That will prevent duplicate
18552 	     error messages from being issued about the missing
18553 	     "template" keyword.  */
18554 	  if (start)
18555 	    cp_lexer_purge_tokens_after (parser->lexer, start);
18556 	  if (is_identifier)
18557 	    *is_identifier = true;
18558 	  parser->context->object_type = NULL_TREE;
18559 	  return identifier;
18560 	}
18561 
18562       /* If the "template" keyword is present, then there is generally
18563 	 no point in doing name-lookup, so we just return IDENTIFIER.
18564 	 But, if the qualifying scope is non-dependent then we can
18565 	 (and must) do name-lookup normally.  */
18566       if (template_keyword_p)
18567 	{
18568 	  tree scope = (parser->scope ? parser->scope
18569 			: parser->context->object_type);
18570 	  if (scope && TYPE_P (scope)
18571 	      && (!CLASS_TYPE_P (scope)
18572 		  || (check_dependency_p && dependent_scope_p (scope))))
18573 	    {
18574 	      /* We're optimizing away the call to cp_parser_lookup_name, but
18575 		 we still need to do this.  */
18576 	      parser->object_scope = parser->context->object_type;
18577 	      parser->context->object_type = NULL_TREE;
18578 	      return identifier;
18579 	    }
18580 	}
18581     }
18582 
18583   /* cp_parser_lookup_name clears OBJECT_TYPE.  */
18584   tree scope = (parser->scope ? parser->scope
18585 		: parser->context->object_type);
18586 
18587   /* Look up the name.  */
18588   decl = cp_parser_lookup_name (parser, identifier,
18589 				tag_type,
18590 				/*is_template=*/1 + template_keyword_p,
18591 				/*is_namespace=*/false,
18592 				check_dependency_p,
18593 				/*ambiguous_decls=*/NULL,
18594 				token->location);
18595 
18596   decl = strip_using_decl (decl);
18597 
18598   /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18599     template-argument-list if it follows a name that is not a
18600     conversion-function-id and
18601     - that follows the keyword template or a ~ after a nested-name-specifier or
18602     in a class member access expression, or
18603     - for which name lookup finds the injected-class-name of a class template
18604     or finds any declaration of a template, or
18605     - that is an unqualified name for which name lookup either finds one or
18606     more functions or finds nothing, or
18607     - that is a terminal name in a using-declarator (9.9), in a declarator-id
18608     (9.3.4), or in a type-only context other than a nested-name-specifier
18609     (13.8).  */
18610 
18611   /* If DECL is a template, then the name was a template-name.  */
18612   if (TREE_CODE (decl) == TEMPLATE_DECL)
18613     {
18614       if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18615 	  && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18616 	{
18617 	  tree d = DECL_TEMPLATE_RESULT (decl);
18618 	  tree attr;
18619 	  if (TREE_CODE (d) == TYPE_DECL)
18620 	    attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18621 	  else
18622 	    attr = DECL_ATTRIBUTES (d);
18623 	  if (TREE_UNAVAILABLE (decl))
18624 	    {
18625 	      attr = lookup_attribute ("unavailable", attr);
18626 	      error_unavailable_use (decl, attr);
18627 	    }
18628 	  else if (TREE_DEPRECATED (decl)
18629 		   && deprecated_state != DEPRECATED_SUPPRESS)
18630 	    {
18631 	      attr = lookup_attribute ("deprecated", attr);
18632 	      warn_deprecated_use (decl, attr);
18633 	    }
18634 	}
18635     }
18636   else
18637     {
18638       /* Look through an overload set for any templates.  */
18639       bool found = false;
18640 
18641       for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18642 	   !found && iter; ++iter)
18643 	if (TREE_CODE (*iter) == TEMPLATE_DECL)
18644 	  found = true;
18645 
18646       /* "an unqualified name for which name lookup either finds one or more
18647 	 functions or finds nothing".  */
18648       if (!found
18649 	  && (cxx_dialect > cxx17)
18650 	  && !scope
18651 	  && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18652 	  && tag_type == none_type)
18653 	{
18654 	  /* The "more functions" case.  Just use the OVERLOAD as normally.
18655 	     We don't use is_overloaded_fn here to avoid considering
18656 	     BASELINKs.  */
18657 	  if (TREE_CODE (decl) == OVERLOAD
18658 	      /* Name lookup found one function.  */
18659 	      || TREE_CODE (decl) == FUNCTION_DECL
18660 	      /* Name lookup found nothing.  */
18661 	      || decl == error_mark_node)
18662 	    found = true;
18663 	}
18664 
18665       /* "that follows the keyword template"..."in a type-only context" */
18666       if (!found && scope
18667 	  && (template_keyword_p || tag_type != none_type)
18668 	  && dependentish_scope_p (scope)
18669 	  && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18670 	found = true;
18671 
18672       if (!found)
18673 	{
18674 	  /* The name does not name a template.  */
18675 	  cp_parser_error (parser, "expected template-name");
18676 	  return error_mark_node;
18677 	}
18678       else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
18679 	       || TREE_CODE (decl) == USING_DECL
18680 	       /* cp_parser_template_id can only handle some TYPE_DECLs.  */
18681 	       || (TREE_CODE (decl) == TYPE_DECL
18682 		   && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
18683 	/* Repeat the lookup at instantiation time.  */
18684 	decl = identifier;
18685     }
18686 
18687   return decl;
18688 }
18689 
18690 /* Parse a template-argument-list.
18691 
18692    template-argument-list:
18693      template-argument ... [opt]
18694      template-argument-list , template-argument ... [opt]
18695 
18696    Returns a TREE_VEC containing the arguments.  */
18697 
18698 static tree
cp_parser_template_argument_list(cp_parser * parser)18699 cp_parser_template_argument_list (cp_parser* parser)
18700 {
18701   bool saved_in_template_argument_list_p;
18702   bool saved_ice_p;
18703   bool saved_non_ice_p;
18704 
18705   /* Don't create location wrapper nodes within a template-argument-list.  */
18706   auto_suppress_location_wrappers sentinel;
18707 
18708   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18709   parser->in_template_argument_list_p = true;
18710   /* Even if the template-id appears in an integral
18711      constant-expression, the contents of the argument list do
18712      not.  */
18713   saved_ice_p = parser->integral_constant_expression_p;
18714   parser->integral_constant_expression_p = false;
18715   saved_non_ice_p = parser->non_integral_constant_expression_p;
18716   parser->non_integral_constant_expression_p = false;
18717 
18718   /* Parse the arguments.  */
18719   auto_vec<tree, 10> args;
18720   do
18721     {
18722       if (!args.is_empty ())
18723 	/* Consume the comma.  */
18724 	cp_lexer_consume_token (parser->lexer);
18725 
18726       /* Parse the template-argument.  */
18727       tree argument = cp_parser_template_argument (parser);
18728 
18729       /* If the next token is an ellipsis, we're expanding a template
18730          argument pack. */
18731       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18732         {
18733 	  if (argument == error_mark_node)
18734 	    {
18735 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
18736 	      error_at (token->location,
18737 			"expected parameter pack before %<...%>");
18738 	    }
18739           /* Consume the `...' token. */
18740           cp_lexer_consume_token (parser->lexer);
18741 
18742           /* Make the argument into a TYPE_PACK_EXPANSION or
18743              EXPR_PACK_EXPANSION. */
18744           argument = make_pack_expansion (argument);
18745         }
18746 
18747       args.safe_push (argument);
18748     }
18749   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18750 
18751   int n_args = args.length ();
18752   tree vec = make_tree_vec (n_args);
18753 
18754   for (int i = 0; i < n_args; i++)
18755     TREE_VEC_ELT (vec, i) = args[i];
18756 
18757   parser->non_integral_constant_expression_p = saved_non_ice_p;
18758   parser->integral_constant_expression_p = saved_ice_p;
18759   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18760   if (CHECKING_P)
18761     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18762   return vec;
18763 }
18764 
18765 /* Parse a template-argument.
18766 
18767    template-argument:
18768      assignment-expression
18769      type-id
18770      id-expression
18771 
18772    The representation is that of an assignment-expression, type-id, or
18773    id-expression -- except that the qualified id-expression is
18774    evaluated, so that the value returned is either a DECL or an
18775    OVERLOAD.
18776 
18777    Although the standard says "assignment-expression", it forbids
18778    throw-expressions or assignments in the template argument.
18779    Therefore, we use "conditional-expression" instead.  */
18780 
18781 static tree
cp_parser_template_argument(cp_parser * parser)18782 cp_parser_template_argument (cp_parser* parser)
18783 {
18784   tree argument;
18785   bool template_p;
18786   bool address_p;
18787   bool maybe_type_id = false;
18788   cp_token *token = NULL, *argument_start_token = NULL;
18789   location_t loc = 0;
18790   cp_id_kind idk;
18791 
18792   /* There's really no way to know what we're looking at, so we just
18793      try each alternative in order.
18794 
18795        [temp.arg]
18796 
18797        In a template-argument, an ambiguity between a type-id and an
18798        expression is resolved to a type-id, regardless of the form of
18799        the corresponding template-parameter.
18800 
18801      Therefore, we try a type-id first.  */
18802   cp_parser_parse_tentatively (parser);
18803   argument = cp_parser_template_type_arg (parser);
18804   /* If there was no error parsing the type-id but the next token is a
18805      '>>', our behavior depends on which dialect of C++ we're
18806      parsing. In C++98, we probably found a typo for '> >'. But there
18807      are type-id which are also valid expressions. For instance:
18808 
18809      struct X { int operator >> (int); };
18810      template <int V> struct Foo {};
18811      Foo<X () >> 5> r;
18812 
18813      Here 'X()' is a valid type-id of a function type, but the user just
18814      wanted to write the expression "X() >> 5". Thus, we remember that we
18815      found a valid type-id, but we still try to parse the argument as an
18816      expression to see what happens.
18817 
18818      In C++0x, the '>>' will be considered two separate '>'
18819      tokens.  */
18820   if (!cp_parser_error_occurred (parser)
18821       && cxx_dialect == cxx98
18822       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18823     {
18824       maybe_type_id = true;
18825       cp_parser_abort_tentative_parse (parser);
18826     }
18827   else
18828     {
18829       /* If the next token isn't a `,' or a `>', then this argument wasn't
18830       really finished. This means that the argument is not a valid
18831       type-id.  */
18832       if (!cp_parser_next_token_ends_template_argument_p (parser))
18833 	cp_parser_error (parser, "expected template-argument");
18834       /* If that worked, we're done.  */
18835       if (cp_parser_parse_definitely (parser))
18836 	return argument;
18837     }
18838   /* We're still not sure what the argument will be.  */
18839   cp_parser_parse_tentatively (parser);
18840   /* Try a template.  */
18841   argument_start_token = cp_lexer_peek_token (parser->lexer);
18842   argument = cp_parser_id_expression (parser,
18843 				      /*template_keyword_p=*/false,
18844 				      /*check_dependency_p=*/true,
18845 				      &template_p,
18846 				      /*declarator_p=*/false,
18847 				      /*optional_p=*/false);
18848   /* If the next token isn't a `,' or a `>', then this argument wasn't
18849      really finished.  */
18850   if (!cp_parser_next_token_ends_template_argument_p (parser))
18851     cp_parser_error (parser, "expected template-argument");
18852   if (!cp_parser_error_occurred (parser))
18853     {
18854       /* Figure out what is being referred to.  If the id-expression
18855 	 was for a class template specialization, then we will have a
18856 	 TYPE_DECL at this point.  There is no need to do name lookup
18857 	 at this point in that case.  */
18858       if (TREE_CODE (argument) != TYPE_DECL)
18859 	argument = cp_parser_lookup_name (parser, argument,
18860 					  none_type,
18861 					  /*is_template=*/template_p,
18862 					  /*is_namespace=*/false,
18863 					  /*check_dependency=*/true,
18864 					  /*ambiguous_decls=*/NULL,
18865 					  argument_start_token->location);
18866       if (TREE_CODE (argument) != TEMPLATE_DECL
18867 	       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
18868 	cp_parser_error (parser, "expected template-name");
18869     }
18870   if (cp_parser_parse_definitely (parser))
18871     {
18872       if (TREE_UNAVAILABLE (argument))
18873 	error_unavailable_use (argument, NULL_TREE);
18874       else if (TREE_DEPRECATED (argument))
18875 	warn_deprecated_use (argument, NULL_TREE);
18876       return argument;
18877     }
18878   /* It must be a non-type argument.  In C++17 any constant-expression is
18879      allowed.  */
18880   if (cxx_dialect > cxx14)
18881     goto general_expr;
18882 
18883   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18884 
18885      -- an integral constant-expression of integral or enumeration
18886 	type; or
18887 
18888      -- the name of a non-type template-parameter; or
18889 
18890      -- the name of an object or function with external linkage...
18891 
18892      -- the address of an object or function with external linkage...
18893 
18894      -- a pointer to member...  */
18895   /* Look for a non-type template parameter.  */
18896   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18897     {
18898       cp_parser_parse_tentatively (parser);
18899       argument = cp_parser_primary_expression (parser,
18900 					       /*address_p=*/false,
18901 					       /*cast_p=*/false,
18902 					       /*template_arg_p=*/true,
18903 					       &idk);
18904       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
18905 	  || !cp_parser_next_token_ends_template_argument_p (parser))
18906 	cp_parser_simulate_error (parser);
18907       if (cp_parser_parse_definitely (parser))
18908 	return argument;
18909     }
18910 
18911   /* If the next token is "&", the argument must be the address of an
18912      object or function with external linkage.  */
18913   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
18914   if (address_p)
18915     {
18916       loc = cp_lexer_peek_token (parser->lexer)->location;
18917       cp_lexer_consume_token (parser->lexer);
18918     }
18919   /* See if we might have an id-expression.  */
18920   token = cp_lexer_peek_token (parser->lexer);
18921   if (token->type == CPP_NAME
18922       || token->keyword == RID_OPERATOR
18923       || token->type == CPP_SCOPE
18924       || token->type == CPP_TEMPLATE_ID
18925       || token->type == CPP_NESTED_NAME_SPECIFIER)
18926     {
18927       cp_parser_parse_tentatively (parser);
18928       argument = cp_parser_primary_expression (parser,
18929 					       address_p,
18930 					       /*cast_p=*/false,
18931 					       /*template_arg_p=*/true,
18932 					       &idk);
18933       if (cp_parser_error_occurred (parser)
18934 	  || !cp_parser_next_token_ends_template_argument_p (parser))
18935 	cp_parser_abort_tentative_parse (parser);
18936       else
18937 	{
18938 	  tree probe;
18939 
18940 	  if (INDIRECT_REF_P (argument))
18941 	    {
18942 	      /* Strip the dereference temporarily.  */
18943 	      gcc_assert (REFERENCE_REF_P (argument));
18944 	      argument = TREE_OPERAND (argument, 0);
18945 	    }
18946 
18947 	  /* If we're in a template, we represent a qualified-id referring
18948 	     to a static data member as a SCOPE_REF even if the scope isn't
18949 	     dependent so that we can check access control later.  */
18950 	  probe = argument;
18951 	  if (TREE_CODE (probe) == SCOPE_REF)
18952 	    probe = TREE_OPERAND (probe, 1);
18953 	  if (VAR_P (probe))
18954 	    {
18955 	      /* A variable without external linkage might still be a
18956 		 valid constant-expression, so no error is issued here
18957 		 if the external-linkage check fails.  */
18958 	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
18959 		cp_parser_simulate_error (parser);
18960 	    }
18961 	  else if (is_overloaded_fn (argument))
18962 	    /* All overloaded functions are allowed; if the external
18963 	       linkage test does not pass, an error will be issued
18964 	       later.  */
18965 	    ;
18966 	  else if (address_p
18967 		   && (TREE_CODE (argument) == OFFSET_REF
18968 		       || TREE_CODE (argument) == SCOPE_REF))
18969 	    /* A pointer-to-member.  */
18970 	    ;
18971 	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
18972 	    ;
18973 	  else
18974 	    cp_parser_simulate_error (parser);
18975 
18976 	  if (cp_parser_parse_definitely (parser))
18977 	    {
18978 	      if (address_p)
18979 		argument = build_x_unary_op (loc, ADDR_EXPR, argument,
18980 					     NULL_TREE, tf_warning_or_error);
18981 	      else
18982 		argument = convert_from_reference (argument);
18983 	      return argument;
18984 	    }
18985 	}
18986     }
18987   /* If the argument started with "&", there are no other valid
18988      alternatives at this point.  */
18989   if (address_p)
18990     {
18991       cp_parser_error (parser, "invalid non-type template argument");
18992       return error_mark_node;
18993     }
18994 
18995  general_expr:
18996   /* If the argument wasn't successfully parsed as a type-id followed
18997      by '>>', the argument can only be a constant expression now.
18998      Otherwise, we try parsing the constant-expression tentatively,
18999      because the argument could really be a type-id.  */
19000   if (maybe_type_id)
19001     cp_parser_parse_tentatively (parser);
19002 
19003   if (cxx_dialect <= cxx14)
19004     argument = cp_parser_constant_expression (parser);
19005   else
19006     {
19007       /* In C++20, we can encounter a braced-init-list.  */
19008       if (cxx_dialect >= cxx20
19009 	  && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19010 	{
19011 	  bool expr_non_constant_p;
19012 	  return cp_parser_braced_list (parser, &expr_non_constant_p);
19013 	}
19014 
19015       /* With C++17 generalized non-type template arguments we need to handle
19016 	 lvalue constant expressions, too.  */
19017       argument = cp_parser_assignment_expression (parser);
19018       require_potential_constant_expression (argument);
19019     }
19020 
19021   if (!maybe_type_id)
19022     return argument;
19023   if (!cp_parser_next_token_ends_template_argument_p (parser))
19024     cp_parser_error (parser, "expected template-argument");
19025   if (cp_parser_parse_definitely (parser))
19026     return argument;
19027   /* We did our best to parse the argument as a non type-id, but that
19028      was the only alternative that matched (albeit with a '>' after
19029      it). We can assume it's just a typo from the user, and a
19030      diagnostic will then be issued.  */
19031   return cp_parser_template_type_arg (parser);
19032 }
19033 
19034 /* Parse an explicit-instantiation.
19035 
19036    explicit-instantiation:
19037      template declaration
19038 
19039    Although the standard says `declaration', what it really means is:
19040 
19041    explicit-instantiation:
19042      template decl-specifier-seq [opt] declarator [opt] ;
19043 
19044    Things like `template int S<int>::i = 5, int S<double>::j;' are not
19045    supposed to be allowed.  A defect report has been filed about this
19046    issue.
19047 
19048    GNU Extension:
19049 
19050    explicit-instantiation:
19051      storage-class-specifier template
19052        decl-specifier-seq [opt] declarator [opt] ;
19053      function-specifier template
19054        decl-specifier-seq [opt] declarator [opt] ;  */
19055 
19056 static void
cp_parser_explicit_instantiation(cp_parser * parser)19057 cp_parser_explicit_instantiation (cp_parser* parser)
19058 {
19059   int declares_class_or_enum;
19060   cp_decl_specifier_seq decl_specifiers;
19061   tree extension_specifier = NULL_TREE;
19062 
19063   timevar_push (TV_TEMPLATE_INST);
19064 
19065   /* Look for an (optional) storage-class-specifier or
19066      function-specifier.  */
19067   if (cp_parser_allow_gnu_extensions_p (parser))
19068     {
19069       extension_specifier
19070 	= cp_parser_storage_class_specifier_opt (parser);
19071       if (!extension_specifier)
19072 	extension_specifier
19073 	  = cp_parser_function_specifier_opt (parser,
19074 					      /*decl_specs=*/NULL);
19075     }
19076 
19077   /* Look for the `template' keyword.  */
19078   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19079   /* Let the front end know that we are processing an explicit
19080      instantiation.  */
19081   begin_explicit_instantiation ();
19082   /* [temp.explicit] says that we are supposed to ignore access
19083      control while processing explicit instantiation directives.  */
19084   push_deferring_access_checks (dk_no_check);
19085   /* Parse a decl-specifier-seq.  */
19086   cp_parser_decl_specifier_seq (parser,
19087 				CP_PARSER_FLAGS_OPTIONAL,
19088 				&decl_specifiers,
19089 				&declares_class_or_enum);
19090 
19091   cp_omp_declare_simd_data odsd;
19092   if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19093     cp_parser_handle_directive_omp_attributes (parser,
19094 					       &decl_specifiers.attributes,
19095 					       &odsd, true);
19096 
19097   /* If there was exactly one decl-specifier, and it declared a class,
19098      and there's no declarator, then we have an explicit type
19099      instantiation.  */
19100   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19101     {
19102       tree type = check_tag_decl (&decl_specifiers,
19103 				  /*explicit_type_instantiation_p=*/true);
19104       /* Turn access control back on for names used during
19105 	 template instantiation.  */
19106       pop_deferring_access_checks ();
19107       if (type)
19108 	do_type_instantiation (type, extension_specifier,
19109 			       /*complain=*/tf_error);
19110     }
19111   else
19112     {
19113       cp_declarator *declarator;
19114       tree decl;
19115 
19116       /* Parse the declarator.  */
19117       declarator
19118 	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19119 				CP_PARSER_FLAGS_NONE,
19120 				/*ctor_dtor_or_conv_p=*/NULL,
19121 				/*parenthesized_p=*/NULL,
19122 				/*member_p=*/false,
19123 				/*friend_p=*/false,
19124 				/*static_p=*/false);
19125       if (declares_class_or_enum & 2)
19126 	cp_parser_check_for_definition_in_return_type (declarator,
19127 						       decl_specifiers.type,
19128 						       decl_specifiers.locations[ds_type_spec]);
19129       if (declarator != cp_error_declarator)
19130 	{
19131 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19132 	    permerror (decl_specifiers.locations[ds_inline],
19133 		       "explicit instantiation shall not use"
19134 		       " %<inline%> specifier");
19135 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19136 	    permerror (decl_specifiers.locations[ds_constexpr],
19137 		       "explicit instantiation shall not use"
19138 		       " %<constexpr%> specifier");
19139 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19140 	    permerror (decl_specifiers.locations[ds_consteval],
19141 		       "explicit instantiation shall not use"
19142 		       " %<consteval%> specifier");
19143 
19144 	  decl = grokdeclarator (declarator, &decl_specifiers,
19145 				 NORMAL, 0, &decl_specifiers.attributes);
19146 	  /* Turn access control back on for names used during
19147 	     template instantiation.  */
19148 	  pop_deferring_access_checks ();
19149 	  /* Do the explicit instantiation.  */
19150 	  do_decl_instantiation (decl, extension_specifier);
19151 	}
19152       else
19153 	{
19154 	  pop_deferring_access_checks ();
19155 	  /* Skip the body of the explicit instantiation.  */
19156 	  cp_parser_skip_to_end_of_statement (parser);
19157 	}
19158     }
19159   /* We're done with the instantiation.  */
19160   end_explicit_instantiation ();
19161 
19162   cp_parser_consume_semicolon_at_end_of_statement (parser);
19163 
19164   timevar_pop (TV_TEMPLATE_INST);
19165 
19166   cp_finalize_omp_declare_simd (parser, &odsd);
19167 }
19168 
19169 /* Parse an explicit-specialization.
19170 
19171    explicit-specialization:
19172      template < > declaration
19173 
19174    Although the standard says `declaration', what it really means is:
19175 
19176    explicit-specialization:
19177      template <> decl-specifier [opt] init-declarator [opt] ;
19178      template <> function-definition
19179      template <> explicit-specialization
19180      template <> template-declaration  */
19181 
19182 static void
cp_parser_explicit_specialization(cp_parser * parser)19183 cp_parser_explicit_specialization (cp_parser* parser)
19184 {
19185   cp_token *token = cp_lexer_peek_token (parser->lexer);
19186 
19187   /* Look for the `template' keyword.  */
19188   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19189   /* Look for the `<'.  */
19190   cp_parser_require (parser, CPP_LESS, RT_LESS);
19191   /* Look for the `>'.  */
19192   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19193   /* We have processed another parameter list.  */
19194   ++parser->num_template_parameter_lists;
19195 
19196   /* [temp]
19197 
19198      A template ... explicit specialization ... shall not have C
19199      linkage.  */
19200   bool need_lang_pop = current_lang_name == lang_name_c;
19201   if (need_lang_pop)
19202     {
19203       error_at (token->location, "template specialization with C linkage");
19204       maybe_show_extern_c_location ();
19205 
19206       /* Give it C++ linkage to avoid confusing other parts of the
19207 	 front end.  */
19208       push_lang_context (lang_name_cplusplus);
19209     }
19210 
19211   /* Let the front end know that we are beginning a specialization.  */
19212   if (begin_specialization ())
19213     {
19214       /* If the next keyword is `template', we need to figure out
19215 	 whether or not we're looking a template-declaration.  */
19216       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19217 	{
19218 	  if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19219 	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19220 	    cp_parser_template_declaration_after_export (parser,
19221 							 /*member_p=*/false);
19222 	  else
19223 	    cp_parser_explicit_specialization (parser);
19224 	}
19225       else
19226 	/* Parse the dependent declaration.  */
19227 	cp_parser_single_declaration (parser,
19228 				      /*checks=*/NULL,
19229 				      /*member_p=*/false,
19230 				      /*explicit_specialization_p=*/true,
19231 				      /*friend_p=*/NULL);
19232     }
19233 
19234   /* We're done with the specialization.  */
19235   end_specialization ();
19236 
19237   /* For the erroneous case of a template with C linkage, we pushed an
19238      implicit C++ linkage scope; exit that scope now.  */
19239   if (need_lang_pop)
19240     pop_lang_context ();
19241 
19242   /* We're done with this parameter list.  */
19243   --parser->num_template_parameter_lists;
19244 }
19245 
19246 /* Preserve the attributes across a garbage collect (by making it a GC
19247    root), which can occur when parsing a member function.  */
19248 
19249 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19250 
19251 /* Parse a type-specifier.
19252 
19253    type-specifier:
19254      simple-type-specifier
19255      class-specifier
19256      enum-specifier
19257      elaborated-type-specifier
19258      cv-qualifier
19259 
19260    GNU Extension:
19261 
19262    type-specifier:
19263      __complex__
19264 
19265    Returns a representation of the type-specifier.  For a
19266    class-specifier, enum-specifier, or elaborated-type-specifier, a
19267    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19268 
19269    The parser flags FLAGS is used to control type-specifier parsing.
19270 
19271    If IS_DECLARATION is TRUE, then this type-specifier is appearing
19272    in a decl-specifier-seq.
19273 
19274    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19275    class-specifier, enum-specifier, or elaborated-type-specifier, then
19276    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
19277    if a type is declared; 2 if it is defined.  Otherwise, it is set to
19278    zero.
19279 
19280    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19281    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
19282    is set to FALSE.  */
19283 
19284 static tree
cp_parser_type_specifier(cp_parser * parser,cp_parser_flags flags,cp_decl_specifier_seq * decl_specs,bool is_declaration,int * declares_class_or_enum,bool * is_cv_qualifier)19285 cp_parser_type_specifier (cp_parser* parser,
19286 			  cp_parser_flags flags,
19287 			  cp_decl_specifier_seq *decl_specs,
19288 			  bool is_declaration,
19289 			  int* declares_class_or_enum,
19290 			  bool* is_cv_qualifier)
19291 {
19292   tree type_spec = NULL_TREE;
19293   cp_token *token;
19294   enum rid keyword;
19295   cp_decl_spec ds = ds_last;
19296 
19297   /* Assume this type-specifier does not declare a new type.  */
19298   if (declares_class_or_enum)
19299     *declares_class_or_enum = 0;
19300   /* And that it does not specify a cv-qualifier.  */
19301   if (is_cv_qualifier)
19302     *is_cv_qualifier = false;
19303   /* Peek at the next token.  */
19304   token = cp_lexer_peek_token (parser->lexer);
19305 
19306   /* If we're looking at a keyword, we can use that to guide the
19307      production we choose.  */
19308   keyword = token->keyword;
19309   switch (keyword)
19310     {
19311     case RID_ENUM:
19312       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19313 	goto elaborated_type_specifier;
19314 
19315       /* Look for the enum-specifier.  */
19316       type_spec = cp_parser_enum_specifier (parser);
19317       /* If that worked, we're done.  */
19318       if (type_spec)
19319 	{
19320 	  if (declares_class_or_enum)
19321 	    *declares_class_or_enum = 2;
19322 	  if (decl_specs)
19323 	    cp_parser_set_decl_spec_type (decl_specs,
19324 					  type_spec,
19325 					  token,
19326 					  /*type_definition_p=*/true);
19327 	  return type_spec;
19328 	}
19329       else
19330 	goto elaborated_type_specifier;
19331 
19332       /* Any of these indicate either a class-specifier, or an
19333 	 elaborated-type-specifier.  */
19334     case RID_CLASS:
19335     case RID_STRUCT:
19336     case RID_UNION:
19337       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19338 	goto elaborated_type_specifier;
19339 
19340       /* Parse tentatively so that we can back up if we don't find a
19341 	 class-specifier.  */
19342       cp_parser_parse_tentatively (parser);
19343       if (decl_specs->attributes)
19344 	vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19345       /* Look for the class-specifier.  */
19346       type_spec = cp_parser_class_specifier (parser);
19347       if (decl_specs->attributes)
19348 	cp_parser_decl_specs_attrs->pop ();
19349       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19350       /* If that worked, we're done.  */
19351       if (cp_parser_parse_definitely (parser))
19352 	{
19353 	  if (declares_class_or_enum)
19354 	    *declares_class_or_enum = 2;
19355 	  if (decl_specs)
19356 	    cp_parser_set_decl_spec_type (decl_specs,
19357 					  type_spec,
19358 					  token,
19359 					  /*type_definition_p=*/true);
19360 	  return type_spec;
19361 	}
19362 
19363       /* Fall through.  */
19364     elaborated_type_specifier:
19365       /* We're declaring (not defining) a class or enum.  */
19366       if (declares_class_or_enum)
19367 	*declares_class_or_enum = 1;
19368 
19369       /* Fall through.  */
19370     case RID_TYPENAME:
19371       /* Look for an elaborated-type-specifier.  */
19372       type_spec
19373 	= (cp_parser_elaborated_type_specifier
19374 	   (parser,
19375 	    decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19376 	    is_declaration));
19377       if (decl_specs)
19378 	cp_parser_set_decl_spec_type (decl_specs,
19379 				      type_spec,
19380 				      token,
19381 				      /*type_definition_p=*/false);
19382       return type_spec;
19383 
19384     case RID_CONST:
19385       ds = ds_const;
19386       if (is_cv_qualifier)
19387 	*is_cv_qualifier = true;
19388       break;
19389 
19390     case RID_VOLATILE:
19391       ds = ds_volatile;
19392       if (is_cv_qualifier)
19393 	*is_cv_qualifier = true;
19394       break;
19395 
19396     case RID_RESTRICT:
19397       ds = ds_restrict;
19398       if (is_cv_qualifier)
19399 	*is_cv_qualifier = true;
19400       break;
19401 
19402     case RID_COMPLEX:
19403       /* The `__complex__' keyword is a GNU extension.  */
19404       ds = ds_complex;
19405       break;
19406 
19407     default:
19408       break;
19409     }
19410 
19411   /* Handle simple keywords.  */
19412   if (ds != ds_last)
19413     {
19414       if (decl_specs)
19415 	{
19416 	  set_and_check_decl_spec_loc (decl_specs, ds, token);
19417 	  decl_specs->any_specifiers_p = true;
19418 	}
19419       return cp_lexer_consume_token (parser->lexer)->u.value;
19420     }
19421 
19422   /* If we do not already have a type-specifier, assume we are looking
19423      at a simple-type-specifier.  */
19424   type_spec = cp_parser_simple_type_specifier (parser,
19425 					       decl_specs,
19426 					       flags);
19427 
19428   /* If we didn't find a type-specifier, and a type-specifier was not
19429      optional in this context, issue an error message.  */
19430   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19431     {
19432       cp_parser_error (parser, "expected type specifier");
19433       return error_mark_node;
19434     }
19435 
19436   return type_spec;
19437 }
19438 
19439 /* Parse a simple-type-specifier.
19440 
19441    simple-type-specifier:
19442      :: [opt] nested-name-specifier [opt] type-name
19443      :: [opt] nested-name-specifier template template-id
19444      char
19445      wchar_t
19446      bool
19447      short
19448      int
19449      long
19450      signed
19451      unsigned
19452      float
19453      double
19454      void
19455 
19456    C++11 Extension:
19457 
19458    simple-type-specifier:
19459      auto
19460      decltype ( expression )
19461      char16_t
19462      char32_t
19463      __underlying_type ( type-id )
19464 
19465    C++17 extension:
19466 
19467      nested-name-specifier(opt) template-name
19468 
19469    GNU Extension:
19470 
19471    simple-type-specifier:
19472      __int128
19473      __typeof__ unary-expression
19474      __typeof__ ( type-id )
19475      __typeof__ ( type-id ) { initializer-list , [opt] }
19476 
19477    Concepts Extension:
19478 
19479    simple-type-specifier:
19480      constrained-type-specifier
19481 
19482    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
19483    appropriately updated.  */
19484 
19485 static tree
cp_parser_simple_type_specifier(cp_parser * parser,cp_decl_specifier_seq * decl_specs,cp_parser_flags flags)19486 cp_parser_simple_type_specifier (cp_parser* parser,
19487 				 cp_decl_specifier_seq *decl_specs,
19488 				 cp_parser_flags flags)
19489 {
19490   tree type = NULL_TREE;
19491   cp_token *token;
19492   int idx;
19493 
19494   /* Peek at the next token.  */
19495   token = cp_lexer_peek_token (parser->lexer);
19496 
19497   /* If we're looking at a keyword, things are easy.  */
19498   switch (token->keyword)
19499     {
19500     case RID_CHAR:
19501       if (decl_specs)
19502 	decl_specs->explicit_char_p = true;
19503       type = char_type_node;
19504       break;
19505     case RID_CHAR8:
19506       type = char8_type_node;
19507       break;
19508     case RID_CHAR16:
19509       type = char16_type_node;
19510       break;
19511     case RID_CHAR32:
19512       type = char32_type_node;
19513       break;
19514     case RID_WCHAR:
19515       type = wchar_type_node;
19516       break;
19517     case RID_BOOL:
19518       type = boolean_type_node;
19519       break;
19520     case RID_SHORT:
19521       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19522       type = short_integer_type_node;
19523       break;
19524     case RID_INT:
19525       if (decl_specs)
19526 	decl_specs->explicit_int_p = true;
19527       type = integer_type_node;
19528       break;
19529     case RID_INT_N_0:
19530     case RID_INT_N_1:
19531     case RID_INT_N_2:
19532     case RID_INT_N_3:
19533       idx = token->keyword - RID_INT_N_0;
19534       if (! int_n_enabled_p [idx])
19535 	break;
19536       if (decl_specs)
19537 	{
19538 	  decl_specs->explicit_intN_p = true;
19539 	  decl_specs->int_n_idx = idx;
19540 	  /* Check if the alternate "__intN__" form has been used instead of
19541 	     "__intN".  */
19542 	  if (startswith (IDENTIFIER_POINTER (token->u.value)
19543 			  + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19544 	    decl_specs->int_n_alt = true;
19545 	}
19546       type = int_n_trees [idx].signed_type;
19547       break;
19548     case RID_LONG:
19549       if (decl_specs)
19550 	set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19551       type = long_integer_type_node;
19552       break;
19553     case RID_SIGNED:
19554       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19555       type = integer_type_node;
19556       break;
19557     case RID_UNSIGNED:
19558       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19559       type = unsigned_type_node;
19560       break;
19561     case RID_FLOAT:
19562       type = float_type_node;
19563       break;
19564     case RID_DOUBLE:
19565       type = double_type_node;
19566       break;
19567     case RID_VOID:
19568       type = void_type_node;
19569       break;
19570 
19571     case RID_AUTO:
19572       maybe_warn_cpp0x (CPP0X_AUTO);
19573       if (parser->auto_is_implicit_function_template_parm_p)
19574 	{
19575 	  /* The 'auto' might be the placeholder return type for a function decl
19576 	     with trailing return type.  */
19577 	  bool have_trailing_return_fn_decl = false;
19578 
19579 	  cp_parser_parse_tentatively (parser);
19580 	  cp_lexer_consume_token (parser->lexer);
19581 	  while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19582 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19583 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19584 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19585 	    {
19586 	      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19587 		{
19588 		  cp_lexer_consume_token (parser->lexer);
19589 		  cp_parser_skip_to_closing_parenthesis (parser,
19590 							 /*recovering*/false,
19591 							 /*or_comma*/false,
19592 							 /*consume_paren*/true);
19593 		  continue;
19594 		}
19595 
19596 	      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19597 		{
19598 		  have_trailing_return_fn_decl = true;
19599 		  break;
19600 		}
19601 
19602 	      cp_lexer_consume_token (parser->lexer);
19603 	    }
19604 	  cp_parser_abort_tentative_parse (parser);
19605 
19606 	  if (have_trailing_return_fn_decl)
19607 	    {
19608 	      type = make_auto ();
19609 	      break;
19610 	    }
19611 
19612 	  if (cxx_dialect >= cxx14)
19613 	    {
19614 	      type = synthesize_implicit_template_parm (parser, NULL_TREE);
19615 	      type = TREE_TYPE (type);
19616 	    }
19617 	  else
19618 	    type = error_mark_node;
19619 
19620 	  if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19621 	    {
19622 	      if (cxx_dialect < cxx14)
19623 		error_at (token->location,
19624 			 "use of %<auto%> in lambda parameter declaration "
19625 			 "only available with "
19626 			 "%<-std=c++14%> or %<-std=gnu++14%>");
19627 	    }
19628 	  else if (cxx_dialect < cxx14)
19629 	    error_at (token->location,
19630 		     "use of %<auto%> in parameter declaration "
19631 		     "only available with "
19632 		     "%<-std=c++14%> or %<-std=gnu++14%>");
19633 	  else if (!flag_concepts)
19634 	    pedwarn (token->location, 0,
19635 		     "use of %<auto%> in parameter declaration "
19636 		     "only available with %<-std=c++20%> or %<-fconcepts%>");
19637 	}
19638       else
19639 	type = make_auto ();
19640       break;
19641 
19642     case RID_DECLTYPE:
19643       /* Since DR 743, decltype can either be a simple-type-specifier by
19644 	 itself or begin a nested-name-specifier.  Parsing it will replace
19645 	 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19646 	 handling below decide what to do.  */
19647       cp_parser_decltype (parser);
19648       cp_lexer_set_token_position (parser->lexer, token);
19649       break;
19650 
19651     case RID_TYPEOF:
19652       /* Consume the `typeof' token.  */
19653       cp_lexer_consume_token (parser->lexer);
19654       /* Parse the operand to `typeof'.  */
19655       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19656       /* If it is not already a TYPE, take its type.  */
19657       if (!TYPE_P (type))
19658 	type = finish_typeof (type);
19659 
19660       if (decl_specs)
19661 	cp_parser_set_decl_spec_type (decl_specs, type,
19662 				      token,
19663 				      /*type_definition_p=*/false);
19664 
19665       return type;
19666 
19667     case RID_UNDERLYING_TYPE:
19668       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
19669       if (decl_specs)
19670 	cp_parser_set_decl_spec_type (decl_specs, type,
19671 				      token,
19672 				      /*type_definition_p=*/false);
19673 
19674       return type;
19675 
19676     case RID_BASES:
19677     case RID_DIRECT_BASES:
19678       type = cp_parser_trait_expr (parser, token->keyword);
19679       if (decl_specs)
19680        cp_parser_set_decl_spec_type (decl_specs, type,
19681                                      token,
19682                                      /*type_definition_p=*/false);
19683       return type;
19684     default:
19685       break;
19686     }
19687 
19688   /* If token is an already-parsed decltype not followed by ::,
19689      it's a simple-type-specifier.  */
19690   if (token->type == CPP_DECLTYPE
19691       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19692     {
19693       type = saved_checks_value (token->u.tree_check_value);
19694       if (decl_specs)
19695 	{
19696 	  cp_parser_set_decl_spec_type (decl_specs, type,
19697 					token,
19698 					/*type_definition_p=*/false);
19699 	  /* Remember that we are handling a decltype in order to
19700 	     implement the resolution of DR 1510 when the argument
19701 	     isn't instantiation dependent.  */
19702 	  decl_specs->decltype_p = true;
19703 	}
19704       cp_lexer_consume_token (parser->lexer);
19705       return type;
19706     }
19707 
19708   /* If the type-specifier was for a built-in type, we're done.  */
19709   if (type)
19710     {
19711       /* Record the type.  */
19712       if (decl_specs
19713 	  && (token->keyword != RID_SIGNED
19714 	      && token->keyword != RID_UNSIGNED
19715 	      && token->keyword != RID_SHORT
19716 	      && token->keyword != RID_LONG))
19717 	cp_parser_set_decl_spec_type (decl_specs,
19718 				      type,
19719 				      token,
19720 				      /*type_definition_p=*/false);
19721       if (decl_specs)
19722 	decl_specs->any_specifiers_p = true;
19723 
19724       /* Consume the token.  */
19725       cp_lexer_consume_token (parser->lexer);
19726 
19727       if (type == error_mark_node)
19728 	return error_mark_node;
19729 
19730       /* There is no valid C++ program where a non-template type is
19731 	 followed by a "<".  That usually indicates that the user thought
19732 	 that the type was a template.  */
19733       cp_parser_check_for_invalid_template_id (parser, type, none_type,
19734 					       token->location);
19735 
19736       return TYPE_NAME (type);
19737     }
19738 
19739   /* The type-specifier must be a user-defined type.  */
19740   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19741     {
19742       bool qualified_p;
19743       bool global_p;
19744       const bool typename_p = (cxx_dialect >= cxx20
19745 			       && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19746 
19747       /* Don't gobble tokens or issue error messages if this is an
19748 	 optional type-specifier.  */
19749       if (flags & CP_PARSER_FLAGS_OPTIONAL)
19750 	cp_parser_parse_tentatively (parser);
19751 
19752       /* Remember current tentative parsing state -- if we know we need
19753 	 a type, we can give better diagnostics here.  */
19754       bool tent = cp_parser_parsing_tentatively (parser);
19755 
19756       token = cp_lexer_peek_token (parser->lexer);
19757 
19758       /* Look for the optional `::' operator.  */
19759       global_p
19760 	= (cp_parser_global_scope_opt (parser,
19761 				       /*current_scope_valid_p=*/false)
19762 	   != NULL_TREE);
19763       /* Look for the nested-name specifier.  */
19764       qualified_p
19765 	= (cp_parser_nested_name_specifier_opt (parser,
19766 						/*typename_keyword_p=*/false,
19767 						/*check_dependency_p=*/true,
19768 						/*type_p=*/false,
19769 						/*is_declaration=*/false)
19770 	   != NULL_TREE);
19771       /* If we have seen a nested-name-specifier, and the next token
19772 	 is `template', then we are using the template-id production.  */
19773       if (parser->scope
19774 	  && cp_parser_optional_template_keyword (parser))
19775 	{
19776 	  /* Look for the template-id.  */
19777 	  type = cp_parser_template_id (parser,
19778 					/*template_keyword_p=*/true,
19779 					/*check_dependency_p=*/true,
19780 					none_type,
19781 					/*is_declaration=*/false);
19782 	  /* If the template-id did not name a type, we are out of
19783 	     luck.  */
19784 	  if (TREE_CODE (type) != TYPE_DECL)
19785 	    {
19786 	      /* ...unless we pretend we have seen 'typename'.  */
19787 	      if (typename_p)
19788 		type = cp_parser_make_typename_type (parser, type,
19789 						     token->location);
19790 	      else
19791 		{
19792 		  cp_parser_error (parser, "expected template-id for type");
19793 		  type = error_mark_node;
19794 		}
19795 	    }
19796 	}
19797       /* DR 1812: A < following a qualified-id in a typename-specifier
19798 	 could safely be assumed to begin a template argument list, so
19799 	 the template keyword should be optional.  */
19800       else if (parser->scope
19801 	       && qualified_p
19802 	       && typename_p
19803 	       && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19804 	{
19805 	  cp_parser_parse_tentatively (parser);
19806 
19807 	  type = cp_parser_template_id (parser,
19808 					/*template_keyword_p=*/true,
19809 					/*check_dependency_p=*/true,
19810 					none_type,
19811 					/*is_declaration=*/false);
19812 	  /* This is handled below, so back off.  */
19813 	  if (type && concept_check_p (type))
19814 	    cp_parser_simulate_error (parser);
19815 
19816 	  if (!cp_parser_parse_definitely (parser))
19817 	    type = NULL_TREE;
19818 	  else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
19819 	    type = make_typename_type (parser->scope, type, typename_type,
19820 				       /*complain=*/tf_error);
19821 	  else if (TREE_CODE (type) != TYPE_DECL)
19822 	    type = NULL_TREE;
19823 	}
19824 
19825       /* Otherwise, look for a type-name.  */
19826       if (!type)
19827 	{
19828 	  if (cxx_dialect >= cxx17)
19829 	    cp_parser_parse_tentatively (parser);
19830 
19831 	  type = cp_parser_type_name (parser, (qualified_p && typename_p));
19832 
19833 	  if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
19834 	    type = NULL_TREE;
19835 	}
19836 
19837       if (!type && flag_concepts && decl_specs)
19838 	{
19839 	  /* Try for a type-constraint with template arguments.  We check
19840 	     decl_specs here to avoid trying this for a functional cast.  */
19841 
19842 	  cp_parser_parse_tentatively (parser);
19843 
19844 	  type = cp_parser_template_id (parser,
19845 					/*template_keyword_p=*/false,
19846 					/*check_dependency_p=*/true,
19847 					none_type,
19848 					/*is_declaration=*/false);
19849 	  if (type && concept_check_p (type))
19850 	    {
19851 	      location_t loc = EXPR_LOCATION (type);
19852 	      type = cp_parser_placeholder_type_specifier (parser, loc,
19853 							   type, tent);
19854 	      if (tent && type == error_mark_node)
19855 		/* Perhaps it's a concept-check expression.  */
19856 		cp_parser_simulate_error (parser);
19857 	    }
19858 	  else
19859 	    cp_parser_simulate_error (parser);
19860 
19861 	  if (!cp_parser_parse_definitely (parser))
19862 	    type = NULL_TREE;
19863 	}
19864 
19865       if (!type && cxx_dialect >= cxx17)
19866 	{
19867 	  /* Try class template argument deduction or type-constraint without
19868 	     template arguments.  */
19869 	  tree name = cp_parser_identifier (parser);
19870 	  if (name && TREE_CODE (name) == IDENTIFIER_NODE
19871 	      && parser->scope != error_mark_node)
19872 	    {
19873 	      location_t loc
19874 		= cp_lexer_previous_token (parser->lexer)->location;
19875 	      tree tmpl = cp_parser_lookup_name (parser, name,
19876 						 none_type,
19877 						 /*is_template=*/false,
19878 						 /*is_namespace=*/false,
19879 						 /*check_dependency=*/true,
19880 						 /*ambiguous_decls=*/NULL,
19881 						 token->location);
19882 	      if (tmpl && tmpl != error_mark_node
19883 		  && ctad_template_p (tmpl))
19884 		type = make_template_placeholder (tmpl);
19885 	      else if (flag_concepts && tmpl && concept_definition_p (tmpl))
19886 		type = cp_parser_placeholder_type_specifier (parser, loc,
19887 							     tmpl, tent);
19888 	      else
19889 		{
19890 		  type = error_mark_node;
19891 		  if (!cp_parser_simulate_error (parser))
19892 		    cp_parser_name_lookup_error (parser, name, tmpl,
19893 						 NLE_TYPE, token->location);
19894 		}
19895 	    }
19896 	  else
19897 	    type = error_mark_node;
19898 	}
19899 
19900       /* If it didn't work out, we don't have a TYPE.  */
19901       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
19902 	  && !cp_parser_parse_definitely (parser))
19903 	type = NULL_TREE;
19904 
19905       /* Keep track of all name-lookups performed in class scopes.  */
19906       if (type
19907 	  && !global_p
19908 	  && !qualified_p
19909 	  && TREE_CODE (type) == TYPE_DECL
19910 	  && identifier_p (DECL_NAME (type)))
19911 	maybe_note_name_used_in_class (DECL_NAME (type), type);
19912 
19913       if (type && decl_specs)
19914 	cp_parser_set_decl_spec_type (decl_specs, type,
19915 				      token,
19916 				      /*type_definition_p=*/false);
19917     }
19918 
19919   /* If we didn't get a type-name, issue an error message.  */
19920   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19921     {
19922       cp_parser_error (parser, "expected type-name");
19923       return error_mark_node;
19924     }
19925 
19926   if (type && type != error_mark_node)
19927     {
19928       /* See if TYPE is an Objective-C type, and if so, parse and
19929 	 accept any protocol references following it.  Do this before
19930 	 the cp_parser_check_for_invalid_template_id() call, because
19931 	 Objective-C types can be followed by '<...>' which would
19932 	 enclose protocol names rather than template arguments, and so
19933 	 everything is fine.  */
19934       if (c_dialect_objc () && !parser->scope
19935 	  && (objc_is_id (type) || objc_is_class_name (type)))
19936 	{
19937 	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
19938 	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
19939 
19940 	  /* Clobber the "unqualified" type previously entered into
19941 	     DECL_SPECS with the new, improved protocol-qualified version.  */
19942 	  if (decl_specs)
19943 	    decl_specs->type = qual_type;
19944 
19945 	  return qual_type;
19946 	}
19947 
19948       /* There is no valid C++ program where a non-template type is
19949 	 followed by a "<".  That usually indicates that the user
19950 	 thought that the type was a template.  */
19951       cp_parser_check_for_invalid_template_id (parser, type,
19952 					       none_type,
19953 					       token->location);
19954     }
19955 
19956   return type;
19957 }
19958 
19959 /* Parse the remainder of a placholder-type-specifier.
19960 
19961    placeholder-type-specifier:
19962      type-constraint_opt auto
19963      type-constraint_opt decltype(auto)
19964 
19965   The raw form of the constraint is parsed in cp_parser_simple_type_specifier
19966   and passed as TMPL. This function converts TMPL to an actual type-constraint,
19967   parses the placeholder type, and performs some contextual syntactic analysis.
19968 
19969   LOC provides the location of the template name.
19970 
19971   TENTATIVE is true if the type-specifier parsing is tentative; in that case,
19972   don't give an error if TMPL isn't a valid type-constraint, as the template-id
19973   might actually be a concept-check,
19974 
19975   Note that the Concepts TS allows the auto or decltype(auto) to be
19976   omitted in a constrained-type-specifier.  */
19977 
19978 static tree
cp_parser_placeholder_type_specifier(cp_parser * parser,location_t loc,tree tmpl,bool tentative)19979 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
19980 				      tree tmpl, bool tentative)
19981 {
19982   if (tmpl == error_mark_node)
19983     return error_mark_node;
19984 
19985   tree orig_tmpl = tmpl;
19986 
19987   /* Get the arguments as written for subsequent analysis.  */
19988   tree args = NULL_TREE;
19989   if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
19990     {
19991       args = TREE_OPERAND (tmpl, 1);
19992       tmpl = TREE_OPERAND (tmpl, 0);
19993     }
19994   else
19995     /* A concept-name with no arguments can't be an expression.  */
19996     tentative = false;
19997 
19998   tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
19999 
20000   /* Get the concept and prototype parameter for the constraint.  */
20001   tree_pair info = finish_type_constraints (tmpl, args, complain);
20002   tree con = info.first;
20003   tree proto = info.second;
20004   if (con == error_mark_node)
20005     return error_mark_node;
20006 
20007   /* As per the standard, require auto or decltype(auto), except in some
20008      cases (template parameter lists, -fconcepts-ts enabled).  */
20009   cp_token *placeholder = NULL, *close_paren = NULL;
20010   if (cxx_dialect >= cxx20)
20011     {
20012       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20013 	placeholder = cp_lexer_consume_token (parser->lexer);
20014       else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20015 	{
20016 	  placeholder = cp_lexer_consume_token (parser->lexer);
20017 	  matching_parens parens;
20018 	  parens.require_open (parser);
20019 	  cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20020 	  close_paren = parens.require_close (parser);
20021 	}
20022     }
20023 
20024   /* A type constraint constrains a contextually determined type or type
20025      parameter pack. However, the Concepts TS does allow concepts
20026      to introduce non-type and template template parameters.  */
20027   if (TREE_CODE (proto) != TYPE_DECL)
20028     {
20029       if (!flag_concepts_ts
20030 	  || !processing_template_parmlist)
20031 	{
20032 	  if (!tentative)
20033 	    {
20034 	      error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20035 	      inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20036 	    }
20037 	  return error_mark_node;
20038 	}
20039     }
20040 
20041   /* In a template parameter list, a type-parameter can be introduced
20042      by type-constraints alone.  */
20043   if (processing_template_parmlist && !placeholder)
20044     {
20045       /* In a default argument we may not be creating new parameters.  */
20046       if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20047 	{
20048 	  /* If this assert turns out to be false, do error() instead.  */
20049 	  gcc_assert (tentative);
20050 	  return error_mark_node;
20051 	}
20052       return build_constrained_parameter (con, proto, args);
20053     }
20054 
20055   /* Diagnose issues placeholder issues.  */
20056   if (!flag_concepts_ts
20057       && !parser->in_result_type_constraint_p
20058       && !placeholder)
20059     {
20060       if (tentative)
20061 	/* Perhaps it's a concept-check expression (c++/91073).  */
20062 	return error_mark_node;
20063 
20064       tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20065       tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20066       error_at (input_location,
20067 		"expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20068       /* Fall through. This is an error of omission.  */
20069     }
20070   else if (parser->in_result_type_constraint_p && placeholder)
20071     {
20072       /* A trailing return type only allows type-constraints.  */
20073       error_at (input_location,
20074 		"unexpected placeholder in constrained result type");
20075     }
20076 
20077   /* In a parameter-declaration-clause, a placeholder-type-specifier
20078      results in an invented template parameter.  */
20079   if (parser->auto_is_implicit_function_template_parm_p)
20080     {
20081       if (close_paren)
20082 	{
20083 	  location_t loc = make_location (placeholder->location,
20084 					  placeholder->location,
20085 					  close_paren->location);
20086 	  error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20087 	  return error_mark_node;
20088 	}
20089       tree parm = build_constrained_parameter (con, proto, args);
20090       return synthesize_implicit_template_parm (parser, parm);
20091     }
20092 
20093   /* Determine if the type should be deduced using template argument
20094      deduction or decltype deduction. Note that the latter is always
20095      used for type-constraints in trailing return types.  */
20096   bool decltype_p = placeholder
20097     ? placeholder->keyword == RID_DECLTYPE
20098     : parser->in_result_type_constraint_p;
20099 
20100   /* Otherwise, this is the type of a variable or return type.  */
20101   if (decltype_p)
20102     return make_constrained_decltype_auto (con, args);
20103   else
20104     return make_constrained_auto (con, args);
20105 }
20106 
20107 /* Parse a type-name.
20108 
20109    type-name:
20110      class-name
20111      enum-name
20112      typedef-name
20113      simple-template-id [in c++0x]
20114 
20115    enum-name:
20116      identifier
20117 
20118    typedef-name:
20119      identifier
20120 
20121   Concepts:
20122 
20123    type-name:
20124      concept-name
20125      partial-concept-id
20126 
20127    concept-name:
20128      identifier
20129 
20130    Returns a TYPE_DECL for the type.  */
20131 
20132 static tree
cp_parser_type_name(cp_parser * parser,bool typename_keyword_p)20133 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20134 {
20135   tree type_decl;
20136 
20137   /* We can't know yet whether it is a class-name or not.  */
20138   cp_parser_parse_tentatively (parser);
20139   /* Try a class-name.  */
20140   type_decl = cp_parser_class_name (parser,
20141 				    typename_keyword_p,
20142 				    /*template_keyword_p=*/false,
20143 				    none_type,
20144 				    /*check_dependency_p=*/true,
20145 				    /*class_head_p=*/false,
20146 				    /*is_declaration=*/false);
20147   /* If it's not a class-name, keep looking.  */
20148   if (!cp_parser_parse_definitely (parser))
20149     {
20150       if (cxx_dialect < cxx11)
20151 	/* It must be a typedef-name or an enum-name.  */
20152 	return cp_parser_nonclass_name (parser);
20153 
20154       cp_parser_parse_tentatively (parser);
20155       /* It is either a simple-template-id representing an
20156 	 instantiation of an alias template...  */
20157       type_decl = cp_parser_template_id (parser,
20158 					 /*template_keyword_p=*/false,
20159 					 /*check_dependency_p=*/true,
20160 					 none_type,
20161 					 /*is_declaration=*/false);
20162       /* Note that this must be an instantiation of an alias template
20163 	 because [temp.names]/6 says:
20164 
20165 	     A template-id that names an alias template specialization
20166 	     is a type-name.
20167 
20168 	 Whereas [temp.names]/7 says:
20169 
20170 	     A simple-template-id that names a class template
20171 	     specialization is a class-name.
20172 
20173          With concepts, this could also be a partial-concept-id that
20174          declares a non-type template parameter. */
20175       if (type_decl != NULL_TREE
20176 	  && TREE_CODE (type_decl) == TYPE_DECL
20177 	  && TYPE_DECL_ALIAS_P (type_decl))
20178 	gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20179       else
20180 	cp_parser_simulate_error (parser);
20181 
20182       if (!cp_parser_parse_definitely (parser))
20183 	/* ... Or a typedef-name or an enum-name.  */
20184 	return cp_parser_nonclass_name (parser);
20185     }
20186 
20187   return type_decl;
20188 }
20189 
20190 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20191    or a concept-name.
20192 
20193    enum-name:
20194      identifier
20195 
20196    typedef-name:
20197      identifier
20198 
20199    concept-name:
20200      identifier
20201 
20202    Returns a TYPE_DECL for the type.  */
20203 
20204 static tree
cp_parser_nonclass_name(cp_parser * parser)20205 cp_parser_nonclass_name (cp_parser* parser)
20206 {
20207   tree type_decl;
20208   tree identifier;
20209 
20210   cp_token *token = cp_lexer_peek_token (parser->lexer);
20211   identifier = cp_parser_identifier (parser);
20212   if (identifier == error_mark_node)
20213     return error_mark_node;
20214 
20215   /* Look up the type-name.  */
20216   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20217 
20218   type_decl = strip_using_decl (type_decl);
20219 
20220   if (TREE_CODE (type_decl) != TYPE_DECL
20221       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20222     {
20223       /* See if this is an Objective-C type.  */
20224       tree protos = cp_parser_objc_protocol_refs_opt (parser);
20225       tree type = objc_get_protocol_qualified_type (identifier, protos);
20226       if (type)
20227 	type_decl = TYPE_NAME (type);
20228     }
20229 
20230   /* Issue an error if we did not find a type-name.  */
20231   if (TREE_CODE (type_decl) != TYPE_DECL
20232       /* In Objective-C, we have the complication that class names are
20233 	 normally type names and start declarations (eg, the
20234 	 "NSObject" in "NSObject *object;"), but can be used in an
20235 	 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20236 	 is an expression.  So, a classname followed by a dot is not a
20237 	 valid type-name.  */
20238       || (objc_is_class_name (TREE_TYPE (type_decl))
20239 	  && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20240     {
20241       if (!cp_parser_simulate_error (parser))
20242 	cp_parser_name_lookup_error (parser, identifier, type_decl,
20243 				     NLE_TYPE, token->location);
20244       return error_mark_node;
20245     }
20246   /* Remember that the name was used in the definition of the
20247      current class so that we can check later to see if the
20248      meaning would have been different after the class was
20249      entirely defined.  */
20250   else if (type_decl != error_mark_node
20251 	   && !parser->scope)
20252     maybe_note_name_used_in_class (identifier, type_decl);
20253 
20254   return type_decl;
20255 }
20256 
20257 /* Parse an elaborated-type-specifier.  Note that the grammar given
20258    here incorporates the resolution to DR68.
20259 
20260    elaborated-type-specifier:
20261      class-key :: [opt] nested-name-specifier [opt] identifier
20262      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20263      enum-key :: [opt] nested-name-specifier [opt] identifier
20264      typename :: [opt] nested-name-specifier identifier
20265      typename :: [opt] nested-name-specifier template [opt]
20266        template-id
20267 
20268    GNU extension:
20269 
20270    elaborated-type-specifier:
20271      class-key attributes :: [opt] nested-name-specifier [opt] identifier
20272      class-key attributes :: [opt] nested-name-specifier [opt]
20273 	       template [opt] template-id
20274      enum attributes :: [opt] nested-name-specifier [opt] identifier
20275 
20276    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20277    declared `friend'.  If IS_DECLARATION is TRUE, then this
20278    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20279    something is being declared.
20280 
20281    Returns the TYPE specified.  */
20282 
20283 static tree
cp_parser_elaborated_type_specifier(cp_parser * parser,bool is_friend,bool is_declaration)20284 cp_parser_elaborated_type_specifier (cp_parser* parser,
20285 				     bool is_friend,
20286 				     bool is_declaration)
20287 {
20288   enum tag_types tag_type;
20289   tree identifier;
20290   tree type = NULL_TREE;
20291   tree attributes = NULL_TREE;
20292   tree globalscope;
20293   cp_token *token = NULL;
20294 
20295   /* For class and enum types the location of the class-key or enum-key.  */
20296   location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20297   /* For a scoped enum, the 'class' or 'struct' keyword id.  */
20298   rid scoped_key = RID_MAX;
20299 
20300   /* See if we're looking at the `enum' keyword.  */
20301   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20302     {
20303       /* Consume the `enum' token.  */
20304       cp_lexer_consume_token (parser->lexer);
20305       /* Remember that it's an enumeration type.  */
20306       tag_type = enum_type;
20307       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20308 	 enums) is used here.  */
20309       cp_token *token = cp_lexer_peek_token (parser->lexer);
20310       if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20311 	  || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20312 	{
20313 	  location_t loc = token->location;
20314 	  gcc_rich_location richloc (loc);
20315 	  richloc.add_range (input_location);
20316 	  richloc.add_fixit_remove ();
20317 	  pedwarn (&richloc, 0, "elaborated-type-specifier for "
20318 		   "a scoped enum must not use the %qD keyword",
20319 		   token->u.value);
20320 	  /* Consume the `struct' or `class' and parse it anyway.  */
20321 	  cp_lexer_consume_token (parser->lexer);
20322 	  /* Create a combined location for the whole scoped-enum-key.  */
20323 	  key_loc = make_location (key_loc, key_loc, loc);
20324 	}
20325       else
20326 	scoped_key = RID_MAX;
20327 
20328       /* Parse the attributes.  */
20329       attributes = cp_parser_attributes_opt (parser);
20330     }
20331   /* Or, it might be `typename'.  */
20332   else if (cp_lexer_next_token_is_keyword (parser->lexer,
20333 					   RID_TYPENAME))
20334     {
20335       /* Consume the `typename' token.  */
20336       cp_lexer_consume_token (parser->lexer);
20337       /* Remember that it's a `typename' type.  */
20338       tag_type = typename_type;
20339     }
20340   /* Otherwise it must be a class-key.  */
20341   else
20342     {
20343       key_loc = cp_lexer_peek_token (parser->lexer)->location;
20344       tag_type = cp_parser_class_key (parser);
20345       if (tag_type == none_type)
20346 	return error_mark_node;
20347       /* Parse the attributes.  */
20348       attributes = cp_parser_attributes_opt (parser);
20349     }
20350 
20351   /* Look for the `::' operator.  */
20352   globalscope =  cp_parser_global_scope_opt (parser,
20353 					     /*current_scope_valid_p=*/false);
20354   /* Look for the nested-name-specifier.  */
20355   tree nested_name_specifier;
20356   if (tag_type == typename_type && !globalscope)
20357     {
20358       nested_name_specifier
20359 	= cp_parser_nested_name_specifier (parser,
20360 					   /*typename_keyword_p=*/true,
20361 					   /*check_dependency_p=*/true,
20362 					   /*type_p=*/true,
20363 					   is_declaration);
20364       if (!nested_name_specifier)
20365 	return error_mark_node;
20366     }
20367   else
20368     /* Even though `typename' is not present, the proposed resolution
20369        to Core Issue 180 says that in `class A<T>::B', `B' should be
20370        considered a type-name, even if `A<T>' is dependent.  */
20371     nested_name_specifier
20372       = cp_parser_nested_name_specifier_opt (parser,
20373 					     /*typename_keyword_p=*/true,
20374 					     /*check_dependency_p=*/true,
20375 					     /*type_p=*/true,
20376 					     is_declaration);
20377  /* For everything but enumeration types, consider a template-id.
20378     For an enumeration type, consider only a plain identifier.  */
20379   if (tag_type != enum_type)
20380     {
20381       bool template_p = false;
20382       tree decl;
20383 
20384       /* Allow the `template' keyword.  */
20385       template_p = cp_parser_optional_template_keyword (parser);
20386       /* If we didn't see `template', we don't know if there's a
20387 	 template-id or not.  */
20388       if (!template_p)
20389 	cp_parser_parse_tentatively (parser);
20390       /* The `template' keyword must follow a nested-name-specifier.  */
20391       else if (!nested_name_specifier && !globalscope)
20392 	{
20393 	  cp_parser_error (parser, "%<template%> must follow a nested-"
20394 			   "name-specifier");
20395 	  return error_mark_node;
20396 	}
20397 
20398       /* Parse the template-id.  */
20399       token = cp_lexer_peek_token (parser->lexer);
20400       decl = cp_parser_template_id (parser, template_p,
20401 				    /*check_dependency_p=*/true,
20402 				    tag_type,
20403 				    is_declaration);
20404       /* If we didn't find a template-id, look for an ordinary
20405 	 identifier.  */
20406       if (!template_p && !cp_parser_parse_definitely (parser))
20407 	;
20408       /* We can get here when cp_parser_template_id, called by
20409 	 cp_parser_class_name with tag_type == none_type, succeeds
20410 	 and caches a BASELINK.  Then, when called again here,
20411 	 instead of failing and returning an error_mark_node
20412 	 returns it (see template/typename17.C in C++11).
20413 	 ??? Could we diagnose this earlier?  */
20414       else if (tag_type == typename_type && BASELINK_P (decl))
20415 	{
20416 	  cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20417 	  type = error_mark_node;
20418 	}
20419       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20420 	 in effect, then we must assume that, upon instantiation, the
20421 	 template will correspond to a class.  */
20422       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20423 	       && tag_type == typename_type)
20424 	type = make_typename_type (parser->scope, decl,
20425 				   typename_type,
20426 				   /*complain=*/tf_error);
20427       /* If the `typename' keyword is in effect and DECL is not a type
20428 	 decl, then type is non existent.   */
20429       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20430         ;
20431       else if (TREE_CODE (decl) == TYPE_DECL)
20432 	{
20433 	  type = check_elaborated_type_specifier (tag_type, decl,
20434 						  /*allow_template_p=*/true);
20435 
20436 	  /* If the next token is a semicolon, this must be a specialization,
20437 	     instantiation, or friend declaration.  Check the scope while we
20438 	     still know whether or not we had a nested-name-specifier.  */
20439 	  if (type != error_mark_node
20440 	      && !nested_name_specifier && !is_friend
20441 	      && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20442 	    check_unqualified_spec_or_inst (type, token->location);
20443 	}
20444       else if (decl == error_mark_node)
20445 	type = error_mark_node;
20446     }
20447 
20448   if (!type)
20449     {
20450       token = cp_lexer_peek_token (parser->lexer);
20451       identifier = cp_parser_identifier (parser);
20452 
20453       if (identifier == error_mark_node)
20454 	{
20455 	  parser->scope = NULL_TREE;
20456 	  return error_mark_node;
20457 	}
20458 
20459       /* For a `typename', we needn't call xref_tag.  */
20460       if (tag_type == typename_type
20461 	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20462 	return cp_parser_make_typename_type (parser, identifier,
20463 					     token->location);
20464 
20465       /* Template parameter lists apply only if we are not within a
20466 	 function parameter list.  */
20467       bool template_parm_lists_apply
20468 	  = parser->num_template_parameter_lists;
20469       if (template_parm_lists_apply)
20470 	for (cp_binding_level *s = current_binding_level;
20471 	     s && s->kind != sk_template_parms;
20472 	     s = s->level_chain)
20473 	  if (s->kind == sk_function_parms)
20474 	    template_parm_lists_apply = false;
20475 
20476       /* Look up a qualified name in the usual way.  */
20477       if (parser->scope)
20478 	{
20479 	  tree decl;
20480 	  tree ambiguous_decls;
20481 
20482 	  decl = cp_parser_lookup_name (parser, identifier,
20483 					tag_type,
20484 					/*is_template=*/false,
20485 					/*is_namespace=*/false,
20486 					/*check_dependency=*/true,
20487 					&ambiguous_decls,
20488 					token->location);
20489 
20490 	  /* If the lookup was ambiguous, an error will already have been
20491 	     issued.  */
20492 	  if (ambiguous_decls)
20493 	    return error_mark_node;
20494 
20495 	  /* If we are parsing friend declaration, DECL may be a
20496 	     TEMPLATE_DECL tree node here.  However, we need to check
20497 	     whether this TEMPLATE_DECL results in valid code.  Consider
20498 	     the following example:
20499 
20500 	       namespace N {
20501 		 template <class T> class C {};
20502 	       }
20503 	       class X {
20504 		 template <class T> friend class N::C; // #1, valid code
20505 	       };
20506 	       template <class T> class Y {
20507 		 friend class N::C;		       // #2, invalid code
20508 	       };
20509 
20510 	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20511 	     name lookup of `N::C'.  We see that friend declaration must
20512 	     be template for the code to be valid.  Note that
20513 	     processing_template_decl does not work here since it is
20514 	     always 1 for the above two cases.  */
20515 
20516 	  decl = (cp_parser_maybe_treat_template_as_class
20517 		  (decl, /*tag_name_p=*/is_friend
20518 			 && template_parm_lists_apply));
20519 
20520 	  if (TREE_CODE (decl) != TYPE_DECL)
20521 	    {
20522 	      cp_parser_diagnose_invalid_type_name (parser,
20523 						    identifier,
20524 						    token->location);
20525 	      return error_mark_node;
20526 	    }
20527 
20528 	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20529             {
20530               bool allow_template = (template_parm_lists_apply
20531 		                     || DECL_SELF_REFERENCE_P (decl));
20532               type = check_elaborated_type_specifier (tag_type, decl,
20533                                                       allow_template);
20534 
20535               if (type == error_mark_node)
20536                 return error_mark_node;
20537             }
20538 
20539           /* Forward declarations of nested types, such as
20540 
20541                class C1::C2;
20542                class C1::C2::C3;
20543 
20544              are invalid unless all components preceding the final '::'
20545              are complete.  If all enclosing types are complete, these
20546              declarations become merely pointless.
20547 
20548              Invalid forward declarations of nested types are errors
20549              caught elsewhere in parsing.  Those that are pointless arrive
20550              here.  */
20551 
20552           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20553 	      && !is_friend && is_declaration
20554 	      && !processing_explicit_instantiation)
20555             warning (0, "declaration %qD does not declare anything", decl);
20556 
20557 	  type = TREE_TYPE (decl);
20558 	}
20559       else
20560 	{
20561 	  /* An elaborated-type-specifier sometimes introduces a new type and
20562 	     sometimes names an existing type.  Normally, the rule is that it
20563 	     introduces a new type only if there is not an existing type of
20564 	     the same name already in scope.  For example, given:
20565 
20566 	       struct S {};
20567 	       void f() { struct S s; }
20568 
20569 	     the `struct S' in the body of `f' is the same `struct S' as in
20570 	     the global scope; the existing definition is used.  However, if
20571 	     there were no global declaration, this would introduce a new
20572 	     local class named `S'.
20573 
20574 	     An exception to this rule applies to the following code:
20575 
20576 	       namespace N { struct S; }
20577 
20578 	     Here, the elaborated-type-specifier names a new type
20579 	     unconditionally; even if there is already an `S' in the
20580 	     containing scope this declaration names a new type.
20581 	     This exception only applies if the elaborated-type-specifier
20582 	     forms the complete declaration:
20583 
20584 	       [class.name]
20585 
20586 	       A declaration consisting solely of `class-key identifier ;' is
20587 	       either a redeclaration of the name in the current scope or a
20588 	       forward declaration of the identifier as a class name.  It
20589 	       introduces the name into the current scope.
20590 
20591 	     We are in this situation precisely when the next token is a `;'.
20592 
20593 	     An exception to the exception is that a `friend' declaration does
20594 	     *not* name a new type; i.e., given:
20595 
20596 	       struct S { friend struct T; };
20597 
20598 	     `T' is not a new type in the scope of `S'.
20599 
20600 	     Also, `new struct S' or `sizeof (struct S)' never results in the
20601 	     definition of a new type; a new type can only be declared in a
20602 	     declaration context.  */
20603 
20604 	  TAG_how how;
20605 
20606 	  if (is_friend)
20607 	    /* Friends have special name lookup rules.  */
20608 	    how = TAG_how::HIDDEN_FRIEND;
20609 	  else if (is_declaration
20610 		   && cp_lexer_next_token_is (parser->lexer,
20611 					      CPP_SEMICOLON))
20612 	    /* This is a `class-key identifier ;' */
20613 	    how = TAG_how::CURRENT_ONLY;
20614 	  else
20615 	    how = TAG_how::GLOBAL;
20616 
20617 	  bool template_p =
20618 	    (template_parm_lists_apply
20619 	     && (cp_parser_next_token_starts_class_definition_p (parser)
20620 		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20621 	  /* An unqualified name was used to reference this type, so
20622 	     there were no qualifying templates.  */
20623 	  if (template_parm_lists_apply
20624 	      && !cp_parser_check_template_parameters (parser,
20625 						       /*num_templates=*/0,
20626 						       /*template_id*/false,
20627 						       token->location,
20628 						       /*declarator=*/NULL))
20629 	    return error_mark_node;
20630 
20631 	  type = xref_tag (tag_type, identifier, how, template_p);
20632 	}
20633     }
20634 
20635   if (type == error_mark_node)
20636     return error_mark_node;
20637 
20638   /* Allow attributes on forward declarations of classes.  */
20639   if (attributes)
20640     {
20641       if (TREE_CODE (type) == TYPENAME_TYPE)
20642 	warning (OPT_Wattributes,
20643 		 "attributes ignored on uninstantiated type");
20644       else if (tag_type != enum_type
20645 	       && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20646 	       && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20647 	       && ! processing_explicit_instantiation)
20648 	warning (OPT_Wattributes,
20649 		 "attributes ignored on template instantiation");
20650       else if (is_friend && cxx11_attribute_p (attributes))
20651 	{
20652 	  if (warning (OPT_Wattributes, "attribute ignored"))
20653 	    inform (input_location, "an attribute that appertains to a friend "
20654 		    "declaration that is not a definition is ignored");
20655 	}
20656       else if (is_declaration && cp_parser_declares_only_class_p (parser))
20657 	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20658       else
20659 	warning (OPT_Wattributes,
20660 		 "attributes ignored on elaborated-type-specifier that is "
20661 		 "not a forward declaration");
20662     }
20663 
20664   if (tag_type == enum_type)
20665     cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20666   else
20667     {
20668       /* Diagnose class/struct/union mismatches.  IS_DECLARATION is false
20669 	 for alias definition.  */
20670       bool decl_class = (is_declaration
20671 			 && cp_parser_declares_only_class_p (parser));
20672       cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20673 				 decl_class);
20674 
20675       /* Indicate whether this class was declared as a `class' or as a
20676 	 `struct'.  */
20677       if (CLASS_TYPE_P (type) && !currently_open_class (type))
20678 	CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20679     }
20680 
20681   /* A "<" cannot follow an elaborated type specifier.  If that
20682      happens, the user was probably trying to form a template-id.  */
20683   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20684 					   token->location);
20685 
20686   return type;
20687 }
20688 
20689 /* Parse an enum-specifier.
20690 
20691    enum-specifier:
20692      enum-head { enumerator-list [opt] }
20693      enum-head { enumerator-list , } [C++0x]
20694 
20695    enum-head:
20696      enum-key identifier [opt] enum-base [opt]
20697      enum-key nested-name-specifier identifier enum-base [opt]
20698 
20699    enum-key:
20700      enum
20701      enum class   [C++0x]
20702      enum struct  [C++0x]
20703 
20704    enum-base:   [C++0x]
20705      : type-specifier-seq
20706 
20707    opaque-enum-specifier:
20708      enum-key identifier enum-base [opt] ;
20709 
20710    GNU Extensions:
20711      enum-key attributes[opt] identifier [opt] enum-base [opt]
20712        { enumerator-list [opt] }attributes[opt]
20713      enum-key attributes[opt] identifier [opt] enum-base [opt]
20714        { enumerator-list, }attributes[opt] [C++0x]
20715 
20716    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20717    if the token stream isn't an enum-specifier after all.  */
20718 
20719 static tree
cp_parser_enum_specifier(cp_parser * parser)20720 cp_parser_enum_specifier (cp_parser* parser)
20721 {
20722   tree identifier;
20723   tree type = NULL_TREE;
20724   tree prev_scope;
20725   tree nested_name_specifier = NULL_TREE;
20726   tree attributes;
20727   bool scoped_enum_p = false;
20728   bool has_underlying_type = false;
20729   bool nested_being_defined = false;
20730   bool new_value_list = false;
20731   bool is_new_type = false;
20732   bool is_unnamed = false;
20733   tree underlying_type = NULL_TREE;
20734   cp_token *type_start_token = NULL;
20735   auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20736 
20737   /* Parse tentatively so that we can back up if we don't find a
20738      enum-specifier.  */
20739   cp_parser_parse_tentatively (parser);
20740 
20741   /* Caller guarantees that the current token is 'enum', an identifier
20742      possibly follows, and the token after that is an opening brace.
20743      If we don't have an identifier, fabricate an anonymous name for
20744      the enumeration being defined.  */
20745   cp_lexer_consume_token (parser->lexer);
20746 
20747   /* Parse the "class" or "struct", which indicates a scoped
20748      enumeration type in C++0x.  */
20749   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20750       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20751     {
20752       if (cxx_dialect < cxx11)
20753         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20754 
20755       /* Consume the `struct' or `class' token.  */
20756       cp_lexer_consume_token (parser->lexer);
20757 
20758       scoped_enum_p = true;
20759     }
20760 
20761   attributes = cp_parser_attributes_opt (parser);
20762 
20763   /* Clear the qualification.  */
20764   parser->scope = NULL_TREE;
20765   parser->qualifying_scope = NULL_TREE;
20766   parser->object_scope = NULL_TREE;
20767 
20768   /* Figure out in what scope the declaration is being placed.  */
20769   prev_scope = current_scope ();
20770 
20771   type_start_token = cp_lexer_peek_token (parser->lexer);
20772 
20773   push_deferring_access_checks (dk_no_check);
20774   nested_name_specifier
20775     = cp_parser_nested_name_specifier_opt (parser,
20776 					   /*typename_keyword_p=*/true,
20777 					   /*check_dependency_p=*/false,
20778 					   /*type_p=*/false,
20779 					   /*is_declaration=*/false);
20780 
20781   if (nested_name_specifier)
20782     {
20783       tree name;
20784 
20785       identifier = cp_parser_identifier (parser);
20786       name = cp_parser_lookup_name (parser, identifier,
20787 				    enum_type,
20788 				    /*is_template=*/false,
20789 				    /*is_namespace=*/false,
20790 				    /*check_dependency=*/true,
20791 				    /*ambiguous_decls=*/NULL,
20792 				    input_location);
20793       if (name && name != error_mark_node)
20794 	{
20795 	  type = TREE_TYPE (name);
20796 	  if (TREE_CODE (type) == TYPENAME_TYPE)
20797 	    {
20798 	      /* Are template enums allowed in ISO? */
20799 	      if (template_parm_scope_p ())
20800 		pedwarn (type_start_token->location, OPT_Wpedantic,
20801 			 "%qD is an enumeration template", name);
20802 	      /* ignore a typename reference, for it will be solved by name
20803 	         in start_enum.  */
20804 	      type = NULL_TREE;
20805 	    }
20806 	}
20807       else if (nested_name_specifier == error_mark_node)
20808 	/* We already issued an error.  */;
20809       else
20810 	{
20811 	  error_at (type_start_token->location,
20812 		    "%qD does not name an enumeration in %qT",
20813 		    identifier, nested_name_specifier);
20814 	  nested_name_specifier = error_mark_node;
20815 	}
20816     }
20817   else
20818     {
20819       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20820 	identifier = cp_parser_identifier (parser);
20821       else
20822 	{
20823 	  identifier = make_anon_name ();
20824 	  is_unnamed = true;
20825 	  if (scoped_enum_p)
20826 	    error_at (type_start_token->location,
20827 		      "unnamed scoped enum is not allowed");
20828 	}
20829     }
20830   pop_deferring_access_checks ();
20831 
20832   /* Check for the `:' that denotes a specified underlying type in C++0x.
20833      Note that a ':' could also indicate a bitfield width, however.  */
20834   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20835     {
20836       cp_decl_specifier_seq type_specifiers;
20837 
20838       /* Consume the `:'.  */
20839       cp_lexer_consume_token (parser->lexer);
20840 
20841       auto tdf
20842 	= make_temp_override (parser->type_definition_forbidden_message,
20843 			      G_("types may not be defined in enum-base"));
20844 
20845       /* Parse the type-specifier-seq.  */
20846       cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
20847 				    /*is_declaration=*/false,
20848 				    /*is_trailing_return=*/false,
20849                                     &type_specifiers);
20850 
20851       /* At this point this is surely not elaborated type specifier.  */
20852       if (!cp_parser_parse_definitely (parser))
20853 	return NULL_TREE;
20854 
20855       if (cxx_dialect < cxx11)
20856         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20857 
20858       has_underlying_type = true;
20859 
20860       /* If that didn't work, stop.  */
20861       if (type_specifiers.type != error_mark_node)
20862         {
20863           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
20864                                             /*initialized=*/0, NULL);
20865           if (underlying_type == error_mark_node
20866 	      || check_for_bare_parameter_packs (underlying_type))
20867             underlying_type = NULL_TREE;
20868         }
20869     }
20870 
20871   /* Look for the `{' but don't consume it yet.  */
20872   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20873     {
20874       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
20875 	{
20876 	  if (has_underlying_type)
20877 	    cp_parser_commit_to_tentative_parse (parser);
20878 	  cp_parser_error (parser, "expected %<{%>");
20879 	  if (has_underlying_type)
20880 	    return error_mark_node;
20881 	}
20882       /* An opaque-enum-specifier must have a ';' here.  */
20883       if ((scoped_enum_p || underlying_type)
20884 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20885 	{
20886 	  if (has_underlying_type)
20887 	    cp_parser_commit_to_tentative_parse (parser);
20888 	  cp_parser_error (parser, "expected %<;%> or %<{%>");
20889 	  if (has_underlying_type)
20890 	    return error_mark_node;
20891 	}
20892     }
20893 
20894   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
20895     return NULL_TREE;
20896 
20897   if (nested_name_specifier)
20898     {
20899       if (CLASS_TYPE_P (nested_name_specifier))
20900 	{
20901 	  nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
20902 	  TYPE_BEING_DEFINED (nested_name_specifier) = 1;
20903 	  push_scope (nested_name_specifier);
20904 	}
20905       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
20906 	push_nested_namespace (nested_name_specifier);
20907     }
20908 
20909   /* Issue an error message if type-definitions are forbidden here.  */
20910   if (!cp_parser_check_type_definition (parser))
20911     type = error_mark_node;
20912   else
20913     /* Create the new type.  We do this before consuming the opening
20914        brace so the enum will be recorded as being on the line of its
20915        tag (or the 'enum' keyword, if there is no tag).  */
20916     type = start_enum (identifier, type, underlying_type,
20917 		       attributes, scoped_enum_p, &is_new_type);
20918 
20919   /* If the next token is not '{' it is an opaque-enum-specifier or an
20920      elaborated-type-specifier.  */
20921   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20922     {
20923       timevar_push (TV_PARSE_ENUM);
20924       if (nested_name_specifier
20925 	  && nested_name_specifier != error_mark_node)
20926 	{
20927 	  /* The following catches invalid code such as:
20928 	     enum class S<int>::E { A, B, C }; */
20929 	  if (!processing_specialization
20930 	      && CLASS_TYPE_P (nested_name_specifier)
20931 	      && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
20932 	    error_at (type_start_token->location, "cannot add an enumerator "
20933 		      "list to a template instantiation");
20934 
20935 	  if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
20936 	    {
20937 	      error_at (type_start_token->location,
20938 			"%<%T::%E%> has not been declared",
20939 			TYPE_CONTEXT (nested_name_specifier),
20940 			nested_name_specifier);
20941 	      type = error_mark_node;
20942 	    }
20943 	  else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
20944 		   && !CLASS_TYPE_P (nested_name_specifier))
20945 	    {
20946 	      error_at (type_start_token->location, "nested name specifier "
20947 			"%qT for enum declaration does not name a class "
20948 			"or namespace", nested_name_specifier);
20949 	      type = error_mark_node;
20950 	    }
20951 	  /* If that scope does not contain the scope in which the
20952 	     class was originally declared, the program is invalid.  */
20953 	  else if (prev_scope && !is_ancestor (prev_scope,
20954 					       nested_name_specifier))
20955 	    {
20956 	      if (at_namespace_scope_p ())
20957 		error_at (type_start_token->location,
20958 			  "declaration of %qD in namespace %qD which does not "
20959 			  "enclose %qD",
20960 			  type, prev_scope, nested_name_specifier);
20961 	      else
20962 		error_at (type_start_token->location,
20963 			  "declaration of %qD in %qD which does not "
20964 			  "enclose %qD",
20965 			  type, prev_scope, nested_name_specifier);
20966 	      type = error_mark_node;
20967 	    }
20968 	  /* If that scope is the scope where the declaration is being placed
20969 	     the program is invalid.  */
20970 	  else if (CLASS_TYPE_P (nested_name_specifier)
20971 		   && CLASS_TYPE_P (prev_scope)
20972 		   && same_type_p (nested_name_specifier, prev_scope))
20973 	    {
20974 	      permerror (type_start_token->location,
20975 			 "extra qualification not allowed");
20976 	      nested_name_specifier = NULL_TREE;
20977 	    }
20978 	}
20979 
20980       if (scoped_enum_p)
20981 	begin_scope (sk_scoped_enum, type);
20982 
20983       /* Consume the opening brace.  */
20984       matching_braces braces;
20985       braces.consume_open (parser);
20986 
20987       if (type == error_mark_node)
20988 	; /* Nothing to add */
20989       else if (OPAQUE_ENUM_P (type)
20990 	       || (cxx_dialect > cxx98 && processing_specialization))
20991 	{
20992 	  new_value_list = true;
20993 	  SET_OPAQUE_ENUM_P (type, false);
20994 	  DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20995 	}
20996       else
20997 	{
20998 	  error_at (type_start_token->location,
20999 		    "multiple definition of %q#T", type);
21000 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21001 		  "previous definition here");
21002 	  type = error_mark_node;
21003 	}
21004 
21005       if (type == error_mark_node)
21006 	cp_parser_skip_to_end_of_block_or_statement (parser);
21007       /* If the next token is not '}', then there are some enumerators.  */
21008       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21009 	{
21010 	  if (is_unnamed && !scoped_enum_p
21011 	      /* Don't warn for enum {} a; here.  */
21012 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21013 	    pedwarn (type_start_token->location, OPT_Wpedantic,
21014 		     "ISO C++ forbids empty unnamed enum");
21015 	}
21016       else
21017 	{
21018 	  /* We've seen a '{' so we know we're in an enum-specifier.
21019 	     Commit to any tentative parse to get syntax errors.  */
21020 	  cp_parser_commit_to_tentative_parse (parser);
21021 	  cp_parser_enumerator_list (parser, type);
21022 	}
21023 
21024       /* Consume the final '}'.  */
21025       braces.require_close (parser);
21026 
21027       if (scoped_enum_p)
21028 	finish_scope ();
21029       timevar_pop (TV_PARSE_ENUM);
21030     }
21031   else
21032     {
21033       /* If a ';' follows, then it is an opaque-enum-specifier
21034 	and additional restrictions apply.  */
21035       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21036 	{
21037 	  if (is_unnamed)
21038 	    error_at (type_start_token->location,
21039 		      "opaque-enum-specifier without name");
21040 	  else if (nested_name_specifier)
21041 	    error_at (type_start_token->location,
21042 		      "opaque-enum-specifier must use a simple identifier");
21043 	}
21044     }
21045 
21046   /* Look for trailing attributes to apply to this enumeration, and
21047      apply them if appropriate.  */
21048   if (cp_parser_allow_gnu_extensions_p (parser))
21049     {
21050       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21051       cplus_decl_attributes (&type,
21052 			     trailing_attr,
21053 			     (int) ATTR_FLAG_TYPE_IN_PLACE);
21054     }
21055 
21056   /* Finish up the enumeration.  */
21057   if (type != error_mark_node)
21058     {
21059       if (new_value_list)
21060 	finish_enum_value_list (type);
21061       if (is_new_type)
21062 	finish_enum (type);
21063     }
21064 
21065   if (nested_name_specifier)
21066     {
21067       if (CLASS_TYPE_P (nested_name_specifier))
21068 	{
21069 	  TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21070 	  pop_scope (nested_name_specifier);
21071 	}
21072       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21073 	pop_nested_namespace (nested_name_specifier);
21074     }
21075   return type;
21076 }
21077 
21078 /* Parse an enumerator-list.  The enumerators all have the indicated
21079    TYPE.
21080 
21081    enumerator-list:
21082      enumerator-definition
21083      enumerator-list , enumerator-definition  */
21084 
21085 static void
cp_parser_enumerator_list(cp_parser * parser,tree type)21086 cp_parser_enumerator_list (cp_parser* parser, tree type)
21087 {
21088   while (true)
21089     {
21090       /* Parse an enumerator-definition.  */
21091       cp_parser_enumerator_definition (parser, type);
21092 
21093       /* If the next token is not a ',', we've reached the end of
21094 	 the list.  */
21095       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21096 	break;
21097       /* Otherwise, consume the `,' and keep going.  */
21098       cp_lexer_consume_token (parser->lexer);
21099       /* If the next token is a `}', there is a trailing comma.  */
21100       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21101 	{
21102 	  if (cxx_dialect < cxx11)
21103 	    pedwarn (input_location, OPT_Wpedantic,
21104                      "comma at end of enumerator list");
21105 	  break;
21106 	}
21107     }
21108 }
21109 
21110 /* Parse an enumerator-definition.  The enumerator has the indicated
21111    TYPE.
21112 
21113    enumerator-definition:
21114      enumerator
21115      enumerator = constant-expression
21116 
21117    enumerator:
21118      identifier
21119 
21120    GNU Extensions:
21121 
21122    enumerator-definition:
21123      enumerator attributes [opt]
21124      enumerator attributes [opt] = constant-expression  */
21125 
21126 static void
cp_parser_enumerator_definition(cp_parser * parser,tree type)21127 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21128 {
21129   tree identifier;
21130   tree value;
21131   location_t loc;
21132 
21133   /* Save the input location because we are interested in the location
21134      of the identifier and not the location of the explicit value.  */
21135   loc = cp_lexer_peek_token (parser->lexer)->location;
21136 
21137   /* Look for the identifier.  */
21138   identifier = cp_parser_identifier (parser);
21139   if (identifier == error_mark_node)
21140     return;
21141 
21142   /* Parse any specified attributes.  */
21143   tree attrs = cp_parser_attributes_opt (parser);
21144 
21145   /* If the next token is an '=', then there is an explicit value.  */
21146   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21147     {
21148       /* Consume the `=' token.  */
21149       cp_lexer_consume_token (parser->lexer);
21150       /* Parse the value.  */
21151       value = cp_parser_constant_expression (parser);
21152     }
21153   else
21154     value = NULL_TREE;
21155 
21156   /* If we are processing a template, make sure the initializer of the
21157      enumerator doesn't contain any bare template parameter pack.  */
21158   if (current_lambda_expr ())
21159     {
21160       /* In a lambda it should work, but doesn't currently.  */
21161       if (uses_parameter_packs (value))
21162 	{
21163 	  sorry ("unexpanded parameter pack in enumerator in lambda");
21164 	  value = error_mark_node;
21165 	}
21166     }
21167   else if (check_for_bare_parameter_packs (value))
21168     value = error_mark_node;
21169 
21170   /* Create the enumerator.  */
21171   build_enumerator (identifier, value, type, attrs, loc);
21172 }
21173 
21174 /* Parse a namespace-name.
21175 
21176    namespace-name:
21177      original-namespace-name
21178      namespace-alias
21179 
21180    Returns the NAMESPACE_DECL for the namespace.  */
21181 
21182 static tree
cp_parser_namespace_name(cp_parser * parser)21183 cp_parser_namespace_name (cp_parser* parser)
21184 {
21185   tree identifier;
21186   tree namespace_decl;
21187 
21188   cp_token *token = cp_lexer_peek_token (parser->lexer);
21189 
21190   /* Get the name of the namespace.  */
21191   identifier = cp_parser_identifier (parser);
21192   if (identifier == error_mark_node)
21193     return error_mark_node;
21194 
21195   /* Look up the identifier in the currently active scope.  Look only
21196      for namespaces, due to:
21197 
21198        [basic.lookup.udir]
21199 
21200        When looking up a namespace-name in a using-directive or alias
21201        definition, only namespace names are considered.
21202 
21203      And:
21204 
21205        [basic.lookup.qual]
21206 
21207        During the lookup of a name preceding the :: scope resolution
21208        operator, object, function, and enumerator names are ignored.
21209 
21210      (Note that cp_parser_qualifying_entity only calls this
21211      function if the token after the name is the scope resolution
21212      operator.)  */
21213   namespace_decl = cp_parser_lookup_name (parser, identifier,
21214 					  none_type,
21215 					  /*is_template=*/false,
21216 					  /*is_namespace=*/true,
21217 					  /*check_dependency=*/true,
21218 					  /*ambiguous_decls=*/NULL,
21219 					  token->location);
21220   /* If it's not a namespace, issue an error.  */
21221   if (namespace_decl == error_mark_node
21222       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21223     {
21224       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21225 	{
21226 	  auto_diagnostic_group d;
21227 	  name_hint hint;
21228 	  if (namespace_decl == error_mark_node
21229 	      && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21230 	    hint = suggest_alternative_in_explicit_scope (token->location,
21231 							  identifier,
21232 							  parser->scope);
21233 	  if (const char *suggestion = hint.suggestion ())
21234 	    {
21235 	      gcc_rich_location richloc (token->location);
21236 	      richloc.add_fixit_replace (suggestion);
21237 	      error_at (&richloc,
21238 			"%qD is not a namespace-name; did you mean %qs?",
21239 			identifier, suggestion);
21240 	    }
21241 	  else
21242 	    error_at (token->location, "%qD is not a namespace-name",
21243 		      identifier);
21244 	}
21245       else
21246 	cp_parser_error (parser, "expected namespace-name");
21247       namespace_decl = error_mark_node;
21248     }
21249 
21250   return namespace_decl;
21251 }
21252 
21253 /* Parse a namespace-definition.
21254 
21255    namespace-definition:
21256      named-namespace-definition
21257      unnamed-namespace-definition
21258 
21259    named-namespace-definition:
21260      original-namespace-definition
21261      extension-namespace-definition
21262 
21263    original-namespace-definition:
21264      namespace identifier { namespace-body }
21265 
21266    extension-namespace-definition:
21267      namespace original-namespace-name { namespace-body }
21268 
21269    unnamed-namespace-definition:
21270      namespace { namespace-body } */
21271 
21272 static void
cp_parser_namespace_definition(cp_parser * parser)21273 cp_parser_namespace_definition (cp_parser* parser)
21274 {
21275   tree identifier;
21276   int nested_definition_count = 0;
21277 
21278   cp_ensure_no_omp_declare_simd (parser);
21279   cp_ensure_no_oacc_routine (parser);
21280 
21281   bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21282   const bool topmost_inline_p = is_inline;
21283 
21284   if (is_inline)
21285     {
21286       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21287       cp_lexer_consume_token (parser->lexer);
21288     }
21289 
21290   /* Look for the `namespace' keyword.  */
21291   cp_token* token
21292     = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21293 
21294   /* Parse any specified attributes before the identifier.  */
21295   tree attribs = cp_parser_attributes_opt (parser);
21296 
21297   for (;;)
21298     {
21299       identifier = NULL_TREE;
21300 
21301       bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21302 							     RID_INLINE);
21303       if (nested_inline_p && nested_definition_count != 0)
21304 	{
21305 	  if (pedantic && cxx_dialect < cxx20)
21306 	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21307 		     OPT_Wc__20_extensions, "nested inline namespace "
21308 		     "definitions only available with %<-std=c++20%> or "
21309 		     "%<-std=gnu++20%>");
21310 	  cp_lexer_consume_token (parser->lexer);
21311 	}
21312 
21313       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21314 	{
21315 	  identifier = cp_parser_identifier (parser);
21316 
21317 	  if (cp_next_tokens_can_be_std_attribute_p (parser))
21318 	    pedwarn (input_location, OPT_Wpedantic,
21319 		     "standard attributes on namespaces must precede "
21320 		     "the namespace name");
21321 
21322 	  /* Parse any attributes specified after the identifier.  */
21323 	  attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21324 	}
21325 
21326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21327 	{
21328 	  /* Don't forget that the innermost namespace might have been
21329 	     marked as inline.  Use |= because we cannot overwrite
21330 	     IS_INLINE in case the outermost namespace is inline, but
21331 	     there are no nested inlines.  */
21332 	  is_inline |= nested_inline_p;
21333 	  break;
21334 	}
21335 
21336       if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21337         pedwarn (input_location, OPT_Wc__17_extensions,
21338 		 "nested namespace definitions only available with "
21339 		 "%<-std=c++17%> or %<-std=gnu++17%>");
21340 
21341       /* Nested namespace names can create new namespaces (unlike
21342 	 other qualified-ids).  */
21343       if (int count = (identifier
21344 		       ? push_namespace (identifier, nested_inline_p)
21345 		       : 0))
21346 	nested_definition_count += count;
21347       else
21348 	cp_parser_error (parser, "nested namespace name required");
21349       cp_lexer_consume_token (parser->lexer);
21350     }
21351 
21352   if (nested_definition_count && !identifier)
21353     cp_parser_error (parser, "namespace name required");
21354 
21355   if (nested_definition_count && attribs)
21356     error_at (token->location,
21357 	      "a nested namespace definition cannot have attributes");
21358   if (nested_definition_count && topmost_inline_p)
21359     error_at (token->location,
21360 	      "a nested namespace definition cannot be inline");
21361 
21362   /* Start the namespace.  */
21363   nested_definition_count += push_namespace (identifier, is_inline);
21364 
21365   bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21366 
21367   warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21368 
21369   /* Look for the `{' to validate starting the namespace.  */
21370   matching_braces braces;
21371   if (braces.require_open (parser))
21372     {
21373       /* Parse the body of the namespace.  */
21374       cp_parser_namespace_body (parser);
21375 
21376       /* Look for the final `}'.  */
21377       braces.require_close (parser);
21378     }
21379 
21380   if (has_visibility)
21381     pop_visibility (1);
21382 
21383   /* Pop the nested namespace definitions.  */
21384   while (nested_definition_count--)
21385     pop_namespace ();
21386 }
21387 
21388 /* Parse a namespace-body.
21389 
21390    namespace-body:
21391      declaration-seq [opt]  */
21392 
21393 static void
cp_parser_namespace_body(cp_parser * parser)21394 cp_parser_namespace_body (cp_parser* parser)
21395 {
21396   cp_parser_declaration_seq_opt (parser);
21397 }
21398 
21399 /* Parse a namespace-alias-definition.
21400 
21401    namespace-alias-definition:
21402      namespace identifier = qualified-namespace-specifier ;  */
21403 
21404 static void
cp_parser_namespace_alias_definition(cp_parser * parser)21405 cp_parser_namespace_alias_definition (cp_parser* parser)
21406 {
21407   tree identifier;
21408   tree namespace_specifier;
21409 
21410   cp_token *token = cp_lexer_peek_token (parser->lexer);
21411 
21412   /* Look for the `namespace' keyword.  */
21413   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21414   /* Look for the identifier.  */
21415   identifier = cp_parser_identifier (parser);
21416   if (identifier == error_mark_node)
21417     return;
21418   /* Look for the `=' token.  */
21419   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21420       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21421     {
21422       error_at (token->location, "%<namespace%> definition is not allowed here");
21423       /* Skip the definition.  */
21424       cp_lexer_consume_token (parser->lexer);
21425       if (cp_parser_skip_to_closing_brace (parser))
21426 	cp_lexer_consume_token (parser->lexer);
21427       return;
21428     }
21429   cp_parser_require (parser, CPP_EQ, RT_EQ);
21430   /* Look for the qualified-namespace-specifier.  */
21431   namespace_specifier
21432     = cp_parser_qualified_namespace_specifier (parser);
21433   cp_warn_deprecated_use_scopes (namespace_specifier);
21434   /* Look for the `;' token.  */
21435   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21436 
21437   /* Register the alias in the symbol table.  */
21438   do_namespace_alias (identifier, namespace_specifier);
21439 }
21440 
21441 /* Parse a qualified-namespace-specifier.
21442 
21443    qualified-namespace-specifier:
21444      :: [opt] nested-name-specifier [opt] namespace-name
21445 
21446    Returns a NAMESPACE_DECL corresponding to the specified
21447    namespace.  */
21448 
21449 static tree
cp_parser_qualified_namespace_specifier(cp_parser * parser)21450 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21451 {
21452   /* Look for the optional `::'.  */
21453   cp_parser_global_scope_opt (parser,
21454 			      /*current_scope_valid_p=*/false);
21455 
21456   /* Look for the optional nested-name-specifier.  */
21457   cp_parser_nested_name_specifier_opt (parser,
21458 				       /*typename_keyword_p=*/false,
21459 				       /*check_dependency_p=*/true,
21460 				       /*type_p=*/false,
21461 				       /*is_declaration=*/true);
21462 
21463   return cp_parser_namespace_name (parser);
21464 }
21465 
21466 /* Subroutine of cp_parser_using_declaration.  */
21467 
21468 static tree
finish_using_decl(tree qscope,tree identifier,bool typename_p=false)21469 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21470 {
21471   tree decl = NULL_TREE;
21472   if (at_class_scope_p ())
21473     {
21474       /* Create the USING_DECL.  */
21475       decl = do_class_using_decl (qscope, identifier);
21476 
21477       if (check_for_bare_parameter_packs (decl))
21478 	return error_mark_node;
21479 
21480       if (decl && typename_p)
21481 	USING_DECL_TYPENAME_P (decl) = 1;
21482 
21483       /* Add it to the list of members in this class.  */
21484       finish_member_declaration (decl);
21485     }
21486   else
21487     finish_nonmember_using_decl (qscope, identifier);
21488   return decl;
21489 }
21490 
21491 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21492    access declaration.
21493 
21494    using-declaration:
21495      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21496      using :: unqualified-id ;
21497 
21498    access-declaration:
21499      qualified-id ;
21500 
21501    */
21502 
21503 static bool
cp_parser_using_declaration(cp_parser * parser,bool access_declaration_p)21504 cp_parser_using_declaration (cp_parser* parser,
21505 			     bool access_declaration_p)
21506 {
21507   cp_token *token;
21508   bool typename_p = false;
21509   bool global_scope_p;
21510   tree identifier;
21511   tree qscope;
21512   int oldcount = errorcount;
21513   cp_token *diag_token = NULL;
21514 
21515   if (access_declaration_p)
21516     {
21517       diag_token = cp_lexer_peek_token (parser->lexer);
21518       cp_parser_parse_tentatively (parser);
21519     }
21520   else
21521     {
21522       /* Look for the `using' keyword.  */
21523       cp_parser_require_keyword (parser, RID_USING, RT_USING);
21524 
21525  again:
21526       /* Peek at the next token.  */
21527       token = cp_lexer_peek_token (parser->lexer);
21528       /* See if it's `typename'.  */
21529       if (token->keyword == RID_TYPENAME)
21530 	{
21531 	  /* Remember that we've seen it.  */
21532 	  typename_p = true;
21533 	  /* Consume the `typename' token.  */
21534 	  cp_lexer_consume_token (parser->lexer);
21535 	}
21536     }
21537 
21538   /* Look for the optional global scope qualification.  */
21539   global_scope_p
21540     = (cp_parser_global_scope_opt (parser,
21541 				   /*current_scope_valid_p=*/false)
21542        != NULL_TREE);
21543 
21544   /* If we saw `typename', or didn't see `::', then there must be a
21545      nested-name-specifier present.  */
21546   if (typename_p || !global_scope_p)
21547     {
21548       qscope = cp_parser_nested_name_specifier (parser, typename_p,
21549 						/*check_dependency_p=*/true,
21550 						/*type_p=*/false,
21551 						/*is_declaration=*/true);
21552       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21553 	{
21554 	  cp_parser_skip_to_end_of_block_or_statement (parser);
21555 	  return false;
21556 	}
21557     }
21558   /* Otherwise, we could be in either of the two productions.  In that
21559      case, treat the nested-name-specifier as optional.  */
21560   else
21561     qscope = cp_parser_nested_name_specifier_opt (parser,
21562 						  /*typename_keyword_p=*/false,
21563 						  /*check_dependency_p=*/true,
21564 						  /*type_p=*/false,
21565 						  /*is_declaration=*/true);
21566   if (!qscope)
21567     qscope = global_namespace;
21568 
21569   cp_warn_deprecated_use_scopes (qscope);
21570 
21571   if (access_declaration_p && cp_parser_error_occurred (parser))
21572     /* Something has already gone wrong; there's no need to parse
21573        further.  Since an error has occurred, the return value of
21574        cp_parser_parse_definitely will be false, as required.  */
21575     return cp_parser_parse_definitely (parser);
21576 
21577   token = cp_lexer_peek_token (parser->lexer);
21578   /* Parse the unqualified-id.  */
21579   identifier = cp_parser_unqualified_id (parser,
21580 					 /*template_keyword_p=*/false,
21581 					 /*check_dependency_p=*/true,
21582 					 /*declarator_p=*/true,
21583 					 /*optional_p=*/false);
21584 
21585   if (access_declaration_p)
21586     {
21587       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21588 	cp_parser_simulate_error (parser);
21589       if (!cp_parser_parse_definitely (parser))
21590 	return false;
21591     }
21592   else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21593     {
21594       cp_token *ell = cp_lexer_consume_token (parser->lexer);
21595       if (cxx_dialect < cxx17)
21596 	pedwarn (ell->location, OPT_Wc__17_extensions,
21597 		 "pack expansion in using-declaration only available "
21598 		 "with %<-std=c++17%> or %<-std=gnu++17%>");
21599       qscope = make_pack_expansion (qscope);
21600     }
21601 
21602   /* The function we call to handle a using-declaration is different
21603      depending on what scope we are in.  */
21604   if (qscope == error_mark_node || identifier == error_mark_node)
21605     ;
21606   else if (!identifier_p (identifier)
21607 	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
21608     /* [namespace.udecl]
21609 
21610        A using declaration shall not name a template-id.  */
21611     error_at (token->location,
21612 	      "a template-id may not appear in a using-declaration");
21613   else
21614     {
21615       tree decl = finish_using_decl (qscope, identifier, typename_p);
21616 
21617       if (decl == error_mark_node)
21618 	{
21619 	  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21620 	  return false;
21621 	}
21622     }
21623 
21624   if (!access_declaration_p
21625       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21626     {
21627       cp_token *comma = cp_lexer_consume_token (parser->lexer);
21628       if (cxx_dialect < cxx17)
21629 	pedwarn (comma->location, OPT_Wc__17_extensions,
21630 		 "comma-separated list in using-declaration only available "
21631 		 "with %<-std=c++17%> or %<-std=gnu++17%>");
21632       goto again;
21633     }
21634 
21635   /* Look for the final `;'.  */
21636   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21637 
21638   if (access_declaration_p && errorcount == oldcount)
21639     warning_at (diag_token->location, OPT_Wdeprecated,
21640 		"access declarations are deprecated "
21641 		"in favour of using-declarations; "
21642 		"suggestion: add the %<using%> keyword");
21643 
21644   return true;
21645 }
21646 
21647 /* C++20 using enum declaration.
21648 
21649    using-enum-declaration :
21650        using elaborated-enum-specifier ;  */
21651 
21652 static void
cp_parser_using_enum(cp_parser * parser)21653 cp_parser_using_enum (cp_parser *parser)
21654 {
21655   cp_parser_require_keyword (parser, RID_USING, RT_USING);
21656 
21657   /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21658      breaks one of the motivating examples in using-enum-5.C.
21659      cp_parser_simple_type_specifier seems to be closer to what we actually
21660      want, though that hasn't been properly specified yet.  */
21661 
21662   /* Consume 'enum'.  */
21663   gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21664   cp_lexer_consume_token (parser->lexer);
21665 
21666   cp_token *start = cp_lexer_peek_token (parser->lexer);
21667 
21668   tree type = (cp_parser_simple_type_specifier
21669 	       (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21670 
21671   cp_token *end = cp_lexer_previous_token (parser->lexer);
21672 
21673   if (type == error_mark_node
21674       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21675     {
21676       cp_parser_skip_to_end_of_block_or_statement (parser);
21677       return;
21678     }
21679   if (TREE_CODE (type) == TYPE_DECL)
21680     type = TREE_TYPE (type);
21681 
21682   /* The elaborated-enum-specifier shall not name a dependent type and the type
21683      shall have a reachable enum-specifier.  */
21684   const char *msg = nullptr;
21685   if (cxx_dialect < cxx20)
21686     msg = _("%<using enum%> "
21687 	    "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21688   else if (dependent_type_p (type))
21689     msg = _("%<using enum%> of dependent type %qT");
21690   else if (TREE_CODE (type) != ENUMERAL_TYPE)
21691     msg = _("%<using enum%> of non-enumeration type %q#T");
21692   else if (!COMPLETE_TYPE_P (type))
21693     msg = _("%<using enum%> of incomplete type %qT");
21694   else if (OPAQUE_ENUM_P (type))
21695     msg = _("%<using enum%> of %qT before its enum-specifier");
21696   if (msg)
21697     {
21698       location_t loc = make_location (start, start, end);
21699       auto_diagnostic_group g;
21700       error_at (loc, msg, type);
21701       loc = location_of (type);
21702       if (cxx_dialect < cxx20 || loc == input_location)
21703 	;
21704       else if (OPAQUE_ENUM_P (type))
21705 	inform (loc, "opaque-enum-declaration here");
21706       else
21707 	inform (loc, "declared here");
21708     }
21709 
21710   /* A using-enum-declaration introduces the enumerator names of the named
21711      enumeration as if by a using-declaration for each enumerator.  */
21712   if (TREE_CODE (type) == ENUMERAL_TYPE)
21713     for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21714       finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21715 }
21716 
21717 /* Parse an alias-declaration.
21718 
21719    alias-declaration:
21720      using identifier attribute-specifier-seq [opt] = type-id  */
21721 
21722 static tree
cp_parser_alias_declaration(cp_parser * parser)21723 cp_parser_alias_declaration (cp_parser* parser)
21724 {
21725   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21726   location_t id_location, type_location;
21727   cp_declarator *declarator;
21728   cp_decl_specifier_seq decl_specs;
21729   bool member_p;
21730   const char *saved_message = NULL;
21731 
21732   /* Look for the `using' keyword.  */
21733   cp_token *using_token
21734     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21735   if (using_token == NULL)
21736     return error_mark_node;
21737 
21738   id_location = cp_lexer_peek_token (parser->lexer)->location;
21739   id = cp_parser_identifier (parser);
21740   if (id == error_mark_node)
21741     return error_mark_node;
21742 
21743   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21744   attributes = cp_parser_attributes_opt (parser);
21745   if (attributes == error_mark_node)
21746     return error_mark_node;
21747 
21748   cp_parser_require (parser, CPP_EQ, RT_EQ);
21749 
21750   if (cp_parser_error_occurred (parser))
21751     return error_mark_node;
21752 
21753   cp_parser_commit_to_tentative_parse (parser);
21754 
21755   /* Now we are going to parse the type-id of the declaration.  */
21756 
21757   /*
21758     [dcl.type]/3 says:
21759 
21760 	"A type-specifier-seq shall not define a class or enumeration
21761 	 unless it appears in the type-id of an alias-declaration (7.1.3) that
21762 	 is not the declaration of a template-declaration."
21763 
21764     In other words, if we currently are in an alias template, the
21765     type-id should not define a type.
21766 
21767     So let's set parser->type_definition_forbidden_message in that
21768     case; cp_parser_check_type_definition (called by
21769     cp_parser_class_specifier) will then emit an error if a type is
21770     defined in the type-id.  */
21771   if (parser->num_template_parameter_lists)
21772     {
21773       saved_message = parser->type_definition_forbidden_message;
21774       parser->type_definition_forbidden_message =
21775 	G_("types may not be defined in alias template declarations");
21776     }
21777 
21778   type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21779 			    &type_location);
21780 
21781   /* Restore the error message if need be.  */
21782   if (parser->num_template_parameter_lists)
21783     parser->type_definition_forbidden_message = saved_message;
21784 
21785   if (type == error_mark_node
21786       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21787     {
21788       cp_parser_skip_to_end_of_block_or_statement (parser);
21789       return error_mark_node;
21790     }
21791 
21792   /* A typedef-name can also be introduced by an alias-declaration. The
21793      identifier following the using keyword becomes a typedef-name. It has
21794      the same semantics as if it were introduced by the typedef
21795      specifier. In particular, it does not define a new type and it shall
21796      not appear in the type-id.  */
21797 
21798   clear_decl_specs (&decl_specs);
21799   decl_specs.type = type;
21800   if (attributes != NULL_TREE)
21801     {
21802       decl_specs.attributes = attributes;
21803       set_and_check_decl_spec_loc (&decl_specs,
21804 				   ds_attribute,
21805 				   attrs_token);
21806     }
21807   set_and_check_decl_spec_loc (&decl_specs,
21808 			       ds_typedef,
21809 			       using_token);
21810   set_and_check_decl_spec_loc (&decl_specs,
21811 			       ds_alias,
21812 			       using_token);
21813   decl_specs.locations[ds_type_spec] = type_location;
21814 
21815   if (parser->num_template_parameter_lists
21816       && !cp_parser_check_template_parameters (parser,
21817 					       /*num_templates=*/0,
21818 					       /*template_id*/false,
21819 					       id_location,
21820 					       /*declarator=*/NULL))
21821     return error_mark_node;
21822 
21823   declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
21824 
21825   member_p = at_class_scope_p ();
21826   if (member_p)
21827     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
21828 		      NULL_TREE, attributes);
21829   else
21830     decl = start_decl (declarator, &decl_specs, 0,
21831 		       attributes, NULL_TREE, &pushed_scope);
21832   if (decl == error_mark_node)
21833     return decl;
21834 
21835   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
21836 
21837   if (pushed_scope)
21838     pop_scope (pushed_scope);
21839 
21840   /* If decl is a template, return its TEMPLATE_DECL so that it gets
21841      added into the symbol table; otherwise, return the TYPE_DECL.  */
21842   if (DECL_LANG_SPECIFIC (decl)
21843       && DECL_TEMPLATE_INFO (decl)
21844       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
21845     {
21846       decl = DECL_TI_TEMPLATE (decl);
21847       if (member_p)
21848 	check_member_template (decl);
21849     }
21850 
21851   return decl;
21852 }
21853 
21854 /* Parse a using-directive.
21855 
21856    using-directive:
21857      attribute-specifier-seq [opt] using namespace :: [opt]
21858        nested-name-specifier [opt] namespace-name ;  */
21859 
21860 static void
cp_parser_using_directive(cp_parser * parser)21861 cp_parser_using_directive (cp_parser* parser)
21862 {
21863   tree namespace_decl;
21864   tree attribs = cp_parser_std_attribute_spec_seq (parser);
21865   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21866     {
21867       /* Error during attribute parsing that resulted in skipping
21868 	 to next semicolon.  */
21869       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21870       return;
21871     }
21872 
21873   /* Look for the `using' keyword.  */
21874   cp_parser_require_keyword (parser, RID_USING, RT_USING);
21875   /* And the `namespace' keyword.  */
21876   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21877   /* Look for the optional `::' operator.  */
21878   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21879   /* And the optional nested-name-specifier.  */
21880   cp_parser_nested_name_specifier_opt (parser,
21881 				       /*typename_keyword_p=*/false,
21882 				       /*check_dependency_p=*/true,
21883 				       /*type_p=*/false,
21884 				       /*is_declaration=*/true);
21885   /* Get the namespace being used.  */
21886   namespace_decl = cp_parser_namespace_name (parser);
21887   cp_warn_deprecated_use_scopes (namespace_decl);
21888   /* And any specified GNU attributes.  */
21889   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21890     attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
21891 
21892   /* Update the symbol table.  */
21893   finish_using_directive (namespace_decl, attribs);
21894 
21895   /* Look for the final `;'.  */
21896   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21897 }
21898 
21899 /* Parse an asm-definition.
21900 
21901   asm-qualifier:
21902     volatile
21903     inline
21904     goto
21905 
21906   asm-qualifier-list:
21907     asm-qualifier
21908     asm-qualifier-list asm-qualifier
21909 
21910    asm-definition:
21911      asm ( string-literal ) ;
21912 
21913    GNU Extension:
21914 
21915    asm-definition:
21916      asm asm-qualifier-list [opt] ( string-literal ) ;
21917      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
21918      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21919 				    : asm-operand-list [opt] ) ;
21920      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21921 				    : asm-operand-list [opt]
21922 			  : asm-clobber-list [opt] ) ;
21923      asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
21924 				    : asm-clobber-list [opt]
21925 				    : asm-goto-list ) ;
21926 
21927   The form with asm-goto-list is valid if and only if the asm-qualifier-list
21928   contains goto, and is the only allowed form in that case.  No duplicates are
21929   allowed in an asm-qualifier-list.  */
21930 
21931 static void
cp_parser_asm_definition(cp_parser * parser)21932 cp_parser_asm_definition (cp_parser* parser)
21933 {
21934   tree string;
21935   tree outputs = NULL_TREE;
21936   tree inputs = NULL_TREE;
21937   tree clobbers = NULL_TREE;
21938   tree labels = NULL_TREE;
21939   tree asm_stmt;
21940   bool extended_p = false;
21941   bool invalid_inputs_p = false;
21942   bool invalid_outputs_p = false;
21943   required_token missing = RT_NONE;
21944   location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
21945 
21946   /* Look for the `asm' keyword.  */
21947   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
21948 
21949   /* In C++20, unevaluated inline assembly is permitted in constexpr
21950      functions.  */
21951   if (parser->in_function_body
21952       && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
21953       && cxx_dialect < cxx20)
21954     pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
21955 	     "function only available with %<-std=c++20%> or "
21956 	     "%<-std=gnu++20%>");
21957 
21958   /* Handle the asm-qualifier-list.  */
21959   location_t volatile_loc = UNKNOWN_LOCATION;
21960   location_t inline_loc = UNKNOWN_LOCATION;
21961   location_t goto_loc = UNKNOWN_LOCATION;
21962   location_t first_loc = UNKNOWN_LOCATION;
21963 
21964   if (cp_parser_allow_gnu_extensions_p (parser))
21965     for (;;)
21966       {
21967 	cp_token *token = cp_lexer_peek_token (parser->lexer);
21968 	location_t loc = token->location;
21969 	switch (cp_lexer_peek_token (parser->lexer)->keyword)
21970 	  {
21971 	  case RID_VOLATILE:
21972 	    if (volatile_loc)
21973 	      {
21974 		error_at (loc, "duplicate %<asm%> qualifier %qT",
21975 			  token->u.value);
21976 		inform (volatile_loc, "first seen here");
21977 	      }
21978 	    else
21979 	      {
21980 		if (!parser->in_function_body)
21981 		  warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
21982 			      "outside of function body", token->u.value);
21983 		volatile_loc = loc;
21984 	      }
21985 	    cp_lexer_consume_token (parser->lexer);
21986 	    continue;
21987 
21988 	  case RID_INLINE:
21989 	    if (inline_loc)
21990 	      {
21991 		error_at (loc, "duplicate %<asm%> qualifier %qT",
21992 			  token->u.value);
21993 		inform (inline_loc, "first seen here");
21994 	      }
21995 	    else
21996 	      inline_loc = loc;
21997 	    if (!first_loc)
21998 	      first_loc = loc;
21999 	    cp_lexer_consume_token (parser->lexer);
22000 	    continue;
22001 
22002 	  case RID_GOTO:
22003 	    if (goto_loc)
22004 	      {
22005 		error_at (loc, "duplicate %<asm%> qualifier %qT",
22006 			  token->u.value);
22007 		inform (goto_loc, "first seen here");
22008 	      }
22009 	    else
22010 	      goto_loc = loc;
22011 	    if (!first_loc)
22012 	      first_loc = loc;
22013 	    cp_lexer_consume_token (parser->lexer);
22014 	    continue;
22015 
22016 	  case RID_CONST:
22017 	  case RID_RESTRICT:
22018 	    error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22019 	    cp_lexer_consume_token (parser->lexer);
22020 	    continue;
22021 
22022 	  default:
22023 	    break;
22024 	  }
22025 	break;
22026       }
22027 
22028   bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22029   bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22030   bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22031 
22032   if (!parser->in_function_body && (inline_p || goto_p))
22033     {
22034       error_at (first_loc, "%<asm%> qualifier outside of function body");
22035       inline_p = goto_p = false;
22036     }
22037 
22038   /* Look for the opening `('.  */
22039   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22040     return;
22041   /* Look for the string.  */
22042   string = cp_parser_string_literal (parser, false, false);
22043   if (string == error_mark_node)
22044     {
22045       cp_parser_skip_to_closing_parenthesis (parser, true, false,
22046 					     /*consume_paren=*/true);
22047       return;
22048     }
22049 
22050   /* If we're allowing GNU extensions, check for the extended assembly
22051      syntax.  Unfortunately, the `:' tokens need not be separated by
22052      a space in C, and so, for compatibility, we tolerate that here
22053      too.  Doing that means that we have to treat the `::' operator as
22054      two `:' tokens.  */
22055   if (cp_parser_allow_gnu_extensions_p (parser)
22056       && parser->in_function_body
22057       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22058 	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22059     {
22060       bool inputs_p = false;
22061       bool clobbers_p = false;
22062       bool labels_p = false;
22063 
22064       /* The extended syntax was used.  */
22065       extended_p = true;
22066 
22067       /* Look for outputs.  */
22068       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22069 	{
22070 	  /* Consume the `:'.  */
22071 	  cp_lexer_consume_token (parser->lexer);
22072 	  /* Parse the output-operands.  */
22073 	  if (cp_lexer_next_token_is_not (parser->lexer,
22074 					  CPP_COLON)
22075 	      && cp_lexer_next_token_is_not (parser->lexer,
22076 					     CPP_SCOPE)
22077 	      && cp_lexer_next_token_is_not (parser->lexer,
22078 					     CPP_CLOSE_PAREN))
22079             {
22080               outputs = cp_parser_asm_operand_list (parser);
22081               if (outputs == error_mark_node)
22082                 invalid_outputs_p = true;
22083             }
22084 	}
22085       /* If the next token is `::', there are no outputs, and the
22086 	 next token is the beginning of the inputs.  */
22087       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22088 	/* The inputs are coming next.  */
22089 	inputs_p = true;
22090 
22091       /* Look for inputs.  */
22092       if (inputs_p
22093 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22094 	{
22095 	  /* Consume the `:' or `::'.  */
22096 	  cp_lexer_consume_token (parser->lexer);
22097 	  /* Parse the output-operands.  */
22098 	  if (cp_lexer_next_token_is_not (parser->lexer,
22099 					  CPP_COLON)
22100 	      && cp_lexer_next_token_is_not (parser->lexer,
22101 					     CPP_SCOPE)
22102 	      && cp_lexer_next_token_is_not (parser->lexer,
22103 					     CPP_CLOSE_PAREN))
22104             {
22105               inputs = cp_parser_asm_operand_list (parser);
22106               if (inputs == error_mark_node)
22107                 invalid_inputs_p = true;
22108             }
22109 	}
22110       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22111 	/* The clobbers are coming next.  */
22112 	clobbers_p = true;
22113 
22114       /* Look for clobbers.  */
22115       if (clobbers_p
22116 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22117 	{
22118 	  clobbers_p = true;
22119 	  /* Consume the `:' or `::'.  */
22120 	  cp_lexer_consume_token (parser->lexer);
22121 	  /* Parse the clobbers.  */
22122 	  if (cp_lexer_next_token_is_not (parser->lexer,
22123 					  CPP_COLON)
22124 	      && cp_lexer_next_token_is_not (parser->lexer,
22125 					     CPP_CLOSE_PAREN))
22126 	    clobbers = cp_parser_asm_clobber_list (parser);
22127 	}
22128       else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22129 	/* The labels are coming next.  */
22130 	labels_p = true;
22131 
22132       /* Look for labels.  */
22133       if (labels_p
22134 	  || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22135 	{
22136 	  labels_p = true;
22137 	  /* Consume the `:' or `::'.  */
22138 	  cp_lexer_consume_token (parser->lexer);
22139 	  /* Parse the labels.  */
22140 	  labels = cp_parser_asm_label_list (parser);
22141 	}
22142 
22143       if (goto_p && !labels_p)
22144 	missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22145     }
22146   else if (goto_p)
22147     missing = RT_COLON_SCOPE;
22148 
22149   /* Look for the closing `)'.  */
22150   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22151 			  missing ? missing : RT_CLOSE_PAREN))
22152     cp_parser_skip_to_closing_parenthesis (parser, true, false,
22153 					   /*consume_paren=*/true);
22154   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22155 
22156   if (!invalid_inputs_p && !invalid_outputs_p)
22157     {
22158       /* Create the ASM_EXPR.  */
22159       if (parser->in_function_body)
22160 	{
22161 	  asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22162 				      inputs, clobbers, labels, inline_p);
22163 	  /* If the extended syntax was not used, mark the ASM_EXPR.  */
22164 	  if (!extended_p)
22165 	    {
22166 	      tree temp = asm_stmt;
22167 	      if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22168 		temp = TREE_OPERAND (temp, 0);
22169 
22170 	      ASM_INPUT_P (temp) = 1;
22171 	    }
22172 	}
22173       else
22174 	symtab->finalize_toplevel_asm (string);
22175     }
22176 }
22177 
22178 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22179    type that comes from the decl-specifier-seq.  */
22180 
22181 static tree
strip_declarator_types(tree type,cp_declarator * declarator)22182 strip_declarator_types (tree type, cp_declarator *declarator)
22183 {
22184   for (cp_declarator *d = declarator; d;)
22185     switch (d->kind)
22186       {
22187       case cdk_id:
22188       case cdk_decomp:
22189       case cdk_error:
22190 	d = NULL;
22191 	break;
22192 
22193       default:
22194 	if (TYPE_PTRMEMFUNC_P (type))
22195 	  type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22196 	type = TREE_TYPE (type);
22197 	d = d->declarator;
22198 	break;
22199       }
22200 
22201   return type;
22202 }
22203 
22204 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22205    a construct looks like a variable definition but is actually a function
22206    declaration.  DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22207    is the declarator for this function declaration.  */
22208 
22209 static void
warn_about_ambiguous_parse(const cp_decl_specifier_seq * decl_specifiers,const cp_declarator * declarator)22210 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22211 			    const cp_declarator *declarator)
22212 {
22213   /* Only warn if we are declaring a function at block scope.  */
22214   if (!at_function_scope_p ())
22215     return;
22216 
22217   /* And only if there is no storage class specified.  */
22218   if (decl_specifiers->storage_class != sc_none
22219       || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22220     return;
22221 
22222   if (declarator->kind != cdk_function
22223       || !declarator->declarator
22224       || declarator->declarator->kind != cdk_id
22225       || !identifier_p (get_unqualified_id
22226 			(const_cast<cp_declarator *>(declarator))))
22227     return;
22228 
22229   /* Don't warn when the whole declarator (not just the declarator-id!)
22230      was parenthesized.  That is, don't warn for int(n()) but do warn
22231      for int(f)().  */
22232   if (declarator->parenthesized != UNKNOWN_LOCATION)
22233     return;
22234 
22235   tree type;
22236   if (decl_specifiers->type)
22237     {
22238       type = decl_specifiers->type;
22239       if (TREE_CODE (type) == TYPE_DECL)
22240 	type = TREE_TYPE (type);
22241 
22242       /* If the return type is void there is no ambiguity.  */
22243       if (same_type_p (type, void_type_node))
22244 	return;
22245     }
22246   else if (decl_specifiers->any_type_specifiers_p)
22247     /* Code like long f(); will have null ->type.  If we have any
22248        type-specifiers, pretend we've seen int.  */
22249     type = integer_type_node;
22250   else
22251     return;
22252 
22253   auto_diagnostic_group d;
22254   location_t loc = declarator->u.function.parens_loc;
22255   tree params = declarator->u.function.parameters;
22256   const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22257 
22258   /* The T t() case.  */
22259   if (params == void_list_node)
22260     {
22261       if (warning_at (loc, OPT_Wvexing_parse,
22262 		      "empty parentheses were disambiguated as a function "
22263 		      "declaration"))
22264 	{
22265 	  /* () means value-initialization (C++03 and up); {} (C++11 and up)
22266 	     means value-initialization or aggregate-initialization, nothing
22267 	     means default-initialization.  We can only suggest removing the
22268 	     parentheses/adding {} if T has a default constructor.  */
22269 	  if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22270 	    {
22271 	      gcc_rich_location iloc (loc);
22272 	      iloc.add_fixit_remove ();
22273 	      inform (&iloc, "remove parentheses to default-initialize "
22274 		      "a variable");
22275 	      if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22276 		{
22277 		  if (CP_AGGREGATE_TYPE_P (type))
22278 		    inform (loc, "or replace parentheses with braces to "
22279 			    "aggregate-initialize a variable");
22280 		  else
22281 		    inform (loc, "or replace parentheses with braces to "
22282 			    "value-initialize a variable");
22283 		}
22284 	    }
22285 	}
22286       return;
22287     }
22288 
22289   /* If we had (...) or the parameter-list wasn't parenthesized,
22290      we're done.  */
22291   if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22292     return;
22293 
22294   /* The T t(X()) case.  */
22295   if (list_length (params) == 2)
22296     {
22297       if (warning_at (loc, OPT_Wvexing_parse,
22298 		      "parentheses were disambiguated as a function "
22299 		      "declaration"))
22300 	{
22301 	  gcc_rich_location iloc (loc);
22302 	  /* {}-initialization means that we can use an initializer-list
22303 	     constructor if no default constructor is available, so don't
22304 	     suggest using {} for classes that have an initializer_list
22305 	     constructor.  */
22306 	  if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22307 	    {
22308 	      iloc.add_fixit_replace (get_start (loc), "{");
22309 	      iloc.add_fixit_replace (get_finish (loc), "}");
22310 	      inform (&iloc, "replace parentheses with braces to declare a "
22311 		      "variable");
22312 	    }
22313 	  else
22314 	    {
22315 	      iloc.add_fixit_insert_after (get_start (loc), "(");
22316 	      iloc.add_fixit_insert_before (get_finish (loc), ")");
22317 	      inform (&iloc, "add parentheses to declare a variable");
22318 	    }
22319 	}
22320     }
22321   /* The T t(X(), X()) case.  */
22322   else if (warning_at (loc, OPT_Wvexing_parse,
22323 		       "parentheses were disambiguated as a function "
22324 		       "declaration"))
22325     {
22326       gcc_rich_location iloc (loc);
22327       if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22328 	{
22329 	  iloc.add_fixit_replace (get_start (loc), "{");
22330 	  iloc.add_fixit_replace (get_finish (loc), "}");
22331 	  inform (&iloc, "replace parentheses with braces to declare a "
22332 		  "variable");
22333 	}
22334     }
22335 }
22336 
22337 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22338    the form of a deduction guide, tag it as such.  CTOR_DTOR_OR_CONV_P
22339    has the same meaning as in cp_parser_declarator.  */
22340 
22341 static void
cp_parser_maybe_adjust_declarator_for_dguide(cp_parser * parser,cp_decl_specifier_seq * decl_specs,cp_declarator * declarator,int * ctor_dtor_or_conv_p)22342 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22343 					      cp_decl_specifier_seq *decl_specs,
22344 					      cp_declarator *declarator,
22345 					      int *ctor_dtor_or_conv_p)
22346 {
22347   if (cxx_dialect >= cxx17
22348       && *ctor_dtor_or_conv_p <= 0
22349       && !decl_specs->type
22350       && !decl_specs->any_type_specifiers_p
22351       && function_declarator_p (declarator))
22352     {
22353       cp_declarator *id = get_id_declarator (declarator);
22354       tree name = id->u.id.unqualified_name;
22355       parser->scope = id->u.id.qualifying_scope;
22356       tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22357       if (tmpl
22358 	  && (DECL_CLASS_TEMPLATE_P (tmpl)
22359 	      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22360 	{
22361 	  id->u.id.unqualified_name = dguide_name (tmpl);
22362 	  id->u.id.sfk = sfk_deduction_guide;
22363 	  *ctor_dtor_or_conv_p = 1;
22364 	}
22365     }
22366 }
22367 
22368 /* Declarators [gram.dcl.decl] */
22369 
22370 /* Parse an init-declarator.
22371 
22372    init-declarator:
22373      declarator initializer [opt]
22374 
22375    GNU Extension:
22376 
22377    init-declarator:
22378      declarator asm-specification [opt] attributes [opt] initializer [opt]
22379 
22380    function-definition:
22381      decl-specifier-seq [opt] declarator ctor-initializer [opt]
22382        function-body
22383      decl-specifier-seq [opt] declarator function-try-block
22384 
22385    GNU Extension:
22386 
22387    function-definition:
22388      __extension__ function-definition
22389 
22390    TM Extension:
22391 
22392    function-definition:
22393      decl-specifier-seq [opt] declarator function-transaction-block
22394 
22395    The parser flags FLAGS is used to control type-specifier parsing.
22396 
22397    The DECL_SPECIFIERS apply to this declarator.  Returns a
22398    representation of the entity declared.  If MEMBER_P is TRUE, then
22399    this declarator appears in a class scope.  The new DECL created by
22400    this declarator is returned.
22401 
22402    The CHECKS are access checks that should be performed once we know
22403    what entity is being declared (and, therefore, what classes have
22404    befriended it).
22405 
22406    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22407    for a function-definition here as well.  If the declarator is a
22408    declarator for a function-definition, *FUNCTION_DEFINITION_P will
22409    be TRUE upon return.  By that point, the function-definition will
22410    have been completely parsed.
22411 
22412    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22413    is FALSE.
22414 
22415    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22416    parsed declaration if it is an uninitialized single declarator not followed
22417    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22418    if present, will not be consumed.  If returned, this declarator will be
22419    created with SD_INITIALIZED but will not call cp_finish_decl.
22420 
22421    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22422    and there is an initializer, the pointed location_t is set to the
22423    location of the '=' or `(', or '{' in C++11 token introducing the
22424    initializer.  */
22425 
22426 static tree
cp_parser_init_declarator(cp_parser * parser,cp_parser_flags flags,cp_decl_specifier_seq * decl_specifiers,vec<deferred_access_check,va_gc> * checks,bool function_definition_allowed_p,bool member_p,int declares_class_or_enum,bool * function_definition_p,tree * maybe_range_for_decl,location_t * init_loc,tree * auto_result)22427 cp_parser_init_declarator (cp_parser* parser,
22428 			   cp_parser_flags flags,
22429 			   cp_decl_specifier_seq *decl_specifiers,
22430 			   vec<deferred_access_check, va_gc> *checks,
22431 			   bool function_definition_allowed_p,
22432 			   bool member_p,
22433 			   int declares_class_or_enum,
22434 			   bool* function_definition_p,
22435 			   tree* maybe_range_for_decl,
22436 			   location_t* init_loc,
22437 			   tree* auto_result)
22438 {
22439   cp_token *token = NULL, *asm_spec_start_token = NULL,
22440            *attributes_start_token = NULL;
22441   cp_declarator *declarator;
22442   tree prefix_attributes;
22443   tree attributes = NULL;
22444   tree asm_specification;
22445   tree initializer;
22446   tree decl = NULL_TREE;
22447   tree scope;
22448   int is_initialized;
22449   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
22450      initialized with "= ..", CPP_OPEN_PAREN if initialized with
22451      "(...)".  */
22452   enum cpp_ttype initialization_kind;
22453   bool is_direct_init = false;
22454   bool is_non_constant_init;
22455   int ctor_dtor_or_conv_p;
22456   bool friend_p = cp_parser_friend_p (decl_specifiers);
22457   bool static_p = decl_specifiers->storage_class == sc_static;
22458   tree pushed_scope = NULL_TREE;
22459   bool range_for_decl_p = false;
22460   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22461   location_t tmp_init_loc = UNKNOWN_LOCATION;
22462 
22463   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22464     flags |= CP_PARSER_FLAGS_CONSTEVAL;
22465 
22466   /* Assume that this is not the declarator for a function
22467      definition.  */
22468   if (function_definition_p)
22469     *function_definition_p = false;
22470 
22471   /* Default arguments are only permitted for function parameters.  */
22472   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22473     parser->default_arg_ok_p = false;
22474 
22475   /* Defer access checks while parsing the declarator; we cannot know
22476      what names are accessible until we know what is being
22477      declared.  */
22478   resume_deferring_access_checks ();
22479 
22480   token = cp_lexer_peek_token (parser->lexer);
22481 
22482   /* Parse the declarator.  */
22483   declarator
22484     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22485 			    flags, &ctor_dtor_or_conv_p,
22486 			    /*parenthesized_p=*/NULL,
22487 			    member_p, friend_p, static_p);
22488   /* Gather up the deferred checks.  */
22489   stop_deferring_access_checks ();
22490 
22491   parser->default_arg_ok_p = saved_default_arg_ok_p;
22492 
22493   /* If the DECLARATOR was erroneous, there's no need to go
22494      further.  */
22495   if (declarator == cp_error_declarator)
22496     return error_mark_node;
22497 
22498   /* Check that the number of template-parameter-lists is OK.  */
22499   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22500 						       token->location))
22501     return error_mark_node;
22502 
22503   if (declares_class_or_enum & 2)
22504     cp_parser_check_for_definition_in_return_type (declarator,
22505 						   decl_specifiers->type,
22506 						   decl_specifiers->locations[ds_type_spec]);
22507 
22508   /* Figure out what scope the entity declared by the DECLARATOR is
22509      located in.  `grokdeclarator' sometimes changes the scope, so
22510      we compute it now.  */
22511   scope = get_scope_of_declarator (declarator);
22512 
22513   /* Perform any lookups in the declared type which were thought to be
22514      dependent, but are not in the scope of the declarator.  */
22515   decl_specifiers->type
22516     = maybe_update_decl_type (decl_specifiers->type, scope);
22517 
22518   /* If we're allowing GNU extensions, look for an
22519      asm-specification.  */
22520   if (cp_parser_allow_gnu_extensions_p (parser))
22521     {
22522       /* Look for an asm-specification.  */
22523       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22524       asm_specification = cp_parser_asm_specification_opt (parser);
22525     }
22526   else
22527     asm_specification = NULL_TREE;
22528 
22529   /* Gather the attributes that were provided with the
22530      decl-specifiers.  */
22531   prefix_attributes = decl_specifiers->attributes;
22532 
22533   /* Look for attributes.  */
22534   attributes_start_token = cp_lexer_peek_token (parser->lexer);
22535   attributes = cp_parser_attributes_opt (parser);
22536 
22537   /* Peek at the next token.  */
22538   token = cp_lexer_peek_token (parser->lexer);
22539 
22540   bool bogus_implicit_tmpl = false;
22541 
22542   if (function_declarator_p (declarator))
22543     {
22544       /* Handle C++17 deduction guides.  Note that class-scope
22545 	 non-template deduction guides are instead handled in
22546 	 cp_parser_member_declaration.  */
22547       cp_parser_maybe_adjust_declarator_for_dguide (parser,
22548 						    decl_specifiers,
22549 						    declarator,
22550 						    &ctor_dtor_or_conv_p);
22551 
22552       if (!member_p && !cp_parser_error_occurred (parser))
22553 	warn_about_ambiguous_parse (decl_specifiers, declarator);
22554 
22555       /* Check to see if the token indicates the start of a
22556 	 function-definition.  */
22557       if (cp_parser_token_starts_function_definition_p (token))
22558 	{
22559 	  if (!function_definition_allowed_p)
22560 	    {
22561 	      /* If a function-definition should not appear here, issue an
22562 		 error message.  */
22563 	      cp_parser_error (parser,
22564 			       "a function-definition is not allowed here");
22565 	      return error_mark_node;
22566 	    }
22567 
22568 	  location_t func_brace_location
22569 	    = cp_lexer_peek_token (parser->lexer)->location;
22570 
22571 	  /* Neither attributes nor an asm-specification are allowed
22572 	     on a function-definition.  */
22573 	  if (asm_specification)
22574 	    error_at (asm_spec_start_token->location,
22575 		      "an %<asm%> specification is not allowed "
22576 		      "on a function-definition");
22577 	  if (attributes)
22578 	    error_at (attributes_start_token->location,
22579 		      "attributes are not allowed "
22580 		      "on a function-definition");
22581 	  /* This is a function-definition.  */
22582 	  *function_definition_p = true;
22583 
22584 	  /* Parse the function definition.  */
22585 	  if (member_p)
22586 	    decl = cp_parser_save_member_function_body (parser,
22587 							decl_specifiers,
22588 							declarator,
22589 							prefix_attributes);
22590 	  else
22591 	    decl =
22592 	      (cp_parser_function_definition_from_specifiers_and_declarator
22593 	       (parser, decl_specifiers, prefix_attributes, declarator));
22594 
22595 	  if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22596 	    {
22597 	      /* This is where the prologue starts...  */
22598 	      DECL_STRUCT_FUNCTION (decl)->function_start_locus
22599 		= func_brace_location;
22600 	    }
22601 
22602 	  return decl;
22603 	}
22604     }
22605   else if (parser->fully_implicit_function_template_p)
22606     {
22607       /* A non-template declaration involving a function parameter list
22608 	 containing an implicit template parameter will be made into a
22609 	 template.  If the resulting declaration is not going to be an
22610 	 actual function then finish the template scope here to prevent it.
22611 	 An error message will be issued once we have a decl to talk about.
22612 
22613          FIXME probably we should do type deduction rather than create an
22614          implicit template, but the standard currently doesn't allow it. */
22615       bogus_implicit_tmpl = true;
22616       finish_fully_implicit_template (parser, NULL_TREE);
22617     }
22618 
22619   /* [dcl.dcl]
22620 
22621      Only in function declarations for constructors, destructors, type
22622      conversions, and deduction guides can the decl-specifier-seq be omitted.
22623 
22624      We explicitly postpone this check past the point where we handle
22625      function-definitions because we tolerate function-definitions
22626      that are missing their return types in some modes.  */
22627   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22628     {
22629       cp_parser_error (parser,
22630 		       "expected constructor, destructor, or type conversion");
22631       return error_mark_node;
22632     }
22633 
22634   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
22635   if (token->type == CPP_EQ
22636       || token->type == CPP_OPEN_PAREN
22637       || token->type == CPP_OPEN_BRACE)
22638     {
22639       is_initialized = SD_INITIALIZED;
22640       initialization_kind = token->type;
22641       declarator->init_loc = token->location;
22642       if (maybe_range_for_decl)
22643 	*maybe_range_for_decl = error_mark_node;
22644       tmp_init_loc = token->location;
22645       if (init_loc && *init_loc == UNKNOWN_LOCATION)
22646 	*init_loc = tmp_init_loc;
22647 
22648       if (token->type == CPP_EQ
22649 	  && function_declarator_p (declarator))
22650 	{
22651 	  cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22652 	  if (t2->keyword == RID_DEFAULT)
22653 	    is_initialized = SD_DEFAULTED;
22654 	  else if (t2->keyword == RID_DELETE)
22655 	    is_initialized = SD_DELETED;
22656 	}
22657     }
22658   else
22659     {
22660       /* If the init-declarator isn't initialized and isn't followed by a
22661 	 `,' or `;', it's not a valid init-declarator.  */
22662       if (token->type != CPP_COMMA
22663 	  && token->type != CPP_SEMICOLON)
22664 	{
22665 	  if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22666 	    range_for_decl_p = true;
22667 	  else
22668 	    {
22669 	      if (!maybe_range_for_decl)
22670 		cp_parser_error (parser, "expected initializer");
22671 	      return error_mark_node;
22672 	    }
22673 	}
22674       is_initialized = SD_UNINITIALIZED;
22675       initialization_kind = CPP_EOF;
22676     }
22677 
22678   /* Because start_decl has side-effects, we should only call it if we
22679      know we're going ahead.  By this point, we know that we cannot
22680      possibly be looking at any other construct.  */
22681   cp_parser_commit_to_tentative_parse (parser);
22682 
22683   /* Enter the newly declared entry in the symbol table.  If we're
22684      processing a declaration in a class-specifier, we wait until
22685      after processing the initializer.  */
22686   if (!member_p)
22687     {
22688       if (parser->in_unbraced_linkage_specification_p)
22689 	decl_specifiers->storage_class = sc_extern;
22690       decl = start_decl (declarator, decl_specifiers,
22691 			 range_for_decl_p? SD_INITIALIZED : is_initialized,
22692 			 attributes, prefix_attributes, &pushed_scope);
22693       cp_finalize_omp_declare_simd (parser, decl);
22694       cp_finalize_oacc_routine (parser, decl, false);
22695       /* Adjust location of decl if declarator->id_loc is more appropriate:
22696 	 set, and decl wasn't merged with another decl, in which case its
22697 	 location would be different from input_location, and more accurate.  */
22698       if (DECL_P (decl)
22699 	  && declarator->id_loc != UNKNOWN_LOCATION
22700 	  && DECL_SOURCE_LOCATION (decl) == input_location)
22701 	DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22702     }
22703   else if (scope)
22704     /* Enter the SCOPE.  That way unqualified names appearing in the
22705        initializer will be looked up in SCOPE.  */
22706     pushed_scope = push_scope (scope);
22707 
22708   /* Perform deferred access control checks, now that we know in which
22709      SCOPE the declared entity resides.  */
22710   if (!member_p && decl)
22711     {
22712       tree saved_current_function_decl = NULL_TREE;
22713 
22714       /* If the entity being declared is a function, pretend that we
22715 	 are in its scope.  If it is a `friend', it may have access to
22716 	 things that would not otherwise be accessible.  */
22717       if (TREE_CODE (decl) == FUNCTION_DECL)
22718 	{
22719 	  saved_current_function_decl = current_function_decl;
22720 	  current_function_decl = decl;
22721 	}
22722 
22723       /* Perform access checks for template parameters.  */
22724       cp_parser_perform_template_parameter_access_checks (checks);
22725 
22726       /* Perform the access control checks for the declarator and the
22727 	 decl-specifiers.  */
22728       perform_deferred_access_checks (tf_warning_or_error);
22729 
22730       /* Restore the saved value.  */
22731       if (TREE_CODE (decl) == FUNCTION_DECL)
22732 	current_function_decl = saved_current_function_decl;
22733     }
22734 
22735   /* Parse the initializer.  */
22736   initializer = NULL_TREE;
22737   is_direct_init = false;
22738   is_non_constant_init = true;
22739   if (is_initialized)
22740     {
22741       if (function_declarator_p (declarator))
22742 	{
22743 	   if (initialization_kind == CPP_EQ)
22744 	     initializer = cp_parser_pure_specifier (parser);
22745 	   else
22746 	     {
22747 	       /* If the declaration was erroneous, we don't really
22748 		  know what the user intended, so just silently
22749 		  consume the initializer.  */
22750 	       if (decl != error_mark_node)
22751 		 error_at (tmp_init_loc, "initializer provided for function");
22752 	       cp_parser_skip_to_closing_parenthesis (parser,
22753 						      /*recovering=*/true,
22754 						      /*or_comma=*/false,
22755 						      /*consume_paren=*/true);
22756 	     }
22757 	}
22758       else
22759 	{
22760 	  /* We want to record the extra mangling scope for in-class
22761 	     initializers of class members and initializers of static
22762 	     data member templates and namespace-scope initializers.
22763 	     The former involves deferring parsing of the initializer
22764 	     until end of class as with default arguments.  So right
22765 	     here we only handle the latter two.  */
22766 	  bool has_lambda_scope = false;
22767 
22768 	  if (decl != error_mark_node
22769 	      && !member_p
22770 	      && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
22771 	    has_lambda_scope = true;
22772 
22773 	  if (has_lambda_scope)
22774 	    start_lambda_scope (decl);
22775 	  initializer = cp_parser_initializer (parser,
22776 					       &is_direct_init,
22777 					       &is_non_constant_init);
22778 	  if (has_lambda_scope)
22779 	    finish_lambda_scope ();
22780 	  if (initializer == error_mark_node)
22781 	    cp_parser_skip_to_end_of_statement (parser);
22782 	}
22783     }
22784 
22785   /* The old parser allows attributes to appear after a parenthesized
22786      initializer.  Mark Mitchell proposed removing this functionality
22787      on the GCC mailing lists on 2002-08-13.  This parser accepts the
22788      attributes -- but ignores them.  Made a permerror in GCC 8.  */
22789   if (cp_parser_allow_gnu_extensions_p (parser)
22790       && initialization_kind == CPP_OPEN_PAREN
22791       && cp_parser_attributes_opt (parser)
22792       && permerror (input_location,
22793 		    "attributes after parenthesized initializer ignored"))
22794     {
22795       static bool hint;
22796       if (flag_permissive && !hint)
22797 	{
22798 	  hint = true;
22799 	  inform (input_location,
22800 		  "this flexibility is deprecated and will be removed");
22801 	}
22802     }
22803 
22804   /* And now complain about a non-function implicit template.  */
22805   if (bogus_implicit_tmpl && decl != error_mark_node)
22806     error_at (DECL_SOURCE_LOCATION (decl),
22807 	      "non-function %qD declared as implicit template", decl);
22808 
22809   /* For an in-class declaration, use `grokfield' to create the
22810      declaration.  */
22811   if (member_p)
22812     {
22813       if (pushed_scope)
22814 	{
22815 	  pop_scope (pushed_scope);
22816 	  pushed_scope = NULL_TREE;
22817 	}
22818       decl = grokfield (declarator, decl_specifiers,
22819 			initializer, !is_non_constant_init,
22820 			/*asmspec=*/NULL_TREE,
22821 			attr_chainon (attributes, prefix_attributes));
22822       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
22823 	cp_parser_save_default_args (parser, decl);
22824       cp_finalize_omp_declare_simd (parser, decl);
22825       cp_finalize_oacc_routine (parser, decl, false);
22826     }
22827 
22828   /* Finish processing the declaration.  But, skip member
22829      declarations.  */
22830   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
22831     {
22832       cp_finish_decl (decl,
22833 		      initializer, !is_non_constant_init,
22834 		      asm_specification,
22835 		      /* If the initializer is in parentheses, then this is
22836 			 a direct-initialization, which means that an
22837 			 `explicit' constructor is OK.  Otherwise, an
22838 			 `explicit' constructor cannot be used.  */
22839 		      ((is_direct_init || !is_initialized)
22840 		       ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
22841     }
22842   else if ((cxx_dialect != cxx98) && friend_p
22843 	   && decl && TREE_CODE (decl) == FUNCTION_DECL)
22844     /* Core issue #226 (C++0x only): A default template-argument
22845        shall not be specified in a friend class template
22846        declaration. */
22847     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
22848                              /*is_partial=*/false, /*is_friend_decl=*/1);
22849 
22850   if (!friend_p && pushed_scope)
22851     pop_scope (pushed_scope);
22852 
22853   if (function_declarator_p (declarator)
22854       && parser->fully_implicit_function_template_p)
22855     {
22856       if (member_p)
22857 	decl = finish_fully_implicit_template (parser, decl);
22858       else
22859 	finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22860     }
22861 
22862   if (auto_result && is_initialized && decl_specifiers->type
22863       && type_uses_auto (decl_specifiers->type))
22864     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
22865 
22866   return decl;
22867 }
22868 
22869 /* Parse a declarator.
22870 
22871    declarator:
22872      direct-declarator
22873      ptr-operator declarator
22874 
22875    abstract-declarator:
22876      ptr-operator abstract-declarator [opt]
22877      direct-abstract-declarator
22878 
22879    GNU Extensions:
22880 
22881    declarator:
22882      attributes [opt] direct-declarator
22883      attributes [opt] ptr-operator declarator
22884 
22885    abstract-declarator:
22886      attributes [opt] ptr-operator abstract-declarator [opt]
22887      attributes [opt] direct-abstract-declarator
22888 
22889    The parser flags FLAGS is used to control type-specifier parsing.
22890 
22891    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
22892    detect constructors, destructors, deduction guides, or conversion operators.
22893    It is set to -1 if the declarator is a name, and +1 if it is a
22894    function. Otherwise it is set to zero. Usually you just want to
22895    test for >0, but internally the negative value is used.
22896 
22897    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
22898    a decl-specifier-seq unless it declares a constructor, destructor,
22899    or conversion.  It might seem that we could check this condition in
22900    semantic analysis, rather than parsing, but that makes it difficult
22901    to handle something like `f()'.  We want to notice that there are
22902    no decl-specifiers, and therefore realize that this is an
22903    expression, not a declaration.)
22904 
22905    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
22906    the declarator is a direct-declarator of the form "(...)".
22907 
22908    MEMBER_P is true iff this declarator is a member-declarator.
22909 
22910    FRIEND_P is true iff this declarator is a friend.
22911 
22912    STATIC_P is true iff the keyword static was seen.  */
22913 
22914 static cp_declarator *
cp_parser_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,cp_parser_flags flags,int * ctor_dtor_or_conv_p,bool * parenthesized_p,bool member_p,bool friend_p,bool static_p)22915 cp_parser_declarator (cp_parser* parser,
22916 		      cp_parser_declarator_kind dcl_kind,
22917 		      cp_parser_flags flags,
22918 		      int* ctor_dtor_or_conv_p,
22919 		      bool* parenthesized_p,
22920 		      bool member_p, bool friend_p, bool static_p)
22921 {
22922   cp_declarator *declarator;
22923   enum tree_code code;
22924   cp_cv_quals cv_quals;
22925   tree class_type;
22926   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
22927 
22928   /* Assume this is not a constructor, destructor, or type-conversion
22929      operator.  */
22930   if (ctor_dtor_or_conv_p)
22931     *ctor_dtor_or_conv_p = 0;
22932 
22933   if (cp_parser_allow_gnu_extensions_p (parser))
22934     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
22935 
22936   /* Check for the ptr-operator production.  */
22937   cp_parser_parse_tentatively (parser);
22938   /* Parse the ptr-operator.  */
22939   code = cp_parser_ptr_operator (parser,
22940 				 &class_type,
22941 				 &cv_quals,
22942 				 &std_attributes);
22943 
22944   /* If that worked, then we have a ptr-operator.  */
22945   if (cp_parser_parse_definitely (parser))
22946     {
22947       /* If a ptr-operator was found, then this declarator was not
22948 	 parenthesized.  */
22949       if (parenthesized_p)
22950 	*parenthesized_p = false;
22951       /* The dependent declarator is optional if we are parsing an
22952 	 abstract-declarator.  */
22953       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
22954 	cp_parser_parse_tentatively (parser);
22955 
22956       /* Parse the dependent declarator.  */
22957       declarator = cp_parser_declarator (parser, dcl_kind, flags,
22958 					 /*ctor_dtor_or_conv_p=*/NULL,
22959 					 /*parenthesized_p=*/NULL,
22960 					 member_p, friend_p, static_p);
22961 
22962       /* If we are parsing an abstract-declarator, we must handle the
22963 	 case where the dependent declarator is absent.  */
22964       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
22965 	  && !cp_parser_parse_definitely (parser))
22966 	declarator = NULL;
22967 
22968       declarator = cp_parser_make_indirect_declarator
22969 	(code, class_type, cv_quals, declarator, std_attributes);
22970     }
22971   /* Everything else is a direct-declarator.  */
22972   else
22973     {
22974       if (parenthesized_p)
22975 	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
22976 						   CPP_OPEN_PAREN);
22977       declarator = cp_parser_direct_declarator (parser, dcl_kind,
22978 						flags, ctor_dtor_or_conv_p,
22979 						member_p, friend_p, static_p);
22980     }
22981 
22982   if (gnu_attributes && declarator && declarator != cp_error_declarator)
22983     declarator->attributes = gnu_attributes;
22984   return declarator;
22985 }
22986 
22987 /* Parse a direct-declarator or direct-abstract-declarator.
22988 
22989    direct-declarator:
22990      declarator-id
22991      direct-declarator ( parameter-declaration-clause )
22992        cv-qualifier-seq [opt]
22993        ref-qualifier [opt]
22994        exception-specification [opt]
22995      direct-declarator [ constant-expression [opt] ]
22996      ( declarator )
22997 
22998    direct-abstract-declarator:
22999      direct-abstract-declarator [opt]
23000        ( parameter-declaration-clause )
23001        cv-qualifier-seq [opt]
23002        ref-qualifier [opt]
23003        exception-specification [opt]
23004      direct-abstract-declarator [opt] [ constant-expression [opt] ]
23005      ( abstract-declarator )
23006 
23007    Returns a representation of the declarator.  DCL_KIND is
23008    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23009    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
23010    we are parsing a direct-declarator.  It is
23011    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23012    of ambiguity we prefer an abstract declarator, as per
23013    [dcl.ambig.res].
23014    The parser flags FLAGS is used to control type-specifier parsing.
23015    CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23016    as for cp_parser_declarator.  */
23017 
23018 static cp_declarator *
cp_parser_direct_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,cp_parser_flags flags,int * ctor_dtor_or_conv_p,bool member_p,bool friend_p,bool static_p)23019 cp_parser_direct_declarator (cp_parser* parser,
23020 			     cp_parser_declarator_kind dcl_kind,
23021 			     cp_parser_flags flags,
23022 			     int* ctor_dtor_or_conv_p,
23023 			     bool member_p, bool friend_p, bool static_p)
23024 {
23025   cp_token *token;
23026   cp_declarator *declarator = NULL;
23027   tree scope = NULL_TREE;
23028   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23029   bool saved_in_declarator_p = parser->in_declarator_p;
23030   bool first = true;
23031   tree pushed_scope = NULL_TREE;
23032   cp_token *open_paren = NULL, *close_paren = NULL;
23033 
23034   while (true)
23035     {
23036       /* Peek at the next token.  */
23037       token = cp_lexer_peek_token (parser->lexer);
23038       if (token->type == CPP_OPEN_PAREN)
23039 	{
23040 	  /* This is either a parameter-declaration-clause, or a
23041 	     parenthesized declarator. When we know we are parsing a
23042 	     named declarator, it must be a parenthesized declarator
23043 	     if FIRST is true. For instance, `(int)' is a
23044 	     parameter-declaration-clause, with an omitted
23045 	     direct-abstract-declarator. But `((*))', is a
23046 	     parenthesized abstract declarator. Finally, when T is a
23047 	     template parameter `(T)' is a
23048 	     parameter-declaration-clause, and not a parenthesized
23049 	     named declarator.
23050 
23051 	     We first try and parse a parameter-declaration-clause,
23052 	     and then try a nested declarator (if FIRST is true).
23053 
23054 	     It is not an error for it not to be a
23055 	     parameter-declaration-clause, even when FIRST is
23056 	     false. Consider,
23057 
23058 	       int i (int);
23059 	       int i (3);
23060 
23061 	     The first is the declaration of a function while the
23062 	     second is the definition of a variable, including its
23063 	     initializer.
23064 
23065 	     Having seen only the parenthesis, we cannot know which of
23066 	     these two alternatives should be selected.  Even more
23067 	     complex are examples like:
23068 
23069 	       int i (int (a));
23070 	       int i (int (3));
23071 
23072 	     The former is a function-declaration; the latter is a
23073 	     variable initialization.
23074 
23075 	     Thus again, we try a parameter-declaration-clause, and if
23076 	     that fails, we back out and return.  */
23077 
23078 	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23079 	    {
23080 	      tree params;
23081 	      bool is_declarator = false;
23082 
23083 	      open_paren = NULL;
23084 
23085 	      /* In a member-declarator, the only valid interpretation
23086 		 of a parenthesis is the start of a
23087 		 parameter-declaration-clause.  (It is invalid to
23088 		 initialize a static data member with a parenthesized
23089 		 initializer; only the "=" form of initialization is
23090 		 permitted.)  */
23091 	      if (!member_p)
23092 		cp_parser_parse_tentatively (parser);
23093 
23094 	      /* Consume the `('.  */
23095 	      const location_t parens_start = token->location;
23096 	      matching_parens parens;
23097 	      parens.consume_open (parser);
23098 	      if (first)
23099 		{
23100 		  /* If this is going to be an abstract declarator, we're
23101 		     in a declarator and we can't have default args.  */
23102 		  parser->default_arg_ok_p = false;
23103 		  parser->in_declarator_p = true;
23104 		}
23105 
23106 	      begin_scope (sk_function_parms, NULL_TREE);
23107 
23108 	      /* Signal we are in the immediate function context.  */
23109 	      if (flags & CP_PARSER_FLAGS_CONSTEVAL)
23110 		current_binding_level->immediate_fn_ctx_p = true;
23111 
23112 	      /* Parse the parameter-declaration-clause.  */
23113 	      params
23114 		= cp_parser_parameter_declaration_clause (parser, flags);
23115 	      const location_t parens_end
23116 		= cp_lexer_peek_token (parser->lexer)->location;
23117 
23118 	      /* Consume the `)'.  */
23119 	      parens.require_close (parser);
23120 
23121 	      /* If all went well, parse the cv-qualifier-seq,
23122 		 ref-qualifier and the exception-specification.  */
23123 	      if (member_p || cp_parser_parse_definitely (parser))
23124 		{
23125 		  cp_cv_quals cv_quals;
23126 		  cp_virt_specifiers virt_specifiers;
23127 		  cp_ref_qualifier ref_qual;
23128 		  tree exception_specification;
23129 		  tree late_return;
23130 		  tree attrs;
23131 		  bool memfn = (member_p || (pushed_scope
23132 					     && CLASS_TYPE_P (pushed_scope)));
23133 		  unsigned char local_variables_forbidden_p
23134 		    = parser->local_variables_forbidden_p;
23135 		  /* 'this' is not allowed in static member functions.  */
23136 		  if (static_p || friend_p)
23137 		    parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23138 
23139 		  is_declarator = true;
23140 
23141 		  if (ctor_dtor_or_conv_p)
23142 		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23143 		  first = false;
23144 
23145 		  /* Parse the cv-qualifier-seq.  */
23146 		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23147 		  /* Parse the ref-qualifier. */
23148 		  ref_qual = cp_parser_ref_qualifier_opt (parser);
23149 		  /* Parse the tx-qualifier.  */
23150 		  tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23151 
23152 		  tree save_ccp = current_class_ptr;
23153 		  tree save_ccr = current_class_ref;
23154 		  if (memfn && !friend_p && !static_p)
23155 		    /* DR 1207: 'this' is in scope after the cv-quals.  */
23156 		    inject_this_parameter (current_class_type, cv_quals);
23157 
23158 		  /* If it turned out that this is e.g. a pointer to a
23159 		     function, we don't want to delay noexcept parsing.  */
23160 		  if (declarator == NULL || declarator->kind != cdk_id)
23161 		    flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23162 
23163 		  /* Parse the exception-specification.  */
23164 		  exception_specification
23165 		    = cp_parser_exception_specification_opt (parser,
23166 							     flags);
23167 
23168 		  attrs = cp_parser_std_attribute_spec_seq (parser);
23169 
23170 		  cp_omp_declare_simd_data odsd;
23171 		  if ((flag_openmp || flag_openmp_simd)
23172 		      && declarator
23173 		      && declarator->std_attributes
23174 		      && declarator->kind == cdk_id)
23175 		    {
23176 		      tree *pa = &declarator->std_attributes;
23177 		      cp_parser_handle_directive_omp_attributes (parser, pa,
23178 								 &odsd, false);
23179 		    }
23180 
23181 		  /* In here, we handle cases where attribute is used after
23182 		     the function declaration.  For example:
23183 		     void func (int x) __attribute__((vector(..)));  */
23184 		  tree gnu_attrs = NULL_TREE;
23185 		  tree requires_clause = NULL_TREE;
23186 		  late_return
23187 		    = cp_parser_late_return_type_opt (parser, declarator,
23188 						      requires_clause);
23189 
23190 		  cp_finalize_omp_declare_simd (parser, &odsd);
23191 
23192 		  /* Parse the virt-specifier-seq.  */
23193 		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23194 
23195 		  location_t parens_loc = make_location (parens_start,
23196 							 parens_start,
23197 							 parens_end);
23198 		  /* Create the function-declarator.  */
23199 		  declarator = make_call_declarator (declarator,
23200 						     params,
23201 						     cv_quals,
23202 						     virt_specifiers,
23203 						     ref_qual,
23204 						     tx_qual,
23205 						     exception_specification,
23206 						     late_return,
23207 						     requires_clause,
23208 						     parens_loc);
23209 		  declarator->std_attributes = attrs;
23210 		  declarator->attributes = gnu_attrs;
23211 		  /* Any subsequent parameter lists are to do with
23212 		     return type, so are not those of the declared
23213 		     function.  */
23214 		  parser->default_arg_ok_p = false;
23215 
23216 		  current_class_ptr = save_ccp;
23217 		  current_class_ref = save_ccr;
23218 
23219 		  /* Restore the state of local_variables_forbidden_p.  */
23220 		  parser->local_variables_forbidden_p
23221 		    = local_variables_forbidden_p;
23222 		}
23223 
23224 	      /* Remove the function parms from scope.  */
23225 	      pop_bindings_and_leave_scope ();
23226 
23227 	      if (is_declarator)
23228 		/* Repeat the main loop.  */
23229 		continue;
23230 	    }
23231 
23232 	  /* If this is the first, we can try a parenthesized
23233 	     declarator.  */
23234 	  if (first)
23235 	    {
23236 	      bool saved_in_type_id_in_expr_p;
23237 
23238 	      parser->default_arg_ok_p = saved_default_arg_ok_p;
23239 	      parser->in_declarator_p = saved_in_declarator_p;
23240 
23241 	      open_paren = token;
23242 	      /* Consume the `('.  */
23243 	      matching_parens parens;
23244 	      parens.consume_open (parser);
23245 	      /* Parse the nested declarator.  */
23246 	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23247 	      parser->in_type_id_in_expr_p = true;
23248 	      declarator
23249 		= cp_parser_declarator (parser, dcl_kind, flags,
23250 					ctor_dtor_or_conv_p,
23251 					/*parenthesized_p=*/NULL,
23252 					member_p, friend_p,
23253 					/*static_p=*/false);
23254 	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23255 	      first = false;
23256 	      /* Expect a `)'.  */
23257 	      close_paren = cp_lexer_peek_token (parser->lexer);
23258 	      if (!parens.require_close (parser))
23259 		declarator = cp_error_declarator;
23260 	      if (declarator == cp_error_declarator)
23261 		break;
23262 
23263 	      goto handle_declarator;
23264 	    }
23265 	  /* Otherwise, we must be done.  */
23266 	  else
23267 	    break;
23268 	}
23269       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23270 	       && token->type == CPP_OPEN_SQUARE
23271 	       && !cp_next_tokens_can_be_attribute_p (parser))
23272 	{
23273 	  /* Parse an array-declarator.  */
23274 	  tree bounds, attrs;
23275 
23276 	  if (ctor_dtor_or_conv_p)
23277 	    *ctor_dtor_or_conv_p = 0;
23278 
23279 	  open_paren = NULL;
23280 	  first = false;
23281 	  parser->default_arg_ok_p = false;
23282 	  parser->in_declarator_p = true;
23283 	  /* Consume the `['.  */
23284 	  cp_lexer_consume_token (parser->lexer);
23285 	  /* Peek at the next token.  */
23286 	  token = cp_lexer_peek_token (parser->lexer);
23287 	  /* If the next token is `]', then there is no
23288 	     constant-expression.  */
23289 	  if (token->type != CPP_CLOSE_SQUARE)
23290 	    {
23291 	      bool non_constant_p;
23292 	      bounds
23293 		= cp_parser_constant_expression (parser,
23294 						 /*allow_non_constant=*/true,
23295 						 &non_constant_p);
23296 	      if (!non_constant_p)
23297 		/* OK */;
23298 	      else if (error_operand_p (bounds))
23299 		/* Already gave an error.  */;
23300 	      else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23301 		/* Let compute_array_index_type diagnose this.  */;
23302 	      else if (!parser->in_function_body
23303 		       || parsing_function_declarator ())
23304 		{
23305 		  /* Normally, the array bound must be an integral constant
23306 		     expression.  However, as an extension, we allow VLAs
23307 		     in function scopes as long as they aren't part of a
23308 		     parameter declaration.  */
23309 		  cp_parser_error (parser,
23310 				   "array bound is not an integer constant");
23311 		  bounds = error_mark_node;
23312 		}
23313 	      else if (processing_template_decl
23314 		       && !type_dependent_expression_p (bounds))
23315 		{
23316 		  /* Remember this wasn't a constant-expression.  */
23317 		  bounds = build_nop (TREE_TYPE (bounds), bounds);
23318 		  TREE_SIDE_EFFECTS (bounds) = 1;
23319 		}
23320 	    }
23321 	  else
23322 	    bounds = NULL_TREE;
23323 	  /* Look for the closing `]'.  */
23324 	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23325 	    {
23326 	      declarator = cp_error_declarator;
23327 	      break;
23328 	    }
23329 
23330 	  attrs = cp_parser_std_attribute_spec_seq (parser);
23331 	  declarator = make_array_declarator (declarator, bounds);
23332 	  declarator->std_attributes = attrs;
23333 	}
23334       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23335 	{
23336 	  {
23337 	    tree qualifying_scope;
23338 	    tree unqualified_name;
23339 	    tree attrs;
23340 	    special_function_kind sfk;
23341 	    bool abstract_ok;
23342 	    bool pack_expansion_p = false;
23343 	    cp_token *declarator_id_start_token;
23344 
23345 	    /* Parse a declarator-id */
23346 	    abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23347 	    if (abstract_ok)
23348 	      {
23349 		cp_parser_parse_tentatively (parser);
23350 
23351 		/* If we see an ellipsis, we should be looking at a
23352 		   parameter pack. */
23353 		if (token->type == CPP_ELLIPSIS)
23354 		  {
23355 		    /* Consume the `...' */
23356 		    cp_lexer_consume_token (parser->lexer);
23357 
23358 		    pack_expansion_p = true;
23359 		  }
23360 	      }
23361 
23362 	    declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23363 	    unqualified_name
23364 	      = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23365 	    qualifying_scope = parser->scope;
23366 	    if (abstract_ok)
23367 	      {
23368 		bool okay = false;
23369 
23370 		if (!unqualified_name && pack_expansion_p)
23371 		  {
23372 		    /* Check whether an error occurred. */
23373 		    okay = !cp_parser_error_occurred (parser);
23374 
23375 		    /* We already consumed the ellipsis to mark a
23376 		       parameter pack, but we have no way to report it,
23377 		       so abort the tentative parse. We will be exiting
23378 		       immediately anyway. */
23379 		    cp_parser_abort_tentative_parse (parser);
23380 		  }
23381 		else
23382 		  okay = cp_parser_parse_definitely (parser);
23383 
23384 		if (!okay)
23385 		  unqualified_name = error_mark_node;
23386 		else if (unqualified_name
23387 			 && (qualifying_scope
23388 			     || (!identifier_p (unqualified_name))))
23389 		  {
23390 		    cp_parser_error (parser, "expected unqualified-id");
23391 		    unqualified_name = error_mark_node;
23392 		  }
23393 	      }
23394 
23395 	    if (!unqualified_name)
23396 	      return NULL;
23397 	    if (unqualified_name == error_mark_node)
23398 	      {
23399 		declarator = cp_error_declarator;
23400 		pack_expansion_p = false;
23401 		declarator->parameter_pack_p = false;
23402 		break;
23403 	      }
23404 
23405 	    attrs = cp_parser_std_attribute_spec_seq (parser);
23406 
23407 	    if (qualifying_scope && at_namespace_scope_p ()
23408 		&& TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23409 	      {
23410 		/* In the declaration of a member of a template class
23411 		   outside of the class itself, the SCOPE will sometimes
23412 		   be a TYPENAME_TYPE.  For example, given:
23413 
23414 		   template <typename T>
23415 		   int S<T>::R::i = 3;
23416 
23417 		   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
23418 		   this context, we must resolve S<T>::R to an ordinary
23419 		   type, rather than a typename type.
23420 
23421 		   The reason we normally avoid resolving TYPENAME_TYPEs
23422 		   is that a specialization of `S' might render
23423 		   `S<T>::R' not a type.  However, if `S' is
23424 		   specialized, then this `i' will not be used, so there
23425 		   is no harm in resolving the types here.  */
23426 		tree type;
23427 
23428 		/* Resolve the TYPENAME_TYPE.  */
23429 		type = resolve_typename_type (qualifying_scope,
23430 					      /*only_current_p=*/false);
23431 		/* If that failed, the declarator is invalid.  */
23432 		if (TREE_CODE (type) == TYPENAME_TYPE)
23433 		  {
23434 		    if (typedef_variant_p (type))
23435 		      error_at (declarator_id_start_token->location,
23436 				"cannot define member of dependent typedef "
23437 				"%qT", type);
23438 		    else
23439 		      error_at (declarator_id_start_token->location,
23440 				"%<%T::%E%> is not a type",
23441 				TYPE_CONTEXT (qualifying_scope),
23442 				TYPE_IDENTIFIER (qualifying_scope));
23443 		  }
23444 		qualifying_scope = type;
23445 	      }
23446 
23447 	    sfk = sfk_none;
23448 
23449 	    if (unqualified_name)
23450 	      {
23451 		tree class_type;
23452 
23453 		if (qualifying_scope
23454 		    && CLASS_TYPE_P (qualifying_scope))
23455 		  class_type = qualifying_scope;
23456 		else
23457 		  class_type = current_class_type;
23458 
23459 		if (TREE_CODE (unqualified_name) == TYPE_DECL)
23460 		  {
23461 		    tree name_type = TREE_TYPE (unqualified_name);
23462 
23463 		    if (!class_type || !same_type_p (name_type, class_type))
23464 		      {
23465 			/* We do not attempt to print the declarator
23466 			   here because we do not have enough
23467 			   information about its original syntactic
23468 			   form.  */
23469 			cp_parser_error (parser, "invalid declarator");
23470 			declarator = cp_error_declarator;
23471 			break;
23472 		      }
23473 		    else if (qualifying_scope
23474 			     && CLASSTYPE_USE_TEMPLATE (name_type))
23475 		      {
23476 			error_at (declarator_id_start_token->location,
23477 				  "invalid use of constructor as a template");
23478 			inform (declarator_id_start_token->location,
23479 				"use %<%T::%D%> instead of %<%T::%D%> to "
23480 				"name the constructor in a qualified name",
23481 				class_type,
23482 				DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23483 				class_type, name_type);
23484 			declarator = cp_error_declarator;
23485 			break;
23486 		      }
23487 		    unqualified_name = constructor_name (class_type);
23488 		  }
23489 
23490 		if (class_type)
23491 		  {
23492 		    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23493 		      sfk = sfk_destructor;
23494 		    else if (identifier_p (unqualified_name)
23495 			     && IDENTIFIER_CONV_OP_P (unqualified_name))
23496 		      sfk = sfk_conversion;
23497 		    else if (/* There's no way to declare a constructor
23498 				for an unnamed type, even if the type
23499 				got a name for linkage purposes.  */
23500 			     !TYPE_WAS_UNNAMED (class_type)
23501 			     /* Handle correctly (c++/19200):
23502 
23503 				struct S {
23504 				  struct T{};
23505 				  friend void S(T);
23506 				};
23507 
23508 				and also:
23509 
23510 				namespace N {
23511 				  void S();
23512 				}
23513 
23514 				struct S {
23515 				  friend void N::S();
23516 				};  */
23517 			     && (!friend_p || class_type == qualifying_scope)
23518 			     && constructor_name_p (unqualified_name,
23519 						    class_type))
23520 		      sfk = sfk_constructor;
23521 		    else if (is_overloaded_fn (unqualified_name)
23522 			     && DECL_CONSTRUCTOR_P (get_first_fn
23523 						    (unqualified_name)))
23524 		      sfk = sfk_constructor;
23525 
23526 		    if (ctor_dtor_or_conv_p && sfk != sfk_none)
23527 		      *ctor_dtor_or_conv_p = -1;
23528 		  }
23529 	      }
23530 	    declarator = make_id_declarator (qualifying_scope,
23531 					     unqualified_name,
23532 					     sfk, token->location);
23533 	    declarator->std_attributes = attrs;
23534 	    declarator->parameter_pack_p = pack_expansion_p;
23535 
23536 	    if (pack_expansion_p)
23537 	      maybe_warn_variadic_templates ();
23538 
23539 	    /* We're looking for this case in [temp.res]:
23540 	       A qualified-id is assumed to name a type if [...]
23541 	       - it is a decl-specifier of the decl-specifier-seq of a
23542 		 parameter-declaration in a declarator of a function or
23543 		 function template declaration, ... */
23544 	    if (cxx_dialect >= cxx20
23545 		&& (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23546 		&& declarator->kind == cdk_id
23547 		&& !at_class_scope_p ()
23548 		&& cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23549 	      {
23550 		/* ...whose declarator-id is qualified.  If it isn't, never
23551 		   assume the parameters to refer to types.  */
23552 		if (qualifying_scope == NULL_TREE)
23553 		  flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23554 		else
23555 		  {
23556 		    /* Now we have something like
23557 		       template <typename T> int C::x(S::p);
23558 		       which can be a function template declaration or a
23559 		       variable template definition.  If name lookup for
23560 		       the declarator-id C::x finds one or more function
23561 		       templates, assume S::p to name a type.  Otherwise,
23562 		       don't.  */
23563 		    tree decl
23564 		      = cp_parser_lookup_name (parser, unqualified_name,
23565 					       none_type,
23566 					       /*is_template=*/false,
23567 					       /*is_namespace=*/false,
23568 					       /*check_dependency=*/false,
23569 					       /*ambiguous_decls=*/NULL,
23570 					       token->location);
23571 
23572 		    if (!is_overloaded_fn (decl)
23573 			/* Allow
23574 			   template<typename T>
23575 			   A<T>::A(T::type) { }  */
23576 			&& !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23577 			     && constructor_name_p (unqualified_name,
23578 						    qualifying_scope)))
23579 		      flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23580 		  }
23581 	      }
23582 	  }
23583 
23584 	handle_declarator:;
23585 	  scope = get_scope_of_declarator (declarator);
23586 	  if (scope)
23587 	    {
23588 	      /* Any names that appear after the declarator-id for a
23589 		 member are looked up in the containing scope.  */
23590 	      if (at_function_scope_p ())
23591 		{
23592 		  /* But declarations with qualified-ids can't appear in a
23593 		     function.  */
23594 		  cp_parser_error (parser, "qualified-id in declaration");
23595 		  declarator = cp_error_declarator;
23596 		  break;
23597 		}
23598 	      pushed_scope = push_scope (scope);
23599 	    }
23600 	  parser->in_declarator_p = true;
23601 	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23602 	      || (declarator && declarator->kind == cdk_id))
23603 	    /* Default args are only allowed on function
23604 	       declarations.  */
23605 	    parser->default_arg_ok_p = saved_default_arg_ok_p;
23606 	  else
23607 	    parser->default_arg_ok_p = false;
23608 
23609 	  first = false;
23610 	}
23611       /* We're done.  */
23612       else
23613 	break;
23614     }
23615 
23616   /* For an abstract declarator, we might wind up with nothing at this
23617      point.  That's an error; the declarator is not optional.  */
23618   if (!declarator)
23619     cp_parser_error (parser, "expected declarator");
23620   else if (open_paren)
23621     {
23622       /* Record overly parenthesized declarator so we can give a
23623 	 diagnostic about confusing decl/expr disambiguation.  */
23624       if (declarator->kind == cdk_array)
23625 	{
23626 	  /* If the open and close parens are on different lines, this
23627 	     is probably a formatting thing, so ignore.  */
23628 	  expanded_location open = expand_location (open_paren->location);
23629 	  expanded_location close = expand_location (close_paren->location);
23630 	  if (open.line != close.line || open.file != close.file)
23631 	    open_paren = NULL;
23632 	}
23633       if (open_paren)
23634 	declarator->parenthesized = make_location (open_paren->location,
23635 						   open_paren->location,
23636 						   close_paren->location);
23637     }
23638 
23639   /* If we entered a scope, we must exit it now.  */
23640   if (pushed_scope)
23641     pop_scope (pushed_scope);
23642 
23643   parser->default_arg_ok_p = saved_default_arg_ok_p;
23644   parser->in_declarator_p = saved_in_declarator_p;
23645 
23646   return declarator;
23647 }
23648 
23649 /* Parse a ptr-operator.
23650 
23651    ptr-operator:
23652      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23653      * cv-qualifier-seq [opt]
23654      &
23655      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23656      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23657 
23658    GNU Extension:
23659 
23660    ptr-operator:
23661      & cv-qualifier-seq [opt]
23662 
23663    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23664    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23665    an rvalue reference. In the case of a pointer-to-member, *TYPE is
23666    filled in with the TYPE containing the member.  *CV_QUALS is
23667    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23668    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
23669    Note that the tree codes returned by this function have nothing
23670    to do with the types of trees that will be eventually be created
23671    to represent the pointer or reference type being parsed. They are
23672    just constants with suggestive names. */
23673 static enum tree_code
cp_parser_ptr_operator(cp_parser * parser,tree * type,cp_cv_quals * cv_quals,tree * attributes)23674 cp_parser_ptr_operator (cp_parser* parser,
23675 			tree* type,
23676 			cp_cv_quals *cv_quals,
23677 			tree *attributes)
23678 {
23679   enum tree_code code = ERROR_MARK;
23680   cp_token *token;
23681   tree attrs = NULL_TREE;
23682 
23683   /* Assume that it's not a pointer-to-member.  */
23684   *type = NULL_TREE;
23685   /* And that there are no cv-qualifiers.  */
23686   *cv_quals = TYPE_UNQUALIFIED;
23687 
23688   /* Peek at the next token.  */
23689   token = cp_lexer_peek_token (parser->lexer);
23690 
23691   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
23692   if (token->type == CPP_MULT)
23693     code = INDIRECT_REF;
23694   else if (token->type == CPP_AND)
23695     code = ADDR_EXPR;
23696   else if ((cxx_dialect != cxx98) &&
23697 	   token->type == CPP_AND_AND) /* C++0x only */
23698     code = NON_LVALUE_EXPR;
23699 
23700   if (code != ERROR_MARK)
23701     {
23702       /* Consume the `*', `&' or `&&'.  */
23703       cp_lexer_consume_token (parser->lexer);
23704 
23705       /* A `*' can be followed by a cv-qualifier-seq, and so can a
23706 	 `&', if we are allowing GNU extensions.  (The only qualifier
23707 	 that can legally appear after `&' is `restrict', but that is
23708 	 enforced during semantic analysis.  */
23709       if (code == INDIRECT_REF
23710 	  || cp_parser_allow_gnu_extensions_p (parser))
23711 	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23712 
23713       attrs = cp_parser_std_attribute_spec_seq (parser);
23714       if (attributes != NULL)
23715 	*attributes = attrs;
23716     }
23717   else
23718     {
23719       /* Try the pointer-to-member case.  */
23720       cp_parser_parse_tentatively (parser);
23721       /* Look for the optional `::' operator.  */
23722       cp_parser_global_scope_opt (parser,
23723 				  /*current_scope_valid_p=*/false);
23724       /* Look for the nested-name specifier.  */
23725       token = cp_lexer_peek_token (parser->lexer);
23726       cp_parser_nested_name_specifier (parser,
23727 				       /*typename_keyword_p=*/false,
23728 				       /*check_dependency_p=*/true,
23729 				       /*type_p=*/false,
23730 				       /*is_declaration=*/false);
23731       /* If we found it, and the next token is a `*', then we are
23732 	 indeed looking at a pointer-to-member operator.  */
23733       if (!cp_parser_error_occurred (parser)
23734 	  && cp_parser_require (parser, CPP_MULT, RT_MULT))
23735 	{
23736 	  /* Indicate that the `*' operator was used.  */
23737 	  code = INDIRECT_REF;
23738 
23739 	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23740 	    error_at (token->location, "%qD is a namespace", parser->scope);
23741 	  else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23742 	    error_at (token->location, "cannot form pointer to member of "
23743 		      "non-class %q#T", parser->scope);
23744 	  else
23745 	    {
23746 	      /* The type of which the member is a member is given by the
23747 		 current SCOPE.  */
23748 	      *type = parser->scope;
23749 	      /* The next name will not be qualified.  */
23750 	      parser->scope = NULL_TREE;
23751 	      parser->qualifying_scope = NULL_TREE;
23752 	      parser->object_scope = NULL_TREE;
23753 	      /* Look for optional c++11 attributes.  */
23754 	      attrs = cp_parser_std_attribute_spec_seq (parser);
23755 	      if (attributes != NULL)
23756 		*attributes = attrs;
23757 	      /* Look for the optional cv-qualifier-seq.  */
23758 	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23759 	    }
23760 	}
23761       /* If that didn't work we don't have a ptr-operator.  */
23762       if (!cp_parser_parse_definitely (parser))
23763 	cp_parser_error (parser, "expected ptr-operator");
23764     }
23765 
23766   return code;
23767 }
23768 
23769 /* Parse an (optional) cv-qualifier-seq.
23770 
23771    cv-qualifier-seq:
23772      cv-qualifier cv-qualifier-seq [opt]
23773 
23774    cv-qualifier:
23775      const
23776      volatile
23777 
23778    GNU Extension:
23779 
23780    cv-qualifier:
23781      __restrict__
23782 
23783    Returns a bitmask representing the cv-qualifiers.  */
23784 
23785 static cp_cv_quals
cp_parser_cv_qualifier_seq_opt(cp_parser * parser)23786 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
23787 {
23788   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
23789 
23790   while (true)
23791     {
23792       cp_token *token;
23793       cp_cv_quals cv_qualifier;
23794 
23795       /* Peek at the next token.  */
23796       token = cp_lexer_peek_token (parser->lexer);
23797       /* See if it's a cv-qualifier.  */
23798       switch (token->keyword)
23799 	{
23800 	case RID_CONST:
23801 	  cv_qualifier = TYPE_QUAL_CONST;
23802 	  break;
23803 
23804 	case RID_VOLATILE:
23805 	  cv_qualifier = TYPE_QUAL_VOLATILE;
23806 	  break;
23807 
23808 	case RID_RESTRICT:
23809 	  cv_qualifier = TYPE_QUAL_RESTRICT;
23810 	  break;
23811 
23812 	default:
23813 	  cv_qualifier = TYPE_UNQUALIFIED;
23814 	  break;
23815 	}
23816 
23817       if (!cv_qualifier)
23818 	break;
23819 
23820       if (cv_quals & cv_qualifier)
23821 	{
23822 	  gcc_rich_location richloc (token->location);
23823 	  richloc.add_fixit_remove ();
23824 	  error_at (&richloc, "duplicate cv-qualifier");
23825 	  cp_lexer_purge_token (parser->lexer);
23826 	}
23827       else
23828 	{
23829 	  cp_lexer_consume_token (parser->lexer);
23830 	  cv_quals |= cv_qualifier;
23831 	}
23832     }
23833 
23834   return cv_quals;
23835 }
23836 
23837 /* Parse an (optional) ref-qualifier
23838 
23839    ref-qualifier:
23840      &
23841      &&
23842 
23843    Returns cp_ref_qualifier representing ref-qualifier. */
23844 
23845 static cp_ref_qualifier
cp_parser_ref_qualifier_opt(cp_parser * parser)23846 cp_parser_ref_qualifier_opt (cp_parser* parser)
23847 {
23848   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
23849 
23850   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
23851   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
23852     return ref_qual;
23853 
23854   while (true)
23855     {
23856       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
23857       cp_token *token = cp_lexer_peek_token (parser->lexer);
23858 
23859       switch (token->type)
23860 	{
23861 	case CPP_AND:
23862 	  curr_ref_qual = REF_QUAL_LVALUE;
23863 	  break;
23864 
23865 	case CPP_AND_AND:
23866 	  curr_ref_qual = REF_QUAL_RVALUE;
23867 	  break;
23868 
23869 	default:
23870 	  curr_ref_qual = REF_QUAL_NONE;
23871 	  break;
23872 	}
23873 
23874       if (!curr_ref_qual)
23875 	break;
23876       else if (ref_qual)
23877 	{
23878 	  error_at (token->location, "multiple ref-qualifiers");
23879 	  cp_lexer_purge_token (parser->lexer);
23880 	}
23881       else
23882 	{
23883 	  ref_qual = curr_ref_qual;
23884 	  cp_lexer_consume_token (parser->lexer);
23885 	}
23886     }
23887 
23888   return ref_qual;
23889 }
23890 
23891 /* Parse an optional tx-qualifier.
23892 
23893    tx-qualifier:
23894      transaction_safe
23895      transaction_safe_dynamic  */
23896 
23897 static tree
cp_parser_tx_qualifier_opt(cp_parser * parser)23898 cp_parser_tx_qualifier_opt (cp_parser *parser)
23899 {
23900   cp_token *token = cp_lexer_peek_token (parser->lexer);
23901   if (token->type == CPP_NAME)
23902     {
23903       tree name = token->u.value;
23904       const char *p = IDENTIFIER_POINTER (name);
23905       const int len = strlen ("transaction_safe");
23906       if (startswith (p, "transaction_safe"))
23907 	{
23908 	  p += len;
23909 	  if (*p == '\0'
23910 	      || !strcmp (p, "_dynamic"))
23911 	    {
23912 	      cp_lexer_consume_token (parser->lexer);
23913 	      if (!flag_tm)
23914 		{
23915 		  error ("%qE requires %<-fgnu-tm%>", name);
23916 		  return NULL_TREE;
23917 		}
23918 	      else
23919 		return name;
23920 	    }
23921 	}
23922     }
23923   return NULL_TREE;
23924 }
23925 
23926 /* Parse an (optional) virt-specifier-seq.
23927 
23928    virt-specifier-seq:
23929      virt-specifier virt-specifier-seq [opt]
23930 
23931    virt-specifier:
23932      override
23933      final
23934 
23935    Returns a bitmask representing the virt-specifiers.  */
23936 
23937 static cp_virt_specifiers
cp_parser_virt_specifier_seq_opt(cp_parser * parser)23938 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
23939 {
23940   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23941 
23942   while (true)
23943     {
23944       cp_token *token;
23945       cp_virt_specifiers virt_specifier;
23946 
23947       /* Peek at the next token.  */
23948       token = cp_lexer_peek_token (parser->lexer);
23949       /* See if it's a virt-specifier-qualifier.  */
23950       if (token->type != CPP_NAME)
23951         break;
23952       if (id_equal (token->u.value, "override"))
23953         {
23954           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23955           virt_specifier = VIRT_SPEC_OVERRIDE;
23956         }
23957       else if (id_equal (token->u.value, "final"))
23958         {
23959           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
23960           virt_specifier = VIRT_SPEC_FINAL;
23961         }
23962       else if (id_equal (token->u.value, "__final"))
23963         {
23964           virt_specifier = VIRT_SPEC_FINAL;
23965         }
23966       else
23967 	break;
23968 
23969       if (virt_specifiers & virt_specifier)
23970 	{
23971 	  gcc_rich_location richloc (token->location);
23972 	  richloc.add_fixit_remove ();
23973 	  error_at (&richloc, "duplicate virt-specifier");
23974 	  cp_lexer_purge_token (parser->lexer);
23975 	}
23976       else
23977 	{
23978 	  cp_lexer_consume_token (parser->lexer);
23979 	  virt_specifiers |= virt_specifier;
23980 	}
23981     }
23982   return virt_specifiers;
23983 }
23984 
23985 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
23986    is in scope even though it isn't real.  */
23987 
23988 void
inject_this_parameter(tree ctype,cp_cv_quals quals)23989 inject_this_parameter (tree ctype, cp_cv_quals quals)
23990 {
23991   tree this_parm;
23992 
23993   if (current_class_ptr)
23994     {
23995       /* We don't clear this between NSDMIs.  Is it already what we want?  */
23996       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
23997       if (DECL_P (current_class_ptr)
23998 	  && DECL_CONTEXT (current_class_ptr) == NULL_TREE
23999 	  && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24000 	  && cp_type_quals (type) == quals)
24001 	return;
24002     }
24003 
24004   this_parm = build_this_parm (NULL_TREE, ctype, quals);
24005   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
24006   current_class_ptr = NULL_TREE;
24007   current_class_ref
24008     = cp_build_fold_indirect_ref (this_parm);
24009   current_class_ptr = this_parm;
24010 }
24011 
24012 /* Return true iff our current scope is a non-static data member
24013    initializer.  */
24014 
24015 bool
parsing_nsdmi(void)24016 parsing_nsdmi (void)
24017 {
24018   /* We recognize NSDMI context by the context-less 'this' pointer set up
24019      by the function above.  */
24020   if (current_class_ptr
24021       && TREE_CODE (current_class_ptr) == PARM_DECL
24022       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24023     return true;
24024   return false;
24025 }
24026 
24027 /* True if we're parsing a function declarator.  */
24028 
24029 bool
parsing_function_declarator()24030 parsing_function_declarator ()
24031 {
24032   /* this_entity is NULL for a function parameter scope while parsing the
24033      declarator; it is set when parsing the body of the function.  */
24034   return (current_binding_level->kind == sk_function_parms
24035 	  && !current_binding_level->this_entity);
24036 }
24037 
24038 /* Parse a late-specified return type, if any.  This is not a separate
24039    non-terminal, but part of a function declarator, which looks like
24040 
24041    -> trailing-type-specifier-seq abstract-declarator(opt)
24042 
24043    Returns the type indicated by the type-id.
24044 
24045    In addition to this, parse any queued up #pragma omp declare simd
24046    clauses, and #pragma acc routine clauses.
24047 
24048    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24049    function.  */
24050 
24051 static tree
cp_parser_late_return_type_opt(cp_parser * parser,cp_declarator * declarator,tree & requires_clause)24052 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24053 				tree& requires_clause)
24054 {
24055   cp_token *token;
24056   tree type = NULL_TREE;
24057   bool declare_simd_p = (parser->omp_declare_simd
24058 			 && declarator
24059 			 && declarator->kind == cdk_id);
24060 
24061   bool oacc_routine_p = (parser->oacc_routine
24062 			 && declarator
24063 			 && declarator->kind == cdk_id);
24064 
24065   /* Peek at the next token.  */
24066   token = cp_lexer_peek_token (parser->lexer);
24067   /* A late-specified return type is indicated by an initial '->'. */
24068   if (token->type != CPP_DEREF
24069       && token->keyword != RID_REQUIRES
24070       && !(token->type == CPP_NAME
24071 	   && token->u.value == ridpointers[RID_REQUIRES])
24072       && !(declare_simd_p || oacc_routine_p))
24073     return NULL_TREE;
24074 
24075   if (token->type == CPP_DEREF)
24076     {
24077       /* Consume the ->.  */
24078       cp_lexer_consume_token (parser->lexer);
24079 
24080       type = cp_parser_trailing_type_id (parser);
24081     }
24082 
24083   /* Function declarations may be followed by a trailing
24084      requires-clause.  */
24085   requires_clause = cp_parser_requires_clause_opt (parser, false);
24086 
24087   if (declare_simd_p)
24088     declarator->attributes
24089       = cp_parser_late_parsing_omp_declare_simd (parser,
24090 						 declarator->attributes);
24091   if (oacc_routine_p)
24092     declarator->attributes
24093       = cp_parser_late_parsing_oacc_routine (parser,
24094 					     declarator->attributes);
24095 
24096   return type;
24097 }
24098 
24099 /* Parse a declarator-id.
24100 
24101    declarator-id:
24102      id-expression
24103      :: [opt] nested-name-specifier [opt] type-name
24104 
24105    In the `id-expression' case, the value returned is as for
24106    cp_parser_id_expression if the id-expression was an unqualified-id.
24107    If the id-expression was a qualified-id, then a SCOPE_REF is
24108    returned.  The first operand is the scope (either a NAMESPACE_DECL
24109    or TREE_TYPE), but the second is still just a representation of an
24110    unqualified-id.  */
24111 
24112 static tree
cp_parser_declarator_id(cp_parser * parser,bool optional_p)24113 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24114 {
24115   tree id;
24116   /* The expression must be an id-expression.  Assume that qualified
24117      names are the names of types so that:
24118 
24119        template <class T>
24120        int S<T>::R::i = 3;
24121 
24122      will work; we must treat `S<T>::R' as the name of a type.
24123      Similarly, assume that qualified names are templates, where
24124      required, so that:
24125 
24126        template <class T>
24127        int S<T>::R<T>::i = 3;
24128 
24129      will work, too.  */
24130   id = cp_parser_id_expression (parser,
24131 				/*template_keyword_p=*/false,
24132 				/*check_dependency_p=*/false,
24133 				/*template_p=*/NULL,
24134 				/*declarator_p=*/true,
24135 				optional_p);
24136   if (id && BASELINK_P (id))
24137     id = BASELINK_FUNCTIONS (id);
24138   return id;
24139 }
24140 
24141 /* Parse a type-id.
24142 
24143    type-id:
24144      type-specifier-seq abstract-declarator [opt]
24145 
24146    The parser flags FLAGS is used to control type-specifier parsing.
24147 
24148    If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24149 
24150    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24151    i.e. we've just seen "->".
24152 
24153    Returns the TYPE specified.  */
24154 
24155 static tree
cp_parser_type_id_1(cp_parser * parser,cp_parser_flags flags,bool is_template_arg,bool is_trailing_return,location_t * type_location)24156 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24157 		     bool is_template_arg, bool is_trailing_return,
24158 		     location_t *type_location)
24159 {
24160   cp_decl_specifier_seq type_specifier_seq;
24161   cp_declarator *abstract_declarator;
24162 
24163   /* Parse the type-specifier-seq.  */
24164   cp_parser_type_specifier_seq (parser, flags,
24165 				/*is_declaration=*/false,
24166 				is_trailing_return,
24167 				&type_specifier_seq);
24168   if (type_location)
24169     *type_location = type_specifier_seq.locations[ds_type_spec];
24170 
24171   if (is_template_arg && type_specifier_seq.type
24172       && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24173       && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24174     /* A bare template name as a template argument is a template template
24175        argument, not a placeholder, so fail parsing it as a type argument.  */
24176     {
24177       gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24178       cp_parser_simulate_error (parser);
24179       return error_mark_node;
24180     }
24181   if (type_specifier_seq.type == error_mark_node)
24182     return error_mark_node;
24183 
24184   /* There might or might not be an abstract declarator.  */
24185   cp_parser_parse_tentatively (parser);
24186   /* Look for the declarator.  */
24187   abstract_declarator
24188     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24189 			    CP_PARSER_FLAGS_NONE, NULL,
24190 			    /*parenthesized_p=*/NULL,
24191 			    /*member_p=*/false,
24192 			    /*friend_p=*/false,
24193 			    /*static_p=*/false);
24194   /* Check to see if there really was a declarator.  */
24195   if (!cp_parser_parse_definitely (parser))
24196     abstract_declarator = NULL;
24197 
24198   bool auto_typeid_ok = false;
24199   /* The concepts TS allows 'auto' as a type-id.  */
24200   if (flag_concepts_ts)
24201     auto_typeid_ok = !parser->in_type_id_in_expr_p;
24202   /* DR 625 prohibits use of auto as a template-argument.  We allow 'auto'
24203      outside the template-argument-list context here only for the sake of
24204      diagnostic: grokdeclarator then can emit a better error message for
24205      e.g. using T = auto.  */
24206   else if (flag_concepts)
24207     auto_typeid_ok = (!parser->in_type_id_in_expr_p
24208 		      && !parser->in_template_argument_list_p);
24209 
24210   if (type_specifier_seq.type
24211       && !auto_typeid_ok
24212       /* None of the valid uses of 'auto' in C++14 involve the type-id
24213 	 nonterminal, but it is valid in a trailing-return-type.  */
24214       && !(cxx_dialect >= cxx14 && is_trailing_return))
24215     if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24216       {
24217 	/* A type-id with type 'auto' is only ok if the abstract declarator
24218 	   is a function declarator with a late-specified return type.
24219 
24220 	   A type-id with 'auto' is also valid in a trailing-return-type
24221 	   in a compound-requirement. */
24222 	if (abstract_declarator
24223 	    && abstract_declarator->kind == cdk_function
24224 	    && abstract_declarator->u.function.late_return_type)
24225 	  /* OK */;
24226 	else if (parser->in_result_type_constraint_p)
24227 	  /* OK */;
24228 	else
24229 	  {
24230 	    if (!cp_parser_simulate_error (parser))
24231 	      {
24232 		location_t loc = type_specifier_seq.locations[ds_type_spec];
24233 		if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24234 		  {
24235 		    error_at (loc, "missing template arguments after %qT",
24236 			      auto_node);
24237 		    inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24238 			    tmpl);
24239 		  }
24240 		else if (parser->in_template_argument_list_p)
24241 		  error_at (loc, "%qT not permitted in template argument",
24242 			    auto_node);
24243 		else
24244 		  error_at (loc, "invalid use of %qT", auto_node);
24245 	      }
24246 	    return error_mark_node;
24247 	  }
24248       }
24249 
24250   return groktypename (&type_specifier_seq, abstract_declarator,
24251 		       is_template_arg);
24252 }
24253 
24254 /* Wrapper for cp_parser_type_id_1.  */
24255 
24256 static tree
cp_parser_type_id(cp_parser * parser,cp_parser_flags flags,location_t * type_location)24257 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24258 		   location_t *type_location)
24259 {
24260   return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24261 }
24262 
24263 /* Wrapper for cp_parser_type_id_1.  */
24264 
24265 static tree
cp_parser_template_type_arg(cp_parser * parser)24266 cp_parser_template_type_arg (cp_parser *parser)
24267 {
24268   tree r;
24269   const char *saved_message = parser->type_definition_forbidden_message;
24270   parser->type_definition_forbidden_message
24271     = G_("types may not be defined in template arguments");
24272   r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24273   parser->type_definition_forbidden_message = saved_message;
24274   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24275     {
24276       error ("invalid use of %<auto%> in template argument");
24277       r = error_mark_node;
24278     }
24279   return r;
24280 }
24281 
24282 /* Wrapper for cp_parser_type_id_1.  */
24283 
24284 static tree
cp_parser_trailing_type_id(cp_parser * parser)24285 cp_parser_trailing_type_id (cp_parser *parser)
24286 {
24287   return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24288 			      false, true, NULL);
24289 }
24290 
24291 /* Parse a type-specifier-seq.
24292 
24293    type-specifier-seq:
24294      type-specifier type-specifier-seq [opt]
24295 
24296    GNU extension:
24297 
24298    type-specifier-seq:
24299      attributes type-specifier-seq [opt]
24300 
24301    The parser flags FLAGS is used to control type-specifier parsing.
24302 
24303    If IS_DECLARATION is true, we are at the start of a "condition" or
24304    exception-declaration, so we might be followed by a declarator-id.
24305 
24306    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24307    i.e. we've just seen "->".
24308 
24309    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
24310 
24311 static void
cp_parser_type_specifier_seq(cp_parser * parser,cp_parser_flags flags,bool is_declaration,bool is_trailing_return,cp_decl_specifier_seq * type_specifier_seq)24312 cp_parser_type_specifier_seq (cp_parser* parser,
24313 			      cp_parser_flags flags,
24314 			      bool is_declaration,
24315 			      bool is_trailing_return,
24316 			      cp_decl_specifier_seq *type_specifier_seq)
24317 {
24318   bool seen_type_specifier = false;
24319   cp_token *start_token = NULL;
24320 
24321   /* Clear the TYPE_SPECIFIER_SEQ.  */
24322   clear_decl_specs (type_specifier_seq);
24323 
24324   flags |= CP_PARSER_FLAGS_OPTIONAL;
24325   /* In the context of a trailing return type, enum E { } is an
24326      elaborated-type-specifier followed by a function-body, not an
24327      enum-specifier.  */
24328   if (is_trailing_return)
24329     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24330 
24331   /* Parse the type-specifiers and attributes.  */
24332   while (true)
24333     {
24334       tree type_specifier;
24335       bool is_cv_qualifier;
24336 
24337       /* Check for attributes first.  */
24338       if (cp_next_tokens_can_be_attribute_p (parser))
24339 	{
24340 	  /* GNU attributes at the end of a declaration apply to the
24341 	     declaration as a whole, not to the trailing return type.  So look
24342 	     ahead to see if these attributes are at the end.  */
24343 	  if (seen_type_specifier && is_trailing_return
24344 	      && cp_next_tokens_can_be_gnu_attribute_p (parser))
24345 	    {
24346 	      size_t n = cp_parser_skip_attributes_opt (parser, 1);
24347 	      cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24348 	      if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24349 		  || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24350 		break;
24351 	    }
24352 	  type_specifier_seq->attributes
24353 	    = attr_chainon (type_specifier_seq->attributes,
24354 			    cp_parser_attributes_opt (parser));
24355 	  continue;
24356 	}
24357 
24358       /* record the token of the beginning of the type specifier seq,
24359          for error reporting purposes*/
24360      if (!start_token)
24361        start_token = cp_lexer_peek_token (parser->lexer);
24362 
24363       /* Look for the type-specifier.  */
24364       type_specifier = cp_parser_type_specifier (parser,
24365 						 flags,
24366 						 type_specifier_seq,
24367 						 /*is_declaration=*/false,
24368 						 NULL,
24369 						 &is_cv_qualifier);
24370       if (!type_specifier)
24371 	{
24372 	  /* If the first type-specifier could not be found, this is not a
24373 	     type-specifier-seq at all.  */
24374 	  if (!seen_type_specifier)
24375 	    {
24376 	      /* Set in_declarator_p to avoid skipping to the semicolon.  */
24377 	      int in_decl = parser->in_declarator_p;
24378 	      parser->in_declarator_p = true;
24379 
24380 	      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24381 		  || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24382 		cp_parser_error (parser, "expected type-specifier");
24383 
24384 	      parser->in_declarator_p = in_decl;
24385 
24386 	      type_specifier_seq->type = error_mark_node;
24387 	      return;
24388 	    }
24389 	  /* If subsequent type-specifiers could not be found, the
24390 	     type-specifier-seq is complete.  */
24391 	  break;
24392 	}
24393 
24394       seen_type_specifier = true;
24395       /* The standard says that a condition can be:
24396 
24397 	    type-specifier-seq declarator = assignment-expression
24398 
24399 	 However, given:
24400 
24401 	   struct S {};
24402 	   if (int S = ...)
24403 
24404 	 we should treat the "S" as a declarator, not as a
24405 	 type-specifier.  The standard doesn't say that explicitly for
24406 	 type-specifier-seq, but it does say that for
24407 	 decl-specifier-seq in an ordinary declaration.  Perhaps it
24408 	 would be clearer just to allow a decl-specifier-seq here, and
24409 	 then add a semantic restriction that if any decl-specifiers
24410 	 that are not type-specifiers appear, the program is invalid.  */
24411       if (is_declaration && !is_cv_qualifier)
24412 	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24413     }
24414 }
24415 
24416 /* Return whether the function currently being declared has an associated
24417    template parameter list.  */
24418 
24419 static bool
function_being_declared_is_template_p(cp_parser * parser)24420 function_being_declared_is_template_p (cp_parser* parser)
24421 {
24422   if (!current_template_parms || processing_template_parmlist)
24423     return false;
24424 
24425   if (parser->implicit_template_scope)
24426     return true;
24427 
24428   if (at_class_scope_p ()
24429       && TYPE_BEING_DEFINED (current_class_type))
24430     return parser->num_template_parameter_lists != 0;
24431 
24432   return ((int) parser->num_template_parameter_lists > template_class_depth
24433 	  (current_class_type));
24434 }
24435 
24436 /* Parse a parameter-declaration-clause.
24437 
24438    parameter-declaration-clause:
24439      parameter-declaration-list [opt] ... [opt]
24440      parameter-declaration-list , ...
24441 
24442    The parser flags FLAGS is used to control type-specifier parsing.
24443 
24444    Returns a representation for the parameter declarations.  A return
24445    value of NULL indicates a parameter-declaration-clause consisting
24446    only of an ellipsis.  */
24447 
24448 static tree
cp_parser_parameter_declaration_clause(cp_parser * parser,cp_parser_flags flags)24449 cp_parser_parameter_declaration_clause (cp_parser* parser,
24450 					cp_parser_flags flags)
24451 {
24452   tree parameters;
24453   cp_token *token;
24454   bool ellipsis_p;
24455 
24456   auto cleanup = make_temp_override
24457     (parser->auto_is_implicit_function_template_parm_p);
24458 
24459   if (!processing_specialization
24460       && !processing_template_parmlist
24461       && !processing_explicit_instantiation
24462       /* default_arg_ok_p tracks whether this is a parameter-clause for an
24463          actual function or a random abstract declarator.  */
24464       && parser->default_arg_ok_p)
24465     if (!current_function_decl
24466 	|| (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24467       parser->auto_is_implicit_function_template_parm_p = true;
24468 
24469   /* Peek at the next token.  */
24470   token = cp_lexer_peek_token (parser->lexer);
24471   /* Check for trivial parameter-declaration-clauses.  */
24472   if (token->type == CPP_ELLIPSIS)
24473     {
24474       /* Consume the `...' token.  */
24475       cp_lexer_consume_token (parser->lexer);
24476       return NULL_TREE;
24477     }
24478   else if (token->type == CPP_CLOSE_PAREN)
24479     /* There are no parameters.  */
24480     return void_list_node;
24481   /* Check for `(void)', too, which is a special case.  */
24482   else if (token->keyword == RID_VOID
24483 	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24484 	       == CPP_CLOSE_PAREN))
24485     {
24486       /* Consume the `void' token.  */
24487       cp_lexer_consume_token (parser->lexer);
24488       /* There are no parameters.  */
24489       return explicit_void_list_node;
24490     }
24491 
24492   /* Parse the parameter-declaration-list.  */
24493   parameters = cp_parser_parameter_declaration_list (parser, flags);
24494   /* If a parse error occurred while parsing the
24495      parameter-declaration-list, then the entire
24496      parameter-declaration-clause is erroneous.  */
24497   if (parameters == error_mark_node)
24498     return NULL_TREE;
24499 
24500   /* Peek at the next token.  */
24501   token = cp_lexer_peek_token (parser->lexer);
24502   /* If it's a `,', the clause should terminate with an ellipsis.  */
24503   if (token->type == CPP_COMMA)
24504     {
24505       /* Consume the `,'.  */
24506       cp_lexer_consume_token (parser->lexer);
24507       /* Expect an ellipsis.  */
24508       ellipsis_p
24509 	= (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24510     }
24511   /* It might also be `...' if the optional trailing `,' was
24512      omitted.  */
24513   else if (token->type == CPP_ELLIPSIS)
24514     {
24515       /* Consume the `...' token.  */
24516       cp_lexer_consume_token (parser->lexer);
24517       /* And remember that we saw it.  */
24518       ellipsis_p = true;
24519     }
24520   else
24521     ellipsis_p = false;
24522 
24523   /* Finish the parameter list.  */
24524   if (!ellipsis_p)
24525     parameters = chainon (parameters, void_list_node);
24526 
24527   return parameters;
24528 }
24529 
24530 /* Parse a parameter-declaration-list.
24531 
24532    parameter-declaration-list:
24533      parameter-declaration
24534      parameter-declaration-list , parameter-declaration
24535 
24536    The parser flags FLAGS is used to control type-specifier parsing.
24537 
24538    Returns a representation of the parameter-declaration-list, as for
24539    cp_parser_parameter_declaration_clause.  However, the
24540    `void_list_node' is never appended to the list.  */
24541 
24542 static tree
cp_parser_parameter_declaration_list(cp_parser * parser,cp_parser_flags flags)24543 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
24544 {
24545   tree parameters = NULL_TREE;
24546   tree *tail = &parameters;
24547   bool saved_in_unbraced_linkage_specification_p;
24548   int index = 0;
24549 
24550   /* The special considerations that apply to a function within an
24551      unbraced linkage specifications do not apply to the parameters
24552      to the function.  */
24553   saved_in_unbraced_linkage_specification_p
24554     = parser->in_unbraced_linkage_specification_p;
24555   parser->in_unbraced_linkage_specification_p = false;
24556 
24557   /* Look for more parameters.  */
24558   while (true)
24559     {
24560       cp_parameter_declarator *parameter;
24561       tree decl = error_mark_node;
24562       bool parenthesized_p = false;
24563 
24564       /* Parse the parameter.  */
24565       parameter
24566 	= cp_parser_parameter_declaration (parser, flags,
24567 					   /*template_parm_p=*/false,
24568 					   &parenthesized_p);
24569 
24570       /* We don't know yet if the enclosing context is unavailable or deprecated,
24571 	 so wait and deal with it in grokparms if appropriate.  */
24572       deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24573 
24574       if (parameter && !cp_parser_error_occurred (parser))
24575 	{
24576 	  decl = grokdeclarator (parameter->declarator,
24577 				 &parameter->decl_specifiers,
24578 				 PARM,
24579 				 parameter->default_argument != NULL_TREE,
24580 				 &parameter->decl_specifiers.attributes);
24581 	  if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24582 	    DECL_SOURCE_LOCATION (decl) = parameter->loc;
24583 	}
24584 
24585       deprecated_state = DEPRECATED_NORMAL;
24586 
24587       /* If a parse error occurred parsing the parameter declaration,
24588 	 then the entire parameter-declaration-list is erroneous.  */
24589       if (decl == error_mark_node)
24590 	{
24591 	  parameters = error_mark_node;
24592 	  break;
24593 	}
24594 
24595       if (parameter->decl_specifiers.attributes)
24596 	cplus_decl_attributes (&decl,
24597 			       parameter->decl_specifiers.attributes,
24598 			       0);
24599       if (DECL_NAME (decl))
24600 	decl = pushdecl (decl);
24601 
24602       if (decl != error_mark_node)
24603 	{
24604 	  retrofit_lang_decl (decl);
24605 	  DECL_PARM_INDEX (decl) = ++index;
24606 	  DECL_PARM_LEVEL (decl) = function_parm_depth ();
24607 	}
24608 
24609       /* Add the new parameter to the list.  */
24610       *tail = build_tree_list (parameter->default_argument, decl);
24611       tail = &TREE_CHAIN (*tail);
24612 
24613       /* If the parameters were parenthesized, it's the case of
24614 	 T foo(X(x)) which looks like a variable definition but
24615 	 is a function declaration.  */
24616       if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24617 	PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24618 
24619       /* Peek at the next token.  */
24620       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24621 	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24622 	  /* These are for Objective-C++ */
24623 	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24624 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24625 	/* The parameter-declaration-list is complete.  */
24626 	break;
24627       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24628 	{
24629 	  cp_token *token;
24630 
24631 	  /* Peek at the next token.  */
24632 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
24633 	  /* If it's an ellipsis, then the list is complete.  */
24634 	  if (token->type == CPP_ELLIPSIS)
24635 	    break;
24636 	  /* Otherwise, there must be more parameters.  Consume the
24637 	     `,'.  */
24638 	  cp_lexer_consume_token (parser->lexer);
24639 	  /* When parsing something like:
24640 
24641 		int i(float f, double d)
24642 
24643 	     we can tell after seeing the declaration for "f" that we
24644 	     are not looking at an initialization of a variable "i",
24645 	     but rather at the declaration of a function "i".
24646 
24647 	     Due to the fact that the parsing of template arguments
24648 	     (as specified to a template-id) requires backtracking we
24649 	     cannot use this technique when inside a template argument
24650 	     list.  */
24651 	  if (!parser->in_template_argument_list_p
24652 	      && !parser->in_type_id_in_expr_p
24653 	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
24654 	      /* However, a parameter-declaration of the form
24655 		 "float(f)" (which is a valid declaration of a
24656 		 parameter "f") can also be interpreted as an
24657 		 expression (the conversion of "f" to "float").  */
24658 	      && !parenthesized_p)
24659 	    cp_parser_commit_to_tentative_parse (parser);
24660 	}
24661       else
24662 	{
24663 	  cp_parser_error (parser, "expected %<,%> or %<...%>");
24664 	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24665 	    cp_parser_skip_to_closing_parenthesis (parser,
24666 						   /*recovering=*/true,
24667 						   /*or_comma=*/false,
24668 						   /*consume_paren=*/false);
24669 	  break;
24670 	}
24671     }
24672 
24673   parser->in_unbraced_linkage_specification_p
24674     = saved_in_unbraced_linkage_specification_p;
24675 
24676   /* Reset implicit_template_scope if we are about to leave the function
24677      parameter list that introduced it.  Note that for out-of-line member
24678      definitions, there will be one or more class scopes before we get to
24679      the template parameter scope.  */
24680 
24681   if (cp_binding_level *its = parser->implicit_template_scope)
24682     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24683       {
24684 	while (maybe_its->kind == sk_class)
24685 	  maybe_its = maybe_its->level_chain;
24686 	if (maybe_its == its)
24687 	  {
24688 	    parser->implicit_template_parms = 0;
24689 	    parser->implicit_template_scope = 0;
24690 	  }
24691       }
24692 
24693   return parameters;
24694 }
24695 
24696 /* Parse a parameter declaration.
24697 
24698    parameter-declaration:
24699      decl-specifier-seq ... [opt] declarator
24700      decl-specifier-seq declarator = assignment-expression
24701      decl-specifier-seq ... [opt] abstract-declarator [opt]
24702      decl-specifier-seq abstract-declarator [opt] = assignment-expression
24703 
24704    The parser flags FLAGS is used to control type-specifier parsing.
24705 
24706    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24707    declares a template parameter.  (In that case, a non-nested `>'
24708    token encountered during the parsing of the assignment-expression
24709    is not interpreted as a greater-than operator.)
24710 
24711    Returns a representation of the parameter, or NULL if an error
24712    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24713    true iff the declarator is of the form "(p)".  */
24714 
24715 static cp_parameter_declarator *
cp_parser_parameter_declaration(cp_parser * parser,cp_parser_flags flags,bool template_parm_p,bool * parenthesized_p)24716 cp_parser_parameter_declaration (cp_parser *parser,
24717 				 cp_parser_flags flags,
24718 				 bool template_parm_p,
24719 				 bool *parenthesized_p)
24720 {
24721   int declares_class_or_enum;
24722   cp_decl_specifier_seq decl_specifiers;
24723   cp_declarator *declarator;
24724   tree default_argument;
24725   cp_token *token = NULL, *declarator_token_start = NULL;
24726   const char *saved_message;
24727   bool template_parameter_pack_p = false;
24728 
24729   /* In a template parameter, `>' is not an operator.
24730 
24731      [temp.param]
24732 
24733      When parsing a default template-argument for a non-type
24734      template-parameter, the first non-nested `>' is taken as the end
24735      of the template parameter-list rather than a greater-than
24736      operator.  */
24737 
24738   /* Type definitions may not appear in parameter types.  */
24739   saved_message = parser->type_definition_forbidden_message;
24740   parser->type_definition_forbidden_message
24741     = G_("types may not be defined in parameter types");
24742 
24743   int template_parm_idx = (function_being_declared_is_template_p (parser) ?
24744 			   TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24745 					    (current_template_parms)) : 0);
24746 
24747   /* Parse the declaration-specifiers.  */
24748   cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24749   cp_parser_decl_specifier_seq (parser,
24750 				flags,
24751 				&decl_specifiers,
24752 				&declares_class_or_enum);
24753 
24754   /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24755      type-constraint opt auto can be used as a decl-specifier of the
24756      decl-specifier-seq of a parameter-declaration of a function declaration
24757      or lambda-expression..." but we must not synthesize an implicit template
24758      type parameter in its declarator.  That is, in "void f(auto[auto{10}]);"
24759      we want to synthesize only the first auto.  */
24760   auto cleanup = make_temp_override
24761     (parser->auto_is_implicit_function_template_parm_p, false);
24762 
24763   /* Complain about missing 'typename' or other invalid type names.  */
24764   if (!decl_specifiers.any_type_specifiers_p
24765       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24766     decl_specifiers.type = error_mark_node;
24767 
24768   /* If an error occurred, there's no reason to attempt to parse the
24769      rest of the declaration.  */
24770   if (cp_parser_error_occurred (parser))
24771     {
24772       parser->type_definition_forbidden_message = saved_message;
24773       return NULL;
24774     }
24775 
24776   /* Peek at the next token.  */
24777   token = cp_lexer_peek_token (parser->lexer);
24778 
24779   /* If the next token is a `)', `,', `=', `>', or `...', then there
24780      is no declarator. However, when variadic templates are enabled,
24781      there may be a declarator following `...'.  */
24782   if (token->type == CPP_CLOSE_PAREN
24783       || token->type == CPP_COMMA
24784       || token->type == CPP_EQ
24785       || token->type == CPP_GREATER)
24786     {
24787       declarator = NULL;
24788       if (parenthesized_p)
24789 	*parenthesized_p = false;
24790     }
24791   /* Otherwise, there should be a declarator.  */
24792   else
24793     {
24794       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24795       parser->default_arg_ok_p = false;
24796 
24797       /* After seeing a decl-specifier-seq, if the next token is not a
24798 	 "(" or "{", there is no possibility that the code is a valid
24799 	 expression.  Therefore, if parsing tentatively, we commit at
24800 	 this point.  */
24801       if (!parser->in_template_argument_list_p
24802 	  /* In an expression context, having seen:
24803 
24804 	       (int((char ...
24805 
24806 	     we cannot be sure whether we are looking at a
24807 	     function-type (taking a "char" as a parameter) or a cast
24808 	     of some object of type "char" to "int".  */
24809 	  && !parser->in_type_id_in_expr_p
24810 	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
24811 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
24812 	{
24813 	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24814 	    {
24815 	      if (decl_specifiers.type
24816 		  && template_placeholder_p (decl_specifiers.type))
24817 		/* This is a CTAD expression, not a parameter declaration.  */
24818 		cp_parser_simulate_error (parser);
24819 	    }
24820 	  else
24821 	    cp_parser_commit_to_tentative_parse (parser);
24822 	}
24823       /* Parse the declarator.  */
24824       declarator_token_start = token;
24825       declarator = cp_parser_declarator (parser,
24826 					 CP_PARSER_DECLARATOR_EITHER,
24827 					 CP_PARSER_FLAGS_NONE,
24828 					 /*ctor_dtor_or_conv_p=*/NULL,
24829 					 parenthesized_p,
24830 					 /*member_p=*/false,
24831 					 /*friend_p=*/false,
24832 					 /*static_p=*/false);
24833       parser->default_arg_ok_p = saved_default_arg_ok_p;
24834       /* After the declarator, allow more attributes.  */
24835       decl_specifiers.attributes
24836 	= attr_chainon (decl_specifiers.attributes,
24837 			cp_parser_attributes_opt (parser));
24838 
24839       /* If the declarator is a template parameter pack, remember that and
24840 	 clear the flag in the declarator itself so we don't get errors
24841 	 from grokdeclarator.  */
24842       if (template_parm_p && declarator && declarator->parameter_pack_p)
24843 	{
24844 	  declarator->parameter_pack_p = false;
24845 	  template_parameter_pack_p = true;
24846 	}
24847     }
24848 
24849   /* If the next token is an ellipsis, and we have not seen a declarator
24850      name, and if either the type of the declarator contains parameter
24851      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
24852      for, eg, abbreviated integral type names), then we actually have a
24853      parameter pack expansion expression. Otherwise, leave the ellipsis
24854      for a C-style variadic function. */
24855   token = cp_lexer_peek_token (parser->lexer);
24856 
24857   /* If a function parameter pack was specified and an implicit template
24858      parameter was introduced during cp_parser_parameter_declaration,
24859      change any implicit parameters introduced into packs.  */
24860   if (parser->implicit_template_parms
24861       && ((token->type == CPP_ELLIPSIS
24862 	   && declarator_can_be_parameter_pack (declarator))
24863 	  || (declarator && declarator->parameter_pack_p)))
24864     {
24865       int latest_template_parm_idx = TREE_VEC_LENGTH
24866 	(INNERMOST_TEMPLATE_PARMS (current_template_parms));
24867 
24868       if (latest_template_parm_idx != template_parm_idx)
24869 	decl_specifiers.type = convert_generic_types_to_packs
24870 	  (decl_specifiers.type,
24871 	   template_parm_idx, latest_template_parm_idx);
24872     }
24873 
24874   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24875     {
24876       tree type = decl_specifiers.type;
24877 
24878       if (type && DECL_P (type))
24879         type = TREE_TYPE (type);
24880 
24881       if (((type
24882 	    && TREE_CODE (type) != TYPE_PACK_EXPANSION
24883 	    && (template_parm_p || uses_parameter_packs (type)))
24884 	   || (!type && template_parm_p))
24885 	  && declarator_can_be_parameter_pack (declarator))
24886 	{
24887 	  /* Consume the `...'. */
24888 	  cp_lexer_consume_token (parser->lexer);
24889 	  maybe_warn_variadic_templates ();
24890 
24891 	  /* Build a pack expansion type */
24892 	  if (template_parm_p)
24893 	    template_parameter_pack_p = true;
24894 	  else if (declarator)
24895 	    declarator->parameter_pack_p = true;
24896 	  else
24897 	    decl_specifiers.type = make_pack_expansion (type);
24898 	}
24899     }
24900 
24901   /* The restriction on defining new types applies only to the type
24902      of the parameter, not to the default argument.  */
24903   parser->type_definition_forbidden_message = saved_message;
24904 
24905   /* If the next token is `=', then process a default argument.  */
24906   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24907     {
24908       tree type = decl_specifiers.type;
24909       token = cp_lexer_peek_token (parser->lexer);
24910       if (declarator)
24911 	declarator->init_loc = token->location;
24912       /* If we are defining a class, then the tokens that make up the
24913 	 default argument must be saved and processed later.  */
24914       if (!template_parm_p && at_class_scope_p ()
24915 	  && TYPE_BEING_DEFINED (current_class_type)
24916 	  && !LAMBDA_TYPE_P (current_class_type))
24917 	default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
24918 
24919       /* A constrained-type-specifier may declare a type
24920 	 template-parameter.  */
24921       else if (declares_constrained_type_template_parameter (type))
24922         default_argument
24923           = cp_parser_default_type_template_argument (parser);
24924 
24925       /* A constrained-type-specifier may declare a
24926 	 template-template-parameter.  */
24927       else if (declares_constrained_template_template_parameter (type))
24928         default_argument
24929           = cp_parser_default_template_template_argument (parser);
24930 
24931       /* Outside of a class definition, we can just parse the
24932 	 assignment-expression.  */
24933       else
24934 	default_argument
24935 	  = cp_parser_default_argument (parser, template_parm_p);
24936 
24937       if (!parser->default_arg_ok_p)
24938 	{
24939 	  permerror (token->location,
24940 		     "default arguments are only "
24941 		     "permitted for function parameters");
24942 	}
24943       else if ((declarator && declarator->parameter_pack_p)
24944 	       || template_parameter_pack_p
24945 	       || (decl_specifiers.type
24946 		   && PACK_EXPANSION_P (decl_specifiers.type)))
24947 	{
24948 	  /* Find the name of the parameter pack.  */
24949 	  cp_declarator *id_declarator = declarator;
24950 	  while (id_declarator && id_declarator->kind != cdk_id)
24951 	    id_declarator = id_declarator->declarator;
24952 
24953 	  if (id_declarator && id_declarator->kind == cdk_id)
24954 	    error_at (declarator_token_start->location,
24955 		      template_parm_p
24956 		      ? G_("template parameter pack %qD "
24957 			   "cannot have a default argument")
24958 		      : G_("parameter pack %qD cannot have "
24959 			   "a default argument"),
24960 		      id_declarator->u.id.unqualified_name);
24961 	  else
24962 	    error_at (declarator_token_start->location,
24963 		      template_parm_p
24964 		      ? G_("template parameter pack cannot have "
24965 			   "a default argument")
24966 		      : G_("parameter pack cannot have a "
24967 			   "default argument"));
24968 
24969 	  default_argument = NULL_TREE;
24970 	}
24971     }
24972   else
24973     default_argument = NULL_TREE;
24974 
24975   if (default_argument)
24976     STRIP_ANY_LOCATION_WRAPPER (default_argument);
24977 
24978   /* Generate a location for the parameter, ranging from the start of the
24979      initial token to the end of the final token (using input_location for
24980      the latter, set up by cp_lexer_set_source_position_from_token when
24981      consuming tokens).
24982 
24983      If we have a identifier, then use it for the caret location, e.g.
24984 
24985        extern int callee (int one, int (*two)(int, int), float three);
24986                                    ~~~~~~^~~~~~~~~~~~~~
24987 
24988      otherwise, reuse the start location for the caret location e.g.:
24989 
24990        extern int callee (int one, int (*)(int, int), float three);
24991                                    ^~~~~~~~~~~~~~~~~
24992 
24993   */
24994   location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
24995 			  ? declarator->id_loc
24996 			  : decl_spec_token_start->location);
24997   location_t param_loc = make_location (caret_loc,
24998 					decl_spec_token_start->location,
24999 					input_location);
25000 
25001   return make_parameter_declarator (&decl_specifiers,
25002 				    declarator,
25003 				    default_argument,
25004 				    param_loc,
25005 				    template_parameter_pack_p);
25006 }
25007 
25008 /* Parse a default argument and return it.
25009 
25010    TEMPLATE_PARM_P is true if this is a default argument for a
25011    non-type template parameter.  */
25012 static tree
cp_parser_default_argument(cp_parser * parser,bool template_parm_p)25013 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25014 {
25015   tree default_argument = NULL_TREE;
25016   bool saved_greater_than_is_operator_p;
25017   unsigned char saved_local_variables_forbidden_p;
25018   bool non_constant_p, is_direct_init;
25019 
25020   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25021      set correctly.  */
25022   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25023   parser->greater_than_is_operator_p = !template_parm_p;
25024   auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25025   auto ord = make_temp_override (parser->oacc_routine, NULL);
25026   auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25027 
25028   /* Local variable names (and the `this' keyword) may not
25029      appear in a default argument.  */
25030   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25031   parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25032   /* Parse the assignment-expression.  */
25033   if (template_parm_p)
25034     push_deferring_access_checks (dk_no_deferred);
25035   tree saved_class_ptr = NULL_TREE;
25036   tree saved_class_ref = NULL_TREE;
25037   /* The "this" pointer is not valid in a default argument.  */
25038   if (cfun)
25039     {
25040       saved_class_ptr = current_class_ptr;
25041       cp_function_chain->x_current_class_ptr = NULL_TREE;
25042       saved_class_ref = current_class_ref;
25043       cp_function_chain->x_current_class_ref = NULL_TREE;
25044     }
25045   default_argument
25046     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
25047   /* Restore the "this" pointer.  */
25048   if (cfun)
25049     {
25050       cp_function_chain->x_current_class_ptr = saved_class_ptr;
25051       cp_function_chain->x_current_class_ref = saved_class_ref;
25052     }
25053   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25054     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25055   if (template_parm_p)
25056     pop_deferring_access_checks ();
25057   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25058   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25059 
25060   return default_argument;
25061 }
25062 
25063 /* Parse a function-body.
25064 
25065    function-body:
25066      compound_statement  */
25067 
25068 static void
cp_parser_function_body(cp_parser * parser,bool in_function_try_block)25069 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25070 {
25071   cp_parser_compound_statement (parser, NULL, (in_function_try_block
25072 					       ? BCS_TRY_BLOCK : BCS_NORMAL),
25073 				true);
25074 }
25075 
25076 /* Parse a ctor-initializer-opt followed by a function-body.  Return
25077    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
25078    is true we are parsing a function-try-block.  */
25079 
25080 static void
cp_parser_ctor_initializer_opt_and_function_body(cp_parser * parser,bool in_function_try_block)25081 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25082 						  bool in_function_try_block)
25083 {
25084   tree body, list;
25085   const bool check_body_p
25086      = (DECL_CONSTRUCTOR_P (current_function_decl)
25087 	&& DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25088   tree last = NULL;
25089 
25090   if (in_function_try_block
25091       && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25092       && cxx_dialect < cxx20)
25093     {
25094       if (DECL_CONSTRUCTOR_P (current_function_decl))
25095 	pedwarn (input_location, OPT_Wc__20_extensions,
25096 		 "function-try-block body of %<constexpr%> constructor only "
25097 		 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25098       else
25099 	pedwarn (input_location, OPT_Wc__20_extensions,
25100 		 "function-try-block body of %<constexpr%> function only "
25101 		 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25102     }
25103 
25104   /* Begin the function body.  */
25105   body = begin_function_body ();
25106   /* Parse the optional ctor-initializer.  */
25107   cp_parser_ctor_initializer_opt (parser);
25108 
25109   /* If we're parsing a constexpr constructor definition, we need
25110      to check that the constructor body is indeed empty.  However,
25111      before we get to cp_parser_function_body lot of junk has been
25112      generated, so we can't just check that we have an empty block.
25113      Rather we take a snapshot of the outermost block, and check whether
25114      cp_parser_function_body changed its state.  */
25115   if (check_body_p)
25116     {
25117       list = cur_stmt_list;
25118       if (STATEMENT_LIST_TAIL (list))
25119 	last = STATEMENT_LIST_TAIL (list)->stmt;
25120     }
25121   /* Parse the function-body.  */
25122   cp_parser_function_body (parser, in_function_try_block);
25123   if (check_body_p)
25124     check_constexpr_ctor_body (last, list, /*complain=*/true);
25125   /* Finish the function body.  */
25126   finish_function_body (body);
25127 }
25128 
25129 /* Parse an initializer.
25130 
25131    initializer:
25132      = initializer-clause
25133      ( expression-list )
25134 
25135    Returns an expression representing the initializer.  If no
25136    initializer is present, NULL_TREE is returned.
25137 
25138    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25139    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
25140    set to TRUE if there is no initializer present.  If there is an
25141    initializer, and it is not a constant-expression, *NON_CONSTANT_P
25142    is set to true; otherwise it is set to false.  */
25143 
25144 static tree
cp_parser_initializer(cp_parser * parser,bool * is_direct_init,bool * non_constant_p,bool subexpression_p)25145 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25146 		       bool* non_constant_p, bool subexpression_p)
25147 {
25148   cp_token *token;
25149   tree init;
25150 
25151   /* Peek at the next token.  */
25152   token = cp_lexer_peek_token (parser->lexer);
25153 
25154   /* Let our caller know whether or not this initializer was
25155      parenthesized.  */
25156   *is_direct_init = (token->type != CPP_EQ);
25157   /* Assume that the initializer is constant.  */
25158   *non_constant_p = false;
25159 
25160   if (token->type == CPP_EQ)
25161     {
25162       /* Consume the `='.  */
25163       cp_lexer_consume_token (parser->lexer);
25164       /* Parse the initializer-clause.  */
25165       init = cp_parser_initializer_clause (parser, non_constant_p);
25166     }
25167   else if (token->type == CPP_OPEN_PAREN)
25168     {
25169       vec<tree, va_gc> *vec;
25170       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25171 						     /*cast_p=*/false,
25172 						     /*allow_expansion_p=*/true,
25173 						     non_constant_p);
25174       if (vec == NULL)
25175 	return error_mark_node;
25176       init = build_tree_list_vec (vec);
25177       release_tree_vector (vec);
25178     }
25179   else if (token->type == CPP_OPEN_BRACE)
25180     {
25181       cp_lexer_set_source_position (parser->lexer);
25182       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25183       init = cp_parser_braced_list (parser, non_constant_p);
25184       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25185     }
25186   else
25187     {
25188       /* Anything else is an error.  */
25189       cp_parser_error (parser, "expected initializer");
25190       init = error_mark_node;
25191     }
25192 
25193   if (!subexpression_p && check_for_bare_parameter_packs (init))
25194     init = error_mark_node;
25195 
25196   return init;
25197 }
25198 
25199 /* Parse an initializer-clause.
25200 
25201    initializer-clause:
25202      assignment-expression
25203      braced-init-list
25204 
25205    Returns an expression representing the initializer.
25206 
25207    If the `assignment-expression' production is used the value
25208    returned is simply a representation for the expression.
25209 
25210    Otherwise, calls cp_parser_braced_list.  */
25211 
25212 static cp_expr
cp_parser_initializer_clause(cp_parser * parser,bool * non_constant_p)25213 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25214 {
25215   cp_expr initializer;
25216 
25217   /* Assume the expression is constant.  */
25218   *non_constant_p = false;
25219 
25220   /* If it is not a `{', then we are looking at an
25221      assignment-expression.  */
25222   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25223     {
25224       initializer
25225 	= cp_parser_constant_expression (parser,
25226 					/*allow_non_constant_p=*/2,
25227 					non_constant_p);
25228     }
25229   else
25230     initializer = cp_parser_braced_list (parser, non_constant_p);
25231 
25232   return initializer;
25233 }
25234 
25235 /* Parse a brace-enclosed initializer list.
25236 
25237    braced-init-list:
25238      { initializer-list , [opt] }
25239      { designated-initializer-list , [opt] }
25240      { }
25241 
25242    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
25243    the elements of the initializer-list (or NULL, if the last
25244    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
25245    NULL_TREE.  There is no way to detect whether or not the optional
25246    trailing `,' was provided.  NON_CONSTANT_P is as for
25247    cp_parser_initializer.  */
25248 
25249 static cp_expr
cp_parser_braced_list(cp_parser * parser,bool * non_constant_p)25250 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25251 {
25252   tree initializer;
25253   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25254 
25255   /* Consume the `{' token.  */
25256   matching_braces braces;
25257   braces.require_open (parser);
25258   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
25259   initializer = make_node (CONSTRUCTOR);
25260   /* If it's not a `}', then there is a non-trivial initializer.  */
25261   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25262     {
25263       bool designated;
25264       /* Parse the initializer list.  */
25265       CONSTRUCTOR_ELTS (initializer)
25266 	= cp_parser_initializer_list (parser, non_constant_p, &designated);
25267       CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25268       /* A trailing `,' token is allowed.  */
25269       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25270 	cp_lexer_consume_token (parser->lexer);
25271     }
25272   else
25273     *non_constant_p = false;
25274   /* Now, there should be a trailing `}'.  */
25275   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25276   braces.require_close (parser);
25277   TREE_TYPE (initializer) = init_list_type_node;
25278 
25279   cp_expr result (initializer);
25280   /* Build a location of the form:
25281        { ... }
25282        ^~~~~~~
25283      with caret==start at the open brace, finish at the close brace.  */
25284   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25285   result.set_location (combined_loc);
25286   return result;
25287 }
25288 
25289 /* Consume tokens up to, and including, the next non-nested closing `]'.
25290    Returns true iff we found a closing `]'.  */
25291 
25292 static bool
cp_parser_skip_to_closing_square_bracket(cp_parser * parser)25293 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25294 {
25295   unsigned square_depth = 0;
25296 
25297   while (true)
25298     {
25299       cp_token * token = cp_lexer_peek_token (parser->lexer);
25300 
25301       switch (token->type)
25302 	{
25303 	case CPP_PRAGMA_EOL:
25304 	  if (!parser->lexer->in_pragma)
25305 	    break;
25306 	  /* FALLTHRU */
25307 	case CPP_EOF:
25308 	  /* If we've run out of tokens, then there is no closing `]'.  */
25309 	  return false;
25310 
25311         case CPP_OPEN_SQUARE:
25312           ++square_depth;
25313           break;
25314 
25315         case CPP_CLOSE_SQUARE:
25316 	  if (!square_depth--)
25317 	    {
25318 	      cp_lexer_consume_token (parser->lexer);
25319 	      return true;
25320 	    }
25321 	  break;
25322 
25323 	default:
25324 	  break;
25325 	}
25326 
25327       /* Consume the token.  */
25328       cp_lexer_consume_token (parser->lexer);
25329     }
25330 }
25331 
25332 /* Return true if we are looking at an array-designator, false otherwise.  */
25333 
25334 static bool
cp_parser_array_designator_p(cp_parser * parser)25335 cp_parser_array_designator_p (cp_parser *parser)
25336 {
25337   /* Consume the `['.  */
25338   cp_lexer_consume_token (parser->lexer);
25339 
25340   cp_lexer_save_tokens (parser->lexer);
25341 
25342   /* Skip tokens until the next token is a closing square bracket.
25343      If we find the closing `]', and the next token is a `=', then
25344      we are looking at an array designator.  */
25345   bool array_designator_p
25346     = (cp_parser_skip_to_closing_square_bracket (parser)
25347        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25348 
25349   /* Roll back the tokens we skipped.  */
25350   cp_lexer_rollback_tokens (parser->lexer);
25351 
25352   return array_designator_p;
25353 }
25354 
25355 /* Parse an initializer-list.
25356 
25357    initializer-list:
25358      initializer-clause ... [opt]
25359      initializer-list , initializer-clause ... [opt]
25360 
25361    C++20 Extension:
25362 
25363    designated-initializer-list:
25364      designated-initializer-clause
25365      designated-initializer-list , designated-initializer-clause
25366 
25367    designated-initializer-clause:
25368      designator brace-or-equal-initializer
25369 
25370    designator:
25371      . identifier
25372 
25373    GNU Extension:
25374 
25375    initializer-list:
25376      designation initializer-clause ...[opt]
25377      initializer-list , designation initializer-clause ...[opt]
25378 
25379    designation:
25380      . identifier =
25381      identifier :
25382      [ constant-expression ] =
25383 
25384    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
25385    for the initializer.  If the INDEX of the elt is non-NULL, it is the
25386    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
25387    as for cp_parser_initializer.  Set *DESIGNATED to a boolean whether there
25388    are any designators.  */
25389 
25390 static vec<constructor_elt, va_gc> *
cp_parser_initializer_list(cp_parser * parser,bool * non_constant_p,bool * designated)25391 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25392 			    bool *designated)
25393 {
25394   vec<constructor_elt, va_gc> *v = NULL;
25395   bool first_p = true;
25396   tree first_designator = NULL_TREE;
25397 
25398   /* Assume all of the expressions are constant.  */
25399   *non_constant_p = false;
25400 
25401   unsigned nelts = 0;
25402   int suppress = suppress_location_wrappers;
25403 
25404   /* Parse the rest of the list.  */
25405   while (true)
25406     {
25407       cp_token *token;
25408       tree designator;
25409       tree initializer;
25410       bool clause_non_constant_p;
25411       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25412 
25413       /* Handle the C++20 syntax, '. id ='.  */
25414       if ((cxx_dialect >= cxx20
25415 	   || cp_parser_allow_gnu_extensions_p (parser))
25416 	  && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25417 	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25418 	  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25419 	      || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25420 		  == CPP_OPEN_BRACE)))
25421 	{
25422 	  if (pedantic && cxx_dialect < cxx20)
25423 	    pedwarn (loc, OPT_Wc__20_extensions,
25424 		     "C++ designated initializers only available with "
25425 		     "%<-std=c++20%> or %<-std=gnu++20%>");
25426 	  /* Consume the `.'.  */
25427 	  cp_lexer_consume_token (parser->lexer);
25428 	  /* Consume the identifier.  */
25429 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
25430 	  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25431 	    /* Consume the `='.  */
25432 	    cp_lexer_consume_token (parser->lexer);
25433 	}
25434       /* Also, if the next token is an identifier and the following one is a
25435 	 colon, we are looking at the GNU designated-initializer
25436 	 syntax.  */
25437       else if (cp_parser_allow_gnu_extensions_p (parser)
25438 	       && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25439 	       && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25440 		   == CPP_COLON))
25441 	{
25442 	  /* Warn the user that they are using an extension.  */
25443 	  pedwarn (loc, OPT_Wpedantic,
25444 		   "ISO C++ does not allow GNU designated initializers");
25445 	  /* Consume the identifier.  */
25446 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
25447 	  /* Consume the `:'.  */
25448 	  cp_lexer_consume_token (parser->lexer);
25449 	}
25450       /* Also handle C99 array designators, '[ const ] ='.  */
25451       else if (cp_parser_allow_gnu_extensions_p (parser)
25452 	       && !c_dialect_objc ()
25453 	       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25454 	{
25455 	  /* In C++11, [ could start a lambda-introducer.  */
25456 	  bool non_const = false;
25457 
25458 	  cp_parser_parse_tentatively (parser);
25459 
25460 	  if (!cp_parser_array_designator_p (parser))
25461 	    {
25462 	      cp_parser_simulate_error (parser);
25463 	      designator = NULL_TREE;
25464 	    }
25465 	  else
25466 	    {
25467 	      designator = cp_parser_constant_expression (parser, true,
25468 							  &non_const);
25469 	      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25470 	      cp_parser_require (parser, CPP_EQ, RT_EQ);
25471 	    }
25472 
25473 	  if (!cp_parser_parse_definitely (parser))
25474 	    designator = NULL_TREE;
25475 	  else if (non_const
25476 		   && (!require_potential_rvalue_constant_expression
25477 		       (designator)))
25478 	    designator = NULL_TREE;
25479 	  if (designator)
25480 	    /* Warn the user that they are using an extension.  */
25481 	    pedwarn (loc, OPT_Wpedantic,
25482 		     "ISO C++ does not allow C99 designated initializers");
25483 	}
25484       else
25485 	designator = NULL_TREE;
25486 
25487       if (first_p)
25488 	{
25489 	  first_designator = designator;
25490 	  first_p = false;
25491 	}
25492       else if (cxx_dialect >= cxx20
25493 	       && first_designator != error_mark_node
25494 	       && (!first_designator != !designator))
25495 	{
25496 	  error_at (loc, "either all initializer clauses should be designated "
25497 			 "or none of them should be");
25498 	  first_designator = error_mark_node;
25499 	}
25500       else if (cxx_dialect < cxx20 && !first_designator)
25501 	first_designator = designator;
25502 
25503       /* Parse the initializer.  */
25504       initializer = cp_parser_initializer_clause (parser,
25505 						  &clause_non_constant_p);
25506       /* If any clause is non-constant, so is the entire initializer.  */
25507       if (clause_non_constant_p)
25508 	*non_constant_p = true;
25509 
25510       /* If we have an ellipsis, this is an initializer pack
25511 	 expansion.  */
25512       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25513         {
25514 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25515 
25516           /* Consume the `...'.  */
25517           cp_lexer_consume_token (parser->lexer);
25518 
25519 	  if (designator && cxx_dialect >= cxx20)
25520 	    error_at (loc,
25521 		      "%<...%> not allowed in designated initializer list");
25522 
25523 	  /* Turn the initializer into an initializer expansion.  */
25524 	  initializer = make_pack_expansion (initializer);
25525         }
25526 
25527       /* Add it to the vector.  */
25528       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25529 
25530       /* If the next token is not a comma, we have reached the end of
25531 	 the list.  */
25532       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25533 	break;
25534 
25535       /* Peek at the next token.  */
25536       token = cp_lexer_peek_nth_token (parser->lexer, 2);
25537       /* If the next token is a `}', then we're still done.  An
25538 	 initializer-clause can have a trailing `,' after the
25539 	 initializer-list and before the closing `}'.  */
25540       if (token->type == CPP_CLOSE_BRACE)
25541 	break;
25542 
25543       /* Suppress location wrappers in a long initializer to save memory
25544 	 (14179).  The cutoff is chosen arbitrarily.  */
25545       const unsigned loc_max = 256;
25546       unsigned incr = 1;
25547       if (TREE_CODE (initializer) == CONSTRUCTOR)
25548 	/* Look one level down because it's easy.  Looking deeper would require
25549 	   passing down a nelts pointer, and I don't think multi-level massive
25550 	   initializers are common enough to justify this.  */
25551 	incr = CONSTRUCTOR_NELTS (initializer);
25552       nelts += incr;
25553       if (nelts >= loc_max && (nelts - incr) < loc_max)
25554 	++suppress_location_wrappers;
25555 
25556       /* Consume the `,' token.  */
25557       cp_lexer_consume_token (parser->lexer);
25558     }
25559 
25560   /* The same identifier shall not appear in multiple designators
25561      of a designated-initializer-list.  */
25562   if (first_designator)
25563     {
25564       unsigned int i;
25565       tree designator, val;
25566       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25567 	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25568 	  {
25569 	    if (IDENTIFIER_MARKED (designator))
25570 	      {
25571 		error_at (cp_expr_loc_or_input_loc (val),
25572 			  "%<.%s%> designator used multiple times in "
25573 			  "the same initializer list",
25574 			  IDENTIFIER_POINTER (designator));
25575 		(*v)[i].index = error_mark_node;
25576 	      }
25577 	    else
25578 	      IDENTIFIER_MARKED (designator) = 1;
25579 	  }
25580       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25581 	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25582 	  IDENTIFIER_MARKED (designator) = 0;
25583     }
25584 
25585   suppress_location_wrappers = suppress;
25586 
25587   *designated = first_designator != NULL_TREE;
25588   return v;
25589 }
25590 
25591 /* Classes [gram.class] */
25592 
25593 /* Parse a class-name.
25594 
25595    class-name:
25596      identifier
25597      template-id
25598 
25599    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25600    to indicate that names looked up in dependent types should be
25601    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
25602    keyword has been used to indicate that the name that appears next
25603    is a template.  TAG_TYPE indicates the explicit tag given before
25604    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
25605    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
25606    is the class being defined in a class-head.  If ENUM_OK is TRUE,
25607    enum-names are also accepted.
25608 
25609    Returns the TYPE_DECL representing the class.  */
25610 
25611 static tree
cp_parser_class_name(cp_parser * parser,bool typename_keyword_p,bool template_keyword_p,enum tag_types tag_type,bool check_dependency_p,bool class_head_p,bool is_declaration,bool enum_ok)25612 cp_parser_class_name (cp_parser *parser,
25613 		      bool typename_keyword_p,
25614 		      bool template_keyword_p,
25615 		      enum tag_types tag_type,
25616 		      bool check_dependency_p,
25617 		      bool class_head_p,
25618 		      bool is_declaration,
25619 		      bool enum_ok)
25620 {
25621   tree decl;
25622   tree identifier = NULL_TREE;
25623 
25624   /* All class-names start with an identifier.  */
25625   cp_token *token = cp_lexer_peek_token (parser->lexer);
25626   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25627     {
25628       cp_parser_error (parser, "expected class-name");
25629       return error_mark_node;
25630     }
25631 
25632   /* PARSER->SCOPE can be cleared when parsing the template-arguments
25633      to a template-id, so we save it here.  Consider object scope too,
25634      so that make_typename_type below can use it (cp_parser_template_name
25635      considers object scope also).  This may happen with code like
25636 
25637        p->template A<T>::a()
25638 
25639       where we first want to look up A<T>::a in the class of the object
25640       expression, as per [basic.lookup.classref].  */
25641   tree scope = parser->scope ? parser->scope : parser->context->object_type;
25642   /* This only checks parser->scope to avoid duplicate errors; if
25643      ->object_type is erroneous, go on to give a parse error.  */
25644   if (parser->scope == error_mark_node)
25645     return error_mark_node;
25646 
25647   /* Any name names a type if we're following the `typename' keyword
25648      in a qualified name where the enclosing scope is type-dependent.  */
25649   const bool typename_p = (typename_keyword_p
25650 			   && parser->scope
25651 			   && TYPE_P (parser->scope)
25652 			   && dependent_scope_p (parser->scope));
25653   /* Handle the common case (an identifier, but not a template-id)
25654      efficiently.  */
25655   if (token->type == CPP_NAME
25656       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25657     {
25658       cp_token *identifier_token;
25659       bool ambiguous_p;
25660 
25661       /* Look for the identifier.  */
25662       identifier_token = cp_lexer_peek_token (parser->lexer);
25663       ambiguous_p = identifier_token->error_reported;
25664       identifier = cp_parser_identifier (parser);
25665       /* If the next token isn't an identifier, we are certainly not
25666 	 looking at a class-name.  */
25667       if (identifier == error_mark_node)
25668 	decl = error_mark_node;
25669       /* If we know this is a type-name, there's no need to look it
25670 	 up.  */
25671       else if (typename_p)
25672 	decl = identifier;
25673       else
25674 	{
25675 	  tree ambiguous_decls;
25676 	  /* If we already know that this lookup is ambiguous, then
25677 	     we've already issued an error message; there's no reason
25678 	     to check again.  */
25679 	  if (ambiguous_p)
25680 	    {
25681 	      cp_parser_simulate_error (parser);
25682 	      return error_mark_node;
25683 	    }
25684 	  /* If the next token is a `::', then the name must be a type
25685 	     name.
25686 
25687 	     [basic.lookup.qual]
25688 
25689 	     During the lookup for a name preceding the :: scope
25690 	     resolution operator, object, function, and enumerator
25691 	     names are ignored.  */
25692 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25693 	    tag_type = scope_type;
25694 	  /* Look up the name.  */
25695 	  decl = cp_parser_lookup_name (parser, identifier,
25696 					tag_type,
25697 					/*is_template=*/false,
25698 					/*is_namespace=*/false,
25699 					check_dependency_p,
25700 					&ambiguous_decls,
25701 					identifier_token->location);
25702 	  if (ambiguous_decls)
25703 	    {
25704 	      if (cp_parser_parsing_tentatively (parser))
25705 		cp_parser_simulate_error (parser);
25706 	      return error_mark_node;
25707 	    }
25708 	}
25709     }
25710   else
25711     {
25712       /* Try a template-id.  */
25713       decl = cp_parser_template_id (parser, template_keyword_p,
25714 				    check_dependency_p,
25715 				    tag_type,
25716 				    is_declaration);
25717       if (decl == error_mark_node)
25718 	return error_mark_node;
25719     }
25720 
25721   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
25722 
25723   /* If this is a typename, create a TYPENAME_TYPE.  */
25724   if (typename_p && decl != error_mark_node)
25725     {
25726       decl = make_typename_type (scope, decl, typename_type,
25727 				 /*complain=*/tf_error);
25728       if (decl != error_mark_node)
25729 	decl = TYPE_NAME (decl);
25730     }
25731 
25732   decl = strip_using_decl (decl);
25733 
25734   /* Check to see that it is really the name of a class.  */
25735   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
25736       && identifier_p (TREE_OPERAND (decl, 0))
25737       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25738     /* Situations like this:
25739 
25740 	 template <typename T> struct A {
25741 	   typename T::template X<int>::I i;
25742 	 };
25743 
25744        are problematic.  Is `T::template X<int>' a class-name?  The
25745        standard does not seem to be definitive, but there is no other
25746        valid interpretation of the following `::'.  Therefore, those
25747        names are considered class-names.  */
25748     {
25749       decl = make_typename_type (scope, decl, tag_type, tf_error);
25750       if (decl != error_mark_node)
25751 	decl = TYPE_NAME (decl);
25752     }
25753   else if (TREE_CODE (decl) != TYPE_DECL
25754 	   || TREE_TYPE (decl) == error_mark_node
25755 	   || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
25756 		|| (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
25757 	   /* In Objective-C 2.0, a classname followed by '.' starts a
25758 	      dot-syntax expression, and it's not a type-name.  */
25759 	   || (c_dialect_objc ()
25760 	       && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
25761 	       && objc_is_class_name (decl)))
25762     decl = error_mark_node;
25763 
25764   if (decl == error_mark_node)
25765     cp_parser_error (parser, "expected class-name");
25766   else if (identifier && !parser->scope)
25767     maybe_note_name_used_in_class (identifier, decl);
25768 
25769   return decl;
25770 }
25771 
25772 /* Make sure that any member-function parameters are in scope.
25773    For instance, a function's noexcept-specifier can use the function's
25774    parameters:
25775 
25776    struct S {
25777      void fn (int p) noexcept(noexcept(p));
25778    };
25779 
25780    so we need to make sure name lookup can find them.  This is used
25781    when we delay parsing of the noexcept-specifier.  */
25782 
25783 static void
inject_parm_decls(tree decl)25784 inject_parm_decls (tree decl)
25785 {
25786   begin_scope (sk_function_parms, decl);
25787   tree args = DECL_ARGUMENTS (decl);
25788 
25789   do_push_parm_decls (decl, args, /*nonparms=*/NULL);
25790 
25791   if (args && is_this_parameter (args))
25792     {
25793       gcc_checking_assert (current_class_ptr == NULL_TREE);
25794       current_class_ref = cp_build_fold_indirect_ref (args);
25795       current_class_ptr = args;
25796     }
25797 }
25798 
25799 /* Undo the effects of inject_parm_decls.  */
25800 
25801 static void
pop_injected_parms(void)25802 pop_injected_parms (void)
25803 {
25804   pop_bindings_and_leave_scope ();
25805   current_class_ptr = current_class_ref = NULL_TREE;
25806 }
25807 
25808 /* Parse a class-specifier.
25809 
25810    class-specifier:
25811      class-head { member-specification [opt] }
25812 
25813    Returns the TREE_TYPE representing the class.  */
25814 
25815 static tree
cp_parser_class_specifier_1(cp_parser * parser)25816 cp_parser_class_specifier_1 (cp_parser* parser)
25817 {
25818   tree type;
25819   tree attributes = NULL_TREE;
25820   bool nested_name_specifier_p;
25821   unsigned saved_num_template_parameter_lists;
25822   bool saved_in_function_body;
25823   unsigned char in_statement;
25824   bool in_switch_statement_p;
25825   bool saved_in_unbraced_linkage_specification_p;
25826   tree old_scope = NULL_TREE;
25827   tree scope = NULL_TREE;
25828   cp_token *closing_brace;
25829 
25830   push_deferring_access_checks (dk_no_deferred);
25831 
25832   /* Parse the class-head.  */
25833   type = cp_parser_class_head (parser,
25834 			       &nested_name_specifier_p);
25835   /* If the class-head was a semantic disaster, skip the entire body
25836      of the class.  */
25837   if (!type)
25838     {
25839       cp_parser_skip_to_end_of_block_or_statement (parser);
25840       pop_deferring_access_checks ();
25841       return error_mark_node;
25842     }
25843 
25844   /* Look for the `{'.  */
25845   matching_braces braces;
25846   if (!braces.require_open (parser))
25847     {
25848       pop_deferring_access_checks ();
25849       return error_mark_node;
25850     }
25851 
25852   cp_ensure_no_omp_declare_simd (parser);
25853   cp_ensure_no_oacc_routine (parser);
25854 
25855   /* Issue an error message if type-definitions are forbidden here.  */
25856   bool type_definition_ok_p = cp_parser_check_type_definition (parser);
25857   /* Remember that we are defining one more class.  */
25858   ++parser->num_classes_being_defined;
25859   /* Inside the class, surrounding template-parameter-lists do not
25860      apply.  */
25861   saved_num_template_parameter_lists
25862     = parser->num_template_parameter_lists;
25863   parser->num_template_parameter_lists = 0;
25864   /* We are not in a function body.  */
25865   saved_in_function_body = parser->in_function_body;
25866   parser->in_function_body = false;
25867   /* Or in a loop.  */
25868   in_statement = parser->in_statement;
25869   parser->in_statement = 0;
25870   /* Or in a switch.  */
25871   in_switch_statement_p = parser->in_switch_statement_p;
25872   parser->in_switch_statement_p = false;
25873   /* We are not immediately inside an extern "lang" block.  */
25874   saved_in_unbraced_linkage_specification_p
25875     = parser->in_unbraced_linkage_specification_p;
25876   parser->in_unbraced_linkage_specification_p = false;
25877   /* 'this' from an enclosing non-static member function is unavailable.  */
25878   tree saved_ccp = current_class_ptr;
25879   tree saved_ccr = current_class_ref;
25880   current_class_ptr = NULL_TREE;
25881   current_class_ref = NULL_TREE;
25882 
25883   /* Start the class.  */
25884   if (nested_name_specifier_p)
25885     {
25886       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
25887       /* SCOPE must be a scope nested inside current scope.  */
25888       if (is_nested_namespace (current_namespace,
25889 			       decl_namespace_context (scope)))
25890 	old_scope = push_inner_scope (scope);
25891       else
25892 	nested_name_specifier_p = false;
25893     }
25894   type = begin_class_definition (type);
25895 
25896   if (type == error_mark_node)
25897     /* If the type is erroneous, skip the entire body of the class.  */
25898     cp_parser_skip_to_closing_brace (parser);
25899   else
25900     /* Parse the member-specification.  */
25901     cp_parser_member_specification_opt (parser);
25902 
25903   /* Look for the trailing `}'.  */
25904   closing_brace = braces.require_close (parser);
25905   /* Look for trailing attributes to apply to this class.  */
25906   if (cp_parser_allow_gnu_extensions_p (parser))
25907     attributes = cp_parser_gnu_attributes_opt (parser);
25908   if (type != error_mark_node)
25909     type = finish_struct (type, attributes);
25910   if (nested_name_specifier_p)
25911     pop_inner_scope (old_scope, scope);
25912 
25913   /* We've finished a type definition.  Check for the common syntax
25914      error of forgetting a semicolon after the definition.  We need to
25915      be careful, as we can't just check for not-a-semicolon and be done
25916      with it; the user might have typed:
25917 
25918      class X { } c = ...;
25919      class X { } *p = ...;
25920 
25921      and so forth.  Instead, enumerate all the possible tokens that
25922      might follow this production; if we don't see one of them, then
25923      complain and silently insert the semicolon.  */
25924   {
25925     cp_token *token = cp_lexer_peek_token (parser->lexer);
25926     bool want_semicolon = true;
25927 
25928     if (cp_next_tokens_can_be_std_attribute_p (parser))
25929       /* Don't try to parse c++11 attributes here.  As per the
25930 	 grammar, that should be a task for
25931 	 cp_parser_decl_specifier_seq.  */
25932       want_semicolon = false;
25933 
25934     switch (token->type)
25935       {
25936       case CPP_NAME:
25937       case CPP_SEMICOLON:
25938       case CPP_MULT:
25939       case CPP_AND:
25940       case CPP_OPEN_PAREN:
25941       case CPP_CLOSE_PAREN:
25942       case CPP_COMMA:
25943       case CPP_SCOPE:
25944         want_semicolon = false;
25945         break;
25946 
25947         /* While it's legal for type qualifiers and storage class
25948            specifiers to follow type definitions in the grammar, only
25949            compiler testsuites contain code like that.  Assume that if
25950            we see such code, then what we're really seeing is a case
25951            like:
25952 
25953            class X { }
25954            const <type> var = ...;
25955 
25956            or
25957 
25958            class Y { }
25959            static <type> func (...) ...
25960 
25961            i.e. the qualifier or specifier applies to the next
25962            declaration.  To do so, however, we need to look ahead one
25963            more token to see if *that* token is a type specifier.
25964 
25965 	   This code could be improved to handle:
25966 
25967 	   class Z { }
25968 	   static const <type> var = ...;  */
25969       case CPP_KEYWORD:
25970 	if (keyword_is_decl_specifier (token->keyword))
25971 	  {
25972 	    cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
25973 
25974 	    /* Handling user-defined types here would be nice, but very
25975 	       tricky.  */
25976 	    want_semicolon
25977 	      = (lookahead->type == CPP_KEYWORD
25978 		 && keyword_begins_type_specifier (lookahead->keyword));
25979 	  }
25980 	break;
25981       default:
25982 	break;
25983       }
25984 
25985     /* If we don't have a type, then something is very wrong and we
25986        shouldn't try to do anything clever.  Likewise for not seeing the
25987        closing brace.  */
25988     if (closing_brace && TYPE_P (type) && want_semicolon)
25989       {
25990 	/* Locate the closing brace.  */
25991 	cp_token_position prev
25992 	  = cp_lexer_previous_token_position (parser->lexer);
25993 	cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
25994 	location_t loc = prev_token->location;
25995 
25996 	/* We want to suggest insertion of a ';' immediately *after* the
25997 	   closing brace, so, if we can, offset the location by 1 column.  */
25998 	location_t next_loc = loc;
25999 	if (!linemap_location_from_macro_expansion_p (line_table, loc))
26000 	  next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26001 
26002 	rich_location richloc (line_table, next_loc);
26003 
26004 	/* If we successfully offset the location, suggest the fix-it.  */
26005 	if (next_loc != loc)
26006 	  richloc.add_fixit_insert_before (next_loc, ";");
26007 
26008 	if (CLASSTYPE_DECLARED_CLASS (type))
26009 	  error_at (&richloc,
26010 		    "expected %<;%> after class definition");
26011 	else if (TREE_CODE (type) == RECORD_TYPE)
26012 	  error_at (&richloc,
26013 		    "expected %<;%> after struct definition");
26014 	else if (TREE_CODE (type) == UNION_TYPE)
26015 	  error_at (&richloc,
26016 		    "expected %<;%> after union definition");
26017 	else
26018 	  gcc_unreachable ();
26019 
26020 	/* Unget one token and smash it to look as though we encountered
26021 	   a semicolon in the input stream.  */
26022 	cp_lexer_set_token_position (parser->lexer, prev);
26023 	token = cp_lexer_peek_token (parser->lexer);
26024 	token->type = CPP_SEMICOLON;
26025 	token->keyword = RID_MAX;
26026       }
26027   }
26028 
26029   /* If this class is not itself within the scope of another class,
26030      then we need to parse the bodies of all of the queued function
26031      definitions.  Note that the queued functions defined in a class
26032      are not always processed immediately following the
26033      class-specifier for that class.  Consider:
26034 
26035        struct A {
26036 	 struct B { void f() { sizeof (A); } };
26037        };
26038 
26039      If `f' were processed before the processing of `A' were
26040      completed, there would be no way to compute the size of `A'.
26041      Note that the nesting we are interested in here is lexical --
26042      not the semantic nesting given by TYPE_CONTEXT.  In particular,
26043      for:
26044 
26045        struct A { struct B; };
26046        struct A::B { void f() { } };
26047 
26048      there is no need to delay the parsing of `A::B::f'.  */
26049   if (--parser->num_classes_being_defined == 0)
26050     {
26051       tree decl;
26052       tree class_type = NULL_TREE;
26053       tree pushed_scope = NULL_TREE;
26054       unsigned ix;
26055       cp_default_arg_entry *e;
26056 
26057       if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26058 	{
26059 	  /* Skip default arguments, NSDMIs, etc, in order to improve
26060 	     error recovery (c++/71169, c++/71832).  */
26061 	  vec_safe_truncate (unparsed_funs_with_default_args, 0);
26062 	  vec_safe_truncate (unparsed_nsdmis, 0);
26063 	  vec_safe_truncate (unparsed_funs_with_definitions, 0);
26064 	}
26065 
26066       /* In a first pass, parse default arguments to the functions.
26067 	 Then, in a second pass, parse the bodies of the functions.
26068 	 This two-phased approach handles cases like:
26069 
26070 	    struct S {
26071 	      void f() { g(); }
26072 	      void g(int i = 3);
26073 	    };
26074 
26075 	 */
26076       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26077 	{
26078 	  decl = e->decl;
26079 	  /* If there are default arguments that have not yet been processed,
26080 	     take care of them now.  */
26081 	  if (class_type != e->class_type)
26082 	    {
26083 	      if (pushed_scope)
26084 		pop_scope (pushed_scope);
26085 	      class_type = e->class_type;
26086 	      pushed_scope = push_scope (class_type);
26087 	    }
26088 	  /* Make sure that any template parameters are in scope.  */
26089 	  maybe_begin_member_template_processing (decl);
26090 	  /* Parse the default argument expressions.  */
26091 	  cp_parser_late_parsing_default_args (parser, decl);
26092 	  /* Remove any template parameters from the symbol table.  */
26093 	  maybe_end_member_template_processing ();
26094 	}
26095       vec_safe_truncate (unparsed_funs_with_default_args, 0);
26096 
26097       /* If there are noexcept-specifiers that have not yet been processed,
26098 	 take care of them now.  Do this before processing NSDMIs as they
26099 	 may depend on noexcept-specifiers already having been processed.  */
26100       FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26101 	{
26102 	  tree ctx = DECL_CONTEXT (decl);
26103 	  if (class_type != ctx)
26104 	    {
26105 	      if (pushed_scope)
26106 		pop_scope (pushed_scope);
26107 	      class_type = ctx;
26108 	      pushed_scope = push_scope (class_type);
26109 	    }
26110 
26111 	  tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26112 	  def_parse = TREE_PURPOSE (def_parse);
26113 
26114 	  /* Make sure that any template parameters are in scope.  */
26115 	  maybe_begin_member_template_processing (decl);
26116 
26117 	  /* Make sure that any member-function parameters are in scope.
26118 	     This function doesn't expect ccp to be set.  */
26119 	  current_class_ptr = current_class_ref = NULL_TREE;
26120 	  inject_parm_decls (decl);
26121 
26122 	  /* 'this' is not allowed in static member functions.  */
26123 	  unsigned char local_variables_forbidden_p
26124 	    = parser->local_variables_forbidden_p;
26125 	  if (DECL_THIS_STATIC (decl))
26126 	    parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26127 
26128 	  /* Now we can parse the noexcept-specifier.  */
26129 	  tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26130 
26131 	  if (spec == error_mark_node)
26132 	    spec = NULL_TREE;
26133 
26134 	  /* Update the fn's type directly -- it might have escaped
26135 	     beyond this decl :(  */
26136 	  fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26137 	  /* Update any instantiations we've already created.  We must
26138 	     keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26139 	     so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26140 	     in the pattern.  */
26141 	  for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26142 	    DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26143 	      = spec ? TREE_PURPOSE (spec) : error_mark_node;
26144 
26145 	  /* Restore the state of local_variables_forbidden_p.  */
26146 	  parser->local_variables_forbidden_p = local_variables_forbidden_p;
26147 
26148 	  /* The finish_struct call above performed various override checking,
26149 	     but it skipped unparsed noexcept-specifier operands.  Now that we
26150 	     have resolved them, check again.  */
26151 	  noexcept_override_late_checks (decl);
26152 
26153 	  /* Remove any member-function parameters from the symbol table.  */
26154 	  pop_injected_parms ();
26155 
26156 	  /* Remove any template parameters from the symbol table.  */
26157 	  maybe_end_member_template_processing ();
26158 	}
26159       vec_safe_truncate (unparsed_noexcepts, 0);
26160 
26161       /* Now parse any NSDMIs.  */
26162       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26163 	{
26164 	  if (class_type != DECL_CONTEXT (decl))
26165 	    {
26166 	      if (pushed_scope)
26167 		pop_scope (pushed_scope);
26168 	      class_type = DECL_CONTEXT (decl);
26169 	      pushed_scope = push_scope (class_type);
26170 	    }
26171 	  inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26172 	  cp_parser_late_parsing_nsdmi (parser, decl);
26173 	}
26174       vec_safe_truncate (unparsed_nsdmis, 0);
26175       current_class_ptr = NULL_TREE;
26176       current_class_ref = NULL_TREE;
26177       if (pushed_scope)
26178 	pop_scope (pushed_scope);
26179 
26180       /* Now parse the body of the functions.  */
26181       if (flag_openmp)
26182 	{
26183 	  /* OpenMP UDRs need to be parsed before all other functions.  */
26184 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26185 	    if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26186 	      cp_parser_late_parsing_for_member (parser, decl);
26187 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26188 	    if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26189 	      cp_parser_late_parsing_for_member (parser, decl);
26190 	}
26191       else
26192 	FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26193 	  cp_parser_late_parsing_for_member (parser, decl);
26194       vec_safe_truncate (unparsed_funs_with_definitions, 0);
26195     }
26196 
26197   /* Put back any saved access checks.  */
26198   pop_deferring_access_checks ();
26199 
26200   /* Restore saved state.  */
26201   parser->in_switch_statement_p = in_switch_statement_p;
26202   parser->in_statement = in_statement;
26203   parser->in_function_body = saved_in_function_body;
26204   parser->num_template_parameter_lists
26205     = saved_num_template_parameter_lists;
26206   parser->in_unbraced_linkage_specification_p
26207     = saved_in_unbraced_linkage_specification_p;
26208   current_class_ptr = saved_ccp;
26209   current_class_ref = saved_ccr;
26210 
26211   return type;
26212 }
26213 
26214 static tree
cp_parser_class_specifier(cp_parser * parser)26215 cp_parser_class_specifier (cp_parser* parser)
26216 {
26217   tree ret;
26218   timevar_push (TV_PARSE_STRUCT);
26219   ret = cp_parser_class_specifier_1 (parser);
26220   timevar_pop (TV_PARSE_STRUCT);
26221   return ret;
26222 }
26223 
26224 /* Parse a class-head.
26225 
26226    class-head:
26227      class-key identifier [opt] base-clause [opt]
26228      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26229      class-key nested-name-specifier [opt] template-id
26230        base-clause [opt]
26231 
26232    class-virt-specifier:
26233      final
26234 
26235    GNU Extensions:
26236      class-key attributes identifier [opt] base-clause [opt]
26237      class-key attributes nested-name-specifier identifier base-clause [opt]
26238      class-key attributes nested-name-specifier [opt] template-id
26239        base-clause [opt]
26240 
26241    Upon return BASES is initialized to the list of base classes (or
26242    NULL, if there are none) in the same form returned by
26243    cp_parser_base_clause.
26244 
26245    Returns the TYPE of the indicated class.  Sets
26246    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26247    involving a nested-name-specifier was used, and FALSE otherwise.
26248 
26249    Returns error_mark_node if this is not a class-head.
26250 
26251    Returns NULL_TREE if the class-head is syntactically valid, but
26252    semantically invalid in a way that means we should skip the entire
26253    body of the class.  */
26254 
26255 static tree
cp_parser_class_head(cp_parser * parser,bool * nested_name_specifier_p)26256 cp_parser_class_head (cp_parser* parser,
26257 		      bool* nested_name_specifier_p)
26258 {
26259   tree nested_name_specifier;
26260   enum tag_types class_key;
26261   tree id = NULL_TREE;
26262   tree type = NULL_TREE;
26263   tree attributes;
26264   tree bases;
26265   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26266   bool template_id_p = false;
26267   bool qualified_p = false;
26268   bool invalid_nested_name_p = false;
26269   bool invalid_explicit_specialization_p = false;
26270   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26271   tree pushed_scope = NULL_TREE;
26272   unsigned num_templates;
26273   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26274   /* Assume no nested-name-specifier will be present.  */
26275   *nested_name_specifier_p = false;
26276   /* Assume no template parameter lists will be used in defining the
26277      type.  */
26278   num_templates = 0;
26279   parser->colon_corrects_to_scope_p = false;
26280 
26281   /* Look for the class-key.  */
26282   class_key = cp_parser_class_key (parser);
26283   if (class_key == none_type)
26284     return error_mark_node;
26285 
26286   location_t class_head_start_location = input_location;
26287 
26288   /* Parse the attributes.  */
26289   attributes = cp_parser_attributes_opt (parser);
26290 
26291   /* If the next token is `::', that is invalid -- but sometimes
26292      people do try to write:
26293 
26294        struct ::S {};
26295 
26296      Handle this gracefully by accepting the extra qualifier, and then
26297      issuing an error about it later if this really is a
26298      class-head.  If it turns out just to be an elaborated type
26299      specifier, remain silent.  */
26300   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26301     qualified_p = true;
26302 
26303   push_deferring_access_checks (dk_no_check);
26304 
26305   /* Determine the name of the class.  Begin by looking for an
26306      optional nested-name-specifier.  */
26307   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26308   nested_name_specifier
26309     = cp_parser_nested_name_specifier_opt (parser,
26310 					   /*typename_keyword_p=*/false,
26311 					   /*check_dependency_p=*/false,
26312 					   /*type_p=*/true,
26313 					   /*is_declaration=*/false);
26314   /* If there was a nested-name-specifier, then there *must* be an
26315      identifier.  */
26316 
26317   cp_token *bad_template_keyword = NULL;
26318 
26319   if (nested_name_specifier)
26320     {
26321       type_start_token = cp_lexer_peek_token (parser->lexer);
26322       /* Although the grammar says `identifier', it really means
26323 	 `class-name' or `template-name'.  You are only allowed to
26324 	 define a class that has already been declared with this
26325 	 syntax.
26326 
26327 	 The proposed resolution for Core Issue 180 says that wherever
26328 	 you see `class T::X' you should treat `X' as a type-name.
26329 
26330 	 It is OK to define an inaccessible class; for example:
26331 
26332 	   class A { class B; };
26333 	   class A::B {};
26334 
26335 	 We do not know if we will see a class-name, or a
26336 	 template-name.  We look for a class-name first, in case the
26337 	 class-name is a template-id; if we looked for the
26338 	 template-name first we would stop after the template-name.  */
26339       cp_parser_parse_tentatively (parser);
26340       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26341 	bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26342       type = cp_parser_class_name (parser,
26343 				   /*typename_keyword_p=*/false,
26344 				   /*template_keyword_p=*/false,
26345 				   class_type,
26346 				   /*check_dependency_p=*/false,
26347 				   /*class_head_p=*/true,
26348 				   /*is_declaration=*/false);
26349       /* If that didn't work, ignore the nested-name-specifier.  */
26350       if (!cp_parser_parse_definitely (parser))
26351 	{
26352 	  invalid_nested_name_p = true;
26353 	  type_start_token = cp_lexer_peek_token (parser->lexer);
26354 	  id = cp_parser_identifier (parser);
26355 	  if (id == error_mark_node)
26356 	    id = NULL_TREE;
26357 	}
26358       /* If we could not find a corresponding TYPE, treat this
26359 	 declaration like an unqualified declaration.  */
26360       if (type == error_mark_node)
26361 	nested_name_specifier = NULL_TREE;
26362       /* Otherwise, count the number of templates used in TYPE and its
26363 	 containing scopes.  */
26364       else
26365 	num_templates = num_template_headers_for_class (TREE_TYPE (type));
26366     }
26367   /* Otherwise, the identifier is optional.  */
26368   else
26369     {
26370       /* We don't know whether what comes next is a template-id,
26371 	 an identifier, or nothing at all.  */
26372       cp_parser_parse_tentatively (parser);
26373       /* Check for a template-id.  */
26374       type_start_token = cp_lexer_peek_token (parser->lexer);
26375       id = cp_parser_template_id (parser,
26376 				  /*template_keyword_p=*/false,
26377 				  /*check_dependency_p=*/true,
26378 				  class_key,
26379 				  /*is_declaration=*/true);
26380       /* If that didn't work, it could still be an identifier.  */
26381       if (!cp_parser_parse_definitely (parser))
26382 	{
26383 	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26384 	    {
26385 	      type_start_token = cp_lexer_peek_token (parser->lexer);
26386 	      id = cp_parser_identifier (parser);
26387 	    }
26388 	  else
26389 	    id = NULL_TREE;
26390 	}
26391       else
26392 	{
26393 	  template_id_p = true;
26394 	  ++num_templates;
26395 	}
26396     }
26397 
26398   pop_deferring_access_checks ();
26399 
26400   if (id)
26401     {
26402       cp_parser_check_for_invalid_template_id (parser, id,
26403 					       class_key,
26404                                                type_start_token->location);
26405     }
26406   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26407 
26408   /* If it's not a `:' or a `{' then we can't really be looking at a
26409      class-head, since a class-head only appears as part of a
26410      class-specifier.  We have to detect this situation before calling
26411      xref_tag, since that has irreversible side-effects.  */
26412   if (!cp_parser_next_token_starts_class_definition_p (parser))
26413     {
26414       cp_parser_error (parser, "expected %<{%> or %<:%>");
26415       type = error_mark_node;
26416       goto out;
26417     }
26418 
26419   /* At this point, we're going ahead with the class-specifier, even
26420      if some other problem occurs.  */
26421   cp_parser_commit_to_tentative_parse (parser);
26422   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26423     {
26424       cp_parser_error (parser,
26425                        "cannot specify %<override%> for a class");
26426       type = error_mark_node;
26427       goto out;
26428     }
26429   /* Issue the error about the overly-qualified name now.  */
26430   if (qualified_p)
26431     {
26432       cp_parser_error (parser,
26433 		       "global qualification of class name is invalid");
26434       type = error_mark_node;
26435       goto out;
26436     }
26437   else if (invalid_nested_name_p)
26438     {
26439       cp_parser_error (parser,
26440 		       "qualified name does not name a class");
26441       type = error_mark_node;
26442       goto out;
26443     }
26444   else if (nested_name_specifier)
26445     {
26446       tree scope;
26447 
26448       if (bad_template_keyword)
26449 	/* [temp.names]: in a qualified-id formed by a class-head-name, the
26450 	   keyword template shall not appear at the top level.  */
26451 	pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26452 		 "keyword %<template%> not allowed in class-head-name");
26453 
26454       /* Reject typedef-names in class heads.  */
26455       if (!DECL_IMPLICIT_TYPEDEF_P (type))
26456 	{
26457 	  error_at (type_start_token->location,
26458 		    "invalid class name in declaration of %qD",
26459 		    type);
26460 	  type = NULL_TREE;
26461 	  goto done;
26462 	}
26463 
26464       /* Figure out in what scope the declaration is being placed.  */
26465       scope = current_scope ();
26466       /* If that scope does not contain the scope in which the
26467 	 class was originally declared, the program is invalid.  */
26468       if (scope && !is_ancestor (scope, nested_name_specifier))
26469 	{
26470 	  if (at_namespace_scope_p ())
26471 	    error_at (type_start_token->location,
26472 		      "declaration of %qD in namespace %qD which does not "
26473 		      "enclose %qD",
26474 		      type, scope, nested_name_specifier);
26475 	  else
26476 	    error_at (type_start_token->location,
26477 		      "declaration of %qD in %qD which does not enclose %qD",
26478 		      type, scope, nested_name_specifier);
26479 	  type = NULL_TREE;
26480 	  goto done;
26481 	}
26482       /* [dcl.meaning]
26483 
26484 	 A declarator-id shall not be qualified except for the
26485 	 definition of a ... nested class outside of its class
26486 	 ... [or] the definition or explicit instantiation of a
26487 	 class member of a namespace outside of its namespace.  */
26488       if (scope == nested_name_specifier)
26489 	permerror (nested_name_specifier_token_start->location,
26490 		   "extra qualification not allowed");
26491     }
26492   /* An explicit-specialization must be preceded by "template <>".  If
26493      it is not, try to recover gracefully.  */
26494   if (at_namespace_scope_p ()
26495       && parser->num_template_parameter_lists == 0
26496       && !processing_template_parmlist
26497       && template_id_p)
26498     {
26499       /* Build a location of this form:
26500            struct typename <ARGS>
26501            ^~~~~~~~~~~~~~~~~~~~~~
26502          with caret==start at the start token, and
26503          finishing at the end of the type.  */
26504       location_t reported_loc
26505         = make_location (class_head_start_location,
26506                          class_head_start_location,
26507                          get_finish (type_start_token->location));
26508       rich_location richloc (line_table, reported_loc);
26509       richloc.add_fixit_insert_before (class_head_start_location,
26510                                        "template <> ");
26511       error_at (&richloc,
26512 		"an explicit specialization must be preceded by"
26513 		" %<template <>%>");
26514       invalid_explicit_specialization_p = true;
26515       /* Take the same action that would have been taken by
26516 	 cp_parser_explicit_specialization.  */
26517       ++parser->num_template_parameter_lists;
26518       begin_specialization ();
26519     }
26520   /* There must be no "return" statements between this point and the
26521      end of this function; set "type "to the correct return value and
26522      use "goto done;" to return.  */
26523   /* Make sure that the right number of template parameters were
26524      present.  */
26525   if (!cp_parser_check_template_parameters (parser, num_templates,
26526 					    template_id_p,
26527 					    type_start_token->location,
26528 					    /*declarator=*/NULL))
26529     {
26530       /* If something went wrong, there is no point in even trying to
26531 	 process the class-definition.  */
26532       type = NULL_TREE;
26533       goto done;
26534     }
26535 
26536   /* Look up the type.  */
26537   if (template_id_p)
26538     {
26539       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26540 	  && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26541 	      || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26542 	{
26543 	  error_at (type_start_token->location,
26544 		    "function template %qD redeclared as a class template", id);
26545 	  type = error_mark_node;
26546 	}
26547       else
26548 	{
26549 	  type = TREE_TYPE (id);
26550 	  type = maybe_process_partial_specialization (type);
26551 
26552 	  /* Check the scope while we still know whether or not we had a
26553 	     nested-name-specifier.  */
26554 	  if (type != error_mark_node)
26555 	    check_unqualified_spec_or_inst (type, type_start_token->location);
26556 	}
26557       if (nested_name_specifier)
26558 	pushed_scope = push_scope (nested_name_specifier);
26559     }
26560   else if (nested_name_specifier)
26561     {
26562       type = TREE_TYPE (type);
26563 
26564       /* Given:
26565 
26566 	    template <typename T> struct S { struct T };
26567 	    template <typename T> struct S<T>::T { };
26568 
26569 	 we will get a TYPENAME_TYPE when processing the definition of
26570 	 `S::T'.  We need to resolve it to the actual type before we
26571 	 try to define it.  */
26572       if (TREE_CODE (type) == TYPENAME_TYPE)
26573 	{
26574 	  type = resolve_typename_type (type, /*only_current_p=*/false);
26575 	  if (TREE_CODE (type) == TYPENAME_TYPE)
26576 	    {
26577 	      cp_parser_error (parser, "could not resolve typename type");
26578 	      type = error_mark_node;
26579 	    }
26580 	}
26581 
26582       type = maybe_process_partial_specialization (type);
26583       if (type == error_mark_node)
26584 	{
26585 	  type = NULL_TREE;
26586 	  goto done;
26587 	}
26588 
26589       /* Enter the scope indicated by the nested-name-specifier.  */
26590       pushed_scope = push_scope (nested_name_specifier);
26591       /* Get the canonical version of this type.  */
26592       type = TYPE_MAIN_DECL (type);
26593       /* Call push_template_decl if it seems like we should be defining a
26594 	 template either from the template headers or the type we're
26595 	 defining, so that we diagnose both extra and missing headers.  */
26596       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26597 	   || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26598 	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26599 	{
26600 	  type = push_template_decl (type);
26601 	  if (type == error_mark_node)
26602 	    {
26603 	      type = NULL_TREE;
26604 	      goto done;
26605 	    }
26606 	}
26607 
26608       type = TREE_TYPE (type);
26609       *nested_name_specifier_p = true;
26610     }
26611   else      /* The name is not a nested name.  */
26612     {
26613       /* If the class was unnamed, create a dummy name.  */
26614       if (!id)
26615 	id = make_anon_name ();
26616       TAG_how how = (parser->in_type_id_in_expr_p
26617 		     ? TAG_how::INNERMOST_NON_CLASS
26618 		     : TAG_how::CURRENT_ONLY);
26619       type = xref_tag (class_key, id, how,
26620 		       parser->num_template_parameter_lists);
26621     }
26622 
26623   /* Diagnose class/struct/union mismatches.  */
26624   cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26625 			     true, true);
26626 
26627   /* Indicate whether this class was declared as a `class' or as a
26628      `struct'.  */
26629   if (TREE_CODE (type) == RECORD_TYPE)
26630     CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26631 
26632   /* If this type was already complete, and we see another definition,
26633      that's an error.  Likewise if the type is already being defined:
26634      this can happen, eg, when it's defined from within an expression
26635      (c++/84605).  */
26636   if (type != error_mark_node
26637       && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
26638     {
26639       error_at (type_start_token->location, "redefinition of %q#T",
26640 		type);
26641       inform (location_of (type), "previous definition of %q#T",
26642 	      type);
26643       type = NULL_TREE;
26644       goto done;
26645     }
26646   else if (type == error_mark_node)
26647     type = NULL_TREE;
26648 
26649   if (type)
26650     {
26651       if (current_lambda_expr ()
26652 	  && uses_parameter_packs (attributes))
26653 	{
26654 	  /* In a lambda this should work, but doesn't currently.  */
26655 	  sorry ("unexpanded parameter pack in local class in lambda");
26656 	  attributes = NULL_TREE;
26657 	}
26658 
26659       /* Apply attributes now, before any use of the class as a template
26660 	 argument in its base list.  */
26661       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
26662       fixup_attribute_variants (type);
26663     }
26664 
26665   /* Associate constraints with the type.  */
26666   if (flag_concepts)
26667     type = associate_classtype_constraints (type);
26668 
26669   /* We will have entered the scope containing the class; the names of
26670      base classes should be looked up in that context.  For example:
26671 
26672        struct A { struct B {}; struct C; };
26673        struct A::C : B {};
26674 
26675      is valid.  */
26676 
26677   /* Get the list of base-classes, if there is one.  Defer access checking
26678      until the entire list has been seen, as per [class.access.general].  */
26679   push_deferring_access_checks (dk_deferred);
26680   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26681     {
26682       if (type)
26683 	pushclass (type);
26684       bases = cp_parser_base_clause (parser);
26685       if (type)
26686 	popclass ();
26687     }
26688   else
26689     bases = NULL_TREE;
26690 
26691   /* If we're really defining a class, process the base classes.
26692      If they're invalid, fail.  */
26693   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26694     xref_basetypes (type, bases);
26695 
26696   /* Now that all bases have been seen and attached to the class, check
26697      accessibility of the types named in the base-clause.  This must be
26698      done relative to the class scope, so that we accept e.g.
26699 
26700        struct A { protected: struct B {}; };
26701        struct C : A::B, A {}; // OK: A::B is accessible via base A
26702 
26703      as per [class.access.general].  */
26704   if (type)
26705     pushclass (type);
26706   pop_to_parent_deferring_access_checks ();
26707   if (type)
26708     popclass ();
26709 
26710  done:
26711   /* Leave the scope given by the nested-name-specifier.  We will
26712      enter the class scope itself while processing the members.  */
26713   if (pushed_scope)
26714     pop_scope (pushed_scope);
26715 
26716   if (invalid_explicit_specialization_p)
26717     {
26718       end_specialization ();
26719       --parser->num_template_parameter_lists;
26720     }
26721 
26722   if (type)
26723     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
26724   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
26725     CLASSTYPE_FINAL (type) = 1;
26726  out:
26727   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26728   return type;
26729 }
26730 
26731 /* Parse a class-key.
26732 
26733    class-key:
26734      class
26735      struct
26736      union
26737 
26738    Returns the kind of class-key specified, or none_type to indicate
26739    error.  */
26740 
26741 static enum tag_types
cp_parser_class_key(cp_parser * parser)26742 cp_parser_class_key (cp_parser* parser)
26743 {
26744   cp_token *token;
26745   enum tag_types tag_type;
26746 
26747   /* Look for the class-key.  */
26748   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
26749   if (!token)
26750     return none_type;
26751 
26752   /* Check to see if the TOKEN is a class-key.  */
26753   tag_type = cp_parser_token_is_class_key (token);
26754   if (!tag_type)
26755     cp_parser_error (parser, "expected class-key");
26756   return tag_type;
26757 }
26758 
26759 /* Parse a type-parameter-key.
26760 
26761    type-parameter-key:
26762      class
26763      typename
26764  */
26765 
26766 static void
cp_parser_type_parameter_key(cp_parser * parser)26767 cp_parser_type_parameter_key (cp_parser* parser)
26768 {
26769   /* Look for the type-parameter-key.  */
26770   enum tag_types tag_type = none_type;
26771   cp_token *token = cp_lexer_peek_token (parser->lexer);
26772   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
26773     {
26774       cp_lexer_consume_token (parser->lexer);
26775       if (pedantic && tag_type == typename_type
26776 	  && cxx_dialect < cxx17)
26777 	/* typename is not allowed in a template template parameter
26778 	   by the standard until C++17.  */
26779 	pedwarn (token->location, OPT_Wc__17_extensions,
26780 		 "ISO C++ forbids typename key in template template parameter;"
26781 		 " use %<-std=c++17%> or %<-std=gnu++17%>");
26782     }
26783   else
26784     cp_parser_error (parser, "expected %<class%> or %<typename%>");
26785 
26786   return;
26787 }
26788 
26789 /* Parse an (optional) member-specification.
26790 
26791    member-specification:
26792      member-declaration member-specification [opt]
26793      access-specifier : member-specification [opt]  */
26794 
26795 static void
cp_parser_member_specification_opt(cp_parser * parser)26796 cp_parser_member_specification_opt (cp_parser* parser)
26797 {
26798   while (true)
26799     {
26800       cp_token *token;
26801       enum rid keyword;
26802 
26803       /* Peek at the next token.  */
26804       token = cp_lexer_peek_token (parser->lexer);
26805       /* If it's a `}', or EOF then we've seen all the members.  */
26806       if (token->type == CPP_CLOSE_BRACE
26807 	  || token->type == CPP_EOF
26808 	  || token->type == CPP_PRAGMA_EOL)
26809 	break;
26810 
26811       /* See if this token is a keyword.  */
26812       keyword = token->keyword;
26813       switch (keyword)
26814 	{
26815 	case RID_PUBLIC:
26816 	case RID_PROTECTED:
26817 	case RID_PRIVATE:
26818 	  /* Consume the access-specifier.  */
26819 	  cp_lexer_consume_token (parser->lexer);
26820 	  /* Remember which access-specifier is active.  */
26821 	  current_access_specifier = token->u.value;
26822 	  /* Look for the `:'.  */
26823 	  cp_parser_require (parser, CPP_COLON, RT_COLON);
26824 	  break;
26825 
26826 	default:
26827 	  /* Accept #pragmas at class scope.  */
26828 	  if (token->type == CPP_PRAGMA)
26829 	    {
26830 	      cp_parser_pragma (parser, pragma_member, NULL);
26831 	      break;
26832 	    }
26833 
26834 	  /* Otherwise, the next construction must be a
26835 	     member-declaration.  */
26836 	  cp_parser_member_declaration (parser);
26837 	}
26838     }
26839 }
26840 
26841 /* Parse a member-declaration.
26842 
26843    member-declaration:
26844      decl-specifier-seq [opt] member-declarator-list [opt] ;
26845      function-definition ; [opt]
26846      :: [opt] nested-name-specifier template [opt] unqualified-id ;
26847      using-declaration
26848      template-declaration
26849      alias-declaration
26850 
26851    member-declarator-list:
26852      member-declarator
26853      member-declarator-list , member-declarator
26854 
26855    member-declarator:
26856      declarator pure-specifier [opt]
26857      declarator constant-initializer [opt]
26858      identifier [opt] : constant-expression
26859 
26860    GNU Extensions:
26861 
26862    member-declaration:
26863      __extension__ member-declaration
26864 
26865    member-declarator:
26866      declarator attributes [opt] pure-specifier [opt]
26867      declarator attributes [opt] constant-initializer [opt]
26868      identifier [opt] attributes [opt] : constant-expression
26869 
26870    C++0x Extensions:
26871 
26872    member-declaration:
26873      static_assert-declaration  */
26874 
26875 static void
cp_parser_member_declaration(cp_parser * parser)26876 cp_parser_member_declaration (cp_parser* parser)
26877 {
26878   cp_decl_specifier_seq decl_specifiers;
26879   tree prefix_attributes;
26880   tree decl;
26881   int declares_class_or_enum;
26882   bool friend_p;
26883   cp_token *token = NULL;
26884   cp_token *decl_spec_token_start = NULL;
26885   cp_token *initializer_token_start = NULL;
26886   int saved_pedantic;
26887   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26888 
26889   /* Check for the `__extension__' keyword.  */
26890   if (cp_parser_extension_opt (parser, &saved_pedantic))
26891     {
26892       /* Recurse.  */
26893       cp_parser_member_declaration (parser);
26894       /* Restore the old value of the PEDANTIC flag.  */
26895       pedantic = saved_pedantic;
26896 
26897       return;
26898     }
26899 
26900   /* Check for a template-declaration.  */
26901   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26902     {
26903       /* An explicit specialization here is an error condition, and we
26904 	 expect the specialization handler to detect and report this.  */
26905       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
26906 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
26907 	cp_parser_explicit_specialization (parser);
26908       else
26909 	cp_parser_template_declaration (parser, /*member_p=*/true);
26910 
26911       return;
26912     }
26913   /* Check for a template introduction.  */
26914   else if (cp_parser_template_declaration_after_export (parser, true))
26915     return;
26916 
26917   /* Check for a using-declaration.  */
26918   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26919     {
26920       if (cxx_dialect < cxx11)
26921 	/* Parse the using-declaration.  */
26922 	cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
26923       else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
26924 	cp_parser_using_enum (parser);
26925       else
26926 	{
26927 	  tree decl;
26928 	  bool alias_decl_expected;
26929 	  cp_parser_parse_tentatively (parser);
26930 	  decl = cp_parser_alias_declaration (parser);
26931 	  /* Note that if we actually see the '=' token after the
26932 	     identifier, cp_parser_alias_declaration commits the
26933 	     tentative parse.  In that case, we really expect an
26934 	     alias-declaration.  Otherwise, we expect a using
26935 	     declaration.  */
26936 	  alias_decl_expected =
26937 	    !cp_parser_uncommitted_to_tentative_parse_p (parser);
26938 	  cp_parser_parse_definitely (parser);
26939 
26940 	  if (alias_decl_expected)
26941 	    finish_member_declaration (decl);
26942 	  else
26943 	    cp_parser_using_declaration (parser,
26944 					 /*access_declaration_p=*/false);
26945 	}
26946       return;
26947     }
26948 
26949   /* Check for @defs.  */
26950   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
26951     {
26952       tree ivar, member;
26953       tree ivar_chains = cp_parser_objc_defs_expression (parser);
26954       ivar = ivar_chains;
26955       while (ivar)
26956 	{
26957 	  member = ivar;
26958 	  ivar = TREE_CHAIN (member);
26959 	  TREE_CHAIN (member) = NULL_TREE;
26960 	  finish_member_declaration (member);
26961 	}
26962       return;
26963     }
26964 
26965   /* If the next token is `static_assert' we have a static assertion.  */
26966   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
26967     {
26968       cp_parser_static_assert (parser, /*member_p=*/true);
26969       return;
26970     }
26971 
26972   parser->colon_corrects_to_scope_p = false;
26973 
26974   cp_omp_declare_simd_data odsd;
26975   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
26976     goto out;
26977 
26978   /* Parse the decl-specifier-seq.  */
26979   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26980   cp_parser_decl_specifier_seq (parser,
26981 				(CP_PARSER_FLAGS_OPTIONAL
26982 				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
26983 				&decl_specifiers,
26984 				&declares_class_or_enum);
26985 
26986   if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
26987     cp_parser_handle_directive_omp_attributes (parser,
26988 					       &decl_specifiers.attributes,
26989 					       &odsd, true);
26990 
26991   /* Check for an invalid type-name.  */
26992   if (!decl_specifiers.any_type_specifiers_p
26993       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26994     goto out;
26995   /* If there is no declarator, then the decl-specifier-seq should
26996      specify a type.  */
26997   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26998     {
26999       /* If there was no decl-specifier-seq, and the next token is a
27000 	 `;', then we have something like:
27001 
27002 	   struct S { ; };
27003 
27004 	 [class.mem]
27005 
27006 	 Each member-declaration shall declare at least one member
27007 	 name of the class.  */
27008       if (!decl_specifiers.any_specifiers_p)
27009 	{
27010 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
27011 	  if (!in_system_header_at (token->location))
27012 	    {
27013 	      gcc_rich_location richloc (token->location);
27014 	      richloc.add_fixit_remove ();
27015 	      pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27016 	    }
27017 	}
27018       else
27019 	{
27020 	  /* See if this declaration is a friend.  */
27021 	  friend_p = cp_parser_friend_p (&decl_specifiers);
27022 	  /* If there were decl-specifiers, check to see if there was
27023 	     a class-declaration.  */
27024 	  tree type = check_tag_decl (&decl_specifiers,
27025 				      /*explicit_type_instantiation_p=*/false);
27026 	  /* Nested classes have already been added to the class, but
27027 	     a `friend' needs to be explicitly registered.  */
27028 	  if (friend_p)
27029 	    {
27030 	      /* If the `friend' keyword was present, the friend must
27031 		 be introduced with a class-key.  */
27032 	       if (!declares_class_or_enum && cxx_dialect < cxx11)
27033 		 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27034 			  "in C++03 a class-key must be used "
27035 			  "when declaring a friend");
27036 	       /* In this case:
27037 
27038 		    template <typename T> struct A {
27039 		      friend struct A<T>::B;
27040 		    };
27041 
27042 		  A<T>::B will be represented by a TYPENAME_TYPE, and
27043 		  therefore not recognized by check_tag_decl.  */
27044 	       if (!type)
27045 		 {
27046 		   type = decl_specifiers.type;
27047 		   if (type && TREE_CODE (type) == TYPE_DECL)
27048 		     type = TREE_TYPE (type);
27049 		 }
27050 	       /* Warn if an attribute cannot appear here, as per
27051 		  [dcl.attr.grammar]/5.  But not when declares_class_or_enum:
27052 		  we ignore attributes in elaborated-type-specifiers.  */
27053 	       if (!declares_class_or_enum
27054 		   && cxx11_attribute_p (decl_specifiers.attributes))
27055 		 {
27056 		   decl_specifiers.attributes = NULL_TREE;
27057 		   if (warning_at (decl_spec_token_start->location,
27058 				   OPT_Wattributes, "attribute ignored"))
27059 		     inform (decl_spec_token_start->location, "an attribute "
27060 			     "that appertains to a friend declaration that "
27061 			     "is not a definition is ignored");
27062 		 }
27063 	       if (!type || !TYPE_P (type))
27064 		 error_at (decl_spec_token_start->location,
27065 			   "friend declaration does not name a class or "
27066 			   "function");
27067 	       else
27068 		 make_friend_class (current_class_type, type,
27069 				    /*complain=*/true);
27070 	    }
27071 	  /* If there is no TYPE, an error message will already have
27072 	     been issued.  */
27073 	  else if (!type || type == error_mark_node)
27074 	    ;
27075 	  /* An anonymous aggregate has to be handled specially; such
27076 	     a declaration really declares a data member (with a
27077 	     particular type), as opposed to a nested class.  */
27078 	  else if (ANON_AGGR_TYPE_P (type))
27079 	    {
27080 	      /* C++11 9.5/6.  */
27081 	      if (decl_specifiers.storage_class != sc_none)
27082 		error_at (decl_spec_token_start->location,
27083 			  "a storage class on an anonymous aggregate "
27084 			  "in class scope is not allowed");
27085 
27086 	      /* Remove constructors and such from TYPE, now that we
27087 		 know it is an anonymous aggregate.  */
27088 	      fixup_anonymous_aggr (type);
27089 	      /* And make the corresponding data member.  */
27090 	      decl = build_decl (decl_spec_token_start->location,
27091 				 FIELD_DECL, NULL_TREE, type);
27092 	      /* Add it to the class.  */
27093 	      finish_member_declaration (decl);
27094 	    }
27095 	  else
27096 	    cp_parser_check_access_in_redeclaration
27097 					      (TYPE_NAME (type),
27098 					       decl_spec_token_start->location);
27099 	}
27100     }
27101   else
27102     {
27103       bool assume_semicolon = false;
27104 
27105       /* Clear attributes from the decl_specifiers but keep them
27106 	 around as prefix attributes that apply them to the entity
27107 	 being declared.  */
27108       prefix_attributes = decl_specifiers.attributes;
27109       decl_specifiers.attributes = NULL_TREE;
27110       if (parser->omp_declare_simd
27111 	  && (parser->omp_declare_simd->attribs[0]
27112 	      == &decl_specifiers.attributes))
27113 	parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27114 
27115       /* See if these declarations will be friends.  */
27116       friend_p = cp_parser_friend_p (&decl_specifiers);
27117 
27118       /* Keep going until we hit the `;' at the end of the
27119 	 declaration.  */
27120       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27121 	{
27122 	  tree attributes = NULL_TREE;
27123 	  tree first_attribute;
27124 	  tree initializer;
27125 	  bool named_bitfld = false;
27126 
27127 	  /* Peek at the next token.  */
27128 	  token = cp_lexer_peek_token (parser->lexer);
27129 
27130 	  /* The following code wants to know early if it is a bit-field
27131 	     or some other declaration.  Attributes can appear before
27132 	     the `:' token.  Skip over them without consuming any tokens
27133 	     to peek if they are followed by `:'.  */
27134 	  if (cp_next_tokens_can_be_attribute_p (parser)
27135 	      || (token->type == CPP_NAME
27136 		  && cp_nth_tokens_can_be_attribute_p (parser, 2)
27137 		  && (named_bitfld = true)))
27138 	    {
27139 	      size_t n
27140 		= cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27141 	      token = cp_lexer_peek_nth_token (parser->lexer, n);
27142 	    }
27143 
27144 	  /* Check for a bitfield declaration.  */
27145 	  if (token->type == CPP_COLON
27146 	      || (token->type == CPP_NAME
27147 		  && token == cp_lexer_peek_token (parser->lexer)
27148 		  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27149 		  && (named_bitfld = true)))
27150 	    {
27151 	      tree identifier;
27152 	      tree width;
27153 	      tree late_attributes = NULL_TREE;
27154 	      location_t id_location
27155 		= cp_lexer_peek_token (parser->lexer)->location;
27156 
27157 	      if (named_bitfld)
27158 		identifier = cp_parser_identifier (parser);
27159 	      else
27160 		identifier = NULL_TREE;
27161 
27162 	      /* Look for attributes that apply to the bitfield.  */
27163 	      attributes = cp_parser_attributes_opt (parser);
27164 
27165 	      /* Consume the `:' token.  */
27166 	      cp_lexer_consume_token (parser->lexer);
27167 
27168 	      /* Get the width of the bitfield.  */
27169 	      width = cp_parser_constant_expression (parser, false, NULL,
27170 						     cxx_dialect >= cxx11);
27171 
27172 	      /* In C++20 and as extension for C++11 and above we allow
27173 		 default member initializers for bit-fields.  */
27174 	      initializer = NULL_TREE;
27175 	      if (cxx_dialect >= cxx11
27176 		  && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27177 		      || cp_lexer_next_token_is (parser->lexer,
27178 						 CPP_OPEN_BRACE)))
27179 		{
27180 		  location_t loc
27181 		    = cp_lexer_peek_token (parser->lexer)->location;
27182 		  if (cxx_dialect < cxx20
27183 		      && identifier != NULL_TREE)
27184 		    pedwarn (loc, OPT_Wc__20_extensions,
27185 			     "default member initializers for bit-fields "
27186 			     "only available with %<-std=c++20%> or "
27187 			     "%<-std=gnu++20%>");
27188 
27189 		  initializer = cp_parser_save_nsdmi (parser);
27190 		  if (identifier == NULL_TREE)
27191 		    {
27192 		      error_at (loc, "default member initializer for "
27193 				     "unnamed bit-field");
27194 		      initializer = NULL_TREE;
27195 		    }
27196 		}
27197 	      else
27198 		{
27199 		  /* Look for attributes that apply to the bitfield after
27200 		     the `:' token and width.  This is where GCC used to
27201 		     parse attributes in the past, pedwarn if there is
27202 		     a std attribute.  */
27203 		  if (cp_next_tokens_can_be_std_attribute_p (parser))
27204 		    pedwarn (input_location, OPT_Wpedantic,
27205 			     "ISO C++ allows bit-field attributes only "
27206 			     "before the %<:%> token");
27207 
27208 		  late_attributes = cp_parser_attributes_opt (parser);
27209 		}
27210 
27211 	      attributes = attr_chainon (attributes, late_attributes);
27212 
27213 	      /* Remember which attributes are prefix attributes and
27214 		 which are not.  */
27215 	      first_attribute = attributes;
27216 	      /* Combine the attributes.  */
27217 	      attributes = attr_chainon (prefix_attributes, attributes);
27218 
27219 	      /* Create the bitfield declaration.  */
27220 	      decl = grokbitfield (identifier
27221 				   ? make_id_declarator (NULL_TREE,
27222 							 identifier,
27223 							 sfk_none,
27224 							 id_location)
27225 				   : NULL,
27226 				   &decl_specifiers,
27227 				   width, initializer,
27228 				   attributes);
27229 	    }
27230 	  else
27231 	    {
27232 	      cp_declarator *declarator;
27233 	      tree asm_specification;
27234 	      int ctor_dtor_or_conv_p;
27235 	      bool static_p = (decl_specifiers.storage_class == sc_static);
27236 	      cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27237 	      /* We can't delay parsing for friends,
27238 		 alias-declarations, and typedefs, even though the
27239 		 standard seems to require it.  */
27240 	      if (!friend_p
27241 		  && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27242 		flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27243 
27244 	      /* Parse the declarator.  */
27245 	      declarator
27246 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27247 					flags,
27248 					&ctor_dtor_or_conv_p,
27249 					/*parenthesized_p=*/NULL,
27250 					/*member_p=*/true,
27251 					friend_p, static_p);
27252 
27253 	      /* If something went wrong parsing the declarator, make sure
27254 		 that we at least consume some tokens.  */
27255 	      if (declarator == cp_error_declarator)
27256 		{
27257 		  /* Skip to the end of the statement.  */
27258 		  cp_parser_skip_to_end_of_statement (parser);
27259 		  /* If the next token is not a semicolon, that is
27260 		     probably because we just skipped over the body of
27261 		     a function.  So, we consume a semicolon if
27262 		     present, but do not issue an error message if it
27263 		     is not present.  */
27264 		  if (cp_lexer_next_token_is (parser->lexer,
27265 					      CPP_SEMICOLON))
27266 		    cp_lexer_consume_token (parser->lexer);
27267 		  goto out;
27268 		}
27269 
27270 	      /* Handle class-scope non-template C++17 deduction guides.  */
27271 	      cp_parser_maybe_adjust_declarator_for_dguide (parser,
27272 							    &decl_specifiers,
27273 							    declarator,
27274 							    &ctor_dtor_or_conv_p);
27275 
27276 	      if (declares_class_or_enum & 2)
27277 		cp_parser_check_for_definition_in_return_type
27278 					    (declarator, decl_specifiers.type,
27279 					     decl_specifiers.locations[ds_type_spec]);
27280 
27281 	      /* Look for an asm-specification.  */
27282 	      asm_specification = cp_parser_asm_specification_opt (parser);
27283 	      /* Look for attributes that apply to the declaration.  */
27284 	      attributes = cp_parser_attributes_opt (parser);
27285 	      /* Remember which attributes are prefix attributes and
27286 		 which are not.  */
27287 	      first_attribute = attributes;
27288 	      /* Combine the attributes.  */
27289 	      attributes = attr_chainon (prefix_attributes, attributes);
27290 
27291 	      /* If it's an `=', then we have a constant-initializer or a
27292 		 pure-specifier.  It is not correct to parse the
27293 		 initializer before registering the member declaration
27294 		 since the member declaration should be in scope while
27295 		 its initializer is processed.  However, the rest of the
27296 		 front end does not yet provide an interface that allows
27297 		 us to handle this correctly.  */
27298 	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27299 		{
27300 		  /* In [class.mem]:
27301 
27302 		     A pure-specifier shall be used only in the declaration of
27303 		     a virtual function.
27304 
27305 		     A member-declarator can contain a constant-initializer
27306 		     only if it declares a static member of integral or
27307 		     enumeration type.
27308 
27309 		     Therefore, if the DECLARATOR is for a function, we look
27310 		     for a pure-specifier; otherwise, we look for a
27311 		     constant-initializer.  When we call `grokfield', it will
27312 		     perform more stringent semantics checks.  */
27313 		  initializer_token_start = cp_lexer_peek_token (parser->lexer);
27314 		  declarator->init_loc = initializer_token_start->location;
27315 		  if (function_declarator_p (declarator)
27316 		      || (decl_specifiers.type
27317 			  && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27318 			  && declarator->kind == cdk_id
27319 			  && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27320 			      == FUNCTION_TYPE)))
27321 		    initializer = cp_parser_pure_specifier (parser);
27322 		  else if (decl_specifiers.storage_class != sc_static)
27323 		    initializer = cp_parser_save_nsdmi (parser);
27324 		  else if (cxx_dialect >= cxx11)
27325 		    {
27326 		      bool nonconst;
27327 		      /* Don't require a constant rvalue in C++11, since we
27328 			 might want a reference constant.  We'll enforce
27329 		         constancy later.  */
27330 		      cp_lexer_consume_token (parser->lexer);
27331 		      /* Parse the initializer.  */
27332 		      initializer = cp_parser_initializer_clause (parser,
27333 								  &nonconst);
27334 		    }
27335 		  else
27336 		    /* Parse the initializer.  */
27337 		    initializer = cp_parser_constant_initializer (parser);
27338 		}
27339 	      else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27340 		       && !function_declarator_p (declarator))
27341 		{
27342 		  bool x;
27343 		  declarator->init_loc
27344 		    = cp_lexer_peek_token (parser->lexer)->location;
27345 		  if (decl_specifiers.storage_class != sc_static)
27346 		    initializer = cp_parser_save_nsdmi (parser);
27347 		  else
27348 		    initializer = cp_parser_initializer (parser, &x, &x);
27349 		}
27350 	      /* Detect invalid bit-field cases such as
27351 
27352 		   int *p : 4;
27353 		   int &&r : 3;
27354 
27355 		 and similar.  */
27356 	      else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27357 		       /* If there were no type specifiers, it was a
27358 			  constructor.  */
27359 		       && decl_specifiers.any_type_specifiers_p)
27360 		{
27361 		  /* This is called for a decent diagnostic only.  */
27362 		  tree d = grokdeclarator (declarator, &decl_specifiers,
27363 					   BITFIELD, /*initialized=*/false,
27364 					   &attributes);
27365 		  if (!error_operand_p (d))
27366 		    error_at (DECL_SOURCE_LOCATION (d),
27367 			      "bit-field %qD has non-integral type %qT",
27368 			      d, TREE_TYPE (d));
27369 		  cp_parser_skip_to_end_of_statement (parser);
27370 		  /* Avoid "extra ;" pedwarns.  */
27371 		  if (cp_lexer_next_token_is (parser->lexer,
27372 					      CPP_SEMICOLON))
27373 		    cp_lexer_consume_token (parser->lexer);
27374 		  goto out;
27375 		}
27376 	      /* Otherwise, there is no initializer.  */
27377 	      else
27378 		initializer = NULL_TREE;
27379 
27380 	      /* See if we are probably looking at a function
27381 		 definition.  We are certainly not looking at a
27382 		 member-declarator.  Calling `grokfield' has
27383 		 side-effects, so we must not do it unless we are sure
27384 		 that we are looking at a member-declarator.  */
27385 	      if (cp_parser_token_starts_function_definition_p
27386 		  (cp_lexer_peek_token (parser->lexer)))
27387 		{
27388 		  /* The grammar does not allow a pure-specifier to be
27389 		     used when a member function is defined.  (It is
27390 		     possible that this fact is an oversight in the
27391 		     standard, since a pure function may be defined
27392 		     outside of the class-specifier.  */
27393 		  if (initializer && initializer_token_start)
27394 		    error_at (initializer_token_start->location,
27395 			      "pure-specifier on function-definition");
27396 		  decl = cp_parser_save_member_function_body (parser,
27397 							      &decl_specifiers,
27398 							      declarator,
27399 							      attributes);
27400 		  if (parser->fully_implicit_function_template_p)
27401 		    decl = finish_fully_implicit_template (parser, decl);
27402 		  /* If the member was not a friend, declare it here.  */
27403 		  if (!friend_p)
27404 		    finish_member_declaration (decl);
27405 		  /* Peek at the next token.  */
27406 		  token = cp_lexer_peek_token (parser->lexer);
27407 		  /* If the next token is a semicolon, consume it.  */
27408 		  if (token->type == CPP_SEMICOLON)
27409 		    {
27410 		      location_t semicolon_loc
27411 			= cp_lexer_consume_token (parser->lexer)->location;
27412 		      gcc_rich_location richloc (semicolon_loc);
27413 		      richloc.add_fixit_remove ();
27414 		      warning_at (&richloc, OPT_Wextra_semi,
27415 				  "extra %<;%> after in-class "
27416 				  "function definition");
27417 		    }
27418 		  goto out;
27419 		}
27420 	      else
27421 		if (declarator->kind == cdk_function)
27422 		  declarator->id_loc = token->location;
27423 	      /* Create the declaration.  */
27424 	      decl = grokfield (declarator, &decl_specifiers,
27425 				initializer, /*init_const_expr_p=*/true,
27426 				asm_specification, attributes);
27427 	      if (parser->fully_implicit_function_template_p)
27428 		{
27429 		  if (friend_p)
27430 		    finish_fully_implicit_template (parser, 0);
27431 		  else
27432 		    decl = finish_fully_implicit_template (parser, decl);
27433 		}
27434 	    }
27435 
27436 	  cp_finalize_omp_declare_simd (parser, decl);
27437 	  cp_finalize_oacc_routine (parser, decl, false);
27438 
27439 	  /* Reset PREFIX_ATTRIBUTES.  */
27440 	  if (attributes != error_mark_node)
27441 	    {
27442 	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
27443 		attributes = TREE_CHAIN (attributes);
27444 	      if (attributes)
27445 		TREE_CHAIN (attributes) = NULL_TREE;
27446 	    }
27447 
27448 	  /* If there is any qualification still in effect, clear it
27449 	     now; we will be starting fresh with the next declarator.  */
27450 	  parser->scope = NULL_TREE;
27451 	  parser->qualifying_scope = NULL_TREE;
27452 	  parser->object_scope = NULL_TREE;
27453 	  /* If it's a `,', then there are more declarators.  */
27454 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27455 	    {
27456 	      cp_lexer_consume_token (parser->lexer);
27457 	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27458 		{
27459 		  cp_token *token = cp_lexer_previous_token (parser->lexer);
27460 		  gcc_rich_location richloc (token->location);
27461 		  richloc.add_fixit_remove ();
27462 		  error_at (&richloc, "stray %<,%> at end of "
27463 			    "member declaration");
27464 		}
27465 	    }
27466 	  /* If the next token isn't a `;', then we have a parse error.  */
27467 	  else if (cp_lexer_next_token_is_not (parser->lexer,
27468 					       CPP_SEMICOLON))
27469 	    {
27470 	      /* The next token might be a ways away from where the
27471 		 actual semicolon is missing.  Find the previous token
27472 		 and use that for our error position.  */
27473 	      cp_token *token = cp_lexer_previous_token (parser->lexer);
27474 	      gcc_rich_location richloc (token->location);
27475 	      richloc.add_fixit_insert_after (";");
27476 	      error_at (&richloc, "expected %<;%> at end of "
27477 			"member declaration");
27478 
27479 	      /* Assume that the user meant to provide a semicolon.  If
27480 		 we were to cp_parser_skip_to_end_of_statement, we might
27481 		 skip to a semicolon inside a member function definition
27482 		 and issue nonsensical error messages.  */
27483 	      assume_semicolon = true;
27484 	    }
27485 
27486 	  if (decl)
27487 	    {
27488 	      /* Add DECL to the list of members.  */
27489 	      if (!friend_p
27490 		  /* Explicitly include, eg, NSDMIs, for better error
27491 		     recovery (c++/58650).  */
27492 		  || !DECL_DECLARES_FUNCTION_P (decl))
27493 		finish_member_declaration (decl);
27494 
27495 	      if (DECL_DECLARES_FUNCTION_P (decl))
27496 		cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27497 	      else if (TREE_CODE (decl) == FIELD_DECL
27498 		       && DECL_INITIAL (decl))
27499 		/* Add DECL to the queue of NSDMI to be parsed later.  */
27500 		vec_safe_push (unparsed_nsdmis, decl);
27501 	    }
27502 
27503 	  if (assume_semicolon)
27504 	    goto out;
27505 	}
27506     }
27507 
27508   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27509  out:
27510   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27511   cp_finalize_omp_declare_simd (parser, &odsd);
27512 }
27513 
27514 /* Parse a pure-specifier.
27515 
27516    pure-specifier:
27517      = 0
27518 
27519    Returns INTEGER_ZERO_NODE if a pure specifier is found.
27520    Otherwise, ERROR_MARK_NODE is returned.  */
27521 
27522 static tree
cp_parser_pure_specifier(cp_parser * parser)27523 cp_parser_pure_specifier (cp_parser* parser)
27524 {
27525   cp_token *token;
27526 
27527   /* Look for the `=' token.  */
27528   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27529     return error_mark_node;
27530   /* Look for the `0' token.  */
27531   token = cp_lexer_peek_token (parser->lexer);
27532 
27533   if (token->type == CPP_EOF
27534       || token->type == CPP_PRAGMA_EOL)
27535     return error_mark_node;
27536 
27537   cp_lexer_consume_token (parser->lexer);
27538 
27539   /* Accept = default or = delete in c++0x mode.  */
27540   if (token->keyword == RID_DEFAULT
27541       || token->keyword == RID_DELETE)
27542     {
27543       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27544       return token->u.value;
27545     }
27546 
27547   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
27548   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27549     {
27550       cp_parser_error (parser,
27551 		       "invalid pure specifier (only %<= 0%> is allowed)");
27552       cp_parser_skip_to_end_of_statement (parser);
27553       return error_mark_node;
27554     }
27555   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27556     {
27557       error_at (token->location, "templates may not be %<virtual%>");
27558       return error_mark_node;
27559     }
27560 
27561   return integer_zero_node;
27562 }
27563 
27564 /* Parse a constant-initializer.
27565 
27566    constant-initializer:
27567      = constant-expression
27568 
27569    Returns a representation of the constant-expression.  */
27570 
27571 static tree
cp_parser_constant_initializer(cp_parser * parser)27572 cp_parser_constant_initializer (cp_parser* parser)
27573 {
27574   /* Look for the `=' token.  */
27575   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27576     return error_mark_node;
27577 
27578   /* It is invalid to write:
27579 
27580        struct S { static const int i = { 7 }; };
27581 
27582      */
27583   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27584     {
27585       cp_parser_error (parser,
27586 		       "a brace-enclosed initializer is not allowed here");
27587       /* Consume the opening brace.  */
27588       matching_braces braces;
27589       braces.consume_open (parser);
27590       /* Skip the initializer.  */
27591       cp_parser_skip_to_closing_brace (parser);
27592       /* Look for the trailing `}'.  */
27593       braces.require_close (parser);
27594 
27595       return error_mark_node;
27596     }
27597 
27598   return cp_parser_constant_expression (parser);
27599 }
27600 
27601 /* Derived classes [gram.class.derived] */
27602 
27603 /* Parse a base-clause.
27604 
27605    base-clause:
27606      : base-specifier-list
27607 
27608    base-specifier-list:
27609      base-specifier ... [opt]
27610      base-specifier-list , base-specifier ... [opt]
27611 
27612    Returns a TREE_LIST representing the base-classes, in the order in
27613    which they were declared.  The representation of each node is as
27614    described by cp_parser_base_specifier.
27615 
27616    In the case that no bases are specified, this function will return
27617    NULL_TREE, not ERROR_MARK_NODE.  */
27618 
27619 static tree
cp_parser_base_clause(cp_parser * parser)27620 cp_parser_base_clause (cp_parser* parser)
27621 {
27622   tree bases = NULL_TREE;
27623 
27624   /* Look for the `:' that begins the list.  */
27625   cp_parser_require (parser, CPP_COLON, RT_COLON);
27626 
27627   /* Scan the base-specifier-list.  */
27628   while (true)
27629     {
27630       cp_token *token;
27631       tree base;
27632       bool pack_expansion_p = false;
27633 
27634       /* Look for the base-specifier.  */
27635       base = cp_parser_base_specifier (parser);
27636       /* Look for the (optional) ellipsis. */
27637       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27638         {
27639           /* Consume the `...'. */
27640           cp_lexer_consume_token (parser->lexer);
27641 
27642           pack_expansion_p = true;
27643         }
27644 
27645       /* Add BASE to the front of the list.  */
27646       if (base && base != error_mark_node)
27647 	{
27648           if (pack_expansion_p)
27649             /* Make this a pack expansion type. */
27650             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
27651 
27652           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
27653             {
27654               TREE_CHAIN (base) = bases;
27655               bases = base;
27656             }
27657 	}
27658       /* Peek at the next token.  */
27659       token = cp_lexer_peek_token (parser->lexer);
27660       /* If it's not a comma, then the list is complete.  */
27661       if (token->type != CPP_COMMA)
27662 	break;
27663       /* Consume the `,'.  */
27664       cp_lexer_consume_token (parser->lexer);
27665     }
27666 
27667   /* PARSER->SCOPE may still be non-NULL at this point, if the last
27668      base class had a qualified name.  However, the next name that
27669      appears is certainly not qualified.  */
27670   parser->scope = NULL_TREE;
27671   parser->qualifying_scope = NULL_TREE;
27672   parser->object_scope = NULL_TREE;
27673 
27674   return nreverse (bases);
27675 }
27676 
27677 /* Parse a base-specifier.
27678 
27679    base-specifier:
27680      :: [opt] nested-name-specifier [opt] class-name
27681      virtual access-specifier [opt] :: [opt] nested-name-specifier
27682        [opt] class-name
27683      access-specifier virtual [opt] :: [opt] nested-name-specifier
27684        [opt] class-name
27685 
27686    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
27687    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27688    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
27689    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
27690 
27691 static tree
cp_parser_base_specifier(cp_parser * parser)27692 cp_parser_base_specifier (cp_parser* parser)
27693 {
27694   cp_token *token;
27695   bool done = false;
27696   bool virtual_p = false;
27697   bool duplicate_virtual_error_issued_p = false;
27698   bool duplicate_access_error_issued_p = false;
27699   bool class_scope_p, template_p;
27700   tree access = access_default_node;
27701   tree type;
27702 
27703   /* Process the optional `virtual' and `access-specifier'.  */
27704   while (!done)
27705     {
27706       /* Peek at the next token.  */
27707       token = cp_lexer_peek_token (parser->lexer);
27708       /* Process `virtual'.  */
27709       switch (token->keyword)
27710 	{
27711 	case RID_VIRTUAL:
27712 	  /* If `virtual' appears more than once, issue an error.  */
27713 	  if (virtual_p && !duplicate_virtual_error_issued_p)
27714 	    {
27715 	      cp_parser_error (parser,
27716 			       "%<virtual%> specified more than once in base-specifier");
27717 	      duplicate_virtual_error_issued_p = true;
27718 	    }
27719 
27720 	  virtual_p = true;
27721 
27722 	  /* Consume the `virtual' token.  */
27723 	  cp_lexer_consume_token (parser->lexer);
27724 
27725 	  break;
27726 
27727 	case RID_PUBLIC:
27728 	case RID_PROTECTED:
27729 	case RID_PRIVATE:
27730 	  /* If more than one access specifier appears, issue an
27731 	     error.  */
27732 	  if (access != access_default_node
27733 	      && !duplicate_access_error_issued_p)
27734 	    {
27735 	      cp_parser_error (parser,
27736 			       "more than one access specifier in base-specifier");
27737 	      duplicate_access_error_issued_p = true;
27738 	    }
27739 
27740 	  access = ridpointers[(int) token->keyword];
27741 
27742 	  /* Consume the access-specifier.  */
27743 	  cp_lexer_consume_token (parser->lexer);
27744 
27745 	  break;
27746 
27747 	default:
27748 	  done = true;
27749 	  break;
27750 	}
27751     }
27752   /* It is not uncommon to see programs mechanically, erroneously, use
27753      the 'typename' keyword to denote (dependent) qualified types
27754      as base classes.  */
27755   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27756     {
27757       token = cp_lexer_peek_token (parser->lexer);
27758       if (!processing_template_decl)
27759 	error_at (token->location,
27760 		  "keyword %<typename%> not allowed outside of templates");
27761       else
27762 	error_at (token->location,
27763 		  "keyword %<typename%> not allowed in this context "
27764 		  "(the base class is implicitly a type)");
27765       cp_lexer_consume_token (parser->lexer);
27766     }
27767 
27768   /* Look for the optional `::' operator.  */
27769   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27770   /* Look for the nested-name-specifier.  The simplest way to
27771      implement:
27772 
27773        [temp.res]
27774 
27775        The keyword `typename' is not permitted in a base-specifier or
27776        mem-initializer; in these contexts a qualified name that
27777        depends on a template-parameter is implicitly assumed to be a
27778        type name.
27779 
27780      is to pretend that we have seen the `typename' keyword at this
27781      point.  */
27782   cp_parser_nested_name_specifier_opt (parser,
27783 				       /*typename_keyword_p=*/true,
27784 				       /*check_dependency_p=*/true,
27785 				       /*type_p=*/true,
27786 				       /*is_declaration=*/true);
27787   /* If the base class is given by a qualified name, assume that names
27788      we see are type names or templates, as appropriate.  */
27789   class_scope_p = (parser->scope && TYPE_P (parser->scope));
27790   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
27791 
27792   if (!parser->scope
27793       && cp_lexer_next_token_is_decltype (parser->lexer))
27794     /* DR 950 allows decltype as a base-specifier.  */
27795     type = cp_parser_decltype (parser);
27796   else
27797     {
27798       /* Otherwise, look for the class-name.  */
27799       type = cp_parser_class_name (parser,
27800 				   class_scope_p,
27801 				   template_p,
27802 				   typename_type,
27803 				   /*check_dependency_p=*/true,
27804 				   /*class_head_p=*/false,
27805 				   /*is_declaration=*/true);
27806       type = TREE_TYPE (type);
27807     }
27808 
27809   if (type == error_mark_node)
27810     return error_mark_node;
27811 
27812   return finish_base_specifier (type, access, virtual_p);
27813 }
27814 
27815 /* Exception handling [gram.exception] */
27816 
27817 /* Save the tokens that make up the noexcept-specifier for a member-function.
27818    Returns a DEFERRED_PARSE.  */
27819 
27820 static tree
cp_parser_save_noexcept(cp_parser * parser)27821 cp_parser_save_noexcept (cp_parser *parser)
27822 {
27823   cp_token *first = parser->lexer->next_token;
27824   /* We want everything up to, including, the final ')'.  */
27825   cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
27826   cp_token *last = parser->lexer->next_token;
27827 
27828   /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
27829      to carry the information we will need.  */
27830   tree expr = make_node (DEFERRED_PARSE);
27831   /* Save away the noexcept-specifier; we will process it when the
27832      class is complete.  */
27833   DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
27834   DEFPARSE_INSTANTIATIONS (expr) = nullptr;
27835   expr = build_tree_list (expr, NULL_TREE);
27836   return expr;
27837 }
27838 
27839 /* Used for late processing of noexcept-specifiers of member-functions.
27840    DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
27841    we saved for later; parse it now.  DECL is the declaration of the
27842    member function.  */
27843 
27844 static tree
cp_parser_late_noexcept_specifier(cp_parser * parser,tree default_arg)27845 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
27846 {
27847   /* Make sure we've gotten something that hasn't been parsed yet.  */
27848   gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
27849 
27850   push_unparsed_function_queues (parser);
27851 
27852   /* Push the saved tokens for the noexcept-specifier onto the parser's
27853      lexer stack.  */
27854   cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
27855   cp_parser_push_lexer_for_tokens (parser, tokens);
27856 
27857   /* Parse the cached noexcept-specifier.  */
27858   tree parsed_arg
27859     = cp_parser_noexcept_specification_opt (parser,
27860 					    CP_PARSER_FLAGS_NONE,
27861 					    /*require_constexpr=*/true,
27862 					    /*consumed_expr=*/NULL,
27863 					    /*return_cond=*/false);
27864 
27865   /* Revert to the main lexer.  */
27866   cp_parser_pop_lexer (parser);
27867 
27868   /* Restore the queue.  */
27869   pop_unparsed_function_queues (parser);
27870 
27871   /* And we're done.  */
27872   return parsed_arg;
27873 }
27874 
27875 /* Perform late checking of overriding function with respect to their
27876    noexcept-specifiers.  FNDECL is the member function that potentially
27877    overrides some virtual function with the same signature.  */
27878 
27879 static void
noexcept_override_late_checks(tree fndecl)27880 noexcept_override_late_checks (tree fndecl)
27881 {
27882   tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
27883   tree base_binfo;
27884 
27885   if (DECL_STATIC_FUNCTION_P (fndecl))
27886     return;
27887 
27888   for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
27889     {
27890       tree basetype = BINFO_TYPE (base_binfo);
27891 
27892       if (!TYPE_POLYMORPHIC_P (basetype))
27893 	continue;
27894 
27895       tree fn = look_for_overrides_here (basetype, fndecl);
27896       if (fn)
27897 	maybe_check_overriding_exception_spec (fndecl, fn);
27898     }
27899 }
27900 
27901 /* Parse an (optional) noexcept-specification.
27902 
27903    noexcept-specification:
27904      noexcept ( constant-expression ) [opt]
27905 
27906    If no noexcept-specification is present, returns NULL_TREE.
27907    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
27908    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
27909    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
27910    Otherwise, returns a noexcept specification unless RETURN_COND is true,
27911    in which case a boolean condition is returned instead.  The parser flags
27912    FLAGS is used to control parsing.  QUALS are qualifiers indicating whether
27913    the (member) function is `const'.  */
27914 
27915 static tree
cp_parser_noexcept_specification_opt(cp_parser * parser,cp_parser_flags flags,bool require_constexpr,bool * consumed_expr,bool return_cond)27916 cp_parser_noexcept_specification_opt (cp_parser* parser,
27917 				      cp_parser_flags flags,
27918 				      bool require_constexpr,
27919 				      bool* consumed_expr,
27920 				      bool return_cond)
27921 {
27922   cp_token *token;
27923   const char *saved_message;
27924 
27925   /* Peek at the next token.  */
27926   token = cp_lexer_peek_token (parser->lexer);
27927 
27928   /* Is it a noexcept-specification?  */
27929   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
27930     {
27931       tree expr;
27932 
27933       /* [class.mem]/6 says that a noexcept-specifer (within the
27934 	 member-specification of the class) is a complete-class context of
27935 	 a class.  So, if the noexcept-specifier has the optional expression,
27936 	 just save the tokens, and reparse this after we're done with the
27937 	 class.  */
27938 
27939       if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
27940 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
27941 	  /* No need to delay parsing for a number literal or true/false.  */
27942 	  && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
27943 		|| cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27944 	       && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
27945 	  && at_class_scope_p ()
27946 	  && TYPE_BEING_DEFINED (current_class_type)
27947 	  && !LAMBDA_TYPE_P (current_class_type))
27948 	return cp_parser_save_noexcept (parser);
27949 
27950       cp_lexer_consume_token (parser->lexer);
27951 
27952       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27953 	{
27954 	  matching_parens parens;
27955 	  parens.consume_open (parser);
27956 
27957 	  if (require_constexpr)
27958 	    {
27959 	      /* Types may not be defined in an exception-specification.  */
27960 	      saved_message = parser->type_definition_forbidden_message;
27961 	      parser->type_definition_forbidden_message
27962 	      = G_("types may not be defined in an exception-specification");
27963 
27964 	      bool non_constant_p;
27965 	      expr
27966 		= cp_parser_constant_expression (parser,
27967 						 /*allow_non_constant=*/true,
27968 						 &non_constant_p);
27969 	      if (non_constant_p
27970 		  && !require_potential_rvalue_constant_expression (expr))
27971 		{
27972 		  expr = NULL_TREE;
27973 		  return_cond = true;
27974 		}
27975 
27976 	      /* Restore the saved message.  */
27977 	      parser->type_definition_forbidden_message = saved_message;
27978 	    }
27979 	  else
27980 	    {
27981 	      expr = cp_parser_expression (parser);
27982 	      *consumed_expr = true;
27983 	    }
27984 
27985 	  parens.require_close (parser);
27986 	}
27987       else
27988 	{
27989 	  expr = boolean_true_node;
27990 	  if (!require_constexpr)
27991 	    *consumed_expr = false;
27992 	}
27993 
27994       /* We cannot build a noexcept-spec right away because this will check
27995 	 that expr is a constexpr.  */
27996       if (!return_cond)
27997 	return build_noexcept_spec (expr, tf_warning_or_error);
27998       else
27999 	return expr;
28000     }
28001   else
28002     return NULL_TREE;
28003 }
28004 
28005 /* Parse an (optional) exception-specification.
28006 
28007    exception-specification:
28008      throw ( type-id-list [opt] )
28009 
28010    Returns a TREE_LIST representing the exception-specification.  The
28011    TREE_VALUE of each node is a type.  The parser flags FLAGS is used to
28012    control parsing.  QUALS are qualifiers indicating whether the (member)
28013    function is `const'.  */
28014 
28015 static tree
cp_parser_exception_specification_opt(cp_parser * parser,cp_parser_flags flags)28016 cp_parser_exception_specification_opt (cp_parser* parser,
28017 				       cp_parser_flags flags)
28018 {
28019   cp_token *token;
28020   tree type_id_list;
28021   const char *saved_message;
28022 
28023   /* Peek at the next token.  */
28024   token = cp_lexer_peek_token (parser->lexer);
28025 
28026   /* Is it a noexcept-specification?  */
28027   type_id_list
28028     = cp_parser_noexcept_specification_opt (parser, flags,
28029 					    /*require_constexpr=*/true,
28030 					    /*consumed_expr=*/NULL,
28031 					    /*return_cond=*/false);
28032   if (type_id_list != NULL_TREE)
28033     return type_id_list;
28034 
28035   /* If it's not `throw', then there's no exception-specification.  */
28036   if (!cp_parser_is_keyword (token, RID_THROW))
28037     return NULL_TREE;
28038 
28039   location_t loc = token->location;
28040 
28041   /* Consume the `throw'.  */
28042   cp_lexer_consume_token (parser->lexer);
28043 
28044   /* Look for the `('.  */
28045   matching_parens parens;
28046   parens.require_open (parser);
28047 
28048   /* Peek at the next token.  */
28049   token = cp_lexer_peek_token (parser->lexer);
28050   /* If it's not a `)', then there is a type-id-list.  */
28051   if (token->type != CPP_CLOSE_PAREN)
28052     {
28053       /* Types may not be defined in an exception-specification.  */
28054       saved_message = parser->type_definition_forbidden_message;
28055       parser->type_definition_forbidden_message
28056 	= G_("types may not be defined in an exception-specification");
28057       /* Parse the type-id-list.  */
28058       type_id_list = cp_parser_type_id_list (parser);
28059       /* Restore the saved message.  */
28060       parser->type_definition_forbidden_message = saved_message;
28061 
28062       if (cxx_dialect >= cxx17)
28063 	{
28064 	  error_at (loc, "ISO C++17 does not allow dynamic exception "
28065 			 "specifications");
28066 	  type_id_list = NULL_TREE;
28067 	}
28068       else if (cxx_dialect >= cxx11)
28069 	warning_at (loc, OPT_Wdeprecated,
28070 		    "dynamic exception specifications are deprecated in "
28071 		    "C++11");
28072     }
28073   /* In C++17, throw() is equivalent to noexcept (true).  throw()
28074      is deprecated in C++11 and above as well, but is still widely used,
28075      so don't warn about it yet.  */
28076   else if (cxx_dialect >= cxx17)
28077     type_id_list = noexcept_true_spec;
28078   else
28079     type_id_list = empty_except_spec;
28080 
28081   /* Look for the `)'.  */
28082   parens.require_close (parser);
28083 
28084   return type_id_list;
28085 }
28086 
28087 /* Parse an (optional) type-id-list.
28088 
28089    type-id-list:
28090      type-id ... [opt]
28091      type-id-list , type-id ... [opt]
28092 
28093    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
28094    in the order that the types were presented.  */
28095 
28096 static tree
cp_parser_type_id_list(cp_parser * parser)28097 cp_parser_type_id_list (cp_parser* parser)
28098 {
28099   tree types = NULL_TREE;
28100 
28101   while (true)
28102     {
28103       cp_token *token;
28104       tree type;
28105 
28106       token = cp_lexer_peek_token (parser->lexer);
28107 
28108       /* Get the next type-id.  */
28109       type = cp_parser_type_id (parser);
28110       /* Check for invalid 'auto'.  */
28111       if (flag_concepts && type_uses_auto (type))
28112 	{
28113 	  error_at (token->location,
28114 		    "invalid use of %<auto%> in exception-specification");
28115 	  type = error_mark_node;
28116 	}
28117       /* Parse the optional ellipsis. */
28118       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28119         {
28120           /* Consume the `...'. */
28121           cp_lexer_consume_token (parser->lexer);
28122 
28123           /* Turn the type into a pack expansion expression. */
28124           type = make_pack_expansion (type);
28125         }
28126       /* Add it to the list.  */
28127       types = add_exception_specifier (types, type, /*complain=*/1);
28128       /* Peek at the next token.  */
28129       token = cp_lexer_peek_token (parser->lexer);
28130       /* If it is not a `,', we are done.  */
28131       if (token->type != CPP_COMMA)
28132 	break;
28133       /* Consume the `,'.  */
28134       cp_lexer_consume_token (parser->lexer);
28135     }
28136 
28137   return nreverse (types);
28138 }
28139 
28140 /* Parse a try-block.
28141 
28142    try-block:
28143      try compound-statement handler-seq  */
28144 
28145 static tree
cp_parser_try_block(cp_parser * parser)28146 cp_parser_try_block (cp_parser* parser)
28147 {
28148   tree try_block;
28149 
28150   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28151   if (parser->in_function_body
28152       && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28153       && cxx_dialect < cxx20)
28154     pedwarn (input_location, OPT_Wc__20_extensions,
28155 	     "%<try%> in %<constexpr%> function only "
28156 	     "available with %<-std=c++20%> or %<-std=gnu++20%>");
28157 
28158   try_block = begin_try_block ();
28159   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28160   finish_try_block (try_block);
28161   cp_parser_handler_seq (parser);
28162   finish_handler_sequence (try_block);
28163 
28164   return try_block;
28165 }
28166 
28167 /* Parse a function-try-block.
28168 
28169    function-try-block:
28170      try ctor-initializer [opt] function-body handler-seq  */
28171 
28172 static void
cp_parser_function_try_block(cp_parser * parser)28173 cp_parser_function_try_block (cp_parser* parser)
28174 {
28175   tree compound_stmt;
28176   tree try_block;
28177 
28178   /* Look for the `try' keyword.  */
28179   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28180     return;
28181   /* Let the rest of the front end know where we are.  */
28182   try_block = begin_function_try_block (&compound_stmt);
28183   /* Parse the function-body.  */
28184   cp_parser_ctor_initializer_opt_and_function_body
28185     (parser, /*in_function_try_block=*/true);
28186   /* We're done with the `try' part.  */
28187   finish_function_try_block (try_block);
28188   /* Parse the handlers.  */
28189   cp_parser_handler_seq (parser);
28190   /* We're done with the handlers.  */
28191   finish_function_handler_sequence (try_block, compound_stmt);
28192 }
28193 
28194 /* Parse a handler-seq.
28195 
28196    handler-seq:
28197      handler handler-seq [opt]  */
28198 
28199 static void
cp_parser_handler_seq(cp_parser * parser)28200 cp_parser_handler_seq (cp_parser* parser)
28201 {
28202   while (true)
28203     {
28204       cp_token *token;
28205 
28206       /* Parse the handler.  */
28207       cp_parser_handler (parser);
28208       /* Peek at the next token.  */
28209       token = cp_lexer_peek_token (parser->lexer);
28210       /* If it's not `catch' then there are no more handlers.  */
28211       if (!cp_parser_is_keyword (token, RID_CATCH))
28212 	break;
28213     }
28214 }
28215 
28216 /* Parse a handler.
28217 
28218    handler:
28219      catch ( exception-declaration ) compound-statement  */
28220 
28221 static void
cp_parser_handler(cp_parser * parser)28222 cp_parser_handler (cp_parser* parser)
28223 {
28224   tree handler;
28225   tree declaration;
28226 
28227   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28228   handler = begin_handler ();
28229   matching_parens parens;
28230   parens.require_open (parser);
28231   declaration = cp_parser_exception_declaration (parser);
28232   finish_handler_parms (declaration, handler);
28233   parens.require_close (parser);
28234   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28235   finish_handler (handler);
28236 }
28237 
28238 /* Parse an exception-declaration.
28239 
28240    exception-declaration:
28241      type-specifier-seq declarator
28242      type-specifier-seq abstract-declarator
28243      type-specifier-seq
28244      ...
28245 
28246    Returns a VAR_DECL for the declaration, or NULL_TREE if the
28247    ellipsis variant is used.  */
28248 
28249 static tree
cp_parser_exception_declaration(cp_parser * parser)28250 cp_parser_exception_declaration (cp_parser* parser)
28251 {
28252   cp_decl_specifier_seq type_specifiers;
28253   cp_declarator *declarator;
28254   const char *saved_message;
28255 
28256   /* If it's an ellipsis, it's easy to handle.  */
28257   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28258     {
28259       /* Consume the `...' token.  */
28260       cp_lexer_consume_token (parser->lexer);
28261       return NULL_TREE;
28262     }
28263 
28264   /* Types may not be defined in exception-declarations.  */
28265   saved_message = parser->type_definition_forbidden_message;
28266   parser->type_definition_forbidden_message
28267     = G_("types may not be defined in exception-declarations");
28268 
28269   /* Parse the type-specifier-seq.  */
28270   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28271 				/*is_declaration=*/true,
28272 				/*is_trailing_return=*/false,
28273 				&type_specifiers);
28274   /* If it's a `)', then there is no declarator.  */
28275   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28276     declarator = NULL;
28277   else
28278     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28279 				       CP_PARSER_FLAGS_NONE,
28280 				       /*ctor_dtor_or_conv_p=*/NULL,
28281 				       /*parenthesized_p=*/NULL,
28282 				       /*member_p=*/false,
28283 				       /*friend_p=*/false,
28284 				       /*static_p=*/false);
28285 
28286   /* Restore the saved message.  */
28287   parser->type_definition_forbidden_message = saved_message;
28288 
28289   if (!type_specifiers.any_specifiers_p)
28290     return error_mark_node;
28291 
28292   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28293 }
28294 
28295 /* Parse a throw-expression.
28296 
28297    throw-expression:
28298      throw assignment-expression [opt]
28299 
28300    Returns a THROW_EXPR representing the throw-expression.  */
28301 
28302 static tree
cp_parser_throw_expression(cp_parser * parser)28303 cp_parser_throw_expression (cp_parser* parser)
28304 {
28305   tree expression;
28306   cp_token* token;
28307   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28308 
28309   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28310   token = cp_lexer_peek_token (parser->lexer);
28311   /* Figure out whether or not there is an assignment-expression
28312      following the "throw" keyword.  */
28313   if (token->type == CPP_COMMA
28314       || token->type == CPP_SEMICOLON
28315       || token->type == CPP_CLOSE_PAREN
28316       || token->type == CPP_CLOSE_SQUARE
28317       || token->type == CPP_CLOSE_BRACE
28318       || token->type == CPP_COLON)
28319     expression = NULL_TREE;
28320   else
28321     expression = cp_parser_assignment_expression (parser);
28322 
28323   /* Construct a location e.g.:
28324        throw x
28325        ^~~~~~~
28326      with caret == start at the start of the "throw" token, and
28327      the end at the end of the final token we consumed.  */
28328   location_t combined_loc = make_location (start_loc, start_loc,
28329 					   parser->lexer);
28330   expression = build_throw (combined_loc, expression);
28331 
28332   return expression;
28333 }
28334 
28335 /* Parse a yield-expression.
28336 
28337    yield-expression:
28338      co_yield assignment-expression
28339      co_yield braced-init-list
28340 
28341    Returns a CO_YIELD_EXPR representing the yield-expression.  */
28342 
28343 static tree
cp_parser_yield_expression(cp_parser * parser)28344 cp_parser_yield_expression (cp_parser* parser)
28345 {
28346   tree expr;
28347 
28348   cp_token *token = cp_lexer_peek_token (parser->lexer);
28349   location_t kw_loc = token->location; /* Save for later.  */
28350 
28351   cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28352 
28353   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28354     {
28355       bool expr_non_constant_p;
28356       cp_lexer_set_source_position (parser->lexer);
28357       /* ??? : probably a moot point?  */
28358       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28359       expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28360     }
28361   else
28362     expr = cp_parser_assignment_expression (parser);
28363 
28364   if (expr == error_mark_node)
28365     return expr;
28366 
28367   return finish_co_yield_expr (kw_loc, expr);
28368 }
28369 
28370 /* GNU Extensions */
28371 
28372 /* Parse an (optional) asm-specification.
28373 
28374    asm-specification:
28375      asm ( string-literal )
28376 
28377    If the asm-specification is present, returns a STRING_CST
28378    corresponding to the string-literal.  Otherwise, returns
28379    NULL_TREE.  */
28380 
28381 static tree
cp_parser_asm_specification_opt(cp_parser * parser)28382 cp_parser_asm_specification_opt (cp_parser* parser)
28383 {
28384   cp_token *token;
28385   tree asm_specification;
28386 
28387   /* Peek at the next token.  */
28388   token = cp_lexer_peek_token (parser->lexer);
28389   /* If the next token isn't the `asm' keyword, then there's no
28390      asm-specification.  */
28391   if (!cp_parser_is_keyword (token, RID_ASM))
28392     return NULL_TREE;
28393 
28394   /* Consume the `asm' token.  */
28395   cp_lexer_consume_token (parser->lexer);
28396   /* Look for the `('.  */
28397   matching_parens parens;
28398   parens.require_open (parser);
28399 
28400   /* Look for the string-literal.  */
28401   asm_specification = cp_parser_string_literal (parser, false, false);
28402 
28403   /* Look for the `)'.  */
28404   parens.require_close (parser);
28405 
28406   return asm_specification;
28407 }
28408 
28409 /* Parse an asm-operand-list.
28410 
28411    asm-operand-list:
28412      asm-operand
28413      asm-operand-list , asm-operand
28414 
28415    asm-operand:
28416      string-literal ( expression )
28417      [ string-literal ] string-literal ( expression )
28418 
28419    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
28420    each node is the expression.  The TREE_PURPOSE is itself a
28421    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28422    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28423    is a STRING_CST for the string literal before the parenthesis. Returns
28424    ERROR_MARK_NODE if any of the operands are invalid.  */
28425 
28426 static tree
cp_parser_asm_operand_list(cp_parser * parser)28427 cp_parser_asm_operand_list (cp_parser* parser)
28428 {
28429   tree asm_operands = NULL_TREE;
28430   bool invalid_operands = false;
28431 
28432   while (true)
28433     {
28434       tree string_literal;
28435       tree expression;
28436       tree name;
28437 
28438       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28439 	{
28440 	  /* Consume the `[' token.  */
28441 	  cp_lexer_consume_token (parser->lexer);
28442 	  /* Read the operand name.  */
28443 	  name = cp_parser_identifier (parser);
28444 	  if (name != error_mark_node)
28445 	    name = build_string (IDENTIFIER_LENGTH (name),
28446 				 IDENTIFIER_POINTER (name));
28447 	  /* Look for the closing `]'.  */
28448 	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28449 	}
28450       else
28451 	name = NULL_TREE;
28452       /* Look for the string-literal.  */
28453       string_literal = cp_parser_string_literal (parser, false, false);
28454 
28455       /* Look for the `('.  */
28456       matching_parens parens;
28457       parens.require_open (parser);
28458       /* Parse the expression.  */
28459       expression = cp_parser_expression (parser);
28460       /* Look for the `)'.  */
28461       parens.require_close (parser);
28462 
28463       if (name == error_mark_node
28464 	  || string_literal == error_mark_node
28465 	  || expression == error_mark_node)
28466         invalid_operands = true;
28467 
28468       /* Add this operand to the list.  */
28469       asm_operands = tree_cons (build_tree_list (name, string_literal),
28470 				expression,
28471 				asm_operands);
28472       /* If the next token is not a `,', there are no more
28473 	 operands.  */
28474       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28475 	break;
28476       /* Consume the `,'.  */
28477       cp_lexer_consume_token (parser->lexer);
28478     }
28479 
28480   return invalid_operands ? error_mark_node : nreverse (asm_operands);
28481 }
28482 
28483 /* Parse an asm-clobber-list.
28484 
28485    asm-clobber-list:
28486      string-literal
28487      asm-clobber-list , string-literal
28488 
28489    Returns a TREE_LIST, indicating the clobbers in the order that they
28490    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
28491 
28492 static tree
cp_parser_asm_clobber_list(cp_parser * parser)28493 cp_parser_asm_clobber_list (cp_parser* parser)
28494 {
28495   tree clobbers = NULL_TREE;
28496 
28497   while (true)
28498     {
28499       tree string_literal;
28500 
28501       /* Look for the string literal.  */
28502       string_literal = cp_parser_string_literal (parser, false, false);
28503       /* Add it to the list.  */
28504       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28505       /* If the next token is not a `,', then the list is
28506 	 complete.  */
28507       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28508 	break;
28509       /* Consume the `,' token.  */
28510       cp_lexer_consume_token (parser->lexer);
28511     }
28512 
28513   return clobbers;
28514 }
28515 
28516 /* Parse an asm-label-list.
28517 
28518    asm-label-list:
28519      identifier
28520      asm-label-list , identifier
28521 
28522    Returns a TREE_LIST, indicating the labels in the order that they
28523    appeared.  The TREE_VALUE of each node is a label.  */
28524 
28525 static tree
cp_parser_asm_label_list(cp_parser * parser)28526 cp_parser_asm_label_list (cp_parser* parser)
28527 {
28528   tree labels = NULL_TREE;
28529 
28530   while (true)
28531     {
28532       tree identifier, label, name;
28533 
28534       /* Look for the identifier.  */
28535       identifier = cp_parser_identifier (parser);
28536       if (!error_operand_p (identifier))
28537         {
28538 	  label = lookup_label (identifier);
28539 	  if (TREE_CODE (label) == LABEL_DECL)
28540 	    {
28541 	      TREE_USED (label) = 1;
28542 	      check_goto (label);
28543 	      name = build_string (IDENTIFIER_LENGTH (identifier),
28544 				   IDENTIFIER_POINTER (identifier));
28545 	      labels = tree_cons (name, label, labels);
28546 	    }
28547 	}
28548       /* If the next token is not a `,', then the list is
28549 	 complete.  */
28550       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28551 	break;
28552       /* Consume the `,' token.  */
28553       cp_lexer_consume_token (parser->lexer);
28554     }
28555 
28556   return nreverse (labels);
28557 }
28558 
28559 /* Return TRUE iff the next tokens in the stream are possibly the
28560    beginning of a GNU extension attribute. */
28561 
28562 static bool
cp_next_tokens_can_be_gnu_attribute_p(cp_parser * parser)28563 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28564 {
28565   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28566 }
28567 
28568 /* Return TRUE iff the next tokens in the stream are possibly the
28569    beginning of a standard C++-11 attribute specifier.  */
28570 
28571 static bool
cp_next_tokens_can_be_std_attribute_p(cp_parser * parser)28572 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28573 {
28574   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28575 }
28576 
28577 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28578    beginning of a standard C++-11 attribute specifier.  */
28579 
28580 static bool
cp_nth_tokens_can_be_std_attribute_p(cp_parser * parser,size_t n)28581 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28582 {
28583   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28584 
28585   return (cxx_dialect >= cxx11
28586 	  && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28587 	      || (token->type == CPP_OPEN_SQUARE
28588 		  && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28589 		  && token->type == CPP_OPEN_SQUARE)));
28590 }
28591 
28592 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28593    beginning of a GNU extension attribute.  */
28594 
28595 static bool
cp_nth_tokens_can_be_gnu_attribute_p(cp_parser * parser,size_t n)28596 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28597 {
28598   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28599 
28600   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28601 }
28602 
28603 /* Return true iff the next tokens can be the beginning of either a
28604    GNU attribute list, or a standard C++11 attribute sequence.  */
28605 
28606 static bool
cp_next_tokens_can_be_attribute_p(cp_parser * parser)28607 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28608 {
28609   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28610 	  || cp_next_tokens_can_be_std_attribute_p (parser));
28611 }
28612 
28613 /* Return true iff the next Nth tokens can be the beginning of either
28614    a GNU attribute list, or a standard C++11 attribute sequence.  */
28615 
28616 static bool
cp_nth_tokens_can_be_attribute_p(cp_parser * parser,size_t n)28617 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28618 {
28619   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28620 	  || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28621 }
28622 
28623 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28624    of GNU attributes, or return NULL.  */
28625 
28626 static tree
cp_parser_attributes_opt(cp_parser * parser)28627 cp_parser_attributes_opt (cp_parser *parser)
28628 {
28629   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28630     return cp_parser_gnu_attributes_opt (parser);
28631   return cp_parser_std_attribute_spec_seq (parser);
28632 }
28633 
28634 /* Parse an (optional) series of attributes.
28635 
28636    attributes:
28637      attributes attribute
28638 
28639    attribute:
28640      __attribute__ (( attribute-list [opt] ))
28641 
28642    The return value is as for cp_parser_gnu_attribute_list.  */
28643 
28644 static tree
cp_parser_gnu_attributes_opt(cp_parser * parser)28645 cp_parser_gnu_attributes_opt (cp_parser* parser)
28646 {
28647   tree attributes = NULL_TREE;
28648 
28649   auto cleanup = make_temp_override
28650     (parser->auto_is_implicit_function_template_parm_p, false);
28651 
28652   while (true)
28653     {
28654       cp_token *token;
28655       tree attribute_list;
28656       bool ok = true;
28657 
28658       /* Peek at the next token.  */
28659       token = cp_lexer_peek_token (parser->lexer);
28660       /* If it's not `__attribute__', then we're done.  */
28661       if (token->keyword != RID_ATTRIBUTE)
28662 	break;
28663 
28664       /* Consume the `__attribute__' keyword.  */
28665       cp_lexer_consume_token (parser->lexer);
28666       /* Look for the two `(' tokens.  */
28667       matching_parens outer_parens;
28668       if (!outer_parens.require_open (parser))
28669 	ok = false;
28670       matching_parens inner_parens;
28671       if (!inner_parens.require_open (parser))
28672 	ok = false;
28673 
28674       /* Peek at the next token.  */
28675       token = cp_lexer_peek_token (parser->lexer);
28676       if (token->type != CPP_CLOSE_PAREN)
28677 	/* Parse the attribute-list.  */
28678 	attribute_list = cp_parser_gnu_attribute_list (parser);
28679       else
28680 	/* If the next token is a `)', then there is no attribute
28681 	   list.  */
28682 	attribute_list = NULL;
28683 
28684       /* Look for the two `)' tokens.  */
28685       if (!inner_parens.require_close (parser))
28686 	ok = false;
28687       if (!outer_parens.require_close (parser))
28688 	ok = false;
28689       if (!ok)
28690 	cp_parser_skip_to_end_of_statement (parser);
28691 
28692       /* Add these new attributes to the list.  */
28693       attributes = attr_chainon (attributes, attribute_list);
28694     }
28695 
28696   return attributes;
28697 }
28698 
28699 /* Parse a GNU attribute-list.
28700 
28701    attribute-list:
28702      attribute
28703      attribute-list , attribute
28704 
28705    attribute:
28706      identifier
28707      identifier ( identifier )
28708      identifier ( identifier , expression-list )
28709      identifier ( expression-list )
28710 
28711    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
28712    to an attribute.  The TREE_PURPOSE of each node is the identifier
28713    indicating which attribute is in use.  The TREE_VALUE represents
28714    the arguments, if any.  */
28715 
28716 static tree
cp_parser_gnu_attribute_list(cp_parser * parser,bool exactly_one)28717 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
28718 {
28719   tree attribute_list = NULL_TREE;
28720   bool save_translate_strings_p = parser->translate_strings_p;
28721 
28722   /* Don't create wrapper nodes within attributes: the
28723      handlers don't know how to handle them.  */
28724   auto_suppress_location_wrappers sentinel;
28725 
28726   parser->translate_strings_p = false;
28727   while (true)
28728     {
28729       cp_token *token;
28730       tree identifier;
28731       tree attribute;
28732 
28733       /* Look for the identifier.  We also allow keywords here; for
28734 	 example `__attribute__ ((const))' is legal.  */
28735       token = cp_lexer_peek_token (parser->lexer);
28736       if (token->type == CPP_NAME
28737 	  || token->type == CPP_KEYWORD)
28738 	{
28739 	  tree arguments = NULL_TREE;
28740 
28741 	  /* Consume the token, but save it since we need it for the
28742 	     SIMD enabled function parsing.  */
28743 	  cp_token *id_token = cp_lexer_consume_token (parser->lexer);
28744 
28745 	  /* Save away the identifier that indicates which attribute
28746 	     this is.  */
28747 	  identifier = (token->type == CPP_KEYWORD)
28748 	    /* For keywords, use the canonical spelling, not the
28749 	       parsed identifier.  */
28750 	    ? ridpointers[(int) token->keyword]
28751 	    : id_token->u.value;
28752 
28753 	  identifier = canonicalize_attr_name (identifier);
28754 	  attribute = build_tree_list (identifier, NULL_TREE);
28755 
28756 	  /* Peek at the next token.  */
28757 	  token = cp_lexer_peek_token (parser->lexer);
28758 	  /* If it's an `(', then parse the attribute arguments.  */
28759 	  if (token->type == CPP_OPEN_PAREN)
28760 	    {
28761 	      vec<tree, va_gc> *vec;
28762 	      int attr_flag = (attribute_takes_identifier_p (identifier)
28763 			       ? id_attr : normal_attr);
28764 	      vec = cp_parser_parenthesized_expression_list
28765 		    (parser, attr_flag, /*cast_p=*/false,
28766 		    /*allow_expansion_p=*/false,
28767 		    /*non_constant_p=*/NULL);
28768 	      if (vec == NULL)
28769 		arguments = error_mark_node;
28770 	      else
28771 		{
28772 		  arguments = build_tree_list_vec (vec);
28773 		  release_tree_vector (vec);
28774 		}
28775 	      /* Save the arguments away.  */
28776 	      TREE_VALUE (attribute) = arguments;
28777 	    }
28778 
28779 	  if (arguments != error_mark_node)
28780 	    {
28781 	      /* Add this attribute to the list.  */
28782 	      TREE_CHAIN (attribute) = attribute_list;
28783 	      attribute_list = attribute;
28784 	    }
28785 
28786 	  token = cp_lexer_peek_token (parser->lexer);
28787 	}
28788       /* Unless EXACTLY_ONE is set look for more attributes.
28789 	 If the next token isn't a `,', we're done.  */
28790       if (exactly_one || token->type != CPP_COMMA)
28791 	break;
28792 
28793       /* Consume the comma and keep going.  */
28794       cp_lexer_consume_token (parser->lexer);
28795     }
28796   parser->translate_strings_p = save_translate_strings_p;
28797 
28798   /* We built up the list in reverse order.  */
28799   return nreverse (attribute_list);
28800 }
28801 
28802 /* Parse arguments of omp::directive attribute.
28803 
28804    ( directive-name ,[opt] clause-list[opt] )
28805 
28806    For directive just remember the first/last tokens for subsequent
28807    parsing.  */
28808 
28809 static void
cp_parser_omp_directive_args(cp_parser * parser,tree attribute)28810 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
28811 {
28812   cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
28813   if (first->type == CPP_CLOSE_PAREN)
28814     {
28815       cp_lexer_consume_token (parser->lexer);
28816       error_at (first->location, "expected OpenMP directive name");
28817       cp_lexer_consume_token (parser->lexer);
28818       TREE_VALUE (attribute) = NULL_TREE;
28819       return;
28820     }
28821   size_t n = cp_parser_skip_balanced_tokens (parser, 1);
28822   if (n == 1)
28823     {
28824       cp_lexer_consume_token (parser->lexer);
28825       error_at (first->location, "expected attribute argument as balanced "
28826 				 "token sequence");
28827       TREE_VALUE (attribute) = NULL_TREE;
28828       return;
28829     }
28830   for (n = n - 2; n; --n)
28831     cp_lexer_consume_token (parser->lexer);
28832   cp_token *last = cp_lexer_peek_token (parser->lexer);
28833   cp_lexer_consume_token (parser->lexer);
28834   tree arg = make_node (DEFERRED_PARSE);
28835   DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
28836   DEFPARSE_INSTANTIATIONS (arg) = nullptr;
28837   TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
28838 }
28839 
28840 /* Parse arguments of omp::sequence attribute.
28841 
28842    ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... )  */
28843 
28844 static void
cp_parser_omp_sequence_args(cp_parser * parser,tree attribute)28845 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
28846 {
28847   matching_parens parens;
28848   parens.consume_open (parser);
28849   do
28850     {
28851       cp_token *token = cp_lexer_peek_token (parser->lexer);
28852       if (token->type == CPP_NAME
28853 	  && token->u.value == omp_identifier
28854 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
28855 	{
28856 	  cp_lexer_consume_token (parser->lexer);
28857 	  cp_lexer_consume_token (parser->lexer);
28858 	  token = cp_lexer_peek_token (parser->lexer);
28859 	}
28860       bool directive = false;
28861       const char *p;
28862       if (token->type != CPP_NAME)
28863 	p = "";
28864       else
28865 	p = IDENTIFIER_POINTER (token->u.value);
28866       if (strcmp (p, "directive") == 0)
28867 	directive = true;
28868       else if (strcmp (p, "sequence") != 0)
28869 	{
28870 	  error_at (token->location, "expected %<directive%> or %<sequence%>");
28871 	  cp_parser_skip_to_closing_parenthesis (parser,
28872 						 /*recovering=*/true,
28873 						 /*or_comma=*/true,
28874 						 /*consume_paren=*/false);
28875 	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28876 	    break;
28877 	  cp_lexer_consume_token (parser->lexer);
28878 	}
28879       cp_lexer_consume_token (parser->lexer);
28880       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
28881 	cp_parser_required_error (parser, RT_OPEN_PAREN, false,
28882 				  UNKNOWN_LOCATION);
28883       else if (directive)
28884 	cp_parser_omp_directive_args (parser, attribute);
28885       else
28886 	cp_parser_omp_sequence_args (parser, attribute);
28887       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28888 	break;
28889       cp_lexer_consume_token (parser->lexer);
28890     }
28891   while (1);
28892   if (!parens.require_close (parser))
28893     cp_parser_skip_to_closing_parenthesis (parser, true, false,
28894 					   /*consume_paren=*/true);
28895 }
28896 
28897 /*  Parse a standard C++11 attribute.
28898 
28899     The returned representation is a TREE_LIST which TREE_PURPOSE is
28900     the scoped name of the attribute, and the TREE_VALUE is its
28901     arguments list.
28902 
28903     Note that the scoped name of the attribute is itself a TREE_LIST
28904     which TREE_PURPOSE is the namespace of the attribute, and
28905     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
28906     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
28907     and which TREE_PURPOSE is directly the attribute name.
28908 
28909     Clients of the attribute code should use get_attribute_namespace
28910     and get_attribute_name to get the actual namespace and name of
28911     attributes, regardless of their being GNU or C++11 attributes.
28912 
28913     attribute:
28914       attribute-token attribute-argument-clause [opt]
28915 
28916     attribute-token:
28917       identifier
28918       attribute-scoped-token
28919 
28920     attribute-scoped-token:
28921       attribute-namespace :: identifier
28922 
28923     attribute-namespace:
28924       identifier
28925 
28926     attribute-argument-clause:
28927       ( balanced-token-seq )
28928 
28929     balanced-token-seq:
28930       balanced-token [opt]
28931       balanced-token-seq balanced-token
28932 
28933     balanced-token:
28934       ( balanced-token-seq )
28935       [ balanced-token-seq ]
28936       { balanced-token-seq }.  */
28937 
28938 static tree
cp_parser_std_attribute(cp_parser * parser,tree attr_ns)28939 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
28940 {
28941   tree attribute, attr_id = NULL_TREE, arguments;
28942   cp_token *token;
28943 
28944   auto cleanup = make_temp_override
28945     (parser->auto_is_implicit_function_template_parm_p, false);
28946 
28947   /* First, parse name of the attribute, a.k.a attribute-token.  */
28948 
28949   token = cp_lexer_peek_token (parser->lexer);
28950   if (token->type == CPP_NAME)
28951     attr_id = token->u.value;
28952   else if (token->type == CPP_KEYWORD)
28953     attr_id = ridpointers[(int) token->keyword];
28954   else if (token->flags & NAMED_OP)
28955     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28956 
28957   if (attr_id == NULL_TREE)
28958     return NULL_TREE;
28959 
28960   cp_lexer_consume_token (parser->lexer);
28961 
28962   token = cp_lexer_peek_token (parser->lexer);
28963   if (token->type == CPP_SCOPE)
28964     {
28965       /* We are seeing a scoped attribute token.  */
28966 
28967       cp_lexer_consume_token (parser->lexer);
28968       if (attr_ns)
28969 	error_at (token->location, "attribute using prefix used together "
28970 				   "with scoped attribute token");
28971       attr_ns = attr_id;
28972 
28973       token = cp_lexer_peek_token (parser->lexer);
28974       if (token->type == CPP_NAME)
28975 	attr_id = token->u.value;
28976       else if (token->type == CPP_KEYWORD)
28977 	attr_id = ridpointers[(int) token->keyword];
28978       else if (token->flags & NAMED_OP)
28979 	attr_id = get_identifier (cpp_type2name (token->type, token->flags));
28980       else
28981 	{
28982 	  error_at (token->location,
28983 		    "expected an identifier for the attribute name");
28984 	  return error_mark_node;
28985 	}
28986       cp_lexer_consume_token (parser->lexer);
28987 
28988       attr_ns = canonicalize_attr_name (attr_ns);
28989       attr_id = canonicalize_attr_name (attr_id);
28990       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
28991 				   NULL_TREE);
28992       token = cp_lexer_peek_token (parser->lexer);
28993     }
28994   else if (attr_ns)
28995     {
28996       attr_ns = canonicalize_attr_name (attr_ns);
28997       attr_id = canonicalize_attr_name (attr_id);
28998       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
28999 				   NULL_TREE);
29000     }
29001   else
29002     {
29003       attr_id = canonicalize_attr_name (attr_id);
29004       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29005 				   NULL_TREE);
29006       /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29007 	 but no longer: we have to be able to tell [[noreturn]] and
29008 	 __attribute__((noreturn)) apart.  */
29009       /* C++14 deprecated attribute is equivalent to GNU's.  */
29010       if (is_attribute_p ("deprecated", attr_id))
29011 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29012       /* C++17 fallthrough attribute is equivalent to GNU's.  */
29013       else if (is_attribute_p ("fallthrough", attr_id))
29014 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29015       /* Transactional Memory TS optimize_for_synchronized attribute is
29016 	 equivalent to GNU transaction_callable.  */
29017       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29018 	TREE_PURPOSE (attribute)
29019 	  = get_identifier ("transaction_callable");
29020       /* Transactional Memory attributes are GNU attributes.  */
29021       else if (tm_attr_to_mask (attr_id))
29022 	TREE_PURPOSE (attribute) = attr_id;
29023     }
29024 
29025   /* Now parse the optional argument clause of the attribute.  */
29026 
29027   if (token->type != CPP_OPEN_PAREN)
29028     {
29029       if ((flag_openmp || flag_openmp_simd)
29030 	  && attr_ns == omp_identifier
29031 	  && (is_attribute_p ("directive", attr_id)
29032 	      || is_attribute_p ("sequence", attr_id)))
29033 	{
29034 	  error_at (token->location, "%<omp::%E%> attribute requires argument",
29035 		    attr_id);
29036 	  return NULL_TREE;
29037 	}
29038       return attribute;
29039     }
29040 
29041   {
29042     vec<tree, va_gc> *vec;
29043     int attr_flag = normal_attr;
29044 
29045     /* Maybe we don't expect to see any arguments for this attribute.  */
29046     const attribute_spec *as
29047       = lookup_attribute_spec (TREE_PURPOSE (attribute));
29048     if (as && as->max_length == 0)
29049       {
29050 	error_at (token->location, "%qE attribute does not take any arguments",
29051 		  attr_id);
29052 	cp_parser_skip_to_closing_parenthesis (parser,
29053 					       /*recovering=*/true,
29054 					       /*or_comma=*/false,
29055 					       /*consume_paren=*/true);
29056 	return error_mark_node;
29057       }
29058 
29059     if (attr_ns == gnu_identifier
29060 	&& attribute_takes_identifier_p (attr_id))
29061       /* A GNU attribute that takes an identifier in parameter.  */
29062       attr_flag = id_attr;
29063 
29064     /* If this is a fake attribute created to handle -Wno-attributes,
29065        we must skip parsing the arguments.  */
29066     if (as == NULL || attribute_ignored_p (as))
29067       {
29068 	if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29069 	  {
29070 	    if (is_attribute_p ("directive", attr_id))
29071 	      {
29072 		cp_parser_omp_directive_args (parser, attribute);
29073 		return attribute;
29074 	      }
29075 	    else if (is_attribute_p ("sequence", attr_id))
29076 	      {
29077 		TREE_VALUE (TREE_PURPOSE (attribute))
29078 		  = get_identifier ("directive");
29079 		cp_parser_omp_sequence_args (parser, attribute);
29080 		TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29081 		return attribute;
29082 	      }
29083 	  }
29084 
29085 	/* For unknown attributes, just skip balanced tokens instead of
29086 	   trying to parse the arguments.  */
29087 	for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29088 	  cp_lexer_consume_token (parser->lexer);
29089 	return attribute;
29090       }
29091 
29092     vec = cp_parser_parenthesized_expression_list
29093       (parser, attr_flag, /*cast_p=*/false,
29094        /*allow_expansion_p=*/true,
29095        /*non_constant_p=*/NULL);
29096     if (vec == NULL)
29097       arguments = error_mark_node;
29098     else
29099       {
29100 	if (vec->is_empty ())
29101 	  /* e.g. [[attr()]].  */
29102 	  error_at (token->location, "parentheses must be omitted if "
29103 		    "%qE attribute argument list is empty",
29104 		    attr_id);
29105 	arguments = build_tree_list_vec (vec);
29106 	release_tree_vector (vec);
29107       }
29108 
29109     if (arguments == error_mark_node)
29110       attribute = error_mark_node;
29111     else
29112       TREE_VALUE (attribute) = arguments;
29113   }
29114 
29115   return attribute;
29116 }
29117 
29118 /* Warn if the attribute ATTRIBUTE appears more than once in the
29119    attribute-list ATTRIBUTES.  This used to be enforced for certain
29120    attributes, but the restriction was removed in P2156.  Note that
29121    carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29122    LOC is the location of ATTRIBUTE.  Returns true if ATTRIBUTE was not
29123    found in ATTRIBUTES.  */
29124 
29125 static bool
cp_parser_check_std_attribute(location_t loc,tree attributes,tree attribute)29126 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29127 {
29128   static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29129 			"likely", "unlikely", "fallthrough",
29130 			"no_unique_address" };
29131   if (attributes)
29132     for (const auto &a : alist)
29133       if (is_attribute_p (a, get_attribute_name (attribute))
29134 	  && lookup_attribute (a, attributes))
29135 	{
29136 	  if (!from_macro_expansion_at (loc))
29137 	    warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29138 			"multiple times", a);
29139 	  return false;
29140 	}
29141   return true;
29142 }
29143 
29144 /* Parse a list of standard C++-11 attributes.
29145 
29146    attribute-list:
29147      attribute [opt]
29148      attribute-list , attribute[opt]
29149      attribute ...
29150      attribute-list , attribute ...
29151 */
29152 
29153 static tree
cp_parser_std_attribute_list(cp_parser * parser,tree attr_ns)29154 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29155 {
29156   tree attributes = NULL_TREE, attribute = NULL_TREE;
29157   cp_token *token = NULL;
29158 
29159   while (true)
29160     {
29161       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29162       attribute = cp_parser_std_attribute (parser, attr_ns);
29163       if (attribute == error_mark_node)
29164 	break;
29165       if (attribute != NULL_TREE)
29166 	{
29167 	  if (cp_parser_check_std_attribute (loc, attributes, attribute))
29168 	    {
29169 	      TREE_CHAIN (attribute) = attributes;
29170 	      attributes = attribute;
29171 	    }
29172 	}
29173       token = cp_lexer_peek_token (parser->lexer);
29174       if (token->type == CPP_ELLIPSIS)
29175 	{
29176 	  cp_lexer_consume_token (parser->lexer);
29177 	  if (attribute == NULL_TREE)
29178 	    error_at (token->location,
29179 		      "expected attribute before %<...%>");
29180 	  else
29181 	    {
29182 	      tree pack = make_pack_expansion (TREE_VALUE (attribute));
29183 	      if (pack == error_mark_node)
29184 		return error_mark_node;
29185 	      TREE_VALUE (attribute) = pack;
29186 	    }
29187 	  token = cp_lexer_peek_token (parser->lexer);
29188 	}
29189       if (token->type != CPP_COMMA)
29190 	break;
29191       cp_lexer_consume_token (parser->lexer);
29192     }
29193   attributes = nreverse (attributes);
29194   return attributes;
29195 }
29196 
29197 /* Parse a standard C++-11 attribute specifier.
29198 
29199    attribute-specifier:
29200      [ [ attribute-using-prefix [opt] attribute-list ] ]
29201      alignment-specifier
29202 
29203    attribute-using-prefix:
29204      using attribute-namespace :
29205 
29206    alignment-specifier:
29207      alignas ( type-id ... [opt] )
29208      alignas ( alignment-expression ... [opt] ).  */
29209 
29210 static tree
cp_parser_std_attribute_spec(cp_parser * parser)29211 cp_parser_std_attribute_spec (cp_parser *parser)
29212 {
29213   tree attributes = NULL_TREE;
29214   cp_token *token = cp_lexer_peek_token (parser->lexer);
29215 
29216   if (token->type == CPP_OPEN_SQUARE
29217       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29218     {
29219       tree attr_ns = NULL_TREE;
29220 
29221       cp_lexer_consume_token (parser->lexer);
29222       cp_lexer_consume_token (parser->lexer);
29223 
29224       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29225 	{
29226 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
29227 	  if (token->type == CPP_NAME)
29228 	    attr_ns = token->u.value;
29229 	  else if (token->type == CPP_KEYWORD)
29230 	    attr_ns = ridpointers[(int) token->keyword];
29231 	  else if (token->flags & NAMED_OP)
29232 	    attr_ns = get_identifier (cpp_type2name (token->type,
29233 						     token->flags));
29234 	  if (attr_ns
29235 	      && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29236 	    {
29237 	      if (cxx_dialect < cxx17)
29238 		pedwarn (input_location, OPT_Wc__17_extensions,
29239 			 "attribute using prefix only available "
29240 			 "with %<-std=c++17%> or %<-std=gnu++17%>");
29241 
29242 	      cp_lexer_consume_token (parser->lexer);
29243 	      cp_lexer_consume_token (parser->lexer);
29244 	      cp_lexer_consume_token (parser->lexer);
29245 	    }
29246 	  else
29247 	    attr_ns = NULL_TREE;
29248 	}
29249 
29250       attributes = cp_parser_std_attribute_list (parser, attr_ns);
29251 
29252       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
29253 	  || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
29254 	cp_parser_skip_to_end_of_statement (parser);
29255       else
29256 	/* Warn about parsing c++11 attribute in non-c++11 mode, only
29257 	   when we are sure that we have actually parsed them.  */
29258 	maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29259     }
29260   else
29261     {
29262       tree alignas_expr;
29263 
29264       /* Look for an alignment-specifier.  */
29265 
29266       token = cp_lexer_peek_token (parser->lexer);
29267 
29268       if (token->type != CPP_KEYWORD
29269 	  || token->keyword != RID_ALIGNAS)
29270 	return NULL_TREE;
29271 
29272       cp_lexer_consume_token (parser->lexer);
29273       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29274 
29275       matching_parens parens;
29276       if (!parens.require_open (parser))
29277 	return error_mark_node;
29278 
29279       cp_parser_parse_tentatively (parser);
29280       alignas_expr = cp_parser_type_id (parser);
29281 
29282       if (!cp_parser_parse_definitely (parser))
29283 	{
29284 	  alignas_expr = cp_parser_assignment_expression (parser);
29285 	  if (alignas_expr == error_mark_node)
29286 	    cp_parser_skip_to_end_of_statement (parser);
29287 	  if (alignas_expr == NULL_TREE
29288 	      || alignas_expr == error_mark_node)
29289 	    return alignas_expr;
29290 	}
29291 
29292       alignas_expr = cxx_alignas_expr (alignas_expr);
29293       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
29294 
29295       /* Handle alignas (pack...).  */
29296       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29297 	{
29298 	  cp_lexer_consume_token (parser->lexer);
29299 	  alignas_expr = make_pack_expansion (alignas_expr);
29300 	}
29301 
29302       /* Something went wrong, so don't build the attribute.  */
29303       if (alignas_expr == error_mark_node)
29304 	return error_mark_node;
29305 
29306       /* Missing ')' means the code cannot possibly be valid; go ahead
29307 	 and commit to make sure we issue a hard error.  */
29308       if (cp_parser_uncommitted_to_tentative_parse_p (parser)
29309 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29310 	cp_parser_commit_to_tentative_parse (parser);
29311 
29312       if (!parens.require_close (parser))
29313 	return error_mark_node;
29314 
29315       /* Build the C++-11 representation of an 'aligned'
29316 	 attribute.  */
29317       attributes
29318 	= build_tree_list (build_tree_list (gnu_identifier,
29319 					    aligned_identifier), alignas_expr);
29320     }
29321 
29322   return attributes;
29323 }
29324 
29325 /* Parse a standard C++-11 attribute-specifier-seq.
29326 
29327    attribute-specifier-seq:
29328      attribute-specifier-seq [opt] attribute-specifier
29329  */
29330 
29331 static tree
cp_parser_std_attribute_spec_seq(cp_parser * parser)29332 cp_parser_std_attribute_spec_seq (cp_parser *parser)
29333 {
29334   tree attr_specs = NULL_TREE;
29335   tree attr_last = NULL_TREE;
29336 
29337   /* Don't create wrapper nodes within attributes: the
29338      handlers don't know how to handle them.  */
29339   auto_suppress_location_wrappers sentinel;
29340 
29341   while (true)
29342     {
29343       tree attr_spec = cp_parser_std_attribute_spec (parser);
29344       if (attr_spec == NULL_TREE)
29345 	break;
29346       if (attr_spec == error_mark_node)
29347 	return error_mark_node;
29348 
29349       if (attr_last)
29350 	TREE_CHAIN (attr_last) = attr_spec;
29351       else
29352 	attr_specs = attr_last = attr_spec;
29353       attr_last = tree_last (attr_last);
29354     }
29355 
29356   return attr_specs;
29357 }
29358 
29359 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29360    return index of the first token after balanced-token, or N on failure.  */
29361 
29362 static size_t
cp_parser_skip_balanced_tokens(cp_parser * parser,size_t n)29363 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
29364 {
29365   size_t orig_n = n;
29366   int nparens = 0, nbraces = 0, nsquares = 0;
29367   do
29368     switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
29369       {
29370       case CPP_PRAGMA_EOL:
29371 	if (!parser->lexer->in_pragma)
29372 	  break;
29373 	/* FALLTHRU */
29374       case CPP_EOF:
29375 	/* Ran out of tokens.  */
29376 	return orig_n;
29377       case CPP_OPEN_PAREN:
29378 	++nparens;
29379 	break;
29380       case CPP_OPEN_BRACE:
29381 	++nbraces;
29382 	break;
29383       case CPP_OPEN_SQUARE:
29384 	++nsquares;
29385 	break;
29386       case CPP_CLOSE_PAREN:
29387 	--nparens;
29388 	break;
29389       case CPP_CLOSE_BRACE:
29390 	--nbraces;
29391 	break;
29392       case CPP_CLOSE_SQUARE:
29393 	--nsquares;
29394 	break;
29395       default:
29396 	break;
29397       }
29398   while (nparens || nbraces || nsquares);
29399   return n;
29400 }
29401 
29402 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29403    return index of the first token after the GNU attribute tokens, or N on
29404    failure.  */
29405 
29406 static size_t
cp_parser_skip_gnu_attributes_opt(cp_parser * parser,size_t n)29407 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
29408 {
29409   while (true)
29410     {
29411       if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
29412 	  || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
29413 	  || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
29414 	break;
29415 
29416       size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
29417       if (n2 == n + 2)
29418 	break;
29419       if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
29420 	break;
29421       n = n2 + 1;
29422     }
29423   return n;
29424 }
29425 
29426 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29427    next token), return index of the first token after the standard C++11
29428    attribute tokens, or N on failure.  */
29429 
29430 static size_t
cp_parser_skip_std_attribute_spec_seq(cp_parser * parser,size_t n)29431 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
29432 {
29433   while (true)
29434     {
29435       if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
29436 	  && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
29437 	{
29438 	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29439 	  if (n2 == n + 1)
29440 	    break;
29441 	  if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
29442 	    break;
29443 	  n = n2 + 1;
29444 	}
29445       else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
29446 	       && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
29447 	{
29448 	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29449 	  if (n2 == n + 1)
29450 	    break;
29451 	  n = n2;
29452 	}
29453       else
29454 	break;
29455     }
29456   return n;
29457 }
29458 
29459 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29460    as the next token), return index of the first token after the attribute
29461    tokens, or N on failure.  */
29462 
29463 static size_t
cp_parser_skip_attributes_opt(cp_parser * parser,size_t n)29464 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
29465 {
29466   if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
29467     return cp_parser_skip_gnu_attributes_opt (parser, n);
29468   return cp_parser_skip_std_attribute_spec_seq (parser, n);
29469 }
29470 
29471 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
29472    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
29473    current value of the PEDANTIC flag, regardless of whether or not
29474    the `__extension__' keyword is present.  The caller is responsible
29475    for restoring the value of the PEDANTIC flag.  */
29476 
29477 static bool
cp_parser_extension_opt(cp_parser * parser,int * saved_pedantic)29478 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
29479 {
29480   /* Save the old value of the PEDANTIC flag.  */
29481   *saved_pedantic = pedantic;
29482 
29483   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
29484     {
29485       /* Consume the `__extension__' token.  */
29486       cp_lexer_consume_token (parser->lexer);
29487       /* We're not being pedantic while the `__extension__' keyword is
29488 	 in effect.  */
29489       pedantic = 0;
29490 
29491       return true;
29492     }
29493 
29494   return false;
29495 }
29496 
29497 /* Parse a label declaration.
29498 
29499    label-declaration:
29500      __label__ label-declarator-seq ;
29501 
29502    label-declarator-seq:
29503      identifier , label-declarator-seq
29504      identifier  */
29505 
29506 static void
cp_parser_label_declaration(cp_parser * parser)29507 cp_parser_label_declaration (cp_parser* parser)
29508 {
29509   /* Look for the `__label__' keyword.  */
29510   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
29511 
29512   while (true)
29513     {
29514       tree identifier;
29515 
29516       /* Look for an identifier.  */
29517       identifier = cp_parser_identifier (parser);
29518       /* If we failed, stop.  */
29519       if (identifier == error_mark_node)
29520 	break;
29521       /* Declare it as a label.  */
29522       finish_label_decl (identifier);
29523       /* If the next token is a `;', stop.  */
29524       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29525 	break;
29526       /* Look for the `,' separating the label declarations.  */
29527       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
29528     }
29529 
29530   /* Look for the final `;'.  */
29531   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29532 }
29533 
29534 // -------------------------------------------------------------------------- //
29535 // Concept definitions
29536 
29537 static tree
cp_parser_concept_definition(cp_parser * parser)29538 cp_parser_concept_definition (cp_parser *parser)
29539 {
29540   /* A concept definition is an unevaluated context.  */
29541   cp_unevaluated u;
29542 
29543   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
29544   cp_lexer_consume_token (parser->lexer);
29545 
29546   cp_expr id = cp_parser_identifier (parser);
29547   if (id == error_mark_node)
29548     {
29549       cp_parser_skip_to_end_of_statement (parser);
29550       cp_parser_consume_semicolon_at_end_of_statement (parser);
29551       return NULL_TREE;
29552     }
29553 
29554   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29555     {
29556       cp_parser_skip_to_end_of_statement (parser);
29557       cp_parser_consume_semicolon_at_end_of_statement (parser);
29558       return error_mark_node;
29559     }
29560 
29561   processing_constraint_expression_sentinel parsing_constraint;
29562   tree init = cp_parser_constraint_expression (parser);
29563   if (init == error_mark_node)
29564     cp_parser_skip_to_end_of_statement (parser);
29565 
29566   /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29567      but continue as if it were.  */
29568   cp_parser_consume_semicolon_at_end_of_statement (parser);
29569 
29570   return finish_concept_definition (id, init);
29571 }
29572 
29573 // -------------------------------------------------------------------------- //
29574 // Requires Clause
29575 
29576 /* Diagnose an expression that should appear in ()'s within a requires-clause
29577    and suggest where to place those parentheses.  */
29578 
29579 static void
cp_parser_diagnose_ungrouped_constraint_plain(location_t loc)29580 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
29581 {
29582   error_at (loc, "expression must be enclosed in parentheses");
29583 }
29584 
29585 static void
cp_parser_diagnose_ungrouped_constraint_rich(location_t loc)29586 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
29587 {
29588   gcc_rich_location richloc (loc);
29589   richloc.add_fixit_insert_before ("(");
29590   richloc.add_fixit_insert_after (")");
29591   error_at (&richloc, "expression must be enclosed in parentheses");
29592 }
29593 
29594 /* Characterizes the likely kind of expression intended by a mis-written
29595    primary constraint.  */
29596 enum primary_constraint_error
29597 {
29598   pce_ok,
29599   pce_maybe_operator,
29600   pce_maybe_postfix
29601 };
29602 
29603 /* Returns true if the token(s) following a primary-expression in a
29604    constraint-logical-* expression would require parentheses.  */
29605 
29606 static primary_constraint_error
cp_parser_constraint_requires_parens(cp_parser * parser,bool lambda_p)29607 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
29608 {
29609   cp_token *token = cp_lexer_peek_token (parser->lexer);
29610   switch (token->type)
29611     {
29612       default:
29613 	return pce_ok;
29614 
29615       case CPP_EQ:
29616 	{
29617 	  /* An equal sign may be part of the definition of a function,
29618 	     and not an assignment operator, when parsing the expression
29619 	     for a trailing requires-clause. For example:
29620 
29621 		template<typename T>
29622 		struct S {
29623 		  S() requires C<T> = default;
29624 		};
29625 
29626 	     Don't try to reparse this a binary operator.  */
29627 	  if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
29628 	      || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
29629 	    return pce_ok;
29630 
29631 	  gcc_fallthrough ();
29632 	}
29633 
29634       /* Arithmetic operators.  */
29635       case CPP_PLUS:
29636       case CPP_MINUS:
29637       case CPP_MULT:
29638       case CPP_DIV:
29639       case CPP_MOD:
29640       /* Bitwise operators.  */
29641       case CPP_AND:
29642       case CPP_OR:
29643       case CPP_XOR:
29644       case CPP_RSHIFT:
29645       case CPP_LSHIFT:
29646       /* Relational operators.  */
29647       case CPP_EQ_EQ:
29648       case CPP_NOT_EQ:
29649       case CPP_LESS:
29650       case CPP_GREATER:
29651       case CPP_LESS_EQ:
29652       case CPP_GREATER_EQ:
29653       case CPP_SPACESHIP:
29654       /* Pointer-to-member.  */
29655       case CPP_DOT_STAR:
29656       case CPP_DEREF_STAR:
29657       /* Assignment operators.  */
29658       case CPP_PLUS_EQ:
29659       case CPP_MINUS_EQ:
29660       case CPP_MULT_EQ:
29661       case CPP_DIV_EQ:
29662       case CPP_MOD_EQ:
29663       case CPP_AND_EQ:
29664       case CPP_OR_EQ:
29665       case CPP_XOR_EQ:
29666       case CPP_RSHIFT_EQ:
29667       case CPP_LSHIFT_EQ:
29668       /* Conditional operator */
29669       case CPP_QUERY:
29670 	/* Unenclosed binary or conditional operator.  */
29671 	return pce_maybe_operator;
29672 
29673       case CPP_OPEN_PAREN:
29674 	{
29675 	  /* A primary constraint that precedes the parameter-list of a
29676 	     lambda expression is followed by an open paren.
29677 
29678 		[]<typename T> requires C (T a, T b) { ... }
29679 
29680 	     Don't try to re-parse this as a postfix expression.  */
29681 	  if (lambda_p)
29682 	    return pce_ok;
29683 
29684 	  gcc_fallthrough ();
29685 	}
29686       case CPP_OPEN_SQUARE:
29687 	{
29688 	  /* A primary-constraint-expression followed by a '[[' is not a
29689 	     postfix expression.  */
29690 	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
29691 	    return pce_ok;
29692 
29693 	  gcc_fallthrough ();
29694 	}
29695       case CPP_PLUS_PLUS:
29696       case CPP_MINUS_MINUS:
29697       case CPP_DOT:
29698 	/* Unenclosed postfix operator.  */
29699 	return pce_maybe_postfix;
29700 
29701       case CPP_DEREF:
29702 	/* A primary constraint that precedes the lambda-declarator of a
29703 	   lambda expression is followed by trailing return type.
29704 
29705 	      []<typename T> requires C -> void {}
29706 
29707 	   Don't try to re-parse this as a postfix expression in
29708 	   C++23 and later.  In C++20 ( needs to come in between but we
29709 	   allow it to be omitted with pedwarn.  */
29710 	if (lambda_p)
29711 	  return pce_ok;
29712 	/* Unenclosed postfix operator.  */
29713 	return pce_maybe_postfix;
29714    }
29715 }
29716 
29717 /* Returns true if the next token begins a unary expression, preceded by
29718    an operator or keyword.  */
29719 
29720 static bool
cp_parser_unary_constraint_requires_parens(cp_parser * parser)29721 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
29722 {
29723   cp_token *token = cp_lexer_peek_token (parser->lexer);
29724   switch (token->type)
29725     {
29726       case CPP_NOT:
29727       case CPP_PLUS:
29728       case CPP_MINUS:
29729       case CPP_MULT:
29730       case CPP_COMPL:
29731       case CPP_PLUS_PLUS:
29732       case CPP_MINUS_MINUS:
29733 	return true;
29734 
29735       case CPP_KEYWORD:
29736 	{
29737 	  switch (token->keyword)
29738 	    {
29739 	      case RID_STATCAST:
29740 	      case RID_DYNCAST:
29741 	      case RID_REINTCAST:
29742 	      case RID_CONSTCAST:
29743 	      case RID_TYPEID:
29744 	      case RID_SIZEOF:
29745 	      case RID_ALIGNOF:
29746 	      case RID_NOEXCEPT:
29747 	      case RID_NEW:
29748 	      case RID_DELETE:
29749 	      case RID_THROW:
29750 		return true;
29751 
29752 	     default:
29753 		break;
29754 	  }
29755 	}
29756 
29757       default:
29758 	break;
29759     }
29760 
29761   return false;
29762 }
29763 
29764 /* Parse a primary expression within a constraint.  */
29765 
29766 static cp_expr
cp_parser_constraint_primary_expression(cp_parser * parser,bool lambda_p)29767 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
29768 {
29769   /* If this looks like a unary expression, parse it as such, but diagnose
29770      it as ill-formed; it requires parens.  */
29771   if (cp_parser_unary_constraint_requires_parens (parser))
29772     {
29773       cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
29774       cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
29775       return e;
29776     }
29777 
29778   cp_lexer_save_tokens (parser->lexer);
29779   cp_id_kind idk;
29780   location_t loc = input_location;
29781   cp_expr expr = cp_parser_primary_expression (parser,
29782 					       /*address_p=*/false,
29783 					       /*cast_p=*/false,
29784 					       /*template_arg_p=*/false,
29785 					       &idk);
29786   expr.maybe_add_location_wrapper ();
29787 
29788   primary_constraint_error pce = pce_ok;
29789   if (expr != error_mark_node)
29790     {
29791       /* The primary-expression could be part of an unenclosed non-logical
29792 	 compound expression.  */
29793       pce = cp_parser_constraint_requires_parens (parser, lambda_p);
29794     }
29795   if (pce == pce_ok)
29796     {
29797       cp_lexer_commit_tokens (parser->lexer);
29798       return finish_constraint_primary_expr (expr);
29799     }
29800 
29801   /* Retry the parse at a lower precedence. If that succeeds, diagnose the
29802      error, but return the expression as if it were valid.  */
29803   cp_lexer_rollback_tokens (parser->lexer);
29804   cp_parser_parse_tentatively (parser);
29805   if (pce == pce_maybe_operator)
29806     expr = cp_parser_assignment_expression (parser, NULL, false, false);
29807   else
29808     expr = cp_parser_simple_cast_expression (parser);
29809   if (cp_parser_parse_definitely (parser))
29810     {
29811       cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
29812       return expr;
29813     }
29814 
29815   /* Otherwise, something has gone very wrong, and we can't generate a more
29816      meaningful diagnostic or recover.  */
29817   cp_parser_diagnose_ungrouped_constraint_plain (loc);
29818   return error_mark_node;
29819 }
29820 
29821 /* Parse a constraint-logical-and-expression.
29822 
29823      constraint-logical-and-expression:
29824        primary-expression
29825        constraint-logical-and-expression '&&' primary-expression  */
29826 
29827 static cp_expr
cp_parser_constraint_logical_and_expression(cp_parser * parser,bool lambda_p)29828 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
29829 {
29830   cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29831   while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
29832     {
29833       cp_token *op = cp_lexer_consume_token (parser->lexer);
29834       tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29835       lhs = finish_constraint_and_expr (op->location, lhs, rhs);
29836     }
29837   return lhs;
29838 }
29839 
29840 /* Parse a constraint-logical-or-expression.
29841 
29842      constraint-logical-or-expression:
29843        constraint-logical-and-expression
29844        constraint-logical-or-expression '||' constraint-logical-and-expression  */
29845 
29846 static cp_expr
cp_parser_constraint_logical_or_expression(cp_parser * parser,bool lambda_p)29847 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
29848 {
29849   cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29850   while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
29851     {
29852       cp_token *op = cp_lexer_consume_token (parser->lexer);
29853       cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29854       lhs = finish_constraint_or_expr (op->location, lhs, rhs);
29855     }
29856   return lhs;
29857 }
29858 
29859 /* Parse the expression after a requires-clause. This has a different grammar
29860     than that in the concepts TS.  */
29861 
29862 static tree
cp_parser_requires_clause_expression(cp_parser * parser,bool lambda_p)29863 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
29864 {
29865   processing_constraint_expression_sentinel parsing_constraint;
29866   ++processing_template_decl;
29867   cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
29868   --processing_template_decl;
29869   if (check_for_bare_parameter_packs (expr))
29870     expr = error_mark_node;
29871   return expr;
29872 }
29873 
29874 /* Parse a expression after a requires clause.
29875 
29876     constraint-expression:
29877       logical-or-expression
29878 
29879    The required logical-or-expression must be a constant expression. Note
29880    that we don't check that the expression is constepxr here. We defer until
29881    we analyze constraints and then, we only check atomic constraints.  */
29882 
29883 static tree
cp_parser_constraint_expression(cp_parser * parser)29884 cp_parser_constraint_expression (cp_parser *parser)
29885 {
29886   processing_constraint_expression_sentinel parsing_constraint;
29887   ++processing_template_decl;
29888   cp_expr expr = cp_parser_binary_expression (parser, false, true,
29889 					      PREC_NOT_OPERATOR, NULL);
29890   --processing_template_decl;
29891   if (check_for_bare_parameter_packs (expr))
29892     expr = error_mark_node;
29893   expr.maybe_add_location_wrapper ();
29894   return expr;
29895 }
29896 
29897 /* Optionally parse a requires clause:
29898 
29899       requires-clause:
29900 	`requires` constraint-logical-or-expression.
29901    [ConceptsTS]
29902 	`requires constraint-expression.
29903 
29904    LAMBDA_P is true when the requires-clause is parsed before the
29905    parameter-list of a lambda-declarator.  */
29906 
29907 static tree
cp_parser_requires_clause_opt(cp_parser * parser,bool lambda_p)29908 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
29909 {
29910   /* A requires clause is an unevaluated context.  */
29911   cp_unevaluated u;
29912 
29913   cp_token *tok = cp_lexer_peek_token (parser->lexer);
29914   if (tok->keyword != RID_REQUIRES)
29915     {
29916       if (!flag_concepts && tok->type == CPP_NAME
29917 	  && tok->u.value == ridpointers[RID_REQUIRES])
29918 	{
29919 	  error_at (cp_lexer_peek_token (parser->lexer)->location,
29920 		    "%<requires%> only available with "
29921 		    "%<-std=c++20%> or %<-fconcepts%>");
29922 	  /* Parse and discard the requires-clause.  */
29923 	  cp_lexer_consume_token (parser->lexer);
29924 	  cp_parser_constraint_expression (parser);
29925 	}
29926       return NULL_TREE;
29927     }
29928 
29929   cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
29930   if (tok2->type == CPP_OPEN_BRACE)
29931     {
29932       /* An opening brace following the start of a requires-clause is
29933 	 ill-formed; the user likely forgot the second `requires' that
29934 	 would start a requires-expression.  */
29935       gcc_rich_location richloc (tok2->location);
29936       richloc.add_fixit_insert_after (tok->location, " requires");
29937       error_at (&richloc, "missing additional %<requires%> to start "
29938 		"a requires-expression");
29939       /* Don't consume the `requires', so that it's reused as the start of a
29940 	 requires-expression.  */
29941     }
29942   else
29943     cp_lexer_consume_token (parser->lexer);
29944 
29945   if (!flag_concepts_ts)
29946     return cp_parser_requires_clause_expression (parser, lambda_p);
29947   else
29948     return cp_parser_constraint_expression (parser);
29949 }
29950 
29951 /*---------------------------------------------------------------------------
29952                            Requires expressions
29953 ---------------------------------------------------------------------------*/
29954 
29955 /* Parse a requires expression
29956 
29957    requirement-expression:
29958        'requires' requirement-parameter-list [opt] requirement-body */
29959 
29960 static tree
cp_parser_requires_expression(cp_parser * parser)29961 cp_parser_requires_expression (cp_parser *parser)
29962 {
29963   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
29964   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
29965 
29966   /* Avoid committing to outer tentative parse.  */
29967   tentative_firewall firewall (parser);
29968 
29969   /* This is definitely a requires-expression.  */
29970   cp_parser_commit_to_tentative_parse (parser);
29971 
29972   tree parms, reqs;
29973   {
29974     /* Local parameters are delared as variables within the scope
29975        of the expression.  They are not visible past the end of
29976        the expression.  Expressions within the requires-expression
29977        are unevaluated.  */
29978     struct scope_sentinel
29979     {
29980       scope_sentinel ()
29981       {
29982 	++cp_unevaluated_operand;
29983 	begin_scope (sk_function_parms, NULL_TREE);
29984 	current_binding_level->requires_expression = true;
29985       }
29986 
29987       ~scope_sentinel ()
29988       {
29989 	pop_bindings_and_leave_scope ();
29990 	--cp_unevaluated_operand;
29991       }
29992     } s;
29993 
29994     /* Parse the optional parameter list. */
29995     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29996       {
29997 	parms = cp_parser_requirement_parameter_list (parser);
29998 	if (parms == error_mark_node)
29999 	  return error_mark_node;
30000       }
30001     else
30002       parms = NULL_TREE;
30003 
30004     /* Parse the requirement body. */
30005     ++processing_template_decl;
30006     reqs = cp_parser_requirement_body (parser);
30007     --processing_template_decl;
30008     if (reqs == error_mark_node)
30009       return error_mark_node;
30010   }
30011 
30012   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30013      the parm chain.  */
30014   grokparms (parms, &parms);
30015   loc = make_location (loc, loc, parser->lexer);
30016   tree expr = finish_requires_expr (loc, parms, reqs);
30017   if (!processing_template_decl)
30018     {
30019       /* Perform semantic processing now to diagnose any invalid types and
30020 	 expressions.  */
30021       int saved_errorcount = errorcount;
30022       tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30023       if (errorcount > saved_errorcount)
30024 	return error_mark_node;
30025     }
30026   return expr;
30027 }
30028 
30029 /* Parse a parameterized requirement.
30030 
30031    requirement-parameter-list:
30032        '(' parameter-declaration-clause ')' */
30033 
30034 static tree
cp_parser_requirement_parameter_list(cp_parser * parser)30035 cp_parser_requirement_parameter_list (cp_parser *parser)
30036 {
30037   matching_parens parens;
30038   if (!parens.require_open (parser))
30039     return error_mark_node;
30040 
30041   tree parms = (cp_parser_parameter_declaration_clause
30042 		(parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30043 
30044   if (!parens.require_close (parser))
30045     return error_mark_node;
30046 
30047   /* Modify the declared parameters by removing their context
30048      so they don't refer to the enclosing scope and explicitly
30049      indicating that they are constraint variables. */
30050   for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
30051     {
30052       if (parm == void_list_node || parm == explicit_void_list_node)
30053 	break;
30054       tree decl = TREE_VALUE (parm);
30055       if (decl != error_mark_node)
30056 	{
30057 	  DECL_CONTEXT (decl) = NULL_TREE;
30058 	  CONSTRAINT_VAR_P (decl) = true;
30059 	}
30060     }
30061 
30062   return parms;
30063 }
30064 
30065 /* Parse the body of a requirement.
30066 
30067    requirement-body:
30068        '{' requirement-list '}' */
30069 static tree
cp_parser_requirement_body(cp_parser * parser)30070 cp_parser_requirement_body (cp_parser *parser)
30071 {
30072   matching_braces braces;
30073   if (!braces.require_open (parser))
30074     return error_mark_node;
30075 
30076   tree reqs = cp_parser_requirement_seq (parser);
30077 
30078   if (!braces.require_close (parser))
30079     return error_mark_node;
30080 
30081   return reqs;
30082 }
30083 
30084 /* Parse a sequence of requirements.
30085 
30086    requirement-seq:
30087        requirement
30088        requirement-seq requirement */
30089 
30090 static tree
cp_parser_requirement_seq(cp_parser * parser)30091 cp_parser_requirement_seq (cp_parser *parser)
30092 {
30093   tree result = NULL_TREE;
30094   do
30095     {
30096       tree req = cp_parser_requirement (parser);
30097       if (req != error_mark_node)
30098 	result = tree_cons (NULL_TREE, req, result);
30099     }
30100   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30101 	 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30102 
30103   /* If there are no valid requirements, this is not a valid expression. */
30104   if (!result)
30105     return error_mark_node;
30106 
30107   /* Reverse the order of requirements so they are analyzed in order. */
30108   return nreverse (result);
30109 }
30110 
30111 /* Parse a syntactic requirement or type requirement.
30112 
30113      requirement:
30114        simple-requirement
30115        compound-requirement
30116        type-requirement
30117        nested-requirement */
30118 
30119 static tree
cp_parser_requirement(cp_parser * parser)30120 cp_parser_requirement (cp_parser *parser)
30121 {
30122   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30123     return cp_parser_compound_requirement (parser);
30124   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30125     return cp_parser_type_requirement (parser);
30126   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30127     return cp_parser_nested_requirement (parser);
30128   else
30129     return cp_parser_simple_requirement (parser);
30130 }
30131 
30132 /* Parse a simple requirement.
30133 
30134      simple-requirement:
30135        expression ';' */
30136 
30137 static tree
cp_parser_simple_requirement(cp_parser * parser)30138 cp_parser_simple_requirement (cp_parser *parser)
30139 {
30140   location_t start = cp_lexer_peek_token (parser->lexer)->location;
30141   cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30142   if (expr == error_mark_node)
30143     cp_parser_skip_to_end_of_statement (parser);
30144 
30145   cp_parser_consume_semicolon_at_end_of_statement (parser);
30146 
30147   if (!expr || expr == error_mark_node)
30148     return error_mark_node;
30149 
30150   /* Sometimes we don't get locations, so use the cached token location
30151      as a reasonable approximation.  */
30152   if (expr.get_location() == UNKNOWN_LOCATION)
30153     expr.set_location (start);
30154 
30155   for (tree t = expr; ; )
30156     {
30157       if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30158 	  || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30159 	{
30160 	  t = TREE_OPERAND (t, 0);
30161 	  continue;
30162 	}
30163       if (concept_check_p (t))
30164 	{
30165 	  gcc_rich_location richloc (get_start (start));
30166 	  richloc.add_fixit_insert_before (start, "requires ");
30167 	  warning_at (&richloc, OPT_Wmissing_requires, "testing "
30168 		      "if a concept-id is a valid expression; add "
30169 		      "%<requires%> to check satisfaction");
30170 	}
30171       break;
30172     }
30173 
30174   return finish_simple_requirement (expr.get_location (), expr);
30175 }
30176 
30177 /* Parse a type requirement
30178 
30179      type-requirement
30180          nested-name-specifier [opt] required-type-name ';'
30181 
30182      required-type-name:
30183          type-name
30184          'template' [opt] simple-template-id  */
30185 
30186 static tree
cp_parser_type_requirement(cp_parser * parser)30187 cp_parser_type_requirement (cp_parser *parser)
30188 {
30189   cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30190   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30191 
30192   // Save the scope before parsing name specifiers.
30193   tree saved_scope = parser->scope;
30194   tree saved_object_scope = parser->object_scope;
30195   tree saved_qualifying_scope = parser->qualifying_scope;
30196   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30197   cp_parser_nested_name_specifier_opt (parser,
30198                                        /*typename_keyword_p=*/true,
30199                                        /*check_dependency_p=*/false,
30200                                        /*type_p=*/true,
30201                                        /*is_declaration=*/false);
30202 
30203   tree type;
30204   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30205     {
30206       cp_lexer_consume_token (parser->lexer);
30207       type = cp_parser_template_id (parser,
30208                                     /*template_keyword_p=*/true,
30209                                     /*check_dependency=*/false,
30210                                     /*tag_type=*/none_type,
30211                                     /*is_declaration=*/false);
30212       type = make_typename_type (parser->scope, type, typename_type,
30213                                  /*complain=*/tf_error);
30214     }
30215   else
30216    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30217 
30218   if (TREE_CODE (type) == TYPE_DECL)
30219     type = TREE_TYPE (type);
30220 
30221   parser->scope = saved_scope;
30222   parser->object_scope = saved_object_scope;
30223   parser->qualifying_scope = saved_qualifying_scope;
30224 
30225   if (type == error_mark_node)
30226     cp_parser_skip_to_end_of_statement (parser);
30227 
30228   cp_parser_consume_semicolon_at_end_of_statement (parser);
30229 
30230   if (type == error_mark_node)
30231     return error_mark_node;
30232 
30233   loc = make_location (loc, start_tok->location, parser->lexer);
30234   return finish_type_requirement (loc, type);
30235 }
30236 
30237 /* Parse a compound requirement
30238 
30239      compound-requirement:
30240          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30241 
30242 static tree
cp_parser_compound_requirement(cp_parser * parser)30243 cp_parser_compound_requirement (cp_parser *parser)
30244 {
30245   /* Parse an expression enclosed in '{ }'s. */
30246   matching_braces braces;
30247   if (!braces.require_open (parser))
30248     return error_mark_node;
30249 
30250   cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
30251 
30252   tree expr = cp_parser_expression (parser, NULL, false, false);
30253   if (expr == error_mark_node)
30254     cp_parser_skip_to_closing_brace (parser);
30255 
30256   if (!braces.require_close (parser))
30257     {
30258       cp_parser_skip_to_end_of_statement (parser);
30259       cp_parser_consume_semicolon_at_end_of_statement (parser);
30260       return error_mark_node;
30261     }
30262 
30263   /* If the expression was invalid, skip the remainder of the requirement.  */
30264   if (!expr || expr == error_mark_node)
30265     {
30266       cp_parser_skip_to_end_of_statement (parser);
30267       cp_parser_consume_semicolon_at_end_of_statement (parser);
30268       return error_mark_node;
30269     }
30270 
30271   /* Parse the optional noexcept. */
30272   bool noexcept_p = false;
30273   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
30274     {
30275       cp_lexer_consume_token (parser->lexer);
30276       noexcept_p = true;
30277     }
30278 
30279   /* Parse the optional trailing return type. */
30280   tree type = NULL_TREE;
30281   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
30282     {
30283       cp_lexer_consume_token (parser->lexer);
30284       cp_token *tok = cp_lexer_peek_token (parser->lexer);
30285 
30286       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
30287       parser->in_result_type_constraint_p = true;
30288       /* C++20 allows either a type-id or a type-constraint. Parsing
30289          a type-id will subsume the parsing for a type-constraint but
30290          allow for more syntactic forms (e.g., const C<T>*).  */
30291       type = cp_parser_trailing_type_id (parser);
30292       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
30293       if (type == error_mark_node)
30294         return error_mark_node;
30295 
30296       location_t type_loc = make_location (tok->location, tok->location,
30297 					   parser->lexer);
30298 
30299       /* Check that we haven't written something like 'const C<T>*'.  */
30300       if (type_uses_auto (type))
30301 	{
30302 	  if (!is_auto (type))
30303 	    {
30304 	      error_at (type_loc,
30305 			"result type is not a plain type-constraint");
30306 	      cp_parser_consume_semicolon_at_end_of_statement (parser);
30307 	      return error_mark_node;
30308 	    }
30309 	}
30310       else if (!flag_concepts_ts)
30311 	/* P1452R2 removed the trailing-return-type option.  */
30312 	error_at (type_loc,
30313 		  "return-type-requirement is not a type-constraint");
30314     }
30315 
30316   location_t loc = make_location (expr_token->location,
30317 				  braces.open_location (),
30318 				  parser->lexer);
30319 
30320   cp_parser_consume_semicolon_at_end_of_statement (parser);
30321 
30322   if (expr == error_mark_node || type == error_mark_node)
30323     return error_mark_node;
30324 
30325   return finish_compound_requirement (loc, expr, type, noexcept_p);
30326 }
30327 
30328 /* Parse a nested requirement. This is the same as a requires clause.
30329 
30330    nested-requirement:
30331      requires-clause */
30332 
30333 static tree
cp_parser_nested_requirement(cp_parser * parser)30334 cp_parser_nested_requirement (cp_parser *parser)
30335 {
30336   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30337   cp_token *tok = cp_lexer_consume_token (parser->lexer);
30338   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30339   tree req = cp_parser_constraint_expression (parser);
30340   if (req == error_mark_node)
30341     cp_parser_skip_to_end_of_statement (parser);
30342   loc = make_location (loc, tok->location, parser->lexer);
30343   cp_parser_consume_semicolon_at_end_of_statement (parser);
30344   if (req == error_mark_node)
30345     return error_mark_node;
30346   return finish_nested_requirement (loc, req);
30347 }
30348 
30349 /* Support Functions */
30350 
30351 /* Return the appropriate prefer_type argument for lookup_name based on
30352    tag_type.  */
30353 
30354 static inline LOOK_want
prefer_type_arg(tag_types tag_type)30355 prefer_type_arg (tag_types tag_type)
30356 {
30357   switch (tag_type)
30358     {
30359     case none_type:  return LOOK_want::NORMAL;	// No preference.
30360     case scope_type: return LOOK_want::TYPE_NAMESPACE;	// Type or namespace.
30361     default:         return LOOK_want::TYPE;	// Type only.
30362     }
30363 }
30364 
30365 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30366    NAME should have one of the representations used for an
30367    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30368    is returned.  If PARSER->SCOPE is a dependent type, then a
30369    SCOPE_REF is returned.
30370 
30371    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30372    returned; the name was already resolved when the TEMPLATE_ID_EXPR
30373    was formed.  Abstractly, such entities should not be passed to this
30374    function, because they do not need to be looked up, but it is
30375    simpler to check for this special case here, rather than at the
30376    call-sites.
30377 
30378    In cases not explicitly covered above, this function returns a
30379    DECL, OVERLOAD, or baselink representing the result of the lookup.
30380    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30381    is returned.
30382 
30383    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30384    (e.g., "struct") that was used.  In that case bindings that do not
30385    refer to types are ignored.
30386 
30387    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30388    ignored.  If IS_TEMPLATE IS 2, the 'template' keyword was specified.
30389 
30390    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30391    are ignored.
30392 
30393    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30394    types.
30395 
30396    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30397    TREE_LIST of candidates if name-lookup results in an ambiguity, and
30398    NULL_TREE otherwise.  */
30399 
30400 static cp_expr
cp_parser_lookup_name(cp_parser * parser,tree name,enum tag_types tag_type,int is_template,bool is_namespace,bool check_dependency,tree * ambiguous_decls,location_t name_location)30401 cp_parser_lookup_name (cp_parser *parser, tree name,
30402 		       enum tag_types tag_type,
30403 		       int is_template,
30404 		       bool is_namespace,
30405 		       bool check_dependency,
30406 		       tree *ambiguous_decls,
30407 		       location_t name_location)
30408 {
30409   tree decl;
30410   tree object_type = parser->context->object_type;
30411 
30412   /* Assume that the lookup will be unambiguous.  */
30413   if (ambiguous_decls)
30414     *ambiguous_decls = NULL_TREE;
30415 
30416   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30417      no longer valid.  Note that if we are parsing tentatively, and
30418      the parse fails, OBJECT_TYPE will be automatically restored.  */
30419   parser->context->object_type = NULL_TREE;
30420 
30421   if (name == error_mark_node)
30422     return error_mark_node;
30423 
30424   /* A template-id has already been resolved; there is no lookup to
30425      do.  */
30426   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
30427     return name;
30428   if (BASELINK_P (name))
30429     {
30430       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
30431 		  == TEMPLATE_ID_EXPR);
30432       return name;
30433     }
30434 
30435   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
30436      it should already have been checked to make sure that the name
30437      used matches the type being destroyed.  */
30438   if (TREE_CODE (name) == BIT_NOT_EXPR)
30439     {
30440       tree type;
30441 
30442       /* Figure out to which type this destructor applies.  */
30443       if (parser->scope)
30444 	type = parser->scope;
30445       else if (object_type)
30446 	type = object_type;
30447       else
30448 	type = current_class_type;
30449       /* If that's not a class type, there is no destructor.  */
30450       if (!type || !CLASS_TYPE_P (type))
30451 	return error_mark_node;
30452 
30453       /* In a non-static member function, check implicit this->.  */
30454       if (current_class_ref)
30455 	return lookup_destructor (current_class_ref, parser->scope, name,
30456 				  tf_warning_or_error);
30457 
30458       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
30459 	lazily_declare_fn (sfk_destructor, type);
30460 
30461       if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
30462 	return dtor;
30463 
30464       return error_mark_node;
30465     }
30466 
30467   /* By this point, the NAME should be an ordinary identifier.  If
30468      the id-expression was a qualified name, the qualifying scope is
30469      stored in PARSER->SCOPE at this point.  */
30470   gcc_assert (identifier_p (name));
30471 
30472   /* Perform the lookup.  */
30473   if (parser->scope)
30474     {
30475       bool dependent_p;
30476 
30477       if (parser->scope == error_mark_node)
30478 	return error_mark_node;
30479 
30480       /* If the SCOPE is dependent, the lookup must be deferred until
30481 	 the template is instantiated -- unless we are explicitly
30482 	 looking up names in uninstantiated templates.  Even then, we
30483 	 cannot look up the name if the scope is not a class type; it
30484 	 might, for example, be a template type parameter.  */
30485       dependent_p = (TYPE_P (parser->scope)
30486 		     && dependent_scope_p (parser->scope));
30487       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
30488 	  && dependent_p)
30489 	/* Defer lookup.  */
30490 	decl = error_mark_node;
30491       else
30492 	{
30493 	  tree pushed_scope = NULL_TREE;
30494 
30495 	  /* If PARSER->SCOPE is a dependent type, then it must be a
30496 	     class type, and we must not be checking dependencies;
30497 	     otherwise, we would have processed this lookup above.  So
30498 	     that PARSER->SCOPE is not considered a dependent base by
30499 	     lookup_member, we must enter the scope here.  */
30500 	  if (dependent_p)
30501 	    pushed_scope = push_scope (parser->scope);
30502 
30503 	  /* If the PARSER->SCOPE is a template specialization, it
30504 	     may be instantiated during name lookup.  In that case,
30505 	     errors may be issued.  Even if we rollback the current
30506 	     tentative parse, those errors are valid.  */
30507 	  decl = lookup_qualified_name (parser->scope, name,
30508 					prefer_type_arg (tag_type),
30509 					/*complain=*/true);
30510 
30511 	  /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30512 	     lookup result and the nested-name-specifier nominates a class C:
30513 	       * if the name specified after the nested-name-specifier, when
30514 	       looked up in C, is the injected-class-name of C (Clause 9), or
30515 	       * if the name specified after the nested-name-specifier is the
30516 	       same as the identifier or the simple-template-id's template-
30517 	       name in the last component of the nested-name-specifier,
30518 	     the name is instead considered to name the constructor of
30519 	     class C. [ Note: for example, the constructor is not an
30520 	     acceptable lookup result in an elaborated-type-specifier so
30521 	     the constructor would not be used in place of the
30522 	     injected-class-name. --end note ] Such a constructor name
30523 	     shall be used only in the declarator-id of a declaration that
30524 	     names a constructor or in a using-declaration.  */
30525 	  if (tag_type == none_type
30526 	      && DECL_SELF_REFERENCE_P (decl)
30527 	      && same_type_p (DECL_CONTEXT (decl), parser->scope))
30528 	    decl = lookup_qualified_name (parser->scope, ctor_identifier,
30529 					  prefer_type_arg (tag_type),
30530 					  /*complain=*/true);
30531 
30532 	  if (pushed_scope)
30533 	    pop_scope (pushed_scope);
30534 	}
30535 
30536       /* If the scope is a dependent type and either we deferred lookup or
30537 	 we did lookup but didn't find the name, rememeber the name.  */
30538       if (decl == error_mark_node && TYPE_P (parser->scope)
30539 	  && dependent_type_p (parser->scope))
30540 	{
30541 	  if (tag_type)
30542 	    {
30543 	      tree type;
30544 
30545 	      /* The resolution to Core Issue 180 says that `struct
30546 		 A::B' should be considered a type-name, even if `A'
30547 		 is dependent.  */
30548 	      type = make_typename_type (parser->scope, name, tag_type,
30549 					 /*complain=*/tf_error);
30550 	      if (type != error_mark_node)
30551 		decl = TYPE_NAME (type);
30552 	    }
30553 	  else if (is_template
30554 		   && (cp_parser_next_token_ends_template_argument_p (parser)
30555 		       || cp_lexer_next_token_is (parser->lexer,
30556 						  CPP_CLOSE_PAREN)))
30557 	    decl = make_unbound_class_template (parser->scope,
30558 						name, NULL_TREE,
30559 						/*complain=*/tf_error);
30560 	  else
30561 	    decl = build_qualified_name (/*type=*/NULL_TREE,
30562 					 parser->scope, name,
30563 					 is_template);
30564 	}
30565       parser->qualifying_scope = parser->scope;
30566       parser->object_scope = NULL_TREE;
30567     }
30568   else if (object_type)
30569     {
30570       bool dep = dependent_scope_p (object_type);
30571 
30572       /* Look up the name in the scope of the OBJECT_TYPE, unless the
30573 	 OBJECT_TYPE is not a class.  */
30574       if (CLASS_TYPE_P (object_type)
30575 	  && !(dep && LAMBDA_TYPE_P (object_type)))
30576 	/* If the OBJECT_TYPE is a template specialization, it may
30577 	   be instantiated during name lookup.  In that case, errors
30578 	   may be issued.  Even if we rollback the current tentative
30579 	   parse, those errors are valid.  */
30580 	decl = lookup_member (object_type,
30581 			      name,
30582 			      /*protect=*/0,
30583 			      /*prefer_type=*/tag_type != none_type,
30584 			      tf_warning_or_error);
30585       else
30586 	decl = NULL_TREE;
30587 
30588       /* If we didn't find a member and have dependent bases, the member lookup
30589 	 is now dependent.  */
30590       if (!dep && !decl && any_dependent_bases_p (object_type))
30591 	dep = true;
30592 
30593       if (dep && is_template == 2)
30594 	/* The template keyword specifies a dependent template.  */;
30595       else if (!decl)
30596 	/* Look it up in the enclosing context.  DR 141: When looking for a
30597 	   template-name after -> or ., only consider class templates.  */
30598 	decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30599 			    /* DR 141: When looking in the
30600 			       current enclosing context for a
30601 			       template-name after -> or ., only
30602 			       consider class templates.  */
30603 			    : is_template ? LOOK_want::TYPE
30604 			    : prefer_type_arg (tag_type));
30605 
30606       /* If we know we're looking for a type (e.g. A in p->A::x),
30607 	 mock up a typename.  */
30608       if (!decl && dep && tag_type != none_type)
30609 	{
30610 	  tree type = build_typename_type (object_type, name, name,
30611 					   typename_type);
30612 	  decl = TYPE_NAME (type);
30613 	}
30614 
30615       parser->object_scope = object_type;
30616       parser->qualifying_scope = NULL_TREE;
30617     }
30618   else
30619     {
30620       decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30621 			  : prefer_type_arg (tag_type));
30622       parser->qualifying_scope = NULL_TREE;
30623       parser->object_scope = NULL_TREE;
30624     }
30625 
30626   /* If the lookup failed, let our caller know.  */
30627   if (!decl || decl == error_mark_node)
30628     return error_mark_node;
30629 
30630   /* If we have resolved the name of a member declaration, check to
30631      see if the declaration is accessible.  When the name resolves to
30632      set of overloaded functions, accessibility is checked when
30633      overload resolution is done.  If we have a TREE_LIST, then the lookup
30634      is either ambiguous or it found multiple injected-class-names, the
30635      accessibility of which is trivially satisfied.
30636 
30637      During an explicit instantiation, access is not checked at all,
30638      as per [temp.explicit].  */
30639   if (DECL_P (decl))
30640     check_accessibility_of_qualified_id (decl, object_type, parser->scope,
30641 					 tf_warning_or_error);
30642 
30643   /* Pull out the template from an injected-class-name (or multiple).  */
30644   if (is_template)
30645     decl = maybe_get_template_decl_from_type_decl (decl);
30646 
30647   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
30648   if (TREE_CODE (decl) == TREE_LIST)
30649     {
30650       if (ambiguous_decls)
30651 	*ambiguous_decls = decl;
30652       /* The error message we have to print is too complicated for
30653 	 cp_parser_error, so we incorporate its actions directly.  */
30654       if (!cp_parser_simulate_error (parser))
30655 	{
30656 	  error_at (name_location, "reference to %qD is ambiguous",
30657 		    name);
30658 	  print_candidates (decl);
30659 	}
30660       return error_mark_node;
30661     }
30662 
30663   gcc_assert (DECL_P (decl)
30664 	      || TREE_CODE (decl) == OVERLOAD
30665 	      || TREE_CODE (decl) == SCOPE_REF
30666 	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
30667 	      || BASELINK_P (decl));
30668 
30669   maybe_record_typedef_use (decl);
30670 
30671   return cp_expr (decl, name_location);
30672 }
30673 
30674 /* Like cp_parser_lookup_name, but for use in the typical case where
30675    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30676    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
30677 
30678 static tree
cp_parser_lookup_name_simple(cp_parser * parser,tree name,location_t location)30679 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
30680 {
30681   return cp_parser_lookup_name (parser, name,
30682 				none_type,
30683 				/*is_template=*/false,
30684 				/*is_namespace=*/false,
30685 				/*check_dependency=*/true,
30686 				/*ambiguous_decls=*/NULL,
30687 				location);
30688 }
30689 
30690 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30691    the current context, return the TYPE_DECL.  If TAG_NAME_P is
30692    true, the DECL indicates the class being defined in a class-head,
30693    or declared in an elaborated-type-specifier.
30694 
30695    Otherwise, return DECL.  */
30696 
30697 static tree
cp_parser_maybe_treat_template_as_class(tree decl,bool tag_name_p)30698 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
30699 {
30700   /* If the TEMPLATE_DECL is being declared as part of a class-head,
30701      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30702 
30703        struct A {
30704 	 template <typename T> struct B;
30705        };
30706 
30707        template <typename T> struct A::B {};
30708 
30709      Similarly, in an elaborated-type-specifier:
30710 
30711        namespace N { struct X{}; }
30712 
30713        struct A {
30714 	 template <typename T> friend struct N::X;
30715        };
30716 
30717      However, if the DECL refers to a class type, and we are in
30718      the scope of the class, then the name lookup automatically
30719      finds the TYPE_DECL created by build_self_reference rather
30720      than a TEMPLATE_DECL.  For example, in:
30721 
30722        template <class T> struct S {
30723 	 S s;
30724        };
30725 
30726      there is no need to handle such case.  */
30727 
30728   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
30729     return DECL_TEMPLATE_RESULT (decl);
30730 
30731   return decl;
30732 }
30733 
30734 /* If too many, or too few, template-parameter lists apply to the
30735    declarator, issue an error message.  Returns TRUE if all went well,
30736    and FALSE otherwise.  */
30737 
30738 static bool
cp_parser_check_declarator_template_parameters(cp_parser * parser,cp_declarator * declarator,location_t declarator_location)30739 cp_parser_check_declarator_template_parameters (cp_parser* parser,
30740 						cp_declarator *declarator,
30741 						location_t declarator_location)
30742 {
30743   switch (declarator->kind)
30744     {
30745     case cdk_id:
30746       {
30747 	unsigned num_templates = 0;
30748 	tree scope = declarator->u.id.qualifying_scope;
30749 	bool template_id_p = false;
30750 
30751 	if (scope)
30752 	  num_templates = num_template_headers_for_class (scope);
30753 	else if (TREE_CODE (declarator->u.id.unqualified_name)
30754 		 == TEMPLATE_ID_EXPR)
30755 	  {
30756 	    /* If the DECLARATOR has the form `X<y>' then it uses one
30757 	       additional level of template parameters.  */
30758 	    ++num_templates;
30759 	    template_id_p = true;
30760 	  }
30761 
30762 	return cp_parser_check_template_parameters
30763 	  (parser, num_templates, template_id_p, declarator_location,
30764 	   declarator);
30765       }
30766 
30767     case cdk_function:
30768     case cdk_array:
30769     case cdk_pointer:
30770     case cdk_reference:
30771     case cdk_ptrmem:
30772       return (cp_parser_check_declarator_template_parameters
30773 	      (parser, declarator->declarator, declarator_location));
30774 
30775     case cdk_decomp:
30776     case cdk_error:
30777       return true;
30778 
30779     default:
30780       gcc_unreachable ();
30781     }
30782   return false;
30783 }
30784 
30785 /* NUM_TEMPLATES were used in the current declaration.  If that is
30786    invalid, return FALSE and issue an error messages.  Otherwise,
30787    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
30788    declarator and we can print more accurate diagnostics.  */
30789 
30790 static bool
cp_parser_check_template_parameters(cp_parser * parser,unsigned num_templates,bool template_id_p,location_t location,cp_declarator * declarator)30791 cp_parser_check_template_parameters (cp_parser* parser,
30792 				     unsigned num_templates,
30793 				     bool template_id_p,
30794 				     location_t location,
30795 				     cp_declarator *declarator)
30796 {
30797   /* If there are the same number of template classes and parameter
30798      lists, that's OK.  */
30799   if (parser->num_template_parameter_lists == num_templates)
30800     return true;
30801   /* If there are more, but only one more, and the name ends in an identifier,
30802      then we are declaring a primary template.  That's OK too.  */
30803   if (!template_id_p
30804       && parser->num_template_parameter_lists == num_templates + 1)
30805     return true;
30806 
30807   if (cp_parser_simulate_error (parser))
30808     return false;
30809 
30810   /* If there are more template classes than parameter lists, we have
30811      something like:
30812 
30813        template <class T> void S<T>::R<T>::f ();  */
30814   if (parser->num_template_parameter_lists < num_templates)
30815     {
30816       if (declarator && !current_function_decl)
30817 	error_at (location, "specializing member %<%T::%E%> "
30818 		  "requires %<template<>%> syntax",
30819 		  declarator->u.id.qualifying_scope,
30820 		  declarator->u.id.unqualified_name);
30821       else if (declarator)
30822 	error_at (location, "invalid declaration of %<%T::%E%>",
30823 		  declarator->u.id.qualifying_scope,
30824 		  declarator->u.id.unqualified_name);
30825       else
30826 	error_at (location, "too few template-parameter-lists");
30827       return false;
30828     }
30829   /* Otherwise, there are too many template parameter lists.  We have
30830      something like:
30831 
30832      template <class T> template <class U> void S::f();  */
30833   error_at (location, "too many template-parameter-lists");
30834   return false;
30835 }
30836 
30837 /* Parse an optional `::' token indicating that the following name is
30838    from the global namespace.  If so, PARSER->SCOPE is set to the
30839    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
30840    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
30841    Returns the new value of PARSER->SCOPE, if the `::' token is
30842    present, and NULL_TREE otherwise.  */
30843 
30844 static tree
cp_parser_global_scope_opt(cp_parser * parser,bool current_scope_valid_p)30845 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
30846 {
30847   cp_token *token;
30848 
30849   /* Peek at the next token.  */
30850   token = cp_lexer_peek_token (parser->lexer);
30851   /* If we're looking at a `::' token then we're starting from the
30852      global namespace, not our current location.  */
30853   if (token->type == CPP_SCOPE)
30854     {
30855       /* Consume the `::' token.  */
30856       cp_lexer_consume_token (parser->lexer);
30857       /* Set the SCOPE so that we know where to start the lookup.  */
30858       parser->scope = global_namespace;
30859       parser->qualifying_scope = global_namespace;
30860       parser->object_scope = NULL_TREE;
30861 
30862       return parser->scope;
30863     }
30864   else if (!current_scope_valid_p)
30865     {
30866       parser->scope = NULL_TREE;
30867       parser->qualifying_scope = NULL_TREE;
30868       parser->object_scope = NULL_TREE;
30869     }
30870 
30871   return NULL_TREE;
30872 }
30873 
30874 /* Returns TRUE if the upcoming token sequence is the start of a
30875    constructor declarator or C++17 deduction guide.  If FRIEND_P is true, the
30876    declarator is preceded by the `friend' specifier.  The parser flags FLAGS
30877    is used to control type-specifier parsing.  */
30878 
30879 static bool
cp_parser_constructor_declarator_p(cp_parser * parser,cp_parser_flags flags,bool friend_p)30880 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
30881 				    bool friend_p)
30882 {
30883   bool constructor_p;
30884   bool outside_class_specifier_p;
30885   tree nested_name_specifier;
30886   cp_token *next_token;
30887 
30888   /* The common case is that this is not a constructor declarator, so
30889      try to avoid doing lots of work if at all possible.  It's not
30890      valid declare a constructor at function scope.  */
30891   if (parser->in_function_body)
30892     return false;
30893   /* And only certain tokens can begin a constructor declarator.  */
30894   next_token = cp_lexer_peek_token (parser->lexer);
30895   if (next_token->type != CPP_NAME
30896       && next_token->type != CPP_SCOPE
30897       && next_token->type != CPP_NESTED_NAME_SPECIFIER
30898       /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
30899 	 declarator-id of a constructor or destructor.  */
30900       && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
30901     return false;
30902 
30903   /* Parse tentatively; we are going to roll back all of the tokens
30904      consumed here.  */
30905   cp_parser_parse_tentatively (parser);
30906   /* Assume that we are looking at a constructor declarator.  */
30907   constructor_p = true;
30908 
30909   /* Look for the optional `::' operator.  */
30910   cp_parser_global_scope_opt (parser,
30911 			      /*current_scope_valid_p=*/false);
30912   /* Look for the nested-name-specifier.  */
30913   nested_name_specifier
30914     = (cp_parser_nested_name_specifier_opt (parser,
30915 					    /*typename_keyword_p=*/false,
30916 					    /*check_dependency_p=*/false,
30917 					    /*type_p=*/false,
30918 					    /*is_declaration=*/false));
30919 
30920   /* Resolve the TYPENAME_TYPE, because the call above didn't do it.  */
30921   if (nested_name_specifier
30922       && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
30923     {
30924       tree s = resolve_typename_type (nested_name_specifier,
30925 				      /*only_current_p=*/false);
30926       if (TREE_CODE (s) != TYPENAME_TYPE)
30927 	nested_name_specifier = s;
30928     }
30929 
30930   outside_class_specifier_p = (!at_class_scope_p ()
30931 			       || !TYPE_BEING_DEFINED (current_class_type)
30932 			       || friend_p);
30933 
30934   /* Outside of a class-specifier, there must be a
30935      nested-name-specifier.  Except in C++17 mode, where we
30936      might be declaring a guiding declaration.  */
30937   if (!nested_name_specifier && outside_class_specifier_p
30938       && cxx_dialect < cxx17)
30939     constructor_p = false;
30940   else if (nested_name_specifier == error_mark_node)
30941     constructor_p = false;
30942 
30943   /* If we have a class scope, this is easy; DR 147 says that S::S always
30944      names the constructor, and no other qualified name could.  */
30945   if (constructor_p && nested_name_specifier
30946       && CLASS_TYPE_P (nested_name_specifier))
30947     {
30948       tree id = cp_parser_unqualified_id (parser,
30949 					  /*template_keyword_p=*/false,
30950 					  /*check_dependency_p=*/false,
30951 					  /*declarator_p=*/true,
30952 					  /*optional_p=*/false);
30953       if (is_overloaded_fn (id))
30954 	id = DECL_NAME (get_first_fn (id));
30955       if (!constructor_name_p (id, nested_name_specifier))
30956 	constructor_p = false;
30957     }
30958   /* If we still think that this might be a constructor-declarator,
30959      look for a class-name.  */
30960   else if (constructor_p)
30961     {
30962       /* If we have:
30963 
30964 	   template <typename T> struct S {
30965 	     S();
30966 	   };
30967 
30968 	 we must recognize that the nested `S' names a class.  */
30969       if (cxx_dialect >= cxx17)
30970 	cp_parser_parse_tentatively (parser);
30971 
30972       tree type_decl;
30973       type_decl = cp_parser_class_name (parser,
30974 					/*typename_keyword_p=*/false,
30975 					/*template_keyword_p=*/false,
30976 					none_type,
30977 					/*check_dependency_p=*/false,
30978 					/*class_head_p=*/false,
30979 					/*is_declaration=*/false);
30980 
30981       if (cxx_dialect >= cxx17
30982 	  && !cp_parser_parse_definitely (parser))
30983 	{
30984 	  type_decl = NULL_TREE;
30985 	  tree tmpl = cp_parser_template_name (parser,
30986 					       /*template_keyword*/false,
30987 					       /*check_dependency_p*/false,
30988 					       /*is_declaration*/false,
30989 					       none_type,
30990 					       /*is_identifier*/NULL);
30991 	  if (DECL_CLASS_TEMPLATE_P (tmpl)
30992 	      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
30993 	    /* It's a deduction guide, return true.  */;
30994 	  else
30995 	    cp_parser_simulate_error (parser);
30996 	}
30997 
30998       /* If there was no class-name, then this is not a constructor.
30999 	 Otherwise, if we are in a class-specifier and we aren't
31000 	 handling a friend declaration, check that its type matches
31001 	 current_class_type (c++/38313).  Note: error_mark_node
31002 	 is left alone for error recovery purposes.  */
31003       constructor_p = (!cp_parser_error_occurred (parser)
31004 		       && (outside_class_specifier_p
31005 			   || type_decl == NULL_TREE
31006 			   || type_decl == error_mark_node
31007 			   || same_type_p (current_class_type,
31008 					   TREE_TYPE (type_decl))));
31009 
31010       /* If we're still considering a constructor, we have to see a `(',
31011 	 to begin the parameter-declaration-clause, followed by either a
31012 	 `)', an `...', or a decl-specifier.  We need to check for a
31013 	 type-specifier to avoid being fooled into thinking that:
31014 
31015 	   S (f) (int);
31016 
31017 	 is a constructor.  (It is actually a function named `f' that
31018 	 takes one parameter (of type `int') and returns a value of type
31019 	 `S'.  */
31020       if (constructor_p
31021 	  && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31022 	constructor_p = false;
31023 
31024       if (constructor_p
31025 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31026 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
31027 	  /* A parameter declaration begins with a decl-specifier,
31028 	     which is either the "attribute" keyword, a storage class
31029 	     specifier, or (usually) a type-specifier.  */
31030 	  && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
31031 	  /* GNU attributes can actually appear both at the start of
31032 	     a parameter and parenthesized declarator.
31033 	     S (__attribute__((unused)) int);
31034 	     is a constructor, but
31035 	     S (__attribute__((unused)) foo) (int);
31036 	     is a function declaration. [[attribute]] can appear in the
31037 	     first form too, but not in the second form.  */
31038 	  && !cp_next_tokens_can_be_std_attribute_p (parser))
31039 	{
31040 	  tree type;
31041 	  tree pushed_scope = NULL_TREE;
31042 	  unsigned saved_num_template_parameter_lists;
31043 
31044 	  if (cp_parser_allow_gnu_extensions_p (parser)
31045 	      && cp_next_tokens_can_be_gnu_attribute_p (parser))
31046 	    {
31047 	      unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
31048 	      while (--n)
31049 		cp_lexer_consume_token (parser->lexer);
31050 	    }
31051 
31052 	  /* Names appearing in the type-specifier should be looked up
31053 	     in the scope of the class.  */
31054 	  if (current_class_type)
31055 	    type = NULL_TREE;
31056 	  else if (type_decl)
31057 	    {
31058 	      type = TREE_TYPE (type_decl);
31059 	      if (TREE_CODE (type) == TYPENAME_TYPE)
31060 		{
31061 		  type = resolve_typename_type (type,
31062 						/*only_current_p=*/false);
31063 		  if (TREE_CODE (type) == TYPENAME_TYPE)
31064 		    {
31065 		      cp_parser_abort_tentative_parse (parser);
31066 		      return false;
31067 		    }
31068 		}
31069 	      pushed_scope = push_scope (type);
31070 	    }
31071 
31072 	  /* Inside the constructor parameter list, surrounding
31073 	     template-parameter-lists do not apply.  */
31074 	  saved_num_template_parameter_lists
31075 	    = parser->num_template_parameter_lists;
31076 	  parser->num_template_parameter_lists = 0;
31077 
31078 	  /* Look for the type-specifier.  It's not optional, but its typename
31079 	     might be.  Unless this is a friend declaration; we don't want to
31080 	     treat
31081 
31082 	       friend S (T::fn)(int);
31083 
31084 	     as a constructor, but with P0634, we might assume a type when
31085 	     looking for the type-specifier.  It is actually a function named
31086 	     `T::fn' that takes one parameter (of type `int') and returns a
31087 	     value of type `S'.  Constructors can be friends, but they must
31088 	     use a qualified name.
31089 
31090 	     Parse with an empty set of declaration specifiers since we're
31091 	     trying to match a decl-specifier-seq of the first parameter.
31092 	     This must be non-null so that cp_parser_simple_type_specifier
31093 	     will recognize a constrained placeholder type such as:
31094 	     'C<int> auto' where C is a type concept.  */
31095 	  cp_decl_specifier_seq ctor_specs;
31096 	  clear_decl_specs (&ctor_specs);
31097 	  cp_parser_type_specifier (parser,
31098 				    (friend_p ? CP_PARSER_FLAGS_NONE
31099 				     : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31100 				    /*decl_specs=*/&ctor_specs,
31101 				    /*is_declarator=*/true,
31102 				    /*declares_class_or_enum=*/NULL,
31103 				    /*is_cv_qualifier=*/NULL);
31104 
31105 	  parser->num_template_parameter_lists
31106 	    = saved_num_template_parameter_lists;
31107 
31108 	  /* Leave the scope of the class.  */
31109 	  if (pushed_scope)
31110 	    pop_scope (pushed_scope);
31111 
31112 	  constructor_p = !cp_parser_error_occurred (parser);
31113 	}
31114     }
31115 
31116   /* We did not really want to consume any tokens.  */
31117   cp_parser_abort_tentative_parse (parser);
31118 
31119   return constructor_p;
31120 }
31121 
31122 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31123    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
31124    they must be performed once we are in the scope of the function.
31125 
31126    Returns the function defined.  */
31127 
31128 static tree
cp_parser_function_definition_from_specifiers_and_declarator(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,tree attributes,const cp_declarator * declarator)31129 cp_parser_function_definition_from_specifiers_and_declarator
31130   (cp_parser* parser,
31131    cp_decl_specifier_seq *decl_specifiers,
31132    tree attributes,
31133    const cp_declarator *declarator)
31134 {
31135   tree fn;
31136   bool success_p;
31137 
31138   /* Begin the function-definition.  */
31139   success_p = start_function (decl_specifiers, declarator, attributes);
31140 
31141   /* The things we're about to see are not directly qualified by any
31142      template headers we've seen thus far.  */
31143   reset_specialization ();
31144 
31145   /* If there were names looked up in the decl-specifier-seq that we
31146      did not check, check them now.  We must wait until we are in the
31147      scope of the function to perform the checks, since the function
31148      might be a friend.  */
31149   perform_deferred_access_checks (tf_warning_or_error);
31150 
31151   if (success_p)
31152     {
31153       cp_finalize_omp_declare_simd (parser, current_function_decl);
31154       parser->omp_declare_simd = NULL;
31155       cp_finalize_oacc_routine (parser, current_function_decl, true);
31156       parser->oacc_routine = NULL;
31157     }
31158 
31159   if (!success_p)
31160     {
31161       /* Skip the entire function.  */
31162       cp_parser_skip_to_end_of_block_or_statement (parser);
31163       fn = error_mark_node;
31164     }
31165   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31166     {
31167       /* Seen already, skip it.  An error message has already been output.  */
31168       cp_parser_skip_to_end_of_block_or_statement (parser);
31169       fn = current_function_decl;
31170       current_function_decl = NULL_TREE;
31171       /* If this is a function from a class, pop the nested class.  */
31172       if (current_class_name)
31173 	pop_nested_class ();
31174     }
31175   else
31176     {
31177       timevar_id_t tv;
31178       if (DECL_DECLARED_INLINE_P (current_function_decl))
31179         tv = TV_PARSE_INLINE;
31180       else
31181         tv = TV_PARSE_FUNC;
31182       timevar_push (tv);
31183       fn = cp_parser_function_definition_after_declarator (parser,
31184 							 /*inline_p=*/false);
31185       timevar_pop (tv);
31186     }
31187 
31188   return fn;
31189 }
31190 
31191 /* Parse the part of a function-definition that follows the
31192    declarator.  INLINE_P is TRUE iff this function is an inline
31193    function defined within a class-specifier.
31194 
31195    Returns the function defined.  */
31196 
31197 static tree
cp_parser_function_definition_after_declarator(cp_parser * parser,bool inline_p)31198 cp_parser_function_definition_after_declarator (cp_parser* parser,
31199 						bool inline_p)
31200 {
31201   tree fn;
31202   bool saved_in_unbraced_linkage_specification_p;
31203   bool saved_in_function_body;
31204   unsigned saved_num_template_parameter_lists;
31205   cp_token *token;
31206   bool fully_implicit_function_template_p
31207     = parser->fully_implicit_function_template_p;
31208   parser->fully_implicit_function_template_p = false;
31209   tree implicit_template_parms
31210     = parser->implicit_template_parms;
31211   parser->implicit_template_parms = 0;
31212   cp_binding_level* implicit_template_scope
31213     = parser->implicit_template_scope;
31214   parser->implicit_template_scope = 0;
31215 
31216   saved_in_function_body = parser->in_function_body;
31217   parser->in_function_body = true;
31218   /* If the next token is `return', then the code may be trying to
31219      make use of the "named return value" extension that G++ used to
31220      support.  */
31221   token = cp_lexer_peek_token (parser->lexer);
31222   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
31223     {
31224       /* Consume the `return' keyword.  */
31225       cp_lexer_consume_token (parser->lexer);
31226       /* Look for the identifier that indicates what value is to be
31227 	 returned.  */
31228       cp_parser_identifier (parser);
31229       /* Issue an error message.  */
31230       error_at (token->location,
31231 		"named return values are no longer supported");
31232       /* Skip tokens until we reach the start of the function body.  */
31233       while (true)
31234 	{
31235 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
31236 	  if (token->type == CPP_OPEN_BRACE
31237 	      || token->type == CPP_EOF
31238 	      || token->type == CPP_PRAGMA_EOL)
31239 	    break;
31240 	  cp_lexer_consume_token (parser->lexer);
31241 	}
31242     }
31243   /* The `extern' in `extern "C" void f () { ... }' does not apply to
31244      anything declared inside `f'.  */
31245   saved_in_unbraced_linkage_specification_p
31246     = parser->in_unbraced_linkage_specification_p;
31247   parser->in_unbraced_linkage_specification_p = false;
31248   /* Inside the function, surrounding template-parameter-lists do not
31249      apply.  */
31250   saved_num_template_parameter_lists
31251     = parser->num_template_parameter_lists;
31252   parser->num_template_parameter_lists = 0;
31253 
31254   /* If the next token is `try', `__transaction_atomic', or
31255      `__transaction_relaxed`, then we are looking at either function-try-block
31256      or function-transaction-block.  Note that all of these include the
31257      function-body.  */
31258   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
31259     cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
31260   else if (cp_lexer_next_token_is_keyword (parser->lexer,
31261       RID_TRANSACTION_RELAXED))
31262     cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
31263   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31264     cp_parser_function_try_block (parser);
31265   else
31266     cp_parser_ctor_initializer_opt_and_function_body
31267       (parser, /*in_function_try_block=*/false);
31268 
31269   /* Finish the function.  */
31270   fn = finish_function (inline_p);
31271 
31272   if (modules_p ()
31273       && !inline_p
31274       && TYPE_P (DECL_CONTEXT (fn))
31275       && (DECL_DECLARED_INLINE_P (fn)
31276 	  || processing_template_decl))
31277     set_defining_module (fn);
31278 
31279   /* Generate code for it, if necessary.  */
31280   expand_or_defer_fn (fn);
31281   /* Restore the saved values.  */
31282   parser->in_unbraced_linkage_specification_p
31283     = saved_in_unbraced_linkage_specification_p;
31284   parser->num_template_parameter_lists
31285     = saved_num_template_parameter_lists;
31286   parser->in_function_body = saved_in_function_body;
31287 
31288   parser->fully_implicit_function_template_p
31289     = fully_implicit_function_template_p;
31290   parser->implicit_template_parms
31291     = implicit_template_parms;
31292   parser->implicit_template_scope
31293     = implicit_template_scope;
31294 
31295   if (parser->fully_implicit_function_template_p)
31296     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
31297 
31298   return fn;
31299 }
31300 
31301 /* Parse a template-declaration body (following argument list).  */
31302 
31303 static void
cp_parser_template_declaration_after_parameters(cp_parser * parser,tree parameter_list,bool member_p)31304 cp_parser_template_declaration_after_parameters (cp_parser* parser,
31305 						 tree parameter_list,
31306 						 bool member_p)
31307 {
31308   tree decl = NULL_TREE;
31309   bool friend_p = false;
31310 
31311   /* We just processed one more parameter list.  */
31312   ++parser->num_template_parameter_lists;
31313 
31314   /* Get the deferred access checks from the parameter list.  These
31315      will be checked once we know what is being declared, as for a
31316      member template the checks must be performed in the scope of the
31317      class containing the member.  */
31318   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
31319 
31320   /* Tentatively parse for a new template parameter list, which can either be
31321      the template keyword or a template introduction.  */
31322   if (cp_parser_template_declaration_after_export (parser, member_p))
31323     /* OK */;
31324   else if (cxx_dialect >= cxx11
31325 	   && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
31326     decl = cp_parser_alias_declaration (parser);
31327   else if (cxx_dialect >= cxx20 /* Implies flag_concept.  */
31328            && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
31329            && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
31330     /* Allow 'concept bool' to be handled as per the TS.  */
31331     decl = cp_parser_concept_definition (parser);
31332   else
31333     {
31334       cp_token *token = cp_lexer_peek_token (parser->lexer);
31335       decl = cp_parser_single_declaration (parser,
31336 					   checks,
31337 					   member_p,
31338                                            /*explicit_specialization_p=*/false,
31339 					   &friend_p);
31340 
31341       /* If this is a member template declaration, let the front
31342 	 end know.  */
31343       if (member_p && !friend_p && decl)
31344 	{
31345 	  if (TREE_CODE (decl) == TYPE_DECL)
31346 	    cp_parser_check_access_in_redeclaration (decl, token->location);
31347 
31348 	  decl = finish_member_template_decl (decl);
31349 	}
31350       else if (friend_p && decl
31351 	       && DECL_DECLARES_TYPE_P (decl))
31352 	make_friend_class (current_class_type, TREE_TYPE (decl),
31353 			   /*complain=*/true);
31354     }
31355   /* We are done with the current parameter list.  */
31356   --parser->num_template_parameter_lists;
31357 
31358   pop_deferring_access_checks ();
31359 
31360   /* Finish up.  */
31361   finish_template_decl (parameter_list);
31362 
31363   /* Check the template arguments for a literal operator template.  */
31364   if (decl
31365       && DECL_DECLARES_FUNCTION_P (decl)
31366       && UDLIT_OPER_P (DECL_NAME (decl)))
31367     {
31368       bool ok = true;
31369       if (parameter_list == NULL_TREE)
31370 	ok = false;
31371       else
31372 	{
31373 	  int num_parms = TREE_VEC_LENGTH (parameter_list);
31374 	  if (num_parms == 1)
31375 	    {
31376 	      tree parm_list = TREE_VEC_ELT (parameter_list, 0);
31377 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31378 	      if (TREE_CODE (parm) != PARM_DECL)
31379 		ok = false;
31380 	      else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
31381 		       && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31382 		/* OK, C++20 string literal operator template.  We don't need
31383 		   to warn in lower dialects here because we will have already
31384 		   warned about the template parameter.  */;
31385 	      else if (TREE_TYPE (parm) != char_type_node
31386 		       || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31387 		ok = false;
31388 	    }
31389 	  else if (num_parms == 2 && cxx_dialect >= cxx14)
31390 	    {
31391 	      tree parm_type = TREE_VEC_ELT (parameter_list, 0);
31392 	      tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
31393 	      tree parm_list = TREE_VEC_ELT (parameter_list, 1);
31394 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31395 	      if (TREE_CODE (parm) != PARM_DECL
31396 		  || TREE_TYPE (parm) != TREE_TYPE (type)
31397 		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31398 		ok = false;
31399 	      else
31400 		/* http://cplusplus.github.io/EWG/ewg-active.html#66  */
31401 		pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
31402 			 "ISO C++ did not adopt string literal operator templa"
31403 			 "tes taking an argument pack of characters");
31404 	    }
31405 	  else
31406 	    ok = false;
31407 	}
31408       if (!ok)
31409 	{
31410 	  if (cxx_dialect > cxx17)
31411 	    error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31412 		      "template %qD has invalid parameter list; expected "
31413 		      "non-type template parameter pack %<<char...>%> or "
31414 		      "single non-type parameter of class type",
31415 		      decl);
31416 	  else
31417 	    error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31418 		      "template %qD has invalid parameter list; expected "
31419 		      "non-type template parameter pack %<<char...>%>",
31420 		      decl);
31421 	}
31422     }
31423 
31424   /* Register member declarations.  */
31425   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
31426     finish_member_declaration (decl);
31427   /* If DECL is a function template, we must return to parse it later.
31428      (Even though there is no definition, there might be default
31429      arguments that need handling.)  */
31430   if (member_p && decl
31431       && DECL_DECLARES_FUNCTION_P (decl))
31432     vec_safe_push (unparsed_funs_with_definitions, decl);
31433 }
31434 
31435 /* Parse a template introduction header for a template-declaration.  Returns
31436    false if tentative parse fails.  */
31437 
31438 static bool
cp_parser_template_introduction(cp_parser * parser,bool member_p)31439 cp_parser_template_introduction (cp_parser* parser, bool member_p)
31440 {
31441   cp_parser_parse_tentatively (parser);
31442 
31443   tree saved_scope = parser->scope;
31444   tree saved_object_scope = parser->object_scope;
31445   tree saved_qualifying_scope = parser->qualifying_scope;
31446   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31447 
31448   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
31449 
31450   /* In classes don't parse valid unnamed bitfields as invalid
31451      template introductions.  */
31452   if (member_p)
31453     parser->colon_corrects_to_scope_p = false;
31454 
31455   /* Look for the optional `::' operator.  */
31456   cp_parser_global_scope_opt (parser,
31457 			      /*current_scope_valid_p=*/false);
31458   /* Look for the nested-name-specifier.  */
31459   cp_parser_nested_name_specifier_opt (parser,
31460 				       /*typename_keyword_p=*/false,
31461 				       /*check_dependency_p=*/true,
31462 				       /*type_p=*/false,
31463 				       /*is_declaration=*/false);
31464 
31465   cp_token *token = cp_lexer_peek_token (parser->lexer);
31466   tree concept_name = cp_parser_identifier (parser);
31467 
31468   /* Look up the concept for which we will be matching
31469      template parameters.  */
31470   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
31471 						 token->location);
31472   parser->scope = saved_scope;
31473   parser->object_scope = saved_object_scope;
31474   parser->qualifying_scope = saved_qualifying_scope;
31475   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31476 
31477   if (concept_name == error_mark_node
31478       || (seen_error () && !concept_definition_p (tmpl_decl)))
31479     cp_parser_simulate_error (parser);
31480 
31481   /* Look for opening brace for introduction.  */
31482   matching_braces braces;
31483   braces.require_open (parser);
31484   location_t open_loc = input_location;
31485 
31486   if (!cp_parser_parse_definitely (parser))
31487     return false;
31488 
31489   push_deferring_access_checks (dk_deferred);
31490 
31491   /* Build vector of placeholder parameters and grab
31492      matching identifiers.  */
31493   tree introduction_list = cp_parser_introduction_list (parser);
31494 
31495   /* Look for closing brace for introduction.  */
31496   if (!braces.require_close (parser))
31497     return true;
31498 
31499   /* The introduction-list shall not be empty.  */
31500   int nargs = TREE_VEC_LENGTH (introduction_list);
31501   if (nargs == 0)
31502     {
31503       /* In cp_parser_introduction_list we have already issued an error.  */
31504       return true;
31505     }
31506 
31507   if (tmpl_decl == error_mark_node)
31508     {
31509       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
31510 				   token->location);
31511       return true;
31512     }
31513 
31514   /* Build and associate the constraint.  */
31515   location_t introduction_loc = make_location (open_loc,
31516 					       start_token->location,
31517 					       parser->lexer);
31518   tree parms = finish_template_introduction (tmpl_decl,
31519 					     introduction_list,
31520 					     introduction_loc);
31521   if (parms && parms != error_mark_node)
31522     {
31523       if (!flag_concepts_ts)
31524 	pedwarn (introduction_loc, 0, "template-introductions"
31525 		 " are not part of C++20 concepts; use %qs to enable",
31526 		 "-fconcepts-ts");
31527 
31528       cp_parser_template_declaration_after_parameters (parser, parms,
31529 						       member_p);
31530       return true;
31531     }
31532 
31533   if (parms == NULL_TREE)
31534     error_at (token->location, "no matching concept for template-introduction");
31535 
31536   return true;
31537 }
31538 
31539 /* Parse a normal template-declaration following the template keyword.  */
31540 
31541 static void
cp_parser_explicit_template_declaration(cp_parser * parser,bool member_p)31542 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
31543 {
31544   tree parameter_list;
31545   bool need_lang_pop;
31546   location_t location = input_location;
31547 
31548   /* Look for the `<' token.  */
31549   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
31550     return;
31551   if (at_class_scope_p () && current_function_decl)
31552     {
31553       /* 14.5.2.2 [temp.mem]
31554 
31555          A local class shall not have member templates.  */
31556       error_at (location,
31557                 "invalid declaration of member template in local class");
31558       cp_parser_skip_to_end_of_block_or_statement (parser);
31559       return;
31560     }
31561   /* [temp]
31562 
31563      A template ... shall not have C linkage.  */
31564   if (current_lang_name == lang_name_c)
31565     {
31566       error_at (location, "template with C linkage");
31567       maybe_show_extern_c_location ();
31568       /* Give it C++ linkage to avoid confusing other parts of the
31569          front end.  */
31570       push_lang_context (lang_name_cplusplus);
31571       need_lang_pop = true;
31572     }
31573   else
31574     need_lang_pop = false;
31575 
31576   /* We cannot perform access checks on the template parameter
31577      declarations until we know what is being declared, just as we
31578      cannot check the decl-specifier list.  */
31579   push_deferring_access_checks (dk_deferred);
31580 
31581   /* If the next token is `>', then we have an invalid
31582      specialization.  Rather than complain about an invalid template
31583      parameter, issue an error message here.  */
31584   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
31585     {
31586       cp_parser_error (parser, "invalid explicit specialization");
31587       begin_specialization ();
31588       parameter_list = NULL_TREE;
31589     }
31590   else
31591     {
31592       /* Parse the template parameters.  */
31593       parameter_list = cp_parser_template_parameter_list (parser);
31594     }
31595 
31596   /* Look for the `>'.  */
31597   cp_parser_require_end_of_template_parameter_list (parser);
31598 
31599   /* Manage template requirements */
31600   if (flag_concepts)
31601   {
31602     tree reqs = get_shorthand_constraints (current_template_parms);
31603     if (tree treqs = cp_parser_requires_clause_opt (parser, false))
31604       reqs = combine_constraint_expressions (reqs, treqs);
31605     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
31606   }
31607 
31608   cp_parser_template_declaration_after_parameters (parser, parameter_list,
31609 						   member_p);
31610 
31611   /* For the erroneous case of a template with C linkage, we pushed an
31612      implicit C++ linkage scope; exit that scope now.  */
31613   if (need_lang_pop)
31614     pop_lang_context ();
31615 }
31616 
31617 /* Parse a template-declaration, assuming that the `export' (and
31618    `extern') keywords, if present, has already been scanned.  MEMBER_P
31619    is as for cp_parser_template_declaration.  */
31620 
31621 static bool
cp_parser_template_declaration_after_export(cp_parser * parser,bool member_p)31622 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
31623 {
31624   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31625     {
31626       cp_lexer_consume_token (parser->lexer);
31627       cp_parser_explicit_template_declaration (parser, member_p);
31628       return true;
31629     }
31630   else if (flag_concepts)
31631     return cp_parser_template_introduction (parser, member_p);
31632 
31633   return false;
31634 }
31635 
31636 /* Perform the deferred access checks from a template-parameter-list.
31637    CHECKS is a TREE_LIST of access checks, as returned by
31638    get_deferred_access_checks.  */
31639 
31640 static void
cp_parser_perform_template_parameter_access_checks(vec<deferred_access_check,va_gc> * checks)31641 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
31642 {
31643   ++processing_template_parmlist;
31644   perform_access_checks (checks, tf_warning_or_error);
31645   --processing_template_parmlist;
31646 }
31647 
31648 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31649    `function-definition' sequence that follows a template header.
31650    If MEMBER_P is true, this declaration appears in a class scope.
31651 
31652    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
31653    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
31654 
31655 static tree
cp_parser_single_declaration(cp_parser * parser,vec<deferred_access_check,va_gc> * checks,bool member_p,bool explicit_specialization_p,bool * friend_p)31656 cp_parser_single_declaration (cp_parser* parser,
31657 			      vec<deferred_access_check, va_gc> *checks,
31658 			      bool member_p,
31659                               bool explicit_specialization_p,
31660 			      bool* friend_p)
31661 {
31662   int declares_class_or_enum;
31663   tree decl = NULL_TREE;
31664   cp_decl_specifier_seq decl_specifiers;
31665   bool function_definition_p = false;
31666   cp_token *decl_spec_token_start;
31667 
31668   /* This function is only used when processing a template
31669      declaration.  */
31670   gcc_assert (innermost_scope_kind () == sk_template_parms
31671 	      || innermost_scope_kind () == sk_template_spec);
31672 
31673   /* Defer access checks until we know what is being declared.  */
31674   push_deferring_access_checks (dk_deferred);
31675 
31676   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31677      alternative.  */
31678   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
31679   cp_parser_decl_specifier_seq (parser,
31680 				(CP_PARSER_FLAGS_OPTIONAL
31681 				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
31682 				&decl_specifiers,
31683 				&declares_class_or_enum);
31684 
31685   cp_omp_declare_simd_data odsd;
31686   if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
31687     cp_parser_handle_directive_omp_attributes (parser,
31688 					       &decl_specifiers.attributes,
31689 					       &odsd, true);
31690 
31691   if (friend_p)
31692     *friend_p = cp_parser_friend_p (&decl_specifiers);
31693 
31694   /* There are no template typedefs.  */
31695   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
31696     {
31697       error_at (decl_spec_token_start->location,
31698 		"template declaration of %<typedef%>");
31699       decl = error_mark_node;
31700     }
31701 
31702   /* Gather up the access checks that occurred the
31703      decl-specifier-seq.  */
31704   stop_deferring_access_checks ();
31705 
31706   /* Check for the declaration of a template class.  */
31707   if (declares_class_or_enum)
31708     {
31709       if (cp_parser_declares_only_class_p (parser)
31710 	  || (declares_class_or_enum & 2))
31711 	{
31712 	  decl = shadow_tag (&decl_specifiers);
31713 
31714 	  /* In this case:
31715 
31716 	       struct C {
31717 		 friend template <typename T> struct A<T>::B;
31718 	       };
31719 
31720 	     A<T>::B will be represented by a TYPENAME_TYPE, and
31721 	     therefore not recognized by shadow_tag.  */
31722 	  if (friend_p && *friend_p
31723 	      && !decl
31724 	      && decl_specifiers.type
31725 	      && TYPE_P (decl_specifiers.type))
31726 	    decl = decl_specifiers.type;
31727 
31728 	  if (decl && decl != error_mark_node)
31729 	    decl = TYPE_NAME (decl);
31730 	  else
31731 	    decl = error_mark_node;
31732 
31733 	  /* If this is a declaration, but not a definition, associate
31734 	     any constraints with the type declaration. Constraints
31735 	     are associated with definitions in cp_parser_class_specifier.  */
31736 	  if (declares_class_or_enum == 1)
31737 	    associate_classtype_constraints (TREE_TYPE (decl));
31738 
31739 	  /* Perform access checks for template parameters.  */
31740 	  cp_parser_perform_template_parameter_access_checks (checks);
31741 
31742 	  /* Give a helpful diagnostic for
31743 	       template <class T> struct A { } a;
31744 	     if we aren't already recovering from an error.  */
31745 	  if (!cp_parser_declares_only_class_p (parser)
31746 	      && !seen_error ())
31747 	    {
31748 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
31749 			"a class template declaration must not declare "
31750 			"anything else");
31751 	      cp_parser_skip_to_end_of_block_or_statement (parser);
31752 	      goto out;
31753 	    }
31754 	}
31755     }
31756 
31757   /* Complain about missing 'typename' or other invalid type names.  */
31758   if (!decl_specifiers.any_type_specifiers_p
31759       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
31760     {
31761       /* cp_parser_parse_and_diagnose_invalid_type_name calls
31762 	 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31763 	 the rest of this declaration.  */
31764       decl = error_mark_node;
31765       goto out;
31766     }
31767 
31768   /* If it's not a template class, try for a template function.  If
31769      the next token is a `;', then this declaration does not declare
31770      anything.  But, if there were errors in the decl-specifiers, then
31771      the error might well have come from an attempted class-specifier.
31772      In that case, there's no need to warn about a missing declarator.  */
31773   if (!decl
31774       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
31775 	  || decl_specifiers.type != error_mark_node))
31776     {
31777       int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
31778       /* We don't delay parsing for friends, though CWG 2510 may change
31779 	 that.  */
31780       if (member_p && !(friend_p && *friend_p))
31781 	flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
31782       decl = cp_parser_init_declarator (parser,
31783 					flags,
31784 				        &decl_specifiers,
31785 				        checks,
31786 				        /*function_definition_allowed_p=*/true,
31787 				        member_p,
31788 				        declares_class_or_enum,
31789 				        &function_definition_p,
31790 					NULL, NULL, NULL);
31791 
31792     /* 7.1.1-1 [dcl.stc]
31793 
31794        A storage-class-specifier shall not be specified in an explicit
31795        specialization...  */
31796     if (decl
31797         && explicit_specialization_p
31798         && decl_specifiers.storage_class != sc_none)
31799       {
31800         error_at (decl_spec_token_start->location,
31801 		  "explicit template specialization cannot have a storage class");
31802         decl = error_mark_node;
31803       }
31804 
31805     if (decl && VAR_P (decl))
31806       check_template_variable (decl);
31807     }
31808 
31809   /* Look for a trailing `;' after the declaration.  */
31810   if (!function_definition_p
31811       && (decl == error_mark_node
31812 	  || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
31813     cp_parser_skip_to_end_of_block_or_statement (parser);
31814 
31815  out:
31816   pop_deferring_access_checks ();
31817 
31818   /* Clear any current qualification; whatever comes next is the start
31819      of something new.  */
31820   parser->scope = NULL_TREE;
31821   parser->qualifying_scope = NULL_TREE;
31822   parser->object_scope = NULL_TREE;
31823 
31824   cp_finalize_omp_declare_simd (parser, &odsd);
31825 
31826   return decl;
31827 }
31828 
31829 /* Parse a cast-expression that is not the operand of a unary "&".  */
31830 
31831 static cp_expr
cp_parser_simple_cast_expression(cp_parser * parser)31832 cp_parser_simple_cast_expression (cp_parser *parser)
31833 {
31834   return cp_parser_cast_expression (parser, /*address_p=*/false,
31835 				    /*cast_p=*/false, /*decltype*/false, NULL);
31836 }
31837 
31838 /* Parse a functional cast to TYPE.  Returns an expression
31839    representing the cast.  */
31840 
31841 static cp_expr
cp_parser_functional_cast(cp_parser * parser,tree type)31842 cp_parser_functional_cast (cp_parser* parser, tree type)
31843 {
31844   vec<tree, va_gc> *vec;
31845   tree expression_list;
31846   cp_expr cast;
31847   bool nonconst_p;
31848 
31849   location_t start_loc = input_location;
31850 
31851   if (!type)
31852     type = error_mark_node;
31853 
31854   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31855     {
31856       cp_lexer_set_source_position (parser->lexer);
31857       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
31858       expression_list = cp_parser_braced_list (parser, &nonconst_p);
31859       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
31860       if (TREE_CODE (type) == TYPE_DECL)
31861 	type = TREE_TYPE (type);
31862 
31863       cast = finish_compound_literal (type, expression_list,
31864 				      tf_warning_or_error, fcl_functional);
31865       /* Create a location of the form:
31866 	    type_name{i, f}
31867 	    ^~~~~~~~~~~~~~~
31868 	 with caret == start at the start of the type name,
31869 	 finishing at the closing brace.  */
31870       location_t combined_loc = make_location (start_loc, start_loc,
31871 					       parser->lexer);
31872       cast.set_location (combined_loc);
31873       return cast;
31874    }
31875 
31876 
31877   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
31878 						 /*cast_p=*/true,
31879 						 /*allow_expansion_p=*/true,
31880 						 /*non_constant_p=*/NULL);
31881   if (vec == NULL)
31882     expression_list = error_mark_node;
31883   else
31884     {
31885       expression_list = build_tree_list_vec (vec);
31886       release_tree_vector (vec);
31887     }
31888 
31889   /* Create a location of the form:
31890        float(i)
31891        ^~~~~~~~
31892      with caret == start at the start of the type name,
31893      finishing at the closing paren.  */
31894   location_t combined_loc = make_location (start_loc, start_loc,
31895 					   parser->lexer);
31896   cast = build_functional_cast (combined_loc, type, expression_list,
31897                                 tf_warning_or_error);
31898 
31899   /* [expr.const]/1: In an integral constant expression "only type
31900      conversions to integral or enumeration type can be used".  */
31901   if (TREE_CODE (type) == TYPE_DECL)
31902     type = TREE_TYPE (type);
31903   if (cast != error_mark_node
31904       && !cast_valid_in_integral_constant_expression_p (type)
31905       && cp_parser_non_integral_constant_expression (parser,
31906 						     NIC_CONSTRUCTOR))
31907     return error_mark_node;
31908 
31909   return cast;
31910 }
31911 
31912 /* Save the tokens that make up the body of a member function defined
31913    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
31914    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
31915    specifiers applied to the declaration.  Returns the FUNCTION_DECL
31916    for the member function.  */
31917 
31918 static tree
cp_parser_save_member_function_body(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree attributes)31919 cp_parser_save_member_function_body (cp_parser* parser,
31920 				     cp_decl_specifier_seq *decl_specifiers,
31921 				     cp_declarator *declarator,
31922 				     tree attributes)
31923 {
31924   cp_token *first;
31925   cp_token *last;
31926   tree fn;
31927   bool function_try_block = false;
31928 
31929   /* Create the FUNCTION_DECL.  */
31930   fn = grokmethod (decl_specifiers, declarator, attributes);
31931   cp_finalize_omp_declare_simd (parser, fn);
31932   cp_finalize_oacc_routine (parser, fn, true);
31933   /* If something went badly wrong, bail out now.  */
31934   if (fn == error_mark_node)
31935     {
31936       /* If there's a function-body, skip it.  */
31937       if (cp_parser_token_starts_function_definition_p
31938 	  (cp_lexer_peek_token (parser->lexer)))
31939 	cp_parser_skip_to_end_of_block_or_statement (parser);
31940       return error_mark_node;
31941     }
31942 
31943   /* Remember it, if there are default args to post process.  */
31944   cp_parser_save_default_args (parser, fn);
31945 
31946   /* Save away the tokens that make up the body of the
31947      function.  */
31948   first = parser->lexer->next_token;
31949 
31950   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
31951     cp_lexer_consume_token (parser->lexer);
31952   else if (cp_lexer_next_token_is_keyword (parser->lexer,
31953 					   RID_TRANSACTION_ATOMIC))
31954     {
31955       cp_lexer_consume_token (parser->lexer);
31956       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
31957       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
31958 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
31959 	  && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
31960 	      || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
31961 	  && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
31962 	  && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
31963 	{
31964 	  cp_lexer_consume_token (parser->lexer);
31965 	  cp_lexer_consume_token (parser->lexer);
31966 	  cp_lexer_consume_token (parser->lexer);
31967 	  cp_lexer_consume_token (parser->lexer);
31968 	  cp_lexer_consume_token (parser->lexer);
31969 	}
31970       else
31971 	while (cp_next_tokens_can_be_gnu_attribute_p (parser)
31972 	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31973 	  {
31974 	    cp_lexer_consume_token (parser->lexer);
31975 	    if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31976 	      break;
31977 	  }
31978     }
31979 
31980   /* Handle function try blocks.  */
31981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31982     {
31983       cp_lexer_consume_token (parser->lexer);
31984       function_try_block = true;
31985     }
31986   /* We can have braced-init-list mem-initializers before the fn body.  */
31987   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31988     {
31989       cp_lexer_consume_token (parser->lexer);
31990       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
31991 	{
31992 	  /* cache_group will stop after an un-nested { } pair, too.  */
31993 	  if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
31994 	    break;
31995 
31996 	  /* variadic mem-inits have ... after the ')'.  */
31997 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31998 	    cp_lexer_consume_token (parser->lexer);
31999 	}
32000     }
32001   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32002   /* Handle function try blocks.  */
32003   if (function_try_block)
32004     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32005       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32006   last = parser->lexer->next_token;
32007 
32008   /* Save away the inline definition; we will process it when the
32009      class is complete.  */
32010   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32011   DECL_PENDING_INLINE_P (fn) = 1;
32012 
32013   /* We need to know that this was defined in the class, so that
32014      friend templates are handled correctly.  */
32015   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32016 
32017   /* Add FN to the queue of functions to be parsed later.  */
32018   vec_safe_push (unparsed_funs_with_definitions, fn);
32019 
32020   return fn;
32021 }
32022 
32023 /* Save the tokens that make up the in-class initializer for a non-static
32024    data member.  Returns a DEFERRED_PARSE.  */
32025 
32026 static tree
cp_parser_save_nsdmi(cp_parser * parser)32027 cp_parser_save_nsdmi (cp_parser* parser)
32028 {
32029   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
32030 }
32031 
32032 /* Parse a template-argument-list, as well as the trailing ">" (but
32033    not the opening "<").  See cp_parser_template_argument_list for the
32034    return value.  */
32035 
32036 static tree
cp_parser_enclosed_template_argument_list(cp_parser * parser)32037 cp_parser_enclosed_template_argument_list (cp_parser* parser)
32038 {
32039   tree arguments;
32040   tree saved_scope;
32041   tree saved_qualifying_scope;
32042   tree saved_object_scope;
32043   bool saved_greater_than_is_operator_p;
32044 
32045   /* [temp.names]
32046 
32047      When parsing a template-id, the first non-nested `>' is taken as
32048      the end of the template-argument-list rather than a greater-than
32049      operator.  */
32050   saved_greater_than_is_operator_p
32051     = parser->greater_than_is_operator_p;
32052   parser->greater_than_is_operator_p = false;
32053   /* Parsing the argument list may modify SCOPE, so we save it
32054      here.  */
32055   saved_scope = parser->scope;
32056   saved_qualifying_scope = parser->qualifying_scope;
32057   saved_object_scope = parser->object_scope;
32058   /* We need to evaluate the template arguments, even though this
32059      template-id may be nested within a "sizeof".  */
32060   cp_evaluated ev;
32061   /* Parse the template-argument-list itself.  */
32062   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
32063       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32064     {
32065       arguments = make_tree_vec (0);
32066       SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
32067     }
32068   else
32069     arguments = cp_parser_template_argument_list (parser);
32070   /* Look for the `>' that ends the template-argument-list. If we find
32071      a '>>' instead, it's probably just a typo.  */
32072   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32073     {
32074       if (cxx_dialect != cxx98)
32075         {
32076           /* In C++0x, a `>>' in a template argument list or cast
32077              expression is considered to be two separate `>'
32078              tokens. So, change the current token to a `>', but don't
32079              consume it: it will be consumed later when the outer
32080              template argument list (or cast expression) is parsed.
32081              Note that this replacement of `>' for `>>' is necessary
32082              even if we are parsing tentatively: in the tentative
32083              case, after calling
32084              cp_parser_enclosed_template_argument_list we will always
32085              throw away all of the template arguments and the first
32086              closing `>', either because the template argument list
32087              was erroneous or because we are replacing those tokens
32088              with a CPP_TEMPLATE_ID token.  The second `>' (which will
32089              not have been thrown away) is needed either to close an
32090              outer template argument list or to complete a new-style
32091              cast.  */
32092 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
32093           token->type = CPP_GREATER;
32094         }
32095       else if (!saved_greater_than_is_operator_p)
32096 	{
32097 	  /* If we're in a nested template argument list, the '>>' has
32098 	    to be a typo for '> >'. We emit the error message, but we
32099 	    continue parsing and we push a '>' as next token, so that
32100 	    the argument list will be parsed correctly.  Note that the
32101 	    global source location is still on the token before the
32102 	    '>>', so we need to say explicitly where we want it.  */
32103 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
32104 	  gcc_rich_location richloc (token->location);
32105 	  richloc.add_fixit_replace ("> >");
32106 	  error_at (&richloc, "%<>>%> should be %<> >%> "
32107 		    "within a nested template argument list");
32108 
32109 	  token->type = CPP_GREATER;
32110 	}
32111       else
32112 	{
32113 	  /* If this is not a nested template argument list, the '>>'
32114 	    is a typo for '>'. Emit an error message and continue.
32115 	    Same deal about the token location, but here we can get it
32116 	    right by consuming the '>>' before issuing the diagnostic.  */
32117 	  cp_token *token = cp_lexer_consume_token (parser->lexer);
32118 	  error_at (token->location,
32119 		    "spurious %<>>%>, use %<>%> to terminate "
32120 		    "a template argument list");
32121 	}
32122     }
32123   else
32124     cp_parser_require_end_of_template_parameter_list (parser);
32125   /* The `>' token might be a greater-than operator again now.  */
32126   parser->greater_than_is_operator_p
32127     = saved_greater_than_is_operator_p;
32128   /* Restore the SAVED_SCOPE.  */
32129   parser->scope = saved_scope;
32130   parser->qualifying_scope = saved_qualifying_scope;
32131   parser->object_scope = saved_object_scope;
32132 
32133   return arguments;
32134 }
32135 
32136 /* MEMBER_FUNCTION is a member function, or a friend.  If default
32137    arguments, or the body of the function have not yet been parsed,
32138    parse them now.  */
32139 
32140 static void
cp_parser_late_parsing_for_member(cp_parser * parser,tree member_function)32141 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32142 {
32143   timevar_push (TV_PARSE_INMETH);
32144   /* If this member is a template, get the underlying
32145      FUNCTION_DECL.  */
32146   if (DECL_FUNCTION_TEMPLATE_P (member_function))
32147     member_function = DECL_TEMPLATE_RESULT (member_function);
32148 
32149   /* There should not be any class definitions in progress at this
32150      point; the bodies of members are only parsed outside of all class
32151      definitions.  */
32152   gcc_assert (parser->num_classes_being_defined == 0);
32153   /* While we're parsing the member functions we might encounter more
32154      classes.  We want to handle them right away, but we don't want
32155      them getting mixed up with functions that are currently in the
32156      queue.  */
32157   push_unparsed_function_queues (parser);
32158 
32159   /* Make sure that any template parameters are in scope.  */
32160   maybe_begin_member_template_processing (member_function);
32161 
32162   /* If the body of the function has not yet been parsed, parse it
32163      now.  Except if the tokens have been purged (PR c++/39751).  */
32164   if (DECL_PENDING_INLINE_P (member_function)
32165       && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
32166     {
32167       tree function_scope;
32168       cp_token_cache *tokens;
32169 
32170       /* The function is no longer pending; we are processing it.  */
32171       tokens = DECL_PENDING_INLINE_INFO (member_function);
32172       DECL_PENDING_INLINE_INFO (member_function) = NULL;
32173       DECL_PENDING_INLINE_P (member_function) = 0;
32174 
32175       /* If this is a local class, enter the scope of the containing
32176 	 function.  */
32177       function_scope = current_function_decl;
32178       if (function_scope)
32179 	push_function_context ();
32180 
32181       /* Push the body of the function onto the lexer stack.  */
32182       cp_parser_push_lexer_for_tokens (parser, tokens);
32183 
32184       /* Let the front end know that we going to be defining this
32185 	 function.  */
32186       start_preparsed_function (member_function, NULL_TREE,
32187 				SF_PRE_PARSED | SF_INCLASS_INLINE);
32188 
32189       /* #pragma omp declare reduction needs special parsing.  */
32190       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
32191 	{
32192 	  parser->lexer->in_pragma = true;
32193 	  cp_parser_omp_declare_reduction_exprs (member_function, parser);
32194 	  finish_function (/*inline_p=*/true);
32195 	  cp_check_omp_declare_reduction (member_function);
32196 	}
32197       else
32198 	/* Now, parse the body of the function.  */
32199 	cp_parser_function_definition_after_declarator (parser,
32200 							/*inline_p=*/true);
32201 
32202       /* Leave the scope of the containing function.  */
32203       if (function_scope)
32204 	pop_function_context ();
32205       cp_parser_pop_lexer (parser);
32206     }
32207 
32208   /* Remove any template parameters from the symbol table.  */
32209   maybe_end_member_template_processing ();
32210 
32211   /* Restore the queue.  */
32212   pop_unparsed_function_queues (parser);
32213   timevar_pop (TV_PARSE_INMETH);
32214 }
32215 
32216 /* If DECL contains any default args, remember it on the unparsed
32217    functions queue.  */
32218 
32219 static void
cp_parser_save_default_args(cp_parser * parser,tree decl)32220 cp_parser_save_default_args (cp_parser* parser, tree decl)
32221 {
32222   tree probe;
32223 
32224   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
32225        probe;
32226        probe = TREE_CHAIN (probe))
32227     if (TREE_PURPOSE (probe))
32228       {
32229 	cp_default_arg_entry entry = {current_class_type, decl};
32230 	vec_safe_push (unparsed_funs_with_default_args, entry);
32231 	break;
32232       }
32233 
32234   /* Remember if there is a noexcept-specifier to post process.  */
32235   tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
32236   if (UNPARSED_NOEXCEPT_SPEC_P (spec))
32237     vec_safe_push (unparsed_noexcepts, decl);
32238 }
32239 
32240 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32241    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
32242    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
32243    from the parameter-type-list.  */
32244 
32245 static tree
cp_parser_late_parse_one_default_arg(cp_parser * parser,tree decl,tree default_arg,tree parmtype)32246 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
32247 				      tree default_arg, tree parmtype)
32248 {
32249   cp_token_cache *tokens;
32250   tree parsed_arg;
32251   bool dummy;
32252 
32253   if (default_arg == error_mark_node)
32254     return error_mark_node;
32255 
32256   /* Push the saved tokens for the default argument onto the parser's
32257      lexer stack.  */
32258   tokens = DEFPARSE_TOKENS (default_arg);
32259   cp_parser_push_lexer_for_tokens (parser, tokens);
32260 
32261   start_lambda_scope (decl);
32262 
32263   /* Parse the default argument.  */
32264   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
32265   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
32266     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32267 
32268   finish_lambda_scope ();
32269 
32270   if (parsed_arg == error_mark_node)
32271     cp_parser_skip_to_end_of_statement (parser);
32272 
32273   if (!processing_template_decl)
32274     {
32275       /* In a non-template class, check conversions now.  In a template,
32276 	 we'll wait and instantiate these as needed.  */
32277       if (TREE_CODE (decl) == PARM_DECL)
32278 	parsed_arg = check_default_argument (parmtype, parsed_arg,
32279 					     tf_warning_or_error);
32280       else if (maybe_reject_flexarray_init (decl, parsed_arg))
32281 	parsed_arg = error_mark_node;
32282       else
32283 	parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
32284     }
32285 
32286   /* If the token stream has not been completely used up, then
32287      there was extra junk after the end of the default
32288      argument.  */
32289   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
32290     {
32291       if (TREE_CODE (decl) == PARM_DECL)
32292 	cp_parser_error (parser, "expected %<,%>");
32293       else
32294 	cp_parser_error (parser, "expected %<;%>");
32295     }
32296 
32297   /* Revert to the main lexer.  */
32298   cp_parser_pop_lexer (parser);
32299 
32300   return parsed_arg;
32301 }
32302 
32303 /* FIELD is a non-static data member with an initializer which we saved for
32304    later; parse it now.  */
32305 
32306 static void
cp_parser_late_parsing_nsdmi(cp_parser * parser,tree field)32307 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
32308 {
32309   tree def;
32310 
32311   maybe_begin_member_template_processing (field);
32312 
32313   push_unparsed_function_queues (parser);
32314   def = cp_parser_late_parse_one_default_arg (parser, field,
32315 					      DECL_INITIAL (field),
32316 					      NULL_TREE);
32317   pop_unparsed_function_queues (parser);
32318 
32319   maybe_end_member_template_processing ();
32320 
32321   DECL_INITIAL (field) = def;
32322 }
32323 
32324 /* FN is a FUNCTION_DECL which may contains a parameter with an
32325    unparsed DEFERRED_PARSE.  Parse the default args now.  This function
32326    assumes that the current scope is the scope in which the default
32327    argument should be processed.  */
32328 
32329 static void
cp_parser_late_parsing_default_args(cp_parser * parser,tree fn)32330 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
32331 {
32332   unsigned char saved_local_variables_forbidden_p;
32333 
32334   /* While we're parsing the default args, we might (due to the
32335      statement expression extension) encounter more classes.  We want
32336      to handle them right away, but we don't want them getting mixed
32337      up with default args that are currently in the queue.  */
32338   push_unparsed_function_queues (parser);
32339 
32340   /* Local variable names (and the `this' keyword) may not appear
32341      in a default argument.  */
32342   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
32343   parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
32344 
32345   push_defarg_context (fn);
32346 
32347   begin_scope (sk_function_parms, fn);
32348 
32349   /* Gather the PARM_DECLs into a vec so we can keep track of them when
32350      pushdecl clears DECL_CHAIN.  */
32351   releasing_vec parms;
32352   for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
32353        parmdecl = DECL_CHAIN (parmdecl))
32354     vec_safe_push (parms, parmdecl);
32355 
32356   tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
32357   for (int i = 0;
32358        parm && parm != void_list_node;
32359        parm = TREE_CHAIN (parm),
32360 	 ++i)
32361     {
32362       tree default_arg = TREE_PURPOSE (parm);
32363       tree parsed_arg;
32364 
32365       tree parmdecl = parms[i];
32366       pushdecl (parmdecl);
32367 
32368       if (!default_arg)
32369 	continue;
32370 
32371       if (TREE_CODE (default_arg) != DEFERRED_PARSE)
32372 	/* This can happen for a friend declaration for a function
32373 	   already declared with default arguments.  */
32374 	continue;
32375 
32376       parsed_arg
32377 	= cp_parser_late_parse_one_default_arg (parser, parmdecl,
32378 						default_arg,
32379 						TREE_VALUE (parm));
32380       TREE_PURPOSE (parm) = parsed_arg;
32381 
32382       /* Update any instantiations we've already created.  */
32383       for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
32384 	TREE_PURPOSE (copy) = parsed_arg;
32385     }
32386 
32387   pop_bindings_and_leave_scope ();
32388 
32389   /* Restore DECL_CHAINs after clobbering by pushdecl.  */
32390   parm = NULL_TREE;
32391   for (int i = parms->length () - 1; i >= 0; --i)
32392     {
32393       DECL_CHAIN (parms[i]) = parm;
32394       parm = parms[i];
32395     }
32396 
32397   pop_defarg_context ();
32398 
32399   /* Make sure no default arg is missing.  */
32400   check_default_args (fn);
32401 
32402   /* Restore the state of local_variables_forbidden_p.  */
32403   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
32404 
32405   /* Restore the queue.  */
32406   pop_unparsed_function_queues (parser);
32407 }
32408 
32409 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
32410 
32411      sizeof ... ( identifier )
32412 
32413    where the 'sizeof' token has already been consumed.  */
32414 
32415 static tree
cp_parser_sizeof_pack(cp_parser * parser)32416 cp_parser_sizeof_pack (cp_parser *parser)
32417 {
32418   /* Consume the `...'.  */
32419   cp_lexer_consume_token (parser->lexer);
32420   maybe_warn_variadic_templates ();
32421 
32422   matching_parens parens;
32423   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
32424   if (paren)
32425     parens.consume_open (parser);
32426   else
32427     permerror (cp_lexer_peek_token (parser->lexer)->location,
32428 	       "%<sizeof...%> argument must be surrounded by parentheses");
32429 
32430   cp_token *token = cp_lexer_peek_token (parser->lexer);
32431   tree name = cp_parser_identifier (parser);
32432   if (name == error_mark_node)
32433     return error_mark_node;
32434   /* The name is not qualified.  */
32435   parser->scope = NULL_TREE;
32436   parser->qualifying_scope = NULL_TREE;
32437   parser->object_scope = NULL_TREE;
32438   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
32439   if (expr == error_mark_node)
32440     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
32441 				 token->location);
32442   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
32443     expr = TREE_TYPE (expr);
32444   else if (TREE_CODE (expr) == CONST_DECL)
32445     expr = DECL_INITIAL (expr);
32446   expr = make_pack_expansion (expr);
32447   PACK_EXPANSION_SIZEOF_P (expr) = true;
32448 
32449   if (paren)
32450     parens.require_close (parser);
32451 
32452   return expr;
32453 }
32454 
32455 /* Parse the operand of `sizeof' (or a similar operator).  Returns
32456    either a TYPE or an expression, depending on the form of the
32457    input.  The KEYWORD indicates which kind of expression we have
32458    encountered.  */
32459 
32460 static tree
cp_parser_sizeof_operand(cp_parser * parser,enum rid keyword)32461 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
32462 {
32463   tree expr = NULL_TREE;
32464   const char *saved_message;
32465   const char *saved_message_arg;
32466   bool saved_integral_constant_expression_p;
32467   bool saved_non_integral_constant_expression_p;
32468 
32469   /* If it's a `...', then we are computing the length of a parameter
32470      pack.  */
32471   if (keyword == RID_SIZEOF
32472       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32473     return cp_parser_sizeof_pack (parser);
32474 
32475   /* Types cannot be defined in a `sizeof' expression.  Save away the
32476      old message.  */
32477   saved_message = parser->type_definition_forbidden_message;
32478   saved_message_arg = parser->type_definition_forbidden_message_arg;
32479   parser->type_definition_forbidden_message
32480     = G_("types may not be defined in %qs expressions");
32481   parser->type_definition_forbidden_message_arg
32482     = IDENTIFIER_POINTER (ridpointers[keyword]);
32483 
32484   /* The restrictions on constant-expressions do not apply inside
32485      sizeof expressions.  */
32486   saved_integral_constant_expression_p
32487     = parser->integral_constant_expression_p;
32488   saved_non_integral_constant_expression_p
32489     = parser->non_integral_constant_expression_p;
32490   parser->integral_constant_expression_p = false;
32491 
32492   auto cleanup = make_temp_override
32493     (parser->auto_is_implicit_function_template_parm_p, false);
32494 
32495   /* Do not actually evaluate the expression.  */
32496   ++cp_unevaluated_operand;
32497   ++c_inhibit_evaluation_warnings;
32498   /* If it's a `(', then we might be looking at the type-id
32499      construction.  */
32500   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32501     {
32502       tree type = NULL_TREE;
32503 
32504       tentative_firewall firewall (parser);
32505 
32506       /* We can't be sure yet whether we're looking at a type-id or an
32507 	 expression.  */
32508       cp_parser_parse_tentatively (parser);
32509 
32510       matching_parens parens;
32511       parens.consume_open (parser);
32512 
32513       /* Note: as a GNU Extension, compound literals are considered
32514 	 postfix-expressions as they are in C99, so they are valid
32515 	 arguments to sizeof.  See comment in cp_parser_cast_expression
32516 	 for details.  */
32517       if (cp_parser_compound_literal_p (parser))
32518 	cp_parser_simulate_error (parser);
32519       else
32520 	{
32521 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
32522 	  parser->in_type_id_in_expr_p = true;
32523 	  /* Look for the type-id.  */
32524 	  type = cp_parser_type_id (parser);
32525 	  /* Look for the closing `)'.  */
32526 	  parens.require_close (parser);
32527 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
32528 	}
32529 
32530       /* If all went well, then we're done.  */
32531       if (cp_parser_parse_definitely (parser))
32532 	expr = type;
32533       else
32534 	{
32535 	  /* Commit to the tentative_firewall so we get syntax errors.  */
32536 	  cp_parser_commit_to_tentative_parse (parser);
32537 
32538 	  expr = cp_parser_unary_expression (parser);
32539 	}
32540     }
32541   else
32542     expr = cp_parser_unary_expression (parser);
32543 
32544   /* Go back to evaluating expressions.  */
32545   --cp_unevaluated_operand;
32546   --c_inhibit_evaluation_warnings;
32547 
32548   /* And restore the old one.  */
32549   parser->type_definition_forbidden_message = saved_message;
32550   parser->type_definition_forbidden_message_arg = saved_message_arg;
32551   parser->integral_constant_expression_p
32552     = saved_integral_constant_expression_p;
32553   parser->non_integral_constant_expression_p
32554     = saved_non_integral_constant_expression_p;
32555 
32556   return expr;
32557 }
32558 
32559 /* If the current declaration has no declarator, return true.  */
32560 
32561 static bool
cp_parser_declares_only_class_p(cp_parser * parser)32562 cp_parser_declares_only_class_p (cp_parser *parser)
32563 {
32564   /* If the next token is a `;' or a `,' then there is no
32565      declarator.  */
32566   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32567 	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
32568 }
32569 
32570 /* Update the DECL_SPECS to reflect the storage class indicated by
32571    KEYWORD.  */
32572 
32573 static void
cp_parser_set_storage_class(cp_parser * parser,cp_decl_specifier_seq * decl_specs,enum rid keyword,cp_token * token)32574 cp_parser_set_storage_class (cp_parser *parser,
32575 			     cp_decl_specifier_seq *decl_specs,
32576 			     enum rid keyword,
32577 			     cp_token *token)
32578 {
32579   cp_storage_class storage_class;
32580 
32581   if (parser->in_unbraced_linkage_specification_p)
32582     {
32583       error_at (token->location, "invalid use of %qD in linkage specification",
32584 		ridpointers[keyword]);
32585       return;
32586     }
32587   else if (decl_specs->storage_class != sc_none)
32588     {
32589       decl_specs->conflicting_specifiers_p = true;
32590       return;
32591     }
32592 
32593   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
32594       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
32595       && decl_specs->gnu_thread_keyword_p)
32596     {
32597       pedwarn (decl_specs->locations[ds_thread], 0,
32598 		"%<__thread%> before %qD", ridpointers[keyword]);
32599     }
32600 
32601   switch (keyword)
32602     {
32603     case RID_AUTO:
32604       storage_class = sc_auto;
32605       break;
32606     case RID_REGISTER:
32607       storage_class = sc_register;
32608       break;
32609     case RID_STATIC:
32610       storage_class = sc_static;
32611       break;
32612     case RID_EXTERN:
32613       storage_class = sc_extern;
32614       break;
32615     case RID_MUTABLE:
32616       storage_class = sc_mutable;
32617       break;
32618     default:
32619       gcc_unreachable ();
32620     }
32621   decl_specs->storage_class = storage_class;
32622   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
32623 
32624   /* A storage class specifier cannot be applied alongside a typedef
32625      specifier. If there is a typedef specifier present then set
32626      conflicting_specifiers_p which will trigger an error later
32627      on in grokdeclarator. */
32628   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
32629     decl_specs->conflicting_specifiers_p = true;
32630 }
32631 
32632 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
32633    is true, the type is a class or enum definition.  */
32634 
32635 static void
cp_parser_set_decl_spec_type(cp_decl_specifier_seq * decl_specs,tree type_spec,cp_token * token,bool type_definition_p)32636 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
32637 			      tree type_spec,
32638 			      cp_token *token,
32639 			      bool type_definition_p)
32640 {
32641   decl_specs->any_specifiers_p = true;
32642 
32643   /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32644      wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32645      this is what happened.  In system headers, we ignore these
32646      declarations so that G++ can work with system headers that are not
32647      C++-safe.  */
32648   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
32649       && !type_definition_p
32650       && (type_spec == boolean_type_node
32651 	  || type_spec == char8_type_node
32652 	  || type_spec == char16_type_node
32653 	  || type_spec == char32_type_node
32654 	  || type_spec == wchar_type_node)
32655       && (decl_specs->type
32656 	  || decl_spec_seq_has_spec_p (decl_specs, ds_long)
32657 	  || decl_spec_seq_has_spec_p (decl_specs, ds_short)
32658 	  || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
32659 	  || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
32660     {
32661       decl_specs->redefined_builtin_type = type_spec;
32662       set_and_check_decl_spec_loc (decl_specs,
32663 				   ds_redefined_builtin_type_spec,
32664 				   token);
32665       if (!decl_specs->type)
32666 	{
32667 	  decl_specs->type = type_spec;
32668 	  decl_specs->type_definition_p = false;
32669 	  set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
32670 	}
32671     }
32672   else if (decl_specs->type)
32673     decl_specs->multiple_types_p = true;
32674   else
32675     {
32676       decl_specs->type = type_spec;
32677       decl_specs->type_definition_p = type_definition_p;
32678       decl_specs->redefined_builtin_type = NULL_TREE;
32679       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
32680     }
32681 }
32682 
32683 /* True iff TOKEN is the GNU keyword __thread.  */
32684 
32685 static bool
token_is__thread(cp_token * token)32686 token_is__thread (cp_token *token)
32687 {
32688   gcc_assert (token->keyword == RID_THREAD);
32689   return id_equal (token->u.value, "__thread");
32690 }
32691 
32692 /* Set the location for a declarator specifier and check if it is
32693    duplicated.
32694 
32695    DECL_SPECS is the sequence of declarator specifiers onto which to
32696    set the location.
32697 
32698    DS is the single declarator specifier to set which location  is to
32699    be set onto the existing sequence of declarators.
32700 
32701    LOCATION is the location for the declarator specifier to
32702    consider.  */
32703 
32704 static void
set_and_check_decl_spec_loc(cp_decl_specifier_seq * decl_specs,cp_decl_spec ds,cp_token * token)32705 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
32706 			     cp_decl_spec ds, cp_token *token)
32707 {
32708   gcc_assert (ds < ds_last);
32709 
32710   if (decl_specs == NULL)
32711     return;
32712 
32713   location_t location = token->location;
32714 
32715   if (decl_specs->locations[ds] == 0)
32716     {
32717       decl_specs->locations[ds] = location;
32718       if (ds == ds_thread)
32719 	decl_specs->gnu_thread_keyword_p = token_is__thread (token);
32720     }
32721   else
32722     {
32723       if (ds == ds_long)
32724 	{
32725 	  if (decl_specs->locations[ds_long_long] != 0)
32726 	    error_at (location,
32727 		      "%<long long long%> is too long for GCC");
32728 	  else
32729 	    {
32730 	      decl_specs->locations[ds_long_long] = location;
32731 	      pedwarn_cxx98 (location,
32732 			     OPT_Wlong_long,
32733 			     "ISO C++ 1998 does not support %<long long%>");
32734 	    }
32735 	}
32736       else if (ds == ds_thread)
32737 	{
32738 	  bool gnu = token_is__thread (token);
32739 	  gcc_rich_location richloc (location);
32740 	  if (gnu != decl_specs->gnu_thread_keyword_p)
32741 	    {
32742 	      richloc.add_range (decl_specs->locations[ds_thread]);
32743 	      error_at (&richloc,
32744 			"both %<__thread%> and %<thread_local%> specified");
32745 	    }
32746 	  else
32747 	    {
32748 	      richloc.add_fixit_remove ();
32749 	      error_at (&richloc, "duplicate %qD", token->u.value);
32750 	    }
32751 	}
32752       else
32753 	{
32754 	  static const char *const decl_spec_names[] = {
32755 	    "signed",
32756 	    "unsigned",
32757 	    "short",
32758 	    "long",
32759 	    "const",
32760 	    "volatile",
32761 	    "restrict",
32762 	    "inline",
32763 	    "virtual",
32764 	    "explicit",
32765 	    "friend",
32766 	    "typedef",
32767 	    "using",
32768 	    "constexpr",
32769 	    "__complex",
32770 	    "constinit",
32771 	    "consteval"
32772 	  };
32773 	  gcc_rich_location richloc (location);
32774 	  richloc.add_fixit_remove ();
32775 	  error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
32776 	}
32777     }
32778 }
32779 
32780 /* Return true iff the declarator specifier DS is present in the
32781    sequence of declarator specifiers DECL_SPECS.  */
32782 
32783 bool
decl_spec_seq_has_spec_p(const cp_decl_specifier_seq * decl_specs,cp_decl_spec ds)32784 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
32785 			  cp_decl_spec ds)
32786 {
32787   gcc_assert (ds < ds_last);
32788 
32789   if (decl_specs == NULL)
32790     return false;
32791 
32792   return decl_specs->locations[ds] != 0;
32793 }
32794 
32795 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
32796    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
32797 
32798 static bool
cp_parser_friend_p(const cp_decl_specifier_seq * decl_specifiers)32799 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
32800 {
32801   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
32802 }
32803 
32804 /* Issue an error message indicating that TOKEN_DESC was expected.
32805    If KEYWORD is true, it indicated this function is called by
32806    cp_parser_require_keword and the required token can only be
32807    a indicated keyword.
32808 
32809    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32810    within any error as the location of an "opening" token matching
32811    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32812    RT_CLOSE_PAREN).  */
32813 
32814 static void
cp_parser_required_error(cp_parser * parser,required_token token_desc,bool keyword,location_t matching_location)32815 cp_parser_required_error (cp_parser *parser,
32816 			  required_token token_desc,
32817 			  bool keyword,
32818 			  location_t matching_location)
32819 {
32820   if (cp_parser_simulate_error (parser))
32821     return;
32822 
32823   const char *gmsgid = NULL;
32824   switch (token_desc)
32825     {
32826       case RT_NEW:
32827 	gmsgid = G_("expected %<new%>");
32828 	break;
32829       case RT_DELETE:
32830 	gmsgid = G_("expected %<delete%>");
32831 	break;
32832       case RT_RETURN:
32833 	gmsgid = G_("expected %<return%>");
32834 	break;
32835       case RT_WHILE:
32836 	gmsgid = G_("expected %<while%>");
32837 	break;
32838       case RT_EXTERN:
32839 	gmsgid = G_("expected %<extern%>");
32840 	break;
32841       case RT_STATIC_ASSERT:
32842 	gmsgid = G_("expected %<static_assert%>");
32843 	break;
32844       case RT_DECLTYPE:
32845 	gmsgid = G_("expected %<decltype%>");
32846 	break;
32847       case RT_OPERATOR:
32848 	gmsgid = G_("expected %<operator%>");
32849 	break;
32850       case RT_CLASS:
32851 	gmsgid = G_("expected %<class%>");
32852 	break;
32853       case RT_TEMPLATE:
32854 	gmsgid = G_("expected %<template%>");
32855 	break;
32856       case RT_NAMESPACE:
32857 	gmsgid = G_("expected %<namespace%>");
32858 	break;
32859       case RT_USING:
32860 	gmsgid = G_("expected %<using%>");
32861 	break;
32862       case RT_ASM:
32863 	gmsgid = G_("expected %<asm%>");
32864 	break;
32865       case RT_TRY:
32866 	gmsgid = G_("expected %<try%>");
32867 	break;
32868       case RT_CATCH:
32869 	gmsgid = G_("expected %<catch%>");
32870 	break;
32871       case RT_THROW:
32872 	gmsgid = G_("expected %<throw%>");
32873 	break;
32874       case RT_AUTO:
32875         gmsgid = G_("expected %<auto%>");
32876         break;
32877       case RT_LABEL:
32878 	gmsgid = G_("expected %<__label__%>");
32879 	break;
32880       case RT_AT_TRY:
32881 	gmsgid = G_("expected %<@try%>");
32882 	break;
32883       case RT_AT_SYNCHRONIZED:
32884 	gmsgid = G_("expected %<@synchronized%>");
32885 	break;
32886       case RT_AT_THROW:
32887 	gmsgid = G_("expected %<@throw%>");
32888 	break;
32889       case RT_TRANSACTION_ATOMIC:
32890 	gmsgid = G_("expected %<__transaction_atomic%>");
32891 	break;
32892       case RT_TRANSACTION_RELAXED:
32893 	gmsgid = G_("expected %<__transaction_relaxed%>");
32894 	break;
32895       case RT_CO_YIELD:
32896 	gmsgid = G_("expected %<co_yield%>");
32897 	break;
32898       default:
32899 	break;
32900     }
32901 
32902   if (!gmsgid && !keyword)
32903     {
32904       switch (token_desc)
32905         {
32906 	  case RT_SEMICOLON:
32907 	    gmsgid = G_("expected %<;%>");
32908 	    break;
32909 	  case RT_OPEN_PAREN:
32910 	    gmsgid = G_("expected %<(%>");
32911 	    break;
32912 	  case RT_CLOSE_BRACE:
32913 	    gmsgid = G_("expected %<}%>");
32914 	    break;
32915 	  case RT_OPEN_BRACE:
32916 	    gmsgid = G_("expected %<{%>");
32917 	    break;
32918 	  case RT_CLOSE_SQUARE:
32919 	    gmsgid = G_("expected %<]%>");
32920 	    break;
32921 	  case RT_OPEN_SQUARE:
32922 	    gmsgid = G_("expected %<[%>");
32923 	    break;
32924 	  case RT_COMMA:
32925 	    gmsgid = G_("expected %<,%>");
32926 	    break;
32927 	  case RT_SCOPE:
32928 	    gmsgid = G_("expected %<::%>");
32929 	    break;
32930 	  case RT_LESS:
32931 	    gmsgid = G_("expected %<<%>");
32932 	    break;
32933 	  case RT_GREATER:
32934 	    gmsgid = G_("expected %<>%>");
32935 	    break;
32936 	  case RT_EQ:
32937 	    gmsgid = G_("expected %<=%>");
32938 	    break;
32939 	  case RT_ELLIPSIS:
32940 	    gmsgid = G_("expected %<...%>");
32941 	    break;
32942 	  case RT_MULT:
32943 	    gmsgid = G_("expected %<*%>");
32944 	    break;
32945 	  case RT_COMPL:
32946 	    gmsgid = G_("expected %<~%>");
32947 	    break;
32948 	  case RT_COLON:
32949 	    gmsgid = G_("expected %<:%>");
32950 	    break;
32951 	  case RT_COLON_SCOPE:
32952 	    gmsgid = G_("expected %<:%> or %<::%>");
32953 	    break;
32954 	  case RT_CLOSE_PAREN:
32955 	    gmsgid = G_("expected %<)%>");
32956 	    break;
32957 	  case RT_COMMA_CLOSE_PAREN:
32958 	    gmsgid = G_("expected %<,%> or %<)%>");
32959 	    break;
32960 	  case RT_PRAGMA_EOL:
32961 	    gmsgid = G_("expected end of line");
32962 	    break;
32963 	  case RT_NAME:
32964 	    gmsgid = G_("expected identifier");
32965 	    break;
32966 	  case RT_SELECT:
32967 	    gmsgid = G_("expected selection-statement");
32968 	    break;
32969 	  case RT_ITERATION:
32970 	    gmsgid = G_("expected iteration-statement");
32971 	    break;
32972 	  case RT_JUMP:
32973 	    gmsgid = G_("expected jump-statement");
32974 	    break;
32975 	  case RT_CLASS_KEY:
32976 	    gmsgid = G_("expected class-key");
32977 	    break;
32978 	  case RT_CLASS_TYPENAME_TEMPLATE:
32979 	    gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
32980 	    break;
32981 	  default:
32982 	    gcc_unreachable ();
32983 	}
32984     }
32985 
32986   if (gmsgid)
32987     cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
32988 }
32989 
32990 
32991 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
32992    issue an error message indicating that TOKEN_DESC was expected.
32993 
32994    Returns the token consumed, if the token had the appropriate type.
32995    Otherwise, returns NULL.
32996 
32997    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32998    within any error as the location of an "opening" token matching
32999    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33000    RT_CLOSE_PAREN).  */
33001 
33002 static cp_token *
cp_parser_require(cp_parser * parser,enum cpp_ttype type,required_token token_desc,location_t matching_location)33003 cp_parser_require (cp_parser* parser,
33004 		   enum cpp_ttype type,
33005 		   required_token token_desc,
33006 		   location_t matching_location)
33007 {
33008   if (cp_lexer_next_token_is (parser->lexer, type))
33009     return cp_lexer_consume_token (parser->lexer);
33010   else
33011     {
33012       /* Output the MESSAGE -- unless we're parsing tentatively.  */
33013       if (!cp_parser_simulate_error (parser))
33014 	cp_parser_required_error (parser, token_desc, /*keyword=*/false,
33015 				  matching_location);
33016       return NULL;
33017     }
33018 }
33019 
33020 /* Skip an entire parameter list from start to finish.  The next token must
33021    be the initial "<" of the parameter list.  Returns true on success and
33022    false otherwise.  */
33023 
33024 static bool
cp_parser_skip_entire_template_parameter_list(cp_parser * parser)33025 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
33026 {
33027   /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33028      requires it.  */
33029   cp_lexer_consume_token (parser->lexer);
33030   return cp_parser_skip_to_end_of_template_parameter_list (parser);
33031 }
33032 
33033 /* Ensure we are at the end of a template parameter list.  If we are, return.
33034    If we are not, something has gone wrong, in which case issue an error and
33035    skip to end of the parameter list.  */
33036 
33037 static void
cp_parser_require_end_of_template_parameter_list(cp_parser * parser)33038 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
33039 {
33040   /* Are we ready, yet?  If not, issue error message.  */
33041   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
33042     return;
33043 
33044   cp_parser_skip_to_end_of_template_parameter_list (parser);
33045 }
33046 
33047 /* You should only call this function from inside a template parameter list
33048    (i.e. the current token should at least be the initial "<" of the
33049    parameter list).  If you are skipping the entire list, it may be better to
33050    use cp_parser_skip_entire_template_parameter_list.
33051 
33052    Tokens are skipped until the final ">" is found, or if we see
33053    '{', '}', ';', or if we find an unbalanced ')' or ']'.
33054 
33055    Returns true if we successfully reached the end, and false if
33056    something unexpected happened (e.g. end of file).  */
33057 
33058 static bool
cp_parser_skip_to_end_of_template_parameter_list(cp_parser * parser)33059 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
33060 {
33061   /* Current level of '< ... >'.  */
33062   unsigned level = 0;
33063   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
33064   unsigned nesting_depth = 0;
33065 
33066   /* Skip tokens until the desired token is found.  */
33067   while (true)
33068     {
33069       /* Peek at the next token.  */
33070       switch (cp_lexer_peek_token (parser->lexer)->type)
33071 	{
33072 	case CPP_LESS:
33073 	  if (!nesting_depth)
33074 	    ++level;
33075 	  break;
33076 
33077         case CPP_RSHIFT:
33078           if (cxx_dialect == cxx98)
33079             /* C++0x views the `>>' operator as two `>' tokens, but
33080                C++98 does not. */
33081             break;
33082           else if (!nesting_depth && level-- == 0)
33083 	    {
33084               /* We've hit a `>>' where the first `>' closes the
33085                  template argument list, and the second `>' is
33086                  spurious.  Just consume the `>>' and stop; we've
33087                  already produced at least one error.  */
33088 	      cp_lexer_consume_token (parser->lexer);
33089 	      return false;
33090 	    }
33091           /* Fall through for C++0x, so we handle the second `>' in
33092              the `>>'.  */
33093 	  gcc_fallthrough ();
33094 
33095 	case CPP_GREATER:
33096 	  if (!nesting_depth && level-- == 0)
33097 	    {
33098 	      /* We've reached the token we want, consume it and stop.  */
33099 	      cp_lexer_consume_token (parser->lexer);
33100 	      return true;
33101 	    }
33102 	  break;
33103 
33104 	case CPP_OPEN_PAREN:
33105 	case CPP_OPEN_SQUARE:
33106 	  ++nesting_depth;
33107 	  break;
33108 
33109 	case CPP_CLOSE_PAREN:
33110 	case CPP_CLOSE_SQUARE:
33111 	  if (nesting_depth-- == 0)
33112 	    return false;
33113 	  break;
33114 
33115 	case CPP_EOF:
33116 	case CPP_PRAGMA_EOL:
33117 	case CPP_SEMICOLON:
33118 	case CPP_OPEN_BRACE:
33119 	case CPP_CLOSE_BRACE:
33120 	  /* The '>' was probably forgotten, don't look further.  */
33121 	  return false;
33122 
33123 	default:
33124 	  break;
33125 	}
33126 
33127       /* Consume this token.  */
33128       cp_lexer_consume_token (parser->lexer);
33129     }
33130 }
33131 
33132 /* If the next token is the indicated keyword, consume it.  Otherwise,
33133    issue an error message indicating that TOKEN_DESC was expected.
33134 
33135    Returns the token consumed, if the token had the appropriate type.
33136    Otherwise, returns NULL.  */
33137 
33138 static cp_token *
cp_parser_require_keyword(cp_parser * parser,enum rid keyword,required_token token_desc)33139 cp_parser_require_keyword (cp_parser* parser,
33140 			   enum rid keyword,
33141 			   required_token token_desc)
33142 {
33143   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
33144 
33145   if (token && token->keyword != keyword)
33146     {
33147       cp_parser_required_error (parser, token_desc, /*keyword=*/true,
33148                                 UNKNOWN_LOCATION);
33149       return NULL;
33150     }
33151 
33152   return token;
33153 }
33154 
33155 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33156    function-definition.  */
33157 
33158 static bool
cp_parser_token_starts_function_definition_p(cp_token * token)33159 cp_parser_token_starts_function_definition_p (cp_token* token)
33160 {
33161   return (/* An ordinary function-body begins with an `{'.  */
33162 	  token->type == CPP_OPEN_BRACE
33163 	  /* A ctor-initializer begins with a `:'.  */
33164 	  || token->type == CPP_COLON
33165 	  /* A function-try-block begins with `try'.  */
33166 	  || token->keyword == RID_TRY
33167 	  /* A function-transaction-block begins with `__transaction_atomic'
33168 	     or `__transaction_relaxed'.  */
33169 	  || token->keyword == RID_TRANSACTION_ATOMIC
33170 	  || token->keyword == RID_TRANSACTION_RELAXED
33171 	  /* The named return value extension begins with `return'.  */
33172 	  || token->keyword == RID_RETURN);
33173 }
33174 
33175 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33176    definition.  */
33177 
33178 static bool
cp_parser_next_token_starts_class_definition_p(cp_parser * parser)33179 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
33180 {
33181   cp_token *token;
33182 
33183   token = cp_lexer_peek_token (parser->lexer);
33184   return (token->type == CPP_OPEN_BRACE
33185 	  || (token->type == CPP_COLON
33186 	      && !parser->colon_doesnt_start_class_def_p));
33187 }
33188 
33189 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33190    C++0x) ending a template-argument.  */
33191 
33192 static bool
cp_parser_next_token_ends_template_argument_p(cp_parser * parser)33193 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
33194 {
33195   cp_token *token;
33196 
33197   token = cp_lexer_peek_token (parser->lexer);
33198   return (token->type == CPP_COMMA
33199           || token->type == CPP_GREATER
33200           || token->type == CPP_ELLIPSIS
33201 	  || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
33202 }
33203 
33204 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33205    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
33206 
33207 static bool
cp_parser_nth_token_starts_template_argument_list_p(cp_parser * parser,size_t n)33208 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
33209 						     size_t n)
33210 {
33211   cp_token *token;
33212 
33213   token = cp_lexer_peek_nth_token (parser->lexer, n);
33214   if (token->type == CPP_LESS)
33215     return true;
33216   /* Check for the sequence `<::' in the original code. It would be lexed as
33217      `[:', where `[' is a digraph, and there is no whitespace before
33218      `:'.  */
33219   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
33220     {
33221       cp_token *token2;
33222       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
33223       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
33224 	return true;
33225     }
33226   return false;
33227 }
33228 
33229 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33230    or none_type otherwise.  */
33231 
33232 static enum tag_types
cp_parser_token_is_class_key(cp_token * token)33233 cp_parser_token_is_class_key (cp_token* token)
33234 {
33235   switch (token->keyword)
33236     {
33237     case RID_CLASS:
33238       return class_type;
33239     case RID_STRUCT:
33240       return record_type;
33241     case RID_UNION:
33242       return union_type;
33243 
33244     default:
33245       return none_type;
33246     }
33247 }
33248 
33249 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33250    or none_type otherwise or if the token is null.  */
33251 
33252 static enum tag_types
cp_parser_token_is_type_parameter_key(cp_token * token)33253 cp_parser_token_is_type_parameter_key (cp_token* token)
33254 {
33255   if (!token)
33256     return none_type;
33257 
33258   switch (token->keyword)
33259     {
33260     case RID_CLASS:
33261       return class_type;
33262     case RID_TYPENAME:
33263       return typename_type;
33264 
33265     default:
33266       return none_type;
33267     }
33268 }
33269 
33270 /* Diagnose redundant enum-keys.  */
33271 
33272 static void
cp_parser_maybe_warn_enum_key(cp_parser * parser,location_t key_loc,tree type,rid scoped_key)33273 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
33274 			       tree type, rid scoped_key)
33275 {
33276   if (!warn_redundant_tags)
33277     return;
33278 
33279   tree type_decl = TYPE_MAIN_DECL (type);
33280   tree name = DECL_NAME (type_decl);
33281   /* Look up the NAME to see if it unambiguously refers to the TYPE.  */
33282   push_deferring_access_checks (dk_no_check);
33283   tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33284   pop_deferring_access_checks ();
33285 
33286   /* The enum-key is redundant for uses of the TYPE that are not
33287      declarations and for which name lookup returns just the type
33288      itself.  */
33289   if (decl != type_decl)
33290     return;
33291 
33292   if (scoped_key != RID_CLASS
33293       && scoped_key != RID_STRUCT
33294       && current_lang_name != lang_name_cplusplus
33295       && current_namespace == global_namespace)
33296     {
33297       /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33298 	 enum tag in shared C/C++ code in files (such as headers) included
33299 	 in the main source file.  */
33300       const line_map_ordinary *map = NULL;
33301       linemap_resolve_location (line_table, key_loc,
33302 				LRK_MACRO_DEFINITION_LOCATION,
33303 				&map);
33304       if (!MAIN_FILE_P (map))
33305 	return;
33306     }
33307 
33308   gcc_rich_location richloc (key_loc);
33309   richloc.add_fixit_remove (key_loc);
33310   warning_at (&richloc, OPT_Wredundant_tags,
33311 	      "redundant enum-key %<enum%s%> in reference to %q#T",
33312 	      (scoped_key == RID_CLASS ? " class"
33313 	       : scoped_key == RID_STRUCT ? " struct" : ""), type);
33314 }
33315 
33316 /* Describes the set of declarations of a struct, class, or class template
33317    or its specializations.  Used for -Wmismatched-tags.  */
33318 
33319 class class_decl_loc_t
33320 {
33321  public:
33322 
class_decl_loc_t()33323   class_decl_loc_t ()
33324     : locvec (), idxdef (), def_class_key ()
33325   {
33326     locvec.create (4);
33327   }
33328 
33329   /* Constructs an object for a single declaration of a class with
33330      CLASS_KEY at the current location in the current function (or
33331      at another scope).  KEY_REDUNDANT is true if the class-key may
33332      be omitted in the current context without an ambiguity with
33333      another symbol with the same name.
33334      DEF_P is true for a class declaration that is a definition.
33335      CURLOC is the associated location.  */
class_decl_loc_t(tag_types class_key,bool key_redundant,bool def_p,location_t curloc=input_location)33336   class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
33337 		    location_t curloc = input_location)
33338     : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
33339   {
33340     locvec.create (4);
33341     class_key_loc_t ckl (current_function_decl, curloc, class_key,
33342 			 key_redundant);
33343     locvec.quick_push (ckl);
33344   }
33345 
33346   /* Copy, assign, and destroy the object.  Necessary because LOCVEC
33347      isn't safely copyable and assignable and doesn't release storage
33348      on its own.  */
class_decl_loc_t(const class_decl_loc_t & rhs)33349   class_decl_loc_t (const class_decl_loc_t &rhs)
33350     : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
33351       def_class_key (rhs.def_class_key)
33352   { }
33353 
operator =(const class_decl_loc_t & rhs)33354   class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
33355   {
33356     if (this == &rhs)
33357       return *this;
33358     locvec.release ();
33359     locvec = rhs.locvec.copy ();
33360     idxdef = rhs.idxdef;
33361     def_class_key = rhs.def_class_key;
33362     return *this;
33363   }
33364 
~class_decl_loc_t()33365   ~class_decl_loc_t ()
33366   {
33367     locvec.release ();
33368   }
33369 
33370   /* Issues -Wmismatched-tags for a single class.  */
33371   void diag_mismatched_tags (tree);
33372 
33373   /* Issues -Wmismatched-tags for all classes.  */
33374   static void diag_mismatched_tags ();
33375 
33376   /* Adds TYPE_DECL to the collection of class decls and diagnoses
33377      redundant tags (if -Wredundant-tags is enabled).  */
33378   static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
33379 
33380   /* Either adds this decl to the collection of class decls
33381      or diagnoses it, whichever is appropriate.  */
33382   void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
33383 
33384 private:
33385 
function(unsigned i) const33386   tree function (unsigned i) const
33387   {
33388     return locvec[i].func;
33389   }
33390 
location(unsigned i) const33391   location_t location (unsigned i) const
33392   {
33393     return locvec[i].loc;
33394   }
33395 
key_redundant(unsigned i) const33396   bool key_redundant (unsigned i) const
33397   {
33398     return locvec[i].key_redundant;
33399   }
33400 
class_key(unsigned i) const33401   tag_types class_key (unsigned i) const
33402   {
33403     return locvec[i].class_key;
33404   }
33405 
33406   /* True if a definition for the class has been seen.  */
def_p() const33407   bool def_p () const
33408   {
33409     return idxdef < locvec.length ();
33410   }
33411 
33412   /* The location of a single mention of a class type with the given
33413      class-key.  */
33414   struct class_key_loc_t
33415   {
class_key_loc_tclass_decl_loc_t::class_key_loc_t33416     class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
33417       : func (func), loc (loc), class_key (key), key_redundant (redundant)
33418     { }
33419 
33420     /* The function the type is mentioned in.  */
33421     tree func;
33422     /* The exact location.  */
33423     location_t loc;
33424     /* The class-key used in the mention of the type.  */
33425     tag_types class_key;
33426     /* True when the class-key could be omitted at this location
33427        without an ambiguity with another symbol of the same name.  */
33428     bool key_redundant;
33429   };
33430   /* Avoid using auto_vec here since it's not safe to copy due to pr90904.  */
33431   vec <class_key_loc_t> locvec;
33432   /* LOCVEC index of the definition or UINT_MAX if none exists.  */
33433   unsigned idxdef;
33434   /* The class-key the class was last declared with or none_type when
33435      it has been declared with a mismatched key.  */
33436   tag_types def_class_key;
33437 
33438   /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33439      description above.  */
33440   typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
33441   static class_to_loc_map_t class2loc;
33442 };
33443 
33444 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
33445 
33446 /* Issue an error message if the CLASS_KEY does not match the TYPE.
33447    DEF_P is expected to be set for a definition of class TYPE.  DECL_P
33448    is set for a declaration of class TYPE and clear for a reference to
33449    it that is not a declaration of it.  */
33450 
33451 static void
cp_parser_check_class_key(cp_parser * parser,location_t key_loc,tag_types class_key,tree type,bool def_p,bool decl_p)33452 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
33453 			   tag_types class_key, tree type, bool def_p,
33454 			   bool decl_p)
33455 {
33456   if (type == error_mark_node)
33457     return;
33458 
33459   bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
33460   if (seen_as_union != (class_key == union_type))
33461     {
33462       if (permerror (input_location, "%qs tag used in naming %q#T",
33463 		     class_key == union_type ? "union"
33464 		     : class_key == record_type ? "struct" : "class",
33465 		     type))
33466 	inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
33467 		"%q#T was previously declared here", type);
33468       return;
33469     }
33470 
33471   if (!warn_mismatched_tags && !warn_redundant_tags)
33472     return;
33473 
33474   /* Only consider the true class-keys below and ignore typename_type,
33475      etc. that are not C++ class-keys.  */
33476   if (class_key != class_type
33477       && class_key != record_type
33478       && class_key != union_type)
33479     return;
33480 
33481   class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
33482 }
33483 
33484 /* Returns the template or specialization of one to which the RECORD_TYPE
33485    TYPE corresponds.  */
33486 
33487 static tree
specialization_of(tree type)33488 specialization_of (tree type)
33489 {
33490   tree ret = type;
33491 
33492   /* Determine the template or its partial specialization to which TYPE
33493      corresponds.  */
33494   if (tree spec = most_specialized_partial_spec (type, tf_none))
33495     if (spec != error_mark_node)
33496       ret = TREE_TYPE (TREE_VALUE (spec));
33497 
33498   if (ret == type)
33499     ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
33500 
33501   return TYPE_MAIN_DECL (ret);
33502 }
33503 
33504 
33505 /* Adds the class TYPE to the collection of class decls and diagnoses
33506    redundant tags (if -Wredundant-tags is enabled).
33507    DEF_P is expected to be set for a definition of class TYPE.  DECL_P
33508    is set for a (likely, based on syntactic context) declaration of class
33509    TYPE and clear for a reference to it that is not a declaration of it.  */
33510 
33511 void
add(cp_parser * parser,location_t key_loc,tag_types class_key,tree type,bool def_p,bool decl_p)33512 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
33513 		       tag_types class_key, tree type, bool def_p, bool decl_p)
33514 {
33515   tree type_decl = TYPE_MAIN_DECL (type);
33516   tree name = DECL_NAME (type_decl);
33517   /* Look up the NAME to see if it unambiguously refers to the TYPE
33518      and set KEY_REDUNDANT if so.  */
33519   push_deferring_access_checks (dk_no_check);
33520   tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33521   pop_deferring_access_checks ();
33522 
33523   /* The class-key is redundant for uses of the CLASS_TYPE that are
33524      neither definitions of it nor declarations, and for which name
33525      lookup returns just the type itself.  */
33526   bool key_redundant = (!def_p && !decl_p
33527 			&& (decl == type_decl
33528 			    || TREE_CODE (decl) == TEMPLATE_DECL
33529 			    || (CLASS_TYPE_P (type)
33530 				&& TYPE_BEING_DEFINED (type))));
33531 
33532   if (key_redundant
33533       && class_key != class_type
33534       && current_lang_name != lang_name_cplusplus
33535       && current_namespace == global_namespace)
33536     {
33537       /* Avoid issuing the diagnostic for apparently redundant struct
33538 	 and union class-keys in shared C/C++ code in files (such as
33539 	 headers) included in the main source file.  */
33540       const line_map_ordinary *map = NULL;
33541       linemap_resolve_location (line_table, key_loc,
33542 				LRK_MACRO_DEFINITION_LOCATION,
33543 				&map);
33544       if (!MAIN_FILE_P (map))
33545 	key_redundant = false;
33546     }
33547 
33548   /* Set if a declaration of TYPE has previously been seen or if it must
33549      exist in a precompiled header.  */
33550   bool exist;
33551   class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
33552   if (!exist)
33553     {
33554       tree type = TREE_TYPE (type_decl);
33555       if (def_p || !COMPLETE_TYPE_P (type))
33556 	{
33557 	  /* TYPE_DECL is the first declaration or definition of the type
33558 	     (outside precompiled headers -- see below).  Just create
33559 	     a new entry for it and return unless it's a declaration
33560 	     involving a template that may need to be diagnosed by
33561 	     -Wredundant-tags.  */
33562 	  *rdl = class_decl_loc_t (class_key, false, def_p);
33563 	  if (TREE_CODE (decl) != TEMPLATE_DECL)
33564 	    return;
33565 	}
33566       else
33567 	{
33568 	  /* TYPE was previously defined in some unknown precompiled header.
33569 	     Simply add a record of its definition at an unknown location and
33570 	     proceed below to add a reference to it at the current location.
33571 	     (Declarations in precompiled headers that are not definitions
33572 	     are ignored.)  */
33573 	  tag_types def_key
33574 	    = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
33575 	  location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
33576 	  *rdl = class_decl_loc_t (def_key, false, true, def_loc);
33577 	  exist = true;
33578 	}
33579     }
33580 
33581   /* A prior declaration of TYPE_DECL has been seen.  */
33582 
33583   if (key_redundant)
33584     {
33585       gcc_rich_location richloc (key_loc);
33586       richloc.add_fixit_remove (key_loc);
33587       warning_at (&richloc, OPT_Wredundant_tags,
33588 		  "redundant class-key %qs in reference to %q#T",
33589 		  class_key == union_type ? "union"
33590 		  : class_key == record_type ? "struct" : "class",
33591 		  type);
33592     }
33593 
33594   if (!exist)
33595     /* Do nothing if this is the first declaration of the type.  */
33596     return;
33597 
33598   if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
33599     /* Do nothing if the class-key in this declaration matches
33600        the definition.  */
33601     return;
33602 
33603   rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
33604 				   def_p);
33605 }
33606 
33607 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
33608    of class decls or diagnoses it, whichever is appropriate.  */
33609 
33610 void
add_or_diag_mismatched_tag(tree type_decl,tag_types class_key,bool redundant,bool def_p)33611 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
33612 					      tag_types class_key,
33613 					      bool redundant,
33614 					      bool def_p)
33615 {
33616   /* Reset the CLASS_KEY associated with this type on mismatch.
33617      This is an optimization that lets the diagnostic code skip
33618      over classes that use the same class-key in all declarations.  */
33619   if (def_class_key != class_key)
33620     def_class_key = none_type;
33621 
33622   /* Set IDXDEF to the index of the vector corresponding to
33623      the definition.  */
33624   if (def_p)
33625     idxdef = locvec.length ();
33626 
33627   /* Append a record of this declaration to the vector.  */
33628   class_key_loc_t ckl (current_function_decl, input_location, class_key,
33629 		       redundant);
33630   locvec.safe_push (ckl);
33631 
33632   if (idxdef == UINT_MAX)
33633     return;
33634 
33635   /* As a space optimization diagnose declarations of a class
33636      whose definition has been seen and purge the LOCVEC of
33637      all entries except the definition.  */
33638   diag_mismatched_tags (type_decl);
33639   if (idxdef)
33640     {
33641       class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
33642       locvec.release ();
33643       locvec.reserve (2);
33644       locvec.safe_push (ent);
33645       idxdef = 0;
33646     }
33647   else
33648     /* Pop the entry pushed above for this declaration.  */
33649     locvec.pop ();
33650 }
33651 
33652 /* Issues -Wmismatched-tags for a single class.  */
33653 
33654 void
diag_mismatched_tags(tree type_decl)33655 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
33656 {
33657   if (!warn_mismatched_tags)
33658     return;
33659 
33660   /* Number of uses of the class.  */
33661   const unsigned ndecls = locvec.length ();
33662 
33663   /* The class (or template) declaration guiding the decisions about
33664      the diagnostic.  For ordinary classes it's the same as THIS.  For
33665      uses of instantiations of templates other than their declarations
33666      it points to the record for the declaration of the corresponding
33667      primary template or partial specialization.  */
33668   class_decl_loc_t *cdlguide = this;
33669 
33670   tree type = TREE_TYPE (type_decl);
33671   if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
33672     {
33673       /* For implicit instantiations of a primary template look up
33674 	 the primary or partial specialization and use it as
33675 	 the expected class-key rather than using the class-key of
33676 	 the first reference to the instantiation.  The primary must
33677 	 be (and inevitably is) at index zero.  */
33678       tree spec = specialization_of (type);
33679       cdlguide = class2loc.get (spec);
33680       /* It's possible that we didn't find SPEC.  Consider:
33681 
33682 	   template<typename T> struct A {
33683 	     template<typename U> struct W { };
33684 	   };
33685 	   struct A<int>::W<int> w; // #1
33686 
33687 	 where while parsing A and #1 we've stashed
33688 	   A<T>
33689 	   A<T>::W<U>
33690 	   A<int>::W<int>
33691 	 into CLASS2LOC.  If TYPE is A<int>::W<int>, specialization_of
33692 	 will yield A<int>::W<U> which may be in CLASS2LOC if we had
33693 	 an A<int> class specialization, but otherwise won't be in it.
33694 	 So try to look up A<T>::W<U>.  */
33695       if (!cdlguide)
33696 	{
33697 	  spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
33698 	  cdlguide = class2loc.get (spec);
33699 	}
33700       /* Now we really should have found something.  */
33701       gcc_assert (cdlguide != NULL);
33702     }
33703   /* Skip declarations that consistently use the same class-key.  */
33704   else if (def_class_key != none_type)
33705     return;
33706 
33707   /* Set if a definition for the class has been seen.  */
33708   const bool def_p = cdlguide->def_p ();
33709 
33710   /* The index of the declaration whose class-key this declaration
33711      is expected to match.  It's either the class-key of the class
33712      definition if one exists or the first declaration otherwise.  */
33713   const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
33714 
33715   /* The class-key the class is expected to be declared with: it's
33716      either the key used in its definition or the first declaration
33717      if no definition has been provided.
33718      For implicit instantiations of a primary template it's
33719      the class-key used to declare the primary with.  The primary
33720      must be at index zero.  */
33721   const tag_types xpect_key = cdlguide->class_key (idxguide);
33722 
33723   unsigned idx = 0;
33724   /* Advance IDX to the first declaration that either is not
33725      a definition or that doesn't match the first declaration
33726      if no definition is provided.  */
33727   while (class_key (idx) == xpect_key)
33728     if (++idx == ndecls)
33729       return;
33730 
33731   /* Save the current function before changing it below.  */
33732   tree save_func = current_function_decl;
33733   /* Set the function declaration to print in diagnostic context.  */
33734   current_function_decl = function (idx);
33735 
33736   const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
33737   const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
33738 
33739   location_t loc = location (idx);
33740   bool key_redundant_p = key_redundant (idx);
33741   auto_diagnostic_group d;
33742   /* Issue a warning for the first mismatched declaration.
33743      Avoid using "%#qT" since the class-key for the same type will
33744      be the same regardless of which one was used in the declaraion.  */
33745   if (warning_at (loc, OPT_Wmismatched_tags,
33746 		  "%qT declared with a mismatched class-key %qs",
33747 		  type_decl, xmatchkstr))
33748     {
33749       /* Suggest how to avoid the warning for each instance since
33750 	 the guidance may be different depending on context.  */
33751       inform (loc,
33752 	      (key_redundant_p
33753 	       ? G_("remove the class-key or replace it with %qs")
33754 	       : G_("replace the class-key with %qs")),
33755 	      xpectkstr);
33756 
33757       /* Also point to the first declaration or definition that guided
33758 	 the decision to issue the warning above.  */
33759       inform (cdlguide->location (idxguide),
33760 	      (def_p
33761 	       ? G_("%qT defined as %qs here")
33762 	       : G_("%qT first declared as %qs here")),
33763 	      type_decl, xpectkstr);
33764     }
33765 
33766   /* Issue warnings for the remaining inconsistent declarations.  */
33767   for (unsigned i = idx + 1; i != ndecls; ++i)
33768     {
33769       tag_types clskey = class_key (i);
33770       /* Skip over the declarations that match either the definition
33771 	 if one was provided or the first declaration.  */
33772       if (clskey == xpect_key)
33773 	continue;
33774 
33775       loc = location (i);
33776       key_redundant_p = key_redundant (i);
33777       /* Set the function declaration to print in diagnostic context.  */
33778       current_function_decl = function (i);
33779       if (warning_at (loc, OPT_Wmismatched_tags,
33780 		      "%qT declared with a mismatched class-key %qs",
33781 		      type_decl, xmatchkstr))
33782 	/* Suggest how to avoid the warning for each instance since
33783 	   the guidance may be different depending on context.  */
33784 	inform (loc,
33785 		(key_redundant_p
33786 		 ? G_("remove the class-key or replace it with %qs")
33787 		 : G_("replace the class-key with %qs")),
33788 		xpectkstr);
33789     }
33790 
33791   /* Restore the current function in case it was replaced above.  */
33792   current_function_decl = save_func;
33793 }
33794 
33795 /* Issues -Wmismatched-tags for all classes.  Called at the end
33796    of processing a translation unit, after declarations of all class
33797    types and their uses have been recorded.  */
33798 
33799 void
diag_mismatched_tags()33800 class_decl_loc_t::diag_mismatched_tags ()
33801 {
33802   /* CLASS2LOC should be empty if both -Wmismatched-tags and
33803      -Wredundant-tags are disabled.  */
33804   gcc_assert (warn_mismatched_tags
33805 	      || warn_redundant_tags
33806 	      || class2loc.is_empty ());
33807 
33808   /* Save the current function before changing on return.  It should
33809      be null at this point.  */
33810   temp_override<tree> cleanup (current_function_decl);
33811 
33812   if (warn_mismatched_tags)
33813     {
33814       /* Iterate over the collected class/struct/template declarations.  */
33815       typedef class_to_loc_map_t::iterator iter_t;
33816       for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
33817 	{
33818 	  tree type_decl = (*it).first;
33819 	  class_decl_loc_t &recloc = (*it).second;
33820 	  recloc.diag_mismatched_tags (type_decl);
33821 	}
33822     }
33823 
33824   class2loc.empty ();
33825 }
33826 
33827 /* Issue an error message if DECL is redeclared with different
33828    access than its original declaration [class.access.spec/3].
33829    This applies to nested classes, nested class templates and
33830    enumerations [class.mem/1].  */
33831 
33832 static void
cp_parser_check_access_in_redeclaration(tree decl,location_t location)33833 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
33834 {
33835   if (!decl
33836       || (!CLASS_TYPE_P (TREE_TYPE (decl))
33837 	  && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
33838     return;
33839 
33840   if ((TREE_PRIVATE (decl)
33841        != (current_access_specifier == access_private_node))
33842       || (TREE_PROTECTED (decl)
33843 	  != (current_access_specifier == access_protected_node)))
33844     error_at (location, "%qD redeclared with different access", decl);
33845 }
33846 
33847 /* Look for the `template' keyword, as a syntactic disambiguator.
33848    Return TRUE iff it is present, in which case it will be
33849    consumed.  */
33850 
33851 static bool
cp_parser_optional_template_keyword(cp_parser * parser)33852 cp_parser_optional_template_keyword (cp_parser *parser)
33853 {
33854   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33855     {
33856       /* In C++98 the `template' keyword can only be used within templates;
33857 	 outside templates the parser can always figure out what is a
33858 	 template and what is not.  In C++11,  per the resolution of DR 468,
33859 	 `template' is allowed in cases where it is not strictly necessary.  */
33860       if (!processing_template_decl
33861 	  && pedantic && cxx_dialect == cxx98)
33862 	{
33863 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
33864 	  pedwarn (token->location, OPT_Wpedantic,
33865 		   "in C++98 %<template%> (as a disambiguator) is only "
33866 		   "allowed within templates");
33867 	  /* If this part of the token stream is rescanned, the same
33868 	     error message would be generated.  So, we purge the token
33869 	     from the stream.  */
33870 	  cp_lexer_purge_token (parser->lexer);
33871 	  return false;
33872 	}
33873       else
33874 	{
33875 	  /* Consume the `template' keyword.  */
33876 	  cp_lexer_consume_token (parser->lexer);
33877 	  return true;
33878 	}
33879     }
33880   return false;
33881 }
33882 
33883 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
33884    set PARSER->SCOPE, and perform other related actions.  */
33885 
33886 static void
cp_parser_pre_parsed_nested_name_specifier(cp_parser * parser)33887 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
33888 {
33889   struct tree_check *check_value;
33890 
33891   /* Get the stored value.  */
33892   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
33893   /* Set the scope from the stored value.  */
33894   parser->scope = saved_checks_value (check_value);
33895   parser->qualifying_scope = check_value->qualifying_scope;
33896   parser->object_scope = parser->context->object_type;
33897   parser->context->object_type = NULL_TREE;
33898 }
33899 
33900 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
33901    encounter the end of a block before what we were looking for.  */
33902 
33903 static bool
cp_parser_cache_group(cp_parser * parser,enum cpp_ttype end,unsigned depth)33904 cp_parser_cache_group (cp_parser *parser,
33905 		       enum cpp_ttype end,
33906 		       unsigned depth)
33907 {
33908   while (true)
33909     {
33910       cp_token *token = cp_lexer_peek_token (parser->lexer);
33911 
33912       /* Abort a parenthesized expression if we encounter a semicolon.  */
33913       if ((end == CPP_CLOSE_PAREN || depth == 0)
33914 	  && token->type == CPP_SEMICOLON)
33915 	return true;
33916       /* If we've reached the end of the file, stop.  */
33917       if (token->type == CPP_EOF
33918 	  || (end != CPP_PRAGMA_EOL
33919 	      && token->type == CPP_PRAGMA_EOL))
33920 	return true;
33921       if (token->type == CPP_CLOSE_BRACE && depth == 0)
33922 	/* We've hit the end of an enclosing block, so there's been some
33923 	   kind of syntax error.  */
33924 	return true;
33925 
33926       /* Consume the token.  */
33927       cp_lexer_consume_token (parser->lexer);
33928       /* See if it starts a new group.  */
33929       if (token->type == CPP_OPEN_BRACE)
33930 	{
33931 	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
33932 	  /* In theory this should probably check end == '}', but
33933 	     cp_parser_save_member_function_body needs it to exit
33934 	     after either '}' or ')' when called with ')'.  */
33935 	  if (depth == 0)
33936 	    return false;
33937 	}
33938       else if (token->type == CPP_OPEN_PAREN)
33939 	{
33940 	  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
33941 	  if (depth == 0 && end == CPP_CLOSE_PAREN)
33942 	    return false;
33943 	}
33944       else if (token->type == CPP_PRAGMA)
33945 	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
33946       else if (token->type == end)
33947 	return false;
33948     }
33949 }
33950 
33951 /* Like above, for caching a default argument or NSDMI.  Both of these are
33952    terminated by a non-nested comma, but it can be unclear whether or not a
33953    comma is nested in a template argument list unless we do more parsing.
33954    In order to handle this ambiguity, when we encounter a ',' after a '<'
33955    we try to parse what follows as a parameter-declaration-list (in the
33956    case of a default argument) or a member-declarator (in the case of an
33957    NSDMI).  If that succeeds, then we stop caching.  */
33958 
33959 static tree
cp_parser_cache_defarg(cp_parser * parser,bool nsdmi)33960 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
33961 {
33962   unsigned depth = 0;
33963   int maybe_template_id = 0;
33964   cp_token *first_token;
33965   cp_token *token;
33966   tree default_argument;
33967 
33968   /* Add tokens until we have processed the entire default
33969      argument.  We add the range [first_token, token).  */
33970   first_token = cp_lexer_peek_token (parser->lexer);
33971   if (first_token->type == CPP_OPEN_BRACE)
33972     {
33973       /* For list-initialization, this is straightforward.  */
33974       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33975       token = cp_lexer_peek_token (parser->lexer);
33976     }
33977   else while (true)
33978     {
33979       bool done = false;
33980 
33981       /* Peek at the next token.  */
33982       token = cp_lexer_peek_token (parser->lexer);
33983       /* What we do depends on what token we have.  */
33984       switch (token->type)
33985 	{
33986 	  /* In valid code, a default argument must be
33987 	     immediately followed by a `,' `)', or `...'.  */
33988 	case CPP_COMMA:
33989 	  if (depth == 0 && maybe_template_id)
33990 	    {
33991 	      /* If we've seen a '<', we might be in a
33992 		 template-argument-list.  Until Core issue 325 is
33993 		 resolved, we don't know how this situation ought
33994 		 to be handled, so try to DTRT.  We check whether
33995 		 what comes after the comma is a valid parameter
33996 		 declaration list.  If it is, then the comma ends
33997 		 the default argument; otherwise the default
33998 		 argument continues.  */
33999 	      bool error = false;
34000 	      cp_token *peek;
34001 
34002 	      /* Set ITALP so cp_parser_parameter_declaration_list
34003 		 doesn't decide to commit to this parse.  */
34004 	      bool saved_italp = parser->in_template_argument_list_p;
34005 	      parser->in_template_argument_list_p = true;
34006 
34007 	      cp_parser_parse_tentatively (parser);
34008 
34009 	      if (nsdmi)
34010 		{
34011 		  /* Parse declarators until we reach a non-comma or
34012 		     somthing that cannot be an initializer.
34013 		     Just checking whether we're looking at a single
34014 		     declarator is insufficient.  Consider:
34015 		       int var = tuple<T,U>::x;
34016 		     The template parameter 'U' looks exactly like a
34017 		     declarator.  */
34018 		  do
34019 		    {
34020 		      int ctor_dtor_or_conv_p;
34021 		      cp_lexer_consume_token (parser->lexer);
34022 		      cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34023 					    CP_PARSER_FLAGS_NONE,
34024 					    &ctor_dtor_or_conv_p,
34025 					    /*parenthesized_p=*/NULL,
34026 					    /*member_p=*/true,
34027 					    /*friend_p=*/false,
34028 					    /*static_p=*/false);
34029 		      peek = cp_lexer_peek_token (parser->lexer);
34030 		      if (cp_parser_error_occurred (parser))
34031 			break;
34032 		    }
34033 		  while (peek->type == CPP_COMMA);
34034 		  /* If we met an '=' or ';' then the original comma
34035 		     was the end of the NSDMI.  Otherwise assume
34036 		     we're still in the NSDMI.  */
34037 		  error = (peek->type != CPP_EQ
34038 			   && peek->type != CPP_SEMICOLON);
34039 		}
34040 	      else
34041 		{
34042 		  cp_lexer_consume_token (parser->lexer);
34043 		  begin_scope (sk_function_parms, NULL_TREE);
34044 		  tree t = cp_parser_parameter_declaration_list
34045 			    (parser, CP_PARSER_FLAGS_NONE);
34046 		  if (t == error_mark_node)
34047 		    error = true;
34048 		  pop_bindings_and_leave_scope ();
34049 		}
34050 	      if (!cp_parser_error_occurred (parser) && !error)
34051 		done = true;
34052 	      cp_parser_abort_tentative_parse (parser);
34053 
34054 	      parser->in_template_argument_list_p = saved_italp;
34055 	      break;
34056 	    }
34057 	  /* FALLTHRU */
34058 	case CPP_CLOSE_PAREN:
34059 	case CPP_ELLIPSIS:
34060 	  /* If we run into a non-nested `;', `}', or `]',
34061 	     then the code is invalid -- but the default
34062 	     argument is certainly over.  */
34063 	case CPP_SEMICOLON:
34064 	case CPP_CLOSE_BRACE:
34065 	case CPP_CLOSE_SQUARE:
34066 	  if (depth == 0
34067 	      /* Handle correctly int n = sizeof ... ( p );  */
34068 	      && token->type != CPP_ELLIPSIS)
34069 	    done = true;
34070 	  /* Update DEPTH, if necessary.  */
34071 	  else if (token->type == CPP_CLOSE_PAREN
34072 		   || token->type == CPP_CLOSE_BRACE
34073 		   || token->type == CPP_CLOSE_SQUARE)
34074 	    --depth;
34075 	  break;
34076 
34077 	case CPP_OPEN_PAREN:
34078 	case CPP_OPEN_SQUARE:
34079 	case CPP_OPEN_BRACE:
34080 	  ++depth;
34081 	  break;
34082 
34083 	case CPP_LESS:
34084 	  if (depth == 0)
34085 	    /* This might be the comparison operator, or it might
34086 	       start a template argument list.  */
34087 	    ++maybe_template_id;
34088 	  break;
34089 
34090 	case CPP_RSHIFT:
34091 	  if (cxx_dialect == cxx98)
34092 	    break;
34093 	  /* Fall through for C++0x, which treats the `>>'
34094 	     operator like two `>' tokens in certain
34095 	     cases.  */
34096 	  gcc_fallthrough ();
34097 
34098 	case CPP_GREATER:
34099 	  if (depth == 0)
34100 	    {
34101 	      /* This might be an operator, or it might close a
34102 		 template argument list.  But if a previous '<'
34103 		 started a template argument list, this will have
34104 		 closed it, so we can't be in one anymore.  */
34105 	      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
34106 	      if (maybe_template_id < 0)
34107 		maybe_template_id = 0;
34108 	    }
34109 	  break;
34110 
34111 	  /* If we run out of tokens, issue an error message.  */
34112 	case CPP_EOF:
34113 	case CPP_PRAGMA_EOL:
34114 	  error_at (token->location, "file ends in default argument");
34115 	  return error_mark_node;
34116 
34117 	case CPP_NAME:
34118 	case CPP_SCOPE:
34119 	  /* In these cases, we should look for template-ids.
34120 	     For example, if the default argument is
34121 	     `X<int, double>()', we need to do name lookup to
34122 	     figure out whether or not `X' is a template; if
34123 	     so, the `,' does not end the default argument.
34124 
34125 	     That is not yet done.  */
34126 	  break;
34127 
34128 	default:
34129 	  break;
34130 	}
34131 
34132       /* If we've reached the end, stop.  */
34133       if (done)
34134 	break;
34135 
34136       /* Add the token to the token block.  */
34137       token = cp_lexer_consume_token (parser->lexer);
34138     }
34139 
34140   /* Create a DEFERRED_PARSE to represent the unparsed default
34141      argument.  */
34142   default_argument = make_node (DEFERRED_PARSE);
34143   DEFPARSE_TOKENS (default_argument)
34144     = cp_token_cache_new (first_token, token);
34145   DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
34146 
34147   return default_argument;
34148 }
34149 
34150 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE.  */
34151 
34152 location_t
defparse_location(tree default_argument)34153 defparse_location (tree default_argument)
34154 {
34155   cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
34156   location_t start = tokens->first->location;
34157   location_t end = tokens->last->location;
34158   return make_location (start, start, end);
34159 }
34160 
34161 /* Begin parsing tentatively.  We always save tokens while parsing
34162    tentatively so that if the tentative parsing fails we can restore the
34163    tokens.  */
34164 
34165 static void
cp_parser_parse_tentatively(cp_parser * parser)34166 cp_parser_parse_tentatively (cp_parser* parser)
34167 {
34168   /* Enter a new parsing context.  */
34169   parser->context = cp_parser_context_new (parser->context);
34170   /* Begin saving tokens.  */
34171   cp_lexer_save_tokens (parser->lexer);
34172   /* In order to avoid repetitive access control error messages,
34173      access checks are queued up until we are no longer parsing
34174      tentatively.  */
34175   push_deferring_access_checks (dk_deferred);
34176 }
34177 
34178 /* Commit to the currently active tentative parse.  */
34179 
34180 static void
cp_parser_commit_to_tentative_parse(cp_parser * parser)34181 cp_parser_commit_to_tentative_parse (cp_parser* parser)
34182 {
34183   cp_parser_context *context;
34184   cp_lexer *lexer;
34185 
34186   /* Mark all of the levels as committed.  */
34187   lexer = parser->lexer;
34188   for (context = parser->context; context->next; context = context->next)
34189     {
34190       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34191 	break;
34192       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34193       while (!cp_lexer_saving_tokens (lexer))
34194 	lexer = lexer->next;
34195       cp_lexer_commit_tokens (lexer);
34196     }
34197 }
34198 
34199 /* Commit to the topmost currently active tentative parse.
34200 
34201    Note that this function shouldn't be called when there are
34202    irreversible side-effects while in a tentative state.  For
34203    example, we shouldn't create a permanent entry in the symbol
34204    table, or issue an error message that might not apply if the
34205    tentative parse is aborted.  */
34206 
34207 static void
cp_parser_commit_to_topmost_tentative_parse(cp_parser * parser)34208 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
34209 {
34210   cp_parser_context *context = parser->context;
34211   cp_lexer *lexer = parser->lexer;
34212 
34213   if (context)
34214     {
34215       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34216 	return;
34217       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34218 
34219       while (!cp_lexer_saving_tokens (lexer))
34220 	lexer = lexer->next;
34221       cp_lexer_commit_tokens (lexer);
34222     }
34223 }
34224 
34225 /* Abort the currently active tentative parse.  All consumed tokens
34226    will be rolled back, and no diagnostics will be issued.  */
34227 
34228 static void
cp_parser_abort_tentative_parse(cp_parser * parser)34229 cp_parser_abort_tentative_parse (cp_parser* parser)
34230 {
34231   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
34232 	      || errorcount > 0);
34233   cp_parser_simulate_error (parser);
34234   /* Now, pretend that we want to see if the construct was
34235      successfully parsed.  */
34236   cp_parser_parse_definitely (parser);
34237 }
34238 
34239 /* Stop parsing tentatively.  If a parse error has occurred, restore the
34240    token stream.  Otherwise, commit to the tokens we have consumed.
34241    Returns true if no error occurred; false otherwise.  */
34242 
34243 static bool
cp_parser_parse_definitely(cp_parser * parser)34244 cp_parser_parse_definitely (cp_parser* parser)
34245 {
34246   bool error_occurred;
34247   cp_parser_context *context;
34248 
34249   /* Remember whether or not an error occurred, since we are about to
34250      destroy that information.  */
34251   error_occurred = cp_parser_error_occurred (parser);
34252   /* Remove the topmost context from the stack.  */
34253   context = parser->context;
34254   parser->context = context->next;
34255   /* If no parse errors occurred, commit to the tentative parse.  */
34256   if (!error_occurred)
34257     {
34258       /* Commit to the tokens read tentatively, unless that was
34259 	 already done.  */
34260       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
34261 	cp_lexer_commit_tokens (parser->lexer);
34262 
34263       pop_to_parent_deferring_access_checks ();
34264     }
34265   /* Otherwise, if errors occurred, roll back our state so that things
34266      are just as they were before we began the tentative parse.  */
34267   else
34268     {
34269       cp_lexer_rollback_tokens (parser->lexer);
34270       pop_deferring_access_checks ();
34271     }
34272   /* Add the context to the front of the free list.  */
34273   context->next = cp_parser_context_free_list;
34274   cp_parser_context_free_list = context;
34275 
34276   return !error_occurred;
34277 }
34278 
34279 /* Returns true if we are parsing tentatively and are not committed to
34280    this tentative parse.  */
34281 
34282 static bool
cp_parser_uncommitted_to_tentative_parse_p(cp_parser * parser)34283 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
34284 {
34285   return (cp_parser_parsing_tentatively (parser)
34286 	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
34287 }
34288 
34289 /* Returns nonzero iff an error has occurred during the most recent
34290    tentative parse.  */
34291 
34292 static bool
cp_parser_error_occurred(cp_parser * parser)34293 cp_parser_error_occurred (cp_parser* parser)
34294 {
34295   return (cp_parser_parsing_tentatively (parser)
34296 	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
34297 }
34298 
34299 /* Returns nonzero if GNU extensions are allowed.  */
34300 
34301 static bool
cp_parser_allow_gnu_extensions_p(cp_parser * parser)34302 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
34303 {
34304   return parser->allow_gnu_extensions_p;
34305 }
34306 
34307 /* Objective-C++ Productions */
34308 
34309 
34310 /* Parse an Objective-C expression, which feeds into a primary-expression
34311    above.
34312 
34313    objc-expression:
34314      objc-message-expression
34315      objc-string-literal
34316      objc-encode-expression
34317      objc-protocol-expression
34318      objc-selector-expression
34319 
34320   Returns a tree representation of the expression.  */
34321 
34322 static cp_expr
cp_parser_objc_expression(cp_parser * parser)34323 cp_parser_objc_expression (cp_parser* parser)
34324 {
34325   /* Try to figure out what kind of declaration is present.  */
34326   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34327 
34328   switch (kwd->type)
34329     {
34330     case CPP_OPEN_SQUARE:
34331       return cp_parser_objc_message_expression (parser);
34332 
34333     case CPP_OBJC_STRING:
34334       kwd = cp_lexer_consume_token (parser->lexer);
34335       return objc_build_string_object (kwd->u.value);
34336 
34337     case CPP_KEYWORD:
34338       switch (kwd->keyword)
34339 	{
34340 	case RID_AT_ENCODE:
34341 	  return cp_parser_objc_encode_expression (parser);
34342 
34343 	case RID_AT_PROTOCOL:
34344 	  return cp_parser_objc_protocol_expression (parser);
34345 
34346 	case RID_AT_SELECTOR:
34347 	  return cp_parser_objc_selector_expression (parser);
34348 
34349 	default:
34350 	  break;
34351 	}
34352       /* FALLTHRU */
34353     default:
34354       error_at (kwd->location,
34355 		"misplaced %<@%D%> Objective-C++ construct",
34356 		kwd->u.value);
34357       cp_parser_skip_to_end_of_block_or_statement (parser);
34358     }
34359 
34360   return error_mark_node;
34361 }
34362 
34363 /* Parse an Objective-C message expression.
34364 
34365    objc-message-expression:
34366      [ objc-message-receiver objc-message-args ]
34367 
34368    Returns a representation of an Objective-C message.  */
34369 
34370 static tree
cp_parser_objc_message_expression(cp_parser * parser)34371 cp_parser_objc_message_expression (cp_parser* parser)
34372 {
34373   tree receiver, messageargs;
34374 
34375   parser->objective_c_message_context_p = true;
34376   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34377   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
34378   receiver = cp_parser_objc_message_receiver (parser);
34379   messageargs = cp_parser_objc_message_args (parser);
34380   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
34381   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
34382 
34383   tree result = objc_build_message_expr (receiver, messageargs);
34384 
34385   /* Construct a location e.g.
34386        [self func1:5]
34387        ^~~~~~~~~~~~~~
34388      ranging from the '[' to the ']', with the caret at the start.  */
34389   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
34390   protected_set_expr_location (result, combined_loc);
34391 
34392   parser->objective_c_message_context_p = false;
34393   return result;
34394 }
34395 
34396 /* Parse an objc-message-receiver.
34397 
34398    objc-message-receiver:
34399      expression
34400      simple-type-specifier
34401 
34402   Returns a representation of the type or expression.  */
34403 
34404 static tree
cp_parser_objc_message_receiver(cp_parser * parser)34405 cp_parser_objc_message_receiver (cp_parser* parser)
34406 {
34407   tree rcv;
34408 
34409   /* An Objective-C message receiver may be either (1) a type
34410      or (2) an expression.  */
34411   cp_parser_parse_tentatively (parser);
34412   rcv = cp_parser_expression (parser);
34413 
34414   /* If that worked out, fine.  */
34415   if (cp_parser_parse_definitely (parser))
34416     return rcv;
34417 
34418   cp_parser_parse_tentatively (parser);
34419   rcv = cp_parser_simple_type_specifier (parser,
34420 					 /*decl_specs=*/NULL,
34421 					 CP_PARSER_FLAGS_NONE);
34422 
34423   if (cp_parser_parse_definitely (parser))
34424     return objc_get_class_reference (rcv);
34425 
34426   cp_parser_error (parser, "objective-c++ message receiver expected");
34427   return error_mark_node;
34428 }
34429 
34430 /* Parse the arguments and selectors comprising an Objective-C message.
34431 
34432    objc-message-args:
34433      objc-selector
34434      objc-selector-args
34435      objc-selector-args , objc-comma-args
34436 
34437    objc-selector-args:
34438      objc-selector [opt] : assignment-expression
34439      objc-selector-args objc-selector [opt] : assignment-expression
34440 
34441    objc-comma-args:
34442      assignment-expression
34443      objc-comma-args , assignment-expression
34444 
34445    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34446    selector arguments and TREE_VALUE containing a list of comma
34447    arguments.  */
34448 
34449 static tree
cp_parser_objc_message_args(cp_parser * parser)34450 cp_parser_objc_message_args (cp_parser* parser)
34451 {
34452   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
34453   bool maybe_unary_selector_p = true;
34454   cp_token *token = cp_lexer_peek_token (parser->lexer);
34455 
34456   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34457     {
34458       tree selector = NULL_TREE, arg;
34459 
34460       if (token->type != CPP_COLON)
34461 	selector = cp_parser_objc_selector (parser);
34462 
34463       /* Detect if we have a unary selector.  */
34464       if (maybe_unary_selector_p
34465 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34466 	return build_tree_list (selector, NULL_TREE);
34467 
34468       maybe_unary_selector_p = false;
34469       cp_parser_require (parser, CPP_COLON, RT_COLON);
34470       arg = cp_parser_assignment_expression (parser);
34471 
34472       sel_args
34473 	= chainon (sel_args,
34474 		   build_tree_list (selector, arg));
34475 
34476       token = cp_lexer_peek_token (parser->lexer);
34477     }
34478 
34479   /* Handle non-selector arguments, if any. */
34480   while (token->type == CPP_COMMA)
34481     {
34482       tree arg;
34483 
34484       cp_lexer_consume_token (parser->lexer);
34485       arg = cp_parser_assignment_expression (parser);
34486 
34487       addl_args
34488 	= chainon (addl_args,
34489 		   build_tree_list (NULL_TREE, arg));
34490 
34491       token = cp_lexer_peek_token (parser->lexer);
34492     }
34493 
34494   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
34495     {
34496       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
34497       return build_tree_list (error_mark_node, error_mark_node);
34498     }
34499 
34500   return build_tree_list (sel_args, addl_args);
34501 }
34502 
34503 /* Parse an Objective-C encode expression.
34504 
34505    objc-encode-expression:
34506      @encode objc-typename
34507 
34508    Returns an encoded representation of the type argument.  */
34509 
34510 static cp_expr
cp_parser_objc_encode_expression(cp_parser * parser)34511 cp_parser_objc_encode_expression (cp_parser* parser)
34512 {
34513   tree type;
34514   cp_token *token;
34515   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34516 
34517   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
34518   matching_parens parens;
34519   parens.require_open (parser);
34520   token = cp_lexer_peek_token (parser->lexer);
34521   type = complete_type (cp_parser_type_id (parser));
34522   parens.require_close (parser);
34523 
34524   if (!type)
34525     {
34526       error_at (token->location,
34527 		"%<@encode%> must specify a type as an argument");
34528       return error_mark_node;
34529     }
34530 
34531   /* This happens if we find @encode(T) (where T is a template
34532      typename or something dependent on a template typename) when
34533      parsing a template.  In that case, we can't compile it
34534      immediately, but we rather create an AT_ENCODE_EXPR which will
34535      need to be instantiated when the template is used.
34536   */
34537   if (dependent_type_p (type))
34538     {
34539       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
34540       TREE_READONLY (value) = 1;
34541       return value;
34542     }
34543 
34544 
34545   /* Build a location of the form:
34546        @encode(int)
34547        ^~~~~~~~~~~~
34548      with caret==start at the @ token, finishing at the close paren.  */
34549   location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34550 
34551   return cp_expr (objc_build_encode_expr (type), combined_loc);
34552 }
34553 
34554 /* Parse an Objective-C @defs expression.  */
34555 
34556 static tree
cp_parser_objc_defs_expression(cp_parser * parser)34557 cp_parser_objc_defs_expression (cp_parser *parser)
34558 {
34559   tree name;
34560 
34561   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
34562   matching_parens parens;
34563   parens.require_open (parser);
34564   name = cp_parser_identifier (parser);
34565   parens.require_close (parser);
34566 
34567   return objc_get_class_ivars (name);
34568 }
34569 
34570 /* Parse an Objective-C protocol expression.
34571 
34572   objc-protocol-expression:
34573     @protocol ( identifier )
34574 
34575   Returns a representation of the protocol expression.  */
34576 
34577 static tree
cp_parser_objc_protocol_expression(cp_parser * parser)34578 cp_parser_objc_protocol_expression (cp_parser* parser)
34579 {
34580   tree proto;
34581   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34582 
34583   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
34584   matching_parens parens;
34585   parens.require_open (parser);
34586   proto = cp_parser_identifier (parser);
34587   parens.require_close (parser);
34588 
34589   /* Build a location of the form:
34590        @protocol(prot)
34591        ^~~~~~~~~~~~~~~
34592      with caret==start at the @ token, finishing at the close paren.  */
34593   location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34594   tree result = objc_build_protocol_expr (proto);
34595   protected_set_expr_location (result, combined_loc);
34596   return result;
34597 }
34598 
34599 /* Parse an Objective-C selector expression.
34600 
34601    objc-selector-expression:
34602      @selector ( objc-method-signature )
34603 
34604    objc-method-signature:
34605      objc-selector
34606      objc-selector-seq
34607 
34608    objc-selector-seq:
34609      objc-selector :
34610      objc-selector-seq objc-selector :
34611 
34612   Returns a representation of the method selector.  */
34613 
34614 static tree
cp_parser_objc_selector_expression(cp_parser * parser)34615 cp_parser_objc_selector_expression (cp_parser* parser)
34616 {
34617   tree sel_seq = NULL_TREE;
34618   bool maybe_unary_selector_p = true;
34619   cp_token *token;
34620   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34621 
34622   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
34623   matching_parens parens;
34624   parens.require_open (parser);
34625   token = cp_lexer_peek_token (parser->lexer);
34626 
34627   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
34628 	 || token->type == CPP_SCOPE)
34629     {
34630       tree selector = NULL_TREE;
34631 
34632       if (token->type != CPP_COLON
34633 	  || token->type == CPP_SCOPE)
34634 	selector = cp_parser_objc_selector (parser);
34635 
34636       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
34637 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
34638 	{
34639 	  /* Detect if we have a unary selector.  */
34640 	  if (maybe_unary_selector_p)
34641 	    {
34642 	      sel_seq = selector;
34643 	      goto finish_selector;
34644 	    }
34645 	  else
34646 	    {
34647 	      cp_parser_error (parser, "expected %<:%>");
34648 	    }
34649 	}
34650       maybe_unary_selector_p = false;
34651       token = cp_lexer_consume_token (parser->lexer);
34652 
34653       if (token->type == CPP_SCOPE)
34654 	{
34655 	  sel_seq
34656 	    = chainon (sel_seq,
34657 		       build_tree_list (selector, NULL_TREE));
34658 	  sel_seq
34659 	    = chainon (sel_seq,
34660 		       build_tree_list (NULL_TREE, NULL_TREE));
34661 	}
34662       else
34663 	sel_seq
34664 	  = chainon (sel_seq,
34665 		     build_tree_list (selector, NULL_TREE));
34666 
34667       token = cp_lexer_peek_token (parser->lexer);
34668     }
34669 
34670  finish_selector:
34671   parens.require_close (parser);
34672 
34673 
34674   /* Build a location of the form:
34675        @selector(func)
34676        ^~~~~~~~~~~~~~~
34677      with caret==start at the @ token, finishing at the close paren.  */
34678   location_t combined_loc = make_location (loc, loc, parser->lexer);
34679   tree result = objc_build_selector_expr (combined_loc, sel_seq);
34680   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
34681   protected_set_expr_location (result, combined_loc);
34682   return result;
34683 }
34684 
34685 /* Parse a list of identifiers.
34686 
34687    objc-identifier-list:
34688      identifier
34689      objc-identifier-list , identifier
34690 
34691    Returns a TREE_LIST of identifier nodes.  */
34692 
34693 static tree
cp_parser_objc_identifier_list(cp_parser * parser)34694 cp_parser_objc_identifier_list (cp_parser* parser)
34695 {
34696   tree identifier;
34697   tree list;
34698   cp_token *sep;
34699 
34700   identifier = cp_parser_identifier (parser);
34701   if (identifier == error_mark_node)
34702     return error_mark_node;
34703 
34704   list = build_tree_list (NULL_TREE, identifier);
34705   sep = cp_lexer_peek_token (parser->lexer);
34706 
34707   while (sep->type == CPP_COMMA)
34708     {
34709       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
34710       identifier = cp_parser_identifier (parser);
34711       if (identifier == error_mark_node)
34712 	return list;
34713 
34714       list = chainon (list, build_tree_list (NULL_TREE,
34715 					     identifier));
34716       sep = cp_lexer_peek_token (parser->lexer);
34717     }
34718 
34719   return list;
34720 }
34721 
34722 /* Parse an Objective-C alias declaration.
34723 
34724    objc-alias-declaration:
34725      @compatibility_alias identifier identifier ;
34726 
34727    This function registers the alias mapping with the Objective-C front end.
34728    It returns nothing.  */
34729 
34730 static void
cp_parser_objc_alias_declaration(cp_parser * parser)34731 cp_parser_objc_alias_declaration (cp_parser* parser)
34732 {
34733   tree alias, orig;
34734 
34735   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
34736   alias = cp_parser_identifier (parser);
34737   orig = cp_parser_identifier (parser);
34738   objc_declare_alias (alias, orig);
34739   cp_parser_consume_semicolon_at_end_of_statement (parser);
34740 }
34741 
34742 /* Parse an Objective-C class forward-declaration.
34743 
34744    objc-class-declaration:
34745      @class objc-identifier-list ;
34746 
34747    The function registers the forward declarations with the Objective-C
34748    front end.  It returns nothing.  */
34749 
34750 static void
cp_parser_objc_class_declaration(cp_parser * parser)34751 cp_parser_objc_class_declaration (cp_parser* parser)
34752 {
34753   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
34754   while (true)
34755     {
34756       tree id;
34757 
34758       id = cp_parser_identifier (parser);
34759       if (id == error_mark_node)
34760 	break;
34761 
34762       objc_declare_class (id);
34763 
34764       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34765 	cp_lexer_consume_token (parser->lexer);
34766       else
34767 	break;
34768     }
34769   cp_parser_consume_semicolon_at_end_of_statement (parser);
34770 }
34771 
34772 /* Parse a list of Objective-C protocol references.
34773 
34774    objc-protocol-refs-opt:
34775      objc-protocol-refs [opt]
34776 
34777    objc-protocol-refs:
34778      < objc-identifier-list >
34779 
34780    Returns a TREE_LIST of identifiers, if any.  */
34781 
34782 static tree
cp_parser_objc_protocol_refs_opt(cp_parser * parser)34783 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
34784 {
34785   tree protorefs = NULL_TREE;
34786 
34787   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
34788     {
34789       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
34790       protorefs = cp_parser_objc_identifier_list (parser);
34791       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
34792     }
34793 
34794   return protorefs;
34795 }
34796 
34797 /* Parse a Objective-C visibility specification.  */
34798 
34799 static void
cp_parser_objc_visibility_spec(cp_parser * parser)34800 cp_parser_objc_visibility_spec (cp_parser* parser)
34801 {
34802   cp_token *vis = cp_lexer_peek_token (parser->lexer);
34803 
34804   switch (vis->keyword)
34805     {
34806     case RID_AT_PRIVATE:
34807       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
34808       break;
34809     case RID_AT_PROTECTED:
34810       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
34811       break;
34812     case RID_AT_PUBLIC:
34813       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
34814       break;
34815     case RID_AT_PACKAGE:
34816       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
34817       break;
34818     default:
34819       return;
34820     }
34821 
34822   /* Eat '@private'/'@protected'/'@public'.  */
34823   cp_lexer_consume_token (parser->lexer);
34824 }
34825 
34826 /* Parse an Objective-C method type.  Return 'true' if it is a class
34827    (+) method, and 'false' if it is an instance (-) method.  */
34828 
34829 static inline bool
cp_parser_objc_method_type(cp_parser * parser)34830 cp_parser_objc_method_type (cp_parser* parser)
34831 {
34832   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
34833     return true;
34834   else
34835     return false;
34836 }
34837 
34838 /* Parse an Objective-C protocol qualifier.  */
34839 
34840 static tree
cp_parser_objc_protocol_qualifiers(cp_parser * parser)34841 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
34842 {
34843   tree quals = NULL_TREE, node;
34844   cp_token *token = cp_lexer_peek_token (parser->lexer);
34845 
34846   node = token->u.value;
34847 
34848   while (node && identifier_p (node)
34849 	 && (node == ridpointers [(int) RID_IN]
34850 	     || node == ridpointers [(int) RID_OUT]
34851 	     || node == ridpointers [(int) RID_INOUT]
34852 	     || node == ridpointers [(int) RID_BYCOPY]
34853 	     || node == ridpointers [(int) RID_BYREF]
34854 	     || node == ridpointers [(int) RID_ONEWAY]))
34855     {
34856       quals = tree_cons (NULL_TREE, node, quals);
34857       cp_lexer_consume_token (parser->lexer);
34858       token = cp_lexer_peek_token (parser->lexer);
34859       node = token->u.value;
34860     }
34861 
34862   return quals;
34863 }
34864 
34865 /* Parse an Objective-C typename.  */
34866 
34867 static tree
cp_parser_objc_typename(cp_parser * parser)34868 cp_parser_objc_typename (cp_parser* parser)
34869 {
34870   tree type_name = NULL_TREE;
34871 
34872   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34873     {
34874       tree proto_quals, cp_type = NULL_TREE;
34875 
34876       matching_parens parens;
34877       parens.consume_open (parser); /* Eat '('.  */
34878       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
34879 
34880       /* An ObjC type name may consist of just protocol qualifiers, in which
34881 	 case the type shall default to 'id'.  */
34882       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34883 	{
34884 	  cp_type = cp_parser_type_id (parser);
34885 
34886 	  /* If the type could not be parsed, an error has already
34887 	     been produced.  For error recovery, behave as if it had
34888 	     not been specified, which will use the default type
34889 	     'id'.  */
34890 	  if (cp_type == error_mark_node)
34891 	    {
34892 	      cp_type = NULL_TREE;
34893 	      /* We need to skip to the closing parenthesis as
34894 		 cp_parser_type_id() does not seem to do it for
34895 		 us.  */
34896 	      cp_parser_skip_to_closing_parenthesis (parser,
34897 						     /*recovering=*/true,
34898 						     /*or_comma=*/false,
34899 						     /*consume_paren=*/false);
34900 	    }
34901 	}
34902 
34903       parens.require_close (parser);
34904       type_name = build_tree_list (proto_quals, cp_type);
34905     }
34906 
34907   return type_name;
34908 }
34909 
34910 /* Check to see if TYPE refers to an Objective-C selector name.  */
34911 
34912 static bool
cp_parser_objc_selector_p(enum cpp_ttype type)34913 cp_parser_objc_selector_p (enum cpp_ttype type)
34914 {
34915   return (type == CPP_NAME || type == CPP_KEYWORD
34916 	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
34917 	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
34918 	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
34919 	  || type == CPP_XOR || type == CPP_XOR_EQ);
34920 }
34921 
34922 /* Parse an Objective-C selector.  */
34923 
34924 static tree
cp_parser_objc_selector(cp_parser * parser)34925 cp_parser_objc_selector (cp_parser* parser)
34926 {
34927   cp_token *token = cp_lexer_consume_token (parser->lexer);
34928 
34929   if (!cp_parser_objc_selector_p (token->type))
34930     {
34931       error_at (token->location, "invalid Objective-C++ selector name");
34932       return error_mark_node;
34933     }
34934 
34935   /* C++ operator names are allowed to appear in ObjC selectors.  */
34936   switch (token->type)
34937     {
34938     case CPP_AND_AND: return get_identifier ("and");
34939     case CPP_AND_EQ: return get_identifier ("and_eq");
34940     case CPP_AND: return get_identifier ("bitand");
34941     case CPP_OR: return get_identifier ("bitor");
34942     case CPP_COMPL: return get_identifier ("compl");
34943     case CPP_NOT: return get_identifier ("not");
34944     case CPP_NOT_EQ: return get_identifier ("not_eq");
34945     case CPP_OR_OR: return get_identifier ("or");
34946     case CPP_OR_EQ: return get_identifier ("or_eq");
34947     case CPP_XOR: return get_identifier ("xor");
34948     case CPP_XOR_EQ: return get_identifier ("xor_eq");
34949     default: return token->u.value;
34950     }
34951 }
34952 
34953 /* Parse an Objective-C params list.  */
34954 
34955 static tree
cp_parser_objc_method_keyword_params(cp_parser * parser,tree * attributes)34956 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
34957 {
34958   tree params = NULL_TREE;
34959   bool maybe_unary_selector_p = true;
34960   cp_token *token = cp_lexer_peek_token (parser->lexer);
34961 
34962   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34963     {
34964       tree selector = NULL_TREE, type_name, identifier;
34965       tree parm_attr = NULL_TREE;
34966 
34967       if (token->keyword == RID_ATTRIBUTE)
34968 	break;
34969 
34970       if (token->type != CPP_COLON)
34971 	selector = cp_parser_objc_selector (parser);
34972 
34973       /* Detect if we have a unary selector.  */
34974       if (maybe_unary_selector_p
34975 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34976 	{
34977 	  params = selector; /* Might be followed by attributes.  */
34978 	  break;
34979 	}
34980 
34981       maybe_unary_selector_p = false;
34982       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34983 	{
34984 	  /* Something went quite wrong.  There should be a colon
34985 	     here, but there is not.  Stop parsing parameters.  */
34986 	  break;
34987 	}
34988       type_name = cp_parser_objc_typename (parser);
34989       /* New ObjC allows attributes on parameters too.  */
34990       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
34991 	parm_attr = cp_parser_attributes_opt (parser);
34992       identifier = cp_parser_identifier (parser);
34993 
34994       params
34995 	= chainon (params,
34996 		   objc_build_keyword_decl (selector,
34997 					    type_name,
34998 					    identifier,
34999 					    parm_attr));
35000 
35001       token = cp_lexer_peek_token (parser->lexer);
35002     }
35003 
35004   if (params == NULL_TREE)
35005     {
35006       cp_parser_error (parser, "objective-c++ method declaration is expected");
35007       return error_mark_node;
35008     }
35009 
35010   /* We allow tail attributes for the method.  */
35011   if (token->keyword == RID_ATTRIBUTE)
35012     {
35013       *attributes = cp_parser_attributes_opt (parser);
35014       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35015 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35016 	return params;
35017       cp_parser_error (parser,
35018 		       "method attributes must be specified at the end");
35019       return error_mark_node;
35020     }
35021 
35022   if (params == NULL_TREE)
35023     {
35024       cp_parser_error (parser, "objective-c++ method declaration is expected");
35025       return error_mark_node;
35026     }
35027   return params;
35028 }
35029 
35030 /* Parse the non-keyword Objective-C params.  */
35031 
35032 static tree
cp_parser_objc_method_tail_params_opt(cp_parser * parser,bool * ellipsisp,tree * attributes)35033 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
35034 				       tree* attributes)
35035 {
35036   tree params = make_node (TREE_LIST);
35037   cp_token *token = cp_lexer_peek_token (parser->lexer);
35038   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
35039 
35040   while (token->type == CPP_COMMA)
35041     {
35042       cp_parameter_declarator *parmdecl;
35043       tree parm;
35044 
35045       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
35046       token = cp_lexer_peek_token (parser->lexer);
35047 
35048       if (token->type == CPP_ELLIPSIS)
35049 	{
35050 	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
35051 	  *ellipsisp = true;
35052 	  token = cp_lexer_peek_token (parser->lexer);
35053 	  break;
35054 	}
35055 
35056       /* TODO: parse attributes for tail parameters.  */
35057       parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35058 						  false, NULL);
35059       parm = grokdeclarator (parmdecl->declarator,
35060 			     &parmdecl->decl_specifiers,
35061 			     PARM, /*initialized=*/0,
35062 			     /*attrlist=*/NULL);
35063 
35064       chainon (params, build_tree_list (NULL_TREE, parm));
35065       token = cp_lexer_peek_token (parser->lexer);
35066     }
35067 
35068   /* We allow tail attributes for the method.  */
35069   if (token->keyword == RID_ATTRIBUTE)
35070     {
35071       if (*attributes == NULL_TREE)
35072 	{
35073 	  *attributes = cp_parser_attributes_opt (parser);
35074 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35075 	      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35076 	    return params;
35077 	}
35078       else
35079 	/* We have an error, but parse the attributes, so that we can
35080 	   carry on.  */
35081 	*attributes = cp_parser_attributes_opt (parser);
35082 
35083       cp_parser_error (parser,
35084 		       "method attributes must be specified at the end");
35085       return error_mark_node;
35086     }
35087 
35088   return params;
35089 }
35090 
35091 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
35092 
35093 static void
cp_parser_objc_interstitial_code(cp_parser * parser)35094 cp_parser_objc_interstitial_code (cp_parser* parser)
35095 {
35096   cp_token *token = cp_lexer_peek_token (parser->lexer);
35097 
35098   /* If the next token is `extern' and the following token is a string
35099      literal, then we have a linkage specification.  */
35100   if (token->keyword == RID_EXTERN
35101       && cp_parser_is_pure_string_literal
35102 	 (cp_lexer_peek_nth_token (parser->lexer, 2)))
35103     cp_parser_linkage_specification (parser, NULL_TREE);
35104   /* Handle #pragma, if any.  */
35105   else if (token->type == CPP_PRAGMA)
35106     cp_parser_pragma (parser, pragma_objc_icode, NULL);
35107   /* Allow stray semicolons.  */
35108   else if (token->type == CPP_SEMICOLON)
35109     cp_lexer_consume_token (parser->lexer);
35110   /* Mark methods as optional or required, when building protocols.  */
35111   else if (token->keyword == RID_AT_OPTIONAL)
35112     {
35113       cp_lexer_consume_token (parser->lexer);
35114       objc_set_method_opt (true);
35115     }
35116   else if (token->keyword == RID_AT_REQUIRED)
35117     {
35118       cp_lexer_consume_token (parser->lexer);
35119       objc_set_method_opt (false);
35120     }
35121   else if (token->keyword == RID_NAMESPACE)
35122     cp_parser_namespace_definition (parser);
35123   /* Other stray characters must generate errors.  */
35124   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
35125     {
35126       cp_lexer_consume_token (parser->lexer);
35127       error ("stray %qs between Objective-C++ methods",
35128 	     token->type == CPP_OPEN_BRACE ? "{" : "}");
35129     }
35130   /* Finally, try to parse a block-declaration, or a function-definition.  */
35131   else
35132     cp_parser_block_declaration (parser, /*statement_p=*/false);
35133 }
35134 
35135 /* Parse a method signature.  */
35136 
35137 static tree
cp_parser_objc_method_signature(cp_parser * parser,tree * attributes)35138 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
35139 {
35140   tree rettype, kwdparms, optparms;
35141   bool ellipsis = false;
35142   bool is_class_method;
35143 
35144   is_class_method = cp_parser_objc_method_type (parser);
35145   rettype = cp_parser_objc_typename (parser);
35146   *attributes = NULL_TREE;
35147   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
35148   if (kwdparms == error_mark_node)
35149     return error_mark_node;
35150   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
35151   if (optparms == error_mark_node)
35152     return error_mark_node;
35153 
35154   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
35155 }
35156 
35157 static bool
cp_parser_objc_method_maybe_bad_prefix_attributes(cp_parser * parser)35158 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
35159 {
35160   tree tattr;
35161   cp_lexer_save_tokens (parser->lexer);
35162   tattr = cp_parser_attributes_opt (parser);
35163   gcc_assert (tattr) ;
35164 
35165   /* If the attributes are followed by a method introducer, this is not allowed.
35166      Dump the attributes and flag the situation.  */
35167   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
35168       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35169     return true;
35170 
35171   /* Otherwise, the attributes introduce some interstitial code, possibly so
35172      rewind to allow that check.  */
35173   cp_lexer_rollback_tokens (parser->lexer);
35174   return false;
35175 }
35176 
35177 /* Parse an Objective-C method prototype list.  */
35178 
35179 static void
cp_parser_objc_method_prototype_list(cp_parser * parser)35180 cp_parser_objc_method_prototype_list (cp_parser* parser)
35181 {
35182   cp_token *token = cp_lexer_peek_token (parser->lexer);
35183 
35184   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
35185     {
35186       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35187 	{
35188 	  tree attributes, sig;
35189 	  bool is_class_method;
35190 	  if (token->type == CPP_PLUS)
35191 	    is_class_method = true;
35192 	  else
35193 	    is_class_method = false;
35194 	  sig = cp_parser_objc_method_signature (parser, &attributes);
35195 	  if (sig == error_mark_node)
35196 	    {
35197 	      cp_parser_skip_to_end_of_block_or_statement (parser);
35198 	      token = cp_lexer_peek_token (parser->lexer);
35199 	      continue;
35200 	    }
35201 	  objc_add_method_declaration (is_class_method, sig, attributes);
35202 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
35203 	}
35204       else if (token->keyword == RID_AT_PROPERTY)
35205 	cp_parser_objc_at_property_declaration (parser);
35206       else if (token->keyword == RID_ATTRIBUTE
35207       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35208 	warning_at (cp_lexer_peek_token (parser->lexer)->location,
35209 		    OPT_Wattributes,
35210 		    "prefix attributes are ignored for methods");
35211       else
35212 	/* Allow for interspersed non-ObjC++ code.  */
35213 	cp_parser_objc_interstitial_code (parser);
35214 
35215       token = cp_lexer_peek_token (parser->lexer);
35216     }
35217 
35218   if (token->type != CPP_EOF)
35219     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
35220   else
35221     cp_parser_error (parser, "expected %<@end%>");
35222 
35223   objc_finish_interface ();
35224 }
35225 
35226 /* Parse an Objective-C method definition list.  */
35227 
35228 static void
cp_parser_objc_method_definition_list(cp_parser * parser)35229 cp_parser_objc_method_definition_list (cp_parser* parser)
35230 {
35231   for (;;)
35232     {
35233       cp_token *token = cp_lexer_peek_token (parser->lexer);
35234 
35235       if (token->keyword == RID_AT_END)
35236 	{
35237 	  cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
35238 	  break;
35239 	}
35240       else if (token->type == CPP_EOF)
35241 	{
35242 	  cp_parser_error (parser, "expected %<@end%>");
35243 	  break;
35244 	}
35245       else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35246 	{
35247 	  bool is_class_method = token->type == CPP_PLUS;
35248 
35249 	  push_deferring_access_checks (dk_deferred);
35250 	  tree attribute;
35251 	  tree sig = cp_parser_objc_method_signature (parser, &attribute);
35252 	  if (sig == error_mark_node)
35253 	    cp_parser_skip_to_end_of_block_or_statement (parser);
35254 	  else
35255 	    {
35256 	      objc_start_method_definition (is_class_method, sig,
35257 					    attribute, NULL_TREE);
35258 
35259 	      /* For historical reasons, we accept an optional semicolon.  */
35260 	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35261 		cp_lexer_consume_token (parser->lexer);
35262 
35263 	      perform_deferred_access_checks (tf_warning_or_error);
35264 	      stop_deferring_access_checks ();
35265 	      tree meth
35266 		= cp_parser_function_definition_after_declarator (parser, false);
35267 	      pop_deferring_access_checks ();
35268 	      objc_finish_method_definition (meth);
35269 	    }
35270 	}
35271       /* The following case will be removed once @synthesize is
35272 	 completely implemented.  */
35273       else if (token->keyword == RID_AT_PROPERTY)
35274 	cp_parser_objc_at_property_declaration (parser);
35275       else if (token->keyword == RID_AT_SYNTHESIZE)
35276 	cp_parser_objc_at_synthesize_declaration (parser);
35277       else if (token->keyword == RID_AT_DYNAMIC)
35278 	cp_parser_objc_at_dynamic_declaration (parser);
35279       else if (token->keyword == RID_ATTRIBUTE
35280       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35281 	warning_at (token->location, OPT_Wattributes,
35282 	       	    "prefix attributes are ignored for methods");
35283       else
35284 	/* Allow for interspersed non-ObjC++ code.  */
35285 	cp_parser_objc_interstitial_code (parser);
35286     }
35287 
35288   objc_finish_implementation ();
35289 }
35290 
35291 /* Parse Objective-C ivars.  */
35292 
35293 static void
cp_parser_objc_class_ivars(cp_parser * parser)35294 cp_parser_objc_class_ivars (cp_parser* parser)
35295 {
35296   cp_token *token = cp_lexer_peek_token (parser->lexer);
35297 
35298   if (token->type != CPP_OPEN_BRACE)
35299     return;	/* No ivars specified.  */
35300 
35301   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
35302   token = cp_lexer_peek_token (parser->lexer);
35303 
35304   while (token->type != CPP_CLOSE_BRACE
35305 	&& token->keyword != RID_AT_END && token->type != CPP_EOF)
35306     {
35307       cp_decl_specifier_seq declspecs;
35308       int decl_class_or_enum_p;
35309       tree prefix_attributes;
35310 
35311       cp_parser_objc_visibility_spec (parser);
35312 
35313       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35314 	break;
35315 
35316       cp_parser_decl_specifier_seq (parser,
35317 				    CP_PARSER_FLAGS_OPTIONAL,
35318 				    &declspecs,
35319 				    &decl_class_or_enum_p);
35320 
35321       /* auto, register, static, extern, mutable.  */
35322       if (declspecs.storage_class != sc_none)
35323 	{
35324 	  cp_parser_error (parser, "invalid type for instance variable");
35325 	  declspecs.storage_class = sc_none;
35326 	}
35327 
35328       /* thread_local.  */
35329       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35330 	{
35331 	  cp_parser_error (parser, "invalid type for instance variable");
35332 	  declspecs.locations[ds_thread] = 0;
35333 	}
35334 
35335       /* typedef.  */
35336       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35337 	{
35338 	  cp_parser_error (parser, "invalid type for instance variable");
35339 	  declspecs.locations[ds_typedef] = 0;
35340 	}
35341 
35342       prefix_attributes = declspecs.attributes;
35343       declspecs.attributes = NULL_TREE;
35344 
35345       /* Keep going until we hit the `;' at the end of the
35346 	 declaration.  */
35347       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35348 	{
35349 	  tree width = NULL_TREE, attributes, first_attribute, decl;
35350 	  cp_declarator *declarator = NULL;
35351 	  int ctor_dtor_or_conv_p;
35352 
35353 	  /* Check for a (possibly unnamed) bitfield declaration.  */
35354 	  token = cp_lexer_peek_token (parser->lexer);
35355 	  if (token->type == CPP_COLON)
35356 	    goto eat_colon;
35357 
35358 	  if (token->type == CPP_NAME
35359 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
35360 		  == CPP_COLON))
35361 	    {
35362 	      /* Get the name of the bitfield.  */
35363 	      declarator = make_id_declarator (NULL_TREE,
35364 					       cp_parser_identifier (parser),
35365 					       sfk_none, token->location);
35366 
35367 	     eat_colon:
35368 	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
35369 	      /* Get the width of the bitfield.  */
35370 	      width
35371 		= cp_parser_constant_expression (parser);
35372 	    }
35373 	  else
35374 	    {
35375 	      /* Parse the declarator.  */
35376 	      declarator
35377 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35378 					CP_PARSER_FLAGS_NONE,
35379 					&ctor_dtor_or_conv_p,
35380 					/*parenthesized_p=*/NULL,
35381 					/*member_p=*/false,
35382 					/*friend_p=*/false,
35383 					/*static_p=*/false);
35384 	    }
35385 
35386 	  /* Look for attributes that apply to the ivar.  */
35387 	  attributes = cp_parser_attributes_opt (parser);
35388 	  /* Remember which attributes are prefix attributes and
35389 	     which are not.  */
35390 	  first_attribute = attributes;
35391 	  /* Combine the attributes.  */
35392 	  attributes = attr_chainon (prefix_attributes, attributes);
35393 
35394 	  if (width)
35395 	    /* Create the bitfield declaration.  */
35396 	    decl = grokbitfield (declarator, &declspecs,
35397 				 width, NULL_TREE, attributes);
35398 	  else
35399 	    decl = grokfield (declarator, &declspecs,
35400 			      NULL_TREE, /*init_const_expr_p=*/false,
35401 			      NULL_TREE, attributes);
35402 
35403 	  /* Add the instance variable.  */
35404 	  if (decl != error_mark_node && decl != NULL_TREE)
35405 	    objc_add_instance_variable (decl);
35406 
35407 	  /* Reset PREFIX_ATTRIBUTES.  */
35408 	  if (attributes != error_mark_node)
35409 	    {
35410 	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
35411 		attributes = TREE_CHAIN (attributes);
35412 	      if (attributes)
35413 		TREE_CHAIN (attributes) = NULL_TREE;
35414 	    }
35415 
35416 	  token = cp_lexer_peek_token (parser->lexer);
35417 
35418 	  if (token->type == CPP_COMMA)
35419 	    {
35420 	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
35421 	      continue;
35422 	    }
35423 	  break;
35424 	}
35425 
35426       cp_parser_consume_semicolon_at_end_of_statement (parser);
35427       token = cp_lexer_peek_token (parser->lexer);
35428     }
35429 
35430   if (token->keyword == RID_AT_END)
35431     cp_parser_error (parser, "expected %<}%>");
35432 
35433   /* Do not consume the RID_AT_END, so it will be read again as terminating
35434      the @interface of @implementation.  */
35435   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
35436     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
35437 
35438   /* For historical reasons, we accept an optional semicolon.  */
35439   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35440     cp_lexer_consume_token (parser->lexer);
35441 }
35442 
35443 /* Parse an Objective-C protocol declaration.  */
35444 
35445 static void
cp_parser_objc_protocol_declaration(cp_parser * parser,tree attributes)35446 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
35447 {
35448   tree proto, protorefs;
35449   cp_token *tok;
35450 
35451   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
35452   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35453     {
35454       tok = cp_lexer_peek_token (parser->lexer);
35455       error_at (tok->location, "identifier expected after %<@protocol%>");
35456       cp_parser_consume_semicolon_at_end_of_statement (parser);
35457       return;
35458     }
35459 
35460   /* See if we have a forward declaration or a definition.  */
35461   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
35462 
35463   /* Try a forward declaration first.  */
35464   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
35465     {
35466       while (true)
35467 	{
35468 	  tree id;
35469 
35470 	  id = cp_parser_identifier (parser);
35471 	  if (id == error_mark_node)
35472 	    break;
35473 
35474 	  objc_declare_protocol (id, attributes);
35475 
35476 	  if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35477 	    cp_lexer_consume_token (parser->lexer);
35478 	  else
35479 	    break;
35480 	}
35481       cp_parser_consume_semicolon_at_end_of_statement (parser);
35482     }
35483 
35484   /* Ok, we got a full-fledged definition (or at least should).  */
35485   else
35486     {
35487       proto = cp_parser_identifier (parser);
35488       protorefs = cp_parser_objc_protocol_refs_opt (parser);
35489       objc_start_protocol (proto, protorefs, attributes);
35490       cp_parser_objc_method_prototype_list (parser);
35491     }
35492 }
35493 
35494 /* Parse an Objective-C superclass or category.  */
35495 
35496 static void
cp_parser_objc_superclass_or_category(cp_parser * parser,bool iface_p,tree * super,tree * categ,bool * is_class_extension)35497 cp_parser_objc_superclass_or_category (cp_parser *parser,
35498 				       bool iface_p,
35499 				       tree *super,
35500 				       tree *categ, bool *is_class_extension)
35501 {
35502   cp_token *next = cp_lexer_peek_token (parser->lexer);
35503 
35504   *super = *categ = NULL_TREE;
35505   *is_class_extension = false;
35506   if (next->type == CPP_COLON)
35507     {
35508       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
35509       *super = cp_parser_identifier (parser);
35510     }
35511   else if (next->type == CPP_OPEN_PAREN)
35512     {
35513       matching_parens parens;
35514       parens.consume_open (parser);  /* Eat '('.  */
35515 
35516       /* If there is no category name, and this is an @interface, we
35517 	 have a class extension.  */
35518       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35519 	{
35520 	  *categ = NULL_TREE;
35521 	  *is_class_extension = true;
35522 	}
35523       else
35524 	*categ = cp_parser_identifier (parser);
35525 
35526       parens.require_close (parser);
35527     }
35528 }
35529 
35530 /* Parse an Objective-C class interface.  */
35531 
35532 static void
cp_parser_objc_class_interface(cp_parser * parser,tree attributes)35533 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
35534 {
35535   tree name, super, categ, protos;
35536   bool is_class_extension;
35537 
35538   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
35539   location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
35540   name = cp_parser_identifier (parser);
35541   if (name == error_mark_node)
35542     {
35543       /* It's hard to recover because even if valid @interface stuff
35544 	 is to follow, we can't compile it (or validate it) if we
35545 	 don't even know which class it refers to.  Let's assume this
35546 	 was a stray '@interface' token in the stream and skip it.
35547       */
35548       return;
35549     }
35550   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
35551 					 &is_class_extension);
35552   protos = cp_parser_objc_protocol_refs_opt (parser);
35553 
35554   /* We have either a class or a category on our hands.  */
35555   if (categ || is_class_extension)
35556     objc_start_category_interface (name, categ, protos, attributes);
35557   else
35558     {
35559       objc_start_class_interface (name, nam_loc, super, protos, attributes);
35560       /* Handle instance variable declarations, if any.  */
35561       cp_parser_objc_class_ivars (parser);
35562       objc_continue_interface ();
35563     }
35564 
35565   cp_parser_objc_method_prototype_list (parser);
35566 }
35567 
35568 /* Parse an Objective-C class implementation.  */
35569 
35570 static void
cp_parser_objc_class_implementation(cp_parser * parser)35571 cp_parser_objc_class_implementation (cp_parser* parser)
35572 {
35573   tree name, super, categ;
35574   bool is_class_extension;
35575 
35576   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
35577   name = cp_parser_identifier (parser);
35578   if (name == error_mark_node)
35579     {
35580       /* It's hard to recover because even if valid @implementation
35581 	 stuff is to follow, we can't compile it (or validate it) if
35582 	 we don't even know which class it refers to.  Let's assume
35583 	 this was a stray '@implementation' token in the stream and
35584 	 skip it.
35585       */
35586       return;
35587     }
35588   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
35589 					 &is_class_extension);
35590 
35591   /* We have either a class or a category on our hands.  */
35592   if (categ)
35593     objc_start_category_implementation (name, categ);
35594   else
35595     {
35596       objc_start_class_implementation (name, super);
35597       /* Handle instance variable declarations, if any.  */
35598       cp_parser_objc_class_ivars (parser);
35599       objc_continue_implementation ();
35600     }
35601 
35602   cp_parser_objc_method_definition_list (parser);
35603 }
35604 
35605 /* Consume the @end token and finish off the implementation.  */
35606 
35607 static void
cp_parser_objc_end_implementation(cp_parser * parser)35608 cp_parser_objc_end_implementation (cp_parser* parser)
35609 {
35610   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
35611   objc_finish_implementation ();
35612 }
35613 
35614 /* Parse an Objective-C declaration.  */
35615 
35616 static void
cp_parser_objc_declaration(cp_parser * parser,tree attributes)35617 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
35618 {
35619   /* Try to figure out what kind of declaration is present.  */
35620   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35621 
35622   if (attributes)
35623     switch (kwd->keyword)
35624       {
35625 	case RID_AT_ALIAS:
35626 	case RID_AT_CLASS:
35627 	case RID_AT_END:
35628 	  error_at (kwd->location, "attributes may not be specified before"
35629 	            " the %<@%D%> Objective-C++ keyword",
35630 		    kwd->u.value);
35631 	  attributes = NULL;
35632 	  break;
35633 	case RID_AT_IMPLEMENTATION:
35634 	  warning_at (kwd->location, OPT_Wattributes,
35635 		      "prefix attributes are ignored before %<@%D%>",
35636 		      kwd->u.value);
35637 	  attributes = NULL;
35638 	default:
35639 	  break;
35640       }
35641 
35642   switch (kwd->keyword)
35643     {
35644     case RID_AT_ALIAS:
35645       cp_parser_objc_alias_declaration (parser);
35646       break;
35647     case RID_AT_CLASS:
35648       cp_parser_objc_class_declaration (parser);
35649       break;
35650     case RID_AT_PROTOCOL:
35651       cp_parser_objc_protocol_declaration (parser, attributes);
35652       break;
35653     case RID_AT_INTERFACE:
35654       cp_parser_objc_class_interface (parser, attributes);
35655       break;
35656     case RID_AT_IMPLEMENTATION:
35657       cp_parser_objc_class_implementation (parser);
35658       break;
35659     case RID_AT_END:
35660       cp_parser_objc_end_implementation (parser);
35661       break;
35662     default:
35663       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35664 		kwd->u.value);
35665       cp_parser_skip_to_end_of_block_or_statement (parser);
35666     }
35667 }
35668 
35669 /* Parse an Objective-C try-catch-finally statement.
35670 
35671    objc-try-catch-finally-stmt:
35672      @try compound-statement objc-catch-clause-seq [opt]
35673        objc-finally-clause [opt]
35674 
35675    objc-catch-clause-seq:
35676      objc-catch-clause objc-catch-clause-seq [opt]
35677 
35678    objc-catch-clause:
35679      @catch ( objc-exception-declaration ) compound-statement
35680 
35681    objc-finally-clause:
35682      @finally compound-statement
35683 
35684    objc-exception-declaration:
35685      parameter-declaration
35686      '...'
35687 
35688    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35689 
35690    Returns NULL_TREE.
35691 
35692    PS: This function is identical to c_parser_objc_try_catch_finally_statement
35693    for C.  Keep them in sync.  */
35694 
35695 static tree
cp_parser_objc_try_catch_finally_statement(cp_parser * parser)35696 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
35697 {
35698   location_t location;
35699   tree stmt;
35700 
35701   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
35702   location = cp_lexer_peek_token (parser->lexer)->location;
35703   objc_maybe_warn_exceptions (location);
35704   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35705      node, lest it get absorbed into the surrounding block.  */
35706   stmt = push_stmt_list ();
35707   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35708   objc_begin_try_stmt (location, pop_stmt_list (stmt));
35709 
35710   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
35711     {
35712       cp_parameter_declarator *parm;
35713       tree parameter_declaration = error_mark_node;
35714       bool seen_open_paren = false;
35715       matching_parens parens;
35716 
35717       cp_lexer_consume_token (parser->lexer);
35718       if (parens.require_open (parser))
35719 	seen_open_paren = true;
35720       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
35721 	{
35722 	  /* We have "@catch (...)" (where the '...' are literally
35723 	     what is in the code).  Skip the '...'.
35724 	     parameter_declaration is set to NULL_TREE, and
35725 	     objc_being_catch_clauses() knows that that means
35726 	     '...'.  */
35727 	  cp_lexer_consume_token (parser->lexer);
35728 	  parameter_declaration = NULL_TREE;
35729 	}
35730       else
35731 	{
35732 	  /* We have "@catch (NSException *exception)" or something
35733 	     like that.  Parse the parameter declaration.  */
35734 	  parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35735 						  false, NULL);
35736 	  if (parm == NULL)
35737 	    parameter_declaration = error_mark_node;
35738 	  else
35739 	    parameter_declaration = grokdeclarator (parm->declarator,
35740 						    &parm->decl_specifiers,
35741 						    PARM, /*initialized=*/0,
35742 						    /*attrlist=*/NULL);
35743 	}
35744       if (seen_open_paren)
35745 	parens.require_close (parser);
35746       else
35747 	{
35748 	  /* If there was no open parenthesis, we are recovering from
35749 	     an error, and we are trying to figure out what mistake
35750 	     the user has made.  */
35751 
35752 	  /* If there is an immediate closing parenthesis, the user
35753 	     probably forgot the opening one (ie, they typed "@catch
35754 	     NSException *e)".  Parse the closing parenthesis and keep
35755 	     going.  */
35756 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35757 	    cp_lexer_consume_token (parser->lexer);
35758 
35759 	  /* If these is no immediate closing parenthesis, the user
35760 	     probably doesn't know that parenthesis are required at
35761 	     all (ie, they typed "@catch NSException *e").  So, just
35762 	     forget about the closing parenthesis and keep going.  */
35763 	}
35764       objc_begin_catch_clause (parameter_declaration);
35765       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35766       objc_finish_catch_clause ();
35767     }
35768   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
35769     {
35770       cp_lexer_consume_token (parser->lexer);
35771       location = cp_lexer_peek_token (parser->lexer)->location;
35772       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
35773 	 node, lest it get absorbed into the surrounding block.  */
35774       stmt = push_stmt_list ();
35775       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35776       objc_build_finally_clause (location, pop_stmt_list (stmt));
35777     }
35778 
35779   return objc_finish_try_stmt ();
35780 }
35781 
35782 /* Parse an Objective-C synchronized statement.
35783 
35784    objc-synchronized-stmt:
35785      @synchronized ( expression ) compound-statement
35786 
35787    Returns NULL_TREE.  */
35788 
35789 static tree
cp_parser_objc_synchronized_statement(cp_parser * parser)35790 cp_parser_objc_synchronized_statement (cp_parser *parser)
35791 {
35792   location_t location;
35793   tree lock, stmt;
35794 
35795   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
35796 
35797   location = cp_lexer_peek_token (parser->lexer)->location;
35798   objc_maybe_warn_exceptions (location);
35799   matching_parens parens;
35800   parens.require_open (parser);
35801   lock = cp_parser_expression (parser);
35802   parens.require_close (parser);
35803 
35804   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
35805      node, lest it get absorbed into the surrounding block.  */
35806   stmt = push_stmt_list ();
35807   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35808 
35809   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
35810 }
35811 
35812 /* Parse an Objective-C throw statement.
35813 
35814    objc-throw-stmt:
35815      @throw assignment-expression [opt] ;
35816 
35817    Returns a constructed '@throw' statement.  */
35818 
35819 static tree
cp_parser_objc_throw_statement(cp_parser * parser)35820 cp_parser_objc_throw_statement (cp_parser *parser)
35821 {
35822   tree expr = NULL_TREE;
35823   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35824 
35825   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
35826 
35827   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35828     expr = cp_parser_expression (parser);
35829 
35830   cp_parser_consume_semicolon_at_end_of_statement (parser);
35831 
35832   return objc_build_throw_stmt (loc, expr);
35833 }
35834 
35835 /* Parse an Objective-C statement.  */
35836 
35837 static tree
cp_parser_objc_statement(cp_parser * parser)35838 cp_parser_objc_statement (cp_parser * parser)
35839 {
35840   /* Try to figure out what kind of declaration is present.  */
35841   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35842 
35843   switch (kwd->keyword)
35844     {
35845     case RID_AT_TRY:
35846       return cp_parser_objc_try_catch_finally_statement (parser);
35847     case RID_AT_SYNCHRONIZED:
35848       return cp_parser_objc_synchronized_statement (parser);
35849     case RID_AT_THROW:
35850       return cp_parser_objc_throw_statement (parser);
35851     default:
35852       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35853 	       kwd->u.value);
35854       cp_parser_skip_to_end_of_block_or_statement (parser);
35855     }
35856 
35857   return error_mark_node;
35858 }
35859 
35860 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
35861    look ahead to see if an objc keyword follows the attributes.  This
35862    is to detect the use of prefix attributes on ObjC @interface and
35863    @protocol.  */
35864 
35865 static bool
cp_parser_objc_valid_prefix_attributes(cp_parser * parser,tree * attrib)35866 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
35867 {
35868   cp_lexer_save_tokens (parser->lexer);
35869   tree addon = cp_parser_attributes_opt (parser);
35870   if (addon
35871       && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
35872     {
35873       cp_lexer_commit_tokens (parser->lexer);
35874       if (*attrib)
35875 	TREE_CHAIN (*attrib) = addon;
35876       else
35877 	*attrib = addon;
35878       return true;
35879     }
35880   cp_lexer_rollback_tokens (parser->lexer);
35881   return false;
35882 }
35883 
35884 /* This routine is a minimal replacement for
35885    c_parser_struct_declaration () used when parsing the list of
35886    types/names or ObjC++ properties.  For example, when parsing the
35887    code
35888 
35889    @property (readonly) int a, b, c;
35890 
35891    this function is responsible for parsing "int a, int b, int c" and
35892    returning the declarations as CHAIN of DECLs.
35893 
35894    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
35895    similar parsing.  */
35896 static tree
cp_parser_objc_struct_declaration(cp_parser * parser)35897 cp_parser_objc_struct_declaration (cp_parser *parser)
35898 {
35899   tree decls = NULL_TREE;
35900   cp_decl_specifier_seq declspecs;
35901   int decl_class_or_enum_p;
35902   tree prefix_attributes;
35903 
35904   cp_parser_decl_specifier_seq (parser,
35905 				CP_PARSER_FLAGS_NONE,
35906 				&declspecs,
35907 				&decl_class_or_enum_p);
35908 
35909   if (declspecs.type == error_mark_node)
35910     return error_mark_node;
35911 
35912   /* auto, register, static, extern, mutable.  */
35913   if (declspecs.storage_class != sc_none)
35914     {
35915       cp_parser_error (parser, "invalid type for property");
35916       declspecs.storage_class = sc_none;
35917     }
35918 
35919   /* thread_local.  */
35920   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35921     {
35922       cp_parser_error (parser, "invalid type for property");
35923       declspecs.locations[ds_thread] = 0;
35924     }
35925 
35926   /* typedef.  */
35927   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35928     {
35929       cp_parser_error (parser, "invalid type for property");
35930       declspecs.locations[ds_typedef] = 0;
35931     }
35932 
35933   prefix_attributes = declspecs.attributes;
35934   declspecs.attributes = NULL_TREE;
35935 
35936   /* Keep going until we hit the `;' at the end of the declaration. */
35937   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35938     {
35939       tree attributes, first_attribute, decl;
35940       cp_declarator *declarator;
35941       cp_token *token;
35942 
35943       /* Parse the declarator.  */
35944       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35945 					 CP_PARSER_FLAGS_NONE,
35946 					 NULL, NULL, false, false, false);
35947 
35948       /* Look for attributes that apply to the ivar.  */
35949       attributes = cp_parser_attributes_opt (parser);
35950       /* Remember which attributes are prefix attributes and
35951 	 which are not.  */
35952       first_attribute = attributes;
35953       /* Combine the attributes.  */
35954       attributes = attr_chainon (prefix_attributes, attributes);
35955 
35956       decl = grokfield (declarator, &declspecs,
35957 			NULL_TREE, /*init_const_expr_p=*/false,
35958 			NULL_TREE, attributes);
35959 
35960       if (decl == error_mark_node || decl == NULL_TREE)
35961 	return error_mark_node;
35962 
35963       /* Reset PREFIX_ATTRIBUTES.  */
35964       if (attributes != error_mark_node)
35965 	{
35966 	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
35967 	    attributes = TREE_CHAIN (attributes);
35968 	  if (attributes)
35969 	    TREE_CHAIN (attributes) = NULL_TREE;
35970 	}
35971 
35972       DECL_CHAIN (decl) = decls;
35973       decls = decl;
35974 
35975       token = cp_lexer_peek_token (parser->lexer);
35976       if (token->type == CPP_COMMA)
35977 	{
35978 	  cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
35979 	  continue;
35980 	}
35981       else
35982 	break;
35983     }
35984   return decls;
35985 }
35986 
35987 /* Parse an Objective-C @property declaration.  The syntax is:
35988 
35989    objc-property-declaration:
35990      '@property' objc-property-attributes[opt] struct-declaration ;
35991 
35992    objc-property-attributes:
35993     '(' objc-property-attribute-list ')'
35994 
35995    objc-property-attribute-list:
35996      objc-property-attribute
35997      objc-property-attribute-list, objc-property-attribute
35998 
35999    objc-property-attribute
36000      'getter' = identifier
36001      'setter' = identifier
36002      'readonly'
36003      'readwrite'
36004      'assign'
36005      'retain'
36006      'copy'
36007      'nonatomic'
36008 
36009   For example:
36010     @property NSString *name;
36011     @property (readonly) id object;
36012     @property (retain, nonatomic, getter=getTheName) id name;
36013     @property int a, b, c;
36014 
36015    PS: This function is identical to
36016    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
36017 static void
cp_parser_objc_at_property_declaration(cp_parser * parser)36018 cp_parser_objc_at_property_declaration (cp_parser *parser)
36019 {
36020   /* Parse the optional attribute list.
36021 
36022      A list of parsed, but not verified, attributes.  */
36023   auto_delete_vec<property_attribute_info> prop_attr_list;
36024   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36025 
36026   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
36027 
36028   /* Parse the optional attribute list...  */
36029   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36030     {
36031       /* Eat the '('.  */
36032       matching_parens parens;
36033       location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
36034       parens.consume_open (parser);
36035       bool syntax_error = false;
36036 
36037       /* Allow empty @property attribute lists, but with a warning.  */
36038       location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
36039       location_t attr_comb;
36040       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36041 	{
36042 	  attr_comb = make_location (attr_end, attr_start, attr_end);
36043 	  warning_at (attr_comb, OPT_Wattributes,
36044 		      "empty property attribute list");
36045 	}
36046       else
36047 	while (true)
36048 	  {
36049 	    cp_token *token = cp_lexer_peek_token (parser->lexer);
36050 	    attr_start = token->location;
36051 	    attr_end = get_finish (token->location);
36052 	    attr_comb = make_location (attr_start, attr_start, attr_end);
36053 
36054 	    if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
36055 	      {
36056 		warning_at (attr_comb, OPT_Wattributes,
36057 			    "missing property attribute");
36058 		if (token->type == CPP_CLOSE_PAREN)
36059 		  break;
36060 		cp_lexer_consume_token (parser->lexer);
36061 		continue;
36062 	      }
36063 
36064 	    tree attr_name = NULL_TREE;
36065 	    if (identifier_p (token->u.value))
36066 	      attr_name = token->u.value;
36067 
36068 	    enum rid keyword;
36069 	    if (token->type == CPP_NAME)
36070 	      keyword = C_RID_CODE (token->u.value);
36071 	    else if (token->type == CPP_KEYWORD
36072 		     && token->keyword == RID_CLASS)
36073 	      /* Account for accepting the 'class' keyword in this context.  */
36074 	      keyword = RID_CLASS;
36075 	    else
36076 	      keyword = RID_MAX; /* By definition, an unknown property.  */
36077 	    cp_lexer_consume_token (parser->lexer);
36078 
36079 	    enum objc_property_attribute_kind prop_kind
36080 	      = objc_prop_attr_kind_for_rid (keyword);
36081 	    property_attribute_info *prop
36082 	      = new property_attribute_info (attr_name, attr_comb, prop_kind);
36083 	    prop_attr_list.safe_push (prop);
36084 
36085 	    tree meth_name;
36086 	    switch (prop->prop_kind)
36087 	      {
36088 	      default: break;
36089 	      case OBJC_PROPERTY_ATTR_UNKNOWN:
36090 		if (attr_name)
36091 		  error_at (attr_start, "unknown property attribute %qE",
36092 			    attr_name);
36093 		else
36094 		  error_at (attr_start, "unknown property attribute");
36095 		prop->parse_error = syntax_error = true;
36096 		break;
36097 
36098 	      case OBJC_PROPERTY_ATTR_GETTER:
36099 	      case OBJC_PROPERTY_ATTR_SETTER:
36100 		if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36101 		  {
36102 		    attr_comb = make_location (attr_end, attr_start, attr_end);
36103 		    error_at (attr_comb, "expected %<=%> after Objective-C %qE",
36104 			      attr_name);
36105 		    prop->parse_error = syntax_error = true;
36106 		    break;
36107 		  }
36108 
36109 		token = cp_lexer_peek_token (parser->lexer);
36110 		attr_end = token->location;
36111 		cp_lexer_consume_token (parser->lexer); /* eat the = */
36112 
36113 		if (!cp_parser_objc_selector_p
36114 		     (cp_lexer_peek_token (parser->lexer)->type))
36115 		  {
36116 		    attr_comb = make_location (attr_end, attr_start, attr_end);
36117 		    error_at (attr_comb, "expected %qE selector name",
36118 			      attr_name);
36119 		    prop->parse_error = syntax_error = true;
36120 		    break;
36121 		  }
36122 
36123 		/* Get the end of the method name, and consume the name.  */
36124 		token = cp_lexer_peek_token (parser->lexer);
36125 		attr_end = get_finish (token->location);
36126 		/* Because method names may contain C++ keywords, we have a
36127 		   routine to fetch them (this also consumes the token).  */
36128 		meth_name = cp_parser_objc_selector (parser);
36129 
36130 		if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
36131 		  {
36132 		    if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36133 		      {
36134 			attr_comb = make_location (attr_end, attr_start,
36135 						   attr_end);
36136 			error_at (attr_comb, "setter method names must"
36137 				  " terminate with %<:%>");
36138 			prop->parse_error = syntax_error = true;
36139 		      }
36140 		    else
36141 		      {
36142 			attr_end = get_finish (cp_lexer_peek_token
36143 					       (parser->lexer)->location);
36144 			cp_lexer_consume_token (parser->lexer);
36145 		      }
36146 		    attr_comb = make_location (attr_start, attr_start,
36147 					       attr_end);
36148 		  }
36149 		else
36150 		  attr_comb = make_location (attr_start, attr_start,
36151 					     attr_end);
36152 		prop->ident = meth_name;
36153 		/* Updated location including all that was successfully
36154 		   parsed.  */
36155 		prop->prop_loc = attr_comb;
36156 		break;
36157 	      }
36158 
36159 	    /* If we see a comma here, then keep going - even if we already
36160 	       saw a syntax error.  For simple mistakes e.g. (asign, getter=x)
36161 	       this makes a more useful output and avoid spurious warnings
36162 	       about missing attributes that are, in fact, specified after the
36163 	       one with the syntax error.  */
36164 	    if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36165 	      cp_lexer_consume_token (parser->lexer);
36166 	    else
36167 	      break;
36168 	  }
36169 
36170       if (syntax_error || !parens.require_close (parser))
36171 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36172 					       /*or_comma=*/false,
36173 					       /*consume_paren=*/true);
36174     }
36175 
36176   /* 'properties' is the list of properties that we read.  Usually a
36177      single one, but maybe more (eg, in "@property int a, b, c;" there
36178      are three).
36179      TODO: Update this parsing so that it accepts (erroneous) bitfields so
36180      that we can issue a meaningful and consistent (between C/C++) error
36181      message from objc_add_property_declaration ().  */
36182   tree properties = cp_parser_objc_struct_declaration (parser);
36183 
36184   if (properties == error_mark_node)
36185     cp_parser_skip_to_end_of_statement (parser);
36186   else if (properties == NULL_TREE)
36187     cp_parser_error (parser, "expected identifier");
36188   else
36189     {
36190       /* Comma-separated properties are chained together in reverse order;
36191 	 add them one by one.  */
36192       properties = nreverse (properties);
36193       for (; properties; properties = TREE_CHAIN (properties))
36194 	objc_add_property_declaration (loc, copy_node (properties),
36195 				       prop_attr_list);
36196     }
36197 
36198   cp_parser_consume_semicolon_at_end_of_statement (parser);
36199 }
36200 
36201 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
36202 
36203    objc-synthesize-declaration:
36204      @synthesize objc-synthesize-identifier-list ;
36205 
36206    objc-synthesize-identifier-list:
36207      objc-synthesize-identifier
36208      objc-synthesize-identifier-list, objc-synthesize-identifier
36209 
36210    objc-synthesize-identifier
36211      identifier
36212      identifier = identifier
36213 
36214   For example:
36215     @synthesize MyProperty;
36216     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36217 
36218   PS: This function is identical to c_parser_objc_at_synthesize_declaration
36219   for C.  Keep them in sync.
36220 */
36221 static void
cp_parser_objc_at_synthesize_declaration(cp_parser * parser)36222 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
36223 {
36224   tree list = NULL_TREE;
36225   location_t loc;
36226   loc = cp_lexer_peek_token (parser->lexer)->location;
36227 
36228   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
36229   while (true)
36230     {
36231       tree property, ivar;
36232       property = cp_parser_identifier (parser);
36233       if (property == error_mark_node)
36234 	{
36235 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
36236 	  return;
36237 	}
36238       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
36239 	{
36240 	  cp_lexer_consume_token (parser->lexer);
36241 	  ivar = cp_parser_identifier (parser);
36242 	  if (ivar == error_mark_node)
36243 	    {
36244 	      cp_parser_consume_semicolon_at_end_of_statement (parser);
36245 	      return;
36246 	    }
36247 	}
36248       else
36249 	ivar = NULL_TREE;
36250       list = chainon (list, build_tree_list (ivar, property));
36251       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36252 	cp_lexer_consume_token (parser->lexer);
36253       else
36254 	break;
36255     }
36256   cp_parser_consume_semicolon_at_end_of_statement (parser);
36257   objc_add_synthesize_declaration (loc, list);
36258 }
36259 
36260 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
36261 
36262    objc-dynamic-declaration:
36263      @dynamic identifier-list ;
36264 
36265    For example:
36266      @dynamic MyProperty;
36267      @dynamic MyProperty, AnotherProperty;
36268 
36269   PS: This function is identical to c_parser_objc_at_dynamic_declaration
36270   for C.  Keep them in sync.
36271 */
36272 static void
cp_parser_objc_at_dynamic_declaration(cp_parser * parser)36273 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
36274 {
36275   tree list = NULL_TREE;
36276   location_t loc;
36277   loc = cp_lexer_peek_token (parser->lexer)->location;
36278 
36279   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
36280   while (true)
36281     {
36282       tree property;
36283       property = cp_parser_identifier (parser);
36284       if (property == error_mark_node)
36285 	{
36286 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
36287 	  return;
36288 	}
36289       list = chainon (list, build_tree_list (NULL, property));
36290       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36291 	cp_lexer_consume_token (parser->lexer);
36292       else
36293 	break;
36294     }
36295   cp_parser_consume_semicolon_at_end_of_statement (parser);
36296   objc_add_dynamic_declaration (loc, list);
36297 }
36298 
36299 
36300 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines.  */
36301 
36302 /* Returns name of the next clause.
36303    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36304    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
36305    returned and the token is consumed.  */
36306 
36307 static pragma_omp_clause
cp_parser_omp_clause_name(cp_parser * parser)36308 cp_parser_omp_clause_name (cp_parser *parser)
36309 {
36310   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
36311 
36312   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36313     result = PRAGMA_OACC_CLAUSE_AUTO;
36314   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
36315     result = PRAGMA_OMP_CLAUSE_IF;
36316   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36317     result = PRAGMA_OMP_CLAUSE_DEFAULT;
36318   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
36319     result = PRAGMA_OACC_CLAUSE_DELETE;
36320   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
36321     result = PRAGMA_OMP_CLAUSE_PRIVATE;
36322   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36323     result = PRAGMA_OMP_CLAUSE_FOR;
36324   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36325     {
36326       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36327       const char *p = IDENTIFIER_POINTER (id);
36328 
36329       switch (p[0])
36330 	{
36331 	case 'a':
36332 	  if (!strcmp ("affinity", p))
36333 	    result = PRAGMA_OMP_CLAUSE_AFFINITY;
36334 	  else if (!strcmp ("aligned", p))
36335 	    result = PRAGMA_OMP_CLAUSE_ALIGNED;
36336 	  else if (!strcmp ("allocate", p))
36337 	    result = PRAGMA_OMP_CLAUSE_ALLOCATE;
36338 	  else if (!strcmp ("async", p))
36339 	    result = PRAGMA_OACC_CLAUSE_ASYNC;
36340 	  else if (!strcmp ("attach", p))
36341 	    result = PRAGMA_OACC_CLAUSE_ATTACH;
36342 	  break;
36343 	case 'b':
36344 	  if (!strcmp ("bind", p))
36345 	    result = PRAGMA_OMP_CLAUSE_BIND;
36346 	  break;
36347 	case 'c':
36348 	  if (!strcmp ("collapse", p))
36349 	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
36350 	  else if (!strcmp ("copy", p))
36351 	    result = PRAGMA_OACC_CLAUSE_COPY;
36352 	  else if (!strcmp ("copyin", p))
36353 	    result = PRAGMA_OMP_CLAUSE_COPYIN;
36354 	  else if (!strcmp ("copyout", p))
36355 	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
36356 	  else if (!strcmp ("copyprivate", p))
36357 	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
36358 	  else if (!strcmp ("create", p))
36359 	    result = PRAGMA_OACC_CLAUSE_CREATE;
36360 	  break;
36361 	case 'd':
36362 	  if (!strcmp ("defaultmap", p))
36363 	    result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
36364 	  else if (!strcmp ("depend", p))
36365 	    result = PRAGMA_OMP_CLAUSE_DEPEND;
36366 	  else if (!strcmp ("detach", p))
36367 	    result = PRAGMA_OACC_CLAUSE_DETACH;
36368 	  else if (!strcmp ("device", p))
36369 	    result = PRAGMA_OMP_CLAUSE_DEVICE;
36370 	  else if (!strcmp ("deviceptr", p))
36371 	    result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
36372 	  else if (!strcmp ("device_resident", p))
36373 	    result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
36374 	  else if (!strcmp ("device_type", p))
36375 	    result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
36376 	  else if (!strcmp ("dist_schedule", p))
36377 	    result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
36378 	  break;
36379 	case 'f':
36380 	  if (!strcmp ("filter", p))
36381 	    result = PRAGMA_OMP_CLAUSE_FILTER;
36382 	  else if (!strcmp ("final", p))
36383 	    result = PRAGMA_OMP_CLAUSE_FINAL;
36384 	  else if (!strcmp ("finalize", p))
36385 	    result = PRAGMA_OACC_CLAUSE_FINALIZE;
36386 	  else if (!strcmp ("firstprivate", p))
36387 	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
36388 	  else if (!strcmp ("from", p))
36389 	    result = PRAGMA_OMP_CLAUSE_FROM;
36390 	  break;
36391 	case 'g':
36392 	  if (!strcmp ("gang", p))
36393 	    result = PRAGMA_OACC_CLAUSE_GANG;
36394 	  else if (!strcmp ("grainsize", p))
36395 	    result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
36396 	  break;
36397 	case 'h':
36398 	  if (!strcmp ("has_device_addr", p))
36399 	    result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
36400 	  else if (!strcmp ("hint", p))
36401 	    result = PRAGMA_OMP_CLAUSE_HINT;
36402 	  else if (!strcmp ("host", p))
36403 	    result = PRAGMA_OACC_CLAUSE_HOST;
36404 	  break;
36405 	case 'i':
36406 	  if (!strcmp ("if_present", p))
36407 	    result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
36408 	  else if (!strcmp ("in_reduction", p))
36409 	    result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
36410 	  else if (!strcmp ("inbranch", p))
36411 	    result = PRAGMA_OMP_CLAUSE_INBRANCH;
36412 	  else if (!strcmp ("independent", p))
36413 	    result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
36414 	  else if (!strcmp ("is_device_ptr", p))
36415 	    result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
36416 	  break;
36417 	case 'l':
36418 	  if (!strcmp ("lastprivate", p))
36419 	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
36420 	  else if (!strcmp ("linear", p))
36421 	    result = PRAGMA_OMP_CLAUSE_LINEAR;
36422 	  else if (!strcmp ("link", p))
36423 	    result = PRAGMA_OMP_CLAUSE_LINK;
36424 	  break;
36425 	case 'm':
36426 	  if (!strcmp ("map", p))
36427 	    result = PRAGMA_OMP_CLAUSE_MAP;
36428 	  else if (!strcmp ("mergeable", p))
36429 	    result = PRAGMA_OMP_CLAUSE_MERGEABLE;
36430 	  break;
36431 	case 'n':
36432 	  if (!strcmp ("no_create", p))
36433 	    result = PRAGMA_OACC_CLAUSE_NO_CREATE;
36434 	  else if (!strcmp ("nogroup", p))
36435 	    result = PRAGMA_OMP_CLAUSE_NOGROUP;
36436 	  else if (!strcmp ("nohost", p))
36437 	    result = PRAGMA_OACC_CLAUSE_NOHOST;
36438 	  else if (!strcmp ("nontemporal", p))
36439 	    result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
36440 	  else if (!strcmp ("notinbranch", p))
36441 	    result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
36442 	  else if (!strcmp ("nowait", p))
36443 	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
36444 	  else if (!strcmp ("num_gangs", p))
36445 	    result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
36446 	  else if (!strcmp ("num_tasks", p))
36447 	    result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
36448 	  else if (!strcmp ("num_teams", p))
36449 	    result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
36450 	  else if (!strcmp ("num_threads", p))
36451 	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
36452 	  else if (!strcmp ("num_workers", p))
36453 	    result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
36454 	  break;
36455 	case 'o':
36456 	  if (!strcmp ("ordered", p))
36457 	    result = PRAGMA_OMP_CLAUSE_ORDERED;
36458 	  else if (!strcmp ("order", p))
36459 	    result = PRAGMA_OMP_CLAUSE_ORDER;
36460 	  break;
36461 	case 'p':
36462 	  if (!strcmp ("parallel", p))
36463 	    result = PRAGMA_OMP_CLAUSE_PARALLEL;
36464 	  else if (!strcmp ("present", p))
36465 	    result = PRAGMA_OACC_CLAUSE_PRESENT;
36466 	  else if (!strcmp ("present_or_copy", p)
36467 		   || !strcmp ("pcopy", p))
36468 	    result = PRAGMA_OACC_CLAUSE_COPY;
36469 	  else if (!strcmp ("present_or_copyin", p)
36470 		   || !strcmp ("pcopyin", p))
36471 	    result = PRAGMA_OACC_CLAUSE_COPYIN;
36472 	  else if (!strcmp ("present_or_copyout", p)
36473 		   || !strcmp ("pcopyout", p))
36474 	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
36475 	  else if (!strcmp ("present_or_create", p)
36476 		   || !strcmp ("pcreate", p))
36477 	    result = PRAGMA_OACC_CLAUSE_CREATE;
36478 	  else if (!strcmp ("priority", p))
36479 	    result = PRAGMA_OMP_CLAUSE_PRIORITY;
36480 	  else if (!strcmp ("proc_bind", p))
36481 	    result = PRAGMA_OMP_CLAUSE_PROC_BIND;
36482 	  break;
36483 	case 'r':
36484 	  if (!strcmp ("reduction", p))
36485 	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
36486 	  break;
36487 	case 's':
36488 	  if (!strcmp ("safelen", p))
36489 	    result = PRAGMA_OMP_CLAUSE_SAFELEN;
36490 	  else if (!strcmp ("schedule", p))
36491 	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
36492 	  else if (!strcmp ("sections", p))
36493 	    result = PRAGMA_OMP_CLAUSE_SECTIONS;
36494 	  else if (!strcmp ("self", p)) /* "self" is a synonym for "host".  */
36495 	    result = PRAGMA_OACC_CLAUSE_HOST;
36496 	  else if (!strcmp ("seq", p))
36497 	    result = PRAGMA_OACC_CLAUSE_SEQ;
36498 	  else if (!strcmp ("shared", p))
36499 	    result = PRAGMA_OMP_CLAUSE_SHARED;
36500 	  else if (!strcmp ("simd", p))
36501 	    result = PRAGMA_OMP_CLAUSE_SIMD;
36502 	  else if (!strcmp ("simdlen", p))
36503 	    result = PRAGMA_OMP_CLAUSE_SIMDLEN;
36504 	  break;
36505 	case 't':
36506 	  if (!strcmp ("task_reduction", p))
36507 	    result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
36508 	  else if (!strcmp ("taskgroup", p))
36509 	    result = PRAGMA_OMP_CLAUSE_TASKGROUP;
36510 	  else if (!strcmp ("thread_limit", p))
36511 	    result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
36512 	  else if (!strcmp ("threads", p))
36513 	    result = PRAGMA_OMP_CLAUSE_THREADS;
36514 	  else if (!strcmp ("tile", p))
36515 	    result = PRAGMA_OACC_CLAUSE_TILE;
36516 	  else if (!strcmp ("to", p))
36517 	    result = PRAGMA_OMP_CLAUSE_TO;
36518 	  break;
36519 	case 'u':
36520 	  if (!strcmp ("uniform", p))
36521 	    result = PRAGMA_OMP_CLAUSE_UNIFORM;
36522 	  else if (!strcmp ("untied", p))
36523 	    result = PRAGMA_OMP_CLAUSE_UNTIED;
36524 	  else if (!strcmp ("use_device", p))
36525 	    result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
36526 	  else if (!strcmp ("use_device_addr", p))
36527 	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
36528 	  else if (!strcmp ("use_device_ptr", p))
36529 	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
36530 	  break;
36531 	case 'v':
36532 	  if (!strcmp ("vector", p))
36533 	    result = PRAGMA_OACC_CLAUSE_VECTOR;
36534 	  else if (!strcmp ("vector_length", p))
36535 	    result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
36536 	  break;
36537 	case 'w':
36538 	  if (!strcmp ("wait", p))
36539 	    result = PRAGMA_OACC_CLAUSE_WAIT;
36540 	  else if (!strcmp ("worker", p))
36541 	    result = PRAGMA_OACC_CLAUSE_WORKER;
36542 	  break;
36543 	}
36544     }
36545 
36546   if (result != PRAGMA_OMP_CLAUSE_NONE)
36547     cp_lexer_consume_token (parser->lexer);
36548 
36549   return result;
36550 }
36551 
36552 /* Validate that a clause of the given type does not already exist.  */
36553 
36554 static void
check_no_duplicate_clause(tree clauses,enum omp_clause_code code,const char * name,location_t location)36555 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
36556 			   const char *name, location_t location)
36557 {
36558   if (omp_find_clause (clauses, code))
36559     error_at (location, "too many %qs clauses", name);
36560 }
36561 
36562 /* OpenMP 2.5:
36563    variable-list:
36564      identifier
36565      variable-list , identifier
36566 
36567    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36568    colon).  An opening parenthesis will have been consumed by the caller.
36569 
36570    If KIND is nonzero, create the appropriate node and install the decl
36571    in OMP_CLAUSE_DECL and add the node to the head of the list.
36572 
36573    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36574    return the list created.
36575 
36576    COLON can be NULL if only closing parenthesis should end the list,
36577    or pointer to bool which will receive false if the list is terminated
36578    by closing parenthesis or true if the list is terminated by colon.
36579 
36580    The optional ALLOW_DEREF argument is true if list items can use the deref
36581    (->) operator.  */
36582 
36583 struct omp_dim
36584 {
36585   tree low_bound, length;
36586   location_t loc;
36587   bool no_colon;
omp_dimomp_dim36588   omp_dim (tree lb, tree len, location_t lo, bool nc)
36589     : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
36590 };
36591 
36592 static tree
cp_parser_omp_var_list_no_open(cp_parser * parser,enum omp_clause_code kind,tree list,bool * colon,bool allow_deref=false)36593 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
36594 				tree list, bool *colon,
36595 				bool allow_deref = false)
36596 {
36597   auto_vec<omp_dim> dims;
36598   bool array_section_p;
36599   cp_token *token;
36600   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36601   if (colon)
36602     {
36603       parser->colon_corrects_to_scope_p = false;
36604       *colon = false;
36605     }
36606   while (1)
36607     {
36608       tree name, decl;
36609 
36610       if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36611 	cp_parser_parse_tentatively (parser);
36612       token = cp_lexer_peek_token (parser->lexer);
36613       if (kind != 0
36614 	  && cp_parser_is_keyword (token, RID_THIS))
36615 	{
36616 	  decl = finish_this_expr ();
36617 	  if (TREE_CODE (decl) == NON_LVALUE_EXPR
36618 	      || CONVERT_EXPR_P (decl))
36619 	    decl = TREE_OPERAND (decl, 0);
36620 	  cp_lexer_consume_token (parser->lexer);
36621 	}
36622       else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
36623 	       || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
36624 	       || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
36625 	{
36626 	  cp_id_kind idk;
36627 	  decl = cp_parser_primary_expression (parser, false, false, false,
36628 					       &idk);
36629 	}
36630       else
36631 	{
36632 	  name = cp_parser_id_expression (parser, /*template_p=*/false,
36633 					  /*check_dependency_p=*/true,
36634 					  /*template_p=*/NULL,
36635 					  /*declarator_p=*/false,
36636 					  /*optional_p=*/false);
36637 	  if (name == error_mark_node)
36638 	    {
36639 	      if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36640 		  && cp_parser_simulate_error (parser))
36641 		goto depend_lvalue;
36642 	      goto skip_comma;
36643 	    }
36644 
36645 	  if (identifier_p (name))
36646 	    decl = cp_parser_lookup_name_simple (parser, name, token->location);
36647 	  else
36648 	    decl = name;
36649 	  if (decl == error_mark_node)
36650 	    {
36651 	      if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36652 		  && cp_parser_simulate_error (parser))
36653 		goto depend_lvalue;
36654 	      cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
36655 					   token->location);
36656 	    }
36657 	}
36658       if (outer_automatic_var_p (decl))
36659 	decl = process_outer_var_ref (decl, tf_warning_or_error);
36660       if (decl == error_mark_node)
36661 	;
36662       else if (kind != 0)
36663 	{
36664 	  switch (kind)
36665 	    {
36666 	    case OMP_CLAUSE__CACHE_:
36667 	      /* The OpenACC cache directive explicitly only allows "array
36668 		 elements or subarrays".  */
36669 	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
36670 		{
36671 		  error_at (token->location, "expected %<[%>");
36672 		  decl = error_mark_node;
36673 		  break;
36674 		}
36675 	      /* FALLTHROUGH.  */
36676 	    case OMP_CLAUSE_MAP:
36677 	    case OMP_CLAUSE_FROM:
36678 	    case OMP_CLAUSE_TO:
36679 	    start_component_ref:
36680 	      while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36681 		     || (allow_deref
36682 			 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
36683 		{
36684 		  cpp_ttype ttype
36685 		    = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36686 		      ? CPP_DOT : CPP_DEREF;
36687 		  location_t loc
36688 		    = cp_lexer_peek_token (parser->lexer)->location;
36689 		  cp_id_kind idk = CP_ID_KIND_NONE;
36690 		  cp_lexer_consume_token (parser->lexer);
36691 		  decl = convert_from_reference (decl);
36692 		  decl
36693 		    = cp_parser_postfix_dot_deref_expression (parser, ttype,
36694 							      decl, false,
36695 							      &idk, loc);
36696 		}
36697 	      /* FALLTHROUGH.  */
36698 	    case OMP_CLAUSE_AFFINITY:
36699 	    case OMP_CLAUSE_DEPEND:
36700 	    case OMP_CLAUSE_REDUCTION:
36701 	    case OMP_CLAUSE_IN_REDUCTION:
36702 	    case OMP_CLAUSE_TASK_REDUCTION:
36703 	    case OMP_CLAUSE_HAS_DEVICE_ADDR:
36704 	      array_section_p = false;
36705 	      dims.truncate (0);
36706 	      while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
36707 		{
36708 		  location_t loc = UNKNOWN_LOCATION;
36709 		  tree low_bound = NULL_TREE, length = NULL_TREE;
36710 		  bool no_colon = false;
36711 
36712 		  parser->colon_corrects_to_scope_p = false;
36713 		  cp_lexer_consume_token (parser->lexer);
36714 		  if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36715 		    {
36716 		      loc = cp_lexer_peek_token (parser->lexer)->location;
36717 		      low_bound = cp_parser_expression (parser);
36718 		      /* Later handling is not prepared to see through these.  */
36719 		      gcc_checking_assert (!location_wrapper_p (low_bound));
36720 		    }
36721 		  if (!colon)
36722 		    parser->colon_corrects_to_scope_p
36723 		      = saved_colon_corrects_to_scope_p;
36724 		  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
36725 		    {
36726 		      length = integer_one_node;
36727 		      no_colon = true;
36728 		    }
36729 		  else
36730 		    {
36731 		      /* Look for `:'.  */
36732 		      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36733 			{
36734 			  if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36735 			      && cp_parser_simulate_error (parser))
36736 			    goto depend_lvalue;
36737 			  goto skip_comma;
36738 			}
36739 		      if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36740 			cp_parser_commit_to_tentative_parse (parser);
36741 		      else
36742 			array_section_p = true;
36743 		      if (!cp_lexer_next_token_is (parser->lexer,
36744 						   CPP_CLOSE_SQUARE))
36745 			{
36746 			  length = cp_parser_expression (parser);
36747 			  /* Later handling is not prepared to see through these.  */
36748 			  gcc_checking_assert (!location_wrapper_p (length));
36749 			}
36750 		    }
36751 		  /* Look for the closing `]'.  */
36752 		  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
36753 					  RT_CLOSE_SQUARE))
36754 		    {
36755 		      if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36756 			  && cp_parser_simulate_error (parser))
36757 			goto depend_lvalue;
36758 		      goto skip_comma;
36759 		    }
36760 
36761 		  dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
36762 		}
36763 
36764 	      if ((kind == OMP_CLAUSE_MAP
36765 		   || kind == OMP_CLAUSE_FROM
36766 		   || kind == OMP_CLAUSE_TO)
36767 		  && !array_section_p
36768 		  && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36769 		      || (allow_deref
36770 			  && cp_lexer_next_token_is (parser->lexer,
36771 						     CPP_DEREF))))
36772 		{
36773 		  for (unsigned i = 0; i < dims.length (); i++)
36774 		    {
36775 		      gcc_assert (dims[i].length == integer_one_node);
36776 		      decl = build_array_ref (dims[i].loc,
36777 					      decl, dims[i].low_bound);
36778 		    }
36779 		  goto start_component_ref;
36780 		}
36781 	      else
36782 		for (unsigned i = 0; i < dims.length (); i++)
36783 		  decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
36784 
36785 	      break;
36786 	    default:
36787 	      break;
36788 	    }
36789 
36790 	  if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36791 	    {
36792 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
36793 		  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
36794 		  && cp_parser_simulate_error (parser))
36795 		{
36796 		depend_lvalue:
36797 		  cp_parser_abort_tentative_parse (parser);
36798 		  decl = cp_parser_assignment_expression (parser, NULL,
36799 							  false, false);
36800 		}
36801 	      else
36802 		cp_parser_parse_definitely (parser);
36803 	    }
36804 
36805 	  tree u = build_omp_clause (token->location, kind);
36806 	  OMP_CLAUSE_DECL (u) = decl;
36807 	  OMP_CLAUSE_CHAIN (u) = list;
36808 	  list = u;
36809 	}
36810       else
36811 	list = tree_cons (decl, NULL_TREE, list);
36812 
36813     get_comma:
36814       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36815 	break;
36816       cp_lexer_consume_token (parser->lexer);
36817     }
36818 
36819   if (colon)
36820     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36821 
36822   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36823     {
36824       *colon = true;
36825       cp_parser_require (parser, CPP_COLON, RT_COLON);
36826       return list;
36827     }
36828 
36829   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36830     {
36831       int ending;
36832 
36833       /* Try to resync to an unnested comma.  Copied from
36834 	 cp_parser_parenthesized_expression_list.  */
36835     skip_comma:
36836       if (colon)
36837 	parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36838       ending = cp_parser_skip_to_closing_parenthesis (parser,
36839 						      /*recovering=*/true,
36840 						      /*or_comma=*/true,
36841 						      /*consume_paren=*/true);
36842       if (ending < 0)
36843 	goto get_comma;
36844     }
36845 
36846   return list;
36847 }
36848 
36849 /* Similarly, but expect leading and trailing parenthesis.  This is a very
36850    common case for omp clauses.  */
36851 
36852 static tree
cp_parser_omp_var_list(cp_parser * parser,enum omp_clause_code kind,tree list,bool allow_deref=false)36853 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
36854 			bool allow_deref = false)
36855 {
36856   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36857     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
36858 					   allow_deref);
36859   return list;
36860 }
36861 
36862 /* OpenACC 2.0:
36863    copy ( variable-list )
36864    copyin ( variable-list )
36865    copyout ( variable-list )
36866    create ( variable-list )
36867    delete ( variable-list )
36868    present ( variable-list )
36869 
36870    OpenACC 2.6:
36871    no_create ( variable-list )
36872    attach ( variable-list )
36873    detach ( variable-list ) */
36874 
36875 static tree
cp_parser_oacc_data_clause(cp_parser * parser,pragma_omp_clause c_kind,tree list)36876 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
36877 			    tree list)
36878 {
36879   enum gomp_map_kind kind;
36880   switch (c_kind)
36881     {
36882     case PRAGMA_OACC_CLAUSE_ATTACH:
36883       kind = GOMP_MAP_ATTACH;
36884       break;
36885     case PRAGMA_OACC_CLAUSE_COPY:
36886       kind = GOMP_MAP_TOFROM;
36887       break;
36888     case PRAGMA_OACC_CLAUSE_COPYIN:
36889       kind = GOMP_MAP_TO;
36890       break;
36891     case PRAGMA_OACC_CLAUSE_COPYOUT:
36892       kind = GOMP_MAP_FROM;
36893       break;
36894     case PRAGMA_OACC_CLAUSE_CREATE:
36895       kind = GOMP_MAP_ALLOC;
36896       break;
36897     case PRAGMA_OACC_CLAUSE_DELETE:
36898       kind = GOMP_MAP_RELEASE;
36899       break;
36900     case PRAGMA_OACC_CLAUSE_DETACH:
36901       kind = GOMP_MAP_DETACH;
36902       break;
36903     case PRAGMA_OACC_CLAUSE_DEVICE:
36904       kind = GOMP_MAP_FORCE_TO;
36905       break;
36906     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
36907       kind = GOMP_MAP_DEVICE_RESIDENT;
36908       break;
36909     case PRAGMA_OACC_CLAUSE_HOST:
36910       kind = GOMP_MAP_FORCE_FROM;
36911       break;
36912     case PRAGMA_OACC_CLAUSE_LINK:
36913       kind = GOMP_MAP_LINK;
36914       break;
36915     case PRAGMA_OACC_CLAUSE_NO_CREATE:
36916       kind = GOMP_MAP_IF_PRESENT;
36917       break;
36918     case PRAGMA_OACC_CLAUSE_PRESENT:
36919       kind = GOMP_MAP_FORCE_PRESENT;
36920       break;
36921     default:
36922       gcc_unreachable ();
36923     }
36924   tree nl, c;
36925   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
36926 
36927   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
36928     OMP_CLAUSE_SET_MAP_KIND (c, kind);
36929 
36930   return nl;
36931 }
36932 
36933 /* OpenACC 2.0:
36934    deviceptr ( variable-list ) */
36935 
36936 static tree
cp_parser_oacc_data_clause_deviceptr(cp_parser * parser,tree list)36937 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
36938 {
36939   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36940   tree vars, t;
36941 
36942   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
36943      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
36944      variable-list must only allow for pointer variables.  */
36945   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36946   for (t = vars; t; t = TREE_CHAIN (t))
36947     {
36948       tree v = TREE_PURPOSE (t);
36949       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
36950       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
36951       OMP_CLAUSE_DECL (u) = v;
36952       OMP_CLAUSE_CHAIN (u) = list;
36953       list = u;
36954     }
36955 
36956   return list;
36957 }
36958 
36959 /* OpenACC 2.5:
36960    auto
36961    finalize
36962    independent
36963    nohost
36964    seq */
36965 
36966 static tree
cp_parser_oacc_simple_clause(location_t loc,enum omp_clause_code code,tree list)36967 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
36968 			      tree list)
36969 {
36970   check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
36971 
36972   tree c = build_omp_clause (loc, code);
36973   OMP_CLAUSE_CHAIN (c) = list;
36974 
36975   return c;
36976 }
36977 
36978  /* OpenACC:
36979    num_gangs ( expression )
36980    num_workers ( expression )
36981    vector_length ( expression )  */
36982 
36983 static tree
cp_parser_oacc_single_int_clause(cp_parser * parser,omp_clause_code code,const char * str,tree list)36984 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
36985 				  const char *str, tree list)
36986 {
36987   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36988 
36989   matching_parens parens;
36990   if (!parens.require_open (parser))
36991     return list;
36992 
36993   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
36994 
36995   if (t == error_mark_node
36996       || !parens.require_close (parser))
36997     {
36998       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36999 					     /*or_comma=*/false,
37000 					     /*consume_paren=*/true);
37001       return list;
37002     }
37003 
37004   check_no_duplicate_clause (list, code, str, loc);
37005 
37006   tree c = build_omp_clause (loc, code);
37007   OMP_CLAUSE_OPERAND (c, 0) = t;
37008   OMP_CLAUSE_CHAIN (c) = list;
37009   return c;
37010 }
37011 
37012 /* OpenACC:
37013 
37014     gang [( gang-arg-list )]
37015     worker [( [num:] int-expr )]
37016     vector [( [length:] int-expr )]
37017 
37018   where gang-arg is one of:
37019 
37020     [num:] int-expr
37021     static: size-expr
37022 
37023   and size-expr may be:
37024 
37025     *
37026     int-expr
37027 */
37028 
37029 static tree
cp_parser_oacc_shape_clause(cp_parser * parser,location_t loc,omp_clause_code kind,const char * str,tree list)37030 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
37031 			     omp_clause_code kind,
37032 			     const char *str, tree list)
37033 {
37034   const char *id = "num";
37035   cp_lexer *lexer = parser->lexer;
37036   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
37037 
37038   if (kind == OMP_CLAUSE_VECTOR)
37039     id = "length";
37040 
37041   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
37042     {
37043       matching_parens parens;
37044       parens.consume_open (parser);
37045 
37046       do
37047 	{
37048 	  cp_token *next = cp_lexer_peek_token (lexer);
37049 	  int idx = 0;
37050 
37051 	  /* Gang static argument.  */
37052 	  if (kind == OMP_CLAUSE_GANG
37053 	      && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
37054 	    {
37055 	      cp_lexer_consume_token (lexer);
37056 
37057 	      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37058 		goto cleanup_error;
37059 
37060 	      idx = 1;
37061 	      if (ops[idx] != NULL)
37062 		{
37063 		  cp_parser_error (parser, "too many %<static%> arguments");
37064 		  goto cleanup_error;
37065 		}
37066 
37067 	      /* Check for the '*' argument.  */
37068 	      if (cp_lexer_next_token_is (lexer, CPP_MULT)
37069 		  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37070 		      || cp_lexer_nth_token_is (parser->lexer, 2,
37071 						CPP_CLOSE_PAREN)))
37072 		{
37073 		  cp_lexer_consume_token (lexer);
37074 		  ops[idx] = integer_minus_one_node;
37075 
37076 		  if (cp_lexer_next_token_is (lexer, CPP_COMMA))
37077 		    {
37078 		      cp_lexer_consume_token (lexer);
37079 		      continue;
37080 		    }
37081 		  else break;
37082 		}
37083 	    }
37084 	  /* Worker num: argument and vector length: arguments.  */
37085 	  else if (cp_lexer_next_token_is (lexer, CPP_NAME)
37086 		   && id_equal (next->u.value, id)
37087 		   && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
37088 	    {
37089 	      cp_lexer_consume_token (lexer);  /* id  */
37090 	      cp_lexer_consume_token (lexer);  /* ':'  */
37091 	    }
37092 
37093 	  /* Now collect the actual argument.  */
37094 	  if (ops[idx] != NULL_TREE)
37095 	    {
37096 	      cp_parser_error (parser, "unexpected argument");
37097 	      goto cleanup_error;
37098 	    }
37099 
37100 	  tree expr = cp_parser_assignment_expression (parser, NULL, false,
37101 						       false);
37102 	  if (expr == error_mark_node)
37103 	    goto cleanup_error;
37104 
37105 	  mark_exp_read (expr);
37106 	  ops[idx] = expr;
37107 
37108 	  if (kind == OMP_CLAUSE_GANG
37109 	      && cp_lexer_next_token_is (lexer, CPP_COMMA))
37110 	    {
37111 	      cp_lexer_consume_token (lexer);
37112 	      continue;
37113 	    }
37114 	  break;
37115 	}
37116       while (1);
37117 
37118       if (!parens.require_close (parser))
37119 	goto cleanup_error;
37120     }
37121 
37122   check_no_duplicate_clause (list, kind, str, loc);
37123 
37124   c = build_omp_clause (loc, kind);
37125 
37126   if (ops[1])
37127     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
37128 
37129   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
37130   OMP_CLAUSE_CHAIN (c) = list;
37131 
37132   return c;
37133 
37134  cleanup_error:
37135   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37136   return list;
37137 }
37138 
37139 /* OpenACC 2.0:
37140    tile ( size-expr-list ) */
37141 
37142 static tree
cp_parser_oacc_clause_tile(cp_parser * parser,location_t clause_loc,tree list)37143 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
37144 {
37145   tree c, expr = error_mark_node;
37146   tree tile = NULL_TREE;
37147 
37148   /* Collapse and tile are mutually exclusive.  (The spec doesn't say
37149      so, but the spec authors never considered such a case and have
37150      differing opinions on what it might mean, including 'not
37151      allowed'.)  */
37152   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
37153   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
37154 			     clause_loc);
37155 
37156   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37157     return list;
37158 
37159   do
37160     {
37161       if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
37162 	return list;
37163 
37164       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
37165 	  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37166 	      || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
37167 	{
37168 	  cp_lexer_consume_token (parser->lexer);
37169 	  expr = integer_zero_node;
37170 	}
37171       else
37172 	expr = cp_parser_constant_expression (parser);
37173 
37174       tile = tree_cons (NULL_TREE, expr, tile);
37175     }
37176   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
37177 
37178   /* Consume the trailing ')'.  */
37179   cp_lexer_consume_token (parser->lexer);
37180 
37181   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
37182   tile = nreverse (tile);
37183   OMP_CLAUSE_TILE_LIST (c) = tile;
37184   OMP_CLAUSE_CHAIN (c) = list;
37185   return c;
37186 }
37187 
37188 /* OpenACC 2.0
37189    Parse wait clause or directive parameters.  */
37190 
37191 static tree
cp_parser_oacc_wait_list(cp_parser * parser,location_t clause_loc,tree list)37192 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
37193 {
37194   vec<tree, va_gc> *args;
37195   tree t, args_tree;
37196 
37197   args = cp_parser_parenthesized_expression_list (parser, non_attr,
37198 						  /*cast_p=*/false,
37199 						  /*allow_expansion_p=*/true,
37200 						  /*non_constant_p=*/NULL);
37201 
37202   if (args == NULL || args->length () == 0)
37203     {
37204       if (args != NULL)
37205 	{
37206 	  cp_parser_error (parser, "expected integer expression list");
37207 	  release_tree_vector (args);
37208 	}
37209       return list;
37210     }
37211 
37212   args_tree = build_tree_list_vec (args);
37213 
37214   release_tree_vector (args);
37215 
37216   for (t = args_tree; t; t = TREE_CHAIN (t))
37217     {
37218       tree targ = TREE_VALUE (t);
37219 
37220       if (targ != error_mark_node)
37221 	{
37222 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
37223 	    error ("%<wait%> expression must be integral");
37224 	  else
37225 	    {
37226 	      tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
37227 
37228 	      targ = mark_rvalue_use (targ);
37229 	      OMP_CLAUSE_DECL (c) = targ;
37230 	      OMP_CLAUSE_CHAIN (c) = list;
37231 	      list = c;
37232 	    }
37233 	}
37234     }
37235 
37236   return list;
37237 }
37238 
37239 /* OpenACC:
37240    wait [( int-expr-list )] */
37241 
37242 static tree
cp_parser_oacc_clause_wait(cp_parser * parser,tree list)37243 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
37244 {
37245   location_t location = cp_lexer_peek_token (parser->lexer)->location;
37246 
37247   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37248     list = cp_parser_oacc_wait_list (parser, location, list);
37249   else
37250     {
37251       tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
37252 
37253       OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37254       OMP_CLAUSE_CHAIN (c) = list;
37255       list = c;
37256     }
37257 
37258   return list;
37259 }
37260 
37261 /* OpenMP 3.0:
37262    collapse ( constant-expression ) */
37263 
37264 static tree
cp_parser_omp_clause_collapse(cp_parser * parser,tree list,location_t location)37265 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
37266 {
37267   tree c, num;
37268   location_t loc;
37269   HOST_WIDE_INT n;
37270 
37271   loc = cp_lexer_peek_token (parser->lexer)->location;
37272   matching_parens parens;
37273   if (!parens.require_open (parser))
37274     return list;
37275 
37276   num = cp_parser_constant_expression (parser);
37277 
37278   if (!parens.require_close (parser))
37279     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37280 					   /*or_comma=*/false,
37281 					   /*consume_paren=*/true);
37282 
37283   if (num == error_mark_node)
37284     return list;
37285   num = fold_non_dependent_expr (num);
37286   if (!tree_fits_shwi_p (num)
37287       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37288       || (n = tree_to_shwi (num)) <= 0
37289       || (int) n != n)
37290     {
37291       error_at (loc, "collapse argument needs positive constant integer expression");
37292       return list;
37293     }
37294 
37295   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
37296   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
37297   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
37298   OMP_CLAUSE_CHAIN (c) = list;
37299   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
37300 
37301   return c;
37302 }
37303 
37304 /* OpenMP 2.5:
37305    default ( none | shared )
37306 
37307    OpenMP 5.1:
37308    default ( private | firstprivate )
37309 
37310    OpenACC:
37311    default ( none | present ) */
37312 
37313 static tree
cp_parser_omp_clause_default(cp_parser * parser,tree list,location_t location,bool is_oacc)37314 cp_parser_omp_clause_default (cp_parser *parser, tree list,
37315 			      location_t location, bool is_oacc)
37316 {
37317   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
37318   tree c;
37319 
37320   matching_parens parens;
37321   if (!parens.require_open (parser))
37322     return list;
37323   if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37324     {
37325       kind = OMP_CLAUSE_DEFAULT_PRIVATE;
37326       cp_lexer_consume_token (parser->lexer);
37327     }
37328   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37329     {
37330       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37331       const char *p = IDENTIFIER_POINTER (id);
37332 
37333       switch (p[0])
37334 	{
37335 	case 'n':
37336 	  if (strcmp ("none", p) != 0)
37337 	    goto invalid_kind;
37338 	  kind = OMP_CLAUSE_DEFAULT_NONE;
37339 	  break;
37340 
37341 	case 'p':
37342 	  if (strcmp ("present", p) != 0 || !is_oacc)
37343 	    goto invalid_kind;
37344 	  kind = OMP_CLAUSE_DEFAULT_PRESENT;
37345 	  break;
37346 
37347 	case 'f':
37348 	  if (strcmp ("firstprivate", p) != 0 || is_oacc)
37349 	    goto invalid_kind;
37350 	  kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
37351 	  break;
37352 
37353 	case 's':
37354 	  if (strcmp ("shared", p) != 0 || is_oacc)
37355 	    goto invalid_kind;
37356 	  kind = OMP_CLAUSE_DEFAULT_SHARED;
37357 	  break;
37358 
37359 	default:
37360 	  goto invalid_kind;
37361 	}
37362 
37363       cp_lexer_consume_token (parser->lexer);
37364     }
37365   else
37366     {
37367     invalid_kind:
37368       if (is_oacc)
37369 	cp_parser_error (parser, "expected %<none%> or %<present%>");
37370       else
37371 	cp_parser_error (parser, "expected %<none%>, %<shared%>, "
37372 				 "%<private%> or %<firstprivate%>");
37373     }
37374 
37375   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
37376       || !parens.require_close (parser))
37377     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37378 					   /*or_comma=*/false,
37379 					   /*consume_paren=*/true);
37380 
37381   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
37382     return list;
37383 
37384   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
37385   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
37386   OMP_CLAUSE_CHAIN (c) = list;
37387   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
37388 
37389   return c;
37390 }
37391 
37392 /* OpenMP 3.1:
37393    final ( expression ) */
37394 
37395 static tree
cp_parser_omp_clause_final(cp_parser * parser,tree list,location_t location)37396 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
37397 {
37398   tree t, c;
37399 
37400   matching_parens parens;
37401   if (!parens.require_open (parser))
37402     return list;
37403 
37404   t = cp_parser_assignment_expression (parser);
37405 
37406   if (t == error_mark_node
37407       || !parens.require_close (parser))
37408     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37409 					   /*or_comma=*/false,
37410 					   /*consume_paren=*/true);
37411 
37412   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
37413 
37414   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
37415   OMP_CLAUSE_FINAL_EXPR (c) = t;
37416   OMP_CLAUSE_CHAIN (c) = list;
37417 
37418   return c;
37419 }
37420 
37421 /* OpenMP 2.5:
37422    if ( expression )
37423 
37424    OpenMP 4.5:
37425    if ( directive-name-modifier : expression )
37426 
37427    directive-name-modifier:
37428      parallel | task | taskloop | target data | target | target update
37429      | target enter data | target exit data
37430 
37431    OpenMP 5.0:
37432    directive-name-modifier:
37433      ... | simd | cancel  */
37434 
37435 static tree
cp_parser_omp_clause_if(cp_parser * parser,tree list,location_t location,bool is_omp)37436 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
37437 			 bool is_omp)
37438 {
37439   tree t, c;
37440   enum tree_code if_modifier = ERROR_MARK;
37441 
37442   matching_parens parens;
37443   if (!parens.require_open (parser))
37444     return list;
37445 
37446   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37447     {
37448       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37449       const char *p = IDENTIFIER_POINTER (id);
37450       int n = 2;
37451 
37452       if (strcmp ("cancel", p) == 0)
37453 	if_modifier = VOID_CST;
37454       else if (strcmp ("parallel", p) == 0)
37455 	if_modifier = OMP_PARALLEL;
37456       else if (strcmp ("simd", p) == 0)
37457 	if_modifier = OMP_SIMD;
37458       else if (strcmp ("task", p) == 0)
37459 	if_modifier = OMP_TASK;
37460       else if (strcmp ("taskloop", p) == 0)
37461 	if_modifier = OMP_TASKLOOP;
37462       else if (strcmp ("target", p) == 0)
37463 	{
37464 	  if_modifier = OMP_TARGET;
37465 	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37466 	    {
37467 	      id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
37468 	      p = IDENTIFIER_POINTER (id);
37469 	      if (strcmp ("data", p) == 0)
37470 		if_modifier = OMP_TARGET_DATA;
37471 	      else if (strcmp ("update", p) == 0)
37472 		if_modifier = OMP_TARGET_UPDATE;
37473 	      else if (strcmp ("enter", p) == 0)
37474 		if_modifier = OMP_TARGET_ENTER_DATA;
37475 	      else if (strcmp ("exit", p) == 0)
37476 		if_modifier = OMP_TARGET_EXIT_DATA;
37477 	      if (if_modifier != OMP_TARGET)
37478 		n = 3;
37479 	      else
37480 		{
37481 		  location_t loc
37482 		    = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
37483 		  error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
37484 				 "or %<exit%>");
37485 		  if_modifier = ERROR_MARK;
37486 		}
37487 	      if (if_modifier == OMP_TARGET_ENTER_DATA
37488 		  || if_modifier == OMP_TARGET_EXIT_DATA)
37489 		{
37490 		  if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
37491 		    {
37492 		      id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
37493 		      p = IDENTIFIER_POINTER (id);
37494 		      if (strcmp ("data", p) == 0)
37495 			n = 4;
37496 		    }
37497 		  if (n != 4)
37498 		    {
37499 		      location_t loc
37500 			= cp_lexer_peek_nth_token (parser->lexer, 3)->location;
37501 		      error_at (loc, "expected %<data%>");
37502 		      if_modifier = ERROR_MARK;
37503 		    }
37504 		}
37505 	    }
37506 	}
37507       if (if_modifier != ERROR_MARK)
37508 	{
37509 	  if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
37510 	    {
37511 	      while (n-- > 0)
37512 		cp_lexer_consume_token (parser->lexer);
37513 	    }
37514 	  else
37515 	    {
37516 	      if (n > 2)
37517 		{
37518 		  location_t loc
37519 		    = cp_lexer_peek_nth_token (parser->lexer, n)->location;
37520 		  error_at (loc, "expected %<:%>");
37521 		}
37522 	      if_modifier = ERROR_MARK;
37523 	    }
37524 	}
37525     }
37526 
37527   t = cp_parser_assignment_expression (parser);
37528 
37529   if (t == error_mark_node
37530       || !parens.require_close (parser))
37531     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37532 					   /*or_comma=*/false,
37533 					   /*consume_paren=*/true);
37534 
37535   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37536     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
37537       {
37538 	if (if_modifier != ERROR_MARK
37539 	    && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37540 	  {
37541 	    const char *p = NULL;
37542 	    switch (if_modifier)
37543 	      {
37544 	      case VOID_CST: p = "cancel"; break;
37545 	      case OMP_PARALLEL: p = "parallel"; break;
37546 	      case OMP_SIMD: p = "simd"; break;
37547 	      case OMP_TASK: p = "task"; break;
37548 	      case OMP_TASKLOOP: p = "taskloop"; break;
37549 	      case OMP_TARGET_DATA: p = "target data"; break;
37550 	      case OMP_TARGET: p = "target"; break;
37551 	      case OMP_TARGET_UPDATE: p = "target update"; break;
37552 	      case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
37553 	      case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
37554 	      default: gcc_unreachable ();
37555 	      }
37556 	    error_at (location, "too many %<if%> clauses with %qs modifier",
37557 		      p);
37558 	    return list;
37559 	  }
37560 	else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37561 	  {
37562 	    if (!is_omp)
37563 	      error_at (location, "too many %<if%> clauses");
37564 	    else
37565 	      error_at (location, "too many %<if%> clauses without modifier");
37566 	    return list;
37567 	  }
37568 	else if (if_modifier == ERROR_MARK
37569 		 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
37570 	  {
37571 	    error_at (location, "if any %<if%> clause has modifier, then all "
37572 				"%<if%> clauses have to use modifier");
37573 	    return list;
37574 	  }
37575       }
37576 
37577   c = build_omp_clause (location, OMP_CLAUSE_IF);
37578   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
37579   OMP_CLAUSE_IF_EXPR (c) = t;
37580   OMP_CLAUSE_CHAIN (c) = list;
37581 
37582   return c;
37583 }
37584 
37585 /* OpenMP 3.1:
37586    mergeable */
37587 
37588 static tree
cp_parser_omp_clause_mergeable(cp_parser *,tree list,location_t location)37589 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
37590 				tree list, location_t location)
37591 {
37592   tree c;
37593 
37594   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
37595 			     location);
37596 
37597   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
37598   OMP_CLAUSE_CHAIN (c) = list;
37599   return c;
37600 }
37601 
37602 /* OpenMP 2.5:
37603    nowait */
37604 
37605 static tree
cp_parser_omp_clause_nowait(cp_parser *,tree list,location_t location)37606 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
37607 			     tree list, location_t location)
37608 {
37609   tree c;
37610 
37611   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
37612 
37613   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
37614   OMP_CLAUSE_CHAIN (c) = list;
37615   return c;
37616 }
37617 
37618 /* OpenMP 2.5:
37619    num_threads ( expression ) */
37620 
37621 static tree
cp_parser_omp_clause_num_threads(cp_parser * parser,tree list,location_t location)37622 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
37623 				  location_t location)
37624 {
37625   tree t, c;
37626 
37627   matching_parens parens;
37628   if (!parens.require_open (parser))
37629     return list;
37630 
37631   t = cp_parser_assignment_expression (parser);
37632 
37633   if (t == error_mark_node
37634       || !parens.require_close (parser))
37635     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37636 					   /*or_comma=*/false,
37637 					   /*consume_paren=*/true);
37638 
37639   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
37640 			     "num_threads", location);
37641 
37642   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
37643   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
37644   OMP_CLAUSE_CHAIN (c) = list;
37645 
37646   return c;
37647 }
37648 
37649 /* OpenMP 4.5:
37650    num_tasks ( expression )
37651 
37652    OpenMP 5.1:
37653    num_tasks ( strict : expression ) */
37654 
37655 static tree
cp_parser_omp_clause_num_tasks(cp_parser * parser,tree list,location_t location)37656 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
37657 				location_t location)
37658 {
37659   tree t, c;
37660 
37661   matching_parens parens;
37662   if (!parens.require_open (parser))
37663     return list;
37664 
37665   bool strict = false;
37666   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37667       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37668     {
37669       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37670       if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37671 	{
37672 	  strict = true;
37673 	  cp_lexer_consume_token (parser->lexer);
37674 	  cp_lexer_consume_token (parser->lexer);
37675 	}
37676     }
37677 
37678   t = cp_parser_assignment_expression (parser);
37679 
37680   if (t == error_mark_node
37681       || !parens.require_close (parser))
37682     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37683 					   /*or_comma=*/false,
37684 					   /*consume_paren=*/true);
37685 
37686   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
37687 			     "num_tasks", location);
37688 
37689   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
37690   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
37691   OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
37692   OMP_CLAUSE_CHAIN (c) = list;
37693 
37694   return c;
37695 }
37696 
37697 /* OpenMP 4.5:
37698    grainsize ( expression )
37699 
37700    OpenMP 5.1:
37701    grainsize ( strict : expression ) */
37702 
37703 static tree
cp_parser_omp_clause_grainsize(cp_parser * parser,tree list,location_t location)37704 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
37705 				location_t location)
37706 {
37707   tree t, c;
37708 
37709   matching_parens parens;
37710   if (!parens.require_open (parser))
37711     return list;
37712 
37713   bool strict = false;
37714   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37715       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37716     {
37717       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37718       if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37719 	{
37720 	  strict = true;
37721 	  cp_lexer_consume_token (parser->lexer);
37722 	  cp_lexer_consume_token (parser->lexer);
37723 	}
37724     }
37725 
37726   t = cp_parser_assignment_expression (parser);
37727 
37728   if (t == error_mark_node
37729       || !parens.require_close (parser))
37730     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37731 					   /*or_comma=*/false,
37732 					   /*consume_paren=*/true);
37733 
37734   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
37735 			     "grainsize", location);
37736 
37737   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
37738   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
37739   OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
37740   OMP_CLAUSE_CHAIN (c) = list;
37741 
37742   return c;
37743 }
37744 
37745 /* OpenMP 4.5:
37746    priority ( expression ) */
37747 
37748 static tree
cp_parser_omp_clause_priority(cp_parser * parser,tree list,location_t location)37749 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
37750 			       location_t location)
37751 {
37752   tree t, c;
37753 
37754   matching_parens parens;
37755   if (!parens.require_open (parser))
37756     return list;
37757 
37758   t = cp_parser_assignment_expression (parser);
37759 
37760   if (t == error_mark_node
37761       || !parens.require_close (parser))
37762     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37763 					   /*or_comma=*/false,
37764 					   /*consume_paren=*/true);
37765 
37766   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
37767 			     "priority", location);
37768 
37769   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
37770   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
37771   OMP_CLAUSE_CHAIN (c) = list;
37772 
37773   return c;
37774 }
37775 
37776 /* OpenMP 4.5:
37777    hint ( expression ) */
37778 
37779 static tree
cp_parser_omp_clause_hint(cp_parser * parser,tree list,location_t location)37780 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
37781 {
37782   tree t, c;
37783 
37784   matching_parens parens;
37785   if (!parens.require_open (parser))
37786     return list;
37787 
37788   t = cp_parser_assignment_expression (parser);
37789 
37790   if (t != error_mark_node)
37791     {
37792       t = fold_non_dependent_expr (t);
37793       if (!value_dependent_expression_p (t)
37794 	  && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
37795 	      || !tree_fits_shwi_p (t)
37796 	      || tree_int_cst_sgn (t) == -1))
37797 	error_at (location, "expected constant integer expression with "
37798 			    "valid sync-hint value");
37799     }
37800   if (t == error_mark_node
37801       || !parens.require_close (parser))
37802     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37803 					   /*or_comma=*/false,
37804 					   /*consume_paren=*/true);
37805   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
37806 
37807   c = build_omp_clause (location, OMP_CLAUSE_HINT);
37808   OMP_CLAUSE_HINT_EXPR (c) = t;
37809   OMP_CLAUSE_CHAIN (c) = list;
37810 
37811   return c;
37812 }
37813 
37814 /* OpenMP 5.1:
37815    filter ( integer-expression ) */
37816 
37817 static tree
cp_parser_omp_clause_filter(cp_parser * parser,tree list,location_t location)37818 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
37819 {
37820   tree t, c;
37821 
37822   matching_parens parens;
37823   if (!parens.require_open (parser))
37824     return list;
37825 
37826   t = cp_parser_assignment_expression (parser);
37827 
37828   if (t == error_mark_node
37829       || !parens.require_close (parser))
37830     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37831 					   /*or_comma=*/false,
37832 					   /*consume_paren=*/true);
37833   check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
37834 
37835   c = build_omp_clause (location, OMP_CLAUSE_FILTER);
37836   OMP_CLAUSE_FILTER_EXPR (c) = t;
37837   OMP_CLAUSE_CHAIN (c) = list;
37838 
37839   return c;
37840 }
37841 
37842 /* OpenMP 4.5:
37843    defaultmap ( tofrom : scalar )
37844 
37845    OpenMP 5.0:
37846    defaultmap ( implicit-behavior [ : variable-category ] ) */
37847 
37848 static tree
cp_parser_omp_clause_defaultmap(cp_parser * parser,tree list,location_t location)37849 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
37850 				 location_t location)
37851 {
37852   tree c, id;
37853   const char *p;
37854   enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37855   enum omp_clause_defaultmap_kind category
37856     = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
37857 
37858   matching_parens parens;
37859   if (!parens.require_open (parser))
37860     return list;
37861 
37862   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37863     p = "default";
37864   else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37865     {
37866     invalid_behavior:
37867       cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
37868 			       "%<tofrom%>, %<firstprivate%>, %<none%> "
37869 			       "or %<default%>");
37870       goto out_err;
37871     }
37872   else
37873     {
37874       id = cp_lexer_peek_token (parser->lexer)->u.value;
37875       p = IDENTIFIER_POINTER (id);
37876     }
37877 
37878   switch (p[0])
37879     {
37880     case 'a':
37881       if (strcmp ("alloc", p) == 0)
37882 	behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
37883       else
37884 	goto invalid_behavior;
37885       break;
37886 
37887     case 'd':
37888       if (strcmp ("default", p) == 0)
37889 	behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
37890       else
37891 	goto invalid_behavior;
37892       break;
37893 
37894     case 'f':
37895       if (strcmp ("firstprivate", p) == 0)
37896 	behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
37897       else if (strcmp ("from", p) == 0)
37898 	behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
37899       else
37900 	goto invalid_behavior;
37901       break;
37902 
37903     case 'n':
37904       if (strcmp ("none", p) == 0)
37905 	behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
37906       else
37907 	goto invalid_behavior;
37908       break;
37909 
37910     case 't':
37911       if (strcmp ("tofrom", p) == 0)
37912 	behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
37913       else if (strcmp ("to", p) == 0)
37914 	behavior = OMP_CLAUSE_DEFAULTMAP_TO;
37915       else
37916 	goto invalid_behavior;
37917       break;
37918 
37919     default:
37920       goto invalid_behavior;
37921     }
37922   cp_lexer_consume_token (parser->lexer);
37923 
37924   if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37925     {
37926       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37927 	goto out_err;
37928 
37929       if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37930 	{
37931 	invalid_category:
37932 	  cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
37933 				   "%<pointer%>");
37934 	  goto out_err;
37935 	}
37936       id = cp_lexer_peek_token (parser->lexer)->u.value;
37937       p = IDENTIFIER_POINTER (id);
37938 
37939       switch (p[0])
37940 	{
37941 	case 'a':
37942 	  if (strcmp ("aggregate", p) == 0)
37943 	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
37944 	  else
37945 	    goto invalid_category;
37946 	  break;
37947 
37948 	case 'p':
37949 	  if (strcmp ("pointer", p) == 0)
37950 	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
37951 	  else
37952 	    goto invalid_category;
37953 	  break;
37954 
37955 	case 's':
37956 	  if (strcmp ("scalar", p) == 0)
37957 	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
37958 	  else
37959 	    goto invalid_category;
37960 	  break;
37961 
37962 	default:
37963 	  goto invalid_category;
37964 	}
37965 
37966       cp_lexer_consume_token (parser->lexer);
37967     }
37968   if (!parens.require_close (parser))
37969     goto out_err;
37970 
37971   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37972     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
37973 	&& (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
37974 	    || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
37975 	    || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
37976 		== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
37977       {
37978 	enum omp_clause_defaultmap_kind cat = category;
37979 	location_t loc = OMP_CLAUSE_LOCATION (c);
37980 	if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
37981 	  cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
37982 	p = NULL;
37983 	switch (cat)
37984 	  {
37985 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
37986 	    p = NULL;
37987 	    break;
37988 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
37989 	    p = "aggregate";
37990 	    break;
37991 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
37992 	    p = "pointer";
37993 	    break;
37994 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
37995 	    p = "scalar";
37996 	    break;
37997 	  default:
37998 	    gcc_unreachable ();
37999 	  }
38000 	if (p)
38001 	  error_at (loc, "too many %<defaultmap%> clauses with %qs category",
38002 		    p);
38003 	else
38004 	  error_at (loc, "too many %<defaultmap%> clauses with unspecified "
38005 			 "category");
38006 	break;
38007       }
38008 
38009   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
38010   OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
38011   OMP_CLAUSE_CHAIN (c) = list;
38012   return c;
38013 
38014  out_err:
38015   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38016 					 /*or_comma=*/false,
38017 					 /*consume_paren=*/true);
38018   return list;
38019 }
38020 
38021 /* OpenMP 5.0:
38022    order ( concurrent )
38023 
38024    OpenMP 5.1:
38025    order ( order-modifier : concurrent )
38026 
38027    order-modifier:
38028      reproducible
38029      unconstrained  */
38030 
38031 static tree
cp_parser_omp_clause_order(cp_parser * parser,tree list,location_t location)38032 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
38033 {
38034   tree c, id;
38035   const char *p;
38036   bool unconstrained = false;
38037   bool reproducible = false;
38038 
38039   matching_parens parens;
38040   if (!parens.require_open (parser))
38041     return list;
38042 
38043   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38044       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38045     {
38046       id = cp_lexer_peek_token (parser->lexer)->u.value;
38047       p = IDENTIFIER_POINTER (id);
38048       if (strcmp (p, "unconstrained") == 0)
38049 	unconstrained = true;
38050       else if (strcmp (p, "reproducible") == 0)
38051 	reproducible = true;
38052       else
38053 	{
38054 	  cp_parser_error (parser, "expected %<reproducible%> or "
38055 				   "%<unconstrained%>");
38056 	  goto out_err;
38057 	}
38058       cp_lexer_consume_token (parser->lexer);
38059       cp_lexer_consume_token (parser->lexer);
38060     }
38061   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38062     {
38063       cp_parser_error (parser, "expected %<concurrent%>");
38064       goto out_err;
38065     }
38066   else
38067     {
38068       id = cp_lexer_peek_token (parser->lexer)->u.value;
38069       p = IDENTIFIER_POINTER (id);
38070     }
38071   if (strcmp (p, "concurrent") != 0)
38072     {
38073       cp_parser_error (parser, "expected %<concurrent%>");
38074       goto out_err;
38075     }
38076   cp_lexer_consume_token (parser->lexer);
38077   if (!parens.require_close (parser))
38078     goto out_err;
38079 
38080   check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
38081   c = build_omp_clause (location, OMP_CLAUSE_ORDER);
38082   OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
38083   OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
38084   OMP_CLAUSE_CHAIN (c) = list;
38085   return c;
38086 
38087  out_err:
38088   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38089 					 /*or_comma=*/false,
38090 					 /*consume_paren=*/true);
38091   return list;
38092 }
38093 
38094 /* OpenMP 5.0:
38095    bind ( teams | parallel | thread ) */
38096 
38097 static tree
cp_parser_omp_clause_bind(cp_parser * parser,tree list,location_t location)38098 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
38099 			   location_t location)
38100 {
38101   tree c;
38102   const char *p;
38103   enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
38104 
38105   matching_parens parens;
38106   if (!parens.require_open (parser))
38107     return list;
38108 
38109   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38110     {
38111     invalid:
38112       cp_parser_error (parser,
38113 		       "expected %<teams%>, %<parallel%> or %<thread%>");
38114       goto out_err;
38115     }
38116   else
38117     {
38118       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38119       p = IDENTIFIER_POINTER (id);
38120     }
38121   if (strcmp (p, "teams") == 0)
38122     kind = OMP_CLAUSE_BIND_TEAMS;
38123   else if (strcmp (p, "parallel") == 0)
38124     kind = OMP_CLAUSE_BIND_PARALLEL;
38125   else if (strcmp (p, "thread") != 0)
38126     goto invalid;
38127   cp_lexer_consume_token (parser->lexer);
38128   if (!parens.require_close (parser))
38129     goto out_err;
38130 
38131   /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
38132   c = build_omp_clause (location, OMP_CLAUSE_BIND);
38133   OMP_CLAUSE_BIND_KIND (c) = kind;
38134   OMP_CLAUSE_CHAIN (c) = list;
38135   return c;
38136 
38137  out_err:
38138   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38139 					 /*or_comma=*/false,
38140 					 /*consume_paren=*/true);
38141   return list;
38142 }
38143 
38144 /* OpenMP 2.5:
38145    ordered
38146 
38147    OpenMP 4.5:
38148    ordered ( constant-expression ) */
38149 
38150 static tree
cp_parser_omp_clause_ordered(cp_parser * parser,tree list,location_t location)38151 cp_parser_omp_clause_ordered (cp_parser *parser,
38152 			      tree list, location_t location)
38153 {
38154   tree c, num = NULL_TREE;
38155   HOST_WIDE_INT n;
38156 
38157   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
38158 			     "ordered", location);
38159 
38160   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38161     {
38162       matching_parens parens;
38163       parens.consume_open (parser);
38164 
38165       num = cp_parser_constant_expression (parser);
38166 
38167       if (!parens.require_close (parser))
38168 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38169 					       /*or_comma=*/false,
38170 					       /*consume_paren=*/true);
38171 
38172       if (num == error_mark_node)
38173 	return list;
38174       num = fold_non_dependent_expr (num);
38175       if (!tree_fits_shwi_p (num)
38176 	  || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38177 	  || (n = tree_to_shwi (num)) <= 0
38178 	  || (int) n != n)
38179 	{
38180 	  error_at (location,
38181 		    "ordered argument needs positive constant integer "
38182 		    "expression");
38183 	  return list;
38184 	}
38185     }
38186 
38187   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
38188   OMP_CLAUSE_ORDERED_EXPR (c) = num;
38189   OMP_CLAUSE_CHAIN (c) = list;
38190   return c;
38191 }
38192 
38193 /* OpenMP 2.5:
38194    reduction ( reduction-operator : variable-list )
38195 
38196    reduction-operator:
38197      One of: + * - & ^ | && ||
38198 
38199    OpenMP 3.1:
38200 
38201    reduction-operator:
38202      One of: + * - & ^ | && || min max
38203 
38204    OpenMP 4.0:
38205 
38206    reduction-operator:
38207      One of: + * - & ^ | && ||
38208      id-expression
38209 
38210    OpenMP 5.0:
38211    reduction ( reduction-modifier, reduction-operator : variable-list )
38212    in_reduction ( reduction-operator : variable-list )
38213    task_reduction ( reduction-operator : variable-list )  */
38214 
38215 static tree
cp_parser_omp_clause_reduction(cp_parser * parser,enum omp_clause_code kind,bool is_omp,tree list)38216 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
38217 				bool is_omp, tree list)
38218 {
38219   enum tree_code code = ERROR_MARK;
38220   tree nlist, c, id = NULL_TREE;
38221   bool task = false;
38222   bool inscan = false;
38223 
38224   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38225     return list;
38226 
38227   if (kind == OMP_CLAUSE_REDUCTION && is_omp)
38228     {
38229       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
38230 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38231 	{
38232 	  cp_lexer_consume_token (parser->lexer);
38233 	  cp_lexer_consume_token (parser->lexer);
38234 	}
38235       else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38236 	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38237 	{
38238 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38239 	  const char *p = IDENTIFIER_POINTER (id);
38240 	  if (strcmp (p, "task") == 0)
38241 	    task = true;
38242 	  else if (strcmp (p, "inscan") == 0)
38243 	    inscan = true;
38244 	  if (task || inscan)
38245 	    {
38246 	      cp_lexer_consume_token (parser->lexer);
38247 	      cp_lexer_consume_token (parser->lexer);
38248 	    }
38249 	}
38250     }
38251 
38252   switch (cp_lexer_peek_token (parser->lexer)->type)
38253     {
38254     case CPP_PLUS: code = PLUS_EXPR; break;
38255     case CPP_MULT: code = MULT_EXPR; break;
38256     case CPP_MINUS: code = MINUS_EXPR; break;
38257     case CPP_AND: code = BIT_AND_EXPR; break;
38258     case CPP_XOR: code = BIT_XOR_EXPR; break;
38259     case CPP_OR: code = BIT_IOR_EXPR; break;
38260     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
38261     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
38262     default: break;
38263     }
38264 
38265   if (code != ERROR_MARK)
38266     cp_lexer_consume_token (parser->lexer);
38267   else
38268     {
38269       bool saved_colon_corrects_to_scope_p;
38270       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38271       parser->colon_corrects_to_scope_p = false;
38272       id = cp_parser_id_expression (parser, /*template_p=*/false,
38273 				    /*check_dependency_p=*/true,
38274 				    /*template_p=*/NULL,
38275 				    /*declarator_p=*/false,
38276 				    /*optional_p=*/false);
38277       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38278       if (identifier_p (id))
38279 	{
38280 	  const char *p = IDENTIFIER_POINTER (id);
38281 
38282 	  if (strcmp (p, "min") == 0)
38283 	    code = MIN_EXPR;
38284 	  else if (strcmp (p, "max") == 0)
38285 	    code = MAX_EXPR;
38286 	  else if (id == ovl_op_identifier (false, PLUS_EXPR))
38287 	    code = PLUS_EXPR;
38288 	  else if (id == ovl_op_identifier (false, MULT_EXPR))
38289 	    code = MULT_EXPR;
38290 	  else if (id == ovl_op_identifier (false, MINUS_EXPR))
38291 	    code = MINUS_EXPR;
38292 	  else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
38293 	    code = BIT_AND_EXPR;
38294 	  else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
38295 	    code = BIT_IOR_EXPR;
38296 	  else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
38297 	    code = BIT_XOR_EXPR;
38298 	  else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
38299 	    code = TRUTH_ANDIF_EXPR;
38300 	  else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
38301 	    code = TRUTH_ORIF_EXPR;
38302 	  id = omp_reduction_id (code, id, NULL_TREE);
38303 	  tree scope = parser->scope;
38304 	  if (scope)
38305 	    id = build_qualified_name (NULL_TREE, scope, id, false);
38306 	  parser->scope = NULL_TREE;
38307 	  parser->qualifying_scope = NULL_TREE;
38308 	  parser->object_scope = NULL_TREE;
38309 	}
38310       else
38311 	{
38312 	  error ("invalid reduction-identifier");
38313 	 resync_fail:
38314 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38315 						 /*or_comma=*/false,
38316 						 /*consume_paren=*/true);
38317 	  return list;
38318 	}
38319     }
38320 
38321   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38322     goto resync_fail;
38323 
38324   nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
38325 					  NULL);
38326   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38327     {
38328       OMP_CLAUSE_REDUCTION_CODE (c) = code;
38329       if (task)
38330 	OMP_CLAUSE_REDUCTION_TASK (c) = 1;
38331       else if (inscan)
38332 	OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
38333       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
38334     }
38335 
38336   return nlist;
38337 }
38338 
38339 /* OpenMP 2.5:
38340    schedule ( schedule-kind )
38341    schedule ( schedule-kind , expression )
38342 
38343    schedule-kind:
38344      static | dynamic | guided | runtime | auto
38345 
38346    OpenMP 4.5:
38347    schedule ( schedule-modifier : schedule-kind )
38348    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38349 
38350    schedule-modifier:
38351      simd
38352      monotonic
38353      nonmonotonic  */
38354 
38355 static tree
cp_parser_omp_clause_schedule(cp_parser * parser,tree list,location_t location)38356 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
38357 {
38358   tree c, t;
38359   int modifiers = 0, nmodifiers = 0;
38360 
38361   matching_parens parens;
38362   if (!parens.require_open (parser))
38363     return list;
38364 
38365   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
38366 
38367   location_t comma = UNKNOWN_LOCATION;
38368   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38369     {
38370       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38371       const char *p = IDENTIFIER_POINTER (id);
38372       if (strcmp ("simd", p) == 0)
38373 	OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
38374       else if (strcmp ("monotonic", p) == 0)
38375 	modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
38376       else if (strcmp ("nonmonotonic", p) == 0)
38377 	modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
38378       else
38379 	break;
38380       comma = UNKNOWN_LOCATION;
38381       cp_lexer_consume_token (parser->lexer);
38382       if (nmodifiers++ == 0
38383 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38384 	{
38385 	  comma = cp_lexer_peek_token (parser->lexer)->location;
38386 	  cp_lexer_consume_token (parser->lexer);
38387 	}
38388       else
38389 	{
38390 	  cp_parser_require (parser, CPP_COLON, RT_COLON);
38391 	  break;
38392 	}
38393     }
38394   if (comma != UNKNOWN_LOCATION)
38395     error_at (comma, "expected %<:%>");
38396 
38397   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38398     {
38399       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38400       const char *p = IDENTIFIER_POINTER (id);
38401 
38402       switch (p[0])
38403 	{
38404 	case 'd':
38405 	  if (strcmp ("dynamic", p) != 0)
38406 	    goto invalid_kind;
38407 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
38408 	  break;
38409 
38410 	case 'g':
38411 	  if (strcmp ("guided", p) != 0)
38412 	    goto invalid_kind;
38413 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
38414 	  break;
38415 
38416 	case 'r':
38417 	  if (strcmp ("runtime", p) != 0)
38418 	    goto invalid_kind;
38419 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
38420 	  break;
38421 
38422 	default:
38423 	  goto invalid_kind;
38424 	}
38425     }
38426   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
38427     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
38428   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
38429     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
38430   else
38431     goto invalid_kind;
38432   cp_lexer_consume_token (parser->lexer);
38433 
38434   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
38435 		    | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38436       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38437 	  | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38438     {
38439       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38440 			  "specified");
38441       modifiers = 0;
38442     }
38443 
38444   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38445     {
38446       cp_token *token;
38447       cp_lexer_consume_token (parser->lexer);
38448 
38449       token = cp_lexer_peek_token (parser->lexer);
38450       t = cp_parser_assignment_expression (parser);
38451 
38452       if (t == error_mark_node)
38453 	goto resync_fail;
38454       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
38455 	error_at (token->location, "schedule %<runtime%> does not take "
38456 		  "a %<chunk_size%> parameter");
38457       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
38458 	error_at (token->location, "schedule %<auto%> does not take "
38459 		  "a %<chunk_size%> parameter");
38460       else
38461 	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
38462 
38463       if (!parens.require_close (parser))
38464 	goto resync_fail;
38465     }
38466   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
38467     goto resync_fail;
38468 
38469   OMP_CLAUSE_SCHEDULE_KIND (c)
38470     = (enum omp_clause_schedule_kind)
38471       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
38472 
38473   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
38474   OMP_CLAUSE_CHAIN (c) = list;
38475   return c;
38476 
38477  invalid_kind:
38478   cp_parser_error (parser, "invalid schedule kind");
38479  resync_fail:
38480   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38481 					 /*or_comma=*/false,
38482 					 /*consume_paren=*/true);
38483   return list;
38484 }
38485 
38486 /* OpenMP 3.0:
38487    untied */
38488 
38489 static tree
cp_parser_omp_clause_untied(cp_parser *,tree list,location_t location)38490 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
38491 			     tree list, location_t location)
38492 {
38493   tree c;
38494 
38495   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
38496 
38497   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
38498   OMP_CLAUSE_CHAIN (c) = list;
38499   return c;
38500 }
38501 
38502 /* OpenMP 4.0:
38503    inbranch
38504    notinbranch */
38505 
38506 static tree
cp_parser_omp_clause_branch(cp_parser *,enum omp_clause_code code,tree list,location_t location)38507 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
38508 			     tree list, location_t location)
38509 {
38510   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38511   tree c = build_omp_clause (location, code);
38512   OMP_CLAUSE_CHAIN (c) = list;
38513   return c;
38514 }
38515 
38516 /* OpenMP 4.0:
38517    parallel
38518    for
38519    sections
38520    taskgroup */
38521 
38522 static tree
cp_parser_omp_clause_cancelkind(cp_parser *,enum omp_clause_code code,tree list,location_t location)38523 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
38524 				 enum omp_clause_code code,
38525 				 tree list, location_t location)
38526 {
38527   tree c = build_omp_clause (location, code);
38528   OMP_CLAUSE_CHAIN (c) = list;
38529   return c;
38530 }
38531 
38532 /* OpenMP 4.5:
38533    nogroup */
38534 
38535 static tree
cp_parser_omp_clause_nogroup(cp_parser *,tree list,location_t location)38536 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
38537 			      tree list, location_t location)
38538 {
38539   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
38540   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
38541   OMP_CLAUSE_CHAIN (c) = list;
38542   return c;
38543 }
38544 
38545 /* OpenMP 4.5:
38546    simd
38547    threads */
38548 
38549 static tree
cp_parser_omp_clause_orderedkind(cp_parser *,enum omp_clause_code code,tree list,location_t location)38550 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
38551 				  enum omp_clause_code code,
38552 				  tree list, location_t location)
38553 {
38554   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38555   tree c = build_omp_clause (location, code);
38556   OMP_CLAUSE_CHAIN (c) = list;
38557   return c;
38558 }
38559 
38560 /* OpenMP 4.0:
38561    num_teams ( expression )
38562 
38563    OpenMP 5.1:
38564    num_teams ( expression : expression ) */
38565 
38566 static tree
cp_parser_omp_clause_num_teams(cp_parser * parser,tree list,location_t location)38567 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
38568 				location_t location)
38569 {
38570   tree upper, lower = NULL_TREE, c;
38571 
38572   matching_parens parens;
38573   if (!parens.require_open (parser))
38574     return list;
38575 
38576   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38577   parser->colon_corrects_to_scope_p = false;
38578   upper = cp_parser_assignment_expression (parser);
38579   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38580 
38581   if (upper != error_mark_node
38582       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38583     {
38584       lower = upper;
38585       cp_lexer_consume_token (parser->lexer);
38586       upper = cp_parser_assignment_expression (parser);
38587     }
38588 
38589   if (upper == error_mark_node
38590       || !parens.require_close (parser))
38591     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38592 					   /*or_comma=*/false,
38593 					   /*consume_paren=*/true);
38594 
38595   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
38596 			     "num_teams", location);
38597 
38598   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
38599   OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
38600   OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
38601   OMP_CLAUSE_CHAIN (c) = list;
38602 
38603   return c;
38604 }
38605 
38606 /* OpenMP 4.0:
38607    thread_limit ( expression ) */
38608 
38609 static tree
cp_parser_omp_clause_thread_limit(cp_parser * parser,tree list,location_t location)38610 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
38611 				   location_t location)
38612 {
38613   tree t, c;
38614 
38615   matching_parens parens;
38616   if (!parens.require_open (parser))
38617     return list;
38618 
38619   t = cp_parser_assignment_expression (parser);
38620 
38621   if (t == error_mark_node
38622       || !parens.require_close (parser))
38623     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38624 					   /*or_comma=*/false,
38625 					   /*consume_paren=*/true);
38626 
38627   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
38628 			     "thread_limit", location);
38629 
38630   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
38631   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
38632   OMP_CLAUSE_CHAIN (c) = list;
38633 
38634   return c;
38635 }
38636 
38637 /* OpenMP 4.0:
38638    aligned ( variable-list )
38639    aligned ( variable-list : constant-expression )  */
38640 
38641 static tree
cp_parser_omp_clause_aligned(cp_parser * parser,tree list)38642 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
38643 {
38644   tree nlist, c, alignment = NULL_TREE;
38645   bool colon;
38646 
38647   matching_parens parens;
38648   if (!parens.require_open (parser))
38649     return list;
38650 
38651   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
38652 					  &colon);
38653 
38654   if (colon)
38655     {
38656       alignment = cp_parser_constant_expression (parser);
38657 
38658       if (!parens.require_close (parser))
38659 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38660 					       /*or_comma=*/false,
38661 					       /*consume_paren=*/true);
38662 
38663       if (alignment == error_mark_node)
38664 	alignment = NULL_TREE;
38665     }
38666 
38667   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38668     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
38669 
38670   return nlist;
38671 }
38672 
38673 /* OpenMP 5.0:
38674    allocate ( variable-list )
38675    allocate ( expression : variable-list )
38676 
38677    OpenMP 5.1:
38678    allocate ( allocator-modifier : variable-list )
38679    allocate ( allocator-modifier , allocator-modifier : variable-list )
38680 
38681    allocator-modifier:
38682    allocator ( expression )
38683    align ( expression )  */
38684 
38685 static tree
cp_parser_omp_clause_allocate(cp_parser * parser,tree list)38686 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
38687 {
38688   tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
38689   bool colon, has_modifiers = false;
38690 
38691   matching_parens parens;
38692   if (!parens.require_open (parser))
38693     return list;
38694 
38695   cp_parser_parse_tentatively (parser);
38696   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38697   parser->colon_corrects_to_scope_p = false;
38698   for (int mod = 0; mod < 2; mod++)
38699     if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38700 	&& cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38701       {
38702 	tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38703 	const char *p = IDENTIFIER_POINTER (id);
38704 	if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
38705 	  break;
38706 	cp_lexer_consume_token (parser->lexer);
38707 	matching_parens parens2;
38708 	if (!parens2.require_open (parser))
38709 	  break;
38710 	if (strcmp (p, "allocator") == 0)
38711 	  {
38712 	    if (allocator != NULL_TREE)
38713 	      break;
38714 	    allocator = cp_parser_assignment_expression (parser);
38715 	  }
38716 	else
38717 	  {
38718 	    if (align != NULL_TREE)
38719 	      break;
38720 	    align = cp_parser_assignment_expression (parser);
38721 	  }
38722 	if (!parens2.require_close (parser))
38723 	  break;
38724 	if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38725 	  {
38726 	    has_modifiers = true;
38727 	    break;
38728 	  }
38729 	if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38730 	  break;
38731 	cp_lexer_consume_token (parser->lexer);
38732       }
38733     else
38734       break;
38735   if (!has_modifiers)
38736     {
38737       cp_parser_abort_tentative_parse (parser);
38738       align = NULL_TREE;
38739       allocator = NULL_TREE;
38740       cp_parser_parse_tentatively (parser);
38741       allocator = cp_parser_assignment_expression (parser);
38742     }
38743   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38744   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38745     {
38746       cp_parser_parse_definitely (parser);
38747       cp_lexer_consume_token (parser->lexer);
38748       if (allocator == error_mark_node)
38749 	allocator = NULL_TREE;
38750       if (align == error_mark_node)
38751 	align = NULL_TREE;
38752     }
38753   else
38754     {
38755       cp_parser_abort_tentative_parse (parser);
38756       allocator = NULL_TREE;
38757       align = NULL_TREE;
38758     }
38759 
38760   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
38761 					  &colon);
38762 
38763   if (allocator || align)
38764     for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38765       {
38766 	OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38767 	OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
38768       }
38769 
38770   return nlist;
38771 }
38772 
38773 /* OpenMP 2.5:
38774    lastprivate ( variable-list )
38775 
38776    OpenMP 5.0:
38777    lastprivate ( [ lastprivate-modifier : ] variable-list )  */
38778 
38779 static tree
cp_parser_omp_clause_lastprivate(cp_parser * parser,tree list)38780 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
38781 {
38782   bool conditional = false;
38783 
38784   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38785     return list;
38786 
38787   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38788       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38789     {
38790       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38791       const char *p = IDENTIFIER_POINTER (id);
38792 
38793       if (strcmp ("conditional", p) == 0)
38794 	{
38795 	  conditional = true;
38796 	  cp_lexer_consume_token (parser->lexer);
38797 	  cp_lexer_consume_token (parser->lexer);
38798 	}
38799     }
38800 
38801   tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
38802 					       list, NULL);
38803 
38804   if (conditional)
38805     for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38806       OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
38807   return nlist;
38808 }
38809 
38810 /* OpenMP 4.0:
38811    linear ( variable-list )
38812    linear ( variable-list : expression )
38813 
38814    OpenMP 4.5:
38815    linear ( modifier ( variable-list ) )
38816    linear ( modifier ( variable-list ) : expression ) */
38817 
38818 static tree
cp_parser_omp_clause_linear(cp_parser * parser,tree list,bool declare_simd)38819 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
38820 			     bool declare_simd)
38821 {
38822   tree nlist, c, step = integer_one_node;
38823   bool colon;
38824   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
38825 
38826   matching_parens parens;
38827   if (!parens.require_open (parser))
38828     return list;
38829 
38830   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38831     {
38832       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38833       const char *p = IDENTIFIER_POINTER (id);
38834 
38835       if (strcmp ("ref", p) == 0)
38836 	kind = OMP_CLAUSE_LINEAR_REF;
38837       else if (strcmp ("val", p) == 0)
38838 	kind = OMP_CLAUSE_LINEAR_VAL;
38839       else if (strcmp ("uval", p) == 0)
38840 	kind = OMP_CLAUSE_LINEAR_UVAL;
38841       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38842 	cp_lexer_consume_token (parser->lexer);
38843       else
38844 	kind = OMP_CLAUSE_LINEAR_DEFAULT;
38845     }
38846 
38847   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
38848     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
38849 					    &colon);
38850   else
38851     {
38852       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
38853       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
38854       if (colon)
38855 	cp_parser_require (parser, CPP_COLON, RT_COLON);
38856       else if (!parens.require_close (parser))
38857 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38858 					       /*or_comma=*/false,
38859 					       /*consume_paren=*/true);
38860     }
38861 
38862   if (colon)
38863     {
38864       step = NULL_TREE;
38865       if (declare_simd
38866 	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38867 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
38868 	{
38869 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
38870 	  cp_parser_parse_tentatively (parser);
38871 	  step = cp_parser_id_expression (parser, /*template_p=*/false,
38872 					  /*check_dependency_p=*/true,
38873 					  /*template_p=*/NULL,
38874 					  /*declarator_p=*/false,
38875 					  /*optional_p=*/false);
38876 	  if (step != error_mark_node)
38877 	    step = cp_parser_lookup_name_simple (parser, step, token->location);
38878 	  if (step == error_mark_node)
38879 	    {
38880 	      step = NULL_TREE;
38881 	      cp_parser_abort_tentative_parse (parser);
38882 	    }
38883 	  else if (!cp_parser_parse_definitely (parser))
38884 	    step = NULL_TREE;
38885 	}
38886       if (!step)
38887 	step = cp_parser_assignment_expression (parser);
38888 
38889       if (!parens.require_close (parser))
38890 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38891 					       /*or_comma=*/false,
38892 					       /*consume_paren=*/true);
38893 
38894       if (step == error_mark_node)
38895 	return list;
38896     }
38897 
38898   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38899     {
38900       OMP_CLAUSE_LINEAR_STEP (c) = step;
38901       OMP_CLAUSE_LINEAR_KIND (c) = kind;
38902     }
38903 
38904   return nlist;
38905 }
38906 
38907 /* OpenMP 4.0:
38908    safelen ( constant-expression )  */
38909 
38910 static tree
cp_parser_omp_clause_safelen(cp_parser * parser,tree list,location_t location)38911 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
38912 			      location_t location)
38913 {
38914   tree t, c;
38915 
38916   matching_parens parens;
38917   if (!parens.require_open (parser))
38918     return list;
38919 
38920   t = cp_parser_constant_expression (parser);
38921 
38922   if (t == error_mark_node
38923       || !parens.require_close (parser))
38924     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38925 					   /*or_comma=*/false,
38926 					   /*consume_paren=*/true);
38927 
38928   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
38929 
38930   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
38931   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
38932   OMP_CLAUSE_CHAIN (c) = list;
38933 
38934   return c;
38935 }
38936 
38937 /* OpenMP 4.0:
38938    simdlen ( constant-expression )  */
38939 
38940 static tree
cp_parser_omp_clause_simdlen(cp_parser * parser,tree list,location_t location)38941 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
38942 			      location_t location)
38943 {
38944   tree t, c;
38945 
38946   matching_parens parens;
38947   if (!parens.require_open (parser))
38948     return list;
38949 
38950   t = cp_parser_constant_expression (parser);
38951 
38952   if (t == error_mark_node
38953       || !parens.require_close (parser))
38954     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38955 					   /*or_comma=*/false,
38956 					   /*consume_paren=*/true);
38957 
38958   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
38959 
38960   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
38961   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
38962   OMP_CLAUSE_CHAIN (c) = list;
38963 
38964   return c;
38965 }
38966 
38967 /* OpenMP 4.5:
38968    vec:
38969      identifier [+/- integer]
38970      vec , identifier [+/- integer]
38971 */
38972 
38973 static tree
cp_parser_omp_clause_depend_sink(cp_parser * parser,location_t clause_loc,tree list)38974 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
38975 				  tree list)
38976 {
38977   tree vec = NULL;
38978 
38979   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38980     {
38981       cp_parser_error (parser, "expected identifier");
38982       return list;
38983     }
38984 
38985   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38986     {
38987       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
38988       tree t, identifier = cp_parser_identifier (parser);
38989       tree addend = NULL;
38990 
38991       if (identifier == error_mark_node)
38992 	t = error_mark_node;
38993       else
38994 	{
38995 	  t = cp_parser_lookup_name_simple
38996 		(parser, identifier,
38997 		 cp_lexer_peek_token (parser->lexer)->location);
38998 	  if (t == error_mark_node)
38999 	    cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
39000 					 id_loc);
39001 	}
39002 
39003       bool neg = false;
39004       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
39005 	neg = true;
39006       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
39007 	{
39008 	  addend = integer_zero_node;
39009 	  goto add_to_vector;
39010 	}
39011       cp_lexer_consume_token (parser->lexer);
39012 
39013       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
39014 	{
39015 	  cp_parser_error (parser, "expected integer");
39016 	  return list;
39017 	}
39018 
39019       addend = cp_lexer_peek_token (parser->lexer)->u.value;
39020       if (TREE_CODE (addend) != INTEGER_CST)
39021 	{
39022 	  cp_parser_error (parser, "expected integer");
39023 	  return list;
39024 	}
39025       cp_lexer_consume_token (parser->lexer);
39026 
39027     add_to_vector:
39028       if (t != error_mark_node)
39029 	{
39030 	  vec = tree_cons (addend, t, vec);
39031 	  if (neg)
39032 	    OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
39033 	}
39034 
39035       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
39036 	  || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39037 	break;
39038 
39039       cp_lexer_consume_token (parser->lexer);
39040     }
39041 
39042   if (vec)
39043     {
39044       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
39045       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
39046       OMP_CLAUSE_DECL (u) = nreverse (vec);
39047       OMP_CLAUSE_CHAIN (u) = list;
39048       return u;
39049     }
39050   return list;
39051 }
39052 
39053 /* OpenMP 5.0:
39054    detach ( event-handle ) */
39055 
39056 static tree
cp_parser_omp_clause_detach(cp_parser * parser,tree list)39057 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
39058 {
39059   matching_parens parens;
39060 
39061   if (!parens.require_open (parser))
39062     return list;
39063 
39064   cp_token *token;
39065   tree name, decl;
39066 
39067   token = cp_lexer_peek_token (parser->lexer);
39068   name = cp_parser_id_expression (parser, /*template_p=*/false,
39069 					  /*check_dependency_p=*/true,
39070 					  /*template_p=*/NULL,
39071 					  /*declarator_p=*/false,
39072 					  /*optional_p=*/false);
39073   if (name == error_mark_node)
39074     decl = error_mark_node;
39075   else
39076     {
39077       if (identifier_p (name))
39078 	decl = cp_parser_lookup_name_simple (parser, name, token->location);
39079       else
39080 	decl = name;
39081       if (decl == error_mark_node)
39082 	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
39083 				     token->location);
39084     }
39085 
39086   if (decl == error_mark_node
39087       || !parens.require_close (parser))
39088     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39089 					   /*or_comma=*/false,
39090 					   /*consume_paren=*/true);
39091 
39092   tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
39093   OMP_CLAUSE_DECL (u) = decl;
39094   OMP_CLAUSE_CHAIN (u) = list;
39095 
39096   return u;
39097 }
39098 
39099 /* OpenMP 5.0:
39100    iterators ( iterators-definition )
39101 
39102    iterators-definition:
39103      iterator-specifier
39104      iterator-specifier , iterators-definition
39105 
39106    iterator-specifier:
39107      identifier = range-specification
39108      iterator-type identifier = range-specification
39109 
39110    range-specification:
39111      begin : end
39112      begin : end : step  */
39113 
39114 static tree
cp_parser_omp_iterators(cp_parser * parser)39115 cp_parser_omp_iterators (cp_parser *parser)
39116 {
39117   tree ret = NULL_TREE, *last = &ret;
39118   cp_lexer_consume_token (parser->lexer);
39119 
39120   matching_parens parens;
39121   if (!parens.require_open (parser))
39122     return error_mark_node;
39123 
39124   bool saved_colon_corrects_to_scope_p
39125     = parser->colon_corrects_to_scope_p;
39126   bool saved_colon_doesnt_start_class_def_p
39127     = parser->colon_doesnt_start_class_def_p;
39128 
39129   do
39130     {
39131       tree iter_type;
39132       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39133 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
39134 	iter_type = integer_type_node;
39135       else
39136 	{
39137 	  const char *saved_message
39138 	    = parser->type_definition_forbidden_message;
39139 	  parser->type_definition_forbidden_message
39140 	    = G_("types may not be defined in iterator type");
39141 
39142 	  iter_type = cp_parser_type_id (parser);
39143 
39144 	  parser->type_definition_forbidden_message = saved_message;
39145 	}
39146 
39147       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39148       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39149 	{
39150 	  cp_parser_error (parser, "expected identifier");
39151 	  break;
39152 	}
39153 
39154       tree id = cp_parser_identifier (parser);
39155       if (id == error_mark_node)
39156 	break;
39157 
39158       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39159 	break;
39160 
39161       parser->colon_corrects_to_scope_p = false;
39162       parser->colon_doesnt_start_class_def_p = true;
39163       tree begin = cp_parser_assignment_expression (parser);
39164 
39165       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39166 	break;
39167 
39168       tree end = cp_parser_assignment_expression (parser);
39169 
39170       tree step = integer_one_node;
39171       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39172 	{
39173 	  cp_lexer_consume_token (parser->lexer);
39174 	  step = cp_parser_assignment_expression (parser);
39175 	}
39176 
39177       tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
39178       DECL_ARTIFICIAL (iter_var) = 1;
39179       DECL_CONTEXT (iter_var) = current_function_decl;
39180       pushdecl (iter_var);
39181 
39182       *last = make_tree_vec (6);
39183       TREE_VEC_ELT (*last, 0) = iter_var;
39184       TREE_VEC_ELT (*last, 1) = begin;
39185       TREE_VEC_ELT (*last, 2) = end;
39186       TREE_VEC_ELT (*last, 3) = step;
39187       last = &TREE_CHAIN (*last);
39188 
39189       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39190 	{
39191 	  cp_lexer_consume_token (parser->lexer);
39192 	  continue;
39193 	}
39194       break;
39195     }
39196   while (1);
39197 
39198   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39199   parser->colon_doesnt_start_class_def_p
39200     = saved_colon_doesnt_start_class_def_p;
39201 
39202   if (!parens.require_close (parser))
39203     cp_parser_skip_to_closing_parenthesis (parser,
39204 					   /*recovering=*/true,
39205 					   /*or_comma=*/false,
39206 					   /*consume_paren=*/true);
39207 
39208   return ret ? ret : error_mark_node;
39209 }
39210 
39211 /* OpenMP 5.0:
39212    affinity ( [aff-modifier :] variable-list )
39213    aff-modifier:
39214      iterator ( iterators-definition )  */
39215 
39216 static tree
cp_parser_omp_clause_affinity(cp_parser * parser,tree list)39217 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
39218 {
39219   tree nlist, c, iterators = NULL_TREE;
39220 
39221   matching_parens parens;
39222   if (!parens.require_open (parser))
39223     return list;
39224 
39225   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39226     {
39227       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39228       const char *p = IDENTIFIER_POINTER (id);
39229       bool parse_iter = ((strcmp ("iterator", p) == 0)
39230 			 && (cp_lexer_nth_token_is (parser->lexer, 2,
39231 						    CPP_OPEN_PAREN)));
39232       if (parse_iter)
39233 	{
39234 	  size_t n = cp_parser_skip_balanced_tokens (parser, 2);
39235 	  parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
39236 	}
39237       if (parse_iter)
39238 	{
39239 	  begin_scope (sk_omp, NULL);
39240 	  iterators = cp_parser_omp_iterators (parser);
39241 	  if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39242 	    {
39243 	      if (iterators)
39244 		poplevel (0, 1, 0);
39245 	      cp_parser_skip_to_closing_parenthesis (parser,
39246 						     /*recovering=*/true,
39247 						     /*or_comma=*/false,
39248 						     /*consume_paren=*/true);
39249 	      return list;
39250 	    }
39251 	}
39252     }
39253   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
39254 					  list, NULL);
39255   if (iterators)
39256     {
39257       tree block = poplevel (1, 1, 0);
39258       if (iterators != error_mark_node)
39259 	{
39260 	  TREE_VEC_ELT (iterators, 5) = block;
39261 	  for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39262 	    OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
39263 						   OMP_CLAUSE_DECL (c));
39264 	}
39265     }
39266   return nlist;
39267 }
39268 
39269 /* OpenMP 4.0:
39270    depend ( depend-kind : variable-list )
39271 
39272    depend-kind:
39273      in | out | inout
39274 
39275    OpenMP 4.5:
39276    depend ( source )
39277 
39278    depend ( sink : vec )
39279 
39280    OpenMP 5.0:
39281    depend ( depend-modifier , depend-kind: variable-list )
39282 
39283    depend-kind:
39284      in | out | inout | mutexinoutset | depobj
39285 
39286    depend-modifier:
39287      iterator ( iterators-definition )  */
39288 
39289 static tree
cp_parser_omp_clause_depend(cp_parser * parser,tree list,location_t loc)39290 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
39291 {
39292   tree nlist, c, iterators = NULL_TREE;
39293   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
39294 
39295   matching_parens parens;
39296   if (!parens.require_open (parser))
39297     return list;
39298 
39299   do
39300     {
39301       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39302 	goto invalid_kind;
39303 
39304       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39305       const char *p = IDENTIFIER_POINTER (id);
39306 
39307       if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
39308 	{
39309 	  begin_scope (sk_omp, NULL);
39310 	  iterators = cp_parser_omp_iterators (parser);
39311 	  cp_parser_require (parser, CPP_COMMA, RT_COMMA);
39312 	  continue;
39313 	}
39314       if (strcmp ("in", p) == 0)
39315 	kind = OMP_CLAUSE_DEPEND_IN;
39316       else if (strcmp ("inout", p) == 0)
39317 	kind = OMP_CLAUSE_DEPEND_INOUT;
39318       else if (strcmp ("mutexinoutset", p) == 0)
39319 	kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39320       else if (strcmp ("out", p) == 0)
39321 	kind = OMP_CLAUSE_DEPEND_OUT;
39322       else if (strcmp ("depobj", p) == 0)
39323 	kind = OMP_CLAUSE_DEPEND_DEPOBJ;
39324       else if (strcmp ("sink", p) == 0)
39325 	kind = OMP_CLAUSE_DEPEND_SINK;
39326       else if (strcmp ("source", p) == 0)
39327 	kind = OMP_CLAUSE_DEPEND_SOURCE;
39328       else
39329 	goto invalid_kind;
39330       break;
39331     }
39332   while (1);
39333 
39334   cp_lexer_consume_token (parser->lexer);
39335 
39336   if (iterators
39337       && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
39338     {
39339       poplevel (0, 1, 0);
39340       error_at (loc, "%<iterator%> modifier incompatible with %qs",
39341 		kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
39342       iterators = NULL_TREE;
39343     }
39344 
39345   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
39346     {
39347       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
39348       OMP_CLAUSE_DEPEND_KIND (c) = kind;
39349       OMP_CLAUSE_DECL (c) = NULL_TREE;
39350       OMP_CLAUSE_CHAIN (c) = list;
39351       if (!parens.require_close (parser))
39352 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39353 					       /*or_comma=*/false,
39354 					       /*consume_paren=*/true);
39355       return c;
39356     }
39357 
39358   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39359     goto resync_fail;
39360 
39361   if (kind == OMP_CLAUSE_DEPEND_SINK)
39362     {
39363       nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
39364       if (!parens.require_close (parser))
39365 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39366 					       /*or_comma=*/false,
39367 					       /*consume_paren=*/true);
39368     }
39369   else
39370     {
39371       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
39372 					      list, NULL);
39373 
39374       if (iterators)
39375 	{
39376 	  tree block = poplevel (1, 1, 0);
39377 	  if (iterators == error_mark_node)
39378 	    iterators = NULL_TREE;
39379 	  else
39380 	    TREE_VEC_ELT (iterators, 5) = block;
39381 	}
39382 
39383       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39384 	{
39385 	  OMP_CLAUSE_DEPEND_KIND (c) = kind;
39386 	  if (iterators)
39387 	    OMP_CLAUSE_DECL (c)
39388 	      = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
39389 	}
39390     }
39391   return nlist;
39392 
39393  invalid_kind:
39394   cp_parser_error (parser, "invalid depend kind");
39395  resync_fail:
39396   if (iterators)
39397     poplevel (0, 1, 0);
39398   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39399 					 /*or_comma=*/false,
39400 					 /*consume_paren=*/true);
39401   return list;
39402 }
39403 
39404 /* OpenMP 4.0:
39405    map ( map-kind : variable-list )
39406    map ( variable-list )
39407 
39408    map-kind:
39409      alloc | to | from | tofrom
39410 
39411    OpenMP 4.5:
39412    map-kind:
39413      alloc | to | from | tofrom | release | delete
39414 
39415    map ( always [,] map-kind: variable-list )
39416 
39417    OpenMP 5.0:
39418    map ( [map-type-modifier[,] ...] map-kind: variable-list )
39419 
39420    map-type-modifier:
39421      always | close */
39422 
39423 static tree
cp_parser_omp_clause_map(cp_parser * parser,tree list)39424 cp_parser_omp_clause_map (cp_parser *parser, tree list)
39425 {
39426   tree nlist, c;
39427   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
39428 
39429   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39430     return list;
39431 
39432   int pos = 1;
39433   int map_kind_pos = 0;
39434   while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
39435 	 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
39436     {
39437       if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
39438 	{
39439 	  map_kind_pos = pos;
39440 	  break;
39441 	}
39442 
39443       if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
39444 	pos++;
39445       pos++;
39446     }
39447 
39448   bool always_modifier = false;
39449   bool close_modifier = false;
39450   for (int pos = 1; pos < map_kind_pos; ++pos)
39451     {
39452       cp_token *tok = cp_lexer_peek_token (parser->lexer);
39453       if (tok->type == CPP_COMMA)
39454 	{
39455 	  cp_lexer_consume_token (parser->lexer);
39456 	  continue;
39457 	}
39458 
39459       const char *p = IDENTIFIER_POINTER (tok->u.value);
39460       if (strcmp ("always", p) == 0)
39461 	{
39462 	  if (always_modifier)
39463 	    {
39464 	      cp_parser_error (parser, "too many %<always%> modifiers");
39465 	      cp_parser_skip_to_closing_parenthesis (parser,
39466 						     /*recovering=*/true,
39467 						     /*or_comma=*/false,
39468 						     /*consume_paren=*/true);
39469 	      return list;
39470 	    }
39471 	  always_modifier = true;
39472 	}
39473       else if (strcmp ("close", p) == 0)
39474 	{
39475 	  if (close_modifier)
39476 	    {
39477 	      cp_parser_error (parser, "too many %<close%> modifiers");
39478 	      cp_parser_skip_to_closing_parenthesis (parser,
39479 						     /*recovering=*/true,
39480 						     /*or_comma=*/false,
39481 						     /*consume_paren=*/true);
39482 	      return list;
39483 	    }
39484 	  close_modifier = true;
39485 	}
39486       else
39487 	{
39488 	  cp_parser_error (parser, "%<#pragma omp target%> with "
39489 				   "modifier other than %<always%> or "
39490 				   "%<close%> on %<map%> clause");
39491 	  cp_parser_skip_to_closing_parenthesis (parser,
39492 						 /*recovering=*/true,
39493 						 /*or_comma=*/false,
39494 						 /*consume_paren=*/true);
39495 	  return list;
39496 	}
39497 
39498 	cp_lexer_consume_token (parser->lexer);
39499     }
39500 
39501   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39502       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39503     {
39504       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39505       const char *p = IDENTIFIER_POINTER (id);
39506 
39507       if (strcmp ("alloc", p) == 0)
39508 	kind = GOMP_MAP_ALLOC;
39509       else if (strcmp ("to", p) == 0)
39510 	kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
39511       else if (strcmp ("from", p) == 0)
39512 	kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
39513       else if (strcmp ("tofrom", p) == 0)
39514 	kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
39515       else if (strcmp ("release", p) == 0)
39516 	kind = GOMP_MAP_RELEASE;
39517       else
39518 	{
39519 	  cp_parser_error (parser, "invalid map kind");
39520 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39521 						 /*or_comma=*/false,
39522 						 /*consume_paren=*/true);
39523 	  return list;
39524 	}
39525       cp_lexer_consume_token (parser->lexer);
39526       cp_lexer_consume_token (parser->lexer);
39527     }
39528   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
39529 	   && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39530     {
39531       kind = GOMP_MAP_DELETE;
39532       cp_lexer_consume_token (parser->lexer);
39533       cp_lexer_consume_token (parser->lexer);
39534     }
39535 
39536   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
39537 					  NULL, true);
39538 
39539   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39540     OMP_CLAUSE_SET_MAP_KIND (c, kind);
39541 
39542   return nlist;
39543 }
39544 
39545 /* OpenMP 4.0:
39546    device ( expression )
39547 
39548    OpenMP 5.0:
39549    device ( [device-modifier :] integer-expression )
39550 
39551    device-modifier:
39552      ancestor | device_num */
39553 
39554 static tree
cp_parser_omp_clause_device(cp_parser * parser,tree list,location_t location)39555 cp_parser_omp_clause_device (cp_parser *parser, tree list,
39556 			     location_t location)
39557 {
39558   tree t, c;
39559   bool ancestor = false;
39560 
39561   matching_parens parens;
39562   if (!parens.require_open (parser))
39563     return list;
39564 
39565   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39566       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39567     {
39568       cp_token *tok = cp_lexer_peek_token (parser->lexer);
39569       const char *p = IDENTIFIER_POINTER (tok->u.value);
39570       if (strcmp ("ancestor", p) == 0)
39571 	{
39572 	  ancestor = true;
39573 
39574 	  /* A requires directive with the reverse_offload clause must be
39575 	  specified.  */
39576 	  if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
39577 	    {
39578 	      error_at (tok->location, "%<ancestor%> device modifier not "
39579 				       "preceded by %<requires%> directive "
39580 				       "with %<reverse_offload%> clause");
39581 	      cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39582 	      return list;
39583 	    }
39584 	}
39585       else if (strcmp ("device_num", p) == 0)
39586 	;
39587       else
39588 	{
39589 	  error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
39590 	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39591 	  return list;
39592 	}
39593       cp_lexer_consume_token (parser->lexer);
39594       cp_lexer_consume_token (parser->lexer);
39595     }
39596 
39597   t = cp_parser_assignment_expression (parser);
39598 
39599   if (t == error_mark_node
39600       || !parens.require_close (parser))
39601     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39602 					   /*or_comma=*/false,
39603 					   /*consume_paren=*/true);
39604 
39605   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
39606 			     "device", location);
39607 
39608   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
39609   OMP_CLAUSE_DEVICE_ID (c) = t;
39610   OMP_CLAUSE_CHAIN (c) = list;
39611   OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
39612 
39613   return c;
39614 }
39615 
39616 /* OpenMP 4.0:
39617    dist_schedule ( static )
39618    dist_schedule ( static , expression )  */
39619 
39620 static tree
cp_parser_omp_clause_dist_schedule(cp_parser * parser,tree list,location_t location)39621 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
39622 				    location_t location)
39623 {
39624   tree c, t;
39625 
39626   matching_parens parens;
39627   if (!parens.require_open (parser))
39628     return list;
39629 
39630   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
39631 
39632   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39633     goto invalid_kind;
39634   cp_lexer_consume_token (parser->lexer);
39635 
39636   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39637     {
39638       cp_lexer_consume_token (parser->lexer);
39639 
39640       t = cp_parser_assignment_expression (parser);
39641 
39642       if (t == error_mark_node)
39643 	goto resync_fail;
39644       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
39645 
39646       if (!parens.require_close (parser))
39647 	goto resync_fail;
39648     }
39649   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39650     goto resync_fail;
39651 
39652   /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
39653 				"dist_schedule", location); */
39654   if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
39655     warning_at (location, 0, "too many %qs clauses", "dist_schedule");
39656   OMP_CLAUSE_CHAIN (c) = list;
39657   return c;
39658 
39659  invalid_kind:
39660   cp_parser_error (parser, "invalid dist_schedule kind");
39661  resync_fail:
39662   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39663 					 /*or_comma=*/false,
39664 					 /*consume_paren=*/true);
39665   return list;
39666 }
39667 
39668 /* OpenMP 4.0:
39669    proc_bind ( proc-bind-kind )
39670 
39671    proc-bind-kind:
39672      primary | master | close | spread
39673    where OpenMP 5.1 added 'primary' and deprecated the alias 'master'.  */
39674 
39675 static tree
cp_parser_omp_clause_proc_bind(cp_parser * parser,tree list,location_t location)39676 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
39677 				location_t location)
39678 {
39679   tree c;
39680   enum omp_clause_proc_bind_kind kind;
39681 
39682   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39683     return list;
39684 
39685   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39686     {
39687       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39688       const char *p = IDENTIFIER_POINTER (id);
39689 
39690       if (strcmp ("primary", p) == 0)
39691 	kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
39692       else if (strcmp ("master", p) == 0)
39693 	kind = OMP_CLAUSE_PROC_BIND_MASTER;
39694       else if (strcmp ("close", p) == 0)
39695 	kind = OMP_CLAUSE_PROC_BIND_CLOSE;
39696       else if (strcmp ("spread", p) == 0)
39697 	kind = OMP_CLAUSE_PROC_BIND_SPREAD;
39698       else
39699 	goto invalid_kind;
39700     }
39701   else
39702     goto invalid_kind;
39703 
39704   cp_lexer_consume_token (parser->lexer);
39705   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39706     goto resync_fail;
39707 
39708   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
39709   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
39710 			     location);
39711   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
39712   OMP_CLAUSE_CHAIN (c) = list;
39713   return c;
39714 
39715  invalid_kind:
39716   cp_parser_error (parser, "invalid depend kind");
39717  resync_fail:
39718   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39719 					 /*or_comma=*/false,
39720 					 /*consume_paren=*/true);
39721   return list;
39722 }
39723 
39724 /* OpenMP 5.0:
39725    device_type ( host | nohost | any )  */
39726 
39727 static tree
cp_parser_omp_clause_device_type(cp_parser * parser,tree list,location_t location)39728 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
39729 				  location_t location)
39730 {
39731   tree c;
39732   enum omp_clause_device_type_kind kind;
39733 
39734   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39735     return list;
39736 
39737   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39738     {
39739       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39740       const char *p = IDENTIFIER_POINTER (id);
39741 
39742       if (strcmp ("host", p) == 0)
39743 	kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
39744       else if (strcmp ("nohost", p) == 0)
39745 	kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
39746       else if (strcmp ("any", p) == 0)
39747 	kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
39748       else
39749 	goto invalid_kind;
39750     }
39751   else
39752     goto invalid_kind;
39753 
39754   cp_lexer_consume_token (parser->lexer);
39755   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39756     goto resync_fail;
39757 
39758   c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
39759   /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
39760 				location);  */
39761   OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
39762   OMP_CLAUSE_CHAIN (c) = list;
39763   return c;
39764 
39765  invalid_kind:
39766   cp_parser_error (parser, "invalid depend kind");
39767  resync_fail:
39768   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39769 					 /*or_comma=*/false,
39770 					 /*consume_paren=*/true);
39771   return list;
39772 }
39773 
39774 /* OpenACC:
39775    async [( int-expr )] */
39776 
39777 static tree
cp_parser_oacc_clause_async(cp_parser * parser,tree list)39778 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
39779 {
39780   tree c, t;
39781   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39782 
39783   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
39784 
39785   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39786     {
39787       matching_parens parens;
39788       parens.consume_open (parser);
39789 
39790       t = cp_parser_assignment_expression (parser);
39791       if (t == error_mark_node
39792 	  || !parens.require_close (parser))
39793 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39794 						/*or_comma=*/false,
39795 						/*consume_paren=*/true);
39796     }
39797 
39798   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
39799 
39800   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
39801   OMP_CLAUSE_ASYNC_EXPR (c) = t;
39802   OMP_CLAUSE_CHAIN (c) = list;
39803   list = c;
39804 
39805   return list;
39806 }
39807 
39808 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
39809    is a bitmask in MASK.  Return the list of clauses found.  */
39810 
39811 static tree
cp_parser_oacc_all_clauses(cp_parser * parser,omp_clause_mask mask,const char * where,cp_token * pragma_tok,bool finish_p=true)39812 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
39813 			    const char *where, cp_token *pragma_tok,
39814 			    bool finish_p = true)
39815 {
39816   tree clauses = NULL;
39817   bool first = true;
39818 
39819   /* Don't create location wrapper nodes within OpenACC clauses.  */
39820   auto_suppress_location_wrappers sentinel;
39821 
39822   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39823     {
39824       location_t here;
39825       pragma_omp_clause c_kind;
39826       omp_clause_code code;
39827       const char *c_name;
39828       tree prev = clauses;
39829 
39830       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39831 	cp_lexer_consume_token (parser->lexer);
39832 
39833       here = cp_lexer_peek_token (parser->lexer)->location;
39834       c_kind = cp_parser_omp_clause_name (parser);
39835 
39836       switch (c_kind)
39837 	{
39838 	case PRAGMA_OACC_CLAUSE_ASYNC:
39839 	  clauses = cp_parser_oacc_clause_async (parser, clauses);
39840 	  c_name = "async";
39841 	  break;
39842 	case PRAGMA_OACC_CLAUSE_AUTO:
39843 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
39844 						  clauses);
39845 	  c_name = "auto";
39846 	  break;
39847 	case PRAGMA_OACC_CLAUSE_ATTACH:
39848 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39849 	  c_name = "attach";
39850 	  break;
39851 	case PRAGMA_OACC_CLAUSE_COLLAPSE:
39852 	  clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
39853 	  c_name = "collapse";
39854 	  break;
39855 	case PRAGMA_OACC_CLAUSE_COPY:
39856 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39857 	  c_name = "copy";
39858 	  break;
39859 	case PRAGMA_OACC_CLAUSE_COPYIN:
39860 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39861 	  c_name = "copyin";
39862 	  break;
39863 	case PRAGMA_OACC_CLAUSE_COPYOUT:
39864 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39865 	  c_name = "copyout";
39866 	  break;
39867 	case PRAGMA_OACC_CLAUSE_CREATE:
39868 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39869 	  c_name = "create";
39870 	  break;
39871 	case PRAGMA_OACC_CLAUSE_DELETE:
39872 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39873 	  c_name = "delete";
39874 	  break;
39875 	case PRAGMA_OMP_CLAUSE_DEFAULT:
39876 	  clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
39877 	  c_name = "default";
39878 	  break;
39879 	case PRAGMA_OACC_CLAUSE_DETACH:
39880 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39881 	  c_name = "detach";
39882 	  break;
39883 	case PRAGMA_OACC_CLAUSE_DEVICE:
39884 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39885 	  c_name = "device";
39886 	  break;
39887 	case PRAGMA_OACC_CLAUSE_DEVICEPTR:
39888 	  clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
39889 	  c_name = "deviceptr";
39890 	  break;
39891 	case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
39892 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39893 	  c_name = "device_resident";
39894 	  break;
39895 	case PRAGMA_OACC_CLAUSE_FINALIZE:
39896 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
39897 						  clauses);
39898 	  c_name = "finalize";
39899 	  break;
39900 	case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
39901 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39902 					    clauses);
39903 	  c_name = "firstprivate";
39904 	  break;
39905 	case PRAGMA_OACC_CLAUSE_GANG:
39906 	  c_name = "gang";
39907 	  clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
39908 						 c_name, clauses);
39909 	  break;
39910 	case PRAGMA_OACC_CLAUSE_HOST:
39911 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39912 	  c_name = "host";
39913 	  break;
39914 	case PRAGMA_OACC_CLAUSE_IF:
39915 	  clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
39916 	  c_name = "if";
39917 	  break;
39918 	case PRAGMA_OACC_CLAUSE_IF_PRESENT:
39919 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
39920 						  clauses);
39921 	  c_name = "if_present";
39922 	  break;
39923 	case PRAGMA_OACC_CLAUSE_INDEPENDENT:
39924 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
39925 						  clauses);
39926 	  c_name = "independent";
39927 	  break;
39928 	case PRAGMA_OACC_CLAUSE_LINK:
39929 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39930 	  c_name = "link";
39931 	  break;
39932 	case PRAGMA_OACC_CLAUSE_NO_CREATE:
39933 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39934 	  c_name = "no_create";
39935 	  break;
39936 	case PRAGMA_OACC_CLAUSE_NOHOST:
39937 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
39938 						  clauses);
39939 	  c_name = "nohost";
39940 	  break;
39941 	case PRAGMA_OACC_CLAUSE_NUM_GANGS:
39942 	  code = OMP_CLAUSE_NUM_GANGS;
39943 	  c_name = "num_gangs";
39944 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39945 						      clauses);
39946 	  break;
39947 	case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
39948 	  c_name = "num_workers";
39949 	  code = OMP_CLAUSE_NUM_WORKERS;
39950 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39951 						      clauses);
39952 	  break;
39953 	case PRAGMA_OACC_CLAUSE_PRESENT:
39954 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
39955 	  c_name = "present";
39956 	  break;
39957 	case PRAGMA_OACC_CLAUSE_PRIVATE:
39958 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
39959 					    clauses);
39960 	  c_name = "private";
39961 	  break;
39962 	case PRAGMA_OACC_CLAUSE_REDUCTION:
39963 	  clauses
39964 	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
39965 					      false, clauses);
39966 	  c_name = "reduction";
39967 	  break;
39968 	case PRAGMA_OACC_CLAUSE_SEQ:
39969 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
39970 						  clauses);
39971 	  c_name = "seq";
39972 	  break;
39973 	case PRAGMA_OACC_CLAUSE_TILE:
39974 	  clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
39975 	  c_name = "tile";
39976 	  break;
39977 	case PRAGMA_OACC_CLAUSE_USE_DEVICE:
39978 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
39979 					    clauses);
39980 	  c_name = "use_device";
39981 	  break;
39982 	case PRAGMA_OACC_CLAUSE_VECTOR:
39983 	  c_name = "vector";
39984 	  clauses = cp_parser_oacc_shape_clause (parser, here,
39985 						 OMP_CLAUSE_VECTOR,
39986 						 c_name, clauses);
39987 	  break;
39988 	case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
39989 	  c_name = "vector_length";
39990 	  code = OMP_CLAUSE_VECTOR_LENGTH;
39991 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
39992 						      clauses);
39993 	  break;
39994 	case PRAGMA_OACC_CLAUSE_WAIT:
39995 	  clauses = cp_parser_oacc_clause_wait (parser, clauses);
39996 	  c_name = "wait";
39997 	  break;
39998 	case PRAGMA_OACC_CLAUSE_WORKER:
39999 	  c_name = "worker";
40000 	  clauses = cp_parser_oacc_shape_clause (parser, here,
40001 						 OMP_CLAUSE_WORKER,
40002 						 c_name, clauses);
40003 	  break;
40004 	default:
40005 	  cp_parser_error (parser, "expected %<#pragma acc%> clause");
40006 	  goto saw_error;
40007 	}
40008 
40009       first = false;
40010 
40011       if (((mask >> c_kind) & 1) == 0)
40012 	{
40013 	  /* Remove the invalid clause(s) from the list to avoid
40014 	     confusing the rest of the compiler.  */
40015 	  clauses = prev;
40016 	  error_at (here, "%qs is not valid for %qs", c_name, where);
40017 	}
40018     }
40019 
40020  saw_error:
40021   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40022 
40023   if (finish_p)
40024     return finish_omp_clauses (clauses, C_ORT_ACC);
40025 
40026   return clauses;
40027 }
40028 
40029 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
40030    is a bitmask in MASK.  Return the list of clauses found.
40031    FINISH_P set if finish_omp_clauses should be called.
40032    NESTED non-zero if clauses should be terminated by closing paren instead
40033    of end of pragma.  If it is 2, additionally commas are required in between
40034    the clauses.  */
40035 
40036 static tree
cp_parser_omp_all_clauses(cp_parser * parser,omp_clause_mask mask,const char * where,cp_token * pragma_tok,bool finish_p=true,int nested=0)40037 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
40038 			   const char *where, cp_token *pragma_tok,
40039 			   bool finish_p = true, int nested = 0)
40040 {
40041   tree clauses = NULL;
40042   bool first = true;
40043   cp_token *token = NULL;
40044 
40045   /* Don't create location wrapper nodes within OpenMP clauses.  */
40046   auto_suppress_location_wrappers sentinel;
40047 
40048   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40049     {
40050       pragma_omp_clause c_kind;
40051       const char *c_name;
40052       tree prev = clauses;
40053 
40054       if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
40055 	break;
40056 
40057       if (!first
40058 	  /* OpenMP 5.1 allows optional comma in between directive-name and
40059 	     clauses everywhere, but as we aren't done with OpenMP 5.0
40060 	     implementation yet, let's allow it for now only in C++11
40061 	     attributes.  */
40062 	  || (parser->lexer->in_omp_attribute_pragma && nested != 2))
40063 	{
40064 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40065 	    cp_lexer_consume_token (parser->lexer);
40066 	  else if (nested == 2)
40067 	    error_at (cp_lexer_peek_token (parser->lexer)->location,
40068 		      "clauses in %<simd%> trait should be separated "
40069                       "by %<,%>");
40070 	}
40071 
40072       token = cp_lexer_peek_token (parser->lexer);
40073       c_kind = cp_parser_omp_clause_name (parser);
40074 
40075       switch (c_kind)
40076 	{
40077 	case PRAGMA_OMP_CLAUSE_BIND:
40078 	  clauses = cp_parser_omp_clause_bind (parser, clauses,
40079 					       token->location);
40080 	  c_name = "bind";
40081 	  break;
40082 	case PRAGMA_OMP_CLAUSE_COLLAPSE:
40083 	  clauses = cp_parser_omp_clause_collapse (parser, clauses,
40084 						   token->location);
40085 	  c_name = "collapse";
40086 	  break;
40087 	case PRAGMA_OMP_CLAUSE_COPYIN:
40088 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
40089 	  c_name = "copyin";
40090 	  break;
40091 	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
40092 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
40093 					    clauses);
40094 	  c_name = "copyprivate";
40095 	  break;
40096 	case PRAGMA_OMP_CLAUSE_DEFAULT:
40097 	  clauses = cp_parser_omp_clause_default (parser, clauses,
40098 						  token->location, false);
40099 	  c_name = "default";
40100 	  break;
40101 	case PRAGMA_OMP_CLAUSE_FILTER:
40102 	  clauses = cp_parser_omp_clause_filter (parser, clauses,
40103 						 token->location);
40104 	  c_name = "filter";
40105 	  break;
40106 	case PRAGMA_OMP_CLAUSE_FINAL:
40107 	  clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
40108 	  c_name = "final";
40109 	  break;
40110 	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
40111 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40112 					    clauses);
40113 	  c_name = "firstprivate";
40114 	  break;
40115 	case PRAGMA_OMP_CLAUSE_GRAINSIZE:
40116 	  clauses = cp_parser_omp_clause_grainsize (parser, clauses,
40117 						    token->location);
40118 	  c_name = "grainsize";
40119 	  break;
40120 	case PRAGMA_OMP_CLAUSE_HINT:
40121 	  clauses = cp_parser_omp_clause_hint (parser, clauses,
40122 					       token->location);
40123 	  c_name = "hint";
40124 	  break;
40125 	case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
40126 	  clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
40127 						     token->location);
40128 	  c_name = "defaultmap";
40129 	  break;
40130 	case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
40131 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
40132 					    clauses);
40133 	  c_name = "use_device_ptr";
40134 	  break;
40135 	case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
40136 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
40137 					    clauses);
40138 	  c_name = "use_device_addr";
40139 	  break;
40140 	case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
40141 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
40142 					    clauses);
40143 	  c_name = "is_device_ptr";
40144 	  break;
40145 	case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
40146 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
40147 					    clauses);
40148 	  c_name = "has_device_addr";
40149 	  break;
40150 	case PRAGMA_OMP_CLAUSE_IF:
40151 	  clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
40152 					     true);
40153 	  c_name = "if";
40154 	  break;
40155 	case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
40156 	  clauses
40157 	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
40158 					      true, clauses);
40159 	  c_name = "in_reduction";
40160 	  break;
40161 	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
40162 	  clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
40163 	  c_name = "lastprivate";
40164 	  break;
40165 	case PRAGMA_OMP_CLAUSE_MERGEABLE:
40166 	  clauses = cp_parser_omp_clause_mergeable (parser, clauses,
40167 						    token->location);
40168 	  c_name = "mergeable";
40169 	  break;
40170 	case PRAGMA_OMP_CLAUSE_NOWAIT:
40171 	  clauses = cp_parser_omp_clause_nowait (parser, clauses,
40172 						 token->location);
40173 	  c_name = "nowait";
40174 	  break;
40175 	case PRAGMA_OMP_CLAUSE_NUM_TASKS:
40176 	  clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
40177 						    token->location);
40178 	  c_name = "num_tasks";
40179 	  break;
40180 	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
40181 	  clauses = cp_parser_omp_clause_num_threads (parser, clauses,
40182 						      token->location);
40183 	  c_name = "num_threads";
40184 	  break;
40185 	case PRAGMA_OMP_CLAUSE_ORDER:
40186 	  clauses = cp_parser_omp_clause_order (parser, clauses,
40187 						token->location);
40188 	  c_name = "order";
40189 	  break;
40190 	case PRAGMA_OMP_CLAUSE_ORDERED:
40191 	  clauses = cp_parser_omp_clause_ordered (parser, clauses,
40192 						  token->location);
40193 	  c_name = "ordered";
40194 	  break;
40195 	case PRAGMA_OMP_CLAUSE_PRIORITY:
40196 	  clauses = cp_parser_omp_clause_priority (parser, clauses,
40197 						   token->location);
40198 	  c_name = "priority";
40199 	  break;
40200 	case PRAGMA_OMP_CLAUSE_PRIVATE:
40201 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40202 					    clauses);
40203 	  c_name = "private";
40204 	  break;
40205 	case PRAGMA_OMP_CLAUSE_REDUCTION:
40206 	  clauses
40207 	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40208 					      true, clauses);
40209 	  c_name = "reduction";
40210 	  break;
40211 	case PRAGMA_OMP_CLAUSE_SCHEDULE:
40212 	  clauses = cp_parser_omp_clause_schedule (parser, clauses,
40213 						   token->location);
40214 	  c_name = "schedule";
40215 	  break;
40216 	case PRAGMA_OMP_CLAUSE_SHARED:
40217 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
40218 					    clauses);
40219 	  c_name = "shared";
40220 	  break;
40221 	case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
40222 	  clauses
40223 	    = cp_parser_omp_clause_reduction (parser,
40224 					      OMP_CLAUSE_TASK_REDUCTION,
40225 					      true, clauses);
40226 	  c_name = "task_reduction";
40227 	  break;
40228 	case PRAGMA_OMP_CLAUSE_UNTIED:
40229 	  clauses = cp_parser_omp_clause_untied (parser, clauses,
40230 						 token->location);
40231 	  c_name = "untied";
40232 	  break;
40233 	case PRAGMA_OMP_CLAUSE_INBRANCH:
40234 	  clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
40235 						 clauses, token->location);
40236 	  c_name = "inbranch";
40237 	  break;
40238 	case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
40239 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
40240 					    clauses);
40241 	  c_name = "nontemporal";
40242 	  break;
40243 	case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
40244 	  clauses = cp_parser_omp_clause_branch (parser,
40245 						 OMP_CLAUSE_NOTINBRANCH,
40246 						 clauses, token->location);
40247 	  c_name = "notinbranch";
40248 	  break;
40249 	case PRAGMA_OMP_CLAUSE_PARALLEL:
40250 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
40251 						     clauses, token->location);
40252 	  c_name = "parallel";
40253 	  if (!first)
40254 	    {
40255 	     clause_not_first:
40256 	      error_at (token->location, "%qs must be the first clause of %qs",
40257 			c_name, where);
40258 	      clauses = prev;
40259 	    }
40260 	  break;
40261 	case PRAGMA_OMP_CLAUSE_FOR:
40262 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
40263 						     clauses, token->location);
40264 	  c_name = "for";
40265 	  if (!first)
40266 	    goto clause_not_first;
40267 	  break;
40268 	case PRAGMA_OMP_CLAUSE_SECTIONS:
40269 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
40270 						     clauses, token->location);
40271 	  c_name = "sections";
40272 	  if (!first)
40273 	    goto clause_not_first;
40274 	  break;
40275 	case PRAGMA_OMP_CLAUSE_TASKGROUP:
40276 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
40277 						     clauses, token->location);
40278 	  c_name = "taskgroup";
40279 	  if (!first)
40280 	    goto clause_not_first;
40281 	  break;
40282 	case PRAGMA_OMP_CLAUSE_LINK:
40283 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
40284 	  c_name = "to";
40285 	  break;
40286 	case PRAGMA_OMP_CLAUSE_TO:
40287 	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
40288 	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
40289 					      clauses);
40290 	  else
40291 	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses,
40292 					      true);
40293 	  c_name = "to";
40294 	  break;
40295 	case PRAGMA_OMP_CLAUSE_FROM:
40296 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses,
40297 					    true);
40298 	  c_name = "from";
40299 	  break;
40300 	case PRAGMA_OMP_CLAUSE_UNIFORM:
40301 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
40302 					    clauses);
40303 	  c_name = "uniform";
40304 	  break;
40305 	case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
40306 	  clauses = cp_parser_omp_clause_num_teams (parser, clauses,
40307 						    token->location);
40308 	  c_name = "num_teams";
40309 	  break;
40310 	case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
40311 	  clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
40312 						       token->location);
40313 	  c_name = "thread_limit";
40314 	  break;
40315 	case PRAGMA_OMP_CLAUSE_ALIGNED:
40316 	  clauses = cp_parser_omp_clause_aligned (parser, clauses);
40317 	  c_name = "aligned";
40318 	  break;
40319 	case PRAGMA_OMP_CLAUSE_ALLOCATE:
40320 	  clauses = cp_parser_omp_clause_allocate (parser, clauses);
40321 	  c_name = "allocate";
40322 	  break;
40323 	case PRAGMA_OMP_CLAUSE_LINEAR:
40324 	  {
40325 	    bool declare_simd = false;
40326 	    if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
40327 	      declare_simd = true;
40328 	    clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
40329 	  }
40330 	  c_name = "linear";
40331 	  break;
40332 	case PRAGMA_OMP_CLAUSE_AFFINITY:
40333 	  clauses = cp_parser_omp_clause_affinity (parser, clauses);
40334 	  c_name = "affinity";
40335 	  break;
40336 	case PRAGMA_OMP_CLAUSE_DEPEND:
40337 	  clauses = cp_parser_omp_clause_depend (parser, clauses,
40338 						 token->location);
40339 	  c_name = "depend";
40340 	  break;
40341 	case PRAGMA_OMP_CLAUSE_DETACH:
40342 	  clauses = cp_parser_omp_clause_detach (parser, clauses);
40343 	  c_name = "detach";
40344 	  break;
40345 	case PRAGMA_OMP_CLAUSE_MAP:
40346 	  clauses = cp_parser_omp_clause_map (parser, clauses);
40347 	  c_name = "map";
40348 	  break;
40349 	case PRAGMA_OMP_CLAUSE_DEVICE:
40350 	  clauses = cp_parser_omp_clause_device (parser, clauses,
40351 						 token->location);
40352 	  c_name = "device";
40353 	  break;
40354 	case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
40355 	  clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
40356 							token->location);
40357 	  c_name = "dist_schedule";
40358 	  break;
40359 	case PRAGMA_OMP_CLAUSE_PROC_BIND:
40360 	  clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
40361 						    token->location);
40362 	  c_name = "proc_bind";
40363 	  break;
40364 	case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
40365 	  clauses = cp_parser_omp_clause_device_type (parser, clauses,
40366 						      token->location);
40367 	  c_name = "device_type";
40368 	  break;
40369 	case PRAGMA_OMP_CLAUSE_SAFELEN:
40370 	  clauses = cp_parser_omp_clause_safelen (parser, clauses,
40371 						  token->location);
40372 	  c_name = "safelen";
40373 	  break;
40374 	case PRAGMA_OMP_CLAUSE_SIMDLEN:
40375 	  clauses = cp_parser_omp_clause_simdlen (parser, clauses,
40376 						  token->location);
40377 	  c_name = "simdlen";
40378 	  break;
40379 	case PRAGMA_OMP_CLAUSE_NOGROUP:
40380 	  clauses = cp_parser_omp_clause_nogroup (parser, clauses,
40381 						  token->location);
40382 	  c_name = "nogroup";
40383 	  break;
40384 	case PRAGMA_OMP_CLAUSE_THREADS:
40385 	  clauses
40386 	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
40387 						clauses, token->location);
40388 	  c_name = "threads";
40389 	  break;
40390 	case PRAGMA_OMP_CLAUSE_SIMD:
40391 	  clauses
40392 	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
40393 						clauses, token->location);
40394 	  c_name = "simd";
40395 	  break;
40396 	default:
40397 	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
40398 	  goto saw_error;
40399 	}
40400 
40401       first = false;
40402 
40403       if (((mask >> c_kind) & 1) == 0)
40404 	{
40405 	  /* Remove the invalid clause(s) from the list to avoid
40406 	     confusing the rest of the compiler.  */
40407 	  clauses = prev;
40408 	  error_at (token->location, "%qs is not valid for %qs", c_name, where);
40409 	}
40410     }
40411  saw_error:
40412   if (!nested)
40413     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40414   if (finish_p)
40415     {
40416       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
40417 	return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
40418       else
40419 	return finish_omp_clauses (clauses, C_ORT_OMP);
40420     }
40421   return clauses;
40422 }
40423 
40424 /* OpenMP 2.5:
40425    structured-block:
40426      statement
40427 
40428    In practice, we're also interested in adding the statement to an
40429    outer node.  So it is convenient if we work around the fact that
40430    cp_parser_statement calls add_stmt.  */
40431 
40432 static unsigned
cp_parser_begin_omp_structured_block(cp_parser * parser)40433 cp_parser_begin_omp_structured_block (cp_parser *parser)
40434 {
40435   unsigned save = parser->in_statement;
40436 
40437   /* Only move the values to IN_OMP_BLOCK if they weren't false.
40438      This preserves the "not within loop or switch" style error messages
40439      for nonsense cases like
40440 	void foo() {
40441 	#pragma omp single
40442 	  break;
40443 	}
40444   */
40445   if (parser->in_statement)
40446     parser->in_statement = IN_OMP_BLOCK;
40447 
40448   return save;
40449 }
40450 
40451 static void
cp_parser_end_omp_structured_block(cp_parser * parser,unsigned save)40452 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
40453 {
40454   parser->in_statement = save;
40455 }
40456 
40457 static tree
cp_parser_omp_structured_block(cp_parser * parser,bool * if_p)40458 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
40459 {
40460   tree stmt = begin_omp_structured_block ();
40461   unsigned int save = cp_parser_begin_omp_structured_block (parser);
40462 
40463   parser->omp_attrs_forbidden_p = true;
40464   cp_parser_statement (parser, NULL_TREE, false, if_p);
40465 
40466   cp_parser_end_omp_structured_block (parser, save);
40467   return finish_omp_structured_block (stmt);
40468 }
40469 
40470 /* OpenMP 5.0:
40471    # pragma omp allocate (list)  [allocator(allocator)]  */
40472 
40473 static void
cp_parser_omp_allocate(cp_parser * parser,cp_token * pragma_tok)40474 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
40475 {
40476   tree allocator = NULL_TREE;
40477   location_t loc = pragma_tok->location;
40478   tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
40479 
40480   /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
40481   if (parser->lexer->in_omp_attribute_pragma
40482       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40483       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40484     cp_lexer_consume_token (parser->lexer);
40485 
40486   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40487     {
40488       matching_parens parens;
40489       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40490       const char *p = IDENTIFIER_POINTER (id);
40491       location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40492       cp_lexer_consume_token (parser->lexer);
40493       if (strcmp (p, "allocator") != 0)
40494 	error_at (cloc, "expected %<allocator%>");
40495       else if (parens.require_open (parser))
40496 	{
40497 	  allocator = cp_parser_assignment_expression (parser);
40498 	  if (allocator == error_mark_node)
40499 	    allocator = NULL_TREE;
40500 	  parens.require_close (parser);
40501 	}
40502     }
40503   cp_parser_require_pragma_eol (parser, pragma_tok);
40504 
40505   if (allocator)
40506     for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
40507       OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40508 
40509   sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
40510 }
40511 
40512 /* OpenMP 2.5:
40513    # pragma omp atomic new-line
40514      expression-stmt
40515 
40516    expression-stmt:
40517      x binop= expr | x++ | ++x | x-- | --x
40518    binop:
40519      +, *, -, /, &, ^, |, <<, >>
40520 
40521   where x is an lvalue expression with scalar type.
40522 
40523    OpenMP 3.1:
40524    # pragma omp atomic new-line
40525      update-stmt
40526 
40527    # pragma omp atomic read new-line
40528      read-stmt
40529 
40530    # pragma omp atomic write new-line
40531      write-stmt
40532 
40533    # pragma omp atomic update new-line
40534      update-stmt
40535 
40536    # pragma omp atomic capture new-line
40537      capture-stmt
40538 
40539    # pragma omp atomic capture new-line
40540      capture-block
40541 
40542    read-stmt:
40543      v = x
40544    write-stmt:
40545      x = expr
40546    update-stmt:
40547      expression-stmt | x = x binop expr
40548    capture-stmt:
40549      v = expression-stmt
40550    capture-block:
40551      { v = x; update-stmt; } | { update-stmt; v = x; }
40552 
40553    OpenMP 4.0:
40554    update-stmt:
40555      expression-stmt | x = x binop expr | x = expr binop x
40556    capture-stmt:
40557      v = update-stmt
40558    capture-block:
40559      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
40560 
40561    OpenMP 5.1:
40562    # pragma omp atomic compare new-line
40563      conditional-update-atomic
40564 
40565    # pragma omp atomic compare capture new-line
40566      conditional-update-capture-atomic
40567 
40568    conditional-update-atomic:
40569      cond-expr-stmt | cond-update-stmt
40570    cond-expr-stmt:
40571      x = expr ordop x ? expr : x;
40572      x = x ordop expr ? expr : x;
40573      x = x == e ? d : x;
40574    cond-update-stmt:
40575      if (expr ordop x) { x = expr; }
40576      if (x ordop expr) { x = expr; }
40577      if (x == e) { x = d; }
40578    ordop:
40579      <, >
40580    conditional-update-capture-atomic:
40581      v = cond-expr-stmt
40582      { v = x; cond-expr-stmt }
40583      { cond-expr-stmt v = x; }
40584      { v = x; cond-update-stmt }
40585      { cond-update-stmt v = x; }
40586      if (x == e) { x = d; } else { v = x; }
40587      { r = x == e; if (r) { x = d; } }
40588      { r = x == e; if (r) { x = d; } else { v = x; } }
40589 
40590   where x, r and v are lvalue expressions with scalar type,
40591   expr, e and d are expressions with scalar type and e might be
40592   the same as v.  */
40593 
40594 static void
cp_parser_omp_atomic(cp_parser * parser,cp_token * pragma_tok,bool openacc)40595 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
40596 {
40597   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
40598   tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
40599   location_t loc = pragma_tok->location;
40600   enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
40601   enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
40602   bool structured_block = false;
40603   bool first = true;
40604   tree clauses = NULL_TREE;
40605   bool capture = false;
40606   bool compare = false;
40607   bool weak = false;
40608   enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40609   bool no_semicolon = false;
40610   bool extra_scope = false;
40611 
40612   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40613     {
40614       /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
40615       if ((!first || parser->lexer->in_omp_attribute_pragma)
40616 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40617 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40618 	cp_lexer_consume_token (parser->lexer);
40619 
40620       first = false;
40621 
40622       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40623 	{
40624 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40625 	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40626 	  const char *p = IDENTIFIER_POINTER (id);
40627 	  enum tree_code new_code = ERROR_MARK;
40628 	  enum omp_memory_order new_memory_order
40629 	    = OMP_MEMORY_ORDER_UNSPECIFIED;
40630 	  bool new_capture = false;
40631 	  bool new_compare = false;
40632 	  bool new_weak = false;
40633 	  enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40634 
40635 	  if (!strcmp (p, "read"))
40636 	    new_code = OMP_ATOMIC_READ;
40637 	  else if (!strcmp (p, "write"))
40638 	    new_code = NOP_EXPR;
40639 	  else if (!strcmp (p, "update"))
40640 	    new_code = OMP_ATOMIC;
40641 	  else if (openacc && !strcmp (p, "capture"))
40642 	    new_code = OMP_ATOMIC_CAPTURE_NEW;
40643 	  else if (openacc)
40644 	    {
40645 	      p = NULL;
40646 	      error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40647 			      "or %<capture%> clause");
40648 	    }
40649 	  else if (!strcmp (p, "capture"))
40650 	    new_capture = true;
40651 	  else if (!strcmp (p, "compare"))
40652 	    new_compare = true;
40653 	  else if (!strcmp (p, "weak"))
40654 	    new_weak = true;
40655 	  else if (!strcmp (p, "fail"))
40656 	    {
40657 	      matching_parens parens;
40658 
40659 	      cp_lexer_consume_token (parser->lexer);
40660 	      if (!parens.require_open (parser))
40661 		continue;
40662 
40663 	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40664 		{
40665 		  id = cp_lexer_peek_token (parser->lexer)->u.value;
40666 		  const char *q = IDENTIFIER_POINTER (id);
40667 
40668 		  if (!strcmp (q, "seq_cst"))
40669 		    new_fail = OMP_MEMORY_ORDER_SEQ_CST;
40670 		  else if (!strcmp (q, "acquire"))
40671 		    new_fail = OMP_MEMORY_ORDER_ACQUIRE;
40672 		  else if (!strcmp (q, "relaxed"))
40673 		    new_fail = OMP_MEMORY_ORDER_RELAXED;
40674 		}
40675 
40676 	      if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40677 		{
40678 		  cp_lexer_consume_token (parser->lexer);
40679 		  if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40680 		    error_at (cloc, "too many %qs clauses", "fail");
40681 		  else
40682 		    fail = new_fail;
40683 		}
40684 	      else
40685 		cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
40686 					 "or %<relaxed%>");
40687 	      if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
40688 		  || !parens.require_close (parser))
40689 		cp_parser_skip_to_closing_parenthesis (parser,
40690 						       /*recovering=*/true,
40691 						       /*or_comma=*/false,
40692 						       /*consume_paren=*/true);
40693 	      continue;
40694 	    }
40695 	  else if (!strcmp (p, "seq_cst"))
40696 	    new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40697 	  else if (!strcmp (p, "acq_rel"))
40698 	    new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40699 	  else if (!strcmp (p, "release"))
40700 	    new_memory_order = OMP_MEMORY_ORDER_RELEASE;
40701 	  else if (!strcmp (p, "acquire"))
40702 	    new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40703 	  else if (!strcmp (p, "relaxed"))
40704 	    new_memory_order = OMP_MEMORY_ORDER_RELAXED;
40705 	  else if (!strcmp (p, "hint"))
40706 	    {
40707 	      cp_lexer_consume_token (parser->lexer);
40708 	      clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
40709 	      continue;
40710 	    }
40711 	  else
40712 	    {
40713 	      p = NULL;
40714 	      error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
40715 			      "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
40716 			      "%<seq_cst%>, %<acq_rel%>, %<release%>, "
40717 			      "%<relaxed%> or %<hint%> clause");
40718 	    }
40719 	  if (p)
40720 	    {
40721 	      if (new_code != ERROR_MARK)
40722 		{
40723 		  /* OpenACC permits 'update capture'.  */
40724 		  if (openacc
40725 		      && code == OMP_ATOMIC
40726 		      && new_code == OMP_ATOMIC_CAPTURE_NEW)
40727 		    code = new_code;
40728 		  else if (code != ERROR_MARK)
40729 		    error_at (cloc, "too many atomic clauses");
40730 		  else
40731 		    code = new_code;
40732 		}
40733 	      else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40734 		{
40735 		  if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
40736 		    error_at (cloc, "too many memory order clauses");
40737 		  else
40738 		    memory_order = new_memory_order;
40739 		}
40740 	      else if (new_capture)
40741 		{
40742 		  if (capture)
40743 		    error_at (cloc, "too many %qs clauses", "capture");
40744 		  else
40745 		    capture = true;
40746 		}
40747 	      else if (new_compare)
40748 		{
40749 		  if (compare)
40750 		    error_at (cloc, "too many %qs clauses", "compare");
40751 		  else
40752 		    compare = true;
40753 		}
40754 	      else if (new_weak)
40755 		{
40756 		  if (weak)
40757 		    error_at (cloc, "too many %qs clauses", "weak");
40758 		  else
40759 		    weak = true;
40760 		}
40761 	      cp_lexer_consume_token (parser->lexer);
40762 	      continue;
40763 	    }
40764 	}
40765       break;
40766     }
40767   cp_parser_require_pragma_eol (parser, pragma_tok);
40768 
40769   if (code == ERROR_MARK)
40770     code = OMP_ATOMIC;
40771   if (capture)
40772     {
40773       if (code != OMP_ATOMIC)
40774 	error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40775 		       "clauses", "capture");
40776       else
40777 	code = OMP_ATOMIC_CAPTURE_NEW;
40778     }
40779   if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
40780     {
40781       error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
40782 		     "clauses", "compare");
40783       compare = false;
40784     }
40785   if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
40786     {
40787       error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
40788       fail = OMP_MEMORY_ORDER_UNSPECIFIED;
40789     }
40790   if (weak && !compare)
40791     {
40792       error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
40793       weak = false;
40794     }
40795   if (openacc)
40796     memory_order = OMP_MEMORY_ORDER_RELAXED;
40797   else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
40798     {
40799       omp_requires_mask
40800 	= (enum omp_requires) (omp_requires_mask
40801 			       | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
40802       switch ((enum omp_memory_order)
40803 	      (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
40804 	{
40805 	case OMP_MEMORY_ORDER_UNSPECIFIED:
40806 	case OMP_MEMORY_ORDER_RELAXED:
40807 	  memory_order = OMP_MEMORY_ORDER_RELAXED;
40808 	  break;
40809 	case OMP_MEMORY_ORDER_SEQ_CST:
40810 	  memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40811 	  break;
40812 	case OMP_MEMORY_ORDER_ACQ_REL:
40813 	  switch (code)
40814 	    {
40815 	    case OMP_ATOMIC_READ:
40816 	      memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40817 	      break;
40818 	    case NOP_EXPR: /* atomic write */
40819 	      memory_order = OMP_MEMORY_ORDER_RELEASE;
40820 	      break;
40821 	    default:
40822 	      memory_order = OMP_MEMORY_ORDER_ACQ_REL;
40823 	      break;
40824 	    }
40825 	  break;
40826 	default:
40827 	  gcc_unreachable ();
40828 	}
40829     }
40830   else
40831     switch (code)
40832       {
40833       case OMP_ATOMIC_READ:
40834 	if (memory_order == OMP_MEMORY_ORDER_RELEASE)
40835 	  {
40836 	    error_at (loc, "%<#pragma omp atomic read%> incompatible with "
40837 			   "%<release%> clause");
40838 	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40839 	  }
40840 	else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40841 	  memory_order = OMP_MEMORY_ORDER_ACQUIRE;
40842 	break;
40843       case NOP_EXPR: /* atomic write */
40844 	if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
40845 	  {
40846 	    error_at (loc, "%<#pragma omp atomic write%> incompatible with "
40847 			   "%<acquire%> clause");
40848 	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
40849 	  }
40850 	else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
40851 	  memory_order = OMP_MEMORY_ORDER_RELEASE;
40852 	break;
40853       default:
40854 	break;
40855       }
40856   if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
40857     memory_order
40858       = (enum omp_memory_order) (memory_order
40859 				 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
40860 
40861   switch (code)
40862     {
40863     case OMP_ATOMIC_READ:
40864     case NOP_EXPR: /* atomic write */
40865       v = cp_parser_unary_expression (parser);
40866       if (v == error_mark_node)
40867 	goto saw_error;
40868       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40869 	goto saw_error;
40870       if (code == NOP_EXPR)
40871 	lhs = cp_parser_expression (parser);
40872       else
40873 	lhs = cp_parser_unary_expression (parser);
40874       if (lhs == error_mark_node)
40875 	goto saw_error;
40876       if (code == NOP_EXPR)
40877 	{
40878 	  /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
40879 	     opcode.  */
40880 	  code = OMP_ATOMIC;
40881 	  rhs = lhs;
40882 	  lhs = v;
40883 	  v = NULL_TREE;
40884 	}
40885       goto done;
40886     case OMP_ATOMIC_CAPTURE_NEW:
40887       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
40888 	{
40889 	  cp_lexer_consume_token (parser->lexer);
40890 	  structured_block = true;
40891 	}
40892       else if (compare
40893 	       && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40894 	break;
40895       else
40896 	{
40897 	  v = cp_parser_unary_expression (parser);
40898 	  if (v == error_mark_node)
40899 	    goto saw_error;
40900 	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40901 	    goto saw_error;
40902 	  if (compare
40903 	      && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40904 	    {
40905 	      location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40906 	      error_at (eloc, "expected expression");
40907 	      goto saw_error;
40908 	    }
40909 	}
40910     default:
40911       break;
40912     }
40913 
40914 restart:
40915   if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
40916     {
40917       cp_lexer_consume_token (parser->lexer);
40918 
40919       matching_parens parens;
40920       if (!parens.require_open (parser))
40921 	goto saw_error;
40922       location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
40923       tree cmp_expr;
40924       if (r)
40925 	cmp_expr = cp_parser_unary_expression (parser);
40926       else
40927 	cmp_expr = cp_parser_binary_expression (parser, false, true,
40928 						PREC_NOT_OPERATOR, NULL);
40929       if (!parens.require_close (parser))
40930 	cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40931       if (cmp_expr == error_mark_node)
40932 	goto saw_error;
40933       if (r)
40934 	{
40935 	  if (!cp_tree_equal (cmp_expr, r))
40936 	    goto bad_if;
40937 	  cmp_expr = rhs;
40938 	  rhs = NULL_TREE;
40939 	  gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
40940 	}
40941       if (TREE_CODE (cmp_expr) == EQ_EXPR)
40942 	;
40943       else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
40944 	{
40945 	  error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40946 		    "expected %<==%> comparison in %<if%> condition");
40947 	  goto saw_error;
40948 	}
40949       else if (TREE_CODE (cmp_expr) != GT_EXPR
40950 	       && TREE_CODE (cmp_expr) != LT_EXPR)
40951 	{
40952 	  error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
40953 		    "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
40954 		    "condition");
40955 	  goto saw_error;
40956 	}
40957       if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
40958 	goto saw_error;
40959 
40960       extra_scope = true;
40961       eloc = cp_lexer_peek_token (parser->lexer)->location;
40962       lhs = cp_parser_unary_expression (parser);
40963       orig_lhs = lhs;
40964       if (lhs == error_mark_node)
40965 	goto saw_error;
40966       if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
40967 	{
40968 	  cp_parser_error (parser, "expected %<=%>");
40969 	  goto saw_error;
40970 	}
40971       cp_lexer_consume_token (parser->lexer);
40972       eloc = cp_lexer_peek_token (parser->lexer)->location;
40973       if (TREE_CODE (cmp_expr) == EQ_EXPR)
40974 	rhs1 = cp_parser_expression (parser);
40975       else
40976 	rhs1 = cp_parser_simple_cast_expression (parser);
40977 
40978       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
40979 	goto saw_error;
40980 
40981       if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
40982 	goto saw_error;
40983 
40984       extra_scope = false;
40985       no_semicolon = true;
40986 
40987       if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
40988 	{
40989 	  if (TREE_CODE (cmp_expr) == EQ_EXPR)
40990 	    {
40991 	      opcode = COND_EXPR;
40992 	      rhs = TREE_OPERAND (cmp_expr, 1);
40993 	    }
40994 	  else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
40995 	    {
40996 	      opcode = (TREE_CODE (cmp_expr) == GT_EXPR
40997 			? MIN_EXPR : MAX_EXPR);
40998 	      rhs = rhs1;
40999 	      rhs1 = TREE_OPERAND (cmp_expr, 0);
41000 	    }
41001 	  else
41002 	    goto bad_if;
41003 	}
41004       else if (TREE_CODE (cmp_expr) == EQ_EXPR)
41005 	goto bad_if;
41006       else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
41007 	       && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
41008 	{
41009 	  opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41010 		    ? MAX_EXPR : MIN_EXPR);
41011 	  rhs = rhs1;
41012 	  rhs1 = TREE_OPERAND (cmp_expr, 1);
41013 	}
41014       else
41015 	{
41016 	bad_if:
41017 	  cp_parser_error (parser,
41018 			   "invalid form of %<#pragma omp atomic compare%>");
41019 	  goto saw_error;
41020 	}
41021 
41022       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
41023 	{
41024 	  if (code != OMP_ATOMIC_CAPTURE_NEW
41025 	      || (structured_block && r == NULL_TREE)
41026 	      || TREE_CODE (cmp_expr) != EQ_EXPR)
41027 	    {
41028 	      eloc = cp_lexer_peek_token (parser->lexer)->location;
41029 	      error_at (eloc, "unexpected %<else%>");
41030 	      goto saw_error;
41031 	    }
41032 
41033 	  cp_lexer_consume_token (parser->lexer);
41034 
41035 	  if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41036 	    goto saw_error;
41037 
41038 	  extra_scope = true;
41039 	  v = cp_parser_unary_expression (parser);
41040 	  if (v == error_mark_node)
41041 	    goto saw_error;
41042 	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41043 	    goto saw_error;
41044 
41045 	  tree expr = cp_parser_simple_cast_expression (parser);
41046 
41047 	  if (!cp_tree_equal (expr, lhs))
41048 	    goto bad_if;
41049 
41050 	  if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41051 	    goto saw_error;
41052 
41053 	  if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41054 	    goto saw_error;
41055 
41056 	  extra_scope = false;
41057 	  code = OMP_ATOMIC_CAPTURE_OLD;
41058 	  if (r == NULL_TREE)
41059 	    /* Signal to c_finish_omp_atomic that in
41060 	       if (x == e) { x = d; } else { v = x; }
41061 	       case the store to v should be conditional.  */
41062 	    r = void_list_node;
41063 	}
41064       else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41065 	{
41066 	  cp_parser_error (parser, "expected %<else%>");
41067 	  goto saw_error;
41068 	}
41069       else if (code == OMP_ATOMIC_CAPTURE_NEW
41070 	       && r != NULL_TREE
41071 	       && v == NULL_TREE)
41072 	code = OMP_ATOMIC;
41073       goto stmt_done;
41074     }
41075   lhs = cp_parser_unary_expression (parser);
41076   orig_lhs = lhs;
41077   switch (TREE_CODE (lhs))
41078     {
41079     case ERROR_MARK:
41080       goto saw_error;
41081 
41082     case POSTINCREMENT_EXPR:
41083       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41084 	code = OMP_ATOMIC_CAPTURE_OLD;
41085       /* FALLTHROUGH */
41086     case PREINCREMENT_EXPR:
41087       lhs = TREE_OPERAND (lhs, 0);
41088       opcode = PLUS_EXPR;
41089       rhs = integer_one_node;
41090       if (compare)
41091 	goto invalid_compare;
41092       break;
41093 
41094     case POSTDECREMENT_EXPR:
41095       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41096 	code = OMP_ATOMIC_CAPTURE_OLD;
41097       /* FALLTHROUGH */
41098     case PREDECREMENT_EXPR:
41099       lhs = TREE_OPERAND (lhs, 0);
41100       opcode = MINUS_EXPR;
41101       rhs = integer_one_node;
41102       if (compare)
41103 	goto invalid_compare;
41104       break;
41105 
41106     case COMPOUND_EXPR:
41107       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
41108 	 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
41109 	 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
41110 	 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
41111 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
41112 					     (TREE_OPERAND (lhs, 1), 0), 0)))
41113 	    == BOOLEAN_TYPE)
41114        /* Undo effects of boolean_increment for post {in,de}crement.  */
41115        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
41116       /* FALLTHRU */
41117     case MODIFY_EXPR:
41118       if (TREE_CODE (lhs) == MODIFY_EXPR
41119 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
41120 	{
41121 	  /* Undo effects of boolean_increment.  */
41122 	  if (integer_onep (TREE_OPERAND (lhs, 1)))
41123 	    {
41124 	      /* This is pre or post increment.  */
41125 	      rhs = TREE_OPERAND (lhs, 1);
41126 	      lhs = TREE_OPERAND (lhs, 0);
41127 	      opcode = NOP_EXPR;
41128 	      if (code == OMP_ATOMIC_CAPTURE_NEW
41129 		  && !structured_block
41130 		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
41131 		code = OMP_ATOMIC_CAPTURE_OLD;
41132 	      if (compare)
41133 		goto invalid_compare;
41134 	      break;
41135 	    }
41136 	}
41137       /* FALLTHRU */
41138     default:
41139       if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
41140 	{
41141 	  cp_parser_error (parser, "expected %<=%>");
41142 	  goto saw_error;
41143 	}
41144       switch (cp_lexer_peek_token (parser->lexer)->type)
41145 	{
41146 	case CPP_MULT_EQ:
41147 	  opcode = MULT_EXPR;
41148 	  break;
41149 	case CPP_DIV_EQ:
41150 	  opcode = TRUNC_DIV_EXPR;
41151 	  break;
41152 	case CPP_PLUS_EQ:
41153 	  opcode = PLUS_EXPR;
41154 	  break;
41155 	case CPP_MINUS_EQ:
41156 	  opcode = MINUS_EXPR;
41157 	  break;
41158 	case CPP_LSHIFT_EQ:
41159 	  opcode = LSHIFT_EXPR;
41160 	  break;
41161 	case CPP_RSHIFT_EQ:
41162 	  opcode = RSHIFT_EXPR;
41163 	  break;
41164 	case CPP_AND_EQ:
41165 	  opcode = BIT_AND_EXPR;
41166 	  break;
41167 	case CPP_OR_EQ:
41168 	  opcode = BIT_IOR_EXPR;
41169 	  break;
41170 	case CPP_XOR_EQ:
41171 	  opcode = BIT_XOR_EXPR;
41172 	  break;
41173 	case CPP_EQ:
41174 	  enum cp_parser_prec oprec;
41175 	  cp_token *token;
41176 	  cp_lexer_consume_token (parser->lexer);
41177 	  cp_parser_parse_tentatively (parser);
41178 	  rhs1 = cp_parser_simple_cast_expression (parser);
41179 	  if (rhs1 == error_mark_node)
41180 	    {
41181 	      cp_parser_abort_tentative_parse (parser);
41182 	      cp_parser_simple_cast_expression (parser);
41183 	      goto saw_error;
41184 	    }
41185 	  token = cp_lexer_peek_token (parser->lexer);
41186 	  if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
41187 	    {
41188 	      cp_parser_abort_tentative_parse (parser);
41189 	      cp_parser_parse_tentatively (parser);
41190 	      rhs = cp_parser_binary_expression (parser, false, true,
41191 						 PREC_NOT_OPERATOR, NULL);
41192 	      if (rhs == error_mark_node)
41193 		{
41194 		  cp_parser_abort_tentative_parse (parser);
41195 		  cp_parser_binary_expression (parser, false, true,
41196 					       PREC_NOT_OPERATOR, NULL);
41197 		  goto saw_error;
41198 		}
41199 	      switch (TREE_CODE (rhs))
41200 		{
41201 		case MULT_EXPR:
41202 		case TRUNC_DIV_EXPR:
41203 		case RDIV_EXPR:
41204 		case PLUS_EXPR:
41205 		case MINUS_EXPR:
41206 		case LSHIFT_EXPR:
41207 		case RSHIFT_EXPR:
41208 		case BIT_AND_EXPR:
41209 		case BIT_IOR_EXPR:
41210 		case BIT_XOR_EXPR:
41211 		  if (compare)
41212 		    break;
41213 		  if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
41214 		    {
41215 		      if (cp_parser_parse_definitely (parser))
41216 			{
41217 			  opcode = TREE_CODE (rhs);
41218 			  rhs1 = TREE_OPERAND (rhs, 0);
41219 			  rhs = TREE_OPERAND (rhs, 1);
41220 			  goto stmt_done;
41221 			}
41222 		      else
41223 			goto saw_error;
41224 		    }
41225 		  break;
41226 		case EQ_EXPR:
41227 		  if (!compare
41228 		      || code != OMP_ATOMIC_CAPTURE_NEW
41229 		      || !structured_block
41230 		      || v
41231 		      || r)
41232 		    break;
41233 		  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
41234 		      && cp_lexer_nth_token_is_keyword (parser->lexer,
41235 							2, RID_IF))
41236 		    {
41237 		      if (cp_parser_parse_definitely (parser))
41238 			{
41239 			  r = lhs;
41240 			  lhs = NULL_TREE;
41241 			  rhs1 = NULL_TREE;
41242 			  cp_lexer_consume_token (parser->lexer);
41243 			  goto restart;
41244 			}
41245 		    }
41246 		  break;
41247 		case GT_EXPR:
41248 		case LT_EXPR:
41249 		  if (compare
41250 		      && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
41251 		      && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
41252 		      && cp_parser_parse_definitely (parser))
41253 		    {
41254 		      opcode = TREE_CODE (rhs);
41255 		      rhs1 = TREE_OPERAND (rhs, 0);
41256 		      rhs = TREE_OPERAND (rhs, 1);
41257 		     cond_expr:
41258 		      cp_lexer_consume_token (parser->lexer);
41259 		      bool saved_colon_corrects_to_scope_p
41260 			= parser->colon_corrects_to_scope_p;
41261 		      parser->colon_corrects_to_scope_p = false;
41262 		      tree e1 = cp_parser_expression (parser);
41263 		      parser->colon_corrects_to_scope_p
41264 			= saved_colon_corrects_to_scope_p;
41265 		      cp_parser_require (parser, CPP_COLON, RT_COLON);
41266 		      tree e2 = cp_parser_simple_cast_expression (parser);
41267 		      if (cp_tree_equal (lhs, e2))
41268 			{
41269 			  if (cp_tree_equal (lhs, rhs1))
41270 			    {
41271 			      if (opcode == EQ_EXPR)
41272 				{
41273 				  opcode = COND_EXPR;
41274 				  rhs1 = e1;
41275 				  goto stmt_done;
41276 				}
41277 			      if (cp_tree_equal (rhs, e1))
41278 				{
41279 				  opcode
41280 				    = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
41281 				  rhs = e1;
41282 				  goto stmt_done;
41283 				}
41284 			    }
41285 			  else
41286 			    {
41287 			      gcc_assert (opcode != EQ_EXPR);
41288 			      if (cp_tree_equal (rhs1, e1))
41289 				{
41290 				  opcode
41291 				    = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
41292 				  rhs1 = rhs;
41293 				  rhs = e1;
41294 				  goto stmt_done;
41295 				}
41296 			    }
41297 			}
41298 		      cp_parser_error (parser,
41299 				       "invalid form of "
41300 				       "%<#pragma omp atomic compare%>");
41301 		      goto saw_error;
41302 		    }
41303 		  break;
41304 		default:
41305 		  break;
41306 		}
41307 	      cp_parser_abort_tentative_parse (parser);
41308 	      if (structured_block
41309 		  && code == OMP_ATOMIC_CAPTURE_OLD
41310 		  && !compare)
41311 		{
41312 		  rhs = cp_parser_expression (parser);
41313 		  if (rhs == error_mark_node)
41314 		    goto saw_error;
41315 		  opcode = NOP_EXPR;
41316 		  rhs1 = NULL_TREE;
41317 		  goto stmt_done;
41318 		}
41319 	      cp_parser_error (parser,
41320 			       "invalid form of %<#pragma omp atomic%>");
41321 	      goto saw_error;
41322 	    }
41323 	  if (!cp_parser_parse_definitely (parser))
41324 	    goto saw_error;
41325 	  switch (token->type)
41326 	    {
41327 	    case CPP_SEMICOLON:
41328 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41329 		{
41330 		  code = OMP_ATOMIC_CAPTURE_OLD;
41331 		  v = lhs;
41332 		  lhs = NULL_TREE;
41333 		  lhs1 = rhs1;
41334 		  rhs1 = NULL_TREE;
41335 		  cp_lexer_consume_token (parser->lexer);
41336 		  goto restart;
41337 		}
41338 	      else if (structured_block && !compare)
41339 		{
41340 		  opcode = NOP_EXPR;
41341 		  rhs = rhs1;
41342 		  rhs1 = NULL_TREE;
41343 		  goto stmt_done;
41344 		}
41345 	      cp_parser_error (parser,
41346 			       "invalid form of %<#pragma omp atomic%>");
41347 	      goto saw_error;
41348 	    case CPP_MULT:
41349 	      opcode = MULT_EXPR;
41350 	      break;
41351 	    case CPP_DIV:
41352 	      opcode = TRUNC_DIV_EXPR;
41353 	      break;
41354 	    case CPP_PLUS:
41355 	      opcode = PLUS_EXPR;
41356 	      break;
41357 	    case CPP_MINUS:
41358 	      opcode = MINUS_EXPR;
41359 	      break;
41360 	    case CPP_LSHIFT:
41361 	      opcode = LSHIFT_EXPR;
41362 	      break;
41363 	    case CPP_RSHIFT:
41364 	      opcode = RSHIFT_EXPR;
41365 	      break;
41366 	    case CPP_AND:
41367 	      opcode = BIT_AND_EXPR;
41368 	      break;
41369 	    case CPP_OR:
41370 	      opcode = BIT_IOR_EXPR;
41371 	      break;
41372 	    case CPP_XOR:
41373 	      opcode = BIT_XOR_EXPR;
41374 	      break;
41375 	    case CPP_EQ_EQ:
41376 	      opcode = EQ_EXPR;
41377 	      break;
41378 	    case CPP_GREATER:
41379 	      opcode = GT_EXPR;
41380 	      break;
41381 	    case CPP_LESS:
41382 	      opcode = LT_EXPR;
41383 	      break;
41384 	    default:
41385 	      cp_parser_error (parser,
41386 			       "invalid operator for %<#pragma omp atomic%>");
41387 	      goto saw_error;
41388 	    }
41389 	  if (compare
41390 	      && TREE_CODE_CLASS (opcode) != tcc_comparison)
41391 	    {
41392 	      cp_parser_error (parser,
41393 			       "invalid form of "
41394 			       "%<#pragma omp atomic compare%>");
41395 	      goto saw_error;
41396 	    }
41397 	  oprec = TOKEN_PRECEDENCE (token);
41398 	  gcc_assert (oprec != PREC_NOT_OPERATOR);
41399 	  if (commutative_tree_code (opcode))
41400 	    oprec = (enum cp_parser_prec) (oprec - 1);
41401 	  cp_lexer_consume_token (parser->lexer);
41402 	  rhs = cp_parser_binary_expression (parser, false, false,
41403 					     oprec, NULL);
41404 	  if (rhs == error_mark_node)
41405 	    goto saw_error;
41406 	  if (compare)
41407 	    {
41408 	      if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
41409 		{
41410 		  cp_parser_error (parser,
41411 				   "invalid form of "
41412 				   "%<#pragma omp atomic compare%>");
41413 		  goto saw_error;
41414 		}
41415 	      goto cond_expr;
41416 	    }
41417 	  goto stmt_done;
41418 	default:
41419 	  cp_parser_error (parser,
41420 			   "invalid operator for %<#pragma omp atomic%>");
41421 	  goto saw_error;
41422 	}
41423       cp_lexer_consume_token (parser->lexer);
41424 
41425       rhs = cp_parser_expression (parser);
41426       if (rhs == error_mark_node)
41427 	goto saw_error;
41428       break;
41429     }
41430 stmt_done:
41431   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
41432     {
41433       if (!no_semicolon
41434 	  && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41435 	goto saw_error;
41436       no_semicolon = false;
41437       v = cp_parser_unary_expression (parser);
41438       if (v == error_mark_node)
41439 	goto saw_error;
41440       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41441 	goto saw_error;
41442       lhs1 = cp_parser_unary_expression (parser);
41443       if (lhs1 == error_mark_node)
41444 	goto saw_error;
41445     }
41446   if (structured_block)
41447     {
41448       if (!no_semicolon)
41449 	cp_parser_consume_semicolon_at_end_of_statement (parser);
41450       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
41451     }
41452 done:
41453   if (weak && opcode != COND_EXPR)
41454     {
41455       error_at (loc, "%<weak%> clause requires atomic equality comparison");
41456       weak = false;
41457     }
41458   clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41459   finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
41460 		     rhs1, r, clauses, memory_order, weak);
41461   if (!structured_block && !no_semicolon)
41462     cp_parser_consume_semicolon_at_end_of_statement (parser);
41463   return;
41464 
41465  invalid_compare:
41466   error ("invalid form of %<pragma omp atomic compare%>");
41467   /* FALLTHRU */
41468  saw_error:
41469   cp_parser_skip_to_end_of_block_or_statement (parser);
41470   if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41471     cp_lexer_consume_token (parser->lexer);
41472   if (structured_block)
41473     {
41474       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41475         cp_lexer_consume_token (parser->lexer);
41476       else if (code == OMP_ATOMIC_CAPTURE_NEW)
41477 	{
41478 	  cp_parser_skip_to_end_of_block_or_statement (parser);
41479 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41480 	    cp_lexer_consume_token (parser->lexer);
41481 	}
41482     }
41483 }
41484 
41485 
41486 /* OpenMP 2.5:
41487    # pragma omp barrier new-line  */
41488 
41489 static void
cp_parser_omp_barrier(cp_parser * parser,cp_token * pragma_tok)41490 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
41491 {
41492   cp_parser_require_pragma_eol (parser, pragma_tok);
41493   finish_omp_barrier ();
41494 }
41495 
41496 /* OpenMP 2.5:
41497    # pragma omp critical [(name)] new-line
41498      structured-block
41499 
41500    OpenMP 4.5:
41501    # pragma omp critical [(name) [hint(expression)]] new-line
41502      structured-block  */
41503 
41504 #define OMP_CRITICAL_CLAUSE_MASK		\
41505 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41506 
41507 static tree
cp_parser_omp_critical(cp_parser * parser,cp_token * pragma_tok,bool * if_p)41508 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41509 {
41510   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
41511 
41512   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41513     {
41514       matching_parens parens;
41515       parens.consume_open (parser);
41516 
41517       name = cp_parser_identifier (parser);
41518 
41519       if (name == error_mark_node
41520 	  || !parens.require_close (parser))
41521 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41522 					       /*or_comma=*/false,
41523 					       /*consume_paren=*/true);
41524       if (name == error_mark_node)
41525 	name = NULL;
41526 
41527       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41528 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41529 	cp_lexer_consume_token (parser->lexer);
41530     }
41531 
41532   clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
41533 				       "#pragma omp critical", pragma_tok);
41534 
41535   stmt = cp_parser_omp_structured_block (parser, if_p);
41536   return c_finish_omp_critical (input_location, stmt, name, clauses);
41537 }
41538 
41539 /* OpenMP 5.0:
41540    # pragma omp depobj ( depobj ) depobj-clause new-line
41541 
41542    depobj-clause:
41543      depend (dependence-type : locator)
41544      destroy
41545      update (dependence-type)
41546 
41547    dependence-type:
41548      in
41549      out
41550      inout
41551      mutexinout  */
41552 
41553 static void
cp_parser_omp_depobj(cp_parser * parser,cp_token * pragma_tok)41554 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
41555 {
41556   location_t loc = pragma_tok->location;
41557   matching_parens parens;
41558   if (!parens.require_open (parser))
41559     {
41560       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41561       return;
41562     }
41563 
41564   tree depobj = cp_parser_assignment_expression (parser);
41565 
41566   if (!parens.require_close (parser))
41567     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41568 					   /*or_comma=*/false,
41569 					   /*consume_paren=*/true);
41570 
41571   tree clause = NULL_TREE;
41572   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
41573   location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
41574   /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
41575   if (parser->lexer->in_omp_attribute_pragma
41576       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41577     cp_lexer_consume_token (parser->lexer);
41578   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41579     {
41580       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41581       const char *p = IDENTIFIER_POINTER (id);
41582 
41583       cp_lexer_consume_token (parser->lexer);
41584       if (!strcmp ("depend", p))
41585 	{
41586 	  /* Don't create location wrapper nodes within the depend clause.  */
41587 	  auto_suppress_location_wrappers sentinel;
41588 	  clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
41589 	  if (clause)
41590 	    clause = finish_omp_clauses (clause, C_ORT_OMP);
41591 	  if (!clause)
41592 	    clause = error_mark_node;
41593 	}
41594       else if (!strcmp ("destroy", p))
41595 	kind = OMP_CLAUSE_DEPEND_LAST;
41596       else if (!strcmp ("update", p))
41597 	{
41598 	  matching_parens c_parens;
41599 	  if (c_parens.require_open (parser))
41600 	    {
41601 	      location_t c2_loc
41602 		= cp_lexer_peek_token (parser->lexer)->location;
41603 	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41604 		{
41605 		  tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
41606 		  const char *p2 = IDENTIFIER_POINTER (id2);
41607 
41608 		  cp_lexer_consume_token (parser->lexer);
41609 		  if (!strcmp ("in", p2))
41610 		    kind = OMP_CLAUSE_DEPEND_IN;
41611 		  else if (!strcmp ("out", p2))
41612 		    kind = OMP_CLAUSE_DEPEND_OUT;
41613 		  else if (!strcmp ("inout", p2))
41614 		    kind = OMP_CLAUSE_DEPEND_INOUT;
41615 		  else if (!strcmp ("mutexinoutset", p2))
41616 		    kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41617 		}
41618 	      if (kind == OMP_CLAUSE_DEPEND_SOURCE)
41619 		{
41620 		  clause = error_mark_node;
41621 		  error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
41622 				    "%<mutexinoutset%>");
41623 		}
41624 	      if (!c_parens.require_close (parser))
41625 		cp_parser_skip_to_closing_parenthesis (parser,
41626 						       /*recovering=*/true,
41627 						       /*or_comma=*/false,
41628 						       /*consume_paren=*/true);
41629 	    }
41630 	  else
41631 	    clause = error_mark_node;
41632 	}
41633     }
41634   if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
41635     {
41636       clause = error_mark_node;
41637       error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
41638     }
41639   cp_parser_require_pragma_eol (parser, pragma_tok);
41640 
41641   finish_omp_depobj (loc, depobj, kind, clause);
41642 }
41643 
41644 
41645 /* OpenMP 2.5:
41646    # pragma omp flush flush-vars[opt] new-line
41647 
41648    flush-vars:
41649      ( variable-list )
41650 
41651    OpenMP 5.0:
41652    # pragma omp flush memory-order-clause new-line  */
41653 
41654 static void
cp_parser_omp_flush(cp_parser * parser,cp_token * pragma_tok)41655 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
41656 {
41657   enum memmodel mo = MEMMODEL_LAST;
41658   /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
41659   if (parser->lexer->in_omp_attribute_pragma
41660       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41661       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41662     cp_lexer_consume_token (parser->lexer);
41663   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41664     {
41665       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41666       const char *p = IDENTIFIER_POINTER (id);
41667       if (!strcmp (p, "seq_cst"))
41668 	mo = MEMMODEL_SEQ_CST;
41669       else if (!strcmp (p, "acq_rel"))
41670 	mo = MEMMODEL_ACQ_REL;
41671       else if (!strcmp (p, "release"))
41672 	mo = MEMMODEL_RELEASE;
41673       else if (!strcmp (p, "acquire"))
41674 	mo = MEMMODEL_ACQUIRE;
41675       else
41676 	error_at (cp_lexer_peek_token (parser->lexer)->location,
41677 		  "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
41678 		  "%<acquire%>");
41679       cp_lexer_consume_token (parser->lexer);
41680     }
41681   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41682     {
41683       if (mo != MEMMODEL_LAST)
41684 	error_at (cp_lexer_peek_token (parser->lexer)->location,
41685 		  "%<flush%> list specified together with memory order "
41686 		  "clause");
41687       (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
41688     }
41689   cp_parser_require_pragma_eol (parser, pragma_tok);
41690 
41691   finish_omp_flush (mo);
41692 }
41693 
41694 /* Helper function, to parse omp for increment expression.  */
41695 
41696 static tree
cp_parser_omp_for_cond(cp_parser * parser,tree decl,enum tree_code code)41697 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
41698 {
41699   tree cond = cp_parser_binary_expression (parser, false, true,
41700 					   PREC_NOT_OPERATOR, NULL);
41701   if (cond == error_mark_node
41702       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
41703     {
41704       cp_parser_skip_to_end_of_statement (parser);
41705       return error_mark_node;
41706     }
41707 
41708   switch (TREE_CODE (cond))
41709     {
41710     case GT_EXPR:
41711     case GE_EXPR:
41712     case LT_EXPR:
41713     case LE_EXPR:
41714       break;
41715     case NE_EXPR:
41716       if (code != OACC_LOOP)
41717 	break;
41718       gcc_fallthrough ();
41719     default:
41720       return error_mark_node;
41721     }
41722 
41723   /* If decl is an iterator, preserve LHS and RHS of the relational
41724      expr until finish_omp_for.  */
41725   if (decl
41726       && (type_dependent_expression_p (decl)
41727 	  || CLASS_TYPE_P (TREE_TYPE (decl))))
41728     return cond;
41729 
41730   return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
41731 			    TREE_CODE (cond),
41732 			    TREE_OPERAND (cond, 0), ERROR_MARK,
41733 			    TREE_OPERAND (cond, 1), ERROR_MARK,
41734 			    NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
41735 }
41736 
41737 /* Helper function, to parse omp for increment expression.  */
41738 
41739 static tree
cp_parser_omp_for_incr(cp_parser * parser,tree decl)41740 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
41741 {
41742   cp_token *token = cp_lexer_peek_token (parser->lexer);
41743   enum tree_code op;
41744   tree lhs, rhs;
41745   cp_id_kind idk;
41746   bool decl_first;
41747 
41748   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41749     {
41750       op = (token->type == CPP_PLUS_PLUS
41751 	    ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
41752       cp_lexer_consume_token (parser->lexer);
41753       lhs = cp_parser_simple_cast_expression (parser);
41754       if (lhs != decl
41755 	  && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41756 	return error_mark_node;
41757       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41758     }
41759 
41760   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
41761   if (lhs != decl
41762       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
41763     return error_mark_node;
41764 
41765   token = cp_lexer_peek_token (parser->lexer);
41766   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
41767     {
41768       op = (token->type == CPP_PLUS_PLUS
41769 	    ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
41770       cp_lexer_consume_token (parser->lexer);
41771       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
41772     }
41773 
41774   op = cp_parser_assignment_operator_opt (parser);
41775   if (op == ERROR_MARK)
41776     return error_mark_node;
41777 
41778   if (op != NOP_EXPR)
41779     {
41780       rhs = cp_parser_assignment_expression (parser);
41781       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
41782       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41783     }
41784 
41785   lhs = cp_parser_binary_expression (parser, false, false,
41786 				     PREC_ADDITIVE_EXPRESSION, NULL);
41787   token = cp_lexer_peek_token (parser->lexer);
41788   decl_first = (lhs == decl
41789 		|| (processing_template_decl && cp_tree_equal (lhs, decl)));
41790   if (decl_first)
41791     lhs = NULL_TREE;
41792   if (token->type != CPP_PLUS
41793       && token->type != CPP_MINUS)
41794     return error_mark_node;
41795 
41796   do
41797     {
41798       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
41799       cp_lexer_consume_token (parser->lexer);
41800       rhs = cp_parser_binary_expression (parser, false, false,
41801 					 PREC_ADDITIVE_EXPRESSION, NULL);
41802       token = cp_lexer_peek_token (parser->lexer);
41803       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
41804 	{
41805 	  if (lhs == NULL_TREE)
41806 	    {
41807 	      if (op == PLUS_EXPR)
41808 		lhs = rhs;
41809 	      else
41810 		lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
41811 					NULL_TREE, tf_warning_or_error);
41812 	    }
41813 	  else
41814 	    lhs = build_x_binary_op (input_location, op,
41815 				     lhs, ERROR_MARK,
41816 				     rhs, ERROR_MARK,
41817 				     NULL_TREE, NULL, tf_warning_or_error);
41818 	}
41819     }
41820   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
41821 
41822   if (!decl_first)
41823     {
41824       if ((rhs != decl
41825 	   && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
41826 	  || op == MINUS_EXPR)
41827 	return error_mark_node;
41828       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
41829     }
41830   else
41831     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
41832 
41833   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
41834 }
41835 
41836 /* Parse the initialization statement of an OpenMP for loop.
41837 
41838    Return true if the resulting construct should have an
41839    OMP_CLAUSE_PRIVATE added to it.  */
41840 
41841 static tree
cp_parser_omp_for_loop_init(cp_parser * parser,tree & this_pre_body,releasing_vec & for_block,tree & init,tree & orig_init,tree & decl,tree & real_decl)41842 cp_parser_omp_for_loop_init (cp_parser *parser,
41843 			     tree &this_pre_body,
41844 			     releasing_vec &for_block,
41845 			     tree &init,
41846 			     tree &orig_init,
41847 			     tree &decl,
41848 			     tree &real_decl)
41849 {
41850   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
41851     return NULL_TREE;
41852 
41853   tree add_private_clause = NULL_TREE;
41854 
41855   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
41856 
41857      init-expr:
41858      var = lb
41859      integer-type var = lb
41860      random-access-iterator-type var = lb
41861      pointer-type var = lb
41862   */
41863   cp_decl_specifier_seq type_specifiers;
41864 
41865   /* First, try to parse as an initialized declaration.  See
41866      cp_parser_condition, from whence the bulk of this is copied.  */
41867 
41868   cp_parser_parse_tentatively (parser);
41869   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
41870 				/*is_declaration=*/true,
41871 				/*is_trailing_return=*/false,
41872 				&type_specifiers);
41873   if (cp_parser_parse_definitely (parser))
41874     {
41875       /* If parsing a type specifier seq succeeded, then this
41876 	 MUST be a initialized declaration.  */
41877       tree asm_specification, attributes;
41878       cp_declarator *declarator;
41879 
41880       declarator = cp_parser_declarator (parser,
41881 					 CP_PARSER_DECLARATOR_NAMED,
41882 					 CP_PARSER_FLAGS_NONE,
41883 					 /*ctor_dtor_or_conv_p=*/NULL,
41884 					 /*parenthesized_p=*/NULL,
41885 					 /*member_p=*/false,
41886 					 /*friend_p=*/false,
41887 					 /*static_p=*/false);
41888       attributes = cp_parser_attributes_opt (parser);
41889       asm_specification = cp_parser_asm_specification_opt (parser);
41890 
41891       if (declarator == cp_error_declarator)
41892 	cp_parser_skip_to_end_of_statement (parser);
41893 
41894       else
41895 	{
41896 	  tree pushed_scope, auto_node;
41897 
41898 	  decl = start_decl (declarator, &type_specifiers,
41899 			     SD_INITIALIZED, attributes,
41900 			     /*prefix_attributes=*/NULL_TREE,
41901 			     &pushed_scope);
41902 
41903 	  auto_node = type_uses_auto (TREE_TYPE (decl));
41904 	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
41905 	    {
41906 	      if (cp_lexer_next_token_is (parser->lexer,
41907 					  CPP_OPEN_PAREN))
41908 	        error ("parenthesized initialization is not allowed in "
41909 		       "OpenMP %<for%> loop");
41910 	      else
41911 		/* Trigger an error.  */
41912 		cp_parser_require (parser, CPP_EQ, RT_EQ);
41913 
41914 	      init = error_mark_node;
41915 	      cp_parser_skip_to_end_of_statement (parser);
41916 	    }
41917 	  else if (CLASS_TYPE_P (TREE_TYPE (decl))
41918 		   || type_dependent_expression_p (decl)
41919 		   || auto_node)
41920 	    {
41921 	      bool is_direct_init, is_non_constant_init;
41922 
41923 	      init = cp_parser_initializer (parser,
41924 					    &is_direct_init,
41925 					    &is_non_constant_init);
41926 
41927 	      if (auto_node)
41928 		{
41929 		  TREE_TYPE (decl)
41930 		    = do_auto_deduction (TREE_TYPE (decl), init,
41931 					 auto_node);
41932 
41933 		  if (!CLASS_TYPE_P (TREE_TYPE (decl))
41934 		      && !type_dependent_expression_p (decl))
41935 		    goto non_class;
41936 		}
41937 
41938 	      cp_finish_decl (decl, init, !is_non_constant_init,
41939 			      asm_specification,
41940 			      LOOKUP_ONLYCONVERTING);
41941 	      orig_init = init;
41942 	      if (CLASS_TYPE_P (TREE_TYPE (decl)))
41943 		{
41944 		  vec_safe_push (for_block, this_pre_body);
41945 		  init = NULL_TREE;
41946 		}
41947 	      else
41948 		{
41949 		  init = pop_stmt_list (this_pre_body);
41950 		  if (init && TREE_CODE (init) == STATEMENT_LIST)
41951 		    {
41952 		      tree_stmt_iterator i = tsi_start (init);
41953 		      /* Move lambda DECL_EXPRs to FOR_BLOCK.  */
41954 		      while (!tsi_end_p (i))
41955 			{
41956 			  tree t = tsi_stmt (i);
41957 			  if (TREE_CODE (t) == DECL_EXPR
41958 			      && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
41959 			    {
41960 			      tsi_delink (&i);
41961 			      vec_safe_push (for_block, t);
41962 			      continue;
41963 			    }
41964 			  break;
41965 			}
41966 		      if (tsi_one_before_end_p (i))
41967 			{
41968 			  tree t = tsi_stmt (i);
41969 			  tsi_delink (&i);
41970 			  free_stmt_list (init);
41971 			  init = t;
41972 			}
41973 		    }
41974 		}
41975 	      this_pre_body = NULL_TREE;
41976 	    }
41977 	  else
41978 	    {
41979 	      /* Consume '='.  */
41980 	      cp_lexer_consume_token (parser->lexer);
41981 	      init = cp_parser_assignment_expression (parser);
41982 
41983 	    non_class:
41984 	      if (TYPE_REF_P (TREE_TYPE (decl)))
41985 		init = error_mark_node;
41986 	      else
41987 		cp_finish_decl (decl, NULL_TREE,
41988 				/*init_const_expr_p=*/false,
41989 				asm_specification,
41990 				LOOKUP_ONLYCONVERTING);
41991 	    }
41992 
41993 	  if (pushed_scope)
41994 	    pop_scope (pushed_scope);
41995 	}
41996     }
41997   else
41998     {
41999       cp_id_kind idk;
42000       /* If parsing a type specifier sequence failed, then
42001 	 this MUST be a simple expression.  */
42002       cp_parser_parse_tentatively (parser);
42003       decl = cp_parser_primary_expression (parser, false, false,
42004 					   false, &idk);
42005       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
42006       if (!cp_parser_error_occurred (parser)
42007 	  && decl
42008 	  && (TREE_CODE (decl) == COMPONENT_REF
42009 	      || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
42010 	{
42011 	  cp_parser_abort_tentative_parse (parser);
42012 	  cp_parser_parse_tentatively (parser);
42013 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
42014 	  tree name = cp_parser_id_expression (parser, /*template_p=*/false,
42015 					       /*check_dependency_p=*/true,
42016 					       /*template_p=*/NULL,
42017 					       /*declarator_p=*/false,
42018 					       /*optional_p=*/false);
42019 	  if (name != error_mark_node
42020 	      && last_tok == cp_lexer_peek_token (parser->lexer))
42021 	    {
42022 	      decl = cp_parser_lookup_name_simple (parser, name,
42023 						   token->location);
42024 	      if (TREE_CODE (decl) == FIELD_DECL)
42025 		add_private_clause = omp_privatize_field (decl, false);
42026 	    }
42027 	  cp_parser_abort_tentative_parse (parser);
42028 	  cp_parser_parse_tentatively (parser);
42029 	  decl = cp_parser_primary_expression (parser, false, false,
42030 					       false, &idk);
42031 	}
42032       if (!cp_parser_error_occurred (parser)
42033 	  && decl
42034 	  && DECL_P (decl)
42035 	  && CLASS_TYPE_P (TREE_TYPE (decl)))
42036 	{
42037 	  tree rhs;
42038 
42039 	  cp_parser_parse_definitely (parser);
42040 	  cp_parser_require (parser, CPP_EQ, RT_EQ);
42041 	  rhs = cp_parser_assignment_expression (parser);
42042 	  orig_init = rhs;
42043 	  finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
42044 						 decl, NOP_EXPR,
42045 						 rhs, NULL_TREE,
42046 						 tf_warning_or_error));
42047 	  if (!add_private_clause)
42048 	    add_private_clause = decl;
42049 	}
42050       else
42051 	{
42052 	  decl = NULL;
42053 	  cp_parser_abort_tentative_parse (parser);
42054 	  init = cp_parser_expression (parser);
42055 	  if (init)
42056 	    {
42057 	      if (TREE_CODE (init) == MODIFY_EXPR
42058 		  || TREE_CODE (init) == MODOP_EXPR)
42059 		real_decl = TREE_OPERAND (init, 0);
42060 	    }
42061 	}
42062     }
42063   return add_private_clause;
42064 }
42065 
42066 /* Helper for cp_parser_omp_for_loop, handle one range-for loop.  */
42067 
42068 void
cp_convert_omp_range_for(tree & this_pre_body,vec<tree,va_gc> * for_block,tree & decl,tree & orig_decl,tree & init,tree & orig_init,tree & cond,tree & incr)42069 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
42070 			  tree &decl, tree &orig_decl, tree &init,
42071 			  tree &orig_init, tree &cond, tree &incr)
42072 {
42073   tree begin, end, range_temp_decl = NULL_TREE;
42074   tree iter_type, begin_expr, end_expr;
42075 
42076   if (processing_template_decl)
42077     {
42078       if (check_for_bare_parameter_packs (init))
42079 	init = error_mark_node;
42080       if (!type_dependent_expression_p (init)
42081 	  /* do_auto_deduction doesn't mess with template init-lists.  */
42082 	  && !BRACE_ENCLOSED_INITIALIZER_P (init))
42083 	{
42084 	  tree d = decl;
42085 	  if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
42086 	    {
42087 	      tree v = DECL_VALUE_EXPR (decl);
42088 	      if (TREE_CODE (v) == ARRAY_REF
42089 		  && VAR_P (TREE_OPERAND (v, 0))
42090 		  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42091 		d = TREE_OPERAND (v, 0);
42092 	    }
42093 	  do_range_for_auto_deduction (d, init);
42094 	}
42095       cond = global_namespace;
42096       incr = NULL_TREE;
42097       orig_init = init;
42098       if (this_pre_body)
42099 	this_pre_body = pop_stmt_list (this_pre_body);
42100       return;
42101     }
42102 
42103   init = mark_lvalue_use (init);
42104 
42105   if (decl == error_mark_node || init == error_mark_node)
42106     /* If an error happened previously do nothing or else a lot of
42107        unhelpful errors would be issued.  */
42108     begin_expr = end_expr = iter_type = error_mark_node;
42109   else
42110     {
42111       tree range_temp;
42112 
42113       if (VAR_P (init)
42114 	  && array_of_runtime_bound_p (TREE_TYPE (init)))
42115 	/* Can't bind a reference to an array of runtime bound.  */
42116 	range_temp = init;
42117       else
42118 	{
42119 	  range_temp = build_range_temp (init);
42120 	  DECL_NAME (range_temp) = NULL_TREE;
42121 	  pushdecl (range_temp);
42122 	  cp_finish_decl (range_temp, init,
42123 			  /*is_constant_init*/false, NULL_TREE,
42124 			  LOOKUP_ONLYCONVERTING);
42125 	  range_temp_decl = range_temp;
42126 	  range_temp = convert_from_reference (range_temp);
42127 	}
42128       iter_type = cp_parser_perform_range_for_lookup (range_temp,
42129 						      &begin_expr, &end_expr);
42130     }
42131 
42132   tree end_iter_type = iter_type;
42133   if (cxx_dialect >= cxx17)
42134     end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
42135   end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
42136   TREE_USED (end) = 1;
42137   DECL_ARTIFICIAL (end) = 1;
42138   pushdecl (end);
42139   cp_finish_decl (end, end_expr,
42140 		  /*is_constant_init*/false, NULL_TREE,
42141 		  LOOKUP_ONLYCONVERTING);
42142 
42143   /* The new for initialization statement.  */
42144   begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
42145   TREE_USED (begin) = 1;
42146   DECL_ARTIFICIAL (begin) = 1;
42147   pushdecl (begin);
42148   orig_init = init;
42149   if (CLASS_TYPE_P (iter_type))
42150     init = NULL_TREE;
42151   else
42152     {
42153       init = begin_expr;
42154       begin_expr = NULL_TREE;
42155     }
42156   cp_finish_decl (begin, begin_expr,
42157 		  /*is_constant_init*/false, NULL_TREE,
42158 		  LOOKUP_ONLYCONVERTING);
42159 
42160   /* The new for condition.  */
42161   if (CLASS_TYPE_P (iter_type))
42162     cond = build2 (NE_EXPR, boolean_type_node, begin, end);
42163   else
42164     cond = build_x_binary_op (input_location, NE_EXPR,
42165 			      begin, ERROR_MARK,
42166 			      end, ERROR_MARK,
42167 			      NULL_TREE, NULL, tf_warning_or_error);
42168 
42169   /* The new increment expression.  */
42170   if (CLASS_TYPE_P (iter_type))
42171     incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
42172   else
42173     incr = finish_unary_op_expr (input_location,
42174 				 PREINCREMENT_EXPR, begin,
42175 				 tf_warning_or_error);
42176 
42177   orig_decl = decl;
42178   decl = begin;
42179   if (for_block)
42180     {
42181       vec_safe_push (for_block, this_pre_body);
42182       this_pre_body = NULL_TREE;
42183     }
42184 
42185   tree decomp_first_name = NULL_TREE;
42186   unsigned decomp_cnt = 0;
42187   if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
42188     {
42189       tree v = DECL_VALUE_EXPR (orig_decl);
42190       if (TREE_CODE (v) == ARRAY_REF
42191 	  && VAR_P (TREE_OPERAND (v, 0))
42192 	  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42193 	{
42194 	  tree d = orig_decl;
42195 	  orig_decl = TREE_OPERAND (v, 0);
42196 	  decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
42197 	  decomp_first_name = d;
42198 	}
42199     }
42200 
42201   tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
42202   if (auto_node)
42203     {
42204       tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42205 				     NULL_TREE, tf_none);
42206       if (!error_operand_p (t))
42207 	TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
42208 						   t, auto_node);
42209     }
42210 
42211   tree v = make_tree_vec (decomp_cnt + 3);
42212   TREE_VEC_ELT (v, 0) = range_temp_decl;
42213   TREE_VEC_ELT (v, 1) = end;
42214   TREE_VEC_ELT (v, 2) = orig_decl;
42215   for (unsigned i = 0; i < decomp_cnt; i++)
42216     {
42217       TREE_VEC_ELT (v, i + 3) = decomp_first_name;
42218       decomp_first_name = DECL_CHAIN (decomp_first_name);
42219     }
42220   orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
42221 }
42222 
42223 /* Helper for cp_parser_omp_for_loop, finalize part of range for
42224    inside of the collapsed body.  */
42225 
42226 void
cp_finish_omp_range_for(tree orig,tree begin)42227 cp_finish_omp_range_for (tree orig, tree begin)
42228 {
42229   gcc_assert (TREE_CODE (orig) == TREE_LIST
42230 	      && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
42231   tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
42232   tree decomp_first_name = NULL_TREE;
42233   unsigned int decomp_cnt = 0;
42234 
42235   if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42236     {
42237       decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
42238       decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
42239       cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
42240     }
42241 
42242   /* The declaration is initialized with *__begin inside the loop body.  */
42243   cp_finish_decl (decl,
42244 		  build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42245 					NULL_TREE, tf_warning_or_error),
42246 		  /*is_constant_init*/false, NULL_TREE,
42247 		  LOOKUP_ONLYCONVERTING);
42248   if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42249     cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
42250 }
42251 
42252 /* Return true if next tokens contain a standard attribute that contains
42253    omp::directive (DIRECTIVE).  */
42254 
42255 static bool
cp_parser_omp_section_scan(cp_parser * parser,const char * directive,bool tentative)42256 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
42257 			    bool tentative)
42258 {
42259   size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
42260   if (n < 10)
42261     return false;
42262   for (i = 5; i < n - 4; i++)
42263     if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
42264 	&& cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
42265 	&& cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
42266       {
42267 	tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
42268 	tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
42269 	if (strcmp (IDENTIFIER_POINTER (first), "directive"))
42270 	  continue;
42271 	if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
42272 	  break;
42273       }
42274   if (i == n - 4)
42275     return false;
42276   cp_parser_parse_tentatively (parser);
42277   location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
42278   location_t last_loc
42279     = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
42280   location_t middle_loc = UNKNOWN_LOCATION;
42281   tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
42282   int cnt = 0;
42283   bool seen = false;
42284   for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
42285     if (get_attribute_namespace (attr) == omp_identifier
42286 	&& is_attribute_p ("directive", get_attribute_name (attr)))
42287       {
42288 	for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
42289 	  {
42290 	    tree d = TREE_VALUE (a);
42291 	    gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
42292 	    cp_token *first = DEFPARSE_TOKENS (d)->first;
42293 	    cnt++;
42294 	    if (first->type == CPP_NAME
42295 		&& strcmp (IDENTIFIER_POINTER (first->u.value),
42296 			   directive) == 0)
42297 	      {
42298 		seen = true;
42299 		if (middle_loc == UNKNOWN_LOCATION)
42300 		  middle_loc = first->location;
42301 	      }
42302 	  }
42303       }
42304   if (!seen || tentative)
42305     {
42306       cp_parser_abort_tentative_parse (parser);
42307       return seen;
42308     }
42309   if (cnt != 1 || TREE_CHAIN (std_attrs))
42310     {
42311       error_at (make_location (first_loc, last_loc, middle_loc),
42312 		"%<[[omp::directive(%s)]]%> must be the only specified "
42313 		"attribute on a statement", directive);
42314       cp_parser_abort_tentative_parse (parser);
42315       return false;
42316     }
42317   if (!cp_parser_parse_definitely (parser))
42318     return false;
42319   cp_parser_handle_statement_omp_attributes (parser, std_attrs);
42320   return true;
42321 }
42322 
42323 /* Parse an OpenMP structured block sequence.  KIND is the corresponding
42324    separating directive.  */
42325 
42326 static tree
cp_parser_omp_structured_block_sequence(cp_parser * parser,enum pragma_kind kind)42327 cp_parser_omp_structured_block_sequence (cp_parser *parser,
42328 					 enum pragma_kind kind)
42329 {
42330   tree stmt = begin_omp_structured_block ();
42331   unsigned int save = cp_parser_begin_omp_structured_block (parser);
42332 
42333   cp_parser_statement (parser, NULL_TREE, false, NULL);
42334   while (true)
42335     {
42336       cp_token *token = cp_lexer_peek_token (parser->lexer);
42337 
42338       if (token->type == CPP_CLOSE_BRACE
42339 	  || token->type == CPP_EOF
42340 	  || token->type == CPP_PRAGMA_EOL
42341 	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
42342 	  || (kind != PRAGMA_NONE
42343 	      && cp_parser_pragma_kind (token) == kind))
42344 	break;
42345 
42346       if (kind != PRAGMA_NONE
42347 	  && cp_parser_omp_section_scan (parser,
42348 					 kind == PRAGMA_OMP_SCAN
42349 					 ? "scan" : "section", false))
42350 	break;
42351 
42352       cp_parser_statement (parser, NULL_TREE, false, NULL);
42353     }
42354 
42355   cp_parser_end_omp_structured_block (parser, save);
42356   return finish_omp_structured_block (stmt);
42357 }
42358 
42359 
42360 /* OpenMP 5.0:
42361 
42362    scan-loop-body:
42363      { structured-block scan-directive structured-block }  */
42364 
42365 static void
cp_parser_omp_scan_loop_body(cp_parser * parser)42366 cp_parser_omp_scan_loop_body (cp_parser *parser)
42367 {
42368   tree substmt, clauses = NULL_TREE;
42369 
42370   matching_braces braces;
42371   if (!braces.require_open (parser))
42372     return;
42373 
42374   substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
42375   substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
42376   add_stmt (substmt);
42377 
42378   cp_token *tok = cp_lexer_peek_token (parser->lexer);
42379   if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
42380     {
42381       enum omp_clause_code clause = OMP_CLAUSE_ERROR;
42382 
42383       cp_lexer_consume_token (parser->lexer);
42384 
42385       if (parser->lexer->in_omp_attribute_pragma
42386 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42387 	cp_lexer_consume_token (parser->lexer);
42388 
42389       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42390 	{
42391 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42392 	  const char *p = IDENTIFIER_POINTER (id);
42393 	  if (strcmp (p, "inclusive") == 0)
42394 	    clause = OMP_CLAUSE_INCLUSIVE;
42395 	  else if (strcmp (p, "exclusive") == 0)
42396 	    clause = OMP_CLAUSE_EXCLUSIVE;
42397 	}
42398       if (clause != OMP_CLAUSE_ERROR)
42399 	{
42400 	  cp_lexer_consume_token (parser->lexer);
42401 	  clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
42402 	}
42403       else
42404 	cp_parser_error (parser, "expected %<inclusive%> or "
42405 				 "%<exclusive%> clause");
42406 
42407       cp_parser_require_pragma_eol (parser, tok);
42408     }
42409   else
42410     error ("expected %<#pragma omp scan%>");
42411 
42412   clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42413   substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
42414   substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
42415 			clauses);
42416   add_stmt (substmt);
42417 
42418   braces.require_close (parser);
42419 }
42420 
42421 /* Parse the restricted form of the for statement allowed by OpenMP.  */
42422 
42423 static tree
cp_parser_omp_for_loop(cp_parser * parser,enum tree_code code,tree clauses,tree * cclauses,bool * if_p)42424 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
42425 			tree *cclauses, bool *if_p)
42426 {
42427   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
42428   tree orig_decl;
42429   tree real_decl, initv, condv, incrv, declv, orig_declv;
42430   tree this_pre_body, cl, ordered_cl = NULL_TREE;
42431   location_t loc_first;
42432   bool collapse_err = false;
42433   int i, collapse = 1, ordered = 0, count, nbraces = 0;
42434   releasing_vec for_block;
42435   auto_vec<tree, 4> orig_inits;
42436   bool tiling = false;
42437   bool inscan = false;
42438 
42439   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
42440     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
42441       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
42442     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
42443       {
42444 	tiling = true;
42445 	collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
42446       }
42447     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
42448 	     && OMP_CLAUSE_ORDERED_EXPR (cl))
42449       {
42450 	ordered_cl = cl;
42451 	ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
42452       }
42453     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
42454 	     && OMP_CLAUSE_REDUCTION_INSCAN (cl)
42455 	     && (code == OMP_SIMD || code == OMP_FOR))
42456       inscan = true;
42457 
42458   if (ordered && ordered < collapse)
42459     {
42460       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42461 		"%<ordered%> clause parameter is less than %<collapse%>");
42462       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
42463 	= build_int_cst (NULL_TREE, collapse);
42464       ordered = collapse;
42465     }
42466   if (ordered)
42467     {
42468       for (tree *pc = &clauses; *pc; )
42469 	if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
42470 	  {
42471 	    error_at (OMP_CLAUSE_LOCATION (*pc),
42472 		      "%<linear%> clause may not be specified together "
42473 		      "with %<ordered%> clause with a parameter");
42474 	    *pc = OMP_CLAUSE_CHAIN (*pc);
42475 	  }
42476 	else
42477 	  pc = &OMP_CLAUSE_CHAIN (*pc);
42478     }
42479 
42480   gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
42481   count = ordered ? ordered : collapse;
42482 
42483   declv = make_tree_vec (count);
42484   initv = make_tree_vec (count);
42485   condv = make_tree_vec (count);
42486   incrv = make_tree_vec (count);
42487   orig_declv = NULL_TREE;
42488 
42489   loc_first = cp_lexer_peek_token (parser->lexer)->location;
42490 
42491   for (i = 0; i < count; i++)
42492     {
42493       int bracecount = 0;
42494       tree add_private_clause = NULL_TREE;
42495       location_t loc;
42496 
42497       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42498 	{
42499 	  if (!collapse_err)
42500 	    cp_parser_error (parser, "for statement expected");
42501 	  return NULL;
42502 	}
42503       loc = cp_lexer_consume_token (parser->lexer)->location;
42504 
42505       /* Don't create location wrapper nodes within an OpenMP "for"
42506 	 statement.  */
42507       auto_suppress_location_wrappers sentinel;
42508 
42509       matching_parens parens;
42510       if (!parens.require_open (parser))
42511 	return NULL;
42512 
42513       init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
42514       this_pre_body = push_stmt_list ();
42515 
42516       if (code != OACC_LOOP && cxx_dialect >= cxx11)
42517 	{
42518 	  /* Save tokens so that we can put them back.  */
42519 	  cp_lexer_save_tokens (parser->lexer);
42520 
42521 	  /* Look for ':' that is not nested in () or {}.  */
42522 	  bool is_range_for
42523 	    = (cp_parser_skip_to_closing_parenthesis_1 (parser,
42524 							/*recovering=*/false,
42525 							CPP_COLON,
42526 							/*consume_paren=*/
42527 							false) == -1);
42528 
42529 	  /* Roll back the tokens we skipped.  */
42530 	  cp_lexer_rollback_tokens (parser->lexer);
42531 
42532 	  if (is_range_for)
42533 	    {
42534 	      bool saved_colon_corrects_to_scope_p
42535 		= parser->colon_corrects_to_scope_p;
42536 
42537 	      /* A colon is used in range-based for.  */
42538 	      parser->colon_corrects_to_scope_p = false;
42539 
42540 	      /* Parse the declaration.  */
42541 	      cp_parser_simple_declaration (parser,
42542 					    /*function_definition_allowed_p=*/
42543 					    false, &decl);
42544 	      parser->colon_corrects_to_scope_p
42545 		= saved_colon_corrects_to_scope_p;
42546 
42547 	      cp_parser_require (parser, CPP_COLON, RT_COLON);
42548 
42549 	      init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
42550 					  false, 0, true);
42551 
42552 	      cp_convert_omp_range_for (this_pre_body, for_block, decl,
42553 					orig_decl, init, orig_init,
42554 					cond, incr);
42555 	      if (this_pre_body)
42556 		{
42557 		  if (pre_body)
42558 		    {
42559 		      tree t = pre_body;
42560 		      pre_body = push_stmt_list ();
42561 		      add_stmt (t);
42562 		      add_stmt (this_pre_body);
42563 		      pre_body = pop_stmt_list (pre_body);
42564 		    }
42565 		  else
42566 		    pre_body = this_pre_body;
42567 		}
42568 
42569 	      if (ordered_cl)
42570 		error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42571 			  "%<ordered%> clause with parameter on "
42572 			  "range-based %<for%> loop");
42573 
42574 	      goto parse_close_paren;
42575 	    }
42576 	}
42577 
42578       add_private_clause
42579 	= cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
42580 				       init, orig_init, decl, real_decl);
42581 
42582       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42583       if (this_pre_body)
42584 	{
42585 	  this_pre_body = pop_stmt_list (this_pre_body);
42586 	  if (pre_body)
42587 	    {
42588 	      tree t = pre_body;
42589 	      pre_body = push_stmt_list ();
42590 	      add_stmt (t);
42591 	      add_stmt (this_pre_body);
42592 	      pre_body = pop_stmt_list (pre_body);
42593 	    }
42594 	  else
42595 	    pre_body = this_pre_body;
42596 	}
42597 
42598       if (decl)
42599 	real_decl = decl;
42600       if (cclauses != NULL
42601 	  && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
42602 	  && real_decl != NULL_TREE
42603 	  && code != OMP_LOOP)
42604 	{
42605 	  tree *c;
42606 	  for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
42607 	    if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
42608 		&& OMP_CLAUSE_DECL (*c) == real_decl)
42609 	      {
42610 		error_at (loc, "iteration variable %qD"
42611 			  " should not be firstprivate", real_decl);
42612 		*c = OMP_CLAUSE_CHAIN (*c);
42613 	      }
42614 	    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
42615 		     && OMP_CLAUSE_DECL (*c) == real_decl)
42616 	      {
42617 		/* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
42618 		tree l = *c;
42619 		*c = OMP_CLAUSE_CHAIN (*c);
42620 		if (code == OMP_SIMD)
42621 		  {
42622 		    OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
42623 		    cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
42624 		  }
42625 		else
42626 		  {
42627 		    OMP_CLAUSE_CHAIN (l) = clauses;
42628 		    clauses = l;
42629 		  }
42630 		add_private_clause = NULL_TREE;
42631 	      }
42632 	    else
42633 	      {
42634 		if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
42635 		    && OMP_CLAUSE_DECL (*c) == real_decl)
42636 		  add_private_clause = NULL_TREE;
42637 		c = &OMP_CLAUSE_CHAIN (*c);
42638 	      }
42639 	}
42640 
42641       if (add_private_clause)
42642 	{
42643 	  tree c;
42644 	  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
42645 	    {
42646 	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
42647 		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
42648 		  && OMP_CLAUSE_DECL (c) == decl)
42649 		break;
42650 	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
42651 		       && OMP_CLAUSE_DECL (c) == decl)
42652 		error_at (loc, "iteration variable %qD "
42653 			  "should not be firstprivate",
42654 			  decl);
42655 	      else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
42656 			|| OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
42657 		       && OMP_CLAUSE_DECL (c) == decl)
42658 		error_at (loc, "iteration variable %qD should not be reduction",
42659 			  decl);
42660 	    }
42661 	  if (c == NULL)
42662 	    {
42663 	      if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
42664 		c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
42665 	      else if (code != OMP_SIMD)
42666 		c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
42667 	      else
42668 		c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
42669 	      OMP_CLAUSE_DECL (c) = add_private_clause;
42670 	      c = finish_omp_clauses (c, C_ORT_OMP);
42671 	      if (c)
42672 		{
42673 		  OMP_CLAUSE_CHAIN (c) = clauses;
42674 		  clauses = c;
42675 		  /* For linear, signal that we need to fill up
42676 		     the so far unknown linear step.  */
42677 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
42678 		    OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
42679 		}
42680 	    }
42681 	}
42682 
42683       cond = NULL;
42684       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42685 	cond = cp_parser_omp_for_cond (parser, decl, code);
42686       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42687 
42688       incr = NULL;
42689       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
42690 	{
42691 	  /* If decl is an iterator, preserve the operator on decl
42692 	     until finish_omp_for.  */
42693 	  if (real_decl
42694 	      && ((processing_template_decl
42695 		   && (TREE_TYPE (real_decl) == NULL_TREE
42696 		       || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
42697 		  || CLASS_TYPE_P (TREE_TYPE (real_decl))))
42698 	    incr = cp_parser_omp_for_incr (parser, real_decl);
42699 	  else
42700 	    incr = cp_parser_expression (parser);
42701 	  protected_set_expr_location_if_unset (incr, input_location);
42702 	}
42703 
42704     parse_close_paren:
42705       if (!parens.require_close (parser))
42706 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42707 					       /*or_comma=*/false,
42708 					       /*consume_paren=*/true);
42709 
42710       TREE_VEC_ELT (declv, i) = decl;
42711       TREE_VEC_ELT (initv, i) = init;
42712       TREE_VEC_ELT (condv, i) = cond;
42713       TREE_VEC_ELT (incrv, i) = incr;
42714       if (orig_init)
42715 	{
42716 	  orig_inits.safe_grow_cleared (i + 1, true);
42717 	  orig_inits[i] = orig_init;
42718 	}
42719       if (orig_decl)
42720 	{
42721 	  if (!orig_declv)
42722 	    orig_declv = copy_node (declv);
42723 	  TREE_VEC_ELT (orig_declv, i) = orig_decl;
42724 	}
42725       else if (orig_declv)
42726 	TREE_VEC_ELT (orig_declv, i) = decl;
42727 
42728       if (i == count - 1)
42729 	break;
42730 
42731       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
42732 	 in between the collapsed for loops to be still considered perfectly
42733 	 nested.  Hopefully the final version clarifies this.
42734 	 For now handle (multiple) {'s and empty statements.  */
42735       cp_parser_parse_tentatively (parser);
42736       for (;;)
42737 	{
42738 	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42739 	    break;
42740 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42741 	    {
42742 	      cp_lexer_consume_token (parser->lexer);
42743 	      bracecount++;
42744 	    }
42745 	  else if (bracecount
42746 		   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42747 	    cp_lexer_consume_token (parser->lexer);
42748 	  else
42749 	    {
42750 	      loc = cp_lexer_peek_token (parser->lexer)->location;
42751 	      error_at (loc, "not enough for loops to collapse");
42752 	      collapse_err = true;
42753 	      cp_parser_abort_tentative_parse (parser);
42754 	      declv = NULL_TREE;
42755 	      break;
42756 	    }
42757 	}
42758 
42759       if (declv)
42760 	{
42761 	  cp_parser_parse_definitely (parser);
42762 	  nbraces += bracecount;
42763 	}
42764     }
42765 
42766   if (nbraces)
42767     if_p = NULL;
42768 
42769   /* Note that we saved the original contents of this flag when we entered
42770      the structured block, and so we don't need to re-save it here.  */
42771   parser->in_statement = IN_OMP_FOR;
42772 
42773   /* Note that the grammar doesn't call for a structured block here,
42774      though the loop as a whole is a structured block.  */
42775   if (orig_declv)
42776     {
42777       body = begin_omp_structured_block ();
42778       for (i = 0; i < count; i++)
42779 	if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
42780 	  cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
42781 				   TREE_VEC_ELT (declv, i));
42782     }
42783   else
42784     body = push_stmt_list ();
42785   if (inscan)
42786     cp_parser_omp_scan_loop_body (parser);
42787   else
42788     cp_parser_statement (parser, NULL_TREE, false, if_p);
42789   if (orig_declv)
42790     body = finish_omp_structured_block (body);
42791   else
42792     body = pop_stmt_list (body);
42793 
42794   if (declv == NULL_TREE)
42795     ret = NULL_TREE;
42796   else
42797     ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
42798 			  incrv, body, pre_body, &orig_inits, clauses);
42799 
42800   while (nbraces)
42801     {
42802       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42803 	{
42804 	  cp_lexer_consume_token (parser->lexer);
42805 	  nbraces--;
42806 	}
42807       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42808 	cp_lexer_consume_token (parser->lexer);
42809       else
42810 	{
42811 	  if (!collapse_err)
42812 	    {
42813 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
42814 			"collapsed loops not perfectly nested");
42815 	    }
42816 	  collapse_err = true;
42817 	  cp_parser_statement_seq_opt (parser, NULL);
42818 	  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
42819 	    break;
42820 	}
42821     }
42822 
42823   while (!for_block->is_empty ())
42824     {
42825       tree t = for_block->pop ();
42826       if (TREE_CODE (t) == STATEMENT_LIST)
42827 	add_stmt (pop_stmt_list (t));
42828       else
42829 	add_stmt (t);
42830     }
42831 
42832   return ret;
42833 }
42834 
42835 /* Helper function for OpenMP parsing, split clauses and call
42836    finish_omp_clauses on each of the set of clauses afterwards.  */
42837 
42838 static void
cp_omp_split_clauses(location_t loc,enum tree_code code,omp_clause_mask mask,tree clauses,tree * cclauses)42839 cp_omp_split_clauses (location_t loc, enum tree_code code,
42840 		      omp_clause_mask mask, tree clauses, tree *cclauses)
42841 {
42842   int i;
42843   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
42844   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
42845     if (cclauses[i])
42846       cclauses[i] = finish_omp_clauses (cclauses[i],
42847 					i == C_OMP_CLAUSE_SPLIT_TARGET
42848 					? C_ORT_OMP_TARGET : C_ORT_OMP);
42849 }
42850 
42851 /* OpenMP 5.0:
42852    #pragma omp loop loop-clause[optseq] new-line
42853      for-loop  */
42854 
42855 #define OMP_LOOP_CLAUSE_MASK					\
42856 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
42857 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
42858 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
42859 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
42860 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND)		\
42861 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42862 
42863 static tree
cp_parser_omp_loop(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)42864 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
42865 		    char *p_name, omp_clause_mask mask, tree *cclauses,
42866 		    bool *if_p)
42867 {
42868   tree clauses, sb, ret;
42869   unsigned int save;
42870   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42871 
42872   strcat (p_name, " loop");
42873   mask |= OMP_LOOP_CLAUSE_MASK;
42874 
42875   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42876 				       cclauses == NULL);
42877   if (cclauses)
42878     {
42879       cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
42880       clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
42881     }
42882 
42883   keep_next_level (true);
42884   sb = begin_omp_structured_block ();
42885   save = cp_parser_begin_omp_structured_block (parser);
42886 
42887   ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
42888 
42889   cp_parser_end_omp_structured_block (parser, save);
42890   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42891 
42892   return ret;
42893 }
42894 
42895 /* OpenMP 4.0:
42896    #pragma omp simd simd-clause[optseq] new-line
42897      for-loop  */
42898 
42899 #define OMP_SIMD_CLAUSE_MASK					\
42900 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)	\
42901 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
42902 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
42903 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
42904 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
42905 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
42906 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
42907 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
42908 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
42909 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL)	\
42910 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42911 
42912 static tree
cp_parser_omp_simd(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)42913 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
42914 		    char *p_name, omp_clause_mask mask, tree *cclauses,
42915 		    bool *if_p)
42916 {
42917   tree clauses, sb, ret;
42918   unsigned int save;
42919   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42920 
42921   strcat (p_name, " simd");
42922   mask |= OMP_SIMD_CLAUSE_MASK;
42923 
42924   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
42925 				       cclauses == NULL);
42926   if (cclauses)
42927     {
42928       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
42929       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
42930       tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
42931 				OMP_CLAUSE_ORDERED);
42932       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
42933 	{
42934 	  error_at (OMP_CLAUSE_LOCATION (c),
42935 		    "%<ordered%> clause with parameter may not be specified "
42936 		    "on %qs construct", p_name);
42937 	  OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
42938 	}
42939     }
42940 
42941   keep_next_level (true);
42942   sb = begin_omp_structured_block ();
42943   save = cp_parser_begin_omp_structured_block (parser);
42944 
42945   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
42946 
42947   cp_parser_end_omp_structured_block (parser, save);
42948   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
42949 
42950   return ret;
42951 }
42952 
42953 /* OpenMP 2.5:
42954    #pragma omp for for-clause[optseq] new-line
42955      for-loop
42956 
42957    OpenMP 4.0:
42958    #pragma omp for simd for-simd-clause[optseq] new-line
42959      for-loop  */
42960 
42961 #define OMP_FOR_CLAUSE_MASK					\
42962 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
42963 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
42964 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
42965 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
42966 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
42967 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)	\
42968 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)	\
42969 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
42970 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
42971 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
42972 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
42973 
42974 static tree
cp_parser_omp_for(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)42975 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
42976 		   char *p_name, omp_clause_mask mask, tree *cclauses,
42977 		   bool *if_p)
42978 {
42979   tree clauses, sb, ret;
42980   unsigned int save;
42981   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
42982 
42983   strcat (p_name, " for");
42984   mask |= OMP_FOR_CLAUSE_MASK;
42985   /* parallel for{, simd} disallows nowait clause, but for
42986      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
42987   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
42988     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
42989   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
42990   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
42991     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
42992 
42993   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42994     {
42995       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42996       const char *p = IDENTIFIER_POINTER (id);
42997 
42998       if (strcmp (p, "simd") == 0)
42999 	{
43000 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43001 	  if (cclauses == NULL)
43002 	    cclauses = cclauses_buf;
43003 
43004 	  cp_lexer_consume_token (parser->lexer);
43005 	  if (!flag_openmp)  /* flag_openmp_simd  */
43006 	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43007 				       cclauses, if_p);
43008 	  sb = begin_omp_structured_block ();
43009 	  save = cp_parser_begin_omp_structured_block (parser);
43010 	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43011 				    cclauses, if_p);
43012 	  cp_parser_end_omp_structured_block (parser, save);
43013 	  tree body = finish_omp_structured_block (sb);
43014 	  if (ret == NULL)
43015 	    return ret;
43016 	  ret = make_node (OMP_FOR);
43017 	  TREE_TYPE (ret) = void_type_node;
43018 	  OMP_FOR_BODY (ret) = body;
43019 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43020 	  SET_EXPR_LOCATION (ret, loc);
43021 	  add_stmt (ret);
43022 	  return ret;
43023 	}
43024     }
43025   if (!flag_openmp)  /* flag_openmp_simd  */
43026     {
43027       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43028       return NULL_TREE;
43029     }
43030 
43031   /* Composite distribute parallel for disallows linear clause.  */
43032   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43033     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
43034 
43035   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43036 				       cclauses == NULL);
43037   if (cclauses)
43038     {
43039       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
43040       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43041     }
43042 
43043   keep_next_level (true);
43044   sb = begin_omp_structured_block ();
43045   save = cp_parser_begin_omp_structured_block (parser);
43046 
43047   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
43048 
43049   cp_parser_end_omp_structured_block (parser, save);
43050   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43051 
43052   return ret;
43053 }
43054 
43055 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
43056 				    omp_clause_mask, tree *, bool *);
43057 
43058 /* OpenMP 2.5:
43059    # pragma omp master new-line
43060      structured-block  */
43061 
43062 static tree
cp_parser_omp_master(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)43063 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
43064 		      char *p_name, omp_clause_mask mask, tree *cclauses,
43065 		      bool *if_p)
43066 {
43067   tree clauses, sb, ret;
43068   unsigned int save;
43069   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43070 
43071   strcat (p_name, " master");
43072 
43073   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43074     {
43075       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43076       const char *p = IDENTIFIER_POINTER (id);
43077 
43078       if (strcmp (p, "taskloop") == 0)
43079 	{
43080 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43081 	  if (cclauses == NULL)
43082 	    cclauses = cclauses_buf;
43083 
43084 	  cp_lexer_consume_token (parser->lexer);
43085 	  if (!flag_openmp)  /* flag_openmp_simd  */
43086 	    return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43087 					   cclauses, if_p);
43088 	  sb = begin_omp_structured_block ();
43089 	  save = cp_parser_begin_omp_structured_block (parser);
43090 	  ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43091 					cclauses, if_p);
43092 	  cp_parser_end_omp_structured_block (parser, save);
43093 	  tree body = finish_omp_structured_block (sb);
43094 	  if (ret == NULL)
43095 	    return ret;
43096 	  ret = c_finish_omp_master (loc, body);
43097 	  OMP_MASTER_COMBINED (ret) = 1;
43098 	  return ret;
43099 	}
43100     }
43101   if (!flag_openmp)  /* flag_openmp_simd  */
43102     {
43103       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43104       return NULL_TREE;
43105     }
43106 
43107   if (cclauses)
43108     {
43109       clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43110 					   false);
43111       cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43112     }
43113   else
43114     cp_parser_require_pragma_eol (parser, pragma_tok);
43115 
43116   return c_finish_omp_master (loc,
43117 			      cp_parser_omp_structured_block (parser, if_p));
43118 }
43119 
43120 /* OpenMP 5.1:
43121    # pragma omp masked masked-clauses new-line
43122      structured-block  */
43123 
43124 #define OMP_MASKED_CLAUSE_MASK					\
43125 	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
43126 
43127 static tree
cp_parser_omp_masked(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)43128 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
43129 		      char *p_name, omp_clause_mask mask, tree *cclauses,
43130 		      bool *if_p)
43131 {
43132   tree clauses, sb, ret;
43133   unsigned int save;
43134   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43135 
43136   strcat (p_name, " masked");
43137   mask |= OMP_MASKED_CLAUSE_MASK;
43138 
43139   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43140     {
43141       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43142       const char *p = IDENTIFIER_POINTER (id);
43143 
43144       if (strcmp (p, "taskloop") == 0)
43145 	{
43146 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43147 	  if (cclauses == NULL)
43148 	    cclauses = cclauses_buf;
43149 
43150 	  cp_lexer_consume_token (parser->lexer);
43151 	  if (!flag_openmp)  /* flag_openmp_simd  */
43152 	    return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43153 					   cclauses, if_p);
43154 	  sb = begin_omp_structured_block ();
43155 	  save = cp_parser_begin_omp_structured_block (parser);
43156 	  ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43157 					cclauses, if_p);
43158 	  cp_parser_end_omp_structured_block (parser, save);
43159 	  tree body = finish_omp_structured_block (sb);
43160 	  if (ret == NULL)
43161 	    return ret;
43162 	  ret = c_finish_omp_masked (loc, body,
43163 				     cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
43164 	  OMP_MASKED_COMBINED (ret) = 1;
43165 	  return ret;
43166 	}
43167     }
43168   if (!flag_openmp)  /* flag_openmp_simd  */
43169     {
43170       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43171       return NULL_TREE;
43172     }
43173 
43174   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43175 				       cclauses == NULL);
43176   if (cclauses)
43177     {
43178       cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43179       clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
43180     }
43181 
43182   return c_finish_omp_masked (loc,
43183 			      cp_parser_omp_structured_block (parser, if_p),
43184 			      clauses);
43185 }
43186 
43187 /* OpenMP 2.5:
43188    # pragma omp ordered new-line
43189      structured-block
43190 
43191    OpenMP 4.5:
43192    # pragma omp ordered ordered-clauses new-line
43193      structured-block  */
43194 
43195 #define OMP_ORDERED_CLAUSE_MASK					\
43196 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)	\
43197 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
43198 
43199 #define OMP_ORDERED_DEPEND_CLAUSE_MASK				\
43200 	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43201 
43202 static bool
cp_parser_omp_ordered(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context,bool * if_p)43203 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
43204 		       enum pragma_context context, bool *if_p)
43205 {
43206   location_t loc = pragma_tok->location;
43207   int n = 1;
43208 
43209   /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
43210   if (parser->lexer->in_omp_attribute_pragma
43211       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43212     n = 2;
43213 
43214   if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
43215     {
43216       tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
43217       const char *p = IDENTIFIER_POINTER (id);
43218 
43219       if (strcmp (p, "depend") == 0)
43220 	{
43221 	  if (!flag_openmp)	/* flag_openmp_simd */
43222 	    {
43223 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43224 	      return false;
43225 	    }
43226 	  if (context == pragma_stmt)
43227 	    {
43228 	      error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
43229 			"%<depend%> clause may only be used in compound "
43230 			"statements");
43231 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43232 	      return true;
43233 	    }
43234 	  tree clauses
43235 	    = cp_parser_omp_all_clauses (parser,
43236 					 OMP_ORDERED_DEPEND_CLAUSE_MASK,
43237 					 "#pragma omp ordered", pragma_tok);
43238 	  c_finish_omp_ordered (loc, clauses, NULL_TREE);
43239 	  return false;
43240 	}
43241     }
43242 
43243   tree clauses
43244     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
43245 				 "#pragma omp ordered", pragma_tok);
43246 
43247   if (!flag_openmp     /* flag_openmp_simd  */
43248       && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
43249     return false;
43250 
43251   c_finish_omp_ordered (loc, clauses,
43252 			cp_parser_omp_structured_block (parser, if_p));
43253   return true;
43254 }
43255 
43256 /* OpenMP 2.5:
43257 
43258    section-scope:
43259      { section-sequence }
43260 
43261    section-sequence:
43262      section-directive[opt] structured-block
43263      section-sequence section-directive structured-block  */
43264 
43265 static tree
cp_parser_omp_sections_scope(cp_parser * parser)43266 cp_parser_omp_sections_scope (cp_parser *parser)
43267 {
43268   tree stmt, substmt;
43269   bool error_suppress = false;
43270   cp_token *tok;
43271 
43272   matching_braces braces;
43273   if (!braces.require_open (parser))
43274     return NULL_TREE;
43275 
43276   stmt = push_stmt_list ();
43277 
43278   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
43279       != PRAGMA_OMP_SECTION
43280       && !cp_parser_omp_section_scan (parser, "section", true))
43281     {
43282       substmt = cp_parser_omp_structured_block_sequence (parser,
43283 							 PRAGMA_OMP_SECTION);
43284       substmt = build1 (OMP_SECTION, void_type_node, substmt);
43285       add_stmt (substmt);
43286     }
43287 
43288   while (1)
43289     {
43290       tok = cp_lexer_peek_token (parser->lexer);
43291       if (tok->type == CPP_CLOSE_BRACE)
43292 	break;
43293       if (tok->type == CPP_EOF)
43294 	break;
43295 
43296       if (cp_parser_omp_section_scan (parser, "section", false))
43297 	tok = cp_lexer_peek_token (parser->lexer);
43298       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
43299 	{
43300 	  cp_lexer_consume_token (parser->lexer);
43301 	  cp_parser_require_pragma_eol (parser, tok);
43302 	  error_suppress = false;
43303 	}
43304       else if (!error_suppress)
43305 	{
43306 	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
43307 	  error_suppress = true;
43308 	}
43309 
43310       substmt = cp_parser_omp_structured_block_sequence (parser,
43311 							 PRAGMA_OMP_SECTION);
43312       substmt = build1 (OMP_SECTION, void_type_node, substmt);
43313       add_stmt (substmt);
43314     }
43315   braces.require_close (parser);
43316 
43317   substmt = pop_stmt_list (stmt);
43318 
43319   stmt = make_node (OMP_SECTIONS);
43320   TREE_TYPE (stmt) = void_type_node;
43321   OMP_SECTIONS_BODY (stmt) = substmt;
43322 
43323   add_stmt (stmt);
43324   return stmt;
43325 }
43326 
43327 /* OpenMP 2.5:
43328    # pragma omp sections sections-clause[optseq] newline
43329      sections-scope  */
43330 
43331 #define OMP_SECTIONS_CLAUSE_MASK				\
43332 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43333 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43334 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
43335 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43336 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43337 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43338 
43339 static tree
cp_parser_omp_sections(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)43340 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
43341 			char *p_name, omp_clause_mask mask, tree *cclauses)
43342 {
43343   tree clauses, ret;
43344   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43345 
43346   strcat (p_name, " sections");
43347   mask |= OMP_SECTIONS_CLAUSE_MASK;
43348   if (cclauses)
43349     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43350 
43351   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43352 				       cclauses == NULL);
43353   if (cclauses)
43354     {
43355       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
43356       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
43357     }
43358 
43359   ret = cp_parser_omp_sections_scope (parser);
43360   if (ret)
43361     OMP_SECTIONS_CLAUSES (ret) = clauses;
43362 
43363   return ret;
43364 }
43365 
43366 /* OpenMP 2.5:
43367    # pragma omp parallel parallel-clause[optseq] new-line
43368      structured-block
43369    # pragma omp parallel for parallel-for-clause[optseq] new-line
43370      structured-block
43371    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43372      structured-block
43373 
43374    OpenMP 4.0:
43375    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43376      structured-block */
43377 
43378 #define OMP_PARALLEL_CLAUSE_MASK				\
43379 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
43380 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43381 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43382 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
43383 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
43384 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)	\
43385 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43386 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)	\
43387 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43388 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43389 
43390 static tree
cp_parser_omp_parallel(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)43391 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
43392 			char *p_name, omp_clause_mask mask, tree *cclauses,
43393 			bool *if_p)
43394 {
43395   tree stmt, clauses, block;
43396   unsigned int save;
43397   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43398 
43399   strcat (p_name, " parallel");
43400   mask |= OMP_PARALLEL_CLAUSE_MASK;
43401   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
43402   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
43403       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
43404     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
43405 
43406   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43407     {
43408       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43409       if (cclauses == NULL)
43410 	cclauses = cclauses_buf;
43411 
43412       cp_lexer_consume_token (parser->lexer);
43413       if (!flag_openmp)  /* flag_openmp_simd  */
43414 	return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43415 				  if_p);
43416       block = begin_omp_parallel ();
43417       save = cp_parser_begin_omp_structured_block (parser);
43418       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43419 				    if_p);
43420       cp_parser_end_omp_structured_block (parser, save);
43421       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43422 				  block);
43423       if (ret == NULL_TREE)
43424 	return ret;
43425       OMP_PARALLEL_COMBINED (stmt) = 1;
43426       return stmt;
43427     }
43428   /* When combined with distribute, parallel has to be followed by for.
43429      #pragma omp target parallel is allowed though.  */
43430   else if (cclauses
43431 	   && (mask & (OMP_CLAUSE_MASK_1
43432 		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43433     {
43434       error_at (loc, "expected %<for%> after %qs", p_name);
43435       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43436       return NULL_TREE;
43437     }
43438   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43439     {
43440       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43441       const char *p = IDENTIFIER_POINTER (id);
43442       if (cclauses == NULL && strcmp (p, "masked") == 0)
43443 	{
43444 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43445 	  cclauses = cclauses_buf;
43446 
43447 	  cp_lexer_consume_token (parser->lexer);
43448 	  if (!flag_openmp)  /* flag_openmp_simd  */
43449 	    return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43450 					 cclauses, if_p);
43451 	  block = begin_omp_parallel ();
43452 	  save = cp_parser_begin_omp_structured_block (parser);
43453 	  tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43454 					   cclauses, if_p);
43455 	  cp_parser_end_omp_structured_block (parser, save);
43456 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43457 				      block);
43458 	  if (ret == NULL_TREE)
43459 	    return ret;
43460 	  /* masked does have just filter clause, but during gimplification
43461 	     isn't represented by a gimplification omp context, so for
43462 	     #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43463 	     so that
43464 	     #pragma omp parallel masked
43465 	     #pragma omp taskloop simd lastprivate (x)
43466 	     isn't confused with
43467 	     #pragma omp parallel masked taskloop simd lastprivate (x)  */
43468 	  if (OMP_MASKED_COMBINED (ret))
43469 	    OMP_PARALLEL_COMBINED (stmt) = 1;
43470 	  return stmt;
43471 	}
43472       else if (cclauses == NULL && strcmp (p, "master") == 0)
43473 	{
43474 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43475 	  cclauses = cclauses_buf;
43476 
43477 	  cp_lexer_consume_token (parser->lexer);
43478 	  if (!flag_openmp)  /* flag_openmp_simd  */
43479 	    return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43480 					 cclauses, if_p);
43481 	  block = begin_omp_parallel ();
43482 	  save = cp_parser_begin_omp_structured_block (parser);
43483 	  tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43484 					   cclauses, if_p);
43485 	  cp_parser_end_omp_structured_block (parser, save);
43486 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43487 				      block);
43488 	  if (ret == NULL_TREE)
43489 	    return ret;
43490 	  /* master doesn't have any clauses and during gimplification
43491 	     isn't represented by a gimplification omp context, so for
43492 	     #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43493 	     so that
43494 	     #pragma omp parallel master
43495 	     #pragma omp taskloop simd lastprivate (x)
43496 	     isn't confused with
43497 	     #pragma omp parallel master taskloop simd lastprivate (x)  */
43498 	  if (OMP_MASTER_COMBINED (ret))
43499 	    OMP_PARALLEL_COMBINED (stmt) = 1;
43500 	  return stmt;
43501 	}
43502       else if (strcmp (p, "loop") == 0)
43503 	{
43504 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43505 	  if (cclauses == NULL)
43506 	    cclauses = cclauses_buf;
43507 
43508 	  cp_lexer_consume_token (parser->lexer);
43509 	  if (!flag_openmp)  /* flag_openmp_simd  */
43510 	    return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43511 				       cclauses, if_p);
43512 	  block = begin_omp_parallel ();
43513 	  save = cp_parser_begin_omp_structured_block (parser);
43514 	  tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43515 					 cclauses, if_p);
43516 	  cp_parser_end_omp_structured_block (parser, save);
43517 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43518 				      block);
43519 	  if (ret == NULL_TREE)
43520 	    return ret;
43521 	  OMP_PARALLEL_COMBINED (stmt) = 1;
43522 	  return stmt;
43523 	}
43524       else if (!flag_openmp)  /* flag_openmp_simd  */
43525 	{
43526 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43527 	  return NULL_TREE;
43528 	}
43529       else if (cclauses == NULL && strcmp (p, "sections") == 0)
43530 	{
43531 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43532 	  cclauses = cclauses_buf;
43533 
43534 	  cp_lexer_consume_token (parser->lexer);
43535 	  block = begin_omp_parallel ();
43536 	  save = cp_parser_begin_omp_structured_block (parser);
43537 	  cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
43538 	  cp_parser_end_omp_structured_block (parser, save);
43539 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43540 				      block);
43541 	  OMP_PARALLEL_COMBINED (stmt) = 1;
43542 	  return stmt;
43543 	}
43544     }
43545   else if (!flag_openmp)  /* flag_openmp_simd  */
43546     {
43547       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43548       return NULL_TREE;
43549     }
43550 
43551   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43552 				       cclauses == NULL);
43553   if (cclauses)
43554     {
43555       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
43556       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
43557     }
43558 
43559   block = begin_omp_parallel ();
43560   save = cp_parser_begin_omp_structured_block (parser);
43561   parser->omp_attrs_forbidden_p = true;
43562   cp_parser_statement (parser, NULL_TREE, false, if_p);
43563   cp_parser_end_omp_structured_block (parser, save);
43564   stmt = finish_omp_parallel (clauses, block);
43565   return stmt;
43566 }
43567 
43568 /* OpenMP 2.5:
43569    # pragma omp single single-clause[optseq] new-line
43570      structured-block  */
43571 
43572 #define OMP_SINGLE_CLAUSE_MASK					\
43573 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43574 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43575 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)	\
43576 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43577 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43578 
43579 static tree
cp_parser_omp_single(cp_parser * parser,cp_token * pragma_tok,bool * if_p)43580 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43581 {
43582   tree stmt = make_node (OMP_SINGLE);
43583   TREE_TYPE (stmt) = void_type_node;
43584   SET_EXPR_LOCATION (stmt, pragma_tok->location);
43585 
43586   OMP_SINGLE_CLAUSES (stmt)
43587     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
43588 				 "#pragma omp single", pragma_tok);
43589   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43590 
43591   return add_stmt (stmt);
43592 }
43593 
43594 /* OpenMP 5.1:
43595    # pragma omp scope scope-clause[optseq] new-line
43596      structured-block  */
43597 
43598 #define OMP_SCOPE_CLAUSE_MASK					\
43599 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43600 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43601 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43602 
43603 static tree
cp_parser_omp_scope(cp_parser * parser,cp_token * pragma_tok,bool * if_p)43604 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43605 {
43606   tree stmt = make_node (OMP_SCOPE);
43607   TREE_TYPE (stmt) = void_type_node;
43608   SET_EXPR_LOCATION (stmt, pragma_tok->location);
43609 
43610   OMP_SCOPE_CLAUSES (stmt)
43611     = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
43612 				 "#pragma omp scope", pragma_tok);
43613   OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43614 
43615   return add_stmt (stmt);
43616 }
43617 
43618 /* OpenMP 3.0:
43619    # pragma omp task task-clause[optseq] new-line
43620      structured-block  */
43621 
43622 #define OMP_TASK_CLAUSE_MASK					\
43623 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
43624 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
43625 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
43626 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43627 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43628 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
43629 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
43630 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
43631 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
43632 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
43633 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43634 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION)	\
43635 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH)	\
43636 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
43637 
43638 static tree
cp_parser_omp_task(cp_parser * parser,cp_token * pragma_tok,bool * if_p)43639 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43640 {
43641   tree clauses, block;
43642   unsigned int save;
43643 
43644   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
43645 				       "#pragma omp task", pragma_tok);
43646   block = begin_omp_task ();
43647   save = cp_parser_begin_omp_structured_block (parser);
43648   parser->omp_attrs_forbidden_p = true;
43649   cp_parser_statement (parser, NULL_TREE, false, if_p);
43650   cp_parser_end_omp_structured_block (parser, save);
43651   return finish_omp_task (clauses, block);
43652 }
43653 
43654 /* OpenMP 3.0:
43655    # pragma omp taskwait new-line
43656 
43657    OpenMP 5.0:
43658    # pragma omp taskwait taskwait-clause[opt] new-line  */
43659 
43660 #define OMP_TASKWAIT_CLAUSE_MASK				\
43661 	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
43662 
43663 static void
cp_parser_omp_taskwait(cp_parser * parser,cp_token * pragma_tok)43664 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
43665 {
43666   tree clauses
43667     = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
43668 				 "#pragma omp taskwait", pragma_tok);
43669 
43670   if (clauses)
43671     {
43672       tree stmt = make_node (OMP_TASK);
43673       TREE_TYPE (stmt) = void_node;
43674       OMP_TASK_CLAUSES (stmt) = clauses;
43675       OMP_TASK_BODY (stmt) = NULL_TREE;
43676       SET_EXPR_LOCATION (stmt, pragma_tok->location);
43677       add_stmt (stmt);
43678     }
43679   else
43680     finish_omp_taskwait ();
43681 }
43682 
43683 /* OpenMP 3.1:
43684    # pragma omp taskyield new-line  */
43685 
43686 static void
cp_parser_omp_taskyield(cp_parser * parser,cp_token * pragma_tok)43687 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
43688 {
43689   cp_parser_require_pragma_eol (parser, pragma_tok);
43690   finish_omp_taskyield ();
43691 }
43692 
43693 /* OpenMP 4.0:
43694    # pragma omp taskgroup new-line
43695      structured-block
43696 
43697    OpenMP 5.0:
43698    # pragma omp taskgroup taskgroup-clause[optseq] new-line  */
43699 
43700 #define OMP_TASKGROUP_CLAUSE_MASK				\
43701 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43702 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
43703 
43704 static tree
cp_parser_omp_taskgroup(cp_parser * parser,cp_token * pragma_tok,bool * if_p)43705 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43706 {
43707   tree clauses
43708     = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
43709 				 "#pragma omp taskgroup", pragma_tok);
43710   return c_finish_omp_taskgroup (input_location,
43711 				 cp_parser_omp_structured_block (parser,
43712 								 if_p),
43713 				 clauses);
43714 }
43715 
43716 
43717 /* OpenMP 2.5:
43718    # pragma omp threadprivate (variable-list) */
43719 
43720 static void
cp_parser_omp_threadprivate(cp_parser * parser,cp_token * pragma_tok)43721 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
43722 {
43723   tree vars;
43724 
43725   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43726   cp_parser_require_pragma_eol (parser, pragma_tok);
43727 
43728   finish_omp_threadprivate (vars);
43729 }
43730 
43731 /* OpenMP 4.0:
43732    # pragma omp cancel cancel-clause[optseq] new-line  */
43733 
43734 #define OMP_CANCEL_CLAUSE_MASK					\
43735 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
43736 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
43737 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
43738 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)	\
43739 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
43740 
43741 static void
cp_parser_omp_cancel(cp_parser * parser,cp_token * pragma_tok)43742 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
43743 {
43744   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
43745 					    "#pragma omp cancel", pragma_tok);
43746   finish_omp_cancel (clauses);
43747 }
43748 
43749 /* OpenMP 4.0:
43750    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
43751 
43752 #define OMP_CANCELLATION_POINT_CLAUSE_MASK			\
43753 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
43754 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
43755 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
43756 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
43757 
43758 static bool
cp_parser_omp_cancellation_point(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)43759 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
43760 				  enum pragma_context context)
43761 {
43762   tree clauses;
43763   bool point_seen = false;
43764 
43765   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43766     {
43767       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43768       const char *p = IDENTIFIER_POINTER (id);
43769 
43770       if (strcmp (p, "point") == 0)
43771 	{
43772 	  cp_lexer_consume_token (parser->lexer);
43773 	  point_seen = true;
43774 	}
43775     }
43776   if (!point_seen)
43777     {
43778       cp_parser_error (parser, "expected %<point%>");
43779       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43780       return false;
43781     }
43782 
43783   if (context != pragma_compound)
43784     {
43785       if (context == pragma_stmt)
43786 	error_at (pragma_tok->location,
43787 		  "%<#pragma %s%> may only be used in compound statements",
43788 		  "omp cancellation point");
43789       else
43790 	cp_parser_error (parser, "expected declaration specifiers");
43791       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43792       return true;
43793     }
43794 
43795   clauses = cp_parser_omp_all_clauses (parser,
43796 				       OMP_CANCELLATION_POINT_CLAUSE_MASK,
43797 				       "#pragma omp cancellation point",
43798 				       pragma_tok);
43799   finish_omp_cancellation_point (clauses);
43800   return true;
43801 }
43802 
43803 /* OpenMP 4.0:
43804    #pragma omp distribute distribute-clause[optseq] new-line
43805      for-loop  */
43806 
43807 #define OMP_DISTRIBUTE_CLAUSE_MASK				\
43808 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43809 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43810 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
43811 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
43812 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43813 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
43814 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43815 
43816 static tree
cp_parser_omp_distribute(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)43817 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
43818 			  char *p_name, omp_clause_mask mask, tree *cclauses,
43819 			  bool *if_p)
43820 {
43821   tree clauses, sb, ret;
43822   unsigned int save;
43823   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43824 
43825   strcat (p_name, " distribute");
43826   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
43827 
43828   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43829     {
43830       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43831       const char *p = IDENTIFIER_POINTER (id);
43832       bool simd = false;
43833       bool parallel = false;
43834 
43835       if (strcmp (p, "simd") == 0)
43836 	simd = true;
43837       else
43838 	parallel = strcmp (p, "parallel") == 0;
43839       if (parallel || simd)
43840 	{
43841 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43842 	  if (cclauses == NULL)
43843 	    cclauses = cclauses_buf;
43844 	  cp_lexer_consume_token (parser->lexer);
43845 	  if (!flag_openmp)  /* flag_openmp_simd  */
43846 	    {
43847 	      if (simd)
43848 		return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43849 					   cclauses, if_p);
43850 	      else
43851 		return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43852 					       cclauses, if_p);
43853 	    }
43854 	  sb = begin_omp_structured_block ();
43855 	  save = cp_parser_begin_omp_structured_block (parser);
43856 	  if (simd)
43857 	    ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43858 				      cclauses, if_p);
43859 	  else
43860 	    ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
43861 					  cclauses, if_p);
43862 	  cp_parser_end_omp_structured_block (parser, save);
43863 	  tree body = finish_omp_structured_block (sb);
43864 	  if (ret == NULL)
43865 	    return ret;
43866 	  ret = make_node (OMP_DISTRIBUTE);
43867 	  TREE_TYPE (ret) = void_type_node;
43868 	  OMP_FOR_BODY (ret) = body;
43869 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43870 	  SET_EXPR_LOCATION (ret, loc);
43871 	  add_stmt (ret);
43872 	  return ret;
43873 	}
43874     }
43875   if (!flag_openmp)  /* flag_openmp_simd  */
43876     {
43877       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43878       return NULL_TREE;
43879     }
43880 
43881   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43882 				       cclauses == NULL);
43883   if (cclauses)
43884     {
43885       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
43886       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
43887     }
43888 
43889   keep_next_level (true);
43890   sb = begin_omp_structured_block ();
43891   save = cp_parser_begin_omp_structured_block (parser);
43892 
43893   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
43894 
43895   cp_parser_end_omp_structured_block (parser, save);
43896   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43897 
43898   return ret;
43899 }
43900 
43901 /* OpenMP 4.0:
43902    # pragma omp teams teams-clause[optseq] new-line
43903      structured-block  */
43904 
43905 #define OMP_TEAMS_CLAUSE_MASK					\
43906 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
43907 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
43908 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
43909 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
43910 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)	\
43911 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
43912 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
43913 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
43914 
43915 static tree
cp_parser_omp_teams(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)43916 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
43917 		     char *p_name, omp_clause_mask mask, tree *cclauses,
43918 		     bool *if_p)
43919 {
43920   tree clauses, sb, ret;
43921   unsigned int save;
43922   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43923 
43924   strcat (p_name, " teams");
43925   mask |= OMP_TEAMS_CLAUSE_MASK;
43926 
43927   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43928     {
43929       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43930       const char *p = IDENTIFIER_POINTER (id);
43931       if (strcmp (p, "distribute") == 0)
43932 	{
43933 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43934 	  if (cclauses == NULL)
43935 	    cclauses = cclauses_buf;
43936 
43937 	  cp_lexer_consume_token (parser->lexer);
43938 	  if (!flag_openmp)  /* flag_openmp_simd  */
43939 	    return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43940 					     cclauses, if_p);
43941 	  keep_next_level (true);
43942 	  sb = begin_omp_structured_block ();
43943 	  save = cp_parser_begin_omp_structured_block (parser);
43944 	  ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
43945 					  cclauses, if_p);
43946 	  cp_parser_end_omp_structured_block (parser, save);
43947 	  tree body = finish_omp_structured_block (sb);
43948 	  if (ret == NULL)
43949 	    return ret;
43950 	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43951 	  ret = make_node (OMP_TEAMS);
43952 	  TREE_TYPE (ret) = void_type_node;
43953 	  OMP_TEAMS_CLAUSES (ret) = clauses;
43954 	  OMP_TEAMS_BODY (ret) = body;
43955 	  OMP_TEAMS_COMBINED (ret) = 1;
43956 	  SET_EXPR_LOCATION (ret, loc);
43957 	  return add_stmt (ret);
43958 	}
43959       else if (strcmp (p, "loop") == 0)
43960 	{
43961 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43962 	  if (cclauses == NULL)
43963 	    cclauses = cclauses_buf;
43964 
43965 	  cp_lexer_consume_token (parser->lexer);
43966 	  if (!flag_openmp)  /* flag_openmp_simd  */
43967 	    return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43968 				       cclauses, if_p);
43969 	  keep_next_level (true);
43970 	  sb = begin_omp_structured_block ();
43971 	  save = cp_parser_begin_omp_structured_block (parser);
43972 	  ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43973 				    cclauses, if_p);
43974 	  cp_parser_end_omp_structured_block (parser, save);
43975 	  tree body = finish_omp_structured_block (sb);
43976 	  if (ret == NULL)
43977 	    return ret;
43978 	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
43979 	  ret = make_node (OMP_TEAMS);
43980 	  TREE_TYPE (ret) = void_type_node;
43981 	  OMP_TEAMS_CLAUSES (ret) = clauses;
43982 	  OMP_TEAMS_BODY (ret) = body;
43983 	  OMP_TEAMS_COMBINED (ret) = 1;
43984 	  SET_EXPR_LOCATION (ret, loc);
43985 	  return add_stmt (ret);
43986 	}
43987     }
43988   if (!flag_openmp)  /* flag_openmp_simd  */
43989     {
43990       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43991       return NULL_TREE;
43992     }
43993 
43994   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43995 				       cclauses == NULL);
43996   if (cclauses)
43997     {
43998       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
43999       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44000     }
44001 
44002   tree stmt = make_node (OMP_TEAMS);
44003   TREE_TYPE (stmt) = void_type_node;
44004   OMP_TEAMS_CLAUSES (stmt) = clauses;
44005   keep_next_level (true);
44006   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44007   SET_EXPR_LOCATION (stmt, loc);
44008 
44009   return add_stmt (stmt);
44010 }
44011 
44012 /* OpenMP 4.0:
44013    # pragma omp target data target-data-clause[optseq] new-line
44014      structured-block  */
44015 
44016 #define OMP_TARGET_DATA_CLAUSE_MASK				\
44017 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44018 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44019 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44020 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
44021 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
44022 
44023 static tree
cp_parser_omp_target_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)44024 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44025 {
44026   tree clauses
44027     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
44028 				 "#pragma omp target data", pragma_tok);
44029   c_omp_adjust_map_clauses (clauses, false);
44030   int map_seen = 0;
44031   for (tree *pc = &clauses; *pc;)
44032     {
44033       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44034 	switch (OMP_CLAUSE_MAP_KIND (*pc))
44035 	  {
44036 	  case GOMP_MAP_TO:
44037 	  case GOMP_MAP_ALWAYS_TO:
44038 	  case GOMP_MAP_FROM:
44039 	  case GOMP_MAP_ALWAYS_FROM:
44040 	  case GOMP_MAP_TOFROM:
44041 	  case GOMP_MAP_ALWAYS_TOFROM:
44042 	  case GOMP_MAP_ALLOC:
44043 	    map_seen = 3;
44044 	    break;
44045 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
44046 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44047 	  case GOMP_MAP_ALWAYS_POINTER:
44048 	  case GOMP_MAP_ATTACH_DETACH:
44049 	    break;
44050 	  default:
44051 	    map_seen |= 1;
44052 	    error_at (OMP_CLAUSE_LOCATION (*pc),
44053 		      "%<#pragma omp target data%> with map-type other "
44054 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
44055 		      "on %<map%> clause");
44056 	    *pc = OMP_CLAUSE_CHAIN (*pc);
44057 	    continue;
44058 	  }
44059       else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
44060 	       || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
44061 	map_seen = 3;
44062       pc = &OMP_CLAUSE_CHAIN (*pc);
44063     }
44064 
44065   if (map_seen != 3)
44066     {
44067       if (map_seen == 0)
44068 	error_at (pragma_tok->location,
44069 		  "%<#pragma omp target data%> must contain at least "
44070 		  "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
44071 		  "clause");
44072       return NULL_TREE;
44073     }
44074 
44075   tree stmt = make_node (OMP_TARGET_DATA);
44076   TREE_TYPE (stmt) = void_type_node;
44077   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
44078 
44079   keep_next_level (true);
44080   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44081 
44082   SET_EXPR_LOCATION (stmt, pragma_tok->location);
44083   return add_stmt (stmt);
44084 }
44085 
44086 /* OpenMP 4.5:
44087    # pragma omp target enter data target-enter-data-clause[optseq] new-line
44088      structured-block  */
44089 
44090 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK			\
44091 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44092 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44093 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44094 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44095 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44096 
44097 static bool
cp_parser_omp_target_enter_data(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)44098 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
44099 				 enum pragma_context context)
44100 {
44101   bool data_seen = false;
44102   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44103     {
44104       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44105       const char *p = IDENTIFIER_POINTER (id);
44106 
44107       if (strcmp (p, "data") == 0)
44108 	{
44109 	  cp_lexer_consume_token (parser->lexer);
44110 	  data_seen = true;
44111 	}
44112     }
44113   if (!data_seen)
44114     {
44115       cp_parser_error (parser, "expected %<data%>");
44116       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44117       return false;
44118     }
44119 
44120   if (context == pragma_stmt)
44121     {
44122       error_at (pragma_tok->location,
44123 		"%<#pragma %s%> may only be used in compound statements",
44124 		"omp target enter data");
44125       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44126       return true;
44127     }
44128 
44129   tree clauses
44130     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
44131 				 "#pragma omp target enter data", pragma_tok);
44132   c_omp_adjust_map_clauses (clauses, false);
44133   int map_seen = 0;
44134   for (tree *pc = &clauses; *pc;)
44135     {
44136       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44137 	switch (OMP_CLAUSE_MAP_KIND (*pc))
44138 	  {
44139 	  case GOMP_MAP_TO:
44140 	  case GOMP_MAP_ALWAYS_TO:
44141 	  case GOMP_MAP_ALLOC:
44142 	    map_seen = 3;
44143 	    break;
44144 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
44145 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44146 	  case GOMP_MAP_ALWAYS_POINTER:
44147 	  case GOMP_MAP_ATTACH_DETACH:
44148 	    break;
44149 	  default:
44150 	    map_seen |= 1;
44151 	    error_at (OMP_CLAUSE_LOCATION (*pc),
44152 		      "%<#pragma omp target enter data%> with map-type other "
44153 		      "than %<to%> or %<alloc%> on %<map%> clause");
44154 	    *pc = OMP_CLAUSE_CHAIN (*pc);
44155 	    continue;
44156 	  }
44157       pc = &OMP_CLAUSE_CHAIN (*pc);
44158     }
44159 
44160   if (map_seen != 3)
44161     {
44162       if (map_seen == 0)
44163 	error_at (pragma_tok->location,
44164 		  "%<#pragma omp target enter data%> must contain at least "
44165 		  "one %<map%> clause");
44166       return true;
44167     }
44168 
44169   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
44170   TREE_TYPE (stmt) = void_type_node;
44171   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
44172   SET_EXPR_LOCATION (stmt, pragma_tok->location);
44173   add_stmt (stmt);
44174   return true;
44175 }
44176 
44177 /* OpenMP 4.5:
44178    # pragma omp target exit data target-enter-data-clause[optseq] new-line
44179      structured-block  */
44180 
44181 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK			\
44182 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44183 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44184 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44185 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44186 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44187 
44188 static bool
cp_parser_omp_target_exit_data(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)44189 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
44190 				enum pragma_context context)
44191 {
44192   bool data_seen = false;
44193   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44194     {
44195       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44196       const char *p = IDENTIFIER_POINTER (id);
44197 
44198       if (strcmp (p, "data") == 0)
44199 	{
44200 	  cp_lexer_consume_token (parser->lexer);
44201 	  data_seen = true;
44202 	}
44203     }
44204   if (!data_seen)
44205     {
44206       cp_parser_error (parser, "expected %<data%>");
44207       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44208       return false;
44209     }
44210 
44211   if (context == pragma_stmt)
44212     {
44213       error_at (pragma_tok->location,
44214 		"%<#pragma %s%> may only be used in compound statements",
44215 		"omp target exit data");
44216       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44217       return true;
44218     }
44219 
44220   tree clauses
44221     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
44222 				 "#pragma omp target exit data", pragma_tok);
44223   c_omp_adjust_map_clauses (clauses, false);
44224   int map_seen = 0;
44225   for (tree *pc = &clauses; *pc;)
44226     {
44227       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44228 	switch (OMP_CLAUSE_MAP_KIND (*pc))
44229 	  {
44230 	  case GOMP_MAP_FROM:
44231 	  case GOMP_MAP_ALWAYS_FROM:
44232 	  case GOMP_MAP_RELEASE:
44233 	  case GOMP_MAP_DELETE:
44234 	    map_seen = 3;
44235 	    break;
44236 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
44237 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44238 	  case GOMP_MAP_ALWAYS_POINTER:
44239 	  case GOMP_MAP_ATTACH_DETACH:
44240 	    break;
44241 	  default:
44242 	    map_seen |= 1;
44243 	    error_at (OMP_CLAUSE_LOCATION (*pc),
44244 		      "%<#pragma omp target exit data%> with map-type other "
44245 		      "than %<from%>, %<release%> or %<delete%> on %<map%>"
44246 		      " clause");
44247 	    *pc = OMP_CLAUSE_CHAIN (*pc);
44248 	    continue;
44249 	  }
44250       pc = &OMP_CLAUSE_CHAIN (*pc);
44251     }
44252 
44253   if (map_seen != 3)
44254     {
44255       if (map_seen == 0)
44256 	error_at (pragma_tok->location,
44257 		  "%<#pragma omp target exit data%> must contain at least "
44258 		  "one %<map%> clause");
44259       return true;
44260     }
44261 
44262   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
44263   TREE_TYPE (stmt) = void_type_node;
44264   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
44265   SET_EXPR_LOCATION (stmt, pragma_tok->location);
44266   add_stmt (stmt);
44267   return true;
44268 }
44269 
44270 /* OpenMP 4.0:
44271    # pragma omp target update target-update-clause[optseq] new-line */
44272 
44273 #define OMP_TARGET_UPDATE_CLAUSE_MASK				\
44274 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)		\
44275 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
44276 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44277 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44278 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44279 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44280 
44281 static bool
cp_parser_omp_target_update(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)44282 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
44283 			     enum pragma_context context)
44284 {
44285   if (context == pragma_stmt)
44286     {
44287       error_at (pragma_tok->location,
44288 		"%<#pragma %s%> may only be used in compound statements",
44289 		"omp target update");
44290       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44291       return true;
44292     }
44293 
44294   tree clauses
44295     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
44296 				 "#pragma omp target update", pragma_tok);
44297   if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
44298       && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
44299     {
44300       error_at (pragma_tok->location,
44301 		"%<#pragma omp target update%> must contain at least one "
44302 		"%<from%> or %<to%> clauses");
44303       return true;
44304     }
44305 
44306   tree stmt = make_node (OMP_TARGET_UPDATE);
44307   TREE_TYPE (stmt) = void_type_node;
44308   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
44309   SET_EXPR_LOCATION (stmt, pragma_tok->location);
44310   add_stmt (stmt);
44311   return true;
44312 }
44313 
44314 /* OpenMP 4.0:
44315    # pragma omp target target-clause[optseq] new-line
44316      structured-block  */
44317 
44318 #define OMP_TARGET_CLAUSE_MASK					\
44319 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
44320 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
44321 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
44322 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
44323 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
44324 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
44325 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
44326 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)	\
44327 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
44328 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION)	\
44329 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
44330 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
44331 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
44332 
44333 static bool
cp_parser_omp_target(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context,bool * if_p)44334 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
44335 		      enum pragma_context context, bool *if_p)
44336 {
44337   if (flag_openmp)
44338     omp_requires_mask
44339       = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44340 
44341   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44342     {
44343       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44344       const char *p = IDENTIFIER_POINTER (id);
44345       enum tree_code ccode = ERROR_MARK;
44346 
44347       if (strcmp (p, "teams") == 0)
44348 	ccode = OMP_TEAMS;
44349       else if (strcmp (p, "parallel") == 0)
44350 	ccode = OMP_PARALLEL;
44351       else if (strcmp (p, "simd") == 0)
44352 	ccode = OMP_SIMD;
44353       if (ccode != ERROR_MARK)
44354 	{
44355 	  tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
44356 	  char p_name[sizeof ("#pragma omp target teams distribute "
44357 			      "parallel for simd")];
44358 
44359 	  cp_lexer_consume_token (parser->lexer);
44360 	  strcpy (p_name, "#pragma omp target");
44361 	  if (!flag_openmp)  /* flag_openmp_simd  */
44362 	    {
44363 	      tree stmt;
44364 	      switch (ccode)
44365 		{
44366 		case OMP_TEAMS:
44367 		  stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
44368 					      OMP_TARGET_CLAUSE_MASK,
44369 					      cclauses, if_p);
44370 		  break;
44371 		case OMP_PARALLEL:
44372 		  stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44373 						 OMP_TARGET_CLAUSE_MASK,
44374 						 cclauses, if_p);
44375 		  break;
44376 		case OMP_SIMD:
44377 		  stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
44378 					     OMP_TARGET_CLAUSE_MASK,
44379 					     cclauses, if_p);
44380 		  break;
44381 		default:
44382 		  gcc_unreachable ();
44383 		}
44384 	      return stmt != NULL_TREE;
44385 	    }
44386 	  keep_next_level (true);
44387 	  tree sb = begin_omp_structured_block (), ret;
44388 	  unsigned save = cp_parser_begin_omp_structured_block (parser);
44389 	  switch (ccode)
44390 	    {
44391 	    case OMP_TEAMS:
44392 	      ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
44393 					 OMP_TARGET_CLAUSE_MASK, cclauses,
44394 					 if_p);
44395 	      break;
44396 	    case OMP_PARALLEL:
44397 	      ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44398 					    OMP_TARGET_CLAUSE_MASK, cclauses,
44399 					    if_p);
44400 	      break;
44401 	    case OMP_SIMD:
44402 	      ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
44403 					OMP_TARGET_CLAUSE_MASK, cclauses,
44404 					if_p);
44405 	      break;
44406 	    default:
44407 	      gcc_unreachable ();
44408 	    }
44409 	  cp_parser_end_omp_structured_block (parser, save);
44410 	  tree body = finish_omp_structured_block (sb);
44411 	  if (ret == NULL_TREE)
44412 	    return false;
44413 	  if (ccode == OMP_TEAMS && !processing_template_decl)
44414 	    /* For combined target teams, ensure the num_teams and
44415 	       thread_limit clause expressions are evaluated on the host,
44416 	       before entering the target construct.  */
44417 	    for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44418 		 c; c = OMP_CLAUSE_CHAIN (c))
44419 	      if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
44420 		  || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
44421 		for (int i = 0;
44422 		     i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
44423 		  if (OMP_CLAUSE_OPERAND (c, i)
44424 		      && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
44425 		    {
44426 		      tree expr = OMP_CLAUSE_OPERAND (c, i);
44427 		      expr = force_target_expr (TREE_TYPE (expr), expr,
44428 						tf_none);
44429 		      if (expr == error_mark_node)
44430 			continue;
44431 		      tree tmp = TARGET_EXPR_SLOT (expr);
44432 		      add_stmt (expr);
44433 		      OMP_CLAUSE_OPERAND (c, i) = expr;
44434 		      tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
44435 						  OMP_CLAUSE_FIRSTPRIVATE);
44436 		      OMP_CLAUSE_DECL (tc) = tmp;
44437 		      OMP_CLAUSE_CHAIN (tc)
44438 			= cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
44439 		      cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
44440 		    }
44441 	  c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
44442 	  finish_omp_target (pragma_tok->location,
44443 			     cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
44444 	  return true;
44445 	}
44446       else if (!flag_openmp)  /* flag_openmp_simd  */
44447 	{
44448 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44449 	  return false;
44450 	}
44451       else if (strcmp (p, "data") == 0)
44452 	{
44453 	  cp_lexer_consume_token (parser->lexer);
44454 	  cp_parser_omp_target_data (parser, pragma_tok, if_p);
44455 	  return true;
44456 	}
44457       else if (strcmp (p, "enter") == 0)
44458 	{
44459 	  cp_lexer_consume_token (parser->lexer);
44460 	  return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
44461 	}
44462       else if (strcmp (p, "exit") == 0)
44463 	{
44464 	  cp_lexer_consume_token (parser->lexer);
44465 	  return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
44466 	}
44467       else if (strcmp (p, "update") == 0)
44468 	{
44469 	  cp_lexer_consume_token (parser->lexer);
44470 	  return cp_parser_omp_target_update (parser, pragma_tok, context);
44471 	}
44472     }
44473   if (!flag_openmp)  /* flag_openmp_simd  */
44474     {
44475       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44476       return false;
44477     }
44478 
44479   tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
44480 					    "#pragma omp target", pragma_tok,
44481 					    false);
44482   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
44483     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44484       {
44485 	tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
44486 	OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
44487 	OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
44488 	OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
44489 	OMP_CLAUSE_CHAIN (c) = nc;
44490       }
44491   clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
44492 
44493   c_omp_adjust_map_clauses (clauses, true);
44494   keep_next_level (true);
44495   tree body = cp_parser_omp_structured_block (parser, if_p);
44496 
44497   finish_omp_target (pragma_tok->location, clauses, body, false);
44498   return true;
44499 }
44500 
44501 /* OpenACC 2.0:
44502    # pragma acc cache (variable-list) new-line
44503 */
44504 
44505 static tree
cp_parser_oacc_cache(cp_parser * parser,cp_token * pragma_tok)44506 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
44507 {
44508   /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
44509      clauses.  */
44510   auto_suppress_location_wrappers sentinel;
44511 
44512   tree stmt, clauses;
44513 
44514   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
44515   clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44516 
44517   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
44518 
44519   stmt = make_node (OACC_CACHE);
44520   TREE_TYPE (stmt) = void_type_node;
44521   OACC_CACHE_CLAUSES (stmt) = clauses;
44522   SET_EXPR_LOCATION (stmt, pragma_tok->location);
44523   add_stmt (stmt);
44524 
44525   return stmt;
44526 }
44527 
44528 /* OpenACC 2.0:
44529    # pragma acc data oacc-data-clause[optseq] new-line
44530      structured-block  */
44531 
44532 #define OACC_DATA_CLAUSE_MASK						\
44533 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44534 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44535 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44536 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44537 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44538 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH)		\
44539 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44540 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44541 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44542 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44543 
44544 static tree
cp_parser_oacc_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)44545 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44546 {
44547   tree stmt, clauses, block;
44548   unsigned int save;
44549 
44550   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
44551 					"#pragma acc data", pragma_tok);
44552 
44553   block = begin_omp_parallel ();
44554   save = cp_parser_begin_omp_structured_block (parser);
44555   cp_parser_statement (parser, NULL_TREE, false, if_p);
44556   cp_parser_end_omp_structured_block (parser, save);
44557   stmt = finish_oacc_data (clauses, block);
44558   return stmt;
44559 }
44560 
44561 /* OpenACC 2.0:
44562   # pragma acc host_data <clauses> new-line
44563   structured-block  */
44564 
44565 #define OACC_HOST_DATA_CLAUSE_MASK					\
44566   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE)                \
44567    | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                        \
44568    | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
44569 
44570 static tree
cp_parser_oacc_host_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)44571 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44572 {
44573   tree stmt, clauses, block;
44574   unsigned int save;
44575 
44576   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
44577 					"#pragma acc host_data", pragma_tok);
44578 
44579   block = begin_omp_parallel ();
44580   save = cp_parser_begin_omp_structured_block (parser);
44581   cp_parser_statement (parser, NULL_TREE, false, if_p);
44582   cp_parser_end_omp_structured_block (parser, save);
44583   stmt = finish_oacc_host_data (clauses, block);
44584   return stmt;
44585 }
44586 
44587 /* OpenACC 2.0:
44588    # pragma acc declare oacc-data-clause[optseq] new-line
44589 */
44590 
44591 #define OACC_DECLARE_CLAUSE_MASK					\
44592 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44593 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44594 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44595 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44596 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44597 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)	\
44598 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)		\
44599 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44600 
44601 static tree
cp_parser_oacc_declare(cp_parser * parser,cp_token * pragma_tok)44602 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
44603 {
44604   tree clauses, stmt;
44605   bool error = false;
44606   bool found_in_scope = global_bindings_p ();
44607 
44608   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
44609 					"#pragma acc declare", pragma_tok, true);
44610 
44611 
44612   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44613     {
44614       error_at (pragma_tok->location,
44615 		"no valid clauses specified in %<#pragma acc declare%>");
44616       return NULL_TREE;
44617     }
44618 
44619   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
44620     {
44621       location_t loc = OMP_CLAUSE_LOCATION (t);
44622       tree decl = OMP_CLAUSE_DECL (t);
44623       if (!DECL_P (decl))
44624 	{
44625 	  error_at (loc, "array section in %<#pragma acc declare%>");
44626 	  error = true;
44627 	  continue;
44628 	}
44629       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
44630       switch (OMP_CLAUSE_MAP_KIND (t))
44631 	{
44632 	case GOMP_MAP_FIRSTPRIVATE_POINTER:
44633 	case GOMP_MAP_ALLOC:
44634 	case GOMP_MAP_TO:
44635 	case GOMP_MAP_FORCE_DEVICEPTR:
44636 	case GOMP_MAP_DEVICE_RESIDENT:
44637 	  break;
44638 
44639 	case GOMP_MAP_LINK:
44640 	  if (!global_bindings_p ()
44641 	      && (TREE_STATIC (decl)
44642 	       || !DECL_EXTERNAL (decl)))
44643 	    {
44644 	      error_at (loc,
44645 			"%qD must be a global variable in "
44646 			"%<#pragma acc declare link%>",
44647 			decl);
44648 	      error = true;
44649 	      continue;
44650 	    }
44651 	  break;
44652 
44653 	default:
44654 	  if (global_bindings_p ())
44655 	    {
44656 	      error_at (loc, "invalid OpenACC clause at file scope");
44657 	      error = true;
44658 	      continue;
44659 	    }
44660 	  if (DECL_EXTERNAL (decl))
44661 	    {
44662 	      error_at (loc,
44663 			"invalid use of %<extern%> variable %qD "
44664 			"in %<#pragma acc declare%>", decl);
44665 	      error = true;
44666 	      continue;
44667 	    }
44668 	  else if (TREE_PUBLIC (decl))
44669 	    {
44670 	      error_at (loc,
44671 			"invalid use of %<global%> variable %qD "
44672 			"in %<#pragma acc declare%>", decl);
44673 	      error = true;
44674 	      continue;
44675 	    }
44676 	  break;
44677 	}
44678 
44679       if (!found_in_scope)
44680 	/* This seems to ignore the existence of cleanup scopes?
44681 	   What is the meaning for local extern decls?  The local
44682 	   extern is in this scope, but it is referring to a decl that
44683 	   is namespace scope.  */
44684 	for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
44685 	  if (d == decl)
44686 	    {
44687 	      found_in_scope = true;
44688 	      break;
44689 	    }
44690       if (!found_in_scope)
44691 	{
44692 	  error_at (loc,
44693 		    "%qD must be a variable declared in the same scope as "
44694 		    "%<#pragma acc declare%>", decl);
44695 	  error = true;
44696 	  continue;
44697 	}
44698 
44699       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
44700 	  || lookup_attribute ("omp declare target link",
44701 			       DECL_ATTRIBUTES (decl)))
44702 	{
44703 	  error_at (loc, "variable %qD used more than once with "
44704 		    "%<#pragma acc declare%>", decl);
44705 	  error = true;
44706 	  continue;
44707 	}
44708 
44709       if (!error)
44710 	{
44711 	  tree id;
44712 
44713 	  if (DECL_LOCAL_DECL_P (decl))
44714 	    /* We need to mark the aliased decl, as that is the entity
44715 	       that is being referred to.  This won't work for
44716 	       dependent variables, but it didn't work for them before
44717 	       DECL_LOCAL_DECL_P was a thing either.  But then
44718 	       dependent local extern variable decls are as rare as
44719 	       hen's teeth.  */
44720 	    if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
44721 	      if (alias != error_mark_node)
44722 		decl = alias;
44723 
44724 	  if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
44725 	    id = get_identifier ("omp declare target link");
44726 	  else
44727 	    id = get_identifier ("omp declare target");
44728 
44729 	  DECL_ATTRIBUTES (decl)
44730 	    = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
44731 	  if (current_binding_level->kind == sk_namespace)
44732 	    {
44733 	      symtab_node *node = symtab_node::get (decl);
44734 	      if (node != NULL)
44735 		{
44736 		  node->offloadable = 1;
44737 		  if (ENABLE_OFFLOADING)
44738 		    {
44739 		      g->have_offload = true;
44740 		      if (is_a <varpool_node *> (node))
44741 			vec_safe_push (offload_vars, decl);
44742 		    }
44743 		}
44744 	    }
44745 	}
44746     }
44747 
44748   if (error || current_binding_level->kind == sk_namespace)
44749     return NULL_TREE;
44750 
44751   stmt = make_node (OACC_DECLARE);
44752   TREE_TYPE (stmt) = void_type_node;
44753   OACC_DECLARE_CLAUSES (stmt) = clauses;
44754   SET_EXPR_LOCATION (stmt, pragma_tok->location);
44755 
44756   add_stmt (stmt);
44757 
44758   return NULL_TREE;
44759 }
44760 
44761 /* OpenACC 2.0:
44762    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
44763 
44764    or
44765 
44766    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
44767 
44768    LOC is the location of the #pragma token.
44769 */
44770 
44771 #define OACC_ENTER_DATA_CLAUSE_MASK					\
44772 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44773 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44774 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44775 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44776 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44777 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44778 
44779 #define OACC_EXIT_DATA_CLAUSE_MASK					\
44780 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44781 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44782 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44783 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) 		\
44784 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH)		\
44785 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) 		\
44786 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44787 
44788 static tree
cp_parser_oacc_enter_exit_data(cp_parser * parser,cp_token * pragma_tok,bool enter)44789 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
44790 				bool enter)
44791 {
44792   location_t loc = pragma_tok->location;
44793   tree stmt, clauses;
44794   const char *p = "";
44795 
44796   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44797     p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44798 
44799   if (strcmp (p, "data") != 0)
44800     {
44801       error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
44802 		enter ? "enter" : "exit");
44803       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44804       return NULL_TREE;
44805     }
44806 
44807   cp_lexer_consume_token (parser->lexer);
44808 
44809   if (enter)
44810     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
44811 					 "#pragma acc enter data", pragma_tok);
44812   else
44813     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
44814 					 "#pragma acc exit data", pragma_tok);
44815 
44816   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
44817     {
44818       error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
44819 		enter ? "enter" : "exit");
44820       return NULL_TREE;
44821     }
44822 
44823   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
44824   TREE_TYPE (stmt) = void_type_node;
44825   OMP_STANDALONE_CLAUSES (stmt) = clauses;
44826   SET_EXPR_LOCATION (stmt, loc);
44827   add_stmt (stmt);
44828   return stmt;
44829 }
44830 
44831 /* OpenACC 2.0:
44832    # pragma acc loop oacc-loop-clause[optseq] new-line
44833      structured-block  */
44834 
44835 #define OACC_LOOP_CLAUSE_MASK						\
44836 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)		\
44837 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
44838 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
44839 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
44840 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
44841 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
44842 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)		\
44843 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)		\
44844 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
44845 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
44846 
44847 static tree
cp_parser_oacc_loop(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)44848 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
44849 		     omp_clause_mask mask, tree *cclauses, bool *if_p)
44850 {
44851   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
44852 
44853   strcat (p_name, " loop");
44854   mask |= OACC_LOOP_CLAUSE_MASK;
44855 
44856   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
44857 					     cclauses == NULL);
44858   if (cclauses)
44859     {
44860       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
44861       if (*cclauses)
44862 	*cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
44863       if (clauses)
44864 	clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44865     }
44866 
44867   tree block = begin_omp_structured_block ();
44868   int save = cp_parser_begin_omp_structured_block (parser);
44869   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
44870   cp_parser_end_omp_structured_block (parser, save);
44871   add_stmt (finish_omp_structured_block (block));
44872 
44873   return stmt;
44874 }
44875 
44876 /* OpenACC 2.0:
44877    # pragma acc kernels oacc-kernels-clause[optseq] new-line
44878      structured-block
44879 
44880    or
44881 
44882    # pragma acc parallel oacc-parallel-clause[optseq] new-line
44883      structured-block
44884 
44885    OpenACC 2.6:
44886 
44887    # pragma acc serial oacc-serial-clause[optseq] new-line
44888 */
44889 
44890 #define OACC_KERNELS_CLAUSE_MASK					\
44891 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44892 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44893 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44894 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44895 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44896 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44897 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
44898 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44899 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44900 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44901 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
44902 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
44903 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
44904 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)	\
44905 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44906 
44907 #define OACC_PARALLEL_CLAUSE_MASK					\
44908 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44909 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44910 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44911 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44912 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44913 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44914 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
44915 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44916 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)	\
44917 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44918 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44919 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
44920 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
44921 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
44922 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
44923 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
44924 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
44925 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44926 
44927 #define OACC_SERIAL_CLAUSE_MASK						\
44928 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
44929 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH)		\
44930 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
44931 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
44932 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
44933 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
44934 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
44935 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
44936 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
44937 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE)		\
44938 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
44939 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)	\
44940 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
44941 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
44942 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
44943 
44944 static tree
cp_parser_oacc_compute(cp_parser * parser,cp_token * pragma_tok,char * p_name,bool * if_p)44945 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
44946 			char *p_name, bool *if_p)
44947 {
44948   omp_clause_mask mask;
44949   enum tree_code code;
44950   switch (cp_parser_pragma_kind (pragma_tok))
44951     {
44952     case PRAGMA_OACC_KERNELS:
44953       strcat (p_name, " kernels");
44954       mask = OACC_KERNELS_CLAUSE_MASK;
44955       code = OACC_KERNELS;
44956       break;
44957     case PRAGMA_OACC_PARALLEL:
44958       strcat (p_name, " parallel");
44959       mask = OACC_PARALLEL_CLAUSE_MASK;
44960       code = OACC_PARALLEL;
44961       break;
44962     case PRAGMA_OACC_SERIAL:
44963       strcat (p_name, " serial");
44964       mask = OACC_SERIAL_CLAUSE_MASK;
44965       code = OACC_SERIAL;
44966       break;
44967     default:
44968       gcc_unreachable ();
44969     }
44970 
44971   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44972     {
44973       const char *p
44974 	= IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
44975       if (strcmp (p, "loop") == 0)
44976 	{
44977 	  cp_lexer_consume_token (parser->lexer);
44978 	  tree block = begin_omp_parallel ();
44979 	  tree clauses;
44980 	  tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
44981 					   &clauses, if_p);
44982 	  protected_set_expr_location (stmt, pragma_tok->location);
44983 	  return finish_omp_construct (code, block, clauses);
44984 	}
44985     }
44986 
44987   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
44988 
44989   tree block = begin_omp_parallel ();
44990   unsigned int save = cp_parser_begin_omp_structured_block (parser);
44991   cp_parser_statement (parser, NULL_TREE, false, if_p);
44992   cp_parser_end_omp_structured_block (parser, save);
44993   return finish_omp_construct (code, block, clauses);
44994 }
44995 
44996 /* OpenACC 2.0:
44997    # pragma acc update oacc-update-clause[optseq] new-line
44998 */
44999 
45000 #define OACC_UPDATE_CLAUSE_MASK						\
45001 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
45002 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)		\
45003 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)		\
45004 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
45005 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT)		\
45006 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
45007 
45008 static tree
cp_parser_oacc_update(cp_parser * parser,cp_token * pragma_tok)45009 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
45010 {
45011   tree stmt, clauses;
45012 
45013   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
45014 					 "#pragma acc update", pragma_tok);
45015 
45016   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45017     {
45018       error_at (pragma_tok->location,
45019 		"%<#pragma acc update%> must contain at least one "
45020 		"%<device%> or %<host%> or %<self%> clause");
45021       return NULL_TREE;
45022     }
45023 
45024   stmt = make_node (OACC_UPDATE);
45025   TREE_TYPE (stmt) = void_type_node;
45026   OACC_UPDATE_CLAUSES (stmt) = clauses;
45027   SET_EXPR_LOCATION (stmt, pragma_tok->location);
45028   add_stmt (stmt);
45029   return stmt;
45030 }
45031 
45032 /* OpenACC 2.0:
45033    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
45034 
45035    LOC is the location of the #pragma token.
45036 */
45037 
45038 #define OACC_WAIT_CLAUSE_MASK					\
45039 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
45040 
45041 static tree
cp_parser_oacc_wait(cp_parser * parser,cp_token * pragma_tok)45042 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
45043 {
45044   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
45045   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45046 
45047   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
45048     list = cp_parser_oacc_wait_list (parser, loc, list);
45049 
45050   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
45051 					"#pragma acc wait", pragma_tok);
45052 
45053   stmt = c_finish_oacc_wait (loc, list, clauses);
45054   stmt = finish_expr_stmt (stmt);
45055 
45056   return stmt;
45057 }
45058 
45059 /* OpenMP 4.0:
45060    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
45061 
45062 #define OMP_DECLARE_SIMD_CLAUSE_MASK				\
45063 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
45064 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
45065 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
45066 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)	\
45067 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)	\
45068 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
45069 
45070 static void
cp_parser_omp_declare_simd(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context,bool variant_p)45071 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
45072 			    enum pragma_context context,
45073 			    bool variant_p)
45074 {
45075   bool first_p = parser->omp_declare_simd == NULL;
45076   cp_omp_declare_simd_data data;
45077   if (first_p)
45078     {
45079       data.error_seen = false;
45080       data.fndecl_seen = false;
45081       data.variant_p = variant_p;
45082       data.tokens = vNULL;
45083       data.attribs[0] = NULL;
45084       data.attribs[1] = NULL;
45085       data.loc = UNKNOWN_LOCATION;
45086       /* It is safe to take the address of a local variable; it will only be
45087 	 used while this scope is live.  */
45088       parser->omp_declare_simd = &data;
45089     }
45090   else if (parser->omp_declare_simd->variant_p != variant_p)
45091     {
45092       error_at (pragma_tok->location,
45093 		"%<#pragma omp declare %s%> followed by "
45094 		"%<#pragma omp declare %s%>",
45095 		parser->omp_declare_simd->variant_p ? "variant" : "simd",
45096 		parser->omp_declare_simd->variant_p ? "simd" : "variant");
45097       parser->omp_declare_simd->error_seen = true;
45098     }
45099 
45100   /* Store away all pragma tokens.  */
45101   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
45102     cp_lexer_consume_token (parser->lexer);
45103   cp_parser_require_pragma_eol (parser, pragma_tok);
45104   struct cp_token_cache *cp
45105     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
45106   parser->omp_declare_simd->tokens.safe_push (cp);
45107 
45108   if (first_p)
45109     {
45110       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
45111 	cp_parser_pragma (parser, context, NULL);
45112       switch (context)
45113 	{
45114 	case pragma_external:
45115 	  cp_parser_declaration (parser, NULL_TREE);
45116 	  break;
45117 	case pragma_member:
45118 	  cp_parser_member_declaration (parser);
45119 	  break;
45120 	case pragma_objc_icode:
45121 	  cp_parser_block_declaration (parser, /*statement_p=*/false);
45122 	  break;
45123 	default:
45124 	  cp_parser_declaration_statement (parser);
45125 	  break;
45126 	}
45127       if (parser->omp_declare_simd
45128 	  && !parser->omp_declare_simd->error_seen
45129 	  && !parser->omp_declare_simd->fndecl_seen)
45130 	error_at (pragma_tok->location,
45131 		  "%<#pragma omp declare %s%> not immediately followed by "
45132 		  "function declaration or definition",
45133 		  parser->omp_declare_simd->variant_p ? "variant" : "simd");
45134       data.tokens.release ();
45135       parser->omp_declare_simd = NULL;
45136     }
45137 }
45138 
45139 static const char *const omp_construct_selectors[] = {
45140   "simd", "target", "teams", "parallel", "for", NULL };
45141 static const char *const omp_device_selectors[] = {
45142   "kind", "isa", "arch", NULL };
45143 static const char *const omp_implementation_selectors[] = {
45144   "vendor", "extension", "atomic_default_mem_order", "unified_address",
45145   "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
45146 static const char *const omp_user_selectors[] = {
45147   "condition", NULL };
45148 
45149 /* OpenMP 5.0:
45150 
45151    trait-selector:
45152      trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
45153 
45154    trait-score:
45155      score(score-expression)  */
45156 
45157 static tree
cp_parser_omp_context_selector(cp_parser * parser,tree set,bool has_parms_p)45158 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
45159 {
45160   tree ret = NULL_TREE;
45161   do
45162     {
45163       tree selector;
45164       if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45165 	  || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45166 	selector = cp_lexer_peek_token (parser->lexer)->u.value;
45167       else
45168 	{
45169 	  cp_parser_error (parser, "expected trait selector name");
45170 	  return error_mark_node;
45171 	}
45172 
45173       tree properties = NULL_TREE;
45174       const char *const *selectors = NULL;
45175       bool allow_score = true;
45176       bool allow_user = false;
45177       int property_limit = 0;
45178       enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
45179 	     CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
45180 	     CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
45181       switch (IDENTIFIER_POINTER (set)[0])
45182 	{
45183 	case 'c': /* construct */
45184 	  selectors = omp_construct_selectors;
45185 	  allow_score = false;
45186 	  property_limit = 1;
45187 	  property_kind = CTX_PROPERTY_SIMD;
45188 	  break;
45189 	case 'd': /* device */
45190 	  selectors = omp_device_selectors;
45191 	  allow_score = false;
45192 	  allow_user = true;
45193 	  property_limit = 3;
45194 	  property_kind = CTX_PROPERTY_NAME_LIST;
45195 	  break;
45196 	case 'i': /* implementation */
45197 	  selectors = omp_implementation_selectors;
45198 	  allow_user = true;
45199 	  property_limit = 3;
45200 	  property_kind = CTX_PROPERTY_NAME_LIST;
45201 	  break;
45202 	case 'u': /* user */
45203 	  selectors = omp_user_selectors;
45204 	  property_limit = 1;
45205 	  property_kind = CTX_PROPERTY_EXPR;
45206 	  break;
45207 	default:
45208 	  gcc_unreachable ();
45209 	}
45210       for (int i = 0; ; i++)
45211 	{
45212 	  if (selectors[i] == NULL)
45213 	    {
45214 	      if (allow_user)
45215 		{
45216 		  property_kind = CTX_PROPERTY_USER;
45217 		  break;
45218 		}
45219 	      else
45220 		{
45221 		  error ("selector %qs not allowed for context selector "
45222 			 "set %qs", IDENTIFIER_POINTER (selector),
45223 			 IDENTIFIER_POINTER (set));
45224 		  cp_lexer_consume_token (parser->lexer);
45225 		  return error_mark_node;
45226 		}
45227 	    }
45228 	  if (i == property_limit)
45229 	    property_kind = CTX_PROPERTY_NONE;
45230 	  if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
45231 	    break;
45232 	}
45233       if (property_kind == CTX_PROPERTY_NAME_LIST
45234 	  && IDENTIFIER_POINTER (set)[0] == 'i'
45235 	  && strcmp (IDENTIFIER_POINTER (selector),
45236 		     "atomic_default_mem_order") == 0)
45237 	property_kind = CTX_PROPERTY_ID;
45238 
45239       cp_lexer_consume_token (parser->lexer);
45240 
45241       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45242 	{
45243 	  if (property_kind == CTX_PROPERTY_NONE)
45244 	    {
45245 	      error ("selector %qs does not accept any properties",
45246 		     IDENTIFIER_POINTER (selector));
45247 	      return error_mark_node;
45248 	    }
45249 
45250 	  matching_parens parens;
45251 	  parens.consume_open (parser);
45252 
45253 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
45254 	  if (allow_score
45255 	      && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45256 	      && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
45257 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
45258 	    {
45259 	      cp_lexer_save_tokens (parser->lexer);
45260 	      cp_lexer_consume_token (parser->lexer);
45261 	      cp_lexer_consume_token (parser->lexer);
45262 	      if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
45263 							 true)
45264 		  && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
45265 		{
45266 		  cp_lexer_rollback_tokens (parser->lexer);
45267 		  cp_lexer_consume_token (parser->lexer);
45268 
45269 		  matching_parens parens2;
45270 		  parens2.require_open (parser);
45271 		  tree score = cp_parser_constant_expression (parser);
45272 		  if (!parens2.require_close (parser))
45273 		    cp_parser_skip_to_closing_parenthesis (parser, true,
45274 							   false, true);
45275 		  cp_parser_require (parser, CPP_COLON, RT_COLON);
45276 		  if (score != error_mark_node)
45277 		    {
45278 		      score = fold_non_dependent_expr (score);
45279 		      if (value_dependent_expression_p (score))
45280 			properties = tree_cons (get_identifier (" score"),
45281 						score, properties);
45282 		      else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
45283 			       || TREE_CODE (score) != INTEGER_CST)
45284 			error_at (token->location, "score argument must be "
45285 				  "constant integer expression");
45286 		      else if (tree_int_cst_sgn (score) < 0)
45287 			error_at (token->location, "score argument must be "
45288 				  "non-negative");
45289 		      else
45290 			properties = tree_cons (get_identifier (" score"),
45291 						score, properties);
45292 		    }
45293 		}
45294 	      else
45295 		cp_lexer_rollback_tokens (parser->lexer);
45296 
45297 	      token = cp_lexer_peek_token (parser->lexer);
45298 	    }
45299 
45300 	  switch (property_kind)
45301 	    {
45302 	      tree t;
45303 	    case CTX_PROPERTY_USER:
45304 	      do
45305 		{
45306 		  t = cp_parser_constant_expression (parser);
45307 		  if (t != error_mark_node)
45308 		    {
45309 		      t = fold_non_dependent_expr (t);
45310 		      if (TREE_CODE (t) == STRING_CST)
45311 			properties = tree_cons (NULL_TREE, t, properties);
45312 		      else if (!value_dependent_expression_p (t)
45313 			       && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45314 				   || !tree_fits_shwi_p (t)))
45315 			error_at (token->location, "property must be "
45316 				  "constant integer expression or string "
45317 				  "literal");
45318 		      else
45319 			properties = tree_cons (NULL_TREE, t, properties);
45320 		    }
45321 		  else
45322 		    return error_mark_node;
45323 
45324 		  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45325 		    cp_lexer_consume_token (parser->lexer);
45326 		  else
45327 		    break;
45328 		}
45329 	      while (1);
45330 	      break;
45331 	    case CTX_PROPERTY_ID:
45332 	      if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45333 		  || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45334 		{
45335 		  tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
45336 		  cp_lexer_consume_token (parser->lexer);
45337 		  properties = tree_cons (prop, NULL_TREE, properties);
45338 		}
45339 	      else
45340 		{
45341 		  cp_parser_error (parser, "expected identifier");
45342 		  return error_mark_node;
45343 		}
45344 	      break;
45345 	    case CTX_PROPERTY_NAME_LIST:
45346 	      do
45347 		{
45348 		  tree prop = NULL_TREE, value = NULL_TREE;
45349 		  if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45350 		      || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45351 		    {
45352 		      prop = cp_lexer_peek_token (parser->lexer)->u.value;
45353 		      cp_lexer_consume_token (parser->lexer);
45354 		    }
45355 		  else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
45356 		    value = cp_parser_string_literal (parser, false, false);
45357 		  else
45358 		    {
45359 		      cp_parser_error (parser, "expected identifier or "
45360 					       "string literal");
45361 		      return error_mark_node;
45362 		    }
45363 
45364 		  properties = tree_cons (prop, value, properties);
45365 
45366 		  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45367 		    cp_lexer_consume_token (parser->lexer);
45368 		  else
45369 		    break;
45370 		}
45371 	      while (1);
45372 	      break;
45373 	    case CTX_PROPERTY_EXPR:
45374 	      t = cp_parser_constant_expression (parser);
45375 	      if (t != error_mark_node)
45376 		{
45377 		  t = fold_non_dependent_expr (t);
45378 		  if (!value_dependent_expression_p (t)
45379 		      && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45380 			  || !tree_fits_shwi_p (t)))
45381 		    error_at (token->location, "property must be "
45382 			      "constant integer expression");
45383 		  else
45384 		    properties = tree_cons (NULL_TREE, t, properties);
45385 		}
45386 	      else
45387 		return error_mark_node;
45388 	      break;
45389 	    case CTX_PROPERTY_SIMD:
45390 	      if (!has_parms_p)
45391 		{
45392 		  error_at (token->location, "properties for %<simd%> "
45393 			    "selector may not be specified in "
45394 			    "%<metadirective%>");
45395 		  return error_mark_node;
45396 		}
45397 	      properties
45398 		= cp_parser_omp_all_clauses (parser,
45399 					     OMP_DECLARE_SIMD_CLAUSE_MASK,
45400 					     "simd", NULL, true, 2);
45401 	      break;
45402 	    default:
45403 	      gcc_unreachable ();
45404 	    }
45405 
45406 	  if (!parens.require_close (parser))
45407 	    cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
45408 
45409 	  properties = nreverse (properties);
45410 	}
45411       else if (property_kind == CTX_PROPERTY_NAME_LIST
45412 	       || property_kind == CTX_PROPERTY_ID
45413 	       || property_kind == CTX_PROPERTY_EXPR)
45414 	{
45415 	  cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
45416 	  return error_mark_node;
45417 	}
45418 
45419       ret = tree_cons (selector, properties, ret);
45420 
45421       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45422 	cp_lexer_consume_token (parser->lexer);
45423       else
45424 	break;
45425     }
45426   while (1);
45427 
45428   return nreverse (ret);
45429 }
45430 
45431 /* OpenMP 5.0:
45432 
45433    trait-set-selector[,trait-set-selector[,...]]
45434 
45435    trait-set-selector:
45436      trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45437 
45438    trait-set-selector-name:
45439      constructor
45440      device
45441      implementation
45442      user  */
45443 
45444 static tree
cp_parser_omp_context_selector_specification(cp_parser * parser,bool has_parms_p)45445 cp_parser_omp_context_selector_specification (cp_parser *parser,
45446 					      bool has_parms_p)
45447 {
45448   tree ret = NULL_TREE;
45449   do
45450     {
45451       const char *setp = "";
45452       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45453 	setp
45454 	  = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45455       switch (setp[0])
45456 	{
45457 	case 'c':
45458 	  if (strcmp (setp, "construct") == 0)
45459 	    setp = NULL;
45460 	  break;
45461 	case 'd':
45462 	  if (strcmp (setp, "device") == 0)
45463 	    setp = NULL;
45464 	  break;
45465 	case 'i':
45466 	  if (strcmp (setp, "implementation") == 0)
45467 	    setp = NULL;
45468 	  break;
45469 	case 'u':
45470 	  if (strcmp (setp, "user") == 0)
45471 	    setp = NULL;
45472 	  break;
45473 	default:
45474 	  break;
45475 	}
45476       if (setp)
45477 	{
45478 	  cp_parser_error (parser, "expected %<construct%>, %<device%>, "
45479 				   "%<implementation%> or %<user%>");
45480 	  return error_mark_node;
45481 	}
45482 
45483       tree set = cp_lexer_peek_token (parser->lexer)->u.value;
45484       cp_lexer_consume_token (parser->lexer);
45485 
45486       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
45487 	return error_mark_node;
45488 
45489       matching_braces braces;
45490       if (!braces.require_open (parser))
45491 	return error_mark_node;
45492 
45493       tree selectors
45494 	= cp_parser_omp_context_selector (parser, set, has_parms_p);
45495       if (selectors == error_mark_node)
45496 	{
45497 	  cp_parser_skip_to_closing_brace (parser);
45498 	  ret = error_mark_node;
45499 	}
45500       else if (ret != error_mark_node)
45501 	ret = tree_cons (set, selectors, ret);
45502 
45503       braces.require_close (parser);
45504 
45505       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45506 	cp_lexer_consume_token (parser->lexer);
45507       else
45508 	break;
45509     }
45510   while (1);
45511 
45512   if (ret == error_mark_node)
45513     return ret;
45514   return nreverse (ret);
45515 }
45516 
45517 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
45518    that into "omp declare variant base" attribute.  */
45519 
45520 static tree
cp_finish_omp_declare_variant(cp_parser * parser,cp_token * pragma_tok,tree attrs)45521 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
45522 			       tree attrs)
45523 {
45524   matching_parens parens;
45525   if (!parens.require_open (parser))
45526     {
45527      fail:
45528       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45529       return attrs;
45530     }
45531 
45532   bool template_p;
45533   cp_id_kind idk = CP_ID_KIND_NONE;
45534   cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
45535   cp_expr varid
45536     = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
45537 			       /*check_dependency_p=*/true,
45538 			       /*template_p=*/&template_p,
45539 			       /*declarator_p=*/false,
45540 			       /*optional_p=*/false);
45541   parens.require_close (parser);
45542 
45543   tree variant;
45544   if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
45545       || TREE_CODE (varid) == TYPE_DECL
45546       || varid == error_mark_node)
45547     variant = varid;
45548   else if (varid_token->type == CPP_NAME && varid_token->error_reported)
45549     variant = NULL_TREE;
45550   else
45551     {
45552       tree ambiguous_decls;
45553       variant = cp_parser_lookup_name (parser, varid, none_type,
45554 				       template_p, /*is_namespace=*/false,
45555 				       /*check_dependency=*/true,
45556 				       &ambiguous_decls,
45557 				       varid.get_location ());
45558       if (ambiguous_decls)
45559 	variant = NULL_TREE;
45560     }
45561   if (variant == NULL_TREE)
45562     variant = error_mark_node;
45563   else if (TREE_CODE (variant) != SCOPE_REF)
45564     {
45565       const char *error_msg;
45566       variant
45567 	= finish_id_expression (varid, variant, parser->scope,
45568 				&idk, false, true,
45569 				&parser->non_integral_constant_expression_p,
45570 				template_p, true, false, false, &error_msg,
45571 				varid.get_location ());
45572       if (error_msg)
45573 	cp_parser_error (parser, error_msg);
45574     }
45575   location_t caret_loc = get_pure_location (varid.get_location ());
45576   location_t start_loc = get_start (varid_token->location);
45577   location_t finish_loc = get_finish (varid.get_location ());
45578   location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
45579 
45580   /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
45581   if (parser->lexer->in_omp_attribute_pragma
45582       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45583       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45584     cp_lexer_consume_token (parser->lexer);
45585 
45586   const char *clause = "";
45587   location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
45588   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45589     clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45590   if (strcmp (clause, "match"))
45591     {
45592       cp_parser_error (parser, "expected %<match%>");
45593       goto fail;
45594     }
45595 
45596   cp_lexer_consume_token (parser->lexer);
45597 
45598   if (!parens.require_open (parser))
45599     goto fail;
45600 
45601   tree ctx = cp_parser_omp_context_selector_specification (parser, true);
45602   if (ctx == error_mark_node)
45603     goto fail;
45604   ctx = omp_check_context_selector (match_loc, ctx);
45605   if (ctx != error_mark_node && variant != error_mark_node)
45606     {
45607       tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
45608 						      match_loc);
45609       tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
45610       loc_node = tree_cons (match_loc_node,
45611 			    build_int_cst (integer_type_node, idk),
45612 			    build_tree_list (loc_node, integer_zero_node));
45613       attrs = tree_cons (get_identifier ("omp declare variant base"),
45614 			 tree_cons (variant, ctx, loc_node), attrs);
45615       if (processing_template_decl)
45616 	ATTR_IS_DEPENDENT (attrs) = 1;
45617     }
45618 
45619   parens.require_close (parser);
45620   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45621   return attrs;
45622 }
45623 
45624 
45625 /* Finalize #pragma omp declare simd clauses after direct declarator has
45626    been parsed, and put that into "omp declare simd" attribute.  */
45627 
45628 static tree
cp_parser_late_parsing_omp_declare_simd(cp_parser * parser,tree attrs)45629 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
45630 {
45631   struct cp_token_cache *ce;
45632   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
45633   int i;
45634 
45635   if (!data->error_seen && data->fndecl_seen)
45636     {
45637       error ("%<#pragma omp declare %s%> not immediately followed by "
45638 	     "a single function declaration or definition",
45639 	     data->variant_p ? "variant" : "simd");
45640       data->error_seen = true;
45641     }
45642   if (data->error_seen)
45643     return attrs;
45644 
45645   FOR_EACH_VEC_ELT (data->tokens, i, ce)
45646     {
45647       tree c, cl;
45648 
45649       cp_parser_push_lexer_for_tokens (parser, ce);
45650       parser->lexer->in_pragma = true;
45651       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
45652       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45653       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45654       const char *kind = IDENTIFIER_POINTER (id);
45655       cp_lexer_consume_token (parser->lexer);
45656       if (strcmp (kind, "simd") == 0)
45657 	{
45658 	  /* For now only in C++ attributes, do it always for OpenMP 5.1.
45659 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45660 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45661 	    cp_lexer_consume_token (parser->lexer);  */
45662 
45663 	  cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
45664 					  "#pragma omp declare simd",
45665 					  pragma_tok);
45666 	  if (cl)
45667 	    cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45668 	  c = build_tree_list (get_identifier ("omp declare simd"), cl);
45669 	  TREE_CHAIN (c) = attrs;
45670 	  if (processing_template_decl)
45671 	    ATTR_IS_DEPENDENT (c) = 1;
45672 	  attrs = c;
45673 	}
45674       else
45675 	{
45676 	  gcc_assert (strcmp (kind, "variant") == 0);
45677 	  attrs
45678 	    = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
45679 	}
45680       cp_parser_pop_lexer (parser);
45681     }
45682 
45683   cp_lexer *lexer = NULL;
45684   for (int i = 0; i < 2; i++)
45685     {
45686       if (data->attribs[i] == NULL)
45687 	continue;
45688       for (tree *pa = data->attribs[i]; *pa; )
45689 	if (get_attribute_namespace (*pa) == omp_identifier
45690 	    && is_attribute_p ("directive", get_attribute_name (*pa)))
45691 	  {
45692 	    for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
45693 	      {
45694 		tree d = TREE_VALUE (a);
45695 		gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
45696 		cp_token *first = DEFPARSE_TOKENS (d)->first;
45697 		cp_token *last = DEFPARSE_TOKENS (d)->last;
45698 		const char *directive[3] = {};
45699 		for (int j = 0; j < 3; j++)
45700 		  {
45701 		    tree id = NULL_TREE;
45702 		    if (first + j == last)
45703 		      break;
45704 		    if (first[j].type == CPP_NAME)
45705 		      id = first[j].u.value;
45706 		    else if (first[j].type == CPP_KEYWORD)
45707 		      id = ridpointers[(int) first[j].keyword];
45708 		    else
45709 		      break;
45710 		    directive[j] = IDENTIFIER_POINTER (id);
45711 		  }
45712 		const c_omp_directive *dir = NULL;
45713 		if (directive[0])
45714 		  dir = c_omp_categorize_directive (directive[0], directive[1],
45715 						    directive[2]);
45716 		if (dir == NULL)
45717 		  {
45718 		    error_at (first->location,
45719 			      "unknown OpenMP directive name in "
45720 			      "%<omp::directive%> attribute argument");
45721 		    continue;
45722 		  }
45723 		if (dir->id != PRAGMA_OMP_DECLARE
45724 		    || (strcmp (directive[1], "simd") != 0
45725 			&& strcmp (directive[1], "variant") != 0))
45726 		  {
45727 		    error_at (first->location,
45728 			      "OpenMP directive other than %<declare simd%> "
45729 			      "or %<declare variant%> appertains to a "
45730 			      "declaration");
45731 		    continue;
45732 		  }
45733 
45734 		if (parser->omp_attrs_forbidden_p)
45735 		  {
45736 		    error_at (first->location,
45737 			      "mixing OpenMP directives with attribute and "
45738 			      "pragma syntax on the same statement");
45739 		    parser->omp_attrs_forbidden_p = false;
45740 		  }
45741 
45742 		if (!flag_openmp && strcmp (directive[1], "simd") != 0)
45743 		  continue;
45744 		if (lexer == NULL)
45745 		  {
45746 		    lexer = cp_lexer_alloc ();
45747 		    lexer->debugging_p = parser->lexer->debugging_p;
45748 		  }
45749 		vec_safe_reserve (lexer->buffer, (last - first) + 2);
45750 		cp_token tok = {};
45751 		tok.type = CPP_PRAGMA;
45752 		tok.keyword = RID_MAX;
45753 		tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
45754 		tok.location = first->location;
45755 		lexer->buffer->quick_push (tok);
45756 		while (++first < last)
45757 		  lexer->buffer->quick_push (*first);
45758 		tok = {};
45759 		tok.type = CPP_PRAGMA_EOL;
45760 		tok.keyword = RID_MAX;
45761 		tok.location = last->location;
45762 		lexer->buffer->quick_push (tok);
45763 		tok = {};
45764 		tok.type = CPP_EOF;
45765 		tok.keyword = RID_MAX;
45766 		tok.location = last->location;
45767 		lexer->buffer->quick_push (tok);
45768 		lexer->next = parser->lexer;
45769 		lexer->next_token = lexer->buffer->address ();
45770 		lexer->last_token = lexer->next_token
45771 				    + lexer->buffer->length ()
45772 		      - 1;
45773 		lexer->in_omp_attribute_pragma = true;
45774 		parser->lexer = lexer;
45775 		/* Move the current source position to that of the first token
45776 		   in the new lexer.  */
45777 		cp_lexer_set_source_position_from_token (lexer->next_token);
45778 
45779 		cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
45780 		tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45781 		const char *kind = IDENTIFIER_POINTER (id);
45782 		cp_lexer_consume_token (parser->lexer);
45783 
45784 		tree c, cl;
45785 		if (strcmp (kind, "simd") == 0)
45786 		  {
45787 		    if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45788 			&& cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45789 		      cp_lexer_consume_token (parser->lexer);
45790 
45791 		    omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
45792 		    cl = cp_parser_omp_all_clauses (parser, mask,
45793 						    "#pragma omp declare simd",
45794 						    pragma_tok);
45795 		    if (cl)
45796 		      cl = tree_cons (NULL_TREE, cl, NULL_TREE);
45797 		    c = build_tree_list (get_identifier ("omp declare simd"),
45798 					 cl);
45799 		    TREE_CHAIN (c) = attrs;
45800 		    if (processing_template_decl)
45801 		      ATTR_IS_DEPENDENT (c) = 1;
45802 		    attrs = c;
45803 		  }
45804 		else
45805 		  {
45806 		    gcc_assert (strcmp (kind, "variant") == 0);
45807 		    attrs
45808 		      = cp_finish_omp_declare_variant (parser, pragma_tok,
45809 						       attrs);
45810 		  }
45811 		gcc_assert (parser->lexer != lexer);
45812 		vec_safe_truncate (lexer->buffer, 0);
45813 	      }
45814 	    *pa = TREE_CHAIN (*pa);
45815 	  }
45816 	else
45817 	  pa = &TREE_CHAIN (*pa);
45818     }
45819   if (lexer)
45820     cp_lexer_destroy (lexer);
45821 
45822   data->fndecl_seen = true;
45823   return attrs;
45824 }
45825 
45826 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
45827    on #pragma omp declare target.  Return false if errors were reported.  */
45828 
45829 static bool
handle_omp_declare_target_clause(tree c,tree t,int device_type)45830 handle_omp_declare_target_clause (tree c, tree t, int device_type)
45831 {
45832   tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
45833   tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
45834   tree id;
45835   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
45836     {
45837       id = get_identifier ("omp declare target link");
45838       std::swap (at1, at2);
45839     }
45840   else
45841     id = get_identifier ("omp declare target");
45842   if (at2)
45843     {
45844       error_at (OMP_CLAUSE_LOCATION (c),
45845 		"%qD specified both in declare target %<link%> and %<to%>"
45846 		" clauses", t);
45847       return false;
45848     }
45849   if (!at1)
45850     {
45851       DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45852       if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
45853 	return true;
45854 
45855       symtab_node *node = symtab_node::get (t);
45856       if (node != NULL)
45857 	{
45858 	  node->offloadable = 1;
45859 	  if (ENABLE_OFFLOADING)
45860 	    {
45861 	      g->have_offload = true;
45862 	      if (is_a <varpool_node *> (node))
45863 		vec_safe_push (offload_vars, t);
45864 	    }
45865 	}
45866     }
45867   if (TREE_CODE (t) != FUNCTION_DECL)
45868     return true;
45869   if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
45870     {
45871       tree at3 = lookup_attribute ("omp declare target host",
45872 				   DECL_ATTRIBUTES (t));
45873       if (at3 == NULL_TREE)
45874 	{
45875 	  id = get_identifier ("omp declare target host");
45876 	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45877 	}
45878     }
45879   if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
45880     {
45881       tree at3 = lookup_attribute ("omp declare target nohost",
45882 				   DECL_ATTRIBUTES (t));
45883       if (at3 == NULL_TREE)
45884 	{
45885 	  id = get_identifier ("omp declare target nohost");
45886 	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
45887 	}
45888     }
45889   return true;
45890 }
45891 
45892 /* OpenMP 4.0:
45893    # pragma omp declare target new-line
45894    declarations and definitions
45895    # pragma omp end declare target new-line
45896 
45897    OpenMP 4.5:
45898    # pragma omp declare target ( extended-list ) new-line
45899 
45900    # pragma omp declare target declare-target-clauses[seq] new-line  */
45901 
45902 #define OMP_DECLARE_TARGET_CLAUSE_MASK				\
45903 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
45904 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)		\
45905 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
45906 
45907 static void
cp_parser_omp_declare_target(cp_parser * parser,cp_token * pragma_tok)45908 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
45909 {
45910   tree clauses = NULL_TREE;
45911   int device_type = 0;
45912   bool only_device_type = true;
45913   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45914       /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
45915       || (parser->lexer->in_omp_attribute_pragma
45916 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45917 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
45918     clauses
45919       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
45920 				   "#pragma omp declare target", pragma_tok);
45921   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45922     {
45923       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
45924 					clauses);
45925       clauses = finish_omp_clauses (clauses, C_ORT_OMP);
45926       cp_parser_require_pragma_eol (parser, pragma_tok);
45927     }
45928   else
45929     {
45930       struct omp_declare_target_attr a
45931 	= { parser->lexer->in_omp_attribute_pragma };
45932       vec_safe_push (scope_chain->omp_declare_target_attribute, a);
45933       cp_parser_require_pragma_eol (parser, pragma_tok);
45934       return;
45935     }
45936   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45937     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45938       device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
45939   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45940     {
45941       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
45942 	continue;
45943       tree t = OMP_CLAUSE_DECL (c);
45944       only_device_type = false;
45945       if (!handle_omp_declare_target_clause (c, t, device_type))
45946 	continue;
45947       if (VAR_OR_FUNCTION_DECL_P (t)
45948 	  && DECL_LOCAL_DECL_P (t)
45949 	  && DECL_LANG_SPECIFIC (t)
45950 	  && DECL_LOCAL_DECL_ALIAS (t)
45951 	  && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
45952 	handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
45953 					  device_type);
45954     }
45955   if (device_type && only_device_type)
45956     warning_at (OMP_CLAUSE_LOCATION (clauses), 0,
45957 		"directive with only %<device_type%> clauses ignored");
45958 }
45959 
45960 static void
cp_parser_omp_end_declare_target(cp_parser * parser,cp_token * pragma_tok)45961 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
45962 {
45963   const char *p = "";
45964   bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
45965   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45966     {
45967       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45968       p = IDENTIFIER_POINTER (id);
45969     }
45970   if (strcmp (p, "declare") == 0)
45971     {
45972       cp_lexer_consume_token (parser->lexer);
45973       p = "";
45974       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45975 	{
45976 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45977 	  p = IDENTIFIER_POINTER (id);
45978 	}
45979       if (strcmp (p, "target") == 0)
45980 	cp_lexer_consume_token (parser->lexer);
45981       else
45982 	{
45983 	  cp_parser_error (parser, "expected %<target%>");
45984 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45985 	  return;
45986 	}
45987     }
45988   else
45989     {
45990       cp_parser_error (parser, "expected %<declare%>");
45991       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45992       return;
45993     }
45994   cp_parser_require_pragma_eol (parser, pragma_tok);
45995   if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
45996     error_at (pragma_tok->location,
45997 	      "%<#pragma omp end declare target%> without corresponding "
45998 	      "%<#pragma omp declare target%>");
45999   else
46000     {
46001       omp_declare_target_attr
46002 	a = scope_chain->omp_declare_target_attribute->pop ();
46003       if (a.attr_syntax != in_omp_attribute_pragma)
46004 	{
46005 	  if (a.attr_syntax)
46006 	    error_at (pragma_tok->location,
46007 		      "%<declare target%> in attribute syntax terminated "
46008 		      "with %<end declare target%> in pragma syntax");
46009 	  else
46010 	    error_at (pragma_tok->location,
46011 		      "%<declare target%> in pragma syntax terminated "
46012 		      "with %<end declare target%> in attribute syntax");
46013 	}
46014     }
46015 }
46016 
46017 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
46018    expression and optional initializer clause of
46019    #pragma omp declare reduction.  We store the expression(s) as
46020    either 3, 6 or 7 special statements inside of the artificial function's
46021    body.  The first two statements are DECL_EXPRs for the artificial
46022    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
46023    expression that uses those variables.
46024    If there was any INITIALIZER clause, this is followed by further statements,
46025    the fourth and fifth statements are DECL_EXPRs for the artificial
46026    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
46027    constructor variant (first token after open paren is not omp_priv),
46028    then the sixth statement is a statement with the function call expression
46029    that uses the OMP_PRIV and optionally OMP_ORIG variable.
46030    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
46031    to initialize the OMP_PRIV artificial variable and there is seventh
46032    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
46033 
46034 static bool
cp_parser_omp_declare_reduction_exprs(tree fndecl,cp_parser * parser)46035 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
46036 {
46037   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
46038   gcc_assert (TYPE_REF_P (type));
46039   type = TREE_TYPE (type);
46040   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
46041   DECL_ARTIFICIAL (omp_out) = 1;
46042   pushdecl (omp_out);
46043   add_decl_expr (omp_out);
46044   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
46045   DECL_ARTIFICIAL (omp_in) = 1;
46046   pushdecl (omp_in);
46047   add_decl_expr (omp_in);
46048   tree combiner;
46049   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
46050 
46051   keep_next_level (true);
46052   tree block = begin_omp_structured_block ();
46053   combiner = cp_parser_expression (parser);
46054   finish_expr_stmt (combiner);
46055   block = finish_omp_structured_block (block);
46056   if (processing_template_decl)
46057     block = build_stmt (input_location, EXPR_STMT, block);
46058   add_stmt (block);
46059 
46060   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
46061     return false;
46062 
46063   /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
46064   if (parser->lexer->in_omp_attribute_pragma
46065       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46066       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46067     cp_lexer_consume_token (parser->lexer);
46068 
46069   const char *p = "";
46070   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46071     {
46072       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46073       p = IDENTIFIER_POINTER (id);
46074     }
46075 
46076   if (strcmp (p, "initializer") == 0)
46077     {
46078       cp_lexer_consume_token (parser->lexer);
46079       matching_parens parens;
46080       if (!parens.require_open (parser))
46081 	return false;
46082 
46083       p = "";
46084       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46085 	{
46086 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46087 	  p = IDENTIFIER_POINTER (id);
46088 	}
46089 
46090       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
46091       DECL_ARTIFICIAL (omp_priv) = 1;
46092       pushdecl (omp_priv);
46093       add_decl_expr (omp_priv);
46094       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
46095       DECL_ARTIFICIAL (omp_orig) = 1;
46096       pushdecl (omp_orig);
46097       add_decl_expr (omp_orig);
46098 
46099       keep_next_level (true);
46100       block = begin_omp_structured_block ();
46101 
46102       bool ctor = false;
46103       if (strcmp (p, "omp_priv") == 0)
46104 	{
46105 	  bool is_direct_init, is_non_constant_init;
46106 	  ctor = true;
46107 	  cp_lexer_consume_token (parser->lexer);
46108 	  /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
46109 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
46110 	      || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46111 		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
46112 		     == CPP_CLOSE_PAREN
46113 		  && cp_lexer_peek_nth_token (parser->lexer, 3)->type
46114 		     == CPP_CLOSE_PAREN))
46115 	    {
46116 	      finish_omp_structured_block (block);
46117 	      error ("invalid initializer clause");
46118 	      return false;
46119 	    }
46120 	  initializer = cp_parser_initializer (parser, &is_direct_init,
46121 					       &is_non_constant_init);
46122 	  cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
46123 			  NULL_TREE, LOOKUP_ONLYCONVERTING);
46124 	}
46125       else
46126 	{
46127 	  cp_parser_parse_tentatively (parser);
46128 	  /* Don't create location wrapper nodes here.  */
46129 	  auto_suppress_location_wrappers sentinel;
46130 	  tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
46131 						  /*check_dependency_p=*/true,
46132 						  /*template_p=*/NULL,
46133 						  /*declarator_p=*/false,
46134 						  /*optional_p=*/false);
46135 	  vec<tree, va_gc> *args;
46136 	  if (fn_name == error_mark_node
46137 	      || cp_parser_error_occurred (parser)
46138 	      || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46139 	      || ((args = cp_parser_parenthesized_expression_list
46140 				(parser, non_attr, /*cast_p=*/false,
46141 				 /*allow_expansion_p=*/true,
46142 				 /*non_constant_p=*/NULL)),
46143 		  cp_parser_error_occurred (parser)))
46144 	    {
46145 	      finish_omp_structured_block (block);
46146 	      cp_parser_abort_tentative_parse (parser);
46147 	      cp_parser_error (parser, "expected id-expression (arguments)");
46148 	      return false;
46149 	    }
46150 	  unsigned int i;
46151 	  tree arg;
46152 	  FOR_EACH_VEC_SAFE_ELT (args, i, arg)
46153 	    if (arg == omp_priv
46154 		|| (TREE_CODE (arg) == ADDR_EXPR
46155 		    && TREE_OPERAND (arg, 0) == omp_priv))
46156 	      break;
46157 	  cp_parser_abort_tentative_parse (parser);
46158 	  if (arg == NULL_TREE)
46159 	    error ("one of the initializer call arguments should be %<omp_priv%>"
46160 		   " or %<&omp_priv%>");
46161 	  initializer = cp_parser_postfix_expression (parser, false, false, false,
46162 						      false, NULL);
46163 	  finish_expr_stmt (initializer);
46164 	}
46165 
46166       block = finish_omp_structured_block (block);
46167       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
46168       if (processing_template_decl)
46169 	block = build_stmt (input_location, EXPR_STMT, block);
46170       add_stmt (block);
46171 
46172       if (ctor)
46173 	add_decl_expr (omp_orig);
46174 
46175       if (!parens.require_close (parser))
46176 	return false;
46177     }
46178 
46179   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46180     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
46181                               UNKNOWN_LOCATION);
46182 
46183   return true;
46184 }
46185 
46186 /* OpenMP 4.0
46187    #pragma omp declare reduction (reduction-id : typename-list : expression) \
46188       initializer-clause[opt] new-line
46189 
46190    initializer-clause:
46191       initializer (omp_priv initializer)
46192       initializer (function-name (argument-list))  */
46193 
46194 static void
cp_parser_omp_declare_reduction(cp_parser * parser,cp_token * pragma_tok,enum pragma_context)46195 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
46196 				 enum pragma_context)
46197 {
46198   auto_vec<tree> types;
46199   enum tree_code reduc_code = ERROR_MARK;
46200   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
46201   unsigned int i;
46202   cp_token *first_token;
46203   cp_token_cache *cp;
46204   int errs;
46205   void *p;
46206 
46207   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
46208   p = obstack_alloc (&declarator_obstack, 0);
46209 
46210   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
46211     goto fail;
46212 
46213   switch (cp_lexer_peek_token (parser->lexer)->type)
46214     {
46215     case CPP_PLUS:
46216       reduc_code = PLUS_EXPR;
46217       break;
46218     case CPP_MULT:
46219       reduc_code = MULT_EXPR;
46220       break;
46221     case CPP_MINUS:
46222       reduc_code = MINUS_EXPR;
46223       break;
46224     case CPP_AND:
46225       reduc_code = BIT_AND_EXPR;
46226       break;
46227     case CPP_XOR:
46228       reduc_code = BIT_XOR_EXPR;
46229       break;
46230     case CPP_OR:
46231       reduc_code = BIT_IOR_EXPR;
46232       break;
46233     case CPP_AND_AND:
46234       reduc_code = TRUTH_ANDIF_EXPR;
46235       break;
46236     case CPP_OR_OR:
46237       reduc_code = TRUTH_ORIF_EXPR;
46238       break;
46239     case CPP_NAME:
46240       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
46241       break;
46242     default:
46243       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46244 			       "%<|%>, %<&&%>, %<||%> or identifier");
46245       goto fail;
46246     }
46247 
46248   if (reduc_code != ERROR_MARK)
46249     cp_lexer_consume_token (parser->lexer);
46250 
46251   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
46252   if (reduc_id == error_mark_node)
46253     goto fail;
46254 
46255   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
46256     goto fail;
46257 
46258   /* Types may not be defined in declare reduction type list.  */
46259   const char *saved_message;
46260   saved_message = parser->type_definition_forbidden_message;
46261   parser->type_definition_forbidden_message
46262     = G_("types may not be defined in declare reduction type list");
46263   bool saved_colon_corrects_to_scope_p;
46264   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
46265   parser->colon_corrects_to_scope_p = false;
46266   bool saved_colon_doesnt_start_class_def_p;
46267   saved_colon_doesnt_start_class_def_p
46268     = parser->colon_doesnt_start_class_def_p;
46269   parser->colon_doesnt_start_class_def_p = true;
46270 
46271   while (true)
46272     {
46273       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46274       type = cp_parser_type_id (parser);
46275       if (type == error_mark_node)
46276 	;
46277       else if (ARITHMETIC_TYPE_P (type)
46278 	       && (orig_reduc_id == NULL_TREE
46279 		   || (TREE_CODE (type) != COMPLEX_TYPE
46280 		       && (id_equal (orig_reduc_id, "min")
46281 			   || id_equal (orig_reduc_id, "max")))))
46282 	error_at (loc, "predeclared arithmetic type %qT in "
46283 		       "%<#pragma omp declare reduction%>", type);
46284       else if (FUNC_OR_METHOD_TYPE_P (type)
46285 	       || TREE_CODE (type) == ARRAY_TYPE)
46286 	error_at (loc, "function or array type %qT in "
46287 		       "%<#pragma omp declare reduction%>", type);
46288       else if (TYPE_REF_P (type))
46289 	error_at (loc, "reference type %qT in "
46290 		       "%<#pragma omp declare reduction%>", type);
46291       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
46292 	error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
46293 		  "type %qT in %<#pragma omp declare reduction%>", type);
46294       else
46295 	types.safe_push (type);
46296 
46297       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46298 	cp_lexer_consume_token (parser->lexer);
46299       else
46300 	break;
46301     }
46302 
46303   /* Restore the saved message.  */
46304   parser->type_definition_forbidden_message = saved_message;
46305   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
46306   parser->colon_doesnt_start_class_def_p
46307     = saved_colon_doesnt_start_class_def_p;
46308 
46309   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
46310       || types.is_empty ())
46311     {
46312      fail:
46313       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46314       goto done;
46315     }
46316 
46317   first_token = cp_lexer_peek_token (parser->lexer);
46318   cp = NULL;
46319   errs = errorcount;
46320   FOR_EACH_VEC_ELT (types, i, type)
46321     {
46322       tree fntype
46323 	= build_function_type_list (void_type_node,
46324 				    cp_build_reference_type (type, false),
46325 				    NULL_TREE);
46326       tree this_reduc_id = reduc_id;
46327       if (!dependent_type_p (type))
46328 	this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
46329       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
46330       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
46331       DECL_ARTIFICIAL (fndecl) = 1;
46332       DECL_EXTERNAL (fndecl) = 1;
46333       DECL_DECLARED_INLINE_P (fndecl) = 1;
46334       DECL_IGNORED_P (fndecl) = 1;
46335       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
46336       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
46337       DECL_ATTRIBUTES (fndecl)
46338 	= tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
46339 		     DECL_ATTRIBUTES (fndecl));
46340       bool block_scope = false;
46341       if (current_function_decl)
46342 	{
46343 	  block_scope = true;
46344 	  DECL_CONTEXT (fndecl) = current_function_decl;
46345 	  DECL_LOCAL_DECL_P (fndecl) = true;
46346 	}
46347 
46348       if (processing_template_decl)
46349 	fndecl = push_template_decl (fndecl);
46350 
46351       if (block_scope)
46352 	{
46353 	  if (!processing_template_decl)
46354 	    pushdecl (fndecl);
46355 	}
46356       else if (current_class_type)
46357 	{
46358 	  if (cp == NULL)
46359 	    {
46360 	      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46361 		cp_lexer_consume_token (parser->lexer);
46362 	      cp = cp_token_cache_new (first_token,
46363 				       cp_lexer_peek_nth_token (parser->lexer,
46364 								2));
46365 	    }
46366 	  DECL_STATIC_FUNCTION_P (fndecl) = 1;
46367 	  finish_member_declaration (fndecl);
46368 	  DECL_PENDING_INLINE_INFO (fndecl) = cp;
46369 	  DECL_PENDING_INLINE_P (fndecl) = 1;
46370 	  vec_safe_push (unparsed_funs_with_definitions, fndecl);
46371 	  continue;
46372 	}
46373       else
46374 	{
46375 	  DECL_CONTEXT (fndecl) = current_namespace;
46376 	  tree d = pushdecl (fndecl);
46377 	  /* We should never meet a matched duplicate decl.  */
46378 	  gcc_checking_assert (d == error_mark_node || d == fndecl);
46379 	}
46380 
46381       tree block = NULL_TREE;
46382       if (!block_scope)
46383 	start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
46384       else
46385 	block = begin_omp_structured_block ();
46386       if (cp)
46387 	{
46388 	  cp_parser_push_lexer_for_tokens (parser, cp);
46389 	  parser->lexer->in_pragma = true;
46390 	}
46391 
46392       bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
46393 
46394       if (cp)
46395 	cp_parser_pop_lexer (parser);
46396       if (!block_scope)
46397 	finish_function (/*inline_p=*/false);
46398       else
46399 	{
46400 	  DECL_CONTEXT (fndecl) = current_function_decl;
46401 	  if (DECL_TEMPLATE_INFO (fndecl))
46402 	    DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
46403 	}
46404       if (!ok)
46405 	goto fail;
46406 
46407       if (block_scope)
46408 	{
46409 	  block = finish_omp_structured_block (block);
46410 	  if (TREE_CODE (block) == BIND_EXPR)
46411 	    DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
46412 	  else if (TREE_CODE (block) == STATEMENT_LIST)
46413 	    DECL_SAVED_TREE (fndecl) = block;
46414 	  if (processing_template_decl)
46415 	    add_decl_expr (fndecl);
46416 	}
46417 
46418       cp_check_omp_declare_reduction (fndecl);
46419       if (cp == NULL && types.length () > 1)
46420 	cp = cp_token_cache_new (first_token,
46421 				 cp_lexer_peek_nth_token (parser->lexer, 2));
46422       if (errs != errorcount)
46423 	break;
46424     }
46425 
46426   cp_parser_require_pragma_eol (parser, pragma_tok);
46427 
46428  done:
46429   /* Free any declarators allocated.  */
46430   obstack_free (&declarator_obstack, p);
46431 }
46432 
46433 /* OpenMP 4.0
46434    #pragma omp declare simd declare-simd-clauses[optseq] new-line
46435    #pragma omp declare reduction (reduction-id : typename-list : expression) \
46436       initializer-clause[opt] new-line
46437    #pragma omp declare target new-line
46438 
46439    OpenMP 5.0
46440    #pragma omp declare variant (identifier) match (context-selector)  */
46441 
46442 static bool
cp_parser_omp_declare(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)46443 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
46444 		       enum pragma_context context)
46445 {
46446   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46447     {
46448       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46449       const char *p = IDENTIFIER_POINTER (id);
46450 
46451       if (strcmp (p, "simd") == 0)
46452 	{
46453 	  cp_lexer_consume_token (parser->lexer);
46454 	  cp_parser_omp_declare_simd (parser, pragma_tok,
46455 				      context, false);
46456 	  return true;
46457 	}
46458       if (flag_openmp && strcmp (p, "variant") == 0)
46459 	{
46460 	  cp_lexer_consume_token (parser->lexer);
46461 	  cp_parser_omp_declare_simd (parser, pragma_tok,
46462 				      context, true);
46463 	  return true;
46464 	}
46465       cp_ensure_no_omp_declare_simd (parser);
46466       if (strcmp (p, "reduction") == 0)
46467 	{
46468 	  cp_lexer_consume_token (parser->lexer);
46469 	  cp_parser_omp_declare_reduction (parser, pragma_tok,
46470 					   context);
46471 	  return false;
46472 	}
46473       if (!flag_openmp)  /* flag_openmp_simd  */
46474 	{
46475 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46476 	  return false;
46477 	}
46478       if (strcmp (p, "target") == 0)
46479 	{
46480 	  cp_lexer_consume_token (parser->lexer);
46481 	  cp_parser_omp_declare_target (parser, pragma_tok);
46482 	  return false;
46483 	}
46484     }
46485   cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
46486 			   "%<target%> or %<variant%>");
46487   cp_parser_require_pragma_eol (parser, pragma_tok);
46488   return false;
46489 }
46490 
46491 /* OpenMP 5.0
46492    #pragma omp requires clauses[optseq] new-line  */
46493 
46494 static bool
cp_parser_omp_requires(cp_parser * parser,cp_token * pragma_tok)46495 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
46496 {
46497   bool first = true;
46498   enum omp_requires new_req = (enum omp_requires) 0;
46499 
46500   location_t loc = pragma_tok->location;
46501   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46502     {
46503       /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
46504       if ((!first || parser->lexer->in_omp_attribute_pragma)
46505 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46506 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46507 	cp_lexer_consume_token (parser->lexer);
46508 
46509       first = false;
46510 
46511       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46512 	{
46513 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46514 	  const char *p = IDENTIFIER_POINTER (id);
46515 	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46516 	  enum omp_requires this_req = (enum omp_requires) 0;
46517 
46518 	  if (!strcmp (p, "unified_address"))
46519 	    this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
46520 	  else if (!strcmp (p, "unified_shared_memory"))
46521 	    this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
46522 	  else if (!strcmp (p, "dynamic_allocators"))
46523 	    this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
46524 	  else if (!strcmp (p, "reverse_offload"))
46525 	    this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
46526 	  else if (!strcmp (p, "atomic_default_mem_order"))
46527 	    {
46528 	      cp_lexer_consume_token (parser->lexer);
46529 
46530 	      matching_parens parens;
46531 	      if (parens.require_open (parser))
46532 		{
46533 		  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46534 		    {
46535 		      id = cp_lexer_peek_token (parser->lexer)->u.value;
46536 		      p = IDENTIFIER_POINTER (id);
46537 
46538 		      if (!strcmp (p, "seq_cst"))
46539 			this_req
46540 			  = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
46541 		      else if (!strcmp (p, "relaxed"))
46542 			this_req
46543 			  = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
46544 		      else if (!strcmp (p, "acq_rel"))
46545 			this_req
46546 			  = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
46547 		    }
46548 		  if (this_req == 0)
46549 		    {
46550 		      error_at (cp_lexer_peek_token (parser->lexer)->location,
46551 				"expected %<seq_cst%>, %<relaxed%> or "
46552 				"%<acq_rel%>");
46553 		      switch (cp_lexer_peek_token (parser->lexer)->type)
46554 			{
46555 			case CPP_EOF:
46556 			case CPP_PRAGMA_EOL:
46557 			case CPP_CLOSE_PAREN:
46558 			  break;
46559 			default:
46560 			  if (cp_lexer_nth_token_is (parser->lexer, 2,
46561 						     CPP_CLOSE_PAREN))
46562 			    cp_lexer_consume_token (parser->lexer);
46563 			  break;
46564 			}
46565 		    }
46566 		  else
46567 		    cp_lexer_consume_token (parser->lexer);
46568 
46569 		  if (!parens.require_close (parser))
46570 		    cp_parser_skip_to_closing_parenthesis (parser,
46571 							   /*recovering=*/true,
46572 							   /*or_comma=*/false,
46573 							   /*consume_paren=*/
46574 							   true);
46575 
46576 		  if (this_req == 0)
46577 		    {
46578 		      cp_parser_require_pragma_eol (parser, pragma_tok);
46579 		      return false;
46580 		    }
46581 		}
46582 	      p = NULL;
46583 	    }
46584 	  else
46585 	    {
46586 	      error_at (cloc, "expected %<unified_address%>, "
46587 			      "%<unified_shared_memory%>, "
46588 			      "%<dynamic_allocators%>, "
46589 			       "%<reverse_offload%> "
46590 			       "or %<atomic_default_mem_order%> clause");
46591 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46592 	      return false;
46593 	    }
46594 	  if (p && this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS)
46595 	    sorry_at (cloc, "%qs clause on %<requires%> directive not "
46596 			    "supported yet", p);
46597 	  if (p)
46598 	    cp_lexer_consume_token (parser->lexer);
46599 	  if (this_req)
46600 	    {
46601 	      if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46602 		{
46603 		  if ((this_req & new_req) != 0)
46604 		    error_at (cloc, "too many %qs clauses", p);
46605 		  if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
46606 		      && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
46607 		    error_at (cloc, "%qs clause used lexically after first "
46608 				    "target construct or offloading API", p);
46609 		}
46610 	      else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46611 		{
46612 		  error_at (cloc, "too many %qs clauses",
46613 			    "atomic_default_mem_order");
46614 		  this_req = (enum omp_requires) 0;
46615 		}
46616 	      else if ((omp_requires_mask
46617 			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
46618 		{
46619 		  error_at (cloc, "more than one %<atomic_default_mem_order%>"
46620 				  " clause in a single compilation unit");
46621 		  this_req
46622 		    = (enum omp_requires)
46623 		       (omp_requires_mask
46624 			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
46625 		}
46626 	      else if ((omp_requires_mask
46627 			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
46628 		error_at (cloc, "%<atomic_default_mem_order%> clause used "
46629 				"lexically after first %<atomic%> construct "
46630 				"without memory order clause");
46631 	      new_req = (enum omp_requires) (new_req | this_req);
46632 	      omp_requires_mask
46633 		= (enum omp_requires) (omp_requires_mask | this_req);
46634 	      continue;
46635 	    }
46636 	}
46637       break;
46638     }
46639   cp_parser_require_pragma_eol (parser, pragma_tok);
46640 
46641   if (new_req == 0)
46642     error_at (loc, "%<pragma omp requires%> requires at least one clause");
46643   return false;
46644 }
46645 
46646 
46647 /* OpenMP 5.1:
46648    #pragma omp nothing new-line  */
46649 
46650 static void
cp_parser_omp_nothing(cp_parser * parser,cp_token * pragma_tok)46651 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
46652 {
46653   cp_parser_require_pragma_eol (parser, pragma_tok);
46654 }
46655 
46656 
46657 /* OpenMP 5.1
46658    #pragma omp error clauses[optseq] new-line  */
46659 
46660 static bool
cp_parser_omp_error(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)46661 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
46662 		     enum pragma_context context)
46663 {
46664   int at_compilation = -1;
46665   int severity_fatal = -1;
46666   tree message = NULL_TREE;
46667   bool first = true;
46668   bool bad = false;
46669   location_t loc = pragma_tok->location;
46670 
46671   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46672     {
46673       /* For now only in C++ attributes, do it always for OpenMP 5.1.  */
46674       if ((!first || parser->lexer->in_omp_attribute_pragma)
46675 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46676 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46677 	cp_lexer_consume_token (parser->lexer);
46678 
46679       first = false;
46680 
46681       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
46682 	break;
46683 
46684       const char *p
46685 	= IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46686       location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46687       static const char *args[] = {
46688 	"execution", "compilation", "warning", "fatal"
46689       };
46690       int *v = NULL;
46691       int idx = 0, n = -1;
46692       tree m = NULL_TREE;
46693 
46694       if (!strcmp (p, "at"))
46695 	v = &at_compilation;
46696       else if (!strcmp (p, "severity"))
46697 	{
46698 	  v = &severity_fatal;
46699 	  idx += 2;
46700 	}
46701       else if (strcmp (p, "message"))
46702 	{
46703 	  error_at (cloc,
46704 		    "expected %<at%>, %<severity%> or %<message%> clause");
46705 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46706 	  return false;
46707 	}
46708 
46709       cp_lexer_consume_token (parser->lexer);
46710 
46711       matching_parens parens;
46712       if (parens.require_open (parser))
46713 	{
46714 	  if (v == NULL)
46715 	    {
46716 	      m = cp_parser_assignment_expression (parser);
46717 	      if (type_dependent_expression_p (m))
46718 		m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
46719 	      else
46720 		m = perform_implicit_conversion_flags (const_string_type_node, m,
46721 						       tf_warning_or_error,
46722 						       LOOKUP_NORMAL);
46723 	    }
46724 	  else
46725 	    {
46726 	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46727 		{
46728 		  tree val = cp_lexer_peek_token (parser->lexer)->u.value;
46729 		  const char *q = IDENTIFIER_POINTER (val);
46730 
46731 		  if (!strcmp (q, args[idx]))
46732 		    n = 0;
46733 		  else if (!strcmp (q, args[idx + 1]))
46734 		    n = 1;
46735 		}
46736 	      if (n == -1)
46737 		{
46738 		  error_at (cp_lexer_peek_token (parser->lexer)->location,
46739 			    "expected %qs or %qs", args[idx], args[idx + 1]);
46740 		  bad = true;
46741 		  switch (cp_lexer_peek_token (parser->lexer)->type)
46742 		    {
46743 		    case CPP_EOF:
46744 		    case CPP_PRAGMA_EOL:
46745 		    case CPP_CLOSE_PAREN:
46746 		      break;
46747 		    default:
46748 		      if (cp_lexer_nth_token_is (parser->lexer, 2,
46749 						 CPP_CLOSE_PAREN))
46750 			cp_lexer_consume_token (parser->lexer);
46751 		      break;
46752 		    }
46753 		}
46754 	      else
46755 		cp_lexer_consume_token (parser->lexer);
46756 	    }
46757 
46758 	  if (!parens.require_close (parser))
46759 	    cp_parser_skip_to_closing_parenthesis (parser,
46760 						   /*recovering=*/true,
46761 						   /*or_comma=*/false,
46762 						   /*consume_paren=*/
46763 						   true);
46764 
46765 	  if (v == NULL)
46766 	    {
46767 	      if (message)
46768 		{
46769 		  error_at (cloc, "too many %qs clauses", p);
46770 		  bad = true;
46771 		}
46772 	      else
46773 		message = m;
46774 	    }
46775 	  else if (n != -1)
46776 	    {
46777 	      if (*v != -1)
46778 		{
46779 		  error_at (cloc, "too many %qs clauses", p);
46780 		  bad = true;
46781 		}
46782 	      else
46783 		*v = n;
46784 	    }
46785 	}
46786       else
46787 	bad = true;
46788     }
46789   cp_parser_require_pragma_eol (parser, pragma_tok);
46790   if (bad)
46791     return true;
46792 
46793   if (at_compilation == -1)
46794     at_compilation = 1;
46795   if (severity_fatal == -1)
46796     severity_fatal = 1;
46797   if (!at_compilation)
46798     {
46799       if (context != pragma_compound)
46800 	{
46801 	  error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
46802 			 "may only be used in compound statements");
46803 	  return true;
46804 	}
46805       tree fndecl
46806 	= builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
46807 						: BUILT_IN_GOMP_WARNING);
46808       if (!message)
46809 	message = build_zero_cst (const_string_type_node);
46810       tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
46811 				       build_all_ones_cst (size_type_node));
46812       add_stmt (stmt);
46813       return true;
46814     }
46815 
46816   if (in_discarded_stmt)
46817     return false;
46818 
46819   const char *msg = NULL;
46820   if (message)
46821     {
46822       msg = c_getstr (fold_for_warn (message));
46823       if (msg == NULL)
46824 	msg = _("<message unknown at compile time>");
46825     }
46826   if (msg)
46827     emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46828 		     "%<pragma omp error%> encountered: %s", msg);
46829   else
46830     emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
46831 		     "%<pragma omp error%> encountered");
46832   return false;
46833 }
46834 
46835 /* OpenMP 4.5:
46836    #pragma omp taskloop taskloop-clause[optseq] new-line
46837      for-loop
46838 
46839    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
46840      for-loop  */
46841 
46842 #define OMP_TASKLOOP_CLAUSE_MASK				\
46843 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
46844 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
46845 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
46846 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
46847 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
46848 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)	\
46849 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)	\
46850 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
46851 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
46852 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
46853 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
46854 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
46855 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)	\
46856 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
46857 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE)	\
46858 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
46859 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
46860 
46861 static tree
cp_parser_omp_taskloop(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses,bool * if_p)46862 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
46863 			char *p_name, omp_clause_mask mask, tree *cclauses,
46864 			bool *if_p)
46865 {
46866   tree clauses, sb, ret;
46867   unsigned int save;
46868   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46869 
46870   strcat (p_name, " taskloop");
46871   mask |= OMP_TASKLOOP_CLAUSE_MASK;
46872   /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
46873      clause.  */
46874   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
46875     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
46876 
46877   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46878     {
46879       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46880       const char *p = IDENTIFIER_POINTER (id);
46881 
46882       if (strcmp (p, "simd") == 0)
46883 	{
46884 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46885 	  if (cclauses == NULL)
46886 	    cclauses = cclauses_buf;
46887 
46888 	  cp_lexer_consume_token (parser->lexer);
46889 	  if (!flag_openmp)  /* flag_openmp_simd  */
46890 	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46891 				       cclauses, if_p);
46892 	  sb = begin_omp_structured_block ();
46893 	  save = cp_parser_begin_omp_structured_block (parser);
46894 	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46895 				    cclauses, if_p);
46896 	  cp_parser_end_omp_structured_block (parser, save);
46897 	  tree body = finish_omp_structured_block (sb);
46898 	  if (ret == NULL)
46899 	    return ret;
46900 	  ret = make_node (OMP_TASKLOOP);
46901 	  TREE_TYPE (ret) = void_type_node;
46902 	  OMP_FOR_BODY (ret) = body;
46903 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46904 	  SET_EXPR_LOCATION (ret, loc);
46905 	  add_stmt (ret);
46906 	  return ret;
46907 	}
46908     }
46909   if (!flag_openmp)  /* flag_openmp_simd  */
46910     {
46911       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46912       return NULL_TREE;
46913     }
46914 
46915   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46916 				       cclauses == NULL);
46917   if (cclauses)
46918     {
46919       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
46920       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
46921     }
46922 
46923   keep_next_level (true);
46924   sb = begin_omp_structured_block ();
46925   save = cp_parser_begin_omp_structured_block (parser);
46926 
46927   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
46928 				if_p);
46929 
46930   cp_parser_end_omp_structured_block (parser, save);
46931   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
46932 
46933   return ret;
46934 }
46935 
46936 
46937 /* OpenACC 2.0:
46938    # pragma acc routine oacc-routine-clause[optseq] new-line
46939      function-definition
46940 
46941    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
46942 */
46943 
46944 #define OACC_ROUTINE_CLAUSE_MASK					\
46945 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
46946 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
46947 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
46948 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
46949 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
46950 
46951 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
46952    component, which must resolve to a declared namespace-scope
46953    function.  The clauses are either processed directly (for a named
46954    function), or defered until the immediatley following declaration
46955    is parsed.  */
46956 
46957 static void
cp_parser_oacc_routine(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)46958 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
46959 			enum pragma_context context)
46960 {
46961   gcc_checking_assert (context == pragma_external);
46962   /* The checking for "another pragma following this one" in the "no optional
46963      '( name )'" case makes sure that we dont re-enter.  */
46964   gcc_checking_assert (parser->oacc_routine == NULL);
46965 
46966   cp_oacc_routine_data data;
46967   data.error_seen = false;
46968   data.fndecl_seen = false;
46969   data.tokens = vNULL;
46970   data.clauses = NULL_TREE;
46971   data.loc = pragma_tok->location;
46972   /* It is safe to take the address of a local variable; it will only be
46973      used while this scope is live.  */
46974   parser->oacc_routine = &data;
46975 
46976   /* Look for optional '( name )'.  */
46977   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46978     {
46979       matching_parens parens;
46980       parens.consume_open (parser); /* '(' */
46981 
46982       /* We parse the name as an id-expression.  If it resolves to
46983 	 anything other than a non-overloaded function at namespace
46984 	 scope, it's an error.  */
46985       location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
46986       tree name = cp_parser_id_expression (parser,
46987 					   /*template_keyword_p=*/false,
46988 					   /*check_dependency_p=*/false,
46989 					   /*template_p=*/NULL,
46990 					   /*declarator_p=*/false,
46991 					   /*optional_p=*/false);
46992       tree decl = (identifier_p (name)
46993 		   ? cp_parser_lookup_name_simple (parser, name, name_loc)
46994 		   : name);
46995       if (name != error_mark_node && decl == error_mark_node)
46996 	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
46997 
46998       if (decl == error_mark_node
46999 	  || !parens.require_close (parser))
47000 	{
47001 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47002 	  parser->oacc_routine = NULL;
47003 	  return;
47004 	}
47005 
47006       data.clauses
47007 	= cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47008 				      "#pragma acc routine",
47009 				      cp_lexer_peek_token (parser->lexer));
47010       /* The clauses are in reverse order; fix that to make later diagnostic
47011 	 emission easier.  */
47012       data.clauses = nreverse (data.clauses);
47013 
47014       if (decl && is_overloaded_fn (decl)
47015 	  && (TREE_CODE (decl) != FUNCTION_DECL
47016 	      || DECL_FUNCTION_TEMPLATE_P  (decl)))
47017 	{
47018 	  error_at (name_loc,
47019 		    "%<#pragma acc routine%> names a set of overloads");
47020 	  parser->oacc_routine = NULL;
47021 	  return;
47022 	}
47023 
47024       /* Perhaps we should use the same rule as declarations in different
47025 	 namespaces?  */
47026       if (!DECL_NAMESPACE_SCOPE_P (decl))
47027 	{
47028 	  error_at (name_loc,
47029 		    "%qD does not refer to a namespace scope function", decl);
47030 	  parser->oacc_routine = NULL;
47031 	  return;
47032 	}
47033 
47034       if (TREE_CODE (decl) != FUNCTION_DECL)
47035 	{
47036 	  error_at (name_loc, "%qD does not refer to a function", decl);
47037 	  parser->oacc_routine = NULL;
47038 	  return;
47039 	}
47040 
47041       cp_finalize_oacc_routine (parser, decl, false);
47042       parser->oacc_routine = NULL;
47043     }
47044   else /* No optional '( name )'.  */
47045     {
47046       /* Store away all pragma tokens.  */
47047       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47048 	cp_lexer_consume_token (parser->lexer);
47049       cp_parser_require_pragma_eol (parser, pragma_tok);
47050       struct cp_token_cache *cp
47051 	= cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47052       parser->oacc_routine->tokens.safe_push (cp);
47053 
47054       /* Emit a helpful diagnostic if there's another pragma following this
47055 	 one.  */
47056       if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47057 	{
47058 	  cp_ensure_no_oacc_routine (parser);
47059 	  data.tokens.release ();
47060 	  /* ..., and then just keep going.  */
47061 	  return;
47062 	}
47063 
47064       /* We only have to consider the pragma_external case here.  */
47065       cp_parser_declaration (parser, NULL_TREE);
47066       if (parser->oacc_routine
47067 	  && !parser->oacc_routine->fndecl_seen)
47068 	cp_ensure_no_oacc_routine (parser);
47069       else
47070 	parser->oacc_routine = NULL;
47071       data.tokens.release ();
47072     }
47073 }
47074 
47075 /* Finalize #pragma acc routine clauses after direct declarator has
47076    been parsed.  */
47077 
47078 static tree
cp_parser_late_parsing_oacc_routine(cp_parser * parser,tree attrs)47079 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
47080 {
47081   struct cp_token_cache *ce;
47082   cp_oacc_routine_data *data = parser->oacc_routine;
47083 
47084   if (!data->error_seen && data->fndecl_seen)
47085     {
47086       error_at (data->loc,
47087 		"%<#pragma acc routine%> not immediately followed by "
47088 		"a single function declaration or definition");
47089       data->error_seen = true;
47090     }
47091   if (data->error_seen)
47092     return attrs;
47093 
47094   gcc_checking_assert (data->tokens.length () == 1);
47095   ce = data->tokens[0];
47096 
47097   cp_parser_push_lexer_for_tokens (parser, ce);
47098   parser->lexer->in_pragma = true;
47099   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47100 
47101   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47102   gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
47103   parser->oacc_routine->clauses
47104     = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47105 				  "#pragma acc routine", pragma_tok);
47106   /* The clauses are in reverse order; fix that to make later diagnostic
47107      emission easier.  */
47108   parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
47109   cp_parser_pop_lexer (parser);
47110   /* Later, cp_finalize_oacc_routine will process the clauses.  */
47111   parser->oacc_routine->fndecl_seen = true;
47112 
47113   return attrs;
47114 }
47115 
47116 /* Apply any saved OpenACC routine clauses to a just-parsed
47117    declaration.  */
47118 
47119 static void
cp_finalize_oacc_routine(cp_parser * parser,tree fndecl,bool is_defn)47120 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
47121 {
47122   if (__builtin_expect (parser->oacc_routine != NULL, 0))
47123     {
47124       /* Keep going if we're in error reporting mode.  */
47125       if (parser->oacc_routine->error_seen
47126 	  || fndecl == error_mark_node)
47127 	return;
47128 
47129       if (TREE_CODE (fndecl) != FUNCTION_DECL)
47130 	{
47131 	  if (parser->oacc_routine->fndecl_seen)
47132 	    {
47133 	      error_at (parser->oacc_routine->loc,
47134 			"%<#pragma acc routine%> not immediately followed by"
47135 			" a single function declaration or definition");
47136 	      parser->oacc_routine = NULL;
47137 	      return;
47138 	    }
47139 
47140 	  cp_ensure_no_oacc_routine (parser);
47141 	  return;
47142 	}
47143 
47144       int compatible
47145 	= oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
47146 				       parser->oacc_routine->loc,
47147 				       "#pragma acc routine");
47148       if (compatible < 0)
47149 	{
47150 	  parser->oacc_routine = NULL;
47151 	  return;
47152 	}
47153       if (compatible > 0)
47154 	{
47155 	}
47156       else
47157 	{
47158 	  if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
47159 	    {
47160 	      error_at (parser->oacc_routine->loc,
47161 			TREE_USED (fndecl)
47162 			? G_("%<#pragma acc routine%> must be applied before"
47163 			     " use")
47164 			: G_("%<#pragma acc routine%> must be applied before"
47165 			     " definition"));
47166 	      parser->oacc_routine = NULL;
47167 	      return;
47168 	    }
47169 
47170 	  /* Set the routine's level of parallelism.  */
47171 	  tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
47172 	  oacc_replace_fn_attrib (fndecl, dims);
47173 
47174 	  /* Add an "omp declare target" attribute.  */
47175 	  DECL_ATTRIBUTES (fndecl)
47176 	    = tree_cons (get_identifier ("omp declare target"),
47177 			 parser->oacc_routine->clauses,
47178 			 DECL_ATTRIBUTES (fndecl));
47179 	}
47180     }
47181 }
47182 
47183 /* Main entry point to OpenMP statement pragmas.  */
47184 
47185 static void
cp_parser_omp_construct(cp_parser * parser,cp_token * pragma_tok,bool * if_p)47186 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47187 {
47188   tree stmt;
47189   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
47190   omp_clause_mask mask (0);
47191 
47192   switch (cp_parser_pragma_kind (pragma_tok))
47193     {
47194     case PRAGMA_OACC_ATOMIC:
47195       cp_parser_omp_atomic (parser, pragma_tok, true);
47196       return;
47197     case PRAGMA_OACC_CACHE:
47198       stmt = cp_parser_oacc_cache (parser, pragma_tok);
47199       break;
47200     case PRAGMA_OACC_DATA:
47201       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
47202       break;
47203     case PRAGMA_OACC_ENTER_DATA:
47204       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
47205       break;
47206     case PRAGMA_OACC_EXIT_DATA:
47207       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
47208       break;
47209     case PRAGMA_OACC_HOST_DATA:
47210       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
47211       break;
47212     case PRAGMA_OACC_KERNELS:
47213     case PRAGMA_OACC_PARALLEL:
47214     case PRAGMA_OACC_SERIAL:
47215       strcpy (p_name, "#pragma acc");
47216       stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
47217       break;
47218     case PRAGMA_OACC_LOOP:
47219       strcpy (p_name, "#pragma acc");
47220       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
47221 				  if_p);
47222       break;
47223     case PRAGMA_OACC_UPDATE:
47224       stmt = cp_parser_oacc_update (parser, pragma_tok);
47225       break;
47226     case PRAGMA_OACC_WAIT:
47227       stmt = cp_parser_oacc_wait (parser, pragma_tok);
47228       break;
47229     case PRAGMA_OMP_ALLOCATE:
47230       cp_parser_omp_allocate (parser, pragma_tok);
47231       return;
47232     case PRAGMA_OMP_ATOMIC:
47233       cp_parser_omp_atomic (parser, pragma_tok, false);
47234       return;
47235     case PRAGMA_OMP_CRITICAL:
47236       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
47237       break;
47238     case PRAGMA_OMP_DISTRIBUTE:
47239       strcpy (p_name, "#pragma omp");
47240       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
47241 				       if_p);
47242       break;
47243     case PRAGMA_OMP_FOR:
47244       strcpy (p_name, "#pragma omp");
47245       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
47246 				if_p);
47247       break;
47248     case PRAGMA_OMP_LOOP:
47249       strcpy (p_name, "#pragma omp");
47250       stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
47251 				 if_p);
47252       break;
47253     case PRAGMA_OMP_MASKED:
47254       strcpy (p_name, "#pragma omp");
47255       stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
47256 				   if_p);
47257       break;
47258     case PRAGMA_OMP_MASTER:
47259       strcpy (p_name, "#pragma omp");
47260       stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
47261 				   if_p);
47262       break;
47263     case PRAGMA_OMP_PARALLEL:
47264       strcpy (p_name, "#pragma omp");
47265       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
47266 				     if_p);
47267       break;
47268     case PRAGMA_OMP_SCOPE:
47269       stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
47270       break;
47271     case PRAGMA_OMP_SECTIONS:
47272       strcpy (p_name, "#pragma omp");
47273       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
47274       break;
47275     case PRAGMA_OMP_SIMD:
47276       strcpy (p_name, "#pragma omp");
47277       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
47278 				 if_p);
47279       break;
47280     case PRAGMA_OMP_SINGLE:
47281       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
47282       break;
47283     case PRAGMA_OMP_TASK:
47284       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
47285       break;
47286     case PRAGMA_OMP_TASKGROUP:
47287       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
47288       break;
47289     case PRAGMA_OMP_TASKLOOP:
47290       strcpy (p_name, "#pragma omp");
47291       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
47292 				     if_p);
47293       break;
47294     case PRAGMA_OMP_TEAMS:
47295       strcpy (p_name, "#pragma omp");
47296       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
47297 				  if_p);
47298       break;
47299     default:
47300       gcc_unreachable ();
47301     }
47302 
47303   protected_set_expr_location (stmt, pragma_tok->location);
47304 }
47305 
47306 /* Transactional Memory parsing routines.  */
47307 
47308 /* Parse a transaction attribute.
47309 
47310    txn-attribute:
47311 	attribute
47312 	[ [ identifier ] ]
47313 
47314    We use this instead of cp_parser_attributes_opt for transactions to avoid
47315    the pedwarn in C++98 mode.  */
47316 
47317 static tree
cp_parser_txn_attribute_opt(cp_parser * parser)47318 cp_parser_txn_attribute_opt (cp_parser *parser)
47319 {
47320   cp_token *token;
47321   tree attr_name, attr = NULL;
47322 
47323   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
47324     return cp_parser_attributes_opt (parser);
47325 
47326   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
47327     return NULL_TREE;
47328   cp_lexer_consume_token (parser->lexer);
47329   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
47330     goto error1;
47331 
47332   token = cp_lexer_peek_token (parser->lexer);
47333   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
47334     {
47335       token = cp_lexer_consume_token (parser->lexer);
47336 
47337       attr_name = (token->type == CPP_KEYWORD
47338 		   /* For keywords, use the canonical spelling,
47339 		      not the parsed identifier.  */
47340 		   ? ridpointers[(int) token->keyword]
47341 		   : token->u.value);
47342       attr = build_tree_list (attr_name, NULL_TREE);
47343     }
47344   else
47345     cp_parser_error (parser, "expected identifier");
47346 
47347   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47348  error1:
47349   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
47350   return attr;
47351 }
47352 
47353 /* Parse a __transaction_atomic or __transaction_relaxed statement.
47354 
47355    transaction-statement:
47356      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
47357        compound-statement
47358      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
47359 */
47360 
47361 static tree
cp_parser_transaction(cp_parser * parser,cp_token * token)47362 cp_parser_transaction (cp_parser *parser, cp_token *token)
47363 {
47364   unsigned char old_in = parser->in_transaction;
47365   unsigned char this_in = 1, new_in;
47366   enum rid keyword = token->keyword;
47367   tree stmt, attrs, noex;
47368 
47369   cp_lexer_consume_token (parser->lexer);
47370 
47371   if (keyword == RID_TRANSACTION_RELAXED
47372       || keyword == RID_SYNCHRONIZED)
47373     this_in |= TM_STMT_ATTR_RELAXED;
47374   else
47375     {
47376       attrs = cp_parser_txn_attribute_opt (parser);
47377       if (attrs)
47378 	this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47379     }
47380 
47381   /* Parse a noexcept specification.  */
47382   if (keyword == RID_ATOMIC_NOEXCEPT)
47383     noex = boolean_true_node;
47384   else if (keyword == RID_ATOMIC_CANCEL)
47385     {
47386       /* cancel-and-throw is unimplemented.  */
47387       sorry ("%<atomic_cancel%>");
47388       noex = NULL_TREE;
47389     }
47390   else
47391     noex = cp_parser_noexcept_specification_opt (parser,
47392 						 CP_PARSER_FLAGS_NONE,
47393 						 /*require_constexpr=*/true,
47394 						 /*consumed_expr=*/NULL,
47395 						 /*return_cond=*/true);
47396 
47397   /* Keep track if we're in the lexical scope of an outer transaction.  */
47398   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
47399 
47400   stmt = begin_transaction_stmt (token->location, NULL, this_in);
47401 
47402   parser->in_transaction = new_in;
47403   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
47404   parser->in_transaction = old_in;
47405 
47406   finish_transaction_stmt (stmt, NULL, this_in, noex);
47407 
47408   return stmt;
47409 }
47410 
47411 /* Parse a __transaction_atomic or __transaction_relaxed expression.
47412 
47413    transaction-expression:
47414      __transaction_atomic txn-noexcept-spec[opt] ( expression )
47415      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
47416 */
47417 
47418 static tree
cp_parser_transaction_expression(cp_parser * parser,enum rid keyword)47419 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
47420 {
47421   unsigned char old_in = parser->in_transaction;
47422   unsigned char this_in = 1;
47423   cp_token *token;
47424   tree expr, noex;
47425   bool noex_expr;
47426   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47427 
47428   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47429       || keyword == RID_TRANSACTION_RELAXED);
47430 
47431   if (!flag_tm)
47432     error_at (loc,
47433 	      keyword == RID_TRANSACTION_RELAXED
47434 	      ? G_("%<__transaction_relaxed%> without transactional memory "
47435 		  "support enabled")
47436 	      : G_("%<__transaction_atomic%> without transactional memory "
47437 		   "support enabled"));
47438 
47439   token = cp_parser_require_keyword (parser, keyword,
47440       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47441 	  : RT_TRANSACTION_RELAXED));
47442   gcc_assert (token != NULL);
47443 
47444   if (keyword == RID_TRANSACTION_RELAXED)
47445     this_in |= TM_STMT_ATTR_RELAXED;
47446 
47447   /* Set this early.  This might mean that we allow transaction_cancel in
47448      an expression that we find out later actually has to be a constexpr.
47449      However, we expect that cxx_constant_value will be able to deal with
47450      this; also, if the noexcept has no constexpr, then what we parse next
47451      really is a transaction's body.  */
47452   parser->in_transaction = this_in;
47453 
47454   /* Parse a noexcept specification.  */
47455   noex = cp_parser_noexcept_specification_opt (parser,
47456 					       CP_PARSER_FLAGS_NONE,
47457 					       /*require_constexpr=*/false,
47458 					       &noex_expr,
47459 					       /*return_cond=*/true);
47460 
47461   if (!noex || !noex_expr
47462       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47463     {
47464       matching_parens parens;
47465       parens.require_open (parser);
47466 
47467       expr = cp_parser_expression (parser);
47468       expr = finish_parenthesized_expr (expr);
47469 
47470       parens.require_close (parser);
47471     }
47472   else
47473     {
47474       /* The only expression that is available got parsed for the noexcept
47475          already.  noexcept is true then.  */
47476       expr = noex;
47477       noex = boolean_true_node;
47478     }
47479 
47480   expr = build_transaction_expr (token->location, expr, this_in, noex);
47481   parser->in_transaction = old_in;
47482 
47483   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
47484     return error_mark_node;
47485 
47486   return (flag_tm ? expr : error_mark_node);
47487 }
47488 
47489 /* Parse a function-transaction-block.
47490 
47491    function-transaction-block:
47492      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
47493 	 function-body
47494      __transaction_atomic txn-attribute[opt] function-try-block
47495      __transaction_relaxed ctor-initializer[opt] function-body
47496      __transaction_relaxed function-try-block
47497 */
47498 
47499 static void
cp_parser_function_transaction(cp_parser * parser,enum rid keyword)47500 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
47501 {
47502   unsigned char old_in = parser->in_transaction;
47503   unsigned char new_in = 1;
47504   tree compound_stmt, stmt, attrs;
47505   cp_token *token;
47506 
47507   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
47508       || keyword == RID_TRANSACTION_RELAXED);
47509   token = cp_parser_require_keyword (parser, keyword,
47510       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
47511 	  : RT_TRANSACTION_RELAXED));
47512   gcc_assert (token != NULL);
47513 
47514   if (keyword == RID_TRANSACTION_RELAXED)
47515     new_in |= TM_STMT_ATTR_RELAXED;
47516   else
47517     {
47518       attrs = cp_parser_txn_attribute_opt (parser);
47519       if (attrs)
47520 	new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
47521     }
47522 
47523   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
47524 
47525   parser->in_transaction = new_in;
47526 
47527   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
47528     cp_parser_function_try_block (parser);
47529   else
47530     cp_parser_ctor_initializer_opt_and_function_body
47531       (parser, /*in_function_try_block=*/false);
47532 
47533   parser->in_transaction = old_in;
47534 
47535   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
47536 }
47537 
47538 /* Parse a __transaction_cancel statement.
47539 
47540    cancel-statement:
47541      __transaction_cancel txn-attribute[opt] ;
47542      __transaction_cancel txn-attribute[opt] throw-expression ;
47543 
47544    ??? Cancel and throw is not yet implemented.  */
47545 
47546 static tree
cp_parser_transaction_cancel(cp_parser * parser)47547 cp_parser_transaction_cancel (cp_parser *parser)
47548 {
47549   cp_token *token;
47550   bool is_outer = false;
47551   tree stmt, attrs;
47552 
47553   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
47554 				     RT_TRANSACTION_CANCEL);
47555   gcc_assert (token != NULL);
47556 
47557   attrs = cp_parser_txn_attribute_opt (parser);
47558   if (attrs)
47559     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
47560 
47561   /* ??? Parse cancel-and-throw here.  */
47562 
47563   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
47564 
47565   if (!flag_tm)
47566     {
47567       error_at (token->location, "%<__transaction_cancel%> without "
47568 		"transactional memory support enabled");
47569       return error_mark_node;
47570     }
47571   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
47572     {
47573       error_at (token->location, "%<__transaction_cancel%> within a "
47574 		"%<__transaction_relaxed%>");
47575       return error_mark_node;
47576     }
47577   else if (is_outer)
47578     {
47579       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
47580 	  && !is_tm_may_cancel_outer (current_function_decl))
47581 	{
47582 	  error_at (token->location, "outer %<__transaction_cancel%> not "
47583 		    "within outer %<__transaction_atomic%>");
47584 	  error_at (token->location,
47585 		    "  or a %<transaction_may_cancel_outer%> function");
47586 	  return error_mark_node;
47587 	}
47588     }
47589   else if (parser->in_transaction == 0)
47590     {
47591       error_at (token->location, "%<__transaction_cancel%> not within "
47592 		"%<__transaction_atomic%>");
47593       return error_mark_node;
47594     }
47595 
47596   stmt = build_tm_abort_call (token->location, is_outer);
47597   add_stmt (stmt);
47598 
47599   return stmt;
47600 }
47601 
47602 /* The parser.  */
47603 
47604 static GTY (()) cp_parser *the_parser;
47605 
47606 
47607 /* Special handling for the first token or line in the file.  The first
47608    thing in the file might be #pragma GCC pch_preprocess, which loads a
47609    PCH file, which is a GC collection point.  So we need to handle this
47610    first pragma without benefit of an existing lexer structure.
47611 
47612    Always returns one token to the caller in *FIRST_TOKEN.  This is
47613    either the true first token of the file, or the first token after
47614    the initial pragma.  */
47615 
47616 static void
cp_parser_initial_pragma(cp_token * first_token)47617 cp_parser_initial_pragma (cp_token *first_token)
47618 {
47619   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
47620     return;
47621 
47622   cp_lexer_get_preprocessor_token (0, first_token);
47623 
47624   tree name = NULL;
47625   if (first_token->type == CPP_STRING)
47626     {
47627       name = first_token->u.value;
47628 
47629       cp_lexer_get_preprocessor_token (0, first_token);
47630     }
47631 
47632   /* Skip to the end of the pragma.  */
47633   if (first_token->type != CPP_PRAGMA_EOL)
47634     {
47635       error_at (first_token->location,
47636 		"malformed %<#pragma GCC pch_preprocess%>");
47637       do
47638 	cp_lexer_get_preprocessor_token (0, first_token);
47639       while (first_token->type != CPP_PRAGMA_EOL);
47640     }
47641 
47642   /* Now actually load the PCH file.  */
47643   if (name)
47644     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
47645 
47646   /* Read one more token to return to our caller.  We have to do this
47647      after reading the PCH file in, since its pointers have to be
47648      live.  */
47649   cp_lexer_get_preprocessor_token (0, first_token);
47650 }
47651 
47652 /* Parse a pragma GCC ivdep.  */
47653 
47654 static bool
cp_parser_pragma_ivdep(cp_parser * parser,cp_token * pragma_tok)47655 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
47656 {
47657   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47658   return true;
47659 }
47660 
47661 /* Parse a pragma GCC unroll.  */
47662 
47663 static unsigned short
cp_parser_pragma_unroll(cp_parser * parser,cp_token * pragma_tok)47664 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
47665 {
47666   location_t location = cp_lexer_peek_token (parser->lexer)->location;
47667   tree expr = cp_parser_constant_expression (parser);
47668   unsigned short unroll;
47669   expr = fold_non_dependent_expr (expr);
47670   HOST_WIDE_INT lunroll = 0;
47671   if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
47672       || TREE_CODE (expr) != INTEGER_CST
47673       || (lunroll = tree_to_shwi (expr)) < 0
47674       || lunroll >= USHRT_MAX)
47675     {
47676       error_at (location, "%<#pragma GCC unroll%> requires an"
47677 		" assignment-expression that evaluates to a non-negative"
47678 		" integral constant less than %u", USHRT_MAX);
47679       unroll = 0;
47680     }
47681   else
47682     {
47683       unroll = (unsigned short)lunroll;
47684       if (unroll == 0)
47685 	unroll = 1;
47686     }
47687   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47688   return unroll;
47689 }
47690 
47691 /* Normal parsing of a pragma token.  Here we can (and must) use the
47692    regular lexer.  */
47693 
47694 static bool
cp_parser_pragma(cp_parser * parser,enum pragma_context context,bool * if_p)47695 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
47696 {
47697   cp_token *pragma_tok;
47698   unsigned int id;
47699   tree stmt;
47700   bool ret = false;
47701 
47702   pragma_tok = cp_lexer_consume_token (parser->lexer);
47703   gcc_assert (pragma_tok->type == CPP_PRAGMA);
47704   parser->lexer->in_pragma = true;
47705 
47706   id = cp_parser_pragma_kind (pragma_tok);
47707   if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
47708     cp_ensure_no_omp_declare_simd (parser);
47709   switch (id)
47710     {
47711     case PRAGMA_GCC_PCH_PREPROCESS:
47712       error_at (pragma_tok->location,
47713 		"%<#pragma GCC pch_preprocess%> must be first");
47714       break;
47715 
47716     case PRAGMA_OMP_BARRIER:
47717       switch (context)
47718 	{
47719 	case pragma_compound:
47720 	  cp_parser_omp_barrier (parser, pragma_tok);
47721 	  return false;
47722 	case pragma_stmt:
47723 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47724 		    "used in compound statements", "omp barrier");
47725 	  ret = true;
47726 	  break;
47727 	default:
47728 	  goto bad_stmt;
47729 	}
47730       break;
47731 
47732     case PRAGMA_OMP_DEPOBJ:
47733       switch (context)
47734 	{
47735 	case pragma_compound:
47736 	  cp_parser_omp_depobj (parser, pragma_tok);
47737 	  return false;
47738 	case pragma_stmt:
47739 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47740 		    "used in compound statements", "omp depobj");
47741 	  ret = true;
47742 	  break;
47743 	default:
47744 	  goto bad_stmt;
47745 	}
47746       break;
47747 
47748     case PRAGMA_OMP_FLUSH:
47749       switch (context)
47750 	{
47751 	case pragma_compound:
47752 	  cp_parser_omp_flush (parser, pragma_tok);
47753 	  return false;
47754 	case pragma_stmt:
47755 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
47756 		    "used in compound statements", "omp flush");
47757 	  ret = true;
47758 	  break;
47759 	default:
47760 	  goto bad_stmt;
47761 	}
47762       break;
47763 
47764     case PRAGMA_OMP_TASKWAIT:
47765       switch (context)
47766 	{
47767 	case pragma_compound:
47768 	  cp_parser_omp_taskwait (parser, pragma_tok);
47769 	  return false;
47770 	case pragma_stmt:
47771 	  error_at (pragma_tok->location,
47772 		    "%<#pragma %s%> may only be used in compound statements",
47773 		    "omp taskwait");
47774 	  ret = true;
47775 	  break;
47776 	default:
47777 	  goto bad_stmt;
47778 	}
47779       break;
47780 
47781     case PRAGMA_OMP_TASKYIELD:
47782       switch (context)
47783 	{
47784 	case pragma_compound:
47785 	  cp_parser_omp_taskyield (parser, pragma_tok);
47786 	  return false;
47787 	case pragma_stmt:
47788 	  error_at (pragma_tok->location,
47789 		    "%<#pragma %s%> may only be used in compound statements",
47790 		    "omp taskyield");
47791 	  ret = true;
47792 	  break;
47793 	default:
47794 	  goto bad_stmt;
47795 	}
47796       break;
47797 
47798     case PRAGMA_OMP_CANCEL:
47799       switch (context)
47800 	{
47801 	case pragma_compound:
47802 	  cp_parser_omp_cancel (parser, pragma_tok);
47803 	  return false;
47804 	case pragma_stmt:
47805 	  error_at (pragma_tok->location,
47806 		    "%<#pragma %s%> may only be used in compound statements",
47807 		    "omp cancel");
47808 	  ret = true;
47809 	  break;
47810 	default:
47811 	  goto bad_stmt;
47812 	}
47813       break;
47814 
47815     case PRAGMA_OMP_CANCELLATION_POINT:
47816       return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
47817 
47818     case PRAGMA_OMP_THREADPRIVATE:
47819       cp_parser_omp_threadprivate (parser, pragma_tok);
47820       return false;
47821 
47822     case PRAGMA_OMP_DECLARE:
47823       return cp_parser_omp_declare (parser, pragma_tok, context);
47824 
47825     case PRAGMA_OACC_DECLARE:
47826       cp_parser_oacc_declare (parser, pragma_tok);
47827       return false;
47828 
47829     case PRAGMA_OACC_ENTER_DATA:
47830       if (context == pragma_stmt)
47831 	{
47832 	  error_at (pragma_tok->location,
47833 		    "%<#pragma %s%> may only be used in compound statements",
47834 		    "acc enter data");
47835 	  ret = true;
47836 	  break;
47837 	}
47838       else if (context != pragma_compound)
47839 	goto bad_stmt;
47840       cp_parser_omp_construct (parser, pragma_tok, if_p);
47841       return true;
47842 
47843     case PRAGMA_OACC_EXIT_DATA:
47844       if (context == pragma_stmt)
47845 	{
47846 	  error_at (pragma_tok->location,
47847 		    "%<#pragma %s%> may only be used in compound statements",
47848 		    "acc exit data");
47849 	  ret = true;
47850 	  break;
47851 	}
47852       else if (context != pragma_compound)
47853 	goto bad_stmt;
47854       cp_parser_omp_construct (parser, pragma_tok, if_p);
47855       return true;
47856 
47857     case PRAGMA_OACC_ROUTINE:
47858       if (context != pragma_external)
47859 	{
47860 	  error_at (pragma_tok->location,
47861 		    "%<#pragma acc routine%> must be at file scope");
47862 	  ret = true;
47863 	  break;
47864 	}
47865       cp_parser_oacc_routine (parser, pragma_tok, context);
47866       return false;
47867 
47868     case PRAGMA_OACC_UPDATE:
47869       if (context == pragma_stmt)
47870 	{
47871 	  error_at (pragma_tok->location,
47872 		    "%<#pragma %s%> may only be used in compound statements",
47873 		    "acc update");
47874 	  ret = true;
47875 	  break;
47876 	}
47877       else if (context != pragma_compound)
47878 	goto bad_stmt;
47879       cp_parser_omp_construct (parser, pragma_tok, if_p);
47880       return true;
47881 
47882     case PRAGMA_OACC_WAIT:
47883       if (context == pragma_stmt)
47884 	{
47885 	  error_at (pragma_tok->location,
47886 		    "%<#pragma %s%> may only be used in compound statements",
47887 		    "acc wait");
47888 	  ret = true;
47889 	  break;
47890 	}
47891       else if (context != pragma_compound)
47892 	goto bad_stmt;
47893       cp_parser_omp_construct (parser, pragma_tok, if_p);
47894       return true;
47895     case PRAGMA_OMP_ALLOCATE:
47896       cp_parser_omp_allocate (parser, pragma_tok);
47897       return false;
47898     case PRAGMA_OACC_ATOMIC:
47899     case PRAGMA_OACC_CACHE:
47900     case PRAGMA_OACC_DATA:
47901     case PRAGMA_OACC_HOST_DATA:
47902     case PRAGMA_OACC_KERNELS:
47903     case PRAGMA_OACC_LOOP:
47904     case PRAGMA_OACC_PARALLEL:
47905     case PRAGMA_OACC_SERIAL:
47906     case PRAGMA_OMP_ATOMIC:
47907     case PRAGMA_OMP_CRITICAL:
47908     case PRAGMA_OMP_DISTRIBUTE:
47909     case PRAGMA_OMP_FOR:
47910     case PRAGMA_OMP_LOOP:
47911     case PRAGMA_OMP_MASKED:
47912     case PRAGMA_OMP_MASTER:
47913     case PRAGMA_OMP_PARALLEL:
47914     case PRAGMA_OMP_SCOPE:
47915     case PRAGMA_OMP_SECTIONS:
47916     case PRAGMA_OMP_SIMD:
47917     case PRAGMA_OMP_SINGLE:
47918     case PRAGMA_OMP_TASK:
47919     case PRAGMA_OMP_TASKGROUP:
47920     case PRAGMA_OMP_TASKLOOP:
47921     case PRAGMA_OMP_TEAMS:
47922       if (context != pragma_stmt && context != pragma_compound)
47923 	goto bad_stmt;
47924       stmt = push_omp_privatization_clauses (false);
47925       cp_parser_omp_construct (parser, pragma_tok, if_p);
47926       pop_omp_privatization_clauses (stmt);
47927       return true;
47928 
47929     case PRAGMA_OMP_REQUIRES:
47930       if (context != pragma_external)
47931 	{
47932 	  error_at (pragma_tok->location,
47933 		    "%<#pragma omp requires%> may only be used at file or "
47934 		    "namespace scope");
47935 	  ret = true;
47936 	  break;
47937 	}
47938       return cp_parser_omp_requires (parser, pragma_tok);
47939 
47940     case PRAGMA_OMP_NOTHING:
47941       cp_parser_omp_nothing (parser, pragma_tok);
47942       return false;
47943 
47944     case PRAGMA_OMP_ERROR:
47945       return cp_parser_omp_error (parser, pragma_tok, context);
47946 
47947     case PRAGMA_OMP_ORDERED:
47948       if (context != pragma_stmt && context != pragma_compound)
47949 	goto bad_stmt;
47950       stmt = push_omp_privatization_clauses (false);
47951       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
47952       pop_omp_privatization_clauses (stmt);
47953       return ret;
47954 
47955     case PRAGMA_OMP_TARGET:
47956       if (context != pragma_stmt && context != pragma_compound)
47957 	goto bad_stmt;
47958       stmt = push_omp_privatization_clauses (false);
47959       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
47960       pop_omp_privatization_clauses (stmt);
47961       return ret;
47962 
47963     case PRAGMA_OMP_END_DECLARE_TARGET:
47964       cp_parser_omp_end_declare_target (parser, pragma_tok);
47965       return false;
47966 
47967     case PRAGMA_OMP_SCAN:
47968       error_at (pragma_tok->location,
47969 		"%<#pragma omp scan%> may only be used in "
47970 		"a loop construct with %<inscan%> %<reduction%> clause");
47971       break;
47972 
47973     case PRAGMA_OMP_SECTION:
47974       error_at (pragma_tok->location,
47975 		"%<#pragma omp section%> may only be used in "
47976 		"%<#pragma omp sections%> construct");
47977       break;
47978 
47979     case PRAGMA_IVDEP:
47980       {
47981 	if (context == pragma_external)
47982 	  {
47983 	    error_at (pragma_tok->location,
47984 		      "%<#pragma GCC ivdep%> must be inside a function");
47985 	    break;
47986 	  }
47987 	const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
47988 	unsigned short unroll;
47989 	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
47990 	if (tok->type == CPP_PRAGMA
47991 	    && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
47992 	  {
47993 	    tok = cp_lexer_consume_token (parser->lexer);
47994 	    unroll = cp_parser_pragma_unroll (parser, tok);
47995 	    tok = cp_lexer_peek_token (the_parser->lexer);
47996 	  }
47997 	else
47998 	  unroll = 0;
47999 	if (tok->type != CPP_KEYWORD
48000 	    || (tok->keyword != RID_FOR
48001 		&& tok->keyword != RID_WHILE
48002 		&& tok->keyword != RID_DO))
48003 	  {
48004 	    cp_parser_error (parser, "for, while or do statement expected");
48005 	    return false;
48006 	  }
48007 	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48008 	return true;
48009       }
48010 
48011     case PRAGMA_UNROLL:
48012       {
48013 	if (context == pragma_external)
48014 	  {
48015 	    error_at (pragma_tok->location,
48016 		      "%<#pragma GCC unroll%> must be inside a function");
48017 	    break;
48018 	  }
48019 	const unsigned short unroll
48020 	  = cp_parser_pragma_unroll (parser, pragma_tok);
48021 	bool ivdep;
48022 	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48023 	if (tok->type == CPP_PRAGMA
48024 	    && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
48025 	  {
48026 	    tok = cp_lexer_consume_token (parser->lexer);
48027 	    ivdep = cp_parser_pragma_ivdep (parser, tok);
48028 	    tok = cp_lexer_peek_token (the_parser->lexer);
48029 	  }
48030 	else
48031 	  ivdep = false;
48032 	if (tok->type != CPP_KEYWORD
48033 	    || (tok->keyword != RID_FOR
48034 		&& tok->keyword != RID_WHILE
48035 		&& tok->keyword != RID_DO))
48036 	  {
48037 	    cp_parser_error (parser, "for, while or do statement expected");
48038 	    return false;
48039 	  }
48040 	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48041 	return true;
48042       }
48043 
48044     default:
48045       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
48046       c_invoke_pragma_handler (id);
48047       break;
48048 
48049     bad_stmt:
48050       cp_parser_error (parser, "expected declaration specifiers");
48051       break;
48052     }
48053 
48054   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48055   return ret;
48056 }
48057 
48058 /* The interface the pragma parsers have to the lexer.  */
48059 
48060 enum cpp_ttype
pragma_lex(tree * value,location_t * loc)48061 pragma_lex (tree *value, location_t *loc)
48062 {
48063   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48064   enum cpp_ttype ret = tok->type;
48065 
48066   *value = tok->u.value;
48067   if (loc)
48068     *loc = tok->location;
48069 
48070   if (ret == CPP_PRAGMA_EOL)
48071     ret = CPP_EOF;
48072   else if (ret == CPP_STRING)
48073     *value = cp_parser_string_literal (the_parser, false, false);
48074   else
48075     {
48076       if (ret == CPP_KEYWORD)
48077 	ret = CPP_NAME;
48078       cp_lexer_consume_token (the_parser->lexer);
48079     }
48080 
48081   return ret;
48082 }
48083 
48084 
48085 /* External interface.  */
48086 
48087 /* Parse one entire translation unit.  */
48088 
48089 void
c_parse_file(void)48090 c_parse_file (void)
48091 {
48092   static bool already_called = false;
48093 
48094   if (already_called)
48095     fatal_error (input_location,
48096 		 "multi-source compilation not implemented for C++");
48097   already_called = true;
48098 
48099   /* cp_lexer_new_main is called before doing any GC allocation
48100      because tokenization might load a PCH file.  */
48101   cp_lexer *lexer = cp_lexer_new_main ();
48102 
48103   the_parser = cp_parser_new (lexer);
48104 
48105   cp_parser_translation_unit (the_parser);
48106   class_decl_loc_t::diag_mismatched_tags ();
48107 
48108   the_parser = NULL;
48109 
48110   finish_translation_unit ();
48111 }
48112 
48113 /* Create an identifier for a generic parameter type (a synthesized
48114    template parameter implied by `auto' or a concept identifier). */
48115 
48116 static GTY(()) int generic_parm_count;
48117 static tree
make_generic_type_name()48118 make_generic_type_name ()
48119 {
48120   char buf[32];
48121   sprintf (buf, "auto:%d", ++generic_parm_count);
48122   return get_identifier (buf);
48123 }
48124 
48125 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
48126    (creating a new template parameter list if necessary).  Returns the newly
48127    created template type parm.  */
48128 
48129 static tree
synthesize_implicit_template_parm(cp_parser * parser,tree constr)48130 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
48131 {
48132   /* A requires-clause is not a function and cannot have placeholders.  */
48133   if (current_binding_level->requires_expression)
48134     {
48135       error ("placeholder type not allowed in this context");
48136       return error_mark_node;
48137     }
48138 
48139   gcc_assert (current_binding_level->kind == sk_function_parms);
48140 
48141   /* We are either continuing a function template that already contains implicit
48142      template parameters, creating a new fully-implicit function template, or
48143      extending an existing explicit function template with implicit template
48144      parameters.  */
48145 
48146   cp_binding_level *const entry_scope = current_binding_level;
48147 
48148   bool become_template = false;
48149   cp_binding_level *parent_scope = 0;
48150 
48151   if (parser->implicit_template_scope)
48152     {
48153       gcc_assert (parser->implicit_template_parms);
48154 
48155       current_binding_level = parser->implicit_template_scope;
48156     }
48157   else
48158     {
48159       /* Roll back to the existing template parameter scope (in the case of
48160 	 extending an explicit function template) or introduce a new template
48161 	 parameter scope ahead of the function parameter scope (or class scope
48162 	 in the case of out-of-line member definitions).  The function scope is
48163 	 added back after template parameter synthesis below.  */
48164 
48165       cp_binding_level *scope = entry_scope;
48166 
48167       while (scope->kind == sk_function_parms)
48168 	{
48169 	  parent_scope = scope;
48170 	  scope = scope->level_chain;
48171 	}
48172       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
48173 	{
48174 	  /* If not defining a class, then any class scope is a scope level in
48175 	     an out-of-line member definition.  In this case simply wind back
48176 	     beyond the first such scope to inject the template parameter list.
48177 	     Otherwise wind back to the class being defined.  The latter can
48178 	     occur in class member friend declarations such as:
48179 
48180 	       class A {
48181 		 void foo (auto);
48182 	       };
48183 	       class B {
48184 		 friend void A::foo (auto);
48185 	       };
48186 
48187 	    The template parameter list synthesized for the friend declaration
48188 	    must be injected in the scope of 'B'.  This can also occur in
48189 	    erroneous cases such as:
48190 
48191 	       struct A {
48192 	         struct B {
48193 		   void foo (auto);
48194 		 };
48195 		 void B::foo (auto) {}
48196 	       };
48197 
48198 	    Here the attempted definition of 'B::foo' within 'A' is ill-formed
48199 	    but, nevertheless, the template parameter list synthesized for the
48200 	    declarator should be injected into the scope of 'A' as if the
48201 	    ill-formed template was specified explicitly.  */
48202 
48203 	  while (scope->kind == sk_class && !scope->defining_class_p)
48204 	    {
48205 	      parent_scope = scope;
48206 	      scope = scope->level_chain;
48207 	    }
48208 	}
48209 
48210       current_binding_level = scope;
48211 
48212       if (scope->kind != sk_template_parms
48213 	  || !function_being_declared_is_template_p (parser))
48214 	{
48215 	  /* Introduce a new template parameter list for implicit template
48216 	     parameters.  */
48217 
48218 	  become_template = true;
48219 
48220 	  parser->implicit_template_scope
48221 	      = begin_scope (sk_template_parms, NULL);
48222 
48223 	  ++processing_template_decl;
48224 
48225 	  parser->fully_implicit_function_template_p = true;
48226 	  ++parser->num_template_parameter_lists;
48227 	}
48228       else
48229 	{
48230 	  /* Synthesize implicit template parameters at the end of the explicit
48231 	     template parameter list.  */
48232 
48233 	  gcc_assert (current_template_parms);
48234 
48235 	  parser->implicit_template_scope = scope;
48236 
48237 	  tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48238 	  parser->implicit_template_parms
48239 	    = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
48240 	}
48241     }
48242 
48243   /* Synthesize a new template parameter and track the current template
48244      parameter chain with implicit_template_parms.  */
48245 
48246   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
48247   tree synth_id = make_generic_type_name ();
48248   tree synth_tmpl_parm;
48249   bool non_type = false;
48250 
48251   /* Synthesize the type template parameter.  */
48252   gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
48253   synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
48254 
48255   if (become_template)
48256     current_template_parms = tree_cons (size_int (current_template_depth + 1),
48257 					NULL_TREE, current_template_parms);
48258 
48259   /* Attach the constraint to the parm before processing.  */
48260   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
48261   TREE_TYPE (node) = constr;
48262   tree new_parm
48263     = process_template_parm (parser->implicit_template_parms,
48264 			     input_location,
48265 			     node,
48266 			     /*non_type=*/non_type,
48267 			     /*param_pack=*/false);
48268 
48269   /* Mark the synthetic declaration "virtual". This is used when
48270      comparing template-heads to determine if whether an abbreviated
48271      function template is equivalent to an explicit template.
48272 
48273      Note that DECL_ARTIFICIAL is used elsewhere for template parameters.  */
48274   if (TREE_VALUE (new_parm) != error_mark_node)
48275     DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
48276 
48277   // Chain the new parameter to the list of implicit parameters.
48278   if (parser->implicit_template_parms)
48279     parser->implicit_template_parms
48280       = TREE_CHAIN (parser->implicit_template_parms);
48281   else
48282     parser->implicit_template_parms = new_parm;
48283 
48284   tree new_decl = get_local_decls ();
48285   if (non_type)
48286     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
48287     new_decl = DECL_INITIAL (new_decl);
48288 
48289   /* If creating a fully implicit function template, start the new implicit
48290      template parameter list with this synthesized type, otherwise grow the
48291      current template parameter list.  */
48292 
48293   if (become_template)
48294     {
48295       parent_scope->level_chain = current_binding_level;
48296 
48297       tree new_parms = make_tree_vec (1);
48298       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
48299       TREE_VALUE (current_template_parms) = new_parms;
48300     }
48301   else
48302     {
48303       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48304       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
48305       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
48306       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
48307     }
48308 
48309   /* If the new parameter was constrained, we need to add that to the
48310      constraints in the template parameter list.  */
48311   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
48312     {
48313       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
48314       reqs = combine_constraint_expressions (reqs, req);
48315       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
48316     }
48317 
48318   current_binding_level = entry_scope;
48319 
48320   return new_decl;
48321 }
48322 
48323 /* Finish the declaration of a fully implicit function template.  Such a
48324    template has no explicit template parameter list so has not been through the
48325    normal template head and tail processing.  synthesize_implicit_template_parm
48326    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
48327    provided if the declaration is a class member such that its template
48328    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
48329    form is returned.  Otherwise NULL_TREE is returned. */
48330 
48331 static tree
finish_fully_implicit_template(cp_parser * parser,tree member_decl_opt)48332 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
48333 {
48334   gcc_assert (parser->fully_implicit_function_template_p);
48335 
48336   if (member_decl_opt && member_decl_opt != error_mark_node
48337       && DECL_VIRTUAL_P (member_decl_opt))
48338     {
48339       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
48340 		"implicit templates may not be %<virtual%>");
48341       DECL_VIRTUAL_P (member_decl_opt) = false;
48342     }
48343 
48344   if (member_decl_opt)
48345     member_decl_opt = finish_member_template_decl (member_decl_opt);
48346   end_template_decl ();
48347 
48348   parser->fully_implicit_function_template_p = false;
48349   parser->implicit_template_parms = 0;
48350   parser->implicit_template_scope = 0;
48351   --parser->num_template_parameter_lists;
48352 
48353   return member_decl_opt;
48354 }
48355 
48356 /* Like finish_fully_implicit_template, but to be used in error
48357    recovery, rearranging scopes so that we restore the state we had
48358    before synthesize_implicit_template_parm inserted the implement
48359    template parms scope.  */
48360 
48361 static void
abort_fully_implicit_template(cp_parser * parser)48362 abort_fully_implicit_template (cp_parser *parser)
48363 {
48364   cp_binding_level *return_to_scope = current_binding_level;
48365 
48366   if (parser->implicit_template_scope
48367       && return_to_scope != parser->implicit_template_scope)
48368     {
48369       cp_binding_level *child = return_to_scope;
48370       for (cp_binding_level *scope = child->level_chain;
48371 	   scope != parser->implicit_template_scope;
48372 	   scope = child->level_chain)
48373 	child = scope;
48374       child->level_chain = parser->implicit_template_scope->level_chain;
48375       parser->implicit_template_scope->level_chain = return_to_scope;
48376       current_binding_level = parser->implicit_template_scope;
48377     }
48378   else
48379     return_to_scope = return_to_scope->level_chain;
48380 
48381   finish_fully_implicit_template (parser, NULL);
48382 
48383   gcc_assert (current_binding_level == return_to_scope);
48384 }
48385 
48386 /* Helper function for diagnostics that have complained about things
48387    being used with 'extern "C"' linkage.
48388 
48389    Attempt to issue a note showing where the 'extern "C"' linkage began.  */
48390 
48391 void
maybe_show_extern_c_location(void)48392 maybe_show_extern_c_location (void)
48393 {
48394   if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
48395     inform (the_parser->innermost_linkage_specification_location,
48396 	    "%<extern \"C\"%> linkage started here");
48397 }
48398 
48399 #include "gt-cp-parser.h"
48400