1 /* -*- C++ -*- Parser.
2    Copyright (C) 2000-2019 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 
49 
50 /* The lexer.  */
51 
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53    and c-lex.c) and the C++ parser.  */
54 
55 static cp_token eof_token =
56 {
57   CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59 
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62   NIC_NONE,
63   /* floating-point literal */
64   NIC_FLOAT,
65   /* %<this%> */
66   NIC_THIS,
67   /* %<__FUNCTION__%> */
68   NIC_FUNC_NAME,
69   /* %<__PRETTY_FUNCTION__%> */
70   NIC_PRETTY_FUNC,
71   /* %<__func__%> */
72   NIC_C99_FUNC,
73   /* "%<va_arg%> */
74   NIC_VA_ARG,
75   /* a cast */
76   NIC_CAST,
77   /* %<typeid%> operator */
78   NIC_TYPEID,
79   /* non-constant compound literals */
80   NIC_NCC,
81   /* a function call */
82   NIC_FUNC_CALL,
83   /* an increment */
84   NIC_INC,
85   /* an decrement */
86   NIC_DEC,
87   /* an array reference */
88   NIC_ARRAY_REF,
89   /* %<->%> */
90   NIC_ARROW,
91   /* %<.%> */
92   NIC_POINT,
93   /* the address of a label */
94   NIC_ADDR_LABEL,
95   /* %<*%> */
96   NIC_STAR,
97   /* %<&%> */
98   NIC_ADDR,
99   /* %<++%> */
100   NIC_PREINCREMENT,
101   /* %<--%> */
102   NIC_PREDECREMENT,
103   /* %<new%> */
104   NIC_NEW,
105   /* %<delete%> */
106   NIC_DEL,
107   /* calls to overloaded operators */
108   NIC_OVERLOADED,
109   /* an assignment */
110   NIC_ASSIGNMENT,
111   /* a comma operator */
112   NIC_COMMA,
113   /* a call to a constructor */
114   NIC_CONSTRUCTOR,
115   /* a transaction expression */
116   NIC_TRANSACTION
117 };
118 
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121   /* NULL */
122   NLE_NULL,
123   /* is not a type */
124   NLE_TYPE,
125   /* is not a class or namespace */
126   NLE_CXX98,
127   /* is not a class, namespace, or enumeration */
128   NLE_NOT_CXX98
129 };
130 
131 /* The various kinds of required token */
132 enum required_token {
133   RT_NONE,
134   RT_SEMICOLON,  /* ';' */
135   RT_OPEN_PAREN, /* '(' */
136   RT_CLOSE_BRACE, /* '}' */
137   RT_OPEN_BRACE,  /* '{' */
138   RT_CLOSE_SQUARE, /* ']' */
139   RT_OPEN_SQUARE,  /* '[' */
140   RT_COMMA, /* ',' */
141   RT_SCOPE, /* '::' */
142   RT_LESS, /* '<' */
143   RT_GREATER, /* '>' */
144   RT_EQ, /* '=' */
145   RT_ELLIPSIS, /* '...' */
146   RT_MULT, /* '*' */
147   RT_COMPL, /* '~' */
148   RT_COLON, /* ':' */
149   RT_COLON_SCOPE, /* ':' or '::' */
150   RT_CLOSE_PAREN, /* ')' */
151   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152   RT_PRAGMA_EOL, /* end of line */
153   RT_NAME, /* identifier */
154 
155   /* The type is CPP_KEYWORD */
156   RT_NEW, /* new */
157   RT_DELETE, /* delete */
158   RT_RETURN, /* return */
159   RT_WHILE, /* while */
160   RT_EXTERN, /* extern */
161   RT_STATIC_ASSERT, /* static_assert */
162   RT_DECLTYPE, /* decltype */
163   RT_OPERATOR, /* operator */
164   RT_CLASS, /* class */
165   RT_TEMPLATE, /* template */
166   RT_NAMESPACE, /* namespace */
167   RT_USING, /* using */
168   RT_ASM, /* asm */
169   RT_TRY, /* try */
170   RT_CATCH, /* catch */
171   RT_THROW, /* throw */
172   RT_LABEL, /* __label__ */
173   RT_AT_TRY, /* @try */
174   RT_AT_SYNCHRONIZED, /* @synchronized */
175   RT_AT_THROW, /* @throw */
176 
177   RT_SELECT,  /* selection-statement */
178   RT_ITERATION, /* iteration-statement */
179   RT_JUMP, /* jump-statement */
180   RT_CLASS_KEY, /* class-key */
181   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184   RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186 
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188    reverting it on destruction.  */
189 
190 class type_id_in_expr_sentinel
191 {
192   cp_parser *parser;
193   bool saved;
194 public:
195   type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
parser(parser)196     : parser (parser),
197       saved (parser->in_type_id_in_expr_p)
198   { parser->in_type_id_in_expr_p = set; }
~type_id_in_expr_sentinel()199   ~type_id_in_expr_sentinel ()
200   { parser->in_type_id_in_expr_p = saved; }
201 };
202 
203 /* Prototypes.  */
204 
205 static cp_lexer *cp_lexer_new_main
206   (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208   (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210   (cp_lexer *);
211 static int cp_lexer_saving_tokens
212   (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214   (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216   (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218   (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220   (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222   (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224   (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226   (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228   (cp_lexer *);
229 static void cp_lexer_purge_token
230   (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232   (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234   (cp_lexer *);
235 static void cp_lexer_commit_tokens
236   (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238   (cp_lexer *);
239 static void cp_lexer_print_token
240   (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242   (cp_lexer *);
243 static void cp_lexer_start_debugging
244   (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246   (cp_lexer *) ATTRIBUTE_UNUSED;
247 
248 static cp_token_cache *cp_token_cache_new
249   (cp_token *, cp_token *);
250 
251 static void cp_parser_initial_pragma
252   (cp_token *);
253 
254 static bool cp_parser_omp_declare_reduction_exprs
255   (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257   (cp_parser *, tree, bool);
258 
259 /* Manifest constants.  */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
262 
263 /* Variables.  */
264 
265 /* The stream to which debugging output should be written.  */
266 static FILE *cp_lexer_debug_stream;
267 
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269    sizeof, typeof, or alignof.  */
270 int cp_unevaluated_operand;
271 
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
274    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
275    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276    highlighted by surrounding it in [[ ]].  */
277 
278 static void
cp_lexer_dump_tokens(FILE * file,vec<cp_token,va_gc> * buffer,cp_token * start_token,unsigned num,cp_token * curr_token)279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 		      cp_token *start_token, unsigned num,
281 		      cp_token *curr_token)
282 {
283   unsigned i, nprinted;
284   cp_token *token;
285   bool do_print;
286 
287   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288 
289   if (buffer == NULL)
290     return;
291 
292   if (num == 0)
293     num = buffer->length ();
294 
295   if (start_token == NULL)
296     start_token = buffer->address ();
297 
298   if (start_token > buffer->address ())
299     {
300       cp_lexer_print_token (file, &(*buffer)[0]);
301       fprintf (file, " ... ");
302     }
303 
304   do_print = false;
305   nprinted = 0;
306   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307     {
308       if (token == start_token)
309 	do_print = true;
310 
311       if (!do_print)
312 	continue;
313 
314       nprinted++;
315       if (token == curr_token)
316 	fprintf (file, "[[");
317 
318       cp_lexer_print_token (file, token);
319 
320       if (token == curr_token)
321 	fprintf (file, "]]");
322 
323       switch (token->type)
324 	{
325 	  case CPP_SEMICOLON:
326 	  case CPP_OPEN_BRACE:
327 	  case CPP_CLOSE_BRACE:
328 	  case CPP_EOF:
329 	    fputc ('\n', file);
330 	    break;
331 
332 	  default:
333 	    fputc (' ', file);
334 	}
335     }
336 
337   if (i == num && i < buffer->length ())
338     {
339       fprintf (file, " ... ");
340       cp_lexer_print_token (file, &buffer->last ());
341     }
342 
343   fprintf (file, "\n");
344 }
345 
346 
347 /* Dump all tokens in BUFFER to stderr.  */
348 
349 void
cp_lexer_debug_tokens(vec<cp_token,va_gc> * buffer)350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 {
352   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
353 }
354 
355 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> & ref)356 debug (vec<cp_token, va_gc> &ref)
357 {
358   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
359 }
360 
361 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> * ptr)362 debug (vec<cp_token, va_gc> *ptr)
363 {
364   if (ptr)
365     debug (*ptr);
366   else
367     fprintf (stderr, "<nil>\n");
368 }
369 
370 
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
372    description for T.  */
373 
374 static void
cp_debug_print_tree_if_set(FILE * file,const char * desc,tree t)375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 {
377   if (t)
378     {
379       fprintf (file, "%s: ", desc);
380       print_node_brief (file, "", t, 0);
381     }
382 }
383 
384 
385 /* Dump parser context C to FILE.  */
386 
387 static void
cp_debug_print_context(FILE * file,cp_parser_context * c)388 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 {
390   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392   print_node_brief (file, "", c->object_type, 0);
393   fprintf (file, "}\n");
394 }
395 
396 
397 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
398 
399 static void
cp_debug_print_context_stack(FILE * file,cp_parser_context * first)400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 {
402   unsigned i;
403   cp_parser_context *c;
404 
405   fprintf (file, "Parsing context stack:\n");
406   for (i = 0, c = first; c; c = c->next, i++)
407     {
408       fprintf (file, "\t#%u: ", i);
409       cp_debug_print_context (file, c);
410     }
411 }
412 
413 
414 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
415 
416 static void
cp_debug_print_flag(FILE * file,const char * desc,bool flag)417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 {
419   if (flag)
420     fprintf (file, "%s: true\n", desc);
421 }
422 
423 
424 /* Print an unparsed function entry UF to FILE.  */
425 
426 static void
cp_debug_print_unparsed_function(FILE * file,cp_unparsed_functions_entry * uf)427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 {
429   unsigned i;
430   cp_default_arg_entry *default_arg_fn;
431   tree fn;
432 
433   fprintf (file, "\tFunctions with default args:\n");
434   for (i = 0;
435        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436        i++)
437     {
438       fprintf (file, "\t\tClass type: ");
439       print_node_brief (file, "", default_arg_fn->class_type, 0);
440       fprintf (file, "\t\tDeclaration: ");
441       print_node_brief (file, "", default_arg_fn->decl, 0);
442       fprintf (file, "\n");
443     }
444 
445   fprintf (file, "\n\tFunctions with definitions that require "
446 	   "post-processing\n\t\t");
447   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448     {
449       print_node_brief (file, "", fn, 0);
450       fprintf (file, " ");
451     }
452   fprintf (file, "\n");
453 
454   fprintf (file, "\n\tNon-static data members with initializers that require "
455            "post-processing\n\t\t");
456   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457     {
458       print_node_brief (file, "", fn, 0);
459       fprintf (file, " ");
460     }
461   fprintf (file, "\n");
462 }
463 
464 
465 /* Print the stack of unparsed member functions S to FILE.  */
466 
467 static void
cp_debug_print_unparsed_queues(FILE * file,vec<cp_unparsed_functions_entry,va_gc> * s)468 cp_debug_print_unparsed_queues (FILE *file,
469 				vec<cp_unparsed_functions_entry, va_gc> *s)
470 {
471   unsigned i;
472   cp_unparsed_functions_entry *uf;
473 
474   fprintf (file, "Unparsed functions\n");
475   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476     {
477       fprintf (file, "#%u:\n", i);
478       cp_debug_print_unparsed_function (file, uf);
479     }
480 }
481 
482 
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
485 
486 static void
cp_debug_parser_tokens(FILE * file,cp_parser * parser,int window_size)487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 {
489   cp_token *next_token, *first_token, *start_token;
490 
491   if (file == NULL)
492     file = stderr;
493 
494   next_token = parser->lexer->next_token;
495   first_token = parser->lexer->buffer->address ();
496   start_token = (next_token > first_token + window_size / 2)
497 		? next_token - window_size / 2
498 		: first_token;
499   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 			next_token);
501 }
502 
503 
504 /* Dump debugging information for the given PARSER.  If FILE is NULL,
505    the output is printed on stderr.  */
506 
507 void
cp_debug_parser(FILE * file,cp_parser * parser)508 cp_debug_parser (FILE *file, cp_parser *parser)
509 {
510   const size_t window_size = 20;
511   cp_token *token;
512   expanded_location eloc;
513 
514   if (file == NULL)
515     file = stderr;
516 
517   fprintf (file, "Parser state\n\n");
518   fprintf (file, "Number of tokens: %u\n",
519 	   vec_safe_length (parser->lexer->buffer));
520   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521   cp_debug_print_tree_if_set (file, "Object scope",
522 				     parser->object_scope);
523   cp_debug_print_tree_if_set (file, "Qualifying scope",
524 				     parser->qualifying_scope);
525   cp_debug_print_context_stack (file, parser->context);
526   cp_debug_print_flag (file, "Allow GNU extensions",
527 			      parser->allow_gnu_extensions_p);
528   cp_debug_print_flag (file, "'>' token is greater-than",
529 			      parser->greater_than_is_operator_p);
530   cp_debug_print_flag (file, "Default args allowed in current "
531 			      "parameter list", parser->default_arg_ok_p);
532   cp_debug_print_flag (file, "Parsing integral constant-expression",
533 			      parser->integral_constant_expression_p);
534   cp_debug_print_flag (file, "Allow non-constant expression in current "
535 			      "constant-expression",
536 			      parser->allow_non_integral_constant_expression_p);
537   cp_debug_print_flag (file, "Seen non-constant expression",
538 			      parser->non_integral_constant_expression_p);
539   cp_debug_print_flag (file, "Local names forbidden in current context",
540 			      (parser->local_variables_forbidden_p
541 			       & LOCAL_VARS_FORBIDDEN));
542   cp_debug_print_flag (file, "'this' forbidden in current context",
543 			      (parser->local_variables_forbidden_p
544 			       & THIS_FORBIDDEN));
545   cp_debug_print_flag (file, "In unbraced linkage specification",
546 			      parser->in_unbraced_linkage_specification_p);
547   cp_debug_print_flag (file, "Parsing a declarator",
548 			      parser->in_declarator_p);
549   cp_debug_print_flag (file, "In template argument list",
550 			      parser->in_template_argument_list_p);
551   cp_debug_print_flag (file, "Parsing an iteration statement",
552 			      parser->in_statement & IN_ITERATION_STMT);
553   cp_debug_print_flag (file, "Parsing a switch statement",
554 			      parser->in_statement & IN_SWITCH_STMT);
555   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 			      parser->in_statement & IN_OMP_BLOCK);
557   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 			      parser->in_statement & IN_OMP_FOR);
559   cp_debug_print_flag (file, "Parsing an if statement",
560 			      parser->in_statement & IN_IF_STMT);
561   cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 			      "context", parser->in_type_id_in_expr_p);
563   cp_debug_print_flag (file, "String expressions should be translated "
564 			      "to execution character set",
565 			      parser->translate_strings_p);
566   cp_debug_print_flag (file, "Parsing function body outside of a "
567 			      "local class", parser->in_function_body);
568   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
569 			      parser->colon_corrects_to_scope_p);
570   cp_debug_print_flag (file, "Colon doesn't start a class definition",
571 			      parser->colon_doesnt_start_class_def_p);
572   if (parser->type_definition_forbidden_message)
573     fprintf (file, "Error message for forbidden type definitions: %s %s\n",
574 	     parser->type_definition_forbidden_message,
575 	     parser->type_definition_forbidden_message_arg
576 	     ? parser->type_definition_forbidden_message_arg : "<none>");
577   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
578   fprintf (file, "Number of class definitions in progress: %u\n",
579 	   parser->num_classes_being_defined);
580   fprintf (file, "Number of template parameter lists for the current "
581 	   "declaration: %u\n", parser->num_template_parameter_lists);
582   cp_debug_parser_tokens (file, parser, window_size);
583   token = parser->lexer->next_token;
584   fprintf (file, "Next token to parse:\n");
585   fprintf (file, "\tToken:  ");
586   cp_lexer_print_token (file, token);
587   eloc = expand_location (token->location);
588   fprintf (file, "\n\tFile:   %s\n", eloc.file);
589   fprintf (file, "\tLine:   %d\n", eloc.line);
590   fprintf (file, "\tColumn: %d\n", eloc.column);
591 }
592 
593 DEBUG_FUNCTION void
debug(cp_parser & ref)594 debug (cp_parser &ref)
595 {
596   cp_debug_parser (stderr, &ref);
597 }
598 
599 DEBUG_FUNCTION void
debug(cp_parser * ptr)600 debug (cp_parser *ptr)
601 {
602   if (ptr)
603     debug (*ptr);
604   else
605     fprintf (stderr, "<nil>\n");
606 }
607 
608 /* Allocate memory for a new lexer object and return it.  */
609 
610 static cp_lexer *
cp_lexer_alloc(void)611 cp_lexer_alloc (void)
612 {
613   cp_lexer *lexer;
614 
615   c_common_no_more_pch ();
616 
617   /* Allocate the memory.  */
618   lexer = ggc_cleared_alloc<cp_lexer> ();
619 
620   /* Initially we are not debugging.  */
621   lexer->debugging_p = false;
622 
623   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
624 
625   /* Create the buffer.  */
626   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
627 
628   return lexer;
629 }
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_lexer *lexer;
639   cp_token token;
640 
641   /* It's possible that parsing the first pragma will load a PCH file,
642      which is a GC collection point.  So we have to do that before
643      allocating any memory.  */
644   cp_parser_initial_pragma (&token);
645 
646   lexer = cp_lexer_alloc ();
647 
648   /* Put the first token in the buffer.  */
649   lexer->buffer->quick_push (token);
650 
651   /* Get the remaining tokens from the preprocessor.  */
652   while (token.type != CPP_EOF)
653     {
654       cp_lexer_get_preprocessor_token (lexer, &token);
655       vec_safe_push (lexer->buffer, token);
656     }
657 
658   lexer->last_token = lexer->buffer->address ()
659                       + lexer->buffer->length ()
660 		      - 1;
661   lexer->next_token = lexer->buffer->length ()
662 		      ? lexer->buffer->address ()
663 		      : &eof_token;
664 
665   /* Subsequent preprocessor diagnostics should use compiler
666      diagnostic functions to get the compiler source location.  */
667   done_lexing = true;
668 
669   gcc_assert (!lexer->next_token->purged_p);
670   return lexer;
671 }
672 
673 /* Create a new lexer whose token stream is primed with the tokens in
674    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
675 
676 static cp_lexer *
cp_lexer_new_from_tokens(cp_token_cache * cache)677 cp_lexer_new_from_tokens (cp_token_cache *cache)
678 {
679   cp_token *first = cache->first;
680   cp_token *last = cache->last;
681   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
682 
683   /* We do not own the buffer.  */
684   lexer->buffer = NULL;
685   lexer->next_token = first == last ? &eof_token : first;
686   lexer->last_token = last;
687 
688   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
689 
690   /* Initially we are not debugging.  */
691   lexer->debugging_p = false;
692 
693   gcc_assert (!lexer->next_token->purged_p);
694   return lexer;
695 }
696 
697 /* Frees all resources associated with LEXER.  */
698 
699 static void
cp_lexer_destroy(cp_lexer * lexer)700 cp_lexer_destroy (cp_lexer *lexer)
701 {
702   vec_free (lexer->buffer);
703   lexer->saved_tokens.release ();
704   ggc_free (lexer);
705 }
706 
707 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
708    be used.  The point of this flag is to help the compiler to fold away calls
709    to cp_lexer_debugging_p within this source file at compile time, when the
710    lexer is not being debugged.  */
711 
712 #define LEXER_DEBUGGING_ENABLED_P false
713 
714 /* Returns nonzero if debugging information should be output.  */
715 
716 static inline bool
cp_lexer_debugging_p(cp_lexer * lexer)717 cp_lexer_debugging_p (cp_lexer *lexer)
718 {
719   if (!LEXER_DEBUGGING_ENABLED_P)
720     return false;
721 
722   return lexer->debugging_p;
723 }
724 
725 
726 static inline cp_token_position
cp_lexer_token_position(cp_lexer * lexer,bool previous_p)727 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
728 {
729   gcc_assert (!previous_p || lexer->next_token != &eof_token);
730 
731   return lexer->next_token - previous_p;
732 }
733 
734 static inline cp_token *
cp_lexer_token_at(cp_lexer *,cp_token_position pos)735 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
736 {
737   return pos;
738 }
739 
740 static inline void
cp_lexer_set_token_position(cp_lexer * lexer,cp_token_position pos)741 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
742 {
743   lexer->next_token = cp_lexer_token_at (lexer, pos);
744 }
745 
746 static inline cp_token_position
cp_lexer_previous_token_position(cp_lexer * lexer)747 cp_lexer_previous_token_position (cp_lexer *lexer)
748 {
749   if (lexer->next_token == &eof_token)
750     return lexer->last_token - 1;
751   else
752     return cp_lexer_token_position (lexer, true);
753 }
754 
755 static inline cp_token *
cp_lexer_previous_token(cp_lexer * lexer)756 cp_lexer_previous_token (cp_lexer *lexer)
757 {
758   cp_token_position tp = cp_lexer_previous_token_position (lexer);
759 
760   /* Skip past purged tokens.  */
761   while (tp->purged_p)
762     {
763       gcc_assert (tp != vec_safe_address (lexer->buffer));
764       tp--;
765     }
766 
767   return cp_lexer_token_at (lexer, tp);
768 }
769 
770 /* nonzero if we are presently saving tokens.  */
771 
772 static inline int
cp_lexer_saving_tokens(const cp_lexer * lexer)773 cp_lexer_saving_tokens (const cp_lexer* lexer)
774 {
775   return lexer->saved_tokens.length () != 0;
776 }
777 
778 /* Store the next token from the preprocessor in *TOKEN.  Return true
779    if we reach EOF.  If LEXER is NULL, assume we are handling an
780    initial #pragma pch_preprocess, and thus want the lexer to return
781    processed strings.  */
782 
783 static void
cp_lexer_get_preprocessor_token(cp_lexer * lexer,cp_token * token)784 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
785 {
786   static int is_extern_c = 0;
787 
788    /* Get a new token from the preprocessor.  */
789   token->type
790     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
791 			lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
792   token->keyword = RID_MAX;
793   token->purged_p = false;
794   token->error_reported = false;
795 
796   /* On some systems, some header files are surrounded by an
797      implicit extern "C" block.  Set a flag in the token if it
798      comes from such a header.  */
799   is_extern_c += pending_lang_change;
800   pending_lang_change = 0;
801   token->implicit_extern_c = is_extern_c > 0;
802 
803   /* Check to see if this token is a keyword.  */
804   if (token->type == CPP_NAME)
805     {
806       if (IDENTIFIER_KEYWORD_P (token->u.value))
807 	{
808 	  /* Mark this token as a keyword.  */
809 	  token->type = CPP_KEYWORD;
810 	  /* Record which keyword.  */
811 	  token->keyword = C_RID_CODE (token->u.value);
812 	}
813       else
814 	{
815           if (warn_cxx11_compat
816               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
817               && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
818             {
819               /* Warn about the C++0x keyword (but still treat it as
820                  an identifier).  */
821               warning (OPT_Wc__11_compat,
822                        "identifier %qE is a keyword in C++11",
823                        token->u.value);
824 
825               /* Clear out the C_RID_CODE so we don't warn about this
826                  particular identifier-turned-keyword again.  */
827               C_SET_RID_CODE (token->u.value, RID_MAX);
828             }
829 
830 	  token->keyword = RID_MAX;
831 	}
832     }
833   else if (token->type == CPP_AT_NAME)
834     {
835       /* This only happens in Objective-C++; it must be a keyword.  */
836       token->type = CPP_KEYWORD;
837       switch (C_RID_CODE (token->u.value))
838 	{
839 	  /* Replace 'class' with '@class', 'private' with '@private',
840 	     etc.  This prevents confusion with the C++ keyword
841 	     'class', and makes the tokens consistent with other
842 	     Objective-C 'AT' keywords.  For example '@class' is
843 	     reported as RID_AT_CLASS which is consistent with
844 	     '@synchronized', which is reported as
845 	     RID_AT_SYNCHRONIZED.
846 	  */
847 	case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
848 	case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
849 	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
850 	case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
851 	case RID_THROW:     token->keyword = RID_AT_THROW; break;
852 	case RID_TRY:       token->keyword = RID_AT_TRY; break;
853 	case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
854 	case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
855 	default:            token->keyword = C_RID_CODE (token->u.value);
856 	}
857     }
858 }
859 
860 /* Update the globals input_location and the input file stack from TOKEN.  */
861 static inline void
cp_lexer_set_source_position_from_token(cp_token * token)862 cp_lexer_set_source_position_from_token (cp_token *token)
863 {
864   if (token->type != CPP_EOF)
865     {
866       input_location = token->location;
867     }
868 }
869 
870 /* Update the globals input_location and the input file stack from LEXER.  */
871 static inline void
cp_lexer_set_source_position(cp_lexer * lexer)872 cp_lexer_set_source_position (cp_lexer *lexer)
873 {
874   cp_token *token = cp_lexer_peek_token (lexer);
875   cp_lexer_set_source_position_from_token (token);
876 }
877 
878 /* Return a pointer to the next token in the token stream, but do not
879    consume it.  */
880 
881 static inline cp_token *
cp_lexer_peek_token(cp_lexer * lexer)882 cp_lexer_peek_token (cp_lexer *lexer)
883 {
884   if (cp_lexer_debugging_p (lexer))
885     {
886       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
887       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
888       putc ('\n', cp_lexer_debug_stream);
889     }
890   return lexer->next_token;
891 }
892 
893 /* Return true if the next token has the indicated TYPE.  */
894 
895 static inline bool
cp_lexer_next_token_is(cp_lexer * lexer,enum cpp_ttype type)896 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
897 {
898   return cp_lexer_peek_token (lexer)->type == type;
899 }
900 
901 /* Return true if the next token does not have the indicated TYPE.  */
902 
903 static inline bool
cp_lexer_next_token_is_not(cp_lexer * lexer,enum cpp_ttype type)904 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
905 {
906   return !cp_lexer_next_token_is (lexer, type);
907 }
908 
909 /* Return true if the next token is the indicated KEYWORD.  */
910 
911 static inline bool
cp_lexer_next_token_is_keyword(cp_lexer * lexer,enum rid keyword)912 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
913 {
914   return cp_lexer_peek_token (lexer)->keyword == keyword;
915 }
916 
917 static inline bool
cp_lexer_nth_token_is(cp_lexer * lexer,size_t n,enum cpp_ttype type)918 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
919 {
920   return cp_lexer_peek_nth_token (lexer, n)->type == type;
921 }
922 
923 static inline bool
cp_lexer_nth_token_is_keyword(cp_lexer * lexer,size_t n,enum rid keyword)924 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
925 {
926   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
927 }
928 
929 /* Return true if KEYWORD can start a decl-specifier.  */
930 
931 bool
cp_keyword_starts_decl_specifier_p(enum rid keyword)932 cp_keyword_starts_decl_specifier_p (enum rid keyword)
933 {
934   switch (keyword)
935     {
936       /* auto specifier: storage-class-specifier in C++,
937          simple-type-specifier in C++0x.  */
938     case RID_AUTO:
939       /* Storage classes.  */
940     case RID_REGISTER:
941     case RID_STATIC:
942     case RID_EXTERN:
943     case RID_MUTABLE:
944     case RID_THREAD:
945       /* Elaborated type specifiers.  */
946     case RID_ENUM:
947     case RID_CLASS:
948     case RID_STRUCT:
949     case RID_UNION:
950     case RID_TYPENAME:
951       /* Simple type specifiers.  */
952     case RID_CHAR:
953     case RID_CHAR8:
954     case RID_CHAR16:
955     case RID_CHAR32:
956     case RID_WCHAR:
957     case RID_BOOL:
958     case RID_SHORT:
959     case RID_INT:
960     case RID_LONG:
961     case RID_SIGNED:
962     case RID_UNSIGNED:
963     case RID_FLOAT:
964     case RID_DOUBLE:
965     case RID_VOID:
966       /* GNU extensions.  */
967     case RID_ATTRIBUTE:
968     case RID_TYPEOF:
969       /* C++0x extensions.  */
970     case RID_DECLTYPE:
971     case RID_UNDERLYING_TYPE:
972     case RID_CONSTEXPR:
973       return true;
974 
975     default:
976       if (keyword >= RID_FIRST_INT_N
977 	  && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
978 	  && int_n_enabled_p[keyword - RID_FIRST_INT_N])
979 	return true;
980       return false;
981     }
982 }
983 
984 /* Return true if the next token is a keyword for a decl-specifier.  */
985 
986 static bool
cp_lexer_next_token_is_decl_specifier_keyword(cp_lexer * lexer)987 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
988 {
989   cp_token *token;
990 
991   token = cp_lexer_peek_token (lexer);
992   return cp_keyword_starts_decl_specifier_p (token->keyword);
993 }
994 
995 /* Returns TRUE iff the token T begins a decltype type.  */
996 
997 static bool
token_is_decltype(cp_token * t)998 token_is_decltype (cp_token *t)
999 {
1000   return (t->keyword == RID_DECLTYPE
1001 	  || t->type == CPP_DECLTYPE);
1002 }
1003 
1004 /* Returns TRUE iff the next token begins a decltype type.  */
1005 
1006 static bool
cp_lexer_next_token_is_decltype(cp_lexer * lexer)1007 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1008 {
1009   cp_token *t = cp_lexer_peek_token (lexer);
1010   return token_is_decltype (t);
1011 }
1012 
1013 /* Called when processing a token with tree_check_value; perform or defer the
1014    associated checks and return the value.  */
1015 
1016 static tree
saved_checks_value(struct tree_check * check_value)1017 saved_checks_value (struct tree_check *check_value)
1018 {
1019   /* Perform any access checks that were deferred.  */
1020   vec<deferred_access_check, va_gc> *checks;
1021   deferred_access_check *chk;
1022   checks = check_value->checks;
1023   if (checks)
1024     {
1025       int i;
1026       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1027 	perform_or_defer_access_check (chk->binfo,
1028 				       chk->decl,
1029 				       chk->diag_decl, tf_warning_or_error);
1030     }
1031   /* Return the stored value.  */
1032   return check_value->value;
1033 }
1034 
1035 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1036    then this is precisely equivalent to cp_lexer_peek_token (except
1037    that it is not inline).  One would like to disallow that case, but
1038    there is one case (cp_parser_nth_token_starts_template_id) where
1039    the caller passes a variable for N and it might be 1.  */
1040 
1041 static cp_token *
cp_lexer_peek_nth_token(cp_lexer * lexer,size_t n)1042 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1043 {
1044   cp_token *token;
1045 
1046   /* N is 1-based, not zero-based.  */
1047   gcc_assert (n > 0);
1048 
1049   if (cp_lexer_debugging_p (lexer))
1050     fprintf (cp_lexer_debug_stream,
1051 	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
1052 
1053   --n;
1054   token = lexer->next_token;
1055   gcc_assert (!n || token != &eof_token);
1056   while (n != 0)
1057     {
1058       ++token;
1059       if (token == lexer->last_token)
1060 	{
1061 	  token = &eof_token;
1062 	  break;
1063 	}
1064 
1065       if (!token->purged_p)
1066 	--n;
1067     }
1068 
1069   if (cp_lexer_debugging_p (lexer))
1070     {
1071       cp_lexer_print_token (cp_lexer_debug_stream, token);
1072       putc ('\n', cp_lexer_debug_stream);
1073     }
1074 
1075   return token;
1076 }
1077 
1078 /* Return the next token, and advance the lexer's next_token pointer
1079    to point to the next non-purged token.  */
1080 
1081 static cp_token *
cp_lexer_consume_token(cp_lexer * lexer)1082 cp_lexer_consume_token (cp_lexer* lexer)
1083 {
1084   cp_token *token = lexer->next_token;
1085 
1086   gcc_assert (token != &eof_token);
1087   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1088 
1089   do
1090     {
1091       lexer->next_token++;
1092       if (lexer->next_token == lexer->last_token)
1093 	{
1094 	  lexer->next_token = &eof_token;
1095 	  break;
1096 	}
1097 
1098     }
1099   while (lexer->next_token->purged_p);
1100 
1101   cp_lexer_set_source_position_from_token (token);
1102 
1103   /* Provide debugging output.  */
1104   if (cp_lexer_debugging_p (lexer))
1105     {
1106       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1107       cp_lexer_print_token (cp_lexer_debug_stream, token);
1108       putc ('\n', cp_lexer_debug_stream);
1109     }
1110 
1111   return token;
1112 }
1113 
1114 /* Permanently remove the next token from the token stream, and
1115    advance the next_token pointer to refer to the next non-purged
1116    token.  */
1117 
1118 static void
cp_lexer_purge_token(cp_lexer * lexer)1119 cp_lexer_purge_token (cp_lexer *lexer)
1120 {
1121   cp_token *tok = lexer->next_token;
1122 
1123   gcc_assert (tok != &eof_token);
1124   tok->purged_p = true;
1125   tok->location = UNKNOWN_LOCATION;
1126   tok->u.value = NULL_TREE;
1127   tok->keyword = RID_MAX;
1128 
1129   do
1130     {
1131       tok++;
1132       if (tok == lexer->last_token)
1133 	{
1134 	  tok = &eof_token;
1135 	  break;
1136 	}
1137     }
1138   while (tok->purged_p);
1139   lexer->next_token = tok;
1140 }
1141 
1142 /* Permanently remove all tokens after TOK, up to, but not
1143    including, the token that will be returned next by
1144    cp_lexer_peek_token.  */
1145 
1146 static void
cp_lexer_purge_tokens_after(cp_lexer * lexer,cp_token * tok)1147 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1148 {
1149   cp_token *peek = lexer->next_token;
1150 
1151   if (peek == &eof_token)
1152     peek = lexer->last_token;
1153 
1154   gcc_assert (tok < peek);
1155 
1156   for ( tok += 1; tok != peek; tok += 1)
1157     {
1158       tok->purged_p = true;
1159       tok->location = UNKNOWN_LOCATION;
1160       tok->u.value = NULL_TREE;
1161       tok->keyword = RID_MAX;
1162     }
1163 }
1164 
1165 /* Begin saving tokens.  All tokens consumed after this point will be
1166    preserved.  */
1167 
1168 static void
cp_lexer_save_tokens(cp_lexer * lexer)1169 cp_lexer_save_tokens (cp_lexer* lexer)
1170 {
1171   /* Provide debugging output.  */
1172   if (cp_lexer_debugging_p (lexer))
1173     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1174 
1175   lexer->saved_tokens.safe_push (lexer->next_token);
1176 }
1177 
1178 /* Commit to the portion of the token stream most recently saved.  */
1179 
1180 static void
cp_lexer_commit_tokens(cp_lexer * lexer)1181 cp_lexer_commit_tokens (cp_lexer* lexer)
1182 {
1183   /* Provide debugging output.  */
1184   if (cp_lexer_debugging_p (lexer))
1185     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1186 
1187   lexer->saved_tokens.pop ();
1188 }
1189 
1190 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1191    to the token stream.  Stop saving tokens.  */
1192 
1193 static void
cp_lexer_rollback_tokens(cp_lexer * lexer)1194 cp_lexer_rollback_tokens (cp_lexer* lexer)
1195 {
1196   /* Provide debugging output.  */
1197   if (cp_lexer_debugging_p (lexer))
1198     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1199 
1200   lexer->next_token = lexer->saved_tokens.pop ();
1201 }
1202 
1203 /* RAII wrapper around the above functions, with sanity checking.  Creating
1204    a variable saves tokens, which are committed when the variable is
1205    destroyed unless they are explicitly rolled back by calling the rollback
1206    member function.  */
1207 
1208 struct saved_token_sentinel
1209 {
1210   cp_lexer *lexer;
1211   unsigned len;
1212   bool commit;
saved_token_sentinelsaved_token_sentinel1213   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1214   {
1215     len = lexer->saved_tokens.length ();
1216     cp_lexer_save_tokens (lexer);
1217   }
rollbacksaved_token_sentinel1218   void rollback ()
1219   {
1220     cp_lexer_rollback_tokens (lexer);
1221     commit = false;
1222   }
~saved_token_sentinelsaved_token_sentinel1223   ~saved_token_sentinel()
1224   {
1225     if (commit)
1226       cp_lexer_commit_tokens (lexer);
1227     gcc_assert (lexer->saved_tokens.length () == len);
1228   }
1229 };
1230 
1231 /* Print a representation of the TOKEN on the STREAM.  */
1232 
1233 static void
cp_lexer_print_token(FILE * stream,cp_token * token)1234 cp_lexer_print_token (FILE * stream, cp_token *token)
1235 {
1236   /* We don't use cpp_type2name here because the parser defines
1237      a few tokens of its own.  */
1238   static const char *const token_names[] = {
1239     /* cpplib-defined token types */
1240 #define OP(e, s) #e,
1241 #define TK(e, s) #e,
1242     TTYPE_TABLE
1243 #undef OP
1244 #undef TK
1245     /* C++ parser token types - see "Manifest constants", above.  */
1246     "KEYWORD",
1247     "TEMPLATE_ID",
1248     "NESTED_NAME_SPECIFIER",
1249   };
1250 
1251   /* For some tokens, print the associated data.  */
1252   switch (token->type)
1253     {
1254     case CPP_KEYWORD:
1255       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1256 	 For example, `struct' is mapped to an INTEGER_CST.  */
1257       if (!identifier_p (token->u.value))
1258 	break;
1259       /* fall through */
1260     case CPP_NAME:
1261       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1262       break;
1263 
1264     case CPP_STRING:
1265     case CPP_STRING16:
1266     case CPP_STRING32:
1267     case CPP_WSTRING:
1268     case CPP_UTF8STRING:
1269       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1270       break;
1271 
1272     case CPP_NUMBER:
1273       print_generic_expr (stream, token->u.value);
1274       break;
1275 
1276     default:
1277       /* If we have a name for the token, print it out.  Otherwise, we
1278 	 simply give the numeric code.  */
1279       if (token->type < ARRAY_SIZE(token_names))
1280 	fputs (token_names[token->type], stream);
1281       else
1282 	fprintf (stream, "[%d]", token->type);
1283       break;
1284     }
1285 }
1286 
1287 DEBUG_FUNCTION void
debug(cp_token & ref)1288 debug (cp_token &ref)
1289 {
1290   cp_lexer_print_token (stderr, &ref);
1291   fprintf (stderr, "\n");
1292 }
1293 
1294 DEBUG_FUNCTION void
debug(cp_token * ptr)1295 debug (cp_token *ptr)
1296 {
1297   if (ptr)
1298     debug (*ptr);
1299   else
1300     fprintf (stderr, "<nil>\n");
1301 }
1302 
1303 
1304 /* Start emitting debugging information.  */
1305 
1306 static void
cp_lexer_start_debugging(cp_lexer * lexer)1307 cp_lexer_start_debugging (cp_lexer* lexer)
1308 {
1309   if (!LEXER_DEBUGGING_ENABLED_P)
1310     fatal_error (input_location,
1311 		 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1312 
1313   lexer->debugging_p = true;
1314   cp_lexer_debug_stream = stderr;
1315 }
1316 
1317 /* Stop emitting debugging information.  */
1318 
1319 static void
cp_lexer_stop_debugging(cp_lexer * lexer)1320 cp_lexer_stop_debugging (cp_lexer* lexer)
1321 {
1322   if (!LEXER_DEBUGGING_ENABLED_P)
1323     fatal_error (input_location,
1324 		 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1325 
1326   lexer->debugging_p = false;
1327   cp_lexer_debug_stream = NULL;
1328 }
1329 
1330 /* Create a new cp_token_cache, representing a range of tokens.  */
1331 
1332 static cp_token_cache *
cp_token_cache_new(cp_token * first,cp_token * last)1333 cp_token_cache_new (cp_token *first, cp_token *last)
1334 {
1335   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1336   cache->first = first;
1337   cache->last = last;
1338   return cache;
1339 }
1340 
1341 /* Diagnose if #pragma omp declare simd isn't followed immediately
1342    by function declaration or definition.  */
1343 
1344 static inline void
cp_ensure_no_omp_declare_simd(cp_parser * parser)1345 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1346 {
1347   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1348     {
1349       error ("%<#pragma omp declare simd%> not immediately followed by "
1350 	     "function declaration or definition");
1351       parser->omp_declare_simd = NULL;
1352     }
1353 }
1354 
1355 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1356    and put that into "omp declare simd" attribute.  */
1357 
1358 static inline void
cp_finalize_omp_declare_simd(cp_parser * parser,tree fndecl)1359 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1360 {
1361   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1362     {
1363       if (fndecl == error_mark_node)
1364 	{
1365 	  parser->omp_declare_simd = NULL;
1366 	  return;
1367 	}
1368       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1369 	{
1370 	  cp_ensure_no_omp_declare_simd (parser);
1371 	  return;
1372 	}
1373     }
1374 }
1375 
1376 /* Diagnose if #pragma acc routine isn't followed immediately by function
1377    declaration or definition.  */
1378 
1379 static inline void
cp_ensure_no_oacc_routine(cp_parser * parser)1380 cp_ensure_no_oacc_routine (cp_parser *parser)
1381 {
1382   if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1383     {
1384       error_at (parser->oacc_routine->loc,
1385 		"%<#pragma acc routine%> not immediately followed by "
1386 		"function declaration or definition");
1387       parser->oacc_routine = NULL;
1388     }
1389 }
1390 
1391 /* Decl-specifiers.  */
1392 
1393 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1394 
1395 static void
clear_decl_specs(cp_decl_specifier_seq * decl_specs)1396 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1397 {
1398   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1399 }
1400 
1401 /* Declarators.  */
1402 
1403 /* Nothing other than the parser should be creating declarators;
1404    declarators are a semi-syntactic representation of C++ entities.
1405    Other parts of the front end that need to create entities (like
1406    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1407 
1408 static cp_declarator *make_call_declarator
1409   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1410 static cp_declarator *make_array_declarator
1411   (cp_declarator *, tree);
1412 static cp_declarator *make_pointer_declarator
1413   (cp_cv_quals, cp_declarator *, tree);
1414 static cp_declarator *make_reference_declarator
1415   (cp_cv_quals, cp_declarator *, bool, tree);
1416 static cp_declarator *make_ptrmem_declarator
1417   (cp_cv_quals, tree, cp_declarator *, tree);
1418 
1419 /* An erroneous declarator.  */
1420 static cp_declarator *cp_error_declarator;
1421 
1422 /* The obstack on which declarators and related data structures are
1423    allocated.  */
1424 static struct obstack declarator_obstack;
1425 
1426 /* Alloc BYTES from the declarator memory pool.  */
1427 
1428 static inline void *
alloc_declarator(size_t bytes)1429 alloc_declarator (size_t bytes)
1430 {
1431   return obstack_alloc (&declarator_obstack, bytes);
1432 }
1433 
1434 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1435    common to all declarators.  */
1436 
1437 static cp_declarator *
make_declarator(cp_declarator_kind kind)1438 make_declarator (cp_declarator_kind kind)
1439 {
1440   cp_declarator *declarator;
1441 
1442   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1443   declarator->kind = kind;
1444   declarator->parenthesized = UNKNOWN_LOCATION;
1445   declarator->attributes = NULL_TREE;
1446   declarator->std_attributes = NULL_TREE;
1447   declarator->declarator = NULL;
1448   declarator->parameter_pack_p = false;
1449   declarator->id_loc = UNKNOWN_LOCATION;
1450 
1451   return declarator;
1452 }
1453 
1454 /* Make a declarator for a generalized identifier.  If
1455    QUALIFYING_SCOPE is non-NULL, the identifier is
1456    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1457    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1458    is, if any.   */
1459 
1460 static cp_declarator *
make_id_declarator(tree qualifying_scope,tree unqualified_name,special_function_kind sfk,location_t id_location)1461 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1462 		    special_function_kind sfk, location_t id_location)
1463 {
1464   cp_declarator *declarator;
1465 
1466   /* It is valid to write:
1467 
1468        class C { void f(); };
1469        typedef C D;
1470        void D::f();
1471 
1472      The standard is not clear about whether `typedef const C D' is
1473      legal; as of 2002-09-15 the committee is considering that
1474      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1475      well.  */
1476   if (qualifying_scope && TYPE_P (qualifying_scope))
1477     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1478 
1479   gcc_assert (identifier_p (unqualified_name)
1480 	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1481 	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1482 
1483   declarator = make_declarator (cdk_id);
1484   declarator->u.id.qualifying_scope = qualifying_scope;
1485   declarator->u.id.unqualified_name = unqualified_name;
1486   declarator->u.id.sfk = sfk;
1487   declarator->id_loc = id_location;
1488 
1489   return declarator;
1490 }
1491 
1492 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1493    of modifiers such as const or volatile to apply to the pointer
1494    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1495    appertain to the pointer or reference.  */
1496 
1497 cp_declarator *
make_pointer_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,tree attributes)1498 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1499 			 tree attributes)
1500 {
1501   cp_declarator *declarator;
1502 
1503   declarator = make_declarator (cdk_pointer);
1504   declarator->declarator = target;
1505   declarator->u.pointer.qualifiers = cv_qualifiers;
1506   declarator->u.pointer.class_type = NULL_TREE;
1507   if (target)
1508     {
1509       declarator->id_loc = target->id_loc;
1510       declarator->parameter_pack_p = target->parameter_pack_p;
1511       target->parameter_pack_p = false;
1512     }
1513   else
1514     declarator->parameter_pack_p = false;
1515 
1516   declarator->std_attributes = attributes;
1517 
1518   return declarator;
1519 }
1520 
1521 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1522    represent the attributes that appertain to the pointer or
1523    reference.  */
1524 
1525 cp_declarator *
make_reference_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,bool rvalue_ref,tree attributes)1526 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1527 			   bool rvalue_ref, tree attributes)
1528 {
1529   cp_declarator *declarator;
1530 
1531   declarator = make_declarator (cdk_reference);
1532   declarator->declarator = target;
1533   declarator->u.reference.qualifiers = cv_qualifiers;
1534   declarator->u.reference.rvalue_ref = rvalue_ref;
1535   if (target)
1536     {
1537       declarator->id_loc = target->id_loc;
1538       declarator->parameter_pack_p = target->parameter_pack_p;
1539       target->parameter_pack_p = false;
1540     }
1541   else
1542     declarator->parameter_pack_p = false;
1543 
1544   declarator->std_attributes = attributes;
1545 
1546   return declarator;
1547 }
1548 
1549 /* Like make_pointer_declarator -- but for a pointer to a non-static
1550    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1551    appertain to the pointer or reference.  */
1552 
1553 cp_declarator *
make_ptrmem_declarator(cp_cv_quals cv_qualifiers,tree class_type,cp_declarator * pointee,tree attributes)1554 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1555 			cp_declarator *pointee,
1556 			tree attributes)
1557 {
1558   cp_declarator *declarator;
1559 
1560   declarator = make_declarator (cdk_ptrmem);
1561   declarator->declarator = pointee;
1562   declarator->u.pointer.qualifiers = cv_qualifiers;
1563   declarator->u.pointer.class_type = class_type;
1564 
1565   if (pointee)
1566     {
1567       declarator->parameter_pack_p = pointee->parameter_pack_p;
1568       pointee->parameter_pack_p = false;
1569     }
1570   else
1571     declarator->parameter_pack_p = false;
1572 
1573   declarator->std_attributes = attributes;
1574 
1575   return declarator;
1576 }
1577 
1578 /* Make a declarator for the function given by TARGET, with the
1579    indicated PARMS.  The CV_QUALIFIERS apply to the function, as in
1580    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1581    indicates what exceptions can be thrown.  */
1582 
1583 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)1584 make_call_declarator (cp_declarator *target,
1585 		      tree parms,
1586 		      cp_cv_quals cv_qualifiers,
1587 		      cp_virt_specifiers virt_specifiers,
1588 		      cp_ref_qualifier ref_qualifier,
1589 		      tree tx_qualifier,
1590 		      tree exception_specification,
1591 		      tree late_return_type,
1592 		      tree requires_clause)
1593 {
1594   cp_declarator *declarator;
1595 
1596   declarator = make_declarator (cdk_function);
1597   declarator->declarator = target;
1598   declarator->u.function.parameters = parms;
1599   declarator->u.function.qualifiers = cv_qualifiers;
1600   declarator->u.function.virt_specifiers = virt_specifiers;
1601   declarator->u.function.ref_qualifier = ref_qualifier;
1602   declarator->u.function.tx_qualifier = tx_qualifier;
1603   declarator->u.function.exception_specification = exception_specification;
1604   declarator->u.function.late_return_type = late_return_type;
1605   declarator->u.function.requires_clause = requires_clause;
1606   if (target)
1607     {
1608       declarator->id_loc = target->id_loc;
1609       declarator->parameter_pack_p = target->parameter_pack_p;
1610       target->parameter_pack_p = false;
1611     }
1612   else
1613     declarator->parameter_pack_p = false;
1614 
1615   return declarator;
1616 }
1617 
1618 /* Make a declarator for an array of BOUNDS elements, each of which is
1619    defined by ELEMENT.  */
1620 
1621 cp_declarator *
make_array_declarator(cp_declarator * element,tree bounds)1622 make_array_declarator (cp_declarator *element, tree bounds)
1623 {
1624   cp_declarator *declarator;
1625 
1626   declarator = make_declarator (cdk_array);
1627   declarator->declarator = element;
1628   declarator->u.array.bounds = bounds;
1629   if (element)
1630     {
1631       declarator->id_loc = element->id_loc;
1632       declarator->parameter_pack_p = element->parameter_pack_p;
1633       element->parameter_pack_p = false;
1634     }
1635   else
1636     declarator->parameter_pack_p = false;
1637 
1638   return declarator;
1639 }
1640 
1641 /* Determine whether the declarator we've seen so far can be a
1642    parameter pack, when followed by an ellipsis.  */
1643 static bool
declarator_can_be_parameter_pack(cp_declarator * declarator)1644 declarator_can_be_parameter_pack (cp_declarator *declarator)
1645 {
1646   if (declarator && declarator->parameter_pack_p)
1647     /* We already saw an ellipsis.  */
1648     return false;
1649 
1650   /* Search for a declarator name, or any other declarator that goes
1651      after the point where the ellipsis could appear in a parameter
1652      pack. If we find any of these, then this declarator cannot be
1653      made into a parameter pack.  */
1654   bool found = false;
1655   while (declarator && !found)
1656     {
1657       switch ((int)declarator->kind)
1658 	{
1659 	case cdk_id:
1660 	case cdk_array:
1661 	case cdk_decomp:
1662 	  found = true;
1663 	  break;
1664 
1665 	case cdk_error:
1666 	  return true;
1667 
1668 	default:
1669 	  declarator = declarator->declarator;
1670 	  break;
1671 	}
1672     }
1673 
1674   return !found;
1675 }
1676 
1677 cp_parameter_declarator *no_parameters;
1678 
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680    DECLARATOR and DEFAULT_ARGUMENT.  */
1681 
1682 cp_parameter_declarator *
1683 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1684 			   cp_declarator *declarator,
1685 			   tree default_argument,
1686 			   location_t loc,
1687 			   bool template_parameter_pack_p = false)
1688 {
1689   cp_parameter_declarator *parameter;
1690 
1691   parameter = ((cp_parameter_declarator *)
1692 	       alloc_declarator (sizeof (cp_parameter_declarator)));
1693   parameter->next = NULL;
1694   if (decl_specifiers)
1695     parameter->decl_specifiers = *decl_specifiers;
1696   else
1697     clear_decl_specs (&parameter->decl_specifiers);
1698   parameter->declarator = declarator;
1699   parameter->default_argument = default_argument;
1700   parameter->template_parameter_pack_p = template_parameter_pack_p;
1701   parameter->loc = loc;
1702 
1703   return parameter;
1704 }
1705 
1706 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1707 
1708 static bool
function_declarator_p(const cp_declarator * declarator)1709 function_declarator_p (const cp_declarator *declarator)
1710 {
1711   while (declarator)
1712     {
1713       if (declarator->kind == cdk_function
1714 	  && declarator->declarator->kind == cdk_id)
1715 	return true;
1716       if (declarator->kind == cdk_id
1717 	  || declarator->kind == cdk_decomp
1718 	  || declarator->kind == cdk_error)
1719 	return false;
1720       declarator = declarator->declarator;
1721     }
1722   return false;
1723 }
1724 
1725 /* The parser.  */
1726 
1727 /* Overview
1728    --------
1729 
1730    A cp_parser parses the token stream as specified by the C++
1731    grammar.  Its job is purely parsing, not semantic analysis.  For
1732    example, the parser breaks the token stream into declarators,
1733    expressions, statements, and other similar syntactic constructs.
1734    It does not check that the types of the expressions on either side
1735    of an assignment-statement are compatible, or that a function is
1736    not declared with a parameter of type `void'.
1737 
1738    The parser invokes routines elsewhere in the compiler to perform
1739    semantic analysis and to build up the abstract syntax tree for the
1740    code processed.
1741 
1742    The parser (and the template instantiation code, which is, in a
1743    way, a close relative of parsing) are the only parts of the
1744    compiler that should be calling push_scope and pop_scope, or
1745    related functions.  The parser (and template instantiation code)
1746    keeps track of what scope is presently active; everything else
1747    should simply honor that.  (The code that generates static
1748    initializers may also need to set the scope, in order to check
1749    access control correctly when emitting the initializers.)
1750 
1751    Methodology
1752    -----------
1753 
1754    The parser is of the standard recursive-descent variety.  Upcoming
1755    tokens in the token stream are examined in order to determine which
1756    production to use when parsing a non-terminal.  Some C++ constructs
1757    require arbitrary look ahead to disambiguate.  For example, it is
1758    impossible, in the general case, to tell whether a statement is an
1759    expression or declaration without scanning the entire statement.
1760    Therefore, the parser is capable of "parsing tentatively."  When the
1761    parser is not sure what construct comes next, it enters this mode.
1762    Then, while we attempt to parse the construct, the parser queues up
1763    error messages, rather than issuing them immediately, and saves the
1764    tokens it consumes.  If the construct is parsed successfully, the
1765    parser "commits", i.e., it issues any queued error messages and
1766    the tokens that were being preserved are permanently discarded.
1767    If, however, the construct is not parsed successfully, the parser
1768    rolls back its state completely so that it can resume parsing using
1769    a different alternative.
1770 
1771    Future Improvements
1772    -------------------
1773 
1774    The performance of the parser could probably be improved substantially.
1775    We could often eliminate the need to parse tentatively by looking ahead
1776    a little bit.  In some places, this approach might not entirely eliminate
1777    the need to parse tentatively, but it might still speed up the average
1778    case.  */
1779 
1780 /* Flags that are passed to some parsing functions.  These values can
1781    be bitwise-ored together.  */
1782 
1783 enum
1784 {
1785   /* No flags.  */
1786   CP_PARSER_FLAGS_NONE = 0x0,
1787   /* The construct is optional.  If it is not present, then no error
1788      should be issued.  */
1789   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1790   /* When parsing a type-specifier, treat user-defined type-names
1791      as non-type identifiers.  */
1792   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1793   /* When parsing a type-specifier, do not try to parse a class-specifier
1794      or enum-specifier.  */
1795   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1796   /* When parsing a decl-specifier-seq, only allow type-specifier or
1797      constexpr.  */
1798   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1799   /* When parsing a decl-specifier-seq, only allow mutable or constexpr.  */
1800   CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1801   /* When parsing a decl-specifier-seq, allow missing typename.  */
1802   CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1803 };
1804 
1805 /* This type is used for parameters and variables which hold
1806    combinations of the above flags.  */
1807 typedef int cp_parser_flags;
1808 
1809 /* The different kinds of declarators we want to parse.  */
1810 
1811 enum cp_parser_declarator_kind
1812 {
1813   /* We want an abstract declarator.  */
1814   CP_PARSER_DECLARATOR_ABSTRACT,
1815   /* We want a named declarator.  */
1816   CP_PARSER_DECLARATOR_NAMED,
1817   /* We don't mind, but the name must be an unqualified-id.  */
1818   CP_PARSER_DECLARATOR_EITHER
1819 };
1820 
1821 /* The precedence values used to parse binary expressions.  The minimum value
1822    of PREC must be 1, because zero is reserved to quickly discriminate
1823    binary operators from other tokens.  */
1824 
1825 enum cp_parser_prec
1826 {
1827   PREC_NOT_OPERATOR,
1828   PREC_LOGICAL_OR_EXPRESSION,
1829   PREC_LOGICAL_AND_EXPRESSION,
1830   PREC_INCLUSIVE_OR_EXPRESSION,
1831   PREC_EXCLUSIVE_OR_EXPRESSION,
1832   PREC_AND_EXPRESSION,
1833   PREC_EQUALITY_EXPRESSION,
1834   PREC_RELATIONAL_EXPRESSION,
1835   PREC_SHIFT_EXPRESSION,
1836   PREC_ADDITIVE_EXPRESSION,
1837   PREC_MULTIPLICATIVE_EXPRESSION,
1838   PREC_PM_EXPRESSION,
1839   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841 
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843    precedence value.  */
1844 
1845 struct cp_parser_binary_operations_map_node
1846 {
1847   /* The token type.  */
1848   enum cpp_ttype token_type;
1849   /* The corresponding tree code.  */
1850   enum tree_code tree_type;
1851   /* The precedence of this operator.  */
1852   enum cp_parser_prec prec;
1853 };
1854 
1855 struct cp_parser_expression_stack_entry
1856 {
1857   /* Left hand side of the binary operation we are currently
1858      parsing.  */
1859   cp_expr lhs;
1860   /* Original tree code for left hand side, if it was a binary
1861      expression itself (used for -Wparentheses).  */
1862   enum tree_code lhs_type;
1863   /* Tree code for the binary operation we are parsing.  */
1864   enum tree_code tree_type;
1865   /* Precedence of the binary operation we are parsing.  */
1866   enum cp_parser_prec prec;
1867   /* Location of the binary operation we are parsing.  */
1868   location_t loc;
1869 };
1870 
1871 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1872    entries because precedence levels on the stack are monotonically
1873    increasing.  */
1874 typedef struct cp_parser_expression_stack_entry
1875   cp_parser_expression_stack[NUM_PREC_VALUES];
1876 
1877 /* Prototypes.  */
1878 
1879 /* Constructors and destructors.  */
1880 
1881 static cp_parser_context *cp_parser_context_new
1882   (cp_parser_context *);
1883 
1884 /* Class variables.  */
1885 
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887 
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889    Transformed into an associative array (binops_by_token) by
1890    cp_parser_new.  */
1891 
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895 
1896   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899 
1900   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902 
1903   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905 
1906   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910 
1911   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913 
1914   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915 
1916   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917 
1918   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919 
1920   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921 
1922   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924 
1925 /* The same as binops, but initialized by cp_parser_new so that
1926    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1927    for speed.  */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929 
1930 /* Constructors and destructors.  */
1931 
1932 /* Construct a new context.  The context below this one on the stack
1933    is given by NEXT.  */
1934 
1935 static cp_parser_context *
cp_parser_context_new(cp_parser_context * next)1936 cp_parser_context_new (cp_parser_context* next)
1937 {
1938   cp_parser_context *context;
1939 
1940   /* Allocate the storage.  */
1941   if (cp_parser_context_free_list != NULL)
1942     {
1943       /* Pull the first entry from the free list.  */
1944       context = cp_parser_context_free_list;
1945       cp_parser_context_free_list = context->next;
1946       memset (context, 0, sizeof (*context));
1947     }
1948   else
1949     context = ggc_cleared_alloc<cp_parser_context> ();
1950 
1951   /* No errors have occurred yet in this context.  */
1952   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953   /* If this is not the bottommost context, copy information that we
1954      need from the previous context.  */
1955   if (next)
1956     {
1957       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 	 expression, then we are parsing one in this context, too.  */
1959       context->object_type = next->object_type;
1960       /* Thread the stack.  */
1961       context->next = next;
1962     }
1963 
1964   return context;
1965 }
1966 
1967 /* Managing the unparsed function queues.  */
1968 
1969 #define unparsed_funs_with_default_args \
1970   parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972   parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974   parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976   parser->unparsed_queues->last ().classes
1977 
1978 static void
push_unparsed_function_queues(cp_parser * parser)1979 push_unparsed_function_queues (cp_parser *parser)
1980 {
1981   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982   vec_safe_push (parser->unparsed_queues, e);
1983 }
1984 
1985 static void
pop_unparsed_function_queues(cp_parser * parser)1986 pop_unparsed_function_queues (cp_parser *parser)
1987 {
1988   release_tree_vector (unparsed_funs_with_definitions);
1989   parser->unparsed_queues->pop ();
1990 }
1991 
1992 /* Prototypes.  */
1993 
1994 /* Constructors and destructors.  */
1995 
1996 static cp_parser *cp_parser_new
1997   (void);
1998 
1999 /* Routines to parse various constructs.
2000 
2001    Those that return `tree' will return the error_mark_node (rather
2002    than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003    Sometimes, they will return an ordinary node if error-recovery was
2004    attempted, even though a parse error occurred.  So, to check
2005    whether or not a parse error occurred, you should always use
2006    cp_parser_error_occurred.  If the construct is optional (indicated
2007    either by an `_opt' in the name of the function that does the
2008    parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009    the construct is not present.  */
2010 
2011 /* Lexical conventions [gram.lex]  */
2012 
2013 static cp_expr cp_parser_identifier
2014   (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016   (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018   (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020   (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022   (cp_parser *);
2023 
2024 /* Basic concepts [gram.basic]  */
2025 
2026 static void cp_parser_translation_unit (cp_parser *);
2027 
2028 /* Expressions [gram.expr]  */
2029 
2030 static cp_expr cp_parser_primary_expression
2031   (cp_parser *, bool, bool, bool, cp_id_kind *);
2032 static cp_expr cp_parser_id_expression
2033   (cp_parser *, bool, bool, bool *, bool, bool);
2034 static cp_expr cp_parser_unqualified_id
2035   (cp_parser *, bool, bool, bool, bool);
2036 static tree cp_parser_nested_name_specifier_opt
2037   (cp_parser *, bool, bool, bool, bool, bool = false);
2038 static tree cp_parser_nested_name_specifier
2039   (cp_parser *, bool, bool, bool, bool);
2040 static tree cp_parser_qualifying_entity
2041   (cp_parser *, bool, bool, bool, bool, bool);
2042 static cp_expr cp_parser_postfix_expression
2043   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2044 static tree cp_parser_postfix_open_square_expression
2045   (cp_parser *, tree, bool, bool);
2046 static tree cp_parser_postfix_dot_deref_expression
2047   (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2048 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2049   (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2050    bool = false);
2051 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2052 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053 static void cp_parser_pseudo_destructor_name
2054   (cp_parser *, tree, tree *, tree *);
2055 static cp_expr cp_parser_unary_expression
2056   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2058   (cp_token *);
2059 static tree cp_parser_has_attribute_expression
2060   (cp_parser *);
2061 static tree cp_parser_new_expression
2062   (cp_parser *);
2063 static vec<tree, va_gc> *cp_parser_new_placement
2064   (cp_parser *);
2065 static tree cp_parser_new_type_id
2066   (cp_parser *, tree *);
2067 static cp_declarator *cp_parser_new_declarator_opt
2068   (cp_parser *);
2069 static cp_declarator *cp_parser_direct_new_declarator
2070   (cp_parser *);
2071 static vec<tree, va_gc> *cp_parser_new_initializer
2072   (cp_parser *);
2073 static tree cp_parser_delete_expression
2074   (cp_parser *);
2075 static cp_expr cp_parser_cast_expression
2076   (cp_parser *, bool, bool, bool, cp_id_kind *);
2077 static cp_expr cp_parser_binary_expression
2078   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2079 static tree cp_parser_question_colon_clause
2080   (cp_parser *, cp_expr);
2081 static cp_expr cp_parser_assignment_expression
2082   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2083 static enum tree_code cp_parser_assignment_operator_opt
2084   (cp_parser *);
2085 static cp_expr cp_parser_expression
2086   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2087 static cp_expr cp_parser_constant_expression
2088   (cp_parser *, bool = false, bool * = NULL, bool = false);
2089 static cp_expr cp_parser_builtin_offsetof
2090   (cp_parser *);
2091 static cp_expr cp_parser_lambda_expression
2092   (cp_parser *);
2093 static void cp_parser_lambda_introducer
2094   (cp_parser *, tree);
2095 static bool cp_parser_lambda_declarator_opt
2096   (cp_parser *, tree);
2097 static void cp_parser_lambda_body
2098   (cp_parser *, tree);
2099 
2100 /* Statements [gram.stmt.stmt]  */
2101 
2102 static void cp_parser_statement
2103   (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2104 static void cp_parser_label_for_labeled_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_expression_statement
2107   (cp_parser *, tree);
2108 static tree cp_parser_compound_statement
2109   (cp_parser *, tree, int, bool);
2110 static void cp_parser_statement_seq_opt
2111   (cp_parser *, tree);
2112 static tree cp_parser_selection_statement
2113   (cp_parser *, bool *, vec<tree> *);
2114 static tree cp_parser_condition
2115   (cp_parser *);
2116 static tree cp_parser_iteration_statement
2117   (cp_parser *, bool *, bool, unsigned short);
2118 static bool cp_parser_init_statement
2119   (cp_parser *, tree *decl);
2120 static tree cp_parser_for
2121   (cp_parser *, bool, unsigned short);
2122 static tree cp_parser_c_for
2123   (cp_parser *, tree, tree, bool, unsigned short);
2124 static tree cp_parser_range_for
2125   (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2126 static void do_range_for_auto_deduction
2127   (tree, tree);
2128 static tree cp_parser_perform_range_for_lookup
2129   (tree, tree *, tree *);
2130 static tree cp_parser_range_for_member_function
2131   (tree, tree);
2132 static tree cp_parser_jump_statement
2133   (cp_parser *);
2134 static void cp_parser_declaration_statement
2135   (cp_parser *);
2136 
2137 static tree cp_parser_implicitly_scoped_statement
2138   (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2139 static void cp_parser_already_scoped_statement
2140   (cp_parser *, bool *, const token_indent_info &);
2141 
2142 /* Declarations [gram.dcl.dcl] */
2143 
2144 static void cp_parser_declaration_seq_opt
2145   (cp_parser *);
2146 static void cp_parser_declaration
2147   (cp_parser *);
2148 static void cp_parser_toplevel_declaration
2149   (cp_parser *);
2150 static void cp_parser_block_declaration
2151   (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153   (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157   (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159   (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162    int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166   (cp_parser *, bool);
2167 static tree cp_parser_nonclass_name
2168   (cp_parser* parser);
2169 static tree cp_parser_elaborated_type_specifier
2170   (cp_parser *, bool, bool);
2171 static tree cp_parser_enum_specifier
2172   (cp_parser *);
2173 static void cp_parser_enumerator_list
2174   (cp_parser *, tree);
2175 static void cp_parser_enumerator_definition
2176   (cp_parser *, tree);
2177 static tree cp_parser_namespace_name
2178   (cp_parser *);
2179 static void cp_parser_namespace_definition
2180   (cp_parser *);
2181 static void cp_parser_namespace_body
2182   (cp_parser *);
2183 static tree cp_parser_qualified_namespace_specifier
2184   (cp_parser *);
2185 static void cp_parser_namespace_alias_definition
2186   (cp_parser *);
2187 static bool cp_parser_using_declaration
2188   (cp_parser *, bool);
2189 static void cp_parser_using_directive
2190   (cp_parser *);
2191 static tree cp_parser_alias_declaration
2192   (cp_parser *);
2193 static void cp_parser_asm_definition
2194   (cp_parser *);
2195 static void cp_parser_linkage_specification
2196   (cp_parser *);
2197 static void cp_parser_static_assert
2198   (cp_parser *, bool);
2199 static tree cp_parser_decltype
2200   (cp_parser *);
2201 static tree cp_parser_decomposition_declaration
2202   (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2203 
2204 /* Declarators [gram.dcl.decl] */
2205 
2206 static tree cp_parser_init_declarator
2207   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2208    vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2209    location_t *, tree *);
2210 static cp_declarator *cp_parser_declarator
2211   (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2212    bool, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214   (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2215    bool);
2216 static enum tree_code cp_parser_ptr_operator
2217   (cp_parser *, tree *, cp_cv_quals *, tree *);
2218 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2219   (cp_parser *);
2220 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2221   (cp_parser *);
2222 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2223   (cp_parser *);
2224 static tree cp_parser_tx_qualifier_opt
2225   (cp_parser *);
2226 static tree cp_parser_late_return_type_opt
2227   (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2228 static tree cp_parser_declarator_id
2229   (cp_parser *, bool);
2230 static tree cp_parser_type_id
2231   (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2232 static tree cp_parser_template_type_arg
2233   (cp_parser *);
2234 static tree cp_parser_trailing_type_id (cp_parser *);
2235 static tree cp_parser_type_id_1
2236   (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2237 static void cp_parser_type_specifier_seq
2238   (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2239 static tree cp_parser_parameter_declaration_clause
2240   (cp_parser *, cp_parser_flags);
2241 static tree cp_parser_parameter_declaration_list
2242   (cp_parser *, cp_parser_flags);
2243 static cp_parameter_declarator *cp_parser_parameter_declaration
2244   (cp_parser *, cp_parser_flags, bool, bool *);
2245 static tree cp_parser_default_argument
2246   (cp_parser *, bool);
2247 static void cp_parser_function_body
2248   (cp_parser *, bool);
2249 static tree cp_parser_initializer
2250   (cp_parser *, bool *, bool *, bool = false);
2251 static cp_expr cp_parser_initializer_clause
2252   (cp_parser *, bool *);
2253 static cp_expr cp_parser_braced_list
2254   (cp_parser*, bool*);
2255 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2256   (cp_parser *, bool *, bool *);
2257 
2258 static void cp_parser_ctor_initializer_opt_and_function_body
2259   (cp_parser *, bool);
2260 
2261 static tree cp_parser_late_parsing_omp_declare_simd
2262   (cp_parser *, tree);
2263 
2264 static tree cp_parser_late_parsing_oacc_routine
2265   (cp_parser *, tree);
2266 
2267 static tree synthesize_implicit_template_parm
2268   (cp_parser *, tree);
2269 static tree finish_fully_implicit_template
2270   (cp_parser *, tree);
2271 static void abort_fully_implicit_template
2272   (cp_parser *);
2273 
2274 /* Classes [gram.class] */
2275 
2276 static tree cp_parser_class_name
2277   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279   (cp_parser *);
2280 static tree cp_parser_class_head
2281   (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283   (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285   (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287   (cp_parser *);
2288 static void cp_parser_member_declaration
2289   (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291   (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293   (cp_parser *);
2294 
2295 /* Derived classes [gram.class.derived] */
2296 
2297 static tree cp_parser_base_clause
2298   (cp_parser *);
2299 static tree cp_parser_base_specifier
2300   (cp_parser *);
2301 
2302 /* Special member functions [gram.special] */
2303 
2304 static tree cp_parser_conversion_function_id
2305   (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307   (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309   (cp_parser *);
2310 static void cp_parser_ctor_initializer_opt
2311   (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313   (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315   (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317   (cp_parser *);
2318 
2319 /* Overloading [gram.over] */
2320 
2321 static cp_expr cp_parser_operator_function_id
2322   (cp_parser *);
2323 static cp_expr cp_parser_operator
2324   (cp_parser *, location_t);
2325 
2326 /* Templates [gram.temp] */
2327 
2328 static void cp_parser_template_declaration
2329   (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331   (cp_parser *);
2332 static tree cp_parser_template_parameter
2333   (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335   (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337   (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341   (cp_parser *);
2342 static tree cp_parser_template_argument
2343   (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345   (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347   (cp_parser *);
2348 
2349 /* Exception handling [gram.exception] */
2350 
2351 static tree cp_parser_try_block
2352   (cp_parser *);
2353 static void cp_parser_function_try_block
2354   (cp_parser *);
2355 static void cp_parser_handler_seq
2356   (cp_parser *);
2357 static void cp_parser_handler
2358   (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360   (cp_parser *);
2361 static tree cp_parser_throw_expression
2362   (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364   (cp_parser *);
2365 static tree cp_parser_type_id_list
2366   (cp_parser *);
2367 
2368 /* GNU Extensions */
2369 
2370 static tree cp_parser_asm_specification_opt
2371   (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373   (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375   (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377   (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379   (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381   (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383   (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385   (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387   (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389   (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391   (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393   (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395   (cp_parser *, bool = false);
2396 static tree cp_parser_std_attribute
2397   (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399   (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401   (cp_parser *);
2402 static size_t cp_parser_skip_attributes_opt
2403   (cp_parser *, size_t);
2404 static bool cp_parser_extension_opt
2405   (cp_parser *, int *);
2406 static void cp_parser_label_declaration
2407   (cp_parser *);
2408 
2409 /* Concept Extensions */
2410 
2411 static tree cp_parser_requires_clause
2412   (cp_parser *);
2413 static tree cp_parser_requires_clause_opt
2414   (cp_parser *);
2415 static tree cp_parser_requires_expression
2416   (cp_parser *);
2417 static tree cp_parser_requirement_parameter_list
2418   (cp_parser *);
2419 static tree cp_parser_requirement_body
2420   (cp_parser *);
2421 static tree cp_parser_requirement_list
2422   (cp_parser *);
2423 static tree cp_parser_requirement
2424   (cp_parser *);
2425 static tree cp_parser_simple_requirement
2426   (cp_parser *);
2427 static tree cp_parser_compound_requirement
2428   (cp_parser *);
2429 static tree cp_parser_type_requirement
2430   (cp_parser *);
2431 static tree cp_parser_nested_requirement
2432   (cp_parser *);
2433 
2434 /* Transactional Memory Extensions */
2435 
2436 static tree cp_parser_transaction
2437   (cp_parser *, cp_token *);
2438 static tree cp_parser_transaction_expression
2439   (cp_parser *, enum rid);
2440 static void cp_parser_function_transaction
2441   (cp_parser *, enum rid);
2442 static tree cp_parser_transaction_cancel
2443   (cp_parser *);
2444 
2445 enum pragma_context {
2446   pragma_external,
2447   pragma_member,
2448   pragma_objc_icode,
2449   pragma_stmt,
2450   pragma_compound
2451 };
2452 static bool cp_parser_pragma
2453   (cp_parser *, enum pragma_context, bool *);
2454 
2455 /* Objective-C++ Productions */
2456 
2457 static tree cp_parser_objc_message_receiver
2458   (cp_parser *);
2459 static tree cp_parser_objc_message_args
2460   (cp_parser *);
2461 static tree cp_parser_objc_message_expression
2462   (cp_parser *);
2463 static cp_expr cp_parser_objc_encode_expression
2464   (cp_parser *);
2465 static tree cp_parser_objc_defs_expression
2466   (cp_parser *);
2467 static tree cp_parser_objc_protocol_expression
2468   (cp_parser *);
2469 static tree cp_parser_objc_selector_expression
2470   (cp_parser *);
2471 static cp_expr cp_parser_objc_expression
2472   (cp_parser *);
2473 static bool cp_parser_objc_selector_p
2474   (enum cpp_ttype);
2475 static tree cp_parser_objc_selector
2476   (cp_parser *);
2477 static tree cp_parser_objc_protocol_refs_opt
2478   (cp_parser *);
2479 static void cp_parser_objc_declaration
2480   (cp_parser *, tree);
2481 static tree cp_parser_objc_statement
2482   (cp_parser *);
2483 static bool cp_parser_objc_valid_prefix_attributes
2484   (cp_parser *, tree *);
2485 static void cp_parser_objc_at_property_declaration
2486   (cp_parser *) ;
2487 static void cp_parser_objc_at_synthesize_declaration
2488   (cp_parser *) ;
2489 static void cp_parser_objc_at_dynamic_declaration
2490   (cp_parser *) ;
2491 static tree cp_parser_objc_struct_declaration
2492   (cp_parser *) ;
2493 
2494 /* Utility Routines */
2495 
2496 static cp_expr cp_parser_lookup_name
2497   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2498 static tree cp_parser_lookup_name_simple
2499   (cp_parser *, tree, location_t);
2500 static tree cp_parser_maybe_treat_template_as_class
2501   (tree, bool);
2502 static bool cp_parser_check_declarator_template_parameters
2503   (cp_parser *, cp_declarator *, location_t);
2504 static bool cp_parser_check_template_parameters
2505   (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2506 static cp_expr cp_parser_simple_cast_expression
2507   (cp_parser *);
2508 static tree cp_parser_global_scope_opt
2509   (cp_parser *, bool);
2510 static bool cp_parser_constructor_declarator_p
2511   (cp_parser *, cp_parser_flags, bool);
2512 static tree cp_parser_function_definition_from_specifiers_and_declarator
2513   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2514 static tree cp_parser_function_definition_after_declarator
2515   (cp_parser *, bool);
2516 static bool cp_parser_template_declaration_after_export
2517   (cp_parser *, bool);
2518 static void cp_parser_perform_template_parameter_access_checks
2519   (vec<deferred_access_check, va_gc> *);
2520 static tree cp_parser_single_declaration
2521   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2522 static cp_expr cp_parser_functional_cast
2523   (cp_parser *, tree);
2524 static tree cp_parser_save_member_function_body
2525   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2526 static tree cp_parser_save_nsdmi
2527   (cp_parser *);
2528 static tree cp_parser_enclosed_template_argument_list
2529   (cp_parser *);
2530 static void cp_parser_save_default_args
2531   (cp_parser *, tree);
2532 static void cp_parser_late_parsing_for_member
2533   (cp_parser *, tree);
2534 static tree cp_parser_late_parse_one_default_arg
2535   (cp_parser *, tree, tree, tree);
2536 static void cp_parser_late_parsing_nsdmi
2537   (cp_parser *, tree);
2538 static void cp_parser_late_parsing_default_args
2539   (cp_parser *, tree);
2540 static tree cp_parser_sizeof_operand
2541   (cp_parser *, enum rid);
2542 static cp_expr cp_parser_trait_expr
2543   (cp_parser *, enum rid);
2544 static bool cp_parser_declares_only_class_p
2545   (cp_parser *);
2546 static void cp_parser_set_storage_class
2547   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2548 static void cp_parser_set_decl_spec_type
2549   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2550 static void set_and_check_decl_spec_loc
2551   (cp_decl_specifier_seq *decl_specs,
2552    cp_decl_spec ds, cp_token *);
2553 static bool cp_parser_friend_p
2554   (const cp_decl_specifier_seq *);
2555 static void cp_parser_required_error
2556   (cp_parser *, required_token, bool, location_t);
2557 static cp_token *cp_parser_require
2558   (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2559 static cp_token *cp_parser_require_keyword
2560   (cp_parser *, enum rid, required_token);
2561 static bool cp_parser_token_starts_function_definition_p
2562   (cp_token *);
2563 static bool cp_parser_next_token_starts_class_definition_p
2564   (cp_parser *);
2565 static bool cp_parser_next_token_ends_template_argument_p
2566   (cp_parser *);
2567 static bool cp_parser_nth_token_starts_template_argument_list_p
2568   (cp_parser *, size_t);
2569 static enum tag_types cp_parser_token_is_class_key
2570   (cp_token *);
2571 static enum tag_types cp_parser_token_is_type_parameter_key
2572   (cp_token *);
2573 static void cp_parser_check_class_key
2574   (enum tag_types, tree type);
2575 static void cp_parser_check_access_in_redeclaration
2576   (tree type, location_t location);
2577 static bool cp_parser_optional_template_keyword
2578   (cp_parser *);
2579 static void cp_parser_pre_parsed_nested_name_specifier
2580   (cp_parser *);
2581 static bool cp_parser_cache_group
2582   (cp_parser *, enum cpp_ttype, unsigned);
2583 static tree cp_parser_cache_defarg
2584   (cp_parser *parser, bool nsdmi);
2585 static void cp_parser_parse_tentatively
2586   (cp_parser *);
2587 static void cp_parser_commit_to_tentative_parse
2588   (cp_parser *);
2589 static void cp_parser_commit_to_topmost_tentative_parse
2590   (cp_parser *);
2591 static void cp_parser_abort_tentative_parse
2592   (cp_parser *);
2593 static bool cp_parser_parse_definitely
2594   (cp_parser *);
2595 static inline bool cp_parser_parsing_tentatively
2596   (cp_parser *);
2597 static bool cp_parser_uncommitted_to_tentative_parse_p
2598   (cp_parser *);
2599 static void cp_parser_error
2600   (cp_parser *, const char *);
2601 static void cp_parser_name_lookup_error
2602   (cp_parser *, tree, tree, name_lookup_error, location_t);
2603 static bool cp_parser_simulate_error
2604   (cp_parser *);
2605 static bool cp_parser_check_type_definition
2606   (cp_parser *);
2607 static void cp_parser_check_for_definition_in_return_type
2608   (cp_declarator *, tree, location_t type_location);
2609 static void cp_parser_check_for_invalid_template_id
2610   (cp_parser *, tree, enum tag_types, location_t location);
2611 static bool cp_parser_non_integral_constant_expression
2612   (cp_parser *, non_integral_constant);
2613 static void cp_parser_diagnose_invalid_type_name
2614   (cp_parser *, tree, location_t);
2615 static bool cp_parser_parse_and_diagnose_invalid_type_name
2616   (cp_parser *);
2617 static int cp_parser_skip_to_closing_parenthesis
2618   (cp_parser *, bool, bool, bool);
2619 static void cp_parser_skip_to_end_of_statement
2620   (cp_parser *);
2621 static void cp_parser_consume_semicolon_at_end_of_statement
2622   (cp_parser *);
2623 static void cp_parser_skip_to_end_of_block_or_statement
2624   (cp_parser *);
2625 static bool cp_parser_skip_to_closing_brace
2626   (cp_parser *);
2627 static void cp_parser_skip_to_end_of_template_parameter_list
2628   (cp_parser *);
2629 static void cp_parser_skip_to_pragma_eol
2630   (cp_parser*, cp_token *);
2631 static bool cp_parser_error_occurred
2632   (cp_parser *);
2633 static bool cp_parser_allow_gnu_extensions_p
2634   (cp_parser *);
2635 static bool cp_parser_is_pure_string_literal
2636   (cp_token *);
2637 static bool cp_parser_is_string_literal
2638   (cp_token *);
2639 static bool cp_parser_is_keyword
2640   (cp_token *, enum rid);
2641 static tree cp_parser_make_typename_type
2642   (cp_parser *, tree, location_t location);
2643 static cp_declarator * cp_parser_make_indirect_declarator
2644   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2645 static bool cp_parser_compound_literal_p
2646   (cp_parser *);
2647 static bool cp_parser_array_designator_p
2648   (cp_parser *);
2649 static bool cp_parser_init_statement_p
2650   (cp_parser *);
2651 static bool cp_parser_skip_to_closing_square_bracket
2652   (cp_parser *);
2653 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2654 
2655 /* Concept-related syntactic transformations */
2656 
2657 static tree cp_parser_maybe_concept_name       (cp_parser *, tree);
2658 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2659 
2660 // -------------------------------------------------------------------------- //
2661 // Unevaluated Operand Guard
2662 //
2663 // Implementation of an RAII helper for unevaluated operand parsing.
cp_unevaluated()2664 cp_unevaluated::cp_unevaluated ()
2665 {
2666   ++cp_unevaluated_operand;
2667   ++c_inhibit_evaluation_warnings;
2668 }
2669 
~cp_unevaluated()2670 cp_unevaluated::~cp_unevaluated ()
2671 {
2672   --c_inhibit_evaluation_warnings;
2673   --cp_unevaluated_operand;
2674 }
2675 
2676 // -------------------------------------------------------------------------- //
2677 // Tentative Parsing
2678 
2679 /* Returns nonzero if we are parsing tentatively.  */
2680 
2681 static inline bool
cp_parser_parsing_tentatively(cp_parser * parser)2682 cp_parser_parsing_tentatively (cp_parser* parser)
2683 {
2684   return parser->context->next != NULL;
2685 }
2686 
2687 /* Returns nonzero if TOKEN is a string literal.  */
2688 
2689 static bool
cp_parser_is_pure_string_literal(cp_token * token)2690 cp_parser_is_pure_string_literal (cp_token* token)
2691 {
2692   return (token->type == CPP_STRING ||
2693 	  token->type == CPP_STRING16 ||
2694 	  token->type == CPP_STRING32 ||
2695 	  token->type == CPP_WSTRING ||
2696 	  token->type == CPP_UTF8STRING);
2697 }
2698 
2699 /* Returns nonzero if TOKEN is a string literal
2700    of a user-defined string literal.  */
2701 
2702 static bool
cp_parser_is_string_literal(cp_token * token)2703 cp_parser_is_string_literal (cp_token* token)
2704 {
2705   return (cp_parser_is_pure_string_literal (token) ||
2706 	  token->type == CPP_STRING_USERDEF ||
2707 	  token->type == CPP_STRING16_USERDEF ||
2708 	  token->type == CPP_STRING32_USERDEF ||
2709 	  token->type == CPP_WSTRING_USERDEF ||
2710 	  token->type == CPP_UTF8STRING_USERDEF);
2711 }
2712 
2713 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2714 
2715 static bool
cp_parser_is_keyword(cp_token * token,enum rid keyword)2716 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2717 {
2718   return token->keyword == keyword;
2719 }
2720 
2721 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2722    PRAGMA_NONE.  */
2723 
2724 static enum pragma_kind
cp_parser_pragma_kind(cp_token * token)2725 cp_parser_pragma_kind (cp_token *token)
2726 {
2727   if (token->type != CPP_PRAGMA)
2728     return PRAGMA_NONE;
2729   /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2730   return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2731 }
2732 
2733 /* Helper function for cp_parser_error.
2734    Having peeked a token of kind TOK1_KIND that might signify
2735    a conflict marker, peek successor tokens to determine
2736    if we actually do have a conflict marker.
2737    Specifically, we consider a run of 7 '<', '=' or '>' characters
2738    at the start of a line as a conflict marker.
2739    These come through the lexer as three pairs and a single,
2740    e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2741    If it returns true, *OUT_LOC is written to with the location/range
2742    of the marker.  */
2743 
2744 static bool
cp_lexer_peek_conflict_marker(cp_lexer * lexer,enum cpp_ttype tok1_kind,location_t * out_loc)2745 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2746 			       location_t *out_loc)
2747 {
2748   cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2749   if (token2->type != tok1_kind)
2750     return false;
2751   cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2752   if (token3->type != tok1_kind)
2753     return false;
2754   cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2755   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2756     return false;
2757 
2758   /* It must be at the start of the line.  */
2759   location_t start_loc = cp_lexer_peek_token (lexer)->location;
2760   if (LOCATION_COLUMN (start_loc) != 1)
2761     return false;
2762 
2763   /* We have a conflict marker.  Construct a location of the form:
2764        <<<<<<<
2765        ^~~~~~~
2766      with start == caret, finishing at the end of the marker.  */
2767   location_t finish_loc = get_finish (token4->location);
2768   *out_loc = make_location (start_loc, start_loc, finish_loc);
2769 
2770   return true;
2771 }
2772 
2773 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2774    RT_CLOSE_PAREN.  */
2775 
2776 static const char *
get_matching_symbol(required_token token_desc)2777 get_matching_symbol (required_token token_desc)
2778 {
2779   switch (token_desc)
2780     {
2781     default:
2782       gcc_unreachable ();
2783       return "";
2784     case RT_CLOSE_BRACE:
2785       return "{";
2786     case RT_CLOSE_PAREN:
2787       return "(";
2788     }
2789 }
2790 
2791 /* Attempt to convert TOKEN_DESC from a required_token to an
2792    enum cpp_ttype, returning CPP_EOF if there is no good conversion.  */
2793 
2794 static enum cpp_ttype
get_required_cpp_ttype(required_token token_desc)2795 get_required_cpp_ttype (required_token token_desc)
2796 {
2797   switch (token_desc)
2798     {
2799     case RT_SEMICOLON:
2800       return CPP_SEMICOLON;
2801     case RT_OPEN_PAREN:
2802       return CPP_OPEN_PAREN;
2803     case RT_CLOSE_BRACE:
2804       return CPP_CLOSE_BRACE;
2805     case RT_OPEN_BRACE:
2806       return CPP_OPEN_BRACE;
2807     case RT_CLOSE_SQUARE:
2808       return CPP_CLOSE_SQUARE;
2809     case RT_OPEN_SQUARE:
2810       return CPP_OPEN_SQUARE;
2811     case RT_COMMA:
2812       return CPP_COMMA;
2813     case RT_COLON:
2814       return CPP_COLON;
2815     case RT_CLOSE_PAREN:
2816       return CPP_CLOSE_PAREN;
2817 
2818     default:
2819       /* Use CPP_EOF as a "no completions possible" code.  */
2820       return CPP_EOF;
2821     }
2822 }
2823 
2824 
2825 /* Subroutine of cp_parser_error and cp_parser_required_error.
2826 
2827    Issue a diagnostic of the form
2828       FILE:LINE: MESSAGE before TOKEN
2829    where TOKEN is the next token in the input stream.  MESSAGE
2830    (specified by the caller) is usually of the form "expected
2831    OTHER-TOKEN".
2832 
2833    This bypasses the check for tentative passing, and potentially
2834    adds material needed by cp_parser_required_error.
2835 
2836    If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2837    suggesting insertion of the missing token.
2838 
2839    Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2840    have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2841    location.  */
2842 
2843 static void
cp_parser_error_1(cp_parser * parser,const char * gmsgid,required_token missing_token_desc,location_t matching_location)2844 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2845 		   required_token missing_token_desc,
2846 		   location_t matching_location)
2847 {
2848   cp_token *token = cp_lexer_peek_token (parser->lexer);
2849   /* This diagnostic makes more sense if it is tagged to the line
2850      of the token we just peeked at.  */
2851   cp_lexer_set_source_position_from_token (token);
2852 
2853   if (token->type == CPP_PRAGMA)
2854     {
2855       error_at (token->location,
2856 		"%<#pragma%> is not allowed here");
2857       cp_parser_skip_to_pragma_eol (parser, token);
2858       return;
2859     }
2860 
2861   /* If this is actually a conflict marker, report it as such.  */
2862   if (token->type == CPP_LSHIFT
2863       || token->type == CPP_RSHIFT
2864       || token->type == CPP_EQ_EQ)
2865     {
2866       location_t loc;
2867       if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2868 	{
2869 	  error_at (loc, "version control conflict marker in file");
2870 	  expanded_location token_exploc = expand_location (token->location);
2871 	  /* Consume tokens until the end of the source line.  */
2872 	  while (1)
2873 	    {
2874 	      cp_lexer_consume_token (parser->lexer);
2875 	      cp_token *next = cp_lexer_peek_token (parser->lexer);
2876 	      if (next == NULL)
2877 		break;
2878 	      expanded_location next_exploc = expand_location (next->location);
2879 	      if (next_exploc.file != token_exploc.file)
2880 		break;
2881 	      if (next_exploc.line != token_exploc.line)
2882 		break;
2883 	    }
2884 	  return;
2885 	}
2886     }
2887 
2888   gcc_rich_location richloc (input_location);
2889 
2890   bool added_matching_location = false;
2891 
2892   if (missing_token_desc != RT_NONE)
2893     {
2894       /* Potentially supply a fix-it hint, suggesting to add the
2895 	 missing token immediately after the *previous* token.
2896 	 This may move the primary location within richloc.  */
2897       enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2898       location_t prev_token_loc
2899 	= cp_lexer_previous_token (parser->lexer)->location;
2900       maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2901 
2902       /* If matching_location != UNKNOWN_LOCATION, highlight it.
2903 	 Attempt to consolidate diagnostics by printing it as a
2904 	secondary range within the main diagnostic.  */
2905       if (matching_location != UNKNOWN_LOCATION)
2906 	added_matching_location
2907 	  = richloc.add_location_if_nearby (matching_location);
2908     }
2909 
2910   /* Actually emit the error.  */
2911   c_parse_error (gmsgid,
2912 		 /* Because c_parser_error does not understand
2913 		    CPP_KEYWORD, keywords are treated like
2914 		    identifiers.  */
2915 		 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2916 		 token->u.value, token->flags, &richloc);
2917 
2918   if (missing_token_desc != RT_NONE)
2919     {
2920       /* If we weren't able to consolidate matching_location, then
2921 	 print it as a secondary diagnostic.  */
2922       if (matching_location != UNKNOWN_LOCATION
2923 	  && !added_matching_location)
2924 	inform (matching_location, "to match this %qs",
2925 		get_matching_symbol (missing_token_desc));
2926     }
2927 }
2928 
2929 /* If not parsing tentatively, issue a diagnostic of the form
2930       FILE:LINE: MESSAGE before TOKEN
2931    where TOKEN is the next token in the input stream.  MESSAGE
2932    (specified by the caller) is usually of the form "expected
2933    OTHER-TOKEN".  */
2934 
2935 static void
cp_parser_error(cp_parser * parser,const char * gmsgid)2936 cp_parser_error (cp_parser* parser, const char* gmsgid)
2937 {
2938   if (!cp_parser_simulate_error (parser))
2939     cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2940 }
2941 
2942 /* Issue an error about name-lookup failing.  NAME is the
2943    IDENTIFIER_NODE DECL is the result of
2944    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2945    the thing that we hoped to find.  */
2946 
2947 static void
cp_parser_name_lookup_error(cp_parser * parser,tree name,tree decl,name_lookup_error desired,location_t location)2948 cp_parser_name_lookup_error (cp_parser* parser,
2949 			     tree name,
2950 			     tree decl,
2951 			     name_lookup_error desired,
2952 			     location_t location)
2953 {
2954   /* If name lookup completely failed, tell the user that NAME was not
2955      declared.  */
2956   if (decl == error_mark_node)
2957     {
2958       if (parser->scope && parser->scope != global_namespace)
2959 	error_at (location, "%<%E::%E%> has not been declared",
2960 		  parser->scope, name);
2961       else if (parser->scope == global_namespace)
2962 	error_at (location, "%<::%E%> has not been declared", name);
2963       else if (parser->object_scope
2964 	       && !CLASS_TYPE_P (parser->object_scope))
2965 	error_at (location, "request for member %qE in non-class type %qT",
2966 		  name, parser->object_scope);
2967       else if (parser->object_scope)
2968 	error_at (location, "%<%T::%E%> has not been declared",
2969 		  parser->object_scope, name);
2970       else
2971 	error_at (location, "%qE has not been declared", name);
2972     }
2973   else if (parser->scope && parser->scope != global_namespace)
2974     {
2975       switch (desired)
2976 	{
2977 	  case NLE_TYPE:
2978 	    error_at (location, "%<%E::%E%> is not a type",
2979 	    			parser->scope, name);
2980 	    break;
2981 	  case NLE_CXX98:
2982 	    error_at (location, "%<%E::%E%> is not a class or namespace",
2983 	    			parser->scope, name);
2984 	    break;
2985 	  case NLE_NOT_CXX98:
2986 	    error_at (location,
2987 	    	      "%<%E::%E%> is not a class, namespace, or enumeration",
2988 		      parser->scope, name);
2989 	    break;
2990 	  default:
2991 	    gcc_unreachable ();
2992 
2993 	}
2994     }
2995   else if (parser->scope == global_namespace)
2996     {
2997       switch (desired)
2998 	{
2999 	  case NLE_TYPE:
3000 	    error_at (location, "%<::%E%> is not a type", name);
3001 	    break;
3002 	  case NLE_CXX98:
3003 	    error_at (location, "%<::%E%> is not a class or namespace", name);
3004 	    break;
3005 	  case NLE_NOT_CXX98:
3006 	    error_at (location,
3007 		      "%<::%E%> is not a class, namespace, or enumeration",
3008 		      name);
3009 	    break;
3010 	  default:
3011 	    gcc_unreachable ();
3012 	}
3013     }
3014   else
3015     {
3016       switch (desired)
3017 	{
3018 	  case NLE_TYPE:
3019 	    error_at (location, "%qE is not a type", name);
3020 	    break;
3021 	  case NLE_CXX98:
3022 	    error_at (location, "%qE is not a class or namespace", name);
3023 	    break;
3024 	  case NLE_NOT_CXX98:
3025 	    error_at (location,
3026 		      "%qE is not a class, namespace, or enumeration", name);
3027 	    break;
3028 	  default:
3029 	    gcc_unreachable ();
3030 	}
3031     }
3032 }
3033 
3034 /* If we are parsing tentatively, remember that an error has occurred
3035    during this tentative parse.  Returns true if the error was
3036    simulated; false if a message should be issued by the caller.  */
3037 
3038 static bool
cp_parser_simulate_error(cp_parser * parser)3039 cp_parser_simulate_error (cp_parser* parser)
3040 {
3041   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3042     {
3043       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3044       return true;
3045     }
3046   return false;
3047 }
3048 
3049 /* This function is called when a type is defined.  If type
3050    definitions are forbidden at this point, an error message is
3051    issued.  */
3052 
3053 static bool
cp_parser_check_type_definition(cp_parser * parser)3054 cp_parser_check_type_definition (cp_parser* parser)
3055 {
3056   /* If types are forbidden here, issue a message.  */
3057   if (parser->type_definition_forbidden_message)
3058     {
3059       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3060 	 or %qs in the message need to be interpreted.  */
3061       error (parser->type_definition_forbidden_message,
3062 	     parser->type_definition_forbidden_message_arg);
3063       return false;
3064     }
3065   return true;
3066 }
3067 
3068 /* This function is called when the DECLARATOR is processed.  The TYPE
3069    was a type defined in the decl-specifiers.  If it is invalid to
3070    define a type in the decl-specifiers for DECLARATOR, an error is
3071    issued. TYPE_LOCATION is the location of TYPE and is used
3072    for error reporting.  */
3073 
3074 static void
cp_parser_check_for_definition_in_return_type(cp_declarator * declarator,tree type,location_t type_location)3075 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3076 					       tree type, location_t type_location)
3077 {
3078   /* [dcl.fct] forbids type definitions in return types.
3079      Unfortunately, it's not easy to know whether or not we are
3080      processing a return type until after the fact.  */
3081   while (declarator
3082 	 && (declarator->kind == cdk_pointer
3083 	     || declarator->kind == cdk_reference
3084 	     || declarator->kind == cdk_ptrmem))
3085     declarator = declarator->declarator;
3086   if (declarator
3087       && declarator->kind == cdk_function)
3088     {
3089       error_at (type_location,
3090 		"new types may not be defined in a return type");
3091       inform (type_location,
3092 	      "(perhaps a semicolon is missing after the definition of %qT)",
3093 	      type);
3094     }
3095 }
3096 
3097 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3098    "<" in any valid C++ program.  If the next token is indeed "<",
3099    issue a message warning the user about what appears to be an
3100    invalid attempt to form a template-id. LOCATION is the location
3101    of the type-specifier (TYPE) */
3102 
3103 static void
cp_parser_check_for_invalid_template_id(cp_parser * parser,tree type,enum tag_types tag_type,location_t location)3104 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3105 					 tree type,
3106 					 enum tag_types tag_type,
3107 					 location_t location)
3108 {
3109   cp_token_position start = 0;
3110 
3111   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3112     {
3113       if (TREE_CODE (type) == TYPE_DECL)
3114 	type = TREE_TYPE (type);
3115       if (TYPE_P (type) && !template_placeholder_p (type))
3116 	error_at (location, "%qT is not a template", type);
3117       else if (identifier_p (type))
3118 	{
3119 	  if (tag_type != none_type)
3120 	    error_at (location, "%qE is not a class template", type);
3121 	  else
3122 	    error_at (location, "%qE is not a template", type);
3123 	}
3124       else
3125 	error_at (location, "invalid template-id");
3126       /* Remember the location of the invalid "<".  */
3127       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3128 	start = cp_lexer_token_position (parser->lexer, true);
3129       /* Consume the "<".  */
3130       cp_lexer_consume_token (parser->lexer);
3131       /* Parse the template arguments.  */
3132       cp_parser_enclosed_template_argument_list (parser);
3133       /* Permanently remove the invalid template arguments so that
3134 	 this error message is not issued again.  */
3135       if (start)
3136 	cp_lexer_purge_tokens_after (parser->lexer, start);
3137     }
3138 }
3139 
3140 /* If parsing an integral constant-expression, issue an error message
3141    about the fact that THING appeared and return true.  Otherwise,
3142    return false.  In either case, set
3143    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3144 
3145 static bool
cp_parser_non_integral_constant_expression(cp_parser * parser,non_integral_constant thing)3146 cp_parser_non_integral_constant_expression (cp_parser  *parser,
3147 					    non_integral_constant thing)
3148 {
3149   parser->non_integral_constant_expression_p = true;
3150   if (parser->integral_constant_expression_p)
3151     {
3152       if (!parser->allow_non_integral_constant_expression_p)
3153 	{
3154 	  const char *msg = NULL;
3155 	  switch (thing)
3156 	    {
3157   	      case NIC_FLOAT:
3158 		pedwarn (input_location, OPT_Wpedantic,
3159 			 "ISO C++ forbids using a floating-point literal "
3160 			 "in a constant-expression");
3161 		return true;
3162 	      case NIC_CAST:
3163 		error ("a cast to a type other than an integral or "
3164 		       "enumeration type cannot appear in a "
3165 		       "constant-expression");
3166 		return true;
3167 	      case NIC_TYPEID:
3168 		error ("%<typeid%> operator "
3169 		       "cannot appear in a constant-expression");
3170 		return true;
3171 	      case NIC_NCC:
3172 		error ("non-constant compound literals "
3173 		       "cannot appear in a constant-expression");
3174 		return true;
3175 	      case NIC_FUNC_CALL:
3176 		error ("a function call "
3177 		       "cannot appear in a constant-expression");
3178 		return true;
3179 	      case NIC_INC:
3180 		error ("an increment "
3181 		       "cannot appear in a constant-expression");
3182 		return true;
3183 	      case NIC_DEC:
3184 		error ("an decrement "
3185 		       "cannot appear in a constant-expression");
3186 		return true;
3187 	      case NIC_ARRAY_REF:
3188 		error ("an array reference "
3189 		       "cannot appear in a constant-expression");
3190 		return true;
3191 	      case NIC_ADDR_LABEL:
3192 		error ("the address of a label "
3193 		       "cannot appear in a constant-expression");
3194 		return true;
3195 	      case NIC_OVERLOADED:
3196 		error ("calls to overloaded operators "
3197 		       "cannot appear in a constant-expression");
3198 		return true;
3199 	      case NIC_ASSIGNMENT:
3200 		error ("an assignment cannot appear in a constant-expression");
3201 		return true;
3202 	      case NIC_COMMA:
3203 		error ("a comma operator "
3204 		       "cannot appear in a constant-expression");
3205 		return true;
3206 	      case NIC_CONSTRUCTOR:
3207 		error ("a call to a constructor "
3208 		       "cannot appear in a constant-expression");
3209 		return true;
3210 	      case NIC_TRANSACTION:
3211 		error ("a transaction expression "
3212 		       "cannot appear in a constant-expression");
3213 		return true;
3214 	      case NIC_THIS:
3215 		msg = "this";
3216 		break;
3217 	      case NIC_FUNC_NAME:
3218 		msg = "__FUNCTION__";
3219 		break;
3220   	      case NIC_PRETTY_FUNC:
3221 		msg = "__PRETTY_FUNCTION__";
3222 		break;
3223 	      case NIC_C99_FUNC:
3224 		msg = "__func__";
3225 		break;
3226 	      case NIC_VA_ARG:
3227 		msg = "va_arg";
3228 		break;
3229 	      case NIC_ARROW:
3230 		msg = "->";
3231 		break;
3232 	      case NIC_POINT:
3233 		msg = ".";
3234 		break;
3235 	      case NIC_STAR:
3236 		msg = "*";
3237 		break;
3238 	      case NIC_ADDR:
3239 		msg = "&";
3240 		break;
3241 	      case NIC_PREINCREMENT:
3242 		msg = "++";
3243 		break;
3244 	      case NIC_PREDECREMENT:
3245 		msg = "--";
3246 		break;
3247 	      case NIC_NEW:
3248 		msg = "new";
3249 		break;
3250 	      case NIC_DEL:
3251 		msg = "delete";
3252 		break;
3253 	      default:
3254 		gcc_unreachable ();
3255 	    }
3256 	  if (msg)
3257 	    error ("%qs cannot appear in a constant-expression", msg);
3258 	  return true;
3259 	}
3260     }
3261   return false;
3262 }
3263 
3264 /* Emit a diagnostic for an invalid type name.  This function commits
3265    to the current active tentative parse, if any.  (Otherwise, the
3266    problematic construct might be encountered again later, resulting
3267    in duplicate error messages.) LOCATION is the location of ID.  */
3268 
3269 static void
cp_parser_diagnose_invalid_type_name(cp_parser * parser,tree id,location_t location)3270 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3271 				      location_t location)
3272 {
3273   tree decl, ambiguous_decls;
3274   cp_parser_commit_to_tentative_parse (parser);
3275   /* Try to lookup the identifier.  */
3276   decl = cp_parser_lookup_name (parser, id, none_type,
3277 				/*is_template=*/false,
3278 				/*is_namespace=*/false,
3279 				/*check_dependency=*/true,
3280 				&ambiguous_decls, location);
3281   if (ambiguous_decls)
3282     /* If the lookup was ambiguous, an error will already have
3283        been issued.  */
3284     return;
3285   /* If the lookup found a template-name, it means that the user forgot
3286   to specify an argument list. Emit a useful error message.  */
3287   if (DECL_TYPE_TEMPLATE_P (decl))
3288     {
3289       auto_diagnostic_group d;
3290       error_at (location,
3291 		"invalid use of template-name %qE without an argument list",
3292 		decl);
3293       if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3294 	inform (location, "class template argument deduction is only available "
3295 		"with %<-std=c++17%> or %<-std=gnu++17%>");
3296       inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3297     }
3298   else if (TREE_CODE (id) == BIT_NOT_EXPR)
3299     error_at (location, "invalid use of destructor %qD as a type", id);
3300   else if (TREE_CODE (decl) == TYPE_DECL)
3301     /* Something like 'unsigned A a;'  */
3302     error_at (location, "invalid combination of multiple type-specifiers");
3303   else if (!parser->scope)
3304     {
3305       /* Issue an error message.  */
3306       auto_diagnostic_group d;
3307       name_hint hint;
3308       if (TREE_CODE (id) == IDENTIFIER_NODE)
3309 	hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3310       if (const char *suggestion = hint.suggestion ())
3311 	{
3312 	  gcc_rich_location richloc (location);
3313 	  richloc.add_fixit_replace (suggestion);
3314 	  error_at (&richloc,
3315 		    "%qE does not name a type; did you mean %qs?",
3316 		    id, suggestion);
3317 	}
3318       else
3319 	error_at (location, "%qE does not name a type", id);
3320       /* If we're in a template class, it's possible that the user was
3321 	 referring to a type from a base class.  For example:
3322 
3323 	   template <typename T> struct A { typedef T X; };
3324 	   template <typename T> struct B : public A<T> { X x; };
3325 
3326 	 The user should have said "typename A<T>::X".  */
3327       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3328 	inform (location, "C++11 %<constexpr%> only available with "
3329 		"%<-std=c++11%> or %<-std=gnu++11%>");
3330       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3331 	inform (location, "C++11 %<noexcept%> only available with "
3332 		"%<-std=c++11%> or %<-std=gnu++11%>");
3333       else if (cxx_dialect < cxx11
3334 	       && TREE_CODE (id) == IDENTIFIER_NODE
3335 	       && id_equal (id, "thread_local"))
3336 	inform (location, "C++11 %<thread_local%> only available with "
3337 		"%<-std=c++11%> or %<-std=gnu++11%>");
3338       else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3339 	inform (location, "%<concept%> only available with %<-fconcepts%>");
3340       else if (processing_template_decl && current_class_type
3341 	       && TYPE_BINFO (current_class_type))
3342 	{
3343 	  tree b;
3344 
3345 	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3346 	       b;
3347 	       b = TREE_CHAIN (b))
3348 	    {
3349 	      tree base_type = BINFO_TYPE (b);
3350 	      if (CLASS_TYPE_P (base_type)
3351 		  && dependent_type_p (base_type))
3352 		{
3353 		  tree field;
3354 		  /* Go from a particular instantiation of the
3355 		     template (which will have an empty TYPE_FIELDs),
3356 		     to the main version.  */
3357 		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3358 		  for (field = TYPE_FIELDS (base_type);
3359 		       field;
3360 		       field = DECL_CHAIN (field))
3361 		    if (TREE_CODE (field) == TYPE_DECL
3362 			&& DECL_NAME (field) == id)
3363 		      {
3364 			inform (location,
3365 				"(perhaps %<typename %T::%E%> was intended)",
3366 				BINFO_TYPE (b), id);
3367 			break;
3368 		      }
3369 		  if (field)
3370 		    break;
3371 		}
3372 	    }
3373 	}
3374     }
3375   /* Here we diagnose qualified-ids where the scope is actually correct,
3376      but the identifier does not resolve to a valid type name.  */
3377   else if (parser->scope != error_mark_node)
3378     {
3379       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3380 	{
3381 	  auto_diagnostic_group d;
3382 	  name_hint hint;
3383 	  if (decl == error_mark_node)
3384 	    hint = suggest_alternative_in_explicit_scope (location, id,
3385 							  parser->scope);
3386 	  const char *suggestion = hint.suggestion ();
3387 	  gcc_rich_location richloc (location_of (id));
3388 	  if (suggestion)
3389 	    richloc.add_fixit_replace (suggestion);
3390 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3391 	    {
3392 	      if (suggestion)
3393 		error_at (&richloc,
3394 			  "%qE in namespace %qE does not name a template"
3395 			  " type; did you mean %qs?",
3396 			  id, parser->scope, suggestion);
3397 	      else
3398 		error_at (&richloc,
3399 			  "%qE in namespace %qE does not name a template type",
3400 			  id, parser->scope);
3401 	    }
3402 	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3403 	    {
3404 	      if (suggestion)
3405 		error_at (&richloc,
3406 			  "%qE in namespace %qE does not name a template"
3407 			  " type; did you mean %qs?",
3408 			  TREE_OPERAND (id, 0), parser->scope, suggestion);
3409 	      else
3410 		error_at (&richloc,
3411 			  "%qE in namespace %qE does not name a template"
3412 			  " type",
3413 			  TREE_OPERAND (id, 0), parser->scope);
3414 	    }
3415 	  else
3416 	    {
3417 	      if (suggestion)
3418 		error_at (&richloc,
3419 			  "%qE in namespace %qE does not name a type"
3420 			  "; did you mean %qs?",
3421 			  id, parser->scope, suggestion);
3422 	      else
3423 		error_at (&richloc,
3424 			  "%qE in namespace %qE does not name a type",
3425 			  id, parser->scope);
3426 	    }
3427 	  if (DECL_P (decl))
3428 	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3429 	}
3430       else if (CLASS_TYPE_P (parser->scope)
3431 	       && constructor_name_p (id, parser->scope))
3432 	{
3433 	  /* A<T>::A<T>() */
3434 	  auto_diagnostic_group d;
3435 	  error_at (location, "%<%T::%E%> names the constructor, not"
3436 		    " the type", parser->scope, id);
3437 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3438 	    error_at (location, "and %qT has no template constructors",
3439 		      parser->scope);
3440 	}
3441       else if (TYPE_P (parser->scope)
3442 	       && dependent_scope_p (parser->scope))
3443 	{
3444 	  gcc_rich_location richloc (location);
3445 	  richloc.add_fixit_insert_before ("typename ");
3446 	  if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3447 	    error_at (&richloc,
3448 		      "need %<typename%> before %<%T::%D::%E%> because "
3449 		      "%<%T::%D%> is a dependent scope",
3450 		      TYPE_CONTEXT (parser->scope),
3451 		      TYPENAME_TYPE_FULLNAME (parser->scope),
3452 		      id,
3453 		      TYPE_CONTEXT (parser->scope),
3454 		      TYPENAME_TYPE_FULLNAME (parser->scope));
3455 	  else
3456 	    error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3457 		      "%qT is a dependent scope",
3458 		      parser->scope, id, parser->scope);
3459 	}
3460       else if (TYPE_P (parser->scope))
3461 	{
3462 	  auto_diagnostic_group d;
3463 	  if (!COMPLETE_TYPE_P (parser->scope))
3464 	    cxx_incomplete_type_error (location_of (id), NULL_TREE,
3465 				       parser->scope);
3466 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3467 	    error_at (location_of (id),
3468 		      "%qE in %q#T does not name a template type",
3469 		      id, parser->scope);
3470 	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3471 	    error_at (location_of (id),
3472 		      "%qE in %q#T does not name a template type",
3473 		      TREE_OPERAND (id, 0), parser->scope);
3474 	  else
3475 	    error_at (location_of (id),
3476 		      "%qE in %q#T does not name a type",
3477 		      id, parser->scope);
3478 	  if (DECL_P (decl))
3479 	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3480 	}
3481       else
3482 	gcc_unreachable ();
3483     }
3484 }
3485 
3486 /* Check for a common situation where a type-name should be present,
3487    but is not, and issue a sensible error message.  Returns true if an
3488    invalid type-name was detected.
3489 
3490    The situation handled by this function are variable declarations of the
3491    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3492    Usually, `ID' should name a type, but if we got here it means that it
3493    does not. We try to emit the best possible error message depending on
3494    how exactly the id-expression looks like.  */
3495 
3496 static bool
cp_parser_parse_and_diagnose_invalid_type_name(cp_parser * parser)3497 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3498 {
3499   tree id;
3500   cp_token *token = cp_lexer_peek_token (parser->lexer);
3501 
3502   /* Avoid duplicate error about ambiguous lookup.  */
3503   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3504     {
3505       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3506       if (next->type == CPP_NAME && next->error_reported)
3507 	goto out;
3508     }
3509 
3510   cp_parser_parse_tentatively (parser);
3511   id = cp_parser_id_expression (parser,
3512 				/*template_keyword_p=*/false,
3513 				/*check_dependency_p=*/true,
3514 				/*template_p=*/NULL,
3515 				/*declarator_p=*/false,
3516 				/*optional_p=*/false);
3517   /* If the next token is a (, this is a function with no explicit return
3518      type, i.e. constructor, destructor or conversion op.  */
3519   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3520       || TREE_CODE (id) == TYPE_DECL)
3521     {
3522       cp_parser_abort_tentative_parse (parser);
3523       return false;
3524     }
3525   if (!cp_parser_parse_definitely (parser))
3526     return false;
3527 
3528   /* Emit a diagnostic for the invalid type.  */
3529   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3530  out:
3531   /* If we aren't in the middle of a declarator (i.e. in a
3532      parameter-declaration-clause), skip to the end of the declaration;
3533      there's no point in trying to process it.  */
3534   if (!parser->in_declarator_p)
3535     cp_parser_skip_to_end_of_block_or_statement (parser);
3536   return true;
3537 }
3538 
3539 /* Consume tokens up to, and including, the next non-nested closing `)'.
3540    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3541    are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3542    found an unnested token of that type.  */
3543 
3544 static int
cp_parser_skip_to_closing_parenthesis_1(cp_parser * parser,bool recovering,cpp_ttype or_ttype,bool consume_paren)3545 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3546 					 bool recovering,
3547 					 cpp_ttype or_ttype,
3548 					 bool consume_paren)
3549 {
3550   unsigned paren_depth = 0;
3551   unsigned brace_depth = 0;
3552   unsigned square_depth = 0;
3553   unsigned condop_depth = 0;
3554 
3555   if (recovering && or_ttype == CPP_EOF
3556       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3557     return 0;
3558 
3559   while (true)
3560     {
3561       cp_token * token = cp_lexer_peek_token (parser->lexer);
3562 
3563       /* Have we found what we're looking for before the closing paren?  */
3564       if (token->type == or_ttype && or_ttype != CPP_EOF
3565 	  && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3566 	return -1;
3567 
3568       switch (token->type)
3569 	{
3570 	case CPP_PRAGMA_EOL:
3571 	  if (!parser->lexer->in_pragma)
3572 	    break;
3573 	  /* FALLTHRU */
3574 	case CPP_EOF:
3575 	  /* If we've run out of tokens, then there is no closing `)'.  */
3576 	  return 0;
3577 
3578         /* This is good for lambda expression capture-lists.  */
3579         case CPP_OPEN_SQUARE:
3580           ++square_depth;
3581           break;
3582         case CPP_CLOSE_SQUARE:
3583           if (!square_depth--)
3584             return 0;
3585           break;
3586 
3587 	case CPP_SEMICOLON:
3588 	  /* This matches the processing in skip_to_end_of_statement.  */
3589 	  if (!brace_depth)
3590 	    return 0;
3591 	  break;
3592 
3593 	case CPP_OPEN_BRACE:
3594 	  ++brace_depth;
3595 	  break;
3596 	case CPP_CLOSE_BRACE:
3597 	  if (!brace_depth--)
3598 	    return 0;
3599 	  break;
3600 
3601 	case CPP_OPEN_PAREN:
3602 	  if (!brace_depth)
3603 	    ++paren_depth;
3604 	  break;
3605 
3606 	case CPP_CLOSE_PAREN:
3607 	  if (!brace_depth && !paren_depth--)
3608 	    {
3609 	      if (consume_paren)
3610 		cp_lexer_consume_token (parser->lexer);
3611 	      return 1;
3612 	    }
3613 	  break;
3614 
3615 	case CPP_QUERY:
3616 	  if (!brace_depth && !paren_depth && !square_depth)
3617 	    ++condop_depth;
3618 	  break;
3619 
3620 	case CPP_COLON:
3621 	  if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3622 	    condop_depth--;
3623 	  break;
3624 
3625 	default:
3626 	  break;
3627 	}
3628 
3629       /* Consume the token.  */
3630       cp_lexer_consume_token (parser->lexer);
3631     }
3632 }
3633 
3634 /* Consume tokens up to, and including, the next non-nested closing `)'.
3635    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3636    are doing error recovery. Returns -1 if OR_COMMA is true and we
3637    found an unnested token of that type.  */
3638 
3639 static int
cp_parser_skip_to_closing_parenthesis(cp_parser * parser,bool recovering,bool or_comma,bool consume_paren)3640 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3641 				       bool recovering,
3642 				       bool or_comma,
3643 				       bool consume_paren)
3644 {
3645   cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3646   return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3647 						  ttype, consume_paren);
3648 }
3649 
3650 /* Consume tokens until we reach the end of the current statement.
3651    Normally, that will be just before consuming a `;'.  However, if a
3652    non-nested `}' comes first, then we stop before consuming that.  */
3653 
3654 static void
cp_parser_skip_to_end_of_statement(cp_parser * parser)3655 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3656 {
3657   unsigned nesting_depth = 0;
3658 
3659   /* Unwind generic function template scope if necessary.  */
3660   if (parser->fully_implicit_function_template_p)
3661     abort_fully_implicit_template (parser);
3662 
3663   while (true)
3664     {
3665       cp_token *token = cp_lexer_peek_token (parser->lexer);
3666 
3667       switch (token->type)
3668 	{
3669 	case CPP_PRAGMA_EOL:
3670 	  if (!parser->lexer->in_pragma)
3671 	    break;
3672 	  /* FALLTHRU */
3673 	case CPP_EOF:
3674 	  /* If we've run out of tokens, stop.  */
3675 	  return;
3676 
3677 	case CPP_SEMICOLON:
3678 	  /* If the next token is a `;', we have reached the end of the
3679 	     statement.  */
3680 	  if (!nesting_depth)
3681 	    return;
3682 	  break;
3683 
3684 	case CPP_CLOSE_BRACE:
3685 	  /* If this is a non-nested '}', stop before consuming it.
3686 	     That way, when confronted with something like:
3687 
3688 	       { 3 + }
3689 
3690 	     we stop before consuming the closing '}', even though we
3691 	     have not yet reached a `;'.  */
3692 	  if (nesting_depth == 0)
3693 	    return;
3694 
3695 	  /* If it is the closing '}' for a block that we have
3696 	     scanned, stop -- but only after consuming the token.
3697 	     That way given:
3698 
3699 		void f g () { ... }
3700 		typedef int I;
3701 
3702 	     we will stop after the body of the erroneously declared
3703 	     function, but before consuming the following `typedef'
3704 	     declaration.  */
3705 	  if (--nesting_depth == 0)
3706 	    {
3707 	      cp_lexer_consume_token (parser->lexer);
3708 	      return;
3709 	    }
3710 	  break;
3711 
3712 	case CPP_OPEN_BRACE:
3713 	  ++nesting_depth;
3714 	  break;
3715 
3716 	default:
3717 	  break;
3718 	}
3719 
3720       /* Consume the token.  */
3721       cp_lexer_consume_token (parser->lexer);
3722     }
3723 }
3724 
3725 /* This function is called at the end of a statement or declaration.
3726    If the next token is a semicolon, it is consumed; otherwise, error
3727    recovery is attempted.  */
3728 
3729 static void
cp_parser_consume_semicolon_at_end_of_statement(cp_parser * parser)3730 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3731 {
3732   /* Look for the trailing `;'.  */
3733   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3734     {
3735       /* If there is additional (erroneous) input, skip to the end of
3736 	 the statement.  */
3737       cp_parser_skip_to_end_of_statement (parser);
3738       /* If the next token is now a `;', consume it.  */
3739       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3740 	cp_lexer_consume_token (parser->lexer);
3741     }
3742 }
3743 
3744 /* Skip tokens until we have consumed an entire block, or until we
3745    have consumed a non-nested `;'.  */
3746 
3747 static void
cp_parser_skip_to_end_of_block_or_statement(cp_parser * parser)3748 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3749 {
3750   int nesting_depth = 0;
3751 
3752   /* Unwind generic function template scope if necessary.  */
3753   if (parser->fully_implicit_function_template_p)
3754     abort_fully_implicit_template (parser);
3755 
3756   while (nesting_depth >= 0)
3757     {
3758       cp_token *token = cp_lexer_peek_token (parser->lexer);
3759 
3760       switch (token->type)
3761 	{
3762 	case CPP_PRAGMA_EOL:
3763 	  if (!parser->lexer->in_pragma)
3764 	    break;
3765 	  /* FALLTHRU */
3766 	case CPP_EOF:
3767 	  /* If we've run out of tokens, stop.  */
3768 	  return;
3769 
3770 	case CPP_SEMICOLON:
3771 	  /* Stop if this is an unnested ';'. */
3772 	  if (!nesting_depth)
3773 	    nesting_depth = -1;
3774 	  break;
3775 
3776 	case CPP_CLOSE_BRACE:
3777 	  /* Stop if this is an unnested '}', or closes the outermost
3778 	     nesting level.  */
3779 	  nesting_depth--;
3780 	  if (nesting_depth < 0)
3781 	    return;
3782 	  if (!nesting_depth)
3783 	    nesting_depth = -1;
3784 	  break;
3785 
3786 	case CPP_OPEN_BRACE:
3787 	  /* Nest. */
3788 	  nesting_depth++;
3789 	  break;
3790 
3791 	default:
3792 	  break;
3793 	}
3794 
3795       /* Consume the token.  */
3796       cp_lexer_consume_token (parser->lexer);
3797     }
3798 }
3799 
3800 /* Skip tokens until a non-nested closing curly brace is the next
3801    token, or there are no more tokens. Return true in the first case,
3802    false otherwise.  */
3803 
3804 static bool
cp_parser_skip_to_closing_brace(cp_parser * parser)3805 cp_parser_skip_to_closing_brace (cp_parser *parser)
3806 {
3807   unsigned nesting_depth = 0;
3808 
3809   while (true)
3810     {
3811       cp_token *token = cp_lexer_peek_token (parser->lexer);
3812 
3813       switch (token->type)
3814 	{
3815 	case CPP_PRAGMA_EOL:
3816 	  if (!parser->lexer->in_pragma)
3817 	    break;
3818 	  /* FALLTHRU */
3819 	case CPP_EOF:
3820 	  /* If we've run out of tokens, stop.  */
3821 	  return false;
3822 
3823 	case CPP_CLOSE_BRACE:
3824 	  /* If the next token is a non-nested `}', then we have reached
3825 	     the end of the current block.  */
3826 	  if (nesting_depth-- == 0)
3827 	    return true;
3828 	  break;
3829 
3830 	case CPP_OPEN_BRACE:
3831 	  /* If it the next token is a `{', then we are entering a new
3832 	     block.  Consume the entire block.  */
3833 	  ++nesting_depth;
3834 	  break;
3835 
3836 	default:
3837 	  break;
3838 	}
3839 
3840       /* Consume the token.  */
3841       cp_lexer_consume_token (parser->lexer);
3842     }
3843 }
3844 
3845 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3846    parameter is the PRAGMA token, allowing us to purge the entire pragma
3847    sequence.  */
3848 
3849 static void
cp_parser_skip_to_pragma_eol(cp_parser * parser,cp_token * pragma_tok)3850 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3851 {
3852   cp_token *token;
3853 
3854   parser->lexer->in_pragma = false;
3855 
3856   do
3857     token = cp_lexer_consume_token (parser->lexer);
3858   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3859 
3860   /* Ensure that the pragma is not parsed again.  */
3861   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3862 }
3863 
3864 /* Require pragma end of line, resyncing with it as necessary.  The
3865    arguments are as for cp_parser_skip_to_pragma_eol.  */
3866 
3867 static void
cp_parser_require_pragma_eol(cp_parser * parser,cp_token * pragma_tok)3868 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3869 {
3870   parser->lexer->in_pragma = false;
3871   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3872     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3873 }
3874 
3875 /* This is a simple wrapper around make_typename_type. When the id is
3876    an unresolved identifier node, we can provide a superior diagnostic
3877    using cp_parser_diagnose_invalid_type_name.  */
3878 
3879 static tree
cp_parser_make_typename_type(cp_parser * parser,tree id,location_t id_location)3880 cp_parser_make_typename_type (cp_parser *parser, tree id,
3881 			      location_t id_location)
3882 {
3883   tree result;
3884   if (identifier_p (id))
3885     {
3886       result = make_typename_type (parser->scope, id, typename_type,
3887 				   /*complain=*/tf_none);
3888       if (result == error_mark_node)
3889 	cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3890       return result;
3891     }
3892   return make_typename_type (parser->scope, id, typename_type, tf_error);
3893 }
3894 
3895 /* This is a wrapper around the
3896    make_{pointer,ptrmem,reference}_declarator functions that decides
3897    which one to call based on the CODE and CLASS_TYPE arguments. The
3898    CODE argument should be one of the values returned by
3899    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3900    appertain to the pointer or reference.  */
3901 
3902 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)3903 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3904 				    cp_cv_quals cv_qualifiers,
3905 				    cp_declarator *target,
3906 				    tree attributes)
3907 {
3908   if (code == ERROR_MARK || target == cp_error_declarator)
3909     return cp_error_declarator;
3910 
3911   if (code == INDIRECT_REF)
3912     if (class_type == NULL_TREE)
3913       return make_pointer_declarator (cv_qualifiers, target, attributes);
3914     else
3915       return make_ptrmem_declarator (cv_qualifiers, class_type,
3916 				     target, attributes);
3917   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3918     return make_reference_declarator (cv_qualifiers, target,
3919 				      false, attributes);
3920   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3921     return make_reference_declarator (cv_qualifiers, target,
3922 				      true, attributes);
3923   gcc_unreachable ();
3924 }
3925 
3926 /* Create a new C++ parser.  */
3927 
3928 static cp_parser *
cp_parser_new(void)3929 cp_parser_new (void)
3930 {
3931   cp_parser *parser;
3932   cp_lexer *lexer;
3933   unsigned i;
3934 
3935   /* cp_lexer_new_main is called before doing GC allocation because
3936      cp_lexer_new_main might load a PCH file.  */
3937   lexer = cp_lexer_new_main ();
3938 
3939   /* Initialize the binops_by_token so that we can get the tree
3940      directly from the token.  */
3941   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3942     binops_by_token[binops[i].token_type] = binops[i];
3943 
3944   parser = ggc_cleared_alloc<cp_parser> ();
3945   parser->lexer = lexer;
3946   parser->context = cp_parser_context_new (NULL);
3947 
3948   /* For now, we always accept GNU extensions.  */
3949   parser->allow_gnu_extensions_p = 1;
3950 
3951   /* The `>' token is a greater-than operator, not the end of a
3952      template-id.  */
3953   parser->greater_than_is_operator_p = true;
3954 
3955   parser->default_arg_ok_p = true;
3956 
3957   /* We are not parsing a constant-expression.  */
3958   parser->integral_constant_expression_p = false;
3959   parser->allow_non_integral_constant_expression_p = false;
3960   parser->non_integral_constant_expression_p = false;
3961 
3962   /* Local variable names are not forbidden.  */
3963   parser->local_variables_forbidden_p = 0;
3964 
3965   /* We are not processing an `extern "C"' declaration.  */
3966   parser->in_unbraced_linkage_specification_p = false;
3967 
3968   /* We are not processing a declarator.  */
3969   parser->in_declarator_p = false;
3970 
3971   /* We are not processing a template-argument-list.  */
3972   parser->in_template_argument_list_p = false;
3973 
3974   /* We are not in an iteration statement.  */
3975   parser->in_statement = 0;
3976 
3977   /* We are not in a switch statement.  */
3978   parser->in_switch_statement_p = false;
3979 
3980   /* We are not parsing a type-id inside an expression.  */
3981   parser->in_type_id_in_expr_p = false;
3982 
3983   /* String literals should be translated to the execution character set.  */
3984   parser->translate_strings_p = true;
3985 
3986   /* We are not parsing a function body.  */
3987   parser->in_function_body = false;
3988 
3989   /* We can correct until told otherwise.  */
3990   parser->colon_corrects_to_scope_p = true;
3991 
3992   /* The unparsed function queue is empty.  */
3993   push_unparsed_function_queues (parser);
3994 
3995   /* There are no classes being defined.  */
3996   parser->num_classes_being_defined = 0;
3997 
3998   /* No template parameters apply.  */
3999   parser->num_template_parameter_lists = 0;
4000 
4001   /* Special parsing data structures.  */
4002   parser->omp_declare_simd = NULL;
4003   parser->oacc_routine = NULL;
4004 
4005   /* Not declaring an implicit function template.  */
4006   parser->auto_is_implicit_function_template_parm_p = false;
4007   parser->fully_implicit_function_template_p = false;
4008   parser->implicit_template_parms = 0;
4009   parser->implicit_template_scope = 0;
4010 
4011   /* Allow constrained-type-specifiers. */
4012   parser->prevent_constrained_type_specifiers = 0;
4013 
4014   /* We haven't yet seen an 'extern "C"'.  */
4015   parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4016 
4017   return parser;
4018 }
4019 
4020 /* Create a cp_lexer structure which will emit the tokens in CACHE
4021    and push it onto the parser's lexer stack.  This is used for delayed
4022    parsing of in-class method bodies and default arguments, and should
4023    not be confused with tentative parsing.  */
4024 static void
cp_parser_push_lexer_for_tokens(cp_parser * parser,cp_token_cache * cache)4025 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4026 {
4027   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4028   lexer->next = parser->lexer;
4029   parser->lexer = lexer;
4030 
4031   /* Move the current source position to that of the first token in the
4032      new lexer.  */
4033   cp_lexer_set_source_position_from_token (lexer->next_token);
4034 }
4035 
4036 /* Pop the top lexer off the parser stack.  This is never used for the
4037    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
4038 static void
cp_parser_pop_lexer(cp_parser * parser)4039 cp_parser_pop_lexer (cp_parser *parser)
4040 {
4041   cp_lexer *lexer = parser->lexer;
4042   parser->lexer = lexer->next;
4043   cp_lexer_destroy (lexer);
4044 
4045   /* Put the current source position back where it was before this
4046      lexer was pushed.  */
4047   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4048 }
4049 
4050 /* Lexical conventions [gram.lex]  */
4051 
4052 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
4053    identifier.  */
4054 
4055 static cp_expr
cp_parser_identifier(cp_parser * parser)4056 cp_parser_identifier (cp_parser* parser)
4057 {
4058   cp_token *token;
4059 
4060   /* Look for the identifier.  */
4061   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4062   /* Return the value.  */
4063   if (token)
4064     return cp_expr (token->u.value, token->location);
4065   else
4066     return error_mark_node;
4067 }
4068 
4069 /* Parse a sequence of adjacent string constants.  Returns a
4070    TREE_STRING representing the combined, nul-terminated string
4071    constant.  If TRANSLATE is true, translate the string to the
4072    execution character set.  If WIDE_OK is true, a wide string is
4073    invalid here.
4074 
4075    C++98 [lex.string] says that if a narrow string literal token is
4076    adjacent to a wide string literal token, the behavior is undefined.
4077    However, C99 6.4.5p4 says that this results in a wide string literal.
4078    We follow C99 here, for consistency with the C front end.
4079 
4080    This code is largely lifted from lex_string() in c-lex.c.
4081 
4082    FUTURE: ObjC++ will need to handle @-strings here.  */
4083 static cp_expr
4084 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4085 			  bool lookup_udlit = true)
4086 {
4087   tree value;
4088   size_t count;
4089   struct obstack str_ob;
4090   struct obstack loc_ob;
4091   cpp_string str, istr, *strs;
4092   cp_token *tok;
4093   enum cpp_ttype type, curr_type;
4094   int have_suffix_p = 0;
4095   tree string_tree;
4096   tree suffix_id = NULL_TREE;
4097   bool curr_tok_is_userdef_p = false;
4098 
4099   tok = cp_lexer_peek_token (parser->lexer);
4100   if (!cp_parser_is_string_literal (tok))
4101     {
4102       cp_parser_error (parser, "expected string-literal");
4103       return error_mark_node;
4104     }
4105 
4106   location_t loc = tok->location;
4107 
4108   if (cpp_userdef_string_p (tok->type))
4109     {
4110       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4111       curr_type = cpp_userdef_string_remove_type (tok->type);
4112       curr_tok_is_userdef_p = true;
4113     }
4114   else
4115     {
4116       string_tree = tok->u.value;
4117       curr_type = tok->type;
4118     }
4119   type = curr_type;
4120 
4121   /* Try to avoid the overhead of creating and destroying an obstack
4122      for the common case of just one string.  */
4123   if (!cp_parser_is_string_literal
4124       (cp_lexer_peek_nth_token (parser->lexer, 2)))
4125     {
4126       cp_lexer_consume_token (parser->lexer);
4127 
4128       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4129       str.len = TREE_STRING_LENGTH (string_tree);
4130       count = 1;
4131 
4132       if (curr_tok_is_userdef_p)
4133 	{
4134 	  suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4135 	  have_suffix_p = 1;
4136 	  curr_type = cpp_userdef_string_remove_type (tok->type);
4137 	}
4138       else
4139 	curr_type = tok->type;
4140 
4141       strs = &str;
4142     }
4143   else
4144     {
4145       location_t last_tok_loc = tok->location;
4146       gcc_obstack_init (&str_ob);
4147       gcc_obstack_init (&loc_ob);
4148       count = 0;
4149 
4150       do
4151 	{
4152 	  cp_lexer_consume_token (parser->lexer);
4153 	  count++;
4154 	  str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4155 	  str.len = TREE_STRING_LENGTH (string_tree);
4156 
4157 	  if (curr_tok_is_userdef_p)
4158 	    {
4159 	      tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4160 	      if (have_suffix_p == 0)
4161 		{
4162 		  suffix_id = curr_suffix_id;
4163 		  have_suffix_p = 1;
4164 		}
4165 	      else if (have_suffix_p == 1
4166 		       && curr_suffix_id != suffix_id)
4167 		{
4168 		  error ("inconsistent user-defined literal suffixes"
4169 			 " %qD and %qD in string literal",
4170 			 suffix_id, curr_suffix_id);
4171 		  have_suffix_p = -1;
4172 		}
4173 	      curr_type = cpp_userdef_string_remove_type (tok->type);
4174 	    }
4175 	  else
4176 	    curr_type = tok->type;
4177 
4178 	  if (type != curr_type)
4179 	    {
4180 	      if (type == CPP_STRING)
4181 		type = curr_type;
4182 	      else if (curr_type != CPP_STRING)
4183 		{
4184 		  rich_location rich_loc (line_table, tok->location);
4185 		  rich_loc.add_range (last_tok_loc);
4186 		  error_at (&rich_loc,
4187 			    "unsupported non-standard concatenation "
4188 			    "of string literals");
4189 		}
4190 	    }
4191 
4192 	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
4193 	  obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4194 
4195 	  last_tok_loc = tok->location;
4196 
4197 	  tok = cp_lexer_peek_token (parser->lexer);
4198 	  if (cpp_userdef_string_p (tok->type))
4199 	    {
4200 	      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4201 	      curr_type = cpp_userdef_string_remove_type (tok->type);
4202 	      curr_tok_is_userdef_p = true;
4203 	    }
4204 	  else
4205 	    {
4206 	      string_tree = tok->u.value;
4207 	      curr_type = tok->type;
4208 	      curr_tok_is_userdef_p = false;
4209 	    }
4210 	}
4211       while (cp_parser_is_string_literal (tok));
4212 
4213       /* A string literal built by concatenation has its caret=start at
4214 	 the start of the initial string, and its finish at the finish of
4215 	 the final string literal.  */
4216       loc = make_location (loc, loc, get_finish (last_tok_loc));
4217 
4218       strs = (cpp_string *) obstack_finish (&str_ob);
4219     }
4220 
4221   if (type != CPP_STRING && !wide_ok)
4222     {
4223       cp_parser_error (parser, "a wide string is invalid in this context");
4224       type = CPP_STRING;
4225     }
4226 
4227   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4228       (parse_in, strs, count, &istr, type))
4229     {
4230       value = build_string (istr.len, (const char *)istr.text);
4231       free (CONST_CAST (unsigned char *, istr.text));
4232       if (count > 1)
4233 	{
4234 	  location_t *locs = (location_t *)obstack_finish (&loc_ob);
4235 	  gcc_assert (g_string_concat_db);
4236 	  g_string_concat_db->record_string_concatenation (count, locs);
4237 	}
4238 
4239       switch (type)
4240 	{
4241 	default:
4242 	case CPP_STRING:
4243 	  TREE_TYPE (value) = char_array_type_node;
4244 	  break;
4245 	case CPP_UTF8STRING:
4246 	  if (flag_char8_t)
4247 	    TREE_TYPE (value) = char8_array_type_node;
4248 	  else
4249 	    TREE_TYPE (value) = char_array_type_node;
4250 	  break;
4251 	case CPP_STRING16:
4252 	  TREE_TYPE (value) = char16_array_type_node;
4253 	  break;
4254 	case CPP_STRING32:
4255 	  TREE_TYPE (value) = char32_array_type_node;
4256 	  break;
4257 	case CPP_WSTRING:
4258 	  TREE_TYPE (value) = wchar_array_type_node;
4259 	  break;
4260 	}
4261 
4262       value = fix_string_type (value);
4263 
4264       if (have_suffix_p)
4265 	{
4266 	  tree literal = build_userdef_literal (suffix_id, value,
4267 						OT_NONE, NULL_TREE);
4268 	  if (lookup_udlit)
4269 	    value = cp_parser_userdef_string_literal (literal);
4270 	  else
4271 	    value = literal;
4272 	}
4273     }
4274   else
4275     /* cpp_interpret_string has issued an error.  */
4276     value = error_mark_node;
4277 
4278   if (count > 1)
4279     {
4280       obstack_free (&str_ob, 0);
4281       obstack_free (&loc_ob, 0);
4282     }
4283 
4284   return cp_expr (value, loc);
4285 }
4286 
4287 /* Look up a literal operator with the name and the exact arguments.  */
4288 
4289 static tree
lookup_literal_operator(tree name,vec<tree,va_gc> * args)4290 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4291 {
4292   tree decl = lookup_name (name);
4293   if (!decl || !is_overloaded_fn (decl))
4294     return error_mark_node;
4295 
4296   for (lkp_iterator iter (decl); iter; ++iter)
4297     {
4298       tree fn = *iter;
4299 
4300       if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4301 	{
4302 	  unsigned int ix;
4303 	  bool found = true;
4304 
4305 	  for (ix = 0;
4306 	       found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4307 	       ++ix, parmtypes = TREE_CHAIN (parmtypes))
4308 	    {
4309 	      tree tparm = TREE_VALUE (parmtypes);
4310 	      tree targ = TREE_TYPE ((*args)[ix]);
4311 	      bool ptr = TYPE_PTR_P (tparm);
4312 	      bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4313 	      if ((ptr || arr || !same_type_p (tparm, targ))
4314 		  && (!ptr || !arr
4315 		      || !same_type_p (TREE_TYPE (tparm),
4316 				       TREE_TYPE (targ))))
4317 		found = false;
4318 	    }
4319 
4320 	  if (found
4321 	      && ix == vec_safe_length (args)
4322 	      /* May be this should be sufficient_parms_p instead,
4323 		 depending on how exactly should user-defined literals
4324 		 work in presence of default arguments on the literal
4325 		 operator parameters.  */
4326 	      && parmtypes == void_list_node)
4327 	    return decl;
4328 	}
4329     }
4330 
4331   return error_mark_node;
4332 }
4333 
4334 /* Parse a user-defined char constant.  Returns a call to a user-defined
4335    literal operator taking the character as an argument.  */
4336 
4337 static cp_expr
cp_parser_userdef_char_literal(cp_parser * parser)4338 cp_parser_userdef_char_literal (cp_parser *parser)
4339 {
4340   cp_token *token = cp_lexer_consume_token (parser->lexer);
4341   tree literal = token->u.value;
4342   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4343   tree value = USERDEF_LITERAL_VALUE (literal);
4344   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4345   tree decl, result;
4346 
4347   /* Build up a call to the user-defined operator  */
4348   /* Lookup the name we got back from the id-expression.  */
4349   vec<tree, va_gc> *args = make_tree_vector ();
4350   vec_safe_push (args, value);
4351   decl = lookup_literal_operator (name, args);
4352   if (!decl || decl == error_mark_node)
4353     {
4354       error ("unable to find character literal operator %qD with %qT argument",
4355 	     name, TREE_TYPE (value));
4356       release_tree_vector (args);
4357       return error_mark_node;
4358     }
4359   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4360   release_tree_vector (args);
4361   return result;
4362 }
4363 
4364 /* A subroutine of cp_parser_userdef_numeric_literal to
4365    create a char... template parameter pack from a string node.  */
4366 
4367 static tree
make_char_string_pack(tree value)4368 make_char_string_pack (tree value)
4369 {
4370   tree charvec;
4371   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4372   const char *str = TREE_STRING_POINTER (value);
4373   int i, len = TREE_STRING_LENGTH (value) - 1;
4374   tree argvec = make_tree_vec (1);
4375 
4376   /* Fill in CHARVEC with all of the parameters.  */
4377   charvec = make_tree_vec (len);
4378   for (i = 0; i < len; ++i)
4379     {
4380       unsigned char s[3] = { '\'', str[i], '\'' };
4381       cpp_string in = { 3, s };
4382       cpp_string out = { 0, 0 };
4383       if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4384 	return NULL_TREE;
4385       gcc_assert (out.len == 2);
4386       TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4387 						 out.text[0]);
4388     }
4389 
4390   /* Build the argument packs.  */
4391   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4392 
4393   TREE_VEC_ELT (argvec, 0) = argpack;
4394 
4395   return argvec;
4396 }
4397 
4398 /* A subroutine of cp_parser_userdef_numeric_literal to
4399    create a char... template parameter pack from a string node.  */
4400 
4401 static tree
make_string_pack(tree value)4402 make_string_pack (tree value)
4403 {
4404   tree charvec;
4405   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4406   const unsigned char *str
4407     = (const unsigned char *) TREE_STRING_POINTER (value);
4408   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4409   int len = TREE_STRING_LENGTH (value) / sz - 1;
4410   tree argvec = make_tree_vec (2);
4411 
4412   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4413   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4414 
4415   /* First template parm is character type.  */
4416   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4417 
4418   /* Fill in CHARVEC with all of the parameters.  */
4419   charvec = make_tree_vec (len);
4420   for (int i = 0; i < len; ++i)
4421     TREE_VEC_ELT (charvec, i)
4422       = double_int_to_tree (str_char_type_node,
4423 			    double_int::from_buffer (str + i * sz, sz));
4424 
4425   /* Build the argument packs.  */
4426   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4427 
4428   TREE_VEC_ELT (argvec, 1) = argpack;
4429 
4430   return argvec;
4431 }
4432 
4433 /* Parse a user-defined numeric constant.  returns a call to a user-defined
4434    literal operator.  */
4435 
4436 static cp_expr
cp_parser_userdef_numeric_literal(cp_parser * parser)4437 cp_parser_userdef_numeric_literal (cp_parser *parser)
4438 {
4439   cp_token *token = cp_lexer_consume_token (parser->lexer);
4440   tree literal = token->u.value;
4441   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4442   tree value = USERDEF_LITERAL_VALUE (literal);
4443   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4444   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4445   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4446   tree decl, result;
4447   vec<tree, va_gc> *args;
4448 
4449   /* Look for a literal operator taking the exact type of numeric argument
4450      as the literal value.  */
4451   args = make_tree_vector ();
4452   vec_safe_push (args, value);
4453   decl = lookup_literal_operator (name, args);
4454   if (decl && decl != error_mark_node)
4455     {
4456       result = finish_call_expr (decl, &args, false, true,
4457 				 tf_warning_or_error);
4458 
4459       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4460 	{
4461 	  warning_at (token->location, OPT_Woverflow,
4462 		      "integer literal exceeds range of %qT type",
4463 		      long_long_unsigned_type_node);
4464 	}
4465       else
4466 	{
4467 	  if (overflow > 0)
4468 	    warning_at (token->location, OPT_Woverflow,
4469 			"floating literal exceeds range of %qT type",
4470 			long_double_type_node);
4471 	  else if (overflow < 0)
4472 	    warning_at (token->location, OPT_Woverflow,
4473 			"floating literal truncated to zero");
4474 	}
4475 
4476       release_tree_vector (args);
4477       return result;
4478     }
4479   release_tree_vector (args);
4480 
4481   /* If the numeric argument didn't work, look for a raw literal
4482      operator taking a const char* argument consisting of the number
4483      in string format.  */
4484   args = make_tree_vector ();
4485   vec_safe_push (args, num_string);
4486   decl = lookup_literal_operator (name, args);
4487   if (decl && decl != error_mark_node)
4488     {
4489       result = finish_call_expr (decl, &args, false, true,
4490 				 tf_warning_or_error);
4491       release_tree_vector (args);
4492       return result;
4493     }
4494   release_tree_vector (args);
4495 
4496   /* If the raw literal didn't work, look for a non-type template
4497      function with parameter pack char....  Call the function with
4498      template parameter characters representing the number.  */
4499   args = make_tree_vector ();
4500   decl = lookup_literal_operator (name, args);
4501   if (decl && decl != error_mark_node)
4502     {
4503       tree tmpl_args = make_char_string_pack (num_string);
4504       if (tmpl_args == NULL_TREE)
4505 	{
4506 	  error ("failed to translate literal to execution character set %qT",
4507 		 num_string);
4508 	  return error_mark_node;
4509 	}
4510       decl = lookup_template_function (decl, tmpl_args);
4511       result = finish_call_expr (decl, &args, false, true,
4512 				 tf_warning_or_error);
4513       release_tree_vector (args);
4514       return result;
4515     }
4516 
4517   release_tree_vector (args);
4518 
4519   /* In C++14 the standard library defines complex number suffixes that
4520      conflict with GNU extensions.  Prefer them if <complex> is #included.  */
4521   bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4522   bool i14 = (cxx_dialect > cxx11
4523 	      && (id_equal (suffix_id, "i")
4524 		  || id_equal (suffix_id, "if")
4525 		  || id_equal (suffix_id, "il")));
4526   diagnostic_t kind = DK_ERROR;
4527   int opt = 0;
4528 
4529   if (i14 && ext)
4530     {
4531       tree cxlit = lookup_qualified_name (std_node,
4532 					  get_identifier ("complex_literals"),
4533 					  0, false, false);
4534       if (cxlit == error_mark_node)
4535 	{
4536 	  /* No <complex>, so pedwarn and use GNU semantics.  */
4537 	  kind = DK_PEDWARN;
4538 	  opt = OPT_Wpedantic;
4539 	}
4540     }
4541 
4542   bool complained
4543     = emit_diagnostic (kind, input_location, opt,
4544 		       "unable to find numeric literal operator %qD", name);
4545 
4546   if (!complained)
4547     /* Don't inform either.  */;
4548   else if (i14)
4549     {
4550       inform (token->location, "add %<using namespace std::complex_literals%> "
4551 	      "(from <complex>) to enable the C++14 user-defined literal "
4552 	      "suffixes");
4553       if (ext)
4554 	inform (token->location, "or use %<j%> instead of %<i%> for the "
4555 		"GNU built-in suffix");
4556     }
4557   else if (!ext)
4558     inform (token->location, "use %<-fext-numeric-literals%> "
4559 	    "to enable more built-in suffixes");
4560 
4561   if (kind == DK_ERROR)
4562     value = error_mark_node;
4563   else
4564     {
4565       /* Use the built-in semantics.  */
4566       tree type;
4567       if (id_equal (suffix_id, "i"))
4568 	{
4569 	  if (TREE_CODE (value) == INTEGER_CST)
4570 	    type = integer_type_node;
4571 	  else
4572 	    type = double_type_node;
4573 	}
4574       else if (id_equal (suffix_id, "if"))
4575 	type = float_type_node;
4576       else /* if (id_equal (suffix_id, "il")) */
4577 	type = long_double_type_node;
4578 
4579       value = build_complex (build_complex_type (type),
4580 			     fold_convert (type, integer_zero_node),
4581 			     fold_convert (type, value));
4582     }
4583 
4584   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4585     /* Avoid repeated diagnostics.  */
4586     token->u.value = value;
4587   return value;
4588 }
4589 
4590 /* Parse a user-defined string constant.  Returns a call to a user-defined
4591    literal operator taking a character pointer and the length of the string
4592    as arguments.  */
4593 
4594 static tree
cp_parser_userdef_string_literal(tree literal)4595 cp_parser_userdef_string_literal (tree literal)
4596 {
4597   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4598   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4599   tree value = USERDEF_LITERAL_VALUE (literal);
4600   int len = TREE_STRING_LENGTH (value)
4601 	/ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4602   tree decl;
4603 
4604   /* Build up a call to the user-defined operator.  */
4605   /* Lookup the name we got back from the id-expression.  */
4606   releasing_vec rargs;
4607   vec<tree, va_gc> *&args = rargs.get_ref();
4608   vec_safe_push (args, value);
4609   vec_safe_push (args, build_int_cst (size_type_node, len));
4610   decl = lookup_literal_operator (name, args);
4611 
4612   if (decl && decl != error_mark_node)
4613     return finish_call_expr (decl, &args, false, true,
4614 			     tf_warning_or_error);
4615 
4616   /* Look for a suitable template function, either (C++20) with a single
4617      parameter of class type, or (N3599) with typename parameter CharT and
4618      parameter pack CharT...  */
4619   args->truncate (0);
4620   decl = lookup_literal_operator (name, args);
4621   if (decl && decl != error_mark_node)
4622     {
4623       /* Use resolve_nondeduced_context to try to choose one form of template
4624 	 or the other.  */
4625       tree tmpl_args = make_tree_vec (1);
4626       TREE_VEC_ELT (tmpl_args, 0) = value;
4627       decl = lookup_template_function (decl, tmpl_args);
4628       tree res = resolve_nondeduced_context (decl, tf_none);
4629       if (DECL_P (res))
4630 	decl = res;
4631       else
4632 	{
4633 	  TREE_OPERAND (decl, 1) = make_string_pack (value);
4634 	  res = resolve_nondeduced_context (decl, tf_none);
4635 	  if (DECL_P (res))
4636 	    decl = res;
4637 	}
4638       if (!DECL_P (decl) && cxx_dialect > cxx17)
4639 	TREE_OPERAND (decl, 1) = tmpl_args;
4640       return finish_call_expr (decl, &args, false, true,
4641 			       tf_warning_or_error);
4642     }
4643 
4644   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4645 	 name, TREE_TYPE (value), size_type_node);
4646   return error_mark_node;
4647 }
4648 
4649 
4650 /* Basic concepts [gram.basic]  */
4651 
4652 /* Parse a translation-unit.
4653 
4654    translation-unit:
4655      declaration-seq [opt]  */
4656 
4657 static void
cp_parser_translation_unit(cp_parser * parser)4658 cp_parser_translation_unit (cp_parser* parser)
4659 {
4660   gcc_checking_assert (!cp_error_declarator);
4661 
4662   /* Create the declarator obstack.  */
4663   gcc_obstack_init (&declarator_obstack);
4664   /* Create the error declarator.  */
4665   cp_error_declarator = make_declarator (cdk_error);
4666   /* Create the empty parameter list.  */
4667   no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4668 					     UNKNOWN_LOCATION);
4669   /* Remember where the base of the declarator obstack lies.  */
4670   void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4671 
4672   bool implicit_extern_c = false;
4673 
4674   for (;;)
4675     {
4676       cp_token *token = cp_lexer_peek_token (parser->lexer);
4677 
4678       /* If we're entering or exiting a region that's implicitly
4679 	 extern "C", modify the lang context appropriately.  */
4680       if (implicit_extern_c
4681 	  != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4682 	{
4683 	  implicit_extern_c = !implicit_extern_c;
4684 	  if (implicit_extern_c)
4685 	    push_lang_context (lang_name_c);
4686 	  else
4687 	    pop_lang_context ();
4688 	}
4689 
4690       if (token->type == CPP_EOF)
4691 	break;
4692 
4693       if (token->type == CPP_CLOSE_BRACE)
4694 	{
4695 	  cp_parser_error (parser, "expected declaration");
4696 	  cp_lexer_consume_token (parser->lexer);
4697 	  /* If the next token is now a `;', consume it.  */
4698 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4699 	    cp_lexer_consume_token (parser->lexer);
4700 	}
4701       else
4702 	cp_parser_toplevel_declaration (parser);
4703     }
4704 
4705   /* Get rid of the token array; we don't need it any more.  */
4706   cp_lexer_destroy (parser->lexer);
4707   parser->lexer = NULL;
4708 
4709   /* The EOF should have reset this. */
4710   gcc_checking_assert (!implicit_extern_c);
4711 
4712   /* Make sure the declarator obstack was fully cleaned up.  */
4713   gcc_assert (obstack_next_free (&declarator_obstack)
4714 	      == declarator_obstack_base);
4715 }
4716 
4717 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4718    decltype context.  */
4719 
4720 static inline tsubst_flags_t
complain_flags(bool decltype_p)4721 complain_flags (bool decltype_p)
4722 {
4723   tsubst_flags_t complain = tf_warning_or_error;
4724   if (decltype_p)
4725     complain |= tf_decltype;
4726   return complain;
4727 }
4728 
4729 /* We're about to parse a collection of statements.  If we're currently
4730    parsing tentatively, set up a firewall so that any nested
4731    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4732 
4733 static cp_token_position
cp_parser_start_tentative_firewall(cp_parser * parser)4734 cp_parser_start_tentative_firewall (cp_parser *parser)
4735 {
4736   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4737     return 0;
4738 
4739   cp_parser_parse_tentatively (parser);
4740   cp_parser_commit_to_topmost_tentative_parse (parser);
4741   return cp_lexer_token_position (parser->lexer, false);
4742 }
4743 
4744 /* We've finished parsing the collection of statements.  Wrap up the
4745    firewall and replace the relevant tokens with the parsed form.  */
4746 
4747 static void
cp_parser_end_tentative_firewall(cp_parser * parser,cp_token_position start,tree expr)4748 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4749 				  tree expr)
4750 {
4751   if (!start)
4752     return;
4753 
4754   /* Finish the firewall level.  */
4755   cp_parser_parse_definitely (parser);
4756   /* And remember the result of the parse for when we try again.  */
4757   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4758   token->type = CPP_PREPARSED_EXPR;
4759   token->u.value = expr;
4760   token->keyword = RID_MAX;
4761   cp_lexer_purge_tokens_after (parser->lexer, start);
4762 }
4763 
4764 /* Like the above functions, but let the user modify the tokens.  Used by
4765    CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4766    later parses, so it makes sense to localize the effects of
4767    cp_parser_commit_to_tentative_parse.  */
4768 
4769 struct tentative_firewall
4770 {
4771   cp_parser *parser;
4772   bool set;
4773 
tentative_firewalltentative_firewall4774   tentative_firewall (cp_parser *p): parser(p)
4775   {
4776     /* If we're currently parsing tentatively, start a committed level as a
4777        firewall and then an inner tentative parse.  */
4778     if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4779       {
4780 	cp_parser_parse_tentatively (parser);
4781 	cp_parser_commit_to_topmost_tentative_parse (parser);
4782 	cp_parser_parse_tentatively (parser);
4783       }
4784   }
4785 
~tentative_firewalltentative_firewall4786   ~tentative_firewall()
4787   {
4788     if (set)
4789       {
4790 	/* Finish the inner tentative parse and the firewall, propagating any
4791 	   uncommitted error state to the outer tentative parse.  */
4792 	bool err = cp_parser_error_occurred (parser);
4793 	cp_parser_parse_definitely (parser);
4794 	cp_parser_parse_definitely (parser);
4795 	if (err)
4796 	  cp_parser_simulate_error (parser);
4797       }
4798   }
4799 };
4800 
4801 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4802    This class is for tracking such a matching pair of symbols.
4803    In particular, it tracks the location of the first token,
4804    so that if the second token is missing, we can highlight the
4805    location of the first token when notifying the user about the
4806    problem.  */
4807 
4808 template <typename traits_t>
4809 class token_pair
4810 {
4811  public:
4812   /* token_pair's ctor.  */
token_pair()4813   token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4814 
4815   /* If the next token is the opening symbol for this pair, consume it and
4816      return true.
4817      Otherwise, issue an error and return false.
4818      In either case, record the location of the opening token.  */
4819 
require_open(cp_parser * parser)4820   bool require_open (cp_parser *parser)
4821   {
4822     m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4823     return cp_parser_require (parser, traits_t::open_token_type,
4824 			      traits_t::required_token_open);
4825   }
4826 
4827   /* Consume the next token from PARSER, recording its location as
4828      that of the opening token within the pair.  */
4829 
consume_open(cp_parser * parser)4830   cp_token * consume_open (cp_parser *parser)
4831   {
4832     cp_token *tok = cp_lexer_consume_token (parser->lexer);
4833     gcc_assert (tok->type == traits_t::open_token_type);
4834     m_open_loc = tok->location;
4835     return tok;
4836   }
4837 
4838   /* If the next token is the closing symbol for this pair, consume it
4839      and return it.
4840      Otherwise, issue an error, highlighting the location of the
4841      corresponding opening token, and return NULL.  */
4842 
require_close(cp_parser * parser)4843   cp_token *require_close (cp_parser *parser) const
4844   {
4845     return cp_parser_require (parser, traits_t::close_token_type,
4846 			      traits_t::required_token_close,
4847 			      m_open_loc);
4848   }
4849 
4850  private:
4851   location_t m_open_loc;
4852 };
4853 
4854 /* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
4855 
4856 struct matching_paren_traits
4857 {
4858   static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4859   static const enum required_token required_token_open  = RT_OPEN_PAREN;
4860   static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4861   static const enum required_token required_token_close = RT_CLOSE_PAREN;
4862 };
4863 
4864 /* "matching_parens" is a token_pair<T> class for tracking matching
4865    pairs of parentheses.  */
4866 
4867 typedef token_pair<matching_paren_traits> matching_parens;
4868 
4869 /* Traits for token_pair<T> for tracking matching pairs of braces.  */
4870 
4871 struct matching_brace_traits
4872 {
4873   static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4874   static const enum required_token required_token_open = RT_OPEN_BRACE;
4875   static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4876   static const enum required_token required_token_close = RT_CLOSE_BRACE;
4877 };
4878 
4879 /* "matching_braces" is a token_pair<T> class for tracking matching
4880    pairs of braces.  */
4881 
4882 typedef token_pair<matching_brace_traits> matching_braces;
4883 
4884 
4885 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4886    enclosing parentheses.  */
4887 
4888 static cp_expr
cp_parser_statement_expr(cp_parser * parser)4889 cp_parser_statement_expr (cp_parser *parser)
4890 {
4891   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4892 
4893   /* Consume the '('.  */
4894   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4895   matching_parens parens;
4896   parens.consume_open (parser);
4897   /* Start the statement-expression.  */
4898   tree expr = begin_stmt_expr ();
4899   /* Parse the compound-statement.  */
4900   cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4901   /* Finish up.  */
4902   expr = finish_stmt_expr (expr, false);
4903   /* Consume the ')'.  */
4904   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4905   if (!parens.require_close (parser))
4906     cp_parser_skip_to_end_of_statement (parser);
4907 
4908   cp_parser_end_tentative_firewall (parser, start, expr);
4909   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4910   return cp_expr (expr, combined_loc);
4911 }
4912 
4913 /* Expressions [gram.expr] */
4914 
4915 /* Parse a fold-operator.
4916 
4917     fold-operator:
4918         -  *  /  %  ^  &  |  =  <  >  <<  >>
4919       =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
4920       ==  !=  <=  >=  &&  ||  ,  .*  ->*
4921 
4922    This returns the tree code corresponding to the matched operator
4923    as an int. When the current token matches a compound assignment
4924    opertor, the resulting tree code is the negative value of the
4925    non-assignment operator. */
4926 
4927 static int
cp_parser_fold_operator(cp_token * token)4928 cp_parser_fold_operator (cp_token *token)
4929 {
4930   switch (token->type)
4931     {
4932     case CPP_PLUS: return PLUS_EXPR;
4933     case CPP_MINUS: return MINUS_EXPR;
4934     case CPP_MULT: return MULT_EXPR;
4935     case CPP_DIV: return TRUNC_DIV_EXPR;
4936     case CPP_MOD: return TRUNC_MOD_EXPR;
4937     case CPP_XOR: return BIT_XOR_EXPR;
4938     case CPP_AND: return BIT_AND_EXPR;
4939     case CPP_OR: return BIT_IOR_EXPR;
4940     case CPP_LSHIFT: return LSHIFT_EXPR;
4941     case CPP_RSHIFT: return RSHIFT_EXPR;
4942 
4943     case CPP_EQ: return -NOP_EXPR;
4944     case CPP_PLUS_EQ: return -PLUS_EXPR;
4945     case CPP_MINUS_EQ: return -MINUS_EXPR;
4946     case CPP_MULT_EQ: return -MULT_EXPR;
4947     case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4948     case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4949     case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4950     case CPP_AND_EQ: return -BIT_AND_EXPR;
4951     case CPP_OR_EQ: return -BIT_IOR_EXPR;
4952     case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4953     case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4954 
4955     case CPP_EQ_EQ: return EQ_EXPR;
4956     case CPP_NOT_EQ: return NE_EXPR;
4957     case CPP_LESS: return LT_EXPR;
4958     case CPP_GREATER: return GT_EXPR;
4959     case CPP_LESS_EQ: return LE_EXPR;
4960     case CPP_GREATER_EQ: return GE_EXPR;
4961 
4962     case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4963     case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4964 
4965     case CPP_COMMA: return COMPOUND_EXPR;
4966 
4967     case CPP_DOT_STAR: return DOTSTAR_EXPR;
4968     case CPP_DEREF_STAR: return MEMBER_REF;
4969 
4970     default: return ERROR_MARK;
4971     }
4972 }
4973 
4974 /* Returns true if CODE indicates a binary expression, which is not allowed in
4975    the LHS of a fold-expression.  More codes will need to be added to use this
4976    function in other contexts.  */
4977 
4978 static bool
is_binary_op(tree_code code)4979 is_binary_op (tree_code code)
4980 {
4981   switch (code)
4982     {
4983     case PLUS_EXPR:
4984     case POINTER_PLUS_EXPR:
4985     case MINUS_EXPR:
4986     case MULT_EXPR:
4987     case TRUNC_DIV_EXPR:
4988     case TRUNC_MOD_EXPR:
4989     case BIT_XOR_EXPR:
4990     case BIT_AND_EXPR:
4991     case BIT_IOR_EXPR:
4992     case LSHIFT_EXPR:
4993     case RSHIFT_EXPR:
4994 
4995     case MODOP_EXPR:
4996 
4997     case EQ_EXPR:
4998     case NE_EXPR:
4999     case LE_EXPR:
5000     case GE_EXPR:
5001     case LT_EXPR:
5002     case GT_EXPR:
5003 
5004     case TRUTH_ANDIF_EXPR:
5005     case TRUTH_ORIF_EXPR:
5006 
5007     case COMPOUND_EXPR:
5008 
5009     case DOTSTAR_EXPR:
5010     case MEMBER_REF:
5011       return true;
5012 
5013     default:
5014       return false;
5015     }
5016 }
5017 
5018 /* If the next token is a suitable fold operator, consume it and return as
5019    the function above.  */
5020 
5021 static int
cp_parser_fold_operator(cp_parser * parser)5022 cp_parser_fold_operator (cp_parser *parser)
5023 {
5024   cp_token* token = cp_lexer_peek_token (parser->lexer);
5025   int code = cp_parser_fold_operator (token);
5026   if (code != ERROR_MARK)
5027     cp_lexer_consume_token (parser->lexer);
5028   return code;
5029 }
5030 
5031 /* Parse a fold-expression.
5032 
5033      fold-expression:
5034        ( ... folding-operator cast-expression)
5035        ( cast-expression folding-operator ... )
5036        ( cast-expression folding operator ... folding-operator cast-expression)
5037 
5038    Note that the '(' and ')' are matched in primary expression. */
5039 
5040 static cp_expr
cp_parser_fold_expression(cp_parser * parser,tree expr1)5041 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5042 {
5043   cp_id_kind pidk;
5044 
5045   // Left fold.
5046   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5047     {
5048       cp_lexer_consume_token (parser->lexer);
5049       int op = cp_parser_fold_operator (parser);
5050       if (op == ERROR_MARK)
5051         {
5052           cp_parser_error (parser, "expected binary operator");
5053           return error_mark_node;
5054         }
5055 
5056       tree expr = cp_parser_cast_expression (parser, false, false,
5057 					     false, &pidk);
5058       if (expr == error_mark_node)
5059         return error_mark_node;
5060       return finish_left_unary_fold_expr (expr, op);
5061     }
5062 
5063   const cp_token* token = cp_lexer_peek_token (parser->lexer);
5064   int op = cp_parser_fold_operator (parser);
5065   if (op == ERROR_MARK)
5066     {
5067       cp_parser_error (parser, "expected binary operator");
5068       return error_mark_node;
5069     }
5070 
5071   if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5072     {
5073       cp_parser_error (parser, "expected ...");
5074       return error_mark_node;
5075     }
5076   cp_lexer_consume_token (parser->lexer);
5077 
5078   /* The operands of a fold-expression are cast-expressions, so binary or
5079      conditional expressions are not allowed.  We check this here to avoid
5080      tentative parsing.  */
5081   if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5082     /* OK, the expression was parenthesized.  */;
5083   else if (is_binary_op (TREE_CODE (expr1)))
5084     error_at (location_of (expr1),
5085 	      "binary expression in operand of fold-expression");
5086   else if (TREE_CODE (expr1) == COND_EXPR
5087 	   || (REFERENCE_REF_P (expr1)
5088 	       && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5089     error_at (location_of (expr1),
5090 	      "conditional expression in operand of fold-expression");
5091 
5092   // Right fold.
5093   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5094     return finish_right_unary_fold_expr (expr1, op);
5095 
5096   if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5097     {
5098       cp_parser_error (parser, "mismatched operator in fold-expression");
5099       return error_mark_node;
5100     }
5101   cp_lexer_consume_token (parser->lexer);
5102 
5103   // Binary left or right fold.
5104   tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5105   if (expr2 == error_mark_node)
5106     return error_mark_node;
5107   return finish_binary_fold_expr (expr1, expr2, op);
5108 }
5109 
5110 /* Parse a primary-expression.
5111 
5112    primary-expression:
5113      literal
5114      this
5115      ( expression )
5116      id-expression
5117      lambda-expression (C++11)
5118 
5119    GNU Extensions:
5120 
5121    primary-expression:
5122      ( compound-statement )
5123      __builtin_va_arg ( assignment-expression , type-id )
5124      __builtin_offsetof ( type-id , offsetof-expression )
5125 
5126    C++ Extensions:
5127      __has_nothrow_assign ( type-id )
5128      __has_nothrow_constructor ( type-id )
5129      __has_nothrow_copy ( type-id )
5130      __has_trivial_assign ( type-id )
5131      __has_trivial_constructor ( type-id )
5132      __has_trivial_copy ( type-id )
5133      __has_trivial_destructor ( type-id )
5134      __has_virtual_destructor ( type-id )
5135      __is_abstract ( type-id )
5136      __is_base_of ( type-id , type-id )
5137      __is_class ( type-id )
5138      __is_empty ( type-id )
5139      __is_enum ( type-id )
5140      __is_final ( type-id )
5141      __is_literal_type ( type-id )
5142      __is_pod ( type-id )
5143      __is_polymorphic ( type-id )
5144      __is_std_layout ( type-id )
5145      __is_trivial ( type-id )
5146      __is_union ( type-id )
5147 
5148    Objective-C++ Extension:
5149 
5150    primary-expression:
5151      objc-expression
5152 
5153    literal:
5154      __null
5155 
5156    ADDRESS_P is true iff this expression was immediately preceded by
5157    "&" and therefore might denote a pointer-to-member.  CAST_P is true
5158    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
5159    true iff this expression is a template argument.
5160 
5161    Returns a representation of the expression.  Upon return, *IDK
5162    indicates what kind of id-expression (if any) was present.  */
5163 
5164 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)5165 cp_parser_primary_expression (cp_parser *parser,
5166 			      bool address_p,
5167 			      bool cast_p,
5168 			      bool template_arg_p,
5169 			      bool decltype_p,
5170 			      cp_id_kind *idk)
5171 {
5172   cp_token *token = NULL;
5173 
5174   /* Assume the primary expression is not an id-expression.  */
5175   *idk = CP_ID_KIND_NONE;
5176 
5177   /* Peek at the next token.  */
5178   token = cp_lexer_peek_token (parser->lexer);
5179   switch ((int) token->type)
5180     {
5181       /* literal:
5182 	   integer-literal
5183 	   character-literal
5184 	   floating-literal
5185 	   string-literal
5186 	   boolean-literal
5187 	   pointer-literal
5188 	   user-defined-literal  */
5189     case CPP_CHAR:
5190     case CPP_CHAR16:
5191     case CPP_CHAR32:
5192     case CPP_WCHAR:
5193     case CPP_UTF8CHAR:
5194     case CPP_NUMBER:
5195     case CPP_PREPARSED_EXPR:
5196       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5197 	return cp_parser_userdef_numeric_literal (parser);
5198       token = cp_lexer_consume_token (parser->lexer);
5199       if (TREE_CODE (token->u.value) == FIXED_CST)
5200 	{
5201 	  error_at (token->location,
5202 		    "fixed-point types not supported in C++");
5203 	  return error_mark_node;
5204 	}
5205       /* Floating-point literals are only allowed in an integral
5206 	 constant expression if they are cast to an integral or
5207 	 enumeration type.  */
5208       if (TREE_CODE (token->u.value) == REAL_CST
5209 	  && parser->integral_constant_expression_p
5210 	  && pedantic)
5211 	{
5212 	  /* CAST_P will be set even in invalid code like "int(2.7 +
5213 	     ...)".   Therefore, we have to check that the next token
5214 	     is sure to end the cast.  */
5215 	  if (cast_p)
5216 	    {
5217 	      cp_token *next_token;
5218 
5219 	      next_token = cp_lexer_peek_token (parser->lexer);
5220 	      if (/* The comma at the end of an
5221 		     enumerator-definition.  */
5222 		  next_token->type != CPP_COMMA
5223 		  /* The curly brace at the end of an enum-specifier.  */
5224 		  && next_token->type != CPP_CLOSE_BRACE
5225 		  /* The end of a statement.  */
5226 		  && next_token->type != CPP_SEMICOLON
5227 		  /* The end of the cast-expression.  */
5228 		  && next_token->type != CPP_CLOSE_PAREN
5229 		  /* The end of an array bound.  */
5230 		  && next_token->type != CPP_CLOSE_SQUARE
5231 		  /* The closing ">" in a template-argument-list.  */
5232 		  && (next_token->type != CPP_GREATER
5233 		      || parser->greater_than_is_operator_p)
5234 		  /* C++0x only: A ">>" treated like two ">" tokens,
5235                      in a template-argument-list.  */
5236 		  && (next_token->type != CPP_RSHIFT
5237                       || (cxx_dialect == cxx98)
5238 		      || parser->greater_than_is_operator_p))
5239 		cast_p = false;
5240 	    }
5241 
5242 	  /* If we are within a cast, then the constraint that the
5243 	     cast is to an integral or enumeration type will be
5244 	     checked at that point.  If we are not within a cast, then
5245 	     this code is invalid.  */
5246 	  if (!cast_p)
5247 	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5248 	}
5249       return (cp_expr (token->u.value, token->location)
5250 	      .maybe_add_location_wrapper ());
5251 
5252     case CPP_CHAR_USERDEF:
5253     case CPP_CHAR16_USERDEF:
5254     case CPP_CHAR32_USERDEF:
5255     case CPP_WCHAR_USERDEF:
5256     case CPP_UTF8CHAR_USERDEF:
5257       return cp_parser_userdef_char_literal (parser);
5258 
5259     case CPP_STRING:
5260     case CPP_STRING16:
5261     case CPP_STRING32:
5262     case CPP_WSTRING:
5263     case CPP_UTF8STRING:
5264     case CPP_STRING_USERDEF:
5265     case CPP_STRING16_USERDEF:
5266     case CPP_STRING32_USERDEF:
5267     case CPP_WSTRING_USERDEF:
5268     case CPP_UTF8STRING_USERDEF:
5269       /* ??? Should wide strings be allowed when parser->translate_strings_p
5270 	 is false (i.e. in attributes)?  If not, we can kill the third
5271 	 argument to cp_parser_string_literal.  */
5272       return (cp_parser_string_literal (parser,
5273 					parser->translate_strings_p,
5274 					true)
5275 	      .maybe_add_location_wrapper ());
5276 
5277     case CPP_OPEN_PAREN:
5278       /* If we see `( { ' then we are looking at the beginning of
5279 	 a GNU statement-expression.  */
5280       if (cp_parser_allow_gnu_extensions_p (parser)
5281 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5282 	{
5283 	  /* Statement-expressions are not allowed by the standard.  */
5284 	  pedwarn (token->location, OPT_Wpedantic,
5285 		   "ISO C++ forbids braced-groups within expressions");
5286 
5287 	  /* And they're not allowed outside of a function-body; you
5288 	     cannot, for example, write:
5289 
5290 	     int i = ({ int j = 3; j + 1; });
5291 
5292 	     at class or namespace scope.  */
5293 	  if (!parser->in_function_body
5294 	      || parser->in_template_argument_list_p)
5295 	    {
5296 	      error_at (token->location,
5297 			"statement-expressions are not allowed outside "
5298 			"functions nor in template-argument lists");
5299 	      cp_parser_skip_to_end_of_block_or_statement (parser);
5300 	      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5301 		cp_lexer_consume_token (parser->lexer);
5302 	      return error_mark_node;
5303 	    }
5304 	  else
5305 	    return cp_parser_statement_expr (parser);
5306 	}
5307       /* Otherwise it's a normal parenthesized expression.  */
5308       {
5309 	cp_expr expr;
5310 	bool saved_greater_than_is_operator_p;
5311 
5312 	location_t open_paren_loc = token->location;
5313 
5314 	/* Consume the `('.  */
5315 	matching_parens parens;
5316 	parens.consume_open (parser);
5317 	/* Within a parenthesized expression, a `>' token is always
5318 	   the greater-than operator.  */
5319 	saved_greater_than_is_operator_p
5320 	  = parser->greater_than_is_operator_p;
5321 	parser->greater_than_is_operator_p = true;
5322 
5323 	if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5324 	  /* Left fold expression. */
5325 	  expr = NULL_TREE;
5326 	else
5327 	  /* Parse the parenthesized expression.  */
5328 	  expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5329 
5330 	token = cp_lexer_peek_token (parser->lexer);
5331 	if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5332 	  {
5333 	    expr = cp_parser_fold_expression (parser, expr);
5334 	    if (expr != error_mark_node
5335 		&& cxx_dialect < cxx17
5336 		&& !in_system_header_at (input_location))
5337 	      pedwarn (input_location, 0, "fold-expressions only available "
5338 		       "with %<-std=c++17%> or %<-std=gnu++17%>");
5339 	  }
5340 	else
5341 	  /* Let the front end know that this expression was
5342 	     enclosed in parentheses. This matters in case, for
5343 	     example, the expression is of the form `A::B', since
5344 	     `&A::B' might be a pointer-to-member, but `&(A::B)' is
5345 	     not.  */
5346 	  expr = finish_parenthesized_expr (expr);
5347 
5348 	/* DR 705: Wrapping an unqualified name in parentheses
5349 	   suppresses arg-dependent lookup.  We want to pass back
5350 	   CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5351 	   (c++/37862), but none of the others.  */
5352 	if (*idk != CP_ID_KIND_QUALIFIED)
5353 	  *idk = CP_ID_KIND_NONE;
5354 
5355 	/* The `>' token might be the end of a template-id or
5356 	   template-parameter-list now.  */
5357 	parser->greater_than_is_operator_p
5358 	  = saved_greater_than_is_operator_p;
5359 
5360 	/* Consume the `)'.  */
5361 	token = cp_lexer_peek_token (parser->lexer);
5362 	location_t close_paren_loc = token->location;
5363 	expr.set_range (open_paren_loc, close_paren_loc);
5364 	if (!parens.require_close (parser)
5365 	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5366 	  cp_parser_skip_to_end_of_statement (parser);
5367 
5368 	return expr;
5369       }
5370 
5371     case CPP_OPEN_SQUARE:
5372       {
5373 	if (c_dialect_objc ())
5374 	  {
5375 	    /* We might have an Objective-C++ message. */
5376 	    cp_parser_parse_tentatively (parser);
5377 	    tree msg = cp_parser_objc_message_expression (parser);
5378 	    /* If that works out, we're done ... */
5379 	    if (cp_parser_parse_definitely (parser))
5380 	      return msg;
5381 	    /* ... else, fall though to see if it's a lambda.  */
5382 	  }
5383 	cp_expr lam = cp_parser_lambda_expression (parser);
5384 	/* Don't warn about a failed tentative parse.  */
5385 	if (cp_parser_error_occurred (parser))
5386 	  return error_mark_node;
5387 	maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5388 	return lam;
5389       }
5390 
5391     case CPP_OBJC_STRING:
5392       if (c_dialect_objc ())
5393 	/* We have an Objective-C++ string literal. */
5394         return cp_parser_objc_expression (parser);
5395       cp_parser_error (parser, "expected primary-expression");
5396       return error_mark_node;
5397 
5398     case CPP_KEYWORD:
5399       switch (token->keyword)
5400 	{
5401 	  /* These two are the boolean literals.  */
5402 	case RID_TRUE:
5403 	  cp_lexer_consume_token (parser->lexer);
5404 	  return cp_expr (boolean_true_node, token->location);
5405 	case RID_FALSE:
5406 	  cp_lexer_consume_token (parser->lexer);
5407 	  return cp_expr (boolean_false_node, token->location);
5408 
5409 	  /* The `__null' literal.  */
5410 	case RID_NULL:
5411 	  cp_lexer_consume_token (parser->lexer);
5412 	  return cp_expr (null_node, token->location);
5413 
5414 	  /* The `nullptr' literal.  */
5415 	case RID_NULLPTR:
5416 	  cp_lexer_consume_token (parser->lexer);
5417 	  return cp_expr (nullptr_node, token->location);
5418 
5419 	  /* Recognize the `this' keyword.  */
5420 	case RID_THIS:
5421 	  cp_lexer_consume_token (parser->lexer);
5422 	  if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5423 	    {
5424 	      error_at (token->location,
5425 			"%<this%> may not be used in this context");
5426 	      return error_mark_node;
5427 	    }
5428 	  /* Pointers cannot appear in constant-expressions.  */
5429 	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5430 	    return error_mark_node;
5431 	  return cp_expr (finish_this_expr (), token->location);
5432 
5433 	  /* The `operator' keyword can be the beginning of an
5434 	     id-expression.  */
5435 	case RID_OPERATOR:
5436 	  goto id_expression;
5437 
5438 	case RID_FUNCTION_NAME:
5439 	case RID_PRETTY_FUNCTION_NAME:
5440 	case RID_C99_FUNCTION_NAME:
5441 	  {
5442 	    non_integral_constant name;
5443 
5444 	    /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5445 	       __func__ are the names of variables -- but they are
5446 	       treated specially.  Therefore, they are handled here,
5447 	       rather than relying on the generic id-expression logic
5448 	       below.  Grammatically, these names are id-expressions.
5449 
5450 	       Consume the token.  */
5451 	    token = cp_lexer_consume_token (parser->lexer);
5452 
5453 	    switch (token->keyword)
5454 	      {
5455 	      case RID_FUNCTION_NAME:
5456 		name = NIC_FUNC_NAME;
5457 		break;
5458 	      case RID_PRETTY_FUNCTION_NAME:
5459 		name = NIC_PRETTY_FUNC;
5460 		break;
5461 	      case RID_C99_FUNCTION_NAME:
5462 		name = NIC_C99_FUNC;
5463 		break;
5464 	      default:
5465 		gcc_unreachable ();
5466 	      }
5467 
5468 	    if (cp_parser_non_integral_constant_expression (parser, name))
5469 	      return error_mark_node;
5470 
5471 	    /* Look up the name.  */
5472 	    return finish_fname (token->u.value);
5473 	  }
5474 
5475 	case RID_VA_ARG:
5476 	  {
5477 	    tree expression;
5478 	    tree type;
5479 	    location_t type_location;
5480 	    location_t start_loc
5481 	      = cp_lexer_peek_token (parser->lexer)->location;
5482 	    /* The `__builtin_va_arg' construct is used to handle
5483 	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
5484 	    cp_lexer_consume_token (parser->lexer);
5485 	    /* Look for the opening `('.  */
5486 	    matching_parens parens;
5487 	    parens.require_open (parser);
5488 	    /* Now, parse the assignment-expression.  */
5489 	    expression = cp_parser_assignment_expression (parser);
5490 	    /* Look for the `,'.  */
5491 	    cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5492 	    type_location = cp_lexer_peek_token (parser->lexer)->location;
5493 	    /* Parse the type-id.  */
5494 	    {
5495 	      type_id_in_expr_sentinel s (parser);
5496 	      type = cp_parser_type_id (parser);
5497 	    }
5498 	    /* Look for the closing `)'.  */
5499 	    location_t finish_loc
5500 	      = cp_lexer_peek_token (parser->lexer)->location;
5501 	    parens.require_close (parser);
5502 	    /* Using `va_arg' in a constant-expression is not
5503 	       allowed.  */
5504 	    if (cp_parser_non_integral_constant_expression (parser,
5505 							    NIC_VA_ARG))
5506 	      return error_mark_node;
5507 	    /* Construct a location of the form:
5508 		 __builtin_va_arg (v, int)
5509 		 ~~~~~~~~~~~~~~~~~~~~~^~~~
5510 	       with the caret at the type, ranging from the start of the
5511 	       "__builtin_va_arg" token to the close paren.  */
5512 	    location_t combined_loc
5513 	      = make_location (type_location, start_loc, finish_loc);
5514 	    return build_x_va_arg (combined_loc, expression, type);
5515 	  }
5516 
5517 	case RID_OFFSETOF:
5518 	  return cp_parser_builtin_offsetof (parser);
5519 
5520 	case RID_HAS_NOTHROW_ASSIGN:
5521 	case RID_HAS_NOTHROW_CONSTRUCTOR:
5522 	case RID_HAS_NOTHROW_COPY:
5523 	case RID_HAS_TRIVIAL_ASSIGN:
5524 	case RID_HAS_TRIVIAL_CONSTRUCTOR:
5525 	case RID_HAS_TRIVIAL_COPY:
5526 	case RID_HAS_TRIVIAL_DESTRUCTOR:
5527 	case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5528 	case RID_HAS_VIRTUAL_DESTRUCTOR:
5529 	case RID_IS_ABSTRACT:
5530 	case RID_IS_AGGREGATE:
5531 	case RID_IS_BASE_OF:
5532 	case RID_IS_CLASS:
5533 	case RID_IS_EMPTY:
5534 	case RID_IS_ENUM:
5535 	case RID_IS_FINAL:
5536 	case RID_IS_LITERAL_TYPE:
5537 	case RID_IS_POD:
5538 	case RID_IS_POLYMORPHIC:
5539 	case RID_IS_SAME_AS:
5540 	case RID_IS_STD_LAYOUT:
5541 	case RID_IS_TRIVIAL:
5542 	case RID_IS_TRIVIALLY_ASSIGNABLE:
5543 	case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5544 	case RID_IS_TRIVIALLY_COPYABLE:
5545 	case RID_IS_UNION:
5546 	case RID_IS_ASSIGNABLE:
5547 	case RID_IS_CONSTRUCTIBLE:
5548 	  return cp_parser_trait_expr (parser, token->keyword);
5549 
5550 	// C++ concepts
5551 	case RID_REQUIRES:
5552 	  return cp_parser_requires_expression (parser);
5553 
5554 	/* Objective-C++ expressions.  */
5555 	case RID_AT_ENCODE:
5556 	case RID_AT_PROTOCOL:
5557 	case RID_AT_SELECTOR:
5558 	  return cp_parser_objc_expression (parser);
5559 
5560 	case RID_TEMPLATE:
5561 	  if (parser->in_function_body
5562 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5563 	      	  == CPP_LESS))
5564 	    {
5565 	      error_at (token->location,
5566 			"a template declaration cannot appear at block scope");
5567 	      cp_parser_skip_to_end_of_block_or_statement (parser);
5568 	      return error_mark_node;
5569 	    }
5570 	  /* FALLTHRU */
5571 	default:
5572 	  cp_parser_error (parser, "expected primary-expression");
5573 	  return error_mark_node;
5574 	}
5575 
5576       /* An id-expression can start with either an identifier, a
5577 	 `::' as the beginning of a qualified-id, or the "operator"
5578 	 keyword.  */
5579     case CPP_NAME:
5580     case CPP_SCOPE:
5581     case CPP_TEMPLATE_ID:
5582     case CPP_NESTED_NAME_SPECIFIER:
5583       {
5584       id_expression:
5585 	cp_expr id_expression;
5586 	cp_expr decl;
5587 	const char *error_msg;
5588 	bool template_p;
5589 	bool done;
5590 	cp_token *id_expr_token;
5591 
5592 	/* Parse the id-expression.  */
5593 	id_expression
5594 	  = cp_parser_id_expression (parser,
5595 				     /*template_keyword_p=*/false,
5596 				     /*check_dependency_p=*/true,
5597 				     &template_p,
5598 				     /*declarator_p=*/false,
5599 				     /*optional_p=*/false);
5600 	if (id_expression == error_mark_node)
5601 	  return error_mark_node;
5602 	id_expr_token = token;
5603 	token = cp_lexer_peek_token (parser->lexer);
5604 	done = (token->type != CPP_OPEN_SQUARE
5605 		&& token->type != CPP_OPEN_PAREN
5606 		&& token->type != CPP_DOT
5607 		&& token->type != CPP_DEREF
5608 		&& token->type != CPP_PLUS_PLUS
5609 		&& token->type != CPP_MINUS_MINUS);
5610 	/* If we have a template-id, then no further lookup is
5611 	   required.  If the template-id was for a template-class, we
5612 	   will sometimes have a TYPE_DECL at this point.  */
5613 	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5614 		 || TREE_CODE (id_expression) == TYPE_DECL)
5615 	  decl = id_expression;
5616 	/* Look up the name.  */
5617 	else
5618 	  {
5619 	    tree ambiguous_decls;
5620 
5621 	    /* If we already know that this lookup is ambiguous, then
5622 	       we've already issued an error message; there's no reason
5623 	       to check again.  */
5624 	    if (id_expr_token->type == CPP_NAME
5625 		&& id_expr_token->error_reported)
5626 	      {
5627 		cp_parser_simulate_error (parser);
5628 		return error_mark_node;
5629 	      }
5630 
5631 	    decl = cp_parser_lookup_name (parser, id_expression,
5632 					  none_type,
5633 					  template_p,
5634 					  /*is_namespace=*/false,
5635 					  /*check_dependency=*/true,
5636 					  &ambiguous_decls,
5637 					  id_expression.get_location ());
5638 	    /* If the lookup was ambiguous, an error will already have
5639 	       been issued.  */
5640 	    if (ambiguous_decls)
5641 	      return error_mark_node;
5642 
5643 	    /* In Objective-C++, we may have an Objective-C 2.0
5644 	       dot-syntax for classes here.  */
5645 	    if (c_dialect_objc ()
5646 		&& cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5647 		&& TREE_CODE (decl) == TYPE_DECL
5648 		&& objc_is_class_name (decl))
5649 	      {
5650 		tree component;
5651 		cp_lexer_consume_token (parser->lexer);
5652 		component = cp_parser_identifier (parser);
5653 		if (component == error_mark_node)
5654 		  return error_mark_node;
5655 
5656 		tree result = objc_build_class_component_ref (id_expression,
5657 							      component);
5658 		/* Build a location of the form:
5659 		     expr.component
5660 		     ~~~~~^~~~~~~~~
5661 		   with caret at the start of the component name (at
5662 		   input_location), ranging from the start of the id_expression
5663 		   to the end of the component name.  */
5664 		location_t combined_loc
5665 		  = make_location (input_location, id_expression.get_start (),
5666 				   get_finish (input_location));
5667 		protected_set_expr_location (result, combined_loc);
5668 		return result;
5669 	      }
5670 
5671 	    /* In Objective-C++, an instance variable (ivar) may be preferred
5672 	       to whatever cp_parser_lookup_name() found.
5673 	       Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5674 	       rest of c-family, we have to do a little extra work to preserve
5675 	       any location information in cp_expr "decl".  Given that
5676 	       objc_lookup_ivar is implemented in "c-family" and "objc", we
5677 	       have a trip through the pure "tree" type, rather than cp_expr.
5678 	       Naively copying it back to "decl" would implicitly give the
5679 	       new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5680 	       store an EXPR_LOCATION.  Hence we only update "decl" (and
5681 	       hence its location_t) if we get back a different tree node.  */
5682 	    tree decl_tree = objc_lookup_ivar (decl.get_value (),
5683 					       id_expression);
5684 	    if (decl_tree != decl.get_value ())
5685 	      decl = cp_expr (decl_tree);
5686 
5687 	    /* If name lookup gives us a SCOPE_REF, then the
5688 	       qualifying scope was dependent.  */
5689 	    if (TREE_CODE (decl) == SCOPE_REF)
5690 	      {
5691 		/* At this point, we do not know if DECL is a valid
5692 		   integral constant expression.  We assume that it is
5693 		   in fact such an expression, so that code like:
5694 
5695 		      template <int N> struct A {
5696 			int a[B<N>::i];
5697 		      };
5698 
5699 		   is accepted.  At template-instantiation time, we
5700 		   will check that B<N>::i is actually a constant.  */
5701 		return decl;
5702 	      }
5703 	    /* Check to see if DECL is a local variable in a context
5704 	       where that is forbidden.  */
5705 	    if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5706 		&& local_variable_p (decl))
5707 	      {
5708 		error_at (id_expression.get_location (),
5709 			  "local variable %qD may not appear in this context",
5710 			  decl.get_value ());
5711 		return error_mark_node;
5712 	      }
5713 	  }
5714 
5715 	decl = (finish_id_expression
5716 		(id_expression, decl, parser->scope,
5717 		 idk,
5718 		 parser->integral_constant_expression_p,
5719 		 parser->allow_non_integral_constant_expression_p,
5720 		 &parser->non_integral_constant_expression_p,
5721 		 template_p, done, address_p,
5722 		 template_arg_p,
5723 		 &error_msg,
5724 		 id_expression.get_location ()));
5725 	if (error_msg)
5726 	  cp_parser_error (parser, error_msg);
5727 	/* Build a location for an id-expression of the form:
5728 	     ::ns::id
5729              ~~~~~~^~
5730 	  or:
5731 	     id
5732 	     ^~
5733 	   i.e. from the start of the first token to the end of the final
5734 	   token, with the caret at the start of the unqualified-id.  */
5735 	location_t caret_loc = get_pure_location (id_expression.get_location ());
5736 	location_t start_loc = get_start (id_expr_token->location);
5737 	location_t finish_loc = get_finish (id_expression.get_location ());
5738 	location_t combined_loc
5739 	  = make_location (caret_loc, start_loc, finish_loc);
5740 
5741 	decl.set_location (combined_loc);
5742 	return decl;
5743       }
5744 
5745       /* Anything else is an error.  */
5746     default:
5747       cp_parser_error (parser, "expected primary-expression");
5748       return error_mark_node;
5749     }
5750 }
5751 
5752 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)5753 cp_parser_primary_expression (cp_parser *parser,
5754 			      bool address_p,
5755 			      bool cast_p,
5756 			      bool template_arg_p,
5757 			      cp_id_kind *idk)
5758 {
5759   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5760 				       /*decltype*/false, idk);
5761 }
5762 
5763 /* Parse an id-expression.
5764 
5765    id-expression:
5766      unqualified-id
5767      qualified-id
5768 
5769    qualified-id:
5770      :: [opt] nested-name-specifier template [opt] unqualified-id
5771      :: identifier
5772      :: operator-function-id
5773      :: template-id
5774 
5775    Return a representation of the unqualified portion of the
5776    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
5777    a `::' or nested-name-specifier.
5778 
5779    Often, if the id-expression was a qualified-id, the caller will
5780    want to make a SCOPE_REF to represent the qualified-id.  This
5781    function does not do this in order to avoid wastefully creating
5782    SCOPE_REFs when they are not required.
5783 
5784    If TEMPLATE_KEYWORD_P is true, then we have just seen the
5785    `template' keyword.
5786 
5787    If CHECK_DEPENDENCY_P is false, then names are looked up inside
5788    uninstantiated templates.
5789 
5790    If *TEMPLATE_P is non-NULL, it is set to true iff the
5791    `template' keyword is used to explicitly indicate that the entity
5792    named is a template.
5793 
5794    If DECLARATOR_P is true, the id-expression is appearing as part of
5795    a declarator, rather than as part of an expression.  */
5796 
5797 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)5798 cp_parser_id_expression (cp_parser *parser,
5799 			 bool template_keyword_p,
5800 			 bool check_dependency_p,
5801 			 bool *template_p,
5802 			 bool declarator_p,
5803 			 bool optional_p)
5804 {
5805   bool global_scope_p;
5806   bool nested_name_specifier_p;
5807 
5808   /* Assume the `template' keyword was not used.  */
5809   if (template_p)
5810     *template_p = template_keyword_p;
5811 
5812   /* Look for the optional `::' operator.  */
5813   global_scope_p
5814     = (!template_keyword_p
5815        && (cp_parser_global_scope_opt (parser,
5816 				       /*current_scope_valid_p=*/false)
5817 	   != NULL_TREE));
5818 
5819   /* Look for the optional nested-name-specifier.  */
5820   nested_name_specifier_p
5821     = (cp_parser_nested_name_specifier_opt (parser,
5822 					    /*typename_keyword_p=*/false,
5823 					    check_dependency_p,
5824 					    /*type_p=*/false,
5825 					    declarator_p,
5826 					    template_keyword_p)
5827        != NULL_TREE);
5828 
5829   /* If there is a nested-name-specifier, then we are looking at
5830      the first qualified-id production.  */
5831   if (nested_name_specifier_p)
5832     {
5833       tree saved_scope;
5834       tree saved_object_scope;
5835       tree saved_qualifying_scope;
5836       cp_expr unqualified_id;
5837       bool is_template;
5838 
5839       /* See if the next token is the `template' keyword.  */
5840       if (!template_p)
5841 	template_p = &is_template;
5842       *template_p = cp_parser_optional_template_keyword (parser);
5843       /* Name lookup we do during the processing of the
5844 	 unqualified-id might obliterate SCOPE.  */
5845       saved_scope = parser->scope;
5846       saved_object_scope = parser->object_scope;
5847       saved_qualifying_scope = parser->qualifying_scope;
5848       /* Process the final unqualified-id.  */
5849       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5850 						 check_dependency_p,
5851 						 declarator_p,
5852 						 /*optional_p=*/false);
5853       /* Restore the SAVED_SCOPE for our caller.  */
5854       parser->scope = saved_scope;
5855       parser->object_scope = saved_object_scope;
5856       parser->qualifying_scope = saved_qualifying_scope;
5857 
5858       return unqualified_id;
5859     }
5860   /* Otherwise, if we are in global scope, then we are looking at one
5861      of the other qualified-id productions.  */
5862   else if (global_scope_p)
5863     {
5864       cp_token *token;
5865       tree id;
5866 
5867       /* Peek at the next token.  */
5868       token = cp_lexer_peek_token (parser->lexer);
5869 
5870       /* If it's an identifier, and the next token is not a "<", then
5871 	 we can avoid the template-id case.  This is an optimization
5872 	 for this common case.  */
5873       if (token->type == CPP_NAME
5874 	  && !cp_parser_nth_token_starts_template_argument_list_p
5875 	       (parser, 2))
5876 	return cp_parser_identifier (parser);
5877 
5878       cp_parser_parse_tentatively (parser);
5879       /* Try a template-id.  */
5880       id = cp_parser_template_id (parser,
5881 				  /*template_keyword_p=*/false,
5882 				  /*check_dependency_p=*/true,
5883 				  none_type,
5884 				  declarator_p);
5885       /* If that worked, we're done.  */
5886       if (cp_parser_parse_definitely (parser))
5887 	return id;
5888 
5889       /* Peek at the next token.  (Changes in the token buffer may
5890 	 have invalidated the pointer obtained above.)  */
5891       token = cp_lexer_peek_token (parser->lexer);
5892 
5893       switch (token->type)
5894 	{
5895 	case CPP_NAME:
5896 	  return cp_parser_identifier (parser);
5897 
5898 	case CPP_KEYWORD:
5899 	  if (token->keyword == RID_OPERATOR)
5900 	    return cp_parser_operator_function_id (parser);
5901 	  /* Fall through.  */
5902 
5903 	default:
5904 	  cp_parser_error (parser, "expected id-expression");
5905 	  return error_mark_node;
5906 	}
5907     }
5908   else
5909     return cp_parser_unqualified_id (parser, template_keyword_p,
5910 				     /*check_dependency_p=*/true,
5911 				     declarator_p,
5912 				     optional_p);
5913 }
5914 
5915 /* Parse an unqualified-id.
5916 
5917    unqualified-id:
5918      identifier
5919      operator-function-id
5920      conversion-function-id
5921      ~ class-name
5922      template-id
5923 
5924    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5925    keyword, in a construct like `A::template ...'.
5926 
5927    Returns a representation of unqualified-id.  For the `identifier'
5928    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
5929    production a BIT_NOT_EXPR is returned; the operand of the
5930    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
5931    other productions, see the documentation accompanying the
5932    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
5933    names are looked up in uninstantiated templates.  If DECLARATOR_P
5934    is true, the unqualified-id is appearing as part of a declarator,
5935    rather than as part of an expression.  */
5936 
5937 static cp_expr
cp_parser_unqualified_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool declarator_p,bool optional_p)5938 cp_parser_unqualified_id (cp_parser* parser,
5939 			  bool template_keyword_p,
5940 			  bool check_dependency_p,
5941 			  bool declarator_p,
5942 			  bool optional_p)
5943 {
5944   cp_token *token;
5945 
5946   /* Peek at the next token.  */
5947   token = cp_lexer_peek_token (parser->lexer);
5948 
5949   switch ((int) token->type)
5950     {
5951     case CPP_NAME:
5952       {
5953 	tree id;
5954 
5955 	/* We don't know yet whether or not this will be a
5956 	   template-id.  */
5957 	cp_parser_parse_tentatively (parser);
5958 	/* Try a template-id.  */
5959 	id = cp_parser_template_id (parser, template_keyword_p,
5960 				    check_dependency_p,
5961 				    none_type,
5962 				    declarator_p);
5963 	/* If it worked, we're done.  */
5964 	if (cp_parser_parse_definitely (parser))
5965 	  return id;
5966 	/* Otherwise, it's an ordinary identifier.  */
5967 	return cp_parser_identifier (parser);
5968       }
5969 
5970     case CPP_TEMPLATE_ID:
5971       return cp_parser_template_id (parser, template_keyword_p,
5972 				    check_dependency_p,
5973 				    none_type,
5974 				    declarator_p);
5975 
5976     case CPP_COMPL:
5977       {
5978 	tree type_decl;
5979 	tree qualifying_scope;
5980 	tree object_scope;
5981 	tree scope;
5982 	bool done;
5983 	location_t tilde_loc = token->location;
5984 
5985 	/* Consume the `~' token.  */
5986 	cp_lexer_consume_token (parser->lexer);
5987 	/* Parse the class-name.  The standard, as written, seems to
5988 	   say that:
5989 
5990 	     template <typename T> struct S { ~S (); };
5991 	     template <typename T> S<T>::~S() {}
5992 
5993 	   is invalid, since `~' must be followed by a class-name, but
5994 	   `S<T>' is dependent, and so not known to be a class.
5995 	   That's not right; we need to look in uninstantiated
5996 	   templates.  A further complication arises from:
5997 
5998 	     template <typename T> void f(T t) {
5999 	       t.T::~T();
6000 	     }
6001 
6002 	   Here, it is not possible to look up `T' in the scope of `T'
6003 	   itself.  We must look in both the current scope, and the
6004 	   scope of the containing complete expression.
6005 
6006 	   Yet another issue is:
6007 
6008 	     struct S {
6009 	       int S;
6010 	       ~S();
6011 	     };
6012 
6013 	     S::~S() {}
6014 
6015 	   The standard does not seem to say that the `S' in `~S'
6016 	   should refer to the type `S' and not the data member
6017 	   `S::S'.  */
6018 
6019 	/* DR 244 says that we look up the name after the "~" in the
6020 	   same scope as we looked up the qualifying name.  That idea
6021 	   isn't fully worked out; it's more complicated than that.  */
6022 	scope = parser->scope;
6023 	object_scope = parser->object_scope;
6024 	qualifying_scope = parser->qualifying_scope;
6025 
6026 	/* Check for invalid scopes.  */
6027 	if (scope == error_mark_node)
6028 	  {
6029 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6030 	      cp_lexer_consume_token (parser->lexer);
6031 	    return error_mark_node;
6032 	  }
6033 	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6034 	  {
6035 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6036 	      error_at (token->location,
6037 			"scope %qT before %<~%> is not a class-name",
6038 			scope);
6039 	    cp_parser_simulate_error (parser);
6040 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6041 	      cp_lexer_consume_token (parser->lexer);
6042 	    return error_mark_node;
6043 	  }
6044 	gcc_assert (!scope || TYPE_P (scope));
6045 
6046 	token = cp_lexer_peek_token (parser->lexer);
6047 
6048 	/* Create a location with caret == start at the tilde,
6049 	   finishing at the end of the peeked token, e.g:
6050 	   ~token
6051 	   ^~~~~~.  */
6052 	location_t loc
6053 	  = make_location (tilde_loc, tilde_loc, token->location);
6054 
6055 	/* If the name is of the form "X::~X" it's OK even if X is a
6056 	   typedef.  */
6057 
6058 	if (scope
6059 	    && token->type == CPP_NAME
6060 	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6061 		!= CPP_LESS)
6062 	    && (token->u.value == TYPE_IDENTIFIER (scope)
6063 		|| (CLASS_TYPE_P (scope)
6064 		    && constructor_name_p (token->u.value, scope))))
6065 	  {
6066 	    cp_lexer_consume_token (parser->lexer);
6067 	    return cp_expr (build_nt (BIT_NOT_EXPR, scope), loc);
6068 	  }
6069 
6070 	/* ~auto means the destructor of whatever the object is.  */
6071 	if (cp_parser_is_keyword (token, RID_AUTO))
6072 	  {
6073 	    if (cxx_dialect < cxx14)
6074 	      pedwarn (loc, 0,
6075 		       "%<~auto%> only available with "
6076 		       "%<-std=c++14%> or %<-std=gnu++14%>");
6077 	    cp_lexer_consume_token (parser->lexer);
6078 	    return cp_expr (build_nt (BIT_NOT_EXPR, make_auto (), loc));
6079 	  }
6080 
6081 	/* If there was an explicit qualification (S::~T), first look
6082 	   in the scope given by the qualification (i.e., S).
6083 
6084 	   Note: in the calls to cp_parser_class_name below we pass
6085 	   typename_type so that lookup finds the injected-class-name
6086 	   rather than the constructor.  */
6087 	done = false;
6088 	type_decl = NULL_TREE;
6089 	if (scope)
6090 	  {
6091 	    cp_parser_parse_tentatively (parser);
6092 	    type_decl = cp_parser_class_name (parser,
6093 					      /*typename_keyword_p=*/false,
6094 					      /*template_keyword_p=*/false,
6095 					      typename_type,
6096 					      /*check_dependency=*/false,
6097 					      /*class_head_p=*/false,
6098 					      declarator_p);
6099 	    if (cp_parser_parse_definitely (parser))
6100 	      done = true;
6101 	  }
6102 	/* In "N::S::~S", look in "N" as well.  */
6103 	if (!done && scope && qualifying_scope)
6104 	  {
6105 	    cp_parser_parse_tentatively (parser);
6106 	    parser->scope = qualifying_scope;
6107 	    parser->object_scope = NULL_TREE;
6108 	    parser->qualifying_scope = NULL_TREE;
6109 	    type_decl
6110 	      = cp_parser_class_name (parser,
6111 				      /*typename_keyword_p=*/false,
6112 				      /*template_keyword_p=*/false,
6113 				      typename_type,
6114 				      /*check_dependency=*/false,
6115 				      /*class_head_p=*/false,
6116 				      declarator_p);
6117 	    if (cp_parser_parse_definitely (parser))
6118 	      done = true;
6119 	  }
6120 	/* In "p->S::~T", look in the scope given by "*p" as well.  */
6121 	else if (!done && object_scope)
6122 	  {
6123 	    cp_parser_parse_tentatively (parser);
6124 	    parser->scope = object_scope;
6125 	    parser->object_scope = NULL_TREE;
6126 	    parser->qualifying_scope = NULL_TREE;
6127 	    type_decl
6128 	      = cp_parser_class_name (parser,
6129 				      /*typename_keyword_p=*/false,
6130 				      /*template_keyword_p=*/false,
6131 				      typename_type,
6132 				      /*check_dependency=*/false,
6133 				      /*class_head_p=*/false,
6134 				      declarator_p);
6135 	    if (cp_parser_parse_definitely (parser))
6136 	      done = true;
6137 	  }
6138 	/* Look in the surrounding context.  */
6139 	if (!done)
6140 	  {
6141 	    parser->scope = NULL_TREE;
6142 	    parser->object_scope = NULL_TREE;
6143 	    parser->qualifying_scope = NULL_TREE;
6144 	    if (processing_template_decl)
6145 	      cp_parser_parse_tentatively (parser);
6146 	    type_decl
6147 	      = cp_parser_class_name (parser,
6148 				      /*typename_keyword_p=*/false,
6149 				      /*template_keyword_p=*/false,
6150 				      typename_type,
6151 				      /*check_dependency=*/false,
6152 				      /*class_head_p=*/false,
6153 				      declarator_p);
6154 	    if (processing_template_decl
6155 		&& ! cp_parser_parse_definitely (parser))
6156 	      {
6157 		/* We couldn't find a type with this name.  If we're parsing
6158 		   tentatively, fail and try something else.  */
6159 		if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6160 		  {
6161 		    cp_parser_simulate_error (parser);
6162 		    return error_mark_node;
6163 		  }
6164 		/* Otherwise, accept it and check for a match at instantiation
6165 		   time.  */
6166 		type_decl = cp_parser_identifier (parser);
6167 		if (type_decl != error_mark_node)
6168 		  type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6169 		return cp_expr (type_decl, loc);
6170 	      }
6171 	  }
6172 	/* If an error occurred, assume that the name of the
6173 	   destructor is the same as the name of the qualifying
6174 	   class.  That allows us to keep parsing after running
6175 	   into ill-formed destructor names.  */
6176 	if (type_decl == error_mark_node && scope)
6177 	  return build_nt (BIT_NOT_EXPR, scope);
6178 	else if (type_decl == error_mark_node)
6179 	  return error_mark_node;
6180 
6181 	/* Check that destructor name and scope match.  */
6182 	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6183 	  {
6184 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6185 	      error_at (loc,
6186 			"declaration of %<~%T%> as member of %qT",
6187 			type_decl, scope);
6188 	    cp_parser_simulate_error (parser);
6189 	    return error_mark_node;
6190 	  }
6191 
6192 	/* [class.dtor]
6193 
6194 	   A typedef-name that names a class shall not be used as the
6195 	   identifier in the declarator for a destructor declaration.  */
6196 	if (declarator_p
6197 	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6198 	    && !DECL_SELF_REFERENCE_P (type_decl)
6199 	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6200 	  error_at (loc,
6201 		    "typedef-name %qD used as destructor declarator",
6202 		    type_decl);
6203 
6204 	return cp_expr (build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl), loc));
6205       }
6206 
6207     case CPP_KEYWORD:
6208       if (token->keyword == RID_OPERATOR)
6209 	{
6210 	  cp_expr id;
6211 
6212 	  /* This could be a template-id, so we try that first.  */
6213 	  cp_parser_parse_tentatively (parser);
6214 	  /* Try a template-id.  */
6215 	  id = cp_parser_template_id (parser, template_keyword_p,
6216 				      /*check_dependency_p=*/true,
6217 				      none_type,
6218 				      declarator_p);
6219 	  /* If that worked, we're done.  */
6220 	  if (cp_parser_parse_definitely (parser))
6221 	    return id;
6222 	  /* We still don't know whether we're looking at an
6223 	     operator-function-id or a conversion-function-id.  */
6224 	  cp_parser_parse_tentatively (parser);
6225 	  /* Try an operator-function-id.  */
6226 	  id = cp_parser_operator_function_id (parser);
6227 	  /* If that didn't work, try a conversion-function-id.  */
6228 	  if (!cp_parser_parse_definitely (parser))
6229 	    id = cp_parser_conversion_function_id (parser);
6230 
6231 	  return id;
6232 	}
6233       /* Fall through.  */
6234 
6235     default:
6236       if (optional_p)
6237 	return NULL_TREE;
6238       cp_parser_error (parser, "expected unqualified-id");
6239       return error_mark_node;
6240     }
6241 }
6242 
6243 /* Parse an (optional) nested-name-specifier.
6244 
6245    nested-name-specifier: [C++98]
6246      class-or-namespace-name :: nested-name-specifier [opt]
6247      class-or-namespace-name :: template nested-name-specifier [opt]
6248 
6249    nested-name-specifier: [C++0x]
6250      type-name ::
6251      namespace-name ::
6252      nested-name-specifier identifier ::
6253      nested-name-specifier template [opt] simple-template-id ::
6254 
6255    PARSER->SCOPE should be set appropriately before this function is
6256    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6257    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
6258    in name lookups.
6259 
6260    Sets PARSER->SCOPE to the class (TYPE) or namespace
6261    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6262    it unchanged if there is no nested-name-specifier.  Returns the new
6263    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6264 
6265    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6266    part of a declaration and/or decl-specifier.  */
6267 
6268 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)6269 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6270 				     bool typename_keyword_p,
6271 				     bool check_dependency_p,
6272 				     bool type_p,
6273 				     bool is_declaration,
6274 				     bool template_keyword_p /* = false */)
6275 {
6276   bool success = false;
6277   cp_token_position start = 0;
6278   cp_token *token;
6279 
6280   /* Remember where the nested-name-specifier starts.  */
6281   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6282     {
6283       start = cp_lexer_token_position (parser->lexer, false);
6284       push_deferring_access_checks (dk_deferred);
6285     }
6286 
6287   while (true)
6288     {
6289       tree new_scope;
6290       tree old_scope;
6291       tree saved_qualifying_scope;
6292 
6293       /* Spot cases that cannot be the beginning of a
6294 	 nested-name-specifier.  */
6295       token = cp_lexer_peek_token (parser->lexer);
6296 
6297       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6298 	 the already parsed nested-name-specifier.  */
6299       if (token->type == CPP_NESTED_NAME_SPECIFIER)
6300 	{
6301 	  /* Grab the nested-name-specifier and continue the loop.  */
6302 	  cp_parser_pre_parsed_nested_name_specifier (parser);
6303 	  /* If we originally encountered this nested-name-specifier
6304 	     with IS_DECLARATION set to false, we will not have
6305 	     resolved TYPENAME_TYPEs, so we must do so here.  */
6306 	  if (is_declaration
6307 	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6308 	    {
6309 	      new_scope = resolve_typename_type (parser->scope,
6310 						 /*only_current_p=*/false);
6311 	      if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6312 		parser->scope = new_scope;
6313 	    }
6314 	  success = true;
6315 	  continue;
6316 	}
6317 
6318       /* Spot cases that cannot be the beginning of a
6319 	 nested-name-specifier.  On the second and subsequent times
6320 	 through the loop, we look for the `template' keyword.  */
6321       if (success && token->keyword == RID_TEMPLATE)
6322 	;
6323       /* A template-id can start a nested-name-specifier.  */
6324       else if (token->type == CPP_TEMPLATE_ID)
6325 	;
6326       /* DR 743: decltype can be used in a nested-name-specifier.  */
6327       else if (token_is_decltype (token))
6328 	;
6329       else
6330 	{
6331 	  /* If the next token is not an identifier, then it is
6332 	     definitely not a type-name or namespace-name.  */
6333 	  if (token->type != CPP_NAME)
6334 	    break;
6335 	  /* If the following token is neither a `<' (to begin a
6336 	     template-id), nor a `::', then we are not looking at a
6337 	     nested-name-specifier.  */
6338 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
6339 
6340 	  if (token->type == CPP_COLON
6341 	      && parser->colon_corrects_to_scope_p
6342 	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6343 	    {
6344 	      gcc_rich_location richloc (token->location);
6345 	      richloc.add_fixit_replace ("::");
6346 	      error_at (&richloc,
6347 			"found %<:%> in nested-name-specifier, "
6348 			"expected %<::%>");
6349 	      token->type = CPP_SCOPE;
6350 	    }
6351 
6352 	  if (token->type != CPP_SCOPE
6353 	      && !cp_parser_nth_token_starts_template_argument_list_p
6354 		  (parser, 2))
6355 	    break;
6356 	}
6357 
6358       /* The nested-name-specifier is optional, so we parse
6359 	 tentatively.  */
6360       cp_parser_parse_tentatively (parser);
6361 
6362       /* Look for the optional `template' keyword, if this isn't the
6363 	 first time through the loop.  */
6364       if (success)
6365 	template_keyword_p = cp_parser_optional_template_keyword (parser);
6366 
6367       /* Save the old scope since the name lookup we are about to do
6368 	 might destroy it.  */
6369       old_scope = parser->scope;
6370       saved_qualifying_scope = parser->qualifying_scope;
6371       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6372 	 look up names in "X<T>::I" in order to determine that "Y" is
6373 	 a template.  So, if we have a typename at this point, we make
6374 	 an effort to look through it.  */
6375       if (is_declaration
6376 	  && !typename_keyword_p
6377 	  && parser->scope
6378 	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6379 	parser->scope = resolve_typename_type (parser->scope,
6380 					       /*only_current_p=*/false);
6381       /* Parse the qualifying entity.  */
6382       new_scope
6383 	= cp_parser_qualifying_entity (parser,
6384                                        typename_keyword_p,
6385                                        template_keyword_p,
6386                                        check_dependency_p,
6387                                        type_p,
6388                                        is_declaration);
6389       /* Look for the `::' token.  */
6390       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6391 
6392       /* If we found what we wanted, we keep going; otherwise, we're
6393 	 done.  */
6394       if (!cp_parser_parse_definitely (parser))
6395 	{
6396 	  bool error_p = false;
6397 
6398 	  /* Restore the OLD_SCOPE since it was valid before the
6399 	     failed attempt at finding the last
6400 	     class-or-namespace-name.  */
6401 	  parser->scope = old_scope;
6402 	  parser->qualifying_scope = saved_qualifying_scope;
6403 
6404 	  /* If the next token is a decltype, and the one after that is a
6405 	     `::', then the decltype has failed to resolve to a class or
6406 	     enumeration type.  Give this error even when parsing
6407 	     tentatively since it can't possibly be valid--and we're going
6408 	     to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6409 	     won't get another chance.*/
6410 	  if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6411 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6412 		  == CPP_SCOPE))
6413 	    {
6414 	      token = cp_lexer_consume_token (parser->lexer);
6415 	      error_at (token->location, "decltype evaluates to %qT, "
6416 			"which is not a class or enumeration type",
6417 			token->u.tree_check_value->value);
6418 	      parser->scope = error_mark_node;
6419 	      error_p = true;
6420 	      /* As below.  */
6421 	      success = true;
6422 	      cp_lexer_consume_token (parser->lexer);
6423 	    }
6424 
6425 	  if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6426 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6427 	    {
6428 	      /* If we have a non-type template-id followed by ::, it can't
6429 		 possibly be valid.  */
6430 	      token = cp_lexer_peek_token (parser->lexer);
6431 	      tree tid = token->u.tree_check_value->value;
6432 	      if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6433 		  && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6434 		{
6435 		  tree tmpl = NULL_TREE;
6436 		  if (is_overloaded_fn (tid))
6437 		    {
6438 		      tree fns = get_fns (tid);
6439 		      if (OVL_SINGLE_P (fns))
6440 			tmpl = OVL_FIRST (fns);
6441 		      error_at (token->location, "function template-id %qD "
6442 				"in nested-name-specifier", tid);
6443 		    }
6444 		  else
6445 		    {
6446 		      /* Variable template.  */
6447 		      tmpl = TREE_OPERAND (tid, 0);
6448 		      gcc_assert (variable_template_p (tmpl));
6449 		      error_at (token->location, "variable template-id %qD "
6450 				"in nested-name-specifier", tid);
6451 		    }
6452 		  if (tmpl)
6453 		    inform (DECL_SOURCE_LOCATION (tmpl),
6454 			    "%qD declared here", tmpl);
6455 
6456 		  parser->scope = error_mark_node;
6457 		  error_p = true;
6458 		  /* As below.  */
6459 		  success = true;
6460 		  cp_lexer_consume_token (parser->lexer);
6461 		  cp_lexer_consume_token (parser->lexer);
6462 		}
6463 	    }
6464 
6465 	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6466 	    break;
6467 	  /* If the next token is an identifier, and the one after
6468 	     that is a `::', then any valid interpretation would have
6469 	     found a class-or-namespace-name.  */
6470 	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6471 		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6472 		     == CPP_SCOPE)
6473 		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6474 		     != CPP_COMPL))
6475 	    {
6476 	      token = cp_lexer_consume_token (parser->lexer);
6477 	      if (!error_p)
6478 		{
6479 		  if (!token->error_reported)
6480 		    {
6481 		      tree decl;
6482 		      tree ambiguous_decls;
6483 
6484 		      decl = cp_parser_lookup_name (parser, token->u.value,
6485 						    none_type,
6486 						    /*is_template=*/false,
6487 						    /*is_namespace=*/false,
6488 						    /*check_dependency=*/true,
6489 						    &ambiguous_decls,
6490 						    token->location);
6491 		      if (TREE_CODE (decl) == TEMPLATE_DECL)
6492 			error_at (token->location,
6493 				  "%qD used without template arguments",
6494 				  decl);
6495 		      else if (ambiguous_decls)
6496 			{
6497 			  // cp_parser_lookup_name has the same diagnostic,
6498 			  // thus make sure to emit it at most once.
6499 			  if (cp_parser_uncommitted_to_tentative_parse_p
6500 			      (parser))
6501 			    {
6502 			      error_at (token->location,
6503 					"reference to %qD is ambiguous",
6504 					token->u.value);
6505 			      print_candidates (ambiguous_decls);
6506 			    }
6507 			  decl = error_mark_node;
6508 			}
6509 		      else
6510                         {
6511                           if (cxx_dialect != cxx98)
6512                             cp_parser_name_lookup_error
6513                             (parser, token->u.value, decl, NLE_NOT_CXX98,
6514 	  		     token->location);
6515 			  else
6516 			    cp_parser_name_lookup_error
6517 			    (parser, token->u.value, decl, NLE_CXX98,
6518 			     token->location);
6519                         }
6520 		    }
6521 		  parser->scope = error_mark_node;
6522 		  error_p = true;
6523 		  /* Treat this as a successful nested-name-specifier
6524 		     due to:
6525 
6526 		     [basic.lookup.qual]
6527 
6528 		     If the name found is not a class-name (clause
6529 		     _class_) or namespace-name (_namespace.def_), the
6530 		     program is ill-formed.  */
6531 		  success = true;
6532 		}
6533 	      cp_lexer_consume_token (parser->lexer);
6534 	    }
6535 	  break;
6536 	}
6537       /* We've found one valid nested-name-specifier.  */
6538       success = true;
6539       /* Name lookup always gives us a DECL.  */
6540       if (TREE_CODE (new_scope) == TYPE_DECL)
6541 	new_scope = TREE_TYPE (new_scope);
6542       /* Uses of "template" must be followed by actual templates.  */
6543       if (template_keyword_p
6544 	  && !(CLASS_TYPE_P (new_scope)
6545 	       && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6546 		    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6547 		   || CLASSTYPE_IS_TEMPLATE (new_scope)))
6548 	  && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6549 	       && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6550 		   == TEMPLATE_ID_EXPR)))
6551 	permerror (input_location, TYPE_P (new_scope)
6552 		   ? G_("%qT is not a template")
6553 		   : G_("%qD is not a template"),
6554 		   new_scope);
6555       /* If it is a class scope, try to complete it; we are about to
6556 	 be looking up names inside the class.  */
6557       if (TYPE_P (new_scope)
6558 	  /* Since checking types for dependency can be expensive,
6559 	     avoid doing it if the type is already complete.  */
6560 	  && !COMPLETE_TYPE_P (new_scope)
6561 	  /* Do not try to complete dependent types.  */
6562 	  && !dependent_type_p (new_scope))
6563 	{
6564 	  new_scope = complete_type (new_scope);
6565 	  /* If it is a typedef to current class, use the current
6566 	     class instead, as the typedef won't have any names inside
6567 	     it yet.  */
6568 	  if (!COMPLETE_TYPE_P (new_scope)
6569 	      && currently_open_class (new_scope))
6570 	    new_scope = TYPE_MAIN_VARIANT (new_scope);
6571 	}
6572       /* Make sure we look in the right scope the next time through
6573 	 the loop.  */
6574       parser->scope = new_scope;
6575     }
6576 
6577   /* If parsing tentatively, replace the sequence of tokens that makes
6578      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6579      token.  That way, should we re-parse the token stream, we will
6580      not have to repeat the effort required to do the parse, nor will
6581      we issue duplicate error messages.  */
6582   if (success && start)
6583     {
6584       cp_token *token;
6585 
6586       token = cp_lexer_token_at (parser->lexer, start);
6587       /* Reset the contents of the START token.  */
6588       token->type = CPP_NESTED_NAME_SPECIFIER;
6589       /* Retrieve any deferred checks.  Do not pop this access checks yet
6590 	 so the memory will not be reclaimed during token replacing below.  */
6591       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6592       token->u.tree_check_value->value = parser->scope;
6593       token->u.tree_check_value->checks = get_deferred_access_checks ();
6594       token->u.tree_check_value->qualifying_scope =
6595 	parser->qualifying_scope;
6596       token->keyword = RID_MAX;
6597 
6598       /* Purge all subsequent tokens.  */
6599       cp_lexer_purge_tokens_after (parser->lexer, start);
6600     }
6601 
6602   if (start)
6603     pop_to_parent_deferring_access_checks ();
6604 
6605   return success ? parser->scope : NULL_TREE;
6606 }
6607 
6608 /* Parse a nested-name-specifier.  See
6609    cp_parser_nested_name_specifier_opt for details.  This function
6610    behaves identically, except that it will an issue an error if no
6611    nested-name-specifier is present.  */
6612 
6613 static tree
cp_parser_nested_name_specifier(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)6614 cp_parser_nested_name_specifier (cp_parser *parser,
6615 				 bool typename_keyword_p,
6616 				 bool check_dependency_p,
6617 				 bool type_p,
6618 				 bool is_declaration)
6619 {
6620   tree scope;
6621 
6622   /* Look for the nested-name-specifier.  */
6623   scope = cp_parser_nested_name_specifier_opt (parser,
6624 					       typename_keyword_p,
6625 					       check_dependency_p,
6626 					       type_p,
6627 					       is_declaration);
6628   /* If it was not present, issue an error message.  */
6629   if (!scope)
6630     {
6631       cp_parser_error (parser, "expected nested-name-specifier");
6632       parser->scope = NULL_TREE;
6633     }
6634 
6635   return scope;
6636 }
6637 
6638 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6639    this is either a class-name or a namespace-name (which corresponds
6640    to the class-or-namespace-name production in the grammar). For
6641    C++0x, it can also be a type-name that refers to an enumeration
6642    type or a simple-template-id.
6643 
6644    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6645    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6646    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6647    TYPE_P is TRUE iff the next name should be taken as a class-name,
6648    even the same name is declared to be another entity in the same
6649    scope.
6650 
6651    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6652    specified by the class-or-namespace-name.  If neither is found the
6653    ERROR_MARK_NODE is returned.  */
6654 
6655 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)6656 cp_parser_qualifying_entity (cp_parser *parser,
6657 			     bool typename_keyword_p,
6658 			     bool template_keyword_p,
6659 			     bool check_dependency_p,
6660 			     bool type_p,
6661 			     bool is_declaration)
6662 {
6663   tree saved_scope;
6664   tree saved_qualifying_scope;
6665   tree saved_object_scope;
6666   tree scope;
6667   bool only_class_p;
6668   bool successful_parse_p;
6669 
6670   /* DR 743: decltype can appear in a nested-name-specifier.  */
6671   if (cp_lexer_next_token_is_decltype (parser->lexer))
6672     {
6673       scope = cp_parser_decltype (parser);
6674       if (TREE_CODE (scope) != ENUMERAL_TYPE
6675 	  && !MAYBE_CLASS_TYPE_P (scope))
6676 	{
6677 	  cp_parser_simulate_error (parser);
6678 	  return error_mark_node;
6679 	}
6680       if (TYPE_NAME (scope))
6681 	scope = TYPE_NAME (scope);
6682       return scope;
6683     }
6684 
6685   /* Before we try to parse the class-name, we must save away the
6686      current PARSER->SCOPE since cp_parser_class_name will destroy
6687      it.  */
6688   saved_scope = parser->scope;
6689   saved_qualifying_scope = parser->qualifying_scope;
6690   saved_object_scope = parser->object_scope;
6691   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
6692      there is no need to look for a namespace-name.  */
6693   only_class_p = template_keyword_p
6694     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6695   if (!only_class_p)
6696     cp_parser_parse_tentatively (parser);
6697   scope = cp_parser_class_name (parser,
6698 				typename_keyword_p,
6699 				template_keyword_p,
6700 				type_p ? class_type : none_type,
6701 				check_dependency_p,
6702 				/*class_head_p=*/false,
6703 				is_declaration,
6704 				/*enum_ok=*/cxx_dialect > cxx98);
6705   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6706   /* If that didn't work, try for a namespace-name.  */
6707   if (!only_class_p && !successful_parse_p)
6708     {
6709       /* Restore the saved scope.  */
6710       parser->scope = saved_scope;
6711       parser->qualifying_scope = saved_qualifying_scope;
6712       parser->object_scope = saved_object_scope;
6713       /* If we are not looking at an identifier followed by the scope
6714 	 resolution operator, then this is not part of a
6715 	 nested-name-specifier.  (Note that this function is only used
6716 	 to parse the components of a nested-name-specifier.)  */
6717       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6718 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6719 	return error_mark_node;
6720       scope = cp_parser_namespace_name (parser);
6721     }
6722 
6723   return scope;
6724 }
6725 
6726 /* Return true if we are looking at a compound-literal, false otherwise.  */
6727 
6728 static bool
cp_parser_compound_literal_p(cp_parser * parser)6729 cp_parser_compound_literal_p (cp_parser *parser)
6730 {
6731   cp_lexer_save_tokens (parser->lexer);
6732 
6733   /* Skip tokens until the next token is a closing parenthesis.
6734      If we find the closing `)', and the next token is a `{', then
6735      we are looking at a compound-literal.  */
6736   bool compound_literal_p
6737     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6738 					      /*consume_paren=*/true)
6739        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6740 
6741   /* Roll back the tokens we skipped.  */
6742   cp_lexer_rollback_tokens (parser->lexer);
6743 
6744   return compound_literal_p;
6745 }
6746 
6747 /* Return true if EXPR is the integer constant zero or a complex constant
6748    of zero, without any folding, but ignoring location wrappers.  */
6749 
6750 bool
literal_integer_zerop(const_tree expr)6751 literal_integer_zerop (const_tree expr)
6752 {
6753   return (location_wrapper_p (expr)
6754 	  && integer_zerop (TREE_OPERAND (expr, 0)));
6755 }
6756 
6757 /* Parse a postfix-expression.
6758 
6759    postfix-expression:
6760      primary-expression
6761      postfix-expression [ expression ]
6762      postfix-expression ( expression-list [opt] )
6763      simple-type-specifier ( expression-list [opt] )
6764      typename :: [opt] nested-name-specifier identifier
6765        ( expression-list [opt] )
6766      typename :: [opt] nested-name-specifier template [opt] template-id
6767        ( expression-list [opt] )
6768      postfix-expression . template [opt] id-expression
6769      postfix-expression -> template [opt] id-expression
6770      postfix-expression . pseudo-destructor-name
6771      postfix-expression -> pseudo-destructor-name
6772      postfix-expression ++
6773      postfix-expression --
6774      dynamic_cast < type-id > ( expression )
6775      static_cast < type-id > ( expression )
6776      reinterpret_cast < type-id > ( expression )
6777      const_cast < type-id > ( expression )
6778      typeid ( expression )
6779      typeid ( type-id )
6780 
6781    GNU Extension:
6782 
6783    postfix-expression:
6784      ( type-id ) { initializer-list , [opt] }
6785 
6786    This extension is a GNU version of the C99 compound-literal
6787    construct.  (The C99 grammar uses `type-name' instead of `type-id',
6788    but they are essentially the same concept.)
6789 
6790    If ADDRESS_P is true, the postfix expression is the operand of the
6791    `&' operator.  CAST_P is true if this expression is the target of a
6792    cast.
6793 
6794    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6795    class member access expressions [expr.ref].
6796 
6797    Returns a representation of the expression.  */
6798 
6799 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)6800 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6801                               bool member_access_only_p, bool decltype_p,
6802 			      cp_id_kind * pidk_return)
6803 {
6804   cp_token *token;
6805   location_t loc;
6806   enum rid keyword;
6807   cp_id_kind idk = CP_ID_KIND_NONE;
6808   cp_expr postfix_expression = NULL_TREE;
6809   bool is_member_access = false;
6810 
6811   /* Peek at the next token.  */
6812   token = cp_lexer_peek_token (parser->lexer);
6813   loc = token->location;
6814   location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6815 
6816   /* Some of the productions are determined by keywords.  */
6817   keyword = token->keyword;
6818   switch (keyword)
6819     {
6820     case RID_DYNCAST:
6821     case RID_STATCAST:
6822     case RID_REINTCAST:
6823     case RID_CONSTCAST:
6824       {
6825 	tree type;
6826 	cp_expr expression;
6827 	const char *saved_message;
6828 	bool saved_in_type_id_in_expr_p;
6829 
6830 	/* All of these can be handled in the same way from the point
6831 	   of view of parsing.  Begin by consuming the token
6832 	   identifying the cast.  */
6833 	cp_lexer_consume_token (parser->lexer);
6834 
6835 	/* New types cannot be defined in the cast.  */
6836 	saved_message = parser->type_definition_forbidden_message;
6837 	parser->type_definition_forbidden_message
6838 	  = G_("types may not be defined in casts");
6839 
6840 	/* Look for the opening `<'.  */
6841 	cp_parser_require (parser, CPP_LESS, RT_LESS);
6842 	/* Parse the type to which we are casting.  */
6843 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6844 	parser->in_type_id_in_expr_p = true;
6845 	type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6846 				  NULL);
6847 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6848 	/* Look for the closing `>'.  */
6849 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6850 	/* Restore the old message.  */
6851 	parser->type_definition_forbidden_message = saved_message;
6852 
6853 	bool saved_greater_than_is_operator_p
6854 	  = parser->greater_than_is_operator_p;
6855 	parser->greater_than_is_operator_p = true;
6856 
6857 	/* And the expression which is being cast.  */
6858 	matching_parens parens;
6859 	parens.require_open (parser);
6860 	expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6861 	cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6862 						   RT_CLOSE_PAREN);
6863 	location_t end_loc = close_paren ?
6864 	  close_paren->location : UNKNOWN_LOCATION;
6865 
6866 	parser->greater_than_is_operator_p
6867 	  = saved_greater_than_is_operator_p;
6868 
6869 	/* Only type conversions to integral or enumeration types
6870 	   can be used in constant-expressions.  */
6871 	if (!cast_valid_in_integral_constant_expression_p (type)
6872 	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6873 	  {
6874 	    postfix_expression = error_mark_node;
6875 	    break;
6876 	  }
6877 
6878 	switch (keyword)
6879 	  {
6880 	  case RID_DYNCAST:
6881 	    postfix_expression
6882 	      = build_dynamic_cast (type, expression, tf_warning_or_error);
6883 	    break;
6884 	  case RID_STATCAST:
6885 	    postfix_expression
6886 	      = build_static_cast (type, expression, tf_warning_or_error);
6887 	    break;
6888 	  case RID_REINTCAST:
6889 	    postfix_expression
6890 	      = build_reinterpret_cast (type, expression,
6891                                         tf_warning_or_error);
6892 	    break;
6893 	  case RID_CONSTCAST:
6894 	    postfix_expression
6895 	      = build_const_cast (type, expression, tf_warning_or_error);
6896 	    break;
6897 	  default:
6898 	    gcc_unreachable ();
6899 	  }
6900 
6901 	/* Construct a location e.g. :
6902 	     reinterpret_cast <int *> (expr)
6903 	     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6904 	   ranging from the start of the "*_cast" token to the final closing
6905 	   paren, with the caret at the start.  */
6906 	location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6907 	postfix_expression.set_location (cp_cast_loc);
6908       }
6909       break;
6910 
6911     case RID_TYPEID:
6912       {
6913 	tree type;
6914 	const char *saved_message;
6915 	bool saved_in_type_id_in_expr_p;
6916 
6917 	/* Consume the `typeid' token.  */
6918 	cp_lexer_consume_token (parser->lexer);
6919 	/* Look for the `(' token.  */
6920 	matching_parens parens;
6921 	parens.require_open (parser);
6922 	/* Types cannot be defined in a `typeid' expression.  */
6923 	saved_message = parser->type_definition_forbidden_message;
6924 	parser->type_definition_forbidden_message
6925 	  = G_("types may not be defined in a %<typeid%> expression");
6926 	/* We can't be sure yet whether we're looking at a type-id or an
6927 	   expression.  */
6928 	cp_parser_parse_tentatively (parser);
6929 	/* Try a type-id first.  */
6930 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6931 	parser->in_type_id_in_expr_p = true;
6932 	type = cp_parser_type_id (parser);
6933 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6934 	/* Look for the `)' token.  Otherwise, we can't be sure that
6935 	   we're not looking at an expression: consider `typeid (int
6936 	   (3))', for example.  */
6937 	cp_token *close_paren = parens.require_close (parser);
6938 	/* If all went well, simply lookup the type-id.  */
6939 	if (cp_parser_parse_definitely (parser))
6940 	  postfix_expression = get_typeid (type, tf_warning_or_error);
6941 	/* Otherwise, fall back to the expression variant.  */
6942 	else
6943 	  {
6944 	    tree expression;
6945 
6946 	    /* Look for an expression.  */
6947 	    expression = cp_parser_expression (parser, & idk);
6948 	    /* Compute its typeid.  */
6949 	    postfix_expression = build_typeid (expression, tf_warning_or_error);
6950 	    /* Look for the `)' token.  */
6951 	    close_paren = parens.require_close (parser);
6952 	  }
6953 	/* Restore the saved message.  */
6954 	parser->type_definition_forbidden_message = saved_message;
6955 	/* `typeid' may not appear in an integral constant expression.  */
6956 	if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6957 	  postfix_expression = error_mark_node;
6958 
6959 	/* Construct a location e.g. :
6960 	     typeid (expr)
6961 	     ^~~~~~~~~~~~~
6962 	   ranging from the start of the "typeid" token to the final closing
6963 	   paren, with the caret at the start.  */
6964 	if (close_paren)
6965 	  {
6966 	    location_t typeid_loc
6967 	      = make_location (start_loc, start_loc, close_paren->location);
6968 	    postfix_expression.set_location (typeid_loc);
6969 	    postfix_expression.maybe_add_location_wrapper ();
6970 	  }
6971       }
6972       break;
6973 
6974     case RID_TYPENAME:
6975       {
6976 	tree type;
6977 	/* The syntax permitted here is the same permitted for an
6978 	   elaborated-type-specifier.  */
6979         ++parser->prevent_constrained_type_specifiers;
6980 	type = cp_parser_elaborated_type_specifier (parser,
6981 						    /*is_friend=*/false,
6982 						    /*is_declaration=*/false);
6983         --parser->prevent_constrained_type_specifiers;
6984 	postfix_expression = cp_parser_functional_cast (parser, type);
6985       }
6986       break;
6987 
6988     case RID_ADDRESSOF:
6989     case RID_BUILTIN_SHUFFLE:
6990     case RID_BUILTIN_LAUNDER:
6991       {
6992 	vec<tree, va_gc> *vec;
6993 	unsigned int i;
6994 	tree p;
6995 
6996 	cp_lexer_consume_token (parser->lexer);
6997 	vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6998 		    /*cast_p=*/false, /*allow_expansion_p=*/true,
6999 		    /*non_constant_p=*/NULL);
7000 	if (vec == NULL)
7001 	  {
7002 	    postfix_expression = error_mark_node;
7003 	    break;
7004 	  }
7005 
7006 	FOR_EACH_VEC_ELT (*vec, i, p)
7007 	  mark_exp_read (p);
7008 
7009 	switch (keyword)
7010 	  {
7011 	  case RID_ADDRESSOF:
7012 	    if (vec->length () == 1)
7013 	      postfix_expression
7014 		= cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7015 	    else
7016 	      {
7017 		error_at (loc, "wrong number of arguments to "
7018 			       "%<__builtin_addressof%>");
7019 		postfix_expression = error_mark_node;
7020 	      }
7021 	    break;
7022 
7023 	  case RID_BUILTIN_LAUNDER:
7024 	    if (vec->length () == 1)
7025 	      postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7026 							   tf_warning_or_error);
7027 	    else
7028 	      {
7029 		error_at (loc, "wrong number of arguments to "
7030 			       "%<__builtin_launder%>");
7031 		postfix_expression = error_mark_node;
7032 	      }
7033 	    break;
7034 
7035 	  case RID_BUILTIN_SHUFFLE:
7036 	    if (vec->length () == 2)
7037 	      postfix_expression
7038 		= build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7039 					 (*vec)[1], tf_warning_or_error);
7040 	    else if (vec->length () == 3)
7041 	      postfix_expression
7042 		= build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7043 					 (*vec)[2], tf_warning_or_error);
7044 	    else
7045 	      {
7046 		error_at (loc, "wrong number of arguments to "
7047 			       "%<__builtin_shuffle%>");
7048 		postfix_expression = error_mark_node;
7049 	      }
7050 	    break;
7051 
7052 	  default:
7053 	    gcc_unreachable ();
7054 	  }
7055 	break;
7056       }
7057 
7058     case RID_BUILTIN_CONVERTVECTOR:
7059       {
7060 	tree expression;
7061 	tree type;
7062 	/* Consume the `__builtin_convertvector' token.  */
7063 	cp_lexer_consume_token (parser->lexer);
7064 	/* Look for the opening `('.  */
7065 	matching_parens parens;
7066 	parens.require_open (parser);
7067 	/* Now, parse the assignment-expression.  */
7068 	expression = cp_parser_assignment_expression (parser);
7069 	/* Look for the `,'.  */
7070 	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7071 	location_t type_location
7072 	  = cp_lexer_peek_token (parser->lexer)->location;
7073 	/* Parse the type-id.  */
7074 	{
7075 	  type_id_in_expr_sentinel s (parser);
7076 	  type = cp_parser_type_id (parser);
7077 	}
7078 	/* Look for the closing `)'.  */
7079 	parens.require_close (parser);
7080 	return cp_build_vec_convert (expression, type_location, type,
7081 				     tf_warning_or_error);
7082       }
7083 
7084     default:
7085       {
7086 	tree type;
7087 
7088 	/* If the next thing is a simple-type-specifier, we may be
7089 	   looking at a functional cast.  We could also be looking at
7090 	   an id-expression.  So, we try the functional cast, and if
7091 	   that doesn't work we fall back to the primary-expression.  */
7092 	cp_parser_parse_tentatively (parser);
7093 	/* Look for the simple-type-specifier.  */
7094         ++parser->prevent_constrained_type_specifiers;
7095 	type = cp_parser_simple_type_specifier (parser,
7096 						/*decl_specs=*/NULL,
7097 						CP_PARSER_FLAGS_NONE);
7098         --parser->prevent_constrained_type_specifiers;
7099 	/* Parse the cast itself.  */
7100 	if (!cp_parser_error_occurred (parser))
7101 	  postfix_expression
7102 	    = cp_parser_functional_cast (parser, type);
7103 	/* If that worked, we're done.  */
7104 	if (cp_parser_parse_definitely (parser))
7105 	  break;
7106 
7107 	/* If the functional-cast didn't work out, try a
7108 	   compound-literal.  */
7109 	if (cp_parser_allow_gnu_extensions_p (parser)
7110 	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7111 	  {
7112 	    cp_expr initializer = NULL_TREE;
7113 
7114 	    cp_parser_parse_tentatively (parser);
7115 
7116 	    matching_parens parens;
7117 	    parens.consume_open (parser);
7118 
7119 	    /* Avoid calling cp_parser_type_id pointlessly, see comment
7120 	       in cp_parser_cast_expression about c++/29234.  */
7121 	    if (!cp_parser_compound_literal_p (parser))
7122 	      cp_parser_simulate_error (parser);
7123 	    else
7124 	      {
7125 		/* Parse the type.  */
7126 		bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7127 		parser->in_type_id_in_expr_p = true;
7128 		type = cp_parser_type_id (parser);
7129 		parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7130 		parens.require_close (parser);
7131 	      }
7132 
7133 	    /* If things aren't going well, there's no need to
7134 	       keep going.  */
7135 	    if (!cp_parser_error_occurred (parser))
7136 	      {
7137 		bool non_constant_p;
7138 		/* Parse the brace-enclosed initializer list.  */
7139 		initializer = cp_parser_braced_list (parser,
7140 						     &non_constant_p);
7141 	      }
7142 	    /* If that worked, we're definitely looking at a
7143 	       compound-literal expression.  */
7144 	    if (cp_parser_parse_definitely (parser))
7145 	      {
7146 		/* Warn the user that a compound literal is not
7147 		   allowed in standard C++.  */
7148 		pedwarn (input_location, OPT_Wpedantic,
7149 			 "ISO C++ forbids compound-literals");
7150 		/* For simplicity, we disallow compound literals in
7151 		   constant-expressions.  We could
7152 		   allow compound literals of integer type, whose
7153 		   initializer was a constant, in constant
7154 		   expressions.  Permitting that usage, as a further
7155 		   extension, would not change the meaning of any
7156 		   currently accepted programs.  (Of course, as
7157 		   compound literals are not part of ISO C++, the
7158 		   standard has nothing to say.)  */
7159 		if (cp_parser_non_integral_constant_expression (parser,
7160 								NIC_NCC))
7161 		  {
7162 		    postfix_expression = error_mark_node;
7163 		    break;
7164 		  }
7165 		/* Form the representation of the compound-literal.  */
7166 		postfix_expression
7167 		  = finish_compound_literal (type, initializer,
7168 					     tf_warning_or_error, fcl_c99);
7169 		postfix_expression.set_location (initializer.get_location ());
7170 		break;
7171 	      }
7172 	  }
7173 
7174 	/* It must be a primary-expression.  */
7175 	postfix_expression
7176 	  = cp_parser_primary_expression (parser, address_p, cast_p,
7177 					  /*template_arg_p=*/false,
7178 					  decltype_p,
7179 					  &idk);
7180       }
7181       break;
7182     }
7183 
7184   /* Note that we don't need to worry about calling build_cplus_new on a
7185      class-valued CALL_EXPR in decltype when it isn't the end of the
7186      postfix-expression; unary_complex_lvalue will take care of that for
7187      all these cases.  */
7188 
7189   /* Keep looping until the postfix-expression is complete.  */
7190   while (true)
7191     {
7192       if (idk == CP_ID_KIND_UNQUALIFIED
7193 	  && identifier_p (postfix_expression)
7194 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7195 	/* It is not a Koenig lookup function call.  */
7196 	postfix_expression
7197 	  = unqualified_name_lookup_error (postfix_expression);
7198 
7199       /* Peek at the next token.  */
7200       token = cp_lexer_peek_token (parser->lexer);
7201 
7202       switch (token->type)
7203 	{
7204 	case CPP_OPEN_SQUARE:
7205 	  if (cp_next_tokens_can_be_std_attribute_p (parser))
7206 	    {
7207 	      cp_parser_error (parser,
7208 			       "two consecutive %<[%> shall "
7209 			       "only introduce an attribute");
7210 	      return error_mark_node;
7211 	    }
7212 	  postfix_expression
7213 	    = cp_parser_postfix_open_square_expression (parser,
7214 							postfix_expression,
7215 							false,
7216 							decltype_p);
7217 	  postfix_expression.set_range (start_loc,
7218 					postfix_expression.get_location ());
7219 
7220 	  idk = CP_ID_KIND_NONE;
7221           is_member_access = false;
7222 	  break;
7223 
7224 	case CPP_OPEN_PAREN:
7225 	  /* postfix-expression ( expression-list [opt] ) */
7226 	  {
7227 	    bool koenig_p;
7228 	    bool is_builtin_constant_p;
7229 	    bool saved_integral_constant_expression_p = false;
7230 	    bool saved_non_integral_constant_expression_p = false;
7231 	    tsubst_flags_t complain = complain_flags (decltype_p);
7232 	    vec<tree, va_gc> *args;
7233 	    location_t close_paren_loc = UNKNOWN_LOCATION;
7234 
7235             is_member_access = false;
7236 
7237 	    tree stripped_expression
7238 	      = tree_strip_any_location_wrapper (postfix_expression);
7239 	    is_builtin_constant_p
7240 	      = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7241 	    if (is_builtin_constant_p)
7242 	      {
7243 		/* The whole point of __builtin_constant_p is to allow
7244 		   non-constant expressions to appear as arguments.  */
7245 		saved_integral_constant_expression_p
7246 		  = parser->integral_constant_expression_p;
7247 		saved_non_integral_constant_expression_p
7248 		  = parser->non_integral_constant_expression_p;
7249 		parser->integral_constant_expression_p = false;
7250 	      }
7251 	    args = (cp_parser_parenthesized_expression_list
7252 		    (parser, non_attr,
7253 		     /*cast_p=*/false, /*allow_expansion_p=*/true,
7254 		     /*non_constant_p=*/NULL,
7255 		     /*close_paren_loc=*/&close_paren_loc,
7256 		     /*wrap_locations_p=*/true));
7257 	    if (is_builtin_constant_p)
7258 	      {
7259 		parser->integral_constant_expression_p
7260 		  = saved_integral_constant_expression_p;
7261 		parser->non_integral_constant_expression_p
7262 		  = saved_non_integral_constant_expression_p;
7263 	      }
7264 
7265 	    if (args == NULL)
7266 	      {
7267 		postfix_expression = error_mark_node;
7268 		break;
7269 	      }
7270 
7271 	    /* Function calls are not permitted in
7272 	       constant-expressions.  */
7273 	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
7274 		&& cp_parser_non_integral_constant_expression (parser,
7275 							       NIC_FUNC_CALL))
7276 	      {
7277 		postfix_expression = error_mark_node;
7278 		release_tree_vector (args);
7279 		break;
7280 	      }
7281 
7282 	    koenig_p = false;
7283 	    if (idk == CP_ID_KIND_UNQUALIFIED
7284 		|| idk == CP_ID_KIND_TEMPLATE_ID)
7285 	      {
7286 		if (identifier_p (postfix_expression)
7287 		    /* In C++2A, we may need to perform ADL for a template
7288 		       name.  */
7289 		    || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7290 			&& identifier_p (TREE_OPERAND (postfix_expression, 0))))
7291 		  {
7292 		    if (!args->is_empty ())
7293 		      {
7294 			koenig_p = true;
7295 			if (!any_type_dependent_arguments_p (args))
7296 			  postfix_expression
7297 			    = perform_koenig_lookup (postfix_expression, args,
7298 						     complain);
7299 		      }
7300 		    else
7301 		      postfix_expression
7302 			= unqualified_fn_lookup_error (postfix_expression);
7303 		  }
7304 		/* We do not perform argument-dependent lookup if
7305 		   normal lookup finds a non-function, in accordance
7306 		   with the expected resolution of DR 218.  */
7307 		else if (!args->is_empty ()
7308 			 && is_overloaded_fn (postfix_expression))
7309 		  {
7310 		    /* We only need to look at the first function,
7311 		       because all the fns share the attribute we're
7312 		       concerned with (all member fns or all local
7313 		       fns).  */
7314 		    tree fn = get_first_fn (postfix_expression);
7315 		    fn = STRIP_TEMPLATE (fn);
7316 
7317 		    /* Do not do argument dependent lookup if regular
7318 		       lookup finds a member function or a block-scope
7319 		       function declaration.  [basic.lookup.argdep]/3  */
7320 		    if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7321 			  || DECL_FUNCTION_MEMBER_P (fn)
7322 			  || DECL_LOCAL_FUNCTION_P (fn)))
7323 		      {
7324 			koenig_p = true;
7325 			if (!any_type_dependent_arguments_p (args))
7326 			  postfix_expression
7327 			    = perform_koenig_lookup (postfix_expression, args,
7328 						     complain);
7329 		      }
7330 		  }
7331 	      }
7332 
7333 	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7334 	      {
7335 		tree instance = TREE_OPERAND (postfix_expression, 0);
7336 		tree fn = TREE_OPERAND (postfix_expression, 1);
7337 
7338 		if (processing_template_decl
7339 		    && (type_dependent_object_expression_p (instance)
7340 			|| (!BASELINK_P (fn)
7341 			    && TREE_CODE (fn) != FIELD_DECL)
7342 			|| type_dependent_expression_p (fn)
7343 			|| any_type_dependent_arguments_p (args)))
7344 		  {
7345 		    maybe_generic_this_capture (instance, fn);
7346 		    postfix_expression
7347 		      = build_min_nt_call_vec (postfix_expression, args);
7348 		    release_tree_vector (args);
7349 		    break;
7350 		  }
7351 
7352 		if (BASELINK_P (fn))
7353 		  {
7354 		  postfix_expression
7355 		    = (build_new_method_call
7356 		       (instance, fn, &args, NULL_TREE,
7357 			(idk == CP_ID_KIND_QUALIFIED
7358 			 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7359 			 : LOOKUP_NORMAL),
7360 			/*fn_p=*/NULL,
7361 			complain));
7362 		  }
7363 		else
7364 		  postfix_expression
7365 		    = finish_call_expr (postfix_expression, &args,
7366 					/*disallow_virtual=*/false,
7367 					/*koenig_p=*/false,
7368 					complain);
7369 	      }
7370 	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
7371 		     || TREE_CODE (postfix_expression) == MEMBER_REF
7372 		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7373 	      postfix_expression = (build_offset_ref_call_from_tree
7374 				    (postfix_expression, &args,
7375 				     complain));
7376 	    else if (idk == CP_ID_KIND_QUALIFIED)
7377 	      /* A call to a static class member, or a namespace-scope
7378 		 function.  */
7379 	      postfix_expression
7380 		= finish_call_expr (postfix_expression, &args,
7381 				    /*disallow_virtual=*/true,
7382 				    koenig_p,
7383 				    complain);
7384 	    else
7385 	      /* All other function calls.  */
7386 	      postfix_expression
7387 		= finish_call_expr (postfix_expression, &args,
7388 				    /*disallow_virtual=*/false,
7389 				    koenig_p,
7390 				    complain);
7391 
7392 	    if (close_paren_loc != UNKNOWN_LOCATION)
7393 	      {
7394 		location_t combined_loc = make_location (token->location,
7395 							 start_loc,
7396 							 close_paren_loc);
7397 		postfix_expression.set_location (combined_loc);
7398 	      }
7399 
7400 	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
7401 	    idk = CP_ID_KIND_NONE;
7402 
7403 	    release_tree_vector (args);
7404 	  }
7405 	  break;
7406 
7407 	case CPP_DOT:
7408 	case CPP_DEREF:
7409 	  /* postfix-expression . template [opt] id-expression
7410 	     postfix-expression . pseudo-destructor-name
7411 	     postfix-expression -> template [opt] id-expression
7412 	     postfix-expression -> pseudo-destructor-name */
7413 
7414 	  /* Consume the `.' or `->' operator.  */
7415 	  cp_lexer_consume_token (parser->lexer);
7416 
7417 	  postfix_expression
7418 	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
7419 						      postfix_expression,
7420 						      false, &idk, loc);
7421 
7422           is_member_access = true;
7423 	  break;
7424 
7425 	case CPP_PLUS_PLUS:
7426 	  /* postfix-expression ++  */
7427 	  /* Consume the `++' token.  */
7428 	  cp_lexer_consume_token (parser->lexer);
7429 	  /* Generate a representation for the complete expression.  */
7430 	  postfix_expression
7431 	    = finish_increment_expr (postfix_expression,
7432 				     POSTINCREMENT_EXPR);
7433 	  /* Increments may not appear in constant-expressions.  */
7434 	  if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7435 	    postfix_expression = error_mark_node;
7436 	  idk = CP_ID_KIND_NONE;
7437           is_member_access = false;
7438 	  break;
7439 
7440 	case CPP_MINUS_MINUS:
7441 	  /* postfix-expression -- */
7442 	  /* Consume the `--' token.  */
7443 	  cp_lexer_consume_token (parser->lexer);
7444 	  /* Generate a representation for the complete expression.  */
7445 	  postfix_expression
7446 	    = finish_increment_expr (postfix_expression,
7447 				     POSTDECREMENT_EXPR);
7448 	  /* Decrements may not appear in constant-expressions.  */
7449 	  if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7450 	    postfix_expression = error_mark_node;
7451 	  idk = CP_ID_KIND_NONE;
7452           is_member_access = false;
7453 	  break;
7454 
7455 	default:
7456 	  if (pidk_return != NULL)
7457 	    * pidk_return = idk;
7458           if (member_access_only_p)
7459             return is_member_access
7460               ? postfix_expression
7461               : cp_expr (error_mark_node);
7462           else
7463             return postfix_expression;
7464 	}
7465     }
7466 
7467   /* We should never get here.  */
7468   gcc_unreachable ();
7469   return error_mark_node;
7470 }
7471 
7472 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7473    by cp_parser_builtin_offsetof.  We're looking for
7474 
7475      postfix-expression [ expression ]
7476      postfix-expression [ braced-init-list ] (C++11)
7477 
7478    FOR_OFFSETOF is set if we're being called in that context, which
7479    changes how we deal with integer constant expressions.  */
7480 
7481 static tree
cp_parser_postfix_open_square_expression(cp_parser * parser,tree postfix_expression,bool for_offsetof,bool decltype_p)7482 cp_parser_postfix_open_square_expression (cp_parser *parser,
7483 					  tree postfix_expression,
7484 					  bool for_offsetof,
7485 					  bool decltype_p)
7486 {
7487   tree index = NULL_TREE;
7488   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7489   bool saved_greater_than_is_operator_p;
7490 
7491   /* Consume the `[' token.  */
7492   cp_lexer_consume_token (parser->lexer);
7493 
7494   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7495   parser->greater_than_is_operator_p = true;
7496 
7497   /* Parse the index expression.  */
7498   /* ??? For offsetof, there is a question of what to allow here.  If
7499      offsetof is not being used in an integral constant expression context,
7500      then we *could* get the right answer by computing the value at runtime.
7501      If we are in an integral constant expression context, then we might
7502      could accept any constant expression; hard to say without analysis.
7503      Rather than open the barn door too wide right away, allow only integer
7504      constant expressions here.  */
7505   if (for_offsetof)
7506     index = cp_parser_constant_expression (parser);
7507   else
7508     {
7509       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7510 	{
7511 	  bool expr_nonconst_p;
7512 	  cp_lexer_set_source_position (parser->lexer);
7513 	  maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7514 	  index = cp_parser_braced_list (parser, &expr_nonconst_p);
7515 	}
7516       else
7517 	index = cp_parser_expression (parser);
7518     }
7519 
7520   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7521 
7522   /* Look for the closing `]'.  */
7523   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7524 
7525   /* Build the ARRAY_REF.  */
7526   postfix_expression = grok_array_decl (loc, postfix_expression,
7527 					index, decltype_p);
7528 
7529   /* When not doing offsetof, array references are not permitted in
7530      constant-expressions.  */
7531   if (!for_offsetof
7532       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7533     postfix_expression = error_mark_node;
7534 
7535   return postfix_expression;
7536 }
7537 
7538 /* A subroutine of cp_parser_postfix_dot_deref_expression.  Handle dot
7539    dereference of incomplete type, returns true if error_mark_node should
7540    be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7541    and *DEPENDENT_P.  */
7542 
7543 bool
cp_parser_dot_deref_incomplete(tree * scope,cp_expr * postfix_expression,bool * dependent_p)7544 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7545 				bool *dependent_p)
7546 {
7547   /* In a template, be permissive by treating an object expression
7548      of incomplete type as dependent (after a pedwarn).  */
7549   diagnostic_t kind = (processing_template_decl
7550 		       && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7551 
7552   switch (TREE_CODE (*postfix_expression))
7553     {
7554     case CAST_EXPR:
7555     case REINTERPRET_CAST_EXPR:
7556     case CONST_CAST_EXPR:
7557     case STATIC_CAST_EXPR:
7558     case DYNAMIC_CAST_EXPR:
7559     case IMPLICIT_CONV_EXPR:
7560     case VIEW_CONVERT_EXPR:
7561     case NON_LVALUE_EXPR:
7562       kind = DK_ERROR;
7563       break;
7564     case OVERLOAD:
7565       /* Don't emit any diagnostic for OVERLOADs.  */
7566       kind = DK_IGNORED;
7567       break;
7568     default:
7569       /* Avoid clobbering e.g. DECLs.  */
7570       if (!EXPR_P (*postfix_expression))
7571 	kind = DK_ERROR;
7572       break;
7573     }
7574 
7575   if (kind == DK_IGNORED)
7576     return false;
7577 
7578   location_t exploc = location_of (*postfix_expression);
7579   cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7580   if (!MAYBE_CLASS_TYPE_P (*scope))
7581     return true;
7582   if (kind == DK_ERROR)
7583     *scope = *postfix_expression = error_mark_node;
7584   else if (processing_template_decl)
7585     {
7586       *dependent_p = true;
7587       *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7588     }
7589   return false;
7590 }
7591 
7592 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7593    by cp_parser_builtin_offsetof.  We're looking for
7594 
7595      postfix-expression . template [opt] id-expression
7596      postfix-expression . pseudo-destructor-name
7597      postfix-expression -> template [opt] id-expression
7598      postfix-expression -> pseudo-destructor-name
7599 
7600    FOR_OFFSETOF is set if we're being called in that context.  That sorta
7601    limits what of the above we'll actually accept, but nevermind.
7602    TOKEN_TYPE is the "." or "->" token, which will already have been
7603    removed from the stream.  */
7604 
7605 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)7606 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7607 					enum cpp_ttype token_type,
7608 					cp_expr postfix_expression,
7609 					bool for_offsetof, cp_id_kind *idk,
7610 					location_t location)
7611 {
7612   tree name;
7613   bool dependent_p;
7614   bool pseudo_destructor_p;
7615   tree scope = NULL_TREE;
7616   location_t start_loc = postfix_expression.get_start ();
7617 
7618   /* If this is a `->' operator, dereference the pointer.  */
7619   if (token_type == CPP_DEREF)
7620     postfix_expression = build_x_arrow (location, postfix_expression,
7621 					tf_warning_or_error);
7622   /* Check to see whether or not the expression is type-dependent and
7623      not the current instantiation.  */
7624   dependent_p = type_dependent_object_expression_p (postfix_expression);
7625   /* The identifier following the `->' or `.' is not qualified.  */
7626   parser->scope = NULL_TREE;
7627   parser->qualifying_scope = NULL_TREE;
7628   parser->object_scope = NULL_TREE;
7629   *idk = CP_ID_KIND_NONE;
7630 
7631   /* Enter the scope corresponding to the type of the object
7632      given by the POSTFIX_EXPRESSION.  */
7633   if (!dependent_p)
7634     {
7635       scope = TREE_TYPE (postfix_expression);
7636       /* According to the standard, no expression should ever have
7637 	 reference type.  Unfortunately, we do not currently match
7638 	 the standard in this respect in that our internal representation
7639 	 of an expression may have reference type even when the standard
7640 	 says it does not.  Therefore, we have to manually obtain the
7641 	 underlying type here.  */
7642       scope = non_reference (scope);
7643       /* The type of the POSTFIX_EXPRESSION must be complete.  */
7644       /* Unlike the object expression in other contexts, *this is not
7645 	 required to be of complete type for purposes of class member
7646 	 access (5.2.5) outside the member function body.  */
7647       if (postfix_expression != current_class_ref
7648 	  && scope != error_mark_node
7649 	  && !currently_open_class (scope))
7650 	{
7651 	  scope = complete_type (scope);
7652 	  if (!COMPLETE_TYPE_P (scope)
7653 	      && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7654 						 &dependent_p))
7655 	    return error_mark_node;
7656 	}
7657 
7658       if (!dependent_p)
7659 	{
7660 	  /* Let the name lookup machinery know that we are processing a
7661 	     class member access expression.  */
7662 	  parser->context->object_type = scope;
7663 	  /* If something went wrong, we want to be able to discern that case,
7664 	     as opposed to the case where there was no SCOPE due to the type
7665 	     of expression being dependent.  */
7666 	  if (!scope)
7667 	    scope = error_mark_node;
7668 	  /* If the SCOPE was erroneous, make the various semantic analysis
7669 	     functions exit quickly -- and without issuing additional error
7670 	     messages.  */
7671 	  if (scope == error_mark_node)
7672 	    postfix_expression = error_mark_node;
7673 	}
7674     }
7675 
7676   if (dependent_p)
7677     /* Tell cp_parser_lookup_name that there was an object, even though it's
7678        type-dependent.  */
7679     parser->context->object_type = unknown_type_node;
7680 
7681   /* Assume this expression is not a pseudo-destructor access.  */
7682   pseudo_destructor_p = false;
7683 
7684   /* If the SCOPE is a scalar type, then, if this is a valid program,
7685      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
7686      is type dependent, it can be pseudo-destructor-name or something else.
7687      Try to parse it as pseudo-destructor-name first.  */
7688   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7689     {
7690       tree s;
7691       tree type;
7692 
7693       cp_parser_parse_tentatively (parser);
7694       /* Parse the pseudo-destructor-name.  */
7695       s = NULL_TREE;
7696       cp_parser_pseudo_destructor_name (parser, postfix_expression,
7697 					&s, &type);
7698       if (dependent_p
7699 	  && (cp_parser_error_occurred (parser)
7700 	      || !SCALAR_TYPE_P (type)))
7701 	cp_parser_abort_tentative_parse (parser);
7702       else if (cp_parser_parse_definitely (parser))
7703 	{
7704 	  pseudo_destructor_p = true;
7705 	  postfix_expression
7706 	    = finish_pseudo_destructor_expr (postfix_expression,
7707 					     s, type, location);
7708 	}
7709     }
7710 
7711   if (!pseudo_destructor_p)
7712     {
7713       /* If the SCOPE is not a scalar type, we are looking at an
7714 	 ordinary class member access expression, rather than a
7715 	 pseudo-destructor-name.  */
7716       bool template_p;
7717       cp_token *token = cp_lexer_peek_token (parser->lexer);
7718       /* Parse the id-expression.  */
7719       name = (cp_parser_id_expression
7720 	      (parser,
7721 	       cp_parser_optional_template_keyword (parser),
7722 	       /*check_dependency_p=*/true,
7723 	       &template_p,
7724 	       /*declarator_p=*/false,
7725 	       /*optional_p=*/false));
7726       /* In general, build a SCOPE_REF if the member name is qualified.
7727 	 However, if the name was not dependent and has already been
7728 	 resolved; there is no need to build the SCOPE_REF.  For example;
7729 
7730 	     struct X { void f(); };
7731 	     template <typename T> void f(T* t) { t->X::f(); }
7732 
7733 	 Even though "t" is dependent, "X::f" is not and has been resolved
7734 	 to a BASELINK; there is no need to include scope information.  */
7735 
7736       /* But we do need to remember that there was an explicit scope for
7737 	 virtual function calls.  */
7738       if (parser->scope)
7739 	*idk = CP_ID_KIND_QUALIFIED;
7740 
7741       /* If the name is a template-id that names a type, we will get a
7742 	 TYPE_DECL here.  That is invalid code.  */
7743       if (TREE_CODE (name) == TYPE_DECL)
7744 	{
7745 	  error_at (token->location, "invalid use of %qD", name);
7746 	  postfix_expression = error_mark_node;
7747 	}
7748       else
7749 	{
7750 	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7751 	    {
7752 	      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7753 		{
7754 		  error_at (token->location, "%<%D::%D%> is not a class member",
7755 			    parser->scope, name);
7756 		  postfix_expression = error_mark_node;
7757 		}
7758 	      else
7759 		name = build_qualified_name (/*type=*/NULL_TREE,
7760 					     parser->scope,
7761 					     name,
7762 					     template_p);
7763 	      parser->scope = NULL_TREE;
7764 	      parser->qualifying_scope = NULL_TREE;
7765 	      parser->object_scope = NULL_TREE;
7766 	    }
7767 	  if (parser->scope && name && BASELINK_P (name))
7768 	    adjust_result_of_qualified_name_lookup
7769 	      (name, parser->scope, scope);
7770 	  postfix_expression
7771 	    = finish_class_member_access_expr (postfix_expression, name,
7772 					       template_p,
7773 					       tf_warning_or_error);
7774 	  /* Build a location e.g.:
7775 	       ptr->access_expr
7776 	       ~~~^~~~~~~~~~~~~
7777 	     where the caret is at the deref token, ranging from
7778 	     the start of postfix_expression to the end of the access expr.  */
7779 	  location_t end_loc
7780 	    = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7781 	  location_t combined_loc
7782 	    = make_location (input_location, start_loc, end_loc);
7783 	  protected_set_expr_location (postfix_expression, combined_loc);
7784 	}
7785     }
7786 
7787   /* We no longer need to look up names in the scope of the object on
7788      the left-hand side of the `.' or `->' operator.  */
7789   parser->context->object_type = NULL_TREE;
7790 
7791   /* Outside of offsetof, these operators may not appear in
7792      constant-expressions.  */
7793   if (!for_offsetof
7794       && (cp_parser_non_integral_constant_expression
7795 	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7796     postfix_expression = error_mark_node;
7797 
7798   return postfix_expression;
7799 }
7800 
7801 /* Parse a parenthesized expression-list.
7802 
7803    expression-list:
7804      assignment-expression
7805      expression-list, assignment-expression
7806 
7807    attribute-list:
7808      expression-list
7809      identifier
7810      identifier, expression-list
7811 
7812    CAST_P is true if this expression is the target of a cast.
7813 
7814    ALLOW_EXPANSION_P is true if this expression allows expansion of an
7815    argument pack.
7816 
7817    WRAP_LOCATIONS_P is true if expressions within this list for which
7818    CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7819    their source locations.
7820 
7821    Returns a vector of trees.  Each element is a representation of an
7822    assignment-expression.  NULL is returned if the ( and or ) are
7823    missing.  An empty, but allocated, vector is returned on no
7824    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
7825    if we are parsing an attribute list for an attribute that wants a
7826    plain identifier argument, normal_attr for an attribute that wants
7827    an expression, or non_attr if we aren't parsing an attribute list.  If
7828    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7829    not all of the expressions in the list were constant.
7830    If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7831    will be written to with the location of the closing parenthesis.  If
7832    an error occurs, it may or may not be written to.  */
7833 
7834 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)7835 cp_parser_parenthesized_expression_list (cp_parser* parser,
7836 					 int is_attribute_list,
7837 					 bool cast_p,
7838                                          bool allow_expansion_p,
7839 					 bool *non_constant_p,
7840 					 location_t *close_paren_loc,
7841 					 bool wrap_locations_p)
7842 {
7843   vec<tree, va_gc> *expression_list;
7844   bool fold_expr_p = is_attribute_list != non_attr;
7845   tree identifier = NULL_TREE;
7846   bool saved_greater_than_is_operator_p;
7847 
7848   /* Assume all the expressions will be constant.  */
7849   if (non_constant_p)
7850     *non_constant_p = false;
7851 
7852   matching_parens parens;
7853   if (!parens.require_open (parser))
7854     return NULL;
7855 
7856   expression_list = make_tree_vector ();
7857 
7858   /* Within a parenthesized expression, a `>' token is always
7859      the greater-than operator.  */
7860   saved_greater_than_is_operator_p
7861     = parser->greater_than_is_operator_p;
7862   parser->greater_than_is_operator_p = true;
7863 
7864   cp_expr expr (NULL_TREE);
7865 
7866   /* Consume expressions until there are no more.  */
7867   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7868     while (true)
7869       {
7870 	/* At the beginning of attribute lists, check to see if the
7871 	   next token is an identifier.  */
7872 	if (is_attribute_list == id_attr
7873 	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7874 	  {
7875 	    cp_token *token;
7876 
7877 	    /* Consume the identifier.  */
7878 	    token = cp_lexer_consume_token (parser->lexer);
7879 	    /* Save the identifier.  */
7880 	    identifier = token->u.value;
7881 	  }
7882 	else
7883 	  {
7884 	    bool expr_non_constant_p;
7885 
7886 	    /* Parse the next assignment-expression.  */
7887 	    if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7888 	      {
7889 		/* A braced-init-list.  */
7890 		cp_lexer_set_source_position (parser->lexer);
7891 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7892 		expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7893 		if (non_constant_p && expr_non_constant_p)
7894 		  *non_constant_p = true;
7895 	      }
7896 	    else if (non_constant_p)
7897 	      {
7898 		expr = (cp_parser_constant_expression
7899 			(parser, /*allow_non_constant_p=*/true,
7900 			 &expr_non_constant_p));
7901 		if (expr_non_constant_p)
7902 		  *non_constant_p = true;
7903 	      }
7904 	    else
7905 	      expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7906 						      cast_p);
7907 
7908 	    if (fold_expr_p)
7909 	      expr = instantiate_non_dependent_expr (expr);
7910 
7911             /* If we have an ellipsis, then this is an expression
7912 	       expansion.  */
7913             if (allow_expansion_p
7914                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7915               {
7916                 /* Consume the `...'.  */
7917                 cp_lexer_consume_token (parser->lexer);
7918 
7919                 /* Build the argument pack.  */
7920                 expr = make_pack_expansion (expr);
7921               }
7922 
7923 	    if (wrap_locations_p)
7924 	      expr.maybe_add_location_wrapper ();
7925 
7926 	     /* Add it to the list.  We add error_mark_node
7927 		expressions to the list, so that we can still tell if
7928 		the correct form for a parenthesized expression-list
7929 		is found. That gives better errors.  */
7930 	    vec_safe_push (expression_list, expr.get_value ());
7931 
7932 	    if (expr == error_mark_node)
7933 	      goto skip_comma;
7934 	  }
7935 
7936 	/* After the first item, attribute lists look the same as
7937 	   expression lists.  */
7938 	is_attribute_list = non_attr;
7939 
7940       get_comma:;
7941 	/* If the next token isn't a `,', then we are done.  */
7942 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7943 	  break;
7944 
7945 	/* Otherwise, consume the `,' and keep going.  */
7946 	cp_lexer_consume_token (parser->lexer);
7947       }
7948 
7949   if (close_paren_loc)
7950     *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7951 
7952   if (!parens.require_close (parser))
7953     {
7954       int ending;
7955 
7956     skip_comma:;
7957       /* We try and resync to an unnested comma, as that will give the
7958 	 user better diagnostics.  */
7959       ending = cp_parser_skip_to_closing_parenthesis (parser,
7960 						      /*recovering=*/true,
7961 						      /*or_comma=*/true,
7962 						      /*consume_paren=*/true);
7963       if (ending < 0)
7964 	goto get_comma;
7965       if (!ending)
7966 	{
7967 	  parser->greater_than_is_operator_p
7968 	    = saved_greater_than_is_operator_p;
7969 	  return NULL;
7970 	}
7971     }
7972 
7973   parser->greater_than_is_operator_p
7974     = saved_greater_than_is_operator_p;
7975 
7976   if (identifier)
7977     vec_safe_insert (expression_list, 0, identifier);
7978 
7979   return expression_list;
7980 }
7981 
7982 /* Parse a pseudo-destructor-name.
7983 
7984    pseudo-destructor-name:
7985      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7986      :: [opt] nested-name-specifier template template-id :: ~ type-name
7987      :: [opt] nested-name-specifier [opt] ~ type-name
7988 
7989    If either of the first two productions is used, sets *SCOPE to the
7990    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7991    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7992    or ERROR_MARK_NODE if the parse fails.  */
7993 
7994 static void
cp_parser_pseudo_destructor_name(cp_parser * parser,tree object,tree * scope,tree * type)7995 cp_parser_pseudo_destructor_name (cp_parser* parser,
7996 				  tree object,
7997 				  tree* scope,
7998 				  tree* type)
7999 {
8000   bool nested_name_specifier_p;
8001 
8002   /* Handle ~auto.  */
8003   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8004       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8005       && !type_dependent_expression_p (object))
8006     {
8007       if (cxx_dialect < cxx14)
8008 	pedwarn (input_location, 0,
8009 		 "%<~auto%> only available with "
8010 		 "%<-std=c++14%> or %<-std=gnu++14%>");
8011       cp_lexer_consume_token (parser->lexer);
8012       cp_lexer_consume_token (parser->lexer);
8013       *scope = NULL_TREE;
8014       *type = TREE_TYPE (object);
8015       return;
8016     }
8017 
8018   /* Assume that things will not work out.  */
8019   *type = error_mark_node;
8020 
8021   /* Look for the optional `::' operator.  */
8022   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8023   /* Look for the optional nested-name-specifier.  */
8024   nested_name_specifier_p
8025     = (cp_parser_nested_name_specifier_opt (parser,
8026 					    /*typename_keyword_p=*/false,
8027 					    /*check_dependency_p=*/true,
8028 					    /*type_p=*/false,
8029 					    /*is_declaration=*/false)
8030        != NULL_TREE);
8031   /* Now, if we saw a nested-name-specifier, we might be doing the
8032      second production.  */
8033   if (nested_name_specifier_p
8034       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8035     {
8036       /* Consume the `template' keyword.  */
8037       cp_lexer_consume_token (parser->lexer);
8038       /* Parse the template-id.  */
8039       cp_parser_template_id (parser,
8040 			     /*template_keyword_p=*/true,
8041 			     /*check_dependency_p=*/false,
8042 			     class_type,
8043 			     /*is_declaration=*/true);
8044       /* Look for the `::' token.  */
8045       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8046     }
8047   /* If the next token is not a `~', then there might be some
8048      additional qualification.  */
8049   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8050     {
8051       /* At this point, we're looking for "type-name :: ~".  The type-name
8052 	 must not be a class-name, since this is a pseudo-destructor.  So,
8053 	 it must be either an enum-name, or a typedef-name -- both of which
8054 	 are just identifiers.  So, we peek ahead to check that the "::"
8055 	 and "~" tokens are present; if they are not, then we can avoid
8056 	 calling type_name.  */
8057       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8058 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8059 	  || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8060 	{
8061 	  cp_parser_error (parser, "non-scalar type");
8062 	  return;
8063 	}
8064 
8065       /* Look for the type-name.  */
8066       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8067       if (*scope == error_mark_node)
8068 	return;
8069 
8070       /* Look for the `::' token.  */
8071       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8072     }
8073   else
8074     *scope = NULL_TREE;
8075 
8076   /* Look for the `~'.  */
8077   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8078 
8079   /* Once we see the ~, this has to be a pseudo-destructor.  */
8080   if (!processing_template_decl && !cp_parser_error_occurred (parser))
8081     cp_parser_commit_to_topmost_tentative_parse (parser);
8082 
8083   /* Look for the type-name again.  We are not responsible for
8084      checking that it matches the first type-name.  */
8085   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8086 }
8087 
8088 /* Parse a unary-expression.
8089 
8090    unary-expression:
8091      postfix-expression
8092      ++ cast-expression
8093      -- cast-expression
8094      unary-operator cast-expression
8095      sizeof unary-expression
8096      sizeof ( type-id )
8097      alignof ( type-id )  [C++0x]
8098      new-expression
8099      delete-expression
8100 
8101    GNU Extensions:
8102 
8103    unary-expression:
8104      __extension__ cast-expression
8105      __alignof__ unary-expression
8106      __alignof__ ( type-id )
8107      alignof unary-expression  [C++0x]
8108      __real__ cast-expression
8109      __imag__ cast-expression
8110      && identifier
8111      sizeof ( type-id ) { initializer-list , [opt] }
8112      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8113      __alignof__ ( type-id ) { initializer-list , [opt] }
8114 
8115    ADDRESS_P is true iff the unary-expression is appearing as the
8116    operand of the `&' operator.   CAST_P is true if this expression is
8117    the target of a cast.
8118 
8119    Returns a representation of the expression.  */
8120 
8121 static cp_expr
cp_parser_unary_expression(cp_parser * parser,cp_id_kind * pidk,bool address_p,bool cast_p,bool decltype_p)8122 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8123 			    bool address_p, bool cast_p, bool decltype_p)
8124 {
8125   cp_token *token;
8126   enum tree_code unary_operator;
8127 
8128   /* Peek at the next token.  */
8129   token = cp_lexer_peek_token (parser->lexer);
8130   /* Some keywords give away the kind of expression.  */
8131   if (token->type == CPP_KEYWORD)
8132     {
8133       enum rid keyword = token->keyword;
8134 
8135       switch (keyword)
8136 	{
8137 	case RID_ALIGNOF:
8138 	case RID_SIZEOF:
8139 	  {
8140 	    tree operand, ret;
8141 	    enum tree_code op;
8142 	    location_t start_loc = token->location;
8143 
8144 	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8145 	    bool std_alignof = id_equal (token->u.value, "alignof");
8146 
8147 	    /* Consume the token.  */
8148 	    cp_lexer_consume_token (parser->lexer);
8149 	    /* Parse the operand.  */
8150 	    operand = cp_parser_sizeof_operand (parser, keyword);
8151 
8152 	    if (TYPE_P (operand))
8153 	      ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8154 						true);
8155 	    else
8156 	      {
8157 		/* ISO C++ defines alignof only with types, not with
8158 		   expressions. So pedwarn if alignof is used with a non-
8159 		   type expression. However, __alignof__ is ok.  */
8160 		if (std_alignof)
8161 		  pedwarn (token->location, OPT_Wpedantic,
8162 			   "ISO C++ does not allow %<alignof%> "
8163 			   "with a non-type");
8164 
8165 		ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8166 	      }
8167 	    /* For SIZEOF_EXPR, just issue diagnostics, but keep
8168 	       SIZEOF_EXPR with the original operand.  */
8169 	    if (op == SIZEOF_EXPR && ret != error_mark_node)
8170 	      {
8171 		if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8172 		  {
8173 		    if (!processing_template_decl && TYPE_P (operand))
8174 		      {
8175 			ret = build_min (SIZEOF_EXPR, size_type_node,
8176 					 build1 (NOP_EXPR, operand,
8177 						 error_mark_node));
8178 			SIZEOF_EXPR_TYPE_P (ret) = 1;
8179 		      }
8180 		    else
8181 		      ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8182 		    TREE_SIDE_EFFECTS (ret) = 0;
8183 		    TREE_READONLY (ret) = 1;
8184 		  }
8185 	      }
8186 
8187 	    /* Construct a location e.g. :
8188 	       alignof (expr)
8189 	       ^~~~~~~~~~~~~~
8190 	       with start == caret at the start of the "alignof"/"sizeof"
8191 	       token, with the endpoint at the final closing paren.  */
8192 	    location_t finish_loc
8193 	      = cp_lexer_previous_token (parser->lexer)->location;
8194 	    location_t compound_loc
8195 	      = make_location (start_loc, start_loc, finish_loc);
8196 
8197 	    cp_expr ret_expr (ret);
8198 	    ret_expr.set_location (compound_loc);
8199 	    ret_expr = ret_expr.maybe_add_location_wrapper ();
8200 	    return ret_expr;
8201 	  }
8202 
8203 	case RID_BUILTIN_HAS_ATTRIBUTE:
8204 	  return cp_parser_has_attribute_expression (parser);
8205 
8206 	case RID_NEW:
8207 	  return cp_parser_new_expression (parser);
8208 
8209 	case RID_DELETE:
8210 	  return cp_parser_delete_expression (parser);
8211 
8212 	case RID_EXTENSION:
8213 	  {
8214 	    /* The saved value of the PEDANTIC flag.  */
8215 	    int saved_pedantic;
8216 	    tree expr;
8217 
8218 	    /* Save away the PEDANTIC flag.  */
8219 	    cp_parser_extension_opt (parser, &saved_pedantic);
8220 	    /* Parse the cast-expression.  */
8221 	    expr = cp_parser_simple_cast_expression (parser);
8222 	    /* Restore the PEDANTIC flag.  */
8223 	    pedantic = saved_pedantic;
8224 
8225 	    return expr;
8226 	  }
8227 
8228 	case RID_REALPART:
8229 	case RID_IMAGPART:
8230 	  {
8231 	    tree expression;
8232 
8233 	    /* Consume the `__real__' or `__imag__' token.  */
8234 	    cp_lexer_consume_token (parser->lexer);
8235 	    /* Parse the cast-expression.  */
8236 	    expression = cp_parser_simple_cast_expression (parser);
8237 	    /* Create the complete representation.  */
8238 	    return build_x_unary_op (token->location,
8239 				     (keyword == RID_REALPART
8240 				      ? REALPART_EXPR : IMAGPART_EXPR),
8241 				     expression,
8242                                      tf_warning_or_error);
8243 	  }
8244 	  break;
8245 
8246 	case RID_TRANSACTION_ATOMIC:
8247 	case RID_TRANSACTION_RELAXED:
8248 	  return cp_parser_transaction_expression (parser, keyword);
8249 
8250 	case RID_NOEXCEPT:
8251 	  {
8252 	    tree expr;
8253 	    const char *saved_message;
8254 	    bool saved_integral_constant_expression_p;
8255 	    bool saved_non_integral_constant_expression_p;
8256 	    bool saved_greater_than_is_operator_p;
8257 
8258 	    location_t start_loc = token->location;
8259 
8260 	    cp_lexer_consume_token (parser->lexer);
8261 	    matching_parens parens;
8262 	    parens.require_open (parser);
8263 
8264 	    saved_message = parser->type_definition_forbidden_message;
8265 	    parser->type_definition_forbidden_message
8266 	      = G_("types may not be defined in %<noexcept%> expressions");
8267 
8268 	    saved_integral_constant_expression_p
8269 	      = parser->integral_constant_expression_p;
8270 	    saved_non_integral_constant_expression_p
8271 	      = parser->non_integral_constant_expression_p;
8272 	    parser->integral_constant_expression_p = false;
8273 
8274 	    saved_greater_than_is_operator_p
8275 	      = parser->greater_than_is_operator_p;
8276 	    parser->greater_than_is_operator_p = true;
8277 
8278 	    ++cp_unevaluated_operand;
8279 	    ++c_inhibit_evaluation_warnings;
8280 	    ++cp_noexcept_operand;
8281 	    expr = cp_parser_expression (parser);
8282 	    --cp_noexcept_operand;
8283 	    --c_inhibit_evaluation_warnings;
8284 	    --cp_unevaluated_operand;
8285 
8286 	    parser->greater_than_is_operator_p
8287 	      = saved_greater_than_is_operator_p;
8288 
8289 	    parser->integral_constant_expression_p
8290 	      = saved_integral_constant_expression_p;
8291 	    parser->non_integral_constant_expression_p
8292 	      = saved_non_integral_constant_expression_p;
8293 
8294 	    parser->type_definition_forbidden_message = saved_message;
8295 
8296 	    location_t finish_loc
8297 	      = cp_lexer_peek_token (parser->lexer)->location;
8298 	    parens.require_close (parser);
8299 
8300 	    /* Construct a location of the form:
8301 	       noexcept (expr)
8302 	       ^~~~~~~~~~~~~~~
8303 	       with start == caret, finishing at the close-paren.  */
8304 	    location_t noexcept_loc
8305 	      = make_location (start_loc, start_loc, finish_loc);
8306 
8307 	    return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8308 			    noexcept_loc);
8309 	  }
8310 
8311 	default:
8312 	  break;
8313 	}
8314     }
8315 
8316   /* Look for the `:: new' and `:: delete', which also signal the
8317      beginning of a new-expression, or delete-expression,
8318      respectively.  If the next token is `::', then it might be one of
8319      these.  */
8320   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8321     {
8322       enum rid keyword;
8323 
8324       /* See if the token after the `::' is one of the keywords in
8325 	 which we're interested.  */
8326       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8327       /* If it's `new', we have a new-expression.  */
8328       if (keyword == RID_NEW)
8329 	return cp_parser_new_expression (parser);
8330       /* Similarly, for `delete'.  */
8331       else if (keyword == RID_DELETE)
8332 	return cp_parser_delete_expression (parser);
8333     }
8334 
8335   /* Look for a unary operator.  */
8336   unary_operator = cp_parser_unary_operator (token);
8337   /* The `++' and `--' operators can be handled similarly, even though
8338      they are not technically unary-operators in the grammar.  */
8339   if (unary_operator == ERROR_MARK)
8340     {
8341       if (token->type == CPP_PLUS_PLUS)
8342 	unary_operator = PREINCREMENT_EXPR;
8343       else if (token->type == CPP_MINUS_MINUS)
8344 	unary_operator = PREDECREMENT_EXPR;
8345       /* Handle the GNU address-of-label extension.  */
8346       else if (cp_parser_allow_gnu_extensions_p (parser)
8347 	       && token->type == CPP_AND_AND)
8348 	{
8349 	  tree identifier;
8350 	  tree expression;
8351 	  location_t start_loc = token->location;
8352 
8353 	  /* Consume the '&&' token.  */
8354 	  cp_lexer_consume_token (parser->lexer);
8355 	  /* Look for the identifier.  */
8356 	  location_t finish_loc
8357 	    = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8358 	  identifier = cp_parser_identifier (parser);
8359 	  /* Construct a location of the form:
8360 	       &&label
8361 	       ^~~~~~~
8362 	     with caret==start at the "&&", finish at the end of the label.  */
8363 	  location_t combined_loc
8364 	    = make_location (start_loc, start_loc, finish_loc);
8365 	  /* Create an expression representing the address.  */
8366 	  expression = finish_label_address_expr (identifier, combined_loc);
8367 	  if (cp_parser_non_integral_constant_expression (parser,
8368 							  NIC_ADDR_LABEL))
8369 	    expression = error_mark_node;
8370 	  return expression;
8371 	}
8372     }
8373   if (unary_operator != ERROR_MARK)
8374     {
8375       cp_expr cast_expression;
8376       cp_expr expression = error_mark_node;
8377       non_integral_constant non_constant_p = NIC_NONE;
8378       location_t loc = token->location;
8379       tsubst_flags_t complain = complain_flags (decltype_p);
8380 
8381       /* Consume the operator token.  */
8382       token = cp_lexer_consume_token (parser->lexer);
8383       enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8384 
8385       /* Parse the cast-expression.  */
8386       cast_expression
8387 	= cp_parser_cast_expression (parser,
8388 				     unary_operator == ADDR_EXPR,
8389 				     /*cast_p=*/false,
8390 				     /*decltype*/false,
8391 				     pidk);
8392 
8393       /* Make a location:
8394 	    OP_TOKEN  CAST_EXPRESSION
8395 	    ^~~~~~~~~~~~~~~~~~~~~~~~~
8396 	 with start==caret at the operator token, and
8397 	 extending to the end of the cast_expression.  */
8398       loc = make_location (loc, loc, cast_expression.get_finish ());
8399 
8400       /* Now, build an appropriate representation.  */
8401       switch (unary_operator)
8402 	{
8403 	case INDIRECT_REF:
8404 	  non_constant_p = NIC_STAR;
8405 	  expression = build_x_indirect_ref (loc, cast_expression,
8406 					     RO_UNARY_STAR,
8407                                              complain);
8408           /* TODO: build_x_indirect_ref does not always honor the
8409              location, so ensure it is set.  */
8410           expression.set_location (loc);
8411 	  break;
8412 
8413 	case ADDR_EXPR:
8414 	   non_constant_p = NIC_ADDR;
8415 	  /* Fall through.  */
8416 	case BIT_NOT_EXPR:
8417 	  expression = build_x_unary_op (loc, unary_operator,
8418 					 cast_expression,
8419                                          complain);
8420           /* TODO: build_x_unary_op does not always honor the location,
8421              so ensure it is set.  */
8422           expression.set_location (loc);
8423 	  break;
8424 
8425 	case PREINCREMENT_EXPR:
8426 	case PREDECREMENT_EXPR:
8427 	  non_constant_p = unary_operator == PREINCREMENT_EXPR
8428 			   ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8429 	  /* Fall through.  */
8430 	case NEGATE_EXPR:
8431 	  /* Immediately fold negation of a constant, unless the constant is 0
8432 	     (since -0 == 0) or it would overflow.  */
8433 	  if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8434 	    {
8435 	      tree stripped_expr
8436 		= tree_strip_any_location_wrapper (cast_expression);
8437 	      if (CONSTANT_CLASS_P (stripped_expr)
8438 		  && !integer_zerop (stripped_expr)
8439 		  && !TREE_OVERFLOW (stripped_expr))
8440 		{
8441 		  tree folded = fold_build1 (unary_operator,
8442 					     TREE_TYPE (stripped_expr),
8443 					     stripped_expr);
8444 		  if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8445 		    {
8446 		      expression = maybe_wrap_with_location (folded, loc);
8447 		      break;
8448 		    }
8449 		}
8450 	    }
8451 	  /* Fall through.  */
8452 	case UNARY_PLUS_EXPR:
8453 	case TRUTH_NOT_EXPR:
8454 	  expression = finish_unary_op_expr (loc, unary_operator,
8455 					     cast_expression, complain);
8456 	  break;
8457 
8458 	default:
8459 	  gcc_unreachable ();
8460 	}
8461 
8462       if (non_constant_p != NIC_NONE
8463 	  && cp_parser_non_integral_constant_expression (parser,
8464 							 non_constant_p))
8465 	expression = error_mark_node;
8466 
8467       return expression;
8468     }
8469 
8470   return cp_parser_postfix_expression (parser, address_p, cast_p,
8471                                        /*member_access_only_p=*/false,
8472 				       decltype_p,
8473 				       pidk);
8474 }
8475 
8476 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
8477    unary-operator, the corresponding tree code is returned.  */
8478 
8479 static enum tree_code
cp_parser_unary_operator(cp_token * token)8480 cp_parser_unary_operator (cp_token* token)
8481 {
8482   switch (token->type)
8483     {
8484     case CPP_MULT:
8485       return INDIRECT_REF;
8486 
8487     case CPP_AND:
8488       return ADDR_EXPR;
8489 
8490     case CPP_PLUS:
8491       return UNARY_PLUS_EXPR;
8492 
8493     case CPP_MINUS:
8494       return NEGATE_EXPR;
8495 
8496     case CPP_NOT:
8497       return TRUTH_NOT_EXPR;
8498 
8499     case CPP_COMPL:
8500       return BIT_NOT_EXPR;
8501 
8502     default:
8503       return ERROR_MARK;
8504     }
8505 }
8506 
8507 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8508    Returns a representation of the expression.  */
8509 
8510 static tree
cp_parser_has_attribute_expression(cp_parser * parser)8511 cp_parser_has_attribute_expression (cp_parser *parser)
8512 {
8513   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8514 
8515   /* Consume the __builtin_has_attribute token.  */
8516   cp_lexer_consume_token (parser->lexer);
8517 
8518   matching_parens parens;
8519   if (!parens.require_open (parser))
8520     return error_mark_node;
8521 
8522   /* Types cannot be defined in a `sizeof' expression.  Save away the
8523      old message.  */
8524   const char *saved_message = parser->type_definition_forbidden_message;
8525   const char *saved_message_arg
8526     = parser->type_definition_forbidden_message_arg;
8527   parser->type_definition_forbidden_message
8528     = G_("types may not be defined in %qs expressions");
8529   parser->type_definition_forbidden_message_arg
8530     = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
8531 
8532   /* The restrictions on constant-expressions do not apply inside
8533      sizeof expressions.  */
8534   bool saved_integral_constant_expression_p
8535     = parser->integral_constant_expression_p;
8536   bool saved_non_integral_constant_expression_p
8537     = parser->non_integral_constant_expression_p;
8538   parser->integral_constant_expression_p = false;
8539 
8540   /* Do not actually evaluate the expression.  */
8541   ++cp_unevaluated_operand;
8542   ++c_inhibit_evaluation_warnings;
8543 
8544   tree oper = NULL_TREE;
8545 
8546   /* We can't be sure yet whether we're looking at a type-id or an
8547      expression.  */
8548   cp_parser_parse_tentatively (parser);
8549 
8550   bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8551   parser->in_type_id_in_expr_p = true;
8552   /* Look for the type-id.  */
8553   oper = cp_parser_type_id (parser);
8554   parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8555 
8556   cp_parser_parse_definitely (parser);
8557 
8558   /* If the type-id production did not work out, then we must be
8559      looking at an expression.  */
8560   if (!oper || oper == error_mark_node)
8561     oper = cp_parser_assignment_expression (parser);
8562 
8563   STRIP_ANY_LOCATION_WRAPPER (oper);
8564 
8565   /* Go back to evaluating expressions.  */
8566   --cp_unevaluated_operand;
8567   --c_inhibit_evaluation_warnings;
8568 
8569   /* And restore the old one.  */
8570   parser->type_definition_forbidden_message = saved_message;
8571   parser->type_definition_forbidden_message_arg = saved_message_arg;
8572   parser->integral_constant_expression_p
8573     = saved_integral_constant_expression_p;
8574   parser->non_integral_constant_expression_p
8575     = saved_non_integral_constant_expression_p;
8576 
8577   /* Consume the comma if it's there.  */
8578   if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8579     {
8580       cp_parser_skip_to_closing_parenthesis (parser, false, false,
8581 					     /*consume_paren=*/true);
8582       return error_mark_node;
8583     }
8584 
8585   /* Parse the attribute specification.  */
8586   bool ret = false;
8587   location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8588   if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8589     {
8590       if (oper != error_mark_node)
8591 	{
8592 	  /* Fold constant expressions used in attributes first.  */
8593 	  cp_check_const_attributes (attr);
8594 
8595 	  /* Finally, see if OPER has been declared with ATTR.  */
8596 	  ret = has_attribute (atloc, oper, attr, default_conversion);
8597 	}
8598 
8599       parens.require_close (parser);
8600     }
8601   else
8602     {
8603       error_at (atloc, "expected identifier");
8604       cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8605     }
8606 
8607   /* Construct a location e.g. :
8608      __builtin_has_attribute (oper, attr)
8609      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8610      with start == caret at the start of the built-in token,
8611      and with the endpoint at the final closing paren.  */
8612   location_t finish_loc
8613     = cp_lexer_previous_token (parser->lexer)->location;
8614   location_t compound_loc
8615     = make_location (start_loc, start_loc, finish_loc);
8616 
8617   cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8618   ret_expr.set_location (compound_loc);
8619   ret_expr = ret_expr.maybe_add_location_wrapper ();
8620   return ret_expr;
8621 }
8622 
8623 /* Parse a new-expression.
8624 
8625    new-expression:
8626      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8627      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8628 
8629    Returns a representation of the expression.  */
8630 
8631 static tree
cp_parser_new_expression(cp_parser * parser)8632 cp_parser_new_expression (cp_parser* parser)
8633 {
8634   bool global_scope_p;
8635   vec<tree, va_gc> *placement;
8636   tree type;
8637   vec<tree, va_gc> *initializer;
8638   tree nelts = NULL_TREE;
8639   tree ret;
8640 
8641   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8642 
8643   /* Look for the optional `::' operator.  */
8644   global_scope_p
8645     = (cp_parser_global_scope_opt (parser,
8646 				   /*current_scope_valid_p=*/false)
8647        != NULL_TREE);
8648   /* Look for the `new' operator.  */
8649   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8650   /* There's no easy way to tell a new-placement from the
8651      `( type-id )' construct.  */
8652   cp_parser_parse_tentatively (parser);
8653   /* Look for a new-placement.  */
8654   placement = cp_parser_new_placement (parser);
8655   /* If that didn't work out, there's no new-placement.  */
8656   if (!cp_parser_parse_definitely (parser))
8657     {
8658       if (placement != NULL)
8659 	release_tree_vector (placement);
8660       placement = NULL;
8661     }
8662 
8663   /* If the next token is a `(', then we have a parenthesized
8664      type-id.  */
8665   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8666     {
8667       cp_token *token;
8668       const char *saved_message = parser->type_definition_forbidden_message;
8669 
8670       /* Consume the `('.  */
8671       matching_parens parens;
8672       parens.consume_open (parser);
8673 
8674       /* Parse the type-id.  */
8675       parser->type_definition_forbidden_message
8676 	= G_("types may not be defined in a new-expression");
8677       {
8678 	type_id_in_expr_sentinel s (parser);
8679 	type = cp_parser_type_id (parser);
8680       }
8681       parser->type_definition_forbidden_message = saved_message;
8682 
8683       /* Look for the closing `)'.  */
8684       parens.require_close (parser);
8685       token = cp_lexer_peek_token (parser->lexer);
8686       /* There should not be a direct-new-declarator in this production,
8687 	 but GCC used to allowed this, so we check and emit a sensible error
8688 	 message for this case.  */
8689       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8690 	{
8691 	  error_at (token->location,
8692 		    "array bound forbidden after parenthesized type-id");
8693 	  inform (token->location,
8694 		  "try removing the parentheses around the type-id");
8695 	  cp_parser_direct_new_declarator (parser);
8696 	}
8697     }
8698   /* Otherwise, there must be a new-type-id.  */
8699   else
8700     type = cp_parser_new_type_id (parser, &nelts);
8701 
8702   /* If the next token is a `(' or '{', then we have a new-initializer.  */
8703   cp_token *token = cp_lexer_peek_token (parser->lexer);
8704   if (token->type == CPP_OPEN_PAREN
8705       || token->type == CPP_OPEN_BRACE)
8706     initializer = cp_parser_new_initializer (parser);
8707   else
8708     initializer = NULL;
8709 
8710   /* A new-expression may not appear in an integral constant
8711      expression.  */
8712   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8713     ret = error_mark_node;
8714   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8715      of a new-type-id or type-id of a new-expression, the new-expression shall
8716      contain a new-initializer of the form ( assignment-expression )".
8717      Additionally, consistently with the spirit of DR 1467, we want to accept
8718      'new auto { 2 }' too.  */
8719   else if ((ret = type_uses_auto (type))
8720 	   && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8721 	   && (vec_safe_length (initializer) != 1
8722 	       || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8723 		   && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8724     {
8725       error_at (token->location,
8726 		"initialization of new-expression for type %<auto%> "
8727 		"requires exactly one element");
8728       ret = error_mark_node;
8729     }
8730   else
8731     {
8732       /* Construct a location e.g.:
8733            ptr = new int[100]
8734                  ^~~~~~~~~~~~
8735          with caret == start at the start of the "new" token, and the end
8736          at the end of the final token we consumed.  */
8737       cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8738       location_t end_loc = get_finish (end_tok->location);
8739       location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8740 
8741       /* Create a representation of the new-expression.  */
8742       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8743 		       tf_warning_or_error);
8744       protected_set_expr_location (ret, combined_loc);
8745     }
8746 
8747   if (placement != NULL)
8748     release_tree_vector (placement);
8749   if (initializer != NULL)
8750     release_tree_vector (initializer);
8751 
8752   return ret;
8753 }
8754 
8755 /* Parse a new-placement.
8756 
8757    new-placement:
8758      ( expression-list )
8759 
8760    Returns the same representation as for an expression-list.  */
8761 
8762 static vec<tree, va_gc> *
cp_parser_new_placement(cp_parser * parser)8763 cp_parser_new_placement (cp_parser* parser)
8764 {
8765   vec<tree, va_gc> *expression_list;
8766 
8767   /* Parse the expression-list.  */
8768   expression_list = (cp_parser_parenthesized_expression_list
8769 		     (parser, non_attr, /*cast_p=*/false,
8770 		      /*allow_expansion_p=*/true,
8771 		      /*non_constant_p=*/NULL));
8772 
8773   if (expression_list && expression_list->is_empty ())
8774     error ("expected expression-list or type-id");
8775 
8776   return expression_list;
8777 }
8778 
8779 /* Parse a new-type-id.
8780 
8781    new-type-id:
8782      type-specifier-seq new-declarator [opt]
8783 
8784    Returns the TYPE allocated.  If the new-type-id indicates an array
8785    type, *NELTS is set to the number of elements in the last array
8786    bound; the TYPE will not include the last array bound.  */
8787 
8788 static tree
cp_parser_new_type_id(cp_parser * parser,tree * nelts)8789 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8790 {
8791   cp_decl_specifier_seq type_specifier_seq;
8792   cp_declarator *new_declarator;
8793   cp_declarator *declarator;
8794   cp_declarator *outer_declarator;
8795   const char *saved_message;
8796 
8797   /* The type-specifier sequence must not contain type definitions.
8798      (It cannot contain declarations of new types either, but if they
8799      are not definitions we will catch that because they are not
8800      complete.)  */
8801   saved_message = parser->type_definition_forbidden_message;
8802   parser->type_definition_forbidden_message
8803     = G_("types may not be defined in a new-type-id");
8804   /* Parse the type-specifier-seq.  */
8805   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8806 				/*is_declaration=*/false,
8807 				/*is_trailing_return=*/false,
8808 				&type_specifier_seq);
8809   /* Restore the old message.  */
8810   parser->type_definition_forbidden_message = saved_message;
8811 
8812   if (type_specifier_seq.type == error_mark_node)
8813     return error_mark_node;
8814 
8815   /* Parse the new-declarator.  */
8816   new_declarator = cp_parser_new_declarator_opt (parser);
8817 
8818   /* Determine the number of elements in the last array dimension, if
8819      any.  */
8820   *nelts = NULL_TREE;
8821   /* Skip down to the last array dimension.  */
8822   declarator = new_declarator;
8823   outer_declarator = NULL;
8824   while (declarator && (declarator->kind == cdk_pointer
8825 			|| declarator->kind == cdk_ptrmem))
8826     {
8827       outer_declarator = declarator;
8828       declarator = declarator->declarator;
8829     }
8830   while (declarator
8831 	 && declarator->kind == cdk_array
8832 	 && declarator->declarator
8833 	 && declarator->declarator->kind == cdk_array)
8834     {
8835       outer_declarator = declarator;
8836       declarator = declarator->declarator;
8837     }
8838 
8839   if (declarator && declarator->kind == cdk_array)
8840     {
8841       *nelts = declarator->u.array.bounds;
8842       if (*nelts == error_mark_node)
8843 	*nelts = integer_one_node;
8844 
8845       if (outer_declarator)
8846 	outer_declarator->declarator = declarator->declarator;
8847       else
8848 	new_declarator = NULL;
8849     }
8850 
8851   return groktypename (&type_specifier_seq, new_declarator, false);
8852 }
8853 
8854 /* Parse an (optional) new-declarator.
8855 
8856    new-declarator:
8857      ptr-operator new-declarator [opt]
8858      direct-new-declarator
8859 
8860    Returns the declarator.  */
8861 
8862 static cp_declarator *
cp_parser_new_declarator_opt(cp_parser * parser)8863 cp_parser_new_declarator_opt (cp_parser* parser)
8864 {
8865   enum tree_code code;
8866   tree type, std_attributes = NULL_TREE;
8867   cp_cv_quals cv_quals;
8868 
8869   /* We don't know if there's a ptr-operator next, or not.  */
8870   cp_parser_parse_tentatively (parser);
8871   /* Look for a ptr-operator.  */
8872   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8873   /* If that worked, look for more new-declarators.  */
8874   if (cp_parser_parse_definitely (parser))
8875     {
8876       cp_declarator *declarator;
8877 
8878       /* Parse another optional declarator.  */
8879       declarator = cp_parser_new_declarator_opt (parser);
8880 
8881       declarator = cp_parser_make_indirect_declarator
8882 	(code, type, cv_quals, declarator, std_attributes);
8883 
8884       return declarator;
8885     }
8886 
8887   /* If the next token is a `[', there is a direct-new-declarator.  */
8888   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8889     return cp_parser_direct_new_declarator (parser);
8890 
8891   return NULL;
8892 }
8893 
8894 /* Parse a direct-new-declarator.
8895 
8896    direct-new-declarator:
8897      [ expression ]
8898      direct-new-declarator [constant-expression]
8899 
8900    */
8901 
8902 static cp_declarator *
cp_parser_direct_new_declarator(cp_parser * parser)8903 cp_parser_direct_new_declarator (cp_parser* parser)
8904 {
8905   cp_declarator *declarator = NULL;
8906 
8907   while (true)
8908     {
8909       tree expression;
8910       cp_token *token;
8911 
8912       /* Look for the opening `['.  */
8913       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8914 
8915       token = cp_lexer_peek_token (parser->lexer);
8916       expression = cp_parser_expression (parser);
8917       /* The standard requires that the expression have integral
8918 	 type.  DR 74 adds enumeration types.  We believe that the
8919 	 real intent is that these expressions be handled like the
8920 	 expression in a `switch' condition, which also allows
8921 	 classes with a single conversion to integral or
8922 	 enumeration type.  */
8923       if (!processing_template_decl)
8924 	{
8925 	  expression
8926 	    = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8927 					  expression,
8928 					  /*complain=*/true);
8929 	  if (!expression)
8930 	    {
8931 	      error_at (token->location,
8932 			"expression in new-declarator must have integral "
8933 			"or enumeration type");
8934 	      expression = error_mark_node;
8935 	    }
8936 	}
8937 
8938       /* Look for the closing `]'.  */
8939       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8940 
8941       /* Add this bound to the declarator.  */
8942       declarator = make_array_declarator (declarator, expression);
8943 
8944       /* If the next token is not a `[', then there are no more
8945 	 bounds.  */
8946       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8947 	break;
8948     }
8949 
8950   return declarator;
8951 }
8952 
8953 /* Parse a new-initializer.
8954 
8955    new-initializer:
8956      ( expression-list [opt] )
8957      braced-init-list
8958 
8959    Returns a representation of the expression-list.  */
8960 
8961 static vec<tree, va_gc> *
cp_parser_new_initializer(cp_parser * parser)8962 cp_parser_new_initializer (cp_parser* parser)
8963 {
8964   vec<tree, va_gc> *expression_list;
8965 
8966   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8967     {
8968       tree t;
8969       bool expr_non_constant_p;
8970       cp_lexer_set_source_position (parser->lexer);
8971       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8972       t = cp_parser_braced_list (parser, &expr_non_constant_p);
8973       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8974       expression_list = make_tree_vector_single (t);
8975     }
8976   else
8977     expression_list = (cp_parser_parenthesized_expression_list
8978 		       (parser, non_attr, /*cast_p=*/false,
8979 			/*allow_expansion_p=*/true,
8980 			/*non_constant_p=*/NULL));
8981 
8982   return expression_list;
8983 }
8984 
8985 /* Parse a delete-expression.
8986 
8987    delete-expression:
8988      :: [opt] delete cast-expression
8989      :: [opt] delete [ ] cast-expression
8990 
8991    Returns a representation of the expression.  */
8992 
8993 static tree
cp_parser_delete_expression(cp_parser * parser)8994 cp_parser_delete_expression (cp_parser* parser)
8995 {
8996   bool global_scope_p;
8997   bool array_p;
8998   tree expression;
8999 
9000   /* Look for the optional `::' operator.  */
9001   global_scope_p
9002     = (cp_parser_global_scope_opt (parser,
9003 				   /*current_scope_valid_p=*/false)
9004        != NULL_TREE);
9005   /* Look for the `delete' keyword.  */
9006   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9007   /* See if the array syntax is in use.  */
9008   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9009     {
9010       /* Consume the `[' token.  */
9011       cp_lexer_consume_token (parser->lexer);
9012       /* Look for the `]' token.  */
9013       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9014       /* Remember that this is the `[]' construct.  */
9015       array_p = true;
9016     }
9017   else
9018     array_p = false;
9019 
9020   /* Parse the cast-expression.  */
9021   expression = cp_parser_simple_cast_expression (parser);
9022 
9023   /* A delete-expression may not appear in an integral constant
9024      expression.  */
9025   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9026     return error_mark_node;
9027 
9028   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9029 			tf_warning_or_error);
9030 }
9031 
9032 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9033    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9034    0 otherwise.  */
9035 
9036 static int
cp_parser_tokens_start_cast_expression(cp_parser * parser)9037 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9038 {
9039   cp_token *token = cp_lexer_peek_token (parser->lexer);
9040   switch (token->type)
9041     {
9042     case CPP_COMMA:
9043     case CPP_SEMICOLON:
9044     case CPP_QUERY:
9045     case CPP_COLON:
9046     case CPP_CLOSE_SQUARE:
9047     case CPP_CLOSE_PAREN:
9048     case CPP_CLOSE_BRACE:
9049     case CPP_OPEN_BRACE:
9050     case CPP_DOT:
9051     case CPP_DOT_STAR:
9052     case CPP_DEREF:
9053     case CPP_DEREF_STAR:
9054     case CPP_DIV:
9055     case CPP_MOD:
9056     case CPP_LSHIFT:
9057     case CPP_RSHIFT:
9058     case CPP_LESS:
9059     case CPP_GREATER:
9060     case CPP_LESS_EQ:
9061     case CPP_GREATER_EQ:
9062     case CPP_EQ_EQ:
9063     case CPP_NOT_EQ:
9064     case CPP_EQ:
9065     case CPP_MULT_EQ:
9066     case CPP_DIV_EQ:
9067     case CPP_MOD_EQ:
9068     case CPP_PLUS_EQ:
9069     case CPP_MINUS_EQ:
9070     case CPP_RSHIFT_EQ:
9071     case CPP_LSHIFT_EQ:
9072     case CPP_AND_EQ:
9073     case CPP_XOR_EQ:
9074     case CPP_OR_EQ:
9075     case CPP_XOR:
9076     case CPP_OR:
9077     case CPP_OR_OR:
9078     case CPP_EOF:
9079     case CPP_ELLIPSIS:
9080       return 0;
9081 
9082     case CPP_OPEN_PAREN:
9083       /* In ((type ()) () the last () isn't a valid cast-expression,
9084 	 so the whole must be parsed as postfix-expression.  */
9085       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9086 	     != CPP_CLOSE_PAREN;
9087 
9088     case CPP_OPEN_SQUARE:
9089       /* '[' may start a primary-expression in obj-c++ and in C++11,
9090 	 as a lambda-expression, eg, '(void)[]{}'.  */
9091       if (cxx_dialect >= cxx11)
9092 	return -1;
9093       return c_dialect_objc ();
9094 
9095     case CPP_PLUS_PLUS:
9096     case CPP_MINUS_MINUS:
9097       /* '++' and '--' may or may not start a cast-expression:
9098 
9099 	 struct T { void operator++(int); };
9100 	 void f() { (T())++; }
9101 
9102 	 vs
9103 
9104 	 int a;
9105 	 (int)++a;  */
9106       return -1;
9107 
9108     default:
9109       return 1;
9110     }
9111 }
9112 
9113 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9114    in the order: const_cast, static_cast, reinterpret_cast.
9115 
9116    Don't suggest dynamic_cast.
9117 
9118    Return the first legal cast kind found, or NULL otherwise.  */
9119 
9120 static const char *
get_cast_suggestion(tree dst_type,tree orig_expr)9121 get_cast_suggestion (tree dst_type, tree orig_expr)
9122 {
9123   tree trial;
9124 
9125   /* Reuse the parser logic by attempting to build the various kinds of
9126      cast, with "complain" disabled.
9127      Identify the first such cast that is valid.  */
9128 
9129   /* Don't attempt to run such logic within template processing.  */
9130   if (processing_template_decl)
9131     return NULL;
9132 
9133   /* First try const_cast.  */
9134   trial = build_const_cast (dst_type, orig_expr, tf_none);
9135   if (trial != error_mark_node)
9136     return "const_cast";
9137 
9138   /* If that fails, try static_cast.  */
9139   trial = build_static_cast (dst_type, orig_expr, tf_none);
9140   if (trial != error_mark_node)
9141     return "static_cast";
9142 
9143   /* Finally, try reinterpret_cast.  */
9144   trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9145   if (trial != error_mark_node)
9146     return "reinterpret_cast";
9147 
9148   /* No such cast possible.  */
9149   return NULL;
9150 }
9151 
9152 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9153    suggesting how to convert a C-style cast of the form:
9154 
9155      (DST_TYPE)ORIG_EXPR
9156 
9157    to a C++-style cast.
9158 
9159    The primary range of RICHLOC is asssumed to be that of the original
9160    expression.  OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9161    of the parens in the C-style cast.  */
9162 
9163 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)9164 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9165 		      location_t close_paren_loc, tree orig_expr,
9166 		      tree dst_type)
9167 {
9168   /* This function is non-trivial, so bail out now if the warning isn't
9169      going to be emitted.  */
9170   if (!warn_old_style_cast)
9171     return;
9172 
9173   /* Try to find a legal C++ cast, trying them in order:
9174      const_cast, static_cast, reinterpret_cast.  */
9175   const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9176   if (!cast_suggestion)
9177     return;
9178 
9179   /* Replace the open paren with "CAST_SUGGESTION<".  */
9180   pretty_printer pp;
9181   pp_printf (&pp, "%s<", cast_suggestion);
9182   rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9183 
9184   /* Replace the close paren with "> (".  */
9185   rich_loc->add_fixit_replace (close_paren_loc, "> (");
9186 
9187   /* Add a closing paren after the expr (the primary range of RICH_LOC).  */
9188   rich_loc->add_fixit_insert_after (")");
9189 }
9190 
9191 
9192 /* Parse a cast-expression.
9193 
9194    cast-expression:
9195      unary-expression
9196      ( type-id ) cast-expression
9197 
9198    ADDRESS_P is true iff the unary-expression is appearing as the
9199    operand of the `&' operator.   CAST_P is true if this expression is
9200    the target of a cast.
9201 
9202    Returns a representation of the expression.  */
9203 
9204 static cp_expr
cp_parser_cast_expression(cp_parser * parser,bool address_p,bool cast_p,bool decltype_p,cp_id_kind * pidk)9205 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9206 			   bool decltype_p, cp_id_kind * pidk)
9207 {
9208   /* If it's a `(', then we might be looking at a cast.  */
9209   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9210     {
9211       tree type = NULL_TREE;
9212       cp_expr expr (NULL_TREE);
9213       int cast_expression = 0;
9214       const char *saved_message;
9215 
9216       /* There's no way to know yet whether or not this is a cast.
9217 	 For example, `(int (3))' is a unary-expression, while `(int)
9218 	 3' is a cast.  So, we resort to parsing tentatively.  */
9219       cp_parser_parse_tentatively (parser);
9220       /* Types may not be defined in a cast.  */
9221       saved_message = parser->type_definition_forbidden_message;
9222       parser->type_definition_forbidden_message
9223 	= G_("types may not be defined in casts");
9224       /* Consume the `('.  */
9225       matching_parens parens;
9226       cp_token *open_paren = parens.consume_open (parser);
9227       location_t open_paren_loc = open_paren->location;
9228       location_t close_paren_loc = UNKNOWN_LOCATION;
9229 
9230       /* A very tricky bit is that `(struct S) { 3 }' is a
9231 	 compound-literal (which we permit in C++ as an extension).
9232 	 But, that construct is not a cast-expression -- it is a
9233 	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
9234 	 is legal; if the compound-literal were a cast-expression,
9235 	 you'd need an extra set of parentheses.)  But, if we parse
9236 	 the type-id, and it happens to be a class-specifier, then we
9237 	 will commit to the parse at that point, because we cannot
9238 	 undo the action that is done when creating a new class.  So,
9239 	 then we cannot back up and do a postfix-expression.
9240 
9241 	 Another tricky case is the following (c++/29234):
9242 
9243          struct S { void operator () (); };
9244 
9245          void foo ()
9246          {
9247            ( S()() );
9248          }
9249 
9250 	 As a type-id we parse the parenthesized S()() as a function
9251 	 returning a function, groktypename complains and we cannot
9252 	 back up in this case either.
9253 
9254 	 Therefore, we scan ahead to the closing `)', and check to see
9255 	 if the tokens after the `)' can start a cast-expression.  Otherwise
9256 	 we are dealing with an unary-expression, a postfix-expression
9257 	 or something else.
9258 
9259 	 Yet another tricky case, in C++11, is the following (c++/54891):
9260 
9261 	 (void)[]{};
9262 
9263          The issue is that usually, besides the case of lambda-expressions,
9264 	 the parenthesized type-id cannot be followed by '[', and, eg, we
9265 	 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9266 	 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9267 	 we don't commit, we try a cast-expression, then an unary-expression.
9268 
9269 	 Save tokens so that we can put them back.  */
9270       cp_lexer_save_tokens (parser->lexer);
9271 
9272       /* We may be looking at a cast-expression.  */
9273       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9274 						 /*consume_paren=*/true))
9275 	cast_expression
9276 	  = cp_parser_tokens_start_cast_expression (parser);
9277 
9278       /* Roll back the tokens we skipped.  */
9279       cp_lexer_rollback_tokens (parser->lexer);
9280       /* If we aren't looking at a cast-expression, simulate an error so
9281 	 that the call to cp_parser_error_occurred below returns true.  */
9282       if (!cast_expression)
9283 	cp_parser_simulate_error (parser);
9284       else
9285 	{
9286 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9287 	  parser->in_type_id_in_expr_p = true;
9288 	  /* Look for the type-id.  */
9289 	  type = cp_parser_type_id (parser);
9290 	  /* Look for the closing `)'.  */
9291 	  cp_token *close_paren = parens.require_close (parser);
9292 	  if (close_paren)
9293 	    close_paren_loc = close_paren->location;
9294 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9295 	}
9296 
9297       /* Restore the saved message.  */
9298       parser->type_definition_forbidden_message = saved_message;
9299 
9300       /* At this point this can only be either a cast or a
9301 	 parenthesized ctor such as `(T ())' that looks like a cast to
9302 	 function returning T.  */
9303       if (!cp_parser_error_occurred (parser))
9304 	{
9305 	  /* Only commit if the cast-expression doesn't start with
9306 	     '++', '--', or '[' in C++11.  */
9307 	  if (cast_expression > 0)
9308 	    cp_parser_commit_to_topmost_tentative_parse (parser);
9309 
9310 	  expr = cp_parser_cast_expression (parser,
9311 					    /*address_p=*/false,
9312 					    /*cast_p=*/true,
9313 					    /*decltype_p=*/false,
9314 					    pidk);
9315 
9316 	  if (cp_parser_parse_definitely (parser))
9317 	    {
9318 	      /* Warn about old-style casts, if so requested.  */
9319 	      if (warn_old_style_cast
9320 		  && !in_system_header_at (input_location)
9321 		  && !VOID_TYPE_P (type)
9322 		  && current_lang_name != lang_name_c)
9323 		{
9324 		  gcc_rich_location rich_loc (input_location);
9325 		  maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9326 					expr, type);
9327 		  warning_at (&rich_loc, OPT_Wold_style_cast,
9328 			      "use of old-style cast to %q#T", type);
9329 		}
9330 
9331 	      /* Only type conversions to integral or enumeration types
9332 		 can be used in constant-expressions.  */
9333 	      if (!cast_valid_in_integral_constant_expression_p (type)
9334 		  && cp_parser_non_integral_constant_expression (parser,
9335 								 NIC_CAST))
9336 		return error_mark_node;
9337 
9338 	      /* Perform the cast.  */
9339 	      /* Make a location:
9340 		   (TYPE) EXPR
9341 		   ^~~~~~~~~~~
9342 		 with start==caret at the open paren, extending to the
9343 		 end of "expr".  */
9344 	      location_t cast_loc = make_location (open_paren_loc,
9345 						   open_paren_loc,
9346 						   expr.get_finish ());
9347 	      expr = build_c_cast (cast_loc, type, expr);
9348 	      return expr;
9349 	    }
9350 	}
9351       else
9352         cp_parser_abort_tentative_parse (parser);
9353     }
9354 
9355   /* If we get here, then it's not a cast, so it must be a
9356      unary-expression.  */
9357   return cp_parser_unary_expression (parser, pidk, address_p,
9358 				     cast_p, decltype_p);
9359 }
9360 
9361 /* Parse a binary expression of the general form:
9362 
9363    pm-expression:
9364      cast-expression
9365      pm-expression .* cast-expression
9366      pm-expression ->* cast-expression
9367 
9368    multiplicative-expression:
9369      pm-expression
9370      multiplicative-expression * pm-expression
9371      multiplicative-expression / pm-expression
9372      multiplicative-expression % pm-expression
9373 
9374    additive-expression:
9375      multiplicative-expression
9376      additive-expression + multiplicative-expression
9377      additive-expression - multiplicative-expression
9378 
9379    shift-expression:
9380      additive-expression
9381      shift-expression << additive-expression
9382      shift-expression >> additive-expression
9383 
9384    relational-expression:
9385      shift-expression
9386      relational-expression < shift-expression
9387      relational-expression > shift-expression
9388      relational-expression <= shift-expression
9389      relational-expression >= shift-expression
9390 
9391   GNU Extension:
9392 
9393    relational-expression:
9394      relational-expression <? shift-expression
9395      relational-expression >? shift-expression
9396 
9397    equality-expression:
9398      relational-expression
9399      equality-expression == relational-expression
9400      equality-expression != relational-expression
9401 
9402    and-expression:
9403      equality-expression
9404      and-expression & equality-expression
9405 
9406    exclusive-or-expression:
9407      and-expression
9408      exclusive-or-expression ^ and-expression
9409 
9410    inclusive-or-expression:
9411      exclusive-or-expression
9412      inclusive-or-expression | exclusive-or-expression
9413 
9414    logical-and-expression:
9415      inclusive-or-expression
9416      logical-and-expression && inclusive-or-expression
9417 
9418    logical-or-expression:
9419      logical-and-expression
9420      logical-or-expression || logical-and-expression
9421 
9422    All these are implemented with a single function like:
9423 
9424    binary-expression:
9425      simple-cast-expression
9426      binary-expression <token> binary-expression
9427 
9428    CAST_P is true if this expression is the target of a cast.
9429 
9430    The binops_by_token map is used to get the tree codes for each <token> type.
9431    binary-expressions are associated according to a precedence table.  */
9432 
9433 #define TOKEN_PRECEDENCE(token)				     \
9434 (((token->type == CPP_GREATER				     \
9435    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9436   && !parser->greater_than_is_operator_p)		     \
9437  ? PREC_NOT_OPERATOR					     \
9438  : binops_by_token[token->type].prec)
9439 
9440 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)9441 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9442 			     bool no_toplevel_fold_p,
9443 			     bool decltype_p,
9444 			     enum cp_parser_prec prec,
9445 			     cp_id_kind * pidk)
9446 {
9447   cp_parser_expression_stack stack;
9448   cp_parser_expression_stack_entry *sp = &stack[0];
9449   cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
9450   cp_parser_expression_stack_entry current;
9451   cp_expr rhs;
9452   cp_token *token;
9453   enum tree_code rhs_type;
9454   enum cp_parser_prec new_prec, lookahead_prec;
9455   tree overload;
9456 
9457   /* Parse the first expression.  */
9458   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9459 		      ? TRUTH_NOT_EXPR : ERROR_MARK);
9460   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9461 					   cast_p, decltype_p, pidk);
9462   current.prec = prec;
9463 
9464   if (cp_parser_error_occurred (parser))
9465     return error_mark_node;
9466 
9467   for (;;)
9468     {
9469       /* Get an operator token.  */
9470       token = cp_lexer_peek_token (parser->lexer);
9471 
9472       if (warn_cxx11_compat
9473           && token->type == CPP_RSHIFT
9474           && !parser->greater_than_is_operator_p)
9475         {
9476           if (warning_at (token->location, OPT_Wc__11_compat,
9477 			  "%<>>%> operator is treated"
9478 			  " as two right angle brackets in C++11"))
9479 	    inform (token->location,
9480 		    "suggest parentheses around %<>>%> expression");
9481         }
9482 
9483       new_prec = TOKEN_PRECEDENCE (token);
9484       if (new_prec != PREC_NOT_OPERATOR
9485 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9486 	/* This is a fold-expression; handle it later.  */
9487 	new_prec = PREC_NOT_OPERATOR;
9488 
9489       /* Popping an entry off the stack means we completed a subexpression:
9490 	 - either we found a token which is not an operator (`>' where it is not
9491 	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9492 	   will happen repeatedly;
9493 	 - or, we found an operator which has lower priority.  This is the case
9494 	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
9495 	   parsing `3 * 4'.  */
9496       if (new_prec <= current.prec)
9497 	{
9498 	  if (sp == stack)
9499 	    break;
9500 	  else
9501 	    goto pop;
9502 	}
9503 
9504      get_rhs:
9505       current.tree_type = binops_by_token[token->type].tree_type;
9506       current.loc = token->location;
9507 
9508       /* We used the operator token.  */
9509       cp_lexer_consume_token (parser->lexer);
9510 
9511       /* For "false && x" or "true || x", x will never be executed;
9512 	 disable warnings while evaluating it.  */
9513       if ((current.tree_type == TRUTH_ANDIF_EXPR
9514 	   && cp_fully_fold (current.lhs) == truthvalue_false_node)
9515 	  || (current.tree_type == TRUTH_ORIF_EXPR
9516 	      && cp_fully_fold (current.lhs) == truthvalue_true_node))
9517 	{
9518 	  disable_warnings_sp = sp;
9519 	  ++c_inhibit_evaluation_warnings;
9520 	}
9521 
9522       /* Extract another operand.  It may be the RHS of this expression
9523 	 or the LHS of a new, higher priority expression.  */
9524       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9525 		  ? TRUTH_NOT_EXPR : ERROR_MARK);
9526       rhs = cp_parser_simple_cast_expression (parser);
9527 
9528       /* Get another operator token.  Look up its precedence to avoid
9529 	 building a useless (immediately popped) stack entry for common
9530 	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
9531       token = cp_lexer_peek_token (parser->lexer);
9532       lookahead_prec = TOKEN_PRECEDENCE (token);
9533       if (lookahead_prec != PREC_NOT_OPERATOR
9534 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9535 	lookahead_prec = PREC_NOT_OPERATOR;
9536       if (lookahead_prec > new_prec)
9537 	{
9538 	  /* ... and prepare to parse the RHS of the new, higher priority
9539 	     expression.  Since precedence levels on the stack are
9540 	     monotonically increasing, we do not have to care about
9541 	     stack overflows.  */
9542 	  *sp = current;
9543 	  ++sp;
9544 	  current.lhs = rhs;
9545 	  current.lhs_type = rhs_type;
9546 	  current.prec = new_prec;
9547 	  new_prec = lookahead_prec;
9548 	  goto get_rhs;
9549 
9550 	 pop:
9551 	  lookahead_prec = new_prec;
9552 	  /* If the stack is not empty, we have parsed into LHS the right side
9553 	     (`4' in the example above) of an expression we had suspended.
9554 	     We can use the information on the stack to recover the LHS (`3')
9555 	     from the stack together with the tree code (`MULT_EXPR'), and
9556 	     the precedence of the higher level subexpression
9557 	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
9558 	     which will be used to actually build the additive expression.  */
9559 	  rhs = current.lhs;
9560 	  rhs_type = current.lhs_type;
9561 	  --sp;
9562 	  current = *sp;
9563 	}
9564 
9565       /* Undo the disabling of warnings done above.  */
9566       if (sp == disable_warnings_sp)
9567 	{
9568 	  disable_warnings_sp = NULL;
9569 	  --c_inhibit_evaluation_warnings;
9570 	}
9571 
9572       if (warn_logical_not_paren
9573 	  && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9574 	  && current.lhs_type == TRUTH_NOT_EXPR
9575 	  /* Avoid warning for !!x == y.  */
9576 	  && (TREE_CODE (current.lhs) != NE_EXPR
9577 	      || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9578 	  && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9579 	      || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9580 		  /* Avoid warning for !b == y where b is boolean.  */
9581 		  && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9582 		      || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9583 			  != BOOLEAN_TYPE))))
9584 	  /* Avoid warning for !!b == y where b is boolean.  */
9585 	  && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9586 	      || TREE_TYPE (current.lhs) == NULL_TREE
9587 	      || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9588 	warn_logical_not_parentheses (current.loc, current.tree_type,
9589 				      current.lhs, maybe_constant_value (rhs));
9590 
9591       overload = NULL;
9592 
9593       location_t combined_loc = make_location (current.loc,
9594 					       current.lhs.get_start (),
9595 					       rhs.get_finish ());
9596 
9597       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9598 	 ERROR_MARK for everything that is not a binary expression.
9599 	 This makes warn_about_parentheses miss some warnings that
9600 	 involve unary operators.  For unary expressions we should
9601 	 pass the correct tree_code unless the unary expression was
9602 	 surrounded by parentheses.
9603       */
9604       if (no_toplevel_fold_p
9605 	  && lookahead_prec <= current.prec
9606 	  && sp == stack)
9607 	{
9608 	  if (current.lhs == error_mark_node || rhs == error_mark_node)
9609 	    current.lhs = error_mark_node;
9610 	  else
9611 	    {
9612 	      current.lhs
9613 		= build_min (current.tree_type,
9614 			     TREE_CODE_CLASS (current.tree_type)
9615 			     == tcc_comparison
9616 			     ? boolean_type_node : TREE_TYPE (current.lhs),
9617 			     current.lhs.get_value (), rhs.get_value ());
9618 	      SET_EXPR_LOCATION (current.lhs, combined_loc);
9619 	    }
9620 	}
9621       else
9622         {
9623 	  op_location_t op_loc (current.loc, combined_loc);
9624 	  current.lhs = build_x_binary_op (op_loc, current.tree_type,
9625                                            current.lhs, current.lhs_type,
9626                                            rhs, rhs_type, &overload,
9627                                            complain_flags (decltype_p));
9628           /* TODO: build_x_binary_op doesn't always honor the location.  */
9629           current.lhs.set_location (combined_loc);
9630         }
9631       current.lhs_type = current.tree_type;
9632 
9633       /* If the binary operator required the use of an overloaded operator,
9634 	 then this expression cannot be an integral constant-expression.
9635 	 An overloaded operator can be used even if both operands are
9636 	 otherwise permissible in an integral constant-expression if at
9637 	 least one of the operands is of enumeration type.  */
9638 
9639       if (overload
9640 	  && cp_parser_non_integral_constant_expression (parser,
9641 							 NIC_OVERLOADED))
9642 	return error_mark_node;
9643     }
9644 
9645   return current.lhs;
9646 }
9647 
9648 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)9649 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9650 			     bool no_toplevel_fold_p,
9651 			     enum cp_parser_prec prec,
9652 			     cp_id_kind * pidk)
9653 {
9654   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9655 				      /*decltype*/false, prec, pidk);
9656 }
9657 
9658 /* Parse the `? expression : assignment-expression' part of a
9659    conditional-expression.  The LOGICAL_OR_EXPR is the
9660    logical-or-expression that started the conditional-expression.
9661    Returns a representation of the entire conditional-expression.
9662 
9663    This routine is used by cp_parser_assignment_expression.
9664 
9665      ? expression : assignment-expression
9666 
9667    GNU Extensions:
9668 
9669      ? : assignment-expression */
9670 
9671 static tree
cp_parser_question_colon_clause(cp_parser * parser,cp_expr logical_or_expr)9672 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9673 {
9674   tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9675   cp_expr assignment_expr;
9676   struct cp_token *token;
9677   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9678 
9679   /* Consume the `?' token.  */
9680   cp_lexer_consume_token (parser->lexer);
9681   token = cp_lexer_peek_token (parser->lexer);
9682   if (cp_parser_allow_gnu_extensions_p (parser)
9683       && token->type == CPP_COLON)
9684     {
9685       pedwarn (token->location, OPT_Wpedantic,
9686                "ISO C++ does not allow ?: with omitted middle operand");
9687       /* Implicit true clause.  */
9688       expr = NULL_TREE;
9689       c_inhibit_evaluation_warnings +=
9690 	folded_logical_or_expr == truthvalue_true_node;
9691       warn_for_omitted_condop (token->location, logical_or_expr);
9692     }
9693   else
9694     {
9695       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9696       parser->colon_corrects_to_scope_p = false;
9697       /* Parse the expression.  */
9698       c_inhibit_evaluation_warnings +=
9699 	folded_logical_or_expr == truthvalue_false_node;
9700       expr = cp_parser_expression (parser);
9701       c_inhibit_evaluation_warnings +=
9702 	((folded_logical_or_expr == truthvalue_true_node)
9703 	 - (folded_logical_or_expr == truthvalue_false_node));
9704       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9705     }
9706 
9707   /* The next token should be a `:'.  */
9708   cp_parser_require (parser, CPP_COLON, RT_COLON);
9709   /* Parse the assignment-expression.  */
9710   assignment_expr = cp_parser_assignment_expression (parser);
9711   c_inhibit_evaluation_warnings -=
9712     folded_logical_or_expr == truthvalue_true_node;
9713 
9714   /* Make a location:
9715        LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9716        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9717      with the caret at the "?", ranging from the start of
9718      the logical_or_expr to the end of the assignment_expr.  */
9719   loc = make_location (loc,
9720 		       logical_or_expr.get_start (),
9721 		       assignment_expr.get_finish ());
9722 
9723   /* Build the conditional-expression.  */
9724   return build_x_conditional_expr (loc, logical_or_expr,
9725 				   expr,
9726 				   assignment_expr,
9727                                    tf_warning_or_error);
9728 }
9729 
9730 /* Parse an assignment-expression.
9731 
9732    assignment-expression:
9733      conditional-expression
9734      logical-or-expression assignment-operator assignment_expression
9735      throw-expression
9736 
9737    CAST_P is true if this expression is the target of a cast.
9738    DECLTYPE_P is true if this expression is the operand of decltype.
9739 
9740    Returns a representation for the expression.  */
9741 
9742 static cp_expr
cp_parser_assignment_expression(cp_parser * parser,cp_id_kind * pidk,bool cast_p,bool decltype_p)9743 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9744 				 bool cast_p, bool decltype_p)
9745 {
9746   cp_expr expr;
9747 
9748   /* If the next token is the `throw' keyword, then we're looking at
9749      a throw-expression.  */
9750   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9751     expr = cp_parser_throw_expression (parser);
9752   /* Otherwise, it must be that we are looking at a
9753      logical-or-expression.  */
9754   else
9755     {
9756       /* Parse the binary expressions (logical-or-expression).  */
9757       expr = cp_parser_binary_expression (parser, cast_p, false,
9758 					  decltype_p,
9759 					  PREC_NOT_OPERATOR, pidk);
9760       /* If the next token is a `?' then we're actually looking at a
9761 	 conditional-expression.  */
9762       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9763 	return cp_parser_question_colon_clause (parser, expr);
9764       else
9765 	{
9766 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9767 
9768 	  /* If it's an assignment-operator, we're using the second
9769 	     production.  */
9770 	  enum tree_code assignment_operator
9771 	    = cp_parser_assignment_operator_opt (parser);
9772 	  if (assignment_operator != ERROR_MARK)
9773 	    {
9774 	      bool non_constant_p;
9775 
9776 	      /* Parse the right-hand side of the assignment.  */
9777 	      cp_expr rhs = cp_parser_initializer_clause (parser,
9778 							  &non_constant_p);
9779 
9780 	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9781 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9782 
9783 	      /* An assignment may not appear in a
9784 		 constant-expression.  */
9785 	      if (cp_parser_non_integral_constant_expression (parser,
9786 							      NIC_ASSIGNMENT))
9787 		return error_mark_node;
9788 	      /* Build the assignment expression.  Its default
9789 		 location:
9790 		   LHS = RHS
9791 		   ~~~~^~~~~
9792 		 is the location of the '=' token as the
9793 		 caret, ranging from the start of the lhs to the
9794 		 end of the rhs.  */
9795 	      loc = make_location (loc,
9796 				   expr.get_start (),
9797 				   rhs.get_finish ());
9798 	      expr = build_x_modify_expr (loc, expr,
9799 					  assignment_operator,
9800 					  rhs,
9801 					  complain_flags (decltype_p));
9802               /* TODO: build_x_modify_expr doesn't honor the location,
9803                  so we must set it here.  */
9804               expr.set_location (loc);
9805 	    }
9806 	}
9807     }
9808 
9809   return expr;
9810 }
9811 
9812 /* Parse an (optional) assignment-operator.
9813 
9814    assignment-operator: one of
9815      = *= /= %= += -= >>= <<= &= ^= |=
9816 
9817    GNU Extension:
9818 
9819    assignment-operator: one of
9820      <?= >?=
9821 
9822    If the next token is an assignment operator, the corresponding tree
9823    code is returned, and the token is consumed.  For example, for
9824    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
9825    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
9826    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
9827    operator, ERROR_MARK is returned.  */
9828 
9829 static enum tree_code
cp_parser_assignment_operator_opt(cp_parser * parser)9830 cp_parser_assignment_operator_opt (cp_parser* parser)
9831 {
9832   enum tree_code op;
9833   cp_token *token;
9834 
9835   /* Peek at the next token.  */
9836   token = cp_lexer_peek_token (parser->lexer);
9837 
9838   switch (token->type)
9839     {
9840     case CPP_EQ:
9841       op = NOP_EXPR;
9842       break;
9843 
9844     case CPP_MULT_EQ:
9845       op = MULT_EXPR;
9846       break;
9847 
9848     case CPP_DIV_EQ:
9849       op = TRUNC_DIV_EXPR;
9850       break;
9851 
9852     case CPP_MOD_EQ:
9853       op = TRUNC_MOD_EXPR;
9854       break;
9855 
9856     case CPP_PLUS_EQ:
9857       op = PLUS_EXPR;
9858       break;
9859 
9860     case CPP_MINUS_EQ:
9861       op = MINUS_EXPR;
9862       break;
9863 
9864     case CPP_RSHIFT_EQ:
9865       op = RSHIFT_EXPR;
9866       break;
9867 
9868     case CPP_LSHIFT_EQ:
9869       op = LSHIFT_EXPR;
9870       break;
9871 
9872     case CPP_AND_EQ:
9873       op = BIT_AND_EXPR;
9874       break;
9875 
9876     case CPP_XOR_EQ:
9877       op = BIT_XOR_EXPR;
9878       break;
9879 
9880     case CPP_OR_EQ:
9881       op = BIT_IOR_EXPR;
9882       break;
9883 
9884     default:
9885       /* Nothing else is an assignment operator.  */
9886       op = ERROR_MARK;
9887     }
9888 
9889   /* An operator followed by ... is a fold-expression, handled elsewhere.  */
9890   if (op != ERROR_MARK
9891       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9892     op = ERROR_MARK;
9893 
9894   /* If it was an assignment operator, consume it.  */
9895   if (op != ERROR_MARK)
9896     cp_lexer_consume_token (parser->lexer);
9897 
9898   return op;
9899 }
9900 
9901 /* Parse an expression.
9902 
9903    expression:
9904      assignment-expression
9905      expression , assignment-expression
9906 
9907    CAST_P is true if this expression is the target of a cast.
9908    DECLTYPE_P is true if this expression is the immediate operand of decltype,
9909      except possibly parenthesized or on the RHS of a comma (N3276).
9910 
9911    Returns a representation of the expression.  */
9912 
9913 static cp_expr
cp_parser_expression(cp_parser * parser,cp_id_kind * pidk,bool cast_p,bool decltype_p)9914 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9915 		      bool cast_p, bool decltype_p)
9916 {
9917   cp_expr expression = NULL_TREE;
9918   location_t loc = UNKNOWN_LOCATION;
9919 
9920   while (true)
9921     {
9922       cp_expr assignment_expression;
9923 
9924       /* Parse the next assignment-expression.  */
9925       assignment_expression
9926 	= cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9927 
9928       /* We don't create a temporary for a call that is the immediate operand
9929 	 of decltype or on the RHS of a comma.  But when we see a comma, we
9930 	 need to create a temporary for a call on the LHS.  */
9931       if (decltype_p && !processing_template_decl
9932 	  && TREE_CODE (assignment_expression) == CALL_EXPR
9933 	  && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9934 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9935 	assignment_expression
9936 	  = build_cplus_new (TREE_TYPE (assignment_expression),
9937 			     assignment_expression, tf_warning_or_error);
9938 
9939       /* If this is the first assignment-expression, we can just
9940 	 save it away.  */
9941       if (!expression)
9942 	expression = assignment_expression;
9943       else
9944 	{
9945 	  /* Create a location with caret at the comma, ranging
9946 	     from the start of the LHS to the end of the RHS.  */
9947 	  loc = make_location (loc,
9948 			       expression.get_start (),
9949 			       assignment_expression.get_finish ());
9950 	  expression = build_x_compound_expr (loc, expression,
9951 					      assignment_expression,
9952 					      complain_flags (decltype_p));
9953 	  expression.set_location (loc);
9954 	}
9955       /* If the next token is not a comma, or we're in a fold-expression, then
9956 	 we are done with the expression.  */
9957       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9958 	  || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9959 	break;
9960       /* Consume the `,'.  */
9961       loc = cp_lexer_peek_token (parser->lexer)->location;
9962       cp_lexer_consume_token (parser->lexer);
9963       /* A comma operator cannot appear in a constant-expression.  */
9964       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9965 	expression = error_mark_node;
9966     }
9967 
9968   return expression;
9969 }
9970 
9971 /* Parse a constant-expression.
9972 
9973    constant-expression:
9974      conditional-expression
9975 
9976   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9977   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
9978   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
9979   is false, NON_CONSTANT_P should be NULL.  If STRICT_P is true,
9980   only parse a conditional-expression, otherwise parse an
9981   assignment-expression.  See below for rationale.  */
9982 
9983 static cp_expr
cp_parser_constant_expression(cp_parser * parser,bool allow_non_constant_p,bool * non_constant_p,bool strict_p)9984 cp_parser_constant_expression (cp_parser* parser,
9985 			       bool allow_non_constant_p,
9986 			       bool *non_constant_p,
9987 			       bool strict_p)
9988 {
9989   bool saved_integral_constant_expression_p;
9990   bool saved_allow_non_integral_constant_expression_p;
9991   bool saved_non_integral_constant_expression_p;
9992   cp_expr expression;
9993 
9994   /* It might seem that we could simply parse the
9995      conditional-expression, and then check to see if it were
9996      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
9997      one that the compiler can figure out is constant, possibly after
9998      doing some simplifications or optimizations.  The standard has a
9999      precise definition of constant-expression, and we must honor
10000      that, even though it is somewhat more restrictive.
10001 
10002      For example:
10003 
10004        int i[(2, 3)];
10005 
10006      is not a legal declaration, because `(2, 3)' is not a
10007      constant-expression.  The `,' operator is forbidden in a
10008      constant-expression.  However, GCC's constant-folding machinery
10009      will fold this operation to an INTEGER_CST for `3'.  */
10010 
10011   /* Save the old settings.  */
10012   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10013   saved_allow_non_integral_constant_expression_p
10014     = parser->allow_non_integral_constant_expression_p;
10015   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10016   /* We are now parsing a constant-expression.  */
10017   parser->integral_constant_expression_p = true;
10018   parser->allow_non_integral_constant_expression_p
10019     = (allow_non_constant_p || cxx_dialect >= cxx11);
10020   parser->non_integral_constant_expression_p = false;
10021   /* Although the grammar says "conditional-expression", when not STRICT_P,
10022      we parse an "assignment-expression", which also permits
10023      "throw-expression" and the use of assignment operators.  In the case
10024      that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10025      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
10026      actually essential that we look for an assignment-expression.
10027      For example, cp_parser_initializer_clauses uses this function to
10028      determine whether a particular assignment-expression is in fact
10029      constant.  */
10030   if (strict_p)
10031     {
10032       /* Parse the binary expressions (logical-or-expression).  */
10033       expression = cp_parser_binary_expression (parser, false, false, false,
10034 						PREC_NOT_OPERATOR, NULL);
10035       /* If the next token is a `?' then we're actually looking at
10036 	 a conditional-expression; otherwise we're done.  */
10037       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10038 	expression = cp_parser_question_colon_clause (parser, expression);
10039     }
10040   else
10041     expression = cp_parser_assignment_expression (parser);
10042   /* Restore the old settings.  */
10043   parser->integral_constant_expression_p
10044     = saved_integral_constant_expression_p;
10045   parser->allow_non_integral_constant_expression_p
10046     = saved_allow_non_integral_constant_expression_p;
10047   if (cxx_dialect >= cxx11)
10048     {
10049       /* Require an rvalue constant expression here; that's what our
10050 	 callers expect.  Reference constant expressions are handled
10051 	 separately in e.g. cp_parser_template_argument.  */
10052       tree decay = expression;
10053       if (TREE_TYPE (expression)
10054 	  && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10055 	decay = build_address (expression);
10056       bool is_const = potential_rvalue_constant_expression (decay);
10057       parser->non_integral_constant_expression_p = !is_const;
10058       if (!is_const && !allow_non_constant_p)
10059 	require_potential_rvalue_constant_expression (decay);
10060     }
10061   if (allow_non_constant_p)
10062     *non_constant_p = parser->non_integral_constant_expression_p;
10063   parser->non_integral_constant_expression_p
10064     = saved_non_integral_constant_expression_p;
10065 
10066   return expression;
10067 }
10068 
10069 /* Parse __builtin_offsetof.
10070 
10071    offsetof-expression:
10072      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10073 
10074    offsetof-member-designator:
10075      id-expression
10076      | offsetof-member-designator "." id-expression
10077      | offsetof-member-designator "[" expression "]"
10078      | offsetof-member-designator "->" id-expression  */
10079 
10080 static cp_expr
cp_parser_builtin_offsetof(cp_parser * parser)10081 cp_parser_builtin_offsetof (cp_parser *parser)
10082 {
10083   int save_ice_p, save_non_ice_p;
10084   tree type;
10085   cp_expr expr;
10086   cp_id_kind dummy;
10087   cp_token *token;
10088   location_t finish_loc;
10089 
10090   /* We're about to accept non-integral-constant things, but will
10091      definitely yield an integral constant expression.  Save and
10092      restore these values around our local parsing.  */
10093   save_ice_p = parser->integral_constant_expression_p;
10094   save_non_ice_p = parser->non_integral_constant_expression_p;
10095 
10096   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10097 
10098   /* Consume the "__builtin_offsetof" token.  */
10099   cp_lexer_consume_token (parser->lexer);
10100   /* Consume the opening `('.  */
10101   matching_parens parens;
10102   parens.require_open (parser);
10103   /* Parse the type-id.  */
10104   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10105   {
10106     const char *saved_message = parser->type_definition_forbidden_message;
10107     parser->type_definition_forbidden_message
10108       = G_("types may not be defined within %<__builtin_offsetof%>");
10109     type = cp_parser_type_id (parser);
10110     parser->type_definition_forbidden_message = saved_message;
10111   }
10112   /* Look for the `,'.  */
10113   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10114   token = cp_lexer_peek_token (parser->lexer);
10115 
10116   /* Build the (type *)null that begins the traditional offsetof macro.  */
10117   tree object_ptr
10118     = build_static_cast (build_pointer_type (type), null_pointer_node,
10119 			 tf_warning_or_error);
10120 
10121   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
10122   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10123 						 true, &dummy, token->location);
10124   while (true)
10125     {
10126       token = cp_lexer_peek_token (parser->lexer);
10127       switch (token->type)
10128 	{
10129 	case CPP_OPEN_SQUARE:
10130 	  /* offsetof-member-designator "[" expression "]" */
10131 	  expr = cp_parser_postfix_open_square_expression (parser, expr,
10132 							   true, false);
10133 	  break;
10134 
10135 	case CPP_DEREF:
10136 	  /* offsetof-member-designator "->" identifier */
10137 	  expr = grok_array_decl (token->location, expr,
10138 				  integer_zero_node, false);
10139 	  /* FALLTHRU */
10140 
10141 	case CPP_DOT:
10142 	  /* offsetof-member-designator "." identifier */
10143 	  cp_lexer_consume_token (parser->lexer);
10144 	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10145 							 expr, true, &dummy,
10146 							 token->location);
10147 	  break;
10148 
10149 	case CPP_CLOSE_PAREN:
10150 	  /* Consume the ")" token.  */
10151 	  finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10152 	  cp_lexer_consume_token (parser->lexer);
10153 	  goto success;
10154 
10155 	default:
10156 	  /* Error.  We know the following require will fail, but
10157 	     that gives the proper error message.  */
10158 	  parens.require_close (parser);
10159 	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10160 	  expr = error_mark_node;
10161 	  goto failure;
10162 	}
10163     }
10164 
10165  success:
10166   /* Make a location of the form:
10167        __builtin_offsetof (struct s, f)
10168        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10169      with caret at the type-id, ranging from the start of the
10170      "_builtin_offsetof" token to the close paren.  */
10171   loc = make_location (loc, start_loc, finish_loc);
10172   /* The result will be an INTEGER_CST, so we need to explicitly
10173      preserve the location.  */
10174   expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10175 
10176  failure:
10177   parser->integral_constant_expression_p = save_ice_p;
10178   parser->non_integral_constant_expression_p = save_non_ice_p;
10179 
10180   expr = expr.maybe_add_location_wrapper ();
10181   return expr;
10182 }
10183 
10184 /* Parse a trait expression.
10185 
10186    Returns a representation of the expression, the underlying type
10187    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
10188 
10189 static cp_expr
cp_parser_trait_expr(cp_parser * parser,enum rid keyword)10190 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10191 {
10192   cp_trait_kind kind;
10193   tree type1, type2 = NULL_TREE;
10194   bool binary = false;
10195   bool variadic = false;
10196 
10197   switch (keyword)
10198     {
10199     case RID_HAS_NOTHROW_ASSIGN:
10200       kind = CPTK_HAS_NOTHROW_ASSIGN;
10201       break;
10202     case RID_HAS_NOTHROW_CONSTRUCTOR:
10203       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10204       break;
10205     case RID_HAS_NOTHROW_COPY:
10206       kind = CPTK_HAS_NOTHROW_COPY;
10207       break;
10208     case RID_HAS_TRIVIAL_ASSIGN:
10209       kind = CPTK_HAS_TRIVIAL_ASSIGN;
10210       break;
10211     case RID_HAS_TRIVIAL_CONSTRUCTOR:
10212       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10213       break;
10214     case RID_HAS_TRIVIAL_COPY:
10215       kind = CPTK_HAS_TRIVIAL_COPY;
10216       break;
10217     case RID_HAS_TRIVIAL_DESTRUCTOR:
10218       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10219       break;
10220     case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10221       kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10222       break;
10223     case RID_HAS_VIRTUAL_DESTRUCTOR:
10224       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10225       break;
10226     case RID_IS_ABSTRACT:
10227       kind = CPTK_IS_ABSTRACT;
10228       break;
10229     case RID_IS_AGGREGATE:
10230       kind = CPTK_IS_AGGREGATE;
10231       break;
10232     case RID_IS_BASE_OF:
10233       kind = CPTK_IS_BASE_OF;
10234       binary = true;
10235       break;
10236     case RID_IS_CLASS:
10237       kind = CPTK_IS_CLASS;
10238       break;
10239     case RID_IS_EMPTY:
10240       kind = CPTK_IS_EMPTY;
10241       break;
10242     case RID_IS_ENUM:
10243       kind = CPTK_IS_ENUM;
10244       break;
10245     case RID_IS_FINAL:
10246       kind = CPTK_IS_FINAL;
10247       break;
10248     case RID_IS_LITERAL_TYPE:
10249       kind = CPTK_IS_LITERAL_TYPE;
10250       break;
10251     case RID_IS_POD:
10252       kind = CPTK_IS_POD;
10253       break;
10254     case RID_IS_POLYMORPHIC:
10255       kind = CPTK_IS_POLYMORPHIC;
10256       break;
10257     case RID_IS_SAME_AS:
10258       kind = CPTK_IS_SAME_AS;
10259       binary = true;
10260       break;
10261     case RID_IS_STD_LAYOUT:
10262       kind = CPTK_IS_STD_LAYOUT;
10263       break;
10264     case RID_IS_TRIVIAL:
10265       kind = CPTK_IS_TRIVIAL;
10266       break;
10267     case RID_IS_TRIVIALLY_ASSIGNABLE:
10268       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10269       binary = true;
10270       break;
10271     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10272       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10273       variadic = true;
10274       break;
10275     case RID_IS_TRIVIALLY_COPYABLE:
10276       kind = CPTK_IS_TRIVIALLY_COPYABLE;
10277       break;
10278     case RID_IS_UNION:
10279       kind = CPTK_IS_UNION;
10280       break;
10281     case RID_UNDERLYING_TYPE:
10282       kind = CPTK_UNDERLYING_TYPE;
10283       break;
10284     case RID_BASES:
10285       kind = CPTK_BASES;
10286       break;
10287     case RID_DIRECT_BASES:
10288       kind = CPTK_DIRECT_BASES;
10289       break;
10290     case RID_IS_ASSIGNABLE:
10291       kind = CPTK_IS_ASSIGNABLE;
10292       binary = true;
10293       break;
10294     case RID_IS_CONSTRUCTIBLE:
10295       kind = CPTK_IS_CONSTRUCTIBLE;
10296       variadic = true;
10297       break;
10298     default:
10299       gcc_unreachable ();
10300     }
10301 
10302   /* Get location of initial token.  */
10303   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10304 
10305   /* Consume the token.  */
10306   cp_lexer_consume_token (parser->lexer);
10307 
10308   matching_parens parens;
10309   parens.require_open (parser);
10310 
10311   {
10312     type_id_in_expr_sentinel s (parser);
10313     type1 = cp_parser_type_id (parser);
10314   }
10315 
10316   if (type1 == error_mark_node)
10317     return error_mark_node;
10318 
10319   if (binary)
10320     {
10321       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10322 
10323       {
10324 	type_id_in_expr_sentinel s (parser);
10325 	type2 = cp_parser_type_id (parser);
10326       }
10327 
10328       if (type2 == error_mark_node)
10329 	return error_mark_node;
10330     }
10331   else if (variadic)
10332     {
10333       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10334 	{
10335 	  cp_lexer_consume_token (parser->lexer);
10336 	  tree elt = cp_parser_type_id (parser);
10337 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10338 	    {
10339 	      cp_lexer_consume_token (parser->lexer);
10340 	      elt = make_pack_expansion (elt);
10341 	    }
10342 	  if (elt == error_mark_node)
10343 	    return error_mark_node;
10344 	  type2 = tree_cons (NULL_TREE, elt, type2);
10345 	}
10346     }
10347 
10348   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10349   parens.require_close (parser);
10350 
10351   /* Construct a location of the form:
10352        __is_trivially_copyable(_Tp)
10353        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10354      with start == caret, finishing at the close-paren.  */
10355   location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10356 
10357   /* Complete the trait expression, which may mean either processing
10358      the trait expr now or saving it for template instantiation.  */
10359   switch (kind)
10360     {
10361     case CPTK_UNDERLYING_TYPE:
10362       return cp_expr (finish_underlying_type (type1), trait_loc);
10363     case CPTK_BASES:
10364       return cp_expr (finish_bases (type1, false), trait_loc);
10365     case CPTK_DIRECT_BASES:
10366       return cp_expr (finish_bases (type1, true), trait_loc);
10367     default:
10368       return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10369     }
10370 }
10371 
10372 /* Parse a lambda expression.
10373 
10374    lambda-expression:
10375      lambda-introducer lambda-declarator [opt] compound-statement
10376 
10377    Returns a representation of the expression.  */
10378 
10379 static cp_expr
cp_parser_lambda_expression(cp_parser * parser)10380 cp_parser_lambda_expression (cp_parser* parser)
10381 {
10382   tree lambda_expr = build_lambda_expr ();
10383   tree type;
10384   bool ok = true;
10385   cp_token *token = cp_lexer_peek_token (parser->lexer);
10386   cp_token_position start = 0;
10387 
10388   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10389 
10390   if (cxx_dialect >= cxx2a)
10391     /* C++20 allows lambdas in unevaluated context.  */;
10392   else if (cp_unevaluated_operand)
10393     {
10394       if (!token->error_reported)
10395 	{
10396 	  error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10397 		    "lambda-expression in unevaluated context"
10398 		    " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10399 	  token->error_reported = true;
10400 	}
10401       ok = false;
10402     }
10403   else if (parser->in_template_argument_list_p || processing_template_parmlist)
10404     {
10405       if (!token->error_reported)
10406 	{
10407 	  error_at (token->location, "lambda-expression in template-argument"
10408 		    " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10409 	  token->error_reported = true;
10410 	}
10411       ok = false;
10412     }
10413 
10414   /* We may be in the middle of deferred access check.  Disable
10415      it now.  */
10416   push_deferring_access_checks (dk_no_deferred);
10417 
10418   cp_parser_lambda_introducer (parser, lambda_expr);
10419   if (cp_parser_error_occurred (parser))
10420     return error_mark_node;
10421 
10422   type = begin_lambda_type (lambda_expr);
10423   if (type == error_mark_node)
10424     return error_mark_node;
10425 
10426   record_lambda_scope (lambda_expr);
10427 
10428   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
10429   determine_visibility (TYPE_NAME (type));
10430 
10431   /* Now that we've started the type, add the capture fields for any
10432      explicit captures.  */
10433   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10434 
10435   {
10436     /* Inside the class, surrounding template-parameter-lists do not apply.  */
10437     unsigned int saved_num_template_parameter_lists
10438         = parser->num_template_parameter_lists;
10439     unsigned char in_statement = parser->in_statement;
10440     bool in_switch_statement_p = parser->in_switch_statement_p;
10441     bool fully_implicit_function_template_p
10442         = parser->fully_implicit_function_template_p;
10443     tree implicit_template_parms = parser->implicit_template_parms;
10444     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10445     bool auto_is_implicit_function_template_parm_p
10446         = parser->auto_is_implicit_function_template_parm_p;
10447 
10448     parser->num_template_parameter_lists = 0;
10449     parser->in_statement = 0;
10450     parser->in_switch_statement_p = false;
10451     parser->fully_implicit_function_template_p = false;
10452     parser->implicit_template_parms = 0;
10453     parser->implicit_template_scope = 0;
10454     parser->auto_is_implicit_function_template_parm_p = false;
10455 
10456     /* The body of a lambda in a discarded statement is not discarded.  */
10457     bool discarded = in_discarded_stmt;
10458     in_discarded_stmt = 0;
10459 
10460     /* By virtue of defining a local class, a lambda expression has access to
10461        the private variables of enclosing classes.  */
10462 
10463     if (cp_parser_start_tentative_firewall (parser))
10464       start = token;
10465 
10466     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10467 
10468     if (ok && cp_parser_error_occurred (parser))
10469       ok = false;
10470 
10471     if (ok)
10472       {
10473 	cp_parser_lambda_body (parser, lambda_expr);
10474       }
10475     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10476       {
10477 	if (cp_parser_skip_to_closing_brace (parser))
10478 	  cp_lexer_consume_token (parser->lexer);
10479       }
10480 
10481     /* The capture list was built up in reverse order; fix that now.  */
10482     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10483       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10484 
10485     if (ok)
10486       maybe_add_lambda_conv_op (type);
10487 
10488     type = finish_struct (type, /*attributes=*/NULL_TREE);
10489 
10490     in_discarded_stmt = discarded;
10491 
10492     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10493     parser->in_statement = in_statement;
10494     parser->in_switch_statement_p = in_switch_statement_p;
10495     parser->fully_implicit_function_template_p
10496 	= fully_implicit_function_template_p;
10497     parser->implicit_template_parms = implicit_template_parms;
10498     parser->implicit_template_scope = implicit_template_scope;
10499     parser->auto_is_implicit_function_template_parm_p
10500 	= auto_is_implicit_function_template_parm_p;
10501   }
10502 
10503   /* This field is only used during parsing of the lambda.  */
10504   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10505 
10506   /* This lambda shouldn't have any proxies left at this point.  */
10507   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10508   /* And now that we're done, push proxies for an enclosing lambda.  */
10509   insert_pending_capture_proxies ();
10510 
10511   /* Update the lambda expression to a range.  */
10512   cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10513   LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10514 						      token->location,
10515 						      end_tok->location);
10516 
10517   if (ok)
10518     lambda_expr = build_lambda_object (lambda_expr);
10519   else
10520     lambda_expr = error_mark_node;
10521 
10522   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10523 
10524   pop_deferring_access_checks ();
10525 
10526   return lambda_expr;
10527 }
10528 
10529 /* Parse the beginning of a lambda expression.
10530 
10531    lambda-introducer:
10532      [ lambda-capture [opt] ]
10533 
10534    LAMBDA_EXPR is the current representation of the lambda expression.  */
10535 
10536 static void
cp_parser_lambda_introducer(cp_parser * parser,tree lambda_expr)10537 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10538 {
10539   /* Need commas after the first capture.  */
10540   bool first = true;
10541 
10542   /* Eat the leading `['.  */
10543   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10544 
10545   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
10546   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10547       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10548     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10549   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10550     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10551 
10552   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10553     {
10554       cp_lexer_consume_token (parser->lexer);
10555       first = false;
10556 
10557       if (!(at_function_scope_p () || parsing_nsdmi ()))
10558 	error ("non-local lambda expression cannot have a capture-default");
10559     }
10560 
10561   hash_set<tree, true> ids;
10562   tree first_capture_id = NULL_TREE;
10563   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10564     {
10565       cp_token* capture_token;
10566       tree capture_id;
10567       tree capture_init_expr;
10568       cp_id_kind idk = CP_ID_KIND_NONE;
10569       bool explicit_init_p = false;
10570 
10571       enum capture_kind_type
10572       {
10573 	BY_COPY,
10574 	BY_REFERENCE
10575       };
10576       enum capture_kind_type capture_kind = BY_COPY;
10577 
10578       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10579 	{
10580 	  error ("expected end of capture-list");
10581 	  return;
10582 	}
10583 
10584       if (first)
10585 	first = false;
10586       else
10587 	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10588 
10589       /* Possibly capture `this'.  */
10590       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10591 	{
10592 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10593 	  if (cxx_dialect < cxx2a
10594 	      && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10595 	    pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10596 		     "with by-copy capture default");
10597 	  cp_lexer_consume_token (parser->lexer);
10598 	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10599 	    pedwarn (input_location, 0,
10600 		     "already captured %qD in lambda expression",
10601 		     this_identifier);
10602 	  else
10603 	    add_capture (lambda_expr, /*id=*/this_identifier,
10604 			 /*initializer=*/finish_this_expr (),
10605 			 /*by_reference_p=*/true, explicit_init_p);
10606 	  continue;
10607 	}
10608 
10609       /* Possibly capture `*this'.  */
10610       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10611 	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10612 	{
10613 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10614 	  if (cxx_dialect < cxx17)
10615 	    pedwarn (loc, 0, "%<*this%> capture only available with "
10616 			     "%<-std=c++17%> or %<-std=gnu++17%>");
10617 	  cp_lexer_consume_token (parser->lexer);
10618 	  cp_lexer_consume_token (parser->lexer);
10619 	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10620 	    pedwarn (input_location, 0,
10621 		     "already captured %qD in lambda expression",
10622 		     this_identifier);
10623 	  else
10624 	    add_capture (lambda_expr, /*id=*/this_identifier,
10625 			 /*initializer=*/finish_this_expr (),
10626 			 /*by_reference_p=*/false, explicit_init_p);
10627 	  continue;
10628 	}
10629 
10630       bool init_pack_expansion = false;
10631       location_t ellipsis_loc = UNKNOWN_LOCATION;
10632       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10633 	{
10634 	  ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
10635 	  if (cxx_dialect < cxx2a)
10636 	    pedwarn (ellipsis_loc, 0, "pack init-capture only available with "
10637 		     "%<-std=c++2a%> or %<-std=gnu++2a%>");
10638 	  cp_lexer_consume_token (parser->lexer);
10639 	  init_pack_expansion = true;
10640 	}
10641 
10642       /* Remember whether we want to capture as a reference or not.  */
10643       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10644 	{
10645 	  capture_kind = BY_REFERENCE;
10646 	  cp_lexer_consume_token (parser->lexer);
10647 	}
10648 
10649       /* Get the identifier.  */
10650       capture_token = cp_lexer_peek_token (parser->lexer);
10651       capture_id = cp_parser_identifier (parser);
10652 
10653       if (capture_id == error_mark_node)
10654 	/* Would be nice to have a cp_parser_skip_to_closing_x for general
10655            delimiters, but I modified this to stop on unnested ']' as well.  It
10656            was already changed to stop on unnested '}', so the
10657            "closing_parenthesis" name is no more misleading with my change.  */
10658 	{
10659 	  cp_parser_skip_to_closing_parenthesis (parser,
10660 						 /*recovering=*/true,
10661 						 /*or_comma=*/true,
10662 						 /*consume_paren=*/true);
10663 	  break;
10664 	}
10665 
10666       /* Find the initializer for this capture.  */
10667       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10668 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10669 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10670 	{
10671 	  bool direct, non_constant;
10672 	  /* An explicit initializer exists.  */
10673 	  if (cxx_dialect < cxx14)
10674 	    pedwarn (input_location, 0,
10675 		     "lambda capture initializers "
10676 		     "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10677 	  capture_init_expr = cp_parser_initializer (parser, &direct,
10678 						     &non_constant, true);
10679 	  explicit_init_p = true;
10680 	  if (capture_init_expr == NULL_TREE)
10681 	    {
10682 	      error ("empty initializer for lambda init-capture");
10683 	      capture_init_expr = error_mark_node;
10684 	    }
10685 	  if (init_pack_expansion)
10686 	    capture_init_expr = make_pack_expansion (capture_init_expr);
10687 	}
10688       else
10689 	{
10690 	  const char* error_msg;
10691 
10692 	  /* Turn the identifier into an id-expression.  */
10693 	  capture_init_expr
10694 	    = cp_parser_lookup_name_simple (parser, capture_id,
10695 					    capture_token->location);
10696 
10697 	  if (capture_init_expr == error_mark_node)
10698 	    {
10699 	      unqualified_name_lookup_error (capture_id);
10700 	      continue;
10701 	    }
10702 	  else if (!VAR_P (capture_init_expr)
10703 		   && TREE_CODE (capture_init_expr) != PARM_DECL)
10704 	    {
10705 	      error_at (capture_token->location,
10706 			"capture of non-variable %qE",
10707 			capture_init_expr);
10708 	      if (DECL_P (capture_init_expr))
10709 		inform (DECL_SOURCE_LOCATION (capture_init_expr),
10710 			"%q#D declared here", capture_init_expr);
10711 	      continue;
10712 	    }
10713 	  if (VAR_P (capture_init_expr)
10714 	      && decl_storage_duration (capture_init_expr) != dk_auto)
10715 	    {
10716 	      if (pedwarn (capture_token->location, 0, "capture of variable "
10717 			   "%qD with non-automatic storage duration",
10718 			   capture_init_expr))
10719 		inform (DECL_SOURCE_LOCATION (capture_init_expr),
10720 			"%q#D declared here", capture_init_expr);
10721 	      continue;
10722 	    }
10723 
10724 	  capture_init_expr
10725             = finish_id_expression
10726                 (capture_id,
10727 		 capture_init_expr,
10728                  parser->scope,
10729                  &idk,
10730                  /*integral_constant_expression_p=*/false,
10731                  /*allow_non_integral_constant_expression_p=*/false,
10732                  /*non_integral_constant_expression_p=*/NULL,
10733                  /*template_p=*/false,
10734                  /*done=*/true,
10735                  /*address_p=*/false,
10736                  /*template_arg_p=*/false,
10737                  &error_msg,
10738                  capture_token->location);
10739 
10740 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10741 	    {
10742 	      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10743 	      cp_lexer_consume_token (parser->lexer);
10744 	      capture_init_expr = make_pack_expansion (capture_init_expr);
10745 	      if (init_pack_expansion)
10746 		{
10747 		  /* If what follows is an initializer, the second '...' is
10748 		     invalid.  But for cases like [...xs...], the first one
10749 		     is invalid.  */
10750 		  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10751 		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10752 		      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10753 		    ellipsis_loc = loc;
10754 		  error_at (ellipsis_loc, "too many %<...%> in lambda capture");
10755 		  continue;
10756 		}
10757 	    }
10758 	}
10759 
10760       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10761 	  && !explicit_init_p)
10762 	{
10763 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10764 	      && capture_kind == BY_COPY)
10765 	    pedwarn (capture_token->location, 0, "explicit by-copy capture "
10766 		     "of %qD redundant with by-copy capture default",
10767 		     capture_id);
10768 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10769 	      && capture_kind == BY_REFERENCE)
10770 	    pedwarn (capture_token->location, 0, "explicit by-reference "
10771 		     "capture of %qD redundant with by-reference capture "
10772 		     "default", capture_id);
10773 	}
10774 
10775       /* Check for duplicates.
10776 	 Optimize for the zero or one explicit captures cases and only create
10777 	 the hash_set after adding second capture.  */
10778       bool found = false;
10779       if (ids.elements ())
10780 	found = ids.add (capture_id);
10781       else if (first_capture_id == NULL_TREE)
10782 	first_capture_id = capture_id;
10783       else if (capture_id == first_capture_id)
10784 	found = true;
10785       else
10786 	{
10787 	  ids.add (first_capture_id);
10788 	  ids.add (capture_id);
10789 	}
10790       if (found)
10791 	pedwarn (input_location, 0,
10792 		 "already captured %qD in lambda expression", capture_id);
10793       else
10794 	add_capture (lambda_expr, capture_id, capture_init_expr,
10795 		     /*by_reference_p=*/capture_kind == BY_REFERENCE,
10796 		     explicit_init_p);
10797 
10798       /* If there is any qualification still in effect, clear it
10799 	 now; we will be starting fresh with the next capture.  */
10800       parser->scope = NULL_TREE;
10801       parser->qualifying_scope = NULL_TREE;
10802       parser->object_scope = NULL_TREE;
10803     }
10804 
10805   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10806 }
10807 
10808 /* Parse the (optional) middle of a lambda expression.
10809 
10810    lambda-declarator:
10811      < template-parameter-list [opt] >
10812      ( parameter-declaration-clause [opt] )
10813        attribute-specifier [opt]
10814        decl-specifier-seq [opt]
10815        exception-specification [opt]
10816        lambda-return-type-clause [opt]
10817 
10818    LAMBDA_EXPR is the current representation of the lambda expression.  */
10819 
10820 static bool
cp_parser_lambda_declarator_opt(cp_parser * parser,tree lambda_expr)10821 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10822 {
10823   /* 5.1.1.4 of the standard says:
10824        If a lambda-expression does not include a lambda-declarator, it is as if
10825        the lambda-declarator were ().
10826      This means an empty parameter list, no attributes, and no exception
10827      specification.  */
10828   tree param_list = void_list_node;
10829   tree std_attrs = NULL_TREE;
10830   tree gnu_attrs = NULL_TREE;
10831   tree exception_spec = NULL_TREE;
10832   tree template_param_list = NULL_TREE;
10833   tree tx_qual = NULL_TREE;
10834   tree return_type = NULL_TREE;
10835   cp_decl_specifier_seq lambda_specs;
10836   clear_decl_specs (&lambda_specs);
10837 
10838   /* The template-parameter-list is optional, but must begin with
10839      an opening angle if present.  */
10840   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10841     {
10842       if (cxx_dialect < cxx14)
10843 	pedwarn (parser->lexer->next_token->location, 0,
10844 		 "lambda templates are only available with "
10845 		 "%<-std=c++14%> or %<-std=gnu++14%>");
10846       else if (cxx_dialect < cxx2a)
10847 	pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10848 		 "lambda templates are only available with "
10849 		 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10850 
10851       cp_lexer_consume_token (parser->lexer);
10852 
10853       template_param_list = cp_parser_template_parameter_list (parser);
10854 
10855       cp_parser_skip_to_end_of_template_parameter_list (parser);
10856 
10857       /* We just processed one more parameter list.  */
10858       ++parser->num_template_parameter_lists;
10859     }
10860 
10861   /* Committee discussion supports allowing attributes here.  */
10862   lambda_specs.attributes = cp_parser_attributes_opt (parser);
10863 
10864   /* The parameter-declaration-clause is optional (unless
10865      template-parameter-list was given), but must begin with an
10866      opening parenthesis if present.  */
10867   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10868     {
10869       matching_parens parens;
10870       parens.consume_open (parser);
10871 
10872       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10873 
10874       /* Parse parameters.  */
10875       param_list
10876 	= cp_parser_parameter_declaration_clause
10877 	    (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10878 
10879       /* Default arguments shall not be specified in the
10880 	 parameter-declaration-clause of a lambda-declarator.  */
10881       if (cxx_dialect < cxx14)
10882 	for (tree t = param_list; t; t = TREE_CHAIN (t))
10883 	  if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10884 	    pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10885 		     "default argument specified for lambda parameter");
10886 
10887       parens.require_close (parser);
10888 
10889       /* In the decl-specifier-seq of the lambda-declarator, each
10890 	 decl-specifier shall either be mutable or constexpr.  */
10891       int declares_class_or_enum;
10892       if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
10893 	  && !cp_next_tokens_can_be_gnu_attribute_p (parser))
10894 	cp_parser_decl_specifier_seq (parser,
10895 				      CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10896 				      &lambda_specs, &declares_class_or_enum);
10897       if (lambda_specs.storage_class == sc_mutable)
10898 	{
10899 	  LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10900 	  if (lambda_specs.conflicting_specifiers_p)
10901 	    error_at (lambda_specs.locations[ds_storage_class],
10902 		      "duplicate %<mutable%>");
10903 	}
10904 
10905       tx_qual = cp_parser_tx_qualifier_opt (parser);
10906 
10907       /* Parse optional exception specification.  */
10908       exception_spec = cp_parser_exception_specification_opt (parser);
10909 
10910       /* GCC 8 accepted attributes here, and this is the place for standard
10911 	 C++11 attributes that appertain to the function type.  */
10912       if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10913 	gnu_attrs = cp_parser_gnu_attributes_opt (parser);
10914       else
10915 	std_attrs = cp_parser_std_attribute_spec_seq (parser);
10916 
10917       /* Parse optional trailing return type.  */
10918       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10919         {
10920           cp_lexer_consume_token (parser->lexer);
10921           return_type = cp_parser_trailing_type_id (parser);
10922         }
10923 
10924       /* Also allow GNU attributes at the very end of the declaration, the
10925 	 usual place for GNU attributes.  */
10926       if (cp_next_tokens_can_be_gnu_attribute_p (parser))
10927 	gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
10928 
10929       /* The function parameters must be in scope all the way until after the
10930          trailing-return-type in case of decltype.  */
10931       pop_bindings_and_leave_scope ();
10932     }
10933   else if (template_param_list != NULL_TREE) // generate diagnostic
10934     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10935 
10936   /* Create the function call operator.
10937 
10938      Messing with declarators like this is no uglier than building up the
10939      FUNCTION_DECL by hand, and this is less likely to get out of sync with
10940      other code.  */
10941   {
10942     cp_decl_specifier_seq return_type_specs;
10943     cp_declarator* declarator;
10944     tree fco;
10945     int quals;
10946     void *p;
10947 
10948     clear_decl_specs (&return_type_specs);
10949     return_type_specs.type = make_auto ();
10950 
10951     if (lambda_specs.locations[ds_constexpr])
10952       {
10953 	if (cxx_dialect >= cxx17)
10954 	  return_type_specs.locations[ds_constexpr]
10955 	    = lambda_specs.locations[ds_constexpr];
10956 	else
10957 	  error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10958 		    "lambda only available with %<-std=c++17%> or "
10959 		    "%<-std=gnu++17%>");
10960       }
10961 
10962     p = obstack_alloc (&declarator_obstack, 0);
10963 
10964     declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10965 				     LAMBDA_EXPR_LOCATION (lambda_expr));
10966 
10967     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10968 	     ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10969     declarator = make_call_declarator (declarator, param_list, quals,
10970 				       VIRT_SPEC_UNSPECIFIED,
10971                                        REF_QUAL_NONE,
10972 				       tx_qual,
10973 				       exception_spec,
10974                                        return_type,
10975                                        /*requires_clause*/NULL_TREE);
10976     declarator->std_attributes = std_attrs;
10977 
10978     fco = grokmethod (&return_type_specs,
10979 		      declarator,
10980 		      chainon (gnu_attrs, lambda_specs.attributes));
10981     if (fco != error_mark_node)
10982       {
10983 	DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10984 	DECL_ARTIFICIAL (fco) = 1;
10985 	/* Give the object parameter a different name.  */
10986 	DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10987 	DECL_LAMBDA_FUNCTION (fco) = 1;
10988       }
10989     if (template_param_list)
10990       {
10991 	fco = finish_member_template_decl (fco);
10992 	finish_template_decl (template_param_list);
10993 	--parser->num_template_parameter_lists;
10994       }
10995     else if (parser->fully_implicit_function_template_p)
10996       fco = finish_fully_implicit_template (parser, fco);
10997 
10998     finish_member_declaration (fco);
10999 
11000     obstack_free (&declarator_obstack, p);
11001 
11002     return (fco != error_mark_node);
11003   }
11004 }
11005 
11006 /* Parse the body of a lambda expression, which is simply
11007 
11008    compound-statement
11009 
11010    but which requires special handling.
11011    LAMBDA_EXPR is the current representation of the lambda expression.  */
11012 
11013 static void
cp_parser_lambda_body(cp_parser * parser,tree lambda_expr)11014 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11015 {
11016   bool nested = (current_function_decl != NULL_TREE);
11017   unsigned char local_variables_forbidden_p
11018     = parser->local_variables_forbidden_p;
11019   bool in_function_body = parser->in_function_body;
11020 
11021   /* The body of a lambda-expression is not a subexpression of the enclosing
11022      expression.  */
11023   cp_evaluated ev;
11024 
11025   if (nested)
11026     push_function_context ();
11027   else
11028     /* Still increment function_depth so that we don't GC in the
11029        middle of an expression.  */
11030     ++function_depth;
11031 
11032   vec<tree> omp_privatization_save;
11033   save_omp_privatization_clauses (omp_privatization_save);
11034   /* Clear this in case we're in the middle of a default argument.  */
11035   parser->local_variables_forbidden_p = 0;
11036   parser->in_function_body = true;
11037 
11038   {
11039     local_specialization_stack s (lss_copy);
11040     tree fco = lambda_function (lambda_expr);
11041     tree body = start_lambda_function (fco, lambda_expr);
11042     matching_braces braces;
11043 
11044     if (braces.require_open (parser))
11045       {
11046 	tree compound_stmt = begin_compound_stmt (0);
11047 
11048 	/* Originally C++11 required us to peek for 'return expr'; and
11049 	   process it specially here to deduce the return type.  N3638
11050 	   removed the need for that.  */
11051 
11052 	while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11053 	  cp_parser_label_declaration (parser);
11054 	cp_parser_statement_seq_opt (parser, NULL_TREE);
11055 	braces.require_close (parser);
11056 
11057 	finish_compound_stmt (compound_stmt);
11058       }
11059 
11060     finish_lambda_function (body);
11061   }
11062 
11063   restore_omp_privatization_clauses (omp_privatization_save);
11064   parser->local_variables_forbidden_p = local_variables_forbidden_p;
11065   parser->in_function_body = in_function_body;
11066   if (nested)
11067     pop_function_context();
11068   else
11069     --function_depth;
11070 }
11071 
11072 /* Statements [gram.stmt.stmt]  */
11073 
11074 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
11075 
11076 static void
add_debug_begin_stmt(location_t loc)11077 add_debug_begin_stmt (location_t loc)
11078 {
11079   if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11080     return;
11081   if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11082     /* A concept is never expanded normally.  */
11083     return;
11084 
11085   tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11086   SET_EXPR_LOCATION (stmt, loc);
11087   add_stmt (stmt);
11088 }
11089 
11090 /* Parse a statement.
11091 
11092    statement:
11093      labeled-statement
11094      expression-statement
11095      compound-statement
11096      selection-statement
11097      iteration-statement
11098      jump-statement
11099      declaration-statement
11100      try-block
11101 
11102   C++11:
11103 
11104   statement:
11105     labeled-statement
11106     attribute-specifier-seq (opt) expression-statement
11107     attribute-specifier-seq (opt) compound-statement
11108     attribute-specifier-seq (opt) selection-statement
11109     attribute-specifier-seq (opt) iteration-statement
11110     attribute-specifier-seq (opt) jump-statement
11111     declaration-statement
11112     attribute-specifier-seq (opt) try-block
11113 
11114   init-statement:
11115     expression-statement
11116     simple-declaration
11117 
11118   TM Extension:
11119 
11120    statement:
11121      atomic-statement
11122 
11123   IN_COMPOUND is true when the statement is nested inside a
11124   cp_parser_compound_statement; this matters for certain pragmas.
11125 
11126   If IF_P is not NULL, *IF_P is set to indicate whether the statement
11127   is a (possibly labeled) if statement which is not enclosed in braces
11128   and has an else clause.  This is used to implement -Wparentheses.
11129 
11130   CHAIN is a vector of if-else-if conditions.  */
11131 
11132 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)11133 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11134 		     bool in_compound, bool *if_p, vec<tree> *chain,
11135 		     location_t *loc_after_labels)
11136 {
11137   tree statement, std_attrs = NULL_TREE;
11138   cp_token *token;
11139   location_t statement_location, attrs_loc;
11140 
11141  restart:
11142   if (if_p != NULL)
11143     *if_p = false;
11144   /* There is no statement yet.  */
11145   statement = NULL_TREE;
11146 
11147   saved_token_sentinel saved_tokens (parser->lexer);
11148   attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11149   if (c_dialect_objc ())
11150     /* In obj-c++, seeing '[[' might be the either the beginning of
11151        c++11 attributes, or a nested objc-message-expression.  So
11152        let's parse the c++11 attributes tentatively.  */
11153     cp_parser_parse_tentatively (parser);
11154   std_attrs = cp_parser_std_attribute_spec_seq (parser);
11155   if (std_attrs)
11156     {
11157       location_t end_loc
11158 	= cp_lexer_previous_token (parser->lexer)->location;
11159       attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11160     }
11161   if (c_dialect_objc ())
11162     {
11163       if (!cp_parser_parse_definitely (parser))
11164 	std_attrs = NULL_TREE;
11165     }
11166 
11167   /* Peek at the next token.  */
11168   token = cp_lexer_peek_token (parser->lexer);
11169   /* Remember the location of the first token in the statement.  */
11170   cp_token *statement_token = token;
11171   statement_location = token->location;
11172   add_debug_begin_stmt (statement_location);
11173   /* If this is a keyword, then that will often determine what kind of
11174      statement we have.  */
11175   if (token->type == CPP_KEYWORD)
11176     {
11177       enum rid keyword = token->keyword;
11178 
11179       switch (keyword)
11180 	{
11181 	case RID_CASE:
11182 	case RID_DEFAULT:
11183 	  /* Looks like a labeled-statement with a case label.
11184 	     Parse the label, and then use tail recursion to parse
11185 	     the statement.  */
11186 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
11187 	  in_compound = false;
11188 	  goto restart;
11189 
11190 	case RID_IF:
11191 	case RID_SWITCH:
11192 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11193 	  statement = cp_parser_selection_statement (parser, if_p, chain);
11194 	  break;
11195 
11196 	case RID_WHILE:
11197 	case RID_DO:
11198 	case RID_FOR:
11199 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11200 	  statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11201 	  break;
11202 
11203 	case RID_BREAK:
11204 	case RID_CONTINUE:
11205 	case RID_RETURN:
11206 	case RID_GOTO:
11207 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11208 	  statement = cp_parser_jump_statement (parser);
11209 	  break;
11210 
11211 	  /* Objective-C++ exception-handling constructs.  */
11212 	case RID_AT_TRY:
11213 	case RID_AT_CATCH:
11214 	case RID_AT_FINALLY:
11215 	case RID_AT_SYNCHRONIZED:
11216 	case RID_AT_THROW:
11217 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11218 	  statement = cp_parser_objc_statement (parser);
11219 	  break;
11220 
11221 	case RID_TRY:
11222 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11223 	  statement = cp_parser_try_block (parser);
11224 	  break;
11225 
11226 	case RID_NAMESPACE:
11227 	  /* This must be a namespace alias definition.  */
11228 	  if (std_attrs != NULL_TREE)
11229 	    {
11230 	      /*  Attributes should be parsed as part of the the
11231 		  declaration, so let's un-parse them.  */
11232 	      saved_tokens.rollback();
11233 	      std_attrs = NULL_TREE;
11234 	    }
11235 	  cp_parser_declaration_statement (parser);
11236 	  return;
11237 
11238 	case RID_TRANSACTION_ATOMIC:
11239 	case RID_TRANSACTION_RELAXED:
11240 	case RID_SYNCHRONIZED:
11241 	case RID_ATOMIC_NOEXCEPT:
11242 	case RID_ATOMIC_CANCEL:
11243 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11244 	  statement = cp_parser_transaction (parser, token);
11245 	  break;
11246 	case RID_TRANSACTION_CANCEL:
11247 	  std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11248 	  statement = cp_parser_transaction_cancel (parser);
11249 	  break;
11250 
11251 	default:
11252 	  /* It might be a keyword like `int' that can start a
11253 	     declaration-statement.  */
11254 	  break;
11255 	}
11256     }
11257   else if (token->type == CPP_NAME)
11258     {
11259       /* If the next token is a `:', then we are looking at a
11260 	 labeled-statement.  */
11261       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11262       if (token->type == CPP_COLON)
11263 	{
11264 	  /* Looks like a labeled-statement with an ordinary label.
11265 	     Parse the label, and then use tail recursion to parse
11266 	     the statement.  */
11267 
11268 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
11269 	  in_compound = false;
11270 	  goto restart;
11271 	}
11272     }
11273   /* Anything that starts with a `{' must be a compound-statement.  */
11274   else if (token->type == CPP_OPEN_BRACE)
11275     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11276   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11277      a statement all its own.  */
11278   else if (token->type == CPP_PRAGMA)
11279     {
11280       /* Only certain OpenMP pragmas are attached to statements, and thus
11281 	 are considered statements themselves.  All others are not.  In
11282 	 the context of a compound, accept the pragma as a "statement" and
11283 	 return so that we can check for a close brace.  Otherwise we
11284 	 require a real statement and must go back and read one.  */
11285       if (in_compound)
11286 	cp_parser_pragma (parser, pragma_compound, if_p);
11287       else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11288 	goto restart;
11289       return;
11290     }
11291   else if (token->type == CPP_EOF)
11292     {
11293       cp_parser_error (parser, "expected statement");
11294       return;
11295     }
11296 
11297   /* Everything else must be a declaration-statement or an
11298      expression-statement.  Try for the declaration-statement
11299      first, unless we are looking at a `;', in which case we know that
11300      we have an expression-statement.  */
11301   if (!statement)
11302     {
11303       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11304 	{
11305 	  if (std_attrs != NULL_TREE)
11306 	    /* Attributes should be parsed as part of the declaration,
11307 	       so let's un-parse them.  */
11308 	    saved_tokens.rollback();
11309 
11310 	  cp_parser_parse_tentatively (parser);
11311 	  /* Try to parse the declaration-statement.  */
11312 	  cp_parser_declaration_statement (parser);
11313 	  /* If that worked, we're done.  */
11314 	  if (cp_parser_parse_definitely (parser))
11315 	    return;
11316 	  /* It didn't work, restore the post-attribute position.  */
11317 	  if (std_attrs)
11318 	    cp_lexer_set_token_position (parser->lexer, statement_token);
11319 	}
11320       /* All preceding labels have been parsed at this point.  */
11321       if (loc_after_labels != NULL)
11322 	*loc_after_labels = statement_location;
11323 
11324       std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11325 
11326       /* Look for an expression-statement instead.  */
11327       statement = cp_parser_expression_statement (parser, in_statement_expr);
11328 
11329       /* Handle [[fallthrough]];.  */
11330       if (attribute_fallthrough_p (std_attrs))
11331 	{
11332 	  /* The next token after the fallthrough attribute is ';'.  */
11333 	  if (statement == NULL_TREE)
11334 	    {
11335 	      /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
11336 	      statement = build_call_expr_internal_loc (statement_location,
11337 							IFN_FALLTHROUGH,
11338 							void_type_node, 0);
11339 	      finish_expr_stmt (statement);
11340 	    }
11341 	  else
11342 	    warning_at (statement_location, OPT_Wattributes,
11343 			"%<fallthrough%> attribute not followed by %<;%>");
11344 	  std_attrs = NULL_TREE;
11345 	}
11346     }
11347 
11348   /* Set the line number for the statement.  */
11349   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11350     SET_EXPR_LOCATION (statement, statement_location);
11351 
11352   /* Allow "[[fallthrough]];", but warn otherwise.  */
11353   if (std_attrs != NULL_TREE)
11354     warning_at (attrs_loc,
11355 		OPT_Wattributes,
11356 		"attributes at the beginning of statement are ignored");
11357 }
11358 
11359 /* Append ATTR to attribute list ATTRS.  */
11360 
11361 static tree
attr_chainon(tree attrs,tree attr)11362 attr_chainon (tree attrs, tree attr)
11363 {
11364   if (attrs == error_mark_node)
11365     return error_mark_node;
11366   if (attr == error_mark_node)
11367     return error_mark_node;
11368   return chainon (attrs, attr);
11369 }
11370 
11371 /* Parse the label for a labeled-statement, i.e.
11372 
11373    identifier :
11374    case constant-expression :
11375    default :
11376 
11377    GNU Extension:
11378    case constant-expression ... constant-expression : statement
11379 
11380    When a label is parsed without errors, the label is added to the
11381    parse tree by the finish_* functions, so this function doesn't
11382    have to return the label.  */
11383 
11384 static void
cp_parser_label_for_labeled_statement(cp_parser * parser,tree attributes)11385 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11386 {
11387   cp_token *token;
11388   tree label = NULL_TREE;
11389   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11390 
11391   /* The next token should be an identifier.  */
11392   token = cp_lexer_peek_token (parser->lexer);
11393   if (token->type != CPP_NAME
11394       && token->type != CPP_KEYWORD)
11395     {
11396       cp_parser_error (parser, "expected labeled-statement");
11397       return;
11398     }
11399 
11400   /* Remember whether this case or a user-defined label is allowed to fall
11401      through to.  */
11402   bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11403 
11404   parser->colon_corrects_to_scope_p = false;
11405   switch (token->keyword)
11406     {
11407     case RID_CASE:
11408       {
11409 	tree expr, expr_hi;
11410 	cp_token *ellipsis;
11411 
11412 	/* Consume the `case' token.  */
11413 	cp_lexer_consume_token (parser->lexer);
11414 	/* Parse the constant-expression.  */
11415 	expr = cp_parser_constant_expression (parser);
11416 	if (check_for_bare_parameter_packs (expr))
11417 	  expr = error_mark_node;
11418 
11419 	ellipsis = cp_lexer_peek_token (parser->lexer);
11420 	if (ellipsis->type == CPP_ELLIPSIS)
11421 	  {
11422 	    /* Consume the `...' token.  */
11423 	    cp_lexer_consume_token (parser->lexer);
11424 	    expr_hi = cp_parser_constant_expression (parser);
11425 	    if (check_for_bare_parameter_packs (expr_hi))
11426 	      expr_hi = error_mark_node;
11427 
11428 	    /* We don't need to emit warnings here, as the common code
11429 	       will do this for us.  */
11430 	  }
11431 	else
11432 	  expr_hi = NULL_TREE;
11433 
11434 	if (parser->in_switch_statement_p)
11435 	  {
11436 	    tree l = finish_case_label (token->location, expr, expr_hi);
11437 	    if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11438 	      {
11439 		label = CASE_LABEL (l);
11440 		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11441 	      }
11442 	  }
11443 	else
11444 	  error_at (token->location,
11445 		    "case label %qE not within a switch statement",
11446 		    expr);
11447       }
11448       break;
11449 
11450     case RID_DEFAULT:
11451       /* Consume the `default' token.  */
11452       cp_lexer_consume_token (parser->lexer);
11453 
11454       if (parser->in_switch_statement_p)
11455 	{
11456 	  tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11457 	  if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11458 	      {
11459 		label = CASE_LABEL (l);
11460 		FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11461 	      }
11462 	}
11463       else
11464 	error_at (token->location, "case label not within a switch statement");
11465       break;
11466 
11467     default:
11468       /* Anything else must be an ordinary label.  */
11469       label = finish_label_stmt (cp_parser_identifier (parser));
11470       if (label && TREE_CODE (label) == LABEL_DECL)
11471 	FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11472       break;
11473     }
11474 
11475   /* Require the `:' token.  */
11476   cp_parser_require (parser, CPP_COLON, RT_COLON);
11477 
11478   /* An ordinary label may optionally be followed by attributes.
11479      However, this is only permitted if the attributes are then
11480      followed by a semicolon.  This is because, for backward
11481      compatibility, when parsing
11482        lab: __attribute__ ((unused)) int i;
11483      we want the attribute to attach to "i", not "lab".  */
11484   if (label != NULL_TREE
11485       && cp_next_tokens_can_be_gnu_attribute_p (parser))
11486     {
11487       tree attrs;
11488       cp_parser_parse_tentatively (parser);
11489       attrs = cp_parser_gnu_attributes_opt (parser);
11490       if (attrs == NULL_TREE
11491 	  /* And fallthrough always binds to the expression-statement.  */
11492 	  || attribute_fallthrough_p (attrs)
11493 	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11494 	cp_parser_abort_tentative_parse (parser);
11495       else if (!cp_parser_parse_definitely (parser))
11496 	;
11497       else
11498 	attributes = attr_chainon (attributes, attrs);
11499     }
11500 
11501   if (attributes != NULL_TREE)
11502     cplus_decl_attributes (&label, attributes, 0);
11503 
11504   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11505 }
11506 
11507 /* Parse an expression-statement.
11508 
11509    expression-statement:
11510      expression [opt] ;
11511 
11512    Returns the new EXPR_STMT -- or NULL_TREE if the expression
11513    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11514    indicates whether this expression-statement is part of an
11515    expression statement.  */
11516 
11517 static tree
cp_parser_expression_statement(cp_parser * parser,tree in_statement_expr)11518 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11519 {
11520   tree statement = NULL_TREE;
11521   cp_token *token = cp_lexer_peek_token (parser->lexer);
11522   location_t loc = token->location;
11523 
11524   /* There might be attribute fallthrough.  */
11525   tree attr = cp_parser_gnu_attributes_opt (parser);
11526 
11527   /* If the next token is a ';', then there is no expression
11528      statement.  */
11529   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11530     {
11531       statement = cp_parser_expression (parser);
11532       if (statement == error_mark_node
11533 	  && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11534 	{
11535 	  cp_parser_skip_to_end_of_block_or_statement (parser);
11536 	  return error_mark_node;
11537 	}
11538     }
11539 
11540   /* Handle [[fallthrough]];.  */
11541   if (attribute_fallthrough_p (attr))
11542     {
11543       /* The next token after the fallthrough attribute is ';'.  */
11544       if (statement == NULL_TREE)
11545 	/* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
11546 	statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11547 						  void_type_node, 0);
11548       else
11549 	warning_at (loc, OPT_Wattributes,
11550 		    "%<fallthrough%> attribute not followed by %<;%>");
11551       attr = NULL_TREE;
11552     }
11553 
11554   /* Allow "[[fallthrough]];", but warn otherwise.  */
11555   if (attr != NULL_TREE)
11556     warning_at (loc, OPT_Wattributes,
11557 		"attributes at the beginning of statement are ignored");
11558 
11559   /* Give a helpful message for "A<T>::type t;" and the like.  */
11560   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11561       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11562     {
11563       if (TREE_CODE (statement) == SCOPE_REF)
11564 	error_at (token->location, "need %<typename%> before %qE because "
11565 		  "%qT is a dependent scope",
11566 		  statement, TREE_OPERAND (statement, 0));
11567       else if (is_overloaded_fn (statement)
11568 	       && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11569 	{
11570 	  /* A::A a; */
11571 	  tree fn = get_first_fn (statement);
11572 	  error_at (token->location,
11573 		    "%<%T::%D%> names the constructor, not the type",
11574 		    DECL_CONTEXT (fn), DECL_NAME (fn));
11575 	}
11576     }
11577 
11578   /* Consume the final `;'.  */
11579   cp_parser_consume_semicolon_at_end_of_statement (parser);
11580 
11581   if (in_statement_expr
11582       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11583     /* This is the final expression statement of a statement
11584        expression.  */
11585     statement = finish_stmt_expr_expr (statement, in_statement_expr);
11586   else if (statement)
11587     statement = finish_expr_stmt (statement);
11588 
11589   return statement;
11590 }
11591 
11592 /* Parse a compound-statement.
11593 
11594    compound-statement:
11595      { statement-seq [opt] }
11596 
11597    GNU extension:
11598 
11599    compound-statement:
11600      { label-declaration-seq [opt] statement-seq [opt] }
11601 
11602    label-declaration-seq:
11603      label-declaration
11604      label-declaration-seq label-declaration
11605 
11606    Returns a tree representing the statement.  */
11607 
11608 static tree
cp_parser_compound_statement(cp_parser * parser,tree in_statement_expr,int bcs_flags,bool function_body)11609 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11610 			      int bcs_flags, bool function_body)
11611 {
11612   tree compound_stmt;
11613   matching_braces braces;
11614 
11615   /* Consume the `{'.  */
11616   if (!braces.require_open (parser))
11617     return error_mark_node;
11618   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11619       && !function_body && cxx_dialect < cxx14)
11620     pedwarn (input_location, OPT_Wpedantic,
11621 	     "compound-statement in %<constexpr%> function");
11622   /* Begin the compound-statement.  */
11623   compound_stmt = begin_compound_stmt (bcs_flags);
11624   /* If the next keyword is `__label__' we have a label declaration.  */
11625   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11626     cp_parser_label_declaration (parser);
11627   /* Parse an (optional) statement-seq.  */
11628   cp_parser_statement_seq_opt (parser, in_statement_expr);
11629   /* Finish the compound-statement.  */
11630   finish_compound_stmt (compound_stmt);
11631   /* Consume the `}'.  */
11632   braces.require_close (parser);
11633 
11634   return compound_stmt;
11635 }
11636 
11637 /* Parse an (optional) statement-seq.
11638 
11639    statement-seq:
11640      statement
11641      statement-seq [opt] statement  */
11642 
11643 static void
cp_parser_statement_seq_opt(cp_parser * parser,tree in_statement_expr)11644 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11645 {
11646   /* Scan statements until there aren't any more.  */
11647   while (true)
11648     {
11649       cp_token *token = cp_lexer_peek_token (parser->lexer);
11650 
11651       /* If we are looking at a `}', then we have run out of
11652 	 statements; the same is true if we have reached the end
11653 	 of file, or have stumbled upon a stray '@end'.  */
11654       if (token->type == CPP_CLOSE_BRACE
11655 	  || token->type == CPP_EOF
11656 	  || token->type == CPP_PRAGMA_EOL
11657 	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11658 	break;
11659 
11660       /* If we are in a compound statement and find 'else' then
11661 	 something went wrong.  */
11662       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11663 	{
11664 	  if (parser->in_statement & IN_IF_STMT)
11665 	    break;
11666 	  else
11667 	    {
11668 	      token = cp_lexer_consume_token (parser->lexer);
11669 	      error_at (token->location, "%<else%> without a previous %<if%>");
11670 	    }
11671 	}
11672 
11673       /* Parse the statement.  */
11674       cp_parser_statement (parser, in_statement_expr, true, NULL);
11675     }
11676 }
11677 
11678 /* Return true if this is the C++20 version of range-based-for with
11679    init-statement.  */
11680 
11681 static bool
cp_parser_range_based_for_with_init_p(cp_parser * parser)11682 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11683 {
11684   bool r = false;
11685 
11686   /* Save tokens so that we can put them back.  */
11687   cp_lexer_save_tokens (parser->lexer);
11688 
11689   /* There has to be an unnested ; followed by an unnested :.  */
11690   if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11691 					       /*recovering=*/false,
11692 					       CPP_SEMICOLON,
11693 					       /*consume_paren=*/false) != -1)
11694     goto out;
11695 
11696   /* We found the semicolon, eat it now.  */
11697   cp_lexer_consume_token (parser->lexer);
11698 
11699   /* Now look for ':' that is not nested in () or {}.  */
11700   r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11701 						/*recovering=*/false,
11702 						CPP_COLON,
11703 						/*consume_paren=*/false) == -1);
11704 
11705 out:
11706   /* Roll back the tokens we skipped.  */
11707   cp_lexer_rollback_tokens (parser->lexer);
11708 
11709   return r;
11710 }
11711 
11712 /* Return true if we're looking at (init; cond), false otherwise.  */
11713 
11714 static bool
cp_parser_init_statement_p(cp_parser * parser)11715 cp_parser_init_statement_p (cp_parser *parser)
11716 {
11717   /* Save tokens so that we can put them back.  */
11718   cp_lexer_save_tokens (parser->lexer);
11719 
11720   /* Look for ';' that is not nested in () or {}.  */
11721   int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11722 						     /*recovering=*/false,
11723 						     CPP_SEMICOLON,
11724 						     /*consume_paren=*/false);
11725 
11726   /* Roll back the tokens we skipped.  */
11727   cp_lexer_rollback_tokens (parser->lexer);
11728 
11729   return ret == -1;
11730 }
11731 
11732 /* Parse a selection-statement.
11733 
11734    selection-statement:
11735      if ( init-statement [opt] condition ) statement
11736      if ( init-statement [opt] condition ) statement else statement
11737      switch ( init-statement [opt] condition ) statement
11738 
11739    Returns the new IF_STMT or SWITCH_STMT.
11740 
11741    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11742    is a (possibly labeled) if statement which is not enclosed in
11743    braces and has an else clause.  This is used to implement
11744    -Wparentheses.
11745 
11746    CHAIN is a vector of if-else-if conditions.  This is used to implement
11747    -Wduplicated-cond.  */
11748 
11749 static tree
cp_parser_selection_statement(cp_parser * parser,bool * if_p,vec<tree> * chain)11750 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11751 			       vec<tree> *chain)
11752 {
11753   cp_token *token;
11754   enum rid keyword;
11755   token_indent_info guard_tinfo;
11756 
11757   if (if_p != NULL)
11758     *if_p = false;
11759 
11760   /* Peek at the next token.  */
11761   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11762   guard_tinfo = get_token_indent_info (token);
11763 
11764   /* See what kind of keyword it is.  */
11765   keyword = token->keyword;
11766   switch (keyword)
11767     {
11768     case RID_IF:
11769     case RID_SWITCH:
11770       {
11771 	tree statement;
11772 	tree condition;
11773 
11774 	bool cx = false;
11775 	if (keyword == RID_IF
11776 	    && cp_lexer_next_token_is_keyword (parser->lexer,
11777 					       RID_CONSTEXPR))
11778 	  {
11779 	    cx = true;
11780 	    cp_token *tok = cp_lexer_consume_token (parser->lexer);
11781 	    if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11782 	      pedwarn (tok->location, 0, "%<if constexpr%> only available "
11783 		       "with %<-std=c++17%> or %<-std=gnu++17%>");
11784 	  }
11785 
11786 	/* Look for the `('.  */
11787 	matching_parens parens;
11788 	if (!parens.require_open (parser))
11789 	  {
11790 	    cp_parser_skip_to_end_of_statement (parser);
11791 	    return error_mark_node;
11792 	  }
11793 
11794 	/* Begin the selection-statement.  */
11795 	if (keyword == RID_IF)
11796 	  {
11797 	    statement = begin_if_stmt ();
11798 	    IF_STMT_CONSTEXPR_P (statement) = cx;
11799 	  }
11800 	else
11801 	  statement = begin_switch_stmt ();
11802 
11803 	/* Parse the optional init-statement.  */
11804 	if (cp_parser_init_statement_p (parser))
11805 	  {
11806 	    tree decl;
11807 	    if (cxx_dialect < cxx17)
11808 	      pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11809 		       "init-statement in selection statements only available "
11810 		       "with %<-std=c++17%> or %<-std=gnu++17%>");
11811 	    cp_parser_init_statement (parser, &decl);
11812 	  }
11813 
11814 	/* Parse the condition.  */
11815 	condition = cp_parser_condition (parser);
11816 	/* Look for the `)'.  */
11817 	if (!parens.require_close (parser))
11818 	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
11819 						 /*consume_paren=*/true);
11820 
11821 	if (keyword == RID_IF)
11822 	  {
11823 	    bool nested_if;
11824 	    unsigned char in_statement;
11825 
11826 	    /* Add the condition.  */
11827 	    condition = finish_if_stmt_cond (condition, statement);
11828 
11829 	    if (warn_duplicated_cond)
11830 	      warn_duplicated_cond_add_or_warn (token->location, condition,
11831 						&chain);
11832 
11833 	    /* Parse the then-clause.  */
11834 	    in_statement = parser->in_statement;
11835 	    parser->in_statement |= IN_IF_STMT;
11836 
11837 	    /* Outside a template, the non-selected branch of a constexpr
11838 	       if is a 'discarded statement', i.e. unevaluated.  */
11839 	    bool was_discarded = in_discarded_stmt;
11840 	    bool discard_then = (cx && !processing_template_decl
11841 				 && integer_zerop (condition));
11842 	    if (discard_then)
11843 	      {
11844 		in_discarded_stmt = true;
11845 		++c_inhibit_evaluation_warnings;
11846 	      }
11847 
11848 	    cp_parser_implicitly_scoped_statement (parser, &nested_if,
11849 						   guard_tinfo);
11850 
11851 	    parser->in_statement = in_statement;
11852 
11853 	    finish_then_clause (statement);
11854 
11855 	    if (discard_then)
11856 	      {
11857 		THEN_CLAUSE (statement) = NULL_TREE;
11858 		in_discarded_stmt = was_discarded;
11859 		--c_inhibit_evaluation_warnings;
11860 	      }
11861 
11862 	    /* If the next token is `else', parse the else-clause.  */
11863 	    if (cp_lexer_next_token_is_keyword (parser->lexer,
11864 						RID_ELSE))
11865 	      {
11866 		bool discard_else = (cx && !processing_template_decl
11867 				     && integer_nonzerop (condition));
11868 		if (discard_else)
11869 		  {
11870 		    in_discarded_stmt = true;
11871 		    ++c_inhibit_evaluation_warnings;
11872 		  }
11873 
11874 		guard_tinfo
11875 		  = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11876 		/* Consume the `else' keyword.  */
11877 		cp_lexer_consume_token (parser->lexer);
11878 		if (warn_duplicated_cond)
11879 		  {
11880 		    if (cp_lexer_next_token_is_keyword (parser->lexer,
11881 							RID_IF)
11882 			&& chain == NULL)
11883 		      {
11884 			/* We've got "if (COND) else if (COND2)".  Start
11885 			   the condition chain and add COND as the first
11886 			   element.  */
11887 			chain = new vec<tree> ();
11888 			if (!CONSTANT_CLASS_P (condition)
11889 			    && !TREE_SIDE_EFFECTS (condition))
11890 			{
11891 			  /* Wrap it in a NOP_EXPR so that we can set the
11892 			     location of the condition.  */
11893 			  tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11894 					   condition);
11895 			  SET_EXPR_LOCATION (e, token->location);
11896 			  chain->safe_push (e);
11897 			}
11898 		      }
11899 		    else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11900 							      RID_IF))
11901 		      {
11902 			/* This is if-else without subsequent if.  Zap the
11903 			   condition chain; we would have already warned at
11904 			   this point.  */
11905 			delete chain;
11906 			chain = NULL;
11907 		      }
11908 		  }
11909 		begin_else_clause (statement);
11910 		/* Parse the else-clause.  */
11911 		cp_parser_implicitly_scoped_statement (parser, NULL,
11912 						       guard_tinfo, chain);
11913 
11914 		finish_else_clause (statement);
11915 
11916 		/* If we are currently parsing a then-clause, then
11917 		   IF_P will not be NULL.  We set it to true to
11918 		   indicate that this if statement has an else clause.
11919 		   This may trigger the Wparentheses warning below
11920 		   when we get back up to the parent if statement.  */
11921 		if (if_p != NULL)
11922 		  *if_p = true;
11923 
11924 		if (discard_else)
11925 		  {
11926 		    ELSE_CLAUSE (statement) = NULL_TREE;
11927 		    in_discarded_stmt = was_discarded;
11928 		    --c_inhibit_evaluation_warnings;
11929 		  }
11930 	      }
11931 	    else
11932 	      {
11933 		/* This if statement does not have an else clause.  If
11934 		   NESTED_IF is true, then the then-clause has an if
11935 		   statement which does have an else clause.  We warn
11936 		   about the potential ambiguity.  */
11937 		if (nested_if)
11938 		  warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11939 			      "suggest explicit braces to avoid ambiguous"
11940 			      " %<else%>");
11941 		if (warn_duplicated_cond)
11942 		  {
11943 		    /* We don't need the condition chain anymore.  */
11944 		    delete chain;
11945 		    chain = NULL;
11946 		  }
11947 	      }
11948 
11949 	    /* Now we're all done with the if-statement.  */
11950 	    finish_if_stmt (statement);
11951 	  }
11952 	else
11953 	  {
11954 	    bool in_switch_statement_p;
11955 	    unsigned char in_statement;
11956 
11957 	    /* Add the condition.  */
11958 	    finish_switch_cond (condition, statement);
11959 
11960 	    /* Parse the body of the switch-statement.  */
11961 	    in_switch_statement_p = parser->in_switch_statement_p;
11962 	    in_statement = parser->in_statement;
11963 	    parser->in_switch_statement_p = true;
11964 	    parser->in_statement |= IN_SWITCH_STMT;
11965 	    cp_parser_implicitly_scoped_statement (parser, if_p,
11966 						   guard_tinfo);
11967 	    parser->in_switch_statement_p = in_switch_statement_p;
11968 	    parser->in_statement = in_statement;
11969 
11970 	    /* Now we're all done with the switch-statement.  */
11971 	    finish_switch_stmt (statement);
11972 	  }
11973 
11974 	return statement;
11975       }
11976       break;
11977 
11978     default:
11979       cp_parser_error (parser, "expected selection-statement");
11980       return error_mark_node;
11981     }
11982 }
11983 
11984 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11985    If we have seen at least one decl-specifier, and the next token
11986    is not a parenthesis, then we must be looking at a declaration.
11987    (After "int (" we might be looking at a functional cast.)  */
11988 
11989 static void
cp_parser_maybe_commit_to_declaration(cp_parser * parser,bool any_specifiers_p)11990 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11991 				       bool any_specifiers_p)
11992 {
11993   if (any_specifiers_p
11994       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11995       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11996       && !cp_parser_error_occurred (parser))
11997     cp_parser_commit_to_tentative_parse (parser);
11998 }
11999 
12000 /* Helper function for cp_parser_condition.  Enforces [stmt.stmt]/2:
12001    The declarator shall not specify a function or an array.  Returns
12002    TRUE if the declarator is valid, FALSE otherwise.  */
12003 
12004 static bool
cp_parser_check_condition_declarator(cp_parser * parser,cp_declarator * declarator,location_t loc)12005 cp_parser_check_condition_declarator (cp_parser* parser,
12006                                      cp_declarator *declarator,
12007                                      location_t loc)
12008 {
12009   if (declarator == cp_error_declarator
12010       || function_declarator_p (declarator)
12011       || declarator->kind == cdk_array)
12012     {
12013       if (declarator == cp_error_declarator)
12014 	/* Already complained.  */;
12015       else if (declarator->kind == cdk_array)
12016        error_at (loc, "condition declares an array");
12017       else
12018        error_at (loc, "condition declares a function");
12019       if (parser->fully_implicit_function_template_p)
12020        abort_fully_implicit_template (parser);
12021       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
12022                                             /*or_comma=*/false,
12023                                             /*consume_paren=*/false);
12024       return false;
12025     }
12026   else
12027     return true;
12028 }
12029 
12030 /* Parse a condition.
12031 
12032    condition:
12033      expression
12034      type-specifier-seq declarator = initializer-clause
12035      type-specifier-seq declarator braced-init-list
12036 
12037    GNU Extension:
12038 
12039    condition:
12040      type-specifier-seq declarator asm-specification [opt]
12041        attributes [opt] = assignment-expression
12042 
12043    Returns the expression that should be tested.  */
12044 
12045 static tree
cp_parser_condition(cp_parser * parser)12046 cp_parser_condition (cp_parser* parser)
12047 {
12048   cp_decl_specifier_seq type_specifiers;
12049   const char *saved_message;
12050   int declares_class_or_enum;
12051 
12052   /* Try the declaration first.  */
12053   cp_parser_parse_tentatively (parser);
12054   /* New types are not allowed in the type-specifier-seq for a
12055      condition.  */
12056   saved_message = parser->type_definition_forbidden_message;
12057   parser->type_definition_forbidden_message
12058     = G_("types may not be defined in conditions");
12059   /* Parse the type-specifier-seq.  */
12060   cp_parser_decl_specifier_seq (parser,
12061 				CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
12062 				&type_specifiers,
12063 				&declares_class_or_enum);
12064   /* Restore the saved message.  */
12065   parser->type_definition_forbidden_message = saved_message;
12066 
12067   cp_parser_maybe_commit_to_declaration (parser,
12068 					 type_specifiers.any_specifiers_p);
12069 
12070   /* If all is well, we might be looking at a declaration.  */
12071   if (!cp_parser_error_occurred (parser))
12072     {
12073       tree decl;
12074       tree asm_specification;
12075       tree attributes;
12076       cp_declarator *declarator;
12077       tree initializer = NULL_TREE;
12078       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12079 
12080       /* Parse the declarator.  */
12081       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12082 					 CP_PARSER_FLAGS_NONE,
12083 					 /*ctor_dtor_or_conv_p=*/NULL,
12084 					 /*parenthesized_p=*/NULL,
12085 					 /*member_p=*/false,
12086 					 /*friend_p=*/false,
12087 					 /*static_p=*/false);
12088       /* Parse the attributes.  */
12089       attributes = cp_parser_attributes_opt (parser);
12090       /* Parse the asm-specification.  */
12091       asm_specification = cp_parser_asm_specification_opt (parser);
12092       /* If the next token is not an `=' or '{', then we might still be
12093 	 looking at an expression.  For example:
12094 
12095 	   if (A(a).x)
12096 
12097 	 looks like a decl-specifier-seq and a declarator -- but then
12098 	 there is no `=', so this is an expression.  */
12099       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12100 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12101 	cp_parser_simulate_error (parser);
12102 
12103       /* If we did see an `=' or '{', then we are looking at a declaration
12104 	 for sure.  */
12105       if (cp_parser_parse_definitely (parser))
12106 	{
12107 	  tree pushed_scope;
12108 	  bool non_constant_p = false;
12109 	  int flags = LOOKUP_ONLYCONVERTING;
12110 
12111 	  if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12112 	    return error_mark_node;
12113 
12114 	  /* Create the declaration.  */
12115 	  decl = start_decl (declarator, &type_specifiers,
12116 			     /*initialized_p=*/true,
12117 			     attributes, /*prefix_attributes=*/NULL_TREE,
12118 			     &pushed_scope);
12119 
12120 	  /* Parse the initializer.  */
12121 	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12122 	    {
12123 	      initializer = cp_parser_braced_list (parser, &non_constant_p);
12124 	      CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12125 	      flags = 0;
12126 	    }
12127 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12128 	    {
12129 	      /* Consume the `='.  */
12130 	      cp_lexer_consume_token (parser->lexer);
12131 	      initializer = cp_parser_initializer_clause (parser,
12132 							  &non_constant_p);
12133 	    }
12134 	  else
12135 	    {
12136 	      cp_parser_error (parser, "expected initializer");
12137 	      initializer = error_mark_node;
12138 	    }
12139 	  if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12140 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12141 
12142 	  /* Process the initializer.  */
12143 	  cp_finish_decl (decl,
12144 			  initializer, !non_constant_p,
12145 			  asm_specification,
12146 			  flags);
12147 
12148 	  if (pushed_scope)
12149 	    pop_scope (pushed_scope);
12150 
12151 	  return convert_from_reference (decl);
12152 	}
12153     }
12154   /* If we didn't even get past the declarator successfully, we are
12155      definitely not looking at a declaration.  */
12156   else
12157     cp_parser_abort_tentative_parse (parser);
12158 
12159   /* Otherwise, we are looking at an expression.  */
12160   return cp_parser_expression (parser);
12161 }
12162 
12163 /* Parses a for-statement or range-for-statement until the closing ')',
12164    not included. */
12165 
12166 static tree
cp_parser_for(cp_parser * parser,bool ivdep,unsigned short unroll)12167 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12168 {
12169   tree init, scope, decl;
12170   bool is_range_for;
12171 
12172   /* Begin the for-statement.  */
12173   scope = begin_for_scope (&init);
12174 
12175   /* Parse the initialization.  */
12176   is_range_for = cp_parser_init_statement (parser, &decl);
12177 
12178   if (is_range_for)
12179     return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12180 				false);
12181   else
12182     return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12183 }
12184 
12185 static tree
cp_parser_c_for(cp_parser * parser,tree scope,tree init,bool ivdep,unsigned short unroll)12186 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12187 		 unsigned short unroll)
12188 {
12189   /* Normal for loop */
12190   tree condition = NULL_TREE;
12191   tree expression = NULL_TREE;
12192   tree stmt;
12193 
12194   stmt = begin_for_stmt (scope, init);
12195   /* The init-statement has already been parsed in
12196      cp_parser_init_statement, so no work is needed here.  */
12197   finish_init_stmt (stmt);
12198 
12199   /* If there's a condition, process it.  */
12200   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12201     condition = cp_parser_condition (parser);
12202   else if (ivdep)
12203     {
12204       cp_parser_error (parser, "missing loop condition in loop with "
12205 		       "%<GCC ivdep%> pragma");
12206       condition = error_mark_node;
12207     }
12208   else if (unroll)
12209     {
12210       cp_parser_error (parser, "missing loop condition in loop with "
12211 		       "%<GCC unroll%> pragma");
12212       condition = error_mark_node;
12213     }
12214   finish_for_cond (condition, stmt, ivdep, unroll);
12215   /* Look for the `;'.  */
12216   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12217 
12218   /* If there's an expression, process it.  */
12219   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12220     expression = cp_parser_expression (parser);
12221   finish_for_expr (expression, stmt);
12222 
12223   return stmt;
12224 }
12225 
12226 /* Tries to parse a range-based for-statement:
12227 
12228   range-based-for:
12229     decl-specifier-seq declarator : expression
12230 
12231   The decl-specifier-seq declarator and the `:' are already parsed by
12232   cp_parser_init_statement.  If processing_template_decl it returns a
12233   newly created RANGE_FOR_STMT; if not, it is converted to a
12234   regular FOR_STMT.  */
12235 
12236 static tree
cp_parser_range_for(cp_parser * parser,tree scope,tree init,tree range_decl,bool ivdep,unsigned short unroll,bool is_omp)12237 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12238 		     bool ivdep, unsigned short unroll, bool is_omp)
12239 {
12240   tree stmt, range_expr;
12241   auto_vec <cxx_binding *, 16> bindings;
12242   auto_vec <tree, 16> names;
12243   tree decomp_first_name = NULL_TREE;
12244   unsigned int decomp_cnt = 0;
12245 
12246   /* Get the range declaration momentarily out of the way so that
12247      the range expression doesn't clash with it. */
12248   if (range_decl != error_mark_node)
12249     {
12250       if (DECL_HAS_VALUE_EXPR_P (range_decl))
12251 	{
12252 	  tree v = DECL_VALUE_EXPR (range_decl);
12253 	  /* For decomposition declaration get all of the corresponding
12254 	     declarations out of the way.  */
12255 	  if (TREE_CODE (v) == ARRAY_REF
12256 	      && VAR_P (TREE_OPERAND (v, 0))
12257 	      && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12258 	    {
12259 	      tree d = range_decl;
12260 	      range_decl = TREE_OPERAND (v, 0);
12261 	      decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12262 	      decomp_first_name = d;
12263 	      for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12264 		{
12265 		  tree name = DECL_NAME (d);
12266 		  names.safe_push (name);
12267 		  bindings.safe_push (IDENTIFIER_BINDING (name));
12268 		  IDENTIFIER_BINDING (name)
12269 		    = IDENTIFIER_BINDING (name)->previous;
12270 		}
12271 	    }
12272 	}
12273       if (names.is_empty ())
12274 	{
12275 	  tree name = DECL_NAME (range_decl);
12276 	  names.safe_push (name);
12277 	  bindings.safe_push (IDENTIFIER_BINDING (name));
12278 	  IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12279 	}
12280     }
12281 
12282   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12283     {
12284       bool expr_non_constant_p;
12285       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12286     }
12287   else
12288     range_expr = cp_parser_expression (parser);
12289 
12290   /* Put the range declaration(s) back into scope. */
12291   for (unsigned int i = 0; i < names.length (); i++)
12292     {
12293       cxx_binding *binding = bindings[i];
12294       binding->previous = IDENTIFIER_BINDING (names[i]);
12295       IDENTIFIER_BINDING (names[i]) = binding;
12296     }
12297 
12298   /* finish_omp_for has its own code for the following, so just
12299      return the range_expr instead.  */
12300   if (is_omp)
12301     return range_expr;
12302 
12303   /* If in template, STMT is converted to a normal for-statement
12304      at instantiation. If not, it is done just ahead. */
12305   if (processing_template_decl)
12306     {
12307       if (check_for_bare_parameter_packs (range_expr))
12308 	range_expr = error_mark_node;
12309       stmt = begin_range_for_stmt (scope, init);
12310       if (ivdep)
12311 	RANGE_FOR_IVDEP (stmt) = 1;
12312       if (unroll)
12313 	RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12314       finish_range_for_decl (stmt, range_decl, range_expr);
12315       if (!type_dependent_expression_p (range_expr)
12316 	  /* do_auto_deduction doesn't mess with template init-lists.  */
12317 	  && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12318 	do_range_for_auto_deduction (range_decl, range_expr);
12319     }
12320   else
12321     {
12322       stmt = begin_for_stmt (scope, init);
12323       stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12324 				   decomp_first_name, decomp_cnt, ivdep,
12325 				   unroll);
12326     }
12327   return stmt;
12328 }
12329 
12330 /* Subroutine of cp_convert_range_for: given the initializer expression,
12331    builds up the range temporary.  */
12332 
12333 static tree
build_range_temp(tree range_expr)12334 build_range_temp (tree range_expr)
12335 {
12336   tree range_type, range_temp;
12337 
12338   /* Find out the type deduced by the declaration
12339      `auto &&__range = range_expr'.  */
12340   range_type = cp_build_reference_type (make_auto (), true);
12341   range_type = do_auto_deduction (range_type, range_expr,
12342 				  type_uses_auto (range_type));
12343 
12344   /* Create the __range variable.  */
12345   range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12346 			   range_type);
12347   TREE_USED (range_temp) = 1;
12348   DECL_ARTIFICIAL (range_temp) = 1;
12349 
12350   return range_temp;
12351 }
12352 
12353 /* Used by cp_parser_range_for in template context: we aren't going to
12354    do a full conversion yet, but we still need to resolve auto in the
12355    type of the for-range-declaration if present.  This is basically
12356    a shortcut version of cp_convert_range_for.  */
12357 
12358 static void
do_range_for_auto_deduction(tree decl,tree range_expr)12359 do_range_for_auto_deduction (tree decl, tree range_expr)
12360 {
12361   tree auto_node = type_uses_auto (TREE_TYPE (decl));
12362   if (auto_node)
12363     {
12364       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12365       range_temp = convert_from_reference (build_range_temp (range_expr));
12366       iter_type = (cp_parser_perform_range_for_lookup
12367 		   (range_temp, &begin_dummy, &end_dummy));
12368       if (iter_type)
12369 	{
12370 	  iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12371 				  iter_type);
12372 	  iter_decl = build_x_indirect_ref (input_location, iter_decl,
12373 					    RO_UNARY_STAR,
12374 					    tf_warning_or_error);
12375 	  TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12376 						iter_decl, auto_node);
12377 	}
12378     }
12379 }
12380 
12381 /* Converts a range-based for-statement into a normal
12382    for-statement, as per the definition.
12383 
12384       for (RANGE_DECL : RANGE_EXPR)
12385 	BLOCK
12386 
12387    should be equivalent to:
12388 
12389       {
12390 	auto &&__range = RANGE_EXPR;
12391 	for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12392 	      __begin != __end;
12393 	      ++__begin)
12394 	  {
12395 	      RANGE_DECL = *__begin;
12396 	      BLOCK
12397 	  }
12398       }
12399 
12400    If RANGE_EXPR is an array:
12401 	BEGIN_EXPR = __range
12402 	END_EXPR = __range + ARRAY_SIZE(__range)
12403    Else if RANGE_EXPR has a member 'begin' or 'end':
12404 	BEGIN_EXPR = __range.begin()
12405 	END_EXPR = __range.end()
12406    Else:
12407 	BEGIN_EXPR = begin(__range)
12408 	END_EXPR = end(__range);
12409 
12410    If __range has a member 'begin' but not 'end', or vice versa, we must
12411    still use the second alternative (it will surely fail, however).
12412    When calling begin()/end() in the third alternative we must use
12413    argument dependent lookup, but always considering 'std' as an associated
12414    namespace.  */
12415 
12416 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)12417 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12418 		      tree decomp_first_name, unsigned int decomp_cnt,
12419 		      bool ivdep, unsigned short unroll)
12420 {
12421   tree begin, end;
12422   tree iter_type, begin_expr, end_expr;
12423   tree condition, expression;
12424 
12425   range_expr = mark_lvalue_use (range_expr);
12426 
12427   if (range_decl == error_mark_node || range_expr == error_mark_node)
12428     /* If an error happened previously do nothing or else a lot of
12429        unhelpful errors would be issued.  */
12430     begin_expr = end_expr = iter_type = error_mark_node;
12431   else
12432     {
12433       tree range_temp;
12434 
12435       if (VAR_P (range_expr)
12436 	  && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12437 	/* Can't bind a reference to an array of runtime bound.  */
12438 	range_temp = range_expr;
12439       else
12440 	{
12441 	  range_temp = build_range_temp (range_expr);
12442 	  pushdecl (range_temp);
12443 	  cp_finish_decl (range_temp, range_expr,
12444 			  /*is_constant_init*/false, NULL_TREE,
12445 			  LOOKUP_ONLYCONVERTING);
12446 	  range_temp = convert_from_reference (range_temp);
12447 	}
12448       iter_type = cp_parser_perform_range_for_lookup (range_temp,
12449 						      &begin_expr, &end_expr);
12450     }
12451 
12452   /* The new for initialization statement.  */
12453   begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12454 		      iter_type);
12455   TREE_USED (begin) = 1;
12456   DECL_ARTIFICIAL (begin) = 1;
12457   pushdecl (begin);
12458   cp_finish_decl (begin, begin_expr,
12459 		  /*is_constant_init*/false, NULL_TREE,
12460 		  LOOKUP_ONLYCONVERTING);
12461 
12462   if (cxx_dialect >= cxx17)
12463     iter_type = cv_unqualified (TREE_TYPE (end_expr));
12464   end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12465   TREE_USED (end) = 1;
12466   DECL_ARTIFICIAL (end) = 1;
12467   pushdecl (end);
12468   cp_finish_decl (end, end_expr,
12469 		  /*is_constant_init*/false, NULL_TREE,
12470 		  LOOKUP_ONLYCONVERTING);
12471 
12472   finish_init_stmt (statement);
12473 
12474   /* The new for condition.  */
12475   condition = build_x_binary_op (input_location, NE_EXPR,
12476 				 begin, ERROR_MARK,
12477 				 end, ERROR_MARK,
12478 				 NULL, tf_warning_or_error);
12479   finish_for_cond (condition, statement, ivdep, unroll);
12480 
12481   /* The new increment expression.  */
12482   expression = finish_unary_op_expr (input_location,
12483 				     PREINCREMENT_EXPR, begin,
12484 				     tf_warning_or_error);
12485   finish_for_expr (expression, statement);
12486 
12487   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12488     cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12489 
12490   /* The declaration is initialized with *__begin inside the loop body.  */
12491   cp_finish_decl (range_decl,
12492 		  build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12493 					tf_warning_or_error),
12494 		  /*is_constant_init*/false, NULL_TREE,
12495 		  LOOKUP_ONLYCONVERTING);
12496   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12497     cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12498 
12499   return statement;
12500 }
12501 
12502 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12503    We need to solve both at the same time because the method used
12504    depends on the existence of members begin or end.
12505    Returns the type deduced for the iterator expression.  */
12506 
12507 static tree
cp_parser_perform_range_for_lookup(tree range,tree * begin,tree * end)12508 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12509 {
12510   if (error_operand_p (range))
12511     {
12512       *begin = *end = error_mark_node;
12513       return error_mark_node;
12514     }
12515 
12516   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12517     {
12518       error ("range-based %<for%> expression of type %qT "
12519 	     "has incomplete type", TREE_TYPE (range));
12520       *begin = *end = error_mark_node;
12521       return error_mark_node;
12522     }
12523   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12524     {
12525       /* If RANGE is an array, we will use pointer arithmetic.  */
12526       *begin = decay_conversion (range, tf_warning_or_error);
12527       *end = build_binary_op (input_location, PLUS_EXPR,
12528 			      range,
12529 			      array_type_nelts_top (TREE_TYPE (range)),
12530 			      false);
12531       return TREE_TYPE (*begin);
12532     }
12533   else
12534     {
12535       /* If it is not an array, we must do a bit of magic.  */
12536       tree id_begin, id_end;
12537       tree member_begin, member_end;
12538 
12539       *begin = *end = error_mark_node;
12540 
12541       id_begin = get_identifier ("begin");
12542       id_end = get_identifier ("end");
12543       member_begin = lookup_member (TREE_TYPE (range), id_begin,
12544 				    /*protect=*/2, /*want_type=*/false,
12545 				    tf_warning_or_error);
12546       member_end = lookup_member (TREE_TYPE (range), id_end,
12547 				  /*protect=*/2, /*want_type=*/false,
12548 				  tf_warning_or_error);
12549 
12550       if (member_begin != NULL_TREE && member_end != NULL_TREE)
12551 	{
12552 	  /* Use the member functions.  */
12553 	  *begin = cp_parser_range_for_member_function (range, id_begin);
12554 	  *end = cp_parser_range_for_member_function (range, id_end);
12555 	}
12556       else
12557 	{
12558 	  /* Use global functions with ADL.  */
12559 	  vec<tree, va_gc> *vec;
12560 	  vec = make_tree_vector ();
12561 
12562 	  vec_safe_push (vec, range);
12563 
12564 	  member_begin = perform_koenig_lookup (id_begin, vec,
12565 						tf_warning_or_error);
12566 	  *begin = finish_call_expr (member_begin, &vec, false, true,
12567 				     tf_warning_or_error);
12568 	  member_end = perform_koenig_lookup (id_end, vec,
12569 					      tf_warning_or_error);
12570 	  *end = finish_call_expr (member_end, &vec, false, true,
12571 				   tf_warning_or_error);
12572 
12573 	  release_tree_vector (vec);
12574 	}
12575 
12576       /* Last common checks.  */
12577       if (*begin == error_mark_node || *end == error_mark_node)
12578 	{
12579 	  /* If one of the expressions is an error do no more checks.  */
12580 	  *begin = *end = error_mark_node;
12581 	  return error_mark_node;
12582 	}
12583       else if (type_dependent_expression_p (*begin)
12584 	       || type_dependent_expression_p (*end))
12585 	/* Can happen, when, eg, in a template context, Koenig lookup
12586 	   can't resolve begin/end (c++/58503).  */
12587 	return NULL_TREE;
12588       else
12589 	{
12590 	  tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12591 	  /* The unqualified type of the __begin and __end temporaries should
12592 	     be the same, as required by the multiple auto declaration.  */
12593 	  if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12594 	    {
12595 	      if (cxx_dialect >= cxx17
12596 		  && (build_x_binary_op (input_location, NE_EXPR,
12597 					 *begin, ERROR_MARK,
12598 					 *end, ERROR_MARK,
12599 					 NULL, tf_none)
12600 		      != error_mark_node))
12601 		/* P0184R0 allows __begin and __end to have different types,
12602 		   but make sure they are comparable so we can give a better
12603 		   diagnostic.  */;
12604 	      else
12605 		error ("inconsistent begin/end types in range-based %<for%> "
12606 		       "statement: %qT and %qT",
12607 		       TREE_TYPE (*begin), TREE_TYPE (*end));
12608 	    }
12609 	  return iter_type;
12610 	}
12611     }
12612 }
12613 
12614 /* Helper function for cp_parser_perform_range_for_lookup.
12615    Builds a tree for RANGE.IDENTIFIER().  */
12616 
12617 static tree
cp_parser_range_for_member_function(tree range,tree identifier)12618 cp_parser_range_for_member_function (tree range, tree identifier)
12619 {
12620   tree member, res;
12621   vec<tree, va_gc> *vec;
12622 
12623   member = finish_class_member_access_expr (range, identifier,
12624 					    false, tf_warning_or_error);
12625   if (member == error_mark_node)
12626     return error_mark_node;
12627 
12628   vec = make_tree_vector ();
12629   res = finish_call_expr (member, &vec,
12630 			  /*disallow_virtual=*/false,
12631 			  /*koenig_p=*/false,
12632 			  tf_warning_or_error);
12633   release_tree_vector (vec);
12634   return res;
12635 }
12636 
12637 /* Parse an iteration-statement.
12638 
12639    iteration-statement:
12640      while ( condition ) statement
12641      do statement while ( expression ) ;
12642      for ( init-statement condition [opt] ; expression [opt] )
12643        statement
12644 
12645    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
12646 
12647 static tree
cp_parser_iteration_statement(cp_parser * parser,bool * if_p,bool ivdep,unsigned short unroll)12648 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12649 			       unsigned short unroll)
12650 {
12651   cp_token *token;
12652   enum rid keyword;
12653   tree statement;
12654   unsigned char in_statement;
12655   token_indent_info guard_tinfo;
12656 
12657   /* Peek at the next token.  */
12658   token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12659   if (!token)
12660     return error_mark_node;
12661 
12662   guard_tinfo = get_token_indent_info (token);
12663 
12664   /* Remember whether or not we are already within an iteration
12665      statement.  */
12666   in_statement = parser->in_statement;
12667 
12668   /* See what kind of keyword it is.  */
12669   keyword = token->keyword;
12670   switch (keyword)
12671     {
12672     case RID_WHILE:
12673       {
12674 	tree condition;
12675 
12676 	/* Begin the while-statement.  */
12677 	statement = begin_while_stmt ();
12678 	/* Look for the `('.  */
12679 	matching_parens parens;
12680 	parens.require_open (parser);
12681 	/* Parse the condition.  */
12682 	condition = cp_parser_condition (parser);
12683 	finish_while_stmt_cond (condition, statement, ivdep, unroll);
12684 	/* Look for the `)'.  */
12685 	parens.require_close (parser);
12686 	/* Parse the dependent statement.  */
12687 	parser->in_statement = IN_ITERATION_STMT;
12688 	bool prev = note_iteration_stmt_body_start ();
12689 	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12690 	note_iteration_stmt_body_end (prev);
12691 	parser->in_statement = in_statement;
12692 	/* We're done with the while-statement.  */
12693 	finish_while_stmt (statement);
12694       }
12695       break;
12696 
12697     case RID_DO:
12698       {
12699 	tree expression;
12700 
12701 	/* Begin the do-statement.  */
12702 	statement = begin_do_stmt ();
12703 	/* Parse the body of the do-statement.  */
12704 	parser->in_statement = IN_ITERATION_STMT;
12705 	bool prev = note_iteration_stmt_body_start ();
12706 	cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12707 	note_iteration_stmt_body_end (prev);
12708 	parser->in_statement = in_statement;
12709 	finish_do_body (statement);
12710 	/* Look for the `while' keyword.  */
12711 	cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12712 	/* Look for the `('.  */
12713 	matching_parens parens;
12714 	parens.require_open (parser);
12715 	/* Parse the expression.  */
12716 	expression = cp_parser_expression (parser);
12717 	/* We're done with the do-statement.  */
12718 	finish_do_stmt (expression, statement, ivdep, unroll);
12719 	/* Look for the `)'.  */
12720 	parens.require_close (parser);
12721 	/* Look for the `;'.  */
12722 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12723       }
12724       break;
12725 
12726     case RID_FOR:
12727       {
12728 	/* Look for the `('.  */
12729 	matching_parens parens;
12730 	parens.require_open (parser);
12731 
12732 	statement = cp_parser_for (parser, ivdep, unroll);
12733 
12734 	/* Look for the `)'.  */
12735 	parens.require_close (parser);
12736 
12737 	/* Parse the body of the for-statement.  */
12738 	parser->in_statement = IN_ITERATION_STMT;
12739 	bool prev = note_iteration_stmt_body_start ();
12740 	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12741 	note_iteration_stmt_body_end (prev);
12742 	parser->in_statement = in_statement;
12743 
12744 	/* We're done with the for-statement.  */
12745 	finish_for_stmt (statement);
12746       }
12747       break;
12748 
12749     default:
12750       cp_parser_error (parser, "expected iteration-statement");
12751       statement = error_mark_node;
12752       break;
12753     }
12754 
12755   return statement;
12756 }
12757 
12758 /* Parse a init-statement or the declarator of a range-based-for.
12759    Returns true if a range-based-for declaration is seen.
12760 
12761    init-statement:
12762      expression-statement
12763      simple-declaration  */
12764 
12765 static bool
cp_parser_init_statement(cp_parser * parser,tree * decl)12766 cp_parser_init_statement (cp_parser *parser, tree *decl)
12767 {
12768   /* If the next token is a `;', then we have an empty
12769      expression-statement.  Grammatically, this is also a
12770      simple-declaration, but an invalid one, because it does not
12771      declare anything.  Therefore, if we did not handle this case
12772      specially, we would issue an error message about an invalid
12773      declaration.  */
12774   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12775     {
12776       bool is_range_for = false;
12777       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12778 
12779       /* Try to parse the init-statement.  */
12780       if (cp_parser_range_based_for_with_init_p (parser))
12781 	{
12782 	  tree dummy;
12783 	  cp_parser_parse_tentatively (parser);
12784 	  /* Parse the declaration.  */
12785 	  cp_parser_simple_declaration (parser,
12786 					/*function_definition_allowed_p=*/false,
12787 					&dummy);
12788 	  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12789 	  if (!cp_parser_parse_definitely (parser))
12790 	    /* That didn't work, try to parse it as an expression-statement.  */
12791 	    cp_parser_expression_statement (parser, NULL_TREE);
12792 
12793 	  if (cxx_dialect < cxx2a)
12794 	    {
12795 	      pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12796 		       "range-based %<for%> loops with initializer only "
12797 		       "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12798 	      *decl = error_mark_node;
12799 	    }
12800 	}
12801 
12802       /* A colon is used in range-based for.  */
12803       parser->colon_corrects_to_scope_p = false;
12804 
12805       /* We're going to speculatively look for a declaration, falling back
12806 	 to an expression, if necessary.  */
12807       cp_parser_parse_tentatively (parser);
12808       /* Parse the declaration.  */
12809       cp_parser_simple_declaration (parser,
12810 				    /*function_definition_allowed_p=*/false,
12811 				    decl);
12812       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12813       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12814 	{
12815 	  /* It is a range-for, consume the ':'.  */
12816 	  cp_lexer_consume_token (parser->lexer);
12817 	  is_range_for = true;
12818 	  if (cxx_dialect < cxx11)
12819 	    pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12820 		     "range-based %<for%> loops only available with "
12821 		     "%<-std=c++11%> or %<-std=gnu++11%>");
12822 	}
12823       else
12824 	/* The ';' is not consumed yet because we told
12825 	   cp_parser_simple_declaration not to.  */
12826 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12827 
12828       if (cp_parser_parse_definitely (parser))
12829 	return is_range_for;
12830       /* If the tentative parse failed, then we shall need to look for an
12831 	 expression-statement.  */
12832     }
12833   /* If we are here, it is an expression-statement.  */
12834   cp_parser_expression_statement (parser, NULL_TREE);
12835   return false;
12836 }
12837 
12838 /* Parse a jump-statement.
12839 
12840    jump-statement:
12841      break ;
12842      continue ;
12843      return expression [opt] ;
12844      return braced-init-list ;
12845      goto identifier ;
12846 
12847    GNU extension:
12848 
12849    jump-statement:
12850      goto * expression ;
12851 
12852    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
12853 
12854 static tree
cp_parser_jump_statement(cp_parser * parser)12855 cp_parser_jump_statement (cp_parser* parser)
12856 {
12857   tree statement = error_mark_node;
12858   cp_token *token;
12859   enum rid keyword;
12860   unsigned char in_statement;
12861 
12862   /* Peek at the next token.  */
12863   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12864   if (!token)
12865     return error_mark_node;
12866 
12867   /* See what kind of keyword it is.  */
12868   keyword = token->keyword;
12869   switch (keyword)
12870     {
12871     case RID_BREAK:
12872       in_statement = parser->in_statement & ~IN_IF_STMT;
12873       switch (in_statement)
12874 	{
12875 	case 0:
12876 	  error_at (token->location, "break statement not within loop or switch");
12877 	  break;
12878 	default:
12879 	  gcc_assert ((in_statement & IN_SWITCH_STMT)
12880 		      || in_statement == IN_ITERATION_STMT);
12881 	  statement = finish_break_stmt ();
12882 	  if (in_statement == IN_ITERATION_STMT)
12883 	    break_maybe_infinite_loop ();
12884 	  break;
12885 	case IN_OMP_BLOCK:
12886 	  error_at (token->location, "invalid exit from OpenMP structured block");
12887 	  break;
12888 	case IN_OMP_FOR:
12889 	  error_at (token->location, "break statement used with OpenMP for loop");
12890 	  break;
12891 	}
12892       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12893       break;
12894 
12895     case RID_CONTINUE:
12896       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12897 	{
12898 	case 0:
12899 	  error_at (token->location, "continue statement not within a loop");
12900 	  break;
12901 	  /* Fall through.  */
12902 	case IN_ITERATION_STMT:
12903 	case IN_OMP_FOR:
12904 	  statement = finish_continue_stmt ();
12905 	  break;
12906 	case IN_OMP_BLOCK:
12907 	  error_at (token->location, "invalid exit from OpenMP structured block");
12908 	  break;
12909 	default:
12910 	  gcc_unreachable ();
12911 	}
12912       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12913       break;
12914 
12915     case RID_RETURN:
12916       {
12917 	tree expr;
12918 	bool expr_non_constant_p;
12919 
12920 	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12921 	  {
12922 	    cp_lexer_set_source_position (parser->lexer);
12923 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12924 	    expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12925 	  }
12926 	else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12927 	  expr = cp_parser_expression (parser);
12928 	else
12929 	  /* If the next token is a `;', then there is no
12930 	     expression.  */
12931 	  expr = NULL_TREE;
12932 	/* Build the return-statement.  */
12933 	if (current_function_auto_return_pattern && in_discarded_stmt)
12934 	  /* Don't deduce from a discarded return statement.  */;
12935 	else
12936 	  statement = finish_return_stmt (expr);
12937 	/* Look for the final `;'.  */
12938 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12939       }
12940       break;
12941 
12942     case RID_GOTO:
12943       if (parser->in_function_body
12944 	  && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12945 	{
12946 	  error ("%<goto%> in %<constexpr%> function");
12947 	  cp_function_chain->invalid_constexpr = true;
12948 	}
12949 
12950       /* Create the goto-statement.  */
12951       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12952 	{
12953 	  /* Issue a warning about this use of a GNU extension.  */
12954 	  pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12955 	  /* Consume the '*' token.  */
12956 	  cp_lexer_consume_token (parser->lexer);
12957 	  /* Parse the dependent expression.  */
12958 	  finish_goto_stmt (cp_parser_expression (parser));
12959 	}
12960       else
12961 	finish_goto_stmt (cp_parser_identifier (parser));
12962       /* Look for the final `;'.  */
12963       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12964       break;
12965 
12966     default:
12967       cp_parser_error (parser, "expected jump-statement");
12968       break;
12969     }
12970 
12971   return statement;
12972 }
12973 
12974 /* Parse a declaration-statement.
12975 
12976    declaration-statement:
12977      block-declaration  */
12978 
12979 static void
cp_parser_declaration_statement(cp_parser * parser)12980 cp_parser_declaration_statement (cp_parser* parser)
12981 {
12982   void *p;
12983 
12984   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12985   p = obstack_alloc (&declarator_obstack, 0);
12986 
12987  /* Parse the block-declaration.  */
12988   cp_parser_block_declaration (parser, /*statement_p=*/true);
12989 
12990   /* Free any declarators allocated.  */
12991   obstack_free (&declarator_obstack, p);
12992 }
12993 
12994 /* Some dependent statements (like `if (cond) statement'), are
12995    implicitly in their own scope.  In other words, if the statement is
12996    a single statement (as opposed to a compound-statement), it is
12997    none-the-less treated as if it were enclosed in braces.  Any
12998    declarations appearing in the dependent statement are out of scope
12999    after control passes that point.  This function parses a statement,
13000    but ensures that is in its own scope, even if it is not a
13001    compound-statement.
13002 
13003    If IF_P is not NULL, *IF_P is set to indicate whether the statement
13004    is a (possibly labeled) if statement which is not enclosed in
13005    braces and has an else clause.  This is used to implement
13006    -Wparentheses.
13007 
13008    CHAIN is a vector of if-else-if conditions.  This is used to implement
13009    -Wduplicated-cond.
13010 
13011    Returns the new statement.  */
13012 
13013 static tree
cp_parser_implicitly_scoped_statement(cp_parser * parser,bool * if_p,const token_indent_info & guard_tinfo,vec<tree> * chain)13014 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
13015 				       const token_indent_info &guard_tinfo,
13016 				       vec<tree> *chain)
13017 {
13018   tree statement;
13019   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
13020   location_t body_loc_after_labels = UNKNOWN_LOCATION;
13021   token_indent_info body_tinfo
13022     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13023 
13024   if (if_p != NULL)
13025     *if_p = false;
13026 
13027   /* Mark if () ; with a special NOP_EXPR.  */
13028   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13029     {
13030       cp_lexer_consume_token (parser->lexer);
13031       statement = add_stmt (build_empty_stmt (body_loc));
13032 
13033       if (guard_tinfo.keyword == RID_IF
13034 	  && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
13035 	warning_at (body_loc, OPT_Wempty_body,
13036 		    "suggest braces around empty body in an %<if%> statement");
13037       else if (guard_tinfo.keyword == RID_ELSE)
13038 	warning_at (body_loc, OPT_Wempty_body,
13039 		    "suggest braces around empty body in an %<else%> statement");
13040     }
13041   /* if a compound is opened, we simply parse the statement directly.  */
13042   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13043     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
13044   /* If the token is not a `{', then we must take special action.  */
13045   else
13046     {
13047       /* Create a compound-statement.  */
13048       statement = begin_compound_stmt (0);
13049       /* Parse the dependent-statement.  */
13050       cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
13051 			   &body_loc_after_labels);
13052       /* Finish the dummy compound-statement.  */
13053       finish_compound_stmt (statement);
13054     }
13055 
13056   token_indent_info next_tinfo
13057     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13058   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13059 
13060   if (body_loc_after_labels != UNKNOWN_LOCATION
13061       && next_tinfo.type != CPP_SEMICOLON)
13062     warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
13063 				    guard_tinfo.location, guard_tinfo.keyword);
13064 
13065   /* Return the statement.  */
13066   return statement;
13067 }
13068 
13069 /* For some dependent statements (like `while (cond) statement'), we
13070    have already created a scope.  Therefore, even if the dependent
13071    statement is a compound-statement, we do not want to create another
13072    scope.  */
13073 
13074 static void
cp_parser_already_scoped_statement(cp_parser * parser,bool * if_p,const token_indent_info & guard_tinfo)13075 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13076 				    const token_indent_info &guard_tinfo)
13077 {
13078   /* If the token is a `{', then we must take special action.  */
13079   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13080     {
13081       token_indent_info body_tinfo
13082 	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13083       location_t loc_after_labels = UNKNOWN_LOCATION;
13084 
13085       cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13086 			   &loc_after_labels);
13087       token_indent_info next_tinfo
13088 	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13089       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13090 
13091       if (loc_after_labels != UNKNOWN_LOCATION
13092 	  && next_tinfo.type != CPP_SEMICOLON)
13093 	warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13094 					guard_tinfo.location,
13095 					guard_tinfo.keyword);
13096     }
13097   else
13098     {
13099       /* Avoid calling cp_parser_compound_statement, so that we
13100 	 don't create a new scope.  Do everything else by hand.  */
13101       matching_braces braces;
13102       braces.require_open (parser);
13103       /* If the next keyword is `__label__' we have a label declaration.  */
13104       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13105 	cp_parser_label_declaration (parser);
13106       /* Parse an (optional) statement-seq.  */
13107       cp_parser_statement_seq_opt (parser, NULL_TREE);
13108       braces.require_close (parser);
13109     }
13110 }
13111 
13112 /* Declarations [gram.dcl.dcl] */
13113 
13114 /* Parse an optional declaration-sequence.
13115 
13116    declaration-seq:
13117      declaration
13118      declaration-seq declaration  */
13119 
13120 static void
cp_parser_declaration_seq_opt(cp_parser * parser)13121 cp_parser_declaration_seq_opt (cp_parser* parser)
13122 {
13123   while (true)
13124     {
13125       cp_token *token = cp_lexer_peek_token (parser->lexer);
13126 
13127       if (token->type == CPP_CLOSE_BRACE
13128 	  || token->type == CPP_EOF)
13129 	break;
13130       else
13131 	cp_parser_toplevel_declaration (parser);
13132     }
13133 }
13134 
13135 /* Parse a declaration.
13136 
13137    declaration:
13138      block-declaration
13139      function-definition
13140      template-declaration
13141      explicit-instantiation
13142      explicit-specialization
13143      linkage-specification
13144      namespace-definition
13145 
13146    C++17:
13147      deduction-guide
13148 
13149    GNU extension:
13150 
13151    declaration:
13152       __extension__ declaration */
13153 
13154 static void
cp_parser_declaration(cp_parser * parser)13155 cp_parser_declaration (cp_parser* parser)
13156 {
13157   cp_token token1;
13158   cp_token token2;
13159   int saved_pedantic;
13160   void *p;
13161   tree attributes = NULL_TREE;
13162 
13163   /* Check for the `__extension__' keyword.  */
13164   if (cp_parser_extension_opt (parser, &saved_pedantic))
13165     {
13166       /* Parse the qualified declaration.  */
13167       cp_parser_declaration (parser);
13168       /* Restore the PEDANTIC flag.  */
13169       pedantic = saved_pedantic;
13170 
13171       return;
13172     }
13173 
13174   /* Try to figure out what kind of declaration is present.  */
13175   token1 = *cp_lexer_peek_token (parser->lexer);
13176 
13177   if (token1.type != CPP_EOF)
13178     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13179   else
13180     {
13181       token2.type = CPP_EOF;
13182       token2.keyword = RID_MAX;
13183     }
13184 
13185   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
13186   p = obstack_alloc (&declarator_obstack, 0);
13187 
13188   /* If the next token is `extern' and the following token is a string
13189      literal, then we have a linkage specification.  */
13190   if (token1.keyword == RID_EXTERN
13191       && cp_parser_is_pure_string_literal (&token2))
13192     cp_parser_linkage_specification (parser);
13193   /* If the next token is `template', then we have either a template
13194      declaration, an explicit instantiation, or an explicit
13195      specialization.  */
13196   else if (token1.keyword == RID_TEMPLATE)
13197     {
13198       /* `template <>' indicates a template specialization.  */
13199       if (token2.type == CPP_LESS
13200 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13201 	cp_parser_explicit_specialization (parser);
13202       /* `template <' indicates a template declaration.  */
13203       else if (token2.type == CPP_LESS)
13204 	cp_parser_template_declaration (parser, /*member_p=*/false);
13205       /* Anything else must be an explicit instantiation.  */
13206       else
13207 	cp_parser_explicit_instantiation (parser);
13208     }
13209   /* If the next token is `export', then we have a template
13210      declaration.  */
13211   else if (token1.keyword == RID_EXPORT)
13212     cp_parser_template_declaration (parser, /*member_p=*/false);
13213   /* If the next token is `extern', 'static' or 'inline' and the one
13214      after that is `template', we have a GNU extended explicit
13215      instantiation directive.  */
13216   else if (cp_parser_allow_gnu_extensions_p (parser)
13217 	   && (token1.keyword == RID_EXTERN
13218 	       || token1.keyword == RID_STATIC
13219 	       || token1.keyword == RID_INLINE)
13220 	   && token2.keyword == RID_TEMPLATE)
13221     cp_parser_explicit_instantiation (parser);
13222   /* If the next token is `namespace', check for a named or unnamed
13223      namespace definition.  */
13224   else if (token1.keyword == RID_NAMESPACE
13225 	   && (/* A named namespace definition.  */
13226 	       (token2.type == CPP_NAME
13227 		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13228 		    != CPP_EQ))
13229                || (token2.type == CPP_OPEN_SQUARE
13230                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13231                    == CPP_OPEN_SQUARE)
13232 	       /* An unnamed namespace definition.  */
13233 	       || token2.type == CPP_OPEN_BRACE
13234 	       || token2.keyword == RID_ATTRIBUTE))
13235     cp_parser_namespace_definition (parser);
13236   /* An inline (associated) namespace definition.  */
13237   else if (token1.keyword == RID_INLINE
13238 	   && token2.keyword == RID_NAMESPACE)
13239     cp_parser_namespace_definition (parser);
13240   /* Objective-C++ declaration/definition.  */
13241   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13242     cp_parser_objc_declaration (parser, NULL_TREE);
13243   else if (c_dialect_objc ()
13244 	   && token1.keyword == RID_ATTRIBUTE
13245 	   && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13246     cp_parser_objc_declaration (parser, attributes);
13247   /* At this point we may have a template declared by a concept
13248      introduction.  */
13249   else if (flag_concepts
13250 	   && cp_parser_template_declaration_after_export (parser,
13251 							   /*member_p=*/false))
13252     /* We did.  */;
13253   else
13254     /* Try to parse a block-declaration, or a function-definition.  */
13255     cp_parser_block_declaration (parser, /*statement_p=*/false);
13256 
13257   /* Free any declarators allocated.  */
13258   obstack_free (&declarator_obstack, p);
13259 }
13260 
13261 /* Parse a namespace-scope declaration.  */
13262 
13263 static void
cp_parser_toplevel_declaration(cp_parser * parser)13264 cp_parser_toplevel_declaration (cp_parser* parser)
13265 {
13266   cp_token *token = cp_lexer_peek_token (parser->lexer);
13267 
13268   if (token->type == CPP_PRAGMA)
13269     /* A top-level declaration can consist solely of a #pragma.  A
13270        nested declaration cannot, so this is done here and not in
13271        cp_parser_declaration.  (A #pragma at block scope is
13272        handled in cp_parser_statement.)  */
13273     cp_parser_pragma (parser, pragma_external, NULL);
13274   else if (token->type == CPP_SEMICOLON)
13275     {
13276       /* A declaration consisting of a single semicolon is
13277 	 invalid.  Allow it unless we're being pedantic.  */
13278       cp_lexer_consume_token (parser->lexer);
13279       if (!in_system_header_at (input_location))
13280 	pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13281     }
13282   else
13283     /* Parse the declaration itself.  */
13284     cp_parser_declaration (parser);
13285 }
13286 
13287 /* Parse a block-declaration.
13288 
13289    block-declaration:
13290      simple-declaration
13291      asm-definition
13292      namespace-alias-definition
13293      using-declaration
13294      using-directive
13295 
13296    GNU Extension:
13297 
13298    block-declaration:
13299      __extension__ block-declaration
13300 
13301    C++0x Extension:
13302 
13303    block-declaration:
13304      static_assert-declaration
13305 
13306    If STATEMENT_P is TRUE, then this block-declaration is occurring as
13307    part of a declaration-statement.  */
13308 
13309 static void
cp_parser_block_declaration(cp_parser * parser,bool statement_p)13310 cp_parser_block_declaration (cp_parser *parser,
13311 			     bool      statement_p)
13312 {
13313   cp_token *token1;
13314   int saved_pedantic;
13315 
13316   /* Check for the `__extension__' keyword.  */
13317   if (cp_parser_extension_opt (parser, &saved_pedantic))
13318     {
13319       /* Parse the qualified declaration.  */
13320       cp_parser_block_declaration (parser, statement_p);
13321       /* Restore the PEDANTIC flag.  */
13322       pedantic = saved_pedantic;
13323 
13324       return;
13325     }
13326 
13327   /* Peek at the next token to figure out which kind of declaration is
13328      present.  */
13329   token1 = cp_lexer_peek_token (parser->lexer);
13330 
13331   /* If the next keyword is `asm', we have an asm-definition.  */
13332   if (token1->keyword == RID_ASM)
13333     {
13334       if (statement_p)
13335 	cp_parser_commit_to_tentative_parse (parser);
13336       cp_parser_asm_definition (parser);
13337     }
13338   /* If the next keyword is `namespace', we have a
13339      namespace-alias-definition.  */
13340   else if (token1->keyword == RID_NAMESPACE)
13341     cp_parser_namespace_alias_definition (parser);
13342   /* If the next keyword is `using', we have a
13343      using-declaration, a using-directive, or an alias-declaration.  */
13344   else if (token1->keyword == RID_USING)
13345     {
13346       cp_token *token2;
13347 
13348       if (statement_p)
13349 	cp_parser_commit_to_tentative_parse (parser);
13350       /* If the token after `using' is `namespace', then we have a
13351 	 using-directive.  */
13352       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13353       if (token2->keyword == RID_NAMESPACE)
13354 	cp_parser_using_directive (parser);
13355       /* If the second token after 'using' is '=', then we have an
13356 	 alias-declaration.  */
13357       else if (cxx_dialect >= cxx11
13358 	       && token2->type == CPP_NAME
13359 	       && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13360 		   || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13361 	cp_parser_alias_declaration (parser);
13362       /* Otherwise, it's a using-declaration.  */
13363       else
13364 	cp_parser_using_declaration (parser,
13365 				     /*access_declaration_p=*/false);
13366     }
13367   /* If the next keyword is `__label__' we have a misplaced label
13368      declaration.  */
13369   else if (token1->keyword == RID_LABEL)
13370     {
13371       cp_lexer_consume_token (parser->lexer);
13372       error_at (token1->location, "%<__label__%> not at the beginning of a block");
13373       cp_parser_skip_to_end_of_statement (parser);
13374       /* If the next token is now a `;', consume it.  */
13375       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13376 	cp_lexer_consume_token (parser->lexer);
13377     }
13378   /* If the next token is `static_assert' we have a static assertion.  */
13379   else if (token1->keyword == RID_STATIC_ASSERT)
13380     cp_parser_static_assert (parser, /*member_p=*/false);
13381   /* Anything else must be a simple-declaration.  */
13382   else
13383     cp_parser_simple_declaration (parser, !statement_p,
13384 				  /*maybe_range_for_decl*/NULL);
13385 }
13386 
13387 /* Parse a simple-declaration.
13388 
13389    simple-declaration:
13390      decl-specifier-seq [opt] init-declarator-list [opt] ;
13391      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13392        brace-or-equal-initializer ;
13393 
13394    init-declarator-list:
13395      init-declarator
13396      init-declarator-list , init-declarator
13397 
13398    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13399    function-definition as a simple-declaration.
13400 
13401    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13402    parsed declaration if it is an uninitialized single declarator not followed
13403    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13404    if present, will not be consumed.  */
13405 
13406 static void
cp_parser_simple_declaration(cp_parser * parser,bool function_definition_allowed_p,tree * maybe_range_for_decl)13407 cp_parser_simple_declaration (cp_parser* parser,
13408 			      bool function_definition_allowed_p,
13409 			      tree *maybe_range_for_decl)
13410 {
13411   cp_decl_specifier_seq decl_specifiers;
13412   int declares_class_or_enum;
13413   bool saw_declarator;
13414   location_t comma_loc = UNKNOWN_LOCATION;
13415   location_t init_loc = UNKNOWN_LOCATION;
13416 
13417   if (maybe_range_for_decl)
13418     *maybe_range_for_decl = NULL_TREE;
13419 
13420   /* Defer access checks until we know what is being declared; the
13421      checks for names appearing in the decl-specifier-seq should be
13422      done as if we were in the scope of the thing being declared.  */
13423   push_deferring_access_checks (dk_deferred);
13424 
13425   /* Parse the decl-specifier-seq.  We have to keep track of whether
13426      or not the decl-specifier-seq declares a named class or
13427      enumeration type, since that is the only case in which the
13428      init-declarator-list is allowed to be empty.
13429 
13430      [dcl.dcl]
13431 
13432      In a simple-declaration, the optional init-declarator-list can be
13433      omitted only when declaring a class or enumeration, that is when
13434      the decl-specifier-seq contains either a class-specifier, an
13435      elaborated-type-specifier, or an enum-specifier.  */
13436   cp_parser_decl_specifier_seq (parser,
13437 				CP_PARSER_FLAGS_OPTIONAL,
13438 				&decl_specifiers,
13439 				&declares_class_or_enum);
13440   /* We no longer need to defer access checks.  */
13441   stop_deferring_access_checks ();
13442 
13443   /* In a block scope, a valid declaration must always have a
13444      decl-specifier-seq.  By not trying to parse declarators, we can
13445      resolve the declaration/expression ambiguity more quickly.  */
13446   if (!function_definition_allowed_p
13447       && !decl_specifiers.any_specifiers_p)
13448     {
13449       cp_parser_error (parser, "expected declaration");
13450       goto done;
13451     }
13452 
13453   /* If the next two tokens are both identifiers, the code is
13454      erroneous. The usual cause of this situation is code like:
13455 
13456        T t;
13457 
13458      where "T" should name a type -- but does not.  */
13459   if (!decl_specifiers.any_type_specifiers_p
13460       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13461     {
13462       /* If parsing tentatively, we should commit; we really are
13463 	 looking at a declaration.  */
13464       cp_parser_commit_to_tentative_parse (parser);
13465       /* Give up.  */
13466       goto done;
13467     }
13468 
13469   cp_parser_maybe_commit_to_declaration (parser,
13470 					 decl_specifiers.any_specifiers_p);
13471 
13472   /* Look for C++17 decomposition declaration.  */
13473   for (size_t n = 1; ; n++)
13474     if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13475 	|| cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13476       continue;
13477     else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13478 	     && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13479 	     && decl_specifiers.any_specifiers_p)
13480       {
13481 	tree decl
13482 	  = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13483 						 maybe_range_for_decl,
13484 						 &init_loc);
13485 
13486 	/* The next token should be either a `,' or a `;'.  */
13487 	cp_token *token = cp_lexer_peek_token (parser->lexer);
13488 	/* If it's a `;', we are done.  */
13489 	if (token->type == CPP_SEMICOLON)
13490 	  goto finish;
13491 	else if (maybe_range_for_decl)
13492 	  {
13493 	    if (*maybe_range_for_decl == NULL_TREE)
13494 	      *maybe_range_for_decl = error_mark_node;
13495 	    goto finish;
13496 	  }
13497 	/* Anything else is an error.  */
13498 	else
13499 	  {
13500 	    /* If we have already issued an error message we don't need
13501 	       to issue another one.  */
13502 	    if ((decl != error_mark_node
13503 		 && DECL_INITIAL (decl) != error_mark_node)
13504 		|| cp_parser_uncommitted_to_tentative_parse_p (parser))
13505 	      cp_parser_error (parser, "expected %<;%>");
13506 	    /* Skip tokens until we reach the end of the statement.  */
13507 	    cp_parser_skip_to_end_of_statement (parser);
13508 	    /* If the next token is now a `;', consume it.  */
13509 	    if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13510 	      cp_lexer_consume_token (parser->lexer);
13511 	    goto done;
13512 	  }
13513       }
13514     else
13515       break;
13516 
13517   tree last_type;
13518   bool auto_specifier_p;
13519   /* NULL_TREE if both variable and function declaration are allowed,
13520      error_mark_node if function declaration are not allowed and
13521      a FUNCTION_DECL that should be diagnosed if it is followed by
13522      variable declarations.  */
13523   tree auto_function_declaration;
13524 
13525   last_type = NULL_TREE;
13526   auto_specifier_p
13527     = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13528   auto_function_declaration = NULL_TREE;
13529 
13530   /* Keep going until we hit the `;' at the end of the simple
13531      declaration.  */
13532   saw_declarator = false;
13533   while (cp_lexer_next_token_is_not (parser->lexer,
13534 				     CPP_SEMICOLON))
13535     {
13536       cp_token *token;
13537       bool function_definition_p;
13538       tree decl;
13539       tree auto_result = NULL_TREE;
13540 
13541       if (saw_declarator)
13542 	{
13543 	  /* If we are processing next declarator, comma is expected */
13544 	  token = cp_lexer_peek_token (parser->lexer);
13545 	  gcc_assert (token->type == CPP_COMMA);
13546 	  cp_lexer_consume_token (parser->lexer);
13547 	  if (maybe_range_for_decl)
13548 	    {
13549 	      *maybe_range_for_decl = error_mark_node;
13550 	      if (comma_loc == UNKNOWN_LOCATION)
13551 		comma_loc = token->location;
13552 	    }
13553 	}
13554       else
13555 	saw_declarator = true;
13556 
13557       /* Parse the init-declarator.  */
13558       decl = cp_parser_init_declarator (parser,
13559 					CP_PARSER_FLAGS_NONE,
13560 					&decl_specifiers,
13561 					/*checks=*/NULL,
13562 					function_definition_allowed_p,
13563 					/*member_p=*/false,
13564 					declares_class_or_enum,
13565 					&function_definition_p,
13566 					maybe_range_for_decl,
13567 					&init_loc,
13568 					&auto_result);
13569       /* If an error occurred while parsing tentatively, exit quickly.
13570 	 (That usually happens when in the body of a function; each
13571 	 statement is treated as a declaration-statement until proven
13572 	 otherwise.)  */
13573       if (cp_parser_error_occurred (parser))
13574 	goto done;
13575 
13576       if (auto_specifier_p && cxx_dialect >= cxx14)
13577 	{
13578 	  /* If the init-declarator-list contains more than one
13579 	     init-declarator, they shall all form declarations of
13580 	     variables.  */
13581 	  if (auto_function_declaration == NULL_TREE)
13582 	    auto_function_declaration
13583 	      = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13584 	  else if (TREE_CODE (decl) == FUNCTION_DECL
13585 		   || auto_function_declaration != error_mark_node)
13586 	    {
13587 	      error_at (decl_specifiers.locations[ds_type_spec],
13588 			"non-variable %qD in declaration with more than one "
13589 			"declarator with placeholder type",
13590 			TREE_CODE (decl) == FUNCTION_DECL
13591 			? decl : auto_function_declaration);
13592 	      auto_function_declaration = error_mark_node;
13593 	    }
13594 	}
13595 
13596       if (auto_result
13597 	  && (!processing_template_decl || !type_uses_auto (auto_result)))
13598 	{
13599 	  if (last_type
13600 	      && last_type != error_mark_node
13601 	      && !same_type_p (auto_result, last_type))
13602 	    {
13603 	      /* If the list of declarators contains more than one declarator,
13604 		 the type of each declared variable is determined as described
13605 		 above. If the type deduced for the template parameter U is not
13606 		 the same in each deduction, the program is ill-formed.  */
13607 	      error_at (decl_specifiers.locations[ds_type_spec],
13608 			"inconsistent deduction for %qT: %qT and then %qT",
13609 			decl_specifiers.type, last_type, auto_result);
13610 	      last_type = error_mark_node;
13611 	    }
13612 	  else
13613 	    last_type = auto_result;
13614 	}
13615 
13616       /* Handle function definitions specially.  */
13617       if (function_definition_p)
13618 	{
13619 	  /* If the next token is a `,', then we are probably
13620 	     processing something like:
13621 
13622 	       void f() {}, *p;
13623 
13624 	     which is erroneous.  */
13625 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13626 	    {
13627 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
13628 	      error_at (token->location,
13629 			"mixing"
13630 			" declarations and function-definitions is forbidden");
13631 	    }
13632 	  /* Otherwise, we're done with the list of declarators.  */
13633 	  else
13634 	    {
13635 	      pop_deferring_access_checks ();
13636 	      return;
13637 	    }
13638 	}
13639       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13640 	*maybe_range_for_decl = decl;
13641       /* The next token should be either a `,' or a `;'.  */
13642       token = cp_lexer_peek_token (parser->lexer);
13643       /* If it's a `,', there are more declarators to come.  */
13644       if (token->type == CPP_COMMA)
13645 	/* will be consumed next time around */;
13646       /* If it's a `;', we are done.  */
13647       else if (token->type == CPP_SEMICOLON)
13648 	break;
13649       else if (maybe_range_for_decl)
13650 	{
13651 	  if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13652 	    permerror (decl_specifiers.locations[ds_type_spec],
13653 		       "types may not be defined in a for-range-declaration");
13654 	  break;
13655 	}
13656       /* Anything else is an error.  */
13657       else
13658 	{
13659 	  /* If we have already issued an error message we don't need
13660 	     to issue another one.  */
13661 	  if ((decl != error_mark_node
13662 	       && DECL_INITIAL (decl) != error_mark_node)
13663 	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
13664 	    cp_parser_error (parser, "expected %<,%> or %<;%>");
13665 	  /* Skip tokens until we reach the end of the statement.  */
13666 	  cp_parser_skip_to_end_of_statement (parser);
13667 	  /* If the next token is now a `;', consume it.  */
13668 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13669 	    cp_lexer_consume_token (parser->lexer);
13670 	  goto done;
13671 	}
13672       /* After the first time around, a function-definition is not
13673 	 allowed -- even if it was OK at first.  For example:
13674 
13675 	   int i, f() {}
13676 
13677 	 is not valid.  */
13678       function_definition_allowed_p = false;
13679     }
13680 
13681   /* Issue an error message if no declarators are present, and the
13682      decl-specifier-seq does not itself declare a class or
13683      enumeration: [dcl.dcl]/3.  */
13684   if (!saw_declarator)
13685     {
13686       if (cp_parser_declares_only_class_p (parser))
13687 	{
13688 	  if (!declares_class_or_enum
13689 	      && decl_specifiers.type
13690 	      && OVERLOAD_TYPE_P (decl_specifiers.type))
13691 	    /* Ensure an error is issued anyway when finish_decltype_type,
13692 	       called via cp_parser_decl_specifier_seq, returns a class or
13693 	       an enumeration (c++/51786).  */
13694 	    decl_specifiers.type = NULL_TREE;
13695 	  shadow_tag (&decl_specifiers);
13696 	}
13697       /* Perform any deferred access checks.  */
13698       perform_deferred_access_checks (tf_warning_or_error);
13699     }
13700 
13701   /* Consume the `;'.  */
13702  finish:
13703   if (!maybe_range_for_decl)
13704     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13705   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13706     {
13707       if (init_loc != UNKNOWN_LOCATION)
13708 	error_at (init_loc, "initializer in range-based %<for%> loop");
13709       if (comma_loc != UNKNOWN_LOCATION)
13710 	error_at (comma_loc,
13711 		  "multiple declarations in range-based %<for%> loop");
13712     }
13713 
13714  done:
13715   pop_deferring_access_checks ();
13716 }
13717 
13718 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13719      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13720        initializer ;  */
13721 
13722 static tree
cp_parser_decomposition_declaration(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,tree * maybe_range_for_decl,location_t * init_loc)13723 cp_parser_decomposition_declaration (cp_parser *parser,
13724 				     cp_decl_specifier_seq *decl_specifiers,
13725 				     tree *maybe_range_for_decl,
13726 				     location_t *init_loc)
13727 {
13728   cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13729   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13730   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13731 
13732   /* Parse the identifier-list.  */
13733   auto_vec<cp_expr, 10> v;
13734   if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13735     while (true)
13736       {
13737 	cp_expr e = cp_parser_identifier (parser);
13738 	if (e.get_value () == error_mark_node)
13739 	  break;
13740 	v.safe_push (e);
13741 	if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13742 	  break;
13743 	cp_lexer_consume_token (parser->lexer);
13744       }
13745 
13746   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13747   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13748     {
13749       end_loc = UNKNOWN_LOCATION;
13750       cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13751 					       false);
13752       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13753 	cp_lexer_consume_token (parser->lexer);
13754       else
13755 	{
13756 	  cp_parser_skip_to_end_of_statement (parser);
13757 	  return error_mark_node;
13758 	}
13759     }
13760 
13761   if (cxx_dialect < cxx17)
13762     pedwarn (loc, 0, "structured bindings only available with "
13763 		     "%<-std=c++17%> or %<-std=gnu++17%>");
13764 
13765   tree pushed_scope;
13766   cp_declarator *declarator = make_declarator (cdk_decomp);
13767   loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13768   declarator->id_loc = loc;
13769   if (ref_qual != REF_QUAL_NONE)
13770     declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13771 					    ref_qual == REF_QUAL_RVALUE,
13772 					    NULL_TREE);
13773   tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13774 			  NULL_TREE, decl_specifiers->attributes,
13775 			  &pushed_scope);
13776   tree orig_decl = decl;
13777 
13778   unsigned int i;
13779   cp_expr e;
13780   cp_decl_specifier_seq decl_specs;
13781   clear_decl_specs (&decl_specs);
13782   decl_specs.type = make_auto ();
13783   tree prev = decl;
13784   FOR_EACH_VEC_ELT (v, i, e)
13785     {
13786       if (i == 0)
13787 	declarator = make_id_declarator (NULL_TREE, e.get_value (),
13788 					 sfk_none, e.get_location ());
13789       else
13790 	{
13791 	  declarator->u.id.unqualified_name = e.get_value ();
13792 	  declarator->id_loc = e.get_location ();
13793 	}
13794       tree elt_pushed_scope;
13795       tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13796 			       NULL_TREE, NULL_TREE, &elt_pushed_scope);
13797       if (decl2 == error_mark_node)
13798 	decl = error_mark_node;
13799       else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13800 	{
13801 	  /* Ensure we've diagnosed redeclaration if we aren't creating
13802 	     a new VAR_DECL.  */
13803 	  gcc_assert (errorcount);
13804 	  decl = error_mark_node;
13805 	}
13806       else
13807 	prev = decl2;
13808       if (elt_pushed_scope)
13809 	pop_scope (elt_pushed_scope);
13810     }
13811 
13812   if (v.is_empty ())
13813     {
13814       error_at (loc, "empty structured binding declaration");
13815       decl = error_mark_node;
13816     }
13817 
13818   if (maybe_range_for_decl == NULL
13819       || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13820     {
13821       bool non_constant_p = false, is_direct_init = false;
13822       *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13823       tree initializer = cp_parser_initializer (parser, &is_direct_init,
13824 						&non_constant_p);
13825       if (initializer == NULL_TREE
13826 	  || (TREE_CODE (initializer) == TREE_LIST
13827 	      && TREE_CHAIN (initializer))
13828 	  || (is_direct_init
13829 	      && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13830 	      && CONSTRUCTOR_NELTS (initializer) != 1))
13831 	{
13832 	  error_at (loc, "invalid initializer for structured binding "
13833 		    "declaration");
13834 	  initializer = error_mark_node;
13835 	}
13836 
13837       if (decl != error_mark_node)
13838 	{
13839 	  cp_maybe_mangle_decomp (decl, prev, v.length ());
13840 	  cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13841 			  is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13842 	  cp_finish_decomp (decl, prev, v.length ());
13843 	}
13844     }
13845   else if (decl != error_mark_node)
13846     {
13847       *maybe_range_for_decl = prev;
13848       /* Ensure DECL_VALUE_EXPR is created for all the decls but
13849 	 the underlying DECL.  */
13850       cp_finish_decomp (decl, prev, v.length ());
13851     }
13852 
13853   if (pushed_scope)
13854     pop_scope (pushed_scope);
13855 
13856   if (decl == error_mark_node && DECL_P (orig_decl))
13857     {
13858       if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13859 	SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13860     }
13861 
13862   return decl;
13863 }
13864 
13865 /* Parse a decl-specifier-seq.
13866 
13867    decl-specifier-seq:
13868      decl-specifier-seq [opt] decl-specifier
13869      decl-specifier attribute-specifier-seq [opt] (C++11)
13870 
13871    decl-specifier:
13872      storage-class-specifier
13873      type-specifier
13874      function-specifier
13875      friend
13876      typedef
13877 
13878    GNU Extension:
13879 
13880    decl-specifier:
13881      attributes
13882 
13883    Concepts Extension:
13884 
13885    decl-specifier:
13886      concept
13887 
13888    Set *DECL_SPECS to a representation of the decl-specifier-seq.
13889 
13890    The parser flags FLAGS is used to control type-specifier parsing.
13891 
13892    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13893    flags:
13894 
13895      1: one of the decl-specifiers is an elaborated-type-specifier
13896 	(i.e., a type declaration)
13897      2: one of the decl-specifiers is an enum-specifier or a
13898 	class-specifier (i.e., a type definition)
13899 
13900    */
13901 
13902 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)13903 cp_parser_decl_specifier_seq (cp_parser* parser,
13904 			      cp_parser_flags flags,
13905 			      cp_decl_specifier_seq *decl_specs,
13906 			      int* declares_class_or_enum)
13907 {
13908   bool constructor_possible_p = !parser->in_declarator_p;
13909   bool found_decl_spec = false;
13910   cp_token *start_token = NULL;
13911   cp_decl_spec ds;
13912 
13913   /* Clear DECL_SPECS.  */
13914   clear_decl_specs (decl_specs);
13915 
13916   /* Assume no class or enumeration type is declared.  */
13917   *declares_class_or_enum = 0;
13918 
13919   /* Keep reading specifiers until there are no more to read.  */
13920   while (true)
13921     {
13922       bool constructor_p;
13923       cp_token *token;
13924       ds = ds_last;
13925 
13926       /* Peek at the next token.  */
13927       token = cp_lexer_peek_token (parser->lexer);
13928 
13929       /* Save the first token of the decl spec list for error
13930          reporting.  */
13931       if (!start_token)
13932 	start_token = token;
13933       /* Handle attributes.  */
13934       if (cp_next_tokens_can_be_attribute_p (parser))
13935 	{
13936 	  /* Parse the attributes.  */
13937 	  tree attrs = cp_parser_attributes_opt (parser);
13938 
13939 	  /* In a sequence of declaration specifiers, c++11 attributes
13940 	     appertain to the type that precede them. In that case
13941 	     [dcl.spec]/1 says:
13942 
13943 	         The attribute-specifier-seq affects the type only for
13944 		 the declaration it appears in, not other declarations
13945 		 involving the same type.
13946 
13947              But for now let's force the user to position the
13948              attribute either at the beginning of the declaration or
13949              after the declarator-id, which would clearly mean that it
13950              applies to the declarator.  */
13951 	  if (cxx11_attribute_p (attrs))
13952 	    {
13953 	      if (!found_decl_spec)
13954 		/* The c++11 attribute is at the beginning of the
13955 		   declaration.  It appertains to the entity being
13956 		   declared.  */;
13957 	      else
13958 		{
13959 		  if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13960 		    {
13961 		      /*  This is an attribute following a
13962 			  class-specifier.  */
13963 		      if (decl_specs->type_definition_p)
13964 			warn_misplaced_attr_for_class_type (token->location,
13965 							    decl_specs->type);
13966 		      attrs = NULL_TREE;
13967 		    }
13968 		  else
13969 		    {
13970 		      decl_specs->std_attributes
13971 			= attr_chainon (decl_specs->std_attributes, attrs);
13972 		      if (decl_specs->locations[ds_std_attribute] == 0)
13973 			decl_specs->locations[ds_std_attribute] = token->location;
13974 		    }
13975 		  continue;
13976 		}
13977 	    }
13978 
13979 	  decl_specs->attributes
13980 	    = attr_chainon (decl_specs->attributes, attrs);
13981 	  if (decl_specs->locations[ds_attribute] == 0)
13982 	    decl_specs->locations[ds_attribute] = token->location;
13983 	  continue;
13984 	}
13985       /* Assume we will find a decl-specifier keyword.  */
13986       found_decl_spec = true;
13987       /* If the next token is an appropriate keyword, we can simply
13988 	 add it to the list.  */
13989       switch (token->keyword)
13990 	{
13991 	  /* decl-specifier:
13992 	       friend
13993                constexpr */
13994 	case RID_FRIEND:
13995 	  if (!at_class_scope_p ())
13996 	    {
13997 	      gcc_rich_location richloc (token->location);
13998 	      richloc.add_fixit_remove ();
13999 	      error_at (&richloc, "%<friend%> used outside of class");
14000 	      cp_lexer_purge_token (parser->lexer);
14001 	    }
14002 	  else
14003 	    {
14004 	      ds = ds_friend;
14005 	      /* Consume the token.  */
14006 	      cp_lexer_consume_token (parser->lexer);
14007 	    }
14008 	  break;
14009 
14010         case RID_CONSTEXPR:
14011 	  ds = ds_constexpr;
14012           cp_lexer_consume_token (parser->lexer);
14013           break;
14014 
14015         case RID_CONCEPT:
14016           ds = ds_concept;
14017           cp_lexer_consume_token (parser->lexer);
14018 
14019 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14020 	    break;
14021 
14022 	  /* In C++20 a concept definition is just 'concept name = expr;'
14023 	     Support that syntax by pretending we've seen 'bool'.  */
14024 	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14025 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
14026 	    {
14027 	      cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
14028 					    token, /*type_definition*/false);
14029 	      decl_specs->any_type_specifiers_p = true;
14030 	    }
14031           break;
14032 
14033 	  /* function-specifier:
14034 	       inline
14035 	       virtual
14036 	       explicit  */
14037 	case RID_INLINE:
14038 	case RID_VIRTUAL:
14039 	case RID_EXPLICIT:
14040 	  cp_parser_function_specifier_opt (parser, decl_specs);
14041 	  break;
14042 
14043 	  /* decl-specifier:
14044 	       typedef  */
14045 	case RID_TYPEDEF:
14046 	  ds = ds_typedef;
14047 	  /* Consume the token.  */
14048 	  cp_lexer_consume_token (parser->lexer);
14049 
14050 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14051 	    break;
14052 
14053 	  /* A constructor declarator cannot appear in a typedef.  */
14054 	  constructor_possible_p = false;
14055 	  /* The "typedef" keyword can only occur in a declaration; we
14056 	     may as well commit at this point.  */
14057 	  cp_parser_commit_to_tentative_parse (parser);
14058 
14059           if (decl_specs->storage_class != sc_none)
14060             decl_specs->conflicting_specifiers_p = true;
14061 	  break;
14062 
14063 	  /* storage-class-specifier:
14064 	       auto
14065 	       register
14066 	       static
14067 	       extern
14068 	       mutable
14069 
14070 	     GNU Extension:
14071 	       thread  */
14072 	case RID_AUTO:
14073           if (cxx_dialect == cxx98)
14074             {
14075 	      /* Consume the token.  */
14076 	      cp_lexer_consume_token (parser->lexer);
14077 
14078 	      /* Complain about `auto' as a storage specifier, if
14079 		 we're complaining about C++0x compatibility.  */
14080 	      gcc_rich_location richloc (token->location);
14081 	      richloc.add_fixit_remove ();
14082 	      warning_at (&richloc, OPT_Wc__11_compat,
14083 			  "%<auto%> changes meaning in C++11; "
14084 			  "please remove it");
14085 
14086               /* Set the storage class anyway.  */
14087               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
14088 					   token);
14089             }
14090           else
14091 	    /* C++0x auto type-specifier.  */
14092 	    found_decl_spec = false;
14093           break;
14094 
14095 	case RID_REGISTER:
14096 	case RID_STATIC:
14097 	case RID_EXTERN:
14098 	case RID_MUTABLE:
14099 	  /* Consume the token.  */
14100 	  cp_lexer_consume_token (parser->lexer);
14101           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14102 				       token);
14103 	  break;
14104 	case RID_THREAD:
14105 	  /* Consume the token.  */
14106 	  ds = ds_thread;
14107 	  cp_lexer_consume_token (parser->lexer);
14108 	  break;
14109 
14110 	default:
14111 	  /* We did not yet find a decl-specifier yet.  */
14112 	  found_decl_spec = false;
14113 	  break;
14114 	}
14115 
14116       if (found_decl_spec
14117 	  && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14118 	  && token->keyword != RID_CONSTEXPR)
14119 	error ("decl-specifier invalid in condition");
14120 
14121       if (found_decl_spec
14122 	  && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14123 	  && token->keyword != RID_MUTABLE
14124 	  && token->keyword != RID_CONSTEXPR)
14125 	error_at (token->location, "%qD invalid in lambda",
14126 		  ridpointers[token->keyword]);
14127 
14128       if (ds != ds_last)
14129 	set_and_check_decl_spec_loc (decl_specs, ds, token);
14130 
14131       /* Constructors are a special case.  The `S' in `S()' is not a
14132 	 decl-specifier; it is the beginning of the declarator.  */
14133       constructor_p
14134 	= (!found_decl_spec
14135 	   && constructor_possible_p
14136 	   && (cp_parser_constructor_declarator_p
14137 	       (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
14138 							 ds_friend))));
14139 
14140       /* If we don't have a DECL_SPEC yet, then we must be looking at
14141 	 a type-specifier.  */
14142       if (!found_decl_spec && !constructor_p)
14143 	{
14144 	  int decl_spec_declares_class_or_enum;
14145 	  bool is_cv_qualifier;
14146 	  tree type_spec;
14147 
14148 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14149 	    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14150 
14151 	  type_spec
14152 	    = cp_parser_type_specifier (parser, flags,
14153 					decl_specs,
14154 					/*is_declaration=*/true,
14155 					&decl_spec_declares_class_or_enum,
14156 					&is_cv_qualifier);
14157 	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14158 
14159 	  /* If this type-specifier referenced a user-defined type
14160 	     (a typedef, class-name, etc.), then we can't allow any
14161 	     more such type-specifiers henceforth.
14162 
14163 	     [dcl.spec]
14164 
14165 	     The longest sequence of decl-specifiers that could
14166 	     possibly be a type name is taken as the
14167 	     decl-specifier-seq of a declaration.  The sequence shall
14168 	     be self-consistent as described below.
14169 
14170 	     [dcl.type]
14171 
14172 	     As a general rule, at most one type-specifier is allowed
14173 	     in the complete decl-specifier-seq of a declaration.  The
14174 	     only exceptions are the following:
14175 
14176 	     -- const or volatile can be combined with any other
14177 		type-specifier.
14178 
14179 	     -- signed or unsigned can be combined with char, long,
14180 		short, or int.
14181 
14182 	     -- ..
14183 
14184 	     Example:
14185 
14186 	       typedef char* Pc;
14187 	       void g (const int Pc);
14188 
14189 	     Here, Pc is *not* part of the decl-specifier seq; it's
14190 	     the declarator.  Therefore, once we see a type-specifier
14191 	     (other than a cv-qualifier), we forbid any additional
14192 	     user-defined types.  We *do* still allow things like `int
14193 	     int' to be considered a decl-specifier-seq, and issue the
14194 	     error message later.  */
14195 	  if (type_spec && !is_cv_qualifier)
14196 	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14197 	  /* A constructor declarator cannot follow a type-specifier.  */
14198 	  if (type_spec)
14199 	    {
14200 	      constructor_possible_p = false;
14201 	      found_decl_spec = true;
14202 	      if (!is_cv_qualifier)
14203 		decl_specs->any_type_specifiers_p = true;
14204 
14205 	      if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14206 		error_at (token->location, "type-specifier invalid in lambda");
14207 	    }
14208 	}
14209 
14210       /* If we still do not have a DECL_SPEC, then there are no more
14211 	 decl-specifiers.  */
14212       if (!found_decl_spec)
14213 	break;
14214 
14215       decl_specs->any_specifiers_p = true;
14216       /* After we see one decl-specifier, further decl-specifiers are
14217 	 always optional.  */
14218       flags |= CP_PARSER_FLAGS_OPTIONAL;
14219     }
14220 
14221   /* Don't allow a friend specifier with a class definition.  */
14222   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14223       && (*declares_class_or_enum & 2))
14224     error_at (decl_specs->locations[ds_friend],
14225 	      "class definition may not be declared a friend");
14226 }
14227 
14228 /* Parse an (optional) storage-class-specifier.
14229 
14230    storage-class-specifier:
14231      auto
14232      register
14233      static
14234      extern
14235      mutable
14236 
14237    GNU Extension:
14238 
14239    storage-class-specifier:
14240      thread
14241 
14242    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
14243 
14244 static tree
cp_parser_storage_class_specifier_opt(cp_parser * parser)14245 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14246 {
14247   switch (cp_lexer_peek_token (parser->lexer)->keyword)
14248     {
14249     case RID_AUTO:
14250       if (cxx_dialect != cxx98)
14251         return NULL_TREE;
14252       /* Fall through for C++98.  */
14253       gcc_fallthrough ();
14254 
14255     case RID_REGISTER:
14256     case RID_STATIC:
14257     case RID_EXTERN:
14258     case RID_MUTABLE:
14259     case RID_THREAD:
14260       /* Consume the token.  */
14261       return cp_lexer_consume_token (parser->lexer)->u.value;
14262 
14263     default:
14264       return NULL_TREE;
14265     }
14266 }
14267 
14268 /* Parse an (optional) function-specifier.
14269 
14270    function-specifier:
14271      inline
14272      virtual
14273      explicit
14274 
14275    C++2A Extension:
14276      explicit(constant-expression)
14277 
14278    Returns an IDENTIFIER_NODE corresponding to the keyword used.
14279    Updates DECL_SPECS, if it is non-NULL.  */
14280 
14281 static tree
cp_parser_function_specifier_opt(cp_parser * parser,cp_decl_specifier_seq * decl_specs)14282 cp_parser_function_specifier_opt (cp_parser* parser,
14283 				  cp_decl_specifier_seq *decl_specs)
14284 {
14285   cp_token *token = cp_lexer_peek_token (parser->lexer);
14286   switch (token->keyword)
14287     {
14288     case RID_INLINE:
14289       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14290       break;
14291 
14292     case RID_VIRTUAL:
14293       /* 14.5.2.3 [temp.mem]
14294 
14295 	 A member function template shall not be virtual.  */
14296       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14297 	  && current_class_type)
14298 	error_at (token->location, "templates may not be %<virtual%>");
14299       else
14300 	set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14301       break;
14302 
14303     case RID_EXPLICIT:
14304       {
14305 	tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14306 	/* If we see '(', it's C++20 explicit(bool).  */
14307 	tree expr;
14308 	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14309 	  {
14310 	    matching_parens parens;
14311 	    parens.consume_open (parser);
14312 
14313 	    /* New types are not allowed in an explicit-specifier.  */
14314 	    const char *saved_message
14315 	      = parser->type_definition_forbidden_message;
14316 	    parser->type_definition_forbidden_message
14317 	      = G_("types may not be defined in explicit-specifier");
14318 
14319 	    if (cxx_dialect < cxx2a)
14320 	      pedwarn (token->location, 0,
14321 		       "%<explicit(bool)%> only available with %<-std=c++2a%> "
14322 		       "or %<-std=gnu++2a%>");
14323 
14324 	    /* Parse the constant-expression.  */
14325 	    expr = cp_parser_constant_expression (parser);
14326 
14327 	    /* Restore the saved message.  */
14328 	    parser->type_definition_forbidden_message = saved_message;
14329 	    parens.require_close (parser);
14330 	  }
14331 	else
14332 	  /* The explicit-specifier explicit without a constant-expression is
14333 	     equivalent to the explicit-specifier explicit(true).  */
14334 	  expr = boolean_true_node;
14335 
14336 	/* [dcl.fct.spec]
14337 	   "the constant-expression, if supplied, shall be a contextually
14338 	   converted constant expression of type bool."  */
14339 	expr = build_explicit_specifier (expr, tf_warning_or_error);
14340 	/* We could evaluate it -- mark the decl as appropriate.  */
14341 	if (expr == boolean_true_node)
14342 	  set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14343 	else if (expr == boolean_false_node)
14344 	  /* Don't mark the decl as explicit.  */;
14345 	else if (decl_specs)
14346 	  /* The expression was value-dependent.  Remember it so that we can
14347 	     substitute it later.  */
14348 	  decl_specs->explicit_specifier = expr;
14349 	return id;
14350       }
14351 
14352     default:
14353       return NULL_TREE;
14354     }
14355 
14356   /* Consume the token.  */
14357   return cp_lexer_consume_token (parser->lexer)->u.value;
14358 }
14359 
14360 /* Parse a linkage-specification.
14361 
14362    linkage-specification:
14363      extern string-literal { declaration-seq [opt] }
14364      extern string-literal declaration  */
14365 
14366 static void
cp_parser_linkage_specification(cp_parser * parser)14367 cp_parser_linkage_specification (cp_parser* parser)
14368 {
14369   tree linkage;
14370 
14371   /* Look for the `extern' keyword.  */
14372   cp_token *extern_token
14373     = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14374 
14375   /* Look for the string-literal.  */
14376   cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14377   linkage = cp_parser_string_literal (parser, false, false);
14378 
14379   /* Transform the literal into an identifier.  If the literal is a
14380      wide-character string, or contains embedded NULs, then we can't
14381      handle it as the user wants.  */
14382   if (strlen (TREE_STRING_POINTER (linkage))
14383       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14384     {
14385       cp_parser_error (parser, "invalid linkage-specification");
14386       /* Assume C++ linkage.  */
14387       linkage = lang_name_cplusplus;
14388     }
14389   else
14390     linkage = get_identifier (TREE_STRING_POINTER (linkage));
14391 
14392   /* We're now using the new linkage.  */
14393   push_lang_context (linkage);
14394 
14395   /* Preserve the location of the the innermost linkage specification,
14396      tracking the locations of nested specifications via a local.  */
14397   location_t saved_location
14398     = parser->innermost_linkage_specification_location;
14399   /* Construct a location ranging from the start of the "extern" to
14400      the end of the string-literal, with the caret at the start, e.g.:
14401        extern "C" {
14402        ^~~~~~~~~~
14403   */
14404   parser->innermost_linkage_specification_location
14405     = make_location (extern_token->location,
14406 		     extern_token->location,
14407 		     get_finish (string_token->location));
14408 
14409   /* If the next token is a `{', then we're using the first
14410      production.  */
14411   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14412     {
14413       cp_ensure_no_omp_declare_simd (parser);
14414       cp_ensure_no_oacc_routine (parser);
14415 
14416       /* Consume the `{' token.  */
14417       matching_braces braces;
14418       braces.consume_open (parser);
14419       /* Parse the declarations.  */
14420       cp_parser_declaration_seq_opt (parser);
14421       /* Look for the closing `}'.  */
14422       braces.require_close (parser);
14423     }
14424   /* Otherwise, there's just one declaration.  */
14425   else
14426     {
14427       bool saved_in_unbraced_linkage_specification_p;
14428 
14429       saved_in_unbraced_linkage_specification_p
14430 	= parser->in_unbraced_linkage_specification_p;
14431       parser->in_unbraced_linkage_specification_p = true;
14432       cp_parser_declaration (parser);
14433       parser->in_unbraced_linkage_specification_p
14434 	= saved_in_unbraced_linkage_specification_p;
14435     }
14436 
14437   /* We're done with the linkage-specification.  */
14438   pop_lang_context ();
14439 
14440   /* Restore location of parent linkage specification, if any.  */
14441   parser->innermost_linkage_specification_location = saved_location;
14442 }
14443 
14444 /* Parse a static_assert-declaration.
14445 
14446    static_assert-declaration:
14447      static_assert ( constant-expression , string-literal ) ;
14448      static_assert ( constant-expression ) ; (C++17)
14449 
14450    If MEMBER_P, this static_assert is a class member.  */
14451 
14452 static void
cp_parser_static_assert(cp_parser * parser,bool member_p)14453 cp_parser_static_assert(cp_parser *parser, bool member_p)
14454 {
14455   cp_expr condition;
14456   location_t token_loc;
14457   tree message;
14458   bool dummy;
14459 
14460   /* Peek at the `static_assert' token so we can keep track of exactly
14461      where the static assertion started.  */
14462   token_loc = cp_lexer_peek_token (parser->lexer)->location;
14463 
14464   /* Look for the `static_assert' keyword.  */
14465   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14466                                   RT_STATIC_ASSERT))
14467     return;
14468 
14469   /*  We know we are in a static assertion; commit to any tentative
14470       parse.  */
14471   if (cp_parser_parsing_tentatively (parser))
14472     cp_parser_commit_to_tentative_parse (parser);
14473 
14474   /* Parse the `(' starting the static assertion condition.  */
14475   matching_parens parens;
14476   parens.require_open (parser);
14477 
14478   /* Parse the constant-expression.  Allow a non-constant expression
14479      here in order to give better diagnostics in finish_static_assert.  */
14480   condition =
14481     cp_parser_constant_expression (parser,
14482                                    /*allow_non_constant_p=*/true,
14483                                    /*non_constant_p=*/&dummy);
14484 
14485   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14486     {
14487       if (cxx_dialect < cxx17)
14488 	pedwarn (input_location, OPT_Wpedantic,
14489 		 "static_assert without a message "
14490 		 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14491       /* Eat the ')'  */
14492       cp_lexer_consume_token (parser->lexer);
14493       message = build_string (1, "");
14494       TREE_TYPE (message) = char_array_type_node;
14495       fix_string_type (message);
14496     }
14497   else
14498     {
14499       /* Parse the separating `,'.  */
14500       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14501 
14502       /* Parse the string-literal message.  */
14503       message = cp_parser_string_literal (parser,
14504                                 	  /*translate=*/false,
14505                                 	  /*wide_ok=*/true);
14506 
14507       /* A `)' completes the static assertion.  */
14508       if (!parens.require_close (parser))
14509 	cp_parser_skip_to_closing_parenthesis (parser,
14510                                                /*recovering=*/true,
14511                                                /*or_comma=*/false,
14512 					       /*consume_paren=*/true);
14513     }
14514 
14515   /* A semicolon terminates the declaration.  */
14516   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14517 
14518   /* Get the location for the static assertion.  Use that of the
14519      condition if available, otherwise, use that of the "static_assert"
14520      token.  */
14521   location_t assert_loc = condition.get_location ();
14522   if (assert_loc == UNKNOWN_LOCATION)
14523     assert_loc = token_loc;
14524 
14525   /* Complete the static assertion, which may mean either processing
14526      the static assert now or saving it for template instantiation.  */
14527   finish_static_assert (condition, message, assert_loc, member_p);
14528 }
14529 
14530 /* Parse the expression in decltype ( expression ).  */
14531 
14532 static tree
cp_parser_decltype_expr(cp_parser * parser,bool & id_expression_or_member_access_p)14533 cp_parser_decltype_expr (cp_parser *parser,
14534 			 bool &id_expression_or_member_access_p)
14535 {
14536   cp_token *id_expr_start_token;
14537   tree expr;
14538 
14539   /* Since we're going to preserve any side-effects from this parse, set up a
14540      firewall to protect our callers from cp_parser_commit_to_tentative_parse
14541      in the expression.  */
14542   tentative_firewall firewall (parser);
14543 
14544   /* First, try parsing an id-expression.  */
14545   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14546   cp_parser_parse_tentatively (parser);
14547   expr = cp_parser_id_expression (parser,
14548                                   /*template_keyword_p=*/false,
14549                                   /*check_dependency_p=*/true,
14550                                   /*template_p=*/NULL,
14551                                   /*declarator_p=*/false,
14552                                   /*optional_p=*/false);
14553 
14554   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14555     {
14556       bool non_integral_constant_expression_p = false;
14557       tree id_expression = expr;
14558       cp_id_kind idk;
14559       const char *error_msg;
14560 
14561       if (identifier_p (expr))
14562 	/* Lookup the name we got back from the id-expression.  */
14563 	expr = cp_parser_lookup_name_simple (parser, expr,
14564 					     id_expr_start_token->location);
14565 
14566       if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14567 	/* A template without args is not a complete id-expression.  */
14568 	expr = error_mark_node;
14569 
14570       if (expr
14571           && expr != error_mark_node
14572           && TREE_CODE (expr) != TYPE_DECL
14573 	  && (TREE_CODE (expr) != BIT_NOT_EXPR
14574 	      || !TYPE_P (TREE_OPERAND (expr, 0)))
14575           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14576         {
14577           /* Complete lookup of the id-expression.  */
14578           expr = (finish_id_expression
14579                   (id_expression, expr, parser->scope, &idk,
14580                    /*integral_constant_expression_p=*/false,
14581                    /*allow_non_integral_constant_expression_p=*/true,
14582                    &non_integral_constant_expression_p,
14583                    /*template_p=*/false,
14584                    /*done=*/true,
14585                    /*address_p=*/false,
14586                    /*template_arg_p=*/false,
14587                    &error_msg,
14588 		   id_expr_start_token->location));
14589 
14590           if (expr == error_mark_node)
14591             /* We found an id-expression, but it was something that we
14592                should not have found. This is an error, not something
14593                we can recover from, so note that we found an
14594                id-expression and we'll recover as gracefully as
14595                possible.  */
14596             id_expression_or_member_access_p = true;
14597         }
14598 
14599       if (expr
14600           && expr != error_mark_node
14601           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14602         /* We have an id-expression.  */
14603         id_expression_or_member_access_p = true;
14604     }
14605 
14606   if (!id_expression_or_member_access_p)
14607     {
14608       /* Abort the id-expression parse.  */
14609       cp_parser_abort_tentative_parse (parser);
14610 
14611       /* Parsing tentatively, again.  */
14612       cp_parser_parse_tentatively (parser);
14613 
14614       /* Parse a class member access.  */
14615       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14616                                            /*cast_p=*/false, /*decltype*/true,
14617                                            /*member_access_only_p=*/true, NULL);
14618 
14619       if (expr
14620           && expr != error_mark_node
14621           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14622         /* We have an id-expression.  */
14623         id_expression_or_member_access_p = true;
14624     }
14625 
14626   if (id_expression_or_member_access_p)
14627     /* We have parsed the complete id-expression or member access.  */
14628     cp_parser_parse_definitely (parser);
14629   else
14630     {
14631       /* Abort our attempt to parse an id-expression or member access
14632          expression.  */
14633       cp_parser_abort_tentative_parse (parser);
14634 
14635       /* Commit to the tentative_firewall so we get syntax errors.  */
14636       cp_parser_commit_to_tentative_parse (parser);
14637 
14638       /* Parse a full expression.  */
14639       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14640 				   /*decltype_p=*/true);
14641     }
14642 
14643   return expr;
14644 }
14645 
14646 /* Parse a `decltype' type. Returns the type.
14647 
14648    simple-type-specifier:
14649      decltype ( expression )
14650    C++14 proposal:
14651      decltype ( auto )  */
14652 
14653 static tree
cp_parser_decltype(cp_parser * parser)14654 cp_parser_decltype (cp_parser *parser)
14655 {
14656   bool id_expression_or_member_access_p = false;
14657   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14658 
14659   if (start_token->type == CPP_DECLTYPE)
14660     {
14661       /* Already parsed.  */
14662       cp_lexer_consume_token (parser->lexer);
14663       return saved_checks_value (start_token->u.tree_check_value);
14664     }
14665 
14666   /* Look for the `decltype' token.  */
14667   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14668     return error_mark_node;
14669 
14670   /* Parse the opening `('.  */
14671   matching_parens parens;
14672   if (!parens.require_open (parser))
14673     return error_mark_node;
14674 
14675   push_deferring_access_checks (dk_deferred);
14676 
14677   tree expr = NULL_TREE;
14678 
14679   if (cxx_dialect >= cxx14
14680       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14681     /* decltype (auto) */
14682     cp_lexer_consume_token (parser->lexer);
14683   else
14684     {
14685       /* decltype (expression)  */
14686 
14687       /* Types cannot be defined in a `decltype' expression.  Save away the
14688 	 old message and set the new one.  */
14689       const char *saved_message = parser->type_definition_forbidden_message;
14690       parser->type_definition_forbidden_message
14691 	= G_("types may not be defined in %<decltype%> expressions");
14692 
14693       /* The restrictions on constant-expressions do not apply inside
14694 	 decltype expressions.  */
14695       bool saved_integral_constant_expression_p
14696 	= parser->integral_constant_expression_p;
14697       bool saved_non_integral_constant_expression_p
14698 	= parser->non_integral_constant_expression_p;
14699       parser->integral_constant_expression_p = false;
14700 
14701       /* Within a parenthesized expression, a `>' token is always
14702 	 the greater-than operator.  */
14703       bool saved_greater_than_is_operator_p
14704 	= parser->greater_than_is_operator_p;
14705       parser->greater_than_is_operator_p = true;
14706 
14707       /* Do not actually evaluate the expression.  */
14708       ++cp_unevaluated_operand;
14709 
14710       /* Do not warn about problems with the expression.  */
14711       ++c_inhibit_evaluation_warnings;
14712 
14713       expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14714       STRIP_ANY_LOCATION_WRAPPER (expr);
14715 
14716       /* Go back to evaluating expressions.  */
14717       --cp_unevaluated_operand;
14718       --c_inhibit_evaluation_warnings;
14719 
14720       /* The `>' token might be the end of a template-id or
14721 	 template-parameter-list now.  */
14722       parser->greater_than_is_operator_p
14723 	= saved_greater_than_is_operator_p;
14724 
14725       /* Restore the old message and the integral constant expression
14726 	 flags.  */
14727       parser->type_definition_forbidden_message = saved_message;
14728       parser->integral_constant_expression_p
14729 	= saved_integral_constant_expression_p;
14730       parser->non_integral_constant_expression_p
14731 	= saved_non_integral_constant_expression_p;
14732     }
14733 
14734   /* Parse to the closing `)'.  */
14735   if (!parens.require_close (parser))
14736     {
14737       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14738 					     /*consume_paren=*/true);
14739       pop_deferring_access_checks ();
14740       return error_mark_node;
14741     }
14742 
14743   if (!expr)
14744     {
14745       /* Build auto.  */
14746       expr = make_decltype_auto ();
14747       AUTO_IS_DECLTYPE (expr) = true;
14748     }
14749   else
14750     expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14751 				 tf_warning_or_error);
14752 
14753   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14754      it again.  */
14755   start_token->type = CPP_DECLTYPE;
14756   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14757   start_token->u.tree_check_value->value = expr;
14758   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14759   start_token->keyword = RID_MAX;
14760   cp_lexer_purge_tokens_after (parser->lexer, start_token);
14761 
14762   pop_to_parent_deferring_access_checks ();
14763 
14764   return expr;
14765 }
14766 
14767 /* Special member functions [gram.special] */
14768 
14769 /* Parse a conversion-function-id.
14770 
14771    conversion-function-id:
14772      operator conversion-type-id
14773 
14774    Returns an IDENTIFIER_NODE representing the operator.  */
14775 
14776 static tree
cp_parser_conversion_function_id(cp_parser * parser)14777 cp_parser_conversion_function_id (cp_parser* parser)
14778 {
14779   tree type;
14780   tree saved_scope;
14781   tree saved_qualifying_scope;
14782   tree saved_object_scope;
14783   tree pushed_scope = NULL_TREE;
14784 
14785   /* Look for the `operator' token.  */
14786   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14787     return error_mark_node;
14788   /* When we parse the conversion-type-id, the current scope will be
14789      reset.  However, we need that information in able to look up the
14790      conversion function later, so we save it here.  */
14791   saved_scope = parser->scope;
14792   saved_qualifying_scope = parser->qualifying_scope;
14793   saved_object_scope = parser->object_scope;
14794   /* We must enter the scope of the class so that the names of
14795      entities declared within the class are available in the
14796      conversion-type-id.  For example, consider:
14797 
14798        struct S {
14799 	 typedef int I;
14800 	 operator I();
14801        };
14802 
14803        S::operator I() { ... }
14804 
14805      In order to see that `I' is a type-name in the definition, we
14806      must be in the scope of `S'.  */
14807   if (saved_scope)
14808     pushed_scope = push_scope (saved_scope);
14809   /* Parse the conversion-type-id.  */
14810   type = cp_parser_conversion_type_id (parser);
14811   /* Leave the scope of the class, if any.  */
14812   if (pushed_scope)
14813     pop_scope (pushed_scope);
14814   /* Restore the saved scope.  */
14815   parser->scope = saved_scope;
14816   parser->qualifying_scope = saved_qualifying_scope;
14817   parser->object_scope = saved_object_scope;
14818   /* If the TYPE is invalid, indicate failure.  */
14819   if (type == error_mark_node)
14820     return error_mark_node;
14821   return make_conv_op_name (type);
14822 }
14823 
14824 /* Parse a conversion-type-id:
14825 
14826    conversion-type-id:
14827      type-specifier-seq conversion-declarator [opt]
14828 
14829    Returns the TYPE specified.  */
14830 
14831 static tree
cp_parser_conversion_type_id(cp_parser * parser)14832 cp_parser_conversion_type_id (cp_parser* parser)
14833 {
14834   tree attributes;
14835   cp_decl_specifier_seq type_specifiers;
14836   cp_declarator *declarator;
14837   tree type_specified;
14838   const char *saved_message;
14839 
14840   /* Parse the attributes.  */
14841   attributes = cp_parser_attributes_opt (parser);
14842 
14843   saved_message = parser->type_definition_forbidden_message;
14844   parser->type_definition_forbidden_message
14845     = G_("types may not be defined in a conversion-type-id");
14846 
14847   /* Parse the type-specifiers.  */
14848   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14849 				/*is_declaration=*/false,
14850 				/*is_trailing_return=*/false,
14851 				&type_specifiers);
14852 
14853   parser->type_definition_forbidden_message = saved_message;
14854 
14855   /* If that didn't work, stop.  */
14856   if (type_specifiers.type == error_mark_node)
14857     return error_mark_node;
14858   /* Parse the conversion-declarator.  */
14859   declarator = cp_parser_conversion_declarator_opt (parser);
14860 
14861   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
14862 				    /*initialized=*/0, &attributes);
14863   if (attributes)
14864     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14865 
14866   /* Don't give this error when parsing tentatively.  This happens to
14867      work because we always parse this definitively once.  */
14868   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14869       && type_uses_auto (type_specified))
14870     {
14871       if (cxx_dialect < cxx14)
14872 	{
14873 	  error ("invalid use of %<auto%> in conversion operator");
14874 	  return error_mark_node;
14875 	}
14876       else if (template_parm_scope_p ())
14877 	warning (0, "use of %<auto%> in member template "
14878 		 "conversion operator can never be deduced");
14879     }
14880 
14881   return type_specified;
14882 }
14883 
14884 /* Parse an (optional) conversion-declarator.
14885 
14886    conversion-declarator:
14887      ptr-operator conversion-declarator [opt]
14888 
14889    */
14890 
14891 static cp_declarator *
cp_parser_conversion_declarator_opt(cp_parser * parser)14892 cp_parser_conversion_declarator_opt (cp_parser* parser)
14893 {
14894   enum tree_code code;
14895   tree class_type, std_attributes = NULL_TREE;
14896   cp_cv_quals cv_quals;
14897 
14898   /* We don't know if there's a ptr-operator next, or not.  */
14899   cp_parser_parse_tentatively (parser);
14900   /* Try the ptr-operator.  */
14901   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14902 				 &std_attributes);
14903   /* If it worked, look for more conversion-declarators.  */
14904   if (cp_parser_parse_definitely (parser))
14905     {
14906       cp_declarator *declarator;
14907 
14908       /* Parse another optional declarator.  */
14909       declarator = cp_parser_conversion_declarator_opt (parser);
14910 
14911       declarator = cp_parser_make_indirect_declarator
14912 	(code, class_type, cv_quals, declarator, std_attributes);
14913 
14914       return declarator;
14915    }
14916 
14917   return NULL;
14918 }
14919 
14920 /* Parse an (optional) ctor-initializer.
14921 
14922    ctor-initializer:
14923      : mem-initializer-list  */
14924 
14925 static void
cp_parser_ctor_initializer_opt(cp_parser * parser)14926 cp_parser_ctor_initializer_opt (cp_parser* parser)
14927 {
14928   /* If the next token is not a `:', then there is no
14929      ctor-initializer.  */
14930   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14931     {
14932       /* Do default initialization of any bases and members.  */
14933       if (DECL_CONSTRUCTOR_P (current_function_decl))
14934 	finish_mem_initializers (NULL_TREE);
14935       return;
14936     }
14937 
14938   /* Consume the `:' token.  */
14939   cp_lexer_consume_token (parser->lexer);
14940   /* And the mem-initializer-list.  */
14941   cp_parser_mem_initializer_list (parser);
14942 }
14943 
14944 /* Parse a mem-initializer-list.
14945 
14946    mem-initializer-list:
14947      mem-initializer ... [opt]
14948      mem-initializer ... [opt] , mem-initializer-list  */
14949 
14950 static void
cp_parser_mem_initializer_list(cp_parser * parser)14951 cp_parser_mem_initializer_list (cp_parser* parser)
14952 {
14953   tree mem_initializer_list = NULL_TREE;
14954   tree target_ctor = error_mark_node;
14955   cp_token *token = cp_lexer_peek_token (parser->lexer);
14956 
14957   /* Let the semantic analysis code know that we are starting the
14958      mem-initializer-list.  */
14959   if (!DECL_CONSTRUCTOR_P (current_function_decl))
14960     error_at (token->location,
14961 	      "only constructors take member initializers");
14962 
14963   /* Loop through the list.  */
14964   while (true)
14965     {
14966       tree mem_initializer;
14967 
14968       token = cp_lexer_peek_token (parser->lexer);
14969       /* Parse the mem-initializer.  */
14970       mem_initializer = cp_parser_mem_initializer (parser);
14971       /* If the next token is a `...', we're expanding member initializers. */
14972       bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14973       if (ellipsis
14974 	  || (mem_initializer != error_mark_node
14975 	      && check_for_bare_parameter_packs (TREE_PURPOSE
14976 						 (mem_initializer))))
14977         {
14978           /* Consume the `...'. */
14979 	  if (ellipsis)
14980 	    cp_lexer_consume_token (parser->lexer);
14981 
14982           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14983              can be expanded but members cannot. */
14984           if (mem_initializer != error_mark_node
14985               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14986             {
14987               error_at (token->location,
14988 			"cannot expand initializer for member %qD",
14989 			TREE_PURPOSE (mem_initializer));
14990               mem_initializer = error_mark_node;
14991             }
14992 
14993           /* Construct the pack expansion type. */
14994           if (mem_initializer != error_mark_node)
14995             mem_initializer = make_pack_expansion (mem_initializer);
14996         }
14997       if (target_ctor != error_mark_node
14998 	  && mem_initializer != error_mark_node)
14999 	{
15000 	  error ("mem-initializer for %qD follows constructor delegation",
15001 		 TREE_PURPOSE (mem_initializer));
15002 	  mem_initializer = error_mark_node;
15003 	}
15004       /* Look for a target constructor. */
15005       if (mem_initializer != error_mark_node
15006 	  && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
15007 	  && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
15008 	{
15009 	  maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
15010 	  if (mem_initializer_list)
15011 	    {
15012 	      error ("constructor delegation follows mem-initializer for %qD",
15013 		     TREE_PURPOSE (mem_initializer_list));
15014 	      mem_initializer = error_mark_node;
15015 	    }
15016 	  target_ctor = mem_initializer;
15017 	}
15018       /* Add it to the list, unless it was erroneous.  */
15019       if (mem_initializer != error_mark_node)
15020 	{
15021 	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
15022 	  mem_initializer_list = mem_initializer;
15023 	}
15024       /* If the next token is not a `,', we're done.  */
15025       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15026 	break;
15027       /* Consume the `,' token.  */
15028       cp_lexer_consume_token (parser->lexer);
15029     }
15030 
15031   /* Perform semantic analysis.  */
15032   if (DECL_CONSTRUCTOR_P (current_function_decl))
15033     finish_mem_initializers (mem_initializer_list);
15034 }
15035 
15036 /* Parse a mem-initializer.
15037 
15038    mem-initializer:
15039      mem-initializer-id ( expression-list [opt] )
15040      mem-initializer-id braced-init-list
15041 
15042    GNU extension:
15043 
15044    mem-initializer:
15045      ( expression-list [opt] )
15046 
15047    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
15048    class) or FIELD_DECL (for a non-static data member) to initialize;
15049    the TREE_VALUE is the expression-list.  An empty initialization
15050    list is represented by void_list_node.  */
15051 
15052 static tree
cp_parser_mem_initializer(cp_parser * parser)15053 cp_parser_mem_initializer (cp_parser* parser)
15054 {
15055   tree mem_initializer_id;
15056   tree expression_list;
15057   tree member;
15058   cp_token *token = cp_lexer_peek_token (parser->lexer);
15059 
15060   /* Find out what is being initialized.  */
15061   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15062     {
15063       permerror (token->location,
15064 		 "anachronistic old-style base class initializer");
15065       mem_initializer_id = NULL_TREE;
15066     }
15067   else
15068     {
15069       mem_initializer_id = cp_parser_mem_initializer_id (parser);
15070       if (mem_initializer_id == error_mark_node)
15071 	return mem_initializer_id;
15072     }
15073   member = expand_member_init (mem_initializer_id);
15074   if (member && !DECL_P (member))
15075     in_base_initializer = 1;
15076 
15077   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15078     {
15079       bool expr_non_constant_p;
15080       cp_lexer_set_source_position (parser->lexer);
15081       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15082       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
15083       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
15084       expression_list = build_tree_list (NULL_TREE, expression_list);
15085     }
15086   else
15087     {
15088       vec<tree, va_gc> *vec;
15089       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
15090 						     /*cast_p=*/false,
15091 						     /*allow_expansion_p=*/true,
15092 						     /*non_constant_p=*/NULL,
15093 						     /*close_paren_loc=*/NULL,
15094 						     /*wrap_locations_p=*/true);
15095       if (vec == NULL)
15096 	return error_mark_node;
15097       expression_list = build_tree_list_vec (vec);
15098       release_tree_vector (vec);
15099     }
15100 
15101   if (expression_list == error_mark_node)
15102     return error_mark_node;
15103   if (!expression_list)
15104     expression_list = void_type_node;
15105 
15106   in_base_initializer = 0;
15107 
15108   return member ? build_tree_list (member, expression_list) : error_mark_node;
15109 }
15110 
15111 /* Parse a mem-initializer-id.
15112 
15113    mem-initializer-id:
15114      :: [opt] nested-name-specifier [opt] class-name
15115      decltype-specifier (C++11)
15116      identifier
15117 
15118    Returns a TYPE indicating the class to be initialized for the first
15119    production (and the second in C++11).  Returns an IDENTIFIER_NODE
15120    indicating the data member to be initialized for the last production.  */
15121 
15122 static tree
cp_parser_mem_initializer_id(cp_parser * parser)15123 cp_parser_mem_initializer_id (cp_parser* parser)
15124 {
15125   bool global_scope_p;
15126   bool nested_name_specifier_p;
15127   bool template_p = false;
15128   tree id;
15129 
15130   cp_token *token = cp_lexer_peek_token (parser->lexer);
15131 
15132   /* `typename' is not allowed in this context ([temp.res]).  */
15133   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15134     {
15135       error_at (token->location,
15136 		"keyword %<typename%> not allowed in this context (a qualified "
15137 		"member initializer is implicitly a type)");
15138       cp_lexer_consume_token (parser->lexer);
15139     }
15140   /* Look for the optional `::' operator.  */
15141   global_scope_p
15142     = (cp_parser_global_scope_opt (parser,
15143 				   /*current_scope_valid_p=*/false)
15144        != NULL_TREE);
15145   /* Look for the optional nested-name-specifier.  The simplest way to
15146      implement:
15147 
15148        [temp.res]
15149 
15150        The keyword `typename' is not permitted in a base-specifier or
15151        mem-initializer; in these contexts a qualified name that
15152        depends on a template-parameter is implicitly assumed to be a
15153        type name.
15154 
15155      is to assume that we have seen the `typename' keyword at this
15156      point.  */
15157   nested_name_specifier_p
15158     = (cp_parser_nested_name_specifier_opt (parser,
15159 					    /*typename_keyword_p=*/true,
15160 					    /*check_dependency_p=*/true,
15161 					    /*type_p=*/true,
15162 					    /*is_declaration=*/true)
15163        != NULL_TREE);
15164   if (nested_name_specifier_p)
15165     template_p = cp_parser_optional_template_keyword (parser);
15166   /* If there is a `::' operator or a nested-name-specifier, then we
15167      are definitely looking for a class-name.  */
15168   if (global_scope_p || nested_name_specifier_p)
15169     return cp_parser_class_name (parser,
15170 				 /*typename_keyword_p=*/true,
15171 				 /*template_keyword_p=*/template_p,
15172 				 typename_type,
15173 				 /*check_dependency_p=*/true,
15174 				 /*class_head_p=*/false,
15175 				 /*is_declaration=*/true);
15176   /* Otherwise, we could also be looking for an ordinary identifier.  */
15177   cp_parser_parse_tentatively (parser);
15178   if (cp_lexer_next_token_is_decltype (parser->lexer))
15179     /* Try a decltype-specifier.  */
15180     id = cp_parser_decltype (parser);
15181   else
15182     /* Otherwise, try a class-name.  */
15183     id = cp_parser_class_name (parser,
15184 			       /*typename_keyword_p=*/true,
15185 			       /*template_keyword_p=*/false,
15186 			       none_type,
15187 			       /*check_dependency_p=*/true,
15188 			       /*class_head_p=*/false,
15189 			       /*is_declaration=*/true);
15190   /* If we found one, we're done.  */
15191   if (cp_parser_parse_definitely (parser))
15192     return id;
15193   /* Otherwise, look for an ordinary identifier.  */
15194   return cp_parser_identifier (parser);
15195 }
15196 
15197 /* Overloading [gram.over] */
15198 
15199 /* Parse an operator-function-id.
15200 
15201    operator-function-id:
15202      operator operator
15203 
15204    Returns an IDENTIFIER_NODE for the operator which is a
15205    human-readable spelling of the identifier, e.g., `operator +'.  */
15206 
15207 static cp_expr
cp_parser_operator_function_id(cp_parser * parser)15208 cp_parser_operator_function_id (cp_parser* parser)
15209 {
15210   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15211   /* Look for the `operator' keyword.  */
15212   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15213     return error_mark_node;
15214   /* And then the name of the operator itself.  */
15215   return cp_parser_operator (parser, start_loc);
15216 }
15217 
15218 /* Return an identifier node for a user-defined literal operator.
15219    The suffix identifier is chained to the operator name identifier.  */
15220 
15221 tree
cp_literal_operator_id(const char * name)15222 cp_literal_operator_id (const char* name)
15223 {
15224   tree identifier;
15225   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15226 			      + strlen (name) + 10);
15227   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15228   identifier = get_identifier (buffer);
15229 
15230   return identifier;
15231 }
15232 
15233 /* Parse an operator.
15234 
15235    operator:
15236      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15237      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15238      || ++ -- , ->* -> () []
15239 
15240    GNU Extensions:
15241 
15242    operator:
15243      <? >? <?= >?=
15244 
15245    Returns an IDENTIFIER_NODE for the operator which is a
15246    human-readable spelling of the identifier, e.g., `operator +'.  */
15247 
15248 static cp_expr
cp_parser_operator(cp_parser * parser,location_t start_loc)15249 cp_parser_operator (cp_parser* parser, location_t start_loc)
15250 {
15251   tree id = NULL_TREE;
15252   cp_token *token;
15253   bool utf8 = false;
15254 
15255   /* Peek at the next token.  */
15256   token = cp_lexer_peek_token (parser->lexer);
15257 
15258   location_t end_loc = token->location;
15259 
15260   /* Figure out which operator we have.  */
15261   enum tree_code op = ERROR_MARK;
15262   bool assop = false;
15263   bool consumed = false;
15264   switch (token->type)
15265     {
15266     case CPP_KEYWORD:
15267       {
15268 	/* The keyword should be either `new' or `delete'.  */
15269 	if (token->keyword == RID_NEW)
15270 	  op = NEW_EXPR;
15271 	else if (token->keyword == RID_DELETE)
15272 	  op = DELETE_EXPR;
15273 	else
15274 	  break;
15275 
15276 	/* Consume the `new' or `delete' token.  */
15277 	end_loc = cp_lexer_consume_token (parser->lexer)->location;
15278 
15279 	/* Peek at the next token.  */
15280 	token = cp_lexer_peek_token (parser->lexer);
15281 	/* If it's a `[' token then this is the array variant of the
15282 	   operator.  */
15283 	if (token->type == CPP_OPEN_SQUARE)
15284 	  {
15285 	    /* Consume the `[' token.  */
15286 	    cp_lexer_consume_token (parser->lexer);
15287 	    /* Look for the `]' token.  */
15288 	    if (cp_token *close_token
15289 		= cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15290 	      end_loc = close_token->location;
15291 	    op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15292 	  }
15293 	consumed = true;
15294 	break;
15295       }
15296 
15297     case CPP_PLUS:
15298       op = PLUS_EXPR;
15299       break;
15300 
15301     case CPP_MINUS:
15302       op = MINUS_EXPR;
15303       break;
15304 
15305     case CPP_MULT:
15306       op = MULT_EXPR;
15307       break;
15308 
15309     case CPP_DIV:
15310       op = TRUNC_DIV_EXPR;
15311       break;
15312 
15313     case CPP_MOD:
15314       op = TRUNC_MOD_EXPR;
15315       break;
15316 
15317     case CPP_XOR:
15318       op = BIT_XOR_EXPR;
15319       break;
15320 
15321     case CPP_AND:
15322       op = BIT_AND_EXPR;
15323       break;
15324 
15325     case CPP_OR:
15326       op = BIT_IOR_EXPR;
15327       break;
15328 
15329     case CPP_COMPL:
15330       op = BIT_NOT_EXPR;
15331       break;
15332 
15333     case CPP_NOT:
15334       op = TRUTH_NOT_EXPR;
15335       break;
15336 
15337     case CPP_EQ:
15338       assop = true;
15339       op = NOP_EXPR;
15340       break;
15341 
15342     case CPP_LESS:
15343       op = LT_EXPR;
15344       break;
15345 
15346     case CPP_GREATER:
15347       op = GT_EXPR;
15348       break;
15349 
15350     case CPP_PLUS_EQ:
15351       assop = true;
15352       op = PLUS_EXPR;
15353       break;
15354 
15355     case CPP_MINUS_EQ:
15356       assop = true;
15357       op = MINUS_EXPR;
15358       break;
15359 
15360     case CPP_MULT_EQ:
15361       assop = true;
15362       op = MULT_EXPR;
15363       break;
15364 
15365     case CPP_DIV_EQ:
15366       assop = true;
15367       op = TRUNC_DIV_EXPR;
15368       break;
15369 
15370     case CPP_MOD_EQ:
15371       assop = true;
15372       op = TRUNC_MOD_EXPR;
15373       break;
15374 
15375     case CPP_XOR_EQ:
15376       assop = true;
15377       op = BIT_XOR_EXPR;
15378       break;
15379 
15380     case CPP_AND_EQ:
15381       assop = true;
15382       op = BIT_AND_EXPR;
15383       break;
15384 
15385     case CPP_OR_EQ:
15386       assop = true;
15387       op = BIT_IOR_EXPR;
15388       break;
15389 
15390     case CPP_LSHIFT:
15391       op = LSHIFT_EXPR;
15392       break;
15393 
15394     case CPP_RSHIFT:
15395       op = RSHIFT_EXPR;
15396       break;
15397 
15398     case CPP_LSHIFT_EQ:
15399       assop = true;
15400       op = LSHIFT_EXPR;
15401       break;
15402 
15403     case CPP_RSHIFT_EQ:
15404       assop = true;
15405       op = RSHIFT_EXPR;
15406       break;
15407 
15408     case CPP_EQ_EQ:
15409       op = EQ_EXPR;
15410       break;
15411 
15412     case CPP_NOT_EQ:
15413       op = NE_EXPR;
15414       break;
15415 
15416     case CPP_LESS_EQ:
15417       op = LE_EXPR;
15418       break;
15419 
15420     case CPP_GREATER_EQ:
15421       op = GE_EXPR;
15422       break;
15423 
15424     case CPP_AND_AND:
15425       op = TRUTH_ANDIF_EXPR;
15426       break;
15427 
15428     case CPP_OR_OR:
15429       op = TRUTH_ORIF_EXPR;
15430       break;
15431 
15432     case CPP_PLUS_PLUS:
15433       op = POSTINCREMENT_EXPR;
15434       break;
15435 
15436     case CPP_MINUS_MINUS:
15437       op = PREDECREMENT_EXPR;
15438       break;
15439 
15440     case CPP_COMMA:
15441       op = COMPOUND_EXPR;
15442       break;
15443 
15444     case CPP_DEREF_STAR:
15445       op = MEMBER_REF;
15446       break;
15447 
15448     case CPP_DEREF:
15449       op = COMPONENT_REF;
15450       break;
15451 
15452     case CPP_OPEN_PAREN:
15453       {
15454         /* Consume the `('.  */
15455         matching_parens parens;
15456         parens.consume_open (parser);
15457         /* Look for the matching `)'.  */
15458         token = parens.require_close (parser);
15459         if (token)
15460 	  end_loc = token->location;
15461 	op = CALL_EXPR;
15462 	consumed = true;
15463 	break;
15464       }
15465 
15466     case CPP_OPEN_SQUARE:
15467       /* Consume the `['.  */
15468       cp_lexer_consume_token (parser->lexer);
15469       /* Look for the matching `]'.  */
15470       token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15471       if (token)
15472 	end_loc = token->location;
15473       op = ARRAY_REF;
15474       consumed = true;
15475       break;
15476 
15477     case CPP_UTF8STRING:
15478     case CPP_UTF8STRING_USERDEF:
15479       utf8 = true;
15480       /* FALLTHRU */
15481     case CPP_STRING:
15482     case CPP_WSTRING:
15483     case CPP_STRING16:
15484     case CPP_STRING32:
15485     case CPP_STRING_USERDEF:
15486     case CPP_WSTRING_USERDEF:
15487     case CPP_STRING16_USERDEF:
15488     case CPP_STRING32_USERDEF:
15489       {
15490 	cp_expr str;
15491 	tree string_tree;
15492 	int sz, len;
15493 
15494 	if (cxx_dialect == cxx98)
15495 	  maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15496 
15497 	/* Consume the string.  */
15498 	str = cp_parser_string_literal (parser, /*translate=*/true,
15499 				      /*wide_ok=*/true, /*lookup_udlit=*/false);
15500 	if (str == error_mark_node)
15501 	  return error_mark_node;
15502 	else if (TREE_CODE (str) == USERDEF_LITERAL)
15503 	  {
15504 	    string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15505 	    id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15506 	    end_loc = str.get_location ();
15507 	  }
15508 	else
15509 	  {
15510 	    string_tree = str;
15511 	    /* Look for the suffix identifier.  */
15512 	    token = cp_lexer_peek_token (parser->lexer);
15513 	    if (token->type == CPP_NAME)
15514 	      {
15515 		id = cp_parser_identifier (parser);
15516 		end_loc = token->location;
15517 	      }
15518 	    else if (token->type == CPP_KEYWORD)
15519 	      {
15520 		error ("unexpected keyword;"
15521 		       " remove space between quotes and suffix identifier");
15522 		return error_mark_node;
15523 	      }
15524 	    else
15525 	      {
15526 		error ("expected suffix identifier");
15527 		return error_mark_node;
15528 	      }
15529 	  }
15530 	sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15531 			       (TREE_TYPE (TREE_TYPE (string_tree))));
15532 	len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15533 	if (len != 0)
15534 	  {
15535 	    error ("expected empty string after %<operator%> keyword");
15536 	    return error_mark_node;
15537 	  }
15538 	if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15539 	    != char_type_node)
15540 	  {
15541 	    error ("invalid encoding prefix in literal operator");
15542 	    return error_mark_node;
15543 	  }
15544 	if (id != error_mark_node)
15545 	  {
15546 	    const char *name = IDENTIFIER_POINTER (id);
15547 	    id = cp_literal_operator_id (name);
15548 	  }
15549 	/* Generate a location of the form:
15550 	     "" _suffix_identifier
15551 	     ^~~~~~~~~~~~~~~~~~~~~
15552 	   with caret == start at the start token, finish at the end of the
15553 	   suffix identifier.  */
15554 	location_t finish_loc
15555 	  = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15556 	location_t combined_loc
15557 	  = make_location (start_loc, start_loc, finish_loc);
15558 	return cp_expr (id, combined_loc);
15559       }
15560 
15561     default:
15562       /* Anything else is an error.  */
15563       break;
15564     }
15565 
15566   /* If we have selected an identifier, we need to consume the
15567      operator token.  */
15568   if (op != ERROR_MARK)
15569     {
15570       id = ovl_op_identifier (assop, op);
15571       if (!consumed)
15572 	cp_lexer_consume_token (parser->lexer);
15573     }
15574   /* Otherwise, no valid operator name was present.  */
15575   else
15576     {
15577       cp_parser_error (parser, "expected operator");
15578       id = error_mark_node;
15579     }
15580 
15581   start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15582   return cp_expr (id, start_loc);
15583 }
15584 
15585 /* Parse a template-declaration.
15586 
15587    template-declaration:
15588      export [opt] template < template-parameter-list > declaration
15589 
15590    If MEMBER_P is TRUE, this template-declaration occurs within a
15591    class-specifier.
15592 
15593    The grammar rule given by the standard isn't correct.  What
15594    is really meant is:
15595 
15596    template-declaration:
15597      export [opt] template-parameter-list-seq
15598        decl-specifier-seq [opt] init-declarator [opt] ;
15599      export [opt] template-parameter-list-seq
15600        function-definition
15601 
15602    template-parameter-list-seq:
15603      template-parameter-list-seq [opt]
15604      template < template-parameter-list >
15605 
15606    Concept Extensions:
15607 
15608    template-parameter-list-seq:
15609      template < template-parameter-list > requires-clause [opt]
15610 
15611    requires-clause:
15612      requires logical-or-expression  */
15613 
15614 static void
cp_parser_template_declaration(cp_parser * parser,bool member_p)15615 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15616 {
15617   /* Check for `export'.  */
15618   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15619     {
15620       /* Consume the `export' token.  */
15621       cp_lexer_consume_token (parser->lexer);
15622       /* Warn that we do not support `export'.  */
15623       warning (0, "keyword %<export%> not implemented, and will be ignored");
15624     }
15625 
15626   cp_parser_template_declaration_after_export (parser, member_p);
15627 }
15628 
15629 /* Parse a template-parameter-list.
15630 
15631    template-parameter-list:
15632      template-parameter
15633      template-parameter-list , template-parameter
15634 
15635    Returns a TREE_LIST.  Each node represents a template parameter.
15636    The nodes are connected via their TREE_CHAINs.  */
15637 
15638 static tree
cp_parser_template_parameter_list(cp_parser * parser)15639 cp_parser_template_parameter_list (cp_parser* parser)
15640 {
15641   tree parameter_list = NULL_TREE;
15642 
15643   /* Don't create wrapper nodes within a template-parameter-list,
15644      since we don't want to have different types based on the
15645      spelling location of constants and decls within them.  */
15646   auto_suppress_location_wrappers sentinel;
15647 
15648   begin_template_parm_list ();
15649 
15650   /* The loop below parses the template parms.  We first need to know
15651      the total number of template parms to be able to compute proper
15652      canonical types of each dependent type. So after the loop, when
15653      we know the total number of template parms,
15654      end_template_parm_list computes the proper canonical types and
15655      fixes up the dependent types accordingly.  */
15656   while (true)
15657     {
15658       tree parameter;
15659       bool is_non_type;
15660       bool is_parameter_pack;
15661       location_t parm_loc;
15662 
15663       /* Parse the template-parameter.  */
15664       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15665       parameter = cp_parser_template_parameter (parser,
15666                                                 &is_non_type,
15667                                                 &is_parameter_pack);
15668       /* Add it to the list.  */
15669       if (parameter != error_mark_node)
15670 	parameter_list = process_template_parm (parameter_list,
15671 						parm_loc,
15672 						parameter,
15673 						is_non_type,
15674 						is_parameter_pack);
15675       else
15676        {
15677          tree err_parm = build_tree_list (parameter, parameter);
15678          parameter_list = chainon (parameter_list, err_parm);
15679        }
15680 
15681       /* If the next token is not a `,', we're done.  */
15682       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15683 	break;
15684       /* Otherwise, consume the `,' token.  */
15685       cp_lexer_consume_token (parser->lexer);
15686     }
15687 
15688   return end_template_parm_list (parameter_list);
15689 }
15690 
15691 /* Parse a introduction-list.
15692 
15693    introduction-list:
15694      introduced-parameter
15695      introduction-list , introduced-parameter
15696 
15697    introduced-parameter:
15698      ...[opt] identifier
15699 
15700    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
15701    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
15702    WILDCARD_DECL will also have DECL_NAME set and token location in
15703    DECL_SOURCE_LOCATION.  */
15704 
15705 static tree
cp_parser_introduction_list(cp_parser * parser)15706 cp_parser_introduction_list (cp_parser *parser)
15707 {
15708   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15709 
15710   while (true)
15711     {
15712       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15713       if (is_pack)
15714 	cp_lexer_consume_token (parser->lexer);
15715 
15716       tree identifier = cp_parser_identifier (parser);
15717       if (identifier == error_mark_node)
15718 	break;
15719 
15720       /* Build placeholder. */
15721       tree parm = build_nt (WILDCARD_DECL);
15722       DECL_SOURCE_LOCATION (parm)
15723 	= cp_lexer_peek_token (parser->lexer)->location;
15724       DECL_NAME (parm) = identifier;
15725       WILDCARD_PACK_P (parm) = is_pack;
15726       vec_safe_push (introduction_vec, parm);
15727 
15728       /* If the next token is not a `,', we're done.  */
15729       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15730 	break;
15731       /* Otherwise, consume the `,' token.  */
15732       cp_lexer_consume_token (parser->lexer);
15733     }
15734 
15735   /* Convert the vec into a TREE_VEC.  */
15736   tree introduction_list = make_tree_vec (introduction_vec->length ());
15737   unsigned int n;
15738   tree parm;
15739   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15740     TREE_VEC_ELT (introduction_list, n) = parm;
15741 
15742   release_tree_vector (introduction_vec);
15743   return introduction_list;
15744 }
15745 
15746 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15747    is an abstract declarator. */
15748 
15749 static inline cp_declarator*
get_id_declarator(cp_declarator * declarator)15750 get_id_declarator (cp_declarator *declarator)
15751 {
15752   cp_declarator *d = declarator;
15753   while (d && d->kind != cdk_id)
15754     d = d->declarator;
15755   return d;
15756 }
15757 
15758 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15759    is an abstract declarator. */
15760 
15761 static inline tree
get_unqualified_id(cp_declarator * declarator)15762 get_unqualified_id (cp_declarator *declarator)
15763 {
15764   declarator = get_id_declarator (declarator);
15765   if (declarator)
15766     return declarator->u.id.unqualified_name;
15767   else
15768     return NULL_TREE;
15769 }
15770 
15771 /* Returns true if DECL represents a constrained-parameter.  */
15772 
15773 static inline bool
is_constrained_parameter(tree decl)15774 is_constrained_parameter (tree decl)
15775 {
15776   return (decl
15777           && TREE_CODE (decl) == TYPE_DECL
15778           && CONSTRAINED_PARM_CONCEPT (decl)
15779           && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15780 }
15781 
15782 /* Returns true if PARM declares a constrained-parameter. */
15783 
15784 static inline bool
is_constrained_parameter(cp_parameter_declarator * parm)15785 is_constrained_parameter (cp_parameter_declarator *parm)
15786 {
15787   return is_constrained_parameter (parm->decl_specifiers.type);
15788 }
15789 
15790 /* Check that the type parameter is only a declarator-id, and that its
15791    type is not cv-qualified. */
15792 
15793 bool
cp_parser_check_constrained_type_parm(cp_parser * parser,cp_parameter_declarator * parm)15794 cp_parser_check_constrained_type_parm (cp_parser *parser,
15795 				       cp_parameter_declarator *parm)
15796 {
15797   if (!parm->declarator)
15798     return true;
15799 
15800   if (parm->declarator->kind != cdk_id)
15801     {
15802       cp_parser_error (parser, "invalid constrained type parameter");
15803       return false;
15804     }
15805 
15806   /* Don't allow cv-qualified type parameters.  */
15807   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15808       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15809     {
15810       cp_parser_error (parser, "cv-qualified type parameter");
15811       return false;
15812     }
15813 
15814   return true;
15815 }
15816 
15817 /* Finish parsing/processing a template type parameter and checking
15818    various restrictions. */
15819 
15820 static inline tree
cp_parser_constrained_type_template_parm(cp_parser * parser,tree id,cp_parameter_declarator * parmdecl)15821 cp_parser_constrained_type_template_parm (cp_parser *parser,
15822                                           tree id,
15823                                           cp_parameter_declarator* parmdecl)
15824 {
15825   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15826     return finish_template_type_parm (class_type_node, id);
15827   else
15828     return error_mark_node;
15829 }
15830 
15831 static tree
finish_constrained_template_template_parm(tree proto,tree id)15832 finish_constrained_template_template_parm (tree proto, tree id)
15833 {
15834   /* FIXME: This should probably be copied, and we may need to adjust
15835      the template parameter depths.  */
15836   tree saved_parms = current_template_parms;
15837   begin_template_parm_list ();
15838   current_template_parms = DECL_TEMPLATE_PARMS (proto);
15839   end_template_parm_list ();
15840 
15841   tree parm = finish_template_template_parm (class_type_node, id);
15842   current_template_parms = saved_parms;
15843 
15844   return parm;
15845 }
15846 
15847 /* Finish parsing/processing a template template parameter by borrowing
15848    the template parameter list from the prototype parameter.  */
15849 
15850 static tree
cp_parser_constrained_template_template_parm(cp_parser * parser,tree proto,tree id,cp_parameter_declarator * parmdecl)15851 cp_parser_constrained_template_template_parm (cp_parser *parser,
15852                                               tree proto,
15853                                               tree id,
15854                                               cp_parameter_declarator *parmdecl)
15855 {
15856   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15857     return error_mark_node;
15858   return finish_constrained_template_template_parm (proto, id);
15859 }
15860 
15861 /* Create a new non-type template parameter from the given PARM
15862    declarator.  */
15863 
15864 static tree
constrained_non_type_template_parm(bool * is_non_type,cp_parameter_declarator * parm)15865 constrained_non_type_template_parm (bool *is_non_type,
15866                                     cp_parameter_declarator *parm)
15867 {
15868   *is_non_type = true;
15869   cp_declarator *decl = parm->declarator;
15870   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15871   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15872   return grokdeclarator (decl, specs, TPARM, 0, NULL);
15873 }
15874 
15875 /* Build a constrained template parameter based on the PARMDECL
15876    declarator. The type of PARMDECL is the constrained type, which
15877    refers to the prototype template parameter that ultimately
15878    specifies the type of the declared parameter. */
15879 
15880 static tree
finish_constrained_parameter(cp_parser * parser,cp_parameter_declarator * parmdecl,bool * is_non_type,bool * is_parameter_pack)15881 finish_constrained_parameter (cp_parser *parser,
15882                               cp_parameter_declarator *parmdecl,
15883                               bool *is_non_type,
15884                               bool *is_parameter_pack)
15885 {
15886   tree decl = parmdecl->decl_specifiers.type;
15887   tree id = get_unqualified_id (parmdecl->declarator);
15888   tree def = parmdecl->default_argument;
15889   tree proto = DECL_INITIAL (decl);
15890 
15891   /* A template parameter constrained by a variadic concept shall also
15892      be declared as a template parameter pack.  */
15893   bool is_variadic = template_parameter_pack_p (proto);
15894   if (is_variadic && !*is_parameter_pack)
15895     cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15896 
15897   /* Build the parameter. Return an error if the declarator was invalid. */
15898   tree parm;
15899   if (TREE_CODE (proto) == TYPE_DECL)
15900     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15901   else if (TREE_CODE (proto) == TEMPLATE_DECL)
15902     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15903 							 parmdecl);
15904   else
15905     parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15906   if (parm == error_mark_node)
15907     return error_mark_node;
15908 
15909   /* Finish the parameter decl and create a node attaching the
15910      default argument and constraint.  */
15911   parm = build_tree_list (def, parm);
15912   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15913 
15914   return parm;
15915 }
15916 
15917 /* Returns true if the parsed type actually represents the declaration
15918    of a type template-parameter.  */
15919 
15920 static inline bool
declares_constrained_type_template_parameter(tree type)15921 declares_constrained_type_template_parameter (tree type)
15922 {
15923   return (is_constrained_parameter (type)
15924 	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15925 }
15926 
15927 
15928 /* Returns true if the parsed type actually represents the declaration of
15929    a template template-parameter.  */
15930 
15931 static bool
declares_constrained_template_template_parameter(tree type)15932 declares_constrained_template_template_parameter (tree type)
15933 {
15934   return (is_constrained_parameter (type)
15935 	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15936 }
15937 
15938 /* Parse a default argument for a type template-parameter.
15939    Note that diagnostics are handled in cp_parser_template_parameter.  */
15940 
15941 static tree
cp_parser_default_type_template_argument(cp_parser * parser)15942 cp_parser_default_type_template_argument (cp_parser *parser)
15943 {
15944   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15945 
15946   /* Consume the `=' token.  */
15947   cp_lexer_consume_token (parser->lexer);
15948 
15949   cp_token *token = cp_lexer_peek_token (parser->lexer);
15950 
15951   /* Parse the default-argument.  */
15952   push_deferring_access_checks (dk_no_deferred);
15953   tree default_argument = cp_parser_type_id (parser,
15954 					     CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15955 					     NULL);
15956   pop_deferring_access_checks ();
15957 
15958   if (flag_concepts && type_uses_auto (default_argument))
15959     {
15960       error_at (token->location,
15961 		"invalid use of %<auto%> in default template argument");
15962       return error_mark_node;
15963     }
15964 
15965   return default_argument;
15966 }
15967 
15968 /* Parse a default argument for a template template-parameter.  */
15969 
15970 static tree
cp_parser_default_template_template_argument(cp_parser * parser)15971 cp_parser_default_template_template_argument (cp_parser *parser)
15972 {
15973   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15974 
15975   bool is_template;
15976 
15977   /* Consume the `='.  */
15978   cp_lexer_consume_token (parser->lexer);
15979   /* Parse the id-expression.  */
15980   push_deferring_access_checks (dk_no_deferred);
15981   /* save token before parsing the id-expression, for error
15982      reporting */
15983   const cp_token* token = cp_lexer_peek_token (parser->lexer);
15984   tree default_argument
15985     = cp_parser_id_expression (parser,
15986                                /*template_keyword_p=*/false,
15987                                /*check_dependency_p=*/true,
15988                                /*template_p=*/&is_template,
15989                                /*declarator_p=*/false,
15990                                /*optional_p=*/false);
15991   if (TREE_CODE (default_argument) == TYPE_DECL)
15992     /* If the id-expression was a template-id that refers to
15993        a template-class, we already have the declaration here,
15994        so no further lookup is needed.  */
15995     ;
15996   else
15997     /* Look up the name.  */
15998     default_argument
15999       = cp_parser_lookup_name (parser, default_argument,
16000                                none_type,
16001                                /*is_template=*/is_template,
16002                                /*is_namespace=*/false,
16003                                /*check_dependency=*/true,
16004                                /*ambiguous_decls=*/NULL,
16005                                token->location);
16006   /* See if the default argument is valid.  */
16007   default_argument = check_template_template_default_arg (default_argument);
16008   pop_deferring_access_checks ();
16009   return default_argument;
16010 }
16011 
16012 /* Parse a template-parameter.
16013 
16014    template-parameter:
16015      type-parameter
16016      parameter-declaration
16017 
16018    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
16019    the parameter.  The TREE_PURPOSE is the default value, if any.
16020    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
16021    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
16022    set to true iff this parameter is a parameter pack. */
16023 
16024 static tree
cp_parser_template_parameter(cp_parser * parser,bool * is_non_type,bool * is_parameter_pack)16025 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
16026                               bool *is_parameter_pack)
16027 {
16028   cp_token *token;
16029   cp_parameter_declarator *parameter_declarator;
16030   tree parm;
16031 
16032   /* Assume it is a type parameter or a template parameter.  */
16033   *is_non_type = false;
16034   /* Assume it not a parameter pack. */
16035   *is_parameter_pack = false;
16036   /* Peek at the next token.  */
16037   token = cp_lexer_peek_token (parser->lexer);
16038   /* If it is `template', we have a type-parameter.  */
16039   if (token->keyword == RID_TEMPLATE)
16040     return cp_parser_type_parameter (parser, is_parameter_pack);
16041   /* If it is `class' or `typename' we do not know yet whether it is a
16042      type parameter or a non-type parameter.  Consider:
16043 
16044        template <typename T, typename T::X X> ...
16045 
16046      or:
16047 
16048        template <class C, class D*> ...
16049 
16050      Here, the first parameter is a type parameter, and the second is
16051      a non-type parameter.  We can tell by looking at the token after
16052      the identifier -- if it is a `,', `=', or `>' then we have a type
16053      parameter.  */
16054   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
16055     {
16056       /* Peek at the token after `class' or `typename'.  */
16057       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16058       /* If it's an ellipsis, we have a template type parameter
16059          pack. */
16060       if (token->type == CPP_ELLIPSIS)
16061         return cp_parser_type_parameter (parser, is_parameter_pack);
16062       /* If it's an identifier, skip it.  */
16063       if (token->type == CPP_NAME)
16064 	token = cp_lexer_peek_nth_token (parser->lexer, 3);
16065       /* Now, see if the token looks like the end of a template
16066 	 parameter.  */
16067       if (token->type == CPP_COMMA
16068 	  || token->type == CPP_EQ
16069 	  || token->type == CPP_GREATER)
16070 	return cp_parser_type_parameter (parser, is_parameter_pack);
16071     }
16072 
16073   /* Otherwise, it is a non-type parameter or a constrained parameter.
16074 
16075      [temp.param]
16076 
16077      When parsing a default template-argument for a non-type
16078      template-parameter, the first non-nested `>' is taken as the end
16079      of the template parameter-list rather than a greater-than
16080      operator.  */
16081   parameter_declarator
16082      = cp_parser_parameter_declaration (parser,
16083 					CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16084 					/*template_parm_p=*/true,
16085 					/*parenthesized_p=*/NULL);
16086 
16087   if (!parameter_declarator)
16088     return error_mark_node;
16089 
16090   /* If the parameter declaration is marked as a parameter pack, set
16091    *IS_PARAMETER_PACK to notify the caller.  */
16092   if (parameter_declarator->template_parameter_pack_p)
16093     *is_parameter_pack = true;
16094 
16095   if (parameter_declarator->default_argument)
16096     {
16097       /* Can happen in some cases of erroneous input (c++/34892).  */
16098       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16099 	/* Consume the `...' for better error recovery.  */
16100 	cp_lexer_consume_token (parser->lexer);
16101     }
16102 
16103   // The parameter may have been constrained.
16104   if (is_constrained_parameter (parameter_declarator))
16105     return finish_constrained_parameter (parser,
16106                                          parameter_declarator,
16107                                          is_non_type,
16108                                          is_parameter_pack);
16109 
16110   // Now we're sure that the parameter is a non-type parameter.
16111   *is_non_type = true;
16112 
16113   parm = grokdeclarator (parameter_declarator->declarator,
16114 			 &parameter_declarator->decl_specifiers,
16115 			 TPARM, /*initialized=*/0,
16116 			 /*attrlist=*/NULL);
16117   if (parm == error_mark_node)
16118     return error_mark_node;
16119 
16120   return build_tree_list (parameter_declarator->default_argument, parm);
16121 }
16122 
16123 /* Parse a type-parameter.
16124 
16125    type-parameter:
16126      class identifier [opt]
16127      class identifier [opt] = type-id
16128      typename identifier [opt]
16129      typename identifier [opt] = type-id
16130      template < template-parameter-list > class identifier [opt]
16131      template < template-parameter-list > class identifier [opt]
16132        = id-expression
16133 
16134    GNU Extension (variadic templates):
16135 
16136    type-parameter:
16137      class ... identifier [opt]
16138      typename ... identifier [opt]
16139 
16140    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
16141    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
16142    the declaration of the parameter.
16143 
16144    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16145 
16146 static tree
cp_parser_type_parameter(cp_parser * parser,bool * is_parameter_pack)16147 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16148 {
16149   cp_token *token;
16150   tree parameter;
16151 
16152   /* Look for a keyword to tell us what kind of parameter this is.  */
16153   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16154   if (!token)
16155     return error_mark_node;
16156 
16157   switch (token->keyword)
16158     {
16159     case RID_CLASS:
16160     case RID_TYPENAME:
16161       {
16162 	tree identifier;
16163 	tree default_argument;
16164 
16165         /* If the next token is an ellipsis, we have a template
16166            argument pack. */
16167         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16168           {
16169             /* Consume the `...' token. */
16170             cp_lexer_consume_token (parser->lexer);
16171             maybe_warn_variadic_templates ();
16172 
16173             *is_parameter_pack = true;
16174           }
16175 
16176 	/* If the next token is an identifier, then it names the
16177 	   parameter.  */
16178 	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16179 	  identifier = cp_parser_identifier (parser);
16180 	else
16181 	  identifier = NULL_TREE;
16182 
16183 	/* Create the parameter.  */
16184 	parameter = finish_template_type_parm (class_type_node, identifier);
16185 
16186 	/* If the next token is an `=', we have a default argument.  */
16187 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16188 	  {
16189 	    default_argument
16190 	      = cp_parser_default_type_template_argument (parser);
16191 
16192             /* Template parameter packs cannot have default
16193                arguments. */
16194             if (*is_parameter_pack)
16195               {
16196                 if (identifier)
16197                   error_at (token->location,
16198 			    "template parameter pack %qD cannot have a "
16199 			    "default argument", identifier);
16200                 else
16201                   error_at (token->location,
16202 			    "template parameter packs cannot have "
16203 			    "default arguments");
16204                 default_argument = NULL_TREE;
16205               }
16206 	    else if (check_for_bare_parameter_packs (default_argument))
16207 	      default_argument = error_mark_node;
16208 	  }
16209 	else
16210 	  default_argument = NULL_TREE;
16211 
16212 	/* Create the combined representation of the parameter and the
16213 	   default argument.  */
16214 	parameter = build_tree_list (default_argument, parameter);
16215       }
16216       break;
16217 
16218     case RID_TEMPLATE:
16219       {
16220 	tree identifier;
16221 	tree default_argument;
16222 
16223 	/* Look for the `<'.  */
16224 	cp_parser_require (parser, CPP_LESS, RT_LESS);
16225 	/* Parse the template-parameter-list.  */
16226 	cp_parser_template_parameter_list (parser);
16227 	/* Look for the `>'.  */
16228 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16229 
16230         // If template requirements are present, parse them.
16231 	if (flag_concepts)
16232           {
16233 	    tree reqs = get_shorthand_constraints (current_template_parms);
16234 	    if (tree r = cp_parser_requires_clause_opt (parser))
16235               reqs = conjoin_constraints (reqs, normalize_expression (r));
16236 	    TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16237           }
16238 
16239 	/* Look for the `class' or 'typename' keywords.  */
16240 	cp_parser_type_parameter_key (parser);
16241         /* If the next token is an ellipsis, we have a template
16242            argument pack. */
16243         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16244           {
16245             /* Consume the `...' token. */
16246             cp_lexer_consume_token (parser->lexer);
16247             maybe_warn_variadic_templates ();
16248 
16249             *is_parameter_pack = true;
16250           }
16251 	/* If the next token is an `=', then there is a
16252 	   default-argument.  If the next token is a `>', we are at
16253 	   the end of the parameter-list.  If the next token is a `,',
16254 	   then we are at the end of this parameter.  */
16255 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16256 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16257 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16258 	  {
16259 	    identifier = cp_parser_identifier (parser);
16260 	    /* Treat invalid names as if the parameter were nameless.  */
16261 	    if (identifier == error_mark_node)
16262 	      identifier = NULL_TREE;
16263 	  }
16264 	else
16265 	  identifier = NULL_TREE;
16266 
16267 	/* Create the template parameter.  */
16268 	parameter = finish_template_template_parm (class_type_node,
16269 						   identifier);
16270 
16271 	/* If the next token is an `=', then there is a
16272 	   default-argument.  */
16273 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16274 	  {
16275 	    default_argument
16276 	      = cp_parser_default_template_template_argument (parser);
16277 
16278             /* Template parameter packs cannot have default
16279                arguments. */
16280             if (*is_parameter_pack)
16281               {
16282                 if (identifier)
16283                   error_at (token->location,
16284 			    "template parameter pack %qD cannot "
16285 			    "have a default argument",
16286 			    identifier);
16287                 else
16288                   error_at (token->location, "template parameter packs cannot "
16289 			    "have default arguments");
16290                 default_argument = NULL_TREE;
16291               }
16292 	  }
16293 	else
16294 	  default_argument = NULL_TREE;
16295 
16296 	/* Create the combined representation of the parameter and the
16297 	   default argument.  */
16298 	parameter = build_tree_list (default_argument, parameter);
16299       }
16300       break;
16301 
16302     default:
16303       gcc_unreachable ();
16304       break;
16305     }
16306 
16307   return parameter;
16308 }
16309 
16310 /* Parse a template-id.
16311 
16312    template-id:
16313      template-name < template-argument-list [opt] >
16314 
16315    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16316    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
16317    returned.  Otherwise, if the template-name names a function, or set
16318    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
16319    names a class, returns a TYPE_DECL for the specialization.
16320 
16321    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16322    uninstantiated templates.  */
16323 
16324 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)16325 cp_parser_template_id (cp_parser *parser,
16326 		       bool template_keyword_p,
16327 		       bool check_dependency_p,
16328 		       enum tag_types tag_type,
16329 		       bool is_declaration)
16330 {
16331   tree templ;
16332   tree arguments;
16333   tree template_id;
16334   cp_token_position start_of_id = 0;
16335   cp_token *next_token = NULL, *next_token_2 = NULL;
16336   bool is_identifier;
16337 
16338   /* If the next token corresponds to a template-id, there is no need
16339      to reparse it.  */
16340   cp_token *token = cp_lexer_peek_token (parser->lexer);
16341   if (token->type == CPP_TEMPLATE_ID)
16342     {
16343       cp_lexer_consume_token (parser->lexer);
16344       return saved_checks_value (token->u.tree_check_value);
16345     }
16346 
16347   /* Avoid performing name lookup if there is no possibility of
16348      finding a template-id.  */
16349   if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16350       || (token->type == CPP_NAME
16351 	  && !cp_parser_nth_token_starts_template_argument_list_p
16352 	       (parser, 2)))
16353     {
16354       cp_parser_error (parser, "expected template-id");
16355       return error_mark_node;
16356     }
16357 
16358   /* Remember where the template-id starts.  */
16359   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16360     start_of_id = cp_lexer_token_position (parser->lexer, false);
16361 
16362   push_deferring_access_checks (dk_deferred);
16363 
16364   /* Parse the template-name.  */
16365   is_identifier = false;
16366   templ = cp_parser_template_name (parser, template_keyword_p,
16367 				   check_dependency_p,
16368 				   is_declaration,
16369 				   tag_type,
16370 				   &is_identifier);
16371 
16372   /* Push any access checks inside the firewall we're about to create.  */
16373   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16374   pop_deferring_access_checks ();
16375   if (templ == error_mark_node || is_identifier)
16376     return templ;
16377 
16378   /* Since we're going to preserve any side-effects from this parse, set up a
16379      firewall to protect our callers from cp_parser_commit_to_tentative_parse
16380      in the template arguments.  */
16381   tentative_firewall firewall (parser);
16382   reopen_deferring_access_checks (checks);
16383 
16384   /* If we find the sequence `[:' after a template-name, it's probably
16385      a digraph-typo for `< ::'. Substitute the tokens and check if we can
16386      parse correctly the argument list.  */
16387   if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16388        == CPP_OPEN_SQUARE)
16389       && next_token->flags & DIGRAPH
16390       && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16391 	  == CPP_COLON)
16392       && !(next_token_2->flags & PREV_WHITE))
16393     {
16394       cp_parser_parse_tentatively (parser);
16395       /* Change `:' into `::'.  */
16396       next_token_2->type = CPP_SCOPE;
16397       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16398 	 CPP_LESS.  */
16399       cp_lexer_consume_token (parser->lexer);
16400 
16401       /* Parse the arguments.  */
16402       arguments = cp_parser_enclosed_template_argument_list (parser);
16403       if (!cp_parser_parse_definitely (parser))
16404 	{
16405 	  /* If we couldn't parse an argument list, then we revert our changes
16406 	     and return simply an error. Maybe this is not a template-id
16407 	     after all.  */
16408 	  next_token_2->type = CPP_COLON;
16409 	  cp_parser_error (parser, "expected %<<%>");
16410 	  pop_deferring_access_checks ();
16411 	  return error_mark_node;
16412 	}
16413       /* Otherwise, emit an error about the invalid digraph, but continue
16414 	 parsing because we got our argument list.  */
16415       if (permerror (next_token->location,
16416 		     "%<<::%> cannot begin a template-argument list"))
16417 	{
16418 	  static bool hint = false;
16419 	  inform (next_token->location,
16420 		  "%<<:%> is an alternate spelling for %<[%>."
16421 		  " Insert whitespace between %<<%> and %<::%>");
16422 	  if (!hint && !flag_permissive)
16423 	    {
16424 	      inform (next_token->location, "(if you use %<-fpermissive%> "
16425 		      "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16426 		      "accept your code)");
16427 	      hint = true;
16428 	    }
16429 	}
16430     }
16431   else
16432     {
16433       /* Look for the `<' that starts the template-argument-list.  */
16434       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16435 	{
16436 	  pop_deferring_access_checks ();
16437 	  return error_mark_node;
16438 	}
16439       /* Parse the arguments.  */
16440       arguments = cp_parser_enclosed_template_argument_list (parser);
16441 
16442       if ((cxx_dialect > cxx17)
16443 	  && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16444 	  && !template_keyword_p
16445 	  && (cp_parser_error_occurred (parser)
16446 	      || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16447 	{
16448 	  /* This didn't go well.  */
16449 	  if (TREE_CODE (templ) == FUNCTION_DECL)
16450 	    {
16451 	      /* C++2A says that "function-name < a;" is now ill-formed.  */
16452 	      if (cp_parser_error_occurred (parser))
16453 		{
16454 		  error_at (token->location, "invalid template-argument-list");
16455 		  inform (token->location, "function name as the left hand "
16456 			  "operand of %<<%> is ill-formed in C++2a; wrap the "
16457 			  "function name in %<()%>");
16458 		}
16459 	      else
16460 		/* We expect "f<targs>" to be followed by "(args)".  */
16461 		error_at (cp_lexer_peek_token (parser->lexer)->location,
16462 			  "expected %<(%> after template-argument-list");
16463 	      if (start_of_id)
16464 		/* Purge all subsequent tokens.  */
16465 		cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16466 	    }
16467 	  else
16468 	    cp_parser_simulate_error (parser);
16469 	  pop_deferring_access_checks ();
16470 	  return error_mark_node;
16471 	}
16472     }
16473 
16474   /* Set the location to be of the form:
16475      template-name < template-argument-list [opt] >
16476      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16477      with caret == start at the start of the template-name,
16478      ranging until the closing '>'.  */
16479   location_t finish_loc
16480     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16481   location_t combined_loc
16482     = make_location (token->location, token->location, finish_loc);
16483 
16484   /* Check for concepts autos where they don't belong.  We could
16485      identify types in some cases of idnetifier TEMPL, looking ahead
16486      for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16487      types.  We reject them in functions, but if what we have is an
16488      identifier, even with none_type we can't conclude it's NOT a
16489      type, we have to wait for template substitution.  */
16490   if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16491     template_id = error_mark_node;
16492   /* Build a representation of the specialization.  */
16493   else if (identifier_p (templ))
16494     template_id = build_min_nt_loc (combined_loc,
16495 				    TEMPLATE_ID_EXPR,
16496 				    templ, arguments);
16497   else if (DECL_TYPE_TEMPLATE_P (templ)
16498 	   || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16499     {
16500       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16501 	 template (rather than some instantiation thereof) only if
16502 	 is not nested within some other construct.  For example, in
16503 	 "template <typename T> void f(T) { A<T>::", A<T> is just an
16504 	 instantiation of A.  */
16505       bool entering_scope
16506 	= (template_parm_scope_p ()
16507 	   && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16508       template_id
16509 	= finish_template_type (templ, arguments, entering_scope);
16510     }
16511   /* A template-like identifier may be a partial concept id. */
16512   else if (flag_concepts
16513            && (template_id = (cp_parser_maybe_partial_concept_id
16514 			      (parser, templ, arguments))))
16515     return template_id;
16516   else if (variable_template_p (templ))
16517     {
16518       template_id = lookup_template_variable (templ, arguments);
16519       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16520 	SET_EXPR_LOCATION (template_id, combined_loc);
16521     }
16522   else
16523     {
16524       /* If it's not a class-template or a template-template, it should be
16525 	 a function-template.  */
16526       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16527 		   || TREE_CODE (templ) == OVERLOAD
16528 		   || TREE_CODE (templ) == FUNCTION_DECL
16529 		   || BASELINK_P (templ)));
16530 
16531       template_id = lookup_template_function (templ, arguments);
16532       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16533 	SET_EXPR_LOCATION (template_id, combined_loc);
16534     }
16535 
16536   /* If parsing tentatively, replace the sequence of tokens that makes
16537      up the template-id with a CPP_TEMPLATE_ID token.  That way,
16538      should we re-parse the token stream, we will not have to repeat
16539      the effort required to do the parse, nor will we issue duplicate
16540      error messages about problems during instantiation of the
16541      template.  */
16542   if (start_of_id
16543       /* Don't do this if we had a parse error in a declarator; re-parsing
16544 	 might succeed if a name changes meaning (60361).  */
16545       && !(cp_parser_error_occurred (parser)
16546 	   && cp_parser_parsing_tentatively (parser)
16547 	   && parser->in_declarator_p))
16548     {
16549       /* Reset the contents of the START_OF_ID token.  */
16550       token->type = CPP_TEMPLATE_ID;
16551       token->location = combined_loc;
16552 
16553       /* Retrieve any deferred checks.  Do not pop this access checks yet
16554 	 so the memory will not be reclaimed during token replacing below.  */
16555       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16556       token->u.tree_check_value->value = template_id;
16557       token->u.tree_check_value->checks = get_deferred_access_checks ();
16558       token->keyword = RID_MAX;
16559 
16560       /* Purge all subsequent tokens.  */
16561       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16562 
16563       /* ??? Can we actually assume that, if template_id ==
16564 	 error_mark_node, we will have issued a diagnostic to the
16565 	 user, as opposed to simply marking the tentative parse as
16566 	 failed?  */
16567       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16568 	error_at (token->location, "parse error in template argument list");
16569     }
16570 
16571   pop_to_parent_deferring_access_checks ();
16572   return template_id;
16573 }
16574 
16575 /* Parse a template-name.
16576 
16577    template-name:
16578      identifier
16579 
16580    The standard should actually say:
16581 
16582    template-name:
16583      identifier
16584      operator-function-id
16585 
16586    A defect report has been filed about this issue.
16587 
16588    A conversion-function-id cannot be a template name because they cannot
16589    be part of a template-id. In fact, looking at this code:
16590 
16591    a.operator K<int>()
16592 
16593    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16594    It is impossible to call a templated conversion-function-id with an
16595    explicit argument list, since the only allowed template parameter is
16596    the type to which it is converting.
16597 
16598    If TEMPLATE_KEYWORD_P is true, then we have just seen the
16599    `template' keyword, in a construction like:
16600 
16601      T::template f<3>()
16602 
16603    In that case `f' is taken to be a template-name, even though there
16604    is no way of knowing for sure.
16605 
16606    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16607    name refers to a set of overloaded functions, at least one of which
16608    is a template, or an IDENTIFIER_NODE with the name of the template,
16609    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
16610    names are looked up inside uninstantiated templates.  */
16611 
16612 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)16613 cp_parser_template_name (cp_parser* parser,
16614 			 bool template_keyword_p,
16615 			 bool check_dependency_p,
16616 			 bool is_declaration,
16617 			 enum tag_types tag_type,
16618 			 bool *is_identifier)
16619 {
16620   tree identifier;
16621   tree decl;
16622   cp_token *token = cp_lexer_peek_token (parser->lexer);
16623 
16624   /* If the next token is `operator', then we have either an
16625      operator-function-id or a conversion-function-id.  */
16626   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16627     {
16628       /* We don't know whether we're looking at an
16629 	 operator-function-id or a conversion-function-id.  */
16630       cp_parser_parse_tentatively (parser);
16631       /* Try an operator-function-id.  */
16632       identifier = cp_parser_operator_function_id (parser);
16633       /* If that didn't work, try a conversion-function-id.  */
16634       if (!cp_parser_parse_definitely (parser))
16635 	{
16636 	  cp_parser_error (parser, "expected template-name");
16637 	  return error_mark_node;
16638 	}
16639     }
16640   /* Look for the identifier.  */
16641   else
16642     identifier = cp_parser_identifier (parser);
16643 
16644   /* If we didn't find an identifier, we don't have a template-id.  */
16645   if (identifier == error_mark_node)
16646     return error_mark_node;
16647 
16648   /* If the name immediately followed the `template' keyword, then it
16649      is a template-name.  However, if the next token is not `<', then
16650      we do not treat it as a template-name, since it is not being used
16651      as part of a template-id.  This enables us to handle constructs
16652      like:
16653 
16654        template <typename T> struct S { S(); };
16655        template <typename T> S<T>::S();
16656 
16657      correctly.  We would treat `S' as a template -- if it were `S<T>'
16658      -- but we do not if there is no `<'.  */
16659 
16660   if (processing_template_decl
16661       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16662     {
16663       /* In a declaration, in a dependent context, we pretend that the
16664 	 "template" keyword was present in order to improve error
16665 	 recovery.  For example, given:
16666 
16667 	   template <typename T> void f(T::X<int>);
16668 
16669 	 we want to treat "X<int>" as a template-id.  */
16670       if (is_declaration
16671 	  && !template_keyword_p
16672 	  && parser->scope && TYPE_P (parser->scope)
16673 	  && check_dependency_p
16674 	  && dependent_scope_p (parser->scope)
16675 	  /* Do not do this for dtors (or ctors), since they never
16676 	     need the template keyword before their name.  */
16677 	  && !constructor_name_p (identifier, parser->scope))
16678 	{
16679 	  cp_token_position start = 0;
16680 
16681 	  /* Explain what went wrong.  */
16682 	  error_at (token->location, "non-template %qD used as template",
16683 		    identifier);
16684 	  inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16685 		  parser->scope, identifier);
16686 	  /* If parsing tentatively, find the location of the "<" token.  */
16687 	  if (cp_parser_simulate_error (parser))
16688 	    start = cp_lexer_token_position (parser->lexer, true);
16689 	  /* Parse the template arguments so that we can issue error
16690 	     messages about them.  */
16691 	  cp_lexer_consume_token (parser->lexer);
16692 	  cp_parser_enclosed_template_argument_list (parser);
16693 	  /* Skip tokens until we find a good place from which to
16694 	     continue parsing.  */
16695 	  cp_parser_skip_to_closing_parenthesis (parser,
16696 						 /*recovering=*/true,
16697 						 /*or_comma=*/true,
16698 						 /*consume_paren=*/false);
16699 	  /* If parsing tentatively, permanently remove the
16700 	     template argument list.  That will prevent duplicate
16701 	     error messages from being issued about the missing
16702 	     "template" keyword.  */
16703 	  if (start)
16704 	    cp_lexer_purge_tokens_after (parser->lexer, start);
16705 	  if (is_identifier)
16706 	    *is_identifier = true;
16707 	  parser->context->object_type = NULL_TREE;
16708 	  return identifier;
16709 	}
16710 
16711       /* If the "template" keyword is present, then there is generally
16712 	 no point in doing name-lookup, so we just return IDENTIFIER.
16713 	 But, if the qualifying scope is non-dependent then we can
16714 	 (and must) do name-lookup normally.  */
16715       if (template_keyword_p)
16716 	{
16717 	  tree scope = (parser->scope ? parser->scope
16718 			: parser->context->object_type);
16719 	  if (scope && TYPE_P (scope)
16720 	      && (!CLASS_TYPE_P (scope)
16721 		  || (check_dependency_p && dependent_type_p (scope))))
16722 	    {
16723 	      /* We're optimizing away the call to cp_parser_lookup_name, but
16724 		 we still need to do this.  */
16725 	      parser->context->object_type = NULL_TREE;
16726 	      return identifier;
16727 	    }
16728 	}
16729     }
16730 
16731   /* cp_parser_lookup_name clears OBJECT_TYPE.  */
16732   const bool scoped_p = ((parser->scope ? parser->scope
16733 			  : parser->context->object_type) != NULL_TREE);
16734 
16735   /* Look up the name.  */
16736   decl = cp_parser_lookup_name (parser, identifier,
16737 				tag_type,
16738 				/*is_template=*/true,
16739 				/*is_namespace=*/false,
16740 				check_dependency_p,
16741 				/*ambiguous_decls=*/NULL,
16742 				token->location);
16743 
16744   decl = strip_using_decl (decl);
16745 
16746   /* If DECL is a template, then the name was a template-name.  */
16747   if (TREE_CODE (decl) == TEMPLATE_DECL)
16748     {
16749       if (TREE_DEPRECATED (decl)
16750 	  && deprecated_state != DEPRECATED_SUPPRESS)
16751 	{
16752 	  tree d = DECL_TEMPLATE_RESULT (decl);
16753 	  tree attr;
16754 	  if (TREE_CODE (d) == TYPE_DECL)
16755 	    attr = lookup_attribute ("deprecated",
16756 				     TYPE_ATTRIBUTES (TREE_TYPE (d)));
16757 	  else
16758 	    attr = lookup_attribute ("deprecated",
16759 				     DECL_ATTRIBUTES (d));
16760 	  warn_deprecated_use (decl, attr);
16761 	}
16762     }
16763   else
16764     {
16765       /* The standard does not explicitly indicate whether a name that
16766 	 names a set of overloaded declarations, some of which are
16767 	 templates, is a template-name.  However, such a name should
16768 	 be a template-name; otherwise, there is no way to form a
16769 	 template-id for the overloaded templates.  */
16770       bool found = false;
16771 
16772       for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16773 	   !found && iter; ++iter)
16774 	if (TREE_CODE (*iter) == TEMPLATE_DECL)
16775 	  found = true;
16776 
16777       if (!found
16778 	  && (cxx_dialect > cxx17)
16779 	  && !scoped_p
16780 	  && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16781 	  && tag_type == none_type)
16782 	{
16783 	  /* [temp.names] says "A name is also considered to refer to a template
16784 	     if it is an unqualified-id followed by a < and name lookup finds
16785 	     either one or more functions or finds nothing."  */
16786 
16787 	  /* The "more functions" case.  Just use the OVERLOAD as normally.
16788 	     We don't use is_overloaded_fn here to avoid considering
16789 	     BASELINKs.  */
16790 	  if (TREE_CODE (decl) == OVERLOAD
16791 	      /* Name lookup found one function.  */
16792 	      || TREE_CODE (decl) == FUNCTION_DECL)
16793 	    found = true;
16794 	  /* Name lookup found nothing.  */
16795 	  else if (decl == error_mark_node)
16796 	    return identifier;
16797 	}
16798 
16799       if (!found)
16800 	{
16801 	  /* The name does not name a template.  */
16802 	  cp_parser_error (parser, "expected template-name");
16803 	  return error_mark_node;
16804 	}
16805     }
16806 
16807   return decl;
16808 }
16809 
16810 /* Parse a template-argument-list.
16811 
16812    template-argument-list:
16813      template-argument ... [opt]
16814      template-argument-list , template-argument ... [opt]
16815 
16816    Returns a TREE_VEC containing the arguments.  */
16817 
16818 static tree
cp_parser_template_argument_list(cp_parser * parser)16819 cp_parser_template_argument_list (cp_parser* parser)
16820 {
16821   tree fixed_args[10];
16822   unsigned n_args = 0;
16823   unsigned alloced = 10;
16824   tree *arg_ary = fixed_args;
16825   tree vec;
16826   bool saved_in_template_argument_list_p;
16827   bool saved_ice_p;
16828   bool saved_non_ice_p;
16829 
16830   /* Don't create location wrapper nodes within a template-argument-list.  */
16831   auto_suppress_location_wrappers sentinel;
16832 
16833   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16834   parser->in_template_argument_list_p = true;
16835   /* Even if the template-id appears in an integral
16836      constant-expression, the contents of the argument list do
16837      not.  */
16838   saved_ice_p = parser->integral_constant_expression_p;
16839   parser->integral_constant_expression_p = false;
16840   saved_non_ice_p = parser->non_integral_constant_expression_p;
16841   parser->non_integral_constant_expression_p = false;
16842 
16843   /* Parse the arguments.  */
16844   do
16845     {
16846       tree argument;
16847 
16848       if (n_args)
16849 	/* Consume the comma.  */
16850 	cp_lexer_consume_token (parser->lexer);
16851 
16852       /* Parse the template-argument.  */
16853       argument = cp_parser_template_argument (parser);
16854 
16855       /* If the next token is an ellipsis, we're expanding a template
16856          argument pack. */
16857       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16858         {
16859 	  if (argument == error_mark_node)
16860 	    {
16861 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
16862 	      error_at (token->location,
16863 			"expected parameter pack before %<...%>");
16864 	    }
16865           /* Consume the `...' token. */
16866           cp_lexer_consume_token (parser->lexer);
16867 
16868           /* Make the argument into a TYPE_PACK_EXPANSION or
16869              EXPR_PACK_EXPANSION. */
16870           argument = make_pack_expansion (argument);
16871         }
16872 
16873       if (n_args == alloced)
16874 	{
16875 	  alloced *= 2;
16876 
16877 	  if (arg_ary == fixed_args)
16878 	    {
16879 	      arg_ary = XNEWVEC (tree, alloced);
16880 	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16881 	    }
16882 	  else
16883 	    arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16884 	}
16885       arg_ary[n_args++] = argument;
16886     }
16887   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16888 
16889   vec = make_tree_vec (n_args);
16890 
16891   while (n_args--)
16892     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16893 
16894   if (arg_ary != fixed_args)
16895     free (arg_ary);
16896   parser->non_integral_constant_expression_p = saved_non_ice_p;
16897   parser->integral_constant_expression_p = saved_ice_p;
16898   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16899   if (CHECKING_P)
16900     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16901   return vec;
16902 }
16903 
16904 /* Parse a template-argument.
16905 
16906    template-argument:
16907      assignment-expression
16908      type-id
16909      id-expression
16910 
16911    The representation is that of an assignment-expression, type-id, or
16912    id-expression -- except that the qualified id-expression is
16913    evaluated, so that the value returned is either a DECL or an
16914    OVERLOAD.
16915 
16916    Although the standard says "assignment-expression", it forbids
16917    throw-expressions or assignments in the template argument.
16918    Therefore, we use "conditional-expression" instead.  */
16919 
16920 static tree
cp_parser_template_argument(cp_parser * parser)16921 cp_parser_template_argument (cp_parser* parser)
16922 {
16923   tree argument;
16924   bool template_p;
16925   bool address_p;
16926   bool maybe_type_id = false;
16927   cp_token *token = NULL, *argument_start_token = NULL;
16928   location_t loc = 0;
16929   cp_id_kind idk;
16930 
16931   /* There's really no way to know what we're looking at, so we just
16932      try each alternative in order.
16933 
16934        [temp.arg]
16935 
16936        In a template-argument, an ambiguity between a type-id and an
16937        expression is resolved to a type-id, regardless of the form of
16938        the corresponding template-parameter.
16939 
16940      Therefore, we try a type-id first.  */
16941   cp_parser_parse_tentatively (parser);
16942   argument = cp_parser_template_type_arg (parser);
16943   /* If there was no error parsing the type-id but the next token is a
16944      '>>', our behavior depends on which dialect of C++ we're
16945      parsing. In C++98, we probably found a typo for '> >'. But there
16946      are type-id which are also valid expressions. For instance:
16947 
16948      struct X { int operator >> (int); };
16949      template <int V> struct Foo {};
16950      Foo<X () >> 5> r;
16951 
16952      Here 'X()' is a valid type-id of a function type, but the user just
16953      wanted to write the expression "X() >> 5". Thus, we remember that we
16954      found a valid type-id, but we still try to parse the argument as an
16955      expression to see what happens.
16956 
16957      In C++0x, the '>>' will be considered two separate '>'
16958      tokens.  */
16959   if (!cp_parser_error_occurred (parser)
16960       && cxx_dialect == cxx98
16961       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16962     {
16963       maybe_type_id = true;
16964       cp_parser_abort_tentative_parse (parser);
16965     }
16966   else
16967     {
16968       /* If the next token isn't a `,' or a `>', then this argument wasn't
16969       really finished. This means that the argument is not a valid
16970       type-id.  */
16971       if (!cp_parser_next_token_ends_template_argument_p (parser))
16972 	cp_parser_error (parser, "expected template-argument");
16973       /* If that worked, we're done.  */
16974       if (cp_parser_parse_definitely (parser))
16975 	return argument;
16976     }
16977   /* We're still not sure what the argument will be.  */
16978   cp_parser_parse_tentatively (parser);
16979   /* Try a template.  */
16980   argument_start_token = cp_lexer_peek_token (parser->lexer);
16981   argument = cp_parser_id_expression (parser,
16982 				      /*template_keyword_p=*/false,
16983 				      /*check_dependency_p=*/true,
16984 				      &template_p,
16985 				      /*declarator_p=*/false,
16986 				      /*optional_p=*/false);
16987   /* If the next token isn't a `,' or a `>', then this argument wasn't
16988      really finished.  */
16989   if (!cp_parser_next_token_ends_template_argument_p (parser))
16990     cp_parser_error (parser, "expected template-argument");
16991   if (!cp_parser_error_occurred (parser))
16992     {
16993       /* Figure out what is being referred to.  If the id-expression
16994 	 was for a class template specialization, then we will have a
16995 	 TYPE_DECL at this point.  There is no need to do name lookup
16996 	 at this point in that case.  */
16997       if (TREE_CODE (argument) != TYPE_DECL)
16998 	argument = cp_parser_lookup_name (parser, argument,
16999 					  none_type,
17000 					  /*is_template=*/template_p,
17001 					  /*is_namespace=*/false,
17002 					  /*check_dependency=*/true,
17003 					  /*ambiguous_decls=*/NULL,
17004 					  argument_start_token->location);
17005       /* Handle a constrained-type-specifier for a non-type template
17006 	 parameter.  */
17007       if (tree decl = cp_parser_maybe_concept_name (parser, argument))
17008 	argument = decl;
17009       else if (TREE_CODE (argument) != TEMPLATE_DECL
17010 	       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
17011 	cp_parser_error (parser, "expected template-name");
17012     }
17013   if (cp_parser_parse_definitely (parser))
17014     {
17015       if (TREE_DEPRECATED (argument))
17016 	warn_deprecated_use (argument, NULL_TREE);
17017       return argument;
17018     }
17019   /* It must be a non-type argument.  In C++17 any constant-expression is
17020      allowed.  */
17021   if (cxx_dialect > cxx14)
17022     goto general_expr;
17023 
17024   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17025 
17026      -- an integral constant-expression of integral or enumeration
17027 	type; or
17028 
17029      -- the name of a non-type template-parameter; or
17030 
17031      -- the name of an object or function with external linkage...
17032 
17033      -- the address of an object or function with external linkage...
17034 
17035      -- a pointer to member...  */
17036   /* Look for a non-type template parameter.  */
17037   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17038     {
17039       cp_parser_parse_tentatively (parser);
17040       argument = cp_parser_primary_expression (parser,
17041 					       /*address_p=*/false,
17042 					       /*cast_p=*/false,
17043 					       /*template_arg_p=*/true,
17044 					       &idk);
17045       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
17046 	  || !cp_parser_next_token_ends_template_argument_p (parser))
17047 	cp_parser_simulate_error (parser);
17048       if (cp_parser_parse_definitely (parser))
17049 	return argument;
17050     }
17051 
17052   /* If the next token is "&", the argument must be the address of an
17053      object or function with external linkage.  */
17054   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
17055   if (address_p)
17056     {
17057       loc = cp_lexer_peek_token (parser->lexer)->location;
17058       cp_lexer_consume_token (parser->lexer);
17059     }
17060   /* See if we might have an id-expression.  */
17061   token = cp_lexer_peek_token (parser->lexer);
17062   if (token->type == CPP_NAME
17063       || token->keyword == RID_OPERATOR
17064       || token->type == CPP_SCOPE
17065       || token->type == CPP_TEMPLATE_ID
17066       || token->type == CPP_NESTED_NAME_SPECIFIER)
17067     {
17068       cp_parser_parse_tentatively (parser);
17069       argument = cp_parser_primary_expression (parser,
17070 					       address_p,
17071 					       /*cast_p=*/false,
17072 					       /*template_arg_p=*/true,
17073 					       &idk);
17074       if (cp_parser_error_occurred (parser)
17075 	  || !cp_parser_next_token_ends_template_argument_p (parser))
17076 	cp_parser_abort_tentative_parse (parser);
17077       else
17078 	{
17079 	  tree probe;
17080 
17081 	  if (INDIRECT_REF_P (argument))
17082 	    {
17083 	      /* Strip the dereference temporarily.  */
17084 	      gcc_assert (REFERENCE_REF_P (argument));
17085 	      argument = TREE_OPERAND (argument, 0);
17086 	    }
17087 
17088 	  /* If we're in a template, we represent a qualified-id referring
17089 	     to a static data member as a SCOPE_REF even if the scope isn't
17090 	     dependent so that we can check access control later.  */
17091 	  probe = argument;
17092 	  if (TREE_CODE (probe) == SCOPE_REF)
17093 	    probe = TREE_OPERAND (probe, 1);
17094 	  if (VAR_P (probe))
17095 	    {
17096 	      /* A variable without external linkage might still be a
17097 		 valid constant-expression, so no error is issued here
17098 		 if the external-linkage check fails.  */
17099 	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
17100 		cp_parser_simulate_error (parser);
17101 	    }
17102 	  else if (is_overloaded_fn (argument))
17103 	    /* All overloaded functions are allowed; if the external
17104 	       linkage test does not pass, an error will be issued
17105 	       later.  */
17106 	    ;
17107 	  else if (address_p
17108 		   && (TREE_CODE (argument) == OFFSET_REF
17109 		       || TREE_CODE (argument) == SCOPE_REF))
17110 	    /* A pointer-to-member.  */
17111 	    ;
17112 	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17113 	    ;
17114 	  else
17115 	    cp_parser_simulate_error (parser);
17116 
17117 	  if (cp_parser_parse_definitely (parser))
17118 	    {
17119 	      if (address_p)
17120 		argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17121 					     tf_warning_or_error);
17122 	      else
17123 		argument = convert_from_reference (argument);
17124 	      return argument;
17125 	    }
17126 	}
17127     }
17128   /* If the argument started with "&", there are no other valid
17129      alternatives at this point.  */
17130   if (address_p)
17131     {
17132       cp_parser_error (parser, "invalid non-type template argument");
17133       return error_mark_node;
17134     }
17135 
17136  general_expr:
17137   /* If the argument wasn't successfully parsed as a type-id followed
17138      by '>>', the argument can only be a constant expression now.
17139      Otherwise, we try parsing the constant-expression tentatively,
17140      because the argument could really be a type-id.  */
17141   if (maybe_type_id)
17142     cp_parser_parse_tentatively (parser);
17143 
17144   if (cxx_dialect <= cxx14)
17145     argument = cp_parser_constant_expression (parser);
17146   else
17147     {
17148       /* In C++20, we can encounter a braced-init-list.  */
17149       if (cxx_dialect >= cxx2a
17150 	  && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17151 	{
17152 	  bool expr_non_constant_p;
17153 	  return cp_parser_braced_list (parser, &expr_non_constant_p);
17154 	}
17155 
17156       /* With C++17 generalized non-type template arguments we need to handle
17157 	 lvalue constant expressions, too.  */
17158       argument = cp_parser_assignment_expression (parser);
17159       require_potential_constant_expression (argument);
17160     }
17161 
17162   if (!maybe_type_id)
17163     return argument;
17164   if (!cp_parser_next_token_ends_template_argument_p (parser))
17165     cp_parser_error (parser, "expected template-argument");
17166   if (cp_parser_parse_definitely (parser))
17167     return argument;
17168   /* We did our best to parse the argument as a non type-id, but that
17169      was the only alternative that matched (albeit with a '>' after
17170      it). We can assume it's just a typo from the user, and a
17171      diagnostic will then be issued.  */
17172   return cp_parser_template_type_arg (parser);
17173 }
17174 
17175 /* Parse an explicit-instantiation.
17176 
17177    explicit-instantiation:
17178      template declaration
17179 
17180    Although the standard says `declaration', what it really means is:
17181 
17182    explicit-instantiation:
17183      template decl-specifier-seq [opt] declarator [opt] ;
17184 
17185    Things like `template int S<int>::i = 5, int S<double>::j;' are not
17186    supposed to be allowed.  A defect report has been filed about this
17187    issue.
17188 
17189    GNU Extension:
17190 
17191    explicit-instantiation:
17192      storage-class-specifier template
17193        decl-specifier-seq [opt] declarator [opt] ;
17194      function-specifier template
17195        decl-specifier-seq [opt] declarator [opt] ;  */
17196 
17197 static void
cp_parser_explicit_instantiation(cp_parser * parser)17198 cp_parser_explicit_instantiation (cp_parser* parser)
17199 {
17200   int declares_class_or_enum;
17201   cp_decl_specifier_seq decl_specifiers;
17202   tree extension_specifier = NULL_TREE;
17203 
17204   timevar_push (TV_TEMPLATE_INST);
17205 
17206   /* Look for an (optional) storage-class-specifier or
17207      function-specifier.  */
17208   if (cp_parser_allow_gnu_extensions_p (parser))
17209     {
17210       extension_specifier
17211 	= cp_parser_storage_class_specifier_opt (parser);
17212       if (!extension_specifier)
17213 	extension_specifier
17214 	  = cp_parser_function_specifier_opt (parser,
17215 					      /*decl_specs=*/NULL);
17216     }
17217 
17218   /* Look for the `template' keyword.  */
17219   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17220   /* Let the front end know that we are processing an explicit
17221      instantiation.  */
17222   begin_explicit_instantiation ();
17223   /* [temp.explicit] says that we are supposed to ignore access
17224      control while processing explicit instantiation directives.  */
17225   push_deferring_access_checks (dk_no_check);
17226   /* Parse a decl-specifier-seq.  */
17227   cp_parser_decl_specifier_seq (parser,
17228 				CP_PARSER_FLAGS_OPTIONAL,
17229 				&decl_specifiers,
17230 				&declares_class_or_enum);
17231   /* If there was exactly one decl-specifier, and it declared a class,
17232      and there's no declarator, then we have an explicit type
17233      instantiation.  */
17234   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17235     {
17236       tree type;
17237 
17238       type = check_tag_decl (&decl_specifiers,
17239 			     /*explicit_type_instantiation_p=*/true);
17240       /* Turn access control back on for names used during
17241 	 template instantiation.  */
17242       pop_deferring_access_checks ();
17243       if (type)
17244 	do_type_instantiation (type, extension_specifier,
17245 			       /*complain=*/tf_error);
17246     }
17247   else
17248     {
17249       cp_declarator *declarator;
17250       tree decl;
17251 
17252       /* Parse the declarator.  */
17253       declarator
17254 	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17255 				CP_PARSER_FLAGS_NONE,
17256 				/*ctor_dtor_or_conv_p=*/NULL,
17257 				/*parenthesized_p=*/NULL,
17258 				/*member_p=*/false,
17259 				/*friend_p=*/false,
17260 				/*static_p=*/false);
17261       if (declares_class_or_enum & 2)
17262 	cp_parser_check_for_definition_in_return_type (declarator,
17263 						       decl_specifiers.type,
17264 						       decl_specifiers.locations[ds_type_spec]);
17265       if (declarator != cp_error_declarator)
17266 	{
17267 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17268 	    permerror (decl_specifiers.locations[ds_inline],
17269 		       "explicit instantiation shall not use"
17270 		       " %<inline%> specifier");
17271 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17272 	    permerror (decl_specifiers.locations[ds_constexpr],
17273 		       "explicit instantiation shall not use"
17274 		       " %<constexpr%> specifier");
17275 
17276 	  decl = grokdeclarator (declarator, &decl_specifiers,
17277 				 NORMAL, 0, &decl_specifiers.attributes);
17278 	  /* Turn access control back on for names used during
17279 	     template instantiation.  */
17280 	  pop_deferring_access_checks ();
17281 	  /* Do the explicit instantiation.  */
17282 	  do_decl_instantiation (decl, extension_specifier);
17283 	}
17284       else
17285 	{
17286 	  pop_deferring_access_checks ();
17287 	  /* Skip the body of the explicit instantiation.  */
17288 	  cp_parser_skip_to_end_of_statement (parser);
17289 	}
17290     }
17291   /* We're done with the instantiation.  */
17292   end_explicit_instantiation ();
17293 
17294   cp_parser_consume_semicolon_at_end_of_statement (parser);
17295 
17296   timevar_pop (TV_TEMPLATE_INST);
17297 }
17298 
17299 /* Parse an explicit-specialization.
17300 
17301    explicit-specialization:
17302      template < > declaration
17303 
17304    Although the standard says `declaration', what it really means is:
17305 
17306    explicit-specialization:
17307      template <> decl-specifier [opt] init-declarator [opt] ;
17308      template <> function-definition
17309      template <> explicit-specialization
17310      template <> template-declaration  */
17311 
17312 static void
cp_parser_explicit_specialization(cp_parser * parser)17313 cp_parser_explicit_specialization (cp_parser* parser)
17314 {
17315   bool need_lang_pop;
17316   cp_token *token = cp_lexer_peek_token (parser->lexer);
17317 
17318   /* Look for the `template' keyword.  */
17319   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17320   /* Look for the `<'.  */
17321   cp_parser_require (parser, CPP_LESS, RT_LESS);
17322   /* Look for the `>'.  */
17323   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17324   /* We have processed another parameter list.  */
17325   ++parser->num_template_parameter_lists;
17326   /* [temp]
17327 
17328      A template ... explicit specialization ... shall not have C
17329      linkage.  */
17330   if (current_lang_name == lang_name_c)
17331     {
17332       error_at (token->location, "template specialization with C linkage");
17333       maybe_show_extern_c_location ();
17334       /* Give it C++ linkage to avoid confusing other parts of the
17335 	 front end.  */
17336       push_lang_context (lang_name_cplusplus);
17337       need_lang_pop = true;
17338     }
17339   else
17340     need_lang_pop = false;
17341   /* Let the front end know that we are beginning a specialization.  */
17342   if (!begin_specialization ())
17343     {
17344       end_specialization ();
17345       return;
17346     }
17347 
17348   /* If the next keyword is `template', we need to figure out whether
17349      or not we're looking a template-declaration.  */
17350   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17351     {
17352       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17353 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17354 	cp_parser_template_declaration_after_export (parser,
17355 						     /*member_p=*/false);
17356       else
17357 	cp_parser_explicit_specialization (parser);
17358     }
17359   else
17360     /* Parse the dependent declaration.  */
17361     cp_parser_single_declaration (parser,
17362 				  /*checks=*/NULL,
17363 				  /*member_p=*/false,
17364 				  /*explicit_specialization_p=*/true,
17365 				  /*friend_p=*/NULL);
17366   /* We're done with the specialization.  */
17367   end_specialization ();
17368   /* For the erroneous case of a template with C linkage, we pushed an
17369      implicit C++ linkage scope; exit that scope now.  */
17370   if (need_lang_pop)
17371     pop_lang_context ();
17372   /* We're done with this parameter list.  */
17373   --parser->num_template_parameter_lists;
17374 }
17375 
17376 /* Parse a type-specifier.
17377 
17378    type-specifier:
17379      simple-type-specifier
17380      class-specifier
17381      enum-specifier
17382      elaborated-type-specifier
17383      cv-qualifier
17384 
17385    GNU Extension:
17386 
17387    type-specifier:
17388      __complex__
17389 
17390    Returns a representation of the type-specifier.  For a
17391    class-specifier, enum-specifier, or elaborated-type-specifier, a
17392    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17393 
17394    The parser flags FLAGS is used to control type-specifier parsing.
17395 
17396    If IS_DECLARATION is TRUE, then this type-specifier is appearing
17397    in a decl-specifier-seq.
17398 
17399    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17400    class-specifier, enum-specifier, or elaborated-type-specifier, then
17401    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
17402    if a type is declared; 2 if it is defined.  Otherwise, it is set to
17403    zero.
17404 
17405    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17406    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
17407    is set to FALSE.  */
17408 
17409 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)17410 cp_parser_type_specifier (cp_parser* parser,
17411 			  cp_parser_flags flags,
17412 			  cp_decl_specifier_seq *decl_specs,
17413 			  bool is_declaration,
17414 			  int* declares_class_or_enum,
17415 			  bool* is_cv_qualifier)
17416 {
17417   tree type_spec = NULL_TREE;
17418   cp_token *token;
17419   enum rid keyword;
17420   cp_decl_spec ds = ds_last;
17421 
17422   /* Assume this type-specifier does not declare a new type.  */
17423   if (declares_class_or_enum)
17424     *declares_class_or_enum = 0;
17425   /* And that it does not specify a cv-qualifier.  */
17426   if (is_cv_qualifier)
17427     *is_cv_qualifier = false;
17428   /* Peek at the next token.  */
17429   token = cp_lexer_peek_token (parser->lexer);
17430 
17431   /* If we're looking at a keyword, we can use that to guide the
17432      production we choose.  */
17433   keyword = token->keyword;
17434   switch (keyword)
17435     {
17436     case RID_ENUM:
17437       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17438 	goto elaborated_type_specifier;
17439 
17440       /* Look for the enum-specifier.  */
17441       type_spec = cp_parser_enum_specifier (parser);
17442       /* If that worked, we're done.  */
17443       if (type_spec)
17444 	{
17445 	  if (declares_class_or_enum)
17446 	    *declares_class_or_enum = 2;
17447 	  if (decl_specs)
17448 	    cp_parser_set_decl_spec_type (decl_specs,
17449 					  type_spec,
17450 					  token,
17451 					  /*type_definition_p=*/true);
17452 	  return type_spec;
17453 	}
17454       else
17455 	goto elaborated_type_specifier;
17456 
17457       /* Any of these indicate either a class-specifier, or an
17458 	 elaborated-type-specifier.  */
17459     case RID_CLASS:
17460     case RID_STRUCT:
17461     case RID_UNION:
17462       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17463 	goto elaborated_type_specifier;
17464 
17465       /* Parse tentatively so that we can back up if we don't find a
17466 	 class-specifier.  */
17467       cp_parser_parse_tentatively (parser);
17468       /* Look for the class-specifier.  */
17469       type_spec = cp_parser_class_specifier (parser);
17470       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17471       /* If that worked, we're done.  */
17472       if (cp_parser_parse_definitely (parser))
17473 	{
17474 	  if (declares_class_or_enum)
17475 	    *declares_class_or_enum = 2;
17476 	  if (decl_specs)
17477 	    cp_parser_set_decl_spec_type (decl_specs,
17478 					  type_spec,
17479 					  token,
17480 					  /*type_definition_p=*/true);
17481 	  return type_spec;
17482 	}
17483 
17484       /* Fall through.  */
17485     elaborated_type_specifier:
17486       /* We're declaring (not defining) a class or enum.  */
17487       if (declares_class_or_enum)
17488 	*declares_class_or_enum = 1;
17489 
17490       /* Fall through.  */
17491     case RID_TYPENAME:
17492       /* Look for an elaborated-type-specifier.  */
17493       type_spec
17494 	= (cp_parser_elaborated_type_specifier
17495 	   (parser,
17496 	    decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17497 	    is_declaration));
17498       if (decl_specs)
17499 	cp_parser_set_decl_spec_type (decl_specs,
17500 				      type_spec,
17501 				      token,
17502 				      /*type_definition_p=*/false);
17503       return type_spec;
17504 
17505     case RID_CONST:
17506       ds = ds_const;
17507       if (is_cv_qualifier)
17508 	*is_cv_qualifier = true;
17509       break;
17510 
17511     case RID_VOLATILE:
17512       ds = ds_volatile;
17513       if (is_cv_qualifier)
17514 	*is_cv_qualifier = true;
17515       break;
17516 
17517     case RID_RESTRICT:
17518       ds = ds_restrict;
17519       if (is_cv_qualifier)
17520 	*is_cv_qualifier = true;
17521       break;
17522 
17523     case RID_COMPLEX:
17524       /* The `__complex__' keyword is a GNU extension.  */
17525       ds = ds_complex;
17526       break;
17527 
17528     default:
17529       break;
17530     }
17531 
17532   /* Handle simple keywords.  */
17533   if (ds != ds_last)
17534     {
17535       if (decl_specs)
17536 	{
17537 	  set_and_check_decl_spec_loc (decl_specs, ds, token);
17538 	  decl_specs->any_specifiers_p = true;
17539 	}
17540       return cp_lexer_consume_token (parser->lexer)->u.value;
17541     }
17542 
17543   /* If we do not already have a type-specifier, assume we are looking
17544      at a simple-type-specifier.  */
17545   type_spec = cp_parser_simple_type_specifier (parser,
17546 					       decl_specs,
17547 					       flags);
17548 
17549   /* If we didn't find a type-specifier, and a type-specifier was not
17550      optional in this context, issue an error message.  */
17551   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17552     {
17553       cp_parser_error (parser, "expected type specifier");
17554       return error_mark_node;
17555     }
17556 
17557   return type_spec;
17558 }
17559 
17560 /* Parse a simple-type-specifier.
17561 
17562    simple-type-specifier:
17563      :: [opt] nested-name-specifier [opt] type-name
17564      :: [opt] nested-name-specifier template template-id
17565      char
17566      wchar_t
17567      bool
17568      short
17569      int
17570      long
17571      signed
17572      unsigned
17573      float
17574      double
17575      void
17576 
17577    C++11 Extension:
17578 
17579    simple-type-specifier:
17580      auto
17581      decltype ( expression )
17582      char16_t
17583      char32_t
17584      __underlying_type ( type-id )
17585 
17586    C++17 extension:
17587 
17588      nested-name-specifier(opt) template-name
17589 
17590    GNU Extension:
17591 
17592    simple-type-specifier:
17593      __int128
17594      __typeof__ unary-expression
17595      __typeof__ ( type-id )
17596      __typeof__ ( type-id ) { initializer-list , [opt] }
17597 
17598    Concepts Extension:
17599 
17600    simple-type-specifier:
17601      constrained-type-specifier
17602 
17603    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
17604    appropriately updated.  */
17605 
17606 static tree
cp_parser_simple_type_specifier(cp_parser * parser,cp_decl_specifier_seq * decl_specs,cp_parser_flags flags)17607 cp_parser_simple_type_specifier (cp_parser* parser,
17608 				 cp_decl_specifier_seq *decl_specs,
17609 				 cp_parser_flags flags)
17610 {
17611   tree type = NULL_TREE;
17612   cp_token *token;
17613   int idx;
17614 
17615   /* Peek at the next token.  */
17616   token = cp_lexer_peek_token (parser->lexer);
17617 
17618   /* If we're looking at a keyword, things are easy.  */
17619   switch (token->keyword)
17620     {
17621     case RID_CHAR:
17622       if (decl_specs)
17623 	decl_specs->explicit_char_p = true;
17624       type = char_type_node;
17625       break;
17626     case RID_CHAR8:
17627       type = char8_type_node;
17628       break;
17629     case RID_CHAR16:
17630       type = char16_type_node;
17631       break;
17632     case RID_CHAR32:
17633       type = char32_type_node;
17634       break;
17635     case RID_WCHAR:
17636       type = wchar_type_node;
17637       break;
17638     case RID_BOOL:
17639       type = boolean_type_node;
17640       break;
17641     case RID_SHORT:
17642       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17643       type = short_integer_type_node;
17644       break;
17645     case RID_INT:
17646       if (decl_specs)
17647 	decl_specs->explicit_int_p = true;
17648       type = integer_type_node;
17649       break;
17650     case RID_INT_N_0:
17651     case RID_INT_N_1:
17652     case RID_INT_N_2:
17653     case RID_INT_N_3:
17654       idx = token->keyword - RID_INT_N_0;
17655       if (! int_n_enabled_p [idx])
17656 	break;
17657       if (decl_specs)
17658 	{
17659 	  decl_specs->explicit_intN_p = true;
17660 	  decl_specs->int_n_idx = idx;
17661 	}
17662       type = int_n_trees [idx].signed_type;
17663       break;
17664     case RID_LONG:
17665       if (decl_specs)
17666 	set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17667       type = long_integer_type_node;
17668       break;
17669     case RID_SIGNED:
17670       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17671       type = integer_type_node;
17672       break;
17673     case RID_UNSIGNED:
17674       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17675       type = unsigned_type_node;
17676       break;
17677     case RID_FLOAT:
17678       type = float_type_node;
17679       break;
17680     case RID_DOUBLE:
17681       type = double_type_node;
17682       break;
17683     case RID_VOID:
17684       type = void_type_node;
17685       break;
17686 
17687     case RID_AUTO:
17688       maybe_warn_cpp0x (CPP0X_AUTO);
17689       if (parser->auto_is_implicit_function_template_parm_p)
17690 	{
17691 	  /* The 'auto' might be the placeholder return type for a function decl
17692 	     with trailing return type.  */
17693 	  bool have_trailing_return_fn_decl = false;
17694 
17695 	  cp_parser_parse_tentatively (parser);
17696 	  cp_lexer_consume_token (parser->lexer);
17697 	  while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17698 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17699 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17700 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17701 	    {
17702 	      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17703 		{
17704 		  cp_lexer_consume_token (parser->lexer);
17705 		  cp_parser_skip_to_closing_parenthesis (parser,
17706 							 /*recovering*/false,
17707 							 /*or_comma*/false,
17708 							 /*consume_paren*/true);
17709 		  continue;
17710 		}
17711 
17712 	      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17713 		{
17714 		  have_trailing_return_fn_decl = true;
17715 		  break;
17716 		}
17717 
17718 	      cp_lexer_consume_token (parser->lexer);
17719 	    }
17720 	  cp_parser_abort_tentative_parse (parser);
17721 
17722 	  if (have_trailing_return_fn_decl)
17723 	    {
17724 	      type = make_auto ();
17725 	      break;
17726 	    }
17727 
17728 	  if (cxx_dialect >= cxx14)
17729 	    {
17730 	      type = synthesize_implicit_template_parm (parser, NULL_TREE);
17731 	      type = TREE_TYPE (type);
17732 	    }
17733 	  else
17734 	    type = error_mark_node;
17735 
17736 	  if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17737 	    {
17738 	      if (cxx_dialect < cxx14)
17739 		error_at (token->location,
17740 			 "use of %<auto%> in lambda parameter declaration "
17741 			 "only available with "
17742 			 "%<-std=c++14%> or %<-std=gnu++14%>");
17743 	    }
17744 	  else if (cxx_dialect < cxx14)
17745 	    error_at (token->location,
17746 		     "use of %<auto%> in parameter declaration "
17747 		     "only available with "
17748 		     "%<-std=c++14%> or %<-std=gnu++14%>");
17749 	  else if (!flag_concepts)
17750 	    pedwarn (token->location, 0,
17751 		     "use of %<auto%> in parameter declaration "
17752 		     "only available with %<-fconcepts%>");
17753 	}
17754       else
17755 	type = make_auto ();
17756       break;
17757 
17758     case RID_DECLTYPE:
17759       /* Since DR 743, decltype can either be a simple-type-specifier by
17760 	 itself or begin a nested-name-specifier.  Parsing it will replace
17761 	 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17762 	 handling below decide what to do.  */
17763       cp_parser_decltype (parser);
17764       cp_lexer_set_token_position (parser->lexer, token);
17765       break;
17766 
17767     case RID_TYPEOF:
17768       /* Consume the `typeof' token.  */
17769       cp_lexer_consume_token (parser->lexer);
17770       /* Parse the operand to `typeof'.  */
17771       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17772       /* If it is not already a TYPE, take its type.  */
17773       if (!TYPE_P (type))
17774 	type = finish_typeof (type);
17775 
17776       if (decl_specs)
17777 	cp_parser_set_decl_spec_type (decl_specs, type,
17778 				      token,
17779 				      /*type_definition_p=*/false);
17780 
17781       return type;
17782 
17783     case RID_UNDERLYING_TYPE:
17784       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17785       if (decl_specs)
17786 	cp_parser_set_decl_spec_type (decl_specs, type,
17787 				      token,
17788 				      /*type_definition_p=*/false);
17789 
17790       return type;
17791 
17792     case RID_BASES:
17793     case RID_DIRECT_BASES:
17794       type = cp_parser_trait_expr (parser, token->keyword);
17795       if (decl_specs)
17796        cp_parser_set_decl_spec_type (decl_specs, type,
17797                                      token,
17798                                      /*type_definition_p=*/false);
17799       return type;
17800     default:
17801       break;
17802     }
17803 
17804   /* If token is an already-parsed decltype not followed by ::,
17805      it's a simple-type-specifier.  */
17806   if (token->type == CPP_DECLTYPE
17807       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17808     {
17809       type = saved_checks_value (token->u.tree_check_value);
17810       if (decl_specs)
17811 	{
17812 	  cp_parser_set_decl_spec_type (decl_specs, type,
17813 					token,
17814 					/*type_definition_p=*/false);
17815 	  /* Remember that we are handling a decltype in order to
17816 	     implement the resolution of DR 1510 when the argument
17817 	     isn't instantiation dependent.  */
17818 	  decl_specs->decltype_p = true;
17819 	}
17820       cp_lexer_consume_token (parser->lexer);
17821       return type;
17822     }
17823 
17824   /* If the type-specifier was for a built-in type, we're done.  */
17825   if (type)
17826     {
17827       /* Record the type.  */
17828       if (decl_specs
17829 	  && (token->keyword != RID_SIGNED
17830 	      && token->keyword != RID_UNSIGNED
17831 	      && token->keyword != RID_SHORT
17832 	      && token->keyword != RID_LONG))
17833 	cp_parser_set_decl_spec_type (decl_specs,
17834 				      type,
17835 				      token,
17836 				      /*type_definition_p=*/false);
17837       if (decl_specs)
17838 	decl_specs->any_specifiers_p = true;
17839 
17840       /* Consume the token.  */
17841       cp_lexer_consume_token (parser->lexer);
17842 
17843       if (type == error_mark_node)
17844 	return error_mark_node;
17845 
17846       /* There is no valid C++ program where a non-template type is
17847 	 followed by a "<".  That usually indicates that the user thought
17848 	 that the type was a template.  */
17849       cp_parser_check_for_invalid_template_id (parser, type, none_type,
17850 					       token->location);
17851 
17852       return TYPE_NAME (type);
17853     }
17854 
17855   /* The type-specifier must be a user-defined type.  */
17856   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17857     {
17858       bool qualified_p;
17859       bool global_p;
17860       const bool typename_p = (cxx_dialect >= cxx2a
17861 			       && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17862 
17863       /* Don't gobble tokens or issue error messages if this is an
17864 	 optional type-specifier.  */
17865       if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17866 	cp_parser_parse_tentatively (parser);
17867 
17868       token = cp_lexer_peek_token (parser->lexer);
17869 
17870       /* Look for the optional `::' operator.  */
17871       global_p
17872 	= (cp_parser_global_scope_opt (parser,
17873 				       /*current_scope_valid_p=*/false)
17874 	   != NULL_TREE);
17875       /* Look for the nested-name specifier.  */
17876       qualified_p
17877 	= (cp_parser_nested_name_specifier_opt (parser,
17878 						/*typename_keyword_p=*/false,
17879 						/*check_dependency_p=*/true,
17880 						/*type_p=*/false,
17881 						/*is_declaration=*/false)
17882 	   != NULL_TREE);
17883       /* If we have seen a nested-name-specifier, and the next token
17884 	 is `template', then we are using the template-id production.  */
17885       if (parser->scope
17886 	  && cp_parser_optional_template_keyword (parser))
17887 	{
17888 	  /* Look for the template-id.  */
17889 	  type = cp_parser_template_id (parser,
17890 					/*template_keyword_p=*/true,
17891 					/*check_dependency_p=*/true,
17892 					none_type,
17893 					/*is_declaration=*/false);
17894 	  /* If the template-id did not name a type, we are out of
17895 	     luck.  */
17896 	  if (TREE_CODE (type) != TYPE_DECL)
17897 	    {
17898 	      /* ...unless we pretend we have seen 'typename'.  */
17899 	      if (typename_p)
17900 		type = cp_parser_make_typename_type (parser, type,
17901 						     token->location);
17902 	      else
17903 		{
17904 		  cp_parser_error (parser, "expected template-id for type");
17905 		  type = NULL_TREE;
17906 		}
17907 	    }
17908 	}
17909       /* Otherwise, look for a type-name.  */
17910       else
17911 	type = cp_parser_type_name (parser, (qualified_p && typename_p));
17912 
17913       /* Keep track of all name-lookups performed in class scopes.  */
17914       if (type
17915 	  && !global_p
17916 	  && !qualified_p
17917 	  && TREE_CODE (type) == TYPE_DECL
17918 	  && identifier_p (DECL_NAME (type)))
17919 	maybe_note_name_used_in_class (DECL_NAME (type), type);
17920       /* If it didn't work out, we don't have a TYPE.  */
17921       if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17922 	  && !cp_parser_parse_definitely (parser))
17923 	type = NULL_TREE;
17924       if (!type && cxx_dialect >= cxx17)
17925 	{
17926 	  if (flags & CP_PARSER_FLAGS_OPTIONAL)
17927 	    cp_parser_parse_tentatively (parser);
17928 
17929 	  cp_parser_global_scope_opt (parser,
17930 				      /*current_scope_valid_p=*/false);
17931 	  cp_parser_nested_name_specifier_opt (parser,
17932 					       /*typename_keyword_p=*/false,
17933 					       /*check_dependency_p=*/true,
17934 					       /*type_p=*/false,
17935 					       /*is_declaration=*/false);
17936 	  tree name = cp_parser_identifier (parser);
17937 	  if (name && TREE_CODE (name) == IDENTIFIER_NODE
17938 	      && parser->scope != error_mark_node)
17939 	    {
17940 	      tree tmpl = cp_parser_lookup_name (parser, name,
17941 						 none_type,
17942 						 /*is_template=*/false,
17943 						 /*is_namespace=*/false,
17944 						 /*check_dependency=*/true,
17945 						 /*ambiguous_decls=*/NULL,
17946 						 token->location);
17947 	      if (tmpl && tmpl != error_mark_node
17948 		  && (DECL_CLASS_TEMPLATE_P (tmpl)
17949 		      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17950 		type = make_template_placeholder (tmpl);
17951 	      else
17952 		{
17953 		  type = error_mark_node;
17954 		  if (!cp_parser_simulate_error (parser))
17955 		    cp_parser_name_lookup_error (parser, name, tmpl,
17956 						 NLE_TYPE, token->location);
17957 		}
17958 	    }
17959 	  else
17960 	    type = error_mark_node;
17961 
17962 	  if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17963 	      && !cp_parser_parse_definitely (parser))
17964 	    type = NULL_TREE;
17965 	}
17966       if (type && decl_specs)
17967 	cp_parser_set_decl_spec_type (decl_specs, type,
17968 				      token,
17969 				      /*type_definition_p=*/false);
17970     }
17971 
17972   /* If we didn't get a type-name, issue an error message.  */
17973   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17974     {
17975       cp_parser_error (parser, "expected type-name");
17976       return error_mark_node;
17977     }
17978 
17979   if (type && type != error_mark_node)
17980     {
17981       /* See if TYPE is an Objective-C type, and if so, parse and
17982 	 accept any protocol references following it.  Do this before
17983 	 the cp_parser_check_for_invalid_template_id() call, because
17984 	 Objective-C types can be followed by '<...>' which would
17985 	 enclose protocol names rather than template arguments, and so
17986 	 everything is fine.  */
17987       if (c_dialect_objc () && !parser->scope
17988 	  && (objc_is_id (type) || objc_is_class_name (type)))
17989 	{
17990 	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
17991 	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
17992 
17993 	  /* Clobber the "unqualified" type previously entered into
17994 	     DECL_SPECS with the new, improved protocol-qualified version.  */
17995 	  if (decl_specs)
17996 	    decl_specs->type = qual_type;
17997 
17998 	  return qual_type;
17999 	}
18000 
18001       /* There is no valid C++ program where a non-template type is
18002 	 followed by a "<".  That usually indicates that the user
18003 	 thought that the type was a template.  */
18004       cp_parser_check_for_invalid_template_id (parser, type,
18005 					       none_type,
18006 					       token->location);
18007     }
18008 
18009   return type;
18010 }
18011 
18012 /* Parse a type-name.
18013 
18014    type-name:
18015      class-name
18016      enum-name
18017      typedef-name
18018      simple-template-id [in c++0x]
18019 
18020    enum-name:
18021      identifier
18022 
18023    typedef-name:
18024      identifier
18025 
18026   Concepts:
18027 
18028    type-name:
18029      concept-name
18030      partial-concept-id
18031 
18032    concept-name:
18033      identifier
18034 
18035    Returns a TYPE_DECL for the type.  */
18036 
18037 static tree
cp_parser_type_name(cp_parser * parser,bool typename_keyword_p)18038 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
18039 {
18040   tree type_decl;
18041 
18042   /* We can't know yet whether it is a class-name or not.  */
18043   cp_parser_parse_tentatively (parser);
18044   /* Try a class-name.  */
18045   type_decl = cp_parser_class_name (parser,
18046 				    typename_keyword_p,
18047 				    /*template_keyword_p=*/false,
18048 				    none_type,
18049 				    /*check_dependency_p=*/true,
18050 				    /*class_head_p=*/false,
18051 				    /*is_declaration=*/false);
18052   /* If it's not a class-name, keep looking.  */
18053   if (!cp_parser_parse_definitely (parser))
18054     {
18055       if (cxx_dialect < cxx11)
18056 	/* It must be a typedef-name or an enum-name.  */
18057 	return cp_parser_nonclass_name (parser);
18058 
18059       cp_parser_parse_tentatively (parser);
18060       /* It is either a simple-template-id representing an
18061 	 instantiation of an alias template...  */
18062       type_decl = cp_parser_template_id (parser,
18063 					 /*template_keyword_p=*/false,
18064 					 /*check_dependency_p=*/true,
18065 					 none_type,
18066 					 /*is_declaration=*/false);
18067       /* Note that this must be an instantiation of an alias template
18068 	 because [temp.names]/6 says:
18069 
18070 	     A template-id that names an alias template specialization
18071 	     is a type-name.
18072 
18073 	 Whereas [temp.names]/7 says:
18074 
18075 	     A simple-template-id that names a class template
18076 	     specialization is a class-name.
18077 
18078          With concepts, this could also be a partial-concept-id that
18079          declares a non-type template parameter. */
18080       if (type_decl != NULL_TREE
18081 	  && TREE_CODE (type_decl) == TYPE_DECL
18082 	  && TYPE_DECL_ALIAS_P (type_decl))
18083 	gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
18084       else if (is_constrained_parameter (type_decl))
18085         /* Don't do anything. */ ;
18086       else
18087 	cp_parser_simulate_error (parser);
18088 
18089       if (!cp_parser_parse_definitely (parser))
18090 	/* ... Or a typedef-name or an enum-name.  */
18091 	return cp_parser_nonclass_name (parser);
18092     }
18093 
18094   return type_decl;
18095 }
18096 
18097 /*  Check if DECL and ARGS can form a constrained-type-specifier.
18098     If ARGS is non-null, we try to form a concept check of the
18099     form DECL<?, ARGS> where ? is a wildcard that matches any
18100     kind of template argument. If ARGS is NULL, then we try to
18101     form a concept check of the form DECL<?>. */
18102 
18103 static tree
cp_parser_maybe_constrained_type_specifier(cp_parser * parser,tree decl,tree args)18104 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
18105 					    tree decl, tree args)
18106 {
18107   gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18108 
18109   /* If we a constrained-type-specifier cannot be deduced. */
18110   if (parser->prevent_constrained_type_specifiers)
18111     return NULL_TREE;
18112 
18113   /* A constrained type specifier can only be found in an
18114      overload set or as a reference to a template declaration.
18115 
18116      FIXME: This might be masking a bug.  It's possible that
18117      that the deduction below is causing template specializations
18118      to be formed with the wildcard as an argument.  */
18119   if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18120     return NULL_TREE;
18121 
18122   /* Try to build a call expression that evaluates the
18123      concept. This can fail if the overload set refers
18124      only to non-templates. */
18125   tree placeholder = build_nt (WILDCARD_DECL);
18126   tree check = build_concept_check (decl, placeholder, args);
18127   if (check == error_mark_node)
18128     return NULL_TREE;
18129 
18130   /* Deduce the checked constraint and the prototype parameter.
18131 
18132      FIXME: In certain cases, failure to deduce should be a
18133      diagnosable error.  */
18134   tree conc;
18135   tree proto;
18136   if (!deduce_constrained_parameter (check, conc, proto))
18137     return NULL_TREE;
18138 
18139   /* In template parameter scope, this results in a constrained
18140      parameter. Return a descriptor of that parm. */
18141   if (processing_template_parmlist)
18142     return build_constrained_parameter (conc, proto, args);
18143 
18144   /* In a parameter-declaration-clause, constrained-type
18145      specifiers result in invented template parameters.  */
18146   if (parser->auto_is_implicit_function_template_parm_p)
18147     {
18148       tree x = build_constrained_parameter (conc, proto, args);
18149       return synthesize_implicit_template_parm (parser, x);
18150     }
18151   else
18152     {
18153      /* Otherwise, we're in a context where the constrained
18154         type name is deduced and the constraint applies
18155         after deduction. */
18156       return make_constrained_auto (conc, args);
18157     }
18158 
18159   return NULL_TREE;
18160 }
18161 
18162 /* If DECL refers to a concept, return a TYPE_DECL representing
18163    the result of using the constrained type specifier in the
18164    current context.  DECL refers to a concept if
18165 
18166   - it is an overload set containing a function concept taking a single
18167     type argument, or
18168 
18169   - it is a variable concept taking a single type argument.  */
18170 
18171 static tree
cp_parser_maybe_concept_name(cp_parser * parser,tree decl)18172 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18173 {
18174   if (flag_concepts
18175       && (TREE_CODE (decl) == OVERLOAD
18176 	  || BASELINK_P (decl)
18177 	  || variable_concept_p (decl)))
18178     return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18179   else
18180     return NULL_TREE;
18181 }
18182 
18183 /* Check if DECL and ARGS form a partial-concept-id.  If so,
18184    assign ID to the resulting constrained placeholder.
18185 
18186    Returns true if the partial-concept-id designates a placeholder
18187    and false otherwise. Note that *id is set to NULL_TREE in
18188    this case. */
18189 
18190 static tree
cp_parser_maybe_partial_concept_id(cp_parser * parser,tree decl,tree args)18191 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18192 {
18193   return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18194 }
18195 
18196 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18197    or a concept-name.
18198 
18199    enum-name:
18200      identifier
18201 
18202    typedef-name:
18203      identifier
18204 
18205    concept-name:
18206      identifier
18207 
18208    Returns a TYPE_DECL for the type.  */
18209 
18210 static tree
cp_parser_nonclass_name(cp_parser * parser)18211 cp_parser_nonclass_name (cp_parser* parser)
18212 {
18213   tree type_decl;
18214   tree identifier;
18215 
18216   cp_token *token = cp_lexer_peek_token (parser->lexer);
18217   identifier = cp_parser_identifier (parser);
18218   if (identifier == error_mark_node)
18219     return error_mark_node;
18220 
18221   /* Look up the type-name.  */
18222   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18223 
18224   type_decl = strip_using_decl (type_decl);
18225 
18226   /* If we found an overload set, then it may refer to a concept-name. */
18227   if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18228     type_decl = decl;
18229 
18230   if (TREE_CODE (type_decl) != TYPE_DECL
18231       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18232     {
18233       /* See if this is an Objective-C type.  */
18234       tree protos = cp_parser_objc_protocol_refs_opt (parser);
18235       tree type = objc_get_protocol_qualified_type (identifier, protos);
18236       if (type)
18237 	type_decl = TYPE_NAME (type);
18238     }
18239 
18240   /* Issue an error if we did not find a type-name.  */
18241   if (TREE_CODE (type_decl) != TYPE_DECL
18242       /* In Objective-C, we have the complication that class names are
18243 	 normally type names and start declarations (eg, the
18244 	 "NSObject" in "NSObject *object;"), but can be used in an
18245 	 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18246 	 is an expression.  So, a classname followed by a dot is not a
18247 	 valid type-name.  */
18248       || (objc_is_class_name (TREE_TYPE (type_decl))
18249 	  && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18250     {
18251       if (!cp_parser_simulate_error (parser))
18252 	cp_parser_name_lookup_error (parser, identifier, type_decl,
18253 				     NLE_TYPE, token->location);
18254       return error_mark_node;
18255     }
18256   /* Remember that the name was used in the definition of the
18257      current class so that we can check later to see if the
18258      meaning would have been different after the class was
18259      entirely defined.  */
18260   else if (type_decl != error_mark_node
18261 	   && !parser->scope)
18262     maybe_note_name_used_in_class (identifier, type_decl);
18263 
18264   return type_decl;
18265 }
18266 
18267 /* Parse an elaborated-type-specifier.  Note that the grammar given
18268    here incorporates the resolution to DR68.
18269 
18270    elaborated-type-specifier:
18271      class-key :: [opt] nested-name-specifier [opt] identifier
18272      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18273      enum-key :: [opt] nested-name-specifier [opt] identifier
18274      typename :: [opt] nested-name-specifier identifier
18275      typename :: [opt] nested-name-specifier template [opt]
18276        template-id
18277 
18278    GNU extension:
18279 
18280    elaborated-type-specifier:
18281      class-key attributes :: [opt] nested-name-specifier [opt] identifier
18282      class-key attributes :: [opt] nested-name-specifier [opt]
18283 	       template [opt] template-id
18284      enum attributes :: [opt] nested-name-specifier [opt] identifier
18285 
18286    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18287    declared `friend'.  If IS_DECLARATION is TRUE, then this
18288    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18289    something is being declared.
18290 
18291    Returns the TYPE specified.  */
18292 
18293 static tree
cp_parser_elaborated_type_specifier(cp_parser * parser,bool is_friend,bool is_declaration)18294 cp_parser_elaborated_type_specifier (cp_parser* parser,
18295 				     bool is_friend,
18296 				     bool is_declaration)
18297 {
18298   enum tag_types tag_type;
18299   tree identifier;
18300   tree type = NULL_TREE;
18301   tree attributes = NULL_TREE;
18302   tree globalscope;
18303   cp_token *token = NULL;
18304 
18305   /* See if we're looking at the `enum' keyword.  */
18306   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18307     {
18308       /* Consume the `enum' token.  */
18309       cp_lexer_consume_token (parser->lexer);
18310       /* Remember that it's an enumeration type.  */
18311       tag_type = enum_type;
18312       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18313 	 enums) is used here.  */
18314       cp_token *token = cp_lexer_peek_token (parser->lexer);
18315       if (cp_parser_is_keyword (token, RID_CLASS)
18316 	  || cp_parser_is_keyword (token, RID_STRUCT))
18317 	{
18318 	  gcc_rich_location richloc (token->location);
18319 	  richloc.add_range (input_location);
18320 	  richloc.add_fixit_remove ();
18321 	  pedwarn (&richloc, 0, "elaborated-type-specifier for "
18322 		   "a scoped enum must not use the %qD keyword",
18323 		   token->u.value);
18324 	  /* Consume the `struct' or `class' and parse it anyway.  */
18325 	  cp_lexer_consume_token (parser->lexer);
18326 	}
18327       /* Parse the attributes.  */
18328       attributes = cp_parser_attributes_opt (parser);
18329     }
18330   /* Or, it might be `typename'.  */
18331   else if (cp_lexer_next_token_is_keyword (parser->lexer,
18332 					   RID_TYPENAME))
18333     {
18334       /* Consume the `typename' token.  */
18335       cp_lexer_consume_token (parser->lexer);
18336       /* Remember that it's a `typename' type.  */
18337       tag_type = typename_type;
18338     }
18339   /* Otherwise it must be a class-key.  */
18340   else
18341     {
18342       tag_type = cp_parser_class_key (parser);
18343       if (tag_type == none_type)
18344 	return error_mark_node;
18345       /* Parse the attributes.  */
18346       attributes = cp_parser_attributes_opt (parser);
18347     }
18348 
18349   /* Look for the `::' operator.  */
18350   globalscope =  cp_parser_global_scope_opt (parser,
18351 					     /*current_scope_valid_p=*/false);
18352   /* Look for the nested-name-specifier.  */
18353   tree nested_name_specifier;
18354   if (tag_type == typename_type && !globalscope)
18355     {
18356       nested_name_specifier
18357 	= cp_parser_nested_name_specifier (parser,
18358 					   /*typename_keyword_p=*/true,
18359 					   /*check_dependency_p=*/true,
18360 					   /*type_p=*/true,
18361 					   is_declaration);
18362       if (!nested_name_specifier)
18363 	return error_mark_node;
18364     }
18365   else
18366     /* Even though `typename' is not present, the proposed resolution
18367        to Core Issue 180 says that in `class A<T>::B', `B' should be
18368        considered a type-name, even if `A<T>' is dependent.  */
18369     nested_name_specifier
18370       = cp_parser_nested_name_specifier_opt (parser,
18371 					     /*typename_keyword_p=*/true,
18372 					     /*check_dependency_p=*/true,
18373 					     /*type_p=*/true,
18374 					     is_declaration);
18375  /* For everything but enumeration types, consider a template-id.
18376     For an enumeration type, consider only a plain identifier.  */
18377   if (tag_type != enum_type)
18378     {
18379       bool template_p = false;
18380       tree decl;
18381 
18382       /* Allow the `template' keyword.  */
18383       template_p = cp_parser_optional_template_keyword (parser);
18384       /* If we didn't see `template', we don't know if there's a
18385 	 template-id or not.  */
18386       if (!template_p)
18387 	cp_parser_parse_tentatively (parser);
18388       /* The `template' keyword must follow a nested-name-specifier.  */
18389       else if (!nested_name_specifier)
18390 	{
18391 	  cp_parser_error (parser, "%<template%> must follow a nested-"
18392 			   "name-specifier");
18393 	  return error_mark_node;
18394 	}
18395 
18396       /* Parse the template-id.  */
18397       token = cp_lexer_peek_token (parser->lexer);
18398       decl = cp_parser_template_id (parser, template_p,
18399 				    /*check_dependency_p=*/true,
18400 				    tag_type,
18401 				    is_declaration);
18402       /* If we didn't find a template-id, look for an ordinary
18403 	 identifier.  */
18404       if (!template_p && !cp_parser_parse_definitely (parser))
18405 	;
18406       /* We can get here when cp_parser_template_id, called by
18407 	 cp_parser_class_name with tag_type == none_type, succeeds
18408 	 and caches a BASELINK.  Then, when called again here,
18409 	 instead of failing and returning an error_mark_node
18410 	 returns it (see template/typename17.C in C++11).
18411 	 ??? Could we diagnose this earlier?  */
18412       else if (tag_type == typename_type && BASELINK_P (decl))
18413 	{
18414 	  cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18415 	  type = error_mark_node;
18416 	}
18417       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18418 	 in effect, then we must assume that, upon instantiation, the
18419 	 template will correspond to a class.  */
18420       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18421 	       && tag_type == typename_type)
18422 	type = make_typename_type (parser->scope, decl,
18423 				   typename_type,
18424 				   /*complain=*/tf_error);
18425       /* If the `typename' keyword is in effect and DECL is not a type
18426 	 decl, then type is non existent.   */
18427       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18428         ;
18429       else if (TREE_CODE (decl) == TYPE_DECL)
18430 	{
18431 	  type = check_elaborated_type_specifier (tag_type, decl,
18432 						  /*allow_template_p=*/true);
18433 
18434 	  /* If the next token is a semicolon, this must be a specialization,
18435 	     instantiation, or friend declaration.  Check the scope while we
18436 	     still know whether or not we had a nested-name-specifier.  */
18437 	  if (type != error_mark_node
18438 	      && !nested_name_specifier && !is_friend
18439 	      && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18440 	    check_unqualified_spec_or_inst (type, token->location);
18441 	}
18442       else if (decl == error_mark_node)
18443 	type = error_mark_node;
18444     }
18445 
18446   if (!type)
18447     {
18448       token = cp_lexer_peek_token (parser->lexer);
18449       identifier = cp_parser_identifier (parser);
18450 
18451       if (identifier == error_mark_node)
18452 	{
18453 	  parser->scope = NULL_TREE;
18454 	  return error_mark_node;
18455 	}
18456 
18457       /* For a `typename', we needn't call xref_tag.  */
18458       if (tag_type == typename_type
18459 	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18460 	return cp_parser_make_typename_type (parser, identifier,
18461 					     token->location);
18462 
18463       /* Template parameter lists apply only if we are not within a
18464 	 function parameter list.  */
18465       bool template_parm_lists_apply
18466 	  = parser->num_template_parameter_lists;
18467       if (template_parm_lists_apply)
18468 	for (cp_binding_level *s = current_binding_level;
18469 	     s && s->kind != sk_template_parms;
18470 	     s = s->level_chain)
18471 	  if (s->kind == sk_function_parms)
18472 	    template_parm_lists_apply = false;
18473 
18474       /* Look up a qualified name in the usual way.  */
18475       if (parser->scope)
18476 	{
18477 	  tree decl;
18478 	  tree ambiguous_decls;
18479 
18480 	  decl = cp_parser_lookup_name (parser, identifier,
18481 					tag_type,
18482 					/*is_template=*/false,
18483 					/*is_namespace=*/false,
18484 					/*check_dependency=*/true,
18485 					&ambiguous_decls,
18486 					token->location);
18487 
18488 	  /* If the lookup was ambiguous, an error will already have been
18489 	     issued.  */
18490 	  if (ambiguous_decls)
18491 	    return error_mark_node;
18492 
18493 	  /* If we are parsing friend declaration, DECL may be a
18494 	     TEMPLATE_DECL tree node here.  However, we need to check
18495 	     whether this TEMPLATE_DECL results in valid code.  Consider
18496 	     the following example:
18497 
18498 	       namespace N {
18499 		 template <class T> class C {};
18500 	       }
18501 	       class X {
18502 		 template <class T> friend class N::C; // #1, valid code
18503 	       };
18504 	       template <class T> class Y {
18505 		 friend class N::C;		       // #2, invalid code
18506 	       };
18507 
18508 	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18509 	     name lookup of `N::C'.  We see that friend declaration must
18510 	     be template for the code to be valid.  Note that
18511 	     processing_template_decl does not work here since it is
18512 	     always 1 for the above two cases.  */
18513 
18514 	  decl = (cp_parser_maybe_treat_template_as_class
18515 		  (decl, /*tag_name_p=*/is_friend
18516 			 && template_parm_lists_apply));
18517 
18518 	  if (TREE_CODE (decl) != TYPE_DECL)
18519 	    {
18520 	      cp_parser_diagnose_invalid_type_name (parser,
18521 						    identifier,
18522 						    token->location);
18523 	      return error_mark_node;
18524 	    }
18525 
18526 	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18527             {
18528               bool allow_template = (template_parm_lists_apply
18529 		                     || DECL_SELF_REFERENCE_P (decl));
18530               type = check_elaborated_type_specifier (tag_type, decl,
18531                                                       allow_template);
18532 
18533               if (type == error_mark_node)
18534                 return error_mark_node;
18535             }
18536 
18537           /* Forward declarations of nested types, such as
18538 
18539                class C1::C2;
18540                class C1::C2::C3;
18541 
18542              are invalid unless all components preceding the final '::'
18543              are complete.  If all enclosing types are complete, these
18544              declarations become merely pointless.
18545 
18546              Invalid forward declarations of nested types are errors
18547              caught elsewhere in parsing.  Those that are pointless arrive
18548              here.  */
18549 
18550           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18551               && !is_friend && !processing_explicit_instantiation)
18552             warning (0, "declaration %qD does not declare anything", decl);
18553 
18554 	  type = TREE_TYPE (decl);
18555 	}
18556       else
18557 	{
18558 	  /* An elaborated-type-specifier sometimes introduces a new type and
18559 	     sometimes names an existing type.  Normally, the rule is that it
18560 	     introduces a new type only if there is not an existing type of
18561 	     the same name already in scope.  For example, given:
18562 
18563 	       struct S {};
18564 	       void f() { struct S s; }
18565 
18566 	     the `struct S' in the body of `f' is the same `struct S' as in
18567 	     the global scope; the existing definition is used.  However, if
18568 	     there were no global declaration, this would introduce a new
18569 	     local class named `S'.
18570 
18571 	     An exception to this rule applies to the following code:
18572 
18573 	       namespace N { struct S; }
18574 
18575 	     Here, the elaborated-type-specifier names a new type
18576 	     unconditionally; even if there is already an `S' in the
18577 	     containing scope this declaration names a new type.
18578 	     This exception only applies if the elaborated-type-specifier
18579 	     forms the complete declaration:
18580 
18581 	       [class.name]
18582 
18583 	       A declaration consisting solely of `class-key identifier ;' is
18584 	       either a redeclaration of the name in the current scope or a
18585 	       forward declaration of the identifier as a class name.  It
18586 	       introduces the name into the current scope.
18587 
18588 	     We are in this situation precisely when the next token is a `;'.
18589 
18590 	     An exception to the exception is that a `friend' declaration does
18591 	     *not* name a new type; i.e., given:
18592 
18593 	       struct S { friend struct T; };
18594 
18595 	     `T' is not a new type in the scope of `S'.
18596 
18597 	     Also, `new struct S' or `sizeof (struct S)' never results in the
18598 	     definition of a new type; a new type can only be declared in a
18599 	     declaration context.  */
18600 
18601 	  tag_scope ts;
18602 	  bool template_p;
18603 
18604 	  if (is_friend)
18605 	    /* Friends have special name lookup rules.  */
18606 	    ts = ts_within_enclosing_non_class;
18607 	  else if (is_declaration
18608 		   && cp_lexer_next_token_is (parser->lexer,
18609 					      CPP_SEMICOLON))
18610 	    /* This is a `class-key identifier ;' */
18611 	    ts = ts_current;
18612 	  else
18613 	    ts = ts_global;
18614 
18615 	  template_p =
18616 	    (template_parm_lists_apply
18617 	     && (cp_parser_next_token_starts_class_definition_p (parser)
18618 		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18619 	  /* An unqualified name was used to reference this type, so
18620 	     there were no qualifying templates.  */
18621 	  if (template_parm_lists_apply
18622 	      && !cp_parser_check_template_parameters (parser,
18623 						       /*num_templates=*/0,
18624 						       /*template_id*/false,
18625 						       token->location,
18626 						       /*declarator=*/NULL))
18627 	    return error_mark_node;
18628 	  type = xref_tag (tag_type, identifier, ts, template_p);
18629 	}
18630     }
18631 
18632   if (type == error_mark_node)
18633     return error_mark_node;
18634 
18635   /* Allow attributes on forward declarations of classes.  */
18636   if (attributes)
18637     {
18638       if (TREE_CODE (type) == TYPENAME_TYPE)
18639 	warning (OPT_Wattributes,
18640 		 "attributes ignored on uninstantiated type");
18641       else if (tag_type != enum_type
18642 	       && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
18643 	       && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18644 	       && ! processing_explicit_instantiation)
18645 	warning (OPT_Wattributes,
18646 		 "attributes ignored on template instantiation");
18647       else if (is_declaration && cp_parser_declares_only_class_p (parser))
18648 	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18649       else
18650 	warning (OPT_Wattributes,
18651 		 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18652     }
18653 
18654   if (tag_type != enum_type)
18655     {
18656       /* Indicate whether this class was declared as a `class' or as a
18657 	 `struct'.  */
18658       if (CLASS_TYPE_P (type))
18659 	CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18660       cp_parser_check_class_key (tag_type, type);
18661     }
18662 
18663   /* A "<" cannot follow an elaborated type specifier.  If that
18664      happens, the user was probably trying to form a template-id.  */
18665   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18666 					   token->location);
18667 
18668   return type;
18669 }
18670 
18671 /* Parse an enum-specifier.
18672 
18673    enum-specifier:
18674      enum-head { enumerator-list [opt] }
18675      enum-head { enumerator-list , } [C++0x]
18676 
18677    enum-head:
18678      enum-key identifier [opt] enum-base [opt]
18679      enum-key nested-name-specifier identifier enum-base [opt]
18680 
18681    enum-key:
18682      enum
18683      enum class   [C++0x]
18684      enum struct  [C++0x]
18685 
18686    enum-base:   [C++0x]
18687      : type-specifier-seq
18688 
18689    opaque-enum-specifier:
18690      enum-key identifier enum-base [opt] ;
18691 
18692    GNU Extensions:
18693      enum-key attributes[opt] identifier [opt] enum-base [opt]
18694        { enumerator-list [opt] }attributes[opt]
18695      enum-key attributes[opt] identifier [opt] enum-base [opt]
18696        { enumerator-list, }attributes[opt] [C++0x]
18697 
18698    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18699    if the token stream isn't an enum-specifier after all.  */
18700 
18701 static tree
cp_parser_enum_specifier(cp_parser * parser)18702 cp_parser_enum_specifier (cp_parser* parser)
18703 {
18704   tree identifier;
18705   tree type = NULL_TREE;
18706   tree prev_scope;
18707   tree nested_name_specifier = NULL_TREE;
18708   tree attributes;
18709   bool scoped_enum_p = false;
18710   bool has_underlying_type = false;
18711   bool nested_being_defined = false;
18712   bool new_value_list = false;
18713   bool is_new_type = false;
18714   bool is_unnamed = false;
18715   tree underlying_type = NULL_TREE;
18716   cp_token *type_start_token = NULL;
18717   temp_override<bool> cleanup (parser->colon_corrects_to_scope_p, false);
18718 
18719   /* Parse tentatively so that we can back up if we don't find a
18720      enum-specifier.  */
18721   cp_parser_parse_tentatively (parser);
18722 
18723   /* Caller guarantees that the current token is 'enum', an identifier
18724      possibly follows, and the token after that is an opening brace.
18725      If we don't have an identifier, fabricate an anonymous name for
18726      the enumeration being defined.  */
18727   cp_lexer_consume_token (parser->lexer);
18728 
18729   /* Parse the "class" or "struct", which indicates a scoped
18730      enumeration type in C++0x.  */
18731   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18732       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18733     {
18734       if (cxx_dialect < cxx11)
18735         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18736 
18737       /* Consume the `struct' or `class' token.  */
18738       cp_lexer_consume_token (parser->lexer);
18739 
18740       scoped_enum_p = true;
18741     }
18742 
18743   attributes = cp_parser_attributes_opt (parser);
18744 
18745   /* Clear the qualification.  */
18746   parser->scope = NULL_TREE;
18747   parser->qualifying_scope = NULL_TREE;
18748   parser->object_scope = NULL_TREE;
18749 
18750   /* Figure out in what scope the declaration is being placed.  */
18751   prev_scope = current_scope ();
18752 
18753   type_start_token = cp_lexer_peek_token (parser->lexer);
18754 
18755   push_deferring_access_checks (dk_no_check);
18756   nested_name_specifier
18757     = cp_parser_nested_name_specifier_opt (parser,
18758 					   /*typename_keyword_p=*/true,
18759 					   /*check_dependency_p=*/false,
18760 					   /*type_p=*/false,
18761 					   /*is_declaration=*/false);
18762 
18763   if (nested_name_specifier)
18764     {
18765       tree name;
18766 
18767       identifier = cp_parser_identifier (parser);
18768       name = cp_parser_lookup_name (parser, identifier,
18769 				    enum_type,
18770 				    /*is_template=*/false,
18771 				    /*is_namespace=*/false,
18772 				    /*check_dependency=*/true,
18773 				    /*ambiguous_decls=*/NULL,
18774 				    input_location);
18775       if (name && name != error_mark_node)
18776 	{
18777 	  type = TREE_TYPE (name);
18778 	  if (TREE_CODE (type) == TYPENAME_TYPE)
18779 	    {
18780 	      /* Are template enums allowed in ISO? */
18781 	      if (template_parm_scope_p ())
18782 		pedwarn (type_start_token->location, OPT_Wpedantic,
18783 			 "%qD is an enumeration template", name);
18784 	      /* ignore a typename reference, for it will be solved by name
18785 	         in start_enum.  */
18786 	      type = NULL_TREE;
18787 	    }
18788 	}
18789       else if (nested_name_specifier == error_mark_node)
18790 	/* We already issued an error.  */;
18791       else
18792 	{
18793 	  error_at (type_start_token->location,
18794 		    "%qD does not name an enumeration in %qT",
18795 		    identifier, nested_name_specifier);
18796 	  nested_name_specifier = error_mark_node;
18797 	}
18798     }
18799   else
18800     {
18801       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18802 	identifier = cp_parser_identifier (parser);
18803       else
18804 	{
18805 	  identifier = make_anon_name ();
18806 	  is_unnamed = true;
18807 	  if (scoped_enum_p)
18808 	    error_at (type_start_token->location,
18809 		      "unnamed scoped enum is not allowed");
18810 	}
18811     }
18812   pop_deferring_access_checks ();
18813 
18814   /* Check for the `:' that denotes a specified underlying type in C++0x.
18815      Note that a ':' could also indicate a bitfield width, however.  */
18816   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18817     {
18818       cp_decl_specifier_seq type_specifiers;
18819 
18820       /* Consume the `:'.  */
18821       cp_lexer_consume_token (parser->lexer);
18822 
18823       /* Parse the type-specifier-seq.  */
18824       cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18825 				    /*is_declaration=*/false,
18826 				    /*is_trailing_return=*/false,
18827                                     &type_specifiers);
18828 
18829       /* At this point this is surely not elaborated type specifier.  */
18830       if (!cp_parser_parse_definitely (parser))
18831 	return NULL_TREE;
18832 
18833       if (cxx_dialect < cxx11)
18834         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18835 
18836       has_underlying_type = true;
18837 
18838       /* If that didn't work, stop.  */
18839       if (type_specifiers.type != error_mark_node)
18840         {
18841           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18842                                             /*initialized=*/0, NULL);
18843           if (underlying_type == error_mark_node
18844 	      || check_for_bare_parameter_packs (underlying_type))
18845             underlying_type = NULL_TREE;
18846         }
18847     }
18848 
18849   /* Look for the `{' but don't consume it yet.  */
18850   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18851     {
18852       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18853 	{
18854 	  if (has_underlying_type)
18855 	    cp_parser_commit_to_tentative_parse (parser);
18856 	  cp_parser_error (parser, "expected %<{%>");
18857 	  if (has_underlying_type)
18858 	    return error_mark_node;
18859 	}
18860       /* An opaque-enum-specifier must have a ';' here.  */
18861       if ((scoped_enum_p || underlying_type)
18862 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18863 	{
18864 	  if (has_underlying_type)
18865 	    cp_parser_commit_to_tentative_parse (parser);
18866 	  cp_parser_error (parser, "expected %<;%> or %<{%>");
18867 	  if (has_underlying_type)
18868 	    return error_mark_node;
18869 	}
18870     }
18871 
18872   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18873     return NULL_TREE;
18874 
18875   if (nested_name_specifier)
18876     {
18877       if (CLASS_TYPE_P (nested_name_specifier))
18878 	{
18879 	  nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18880 	  TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18881 	  push_scope (nested_name_specifier);
18882 	}
18883       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18884 	push_nested_namespace (nested_name_specifier);
18885     }
18886 
18887   /* Issue an error message if type-definitions are forbidden here.  */
18888   if (!cp_parser_check_type_definition (parser))
18889     type = error_mark_node;
18890   else
18891     /* Create the new type.  We do this before consuming the opening
18892        brace so the enum will be recorded as being on the line of its
18893        tag (or the 'enum' keyword, if there is no tag).  */
18894     type = start_enum (identifier, type, underlying_type,
18895 		       attributes, scoped_enum_p, &is_new_type);
18896 
18897   /* If the next token is not '{' it is an opaque-enum-specifier or an
18898      elaborated-type-specifier.  */
18899   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18900     {
18901       timevar_push (TV_PARSE_ENUM);
18902       if (nested_name_specifier
18903 	  && nested_name_specifier != error_mark_node)
18904 	{
18905 	  /* The following catches invalid code such as:
18906 	     enum class S<int>::E { A, B, C }; */
18907 	  if (!processing_specialization
18908 	      && CLASS_TYPE_P (nested_name_specifier)
18909 	      && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18910 	    error_at (type_start_token->location, "cannot add an enumerator "
18911 		      "list to a template instantiation");
18912 
18913 	  if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18914 	    {
18915 	      error_at (type_start_token->location,
18916 			"%<%T::%E%> has not been declared",
18917 			TYPE_CONTEXT (nested_name_specifier),
18918 			nested_name_specifier);
18919 	      type = error_mark_node;
18920 	    }
18921 	  else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18922 		   && !CLASS_TYPE_P (nested_name_specifier))
18923 	    {
18924 	      error_at (type_start_token->location, "nested name specifier "
18925 			"%qT for enum declaration does not name a class "
18926 			"or namespace", nested_name_specifier);
18927 	      type = error_mark_node;
18928 	    }
18929 	  /* If that scope does not contain the scope in which the
18930 	     class was originally declared, the program is invalid.  */
18931 	  else if (prev_scope && !is_ancestor (prev_scope,
18932 					       nested_name_specifier))
18933 	    {
18934 	      if (at_namespace_scope_p ())
18935 		error_at (type_start_token->location,
18936 			  "declaration of %qD in namespace %qD which does not "
18937 			  "enclose %qD",
18938 			  type, prev_scope, nested_name_specifier);
18939 	      else
18940 		error_at (type_start_token->location,
18941 			  "declaration of %qD in %qD which does not "
18942 			  "enclose %qD",
18943 			  type, prev_scope, nested_name_specifier);
18944 	      type = error_mark_node;
18945 	    }
18946 	  /* If that scope is the scope where the declaration is being placed
18947 	     the program is invalid.  */
18948 	  else if (CLASS_TYPE_P (nested_name_specifier)
18949 		   && CLASS_TYPE_P (prev_scope)
18950 		   && same_type_p (nested_name_specifier, prev_scope))
18951 	    {
18952 	      permerror (type_start_token->location,
18953 			 "extra qualification not allowed");
18954 	      nested_name_specifier = NULL_TREE;
18955 	    }
18956 	}
18957 
18958       if (scoped_enum_p)
18959 	begin_scope (sk_scoped_enum, type);
18960 
18961       /* Consume the opening brace.  */
18962       matching_braces braces;
18963       braces.consume_open (parser);
18964 
18965       if (type == error_mark_node)
18966 	; /* Nothing to add */
18967       else if (OPAQUE_ENUM_P (type)
18968 	       || (cxx_dialect > cxx98 && processing_specialization))
18969 	{
18970 	  new_value_list = true;
18971 	  SET_OPAQUE_ENUM_P (type, false);
18972 	  DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18973 	}
18974       else
18975 	{
18976 	  error_at (type_start_token->location,
18977 		    "multiple definition of %q#T", type);
18978 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18979 		  "previous definition here");
18980 	  type = error_mark_node;
18981 	}
18982 
18983       if (type == error_mark_node)
18984 	cp_parser_skip_to_end_of_block_or_statement (parser);
18985       /* If the next token is not '}', then there are some enumerators.  */
18986       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18987 	{
18988 	  if (is_unnamed && !scoped_enum_p)
18989 	    pedwarn (type_start_token->location, OPT_Wpedantic,
18990 		     "ISO C++ forbids empty unnamed enum");
18991 	}
18992       else
18993 	cp_parser_enumerator_list (parser, type);
18994 
18995       /* Consume the final '}'.  */
18996       braces.require_close (parser);
18997 
18998       if (scoped_enum_p)
18999 	finish_scope ();
19000       timevar_pop (TV_PARSE_ENUM);
19001     }
19002   else
19003     {
19004       /* If a ';' follows, then it is an opaque-enum-specifier
19005 	and additional restrictions apply.  */
19006       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19007 	{
19008 	  if (is_unnamed)
19009 	    error_at (type_start_token->location,
19010 		      "opaque-enum-specifier without name");
19011 	  else if (nested_name_specifier)
19012 	    error_at (type_start_token->location,
19013 		      "opaque-enum-specifier must use a simple identifier");
19014 	}
19015     }
19016 
19017   /* Look for trailing attributes to apply to this enumeration, and
19018      apply them if appropriate.  */
19019   if (cp_parser_allow_gnu_extensions_p (parser))
19020     {
19021       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
19022       cplus_decl_attributes (&type,
19023 			     trailing_attr,
19024 			     (int) ATTR_FLAG_TYPE_IN_PLACE);
19025     }
19026 
19027   /* Finish up the enumeration.  */
19028   if (type != error_mark_node)
19029     {
19030       if (new_value_list)
19031 	finish_enum_value_list (type);
19032       if (is_new_type)
19033 	finish_enum (type);
19034     }
19035 
19036   if (nested_name_specifier)
19037     {
19038       if (CLASS_TYPE_P (nested_name_specifier))
19039 	{
19040 	  TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
19041 	  pop_scope (nested_name_specifier);
19042 	}
19043       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
19044 	pop_nested_namespace (nested_name_specifier);
19045     }
19046   return type;
19047 }
19048 
19049 /* Parse an enumerator-list.  The enumerators all have the indicated
19050    TYPE.
19051 
19052    enumerator-list:
19053      enumerator-definition
19054      enumerator-list , enumerator-definition  */
19055 
19056 static void
cp_parser_enumerator_list(cp_parser * parser,tree type)19057 cp_parser_enumerator_list (cp_parser* parser, tree type)
19058 {
19059   while (true)
19060     {
19061       /* Parse an enumerator-definition.  */
19062       cp_parser_enumerator_definition (parser, type);
19063 
19064       /* If the next token is not a ',', we've reached the end of
19065 	 the list.  */
19066       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19067 	break;
19068       /* Otherwise, consume the `,' and keep going.  */
19069       cp_lexer_consume_token (parser->lexer);
19070       /* If the next token is a `}', there is a trailing comma.  */
19071       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19072 	{
19073 	  if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
19074 	    pedwarn (input_location, OPT_Wpedantic,
19075                      "comma at end of enumerator list");
19076 	  break;
19077 	}
19078     }
19079 }
19080 
19081 /* Parse an enumerator-definition.  The enumerator has the indicated
19082    TYPE.
19083 
19084    enumerator-definition:
19085      enumerator
19086      enumerator = constant-expression
19087 
19088    enumerator:
19089      identifier
19090 
19091    GNU Extensions:
19092 
19093    enumerator-definition:
19094      enumerator attributes [opt]
19095      enumerator attributes [opt] = constant-expression  */
19096 
19097 static void
cp_parser_enumerator_definition(cp_parser * parser,tree type)19098 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19099 {
19100   tree identifier;
19101   tree value;
19102   location_t loc;
19103 
19104   /* Save the input location because we are interested in the location
19105      of the identifier and not the location of the explicit value.  */
19106   loc = cp_lexer_peek_token (parser->lexer)->location;
19107 
19108   /* Look for the identifier.  */
19109   identifier = cp_parser_identifier (parser);
19110   if (identifier == error_mark_node)
19111     return;
19112 
19113   /* Parse any specified attributes.  */
19114   tree attrs = cp_parser_attributes_opt (parser);
19115 
19116   /* If the next token is an '=', then there is an explicit value.  */
19117   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19118     {
19119       /* Consume the `=' token.  */
19120       cp_lexer_consume_token (parser->lexer);
19121       /* Parse the value.  */
19122       value = cp_parser_constant_expression (parser);
19123     }
19124   else
19125     value = NULL_TREE;
19126 
19127   /* If we are processing a template, make sure the initializer of the
19128      enumerator doesn't contain any bare template parameter pack.  */
19129   if (check_for_bare_parameter_packs (value))
19130     value = error_mark_node;
19131 
19132   /* Create the enumerator.  */
19133   build_enumerator (identifier, value, type, attrs, loc);
19134 }
19135 
19136 /* Parse a namespace-name.
19137 
19138    namespace-name:
19139      original-namespace-name
19140      namespace-alias
19141 
19142    Returns the NAMESPACE_DECL for the namespace.  */
19143 
19144 static tree
cp_parser_namespace_name(cp_parser * parser)19145 cp_parser_namespace_name (cp_parser* parser)
19146 {
19147   tree identifier;
19148   tree namespace_decl;
19149 
19150   cp_token *token = cp_lexer_peek_token (parser->lexer);
19151 
19152   /* Get the name of the namespace.  */
19153   identifier = cp_parser_identifier (parser);
19154   if (identifier == error_mark_node)
19155     return error_mark_node;
19156 
19157   /* Look up the identifier in the currently active scope.  Look only
19158      for namespaces, due to:
19159 
19160        [basic.lookup.udir]
19161 
19162        When looking up a namespace-name in a using-directive or alias
19163        definition, only namespace names are considered.
19164 
19165      And:
19166 
19167        [basic.lookup.qual]
19168 
19169        During the lookup of a name preceding the :: scope resolution
19170        operator, object, function, and enumerator names are ignored.
19171 
19172      (Note that cp_parser_qualifying_entity only calls this
19173      function if the token after the name is the scope resolution
19174      operator.)  */
19175   namespace_decl = cp_parser_lookup_name (parser, identifier,
19176 					  none_type,
19177 					  /*is_template=*/false,
19178 					  /*is_namespace=*/true,
19179 					  /*check_dependency=*/true,
19180 					  /*ambiguous_decls=*/NULL,
19181 					  token->location);
19182   /* If it's not a namespace, issue an error.  */
19183   if (namespace_decl == error_mark_node
19184       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19185     {
19186       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19187 	{
19188 	  auto_diagnostic_group d;
19189 	  name_hint hint;
19190 	  if (namespace_decl == error_mark_node
19191 	      && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19192 	    hint = suggest_alternative_in_explicit_scope (token->location,
19193 							  identifier,
19194 							  parser->scope);
19195 	  if (const char *suggestion = hint.suggestion ())
19196 	    {
19197 	      gcc_rich_location richloc (token->location);
19198 	      richloc.add_fixit_replace (suggestion);
19199 	      error_at (&richloc,
19200 			"%qD is not a namespace-name; did you mean %qs?",
19201 			identifier, suggestion);
19202 	    }
19203 	  else
19204 	    error_at (token->location, "%qD is not a namespace-name",
19205 		      identifier);
19206 	}
19207       else
19208 	cp_parser_error (parser, "expected namespace-name");
19209       namespace_decl = error_mark_node;
19210     }
19211 
19212   return namespace_decl;
19213 }
19214 
19215 /* Parse a namespace-definition.
19216 
19217    namespace-definition:
19218      named-namespace-definition
19219      unnamed-namespace-definition
19220 
19221    named-namespace-definition:
19222      original-namespace-definition
19223      extension-namespace-definition
19224 
19225    original-namespace-definition:
19226      namespace identifier { namespace-body }
19227 
19228    extension-namespace-definition:
19229      namespace original-namespace-name { namespace-body }
19230 
19231    unnamed-namespace-definition:
19232      namespace { namespace-body } */
19233 
19234 static void
cp_parser_namespace_definition(cp_parser * parser)19235 cp_parser_namespace_definition (cp_parser* parser)
19236 {
19237   tree identifier;
19238   int nested_definition_count = 0;
19239 
19240   cp_ensure_no_omp_declare_simd (parser);
19241   cp_ensure_no_oacc_routine (parser);
19242 
19243   bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19244   const bool topmost_inline_p = is_inline;
19245 
19246   if (is_inline)
19247     {
19248       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19249       cp_lexer_consume_token (parser->lexer);
19250     }
19251 
19252   /* Look for the `namespace' keyword.  */
19253   cp_token* token
19254     = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19255 
19256   /* Parse any specified attributes before the identifier.  */
19257   tree attribs = cp_parser_attributes_opt (parser);
19258 
19259   for (;;)
19260     {
19261       identifier = NULL_TREE;
19262 
19263       bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19264 							     RID_INLINE);
19265       if (nested_inline_p && nested_definition_count != 0)
19266 	{
19267 	  if (cxx_dialect < cxx2a)
19268 	    pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19269 		     OPT_Wpedantic, "nested inline namespace definitions only "
19270 		     "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19271 	  cp_lexer_consume_token (parser->lexer);
19272 	}
19273 
19274       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19275 	{
19276 	  identifier = cp_parser_identifier (parser);
19277 
19278 	  if (cp_next_tokens_can_be_std_attribute_p (parser))
19279 	    pedwarn (input_location, OPT_Wpedantic,
19280 		     "standard attributes on namespaces must precede "
19281 		     "the namespace name");
19282 
19283 	  /* Parse any attributes specified after the identifier.  */
19284 	  attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19285 	}
19286 
19287       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19288 	{
19289 	  /* Don't forget that the innermost namespace might have been
19290 	     marked as inline.  Use |= because we cannot overwrite
19291 	     IS_INLINE in case the outermost namespace is inline, but
19292 	     there are no nested inlines.  */
19293 	  is_inline |= nested_inline_p;
19294 	  break;
19295 	}
19296 
19297       if (!nested_definition_count && cxx_dialect < cxx17)
19298         pedwarn (input_location, OPT_Wpedantic,
19299 		 "nested namespace definitions only available with "
19300 		 "%<-std=c++17%> or %<-std=gnu++17%>");
19301 
19302       /* Nested namespace names can create new namespaces (unlike
19303 	 other qualified-ids).  */
19304       if (int count = (identifier
19305 		       ? push_namespace (identifier, nested_inline_p)
19306 		       : 0))
19307 	nested_definition_count += count;
19308       else
19309 	cp_parser_error (parser, "nested namespace name required");
19310       cp_lexer_consume_token (parser->lexer);
19311     }
19312 
19313   if (nested_definition_count && !identifier)
19314     cp_parser_error (parser, "namespace name required");
19315 
19316   if (nested_definition_count && attribs)
19317     error_at (token->location,
19318 	      "a nested namespace definition cannot have attributes");
19319   if (nested_definition_count && topmost_inline_p)
19320     error_at (token->location,
19321 	      "a nested namespace definition cannot be inline");
19322 
19323   /* Start the namespace.  */
19324   nested_definition_count += push_namespace (identifier, is_inline);
19325 
19326   bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19327 
19328   warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19329 
19330   /* Look for the `{' to validate starting the namespace.  */
19331   matching_braces braces;
19332   if (braces.require_open (parser))
19333     {
19334       /* Parse the body of the namespace.  */
19335       cp_parser_namespace_body (parser);
19336 
19337       /* Look for the final `}'.  */
19338       braces.require_close (parser);
19339     }
19340 
19341   if (has_visibility)
19342     pop_visibility (1);
19343 
19344   /* Pop the nested namespace definitions.  */
19345   while (nested_definition_count--)
19346     pop_namespace ();
19347 }
19348 
19349 /* Parse a namespace-body.
19350 
19351    namespace-body:
19352      declaration-seq [opt]  */
19353 
19354 static void
cp_parser_namespace_body(cp_parser * parser)19355 cp_parser_namespace_body (cp_parser* parser)
19356 {
19357   cp_parser_declaration_seq_opt (parser);
19358 }
19359 
19360 /* Parse a namespace-alias-definition.
19361 
19362    namespace-alias-definition:
19363      namespace identifier = qualified-namespace-specifier ;  */
19364 
19365 static void
cp_parser_namespace_alias_definition(cp_parser * parser)19366 cp_parser_namespace_alias_definition (cp_parser* parser)
19367 {
19368   tree identifier;
19369   tree namespace_specifier;
19370 
19371   cp_token *token = cp_lexer_peek_token (parser->lexer);
19372 
19373   /* Look for the `namespace' keyword.  */
19374   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19375   /* Look for the identifier.  */
19376   identifier = cp_parser_identifier (parser);
19377   if (identifier == error_mark_node)
19378     return;
19379   /* Look for the `=' token.  */
19380   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19381       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19382     {
19383       error_at (token->location, "%<namespace%> definition is not allowed here");
19384       /* Skip the definition.  */
19385       cp_lexer_consume_token (parser->lexer);
19386       if (cp_parser_skip_to_closing_brace (parser))
19387 	cp_lexer_consume_token (parser->lexer);
19388       return;
19389     }
19390   cp_parser_require (parser, CPP_EQ, RT_EQ);
19391   /* Look for the qualified-namespace-specifier.  */
19392   namespace_specifier
19393     = cp_parser_qualified_namespace_specifier (parser);
19394   /* Look for the `;' token.  */
19395   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19396 
19397   /* Register the alias in the symbol table.  */
19398   do_namespace_alias (identifier, namespace_specifier);
19399 }
19400 
19401 /* Parse a qualified-namespace-specifier.
19402 
19403    qualified-namespace-specifier:
19404      :: [opt] nested-name-specifier [opt] namespace-name
19405 
19406    Returns a NAMESPACE_DECL corresponding to the specified
19407    namespace.  */
19408 
19409 static tree
cp_parser_qualified_namespace_specifier(cp_parser * parser)19410 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19411 {
19412   /* Look for the optional `::'.  */
19413   cp_parser_global_scope_opt (parser,
19414 			      /*current_scope_valid_p=*/false);
19415 
19416   /* Look for the optional nested-name-specifier.  */
19417   cp_parser_nested_name_specifier_opt (parser,
19418 				       /*typename_keyword_p=*/false,
19419 				       /*check_dependency_p=*/true,
19420 				       /*type_p=*/false,
19421 				       /*is_declaration=*/true);
19422 
19423   return cp_parser_namespace_name (parser);
19424 }
19425 
19426 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19427    access declaration.
19428 
19429    using-declaration:
19430      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19431      using :: unqualified-id ;
19432 
19433    access-declaration:
19434      qualified-id ;
19435 
19436    */
19437 
19438 static bool
cp_parser_using_declaration(cp_parser * parser,bool access_declaration_p)19439 cp_parser_using_declaration (cp_parser* parser,
19440 			     bool access_declaration_p)
19441 {
19442   cp_token *token;
19443   bool typename_p = false;
19444   bool global_scope_p;
19445   tree decl;
19446   tree identifier;
19447   tree qscope;
19448   int oldcount = errorcount;
19449   cp_token *diag_token = NULL;
19450 
19451   if (access_declaration_p)
19452     {
19453       diag_token = cp_lexer_peek_token (parser->lexer);
19454       cp_parser_parse_tentatively (parser);
19455     }
19456   else
19457     {
19458       /* Look for the `using' keyword.  */
19459       cp_parser_require_keyword (parser, RID_USING, RT_USING);
19460 
19461  again:
19462       /* Peek at the next token.  */
19463       token = cp_lexer_peek_token (parser->lexer);
19464       /* See if it's `typename'.  */
19465       if (token->keyword == RID_TYPENAME)
19466 	{
19467 	  /* Remember that we've seen it.  */
19468 	  typename_p = true;
19469 	  /* Consume the `typename' token.  */
19470 	  cp_lexer_consume_token (parser->lexer);
19471 	}
19472     }
19473 
19474   /* Look for the optional global scope qualification.  */
19475   global_scope_p
19476     = (cp_parser_global_scope_opt (parser,
19477 				   /*current_scope_valid_p=*/false)
19478        != NULL_TREE);
19479 
19480   /* If we saw `typename', or didn't see `::', then there must be a
19481      nested-name-specifier present.  */
19482   if (typename_p || !global_scope_p)
19483     {
19484       qscope = cp_parser_nested_name_specifier (parser, typename_p,
19485 						/*check_dependency_p=*/true,
19486 						/*type_p=*/false,
19487 						/*is_declaration=*/true);
19488       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19489 	{
19490 	  cp_parser_skip_to_end_of_block_or_statement (parser);
19491 	  return false;
19492 	}
19493     }
19494   /* Otherwise, we could be in either of the two productions.  In that
19495      case, treat the nested-name-specifier as optional.  */
19496   else
19497     qscope = cp_parser_nested_name_specifier_opt (parser,
19498 						  /*typename_keyword_p=*/false,
19499 						  /*check_dependency_p=*/true,
19500 						  /*type_p=*/false,
19501 						  /*is_declaration=*/true);
19502   if (!qscope)
19503     qscope = global_namespace;
19504   else if (UNSCOPED_ENUM_P (qscope)
19505 	   && !TYPE_FUNCTION_SCOPE_P (qscope))
19506     qscope = CP_TYPE_CONTEXT (qscope);
19507 
19508   if (access_declaration_p && cp_parser_error_occurred (parser))
19509     /* Something has already gone wrong; there's no need to parse
19510        further.  Since an error has occurred, the return value of
19511        cp_parser_parse_definitely will be false, as required.  */
19512     return cp_parser_parse_definitely (parser);
19513 
19514   token = cp_lexer_peek_token (parser->lexer);
19515   /* Parse the unqualified-id.  */
19516   identifier = cp_parser_unqualified_id (parser,
19517 					 /*template_keyword_p=*/false,
19518 					 /*check_dependency_p=*/true,
19519 					 /*declarator_p=*/true,
19520 					 /*optional_p=*/false);
19521 
19522   if (access_declaration_p)
19523     {
19524       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19525 	cp_parser_simulate_error (parser);
19526       if (!cp_parser_parse_definitely (parser))
19527 	return false;
19528     }
19529   else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19530     {
19531       cp_token *ell = cp_lexer_consume_token (parser->lexer);
19532       if (cxx_dialect < cxx17
19533 	  && !in_system_header_at (ell->location))
19534 	pedwarn (ell->location, 0,
19535 		 "pack expansion in using-declaration only available "
19536 		 "with %<-std=c++17%> or %<-std=gnu++17%>");
19537       qscope = make_pack_expansion (qscope);
19538     }
19539 
19540   /* The function we call to handle a using-declaration is different
19541      depending on what scope we are in.  */
19542   if (qscope == error_mark_node || identifier == error_mark_node)
19543     ;
19544   else if (!identifier_p (identifier)
19545 	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
19546     /* [namespace.udecl]
19547 
19548        A using declaration shall not name a template-id.  */
19549     error_at (token->location,
19550 	      "a template-id may not appear in a using-declaration");
19551   else
19552     {
19553       if (at_class_scope_p ())
19554 	{
19555 	  /* Create the USING_DECL.  */
19556 	  decl = do_class_using_decl (qscope, identifier);
19557 
19558 	  if (decl && typename_p)
19559 	    USING_DECL_TYPENAME_P (decl) = 1;
19560 
19561 	  if (check_for_bare_parameter_packs (decl))
19562 	    {
19563 	      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19564 	      return false;
19565 	    }
19566 	  else
19567 	    /* Add it to the list of members in this class.  */
19568 	    finish_member_declaration (decl);
19569 	}
19570       else
19571 	{
19572 	  decl = cp_parser_lookup_name_simple (parser,
19573 					       identifier,
19574 					       token->location);
19575 	  if (decl == error_mark_node)
19576 	    cp_parser_name_lookup_error (parser, identifier,
19577 					 decl, NLE_NULL,
19578 					 token->location);
19579 	  else if (check_for_bare_parameter_packs (decl))
19580 	    {
19581 	      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19582 	      return false;
19583 	    }
19584 	  else if (!at_namespace_scope_p ())
19585 	    finish_local_using_decl (decl, qscope, identifier);
19586 	  else
19587 	    finish_namespace_using_decl (decl, qscope, identifier);
19588 	}
19589     }
19590 
19591   if (!access_declaration_p
19592       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19593     {
19594       cp_token *comma = cp_lexer_consume_token (parser->lexer);
19595       if (cxx_dialect < cxx17)
19596 	pedwarn (comma->location, 0,
19597 		 "comma-separated list in using-declaration only available "
19598 		 "with %<-std=c++17%> or %<-std=gnu++17%>");
19599       goto again;
19600     }
19601 
19602   /* Look for the final `;'.  */
19603   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19604 
19605   if (access_declaration_p && errorcount == oldcount)
19606     warning_at (diag_token->location, OPT_Wdeprecated,
19607 		"access declarations are deprecated "
19608 		"in favour of using-declarations; "
19609 		"suggestion: add the %<using%> keyword");
19610 
19611   return true;
19612 }
19613 
19614 /* Parse an alias-declaration.
19615 
19616    alias-declaration:
19617      using identifier attribute-specifier-seq [opt] = type-id  */
19618 
19619 static tree
cp_parser_alias_declaration(cp_parser * parser)19620 cp_parser_alias_declaration (cp_parser* parser)
19621 {
19622   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19623   location_t id_location, type_location;
19624   cp_declarator *declarator;
19625   cp_decl_specifier_seq decl_specs;
19626   bool member_p;
19627   const char *saved_message = NULL;
19628 
19629   /* Look for the `using' keyword.  */
19630   cp_token *using_token
19631     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19632   if (using_token == NULL)
19633     return error_mark_node;
19634 
19635   id_location = cp_lexer_peek_token (parser->lexer)->location;
19636   id = cp_parser_identifier (parser);
19637   if (id == error_mark_node)
19638     return error_mark_node;
19639 
19640   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19641   attributes = cp_parser_attributes_opt (parser);
19642   if (attributes == error_mark_node)
19643     return error_mark_node;
19644 
19645   cp_parser_require (parser, CPP_EQ, RT_EQ);
19646 
19647   if (cp_parser_error_occurred (parser))
19648     return error_mark_node;
19649 
19650   cp_parser_commit_to_tentative_parse (parser);
19651 
19652   /* Now we are going to parse the type-id of the declaration.  */
19653 
19654   /*
19655     [dcl.type]/3 says:
19656 
19657 	"A type-specifier-seq shall not define a class or enumeration
19658 	 unless it appears in the type-id of an alias-declaration (7.1.3) that
19659 	 is not the declaration of a template-declaration."
19660 
19661     In other words, if we currently are in an alias template, the
19662     type-id should not define a type.
19663 
19664     So let's set parser->type_definition_forbidden_message in that
19665     case; cp_parser_check_type_definition (called by
19666     cp_parser_class_specifier) will then emit an error if a type is
19667     defined in the type-id.  */
19668   if (parser->num_template_parameter_lists)
19669     {
19670       saved_message = parser->type_definition_forbidden_message;
19671       parser->type_definition_forbidden_message =
19672 	G_("types may not be defined in alias template declarations");
19673     }
19674 
19675   type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19676 			    &type_location);
19677 
19678   /* Restore the error message if need be.  */
19679   if (parser->num_template_parameter_lists)
19680     parser->type_definition_forbidden_message = saved_message;
19681 
19682   if (type == error_mark_node
19683       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19684     {
19685       cp_parser_skip_to_end_of_block_or_statement (parser);
19686       return error_mark_node;
19687     }
19688 
19689   /* A typedef-name can also be introduced by an alias-declaration. The
19690      identifier following the using keyword becomes a typedef-name. It has
19691      the same semantics as if it were introduced by the typedef
19692      specifier. In particular, it does not define a new type and it shall
19693      not appear in the type-id.  */
19694 
19695   clear_decl_specs (&decl_specs);
19696   decl_specs.type = type;
19697   if (attributes != NULL_TREE)
19698     {
19699       decl_specs.attributes = attributes;
19700       set_and_check_decl_spec_loc (&decl_specs,
19701 				   ds_attribute,
19702 				   attrs_token);
19703     }
19704   set_and_check_decl_spec_loc (&decl_specs,
19705 			       ds_typedef,
19706 			       using_token);
19707   set_and_check_decl_spec_loc (&decl_specs,
19708 			       ds_alias,
19709 			       using_token);
19710   decl_specs.locations[ds_type_spec] = type_location;
19711 
19712   if (parser->num_template_parameter_lists
19713       && !cp_parser_check_template_parameters (parser,
19714 					       /*num_templates=*/0,
19715 					       /*template_id*/false,
19716 					       id_location,
19717 					       /*declarator=*/NULL))
19718     return error_mark_node;
19719 
19720   declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19721 
19722   member_p = at_class_scope_p ();
19723   if (member_p)
19724     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19725 		      NULL_TREE, attributes);
19726   else
19727     decl = start_decl (declarator, &decl_specs, 0,
19728 		       attributes, NULL_TREE, &pushed_scope);
19729   if (decl == error_mark_node)
19730     return decl;
19731 
19732   // Attach constraints to the alias declaration.
19733   if (flag_concepts && current_template_parms)
19734     {
19735       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19736       tree constr = build_constraints (reqs, NULL_TREE);
19737       set_constraints (decl, constr);
19738     }
19739 
19740   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19741 
19742   if (pushed_scope)
19743     pop_scope (pushed_scope);
19744 
19745   /* If decl is a template, return its TEMPLATE_DECL so that it gets
19746      added into the symbol table; otherwise, return the TYPE_DECL.  */
19747   if (DECL_LANG_SPECIFIC (decl)
19748       && DECL_TEMPLATE_INFO (decl)
19749       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19750     {
19751       decl = DECL_TI_TEMPLATE (decl);
19752       if (member_p)
19753 	check_member_template (decl);
19754     }
19755 
19756   return decl;
19757 }
19758 
19759 /* Parse a using-directive.
19760 
19761    using-directive:
19762      using namespace :: [opt] nested-name-specifier [opt]
19763        namespace-name ;  */
19764 
19765 static void
cp_parser_using_directive(cp_parser * parser)19766 cp_parser_using_directive (cp_parser* parser)
19767 {
19768   tree namespace_decl;
19769   tree attribs;
19770 
19771   /* Look for the `using' keyword.  */
19772   cp_parser_require_keyword (parser, RID_USING, RT_USING);
19773   /* And the `namespace' keyword.  */
19774   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19775   /* Look for the optional `::' operator.  */
19776   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19777   /* And the optional nested-name-specifier.  */
19778   cp_parser_nested_name_specifier_opt (parser,
19779 				       /*typename_keyword_p=*/false,
19780 				       /*check_dependency_p=*/true,
19781 				       /*type_p=*/false,
19782 				       /*is_declaration=*/true);
19783   /* Get the namespace being used.  */
19784   namespace_decl = cp_parser_namespace_name (parser);
19785   /* And any specified attributes.  */
19786   attribs = cp_parser_attributes_opt (parser);
19787 
19788   /* Update the symbol table.  */
19789   if (namespace_bindings_p ())
19790     finish_namespace_using_directive (namespace_decl, attribs);
19791   else
19792     finish_local_using_directive (namespace_decl, attribs);
19793 
19794   /* Look for the final `;'.  */
19795   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19796 }
19797 
19798 /* Parse an asm-definition.
19799 
19800   asm-qualifier:
19801     volatile
19802     inline
19803     goto
19804 
19805   asm-qualifier-list:
19806     asm-qualifier
19807     asm-qualifier-list asm-qualifier
19808 
19809    asm-definition:
19810      asm ( string-literal ) ;
19811 
19812    GNU Extension:
19813 
19814    asm-definition:
19815      asm asm-qualifier-list [opt] ( string-literal ) ;
19816      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19817      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19818 				    : asm-operand-list [opt] ) ;
19819      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19820 				    : asm-operand-list [opt]
19821 			  : asm-clobber-list [opt] ) ;
19822      asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19823 				    : asm-clobber-list [opt]
19824 				    : asm-goto-list ) ;
19825 
19826   The form with asm-goto-list is valid if and only if the asm-qualifier-list
19827   contains goto, and is the only allowed form in that case.  No duplicates are
19828   allowed in an asm-qualifier-list.  */
19829 
19830 static void
cp_parser_asm_definition(cp_parser * parser)19831 cp_parser_asm_definition (cp_parser* parser)
19832 {
19833   tree string;
19834   tree outputs = NULL_TREE;
19835   tree inputs = NULL_TREE;
19836   tree clobbers = NULL_TREE;
19837   tree labels = NULL_TREE;
19838   tree asm_stmt;
19839   bool extended_p = false;
19840   bool invalid_inputs_p = false;
19841   bool invalid_outputs_p = false;
19842   required_token missing = RT_NONE;
19843 
19844   /* Look for the `asm' keyword.  */
19845   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19846 
19847   if (parser->in_function_body
19848       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19849     {
19850       error ("%<asm%> in %<constexpr%> function");
19851       cp_function_chain->invalid_constexpr = true;
19852     }
19853 
19854   /* Handle the asm-qualifier-list.  */
19855   location_t volatile_loc = UNKNOWN_LOCATION;
19856   location_t inline_loc = UNKNOWN_LOCATION;
19857   location_t goto_loc = UNKNOWN_LOCATION;
19858   location_t first_loc = UNKNOWN_LOCATION;
19859 
19860   if (cp_parser_allow_gnu_extensions_p (parser))
19861     for (;;)
19862       {
19863 	cp_token *token = cp_lexer_peek_token (parser->lexer);
19864 	location_t loc = token->location;
19865 	switch (cp_lexer_peek_token (parser->lexer)->keyword)
19866 	  {
19867 	  case RID_VOLATILE:
19868 	    if (volatile_loc)
19869 	      {
19870 		error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19871 		inform (volatile_loc, "first seen here");
19872 	      }
19873 	    else
19874 	      {
19875 		if (!parser->in_function_body)
19876 		  warning_at (loc, 0, "asm qualifier %qT ignored outside of "
19877 				      "function body", token->u.value);
19878 		volatile_loc = loc;
19879 	      }
19880 	    cp_lexer_consume_token (parser->lexer);
19881 	    continue;
19882 
19883 	  case RID_INLINE:
19884 	    if (inline_loc)
19885 	      {
19886 		error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19887 		inform (inline_loc, "first seen here");
19888 	      }
19889 	    else
19890 	      inline_loc = loc;
19891 	    if (!first_loc)
19892 	      first_loc = loc;
19893 	    cp_lexer_consume_token (parser->lexer);
19894 	    continue;
19895 
19896 	  case RID_GOTO:
19897 	    if (goto_loc)
19898 	      {
19899 		error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19900 		inform (goto_loc, "first seen here");
19901 	      }
19902 	    else
19903 	      goto_loc = loc;
19904 	    if (!first_loc)
19905 	      first_loc = loc;
19906 	    cp_lexer_consume_token (parser->lexer);
19907 	    continue;
19908 
19909 	  case RID_CONST:
19910 	  case RID_RESTRICT:
19911 	    error_at (loc, "%qT is not an asm qualifier", token->u.value);
19912 	    cp_lexer_consume_token (parser->lexer);
19913 	    continue;
19914 
19915 	  default:
19916 	    break;
19917 	  }
19918 	break;
19919       }
19920 
19921   bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19922   bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19923   bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19924 
19925   if (!parser->in_function_body && (inline_p || goto_p))
19926     {
19927       error_at (first_loc, "asm qualifier outside of function body");
19928       inline_p = goto_p = false;
19929     }
19930 
19931   /* Look for the opening `('.  */
19932   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19933     return;
19934   /* Look for the string.  */
19935   string = cp_parser_string_literal (parser, false, false);
19936   if (string == error_mark_node)
19937     {
19938       cp_parser_skip_to_closing_parenthesis (parser, true, false,
19939 					     /*consume_paren=*/true);
19940       return;
19941     }
19942 
19943   /* If we're allowing GNU extensions, check for the extended assembly
19944      syntax.  Unfortunately, the `:' tokens need not be separated by
19945      a space in C, and so, for compatibility, we tolerate that here
19946      too.  Doing that means that we have to treat the `::' operator as
19947      two `:' tokens.  */
19948   if (cp_parser_allow_gnu_extensions_p (parser)
19949       && parser->in_function_body
19950       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19951 	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19952     {
19953       bool inputs_p = false;
19954       bool clobbers_p = false;
19955       bool labels_p = false;
19956 
19957       /* The extended syntax was used.  */
19958       extended_p = true;
19959 
19960       /* Look for outputs.  */
19961       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19962 	{
19963 	  /* Consume the `:'.  */
19964 	  cp_lexer_consume_token (parser->lexer);
19965 	  /* Parse the output-operands.  */
19966 	  if (cp_lexer_next_token_is_not (parser->lexer,
19967 					  CPP_COLON)
19968 	      && cp_lexer_next_token_is_not (parser->lexer,
19969 					     CPP_SCOPE)
19970 	      && cp_lexer_next_token_is_not (parser->lexer,
19971 					     CPP_CLOSE_PAREN)
19972 	      && !goto_p)
19973             {
19974               outputs = cp_parser_asm_operand_list (parser);
19975               if (outputs == error_mark_node)
19976                 invalid_outputs_p = true;
19977             }
19978 	}
19979       /* If the next token is `::', there are no outputs, and the
19980 	 next token is the beginning of the inputs.  */
19981       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19982 	/* The inputs are coming next.  */
19983 	inputs_p = true;
19984 
19985       /* Look for inputs.  */
19986       if (inputs_p
19987 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19988 	{
19989 	  /* Consume the `:' or `::'.  */
19990 	  cp_lexer_consume_token (parser->lexer);
19991 	  /* Parse the output-operands.  */
19992 	  if (cp_lexer_next_token_is_not (parser->lexer,
19993 					  CPP_COLON)
19994 	      && cp_lexer_next_token_is_not (parser->lexer,
19995 					     CPP_SCOPE)
19996 	      && cp_lexer_next_token_is_not (parser->lexer,
19997 					     CPP_CLOSE_PAREN))
19998             {
19999               inputs = cp_parser_asm_operand_list (parser);
20000               if (inputs == error_mark_node)
20001                 invalid_inputs_p = true;
20002             }
20003 	}
20004       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20005 	/* The clobbers are coming next.  */
20006 	clobbers_p = true;
20007 
20008       /* Look for clobbers.  */
20009       if (clobbers_p
20010 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20011 	{
20012 	  clobbers_p = true;
20013 	  /* Consume the `:' or `::'.  */
20014 	  cp_lexer_consume_token (parser->lexer);
20015 	  /* Parse the clobbers.  */
20016 	  if (cp_lexer_next_token_is_not (parser->lexer,
20017 					  CPP_COLON)
20018 	      && cp_lexer_next_token_is_not (parser->lexer,
20019 					     CPP_CLOSE_PAREN))
20020 	    clobbers = cp_parser_asm_clobber_list (parser);
20021 	}
20022       else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
20023 	/* The labels are coming next.  */
20024 	labels_p = true;
20025 
20026       /* Look for labels.  */
20027       if (labels_p
20028 	  || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
20029 	{
20030 	  labels_p = true;
20031 	  /* Consume the `:' or `::'.  */
20032 	  cp_lexer_consume_token (parser->lexer);
20033 	  /* Parse the labels.  */
20034 	  labels = cp_parser_asm_label_list (parser);
20035 	}
20036 
20037       if (goto_p && !labels_p)
20038 	missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
20039     }
20040   else if (goto_p)
20041     missing = RT_COLON_SCOPE;
20042 
20043   /* Look for the closing `)'.  */
20044   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
20045 			  missing ? missing : RT_CLOSE_PAREN))
20046     cp_parser_skip_to_closing_parenthesis (parser, true, false,
20047 					   /*consume_paren=*/true);
20048   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20049 
20050   if (!invalid_inputs_p && !invalid_outputs_p)
20051     {
20052       /* Create the ASM_EXPR.  */
20053       if (parser->in_function_body)
20054 	{
20055 	  asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
20056 				      inputs, clobbers, labels, inline_p);
20057 	  /* If the extended syntax was not used, mark the ASM_EXPR.  */
20058 	  if (!extended_p)
20059 	    {
20060 	      tree temp = asm_stmt;
20061 	      if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
20062 		temp = TREE_OPERAND (temp, 0);
20063 
20064 	      ASM_INPUT_P (temp) = 1;
20065 	    }
20066 	}
20067       else
20068 	symtab->finalize_toplevel_asm (string);
20069     }
20070 }
20071 
20072 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20073    type that comes from the decl-specifier-seq.  */
20074 
20075 static tree
strip_declarator_types(tree type,cp_declarator * declarator)20076 strip_declarator_types (tree type, cp_declarator *declarator)
20077 {
20078   for (cp_declarator *d = declarator; d;)
20079     switch (d->kind)
20080       {
20081       case cdk_id:
20082       case cdk_decomp:
20083       case cdk_error:
20084 	d = NULL;
20085 	break;
20086 
20087       default:
20088 	if (TYPE_PTRMEMFUNC_P (type))
20089 	  type = TYPE_PTRMEMFUNC_FN_TYPE (type);
20090 	type = TREE_TYPE (type);
20091 	d = d->declarator;
20092 	break;
20093       }
20094 
20095   return type;
20096 }
20097 
20098 /* Declarators [gram.dcl.decl] */
20099 
20100 /* Parse an init-declarator.
20101 
20102    init-declarator:
20103      declarator initializer [opt]
20104 
20105    GNU Extension:
20106 
20107    init-declarator:
20108      declarator asm-specification [opt] attributes [opt] initializer [opt]
20109 
20110    function-definition:
20111      decl-specifier-seq [opt] declarator ctor-initializer [opt]
20112        function-body
20113      decl-specifier-seq [opt] declarator function-try-block
20114 
20115    GNU Extension:
20116 
20117    function-definition:
20118      __extension__ function-definition
20119 
20120    TM Extension:
20121 
20122    function-definition:
20123      decl-specifier-seq [opt] declarator function-transaction-block
20124 
20125    The parser flags FLAGS is used to control type-specifier parsing.
20126 
20127    The DECL_SPECIFIERS apply to this declarator.  Returns a
20128    representation of the entity declared.  If MEMBER_P is TRUE, then
20129    this declarator appears in a class scope.  The new DECL created by
20130    this declarator is returned.
20131 
20132    The CHECKS are access checks that should be performed once we know
20133    what entity is being declared (and, therefore, what classes have
20134    befriended it).
20135 
20136    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20137    for a function-definition here as well.  If the declarator is a
20138    declarator for a function-definition, *FUNCTION_DEFINITION_P will
20139    be TRUE upon return.  By that point, the function-definition will
20140    have been completely parsed.
20141 
20142    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20143    is FALSE.
20144 
20145    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20146    parsed declaration if it is an uninitialized single declarator not followed
20147    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20148    if present, will not be consumed.  If returned, this declarator will be
20149    created with SD_INITIALIZED but will not call cp_finish_decl.
20150 
20151    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20152    and there is an initializer, the pointed location_t is set to the
20153    location of the '=' or `(', or '{' in C++11 token introducing the
20154    initializer.  */
20155 
20156 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)20157 cp_parser_init_declarator (cp_parser* parser,
20158 			   cp_parser_flags flags,
20159 			   cp_decl_specifier_seq *decl_specifiers,
20160 			   vec<deferred_access_check, va_gc> *checks,
20161 			   bool function_definition_allowed_p,
20162 			   bool member_p,
20163 			   int declares_class_or_enum,
20164 			   bool* function_definition_p,
20165 			   tree* maybe_range_for_decl,
20166 			   location_t* init_loc,
20167 			   tree* auto_result)
20168 {
20169   cp_token *token = NULL, *asm_spec_start_token = NULL,
20170            *attributes_start_token = NULL;
20171   cp_declarator *declarator;
20172   tree prefix_attributes;
20173   tree attributes = NULL;
20174   tree asm_specification;
20175   tree initializer;
20176   tree decl = NULL_TREE;
20177   tree scope;
20178   int is_initialized;
20179   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
20180      initialized with "= ..", CPP_OPEN_PAREN if initialized with
20181      "(...)".  */
20182   enum cpp_ttype initialization_kind;
20183   bool is_direct_init = false;
20184   bool is_non_constant_init;
20185   int ctor_dtor_or_conv_p;
20186   bool friend_p = cp_parser_friend_p (decl_specifiers);
20187   tree pushed_scope = NULL_TREE;
20188   bool range_for_decl_p = false;
20189   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20190   location_t tmp_init_loc = UNKNOWN_LOCATION;
20191 
20192   /* Gather the attributes that were provided with the
20193      decl-specifiers.  */
20194   prefix_attributes = decl_specifiers->attributes;
20195 
20196   /* Assume that this is not the declarator for a function
20197      definition.  */
20198   if (function_definition_p)
20199     *function_definition_p = false;
20200 
20201   /* Default arguments are only permitted for function parameters.  */
20202   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20203     parser->default_arg_ok_p = false;
20204 
20205   /* Defer access checks while parsing the declarator; we cannot know
20206      what names are accessible until we know what is being
20207      declared.  */
20208   resume_deferring_access_checks ();
20209 
20210   token = cp_lexer_peek_token (parser->lexer);
20211 
20212   /* Parse the declarator.  */
20213   declarator
20214     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20215 			    flags, &ctor_dtor_or_conv_p,
20216 			    /*parenthesized_p=*/NULL,
20217 			    member_p, friend_p, /*static_p=*/false);
20218   /* Gather up the deferred checks.  */
20219   stop_deferring_access_checks ();
20220 
20221   parser->default_arg_ok_p = saved_default_arg_ok_p;
20222 
20223   /* If the DECLARATOR was erroneous, there's no need to go
20224      further.  */
20225   if (declarator == cp_error_declarator)
20226     return error_mark_node;
20227 
20228   /* Check that the number of template-parameter-lists is OK.  */
20229   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20230 						       token->location))
20231     return error_mark_node;
20232 
20233   if (declares_class_or_enum & 2)
20234     cp_parser_check_for_definition_in_return_type (declarator,
20235 						   decl_specifiers->type,
20236 						   decl_specifiers->locations[ds_type_spec]);
20237 
20238   /* Figure out what scope the entity declared by the DECLARATOR is
20239      located in.  `grokdeclarator' sometimes changes the scope, so
20240      we compute it now.  */
20241   scope = get_scope_of_declarator (declarator);
20242 
20243   /* Perform any lookups in the declared type which were thought to be
20244      dependent, but are not in the scope of the declarator.  */
20245   decl_specifiers->type
20246     = maybe_update_decl_type (decl_specifiers->type, scope);
20247 
20248   /* If we're allowing GNU extensions, look for an
20249      asm-specification.  */
20250   if (cp_parser_allow_gnu_extensions_p (parser))
20251     {
20252       /* Look for an asm-specification.  */
20253       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20254       asm_specification = cp_parser_asm_specification_opt (parser);
20255     }
20256   else
20257     asm_specification = NULL_TREE;
20258 
20259   /* Look for attributes.  */
20260   attributes_start_token = cp_lexer_peek_token (parser->lexer);
20261   attributes = cp_parser_attributes_opt (parser);
20262 
20263   /* Peek at the next token.  */
20264   token = cp_lexer_peek_token (parser->lexer);
20265 
20266   bool bogus_implicit_tmpl = false;
20267 
20268   if (function_declarator_p (declarator))
20269     {
20270       /* Handle C++17 deduction guides.  */
20271       if (!decl_specifiers->type
20272 	  && !decl_specifiers->any_type_specifiers_p
20273 	  && ctor_dtor_or_conv_p <= 0
20274 	  && cxx_dialect >= cxx17)
20275 	{
20276 	  cp_declarator *id = get_id_declarator (declarator);
20277 	  tree name = id->u.id.unqualified_name;
20278 	  parser->scope = id->u.id.qualifying_scope;
20279 	  tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20280 	  if (tmpl
20281 	      && (DECL_CLASS_TEMPLATE_P (tmpl)
20282 		  || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20283 	    {
20284 	      id->u.id.unqualified_name = dguide_name (tmpl);
20285 	      id->u.id.sfk = sfk_deduction_guide;
20286 	      ctor_dtor_or_conv_p = 1;
20287 	    }
20288 	}
20289 
20290       /* Check to see if the token indicates the start of a
20291 	 function-definition.  */
20292       if (cp_parser_token_starts_function_definition_p (token))
20293 	{
20294 	  if (!function_definition_allowed_p)
20295 	    {
20296 	      /* If a function-definition should not appear here, issue an
20297 		 error message.  */
20298 	      cp_parser_error (parser,
20299 			       "a function-definition is not allowed here");
20300 	      return error_mark_node;
20301 	    }
20302 
20303 	  location_t func_brace_location
20304 	    = cp_lexer_peek_token (parser->lexer)->location;
20305 
20306 	  /* Neither attributes nor an asm-specification are allowed
20307 	     on a function-definition.  */
20308 	  if (asm_specification)
20309 	    error_at (asm_spec_start_token->location,
20310 		      "an asm-specification is not allowed "
20311 		      "on a function-definition");
20312 	  if (attributes)
20313 	    error_at (attributes_start_token->location,
20314 		      "attributes are not allowed "
20315 		      "on a function-definition");
20316 	  /* This is a function-definition.  */
20317 	  *function_definition_p = true;
20318 
20319 	  /* Parse the function definition.  */
20320 	  if (member_p)
20321 	    decl = cp_parser_save_member_function_body (parser,
20322 							decl_specifiers,
20323 							declarator,
20324 							prefix_attributes);
20325 	  else
20326 	    decl =
20327 	      (cp_parser_function_definition_from_specifiers_and_declarator
20328 	       (parser, decl_specifiers, prefix_attributes, declarator));
20329 
20330 	  if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20331 	    {
20332 	      /* This is where the prologue starts...  */
20333 	      DECL_STRUCT_FUNCTION (decl)->function_start_locus
20334 		= func_brace_location;
20335 	    }
20336 
20337 	  return decl;
20338 	}
20339     }
20340   else if (parser->fully_implicit_function_template_p)
20341     {
20342       /* A non-template declaration involving a function parameter list
20343 	 containing an implicit template parameter will be made into a
20344 	 template.  If the resulting declaration is not going to be an
20345 	 actual function then finish the template scope here to prevent it.
20346 	 An error message will be issued once we have a decl to talk about.
20347 
20348          FIXME probably we should do type deduction rather than create an
20349          implicit template, but the standard currently doesn't allow it. */
20350       bogus_implicit_tmpl = true;
20351       finish_fully_implicit_template (parser, NULL_TREE);
20352     }
20353 
20354   /* [dcl.dcl]
20355 
20356      Only in function declarations for constructors, destructors, type
20357      conversions, and deduction guides can the decl-specifier-seq be omitted.
20358 
20359      We explicitly postpone this check past the point where we handle
20360      function-definitions because we tolerate function-definitions
20361      that are missing their return types in some modes.  */
20362   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20363     {
20364       cp_parser_error (parser,
20365 		       "expected constructor, destructor, or type conversion");
20366       return error_mark_node;
20367     }
20368 
20369   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
20370   if (token->type == CPP_EQ
20371       || token->type == CPP_OPEN_PAREN
20372       || token->type == CPP_OPEN_BRACE)
20373     {
20374       is_initialized = SD_INITIALIZED;
20375       initialization_kind = token->type;
20376       if (maybe_range_for_decl)
20377 	*maybe_range_for_decl = error_mark_node;
20378       tmp_init_loc = token->location;
20379       if (init_loc && *init_loc == UNKNOWN_LOCATION)
20380 	*init_loc = tmp_init_loc;
20381 
20382       if (token->type == CPP_EQ
20383 	  && function_declarator_p (declarator))
20384 	{
20385 	  cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20386 	  if (t2->keyword == RID_DEFAULT)
20387 	    is_initialized = SD_DEFAULTED;
20388 	  else if (t2->keyword == RID_DELETE)
20389 	    is_initialized = SD_DELETED;
20390 	}
20391     }
20392   else
20393     {
20394       /* If the init-declarator isn't initialized and isn't followed by a
20395 	 `,' or `;', it's not a valid init-declarator.  */
20396       if (token->type != CPP_COMMA
20397 	  && token->type != CPP_SEMICOLON)
20398 	{
20399 	  if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20400 	    range_for_decl_p = true;
20401 	  else
20402 	    {
20403 	      if (!maybe_range_for_decl)
20404 		cp_parser_error (parser, "expected initializer");
20405 	      return error_mark_node;
20406 	    }
20407 	}
20408       is_initialized = SD_UNINITIALIZED;
20409       initialization_kind = CPP_EOF;
20410     }
20411 
20412   /* Because start_decl has side-effects, we should only call it if we
20413      know we're going ahead.  By this point, we know that we cannot
20414      possibly be looking at any other construct.  */
20415   cp_parser_commit_to_tentative_parse (parser);
20416 
20417   /* Enter the newly declared entry in the symbol table.  If we're
20418      processing a declaration in a class-specifier, we wait until
20419      after processing the initializer.  */
20420   if (!member_p)
20421     {
20422       if (parser->in_unbraced_linkage_specification_p)
20423 	decl_specifiers->storage_class = sc_extern;
20424       decl = start_decl (declarator, decl_specifiers,
20425 			 range_for_decl_p? SD_INITIALIZED : is_initialized,
20426 			 attributes, prefix_attributes, &pushed_scope);
20427       cp_finalize_omp_declare_simd (parser, decl);
20428       cp_finalize_oacc_routine (parser, decl, false);
20429       /* Adjust location of decl if declarator->id_loc is more appropriate:
20430 	 set, and decl wasn't merged with another decl, in which case its
20431 	 location would be different from input_location, and more accurate.  */
20432       if (DECL_P (decl)
20433 	  && declarator->id_loc != UNKNOWN_LOCATION
20434 	  && DECL_SOURCE_LOCATION (decl) == input_location)
20435 	DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20436     }
20437   else if (scope)
20438     /* Enter the SCOPE.  That way unqualified names appearing in the
20439        initializer will be looked up in SCOPE.  */
20440     pushed_scope = push_scope (scope);
20441 
20442   /* Perform deferred access control checks, now that we know in which
20443      SCOPE the declared entity resides.  */
20444   if (!member_p && decl)
20445     {
20446       tree saved_current_function_decl = NULL_TREE;
20447 
20448       /* If the entity being declared is a function, pretend that we
20449 	 are in its scope.  If it is a `friend', it may have access to
20450 	 things that would not otherwise be accessible.  */
20451       if (TREE_CODE (decl) == FUNCTION_DECL)
20452 	{
20453 	  saved_current_function_decl = current_function_decl;
20454 	  current_function_decl = decl;
20455 	}
20456 
20457       /* Perform access checks for template parameters.  */
20458       cp_parser_perform_template_parameter_access_checks (checks);
20459 
20460       /* Perform the access control checks for the declarator and the
20461 	 decl-specifiers.  */
20462       perform_deferred_access_checks (tf_warning_or_error);
20463 
20464       /* Restore the saved value.  */
20465       if (TREE_CODE (decl) == FUNCTION_DECL)
20466 	current_function_decl = saved_current_function_decl;
20467     }
20468 
20469   /* Parse the initializer.  */
20470   initializer = NULL_TREE;
20471   is_direct_init = false;
20472   is_non_constant_init = true;
20473   if (is_initialized)
20474     {
20475       if (function_declarator_p (declarator))
20476 	{
20477 	   if (initialization_kind == CPP_EQ)
20478 	     initializer = cp_parser_pure_specifier (parser);
20479 	   else
20480 	     {
20481 	       /* If the declaration was erroneous, we don't really
20482 		  know what the user intended, so just silently
20483 		  consume the initializer.  */
20484 	       if (decl != error_mark_node)
20485 		 error_at (tmp_init_loc, "initializer provided for function");
20486 	       cp_parser_skip_to_closing_parenthesis (parser,
20487 						      /*recovering=*/true,
20488 						      /*or_comma=*/false,
20489 						      /*consume_paren=*/true);
20490 	     }
20491 	}
20492       else
20493 	{
20494 	  /* We want to record the extra mangling scope for in-class
20495 	     initializers of class members and initializers of static data
20496 	     member templates.  The former involves deferring
20497 	     parsing of the initializer until end of class as with default
20498 	     arguments.  So right here we only handle the latter.  */
20499 	  if (!member_p && processing_template_decl && decl != error_mark_node)
20500 	    start_lambda_scope (decl);
20501 	  initializer = cp_parser_initializer (parser,
20502 					       &is_direct_init,
20503 					       &is_non_constant_init);
20504 	  if (!member_p && processing_template_decl && decl != error_mark_node)
20505 	    finish_lambda_scope ();
20506 	  if (initializer == error_mark_node)
20507 	    cp_parser_skip_to_end_of_statement (parser);
20508 	}
20509     }
20510 
20511   /* The old parser allows attributes to appear after a parenthesized
20512      initializer.  Mark Mitchell proposed removing this functionality
20513      on the GCC mailing lists on 2002-08-13.  This parser accepts the
20514      attributes -- but ignores them.  Made a permerror in GCC 8.  */
20515   if (cp_parser_allow_gnu_extensions_p (parser)
20516       && initialization_kind == CPP_OPEN_PAREN
20517       && cp_parser_attributes_opt (parser)
20518       && permerror (input_location,
20519 		    "attributes after parenthesized initializer ignored"))
20520     {
20521       static bool hint;
20522       if (flag_permissive && !hint)
20523 	{
20524 	  hint = true;
20525 	  inform (input_location,
20526 		  "this flexibility is deprecated and will be removed");
20527 	}
20528     }
20529 
20530   /* And now complain about a non-function implicit template.  */
20531   if (bogus_implicit_tmpl && decl != error_mark_node)
20532     error_at (DECL_SOURCE_LOCATION (decl),
20533 	      "non-function %qD declared as implicit template", decl);
20534 
20535   /* For an in-class declaration, use `grokfield' to create the
20536      declaration.  */
20537   if (member_p)
20538     {
20539       if (pushed_scope)
20540 	{
20541 	  pop_scope (pushed_scope);
20542 	  pushed_scope = NULL_TREE;
20543 	}
20544       decl = grokfield (declarator, decl_specifiers,
20545 			initializer, !is_non_constant_init,
20546 			/*asmspec=*/NULL_TREE,
20547 			attr_chainon (attributes, prefix_attributes));
20548       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20549 	cp_parser_save_default_args (parser, decl);
20550       cp_finalize_omp_declare_simd (parser, decl);
20551       cp_finalize_oacc_routine (parser, decl, false);
20552     }
20553 
20554   /* Finish processing the declaration.  But, skip member
20555      declarations.  */
20556   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20557     {
20558       cp_finish_decl (decl,
20559 		      initializer, !is_non_constant_init,
20560 		      asm_specification,
20561 		      /* If the initializer is in parentheses, then this is
20562 			 a direct-initialization, which means that an
20563 			 `explicit' constructor is OK.  Otherwise, an
20564 			 `explicit' constructor cannot be used.  */
20565 		      ((is_direct_init || !is_initialized)
20566 		       ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20567     }
20568   else if ((cxx_dialect != cxx98) && friend_p
20569 	   && decl && TREE_CODE (decl) == FUNCTION_DECL)
20570     /* Core issue #226 (C++0x only): A default template-argument
20571        shall not be specified in a friend class template
20572        declaration. */
20573     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20574                              /*is_partial=*/false, /*is_friend_decl=*/1);
20575 
20576   if (!friend_p && pushed_scope)
20577     pop_scope (pushed_scope);
20578 
20579   if (function_declarator_p (declarator)
20580       && parser->fully_implicit_function_template_p)
20581     {
20582       if (member_p)
20583 	decl = finish_fully_implicit_template (parser, decl);
20584       else
20585 	finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20586     }
20587 
20588   if (auto_result && is_initialized && decl_specifiers->type
20589       && type_uses_auto (decl_specifiers->type))
20590     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20591 
20592   return decl;
20593 }
20594 
20595 /* Parse a declarator.
20596 
20597    declarator:
20598      direct-declarator
20599      ptr-operator declarator
20600 
20601    abstract-declarator:
20602      ptr-operator abstract-declarator [opt]
20603      direct-abstract-declarator
20604 
20605    GNU Extensions:
20606 
20607    declarator:
20608      attributes [opt] direct-declarator
20609      attributes [opt] ptr-operator declarator
20610 
20611    abstract-declarator:
20612      attributes [opt] ptr-operator abstract-declarator [opt]
20613      attributes [opt] direct-abstract-declarator
20614 
20615    The parser flags FLAGS is used to control type-specifier parsing.
20616 
20617    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20618    detect constructors, destructors, deduction guides, or conversion operators.
20619    It is set to -1 if the declarator is a name, and +1 if it is a
20620    function. Otherwise it is set to zero. Usually you just want to
20621    test for >0, but internally the negative value is used.
20622 
20623    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20624    a decl-specifier-seq unless it declares a constructor, destructor,
20625    or conversion.  It might seem that we could check this condition in
20626    semantic analysis, rather than parsing, but that makes it difficult
20627    to handle something like `f()'.  We want to notice that there are
20628    no decl-specifiers, and therefore realize that this is an
20629    expression, not a declaration.)
20630 
20631    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20632    the declarator is a direct-declarator of the form "(...)".
20633 
20634    MEMBER_P is true iff this declarator is a member-declarator.
20635 
20636    FRIEND_P is true iff this declarator is a friend.
20637 
20638    STATIC_P is true iff the keyword static was seen.  */
20639 
20640 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)20641 cp_parser_declarator (cp_parser* parser,
20642 		      cp_parser_declarator_kind dcl_kind,
20643 		      cp_parser_flags flags,
20644 		      int* ctor_dtor_or_conv_p,
20645 		      bool* parenthesized_p,
20646 		      bool member_p, bool friend_p, bool static_p)
20647 {
20648   cp_declarator *declarator;
20649   enum tree_code code;
20650   cp_cv_quals cv_quals;
20651   tree class_type;
20652   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20653 
20654   /* Assume this is not a constructor, destructor, or type-conversion
20655      operator.  */
20656   if (ctor_dtor_or_conv_p)
20657     *ctor_dtor_or_conv_p = 0;
20658 
20659   if (cp_parser_allow_gnu_extensions_p (parser))
20660     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20661 
20662   /* Check for the ptr-operator production.  */
20663   cp_parser_parse_tentatively (parser);
20664   /* Parse the ptr-operator.  */
20665   code = cp_parser_ptr_operator (parser,
20666 				 &class_type,
20667 				 &cv_quals,
20668 				 &std_attributes);
20669 
20670   /* If that worked, then we have a ptr-operator.  */
20671   if (cp_parser_parse_definitely (parser))
20672     {
20673       /* If a ptr-operator was found, then this declarator was not
20674 	 parenthesized.  */
20675       if (parenthesized_p)
20676 	*parenthesized_p = true;
20677       /* The dependent declarator is optional if we are parsing an
20678 	 abstract-declarator.  */
20679       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20680 	cp_parser_parse_tentatively (parser);
20681 
20682       /* Parse the dependent declarator.  */
20683       declarator = cp_parser_declarator (parser, dcl_kind,
20684 					 CP_PARSER_FLAGS_NONE,
20685 					 /*ctor_dtor_or_conv_p=*/NULL,
20686 					 /*parenthesized_p=*/NULL,
20687 					 /*member_p=*/false,
20688 					 friend_p, /*static_p=*/false);
20689 
20690       /* If we are parsing an abstract-declarator, we must handle the
20691 	 case where the dependent declarator is absent.  */
20692       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20693 	  && !cp_parser_parse_definitely (parser))
20694 	declarator = NULL;
20695 
20696       declarator = cp_parser_make_indirect_declarator
20697 	(code, class_type, cv_quals, declarator, std_attributes);
20698     }
20699   /* Everything else is a direct-declarator.  */
20700   else
20701     {
20702       if (parenthesized_p)
20703 	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20704 						   CPP_OPEN_PAREN);
20705       declarator = cp_parser_direct_declarator (parser, dcl_kind,
20706 						flags, ctor_dtor_or_conv_p,
20707 						member_p, friend_p, static_p);
20708     }
20709 
20710   if (gnu_attributes && declarator && declarator != cp_error_declarator)
20711     declarator->attributes = gnu_attributes;
20712   return declarator;
20713 }
20714 
20715 /* Parse a direct-declarator or direct-abstract-declarator.
20716 
20717    direct-declarator:
20718      declarator-id
20719      direct-declarator ( parameter-declaration-clause )
20720        cv-qualifier-seq [opt]
20721        ref-qualifier [opt]
20722        exception-specification [opt]
20723      direct-declarator [ constant-expression [opt] ]
20724      ( declarator )
20725 
20726    direct-abstract-declarator:
20727      direct-abstract-declarator [opt]
20728        ( parameter-declaration-clause )
20729        cv-qualifier-seq [opt]
20730        ref-qualifier [opt]
20731        exception-specification [opt]
20732      direct-abstract-declarator [opt] [ constant-expression [opt] ]
20733      ( abstract-declarator )
20734 
20735    Returns a representation of the declarator.  DCL_KIND is
20736    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20737    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
20738    we are parsing a direct-declarator.  It is
20739    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20740    of ambiguity we prefer an abstract declarator, as per
20741    [dcl.ambig.res].
20742    The parser flags FLAGS is used to control type-specifier parsing.
20743    CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20744    as for cp_parser_declarator.  */
20745 
20746 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)20747 cp_parser_direct_declarator (cp_parser* parser,
20748 			     cp_parser_declarator_kind dcl_kind,
20749 			     cp_parser_flags flags,
20750 			     int* ctor_dtor_or_conv_p,
20751 			     bool member_p, bool friend_p, bool static_p)
20752 {
20753   cp_token *token;
20754   cp_declarator *declarator = NULL;
20755   tree scope = NULL_TREE;
20756   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20757   bool saved_in_declarator_p = parser->in_declarator_p;
20758   bool first = true;
20759   tree pushed_scope = NULL_TREE;
20760   cp_token *open_paren = NULL, *close_paren = NULL;
20761 
20762   while (true)
20763     {
20764       /* Peek at the next token.  */
20765       token = cp_lexer_peek_token (parser->lexer);
20766       if (token->type == CPP_OPEN_PAREN)
20767 	{
20768 	  /* This is either a parameter-declaration-clause, or a
20769 	     parenthesized declarator. When we know we are parsing a
20770 	     named declarator, it must be a parenthesized declarator
20771 	     if FIRST is true. For instance, `(int)' is a
20772 	     parameter-declaration-clause, with an omitted
20773 	     direct-abstract-declarator. But `((*))', is a
20774 	     parenthesized abstract declarator. Finally, when T is a
20775 	     template parameter `(T)' is a
20776 	     parameter-declaration-clause, and not a parenthesized
20777 	     named declarator.
20778 
20779 	     We first try and parse a parameter-declaration-clause,
20780 	     and then try a nested declarator (if FIRST is true).
20781 
20782 	     It is not an error for it not to be a
20783 	     parameter-declaration-clause, even when FIRST is
20784 	     false. Consider,
20785 
20786 	       int i (int);
20787 	       int i (3);
20788 
20789 	     The first is the declaration of a function while the
20790 	     second is the definition of a variable, including its
20791 	     initializer.
20792 
20793 	     Having seen only the parenthesis, we cannot know which of
20794 	     these two alternatives should be selected.  Even more
20795 	     complex are examples like:
20796 
20797 	       int i (int (a));
20798 	       int i (int (3));
20799 
20800 	     The former is a function-declaration; the latter is a
20801 	     variable initialization.
20802 
20803 	     Thus again, we try a parameter-declaration-clause, and if
20804 	     that fails, we back out and return.  */
20805 
20806 	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20807 	    {
20808 	      tree params;
20809 	      bool is_declarator = false;
20810 
20811 	      open_paren = NULL;
20812 
20813 	      /* In a member-declarator, the only valid interpretation
20814 		 of a parenthesis is the start of a
20815 		 parameter-declaration-clause.  (It is invalid to
20816 		 initialize a static data member with a parenthesized
20817 		 initializer; only the "=" form of initialization is
20818 		 permitted.)  */
20819 	      if (!member_p)
20820 		cp_parser_parse_tentatively (parser);
20821 
20822 	      /* Consume the `('.  */
20823 	      matching_parens parens;
20824 	      parens.consume_open (parser);
20825 	      if (first)
20826 		{
20827 		  /* If this is going to be an abstract declarator, we're
20828 		     in a declarator and we can't have default args.  */
20829 		  parser->default_arg_ok_p = false;
20830 		  parser->in_declarator_p = true;
20831 		}
20832 
20833 	      begin_scope (sk_function_parms, NULL_TREE);
20834 
20835 	      /* Parse the parameter-declaration-clause.  */
20836 	      params
20837 		= cp_parser_parameter_declaration_clause (parser, flags);
20838 
20839 	      /* Consume the `)'.  */
20840 	      parens.require_close (parser);
20841 
20842 	      /* If all went well, parse the cv-qualifier-seq,
20843 		 ref-qualifier and the exception-specification.  */
20844 	      if (member_p || cp_parser_parse_definitely (parser))
20845 		{
20846 		  cp_cv_quals cv_quals;
20847 		  cp_virt_specifiers virt_specifiers;
20848 		  cp_ref_qualifier ref_qual;
20849 		  tree exception_specification;
20850 		  tree late_return;
20851 		  tree attrs;
20852 		  bool memfn = (member_p || (pushed_scope
20853 					     && CLASS_TYPE_P (pushed_scope)));
20854 		  unsigned char local_variables_forbidden_p
20855 		    = parser->local_variables_forbidden_p;
20856 		  /* 'this' is not allowed in static member functions.  */
20857 		  if (static_p || friend_p)
20858 		    parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20859 
20860 		  is_declarator = true;
20861 
20862 		  if (ctor_dtor_or_conv_p)
20863 		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20864 		  first = false;
20865 
20866 		  /* Parse the cv-qualifier-seq.  */
20867 		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20868 		  /* Parse the ref-qualifier. */
20869 		  ref_qual = cp_parser_ref_qualifier_opt (parser);
20870 		  /* Parse the tx-qualifier.  */
20871 		  tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20872 		  /* And the exception-specification.  */
20873 		  exception_specification
20874 		    = cp_parser_exception_specification_opt (parser);
20875 
20876 		  attrs = cp_parser_std_attribute_spec_seq (parser);
20877 
20878 		  /* In here, we handle cases where attribute is used after
20879 		     the function declaration.  For example:
20880 		     void func (int x) __attribute__((vector(..)));  */
20881 		  tree gnu_attrs = NULL_TREE;
20882 		  tree requires_clause = NULL_TREE;
20883 		  late_return = (cp_parser_late_return_type_opt
20884 				 (parser, declarator, requires_clause,
20885 				  memfn ? cv_quals : -1));
20886 
20887 		  /* Parse the virt-specifier-seq.  */
20888 		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20889 
20890 		  /* Create the function-declarator.  */
20891 		  declarator = make_call_declarator (declarator,
20892 						     params,
20893 						     cv_quals,
20894 						     virt_specifiers,
20895 						     ref_qual,
20896 						     tx_qual,
20897 						     exception_specification,
20898 						     late_return,
20899 						     requires_clause);
20900 		  declarator->std_attributes = attrs;
20901 		  declarator->attributes = gnu_attrs;
20902 		  /* Any subsequent parameter lists are to do with
20903 		     return type, so are not those of the declared
20904 		     function.  */
20905 		  parser->default_arg_ok_p = false;
20906 
20907 		  /* Restore the state of local_variables_forbidden_p.  */
20908 		  parser->local_variables_forbidden_p
20909 		    = local_variables_forbidden_p;
20910 		}
20911 
20912 	      /* Remove the function parms from scope.  */
20913 	      pop_bindings_and_leave_scope ();
20914 
20915 	      if (is_declarator)
20916 		/* Repeat the main loop.  */
20917 		continue;
20918 	    }
20919 
20920 	  /* If this is the first, we can try a parenthesized
20921 	     declarator.  */
20922 	  if (first)
20923 	    {
20924 	      bool saved_in_type_id_in_expr_p;
20925 
20926 	      parser->default_arg_ok_p = saved_default_arg_ok_p;
20927 	      parser->in_declarator_p = saved_in_declarator_p;
20928 
20929 	      open_paren = token;
20930 	      /* Consume the `('.  */
20931 	      matching_parens parens;
20932 	      parens.consume_open (parser);
20933 	      /* Parse the nested declarator.  */
20934 	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20935 	      parser->in_type_id_in_expr_p = true;
20936 	      declarator
20937 		= cp_parser_declarator (parser, dcl_kind, flags,
20938 					ctor_dtor_or_conv_p,
20939 					/*parenthesized_p=*/NULL,
20940 					member_p, friend_p,
20941 					/*static_p=*/false);
20942 	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20943 	      first = false;
20944 	      /* Expect a `)'.  */
20945 	      close_paren = cp_lexer_peek_token (parser->lexer);
20946 	      if (!parens.require_close (parser))
20947 		declarator = cp_error_declarator;
20948 	      if (declarator == cp_error_declarator)
20949 		break;
20950 
20951 	      goto handle_declarator;
20952 	    }
20953 	  /* Otherwise, we must be done.  */
20954 	  else
20955 	    break;
20956 	}
20957       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20958 	       && token->type == CPP_OPEN_SQUARE
20959 	       && !cp_next_tokens_can_be_attribute_p (parser))
20960 	{
20961 	  /* Parse an array-declarator.  */
20962 	  tree bounds, attrs;
20963 
20964 	  if (ctor_dtor_or_conv_p)
20965 	    *ctor_dtor_or_conv_p = 0;
20966 
20967 	  open_paren = NULL;
20968 	  first = false;
20969 	  parser->default_arg_ok_p = false;
20970 	  parser->in_declarator_p = true;
20971 	  /* Consume the `['.  */
20972 	  cp_lexer_consume_token (parser->lexer);
20973 	  /* Peek at the next token.  */
20974 	  token = cp_lexer_peek_token (parser->lexer);
20975 	  /* If the next token is `]', then there is no
20976 	     constant-expression.  */
20977 	  if (token->type != CPP_CLOSE_SQUARE)
20978 	    {
20979 	      bool non_constant_p;
20980 	      bounds
20981 		= cp_parser_constant_expression (parser,
20982 						 /*allow_non_constant=*/true,
20983 						 &non_constant_p);
20984 	      if (!non_constant_p)
20985 		/* OK */;
20986 	      else if (error_operand_p (bounds))
20987 		/* Already gave an error.  */;
20988 	      else if (!parser->in_function_body
20989 		       || current_binding_level->kind == sk_function_parms)
20990 		{
20991 		  /* Normally, the array bound must be an integral constant
20992 		     expression.  However, as an extension, we allow VLAs
20993 		     in function scopes as long as they aren't part of a
20994 		     parameter declaration.  */
20995 		  cp_parser_error (parser,
20996 				   "array bound is not an integer constant");
20997 		  bounds = error_mark_node;
20998 		}
20999 	      else if (processing_template_decl
21000 		       && !type_dependent_expression_p (bounds))
21001 		{
21002 		  /* Remember this wasn't a constant-expression.  */
21003 		  bounds = build_nop (TREE_TYPE (bounds), bounds);
21004 		  TREE_SIDE_EFFECTS (bounds) = 1;
21005 		}
21006 	    }
21007 	  else
21008 	    bounds = NULL_TREE;
21009 	  /* Look for the closing `]'.  */
21010 	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21011 	    {
21012 	      declarator = cp_error_declarator;
21013 	      break;
21014 	    }
21015 
21016 	  attrs = cp_parser_std_attribute_spec_seq (parser);
21017 	  declarator = make_array_declarator (declarator, bounds);
21018 	  declarator->std_attributes = attrs;
21019 	}
21020       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
21021 	{
21022 	  {
21023 	    tree qualifying_scope;
21024 	    tree unqualified_name;
21025 	    tree attrs;
21026 	    special_function_kind sfk;
21027 	    bool abstract_ok;
21028 	    bool pack_expansion_p = false;
21029 	    cp_token *declarator_id_start_token;
21030 
21031 	    /* Parse a declarator-id */
21032 	    abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
21033 	    if (abstract_ok)
21034 	      {
21035 		cp_parser_parse_tentatively (parser);
21036 
21037 		/* If we see an ellipsis, we should be looking at a
21038 		   parameter pack. */
21039 		if (token->type == CPP_ELLIPSIS)
21040 		  {
21041 		    /* Consume the `...' */
21042 		    cp_lexer_consume_token (parser->lexer);
21043 
21044 		    pack_expansion_p = true;
21045 		  }
21046 	      }
21047 
21048 	    declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
21049 	    unqualified_name
21050 	      = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
21051 	    qualifying_scope = parser->scope;
21052 	    if (abstract_ok)
21053 	      {
21054 		bool okay = false;
21055 
21056 		if (!unqualified_name && pack_expansion_p)
21057 		  {
21058 		    /* Check whether an error occurred. */
21059 		    okay = !cp_parser_error_occurred (parser);
21060 
21061 		    /* We already consumed the ellipsis to mark a
21062 		       parameter pack, but we have no way to report it,
21063 		       so abort the tentative parse. We will be exiting
21064 		       immediately anyway. */
21065 		    cp_parser_abort_tentative_parse (parser);
21066 		  }
21067 		else
21068 		  okay = cp_parser_parse_definitely (parser);
21069 
21070 		if (!okay)
21071 		  unqualified_name = error_mark_node;
21072 		else if (unqualified_name
21073 			 && (qualifying_scope
21074 			     || (!identifier_p (unqualified_name))))
21075 		  {
21076 		    cp_parser_error (parser, "expected unqualified-id");
21077 		    unqualified_name = error_mark_node;
21078 		  }
21079 	      }
21080 
21081 	    if (!unqualified_name)
21082 	      return NULL;
21083 	    if (unqualified_name == error_mark_node)
21084 	      {
21085 		declarator = cp_error_declarator;
21086 		pack_expansion_p = false;
21087 		declarator->parameter_pack_p = false;
21088 		break;
21089 	      }
21090 
21091 	    attrs = cp_parser_std_attribute_spec_seq (parser);
21092 
21093 	    if (qualifying_scope && at_namespace_scope_p ()
21094 		&& TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
21095 	      {
21096 		/* In the declaration of a member of a template class
21097 		   outside of the class itself, the SCOPE will sometimes
21098 		   be a TYPENAME_TYPE.  For example, given:
21099 
21100 		   template <typename T>
21101 		   int S<T>::R::i = 3;
21102 
21103 		   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
21104 		   this context, we must resolve S<T>::R to an ordinary
21105 		   type, rather than a typename type.
21106 
21107 		   The reason we normally avoid resolving TYPENAME_TYPEs
21108 		   is that a specialization of `S' might render
21109 		   `S<T>::R' not a type.  However, if `S' is
21110 		   specialized, then this `i' will not be used, so there
21111 		   is no harm in resolving the types here.  */
21112 		tree type;
21113 
21114 		/* Resolve the TYPENAME_TYPE.  */
21115 		type = resolve_typename_type (qualifying_scope,
21116 					      /*only_current_p=*/false);
21117 		/* If that failed, the declarator is invalid.  */
21118 		if (TREE_CODE (type) == TYPENAME_TYPE)
21119 		  {
21120 		    if (typedef_variant_p (type))
21121 		      error_at (declarator_id_start_token->location,
21122 				"cannot define member of dependent typedef "
21123 				"%qT", type);
21124 		    else
21125 		      error_at (declarator_id_start_token->location,
21126 				"%<%T::%E%> is not a type",
21127 				TYPE_CONTEXT (qualifying_scope),
21128 				TYPE_IDENTIFIER (qualifying_scope));
21129 		  }
21130 		qualifying_scope = type;
21131 	      }
21132 
21133 	    sfk = sfk_none;
21134 
21135 	    if (unqualified_name)
21136 	      {
21137 		tree class_type;
21138 
21139 		if (qualifying_scope
21140 		    && CLASS_TYPE_P (qualifying_scope))
21141 		  class_type = qualifying_scope;
21142 		else
21143 		  class_type = current_class_type;
21144 
21145 		if (TREE_CODE (unqualified_name) == TYPE_DECL)
21146 		  {
21147 		    tree name_type = TREE_TYPE (unqualified_name);
21148 
21149 		    if (!class_type || !same_type_p (name_type, class_type))
21150 		      {
21151 			/* We do not attempt to print the declarator
21152 			   here because we do not have enough
21153 			   information about its original syntactic
21154 			   form.  */
21155 			cp_parser_error (parser, "invalid declarator");
21156 			declarator = cp_error_declarator;
21157 			break;
21158 		      }
21159 		    else if (qualifying_scope
21160 			     && CLASSTYPE_USE_TEMPLATE (name_type))
21161 		      {
21162 			error_at (declarator_id_start_token->location,
21163 				  "invalid use of constructor as a template");
21164 			inform (declarator_id_start_token->location,
21165 				"use %<%T::%D%> instead of %<%T::%D%> to "
21166 				"name the constructor in a qualified name",
21167 				class_type,
21168 				DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21169 				class_type, name_type);
21170 			declarator = cp_error_declarator;
21171 			break;
21172 		      }
21173 		    unqualified_name = constructor_name (class_type);
21174 		  }
21175 
21176 		if (class_type)
21177 		  {
21178 		    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21179 		      sfk = sfk_destructor;
21180 		    else if (identifier_p (unqualified_name)
21181 			     && IDENTIFIER_CONV_OP_P (unqualified_name))
21182 		      sfk = sfk_conversion;
21183 		    else if (/* There's no way to declare a constructor
21184 				for an unnamed type, even if the type
21185 				got a name for linkage purposes.  */
21186 			     !TYPE_WAS_UNNAMED (class_type)
21187 			     /* Handle correctly (c++/19200):
21188 
21189 				struct S {
21190 				  struct T{};
21191 				  friend void S(T);
21192 				};
21193 
21194 				and also:
21195 
21196 				namespace N {
21197 				  void S();
21198 				}
21199 
21200 				struct S {
21201 				  friend void N::S();
21202 				};  */
21203 			     && (!friend_p || class_type == qualifying_scope)
21204 			     && constructor_name_p (unqualified_name,
21205 						    class_type))
21206 		      sfk = sfk_constructor;
21207 		    else if (is_overloaded_fn (unqualified_name)
21208 			     && DECL_CONSTRUCTOR_P (get_first_fn
21209 						    (unqualified_name)))
21210 		      sfk = sfk_constructor;
21211 
21212 		    if (ctor_dtor_or_conv_p && sfk != sfk_none)
21213 		      *ctor_dtor_or_conv_p = -1;
21214 		  }
21215 	      }
21216 	    declarator = make_id_declarator (qualifying_scope,
21217 					     unqualified_name,
21218 					     sfk, token->location);
21219 	    declarator->std_attributes = attrs;
21220 	    declarator->parameter_pack_p = pack_expansion_p;
21221 
21222 	    if (pack_expansion_p)
21223 	      maybe_warn_variadic_templates ();
21224 
21225 	    /* We're looking for this case in [temp.res]:
21226 	       A qualified-id is assumed to name a type if [...]
21227 	       - it is a decl-specifier of the decl-specifier-seq of a
21228 		 parameter-declaration in a declarator of a function or
21229 		 function template declaration, ... */
21230 	    if (cxx_dialect >= cxx2a
21231 		&& (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21232 		&& declarator->kind == cdk_id
21233 		&& !at_class_scope_p ()
21234 		&& cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21235 	      {
21236 		/* ...whose declarator-id is qualified.  If it isn't, never
21237 		   assume the parameters to refer to types.  */
21238 		if (qualifying_scope == NULL_TREE)
21239 		  flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21240 		else
21241 		  {
21242 		    /* Now we have something like
21243 		       template <typename T> int C::x(S::p);
21244 		       which can be a function template declaration or a
21245 		       variable template definition.  If name lookup for
21246 		       the declarator-id C::x finds one or more function
21247 		       templates, assume S::p to name a type.  Otherwise,
21248 		       don't.  */
21249 		    tree decl
21250 		      = cp_parser_lookup_name_simple (parser, unqualified_name,
21251 						      token->location);
21252 		    if (!is_overloaded_fn (decl)
21253 			/* Allow
21254 			   template<typename T>
21255 			   A<T>::A(T::type) { }  */
21256 			&& !(MAYBE_CLASS_TYPE_P (qualifying_scope)
21257 			     && constructor_name_p (unqualified_name,
21258 						    qualifying_scope)))
21259 		      flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21260 		  }
21261 	      }
21262 	  }
21263 
21264 	handle_declarator:;
21265 	  scope = get_scope_of_declarator (declarator);
21266 	  if (scope)
21267 	    {
21268 	      /* Any names that appear after the declarator-id for a
21269 		 member are looked up in the containing scope.  */
21270 	      if (at_function_scope_p ())
21271 		{
21272 		  /* But declarations with qualified-ids can't appear in a
21273 		     function.  */
21274 		  cp_parser_error (parser, "qualified-id in declaration");
21275 		  declarator = cp_error_declarator;
21276 		  break;
21277 		}
21278 	      pushed_scope = push_scope (scope);
21279 	    }
21280 	  parser->in_declarator_p = true;
21281 	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21282 	      || (declarator && declarator->kind == cdk_id))
21283 	    /* Default args are only allowed on function
21284 	       declarations.  */
21285 	    parser->default_arg_ok_p = saved_default_arg_ok_p;
21286 	  else
21287 	    parser->default_arg_ok_p = false;
21288 
21289 	  first = false;
21290 	}
21291       /* We're done.  */
21292       else
21293 	break;
21294     }
21295 
21296   /* For an abstract declarator, we might wind up with nothing at this
21297      point.  That's an error; the declarator is not optional.  */
21298   if (!declarator)
21299     cp_parser_error (parser, "expected declarator");
21300   else if (open_paren)
21301     {
21302       /* Record overly parenthesized declarator so we can give a
21303 	 diagnostic about confusing decl/expr disambiguation.  */
21304       if (declarator->kind == cdk_array)
21305 	{
21306 	  /* If the open and close parens are on different lines, this
21307 	     is probably a formatting thing, so ignore.  */
21308 	  expanded_location open = expand_location (open_paren->location);
21309 	  expanded_location close = expand_location (close_paren->location);
21310 	  if (open.line != close.line || open.file != close.file)
21311 	    open_paren = NULL;
21312 	}
21313       if (open_paren)
21314 	declarator->parenthesized = open_paren->location;
21315     }
21316 
21317   /* If we entered a scope, we must exit it now.  */
21318   if (pushed_scope)
21319     pop_scope (pushed_scope);
21320 
21321   parser->default_arg_ok_p = saved_default_arg_ok_p;
21322   parser->in_declarator_p = saved_in_declarator_p;
21323 
21324   return declarator;
21325 }
21326 
21327 /* Parse a ptr-operator.
21328 
21329    ptr-operator:
21330      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21331      * cv-qualifier-seq [opt]
21332      &
21333      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21334      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21335 
21336    GNU Extension:
21337 
21338    ptr-operator:
21339      & cv-qualifier-seq [opt]
21340 
21341    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21342    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21343    an rvalue reference. In the case of a pointer-to-member, *TYPE is
21344    filled in with the TYPE containing the member.  *CV_QUALS is
21345    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21346    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
21347    Note that the tree codes returned by this function have nothing
21348    to do with the types of trees that will be eventually be created
21349    to represent the pointer or reference type being parsed. They are
21350    just constants with suggestive names. */
21351 static enum tree_code
cp_parser_ptr_operator(cp_parser * parser,tree * type,cp_cv_quals * cv_quals,tree * attributes)21352 cp_parser_ptr_operator (cp_parser* parser,
21353 			tree* type,
21354 			cp_cv_quals *cv_quals,
21355 			tree *attributes)
21356 {
21357   enum tree_code code = ERROR_MARK;
21358   cp_token *token;
21359   tree attrs = NULL_TREE;
21360 
21361   /* Assume that it's not a pointer-to-member.  */
21362   *type = NULL_TREE;
21363   /* And that there are no cv-qualifiers.  */
21364   *cv_quals = TYPE_UNQUALIFIED;
21365 
21366   /* Peek at the next token.  */
21367   token = cp_lexer_peek_token (parser->lexer);
21368 
21369   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
21370   if (token->type == CPP_MULT)
21371     code = INDIRECT_REF;
21372   else if (token->type == CPP_AND)
21373     code = ADDR_EXPR;
21374   else if ((cxx_dialect != cxx98) &&
21375 	   token->type == CPP_AND_AND) /* C++0x only */
21376     code = NON_LVALUE_EXPR;
21377 
21378   if (code != ERROR_MARK)
21379     {
21380       /* Consume the `*', `&' or `&&'.  */
21381       cp_lexer_consume_token (parser->lexer);
21382 
21383       /* A `*' can be followed by a cv-qualifier-seq, and so can a
21384 	 `&', if we are allowing GNU extensions.  (The only qualifier
21385 	 that can legally appear after `&' is `restrict', but that is
21386 	 enforced during semantic analysis.  */
21387       if (code == INDIRECT_REF
21388 	  || cp_parser_allow_gnu_extensions_p (parser))
21389 	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21390 
21391       attrs = cp_parser_std_attribute_spec_seq (parser);
21392       if (attributes != NULL)
21393 	*attributes = attrs;
21394     }
21395   else
21396     {
21397       /* Try the pointer-to-member case.  */
21398       cp_parser_parse_tentatively (parser);
21399       /* Look for the optional `::' operator.  */
21400       cp_parser_global_scope_opt (parser,
21401 				  /*current_scope_valid_p=*/false);
21402       /* Look for the nested-name specifier.  */
21403       token = cp_lexer_peek_token (parser->lexer);
21404       cp_parser_nested_name_specifier (parser,
21405 				       /*typename_keyword_p=*/false,
21406 				       /*check_dependency_p=*/true,
21407 				       /*type_p=*/false,
21408 				       /*is_declaration=*/false);
21409       /* If we found it, and the next token is a `*', then we are
21410 	 indeed looking at a pointer-to-member operator.  */
21411       if (!cp_parser_error_occurred (parser)
21412 	  && cp_parser_require (parser, CPP_MULT, RT_MULT))
21413 	{
21414 	  /* Indicate that the `*' operator was used.  */
21415 	  code = INDIRECT_REF;
21416 
21417 	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21418 	    error_at (token->location, "%qD is a namespace", parser->scope);
21419 	  else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21420 	    error_at (token->location, "cannot form pointer to member of "
21421 		      "non-class %q#T", parser->scope);
21422 	  else
21423 	    {
21424 	      /* The type of which the member is a member is given by the
21425 		 current SCOPE.  */
21426 	      *type = parser->scope;
21427 	      /* The next name will not be qualified.  */
21428 	      parser->scope = NULL_TREE;
21429 	      parser->qualifying_scope = NULL_TREE;
21430 	      parser->object_scope = NULL_TREE;
21431 	      /* Look for optional c++11 attributes.  */
21432 	      attrs = cp_parser_std_attribute_spec_seq (parser);
21433 	      if (attributes != NULL)
21434 		*attributes = attrs;
21435 	      /* Look for the optional cv-qualifier-seq.  */
21436 	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21437 	    }
21438 	}
21439       /* If that didn't work we don't have a ptr-operator.  */
21440       if (!cp_parser_parse_definitely (parser))
21441 	cp_parser_error (parser, "expected ptr-operator");
21442     }
21443 
21444   return code;
21445 }
21446 
21447 /* Parse an (optional) cv-qualifier-seq.
21448 
21449    cv-qualifier-seq:
21450      cv-qualifier cv-qualifier-seq [opt]
21451 
21452    cv-qualifier:
21453      const
21454      volatile
21455 
21456    GNU Extension:
21457 
21458    cv-qualifier:
21459      __restrict__
21460 
21461    Returns a bitmask representing the cv-qualifiers.  */
21462 
21463 static cp_cv_quals
cp_parser_cv_qualifier_seq_opt(cp_parser * parser)21464 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21465 {
21466   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21467 
21468   while (true)
21469     {
21470       cp_token *token;
21471       cp_cv_quals cv_qualifier;
21472 
21473       /* Peek at the next token.  */
21474       token = cp_lexer_peek_token (parser->lexer);
21475       /* See if it's a cv-qualifier.  */
21476       switch (token->keyword)
21477 	{
21478 	case RID_CONST:
21479 	  cv_qualifier = TYPE_QUAL_CONST;
21480 	  break;
21481 
21482 	case RID_VOLATILE:
21483 	  cv_qualifier = TYPE_QUAL_VOLATILE;
21484 	  break;
21485 
21486 	case RID_RESTRICT:
21487 	  cv_qualifier = TYPE_QUAL_RESTRICT;
21488 	  break;
21489 
21490 	default:
21491 	  cv_qualifier = TYPE_UNQUALIFIED;
21492 	  break;
21493 	}
21494 
21495       if (!cv_qualifier)
21496 	break;
21497 
21498       if (cv_quals & cv_qualifier)
21499 	{
21500 	  gcc_rich_location richloc (token->location);
21501 	  richloc.add_fixit_remove ();
21502 	  error_at (&richloc, "duplicate cv-qualifier");
21503 	  cp_lexer_purge_token (parser->lexer);
21504 	}
21505       else
21506 	{
21507 	  cp_lexer_consume_token (parser->lexer);
21508 	  cv_quals |= cv_qualifier;
21509 	}
21510     }
21511 
21512   return cv_quals;
21513 }
21514 
21515 /* Parse an (optional) ref-qualifier
21516 
21517    ref-qualifier:
21518      &
21519      &&
21520 
21521    Returns cp_ref_qualifier representing ref-qualifier. */
21522 
21523 static cp_ref_qualifier
cp_parser_ref_qualifier_opt(cp_parser * parser)21524 cp_parser_ref_qualifier_opt (cp_parser* parser)
21525 {
21526   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21527 
21528   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
21529   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21530     return ref_qual;
21531 
21532   while (true)
21533     {
21534       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21535       cp_token *token = cp_lexer_peek_token (parser->lexer);
21536 
21537       switch (token->type)
21538 	{
21539 	case CPP_AND:
21540 	  curr_ref_qual = REF_QUAL_LVALUE;
21541 	  break;
21542 
21543 	case CPP_AND_AND:
21544 	  curr_ref_qual = REF_QUAL_RVALUE;
21545 	  break;
21546 
21547 	default:
21548 	  curr_ref_qual = REF_QUAL_NONE;
21549 	  break;
21550 	}
21551 
21552       if (!curr_ref_qual)
21553 	break;
21554       else if (ref_qual)
21555 	{
21556 	  error_at (token->location, "multiple ref-qualifiers");
21557 	  cp_lexer_purge_token (parser->lexer);
21558 	}
21559       else
21560 	{
21561 	  ref_qual = curr_ref_qual;
21562 	  cp_lexer_consume_token (parser->lexer);
21563 	}
21564     }
21565 
21566   return ref_qual;
21567 }
21568 
21569 /* Parse an optional tx-qualifier.
21570 
21571    tx-qualifier:
21572      transaction_safe
21573      transaction_safe_dynamic  */
21574 
21575 static tree
cp_parser_tx_qualifier_opt(cp_parser * parser)21576 cp_parser_tx_qualifier_opt (cp_parser *parser)
21577 {
21578   cp_token *token = cp_lexer_peek_token (parser->lexer);
21579   if (token->type == CPP_NAME)
21580     {
21581       tree name = token->u.value;
21582       const char *p = IDENTIFIER_POINTER (name);
21583       const int len = strlen ("transaction_safe");
21584       if (!strncmp (p, "transaction_safe", len))
21585 	{
21586 	  p += len;
21587 	  if (*p == '\0'
21588 	      || !strcmp (p, "_dynamic"))
21589 	    {
21590 	      cp_lexer_consume_token (parser->lexer);
21591 	      if (!flag_tm)
21592 		{
21593 		  error ("%qE requires %<-fgnu-tm%>", name);
21594 		  return NULL_TREE;
21595 		}
21596 	      else
21597 		return name;
21598 	    }
21599 	}
21600     }
21601   return NULL_TREE;
21602 }
21603 
21604 /* Parse an (optional) virt-specifier-seq.
21605 
21606    virt-specifier-seq:
21607      virt-specifier virt-specifier-seq [opt]
21608 
21609    virt-specifier:
21610      override
21611      final
21612 
21613    Returns a bitmask representing the virt-specifiers.  */
21614 
21615 static cp_virt_specifiers
cp_parser_virt_specifier_seq_opt(cp_parser * parser)21616 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21617 {
21618   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21619 
21620   while (true)
21621     {
21622       cp_token *token;
21623       cp_virt_specifiers virt_specifier;
21624 
21625       /* Peek at the next token.  */
21626       token = cp_lexer_peek_token (parser->lexer);
21627       /* See if it's a virt-specifier-qualifier.  */
21628       if (token->type != CPP_NAME)
21629         break;
21630       if (id_equal (token->u.value, "override"))
21631         {
21632           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21633           virt_specifier = VIRT_SPEC_OVERRIDE;
21634         }
21635       else if (id_equal (token->u.value, "final"))
21636         {
21637           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21638           virt_specifier = VIRT_SPEC_FINAL;
21639         }
21640       else if (id_equal (token->u.value, "__final"))
21641         {
21642           virt_specifier = VIRT_SPEC_FINAL;
21643         }
21644       else
21645 	break;
21646 
21647       if (virt_specifiers & virt_specifier)
21648 	{
21649 	  gcc_rich_location richloc (token->location);
21650 	  richloc.add_fixit_remove ();
21651 	  error_at (&richloc, "duplicate virt-specifier");
21652 	  cp_lexer_purge_token (parser->lexer);
21653 	}
21654       else
21655 	{
21656 	  cp_lexer_consume_token (parser->lexer);
21657 	  virt_specifiers |= virt_specifier;
21658 	}
21659     }
21660   return virt_specifiers;
21661 }
21662 
21663 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21664    is in scope even though it isn't real.  */
21665 
21666 void
inject_this_parameter(tree ctype,cp_cv_quals quals)21667 inject_this_parameter (tree ctype, cp_cv_quals quals)
21668 {
21669   tree this_parm;
21670 
21671   if (current_class_ptr)
21672     {
21673       /* We don't clear this between NSDMIs.  Is it already what we want?  */
21674       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21675       if (DECL_P (current_class_ptr)
21676 	  && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21677 	  && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21678 	  && cp_type_quals (type) == quals)
21679 	return;
21680     }
21681 
21682   this_parm = build_this_parm (NULL_TREE, ctype, quals);
21683   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
21684   current_class_ptr = NULL_TREE;
21685   current_class_ref
21686     = cp_build_fold_indirect_ref (this_parm);
21687   current_class_ptr = this_parm;
21688 }
21689 
21690 /* Return true iff our current scope is a non-static data member
21691    initializer.  */
21692 
21693 bool
parsing_nsdmi(void)21694 parsing_nsdmi (void)
21695 {
21696   /* We recognize NSDMI context by the context-less 'this' pointer set up
21697      by the function above.  */
21698   if (current_class_ptr
21699       && TREE_CODE (current_class_ptr) == PARM_DECL
21700       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21701     return true;
21702   return false;
21703 }
21704 
21705 /* Parse a late-specified return type, if any.  This is not a separate
21706    non-terminal, but part of a function declarator, which looks like
21707 
21708    -> trailing-type-specifier-seq abstract-declarator(opt)
21709 
21710    Returns the type indicated by the type-id.
21711 
21712    In addition to this, parse any queued up #pragma omp declare simd
21713    clauses, and #pragma acc routine clauses.
21714 
21715    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21716    function.  */
21717 
21718 static tree
cp_parser_late_return_type_opt(cp_parser * parser,cp_declarator * declarator,tree & requires_clause,cp_cv_quals quals)21719 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21720 				tree& requires_clause, cp_cv_quals quals)
21721 {
21722   cp_token *token;
21723   tree type = NULL_TREE;
21724   bool declare_simd_p = (parser->omp_declare_simd
21725 			 && declarator
21726 			 && declarator->kind == cdk_id);
21727 
21728   bool oacc_routine_p = (parser->oacc_routine
21729 			 && declarator
21730 			 && declarator->kind == cdk_id);
21731 
21732   /* Peek at the next token.  */
21733   token = cp_lexer_peek_token (parser->lexer);
21734   /* A late-specified return type is indicated by an initial '->'. */
21735   if (token->type != CPP_DEREF
21736       && token->keyword != RID_REQUIRES
21737       && !(token->type == CPP_NAME
21738 	   && token->u.value == ridpointers[RID_REQUIRES])
21739       && !(declare_simd_p || oacc_routine_p))
21740     return NULL_TREE;
21741 
21742   tree save_ccp = current_class_ptr;
21743   tree save_ccr = current_class_ref;
21744   if (quals >= 0)
21745     {
21746       /* DR 1207: 'this' is in scope in the trailing return type.  */
21747       inject_this_parameter (current_class_type, quals);
21748     }
21749 
21750   if (token->type == CPP_DEREF)
21751     {
21752       /* Consume the ->.  */
21753       cp_lexer_consume_token (parser->lexer);
21754 
21755       type = cp_parser_trailing_type_id (parser);
21756     }
21757 
21758   /* Function declarations may be followed by a trailing
21759      requires-clause.  */
21760   requires_clause = cp_parser_requires_clause_opt (parser);
21761 
21762   if (declare_simd_p)
21763     declarator->attributes
21764       = cp_parser_late_parsing_omp_declare_simd (parser,
21765 						 declarator->attributes);
21766   if (oacc_routine_p)
21767     declarator->attributes
21768       = cp_parser_late_parsing_oacc_routine (parser,
21769 					     declarator->attributes);
21770 
21771   if (quals >= 0)
21772     {
21773       current_class_ptr = save_ccp;
21774       current_class_ref = save_ccr;
21775     }
21776 
21777   return type;
21778 }
21779 
21780 /* Parse a declarator-id.
21781 
21782    declarator-id:
21783      id-expression
21784      :: [opt] nested-name-specifier [opt] type-name
21785 
21786    In the `id-expression' case, the value returned is as for
21787    cp_parser_id_expression if the id-expression was an unqualified-id.
21788    If the id-expression was a qualified-id, then a SCOPE_REF is
21789    returned.  The first operand is the scope (either a NAMESPACE_DECL
21790    or TREE_TYPE), but the second is still just a representation of an
21791    unqualified-id.  */
21792 
21793 static tree
cp_parser_declarator_id(cp_parser * parser,bool optional_p)21794 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21795 {
21796   tree id;
21797   /* The expression must be an id-expression.  Assume that qualified
21798      names are the names of types so that:
21799 
21800        template <class T>
21801        int S<T>::R::i = 3;
21802 
21803      will work; we must treat `S<T>::R' as the name of a type.
21804      Similarly, assume that qualified names are templates, where
21805      required, so that:
21806 
21807        template <class T>
21808        int S<T>::R<T>::i = 3;
21809 
21810      will work, too.  */
21811   id = cp_parser_id_expression (parser,
21812 				/*template_keyword_p=*/false,
21813 				/*check_dependency_p=*/false,
21814 				/*template_p=*/NULL,
21815 				/*declarator_p=*/true,
21816 				optional_p);
21817   if (id && BASELINK_P (id))
21818     id = BASELINK_FUNCTIONS (id);
21819   return id;
21820 }
21821 
21822 /* Parse a type-id.
21823 
21824    type-id:
21825      type-specifier-seq abstract-declarator [opt]
21826 
21827    The parser flags FLAGS is used to control type-specifier parsing.
21828 
21829    If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21830 
21831    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21832    i.e. we've just seen "->".
21833 
21834    Returns the TYPE specified.  */
21835 
21836 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)21837 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21838 		     bool is_template_arg, bool is_trailing_return,
21839 		     location_t *type_location)
21840 {
21841   cp_decl_specifier_seq type_specifier_seq;
21842   cp_declarator *abstract_declarator;
21843 
21844   /* Parse the type-specifier-seq.  */
21845   cp_parser_type_specifier_seq (parser, flags,
21846 				/*is_declaration=*/false,
21847 				is_trailing_return,
21848 				&type_specifier_seq);
21849   if (type_location)
21850     *type_location = type_specifier_seq.locations[ds_type_spec];
21851 
21852   if (is_template_arg && type_specifier_seq.type
21853       && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21854       && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21855     /* A bare template name as a template argument is a template template
21856        argument, not a placeholder, so fail parsing it as a type argument.  */
21857     {
21858       gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21859       cp_parser_simulate_error (parser);
21860       return error_mark_node;
21861     }
21862   if (type_specifier_seq.type == error_mark_node)
21863     return error_mark_node;
21864 
21865   /* There might or might not be an abstract declarator.  */
21866   cp_parser_parse_tentatively (parser);
21867   /* Look for the declarator.  */
21868   abstract_declarator
21869     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21870 			    CP_PARSER_FLAGS_NONE, NULL,
21871 			    /*parenthesized_p=*/NULL,
21872 			    /*member_p=*/false,
21873 			    /*friend_p=*/false,
21874 			    /*static_p=*/false);
21875   /* Check to see if there really was a declarator.  */
21876   if (!cp_parser_parse_definitely (parser))
21877     abstract_declarator = NULL;
21878 
21879   if (type_specifier_seq.type
21880       /* The concepts TS allows 'auto' as a type-id.  */
21881       && (!flag_concepts || parser->in_type_id_in_expr_p)
21882       /* None of the valid uses of 'auto' in C++14 involve the type-id
21883 	 nonterminal, but it is valid in a trailing-return-type.  */
21884       && !(cxx_dialect >= cxx14 && is_trailing_return))
21885     if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21886       {
21887 	/* A type-id with type 'auto' is only ok if the abstract declarator
21888 	   is a function declarator with a late-specified return type.
21889 
21890 	   A type-id with 'auto' is also valid in a trailing-return-type
21891 	   in a compound-requirement. */
21892 	if (abstract_declarator
21893 	    && abstract_declarator->kind == cdk_function
21894 	    && abstract_declarator->u.function.late_return_type)
21895 	  /* OK */;
21896 	else if (parser->in_result_type_constraint_p)
21897 	  /* OK */;
21898 	else
21899 	  {
21900 	    location_t loc = type_specifier_seq.locations[ds_type_spec];
21901 	    if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21902 	      {
21903 		error_at (loc, "missing template arguments after %qT",
21904 			  auto_node);
21905 		inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21906 			tmpl);
21907 	      }
21908 	    else
21909 	      error_at (loc, "invalid use of %qT", auto_node);
21910 	    return error_mark_node;
21911 	  }
21912       }
21913 
21914   return groktypename (&type_specifier_seq, abstract_declarator,
21915 		       is_template_arg);
21916 }
21917 
21918 /* Wrapper for cp_parser_type_id_1.  */
21919 
21920 static tree
cp_parser_type_id(cp_parser * parser,cp_parser_flags flags,location_t * type_location)21921 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21922 		   location_t *type_location)
21923 {
21924   return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21925 }
21926 
21927 /* Wrapper for cp_parser_type_id_1.  */
21928 
21929 static tree
cp_parser_template_type_arg(cp_parser * parser)21930 cp_parser_template_type_arg (cp_parser *parser)
21931 {
21932   tree r;
21933   const char *saved_message = parser->type_definition_forbidden_message;
21934   parser->type_definition_forbidden_message
21935     = G_("types may not be defined in template arguments");
21936   r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21937   parser->type_definition_forbidden_message = saved_message;
21938   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21939     {
21940       error ("invalid use of %<auto%> in template argument");
21941       r = error_mark_node;
21942     }
21943   return r;
21944 }
21945 
21946 /* Wrapper for cp_parser_type_id_1.  */
21947 
21948 static tree
cp_parser_trailing_type_id(cp_parser * parser)21949 cp_parser_trailing_type_id (cp_parser *parser)
21950 {
21951   return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21952 			      false, true, NULL);
21953 }
21954 
21955 /* Parse a type-specifier-seq.
21956 
21957    type-specifier-seq:
21958      type-specifier type-specifier-seq [opt]
21959 
21960    GNU extension:
21961 
21962    type-specifier-seq:
21963      attributes type-specifier-seq [opt]
21964 
21965    The parser flags FLAGS is used to control type-specifier parsing.
21966 
21967    If IS_DECLARATION is true, we are at the start of a "condition" or
21968    exception-declaration, so we might be followed by a declarator-id.
21969 
21970    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21971    i.e. we've just seen "->".
21972 
21973    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
21974 
21975 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)21976 cp_parser_type_specifier_seq (cp_parser* parser,
21977 			      cp_parser_flags flags,
21978 			      bool is_declaration,
21979 			      bool is_trailing_return,
21980 			      cp_decl_specifier_seq *type_specifier_seq)
21981 {
21982   bool seen_type_specifier = false;
21983   cp_token *start_token = NULL;
21984 
21985   /* Clear the TYPE_SPECIFIER_SEQ.  */
21986   clear_decl_specs (type_specifier_seq);
21987 
21988   flags |= CP_PARSER_FLAGS_OPTIONAL;
21989   /* In the context of a trailing return type, enum E { } is an
21990      elaborated-type-specifier followed by a function-body, not an
21991      enum-specifier.  */
21992   if (is_trailing_return)
21993     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21994 
21995   /* Parse the type-specifiers and attributes.  */
21996   while (true)
21997     {
21998       tree type_specifier;
21999       bool is_cv_qualifier;
22000 
22001       /* Check for attributes first.  */
22002       if (cp_next_tokens_can_be_attribute_p (parser))
22003 	{
22004 	  /* GNU attributes at the end of a declaration apply to the
22005 	     declaration as a whole, not to the trailing return type.  So look
22006 	     ahead to see if these attributes are at the end.  */
22007 	  if (seen_type_specifier && is_trailing_return
22008 	      && cp_next_tokens_can_be_gnu_attribute_p (parser))
22009 	    {
22010 	      size_t n = cp_parser_skip_attributes_opt (parser, 1);
22011 	      cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
22012 	      if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
22013 		  || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
22014 		break;
22015 	    }
22016 	  type_specifier_seq->attributes
22017 	    = attr_chainon (type_specifier_seq->attributes,
22018 			    cp_parser_attributes_opt (parser));
22019 	  continue;
22020 	}
22021 
22022       /* record the token of the beginning of the type specifier seq,
22023          for error reporting purposes*/
22024      if (!start_token)
22025        start_token = cp_lexer_peek_token (parser->lexer);
22026 
22027       /* Look for the type-specifier.  */
22028       type_specifier = cp_parser_type_specifier (parser,
22029 						 flags,
22030 						 type_specifier_seq,
22031 						 /*is_declaration=*/false,
22032 						 NULL,
22033 						 &is_cv_qualifier);
22034       if (!type_specifier)
22035 	{
22036 	  /* If the first type-specifier could not be found, this is not a
22037 	     type-specifier-seq at all.  */
22038 	  if (!seen_type_specifier)
22039 	    {
22040 	      /* Set in_declarator_p to avoid skipping to the semicolon.  */
22041 	      int in_decl = parser->in_declarator_p;
22042 	      parser->in_declarator_p = true;
22043 
22044 	      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
22045 		  || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
22046 		cp_parser_error (parser, "expected type-specifier");
22047 
22048 	      parser->in_declarator_p = in_decl;
22049 
22050 	      type_specifier_seq->type = error_mark_node;
22051 	      return;
22052 	    }
22053 	  /* If subsequent type-specifiers could not be found, the
22054 	     type-specifier-seq is complete.  */
22055 	  break;
22056 	}
22057 
22058       seen_type_specifier = true;
22059       /* The standard says that a condition can be:
22060 
22061 	    type-specifier-seq declarator = assignment-expression
22062 
22063 	 However, given:
22064 
22065 	   struct S {};
22066 	   if (int S = ...)
22067 
22068 	 we should treat the "S" as a declarator, not as a
22069 	 type-specifier.  The standard doesn't say that explicitly for
22070 	 type-specifier-seq, but it does say that for
22071 	 decl-specifier-seq in an ordinary declaration.  Perhaps it
22072 	 would be clearer just to allow a decl-specifier-seq here, and
22073 	 then add a semantic restriction that if any decl-specifiers
22074 	 that are not type-specifiers appear, the program is invalid.  */
22075       if (is_declaration && !is_cv_qualifier)
22076 	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
22077     }
22078 }
22079 
22080 /* Return whether the function currently being declared has an associated
22081    template parameter list.  */
22082 
22083 static bool
function_being_declared_is_template_p(cp_parser * parser)22084 function_being_declared_is_template_p (cp_parser* parser)
22085 {
22086   if (!current_template_parms || processing_template_parmlist)
22087     return false;
22088 
22089   if (parser->implicit_template_scope)
22090     return true;
22091 
22092   if (at_class_scope_p ()
22093       && TYPE_BEING_DEFINED (current_class_type))
22094     return parser->num_template_parameter_lists != 0;
22095 
22096   return ((int) parser->num_template_parameter_lists > template_class_depth
22097 	  (current_class_type));
22098 }
22099 
22100 /* Parse a parameter-declaration-clause.
22101 
22102    parameter-declaration-clause:
22103      parameter-declaration-list [opt] ... [opt]
22104      parameter-declaration-list , ...
22105 
22106    The parser flags FLAGS is used to control type-specifier parsing.
22107 
22108    Returns a representation for the parameter declarations.  A return
22109    value of NULL indicates a parameter-declaration-clause consisting
22110    only of an ellipsis.  */
22111 
22112 static tree
cp_parser_parameter_declaration_clause(cp_parser * parser,cp_parser_flags flags)22113 cp_parser_parameter_declaration_clause (cp_parser* parser,
22114 					cp_parser_flags flags)
22115 {
22116   tree parameters;
22117   cp_token *token;
22118   bool ellipsis_p;
22119 
22120   temp_override<bool> cleanup
22121     (parser->auto_is_implicit_function_template_parm_p);
22122 
22123   if (!processing_specialization
22124       && !processing_template_parmlist
22125       && !processing_explicit_instantiation
22126       /* default_arg_ok_p tracks whether this is a parameter-clause for an
22127          actual function or a random abstract declarator.  */
22128       && parser->default_arg_ok_p)
22129     if (!current_function_decl
22130 	|| (current_class_type && LAMBDA_TYPE_P (current_class_type)))
22131       parser->auto_is_implicit_function_template_parm_p = true;
22132 
22133   /* Peek at the next token.  */
22134   token = cp_lexer_peek_token (parser->lexer);
22135   /* Check for trivial parameter-declaration-clauses.  */
22136   if (token->type == CPP_ELLIPSIS)
22137     {
22138       /* Consume the `...' token.  */
22139       cp_lexer_consume_token (parser->lexer);
22140       return NULL_TREE;
22141     }
22142   else if (token->type == CPP_CLOSE_PAREN)
22143     /* There are no parameters.  */
22144     return void_list_node;
22145   /* Check for `(void)', too, which is a special case.  */
22146   else if (token->keyword == RID_VOID
22147 	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22148 	       == CPP_CLOSE_PAREN))
22149     {
22150       /* Consume the `void' token.  */
22151       cp_lexer_consume_token (parser->lexer);
22152       /* There are no parameters.  */
22153       return void_list_node;
22154     }
22155 
22156   /* Parse the parameter-declaration-list.  */
22157   parameters = cp_parser_parameter_declaration_list (parser, flags);
22158   /* If a parse error occurred while parsing the
22159      parameter-declaration-list, then the entire
22160      parameter-declaration-clause is erroneous.  */
22161   if (parameters == error_mark_node)
22162     return NULL_TREE;
22163 
22164   /* Peek at the next token.  */
22165   token = cp_lexer_peek_token (parser->lexer);
22166   /* If it's a `,', the clause should terminate with an ellipsis.  */
22167   if (token->type == CPP_COMMA)
22168     {
22169       /* Consume the `,'.  */
22170       cp_lexer_consume_token (parser->lexer);
22171       /* Expect an ellipsis.  */
22172       ellipsis_p
22173 	= (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22174     }
22175   /* It might also be `...' if the optional trailing `,' was
22176      omitted.  */
22177   else if (token->type == CPP_ELLIPSIS)
22178     {
22179       /* Consume the `...' token.  */
22180       cp_lexer_consume_token (parser->lexer);
22181       /* And remember that we saw it.  */
22182       ellipsis_p = true;
22183     }
22184   else
22185     ellipsis_p = false;
22186 
22187   /* Finish the parameter list.  */
22188   if (!ellipsis_p)
22189     parameters = chainon (parameters, void_list_node);
22190 
22191   return parameters;
22192 }
22193 
22194 /* Parse a parameter-declaration-list.
22195 
22196    parameter-declaration-list:
22197      parameter-declaration
22198      parameter-declaration-list , parameter-declaration
22199 
22200    The parser flags FLAGS is used to control type-specifier parsing.
22201 
22202    Returns a representation of the parameter-declaration-list, as for
22203    cp_parser_parameter_declaration_clause.  However, the
22204    `void_list_node' is never appended to the list.  */
22205 
22206 static tree
cp_parser_parameter_declaration_list(cp_parser * parser,cp_parser_flags flags)22207 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22208 {
22209   tree parameters = NULL_TREE;
22210   tree *tail = &parameters;
22211   bool saved_in_unbraced_linkage_specification_p;
22212   int index = 0;
22213 
22214   /* The special considerations that apply to a function within an
22215      unbraced linkage specifications do not apply to the parameters
22216      to the function.  */
22217   saved_in_unbraced_linkage_specification_p
22218     = parser->in_unbraced_linkage_specification_p;
22219   parser->in_unbraced_linkage_specification_p = false;
22220 
22221   /* Look for more parameters.  */
22222   while (true)
22223     {
22224       cp_parameter_declarator *parameter;
22225       tree decl = error_mark_node;
22226       bool parenthesized_p = false;
22227 
22228       /* Parse the parameter.  */
22229       parameter
22230 	= cp_parser_parameter_declaration (parser, flags,
22231 					   /*template_parm_p=*/false,
22232 					   &parenthesized_p);
22233 
22234       /* We don't know yet if the enclosing context is deprecated, so wait
22235 	 and warn in grokparms if appropriate.  */
22236       deprecated_state = DEPRECATED_SUPPRESS;
22237 
22238       if (parameter)
22239 	{
22240 	  decl = grokdeclarator (parameter->declarator,
22241 				 &parameter->decl_specifiers,
22242 				 PARM,
22243 				 parameter->default_argument != NULL_TREE,
22244 				 &parameter->decl_specifiers.attributes);
22245 	  if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22246 	    DECL_SOURCE_LOCATION (decl) = parameter->loc;
22247 	}
22248 
22249       deprecated_state = DEPRECATED_NORMAL;
22250 
22251       /* If a parse error occurred parsing the parameter declaration,
22252 	 then the entire parameter-declaration-list is erroneous.  */
22253       if (decl == error_mark_node)
22254 	{
22255 	  parameters = error_mark_node;
22256 	  break;
22257 	}
22258 
22259       if (parameter->decl_specifiers.attributes)
22260 	cplus_decl_attributes (&decl,
22261 			       parameter->decl_specifiers.attributes,
22262 			       0);
22263       if (DECL_NAME (decl))
22264 	decl = pushdecl (decl);
22265 
22266       if (decl != error_mark_node)
22267 	{
22268 	  retrofit_lang_decl (decl);
22269 	  DECL_PARM_INDEX (decl) = ++index;
22270 	  DECL_PARM_LEVEL (decl) = function_parm_depth ();
22271 	}
22272 
22273       /* Add the new parameter to the list.  */
22274       *tail = build_tree_list (parameter->default_argument, decl);
22275       tail = &TREE_CHAIN (*tail);
22276 
22277       /* Peek at the next token.  */
22278       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22279 	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22280 	  /* These are for Objective-C++ */
22281 	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22282 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22283 	/* The parameter-declaration-list is complete.  */
22284 	break;
22285       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22286 	{
22287 	  cp_token *token;
22288 
22289 	  /* Peek at the next token.  */
22290 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
22291 	  /* If it's an ellipsis, then the list is complete.  */
22292 	  if (token->type == CPP_ELLIPSIS)
22293 	    break;
22294 	  /* Otherwise, there must be more parameters.  Consume the
22295 	     `,'.  */
22296 	  cp_lexer_consume_token (parser->lexer);
22297 	  /* When parsing something like:
22298 
22299 		int i(float f, double d)
22300 
22301 	     we can tell after seeing the declaration for "f" that we
22302 	     are not looking at an initialization of a variable "i",
22303 	     but rather at the declaration of a function "i".
22304 
22305 	     Due to the fact that the parsing of template arguments
22306 	     (as specified to a template-id) requires backtracking we
22307 	     cannot use this technique when inside a template argument
22308 	     list.  */
22309 	  if (!parser->in_template_argument_list_p
22310 	      && !parser->in_type_id_in_expr_p
22311 	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
22312 	      /* However, a parameter-declaration of the form
22313 		 "float(f)" (which is a valid declaration of a
22314 		 parameter "f") can also be interpreted as an
22315 		 expression (the conversion of "f" to "float").  */
22316 	      && !parenthesized_p)
22317 	    cp_parser_commit_to_tentative_parse (parser);
22318 	}
22319       else
22320 	{
22321 	  cp_parser_error (parser, "expected %<,%> or %<...%>");
22322 	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22323 	    cp_parser_skip_to_closing_parenthesis (parser,
22324 						   /*recovering=*/true,
22325 						   /*or_comma=*/false,
22326 						   /*consume_paren=*/false);
22327 	  break;
22328 	}
22329     }
22330 
22331   parser->in_unbraced_linkage_specification_p
22332     = saved_in_unbraced_linkage_specification_p;
22333 
22334   /* Reset implicit_template_scope if we are about to leave the function
22335      parameter list that introduced it.  Note that for out-of-line member
22336      definitions, there will be one or more class scopes before we get to
22337      the template parameter scope.  */
22338 
22339   if (cp_binding_level *its = parser->implicit_template_scope)
22340     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22341       {
22342 	while (maybe_its->kind == sk_class)
22343 	  maybe_its = maybe_its->level_chain;
22344 	if (maybe_its == its)
22345 	  {
22346 	    parser->implicit_template_parms = 0;
22347 	    parser->implicit_template_scope = 0;
22348 	  }
22349       }
22350 
22351   return parameters;
22352 }
22353 
22354 /* Parse a parameter declaration.
22355 
22356    parameter-declaration:
22357      decl-specifier-seq ... [opt] declarator
22358      decl-specifier-seq declarator = assignment-expression
22359      decl-specifier-seq ... [opt] abstract-declarator [opt]
22360      decl-specifier-seq abstract-declarator [opt] = assignment-expression
22361 
22362    The parser flags FLAGS is used to control type-specifier parsing.
22363 
22364    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22365    declares a template parameter.  (In that case, a non-nested `>'
22366    token encountered during the parsing of the assignment-expression
22367    is not interpreted as a greater-than operator.)
22368 
22369    Returns a representation of the parameter, or NULL if an error
22370    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22371    true iff the declarator is of the form "(p)".  */
22372 
22373 static cp_parameter_declarator *
cp_parser_parameter_declaration(cp_parser * parser,cp_parser_flags flags,bool template_parm_p,bool * parenthesized_p)22374 cp_parser_parameter_declaration (cp_parser *parser,
22375 				 cp_parser_flags flags,
22376 				 bool template_parm_p,
22377 				 bool *parenthesized_p)
22378 {
22379   int declares_class_or_enum;
22380   cp_decl_specifier_seq decl_specifiers;
22381   cp_declarator *declarator;
22382   tree default_argument;
22383   cp_token *token = NULL, *declarator_token_start = NULL;
22384   const char *saved_message;
22385   bool template_parameter_pack_p = false;
22386 
22387   /* In a template parameter, `>' is not an operator.
22388 
22389      [temp.param]
22390 
22391      When parsing a default template-argument for a non-type
22392      template-parameter, the first non-nested `>' is taken as the end
22393      of the template parameter-list rather than a greater-than
22394      operator.  */
22395 
22396   /* Type definitions may not appear in parameter types.  */
22397   saved_message = parser->type_definition_forbidden_message;
22398   parser->type_definition_forbidden_message
22399     = G_("types may not be defined in parameter types");
22400 
22401   int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22402 			   TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22403 					    (current_template_parms)) : 0);
22404 
22405   /* Parse the declaration-specifiers.  */
22406   cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22407   cp_parser_decl_specifier_seq (parser,
22408 				flags,
22409 				&decl_specifiers,
22410 				&declares_class_or_enum);
22411 
22412   /* Complain about missing 'typename' or other invalid type names.  */
22413   if (!decl_specifiers.any_type_specifiers_p
22414       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22415     decl_specifiers.type = error_mark_node;
22416 
22417   /* If an error occurred, there's no reason to attempt to parse the
22418      rest of the declaration.  */
22419   if (cp_parser_error_occurred (parser))
22420     {
22421       parser->type_definition_forbidden_message = saved_message;
22422       return NULL;
22423     }
22424 
22425   /* Peek at the next token.  */
22426   token = cp_lexer_peek_token (parser->lexer);
22427 
22428   /* If the next token is a `)', `,', `=', `>', or `...', then there
22429      is no declarator. However, when variadic templates are enabled,
22430      there may be a declarator following `...'.  */
22431   if (token->type == CPP_CLOSE_PAREN
22432       || token->type == CPP_COMMA
22433       || token->type == CPP_EQ
22434       || token->type == CPP_GREATER)
22435     {
22436       declarator = NULL;
22437       if (parenthesized_p)
22438 	*parenthesized_p = false;
22439     }
22440   /* Otherwise, there should be a declarator.  */
22441   else
22442     {
22443       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22444       parser->default_arg_ok_p = false;
22445 
22446       /* After seeing a decl-specifier-seq, if the next token is not a
22447 	 "(", there is no possibility that the code is a valid
22448 	 expression.  Therefore, if parsing tentatively, we commit at
22449 	 this point.  */
22450       if (!parser->in_template_argument_list_p
22451 	  /* In an expression context, having seen:
22452 
22453 	       (int((char ...
22454 
22455 	     we cannot be sure whether we are looking at a
22456 	     function-type (taking a "char" as a parameter) or a cast
22457 	     of some object of type "char" to "int".  */
22458 	  && !parser->in_type_id_in_expr_p
22459 	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
22460 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22461 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22462 	cp_parser_commit_to_tentative_parse (parser);
22463       /* Parse the declarator.  */
22464       declarator_token_start = token;
22465       declarator = cp_parser_declarator (parser,
22466 					 CP_PARSER_DECLARATOR_EITHER,
22467 					 CP_PARSER_FLAGS_NONE,
22468 					 /*ctor_dtor_or_conv_p=*/NULL,
22469 					 parenthesized_p,
22470 					 /*member_p=*/false,
22471 					 /*friend_p=*/false,
22472 					 /*static_p=*/false);
22473       parser->default_arg_ok_p = saved_default_arg_ok_p;
22474       /* After the declarator, allow more attributes.  */
22475       decl_specifiers.attributes
22476 	= attr_chainon (decl_specifiers.attributes,
22477 			cp_parser_attributes_opt (parser));
22478 
22479       /* If the declarator is a template parameter pack, remember that and
22480 	 clear the flag in the declarator itself so we don't get errors
22481 	 from grokdeclarator.  */
22482       if (template_parm_p && declarator && declarator->parameter_pack_p)
22483 	{
22484 	  declarator->parameter_pack_p = false;
22485 	  template_parameter_pack_p = true;
22486 	}
22487     }
22488 
22489   /* If the next token is an ellipsis, and we have not seen a declarator
22490      name, and if either the type of the declarator contains parameter
22491      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22492      for, eg, abbreviated integral type names), then we actually have a
22493      parameter pack expansion expression. Otherwise, leave the ellipsis
22494      for a C-style variadic function. */
22495   token = cp_lexer_peek_token (parser->lexer);
22496 
22497   /* If a function parameter pack was specified and an implicit template
22498      parameter was introduced during cp_parser_parameter_declaration,
22499      change any implicit parameters introduced into packs.  */
22500   if (parser->implicit_template_parms
22501       && ((token->type == CPP_ELLIPSIS
22502 	   && declarator_can_be_parameter_pack (declarator))
22503 	  || (declarator && declarator->parameter_pack_p)))
22504     {
22505       int latest_template_parm_idx = TREE_VEC_LENGTH
22506 	(INNERMOST_TEMPLATE_PARMS (current_template_parms));
22507 
22508       if (latest_template_parm_idx != template_parm_idx)
22509 	decl_specifiers.type = convert_generic_types_to_packs
22510 	  (decl_specifiers.type,
22511 	   template_parm_idx, latest_template_parm_idx);
22512     }
22513 
22514   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22515     {
22516       tree type = decl_specifiers.type;
22517 
22518       if (type && DECL_P (type))
22519         type = TREE_TYPE (type);
22520 
22521       if (((type
22522 	    && TREE_CODE (type) != TYPE_PACK_EXPANSION
22523 	    && (template_parm_p || uses_parameter_packs (type)))
22524 	   || (!type && template_parm_p))
22525 	  && declarator_can_be_parameter_pack (declarator))
22526 	{
22527 	  /* Consume the `...'. */
22528 	  cp_lexer_consume_token (parser->lexer);
22529 	  maybe_warn_variadic_templates ();
22530 
22531 	  /* Build a pack expansion type */
22532 	  if (template_parm_p)
22533 	    template_parameter_pack_p = true;
22534 	  else if (declarator)
22535 	    declarator->parameter_pack_p = true;
22536 	  else
22537 	    decl_specifiers.type = make_pack_expansion (type);
22538 	}
22539     }
22540 
22541   /* The restriction on defining new types applies only to the type
22542      of the parameter, not to the default argument.  */
22543   parser->type_definition_forbidden_message = saved_message;
22544 
22545   /* If the next token is `=', then process a default argument.  */
22546   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22547     {
22548       tree type = decl_specifiers.type;
22549       token = cp_lexer_peek_token (parser->lexer);
22550       /* If we are defining a class, then the tokens that make up the
22551 	 default argument must be saved and processed later.  */
22552       if (!template_parm_p && at_class_scope_p ()
22553 	  && TYPE_BEING_DEFINED (current_class_type)
22554 	  && !LAMBDA_TYPE_P (current_class_type))
22555 	default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22556 
22557       // A constrained-type-specifier may declare a type template-parameter.
22558       else if (declares_constrained_type_template_parameter (type))
22559         default_argument
22560           = cp_parser_default_type_template_argument (parser);
22561 
22562       // A constrained-type-specifier may declare a template-template-parameter.
22563       else if (declares_constrained_template_template_parameter (type))
22564         default_argument
22565           = cp_parser_default_template_template_argument (parser);
22566 
22567       /* Outside of a class definition, we can just parse the
22568 	 assignment-expression.  */
22569       else
22570 	default_argument
22571 	  = cp_parser_default_argument (parser, template_parm_p);
22572 
22573       if (!parser->default_arg_ok_p)
22574 	{
22575 	  permerror (token->location,
22576 		     "default arguments are only "
22577 		     "permitted for function parameters");
22578 	}
22579       else if ((declarator && declarator->parameter_pack_p)
22580 	       || template_parameter_pack_p
22581 	       || (decl_specifiers.type
22582 		   && PACK_EXPANSION_P (decl_specifiers.type)))
22583 	{
22584 	  /* Find the name of the parameter pack.  */
22585 	  cp_declarator *id_declarator = declarator;
22586 	  while (id_declarator && id_declarator->kind != cdk_id)
22587 	    id_declarator = id_declarator->declarator;
22588 
22589 	  if (id_declarator && id_declarator->kind == cdk_id)
22590 	    error_at (declarator_token_start->location,
22591 		      template_parm_p
22592 		      ? G_("template parameter pack %qD "
22593 			   "cannot have a default argument")
22594 		      : G_("parameter pack %qD cannot have "
22595 			   "a default argument"),
22596 		      id_declarator->u.id.unqualified_name);
22597 	  else
22598 	    error_at (declarator_token_start->location,
22599 		      template_parm_p
22600 		      ? G_("template parameter pack cannot have "
22601 			   "a default argument")
22602 		      : G_("parameter pack cannot have a "
22603 			   "default argument"));
22604 
22605 	  default_argument = NULL_TREE;
22606 	}
22607     }
22608   else
22609     default_argument = NULL_TREE;
22610 
22611   if (default_argument)
22612     STRIP_ANY_LOCATION_WRAPPER (default_argument);
22613 
22614   /* Generate a location for the parameter, ranging from the start of the
22615      initial token to the end of the final token (using input_location for
22616      the latter, set up by cp_lexer_set_source_position_from_token when
22617      consuming tokens).
22618 
22619      If we have a identifier, then use it for the caret location, e.g.
22620 
22621        extern int callee (int one, int (*two)(int, int), float three);
22622                                    ~~~~~~^~~~~~~~~~~~~~
22623 
22624      otherwise, reuse the start location for the caret location e.g.:
22625 
22626        extern int callee (int one, int (*)(int, int), float three);
22627                                    ^~~~~~~~~~~~~~~~~
22628 
22629   */
22630   location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22631 			  ? declarator->id_loc
22632 			  : decl_spec_token_start->location);
22633   location_t param_loc = make_location (caret_loc,
22634 					decl_spec_token_start->location,
22635 					input_location);
22636 
22637   return make_parameter_declarator (&decl_specifiers,
22638 				    declarator,
22639 				    default_argument,
22640 				    param_loc,
22641 				    template_parameter_pack_p);
22642 }
22643 
22644 /* Parse a default argument and return it.
22645 
22646    TEMPLATE_PARM_P is true if this is a default argument for a
22647    non-type template parameter.  */
22648 static tree
cp_parser_default_argument(cp_parser * parser,bool template_parm_p)22649 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22650 {
22651   tree default_argument = NULL_TREE;
22652   bool saved_greater_than_is_operator_p;
22653   unsigned char saved_local_variables_forbidden_p;
22654   bool non_constant_p, is_direct_init;
22655 
22656   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22657      set correctly.  */
22658   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22659   parser->greater_than_is_operator_p = !template_parm_p;
22660   /* Local variable names (and the `this' keyword) may not
22661      appear in a default argument.  */
22662   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22663   parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22664   /* Parse the assignment-expression.  */
22665   if (template_parm_p)
22666     push_deferring_access_checks (dk_no_deferred);
22667   tree saved_class_ptr = NULL_TREE;
22668   tree saved_class_ref = NULL_TREE;
22669   /* The "this" pointer is not valid in a default argument.  */
22670   if (cfun)
22671     {
22672       saved_class_ptr = current_class_ptr;
22673       cp_function_chain->x_current_class_ptr = NULL_TREE;
22674       saved_class_ref = current_class_ref;
22675       cp_function_chain->x_current_class_ref = NULL_TREE;
22676     }
22677   default_argument
22678     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22679   /* Restore the "this" pointer.  */
22680   if (cfun)
22681     {
22682       cp_function_chain->x_current_class_ptr = saved_class_ptr;
22683       cp_function_chain->x_current_class_ref = saved_class_ref;
22684     }
22685   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22686     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22687   if (template_parm_p)
22688     pop_deferring_access_checks ();
22689   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22690   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22691 
22692   return default_argument;
22693 }
22694 
22695 /* Parse a function-body.
22696 
22697    function-body:
22698      compound_statement  */
22699 
22700 static void
cp_parser_function_body(cp_parser * parser,bool in_function_try_block)22701 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22702 {
22703   cp_parser_compound_statement (parser, NULL, (in_function_try_block
22704 					       ? BCS_TRY_BLOCK : BCS_NORMAL),
22705 				true);
22706 }
22707 
22708 /* Parse a ctor-initializer-opt followed by a function-body.  Return
22709    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
22710    is true we are parsing a function-try-block.  */
22711 
22712 static void
cp_parser_ctor_initializer_opt_and_function_body(cp_parser * parser,bool in_function_try_block)22713 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22714 						  bool in_function_try_block)
22715 {
22716   tree body, list;
22717   const bool check_body_p
22718      = (DECL_CONSTRUCTOR_P (current_function_decl)
22719 	&& DECL_DECLARED_CONSTEXPR_P (current_function_decl));
22720   tree last = NULL;
22721 
22722   if (in_function_try_block
22723       && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22724       && cxx_dialect < cxx2a)
22725     {
22726       if (DECL_CONSTRUCTOR_P (current_function_decl))
22727 	pedwarn (input_location, 0,
22728 		 "function-try-block body of %<constexpr%> constructor only "
22729 		 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22730       else
22731 	pedwarn (input_location, 0,
22732 		 "function-try-block body of %<constexpr%> function only "
22733 		 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22734     }
22735 
22736   /* Begin the function body.  */
22737   body = begin_function_body ();
22738   /* Parse the optional ctor-initializer.  */
22739   cp_parser_ctor_initializer_opt (parser);
22740 
22741   /* If we're parsing a constexpr constructor definition, we need
22742      to check that the constructor body is indeed empty.  However,
22743      before we get to cp_parser_function_body lot of junk has been
22744      generated, so we can't just check that we have an empty block.
22745      Rather we take a snapshot of the outermost block, and check whether
22746      cp_parser_function_body changed its state.  */
22747   if (check_body_p)
22748     {
22749       list = cur_stmt_list;
22750       if (STATEMENT_LIST_TAIL (list))
22751 	last = STATEMENT_LIST_TAIL (list)->stmt;
22752     }
22753   /* Parse the function-body.  */
22754   cp_parser_function_body (parser, in_function_try_block);
22755   if (check_body_p)
22756     check_constexpr_ctor_body (last, list, /*complain=*/true);
22757   /* Finish the function body.  */
22758   finish_function_body (body);
22759 }
22760 
22761 /* Parse an initializer.
22762 
22763    initializer:
22764      = initializer-clause
22765      ( expression-list )
22766 
22767    Returns an expression representing the initializer.  If no
22768    initializer is present, NULL_TREE is returned.
22769 
22770    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22771    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
22772    set to TRUE if there is no initializer present.  If there is an
22773    initializer, and it is not a constant-expression, *NON_CONSTANT_P
22774    is set to true; otherwise it is set to false.  */
22775 
22776 static tree
cp_parser_initializer(cp_parser * parser,bool * is_direct_init,bool * non_constant_p,bool subexpression_p)22777 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22778 		       bool* non_constant_p, bool subexpression_p)
22779 {
22780   cp_token *token;
22781   tree init;
22782 
22783   /* Peek at the next token.  */
22784   token = cp_lexer_peek_token (parser->lexer);
22785 
22786   /* Let our caller know whether or not this initializer was
22787      parenthesized.  */
22788   *is_direct_init = (token->type != CPP_EQ);
22789   /* Assume that the initializer is constant.  */
22790   *non_constant_p = false;
22791 
22792   if (token->type == CPP_EQ)
22793     {
22794       /* Consume the `='.  */
22795       cp_lexer_consume_token (parser->lexer);
22796       /* Parse the initializer-clause.  */
22797       init = cp_parser_initializer_clause (parser, non_constant_p);
22798     }
22799   else if (token->type == CPP_OPEN_PAREN)
22800     {
22801       vec<tree, va_gc> *vec;
22802       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22803 						     /*cast_p=*/false,
22804 						     /*allow_expansion_p=*/true,
22805 						     non_constant_p);
22806       if (vec == NULL)
22807 	return error_mark_node;
22808       init = build_tree_list_vec (vec);
22809       release_tree_vector (vec);
22810     }
22811   else if (token->type == CPP_OPEN_BRACE)
22812     {
22813       cp_lexer_set_source_position (parser->lexer);
22814       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22815       init = cp_parser_braced_list (parser, non_constant_p);
22816       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22817     }
22818   else
22819     {
22820       /* Anything else is an error.  */
22821       cp_parser_error (parser, "expected initializer");
22822       init = error_mark_node;
22823     }
22824 
22825   if (!subexpression_p && check_for_bare_parameter_packs (init))
22826     init = error_mark_node;
22827 
22828   return init;
22829 }
22830 
22831 /* Parse an initializer-clause.
22832 
22833    initializer-clause:
22834      assignment-expression
22835      braced-init-list
22836 
22837    Returns an expression representing the initializer.
22838 
22839    If the `assignment-expression' production is used the value
22840    returned is simply a representation for the expression.
22841 
22842    Otherwise, calls cp_parser_braced_list.  */
22843 
22844 static cp_expr
cp_parser_initializer_clause(cp_parser * parser,bool * non_constant_p)22845 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22846 {
22847   cp_expr initializer;
22848 
22849   /* Assume the expression is constant.  */
22850   *non_constant_p = false;
22851 
22852   /* If it is not a `{', then we are looking at an
22853      assignment-expression.  */
22854   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22855     {
22856       initializer
22857 	= cp_parser_constant_expression (parser,
22858 					/*allow_non_constant_p=*/true,
22859 					non_constant_p);
22860     }
22861   else
22862     initializer = cp_parser_braced_list (parser, non_constant_p);
22863 
22864   return initializer;
22865 }
22866 
22867 /* Parse a brace-enclosed initializer list.
22868 
22869    braced-init-list:
22870      { initializer-list , [opt] }
22871      { designated-initializer-list , [opt] }
22872      { }
22873 
22874    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
22875    the elements of the initializer-list (or NULL, if the last
22876    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
22877    NULL_TREE.  There is no way to detect whether or not the optional
22878    trailing `,' was provided.  NON_CONSTANT_P is as for
22879    cp_parser_initializer.  */
22880 
22881 static cp_expr
cp_parser_braced_list(cp_parser * parser,bool * non_constant_p)22882 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22883 {
22884   tree initializer;
22885   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22886 
22887   /* Consume the `{' token.  */
22888   matching_braces braces;
22889   braces.require_open (parser);
22890   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
22891   initializer = make_node (CONSTRUCTOR);
22892   /* If it's not a `}', then there is a non-trivial initializer.  */
22893   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22894     {
22895       bool designated;
22896       /* Parse the initializer list.  */
22897       CONSTRUCTOR_ELTS (initializer)
22898 	= cp_parser_initializer_list (parser, non_constant_p, &designated);
22899       CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
22900       /* A trailing `,' token is allowed.  */
22901       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22902 	cp_lexer_consume_token (parser->lexer);
22903     }
22904   else
22905     *non_constant_p = false;
22906   /* Now, there should be a trailing `}'.  */
22907   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22908   braces.require_close (parser);
22909   TREE_TYPE (initializer) = init_list_type_node;
22910 
22911   cp_expr result (initializer);
22912   /* Build a location of the form:
22913        { ... }
22914        ^~~~~~~
22915      with caret==start at the open brace, finish at the close brace.  */
22916   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22917   result.set_location (combined_loc);
22918   return result;
22919 }
22920 
22921 /* Consume tokens up to, and including, the next non-nested closing `]'.
22922    Returns true iff we found a closing `]'.  */
22923 
22924 static bool
cp_parser_skip_to_closing_square_bracket(cp_parser * parser)22925 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22926 {
22927   unsigned square_depth = 0;
22928 
22929   while (true)
22930     {
22931       cp_token * token = cp_lexer_peek_token (parser->lexer);
22932 
22933       switch (token->type)
22934 	{
22935 	case CPP_PRAGMA_EOL:
22936 	  if (!parser->lexer->in_pragma)
22937 	    break;
22938 	  /* FALLTHRU */
22939 	case CPP_EOF:
22940 	  /* If we've run out of tokens, then there is no closing `]'.  */
22941 	  return false;
22942 
22943         case CPP_OPEN_SQUARE:
22944           ++square_depth;
22945           break;
22946 
22947         case CPP_CLOSE_SQUARE:
22948 	  if (!square_depth--)
22949 	    {
22950 	      cp_lexer_consume_token (parser->lexer);
22951 	      return true;
22952 	    }
22953 	  break;
22954 
22955 	default:
22956 	  break;
22957 	}
22958 
22959       /* Consume the token.  */
22960       cp_lexer_consume_token (parser->lexer);
22961     }
22962 }
22963 
22964 /* Return true if we are looking at an array-designator, false otherwise.  */
22965 
22966 static bool
cp_parser_array_designator_p(cp_parser * parser)22967 cp_parser_array_designator_p (cp_parser *parser)
22968 {
22969   /* Consume the `['.  */
22970   cp_lexer_consume_token (parser->lexer);
22971 
22972   cp_lexer_save_tokens (parser->lexer);
22973 
22974   /* Skip tokens until the next token is a closing square bracket.
22975      If we find the closing `]', and the next token is a `=', then
22976      we are looking at an array designator.  */
22977   bool array_designator_p
22978     = (cp_parser_skip_to_closing_square_bracket (parser)
22979        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22980 
22981   /* Roll back the tokens we skipped.  */
22982   cp_lexer_rollback_tokens (parser->lexer);
22983 
22984   return array_designator_p;
22985 }
22986 
22987 /* Parse an initializer-list.
22988 
22989    initializer-list:
22990      initializer-clause ... [opt]
22991      initializer-list , initializer-clause ... [opt]
22992 
22993    C++2A Extension:
22994 
22995    designated-initializer-list:
22996      designated-initializer-clause
22997      designated-initializer-list , designated-initializer-clause
22998 
22999    designated-initializer-clause:
23000      designator brace-or-equal-initializer
23001 
23002    designator:
23003      . identifier
23004 
23005    GNU Extension:
23006 
23007    initializer-list:
23008      designation initializer-clause ...[opt]
23009      initializer-list , designation initializer-clause ...[opt]
23010 
23011    designation:
23012      . identifier =
23013      identifier :
23014      [ constant-expression ] =
23015 
23016    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
23017    for the initializer.  If the INDEX of the elt is non-NULL, it is the
23018    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
23019    as for cp_parser_initializer.  Set *DESIGNATED to a boolean whether there
23020    are any designators.  */
23021 
23022 static vec<constructor_elt, va_gc> *
cp_parser_initializer_list(cp_parser * parser,bool * non_constant_p,bool * designated)23023 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
23024 			    bool *designated)
23025 {
23026   vec<constructor_elt, va_gc> *v = NULL;
23027   bool first_p = true;
23028   tree first_designator = NULL_TREE;
23029 
23030   /* Assume all of the expressions are constant.  */
23031   *non_constant_p = false;
23032 
23033   /* Parse the rest of the list.  */
23034   while (true)
23035     {
23036       cp_token *token;
23037       tree designator;
23038       tree initializer;
23039       bool clause_non_constant_p;
23040       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23041 
23042       /* Handle the C++2A syntax, '. id ='.  */
23043       if ((cxx_dialect >= cxx2a
23044 	   || cp_parser_allow_gnu_extensions_p (parser))
23045 	  && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
23046 	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
23047 	  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
23048 	      || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
23049 		  == CPP_OPEN_BRACE)))
23050 	{
23051 	  if (cxx_dialect < cxx2a)
23052 	    pedwarn (loc, OPT_Wpedantic,
23053 		     "C++ designated initializers only available with "
23054 		     "%<-std=c++2a%> or %<-std=gnu++2a%>");
23055 	  /* Consume the `.'.  */
23056 	  cp_lexer_consume_token (parser->lexer);
23057 	  /* Consume the identifier.  */
23058 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
23059 	  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23060 	    /* Consume the `='.  */
23061 	    cp_lexer_consume_token (parser->lexer);
23062 	}
23063       /* Also, if the next token is an identifier and the following one is a
23064 	 colon, we are looking at the GNU designated-initializer
23065 	 syntax.  */
23066       else if (cp_parser_allow_gnu_extensions_p (parser)
23067 	       && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
23068 	       && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23069 		   == CPP_COLON))
23070 	{
23071 	  /* Warn the user that they are using an extension.  */
23072 	  pedwarn (loc, OPT_Wpedantic,
23073 		   "ISO C++ does not allow GNU designated initializers");
23074 	  /* Consume the identifier.  */
23075 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
23076 	  /* Consume the `:'.  */
23077 	  cp_lexer_consume_token (parser->lexer);
23078 	}
23079       /* Also handle C99 array designators, '[ const ] ='.  */
23080       else if (cp_parser_allow_gnu_extensions_p (parser)
23081 	       && !c_dialect_objc ()
23082 	       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23083 	{
23084 	  /* In C++11, [ could start a lambda-introducer.  */
23085 	  bool non_const = false;
23086 
23087 	  cp_parser_parse_tentatively (parser);
23088 
23089 	  if (!cp_parser_array_designator_p (parser))
23090 	    {
23091 	      cp_parser_simulate_error (parser);
23092 	      designator = NULL_TREE;
23093 	    }
23094 	  else
23095 	    {
23096 	      designator = cp_parser_constant_expression (parser, true,
23097 							  &non_const);
23098 	      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23099 	      cp_parser_require (parser, CPP_EQ, RT_EQ);
23100 	    }
23101 
23102 	  if (!cp_parser_parse_definitely (parser))
23103 	    designator = NULL_TREE;
23104 	  else if (non_const
23105 		   && (!require_potential_rvalue_constant_expression
23106 		       (designator)))
23107 	    designator = NULL_TREE;
23108 	  if (designator)
23109 	    /* Warn the user that they are using an extension.  */
23110 	    pedwarn (loc, OPT_Wpedantic,
23111 		     "ISO C++ does not allow C99 designated initializers");
23112 	}
23113       else
23114 	designator = NULL_TREE;
23115 
23116       if (first_p)
23117 	{
23118 	  first_designator = designator;
23119 	  first_p = false;
23120 	}
23121       else if (cxx_dialect >= cxx2a
23122 	       && first_designator != error_mark_node
23123 	       && (!first_designator != !designator))
23124 	{
23125 	  error_at (loc, "either all initializer clauses should be designated "
23126 			 "or none of them should be");
23127 	  first_designator = error_mark_node;
23128 	}
23129       else if (cxx_dialect < cxx2a && !first_designator)
23130 	first_designator = designator;
23131 
23132       /* Parse the initializer.  */
23133       initializer = cp_parser_initializer_clause (parser,
23134 						  &clause_non_constant_p);
23135       /* If any clause is non-constant, so is the entire initializer.  */
23136       if (clause_non_constant_p)
23137 	*non_constant_p = true;
23138 
23139       /* If we have an ellipsis, this is an initializer pack
23140 	 expansion.  */
23141       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23142         {
23143 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23144 
23145           /* Consume the `...'.  */
23146           cp_lexer_consume_token (parser->lexer);
23147 
23148 	  if (designator && cxx_dialect >= cxx2a)
23149 	    error_at (loc,
23150 		      "%<...%> not allowed in designated initializer list");
23151 
23152 	  /* Turn the initializer into an initializer expansion.  */
23153 	  initializer = make_pack_expansion (initializer);
23154         }
23155 
23156       /* Add it to the vector.  */
23157       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23158 
23159       /* If the next token is not a comma, we have reached the end of
23160 	 the list.  */
23161       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23162 	break;
23163 
23164       /* Peek at the next token.  */
23165       token = cp_lexer_peek_nth_token (parser->lexer, 2);
23166       /* If the next token is a `}', then we're still done.  An
23167 	 initializer-clause can have a trailing `,' after the
23168 	 initializer-list and before the closing `}'.  */
23169       if (token->type == CPP_CLOSE_BRACE)
23170 	break;
23171 
23172       /* Consume the `,' token.  */
23173       cp_lexer_consume_token (parser->lexer);
23174     }
23175 
23176   /* The same identifier shall not appear in multiple designators
23177      of a designated-initializer-list.  */
23178   if (first_designator)
23179     {
23180       unsigned int i;
23181       tree designator, val;
23182       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23183 	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23184 	  {
23185 	    if (IDENTIFIER_MARKED (designator))
23186 	      {
23187 		error_at (cp_expr_loc_or_loc (val, input_location),
23188 			  "%<.%s%> designator used multiple times in "
23189 			  "the same initializer list",
23190 			  IDENTIFIER_POINTER (designator));
23191 		(*v)[i].index = error_mark_node;
23192 	      }
23193 	    else
23194 	      IDENTIFIER_MARKED (designator) = 1;
23195 	  }
23196       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23197 	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23198 	  IDENTIFIER_MARKED (designator) = 0;
23199     }
23200 
23201   *designated = first_designator != NULL_TREE;
23202   return v;
23203 }
23204 
23205 /* Classes [gram.class] */
23206 
23207 /* Parse a class-name.
23208 
23209    class-name:
23210      identifier
23211      template-id
23212 
23213    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23214    to indicate that names looked up in dependent types should be
23215    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
23216    keyword has been used to indicate that the name that appears next
23217    is a template.  TAG_TYPE indicates the explicit tag given before
23218    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
23219    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
23220    is the class being defined in a class-head.  If ENUM_OK is TRUE,
23221    enum-names are also accepted.
23222 
23223    Returns the TYPE_DECL representing the class.  */
23224 
23225 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)23226 cp_parser_class_name (cp_parser *parser,
23227 		      bool typename_keyword_p,
23228 		      bool template_keyword_p,
23229 		      enum tag_types tag_type,
23230 		      bool check_dependency_p,
23231 		      bool class_head_p,
23232 		      bool is_declaration,
23233 		      bool enum_ok)
23234 {
23235   tree decl;
23236   tree scope;
23237   bool typename_p;
23238   cp_token *token;
23239   tree identifier = NULL_TREE;
23240 
23241   /* All class-names start with an identifier.  */
23242   token = cp_lexer_peek_token (parser->lexer);
23243   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23244     {
23245       cp_parser_error (parser, "expected class-name");
23246       return error_mark_node;
23247     }
23248 
23249   /* PARSER->SCOPE can be cleared when parsing the template-arguments
23250      to a template-id, so we save it here.  */
23251   scope = parser->scope;
23252   if (scope == error_mark_node)
23253     return error_mark_node;
23254 
23255   /* Any name names a type if we're following the `typename' keyword
23256      in a qualified name where the enclosing scope is type-dependent.  */
23257   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23258 		&& dependent_type_p (scope));
23259   /* Handle the common case (an identifier, but not a template-id)
23260      efficiently.  */
23261   if (token->type == CPP_NAME
23262       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23263     {
23264       cp_token *identifier_token;
23265       bool ambiguous_p;
23266 
23267       /* Look for the identifier.  */
23268       identifier_token = cp_lexer_peek_token (parser->lexer);
23269       ambiguous_p = identifier_token->error_reported;
23270       identifier = cp_parser_identifier (parser);
23271       /* If the next token isn't an identifier, we are certainly not
23272 	 looking at a class-name.  */
23273       if (identifier == error_mark_node)
23274 	decl = error_mark_node;
23275       /* If we know this is a type-name, there's no need to look it
23276 	 up.  */
23277       else if (typename_p)
23278 	decl = identifier;
23279       else
23280 	{
23281 	  tree ambiguous_decls;
23282 	  /* If we already know that this lookup is ambiguous, then
23283 	     we've already issued an error message; there's no reason
23284 	     to check again.  */
23285 	  if (ambiguous_p)
23286 	    {
23287 	      cp_parser_simulate_error (parser);
23288 	      return error_mark_node;
23289 	    }
23290 	  /* If the next token is a `::', then the name must be a type
23291 	     name.
23292 
23293 	     [basic.lookup.qual]
23294 
23295 	     During the lookup for a name preceding the :: scope
23296 	     resolution operator, object, function, and enumerator
23297 	     names are ignored.  */
23298 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23299 	    tag_type = scope_type;
23300 	  /* Look up the name.  */
23301 	  decl = cp_parser_lookup_name (parser, identifier,
23302 					tag_type,
23303 					/*is_template=*/false,
23304 					/*is_namespace=*/false,
23305 					check_dependency_p,
23306 					&ambiguous_decls,
23307 					identifier_token->location);
23308 	  if (ambiguous_decls)
23309 	    {
23310 	      if (cp_parser_parsing_tentatively (parser))
23311 		cp_parser_simulate_error (parser);
23312 	      return error_mark_node;
23313 	    }
23314 	}
23315     }
23316   else
23317     {
23318       /* Try a template-id.  */
23319       decl = cp_parser_template_id (parser, template_keyword_p,
23320 				    check_dependency_p,
23321 				    tag_type,
23322 				    is_declaration);
23323       if (decl == error_mark_node)
23324 	return error_mark_node;
23325     }
23326 
23327   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23328 
23329   /* If this is a typename, create a TYPENAME_TYPE.  */
23330   if (typename_p
23331       && decl != error_mark_node
23332       && !is_overloaded_fn (decl))
23333     {
23334       decl = make_typename_type (scope, decl, typename_type,
23335 				 /*complain=*/tf_error);
23336       if (decl != error_mark_node)
23337 	decl = TYPE_NAME (decl);
23338     }
23339 
23340   decl = strip_using_decl (decl);
23341 
23342   /* Check to see that it is really the name of a class.  */
23343   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23344       && identifier_p (TREE_OPERAND (decl, 0))
23345       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23346     /* Situations like this:
23347 
23348 	 template <typename T> struct A {
23349 	   typename T::template X<int>::I i;
23350 	 };
23351 
23352        are problematic.  Is `T::template X<int>' a class-name?  The
23353        standard does not seem to be definitive, but there is no other
23354        valid interpretation of the following `::'.  Therefore, those
23355        names are considered class-names.  */
23356     {
23357       decl = make_typename_type (scope, decl, tag_type, tf_error);
23358       if (decl != error_mark_node)
23359 	decl = TYPE_NAME (decl);
23360     }
23361   else if (TREE_CODE (decl) != TYPE_DECL
23362 	   || TREE_TYPE (decl) == error_mark_node
23363 	   || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23364 		|| (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23365 	   /* In Objective-C 2.0, a classname followed by '.' starts a
23366 	      dot-syntax expression, and it's not a type-name.  */
23367 	   || (c_dialect_objc ()
23368 	       && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23369 	       && objc_is_class_name (decl)))
23370     decl = error_mark_node;
23371 
23372   if (decl == error_mark_node)
23373     cp_parser_error (parser, "expected class-name");
23374   else if (identifier && !parser->scope)
23375     maybe_note_name_used_in_class (identifier, decl);
23376 
23377   return decl;
23378 }
23379 
23380 /* Parse a class-specifier.
23381 
23382    class-specifier:
23383      class-head { member-specification [opt] }
23384 
23385    Returns the TREE_TYPE representing the class.  */
23386 
23387 static tree
cp_parser_class_specifier_1(cp_parser * parser)23388 cp_parser_class_specifier_1 (cp_parser* parser)
23389 {
23390   tree type;
23391   tree attributes = NULL_TREE;
23392   bool nested_name_specifier_p;
23393   unsigned saved_num_template_parameter_lists;
23394   bool saved_in_function_body;
23395   unsigned char in_statement;
23396   bool in_switch_statement_p;
23397   bool saved_in_unbraced_linkage_specification_p;
23398   tree old_scope = NULL_TREE;
23399   tree scope = NULL_TREE;
23400   cp_token *closing_brace;
23401 
23402   push_deferring_access_checks (dk_no_deferred);
23403 
23404   /* Parse the class-head.  */
23405   type = cp_parser_class_head (parser,
23406 			       &nested_name_specifier_p);
23407   /* If the class-head was a semantic disaster, skip the entire body
23408      of the class.  */
23409   if (!type)
23410     {
23411       cp_parser_skip_to_end_of_block_or_statement (parser);
23412       pop_deferring_access_checks ();
23413       return error_mark_node;
23414     }
23415 
23416   /* Look for the `{'.  */
23417   matching_braces braces;
23418   if (!braces.require_open (parser))
23419     {
23420       pop_deferring_access_checks ();
23421       return error_mark_node;
23422     }
23423 
23424   cp_ensure_no_omp_declare_simd (parser);
23425   cp_ensure_no_oacc_routine (parser);
23426 
23427   /* Issue an error message if type-definitions are forbidden here.  */
23428   bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23429   /* Remember that we are defining one more class.  */
23430   ++parser->num_classes_being_defined;
23431   /* Inside the class, surrounding template-parameter-lists do not
23432      apply.  */
23433   saved_num_template_parameter_lists
23434     = parser->num_template_parameter_lists;
23435   parser->num_template_parameter_lists = 0;
23436   /* We are not in a function body.  */
23437   saved_in_function_body = parser->in_function_body;
23438   parser->in_function_body = false;
23439   /* Or in a loop.  */
23440   in_statement = parser->in_statement;
23441   parser->in_statement = 0;
23442   /* Or in a switch.  */
23443   in_switch_statement_p = parser->in_switch_statement_p;
23444   parser->in_switch_statement_p = false;
23445   /* We are not immediately inside an extern "lang" block.  */
23446   saved_in_unbraced_linkage_specification_p
23447     = parser->in_unbraced_linkage_specification_p;
23448   parser->in_unbraced_linkage_specification_p = false;
23449 
23450   // Associate constraints with the type.
23451   if (flag_concepts)
23452     type = associate_classtype_constraints (type);
23453 
23454   /* Start the class.  */
23455   if (nested_name_specifier_p)
23456     {
23457       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23458       old_scope = push_inner_scope (scope);
23459     }
23460   type = begin_class_definition (type);
23461 
23462   if (type == error_mark_node)
23463     /* If the type is erroneous, skip the entire body of the class.  */
23464     cp_parser_skip_to_closing_brace (parser);
23465   else
23466     /* Parse the member-specification.  */
23467     cp_parser_member_specification_opt (parser);
23468 
23469   /* Look for the trailing `}'.  */
23470   closing_brace = braces.require_close (parser);
23471   /* Look for trailing attributes to apply to this class.  */
23472   if (cp_parser_allow_gnu_extensions_p (parser))
23473     attributes = cp_parser_gnu_attributes_opt (parser);
23474   if (type != error_mark_node)
23475     type = finish_struct (type, attributes);
23476   if (nested_name_specifier_p)
23477     pop_inner_scope (old_scope, scope);
23478 
23479   /* We've finished a type definition.  Check for the common syntax
23480      error of forgetting a semicolon after the definition.  We need to
23481      be careful, as we can't just check for not-a-semicolon and be done
23482      with it; the user might have typed:
23483 
23484      class X { } c = ...;
23485      class X { } *p = ...;
23486 
23487      and so forth.  Instead, enumerate all the possible tokens that
23488      might follow this production; if we don't see one of them, then
23489      complain and silently insert the semicolon.  */
23490   {
23491     cp_token *token = cp_lexer_peek_token (parser->lexer);
23492     bool want_semicolon = true;
23493 
23494     if (cp_next_tokens_can_be_std_attribute_p (parser))
23495       /* Don't try to parse c++11 attributes here.  As per the
23496 	 grammar, that should be a task for
23497 	 cp_parser_decl_specifier_seq.  */
23498       want_semicolon = false;
23499 
23500     switch (token->type)
23501       {
23502       case CPP_NAME:
23503       case CPP_SEMICOLON:
23504       case CPP_MULT:
23505       case CPP_AND:
23506       case CPP_OPEN_PAREN:
23507       case CPP_CLOSE_PAREN:
23508       case CPP_COMMA:
23509         want_semicolon = false;
23510         break;
23511 
23512         /* While it's legal for type qualifiers and storage class
23513            specifiers to follow type definitions in the grammar, only
23514            compiler testsuites contain code like that.  Assume that if
23515            we see such code, then what we're really seeing is a case
23516            like:
23517 
23518            class X { }
23519            const <type> var = ...;
23520 
23521            or
23522 
23523            class Y { }
23524            static <type> func (...) ...
23525 
23526            i.e. the qualifier or specifier applies to the next
23527            declaration.  To do so, however, we need to look ahead one
23528            more token to see if *that* token is a type specifier.
23529 
23530 	   This code could be improved to handle:
23531 
23532 	   class Z { }
23533 	   static const <type> var = ...;  */
23534       case CPP_KEYWORD:
23535 	if (keyword_is_decl_specifier (token->keyword))
23536 	  {
23537 	    cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23538 
23539 	    /* Handling user-defined types here would be nice, but very
23540 	       tricky.  */
23541 	    want_semicolon
23542 	      = (lookahead->type == CPP_KEYWORD
23543 		 && keyword_begins_type_specifier (lookahead->keyword));
23544 	  }
23545 	break;
23546       default:
23547 	break;
23548       }
23549 
23550     /* If we don't have a type, then something is very wrong and we
23551        shouldn't try to do anything clever.  Likewise for not seeing the
23552        closing brace.  */
23553     if (closing_brace && TYPE_P (type) && want_semicolon)
23554       {
23555 	/* Locate the closing brace.  */
23556 	cp_token_position prev
23557 	  = cp_lexer_previous_token_position (parser->lexer);
23558 	cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23559 	location_t loc = prev_token->location;
23560 
23561 	/* We want to suggest insertion of a ';' immediately *after* the
23562 	   closing brace, so, if we can, offset the location by 1 column.  */
23563 	location_t next_loc = loc;
23564 	if (!linemap_location_from_macro_expansion_p (line_table, loc))
23565 	  next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23566 
23567 	rich_location richloc (line_table, next_loc);
23568 
23569 	/* If we successfully offset the location, suggest the fix-it.  */
23570 	if (next_loc != loc)
23571 	  richloc.add_fixit_insert_before (next_loc, ";");
23572 
23573 	if (CLASSTYPE_DECLARED_CLASS (type))
23574 	  error_at (&richloc,
23575 		    "expected %<;%> after class definition");
23576 	else if (TREE_CODE (type) == RECORD_TYPE)
23577 	  error_at (&richloc,
23578 		    "expected %<;%> after struct definition");
23579 	else if (TREE_CODE (type) == UNION_TYPE)
23580 	  error_at (&richloc,
23581 		    "expected %<;%> after union definition");
23582 	else
23583 	  gcc_unreachable ();
23584 
23585 	/* Unget one token and smash it to look as though we encountered
23586 	   a semicolon in the input stream.  */
23587 	cp_lexer_set_token_position (parser->lexer, prev);
23588 	token = cp_lexer_peek_token (parser->lexer);
23589 	token->type = CPP_SEMICOLON;
23590 	token->keyword = RID_MAX;
23591       }
23592   }
23593 
23594   /* If this class is not itself within the scope of another class,
23595      then we need to parse the bodies of all of the queued function
23596      definitions.  Note that the queued functions defined in a class
23597      are not always processed immediately following the
23598      class-specifier for that class.  Consider:
23599 
23600        struct A {
23601 	 struct B { void f() { sizeof (A); } };
23602        };
23603 
23604      If `f' were processed before the processing of `A' were
23605      completed, there would be no way to compute the size of `A'.
23606      Note that the nesting we are interested in here is lexical --
23607      not the semantic nesting given by TYPE_CONTEXT.  In particular,
23608      for:
23609 
23610        struct A { struct B; };
23611        struct A::B { void f() { } };
23612 
23613      there is no need to delay the parsing of `A::B::f'.  */
23614   if (--parser->num_classes_being_defined == 0)
23615     {
23616       tree decl;
23617       tree class_type = NULL_TREE;
23618       tree pushed_scope = NULL_TREE;
23619       unsigned ix;
23620       cp_default_arg_entry *e;
23621       tree save_ccp, save_ccr;
23622 
23623       if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23624 	{
23625 	  /* Skip default arguments, NSDMIs, etc, in order to improve
23626 	     error recovery (c++/71169, c++/71832).  */
23627 	  vec_safe_truncate (unparsed_funs_with_default_args, 0);
23628 	  vec_safe_truncate (unparsed_nsdmis, 0);
23629 	  vec_safe_truncate (unparsed_classes, 0);
23630 	  vec_safe_truncate (unparsed_funs_with_definitions, 0);
23631 	}
23632 
23633       /* In a first pass, parse default arguments to the functions.
23634 	 Then, in a second pass, parse the bodies of the functions.
23635 	 This two-phased approach handles cases like:
23636 
23637 	    struct S {
23638 	      void f() { g(); }
23639 	      void g(int i = 3);
23640 	    };
23641 
23642 	 */
23643       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23644 	{
23645 	  decl = e->decl;
23646 	  /* If there are default arguments that have not yet been processed,
23647 	     take care of them now.  */
23648 	  if (class_type != e->class_type)
23649 	    {
23650 	      if (pushed_scope)
23651 		pop_scope (pushed_scope);
23652 	      class_type = e->class_type;
23653 	      pushed_scope = push_scope (class_type);
23654 	    }
23655 	  /* Make sure that any template parameters are in scope.  */
23656 	  maybe_begin_member_template_processing (decl);
23657 	  /* Parse the default argument expressions.  */
23658 	  cp_parser_late_parsing_default_args (parser, decl);
23659 	  /* Remove any template parameters from the symbol table.  */
23660 	  maybe_end_member_template_processing ();
23661 	}
23662       vec_safe_truncate (unparsed_funs_with_default_args, 0);
23663       /* Now parse any NSDMIs.  */
23664       save_ccp = current_class_ptr;
23665       save_ccr = current_class_ref;
23666       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23667 	{
23668 	  if (class_type != DECL_CONTEXT (decl))
23669 	    {
23670 	      if (pushed_scope)
23671 		pop_scope (pushed_scope);
23672 	      class_type = DECL_CONTEXT (decl);
23673 	      pushed_scope = push_scope (class_type);
23674 	    }
23675 	  inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23676 	  cp_parser_late_parsing_nsdmi (parser, decl);
23677 	}
23678       vec_safe_truncate (unparsed_nsdmis, 0);
23679       current_class_ptr = save_ccp;
23680       current_class_ref = save_ccr;
23681       if (pushed_scope)
23682 	pop_scope (pushed_scope);
23683 
23684       /* Now do some post-NSDMI bookkeeping.  */
23685       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23686 	after_nsdmi_defaulted_late_checks (class_type);
23687       vec_safe_truncate (unparsed_classes, 0);
23688       after_nsdmi_defaulted_late_checks (type);
23689 
23690       /* Now parse the body of the functions.  */
23691       if (flag_openmp)
23692 	{
23693 	  /* OpenMP UDRs need to be parsed before all other functions.  */
23694 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23695 	    if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23696 	      cp_parser_late_parsing_for_member (parser, decl);
23697 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23698 	    if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23699 	      cp_parser_late_parsing_for_member (parser, decl);
23700 	}
23701       else
23702 	FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23703 	  cp_parser_late_parsing_for_member (parser, decl);
23704       vec_safe_truncate (unparsed_funs_with_definitions, 0);
23705     }
23706   else
23707     vec_safe_push (unparsed_classes, type);
23708 
23709   /* Put back any saved access checks.  */
23710   pop_deferring_access_checks ();
23711 
23712   /* Restore saved state.  */
23713   parser->in_switch_statement_p = in_switch_statement_p;
23714   parser->in_statement = in_statement;
23715   parser->in_function_body = saved_in_function_body;
23716   parser->num_template_parameter_lists
23717     = saved_num_template_parameter_lists;
23718   parser->in_unbraced_linkage_specification_p
23719     = saved_in_unbraced_linkage_specification_p;
23720 
23721   return type;
23722 }
23723 
23724 static tree
cp_parser_class_specifier(cp_parser * parser)23725 cp_parser_class_specifier (cp_parser* parser)
23726 {
23727   tree ret;
23728   timevar_push (TV_PARSE_STRUCT);
23729   ret = cp_parser_class_specifier_1 (parser);
23730   timevar_pop (TV_PARSE_STRUCT);
23731   return ret;
23732 }
23733 
23734 /* Parse a class-head.
23735 
23736    class-head:
23737      class-key identifier [opt] base-clause [opt]
23738      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23739      class-key nested-name-specifier [opt] template-id
23740        base-clause [opt]
23741 
23742    class-virt-specifier:
23743      final
23744 
23745    GNU Extensions:
23746      class-key attributes identifier [opt] base-clause [opt]
23747      class-key attributes nested-name-specifier identifier base-clause [opt]
23748      class-key attributes nested-name-specifier [opt] template-id
23749        base-clause [opt]
23750 
23751    Upon return BASES is initialized to the list of base classes (or
23752    NULL, if there are none) in the same form returned by
23753    cp_parser_base_clause.
23754 
23755    Returns the TYPE of the indicated class.  Sets
23756    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23757    involving a nested-name-specifier was used, and FALSE otherwise.
23758 
23759    Returns error_mark_node if this is not a class-head.
23760 
23761    Returns NULL_TREE if the class-head is syntactically valid, but
23762    semantically invalid in a way that means we should skip the entire
23763    body of the class.  */
23764 
23765 static tree
cp_parser_class_head(cp_parser * parser,bool * nested_name_specifier_p)23766 cp_parser_class_head (cp_parser* parser,
23767 		      bool* nested_name_specifier_p)
23768 {
23769   tree nested_name_specifier;
23770   enum tag_types class_key;
23771   tree id = NULL_TREE;
23772   tree type = NULL_TREE;
23773   tree attributes;
23774   tree bases;
23775   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23776   bool template_id_p = false;
23777   bool qualified_p = false;
23778   bool invalid_nested_name_p = false;
23779   bool invalid_explicit_specialization_p = false;
23780   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23781   tree pushed_scope = NULL_TREE;
23782   unsigned num_templates;
23783   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23784   /* Assume no nested-name-specifier will be present.  */
23785   *nested_name_specifier_p = false;
23786   /* Assume no template parameter lists will be used in defining the
23787      type.  */
23788   num_templates = 0;
23789   parser->colon_corrects_to_scope_p = false;
23790 
23791   /* Look for the class-key.  */
23792   class_key = cp_parser_class_key (parser);
23793   if (class_key == none_type)
23794     return error_mark_node;
23795 
23796   location_t class_head_start_location = input_location;
23797 
23798   /* Parse the attributes.  */
23799   attributes = cp_parser_attributes_opt (parser);
23800 
23801   /* If the next token is `::', that is invalid -- but sometimes
23802      people do try to write:
23803 
23804        struct ::S {};
23805 
23806      Handle this gracefully by accepting the extra qualifier, and then
23807      issuing an error about it later if this really is a
23808      class-head.  If it turns out just to be an elaborated type
23809      specifier, remain silent.  */
23810   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23811     qualified_p = true;
23812 
23813   push_deferring_access_checks (dk_no_check);
23814 
23815   /* Determine the name of the class.  Begin by looking for an
23816      optional nested-name-specifier.  */
23817   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23818   nested_name_specifier
23819     = cp_parser_nested_name_specifier_opt (parser,
23820 					   /*typename_keyword_p=*/false,
23821 					   /*check_dependency_p=*/false,
23822 					   /*type_p=*/true,
23823 					   /*is_declaration=*/false);
23824   /* If there was a nested-name-specifier, then there *must* be an
23825      identifier.  */
23826 
23827   cp_token *bad_template_keyword = NULL;
23828 
23829   if (nested_name_specifier)
23830     {
23831       type_start_token = cp_lexer_peek_token (parser->lexer);
23832       /* Although the grammar says `identifier', it really means
23833 	 `class-name' or `template-name'.  You are only allowed to
23834 	 define a class that has already been declared with this
23835 	 syntax.
23836 
23837 	 The proposed resolution for Core Issue 180 says that wherever
23838 	 you see `class T::X' you should treat `X' as a type-name.
23839 
23840 	 It is OK to define an inaccessible class; for example:
23841 
23842 	   class A { class B; };
23843 	   class A::B {};
23844 
23845 	 We do not know if we will see a class-name, or a
23846 	 template-name.  We look for a class-name first, in case the
23847 	 class-name is a template-id; if we looked for the
23848 	 template-name first we would stop after the template-name.  */
23849       cp_parser_parse_tentatively (parser);
23850       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23851 	bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23852       type = cp_parser_class_name (parser,
23853 				   /*typename_keyword_p=*/false,
23854 				   /*template_keyword_p=*/false,
23855 				   class_type,
23856 				   /*check_dependency_p=*/false,
23857 				   /*class_head_p=*/true,
23858 				   /*is_declaration=*/false);
23859       /* If that didn't work, ignore the nested-name-specifier.  */
23860       if (!cp_parser_parse_definitely (parser))
23861 	{
23862 	  invalid_nested_name_p = true;
23863 	  type_start_token = cp_lexer_peek_token (parser->lexer);
23864 	  id = cp_parser_identifier (parser);
23865 	  if (id == error_mark_node)
23866 	    id = NULL_TREE;
23867 	}
23868       /* If we could not find a corresponding TYPE, treat this
23869 	 declaration like an unqualified declaration.  */
23870       if (type == error_mark_node)
23871 	nested_name_specifier = NULL_TREE;
23872       /* Otherwise, count the number of templates used in TYPE and its
23873 	 containing scopes.  */
23874       else
23875 	num_templates = num_template_headers_for_class (TREE_TYPE (type));
23876     }
23877   /* Otherwise, the identifier is optional.  */
23878   else
23879     {
23880       /* We don't know whether what comes next is a template-id,
23881 	 an identifier, or nothing at all.  */
23882       cp_parser_parse_tentatively (parser);
23883       /* Check for a template-id.  */
23884       type_start_token = cp_lexer_peek_token (parser->lexer);
23885       id = cp_parser_template_id (parser,
23886 				  /*template_keyword_p=*/false,
23887 				  /*check_dependency_p=*/true,
23888 				  class_key,
23889 				  /*is_declaration=*/true);
23890       /* If that didn't work, it could still be an identifier.  */
23891       if (!cp_parser_parse_definitely (parser))
23892 	{
23893 	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23894 	    {
23895 	      type_start_token = cp_lexer_peek_token (parser->lexer);
23896 	      id = cp_parser_identifier (parser);
23897 	    }
23898 	  else
23899 	    id = NULL_TREE;
23900 	}
23901       else
23902 	{
23903 	  template_id_p = true;
23904 	  ++num_templates;
23905 	}
23906     }
23907 
23908   pop_deferring_access_checks ();
23909 
23910   if (id)
23911     {
23912       cp_parser_check_for_invalid_template_id (parser, id,
23913 					       class_key,
23914                                                type_start_token->location);
23915     }
23916   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23917 
23918   /* If it's not a `:' or a `{' then we can't really be looking at a
23919      class-head, since a class-head only appears as part of a
23920      class-specifier.  We have to detect this situation before calling
23921      xref_tag, since that has irreversible side-effects.  */
23922   if (!cp_parser_next_token_starts_class_definition_p (parser))
23923     {
23924       cp_parser_error (parser, "expected %<{%> or %<:%>");
23925       type = error_mark_node;
23926       goto out;
23927     }
23928 
23929   /* At this point, we're going ahead with the class-specifier, even
23930      if some other problem occurs.  */
23931   cp_parser_commit_to_tentative_parse (parser);
23932   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23933     {
23934       cp_parser_error (parser,
23935                        "cannot specify %<override%> for a class");
23936       type = error_mark_node;
23937       goto out;
23938     }
23939   /* Issue the error about the overly-qualified name now.  */
23940   if (qualified_p)
23941     {
23942       cp_parser_error (parser,
23943 		       "global qualification of class name is invalid");
23944       type = error_mark_node;
23945       goto out;
23946     }
23947   else if (invalid_nested_name_p)
23948     {
23949       cp_parser_error (parser,
23950 		       "qualified name does not name a class");
23951       type = error_mark_node;
23952       goto out;
23953     }
23954   else if (nested_name_specifier)
23955     {
23956       tree scope;
23957 
23958       if (bad_template_keyword)
23959 	/* [temp.names]: in a qualified-id formed by a class-head-name, the
23960 	   keyword template shall not appear at the top level.  */
23961 	pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23962 		 "keyword %<template%> not allowed in class-head-name");
23963 
23964       /* Reject typedef-names in class heads.  */
23965       if (!DECL_IMPLICIT_TYPEDEF_P (type))
23966 	{
23967 	  error_at (type_start_token->location,
23968 		    "invalid class name in declaration of %qD",
23969 		    type);
23970 	  type = NULL_TREE;
23971 	  goto done;
23972 	}
23973 
23974       /* Figure out in what scope the declaration is being placed.  */
23975       scope = current_scope ();
23976       /* If that scope does not contain the scope in which the
23977 	 class was originally declared, the program is invalid.  */
23978       if (scope && !is_ancestor (scope, nested_name_specifier))
23979 	{
23980 	  if (at_namespace_scope_p ())
23981 	    error_at (type_start_token->location,
23982 		      "declaration of %qD in namespace %qD which does not "
23983 		      "enclose %qD",
23984 		      type, scope, nested_name_specifier);
23985 	  else
23986 	    error_at (type_start_token->location,
23987 		      "declaration of %qD in %qD which does not enclose %qD",
23988 		      type, scope, nested_name_specifier);
23989 	  type = NULL_TREE;
23990 	  goto done;
23991 	}
23992       /* [dcl.meaning]
23993 
23994 	 A declarator-id shall not be qualified except for the
23995 	 definition of a ... nested class outside of its class
23996 	 ... [or] the definition or explicit instantiation of a
23997 	 class member of a namespace outside of its namespace.  */
23998       if (scope == nested_name_specifier)
23999 	{
24000 	  permerror (nested_name_specifier_token_start->location,
24001 		     "extra qualification not allowed");
24002 	  nested_name_specifier = NULL_TREE;
24003 	  num_templates = 0;
24004 	}
24005     }
24006   /* An explicit-specialization must be preceded by "template <>".  If
24007      it is not, try to recover gracefully.  */
24008   if (at_namespace_scope_p ()
24009       && parser->num_template_parameter_lists == 0
24010       && !processing_template_parmlist
24011       && template_id_p)
24012     {
24013       /* Build a location of this form:
24014            struct typename <ARGS>
24015            ^~~~~~~~~~~~~~~~~~~~~~
24016          with caret==start at the start token, and
24017          finishing at the end of the type.  */
24018       location_t reported_loc
24019         = make_location (class_head_start_location,
24020                          class_head_start_location,
24021                          get_finish (type_start_token->location));
24022       rich_location richloc (line_table, reported_loc);
24023       richloc.add_fixit_insert_before (class_head_start_location,
24024                                        "template <> ");
24025       error_at (&richloc,
24026 		"an explicit specialization must be preceded by"
24027 		" %<template <>%>");
24028       invalid_explicit_specialization_p = true;
24029       /* Take the same action that would have been taken by
24030 	 cp_parser_explicit_specialization.  */
24031       ++parser->num_template_parameter_lists;
24032       begin_specialization ();
24033     }
24034   /* There must be no "return" statements between this point and the
24035      end of this function; set "type "to the correct return value and
24036      use "goto done;" to return.  */
24037   /* Make sure that the right number of template parameters were
24038      present.  */
24039   if (!cp_parser_check_template_parameters (parser, num_templates,
24040 					    template_id_p,
24041 					    type_start_token->location,
24042 					    /*declarator=*/NULL))
24043     {
24044       /* If something went wrong, there is no point in even trying to
24045 	 process the class-definition.  */
24046       type = NULL_TREE;
24047       goto done;
24048     }
24049 
24050   /* Look up the type.  */
24051   if (template_id_p)
24052     {
24053       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
24054 	  && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
24055 	      || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
24056 	{
24057 	  error_at (type_start_token->location,
24058 		    "function template %qD redeclared as a class template", id);
24059 	  type = error_mark_node;
24060 	}
24061       else
24062 	{
24063 	  type = TREE_TYPE (id);
24064 	  type = maybe_process_partial_specialization (type);
24065 
24066 	  /* Check the scope while we still know whether or not we had a
24067 	     nested-name-specifier.  */
24068 	  if (type != error_mark_node)
24069 	    check_unqualified_spec_or_inst (type, type_start_token->location);
24070 	}
24071       if (nested_name_specifier)
24072 	pushed_scope = push_scope (nested_name_specifier);
24073     }
24074   else if (nested_name_specifier)
24075     {
24076       tree class_type;
24077 
24078       /* Given:
24079 
24080 	    template <typename T> struct S { struct T };
24081 	    template <typename T> struct S<T>::T { };
24082 
24083 	 we will get a TYPENAME_TYPE when processing the definition of
24084 	 `S::T'.  We need to resolve it to the actual type before we
24085 	 try to define it.  */
24086       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
24087 	{
24088 	  class_type = resolve_typename_type (TREE_TYPE (type),
24089 					      /*only_current_p=*/false);
24090 	  if (TREE_CODE (class_type) != TYPENAME_TYPE)
24091 	    type = TYPE_NAME (class_type);
24092 	  else
24093 	    {
24094 	      cp_parser_error (parser, "could not resolve typename type");
24095 	      type = error_mark_node;
24096 	    }
24097 	}
24098 
24099       if (maybe_process_partial_specialization (TREE_TYPE (type))
24100 	  == error_mark_node)
24101 	{
24102 	  type = NULL_TREE;
24103 	  goto done;
24104 	}
24105 
24106       class_type = current_class_type;
24107       /* Enter the scope indicated by the nested-name-specifier.  */
24108       pushed_scope = push_scope (nested_name_specifier);
24109       /* Get the canonical version of this type.  */
24110       type = TYPE_MAIN_DECL (TREE_TYPE (type));
24111       /* Call push_template_decl if it seems like we should be defining a
24112 	 template either from the template headers or the type we're
24113 	 defining, so that we diagnose both extra and missing headers.  */
24114       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24115 	   || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
24116 	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
24117 	{
24118 	  type = push_template_decl (type);
24119 	  if (type == error_mark_node)
24120 	    {
24121 	      type = NULL_TREE;
24122 	      goto done;
24123 	    }
24124 	}
24125 
24126       type = TREE_TYPE (type);
24127       *nested_name_specifier_p = true;
24128     }
24129   else      /* The name is not a nested name.  */
24130     {
24131       /* If the class was unnamed, create a dummy name.  */
24132       if (!id)
24133 	id = make_anon_name ();
24134       tag_scope tag_scope = (parser->in_type_id_in_expr_p
24135 			     ? ts_within_enclosing_non_class
24136 			     : ts_current);
24137       type = xref_tag (class_key, id, tag_scope,
24138 		       parser->num_template_parameter_lists);
24139     }
24140 
24141   /* Indicate whether this class was declared as a `class' or as a
24142      `struct'.  */
24143   if (TREE_CODE (type) == RECORD_TYPE)
24144     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
24145   cp_parser_check_class_key (class_key, type);
24146 
24147   /* If this type was already complete, and we see another definition,
24148      that's an error.  Likewise if the type is already being defined:
24149      this can happen, eg, when it's defined from within an expression
24150      (c++/84605).  */
24151   if (type != error_mark_node
24152       && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
24153     {
24154       error_at (type_start_token->location, "redefinition of %q#T",
24155 		type);
24156       inform (location_of (type), "previous definition of %q#T",
24157 	      type);
24158       type = NULL_TREE;
24159       goto done;
24160     }
24161   else if (type == error_mark_node)
24162     type = NULL_TREE;
24163 
24164   if (type)
24165     {
24166       /* Apply attributes now, before any use of the class as a template
24167 	 argument in its base list.  */
24168       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24169       fixup_attribute_variants (type);
24170     }
24171 
24172   /* We will have entered the scope containing the class; the names of
24173      base classes should be looked up in that context.  For example:
24174 
24175        struct A { struct B {}; struct C; };
24176        struct A::C : B {};
24177 
24178      is valid.  */
24179 
24180   /* Get the list of base-classes, if there is one.  */
24181   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24182     {
24183       /* PR59482: enter the class scope so that base-specifiers are looked
24184 	 up correctly.  */
24185       if (type)
24186 	pushclass (type);
24187       bases = cp_parser_base_clause (parser);
24188       /* PR59482: get out of the previously pushed class scope so that the
24189 	 subsequent pops pop the right thing.  */
24190       if (type)
24191 	popclass ();
24192     }
24193   else
24194     bases = NULL_TREE;
24195 
24196   /* If we're really defining a class, process the base classes.
24197      If they're invalid, fail.  */
24198   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24199     xref_basetypes (type, bases);
24200 
24201  done:
24202   /* Leave the scope given by the nested-name-specifier.  We will
24203      enter the class scope itself while processing the members.  */
24204   if (pushed_scope)
24205     pop_scope (pushed_scope);
24206 
24207   if (invalid_explicit_specialization_p)
24208     {
24209       end_specialization ();
24210       --parser->num_template_parameter_lists;
24211     }
24212 
24213   if (type)
24214     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24215   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24216     CLASSTYPE_FINAL (type) = 1;
24217  out:
24218   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24219   return type;
24220 }
24221 
24222 /* Parse a class-key.
24223 
24224    class-key:
24225      class
24226      struct
24227      union
24228 
24229    Returns the kind of class-key specified, or none_type to indicate
24230    error.  */
24231 
24232 static enum tag_types
cp_parser_class_key(cp_parser * parser)24233 cp_parser_class_key (cp_parser* parser)
24234 {
24235   cp_token *token;
24236   enum tag_types tag_type;
24237 
24238   /* Look for the class-key.  */
24239   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24240   if (!token)
24241     return none_type;
24242 
24243   /* Check to see if the TOKEN is a class-key.  */
24244   tag_type = cp_parser_token_is_class_key (token);
24245   if (!tag_type)
24246     cp_parser_error (parser, "expected class-key");
24247   return tag_type;
24248 }
24249 
24250 /* Parse a type-parameter-key.
24251 
24252    type-parameter-key:
24253      class
24254      typename
24255  */
24256 
24257 static void
cp_parser_type_parameter_key(cp_parser * parser)24258 cp_parser_type_parameter_key (cp_parser* parser)
24259 {
24260   /* Look for the type-parameter-key.  */
24261   enum tag_types tag_type = none_type;
24262   cp_token *token = cp_lexer_peek_token (parser->lexer);
24263   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24264     {
24265       cp_lexer_consume_token (parser->lexer);
24266       if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24267 	/* typename is not allowed in a template template parameter
24268 	   by the standard until C++17.  */
24269 	pedwarn (token->location, OPT_Wpedantic,
24270 		 "ISO C++ forbids typename key in template template parameter;"
24271 		 " use %<-std=c++17%> or %<-std=gnu++17%>");
24272     }
24273   else
24274     cp_parser_error (parser, "expected %<class%> or %<typename%>");
24275 
24276   return;
24277 }
24278 
24279 /* Parse an (optional) member-specification.
24280 
24281    member-specification:
24282      member-declaration member-specification [opt]
24283      access-specifier : member-specification [opt]  */
24284 
24285 static void
cp_parser_member_specification_opt(cp_parser * parser)24286 cp_parser_member_specification_opt (cp_parser* parser)
24287 {
24288   while (true)
24289     {
24290       cp_token *token;
24291       enum rid keyword;
24292 
24293       /* Peek at the next token.  */
24294       token = cp_lexer_peek_token (parser->lexer);
24295       /* If it's a `}', or EOF then we've seen all the members.  */
24296       if (token->type == CPP_CLOSE_BRACE
24297 	  || token->type == CPP_EOF
24298 	  || token->type == CPP_PRAGMA_EOL)
24299 	break;
24300 
24301       /* See if this token is a keyword.  */
24302       keyword = token->keyword;
24303       switch (keyword)
24304 	{
24305 	case RID_PUBLIC:
24306 	case RID_PROTECTED:
24307 	case RID_PRIVATE:
24308 	  /* Consume the access-specifier.  */
24309 	  cp_lexer_consume_token (parser->lexer);
24310 	  /* Remember which access-specifier is active.  */
24311 	  current_access_specifier = token->u.value;
24312 	  /* Look for the `:'.  */
24313 	  cp_parser_require (parser, CPP_COLON, RT_COLON);
24314 	  break;
24315 
24316 	default:
24317 	  /* Accept #pragmas at class scope.  */
24318 	  if (token->type == CPP_PRAGMA)
24319 	    {
24320 	      cp_parser_pragma (parser, pragma_member, NULL);
24321 	      break;
24322 	    }
24323 
24324 	  /* Otherwise, the next construction must be a
24325 	     member-declaration.  */
24326 	  cp_parser_member_declaration (parser);
24327 	}
24328     }
24329 }
24330 
24331 /* Parse a member-declaration.
24332 
24333    member-declaration:
24334      decl-specifier-seq [opt] member-declarator-list [opt] ;
24335      function-definition ; [opt]
24336      :: [opt] nested-name-specifier template [opt] unqualified-id ;
24337      using-declaration
24338      template-declaration
24339      alias-declaration
24340 
24341    member-declarator-list:
24342      member-declarator
24343      member-declarator-list , member-declarator
24344 
24345    member-declarator:
24346      declarator pure-specifier [opt]
24347      declarator constant-initializer [opt]
24348      identifier [opt] : constant-expression
24349 
24350    GNU Extensions:
24351 
24352    member-declaration:
24353      __extension__ member-declaration
24354 
24355    member-declarator:
24356      declarator attributes [opt] pure-specifier [opt]
24357      declarator attributes [opt] constant-initializer [opt]
24358      identifier [opt] attributes [opt] : constant-expression
24359 
24360    C++0x Extensions:
24361 
24362    member-declaration:
24363      static_assert-declaration  */
24364 
24365 static void
cp_parser_member_declaration(cp_parser * parser)24366 cp_parser_member_declaration (cp_parser* parser)
24367 {
24368   cp_decl_specifier_seq decl_specifiers;
24369   tree prefix_attributes;
24370   tree decl;
24371   int declares_class_or_enum;
24372   bool friend_p;
24373   cp_token *token = NULL;
24374   cp_token *decl_spec_token_start = NULL;
24375   cp_token *initializer_token_start = NULL;
24376   int saved_pedantic;
24377   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24378 
24379   /* Check for the `__extension__' keyword.  */
24380   if (cp_parser_extension_opt (parser, &saved_pedantic))
24381     {
24382       /* Recurse.  */
24383       cp_parser_member_declaration (parser);
24384       /* Restore the old value of the PEDANTIC flag.  */
24385       pedantic = saved_pedantic;
24386 
24387       return;
24388     }
24389 
24390   /* Check for a template-declaration.  */
24391   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24392     {
24393       /* An explicit specialization here is an error condition, and we
24394 	 expect the specialization handler to detect and report this.  */
24395       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24396 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24397 	cp_parser_explicit_specialization (parser);
24398       else
24399 	cp_parser_template_declaration (parser, /*member_p=*/true);
24400 
24401       return;
24402     }
24403   /* Check for a template introduction.  */
24404   else if (cp_parser_template_declaration_after_export (parser, true))
24405     return;
24406 
24407   /* Check for a using-declaration.  */
24408   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24409     {
24410       if (cxx_dialect < cxx11)
24411 	{
24412 	  /* Parse the using-declaration.  */
24413 	  cp_parser_using_declaration (parser,
24414 				       /*access_declaration_p=*/false);
24415 	  return;
24416 	}
24417       else
24418 	{
24419 	  tree decl;
24420 	  bool alias_decl_expected;
24421 	  cp_parser_parse_tentatively (parser);
24422 	  decl = cp_parser_alias_declaration (parser);
24423 	  /* Note that if we actually see the '=' token after the
24424 	     identifier, cp_parser_alias_declaration commits the
24425 	     tentative parse.  In that case, we really expect an
24426 	     alias-declaration.  Otherwise, we expect a using
24427 	     declaration.  */
24428 	  alias_decl_expected =
24429 	    !cp_parser_uncommitted_to_tentative_parse_p (parser);
24430 	  cp_parser_parse_definitely (parser);
24431 
24432 	  if (alias_decl_expected)
24433 	    finish_member_declaration (decl);
24434 	  else
24435 	    cp_parser_using_declaration (parser,
24436 					 /*access_declaration_p=*/false);
24437 	  return;
24438 	}
24439     }
24440 
24441   /* Check for @defs.  */
24442   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24443     {
24444       tree ivar, member;
24445       tree ivar_chains = cp_parser_objc_defs_expression (parser);
24446       ivar = ivar_chains;
24447       while (ivar)
24448 	{
24449 	  member = ivar;
24450 	  ivar = TREE_CHAIN (member);
24451 	  TREE_CHAIN (member) = NULL_TREE;
24452 	  finish_member_declaration (member);
24453 	}
24454       return;
24455     }
24456 
24457   /* If the next token is `static_assert' we have a static assertion.  */
24458   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24459     {
24460       cp_parser_static_assert (parser, /*member_p=*/true);
24461       return;
24462     }
24463 
24464   parser->colon_corrects_to_scope_p = false;
24465 
24466   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24467       goto out;
24468 
24469   /* Parse the decl-specifier-seq.  */
24470   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24471   cp_parser_decl_specifier_seq (parser,
24472 				(CP_PARSER_FLAGS_OPTIONAL
24473 				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24474 				&decl_specifiers,
24475 				&declares_class_or_enum);
24476   /* Check for an invalid type-name.  */
24477   if (!decl_specifiers.any_type_specifiers_p
24478       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24479     goto out;
24480   /* If there is no declarator, then the decl-specifier-seq should
24481      specify a type.  */
24482   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24483     {
24484       /* If there was no decl-specifier-seq, and the next token is a
24485 	 `;', then we have something like:
24486 
24487 	   struct S { ; };
24488 
24489 	 [class.mem]
24490 
24491 	 Each member-declaration shall declare at least one member
24492 	 name of the class.  */
24493       if (!decl_specifiers.any_specifiers_p)
24494 	{
24495 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
24496 	  if (!in_system_header_at (token->location))
24497 	    {
24498 	      gcc_rich_location richloc (token->location);
24499 	      richloc.add_fixit_remove ();
24500 	      pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24501 	    }
24502 	}
24503       else
24504 	{
24505 	  tree type;
24506 
24507 	  /* See if this declaration is a friend.  */
24508 	  friend_p = cp_parser_friend_p (&decl_specifiers);
24509 	  /* If there were decl-specifiers, check to see if there was
24510 	     a class-declaration.  */
24511 	  type = check_tag_decl (&decl_specifiers,
24512 				 /*explicit_type_instantiation_p=*/false);
24513 	  /* Nested classes have already been added to the class, but
24514 	     a `friend' needs to be explicitly registered.  */
24515 	  if (friend_p)
24516 	    {
24517 	      /* If the `friend' keyword was present, the friend must
24518 		 be introduced with a class-key.  */
24519 	       if (!declares_class_or_enum && cxx_dialect < cxx11)
24520 		 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24521 			  "in C++03 a class-key must be used "
24522 			  "when declaring a friend");
24523 	       /* In this case:
24524 
24525 		    template <typename T> struct A {
24526 		      friend struct A<T>::B;
24527 		    };
24528 
24529 		  A<T>::B will be represented by a TYPENAME_TYPE, and
24530 		  therefore not recognized by check_tag_decl.  */
24531 	       if (!type)
24532 		 {
24533 		   type = decl_specifiers.type;
24534 		   if (type && TREE_CODE (type) == TYPE_DECL)
24535 		     type = TREE_TYPE (type);
24536 		 }
24537 	       if (!type || !TYPE_P (type))
24538 		 error_at (decl_spec_token_start->location,
24539 			   "friend declaration does not name a class or "
24540 			   "function");
24541 	       else
24542 		 make_friend_class (current_class_type, type,
24543 				    /*complain=*/true);
24544 	    }
24545 	  /* If there is no TYPE, an error message will already have
24546 	     been issued.  */
24547 	  else if (!type || type == error_mark_node)
24548 	    ;
24549 	  /* An anonymous aggregate has to be handled specially; such
24550 	     a declaration really declares a data member (with a
24551 	     particular type), as opposed to a nested class.  */
24552 	  else if (ANON_AGGR_TYPE_P (type))
24553 	    {
24554 	      /* C++11 9.5/6.  */
24555 	      if (decl_specifiers.storage_class != sc_none)
24556 		error_at (decl_spec_token_start->location,
24557 			  "a storage class on an anonymous aggregate "
24558 			  "in class scope is not allowed");
24559 
24560 	      /* Remove constructors and such from TYPE, now that we
24561 		 know it is an anonymous aggregate.  */
24562 	      fixup_anonymous_aggr (type);
24563 	      /* And make the corresponding data member.  */
24564 	      decl = build_decl (decl_spec_token_start->location,
24565 				 FIELD_DECL, NULL_TREE, type);
24566 	      /* Add it to the class.  */
24567 	      finish_member_declaration (decl);
24568 	    }
24569 	  else
24570 	    cp_parser_check_access_in_redeclaration
24571 					      (TYPE_NAME (type),
24572 					       decl_spec_token_start->location);
24573 	}
24574     }
24575   else
24576     {
24577       bool assume_semicolon = false;
24578 
24579       /* Clear attributes from the decl_specifiers but keep them
24580 	 around as prefix attributes that apply them to the entity
24581 	 being declared.  */
24582       prefix_attributes = decl_specifiers.attributes;
24583       decl_specifiers.attributes = NULL_TREE;
24584 
24585       /* See if these declarations will be friends.  */
24586       friend_p = cp_parser_friend_p (&decl_specifiers);
24587 
24588       /* Keep going until we hit the `;' at the end of the
24589 	 declaration.  */
24590       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24591 	{
24592 	  tree attributes = NULL_TREE;
24593 	  tree first_attribute;
24594 	  tree initializer;
24595 	  bool named_bitfld = false;
24596 
24597 	  /* Peek at the next token.  */
24598 	  token = cp_lexer_peek_token (parser->lexer);
24599 
24600 	  /* The following code wants to know early if it is a bit-field
24601 	     or some other declaration.  Attributes can appear before
24602 	     the `:' token.  Skip over them without consuming any tokens
24603 	     to peek if they are followed by `:'.  */
24604 	  if (cp_next_tokens_can_be_attribute_p (parser)
24605 	      || (token->type == CPP_NAME
24606 		  && cp_nth_tokens_can_be_attribute_p (parser, 2)
24607 		  && (named_bitfld = true)))
24608 	    {
24609 	      size_t n
24610 		= cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24611 	      token = cp_lexer_peek_nth_token (parser->lexer, n);
24612 	    }
24613 
24614 	  /* Check for a bitfield declaration.  */
24615 	  if (token->type == CPP_COLON
24616 	      || (token->type == CPP_NAME
24617 		  && token == cp_lexer_peek_token (parser->lexer)
24618 		  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24619 		  && (named_bitfld = true)))
24620 	    {
24621 	      tree identifier;
24622 	      tree width;
24623 	      tree late_attributes = NULL_TREE;
24624 	      location_t id_location
24625 		= cp_lexer_peek_token (parser->lexer)->location;
24626 
24627 	      if (named_bitfld)
24628 		identifier = cp_parser_identifier (parser);
24629 	      else
24630 		identifier = NULL_TREE;
24631 
24632 	      /* Look for attributes that apply to the bitfield.  */
24633 	      attributes = cp_parser_attributes_opt (parser);
24634 
24635 	      /* Consume the `:' token.  */
24636 	      cp_lexer_consume_token (parser->lexer);
24637 
24638 	      /* Get the width of the bitfield.  */
24639 	      width = cp_parser_constant_expression (parser, false, NULL,
24640 						     cxx_dialect >= cxx11);
24641 
24642 	      /* In C++2A and as extension for C++11 and above we allow
24643 		 default member initializers for bit-fields.  */
24644 	      initializer = NULL_TREE;
24645 	      if (cxx_dialect >= cxx11
24646 		  && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24647 		      || cp_lexer_next_token_is (parser->lexer,
24648 						 CPP_OPEN_BRACE)))
24649 		{
24650 		  location_t loc
24651 		    = cp_lexer_peek_token (parser->lexer)->location;
24652 		  if (cxx_dialect < cxx2a
24653 		      && !in_system_header_at (loc)
24654 		      && identifier != NULL_TREE)
24655 		    pedwarn (loc, 0,
24656 			     "default member initializers for bit-fields "
24657 			     "only available with %<-std=c++2a%> or "
24658 			     "%<-std=gnu++2a%>");
24659 
24660 		  initializer = cp_parser_save_nsdmi (parser);
24661 		  if (identifier == NULL_TREE)
24662 		    {
24663 		      error_at (loc, "default member initializer for "
24664 				     "unnamed bit-field");
24665 		      initializer = NULL_TREE;
24666 		    }
24667 		}
24668 	      else
24669 		{
24670 		  /* Look for attributes that apply to the bitfield after
24671 		     the `:' token and width.  This is where GCC used to
24672 		     parse attributes in the past, pedwarn if there is
24673 		     a std attribute.  */
24674 		  if (cp_next_tokens_can_be_std_attribute_p (parser))
24675 		    pedwarn (input_location, OPT_Wpedantic,
24676 			     "ISO C++ allows bit-field attributes only "
24677 			     "before the %<:%> token");
24678 
24679 		  late_attributes = cp_parser_attributes_opt (parser);
24680 		}
24681 
24682 	      attributes = attr_chainon (attributes, late_attributes);
24683 
24684 	      /* Remember which attributes are prefix attributes and
24685 		 which are not.  */
24686 	      first_attribute = attributes;
24687 	      /* Combine the attributes.  */
24688 	      attributes = attr_chainon (prefix_attributes, attributes);
24689 
24690 	      /* Create the bitfield declaration.  */
24691 	      decl = grokbitfield (identifier
24692 				   ? make_id_declarator (NULL_TREE,
24693 							 identifier,
24694 							 sfk_none,
24695 							 id_location)
24696 				   : NULL,
24697 				   &decl_specifiers,
24698 				   width, initializer,
24699 				   attributes);
24700 	    }
24701 	  else
24702 	    {
24703 	      cp_declarator *declarator;
24704 	      tree asm_specification;
24705 	      int ctor_dtor_or_conv_p;
24706 	      bool static_p = (decl_specifiers.storage_class == sc_static);
24707 
24708 	      /* Parse the declarator.  */
24709 	      declarator
24710 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24711 					CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24712 					&ctor_dtor_or_conv_p,
24713 					/*parenthesized_p=*/NULL,
24714 					/*member_p=*/true,
24715 					friend_p, static_p);
24716 
24717 	      /* If something went wrong parsing the declarator, make sure
24718 		 that we at least consume some tokens.  */
24719 	      if (declarator == cp_error_declarator)
24720 		{
24721 		  /* Skip to the end of the statement.  */
24722 		  cp_parser_skip_to_end_of_statement (parser);
24723 		  /* If the next token is not a semicolon, that is
24724 		     probably because we just skipped over the body of
24725 		     a function.  So, we consume a semicolon if
24726 		     present, but do not issue an error message if it
24727 		     is not present.  */
24728 		  if (cp_lexer_next_token_is (parser->lexer,
24729 					      CPP_SEMICOLON))
24730 		    cp_lexer_consume_token (parser->lexer);
24731 		  goto out;
24732 		}
24733 
24734 	      if (declares_class_or_enum & 2)
24735 		cp_parser_check_for_definition_in_return_type
24736 					    (declarator, decl_specifiers.type,
24737 					     decl_specifiers.locations[ds_type_spec]);
24738 
24739 	      /* Look for an asm-specification.  */
24740 	      asm_specification = cp_parser_asm_specification_opt (parser);
24741 	      /* Look for attributes that apply to the declaration.  */
24742 	      attributes = cp_parser_attributes_opt (parser);
24743 	      /* Remember which attributes are prefix attributes and
24744 		 which are not.  */
24745 	      first_attribute = attributes;
24746 	      /* Combine the attributes.  */
24747 	      attributes = attr_chainon (prefix_attributes, attributes);
24748 
24749 	      /* If it's an `=', then we have a constant-initializer or a
24750 		 pure-specifier.  It is not correct to parse the
24751 		 initializer before registering the member declaration
24752 		 since the member declaration should be in scope while
24753 		 its initializer is processed.  However, the rest of the
24754 		 front end does not yet provide an interface that allows
24755 		 us to handle this correctly.  */
24756 	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24757 		{
24758 		  /* In [class.mem]:
24759 
24760 		     A pure-specifier shall be used only in the declaration of
24761 		     a virtual function.
24762 
24763 		     A member-declarator can contain a constant-initializer
24764 		     only if it declares a static member of integral or
24765 		     enumeration type.
24766 
24767 		     Therefore, if the DECLARATOR is for a function, we look
24768 		     for a pure-specifier; otherwise, we look for a
24769 		     constant-initializer.  When we call `grokfield', it will
24770 		     perform more stringent semantics checks.  */
24771 		  initializer_token_start = cp_lexer_peek_token (parser->lexer);
24772 		  if (function_declarator_p (declarator)
24773 		      || (decl_specifiers.type
24774 			  && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24775 			  && declarator->kind == cdk_id
24776 			  && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24777 			      == FUNCTION_TYPE)))
24778 		    initializer = cp_parser_pure_specifier (parser);
24779 		  else if (decl_specifiers.storage_class != sc_static)
24780 		    initializer = cp_parser_save_nsdmi (parser);
24781 		  else if (cxx_dialect >= cxx11)
24782 		    {
24783 		      bool nonconst;
24784 		      /* Don't require a constant rvalue in C++11, since we
24785 			 might want a reference constant.  We'll enforce
24786 		         constancy later.  */
24787 		      cp_lexer_consume_token (parser->lexer);
24788 		      /* Parse the initializer.  */
24789 		      initializer = cp_parser_initializer_clause (parser,
24790 								  &nonconst);
24791 		    }
24792 		  else
24793 		    /* Parse the initializer.  */
24794 		    initializer = cp_parser_constant_initializer (parser);
24795 		}
24796 	      else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24797 		       && !function_declarator_p (declarator))
24798 		{
24799 		  bool x;
24800 		  if (decl_specifiers.storage_class != sc_static)
24801 		    initializer = cp_parser_save_nsdmi (parser);
24802 		  else
24803 		    initializer = cp_parser_initializer (parser, &x, &x);
24804 		}
24805 	      /* Otherwise, there is no initializer.  */
24806 	      else
24807 		initializer = NULL_TREE;
24808 
24809 	      /* See if we are probably looking at a function
24810 		 definition.  We are certainly not looking at a
24811 		 member-declarator.  Calling `grokfield' has
24812 		 side-effects, so we must not do it unless we are sure
24813 		 that we are looking at a member-declarator.  */
24814 	      if (cp_parser_token_starts_function_definition_p
24815 		  (cp_lexer_peek_token (parser->lexer)))
24816 		{
24817 		  /* The grammar does not allow a pure-specifier to be
24818 		     used when a member function is defined.  (It is
24819 		     possible that this fact is an oversight in the
24820 		     standard, since a pure function may be defined
24821 		     outside of the class-specifier.  */
24822 		  if (initializer && initializer_token_start)
24823 		    error_at (initializer_token_start->location,
24824 			      "pure-specifier on function-definition");
24825 		  decl = cp_parser_save_member_function_body (parser,
24826 							      &decl_specifiers,
24827 							      declarator,
24828 							      attributes);
24829 		  if (parser->fully_implicit_function_template_p)
24830 		    decl = finish_fully_implicit_template (parser, decl);
24831 		  /* If the member was not a friend, declare it here.  */
24832 		  if (!friend_p)
24833 		    finish_member_declaration (decl);
24834 		  /* Peek at the next token.  */
24835 		  token = cp_lexer_peek_token (parser->lexer);
24836 		  /* If the next token is a semicolon, consume it.  */
24837 		  if (token->type == CPP_SEMICOLON)
24838 		    {
24839 		      location_t semicolon_loc
24840 			= cp_lexer_consume_token (parser->lexer)->location;
24841 		      gcc_rich_location richloc (semicolon_loc);
24842 		      richloc.add_fixit_remove ();
24843 		      warning_at (&richloc, OPT_Wextra_semi,
24844 				  "extra %<;%> after in-class "
24845 				  "function definition");
24846 		    }
24847 		  goto out;
24848 		}
24849 	      else
24850 		if (declarator->kind == cdk_function)
24851 		  declarator->id_loc = token->location;
24852 	      /* Create the declaration.  */
24853 	      decl = grokfield (declarator, &decl_specifiers,
24854 				initializer, /*init_const_expr_p=*/true,
24855 				asm_specification, attributes);
24856 	      if (parser->fully_implicit_function_template_p)
24857 		{
24858 		  if (friend_p)
24859 		    finish_fully_implicit_template (parser, 0);
24860 		  else
24861 		    decl = finish_fully_implicit_template (parser, decl);
24862 		}
24863 	    }
24864 
24865 	  cp_finalize_omp_declare_simd (parser, decl);
24866 	  cp_finalize_oacc_routine (parser, decl, false);
24867 
24868 	  /* Reset PREFIX_ATTRIBUTES.  */
24869 	  if (attributes != error_mark_node)
24870 	    {
24871 	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
24872 		attributes = TREE_CHAIN (attributes);
24873 	      if (attributes)
24874 		TREE_CHAIN (attributes) = NULL_TREE;
24875 	    }
24876 
24877 	  /* If there is any qualification still in effect, clear it
24878 	     now; we will be starting fresh with the next declarator.  */
24879 	  parser->scope = NULL_TREE;
24880 	  parser->qualifying_scope = NULL_TREE;
24881 	  parser->object_scope = NULL_TREE;
24882 	  /* If it's a `,', then there are more declarators.  */
24883 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24884 	    {
24885 	      cp_lexer_consume_token (parser->lexer);
24886 	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24887 		{
24888 		  cp_token *token = cp_lexer_previous_token (parser->lexer);
24889 		  gcc_rich_location richloc (token->location);
24890 		  richloc.add_fixit_remove ();
24891 		  error_at (&richloc, "stray %<,%> at end of "
24892 			    "member declaration");
24893 		}
24894 	    }
24895 	  /* If the next token isn't a `;', then we have a parse error.  */
24896 	  else if (cp_lexer_next_token_is_not (parser->lexer,
24897 					       CPP_SEMICOLON))
24898 	    {
24899 	      /* The next token might be a ways away from where the
24900 		 actual semicolon is missing.  Find the previous token
24901 		 and use that for our error position.  */
24902 	      cp_token *token = cp_lexer_previous_token (parser->lexer);
24903 	      gcc_rich_location richloc (token->location);
24904 	      richloc.add_fixit_insert_after (";");
24905 	      error_at (&richloc, "expected %<;%> at end of "
24906 			"member declaration");
24907 
24908 	      /* Assume that the user meant to provide a semicolon.  If
24909 		 we were to cp_parser_skip_to_end_of_statement, we might
24910 		 skip to a semicolon inside a member function definition
24911 		 and issue nonsensical error messages.  */
24912 	      assume_semicolon = true;
24913 	    }
24914 
24915 	  if (decl)
24916 	    {
24917 	      /* Add DECL to the list of members.  */
24918 	      if (!friend_p
24919 		  /* Explicitly include, eg, NSDMIs, for better error
24920 		     recovery (c++/58650).  */
24921 		  || !DECL_DECLARES_FUNCTION_P (decl))
24922 		finish_member_declaration (decl);
24923 
24924 	      if (TREE_CODE (decl) == FUNCTION_DECL)
24925 		cp_parser_save_default_args (parser, decl);
24926 	      else if (TREE_CODE (decl) == FIELD_DECL
24927 		       && DECL_INITIAL (decl))
24928 		/* Add DECL to the queue of NSDMI to be parsed later.  */
24929 		vec_safe_push (unparsed_nsdmis, decl);
24930 	    }
24931 
24932 	  if (assume_semicolon)
24933 	    goto out;
24934 	}
24935     }
24936 
24937   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24938  out:
24939   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24940 }
24941 
24942 /* Parse a pure-specifier.
24943 
24944    pure-specifier:
24945      = 0
24946 
24947    Returns INTEGER_ZERO_NODE if a pure specifier is found.
24948    Otherwise, ERROR_MARK_NODE is returned.  */
24949 
24950 static tree
cp_parser_pure_specifier(cp_parser * parser)24951 cp_parser_pure_specifier (cp_parser* parser)
24952 {
24953   cp_token *token;
24954 
24955   /* Look for the `=' token.  */
24956   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24957     return error_mark_node;
24958   /* Look for the `0' token.  */
24959   token = cp_lexer_peek_token (parser->lexer);
24960 
24961   if (token->type == CPP_EOF
24962       || token->type == CPP_PRAGMA_EOL)
24963     return error_mark_node;
24964 
24965   cp_lexer_consume_token (parser->lexer);
24966 
24967   /* Accept = default or = delete in c++0x mode.  */
24968   if (token->keyword == RID_DEFAULT
24969       || token->keyword == RID_DELETE)
24970     {
24971       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24972       return token->u.value;
24973     }
24974 
24975   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
24976   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24977     {
24978       cp_parser_error (parser,
24979 		       "invalid pure specifier (only %<= 0%> is allowed)");
24980       cp_parser_skip_to_end_of_statement (parser);
24981       return error_mark_node;
24982     }
24983   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24984     {
24985       error_at (token->location, "templates may not be %<virtual%>");
24986       return error_mark_node;
24987     }
24988 
24989   return integer_zero_node;
24990 }
24991 
24992 /* Parse a constant-initializer.
24993 
24994    constant-initializer:
24995      = constant-expression
24996 
24997    Returns a representation of the constant-expression.  */
24998 
24999 static tree
cp_parser_constant_initializer(cp_parser * parser)25000 cp_parser_constant_initializer (cp_parser* parser)
25001 {
25002   /* Look for the `=' token.  */
25003   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25004     return error_mark_node;
25005 
25006   /* It is invalid to write:
25007 
25008        struct S { static const int i = { 7 }; };
25009 
25010      */
25011   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25012     {
25013       cp_parser_error (parser,
25014 		       "a brace-enclosed initializer is not allowed here");
25015       /* Consume the opening brace.  */
25016       matching_braces braces;
25017       braces.consume_open (parser);
25018       /* Skip the initializer.  */
25019       cp_parser_skip_to_closing_brace (parser);
25020       /* Look for the trailing `}'.  */
25021       braces.require_close (parser);
25022 
25023       return error_mark_node;
25024     }
25025 
25026   return cp_parser_constant_expression (parser);
25027 }
25028 
25029 /* Derived classes [gram.class.derived] */
25030 
25031 /* Parse a base-clause.
25032 
25033    base-clause:
25034      : base-specifier-list
25035 
25036    base-specifier-list:
25037      base-specifier ... [opt]
25038      base-specifier-list , base-specifier ... [opt]
25039 
25040    Returns a TREE_LIST representing the base-classes, in the order in
25041    which they were declared.  The representation of each node is as
25042    described by cp_parser_base_specifier.
25043 
25044    In the case that no bases are specified, this function will return
25045    NULL_TREE, not ERROR_MARK_NODE.  */
25046 
25047 static tree
cp_parser_base_clause(cp_parser * parser)25048 cp_parser_base_clause (cp_parser* parser)
25049 {
25050   tree bases = NULL_TREE;
25051 
25052   /* Look for the `:' that begins the list.  */
25053   cp_parser_require (parser, CPP_COLON, RT_COLON);
25054 
25055   /* Scan the base-specifier-list.  */
25056   while (true)
25057     {
25058       cp_token *token;
25059       tree base;
25060       bool pack_expansion_p = false;
25061 
25062       /* Look for the base-specifier.  */
25063       base = cp_parser_base_specifier (parser);
25064       /* Look for the (optional) ellipsis. */
25065       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25066         {
25067           /* Consume the `...'. */
25068           cp_lexer_consume_token (parser->lexer);
25069 
25070           pack_expansion_p = true;
25071         }
25072 
25073       /* Add BASE to the front of the list.  */
25074       if (base && base != error_mark_node)
25075 	{
25076           if (pack_expansion_p)
25077             /* Make this a pack expansion type. */
25078             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
25079 
25080           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
25081             {
25082               TREE_CHAIN (base) = bases;
25083               bases = base;
25084             }
25085 	}
25086       /* Peek at the next token.  */
25087       token = cp_lexer_peek_token (parser->lexer);
25088       /* If it's not a comma, then the list is complete.  */
25089       if (token->type != CPP_COMMA)
25090 	break;
25091       /* Consume the `,'.  */
25092       cp_lexer_consume_token (parser->lexer);
25093     }
25094 
25095   /* PARSER->SCOPE may still be non-NULL at this point, if the last
25096      base class had a qualified name.  However, the next name that
25097      appears is certainly not qualified.  */
25098   parser->scope = NULL_TREE;
25099   parser->qualifying_scope = NULL_TREE;
25100   parser->object_scope = NULL_TREE;
25101 
25102   return nreverse (bases);
25103 }
25104 
25105 /* Parse a base-specifier.
25106 
25107    base-specifier:
25108      :: [opt] nested-name-specifier [opt] class-name
25109      virtual access-specifier [opt] :: [opt] nested-name-specifier
25110        [opt] class-name
25111      access-specifier virtual [opt] :: [opt] nested-name-specifier
25112        [opt] class-name
25113 
25114    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
25115    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25116    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
25117    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
25118 
25119 static tree
cp_parser_base_specifier(cp_parser * parser)25120 cp_parser_base_specifier (cp_parser* parser)
25121 {
25122   cp_token *token;
25123   bool done = false;
25124   bool virtual_p = false;
25125   bool duplicate_virtual_error_issued_p = false;
25126   bool duplicate_access_error_issued_p = false;
25127   bool class_scope_p, template_p;
25128   tree access = access_default_node;
25129   tree type;
25130 
25131   /* Process the optional `virtual' and `access-specifier'.  */
25132   while (!done)
25133     {
25134       /* Peek at the next token.  */
25135       token = cp_lexer_peek_token (parser->lexer);
25136       /* Process `virtual'.  */
25137       switch (token->keyword)
25138 	{
25139 	case RID_VIRTUAL:
25140 	  /* If `virtual' appears more than once, issue an error.  */
25141 	  if (virtual_p && !duplicate_virtual_error_issued_p)
25142 	    {
25143 	      cp_parser_error (parser,
25144 			       "%<virtual%> specified more than once in base-specifier");
25145 	      duplicate_virtual_error_issued_p = true;
25146 	    }
25147 
25148 	  virtual_p = true;
25149 
25150 	  /* Consume the `virtual' token.  */
25151 	  cp_lexer_consume_token (parser->lexer);
25152 
25153 	  break;
25154 
25155 	case RID_PUBLIC:
25156 	case RID_PROTECTED:
25157 	case RID_PRIVATE:
25158 	  /* If more than one access specifier appears, issue an
25159 	     error.  */
25160 	  if (access != access_default_node
25161 	      && !duplicate_access_error_issued_p)
25162 	    {
25163 	      cp_parser_error (parser,
25164 			       "more than one access specifier in base-specifier");
25165 	      duplicate_access_error_issued_p = true;
25166 	    }
25167 
25168 	  access = ridpointers[(int) token->keyword];
25169 
25170 	  /* Consume the access-specifier.  */
25171 	  cp_lexer_consume_token (parser->lexer);
25172 
25173 	  break;
25174 
25175 	default:
25176 	  done = true;
25177 	  break;
25178 	}
25179     }
25180   /* It is not uncommon to see programs mechanically, erroneously, use
25181      the 'typename' keyword to denote (dependent) qualified types
25182      as base classes.  */
25183   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25184     {
25185       token = cp_lexer_peek_token (parser->lexer);
25186       if (!processing_template_decl)
25187 	error_at (token->location,
25188 		  "keyword %<typename%> not allowed outside of templates");
25189       else
25190 	error_at (token->location,
25191 		  "keyword %<typename%> not allowed in this context "
25192 		  "(the base class is implicitly a type)");
25193       cp_lexer_consume_token (parser->lexer);
25194     }
25195 
25196   /* Look for the optional `::' operator.  */
25197   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25198   /* Look for the nested-name-specifier.  The simplest way to
25199      implement:
25200 
25201        [temp.res]
25202 
25203        The keyword `typename' is not permitted in a base-specifier or
25204        mem-initializer; in these contexts a qualified name that
25205        depends on a template-parameter is implicitly assumed to be a
25206        type name.
25207 
25208      is to pretend that we have seen the `typename' keyword at this
25209      point.  */
25210   cp_parser_nested_name_specifier_opt (parser,
25211 				       /*typename_keyword_p=*/true,
25212 				       /*check_dependency_p=*/true,
25213 				       /*type_p=*/true,
25214 				       /*is_declaration=*/true);
25215   /* If the base class is given by a qualified name, assume that names
25216      we see are type names or templates, as appropriate.  */
25217   class_scope_p = (parser->scope && TYPE_P (parser->scope));
25218   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25219 
25220   if (!parser->scope
25221       && cp_lexer_next_token_is_decltype (parser->lexer))
25222     /* DR 950 allows decltype as a base-specifier.  */
25223     type = cp_parser_decltype (parser);
25224   else
25225     {
25226       /* Otherwise, look for the class-name.  */
25227       type = cp_parser_class_name (parser,
25228 				   class_scope_p,
25229 				   template_p,
25230 				   typename_type,
25231 				   /*check_dependency_p=*/true,
25232 				   /*class_head_p=*/false,
25233 				   /*is_declaration=*/true);
25234       type = TREE_TYPE (type);
25235     }
25236 
25237   if (type == error_mark_node)
25238     return error_mark_node;
25239 
25240   return finish_base_specifier (type, access, virtual_p);
25241 }
25242 
25243 /* Exception handling [gram.exception] */
25244 
25245 /* Parse an (optional) noexcept-specification.
25246 
25247    noexcept-specification:
25248      noexcept ( constant-expression ) [opt]
25249 
25250    If no noexcept-specification is present, returns NULL_TREE.
25251    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25252    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25253    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
25254    Otherwise, returns a noexcept specification unless RETURN_COND is true,
25255    in which case a boolean condition is returned instead.  */
25256 
25257 static tree
cp_parser_noexcept_specification_opt(cp_parser * parser,bool require_constexpr,bool * consumed_expr,bool return_cond)25258 cp_parser_noexcept_specification_opt (cp_parser* parser,
25259 				      bool require_constexpr,
25260 				      bool* consumed_expr,
25261 				      bool return_cond)
25262 {
25263   cp_token *token;
25264   const char *saved_message;
25265 
25266   /* Peek at the next token.  */
25267   token = cp_lexer_peek_token (parser->lexer);
25268 
25269   /* Is it a noexcept-specification?  */
25270   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25271     {
25272       tree expr;
25273       cp_lexer_consume_token (parser->lexer);
25274 
25275       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25276 	{
25277 	  matching_parens parens;
25278 	  parens.consume_open (parser);
25279 
25280 	  tree save_ccp = current_class_ptr;
25281 	  tree save_ccr = current_class_ref;
25282 
25283 	  if (current_class_type)
25284 	    inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25285 
25286 	  if (require_constexpr)
25287 	    {
25288 	      /* Types may not be defined in an exception-specification.  */
25289 	      saved_message = parser->type_definition_forbidden_message;
25290 	      parser->type_definition_forbidden_message
25291 	      = G_("types may not be defined in an exception-specification");
25292 
25293 	      bool non_constant_p;
25294 	      expr
25295 		= cp_parser_constant_expression (parser,
25296 						 /*allow_non_constant=*/true,
25297 						 &non_constant_p);
25298 	      if (non_constant_p
25299 		  && !require_potential_rvalue_constant_expression (expr))
25300 		{
25301 		  expr = NULL_TREE;
25302 		  return_cond = true;
25303 		}
25304 
25305 	      /* Restore the saved message.  */
25306 	      parser->type_definition_forbidden_message = saved_message;
25307 	    }
25308 	  else
25309 	    {
25310 	      expr = cp_parser_expression (parser);
25311 	      *consumed_expr = true;
25312 	    }
25313 
25314 	  parens.require_close (parser);
25315 
25316 	  current_class_ptr = save_ccp;
25317 	  current_class_ref = save_ccr;
25318 	}
25319       else
25320 	{
25321 	  expr = boolean_true_node;
25322 	  if (!require_constexpr)
25323 	    *consumed_expr = false;
25324 	}
25325 
25326       /* We cannot build a noexcept-spec right away because this will check
25327 	 that expr is a constexpr.  */
25328       if (!return_cond)
25329 	return build_noexcept_spec (expr, tf_warning_or_error);
25330       else
25331 	return expr;
25332     }
25333   else
25334     return NULL_TREE;
25335 }
25336 
25337 /* Parse an (optional) exception-specification.
25338 
25339    exception-specification:
25340      throw ( type-id-list [opt] )
25341 
25342    Returns a TREE_LIST representing the exception-specification.  The
25343    TREE_VALUE of each node is a type.  */
25344 
25345 static tree
cp_parser_exception_specification_opt(cp_parser * parser)25346 cp_parser_exception_specification_opt (cp_parser* parser)
25347 {
25348   cp_token *token;
25349   tree type_id_list;
25350   const char *saved_message;
25351 
25352   /* Peek at the next token.  */
25353   token = cp_lexer_peek_token (parser->lexer);
25354 
25355   /* Is it a noexcept-specification?  */
25356   type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25357 						       false);
25358   if (type_id_list != NULL_TREE)
25359     return type_id_list;
25360 
25361   /* If it's not `throw', then there's no exception-specification.  */
25362   if (!cp_parser_is_keyword (token, RID_THROW))
25363     return NULL_TREE;
25364 
25365   location_t loc = token->location;
25366 
25367   /* Consume the `throw'.  */
25368   cp_lexer_consume_token (parser->lexer);
25369 
25370   /* Look for the `('.  */
25371   matching_parens parens;
25372   parens.require_open (parser);
25373 
25374   /* Peek at the next token.  */
25375   token = cp_lexer_peek_token (parser->lexer);
25376   /* If it's not a `)', then there is a type-id-list.  */
25377   if (token->type != CPP_CLOSE_PAREN)
25378     {
25379       /* Types may not be defined in an exception-specification.  */
25380       saved_message = parser->type_definition_forbidden_message;
25381       parser->type_definition_forbidden_message
25382 	= G_("types may not be defined in an exception-specification");
25383       /* Parse the type-id-list.  */
25384       type_id_list = cp_parser_type_id_list (parser);
25385       /* Restore the saved message.  */
25386       parser->type_definition_forbidden_message = saved_message;
25387 
25388       if (cxx_dialect >= cxx17)
25389 	{
25390 	  error_at (loc, "ISO C++17 does not allow dynamic exception "
25391 			 "specifications");
25392 	  type_id_list = NULL_TREE;
25393 	}
25394       else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25395 	warning_at (loc, OPT_Wdeprecated,
25396 		    "dynamic exception specifications are deprecated in "
25397 		    "C++11");
25398     }
25399   /* In C++17, throw() is equivalent to noexcept (true).  throw()
25400      is deprecated in C++11 and above as well, but is still widely used,
25401      so don't warn about it yet.  */
25402   else if (cxx_dialect >= cxx17)
25403     type_id_list = noexcept_true_spec;
25404   else
25405     type_id_list = empty_except_spec;
25406 
25407   /* Look for the `)'.  */
25408   parens.require_close (parser);
25409 
25410   return type_id_list;
25411 }
25412 
25413 /* Parse an (optional) type-id-list.
25414 
25415    type-id-list:
25416      type-id ... [opt]
25417      type-id-list , type-id ... [opt]
25418 
25419    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
25420    in the order that the types were presented.  */
25421 
25422 static tree
cp_parser_type_id_list(cp_parser * parser)25423 cp_parser_type_id_list (cp_parser* parser)
25424 {
25425   tree types = NULL_TREE;
25426 
25427   while (true)
25428     {
25429       cp_token *token;
25430       tree type;
25431 
25432       token = cp_lexer_peek_token (parser->lexer);
25433 
25434       /* Get the next type-id.  */
25435       type = cp_parser_type_id (parser);
25436       /* Check for invalid 'auto'.  */
25437       if (flag_concepts && type_uses_auto (type))
25438 	{
25439 	  error_at (token->location,
25440 		    "invalid use of %<auto%> in exception-specification");
25441 	  type = error_mark_node;
25442 	}
25443       /* Parse the optional ellipsis. */
25444       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25445         {
25446           /* Consume the `...'. */
25447           cp_lexer_consume_token (parser->lexer);
25448 
25449           /* Turn the type into a pack expansion expression. */
25450           type = make_pack_expansion (type);
25451         }
25452       /* Add it to the list.  */
25453       types = add_exception_specifier (types, type, /*complain=*/1);
25454       /* Peek at the next token.  */
25455       token = cp_lexer_peek_token (parser->lexer);
25456       /* If it is not a `,', we are done.  */
25457       if (token->type != CPP_COMMA)
25458 	break;
25459       /* Consume the `,'.  */
25460       cp_lexer_consume_token (parser->lexer);
25461     }
25462 
25463   return nreverse (types);
25464 }
25465 
25466 /* Parse a try-block.
25467 
25468    try-block:
25469      try compound-statement handler-seq  */
25470 
25471 static tree
cp_parser_try_block(cp_parser * parser)25472 cp_parser_try_block (cp_parser* parser)
25473 {
25474   tree try_block;
25475 
25476   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25477   if (parser->in_function_body
25478       && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25479       && cxx_dialect < cxx2a)
25480     pedwarn (input_location, 0,
25481 	     "%<try%> in %<constexpr%> function only "
25482 	     "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25483 
25484   try_block = begin_try_block ();
25485   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25486   finish_try_block (try_block);
25487   cp_parser_handler_seq (parser);
25488   finish_handler_sequence (try_block);
25489 
25490   return try_block;
25491 }
25492 
25493 /* Parse a function-try-block.
25494 
25495    function-try-block:
25496      try ctor-initializer [opt] function-body handler-seq  */
25497 
25498 static void
cp_parser_function_try_block(cp_parser * parser)25499 cp_parser_function_try_block (cp_parser* parser)
25500 {
25501   tree compound_stmt;
25502   tree try_block;
25503 
25504   /* Look for the `try' keyword.  */
25505   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25506     return;
25507   /* Let the rest of the front end know where we are.  */
25508   try_block = begin_function_try_block (&compound_stmt);
25509   /* Parse the function-body.  */
25510   cp_parser_ctor_initializer_opt_and_function_body
25511     (parser, /*in_function_try_block=*/true);
25512   /* We're done with the `try' part.  */
25513   finish_function_try_block (try_block);
25514   /* Parse the handlers.  */
25515   cp_parser_handler_seq (parser);
25516   /* We're done with the handlers.  */
25517   finish_function_handler_sequence (try_block, compound_stmt);
25518 }
25519 
25520 /* Parse a handler-seq.
25521 
25522    handler-seq:
25523      handler handler-seq [opt]  */
25524 
25525 static void
cp_parser_handler_seq(cp_parser * parser)25526 cp_parser_handler_seq (cp_parser* parser)
25527 {
25528   while (true)
25529     {
25530       cp_token *token;
25531 
25532       /* Parse the handler.  */
25533       cp_parser_handler (parser);
25534       /* Peek at the next token.  */
25535       token = cp_lexer_peek_token (parser->lexer);
25536       /* If it's not `catch' then there are no more handlers.  */
25537       if (!cp_parser_is_keyword (token, RID_CATCH))
25538 	break;
25539     }
25540 }
25541 
25542 /* Parse a handler.
25543 
25544    handler:
25545      catch ( exception-declaration ) compound-statement  */
25546 
25547 static void
cp_parser_handler(cp_parser * parser)25548 cp_parser_handler (cp_parser* parser)
25549 {
25550   tree handler;
25551   tree declaration;
25552 
25553   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25554   handler = begin_handler ();
25555   matching_parens parens;
25556   parens.require_open (parser);
25557   declaration = cp_parser_exception_declaration (parser);
25558   finish_handler_parms (declaration, handler);
25559   parens.require_close (parser);
25560   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25561   finish_handler (handler);
25562 }
25563 
25564 /* Parse an exception-declaration.
25565 
25566    exception-declaration:
25567      type-specifier-seq declarator
25568      type-specifier-seq abstract-declarator
25569      type-specifier-seq
25570      ...
25571 
25572    Returns a VAR_DECL for the declaration, or NULL_TREE if the
25573    ellipsis variant is used.  */
25574 
25575 static tree
cp_parser_exception_declaration(cp_parser * parser)25576 cp_parser_exception_declaration (cp_parser* parser)
25577 {
25578   cp_decl_specifier_seq type_specifiers;
25579   cp_declarator *declarator;
25580   const char *saved_message;
25581 
25582   /* If it's an ellipsis, it's easy to handle.  */
25583   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25584     {
25585       /* Consume the `...' token.  */
25586       cp_lexer_consume_token (parser->lexer);
25587       return NULL_TREE;
25588     }
25589 
25590   /* Types may not be defined in exception-declarations.  */
25591   saved_message = parser->type_definition_forbidden_message;
25592   parser->type_definition_forbidden_message
25593     = G_("types may not be defined in exception-declarations");
25594 
25595   /* Parse the type-specifier-seq.  */
25596   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25597 				/*is_declaration=*/true,
25598 				/*is_trailing_return=*/false,
25599 				&type_specifiers);
25600   /* If it's a `)', then there is no declarator.  */
25601   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25602     declarator = NULL;
25603   else
25604     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25605 				       CP_PARSER_FLAGS_NONE,
25606 				       /*ctor_dtor_or_conv_p=*/NULL,
25607 				       /*parenthesized_p=*/NULL,
25608 				       /*member_p=*/false,
25609 				       /*friend_p=*/false,
25610 				       /*static_p=*/false);
25611 
25612   /* Restore the saved message.  */
25613   parser->type_definition_forbidden_message = saved_message;
25614 
25615   if (!type_specifiers.any_specifiers_p)
25616     return error_mark_node;
25617 
25618   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25619 }
25620 
25621 /* Parse a throw-expression.
25622 
25623    throw-expression:
25624      throw assignment-expression [opt]
25625 
25626    Returns a THROW_EXPR representing the throw-expression.  */
25627 
25628 static tree
cp_parser_throw_expression(cp_parser * parser)25629 cp_parser_throw_expression (cp_parser* parser)
25630 {
25631   tree expression;
25632   cp_token* token;
25633 
25634   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25635   token = cp_lexer_peek_token (parser->lexer);
25636   /* Figure out whether or not there is an assignment-expression
25637      following the "throw" keyword.  */
25638   if (token->type == CPP_COMMA
25639       || token->type == CPP_SEMICOLON
25640       || token->type == CPP_CLOSE_PAREN
25641       || token->type == CPP_CLOSE_SQUARE
25642       || token->type == CPP_CLOSE_BRACE
25643       || token->type == CPP_COLON)
25644     expression = NULL_TREE;
25645   else
25646     expression = cp_parser_assignment_expression (parser);
25647 
25648   return build_throw (expression);
25649 }
25650 
25651 /* GNU Extensions */
25652 
25653 /* Parse an (optional) asm-specification.
25654 
25655    asm-specification:
25656      asm ( string-literal )
25657 
25658    If the asm-specification is present, returns a STRING_CST
25659    corresponding to the string-literal.  Otherwise, returns
25660    NULL_TREE.  */
25661 
25662 static tree
cp_parser_asm_specification_opt(cp_parser * parser)25663 cp_parser_asm_specification_opt (cp_parser* parser)
25664 {
25665   cp_token *token;
25666   tree asm_specification;
25667 
25668   /* Peek at the next token.  */
25669   token = cp_lexer_peek_token (parser->lexer);
25670   /* If the next token isn't the `asm' keyword, then there's no
25671      asm-specification.  */
25672   if (!cp_parser_is_keyword (token, RID_ASM))
25673     return NULL_TREE;
25674 
25675   /* Consume the `asm' token.  */
25676   cp_lexer_consume_token (parser->lexer);
25677   /* Look for the `('.  */
25678   matching_parens parens;
25679   parens.require_open (parser);
25680 
25681   /* Look for the string-literal.  */
25682   asm_specification = cp_parser_string_literal (parser, false, false);
25683 
25684   /* Look for the `)'.  */
25685   parens.require_close (parser);
25686 
25687   return asm_specification;
25688 }
25689 
25690 /* Parse an asm-operand-list.
25691 
25692    asm-operand-list:
25693      asm-operand
25694      asm-operand-list , asm-operand
25695 
25696    asm-operand:
25697      string-literal ( expression )
25698      [ string-literal ] string-literal ( expression )
25699 
25700    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
25701    each node is the expression.  The TREE_PURPOSE is itself a
25702    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25703    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25704    is a STRING_CST for the string literal before the parenthesis. Returns
25705    ERROR_MARK_NODE if any of the operands are invalid.  */
25706 
25707 static tree
cp_parser_asm_operand_list(cp_parser * parser)25708 cp_parser_asm_operand_list (cp_parser* parser)
25709 {
25710   tree asm_operands = NULL_TREE;
25711   bool invalid_operands = false;
25712 
25713   while (true)
25714     {
25715       tree string_literal;
25716       tree expression;
25717       tree name;
25718 
25719       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25720 	{
25721 	  /* Consume the `[' token.  */
25722 	  cp_lexer_consume_token (parser->lexer);
25723 	  /* Read the operand name.  */
25724 	  name = cp_parser_identifier (parser);
25725 	  if (name != error_mark_node)
25726 	    name = build_string (IDENTIFIER_LENGTH (name),
25727 				 IDENTIFIER_POINTER (name));
25728 	  /* Look for the closing `]'.  */
25729 	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25730 	}
25731       else
25732 	name = NULL_TREE;
25733       /* Look for the string-literal.  */
25734       string_literal = cp_parser_string_literal (parser, false, false);
25735 
25736       /* Look for the `('.  */
25737       matching_parens parens;
25738       parens.require_open (parser);
25739       /* Parse the expression.  */
25740       expression = cp_parser_expression (parser);
25741       /* Look for the `)'.  */
25742       parens.require_close (parser);
25743 
25744       if (name == error_mark_node
25745 	  || string_literal == error_mark_node
25746 	  || expression == error_mark_node)
25747         invalid_operands = true;
25748 
25749       /* Add this operand to the list.  */
25750       asm_operands = tree_cons (build_tree_list (name, string_literal),
25751 				expression,
25752 				asm_operands);
25753       /* If the next token is not a `,', there are no more
25754 	 operands.  */
25755       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25756 	break;
25757       /* Consume the `,'.  */
25758       cp_lexer_consume_token (parser->lexer);
25759     }
25760 
25761   return invalid_operands ? error_mark_node : nreverse (asm_operands);
25762 }
25763 
25764 /* Parse an asm-clobber-list.
25765 
25766    asm-clobber-list:
25767      string-literal
25768      asm-clobber-list , string-literal
25769 
25770    Returns a TREE_LIST, indicating the clobbers in the order that they
25771    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
25772 
25773 static tree
cp_parser_asm_clobber_list(cp_parser * parser)25774 cp_parser_asm_clobber_list (cp_parser* parser)
25775 {
25776   tree clobbers = NULL_TREE;
25777 
25778   while (true)
25779     {
25780       tree string_literal;
25781 
25782       /* Look for the string literal.  */
25783       string_literal = cp_parser_string_literal (parser, false, false);
25784       /* Add it to the list.  */
25785       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25786       /* If the next token is not a `,', then the list is
25787 	 complete.  */
25788       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25789 	break;
25790       /* Consume the `,' token.  */
25791       cp_lexer_consume_token (parser->lexer);
25792     }
25793 
25794   return clobbers;
25795 }
25796 
25797 /* Parse an asm-label-list.
25798 
25799    asm-label-list:
25800      identifier
25801      asm-label-list , identifier
25802 
25803    Returns a TREE_LIST, indicating the labels in the order that they
25804    appeared.  The TREE_VALUE of each node is a label.  */
25805 
25806 static tree
cp_parser_asm_label_list(cp_parser * parser)25807 cp_parser_asm_label_list (cp_parser* parser)
25808 {
25809   tree labels = NULL_TREE;
25810 
25811   while (true)
25812     {
25813       tree identifier, label, name;
25814 
25815       /* Look for the identifier.  */
25816       identifier = cp_parser_identifier (parser);
25817       if (!error_operand_p (identifier))
25818         {
25819 	  label = lookup_label (identifier);
25820 	  if (TREE_CODE (label) == LABEL_DECL)
25821 	    {
25822 	      TREE_USED (label) = 1;
25823 	      check_goto (label);
25824 	      name = build_string (IDENTIFIER_LENGTH (identifier),
25825 				   IDENTIFIER_POINTER (identifier));
25826 	      labels = tree_cons (name, label, labels);
25827 	    }
25828 	}
25829       /* If the next token is not a `,', then the list is
25830 	 complete.  */
25831       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25832 	break;
25833       /* Consume the `,' token.  */
25834       cp_lexer_consume_token (parser->lexer);
25835     }
25836 
25837   return nreverse (labels);
25838 }
25839 
25840 /* Return TRUE iff the next tokens in the stream are possibly the
25841    beginning of a GNU extension attribute. */
25842 
25843 static bool
cp_next_tokens_can_be_gnu_attribute_p(cp_parser * parser)25844 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25845 {
25846   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25847 }
25848 
25849 /* Return TRUE iff the next tokens in the stream are possibly the
25850    beginning of a standard C++-11 attribute specifier.  */
25851 
25852 static bool
cp_next_tokens_can_be_std_attribute_p(cp_parser * parser)25853 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25854 {
25855   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25856 }
25857 
25858 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25859    beginning of a standard C++-11 attribute specifier.  */
25860 
25861 static bool
cp_nth_tokens_can_be_std_attribute_p(cp_parser * parser,size_t n)25862 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25863 {
25864   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25865 
25866   return (cxx_dialect >= cxx11
25867 	  && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25868 	      || (token->type == CPP_OPEN_SQUARE
25869 		  && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25870 		  && token->type == CPP_OPEN_SQUARE)));
25871 }
25872 
25873 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25874    beginning of a GNU extension attribute.  */
25875 
25876 static bool
cp_nth_tokens_can_be_gnu_attribute_p(cp_parser * parser,size_t n)25877 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25878 {
25879   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25880 
25881   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25882 }
25883 
25884 /* Return true iff the next tokens can be the beginning of either a
25885    GNU attribute list, or a standard C++11 attribute sequence.  */
25886 
25887 static bool
cp_next_tokens_can_be_attribute_p(cp_parser * parser)25888 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25889 {
25890   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25891 	  || cp_next_tokens_can_be_std_attribute_p (parser));
25892 }
25893 
25894 /* Return true iff the next Nth tokens can be the beginning of either
25895    a GNU attribute list, or a standard C++11 attribute sequence.  */
25896 
25897 static bool
cp_nth_tokens_can_be_attribute_p(cp_parser * parser,size_t n)25898 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25899 {
25900   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25901 	  || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25902 }
25903 
25904 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25905    of GNU attributes, or return NULL.  */
25906 
25907 static tree
cp_parser_attributes_opt(cp_parser * parser)25908 cp_parser_attributes_opt (cp_parser *parser)
25909 {
25910   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25911     return cp_parser_gnu_attributes_opt (parser);
25912   return cp_parser_std_attribute_spec_seq (parser);
25913 }
25914 
25915 /* Parse an (optional) series of attributes.
25916 
25917    attributes:
25918      attributes attribute
25919 
25920    attribute:
25921      __attribute__ (( attribute-list [opt] ))
25922 
25923    The return value is as for cp_parser_gnu_attribute_list.  */
25924 
25925 static tree
cp_parser_gnu_attributes_opt(cp_parser * parser)25926 cp_parser_gnu_attributes_opt (cp_parser* parser)
25927 {
25928   tree attributes = NULL_TREE;
25929 
25930   temp_override<bool> cleanup
25931     (parser->auto_is_implicit_function_template_parm_p, false);
25932 
25933   while (true)
25934     {
25935       cp_token *token;
25936       tree attribute_list;
25937       bool ok = true;
25938 
25939       /* Peek at the next token.  */
25940       token = cp_lexer_peek_token (parser->lexer);
25941       /* If it's not `__attribute__', then we're done.  */
25942       if (token->keyword != RID_ATTRIBUTE)
25943 	break;
25944 
25945       /* Consume the `__attribute__' keyword.  */
25946       cp_lexer_consume_token (parser->lexer);
25947       /* Look for the two `(' tokens.  */
25948       matching_parens outer_parens;
25949       if (!outer_parens.require_open (parser))
25950 	ok = false;
25951       matching_parens inner_parens;
25952       if (!inner_parens.require_open (parser))
25953 	ok = false;
25954 
25955       /* Peek at the next token.  */
25956       token = cp_lexer_peek_token (parser->lexer);
25957       if (token->type != CPP_CLOSE_PAREN)
25958 	/* Parse the attribute-list.  */
25959 	attribute_list = cp_parser_gnu_attribute_list (parser);
25960       else
25961 	/* If the next token is a `)', then there is no attribute
25962 	   list.  */
25963 	attribute_list = NULL;
25964 
25965       /* Look for the two `)' tokens.  */
25966       if (!inner_parens.require_close (parser))
25967 	ok = false;
25968       if (!outer_parens.require_close (parser))
25969 	ok = false;
25970       if (!ok)
25971 	cp_parser_skip_to_end_of_statement (parser);
25972 
25973       /* Add these new attributes to the list.  */
25974       attributes = attr_chainon (attributes, attribute_list);
25975     }
25976 
25977   return attributes;
25978 }
25979 
25980 /* Parse a GNU attribute-list.
25981 
25982    attribute-list:
25983      attribute
25984      attribute-list , attribute
25985 
25986    attribute:
25987      identifier
25988      identifier ( identifier )
25989      identifier ( identifier , expression-list )
25990      identifier ( expression-list )
25991 
25992    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
25993    to an attribute.  The TREE_PURPOSE of each node is the identifier
25994    indicating which attribute is in use.  The TREE_VALUE represents
25995    the arguments, if any.  */
25996 
25997 static tree
cp_parser_gnu_attribute_list(cp_parser * parser,bool exactly_one)25998 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25999 {
26000   tree attribute_list = NULL_TREE;
26001   bool save_translate_strings_p = parser->translate_strings_p;
26002 
26003   /* Don't create wrapper nodes within attributes: the
26004      handlers don't know how to handle them.  */
26005   auto_suppress_location_wrappers sentinel;
26006 
26007   parser->translate_strings_p = false;
26008   while (true)
26009     {
26010       cp_token *token;
26011       tree identifier;
26012       tree attribute;
26013 
26014       /* Look for the identifier.  We also allow keywords here; for
26015 	 example `__attribute__ ((const))' is legal.  */
26016       token = cp_lexer_peek_token (parser->lexer);
26017       if (token->type == CPP_NAME
26018 	  || token->type == CPP_KEYWORD)
26019 	{
26020 	  tree arguments = NULL_TREE;
26021 
26022 	  /* Consume the token, but save it since we need it for the
26023 	     SIMD enabled function parsing.  */
26024 	  cp_token *id_token = cp_lexer_consume_token (parser->lexer);
26025 
26026 	  /* Save away the identifier that indicates which attribute
26027 	     this is.  */
26028 	  identifier = (token->type == CPP_KEYWORD)
26029 	    /* For keywords, use the canonical spelling, not the
26030 	       parsed identifier.  */
26031 	    ? ridpointers[(int) token->keyword]
26032 	    : id_token->u.value;
26033 
26034 	  identifier = canonicalize_attr_name (identifier);
26035 	  attribute = build_tree_list (identifier, NULL_TREE);
26036 
26037 	  /* Peek at the next token.  */
26038 	  token = cp_lexer_peek_token (parser->lexer);
26039 	  /* If it's an `(', then parse the attribute arguments.  */
26040 	  if (token->type == CPP_OPEN_PAREN)
26041 	    {
26042 	      vec<tree, va_gc> *vec;
26043 	      int attr_flag = (attribute_takes_identifier_p (identifier)
26044 			       ? id_attr : normal_attr);
26045 	      vec = cp_parser_parenthesized_expression_list
26046 		    (parser, attr_flag, /*cast_p=*/false,
26047 		    /*allow_expansion_p=*/false,
26048 		    /*non_constant_p=*/NULL);
26049 	      if (vec == NULL)
26050 		arguments = error_mark_node;
26051 	      else
26052 		{
26053 		  arguments = build_tree_list_vec (vec);
26054 		  release_tree_vector (vec);
26055 		}
26056 	      /* Save the arguments away.  */
26057 	      TREE_VALUE (attribute) = arguments;
26058 	    }
26059 
26060 	  if (arguments != error_mark_node)
26061 	    {
26062 	      /* Add this attribute to the list.  */
26063 	      TREE_CHAIN (attribute) = attribute_list;
26064 	      attribute_list = attribute;
26065 	    }
26066 
26067 	  token = cp_lexer_peek_token (parser->lexer);
26068 	}
26069       /* Unless EXACTLY_ONE is set look for more attributes.
26070 	 If the next token isn't a `,', we're done.  */
26071       if (exactly_one || token->type != CPP_COMMA)
26072 	break;
26073 
26074       /* Consume the comma and keep going.  */
26075       cp_lexer_consume_token (parser->lexer);
26076     }
26077   parser->translate_strings_p = save_translate_strings_p;
26078 
26079   /* We built up the list in reverse order.  */
26080   return nreverse (attribute_list);
26081 }
26082 
26083 /*  Parse a standard C++11 attribute.
26084 
26085     The returned representation is a TREE_LIST which TREE_PURPOSE is
26086     the scoped name of the attribute, and the TREE_VALUE is its
26087     arguments list.
26088 
26089     Note that the scoped name of the attribute is itself a TREE_LIST
26090     which TREE_PURPOSE is the namespace of the attribute, and
26091     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
26092     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26093     and which TREE_PURPOSE is directly the attribute name.
26094 
26095     Clients of the attribute code should use get_attribute_namespace
26096     and get_attribute_name to get the actual namespace and name of
26097     attributes, regardless of their being GNU or C++11 attributes.
26098 
26099     attribute:
26100       attribute-token attribute-argument-clause [opt]
26101 
26102     attribute-token:
26103       identifier
26104       attribute-scoped-token
26105 
26106     attribute-scoped-token:
26107       attribute-namespace :: identifier
26108 
26109     attribute-namespace:
26110       identifier
26111 
26112     attribute-argument-clause:
26113       ( balanced-token-seq )
26114 
26115     balanced-token-seq:
26116       balanced-token [opt]
26117       balanced-token-seq balanced-token
26118 
26119     balanced-token:
26120       ( balanced-token-seq )
26121       [ balanced-token-seq ]
26122       { balanced-token-seq }.  */
26123 
26124 static tree
cp_parser_std_attribute(cp_parser * parser,tree attr_ns)26125 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
26126 {
26127   tree attribute, attr_id = NULL_TREE, arguments;
26128   cp_token *token;
26129 
26130   temp_override<bool> cleanup
26131     (parser->auto_is_implicit_function_template_parm_p, false);
26132 
26133   /* First, parse name of the attribute, a.k.a attribute-token.  */
26134 
26135   token = cp_lexer_peek_token (parser->lexer);
26136   if (token->type == CPP_NAME)
26137     attr_id = token->u.value;
26138   else if (token->type == CPP_KEYWORD)
26139     attr_id = ridpointers[(int) token->keyword];
26140   else if (token->flags & NAMED_OP)
26141     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26142 
26143   if (attr_id == NULL_TREE)
26144     return NULL_TREE;
26145 
26146   cp_lexer_consume_token (parser->lexer);
26147 
26148   token = cp_lexer_peek_token (parser->lexer);
26149   if (token->type == CPP_SCOPE)
26150     {
26151       /* We are seeing a scoped attribute token.  */
26152 
26153       cp_lexer_consume_token (parser->lexer);
26154       if (attr_ns)
26155 	error_at (token->location, "attribute using prefix used together "
26156 				   "with scoped attribute token");
26157       attr_ns = attr_id;
26158 
26159       token = cp_lexer_consume_token (parser->lexer);
26160       if (token->type == CPP_NAME)
26161 	attr_id = token->u.value;
26162       else if (token->type == CPP_KEYWORD)
26163 	attr_id = ridpointers[(int) token->keyword];
26164       else if (token->flags & NAMED_OP)
26165 	attr_id = get_identifier (cpp_type2name (token->type, token->flags));
26166       else
26167 	{
26168 	  error_at (token->location,
26169 		    "expected an identifier for the attribute name");
26170 	  return error_mark_node;
26171 	}
26172 
26173       attr_ns = canonicalize_attr_name (attr_ns);
26174       attr_id = canonicalize_attr_name (attr_id);
26175       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26176 				   NULL_TREE);
26177       token = cp_lexer_peek_token (parser->lexer);
26178     }
26179   else if (attr_ns)
26180     {
26181       attr_ns = canonicalize_attr_name (attr_ns);
26182       attr_id = canonicalize_attr_name (attr_id);
26183       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26184 				   NULL_TREE);
26185     }
26186   else
26187     {
26188       attr_id = canonicalize_attr_name (attr_id);
26189       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26190 				   NULL_TREE);
26191       /* C++11 noreturn attribute is equivalent to GNU's.  */
26192       if (is_attribute_p ("noreturn", attr_id))
26193 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26194       /* C++14 deprecated attribute is equivalent to GNU's.  */
26195       else if (is_attribute_p ("deprecated", attr_id))
26196 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26197       /* C++17 fallthrough attribute is equivalent to GNU's.  */
26198       else if (is_attribute_p ("fallthrough", attr_id))
26199 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26200       /* Transactional Memory TS optimize_for_synchronized attribute is
26201 	 equivalent to GNU transaction_callable.  */
26202       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26203 	TREE_PURPOSE (attribute)
26204 	  = get_identifier ("transaction_callable");
26205       /* Transactional Memory attributes are GNU attributes.  */
26206       else if (tm_attr_to_mask (attr_id))
26207 	TREE_PURPOSE (attribute) = attr_id;
26208     }
26209 
26210   /* Now parse the optional argument clause of the attribute.  */
26211 
26212   if (token->type != CPP_OPEN_PAREN)
26213     return attribute;
26214 
26215   {
26216     vec<tree, va_gc> *vec;
26217     int attr_flag = normal_attr;
26218 
26219     if (attr_ns == gnu_identifier
26220 	&& attribute_takes_identifier_p (attr_id))
26221       /* A GNU attribute that takes an identifier in parameter.  */
26222       attr_flag = id_attr;
26223 
26224     const attribute_spec *as
26225       = lookup_attribute_spec (TREE_PURPOSE (attribute));
26226     if (as == NULL)
26227       {
26228 	/* For unknown attributes, just skip balanced tokens instead of
26229 	   trying to parse the arguments.  */
26230 	for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
26231 	  cp_lexer_consume_token (parser->lexer);
26232 	return attribute;
26233       }
26234 
26235     vec = cp_parser_parenthesized_expression_list
26236       (parser, attr_flag, /*cast_p=*/false,
26237        /*allow_expansion_p=*/true,
26238        /*non_constant_p=*/NULL);
26239     if (vec == NULL)
26240       arguments = error_mark_node;
26241     else
26242       {
26243 	arguments = build_tree_list_vec (vec);
26244 	release_tree_vector (vec);
26245       }
26246 
26247     if (arguments == error_mark_node)
26248       attribute = error_mark_node;
26249     else
26250       TREE_VALUE (attribute) = arguments;
26251   }
26252 
26253   return attribute;
26254 }
26255 
26256 /* Check that the attribute ATTRIBUTE appears at most once in the
26257    attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3)
26258    and deprecated (7.6.5).  Note that carries_dependency (7.6.4)
26259    isn't implemented yet in GCC.  */
26260 
26261 static void
cp_parser_check_std_attribute(tree attributes,tree attribute)26262 cp_parser_check_std_attribute (tree attributes, tree attribute)
26263 {
26264   if (attributes)
26265     {
26266       tree name = get_attribute_name (attribute);
26267       if (is_attribute_p ("noreturn", name)
26268 	  && lookup_attribute ("noreturn", attributes))
26269 	error ("attribute %<noreturn%> can appear at most once "
26270 	       "in an attribute-list");
26271       else if (is_attribute_p ("deprecated", name)
26272 	       && lookup_attribute ("deprecated", attributes))
26273 	error ("attribute %<deprecated%> can appear at most once "
26274 	       "in an attribute-list");
26275     }
26276 }
26277 
26278 /* Parse a list of standard C++-11 attributes.
26279 
26280    attribute-list:
26281      attribute [opt]
26282      attribute-list , attribute[opt]
26283      attribute ...
26284      attribute-list , attribute ...
26285 */
26286 
26287 static tree
cp_parser_std_attribute_list(cp_parser * parser,tree attr_ns)26288 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26289 {
26290   tree attributes = NULL_TREE, attribute = NULL_TREE;
26291   cp_token *token = NULL;
26292 
26293   while (true)
26294     {
26295       attribute = cp_parser_std_attribute (parser, attr_ns);
26296       if (attribute == error_mark_node)
26297 	break;
26298       if (attribute != NULL_TREE)
26299 	{
26300 	  cp_parser_check_std_attribute (attributes, attribute);
26301 	  TREE_CHAIN (attribute) = attributes;
26302 	  attributes = attribute;
26303 	}
26304       token = cp_lexer_peek_token (parser->lexer);
26305       if (token->type == CPP_ELLIPSIS)
26306 	{
26307 	  cp_lexer_consume_token (parser->lexer);
26308 	  if (attribute == NULL_TREE)
26309 	    error_at (token->location,
26310 		      "expected attribute before %<...%>");
26311 	  else
26312 	    {
26313 	      tree pack = make_pack_expansion (TREE_VALUE (attribute));
26314 	      if (pack == error_mark_node)
26315 		return error_mark_node;
26316 	      TREE_VALUE (attribute) = pack;
26317 	    }
26318 	  token = cp_lexer_peek_token (parser->lexer);
26319 	}
26320       if (token->type != CPP_COMMA)
26321 	break;
26322       cp_lexer_consume_token (parser->lexer);
26323     }
26324   attributes = nreverse (attributes);
26325   return attributes;
26326 }
26327 
26328 /* Parse a standard C++-11 attribute specifier.
26329 
26330    attribute-specifier:
26331      [ [ attribute-using-prefix [opt] attribute-list ] ]
26332      alignment-specifier
26333 
26334    attribute-using-prefix:
26335      using attribute-namespace :
26336 
26337    alignment-specifier:
26338      alignas ( type-id ... [opt] )
26339      alignas ( alignment-expression ... [opt] ).  */
26340 
26341 static tree
cp_parser_std_attribute_spec(cp_parser * parser)26342 cp_parser_std_attribute_spec (cp_parser *parser)
26343 {
26344   tree attributes = NULL_TREE;
26345   cp_token *token = cp_lexer_peek_token (parser->lexer);
26346 
26347   if (token->type == CPP_OPEN_SQUARE
26348       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26349     {
26350       tree attr_ns = NULL_TREE;
26351 
26352       cp_lexer_consume_token (parser->lexer);
26353       cp_lexer_consume_token (parser->lexer);
26354 
26355       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26356 	{
26357 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
26358 	  if (token->type == CPP_NAME)
26359 	    attr_ns = token->u.value;
26360 	  else if (token->type == CPP_KEYWORD)
26361 	    attr_ns = ridpointers[(int) token->keyword];
26362 	  else if (token->flags & NAMED_OP)
26363 	    attr_ns = get_identifier (cpp_type2name (token->type,
26364 						     token->flags));
26365 	  if (attr_ns
26366 	      && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26367 	    {
26368 	      if (cxx_dialect < cxx17
26369 		  && !in_system_header_at (input_location))
26370 		pedwarn (input_location, 0,
26371 			 "attribute using prefix only available "
26372 			 "with %<-std=c++17%> or %<-std=gnu++17%>");
26373 
26374 	      cp_lexer_consume_token (parser->lexer);
26375 	      cp_lexer_consume_token (parser->lexer);
26376 	      cp_lexer_consume_token (parser->lexer);
26377 	    }
26378 	  else
26379 	    attr_ns = NULL_TREE;
26380 	}
26381 
26382       attributes = cp_parser_std_attribute_list (parser, attr_ns);
26383 
26384       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26385 	  || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26386 	cp_parser_skip_to_end_of_statement (parser);
26387       else
26388 	/* Warn about parsing c++11 attribute in non-c++11 mode, only
26389 	   when we are sure that we have actually parsed them.  */
26390 	maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26391     }
26392   else
26393     {
26394       tree alignas_expr;
26395 
26396       /* Look for an alignment-specifier.  */
26397 
26398       token = cp_lexer_peek_token (parser->lexer);
26399 
26400       if (token->type != CPP_KEYWORD
26401 	  || token->keyword != RID_ALIGNAS)
26402 	return NULL_TREE;
26403 
26404       cp_lexer_consume_token (parser->lexer);
26405       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26406 
26407       matching_parens parens;
26408       if (!parens.require_open (parser))
26409 	return error_mark_node;
26410 
26411       cp_parser_parse_tentatively (parser);
26412       alignas_expr = cp_parser_type_id (parser);
26413 
26414       if (!cp_parser_parse_definitely (parser))
26415 	{
26416 	  alignas_expr = cp_parser_assignment_expression (parser);
26417 	  if (alignas_expr == error_mark_node)
26418 	    cp_parser_skip_to_end_of_statement (parser);
26419 	  if (alignas_expr == NULL_TREE
26420 	      || alignas_expr == error_mark_node)
26421 	    return alignas_expr;
26422 	}
26423 
26424       alignas_expr = cxx_alignas_expr (alignas_expr);
26425       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26426 
26427       /* Handle alignas (pack...).  */
26428       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26429 	{
26430 	  cp_lexer_consume_token (parser->lexer);
26431 	  alignas_expr = make_pack_expansion (alignas_expr);
26432 	}
26433 
26434       /* Something went wrong, so don't build the attribute.  */
26435       if (alignas_expr == error_mark_node)
26436 	return error_mark_node;
26437 
26438       if (!parens.require_close (parser))
26439 	return error_mark_node;
26440 
26441       /* Build the C++-11 representation of an 'aligned'
26442 	 attribute.  */
26443       attributes
26444 	= build_tree_list (build_tree_list (gnu_identifier,
26445 					    aligned_identifier), alignas_expr);
26446     }
26447 
26448   return attributes;
26449 }
26450 
26451 /* Parse a standard C++-11 attribute-specifier-seq.
26452 
26453    attribute-specifier-seq:
26454      attribute-specifier-seq [opt] attribute-specifier
26455  */
26456 
26457 static tree
cp_parser_std_attribute_spec_seq(cp_parser * parser)26458 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26459 {
26460   tree attr_specs = NULL_TREE;
26461   tree attr_last = NULL_TREE;
26462 
26463   /* Don't create wrapper nodes within attributes: the
26464      handlers don't know how to handle them.  */
26465   auto_suppress_location_wrappers sentinel;
26466 
26467   while (true)
26468     {
26469       tree attr_spec = cp_parser_std_attribute_spec (parser);
26470       if (attr_spec == NULL_TREE)
26471 	break;
26472       if (attr_spec == error_mark_node)
26473 	return error_mark_node;
26474 
26475       if (attr_last)
26476 	TREE_CHAIN (attr_last) = attr_spec;
26477       else
26478 	attr_specs = attr_last = attr_spec;
26479       attr_last = tree_last (attr_last);
26480     }
26481 
26482   return attr_specs;
26483 }
26484 
26485 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26486    return index of the first token after balanced-token, or N on failure.  */
26487 
26488 static size_t
cp_parser_skip_balanced_tokens(cp_parser * parser,size_t n)26489 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26490 {
26491   size_t orig_n = n;
26492   int nparens = 0, nbraces = 0, nsquares = 0;
26493   do
26494     switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26495       {
26496       case CPP_PRAGMA_EOL:
26497 	if (!parser->lexer->in_pragma)
26498 	  break;
26499 	/* FALLTHRU */
26500       case CPP_EOF:
26501 	/* Ran out of tokens.  */
26502 	return orig_n;
26503       case CPP_OPEN_PAREN:
26504 	++nparens;
26505 	break;
26506       case CPP_OPEN_BRACE:
26507 	++nbraces;
26508 	break;
26509       case CPP_OPEN_SQUARE:
26510 	++nsquares;
26511 	break;
26512       case CPP_CLOSE_PAREN:
26513 	--nparens;
26514 	break;
26515       case CPP_CLOSE_BRACE:
26516 	--nbraces;
26517 	break;
26518       case CPP_CLOSE_SQUARE:
26519 	--nsquares;
26520 	break;
26521       default:
26522 	break;
26523       }
26524   while (nparens || nbraces || nsquares);
26525   return n;
26526 }
26527 
26528 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26529    return index of the first token after the GNU attribute tokens, or N on
26530    failure.  */
26531 
26532 static size_t
cp_parser_skip_gnu_attributes_opt(cp_parser * parser,size_t n)26533 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26534 {
26535   while (true)
26536     {
26537       if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26538 	  || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26539 	  || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26540 	break;
26541 
26542       size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26543       if (n2 == n + 2)
26544 	break;
26545       if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26546 	break;
26547       n = n2 + 1;
26548     }
26549   return n;
26550 }
26551 
26552 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26553    next token), return index of the first token after the standard C++11
26554    attribute tokens, or N on failure.  */
26555 
26556 static size_t
cp_parser_skip_std_attribute_spec_seq(cp_parser * parser,size_t n)26557 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26558 {
26559   while (true)
26560     {
26561       if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26562 	  && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26563 	{
26564 	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26565 	  if (n2 == n + 1)
26566 	    break;
26567 	  if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26568 	    break;
26569 	  n = n2 + 1;
26570 	}
26571       else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26572 	       && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26573 	{
26574 	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26575 	  if (n2 == n + 1)
26576 	    break;
26577 	  n = n2;
26578 	}
26579       else
26580 	break;
26581     }
26582   return n;
26583 }
26584 
26585 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26586    as the next token), return index of the first token after the attribute
26587    tokens, or N on failure.  */
26588 
26589 static size_t
cp_parser_skip_attributes_opt(cp_parser * parser,size_t n)26590 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26591 {
26592   if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26593     return cp_parser_skip_gnu_attributes_opt (parser, n);
26594   return cp_parser_skip_std_attribute_spec_seq (parser, n);
26595 }
26596 
26597 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
26598    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
26599    current value of the PEDANTIC flag, regardless of whether or not
26600    the `__extension__' keyword is present.  The caller is responsible
26601    for restoring the value of the PEDANTIC flag.  */
26602 
26603 static bool
cp_parser_extension_opt(cp_parser * parser,int * saved_pedantic)26604 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26605 {
26606   /* Save the old value of the PEDANTIC flag.  */
26607   *saved_pedantic = pedantic;
26608 
26609   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26610     {
26611       /* Consume the `__extension__' token.  */
26612       cp_lexer_consume_token (parser->lexer);
26613       /* We're not being pedantic while the `__extension__' keyword is
26614 	 in effect.  */
26615       pedantic = 0;
26616 
26617       return true;
26618     }
26619 
26620   return false;
26621 }
26622 
26623 /* Parse a label declaration.
26624 
26625    label-declaration:
26626      __label__ label-declarator-seq ;
26627 
26628    label-declarator-seq:
26629      identifier , label-declarator-seq
26630      identifier  */
26631 
26632 static void
cp_parser_label_declaration(cp_parser * parser)26633 cp_parser_label_declaration (cp_parser* parser)
26634 {
26635   /* Look for the `__label__' keyword.  */
26636   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26637 
26638   while (true)
26639     {
26640       tree identifier;
26641 
26642       /* Look for an identifier.  */
26643       identifier = cp_parser_identifier (parser);
26644       /* If we failed, stop.  */
26645       if (identifier == error_mark_node)
26646 	break;
26647       /* Declare it as a label.  */
26648       finish_label_decl (identifier);
26649       /* If the next token is a `;', stop.  */
26650       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26651 	break;
26652       /* Look for the `,' separating the label declarations.  */
26653       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26654     }
26655 
26656   /* Look for the final `;'.  */
26657   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26658 }
26659 
26660 // -------------------------------------------------------------------------- //
26661 // Requires Clause
26662 
26663 // Parse a requires clause.
26664 //
26665 //    requires-clause:
26666 //      'requires' logical-or-expression
26667 //
26668 // The required logical-or-expression must be a constant expression. Note
26669 // that we don't check that the expression is constepxr here. We defer until
26670 // we analyze constraints and then, we only check atomic constraints.
26671 static tree
cp_parser_requires_clause(cp_parser * parser)26672 cp_parser_requires_clause (cp_parser *parser)
26673 {
26674   // Parse the requires clause so that it is not automatically folded.
26675   ++processing_template_decl;
26676   tree expr = cp_parser_binary_expression (parser, false, false,
26677 					   PREC_NOT_OPERATOR, NULL);
26678   if (check_for_bare_parameter_packs (expr))
26679     expr = error_mark_node;
26680   --processing_template_decl;
26681   return expr;
26682 }
26683 
26684 // Optionally parse a requires clause:
26685 static tree
cp_parser_requires_clause_opt(cp_parser * parser)26686 cp_parser_requires_clause_opt (cp_parser *parser)
26687 {
26688   cp_token *tok = cp_lexer_peek_token (parser->lexer);
26689   if (tok->keyword != RID_REQUIRES)
26690     {
26691       if (!flag_concepts && tok->type == CPP_NAME
26692 	  && tok->u.value == ridpointers[RID_REQUIRES])
26693 	{
26694 	  error_at (cp_lexer_peek_token (parser->lexer)->location,
26695 		    "%<requires%> only available with %<-fconcepts%>");
26696 	  /* Parse and discard the requires-clause.  */
26697 	  cp_lexer_consume_token (parser->lexer);
26698 	  cp_parser_requires_clause (parser);
26699 	}
26700       return NULL_TREE;
26701     }
26702   cp_lexer_consume_token (parser->lexer);
26703   return cp_parser_requires_clause (parser);
26704 }
26705 
26706 
26707 /*---------------------------------------------------------------------------
26708                            Requires expressions
26709 ---------------------------------------------------------------------------*/
26710 
26711 /* Parse a requires expression
26712 
26713    requirement-expression:
26714        'requires' requirement-parameter-list [opt] requirement-body */
26715 static tree
cp_parser_requires_expression(cp_parser * parser)26716 cp_parser_requires_expression (cp_parser *parser)
26717 {
26718   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26719   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26720 
26721   /* A requires-expression shall appear only within a concept
26722      definition or a requires-clause.
26723 
26724      TODO: Implement this diagnostic correctly. */
26725   if (!processing_template_decl)
26726     {
26727       error_at (loc, "a requires expression cannot appear outside a template");
26728       cp_parser_skip_to_end_of_statement (parser);
26729       return error_mark_node;
26730     }
26731 
26732   tree parms, reqs;
26733   {
26734     /* Local parameters are delared as variables within the scope
26735        of the expression.  They are not visible past the end of
26736        the expression.  Expressions within the requires-expression
26737        are unevaluated.  */
26738     struct scope_sentinel
26739     {
26740       scope_sentinel ()
26741       {
26742 	++cp_unevaluated_operand;
26743 	begin_scope (sk_block, NULL_TREE);
26744       }
26745 
26746       ~scope_sentinel ()
26747       {
26748 	pop_bindings_and_leave_scope ();
26749 	--cp_unevaluated_operand;
26750       }
26751     } s;
26752 
26753     /* Parse the optional parameter list. */
26754     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26755       {
26756 	parms = cp_parser_requirement_parameter_list (parser);
26757 	if (parms == error_mark_node)
26758 	  return error_mark_node;
26759       }
26760     else
26761       parms = NULL_TREE;
26762 
26763     /* Parse the requirement body. */
26764     reqs = cp_parser_requirement_body (parser);
26765     if (reqs == error_mark_node)
26766       return error_mark_node;
26767   }
26768 
26769   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26770      the parm chain.  */
26771   grokparms (parms, &parms);
26772   return finish_requires_expr (parms, reqs);
26773 }
26774 
26775 /* Parse a parameterized requirement.
26776 
26777    requirement-parameter-list:
26778        '(' parameter-declaration-clause ')' */
26779 static tree
cp_parser_requirement_parameter_list(cp_parser * parser)26780 cp_parser_requirement_parameter_list (cp_parser *parser)
26781 {
26782   matching_parens parens;
26783   if (!parens.require_open (parser))
26784     return error_mark_node;
26785 
26786   tree parms
26787     = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26788 
26789   if (!parens.require_close (parser))
26790     return error_mark_node;
26791 
26792   return parms;
26793 }
26794 
26795 /* Parse the body of a requirement.
26796 
26797    requirement-body:
26798        '{' requirement-list '}' */
26799 static tree
cp_parser_requirement_body(cp_parser * parser)26800 cp_parser_requirement_body (cp_parser *parser)
26801 {
26802   matching_braces braces;
26803   if (!braces.require_open (parser))
26804     return error_mark_node;
26805 
26806   tree reqs = cp_parser_requirement_list (parser);
26807 
26808   if (!braces.require_close (parser))
26809     return error_mark_node;
26810 
26811   return reqs;
26812 }
26813 
26814 /* Parse a list of requirements.
26815 
26816    requirement-list:
26817        requirement
26818        requirement-list ';' requirement[opt] */
26819 static tree
cp_parser_requirement_list(cp_parser * parser)26820 cp_parser_requirement_list (cp_parser *parser)
26821 {
26822   tree result = NULL_TREE;
26823   while (true)
26824     {
26825       tree req = cp_parser_requirement (parser);
26826       if (req == error_mark_node)
26827         return error_mark_node;
26828 
26829       result = tree_cons (NULL_TREE, req, result);
26830 
26831       /* If we see a semi-colon, consume it. */
26832       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26833 	cp_lexer_consume_token (parser->lexer);
26834 
26835       /* Stop processing at the end of the list. */
26836       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26837         break;
26838     }
26839 
26840   /* Reverse the order of requirements so they are analyzed in
26841      declaration order. */
26842   return nreverse (result);
26843 }
26844 
26845 /* Parse a syntactic requirement or type requirement.
26846 
26847      requirement:
26848        simple-requirement
26849        compound-requirement
26850        type-requirement
26851        nested-requirement */
26852 static tree
cp_parser_requirement(cp_parser * parser)26853 cp_parser_requirement (cp_parser *parser)
26854 {
26855   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26856     return cp_parser_compound_requirement (parser);
26857   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26858     return cp_parser_type_requirement (parser);
26859   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26860     return cp_parser_nested_requirement (parser);
26861   else
26862     return cp_parser_simple_requirement (parser);
26863 }
26864 
26865 /* Parse a simple requirement.
26866 
26867      simple-requirement:
26868        expression ';' */
26869 static tree
cp_parser_simple_requirement(cp_parser * parser)26870 cp_parser_simple_requirement (cp_parser *parser)
26871 {
26872   tree expr = cp_parser_expression (parser, NULL, false, false);
26873   if (!expr || expr == error_mark_node)
26874     return error_mark_node;
26875 
26876   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26877     return error_mark_node;
26878 
26879   return finish_simple_requirement (expr);
26880 }
26881 
26882 /* Parse a type requirement
26883 
26884      type-requirement
26885          nested-name-specifier [opt] required-type-name ';'
26886 
26887      required-type-name:
26888          type-name
26889          'template' [opt] simple-template-id  */
26890 static tree
cp_parser_type_requirement(cp_parser * parser)26891 cp_parser_type_requirement (cp_parser *parser)
26892 {
26893   cp_lexer_consume_token (parser->lexer);
26894 
26895   // Save the scope before parsing name specifiers.
26896   tree saved_scope = parser->scope;
26897   tree saved_object_scope = parser->object_scope;
26898   tree saved_qualifying_scope = parser->qualifying_scope;
26899   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26900   cp_parser_nested_name_specifier_opt (parser,
26901                                        /*typename_keyword_p=*/true,
26902                                        /*check_dependency_p=*/false,
26903                                        /*type_p=*/true,
26904                                        /*is_declaration=*/false);
26905 
26906   tree type;
26907   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26908     {
26909       cp_lexer_consume_token (parser->lexer);
26910       type = cp_parser_template_id (parser,
26911                                     /*template_keyword_p=*/true,
26912                                     /*check_dependency=*/false,
26913                                     /*tag_type=*/none_type,
26914                                     /*is_declaration=*/false);
26915       type = make_typename_type (parser->scope, type, typename_type,
26916                                  /*complain=*/tf_error);
26917     }
26918   else
26919    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26920 
26921   if (TREE_CODE (type) == TYPE_DECL)
26922     type = TREE_TYPE (type);
26923 
26924   parser->scope = saved_scope;
26925   parser->object_scope = saved_object_scope;
26926   parser->qualifying_scope = saved_qualifying_scope;
26927 
26928   if (type == error_mark_node)
26929     cp_parser_skip_to_end_of_statement (parser);
26930 
26931   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26932     return error_mark_node;
26933   if (type == error_mark_node)
26934     return error_mark_node;
26935 
26936   return finish_type_requirement (type);
26937 }
26938 
26939 /* Parse a compound requirement
26940 
26941      compound-requirement:
26942          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26943 static tree
cp_parser_compound_requirement(cp_parser * parser)26944 cp_parser_compound_requirement (cp_parser *parser)
26945 {
26946   /* Parse an expression enclosed in '{ }'s. */
26947   matching_braces braces;
26948   if (!braces.require_open (parser))
26949     return error_mark_node;
26950 
26951   tree expr = cp_parser_expression (parser, NULL, false, false);
26952   if (!expr || expr == error_mark_node)
26953     return error_mark_node;
26954 
26955   if (!braces.require_close (parser))
26956     return error_mark_node;
26957 
26958   /* Parse the optional noexcept. */
26959   bool noexcept_p = false;
26960   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26961     {
26962       cp_lexer_consume_token (parser->lexer);
26963       noexcept_p = true;
26964     }
26965 
26966   /* Parse the optional trailing return type. */
26967   tree type = NULL_TREE;
26968   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26969     {
26970       cp_lexer_consume_token (parser->lexer);
26971       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26972       parser->in_result_type_constraint_p = true;
26973       type = cp_parser_trailing_type_id (parser);
26974       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26975       if (type == error_mark_node)
26976         return error_mark_node;
26977     }
26978 
26979   return finish_compound_requirement (expr, type, noexcept_p);
26980 }
26981 
26982 /* Parse a nested requirement. This is the same as a requires clause.
26983 
26984    nested-requirement:
26985      requires-clause */
26986 static tree
cp_parser_nested_requirement(cp_parser * parser)26987 cp_parser_nested_requirement (cp_parser *parser)
26988 {
26989   cp_lexer_consume_token (parser->lexer);
26990   tree req = cp_parser_requires_clause (parser);
26991   if (req == error_mark_node)
26992     return error_mark_node;
26993   return finish_nested_requirement (req);
26994 }
26995 
26996 /* Support Functions */
26997 
26998 /* Return the appropriate prefer_type argument for lookup_name_real based on
26999    tag_type and template_mem_access.  */
27000 
27001 static inline int
27002 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
27003 {
27004   /* DR 141: When looking in the current enclosing context for a template-name
27005      after -> or ., only consider class templates.  */
27006   if (template_mem_access)
27007     return 2;
27008   switch (tag_type)
27009     {
27010     case none_type:  return 0;	// No preference.
27011     case scope_type: return 1;	// Type or namespace.
27012     default:         return 2;	// Type only.
27013     }
27014 }
27015 
27016 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
27017    NAME should have one of the representations used for an
27018    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
27019    is returned.  If PARSER->SCOPE is a dependent type, then a
27020    SCOPE_REF is returned.
27021 
27022    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
27023    returned; the name was already resolved when the TEMPLATE_ID_EXPR
27024    was formed.  Abstractly, such entities should not be passed to this
27025    function, because they do not need to be looked up, but it is
27026    simpler to check for this special case here, rather than at the
27027    call-sites.
27028 
27029    In cases not explicitly covered above, this function returns a
27030    DECL, OVERLOAD, or baselink representing the result of the lookup.
27031    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
27032    is returned.
27033 
27034    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
27035    (e.g., "struct") that was used.  In that case bindings that do not
27036    refer to types are ignored.
27037 
27038    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
27039    ignored.
27040 
27041    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
27042    are ignored.
27043 
27044    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
27045    types.
27046 
27047    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
27048    TREE_LIST of candidates if name-lookup results in an ambiguity, and
27049    NULL_TREE otherwise.  */
27050 
27051 static cp_expr
cp_parser_lookup_name(cp_parser * parser,tree name,enum tag_types tag_type,bool is_template,bool is_namespace,bool check_dependency,tree * ambiguous_decls,location_t name_location)27052 cp_parser_lookup_name (cp_parser *parser, tree name,
27053 		       enum tag_types tag_type,
27054 		       bool is_template,
27055 		       bool is_namespace,
27056 		       bool check_dependency,
27057 		       tree *ambiguous_decls,
27058 		       location_t name_location)
27059 {
27060   tree decl;
27061   tree object_type = parser->context->object_type;
27062 
27063   /* Assume that the lookup will be unambiguous.  */
27064   if (ambiguous_decls)
27065     *ambiguous_decls = NULL_TREE;
27066 
27067   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27068      no longer valid.  Note that if we are parsing tentatively, and
27069      the parse fails, OBJECT_TYPE will be automatically restored.  */
27070   parser->context->object_type = NULL_TREE;
27071 
27072   if (name == error_mark_node)
27073     return error_mark_node;
27074 
27075   /* A template-id has already been resolved; there is no lookup to
27076      do.  */
27077   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
27078     return name;
27079   if (BASELINK_P (name))
27080     {
27081       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
27082 		  == TEMPLATE_ID_EXPR);
27083       return name;
27084     }
27085 
27086   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
27087      it should already have been checked to make sure that the name
27088      used matches the type being destroyed.  */
27089   if (TREE_CODE (name) == BIT_NOT_EXPR)
27090     {
27091       tree type;
27092 
27093       /* Figure out to which type this destructor applies.  */
27094       if (parser->scope)
27095 	type = parser->scope;
27096       else if (object_type)
27097 	type = object_type;
27098       else
27099 	type = current_class_type;
27100       /* If that's not a class type, there is no destructor.  */
27101       if (!type || !CLASS_TYPE_P (type))
27102 	return error_mark_node;
27103 
27104       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
27105 	lazily_declare_fn (sfk_destructor, type);
27106 
27107       if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
27108 	return dtor;
27109 
27110       return error_mark_node;
27111     }
27112 
27113   /* By this point, the NAME should be an ordinary identifier.  If
27114      the id-expression was a qualified name, the qualifying scope is
27115      stored in PARSER->SCOPE at this point.  */
27116   gcc_assert (identifier_p (name));
27117 
27118   /* Perform the lookup.  */
27119   if (parser->scope)
27120     {
27121       bool dependent_p;
27122 
27123       if (parser->scope == error_mark_node)
27124 	return error_mark_node;
27125 
27126       /* If the SCOPE is dependent, the lookup must be deferred until
27127 	 the template is instantiated -- unless we are explicitly
27128 	 looking up names in uninstantiated templates.  Even then, we
27129 	 cannot look up the name if the scope is not a class type; it
27130 	 might, for example, be a template type parameter.  */
27131       dependent_p = (TYPE_P (parser->scope)
27132 		     && dependent_scope_p (parser->scope));
27133       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
27134 	  && dependent_p)
27135 	/* Defer lookup.  */
27136 	decl = error_mark_node;
27137       else
27138 	{
27139 	  tree pushed_scope = NULL_TREE;
27140 
27141 	  /* If PARSER->SCOPE is a dependent type, then it must be a
27142 	     class type, and we must not be checking dependencies;
27143 	     otherwise, we would have processed this lookup above.  So
27144 	     that PARSER->SCOPE is not considered a dependent base by
27145 	     lookup_member, we must enter the scope here.  */
27146 	  if (dependent_p)
27147 	    pushed_scope = push_scope (parser->scope);
27148 
27149 	  /* If the PARSER->SCOPE is a template specialization, it
27150 	     may be instantiated during name lookup.  In that case,
27151 	     errors may be issued.  Even if we rollback the current
27152 	     tentative parse, those errors are valid.  */
27153 	  decl = lookup_qualified_name (parser->scope, name,
27154 					prefer_type_arg (tag_type),
27155 					/*complain=*/true);
27156 
27157 	  /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27158 	     lookup result and the nested-name-specifier nominates a class C:
27159 	       * if the name specified after the nested-name-specifier, when
27160 	       looked up in C, is the injected-class-name of C (Clause 9), or
27161 	       * if the name specified after the nested-name-specifier is the
27162 	       same as the identifier or the simple-template-id's template-
27163 	       name in the last component of the nested-name-specifier,
27164 	     the name is instead considered to name the constructor of
27165 	     class C. [ Note: for example, the constructor is not an
27166 	     acceptable lookup result in an elaborated-type-specifier so
27167 	     the constructor would not be used in place of the
27168 	     injected-class-name. --end note ] Such a constructor name
27169 	     shall be used only in the declarator-id of a declaration that
27170 	     names a constructor or in a using-declaration.  */
27171 	  if (tag_type == none_type
27172 	      && DECL_SELF_REFERENCE_P (decl)
27173 	      && same_type_p (DECL_CONTEXT (decl), parser->scope))
27174 	    decl = lookup_qualified_name (parser->scope, ctor_identifier,
27175 					  prefer_type_arg (tag_type),
27176 					  /*complain=*/true);
27177 
27178 	  /* If we have a single function from a using decl, pull it out.  */
27179 	  if (TREE_CODE (decl) == OVERLOAD
27180 	      && !really_overloaded_fn (decl))
27181 	    decl = OVL_FUNCTION (decl);
27182 
27183 	  if (pushed_scope)
27184 	    pop_scope (pushed_scope);
27185 	}
27186 
27187       /* If the scope is a dependent type and either we deferred lookup or
27188 	 we did lookup but didn't find the name, rememeber the name.  */
27189       if (decl == error_mark_node && TYPE_P (parser->scope)
27190 	  && dependent_type_p (parser->scope))
27191 	{
27192 	  if (tag_type)
27193 	    {
27194 	      tree type;
27195 
27196 	      /* The resolution to Core Issue 180 says that `struct
27197 		 A::B' should be considered a type-name, even if `A'
27198 		 is dependent.  */
27199 	      type = make_typename_type (parser->scope, name, tag_type,
27200 					 /*complain=*/tf_error);
27201 	      if (type != error_mark_node)
27202 		decl = TYPE_NAME (type);
27203 	    }
27204 	  else if (is_template
27205 		   && (cp_parser_next_token_ends_template_argument_p (parser)
27206 		       || cp_lexer_next_token_is (parser->lexer,
27207 						  CPP_CLOSE_PAREN)))
27208 	    decl = make_unbound_class_template (parser->scope,
27209 						name, NULL_TREE,
27210 						/*complain=*/tf_error);
27211 	  else
27212 	    decl = build_qualified_name (/*type=*/NULL_TREE,
27213 					 parser->scope, name,
27214 					 is_template);
27215 	}
27216       parser->qualifying_scope = parser->scope;
27217       parser->object_scope = NULL_TREE;
27218     }
27219   else if (object_type)
27220     {
27221       /* Look up the name in the scope of the OBJECT_TYPE, unless the
27222 	 OBJECT_TYPE is not a class.  */
27223       if (CLASS_TYPE_P (object_type))
27224 	/* If the OBJECT_TYPE is a template specialization, it may
27225 	   be instantiated during name lookup.  In that case, errors
27226 	   may be issued.  Even if we rollback the current tentative
27227 	   parse, those errors are valid.  */
27228 	decl = lookup_member (object_type,
27229 			      name,
27230 			      /*protect=*/0,
27231 			      prefer_type_arg (tag_type),
27232 			      tf_warning_or_error);
27233       else
27234 	decl = NULL_TREE;
27235 
27236       if (!decl)
27237 	/* Look it up in the enclosing context.  DR 141: When looking for a
27238 	   template-name after -> or ., only consider class templates.  */
27239 	decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27240 				 /*nonclass=*/0,
27241 				 /*block_p=*/true, is_namespace, 0);
27242       if (object_type == unknown_type_node)
27243 	/* The object is type-dependent, so we can't look anything up; we used
27244 	   this to get the DR 141 behavior.  */
27245 	object_type = NULL_TREE;
27246       parser->object_scope = object_type;
27247       parser->qualifying_scope = NULL_TREE;
27248     }
27249   else
27250     {
27251       decl = lookup_name_real (name, prefer_type_arg (tag_type),
27252 			       /*nonclass=*/0,
27253 			       /*block_p=*/true, is_namespace, 0);
27254       parser->qualifying_scope = NULL_TREE;
27255       parser->object_scope = NULL_TREE;
27256     }
27257 
27258   /* If the lookup failed, let our caller know.  */
27259   if (!decl || decl == error_mark_node)
27260     return error_mark_node;
27261 
27262   /* Pull out the template from an injected-class-name (or multiple).  */
27263   if (is_template)
27264     decl = maybe_get_template_decl_from_type_decl (decl);
27265 
27266   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
27267   if (TREE_CODE (decl) == TREE_LIST)
27268     {
27269       if (ambiguous_decls)
27270 	*ambiguous_decls = decl;
27271       /* The error message we have to print is too complicated for
27272 	 cp_parser_error, so we incorporate its actions directly.  */
27273       if (!cp_parser_simulate_error (parser))
27274 	{
27275 	  error_at (name_location, "reference to %qD is ambiguous",
27276 		    name);
27277 	  print_candidates (decl);
27278 	}
27279       return error_mark_node;
27280     }
27281 
27282   gcc_assert (DECL_P (decl)
27283 	      || TREE_CODE (decl) == OVERLOAD
27284 	      || TREE_CODE (decl) == SCOPE_REF
27285 	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27286 	      || BASELINK_P (decl));
27287 
27288   /* If we have resolved the name of a member declaration, check to
27289      see if the declaration is accessible.  When the name resolves to
27290      set of overloaded functions, accessibility is checked when
27291      overload resolution is done.
27292 
27293      During an explicit instantiation, access is not checked at all,
27294      as per [temp.explicit].  */
27295   if (DECL_P (decl))
27296     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27297 
27298   maybe_record_typedef_use (decl);
27299 
27300   return cp_expr (decl, name_location);
27301 }
27302 
27303 /* Like cp_parser_lookup_name, but for use in the typical case where
27304    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27305    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
27306 
27307 static tree
cp_parser_lookup_name_simple(cp_parser * parser,tree name,location_t location)27308 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27309 {
27310   return cp_parser_lookup_name (parser, name,
27311 				none_type,
27312 				/*is_template=*/false,
27313 				/*is_namespace=*/false,
27314 				/*check_dependency=*/true,
27315 				/*ambiguous_decls=*/NULL,
27316 				location);
27317 }
27318 
27319 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27320    the current context, return the TYPE_DECL.  If TAG_NAME_P is
27321    true, the DECL indicates the class being defined in a class-head,
27322    or declared in an elaborated-type-specifier.
27323 
27324    Otherwise, return DECL.  */
27325 
27326 static tree
cp_parser_maybe_treat_template_as_class(tree decl,bool tag_name_p)27327 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27328 {
27329   /* If the TEMPLATE_DECL is being declared as part of a class-head,
27330      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27331 
27332        struct A {
27333 	 template <typename T> struct B;
27334        };
27335 
27336        template <typename T> struct A::B {};
27337 
27338      Similarly, in an elaborated-type-specifier:
27339 
27340        namespace N { struct X{}; }
27341 
27342        struct A {
27343 	 template <typename T> friend struct N::X;
27344        };
27345 
27346      However, if the DECL refers to a class type, and we are in
27347      the scope of the class, then the name lookup automatically
27348      finds the TYPE_DECL created by build_self_reference rather
27349      than a TEMPLATE_DECL.  For example, in:
27350 
27351        template <class T> struct S {
27352 	 S s;
27353        };
27354 
27355      there is no need to handle such case.  */
27356 
27357   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27358     return DECL_TEMPLATE_RESULT (decl);
27359 
27360   return decl;
27361 }
27362 
27363 /* If too many, or too few, template-parameter lists apply to the
27364    declarator, issue an error message.  Returns TRUE if all went well,
27365    and FALSE otherwise.  */
27366 
27367 static bool
cp_parser_check_declarator_template_parameters(cp_parser * parser,cp_declarator * declarator,location_t declarator_location)27368 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27369 						cp_declarator *declarator,
27370 						location_t declarator_location)
27371 {
27372   switch (declarator->kind)
27373     {
27374     case cdk_id:
27375       {
27376 	unsigned num_templates = 0;
27377 	tree scope = declarator->u.id.qualifying_scope;
27378 	bool template_id_p = false;
27379 
27380 	if (scope)
27381 	  num_templates = num_template_headers_for_class (scope);
27382 	else if (TREE_CODE (declarator->u.id.unqualified_name)
27383 		 == TEMPLATE_ID_EXPR)
27384 	  {
27385 	    /* If the DECLARATOR has the form `X<y>' then it uses one
27386 	       additional level of template parameters.  */
27387 	    ++num_templates;
27388 	    template_id_p = true;
27389 	  }
27390 
27391 	return cp_parser_check_template_parameters
27392 	  (parser, num_templates, template_id_p, declarator_location,
27393 	   declarator);
27394       }
27395 
27396     case cdk_function:
27397     case cdk_array:
27398     case cdk_pointer:
27399     case cdk_reference:
27400     case cdk_ptrmem:
27401       return (cp_parser_check_declarator_template_parameters
27402 	      (parser, declarator->declarator, declarator_location));
27403 
27404     case cdk_decomp:
27405     case cdk_error:
27406       return true;
27407 
27408     default:
27409       gcc_unreachable ();
27410     }
27411   return false;
27412 }
27413 
27414 /* NUM_TEMPLATES were used in the current declaration.  If that is
27415    invalid, return FALSE and issue an error messages.  Otherwise,
27416    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
27417    declarator and we can print more accurate diagnostics.  */
27418 
27419 static bool
cp_parser_check_template_parameters(cp_parser * parser,unsigned num_templates,bool template_id_p,location_t location,cp_declarator * declarator)27420 cp_parser_check_template_parameters (cp_parser* parser,
27421 				     unsigned num_templates,
27422 				     bool template_id_p,
27423 				     location_t location,
27424 				     cp_declarator *declarator)
27425 {
27426   /* If there are the same number of template classes and parameter
27427      lists, that's OK.  */
27428   if (parser->num_template_parameter_lists == num_templates)
27429     return true;
27430   /* If there are more, but only one more, and the name ends in an identifier,
27431      then we are declaring a primary template.  That's OK too.  */
27432   if (!template_id_p
27433       && parser->num_template_parameter_lists == num_templates + 1)
27434     return true;
27435   /* If there are more template classes than parameter lists, we have
27436      something like:
27437 
27438        template <class T> void S<T>::R<T>::f ();  */
27439   if (parser->num_template_parameter_lists < num_templates)
27440     {
27441       if (declarator && !current_function_decl)
27442 	error_at (location, "specializing member %<%T::%E%> "
27443 		  "requires %<template<>%> syntax",
27444 		  declarator->u.id.qualifying_scope,
27445 		  declarator->u.id.unqualified_name);
27446       else if (declarator)
27447 	error_at (location, "invalid declaration of %<%T::%E%>",
27448 		  declarator->u.id.qualifying_scope,
27449 		  declarator->u.id.unqualified_name);
27450       else
27451 	error_at (location, "too few template-parameter-lists");
27452       return false;
27453     }
27454   /* Otherwise, there are too many template parameter lists.  We have
27455      something like:
27456 
27457      template <class T> template <class U> void S::f();  */
27458   error_at (location, "too many template-parameter-lists");
27459   return false;
27460 }
27461 
27462 /* Parse an optional `::' token indicating that the following name is
27463    from the global namespace.  If so, PARSER->SCOPE is set to the
27464    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27465    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27466    Returns the new value of PARSER->SCOPE, if the `::' token is
27467    present, and NULL_TREE otherwise.  */
27468 
27469 static tree
cp_parser_global_scope_opt(cp_parser * parser,bool current_scope_valid_p)27470 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27471 {
27472   cp_token *token;
27473 
27474   /* Peek at the next token.  */
27475   token = cp_lexer_peek_token (parser->lexer);
27476   /* If we're looking at a `::' token then we're starting from the
27477      global namespace, not our current location.  */
27478   if (token->type == CPP_SCOPE)
27479     {
27480       /* Consume the `::' token.  */
27481       cp_lexer_consume_token (parser->lexer);
27482       /* Set the SCOPE so that we know where to start the lookup.  */
27483       parser->scope = global_namespace;
27484       parser->qualifying_scope = global_namespace;
27485       parser->object_scope = NULL_TREE;
27486 
27487       return parser->scope;
27488     }
27489   else if (!current_scope_valid_p)
27490     {
27491       parser->scope = NULL_TREE;
27492       parser->qualifying_scope = NULL_TREE;
27493       parser->object_scope = NULL_TREE;
27494     }
27495 
27496   return NULL_TREE;
27497 }
27498 
27499 /* Returns TRUE if the upcoming token sequence is the start of a
27500    constructor declarator or C++17 deduction guide.  If FRIEND_P is true, the
27501    declarator is preceded by the `friend' specifier.  The parser flags FLAGS
27502    is used to control type-specifier parsing.  */
27503 
27504 static bool
cp_parser_constructor_declarator_p(cp_parser * parser,cp_parser_flags flags,bool friend_p)27505 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
27506 				    bool friend_p)
27507 {
27508   bool constructor_p;
27509   bool outside_class_specifier_p;
27510   tree nested_name_specifier;
27511   cp_token *next_token;
27512 
27513   /* The common case is that this is not a constructor declarator, so
27514      try to avoid doing lots of work if at all possible.  It's not
27515      valid declare a constructor at function scope.  */
27516   if (parser->in_function_body)
27517     return false;
27518   /* And only certain tokens can begin a constructor declarator.  */
27519   next_token = cp_lexer_peek_token (parser->lexer);
27520   if (next_token->type != CPP_NAME
27521       && next_token->type != CPP_SCOPE
27522       && next_token->type != CPP_NESTED_NAME_SPECIFIER
27523       && next_token->type != CPP_TEMPLATE_ID)
27524     return false;
27525 
27526   /* Parse tentatively; we are going to roll back all of the tokens
27527      consumed here.  */
27528   cp_parser_parse_tentatively (parser);
27529   /* Assume that we are looking at a constructor declarator.  */
27530   constructor_p = true;
27531 
27532   /* Look for the optional `::' operator.  */
27533   cp_parser_global_scope_opt (parser,
27534 			      /*current_scope_valid_p=*/false);
27535   /* Look for the nested-name-specifier.  */
27536   nested_name_specifier
27537     = (cp_parser_nested_name_specifier_opt (parser,
27538 					    /*typename_keyword_p=*/false,
27539 					    /*check_dependency_p=*/false,
27540 					    /*type_p=*/false,
27541 					    /*is_declaration=*/false));
27542 
27543   /* Resolve the TYPENAME_TYPE, because the call above didn't do it.  */
27544   if (nested_name_specifier
27545       && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27546     {
27547       tree s = resolve_typename_type (nested_name_specifier,
27548 				      /*only_current_p=*/false);
27549       if (TREE_CODE (s) != TYPENAME_TYPE)
27550 	nested_name_specifier = s;
27551     }
27552 
27553   outside_class_specifier_p = (!at_class_scope_p ()
27554 			       || !TYPE_BEING_DEFINED (current_class_type)
27555 			       || friend_p);
27556 
27557   /* Outside of a class-specifier, there must be a
27558      nested-name-specifier.  Except in C++17 mode, where we
27559      might be declaring a guiding declaration.  */
27560   if (!nested_name_specifier && outside_class_specifier_p
27561       && cxx_dialect < cxx17)
27562     constructor_p = false;
27563   else if (nested_name_specifier == error_mark_node)
27564     constructor_p = false;
27565 
27566   /* If we have a class scope, this is easy; DR 147 says that S::S always
27567      names the constructor, and no other qualified name could.  */
27568   if (constructor_p && nested_name_specifier
27569       && CLASS_TYPE_P (nested_name_specifier))
27570     {
27571       tree id = cp_parser_unqualified_id (parser,
27572 					  /*template_keyword_p=*/false,
27573 					  /*check_dependency_p=*/false,
27574 					  /*declarator_p=*/true,
27575 					  /*optional_p=*/false);
27576       if (is_overloaded_fn (id))
27577 	id = DECL_NAME (get_first_fn (id));
27578       if (!constructor_name_p (id, nested_name_specifier))
27579 	constructor_p = false;
27580     }
27581   /* If we still think that this might be a constructor-declarator,
27582      look for a class-name.  */
27583   else if (constructor_p)
27584     {
27585       /* If we have:
27586 
27587 	   template <typename T> struct S {
27588 	     S();
27589 	   };
27590 
27591 	 we must recognize that the nested `S' names a class.  */
27592       if (cxx_dialect >= cxx17)
27593 	cp_parser_parse_tentatively (parser);
27594 
27595       tree type_decl;
27596       type_decl = cp_parser_class_name (parser,
27597 					/*typename_keyword_p=*/false,
27598 					/*template_keyword_p=*/false,
27599 					none_type,
27600 					/*check_dependency_p=*/false,
27601 					/*class_head_p=*/false,
27602 					/*is_declaration=*/false);
27603 
27604       if (cxx_dialect >= cxx17
27605 	  && !cp_parser_parse_definitely (parser))
27606 	{
27607 	  type_decl = NULL_TREE;
27608 	  tree tmpl = cp_parser_template_name (parser,
27609 					       /*template_keyword*/false,
27610 					       /*check_dependency_p*/false,
27611 					       /*is_declaration*/false,
27612 					       none_type,
27613 					       /*is_identifier*/NULL);
27614 	  if (DECL_CLASS_TEMPLATE_P (tmpl)
27615 	      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27616 	    /* It's a deduction guide, return true.  */;
27617 	  else
27618 	    cp_parser_simulate_error (parser);
27619 	}
27620 
27621       /* If there was no class-name, then this is not a constructor.
27622 	 Otherwise, if we are in a class-specifier and we aren't
27623 	 handling a friend declaration, check that its type matches
27624 	 current_class_type (c++/38313).  Note: error_mark_node
27625 	 is left alone for error recovery purposes.  */
27626       constructor_p = (!cp_parser_error_occurred (parser)
27627 		       && (outside_class_specifier_p
27628 			   || type_decl == NULL_TREE
27629 			   || type_decl == error_mark_node
27630 			   || same_type_p (current_class_type,
27631 					   TREE_TYPE (type_decl))));
27632 
27633       /* If we're still considering a constructor, we have to see a `(',
27634 	 to begin the parameter-declaration-clause, followed by either a
27635 	 `)', an `...', or a decl-specifier.  We need to check for a
27636 	 type-specifier to avoid being fooled into thinking that:
27637 
27638 	   S (f) (int);
27639 
27640 	 is a constructor.  (It is actually a function named `f' that
27641 	 takes one parameter (of type `int') and returns a value of type
27642 	 `S'.  */
27643       if (constructor_p
27644 	  && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27645 	constructor_p = false;
27646 
27647       if (constructor_p
27648 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27649 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27650 	  /* A parameter declaration begins with a decl-specifier,
27651 	     which is either the "attribute" keyword, a storage class
27652 	     specifier, or (usually) a type-specifier.  */
27653 	  && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
27654 	      /* GNU attributes can actually appear both at the start of
27655 		 a parameter and parenthesized declarator.
27656 		 S (__attribute__((unused)) int);
27657 		 is a constructor, but
27658 		 S (__attribute__((unused)) foo) (int);
27659 		 is a function declaration.  */
27660 	      || (cp_parser_allow_gnu_extensions_p (parser)
27661 		  && cp_next_tokens_can_be_gnu_attribute_p (parser)))
27662 	  /* A parameter declaration can also begin with [[attribute]].  */
27663 	  && !cp_next_tokens_can_be_std_attribute_p (parser))
27664 	{
27665 	  tree type;
27666 	  tree pushed_scope = NULL_TREE;
27667 	  unsigned saved_num_template_parameter_lists;
27668 
27669 	  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
27670 	    {
27671 	      unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
27672 	      while (--n)
27673 		cp_lexer_consume_token (parser->lexer);
27674 	    }
27675 
27676 	  /* Names appearing in the type-specifier should be looked up
27677 	     in the scope of the class.  */
27678 	  if (current_class_type)
27679 	    type = NULL_TREE;
27680 	  else if (type_decl)
27681 	    {
27682 	      type = TREE_TYPE (type_decl);
27683 	      if (TREE_CODE (type) == TYPENAME_TYPE)
27684 		{
27685 		  type = resolve_typename_type (type,
27686 						/*only_current_p=*/false);
27687 		  if (TREE_CODE (type) == TYPENAME_TYPE)
27688 		    {
27689 		      cp_parser_abort_tentative_parse (parser);
27690 		      return false;
27691 		    }
27692 		}
27693 	      pushed_scope = push_scope (type);
27694 	    }
27695 
27696 	  /* Inside the constructor parameter list, surrounding
27697 	     template-parameter-lists do not apply.  */
27698 	  saved_num_template_parameter_lists
27699 	    = parser->num_template_parameter_lists;
27700 	  parser->num_template_parameter_lists = 0;
27701 
27702 	  /* Look for the type-specifier.  It's not optional, but its typename
27703 	     might be.  Unless this is a friend declaration; we don't want to
27704 	     treat
27705 
27706 	       friend S (T::fn)(int);
27707 
27708 	     as a constructor, but with P0634, we might assume a type when
27709 	     looking for the type-specifier.  It is actually a function named
27710 	     `T::fn' that takes one parameter (of type `int') and returns a
27711 	     value of type `S'.  Constructors can be friends, but they must
27712 	     use a qualified name.  */
27713 	  cp_parser_type_specifier (parser,
27714 				    (friend_p ? CP_PARSER_FLAGS_NONE
27715 				     : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
27716 				    /*decl_specs=*/NULL,
27717 				    /*is_declarator=*/true,
27718 				    /*declares_class_or_enum=*/NULL,
27719 				    /*is_cv_qualifier=*/NULL);
27720 
27721 	  parser->num_template_parameter_lists
27722 	    = saved_num_template_parameter_lists;
27723 
27724 	  /* Leave the scope of the class.  */
27725 	  if (pushed_scope)
27726 	    pop_scope (pushed_scope);
27727 
27728 	  constructor_p = !cp_parser_error_occurred (parser);
27729 	}
27730     }
27731 
27732   /* We did not really want to consume any tokens.  */
27733   cp_parser_abort_tentative_parse (parser);
27734 
27735   return constructor_p;
27736 }
27737 
27738 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27739    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
27740    they must be performed once we are in the scope of the function.
27741 
27742    Returns the function defined.  */
27743 
27744 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)27745 cp_parser_function_definition_from_specifiers_and_declarator
27746   (cp_parser* parser,
27747    cp_decl_specifier_seq *decl_specifiers,
27748    tree attributes,
27749    const cp_declarator *declarator)
27750 {
27751   tree fn;
27752   bool success_p;
27753 
27754   /* Begin the function-definition.  */
27755   success_p = start_function (decl_specifiers, declarator, attributes);
27756 
27757   /* The things we're about to see are not directly qualified by any
27758      template headers we've seen thus far.  */
27759   reset_specialization ();
27760 
27761   /* If there were names looked up in the decl-specifier-seq that we
27762      did not check, check them now.  We must wait until we are in the
27763      scope of the function to perform the checks, since the function
27764      might be a friend.  */
27765   perform_deferred_access_checks (tf_warning_or_error);
27766 
27767   if (success_p)
27768     {
27769       cp_finalize_omp_declare_simd (parser, current_function_decl);
27770       parser->omp_declare_simd = NULL;
27771       cp_finalize_oacc_routine (parser, current_function_decl, true);
27772       parser->oacc_routine = NULL;
27773     }
27774 
27775   if (!success_p)
27776     {
27777       /* Skip the entire function.  */
27778       cp_parser_skip_to_end_of_block_or_statement (parser);
27779       fn = error_mark_node;
27780     }
27781   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27782     {
27783       /* Seen already, skip it.  An error message has already been output.  */
27784       cp_parser_skip_to_end_of_block_or_statement (parser);
27785       fn = current_function_decl;
27786       current_function_decl = NULL_TREE;
27787       /* If this is a function from a class, pop the nested class.  */
27788       if (current_class_name)
27789 	pop_nested_class ();
27790     }
27791   else
27792     {
27793       timevar_id_t tv;
27794       if (DECL_DECLARED_INLINE_P (current_function_decl))
27795         tv = TV_PARSE_INLINE;
27796       else
27797         tv = TV_PARSE_FUNC;
27798       timevar_push (tv);
27799       fn = cp_parser_function_definition_after_declarator (parser,
27800 							 /*inline_p=*/false);
27801       timevar_pop (tv);
27802     }
27803 
27804   return fn;
27805 }
27806 
27807 /* Parse the part of a function-definition that follows the
27808    declarator.  INLINE_P is TRUE iff this function is an inline
27809    function defined within a class-specifier.
27810 
27811    Returns the function defined.  */
27812 
27813 static tree
cp_parser_function_definition_after_declarator(cp_parser * parser,bool inline_p)27814 cp_parser_function_definition_after_declarator (cp_parser* parser,
27815 						bool inline_p)
27816 {
27817   tree fn;
27818   bool saved_in_unbraced_linkage_specification_p;
27819   bool saved_in_function_body;
27820   unsigned saved_num_template_parameter_lists;
27821   cp_token *token;
27822   bool fully_implicit_function_template_p
27823     = parser->fully_implicit_function_template_p;
27824   parser->fully_implicit_function_template_p = false;
27825   tree implicit_template_parms
27826     = parser->implicit_template_parms;
27827   parser->implicit_template_parms = 0;
27828   cp_binding_level* implicit_template_scope
27829     = parser->implicit_template_scope;
27830   parser->implicit_template_scope = 0;
27831 
27832   saved_in_function_body = parser->in_function_body;
27833   parser->in_function_body = true;
27834   /* If the next token is `return', then the code may be trying to
27835      make use of the "named return value" extension that G++ used to
27836      support.  */
27837   token = cp_lexer_peek_token (parser->lexer);
27838   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27839     {
27840       /* Consume the `return' keyword.  */
27841       cp_lexer_consume_token (parser->lexer);
27842       /* Look for the identifier that indicates what value is to be
27843 	 returned.  */
27844       cp_parser_identifier (parser);
27845       /* Issue an error message.  */
27846       error_at (token->location,
27847 		"named return values are no longer supported");
27848       /* Skip tokens until we reach the start of the function body.  */
27849       while (true)
27850 	{
27851 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
27852 	  if (token->type == CPP_OPEN_BRACE
27853 	      || token->type == CPP_EOF
27854 	      || token->type == CPP_PRAGMA_EOL)
27855 	    break;
27856 	  cp_lexer_consume_token (parser->lexer);
27857 	}
27858     }
27859   /* The `extern' in `extern "C" void f () { ... }' does not apply to
27860      anything declared inside `f'.  */
27861   saved_in_unbraced_linkage_specification_p
27862     = parser->in_unbraced_linkage_specification_p;
27863   parser->in_unbraced_linkage_specification_p = false;
27864   /* Inside the function, surrounding template-parameter-lists do not
27865      apply.  */
27866   saved_num_template_parameter_lists
27867     = parser->num_template_parameter_lists;
27868   parser->num_template_parameter_lists = 0;
27869 
27870   /* If the next token is `try', `__transaction_atomic', or
27871      `__transaction_relaxed`, then we are looking at either function-try-block
27872      or function-transaction-block.  Note that all of these include the
27873      function-body.  */
27874   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27875     cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27876   else if (cp_lexer_next_token_is_keyword (parser->lexer,
27877       RID_TRANSACTION_RELAXED))
27878     cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27879   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27880     cp_parser_function_try_block (parser);
27881   else
27882     cp_parser_ctor_initializer_opt_and_function_body
27883       (parser, /*in_function_try_block=*/false);
27884 
27885   /* Finish the function.  */
27886   fn = finish_function (inline_p);
27887   /* Generate code for it, if necessary.  */
27888   expand_or_defer_fn (fn);
27889   /* Restore the saved values.  */
27890   parser->in_unbraced_linkage_specification_p
27891     = saved_in_unbraced_linkage_specification_p;
27892   parser->num_template_parameter_lists
27893     = saved_num_template_parameter_lists;
27894   parser->in_function_body = saved_in_function_body;
27895 
27896   parser->fully_implicit_function_template_p
27897     = fully_implicit_function_template_p;
27898   parser->implicit_template_parms
27899     = implicit_template_parms;
27900   parser->implicit_template_scope
27901     = implicit_template_scope;
27902 
27903   if (parser->fully_implicit_function_template_p)
27904     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27905 
27906   return fn;
27907 }
27908 
27909 /* Parse a template-declaration body (following argument list).  */
27910 
27911 static void
cp_parser_template_declaration_after_parameters(cp_parser * parser,tree parameter_list,bool member_p)27912 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27913 						 tree parameter_list,
27914 						 bool member_p)
27915 {
27916   tree decl = NULL_TREE;
27917   bool friend_p = false;
27918 
27919   /* We just processed one more parameter list.  */
27920   ++parser->num_template_parameter_lists;
27921 
27922   /* Get the deferred access checks from the parameter list.  These
27923      will be checked once we know what is being declared, as for a
27924      member template the checks must be performed in the scope of the
27925      class containing the member.  */
27926   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27927 
27928   /* Tentatively parse for a new template parameter list, which can either be
27929      the template keyword or a template introduction.  */
27930   if (cp_parser_template_declaration_after_export (parser, member_p))
27931     /* OK */;
27932   else if (cxx_dialect >= cxx11
27933 	   && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27934     decl = cp_parser_alias_declaration (parser);
27935   else
27936     {
27937       /* There are no access checks when parsing a template, as we do not
27938 	 know if a specialization will be a friend.  */
27939       push_deferring_access_checks (dk_no_check);
27940       cp_token *token = cp_lexer_peek_token (parser->lexer);
27941       decl = cp_parser_single_declaration (parser,
27942 					   checks,
27943 					   member_p,
27944                                            /*explicit_specialization_p=*/false,
27945 					   &friend_p);
27946       pop_deferring_access_checks ();
27947 
27948       /* If this is a member template declaration, let the front
27949 	 end know.  */
27950       if (member_p && !friend_p && decl)
27951 	{
27952 	  if (TREE_CODE (decl) == TYPE_DECL)
27953 	    cp_parser_check_access_in_redeclaration (decl, token->location);
27954 
27955 	  decl = finish_member_template_decl (decl);
27956 	}
27957       else if (friend_p && decl
27958 	       && DECL_DECLARES_TYPE_P (decl))
27959 	make_friend_class (current_class_type, TREE_TYPE (decl),
27960 			   /*complain=*/true);
27961     }
27962   /* We are done with the current parameter list.  */
27963   --parser->num_template_parameter_lists;
27964 
27965   pop_deferring_access_checks ();
27966 
27967   /* Finish up.  */
27968   finish_template_decl (parameter_list);
27969 
27970   /* Check the template arguments for a literal operator template.  */
27971   if (decl
27972       && DECL_DECLARES_FUNCTION_P (decl)
27973       && UDLIT_OPER_P (DECL_NAME (decl)))
27974     {
27975       bool ok = true;
27976       if (parameter_list == NULL_TREE)
27977 	ok = false;
27978       else
27979 	{
27980 	  int num_parms = TREE_VEC_LENGTH (parameter_list);
27981 	  if (num_parms == 1)
27982 	    {
27983 	      tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27984 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27985 	      if (TREE_CODE (parm) != PARM_DECL)
27986 		ok = false;
27987 	      else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
27988 		       && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27989 		/* OK, C++20 string literal operator template.  We don't need
27990 		   to warn in lower dialects here because we will have already
27991 		   warned about the template parameter.  */;
27992 	      else if (TREE_TYPE (parm) != char_type_node
27993 		       || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27994 		ok = false;
27995 	    }
27996 	  else if (num_parms == 2 && cxx_dialect >= cxx14)
27997 	    {
27998 	      tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27999 	      tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
28000 	      tree parm_list = TREE_VEC_ELT (parameter_list, 1);
28001 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
28002 	      if (TREE_CODE (parm) != PARM_DECL
28003 		  || TREE_TYPE (parm) != TREE_TYPE (type)
28004 		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
28005 		ok = false;
28006 	      else
28007 		/* http://cplusplus.github.io/EWG/ewg-active.html#66  */
28008 		pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
28009 			 "ISO C++ did not adopt string literal operator templa"
28010 			 "tes taking an argument pack of characters");
28011 	    }
28012 	  else
28013 	    ok = false;
28014 	}
28015       if (!ok)
28016 	{
28017 	  if (cxx_dialect > cxx17)
28018 	    error ("literal operator template %qD has invalid parameter list;"
28019 		   "  Expected non-type template parameter pack <char...> "
28020 		   "or single non-type parameter of class type",
28021 		   decl);
28022 	  else
28023 	    error ("literal operator template %qD has invalid parameter list."
28024 		   "  Expected non-type template parameter pack <char...>",
28025 		   decl);
28026 	}
28027     }
28028 
28029   /* Register member declarations.  */
28030   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
28031     finish_member_declaration (decl);
28032   /* If DECL is a function template, we must return to parse it later.
28033      (Even though there is no definition, there might be default
28034      arguments that need handling.)  */
28035   if (member_p && decl
28036       && DECL_DECLARES_FUNCTION_P (decl))
28037     vec_safe_push (unparsed_funs_with_definitions, decl);
28038 }
28039 
28040 /* Parse a template introduction header for a template-declaration.  Returns
28041    false if tentative parse fails.  */
28042 
28043 static bool
cp_parser_template_introduction(cp_parser * parser,bool member_p)28044 cp_parser_template_introduction (cp_parser* parser, bool member_p)
28045 {
28046   cp_parser_parse_tentatively (parser);
28047 
28048   tree saved_scope = parser->scope;
28049   tree saved_object_scope = parser->object_scope;
28050   tree saved_qualifying_scope = parser->qualifying_scope;
28051 
28052   /* Look for the optional `::' operator.  */
28053   cp_parser_global_scope_opt (parser,
28054 			      /*current_scope_valid_p=*/false);
28055   /* Look for the nested-name-specifier.  */
28056   cp_parser_nested_name_specifier_opt (parser,
28057 				       /*typename_keyword_p=*/false,
28058 				       /*check_dependency_p=*/true,
28059 				       /*type_p=*/false,
28060 				       /*is_declaration=*/false);
28061 
28062   cp_token *token = cp_lexer_peek_token (parser->lexer);
28063   tree concept_name = cp_parser_identifier (parser);
28064 
28065   /* Look up the concept for which we will be matching
28066      template parameters.  */
28067   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
28068 						 token->location);
28069   parser->scope = saved_scope;
28070   parser->object_scope = saved_object_scope;
28071   parser->qualifying_scope = saved_qualifying_scope;
28072 
28073   if (concept_name == error_mark_node)
28074     cp_parser_simulate_error (parser);
28075 
28076   /* Look for opening brace for introduction.  */
28077   matching_braces braces;
28078   braces.require_open (parser);
28079 
28080   if (!cp_parser_parse_definitely (parser))
28081     return false;
28082 
28083   push_deferring_access_checks (dk_deferred);
28084 
28085   /* Build vector of placeholder parameters and grab
28086      matching identifiers.  */
28087   tree introduction_list = cp_parser_introduction_list (parser);
28088 
28089   /* Look for closing brace for introduction.  */
28090   if (!braces.require_close (parser))
28091     return true;
28092 
28093   /* The introduction-list shall not be empty.  */
28094   int nargs = TREE_VEC_LENGTH (introduction_list);
28095   if (nargs == 0)
28096     {
28097       /* In cp_parser_introduction_list we have already issued an error.  */
28098       return true;
28099     }
28100 
28101   if (tmpl_decl == error_mark_node)
28102     {
28103       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
28104 				   token->location);
28105       return true;
28106     }
28107 
28108   /* Build and associate the constraint.  */
28109   tree parms = finish_template_introduction (tmpl_decl, introduction_list);
28110   if (parms && parms != error_mark_node)
28111     {
28112       cp_parser_template_declaration_after_parameters (parser, parms,
28113 						       member_p);
28114       return true;
28115     }
28116 
28117   error_at (token->location, "no matching concept for template-introduction");
28118   return true;
28119 }
28120 
28121 /* Parse a normal template-declaration following the template keyword.  */
28122 
28123 static void
cp_parser_explicit_template_declaration(cp_parser * parser,bool member_p)28124 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
28125 {
28126   tree parameter_list;
28127   bool need_lang_pop;
28128   location_t location = input_location;
28129 
28130   /* Look for the `<' token.  */
28131   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
28132     return;
28133   if (at_class_scope_p () && current_function_decl)
28134     {
28135       /* 14.5.2.2 [temp.mem]
28136 
28137          A local class shall not have member templates.  */
28138       error_at (location,
28139                 "invalid declaration of member template in local class");
28140       cp_parser_skip_to_end_of_block_or_statement (parser);
28141       return;
28142     }
28143   /* [temp]
28144 
28145      A template ... shall not have C linkage.  */
28146   if (current_lang_name == lang_name_c)
28147     {
28148       error_at (location, "template with C linkage");
28149       maybe_show_extern_c_location ();
28150       /* Give it C++ linkage to avoid confusing other parts of the
28151          front end.  */
28152       push_lang_context (lang_name_cplusplus);
28153       need_lang_pop = true;
28154     }
28155   else
28156     need_lang_pop = false;
28157 
28158   /* We cannot perform access checks on the template parameter
28159      declarations until we know what is being declared, just as we
28160      cannot check the decl-specifier list.  */
28161   push_deferring_access_checks (dk_deferred);
28162 
28163   /* If the next token is `>', then we have an invalid
28164      specialization.  Rather than complain about an invalid template
28165      parameter, issue an error message here.  */
28166   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
28167     {
28168       cp_parser_error (parser, "invalid explicit specialization");
28169       begin_specialization ();
28170       parameter_list = NULL_TREE;
28171     }
28172   else
28173     {
28174       /* Parse the template parameters.  */
28175       parameter_list = cp_parser_template_parameter_list (parser);
28176     }
28177 
28178   /* Look for the `>'.  */
28179   cp_parser_skip_to_end_of_template_parameter_list (parser);
28180 
28181   /* Manage template requirements */
28182   if (flag_concepts)
28183   {
28184     tree reqs = get_shorthand_constraints (current_template_parms);
28185     if (tree r = cp_parser_requires_clause_opt (parser))
28186       reqs = conjoin_constraints (reqs, normalize_expression (r));
28187     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
28188   }
28189 
28190   cp_parser_template_declaration_after_parameters (parser, parameter_list,
28191 						   member_p);
28192 
28193   /* For the erroneous case of a template with C linkage, we pushed an
28194      implicit C++ linkage scope; exit that scope now.  */
28195   if (need_lang_pop)
28196     pop_lang_context ();
28197 }
28198 
28199 /* Parse a template-declaration, assuming that the `export' (and
28200    `extern') keywords, if present, has already been scanned.  MEMBER_P
28201    is as for cp_parser_template_declaration.  */
28202 
28203 static bool
cp_parser_template_declaration_after_export(cp_parser * parser,bool member_p)28204 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
28205 {
28206   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28207     {
28208       cp_lexer_consume_token (parser->lexer);
28209       cp_parser_explicit_template_declaration (parser, member_p);
28210       return true;
28211     }
28212   else if (flag_concepts)
28213     return cp_parser_template_introduction (parser, member_p);
28214 
28215   return false;
28216 }
28217 
28218 /* Perform the deferred access checks from a template-parameter-list.
28219    CHECKS is a TREE_LIST of access checks, as returned by
28220    get_deferred_access_checks.  */
28221 
28222 static void
cp_parser_perform_template_parameter_access_checks(vec<deferred_access_check,va_gc> * checks)28223 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28224 {
28225   ++processing_template_parmlist;
28226   perform_access_checks (checks, tf_warning_or_error);
28227   --processing_template_parmlist;
28228 }
28229 
28230 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28231    `function-definition' sequence that follows a template header.
28232    If MEMBER_P is true, this declaration appears in a class scope.
28233 
28234    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
28235    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
28236 
28237 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)28238 cp_parser_single_declaration (cp_parser* parser,
28239 			      vec<deferred_access_check, va_gc> *checks,
28240 			      bool member_p,
28241                               bool explicit_specialization_p,
28242 			      bool* friend_p)
28243 {
28244   int declares_class_or_enum;
28245   tree decl = NULL_TREE;
28246   cp_decl_specifier_seq decl_specifiers;
28247   bool function_definition_p = false;
28248   cp_token *decl_spec_token_start;
28249 
28250   /* This function is only used when processing a template
28251      declaration.  */
28252   gcc_assert (innermost_scope_kind () == sk_template_parms
28253 	      || innermost_scope_kind () == sk_template_spec);
28254 
28255   /* Defer access checks until we know what is being declared.  */
28256   push_deferring_access_checks (dk_deferred);
28257 
28258   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28259      alternative.  */
28260   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28261   cp_parser_decl_specifier_seq (parser,
28262 				(CP_PARSER_FLAGS_OPTIONAL
28263 				 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28264 				&decl_specifiers,
28265 				&declares_class_or_enum);
28266   if (friend_p)
28267     *friend_p = cp_parser_friend_p (&decl_specifiers);
28268 
28269   /* There are no template typedefs.  */
28270   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28271     {
28272       error_at (decl_spec_token_start->location,
28273 		"template declaration of %<typedef%>");
28274       decl = error_mark_node;
28275     }
28276 
28277   /* Gather up the access checks that occurred the
28278      decl-specifier-seq.  */
28279   stop_deferring_access_checks ();
28280 
28281   /* Check for the declaration of a template class.  */
28282   if (declares_class_or_enum)
28283     {
28284       if (cp_parser_declares_only_class_p (parser)
28285 	  || (declares_class_or_enum & 2))
28286 	{
28287 	  // If this is a declaration, but not a definition, associate
28288 	  // any constraints with the type declaration. Constraints
28289 	  // are associated with definitions in cp_parser_class_specifier.
28290 	  if (declares_class_or_enum == 1)
28291 	    associate_classtype_constraints (decl_specifiers.type);
28292 
28293 	  decl = shadow_tag (&decl_specifiers);
28294 
28295 	  /* In this case:
28296 
28297 	       struct C {
28298 		 friend template <typename T> struct A<T>::B;
28299 	       };
28300 
28301 	     A<T>::B will be represented by a TYPENAME_TYPE, and
28302 	     therefore not recognized by shadow_tag.  */
28303 	  if (friend_p && *friend_p
28304 	      && !decl
28305 	      && decl_specifiers.type
28306 	      && TYPE_P (decl_specifiers.type))
28307 	    decl = decl_specifiers.type;
28308 
28309 	  if (decl && decl != error_mark_node)
28310 	    decl = TYPE_NAME (decl);
28311 	  else
28312 	    decl = error_mark_node;
28313 
28314 	  /* Perform access checks for template parameters.  */
28315 	  cp_parser_perform_template_parameter_access_checks (checks);
28316 
28317 	  /* Give a helpful diagnostic for
28318 	       template <class T> struct A { } a;
28319 	     if we aren't already recovering from an error.  */
28320 	  if (!cp_parser_declares_only_class_p (parser)
28321 	      && !seen_error ())
28322 	    {
28323 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
28324 			"a class template declaration must not declare "
28325 			"anything else");
28326 	      cp_parser_skip_to_end_of_block_or_statement (parser);
28327 	      goto out;
28328 	    }
28329 	}
28330     }
28331 
28332   /* Complain about missing 'typename' or other invalid type names.  */
28333   if (!decl_specifiers.any_type_specifiers_p
28334       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28335     {
28336       /* cp_parser_parse_and_diagnose_invalid_type_name calls
28337 	 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28338 	 the rest of this declaration.  */
28339       decl = error_mark_node;
28340       goto out;
28341     }
28342 
28343   /* If it's not a template class, try for a template function.  If
28344      the next token is a `;', then this declaration does not declare
28345      anything.  But, if there were errors in the decl-specifiers, then
28346      the error might well have come from an attempted class-specifier.
28347      In that case, there's no need to warn about a missing declarator.  */
28348   if (!decl
28349       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28350 	  || decl_specifiers.type != error_mark_node))
28351     {
28352       decl = cp_parser_init_declarator (parser,
28353 					CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28354 				        &decl_specifiers,
28355 				        checks,
28356 				        /*function_definition_allowed_p=*/true,
28357 				        member_p,
28358 				        declares_class_or_enum,
28359 				        &function_definition_p,
28360 					NULL, NULL, NULL);
28361 
28362     /* 7.1.1-1 [dcl.stc]
28363 
28364        A storage-class-specifier shall not be specified in an explicit
28365        specialization...  */
28366     if (decl
28367         && explicit_specialization_p
28368         && decl_specifiers.storage_class != sc_none)
28369       {
28370         error_at (decl_spec_token_start->location,
28371 		  "explicit template specialization cannot have a storage class");
28372         decl = error_mark_node;
28373       }
28374 
28375     if (decl && VAR_P (decl))
28376       check_template_variable (decl);
28377     }
28378 
28379   /* Look for a trailing `;' after the declaration.  */
28380   if (!function_definition_p
28381       && (decl == error_mark_node
28382 	  || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28383     cp_parser_skip_to_end_of_block_or_statement (parser);
28384 
28385  out:
28386   pop_deferring_access_checks ();
28387 
28388   /* Clear any current qualification; whatever comes next is the start
28389      of something new.  */
28390   parser->scope = NULL_TREE;
28391   parser->qualifying_scope = NULL_TREE;
28392   parser->object_scope = NULL_TREE;
28393 
28394   return decl;
28395 }
28396 
28397 /* Parse a cast-expression that is not the operand of a unary "&".  */
28398 
28399 static cp_expr
cp_parser_simple_cast_expression(cp_parser * parser)28400 cp_parser_simple_cast_expression (cp_parser *parser)
28401 {
28402   return cp_parser_cast_expression (parser, /*address_p=*/false,
28403 				    /*cast_p=*/false, /*decltype*/false, NULL);
28404 }
28405 
28406 /* Parse a functional cast to TYPE.  Returns an expression
28407    representing the cast.  */
28408 
28409 static cp_expr
cp_parser_functional_cast(cp_parser * parser,tree type)28410 cp_parser_functional_cast (cp_parser* parser, tree type)
28411 {
28412   vec<tree, va_gc> *vec;
28413   tree expression_list;
28414   cp_expr cast;
28415   bool nonconst_p;
28416 
28417   location_t start_loc = input_location;
28418 
28419   if (!type)
28420     type = error_mark_node;
28421 
28422   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28423     {
28424       cp_lexer_set_source_position (parser->lexer);
28425       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28426       expression_list = cp_parser_braced_list (parser, &nonconst_p);
28427       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28428       if (TREE_CODE (type) == TYPE_DECL)
28429 	type = TREE_TYPE (type);
28430 
28431       cast = finish_compound_literal (type, expression_list,
28432 				      tf_warning_or_error, fcl_functional);
28433       /* Create a location of the form:
28434 	    type_name{i, f}
28435 	    ^~~~~~~~~~~~~~~
28436 	 with caret == start at the start of the type name,
28437 	 finishing at the closing brace.  */
28438       location_t finish_loc
28439 	= get_finish (cp_lexer_previous_token (parser->lexer)->location);
28440       location_t combined_loc = make_location (start_loc, start_loc,
28441 					       finish_loc);
28442       cast.set_location (combined_loc);
28443       return cast;
28444    }
28445 
28446 
28447   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28448 						 /*cast_p=*/true,
28449 						 /*allow_expansion_p=*/true,
28450 						 /*non_constant_p=*/NULL);
28451   if (vec == NULL)
28452     expression_list = error_mark_node;
28453   else
28454     {
28455       expression_list = build_tree_list_vec (vec);
28456       release_tree_vector (vec);
28457     }
28458 
28459   cast = build_functional_cast (type, expression_list,
28460                                 tf_warning_or_error);
28461   /* [expr.const]/1: In an integral constant expression "only type
28462      conversions to integral or enumeration type can be used".  */
28463   if (TREE_CODE (type) == TYPE_DECL)
28464     type = TREE_TYPE (type);
28465   if (cast != error_mark_node
28466       && !cast_valid_in_integral_constant_expression_p (type)
28467       && cp_parser_non_integral_constant_expression (parser,
28468 						     NIC_CONSTRUCTOR))
28469     return error_mark_node;
28470 
28471   /* Create a location of the form:
28472        float(i)
28473        ^~~~~~~~
28474      with caret == start at the start of the type name,
28475      finishing at the closing paren.  */
28476   location_t finish_loc
28477     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28478   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28479   cast.set_location (combined_loc);
28480   return cast;
28481 }
28482 
28483 /* Save the tokens that make up the body of a member function defined
28484    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
28485    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
28486    specifiers applied to the declaration.  Returns the FUNCTION_DECL
28487    for the member function.  */
28488 
28489 static tree
cp_parser_save_member_function_body(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree attributes)28490 cp_parser_save_member_function_body (cp_parser* parser,
28491 				     cp_decl_specifier_seq *decl_specifiers,
28492 				     cp_declarator *declarator,
28493 				     tree attributes)
28494 {
28495   cp_token *first;
28496   cp_token *last;
28497   tree fn;
28498   bool function_try_block = false;
28499 
28500   /* Create the FUNCTION_DECL.  */
28501   fn = grokmethod (decl_specifiers, declarator, attributes);
28502   cp_finalize_omp_declare_simd (parser, fn);
28503   cp_finalize_oacc_routine (parser, fn, true);
28504   /* If something went badly wrong, bail out now.  */
28505   if (fn == error_mark_node)
28506     {
28507       /* If there's a function-body, skip it.  */
28508       if (cp_parser_token_starts_function_definition_p
28509 	  (cp_lexer_peek_token (parser->lexer)))
28510 	cp_parser_skip_to_end_of_block_or_statement (parser);
28511       return error_mark_node;
28512     }
28513 
28514   /* Remember it, if there default args to post process.  */
28515   cp_parser_save_default_args (parser, fn);
28516 
28517   /* Save away the tokens that make up the body of the
28518      function.  */
28519   first = parser->lexer->next_token;
28520 
28521   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28522     cp_lexer_consume_token (parser->lexer);
28523   else if (cp_lexer_next_token_is_keyword (parser->lexer,
28524 					   RID_TRANSACTION_ATOMIC))
28525     {
28526       cp_lexer_consume_token (parser->lexer);
28527       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
28528       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28529 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28530 	  && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28531 	      || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28532 	  && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28533 	  && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28534 	{
28535 	  cp_lexer_consume_token (parser->lexer);
28536 	  cp_lexer_consume_token (parser->lexer);
28537 	  cp_lexer_consume_token (parser->lexer);
28538 	  cp_lexer_consume_token (parser->lexer);
28539 	  cp_lexer_consume_token (parser->lexer);
28540 	}
28541       else
28542 	while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28543 	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28544 	  {
28545 	    cp_lexer_consume_token (parser->lexer);
28546 	    if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28547 	      break;
28548 	  }
28549     }
28550 
28551   /* Handle function try blocks.  */
28552   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28553     {
28554       cp_lexer_consume_token (parser->lexer);
28555       function_try_block = true;
28556     }
28557   /* We can have braced-init-list mem-initializers before the fn body.  */
28558   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28559     {
28560       cp_lexer_consume_token (parser->lexer);
28561       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28562 	{
28563 	  /* cache_group will stop after an un-nested { } pair, too.  */
28564 	  if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28565 	    break;
28566 
28567 	  /* variadic mem-inits have ... after the ')'.  */
28568 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28569 	    cp_lexer_consume_token (parser->lexer);
28570 	}
28571     }
28572   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28573   /* Handle function try blocks.  */
28574   if (function_try_block)
28575     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28576       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28577   last = parser->lexer->next_token;
28578 
28579   /* Save away the inline definition; we will process it when the
28580      class is complete.  */
28581   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28582   DECL_PENDING_INLINE_P (fn) = 1;
28583 
28584   /* We need to know that this was defined in the class, so that
28585      friend templates are handled correctly.  */
28586   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28587 
28588   /* Add FN to the queue of functions to be parsed later.  */
28589   vec_safe_push (unparsed_funs_with_definitions, fn);
28590 
28591   return fn;
28592 }
28593 
28594 /* Save the tokens that make up the in-class initializer for a non-static
28595    data member.  Returns a DEFAULT_ARG.  */
28596 
28597 static tree
cp_parser_save_nsdmi(cp_parser * parser)28598 cp_parser_save_nsdmi (cp_parser* parser)
28599 {
28600   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28601 }
28602 
28603 /* Parse a template-argument-list, as well as the trailing ">" (but
28604    not the opening "<").  See cp_parser_template_argument_list for the
28605    return value.  */
28606 
28607 static tree
cp_parser_enclosed_template_argument_list(cp_parser * parser)28608 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28609 {
28610   tree arguments;
28611   tree saved_scope;
28612   tree saved_qualifying_scope;
28613   tree saved_object_scope;
28614   bool saved_greater_than_is_operator_p;
28615 
28616   /* [temp.names]
28617 
28618      When parsing a template-id, the first non-nested `>' is taken as
28619      the end of the template-argument-list rather than a greater-than
28620      operator.  */
28621   saved_greater_than_is_operator_p
28622     = parser->greater_than_is_operator_p;
28623   parser->greater_than_is_operator_p = false;
28624   /* Parsing the argument list may modify SCOPE, so we save it
28625      here.  */
28626   saved_scope = parser->scope;
28627   saved_qualifying_scope = parser->qualifying_scope;
28628   saved_object_scope = parser->object_scope;
28629   /* We need to evaluate the template arguments, even though this
28630      template-id may be nested within a "sizeof".  */
28631   cp_evaluated ev;
28632   /* Parse the template-argument-list itself.  */
28633   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28634       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28635     arguments = NULL_TREE;
28636   else
28637     arguments = cp_parser_template_argument_list (parser);
28638   /* Look for the `>' that ends the template-argument-list. If we find
28639      a '>>' instead, it's probably just a typo.  */
28640   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28641     {
28642       if (cxx_dialect != cxx98)
28643         {
28644           /* In C++0x, a `>>' in a template argument list or cast
28645              expression is considered to be two separate `>'
28646              tokens. So, change the current token to a `>', but don't
28647              consume it: it will be consumed later when the outer
28648              template argument list (or cast expression) is parsed.
28649              Note that this replacement of `>' for `>>' is necessary
28650              even if we are parsing tentatively: in the tentative
28651              case, after calling
28652              cp_parser_enclosed_template_argument_list we will always
28653              throw away all of the template arguments and the first
28654              closing `>', either because the template argument list
28655              was erroneous or because we are replacing those tokens
28656              with a CPP_TEMPLATE_ID token.  The second `>' (which will
28657              not have been thrown away) is needed either to close an
28658              outer template argument list or to complete a new-style
28659              cast.  */
28660 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
28661           token->type = CPP_GREATER;
28662         }
28663       else if (!saved_greater_than_is_operator_p)
28664 	{
28665 	  /* If we're in a nested template argument list, the '>>' has
28666 	    to be a typo for '> >'. We emit the error message, but we
28667 	    continue parsing and we push a '>' as next token, so that
28668 	    the argument list will be parsed correctly.  Note that the
28669 	    global source location is still on the token before the
28670 	    '>>', so we need to say explicitly where we want it.  */
28671 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
28672 	  gcc_rich_location richloc (token->location);
28673 	  richloc.add_fixit_replace ("> >");
28674 	  error_at (&richloc, "%<>>%> should be %<> >%> "
28675 		    "within a nested template argument list");
28676 
28677 	  token->type = CPP_GREATER;
28678 	}
28679       else
28680 	{
28681 	  /* If this is not a nested template argument list, the '>>'
28682 	    is a typo for '>'. Emit an error message and continue.
28683 	    Same deal about the token location, but here we can get it
28684 	    right by consuming the '>>' before issuing the diagnostic.  */
28685 	  cp_token *token = cp_lexer_consume_token (parser->lexer);
28686 	  error_at (token->location,
28687 		    "spurious %<>>%>, use %<>%> to terminate "
28688 		    "a template argument list");
28689 	}
28690     }
28691   else
28692     cp_parser_skip_to_end_of_template_parameter_list (parser);
28693   /* The `>' token might be a greater-than operator again now.  */
28694   parser->greater_than_is_operator_p
28695     = saved_greater_than_is_operator_p;
28696   /* Restore the SAVED_SCOPE.  */
28697   parser->scope = saved_scope;
28698   parser->qualifying_scope = saved_qualifying_scope;
28699   parser->object_scope = saved_object_scope;
28700 
28701   return arguments;
28702 }
28703 
28704 /* MEMBER_FUNCTION is a member function, or a friend.  If default
28705    arguments, or the body of the function have not yet been parsed,
28706    parse them now.  */
28707 
28708 static void
cp_parser_late_parsing_for_member(cp_parser * parser,tree member_function)28709 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28710 {
28711   timevar_push (TV_PARSE_INMETH);
28712   /* If this member is a template, get the underlying
28713      FUNCTION_DECL.  */
28714   if (DECL_FUNCTION_TEMPLATE_P (member_function))
28715     member_function = DECL_TEMPLATE_RESULT (member_function);
28716 
28717   /* There should not be any class definitions in progress at this
28718      point; the bodies of members are only parsed outside of all class
28719      definitions.  */
28720   gcc_assert (parser->num_classes_being_defined == 0);
28721   /* While we're parsing the member functions we might encounter more
28722      classes.  We want to handle them right away, but we don't want
28723      them getting mixed up with functions that are currently in the
28724      queue.  */
28725   push_unparsed_function_queues (parser);
28726 
28727   /* Make sure that any template parameters are in scope.  */
28728   maybe_begin_member_template_processing (member_function);
28729 
28730   /* If the body of the function has not yet been parsed, parse it
28731      now.  */
28732   if (DECL_PENDING_INLINE_P (member_function))
28733     {
28734       tree function_scope;
28735       cp_token_cache *tokens;
28736 
28737       /* The function is no longer pending; we are processing it.  */
28738       tokens = DECL_PENDING_INLINE_INFO (member_function);
28739       DECL_PENDING_INLINE_INFO (member_function) = NULL;
28740       DECL_PENDING_INLINE_P (member_function) = 0;
28741 
28742       /* If this is a local class, enter the scope of the containing
28743 	 function.  */
28744       function_scope = current_function_decl;
28745       if (function_scope)
28746 	push_function_context ();
28747 
28748       /* Push the body of the function onto the lexer stack.  */
28749       cp_parser_push_lexer_for_tokens (parser, tokens);
28750 
28751       /* Let the front end know that we going to be defining this
28752 	 function.  */
28753       start_preparsed_function (member_function, NULL_TREE,
28754 				SF_PRE_PARSED | SF_INCLASS_INLINE);
28755 
28756       /* Don't do access checking if it is a templated function.  */
28757       if (processing_template_decl)
28758 	push_deferring_access_checks (dk_no_check);
28759 
28760       /* #pragma omp declare reduction needs special parsing.  */
28761       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28762 	{
28763 	  parser->lexer->in_pragma = true;
28764 	  cp_parser_omp_declare_reduction_exprs (member_function, parser);
28765 	  finish_function (/*inline_p=*/true);
28766 	  cp_check_omp_declare_reduction (member_function);
28767 	}
28768       else
28769 	/* Now, parse the body of the function.  */
28770 	cp_parser_function_definition_after_declarator (parser,
28771 							/*inline_p=*/true);
28772 
28773       if (processing_template_decl)
28774 	pop_deferring_access_checks ();
28775 
28776       /* Leave the scope of the containing function.  */
28777       if (function_scope)
28778 	pop_function_context ();
28779       cp_parser_pop_lexer (parser);
28780     }
28781 
28782   /* Remove any template parameters from the symbol table.  */
28783   maybe_end_member_template_processing ();
28784 
28785   /* Restore the queue.  */
28786   pop_unparsed_function_queues (parser);
28787   timevar_pop (TV_PARSE_INMETH);
28788 }
28789 
28790 /* If DECL contains any default args, remember it on the unparsed
28791    functions queue.  */
28792 
28793 static void
cp_parser_save_default_args(cp_parser * parser,tree decl)28794 cp_parser_save_default_args (cp_parser* parser, tree decl)
28795 {
28796   tree probe;
28797 
28798   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28799        probe;
28800        probe = TREE_CHAIN (probe))
28801     if (TREE_PURPOSE (probe))
28802       {
28803 	cp_default_arg_entry entry = {current_class_type, decl};
28804 	vec_safe_push (unparsed_funs_with_default_args, entry);
28805 	break;
28806       }
28807 }
28808 
28809 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28810    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
28811    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
28812    from the parameter-type-list.  */
28813 
28814 static tree
cp_parser_late_parse_one_default_arg(cp_parser * parser,tree decl,tree default_arg,tree parmtype)28815 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28816 				      tree default_arg, tree parmtype)
28817 {
28818   cp_token_cache *tokens;
28819   tree parsed_arg;
28820   bool dummy;
28821 
28822   if (default_arg == error_mark_node)
28823     return error_mark_node;
28824 
28825   /* Push the saved tokens for the default argument onto the parser's
28826      lexer stack.  */
28827   tokens = DEFARG_TOKENS (default_arg);
28828   cp_parser_push_lexer_for_tokens (parser, tokens);
28829 
28830   start_lambda_scope (decl);
28831 
28832   /* Parse the default argument.  */
28833   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28834   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28835     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28836 
28837   finish_lambda_scope ();
28838 
28839   if (parsed_arg == error_mark_node)
28840     cp_parser_skip_to_end_of_statement (parser);
28841 
28842   if (!processing_template_decl)
28843     {
28844       /* In a non-template class, check conversions now.  In a template,
28845 	 we'll wait and instantiate these as needed.  */
28846       if (TREE_CODE (decl) == PARM_DECL)
28847 	parsed_arg = check_default_argument (parmtype, parsed_arg,
28848 					     tf_warning_or_error);
28849       else if (maybe_reject_flexarray_init (decl, parsed_arg))
28850 	parsed_arg = error_mark_node;
28851       else
28852 	parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28853     }
28854 
28855   /* If the token stream has not been completely used up, then
28856      there was extra junk after the end of the default
28857      argument.  */
28858   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28859     {
28860       if (TREE_CODE (decl) == PARM_DECL)
28861 	cp_parser_error (parser, "expected %<,%>");
28862       else
28863 	cp_parser_error (parser, "expected %<;%>");
28864     }
28865 
28866   /* Revert to the main lexer.  */
28867   cp_parser_pop_lexer (parser);
28868 
28869   return parsed_arg;
28870 }
28871 
28872 /* FIELD is a non-static data member with an initializer which we saved for
28873    later; parse it now.  */
28874 
28875 static void
cp_parser_late_parsing_nsdmi(cp_parser * parser,tree field)28876 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28877 {
28878   tree def;
28879 
28880   maybe_begin_member_template_processing (field);
28881 
28882   push_unparsed_function_queues (parser);
28883   def = cp_parser_late_parse_one_default_arg (parser, field,
28884 					      DECL_INITIAL (field),
28885 					      NULL_TREE);
28886   pop_unparsed_function_queues (parser);
28887 
28888   maybe_end_member_template_processing ();
28889 
28890   DECL_INITIAL (field) = def;
28891 }
28892 
28893 /* FN is a FUNCTION_DECL which may contains a parameter with an
28894    unparsed DEFAULT_ARG.  Parse the default args now.  This function
28895    assumes that the current scope is the scope in which the default
28896    argument should be processed.  */
28897 
28898 static void
cp_parser_late_parsing_default_args(cp_parser * parser,tree fn)28899 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28900 {
28901   unsigned char saved_local_variables_forbidden_p;
28902   tree parm, parmdecl;
28903 
28904   /* While we're parsing the default args, we might (due to the
28905      statement expression extension) encounter more classes.  We want
28906      to handle them right away, but we don't want them getting mixed
28907      up with default args that are currently in the queue.  */
28908   push_unparsed_function_queues (parser);
28909 
28910   /* Local variable names (and the `this' keyword) may not appear
28911      in a default argument.  */
28912   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28913   parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28914 
28915   push_defarg_context (fn);
28916 
28917   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28918 	 parmdecl = DECL_ARGUMENTS (fn);
28919        parm && parm != void_list_node;
28920        parm = TREE_CHAIN (parm),
28921 	 parmdecl = DECL_CHAIN (parmdecl))
28922     {
28923       tree default_arg = TREE_PURPOSE (parm);
28924       tree parsed_arg;
28925       vec<tree, va_gc> *insts;
28926       tree copy;
28927       unsigned ix;
28928 
28929       if (!default_arg)
28930 	continue;
28931 
28932       if (TREE_CODE (default_arg) != DEFAULT_ARG)
28933 	/* This can happen for a friend declaration for a function
28934 	   already declared with default arguments.  */
28935 	continue;
28936 
28937       parsed_arg
28938 	= cp_parser_late_parse_one_default_arg (parser, parmdecl,
28939 						default_arg,
28940 						TREE_VALUE (parm));
28941       TREE_PURPOSE (parm) = parsed_arg;
28942 
28943       /* Update any instantiations we've already created.  */
28944       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28945 	   vec_safe_iterate (insts, ix, &copy); ix++)
28946 	TREE_PURPOSE (copy) = parsed_arg;
28947     }
28948 
28949   pop_defarg_context ();
28950 
28951   /* Make sure no default arg is missing.  */
28952   check_default_args (fn);
28953 
28954   /* Restore the state of local_variables_forbidden_p.  */
28955   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28956 
28957   /* Restore the queue.  */
28958   pop_unparsed_function_queues (parser);
28959 }
28960 
28961 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28962 
28963      sizeof ... ( identifier )
28964 
28965    where the 'sizeof' token has already been consumed.  */
28966 
28967 static tree
cp_parser_sizeof_pack(cp_parser * parser)28968 cp_parser_sizeof_pack (cp_parser *parser)
28969 {
28970   /* Consume the `...'.  */
28971   cp_lexer_consume_token (parser->lexer);
28972   maybe_warn_variadic_templates ();
28973 
28974   matching_parens parens;
28975   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28976   if (paren)
28977     parens.consume_open (parser);
28978   else
28979     permerror (cp_lexer_peek_token (parser->lexer)->location,
28980 	       "%<sizeof...%> argument must be surrounded by parentheses");
28981 
28982   cp_token *token = cp_lexer_peek_token (parser->lexer);
28983   tree name = cp_parser_identifier (parser);
28984   if (name == error_mark_node)
28985     return error_mark_node;
28986   /* The name is not qualified.  */
28987   parser->scope = NULL_TREE;
28988   parser->qualifying_scope = NULL_TREE;
28989   parser->object_scope = NULL_TREE;
28990   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28991   if (expr == error_mark_node)
28992     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28993 				 token->location);
28994   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28995     expr = TREE_TYPE (expr);
28996   else if (TREE_CODE (expr) == CONST_DECL)
28997     expr = DECL_INITIAL (expr);
28998   expr = make_pack_expansion (expr);
28999   PACK_EXPANSION_SIZEOF_P (expr) = true;
29000 
29001   if (paren)
29002     parens.require_close (parser);
29003 
29004   return expr;
29005 }
29006 
29007 /* Parse the operand of `sizeof' (or a similar operator).  Returns
29008    either a TYPE or an expression, depending on the form of the
29009    input.  The KEYWORD indicates which kind of expression we have
29010    encountered.  */
29011 
29012 static tree
cp_parser_sizeof_operand(cp_parser * parser,enum rid keyword)29013 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
29014 {
29015   tree expr = NULL_TREE;
29016   const char *saved_message;
29017   const char *saved_message_arg;
29018   bool saved_integral_constant_expression_p;
29019   bool saved_non_integral_constant_expression_p;
29020 
29021   /* If it's a `...', then we are computing the length of a parameter
29022      pack.  */
29023   if (keyword == RID_SIZEOF
29024       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29025     return cp_parser_sizeof_pack (parser);
29026 
29027   /* Types cannot be defined in a `sizeof' expression.  Save away the
29028      old message.  */
29029   saved_message = parser->type_definition_forbidden_message;
29030   saved_message_arg = parser->type_definition_forbidden_message_arg;
29031   parser->type_definition_forbidden_message
29032     = G_("types may not be defined in %qs expressions");
29033   parser->type_definition_forbidden_message_arg
29034     = IDENTIFIER_POINTER (ridpointers[keyword]);
29035 
29036   /* The restrictions on constant-expressions do not apply inside
29037      sizeof expressions.  */
29038   saved_integral_constant_expression_p
29039     = parser->integral_constant_expression_p;
29040   saved_non_integral_constant_expression_p
29041     = parser->non_integral_constant_expression_p;
29042   parser->integral_constant_expression_p = false;
29043 
29044   /* Do not actually evaluate the expression.  */
29045   ++cp_unevaluated_operand;
29046   ++c_inhibit_evaluation_warnings;
29047   /* If it's a `(', then we might be looking at the type-id
29048      construction.  */
29049   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29050     {
29051       tree type = NULL_TREE;
29052 
29053       /* We can't be sure yet whether we're looking at a type-id or an
29054 	 expression.  */
29055       cp_parser_parse_tentatively (parser);
29056 
29057       matching_parens parens;
29058       parens.consume_open (parser);
29059 
29060       /* Note: as a GNU Extension, compound literals are considered
29061 	 postfix-expressions as they are in C99, so they are valid
29062 	 arguments to sizeof.  See comment in cp_parser_cast_expression
29063 	 for details.  */
29064       if (cp_parser_compound_literal_p (parser))
29065 	cp_parser_simulate_error (parser);
29066       else
29067 	{
29068 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
29069 	  parser->in_type_id_in_expr_p = true;
29070 	  /* Look for the type-id.  */
29071 	  type = cp_parser_type_id (parser);
29072 	  /* Look for the closing `)'.  */
29073 	  parens.require_close (parser);
29074 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
29075 	}
29076 
29077       /* If all went well, then we're done.  */
29078       if (cp_parser_parse_definitely (parser))
29079 	expr = type;
29080     }
29081 
29082   /* If the type-id production did not work out, then we must be
29083      looking at the unary-expression production.  */
29084   if (!expr)
29085     expr = cp_parser_unary_expression (parser);
29086 
29087   /* Go back to evaluating expressions.  */
29088   --cp_unevaluated_operand;
29089   --c_inhibit_evaluation_warnings;
29090 
29091   /* And restore the old one.  */
29092   parser->type_definition_forbidden_message = saved_message;
29093   parser->type_definition_forbidden_message_arg = saved_message_arg;
29094   parser->integral_constant_expression_p
29095     = saved_integral_constant_expression_p;
29096   parser->non_integral_constant_expression_p
29097     = saved_non_integral_constant_expression_p;
29098 
29099   return expr;
29100 }
29101 
29102 /* If the current declaration has no declarator, return true.  */
29103 
29104 static bool
cp_parser_declares_only_class_p(cp_parser * parser)29105 cp_parser_declares_only_class_p (cp_parser *parser)
29106 {
29107   /* If the next token is a `;' or a `,' then there is no
29108      declarator.  */
29109   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29110 	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
29111 }
29112 
29113 /* Update the DECL_SPECS to reflect the storage class indicated by
29114    KEYWORD.  */
29115 
29116 static void
cp_parser_set_storage_class(cp_parser * parser,cp_decl_specifier_seq * decl_specs,enum rid keyword,cp_token * token)29117 cp_parser_set_storage_class (cp_parser *parser,
29118 			     cp_decl_specifier_seq *decl_specs,
29119 			     enum rid keyword,
29120 			     cp_token *token)
29121 {
29122   cp_storage_class storage_class;
29123 
29124   if (parser->in_unbraced_linkage_specification_p)
29125     {
29126       error_at (token->location, "invalid use of %qD in linkage specification",
29127 		ridpointers[keyword]);
29128       return;
29129     }
29130   else if (decl_specs->storage_class != sc_none)
29131     {
29132       decl_specs->conflicting_specifiers_p = true;
29133       return;
29134     }
29135 
29136   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
29137       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
29138       && decl_specs->gnu_thread_keyword_p)
29139     {
29140       pedwarn (decl_specs->locations[ds_thread], 0,
29141 		"%<__thread%> before %qD", ridpointers[keyword]);
29142     }
29143 
29144   switch (keyword)
29145     {
29146     case RID_AUTO:
29147       storage_class = sc_auto;
29148       break;
29149     case RID_REGISTER:
29150       storage_class = sc_register;
29151       break;
29152     case RID_STATIC:
29153       storage_class = sc_static;
29154       break;
29155     case RID_EXTERN:
29156       storage_class = sc_extern;
29157       break;
29158     case RID_MUTABLE:
29159       storage_class = sc_mutable;
29160       break;
29161     default:
29162       gcc_unreachable ();
29163     }
29164   decl_specs->storage_class = storage_class;
29165   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
29166 
29167   /* A storage class specifier cannot be applied alongside a typedef
29168      specifier. If there is a typedef specifier present then set
29169      conflicting_specifiers_p which will trigger an error later
29170      on in grokdeclarator. */
29171   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
29172     decl_specs->conflicting_specifiers_p = true;
29173 }
29174 
29175 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
29176    is true, the type is a class or enum definition.  */
29177 
29178 static void
cp_parser_set_decl_spec_type(cp_decl_specifier_seq * decl_specs,tree type_spec,cp_token * token,bool type_definition_p)29179 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
29180 			      tree type_spec,
29181 			      cp_token *token,
29182 			      bool type_definition_p)
29183 {
29184   decl_specs->any_specifiers_p = true;
29185 
29186   /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
29187      wchar_t (with, for example, in "typedef int wchar_t;") we remember that
29188      this is what happened.  In system headers, we ignore these
29189      declarations so that G++ can work with system headers that are not
29190      C++-safe.  */
29191   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
29192       && !type_definition_p
29193       && (type_spec == boolean_type_node
29194 	  || type_spec == char8_type_node
29195 	  || type_spec == char16_type_node
29196 	  || type_spec == char32_type_node
29197 	  || type_spec == wchar_type_node)
29198       && (decl_specs->type
29199 	  || decl_spec_seq_has_spec_p (decl_specs, ds_long)
29200 	  || decl_spec_seq_has_spec_p (decl_specs, ds_short)
29201 	  || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
29202 	  || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
29203     {
29204       decl_specs->redefined_builtin_type = type_spec;
29205       set_and_check_decl_spec_loc (decl_specs,
29206 				   ds_redefined_builtin_type_spec,
29207 				   token);
29208       if (!decl_specs->type)
29209 	{
29210 	  decl_specs->type = type_spec;
29211 	  decl_specs->type_definition_p = false;
29212 	  set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29213 	}
29214     }
29215   else if (decl_specs->type)
29216     decl_specs->multiple_types_p = true;
29217   else
29218     {
29219       decl_specs->type = type_spec;
29220       decl_specs->type_definition_p = type_definition_p;
29221       decl_specs->redefined_builtin_type = NULL_TREE;
29222       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29223     }
29224 }
29225 
29226 /* True iff TOKEN is the GNU keyword __thread.  */
29227 
29228 static bool
token_is__thread(cp_token * token)29229 token_is__thread (cp_token *token)
29230 {
29231   gcc_assert (token->keyword == RID_THREAD);
29232   return id_equal (token->u.value, "__thread");
29233 }
29234 
29235 /* Set the location for a declarator specifier and check if it is
29236    duplicated.
29237 
29238    DECL_SPECS is the sequence of declarator specifiers onto which to
29239    set the location.
29240 
29241    DS is the single declarator specifier to set which location  is to
29242    be set onto the existing sequence of declarators.
29243 
29244    LOCATION is the location for the declarator specifier to
29245    consider.  */
29246 
29247 static void
set_and_check_decl_spec_loc(cp_decl_specifier_seq * decl_specs,cp_decl_spec ds,cp_token * token)29248 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29249 			     cp_decl_spec ds, cp_token *token)
29250 {
29251   gcc_assert (ds < ds_last);
29252 
29253   if (decl_specs == NULL)
29254     return;
29255 
29256   location_t location = token->location;
29257 
29258   if (decl_specs->locations[ds] == 0)
29259     {
29260       decl_specs->locations[ds] = location;
29261       if (ds == ds_thread)
29262 	decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29263     }
29264   else
29265     {
29266       if (ds == ds_long)
29267 	{
29268 	  if (decl_specs->locations[ds_long_long] != 0)
29269 	    error_at (location,
29270 		      "%<long long long%> is too long for GCC");
29271 	  else
29272 	    {
29273 	      decl_specs->locations[ds_long_long] = location;
29274 	      pedwarn_cxx98 (location,
29275 			     OPT_Wlong_long,
29276 			     "ISO C++ 1998 does not support %<long long%>");
29277 	    }
29278 	}
29279       else if (ds == ds_thread)
29280 	{
29281 	  bool gnu = token_is__thread (token);
29282 	  gcc_rich_location richloc (location);
29283 	  if (gnu != decl_specs->gnu_thread_keyword_p)
29284 	    {
29285 	      richloc.add_range (decl_specs->locations[ds_thread]);
29286 	      error_at (&richloc,
29287 			"both %<__thread%> and %<thread_local%> specified");
29288 	    }
29289 	  else
29290 	    {
29291 	      richloc.add_fixit_remove ();
29292 	      error_at (&richloc, "duplicate %qD", token->u.value);
29293 	    }
29294 	}
29295       else
29296 	{
29297 	  static const char *const decl_spec_names[] = {
29298 	    "signed",
29299 	    "unsigned",
29300 	    "short",
29301 	    "long",
29302 	    "const",
29303 	    "volatile",
29304 	    "restrict",
29305 	    "inline",
29306 	    "virtual",
29307 	    "explicit",
29308 	    "friend",
29309 	    "typedef",
29310 	    "using",
29311             "constexpr",
29312 	    "__complex"
29313 	  };
29314 	  gcc_rich_location richloc (location);
29315 	  richloc.add_fixit_remove ();
29316 	  error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29317 	}
29318     }
29319 }
29320 
29321 /* Return true iff the declarator specifier DS is present in the
29322    sequence of declarator specifiers DECL_SPECS.  */
29323 
29324 bool
decl_spec_seq_has_spec_p(const cp_decl_specifier_seq * decl_specs,cp_decl_spec ds)29325 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29326 			  cp_decl_spec ds)
29327 {
29328   gcc_assert (ds < ds_last);
29329 
29330   if (decl_specs == NULL)
29331     return false;
29332 
29333   return decl_specs->locations[ds] != 0;
29334 }
29335 
29336 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29337    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
29338 
29339 static bool
cp_parser_friend_p(const cp_decl_specifier_seq * decl_specifiers)29340 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29341 {
29342   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29343 }
29344 
29345 /* Issue an error message indicating that TOKEN_DESC was expected.
29346    If KEYWORD is true, it indicated this function is called by
29347    cp_parser_require_keword and the required token can only be
29348    a indicated keyword.
29349 
29350    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29351    within any error as the location of an "opening" token matching
29352    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29353    RT_CLOSE_PAREN).  */
29354 
29355 static void
cp_parser_required_error(cp_parser * parser,required_token token_desc,bool keyword,location_t matching_location)29356 cp_parser_required_error (cp_parser *parser,
29357 			  required_token token_desc,
29358 			  bool keyword,
29359 			  location_t matching_location)
29360 {
29361   if (cp_parser_simulate_error (parser))
29362     return;
29363 
29364   const char *gmsgid = NULL;
29365   switch (token_desc)
29366     {
29367       case RT_NEW:
29368 	gmsgid = G_("expected %<new%>");
29369 	break;
29370       case RT_DELETE:
29371 	gmsgid = G_("expected %<delete%>");
29372 	break;
29373       case RT_RETURN:
29374 	gmsgid = G_("expected %<return%>");
29375 	break;
29376       case RT_WHILE:
29377 	gmsgid = G_("expected %<while%>");
29378 	break;
29379       case RT_EXTERN:
29380 	gmsgid = G_("expected %<extern%>");
29381 	break;
29382       case RT_STATIC_ASSERT:
29383 	gmsgid = G_("expected %<static_assert%>");
29384 	break;
29385       case RT_DECLTYPE:
29386 	gmsgid = G_("expected %<decltype%>");
29387 	break;
29388       case RT_OPERATOR:
29389 	gmsgid = G_("expected %<operator%>");
29390 	break;
29391       case RT_CLASS:
29392 	gmsgid = G_("expected %<class%>");
29393 	break;
29394       case RT_TEMPLATE:
29395 	gmsgid = G_("expected %<template%>");
29396 	break;
29397       case RT_NAMESPACE:
29398 	gmsgid = G_("expected %<namespace%>");
29399 	break;
29400       case RT_USING:
29401 	gmsgid = G_("expected %<using%>");
29402 	break;
29403       case RT_ASM:
29404 	gmsgid = G_("expected %<asm%>");
29405 	break;
29406       case RT_TRY:
29407 	gmsgid = G_("expected %<try%>");
29408 	break;
29409       case RT_CATCH:
29410 	gmsgid = G_("expected %<catch%>");
29411 	break;
29412       case RT_THROW:
29413 	gmsgid = G_("expected %<throw%>");
29414 	break;
29415       case RT_LABEL:
29416 	gmsgid = G_("expected %<__label__%>");
29417 	break;
29418       case RT_AT_TRY:
29419 	gmsgid = G_("expected %<@try%>");
29420 	break;
29421       case RT_AT_SYNCHRONIZED:
29422 	gmsgid = G_("expected %<@synchronized%>");
29423 	break;
29424       case RT_AT_THROW:
29425 	gmsgid = G_("expected %<@throw%>");
29426 	break;
29427       case RT_TRANSACTION_ATOMIC:
29428 	gmsgid = G_("expected %<__transaction_atomic%>");
29429 	break;
29430       case RT_TRANSACTION_RELAXED:
29431 	gmsgid = G_("expected %<__transaction_relaxed%>");
29432 	break;
29433       default:
29434 	break;
29435     }
29436 
29437   if (!gmsgid && !keyword)
29438     {
29439       switch (token_desc)
29440         {
29441 	  case RT_SEMICOLON:
29442 	    gmsgid = G_("expected %<;%>");
29443 	    break;
29444 	  case RT_OPEN_PAREN:
29445 	    gmsgid = G_("expected %<(%>");
29446 	    break;
29447 	  case RT_CLOSE_BRACE:
29448 	    gmsgid = G_("expected %<}%>");
29449 	    break;
29450 	  case RT_OPEN_BRACE:
29451 	    gmsgid = G_("expected %<{%>");
29452 	    break;
29453 	  case RT_CLOSE_SQUARE:
29454 	    gmsgid = G_("expected %<]%>");
29455 	    break;
29456 	  case RT_OPEN_SQUARE:
29457 	    gmsgid = G_("expected %<[%>");
29458 	    break;
29459 	  case RT_COMMA:
29460 	    gmsgid = G_("expected %<,%>");
29461 	    break;
29462 	  case RT_SCOPE:
29463 	    gmsgid = G_("expected %<::%>");
29464 	    break;
29465 	  case RT_LESS:
29466 	    gmsgid = G_("expected %<<%>");
29467 	    break;
29468 	  case RT_GREATER:
29469 	    gmsgid = G_("expected %<>%>");
29470 	    break;
29471 	  case RT_EQ:
29472 	    gmsgid = G_("expected %<=%>");
29473 	    break;
29474 	  case RT_ELLIPSIS:
29475 	    gmsgid = G_("expected %<...%>");
29476 	    break;
29477 	  case RT_MULT:
29478 	    gmsgid = G_("expected %<*%>");
29479 	    break;
29480 	  case RT_COMPL:
29481 	    gmsgid = G_("expected %<~%>");
29482 	    break;
29483 	  case RT_COLON:
29484 	    gmsgid = G_("expected %<:%>");
29485 	    break;
29486 	  case RT_COLON_SCOPE:
29487 	    gmsgid = G_("expected %<:%> or %<::%>");
29488 	    break;
29489 	  case RT_CLOSE_PAREN:
29490 	    gmsgid = G_("expected %<)%>");
29491 	    break;
29492 	  case RT_COMMA_CLOSE_PAREN:
29493 	    gmsgid = G_("expected %<,%> or %<)%>");
29494 	    break;
29495 	  case RT_PRAGMA_EOL:
29496 	    gmsgid = G_("expected end of line");
29497 	    break;
29498 	  case RT_NAME:
29499 	    gmsgid = G_("expected identifier");
29500 	    break;
29501 	  case RT_SELECT:
29502 	    gmsgid = G_("expected selection-statement");
29503 	    break;
29504 	  case RT_ITERATION:
29505 	    gmsgid = G_("expected iteration-statement");
29506 	    break;
29507 	  case RT_JUMP:
29508 	    gmsgid = G_("expected jump-statement");
29509 	    break;
29510 	  case RT_CLASS_KEY:
29511 	    gmsgid = G_("expected class-key");
29512 	    break;
29513 	  case RT_CLASS_TYPENAME_TEMPLATE:
29514 	    gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29515 	    break;
29516 	  default:
29517 	    gcc_unreachable ();
29518 	}
29519     }
29520 
29521   if (gmsgid)
29522     cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29523 }
29524 
29525 
29526 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
29527    issue an error message indicating that TOKEN_DESC was expected.
29528 
29529    Returns the token consumed, if the token had the appropriate type.
29530    Otherwise, returns NULL.
29531 
29532    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29533    within any error as the location of an "opening" token matching
29534    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29535    RT_CLOSE_PAREN).  */
29536 
29537 static cp_token *
cp_parser_require(cp_parser * parser,enum cpp_ttype type,required_token token_desc,location_t matching_location)29538 cp_parser_require (cp_parser* parser,
29539 		   enum cpp_ttype type,
29540 		   required_token token_desc,
29541 		   location_t matching_location)
29542 {
29543   if (cp_lexer_next_token_is (parser->lexer, type))
29544     return cp_lexer_consume_token (parser->lexer);
29545   else
29546     {
29547       /* Output the MESSAGE -- unless we're parsing tentatively.  */
29548       if (!cp_parser_simulate_error (parser))
29549 	cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29550 				  matching_location);
29551       return NULL;
29552     }
29553 }
29554 
29555 /* An error message is produced if the next token is not '>'.
29556    All further tokens are skipped until the desired token is
29557    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
29558 
29559 static void
cp_parser_skip_to_end_of_template_parameter_list(cp_parser * parser)29560 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29561 {
29562   /* Current level of '< ... >'.  */
29563   unsigned level = 0;
29564   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
29565   unsigned nesting_depth = 0;
29566 
29567   /* Are we ready, yet?  If not, issue error message.  */
29568   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29569     return;
29570 
29571   /* Skip tokens until the desired token is found.  */
29572   while (true)
29573     {
29574       /* Peek at the next token.  */
29575       switch (cp_lexer_peek_token (parser->lexer)->type)
29576 	{
29577 	case CPP_LESS:
29578 	  if (!nesting_depth)
29579 	    ++level;
29580 	  break;
29581 
29582         case CPP_RSHIFT:
29583           if (cxx_dialect == cxx98)
29584             /* C++0x views the `>>' operator as two `>' tokens, but
29585                C++98 does not. */
29586             break;
29587           else if (!nesting_depth && level-- == 0)
29588 	    {
29589               /* We've hit a `>>' where the first `>' closes the
29590                  template argument list, and the second `>' is
29591                  spurious.  Just consume the `>>' and stop; we've
29592                  already produced at least one error.  */
29593 	      cp_lexer_consume_token (parser->lexer);
29594 	      return;
29595 	    }
29596           /* Fall through for C++0x, so we handle the second `>' in
29597              the `>>'.  */
29598 	  gcc_fallthrough ();
29599 
29600 	case CPP_GREATER:
29601 	  if (!nesting_depth && level-- == 0)
29602 	    {
29603 	      /* We've reached the token we want, consume it and stop.  */
29604 	      cp_lexer_consume_token (parser->lexer);
29605 	      return;
29606 	    }
29607 	  break;
29608 
29609 	case CPP_OPEN_PAREN:
29610 	case CPP_OPEN_SQUARE:
29611 	  ++nesting_depth;
29612 	  break;
29613 
29614 	case CPP_CLOSE_PAREN:
29615 	case CPP_CLOSE_SQUARE:
29616 	  if (nesting_depth-- == 0)
29617 	    return;
29618 	  break;
29619 
29620 	case CPP_EOF:
29621 	case CPP_PRAGMA_EOL:
29622 	case CPP_SEMICOLON:
29623 	case CPP_OPEN_BRACE:
29624 	case CPP_CLOSE_BRACE:
29625 	  /* The '>' was probably forgotten, don't look further.  */
29626 	  return;
29627 
29628 	default:
29629 	  break;
29630 	}
29631 
29632       /* Consume this token.  */
29633       cp_lexer_consume_token (parser->lexer);
29634     }
29635 }
29636 
29637 /* If the next token is the indicated keyword, consume it.  Otherwise,
29638    issue an error message indicating that TOKEN_DESC was expected.
29639 
29640    Returns the token consumed, if the token had the appropriate type.
29641    Otherwise, returns NULL.  */
29642 
29643 static cp_token *
cp_parser_require_keyword(cp_parser * parser,enum rid keyword,required_token token_desc)29644 cp_parser_require_keyword (cp_parser* parser,
29645 			   enum rid keyword,
29646 			   required_token token_desc)
29647 {
29648   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29649 
29650   if (token && token->keyword != keyword)
29651     {
29652       cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29653                                 UNKNOWN_LOCATION);
29654       return NULL;
29655     }
29656 
29657   return token;
29658 }
29659 
29660 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29661    function-definition.  */
29662 
29663 static bool
cp_parser_token_starts_function_definition_p(cp_token * token)29664 cp_parser_token_starts_function_definition_p (cp_token* token)
29665 {
29666   return (/* An ordinary function-body begins with an `{'.  */
29667 	  token->type == CPP_OPEN_BRACE
29668 	  /* A ctor-initializer begins with a `:'.  */
29669 	  || token->type == CPP_COLON
29670 	  /* A function-try-block begins with `try'.  */
29671 	  || token->keyword == RID_TRY
29672 	  /* A function-transaction-block begins with `__transaction_atomic'
29673 	     or `__transaction_relaxed'.  */
29674 	  || token->keyword == RID_TRANSACTION_ATOMIC
29675 	  || token->keyword == RID_TRANSACTION_RELAXED
29676 	  /* The named return value extension begins with `return'.  */
29677 	  || token->keyword == RID_RETURN);
29678 }
29679 
29680 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29681    definition.  */
29682 
29683 static bool
cp_parser_next_token_starts_class_definition_p(cp_parser * parser)29684 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29685 {
29686   cp_token *token;
29687 
29688   token = cp_lexer_peek_token (parser->lexer);
29689   return (token->type == CPP_OPEN_BRACE
29690 	  || (token->type == CPP_COLON
29691 	      && !parser->colon_doesnt_start_class_def_p));
29692 }
29693 
29694 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29695    C++0x) ending a template-argument.  */
29696 
29697 static bool
cp_parser_next_token_ends_template_argument_p(cp_parser * parser)29698 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29699 {
29700   cp_token *token;
29701 
29702   token = cp_lexer_peek_token (parser->lexer);
29703   return (token->type == CPP_COMMA
29704           || token->type == CPP_GREATER
29705           || token->type == CPP_ELLIPSIS
29706 	  || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29707 }
29708 
29709 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29710    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
29711 
29712 static bool
cp_parser_nth_token_starts_template_argument_list_p(cp_parser * parser,size_t n)29713 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29714 						     size_t n)
29715 {
29716   cp_token *token;
29717 
29718   token = cp_lexer_peek_nth_token (parser->lexer, n);
29719   if (token->type == CPP_LESS)
29720     return true;
29721   /* Check for the sequence `<::' in the original code. It would be lexed as
29722      `[:', where `[' is a digraph, and there is no whitespace before
29723      `:'.  */
29724   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29725     {
29726       cp_token *token2;
29727       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29728       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29729 	return true;
29730     }
29731   return false;
29732 }
29733 
29734 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29735    or none_type otherwise.  */
29736 
29737 static enum tag_types
cp_parser_token_is_class_key(cp_token * token)29738 cp_parser_token_is_class_key (cp_token* token)
29739 {
29740   switch (token->keyword)
29741     {
29742     case RID_CLASS:
29743       return class_type;
29744     case RID_STRUCT:
29745       return record_type;
29746     case RID_UNION:
29747       return union_type;
29748 
29749     default:
29750       return none_type;
29751     }
29752 }
29753 
29754 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29755    or none_type otherwise or if the token is null.  */
29756 
29757 static enum tag_types
cp_parser_token_is_type_parameter_key(cp_token * token)29758 cp_parser_token_is_type_parameter_key (cp_token* token)
29759 {
29760   if (!token)
29761     return none_type;
29762 
29763   switch (token->keyword)
29764     {
29765     case RID_CLASS:
29766       return class_type;
29767     case RID_TYPENAME:
29768       return typename_type;
29769 
29770     default:
29771       return none_type;
29772     }
29773 }
29774 
29775 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
29776 
29777 static void
cp_parser_check_class_key(enum tag_types class_key,tree type)29778 cp_parser_check_class_key (enum tag_types class_key, tree type)
29779 {
29780   if (type == error_mark_node)
29781     return;
29782   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29783     {
29784       if (permerror (input_location, "%qs tag used in naming %q#T",
29785 		     class_key == union_type ? "union"
29786 		     : class_key == record_type ? "struct" : "class",
29787 		     type))
29788 	inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29789 		"%q#T was previously declared here", type);
29790     }
29791 }
29792 
29793 /* Issue an error message if DECL is redeclared with different
29794    access than its original declaration [class.access.spec/3].
29795    This applies to nested classes, nested class templates and
29796    enumerations [class.mem/1].  */
29797 
29798 static void
cp_parser_check_access_in_redeclaration(tree decl,location_t location)29799 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29800 {
29801   if (!decl
29802       || (!CLASS_TYPE_P (TREE_TYPE (decl))
29803 	  && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29804     return;
29805 
29806   if ((TREE_PRIVATE (decl)
29807        != (current_access_specifier == access_private_node))
29808       || (TREE_PROTECTED (decl)
29809 	  != (current_access_specifier == access_protected_node)))
29810     error_at (location, "%qD redeclared with different access", decl);
29811 }
29812 
29813 /* Look for the `template' keyword, as a syntactic disambiguator.
29814    Return TRUE iff it is present, in which case it will be
29815    consumed.  */
29816 
29817 static bool
cp_parser_optional_template_keyword(cp_parser * parser)29818 cp_parser_optional_template_keyword (cp_parser *parser)
29819 {
29820   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29821     {
29822       /* In C++98 the `template' keyword can only be used within templates;
29823 	 outside templates the parser can always figure out what is a
29824 	 template and what is not.  In C++11,  per the resolution of DR 468,
29825 	 `template' is allowed in cases where it is not strictly necessary.  */
29826       if (!processing_template_decl
29827 	  && pedantic && cxx_dialect == cxx98)
29828 	{
29829 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
29830 	  pedwarn (token->location, OPT_Wpedantic,
29831 		   "in C++98 %<template%> (as a disambiguator) is only "
29832 		   "allowed within templates");
29833 	  /* If this part of the token stream is rescanned, the same
29834 	     error message would be generated.  So, we purge the token
29835 	     from the stream.  */
29836 	  cp_lexer_purge_token (parser->lexer);
29837 	  return false;
29838 	}
29839       else
29840 	{
29841 	  /* Consume the `template' keyword.  */
29842 	  cp_lexer_consume_token (parser->lexer);
29843 	  return true;
29844 	}
29845     }
29846   return false;
29847 }
29848 
29849 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
29850    set PARSER->SCOPE, and perform other related actions.  */
29851 
29852 static void
cp_parser_pre_parsed_nested_name_specifier(cp_parser * parser)29853 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29854 {
29855   struct tree_check *check_value;
29856 
29857   /* Get the stored value.  */
29858   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29859   /* Set the scope from the stored value.  */
29860   parser->scope = saved_checks_value (check_value);
29861   parser->qualifying_scope = check_value->qualifying_scope;
29862   parser->object_scope = NULL_TREE;
29863 }
29864 
29865 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
29866    encounter the end of a block before what we were looking for.  */
29867 
29868 static bool
cp_parser_cache_group(cp_parser * parser,enum cpp_ttype end,unsigned depth)29869 cp_parser_cache_group (cp_parser *parser,
29870 		       enum cpp_ttype end,
29871 		       unsigned depth)
29872 {
29873   while (true)
29874     {
29875       cp_token *token = cp_lexer_peek_token (parser->lexer);
29876 
29877       /* Abort a parenthesized expression if we encounter a semicolon.  */
29878       if ((end == CPP_CLOSE_PAREN || depth == 0)
29879 	  && token->type == CPP_SEMICOLON)
29880 	return true;
29881       /* If we've reached the end of the file, stop.  */
29882       if (token->type == CPP_EOF
29883 	  || (end != CPP_PRAGMA_EOL
29884 	      && token->type == CPP_PRAGMA_EOL))
29885 	return true;
29886       if (token->type == CPP_CLOSE_BRACE && depth == 0)
29887 	/* We've hit the end of an enclosing block, so there's been some
29888 	   kind of syntax error.  */
29889 	return true;
29890 
29891       /* Consume the token.  */
29892       cp_lexer_consume_token (parser->lexer);
29893       /* See if it starts a new group.  */
29894       if (token->type == CPP_OPEN_BRACE)
29895 	{
29896 	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29897 	  /* In theory this should probably check end == '}', but
29898 	     cp_parser_save_member_function_body needs it to exit
29899 	     after either '}' or ')' when called with ')'.  */
29900 	  if (depth == 0)
29901 	    return false;
29902 	}
29903       else if (token->type == CPP_OPEN_PAREN)
29904 	{
29905 	  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29906 	  if (depth == 0 && end == CPP_CLOSE_PAREN)
29907 	    return false;
29908 	}
29909       else if (token->type == CPP_PRAGMA)
29910 	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29911       else if (token->type == end)
29912 	return false;
29913     }
29914 }
29915 
29916 /* Like above, for caching a default argument or NSDMI.  Both of these are
29917    terminated by a non-nested comma, but it can be unclear whether or not a
29918    comma is nested in a template argument list unless we do more parsing.
29919    In order to handle this ambiguity, when we encounter a ',' after a '<'
29920    we try to parse what follows as a parameter-declaration-list (in the
29921    case of a default argument) or a member-declarator (in the case of an
29922    NSDMI).  If that succeeds, then we stop caching.  */
29923 
29924 static tree
cp_parser_cache_defarg(cp_parser * parser,bool nsdmi)29925 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29926 {
29927   unsigned depth = 0;
29928   int maybe_template_id = 0;
29929   cp_token *first_token;
29930   cp_token *token;
29931   tree default_argument;
29932 
29933   /* Add tokens until we have processed the entire default
29934      argument.  We add the range [first_token, token).  */
29935   first_token = cp_lexer_peek_token (parser->lexer);
29936   if (first_token->type == CPP_OPEN_BRACE)
29937     {
29938       /* For list-initialization, this is straightforward.  */
29939       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29940       token = cp_lexer_peek_token (parser->lexer);
29941     }
29942   else while (true)
29943     {
29944       bool done = false;
29945 
29946       /* Peek at the next token.  */
29947       token = cp_lexer_peek_token (parser->lexer);
29948       /* What we do depends on what token we have.  */
29949       switch (token->type)
29950 	{
29951 	  /* In valid code, a default argument must be
29952 	     immediately followed by a `,' `)', or `...'.  */
29953 	case CPP_COMMA:
29954 	  if (depth == 0 && maybe_template_id)
29955 	    {
29956 	      /* If we've seen a '<', we might be in a
29957 		 template-argument-list.  Until Core issue 325 is
29958 		 resolved, we don't know how this situation ought
29959 		 to be handled, so try to DTRT.  We check whether
29960 		 what comes after the comma is a valid parameter
29961 		 declaration list.  If it is, then the comma ends
29962 		 the default argument; otherwise the default
29963 		 argument continues.  */
29964 	      bool error = false;
29965 	      cp_token *peek;
29966 
29967 	      /* Set ITALP so cp_parser_parameter_declaration_list
29968 		 doesn't decide to commit to this parse.  */
29969 	      bool saved_italp = parser->in_template_argument_list_p;
29970 	      parser->in_template_argument_list_p = true;
29971 
29972 	      cp_parser_parse_tentatively (parser);
29973 
29974 	      if (nsdmi)
29975 		{
29976 		  /* Parse declarators until we reach a non-comma or
29977 		     somthing that cannot be an initializer.
29978 		     Just checking whether we're looking at a single
29979 		     declarator is insufficient.  Consider:
29980 		       int var = tuple<T,U>::x;
29981 		     The template parameter 'U' looks exactly like a
29982 		     declarator.  */
29983 		  do
29984 		    {
29985 		      int ctor_dtor_or_conv_p;
29986 		      cp_lexer_consume_token (parser->lexer);
29987 		      cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29988 					    CP_PARSER_FLAGS_NONE,
29989 					    &ctor_dtor_or_conv_p,
29990 					    /*parenthesized_p=*/NULL,
29991 					    /*member_p=*/true,
29992 					    /*friend_p=*/false,
29993 					    /*static_p=*/false);
29994 		      peek = cp_lexer_peek_token (parser->lexer);
29995 		      if (cp_parser_error_occurred (parser))
29996 			break;
29997 		    }
29998 		  while (peek->type == CPP_COMMA);
29999 		  /* If we met an '=' or ';' then the original comma
30000 		     was the end of the NSDMI.  Otherwise assume
30001 		     we're still in the NSDMI.  */
30002 		  error = (peek->type != CPP_EQ
30003 			   && peek->type != CPP_SEMICOLON);
30004 		}
30005 	      else
30006 		{
30007 		  cp_lexer_consume_token (parser->lexer);
30008 		  begin_scope (sk_function_parms, NULL_TREE);
30009 		  tree t = cp_parser_parameter_declaration_list
30010 			    (parser, CP_PARSER_FLAGS_NONE);
30011 		  if (t == error_mark_node)
30012 		    error = true;
30013 		  pop_bindings_and_leave_scope ();
30014 		}
30015 	      if (!cp_parser_error_occurred (parser) && !error)
30016 		done = true;
30017 	      cp_parser_abort_tentative_parse (parser);
30018 
30019 	      parser->in_template_argument_list_p = saved_italp;
30020 	      break;
30021 	    }
30022 	  /* FALLTHRU */
30023 	case CPP_CLOSE_PAREN:
30024 	case CPP_ELLIPSIS:
30025 	  /* If we run into a non-nested `;', `}', or `]',
30026 	     then the code is invalid -- but the default
30027 	     argument is certainly over.  */
30028 	case CPP_SEMICOLON:
30029 	case CPP_CLOSE_BRACE:
30030 	case CPP_CLOSE_SQUARE:
30031 	  if (depth == 0
30032 	      /* Handle correctly int n = sizeof ... ( p );  */
30033 	      && token->type != CPP_ELLIPSIS)
30034 	    done = true;
30035 	  /* Update DEPTH, if necessary.  */
30036 	  else if (token->type == CPP_CLOSE_PAREN
30037 		   || token->type == CPP_CLOSE_BRACE
30038 		   || token->type == CPP_CLOSE_SQUARE)
30039 	    --depth;
30040 	  break;
30041 
30042 	case CPP_OPEN_PAREN:
30043 	case CPP_OPEN_SQUARE:
30044 	case CPP_OPEN_BRACE:
30045 	  ++depth;
30046 	  break;
30047 
30048 	case CPP_LESS:
30049 	  if (depth == 0)
30050 	    /* This might be the comparison operator, or it might
30051 	       start a template argument list.  */
30052 	    ++maybe_template_id;
30053 	  break;
30054 
30055 	case CPP_RSHIFT:
30056 	  if (cxx_dialect == cxx98)
30057 	    break;
30058 	  /* Fall through for C++0x, which treats the `>>'
30059 	     operator like two `>' tokens in certain
30060 	     cases.  */
30061 	  gcc_fallthrough ();
30062 
30063 	case CPP_GREATER:
30064 	  if (depth == 0)
30065 	    {
30066 	      /* This might be an operator, or it might close a
30067 		 template argument list.  But if a previous '<'
30068 		 started a template argument list, this will have
30069 		 closed it, so we can't be in one anymore.  */
30070 	      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
30071 	      if (maybe_template_id < 0)
30072 		maybe_template_id = 0;
30073 	    }
30074 	  break;
30075 
30076 	  /* If we run out of tokens, issue an error message.  */
30077 	case CPP_EOF:
30078 	case CPP_PRAGMA_EOL:
30079 	  error_at (token->location, "file ends in default argument");
30080 	  return error_mark_node;
30081 
30082 	case CPP_NAME:
30083 	case CPP_SCOPE:
30084 	  /* In these cases, we should look for template-ids.
30085 	     For example, if the default argument is
30086 	     `X<int, double>()', we need to do name lookup to
30087 	     figure out whether or not `X' is a template; if
30088 	     so, the `,' does not end the default argument.
30089 
30090 	     That is not yet done.  */
30091 	  break;
30092 
30093 	default:
30094 	  break;
30095 	}
30096 
30097       /* If we've reached the end, stop.  */
30098       if (done)
30099 	break;
30100 
30101       /* Add the token to the token block.  */
30102       token = cp_lexer_consume_token (parser->lexer);
30103     }
30104 
30105   /* Create a DEFAULT_ARG to represent the unparsed default
30106      argument.  */
30107   default_argument = make_node (DEFAULT_ARG);
30108   DEFARG_TOKENS (default_argument)
30109     = cp_token_cache_new (first_token, token);
30110   DEFARG_INSTANTIATIONS (default_argument) = NULL;
30111 
30112   return default_argument;
30113 }
30114 
30115 /* A location to use for diagnostics about an unparsed DEFAULT_ARG.  */
30116 
30117 location_t
defarg_location(tree default_argument)30118 defarg_location (tree default_argument)
30119 {
30120   cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
30121   location_t start = tokens->first->location;
30122   location_t end = tokens->last->location;
30123   return make_location (start, start, end);
30124 }
30125 
30126 /* Begin parsing tentatively.  We always save tokens while parsing
30127    tentatively so that if the tentative parsing fails we can restore the
30128    tokens.  */
30129 
30130 static void
cp_parser_parse_tentatively(cp_parser * parser)30131 cp_parser_parse_tentatively (cp_parser* parser)
30132 {
30133   /* Enter a new parsing context.  */
30134   parser->context = cp_parser_context_new (parser->context);
30135   /* Begin saving tokens.  */
30136   cp_lexer_save_tokens (parser->lexer);
30137   /* In order to avoid repetitive access control error messages,
30138      access checks are queued up until we are no longer parsing
30139      tentatively.  */
30140   push_deferring_access_checks (dk_deferred);
30141 }
30142 
30143 /* Commit to the currently active tentative parse.  */
30144 
30145 static void
cp_parser_commit_to_tentative_parse(cp_parser * parser)30146 cp_parser_commit_to_tentative_parse (cp_parser* parser)
30147 {
30148   cp_parser_context *context;
30149   cp_lexer *lexer;
30150 
30151   /* Mark all of the levels as committed.  */
30152   lexer = parser->lexer;
30153   for (context = parser->context; context->next; context = context->next)
30154     {
30155       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30156 	break;
30157       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30158       while (!cp_lexer_saving_tokens (lexer))
30159 	lexer = lexer->next;
30160       cp_lexer_commit_tokens (lexer);
30161     }
30162 }
30163 
30164 /* Commit to the topmost currently active tentative parse.
30165 
30166    Note that this function shouldn't be called when there are
30167    irreversible side-effects while in a tentative state.  For
30168    example, we shouldn't create a permanent entry in the symbol
30169    table, or issue an error message that might not apply if the
30170    tentative parse is aborted.  */
30171 
30172 static void
cp_parser_commit_to_topmost_tentative_parse(cp_parser * parser)30173 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
30174 {
30175   cp_parser_context *context = parser->context;
30176   cp_lexer *lexer = parser->lexer;
30177 
30178   if (context)
30179     {
30180       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
30181 	return;
30182       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
30183 
30184       while (!cp_lexer_saving_tokens (lexer))
30185 	lexer = lexer->next;
30186       cp_lexer_commit_tokens (lexer);
30187     }
30188 }
30189 
30190 /* Abort the currently active tentative parse.  All consumed tokens
30191    will be rolled back, and no diagnostics will be issued.  */
30192 
30193 static void
cp_parser_abort_tentative_parse(cp_parser * parser)30194 cp_parser_abort_tentative_parse (cp_parser* parser)
30195 {
30196   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
30197 	      || errorcount > 0);
30198   cp_parser_simulate_error (parser);
30199   /* Now, pretend that we want to see if the construct was
30200      successfully parsed.  */
30201   cp_parser_parse_definitely (parser);
30202 }
30203 
30204 /* Stop parsing tentatively.  If a parse error has occurred, restore the
30205    token stream.  Otherwise, commit to the tokens we have consumed.
30206    Returns true if no error occurred; false otherwise.  */
30207 
30208 static bool
cp_parser_parse_definitely(cp_parser * parser)30209 cp_parser_parse_definitely (cp_parser* parser)
30210 {
30211   bool error_occurred;
30212   cp_parser_context *context;
30213 
30214   /* Remember whether or not an error occurred, since we are about to
30215      destroy that information.  */
30216   error_occurred = cp_parser_error_occurred (parser);
30217   /* Remove the topmost context from the stack.  */
30218   context = parser->context;
30219   parser->context = context->next;
30220   /* If no parse errors occurred, commit to the tentative parse.  */
30221   if (!error_occurred)
30222     {
30223       /* Commit to the tokens read tentatively, unless that was
30224 	 already done.  */
30225       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30226 	cp_lexer_commit_tokens (parser->lexer);
30227 
30228       pop_to_parent_deferring_access_checks ();
30229     }
30230   /* Otherwise, if errors occurred, roll back our state so that things
30231      are just as they were before we began the tentative parse.  */
30232   else
30233     {
30234       cp_lexer_rollback_tokens (parser->lexer);
30235       pop_deferring_access_checks ();
30236     }
30237   /* Add the context to the front of the free list.  */
30238   context->next = cp_parser_context_free_list;
30239   cp_parser_context_free_list = context;
30240 
30241   return !error_occurred;
30242 }
30243 
30244 /* Returns true if we are parsing tentatively and are not committed to
30245    this tentative parse.  */
30246 
30247 static bool
cp_parser_uncommitted_to_tentative_parse_p(cp_parser * parser)30248 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30249 {
30250   return (cp_parser_parsing_tentatively (parser)
30251 	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30252 }
30253 
30254 /* Returns nonzero iff an error has occurred during the most recent
30255    tentative parse.  */
30256 
30257 static bool
cp_parser_error_occurred(cp_parser * parser)30258 cp_parser_error_occurred (cp_parser* parser)
30259 {
30260   return (cp_parser_parsing_tentatively (parser)
30261 	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30262 }
30263 
30264 /* Returns nonzero if GNU extensions are allowed.  */
30265 
30266 static bool
cp_parser_allow_gnu_extensions_p(cp_parser * parser)30267 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30268 {
30269   return parser->allow_gnu_extensions_p;
30270 }
30271 
30272 /* Objective-C++ Productions */
30273 
30274 
30275 /* Parse an Objective-C expression, which feeds into a primary-expression
30276    above.
30277 
30278    objc-expression:
30279      objc-message-expression
30280      objc-string-literal
30281      objc-encode-expression
30282      objc-protocol-expression
30283      objc-selector-expression
30284 
30285   Returns a tree representation of the expression.  */
30286 
30287 static cp_expr
cp_parser_objc_expression(cp_parser * parser)30288 cp_parser_objc_expression (cp_parser* parser)
30289 {
30290   /* Try to figure out what kind of declaration is present.  */
30291   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30292 
30293   switch (kwd->type)
30294     {
30295     case CPP_OPEN_SQUARE:
30296       return cp_parser_objc_message_expression (parser);
30297 
30298     case CPP_OBJC_STRING:
30299       kwd = cp_lexer_consume_token (parser->lexer);
30300       return objc_build_string_object (kwd->u.value);
30301 
30302     case CPP_KEYWORD:
30303       switch (kwd->keyword)
30304 	{
30305 	case RID_AT_ENCODE:
30306 	  return cp_parser_objc_encode_expression (parser);
30307 
30308 	case RID_AT_PROTOCOL:
30309 	  return cp_parser_objc_protocol_expression (parser);
30310 
30311 	case RID_AT_SELECTOR:
30312 	  return cp_parser_objc_selector_expression (parser);
30313 
30314 	default:
30315 	  break;
30316 	}
30317       /* FALLTHRU */
30318     default:
30319       error_at (kwd->location,
30320 		"misplaced %<@%D%> Objective-C++ construct",
30321 		kwd->u.value);
30322       cp_parser_skip_to_end_of_block_or_statement (parser);
30323     }
30324 
30325   return error_mark_node;
30326 }
30327 
30328 /* Parse an Objective-C message expression.
30329 
30330    objc-message-expression:
30331      [ objc-message-receiver objc-message-args ]
30332 
30333    Returns a representation of an Objective-C message.  */
30334 
30335 static tree
cp_parser_objc_message_expression(cp_parser * parser)30336 cp_parser_objc_message_expression (cp_parser* parser)
30337 {
30338   tree receiver, messageargs;
30339 
30340   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30341   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
30342   receiver = cp_parser_objc_message_receiver (parser);
30343   messageargs = cp_parser_objc_message_args (parser);
30344   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30345   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30346 
30347   tree result = objc_build_message_expr (receiver, messageargs);
30348 
30349   /* Construct a location e.g.
30350        [self func1:5]
30351        ^~~~~~~~~~~~~~
30352      ranging from the '[' to the ']', with the caret at the start.  */
30353   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30354   protected_set_expr_location (result, combined_loc);
30355 
30356   return result;
30357 }
30358 
30359 /* Parse an objc-message-receiver.
30360 
30361    objc-message-receiver:
30362      expression
30363      simple-type-specifier
30364 
30365   Returns a representation of the type or expression.  */
30366 
30367 static tree
cp_parser_objc_message_receiver(cp_parser * parser)30368 cp_parser_objc_message_receiver (cp_parser* parser)
30369 {
30370   tree rcv;
30371 
30372   /* An Objective-C message receiver may be either (1) a type
30373      or (2) an expression.  */
30374   cp_parser_parse_tentatively (parser);
30375   rcv = cp_parser_expression (parser);
30376 
30377   /* If that worked out, fine.  */
30378   if (cp_parser_parse_definitely (parser))
30379     return rcv;
30380 
30381   cp_parser_parse_tentatively (parser);
30382   rcv = cp_parser_simple_type_specifier (parser,
30383 					 /*decl_specs=*/NULL,
30384 					 CP_PARSER_FLAGS_NONE);
30385 
30386   if (cp_parser_parse_definitely (parser))
30387     return objc_get_class_reference (rcv);
30388 
30389   cp_parser_error (parser, "objective-c++ message receiver expected");
30390   return error_mark_node;
30391 }
30392 
30393 /* Parse the arguments and selectors comprising an Objective-C message.
30394 
30395    objc-message-args:
30396      objc-selector
30397      objc-selector-args
30398      objc-selector-args , objc-comma-args
30399 
30400    objc-selector-args:
30401      objc-selector [opt] : assignment-expression
30402      objc-selector-args objc-selector [opt] : assignment-expression
30403 
30404    objc-comma-args:
30405      assignment-expression
30406      objc-comma-args , assignment-expression
30407 
30408    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30409    selector arguments and TREE_VALUE containing a list of comma
30410    arguments.  */
30411 
30412 static tree
cp_parser_objc_message_args(cp_parser * parser)30413 cp_parser_objc_message_args (cp_parser* parser)
30414 {
30415   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30416   bool maybe_unary_selector_p = true;
30417   cp_token *token = cp_lexer_peek_token (parser->lexer);
30418 
30419   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30420     {
30421       tree selector = NULL_TREE, arg;
30422 
30423       if (token->type != CPP_COLON)
30424 	selector = cp_parser_objc_selector (parser);
30425 
30426       /* Detect if we have a unary selector.  */
30427       if (maybe_unary_selector_p
30428 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30429 	return build_tree_list (selector, NULL_TREE);
30430 
30431       maybe_unary_selector_p = false;
30432       cp_parser_require (parser, CPP_COLON, RT_COLON);
30433       arg = cp_parser_assignment_expression (parser);
30434 
30435       sel_args
30436 	= chainon (sel_args,
30437 		   build_tree_list (selector, arg));
30438 
30439       token = cp_lexer_peek_token (parser->lexer);
30440     }
30441 
30442   /* Handle non-selector arguments, if any. */
30443   while (token->type == CPP_COMMA)
30444     {
30445       tree arg;
30446 
30447       cp_lexer_consume_token (parser->lexer);
30448       arg = cp_parser_assignment_expression (parser);
30449 
30450       addl_args
30451 	= chainon (addl_args,
30452 		   build_tree_list (NULL_TREE, arg));
30453 
30454       token = cp_lexer_peek_token (parser->lexer);
30455     }
30456 
30457   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30458     {
30459       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30460       return build_tree_list (error_mark_node, error_mark_node);
30461     }
30462 
30463   return build_tree_list (sel_args, addl_args);
30464 }
30465 
30466 /* Parse an Objective-C encode expression.
30467 
30468    objc-encode-expression:
30469      @encode objc-typename
30470 
30471    Returns an encoded representation of the type argument.  */
30472 
30473 static cp_expr
cp_parser_objc_encode_expression(cp_parser * parser)30474 cp_parser_objc_encode_expression (cp_parser* parser)
30475 {
30476   tree type;
30477   cp_token *token;
30478   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30479 
30480   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
30481   matching_parens parens;
30482   parens.require_open (parser);
30483   token = cp_lexer_peek_token (parser->lexer);
30484   type = complete_type (cp_parser_type_id (parser));
30485   parens.require_close (parser);
30486 
30487   if (!type)
30488     {
30489       error_at (token->location,
30490 		"%<@encode%> must specify a type as an argument");
30491       return error_mark_node;
30492     }
30493 
30494   /* This happens if we find @encode(T) (where T is a template
30495      typename or something dependent on a template typename) when
30496      parsing a template.  In that case, we can't compile it
30497      immediately, but we rather create an AT_ENCODE_EXPR which will
30498      need to be instantiated when the template is used.
30499   */
30500   if (dependent_type_p (type))
30501     {
30502       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30503       TREE_READONLY (value) = 1;
30504       return value;
30505     }
30506 
30507 
30508   /* Build a location of the form:
30509        @encode(int)
30510        ^~~~~~~~~~~~
30511      with caret==start at the @ token, finishing at the close paren.  */
30512   location_t combined_loc
30513     = make_location (start_loc, start_loc,
30514                      cp_lexer_previous_token (parser->lexer)->location);
30515 
30516   return cp_expr (objc_build_encode_expr (type), combined_loc);
30517 }
30518 
30519 /* Parse an Objective-C @defs expression.  */
30520 
30521 static tree
cp_parser_objc_defs_expression(cp_parser * parser)30522 cp_parser_objc_defs_expression (cp_parser *parser)
30523 {
30524   tree name;
30525 
30526   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
30527   matching_parens parens;
30528   parens.require_open (parser);
30529   name = cp_parser_identifier (parser);
30530   parens.require_close (parser);
30531 
30532   return objc_get_class_ivars (name);
30533 }
30534 
30535 /* Parse an Objective-C protocol expression.
30536 
30537   objc-protocol-expression:
30538     @protocol ( identifier )
30539 
30540   Returns a representation of the protocol expression.  */
30541 
30542 static tree
cp_parser_objc_protocol_expression(cp_parser * parser)30543 cp_parser_objc_protocol_expression (cp_parser* parser)
30544 {
30545   tree proto;
30546   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30547 
30548   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
30549   matching_parens parens;
30550   parens.require_open (parser);
30551   proto = cp_parser_identifier (parser);
30552   parens.require_close (parser);
30553 
30554   /* Build a location of the form:
30555        @protocol(prot)
30556        ^~~~~~~~~~~~~~~
30557      with caret==start at the @ token, finishing at the close paren.  */
30558   location_t combined_loc
30559     = make_location (start_loc, start_loc,
30560                      cp_lexer_previous_token (parser->lexer)->location);
30561   tree result = objc_build_protocol_expr (proto);
30562   protected_set_expr_location (result, combined_loc);
30563   return result;
30564 }
30565 
30566 /* Parse an Objective-C selector expression.
30567 
30568    objc-selector-expression:
30569      @selector ( objc-method-signature )
30570 
30571    objc-method-signature:
30572      objc-selector
30573      objc-selector-seq
30574 
30575    objc-selector-seq:
30576      objc-selector :
30577      objc-selector-seq objc-selector :
30578 
30579   Returns a representation of the method selector.  */
30580 
30581 static tree
cp_parser_objc_selector_expression(cp_parser * parser)30582 cp_parser_objc_selector_expression (cp_parser* parser)
30583 {
30584   tree sel_seq = NULL_TREE;
30585   bool maybe_unary_selector_p = true;
30586   cp_token *token;
30587   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30588 
30589   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
30590   matching_parens parens;
30591   parens.require_open (parser);
30592   token = cp_lexer_peek_token (parser->lexer);
30593 
30594   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30595 	 || token->type == CPP_SCOPE)
30596     {
30597       tree selector = NULL_TREE;
30598 
30599       if (token->type != CPP_COLON
30600 	  || token->type == CPP_SCOPE)
30601 	selector = cp_parser_objc_selector (parser);
30602 
30603       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30604 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30605 	{
30606 	  /* Detect if we have a unary selector.  */
30607 	  if (maybe_unary_selector_p)
30608 	    {
30609 	      sel_seq = selector;
30610 	      goto finish_selector;
30611 	    }
30612 	  else
30613 	    {
30614 	      cp_parser_error (parser, "expected %<:%>");
30615 	    }
30616 	}
30617       maybe_unary_selector_p = false;
30618       token = cp_lexer_consume_token (parser->lexer);
30619 
30620       if (token->type == CPP_SCOPE)
30621 	{
30622 	  sel_seq
30623 	    = chainon (sel_seq,
30624 		       build_tree_list (selector, NULL_TREE));
30625 	  sel_seq
30626 	    = chainon (sel_seq,
30627 		       build_tree_list (NULL_TREE, NULL_TREE));
30628 	}
30629       else
30630 	sel_seq
30631 	  = chainon (sel_seq,
30632 		     build_tree_list (selector, NULL_TREE));
30633 
30634       token = cp_lexer_peek_token (parser->lexer);
30635     }
30636 
30637  finish_selector:
30638   parens.require_close (parser);
30639 
30640 
30641   /* Build a location of the form:
30642        @selector(func)
30643        ^~~~~~~~~~~~~~~
30644      with caret==start at the @ token, finishing at the close paren.  */
30645   location_t combined_loc
30646     = make_location (loc, loc,
30647                      cp_lexer_previous_token (parser->lexer)->location);
30648   tree result = objc_build_selector_expr (combined_loc, sel_seq);
30649   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
30650   protected_set_expr_location (result, combined_loc);
30651   return result;
30652 }
30653 
30654 /* Parse a list of identifiers.
30655 
30656    objc-identifier-list:
30657      identifier
30658      objc-identifier-list , identifier
30659 
30660    Returns a TREE_LIST of identifier nodes.  */
30661 
30662 static tree
cp_parser_objc_identifier_list(cp_parser * parser)30663 cp_parser_objc_identifier_list (cp_parser* parser)
30664 {
30665   tree identifier;
30666   tree list;
30667   cp_token *sep;
30668 
30669   identifier = cp_parser_identifier (parser);
30670   if (identifier == error_mark_node)
30671     return error_mark_node;
30672 
30673   list = build_tree_list (NULL_TREE, identifier);
30674   sep = cp_lexer_peek_token (parser->lexer);
30675 
30676   while (sep->type == CPP_COMMA)
30677     {
30678       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30679       identifier = cp_parser_identifier (parser);
30680       if (identifier == error_mark_node)
30681 	return list;
30682 
30683       list = chainon (list, build_tree_list (NULL_TREE,
30684 					     identifier));
30685       sep = cp_lexer_peek_token (parser->lexer);
30686     }
30687 
30688   return list;
30689 }
30690 
30691 /* Parse an Objective-C alias declaration.
30692 
30693    objc-alias-declaration:
30694      @compatibility_alias identifier identifier ;
30695 
30696    This function registers the alias mapping with the Objective-C front end.
30697    It returns nothing.  */
30698 
30699 static void
cp_parser_objc_alias_declaration(cp_parser * parser)30700 cp_parser_objc_alias_declaration (cp_parser* parser)
30701 {
30702   tree alias, orig;
30703 
30704   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
30705   alias = cp_parser_identifier (parser);
30706   orig = cp_parser_identifier (parser);
30707   objc_declare_alias (alias, orig);
30708   cp_parser_consume_semicolon_at_end_of_statement (parser);
30709 }
30710 
30711 /* Parse an Objective-C class forward-declaration.
30712 
30713    objc-class-declaration:
30714      @class objc-identifier-list ;
30715 
30716    The function registers the forward declarations with the Objective-C
30717    front end.  It returns nothing.  */
30718 
30719 static void
cp_parser_objc_class_declaration(cp_parser * parser)30720 cp_parser_objc_class_declaration (cp_parser* parser)
30721 {
30722   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
30723   while (true)
30724     {
30725       tree id;
30726 
30727       id = cp_parser_identifier (parser);
30728       if (id == error_mark_node)
30729 	break;
30730 
30731       objc_declare_class (id);
30732 
30733       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30734 	cp_lexer_consume_token (parser->lexer);
30735       else
30736 	break;
30737     }
30738   cp_parser_consume_semicolon_at_end_of_statement (parser);
30739 }
30740 
30741 /* Parse a list of Objective-C protocol references.
30742 
30743    objc-protocol-refs-opt:
30744      objc-protocol-refs [opt]
30745 
30746    objc-protocol-refs:
30747      < objc-identifier-list >
30748 
30749    Returns a TREE_LIST of identifiers, if any.  */
30750 
30751 static tree
cp_parser_objc_protocol_refs_opt(cp_parser * parser)30752 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30753 {
30754   tree protorefs = NULL_TREE;
30755 
30756   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30757     {
30758       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
30759       protorefs = cp_parser_objc_identifier_list (parser);
30760       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30761     }
30762 
30763   return protorefs;
30764 }
30765 
30766 /* Parse a Objective-C visibility specification.  */
30767 
30768 static void
cp_parser_objc_visibility_spec(cp_parser * parser)30769 cp_parser_objc_visibility_spec (cp_parser* parser)
30770 {
30771   cp_token *vis = cp_lexer_peek_token (parser->lexer);
30772 
30773   switch (vis->keyword)
30774     {
30775     case RID_AT_PRIVATE:
30776       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30777       break;
30778     case RID_AT_PROTECTED:
30779       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30780       break;
30781     case RID_AT_PUBLIC:
30782       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30783       break;
30784     case RID_AT_PACKAGE:
30785       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30786       break;
30787     default:
30788       return;
30789     }
30790 
30791   /* Eat '@private'/'@protected'/'@public'.  */
30792   cp_lexer_consume_token (parser->lexer);
30793 }
30794 
30795 /* Parse an Objective-C method type.  Return 'true' if it is a class
30796    (+) method, and 'false' if it is an instance (-) method.  */
30797 
30798 static inline bool
cp_parser_objc_method_type(cp_parser * parser)30799 cp_parser_objc_method_type (cp_parser* parser)
30800 {
30801   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30802     return true;
30803   else
30804     return false;
30805 }
30806 
30807 /* Parse an Objective-C protocol qualifier.  */
30808 
30809 static tree
cp_parser_objc_protocol_qualifiers(cp_parser * parser)30810 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30811 {
30812   tree quals = NULL_TREE, node;
30813   cp_token *token = cp_lexer_peek_token (parser->lexer);
30814 
30815   node = token->u.value;
30816 
30817   while (node && identifier_p (node)
30818 	 && (node == ridpointers [(int) RID_IN]
30819 	     || node == ridpointers [(int) RID_OUT]
30820 	     || node == ridpointers [(int) RID_INOUT]
30821 	     || node == ridpointers [(int) RID_BYCOPY]
30822 	     || node == ridpointers [(int) RID_BYREF]
30823 	     || node == ridpointers [(int) RID_ONEWAY]))
30824     {
30825       quals = tree_cons (NULL_TREE, node, quals);
30826       cp_lexer_consume_token (parser->lexer);
30827       token = cp_lexer_peek_token (parser->lexer);
30828       node = token->u.value;
30829     }
30830 
30831   return quals;
30832 }
30833 
30834 /* Parse an Objective-C typename.  */
30835 
30836 static tree
cp_parser_objc_typename(cp_parser * parser)30837 cp_parser_objc_typename (cp_parser* parser)
30838 {
30839   tree type_name = NULL_TREE;
30840 
30841   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30842     {
30843       tree proto_quals, cp_type = NULL_TREE;
30844 
30845       matching_parens parens;
30846       parens.consume_open (parser); /* Eat '('.  */
30847       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30848 
30849       /* An ObjC type name may consist of just protocol qualifiers, in which
30850 	 case the type shall default to 'id'.  */
30851       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30852 	{
30853 	  cp_type = cp_parser_type_id (parser);
30854 
30855 	  /* If the type could not be parsed, an error has already
30856 	     been produced.  For error recovery, behave as if it had
30857 	     not been specified, which will use the default type
30858 	     'id'.  */
30859 	  if (cp_type == error_mark_node)
30860 	    {
30861 	      cp_type = NULL_TREE;
30862 	      /* We need to skip to the closing parenthesis as
30863 		 cp_parser_type_id() does not seem to do it for
30864 		 us.  */
30865 	      cp_parser_skip_to_closing_parenthesis (parser,
30866 						     /*recovering=*/true,
30867 						     /*or_comma=*/false,
30868 						     /*consume_paren=*/false);
30869 	    }
30870 	}
30871 
30872       parens.require_close (parser);
30873       type_name = build_tree_list (proto_quals, cp_type);
30874     }
30875 
30876   return type_name;
30877 }
30878 
30879 /* Check to see if TYPE refers to an Objective-C selector name.  */
30880 
30881 static bool
cp_parser_objc_selector_p(enum cpp_ttype type)30882 cp_parser_objc_selector_p (enum cpp_ttype type)
30883 {
30884   return (type == CPP_NAME || type == CPP_KEYWORD
30885 	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30886 	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30887 	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30888 	  || type == CPP_XOR || type == CPP_XOR_EQ);
30889 }
30890 
30891 /* Parse an Objective-C selector.  */
30892 
30893 static tree
cp_parser_objc_selector(cp_parser * parser)30894 cp_parser_objc_selector (cp_parser* parser)
30895 {
30896   cp_token *token = cp_lexer_consume_token (parser->lexer);
30897 
30898   if (!cp_parser_objc_selector_p (token->type))
30899     {
30900       error_at (token->location, "invalid Objective-C++ selector name");
30901       return error_mark_node;
30902     }
30903 
30904   /* C++ operator names are allowed to appear in ObjC selectors.  */
30905   switch (token->type)
30906     {
30907     case CPP_AND_AND: return get_identifier ("and");
30908     case CPP_AND_EQ: return get_identifier ("and_eq");
30909     case CPP_AND: return get_identifier ("bitand");
30910     case CPP_OR: return get_identifier ("bitor");
30911     case CPP_COMPL: return get_identifier ("compl");
30912     case CPP_NOT: return get_identifier ("not");
30913     case CPP_NOT_EQ: return get_identifier ("not_eq");
30914     case CPP_OR_OR: return get_identifier ("or");
30915     case CPP_OR_EQ: return get_identifier ("or_eq");
30916     case CPP_XOR: return get_identifier ("xor");
30917     case CPP_XOR_EQ: return get_identifier ("xor_eq");
30918     default: return token->u.value;
30919     }
30920 }
30921 
30922 /* Parse an Objective-C params list.  */
30923 
30924 static tree
cp_parser_objc_method_keyword_params(cp_parser * parser,tree * attributes)30925 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30926 {
30927   tree params = NULL_TREE;
30928   bool maybe_unary_selector_p = true;
30929   cp_token *token = cp_lexer_peek_token (parser->lexer);
30930 
30931   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30932     {
30933       tree selector = NULL_TREE, type_name, identifier;
30934       tree parm_attr = NULL_TREE;
30935 
30936       if (token->keyword == RID_ATTRIBUTE)
30937 	break;
30938 
30939       if (token->type != CPP_COLON)
30940 	selector = cp_parser_objc_selector (parser);
30941 
30942       /* Detect if we have a unary selector.  */
30943       if (maybe_unary_selector_p
30944 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30945 	{
30946 	  params = selector; /* Might be followed by attributes.  */
30947 	  break;
30948 	}
30949 
30950       maybe_unary_selector_p = false;
30951       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30952 	{
30953 	  /* Something went quite wrong.  There should be a colon
30954 	     here, but there is not.  Stop parsing parameters.  */
30955 	  break;
30956 	}
30957       type_name = cp_parser_objc_typename (parser);
30958       /* New ObjC allows attributes on parameters too.  */
30959       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30960 	parm_attr = cp_parser_attributes_opt (parser);
30961       identifier = cp_parser_identifier (parser);
30962 
30963       params
30964 	= chainon (params,
30965 		   objc_build_keyword_decl (selector,
30966 					    type_name,
30967 					    identifier,
30968 					    parm_attr));
30969 
30970       token = cp_lexer_peek_token (parser->lexer);
30971     }
30972 
30973   if (params == NULL_TREE)
30974     {
30975       cp_parser_error (parser, "objective-c++ method declaration is expected");
30976       return error_mark_node;
30977     }
30978 
30979   /* We allow tail attributes for the method.  */
30980   if (token->keyword == RID_ATTRIBUTE)
30981     {
30982       *attributes = cp_parser_attributes_opt (parser);
30983       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30984 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30985 	return params;
30986       cp_parser_error (parser,
30987 		       "method attributes must be specified at the end");
30988       return error_mark_node;
30989     }
30990 
30991   if (params == NULL_TREE)
30992     {
30993       cp_parser_error (parser, "objective-c++ method declaration is expected");
30994       return error_mark_node;
30995     }
30996   return params;
30997 }
30998 
30999 /* Parse the non-keyword Objective-C params.  */
31000 
31001 static tree
cp_parser_objc_method_tail_params_opt(cp_parser * parser,bool * ellipsisp,tree * attributes)31002 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
31003 				       tree* attributes)
31004 {
31005   tree params = make_node (TREE_LIST);
31006   cp_token *token = cp_lexer_peek_token (parser->lexer);
31007   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
31008 
31009   while (token->type == CPP_COMMA)
31010     {
31011       cp_parameter_declarator *parmdecl;
31012       tree parm;
31013 
31014       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
31015       token = cp_lexer_peek_token (parser->lexer);
31016 
31017       if (token->type == CPP_ELLIPSIS)
31018 	{
31019 	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
31020 	  *ellipsisp = true;
31021 	  token = cp_lexer_peek_token (parser->lexer);
31022 	  break;
31023 	}
31024 
31025       /* TODO: parse attributes for tail parameters.  */
31026       parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31027 						  false, NULL);
31028       parm = grokdeclarator (parmdecl->declarator,
31029 			     &parmdecl->decl_specifiers,
31030 			     PARM, /*initialized=*/0,
31031 			     /*attrlist=*/NULL);
31032 
31033       chainon (params, build_tree_list (NULL_TREE, parm));
31034       token = cp_lexer_peek_token (parser->lexer);
31035     }
31036 
31037   /* We allow tail attributes for the method.  */
31038   if (token->keyword == RID_ATTRIBUTE)
31039     {
31040       if (*attributes == NULL_TREE)
31041 	{
31042 	  *attributes = cp_parser_attributes_opt (parser);
31043 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
31044 	      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31045 	    return params;
31046 	}
31047       else
31048 	/* We have an error, but parse the attributes, so that we can
31049 	   carry on.  */
31050 	*attributes = cp_parser_attributes_opt (parser);
31051 
31052       cp_parser_error (parser,
31053 		       "method attributes must be specified at the end");
31054       return error_mark_node;
31055     }
31056 
31057   return params;
31058 }
31059 
31060 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
31061 
31062 static void
cp_parser_objc_interstitial_code(cp_parser * parser)31063 cp_parser_objc_interstitial_code (cp_parser* parser)
31064 {
31065   cp_token *token = cp_lexer_peek_token (parser->lexer);
31066 
31067   /* If the next token is `extern' and the following token is a string
31068      literal, then we have a linkage specification.  */
31069   if (token->keyword == RID_EXTERN
31070       && cp_parser_is_pure_string_literal
31071 	 (cp_lexer_peek_nth_token (parser->lexer, 2)))
31072     cp_parser_linkage_specification (parser);
31073   /* Handle #pragma, if any.  */
31074   else if (token->type == CPP_PRAGMA)
31075     cp_parser_pragma (parser, pragma_objc_icode, NULL);
31076   /* Allow stray semicolons.  */
31077   else if (token->type == CPP_SEMICOLON)
31078     cp_lexer_consume_token (parser->lexer);
31079   /* Mark methods as optional or required, when building protocols.  */
31080   else if (token->keyword == RID_AT_OPTIONAL)
31081     {
31082       cp_lexer_consume_token (parser->lexer);
31083       objc_set_method_opt (true);
31084     }
31085   else if (token->keyword == RID_AT_REQUIRED)
31086     {
31087       cp_lexer_consume_token (parser->lexer);
31088       objc_set_method_opt (false);
31089     }
31090   else if (token->keyword == RID_NAMESPACE)
31091     cp_parser_namespace_definition (parser);
31092   /* Other stray characters must generate errors.  */
31093   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
31094     {
31095       cp_lexer_consume_token (parser->lexer);
31096       error ("stray %qs between Objective-C++ methods",
31097 	     token->type == CPP_OPEN_BRACE ? "{" : "}");
31098     }
31099   /* Finally, try to parse a block-declaration, or a function-definition.  */
31100   else
31101     cp_parser_block_declaration (parser, /*statement_p=*/false);
31102 }
31103 
31104 /* Parse a method signature.  */
31105 
31106 static tree
cp_parser_objc_method_signature(cp_parser * parser,tree * attributes)31107 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
31108 {
31109   tree rettype, kwdparms, optparms;
31110   bool ellipsis = false;
31111   bool is_class_method;
31112 
31113   is_class_method = cp_parser_objc_method_type (parser);
31114   rettype = cp_parser_objc_typename (parser);
31115   *attributes = NULL_TREE;
31116   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
31117   if (kwdparms == error_mark_node)
31118     return error_mark_node;
31119   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
31120   if (optparms == error_mark_node)
31121     return error_mark_node;
31122 
31123   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
31124 }
31125 
31126 static bool
cp_parser_objc_method_maybe_bad_prefix_attributes(cp_parser * parser)31127 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
31128 {
31129   tree tattr;
31130   cp_lexer_save_tokens (parser->lexer);
31131   tattr = cp_parser_attributes_opt (parser);
31132   gcc_assert (tattr) ;
31133 
31134   /* If the attributes are followed by a method introducer, this is not allowed.
31135      Dump the attributes and flag the situation.  */
31136   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
31137       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31138     return true;
31139 
31140   /* Otherwise, the attributes introduce some interstitial code, possibly so
31141      rewind to allow that check.  */
31142   cp_lexer_rollback_tokens (parser->lexer);
31143   return false;
31144 }
31145 
31146 /* Parse an Objective-C method prototype list.  */
31147 
31148 static void
cp_parser_objc_method_prototype_list(cp_parser * parser)31149 cp_parser_objc_method_prototype_list (cp_parser* parser)
31150 {
31151   cp_token *token = cp_lexer_peek_token (parser->lexer);
31152 
31153   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31154     {
31155       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31156 	{
31157 	  tree attributes, sig;
31158 	  bool is_class_method;
31159 	  if (token->type == CPP_PLUS)
31160 	    is_class_method = true;
31161 	  else
31162 	    is_class_method = false;
31163 	  sig = cp_parser_objc_method_signature (parser, &attributes);
31164 	  if (sig == error_mark_node)
31165 	    {
31166 	      cp_parser_skip_to_end_of_block_or_statement (parser);
31167 	      token = cp_lexer_peek_token (parser->lexer);
31168 	      continue;
31169 	    }
31170 	  objc_add_method_declaration (is_class_method, sig, attributes);
31171 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
31172 	}
31173       else if (token->keyword == RID_AT_PROPERTY)
31174 	cp_parser_objc_at_property_declaration (parser);
31175       else if (token->keyword == RID_ATTRIBUTE
31176       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31177 	warning_at (cp_lexer_peek_token (parser->lexer)->location,
31178 		    OPT_Wattributes,
31179 		    "prefix attributes are ignored for methods");
31180       else
31181 	/* Allow for interspersed non-ObjC++ code.  */
31182 	cp_parser_objc_interstitial_code (parser);
31183 
31184       token = cp_lexer_peek_token (parser->lexer);
31185     }
31186 
31187   if (token->type != CPP_EOF)
31188     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
31189   else
31190     cp_parser_error (parser, "expected %<@end%>");
31191 
31192   objc_finish_interface ();
31193 }
31194 
31195 /* Parse an Objective-C method definition list.  */
31196 
31197 static void
cp_parser_objc_method_definition_list(cp_parser * parser)31198 cp_parser_objc_method_definition_list (cp_parser* parser)
31199 {
31200   cp_token *token = cp_lexer_peek_token (parser->lexer);
31201 
31202   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
31203     {
31204       tree meth;
31205 
31206       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
31207 	{
31208 	  cp_token *ptk;
31209 	  tree sig, attribute;
31210 	  bool is_class_method;
31211 	  if (token->type == CPP_PLUS)
31212 	    is_class_method = true;
31213 	  else
31214 	    is_class_method = false;
31215 	  push_deferring_access_checks (dk_deferred);
31216 	  sig = cp_parser_objc_method_signature (parser, &attribute);
31217 	  if (sig == error_mark_node)
31218 	    {
31219 	      cp_parser_skip_to_end_of_block_or_statement (parser);
31220 	      token = cp_lexer_peek_token (parser->lexer);
31221 	      continue;
31222 	    }
31223 	  objc_start_method_definition (is_class_method, sig, attribute,
31224 					NULL_TREE);
31225 
31226 	  /* For historical reasons, we accept an optional semicolon.  */
31227 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31228 	    cp_lexer_consume_token (parser->lexer);
31229 
31230 	  ptk = cp_lexer_peek_token (parser->lexer);
31231 	  if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31232 		|| ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31233 	    {
31234 	      perform_deferred_access_checks (tf_warning_or_error);
31235 	      stop_deferring_access_checks ();
31236 	      meth = cp_parser_function_definition_after_declarator (parser,
31237 								     false);
31238 	      pop_deferring_access_checks ();
31239 	      objc_finish_method_definition (meth);
31240 	    }
31241 	}
31242       /* The following case will be removed once @synthesize is
31243 	 completely implemented.  */
31244       else if (token->keyword == RID_AT_PROPERTY)
31245 	cp_parser_objc_at_property_declaration (parser);
31246       else if (token->keyword == RID_AT_SYNTHESIZE)
31247 	cp_parser_objc_at_synthesize_declaration (parser);
31248       else if (token->keyword == RID_AT_DYNAMIC)
31249 	cp_parser_objc_at_dynamic_declaration (parser);
31250       else if (token->keyword == RID_ATTRIBUTE
31251       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31252 	warning_at (token->location, OPT_Wattributes,
31253 	       	    "prefix attributes are ignored for methods");
31254       else
31255 	/* Allow for interspersed non-ObjC++ code.  */
31256 	cp_parser_objc_interstitial_code (parser);
31257 
31258       token = cp_lexer_peek_token (parser->lexer);
31259     }
31260 
31261   if (token->type != CPP_EOF)
31262     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
31263   else
31264     cp_parser_error (parser, "expected %<@end%>");
31265 
31266   objc_finish_implementation ();
31267 }
31268 
31269 /* Parse Objective-C ivars.  */
31270 
31271 static void
cp_parser_objc_class_ivars(cp_parser * parser)31272 cp_parser_objc_class_ivars (cp_parser* parser)
31273 {
31274   cp_token *token = cp_lexer_peek_token (parser->lexer);
31275 
31276   if (token->type != CPP_OPEN_BRACE)
31277     return;	/* No ivars specified.  */
31278 
31279   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
31280   token = cp_lexer_peek_token (parser->lexer);
31281 
31282   while (token->type != CPP_CLOSE_BRACE
31283 	&& token->keyword != RID_AT_END && token->type != CPP_EOF)
31284     {
31285       cp_decl_specifier_seq declspecs;
31286       int decl_class_or_enum_p;
31287       tree prefix_attributes;
31288 
31289       cp_parser_objc_visibility_spec (parser);
31290 
31291       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31292 	break;
31293 
31294       cp_parser_decl_specifier_seq (parser,
31295 				    CP_PARSER_FLAGS_OPTIONAL,
31296 				    &declspecs,
31297 				    &decl_class_or_enum_p);
31298 
31299       /* auto, register, static, extern, mutable.  */
31300       if (declspecs.storage_class != sc_none)
31301 	{
31302 	  cp_parser_error (parser, "invalid type for instance variable");
31303 	  declspecs.storage_class = sc_none;
31304 	}
31305 
31306       /* thread_local.  */
31307       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31308 	{
31309 	  cp_parser_error (parser, "invalid type for instance variable");
31310 	  declspecs.locations[ds_thread] = 0;
31311 	}
31312 
31313       /* typedef.  */
31314       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31315 	{
31316 	  cp_parser_error (parser, "invalid type for instance variable");
31317 	  declspecs.locations[ds_typedef] = 0;
31318 	}
31319 
31320       prefix_attributes = declspecs.attributes;
31321       declspecs.attributes = NULL_TREE;
31322 
31323       /* Keep going until we hit the `;' at the end of the
31324 	 declaration.  */
31325       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31326 	{
31327 	  tree width = NULL_TREE, attributes, first_attribute, decl;
31328 	  cp_declarator *declarator = NULL;
31329 	  int ctor_dtor_or_conv_p;
31330 
31331 	  /* Check for a (possibly unnamed) bitfield declaration.  */
31332 	  token = cp_lexer_peek_token (parser->lexer);
31333 	  if (token->type == CPP_COLON)
31334 	    goto eat_colon;
31335 
31336 	  if (token->type == CPP_NAME
31337 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31338 		  == CPP_COLON))
31339 	    {
31340 	      /* Get the name of the bitfield.  */
31341 	      declarator = make_id_declarator (NULL_TREE,
31342 					       cp_parser_identifier (parser),
31343 					       sfk_none, token->location);
31344 
31345 	     eat_colon:
31346 	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
31347 	      /* Get the width of the bitfield.  */
31348 	      width
31349 		= cp_parser_constant_expression (parser);
31350 	    }
31351 	  else
31352 	    {
31353 	      /* Parse the declarator.  */
31354 	      declarator
31355 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31356 					CP_PARSER_FLAGS_NONE,
31357 					&ctor_dtor_or_conv_p,
31358 					/*parenthesized_p=*/NULL,
31359 					/*member_p=*/false,
31360 					/*friend_p=*/false,
31361 					/*static_p=*/false);
31362 	    }
31363 
31364 	  /* Look for attributes that apply to the ivar.  */
31365 	  attributes = cp_parser_attributes_opt (parser);
31366 	  /* Remember which attributes are prefix attributes and
31367 	     which are not.  */
31368 	  first_attribute = attributes;
31369 	  /* Combine the attributes.  */
31370 	  attributes = attr_chainon (prefix_attributes, attributes);
31371 
31372 	  if (width)
31373 	    /* Create the bitfield declaration.  */
31374 	    decl = grokbitfield (declarator, &declspecs,
31375 				 width, NULL_TREE, attributes);
31376 	  else
31377 	    decl = grokfield (declarator, &declspecs,
31378 			      NULL_TREE, /*init_const_expr_p=*/false,
31379 			      NULL_TREE, attributes);
31380 
31381 	  /* Add the instance variable.  */
31382 	  if (decl != error_mark_node && decl != NULL_TREE)
31383 	    objc_add_instance_variable (decl);
31384 
31385 	  /* Reset PREFIX_ATTRIBUTES.  */
31386 	  if (attributes != error_mark_node)
31387 	    {
31388 	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
31389 		attributes = TREE_CHAIN (attributes);
31390 	      if (attributes)
31391 		TREE_CHAIN (attributes) = NULL_TREE;
31392 	    }
31393 
31394 	  token = cp_lexer_peek_token (parser->lexer);
31395 
31396 	  if (token->type == CPP_COMMA)
31397 	    {
31398 	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
31399 	      continue;
31400 	    }
31401 	  break;
31402 	}
31403 
31404       cp_parser_consume_semicolon_at_end_of_statement (parser);
31405       token = cp_lexer_peek_token (parser->lexer);
31406     }
31407 
31408   if (token->keyword == RID_AT_END)
31409     cp_parser_error (parser, "expected %<}%>");
31410 
31411   /* Do not consume the RID_AT_END, so it will be read again as terminating
31412      the @interface of @implementation.  */
31413   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31414     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
31415 
31416   /* For historical reasons, we accept an optional semicolon.  */
31417   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31418     cp_lexer_consume_token (parser->lexer);
31419 }
31420 
31421 /* Parse an Objective-C protocol declaration.  */
31422 
31423 static void
cp_parser_objc_protocol_declaration(cp_parser * parser,tree attributes)31424 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31425 {
31426   tree proto, protorefs;
31427   cp_token *tok;
31428 
31429   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
31430   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31431     {
31432       tok = cp_lexer_peek_token (parser->lexer);
31433       error_at (tok->location, "identifier expected after %<@protocol%>");
31434       cp_parser_consume_semicolon_at_end_of_statement (parser);
31435       return;
31436     }
31437 
31438   /* See if we have a forward declaration or a definition.  */
31439   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31440 
31441   /* Try a forward declaration first.  */
31442   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31443     {
31444       while (true)
31445 	{
31446 	  tree id;
31447 
31448 	  id = cp_parser_identifier (parser);
31449 	  if (id == error_mark_node)
31450 	    break;
31451 
31452 	  objc_declare_protocol (id, attributes);
31453 
31454 	  if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31455 	    cp_lexer_consume_token (parser->lexer);
31456 	  else
31457 	    break;
31458 	}
31459       cp_parser_consume_semicolon_at_end_of_statement (parser);
31460     }
31461 
31462   /* Ok, we got a full-fledged definition (or at least should).  */
31463   else
31464     {
31465       proto = cp_parser_identifier (parser);
31466       protorefs = cp_parser_objc_protocol_refs_opt (parser);
31467       objc_start_protocol (proto, protorefs, attributes);
31468       cp_parser_objc_method_prototype_list (parser);
31469     }
31470 }
31471 
31472 /* Parse an Objective-C superclass or category.  */
31473 
31474 static void
cp_parser_objc_superclass_or_category(cp_parser * parser,bool iface_p,tree * super,tree * categ,bool * is_class_extension)31475 cp_parser_objc_superclass_or_category (cp_parser *parser,
31476 				       bool iface_p,
31477 				       tree *super,
31478 				       tree *categ, bool *is_class_extension)
31479 {
31480   cp_token *next = cp_lexer_peek_token (parser->lexer);
31481 
31482   *super = *categ = NULL_TREE;
31483   *is_class_extension = false;
31484   if (next->type == CPP_COLON)
31485     {
31486       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
31487       *super = cp_parser_identifier (parser);
31488     }
31489   else if (next->type == CPP_OPEN_PAREN)
31490     {
31491       matching_parens parens;
31492       parens.consume_open (parser);  /* Eat '('.  */
31493 
31494       /* If there is no category name, and this is an @interface, we
31495 	 have a class extension.  */
31496       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31497 	{
31498 	  *categ = NULL_TREE;
31499 	  *is_class_extension = true;
31500 	}
31501       else
31502 	*categ = cp_parser_identifier (parser);
31503 
31504       parens.require_close (parser);
31505     }
31506 }
31507 
31508 /* Parse an Objective-C class interface.  */
31509 
31510 static void
cp_parser_objc_class_interface(cp_parser * parser,tree attributes)31511 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31512 {
31513   tree name, super, categ, protos;
31514   bool is_class_extension;
31515 
31516   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
31517   name = cp_parser_identifier (parser);
31518   if (name == error_mark_node)
31519     {
31520       /* It's hard to recover because even if valid @interface stuff
31521 	 is to follow, we can't compile it (or validate it) if we
31522 	 don't even know which class it refers to.  Let's assume this
31523 	 was a stray '@interface' token in the stream and skip it.
31524       */
31525       return;
31526     }
31527   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31528 					 &is_class_extension);
31529   protos = cp_parser_objc_protocol_refs_opt (parser);
31530 
31531   /* We have either a class or a category on our hands.  */
31532   if (categ || is_class_extension)
31533     objc_start_category_interface (name, categ, protos, attributes);
31534   else
31535     {
31536       objc_start_class_interface (name, super, protos, attributes);
31537       /* Handle instance variable declarations, if any.  */
31538       cp_parser_objc_class_ivars (parser);
31539       objc_continue_interface ();
31540     }
31541 
31542   cp_parser_objc_method_prototype_list (parser);
31543 }
31544 
31545 /* Parse an Objective-C class implementation.  */
31546 
31547 static void
cp_parser_objc_class_implementation(cp_parser * parser)31548 cp_parser_objc_class_implementation (cp_parser* parser)
31549 {
31550   tree name, super, categ;
31551   bool is_class_extension;
31552 
31553   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
31554   name = cp_parser_identifier (parser);
31555   if (name == error_mark_node)
31556     {
31557       /* It's hard to recover because even if valid @implementation
31558 	 stuff is to follow, we can't compile it (or validate it) if
31559 	 we don't even know which class it refers to.  Let's assume
31560 	 this was a stray '@implementation' token in the stream and
31561 	 skip it.
31562       */
31563       return;
31564     }
31565   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31566 					 &is_class_extension);
31567 
31568   /* We have either a class or a category on our hands.  */
31569   if (categ)
31570     objc_start_category_implementation (name, categ);
31571   else
31572     {
31573       objc_start_class_implementation (name, super);
31574       /* Handle instance variable declarations, if any.  */
31575       cp_parser_objc_class_ivars (parser);
31576       objc_continue_implementation ();
31577     }
31578 
31579   cp_parser_objc_method_definition_list (parser);
31580 }
31581 
31582 /* Consume the @end token and finish off the implementation.  */
31583 
31584 static void
cp_parser_objc_end_implementation(cp_parser * parser)31585 cp_parser_objc_end_implementation (cp_parser* parser)
31586 {
31587   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
31588   objc_finish_implementation ();
31589 }
31590 
31591 /* Parse an Objective-C declaration.  */
31592 
31593 static void
cp_parser_objc_declaration(cp_parser * parser,tree attributes)31594 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31595 {
31596   /* Try to figure out what kind of declaration is present.  */
31597   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31598 
31599   if (attributes)
31600     switch (kwd->keyword)
31601       {
31602 	case RID_AT_ALIAS:
31603 	case RID_AT_CLASS:
31604 	case RID_AT_END:
31605 	  error_at (kwd->location, "attributes may not be specified before"
31606 	            " the %<@%D%> Objective-C++ keyword",
31607 		    kwd->u.value);
31608 	  attributes = NULL;
31609 	  break;
31610 	case RID_AT_IMPLEMENTATION:
31611 	  warning_at (kwd->location, OPT_Wattributes,
31612 		      "prefix attributes are ignored before %<@%D%>",
31613 		      kwd->u.value);
31614 	  attributes = NULL;
31615 	default:
31616 	  break;
31617       }
31618 
31619   switch (kwd->keyword)
31620     {
31621     case RID_AT_ALIAS:
31622       cp_parser_objc_alias_declaration (parser);
31623       break;
31624     case RID_AT_CLASS:
31625       cp_parser_objc_class_declaration (parser);
31626       break;
31627     case RID_AT_PROTOCOL:
31628       cp_parser_objc_protocol_declaration (parser, attributes);
31629       break;
31630     case RID_AT_INTERFACE:
31631       cp_parser_objc_class_interface (parser, attributes);
31632       break;
31633     case RID_AT_IMPLEMENTATION:
31634       cp_parser_objc_class_implementation (parser);
31635       break;
31636     case RID_AT_END:
31637       cp_parser_objc_end_implementation (parser);
31638       break;
31639     default:
31640       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31641 		kwd->u.value);
31642       cp_parser_skip_to_end_of_block_or_statement (parser);
31643     }
31644 }
31645 
31646 /* Parse an Objective-C try-catch-finally statement.
31647 
31648    objc-try-catch-finally-stmt:
31649      @try compound-statement objc-catch-clause-seq [opt]
31650        objc-finally-clause [opt]
31651 
31652    objc-catch-clause-seq:
31653      objc-catch-clause objc-catch-clause-seq [opt]
31654 
31655    objc-catch-clause:
31656      @catch ( objc-exception-declaration ) compound-statement
31657 
31658    objc-finally-clause:
31659      @finally compound-statement
31660 
31661    objc-exception-declaration:
31662      parameter-declaration
31663      '...'
31664 
31665    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31666 
31667    Returns NULL_TREE.
31668 
31669    PS: This function is identical to c_parser_objc_try_catch_finally_statement
31670    for C.  Keep them in sync.  */
31671 
31672 static tree
cp_parser_objc_try_catch_finally_statement(cp_parser * parser)31673 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31674 {
31675   location_t location;
31676   tree stmt;
31677 
31678   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31679   location = cp_lexer_peek_token (parser->lexer)->location;
31680   objc_maybe_warn_exceptions (location);
31681   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31682      node, lest it get absorbed into the surrounding block.  */
31683   stmt = push_stmt_list ();
31684   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31685   objc_begin_try_stmt (location, pop_stmt_list (stmt));
31686 
31687   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31688     {
31689       cp_parameter_declarator *parm;
31690       tree parameter_declaration = error_mark_node;
31691       bool seen_open_paren = false;
31692       matching_parens parens;
31693 
31694       cp_lexer_consume_token (parser->lexer);
31695       if (parens.require_open (parser))
31696 	seen_open_paren = true;
31697       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31698 	{
31699 	  /* We have "@catch (...)" (where the '...' are literally
31700 	     what is in the code).  Skip the '...'.
31701 	     parameter_declaration is set to NULL_TREE, and
31702 	     objc_being_catch_clauses() knows that that means
31703 	     '...'.  */
31704 	  cp_lexer_consume_token (parser->lexer);
31705 	  parameter_declaration = NULL_TREE;
31706 	}
31707       else
31708 	{
31709 	  /* We have "@catch (NSException *exception)" or something
31710 	     like that.  Parse the parameter declaration.  */
31711 	  parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31712 						  false, NULL);
31713 	  if (parm == NULL)
31714 	    parameter_declaration = error_mark_node;
31715 	  else
31716 	    parameter_declaration = grokdeclarator (parm->declarator,
31717 						    &parm->decl_specifiers,
31718 						    PARM, /*initialized=*/0,
31719 						    /*attrlist=*/NULL);
31720 	}
31721       if (seen_open_paren)
31722 	parens.require_close (parser);
31723       else
31724 	{
31725 	  /* If there was no open parenthesis, we are recovering from
31726 	     an error, and we are trying to figure out what mistake
31727 	     the user has made.  */
31728 
31729 	  /* If there is an immediate closing parenthesis, the user
31730 	     probably forgot the opening one (ie, they typed "@catch
31731 	     NSException *e)".  Parse the closing parenthesis and keep
31732 	     going.  */
31733 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31734 	    cp_lexer_consume_token (parser->lexer);
31735 
31736 	  /* If these is no immediate closing parenthesis, the user
31737 	     probably doesn't know that parenthesis are required at
31738 	     all (ie, they typed "@catch NSException *e").  So, just
31739 	     forget about the closing parenthesis and keep going.  */
31740 	}
31741       objc_begin_catch_clause (parameter_declaration);
31742       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31743       objc_finish_catch_clause ();
31744     }
31745   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31746     {
31747       cp_lexer_consume_token (parser->lexer);
31748       location = cp_lexer_peek_token (parser->lexer)->location;
31749       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31750 	 node, lest it get absorbed into the surrounding block.  */
31751       stmt = push_stmt_list ();
31752       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31753       objc_build_finally_clause (location, pop_stmt_list (stmt));
31754     }
31755 
31756   return objc_finish_try_stmt ();
31757 }
31758 
31759 /* Parse an Objective-C synchronized statement.
31760 
31761    objc-synchronized-stmt:
31762      @synchronized ( expression ) compound-statement
31763 
31764    Returns NULL_TREE.  */
31765 
31766 static tree
cp_parser_objc_synchronized_statement(cp_parser * parser)31767 cp_parser_objc_synchronized_statement (cp_parser *parser)
31768 {
31769   location_t location;
31770   tree lock, stmt;
31771 
31772   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31773 
31774   location = cp_lexer_peek_token (parser->lexer)->location;
31775   objc_maybe_warn_exceptions (location);
31776   matching_parens parens;
31777   parens.require_open (parser);
31778   lock = cp_parser_expression (parser);
31779   parens.require_close (parser);
31780 
31781   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31782      node, lest it get absorbed into the surrounding block.  */
31783   stmt = push_stmt_list ();
31784   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31785 
31786   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31787 }
31788 
31789 /* Parse an Objective-C throw statement.
31790 
31791    objc-throw-stmt:
31792      @throw assignment-expression [opt] ;
31793 
31794    Returns a constructed '@throw' statement.  */
31795 
31796 static tree
cp_parser_objc_throw_statement(cp_parser * parser)31797 cp_parser_objc_throw_statement (cp_parser *parser)
31798 {
31799   tree expr = NULL_TREE;
31800   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31801 
31802   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31803 
31804   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31805     expr = cp_parser_expression (parser);
31806 
31807   cp_parser_consume_semicolon_at_end_of_statement (parser);
31808 
31809   return objc_build_throw_stmt (loc, expr);
31810 }
31811 
31812 /* Parse an Objective-C statement.  */
31813 
31814 static tree
cp_parser_objc_statement(cp_parser * parser)31815 cp_parser_objc_statement (cp_parser * parser)
31816 {
31817   /* Try to figure out what kind of declaration is present.  */
31818   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31819 
31820   switch (kwd->keyword)
31821     {
31822     case RID_AT_TRY:
31823       return cp_parser_objc_try_catch_finally_statement (parser);
31824     case RID_AT_SYNCHRONIZED:
31825       return cp_parser_objc_synchronized_statement (parser);
31826     case RID_AT_THROW:
31827       return cp_parser_objc_throw_statement (parser);
31828     default:
31829       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31830 	       kwd->u.value);
31831       cp_parser_skip_to_end_of_block_or_statement (parser);
31832     }
31833 
31834   return error_mark_node;
31835 }
31836 
31837 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31838    look ahead to see if an objc keyword follows the attributes.  This
31839    is to detect the use of prefix attributes on ObjC @interface and
31840    @protocol.  */
31841 
31842 static bool
cp_parser_objc_valid_prefix_attributes(cp_parser * parser,tree * attrib)31843 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31844 {
31845   cp_lexer_save_tokens (parser->lexer);
31846   *attrib = cp_parser_attributes_opt (parser);
31847   gcc_assert (*attrib);
31848   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31849     {
31850       cp_lexer_commit_tokens (parser->lexer);
31851       return true;
31852     }
31853   cp_lexer_rollback_tokens (parser->lexer);
31854   return false;
31855 }
31856 
31857 /* This routine is a minimal replacement for
31858    c_parser_struct_declaration () used when parsing the list of
31859    types/names or ObjC++ properties.  For example, when parsing the
31860    code
31861 
31862    @property (readonly) int a, b, c;
31863 
31864    this function is responsible for parsing "int a, int b, int c" and
31865    returning the declarations as CHAIN of DECLs.
31866 
31867    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
31868    similar parsing.  */
31869 static tree
cp_parser_objc_struct_declaration(cp_parser * parser)31870 cp_parser_objc_struct_declaration (cp_parser *parser)
31871 {
31872   tree decls = NULL_TREE;
31873   cp_decl_specifier_seq declspecs;
31874   int decl_class_or_enum_p;
31875   tree prefix_attributes;
31876 
31877   cp_parser_decl_specifier_seq (parser,
31878 				CP_PARSER_FLAGS_NONE,
31879 				&declspecs,
31880 				&decl_class_or_enum_p);
31881 
31882   if (declspecs.type == error_mark_node)
31883     return error_mark_node;
31884 
31885   /* auto, register, static, extern, mutable.  */
31886   if (declspecs.storage_class != sc_none)
31887     {
31888       cp_parser_error (parser, "invalid type for property");
31889       declspecs.storage_class = sc_none;
31890     }
31891 
31892   /* thread_local.  */
31893   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31894     {
31895       cp_parser_error (parser, "invalid type for property");
31896       declspecs.locations[ds_thread] = 0;
31897     }
31898 
31899   /* typedef.  */
31900   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31901     {
31902       cp_parser_error (parser, "invalid type for property");
31903       declspecs.locations[ds_typedef] = 0;
31904     }
31905 
31906   prefix_attributes = declspecs.attributes;
31907   declspecs.attributes = NULL_TREE;
31908 
31909   /* Keep going until we hit the `;' at the end of the declaration. */
31910   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31911     {
31912       tree attributes, first_attribute, decl;
31913       cp_declarator *declarator;
31914       cp_token *token;
31915 
31916       /* Parse the declarator.  */
31917       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31918 					 CP_PARSER_FLAGS_NONE,
31919 					 NULL, NULL, false, false, false);
31920 
31921       /* Look for attributes that apply to the ivar.  */
31922       attributes = cp_parser_attributes_opt (parser);
31923       /* Remember which attributes are prefix attributes and
31924 	 which are not.  */
31925       first_attribute = attributes;
31926       /* Combine the attributes.  */
31927       attributes = attr_chainon (prefix_attributes, attributes);
31928 
31929       decl = grokfield (declarator, &declspecs,
31930 			NULL_TREE, /*init_const_expr_p=*/false,
31931 			NULL_TREE, attributes);
31932 
31933       if (decl == error_mark_node || decl == NULL_TREE)
31934 	return error_mark_node;
31935 
31936       /* Reset PREFIX_ATTRIBUTES.  */
31937       if (attributes != error_mark_node)
31938 	{
31939 	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
31940 	    attributes = TREE_CHAIN (attributes);
31941 	  if (attributes)
31942 	    TREE_CHAIN (attributes) = NULL_TREE;
31943 	}
31944 
31945       DECL_CHAIN (decl) = decls;
31946       decls = decl;
31947 
31948       token = cp_lexer_peek_token (parser->lexer);
31949       if (token->type == CPP_COMMA)
31950 	{
31951 	  cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
31952 	  continue;
31953 	}
31954       else
31955 	break;
31956     }
31957   return decls;
31958 }
31959 
31960 /* Parse an Objective-C @property declaration.  The syntax is:
31961 
31962    objc-property-declaration:
31963      '@property' objc-property-attributes[opt] struct-declaration ;
31964 
31965    objc-property-attributes:
31966     '(' objc-property-attribute-list ')'
31967 
31968    objc-property-attribute-list:
31969      objc-property-attribute
31970      objc-property-attribute-list, objc-property-attribute
31971 
31972    objc-property-attribute
31973      'getter' = identifier
31974      'setter' = identifier
31975      'readonly'
31976      'readwrite'
31977      'assign'
31978      'retain'
31979      'copy'
31980      'nonatomic'
31981 
31982   For example:
31983     @property NSString *name;
31984     @property (readonly) id object;
31985     @property (retain, nonatomic, getter=getTheName) id name;
31986     @property int a, b, c;
31987 
31988    PS: This function is identical to
31989    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
31990 static void
cp_parser_objc_at_property_declaration(cp_parser * parser)31991 cp_parser_objc_at_property_declaration (cp_parser *parser)
31992 {
31993   /* The following variables hold the attributes of the properties as
31994      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
31995      seen.  When we see an attribute, we set them to 'true' (if they
31996      are boolean properties) or to the identifier (if they have an
31997      argument, ie, for getter and setter).  Note that here we only
31998      parse the list of attributes, check the syntax and accumulate the
31999      attributes that we find.  objc_add_property_declaration() will
32000      then process the information.  */
32001   bool property_assign = false;
32002   bool property_copy = false;
32003   tree property_getter_ident = NULL_TREE;
32004   bool property_nonatomic = false;
32005   bool property_readonly = false;
32006   bool property_readwrite = false;
32007   bool property_retain = false;
32008   tree property_setter_ident = NULL_TREE;
32009 
32010   /* 'properties' is the list of properties that we read.  Usually a
32011      single one, but maybe more (eg, in "@property int a, b, c;" there
32012      are three).  */
32013   tree properties;
32014   location_t loc;
32015 
32016   loc = cp_lexer_peek_token (parser->lexer)->location;
32017 
32018   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
32019 
32020   /* Parse the optional attribute list...  */
32021   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32022     {
32023       /* Eat the '('.  */
32024       matching_parens parens;
32025       parens.consume_open (parser);
32026 
32027       while (true)
32028 	{
32029 	  bool syntax_error = false;
32030 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
32031       	  enum rid keyword;
32032 
32033 	  if (token->type != CPP_NAME)
32034 	    {
32035 	      cp_parser_error (parser, "expected identifier");
32036 	      break;
32037 	    }
32038 	  keyword = C_RID_CODE (token->u.value);
32039 	  cp_lexer_consume_token (parser->lexer);
32040 	  switch (keyword)
32041 	    {
32042 	    case RID_ASSIGN:    property_assign = true;    break;
32043 	    case RID_COPY:      property_copy = true;      break;
32044 	    case RID_NONATOMIC: property_nonatomic = true; break;
32045 	    case RID_READONLY:  property_readonly = true;  break;
32046 	    case RID_READWRITE: property_readwrite = true; break;
32047 	    case RID_RETAIN:    property_retain = true;    break;
32048 
32049 	    case RID_GETTER:
32050 	    case RID_SETTER:
32051 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
32052 		{
32053 		  if (keyword == RID_GETTER)
32054 		    cp_parser_error (parser,
32055 				     "missing %<=%> (after %<getter%> attribute)");
32056 		  else
32057 		    cp_parser_error (parser,
32058 				     "missing %<=%> (after %<setter%> attribute)");
32059 		  syntax_error = true;
32060 		  break;
32061 		}
32062 	      cp_lexer_consume_token (parser->lexer); /* eat the = */
32063 	      if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
32064 		{
32065 		  cp_parser_error (parser, "expected identifier");
32066 		  syntax_error = true;
32067 		  break;
32068 		}
32069 	      if (keyword == RID_SETTER)
32070 		{
32071 		  if (property_setter_ident != NULL_TREE)
32072 		    {
32073 		      cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
32074 		      cp_lexer_consume_token (parser->lexer);
32075 		    }
32076 		  else
32077 		    property_setter_ident = cp_parser_objc_selector (parser);
32078 		  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
32079 		    cp_parser_error (parser, "setter name must terminate with %<:%>");
32080 		  else
32081 		    cp_lexer_consume_token (parser->lexer);
32082 		}
32083 	      else
32084 		{
32085 		  if (property_getter_ident != NULL_TREE)
32086 		    {
32087 		      cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
32088 		      cp_lexer_consume_token (parser->lexer);
32089 		    }
32090 		  else
32091 		    property_getter_ident = cp_parser_objc_selector (parser);
32092 		}
32093 	      break;
32094 	    default:
32095 	      cp_parser_error (parser, "unknown property attribute");
32096 	      syntax_error = true;
32097 	      break;
32098 	    }
32099 
32100 	  if (syntax_error)
32101 	    break;
32102 
32103 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32104 	    cp_lexer_consume_token (parser->lexer);
32105 	  else
32106 	    break;
32107 	}
32108 
32109       /* FIXME: "@property (setter, assign);" will generate a spurious
32110 	 "error: expected ‘)’ before ‘,’ token".  This is because
32111 	 cp_parser_require, unlike the C counterpart, will produce an
32112 	 error even if we are in error recovery.  */
32113       if (!parens.require_close (parser))
32114 	{
32115 	  cp_parser_skip_to_closing_parenthesis (parser,
32116 						 /*recovering=*/true,
32117 						 /*or_comma=*/false,
32118 						 /*consume_paren=*/true);
32119 	}
32120     }
32121 
32122   /* ... and the property declaration(s).  */
32123   properties = cp_parser_objc_struct_declaration (parser);
32124 
32125   if (properties == error_mark_node)
32126     {
32127       cp_parser_skip_to_end_of_statement (parser);
32128       /* If the next token is now a `;', consume it.  */
32129       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
32130 	cp_lexer_consume_token (parser->lexer);
32131       return;
32132     }
32133 
32134   if (properties == NULL_TREE)
32135     cp_parser_error (parser, "expected identifier");
32136   else
32137     {
32138       /* Comma-separated properties are chained together in
32139 	 reverse order; add them one by one.  */
32140       properties = nreverse (properties);
32141 
32142       for (; properties; properties = TREE_CHAIN (properties))
32143 	objc_add_property_declaration (loc, copy_node (properties),
32144 				       property_readonly, property_readwrite,
32145 				       property_assign, property_retain,
32146 				       property_copy, property_nonatomic,
32147 				       property_getter_ident, property_setter_ident);
32148     }
32149 
32150   cp_parser_consume_semicolon_at_end_of_statement (parser);
32151 }
32152 
32153 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
32154 
32155    objc-synthesize-declaration:
32156      @synthesize objc-synthesize-identifier-list ;
32157 
32158    objc-synthesize-identifier-list:
32159      objc-synthesize-identifier
32160      objc-synthesize-identifier-list, objc-synthesize-identifier
32161 
32162    objc-synthesize-identifier
32163      identifier
32164      identifier = identifier
32165 
32166   For example:
32167     @synthesize MyProperty;
32168     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32169 
32170   PS: This function is identical to c_parser_objc_at_synthesize_declaration
32171   for C.  Keep them in sync.
32172 */
32173 static void
cp_parser_objc_at_synthesize_declaration(cp_parser * parser)32174 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
32175 {
32176   tree list = NULL_TREE;
32177   location_t loc;
32178   loc = cp_lexer_peek_token (parser->lexer)->location;
32179 
32180   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
32181   while (true)
32182     {
32183       tree property, ivar;
32184       property = cp_parser_identifier (parser);
32185       if (property == error_mark_node)
32186 	{
32187 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
32188 	  return;
32189 	}
32190       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
32191 	{
32192 	  cp_lexer_consume_token (parser->lexer);
32193 	  ivar = cp_parser_identifier (parser);
32194 	  if (ivar == error_mark_node)
32195 	    {
32196 	      cp_parser_consume_semicolon_at_end_of_statement (parser);
32197 	      return;
32198 	    }
32199 	}
32200       else
32201 	ivar = NULL_TREE;
32202       list = chainon (list, build_tree_list (ivar, property));
32203       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32204 	cp_lexer_consume_token (parser->lexer);
32205       else
32206 	break;
32207     }
32208   cp_parser_consume_semicolon_at_end_of_statement (parser);
32209   objc_add_synthesize_declaration (loc, list);
32210 }
32211 
32212 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
32213 
32214    objc-dynamic-declaration:
32215      @dynamic identifier-list ;
32216 
32217    For example:
32218      @dynamic MyProperty;
32219      @dynamic MyProperty, AnotherProperty;
32220 
32221   PS: This function is identical to c_parser_objc_at_dynamic_declaration
32222   for C.  Keep them in sync.
32223 */
32224 static void
cp_parser_objc_at_dynamic_declaration(cp_parser * parser)32225 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32226 {
32227   tree list = NULL_TREE;
32228   location_t loc;
32229   loc = cp_lexer_peek_token (parser->lexer)->location;
32230 
32231   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
32232   while (true)
32233     {
32234       tree property;
32235       property = cp_parser_identifier (parser);
32236       if (property == error_mark_node)
32237 	{
32238 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
32239 	  return;
32240 	}
32241       list = chainon (list, build_tree_list (NULL, property));
32242       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32243 	cp_lexer_consume_token (parser->lexer);
32244       else
32245 	break;
32246     }
32247   cp_parser_consume_semicolon_at_end_of_statement (parser);
32248   objc_add_dynamic_declaration (loc, list);
32249 }
32250 
32251 
32252 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines.  */
32253 
32254 /* Returns name of the next clause.
32255    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32256    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
32257    returned and the token is consumed.  */
32258 
32259 static pragma_omp_clause
cp_parser_omp_clause_name(cp_parser * parser)32260 cp_parser_omp_clause_name (cp_parser *parser)
32261 {
32262   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32263 
32264   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32265     result = PRAGMA_OACC_CLAUSE_AUTO;
32266   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32267     result = PRAGMA_OMP_CLAUSE_IF;
32268   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32269     result = PRAGMA_OMP_CLAUSE_DEFAULT;
32270   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32271     result = PRAGMA_OACC_CLAUSE_DELETE;
32272   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32273     result = PRAGMA_OMP_CLAUSE_PRIVATE;
32274   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32275     result = PRAGMA_OMP_CLAUSE_FOR;
32276   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32277     {
32278       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32279       const char *p = IDENTIFIER_POINTER (id);
32280 
32281       switch (p[0])
32282 	{
32283 	case 'a':
32284 	  if (!strcmp ("aligned", p))
32285 	    result = PRAGMA_OMP_CLAUSE_ALIGNED;
32286 	  else if (!strcmp ("async", p))
32287 	    result = PRAGMA_OACC_CLAUSE_ASYNC;
32288 	  break;
32289 	case 'c':
32290 	  if (!strcmp ("collapse", p))
32291 	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32292 	  else if (!strcmp ("copy", p))
32293 	    result = PRAGMA_OACC_CLAUSE_COPY;
32294 	  else if (!strcmp ("copyin", p))
32295 	    result = PRAGMA_OMP_CLAUSE_COPYIN;
32296 	  else if (!strcmp ("copyout", p))
32297 	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
32298 	  else if (!strcmp ("copyprivate", p))
32299 	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32300 	  else if (!strcmp ("create", p))
32301 	    result = PRAGMA_OACC_CLAUSE_CREATE;
32302 	  break;
32303 	case 'd':
32304 	  if (!strcmp ("defaultmap", p))
32305 	    result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32306 	  else if (!strcmp ("depend", p))
32307 	    result = PRAGMA_OMP_CLAUSE_DEPEND;
32308 	  else if (!strcmp ("device", p))
32309 	    result = PRAGMA_OMP_CLAUSE_DEVICE;
32310 	  else if (!strcmp ("deviceptr", p))
32311 	    result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32312 	  else if (!strcmp ("device_resident", p))
32313 	    result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32314 	  else if (!strcmp ("dist_schedule", p))
32315 	    result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32316 	  break;
32317 	case 'f':
32318 	  if (!strcmp ("final", p))
32319 	    result = PRAGMA_OMP_CLAUSE_FINAL;
32320 	  else if (!strcmp ("finalize", p))
32321 	    result = PRAGMA_OACC_CLAUSE_FINALIZE;
32322 	  else if (!strcmp ("firstprivate", p))
32323 	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32324 	  else if (!strcmp ("from", p))
32325 	    result = PRAGMA_OMP_CLAUSE_FROM;
32326 	  break;
32327 	case 'g':
32328 	  if (!strcmp ("gang", p))
32329 	    result = PRAGMA_OACC_CLAUSE_GANG;
32330 	  else if (!strcmp ("grainsize", p))
32331 	    result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32332 	  break;
32333 	case 'h':
32334 	  if (!strcmp ("hint", p))
32335 	    result = PRAGMA_OMP_CLAUSE_HINT;
32336 	  else if (!strcmp ("host", p))
32337 	    result = PRAGMA_OACC_CLAUSE_HOST;
32338 	  break;
32339 	case 'i':
32340 	  if (!strcmp ("if_present", p))
32341 	    result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32342 	  else if (!strcmp ("in_reduction", p))
32343 	    result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32344 	  else if (!strcmp ("inbranch", p))
32345 	    result = PRAGMA_OMP_CLAUSE_INBRANCH;
32346 	  else if (!strcmp ("independent", p))
32347 	    result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32348 	  else if (!strcmp ("is_device_ptr", p))
32349 	    result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32350 	  break;
32351 	case 'l':
32352 	  if (!strcmp ("lastprivate", p))
32353 	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32354 	  else if (!strcmp ("linear", p))
32355 	    result = PRAGMA_OMP_CLAUSE_LINEAR;
32356 	  else if (!strcmp ("link", p))
32357 	    result = PRAGMA_OMP_CLAUSE_LINK;
32358 	  break;
32359 	case 'm':
32360 	  if (!strcmp ("map", p))
32361 	    result = PRAGMA_OMP_CLAUSE_MAP;
32362 	  else if (!strcmp ("mergeable", p))
32363 	    result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32364 	  break;
32365 	case 'n':
32366 	  if (!strcmp ("nogroup", p))
32367 	    result = PRAGMA_OMP_CLAUSE_NOGROUP;
32368 	  else if (!strcmp ("nontemporal", p))
32369 	    result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32370 	  else if (!strcmp ("notinbranch", p))
32371 	    result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32372 	  else if (!strcmp ("nowait", p))
32373 	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
32374 	  else if (!strcmp ("num_gangs", p))
32375 	    result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32376 	  else if (!strcmp ("num_tasks", p))
32377 	    result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32378 	  else if (!strcmp ("num_teams", p))
32379 	    result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32380 	  else if (!strcmp ("num_threads", p))
32381 	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32382 	  else if (!strcmp ("num_workers", p))
32383 	    result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32384 	  break;
32385 	case 'o':
32386 	  if (!strcmp ("ordered", p))
32387 	    result = PRAGMA_OMP_CLAUSE_ORDERED;
32388 	  break;
32389 	case 'p':
32390 	  if (!strcmp ("parallel", p))
32391 	    result = PRAGMA_OMP_CLAUSE_PARALLEL;
32392 	  else if (!strcmp ("present", p))
32393 	    result = PRAGMA_OACC_CLAUSE_PRESENT;
32394 	  else if (!strcmp ("present_or_copy", p)
32395 		   || !strcmp ("pcopy", p))
32396 	    result = PRAGMA_OACC_CLAUSE_COPY;
32397 	  else if (!strcmp ("present_or_copyin", p)
32398 		   || !strcmp ("pcopyin", p))
32399 	    result = PRAGMA_OACC_CLAUSE_COPYIN;
32400 	  else if (!strcmp ("present_or_copyout", p)
32401 		   || !strcmp ("pcopyout", p))
32402 	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
32403 	  else if (!strcmp ("present_or_create", p)
32404 		   || !strcmp ("pcreate", p))
32405 	    result = PRAGMA_OACC_CLAUSE_CREATE;
32406 	  else if (!strcmp ("priority", p))
32407 	    result = PRAGMA_OMP_CLAUSE_PRIORITY;
32408 	  else if (!strcmp ("proc_bind", p))
32409 	    result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32410 	  break;
32411 	case 'r':
32412 	  if (!strcmp ("reduction", p))
32413 	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
32414 	  break;
32415 	case 's':
32416 	  if (!strcmp ("safelen", p))
32417 	    result = PRAGMA_OMP_CLAUSE_SAFELEN;
32418 	  else if (!strcmp ("schedule", p))
32419 	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32420 	  else if (!strcmp ("sections", p))
32421 	    result = PRAGMA_OMP_CLAUSE_SECTIONS;
32422 	  else if (!strcmp ("self", p)) /* "self" is a synonym for "host".  */
32423 	    result = PRAGMA_OACC_CLAUSE_HOST;
32424 	  else if (!strcmp ("seq", p))
32425 	    result = PRAGMA_OACC_CLAUSE_SEQ;
32426 	  else if (!strcmp ("shared", p))
32427 	    result = PRAGMA_OMP_CLAUSE_SHARED;
32428 	  else if (!strcmp ("simd", p))
32429 	    result = PRAGMA_OMP_CLAUSE_SIMD;
32430 	  else if (!strcmp ("simdlen", p))
32431 	    result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32432 	  break;
32433 	case 't':
32434 	  if (!strcmp ("task_reduction", p))
32435 	    result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32436 	  else if (!strcmp ("taskgroup", p))
32437 	    result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32438 	  else if (!strcmp ("thread_limit", p))
32439 	    result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32440 	  else if (!strcmp ("threads", p))
32441 	    result = PRAGMA_OMP_CLAUSE_THREADS;
32442 	  else if (!strcmp ("tile", p))
32443 	    result = PRAGMA_OACC_CLAUSE_TILE;
32444 	  else if (!strcmp ("to", p))
32445 	    result = PRAGMA_OMP_CLAUSE_TO;
32446 	  break;
32447 	case 'u':
32448 	  if (!strcmp ("uniform", p))
32449 	    result = PRAGMA_OMP_CLAUSE_UNIFORM;
32450 	  else if (!strcmp ("untied", p))
32451 	    result = PRAGMA_OMP_CLAUSE_UNTIED;
32452 	  else if (!strcmp ("use_device", p))
32453 	    result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32454 	  else if (!strcmp ("use_device_ptr", p))
32455 	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32456 	  break;
32457 	case 'v':
32458 	  if (!strcmp ("vector", p))
32459 	    result = PRAGMA_OACC_CLAUSE_VECTOR;
32460 	  else if (!strcmp ("vector_length", p))
32461 	    result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32462 	  break;
32463 	case 'w':
32464 	  if (!strcmp ("wait", p))
32465 	    result = PRAGMA_OACC_CLAUSE_WAIT;
32466 	  else if (!strcmp ("worker", p))
32467 	    result = PRAGMA_OACC_CLAUSE_WORKER;
32468 	  break;
32469 	}
32470     }
32471 
32472   if (result != PRAGMA_OMP_CLAUSE_NONE)
32473     cp_lexer_consume_token (parser->lexer);
32474 
32475   return result;
32476 }
32477 
32478 /* Validate that a clause of the given type does not already exist.  */
32479 
32480 static void
check_no_duplicate_clause(tree clauses,enum omp_clause_code code,const char * name,location_t location)32481 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32482 			   const char *name, location_t location)
32483 {
32484   tree c;
32485 
32486   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32487     if (OMP_CLAUSE_CODE (c) == code)
32488       {
32489 	error_at (location, "too many %qs clauses", name);
32490 	break;
32491       }
32492 }
32493 
32494 /* OpenMP 2.5:
32495    variable-list:
32496      identifier
32497      variable-list , identifier
32498 
32499    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32500    colon).  An opening parenthesis will have been consumed by the caller.
32501 
32502    If KIND is nonzero, create the appropriate node and install the decl
32503    in OMP_CLAUSE_DECL and add the node to the head of the list.
32504 
32505    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32506    return the list created.
32507 
32508    COLON can be NULL if only closing parenthesis should end the list,
32509    or pointer to bool which will receive false if the list is terminated
32510    by closing parenthesis or true if the list is terminated by colon.  */
32511 
32512 static tree
cp_parser_omp_var_list_no_open(cp_parser * parser,enum omp_clause_code kind,tree list,bool * colon)32513 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32514 				tree list, bool *colon)
32515 {
32516   cp_token *token;
32517   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32518   if (colon)
32519     {
32520       parser->colon_corrects_to_scope_p = false;
32521       *colon = false;
32522     }
32523   while (1)
32524     {
32525       tree name, decl;
32526 
32527       if (kind == OMP_CLAUSE_DEPEND)
32528 	cp_parser_parse_tentatively (parser);
32529       token = cp_lexer_peek_token (parser->lexer);
32530       if (kind != 0
32531 	  && current_class_ptr
32532 	  && cp_parser_is_keyword (token, RID_THIS))
32533 	{
32534 	  decl = finish_this_expr ();
32535 	  if (TREE_CODE (decl) == NON_LVALUE_EXPR
32536 	      || CONVERT_EXPR_P (decl))
32537 	    decl = TREE_OPERAND (decl, 0);
32538 	  cp_lexer_consume_token (parser->lexer);
32539 	}
32540       else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
32541 	       || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
32542 	       || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
32543 	{
32544 	  cp_id_kind idk;
32545 	  decl = cp_parser_primary_expression (parser, false, false, false,
32546 					       &idk);
32547 	}
32548       else
32549 	{
32550 	  name = cp_parser_id_expression (parser, /*template_p=*/false,
32551 					  /*check_dependency_p=*/true,
32552 					  /*template_p=*/NULL,
32553 					  /*declarator_p=*/false,
32554 					  /*optional_p=*/false);
32555 	  if (name == error_mark_node)
32556 	    {
32557 	      if (kind == OMP_CLAUSE_DEPEND
32558 		  && cp_parser_simulate_error (parser))
32559 		goto depend_lvalue;
32560 	      goto skip_comma;
32561 	    }
32562 
32563 	  if (identifier_p (name))
32564 	    decl = cp_parser_lookup_name_simple (parser, name, token->location);
32565 	  else
32566 	    decl = name;
32567 	  if (decl == error_mark_node)
32568 	    {
32569 	      if (kind == OMP_CLAUSE_DEPEND
32570 		  && cp_parser_simulate_error (parser))
32571 		goto depend_lvalue;
32572 	      cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32573 					   token->location);
32574 	    }
32575 	}
32576       if (outer_automatic_var_p (decl))
32577 	decl = process_outer_var_ref (decl, tf_warning_or_error);
32578       if (decl == error_mark_node)
32579 	;
32580       else if (kind != 0)
32581 	{
32582 	  switch (kind)
32583 	    {
32584 	    case OMP_CLAUSE__CACHE_:
32585 	      /* The OpenACC cache directive explicitly only allows "array
32586 		 elements or subarrays".  */
32587 	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32588 		{
32589 		  error_at (token->location, "expected %<[%>");
32590 		  decl = error_mark_node;
32591 		  break;
32592 		}
32593 	      /* FALLTHROUGH.  */
32594 	    case OMP_CLAUSE_MAP:
32595 	    case OMP_CLAUSE_FROM:
32596 	    case OMP_CLAUSE_TO:
32597 	      while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32598 		{
32599 		  location_t loc
32600 		    = cp_lexer_peek_token (parser->lexer)->location;
32601 		  cp_id_kind idk = CP_ID_KIND_NONE;
32602 		  cp_lexer_consume_token (parser->lexer);
32603 		  decl = convert_from_reference (decl);
32604 		  decl
32605 		    = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32606 							      decl, false,
32607 							      &idk, loc);
32608 		}
32609 	      /* FALLTHROUGH.  */
32610 	    case OMP_CLAUSE_DEPEND:
32611 	    case OMP_CLAUSE_REDUCTION:
32612 	    case OMP_CLAUSE_IN_REDUCTION:
32613 	    case OMP_CLAUSE_TASK_REDUCTION:
32614 	      while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32615 		{
32616 		  tree low_bound = NULL_TREE, length = NULL_TREE;
32617 
32618 		  parser->colon_corrects_to_scope_p = false;
32619 		  cp_lexer_consume_token (parser->lexer);
32620 		  if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32621 		    {
32622 		      low_bound = cp_parser_expression (parser);
32623 		      /* Later handling is not prepared to see through these.  */
32624 		      gcc_checking_assert (!location_wrapper_p (low_bound));
32625 		    }
32626 		  if (!colon)
32627 		    parser->colon_corrects_to_scope_p
32628 		      = saved_colon_corrects_to_scope_p;
32629 		  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32630 		    length = integer_one_node;
32631 		  else
32632 		    {
32633 		      /* Look for `:'.  */
32634 		      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32635 			{
32636 			  if (kind == OMP_CLAUSE_DEPEND
32637 			      && cp_parser_simulate_error (parser))
32638 			    goto depend_lvalue;
32639 			  goto skip_comma;
32640 			}
32641 		      if (kind == OMP_CLAUSE_DEPEND)
32642 			cp_parser_commit_to_tentative_parse (parser);
32643 		      if (!cp_lexer_next_token_is (parser->lexer,
32644 						   CPP_CLOSE_SQUARE))
32645 			{
32646 			  length = cp_parser_expression (parser);
32647 			  /* Later handling is not prepared to see through these.  */
32648 			  gcc_checking_assert (!location_wrapper_p (length));
32649 			}
32650 		    }
32651 		  /* Look for the closing `]'.  */
32652 		  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32653 					  RT_CLOSE_SQUARE))
32654 		    {
32655 		      if (kind == OMP_CLAUSE_DEPEND
32656 			  && cp_parser_simulate_error (parser))
32657 			goto depend_lvalue;
32658 		      goto skip_comma;
32659 		    }
32660 
32661 		  decl = tree_cons (low_bound, length, decl);
32662 		}
32663 	      break;
32664 	    default:
32665 	      break;
32666 	    }
32667 
32668 	  if (kind == OMP_CLAUSE_DEPEND)
32669 	    {
32670 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32671 		  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32672 		  && cp_parser_simulate_error (parser))
32673 		{
32674 		depend_lvalue:
32675 		  cp_parser_abort_tentative_parse (parser);
32676 		  decl = cp_parser_assignment_expression (parser, NULL,
32677 							  false, false);
32678 		}
32679 	      else
32680 		cp_parser_parse_definitely (parser);
32681 	    }
32682 
32683 	  tree u = build_omp_clause (token->location, kind);
32684 	  OMP_CLAUSE_DECL (u) = decl;
32685 	  OMP_CLAUSE_CHAIN (u) = list;
32686 	  list = u;
32687 	}
32688       else
32689 	list = tree_cons (decl, NULL_TREE, list);
32690 
32691     get_comma:
32692       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32693 	break;
32694       cp_lexer_consume_token (parser->lexer);
32695     }
32696 
32697   if (colon)
32698     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32699 
32700   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32701     {
32702       *colon = true;
32703       cp_parser_require (parser, CPP_COLON, RT_COLON);
32704       return list;
32705     }
32706 
32707   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32708     {
32709       int ending;
32710 
32711       /* Try to resync to an unnested comma.  Copied from
32712 	 cp_parser_parenthesized_expression_list.  */
32713     skip_comma:
32714       if (colon)
32715 	parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32716       ending = cp_parser_skip_to_closing_parenthesis (parser,
32717 						      /*recovering=*/true,
32718 						      /*or_comma=*/true,
32719 						      /*consume_paren=*/true);
32720       if (ending < 0)
32721 	goto get_comma;
32722     }
32723 
32724   return list;
32725 }
32726 
32727 /* Similarly, but expect leading and trailing parenthesis.  This is a very
32728    common case for omp clauses.  */
32729 
32730 static tree
cp_parser_omp_var_list(cp_parser * parser,enum omp_clause_code kind,tree list)32731 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32732 {
32733   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32734     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32735   return list;
32736 }
32737 
32738 /* OpenACC 2.0:
32739    copy ( variable-list )
32740    copyin ( variable-list )
32741    copyout ( variable-list )
32742    create ( variable-list )
32743    delete ( variable-list )
32744    present ( variable-list ) */
32745 
32746 static tree
cp_parser_oacc_data_clause(cp_parser * parser,pragma_omp_clause c_kind,tree list)32747 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32748 			    tree list)
32749 {
32750   enum gomp_map_kind kind;
32751   switch (c_kind)
32752     {
32753     case PRAGMA_OACC_CLAUSE_COPY:
32754       kind = GOMP_MAP_TOFROM;
32755       break;
32756     case PRAGMA_OACC_CLAUSE_COPYIN:
32757       kind = GOMP_MAP_TO;
32758       break;
32759     case PRAGMA_OACC_CLAUSE_COPYOUT:
32760       kind = GOMP_MAP_FROM;
32761       break;
32762     case PRAGMA_OACC_CLAUSE_CREATE:
32763       kind = GOMP_MAP_ALLOC;
32764       break;
32765     case PRAGMA_OACC_CLAUSE_DELETE:
32766       kind = GOMP_MAP_RELEASE;
32767       break;
32768     case PRAGMA_OACC_CLAUSE_DEVICE:
32769       kind = GOMP_MAP_FORCE_TO;
32770       break;
32771     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32772       kind = GOMP_MAP_DEVICE_RESIDENT;
32773       break;
32774     case PRAGMA_OACC_CLAUSE_HOST:
32775       kind = GOMP_MAP_FORCE_FROM;
32776       break;
32777     case PRAGMA_OACC_CLAUSE_LINK:
32778       kind = GOMP_MAP_LINK;
32779       break;
32780     case PRAGMA_OACC_CLAUSE_PRESENT:
32781       kind = GOMP_MAP_FORCE_PRESENT;
32782       break;
32783     default:
32784       gcc_unreachable ();
32785     }
32786   tree nl, c;
32787   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32788 
32789   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32790     OMP_CLAUSE_SET_MAP_KIND (c, kind);
32791 
32792   return nl;
32793 }
32794 
32795 /* OpenACC 2.0:
32796    deviceptr ( variable-list ) */
32797 
32798 static tree
cp_parser_oacc_data_clause_deviceptr(cp_parser * parser,tree list)32799 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32800 {
32801   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32802   tree vars, t;
32803 
32804   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32805      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32806      variable-list must only allow for pointer variables.  */
32807   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32808   for (t = vars; t; t = TREE_CHAIN (t))
32809     {
32810       tree v = TREE_PURPOSE (t);
32811       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32812       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32813       OMP_CLAUSE_DECL (u) = v;
32814       OMP_CLAUSE_CHAIN (u) = list;
32815       list = u;
32816     }
32817 
32818   return list;
32819 }
32820 
32821 /* OpenACC 2.5:
32822    auto
32823    finalize
32824    independent
32825    nohost
32826    seq */
32827 
32828 static tree
cp_parser_oacc_simple_clause(location_t loc,enum omp_clause_code code,tree list)32829 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32830 			      tree list)
32831 {
32832   check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32833 
32834   tree c = build_omp_clause (loc, code);
32835   OMP_CLAUSE_CHAIN (c) = list;
32836 
32837   return c;
32838 }
32839 
32840  /* OpenACC:
32841    num_gangs ( expression )
32842    num_workers ( expression )
32843    vector_length ( expression )  */
32844 
32845 static tree
cp_parser_oacc_single_int_clause(cp_parser * parser,omp_clause_code code,const char * str,tree list)32846 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32847 				  const char *str, tree list)
32848 {
32849   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32850 
32851   matching_parens parens;
32852   if (!parens.require_open (parser))
32853     return list;
32854 
32855   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32856 
32857   if (t == error_mark_node
32858       || !parens.require_close (parser))
32859     {
32860       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32861 					     /*or_comma=*/false,
32862 					     /*consume_paren=*/true);
32863       return list;
32864     }
32865 
32866   check_no_duplicate_clause (list, code, str, loc);
32867 
32868   tree c = build_omp_clause (loc, code);
32869   OMP_CLAUSE_OPERAND (c, 0) = t;
32870   OMP_CLAUSE_CHAIN (c) = list;
32871   return c;
32872 }
32873 
32874 /* OpenACC:
32875 
32876     gang [( gang-arg-list )]
32877     worker [( [num:] int-expr )]
32878     vector [( [length:] int-expr )]
32879 
32880   where gang-arg is one of:
32881 
32882     [num:] int-expr
32883     static: size-expr
32884 
32885   and size-expr may be:
32886 
32887     *
32888     int-expr
32889 */
32890 
32891 static tree
cp_parser_oacc_shape_clause(cp_parser * parser,location_t loc,omp_clause_code kind,const char * str,tree list)32892 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
32893 			     omp_clause_code kind,
32894 			     const char *str, tree list)
32895 {
32896   const char *id = "num";
32897   cp_lexer *lexer = parser->lexer;
32898   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32899 
32900   if (kind == OMP_CLAUSE_VECTOR)
32901     id = "length";
32902 
32903   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32904     {
32905       matching_parens parens;
32906       parens.consume_open (parser);
32907 
32908       do
32909 	{
32910 	  cp_token *next = cp_lexer_peek_token (lexer);
32911 	  int idx = 0;
32912 
32913 	  /* Gang static argument.  */
32914 	  if (kind == OMP_CLAUSE_GANG
32915 	      && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32916 	    {
32917 	      cp_lexer_consume_token (lexer);
32918 
32919 	      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32920 		goto cleanup_error;
32921 
32922 	      idx = 1;
32923 	      if (ops[idx] != NULL)
32924 		{
32925 		  cp_parser_error (parser, "too many %<static%> arguments");
32926 		  goto cleanup_error;
32927 		}
32928 
32929 	      /* Check for the '*' argument.  */
32930 	      if (cp_lexer_next_token_is (lexer, CPP_MULT)
32931 		  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32932 		      || cp_lexer_nth_token_is (parser->lexer, 2,
32933 						CPP_CLOSE_PAREN)))
32934 		{
32935 		  cp_lexer_consume_token (lexer);
32936 		  ops[idx] = integer_minus_one_node;
32937 
32938 		  if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32939 		    {
32940 		      cp_lexer_consume_token (lexer);
32941 		      continue;
32942 		    }
32943 		  else break;
32944 		}
32945 	    }
32946 	  /* Worker num: argument and vector length: arguments.  */
32947 	  else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32948 		   && id_equal (next->u.value, id)
32949 		   && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32950 	    {
32951 	      cp_lexer_consume_token (lexer);  /* id  */
32952 	      cp_lexer_consume_token (lexer);  /* ':'  */
32953 	    }
32954 
32955 	  /* Now collect the actual argument.  */
32956 	  if (ops[idx] != NULL_TREE)
32957 	    {
32958 	      cp_parser_error (parser, "unexpected argument");
32959 	      goto cleanup_error;
32960 	    }
32961 
32962 	  tree expr = cp_parser_assignment_expression (parser, NULL, false,
32963 						       false);
32964 	  if (expr == error_mark_node)
32965 	    goto cleanup_error;
32966 
32967 	  mark_exp_read (expr);
32968 	  ops[idx] = expr;
32969 
32970 	  if (kind == OMP_CLAUSE_GANG
32971 	      && cp_lexer_next_token_is (lexer, CPP_COMMA))
32972 	    {
32973 	      cp_lexer_consume_token (lexer);
32974 	      continue;
32975 	    }
32976 	  break;
32977 	}
32978       while (1);
32979 
32980       if (!parens.require_close (parser))
32981 	goto cleanup_error;
32982     }
32983 
32984   check_no_duplicate_clause (list, kind, str, loc);
32985 
32986   c = build_omp_clause (loc, kind);
32987 
32988   if (ops[1])
32989     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32990 
32991   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32992   OMP_CLAUSE_CHAIN (c) = list;
32993 
32994   return c;
32995 
32996  cleanup_error:
32997   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32998   return list;
32999 }
33000 
33001 /* OpenACC 2.0:
33002    tile ( size-expr-list ) */
33003 
33004 static tree
cp_parser_oacc_clause_tile(cp_parser * parser,location_t clause_loc,tree list)33005 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
33006 {
33007   tree c, expr = error_mark_node;
33008   tree tile = NULL_TREE;
33009 
33010   /* Collapse and tile are mutually exclusive.  (The spec doesn't say
33011      so, but the spec authors never considered such a case and have
33012      differing opinions on what it might mean, including 'not
33013      allowed'.)  */
33014   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
33015   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
33016 			     clause_loc);
33017 
33018   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33019     return list;
33020 
33021   do
33022     {
33023       if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
33024 	return list;
33025 
33026       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
33027 	  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
33028 	      || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
33029 	{
33030 	  cp_lexer_consume_token (parser->lexer);
33031 	  expr = integer_zero_node;
33032 	}
33033       else
33034 	expr = cp_parser_constant_expression (parser);
33035 
33036       tile = tree_cons (NULL_TREE, expr, tile);
33037     }
33038   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
33039 
33040   /* Consume the trailing ')'.  */
33041   cp_lexer_consume_token (parser->lexer);
33042 
33043   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
33044   tile = nreverse (tile);
33045   OMP_CLAUSE_TILE_LIST (c) = tile;
33046   OMP_CLAUSE_CHAIN (c) = list;
33047   return c;
33048 }
33049 
33050 /* OpenACC 2.0
33051    Parse wait clause or directive parameters.  */
33052 
33053 static tree
cp_parser_oacc_wait_list(cp_parser * parser,location_t clause_loc,tree list)33054 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
33055 {
33056   vec<tree, va_gc> *args;
33057   tree t, args_tree;
33058 
33059   args = cp_parser_parenthesized_expression_list (parser, non_attr,
33060 						  /*cast_p=*/false,
33061 						  /*allow_expansion_p=*/true,
33062 						  /*non_constant_p=*/NULL);
33063 
33064   if (args == NULL || args->length () == 0)
33065     {
33066       if (args != NULL)
33067 	{
33068 	  cp_parser_error (parser, "expected integer expression list");
33069 	  release_tree_vector (args);
33070 	}
33071       return list;
33072     }
33073 
33074   args_tree = build_tree_list_vec (args);
33075 
33076   release_tree_vector (args);
33077 
33078   for (t = args_tree; t; t = TREE_CHAIN (t))
33079     {
33080       tree targ = TREE_VALUE (t);
33081 
33082       if (targ != error_mark_node)
33083 	{
33084 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
33085 	    error ("%<wait%> expression must be integral");
33086 	  else
33087 	    {
33088 	      tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
33089 
33090 	      targ = mark_rvalue_use (targ);
33091 	      OMP_CLAUSE_DECL (c) = targ;
33092 	      OMP_CLAUSE_CHAIN (c) = list;
33093 	      list = c;
33094 	    }
33095 	}
33096     }
33097 
33098   return list;
33099 }
33100 
33101 /* OpenACC:
33102    wait [( int-expr-list )] */
33103 
33104 static tree
cp_parser_oacc_clause_wait(cp_parser * parser,tree list)33105 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
33106 {
33107   location_t location = cp_lexer_peek_token (parser->lexer)->location;
33108 
33109   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33110     list = cp_parser_oacc_wait_list (parser, location, list);
33111   else
33112     {
33113       tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
33114 
33115       OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33116       OMP_CLAUSE_CHAIN (c) = list;
33117       list = c;
33118     }
33119 
33120   return list;
33121 }
33122 
33123 /* OpenMP 3.0:
33124    collapse ( constant-expression ) */
33125 
33126 static tree
cp_parser_omp_clause_collapse(cp_parser * parser,tree list,location_t location)33127 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
33128 {
33129   tree c, num;
33130   location_t loc;
33131   HOST_WIDE_INT n;
33132 
33133   loc = cp_lexer_peek_token (parser->lexer)->location;
33134   matching_parens parens;
33135   if (!parens.require_open (parser))
33136     return list;
33137 
33138   num = cp_parser_constant_expression (parser);
33139 
33140   if (!parens.require_close (parser))
33141     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33142 					   /*or_comma=*/false,
33143 					   /*consume_paren=*/true);
33144 
33145   if (num == error_mark_node)
33146     return list;
33147   num = fold_non_dependent_expr (num);
33148   if (!tree_fits_shwi_p (num)
33149       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33150       || (n = tree_to_shwi (num)) <= 0
33151       || (int) n != n)
33152     {
33153       error_at (loc, "collapse argument needs positive constant integer expression");
33154       return list;
33155     }
33156 
33157   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
33158   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
33159   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
33160   OMP_CLAUSE_CHAIN (c) = list;
33161   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
33162 
33163   return c;
33164 }
33165 
33166 /* OpenMP 2.5:
33167    default ( none | shared )
33168 
33169    OpenACC:
33170    default ( none | present ) */
33171 
33172 static tree
cp_parser_omp_clause_default(cp_parser * parser,tree list,location_t location,bool is_oacc)33173 cp_parser_omp_clause_default (cp_parser *parser, tree list,
33174 			      location_t location, bool is_oacc)
33175 {
33176   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
33177   tree c;
33178 
33179   matching_parens parens;
33180   if (!parens.require_open (parser))
33181     return list;
33182   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33183     {
33184       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33185       const char *p = IDENTIFIER_POINTER (id);
33186 
33187       switch (p[0])
33188 	{
33189 	case 'n':
33190 	  if (strcmp ("none", p) != 0)
33191 	    goto invalid_kind;
33192 	  kind = OMP_CLAUSE_DEFAULT_NONE;
33193 	  break;
33194 
33195 	case 'p':
33196 	  if (strcmp ("present", p) != 0 || !is_oacc)
33197 	    goto invalid_kind;
33198 	  kind = OMP_CLAUSE_DEFAULT_PRESENT;
33199 	  break;
33200 
33201 	case 's':
33202 	  if (strcmp ("shared", p) != 0 || is_oacc)
33203 	    goto invalid_kind;
33204 	  kind = OMP_CLAUSE_DEFAULT_SHARED;
33205 	  break;
33206 
33207 	default:
33208 	  goto invalid_kind;
33209 	}
33210 
33211       cp_lexer_consume_token (parser->lexer);
33212     }
33213   else
33214     {
33215     invalid_kind:
33216       if (is_oacc)
33217 	cp_parser_error (parser, "expected %<none%> or %<present%>");
33218       else
33219 	cp_parser_error (parser, "expected %<none%> or %<shared%>");
33220     }
33221 
33222   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
33223       || !parens.require_close (parser))
33224     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33225 					   /*or_comma=*/false,
33226 					   /*consume_paren=*/true);
33227 
33228   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
33229     return list;
33230 
33231   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33232   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33233   OMP_CLAUSE_CHAIN (c) = list;
33234   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33235 
33236   return c;
33237 }
33238 
33239 /* OpenMP 3.1:
33240    final ( expression ) */
33241 
33242 static tree
cp_parser_omp_clause_final(cp_parser * parser,tree list,location_t location)33243 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33244 {
33245   tree t, c;
33246 
33247   matching_parens parens;
33248   if (!parens.require_open (parser))
33249     return list;
33250 
33251   t = cp_parser_assignment_expression (parser);
33252 
33253   if (t == error_mark_node
33254       || !parens.require_close (parser))
33255     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33256 					   /*or_comma=*/false,
33257 					   /*consume_paren=*/true);
33258 
33259   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33260 
33261   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33262   OMP_CLAUSE_FINAL_EXPR (c) = t;
33263   OMP_CLAUSE_CHAIN (c) = list;
33264 
33265   return c;
33266 }
33267 
33268 /* OpenMP 2.5:
33269    if ( expression )
33270 
33271    OpenMP 4.5:
33272    if ( directive-name-modifier : expression )
33273 
33274    directive-name-modifier:
33275      parallel | task | taskloop | target data | target | target update
33276      | target enter data | target exit data
33277 
33278    OpenMP 5.0:
33279    directive-name-modifier:
33280      ... | simd | cancel  */
33281 
33282 static tree
cp_parser_omp_clause_if(cp_parser * parser,tree list,location_t location,bool is_omp)33283 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33284 			 bool is_omp)
33285 {
33286   tree t, c;
33287   enum tree_code if_modifier = ERROR_MARK;
33288 
33289   matching_parens parens;
33290   if (!parens.require_open (parser))
33291     return list;
33292 
33293   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33294     {
33295       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33296       const char *p = IDENTIFIER_POINTER (id);
33297       int n = 2;
33298 
33299       if (strcmp ("cancel", p) == 0)
33300 	if_modifier = VOID_CST;
33301       else if (strcmp ("parallel", p) == 0)
33302 	if_modifier = OMP_PARALLEL;
33303       else if (strcmp ("simd", p) == 0)
33304 	if_modifier = OMP_SIMD;
33305       else if (strcmp ("task", p) == 0)
33306 	if_modifier = OMP_TASK;
33307       else if (strcmp ("taskloop", p) == 0)
33308 	if_modifier = OMP_TASKLOOP;
33309       else if (strcmp ("target", p) == 0)
33310 	{
33311 	  if_modifier = OMP_TARGET;
33312 	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33313 	    {
33314 	      id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33315 	      p = IDENTIFIER_POINTER (id);
33316 	      if (strcmp ("data", p) == 0)
33317 		if_modifier = OMP_TARGET_DATA;
33318 	      else if (strcmp ("update", p) == 0)
33319 		if_modifier = OMP_TARGET_UPDATE;
33320 	      else if (strcmp ("enter", p) == 0)
33321 		if_modifier = OMP_TARGET_ENTER_DATA;
33322 	      else if (strcmp ("exit", p) == 0)
33323 		if_modifier = OMP_TARGET_EXIT_DATA;
33324 	      if (if_modifier != OMP_TARGET)
33325 		n = 3;
33326 	      else
33327 		{
33328 		  location_t loc
33329 		    = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33330 		  error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33331 				 "or %<exit%>");
33332 		  if_modifier = ERROR_MARK;
33333 		}
33334 	      if (if_modifier == OMP_TARGET_ENTER_DATA
33335 		  || if_modifier == OMP_TARGET_EXIT_DATA)
33336 		{
33337 		  if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33338 		    {
33339 		      id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33340 		      p = IDENTIFIER_POINTER (id);
33341 		      if (strcmp ("data", p) == 0)
33342 			n = 4;
33343 		    }
33344 		  if (n != 4)
33345 		    {
33346 		      location_t loc
33347 			= cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33348 		      error_at (loc, "expected %<data%>");
33349 		      if_modifier = ERROR_MARK;
33350 		    }
33351 		}
33352 	    }
33353 	}
33354       if (if_modifier != ERROR_MARK)
33355 	{
33356 	  if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33357 	    {
33358 	      while (n-- > 0)
33359 		cp_lexer_consume_token (parser->lexer);
33360 	    }
33361 	  else
33362 	    {
33363 	      if (n > 2)
33364 		{
33365 		  location_t loc
33366 		    = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33367 		  error_at (loc, "expected %<:%>");
33368 		}
33369 	      if_modifier = ERROR_MARK;
33370 	    }
33371 	}
33372     }
33373 
33374   t = cp_parser_assignment_expression (parser);
33375 
33376   if (t == error_mark_node
33377       || !parens.require_close (parser))
33378     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33379 					   /*or_comma=*/false,
33380 					   /*consume_paren=*/true);
33381 
33382   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33383     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33384       {
33385 	if (if_modifier != ERROR_MARK
33386 	    && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33387 	  {
33388 	    const char *p = NULL;
33389 	    switch (if_modifier)
33390 	      {
33391 	      case VOID_CST: p = "cancel"; break;
33392 	      case OMP_PARALLEL: p = "parallel"; break;
33393 	      case OMP_SIMD: p = "simd"; break;
33394 	      case OMP_TASK: p = "task"; break;
33395 	      case OMP_TASKLOOP: p = "taskloop"; break;
33396 	      case OMP_TARGET_DATA: p = "target data"; break;
33397 	      case OMP_TARGET: p = "target"; break;
33398 	      case OMP_TARGET_UPDATE: p = "target update"; break;
33399 	      case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33400 	      case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33401 	      default: gcc_unreachable ();
33402 	      }
33403 	    error_at (location, "too many %<if%> clauses with %qs modifier",
33404 		      p);
33405 	    return list;
33406 	  }
33407 	else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33408 	  {
33409 	    if (!is_omp)
33410 	      error_at (location, "too many %<if%> clauses");
33411 	    else
33412 	      error_at (location, "too many %<if%> clauses without modifier");
33413 	    return list;
33414 	  }
33415 	else if (if_modifier == ERROR_MARK
33416 		 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33417 	  {
33418 	    error_at (location, "if any %<if%> clause has modifier, then all "
33419 				"%<if%> clauses have to use modifier");
33420 	    return list;
33421 	  }
33422       }
33423 
33424   c = build_omp_clause (location, OMP_CLAUSE_IF);
33425   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33426   OMP_CLAUSE_IF_EXPR (c) = t;
33427   OMP_CLAUSE_CHAIN (c) = list;
33428 
33429   return c;
33430 }
33431 
33432 /* OpenMP 3.1:
33433    mergeable */
33434 
33435 static tree
cp_parser_omp_clause_mergeable(cp_parser *,tree list,location_t location)33436 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33437 				tree list, location_t location)
33438 {
33439   tree c;
33440 
33441   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33442 			     location);
33443 
33444   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33445   OMP_CLAUSE_CHAIN (c) = list;
33446   return c;
33447 }
33448 
33449 /* OpenMP 2.5:
33450    nowait */
33451 
33452 static tree
cp_parser_omp_clause_nowait(cp_parser *,tree list,location_t location)33453 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33454 			     tree list, location_t location)
33455 {
33456   tree c;
33457 
33458   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33459 
33460   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33461   OMP_CLAUSE_CHAIN (c) = list;
33462   return c;
33463 }
33464 
33465 /* OpenMP 2.5:
33466    num_threads ( expression ) */
33467 
33468 static tree
cp_parser_omp_clause_num_threads(cp_parser * parser,tree list,location_t location)33469 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33470 				  location_t location)
33471 {
33472   tree t, c;
33473 
33474   matching_parens parens;
33475   if (!parens.require_open (parser))
33476     return list;
33477 
33478   t = cp_parser_assignment_expression (parser);
33479 
33480   if (t == error_mark_node
33481       || !parens.require_close (parser))
33482     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33483 					   /*or_comma=*/false,
33484 					   /*consume_paren=*/true);
33485 
33486   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33487 			     "num_threads", location);
33488 
33489   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33490   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33491   OMP_CLAUSE_CHAIN (c) = list;
33492 
33493   return c;
33494 }
33495 
33496 /* OpenMP 4.5:
33497    num_tasks ( expression ) */
33498 
33499 static tree
cp_parser_omp_clause_num_tasks(cp_parser * parser,tree list,location_t location)33500 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33501 				location_t location)
33502 {
33503   tree t, c;
33504 
33505   matching_parens parens;
33506   if (!parens.require_open (parser))
33507     return list;
33508 
33509   t = cp_parser_assignment_expression (parser);
33510 
33511   if (t == error_mark_node
33512       || !parens.require_close (parser))
33513     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33514 					   /*or_comma=*/false,
33515 					   /*consume_paren=*/true);
33516 
33517   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33518 			     "num_tasks", location);
33519 
33520   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33521   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33522   OMP_CLAUSE_CHAIN (c) = list;
33523 
33524   return c;
33525 }
33526 
33527 /* OpenMP 4.5:
33528    grainsize ( expression ) */
33529 
33530 static tree
cp_parser_omp_clause_grainsize(cp_parser * parser,tree list,location_t location)33531 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33532 				location_t location)
33533 {
33534   tree t, c;
33535 
33536   matching_parens parens;
33537   if (!parens.require_open (parser))
33538     return list;
33539 
33540   t = cp_parser_assignment_expression (parser);
33541 
33542   if (t == error_mark_node
33543       || !parens.require_close (parser))
33544     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33545 					   /*or_comma=*/false,
33546 					   /*consume_paren=*/true);
33547 
33548   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33549 			     "grainsize", location);
33550 
33551   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33552   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33553   OMP_CLAUSE_CHAIN (c) = list;
33554 
33555   return c;
33556 }
33557 
33558 /* OpenMP 4.5:
33559    priority ( expression ) */
33560 
33561 static tree
cp_parser_omp_clause_priority(cp_parser * parser,tree list,location_t location)33562 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33563 			       location_t location)
33564 {
33565   tree t, c;
33566 
33567   matching_parens parens;
33568   if (!parens.require_open (parser))
33569     return list;
33570 
33571   t = cp_parser_assignment_expression (parser);
33572 
33573   if (t == error_mark_node
33574       || !parens.require_close (parser))
33575     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33576 					   /*or_comma=*/false,
33577 					   /*consume_paren=*/true);
33578 
33579   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33580 			     "priority", location);
33581 
33582   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33583   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33584   OMP_CLAUSE_CHAIN (c) = list;
33585 
33586   return c;
33587 }
33588 
33589 /* OpenMP 4.5:
33590    hint ( expression ) */
33591 
33592 static tree
cp_parser_omp_clause_hint(cp_parser * parser,tree list,location_t location)33593 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33594 {
33595   tree t, c;
33596 
33597   matching_parens parens;
33598   if (!parens.require_open (parser))
33599     return list;
33600 
33601   t = cp_parser_assignment_expression (parser);
33602 
33603   if (t == error_mark_node
33604       || !parens.require_close (parser))
33605     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33606 					   /*or_comma=*/false,
33607 					   /*consume_paren=*/true);
33608 
33609   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33610 
33611   c = build_omp_clause (location, OMP_CLAUSE_HINT);
33612   OMP_CLAUSE_HINT_EXPR (c) = t;
33613   OMP_CLAUSE_CHAIN (c) = list;
33614 
33615   return c;
33616 }
33617 
33618 /* OpenMP 4.5:
33619    defaultmap ( tofrom : scalar )
33620 
33621    OpenMP 5.0:
33622    defaultmap ( implicit-behavior [ : variable-category ] ) */
33623 
33624 static tree
cp_parser_omp_clause_defaultmap(cp_parser * parser,tree list,location_t location)33625 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33626 				 location_t location)
33627 {
33628   tree c, id;
33629   const char *p;
33630   enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33631   enum omp_clause_defaultmap_kind category
33632     = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33633 
33634   matching_parens parens;
33635   if (!parens.require_open (parser))
33636     return list;
33637 
33638   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33639     p = "default";
33640   else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33641     {
33642     invalid_behavior:
33643       cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33644 			       "%<tofrom%>, %<firstprivate%>, %<none%> "
33645 			       "or %<default%>");
33646       goto out_err;
33647     }
33648   else
33649     {
33650       id = cp_lexer_peek_token (parser->lexer)->u.value;
33651       p = IDENTIFIER_POINTER (id);
33652     }
33653 
33654   switch (p[0])
33655     {
33656     case 'a':
33657       if (strcmp ("alloc", p) == 0)
33658 	behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33659       else
33660 	goto invalid_behavior;
33661       break;
33662 
33663     case 'd':
33664       if (strcmp ("default", p) == 0)
33665 	behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33666       else
33667 	goto invalid_behavior;
33668       break;
33669 
33670     case 'f':
33671       if (strcmp ("firstprivate", p) == 0)
33672 	behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33673       else if (strcmp ("from", p) == 0)
33674 	behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33675       else
33676 	goto invalid_behavior;
33677       break;
33678 
33679     case 'n':
33680       if (strcmp ("none", p) == 0)
33681 	behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33682       else
33683 	goto invalid_behavior;
33684       break;
33685 
33686     case 't':
33687       if (strcmp ("tofrom", p) == 0)
33688 	behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33689       else if (strcmp ("to", p) == 0)
33690 	behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33691       else
33692 	goto invalid_behavior;
33693       break;
33694 
33695     default:
33696       goto invalid_behavior;
33697     }
33698   cp_lexer_consume_token (parser->lexer);
33699 
33700   if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33701     {
33702       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33703 	goto out_err;
33704 
33705       if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33706 	{
33707 	invalid_category:
33708 	  cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33709 				   "%<pointer%>");
33710 	  goto out_err;
33711 	}
33712       id = cp_lexer_peek_token (parser->lexer)->u.value;
33713       p = IDENTIFIER_POINTER (id);
33714 
33715       switch (p[0])
33716 	{
33717 	case 'a':
33718 	  if (strcmp ("aggregate", p) == 0)
33719 	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33720 	  else
33721 	    goto invalid_category;
33722 	  break;
33723 
33724 	case 'p':
33725 	  if (strcmp ("pointer", p) == 0)
33726 	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33727 	  else
33728 	    goto invalid_category;
33729 	  break;
33730 
33731 	case 's':
33732 	  if (strcmp ("scalar", p) == 0)
33733 	    category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33734 	  else
33735 	    goto invalid_category;
33736 	  break;
33737 
33738 	default:
33739 	  goto invalid_category;
33740 	}
33741 
33742       cp_lexer_consume_token (parser->lexer);
33743     }
33744   if (!parens.require_close (parser))
33745     goto out_err;
33746 
33747   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33748     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33749 	&& (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33750 	    || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33751 	    || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33752 		== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33753       {
33754 	enum omp_clause_defaultmap_kind cat = category;
33755 	location_t loc = OMP_CLAUSE_LOCATION (c);
33756 	if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33757 	  cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33758 	p = NULL;
33759 	switch (cat)
33760 	  {
33761 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33762 	    p = NULL;
33763 	    break;
33764 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33765 	    p = "aggregate";
33766 	    break;
33767 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33768 	    p = "pointer";
33769 	    break;
33770 	  case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33771 	    p = "scalar";
33772 	    break;
33773 	  default:
33774 	    gcc_unreachable ();
33775 	  }
33776 	if (p)
33777 	  error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33778 		    p);
33779 	else
33780 	  error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33781 			 "category");
33782 	break;
33783       }
33784 
33785   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33786   OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33787   OMP_CLAUSE_CHAIN (c) = list;
33788   return c;
33789 
33790  out_err:
33791   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33792 					 /*or_comma=*/false,
33793 					 /*consume_paren=*/true);
33794   return list;
33795 }
33796 
33797 /* OpenMP 2.5:
33798    ordered
33799 
33800    OpenMP 4.5:
33801    ordered ( constant-expression ) */
33802 
33803 static tree
cp_parser_omp_clause_ordered(cp_parser * parser,tree list,location_t location)33804 cp_parser_omp_clause_ordered (cp_parser *parser,
33805 			      tree list, location_t location)
33806 {
33807   tree c, num = NULL_TREE;
33808   HOST_WIDE_INT n;
33809 
33810   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33811 			     "ordered", location);
33812 
33813   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33814     {
33815       matching_parens parens;
33816       parens.consume_open (parser);
33817 
33818       num = cp_parser_constant_expression (parser);
33819 
33820       if (!parens.require_close (parser))
33821 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33822 					       /*or_comma=*/false,
33823 					       /*consume_paren=*/true);
33824 
33825       if (num == error_mark_node)
33826 	return list;
33827       num = fold_non_dependent_expr (num);
33828       if (!tree_fits_shwi_p (num)
33829 	  || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33830 	  || (n = tree_to_shwi (num)) <= 0
33831 	  || (int) n != n)
33832 	{
33833 	  error_at (location,
33834 		    "ordered argument needs positive constant integer "
33835 		    "expression");
33836 	  return list;
33837 	}
33838     }
33839 
33840   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33841   OMP_CLAUSE_ORDERED_EXPR (c) = num;
33842   OMP_CLAUSE_CHAIN (c) = list;
33843   return c;
33844 }
33845 
33846 /* OpenMP 2.5:
33847    reduction ( reduction-operator : variable-list )
33848 
33849    reduction-operator:
33850      One of: + * - & ^ | && ||
33851 
33852    OpenMP 3.1:
33853 
33854    reduction-operator:
33855      One of: + * - & ^ | && || min max
33856 
33857    OpenMP 4.0:
33858 
33859    reduction-operator:
33860      One of: + * - & ^ | && ||
33861      id-expression
33862 
33863    OpenMP 5.0:
33864    reduction ( reduction-modifier, reduction-operator : variable-list )
33865    in_reduction ( reduction-operator : variable-list )
33866    task_reduction ( reduction-operator : variable-list )  */
33867 
33868 static tree
cp_parser_omp_clause_reduction(cp_parser * parser,enum omp_clause_code kind,bool is_omp,tree list)33869 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33870 				bool is_omp, tree list)
33871 {
33872   enum tree_code code = ERROR_MARK;
33873   tree nlist, c, id = NULL_TREE;
33874   bool task = false;
33875   bool inscan = false;
33876 
33877   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33878     return list;
33879 
33880   if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33881     {
33882       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33883 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33884 	{
33885 	  cp_lexer_consume_token (parser->lexer);
33886 	  cp_lexer_consume_token (parser->lexer);
33887 	}
33888       else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33889 	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33890 	{
33891 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33892 	  const char *p = IDENTIFIER_POINTER (id);
33893 	  if (strcmp (p, "task") == 0)
33894 	    task = true;
33895 	  else if (strcmp (p, "inscan") == 0)
33896 	    {
33897 	      inscan = true;
33898 	      sorry ("%<inscan%> modifier on %<reduction%> clause "
33899 		     "not supported yet");
33900 	    }
33901 	  if (task || inscan)
33902 	    {
33903 	      cp_lexer_consume_token (parser->lexer);
33904 	      cp_lexer_consume_token (parser->lexer);
33905 	    }
33906 	}
33907     }
33908 
33909   switch (cp_lexer_peek_token (parser->lexer)->type)
33910     {
33911     case CPP_PLUS: code = PLUS_EXPR; break;
33912     case CPP_MULT: code = MULT_EXPR; break;
33913     case CPP_MINUS: code = MINUS_EXPR; break;
33914     case CPP_AND: code = BIT_AND_EXPR; break;
33915     case CPP_XOR: code = BIT_XOR_EXPR; break;
33916     case CPP_OR: code = BIT_IOR_EXPR; break;
33917     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33918     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33919     default: break;
33920     }
33921 
33922   if (code != ERROR_MARK)
33923     cp_lexer_consume_token (parser->lexer);
33924   else
33925     {
33926       bool saved_colon_corrects_to_scope_p;
33927       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33928       parser->colon_corrects_to_scope_p = false;
33929       id = cp_parser_id_expression (parser, /*template_p=*/false,
33930 				    /*check_dependency_p=*/true,
33931 				    /*template_p=*/NULL,
33932 				    /*declarator_p=*/false,
33933 				    /*optional_p=*/false);
33934       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33935       if (identifier_p (id))
33936 	{
33937 	  const char *p = IDENTIFIER_POINTER (id);
33938 
33939 	  if (strcmp (p, "min") == 0)
33940 	    code = MIN_EXPR;
33941 	  else if (strcmp (p, "max") == 0)
33942 	    code = MAX_EXPR;
33943 	  else if (id == ovl_op_identifier (false, PLUS_EXPR))
33944 	    code = PLUS_EXPR;
33945 	  else if (id == ovl_op_identifier (false, MULT_EXPR))
33946 	    code = MULT_EXPR;
33947 	  else if (id == ovl_op_identifier (false, MINUS_EXPR))
33948 	    code = MINUS_EXPR;
33949 	  else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33950 	    code = BIT_AND_EXPR;
33951 	  else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33952 	    code = BIT_IOR_EXPR;
33953 	  else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33954 	    code = BIT_XOR_EXPR;
33955 	  else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33956 	    code = TRUTH_ANDIF_EXPR;
33957 	  else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33958 	    code = TRUTH_ORIF_EXPR;
33959 	  id = omp_reduction_id (code, id, NULL_TREE);
33960 	  tree scope = parser->scope;
33961 	  if (scope)
33962 	    id = build_qualified_name (NULL_TREE, scope, id, false);
33963 	  parser->scope = NULL_TREE;
33964 	  parser->qualifying_scope = NULL_TREE;
33965 	  parser->object_scope = NULL_TREE;
33966 	}
33967       else
33968 	{
33969 	  error ("invalid reduction-identifier");
33970 	 resync_fail:
33971 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33972 						 /*or_comma=*/false,
33973 						 /*consume_paren=*/true);
33974 	  return list;
33975 	}
33976     }
33977 
33978   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33979     goto resync_fail;
33980 
33981   nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33982 					  NULL);
33983   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33984     {
33985       OMP_CLAUSE_REDUCTION_CODE (c) = code;
33986       if (task)
33987 	OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33988       else if (inscan)
33989 	OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33990       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33991     }
33992 
33993   return nlist;
33994 }
33995 
33996 /* OpenMP 2.5:
33997    schedule ( schedule-kind )
33998    schedule ( schedule-kind , expression )
33999 
34000    schedule-kind:
34001      static | dynamic | guided | runtime | auto
34002 
34003    OpenMP 4.5:
34004    schedule ( schedule-modifier : schedule-kind )
34005    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
34006 
34007    schedule-modifier:
34008      simd
34009      monotonic
34010      nonmonotonic  */
34011 
34012 static tree
cp_parser_omp_clause_schedule(cp_parser * parser,tree list,location_t location)34013 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
34014 {
34015   tree c, t;
34016   int modifiers = 0, nmodifiers = 0;
34017 
34018   matching_parens parens;
34019   if (!parens.require_open (parser))
34020     return list;
34021 
34022   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
34023 
34024   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34025     {
34026       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34027       const char *p = IDENTIFIER_POINTER (id);
34028       if (strcmp ("simd", p) == 0)
34029 	OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
34030       else if (strcmp ("monotonic", p) == 0)
34031 	modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
34032       else if (strcmp ("nonmonotonic", p) == 0)
34033 	modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
34034       else
34035 	break;
34036       cp_lexer_consume_token (parser->lexer);
34037       if (nmodifiers++ == 0
34038 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34039 	cp_lexer_consume_token (parser->lexer);
34040       else
34041 	{
34042 	  cp_parser_require (parser, CPP_COLON, RT_COLON);
34043 	  break;
34044 	}
34045     }
34046 
34047   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34048     {
34049       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34050       const char *p = IDENTIFIER_POINTER (id);
34051 
34052       switch (p[0])
34053 	{
34054 	case 'd':
34055 	  if (strcmp ("dynamic", p) != 0)
34056 	    goto invalid_kind;
34057 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
34058 	  break;
34059 
34060 	case 'g':
34061 	  if (strcmp ("guided", p) != 0)
34062 	    goto invalid_kind;
34063 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
34064 	  break;
34065 
34066 	case 'r':
34067 	  if (strcmp ("runtime", p) != 0)
34068 	    goto invalid_kind;
34069 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
34070 	  break;
34071 
34072 	default:
34073 	  goto invalid_kind;
34074 	}
34075     }
34076   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34077     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
34078   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
34079     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
34080   else
34081     goto invalid_kind;
34082   cp_lexer_consume_token (parser->lexer);
34083 
34084   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
34085 		    | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34086       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
34087 	  | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
34088     {
34089       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
34090 			  "specified");
34091       modifiers = 0;
34092     }
34093 
34094   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34095     {
34096       cp_token *token;
34097       cp_lexer_consume_token (parser->lexer);
34098 
34099       token = cp_lexer_peek_token (parser->lexer);
34100       t = cp_parser_assignment_expression (parser);
34101 
34102       if (t == error_mark_node)
34103 	goto resync_fail;
34104       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
34105 	error_at (token->location, "schedule %<runtime%> does not take "
34106 		  "a %<chunk_size%> parameter");
34107       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
34108 	error_at (token->location, "schedule %<auto%> does not take "
34109 		  "a %<chunk_size%> parameter");
34110       else
34111 	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
34112 
34113       if (!parens.require_close (parser))
34114 	goto resync_fail;
34115     }
34116   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34117     goto resync_fail;
34118 
34119   OMP_CLAUSE_SCHEDULE_KIND (c)
34120     = (enum omp_clause_schedule_kind)
34121       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
34122 
34123   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
34124   OMP_CLAUSE_CHAIN (c) = list;
34125   return c;
34126 
34127  invalid_kind:
34128   cp_parser_error (parser, "invalid schedule kind");
34129  resync_fail:
34130   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34131 					 /*or_comma=*/false,
34132 					 /*consume_paren=*/true);
34133   return list;
34134 }
34135 
34136 /* OpenMP 3.0:
34137    untied */
34138 
34139 static tree
cp_parser_omp_clause_untied(cp_parser *,tree list,location_t location)34140 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
34141 			     tree list, location_t location)
34142 {
34143   tree c;
34144 
34145   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
34146 
34147   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
34148   OMP_CLAUSE_CHAIN (c) = list;
34149   return c;
34150 }
34151 
34152 /* OpenMP 4.0:
34153    inbranch
34154    notinbranch */
34155 
34156 static tree
cp_parser_omp_clause_branch(cp_parser *,enum omp_clause_code code,tree list,location_t location)34157 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
34158 			     tree list, location_t location)
34159 {
34160   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34161   tree c = build_omp_clause (location, code);
34162   OMP_CLAUSE_CHAIN (c) = list;
34163   return c;
34164 }
34165 
34166 /* OpenMP 4.0:
34167    parallel
34168    for
34169    sections
34170    taskgroup */
34171 
34172 static tree
cp_parser_omp_clause_cancelkind(cp_parser *,enum omp_clause_code code,tree list,location_t location)34173 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
34174 				 enum omp_clause_code code,
34175 				 tree list, location_t location)
34176 {
34177   tree c = build_omp_clause (location, code);
34178   OMP_CLAUSE_CHAIN (c) = list;
34179   return c;
34180 }
34181 
34182 /* OpenMP 4.5:
34183    nogroup */
34184 
34185 static tree
cp_parser_omp_clause_nogroup(cp_parser *,tree list,location_t location)34186 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
34187 			      tree list, location_t location)
34188 {
34189   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
34190   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
34191   OMP_CLAUSE_CHAIN (c) = list;
34192   return c;
34193 }
34194 
34195 /* OpenMP 4.5:
34196    simd
34197    threads */
34198 
34199 static tree
cp_parser_omp_clause_orderedkind(cp_parser *,enum omp_clause_code code,tree list,location_t location)34200 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
34201 				  enum omp_clause_code code,
34202 				  tree list, location_t location)
34203 {
34204   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
34205   tree c = build_omp_clause (location, code);
34206   OMP_CLAUSE_CHAIN (c) = list;
34207   return c;
34208 }
34209 
34210 /* OpenMP 4.0:
34211    num_teams ( expression ) */
34212 
34213 static tree
cp_parser_omp_clause_num_teams(cp_parser * parser,tree list,location_t location)34214 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
34215 				location_t location)
34216 {
34217   tree t, c;
34218 
34219   matching_parens parens;
34220   if (!parens.require_open (parser))
34221     return list;
34222 
34223   t = cp_parser_assignment_expression (parser);
34224 
34225   if (t == error_mark_node
34226       || !parens.require_close (parser))
34227     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34228 					   /*or_comma=*/false,
34229 					   /*consume_paren=*/true);
34230 
34231   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34232 			     "num_teams", location);
34233 
34234   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34235   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34236   OMP_CLAUSE_CHAIN (c) = list;
34237 
34238   return c;
34239 }
34240 
34241 /* OpenMP 4.0:
34242    thread_limit ( expression ) */
34243 
34244 static tree
cp_parser_omp_clause_thread_limit(cp_parser * parser,tree list,location_t location)34245 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34246 				   location_t location)
34247 {
34248   tree t, c;
34249 
34250   matching_parens parens;
34251   if (!parens.require_open (parser))
34252     return list;
34253 
34254   t = cp_parser_assignment_expression (parser);
34255 
34256   if (t == error_mark_node
34257       || !parens.require_close (parser))
34258     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34259 					   /*or_comma=*/false,
34260 					   /*consume_paren=*/true);
34261 
34262   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34263 			     "thread_limit", location);
34264 
34265   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34266   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34267   OMP_CLAUSE_CHAIN (c) = list;
34268 
34269   return c;
34270 }
34271 
34272 /* OpenMP 4.0:
34273    aligned ( variable-list )
34274    aligned ( variable-list : constant-expression )  */
34275 
34276 static tree
cp_parser_omp_clause_aligned(cp_parser * parser,tree list)34277 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34278 {
34279   tree nlist, c, alignment = NULL_TREE;
34280   bool colon;
34281 
34282   matching_parens parens;
34283   if (!parens.require_open (parser))
34284     return list;
34285 
34286   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34287 					  &colon);
34288 
34289   if (colon)
34290     {
34291       alignment = cp_parser_constant_expression (parser);
34292 
34293       if (!parens.require_close (parser))
34294 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34295 					       /*or_comma=*/false,
34296 					       /*consume_paren=*/true);
34297 
34298       if (alignment == error_mark_node)
34299 	alignment = NULL_TREE;
34300     }
34301 
34302   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34303     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34304 
34305   return nlist;
34306 }
34307 
34308 /* OpenMP 2.5:
34309    lastprivate ( variable-list )
34310 
34311    OpenMP 5.0:
34312    lastprivate ( [ lastprivate-modifier : ] variable-list )  */
34313 
34314 static tree
cp_parser_omp_clause_lastprivate(cp_parser * parser,tree list)34315 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34316 {
34317   bool conditional = false;
34318 
34319   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34320     return list;
34321 
34322   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34323       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34324     {
34325       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34326       const char *p = IDENTIFIER_POINTER (id);
34327 
34328       if (strcmp ("conditional", p) == 0)
34329 	{
34330 	  conditional = true;
34331 	  cp_lexer_consume_token (parser->lexer);
34332 	  cp_lexer_consume_token (parser->lexer);
34333 	}
34334     }
34335 
34336   tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34337 					       list, NULL);
34338 
34339   if (conditional)
34340     for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34341       OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34342   return nlist;
34343 }
34344 
34345 /* OpenMP 4.0:
34346    linear ( variable-list )
34347    linear ( variable-list : expression )
34348 
34349    OpenMP 4.5:
34350    linear ( modifier ( variable-list ) )
34351    linear ( modifier ( variable-list ) : expression ) */
34352 
34353 static tree
cp_parser_omp_clause_linear(cp_parser * parser,tree list,bool declare_simd)34354 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34355 			     bool declare_simd)
34356 {
34357   tree nlist, c, step = integer_one_node;
34358   bool colon;
34359   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34360 
34361   matching_parens parens;
34362   if (!parens.require_open (parser))
34363     return list;
34364 
34365   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34366     {
34367       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34368       const char *p = IDENTIFIER_POINTER (id);
34369 
34370       if (strcmp ("ref", p) == 0)
34371 	kind = OMP_CLAUSE_LINEAR_REF;
34372       else if (strcmp ("val", p) == 0)
34373 	kind = OMP_CLAUSE_LINEAR_VAL;
34374       else if (strcmp ("uval", p) == 0)
34375 	kind = OMP_CLAUSE_LINEAR_UVAL;
34376       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34377 	cp_lexer_consume_token (parser->lexer);
34378       else
34379 	kind = OMP_CLAUSE_LINEAR_DEFAULT;
34380     }
34381 
34382   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34383     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34384 					    &colon);
34385   else
34386     {
34387       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34388       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34389       if (colon)
34390 	cp_parser_require (parser, CPP_COLON, RT_COLON);
34391       else if (!parens.require_close (parser))
34392 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34393 					       /*or_comma=*/false,
34394 					       /*consume_paren=*/true);
34395     }
34396 
34397   if (colon)
34398     {
34399       step = NULL_TREE;
34400       if (declare_simd
34401 	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34402 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34403 	{
34404 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
34405 	  cp_parser_parse_tentatively (parser);
34406 	  step = cp_parser_id_expression (parser, /*template_p=*/false,
34407 					  /*check_dependency_p=*/true,
34408 					  /*template_p=*/NULL,
34409 					  /*declarator_p=*/false,
34410 					  /*optional_p=*/false);
34411 	  if (step != error_mark_node)
34412 	    step = cp_parser_lookup_name_simple (parser, step, token->location);
34413 	  if (step == error_mark_node)
34414 	    {
34415 	      step = NULL_TREE;
34416 	      cp_parser_abort_tentative_parse (parser);
34417 	    }
34418 	  else if (!cp_parser_parse_definitely (parser))
34419 	    step = NULL_TREE;
34420 	}
34421       if (!step)
34422 	step = cp_parser_assignment_expression (parser);
34423 
34424       if (!parens.require_close (parser))
34425 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34426 					       /*or_comma=*/false,
34427 					       /*consume_paren=*/true);
34428 
34429       if (step == error_mark_node)
34430 	return list;
34431     }
34432 
34433   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34434     {
34435       OMP_CLAUSE_LINEAR_STEP (c) = step;
34436       OMP_CLAUSE_LINEAR_KIND (c) = kind;
34437     }
34438 
34439   return nlist;
34440 }
34441 
34442 /* OpenMP 4.0:
34443    safelen ( constant-expression )  */
34444 
34445 static tree
cp_parser_omp_clause_safelen(cp_parser * parser,tree list,location_t location)34446 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34447 			      location_t location)
34448 {
34449   tree t, c;
34450 
34451   matching_parens parens;
34452   if (!parens.require_open (parser))
34453     return list;
34454 
34455   t = cp_parser_constant_expression (parser);
34456 
34457   if (t == error_mark_node
34458       || !parens.require_close (parser))
34459     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34460 					   /*or_comma=*/false,
34461 					   /*consume_paren=*/true);
34462 
34463   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34464 
34465   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34466   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34467   OMP_CLAUSE_CHAIN (c) = list;
34468 
34469   return c;
34470 }
34471 
34472 /* OpenMP 4.0:
34473    simdlen ( constant-expression )  */
34474 
34475 static tree
cp_parser_omp_clause_simdlen(cp_parser * parser,tree list,location_t location)34476 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34477 			      location_t location)
34478 {
34479   tree t, c;
34480 
34481   matching_parens parens;
34482   if (!parens.require_open (parser))
34483     return list;
34484 
34485   t = cp_parser_constant_expression (parser);
34486 
34487   if (t == error_mark_node
34488       || !parens.require_close (parser))
34489     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34490 					   /*or_comma=*/false,
34491 					   /*consume_paren=*/true);
34492 
34493   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34494 
34495   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34496   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34497   OMP_CLAUSE_CHAIN (c) = list;
34498 
34499   return c;
34500 }
34501 
34502 /* OpenMP 4.5:
34503    vec:
34504      identifier [+/- integer]
34505      vec , identifier [+/- integer]
34506 */
34507 
34508 static tree
cp_parser_omp_clause_depend_sink(cp_parser * parser,location_t clause_loc,tree list)34509 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34510 				  tree list)
34511 {
34512   tree vec = NULL;
34513 
34514   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34515     {
34516       cp_parser_error (parser, "expected identifier");
34517       return list;
34518     }
34519 
34520   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34521     {
34522       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34523       tree t, identifier = cp_parser_identifier (parser);
34524       tree addend = NULL;
34525 
34526       if (identifier == error_mark_node)
34527 	t = error_mark_node;
34528       else
34529 	{
34530 	  t = cp_parser_lookup_name_simple
34531 		(parser, identifier,
34532 		 cp_lexer_peek_token (parser->lexer)->location);
34533 	  if (t == error_mark_node)
34534 	    cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34535 					 id_loc);
34536 	}
34537 
34538       bool neg = false;
34539       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34540 	neg = true;
34541       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34542 	{
34543 	  addend = integer_zero_node;
34544 	  goto add_to_vector;
34545 	}
34546       cp_lexer_consume_token (parser->lexer);
34547 
34548       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34549 	{
34550 	  cp_parser_error (parser, "expected integer");
34551 	  return list;
34552 	}
34553 
34554       addend = cp_lexer_peek_token (parser->lexer)->u.value;
34555       if (TREE_CODE (addend) != INTEGER_CST)
34556 	{
34557 	  cp_parser_error (parser, "expected integer");
34558 	  return list;
34559 	}
34560       cp_lexer_consume_token (parser->lexer);
34561 
34562     add_to_vector:
34563       if (t != error_mark_node)
34564 	{
34565 	  vec = tree_cons (addend, t, vec);
34566 	  if (neg)
34567 	    OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34568 	}
34569 
34570       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34571 	break;
34572 
34573       cp_lexer_consume_token (parser->lexer);
34574     }
34575 
34576   if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34577     {
34578       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34579       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34580       OMP_CLAUSE_DECL (u) = nreverse (vec);
34581       OMP_CLAUSE_CHAIN (u) = list;
34582       return u;
34583     }
34584   return list;
34585 }
34586 
34587 /* OpenMP 5.0:
34588    iterators ( iterators-definition )
34589 
34590    iterators-definition:
34591      iterator-specifier
34592      iterator-specifier , iterators-definition
34593 
34594    iterator-specifier:
34595      identifier = range-specification
34596      iterator-type identifier = range-specification
34597 
34598    range-specification:
34599      begin : end
34600      begin : end : step  */
34601 
34602 static tree
cp_parser_omp_iterators(cp_parser * parser)34603 cp_parser_omp_iterators (cp_parser *parser)
34604 {
34605   tree ret = NULL_TREE, *last = &ret;
34606   cp_lexer_consume_token (parser->lexer);
34607 
34608   matching_parens parens;
34609   if (!parens.require_open (parser))
34610     return error_mark_node;
34611 
34612   bool saved_colon_corrects_to_scope_p
34613     = parser->colon_corrects_to_scope_p;
34614   bool saved_colon_doesnt_start_class_def_p
34615     = parser->colon_doesnt_start_class_def_p;
34616 
34617   do
34618     {
34619       tree iter_type;
34620       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34621 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34622 	iter_type = integer_type_node;
34623       else
34624 	{
34625 	  const char *saved_message
34626 	    = parser->type_definition_forbidden_message;
34627 	  parser->type_definition_forbidden_message
34628 	    = G_("types may not be defined in iterator type");
34629 
34630 	  iter_type = cp_parser_type_id (parser);
34631 
34632 	  parser->type_definition_forbidden_message = saved_message;
34633 	}
34634 
34635       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34636       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34637 	{
34638 	  cp_parser_error (parser, "expected identifier");
34639 	  break;
34640 	}
34641 
34642       tree id = cp_parser_identifier (parser);
34643       if (id == error_mark_node)
34644 	break;
34645 
34646       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34647 	break;
34648 
34649       parser->colon_corrects_to_scope_p = false;
34650       parser->colon_doesnt_start_class_def_p = true;
34651       tree begin = cp_parser_assignment_expression (parser);
34652 
34653       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34654 	break;
34655 
34656       tree end = cp_parser_assignment_expression (parser);
34657 
34658       tree step = integer_one_node;
34659       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34660 	{
34661 	  cp_lexer_consume_token (parser->lexer);
34662 	  step = cp_parser_assignment_expression (parser);
34663 	}
34664 
34665       tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34666       DECL_ARTIFICIAL (iter_var) = 1;
34667       DECL_CONTEXT (iter_var) = current_function_decl;
34668       pushdecl (iter_var);
34669 
34670       *last = make_tree_vec (6);
34671       TREE_VEC_ELT (*last, 0) = iter_var;
34672       TREE_VEC_ELT (*last, 1) = begin;
34673       TREE_VEC_ELT (*last, 2) = end;
34674       TREE_VEC_ELT (*last, 3) = step;
34675       last = &TREE_CHAIN (*last);
34676 
34677       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34678 	{
34679 	  cp_lexer_consume_token (parser->lexer);
34680 	  continue;
34681 	}
34682       break;
34683     }
34684   while (1);
34685 
34686   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34687   parser->colon_doesnt_start_class_def_p
34688     = saved_colon_doesnt_start_class_def_p;
34689 
34690   if (!parens.require_close (parser))
34691     cp_parser_skip_to_closing_parenthesis (parser,
34692 					   /*recovering=*/true,
34693 					   /*or_comma=*/false,
34694 					   /*consume_paren=*/true);
34695 
34696   return ret ? ret : error_mark_node;
34697 }
34698 
34699 /* OpenMP 4.0:
34700    depend ( depend-kind : variable-list )
34701 
34702    depend-kind:
34703      in | out | inout
34704 
34705    OpenMP 4.5:
34706    depend ( source )
34707 
34708    depend ( sink : vec )
34709 
34710    OpenMP 5.0:
34711    depend ( depend-modifier , depend-kind: variable-list )
34712 
34713    depend-kind:
34714      in | out | inout | mutexinoutset | depobj
34715 
34716    depend-modifier:
34717      iterator ( iterators-definition )  */
34718 
34719 static tree
cp_parser_omp_clause_depend(cp_parser * parser,tree list,location_t loc)34720 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34721 {
34722   tree nlist, c, iterators = NULL_TREE;
34723   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34724 
34725   matching_parens parens;
34726   if (!parens.require_open (parser))
34727     return list;
34728 
34729   do
34730     {
34731       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34732 	goto invalid_kind;
34733 
34734       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34735       const char *p = IDENTIFIER_POINTER (id);
34736 
34737       if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34738 	{
34739 	  begin_scope (sk_omp, NULL);
34740 	  iterators = cp_parser_omp_iterators (parser);
34741 	  cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34742 	  continue;
34743 	}
34744       if (strcmp ("in", p) == 0)
34745 	kind = OMP_CLAUSE_DEPEND_IN;
34746       else if (strcmp ("inout", p) == 0)
34747 	kind = OMP_CLAUSE_DEPEND_INOUT;
34748       else if (strcmp ("mutexinoutset", p) == 0)
34749 	kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34750       else if (strcmp ("out", p) == 0)
34751 	kind = OMP_CLAUSE_DEPEND_OUT;
34752       else if (strcmp ("depobj", p) == 0)
34753 	kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34754       else if (strcmp ("sink", p) == 0)
34755 	kind = OMP_CLAUSE_DEPEND_SINK;
34756       else if (strcmp ("source", p) == 0)
34757 	kind = OMP_CLAUSE_DEPEND_SOURCE;
34758       else
34759 	goto invalid_kind;
34760       break;
34761     }
34762   while (1);
34763 
34764   cp_lexer_consume_token (parser->lexer);
34765 
34766   if (iterators
34767       && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34768     {
34769       poplevel (0, 1, 0);
34770       error_at (loc, "%<iterator%> modifier incompatible with %qs",
34771 		kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34772       iterators = NULL_TREE;
34773     }
34774 
34775   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34776     {
34777       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34778       OMP_CLAUSE_DEPEND_KIND (c) = kind;
34779       OMP_CLAUSE_DECL (c) = NULL_TREE;
34780       OMP_CLAUSE_CHAIN (c) = list;
34781       if (!parens.require_close (parser))
34782 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34783 					       /*or_comma=*/false,
34784 					       /*consume_paren=*/true);
34785       return c;
34786     }
34787 
34788   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34789     goto resync_fail;
34790 
34791   if (kind == OMP_CLAUSE_DEPEND_SINK)
34792     nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34793   else
34794     {
34795       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34796 					      list, NULL);
34797 
34798       if (iterators)
34799 	{
34800 	  tree block = poplevel (1, 1, 0);
34801 	  if (iterators == error_mark_node)
34802 	    iterators = NULL_TREE;
34803 	  else
34804 	    TREE_VEC_ELT (iterators, 5) = block;
34805 	}
34806 
34807       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34808 	{
34809 	  OMP_CLAUSE_DEPEND_KIND (c) = kind;
34810 	  if (iterators)
34811 	    OMP_CLAUSE_DECL (c)
34812 	      = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34813 	}
34814     }
34815   return nlist;
34816 
34817  invalid_kind:
34818   cp_parser_error (parser, "invalid depend kind");
34819  resync_fail:
34820   if (iterators)
34821     poplevel (0, 1, 0);
34822   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34823 					 /*or_comma=*/false,
34824 					 /*consume_paren=*/true);
34825   return list;
34826 }
34827 
34828 /* OpenMP 4.0:
34829    map ( map-kind : variable-list )
34830    map ( variable-list )
34831 
34832    map-kind:
34833      alloc | to | from | tofrom
34834 
34835    OpenMP 4.5:
34836    map-kind:
34837      alloc | to | from | tofrom | release | delete
34838 
34839    map ( always [,] map-kind: variable-list ) */
34840 
34841 static tree
cp_parser_omp_clause_map(cp_parser * parser,tree list)34842 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34843 {
34844   tree nlist, c;
34845   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34846   bool always = false;
34847 
34848   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34849     return list;
34850 
34851   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34852     {
34853       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34854       const char *p = IDENTIFIER_POINTER (id);
34855 
34856       if (strcmp ("always", p) == 0)
34857 	{
34858 	  int nth = 2;
34859 	  if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34860 	    nth++;
34861 	  if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34862 	       || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34863 		   == RID_DELETE))
34864 	      && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34865 		  == CPP_COLON))
34866 	    {
34867 	      always = true;
34868 	      cp_lexer_consume_token (parser->lexer);
34869 	      if (nth == 3)
34870 		cp_lexer_consume_token (parser->lexer);
34871 	    }
34872 	}
34873     }
34874 
34875   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34876       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34877     {
34878       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34879       const char *p = IDENTIFIER_POINTER (id);
34880 
34881       if (strcmp ("alloc", p) == 0)
34882 	kind = GOMP_MAP_ALLOC;
34883       else if (strcmp ("to", p) == 0)
34884 	kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34885       else if (strcmp ("from", p) == 0)
34886 	kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34887       else if (strcmp ("tofrom", p) == 0)
34888 	kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34889       else if (strcmp ("release", p) == 0)
34890 	kind = GOMP_MAP_RELEASE;
34891       else
34892 	{
34893 	  cp_parser_error (parser, "invalid map kind");
34894 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34895 						 /*or_comma=*/false,
34896 						 /*consume_paren=*/true);
34897 	  return list;
34898 	}
34899       cp_lexer_consume_token (parser->lexer);
34900       cp_lexer_consume_token (parser->lexer);
34901     }
34902   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34903 	   && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34904     {
34905       kind = GOMP_MAP_DELETE;
34906       cp_lexer_consume_token (parser->lexer);
34907       cp_lexer_consume_token (parser->lexer);
34908     }
34909 
34910   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34911 					  NULL);
34912 
34913   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34914     OMP_CLAUSE_SET_MAP_KIND (c, kind);
34915 
34916   return nlist;
34917 }
34918 
34919 /* OpenMP 4.0:
34920    device ( expression ) */
34921 
34922 static tree
cp_parser_omp_clause_device(cp_parser * parser,tree list,location_t location)34923 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34924 			     location_t location)
34925 {
34926   tree t, c;
34927 
34928   matching_parens parens;
34929   if (!parens.require_open (parser))
34930     return list;
34931 
34932   t = cp_parser_assignment_expression (parser);
34933 
34934   if (t == error_mark_node
34935       || !parens.require_close (parser))
34936     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34937 					   /*or_comma=*/false,
34938 					   /*consume_paren=*/true);
34939 
34940   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34941 			     "device", location);
34942 
34943   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34944   OMP_CLAUSE_DEVICE_ID (c) = t;
34945   OMP_CLAUSE_CHAIN (c) = list;
34946 
34947   return c;
34948 }
34949 
34950 /* OpenMP 4.0:
34951    dist_schedule ( static )
34952    dist_schedule ( static , expression )  */
34953 
34954 static tree
cp_parser_omp_clause_dist_schedule(cp_parser * parser,tree list,location_t location)34955 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34956 				    location_t location)
34957 {
34958   tree c, t;
34959 
34960   matching_parens parens;
34961   if (!parens.require_open (parser))
34962     return list;
34963 
34964   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34965 
34966   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34967     goto invalid_kind;
34968   cp_lexer_consume_token (parser->lexer);
34969 
34970   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34971     {
34972       cp_lexer_consume_token (parser->lexer);
34973 
34974       t = cp_parser_assignment_expression (parser);
34975 
34976       if (t == error_mark_node)
34977 	goto resync_fail;
34978       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34979 
34980       if (!parens.require_close (parser))
34981 	goto resync_fail;
34982     }
34983   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34984     goto resync_fail;
34985 
34986   /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
34987 				"dist_schedule", location); */
34988   if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
34989     warning_at (location, 0, "too many %qs clauses", "dist_schedule");
34990   OMP_CLAUSE_CHAIN (c) = list;
34991   return c;
34992 
34993  invalid_kind:
34994   cp_parser_error (parser, "invalid dist_schedule kind");
34995  resync_fail:
34996   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34997 					 /*or_comma=*/false,
34998 					 /*consume_paren=*/true);
34999   return list;
35000 }
35001 
35002 /* OpenMP 4.0:
35003    proc_bind ( proc-bind-kind )
35004 
35005    proc-bind-kind:
35006      master | close | spread  */
35007 
35008 static tree
cp_parser_omp_clause_proc_bind(cp_parser * parser,tree list,location_t location)35009 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
35010 				location_t location)
35011 {
35012   tree c;
35013   enum omp_clause_proc_bind_kind kind;
35014 
35015   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35016     return list;
35017 
35018   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35019     {
35020       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35021       const char *p = IDENTIFIER_POINTER (id);
35022 
35023       if (strcmp ("master", p) == 0)
35024 	kind = OMP_CLAUSE_PROC_BIND_MASTER;
35025       else if (strcmp ("close", p) == 0)
35026 	kind = OMP_CLAUSE_PROC_BIND_CLOSE;
35027       else if (strcmp ("spread", p) == 0)
35028 	kind = OMP_CLAUSE_PROC_BIND_SPREAD;
35029       else
35030 	goto invalid_kind;
35031     }
35032   else
35033     goto invalid_kind;
35034 
35035   cp_lexer_consume_token (parser->lexer);
35036   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
35037     goto resync_fail;
35038 
35039   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
35040   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
35041 			     location);
35042   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
35043   OMP_CLAUSE_CHAIN (c) = list;
35044   return c;
35045 
35046  invalid_kind:
35047   cp_parser_error (parser, "invalid depend kind");
35048  resync_fail:
35049   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35050 					 /*or_comma=*/false,
35051 					 /*consume_paren=*/true);
35052   return list;
35053 }
35054 
35055 /* OpenACC:
35056    async [( int-expr )] */
35057 
35058 static tree
cp_parser_oacc_clause_async(cp_parser * parser,tree list)35059 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
35060 {
35061   tree c, t;
35062   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35063 
35064   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
35065 
35066   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35067     {
35068       matching_parens parens;
35069       parens.consume_open (parser);
35070 
35071       t = cp_parser_assignment_expression (parser);
35072       if (t == error_mark_node
35073 	  || !parens.require_close (parser))
35074 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35075 						/*or_comma=*/false,
35076 						/*consume_paren=*/true);
35077     }
35078 
35079   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
35080 
35081   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
35082   OMP_CLAUSE_ASYNC_EXPR (c) = t;
35083   OMP_CLAUSE_CHAIN (c) = list;
35084   list = c;
35085 
35086   return list;
35087 }
35088 
35089 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
35090    is a bitmask in MASK.  Return the list of clauses found.  */
35091 
35092 static tree
35093 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
35094 			   const char *where, cp_token *pragma_tok,
35095 			   bool finish_p = true)
35096 {
35097   tree clauses = NULL;
35098   bool first = true;
35099 
35100   /* Don't create location wrapper nodes within OpenACC clauses.  */
35101   auto_suppress_location_wrappers sentinel;
35102 
35103   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35104     {
35105       location_t here;
35106       pragma_omp_clause c_kind;
35107       omp_clause_code code;
35108       const char *c_name;
35109       tree prev = clauses;
35110 
35111       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35112 	cp_lexer_consume_token (parser->lexer);
35113 
35114       here = cp_lexer_peek_token (parser->lexer)->location;
35115       c_kind = cp_parser_omp_clause_name (parser);
35116 
35117       switch (c_kind)
35118 	{
35119 	case PRAGMA_OACC_CLAUSE_ASYNC:
35120 	  clauses = cp_parser_oacc_clause_async (parser, clauses);
35121 	  c_name = "async";
35122 	  break;
35123 	case PRAGMA_OACC_CLAUSE_AUTO:
35124 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
35125 						  clauses);
35126 	  c_name = "auto";
35127 	  break;
35128 	case PRAGMA_OACC_CLAUSE_COLLAPSE:
35129 	  clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
35130 	  c_name = "collapse";
35131 	  break;
35132 	case PRAGMA_OACC_CLAUSE_COPY:
35133 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35134 	  c_name = "copy";
35135 	  break;
35136 	case PRAGMA_OACC_CLAUSE_COPYIN:
35137 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35138 	  c_name = "copyin";
35139 	  break;
35140 	case PRAGMA_OACC_CLAUSE_COPYOUT:
35141 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35142 	  c_name = "copyout";
35143 	  break;
35144 	case PRAGMA_OACC_CLAUSE_CREATE:
35145 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35146 	  c_name = "create";
35147 	  break;
35148 	case PRAGMA_OACC_CLAUSE_DELETE:
35149 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35150 	  c_name = "delete";
35151 	  break;
35152 	case PRAGMA_OMP_CLAUSE_DEFAULT:
35153 	  clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
35154 	  c_name = "default";
35155 	  break;
35156 	case PRAGMA_OACC_CLAUSE_DEVICE:
35157 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35158 	  c_name = "device";
35159 	  break;
35160 	case PRAGMA_OACC_CLAUSE_DEVICEPTR:
35161 	  clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
35162 	  c_name = "deviceptr";
35163 	  break;
35164 	case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
35165 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35166 	  c_name = "device_resident";
35167 	  break;
35168 	case PRAGMA_OACC_CLAUSE_FINALIZE:
35169 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
35170 						  clauses);
35171 	  c_name = "finalize";
35172 	  break;
35173 	case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
35174 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35175 					    clauses);
35176 	  c_name = "firstprivate";
35177 	  break;
35178 	case PRAGMA_OACC_CLAUSE_GANG:
35179 	  c_name = "gang";
35180 	  clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
35181 						 c_name, clauses);
35182 	  break;
35183 	case PRAGMA_OACC_CLAUSE_HOST:
35184 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35185 	  c_name = "host";
35186 	  break;
35187 	case PRAGMA_OACC_CLAUSE_IF:
35188 	  clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
35189 	  c_name = "if";
35190 	  break;
35191 	case PRAGMA_OACC_CLAUSE_IF_PRESENT:
35192 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
35193 						  clauses);
35194 	  c_name = "if_present";
35195 	  break;
35196 	case PRAGMA_OACC_CLAUSE_INDEPENDENT:
35197 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
35198 						  clauses);
35199 	  c_name = "independent";
35200 	  break;
35201 	case PRAGMA_OACC_CLAUSE_LINK:
35202 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35203 	  c_name = "link";
35204 	  break;
35205 	case PRAGMA_OACC_CLAUSE_NUM_GANGS:
35206 	  code = OMP_CLAUSE_NUM_GANGS;
35207 	  c_name = "num_gangs";
35208 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35209 						      clauses);
35210 	  break;
35211 	case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
35212 	  c_name = "num_workers";
35213 	  code = OMP_CLAUSE_NUM_WORKERS;
35214 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35215 						      clauses);
35216 	  break;
35217 	case PRAGMA_OACC_CLAUSE_PRESENT:
35218 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
35219 	  c_name = "present";
35220 	  break;
35221 	case PRAGMA_OACC_CLAUSE_PRIVATE:
35222 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35223 					    clauses);
35224 	  c_name = "private";
35225 	  break;
35226 	case PRAGMA_OACC_CLAUSE_REDUCTION:
35227 	  clauses
35228 	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35229 					      false, clauses);
35230 	  c_name = "reduction";
35231 	  break;
35232 	case PRAGMA_OACC_CLAUSE_SEQ:
35233 	  clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
35234 						  clauses);
35235 	  c_name = "seq";
35236 	  break;
35237 	case PRAGMA_OACC_CLAUSE_TILE:
35238 	  clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35239 	  c_name = "tile";
35240 	  break;
35241 	case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35242 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35243 					    clauses);
35244 	  c_name = "use_device";
35245 	  break;
35246 	case PRAGMA_OACC_CLAUSE_VECTOR:
35247 	  c_name = "vector";
35248 	  clauses = cp_parser_oacc_shape_clause (parser, here,
35249 						 OMP_CLAUSE_VECTOR,
35250 						 c_name, clauses);
35251 	  break;
35252 	case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35253 	  c_name = "vector_length";
35254 	  code = OMP_CLAUSE_VECTOR_LENGTH;
35255 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35256 						      clauses);
35257 	  break;
35258 	case PRAGMA_OACC_CLAUSE_WAIT:
35259 	  clauses = cp_parser_oacc_clause_wait (parser, clauses);
35260 	  c_name = "wait";
35261 	  break;
35262 	case PRAGMA_OACC_CLAUSE_WORKER:
35263 	  c_name = "worker";
35264 	  clauses = cp_parser_oacc_shape_clause (parser, here,
35265 						 OMP_CLAUSE_WORKER,
35266 						 c_name, clauses);
35267 	  break;
35268 	default:
35269 	  cp_parser_error (parser, "expected %<#pragma acc%> clause");
35270 	  goto saw_error;
35271 	}
35272 
35273       first = false;
35274 
35275       if (((mask >> c_kind) & 1) == 0)
35276 	{
35277 	  /* Remove the invalid clause(s) from the list to avoid
35278 	     confusing the rest of the compiler.  */
35279 	  clauses = prev;
35280 	  error_at (here, "%qs is not valid for %qs", c_name, where);
35281 	}
35282     }
35283 
35284  saw_error:
35285   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35286 
35287   if (finish_p)
35288     return finish_omp_clauses (clauses, C_ORT_ACC);
35289 
35290   return clauses;
35291 }
35292 
35293 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
35294    is a bitmask in MASK.  Return the list of clauses found; the result
35295    of clause default goes in *pdefault.  */
35296 
35297 static tree
35298 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35299 			   const char *where, cp_token *pragma_tok,
35300 			   bool finish_p = true)
35301 {
35302   tree clauses = NULL;
35303   bool first = true;
35304   cp_token *token = NULL;
35305 
35306   /* Don't create location wrapper nodes within OpenMP clauses.  */
35307   auto_suppress_location_wrappers sentinel;
35308 
35309   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35310     {
35311       pragma_omp_clause c_kind;
35312       const char *c_name;
35313       tree prev = clauses;
35314 
35315       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35316 	cp_lexer_consume_token (parser->lexer);
35317 
35318       token = cp_lexer_peek_token (parser->lexer);
35319       c_kind = cp_parser_omp_clause_name (parser);
35320 
35321       switch (c_kind)
35322 	{
35323 	case PRAGMA_OMP_CLAUSE_COLLAPSE:
35324 	  clauses = cp_parser_omp_clause_collapse (parser, clauses,
35325 						   token->location);
35326 	  c_name = "collapse";
35327 	  break;
35328 	case PRAGMA_OMP_CLAUSE_COPYIN:
35329 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35330 	  c_name = "copyin";
35331 	  break;
35332 	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35333 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35334 					    clauses);
35335 	  c_name = "copyprivate";
35336 	  break;
35337 	case PRAGMA_OMP_CLAUSE_DEFAULT:
35338 	  clauses = cp_parser_omp_clause_default (parser, clauses,
35339 						  token->location, false);
35340 	  c_name = "default";
35341 	  break;
35342 	case PRAGMA_OMP_CLAUSE_FINAL:
35343 	  clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35344 	  c_name = "final";
35345 	  break;
35346 	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35347 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35348 					    clauses);
35349 	  c_name = "firstprivate";
35350 	  break;
35351 	case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35352 	  clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35353 						    token->location);
35354 	  c_name = "grainsize";
35355 	  break;
35356 	case PRAGMA_OMP_CLAUSE_HINT:
35357 	  clauses = cp_parser_omp_clause_hint (parser, clauses,
35358 					       token->location);
35359 	  c_name = "hint";
35360 	  break;
35361 	case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35362 	  clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35363 						     token->location);
35364 	  c_name = "defaultmap";
35365 	  break;
35366 	case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35367 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35368 					    clauses);
35369 	  c_name = "use_device_ptr";
35370 	  break;
35371 	case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35372 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35373 					    clauses);
35374 	  c_name = "is_device_ptr";
35375 	  break;
35376 	case PRAGMA_OMP_CLAUSE_IF:
35377 	  clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35378 					     true);
35379 	  c_name = "if";
35380 	  break;
35381 	case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35382 	  clauses
35383 	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35384 					      true, clauses);
35385 	  c_name = "in_reduction";
35386 	  break;
35387 	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35388 	  clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35389 	  c_name = "lastprivate";
35390 	  break;
35391 	case PRAGMA_OMP_CLAUSE_MERGEABLE:
35392 	  clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35393 						    token->location);
35394 	  c_name = "mergeable";
35395 	  break;
35396 	case PRAGMA_OMP_CLAUSE_NOWAIT:
35397 	  clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35398 	  c_name = "nowait";
35399 	  break;
35400 	case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35401 	  clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35402 						    token->location);
35403 	  c_name = "num_tasks";
35404 	  break;
35405 	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35406 	  clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35407 						      token->location);
35408 	  c_name = "num_threads";
35409 	  break;
35410 	case PRAGMA_OMP_CLAUSE_ORDERED:
35411 	  clauses = cp_parser_omp_clause_ordered (parser, clauses,
35412 						  token->location);
35413 	  c_name = "ordered";
35414 	  break;
35415 	case PRAGMA_OMP_CLAUSE_PRIORITY:
35416 	  clauses = cp_parser_omp_clause_priority (parser, clauses,
35417 						   token->location);
35418 	  c_name = "priority";
35419 	  break;
35420 	case PRAGMA_OMP_CLAUSE_PRIVATE:
35421 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35422 					    clauses);
35423 	  c_name = "private";
35424 	  break;
35425 	case PRAGMA_OMP_CLAUSE_REDUCTION:
35426 	  clauses
35427 	    = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35428 					      true, clauses);
35429 	  c_name = "reduction";
35430 	  break;
35431 	case PRAGMA_OMP_CLAUSE_SCHEDULE:
35432 	  clauses = cp_parser_omp_clause_schedule (parser, clauses,
35433 						   token->location);
35434 	  c_name = "schedule";
35435 	  break;
35436 	case PRAGMA_OMP_CLAUSE_SHARED:
35437 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35438 					    clauses);
35439 	  c_name = "shared";
35440 	  break;
35441 	case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35442 	  clauses
35443 	    = cp_parser_omp_clause_reduction (parser,
35444 					      OMP_CLAUSE_TASK_REDUCTION,
35445 					      true, clauses);
35446 	  c_name = "task_reduction";
35447 	  break;
35448 	case PRAGMA_OMP_CLAUSE_UNTIED:
35449 	  clauses = cp_parser_omp_clause_untied (parser, clauses,
35450 						 token->location);
35451 	  c_name = "untied";
35452 	  break;
35453 	case PRAGMA_OMP_CLAUSE_INBRANCH:
35454 	  clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35455 						 clauses, token->location);
35456 	  c_name = "inbranch";
35457 	  break;
35458 	case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35459 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35460 					    clauses);
35461 	  c_name = "nontemporal";
35462 	  break;
35463 	case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35464 	  clauses = cp_parser_omp_clause_branch (parser,
35465 						 OMP_CLAUSE_NOTINBRANCH,
35466 						 clauses, token->location);
35467 	  c_name = "notinbranch";
35468 	  break;
35469 	case PRAGMA_OMP_CLAUSE_PARALLEL:
35470 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35471 						     clauses, token->location);
35472 	  c_name = "parallel";
35473 	  if (!first)
35474 	    {
35475 	     clause_not_first:
35476 	      error_at (token->location, "%qs must be the first clause of %qs",
35477 			c_name, where);
35478 	      clauses = prev;
35479 	    }
35480 	  break;
35481 	case PRAGMA_OMP_CLAUSE_FOR:
35482 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35483 						     clauses, token->location);
35484 	  c_name = "for";
35485 	  if (!first)
35486 	    goto clause_not_first;
35487 	  break;
35488 	case PRAGMA_OMP_CLAUSE_SECTIONS:
35489 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35490 						     clauses, token->location);
35491 	  c_name = "sections";
35492 	  if (!first)
35493 	    goto clause_not_first;
35494 	  break;
35495 	case PRAGMA_OMP_CLAUSE_TASKGROUP:
35496 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35497 						     clauses, token->location);
35498 	  c_name = "taskgroup";
35499 	  if (!first)
35500 	    goto clause_not_first;
35501 	  break;
35502 	case PRAGMA_OMP_CLAUSE_LINK:
35503 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35504 	  c_name = "to";
35505 	  break;
35506 	case PRAGMA_OMP_CLAUSE_TO:
35507 	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35508 	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35509 					      clauses);
35510 	  else
35511 	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35512 	  c_name = "to";
35513 	  break;
35514 	case PRAGMA_OMP_CLAUSE_FROM:
35515 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35516 	  c_name = "from";
35517 	  break;
35518 	case PRAGMA_OMP_CLAUSE_UNIFORM:
35519 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35520 					    clauses);
35521 	  c_name = "uniform";
35522 	  break;
35523 	case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35524 	  clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35525 						    token->location);
35526 	  c_name = "num_teams";
35527 	  break;
35528 	case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35529 	  clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35530 						       token->location);
35531 	  c_name = "thread_limit";
35532 	  break;
35533 	case PRAGMA_OMP_CLAUSE_ALIGNED:
35534 	  clauses = cp_parser_omp_clause_aligned (parser, clauses);
35535 	  c_name = "aligned";
35536 	  break;
35537 	case PRAGMA_OMP_CLAUSE_LINEAR:
35538 	  {
35539 	    bool declare_simd = false;
35540 	    if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35541 	      declare_simd = true;
35542 	    clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35543 	  }
35544 	  c_name = "linear";
35545 	  break;
35546 	case PRAGMA_OMP_CLAUSE_DEPEND:
35547 	  clauses = cp_parser_omp_clause_depend (parser, clauses,
35548 						 token->location);
35549 	  c_name = "depend";
35550 	  break;
35551 	case PRAGMA_OMP_CLAUSE_MAP:
35552 	  clauses = cp_parser_omp_clause_map (parser, clauses);
35553 	  c_name = "map";
35554 	  break;
35555 	case PRAGMA_OMP_CLAUSE_DEVICE:
35556 	  clauses = cp_parser_omp_clause_device (parser, clauses,
35557 						 token->location);
35558 	  c_name = "device";
35559 	  break;
35560 	case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35561 	  clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35562 							token->location);
35563 	  c_name = "dist_schedule";
35564 	  break;
35565 	case PRAGMA_OMP_CLAUSE_PROC_BIND:
35566 	  clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35567 						    token->location);
35568 	  c_name = "proc_bind";
35569 	  break;
35570 	case PRAGMA_OMP_CLAUSE_SAFELEN:
35571 	  clauses = cp_parser_omp_clause_safelen (parser, clauses,
35572 						  token->location);
35573 	  c_name = "safelen";
35574 	  break;
35575 	case PRAGMA_OMP_CLAUSE_SIMDLEN:
35576 	  clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35577 						  token->location);
35578 	  c_name = "simdlen";
35579 	  break;
35580 	case PRAGMA_OMP_CLAUSE_NOGROUP:
35581 	  clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35582 						  token->location);
35583 	  c_name = "nogroup";
35584 	  break;
35585 	case PRAGMA_OMP_CLAUSE_THREADS:
35586 	  clauses
35587 	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35588 						clauses, token->location);
35589 	  c_name = "threads";
35590 	  break;
35591 	case PRAGMA_OMP_CLAUSE_SIMD:
35592 	  clauses
35593 	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35594 						clauses, token->location);
35595 	  c_name = "simd";
35596 	  break;
35597 	default:
35598 	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
35599 	  goto saw_error;
35600 	}
35601 
35602       first = false;
35603 
35604       if (((mask >> c_kind) & 1) == 0)
35605 	{
35606 	  /* Remove the invalid clause(s) from the list to avoid
35607 	     confusing the rest of the compiler.  */
35608 	  clauses = prev;
35609 	  error_at (token->location, "%qs is not valid for %qs", c_name, where);
35610 	}
35611     }
35612  saw_error:
35613   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35614   if (finish_p)
35615     {
35616       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35617 	return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35618       else
35619 	return finish_omp_clauses (clauses, C_ORT_OMP);
35620     }
35621   return clauses;
35622 }
35623 
35624 /* OpenMP 2.5:
35625    structured-block:
35626      statement
35627 
35628    In practice, we're also interested in adding the statement to an
35629    outer node.  So it is convenient if we work around the fact that
35630    cp_parser_statement calls add_stmt.  */
35631 
35632 static unsigned
cp_parser_begin_omp_structured_block(cp_parser * parser)35633 cp_parser_begin_omp_structured_block (cp_parser *parser)
35634 {
35635   unsigned save = parser->in_statement;
35636 
35637   /* Only move the values to IN_OMP_BLOCK if they weren't false.
35638      This preserves the "not within loop or switch" style error messages
35639      for nonsense cases like
35640 	void foo() {
35641 	#pragma omp single
35642 	  break;
35643 	}
35644   */
35645   if (parser->in_statement)
35646     parser->in_statement = IN_OMP_BLOCK;
35647 
35648   return save;
35649 }
35650 
35651 static void
cp_parser_end_omp_structured_block(cp_parser * parser,unsigned save)35652 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35653 {
35654   parser->in_statement = save;
35655 }
35656 
35657 static tree
cp_parser_omp_structured_block(cp_parser * parser,bool * if_p)35658 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35659 {
35660   tree stmt = begin_omp_structured_block ();
35661   unsigned int save = cp_parser_begin_omp_structured_block (parser);
35662 
35663   cp_parser_statement (parser, NULL_TREE, false, if_p);
35664 
35665   cp_parser_end_omp_structured_block (parser, save);
35666   return finish_omp_structured_block (stmt);
35667 }
35668 
35669 /* OpenMP 2.5:
35670    # pragma omp atomic new-line
35671      expression-stmt
35672 
35673    expression-stmt:
35674      x binop= expr | x++ | ++x | x-- | --x
35675    binop:
35676      +, *, -, /, &, ^, |, <<, >>
35677 
35678   where x is an lvalue expression with scalar type.
35679 
35680    OpenMP 3.1:
35681    # pragma omp atomic new-line
35682      update-stmt
35683 
35684    # pragma omp atomic read new-line
35685      read-stmt
35686 
35687    # pragma omp atomic write new-line
35688      write-stmt
35689 
35690    # pragma omp atomic update new-line
35691      update-stmt
35692 
35693    # pragma omp atomic capture new-line
35694      capture-stmt
35695 
35696    # pragma omp atomic capture new-line
35697      capture-block
35698 
35699    read-stmt:
35700      v = x
35701    write-stmt:
35702      x = expr
35703    update-stmt:
35704      expression-stmt | x = x binop expr
35705    capture-stmt:
35706      v = expression-stmt
35707    capture-block:
35708      { v = x; update-stmt; } | { update-stmt; v = x; }
35709 
35710    OpenMP 4.0:
35711    update-stmt:
35712      expression-stmt | x = x binop expr | x = expr binop x
35713    capture-stmt:
35714      v = update-stmt
35715    capture-block:
35716      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35717 
35718   where x and v are lvalue expressions with scalar type.  */
35719 
35720 static void
cp_parser_omp_atomic(cp_parser * parser,cp_token * pragma_tok)35721 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35722 {
35723   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35724   tree rhs1 = NULL_TREE, orig_lhs;
35725   location_t loc = pragma_tok->location;
35726   enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35727   enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35728   bool structured_block = false;
35729   bool first = true;
35730   tree clauses = NULL_TREE;
35731 
35732   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35733     {
35734       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35735 	cp_lexer_consume_token (parser->lexer);
35736 
35737       first = false;
35738 
35739       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35740 	{
35741 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35742 	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35743 	  const char *p = IDENTIFIER_POINTER (id);
35744 	  enum tree_code new_code = ERROR_MARK;
35745 	  enum omp_memory_order new_memory_order
35746 	    = OMP_MEMORY_ORDER_UNSPECIFIED;
35747 
35748 	  if (!strcmp (p, "read"))
35749 	    new_code = OMP_ATOMIC_READ;
35750 	  else if (!strcmp (p, "write"))
35751 	    new_code = NOP_EXPR;
35752 	  else if (!strcmp (p, "update"))
35753 	    new_code = OMP_ATOMIC;
35754 	  else if (!strcmp (p, "capture"))
35755 	    new_code = OMP_ATOMIC_CAPTURE_NEW;
35756 	  else if (!strcmp (p, "seq_cst"))
35757 	    new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35758 	  else if (!strcmp (p, "acq_rel"))
35759 	    new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35760 	  else if (!strcmp (p, "release"))
35761 	    new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35762 	  else if (!strcmp (p, "acquire"))
35763 	    new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35764 	  else if (!strcmp (p, "relaxed"))
35765 	    new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35766 	  else if (!strcmp (p, "hint"))
35767 	    {
35768 	      cp_lexer_consume_token (parser->lexer);
35769 	      clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35770 	      continue;
35771 	    }
35772 	  else
35773 	    {
35774 	      p = NULL;
35775 	      error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35776 			      "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35777 			      "%<release%>, %<relaxed%> or %<hint%> clause");
35778 	    }
35779 	  if (p)
35780 	    {
35781 	      if (new_code != ERROR_MARK)
35782 		{
35783 		  if (code != ERROR_MARK)
35784 		    error_at (cloc, "too many atomic clauses");
35785 		  else
35786 		    code = new_code;
35787 		}
35788 	      else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35789 		{
35790 		  if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35791 		    error_at (cloc, "too many memory order clauses");
35792 		  else
35793 		    memory_order = new_memory_order;
35794 		}
35795 	      cp_lexer_consume_token (parser->lexer);
35796 	      continue;
35797 	    }
35798 	}
35799       break;
35800     }
35801   cp_parser_require_pragma_eol (parser, pragma_tok);
35802 
35803   if (code == ERROR_MARK)
35804     code = OMP_ATOMIC;
35805   if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35806     {
35807       omp_requires_mask
35808 	= (enum omp_requires) (omp_requires_mask
35809 			       | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35810       switch ((enum omp_memory_order)
35811 	      (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35812 	{
35813 	case OMP_MEMORY_ORDER_UNSPECIFIED:
35814 	case OMP_MEMORY_ORDER_RELAXED:
35815 	  memory_order = OMP_MEMORY_ORDER_RELAXED;
35816 	  break;
35817 	case OMP_MEMORY_ORDER_SEQ_CST:
35818 	  memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35819 	  break;
35820 	case OMP_MEMORY_ORDER_ACQ_REL:
35821 	  switch (code)
35822 	    {
35823 	    case OMP_ATOMIC_READ:
35824 	      memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35825 	      break;
35826 	    case NOP_EXPR: /* atomic write */
35827 	    case OMP_ATOMIC:
35828 	      memory_order = OMP_MEMORY_ORDER_RELEASE;
35829 	      break;
35830 	    default:
35831 	      memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35832 	      break;
35833 	    }
35834 	  break;
35835 	default:
35836 	  gcc_unreachable ();
35837 	}
35838     }
35839   else
35840     switch (code)
35841       {
35842       case OMP_ATOMIC_READ:
35843 	if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35844 	    || memory_order == OMP_MEMORY_ORDER_RELEASE)
35845 	  {
35846 	    error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35847 			   "%<acq_rel%> or %<release%> clauses");
35848 	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35849 	  }
35850 	break;
35851       case NOP_EXPR: /* atomic write */
35852 	if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35853 	    || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35854 	  {
35855 	    error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35856 			   "%<acq_rel%> or %<acquire%> clauses");
35857 	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35858 	  }
35859 	break;
35860       case OMP_ATOMIC:
35861 	if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35862 	    || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35863 	  {
35864 	    error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35865 			   "%<acq_rel%> or %<acquire%> clauses");
35866 	    memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35867 	  }
35868 	break;
35869       default:
35870 	break;
35871       }
35872 
35873   switch (code)
35874     {
35875     case OMP_ATOMIC_READ:
35876     case NOP_EXPR: /* atomic write */
35877       v = cp_parser_unary_expression (parser);
35878       if (v == error_mark_node)
35879 	goto saw_error;
35880       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35881 	goto saw_error;
35882       if (code == NOP_EXPR)
35883 	lhs = cp_parser_expression (parser);
35884       else
35885 	lhs = cp_parser_unary_expression (parser);
35886       if (lhs == error_mark_node)
35887 	goto saw_error;
35888       if (code == NOP_EXPR)
35889 	{
35890 	  /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35891 	     opcode.  */
35892 	  code = OMP_ATOMIC;
35893 	  rhs = lhs;
35894 	  lhs = v;
35895 	  v = NULL_TREE;
35896 	}
35897       goto done;
35898     case OMP_ATOMIC_CAPTURE_NEW:
35899       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35900 	{
35901 	  cp_lexer_consume_token (parser->lexer);
35902 	  structured_block = true;
35903 	}
35904       else
35905 	{
35906 	  v = cp_parser_unary_expression (parser);
35907 	  if (v == error_mark_node)
35908 	    goto saw_error;
35909 	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35910 	    goto saw_error;
35911 	}
35912     default:
35913       break;
35914     }
35915 
35916 restart:
35917   lhs = cp_parser_unary_expression (parser);
35918   orig_lhs = lhs;
35919   switch (TREE_CODE (lhs))
35920     {
35921     case ERROR_MARK:
35922       goto saw_error;
35923 
35924     case POSTINCREMENT_EXPR:
35925       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35926 	code = OMP_ATOMIC_CAPTURE_OLD;
35927       /* FALLTHROUGH */
35928     case PREINCREMENT_EXPR:
35929       lhs = TREE_OPERAND (lhs, 0);
35930       opcode = PLUS_EXPR;
35931       rhs = integer_one_node;
35932       break;
35933 
35934     case POSTDECREMENT_EXPR:
35935       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35936 	code = OMP_ATOMIC_CAPTURE_OLD;
35937       /* FALLTHROUGH */
35938     case PREDECREMENT_EXPR:
35939       lhs = TREE_OPERAND (lhs, 0);
35940       opcode = MINUS_EXPR;
35941       rhs = integer_one_node;
35942       break;
35943 
35944     case COMPOUND_EXPR:
35945       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35946 	 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35947 	 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35948 	 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35949 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35950 					     (TREE_OPERAND (lhs, 1), 0), 0)))
35951 	    == BOOLEAN_TYPE)
35952        /* Undo effects of boolean_increment for post {in,de}crement.  */
35953        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35954       /* FALLTHRU */
35955     case MODIFY_EXPR:
35956       if (TREE_CODE (lhs) == MODIFY_EXPR
35957 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35958 	{
35959 	  /* Undo effects of boolean_increment.  */
35960 	  if (integer_onep (TREE_OPERAND (lhs, 1)))
35961 	    {
35962 	      /* This is pre or post increment.  */
35963 	      rhs = TREE_OPERAND (lhs, 1);
35964 	      lhs = TREE_OPERAND (lhs, 0);
35965 	      opcode = NOP_EXPR;
35966 	      if (code == OMP_ATOMIC_CAPTURE_NEW
35967 		  && !structured_block
35968 		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35969 		code = OMP_ATOMIC_CAPTURE_OLD;
35970 	      break;
35971 	    }
35972 	}
35973       /* FALLTHRU */
35974     default:
35975       switch (cp_lexer_peek_token (parser->lexer)->type)
35976 	{
35977 	case CPP_MULT_EQ:
35978 	  opcode = MULT_EXPR;
35979 	  break;
35980 	case CPP_DIV_EQ:
35981 	  opcode = TRUNC_DIV_EXPR;
35982 	  break;
35983 	case CPP_PLUS_EQ:
35984 	  opcode = PLUS_EXPR;
35985 	  break;
35986 	case CPP_MINUS_EQ:
35987 	  opcode = MINUS_EXPR;
35988 	  break;
35989 	case CPP_LSHIFT_EQ:
35990 	  opcode = LSHIFT_EXPR;
35991 	  break;
35992 	case CPP_RSHIFT_EQ:
35993 	  opcode = RSHIFT_EXPR;
35994 	  break;
35995 	case CPP_AND_EQ:
35996 	  opcode = BIT_AND_EXPR;
35997 	  break;
35998 	case CPP_OR_EQ:
35999 	  opcode = BIT_IOR_EXPR;
36000 	  break;
36001 	case CPP_XOR_EQ:
36002 	  opcode = BIT_XOR_EXPR;
36003 	  break;
36004 	case CPP_EQ:
36005 	  enum cp_parser_prec oprec;
36006 	  cp_token *token;
36007 	  cp_lexer_consume_token (parser->lexer);
36008 	  cp_parser_parse_tentatively (parser);
36009 	  rhs1 = cp_parser_simple_cast_expression (parser);
36010 	  if (rhs1 == error_mark_node)
36011 	    {
36012 	      cp_parser_abort_tentative_parse (parser);
36013 	      cp_parser_simple_cast_expression (parser);
36014 	      goto saw_error;
36015 	    }
36016 	  token = cp_lexer_peek_token (parser->lexer);
36017 	  if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
36018 	    {
36019 	      cp_parser_abort_tentative_parse (parser);
36020 	      cp_parser_parse_tentatively (parser);
36021 	      rhs = cp_parser_binary_expression (parser, false, true,
36022 						 PREC_NOT_OPERATOR, NULL);
36023 	      if (rhs == error_mark_node)
36024 		{
36025 		  cp_parser_abort_tentative_parse (parser);
36026 		  cp_parser_binary_expression (parser, false, true,
36027 					       PREC_NOT_OPERATOR, NULL);
36028 		  goto saw_error;
36029 		}
36030 	      switch (TREE_CODE (rhs))
36031 		{
36032 		case MULT_EXPR:
36033 		case TRUNC_DIV_EXPR:
36034 		case RDIV_EXPR:
36035 		case PLUS_EXPR:
36036 		case MINUS_EXPR:
36037 		case LSHIFT_EXPR:
36038 		case RSHIFT_EXPR:
36039 		case BIT_AND_EXPR:
36040 		case BIT_IOR_EXPR:
36041 		case BIT_XOR_EXPR:
36042 		  if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
36043 		    {
36044 		      if (cp_parser_parse_definitely (parser))
36045 			{
36046 			  opcode = TREE_CODE (rhs);
36047 			  rhs1 = TREE_OPERAND (rhs, 0);
36048 			  rhs = TREE_OPERAND (rhs, 1);
36049 			  goto stmt_done;
36050 			}
36051 		      else
36052 			goto saw_error;
36053 		    }
36054 		  break;
36055 		default:
36056 		  break;
36057 		}
36058 	      cp_parser_abort_tentative_parse (parser);
36059 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
36060 		{
36061 		  rhs = cp_parser_expression (parser);
36062 		  if (rhs == error_mark_node)
36063 		    goto saw_error;
36064 		  opcode = NOP_EXPR;
36065 		  rhs1 = NULL_TREE;
36066 		  goto stmt_done;
36067 		}
36068 	      cp_parser_error (parser,
36069 			       "invalid form of %<#pragma omp atomic%>");
36070 	      goto saw_error;
36071 	    }
36072 	  if (!cp_parser_parse_definitely (parser))
36073 	    goto saw_error;
36074 	  switch (token->type)
36075 	    {
36076 	    case CPP_SEMICOLON:
36077 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36078 		{
36079 		  code = OMP_ATOMIC_CAPTURE_OLD;
36080 		  v = lhs;
36081 		  lhs = NULL_TREE;
36082 		  lhs1 = rhs1;
36083 		  rhs1 = NULL_TREE;
36084 		  cp_lexer_consume_token (parser->lexer);
36085 		  goto restart;
36086 		}
36087 	      else if (structured_block)
36088 		{
36089 		  opcode = NOP_EXPR;
36090 		  rhs = rhs1;
36091 		  rhs1 = NULL_TREE;
36092 		  goto stmt_done;
36093 		}
36094 	      cp_parser_error (parser,
36095 			       "invalid form of %<#pragma omp atomic%>");
36096 	      goto saw_error;
36097 	    case CPP_MULT:
36098 	      opcode = MULT_EXPR;
36099 	      break;
36100 	    case CPP_DIV:
36101 	      opcode = TRUNC_DIV_EXPR;
36102 	      break;
36103 	    case CPP_PLUS:
36104 	      opcode = PLUS_EXPR;
36105 	      break;
36106 	    case CPP_MINUS:
36107 	      opcode = MINUS_EXPR;
36108 	      break;
36109 	    case CPP_LSHIFT:
36110 	      opcode = LSHIFT_EXPR;
36111 	      break;
36112 	    case CPP_RSHIFT:
36113 	      opcode = RSHIFT_EXPR;
36114 	      break;
36115 	    case CPP_AND:
36116 	      opcode = BIT_AND_EXPR;
36117 	      break;
36118 	    case CPP_OR:
36119 	      opcode = BIT_IOR_EXPR;
36120 	      break;
36121 	    case CPP_XOR:
36122 	      opcode = BIT_XOR_EXPR;
36123 	      break;
36124 	    default:
36125 	      cp_parser_error (parser,
36126 			       "invalid operator for %<#pragma omp atomic%>");
36127 	      goto saw_error;
36128 	    }
36129 	  oprec = TOKEN_PRECEDENCE (token);
36130 	  gcc_assert (oprec != PREC_NOT_OPERATOR);
36131 	  if (commutative_tree_code (opcode))
36132 	    oprec = (enum cp_parser_prec) (oprec - 1);
36133 	  cp_lexer_consume_token (parser->lexer);
36134 	  rhs = cp_parser_binary_expression (parser, false, false,
36135 					     oprec, NULL);
36136 	  if (rhs == error_mark_node)
36137 	    goto saw_error;
36138 	  goto stmt_done;
36139 	  /* FALLTHROUGH */
36140 	default:
36141 	  cp_parser_error (parser,
36142 			   "invalid operator for %<#pragma omp atomic%>");
36143 	  goto saw_error;
36144 	}
36145       cp_lexer_consume_token (parser->lexer);
36146 
36147       rhs = cp_parser_expression (parser);
36148       if (rhs == error_mark_node)
36149 	goto saw_error;
36150       break;
36151     }
36152 stmt_done:
36153   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
36154     {
36155       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
36156 	goto saw_error;
36157       v = cp_parser_unary_expression (parser);
36158       if (v == error_mark_node)
36159 	goto saw_error;
36160       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
36161 	goto saw_error;
36162       lhs1 = cp_parser_unary_expression (parser);
36163       if (lhs1 == error_mark_node)
36164 	goto saw_error;
36165     }
36166   if (structured_block)
36167     {
36168       cp_parser_consume_semicolon_at_end_of_statement (parser);
36169       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
36170     }
36171 done:
36172   clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36173   finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
36174 		     rhs1, clauses, memory_order);
36175   if (!structured_block)
36176     cp_parser_consume_semicolon_at_end_of_statement (parser);
36177   return;
36178 
36179  saw_error:
36180   cp_parser_skip_to_end_of_block_or_statement (parser);
36181   if (structured_block)
36182     {
36183       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36184         cp_lexer_consume_token (parser->lexer);
36185       else if (code == OMP_ATOMIC_CAPTURE_NEW)
36186 	{
36187 	  cp_parser_skip_to_end_of_block_or_statement (parser);
36188 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36189 	    cp_lexer_consume_token (parser->lexer);
36190 	}
36191     }
36192 }
36193 
36194 
36195 /* OpenMP 2.5:
36196    # pragma omp barrier new-line  */
36197 
36198 static void
cp_parser_omp_barrier(cp_parser * parser,cp_token * pragma_tok)36199 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
36200 {
36201   cp_parser_require_pragma_eol (parser, pragma_tok);
36202   finish_omp_barrier ();
36203 }
36204 
36205 /* OpenMP 2.5:
36206    # pragma omp critical [(name)] new-line
36207      structured-block
36208 
36209    OpenMP 4.5:
36210    # pragma omp critical [(name) [hint(expression)]] new-line
36211      structured-block  */
36212 
36213 #define OMP_CRITICAL_CLAUSE_MASK		\
36214 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
36215 
36216 static tree
cp_parser_omp_critical(cp_parser * parser,cp_token * pragma_tok,bool * if_p)36217 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36218 {
36219   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
36220 
36221   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36222     {
36223       matching_parens parens;
36224       parens.consume_open (parser);
36225 
36226       name = cp_parser_identifier (parser);
36227 
36228       if (name == error_mark_node
36229 	  || !parens.require_close (parser))
36230 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36231 					       /*or_comma=*/false,
36232 					       /*consume_paren=*/true);
36233       if (name == error_mark_node)
36234 	name = NULL;
36235 
36236       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36237 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36238 	cp_lexer_consume_token (parser->lexer);
36239 
36240       clauses = cp_parser_omp_all_clauses (parser,
36241 					   OMP_CRITICAL_CLAUSE_MASK,
36242 					   "#pragma omp critical", pragma_tok);
36243     }
36244   else
36245     cp_parser_require_pragma_eol (parser, pragma_tok);
36246 
36247   stmt = cp_parser_omp_structured_block (parser, if_p);
36248   return c_finish_omp_critical (input_location, stmt, name, clauses);
36249 }
36250 
36251 /* OpenMP 5.0:
36252    # pragma omp depobj ( depobj ) depobj-clause new-line
36253 
36254    depobj-clause:
36255      depend (dependence-type : locator)
36256      destroy
36257      update (dependence-type)
36258 
36259    dependence-type:
36260      in
36261      out
36262      inout
36263      mutexinout  */
36264 
36265 static void
cp_parser_omp_depobj(cp_parser * parser,cp_token * pragma_tok)36266 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36267 {
36268   location_t loc = pragma_tok->location;
36269   matching_parens parens;
36270   if (!parens.require_open (parser))
36271     {
36272       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36273       return;
36274     }
36275 
36276   tree depobj = cp_parser_assignment_expression (parser);
36277 
36278   if (!parens.require_close (parser))
36279     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36280 					   /*or_comma=*/false,
36281 					   /*consume_paren=*/true);
36282 
36283   tree clause = NULL_TREE;
36284   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36285   location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36286   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36287     {
36288       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36289       const char *p = IDENTIFIER_POINTER (id);
36290 
36291       cp_lexer_consume_token (parser->lexer);
36292       if (!strcmp ("depend", p))
36293 	{
36294 	  /* Don't create location wrapper nodes within the depend clause.  */
36295 	  auto_suppress_location_wrappers sentinel;
36296 	  clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36297 	  if (clause)
36298 	    clause = finish_omp_clauses (clause, C_ORT_OMP);
36299 	  if (!clause)
36300 	    clause = error_mark_node;
36301 	}
36302       else if (!strcmp ("destroy", p))
36303 	kind = OMP_CLAUSE_DEPEND_LAST;
36304       else if (!strcmp ("update", p))
36305 	{
36306 	  matching_parens c_parens;
36307 	  if (c_parens.require_open (parser))
36308 	    {
36309 	      location_t c2_loc
36310 		= cp_lexer_peek_token (parser->lexer)->location;
36311 	      if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36312 		{
36313 		  tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36314 		  const char *p2 = IDENTIFIER_POINTER (id2);
36315 
36316 		  cp_lexer_consume_token (parser->lexer);
36317 		  if (!strcmp ("in", p2))
36318 		    kind = OMP_CLAUSE_DEPEND_IN;
36319 		  else if (!strcmp ("out", p2))
36320 		    kind = OMP_CLAUSE_DEPEND_OUT;
36321 		  else if (!strcmp ("inout", p2))
36322 		    kind = OMP_CLAUSE_DEPEND_INOUT;
36323 		  else if (!strcmp ("mutexinoutset", p2))
36324 		    kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36325 		}
36326 	      if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36327 		{
36328 		  clause = error_mark_node;
36329 		  error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36330 				    "%<mutexinoutset%>");
36331 		}
36332 	      if (!c_parens.require_close (parser))
36333 		cp_parser_skip_to_closing_parenthesis (parser,
36334 						       /*recovering=*/true,
36335 						       /*or_comma=*/false,
36336 						       /*consume_paren=*/true);
36337 	    }
36338 	  else
36339 	    clause = error_mark_node;
36340 	}
36341     }
36342   if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36343     {
36344       clause = error_mark_node;
36345       error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36346     }
36347   cp_parser_require_pragma_eol (parser, pragma_tok);
36348 
36349   finish_omp_depobj (loc, depobj, kind, clause);
36350 }
36351 
36352 
36353 /* OpenMP 2.5:
36354    # pragma omp flush flush-vars[opt] new-line
36355 
36356    flush-vars:
36357      ( variable-list )
36358 
36359    OpenMP 5.0:
36360    # pragma omp flush memory-order-clause new-line  */
36361 
36362 static void
cp_parser_omp_flush(cp_parser * parser,cp_token * pragma_tok)36363 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36364 {
36365   enum memmodel mo = MEMMODEL_LAST;
36366   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36367     {
36368       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36369       const char *p = IDENTIFIER_POINTER (id);
36370       if (!strcmp (p, "acq_rel"))
36371 	mo = MEMMODEL_ACQ_REL;
36372       else if (!strcmp (p, "release"))
36373 	mo = MEMMODEL_RELEASE;
36374       else if (!strcmp (p, "acquire"))
36375 	mo = MEMMODEL_ACQUIRE;
36376       else
36377 	error_at (cp_lexer_peek_token (parser->lexer)->location,
36378 		  "expected %<acq_rel%>, %<release%> or %<acquire%>");
36379       cp_lexer_consume_token (parser->lexer);
36380     }
36381   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36382     {
36383       if (mo != MEMMODEL_LAST)
36384 	error_at (cp_lexer_peek_token (parser->lexer)->location,
36385 		  "%<flush%> list specified together with memory order "
36386 		  "clause");
36387       (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36388     }
36389   cp_parser_require_pragma_eol (parser, pragma_tok);
36390 
36391   finish_omp_flush (mo);
36392 }
36393 
36394 /* Helper function, to parse omp for increment expression.  */
36395 
36396 static tree
cp_parser_omp_for_cond(cp_parser * parser,tree decl,enum tree_code code)36397 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36398 {
36399   tree cond = cp_parser_binary_expression (parser, false, true,
36400 					   PREC_NOT_OPERATOR, NULL);
36401   if (cond == error_mark_node
36402       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36403     {
36404       cp_parser_skip_to_end_of_statement (parser);
36405       return error_mark_node;
36406     }
36407 
36408   switch (TREE_CODE (cond))
36409     {
36410     case GT_EXPR:
36411     case GE_EXPR:
36412     case LT_EXPR:
36413     case LE_EXPR:
36414       break;
36415     case NE_EXPR:
36416       if (code != OACC_LOOP)
36417 	break;
36418       gcc_fallthrough ();
36419     default:
36420       return error_mark_node;
36421     }
36422 
36423   /* If decl is an iterator, preserve LHS and RHS of the relational
36424      expr until finish_omp_for.  */
36425   if (decl
36426       && (type_dependent_expression_p (decl)
36427 	  || CLASS_TYPE_P (TREE_TYPE (decl))))
36428     return cond;
36429 
36430   return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36431 			    TREE_CODE (cond),
36432 			    TREE_OPERAND (cond, 0), ERROR_MARK,
36433 			    TREE_OPERAND (cond, 1), ERROR_MARK,
36434 			    /*overload=*/NULL, tf_warning_or_error);
36435 }
36436 
36437 /* Helper function, to parse omp for increment expression.  */
36438 
36439 static tree
cp_parser_omp_for_incr(cp_parser * parser,tree decl)36440 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36441 {
36442   cp_token *token = cp_lexer_peek_token (parser->lexer);
36443   enum tree_code op;
36444   tree lhs, rhs;
36445   cp_id_kind idk;
36446   bool decl_first;
36447 
36448   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36449     {
36450       op = (token->type == CPP_PLUS_PLUS
36451 	    ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36452       cp_lexer_consume_token (parser->lexer);
36453       lhs = cp_parser_simple_cast_expression (parser);
36454       if (lhs != decl
36455 	  && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36456 	return error_mark_node;
36457       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36458     }
36459 
36460   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36461   if (lhs != decl
36462       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36463     return error_mark_node;
36464 
36465   token = cp_lexer_peek_token (parser->lexer);
36466   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36467     {
36468       op = (token->type == CPP_PLUS_PLUS
36469 	    ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36470       cp_lexer_consume_token (parser->lexer);
36471       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36472     }
36473 
36474   op = cp_parser_assignment_operator_opt (parser);
36475   if (op == ERROR_MARK)
36476     return error_mark_node;
36477 
36478   if (op != NOP_EXPR)
36479     {
36480       rhs = cp_parser_assignment_expression (parser);
36481       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36482       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36483     }
36484 
36485   lhs = cp_parser_binary_expression (parser, false, false,
36486 				     PREC_ADDITIVE_EXPRESSION, NULL);
36487   token = cp_lexer_peek_token (parser->lexer);
36488   decl_first = (lhs == decl
36489 		|| (processing_template_decl && cp_tree_equal (lhs, decl)));
36490   if (decl_first)
36491     lhs = NULL_TREE;
36492   if (token->type != CPP_PLUS
36493       && token->type != CPP_MINUS)
36494     return error_mark_node;
36495 
36496   do
36497     {
36498       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36499       cp_lexer_consume_token (parser->lexer);
36500       rhs = cp_parser_binary_expression (parser, false, false,
36501 					 PREC_ADDITIVE_EXPRESSION, NULL);
36502       token = cp_lexer_peek_token (parser->lexer);
36503       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36504 	{
36505 	  if (lhs == NULL_TREE)
36506 	    {
36507 	      if (op == PLUS_EXPR)
36508 		lhs = rhs;
36509 	      else
36510 		lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36511 					tf_warning_or_error);
36512 	    }
36513 	  else
36514 	    lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36515 				     ERROR_MARK, NULL, tf_warning_or_error);
36516 	}
36517     }
36518   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36519 
36520   if (!decl_first)
36521     {
36522       if ((rhs != decl
36523 	   && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36524 	  || op == MINUS_EXPR)
36525 	return error_mark_node;
36526       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36527     }
36528   else
36529     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36530 
36531   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36532 }
36533 
36534 /* Parse the initialization statement of an OpenMP for loop.
36535 
36536    Return true if the resulting construct should have an
36537    OMP_CLAUSE_PRIVATE added to it.  */
36538 
36539 static tree
cp_parser_omp_for_loop_init(cp_parser * parser,tree & this_pre_body,vec<tree,va_gc> * & for_block,tree & init,tree & orig_init,tree & decl,tree & real_decl)36540 cp_parser_omp_for_loop_init (cp_parser *parser,
36541 			     tree &this_pre_body,
36542 			     vec<tree, va_gc> *&for_block,
36543 			     tree &init,
36544 			     tree &orig_init,
36545 			     tree &decl,
36546 			     tree &real_decl)
36547 {
36548   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36549     return NULL_TREE;
36550 
36551   tree add_private_clause = NULL_TREE;
36552 
36553   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36554 
36555      init-expr:
36556      var = lb
36557      integer-type var = lb
36558      random-access-iterator-type var = lb
36559      pointer-type var = lb
36560   */
36561   cp_decl_specifier_seq type_specifiers;
36562 
36563   /* First, try to parse as an initialized declaration.  See
36564      cp_parser_condition, from whence the bulk of this is copied.  */
36565 
36566   cp_parser_parse_tentatively (parser);
36567   cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36568 				/*is_declaration=*/true,
36569 				/*is_trailing_return=*/false,
36570 				&type_specifiers);
36571   if (cp_parser_parse_definitely (parser))
36572     {
36573       /* If parsing a type specifier seq succeeded, then this
36574 	 MUST be a initialized declaration.  */
36575       tree asm_specification, attributes;
36576       cp_declarator *declarator;
36577 
36578       declarator = cp_parser_declarator (parser,
36579 					 CP_PARSER_DECLARATOR_NAMED,
36580 					 CP_PARSER_FLAGS_NONE,
36581 					 /*ctor_dtor_or_conv_p=*/NULL,
36582 					 /*parenthesized_p=*/NULL,
36583 					 /*member_p=*/false,
36584 					 /*friend_p=*/false,
36585 					 /*static_p=*/false);
36586       attributes = cp_parser_attributes_opt (parser);
36587       asm_specification = cp_parser_asm_specification_opt (parser);
36588 
36589       if (declarator == cp_error_declarator)
36590 	cp_parser_skip_to_end_of_statement (parser);
36591 
36592       else
36593 	{
36594 	  tree pushed_scope, auto_node;
36595 
36596 	  decl = start_decl (declarator, &type_specifiers,
36597 			     SD_INITIALIZED, attributes,
36598 			     /*prefix_attributes=*/NULL_TREE,
36599 			     &pushed_scope);
36600 
36601 	  auto_node = type_uses_auto (TREE_TYPE (decl));
36602 	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36603 	    {
36604 	      if (cp_lexer_next_token_is (parser->lexer,
36605 					  CPP_OPEN_PAREN))
36606 	        error ("parenthesized initialization is not allowed in "
36607 		       "OpenMP %<for%> loop");
36608 	      else
36609 		/* Trigger an error.  */
36610 		cp_parser_require (parser, CPP_EQ, RT_EQ);
36611 
36612 	      init = error_mark_node;
36613 	      cp_parser_skip_to_end_of_statement (parser);
36614 	    }
36615 	  else if (CLASS_TYPE_P (TREE_TYPE (decl))
36616 		   || type_dependent_expression_p (decl)
36617 		   || auto_node)
36618 	    {
36619 	      bool is_direct_init, is_non_constant_init;
36620 
36621 	      init = cp_parser_initializer (parser,
36622 					    &is_direct_init,
36623 					    &is_non_constant_init);
36624 
36625 	      if (auto_node)
36626 		{
36627 		  TREE_TYPE (decl)
36628 		    = do_auto_deduction (TREE_TYPE (decl), init,
36629 					 auto_node);
36630 
36631 		  if (!CLASS_TYPE_P (TREE_TYPE (decl))
36632 		      && !type_dependent_expression_p (decl))
36633 		    goto non_class;
36634 		}
36635 
36636 	      cp_finish_decl (decl, init, !is_non_constant_init,
36637 			      asm_specification,
36638 			      LOOKUP_ONLYCONVERTING);
36639 	      orig_init = init;
36640 	      if (CLASS_TYPE_P (TREE_TYPE (decl)))
36641 		{
36642 		  vec_safe_push (for_block, this_pre_body);
36643 		  init = NULL_TREE;
36644 		}
36645 	      else
36646 		{
36647 		  init = pop_stmt_list (this_pre_body);
36648 		  if (init && TREE_CODE (init) == STATEMENT_LIST)
36649 		    {
36650 		      tree_stmt_iterator i = tsi_start (init);
36651 		      /* Move lambda DECL_EXPRs to FOR_BLOCK.  */
36652 		      while (!tsi_end_p (i))
36653 			{
36654 			  tree t = tsi_stmt (i);
36655 			  if (TREE_CODE (t) == DECL_EXPR
36656 			      && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36657 			    {
36658 			      tsi_delink (&i);
36659 			      vec_safe_push (for_block, t);
36660 			      continue;
36661 			    }
36662 			  break;
36663 			}
36664 		      if (tsi_one_before_end_p (i))
36665 			{
36666 			  tree t = tsi_stmt (i);
36667 			  tsi_delink (&i);
36668 			  free_stmt_list (init);
36669 			  init = t;
36670 			}
36671 		    }
36672 		}
36673 	      this_pre_body = NULL_TREE;
36674 	    }
36675 	  else
36676 	    {
36677 	      /* Consume '='.  */
36678 	      cp_lexer_consume_token (parser->lexer);
36679 	      init = cp_parser_assignment_expression (parser);
36680 
36681 	    non_class:
36682 	      if (TYPE_REF_P (TREE_TYPE (decl)))
36683 		init = error_mark_node;
36684 	      else
36685 		cp_finish_decl (decl, NULL_TREE,
36686 				/*init_const_expr_p=*/false,
36687 				asm_specification,
36688 				LOOKUP_ONLYCONVERTING);
36689 	    }
36690 
36691 	  if (pushed_scope)
36692 	    pop_scope (pushed_scope);
36693 	}
36694     }
36695   else
36696     {
36697       cp_id_kind idk;
36698       /* If parsing a type specifier sequence failed, then
36699 	 this MUST be a simple expression.  */
36700       cp_parser_parse_tentatively (parser);
36701       decl = cp_parser_primary_expression (parser, false, false,
36702 					   false, &idk);
36703       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36704       if (!cp_parser_error_occurred (parser)
36705 	  && decl
36706 	  && (TREE_CODE (decl) == COMPONENT_REF
36707 	      || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36708 	{
36709 	  cp_parser_abort_tentative_parse (parser);
36710 	  cp_parser_parse_tentatively (parser);
36711 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
36712 	  tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36713 					       /*check_dependency_p=*/true,
36714 					       /*template_p=*/NULL,
36715 					       /*declarator_p=*/false,
36716 					       /*optional_p=*/false);
36717 	  if (name != error_mark_node
36718 	      && last_tok == cp_lexer_peek_token (parser->lexer))
36719 	    {
36720 	      decl = cp_parser_lookup_name_simple (parser, name,
36721 						   token->location);
36722 	      if (TREE_CODE (decl) == FIELD_DECL)
36723 		add_private_clause = omp_privatize_field (decl, false);
36724 	    }
36725 	  cp_parser_abort_tentative_parse (parser);
36726 	  cp_parser_parse_tentatively (parser);
36727 	  decl = cp_parser_primary_expression (parser, false, false,
36728 					       false, &idk);
36729 	}
36730       if (!cp_parser_error_occurred (parser)
36731 	  && decl
36732 	  && DECL_P (decl)
36733 	  && CLASS_TYPE_P (TREE_TYPE (decl)))
36734 	{
36735 	  tree rhs;
36736 
36737 	  cp_parser_parse_definitely (parser);
36738 	  cp_parser_require (parser, CPP_EQ, RT_EQ);
36739 	  rhs = cp_parser_assignment_expression (parser);
36740 	  orig_init = rhs;
36741 	  finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36742 						 decl, NOP_EXPR,
36743 						 rhs,
36744 						 tf_warning_or_error));
36745 	  if (!add_private_clause)
36746 	    add_private_clause = decl;
36747 	}
36748       else
36749 	{
36750 	  decl = NULL;
36751 	  cp_parser_abort_tentative_parse (parser);
36752 	  init = cp_parser_expression (parser);
36753 	  if (init)
36754 	    {
36755 	      if (TREE_CODE (init) == MODIFY_EXPR
36756 		  || TREE_CODE (init) == MODOP_EXPR)
36757 		real_decl = TREE_OPERAND (init, 0);
36758 	    }
36759 	}
36760     }
36761   return add_private_clause;
36762 }
36763 
36764 /* Helper for cp_parser_omp_for_loop, handle one range-for loop.  */
36765 
36766 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)36767 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36768 			  tree &decl, tree &orig_decl, tree &init,
36769 			  tree &orig_init, tree &cond, tree &incr)
36770 {
36771   tree begin, end, range_temp_decl = NULL_TREE;
36772   tree iter_type, begin_expr, end_expr;
36773 
36774   if (processing_template_decl)
36775     {
36776       if (check_for_bare_parameter_packs (init))
36777 	init = error_mark_node;
36778       if (!type_dependent_expression_p (init)
36779 	  /* do_auto_deduction doesn't mess with template init-lists.  */
36780 	  && !BRACE_ENCLOSED_INITIALIZER_P (init))
36781 	{
36782 	  tree d = decl;
36783 	  if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36784 	    {
36785 	      tree v = DECL_VALUE_EXPR (decl);
36786 	      if (TREE_CODE (v) == ARRAY_REF
36787 		  && VAR_P (TREE_OPERAND (v, 0))
36788 		  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36789 		d = TREE_OPERAND (v, 0);
36790 	    }
36791 	  do_range_for_auto_deduction (d, init);
36792 	}
36793       cond = global_namespace;
36794       incr = NULL_TREE;
36795       orig_init = init;
36796       if (this_pre_body)
36797 	this_pre_body = pop_stmt_list (this_pre_body);
36798       return;
36799     }
36800 
36801   init = mark_lvalue_use (init);
36802 
36803   if (decl == error_mark_node || init == error_mark_node)
36804     /* If an error happened previously do nothing or else a lot of
36805        unhelpful errors would be issued.  */
36806     begin_expr = end_expr = iter_type = error_mark_node;
36807   else
36808     {
36809       tree range_temp;
36810 
36811       if (VAR_P (init)
36812 	  && array_of_runtime_bound_p (TREE_TYPE (init)))
36813 	/* Can't bind a reference to an array of runtime bound.  */
36814 	range_temp = init;
36815       else
36816 	{
36817 	  range_temp = build_range_temp (init);
36818 	  DECL_NAME (range_temp) = NULL_TREE;
36819 	  pushdecl (range_temp);
36820 	  cp_finish_decl (range_temp, init,
36821 			  /*is_constant_init*/false, NULL_TREE,
36822 			  LOOKUP_ONLYCONVERTING);
36823 	  range_temp_decl = range_temp;
36824 	  range_temp = convert_from_reference (range_temp);
36825 	}
36826       iter_type = cp_parser_perform_range_for_lookup (range_temp,
36827 						      &begin_expr, &end_expr);
36828     }
36829 
36830   tree end_iter_type = iter_type;
36831   if (cxx_dialect >= cxx17)
36832     end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36833   end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36834   TREE_USED (end) = 1;
36835   DECL_ARTIFICIAL (end) = 1;
36836   pushdecl (end);
36837   cp_finish_decl (end, end_expr,
36838 		  /*is_constant_init*/false, NULL_TREE,
36839 		  LOOKUP_ONLYCONVERTING);
36840 
36841   /* The new for initialization statement.  */
36842   begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36843   TREE_USED (begin) = 1;
36844   DECL_ARTIFICIAL (begin) = 1;
36845   pushdecl (begin);
36846   orig_init = init;
36847   if (CLASS_TYPE_P (iter_type))
36848     init = NULL_TREE;
36849   else
36850     {
36851       init = begin_expr;
36852       begin_expr = NULL_TREE;
36853     }
36854   cp_finish_decl (begin, begin_expr,
36855 		  /*is_constant_init*/false, NULL_TREE,
36856 		  LOOKUP_ONLYCONVERTING);
36857 
36858   /* The new for condition.  */
36859   if (CLASS_TYPE_P (iter_type))
36860     cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36861   else
36862     cond = build_x_binary_op (input_location, NE_EXPR,
36863 			      begin, ERROR_MARK,
36864 			      end, ERROR_MARK,
36865 			      NULL, tf_warning_or_error);
36866 
36867   /* The new increment expression.  */
36868   if (CLASS_TYPE_P (iter_type))
36869     incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36870   else
36871     incr = finish_unary_op_expr (input_location,
36872 				 PREINCREMENT_EXPR, begin,
36873 				 tf_warning_or_error);
36874 
36875   orig_decl = decl;
36876   decl = begin;
36877   if (for_block)
36878     {
36879       vec_safe_push (for_block, this_pre_body);
36880       this_pre_body = NULL_TREE;
36881     }
36882 
36883   tree decomp_first_name = NULL_TREE;
36884   unsigned decomp_cnt = 0;
36885   if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36886     {
36887       tree v = DECL_VALUE_EXPR (orig_decl);
36888       if (TREE_CODE (v) == ARRAY_REF
36889 	  && VAR_P (TREE_OPERAND (v, 0))
36890 	  && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36891 	{
36892 	  tree d = orig_decl;
36893 	  orig_decl = TREE_OPERAND (v, 0);
36894 	  decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36895 	  decomp_first_name = d;
36896 	}
36897     }
36898 
36899   tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36900   if (auto_node)
36901     {
36902       tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36903 				     tf_none);
36904       if (!error_operand_p (t))
36905 	TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36906 						   t, auto_node);
36907     }
36908 
36909   tree v = make_tree_vec (decomp_cnt + 3);
36910   TREE_VEC_ELT (v, 0) = range_temp_decl;
36911   TREE_VEC_ELT (v, 1) = end;
36912   TREE_VEC_ELT (v, 2) = orig_decl;
36913   for (unsigned i = 0; i < decomp_cnt; i++)
36914     {
36915       TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36916       decomp_first_name = DECL_CHAIN (decomp_first_name);
36917     }
36918   orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36919 }
36920 
36921 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36922    inside of the collapsed body.  */
36923 
36924 void
cp_finish_omp_range_for(tree orig,tree begin)36925 cp_finish_omp_range_for (tree orig, tree begin)
36926 {
36927   gcc_assert (TREE_CODE (orig) == TREE_LIST
36928 	      && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36929   tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36930   tree decomp_first_name = NULL_TREE;
36931   unsigned int decomp_cnt = 0;
36932 
36933   if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36934     {
36935       decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36936       decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36937       cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36938     }
36939 
36940   /* The declaration is initialized with *__begin inside the loop body.  */
36941   cp_finish_decl (decl,
36942 		  build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36943 					tf_warning_or_error),
36944 		  /*is_constant_init*/false, NULL_TREE,
36945 		  LOOKUP_ONLYCONVERTING);
36946   if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36947     cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36948 }
36949 
36950 /* Parse the restricted form of the for statement allowed by OpenMP.  */
36951 
36952 static tree
cp_parser_omp_for_loop(cp_parser * parser,enum tree_code code,tree clauses,tree * cclauses,bool * if_p)36953 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36954 			tree *cclauses, bool *if_p)
36955 {
36956   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36957   tree orig_decl;
36958   tree real_decl, initv, condv, incrv, declv, orig_declv;
36959   tree this_pre_body, cl, ordered_cl = NULL_TREE;
36960   location_t loc_first;
36961   bool collapse_err = false;
36962   int i, collapse = 1, ordered = 0, count, nbraces = 0;
36963   vec<tree, va_gc> *for_block = make_tree_vector ();
36964   auto_vec<tree, 4> orig_inits;
36965   bool tiling = false;
36966 
36967   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36968     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36969       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36970     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36971       {
36972 	tiling = true;
36973 	collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36974       }
36975     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36976 	     && OMP_CLAUSE_ORDERED_EXPR (cl))
36977       {
36978 	ordered_cl = cl;
36979 	ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36980       }
36981 
36982   if (ordered && ordered < collapse)
36983     {
36984       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36985 		"%<ordered%> clause parameter is less than %<collapse%>");
36986       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36987 	= build_int_cst (NULL_TREE, collapse);
36988       ordered = collapse;
36989     }
36990   if (ordered)
36991     {
36992       for (tree *pc = &clauses; *pc; )
36993 	if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36994 	  {
36995 	    error_at (OMP_CLAUSE_LOCATION (*pc),
36996 		      "%<linear%> clause may not be specified together "
36997 		      "with %<ordered%> clause with a parameter");
36998 	    *pc = OMP_CLAUSE_CHAIN (*pc);
36999 	  }
37000 	else
37001 	  pc = &OMP_CLAUSE_CHAIN (*pc);
37002     }
37003 
37004   gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
37005   count = ordered ? ordered : collapse;
37006 
37007   declv = make_tree_vec (count);
37008   initv = make_tree_vec (count);
37009   condv = make_tree_vec (count);
37010   incrv = make_tree_vec (count);
37011   orig_declv = NULL_TREE;
37012 
37013   loc_first = cp_lexer_peek_token (parser->lexer)->location;
37014 
37015   for (i = 0; i < count; i++)
37016     {
37017       int bracecount = 0;
37018       tree add_private_clause = NULL_TREE;
37019       location_t loc;
37020 
37021       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37022 	{
37023 	  if (!collapse_err)
37024 	    cp_parser_error (parser, "for statement expected");
37025 	  return NULL;
37026 	}
37027       loc = cp_lexer_consume_token (parser->lexer)->location;
37028 
37029       /* Don't create location wrapper nodes within an OpenMP "for"
37030 	 statement.  */
37031       auto_suppress_location_wrappers sentinel;
37032 
37033       matching_parens parens;
37034       if (!parens.require_open (parser))
37035 	return NULL;
37036 
37037       init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
37038       this_pre_body = push_stmt_list ();
37039 
37040       if (code != OACC_LOOP && cxx_dialect >= cxx11)
37041 	{
37042 	  /* Save tokens so that we can put them back.  */
37043 	  cp_lexer_save_tokens (parser->lexer);
37044 
37045 	  /* Look for ':' that is not nested in () or {}.  */
37046 	  bool is_range_for
37047 	    = (cp_parser_skip_to_closing_parenthesis_1 (parser,
37048 							/*recovering=*/false,
37049 							CPP_COLON,
37050 							/*consume_paren=*/
37051 							false) == -1);
37052 
37053 	  /* Roll back the tokens we skipped.  */
37054 	  cp_lexer_rollback_tokens (parser->lexer);
37055 
37056 	  if (is_range_for)
37057 	    {
37058 	      bool saved_colon_corrects_to_scope_p
37059 		= parser->colon_corrects_to_scope_p;
37060 
37061 	      /* A colon is used in range-based for.  */
37062 	      parser->colon_corrects_to_scope_p = false;
37063 
37064 	      /* Parse the declaration.  */
37065 	      cp_parser_simple_declaration (parser,
37066 					    /*function_definition_allowed_p=*/
37067 					    false, &decl);
37068 	      parser->colon_corrects_to_scope_p
37069 		= saved_colon_corrects_to_scope_p;
37070 
37071 	      cp_parser_require (parser, CPP_COLON, RT_COLON);
37072 
37073 	      init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
37074 					  false, 0, true);
37075 
37076 	      cp_convert_omp_range_for (this_pre_body, for_block, decl,
37077 					orig_decl, init, orig_init,
37078 					cond, incr);
37079 	      if (this_pre_body)
37080 		{
37081 		  if (pre_body)
37082 		    {
37083 		      tree t = pre_body;
37084 		      pre_body = push_stmt_list ();
37085 		      add_stmt (t);
37086 		      add_stmt (this_pre_body);
37087 		      pre_body = pop_stmt_list (pre_body);
37088 		    }
37089 		  else
37090 		    pre_body = this_pre_body;
37091 		}
37092 
37093 	      if (ordered_cl)
37094 		error_at (OMP_CLAUSE_LOCATION (ordered_cl),
37095 			  "%<ordered%> clause with parameter on "
37096 			  "range-based %<for%> loop");
37097 
37098 	      goto parse_close_paren;
37099 	    }
37100 	}
37101 
37102       add_private_clause
37103 	= cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
37104 				       init, orig_init, decl, real_decl);
37105 
37106       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37107       if (this_pre_body)
37108 	{
37109 	  this_pre_body = pop_stmt_list (this_pre_body);
37110 	  if (pre_body)
37111 	    {
37112 	      tree t = pre_body;
37113 	      pre_body = push_stmt_list ();
37114 	      add_stmt (t);
37115 	      add_stmt (this_pre_body);
37116 	      pre_body = pop_stmt_list (pre_body);
37117 	    }
37118 	  else
37119 	    pre_body = this_pre_body;
37120 	}
37121 
37122       if (decl)
37123 	real_decl = decl;
37124       if (cclauses != NULL
37125 	  && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
37126 	  && real_decl != NULL_TREE)
37127 	{
37128 	  tree *c;
37129 	  for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
37130 	    if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
37131 		&& OMP_CLAUSE_DECL (*c) == real_decl)
37132 	      {
37133 		error_at (loc, "iteration variable %qD"
37134 			  " should not be firstprivate", real_decl);
37135 		*c = OMP_CLAUSE_CHAIN (*c);
37136 	      }
37137 	    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
37138 		     && OMP_CLAUSE_DECL (*c) == real_decl)
37139 	      {
37140 		/* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
37141 		tree l = *c;
37142 		*c = OMP_CLAUSE_CHAIN (*c);
37143 		if (code == OMP_SIMD)
37144 		  {
37145 		    OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37146 		    cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
37147 		  }
37148 		else
37149 		  {
37150 		    OMP_CLAUSE_CHAIN (l) = clauses;
37151 		    clauses = l;
37152 		  }
37153 		add_private_clause = NULL_TREE;
37154 	      }
37155 	    else
37156 	      {
37157 		if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
37158 		    && OMP_CLAUSE_DECL (*c) == real_decl)
37159 		  add_private_clause = NULL_TREE;
37160 		c = &OMP_CLAUSE_CHAIN (*c);
37161 	      }
37162 	}
37163 
37164       if (add_private_clause)
37165 	{
37166 	  tree c;
37167 	  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
37168 	    {
37169 	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
37170 		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
37171 		  && OMP_CLAUSE_DECL (c) == decl)
37172 		break;
37173 	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
37174 		       && OMP_CLAUSE_DECL (c) == decl)
37175 		error_at (loc, "iteration variable %qD "
37176 			  "should not be firstprivate",
37177 			  decl);
37178 	      else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
37179 			|| OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
37180 		       && OMP_CLAUSE_DECL (c) == decl)
37181 		error_at (loc, "iteration variable %qD should not be reduction",
37182 			  decl);
37183 	    }
37184 	  if (c == NULL)
37185 	    {
37186 	      if (code != OMP_SIMD)
37187 		c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
37188 	      else if (collapse == 1)
37189 		c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37190 	      else
37191 		c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
37192 	      OMP_CLAUSE_DECL (c) = add_private_clause;
37193 	      c = finish_omp_clauses (c, C_ORT_OMP);
37194 	      if (c)
37195 		{
37196 		  OMP_CLAUSE_CHAIN (c) = clauses;
37197 		  clauses = c;
37198 		  /* For linear, signal that we need to fill up
37199 		     the so far unknown linear step.  */
37200 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
37201 		    OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
37202 		}
37203 	    }
37204 	}
37205 
37206       cond = NULL;
37207       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37208 	cond = cp_parser_omp_for_cond (parser, decl, code);
37209       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37210 
37211       incr = NULL;
37212       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
37213 	{
37214 	  /* If decl is an iterator, preserve the operator on decl
37215 	     until finish_omp_for.  */
37216 	  if (real_decl
37217 	      && ((processing_template_decl
37218 		   && (TREE_TYPE (real_decl) == NULL_TREE
37219 		       || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
37220 		  || CLASS_TYPE_P (TREE_TYPE (real_decl))))
37221 	    incr = cp_parser_omp_for_incr (parser, real_decl);
37222 	  else
37223 	    incr = cp_parser_expression (parser);
37224 	  if (!EXPR_HAS_LOCATION (incr))
37225 	    protected_set_expr_location (incr, input_location);
37226 	}
37227 
37228     parse_close_paren:
37229       if (!parens.require_close (parser))
37230 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37231 					       /*or_comma=*/false,
37232 					       /*consume_paren=*/true);
37233 
37234       TREE_VEC_ELT (declv, i) = decl;
37235       TREE_VEC_ELT (initv, i) = init;
37236       TREE_VEC_ELT (condv, i) = cond;
37237       TREE_VEC_ELT (incrv, i) = incr;
37238       if (orig_init)
37239 	{
37240 	  orig_inits.safe_grow_cleared (i + 1);
37241 	  orig_inits[i] = orig_init;
37242 	}
37243       if (orig_decl)
37244 	{
37245 	  if (!orig_declv)
37246 	    orig_declv = copy_node (declv);
37247 	  TREE_VEC_ELT (orig_declv, i) = orig_decl;
37248 	}
37249       else if (orig_declv)
37250 	TREE_VEC_ELT (orig_declv, i) = decl;
37251 
37252       if (i == count - 1)
37253 	break;
37254 
37255       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37256 	 in between the collapsed for loops to be still considered perfectly
37257 	 nested.  Hopefully the final version clarifies this.
37258 	 For now handle (multiple) {'s and empty statements.  */
37259       cp_parser_parse_tentatively (parser);
37260       for (;;)
37261 	{
37262 	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37263 	    break;
37264 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37265 	    {
37266 	      cp_lexer_consume_token (parser->lexer);
37267 	      bracecount++;
37268 	    }
37269 	  else if (bracecount
37270 		   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37271 	    cp_lexer_consume_token (parser->lexer);
37272 	  else
37273 	    {
37274 	      loc = cp_lexer_peek_token (parser->lexer)->location;
37275 	      error_at (loc, "not enough for loops to collapse");
37276 	      collapse_err = true;
37277 	      cp_parser_abort_tentative_parse (parser);
37278 	      declv = NULL_TREE;
37279 	      break;
37280 	    }
37281 	}
37282 
37283       if (declv)
37284 	{
37285 	  cp_parser_parse_definitely (parser);
37286 	  nbraces += bracecount;
37287 	}
37288     }
37289 
37290   if (nbraces)
37291     if_p = NULL;
37292 
37293   /* Note that we saved the original contents of this flag when we entered
37294      the structured block, and so we don't need to re-save it here.  */
37295   parser->in_statement = IN_OMP_FOR;
37296 
37297   /* Note that the grammar doesn't call for a structured block here,
37298      though the loop as a whole is a structured block.  */
37299   if (orig_declv)
37300     {
37301       body = begin_omp_structured_block ();
37302       for (i = 0; i < count; i++)
37303 	if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37304 	  cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37305 				   TREE_VEC_ELT (declv, i));
37306     }
37307   else
37308     body = push_stmt_list ();
37309   cp_parser_statement (parser, NULL_TREE, false, if_p);
37310   if (orig_declv)
37311     body = finish_omp_structured_block (body);
37312   else
37313     body = pop_stmt_list (body);
37314 
37315   if (declv == NULL_TREE)
37316     ret = NULL_TREE;
37317   else
37318     ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37319 			  incrv, body, pre_body, &orig_inits, clauses);
37320 
37321   while (nbraces)
37322     {
37323       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37324 	{
37325 	  cp_lexer_consume_token (parser->lexer);
37326 	  nbraces--;
37327 	}
37328       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37329 	cp_lexer_consume_token (parser->lexer);
37330       else
37331 	{
37332 	  if (!collapse_err)
37333 	    {
37334 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
37335 			"collapsed loops not perfectly nested");
37336 	    }
37337 	  collapse_err = true;
37338 	  cp_parser_statement_seq_opt (parser, NULL);
37339 	  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37340 	    break;
37341 	}
37342     }
37343 
37344   while (!for_block->is_empty ())
37345     {
37346       tree t = for_block->pop ();
37347       if (TREE_CODE (t) == STATEMENT_LIST)
37348 	add_stmt (pop_stmt_list (t));
37349       else
37350 	add_stmt (t);
37351     }
37352   release_tree_vector (for_block);
37353 
37354   return ret;
37355 }
37356 
37357 /* Helper function for OpenMP parsing, split clauses and call
37358    finish_omp_clauses on each of the set of clauses afterwards.  */
37359 
37360 static void
cp_omp_split_clauses(location_t loc,enum tree_code code,omp_clause_mask mask,tree clauses,tree * cclauses)37361 cp_omp_split_clauses (location_t loc, enum tree_code code,
37362 		      omp_clause_mask mask, tree clauses, tree *cclauses)
37363 {
37364   int i;
37365   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37366   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37367     if (cclauses[i])
37368       cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37369 }
37370 
37371 /* OpenMP 4.0:
37372    #pragma omp simd simd-clause[optseq] new-line
37373      for-loop  */
37374 
37375 #define OMP_SIMD_CLAUSE_MASK					\
37376 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)	\
37377 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
37378 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
37379 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
37380 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
37381 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
37382 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
37383 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
37384 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
37385 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37386 
37387 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)37388 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37389 		    char *p_name, omp_clause_mask mask, tree *cclauses,
37390 		    bool *if_p)
37391 {
37392   tree clauses, sb, ret;
37393   unsigned int save;
37394   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37395 
37396   strcat (p_name, " simd");
37397   mask |= OMP_SIMD_CLAUSE_MASK;
37398 
37399   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37400 				       cclauses == NULL);
37401   if (cclauses)
37402     {
37403       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37404       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37405       tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37406 				OMP_CLAUSE_ORDERED);
37407       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37408 	{
37409 	  error_at (OMP_CLAUSE_LOCATION (c),
37410 		    "%<ordered%> clause with parameter may not be specified "
37411 		    "on %qs construct", p_name);
37412 	  OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37413 	}
37414     }
37415 
37416   keep_next_level (true);
37417   sb = begin_omp_structured_block ();
37418   save = cp_parser_begin_omp_structured_block (parser);
37419 
37420   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37421 
37422   cp_parser_end_omp_structured_block (parser, save);
37423   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37424 
37425   return ret;
37426 }
37427 
37428 /* OpenMP 2.5:
37429    #pragma omp for for-clause[optseq] new-line
37430      for-loop
37431 
37432    OpenMP 4.0:
37433    #pragma omp for simd for-simd-clause[optseq] new-line
37434      for-loop  */
37435 
37436 #define OMP_FOR_CLAUSE_MASK					\
37437 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
37438 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
37439 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
37440 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
37441 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
37442 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)	\
37443 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)	\
37444 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
37445 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37446 
37447 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)37448 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37449 		   char *p_name, omp_clause_mask mask, tree *cclauses,
37450 		   bool *if_p)
37451 {
37452   tree clauses, sb, ret;
37453   unsigned int save;
37454   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37455 
37456   strcat (p_name, " for");
37457   mask |= OMP_FOR_CLAUSE_MASK;
37458   /* parallel for{, simd} disallows nowait clause, but for
37459      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
37460   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37461     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37462   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
37463   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37464     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37465 
37466   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37467     {
37468       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37469       const char *p = IDENTIFIER_POINTER (id);
37470 
37471       if (strcmp (p, "simd") == 0)
37472 	{
37473 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37474 	  if (cclauses == NULL)
37475 	    cclauses = cclauses_buf;
37476 
37477 	  cp_lexer_consume_token (parser->lexer);
37478 	  if (!flag_openmp)  /* flag_openmp_simd  */
37479 	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37480 				       cclauses, if_p);
37481 	  sb = begin_omp_structured_block ();
37482 	  save = cp_parser_begin_omp_structured_block (parser);
37483 	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37484 				    cclauses, if_p);
37485 	  cp_parser_end_omp_structured_block (parser, save);
37486 	  tree body = finish_omp_structured_block (sb);
37487 	  if (ret == NULL)
37488 	    return ret;
37489 	  ret = make_node (OMP_FOR);
37490 	  TREE_TYPE (ret) = void_type_node;
37491 	  OMP_FOR_BODY (ret) = body;
37492 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37493 	  SET_EXPR_LOCATION (ret, loc);
37494 	  add_stmt (ret);
37495 	  return ret;
37496 	}
37497     }
37498   if (!flag_openmp)  /* flag_openmp_simd  */
37499     {
37500       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37501       return NULL_TREE;
37502     }
37503 
37504   /* Composite distribute parallel for disallows linear clause.  */
37505   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37506     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37507 
37508   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37509 				       cclauses == NULL);
37510   if (cclauses)
37511     {
37512       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37513       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37514     }
37515 
37516   keep_next_level (true);
37517   sb = begin_omp_structured_block ();
37518   save = cp_parser_begin_omp_structured_block (parser);
37519 
37520   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37521 
37522   cp_parser_end_omp_structured_block (parser, save);
37523   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37524 
37525   return ret;
37526 }
37527 
37528 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37529 				    omp_clause_mask, tree *, bool *);
37530 
37531 /* OpenMP 2.5:
37532    # pragma omp master new-line
37533      structured-block  */
37534 
37535 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)37536 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37537 		      char *p_name, omp_clause_mask mask, tree *cclauses,
37538 		      bool *if_p)
37539 {
37540   tree clauses, sb, ret;
37541   unsigned int save;
37542   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37543 
37544   strcat (p_name, " master");
37545 
37546   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37547     {
37548       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37549       const char *p = IDENTIFIER_POINTER (id);
37550 
37551       if (strcmp (p, "taskloop") == 0)
37552 	{
37553 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37554 	  if (cclauses == NULL)
37555 	    cclauses = cclauses_buf;
37556 
37557 	  cp_lexer_consume_token (parser->lexer);
37558 	  if (!flag_openmp)  /* flag_openmp_simd  */
37559 	    return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37560 					   cclauses, if_p);
37561 	  sb = begin_omp_structured_block ();
37562 	  save = cp_parser_begin_omp_structured_block (parser);
37563 	  ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37564 					cclauses, if_p);
37565 	  cp_parser_end_omp_structured_block (parser, save);
37566 	  tree body = finish_omp_structured_block (sb);
37567 	  if (ret == NULL)
37568 	    return ret;
37569 	  return c_finish_omp_master (loc, body);
37570 	}
37571     }
37572   if (!flag_openmp)  /* flag_openmp_simd  */
37573     {
37574       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37575       return NULL_TREE;
37576     }
37577 
37578   if (cclauses)
37579     {
37580       clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37581 					   false);
37582       cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37583     }
37584   else
37585     cp_parser_require_pragma_eol (parser, pragma_tok);
37586 
37587   return c_finish_omp_master (loc,
37588 			      cp_parser_omp_structured_block (parser, if_p));
37589 }
37590 
37591 /* OpenMP 2.5:
37592    # pragma omp ordered new-line
37593      structured-block
37594 
37595    OpenMP 4.5:
37596    # pragma omp ordered ordered-clauses new-line
37597      structured-block  */
37598 
37599 #define OMP_ORDERED_CLAUSE_MASK					\
37600 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)	\
37601 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37602 
37603 #define OMP_ORDERED_DEPEND_CLAUSE_MASK				\
37604 	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37605 
37606 static bool
cp_parser_omp_ordered(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context,bool * if_p)37607 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37608 		       enum pragma_context context, bool *if_p)
37609 {
37610   location_t loc = pragma_tok->location;
37611 
37612   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37613     {
37614       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37615       const char *p = IDENTIFIER_POINTER (id);
37616 
37617       if (strcmp (p, "depend") == 0)
37618 	{
37619 	  if (!flag_openmp)	/* flag_openmp_simd */
37620 	    {
37621 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37622 	      return false;
37623 	    }
37624 	  if (context == pragma_stmt)
37625 	    {
37626 	      error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37627 			"%<depend%> clause may only be used in compound "
37628 			"statements");
37629 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37630 	      return false;
37631 	    }
37632 	  tree clauses
37633 	    = cp_parser_omp_all_clauses (parser,
37634 					 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37635 					 "#pragma omp ordered", pragma_tok);
37636 	  c_finish_omp_ordered (loc, clauses, NULL_TREE);
37637 	  return false;
37638 	}
37639     }
37640 
37641   tree clauses
37642     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37643 				 "#pragma omp ordered", pragma_tok);
37644 
37645   if (!flag_openmp     /* flag_openmp_simd  */
37646       && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37647     return false;
37648 
37649   c_finish_omp_ordered (loc, clauses,
37650 			cp_parser_omp_structured_block (parser, if_p));
37651   return true;
37652 }
37653 
37654 /* OpenMP 2.5:
37655 
37656    section-scope:
37657      { section-sequence }
37658 
37659    section-sequence:
37660      section-directive[opt] structured-block
37661      section-sequence section-directive structured-block  */
37662 
37663 static tree
cp_parser_omp_sections_scope(cp_parser * parser)37664 cp_parser_omp_sections_scope (cp_parser *parser)
37665 {
37666   tree stmt, substmt;
37667   bool error_suppress = false;
37668   cp_token *tok;
37669 
37670   matching_braces braces;
37671   if (!braces.require_open (parser))
37672     return NULL_TREE;
37673 
37674   stmt = push_stmt_list ();
37675 
37676   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37677       != PRAGMA_OMP_SECTION)
37678     {
37679       substmt = cp_parser_omp_structured_block (parser, NULL);
37680       substmt = build1 (OMP_SECTION, void_type_node, substmt);
37681       add_stmt (substmt);
37682     }
37683 
37684   while (1)
37685     {
37686       tok = cp_lexer_peek_token (parser->lexer);
37687       if (tok->type == CPP_CLOSE_BRACE)
37688 	break;
37689       if (tok->type == CPP_EOF)
37690 	break;
37691 
37692       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37693 	{
37694 	  cp_lexer_consume_token (parser->lexer);
37695 	  cp_parser_require_pragma_eol (parser, tok);
37696 	  error_suppress = false;
37697 	}
37698       else if (!error_suppress)
37699 	{
37700 	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37701 	  error_suppress = true;
37702 	}
37703 
37704       substmt = cp_parser_omp_structured_block (parser, NULL);
37705       substmt = build1 (OMP_SECTION, void_type_node, substmt);
37706       add_stmt (substmt);
37707     }
37708   braces.require_close (parser);
37709 
37710   substmt = pop_stmt_list (stmt);
37711 
37712   stmt = make_node (OMP_SECTIONS);
37713   TREE_TYPE (stmt) = void_type_node;
37714   OMP_SECTIONS_BODY (stmt) = substmt;
37715 
37716   add_stmt (stmt);
37717   return stmt;
37718 }
37719 
37720 /* OpenMP 2.5:
37721    # pragma omp sections sections-clause[optseq] newline
37722      sections-scope  */
37723 
37724 #define OMP_SECTIONS_CLAUSE_MASK				\
37725 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
37726 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
37727 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
37728 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
37729 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37730 
37731 static tree
cp_parser_omp_sections(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)37732 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37733 			char *p_name, omp_clause_mask mask, tree *cclauses)
37734 {
37735   tree clauses, ret;
37736   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37737 
37738   strcat (p_name, " sections");
37739   mask |= OMP_SECTIONS_CLAUSE_MASK;
37740   if (cclauses)
37741     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37742 
37743   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37744 				       cclauses == NULL);
37745   if (cclauses)
37746     {
37747       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37748       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37749     }
37750 
37751   ret = cp_parser_omp_sections_scope (parser);
37752   if (ret)
37753     OMP_SECTIONS_CLAUSES (ret) = clauses;
37754 
37755   return ret;
37756 }
37757 
37758 /* OpenMP 2.5:
37759    # pragma omp parallel parallel-clause[optseq] new-line
37760      structured-block
37761    # pragma omp parallel for parallel-for-clause[optseq] new-line
37762      structured-block
37763    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37764      structured-block
37765 
37766    OpenMP 4.0:
37767    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37768      structured-block */
37769 
37770 #define OMP_PARALLEL_CLAUSE_MASK				\
37771 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
37772 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
37773 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
37774 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
37775 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
37776 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)	\
37777 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
37778 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)	\
37779 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37780 
37781 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)37782 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37783 			char *p_name, omp_clause_mask mask, tree *cclauses,
37784 			bool *if_p)
37785 {
37786   tree stmt, clauses, block;
37787   unsigned int save;
37788   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37789 
37790   strcat (p_name, " parallel");
37791   mask |= OMP_PARALLEL_CLAUSE_MASK;
37792   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
37793   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37794       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37795     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37796 
37797   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37798     {
37799       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37800       if (cclauses == NULL)
37801 	cclauses = cclauses_buf;
37802 
37803       cp_lexer_consume_token (parser->lexer);
37804       if (!flag_openmp)  /* flag_openmp_simd  */
37805 	return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37806 				  if_p);
37807       block = begin_omp_parallel ();
37808       save = cp_parser_begin_omp_structured_block (parser);
37809       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37810 				    if_p);
37811       cp_parser_end_omp_structured_block (parser, save);
37812       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37813 				  block);
37814       if (ret == NULL_TREE)
37815 	return ret;
37816       OMP_PARALLEL_COMBINED (stmt) = 1;
37817       return stmt;
37818     }
37819   /* When combined with distribute, parallel has to be followed by for.
37820      #pragma omp target parallel is allowed though.  */
37821   else if (cclauses
37822 	   && (mask & (OMP_CLAUSE_MASK_1
37823 		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37824     {
37825       error_at (loc, "expected %<for%> after %qs", p_name);
37826       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37827       return NULL_TREE;
37828     }
37829   else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37830     {
37831       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37832       const char *p = IDENTIFIER_POINTER (id);
37833       if (strcmp (p, "master") == 0)
37834 	{
37835 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37836 	  cclauses = cclauses_buf;
37837 
37838 	  cp_lexer_consume_token (parser->lexer);
37839 	  if (!flag_openmp)  /* flag_openmp_simd  */
37840 	    return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37841 					 cclauses, if_p);
37842 	  block = begin_omp_parallel ();
37843 	  save = cp_parser_begin_omp_structured_block (parser);
37844 	  tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37845 					   cclauses, if_p);
37846 	  cp_parser_end_omp_structured_block (parser, save);
37847 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37848 				      block);
37849 	  if (ret == NULL_TREE)
37850 	    return ret;
37851 	  OMP_PARALLEL_COMBINED (stmt) = 1;
37852 	  return stmt;
37853 	}
37854       else if (!flag_openmp)  /* flag_openmp_simd  */
37855 	{
37856 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37857 	  return NULL_TREE;
37858 	}
37859       else if (strcmp (p, "sections") == 0)
37860 	{
37861 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37862 	  cclauses = cclauses_buf;
37863 
37864 	  cp_lexer_consume_token (parser->lexer);
37865 	  block = begin_omp_parallel ();
37866 	  save = cp_parser_begin_omp_structured_block (parser);
37867 	  cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37868 	  cp_parser_end_omp_structured_block (parser, save);
37869 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37870 				      block);
37871 	  OMP_PARALLEL_COMBINED (stmt) = 1;
37872 	  return stmt;
37873 	}
37874     }
37875   else if (!flag_openmp)  /* flag_openmp_simd  */
37876     {
37877       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37878       return NULL_TREE;
37879     }
37880 
37881   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37882 				       cclauses == NULL);
37883   if (cclauses)
37884     {
37885       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37886       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37887     }
37888 
37889   block = begin_omp_parallel ();
37890   save = cp_parser_begin_omp_structured_block (parser);
37891   cp_parser_statement (parser, NULL_TREE, false, if_p);
37892   cp_parser_end_omp_structured_block (parser, save);
37893   stmt = finish_omp_parallel (clauses, block);
37894   return stmt;
37895 }
37896 
37897 /* OpenMP 2.5:
37898    # pragma omp single single-clause[optseq] new-line
37899      structured-block  */
37900 
37901 #define OMP_SINGLE_CLAUSE_MASK					\
37902 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
37903 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
37904 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)	\
37905 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37906 
37907 static tree
cp_parser_omp_single(cp_parser * parser,cp_token * pragma_tok,bool * if_p)37908 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37909 {
37910   tree stmt = make_node (OMP_SINGLE);
37911   TREE_TYPE (stmt) = void_type_node;
37912   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37913 
37914   OMP_SINGLE_CLAUSES (stmt)
37915     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37916 				 "#pragma omp single", pragma_tok);
37917   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37918 
37919   return add_stmt (stmt);
37920 }
37921 
37922 /* OpenMP 3.0:
37923    # pragma omp task task-clause[optseq] new-line
37924      structured-block  */
37925 
37926 #define OMP_TASK_CLAUSE_MASK					\
37927 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
37928 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
37929 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
37930 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
37931 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
37932 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
37933 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
37934 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
37935 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
37936 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
37937 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37938 
37939 static tree
cp_parser_omp_task(cp_parser * parser,cp_token * pragma_tok,bool * if_p)37940 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37941 {
37942   tree clauses, block;
37943   unsigned int save;
37944 
37945   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37946 				       "#pragma omp task", pragma_tok);
37947   block = begin_omp_task ();
37948   save = cp_parser_begin_omp_structured_block (parser);
37949   cp_parser_statement (parser, NULL_TREE, false, if_p);
37950   cp_parser_end_omp_structured_block (parser, save);
37951   return finish_omp_task (clauses, block);
37952 }
37953 
37954 /* OpenMP 3.0:
37955    # pragma omp taskwait new-line
37956 
37957    OpenMP 5.0:
37958    # pragma omp taskwait taskwait-clause[opt] new-line  */
37959 
37960 #define OMP_TASKWAIT_CLAUSE_MASK				\
37961 	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37962 
37963 static void
cp_parser_omp_taskwait(cp_parser * parser,cp_token * pragma_tok)37964 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37965 {
37966   tree clauses
37967     = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37968 				 "#pragma omp taskwait", pragma_tok);
37969 
37970   if (clauses)
37971     {
37972       tree stmt = make_node (OMP_TASK);
37973       TREE_TYPE (stmt) = void_node;
37974       OMP_TASK_CLAUSES (stmt) = clauses;
37975       OMP_TASK_BODY (stmt) = NULL_TREE;
37976       SET_EXPR_LOCATION (stmt, pragma_tok->location);
37977       add_stmt (stmt);
37978     }
37979   else
37980     finish_omp_taskwait ();
37981 }
37982 
37983 /* OpenMP 3.1:
37984    # pragma omp taskyield new-line  */
37985 
37986 static void
cp_parser_omp_taskyield(cp_parser * parser,cp_token * pragma_tok)37987 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37988 {
37989   cp_parser_require_pragma_eol (parser, pragma_tok);
37990   finish_omp_taskyield ();
37991 }
37992 
37993 /* OpenMP 4.0:
37994    # pragma omp taskgroup new-line
37995      structured-block
37996 
37997    OpenMP 5.0:
37998    # pragma omp taskgroup taskgroup-clause[optseq] new-line  */
37999 
38000 #define OMP_TASKGROUP_CLAUSE_MASK				\
38001 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
38002 
38003 static tree
cp_parser_omp_taskgroup(cp_parser * parser,cp_token * pragma_tok,bool * if_p)38004 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38005 {
38006   tree clauses
38007     = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
38008 				 "#pragma omp taskgroup", pragma_tok);
38009   return c_finish_omp_taskgroup (input_location,
38010 				 cp_parser_omp_structured_block (parser,
38011 								 if_p),
38012 				 clauses);
38013 }
38014 
38015 
38016 /* OpenMP 2.5:
38017    # pragma omp threadprivate (variable-list) */
38018 
38019 static void
cp_parser_omp_threadprivate(cp_parser * parser,cp_token * pragma_tok)38020 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
38021 {
38022   tree vars;
38023 
38024   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38025   cp_parser_require_pragma_eol (parser, pragma_tok);
38026 
38027   finish_omp_threadprivate (vars);
38028 }
38029 
38030 /* OpenMP 4.0:
38031    # pragma omp cancel cancel-clause[optseq] new-line  */
38032 
38033 #define OMP_CANCEL_CLAUSE_MASK					\
38034 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
38035 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
38036 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
38037 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)	\
38038 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
38039 
38040 static void
cp_parser_omp_cancel(cp_parser * parser,cp_token * pragma_tok)38041 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
38042 {
38043   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
38044 					    "#pragma omp cancel", pragma_tok);
38045   finish_omp_cancel (clauses);
38046 }
38047 
38048 /* OpenMP 4.0:
38049    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
38050 
38051 #define OMP_CANCELLATION_POINT_CLAUSE_MASK			\
38052 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
38053 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
38054 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
38055 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
38056 
38057 static void
cp_parser_omp_cancellation_point(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)38058 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
38059 				  enum pragma_context context)
38060 {
38061   tree clauses;
38062   bool point_seen = false;
38063 
38064   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38065     {
38066       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38067       const char *p = IDENTIFIER_POINTER (id);
38068 
38069       if (strcmp (p, "point") == 0)
38070 	{
38071 	  cp_lexer_consume_token (parser->lexer);
38072 	  point_seen = true;
38073 	}
38074     }
38075   if (!point_seen)
38076     {
38077       cp_parser_error (parser, "expected %<point%>");
38078       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38079       return;
38080     }
38081 
38082   if (context != pragma_compound)
38083     {
38084       if (context == pragma_stmt)
38085 	error_at (pragma_tok->location,
38086 		  "%<#pragma %s%> may only be used in compound statements",
38087 		  "omp cancellation point");
38088       else
38089 	cp_parser_error (parser, "expected declaration specifiers");
38090       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38091       return;
38092     }
38093 
38094   clauses = cp_parser_omp_all_clauses (parser,
38095 				       OMP_CANCELLATION_POINT_CLAUSE_MASK,
38096 				       "#pragma omp cancellation point",
38097 				       pragma_tok);
38098   finish_omp_cancellation_point (clauses);
38099 }
38100 
38101 /* OpenMP 4.0:
38102    #pragma omp distribute distribute-clause[optseq] new-line
38103      for-loop  */
38104 
38105 #define OMP_DISTRIBUTE_CLAUSE_MASK				\
38106 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
38107 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
38108 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
38109 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
38110 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
38111 
38112 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)38113 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
38114 			  char *p_name, omp_clause_mask mask, tree *cclauses,
38115 			  bool *if_p)
38116 {
38117   tree clauses, sb, ret;
38118   unsigned int save;
38119   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38120 
38121   strcat (p_name, " distribute");
38122   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
38123 
38124   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38125     {
38126       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38127       const char *p = IDENTIFIER_POINTER (id);
38128       bool simd = false;
38129       bool parallel = false;
38130 
38131       if (strcmp (p, "simd") == 0)
38132 	simd = true;
38133       else
38134 	parallel = strcmp (p, "parallel") == 0;
38135       if (parallel || simd)
38136 	{
38137 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38138 	  if (cclauses == NULL)
38139 	    cclauses = cclauses_buf;
38140 	  cp_lexer_consume_token (parser->lexer);
38141 	  if (!flag_openmp)  /* flag_openmp_simd  */
38142 	    {
38143 	      if (simd)
38144 		return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38145 					   cclauses, if_p);
38146 	      else
38147 		return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38148 					       cclauses, if_p);
38149 	    }
38150 	  sb = begin_omp_structured_block ();
38151 	  save = cp_parser_begin_omp_structured_block (parser);
38152 	  if (simd)
38153 	    ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38154 				      cclauses, if_p);
38155 	  else
38156 	    ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
38157 					  cclauses, if_p);
38158 	  cp_parser_end_omp_structured_block (parser, save);
38159 	  tree body = finish_omp_structured_block (sb);
38160 	  if (ret == NULL)
38161 	    return ret;
38162 	  ret = make_node (OMP_DISTRIBUTE);
38163 	  TREE_TYPE (ret) = void_type_node;
38164 	  OMP_FOR_BODY (ret) = body;
38165 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38166 	  SET_EXPR_LOCATION (ret, loc);
38167 	  add_stmt (ret);
38168 	  return ret;
38169 	}
38170     }
38171   if (!flag_openmp)  /* flag_openmp_simd  */
38172     {
38173       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38174       return NULL_TREE;
38175     }
38176 
38177   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38178 				       cclauses == NULL);
38179   if (cclauses)
38180     {
38181       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
38182       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
38183     }
38184 
38185   keep_next_level (true);
38186   sb = begin_omp_structured_block ();
38187   save = cp_parser_begin_omp_structured_block (parser);
38188 
38189   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
38190 
38191   cp_parser_end_omp_structured_block (parser, save);
38192   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
38193 
38194   return ret;
38195 }
38196 
38197 /* OpenMP 4.0:
38198    # pragma omp teams teams-clause[optseq] new-line
38199      structured-block  */
38200 
38201 #define OMP_TEAMS_CLAUSE_MASK					\
38202 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
38203 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
38204 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
38205 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
38206 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)	\
38207 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
38208 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
38209 
38210 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)38211 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
38212 		     char *p_name, omp_clause_mask mask, tree *cclauses,
38213 		     bool *if_p)
38214 {
38215   tree clauses, sb, ret;
38216   unsigned int save;
38217   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38218 
38219   strcat (p_name, " teams");
38220   mask |= OMP_TEAMS_CLAUSE_MASK;
38221 
38222   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38223     {
38224       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38225       const char *p = IDENTIFIER_POINTER (id);
38226       if (strcmp (p, "distribute") == 0)
38227 	{
38228 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38229 	  if (cclauses == NULL)
38230 	    cclauses = cclauses_buf;
38231 
38232 	  cp_lexer_consume_token (parser->lexer);
38233 	  if (!flag_openmp)  /* flag_openmp_simd  */
38234 	    return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38235 					     cclauses, if_p);
38236 	  keep_next_level (true);
38237 	  sb = begin_omp_structured_block ();
38238 	  save = cp_parser_begin_omp_structured_block (parser);
38239 	  ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
38240 					  cclauses, if_p);
38241 	  cp_parser_end_omp_structured_block (parser, save);
38242 	  tree body = finish_omp_structured_block (sb);
38243 	  if (ret == NULL)
38244 	    return ret;
38245 	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38246 	  ret = make_node (OMP_TEAMS);
38247 	  TREE_TYPE (ret) = void_type_node;
38248 	  OMP_TEAMS_CLAUSES (ret) = clauses;
38249 	  OMP_TEAMS_BODY (ret) = body;
38250 	  OMP_TEAMS_COMBINED (ret) = 1;
38251 	  SET_EXPR_LOCATION (ret, loc);
38252 	  return add_stmt (ret);
38253 	}
38254     }
38255   if (!flag_openmp)  /* flag_openmp_simd  */
38256     {
38257       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38258       return NULL_TREE;
38259     }
38260 
38261   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38262 				       cclauses == NULL);
38263   if (cclauses)
38264     {
38265       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38266       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38267     }
38268 
38269   tree stmt = make_node (OMP_TEAMS);
38270   TREE_TYPE (stmt) = void_type_node;
38271   OMP_TEAMS_CLAUSES (stmt) = clauses;
38272   keep_next_level (true);
38273   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38274   SET_EXPR_LOCATION (stmt, loc);
38275 
38276   return add_stmt (stmt);
38277 }
38278 
38279 /* OpenMP 4.0:
38280    # pragma omp target data target-data-clause[optseq] new-line
38281      structured-block  */
38282 
38283 #define OMP_TARGET_DATA_CLAUSE_MASK				\
38284 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
38285 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
38286 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
38287 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38288 
38289 static tree
cp_parser_omp_target_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)38290 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38291 {
38292   tree clauses
38293     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38294 				 "#pragma omp target data", pragma_tok);
38295   int map_seen = 0;
38296   for (tree *pc = &clauses; *pc;)
38297     {
38298       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38299 	switch (OMP_CLAUSE_MAP_KIND (*pc))
38300 	  {
38301 	  case GOMP_MAP_TO:
38302 	  case GOMP_MAP_ALWAYS_TO:
38303 	  case GOMP_MAP_FROM:
38304 	  case GOMP_MAP_ALWAYS_FROM:
38305 	  case GOMP_MAP_TOFROM:
38306 	  case GOMP_MAP_ALWAYS_TOFROM:
38307 	  case GOMP_MAP_ALLOC:
38308 	    map_seen = 3;
38309 	    break;
38310 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
38311 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38312 	  case GOMP_MAP_ALWAYS_POINTER:
38313 	    break;
38314 	  default:
38315 	    map_seen |= 1;
38316 	    error_at (OMP_CLAUSE_LOCATION (*pc),
38317 		      "%<#pragma omp target data%> with map-type other "
38318 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38319 		      "on %<map%> clause");
38320 	    *pc = OMP_CLAUSE_CHAIN (*pc);
38321 	    continue;
38322 	  }
38323       else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38324 	map_seen = 3;
38325       pc = &OMP_CLAUSE_CHAIN (*pc);
38326     }
38327 
38328   if (map_seen != 3)
38329     {
38330       if (map_seen == 0)
38331 	error_at (pragma_tok->location,
38332 		  "%<#pragma omp target data%> must contain at least "
38333 		  "one %<map%> or %<use_device_ptr%> clause");
38334       return NULL_TREE;
38335     }
38336 
38337   tree stmt = make_node (OMP_TARGET_DATA);
38338   TREE_TYPE (stmt) = void_type_node;
38339   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38340 
38341   keep_next_level (true);
38342   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38343 
38344   SET_EXPR_LOCATION (stmt, pragma_tok->location);
38345   return add_stmt (stmt);
38346 }
38347 
38348 /* OpenMP 4.5:
38349    # pragma omp target enter data target-enter-data-clause[optseq] new-line
38350      structured-block  */
38351 
38352 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK			\
38353 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
38354 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
38355 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
38356 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
38357 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38358 
38359 static tree
cp_parser_omp_target_enter_data(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)38360 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38361 				 enum pragma_context context)
38362 {
38363   bool data_seen = false;
38364   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38365     {
38366       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38367       const char *p = IDENTIFIER_POINTER (id);
38368 
38369       if (strcmp (p, "data") == 0)
38370 	{
38371 	  cp_lexer_consume_token (parser->lexer);
38372 	  data_seen = true;
38373 	}
38374     }
38375   if (!data_seen)
38376     {
38377       cp_parser_error (parser, "expected %<data%>");
38378       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38379       return NULL_TREE;
38380     }
38381 
38382   if (context == pragma_stmt)
38383     {
38384       error_at (pragma_tok->location,
38385 		"%<#pragma %s%> may only be used in compound statements",
38386 		"omp target enter data");
38387       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38388       return NULL_TREE;
38389     }
38390 
38391   tree clauses
38392     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38393 				 "#pragma omp target enter data", pragma_tok);
38394   int map_seen = 0;
38395   for (tree *pc = &clauses; *pc;)
38396     {
38397       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38398 	switch (OMP_CLAUSE_MAP_KIND (*pc))
38399 	  {
38400 	  case GOMP_MAP_TO:
38401 	  case GOMP_MAP_ALWAYS_TO:
38402 	  case GOMP_MAP_ALLOC:
38403 	    map_seen = 3;
38404 	    break;
38405 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
38406 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38407 	  case GOMP_MAP_ALWAYS_POINTER:
38408 	    break;
38409 	  default:
38410 	    map_seen |= 1;
38411 	    error_at (OMP_CLAUSE_LOCATION (*pc),
38412 		      "%<#pragma omp target enter data%> with map-type other "
38413 		      "than %<to%> or %<alloc%> on %<map%> clause");
38414 	    *pc = OMP_CLAUSE_CHAIN (*pc);
38415 	    continue;
38416 	  }
38417       pc = &OMP_CLAUSE_CHAIN (*pc);
38418     }
38419 
38420   if (map_seen != 3)
38421     {
38422       if (map_seen == 0)
38423 	error_at (pragma_tok->location,
38424 		  "%<#pragma omp target enter data%> must contain at least "
38425 		  "one %<map%> clause");
38426       return NULL_TREE;
38427     }
38428 
38429   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38430   TREE_TYPE (stmt) = void_type_node;
38431   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38432   SET_EXPR_LOCATION (stmt, pragma_tok->location);
38433   return add_stmt (stmt);
38434 }
38435 
38436 /* OpenMP 4.5:
38437    # pragma omp target exit data target-enter-data-clause[optseq] new-line
38438      structured-block  */
38439 
38440 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK			\
38441 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
38442 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
38443 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
38444 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
38445 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38446 
38447 static tree
cp_parser_omp_target_exit_data(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)38448 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38449 				enum pragma_context context)
38450 {
38451   bool data_seen = false;
38452   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38453     {
38454       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38455       const char *p = IDENTIFIER_POINTER (id);
38456 
38457       if (strcmp (p, "data") == 0)
38458 	{
38459 	  cp_lexer_consume_token (parser->lexer);
38460 	  data_seen = true;
38461 	}
38462     }
38463   if (!data_seen)
38464     {
38465       cp_parser_error (parser, "expected %<data%>");
38466       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38467       return NULL_TREE;
38468     }
38469 
38470   if (context == pragma_stmt)
38471     {
38472       error_at (pragma_tok->location,
38473 		"%<#pragma %s%> may only be used in compound statements",
38474 		"omp target exit data");
38475       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38476       return NULL_TREE;
38477     }
38478 
38479   tree clauses
38480     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38481 				 "#pragma omp target exit data", pragma_tok);
38482   int map_seen = 0;
38483   for (tree *pc = &clauses; *pc;)
38484     {
38485       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38486 	switch (OMP_CLAUSE_MAP_KIND (*pc))
38487 	  {
38488 	  case GOMP_MAP_FROM:
38489 	  case GOMP_MAP_ALWAYS_FROM:
38490 	  case GOMP_MAP_RELEASE:
38491 	  case GOMP_MAP_DELETE:
38492 	    map_seen = 3;
38493 	    break;
38494 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
38495 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38496 	  case GOMP_MAP_ALWAYS_POINTER:
38497 	    break;
38498 	  default:
38499 	    map_seen |= 1;
38500 	    error_at (OMP_CLAUSE_LOCATION (*pc),
38501 		      "%<#pragma omp target exit data%> with map-type other "
38502 		      "than %<from%>, %<release%> or %<delete%> on %<map%>"
38503 		      " clause");
38504 	    *pc = OMP_CLAUSE_CHAIN (*pc);
38505 	    continue;
38506 	  }
38507       pc = &OMP_CLAUSE_CHAIN (*pc);
38508     }
38509 
38510   if (map_seen != 3)
38511     {
38512       if (map_seen == 0)
38513 	error_at (pragma_tok->location,
38514 		  "%<#pragma omp target exit data%> must contain at least "
38515 		  "one %<map%> clause");
38516       return NULL_TREE;
38517     }
38518 
38519   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38520   TREE_TYPE (stmt) = void_type_node;
38521   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38522   SET_EXPR_LOCATION (stmt, pragma_tok->location);
38523   return add_stmt (stmt);
38524 }
38525 
38526 /* OpenMP 4.0:
38527    # pragma omp target update target-update-clause[optseq] new-line */
38528 
38529 #define OMP_TARGET_UPDATE_CLAUSE_MASK				\
38530 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)		\
38531 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
38532 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
38533 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
38534 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
38535 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38536 
38537 static bool
cp_parser_omp_target_update(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)38538 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38539 			     enum pragma_context context)
38540 {
38541   if (context == pragma_stmt)
38542     {
38543       error_at (pragma_tok->location,
38544 		"%<#pragma %s%> may only be used in compound statements",
38545 		"omp target update");
38546       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38547       return false;
38548     }
38549 
38550   tree clauses
38551     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38552 				 "#pragma omp target update", pragma_tok);
38553   if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38554       && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38555     {
38556       error_at (pragma_tok->location,
38557 		"%<#pragma omp target update%> must contain at least one "
38558 		"%<from%> or %<to%> clauses");
38559       return false;
38560     }
38561 
38562   tree stmt = make_node (OMP_TARGET_UPDATE);
38563   TREE_TYPE (stmt) = void_type_node;
38564   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38565   SET_EXPR_LOCATION (stmt, pragma_tok->location);
38566   add_stmt (stmt);
38567   return false;
38568 }
38569 
38570 /* OpenMP 4.0:
38571    # pragma omp target target-clause[optseq] new-line
38572      structured-block  */
38573 
38574 #define OMP_TARGET_CLAUSE_MASK					\
38575 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
38576 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
38577 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
38578 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
38579 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
38580 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
38581 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
38582 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)	\
38583 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38584 
38585 static bool
cp_parser_omp_target(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context,bool * if_p)38586 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38587 		      enum pragma_context context, bool *if_p)
38588 {
38589   tree *pc = NULL, stmt;
38590 
38591   if (flag_openmp)
38592     omp_requires_mask
38593       = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38594 
38595   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38596     {
38597       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38598       const char *p = IDENTIFIER_POINTER (id);
38599       enum tree_code ccode = ERROR_MARK;
38600 
38601       if (strcmp (p, "teams") == 0)
38602 	ccode = OMP_TEAMS;
38603       else if (strcmp (p, "parallel") == 0)
38604 	ccode = OMP_PARALLEL;
38605       else if (strcmp (p, "simd") == 0)
38606 	ccode = OMP_SIMD;
38607       if (ccode != ERROR_MARK)
38608 	{
38609 	  tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38610 	  char p_name[sizeof ("#pragma omp target teams distribute "
38611 			      "parallel for simd")];
38612 
38613 	  cp_lexer_consume_token (parser->lexer);
38614 	  strcpy (p_name, "#pragma omp target");
38615 	  if (!flag_openmp)  /* flag_openmp_simd  */
38616 	    {
38617 	      tree stmt;
38618 	      switch (ccode)
38619 		{
38620 		case OMP_TEAMS:
38621 		  stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38622 					      OMP_TARGET_CLAUSE_MASK,
38623 					      cclauses, if_p);
38624 		  break;
38625 		case OMP_PARALLEL:
38626 		  stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38627 						 OMP_TARGET_CLAUSE_MASK,
38628 						 cclauses, if_p);
38629 		  break;
38630 		case OMP_SIMD:
38631 		  stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38632 					     OMP_TARGET_CLAUSE_MASK,
38633 					     cclauses, if_p);
38634 		  break;
38635 		default:
38636 		  gcc_unreachable ();
38637 		}
38638 	      return stmt != NULL_TREE;
38639 	    }
38640 	  keep_next_level (true);
38641 	  tree sb = begin_omp_structured_block (), ret;
38642 	  unsigned save = cp_parser_begin_omp_structured_block (parser);
38643 	  switch (ccode)
38644 	    {
38645 	    case OMP_TEAMS:
38646 	      ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38647 					 OMP_TARGET_CLAUSE_MASK, cclauses,
38648 					 if_p);
38649 	      break;
38650 	    case OMP_PARALLEL:
38651 	      ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38652 					    OMP_TARGET_CLAUSE_MASK, cclauses,
38653 					    if_p);
38654 	      break;
38655 	    case OMP_SIMD:
38656 	      ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38657 					OMP_TARGET_CLAUSE_MASK, cclauses,
38658 					if_p);
38659 	      break;
38660 	    default:
38661 	      gcc_unreachable ();
38662 	    }
38663 	  cp_parser_end_omp_structured_block (parser, save);
38664 	  tree body = finish_omp_structured_block (sb);
38665 	  if (ret == NULL_TREE)
38666 	    return false;
38667 	  if (ccode == OMP_TEAMS && !processing_template_decl)
38668 	    {
38669 	      /* For combined target teams, ensure the num_teams and
38670 		 thread_limit clause expressions are evaluated on the host,
38671 		 before entering the target construct.  */
38672 	      tree c;
38673 	      for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38674 		   c; c = OMP_CLAUSE_CHAIN (c))
38675 		if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38676 		     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38677 		    && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38678 		  {
38679 		    tree expr = OMP_CLAUSE_OPERAND (c, 0);
38680 		    expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38681 		    if (expr == error_mark_node)
38682 		      continue;
38683 		    tree tmp = TARGET_EXPR_SLOT (expr);
38684 		    add_stmt (expr);
38685 		    OMP_CLAUSE_OPERAND (c, 0) = expr;
38686 		    tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38687 						OMP_CLAUSE_FIRSTPRIVATE);
38688 		    OMP_CLAUSE_DECL (tc) = tmp;
38689 		    OMP_CLAUSE_CHAIN (tc)
38690 		      = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38691 		    cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38692 		  }
38693 	    }
38694 	  tree stmt = make_node (OMP_TARGET);
38695 	  TREE_TYPE (stmt) = void_type_node;
38696 	  OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38697 	  OMP_TARGET_BODY (stmt) = body;
38698 	  OMP_TARGET_COMBINED (stmt) = 1;
38699 	  SET_EXPR_LOCATION (stmt, pragma_tok->location);
38700 	  add_stmt (stmt);
38701 	  pc = &OMP_TARGET_CLAUSES (stmt);
38702 	  goto check_clauses;
38703 	}
38704       else if (!flag_openmp)  /* flag_openmp_simd  */
38705 	{
38706 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38707 	  return false;
38708 	}
38709       else if (strcmp (p, "data") == 0)
38710 	{
38711 	  cp_lexer_consume_token (parser->lexer);
38712 	  cp_parser_omp_target_data (parser, pragma_tok, if_p);
38713 	  return true;
38714 	}
38715       else if (strcmp (p, "enter") == 0)
38716 	{
38717 	  cp_lexer_consume_token (parser->lexer);
38718 	  cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38719 	  return false;
38720 	}
38721       else if (strcmp (p, "exit") == 0)
38722 	{
38723 	  cp_lexer_consume_token (parser->lexer);
38724 	  cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38725 	  return false;
38726 	}
38727       else if (strcmp (p, "update") == 0)
38728 	{
38729 	  cp_lexer_consume_token (parser->lexer);
38730 	  return cp_parser_omp_target_update (parser, pragma_tok, context);
38731 	}
38732     }
38733   if (!flag_openmp)  /* flag_openmp_simd  */
38734     {
38735       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38736       return false;
38737     }
38738 
38739   stmt = make_node (OMP_TARGET);
38740   TREE_TYPE (stmt) = void_type_node;
38741 
38742   OMP_TARGET_CLAUSES (stmt)
38743     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38744 				 "#pragma omp target", pragma_tok);
38745   pc = &OMP_TARGET_CLAUSES (stmt);
38746   keep_next_level (true);
38747   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38748 
38749   SET_EXPR_LOCATION (stmt, pragma_tok->location);
38750   add_stmt (stmt);
38751 
38752 check_clauses:
38753   while (*pc)
38754     {
38755       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38756 	switch (OMP_CLAUSE_MAP_KIND (*pc))
38757 	  {
38758 	  case GOMP_MAP_TO:
38759 	  case GOMP_MAP_ALWAYS_TO:
38760 	  case GOMP_MAP_FROM:
38761 	  case GOMP_MAP_ALWAYS_FROM:
38762 	  case GOMP_MAP_TOFROM:
38763 	  case GOMP_MAP_ALWAYS_TOFROM:
38764 	  case GOMP_MAP_ALLOC:
38765 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
38766 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38767 	  case GOMP_MAP_ALWAYS_POINTER:
38768 	    break;
38769 	  default:
38770 	    error_at (OMP_CLAUSE_LOCATION (*pc),
38771 		      "%<#pragma omp target%> with map-type other "
38772 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38773 		      "on %<map%> clause");
38774 	    *pc = OMP_CLAUSE_CHAIN (*pc);
38775 	    continue;
38776 	  }
38777       pc = &OMP_CLAUSE_CHAIN (*pc);
38778     }
38779   return true;
38780 }
38781 
38782 /* OpenACC 2.0:
38783    # pragma acc cache (variable-list) new-line
38784 */
38785 
38786 static tree
cp_parser_oacc_cache(cp_parser * parser,cp_token * pragma_tok)38787 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38788 {
38789   /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
38790      clauses.  */
38791   auto_suppress_location_wrappers sentinel;
38792 
38793   tree stmt, clauses;
38794 
38795   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38796   clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38797 
38798   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38799 
38800   stmt = make_node (OACC_CACHE);
38801   TREE_TYPE (stmt) = void_type_node;
38802   OACC_CACHE_CLAUSES (stmt) = clauses;
38803   SET_EXPR_LOCATION (stmt, pragma_tok->location);
38804   add_stmt (stmt);
38805 
38806   return stmt;
38807 }
38808 
38809 /* OpenACC 2.0:
38810    # pragma acc data oacc-data-clause[optseq] new-line
38811      structured-block  */
38812 
38813 #define OACC_DATA_CLAUSE_MASK						\
38814 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
38815 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
38816 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
38817 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
38818 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
38819 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
38820 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38821 
38822 static tree
cp_parser_oacc_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)38823 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38824 {
38825   tree stmt, clauses, block;
38826   unsigned int save;
38827 
38828   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38829 					"#pragma acc data", pragma_tok);
38830 
38831   block = begin_omp_parallel ();
38832   save = cp_parser_begin_omp_structured_block (parser);
38833   cp_parser_statement (parser, NULL_TREE, false, if_p);
38834   cp_parser_end_omp_structured_block (parser, save);
38835   stmt = finish_oacc_data (clauses, block);
38836   return stmt;
38837 }
38838 
38839 /* OpenACC 2.0:
38840   # pragma acc host_data <clauses> new-line
38841   structured-block  */
38842 
38843 #define OACC_HOST_DATA_CLAUSE_MASK					\
38844   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38845 
38846 static tree
cp_parser_oacc_host_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)38847 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38848 {
38849   tree stmt, clauses, block;
38850   unsigned int save;
38851 
38852   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38853 					"#pragma acc host_data", pragma_tok);
38854 
38855   block = begin_omp_parallel ();
38856   save = cp_parser_begin_omp_structured_block (parser);
38857   cp_parser_statement (parser, NULL_TREE, false, if_p);
38858   cp_parser_end_omp_structured_block (parser, save);
38859   stmt = finish_oacc_host_data (clauses, block);
38860   return stmt;
38861 }
38862 
38863 /* OpenACC 2.0:
38864    # pragma acc declare oacc-data-clause[optseq] new-line
38865 */
38866 
38867 #define OACC_DECLARE_CLAUSE_MASK					\
38868 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
38869 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
38870 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
38871 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
38872 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
38873 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)	\
38874 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)		\
38875 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38876 
38877 static tree
cp_parser_oacc_declare(cp_parser * parser,cp_token * pragma_tok)38878 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38879 {
38880   tree clauses, stmt;
38881   bool error = false;
38882 
38883   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38884 					"#pragma acc declare", pragma_tok, true);
38885 
38886 
38887   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38888     {
38889       error_at (pragma_tok->location,
38890 		"no valid clauses specified in %<#pragma acc declare%>");
38891       return NULL_TREE;
38892     }
38893 
38894   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38895     {
38896       location_t loc = OMP_CLAUSE_LOCATION (t);
38897       tree decl = OMP_CLAUSE_DECL (t);
38898       if (!DECL_P (decl))
38899 	{
38900 	  error_at (loc, "array section in %<#pragma acc declare%>");
38901 	  error = true;
38902 	  continue;
38903 	}
38904       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38905       switch (OMP_CLAUSE_MAP_KIND (t))
38906 	{
38907 	case GOMP_MAP_FIRSTPRIVATE_POINTER:
38908 	case GOMP_MAP_ALLOC:
38909 	case GOMP_MAP_TO:
38910 	case GOMP_MAP_FORCE_DEVICEPTR:
38911 	case GOMP_MAP_DEVICE_RESIDENT:
38912 	  break;
38913 
38914 	case GOMP_MAP_LINK:
38915 	  if (!global_bindings_p ()
38916 	      && (TREE_STATIC (decl)
38917 	       || !DECL_EXTERNAL (decl)))
38918 	    {
38919 	      error_at (loc,
38920 			"%qD must be a global variable in "
38921 			"%<#pragma acc declare link%>",
38922 			decl);
38923 	      error = true;
38924 	      continue;
38925 	    }
38926 	  break;
38927 
38928 	default:
38929 	  if (global_bindings_p ())
38930 	    {
38931 	      error_at (loc, "invalid OpenACC clause at file scope");
38932 	      error = true;
38933 	      continue;
38934 	    }
38935 	  if (DECL_EXTERNAL (decl))
38936 	    {
38937 	      error_at (loc,
38938 			"invalid use of %<extern%> variable %qD "
38939 			"in %<#pragma acc declare%>", decl);
38940 	      error = true;
38941 	      continue;
38942 	    }
38943 	  else if (TREE_PUBLIC (decl))
38944 	    {
38945 	      error_at (loc,
38946 			"invalid use of %<global%> variable %qD "
38947 			"in %<#pragma acc declare%>", decl);
38948 	      error = true;
38949 	      continue;
38950 	    }
38951 	  break;
38952 	}
38953 
38954       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38955 	  || lookup_attribute ("omp declare target link",
38956 			       DECL_ATTRIBUTES (decl)))
38957 	{
38958 	  error_at (loc, "variable %qD used more than once with "
38959 		    "%<#pragma acc declare%>", decl);
38960 	  error = true;
38961 	  continue;
38962 	}
38963 
38964       if (!error)
38965 	{
38966 	  tree id;
38967 
38968 	  if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38969 	    id = get_identifier ("omp declare target link");
38970 	  else
38971 	    id = get_identifier ("omp declare target");
38972 
38973 	  DECL_ATTRIBUTES (decl)
38974 	    = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38975 	  if (global_bindings_p ())
38976 	    {
38977 	      symtab_node *node = symtab_node::get (decl);
38978 	      if (node != NULL)
38979 		{
38980 		  node->offloadable = 1;
38981 		  if (ENABLE_OFFLOADING)
38982 		    {
38983 		      g->have_offload = true;
38984 		      if (is_a <varpool_node *> (node))
38985 			vec_safe_push (offload_vars, decl);
38986 		    }
38987 		}
38988 	    }
38989 	}
38990     }
38991 
38992   if (error || global_bindings_p ())
38993     return NULL_TREE;
38994 
38995   stmt = make_node (OACC_DECLARE);
38996   TREE_TYPE (stmt) = void_type_node;
38997   OACC_DECLARE_CLAUSES (stmt) = clauses;
38998   SET_EXPR_LOCATION (stmt, pragma_tok->location);
38999 
39000   add_stmt (stmt);
39001 
39002   return NULL_TREE;
39003 }
39004 
39005 /* OpenACC 2.0:
39006    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
39007 
39008    or
39009 
39010    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
39011 
39012    LOC is the location of the #pragma token.
39013 */
39014 
39015 #define OACC_ENTER_DATA_CLAUSE_MASK					\
39016 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
39017 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
39018 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
39019 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
39020 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39021 
39022 #define OACC_EXIT_DATA_CLAUSE_MASK					\
39023 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
39024 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
39025 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
39026 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) 		\
39027 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) 		\
39028 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39029 
39030 static tree
cp_parser_oacc_enter_exit_data(cp_parser * parser,cp_token * pragma_tok,bool enter)39031 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
39032 				bool enter)
39033 {
39034   location_t loc = pragma_tok->location;
39035   tree stmt, clauses;
39036   const char *p = "";
39037 
39038   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39039     p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39040 
39041   if (strcmp (p, "data") != 0)
39042     {
39043       error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
39044 		enter ? "enter" : "exit");
39045       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39046       return NULL_TREE;
39047     }
39048 
39049   cp_lexer_consume_token (parser->lexer);
39050 
39051   if (enter)
39052     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
39053 					 "#pragma acc enter data", pragma_tok);
39054   else
39055     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
39056 					 "#pragma acc exit data", pragma_tok);
39057 
39058   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39059     {
39060       error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
39061 		enter ? "enter" : "exit");
39062       return NULL_TREE;
39063     }
39064 
39065   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
39066   TREE_TYPE (stmt) = void_type_node;
39067   OMP_STANDALONE_CLAUSES (stmt) = clauses;
39068   SET_EXPR_LOCATION (stmt, loc);
39069   add_stmt (stmt);
39070   return stmt;
39071 }
39072 
39073 /* OpenACC 2.0:
39074    # pragma acc loop oacc-loop-clause[optseq] new-line
39075      structured-block  */
39076 
39077 #define OACC_LOOP_CLAUSE_MASK						\
39078 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)		\
39079 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
39080 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
39081 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
39082 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
39083 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
39084 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)		\
39085 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)		\
39086 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
39087 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
39088 
39089 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)39090 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
39091 		     omp_clause_mask mask, tree *cclauses, bool *if_p)
39092 {
39093   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
39094 
39095   strcat (p_name, " loop");
39096   mask |= OACC_LOOP_CLAUSE_MASK;
39097 
39098   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
39099 					     cclauses == NULL);
39100   if (cclauses)
39101     {
39102       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
39103       if (*cclauses)
39104 	*cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
39105       if (clauses)
39106 	clauses = finish_omp_clauses (clauses, C_ORT_ACC);
39107     }
39108 
39109   tree block = begin_omp_structured_block ();
39110   int save = cp_parser_begin_omp_structured_block (parser);
39111   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
39112   cp_parser_end_omp_structured_block (parser, save);
39113   add_stmt (finish_omp_structured_block (block));
39114 
39115   return stmt;
39116 }
39117 
39118 /* OpenACC 2.0:
39119    # pragma acc kernels oacc-kernels-clause[optseq] new-line
39120      structured-block
39121 
39122    or
39123 
39124    # pragma acc parallel oacc-parallel-clause[optseq] new-line
39125      structured-block
39126 */
39127 
39128 #define OACC_KERNELS_CLAUSE_MASK					\
39129 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
39130 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
39131 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
39132 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
39133 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
39134 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
39135 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
39136 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
39137 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
39138 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
39139 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
39140 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)	\
39141 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39142 
39143 #define OACC_PARALLEL_CLAUSE_MASK					\
39144 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
39145 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
39146 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
39147 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
39148 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
39149 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
39150 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
39151 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)       	\
39152 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
39153 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
39154 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
39155 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
39156 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
39157 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
39158 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
39159 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
39160 
39161 static tree
cp_parser_oacc_kernels_parallel(cp_parser * parser,cp_token * pragma_tok,char * p_name,bool * if_p)39162 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
39163 				 char *p_name, bool *if_p)
39164 {
39165   omp_clause_mask mask;
39166   enum tree_code code;
39167   switch (cp_parser_pragma_kind (pragma_tok))
39168     {
39169     case PRAGMA_OACC_KERNELS:
39170       strcat (p_name, " kernels");
39171       mask = OACC_KERNELS_CLAUSE_MASK;
39172       code = OACC_KERNELS;
39173       break;
39174     case PRAGMA_OACC_PARALLEL:
39175       strcat (p_name, " parallel");
39176       mask = OACC_PARALLEL_CLAUSE_MASK;
39177       code = OACC_PARALLEL;
39178       break;
39179     default:
39180       gcc_unreachable ();
39181     }
39182 
39183   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39184     {
39185       const char *p
39186 	= IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
39187       if (strcmp (p, "loop") == 0)
39188 	{
39189 	  cp_lexer_consume_token (parser->lexer);
39190 	  tree block = begin_omp_parallel ();
39191 	  tree clauses;
39192 	  tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
39193 					   &clauses, if_p);
39194 	  protected_set_expr_location (stmt, pragma_tok->location);
39195 	  return finish_omp_construct (code, block, clauses);
39196 	}
39197     }
39198 
39199   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
39200 
39201   tree block = begin_omp_parallel ();
39202   unsigned int save = cp_parser_begin_omp_structured_block (parser);
39203   cp_parser_statement (parser, NULL_TREE, false, if_p);
39204   cp_parser_end_omp_structured_block (parser, save);
39205   return finish_omp_construct (code, block, clauses);
39206 }
39207 
39208 /* OpenACC 2.0:
39209    # pragma acc update oacc-update-clause[optseq] new-line
39210 */
39211 
39212 #define OACC_UPDATE_CLAUSE_MASK						\
39213 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
39214 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)		\
39215 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)		\
39216 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
39217 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT)		\
39218 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
39219 
39220 static tree
cp_parser_oacc_update(cp_parser * parser,cp_token * pragma_tok)39221 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
39222 {
39223   tree stmt, clauses;
39224 
39225   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
39226 					 "#pragma acc update", pragma_tok);
39227 
39228   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
39229     {
39230       error_at (pragma_tok->location,
39231 		"%<#pragma acc update%> must contain at least one "
39232 		"%<device%> or %<host%> or %<self%> clause");
39233       return NULL_TREE;
39234     }
39235 
39236   stmt = make_node (OACC_UPDATE);
39237   TREE_TYPE (stmt) = void_type_node;
39238   OACC_UPDATE_CLAUSES (stmt) = clauses;
39239   SET_EXPR_LOCATION (stmt, pragma_tok->location);
39240   add_stmt (stmt);
39241   return stmt;
39242 }
39243 
39244 /* OpenACC 2.0:
39245    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39246 
39247    LOC is the location of the #pragma token.
39248 */
39249 
39250 #define OACC_WAIT_CLAUSE_MASK					\
39251 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39252 
39253 static tree
cp_parser_oacc_wait(cp_parser * parser,cp_token * pragma_tok)39254 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39255 {
39256   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39257   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39258 
39259   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39260     list = cp_parser_oacc_wait_list (parser, loc, list);
39261 
39262   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39263 					"#pragma acc wait", pragma_tok);
39264 
39265   stmt = c_finish_oacc_wait (loc, list, clauses);
39266   stmt = finish_expr_stmt (stmt);
39267 
39268   return stmt;
39269 }
39270 
39271 /* OpenMP 4.0:
39272    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
39273 
39274 #define OMP_DECLARE_SIMD_CLAUSE_MASK				\
39275 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
39276 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
39277 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
39278 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)	\
39279 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)	\
39280 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39281 
39282 static void
cp_parser_omp_declare_simd(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)39283 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39284 			    enum pragma_context context)
39285 {
39286   bool first_p = parser->omp_declare_simd == NULL;
39287   cp_omp_declare_simd_data data;
39288   if (first_p)
39289     {
39290       data.error_seen = false;
39291       data.fndecl_seen = false;
39292       data.tokens = vNULL;
39293       data.clauses = NULL_TREE;
39294       /* It is safe to take the address of a local variable; it will only be
39295 	 used while this scope is live.  */
39296       parser->omp_declare_simd = &data;
39297     }
39298 
39299   /* Store away all pragma tokens.  */
39300   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39301 	 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39302     cp_lexer_consume_token (parser->lexer);
39303   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39304     parser->omp_declare_simd->error_seen = true;
39305   cp_parser_require_pragma_eol (parser, pragma_tok);
39306   struct cp_token_cache *cp
39307     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39308   parser->omp_declare_simd->tokens.safe_push (cp);
39309 
39310   if (first_p)
39311     {
39312       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39313 	cp_parser_pragma (parser, context, NULL);
39314       switch (context)
39315 	{
39316 	case pragma_external:
39317 	  cp_parser_declaration (parser);
39318 	  break;
39319 	case pragma_member:
39320 	  cp_parser_member_declaration (parser);
39321 	  break;
39322 	case pragma_objc_icode:
39323 	  cp_parser_block_declaration (parser, /*statement_p=*/false);
39324 	  break;
39325 	default:
39326 	  cp_parser_declaration_statement (parser);
39327 	  break;
39328 	}
39329       if (parser->omp_declare_simd
39330 	  && !parser->omp_declare_simd->error_seen
39331 	  && !parser->omp_declare_simd->fndecl_seen)
39332 	error_at (pragma_tok->location,
39333 		  "%<#pragma omp declare simd%> not immediately followed by "
39334 		  "function declaration or definition");
39335       data.tokens.release ();
39336       parser->omp_declare_simd = NULL;
39337     }
39338 }
39339 
39340 /* Finalize #pragma omp declare simd clauses after direct declarator has
39341    been parsed, and put that into "omp declare simd" attribute.  */
39342 
39343 static tree
cp_parser_late_parsing_omp_declare_simd(cp_parser * parser,tree attrs)39344 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39345 {
39346   struct cp_token_cache *ce;
39347   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39348   int i;
39349 
39350   if (!data->error_seen && data->fndecl_seen)
39351     {
39352       error ("%<#pragma omp declare simd%> not immediately followed by "
39353 	     "a single function declaration or definition");
39354       data->error_seen = true;
39355     }
39356   if (data->error_seen)
39357     return attrs;
39358 
39359   FOR_EACH_VEC_ELT (data->tokens, i, ce)
39360     {
39361       tree c, cl;
39362 
39363       cp_parser_push_lexer_for_tokens (parser, ce);
39364       parser->lexer->in_pragma = true;
39365       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39366       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39367       cp_lexer_consume_token (parser->lexer);
39368       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39369 				      "#pragma omp declare simd", pragma_tok);
39370       cp_parser_pop_lexer (parser);
39371       if (cl)
39372 	cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39373       c = build_tree_list (get_identifier ("omp declare simd"), cl);
39374       TREE_CHAIN (c) = attrs;
39375       if (processing_template_decl)
39376 	ATTR_IS_DEPENDENT (c) = 1;
39377       attrs = c;
39378     }
39379 
39380   data->fndecl_seen = true;
39381   return attrs;
39382 }
39383 
39384 
39385 /* OpenMP 4.0:
39386    # pragma omp declare target new-line
39387    declarations and definitions
39388    # pragma omp end declare target new-line
39389 
39390    OpenMP 4.5:
39391    # pragma omp declare target ( extended-list ) new-line
39392 
39393    # pragma omp declare target declare-target-clauses[seq] new-line  */
39394 
39395 #define OMP_DECLARE_TARGET_CLAUSE_MASK				\
39396 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
39397 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39398 
39399 static void
cp_parser_omp_declare_target(cp_parser * parser,cp_token * pragma_tok)39400 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39401 {
39402   tree clauses = NULL_TREE;
39403   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39404     clauses
39405       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39406 				   "#pragma omp declare target", pragma_tok);
39407   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39408     {
39409       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39410 					clauses);
39411       clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39412       cp_parser_require_pragma_eol (parser, pragma_tok);
39413     }
39414   else
39415     {
39416       cp_parser_require_pragma_eol (parser, pragma_tok);
39417       scope_chain->omp_declare_target_attribute++;
39418       return;
39419     }
39420   if (scope_chain->omp_declare_target_attribute)
39421     error_at (pragma_tok->location,
39422 	      "%<#pragma omp declare target%> with clauses in between "
39423 	      "%<#pragma omp declare target%> without clauses and "
39424 	      "%<#pragma omp end declare target%>");
39425   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39426     {
39427       tree t = OMP_CLAUSE_DECL (c), id;
39428       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39429       tree at2 = lookup_attribute ("omp declare target link",
39430 				   DECL_ATTRIBUTES (t));
39431       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39432 	{
39433 	  id = get_identifier ("omp declare target link");
39434 	  std::swap (at1, at2);
39435 	}
39436       else
39437 	id = get_identifier ("omp declare target");
39438       if (at2)
39439 	{
39440 	  error_at (OMP_CLAUSE_LOCATION (c),
39441 		    "%qD specified both in declare target %<link%> and %<to%>"
39442 		    " clauses", t);
39443 	  continue;
39444 	}
39445       if (!at1)
39446 	{
39447 	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39448 	  if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39449 	    continue;
39450 
39451 	  symtab_node *node = symtab_node::get (t);
39452 	  if (node != NULL)
39453 	    {
39454 	      node->offloadable = 1;
39455 	      if (ENABLE_OFFLOADING)
39456 		{
39457 		  g->have_offload = true;
39458 		  if (is_a <varpool_node *> (node))
39459 		    vec_safe_push (offload_vars, t);
39460 		}
39461 	    }
39462 	}
39463     }
39464 }
39465 
39466 static void
cp_parser_omp_end_declare_target(cp_parser * parser,cp_token * pragma_tok)39467 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39468 {
39469   const char *p = "";
39470   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39471     {
39472       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39473       p = IDENTIFIER_POINTER (id);
39474     }
39475   if (strcmp (p, "declare") == 0)
39476     {
39477       cp_lexer_consume_token (parser->lexer);
39478       p = "";
39479       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39480 	{
39481 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39482 	  p = IDENTIFIER_POINTER (id);
39483 	}
39484       if (strcmp (p, "target") == 0)
39485 	cp_lexer_consume_token (parser->lexer);
39486       else
39487 	{
39488 	  cp_parser_error (parser, "expected %<target%>");
39489 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39490 	  return;
39491 	}
39492     }
39493   else
39494     {
39495       cp_parser_error (parser, "expected %<declare%>");
39496       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39497       return;
39498     }
39499   cp_parser_require_pragma_eol (parser, pragma_tok);
39500   if (!scope_chain->omp_declare_target_attribute)
39501     error_at (pragma_tok->location,
39502 	      "%<#pragma omp end declare target%> without corresponding "
39503 	      "%<#pragma omp declare target%>");
39504   else
39505     scope_chain->omp_declare_target_attribute--;
39506 }
39507 
39508 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
39509    expression and optional initializer clause of
39510    #pragma omp declare reduction.  We store the expression(s) as
39511    either 3, 6 or 7 special statements inside of the artificial function's
39512    body.  The first two statements are DECL_EXPRs for the artificial
39513    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39514    expression that uses those variables.
39515    If there was any INITIALIZER clause, this is followed by further statements,
39516    the fourth and fifth statements are DECL_EXPRs for the artificial
39517    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
39518    constructor variant (first token after open paren is not omp_priv),
39519    then the sixth statement is a statement with the function call expression
39520    that uses the OMP_PRIV and optionally OMP_ORIG variable.
39521    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39522    to initialize the OMP_PRIV artificial variable and there is seventh
39523    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
39524 
39525 static bool
cp_parser_omp_declare_reduction_exprs(tree fndecl,cp_parser * parser)39526 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39527 {
39528   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39529   gcc_assert (TYPE_REF_P (type));
39530   type = TREE_TYPE (type);
39531   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39532   DECL_ARTIFICIAL (omp_out) = 1;
39533   pushdecl (omp_out);
39534   add_decl_expr (omp_out);
39535   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39536   DECL_ARTIFICIAL (omp_in) = 1;
39537   pushdecl (omp_in);
39538   add_decl_expr (omp_in);
39539   tree combiner;
39540   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39541 
39542   keep_next_level (true);
39543   tree block = begin_omp_structured_block ();
39544   combiner = cp_parser_expression (parser);
39545   finish_expr_stmt (combiner);
39546   block = finish_omp_structured_block (block);
39547   if (processing_template_decl)
39548     block = build_stmt (input_location, EXPR_STMT, block);
39549   add_stmt (block);
39550 
39551   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39552     return false;
39553 
39554   const char *p = "";
39555   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39556     {
39557       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39558       p = IDENTIFIER_POINTER (id);
39559     }
39560 
39561   if (strcmp (p, "initializer") == 0)
39562     {
39563       cp_lexer_consume_token (parser->lexer);
39564       matching_parens parens;
39565       if (!parens.require_open (parser))
39566 	return false;
39567 
39568       p = "";
39569       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39570 	{
39571 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39572 	  p = IDENTIFIER_POINTER (id);
39573 	}
39574 
39575       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39576       DECL_ARTIFICIAL (omp_priv) = 1;
39577       pushdecl (omp_priv);
39578       add_decl_expr (omp_priv);
39579       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39580       DECL_ARTIFICIAL (omp_orig) = 1;
39581       pushdecl (omp_orig);
39582       add_decl_expr (omp_orig);
39583 
39584       keep_next_level (true);
39585       block = begin_omp_structured_block ();
39586 
39587       bool ctor = false;
39588       if (strcmp (p, "omp_priv") == 0)
39589 	{
39590 	  bool is_direct_init, is_non_constant_init;
39591 	  ctor = true;
39592 	  cp_lexer_consume_token (parser->lexer);
39593 	  /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
39594 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39595 	      || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39596 		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39597 		     == CPP_CLOSE_PAREN
39598 		  && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39599 		     == CPP_CLOSE_PAREN))
39600 	    {
39601 	      finish_omp_structured_block (block);
39602 	      error ("invalid initializer clause");
39603 	      return false;
39604 	    }
39605 	  initializer = cp_parser_initializer (parser, &is_direct_init,
39606 					       &is_non_constant_init);
39607 	  cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39608 			  NULL_TREE, LOOKUP_ONLYCONVERTING);
39609 	}
39610       else
39611 	{
39612 	  cp_parser_parse_tentatively (parser);
39613 	  /* Don't create location wrapper nodes here.  */
39614 	  auto_suppress_location_wrappers sentinel;
39615 	  tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39616 						  /*check_dependency_p=*/true,
39617 						  /*template_p=*/NULL,
39618 						  /*declarator_p=*/false,
39619 						  /*optional_p=*/false);
39620 	  vec<tree, va_gc> *args;
39621 	  if (fn_name == error_mark_node
39622 	      || cp_parser_error_occurred (parser)
39623 	      || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39624 	      || ((args = cp_parser_parenthesized_expression_list
39625 				(parser, non_attr, /*cast_p=*/false,
39626 				 /*allow_expansion_p=*/true,
39627 				 /*non_constant_p=*/NULL)),
39628 		  cp_parser_error_occurred (parser)))
39629 	    {
39630 	      finish_omp_structured_block (block);
39631 	      cp_parser_abort_tentative_parse (parser);
39632 	      cp_parser_error (parser, "expected id-expression (arguments)");
39633 	      return false;
39634 	    }
39635 	  unsigned int i;
39636 	  tree arg;
39637 	  FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39638 	    if (arg == omp_priv
39639 		|| (TREE_CODE (arg) == ADDR_EXPR
39640 		    && TREE_OPERAND (arg, 0) == omp_priv))
39641 	      break;
39642 	  cp_parser_abort_tentative_parse (parser);
39643 	  if (arg == NULL_TREE)
39644 	    error ("one of the initializer call arguments should be %<omp_priv%>"
39645 		   " or %<&omp_priv%>");
39646 	  initializer = cp_parser_postfix_expression (parser, false, false, false,
39647 						      false, NULL);
39648 	  finish_expr_stmt (initializer);
39649 	}
39650 
39651       block = finish_omp_structured_block (block);
39652       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39653       if (processing_template_decl)
39654 	block = build_stmt (input_location, EXPR_STMT, block);
39655       add_stmt (block);
39656 
39657       if (ctor)
39658 	add_decl_expr (omp_orig);
39659 
39660       if (!parens.require_close (parser))
39661 	return false;
39662     }
39663 
39664   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39665     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39666                               UNKNOWN_LOCATION);
39667 
39668   return true;
39669 }
39670 
39671 /* OpenMP 4.0
39672    #pragma omp declare reduction (reduction-id : typename-list : expression) \
39673       initializer-clause[opt] new-line
39674 
39675    initializer-clause:
39676       initializer (omp_priv initializer)
39677       initializer (function-name (argument-list))  */
39678 
39679 static void
cp_parser_omp_declare_reduction(cp_parser * parser,cp_token * pragma_tok,enum pragma_context)39680 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39681 				 enum pragma_context)
39682 {
39683   auto_vec<tree> types;
39684   enum tree_code reduc_code = ERROR_MARK;
39685   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39686   unsigned int i;
39687   cp_token *first_token;
39688   cp_token_cache *cp;
39689   int errs;
39690   void *p;
39691 
39692   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
39693   p = obstack_alloc (&declarator_obstack, 0);
39694 
39695   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39696     goto fail;
39697 
39698   switch (cp_lexer_peek_token (parser->lexer)->type)
39699     {
39700     case CPP_PLUS:
39701       reduc_code = PLUS_EXPR;
39702       break;
39703     case CPP_MULT:
39704       reduc_code = MULT_EXPR;
39705       break;
39706     case CPP_MINUS:
39707       reduc_code = MINUS_EXPR;
39708       break;
39709     case CPP_AND:
39710       reduc_code = BIT_AND_EXPR;
39711       break;
39712     case CPP_XOR:
39713       reduc_code = BIT_XOR_EXPR;
39714       break;
39715     case CPP_OR:
39716       reduc_code = BIT_IOR_EXPR;
39717       break;
39718     case CPP_AND_AND:
39719       reduc_code = TRUTH_ANDIF_EXPR;
39720       break;
39721     case CPP_OR_OR:
39722       reduc_code = TRUTH_ORIF_EXPR;
39723       break;
39724     case CPP_NAME:
39725       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39726       break;
39727     default:
39728       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39729 			       "%<|%>, %<&&%>, %<||%> or identifier");
39730       goto fail;
39731     }
39732 
39733   if (reduc_code != ERROR_MARK)
39734     cp_lexer_consume_token (parser->lexer);
39735 
39736   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39737   if (reduc_id == error_mark_node)
39738     goto fail;
39739 
39740   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39741     goto fail;
39742 
39743   /* Types may not be defined in declare reduction type list.  */
39744   const char *saved_message;
39745   saved_message = parser->type_definition_forbidden_message;
39746   parser->type_definition_forbidden_message
39747     = G_("types may not be defined in declare reduction type list");
39748   bool saved_colon_corrects_to_scope_p;
39749   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39750   parser->colon_corrects_to_scope_p = false;
39751   bool saved_colon_doesnt_start_class_def_p;
39752   saved_colon_doesnt_start_class_def_p
39753     = parser->colon_doesnt_start_class_def_p;
39754   parser->colon_doesnt_start_class_def_p = true;
39755 
39756   while (true)
39757     {
39758       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39759       type = cp_parser_type_id (parser);
39760       if (type == error_mark_node)
39761 	;
39762       else if (ARITHMETIC_TYPE_P (type)
39763 	       && (orig_reduc_id == NULL_TREE
39764 		   || (TREE_CODE (type) != COMPLEX_TYPE
39765 		       && (id_equal (orig_reduc_id, "min")
39766 			   || id_equal (orig_reduc_id, "max")))))
39767 	error_at (loc, "predeclared arithmetic type %qT in "
39768 		       "%<#pragma omp declare reduction%>", type);
39769       else if (TREE_CODE (type) == FUNCTION_TYPE
39770 	       || TREE_CODE (type) == METHOD_TYPE
39771 	       || TREE_CODE (type) == ARRAY_TYPE)
39772 	error_at (loc, "function or array type %qT in "
39773 		       "%<#pragma omp declare reduction%>", type);
39774       else if (TYPE_REF_P (type))
39775 	error_at (loc, "reference type %qT in "
39776 		       "%<#pragma omp declare reduction%>", type);
39777       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39778 	error_at (loc, "const, volatile or __restrict qualified type %qT in "
39779 		       "%<#pragma omp declare reduction%>", type);
39780       else
39781 	types.safe_push (type);
39782 
39783       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39784 	cp_lexer_consume_token (parser->lexer);
39785       else
39786 	break;
39787     }
39788 
39789   /* Restore the saved message.  */
39790   parser->type_definition_forbidden_message = saved_message;
39791   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39792   parser->colon_doesnt_start_class_def_p
39793     = saved_colon_doesnt_start_class_def_p;
39794 
39795   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39796       || types.is_empty ())
39797     {
39798      fail:
39799       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39800       goto done;
39801     }
39802 
39803   first_token = cp_lexer_peek_token (parser->lexer);
39804   cp = NULL;
39805   errs = errorcount;
39806   FOR_EACH_VEC_ELT (types, i, type)
39807     {
39808       tree fntype
39809 	= build_function_type_list (void_type_node,
39810 				    cp_build_reference_type (type, false),
39811 				    NULL_TREE);
39812       tree this_reduc_id = reduc_id;
39813       if (!dependent_type_p (type))
39814 	this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39815       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39816       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39817       DECL_ARTIFICIAL (fndecl) = 1;
39818       DECL_EXTERNAL (fndecl) = 1;
39819       DECL_DECLARED_INLINE_P (fndecl) = 1;
39820       DECL_IGNORED_P (fndecl) = 1;
39821       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39822       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39823       DECL_ATTRIBUTES (fndecl)
39824 	= tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39825 		     DECL_ATTRIBUTES (fndecl));
39826       if (processing_template_decl)
39827 	fndecl = push_template_decl (fndecl);
39828       bool block_scope = false;
39829       tree block = NULL_TREE;
39830       if (current_function_decl)
39831 	{
39832 	  block_scope = true;
39833 	  DECL_CONTEXT (fndecl) = global_namespace;
39834 	  if (!processing_template_decl)
39835 	    pushdecl (fndecl);
39836 	}
39837       else if (current_class_type)
39838 	{
39839 	  if (cp == NULL)
39840 	    {
39841 	      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39842 		     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39843 		cp_lexer_consume_token (parser->lexer);
39844 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39845 		goto fail;
39846 	      cp = cp_token_cache_new (first_token,
39847 				       cp_lexer_peek_nth_token (parser->lexer,
39848 								2));
39849 	    }
39850 	  DECL_STATIC_FUNCTION_P (fndecl) = 1;
39851 	  finish_member_declaration (fndecl);
39852 	  DECL_PENDING_INLINE_INFO (fndecl) = cp;
39853 	  DECL_PENDING_INLINE_P (fndecl) = 1;
39854 	  vec_safe_push (unparsed_funs_with_definitions, fndecl);
39855 	  continue;
39856 	}
39857       else
39858 	{
39859 	  DECL_CONTEXT (fndecl) = current_namespace;
39860 	  pushdecl (fndecl);
39861 	}
39862       if (!block_scope)
39863 	start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39864       else
39865 	block = begin_omp_structured_block ();
39866       if (cp)
39867 	{
39868 	  cp_parser_push_lexer_for_tokens (parser, cp);
39869 	  parser->lexer->in_pragma = true;
39870 	}
39871       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39872 	{
39873 	  if (!block_scope)
39874 	    finish_function (/*inline_p=*/false);
39875 	  else
39876 	    DECL_CONTEXT (fndecl) = current_function_decl;
39877 	  if (cp)
39878 	    cp_parser_pop_lexer (parser);
39879 	  goto fail;
39880 	}
39881       if (cp)
39882 	cp_parser_pop_lexer (parser);
39883       if (!block_scope)
39884 	finish_function (/*inline_p=*/false);
39885       else
39886 	{
39887 	  DECL_CONTEXT (fndecl) = current_function_decl;
39888 	  block = finish_omp_structured_block (block);
39889 	  if (TREE_CODE (block) == BIND_EXPR)
39890 	    DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39891 	  else if (TREE_CODE (block) == STATEMENT_LIST)
39892 	    DECL_SAVED_TREE (fndecl) = block;
39893 	  if (processing_template_decl)
39894 	    add_decl_expr (fndecl);
39895 	}
39896       cp_check_omp_declare_reduction (fndecl);
39897       if (cp == NULL && types.length () > 1)
39898 	cp = cp_token_cache_new (first_token,
39899 				 cp_lexer_peek_nth_token (parser->lexer, 2));
39900       if (errs != errorcount)
39901 	break;
39902     }
39903 
39904   cp_parser_require_pragma_eol (parser, pragma_tok);
39905 
39906  done:
39907   /* Free any declarators allocated.  */
39908   obstack_free (&declarator_obstack, p);
39909 }
39910 
39911 /* OpenMP 4.0
39912    #pragma omp declare simd declare-simd-clauses[optseq] new-line
39913    #pragma omp declare reduction (reduction-id : typename-list : expression) \
39914       initializer-clause[opt] new-line
39915    #pragma omp declare target new-line  */
39916 
39917 static bool
cp_parser_omp_declare(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)39918 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39919 		       enum pragma_context context)
39920 {
39921   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39922     {
39923       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39924       const char *p = IDENTIFIER_POINTER (id);
39925 
39926       if (strcmp (p, "simd") == 0)
39927 	{
39928 	  cp_lexer_consume_token (parser->lexer);
39929 	  cp_parser_omp_declare_simd (parser, pragma_tok,
39930 				      context);
39931 	  return true;
39932 	}
39933       cp_ensure_no_omp_declare_simd (parser);
39934       if (strcmp (p, "reduction") == 0)
39935 	{
39936 	  cp_lexer_consume_token (parser->lexer);
39937 	  cp_parser_omp_declare_reduction (parser, pragma_tok,
39938 					   context);
39939 	  return false;
39940 	}
39941       if (!flag_openmp)  /* flag_openmp_simd  */
39942 	{
39943 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39944 	  return false;
39945 	}
39946       if (strcmp (p, "target") == 0)
39947 	{
39948 	  cp_lexer_consume_token (parser->lexer);
39949 	  cp_parser_omp_declare_target (parser, pragma_tok);
39950 	  return false;
39951 	}
39952     }
39953   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39954 			   "or %<target%>");
39955   cp_parser_require_pragma_eol (parser, pragma_tok);
39956   return false;
39957 }
39958 
39959 /* OpenMP 5.0
39960    #pragma omp requires clauses[optseq] new-line  */
39961 
39962 static bool
cp_parser_omp_requires(cp_parser * parser,cp_token * pragma_tok)39963 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39964 {
39965   bool first = true;
39966   enum omp_requires new_req = (enum omp_requires) 0;
39967 
39968   location_t loc = pragma_tok->location;
39969   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39970     {
39971       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39972 	cp_lexer_consume_token (parser->lexer);
39973 
39974       first = false;
39975 
39976       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39977 	{
39978 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39979 	  const char *p = IDENTIFIER_POINTER (id);
39980 	  location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39981 	  enum omp_requires this_req = (enum omp_requires) 0;
39982 
39983 	  if (!strcmp (p, "unified_address"))
39984 	    this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39985 	  else if (!strcmp (p, "unified_shared_memory"))
39986 	    this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39987 	  else if (!strcmp (p, "dynamic_allocators"))
39988 	    this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39989 	  else if (!strcmp (p, "reverse_offload"))
39990 	    this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39991 	  else if (!strcmp (p, "atomic_default_mem_order"))
39992 	    {
39993 	      cp_lexer_consume_token (parser->lexer);
39994 
39995 	      matching_parens parens;
39996 	      if (parens.require_open (parser))
39997 		{
39998 		  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39999 		    {
40000 		      id = cp_lexer_peek_token (parser->lexer)->u.value;
40001 		      p = IDENTIFIER_POINTER (id);
40002 
40003 		      if (!strcmp (p, "seq_cst"))
40004 			this_req
40005 			  = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
40006 		      else if (!strcmp (p, "relaxed"))
40007 			this_req
40008 			  = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
40009 		      else if (!strcmp (p, "acq_rel"))
40010 			this_req
40011 			  = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
40012 		    }
40013 		  if (this_req == 0)
40014 		    {
40015 		      error_at (cp_lexer_peek_token (parser->lexer)->location,
40016 				"expected %<seq_cst%>, %<relaxed%> or "
40017 				"%<acq_rel%>");
40018 		      if (cp_lexer_nth_token_is (parser->lexer, 2,
40019 						 CPP_CLOSE_PAREN))
40020 			cp_lexer_consume_token (parser->lexer);
40021 		    }
40022 		  else
40023 		    cp_lexer_consume_token (parser->lexer);
40024 
40025 		  if (!parens.require_close (parser))
40026 		    cp_parser_skip_to_closing_parenthesis (parser,
40027 							   /*recovering=*/true,
40028 							   /*or_comma=*/false,
40029 							   /*consume_paren=*/
40030 							   true);
40031 
40032 		  if (this_req == 0)
40033 		    {
40034 		      cp_parser_require_pragma_eol (parser, pragma_tok);
40035 		      return false;
40036 		    }
40037 		}
40038 	      p = NULL;
40039 	    }
40040 	  else
40041 	    {
40042 	      error_at (cloc, "expected %<unified_address%>, "
40043 			      "%<unified_shared_memory%>, "
40044 			      "%<dynamic_allocators%>, "
40045 			       "%<reverse_offload%> "
40046 			       "or %<atomic_default_mem_order%> clause");
40047 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40048 	      return false;
40049 	    }
40050 	  if (p)
40051 	    sorry_at (cloc, "%qs clause on %<requires%> directive not "
40052 			    "supported yet", p);
40053 	  if (p)
40054 	    cp_lexer_consume_token (parser->lexer);
40055 	  if (this_req)
40056 	    {
40057 	      if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40058 		{
40059 		  if ((this_req & new_req) != 0)
40060 		    error_at (cloc, "too many %qs clauses", p);
40061 		  if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
40062 		      && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
40063 		    error_at (cloc, "%qs clause used lexically after first "
40064 				    "target construct or offloading API", p);
40065 		}
40066 	      else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40067 		{
40068 		  error_at (cloc, "too many %qs clauses",
40069 			    "atomic_default_mem_order");
40070 		  this_req = (enum omp_requires) 0;
40071 		}
40072 	      else if ((omp_requires_mask
40073 			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
40074 		{
40075 		  error_at (cloc, "more than one %<atomic_default_mem_order%>"
40076 				  " clause in a single compilation unit");
40077 		  this_req
40078 		    = (enum omp_requires)
40079 		       (omp_requires_mask
40080 			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
40081 		}
40082 	      else if ((omp_requires_mask
40083 			& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
40084 		error_at (cloc, "%<atomic_default_mem_order%> clause used "
40085 				"lexically after first %<atomic%> construct "
40086 				"without memory order clause");
40087 	      new_req = (enum omp_requires) (new_req | this_req);
40088 	      omp_requires_mask
40089 		= (enum omp_requires) (omp_requires_mask | this_req);
40090 	      continue;
40091 	    }
40092 	}
40093       break;
40094     }
40095   cp_parser_require_pragma_eol (parser, pragma_tok);
40096 
40097   if (new_req == 0)
40098     error_at (loc, "%<pragma omp requires%> requires at least one clause");
40099   return false;
40100 }
40101 
40102 
40103 /* OpenMP 4.5:
40104    #pragma omp taskloop taskloop-clause[optseq] new-line
40105      for-loop
40106 
40107    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
40108      for-loop  */
40109 
40110 #define OMP_TASKLOOP_CLAUSE_MASK				\
40111 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
40112 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
40113 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
40114 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
40115 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
40116 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)	\
40117 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)	\
40118 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
40119 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
40120 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
40121 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
40122 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
40123 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)	\
40124 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY)	\
40125 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
40126 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40127 
40128 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)40129 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
40130 			char *p_name, omp_clause_mask mask, tree *cclauses,
40131 			bool *if_p)
40132 {
40133   tree clauses, sb, ret;
40134   unsigned int save;
40135   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40136 
40137   strcat (p_name, " taskloop");
40138   mask |= OMP_TASKLOOP_CLAUSE_MASK;
40139   /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
40140      clause.  */
40141   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
40142     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
40143 
40144   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40145     {
40146       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40147       const char *p = IDENTIFIER_POINTER (id);
40148 
40149       if (strcmp (p, "simd") == 0)
40150 	{
40151 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
40152 	  if (cclauses == NULL)
40153 	    cclauses = cclauses_buf;
40154 
40155 	  cp_lexer_consume_token (parser->lexer);
40156 	  if (!flag_openmp)  /* flag_openmp_simd  */
40157 	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40158 				       cclauses, if_p);
40159 	  sb = begin_omp_structured_block ();
40160 	  save = cp_parser_begin_omp_structured_block (parser);
40161 	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
40162 				    cclauses, if_p);
40163 	  cp_parser_end_omp_structured_block (parser, save);
40164 	  tree body = finish_omp_structured_block (sb);
40165 	  if (ret == NULL)
40166 	    return ret;
40167 	  ret = make_node (OMP_TASKLOOP);
40168 	  TREE_TYPE (ret) = void_type_node;
40169 	  OMP_FOR_BODY (ret) = body;
40170 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40171 	  SET_EXPR_LOCATION (ret, loc);
40172 	  add_stmt (ret);
40173 	  return ret;
40174 	}
40175     }
40176   if (!flag_openmp)  /* flag_openmp_simd  */
40177     {
40178       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40179       return NULL_TREE;
40180     }
40181 
40182   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
40183 				       cclauses == NULL);
40184   if (cclauses)
40185     {
40186       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
40187       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
40188     }
40189 
40190   keep_next_level (true);
40191   sb = begin_omp_structured_block ();
40192   save = cp_parser_begin_omp_structured_block (parser);
40193 
40194   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
40195 				if_p);
40196 
40197   cp_parser_end_omp_structured_block (parser, save);
40198   add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
40199 
40200   return ret;
40201 }
40202 
40203 
40204 /* OpenACC 2.0:
40205    # pragma acc routine oacc-routine-clause[optseq] new-line
40206      function-definition
40207 
40208    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
40209 */
40210 
40211 #define OACC_ROUTINE_CLAUSE_MASK					\
40212 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
40213 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
40214 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
40215 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
40216 
40217 
40218 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
40219    component, which must resolve to a declared namespace-scope
40220    function.  The clauses are either processed directly (for a named
40221    function), or defered until the immediatley following declaration
40222    is parsed.  */
40223 
40224 static void
cp_parser_oacc_routine(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)40225 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
40226 			enum pragma_context context)
40227 {
40228   gcc_checking_assert (context == pragma_external);
40229   /* The checking for "another pragma following this one" in the "no optional
40230      '( name )'" case makes sure that we dont re-enter.  */
40231   gcc_checking_assert (parser->oacc_routine == NULL);
40232 
40233   cp_oacc_routine_data data;
40234   data.error_seen = false;
40235   data.fndecl_seen = false;
40236   data.tokens = vNULL;
40237   data.clauses = NULL_TREE;
40238   data.loc = pragma_tok->location;
40239   /* It is safe to take the address of a local variable; it will only be
40240      used while this scope is live.  */
40241   parser->oacc_routine = &data;
40242 
40243   /* Look for optional '( name )'.  */
40244   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40245     {
40246       matching_parens parens;
40247       parens.consume_open (parser); /* '(' */
40248 
40249       /* We parse the name as an id-expression.  If it resolves to
40250 	 anything other than a non-overloaded function at namespace
40251 	 scope, it's an error.  */
40252       location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40253       tree name = cp_parser_id_expression (parser,
40254 					   /*template_keyword_p=*/false,
40255 					   /*check_dependency_p=*/false,
40256 					   /*template_p=*/NULL,
40257 					   /*declarator_p=*/false,
40258 					   /*optional_p=*/false);
40259       tree decl = (identifier_p (name)
40260 		   ? cp_parser_lookup_name_simple (parser, name, name_loc)
40261 		   : name);
40262       if (name != error_mark_node && decl == error_mark_node)
40263 	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40264 
40265       if (decl == error_mark_node
40266 	  || !parens.require_close (parser))
40267 	{
40268 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40269 	  parser->oacc_routine = NULL;
40270 	  return;
40271 	}
40272 
40273       data.clauses
40274 	= cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40275 				      "#pragma acc routine",
40276 				      cp_lexer_peek_token (parser->lexer));
40277 
40278       if (decl && is_overloaded_fn (decl)
40279 	  && (TREE_CODE (decl) != FUNCTION_DECL
40280 	      || DECL_FUNCTION_TEMPLATE_P  (decl)))
40281 	{
40282 	  error_at (name_loc,
40283 		    "%<#pragma acc routine%> names a set of overloads");
40284 	  parser->oacc_routine = NULL;
40285 	  return;
40286 	}
40287 
40288       /* Perhaps we should use the same rule as declarations in different
40289 	 namespaces?  */
40290       if (!DECL_NAMESPACE_SCOPE_P (decl))
40291 	{
40292 	  error_at (name_loc,
40293 		    "%qD does not refer to a namespace scope function", decl);
40294 	  parser->oacc_routine = NULL;
40295 	  return;
40296 	}
40297 
40298       if (TREE_CODE (decl) != FUNCTION_DECL)
40299 	{
40300 	  error_at (name_loc, "%qD does not refer to a function", decl);
40301 	  parser->oacc_routine = NULL;
40302 	  return;
40303 	}
40304 
40305       cp_finalize_oacc_routine (parser, decl, false);
40306       parser->oacc_routine = NULL;
40307     }
40308   else /* No optional '( name )'.  */
40309     {
40310       /* Store away all pragma tokens.  */
40311       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40312 	     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40313 	cp_lexer_consume_token (parser->lexer);
40314       if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40315 	parser->oacc_routine->error_seen = true;
40316       cp_parser_require_pragma_eol (parser, pragma_tok);
40317       struct cp_token_cache *cp
40318 	= cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40319       parser->oacc_routine->tokens.safe_push (cp);
40320 
40321       /* Emit a helpful diagnostic if there's another pragma following this
40322 	 one.  */
40323       if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40324 	{
40325 	  cp_ensure_no_oacc_routine (parser);
40326 	  data.tokens.release ();
40327 	  /* ..., and then just keep going.  */
40328 	  return;
40329 	}
40330 
40331       /* We only have to consider the pragma_external case here.  */
40332       cp_parser_declaration (parser);
40333       if (parser->oacc_routine
40334 	  && !parser->oacc_routine->fndecl_seen)
40335 	cp_ensure_no_oacc_routine (parser);
40336       else
40337 	parser->oacc_routine = NULL;
40338       data.tokens.release ();
40339     }
40340 }
40341 
40342 /* Finalize #pragma acc routine clauses after direct declarator has
40343    been parsed.  */
40344 
40345 static tree
cp_parser_late_parsing_oacc_routine(cp_parser * parser,tree attrs)40346 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40347 {
40348   struct cp_token_cache *ce;
40349   cp_oacc_routine_data *data = parser->oacc_routine;
40350 
40351   if (!data->error_seen && data->fndecl_seen)
40352     {
40353       error_at (data->loc,
40354 		"%<#pragma acc routine%> not immediately followed by "
40355 		"a single function declaration or definition");
40356       data->error_seen = true;
40357     }
40358   if (data->error_seen)
40359     return attrs;
40360 
40361   gcc_checking_assert (data->tokens.length () == 1);
40362   ce = data->tokens[0];
40363 
40364   cp_parser_push_lexer_for_tokens (parser, ce);
40365   parser->lexer->in_pragma = true;
40366   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40367 
40368   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40369   gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40370   parser->oacc_routine->clauses
40371     = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40372 				  "#pragma acc routine", pragma_tok);
40373   cp_parser_pop_lexer (parser);
40374   /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40375      fndecl_seen.  */
40376 
40377   return attrs;
40378 }
40379 
40380 /* Apply any saved OpenACC routine clauses to a just-parsed
40381    declaration.  */
40382 
40383 static void
cp_finalize_oacc_routine(cp_parser * parser,tree fndecl,bool is_defn)40384 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40385 {
40386   if (__builtin_expect (parser->oacc_routine != NULL, 0))
40387     {
40388       /* Keep going if we're in error reporting mode.  */
40389       if (parser->oacc_routine->error_seen
40390 	  || fndecl == error_mark_node)
40391 	return;
40392 
40393       if (parser->oacc_routine->fndecl_seen)
40394 	{
40395 	  error_at (parser->oacc_routine->loc,
40396 		    "%<#pragma acc routine%> not immediately followed by"
40397 		    " a single function declaration or definition");
40398 	  parser->oacc_routine = NULL;
40399 	  return;
40400 	}
40401       if (TREE_CODE (fndecl) != FUNCTION_DECL)
40402 	{
40403 	  cp_ensure_no_oacc_routine (parser);
40404 	  return;
40405 	}
40406 
40407       if (oacc_get_fn_attrib (fndecl))
40408 	{
40409 	  error_at (parser->oacc_routine->loc,
40410 		    "%<#pragma acc routine%> already applied to %qD", fndecl);
40411 	  parser->oacc_routine = NULL;
40412 	  return;
40413 	}
40414 
40415       if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40416 	{
40417 	  error_at (parser->oacc_routine->loc,
40418 		    TREE_USED (fndecl)
40419 		    ? G_("%<#pragma acc routine%> must be applied before use")
40420 		    : G_("%<#pragma acc routine%> must be applied before "
40421 			 "definition"));
40422 	  parser->oacc_routine = NULL;
40423 	  return;
40424 	}
40425 
40426       /* Process the routine's dimension clauses.  */
40427       tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40428       oacc_replace_fn_attrib (fndecl, dims);
40429 
40430       /* Add an "omp declare target" attribute.  */
40431       DECL_ATTRIBUTES (fndecl)
40432 	= tree_cons (get_identifier ("omp declare target"),
40433 		     NULL_TREE, DECL_ATTRIBUTES (fndecl));
40434 
40435       /* Don't unset parser->oacc_routine here: we may still need it to
40436 	 diagnose wrong usage.  But, remember that we've used this "#pragma acc
40437 	 routine".  */
40438       parser->oacc_routine->fndecl_seen = true;
40439     }
40440 }
40441 
40442 /* Main entry point to OpenMP statement pragmas.  */
40443 
40444 static void
cp_parser_omp_construct(cp_parser * parser,cp_token * pragma_tok,bool * if_p)40445 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40446 {
40447   tree stmt;
40448   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40449   omp_clause_mask mask (0);
40450 
40451   switch (cp_parser_pragma_kind (pragma_tok))
40452     {
40453     case PRAGMA_OACC_ATOMIC:
40454       cp_parser_omp_atomic (parser, pragma_tok);
40455       return;
40456     case PRAGMA_OACC_CACHE:
40457       stmt = cp_parser_oacc_cache (parser, pragma_tok);
40458       break;
40459     case PRAGMA_OACC_DATA:
40460       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40461       break;
40462     case PRAGMA_OACC_ENTER_DATA:
40463       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40464       break;
40465     case PRAGMA_OACC_EXIT_DATA:
40466       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40467       break;
40468     case PRAGMA_OACC_HOST_DATA:
40469       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40470       break;
40471     case PRAGMA_OACC_KERNELS:
40472     case PRAGMA_OACC_PARALLEL:
40473       strcpy (p_name, "#pragma acc");
40474       stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40475 					      if_p);
40476       break;
40477     case PRAGMA_OACC_LOOP:
40478       strcpy (p_name, "#pragma acc");
40479       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40480 				  if_p);
40481       break;
40482     case PRAGMA_OACC_UPDATE:
40483       stmt = cp_parser_oacc_update (parser, pragma_tok);
40484       break;
40485     case PRAGMA_OACC_WAIT:
40486       stmt = cp_parser_oacc_wait (parser, pragma_tok);
40487       break;
40488     case PRAGMA_OMP_ATOMIC:
40489       cp_parser_omp_atomic (parser, pragma_tok);
40490       return;
40491     case PRAGMA_OMP_CRITICAL:
40492       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40493       break;
40494     case PRAGMA_OMP_DISTRIBUTE:
40495       strcpy (p_name, "#pragma omp");
40496       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40497 				       if_p);
40498       break;
40499     case PRAGMA_OMP_FOR:
40500       strcpy (p_name, "#pragma omp");
40501       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40502 				if_p);
40503       break;
40504     case PRAGMA_OMP_MASTER:
40505       strcpy (p_name, "#pragma omp");
40506       stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40507 				   if_p);
40508       break;
40509     case PRAGMA_OMP_PARALLEL:
40510       strcpy (p_name, "#pragma omp");
40511       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40512 				     if_p);
40513       break;
40514     case PRAGMA_OMP_SECTIONS:
40515       strcpy (p_name, "#pragma omp");
40516       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40517       break;
40518     case PRAGMA_OMP_SIMD:
40519       strcpy (p_name, "#pragma omp");
40520       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40521 				 if_p);
40522       break;
40523     case PRAGMA_OMP_SINGLE:
40524       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40525       break;
40526     case PRAGMA_OMP_TASK:
40527       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40528       break;
40529     case PRAGMA_OMP_TASKGROUP:
40530       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40531       break;
40532     case PRAGMA_OMP_TASKLOOP:
40533       strcpy (p_name, "#pragma omp");
40534       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40535 				     if_p);
40536       break;
40537     case PRAGMA_OMP_TEAMS:
40538       strcpy (p_name, "#pragma omp");
40539       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40540 				  if_p);
40541       break;
40542     default:
40543       gcc_unreachable ();
40544     }
40545 
40546   protected_set_expr_location (stmt, pragma_tok->location);
40547 }
40548 
40549 /* Transactional Memory parsing routines.  */
40550 
40551 /* Parse a transaction attribute.
40552 
40553    txn-attribute:
40554 	attribute
40555 	[ [ identifier ] ]
40556 
40557    We use this instead of cp_parser_attributes_opt for transactions to avoid
40558    the pedwarn in C++98 mode.  */
40559 
40560 static tree
cp_parser_txn_attribute_opt(cp_parser * parser)40561 cp_parser_txn_attribute_opt (cp_parser *parser)
40562 {
40563   cp_token *token;
40564   tree attr_name, attr = NULL;
40565 
40566   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40567     return cp_parser_attributes_opt (parser);
40568 
40569   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40570     return NULL_TREE;
40571   cp_lexer_consume_token (parser->lexer);
40572   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40573     goto error1;
40574 
40575   token = cp_lexer_peek_token (parser->lexer);
40576   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40577     {
40578       token = cp_lexer_consume_token (parser->lexer);
40579 
40580       attr_name = (token->type == CPP_KEYWORD
40581 		   /* For keywords, use the canonical spelling,
40582 		      not the parsed identifier.  */
40583 		   ? ridpointers[(int) token->keyword]
40584 		   : token->u.value);
40585       attr = build_tree_list (attr_name, NULL_TREE);
40586     }
40587   else
40588     cp_parser_error (parser, "expected identifier");
40589 
40590   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40591  error1:
40592   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40593   return attr;
40594 }
40595 
40596 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40597 
40598    transaction-statement:
40599      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40600        compound-statement
40601      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40602 */
40603 
40604 static tree
cp_parser_transaction(cp_parser * parser,cp_token * token)40605 cp_parser_transaction (cp_parser *parser, cp_token *token)
40606 {
40607   unsigned char old_in = parser->in_transaction;
40608   unsigned char this_in = 1, new_in;
40609   enum rid keyword = token->keyword;
40610   tree stmt, attrs, noex;
40611 
40612   cp_lexer_consume_token (parser->lexer);
40613 
40614   if (keyword == RID_TRANSACTION_RELAXED
40615       || keyword == RID_SYNCHRONIZED)
40616     this_in |= TM_STMT_ATTR_RELAXED;
40617   else
40618     {
40619       attrs = cp_parser_txn_attribute_opt (parser);
40620       if (attrs)
40621 	this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40622     }
40623 
40624   /* Parse a noexcept specification.  */
40625   if (keyword == RID_ATOMIC_NOEXCEPT)
40626     noex = boolean_true_node;
40627   else if (keyword == RID_ATOMIC_CANCEL)
40628     {
40629       /* cancel-and-throw is unimplemented.  */
40630       sorry ("atomic_cancel");
40631       noex = NULL_TREE;
40632     }
40633   else
40634     noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40635 
40636   /* Keep track if we're in the lexical scope of an outer transaction.  */
40637   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40638 
40639   stmt = begin_transaction_stmt (token->location, NULL, this_in);
40640 
40641   parser->in_transaction = new_in;
40642   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40643   parser->in_transaction = old_in;
40644 
40645   finish_transaction_stmt (stmt, NULL, this_in, noex);
40646 
40647   return stmt;
40648 }
40649 
40650 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40651 
40652    transaction-expression:
40653      __transaction_atomic txn-noexcept-spec[opt] ( expression )
40654      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40655 */
40656 
40657 static tree
cp_parser_transaction_expression(cp_parser * parser,enum rid keyword)40658 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40659 {
40660   unsigned char old_in = parser->in_transaction;
40661   unsigned char this_in = 1;
40662   cp_token *token;
40663   tree expr, noex;
40664   bool noex_expr;
40665   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40666 
40667   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40668       || keyword == RID_TRANSACTION_RELAXED);
40669 
40670   if (!flag_tm)
40671     error_at (loc,
40672 	      keyword == RID_TRANSACTION_RELAXED
40673 	      ? G_("%<__transaction_relaxed%> without transactional memory "
40674 		  "support enabled")
40675 	      : G_("%<__transaction_atomic%> without transactional memory "
40676 		   "support enabled"));
40677 
40678   token = cp_parser_require_keyword (parser, keyword,
40679       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40680 	  : RT_TRANSACTION_RELAXED));
40681   gcc_assert (token != NULL);
40682 
40683   if (keyword == RID_TRANSACTION_RELAXED)
40684     this_in |= TM_STMT_ATTR_RELAXED;
40685 
40686   /* Set this early.  This might mean that we allow transaction_cancel in
40687      an expression that we find out later actually has to be a constexpr.
40688      However, we expect that cxx_constant_value will be able to deal with
40689      this; also, if the noexcept has no constexpr, then what we parse next
40690      really is a transaction's body.  */
40691   parser->in_transaction = this_in;
40692 
40693   /* Parse a noexcept specification.  */
40694   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40695 					       true);
40696 
40697   if (!noex || !noex_expr
40698       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40699     {
40700       matching_parens parens;
40701       parens.require_open (parser);
40702 
40703       expr = cp_parser_expression (parser);
40704       expr = finish_parenthesized_expr (expr);
40705 
40706       parens.require_close (parser);
40707     }
40708   else
40709     {
40710       /* The only expression that is available got parsed for the noexcept
40711          already.  noexcept is true then.  */
40712       expr = noex;
40713       noex = boolean_true_node;
40714     }
40715 
40716   expr = build_transaction_expr (token->location, expr, this_in, noex);
40717   parser->in_transaction = old_in;
40718 
40719   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40720     return error_mark_node;
40721 
40722   return (flag_tm ? expr : error_mark_node);
40723 }
40724 
40725 /* Parse a function-transaction-block.
40726 
40727    function-transaction-block:
40728      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40729 	 function-body
40730      __transaction_atomic txn-attribute[opt] function-try-block
40731      __transaction_relaxed ctor-initializer[opt] function-body
40732      __transaction_relaxed function-try-block
40733 */
40734 
40735 static void
cp_parser_function_transaction(cp_parser * parser,enum rid keyword)40736 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40737 {
40738   unsigned char old_in = parser->in_transaction;
40739   unsigned char new_in = 1;
40740   tree compound_stmt, stmt, attrs;
40741   cp_token *token;
40742 
40743   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40744       || keyword == RID_TRANSACTION_RELAXED);
40745   token = cp_parser_require_keyword (parser, keyword,
40746       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40747 	  : RT_TRANSACTION_RELAXED));
40748   gcc_assert (token != NULL);
40749 
40750   if (keyword == RID_TRANSACTION_RELAXED)
40751     new_in |= TM_STMT_ATTR_RELAXED;
40752   else
40753     {
40754       attrs = cp_parser_txn_attribute_opt (parser);
40755       if (attrs)
40756 	new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40757     }
40758 
40759   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40760 
40761   parser->in_transaction = new_in;
40762 
40763   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40764     cp_parser_function_try_block (parser);
40765   else
40766     cp_parser_ctor_initializer_opt_and_function_body
40767       (parser, /*in_function_try_block=*/false);
40768 
40769   parser->in_transaction = old_in;
40770 
40771   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40772 }
40773 
40774 /* Parse a __transaction_cancel statement.
40775 
40776    cancel-statement:
40777      __transaction_cancel txn-attribute[opt] ;
40778      __transaction_cancel txn-attribute[opt] throw-expression ;
40779 
40780    ??? Cancel and throw is not yet implemented.  */
40781 
40782 static tree
cp_parser_transaction_cancel(cp_parser * parser)40783 cp_parser_transaction_cancel (cp_parser *parser)
40784 {
40785   cp_token *token;
40786   bool is_outer = false;
40787   tree stmt, attrs;
40788 
40789   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40790 				     RT_TRANSACTION_CANCEL);
40791   gcc_assert (token != NULL);
40792 
40793   attrs = cp_parser_txn_attribute_opt (parser);
40794   if (attrs)
40795     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40796 
40797   /* ??? Parse cancel-and-throw here.  */
40798 
40799   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40800 
40801   if (!flag_tm)
40802     {
40803       error_at (token->location, "%<__transaction_cancel%> without "
40804 		"transactional memory support enabled");
40805       return error_mark_node;
40806     }
40807   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40808     {
40809       error_at (token->location, "%<__transaction_cancel%> within a "
40810 		"%<__transaction_relaxed%>");
40811       return error_mark_node;
40812     }
40813   else if (is_outer)
40814     {
40815       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40816 	  && !is_tm_may_cancel_outer (current_function_decl))
40817 	{
40818 	  error_at (token->location, "outer %<__transaction_cancel%> not "
40819 		    "within outer %<__transaction_atomic%>");
40820 	  error_at (token->location,
40821 		    "  or a %<transaction_may_cancel_outer%> function");
40822 	  return error_mark_node;
40823 	}
40824     }
40825   else if (parser->in_transaction == 0)
40826     {
40827       error_at (token->location, "%<__transaction_cancel%> not within "
40828 		"%<__transaction_atomic%>");
40829       return error_mark_node;
40830     }
40831 
40832   stmt = build_tm_abort_call (token->location, is_outer);
40833   add_stmt (stmt);
40834 
40835   return stmt;
40836 }
40837 
40838 /* The parser.  */
40839 
40840 static GTY (()) cp_parser *the_parser;
40841 
40842 
40843 /* Special handling for the first token or line in the file.  The first
40844    thing in the file might be #pragma GCC pch_preprocess, which loads a
40845    PCH file, which is a GC collection point.  So we need to handle this
40846    first pragma without benefit of an existing lexer structure.
40847 
40848    Always returns one token to the caller in *FIRST_TOKEN.  This is
40849    either the true first token of the file, or the first token after
40850    the initial pragma.  */
40851 
40852 static void
cp_parser_initial_pragma(cp_token * first_token)40853 cp_parser_initial_pragma (cp_token *first_token)
40854 {
40855   tree name = NULL;
40856 
40857   cp_lexer_get_preprocessor_token (NULL, first_token);
40858   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40859     {
40860       c_common_no_more_pch ();
40861       return;
40862     }
40863 
40864   cp_lexer_get_preprocessor_token (NULL, first_token);
40865   if (first_token->type == CPP_STRING)
40866     {
40867       name = first_token->u.value;
40868 
40869       cp_lexer_get_preprocessor_token (NULL, first_token);
40870       if (first_token->type != CPP_PRAGMA_EOL)
40871 	error_at (first_token->location,
40872 		  "junk at end of %<#pragma GCC pch_preprocess%>");
40873     }
40874   else
40875     error_at (first_token->location, "expected string literal");
40876 
40877   /* Skip to the end of the pragma.  */
40878   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40879     cp_lexer_get_preprocessor_token (NULL, first_token);
40880 
40881   /* Now actually load the PCH file.  */
40882   if (name)
40883     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40884 
40885   /* Read one more token to return to our caller.  We have to do this
40886      after reading the PCH file in, since its pointers have to be
40887      live.  */
40888   cp_lexer_get_preprocessor_token (NULL, first_token);
40889 }
40890 
40891 /* Parse a pragma GCC ivdep.  */
40892 
40893 static bool
cp_parser_pragma_ivdep(cp_parser * parser,cp_token * pragma_tok)40894 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40895 {
40896   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40897   return true;
40898 }
40899 
40900 /* Parse a pragma GCC unroll.  */
40901 
40902 static unsigned short
cp_parser_pragma_unroll(cp_parser * parser,cp_token * pragma_tok)40903 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40904 {
40905   location_t location = cp_lexer_peek_token (parser->lexer)->location;
40906   tree expr = cp_parser_constant_expression (parser);
40907   unsigned short unroll;
40908   expr = maybe_constant_value (expr);
40909   HOST_WIDE_INT lunroll = 0;
40910   if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40911       || TREE_CODE (expr) != INTEGER_CST
40912       || (lunroll = tree_to_shwi (expr)) < 0
40913       || lunroll >= USHRT_MAX)
40914     {
40915       error_at (location, "%<#pragma GCC unroll%> requires an"
40916 		" assignment-expression that evaluates to a non-negative"
40917 		" integral constant less than %u", USHRT_MAX);
40918       unroll = 0;
40919     }
40920   else
40921     {
40922       unroll = (unsigned short)lunroll;
40923       if (unroll == 0)
40924 	unroll = 1;
40925     }
40926   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40927   return unroll;
40928 }
40929 
40930 /* Normal parsing of a pragma token.  Here we can (and must) use the
40931    regular lexer.  */
40932 
40933 static bool
cp_parser_pragma(cp_parser * parser,enum pragma_context context,bool * if_p)40934 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40935 {
40936   cp_token *pragma_tok;
40937   unsigned int id;
40938   tree stmt;
40939   bool ret;
40940 
40941   pragma_tok = cp_lexer_consume_token (parser->lexer);
40942   gcc_assert (pragma_tok->type == CPP_PRAGMA);
40943   parser->lexer->in_pragma = true;
40944 
40945   id = cp_parser_pragma_kind (pragma_tok);
40946   if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40947     cp_ensure_no_omp_declare_simd (parser);
40948   switch (id)
40949     {
40950     case PRAGMA_GCC_PCH_PREPROCESS:
40951       error_at (pragma_tok->location,
40952 		"%<#pragma GCC pch_preprocess%> must be first");
40953       break;
40954 
40955     case PRAGMA_OMP_BARRIER:
40956       switch (context)
40957 	{
40958 	case pragma_compound:
40959 	  cp_parser_omp_barrier (parser, pragma_tok);
40960 	  return false;
40961 	case pragma_stmt:
40962 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40963 		    "used in compound statements", "omp barrier");
40964 	  break;
40965 	default:
40966 	  goto bad_stmt;
40967 	}
40968       break;
40969 
40970     case PRAGMA_OMP_DEPOBJ:
40971       switch (context)
40972 	{
40973 	case pragma_compound:
40974 	  cp_parser_omp_depobj (parser, pragma_tok);
40975 	  return false;
40976 	case pragma_stmt:
40977 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40978 		    "used in compound statements", "omp depobj");
40979 	  break;
40980 	default:
40981 	  goto bad_stmt;
40982 	}
40983       break;
40984 
40985     case PRAGMA_OMP_FLUSH:
40986       switch (context)
40987 	{
40988 	case pragma_compound:
40989 	  cp_parser_omp_flush (parser, pragma_tok);
40990 	  return false;
40991 	case pragma_stmt:
40992 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40993 		    "used in compound statements", "omp flush");
40994 	  break;
40995 	default:
40996 	  goto bad_stmt;
40997 	}
40998       break;
40999 
41000     case PRAGMA_OMP_TASKWAIT:
41001       switch (context)
41002 	{
41003 	case pragma_compound:
41004 	  cp_parser_omp_taskwait (parser, pragma_tok);
41005 	  return false;
41006 	case pragma_stmt:
41007 	  error_at (pragma_tok->location,
41008 		    "%<#pragma %s%> may only be used in compound statements",
41009 		    "omp taskwait");
41010 	  break;
41011 	default:
41012 	  goto bad_stmt;
41013 	}
41014       break;
41015 
41016     case PRAGMA_OMP_TASKYIELD:
41017       switch (context)
41018 	{
41019 	case pragma_compound:
41020 	  cp_parser_omp_taskyield (parser, pragma_tok);
41021 	  return false;
41022 	case pragma_stmt:
41023 	  error_at (pragma_tok->location,
41024 		    "%<#pragma %s%> may only be used in compound statements",
41025 		    "omp taskyield");
41026 	  break;
41027 	default:
41028 	  goto bad_stmt;
41029 	}
41030       break;
41031 
41032     case PRAGMA_OMP_CANCEL:
41033       switch (context)
41034 	{
41035 	case pragma_compound:
41036 	  cp_parser_omp_cancel (parser, pragma_tok);
41037 	  return false;
41038 	case pragma_stmt:
41039 	  error_at (pragma_tok->location,
41040 		    "%<#pragma %s%> may only be used in compound statements",
41041 		    "omp cancel");
41042 	  break;
41043 	default:
41044 	  goto bad_stmt;
41045 	}
41046       break;
41047 
41048     case PRAGMA_OMP_CANCELLATION_POINT:
41049       cp_parser_omp_cancellation_point (parser, pragma_tok, context);
41050       return false;
41051 
41052     case PRAGMA_OMP_THREADPRIVATE:
41053       cp_parser_omp_threadprivate (parser, pragma_tok);
41054       return false;
41055 
41056     case PRAGMA_OMP_DECLARE:
41057       return cp_parser_omp_declare (parser, pragma_tok, context);
41058 
41059     case PRAGMA_OACC_DECLARE:
41060       cp_parser_oacc_declare (parser, pragma_tok);
41061       return false;
41062 
41063     case PRAGMA_OACC_ENTER_DATA:
41064       if (context == pragma_stmt)
41065 	{
41066 	  error_at (pragma_tok->location,
41067 		    "%<#pragma %s%> may only be used in compound statements",
41068 		    "acc enter data");
41069 	  break;
41070 	}
41071       else if (context != pragma_compound)
41072 	goto bad_stmt;
41073       cp_parser_omp_construct (parser, pragma_tok, if_p);
41074       return true;
41075 
41076     case PRAGMA_OACC_EXIT_DATA:
41077       if (context == pragma_stmt)
41078 	{
41079 	  error_at (pragma_tok->location,
41080 		    "%<#pragma %s%> may only be used in compound statements",
41081 		    "acc exit data");
41082 	  break;
41083 	}
41084       else if (context != pragma_compound)
41085 	goto bad_stmt;
41086       cp_parser_omp_construct (parser, pragma_tok, if_p);
41087       return true;
41088 
41089     case PRAGMA_OACC_ROUTINE:
41090       if (context != pragma_external)
41091 	{
41092 	  error_at (pragma_tok->location,
41093 		    "%<#pragma acc routine%> must be at file scope");
41094 	  break;
41095 	}
41096       cp_parser_oacc_routine (parser, pragma_tok, context);
41097       return false;
41098 
41099     case PRAGMA_OACC_UPDATE:
41100       if (context == pragma_stmt)
41101 	{
41102 	  error_at (pragma_tok->location,
41103 		    "%<#pragma %s%> may only be used in compound statements",
41104 		    "acc update");
41105 	  break;
41106 	}
41107       else if (context != pragma_compound)
41108 	goto bad_stmt;
41109       cp_parser_omp_construct (parser, pragma_tok, if_p);
41110       return true;
41111 
41112     case PRAGMA_OACC_WAIT:
41113       if (context == pragma_stmt)
41114 	{
41115 	  error_at (pragma_tok->location,
41116 		    "%<#pragma %s%> may only be used in compound statements",
41117 		    "acc wait");
41118 	  break;
41119 	}
41120       else if (context != pragma_compound)
41121 	goto bad_stmt;
41122       cp_parser_omp_construct (parser, pragma_tok, if_p);
41123       return true;
41124 
41125     case PRAGMA_OACC_ATOMIC:
41126     case PRAGMA_OACC_CACHE:
41127     case PRAGMA_OACC_DATA:
41128     case PRAGMA_OACC_HOST_DATA:
41129     case PRAGMA_OACC_KERNELS:
41130     case PRAGMA_OACC_PARALLEL:
41131     case PRAGMA_OACC_LOOP:
41132     case PRAGMA_OMP_ATOMIC:
41133     case PRAGMA_OMP_CRITICAL:
41134     case PRAGMA_OMP_DISTRIBUTE:
41135     case PRAGMA_OMP_FOR:
41136     case PRAGMA_OMP_MASTER:
41137     case PRAGMA_OMP_PARALLEL:
41138     case PRAGMA_OMP_SECTIONS:
41139     case PRAGMA_OMP_SIMD:
41140     case PRAGMA_OMP_SINGLE:
41141     case PRAGMA_OMP_TASK:
41142     case PRAGMA_OMP_TASKGROUP:
41143     case PRAGMA_OMP_TASKLOOP:
41144     case PRAGMA_OMP_TEAMS:
41145       if (context != pragma_stmt && context != pragma_compound)
41146 	goto bad_stmt;
41147       stmt = push_omp_privatization_clauses (false);
41148       cp_parser_omp_construct (parser, pragma_tok, if_p);
41149       pop_omp_privatization_clauses (stmt);
41150       return true;
41151 
41152     case PRAGMA_OMP_REQUIRES:
41153       return cp_parser_omp_requires (parser, pragma_tok);
41154 
41155     case PRAGMA_OMP_ORDERED:
41156       if (context != pragma_stmt && context != pragma_compound)
41157 	goto bad_stmt;
41158       stmt = push_omp_privatization_clauses (false);
41159       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
41160       pop_omp_privatization_clauses (stmt);
41161       return ret;
41162 
41163     case PRAGMA_OMP_TARGET:
41164       if (context != pragma_stmt && context != pragma_compound)
41165 	goto bad_stmt;
41166       stmt = push_omp_privatization_clauses (false);
41167       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
41168       pop_omp_privatization_clauses (stmt);
41169       return ret;
41170 
41171     case PRAGMA_OMP_END_DECLARE_TARGET:
41172       cp_parser_omp_end_declare_target (parser, pragma_tok);
41173       return false;
41174 
41175     case PRAGMA_OMP_SECTION:
41176       error_at (pragma_tok->location,
41177 		"%<#pragma omp section%> may only be used in "
41178 		"%<#pragma omp sections%> construct");
41179       break;
41180 
41181     case PRAGMA_IVDEP:
41182       {
41183 	if (context == pragma_external)
41184 	  {
41185 	    error_at (pragma_tok->location,
41186 		      "%<#pragma GCC ivdep%> must be inside a function");
41187 	    break;
41188 	  }
41189 	const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
41190 	unsigned short unroll;
41191 	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41192 	if (tok->type == CPP_PRAGMA
41193 	    && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
41194 	  {
41195 	    tok = cp_lexer_consume_token (parser->lexer);
41196 	    unroll = cp_parser_pragma_unroll (parser, tok);
41197 	    tok = cp_lexer_peek_token (the_parser->lexer);
41198 	  }
41199 	else
41200 	  unroll = 0;
41201 	if (tok->type != CPP_KEYWORD
41202 	    || (tok->keyword != RID_FOR
41203 		&& tok->keyword != RID_WHILE
41204 		&& tok->keyword != RID_DO))
41205 	  {
41206 	    cp_parser_error (parser, "for, while or do statement expected");
41207 	    return false;
41208 	  }
41209 	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41210 	return true;
41211       }
41212 
41213     case PRAGMA_UNROLL:
41214       {
41215 	if (context == pragma_external)
41216 	  {
41217 	    error_at (pragma_tok->location,
41218 		      "%<#pragma GCC unroll%> must be inside a function");
41219 	    break;
41220 	  }
41221 	const unsigned short unroll
41222 	  = cp_parser_pragma_unroll (parser, pragma_tok);
41223 	bool ivdep;
41224 	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41225 	if (tok->type == CPP_PRAGMA
41226 	    && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
41227 	  {
41228 	    tok = cp_lexer_consume_token (parser->lexer);
41229 	    ivdep = cp_parser_pragma_ivdep (parser, tok);
41230 	    tok = cp_lexer_peek_token (the_parser->lexer);
41231 	  }
41232 	else
41233 	  ivdep = false;
41234 	if (tok->type != CPP_KEYWORD
41235 	    || (tok->keyword != RID_FOR
41236 		&& tok->keyword != RID_WHILE
41237 		&& tok->keyword != RID_DO))
41238 	  {
41239 	    cp_parser_error (parser, "for, while or do statement expected");
41240 	    return false;
41241 	  }
41242 	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
41243 	return true;
41244       }
41245 
41246     default:
41247       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
41248       c_invoke_pragma_handler (id);
41249       break;
41250 
41251     bad_stmt:
41252       cp_parser_error (parser, "expected declaration specifiers");
41253       break;
41254     }
41255 
41256   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41257   return false;
41258 }
41259 
41260 /* The interface the pragma parsers have to the lexer.  */
41261 
41262 enum cpp_ttype
pragma_lex(tree * value,location_t * loc)41263 pragma_lex (tree *value, location_t *loc)
41264 {
41265   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41266   enum cpp_ttype ret = tok->type;
41267 
41268   *value = tok->u.value;
41269   if (loc)
41270     *loc = tok->location;
41271 
41272   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41273     ret = CPP_EOF;
41274   else if (ret == CPP_STRING)
41275     *value = cp_parser_string_literal (the_parser, false, false);
41276   else
41277     {
41278       if (ret == CPP_KEYWORD)
41279 	ret = CPP_NAME;
41280       cp_lexer_consume_token (the_parser->lexer);
41281     }
41282 
41283   return ret;
41284 }
41285 
41286 
41287 /* External interface.  */
41288 
41289 /* Parse one entire translation unit.  */
41290 
41291 void
c_parse_file(void)41292 c_parse_file (void)
41293 {
41294   static bool already_called = false;
41295 
41296   if (already_called)
41297     fatal_error (input_location,
41298 		 "inter-module optimizations not implemented for C++");
41299   already_called = true;
41300 
41301   the_parser = cp_parser_new ();
41302   push_deferring_access_checks (flag_access_control
41303 				? dk_no_deferred : dk_no_check);
41304   cp_parser_translation_unit (the_parser);
41305   the_parser = NULL;
41306 
41307   finish_translation_unit ();
41308 }
41309 
41310 /* Create an identifier for a generic parameter type (a synthesized
41311    template parameter implied by `auto' or a concept identifier). */
41312 
41313 static GTY(()) int generic_parm_count;
41314 static tree
make_generic_type_name()41315 make_generic_type_name ()
41316 {
41317   char buf[32];
41318   sprintf (buf, "auto:%d", ++generic_parm_count);
41319   return get_identifier (buf);
41320 }
41321 
41322 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41323    (creating a new template parameter list if necessary).  Returns the newly
41324    created template type parm.  */
41325 
41326 static tree
synthesize_implicit_template_parm(cp_parser * parser,tree constr)41327 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
41328 {
41329   gcc_assert (current_binding_level->kind == sk_function_parms);
41330 
41331    /* Before committing to modifying any scope, if we're in an
41332       implicit template scope, and we're trying to synthesize a
41333       constrained parameter, try to find a previous parameter with
41334       the same name.  This is the same-type rule for abbreviated
41335       function templates.
41336 
41337       NOTE: We can generate implicit parameters when tentatively
41338       parsing a nested name specifier, only to reject that parse
41339       later. However, matching the same template-id as part of a
41340       direct-declarator should generate an identical template
41341       parameter, so this rule will merge them. */
41342   if (parser->implicit_template_scope && constr)
41343     {
41344       tree t = parser->implicit_template_parms;
41345       while (t)
41346         {
41347           if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41348 	    {
41349 	      tree d = TREE_VALUE (t);
41350 	      if (TREE_CODE (d) == PARM_DECL)
41351 		/* Return the TEMPLATE_PARM_INDEX.  */
41352 		d = DECL_INITIAL (d);
41353 	      return d;
41354 	    }
41355           t = TREE_CHAIN (t);
41356         }
41357     }
41358 
41359   /* We are either continuing a function template that already contains implicit
41360      template parameters, creating a new fully-implicit function template, or
41361      extending an existing explicit function template with implicit template
41362      parameters.  */
41363 
41364   cp_binding_level *const entry_scope = current_binding_level;
41365 
41366   bool become_template = false;
41367   cp_binding_level *parent_scope = 0;
41368 
41369   if (parser->implicit_template_scope)
41370     {
41371       gcc_assert (parser->implicit_template_parms);
41372 
41373       current_binding_level = parser->implicit_template_scope;
41374     }
41375   else
41376     {
41377       /* Roll back to the existing template parameter scope (in the case of
41378 	 extending an explicit function template) or introduce a new template
41379 	 parameter scope ahead of the function parameter scope (or class scope
41380 	 in the case of out-of-line member definitions).  The function scope is
41381 	 added back after template parameter synthesis below.  */
41382 
41383       cp_binding_level *scope = entry_scope;
41384 
41385       while (scope->kind == sk_function_parms)
41386 	{
41387 	  parent_scope = scope;
41388 	  scope = scope->level_chain;
41389 	}
41390       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41391 	{
41392 	  /* If not defining a class, then any class scope is a scope level in
41393 	     an out-of-line member definition.  In this case simply wind back
41394 	     beyond the first such scope to inject the template parameter list.
41395 	     Otherwise wind back to the class being defined.  The latter can
41396 	     occur in class member friend declarations such as:
41397 
41398 	       class A {
41399 		 void foo (auto);
41400 	       };
41401 	       class B {
41402 		 friend void A::foo (auto);
41403 	       };
41404 
41405 	    The template parameter list synthesized for the friend declaration
41406 	    must be injected in the scope of 'B'.  This can also occur in
41407 	    erroneous cases such as:
41408 
41409 	       struct A {
41410 	         struct B {
41411 		   void foo (auto);
41412 		 };
41413 		 void B::foo (auto) {}
41414 	       };
41415 
41416 	    Here the attempted definition of 'B::foo' within 'A' is ill-formed
41417 	    but, nevertheless, the template parameter list synthesized for the
41418 	    declarator should be injected into the scope of 'A' as if the
41419 	    ill-formed template was specified explicitly.  */
41420 
41421 	  while (scope->kind == sk_class && !scope->defining_class_p)
41422 	    {
41423 	      parent_scope = scope;
41424 	      scope = scope->level_chain;
41425 	    }
41426 	}
41427 
41428       current_binding_level = scope;
41429 
41430       if (scope->kind != sk_template_parms
41431 	  || !function_being_declared_is_template_p (parser))
41432 	{
41433 	  /* Introduce a new template parameter list for implicit template
41434 	     parameters.  */
41435 
41436 	  become_template = true;
41437 
41438 	  parser->implicit_template_scope
41439 	      = begin_scope (sk_template_parms, NULL);
41440 
41441 	  ++processing_template_decl;
41442 
41443 	  parser->fully_implicit_function_template_p = true;
41444 	  ++parser->num_template_parameter_lists;
41445 	}
41446       else
41447 	{
41448 	  /* Synthesize implicit template parameters at the end of the explicit
41449 	     template parameter list.  */
41450 
41451 	  gcc_assert (current_template_parms);
41452 
41453 	  parser->implicit_template_scope = scope;
41454 
41455 	  tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41456 	  parser->implicit_template_parms
41457 	    = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41458 	}
41459     }
41460 
41461   /* Synthesize a new template parameter and track the current template
41462      parameter chain with implicit_template_parms.  */
41463 
41464   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41465   tree synth_id = make_generic_type_name ();
41466   tree synth_tmpl_parm;
41467   bool non_type = false;
41468 
41469   if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41470     synth_tmpl_parm
41471       = finish_template_type_parm (class_type_node, synth_id);
41472   else if (TREE_CODE (proto) == TEMPLATE_DECL)
41473     synth_tmpl_parm
41474       = finish_constrained_template_template_parm (proto, synth_id);
41475   else
41476     {
41477       synth_tmpl_parm = copy_decl (proto);
41478       DECL_NAME (synth_tmpl_parm) = synth_id;
41479       non_type = true;
41480     }
41481 
41482   // Attach the constraint to the parm before processing.
41483   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41484   TREE_TYPE (node) = constr;
41485   tree new_parm
41486     = process_template_parm (parser->implicit_template_parms,
41487 			     input_location,
41488 			     node,
41489 			     /*non_type=*/non_type,
41490 			     /*param_pack=*/false);
41491 
41492   // Chain the new parameter to the list of implicit parameters.
41493   if (parser->implicit_template_parms)
41494     parser->implicit_template_parms
41495       = TREE_CHAIN (parser->implicit_template_parms);
41496   else
41497     parser->implicit_template_parms = new_parm;
41498 
41499   tree new_decl = get_local_decls ();
41500   if (non_type)
41501     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
41502     new_decl = DECL_INITIAL (new_decl);
41503 
41504   /* If creating a fully implicit function template, start the new implicit
41505      template parameter list with this synthesized type, otherwise grow the
41506      current template parameter list.  */
41507 
41508   if (become_template)
41509     {
41510       parent_scope->level_chain = current_binding_level;
41511 
41512       tree new_parms = make_tree_vec (1);
41513       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41514       current_template_parms = tree_cons (size_int (processing_template_decl),
41515 					  new_parms, current_template_parms);
41516     }
41517   else
41518     {
41519       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41520       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41521       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41522       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41523     }
41524 
41525   // If the new parameter was constrained, we need to add that to the
41526   // constraints in the template parameter list.
41527   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41528     {
41529       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41530       reqs = conjoin_constraints (reqs, req);
41531       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41532     }
41533 
41534   current_binding_level = entry_scope;
41535 
41536   return new_decl;
41537 }
41538 
41539 /* Finish the declaration of a fully implicit function template.  Such a
41540    template has no explicit template parameter list so has not been through the
41541    normal template head and tail processing.  synthesize_implicit_template_parm
41542    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
41543    provided if the declaration is a class member such that its template
41544    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
41545    form is returned.  Otherwise NULL_TREE is returned. */
41546 
41547 static tree
finish_fully_implicit_template(cp_parser * parser,tree member_decl_opt)41548 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41549 {
41550   gcc_assert (parser->fully_implicit_function_template_p);
41551 
41552   if (member_decl_opt && member_decl_opt != error_mark_node
41553       && DECL_VIRTUAL_P (member_decl_opt))
41554     {
41555       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41556 		"implicit templates may not be %<virtual%>");
41557       DECL_VIRTUAL_P (member_decl_opt) = false;
41558     }
41559 
41560   if (member_decl_opt)
41561     member_decl_opt = finish_member_template_decl (member_decl_opt);
41562   end_template_decl ();
41563 
41564   parser->fully_implicit_function_template_p = false;
41565   parser->implicit_template_parms = 0;
41566   parser->implicit_template_scope = 0;
41567   --parser->num_template_parameter_lists;
41568 
41569   return member_decl_opt;
41570 }
41571 
41572 /* Like finish_fully_implicit_template, but to be used in error
41573    recovery, rearranging scopes so that we restore the state we had
41574    before synthesize_implicit_template_parm inserted the implement
41575    template parms scope.  */
41576 
41577 static void
abort_fully_implicit_template(cp_parser * parser)41578 abort_fully_implicit_template (cp_parser *parser)
41579 {
41580   cp_binding_level *return_to_scope = current_binding_level;
41581 
41582   if (parser->implicit_template_scope
41583       && return_to_scope != parser->implicit_template_scope)
41584     {
41585       cp_binding_level *child = return_to_scope;
41586       for (cp_binding_level *scope = child->level_chain;
41587 	   scope != parser->implicit_template_scope;
41588 	   scope = child->level_chain)
41589 	child = scope;
41590       child->level_chain = parser->implicit_template_scope->level_chain;
41591       parser->implicit_template_scope->level_chain = return_to_scope;
41592       current_binding_level = parser->implicit_template_scope;
41593     }
41594   else
41595     return_to_scope = return_to_scope->level_chain;
41596 
41597   finish_fully_implicit_template (parser, NULL);
41598 
41599   gcc_assert (current_binding_level == return_to_scope);
41600 }
41601 
41602 /* Helper function for diagnostics that have complained about things
41603    being used with 'extern "C"' linkage.
41604 
41605    Attempt to issue a note showing where the 'extern "C"' linkage began.  */
41606 
41607 void
maybe_show_extern_c_location(void)41608 maybe_show_extern_c_location (void)
41609 {
41610   if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41611     inform (the_parser->innermost_linkage_specification_location,
41612 	    "%<extern \"C\"%> linkage started here");
41613 }
41614 
41615 #include "gt-cp-parser.h"
41616