1 /* -*- C++ -*- Parser.
2    Copyright (C) 2000-2018 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11 
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20 
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
47 
48 
49 /* The lexer.  */
50 
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52    and c-lex.c) and the C++ parser.  */
53 
54 static cp_token eof_token =
55 {
56   CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 };
58 
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61   NIC_NONE,
62   /* floating-point literal */
63   NIC_FLOAT,
64   /* %<this%> */
65   NIC_THIS,
66   /* %<__FUNCTION__%> */
67   NIC_FUNC_NAME,
68   /* %<__PRETTY_FUNCTION__%> */
69   NIC_PRETTY_FUNC,
70   /* %<__func__%> */
71   NIC_C99_FUNC,
72   /* "%<va_arg%> */
73   NIC_VA_ARG,
74   /* a cast */
75   NIC_CAST,
76   /* %<typeid%> operator */
77   NIC_TYPEID,
78   /* non-constant compound literals */
79   NIC_NCC,
80   /* a function call */
81   NIC_FUNC_CALL,
82   /* an increment */
83   NIC_INC,
84   /* an decrement */
85   NIC_DEC,
86   /* an array reference */
87   NIC_ARRAY_REF,
88   /* %<->%> */
89   NIC_ARROW,
90   /* %<.%> */
91   NIC_POINT,
92   /* the address of a label */
93   NIC_ADDR_LABEL,
94   /* %<*%> */
95   NIC_STAR,
96   /* %<&%> */
97   NIC_ADDR,
98   /* %<++%> */
99   NIC_PREINCREMENT,
100   /* %<--%> */
101   NIC_PREDECREMENT,
102   /* %<new%> */
103   NIC_NEW,
104   /* %<delete%> */
105   NIC_DEL,
106   /* calls to overloaded operators */
107   NIC_OVERLOADED,
108   /* an assignment */
109   NIC_ASSIGNMENT,
110   /* a comma operator */
111   NIC_COMMA,
112   /* a call to a constructor */
113   NIC_CONSTRUCTOR,
114   /* a transaction expression */
115   NIC_TRANSACTION
116 };
117 
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120   /* NULL */
121   NLE_NULL,
122   /* is not a type */
123   NLE_TYPE,
124   /* is not a class or namespace */
125   NLE_CXX98,
126   /* is not a class, namespace, or enumeration */
127   NLE_NOT_CXX98
128 };
129 
130 /* The various kinds of required token */
131 enum required_token {
132   RT_NONE,
133   RT_SEMICOLON,  /* ';' */
134   RT_OPEN_PAREN, /* '(' */
135   RT_CLOSE_BRACE, /* '}' */
136   RT_OPEN_BRACE,  /* '{' */
137   RT_CLOSE_SQUARE, /* ']' */
138   RT_OPEN_SQUARE,  /* '[' */
139   RT_COMMA, /* ',' */
140   RT_SCOPE, /* '::' */
141   RT_LESS, /* '<' */
142   RT_GREATER, /* '>' */
143   RT_EQ, /* '=' */
144   RT_ELLIPSIS, /* '...' */
145   RT_MULT, /* '*' */
146   RT_COMPL, /* '~' */
147   RT_COLON, /* ':' */
148   RT_COLON_SCOPE, /* ':' or '::' */
149   RT_CLOSE_PAREN, /* ')' */
150   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151   RT_PRAGMA_EOL, /* end of line */
152   RT_NAME, /* identifier */
153 
154   /* The type is CPP_KEYWORD */
155   RT_NEW, /* new */
156   RT_DELETE, /* delete */
157   RT_RETURN, /* return */
158   RT_WHILE, /* while */
159   RT_EXTERN, /* extern */
160   RT_STATIC_ASSERT, /* static_assert */
161   RT_DECLTYPE, /* decltype */
162   RT_OPERATOR, /* operator */
163   RT_CLASS, /* class */
164   RT_TEMPLATE, /* template */
165   RT_NAMESPACE, /* namespace */
166   RT_USING, /* using */
167   RT_ASM, /* asm */
168   RT_TRY, /* try */
169   RT_CATCH, /* catch */
170   RT_THROW, /* throw */
171   RT_LABEL, /* __label__ */
172   RT_AT_TRY, /* @try */
173   RT_AT_SYNCHRONIZED, /* @synchronized */
174   RT_AT_THROW, /* @throw */
175 
176   RT_SELECT,  /* selection-statement */
177   RT_ITERATION, /* iteration-statement */
178   RT_JUMP, /* jump-statement */
179   RT_CLASS_KEY, /* class-key */
180   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183   RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 };
185 
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187    reverting it on destruction.  */
188 
189 class type_id_in_expr_sentinel
190 {
191   cp_parser *parser;
192   bool saved;
193 public:
194   type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
parser(parser)195     : parser (parser),
196       saved (parser->in_type_id_in_expr_p)
197   { parser->in_type_id_in_expr_p = set; }
~type_id_in_expr_sentinel()198   ~type_id_in_expr_sentinel ()
199   { parser->in_type_id_in_expr_p = saved; }
200 };
201 
202 /* Prototypes.  */
203 
204 static cp_lexer *cp_lexer_new_main
205   (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207   (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209   (cp_lexer *);
210 static int cp_lexer_saving_tokens
211   (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213   (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215   (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217   (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219   (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221   (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223   (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225   (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227   (cp_lexer *);
228 static void cp_lexer_purge_token
229   (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231   (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233   (cp_lexer *);
234 static void cp_lexer_commit_tokens
235   (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237   (cp_lexer *);
238 static void cp_lexer_print_token
239   (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241   (cp_lexer *);
242 static void cp_lexer_start_debugging
243   (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245   (cp_lexer *) ATTRIBUTE_UNUSED;
246 
247 static cp_token_cache *cp_token_cache_new
248   (cp_token *, cp_token *);
249 
250 static void cp_parser_initial_pragma
251   (cp_token *);
252 
253 static bool cp_parser_omp_declare_reduction_exprs
254   (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256   (cp_parser *, tree, bool);
257 
258 /* Manifest constants.  */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
261 
262 /* Variables.  */
263 
264 /* The stream to which debugging output should be written.  */
265 static FILE *cp_lexer_debug_stream;
266 
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268    sizeof, typeof, or alignof.  */
269 int cp_unevaluated_operand;
270 
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
273    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
274    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275    highlighted by surrounding it in [[ ]].  */
276 
277 static void
cp_lexer_dump_tokens(FILE * file,vec<cp_token,va_gc> * buffer,cp_token * start_token,unsigned num,cp_token * curr_token)278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 		      cp_token *start_token, unsigned num,
280 		      cp_token *curr_token)
281 {
282   unsigned i, nprinted;
283   cp_token *token;
284   bool do_print;
285 
286   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
287 
288   if (buffer == NULL)
289     return;
290 
291   if (num == 0)
292     num = buffer->length ();
293 
294   if (start_token == NULL)
295     start_token = buffer->address ();
296 
297   if (start_token > buffer->address ())
298     {
299       cp_lexer_print_token (file, &(*buffer)[0]);
300       fprintf (file, " ... ");
301     }
302 
303   do_print = false;
304   nprinted = 0;
305   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
306     {
307       if (token == start_token)
308 	do_print = true;
309 
310       if (!do_print)
311 	continue;
312 
313       nprinted++;
314       if (token == curr_token)
315 	fprintf (file, "[[");
316 
317       cp_lexer_print_token (file, token);
318 
319       if (token == curr_token)
320 	fprintf (file, "]]");
321 
322       switch (token->type)
323 	{
324 	  case CPP_SEMICOLON:
325 	  case CPP_OPEN_BRACE:
326 	  case CPP_CLOSE_BRACE:
327 	  case CPP_EOF:
328 	    fputc ('\n', file);
329 	    break;
330 
331 	  default:
332 	    fputc (' ', file);
333 	}
334     }
335 
336   if (i == num && i < buffer->length ())
337     {
338       fprintf (file, " ... ");
339       cp_lexer_print_token (file, &buffer->last ());
340     }
341 
342   fprintf (file, "\n");
343 }
344 
345 
346 /* Dump all tokens in BUFFER to stderr.  */
347 
348 void
cp_lexer_debug_tokens(vec<cp_token,va_gc> * buffer)349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
350 {
351   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
352 }
353 
354 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> & ref)355 debug (vec<cp_token, va_gc> &ref)
356 {
357   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
358 }
359 
360 DEBUG_FUNCTION void
debug(vec<cp_token,va_gc> * ptr)361 debug (vec<cp_token, va_gc> *ptr)
362 {
363   if (ptr)
364     debug (*ptr);
365   else
366     fprintf (stderr, "<nil>\n");
367 }
368 
369 
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
371    description for T.  */
372 
373 static void
cp_debug_print_tree_if_set(FILE * file,const char * desc,tree t)374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
375 {
376   if (t)
377     {
378       fprintf (file, "%s: ", desc);
379       print_node_brief (file, "", t, 0);
380     }
381 }
382 
383 
384 /* Dump parser context C to FILE.  */
385 
386 static void
cp_debug_print_context(FILE * file,cp_parser_context * c)387 cp_debug_print_context (FILE *file, cp_parser_context *c)
388 {
389   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391   print_node_brief (file, "", c->object_type, 0);
392   fprintf (file, "}\n");
393 }
394 
395 
396 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
397 
398 static void
cp_debug_print_context_stack(FILE * file,cp_parser_context * first)399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
400 {
401   unsigned i;
402   cp_parser_context *c;
403 
404   fprintf (file, "Parsing context stack:\n");
405   for (i = 0, c = first; c; c = c->next, i++)
406     {
407       fprintf (file, "\t#%u: ", i);
408       cp_debug_print_context (file, c);
409     }
410 }
411 
412 
413 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
414 
415 static void
cp_debug_print_flag(FILE * file,const char * desc,bool flag)416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
417 {
418   if (flag)
419     fprintf (file, "%s: true\n", desc);
420 }
421 
422 
423 /* Print an unparsed function entry UF to FILE.  */
424 
425 static void
cp_debug_print_unparsed_function(FILE * file,cp_unparsed_functions_entry * uf)426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
427 {
428   unsigned i;
429   cp_default_arg_entry *default_arg_fn;
430   tree fn;
431 
432   fprintf (file, "\tFunctions with default args:\n");
433   for (i = 0;
434        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435        i++)
436     {
437       fprintf (file, "\t\tClass type: ");
438       print_node_brief (file, "", default_arg_fn->class_type, 0);
439       fprintf (file, "\t\tDeclaration: ");
440       print_node_brief (file, "", default_arg_fn->decl, 0);
441       fprintf (file, "\n");
442     }
443 
444   fprintf (file, "\n\tFunctions with definitions that require "
445 	   "post-processing\n\t\t");
446   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
447     {
448       print_node_brief (file, "", fn, 0);
449       fprintf (file, " ");
450     }
451   fprintf (file, "\n");
452 
453   fprintf (file, "\n\tNon-static data members with initializers that require "
454            "post-processing\n\t\t");
455   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
456     {
457       print_node_brief (file, "", fn, 0);
458       fprintf (file, " ");
459     }
460   fprintf (file, "\n");
461 }
462 
463 
464 /* Print the stack of unparsed member functions S to FILE.  */
465 
466 static void
cp_debug_print_unparsed_queues(FILE * file,vec<cp_unparsed_functions_entry,va_gc> * s)467 cp_debug_print_unparsed_queues (FILE *file,
468 				vec<cp_unparsed_functions_entry, va_gc> *s)
469 {
470   unsigned i;
471   cp_unparsed_functions_entry *uf;
472 
473   fprintf (file, "Unparsed functions\n");
474   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
475     {
476       fprintf (file, "#%u:\n", i);
477       cp_debug_print_unparsed_function (file, uf);
478     }
479 }
480 
481 
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
484 
485 static void
cp_debug_parser_tokens(FILE * file,cp_parser * parser,int window_size)486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
487 {
488   cp_token *next_token, *first_token, *start_token;
489 
490   if (file == NULL)
491     file = stderr;
492 
493   next_token = parser->lexer->next_token;
494   first_token = parser->lexer->buffer->address ();
495   start_token = (next_token > first_token + window_size / 2)
496 		? next_token - window_size / 2
497 		: first_token;
498   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 			next_token);
500 }
501 
502 
503 /* Dump debugging information for the given PARSER.  If FILE is NULL,
504    the output is printed on stderr.  */
505 
506 void
cp_debug_parser(FILE * file,cp_parser * parser)507 cp_debug_parser (FILE *file, cp_parser *parser)
508 {
509   const size_t window_size = 20;
510   cp_token *token;
511   expanded_location eloc;
512 
513   if (file == NULL)
514     file = stderr;
515 
516   fprintf (file, "Parser state\n\n");
517   fprintf (file, "Number of tokens: %u\n",
518 	   vec_safe_length (parser->lexer->buffer));
519   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520   cp_debug_print_tree_if_set (file, "Object scope",
521 				     parser->object_scope);
522   cp_debug_print_tree_if_set (file, "Qualifying scope",
523 				     parser->qualifying_scope);
524   cp_debug_print_context_stack (file, parser->context);
525   cp_debug_print_flag (file, "Allow GNU extensions",
526 			      parser->allow_gnu_extensions_p);
527   cp_debug_print_flag (file, "'>' token is greater-than",
528 			      parser->greater_than_is_operator_p);
529   cp_debug_print_flag (file, "Default args allowed in current "
530 			      "parameter list", parser->default_arg_ok_p);
531   cp_debug_print_flag (file, "Parsing integral constant-expression",
532 			      parser->integral_constant_expression_p);
533   cp_debug_print_flag (file, "Allow non-constant expression in current "
534 			      "constant-expression",
535 			      parser->allow_non_integral_constant_expression_p);
536   cp_debug_print_flag (file, "Seen non-constant expression",
537 			      parser->non_integral_constant_expression_p);
538   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 			      "current context",
540 			      parser->local_variables_forbidden_p);
541   cp_debug_print_flag (file, "In unbraced linkage specification",
542 			      parser->in_unbraced_linkage_specification_p);
543   cp_debug_print_flag (file, "Parsing a declarator",
544 			      parser->in_declarator_p);
545   cp_debug_print_flag (file, "In template argument list",
546 			      parser->in_template_argument_list_p);
547   cp_debug_print_flag (file, "Parsing an iteration statement",
548 			      parser->in_statement & IN_ITERATION_STMT);
549   cp_debug_print_flag (file, "Parsing a switch statement",
550 			      parser->in_statement & IN_SWITCH_STMT);
551   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 			      parser->in_statement & IN_OMP_BLOCK);
553   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 			      parser->in_statement & IN_OMP_FOR);
555   cp_debug_print_flag (file, "Parsing an if statement",
556 			      parser->in_statement & IN_IF_STMT);
557   cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 			      "context", parser->in_type_id_in_expr_p);
559   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 			      parser->implicit_extern_c);
561   cp_debug_print_flag (file, "String expressions should be translated "
562 			      "to execution character set",
563 			      parser->translate_strings_p);
564   cp_debug_print_flag (file, "Parsing function body outside of a "
565 			      "local class", parser->in_function_body);
566   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 			      parser->colon_corrects_to_scope_p);
568   cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 			      parser->colon_doesnt_start_class_def_p);
570   if (parser->type_definition_forbidden_message)
571     fprintf (file, "Error message for forbidden type definitions: %s\n",
572 	     parser->type_definition_forbidden_message);
573   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574   fprintf (file, "Number of class definitions in progress: %u\n",
575 	   parser->num_classes_being_defined);
576   fprintf (file, "Number of template parameter lists for the current "
577 	   "declaration: %u\n", parser->num_template_parameter_lists);
578   cp_debug_parser_tokens (file, parser, window_size);
579   token = parser->lexer->next_token;
580   fprintf (file, "Next token to parse:\n");
581   fprintf (file, "\tToken:  ");
582   cp_lexer_print_token (file, token);
583   eloc = expand_location (token->location);
584   fprintf (file, "\n\tFile:   %s\n", eloc.file);
585   fprintf (file, "\tLine:   %d\n", eloc.line);
586   fprintf (file, "\tColumn: %d\n", eloc.column);
587 }
588 
589 DEBUG_FUNCTION void
debug(cp_parser & ref)590 debug (cp_parser &ref)
591 {
592   cp_debug_parser (stderr, &ref);
593 }
594 
595 DEBUG_FUNCTION void
debug(cp_parser * ptr)596 debug (cp_parser *ptr)
597 {
598   if (ptr)
599     debug (*ptr);
600   else
601     fprintf (stderr, "<nil>\n");
602 }
603 
604 /* Allocate memory for a new lexer object and return it.  */
605 
606 static cp_lexer *
cp_lexer_alloc(void)607 cp_lexer_alloc (void)
608 {
609   cp_lexer *lexer;
610 
611   c_common_no_more_pch ();
612 
613   /* Allocate the memory.  */
614   lexer = ggc_cleared_alloc<cp_lexer> ();
615 
616   /* Initially we are not debugging.  */
617   lexer->debugging_p = false;
618 
619   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620 
621   /* Create the buffer.  */
622   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623 
624   return lexer;
625 }
626 
627 
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629    preprocessor.  */
630 
631 static cp_lexer *
cp_lexer_new_main(void)632 cp_lexer_new_main (void)
633 {
634   cp_lexer *lexer;
635   cp_token token;
636 
637   /* It's possible that parsing the first pragma will load a PCH file,
638      which is a GC collection point.  So we have to do that before
639      allocating any memory.  */
640   cp_parser_initial_pragma (&token);
641 
642   lexer = cp_lexer_alloc ();
643 
644   /* Put the first token in the buffer.  */
645   lexer->buffer->quick_push (token);
646 
647   /* Get the remaining tokens from the preprocessor.  */
648   while (token.type != CPP_EOF)
649     {
650       cp_lexer_get_preprocessor_token (lexer, &token);
651       vec_safe_push (lexer->buffer, token);
652     }
653 
654   lexer->last_token = lexer->buffer->address ()
655                       + lexer->buffer->length ()
656 		      - 1;
657   lexer->next_token = lexer->buffer->length ()
658 		      ? lexer->buffer->address ()
659 		      : &eof_token;
660 
661   /* Subsequent preprocessor diagnostics should use compiler
662      diagnostic functions to get the compiler source location.  */
663   done_lexing = true;
664 
665   gcc_assert (!lexer->next_token->purged_p);
666   return lexer;
667 }
668 
669 /* Create a new lexer whose token stream is primed with the tokens in
670    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
671 
672 static cp_lexer *
cp_lexer_new_from_tokens(cp_token_cache * cache)673 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 {
675   cp_token *first = cache->first;
676   cp_token *last = cache->last;
677   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678 
679   /* We do not own the buffer.  */
680   lexer->buffer = NULL;
681   lexer->next_token = first == last ? &eof_token : first;
682   lexer->last_token = last;
683 
684   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685 
686   /* Initially we are not debugging.  */
687   lexer->debugging_p = false;
688 
689   gcc_assert (!lexer->next_token->purged_p);
690   return lexer;
691 }
692 
693 /* Frees all resources associated with LEXER.  */
694 
695 static void
cp_lexer_destroy(cp_lexer * lexer)696 cp_lexer_destroy (cp_lexer *lexer)
697 {
698   vec_free (lexer->buffer);
699   lexer->saved_tokens.release ();
700   ggc_free (lexer);
701 }
702 
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704    be used.  The point of this flag is to help the compiler to fold away calls
705    to cp_lexer_debugging_p within this source file at compile time, when the
706    lexer is not being debugged.  */
707 
708 #define LEXER_DEBUGGING_ENABLED_P false
709 
710 /* Returns nonzero if debugging information should be output.  */
711 
712 static inline bool
cp_lexer_debugging_p(cp_lexer * lexer)713 cp_lexer_debugging_p (cp_lexer *lexer)
714 {
715   if (!LEXER_DEBUGGING_ENABLED_P)
716     return false;
717 
718   return lexer->debugging_p;
719 }
720 
721 
722 static inline cp_token_position
cp_lexer_token_position(cp_lexer * lexer,bool previous_p)723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 {
725   gcc_assert (!previous_p || lexer->next_token != &eof_token);
726 
727   return lexer->next_token - previous_p;
728 }
729 
730 static inline cp_token *
cp_lexer_token_at(cp_lexer *,cp_token_position pos)731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733   return pos;
734 }
735 
736 static inline void
cp_lexer_set_token_position(cp_lexer * lexer,cp_token_position pos)737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 {
739   lexer->next_token = cp_lexer_token_at (lexer, pos);
740 }
741 
742 static inline cp_token_position
cp_lexer_previous_token_position(cp_lexer * lexer)743 cp_lexer_previous_token_position (cp_lexer *lexer)
744 {
745   if (lexer->next_token == &eof_token)
746     return lexer->last_token - 1;
747   else
748     return cp_lexer_token_position (lexer, true);
749 }
750 
751 static inline cp_token *
cp_lexer_previous_token(cp_lexer * lexer)752 cp_lexer_previous_token (cp_lexer *lexer)
753 {
754   cp_token_position tp = cp_lexer_previous_token_position (lexer);
755 
756   /* Skip past purged tokens.  */
757   while (tp->purged_p)
758     {
759       gcc_assert (tp != vec_safe_address (lexer->buffer));
760       tp--;
761     }
762 
763   return cp_lexer_token_at (lexer, tp);
764 }
765 
766 /* nonzero if we are presently saving tokens.  */
767 
768 static inline int
cp_lexer_saving_tokens(const cp_lexer * lexer)769 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 {
771   return lexer->saved_tokens.length () != 0;
772 }
773 
774 /* Store the next token from the preprocessor in *TOKEN.  Return true
775    if we reach EOF.  If LEXER is NULL, assume we are handling an
776    initial #pragma pch_preprocess, and thus want the lexer to return
777    processed strings.  */
778 
779 static void
cp_lexer_get_preprocessor_token(cp_lexer * lexer,cp_token * token)780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 {
782   static int is_extern_c = 0;
783 
784    /* Get a new token from the preprocessor.  */
785   token->type
786     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 			lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788   token->keyword = RID_MAX;
789   token->purged_p = false;
790   token->error_reported = false;
791 
792   /* On some systems, some header files are surrounded by an
793      implicit extern "C" block.  Set a flag in the token if it
794      comes from such a header.  */
795   is_extern_c += pending_lang_change;
796   pending_lang_change = 0;
797   token->implicit_extern_c = is_extern_c > 0;
798 
799   /* Check to see if this token is a keyword.  */
800   if (token->type == CPP_NAME)
801     {
802       if (IDENTIFIER_KEYWORD_P (token->u.value))
803 	{
804 	  /* Mark this token as a keyword.  */
805 	  token->type = CPP_KEYWORD;
806 	  /* Record which keyword.  */
807 	  token->keyword = C_RID_CODE (token->u.value);
808 	}
809       else
810 	{
811           if (warn_cxx11_compat
812               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813               && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814             {
815               /* Warn about the C++0x keyword (but still treat it as
816                  an identifier).  */
817               warning (OPT_Wc__11_compat,
818                        "identifier %qE is a keyword in C++11",
819                        token->u.value);
820 
821               /* Clear out the C_RID_CODE so we don't warn about this
822                  particular identifier-turned-keyword again.  */
823               C_SET_RID_CODE (token->u.value, RID_MAX);
824             }
825 
826 	  token->keyword = RID_MAX;
827 	}
828     }
829   else if (token->type == CPP_AT_NAME)
830     {
831       /* This only happens in Objective-C++; it must be a keyword.  */
832       token->type = CPP_KEYWORD;
833       switch (C_RID_CODE (token->u.value))
834 	{
835 	  /* Replace 'class' with '@class', 'private' with '@private',
836 	     etc.  This prevents confusion with the C++ keyword
837 	     'class', and makes the tokens consistent with other
838 	     Objective-C 'AT' keywords.  For example '@class' is
839 	     reported as RID_AT_CLASS which is consistent with
840 	     '@synchronized', which is reported as
841 	     RID_AT_SYNCHRONIZED.
842 	  */
843 	case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
844 	case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
845 	case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 	case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
847 	case RID_THROW:     token->keyword = RID_AT_THROW; break;
848 	case RID_TRY:       token->keyword = RID_AT_TRY; break;
849 	case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
850 	case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 	default:            token->keyword = C_RID_CODE (token->u.value);
852 	}
853     }
854 }
855 
856 /* Update the globals input_location and the input file stack from TOKEN.  */
857 static inline void
cp_lexer_set_source_position_from_token(cp_token * token)858 cp_lexer_set_source_position_from_token (cp_token *token)
859 {
860   if (token->type != CPP_EOF)
861     {
862       input_location = token->location;
863     }
864 }
865 
866 /* Update the globals input_location and the input file stack from LEXER.  */
867 static inline void
cp_lexer_set_source_position(cp_lexer * lexer)868 cp_lexer_set_source_position (cp_lexer *lexer)
869 {
870   cp_token *token = cp_lexer_peek_token (lexer);
871   cp_lexer_set_source_position_from_token (token);
872 }
873 
874 /* Return a pointer to the next token in the token stream, but do not
875    consume it.  */
876 
877 static inline cp_token *
cp_lexer_peek_token(cp_lexer * lexer)878 cp_lexer_peek_token (cp_lexer *lexer)
879 {
880   if (cp_lexer_debugging_p (lexer))
881     {
882       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884       putc ('\n', cp_lexer_debug_stream);
885     }
886   return lexer->next_token;
887 }
888 
889 /* Return true if the next token has the indicated TYPE.  */
890 
891 static inline bool
cp_lexer_next_token_is(cp_lexer * lexer,enum cpp_ttype type)892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 {
894   return cp_lexer_peek_token (lexer)->type == type;
895 }
896 
897 /* Return true if the next token does not have the indicated TYPE.  */
898 
899 static inline bool
cp_lexer_next_token_is_not(cp_lexer * lexer,enum cpp_ttype type)900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 {
902   return !cp_lexer_next_token_is (lexer, type);
903 }
904 
905 /* Return true if the next token is the indicated KEYWORD.  */
906 
907 static inline bool
cp_lexer_next_token_is_keyword(cp_lexer * lexer,enum rid keyword)908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 {
910   return cp_lexer_peek_token (lexer)->keyword == keyword;
911 }
912 
913 static inline bool
cp_lexer_nth_token_is(cp_lexer * lexer,size_t n,enum cpp_ttype type)914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 {
916   return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 }
918 
919 static inline bool
cp_lexer_nth_token_is_keyword(cp_lexer * lexer,size_t n,enum rid keyword)920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 {
922   return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 }
924 
925 /* Return true if the next token is not the indicated KEYWORD.  */
926 
927 static inline bool
cp_lexer_next_token_is_not_keyword(cp_lexer * lexer,enum rid keyword)928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929 {
930   return cp_lexer_peek_token (lexer)->keyword != keyword;
931 }
932 
933 /* Return true if KEYWORD can start a decl-specifier.  */
934 
935 bool
cp_keyword_starts_decl_specifier_p(enum rid keyword)936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
937 {
938   switch (keyword)
939     {
940       /* auto specifier: storage-class-specifier in C++,
941          simple-type-specifier in C++0x.  */
942     case RID_AUTO:
943       /* Storage classes.  */
944     case RID_REGISTER:
945     case RID_STATIC:
946     case RID_EXTERN:
947     case RID_MUTABLE:
948     case RID_THREAD:
949       /* Elaborated type specifiers.  */
950     case RID_ENUM:
951     case RID_CLASS:
952     case RID_STRUCT:
953     case RID_UNION:
954     case RID_TYPENAME:
955       /* Simple type specifiers.  */
956     case RID_CHAR:
957     case RID_CHAR16:
958     case RID_CHAR32:
959     case RID_WCHAR:
960     case RID_BOOL:
961     case RID_SHORT:
962     case RID_INT:
963     case RID_LONG:
964     case RID_SIGNED:
965     case RID_UNSIGNED:
966     case RID_FLOAT:
967     case RID_DOUBLE:
968     case RID_VOID:
969       /* GNU extensions.  */
970     case RID_ATTRIBUTE:
971     case RID_TYPEOF:
972       /* C++0x extensions.  */
973     case RID_DECLTYPE:
974     case RID_UNDERLYING_TYPE:
975     case RID_CONSTEXPR:
976       return true;
977 
978     default:
979       if (keyword >= RID_FIRST_INT_N
980 	  && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 	  && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 	return true;
983       return false;
984     }
985 }
986 
987 /* Return true if the next token is a keyword for a decl-specifier.  */
988 
989 static bool
cp_lexer_next_token_is_decl_specifier_keyword(cp_lexer * lexer)990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
991 {
992   cp_token *token;
993 
994   token = cp_lexer_peek_token (lexer);
995   return cp_keyword_starts_decl_specifier_p (token->keyword);
996 }
997 
998 /* Returns TRUE iff the token T begins a decltype type.  */
999 
1000 static bool
token_is_decltype(cp_token * t)1001 token_is_decltype (cp_token *t)
1002 {
1003   return (t->keyword == RID_DECLTYPE
1004 	  || t->type == CPP_DECLTYPE);
1005 }
1006 
1007 /* Returns TRUE iff the next token begins a decltype type.  */
1008 
1009 static bool
cp_lexer_next_token_is_decltype(cp_lexer * lexer)1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1011 {
1012   cp_token *t = cp_lexer_peek_token (lexer);
1013   return token_is_decltype (t);
1014 }
1015 
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017    associated checks and return the value.  */
1018 
1019 static tree
saved_checks_value(struct tree_check * check_value)1020 saved_checks_value (struct tree_check *check_value)
1021 {
1022   /* Perform any access checks that were deferred.  */
1023   vec<deferred_access_check, va_gc> *checks;
1024   deferred_access_check *chk;
1025   checks = check_value->checks;
1026   if (checks)
1027     {
1028       int i;
1029       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 	perform_or_defer_access_check (chk->binfo,
1031 				       chk->decl,
1032 				       chk->diag_decl, tf_warning_or_error);
1033     }
1034   /* Return the stored value.  */
1035   return check_value->value;
1036 }
1037 
1038 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1039    then this is precisely equivalent to cp_lexer_peek_token (except
1040    that it is not inline).  One would like to disallow that case, but
1041    there is one case (cp_parser_nth_token_starts_template_id) where
1042    the caller passes a variable for N and it might be 1.  */
1043 
1044 static cp_token *
cp_lexer_peek_nth_token(cp_lexer * lexer,size_t n)1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1046 {
1047   cp_token *token;
1048 
1049   /* N is 1-based, not zero-based.  */
1050   gcc_assert (n > 0);
1051 
1052   if (cp_lexer_debugging_p (lexer))
1053     fprintf (cp_lexer_debug_stream,
1054 	     "cp_lexer: peeking ahead %ld at token: ", (long)n);
1055 
1056   --n;
1057   token = lexer->next_token;
1058   gcc_assert (!n || token != &eof_token);
1059   while (n != 0)
1060     {
1061       ++token;
1062       if (token == lexer->last_token)
1063 	{
1064 	  token = &eof_token;
1065 	  break;
1066 	}
1067 
1068       if (!token->purged_p)
1069 	--n;
1070     }
1071 
1072   if (cp_lexer_debugging_p (lexer))
1073     {
1074       cp_lexer_print_token (cp_lexer_debug_stream, token);
1075       putc ('\n', cp_lexer_debug_stream);
1076     }
1077 
1078   return token;
1079 }
1080 
1081 /* Return the next token, and advance the lexer's next_token pointer
1082    to point to the next non-purged token.  */
1083 
1084 static cp_token *
cp_lexer_consume_token(cp_lexer * lexer)1085 cp_lexer_consume_token (cp_lexer* lexer)
1086 {
1087   cp_token *token = lexer->next_token;
1088 
1089   gcc_assert (token != &eof_token);
1090   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091 
1092   do
1093     {
1094       lexer->next_token++;
1095       if (lexer->next_token == lexer->last_token)
1096 	{
1097 	  lexer->next_token = &eof_token;
1098 	  break;
1099 	}
1100 
1101     }
1102   while (lexer->next_token->purged_p);
1103 
1104   cp_lexer_set_source_position_from_token (token);
1105 
1106   /* Provide debugging output.  */
1107   if (cp_lexer_debugging_p (lexer))
1108     {
1109       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110       cp_lexer_print_token (cp_lexer_debug_stream, token);
1111       putc ('\n', cp_lexer_debug_stream);
1112     }
1113 
1114   return token;
1115 }
1116 
1117 /* Permanently remove the next token from the token stream, and
1118    advance the next_token pointer to refer to the next non-purged
1119    token.  */
1120 
1121 static void
cp_lexer_purge_token(cp_lexer * lexer)1122 cp_lexer_purge_token (cp_lexer *lexer)
1123 {
1124   cp_token *tok = lexer->next_token;
1125 
1126   gcc_assert (tok != &eof_token);
1127   tok->purged_p = true;
1128   tok->location = UNKNOWN_LOCATION;
1129   tok->u.value = NULL_TREE;
1130   tok->keyword = RID_MAX;
1131 
1132   do
1133     {
1134       tok++;
1135       if (tok == lexer->last_token)
1136 	{
1137 	  tok = &eof_token;
1138 	  break;
1139 	}
1140     }
1141   while (tok->purged_p);
1142   lexer->next_token = tok;
1143 }
1144 
1145 /* Permanently remove all tokens after TOK, up to, but not
1146    including, the token that will be returned next by
1147    cp_lexer_peek_token.  */
1148 
1149 static void
cp_lexer_purge_tokens_after(cp_lexer * lexer,cp_token * tok)1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1151 {
1152   cp_token *peek = lexer->next_token;
1153 
1154   if (peek == &eof_token)
1155     peek = lexer->last_token;
1156 
1157   gcc_assert (tok < peek);
1158 
1159   for ( tok += 1; tok != peek; tok += 1)
1160     {
1161       tok->purged_p = true;
1162       tok->location = UNKNOWN_LOCATION;
1163       tok->u.value = NULL_TREE;
1164       tok->keyword = RID_MAX;
1165     }
1166 }
1167 
1168 /* Begin saving tokens.  All tokens consumed after this point will be
1169    preserved.  */
1170 
1171 static void
cp_lexer_save_tokens(cp_lexer * lexer)1172 cp_lexer_save_tokens (cp_lexer* lexer)
1173 {
1174   /* Provide debugging output.  */
1175   if (cp_lexer_debugging_p (lexer))
1176     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1177 
1178   lexer->saved_tokens.safe_push (lexer->next_token);
1179 }
1180 
1181 /* Commit to the portion of the token stream most recently saved.  */
1182 
1183 static void
cp_lexer_commit_tokens(cp_lexer * lexer)1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1185 {
1186   /* Provide debugging output.  */
1187   if (cp_lexer_debugging_p (lexer))
1188     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1189 
1190   lexer->saved_tokens.pop ();
1191 }
1192 
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194    to the token stream.  Stop saving tokens.  */
1195 
1196 static void
cp_lexer_rollback_tokens(cp_lexer * lexer)1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1198 {
1199   /* Provide debugging output.  */
1200   if (cp_lexer_debugging_p (lexer))
1201     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1202 
1203   lexer->next_token = lexer->saved_tokens.pop ();
1204 }
1205 
1206 /* RAII wrapper around the above functions, with sanity checking.  Creating
1207    a variable saves tokens, which are committed when the variable is
1208    destroyed unless they are explicitly rolled back by calling the rollback
1209    member function.  */
1210 
1211 struct saved_token_sentinel
1212 {
1213   cp_lexer *lexer;
1214   unsigned len;
1215   bool commit;
saved_token_sentinelsaved_token_sentinel1216   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1217   {
1218     len = lexer->saved_tokens.length ();
1219     cp_lexer_save_tokens (lexer);
1220   }
rollbacksaved_token_sentinel1221   void rollback ()
1222   {
1223     cp_lexer_rollback_tokens (lexer);
1224     commit = false;
1225   }
~saved_token_sentinelsaved_token_sentinel1226   ~saved_token_sentinel()
1227   {
1228     if (commit)
1229       cp_lexer_commit_tokens (lexer);
1230     gcc_assert (lexer->saved_tokens.length () == len);
1231   }
1232 };
1233 
1234 /* Print a representation of the TOKEN on the STREAM.  */
1235 
1236 static void
cp_lexer_print_token(FILE * stream,cp_token * token)1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1238 {
1239   /* We don't use cpp_type2name here because the parser defines
1240      a few tokens of its own.  */
1241   static const char *const token_names[] = {
1242     /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245     TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248     /* C++ parser token types - see "Manifest constants", above.  */
1249     "KEYWORD",
1250     "TEMPLATE_ID",
1251     "NESTED_NAME_SPECIFIER",
1252   };
1253 
1254   /* For some tokens, print the associated data.  */
1255   switch (token->type)
1256     {
1257     case CPP_KEYWORD:
1258       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 	 For example, `struct' is mapped to an INTEGER_CST.  */
1260       if (!identifier_p (token->u.value))
1261 	break;
1262       /* fall through */
1263     case CPP_NAME:
1264       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265       break;
1266 
1267     case CPP_STRING:
1268     case CPP_STRING16:
1269     case CPP_STRING32:
1270     case CPP_WSTRING:
1271     case CPP_UTF8STRING:
1272       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273       break;
1274 
1275     case CPP_NUMBER:
1276       print_generic_expr (stream, token->u.value);
1277       break;
1278 
1279     default:
1280       /* If we have a name for the token, print it out.  Otherwise, we
1281 	 simply give the numeric code.  */
1282       if (token->type < ARRAY_SIZE(token_names))
1283 	fputs (token_names[token->type], stream);
1284       else
1285 	fprintf (stream, "[%d]", token->type);
1286       break;
1287     }
1288 }
1289 
1290 DEBUG_FUNCTION void
debug(cp_token & ref)1291 debug (cp_token &ref)
1292 {
1293   cp_lexer_print_token (stderr, &ref);
1294   fprintf (stderr, "\n");
1295 }
1296 
1297 DEBUG_FUNCTION void
debug(cp_token * ptr)1298 debug (cp_token *ptr)
1299 {
1300   if (ptr)
1301     debug (*ptr);
1302   else
1303     fprintf (stderr, "<nil>\n");
1304 }
1305 
1306 
1307 /* Start emitting debugging information.  */
1308 
1309 static void
cp_lexer_start_debugging(cp_lexer * lexer)1310 cp_lexer_start_debugging (cp_lexer* lexer)
1311 {
1312   if (!LEXER_DEBUGGING_ENABLED_P)
1313     fatal_error (input_location,
1314 		 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1315 
1316   lexer->debugging_p = true;
1317   cp_lexer_debug_stream = stderr;
1318 }
1319 
1320 /* Stop emitting debugging information.  */
1321 
1322 static void
cp_lexer_stop_debugging(cp_lexer * lexer)1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1324 {
1325   if (!LEXER_DEBUGGING_ENABLED_P)
1326     fatal_error (input_location,
1327 		 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1328 
1329   lexer->debugging_p = false;
1330   cp_lexer_debug_stream = NULL;
1331 }
1332 
1333 /* Create a new cp_token_cache, representing a range of tokens.  */
1334 
1335 static cp_token_cache *
cp_token_cache_new(cp_token * first,cp_token * last)1336 cp_token_cache_new (cp_token *first, cp_token *last)
1337 {
1338   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339   cache->first = first;
1340   cache->last = last;
1341   return cache;
1342 }
1343 
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345    by function declaration or definition.  */
1346 
1347 static inline void
cp_ensure_no_omp_declare_simd(cp_parser * parser)1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1349 {
1350   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1351     {
1352       error ("%<#pragma omp declare simd%> not immediately followed by "
1353 	     "function declaration or definition");
1354       parser->omp_declare_simd = NULL;
1355     }
1356 }
1357 
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359    and put that into "omp declare simd" attribute.  */
1360 
1361 static inline void
cp_finalize_omp_declare_simd(cp_parser * parser,tree fndecl)1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1363 {
1364   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1365     {
1366       if (fndecl == error_mark_node)
1367 	{
1368 	  parser->omp_declare_simd = NULL;
1369 	  return;
1370 	}
1371       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1372 	{
1373 	  cp_ensure_no_omp_declare_simd (parser);
1374 	  return;
1375 	}
1376     }
1377 }
1378 
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380    declaration or definition.  */
1381 
1382 static inline void
cp_ensure_no_oacc_routine(cp_parser * parser)1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1384 {
1385   if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1386     {
1387       error_at (parser->oacc_routine->loc,
1388 		"%<#pragma acc routine%> not immediately followed by "
1389 		"function declaration or definition");
1390       parser->oacc_routine = NULL;
1391     }
1392 }
1393 
1394 /* Decl-specifiers.  */
1395 
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1397 
1398 static void
clear_decl_specs(cp_decl_specifier_seq * decl_specs)1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 {
1401   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1402 }
1403 
1404 /* Declarators.  */
1405 
1406 /* Nothing other than the parser should be creating declarators;
1407    declarators are a semi-syntactic representation of C++ entities.
1408    Other parts of the front end that need to create entities (like
1409    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1410 
1411 static cp_declarator *make_call_declarator
1412   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414   (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416   (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418   (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420   (cp_cv_quals, tree, cp_declarator *, tree);
1421 
1422 /* An erroneous declarator.  */
1423 static cp_declarator *cp_error_declarator;
1424 
1425 /* The obstack on which declarators and related data structures are
1426    allocated.  */
1427 static struct obstack declarator_obstack;
1428 
1429 /* Alloc BYTES from the declarator memory pool.  */
1430 
1431 static inline void *
alloc_declarator(size_t bytes)1432 alloc_declarator (size_t bytes)
1433 {
1434   return obstack_alloc (&declarator_obstack, bytes);
1435 }
1436 
1437 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1438    common to all declarators.  */
1439 
1440 static cp_declarator *
make_declarator(cp_declarator_kind kind)1441 make_declarator (cp_declarator_kind kind)
1442 {
1443   cp_declarator *declarator;
1444 
1445   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446   declarator->kind = kind;
1447   declarator->parenthesized = UNKNOWN_LOCATION;
1448   declarator->attributes = NULL_TREE;
1449   declarator->std_attributes = NULL_TREE;
1450   declarator->declarator = NULL;
1451   declarator->parameter_pack_p = false;
1452   declarator->id_loc = UNKNOWN_LOCATION;
1453 
1454   return declarator;
1455 }
1456 
1457 /* Make a declarator for a generalized identifier.  If
1458    QUALIFYING_SCOPE is non-NULL, the identifier is
1459    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1461    is, if any.   */
1462 
1463 static cp_declarator *
make_id_declarator(tree qualifying_scope,tree unqualified_name,special_function_kind sfk)1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 		    special_function_kind sfk)
1466 {
1467   cp_declarator *declarator;
1468 
1469   /* It is valid to write:
1470 
1471        class C { void f(); };
1472        typedef C D;
1473        void D::f();
1474 
1475      The standard is not clear about whether `typedef const C D' is
1476      legal; as of 2002-09-15 the committee is considering that
1477      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1478      well.  */
1479   if (qualifying_scope && TYPE_P (qualifying_scope))
1480     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481 
1482   gcc_assert (identifier_p (unqualified_name)
1483 	      || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 	      || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485 
1486   declarator = make_declarator (cdk_id);
1487   declarator->u.id.qualifying_scope = qualifying_scope;
1488   declarator->u.id.unqualified_name = unqualified_name;
1489   declarator->u.id.sfk = sfk;
1490 
1491   return declarator;
1492 }
1493 
1494 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1495    of modifiers such as const or volatile to apply to the pointer
1496    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1497    appertain to the pointer or reference.  */
1498 
1499 cp_declarator *
make_pointer_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,tree attributes)1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 			 tree attributes)
1502 {
1503   cp_declarator *declarator;
1504 
1505   declarator = make_declarator (cdk_pointer);
1506   declarator->declarator = target;
1507   declarator->u.pointer.qualifiers = cv_qualifiers;
1508   declarator->u.pointer.class_type = NULL_TREE;
1509   if (target)
1510     {
1511       declarator->id_loc = target->id_loc;
1512       declarator->parameter_pack_p = target->parameter_pack_p;
1513       target->parameter_pack_p = false;
1514     }
1515   else
1516     declarator->parameter_pack_p = false;
1517 
1518   declarator->std_attributes = attributes;
1519 
1520   return declarator;
1521 }
1522 
1523 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1524    represent the attributes that appertain to the pointer or
1525    reference.  */
1526 
1527 cp_declarator *
make_reference_declarator(cp_cv_quals cv_qualifiers,cp_declarator * target,bool rvalue_ref,tree attributes)1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 			   bool rvalue_ref, tree attributes)
1530 {
1531   cp_declarator *declarator;
1532 
1533   declarator = make_declarator (cdk_reference);
1534   declarator->declarator = target;
1535   declarator->u.reference.qualifiers = cv_qualifiers;
1536   declarator->u.reference.rvalue_ref = rvalue_ref;
1537   if (target)
1538     {
1539       declarator->id_loc = target->id_loc;
1540       declarator->parameter_pack_p = target->parameter_pack_p;
1541       target->parameter_pack_p = false;
1542     }
1543   else
1544     declarator->parameter_pack_p = false;
1545 
1546   declarator->std_attributes = attributes;
1547 
1548   return declarator;
1549 }
1550 
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1553    appertain to the pointer or reference.  */
1554 
1555 cp_declarator *
make_ptrmem_declarator(cp_cv_quals cv_qualifiers,tree class_type,cp_declarator * pointee,tree attributes)1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 			cp_declarator *pointee,
1558 			tree attributes)
1559 {
1560   cp_declarator *declarator;
1561 
1562   declarator = make_declarator (cdk_ptrmem);
1563   declarator->declarator = pointee;
1564   declarator->u.pointer.qualifiers = cv_qualifiers;
1565   declarator->u.pointer.class_type = class_type;
1566 
1567   if (pointee)
1568     {
1569       declarator->parameter_pack_p = pointee->parameter_pack_p;
1570       pointee->parameter_pack_p = false;
1571     }
1572   else
1573     declarator->parameter_pack_p = false;
1574 
1575   declarator->std_attributes = attributes;
1576 
1577   return declarator;
1578 }
1579 
1580 /* Make a declarator for the function given by TARGET, with the
1581    indicated PARMS.  The CV_QUALIFIERS apply to the function, as in
1582    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1583    indicates what exceptions can be thrown.  */
1584 
1585 cp_declarator *
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)1586 make_call_declarator (cp_declarator *target,
1587 		      tree parms,
1588 		      cp_cv_quals cv_qualifiers,
1589 		      cp_virt_specifiers virt_specifiers,
1590 		      cp_ref_qualifier ref_qualifier,
1591 		      tree tx_qualifier,
1592 		      tree exception_specification,
1593 		      tree late_return_type,
1594 		      tree requires_clause)
1595 {
1596   cp_declarator *declarator;
1597 
1598   declarator = make_declarator (cdk_function);
1599   declarator->declarator = target;
1600   declarator->u.function.parameters = parms;
1601   declarator->u.function.qualifiers = cv_qualifiers;
1602   declarator->u.function.virt_specifiers = virt_specifiers;
1603   declarator->u.function.ref_qualifier = ref_qualifier;
1604   declarator->u.function.tx_qualifier = tx_qualifier;
1605   declarator->u.function.exception_specification = exception_specification;
1606   declarator->u.function.late_return_type = late_return_type;
1607   declarator->u.function.requires_clause = requires_clause;
1608   if (target)
1609     {
1610       declarator->id_loc = target->id_loc;
1611       declarator->parameter_pack_p = target->parameter_pack_p;
1612       target->parameter_pack_p = false;
1613     }
1614   else
1615     declarator->parameter_pack_p = false;
1616 
1617   return declarator;
1618 }
1619 
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621    defined by ELEMENT.  */
1622 
1623 cp_declarator *
make_array_declarator(cp_declarator * element,tree bounds)1624 make_array_declarator (cp_declarator *element, tree bounds)
1625 {
1626   cp_declarator *declarator;
1627 
1628   declarator = make_declarator (cdk_array);
1629   declarator->declarator = element;
1630   declarator->u.array.bounds = bounds;
1631   if (element)
1632     {
1633       declarator->id_loc = element->id_loc;
1634       declarator->parameter_pack_p = element->parameter_pack_p;
1635       element->parameter_pack_p = false;
1636     }
1637   else
1638     declarator->parameter_pack_p = false;
1639 
1640   return declarator;
1641 }
1642 
1643 /* Determine whether the declarator we've seen so far can be a
1644    parameter pack, when followed by an ellipsis.  */
1645 static bool
declarator_can_be_parameter_pack(cp_declarator * declarator)1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1647 {
1648   if (declarator && declarator->parameter_pack_p)
1649     /* We already saw an ellipsis.  */
1650     return false;
1651 
1652   /* Search for a declarator name, or any other declarator that goes
1653      after the point where the ellipsis could appear in a parameter
1654      pack. If we find any of these, then this declarator can not be
1655      made into a parameter pack.  */
1656   bool found = false;
1657   while (declarator && !found)
1658     {
1659       switch ((int)declarator->kind)
1660 	{
1661 	case cdk_id:
1662 	case cdk_array:
1663 	case cdk_decomp:
1664 	  found = true;
1665 	  break;
1666 
1667 	case cdk_error:
1668 	  return true;
1669 
1670 	default:
1671 	  declarator = declarator->declarator;
1672 	  break;
1673 	}
1674     }
1675 
1676   return !found;
1677 }
1678 
1679 cp_parameter_declarator *no_parameters;
1680 
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682    DECLARATOR and DEFAULT_ARGUMENT.  */
1683 
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 			   cp_declarator *declarator,
1687 			   tree default_argument,
1688 			   location_t loc,
1689 			   bool template_parameter_pack_p = false)
1690 {
1691   cp_parameter_declarator *parameter;
1692 
1693   parameter = ((cp_parameter_declarator *)
1694 	       alloc_declarator (sizeof (cp_parameter_declarator)));
1695   parameter->next = NULL;
1696   if (decl_specifiers)
1697     parameter->decl_specifiers = *decl_specifiers;
1698   else
1699     clear_decl_specs (&parameter->decl_specifiers);
1700   parameter->declarator = declarator;
1701   parameter->default_argument = default_argument;
1702   parameter->template_parameter_pack_p = template_parameter_pack_p;
1703   parameter->loc = loc;
1704 
1705   return parameter;
1706 }
1707 
1708 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1709 
1710 static bool
function_declarator_p(const cp_declarator * declarator)1711 function_declarator_p (const cp_declarator *declarator)
1712 {
1713   while (declarator)
1714     {
1715       if (declarator->kind == cdk_function
1716 	  && declarator->declarator->kind == cdk_id)
1717 	return true;
1718       if (declarator->kind == cdk_id
1719 	  || declarator->kind == cdk_decomp
1720 	  || declarator->kind == cdk_error)
1721 	return false;
1722       declarator = declarator->declarator;
1723     }
1724   return false;
1725 }
1726 
1727 /* The parser.  */
1728 
1729 /* Overview
1730    --------
1731 
1732    A cp_parser parses the token stream as specified by the C++
1733    grammar.  Its job is purely parsing, not semantic analysis.  For
1734    example, the parser breaks the token stream into declarators,
1735    expressions, statements, and other similar syntactic constructs.
1736    It does not check that the types of the expressions on either side
1737    of an assignment-statement are compatible, or that a function is
1738    not declared with a parameter of type `void'.
1739 
1740    The parser invokes routines elsewhere in the compiler to perform
1741    semantic analysis and to build up the abstract syntax tree for the
1742    code processed.
1743 
1744    The parser (and the template instantiation code, which is, in a
1745    way, a close relative of parsing) are the only parts of the
1746    compiler that should be calling push_scope and pop_scope, or
1747    related functions.  The parser (and template instantiation code)
1748    keeps track of what scope is presently active; everything else
1749    should simply honor that.  (The code that generates static
1750    initializers may also need to set the scope, in order to check
1751    access control correctly when emitting the initializers.)
1752 
1753    Methodology
1754    -----------
1755 
1756    The parser is of the standard recursive-descent variety.  Upcoming
1757    tokens in the token stream are examined in order to determine which
1758    production to use when parsing a non-terminal.  Some C++ constructs
1759    require arbitrary look ahead to disambiguate.  For example, it is
1760    impossible, in the general case, to tell whether a statement is an
1761    expression or declaration without scanning the entire statement.
1762    Therefore, the parser is capable of "parsing tentatively."  When the
1763    parser is not sure what construct comes next, it enters this mode.
1764    Then, while we attempt to parse the construct, the parser queues up
1765    error messages, rather than issuing them immediately, and saves the
1766    tokens it consumes.  If the construct is parsed successfully, the
1767    parser "commits", i.e., it issues any queued error messages and
1768    the tokens that were being preserved are permanently discarded.
1769    If, however, the construct is not parsed successfully, the parser
1770    rolls back its state completely so that it can resume parsing using
1771    a different alternative.
1772 
1773    Future Improvements
1774    -------------------
1775 
1776    The performance of the parser could probably be improved substantially.
1777    We could often eliminate the need to parse tentatively by looking ahead
1778    a little bit.  In some places, this approach might not entirely eliminate
1779    the need to parse tentatively, but it might still speed up the average
1780    case.  */
1781 
1782 /* Flags that are passed to some parsing functions.  These values can
1783    be bitwise-ored together.  */
1784 
1785 enum
1786 {
1787   /* No flags.  */
1788   CP_PARSER_FLAGS_NONE = 0x0,
1789   /* The construct is optional.  If it is not present, then no error
1790      should be issued.  */
1791   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792   /* When parsing a type-specifier, treat user-defined type-names
1793      as non-type identifiers.  */
1794   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795   /* When parsing a type-specifier, do not try to parse a class-specifier
1796      or enum-specifier.  */
1797   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798   /* When parsing a decl-specifier-seq, only allow type-specifier or
1799      constexpr.  */
1800   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801   /* When parsing a decl-specifier-seq, only allow mutable or constexpr.  */
1802   CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1803 };
1804 
1805 /* This type is used for parameters and variables which hold
1806    combinations of the above flags.  */
1807 typedef int cp_parser_flags;
1808 
1809 /* The different kinds of declarators we want to parse.  */
1810 
1811 enum cp_parser_declarator_kind
1812 {
1813   /* We want an abstract declarator.  */
1814   CP_PARSER_DECLARATOR_ABSTRACT,
1815   /* We want a named declarator.  */
1816   CP_PARSER_DECLARATOR_NAMED,
1817   /* We don't mind, but the name must be an unqualified-id.  */
1818   CP_PARSER_DECLARATOR_EITHER
1819 };
1820 
1821 /* The precedence values used to parse binary expressions.  The minimum value
1822    of PREC must be 1, because zero is reserved to quickly discriminate
1823    binary operators from other tokens.  */
1824 
1825 enum cp_parser_prec
1826 {
1827   PREC_NOT_OPERATOR,
1828   PREC_LOGICAL_OR_EXPRESSION,
1829   PREC_LOGICAL_AND_EXPRESSION,
1830   PREC_INCLUSIVE_OR_EXPRESSION,
1831   PREC_EXCLUSIVE_OR_EXPRESSION,
1832   PREC_AND_EXPRESSION,
1833   PREC_EQUALITY_EXPRESSION,
1834   PREC_RELATIONAL_EXPRESSION,
1835   PREC_SHIFT_EXPRESSION,
1836   PREC_ADDITIVE_EXPRESSION,
1837   PREC_MULTIPLICATIVE_EXPRESSION,
1838   PREC_PM_EXPRESSION,
1839   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841 
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843    precedence value.  */
1844 
1845 struct cp_parser_binary_operations_map_node
1846 {
1847   /* The token type.  */
1848   enum cpp_ttype token_type;
1849   /* The corresponding tree code.  */
1850   enum tree_code tree_type;
1851   /* The precedence of this operator.  */
1852   enum cp_parser_prec prec;
1853 };
1854 
1855 struct cp_parser_expression_stack_entry
1856 {
1857   /* Left hand side of the binary operation we are currently
1858      parsing.  */
1859   cp_expr lhs;
1860   /* Original tree code for left hand side, if it was a binary
1861      expression itself (used for -Wparentheses).  */
1862   enum tree_code lhs_type;
1863   /* Tree code for the binary operation we are parsing.  */
1864   enum tree_code tree_type;
1865   /* Precedence of the binary operation we are parsing.  */
1866   enum cp_parser_prec prec;
1867   /* Location of the binary operation we are parsing.  */
1868   location_t loc;
1869 };
1870 
1871 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1872    entries because precedence levels on the stack are monotonically
1873    increasing.  */
1874 typedef struct cp_parser_expression_stack_entry
1875   cp_parser_expression_stack[NUM_PREC_VALUES];
1876 
1877 /* Prototypes.  */
1878 
1879 /* Constructors and destructors.  */
1880 
1881 static cp_parser_context *cp_parser_context_new
1882   (cp_parser_context *);
1883 
1884 /* Class variables.  */
1885 
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887 
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889    Transformed into an associative array (binops_by_token) by
1890    cp_parser_new.  */
1891 
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895 
1896   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899 
1900   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902 
1903   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905 
1906   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910 
1911   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913 
1914   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915 
1916   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917 
1918   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919 
1920   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921 
1922   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924 
1925 /* The same as binops, but initialized by cp_parser_new so that
1926    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1927    for speed.  */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929 
1930 /* Constructors and destructors.  */
1931 
1932 /* Construct a new context.  The context below this one on the stack
1933    is given by NEXT.  */
1934 
1935 static cp_parser_context *
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 bool cp_parser_translation_unit
2027   (cp_parser *);
2028 
2029 /* Expressions [gram.expr]  */
2030 
2031 static cp_expr cp_parser_primary_expression
2032   (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034   (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036   (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038   (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040   (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042   (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046   (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048   (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050   (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051    bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055   (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059   (cp_token *);
2060 static tree cp_parser_new_expression
2061   (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063   (cp_parser *);
2064 static tree cp_parser_new_type_id
2065   (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067   (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069   (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071   (cp_parser *);
2072 static tree cp_parser_delete_expression
2073   (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075   (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079   (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083   (cp_parser *);
2084 static cp_expr cp_parser_expression
2085   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087   (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089   (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091   (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093   (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095   (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097   (cp_parser *, tree);
2098 
2099 /* Statements [gram.stmt.stmt]  */
2100 
2101 static void cp_parser_statement
2102   (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106   (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108   (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110   (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112   (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114   (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116   (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118   (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120   (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122   (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124   (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126   (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128   (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130   (tree, tree);
2131 static tree cp_parser_jump_statement
2132   (cp_parser *);
2133 static void cp_parser_declaration_statement
2134   (cp_parser *);
2135 
2136 static tree cp_parser_implicitly_scoped_statement
2137   (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139   (cp_parser *, bool *, const token_indent_info &);
2140 
2141 /* Declarations [gram.dcl.dcl] */
2142 
2143 static void cp_parser_declaration_seq_opt
2144   (cp_parser *);
2145 static void cp_parser_declaration
2146   (cp_parser *);
2147 static void cp_parser_block_declaration
2148   (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150   (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154   (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156   (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159    int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163   (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165   (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167   (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169   (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171   (cp_parser *);
2172 static void cp_parser_enumerator_list
2173   (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175   (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177   (cp_parser *);
2178 static void cp_parser_namespace_definition
2179   (cp_parser *);
2180 static void cp_parser_namespace_body
2181   (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183   (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185   (cp_parser *);
2186 static bool cp_parser_using_declaration
2187   (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189   (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191   (cp_parser *);
2192 static void cp_parser_asm_definition
2193   (cp_parser *);
2194 static void cp_parser_linkage_specification
2195   (cp_parser *);
2196 static void cp_parser_static_assert
2197   (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199   (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201   (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202 
2203 /* Declarators [gram.dcl.decl] */
2204 
2205 static tree cp_parser_init_declarator
2206   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207    bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211   (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213   (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215   (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217   (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219   (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221   (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223   (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225   (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227   (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229   (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232   (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236   (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238   (cp_parser *, bool *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240   (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242   (cp_parser *, bool);
2243 static void cp_parser_function_body
2244   (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246   (cp_parser *, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248   (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250   (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252   (cp_parser *, bool *);
2253 
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255   (cp_parser *, bool);
2256 
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258   (cp_parser *, tree);
2259 
2260 static tree cp_parser_late_parsing_oacc_routine
2261   (cp_parser *, tree);
2262 
2263 static tree synthesize_implicit_template_parm
2264   (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266   (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268   (cp_parser *);
2269 
2270 /* Classes [gram.class] */
2271 
2272 static tree cp_parser_class_name
2273   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275   (cp_parser *);
2276 static tree cp_parser_class_head
2277   (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279   (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281   (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283   (cp_parser *);
2284 static void cp_parser_member_declaration
2285   (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287   (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289   (cp_parser *);
2290 
2291 /* Derived classes [gram.class.derived] */
2292 
2293 static tree cp_parser_base_clause
2294   (cp_parser *);
2295 static tree cp_parser_base_specifier
2296   (cp_parser *);
2297 
2298 /* Special member functions [gram.special] */
2299 
2300 static tree cp_parser_conversion_function_id
2301   (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303   (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305   (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307   (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309   (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311   (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313   (cp_parser *);
2314 
2315 /* Overloading [gram.over] */
2316 
2317 static cp_expr cp_parser_operator_function_id
2318   (cp_parser *);
2319 static cp_expr cp_parser_operator
2320   (cp_parser *);
2321 
2322 /* Templates [gram.temp] */
2323 
2324 static void cp_parser_template_declaration
2325   (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327   (cp_parser *);
2328 static tree cp_parser_template_parameter
2329   (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331   (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333   (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337   (cp_parser *);
2338 static tree cp_parser_template_argument
2339   (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341   (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343   (cp_parser *);
2344 
2345 /* Exception handling [gram.exception] */
2346 
2347 static tree cp_parser_try_block
2348   (cp_parser *);
2349 static void cp_parser_function_try_block
2350   (cp_parser *);
2351 static void cp_parser_handler_seq
2352   (cp_parser *);
2353 static void cp_parser_handler
2354   (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356   (cp_parser *);
2357 static tree cp_parser_throw_expression
2358   (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360   (cp_parser *);
2361 static tree cp_parser_type_id_list
2362   (cp_parser *);
2363 
2364 /* GNU Extensions */
2365 
2366 static tree cp_parser_asm_specification_opt
2367   (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369   (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371   (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373   (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375   (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377   (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379   (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381   (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383   (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385   (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387   (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389   (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391   (cp_parser *);
2392 static tree cp_parser_std_attribute
2393   (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395   (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397   (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399   (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401   (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403   (cp_parser *);
2404 
2405 /* Concept Extensions */
2406 
2407 static tree cp_parser_requires_clause
2408   (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410   (cp_parser *);
2411 static tree cp_parser_requires_expression
2412   (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414   (cp_parser *);
2415 static tree cp_parser_requirement_body
2416   (cp_parser *);
2417 static tree cp_parser_requirement_list
2418   (cp_parser *);
2419 static tree cp_parser_requirement
2420   (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422   (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424   (cp_parser *);
2425 static tree cp_parser_type_requirement
2426   (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428   (cp_parser *);
2429 
2430 /* Transactional Memory Extensions */
2431 
2432 static tree cp_parser_transaction
2433   (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435   (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437   (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439   (cp_parser *);
2440 
2441 enum pragma_context {
2442   pragma_external,
2443   pragma_member,
2444   pragma_objc_icode,
2445   pragma_stmt,
2446   pragma_compound
2447 };
2448 static bool cp_parser_pragma
2449   (cp_parser *, enum pragma_context, bool *);
2450 
2451 /* Objective-C++ Productions */
2452 
2453 static tree cp_parser_objc_message_receiver
2454   (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456   (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458   (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460   (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462   (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464   (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466   (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468   (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470   (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472   (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474   (cp_parser *);
2475 static void cp_parser_objc_declaration
2476   (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478   (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480   (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482   (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484   (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486   (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488   (cp_parser *) ;
2489 
2490 /* Utility Routines */
2491 
2492 static cp_expr cp_parser_lookup_name
2493   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495   (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497   (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499   (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501   (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503   (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505   (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507   (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511   (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513   (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515   (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519   (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523   (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525   (cp_parser *);
2526 static void cp_parser_save_default_args
2527   (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529   (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531   (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533   (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535   (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537   (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539   (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541   (cp_parser *);
2542 static void cp_parser_set_storage_class
2543   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547   (cp_decl_specifier_seq *decl_specs,
2548    cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550   (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552   (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554   (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556   (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558   (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560   (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562   (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564   (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566   (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568   (cp_token *);
2569 static void cp_parser_check_class_key
2570   (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572   (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574   (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576   (cp_parser *);
2577 static bool cp_parser_cache_group
2578   (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580   (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582   (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584   (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586   (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588   (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590   (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592   (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594   (cp_parser *);
2595 static void cp_parser_error
2596   (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598   (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600   (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602   (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604   (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606   (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608   (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610   (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612   (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614   (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616   (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618   (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620   (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622   (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624   (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626   (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628   (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630   (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632   (cp_token *);
2633 static bool cp_parser_is_string_literal
2634   (cp_token *);
2635 static bool cp_parser_is_keyword
2636   (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638   (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642   (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644   (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646   (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648   (cp_parser *);
2649 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2650 
2651 /* Concept-related syntactic transformations */
2652 
2653 static tree cp_parser_maybe_concept_name       (cp_parser *, tree);
2654 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2655 
2656 // -------------------------------------------------------------------------- //
2657 // Unevaluated Operand Guard
2658 //
2659 // Implementation of an RAII helper for unevaluated operand parsing.
cp_unevaluated()2660 cp_unevaluated::cp_unevaluated ()
2661 {
2662   ++cp_unevaluated_operand;
2663   ++c_inhibit_evaluation_warnings;
2664 }
2665 
~cp_unevaluated()2666 cp_unevaluated::~cp_unevaluated ()
2667 {
2668   --c_inhibit_evaluation_warnings;
2669   --cp_unevaluated_operand;
2670 }
2671 
2672 // -------------------------------------------------------------------------- //
2673 // Tentative Parsing
2674 
2675 /* Returns nonzero if we are parsing tentatively.  */
2676 
2677 static inline bool
cp_parser_parsing_tentatively(cp_parser * parser)2678 cp_parser_parsing_tentatively (cp_parser* parser)
2679 {
2680   return parser->context->next != NULL;
2681 }
2682 
2683 /* Returns nonzero if TOKEN is a string literal.  */
2684 
2685 static bool
cp_parser_is_pure_string_literal(cp_token * token)2686 cp_parser_is_pure_string_literal (cp_token* token)
2687 {
2688   return (token->type == CPP_STRING ||
2689 	  token->type == CPP_STRING16 ||
2690 	  token->type == CPP_STRING32 ||
2691 	  token->type == CPP_WSTRING ||
2692 	  token->type == CPP_UTF8STRING);
2693 }
2694 
2695 /* Returns nonzero if TOKEN is a string literal
2696    of a user-defined string literal.  */
2697 
2698 static bool
cp_parser_is_string_literal(cp_token * token)2699 cp_parser_is_string_literal (cp_token* token)
2700 {
2701   return (cp_parser_is_pure_string_literal (token) ||
2702 	  token->type == CPP_STRING_USERDEF ||
2703 	  token->type == CPP_STRING16_USERDEF ||
2704 	  token->type == CPP_STRING32_USERDEF ||
2705 	  token->type == CPP_WSTRING_USERDEF ||
2706 	  token->type == CPP_UTF8STRING_USERDEF);
2707 }
2708 
2709 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2710 
2711 static bool
cp_parser_is_keyword(cp_token * token,enum rid keyword)2712 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2713 {
2714   return token->keyword == keyword;
2715 }
2716 
2717 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2718    PRAGMA_NONE.  */
2719 
2720 static enum pragma_kind
cp_parser_pragma_kind(cp_token * token)2721 cp_parser_pragma_kind (cp_token *token)
2722 {
2723   if (token->type != CPP_PRAGMA)
2724     return PRAGMA_NONE;
2725   /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2726   return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2727 }
2728 
2729 /* Helper function for cp_parser_error.
2730    Having peeked a token of kind TOK1_KIND that might signify
2731    a conflict marker, peek successor tokens to determine
2732    if we actually do have a conflict marker.
2733    Specifically, we consider a run of 7 '<', '=' or '>' characters
2734    at the start of a line as a conflict marker.
2735    These come through the lexer as three pairs and a single,
2736    e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2737    If it returns true, *OUT_LOC is written to with the location/range
2738    of the marker.  */
2739 
2740 static bool
cp_lexer_peek_conflict_marker(cp_lexer * lexer,enum cpp_ttype tok1_kind,location_t * out_loc)2741 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2742 			       location_t *out_loc)
2743 {
2744   cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2745   if (token2->type != tok1_kind)
2746     return false;
2747   cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2748   if (token3->type != tok1_kind)
2749     return false;
2750   cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2751   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2752     return false;
2753 
2754   /* It must be at the start of the line.  */
2755   location_t start_loc = cp_lexer_peek_token (lexer)->location;
2756   if (LOCATION_COLUMN (start_loc) != 1)
2757     return false;
2758 
2759   /* We have a conflict marker.  Construct a location of the form:
2760        <<<<<<<
2761        ^~~~~~~
2762      with start == caret, finishing at the end of the marker.  */
2763   location_t finish_loc = get_finish (token4->location);
2764   *out_loc = make_location (start_loc, start_loc, finish_loc);
2765 
2766   return true;
2767 }
2768 
2769 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2770    RT_CLOSE_PAREN.  */
2771 
2772 static const char *
get_matching_symbol(required_token token_desc)2773 get_matching_symbol (required_token token_desc)
2774 {
2775   switch (token_desc)
2776     {
2777     default:
2778       gcc_unreachable ();
2779       return "";
2780     case RT_CLOSE_BRACE:
2781       return "{";
2782     case RT_CLOSE_PAREN:
2783       return "(";
2784     }
2785 }
2786 
2787 /* Attempt to convert TOKEN_DESC from a required_token to an
2788    enum cpp_ttype, returning CPP_EOF if there is no good conversion.  */
2789 
2790 static enum cpp_ttype
get_required_cpp_ttype(required_token token_desc)2791 get_required_cpp_ttype (required_token token_desc)
2792 {
2793   switch (token_desc)
2794     {
2795     case RT_SEMICOLON:
2796       return CPP_SEMICOLON;
2797     case RT_OPEN_PAREN:
2798       return CPP_OPEN_PAREN;
2799     case RT_CLOSE_BRACE:
2800       return CPP_CLOSE_BRACE;
2801     case RT_OPEN_BRACE:
2802       return CPP_OPEN_BRACE;
2803     case RT_CLOSE_SQUARE:
2804       return CPP_CLOSE_SQUARE;
2805     case RT_OPEN_SQUARE:
2806       return CPP_OPEN_SQUARE;
2807     case RT_COMMA:
2808       return CPP_COMMA;
2809     case RT_COLON:
2810       return CPP_COLON;
2811     case RT_CLOSE_PAREN:
2812       return CPP_CLOSE_PAREN;
2813 
2814     default:
2815       /* Use CPP_EOF as a "no completions possible" code.  */
2816       return CPP_EOF;
2817     }
2818 }
2819 
2820 
2821 /* Subroutine of cp_parser_error and cp_parser_required_error.
2822 
2823    Issue a diagnostic of the form
2824       FILE:LINE: MESSAGE before TOKEN
2825    where TOKEN is the next token in the input stream.  MESSAGE
2826    (specified by the caller) is usually of the form "expected
2827    OTHER-TOKEN".
2828 
2829    This bypasses the check for tentative passing, and potentially
2830    adds material needed by cp_parser_required_error.
2831 
2832    If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2833    suggesting insertion of the missing token.
2834 
2835    Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2836    have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2837    location.  */
2838 
2839 static void
cp_parser_error_1(cp_parser * parser,const char * gmsgid,required_token missing_token_desc,location_t matching_location)2840 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2841 		   required_token missing_token_desc,
2842 		   location_t matching_location)
2843 {
2844   cp_token *token = cp_lexer_peek_token (parser->lexer);
2845   /* This diagnostic makes more sense if it is tagged to the line
2846      of the token we just peeked at.  */
2847   cp_lexer_set_source_position_from_token (token);
2848 
2849   if (token->type == CPP_PRAGMA)
2850     {
2851       error_at (token->location,
2852 		"%<#pragma%> is not allowed here");
2853       cp_parser_skip_to_pragma_eol (parser, token);
2854       return;
2855     }
2856 
2857   /* If this is actually a conflict marker, report it as such.  */
2858   if (token->type == CPP_LSHIFT
2859       || token->type == CPP_RSHIFT
2860       || token->type == CPP_EQ_EQ)
2861     {
2862       location_t loc;
2863       if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2864 	{
2865 	  error_at (loc, "version control conflict marker in file");
2866 	  return;
2867 	}
2868     }
2869 
2870   gcc_rich_location richloc (input_location);
2871 
2872   bool added_matching_location = false;
2873 
2874   if (missing_token_desc != RT_NONE)
2875     {
2876       /* Potentially supply a fix-it hint, suggesting to add the
2877 	 missing token immediately after the *previous* token.
2878 	 This may move the primary location within richloc.  */
2879       enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2880       location_t prev_token_loc
2881 	= cp_lexer_previous_token (parser->lexer)->location;
2882       maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2883 
2884       /* If matching_location != UNKNOWN_LOCATION, highlight it.
2885 	 Attempt to consolidate diagnostics by printing it as a
2886 	secondary range within the main diagnostic.  */
2887       if (matching_location != UNKNOWN_LOCATION)
2888 	added_matching_location
2889 	  = richloc.add_location_if_nearby (matching_location);
2890     }
2891 
2892   /* Actually emit the error.  */
2893   c_parse_error (gmsgid,
2894 		 /* Because c_parser_error does not understand
2895 		    CPP_KEYWORD, keywords are treated like
2896 		    identifiers.  */
2897 		 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2898 		 token->u.value, token->flags, &richloc);
2899 
2900   if (missing_token_desc != RT_NONE)
2901     {
2902       /* If we weren't able to consolidate matching_location, then
2903 	 print it as a secondary diagnostic.  */
2904       if (matching_location != UNKNOWN_LOCATION
2905 	  && !added_matching_location)
2906 	inform (matching_location, "to match this %qs",
2907 		get_matching_symbol (missing_token_desc));
2908     }
2909 }
2910 
2911 /* If not parsing tentatively, issue a diagnostic of the form
2912       FILE:LINE: MESSAGE before TOKEN
2913    where TOKEN is the next token in the input stream.  MESSAGE
2914    (specified by the caller) is usually of the form "expected
2915    OTHER-TOKEN".  */
2916 
2917 static void
cp_parser_error(cp_parser * parser,const char * gmsgid)2918 cp_parser_error (cp_parser* parser, const char* gmsgid)
2919 {
2920   if (!cp_parser_simulate_error (parser))
2921     cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2922 }
2923 
2924 /* Issue an error about name-lookup failing.  NAME is the
2925    IDENTIFIER_NODE DECL is the result of
2926    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2927    the thing that we hoped to find.  */
2928 
2929 static void
cp_parser_name_lookup_error(cp_parser * parser,tree name,tree decl,name_lookup_error desired,location_t location)2930 cp_parser_name_lookup_error (cp_parser* parser,
2931 			     tree name,
2932 			     tree decl,
2933 			     name_lookup_error desired,
2934 			     location_t location)
2935 {
2936   /* If name lookup completely failed, tell the user that NAME was not
2937      declared.  */
2938   if (decl == error_mark_node)
2939     {
2940       if (parser->scope && parser->scope != global_namespace)
2941 	error_at (location, "%<%E::%E%> has not been declared",
2942 		  parser->scope, name);
2943       else if (parser->scope == global_namespace)
2944 	error_at (location, "%<::%E%> has not been declared", name);
2945       else if (parser->object_scope
2946 	       && !CLASS_TYPE_P (parser->object_scope))
2947 	error_at (location, "request for member %qE in non-class type %qT",
2948 		  name, parser->object_scope);
2949       else if (parser->object_scope)
2950 	error_at (location, "%<%T::%E%> has not been declared",
2951 		  parser->object_scope, name);
2952       else
2953 	error_at (location, "%qE has not been declared", name);
2954     }
2955   else if (parser->scope && parser->scope != global_namespace)
2956     {
2957       switch (desired)
2958 	{
2959 	  case NLE_TYPE:
2960 	    error_at (location, "%<%E::%E%> is not a type",
2961 	    			parser->scope, name);
2962 	    break;
2963 	  case NLE_CXX98:
2964 	    error_at (location, "%<%E::%E%> is not a class or namespace",
2965 	    			parser->scope, name);
2966 	    break;
2967 	  case NLE_NOT_CXX98:
2968 	    error_at (location,
2969 	    	      "%<%E::%E%> is not a class, namespace, or enumeration",
2970 		      parser->scope, name);
2971 	    break;
2972 	  default:
2973 	    gcc_unreachable ();
2974 
2975 	}
2976     }
2977   else if (parser->scope == global_namespace)
2978     {
2979       switch (desired)
2980 	{
2981 	  case NLE_TYPE:
2982 	    error_at (location, "%<::%E%> is not a type", name);
2983 	    break;
2984 	  case NLE_CXX98:
2985 	    error_at (location, "%<::%E%> is not a class or namespace", name);
2986 	    break;
2987 	  case NLE_NOT_CXX98:
2988 	    error_at (location,
2989 		      "%<::%E%> is not a class, namespace, or enumeration",
2990 		      name);
2991 	    break;
2992 	  default:
2993 	    gcc_unreachable ();
2994 	}
2995     }
2996   else
2997     {
2998       switch (desired)
2999 	{
3000 	  case NLE_TYPE:
3001 	    error_at (location, "%qE is not a type", name);
3002 	    break;
3003 	  case NLE_CXX98:
3004 	    error_at (location, "%qE is not a class or namespace", name);
3005 	    break;
3006 	  case NLE_NOT_CXX98:
3007 	    error_at (location,
3008 		      "%qE is not a class, namespace, or enumeration", name);
3009 	    break;
3010 	  default:
3011 	    gcc_unreachable ();
3012 	}
3013     }
3014 }
3015 
3016 /* If we are parsing tentatively, remember that an error has occurred
3017    during this tentative parse.  Returns true if the error was
3018    simulated; false if a message should be issued by the caller.  */
3019 
3020 static bool
cp_parser_simulate_error(cp_parser * parser)3021 cp_parser_simulate_error (cp_parser* parser)
3022 {
3023   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3024     {
3025       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3026       return true;
3027     }
3028   return false;
3029 }
3030 
3031 /* This function is called when a type is defined.  If type
3032    definitions are forbidden at this point, an error message is
3033    issued.  */
3034 
3035 static bool
cp_parser_check_type_definition(cp_parser * parser)3036 cp_parser_check_type_definition (cp_parser* parser)
3037 {
3038   /* If types are forbidden here, issue a message.  */
3039   if (parser->type_definition_forbidden_message)
3040     {
3041       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3042 	 in the message need to be interpreted.  */
3043       error (parser->type_definition_forbidden_message);
3044       return false;
3045     }
3046   return true;
3047 }
3048 
3049 /* This function is called when the DECLARATOR is processed.  The TYPE
3050    was a type defined in the decl-specifiers.  If it is invalid to
3051    define a type in the decl-specifiers for DECLARATOR, an error is
3052    issued. TYPE_LOCATION is the location of TYPE and is used
3053    for error reporting.  */
3054 
3055 static void
cp_parser_check_for_definition_in_return_type(cp_declarator * declarator,tree type,location_t type_location)3056 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3057 					       tree type, location_t type_location)
3058 {
3059   /* [dcl.fct] forbids type definitions in return types.
3060      Unfortunately, it's not easy to know whether or not we are
3061      processing a return type until after the fact.  */
3062   while (declarator
3063 	 && (declarator->kind == cdk_pointer
3064 	     || declarator->kind == cdk_reference
3065 	     || declarator->kind == cdk_ptrmem))
3066     declarator = declarator->declarator;
3067   if (declarator
3068       && declarator->kind == cdk_function)
3069     {
3070       error_at (type_location,
3071 		"new types may not be defined in a return type");
3072       inform (type_location,
3073 	      "(perhaps a semicolon is missing after the definition of %qT)",
3074 	      type);
3075     }
3076 }
3077 
3078 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3079    "<" in any valid C++ program.  If the next token is indeed "<",
3080    issue a message warning the user about what appears to be an
3081    invalid attempt to form a template-id. LOCATION is the location
3082    of the type-specifier (TYPE) */
3083 
3084 static void
cp_parser_check_for_invalid_template_id(cp_parser * parser,tree type,enum tag_types tag_type,location_t location)3085 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3086 					 tree type,
3087 					 enum tag_types tag_type,
3088 					 location_t location)
3089 {
3090   cp_token_position start = 0;
3091 
3092   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3093     {
3094       if (TREE_CODE (type) == TYPE_DECL)
3095 	type = TREE_TYPE (type);
3096       if (TYPE_P (type) && !template_placeholder_p (type))
3097 	error_at (location, "%qT is not a template", type);
3098       else if (identifier_p (type))
3099 	{
3100 	  if (tag_type != none_type)
3101 	    error_at (location, "%qE is not a class template", type);
3102 	  else
3103 	    error_at (location, "%qE is not a template", type);
3104 	}
3105       else
3106 	error_at (location, "invalid template-id");
3107       /* Remember the location of the invalid "<".  */
3108       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3109 	start = cp_lexer_token_position (parser->lexer, true);
3110       /* Consume the "<".  */
3111       cp_lexer_consume_token (parser->lexer);
3112       /* Parse the template arguments.  */
3113       cp_parser_enclosed_template_argument_list (parser);
3114       /* Permanently remove the invalid template arguments so that
3115 	 this error message is not issued again.  */
3116       if (start)
3117 	cp_lexer_purge_tokens_after (parser->lexer, start);
3118     }
3119 }
3120 
3121 /* If parsing an integral constant-expression, issue an error message
3122    about the fact that THING appeared and return true.  Otherwise,
3123    return false.  In either case, set
3124    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3125 
3126 static bool
cp_parser_non_integral_constant_expression(cp_parser * parser,non_integral_constant thing)3127 cp_parser_non_integral_constant_expression (cp_parser  *parser,
3128 					    non_integral_constant thing)
3129 {
3130   parser->non_integral_constant_expression_p = true;
3131   if (parser->integral_constant_expression_p)
3132     {
3133       if (!parser->allow_non_integral_constant_expression_p)
3134 	{
3135 	  const char *msg = NULL;
3136 	  switch (thing)
3137 	    {
3138   	      case NIC_FLOAT:
3139 		pedwarn (input_location, OPT_Wpedantic,
3140 			 "ISO C++ forbids using a floating-point literal "
3141 			 "in a constant-expression");
3142 		return true;
3143 	      case NIC_CAST:
3144 		error ("a cast to a type other than an integral or "
3145 		       "enumeration type cannot appear in a "
3146 		       "constant-expression");
3147 		return true;
3148 	      case NIC_TYPEID:
3149 		error ("%<typeid%> operator "
3150 		       "cannot appear in a constant-expression");
3151 		return true;
3152 	      case NIC_NCC:
3153 		error ("non-constant compound literals "
3154 		       "cannot appear in a constant-expression");
3155 		return true;
3156 	      case NIC_FUNC_CALL:
3157 		error ("a function call "
3158 		       "cannot appear in a constant-expression");
3159 		return true;
3160 	      case NIC_INC:
3161 		error ("an increment "
3162 		       "cannot appear in a constant-expression");
3163 		return true;
3164 	      case NIC_DEC:
3165 		error ("an decrement "
3166 		       "cannot appear in a constant-expression");
3167 		return true;
3168 	      case NIC_ARRAY_REF:
3169 		error ("an array reference "
3170 		       "cannot appear in a constant-expression");
3171 		return true;
3172 	      case NIC_ADDR_LABEL:
3173 		error ("the address of a label "
3174 		       "cannot appear in a constant-expression");
3175 		return true;
3176 	      case NIC_OVERLOADED:
3177 		error ("calls to overloaded operators "
3178 		       "cannot appear in a constant-expression");
3179 		return true;
3180 	      case NIC_ASSIGNMENT:
3181 		error ("an assignment cannot appear in a constant-expression");
3182 		return true;
3183 	      case NIC_COMMA:
3184 		error ("a comma operator "
3185 		       "cannot appear in a constant-expression");
3186 		return true;
3187 	      case NIC_CONSTRUCTOR:
3188 		error ("a call to a constructor "
3189 		       "cannot appear in a constant-expression");
3190 		return true;
3191 	      case NIC_TRANSACTION:
3192 		error ("a transaction expression "
3193 		       "cannot appear in a constant-expression");
3194 		return true;
3195 	      case NIC_THIS:
3196 		msg = "this";
3197 		break;
3198 	      case NIC_FUNC_NAME:
3199 		msg = "__FUNCTION__";
3200 		break;
3201   	      case NIC_PRETTY_FUNC:
3202 		msg = "__PRETTY_FUNCTION__";
3203 		break;
3204 	      case NIC_C99_FUNC:
3205 		msg = "__func__";
3206 		break;
3207 	      case NIC_VA_ARG:
3208 		msg = "va_arg";
3209 		break;
3210 	      case NIC_ARROW:
3211 		msg = "->";
3212 		break;
3213 	      case NIC_POINT:
3214 		msg = ".";
3215 		break;
3216 	      case NIC_STAR:
3217 		msg = "*";
3218 		break;
3219 	      case NIC_ADDR:
3220 		msg = "&";
3221 		break;
3222 	      case NIC_PREINCREMENT:
3223 		msg = "++";
3224 		break;
3225 	      case NIC_PREDECREMENT:
3226 		msg = "--";
3227 		break;
3228 	      case NIC_NEW:
3229 		msg = "new";
3230 		break;
3231 	      case NIC_DEL:
3232 		msg = "delete";
3233 		break;
3234 	      default:
3235 		gcc_unreachable ();
3236 	    }
3237 	  if (msg)
3238 	    error ("%qs cannot appear in a constant-expression", msg);
3239 	  return true;
3240 	}
3241     }
3242   return false;
3243 }
3244 
3245 /* Emit a diagnostic for an invalid type name.  This function commits
3246    to the current active tentative parse, if any.  (Otherwise, the
3247    problematic construct might be encountered again later, resulting
3248    in duplicate error messages.) LOCATION is the location of ID.  */
3249 
3250 static void
cp_parser_diagnose_invalid_type_name(cp_parser * parser,tree id,location_t location)3251 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3252 				      location_t location)
3253 {
3254   tree decl, ambiguous_decls;
3255   cp_parser_commit_to_tentative_parse (parser);
3256   /* Try to lookup the identifier.  */
3257   decl = cp_parser_lookup_name (parser, id, none_type,
3258 				/*is_template=*/false,
3259 				/*is_namespace=*/false,
3260 				/*check_dependency=*/true,
3261 				&ambiguous_decls, location);
3262   if (ambiguous_decls)
3263     /* If the lookup was ambiguous, an error will already have
3264        been issued.  */
3265     return;
3266   /* If the lookup found a template-name, it means that the user forgot
3267   to specify an argument list. Emit a useful error message.  */
3268   if (DECL_TYPE_TEMPLATE_P (decl))
3269     {
3270       error_at (location,
3271 		"invalid use of template-name %qE without an argument list",
3272 		decl);
3273       if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3274 	inform (location, "class template argument deduction is only available "
3275 		"with -std=c++17 or -std=gnu++17");
3276       inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3277     }
3278   else if (TREE_CODE (id) == BIT_NOT_EXPR)
3279     error_at (location, "invalid use of destructor %qD as a type", id);
3280   else if (TREE_CODE (decl) == TYPE_DECL)
3281     /* Something like 'unsigned A a;'  */
3282     error_at (location, "invalid combination of multiple type-specifiers");
3283   else if (!parser->scope)
3284     {
3285       /* Issue an error message.  */
3286       name_hint hint;
3287       if (TREE_CODE (id) == IDENTIFIER_NODE)
3288 	hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3289       if (hint)
3290 	{
3291 	  gcc_rich_location richloc (location);
3292 	  richloc.add_fixit_replace (hint.suggestion ());
3293 	  error_at (&richloc,
3294 		    "%qE does not name a type; did you mean %qs?",
3295 		    id, hint.suggestion ());
3296 	}
3297       else
3298 	error_at (location, "%qE does not name a type", id);
3299       /* If we're in a template class, it's possible that the user was
3300 	 referring to a type from a base class.  For example:
3301 
3302 	   template <typename T> struct A { typedef T X; };
3303 	   template <typename T> struct B : public A<T> { X x; };
3304 
3305 	 The user should have said "typename A<T>::X".  */
3306       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3307 	inform (location, "C++11 %<constexpr%> only available with "
3308 		"-std=c++11 or -std=gnu++11");
3309       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3310 	inform (location, "C++11 %<noexcept%> only available with "
3311 		"-std=c++11 or -std=gnu++11");
3312       else if (cxx_dialect < cxx11
3313 	       && TREE_CODE (id) == IDENTIFIER_NODE
3314 	       && id_equal (id, "thread_local"))
3315 	inform (location, "C++11 %<thread_local%> only available with "
3316 		"-std=c++11 or -std=gnu++11");
3317       else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3318 	inform (location, "%<concept%> only available with -fconcepts");
3319       else if (processing_template_decl && current_class_type
3320 	       && TYPE_BINFO (current_class_type))
3321 	{
3322 	  tree b;
3323 
3324 	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3325 	       b;
3326 	       b = TREE_CHAIN (b))
3327 	    {
3328 	      tree base_type = BINFO_TYPE (b);
3329 	      if (CLASS_TYPE_P (base_type)
3330 		  && dependent_type_p (base_type))
3331 		{
3332 		  tree field;
3333 		  /* Go from a particular instantiation of the
3334 		     template (which will have an empty TYPE_FIELDs),
3335 		     to the main version.  */
3336 		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3337 		  for (field = TYPE_FIELDS (base_type);
3338 		       field;
3339 		       field = DECL_CHAIN (field))
3340 		    if (TREE_CODE (field) == TYPE_DECL
3341 			&& DECL_NAME (field) == id)
3342 		      {
3343 			inform (location,
3344 				"(perhaps %<typename %T::%E%> was intended)",
3345 				BINFO_TYPE (b), id);
3346 			break;
3347 		      }
3348 		  if (field)
3349 		    break;
3350 		}
3351 	    }
3352 	}
3353     }
3354   /* Here we diagnose qualified-ids where the scope is actually correct,
3355      but the identifier does not resolve to a valid type name.  */
3356   else if (parser->scope != error_mark_node)
3357     {
3358       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3359 	{
3360 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3361 	    error_at (location_of (id),
3362 		      "%qE in namespace %qE does not name a template type",
3363 		      id, parser->scope);
3364 	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3365 	    error_at (location_of (id),
3366 		      "%qE in namespace %qE does not name a template type",
3367 		      TREE_OPERAND (id, 0), parser->scope);
3368 	  else
3369 	    error_at (location_of (id),
3370 		      "%qE in namespace %qE does not name a type",
3371 		      id, parser->scope);
3372 	  if (DECL_P (decl))
3373 	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3374 	  else if (decl == error_mark_node)
3375 	    suggest_alternative_in_explicit_scope (location, id,
3376 						   parser->scope);
3377 	}
3378       else if (CLASS_TYPE_P (parser->scope)
3379 	       && constructor_name_p (id, parser->scope))
3380 	{
3381 	  /* A<T>::A<T>() */
3382 	  error_at (location, "%<%T::%E%> names the constructor, not"
3383 		    " the type", parser->scope, id);
3384 	  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3385 	    error_at (location, "and %qT has no template constructors",
3386 		      parser->scope);
3387 	}
3388       else if (TYPE_P (parser->scope)
3389 	       && dependent_scope_p (parser->scope))
3390 	{
3391 	  if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3392 	    error_at (location,
3393 		      "need %<typename%> before %<%T::%D::%E%> because "
3394 		      "%<%T::%D%> is a dependent scope",
3395 		      TYPE_CONTEXT (parser->scope),
3396 		      TYPENAME_TYPE_FULLNAME (parser->scope),
3397 		      id,
3398 		      TYPE_CONTEXT (parser->scope),
3399 		      TYPENAME_TYPE_FULLNAME (parser->scope));
3400 	  else
3401 	    error_at (location, "need %<typename%> before %<%T::%E%> because "
3402 		      "%qT is a dependent scope",
3403 		      parser->scope, id, parser->scope);
3404 	}
3405       else if (TYPE_P (parser->scope))
3406 	{
3407 	  if (!COMPLETE_TYPE_P (parser->scope))
3408 	    cxx_incomplete_type_error (location_of (id), NULL_TREE,
3409 				       parser->scope);
3410 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3411 	    error_at (location_of (id),
3412 		      "%qE in %q#T does not name a template type",
3413 		      id, parser->scope);
3414 	  else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3415 	    error_at (location_of (id),
3416 		      "%qE in %q#T does not name a template type",
3417 		      TREE_OPERAND (id, 0), parser->scope);
3418 	  else
3419 	    error_at (location_of (id),
3420 		      "%qE in %q#T does not name a type",
3421 		      id, parser->scope);
3422 	  if (DECL_P (decl))
3423 	    inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3424 	}
3425       else
3426 	gcc_unreachable ();
3427     }
3428 }
3429 
3430 /* Check for a common situation where a type-name should be present,
3431    but is not, and issue a sensible error message.  Returns true if an
3432    invalid type-name was detected.
3433 
3434    The situation handled by this function are variable declarations of the
3435    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3436    Usually, `ID' should name a type, but if we got here it means that it
3437    does not. We try to emit the best possible error message depending on
3438    how exactly the id-expression looks like.  */
3439 
3440 static bool
cp_parser_parse_and_diagnose_invalid_type_name(cp_parser * parser)3441 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3442 {
3443   tree id;
3444   cp_token *token = cp_lexer_peek_token (parser->lexer);
3445 
3446   /* Avoid duplicate error about ambiguous lookup.  */
3447   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3448     {
3449       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3450       if (next->type == CPP_NAME && next->error_reported)
3451 	goto out;
3452     }
3453 
3454   cp_parser_parse_tentatively (parser);
3455   id = cp_parser_id_expression (parser,
3456 				/*template_keyword_p=*/false,
3457 				/*check_dependency_p=*/true,
3458 				/*template_p=*/NULL,
3459 				/*declarator_p=*/false,
3460 				/*optional_p=*/false);
3461   /* If the next token is a (, this is a function with no explicit return
3462      type, i.e. constructor, destructor or conversion op.  */
3463   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3464       || TREE_CODE (id) == TYPE_DECL)
3465     {
3466       cp_parser_abort_tentative_parse (parser);
3467       return false;
3468     }
3469   if (!cp_parser_parse_definitely (parser))
3470     return false;
3471 
3472   /* Emit a diagnostic for the invalid type.  */
3473   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3474  out:
3475   /* If we aren't in the middle of a declarator (i.e. in a
3476      parameter-declaration-clause), skip to the end of the declaration;
3477      there's no point in trying to process it.  */
3478   if (!parser->in_declarator_p)
3479     cp_parser_skip_to_end_of_block_or_statement (parser);
3480   return true;
3481 }
3482 
3483 /* Consume tokens up to, and including, the next non-nested closing `)'.
3484    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3485    are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3486    found an unnested token of that type.  */
3487 
3488 static int
cp_parser_skip_to_closing_parenthesis_1(cp_parser * parser,bool recovering,cpp_ttype or_ttype,bool consume_paren)3489 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3490 					 bool recovering,
3491 					 cpp_ttype or_ttype,
3492 					 bool consume_paren)
3493 {
3494   unsigned paren_depth = 0;
3495   unsigned brace_depth = 0;
3496   unsigned square_depth = 0;
3497 
3498   if (recovering && or_ttype == CPP_EOF
3499       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3500     return 0;
3501 
3502   while (true)
3503     {
3504       cp_token * token = cp_lexer_peek_token (parser->lexer);
3505 
3506       /* Have we found what we're looking for before the closing paren?  */
3507       if (token->type == or_ttype && or_ttype != CPP_EOF
3508 	  && !brace_depth && !paren_depth && !square_depth)
3509 	return -1;
3510 
3511       switch (token->type)
3512 	{
3513 	case CPP_EOF:
3514 	case CPP_PRAGMA_EOL:
3515 	  /* If we've run out of tokens, then there is no closing `)'.  */
3516 	  return 0;
3517 
3518         /* This is good for lambda expression capture-lists.  */
3519         case CPP_OPEN_SQUARE:
3520           ++square_depth;
3521           break;
3522         case CPP_CLOSE_SQUARE:
3523           if (!square_depth--)
3524             return 0;
3525           break;
3526 
3527 	case CPP_SEMICOLON:
3528 	  /* This matches the processing in skip_to_end_of_statement.  */
3529 	  if (!brace_depth)
3530 	    return 0;
3531 	  break;
3532 
3533 	case CPP_OPEN_BRACE:
3534 	  ++brace_depth;
3535 	  break;
3536 	case CPP_CLOSE_BRACE:
3537 	  if (!brace_depth--)
3538 	    return 0;
3539 	  break;
3540 
3541 	case CPP_OPEN_PAREN:
3542 	  if (!brace_depth)
3543 	    ++paren_depth;
3544 	  break;
3545 
3546 	case CPP_CLOSE_PAREN:
3547 	  if (!brace_depth && !paren_depth--)
3548 	    {
3549 	      if (consume_paren)
3550 		cp_lexer_consume_token (parser->lexer);
3551 	      return 1;
3552 	    }
3553 	  break;
3554 
3555 	default:
3556 	  break;
3557 	}
3558 
3559       /* Consume the token.  */
3560       cp_lexer_consume_token (parser->lexer);
3561     }
3562 }
3563 
3564 /* Consume tokens up to, and including, the next non-nested closing `)'.
3565    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3566    are doing error recovery. Returns -1 if OR_COMMA is true and we
3567    found an unnested token of that type.  */
3568 
3569 static int
cp_parser_skip_to_closing_parenthesis(cp_parser * parser,bool recovering,bool or_comma,bool consume_paren)3570 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3571 				       bool recovering,
3572 				       bool or_comma,
3573 				       bool consume_paren)
3574 {
3575   cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3576   return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3577 						  ttype, consume_paren);
3578 }
3579 
3580 /* Consume tokens until we reach the end of the current statement.
3581    Normally, that will be just before consuming a `;'.  However, if a
3582    non-nested `}' comes first, then we stop before consuming that.  */
3583 
3584 static void
cp_parser_skip_to_end_of_statement(cp_parser * parser)3585 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3586 {
3587   unsigned nesting_depth = 0;
3588 
3589   /* Unwind generic function template scope if necessary.  */
3590   if (parser->fully_implicit_function_template_p)
3591     abort_fully_implicit_template (parser);
3592 
3593   while (true)
3594     {
3595       cp_token *token = cp_lexer_peek_token (parser->lexer);
3596 
3597       switch (token->type)
3598 	{
3599 	case CPP_EOF:
3600 	case CPP_PRAGMA_EOL:
3601 	  /* If we've run out of tokens, stop.  */
3602 	  return;
3603 
3604 	case CPP_SEMICOLON:
3605 	  /* If the next token is a `;', we have reached the end of the
3606 	     statement.  */
3607 	  if (!nesting_depth)
3608 	    return;
3609 	  break;
3610 
3611 	case CPP_CLOSE_BRACE:
3612 	  /* If this is a non-nested '}', stop before consuming it.
3613 	     That way, when confronted with something like:
3614 
3615 	       { 3 + }
3616 
3617 	     we stop before consuming the closing '}', even though we
3618 	     have not yet reached a `;'.  */
3619 	  if (nesting_depth == 0)
3620 	    return;
3621 
3622 	  /* If it is the closing '}' for a block that we have
3623 	     scanned, stop -- but only after consuming the token.
3624 	     That way given:
3625 
3626 		void f g () { ... }
3627 		typedef int I;
3628 
3629 	     we will stop after the body of the erroneously declared
3630 	     function, but before consuming the following `typedef'
3631 	     declaration.  */
3632 	  if (--nesting_depth == 0)
3633 	    {
3634 	      cp_lexer_consume_token (parser->lexer);
3635 	      return;
3636 	    }
3637 	  break;
3638 
3639 	case CPP_OPEN_BRACE:
3640 	  ++nesting_depth;
3641 	  break;
3642 
3643 	default:
3644 	  break;
3645 	}
3646 
3647       /* Consume the token.  */
3648       cp_lexer_consume_token (parser->lexer);
3649     }
3650 }
3651 
3652 /* This function is called at the end of a statement or declaration.
3653    If the next token is a semicolon, it is consumed; otherwise, error
3654    recovery is attempted.  */
3655 
3656 static void
cp_parser_consume_semicolon_at_end_of_statement(cp_parser * parser)3657 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3658 {
3659   /* Look for the trailing `;'.  */
3660   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3661     {
3662       /* If there is additional (erroneous) input, skip to the end of
3663 	 the statement.  */
3664       cp_parser_skip_to_end_of_statement (parser);
3665       /* If the next token is now a `;', consume it.  */
3666       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3667 	cp_lexer_consume_token (parser->lexer);
3668     }
3669 }
3670 
3671 /* Skip tokens until we have consumed an entire block, or until we
3672    have consumed a non-nested `;'.  */
3673 
3674 static void
cp_parser_skip_to_end_of_block_or_statement(cp_parser * parser)3675 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3676 {
3677   int nesting_depth = 0;
3678 
3679   /* Unwind generic function template scope if necessary.  */
3680   if (parser->fully_implicit_function_template_p)
3681     abort_fully_implicit_template (parser);
3682 
3683   while (nesting_depth >= 0)
3684     {
3685       cp_token *token = cp_lexer_peek_token (parser->lexer);
3686 
3687       switch (token->type)
3688 	{
3689 	case CPP_EOF:
3690 	case CPP_PRAGMA_EOL:
3691 	  /* If we've run out of tokens, stop.  */
3692 	  return;
3693 
3694 	case CPP_SEMICOLON:
3695 	  /* Stop if this is an unnested ';'. */
3696 	  if (!nesting_depth)
3697 	    nesting_depth = -1;
3698 	  break;
3699 
3700 	case CPP_CLOSE_BRACE:
3701 	  /* Stop if this is an unnested '}', or closes the outermost
3702 	     nesting level.  */
3703 	  nesting_depth--;
3704 	  if (nesting_depth < 0)
3705 	    return;
3706 	  if (!nesting_depth)
3707 	    nesting_depth = -1;
3708 	  break;
3709 
3710 	case CPP_OPEN_BRACE:
3711 	  /* Nest. */
3712 	  nesting_depth++;
3713 	  break;
3714 
3715 	default:
3716 	  break;
3717 	}
3718 
3719       /* Consume the token.  */
3720       cp_lexer_consume_token (parser->lexer);
3721     }
3722 }
3723 
3724 /* Skip tokens until a non-nested closing curly brace is the next
3725    token, or there are no more tokens. Return true in the first case,
3726    false otherwise.  */
3727 
3728 static bool
cp_parser_skip_to_closing_brace(cp_parser * parser)3729 cp_parser_skip_to_closing_brace (cp_parser *parser)
3730 {
3731   unsigned nesting_depth = 0;
3732 
3733   while (true)
3734     {
3735       cp_token *token = cp_lexer_peek_token (parser->lexer);
3736 
3737       switch (token->type)
3738 	{
3739 	case CPP_EOF:
3740 	case CPP_PRAGMA_EOL:
3741 	  /* If we've run out of tokens, stop.  */
3742 	  return false;
3743 
3744 	case CPP_CLOSE_BRACE:
3745 	  /* If the next token is a non-nested `}', then we have reached
3746 	     the end of the current block.  */
3747 	  if (nesting_depth-- == 0)
3748 	    return true;
3749 	  break;
3750 
3751 	case CPP_OPEN_BRACE:
3752 	  /* If it the next token is a `{', then we are entering a new
3753 	     block.  Consume the entire block.  */
3754 	  ++nesting_depth;
3755 	  break;
3756 
3757 	default:
3758 	  break;
3759 	}
3760 
3761       /* Consume the token.  */
3762       cp_lexer_consume_token (parser->lexer);
3763     }
3764 }
3765 
3766 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3767    parameter is the PRAGMA token, allowing us to purge the entire pragma
3768    sequence.  */
3769 
3770 static void
cp_parser_skip_to_pragma_eol(cp_parser * parser,cp_token * pragma_tok)3771 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3772 {
3773   cp_token *token;
3774 
3775   parser->lexer->in_pragma = false;
3776 
3777   do
3778     token = cp_lexer_consume_token (parser->lexer);
3779   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3780 
3781   /* Ensure that the pragma is not parsed again.  */
3782   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3783 }
3784 
3785 /* Require pragma end of line, resyncing with it as necessary.  The
3786    arguments are as for cp_parser_skip_to_pragma_eol.  */
3787 
3788 static void
cp_parser_require_pragma_eol(cp_parser * parser,cp_token * pragma_tok)3789 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3790 {
3791   parser->lexer->in_pragma = false;
3792   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3793     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3794 }
3795 
3796 /* This is a simple wrapper around make_typename_type. When the id is
3797    an unresolved identifier node, we can provide a superior diagnostic
3798    using cp_parser_diagnose_invalid_type_name.  */
3799 
3800 static tree
cp_parser_make_typename_type(cp_parser * parser,tree id,location_t id_location)3801 cp_parser_make_typename_type (cp_parser *parser, tree id,
3802 			      location_t id_location)
3803 {
3804   tree result;
3805   if (identifier_p (id))
3806     {
3807       result = make_typename_type (parser->scope, id, typename_type,
3808 				   /*complain=*/tf_none);
3809       if (result == error_mark_node)
3810 	cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3811       return result;
3812     }
3813   return make_typename_type (parser->scope, id, typename_type, tf_error);
3814 }
3815 
3816 /* This is a wrapper around the
3817    make_{pointer,ptrmem,reference}_declarator functions that decides
3818    which one to call based on the CODE and CLASS_TYPE arguments. The
3819    CODE argument should be one of the values returned by
3820    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3821    appertain to the pointer or reference.  */
3822 
3823 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)3824 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3825 				    cp_cv_quals cv_qualifiers,
3826 				    cp_declarator *target,
3827 				    tree attributes)
3828 {
3829   if (code == ERROR_MARK || target == cp_error_declarator)
3830     return cp_error_declarator;
3831 
3832   if (code == INDIRECT_REF)
3833     if (class_type == NULL_TREE)
3834       return make_pointer_declarator (cv_qualifiers, target, attributes);
3835     else
3836       return make_ptrmem_declarator (cv_qualifiers, class_type,
3837 				     target, attributes);
3838   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3839     return make_reference_declarator (cv_qualifiers, target,
3840 				      false, attributes);
3841   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3842     return make_reference_declarator (cv_qualifiers, target,
3843 				      true, attributes);
3844   gcc_unreachable ();
3845 }
3846 
3847 /* Create a new C++ parser.  */
3848 
3849 static cp_parser *
cp_parser_new(void)3850 cp_parser_new (void)
3851 {
3852   cp_parser *parser;
3853   cp_lexer *lexer;
3854   unsigned i;
3855 
3856   /* cp_lexer_new_main is called before doing GC allocation because
3857      cp_lexer_new_main might load a PCH file.  */
3858   lexer = cp_lexer_new_main ();
3859 
3860   /* Initialize the binops_by_token so that we can get the tree
3861      directly from the token.  */
3862   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3863     binops_by_token[binops[i].token_type] = binops[i];
3864 
3865   parser = ggc_cleared_alloc<cp_parser> ();
3866   parser->lexer = lexer;
3867   parser->context = cp_parser_context_new (NULL);
3868 
3869   /* For now, we always accept GNU extensions.  */
3870   parser->allow_gnu_extensions_p = 1;
3871 
3872   /* The `>' token is a greater-than operator, not the end of a
3873      template-id.  */
3874   parser->greater_than_is_operator_p = true;
3875 
3876   parser->default_arg_ok_p = true;
3877 
3878   /* We are not parsing a constant-expression.  */
3879   parser->integral_constant_expression_p = false;
3880   parser->allow_non_integral_constant_expression_p = false;
3881   parser->non_integral_constant_expression_p = false;
3882 
3883   /* Local variable names are not forbidden.  */
3884   parser->local_variables_forbidden_p = false;
3885 
3886   /* We are not processing an `extern "C"' declaration.  */
3887   parser->in_unbraced_linkage_specification_p = false;
3888 
3889   /* We are not processing a declarator.  */
3890   parser->in_declarator_p = false;
3891 
3892   /* We are not processing a template-argument-list.  */
3893   parser->in_template_argument_list_p = false;
3894 
3895   /* We are not in an iteration statement.  */
3896   parser->in_statement = 0;
3897 
3898   /* We are not in a switch statement.  */
3899   parser->in_switch_statement_p = false;
3900 
3901   /* We are not parsing a type-id inside an expression.  */
3902   parser->in_type_id_in_expr_p = false;
3903 
3904   /* Declarations aren't implicitly extern "C".  */
3905   parser->implicit_extern_c = false;
3906 
3907   /* String literals should be translated to the execution character set.  */
3908   parser->translate_strings_p = true;
3909 
3910   /* We are not parsing a function body.  */
3911   parser->in_function_body = false;
3912 
3913   /* We can correct until told otherwise.  */
3914   parser->colon_corrects_to_scope_p = true;
3915 
3916   /* The unparsed function queue is empty.  */
3917   push_unparsed_function_queues (parser);
3918 
3919   /* There are no classes being defined.  */
3920   parser->num_classes_being_defined = 0;
3921 
3922   /* No template parameters apply.  */
3923   parser->num_template_parameter_lists = 0;
3924 
3925   /* Special parsing data structures.  */
3926   parser->omp_declare_simd = NULL;
3927   parser->oacc_routine = NULL;
3928 
3929   /* Not declaring an implicit function template.  */
3930   parser->auto_is_implicit_function_template_parm_p = false;
3931   parser->fully_implicit_function_template_p = false;
3932   parser->implicit_template_parms = 0;
3933   parser->implicit_template_scope = 0;
3934 
3935   /* Allow constrained-type-specifiers. */
3936   parser->prevent_constrained_type_specifiers = 0;
3937 
3938   /* We haven't yet seen an 'extern "C"'.  */
3939   parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3940 
3941   return parser;
3942 }
3943 
3944 /* Create a cp_lexer structure which will emit the tokens in CACHE
3945    and push it onto the parser's lexer stack.  This is used for delayed
3946    parsing of in-class method bodies and default arguments, and should
3947    not be confused with tentative parsing.  */
3948 static void
cp_parser_push_lexer_for_tokens(cp_parser * parser,cp_token_cache * cache)3949 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3950 {
3951   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3952   lexer->next = parser->lexer;
3953   parser->lexer = lexer;
3954 
3955   /* Move the current source position to that of the first token in the
3956      new lexer.  */
3957   cp_lexer_set_source_position_from_token (lexer->next_token);
3958 }
3959 
3960 /* Pop the top lexer off the parser stack.  This is never used for the
3961    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3962 static void
cp_parser_pop_lexer(cp_parser * parser)3963 cp_parser_pop_lexer (cp_parser *parser)
3964 {
3965   cp_lexer *lexer = parser->lexer;
3966   parser->lexer = lexer->next;
3967   cp_lexer_destroy (lexer);
3968 
3969   /* Put the current source position back where it was before this
3970      lexer was pushed.  */
3971   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3972 }
3973 
3974 /* Lexical conventions [gram.lex]  */
3975 
3976 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3977    identifier.  */
3978 
3979 static cp_expr
cp_parser_identifier(cp_parser * parser)3980 cp_parser_identifier (cp_parser* parser)
3981 {
3982   cp_token *token;
3983 
3984   /* Look for the identifier.  */
3985   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3986   /* Return the value.  */
3987   if (token)
3988     return cp_expr (token->u.value, token->location);
3989   else
3990     return error_mark_node;
3991 }
3992 
3993 /* Parse a sequence of adjacent string constants.  Returns a
3994    TREE_STRING representing the combined, nul-terminated string
3995    constant.  If TRANSLATE is true, translate the string to the
3996    execution character set.  If WIDE_OK is true, a wide string is
3997    invalid here.
3998 
3999    C++98 [lex.string] says that if a narrow string literal token is
4000    adjacent to a wide string literal token, the behavior is undefined.
4001    However, C99 6.4.5p4 says that this results in a wide string literal.
4002    We follow C99 here, for consistency with the C front end.
4003 
4004    This code is largely lifted from lex_string() in c-lex.c.
4005 
4006    FUTURE: ObjC++ will need to handle @-strings here.  */
4007 static cp_expr
4008 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4009 			  bool lookup_udlit = true)
4010 {
4011   tree value;
4012   size_t count;
4013   struct obstack str_ob;
4014   cpp_string str, istr, *strs;
4015   cp_token *tok;
4016   enum cpp_ttype type, curr_type;
4017   int have_suffix_p = 0;
4018   tree string_tree;
4019   tree suffix_id = NULL_TREE;
4020   bool curr_tok_is_userdef_p = false;
4021 
4022   tok = cp_lexer_peek_token (parser->lexer);
4023   if (!cp_parser_is_string_literal (tok))
4024     {
4025       cp_parser_error (parser, "expected string-literal");
4026       return error_mark_node;
4027     }
4028 
4029   location_t loc = tok->location;
4030 
4031   if (cpp_userdef_string_p (tok->type))
4032     {
4033       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4034       curr_type = cpp_userdef_string_remove_type (tok->type);
4035       curr_tok_is_userdef_p = true;
4036     }
4037   else
4038     {
4039       string_tree = tok->u.value;
4040       curr_type = tok->type;
4041     }
4042   type = curr_type;
4043 
4044   /* Try to avoid the overhead of creating and destroying an obstack
4045      for the common case of just one string.  */
4046   if (!cp_parser_is_string_literal
4047       (cp_lexer_peek_nth_token (parser->lexer, 2)))
4048     {
4049       cp_lexer_consume_token (parser->lexer);
4050 
4051       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4052       str.len = TREE_STRING_LENGTH (string_tree);
4053       count = 1;
4054 
4055       if (curr_tok_is_userdef_p)
4056 	{
4057 	  suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4058 	  have_suffix_p = 1;
4059 	  curr_type = cpp_userdef_string_remove_type (tok->type);
4060 	}
4061       else
4062 	curr_type = tok->type;
4063 
4064       strs = &str;
4065     }
4066   else
4067     {
4068       location_t last_tok_loc = tok->location;
4069       gcc_obstack_init (&str_ob);
4070       count = 0;
4071 
4072       do
4073 	{
4074 	  cp_lexer_consume_token (parser->lexer);
4075 	  count++;
4076 	  str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4077 	  str.len = TREE_STRING_LENGTH (string_tree);
4078 
4079 	  if (curr_tok_is_userdef_p)
4080 	    {
4081 	      tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4082 	      if (have_suffix_p == 0)
4083 		{
4084 		  suffix_id = curr_suffix_id;
4085 		  have_suffix_p = 1;
4086 		}
4087 	      else if (have_suffix_p == 1
4088 		       && curr_suffix_id != suffix_id)
4089 		{
4090 		  error ("inconsistent user-defined literal suffixes"
4091 			 " %qD and %qD in string literal",
4092 			 suffix_id, curr_suffix_id);
4093 		  have_suffix_p = -1;
4094 		}
4095 	      curr_type = cpp_userdef_string_remove_type (tok->type);
4096 	    }
4097 	  else
4098 	    curr_type = tok->type;
4099 
4100 	  if (type != curr_type)
4101 	    {
4102 	      if (type == CPP_STRING)
4103 		type = curr_type;
4104 	      else if (curr_type != CPP_STRING)
4105 		{
4106 		  rich_location rich_loc (line_table, tok->location);
4107 		  rich_loc.add_range (last_tok_loc, false);
4108 		  error_at (&rich_loc,
4109 			    "unsupported non-standard concatenation "
4110 			    "of string literals");
4111 		}
4112 	    }
4113 
4114 	  obstack_grow (&str_ob, &str, sizeof (cpp_string));
4115 
4116 	  last_tok_loc = tok->location;
4117 
4118 	  tok = cp_lexer_peek_token (parser->lexer);
4119 	  if (cpp_userdef_string_p (tok->type))
4120 	    {
4121 	      string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4122 	      curr_type = cpp_userdef_string_remove_type (tok->type);
4123 	      curr_tok_is_userdef_p = true;
4124 	    }
4125 	  else
4126 	    {
4127 	      string_tree = tok->u.value;
4128 	      curr_type = tok->type;
4129 	      curr_tok_is_userdef_p = false;
4130 	    }
4131 	}
4132       while (cp_parser_is_string_literal (tok));
4133 
4134       /* A string literal built by concatenation has its caret=start at
4135 	 the start of the initial string, and its finish at the finish of
4136 	 the final string literal.  */
4137       loc = make_location (loc, loc, get_finish (last_tok_loc));
4138 
4139       strs = (cpp_string *) obstack_finish (&str_ob);
4140     }
4141 
4142   if (type != CPP_STRING && !wide_ok)
4143     {
4144       cp_parser_error (parser, "a wide string is invalid in this context");
4145       type = CPP_STRING;
4146     }
4147 
4148   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4149       (parse_in, strs, count, &istr, type))
4150     {
4151       value = build_string (istr.len, (const char *)istr.text);
4152       free (CONST_CAST (unsigned char *, istr.text));
4153 
4154       switch (type)
4155 	{
4156 	default:
4157 	case CPP_STRING:
4158 	case CPP_UTF8STRING:
4159 	  TREE_TYPE (value) = char_array_type_node;
4160 	  break;
4161 	case CPP_STRING16:
4162 	  TREE_TYPE (value) = char16_array_type_node;
4163 	  break;
4164 	case CPP_STRING32:
4165 	  TREE_TYPE (value) = char32_array_type_node;
4166 	  break;
4167 	case CPP_WSTRING:
4168 	  TREE_TYPE (value) = wchar_array_type_node;
4169 	  break;
4170 	}
4171 
4172       value = fix_string_type (value);
4173 
4174       if (have_suffix_p)
4175 	{
4176 	  tree literal = build_userdef_literal (suffix_id, value,
4177 						OT_NONE, NULL_TREE);
4178 	  if (lookup_udlit)
4179 	    value = cp_parser_userdef_string_literal (literal);
4180 	  else
4181 	    value = literal;
4182 	}
4183     }
4184   else
4185     /* cpp_interpret_string has issued an error.  */
4186     value = error_mark_node;
4187 
4188   if (count > 1)
4189     obstack_free (&str_ob, 0);
4190 
4191   return cp_expr (value, loc);
4192 }
4193 
4194 /* Look up a literal operator with the name and the exact arguments.  */
4195 
4196 static tree
lookup_literal_operator(tree name,vec<tree,va_gc> * args)4197 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4198 {
4199   tree decl;
4200   decl = lookup_name (name);
4201   if (!decl || !is_overloaded_fn (decl))
4202     return error_mark_node;
4203 
4204   for (lkp_iterator iter (decl); iter; ++iter)
4205     {
4206       unsigned int ix;
4207       bool found = true;
4208       tree fn = *iter;
4209       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4210       if (parmtypes != NULL_TREE)
4211 	{
4212 	  for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4213 	       ++ix, parmtypes = TREE_CHAIN (parmtypes))
4214 	    {
4215 	      tree tparm = TREE_VALUE (parmtypes);
4216 	      tree targ = TREE_TYPE ((*args)[ix]);
4217 	      bool ptr = TYPE_PTR_P (tparm);
4218 	      bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4219 	      if ((ptr || arr || !same_type_p (tparm, targ))
4220 		  && (!ptr || !arr
4221 		      || !same_type_p (TREE_TYPE (tparm),
4222 				       TREE_TYPE (targ))))
4223 		found = false;
4224 	    }
4225 	  if (found
4226 	      && ix == vec_safe_length (args)
4227 	      /* May be this should be sufficient_parms_p instead,
4228 		 depending on how exactly should user-defined literals
4229 		 work in presence of default arguments on the literal
4230 		 operator parameters.  */
4231 	      && parmtypes == void_list_node)
4232 	    return decl;
4233 	}
4234     }
4235 
4236   return error_mark_node;
4237 }
4238 
4239 /* Parse a user-defined char constant.  Returns a call to a user-defined
4240    literal operator taking the character as an argument.  */
4241 
4242 static cp_expr
cp_parser_userdef_char_literal(cp_parser * parser)4243 cp_parser_userdef_char_literal (cp_parser *parser)
4244 {
4245   cp_token *token = cp_lexer_consume_token (parser->lexer);
4246   tree literal = token->u.value;
4247   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4248   tree value = USERDEF_LITERAL_VALUE (literal);
4249   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4250   tree decl, result;
4251 
4252   /* Build up a call to the user-defined operator  */
4253   /* Lookup the name we got back from the id-expression.  */
4254   vec<tree, va_gc> *args = make_tree_vector ();
4255   vec_safe_push (args, value);
4256   decl = lookup_literal_operator (name, args);
4257   if (!decl || decl == error_mark_node)
4258     {
4259       error ("unable to find character literal operator %qD with %qT argument",
4260 	     name, TREE_TYPE (value));
4261       release_tree_vector (args);
4262       return error_mark_node;
4263     }
4264   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4265   release_tree_vector (args);
4266   return result;
4267 }
4268 
4269 /* A subroutine of cp_parser_userdef_numeric_literal to
4270    create a char... template parameter pack from a string node.  */
4271 
4272 static tree
make_char_string_pack(tree value)4273 make_char_string_pack (tree value)
4274 {
4275   tree charvec;
4276   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4277   const char *str = TREE_STRING_POINTER (value);
4278   int i, len = TREE_STRING_LENGTH (value) - 1;
4279   tree argvec = make_tree_vec (1);
4280 
4281   /* Fill in CHARVEC with all of the parameters.  */
4282   charvec = make_tree_vec (len);
4283   for (i = 0; i < len; ++i)
4284     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4285 
4286   /* Build the argument packs.  */
4287   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4288 
4289   TREE_VEC_ELT (argvec, 0) = argpack;
4290 
4291   return argvec;
4292 }
4293 
4294 /* A subroutine of cp_parser_userdef_numeric_literal to
4295    create a char... template parameter pack from a string node.  */
4296 
4297 static tree
make_string_pack(tree value)4298 make_string_pack (tree value)
4299 {
4300   tree charvec;
4301   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4302   const unsigned char *str
4303     = (const unsigned char *) TREE_STRING_POINTER (value);
4304   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4305   int len = TREE_STRING_LENGTH (value) / sz - 1;
4306   tree argvec = make_tree_vec (2);
4307 
4308   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4309   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4310 
4311   /* First template parm is character type.  */
4312   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4313 
4314   /* Fill in CHARVEC with all of the parameters.  */
4315   charvec = make_tree_vec (len);
4316   for (int i = 0; i < len; ++i)
4317     TREE_VEC_ELT (charvec, i)
4318       = double_int_to_tree (str_char_type_node,
4319 			    double_int::from_buffer (str + i * sz, sz));
4320 
4321   /* Build the argument packs.  */
4322   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4323 
4324   TREE_VEC_ELT (argvec, 1) = argpack;
4325 
4326   return argvec;
4327 }
4328 
4329 /* Parse a user-defined numeric constant.  returns a call to a user-defined
4330    literal operator.  */
4331 
4332 static cp_expr
cp_parser_userdef_numeric_literal(cp_parser * parser)4333 cp_parser_userdef_numeric_literal (cp_parser *parser)
4334 {
4335   cp_token *token = cp_lexer_consume_token (parser->lexer);
4336   tree literal = token->u.value;
4337   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4338   tree value = USERDEF_LITERAL_VALUE (literal);
4339   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4340   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4341   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4342   tree decl, result;
4343   vec<tree, va_gc> *args;
4344 
4345   /* Look for a literal operator taking the exact type of numeric argument
4346      as the literal value.  */
4347   args = make_tree_vector ();
4348   vec_safe_push (args, value);
4349   decl = lookup_literal_operator (name, args);
4350   if (decl && decl != error_mark_node)
4351     {
4352       result = finish_call_expr (decl, &args, false, true,
4353 				 tf_warning_or_error);
4354 
4355       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4356 	{
4357 	  warning_at (token->location, OPT_Woverflow,
4358 		      "integer literal exceeds range of %qT type",
4359 		      long_long_unsigned_type_node);
4360 	}
4361       else
4362 	{
4363 	  if (overflow > 0)
4364 	    warning_at (token->location, OPT_Woverflow,
4365 			"floating literal exceeds range of %qT type",
4366 			long_double_type_node);
4367 	  else if (overflow < 0)
4368 	    warning_at (token->location, OPT_Woverflow,
4369 			"floating literal truncated to zero");
4370 	}
4371 
4372       release_tree_vector (args);
4373       return result;
4374     }
4375   release_tree_vector (args);
4376 
4377   /* If the numeric argument didn't work, look for a raw literal
4378      operator taking a const char* argument consisting of the number
4379      in string format.  */
4380   args = make_tree_vector ();
4381   vec_safe_push (args, num_string);
4382   decl = lookup_literal_operator (name, args);
4383   if (decl && decl != error_mark_node)
4384     {
4385       result = finish_call_expr (decl, &args, false, true,
4386 				 tf_warning_or_error);
4387       release_tree_vector (args);
4388       return result;
4389     }
4390   release_tree_vector (args);
4391 
4392   /* If the raw literal didn't work, look for a non-type template
4393      function with parameter pack char....  Call the function with
4394      template parameter characters representing the number.  */
4395   args = make_tree_vector ();
4396   decl = lookup_literal_operator (name, args);
4397   if (decl && decl != error_mark_node)
4398     {
4399       tree tmpl_args = make_char_string_pack (num_string);
4400       decl = lookup_template_function (decl, tmpl_args);
4401       result = finish_call_expr (decl, &args, false, true,
4402 				 tf_warning_or_error);
4403       release_tree_vector (args);
4404       return result;
4405     }
4406 
4407   release_tree_vector (args);
4408 
4409   /* In C++14 the standard library defines complex number suffixes that
4410      conflict with GNU extensions.  Prefer them if <complex> is #included.  */
4411   bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4412   bool i14 = (cxx_dialect > cxx11
4413 	      && (id_equal (suffix_id, "i")
4414 		  || id_equal (suffix_id, "if")
4415 		  || id_equal (suffix_id, "il")));
4416   diagnostic_t kind = DK_ERROR;
4417   int opt = 0;
4418 
4419   if (i14 && ext)
4420     {
4421       tree cxlit = lookup_qualified_name (std_node,
4422 					  get_identifier ("complex_literals"),
4423 					  0, false, false);
4424       if (cxlit == error_mark_node)
4425 	{
4426 	  /* No <complex>, so pedwarn and use GNU semantics.  */
4427 	  kind = DK_PEDWARN;
4428 	  opt = OPT_Wpedantic;
4429 	}
4430     }
4431 
4432   bool complained
4433     = emit_diagnostic (kind, input_location, opt,
4434 		       "unable to find numeric literal operator %qD", name);
4435 
4436   if (!complained)
4437     /* Don't inform either.  */;
4438   else if (i14)
4439     {
4440       inform (token->location, "add %<using namespace std::complex_literals%> "
4441 	      "(from <complex>) to enable the C++14 user-defined literal "
4442 	      "suffixes");
4443       if (ext)
4444 	inform (token->location, "or use %<j%> instead of %<i%> for the "
4445 		"GNU built-in suffix");
4446     }
4447   else if (!ext)
4448     inform (token->location, "use -fext-numeric-literals "
4449 	    "to enable more built-in suffixes");
4450 
4451   if (kind == DK_ERROR)
4452     value = error_mark_node;
4453   else
4454     {
4455       /* Use the built-in semantics.  */
4456       tree type;
4457       if (id_equal (suffix_id, "i"))
4458 	{
4459 	  if (TREE_CODE (value) == INTEGER_CST)
4460 	    type = integer_type_node;
4461 	  else
4462 	    type = double_type_node;
4463 	}
4464       else if (id_equal (suffix_id, "if"))
4465 	type = float_type_node;
4466       else /* if (id_equal (suffix_id, "il")) */
4467 	type = long_double_type_node;
4468 
4469       value = build_complex (build_complex_type (type),
4470 			     fold_convert (type, integer_zero_node),
4471 			     fold_convert (type, value));
4472     }
4473 
4474   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4475     /* Avoid repeated diagnostics.  */
4476     token->u.value = value;
4477   return value;
4478 }
4479 
4480 /* Parse a user-defined string constant.  Returns a call to a user-defined
4481    literal operator taking a character pointer and the length of the string
4482    as arguments.  */
4483 
4484 static tree
cp_parser_userdef_string_literal(tree literal)4485 cp_parser_userdef_string_literal (tree literal)
4486 {
4487   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4488   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4489   tree value = USERDEF_LITERAL_VALUE (literal);
4490   int len = TREE_STRING_LENGTH (value)
4491 	/ TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4492   tree decl, result;
4493   vec<tree, va_gc> *args;
4494 
4495   /* Build up a call to the user-defined operator.  */
4496   /* Lookup the name we got back from the id-expression.  */
4497   args = make_tree_vector ();
4498   vec_safe_push (args, value);
4499   vec_safe_push (args, build_int_cst (size_type_node, len));
4500   decl = lookup_literal_operator (name, args);
4501 
4502   if (decl && decl != error_mark_node)
4503     {
4504       result = finish_call_expr (decl, &args, false, true,
4505 				 tf_warning_or_error);
4506       release_tree_vector (args);
4507       return result;
4508     }
4509   release_tree_vector (args);
4510 
4511   /* Look for a template function with typename parameter CharT
4512      and parameter pack CharT...  Call the function with
4513      template parameter characters representing the string.  */
4514   args = make_tree_vector ();
4515   decl = lookup_literal_operator (name, args);
4516   if (decl && decl != error_mark_node)
4517     {
4518       tree tmpl_args = make_string_pack (value);
4519       decl = lookup_template_function (decl, tmpl_args);
4520       result = finish_call_expr (decl, &args, false, true,
4521 				 tf_warning_or_error);
4522       release_tree_vector (args);
4523       return result;
4524     }
4525   release_tree_vector (args);
4526 
4527   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4528 	 name, TREE_TYPE (value), size_type_node);
4529   return error_mark_node;
4530 }
4531 
4532 
4533 /* Basic concepts [gram.basic]  */
4534 
4535 /* Parse a translation-unit.
4536 
4537    translation-unit:
4538      declaration-seq [opt]
4539 
4540    Returns TRUE if all went well.  */
4541 
4542 static bool
cp_parser_translation_unit(cp_parser * parser)4543 cp_parser_translation_unit (cp_parser* parser)
4544 {
4545   /* The address of the first non-permanent object on the declarator
4546      obstack.  */
4547   static void *declarator_obstack_base;
4548 
4549   bool success;
4550 
4551   /* Create the declarator obstack, if necessary.  */
4552   if (!cp_error_declarator)
4553     {
4554       gcc_obstack_init (&declarator_obstack);
4555       /* Create the error declarator.  */
4556       cp_error_declarator = make_declarator (cdk_error);
4557       /* Create the empty parameter list.  */
4558       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4559 						 UNKNOWN_LOCATION);
4560       /* Remember where the base of the declarator obstack lies.  */
4561       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4562     }
4563 
4564   cp_parser_declaration_seq_opt (parser);
4565 
4566   /* If there are no tokens left then all went well.  */
4567   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4568     {
4569       /* Get rid of the token array; we don't need it any more.  */
4570       cp_lexer_destroy (parser->lexer);
4571       parser->lexer = NULL;
4572 
4573       /* This file might have been a context that's implicitly extern
4574 	 "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4575       if (parser->implicit_extern_c)
4576 	{
4577 	  pop_lang_context ();
4578 	  parser->implicit_extern_c = false;
4579 	}
4580 
4581       /* Finish up.  */
4582       finish_translation_unit ();
4583 
4584       success = true;
4585     }
4586   else
4587     {
4588       cp_parser_error (parser, "expected declaration");
4589       success = false;
4590     }
4591 
4592   /* Make sure the declarator obstack was fully cleaned up.  */
4593   gcc_assert (obstack_next_free (&declarator_obstack)
4594 	      == declarator_obstack_base);
4595 
4596   /* All went well.  */
4597   return success;
4598 }
4599 
4600 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4601    decltype context.  */
4602 
4603 static inline tsubst_flags_t
complain_flags(bool decltype_p)4604 complain_flags (bool decltype_p)
4605 {
4606   tsubst_flags_t complain = tf_warning_or_error;
4607   if (decltype_p)
4608     complain |= tf_decltype;
4609   return complain;
4610 }
4611 
4612 /* We're about to parse a collection of statements.  If we're currently
4613    parsing tentatively, set up a firewall so that any nested
4614    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4615 
4616 static cp_token_position
cp_parser_start_tentative_firewall(cp_parser * parser)4617 cp_parser_start_tentative_firewall (cp_parser *parser)
4618 {
4619   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4620     return 0;
4621 
4622   cp_parser_parse_tentatively (parser);
4623   cp_parser_commit_to_topmost_tentative_parse (parser);
4624   return cp_lexer_token_position (parser->lexer, false);
4625 }
4626 
4627 /* We've finished parsing the collection of statements.  Wrap up the
4628    firewall and replace the relevant tokens with the parsed form.  */
4629 
4630 static void
cp_parser_end_tentative_firewall(cp_parser * parser,cp_token_position start,tree expr)4631 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4632 				  tree expr)
4633 {
4634   if (!start)
4635     return;
4636 
4637   /* Finish the firewall level.  */
4638   cp_parser_parse_definitely (parser);
4639   /* And remember the result of the parse for when we try again.  */
4640   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4641   token->type = CPP_PREPARSED_EXPR;
4642   token->u.value = expr;
4643   token->keyword = RID_MAX;
4644   cp_lexer_purge_tokens_after (parser->lexer, start);
4645 }
4646 
4647 /* Like the above functions, but let the user modify the tokens.  Used by
4648    CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4649    later parses, so it makes sense to localize the effects of
4650    cp_parser_commit_to_tentative_parse.  */
4651 
4652 struct tentative_firewall
4653 {
4654   cp_parser *parser;
4655   bool set;
4656 
tentative_firewalltentative_firewall4657   tentative_firewall (cp_parser *p): parser(p)
4658   {
4659     /* If we're currently parsing tentatively, start a committed level as a
4660        firewall and then an inner tentative parse.  */
4661     if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4662       {
4663 	cp_parser_parse_tentatively (parser);
4664 	cp_parser_commit_to_topmost_tentative_parse (parser);
4665 	cp_parser_parse_tentatively (parser);
4666       }
4667   }
4668 
~tentative_firewalltentative_firewall4669   ~tentative_firewall()
4670   {
4671     if (set)
4672       {
4673 	/* Finish the inner tentative parse and the firewall, propagating any
4674 	   uncommitted error state to the outer tentative parse.  */
4675 	bool err = cp_parser_error_occurred (parser);
4676 	cp_parser_parse_definitely (parser);
4677 	cp_parser_parse_definitely (parser);
4678 	if (err)
4679 	  cp_parser_simulate_error (parser);
4680       }
4681   }
4682 };
4683 
4684 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4685    This class is for tracking such a matching pair of symbols.
4686    In particular, it tracks the location of the first token,
4687    so that if the second token is missing, we can highlight the
4688    location of the first token when notifying the user about the
4689    problem.  */
4690 
4691 template <typename traits_t>
4692 class token_pair
4693 {
4694  public:
4695   /* token_pair's ctor.  */
token_pair()4696   token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4697 
4698   /* If the next token is the opening symbol for this pair, consume it and
4699      return true.
4700      Otherwise, issue an error and return false.
4701      In either case, record the location of the opening token.  */
4702 
require_open(cp_parser * parser)4703   bool require_open (cp_parser *parser)
4704   {
4705     m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4706     return cp_parser_require (parser, traits_t::open_token_type,
4707 			      traits_t::required_token_open);
4708   }
4709 
4710   /* Consume the next token from PARSER, recording its location as
4711      that of the opening token within the pair.  */
4712 
consume_open(cp_parser * parser)4713   cp_token * consume_open (cp_parser *parser)
4714   {
4715     cp_token *tok = cp_lexer_consume_token (parser->lexer);
4716     gcc_assert (tok->type == traits_t::open_token_type);
4717     m_open_loc = tok->location;
4718     return tok;
4719   }
4720 
4721   /* If the next token is the closing symbol for this pair, consume it
4722      and return it.
4723      Otherwise, issue an error, highlighting the location of the
4724      corresponding opening token, and return NULL.  */
4725 
require_close(cp_parser * parser)4726   cp_token *require_close (cp_parser *parser) const
4727   {
4728     return cp_parser_require (parser, traits_t::close_token_type,
4729 			      traits_t::required_token_close,
4730 			      m_open_loc);
4731   }
4732 
4733  private:
4734   location_t m_open_loc;
4735 };
4736 
4737 /* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
4738 
4739 struct matching_paren_traits
4740 {
4741   static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4742   static const enum required_token required_token_open  = RT_OPEN_PAREN;
4743   static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4744   static const enum required_token required_token_close = RT_CLOSE_PAREN;
4745 };
4746 
4747 /* "matching_parens" is a token_pair<T> class for tracking matching
4748    pairs of parentheses.  */
4749 
4750 typedef token_pair<matching_paren_traits> matching_parens;
4751 
4752 /* Traits for token_pair<T> for tracking matching pairs of braces.  */
4753 
4754 struct matching_brace_traits
4755 {
4756   static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4757   static const enum required_token required_token_open = RT_OPEN_BRACE;
4758   static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4759   static const enum required_token required_token_close = RT_CLOSE_BRACE;
4760 };
4761 
4762 /* "matching_braces" is a token_pair<T> class for tracking matching
4763    pairs of braces.  */
4764 
4765 typedef token_pair<matching_brace_traits> matching_braces;
4766 
4767 
4768 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4769    enclosing parentheses.  */
4770 
4771 static cp_expr
cp_parser_statement_expr(cp_parser * parser)4772 cp_parser_statement_expr (cp_parser *parser)
4773 {
4774   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4775 
4776   /* Consume the '('.  */
4777   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4778   matching_parens parens;
4779   parens.consume_open (parser);
4780   /* Start the statement-expression.  */
4781   tree expr = begin_stmt_expr ();
4782   /* Parse the compound-statement.  */
4783   cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4784   /* Finish up.  */
4785   expr = finish_stmt_expr (expr, false);
4786   /* Consume the ')'.  */
4787   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4788   if (!parens.require_close (parser))
4789     cp_parser_skip_to_end_of_statement (parser);
4790 
4791   cp_parser_end_tentative_firewall (parser, start, expr);
4792   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4793   return cp_expr (expr, combined_loc);
4794 }
4795 
4796 /* Expressions [gram.expr] */
4797 
4798 /* Parse a fold-operator.
4799 
4800     fold-operator:
4801         -  *  /  %  ^  &  |  =  <  >  <<  >>
4802       =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
4803       ==  !=  <=  >=  &&  ||  ,  .*  ->*
4804 
4805    This returns the tree code corresponding to the matched operator
4806    as an int. When the current token matches a compound assignment
4807    opertor, the resulting tree code is the negative value of the
4808    non-assignment operator. */
4809 
4810 static int
cp_parser_fold_operator(cp_token * token)4811 cp_parser_fold_operator (cp_token *token)
4812 {
4813   switch (token->type)
4814     {
4815     case CPP_PLUS: return PLUS_EXPR;
4816     case CPP_MINUS: return MINUS_EXPR;
4817     case CPP_MULT: return MULT_EXPR;
4818     case CPP_DIV: return TRUNC_DIV_EXPR;
4819     case CPP_MOD: return TRUNC_MOD_EXPR;
4820     case CPP_XOR: return BIT_XOR_EXPR;
4821     case CPP_AND: return BIT_AND_EXPR;
4822     case CPP_OR: return BIT_IOR_EXPR;
4823     case CPP_LSHIFT: return LSHIFT_EXPR;
4824     case CPP_RSHIFT: return RSHIFT_EXPR;
4825 
4826     case CPP_EQ: return -NOP_EXPR;
4827     case CPP_PLUS_EQ: return -PLUS_EXPR;
4828     case CPP_MINUS_EQ: return -MINUS_EXPR;
4829     case CPP_MULT_EQ: return -MULT_EXPR;
4830     case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4831     case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4832     case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4833     case CPP_AND_EQ: return -BIT_AND_EXPR;
4834     case CPP_OR_EQ: return -BIT_IOR_EXPR;
4835     case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4836     case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4837 
4838     case CPP_EQ_EQ: return EQ_EXPR;
4839     case CPP_NOT_EQ: return NE_EXPR;
4840     case CPP_LESS: return LT_EXPR;
4841     case CPP_GREATER: return GT_EXPR;
4842     case CPP_LESS_EQ: return LE_EXPR;
4843     case CPP_GREATER_EQ: return GE_EXPR;
4844 
4845     case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4846     case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4847 
4848     case CPP_COMMA: return COMPOUND_EXPR;
4849 
4850     case CPP_DOT_STAR: return DOTSTAR_EXPR;
4851     case CPP_DEREF_STAR: return MEMBER_REF;
4852 
4853     default: return ERROR_MARK;
4854     }
4855 }
4856 
4857 /* Returns true if CODE indicates a binary expression, which is not allowed in
4858    the LHS of a fold-expression.  More codes will need to be added to use this
4859    function in other contexts.  */
4860 
4861 static bool
is_binary_op(tree_code code)4862 is_binary_op (tree_code code)
4863 {
4864   switch (code)
4865     {
4866     case PLUS_EXPR:
4867     case POINTER_PLUS_EXPR:
4868     case MINUS_EXPR:
4869     case MULT_EXPR:
4870     case TRUNC_DIV_EXPR:
4871     case TRUNC_MOD_EXPR:
4872     case BIT_XOR_EXPR:
4873     case BIT_AND_EXPR:
4874     case BIT_IOR_EXPR:
4875     case LSHIFT_EXPR:
4876     case RSHIFT_EXPR:
4877 
4878     case MODOP_EXPR:
4879 
4880     case EQ_EXPR:
4881     case NE_EXPR:
4882     case LE_EXPR:
4883     case GE_EXPR:
4884     case LT_EXPR:
4885     case GT_EXPR:
4886 
4887     case TRUTH_ANDIF_EXPR:
4888     case TRUTH_ORIF_EXPR:
4889 
4890     case COMPOUND_EXPR:
4891 
4892     case DOTSTAR_EXPR:
4893     case MEMBER_REF:
4894       return true;
4895 
4896     default:
4897       return false;
4898     }
4899 }
4900 
4901 /* If the next token is a suitable fold operator, consume it and return as
4902    the function above.  */
4903 
4904 static int
cp_parser_fold_operator(cp_parser * parser)4905 cp_parser_fold_operator (cp_parser *parser)
4906 {
4907   cp_token* token = cp_lexer_peek_token (parser->lexer);
4908   int code = cp_parser_fold_operator (token);
4909   if (code != ERROR_MARK)
4910     cp_lexer_consume_token (parser->lexer);
4911   return code;
4912 }
4913 
4914 /* Parse a fold-expression.
4915 
4916      fold-expression:
4917        ( ... folding-operator cast-expression)
4918        ( cast-expression folding-operator ... )
4919        ( cast-expression folding operator ... folding-operator cast-expression)
4920 
4921    Note that the '(' and ')' are matched in primary expression. */
4922 
4923 static cp_expr
cp_parser_fold_expression(cp_parser * parser,tree expr1)4924 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4925 {
4926   cp_id_kind pidk;
4927 
4928   // Left fold.
4929   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4930     {
4931       cp_lexer_consume_token (parser->lexer);
4932       int op = cp_parser_fold_operator (parser);
4933       if (op == ERROR_MARK)
4934         {
4935           cp_parser_error (parser, "expected binary operator");
4936           return error_mark_node;
4937         }
4938 
4939       tree expr = cp_parser_cast_expression (parser, false, false,
4940 					     false, &pidk);
4941       if (expr == error_mark_node)
4942         return error_mark_node;
4943       return finish_left_unary_fold_expr (expr, op);
4944     }
4945 
4946   const cp_token* token = cp_lexer_peek_token (parser->lexer);
4947   int op = cp_parser_fold_operator (parser);
4948   if (op == ERROR_MARK)
4949     {
4950       cp_parser_error (parser, "expected binary operator");
4951       return error_mark_node;
4952     }
4953 
4954   if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4955     {
4956       cp_parser_error (parser, "expected ...");
4957       return error_mark_node;
4958     }
4959   cp_lexer_consume_token (parser->lexer);
4960 
4961   /* The operands of a fold-expression are cast-expressions, so binary or
4962      conditional expressions are not allowed.  We check this here to avoid
4963      tentative parsing.  */
4964   if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4965     /* OK, the expression was parenthesized.  */;
4966   else if (is_binary_op (TREE_CODE (expr1)))
4967     error_at (location_of (expr1),
4968 	      "binary expression in operand of fold-expression");
4969   else if (TREE_CODE (expr1) == COND_EXPR
4970 	   || (REFERENCE_REF_P (expr1)
4971 	       && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4972     error_at (location_of (expr1),
4973 	      "conditional expression in operand of fold-expression");
4974 
4975   // Right fold.
4976   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4977     return finish_right_unary_fold_expr (expr1, op);
4978 
4979   if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4980     {
4981       cp_parser_error (parser, "mismatched operator in fold-expression");
4982       return error_mark_node;
4983     }
4984   cp_lexer_consume_token (parser->lexer);
4985 
4986   // Binary left or right fold.
4987   tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4988   if (expr2 == error_mark_node)
4989     return error_mark_node;
4990   return finish_binary_fold_expr (expr1, expr2, op);
4991 }
4992 
4993 /* Parse a primary-expression.
4994 
4995    primary-expression:
4996      literal
4997      this
4998      ( expression )
4999      id-expression
5000      lambda-expression (C++11)
5001 
5002    GNU Extensions:
5003 
5004    primary-expression:
5005      ( compound-statement )
5006      __builtin_va_arg ( assignment-expression , type-id )
5007      __builtin_offsetof ( type-id , offsetof-expression )
5008 
5009    C++ Extensions:
5010      __has_nothrow_assign ( type-id )
5011      __has_nothrow_constructor ( type-id )
5012      __has_nothrow_copy ( type-id )
5013      __has_trivial_assign ( type-id )
5014      __has_trivial_constructor ( type-id )
5015      __has_trivial_copy ( type-id )
5016      __has_trivial_destructor ( type-id )
5017      __has_virtual_destructor ( type-id )
5018      __is_abstract ( type-id )
5019      __is_base_of ( type-id , type-id )
5020      __is_class ( type-id )
5021      __is_empty ( type-id )
5022      __is_enum ( type-id )
5023      __is_final ( type-id )
5024      __is_literal_type ( type-id )
5025      __is_pod ( type-id )
5026      __is_polymorphic ( type-id )
5027      __is_std_layout ( type-id )
5028      __is_trivial ( type-id )
5029      __is_union ( type-id )
5030 
5031    Objective-C++ Extension:
5032 
5033    primary-expression:
5034      objc-expression
5035 
5036    literal:
5037      __null
5038 
5039    ADDRESS_P is true iff this expression was immediately preceded by
5040    "&" and therefore might denote a pointer-to-member.  CAST_P is true
5041    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
5042    true iff this expression is a template argument.
5043 
5044    Returns a representation of the expression.  Upon return, *IDK
5045    indicates what kind of id-expression (if any) was present.  */
5046 
5047 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)5048 cp_parser_primary_expression (cp_parser *parser,
5049 			      bool address_p,
5050 			      bool cast_p,
5051 			      bool template_arg_p,
5052 			      bool decltype_p,
5053 			      cp_id_kind *idk)
5054 {
5055   cp_token *token = NULL;
5056 
5057   /* Assume the primary expression is not an id-expression.  */
5058   *idk = CP_ID_KIND_NONE;
5059 
5060   /* Peek at the next token.  */
5061   token = cp_lexer_peek_token (parser->lexer);
5062   switch ((int) token->type)
5063     {
5064       /* literal:
5065 	   integer-literal
5066 	   character-literal
5067 	   floating-literal
5068 	   string-literal
5069 	   boolean-literal
5070 	   pointer-literal
5071 	   user-defined-literal  */
5072     case CPP_CHAR:
5073     case CPP_CHAR16:
5074     case CPP_CHAR32:
5075     case CPP_WCHAR:
5076     case CPP_UTF8CHAR:
5077     case CPP_NUMBER:
5078     case CPP_PREPARSED_EXPR:
5079       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5080 	return cp_parser_userdef_numeric_literal (parser);
5081       token = cp_lexer_consume_token (parser->lexer);
5082       if (TREE_CODE (token->u.value) == FIXED_CST)
5083 	{
5084 	  error_at (token->location,
5085 		    "fixed-point types not supported in C++");
5086 	  return error_mark_node;
5087 	}
5088       /* Floating-point literals are only allowed in an integral
5089 	 constant expression if they are cast to an integral or
5090 	 enumeration type.  */
5091       if (TREE_CODE (token->u.value) == REAL_CST
5092 	  && parser->integral_constant_expression_p
5093 	  && pedantic)
5094 	{
5095 	  /* CAST_P will be set even in invalid code like "int(2.7 +
5096 	     ...)".   Therefore, we have to check that the next token
5097 	     is sure to end the cast.  */
5098 	  if (cast_p)
5099 	    {
5100 	      cp_token *next_token;
5101 
5102 	      next_token = cp_lexer_peek_token (parser->lexer);
5103 	      if (/* The comma at the end of an
5104 		     enumerator-definition.  */
5105 		  next_token->type != CPP_COMMA
5106 		  /* The curly brace at the end of an enum-specifier.  */
5107 		  && next_token->type != CPP_CLOSE_BRACE
5108 		  /* The end of a statement.  */
5109 		  && next_token->type != CPP_SEMICOLON
5110 		  /* The end of the cast-expression.  */
5111 		  && next_token->type != CPP_CLOSE_PAREN
5112 		  /* The end of an array bound.  */
5113 		  && next_token->type != CPP_CLOSE_SQUARE
5114 		  /* The closing ">" in a template-argument-list.  */
5115 		  && (next_token->type != CPP_GREATER
5116 		      || parser->greater_than_is_operator_p)
5117 		  /* C++0x only: A ">>" treated like two ">" tokens,
5118                      in a template-argument-list.  */
5119 		  && (next_token->type != CPP_RSHIFT
5120                       || (cxx_dialect == cxx98)
5121 		      || parser->greater_than_is_operator_p))
5122 		cast_p = false;
5123 	    }
5124 
5125 	  /* If we are within a cast, then the constraint that the
5126 	     cast is to an integral or enumeration type will be
5127 	     checked at that point.  If we are not within a cast, then
5128 	     this code is invalid.  */
5129 	  if (!cast_p)
5130 	    cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5131 	}
5132       return cp_expr (token->u.value, token->location);
5133 
5134     case CPP_CHAR_USERDEF:
5135     case CPP_CHAR16_USERDEF:
5136     case CPP_CHAR32_USERDEF:
5137     case CPP_WCHAR_USERDEF:
5138     case CPP_UTF8CHAR_USERDEF:
5139       return cp_parser_userdef_char_literal (parser);
5140 
5141     case CPP_STRING:
5142     case CPP_STRING16:
5143     case CPP_STRING32:
5144     case CPP_WSTRING:
5145     case CPP_UTF8STRING:
5146     case CPP_STRING_USERDEF:
5147     case CPP_STRING16_USERDEF:
5148     case CPP_STRING32_USERDEF:
5149     case CPP_WSTRING_USERDEF:
5150     case CPP_UTF8STRING_USERDEF:
5151       /* ??? Should wide strings be allowed when parser->translate_strings_p
5152 	 is false (i.e. in attributes)?  If not, we can kill the third
5153 	 argument to cp_parser_string_literal.  */
5154       return cp_parser_string_literal (parser,
5155 				       parser->translate_strings_p,
5156 				       true);
5157 
5158     case CPP_OPEN_PAREN:
5159       /* If we see `( { ' then we are looking at the beginning of
5160 	 a GNU statement-expression.  */
5161       if (cp_parser_allow_gnu_extensions_p (parser)
5162 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5163 	{
5164 	  /* Statement-expressions are not allowed by the standard.  */
5165 	  pedwarn (token->location, OPT_Wpedantic,
5166 		   "ISO C++ forbids braced-groups within expressions");
5167 
5168 	  /* And they're not allowed outside of a function-body; you
5169 	     cannot, for example, write:
5170 
5171 	     int i = ({ int j = 3; j + 1; });
5172 
5173 	     at class or namespace scope.  */
5174 	  if (!parser->in_function_body
5175 	      || parser->in_template_argument_list_p)
5176 	    {
5177 	      error_at (token->location,
5178 			"statement-expressions are not allowed outside "
5179 			"functions nor in template-argument lists");
5180 	      cp_parser_skip_to_end_of_block_or_statement (parser);
5181 	      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5182 		cp_lexer_consume_token (parser->lexer);
5183 	      return error_mark_node;
5184 	    }
5185 	  else
5186 	    return cp_parser_statement_expr (parser);
5187 	}
5188       /* Otherwise it's a normal parenthesized expression.  */
5189       {
5190 	cp_expr expr;
5191 	bool saved_greater_than_is_operator_p;
5192 
5193 	location_t open_paren_loc = token->location;
5194 
5195 	/* Consume the `('.  */
5196 	matching_parens parens;
5197 	parens.consume_open (parser);
5198 	/* Within a parenthesized expression, a `>' token is always
5199 	   the greater-than operator.  */
5200 	saved_greater_than_is_operator_p
5201 	  = parser->greater_than_is_operator_p;
5202 	parser->greater_than_is_operator_p = true;
5203 
5204 	if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5205 	  /* Left fold expression. */
5206 	  expr = NULL_TREE;
5207 	else
5208 	  /* Parse the parenthesized expression.  */
5209 	  expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5210 
5211 	token = cp_lexer_peek_token (parser->lexer);
5212 	if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5213 	  {
5214 	    expr = cp_parser_fold_expression (parser, expr);
5215 	    if (expr != error_mark_node
5216 		&& cxx_dialect < cxx17
5217 		&& !in_system_header_at (input_location))
5218 	      pedwarn (input_location, 0, "fold-expressions only available "
5219 		       "with -std=c++17 or -std=gnu++17");
5220 	  }
5221 	else
5222 	  /* Let the front end know that this expression was
5223 	     enclosed in parentheses. This matters in case, for
5224 	     example, the expression is of the form `A::B', since
5225 	     `&A::B' might be a pointer-to-member, but `&(A::B)' is
5226 	     not.  */
5227 	  expr = finish_parenthesized_expr (expr);
5228 
5229 	/* DR 705: Wrapping an unqualified name in parentheses
5230 	   suppresses arg-dependent lookup.  We want to pass back
5231 	   CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5232 	   (c++/37862), but none of the others.  */
5233 	if (*idk != CP_ID_KIND_QUALIFIED)
5234 	  *idk = CP_ID_KIND_NONE;
5235 
5236 	/* The `>' token might be the end of a template-id or
5237 	   template-parameter-list now.  */
5238 	parser->greater_than_is_operator_p
5239 	  = saved_greater_than_is_operator_p;
5240 
5241 	/* Consume the `)'.  */
5242 	token = cp_lexer_peek_token (parser->lexer);
5243 	location_t close_paren_loc = token->location;
5244 	expr.set_range (open_paren_loc, close_paren_loc);
5245 	if (!parens.require_close (parser)
5246 	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5247 	  cp_parser_skip_to_end_of_statement (parser);
5248 
5249 	return expr;
5250       }
5251 
5252     case CPP_OPEN_SQUARE:
5253       {
5254 	if (c_dialect_objc ())
5255 	  {
5256 	    /* We might have an Objective-C++ message. */
5257 	    cp_parser_parse_tentatively (parser);
5258 	    tree msg = cp_parser_objc_message_expression (parser);
5259 	    /* If that works out, we're done ... */
5260 	    if (cp_parser_parse_definitely (parser))
5261 	      return msg;
5262 	    /* ... else, fall though to see if it's a lambda.  */
5263 	  }
5264 	cp_expr lam = cp_parser_lambda_expression (parser);
5265 	/* Don't warn about a failed tentative parse.  */
5266 	if (cp_parser_error_occurred (parser))
5267 	  return error_mark_node;
5268 	maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5269 	return lam;
5270       }
5271 
5272     case CPP_OBJC_STRING:
5273       if (c_dialect_objc ())
5274 	/* We have an Objective-C++ string literal. */
5275         return cp_parser_objc_expression (parser);
5276       cp_parser_error (parser, "expected primary-expression");
5277       return error_mark_node;
5278 
5279     case CPP_KEYWORD:
5280       switch (token->keyword)
5281 	{
5282 	  /* These two are the boolean literals.  */
5283 	case RID_TRUE:
5284 	  cp_lexer_consume_token (parser->lexer);
5285 	  return cp_expr (boolean_true_node, token->location);
5286 	case RID_FALSE:
5287 	  cp_lexer_consume_token (parser->lexer);
5288 	  return cp_expr (boolean_false_node, token->location);
5289 
5290 	  /* The `__null' literal.  */
5291 	case RID_NULL:
5292 	  cp_lexer_consume_token (parser->lexer);
5293 	  return cp_expr (null_node, token->location);
5294 
5295 	  /* The `nullptr' literal.  */
5296 	case RID_NULLPTR:
5297 	  cp_lexer_consume_token (parser->lexer);
5298 	  return cp_expr (nullptr_node, token->location);
5299 
5300 	  /* Recognize the `this' keyword.  */
5301 	case RID_THIS:
5302 	  cp_lexer_consume_token (parser->lexer);
5303 	  if (parser->local_variables_forbidden_p)
5304 	    {
5305 	      error_at (token->location,
5306 			"%<this%> may not be used in this context");
5307 	      return error_mark_node;
5308 	    }
5309 	  /* Pointers cannot appear in constant-expressions.  */
5310 	  if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5311 	    return error_mark_node;
5312 	  return cp_expr (finish_this_expr (), token->location);
5313 
5314 	  /* The `operator' keyword can be the beginning of an
5315 	     id-expression.  */
5316 	case RID_OPERATOR:
5317 	  goto id_expression;
5318 
5319 	case RID_FUNCTION_NAME:
5320 	case RID_PRETTY_FUNCTION_NAME:
5321 	case RID_C99_FUNCTION_NAME:
5322 	  {
5323 	    non_integral_constant name;
5324 
5325 	    /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5326 	       __func__ are the names of variables -- but they are
5327 	       treated specially.  Therefore, they are handled here,
5328 	       rather than relying on the generic id-expression logic
5329 	       below.  Grammatically, these names are id-expressions.
5330 
5331 	       Consume the token.  */
5332 	    token = cp_lexer_consume_token (parser->lexer);
5333 
5334 	    switch (token->keyword)
5335 	      {
5336 	      case RID_FUNCTION_NAME:
5337 		name = NIC_FUNC_NAME;
5338 		break;
5339 	      case RID_PRETTY_FUNCTION_NAME:
5340 		name = NIC_PRETTY_FUNC;
5341 		break;
5342 	      case RID_C99_FUNCTION_NAME:
5343 		name = NIC_C99_FUNC;
5344 		break;
5345 	      default:
5346 		gcc_unreachable ();
5347 	      }
5348 
5349 	    if (cp_parser_non_integral_constant_expression (parser, name))
5350 	      return error_mark_node;
5351 
5352 	    /* Look up the name.  */
5353 	    return finish_fname (token->u.value);
5354 	  }
5355 
5356 	case RID_VA_ARG:
5357 	  {
5358 	    tree expression;
5359 	    tree type;
5360 	    source_location type_location;
5361 	    location_t start_loc
5362 	      = cp_lexer_peek_token (parser->lexer)->location;
5363 	    /* The `__builtin_va_arg' construct is used to handle
5364 	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
5365 	    cp_lexer_consume_token (parser->lexer);
5366 	    /* Look for the opening `('.  */
5367 	    matching_parens parens;
5368 	    parens.require_open (parser);
5369 	    /* Now, parse the assignment-expression.  */
5370 	    expression = cp_parser_assignment_expression (parser);
5371 	    /* Look for the `,'.  */
5372 	    cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5373 	    type_location = cp_lexer_peek_token (parser->lexer)->location;
5374 	    /* Parse the type-id.  */
5375 	    {
5376 	      type_id_in_expr_sentinel s (parser);
5377 	      type = cp_parser_type_id (parser);
5378 	    }
5379 	    /* Look for the closing `)'.  */
5380 	    location_t finish_loc
5381 	      = cp_lexer_peek_token (parser->lexer)->location;
5382 	    parens.require_close (parser);
5383 	    /* Using `va_arg' in a constant-expression is not
5384 	       allowed.  */
5385 	    if (cp_parser_non_integral_constant_expression (parser,
5386 							    NIC_VA_ARG))
5387 	      return error_mark_node;
5388 	    /* Construct a location of the form:
5389 		 __builtin_va_arg (v, int)
5390 		 ~~~~~~~~~~~~~~~~~~~~~^~~~
5391 	       with the caret at the type, ranging from the start of the
5392 	       "__builtin_va_arg" token to the close paren.  */
5393 	    location_t combined_loc
5394 	      = make_location (type_location, start_loc, finish_loc);
5395 	    return build_x_va_arg (combined_loc, expression, type);
5396 	  }
5397 
5398 	case RID_OFFSETOF:
5399 	  return cp_parser_builtin_offsetof (parser);
5400 
5401 	case RID_HAS_NOTHROW_ASSIGN:
5402 	case RID_HAS_NOTHROW_CONSTRUCTOR:
5403 	case RID_HAS_NOTHROW_COPY:
5404 	case RID_HAS_TRIVIAL_ASSIGN:
5405 	case RID_HAS_TRIVIAL_CONSTRUCTOR:
5406 	case RID_HAS_TRIVIAL_COPY:
5407 	case RID_HAS_TRIVIAL_DESTRUCTOR:
5408 	case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5409 	case RID_HAS_VIRTUAL_DESTRUCTOR:
5410 	case RID_IS_ABSTRACT:
5411 	case RID_IS_AGGREGATE:
5412 	case RID_IS_BASE_OF:
5413 	case RID_IS_CLASS:
5414 	case RID_IS_EMPTY:
5415 	case RID_IS_ENUM:
5416 	case RID_IS_FINAL:
5417 	case RID_IS_LITERAL_TYPE:
5418 	case RID_IS_POD:
5419 	case RID_IS_POLYMORPHIC:
5420 	case RID_IS_SAME_AS:
5421 	case RID_IS_STD_LAYOUT:
5422 	case RID_IS_TRIVIAL:
5423 	case RID_IS_TRIVIALLY_ASSIGNABLE:
5424 	case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5425 	case RID_IS_TRIVIALLY_COPYABLE:
5426 	case RID_IS_UNION:
5427 	case RID_IS_ASSIGNABLE:
5428 	case RID_IS_CONSTRUCTIBLE:
5429 	  return cp_parser_trait_expr (parser, token->keyword);
5430 
5431 	// C++ concepts
5432 	case RID_REQUIRES:
5433 	  return cp_parser_requires_expression (parser);
5434 
5435 	/* Objective-C++ expressions.  */
5436 	case RID_AT_ENCODE:
5437 	case RID_AT_PROTOCOL:
5438 	case RID_AT_SELECTOR:
5439 	  return cp_parser_objc_expression (parser);
5440 
5441 	case RID_TEMPLATE:
5442 	  if (parser->in_function_body
5443 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5444 	      	  == CPP_LESS))
5445 	    {
5446 	      error_at (token->location,
5447 			"a template declaration cannot appear at block scope");
5448 	      cp_parser_skip_to_end_of_block_or_statement (parser);
5449 	      return error_mark_node;
5450 	    }
5451 	  /* FALLTHRU */
5452 	default:
5453 	  cp_parser_error (parser, "expected primary-expression");
5454 	  return error_mark_node;
5455 	}
5456 
5457       /* An id-expression can start with either an identifier, a
5458 	 `::' as the beginning of a qualified-id, or the "operator"
5459 	 keyword.  */
5460     case CPP_NAME:
5461     case CPP_SCOPE:
5462     case CPP_TEMPLATE_ID:
5463     case CPP_NESTED_NAME_SPECIFIER:
5464       {
5465       id_expression:
5466 	cp_expr id_expression;
5467 	cp_expr decl;
5468 	const char *error_msg;
5469 	bool template_p;
5470 	bool done;
5471 	cp_token *id_expr_token;
5472 
5473 	/* Parse the id-expression.  */
5474 	id_expression
5475 	  = cp_parser_id_expression (parser,
5476 				     /*template_keyword_p=*/false,
5477 				     /*check_dependency_p=*/true,
5478 				     &template_p,
5479 				     /*declarator_p=*/false,
5480 				     /*optional_p=*/false);
5481 	if (id_expression == error_mark_node)
5482 	  return error_mark_node;
5483 	id_expr_token = token;
5484 	token = cp_lexer_peek_token (parser->lexer);
5485 	done = (token->type != CPP_OPEN_SQUARE
5486 		&& token->type != CPP_OPEN_PAREN
5487 		&& token->type != CPP_DOT
5488 		&& token->type != CPP_DEREF
5489 		&& token->type != CPP_PLUS_PLUS
5490 		&& token->type != CPP_MINUS_MINUS);
5491 	/* If we have a template-id, then no further lookup is
5492 	   required.  If the template-id was for a template-class, we
5493 	   will sometimes have a TYPE_DECL at this point.  */
5494 	if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5495 		 || TREE_CODE (id_expression) == TYPE_DECL)
5496 	  decl = id_expression;
5497 	/* Look up the name.  */
5498 	else
5499 	  {
5500 	    tree ambiguous_decls;
5501 
5502 	    /* If we already know that this lookup is ambiguous, then
5503 	       we've already issued an error message; there's no reason
5504 	       to check again.  */
5505 	    if (id_expr_token->type == CPP_NAME
5506 		&& id_expr_token->error_reported)
5507 	      {
5508 		cp_parser_simulate_error (parser);
5509 		return error_mark_node;
5510 	      }
5511 
5512 	    decl = cp_parser_lookup_name (parser, id_expression,
5513 					  none_type,
5514 					  template_p,
5515 					  /*is_namespace=*/false,
5516 					  /*check_dependency=*/true,
5517 					  &ambiguous_decls,
5518 					  id_expr_token->location);
5519 	    /* If the lookup was ambiguous, an error will already have
5520 	       been issued.  */
5521 	    if (ambiguous_decls)
5522 	      return error_mark_node;
5523 
5524 	    /* In Objective-C++, we may have an Objective-C 2.0
5525 	       dot-syntax for classes here.  */
5526 	    if (c_dialect_objc ()
5527 		&& cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5528 		&& TREE_CODE (decl) == TYPE_DECL
5529 		&& objc_is_class_name (decl))
5530 	      {
5531 		tree component;
5532 		cp_lexer_consume_token (parser->lexer);
5533 		component = cp_parser_identifier (parser);
5534 		if (component == error_mark_node)
5535 		  return error_mark_node;
5536 
5537 		tree result = objc_build_class_component_ref (id_expression,
5538 							      component);
5539 		/* Build a location of the form:
5540 		     expr.component
5541 		     ~~~~~^~~~~~~~~
5542 		   with caret at the start of the component name (at
5543 		   input_location), ranging from the start of the id_expression
5544 		   to the end of the component name.  */
5545 		location_t combined_loc
5546 		  = make_location (input_location, id_expression.get_start (),
5547 				   get_finish (input_location));
5548 		protected_set_expr_location (result, combined_loc);
5549 		return result;
5550 	      }
5551 
5552 	    /* In Objective-C++, an instance variable (ivar) may be preferred
5553 	       to whatever cp_parser_lookup_name() found.
5554 	       Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5555 	       rest of c-family, we have to do a little extra work to preserve
5556 	       any location information in cp_expr "decl".  Given that
5557 	       objc_lookup_ivar is implemented in "c-family" and "objc", we
5558 	       have a trip through the pure "tree" type, rather than cp_expr.
5559 	       Naively copying it back to "decl" would implicitly give the
5560 	       new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5561 	       store an EXPR_LOCATION.  Hence we only update "decl" (and
5562 	       hence its location_t) if we get back a different tree node.  */
5563 	    tree decl_tree = objc_lookup_ivar (decl.get_value (),
5564 					       id_expression);
5565 	    if (decl_tree != decl.get_value ())
5566 	      decl = cp_expr (decl_tree);
5567 
5568 	    /* If name lookup gives us a SCOPE_REF, then the
5569 	       qualifying scope was dependent.  */
5570 	    if (TREE_CODE (decl) == SCOPE_REF)
5571 	      {
5572 		/* At this point, we do not know if DECL is a valid
5573 		   integral constant expression.  We assume that it is
5574 		   in fact such an expression, so that code like:
5575 
5576 		      template <int N> struct A {
5577 			int a[B<N>::i];
5578 		      };
5579 
5580 		   is accepted.  At template-instantiation time, we
5581 		   will check that B<N>::i is actually a constant.  */
5582 		return decl;
5583 	      }
5584 	    /* Check to see if DECL is a local variable in a context
5585 	       where that is forbidden.  */
5586 	    if (parser->local_variables_forbidden_p
5587 		&& local_variable_p (decl))
5588 	      {
5589 		/* It might be that we only found DECL because we are
5590 		   trying to be generous with pre-ISO scoping rules.
5591 		   For example, consider:
5592 
5593 		     int i;
5594 		     void g() {
5595 		       for (int i = 0; i < 10; ++i) {}
5596 		       extern void f(int j = i);
5597 		     }
5598 
5599 		   Here, name look up will originally find the out
5600 		   of scope `i'.  We need to issue a warning message,
5601 		   but then use the global `i'.  */
5602 		decl = check_for_out_of_scope_variable (decl);
5603 		if (local_variable_p (decl))
5604 		  {
5605 		    error_at (id_expr_token->location,
5606 			      "local variable %qD may not appear in this context",
5607 			      decl.get_value ());
5608 		    return error_mark_node;
5609 		  }
5610 	      }
5611 	  }
5612 
5613 	if (processing_template_decl && is_overloaded_fn (decl))
5614 	  lookup_keep (get_fns (decl), true);
5615 
5616 	decl = (finish_id_expression
5617 		(id_expression, decl, parser->scope,
5618 		 idk,
5619 		 parser->integral_constant_expression_p,
5620 		 parser->allow_non_integral_constant_expression_p,
5621 		 &parser->non_integral_constant_expression_p,
5622 		 template_p, done, address_p,
5623 		 template_arg_p,
5624 		 &error_msg,
5625 		 id_expression.get_location ()));
5626 	if (error_msg)
5627 	  cp_parser_error (parser, error_msg);
5628 	decl.set_location (id_expr_token->location);
5629 	return decl;
5630       }
5631 
5632       /* Anything else is an error.  */
5633     default:
5634       cp_parser_error (parser, "expected primary-expression");
5635       return error_mark_node;
5636     }
5637 }
5638 
5639 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)5640 cp_parser_primary_expression (cp_parser *parser,
5641 			      bool address_p,
5642 			      bool cast_p,
5643 			      bool template_arg_p,
5644 			      cp_id_kind *idk)
5645 {
5646   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5647 				       /*decltype*/false, idk);
5648 }
5649 
5650 /* Parse an id-expression.
5651 
5652    id-expression:
5653      unqualified-id
5654      qualified-id
5655 
5656    qualified-id:
5657      :: [opt] nested-name-specifier template [opt] unqualified-id
5658      :: identifier
5659      :: operator-function-id
5660      :: template-id
5661 
5662    Return a representation of the unqualified portion of the
5663    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
5664    a `::' or nested-name-specifier.
5665 
5666    Often, if the id-expression was a qualified-id, the caller will
5667    want to make a SCOPE_REF to represent the qualified-id.  This
5668    function does not do this in order to avoid wastefully creating
5669    SCOPE_REFs when they are not required.
5670 
5671    If TEMPLATE_KEYWORD_P is true, then we have just seen the
5672    `template' keyword.
5673 
5674    If CHECK_DEPENDENCY_P is false, then names are looked up inside
5675    uninstantiated templates.
5676 
5677    If *TEMPLATE_P is non-NULL, it is set to true iff the
5678    `template' keyword is used to explicitly indicate that the entity
5679    named is a template.
5680 
5681    If DECLARATOR_P is true, the id-expression is appearing as part of
5682    a declarator, rather than as part of an expression.  */
5683 
5684 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)5685 cp_parser_id_expression (cp_parser *parser,
5686 			 bool template_keyword_p,
5687 			 bool check_dependency_p,
5688 			 bool *template_p,
5689 			 bool declarator_p,
5690 			 bool optional_p)
5691 {
5692   bool global_scope_p;
5693   bool nested_name_specifier_p;
5694 
5695   /* Assume the `template' keyword was not used.  */
5696   if (template_p)
5697     *template_p = template_keyword_p;
5698 
5699   /* Look for the optional `::' operator.  */
5700   global_scope_p
5701     = (!template_keyword_p
5702        && (cp_parser_global_scope_opt (parser,
5703 				       /*current_scope_valid_p=*/false)
5704 	   != NULL_TREE));
5705 
5706   /* Look for the optional nested-name-specifier.  */
5707   nested_name_specifier_p
5708     = (cp_parser_nested_name_specifier_opt (parser,
5709 					    /*typename_keyword_p=*/false,
5710 					    check_dependency_p,
5711 					    /*type_p=*/false,
5712 					    declarator_p,
5713 					    template_keyword_p)
5714        != NULL_TREE);
5715 
5716   /* If there is a nested-name-specifier, then we are looking at
5717      the first qualified-id production.  */
5718   if (nested_name_specifier_p)
5719     {
5720       tree saved_scope;
5721       tree saved_object_scope;
5722       tree saved_qualifying_scope;
5723       cp_expr unqualified_id;
5724       bool is_template;
5725 
5726       /* See if the next token is the `template' keyword.  */
5727       if (!template_p)
5728 	template_p = &is_template;
5729       *template_p = cp_parser_optional_template_keyword (parser);
5730       /* Name lookup we do during the processing of the
5731 	 unqualified-id might obliterate SCOPE.  */
5732       saved_scope = parser->scope;
5733       saved_object_scope = parser->object_scope;
5734       saved_qualifying_scope = parser->qualifying_scope;
5735       /* Process the final unqualified-id.  */
5736       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5737 						 check_dependency_p,
5738 						 declarator_p,
5739 						 /*optional_p=*/false);
5740       /* Restore the SAVED_SCOPE for our caller.  */
5741       parser->scope = saved_scope;
5742       parser->object_scope = saved_object_scope;
5743       parser->qualifying_scope = saved_qualifying_scope;
5744 
5745       return unqualified_id;
5746     }
5747   /* Otherwise, if we are in global scope, then we are looking at one
5748      of the other qualified-id productions.  */
5749   else if (global_scope_p)
5750     {
5751       cp_token *token;
5752       tree id;
5753 
5754       /* Peek at the next token.  */
5755       token = cp_lexer_peek_token (parser->lexer);
5756 
5757       /* If it's an identifier, and the next token is not a "<", then
5758 	 we can avoid the template-id case.  This is an optimization
5759 	 for this common case.  */
5760       if (token->type == CPP_NAME
5761 	  && !cp_parser_nth_token_starts_template_argument_list_p
5762 	       (parser, 2))
5763 	return cp_parser_identifier (parser);
5764 
5765       cp_parser_parse_tentatively (parser);
5766       /* Try a template-id.  */
5767       id = cp_parser_template_id (parser,
5768 				  /*template_keyword_p=*/false,
5769 				  /*check_dependency_p=*/true,
5770 				  none_type,
5771 				  declarator_p);
5772       /* If that worked, we're done.  */
5773       if (cp_parser_parse_definitely (parser))
5774 	return id;
5775 
5776       /* Peek at the next token.  (Changes in the token buffer may
5777 	 have invalidated the pointer obtained above.)  */
5778       token = cp_lexer_peek_token (parser->lexer);
5779 
5780       switch (token->type)
5781 	{
5782 	case CPP_NAME:
5783 	  return cp_parser_identifier (parser);
5784 
5785 	case CPP_KEYWORD:
5786 	  if (token->keyword == RID_OPERATOR)
5787 	    return cp_parser_operator_function_id (parser);
5788 	  /* Fall through.  */
5789 
5790 	default:
5791 	  cp_parser_error (parser, "expected id-expression");
5792 	  return error_mark_node;
5793 	}
5794     }
5795   else
5796     return cp_parser_unqualified_id (parser, template_keyword_p,
5797 				     /*check_dependency_p=*/true,
5798 				     declarator_p,
5799 				     optional_p);
5800 }
5801 
5802 /* Parse an unqualified-id.
5803 
5804    unqualified-id:
5805      identifier
5806      operator-function-id
5807      conversion-function-id
5808      ~ class-name
5809      template-id
5810 
5811    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5812    keyword, in a construct like `A::template ...'.
5813 
5814    Returns a representation of unqualified-id.  For the `identifier'
5815    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
5816    production a BIT_NOT_EXPR is returned; the operand of the
5817    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
5818    other productions, see the documentation accompanying the
5819    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
5820    names are looked up in uninstantiated templates.  If DECLARATOR_P
5821    is true, the unqualified-id is appearing as part of a declarator,
5822    rather than as part of an expression.  */
5823 
5824 static cp_expr
cp_parser_unqualified_id(cp_parser * parser,bool template_keyword_p,bool check_dependency_p,bool declarator_p,bool optional_p)5825 cp_parser_unqualified_id (cp_parser* parser,
5826 			  bool template_keyword_p,
5827 			  bool check_dependency_p,
5828 			  bool declarator_p,
5829 			  bool optional_p)
5830 {
5831   cp_token *token;
5832 
5833   /* Peek at the next token.  */
5834   token = cp_lexer_peek_token (parser->lexer);
5835 
5836   switch ((int) token->type)
5837     {
5838     case CPP_NAME:
5839       {
5840 	tree id;
5841 
5842 	/* We don't know yet whether or not this will be a
5843 	   template-id.  */
5844 	cp_parser_parse_tentatively (parser);
5845 	/* Try a template-id.  */
5846 	id = cp_parser_template_id (parser, template_keyword_p,
5847 				    check_dependency_p,
5848 				    none_type,
5849 				    declarator_p);
5850 	/* If it worked, we're done.  */
5851 	if (cp_parser_parse_definitely (parser))
5852 	  return id;
5853 	/* Otherwise, it's an ordinary identifier.  */
5854 	return cp_parser_identifier (parser);
5855       }
5856 
5857     case CPP_TEMPLATE_ID:
5858       return cp_parser_template_id (parser, template_keyword_p,
5859 				    check_dependency_p,
5860 				    none_type,
5861 				    declarator_p);
5862 
5863     case CPP_COMPL:
5864       {
5865 	tree type_decl;
5866 	tree qualifying_scope;
5867 	tree object_scope;
5868 	tree scope;
5869 	bool done;
5870 
5871 	/* Consume the `~' token.  */
5872 	cp_lexer_consume_token (parser->lexer);
5873 	/* Parse the class-name.  The standard, as written, seems to
5874 	   say that:
5875 
5876 	     template <typename T> struct S { ~S (); };
5877 	     template <typename T> S<T>::~S() {}
5878 
5879 	   is invalid, since `~' must be followed by a class-name, but
5880 	   `S<T>' is dependent, and so not known to be a class.
5881 	   That's not right; we need to look in uninstantiated
5882 	   templates.  A further complication arises from:
5883 
5884 	     template <typename T> void f(T t) {
5885 	       t.T::~T();
5886 	     }
5887 
5888 	   Here, it is not possible to look up `T' in the scope of `T'
5889 	   itself.  We must look in both the current scope, and the
5890 	   scope of the containing complete expression.
5891 
5892 	   Yet another issue is:
5893 
5894 	     struct S {
5895 	       int S;
5896 	       ~S();
5897 	     };
5898 
5899 	     S::~S() {}
5900 
5901 	   The standard does not seem to say that the `S' in `~S'
5902 	   should refer to the type `S' and not the data member
5903 	   `S::S'.  */
5904 
5905 	/* DR 244 says that we look up the name after the "~" in the
5906 	   same scope as we looked up the qualifying name.  That idea
5907 	   isn't fully worked out; it's more complicated than that.  */
5908 	scope = parser->scope;
5909 	object_scope = parser->object_scope;
5910 	qualifying_scope = parser->qualifying_scope;
5911 
5912 	/* Check for invalid scopes.  */
5913 	if (scope == error_mark_node)
5914 	  {
5915 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5916 	      cp_lexer_consume_token (parser->lexer);
5917 	    return error_mark_node;
5918 	  }
5919 	if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5920 	  {
5921 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5922 	      error_at (token->location,
5923 			"scope %qT before %<~%> is not a class-name",
5924 			scope);
5925 	    cp_parser_simulate_error (parser);
5926 	    if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5927 	      cp_lexer_consume_token (parser->lexer);
5928 	    return error_mark_node;
5929 	  }
5930 	gcc_assert (!scope || TYPE_P (scope));
5931 
5932 	/* If the name is of the form "X::~X" it's OK even if X is a
5933 	   typedef.  */
5934 	token = cp_lexer_peek_token (parser->lexer);
5935 	if (scope
5936 	    && token->type == CPP_NAME
5937 	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5938 		!= CPP_LESS)
5939 	    && (token->u.value == TYPE_IDENTIFIER (scope)
5940 		|| (CLASS_TYPE_P (scope)
5941 		    && constructor_name_p (token->u.value, scope))))
5942 	  {
5943 	    cp_lexer_consume_token (parser->lexer);
5944 	    return build_nt (BIT_NOT_EXPR, scope);
5945 	  }
5946 
5947 	/* ~auto means the destructor of whatever the object is.  */
5948 	if (cp_parser_is_keyword (token, RID_AUTO))
5949 	  {
5950 	    if (cxx_dialect < cxx14)
5951 	      pedwarn (input_location, 0,
5952 		       "%<~auto%> only available with "
5953 		       "-std=c++14 or -std=gnu++14");
5954 	    cp_lexer_consume_token (parser->lexer);
5955 	    return build_nt (BIT_NOT_EXPR, make_auto ());
5956 	  }
5957 
5958 	/* If there was an explicit qualification (S::~T), first look
5959 	   in the scope given by the qualification (i.e., S).
5960 
5961 	   Note: in the calls to cp_parser_class_name below we pass
5962 	   typename_type so that lookup finds the injected-class-name
5963 	   rather than the constructor.  */
5964 	done = false;
5965 	type_decl = NULL_TREE;
5966 	if (scope)
5967 	  {
5968 	    cp_parser_parse_tentatively (parser);
5969 	    type_decl = cp_parser_class_name (parser,
5970 					      /*typename_keyword_p=*/false,
5971 					      /*template_keyword_p=*/false,
5972 					      typename_type,
5973 					      /*check_dependency=*/false,
5974 					      /*class_head_p=*/false,
5975 					      declarator_p);
5976 	    if (cp_parser_parse_definitely (parser))
5977 	      done = true;
5978 	  }
5979 	/* In "N::S::~S", look in "N" as well.  */
5980 	if (!done && scope && qualifying_scope)
5981 	  {
5982 	    cp_parser_parse_tentatively (parser);
5983 	    parser->scope = qualifying_scope;
5984 	    parser->object_scope = NULL_TREE;
5985 	    parser->qualifying_scope = NULL_TREE;
5986 	    type_decl
5987 	      = cp_parser_class_name (parser,
5988 				      /*typename_keyword_p=*/false,
5989 				      /*template_keyword_p=*/false,
5990 				      typename_type,
5991 				      /*check_dependency=*/false,
5992 				      /*class_head_p=*/false,
5993 				      declarator_p);
5994 	    if (cp_parser_parse_definitely (parser))
5995 	      done = true;
5996 	  }
5997 	/* In "p->S::~T", look in the scope given by "*p" as well.  */
5998 	else if (!done && object_scope)
5999 	  {
6000 	    cp_parser_parse_tentatively (parser);
6001 	    parser->scope = object_scope;
6002 	    parser->object_scope = NULL_TREE;
6003 	    parser->qualifying_scope = NULL_TREE;
6004 	    type_decl
6005 	      = cp_parser_class_name (parser,
6006 				      /*typename_keyword_p=*/false,
6007 				      /*template_keyword_p=*/false,
6008 				      typename_type,
6009 				      /*check_dependency=*/false,
6010 				      /*class_head_p=*/false,
6011 				      declarator_p);
6012 	    if (cp_parser_parse_definitely (parser))
6013 	      done = true;
6014 	  }
6015 	/* Look in the surrounding context.  */
6016 	if (!done)
6017 	  {
6018 	    parser->scope = NULL_TREE;
6019 	    parser->object_scope = NULL_TREE;
6020 	    parser->qualifying_scope = NULL_TREE;
6021 	    if (processing_template_decl)
6022 	      cp_parser_parse_tentatively (parser);
6023 	    type_decl
6024 	      = cp_parser_class_name (parser,
6025 				      /*typename_keyword_p=*/false,
6026 				      /*template_keyword_p=*/false,
6027 				      typename_type,
6028 				      /*check_dependency=*/false,
6029 				      /*class_head_p=*/false,
6030 				      declarator_p);
6031 	    if (processing_template_decl
6032 		&& ! cp_parser_parse_definitely (parser))
6033 	      {
6034 		/* We couldn't find a type with this name.  If we're parsing
6035 		   tentatively, fail and try something else.  */
6036 		if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6037 		  {
6038 		    cp_parser_simulate_error (parser);
6039 		    return error_mark_node;
6040 		  }
6041 		/* Otherwise, accept it and check for a match at instantiation
6042 		   time.  */
6043 		type_decl = cp_parser_identifier (parser);
6044 		if (type_decl != error_mark_node)
6045 		  type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6046 		return type_decl;
6047 	      }
6048 	  }
6049 	/* If an error occurred, assume that the name of the
6050 	   destructor is the same as the name of the qualifying
6051 	   class.  That allows us to keep parsing after running
6052 	   into ill-formed destructor names.  */
6053 	if (type_decl == error_mark_node && scope)
6054 	  return build_nt (BIT_NOT_EXPR, scope);
6055 	else if (type_decl == error_mark_node)
6056 	  return error_mark_node;
6057 
6058 	/* Check that destructor name and scope match.  */
6059 	if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6060 	  {
6061 	    if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6062 	      error_at (token->location,
6063 			"declaration of %<~%T%> as member of %qT",
6064 			type_decl, scope);
6065 	    cp_parser_simulate_error (parser);
6066 	    return error_mark_node;
6067 	  }
6068 
6069 	/* [class.dtor]
6070 
6071 	   A typedef-name that names a class shall not be used as the
6072 	   identifier in the declarator for a destructor declaration.  */
6073 	if (declarator_p
6074 	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6075 	    && !DECL_SELF_REFERENCE_P (type_decl)
6076 	    && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6077 	  error_at (token->location,
6078 		    "typedef-name %qD used as destructor declarator",
6079 		    type_decl);
6080 
6081 	return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6082       }
6083 
6084     case CPP_KEYWORD:
6085       if (token->keyword == RID_OPERATOR)
6086 	{
6087 	  cp_expr id;
6088 
6089 	  /* This could be a template-id, so we try that first.  */
6090 	  cp_parser_parse_tentatively (parser);
6091 	  /* Try a template-id.  */
6092 	  id = cp_parser_template_id (parser, template_keyword_p,
6093 				      /*check_dependency_p=*/true,
6094 				      none_type,
6095 				      declarator_p);
6096 	  /* If that worked, we're done.  */
6097 	  if (cp_parser_parse_definitely (parser))
6098 	    return id;
6099 	  /* We still don't know whether we're looking at an
6100 	     operator-function-id or a conversion-function-id.  */
6101 	  cp_parser_parse_tentatively (parser);
6102 	  /* Try an operator-function-id.  */
6103 	  id = cp_parser_operator_function_id (parser);
6104 	  /* If that didn't work, try a conversion-function-id.  */
6105 	  if (!cp_parser_parse_definitely (parser))
6106 	    id = cp_parser_conversion_function_id (parser);
6107 
6108 	  return id;
6109 	}
6110       /* Fall through.  */
6111 
6112     default:
6113       if (optional_p)
6114 	return NULL_TREE;
6115       cp_parser_error (parser, "expected unqualified-id");
6116       return error_mark_node;
6117     }
6118 }
6119 
6120 /* Parse an (optional) nested-name-specifier.
6121 
6122    nested-name-specifier: [C++98]
6123      class-or-namespace-name :: nested-name-specifier [opt]
6124      class-or-namespace-name :: template nested-name-specifier [opt]
6125 
6126    nested-name-specifier: [C++0x]
6127      type-name ::
6128      namespace-name ::
6129      nested-name-specifier identifier ::
6130      nested-name-specifier template [opt] simple-template-id ::
6131 
6132    PARSER->SCOPE should be set appropriately before this function is
6133    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6134    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
6135    in name lookups.
6136 
6137    Sets PARSER->SCOPE to the class (TYPE) or namespace
6138    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6139    it unchanged if there is no nested-name-specifier.  Returns the new
6140    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6141 
6142    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6143    part of a declaration and/or decl-specifier.  */
6144 
6145 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)6146 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6147 				     bool typename_keyword_p,
6148 				     bool check_dependency_p,
6149 				     bool type_p,
6150 				     bool is_declaration,
6151 				     bool template_keyword_p /* = false */)
6152 {
6153   bool success = false;
6154   cp_token_position start = 0;
6155   cp_token *token;
6156 
6157   /* Remember where the nested-name-specifier starts.  */
6158   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6159     {
6160       start = cp_lexer_token_position (parser->lexer, false);
6161       push_deferring_access_checks (dk_deferred);
6162     }
6163 
6164   while (true)
6165     {
6166       tree new_scope;
6167       tree old_scope;
6168       tree saved_qualifying_scope;
6169 
6170       /* Spot cases that cannot be the beginning of a
6171 	 nested-name-specifier.  */
6172       token = cp_lexer_peek_token (parser->lexer);
6173 
6174       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6175 	 the already parsed nested-name-specifier.  */
6176       if (token->type == CPP_NESTED_NAME_SPECIFIER)
6177 	{
6178 	  /* Grab the nested-name-specifier and continue the loop.  */
6179 	  cp_parser_pre_parsed_nested_name_specifier (parser);
6180 	  /* If we originally encountered this nested-name-specifier
6181 	     with IS_DECLARATION set to false, we will not have
6182 	     resolved TYPENAME_TYPEs, so we must do so here.  */
6183 	  if (is_declaration
6184 	      && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6185 	    {
6186 	      new_scope = resolve_typename_type (parser->scope,
6187 						 /*only_current_p=*/false);
6188 	      if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6189 		parser->scope = new_scope;
6190 	    }
6191 	  success = true;
6192 	  continue;
6193 	}
6194 
6195       /* Spot cases that cannot be the beginning of a
6196 	 nested-name-specifier.  On the second and subsequent times
6197 	 through the loop, we look for the `template' keyword.  */
6198       if (success && token->keyword == RID_TEMPLATE)
6199 	;
6200       /* A template-id can start a nested-name-specifier.  */
6201       else if (token->type == CPP_TEMPLATE_ID)
6202 	;
6203       /* DR 743: decltype can be used in a nested-name-specifier.  */
6204       else if (token_is_decltype (token))
6205 	;
6206       else
6207 	{
6208 	  /* If the next token is not an identifier, then it is
6209 	     definitely not a type-name or namespace-name.  */
6210 	  if (token->type != CPP_NAME)
6211 	    break;
6212 	  /* If the following token is neither a `<' (to begin a
6213 	     template-id), nor a `::', then we are not looking at a
6214 	     nested-name-specifier.  */
6215 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
6216 
6217 	  if (token->type == CPP_COLON
6218 	      && parser->colon_corrects_to_scope_p
6219 	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6220 	    {
6221 	      gcc_rich_location richloc (token->location);
6222 	      richloc.add_fixit_replace ("::");
6223 	      error_at (&richloc,
6224 			"found %<:%> in nested-name-specifier, "
6225 			"expected %<::%>");
6226 	      token->type = CPP_SCOPE;
6227 	    }
6228 
6229 	  if (token->type != CPP_SCOPE
6230 	      && !cp_parser_nth_token_starts_template_argument_list_p
6231 		  (parser, 2))
6232 	    break;
6233 	}
6234 
6235       /* The nested-name-specifier is optional, so we parse
6236 	 tentatively.  */
6237       cp_parser_parse_tentatively (parser);
6238 
6239       /* Look for the optional `template' keyword, if this isn't the
6240 	 first time through the loop.  */
6241       if (success)
6242 	template_keyword_p = cp_parser_optional_template_keyword (parser);
6243 
6244       /* Save the old scope since the name lookup we are about to do
6245 	 might destroy it.  */
6246       old_scope = parser->scope;
6247       saved_qualifying_scope = parser->qualifying_scope;
6248       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6249 	 look up names in "X<T>::I" in order to determine that "Y" is
6250 	 a template.  So, if we have a typename at this point, we make
6251 	 an effort to look through it.  */
6252       if (is_declaration
6253 	  && !typename_keyword_p
6254 	  && parser->scope
6255 	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6256 	parser->scope = resolve_typename_type (parser->scope,
6257 					       /*only_current_p=*/false);
6258       /* Parse the qualifying entity.  */
6259       new_scope
6260 	= cp_parser_qualifying_entity (parser,
6261                                        typename_keyword_p,
6262                                        template_keyword_p,
6263                                        check_dependency_p,
6264                                        type_p,
6265                                        is_declaration);
6266       /* Look for the `::' token.  */
6267       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6268 
6269       /* If we found what we wanted, we keep going; otherwise, we're
6270 	 done.  */
6271       if (!cp_parser_parse_definitely (parser))
6272 	{
6273 	  bool error_p = false;
6274 
6275 	  /* Restore the OLD_SCOPE since it was valid before the
6276 	     failed attempt at finding the last
6277 	     class-or-namespace-name.  */
6278 	  parser->scope = old_scope;
6279 	  parser->qualifying_scope = saved_qualifying_scope;
6280 
6281 	  /* If the next token is a decltype, and the one after that is a
6282 	     `::', then the decltype has failed to resolve to a class or
6283 	     enumeration type.  Give this error even when parsing
6284 	     tentatively since it can't possibly be valid--and we're going
6285 	     to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6286 	     won't get another chance.*/
6287 	  if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6288 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6289 		  == CPP_SCOPE))
6290 	    {
6291 	      token = cp_lexer_consume_token (parser->lexer);
6292 	      error_at (token->location, "decltype evaluates to %qT, "
6293 			"which is not a class or enumeration type",
6294 			token->u.tree_check_value->value);
6295 	      parser->scope = error_mark_node;
6296 	      error_p = true;
6297 	      /* As below.  */
6298 	      success = true;
6299 	      cp_lexer_consume_token (parser->lexer);
6300 	    }
6301 
6302 	  if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6303 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6304 	    {
6305 	      /* If we have a non-type template-id followed by ::, it can't
6306 		 possibly be valid.  */
6307 	      token = cp_lexer_peek_token (parser->lexer);
6308 	      tree tid = token->u.tree_check_value->value;
6309 	      if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6310 		  && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6311 		{
6312 		  tree tmpl = NULL_TREE;
6313 		  if (is_overloaded_fn (tid))
6314 		    {
6315 		      tree fns = get_fns (tid);
6316 		      if (OVL_SINGLE_P (fns))
6317 			tmpl = OVL_FIRST (fns);
6318 		      error_at (token->location, "function template-id %qD "
6319 				"in nested-name-specifier", tid);
6320 		    }
6321 		  else
6322 		    {
6323 		      /* Variable template.  */
6324 		      tmpl = TREE_OPERAND (tid, 0);
6325 		      gcc_assert (variable_template_p (tmpl));
6326 		      error_at (token->location, "variable template-id %qD "
6327 				"in nested-name-specifier", tid);
6328 		    }
6329 		  if (tmpl)
6330 		    inform (DECL_SOURCE_LOCATION (tmpl),
6331 			    "%qD declared here", tmpl);
6332 
6333 		  parser->scope = error_mark_node;
6334 		  error_p = true;
6335 		  /* As below.  */
6336 		  success = true;
6337 		  cp_lexer_consume_token (parser->lexer);
6338 		  cp_lexer_consume_token (parser->lexer);
6339 		}
6340 	    }
6341 
6342 	  if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6343 	    break;
6344 	  /* If the next token is an identifier, and the one after
6345 	     that is a `::', then any valid interpretation would have
6346 	     found a class-or-namespace-name.  */
6347 	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6348 		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6349 		     == CPP_SCOPE)
6350 		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6351 		     != CPP_COMPL))
6352 	    {
6353 	      token = cp_lexer_consume_token (parser->lexer);
6354 	      if (!error_p)
6355 		{
6356 		  if (!token->error_reported)
6357 		    {
6358 		      tree decl;
6359 		      tree ambiguous_decls;
6360 
6361 		      decl = cp_parser_lookup_name (parser, token->u.value,
6362 						    none_type,
6363 						    /*is_template=*/false,
6364 						    /*is_namespace=*/false,
6365 						    /*check_dependency=*/true,
6366 						    &ambiguous_decls,
6367 						    token->location);
6368 		      if (TREE_CODE (decl) == TEMPLATE_DECL)
6369 			error_at (token->location,
6370 				  "%qD used without template parameters",
6371 				  decl);
6372 		      else if (ambiguous_decls)
6373 			{
6374 			  // cp_parser_lookup_name has the same diagnostic,
6375 			  // thus make sure to emit it at most once.
6376 			  if (cp_parser_uncommitted_to_tentative_parse_p
6377 			      (parser))
6378 			    {
6379 			      error_at (token->location,
6380 					"reference to %qD is ambiguous",
6381 					token->u.value);
6382 			      print_candidates (ambiguous_decls);
6383 			    }
6384 			  decl = error_mark_node;
6385 			}
6386 		      else
6387                         {
6388                           if (cxx_dialect != cxx98)
6389                             cp_parser_name_lookup_error
6390                             (parser, token->u.value, decl, NLE_NOT_CXX98,
6391 	  		     token->location);
6392 			  else
6393 			    cp_parser_name_lookup_error
6394 			    (parser, token->u.value, decl, NLE_CXX98,
6395 			     token->location);
6396                         }
6397 		    }
6398 		  parser->scope = error_mark_node;
6399 		  error_p = true;
6400 		  /* Treat this as a successful nested-name-specifier
6401 		     due to:
6402 
6403 		     [basic.lookup.qual]
6404 
6405 		     If the name found is not a class-name (clause
6406 		     _class_) or namespace-name (_namespace.def_), the
6407 		     program is ill-formed.  */
6408 		  success = true;
6409 		}
6410 	      cp_lexer_consume_token (parser->lexer);
6411 	    }
6412 	  break;
6413 	}
6414       /* We've found one valid nested-name-specifier.  */
6415       success = true;
6416       /* Name lookup always gives us a DECL.  */
6417       if (TREE_CODE (new_scope) == TYPE_DECL)
6418 	new_scope = TREE_TYPE (new_scope);
6419       /* Uses of "template" must be followed by actual templates.  */
6420       if (template_keyword_p
6421 	  && !(CLASS_TYPE_P (new_scope)
6422 	       && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6423 		    && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6424 		   || CLASSTYPE_IS_TEMPLATE (new_scope)))
6425 	  && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6426 	       && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6427 		   == TEMPLATE_ID_EXPR)))
6428 	permerror (input_location, TYPE_P (new_scope)
6429 		   ? G_("%qT is not a template")
6430 		   : G_("%qD is not a template"),
6431 		   new_scope);
6432       /* If it is a class scope, try to complete it; we are about to
6433 	 be looking up names inside the class.  */
6434       if (TYPE_P (new_scope)
6435 	  /* Since checking types for dependency can be expensive,
6436 	     avoid doing it if the type is already complete.  */
6437 	  && !COMPLETE_TYPE_P (new_scope)
6438 	  /* Do not try to complete dependent types.  */
6439 	  && !dependent_type_p (new_scope))
6440 	{
6441 	  new_scope = complete_type (new_scope);
6442 	  /* If it is a typedef to current class, use the current
6443 	     class instead, as the typedef won't have any names inside
6444 	     it yet.  */
6445 	  if (!COMPLETE_TYPE_P (new_scope)
6446 	      && currently_open_class (new_scope))
6447 	    new_scope = TYPE_MAIN_VARIANT (new_scope);
6448 	}
6449       /* Make sure we look in the right scope the next time through
6450 	 the loop.  */
6451       parser->scope = new_scope;
6452     }
6453 
6454   /* If parsing tentatively, replace the sequence of tokens that makes
6455      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6456      token.  That way, should we re-parse the token stream, we will
6457      not have to repeat the effort required to do the parse, nor will
6458      we issue duplicate error messages.  */
6459   if (success && start)
6460     {
6461       cp_token *token;
6462 
6463       token = cp_lexer_token_at (parser->lexer, start);
6464       /* Reset the contents of the START token.  */
6465       token->type = CPP_NESTED_NAME_SPECIFIER;
6466       /* Retrieve any deferred checks.  Do not pop this access checks yet
6467 	 so the memory will not be reclaimed during token replacing below.  */
6468       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6469       token->u.tree_check_value->value = parser->scope;
6470       token->u.tree_check_value->checks = get_deferred_access_checks ();
6471       token->u.tree_check_value->qualifying_scope =
6472 	parser->qualifying_scope;
6473       token->keyword = RID_MAX;
6474 
6475       /* Purge all subsequent tokens.  */
6476       cp_lexer_purge_tokens_after (parser->lexer, start);
6477     }
6478 
6479   if (start)
6480     pop_to_parent_deferring_access_checks ();
6481 
6482   return success ? parser->scope : NULL_TREE;
6483 }
6484 
6485 /* Parse a nested-name-specifier.  See
6486    cp_parser_nested_name_specifier_opt for details.  This function
6487    behaves identically, except that it will an issue an error if no
6488    nested-name-specifier is present.  */
6489 
6490 static tree
cp_parser_nested_name_specifier(cp_parser * parser,bool typename_keyword_p,bool check_dependency_p,bool type_p,bool is_declaration)6491 cp_parser_nested_name_specifier (cp_parser *parser,
6492 				 bool typename_keyword_p,
6493 				 bool check_dependency_p,
6494 				 bool type_p,
6495 				 bool is_declaration)
6496 {
6497   tree scope;
6498 
6499   /* Look for the nested-name-specifier.  */
6500   scope = cp_parser_nested_name_specifier_opt (parser,
6501 					       typename_keyword_p,
6502 					       check_dependency_p,
6503 					       type_p,
6504 					       is_declaration);
6505   /* If it was not present, issue an error message.  */
6506   if (!scope)
6507     {
6508       cp_parser_error (parser, "expected nested-name-specifier");
6509       parser->scope = NULL_TREE;
6510     }
6511 
6512   return scope;
6513 }
6514 
6515 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6516    this is either a class-name or a namespace-name (which corresponds
6517    to the class-or-namespace-name production in the grammar). For
6518    C++0x, it can also be a type-name that refers to an enumeration
6519    type or a simple-template-id.
6520 
6521    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6522    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6523    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6524    TYPE_P is TRUE iff the next name should be taken as a class-name,
6525    even the same name is declared to be another entity in the same
6526    scope.
6527 
6528    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6529    specified by the class-or-namespace-name.  If neither is found the
6530    ERROR_MARK_NODE is returned.  */
6531 
6532 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)6533 cp_parser_qualifying_entity (cp_parser *parser,
6534 			     bool typename_keyword_p,
6535 			     bool template_keyword_p,
6536 			     bool check_dependency_p,
6537 			     bool type_p,
6538 			     bool is_declaration)
6539 {
6540   tree saved_scope;
6541   tree saved_qualifying_scope;
6542   tree saved_object_scope;
6543   tree scope;
6544   bool only_class_p;
6545   bool successful_parse_p;
6546 
6547   /* DR 743: decltype can appear in a nested-name-specifier.  */
6548   if (cp_lexer_next_token_is_decltype (parser->lexer))
6549     {
6550       scope = cp_parser_decltype (parser);
6551       if (TREE_CODE (scope) != ENUMERAL_TYPE
6552 	  && !MAYBE_CLASS_TYPE_P (scope))
6553 	{
6554 	  cp_parser_simulate_error (parser);
6555 	  return error_mark_node;
6556 	}
6557       if (TYPE_NAME (scope))
6558 	scope = TYPE_NAME (scope);
6559       return scope;
6560     }
6561 
6562   /* Before we try to parse the class-name, we must save away the
6563      current PARSER->SCOPE since cp_parser_class_name will destroy
6564      it.  */
6565   saved_scope = parser->scope;
6566   saved_qualifying_scope = parser->qualifying_scope;
6567   saved_object_scope = parser->object_scope;
6568   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
6569      there is no need to look for a namespace-name.  */
6570   only_class_p = template_keyword_p
6571     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6572   if (!only_class_p)
6573     cp_parser_parse_tentatively (parser);
6574   scope = cp_parser_class_name (parser,
6575 				typename_keyword_p,
6576 				template_keyword_p,
6577 				type_p ? class_type : none_type,
6578 				check_dependency_p,
6579 				/*class_head_p=*/false,
6580 				is_declaration,
6581 				/*enum_ok=*/cxx_dialect > cxx98);
6582   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6583   /* If that didn't work, try for a namespace-name.  */
6584   if (!only_class_p && !successful_parse_p)
6585     {
6586       /* Restore the saved scope.  */
6587       parser->scope = saved_scope;
6588       parser->qualifying_scope = saved_qualifying_scope;
6589       parser->object_scope = saved_object_scope;
6590       /* If we are not looking at an identifier followed by the scope
6591 	 resolution operator, then this is not part of a
6592 	 nested-name-specifier.  (Note that this function is only used
6593 	 to parse the components of a nested-name-specifier.)  */
6594       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6595 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6596 	return error_mark_node;
6597       scope = cp_parser_namespace_name (parser);
6598     }
6599 
6600   return scope;
6601 }
6602 
6603 /* Return true if we are looking at a compound-literal, false otherwise.  */
6604 
6605 static bool
cp_parser_compound_literal_p(cp_parser * parser)6606 cp_parser_compound_literal_p (cp_parser *parser)
6607 {
6608   cp_lexer_save_tokens (parser->lexer);
6609 
6610   /* Skip tokens until the next token is a closing parenthesis.
6611      If we find the closing `)', and the next token is a `{', then
6612      we are looking at a compound-literal.  */
6613   bool compound_literal_p
6614     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6615 					      /*consume_paren=*/true)
6616        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6617 
6618   /* Roll back the tokens we skipped.  */
6619   cp_lexer_rollback_tokens (parser->lexer);
6620 
6621   return compound_literal_p;
6622 }
6623 
6624 /* Return true if EXPR is the integer constant zero or a complex constant
6625    of zero, without any folding, but ignoring location wrappers.  */
6626 
6627 static bool
literal_integer_zerop(const_tree expr)6628 literal_integer_zerop (const_tree expr)
6629 {
6630   STRIP_ANY_LOCATION_WRAPPER (expr);
6631   return integer_zerop (expr);
6632 }
6633 
6634 /* Parse a postfix-expression.
6635 
6636    postfix-expression:
6637      primary-expression
6638      postfix-expression [ expression ]
6639      postfix-expression ( expression-list [opt] )
6640      simple-type-specifier ( expression-list [opt] )
6641      typename :: [opt] nested-name-specifier identifier
6642        ( expression-list [opt] )
6643      typename :: [opt] nested-name-specifier template [opt] template-id
6644        ( expression-list [opt] )
6645      postfix-expression . template [opt] id-expression
6646      postfix-expression -> template [opt] id-expression
6647      postfix-expression . pseudo-destructor-name
6648      postfix-expression -> pseudo-destructor-name
6649      postfix-expression ++
6650      postfix-expression --
6651      dynamic_cast < type-id > ( expression )
6652      static_cast < type-id > ( expression )
6653      reinterpret_cast < type-id > ( expression )
6654      const_cast < type-id > ( expression )
6655      typeid ( expression )
6656      typeid ( type-id )
6657 
6658    GNU Extension:
6659 
6660    postfix-expression:
6661      ( type-id ) { initializer-list , [opt] }
6662 
6663    This extension is a GNU version of the C99 compound-literal
6664    construct.  (The C99 grammar uses `type-name' instead of `type-id',
6665    but they are essentially the same concept.)
6666 
6667    If ADDRESS_P is true, the postfix expression is the operand of the
6668    `&' operator.  CAST_P is true if this expression is the target of a
6669    cast.
6670 
6671    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6672    class member access expressions [expr.ref].
6673 
6674    Returns a representation of the expression.  */
6675 
6676 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)6677 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6678                               bool member_access_only_p, bool decltype_p,
6679 			      cp_id_kind * pidk_return)
6680 {
6681   cp_token *token;
6682   location_t loc;
6683   enum rid keyword;
6684   cp_id_kind idk = CP_ID_KIND_NONE;
6685   cp_expr postfix_expression = NULL_TREE;
6686   bool is_member_access = false;
6687 
6688   /* Peek at the next token.  */
6689   token = cp_lexer_peek_token (parser->lexer);
6690   loc = token->location;
6691   location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6692 
6693   /* Some of the productions are determined by keywords.  */
6694   keyword = token->keyword;
6695   switch (keyword)
6696     {
6697     case RID_DYNCAST:
6698     case RID_STATCAST:
6699     case RID_REINTCAST:
6700     case RID_CONSTCAST:
6701       {
6702 	tree type;
6703 	cp_expr expression;
6704 	const char *saved_message;
6705 	bool saved_in_type_id_in_expr_p;
6706 
6707 	/* All of these can be handled in the same way from the point
6708 	   of view of parsing.  Begin by consuming the token
6709 	   identifying the cast.  */
6710 	cp_lexer_consume_token (parser->lexer);
6711 
6712 	/* New types cannot be defined in the cast.  */
6713 	saved_message = parser->type_definition_forbidden_message;
6714 	parser->type_definition_forbidden_message
6715 	  = G_("types may not be defined in casts");
6716 
6717 	/* Look for the opening `<'.  */
6718 	cp_parser_require (parser, CPP_LESS, RT_LESS);
6719 	/* Parse the type to which we are casting.  */
6720 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6721 	parser->in_type_id_in_expr_p = true;
6722 	type = cp_parser_type_id (parser);
6723 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6724 	/* Look for the closing `>'.  */
6725 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6726 	/* Restore the old message.  */
6727 	parser->type_definition_forbidden_message = saved_message;
6728 
6729 	bool saved_greater_than_is_operator_p
6730 	  = parser->greater_than_is_operator_p;
6731 	parser->greater_than_is_operator_p = true;
6732 
6733 	/* And the expression which is being cast.  */
6734 	matching_parens parens;
6735 	parens.require_open (parser);
6736 	expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6737 	cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6738 						   RT_CLOSE_PAREN);
6739 	location_t end_loc = close_paren ?
6740 	  close_paren->location : UNKNOWN_LOCATION;
6741 
6742 	parser->greater_than_is_operator_p
6743 	  = saved_greater_than_is_operator_p;
6744 
6745 	/* Only type conversions to integral or enumeration types
6746 	   can be used in constant-expressions.  */
6747 	if (!cast_valid_in_integral_constant_expression_p (type)
6748 	    && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6749 	  {
6750 	    postfix_expression = error_mark_node;
6751 	    break;
6752 	  }
6753 
6754 	switch (keyword)
6755 	  {
6756 	  case RID_DYNCAST:
6757 	    postfix_expression
6758 	      = build_dynamic_cast (type, expression, tf_warning_or_error);
6759 	    break;
6760 	  case RID_STATCAST:
6761 	    postfix_expression
6762 	      = build_static_cast (type, expression, tf_warning_or_error);
6763 	    break;
6764 	  case RID_REINTCAST:
6765 	    postfix_expression
6766 	      = build_reinterpret_cast (type, expression,
6767                                         tf_warning_or_error);
6768 	    break;
6769 	  case RID_CONSTCAST:
6770 	    postfix_expression
6771 	      = build_const_cast (type, expression, tf_warning_or_error);
6772 	    break;
6773 	  default:
6774 	    gcc_unreachable ();
6775 	  }
6776 
6777 	/* Construct a location e.g. :
6778 	     reinterpret_cast <int *> (expr)
6779 	     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6780 	   ranging from the start of the "*_cast" token to the final closing
6781 	   paren, with the caret at the start.  */
6782 	location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6783 	postfix_expression.set_location (cp_cast_loc);
6784       }
6785       break;
6786 
6787     case RID_TYPEID:
6788       {
6789 	tree type;
6790 	const char *saved_message;
6791 	bool saved_in_type_id_in_expr_p;
6792 
6793 	/* Consume the `typeid' token.  */
6794 	cp_lexer_consume_token (parser->lexer);
6795 	/* Look for the `(' token.  */
6796 	matching_parens parens;
6797 	parens.require_open (parser);
6798 	/* Types cannot be defined in a `typeid' expression.  */
6799 	saved_message = parser->type_definition_forbidden_message;
6800 	parser->type_definition_forbidden_message
6801 	  = G_("types may not be defined in a %<typeid%> expression");
6802 	/* We can't be sure yet whether we're looking at a type-id or an
6803 	   expression.  */
6804 	cp_parser_parse_tentatively (parser);
6805 	/* Try a type-id first.  */
6806 	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6807 	parser->in_type_id_in_expr_p = true;
6808 	type = cp_parser_type_id (parser);
6809 	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6810 	/* Look for the `)' token.  Otherwise, we can't be sure that
6811 	   we're not looking at an expression: consider `typeid (int
6812 	   (3))', for example.  */
6813 	cp_token *close_paren = parens.require_close (parser);
6814 	/* If all went well, simply lookup the type-id.  */
6815 	if (cp_parser_parse_definitely (parser))
6816 	  postfix_expression = get_typeid (type, tf_warning_or_error);
6817 	/* Otherwise, fall back to the expression variant.  */
6818 	else
6819 	  {
6820 	    tree expression;
6821 
6822 	    /* Look for an expression.  */
6823 	    expression = cp_parser_expression (parser, & idk);
6824 	    /* Compute its typeid.  */
6825 	    postfix_expression = build_typeid (expression, tf_warning_or_error);
6826 	    /* Look for the `)' token.  */
6827 	    close_paren = parens.require_close (parser);
6828 	  }
6829 	/* Restore the saved message.  */
6830 	parser->type_definition_forbidden_message = saved_message;
6831 	/* `typeid' may not appear in an integral constant expression.  */
6832 	if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6833 	  postfix_expression = error_mark_node;
6834 
6835 	/* Construct a location e.g. :
6836 	     typeid (expr)
6837 	     ^~~~~~~~~~~~~
6838 	   ranging from the start of the "typeid" token to the final closing
6839 	   paren, with the caret at the start.  */
6840 	if (close_paren)
6841 	  {
6842 	    location_t typeid_loc
6843 	      = make_location (start_loc, start_loc, close_paren->location);
6844 	    postfix_expression.set_location (typeid_loc);
6845 	    postfix_expression.maybe_add_location_wrapper ();
6846 	  }
6847       }
6848       break;
6849 
6850     case RID_TYPENAME:
6851       {
6852 	tree type;
6853 	/* The syntax permitted here is the same permitted for an
6854 	   elaborated-type-specifier.  */
6855         ++parser->prevent_constrained_type_specifiers;
6856 	type = cp_parser_elaborated_type_specifier (parser,
6857 						    /*is_friend=*/false,
6858 						    /*is_declaration=*/false);
6859         --parser->prevent_constrained_type_specifiers;
6860 	postfix_expression = cp_parser_functional_cast (parser, type);
6861       }
6862       break;
6863 
6864     case RID_ADDRESSOF:
6865     case RID_BUILTIN_SHUFFLE:
6866     case RID_BUILTIN_LAUNDER:
6867       {
6868 	vec<tree, va_gc> *vec;
6869 	unsigned int i;
6870 	tree p;
6871 
6872 	cp_lexer_consume_token (parser->lexer);
6873 	vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6874 		    /*cast_p=*/false, /*allow_expansion_p=*/true,
6875 		    /*non_constant_p=*/NULL);
6876 	if (vec == NULL)
6877 	  {
6878 	    postfix_expression = error_mark_node;
6879 	    break;
6880 	  }
6881 
6882 	FOR_EACH_VEC_ELT (*vec, i, p)
6883 	  mark_exp_read (p);
6884 
6885 	switch (keyword)
6886 	  {
6887 	  case RID_ADDRESSOF:
6888 	    if (vec->length () == 1)
6889 	      postfix_expression
6890 		= cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6891 	    else
6892 	      {
6893 		error_at (loc, "wrong number of arguments to "
6894 			       "%<__builtin_addressof%>");
6895 		postfix_expression = error_mark_node;
6896 	      }
6897 	    break;
6898 
6899 	  case RID_BUILTIN_LAUNDER:
6900 	    if (vec->length () == 1)
6901 	      postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6902 							   tf_warning_or_error);
6903 	    else
6904 	      {
6905 		error_at (loc, "wrong number of arguments to "
6906 			       "%<__builtin_launder%>");
6907 		postfix_expression = error_mark_node;
6908 	      }
6909 	    break;
6910 
6911 	  case RID_BUILTIN_SHUFFLE:
6912 	    if (vec->length () == 2)
6913 	      postfix_expression
6914 		= build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6915 					 (*vec)[1], tf_warning_or_error);
6916 	    else if (vec->length () == 3)
6917 	      postfix_expression
6918 		= build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6919 					 (*vec)[2], tf_warning_or_error);
6920 	    else
6921 	      {
6922 		error_at (loc, "wrong number of arguments to "
6923 			       "%<__builtin_shuffle%>");
6924 		postfix_expression = error_mark_node;
6925 	      }
6926 	    break;
6927 
6928 	  default:
6929 	    gcc_unreachable ();
6930 	  }
6931 	break;
6932       }
6933 
6934     default:
6935       {
6936 	tree type;
6937 
6938 	/* If the next thing is a simple-type-specifier, we may be
6939 	   looking at a functional cast.  We could also be looking at
6940 	   an id-expression.  So, we try the functional cast, and if
6941 	   that doesn't work we fall back to the primary-expression.  */
6942 	cp_parser_parse_tentatively (parser);
6943 	/* Look for the simple-type-specifier.  */
6944         ++parser->prevent_constrained_type_specifiers;
6945 	type = cp_parser_simple_type_specifier (parser,
6946 						/*decl_specs=*/NULL,
6947 						CP_PARSER_FLAGS_NONE);
6948         --parser->prevent_constrained_type_specifiers;
6949 	/* Parse the cast itself.  */
6950 	if (!cp_parser_error_occurred (parser))
6951 	  postfix_expression
6952 	    = cp_parser_functional_cast (parser, type);
6953 	/* If that worked, we're done.  */
6954 	if (cp_parser_parse_definitely (parser))
6955 	  break;
6956 
6957 	/* If the functional-cast didn't work out, try a
6958 	   compound-literal.  */
6959 	if (cp_parser_allow_gnu_extensions_p (parser)
6960 	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6961 	  {
6962 	    cp_expr initializer = NULL_TREE;
6963 
6964 	    cp_parser_parse_tentatively (parser);
6965 
6966 	    matching_parens parens;
6967 	    parens.consume_open (parser);
6968 
6969 	    /* Avoid calling cp_parser_type_id pointlessly, see comment
6970 	       in cp_parser_cast_expression about c++/29234.  */
6971 	    if (!cp_parser_compound_literal_p (parser))
6972 	      cp_parser_simulate_error (parser);
6973 	    else
6974 	      {
6975 		/* Parse the type.  */
6976 		bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6977 		parser->in_type_id_in_expr_p = true;
6978 		type = cp_parser_type_id (parser);
6979 		parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6980 		parens.require_close (parser);
6981 	      }
6982 
6983 	    /* If things aren't going well, there's no need to
6984 	       keep going.  */
6985 	    if (!cp_parser_error_occurred (parser))
6986 	      {
6987 		bool non_constant_p;
6988 		/* Parse the brace-enclosed initializer list.  */
6989 		initializer = cp_parser_braced_list (parser,
6990 						     &non_constant_p);
6991 	      }
6992 	    /* If that worked, we're definitely looking at a
6993 	       compound-literal expression.  */
6994 	    if (cp_parser_parse_definitely (parser))
6995 	      {
6996 		/* Warn the user that a compound literal is not
6997 		   allowed in standard C++.  */
6998 		pedwarn (input_location, OPT_Wpedantic,
6999 			 "ISO C++ forbids compound-literals");
7000 		/* For simplicity, we disallow compound literals in
7001 		   constant-expressions.  We could
7002 		   allow compound literals of integer type, whose
7003 		   initializer was a constant, in constant
7004 		   expressions.  Permitting that usage, as a further
7005 		   extension, would not change the meaning of any
7006 		   currently accepted programs.  (Of course, as
7007 		   compound literals are not part of ISO C++, the
7008 		   standard has nothing to say.)  */
7009 		if (cp_parser_non_integral_constant_expression (parser,
7010 								NIC_NCC))
7011 		  {
7012 		    postfix_expression = error_mark_node;
7013 		    break;
7014 		  }
7015 		/* Form the representation of the compound-literal.  */
7016 		postfix_expression
7017 		  = finish_compound_literal (type, initializer,
7018 					     tf_warning_or_error, fcl_c99);
7019 		postfix_expression.set_location (initializer.get_location ());
7020 		break;
7021 	      }
7022 	  }
7023 
7024 	/* It must be a primary-expression.  */
7025 	postfix_expression
7026 	  = cp_parser_primary_expression (parser, address_p, cast_p,
7027 					  /*template_arg_p=*/false,
7028 					  decltype_p,
7029 					  &idk);
7030       }
7031       break;
7032     }
7033 
7034   /* Note that we don't need to worry about calling build_cplus_new on a
7035      class-valued CALL_EXPR in decltype when it isn't the end of the
7036      postfix-expression; unary_complex_lvalue will take care of that for
7037      all these cases.  */
7038 
7039   /* Keep looping until the postfix-expression is complete.  */
7040   while (true)
7041     {
7042       if (idk == CP_ID_KIND_UNQUALIFIED
7043 	  && identifier_p (postfix_expression)
7044 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7045 	/* It is not a Koenig lookup function call.  */
7046 	postfix_expression
7047 	  = unqualified_name_lookup_error (postfix_expression);
7048 
7049       /* Peek at the next token.  */
7050       token = cp_lexer_peek_token (parser->lexer);
7051 
7052       switch (token->type)
7053 	{
7054 	case CPP_OPEN_SQUARE:
7055 	  if (cp_next_tokens_can_be_std_attribute_p (parser))
7056 	    {
7057 	      cp_parser_error (parser,
7058 			       "two consecutive %<[%> shall "
7059 			       "only introduce an attribute");
7060 	      return error_mark_node;
7061 	    }
7062 	  postfix_expression
7063 	    = cp_parser_postfix_open_square_expression (parser,
7064 							postfix_expression,
7065 							false,
7066 							decltype_p);
7067 	  postfix_expression.set_range (start_loc,
7068 					postfix_expression.get_location ());
7069 
7070 	  idk = CP_ID_KIND_NONE;
7071           is_member_access = false;
7072 	  break;
7073 
7074 	case CPP_OPEN_PAREN:
7075 	  /* postfix-expression ( expression-list [opt] ) */
7076 	  {
7077 	    bool koenig_p;
7078 	    bool is_builtin_constant_p;
7079 	    bool saved_integral_constant_expression_p = false;
7080 	    bool saved_non_integral_constant_expression_p = false;
7081 	    tsubst_flags_t complain = complain_flags (decltype_p);
7082 	    vec<tree, va_gc> *args;
7083 	    location_t close_paren_loc = UNKNOWN_LOCATION;
7084 
7085             is_member_access = false;
7086 
7087 	    is_builtin_constant_p
7088 	      = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7089 	    if (is_builtin_constant_p)
7090 	      {
7091 		/* The whole point of __builtin_constant_p is to allow
7092 		   non-constant expressions to appear as arguments.  */
7093 		saved_integral_constant_expression_p
7094 		  = parser->integral_constant_expression_p;
7095 		saved_non_integral_constant_expression_p
7096 		  = parser->non_integral_constant_expression_p;
7097 		parser->integral_constant_expression_p = false;
7098 	      }
7099 	    args = (cp_parser_parenthesized_expression_list
7100 		    (parser, non_attr,
7101 		     /*cast_p=*/false, /*allow_expansion_p=*/true,
7102 		     /*non_constant_p=*/NULL,
7103 		     /*close_paren_loc=*/&close_paren_loc,
7104 		     /*wrap_locations_p=*/true));
7105 	    if (is_builtin_constant_p)
7106 	      {
7107 		parser->integral_constant_expression_p
7108 		  = saved_integral_constant_expression_p;
7109 		parser->non_integral_constant_expression_p
7110 		  = saved_non_integral_constant_expression_p;
7111 	      }
7112 
7113 	    if (args == NULL)
7114 	      {
7115 		postfix_expression = error_mark_node;
7116 		break;
7117 	      }
7118 
7119 	    /* Function calls are not permitted in
7120 	       constant-expressions.  */
7121 	    if (! builtin_valid_in_constant_expr_p (postfix_expression)
7122 		&& cp_parser_non_integral_constant_expression (parser,
7123 							       NIC_FUNC_CALL))
7124 	      {
7125 		postfix_expression = error_mark_node;
7126 		release_tree_vector (args);
7127 		break;
7128 	      }
7129 
7130 	    koenig_p = false;
7131 	    if (idk == CP_ID_KIND_UNQUALIFIED
7132 		|| idk == CP_ID_KIND_TEMPLATE_ID)
7133 	      {
7134 		if (identifier_p (postfix_expression))
7135 		  {
7136 		    if (!args->is_empty ())
7137 		      {
7138 			koenig_p = true;
7139 			if (!any_type_dependent_arguments_p (args))
7140 			  postfix_expression
7141 			    = perform_koenig_lookup (postfix_expression, args,
7142 						     complain);
7143 		      }
7144 		    else
7145 		      postfix_expression
7146 			= unqualified_fn_lookup_error (postfix_expression);
7147 		  }
7148 		/* We do not perform argument-dependent lookup if
7149 		   normal lookup finds a non-function, in accordance
7150 		   with the expected resolution of DR 218.  */
7151 		else if (!args->is_empty ()
7152 			 && is_overloaded_fn (postfix_expression))
7153 		  {
7154 		    /* We only need to look at the first function,
7155 		       because all the fns share the attribute we're
7156 		       concerned with (all member fns or all local
7157 		       fns).  */
7158 		    tree fn = get_first_fn (postfix_expression);
7159 		    fn = STRIP_TEMPLATE (fn);
7160 
7161 		    /* Do not do argument dependent lookup if regular
7162 		       lookup finds a member function or a block-scope
7163 		       function declaration.  [basic.lookup.argdep]/3  */
7164 		    if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7165 			  || DECL_FUNCTION_MEMBER_P (fn)
7166 			  || DECL_LOCAL_FUNCTION_P (fn)))
7167 		      {
7168 			koenig_p = true;
7169 			if (!any_type_dependent_arguments_p (args))
7170 			  postfix_expression
7171 			    = perform_koenig_lookup (postfix_expression, args,
7172 						     complain);
7173 		      }
7174 		  }
7175 	      }
7176 
7177 	    if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7178 		&& DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7179 		&& DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7180 		&& vec_safe_length (args) == 3)
7181 	      {
7182 		tree arg0 = (*args)[0];
7183 		tree arg1 = (*args)[1];
7184 		tree arg2 = (*args)[2];
7185 		int literal_mask = ((literal_integer_zerop (arg1) << 1)
7186 				    | (literal_integer_zerop (arg2) << 2));
7187 		warn_for_memset (input_location, arg0, arg2, literal_mask);
7188 	      }
7189 
7190 	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7191 	      {
7192 		tree instance = TREE_OPERAND (postfix_expression, 0);
7193 		tree fn = TREE_OPERAND (postfix_expression, 1);
7194 
7195 		if (processing_template_decl
7196 		    && (type_dependent_object_expression_p (instance)
7197 			|| (!BASELINK_P (fn)
7198 			    && TREE_CODE (fn) != FIELD_DECL)
7199 			|| type_dependent_expression_p (fn)
7200 			|| any_type_dependent_arguments_p (args)))
7201 		  {
7202 		    maybe_generic_this_capture (instance, fn);
7203 		    postfix_expression
7204 		      = build_min_nt_call_vec (postfix_expression, args);
7205 		    release_tree_vector (args);
7206 		    break;
7207 		  }
7208 
7209 		if (BASELINK_P (fn))
7210 		  {
7211 		  postfix_expression
7212 		    = (build_new_method_call
7213 		       (instance, fn, &args, NULL_TREE,
7214 			(idk == CP_ID_KIND_QUALIFIED
7215 			 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7216 			 : LOOKUP_NORMAL),
7217 			/*fn_p=*/NULL,
7218 			complain));
7219 		  }
7220 		else
7221 		  postfix_expression
7222 		    = finish_call_expr (postfix_expression, &args,
7223 					/*disallow_virtual=*/false,
7224 					/*koenig_p=*/false,
7225 					complain);
7226 	      }
7227 	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
7228 		     || TREE_CODE (postfix_expression) == MEMBER_REF
7229 		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7230 	      postfix_expression = (build_offset_ref_call_from_tree
7231 				    (postfix_expression, &args,
7232 				     complain));
7233 	    else if (idk == CP_ID_KIND_QUALIFIED)
7234 	      /* A call to a static class member, or a namespace-scope
7235 		 function.  */
7236 	      postfix_expression
7237 		= finish_call_expr (postfix_expression, &args,
7238 				    /*disallow_virtual=*/true,
7239 				    koenig_p,
7240 				    complain);
7241 	    else
7242 	      /* All other function calls.  */
7243 	      postfix_expression
7244 		= finish_call_expr (postfix_expression, &args,
7245 				    /*disallow_virtual=*/false,
7246 				    koenig_p,
7247 				    complain);
7248 
7249 	    if (close_paren_loc != UNKNOWN_LOCATION)
7250 	      {
7251 		location_t combined_loc = make_location (token->location,
7252 							 start_loc,
7253 							 close_paren_loc);
7254 		postfix_expression.set_location (combined_loc);
7255 	      }
7256 
7257 	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
7258 	    idk = CP_ID_KIND_NONE;
7259 
7260 	    release_tree_vector (args);
7261 	  }
7262 	  break;
7263 
7264 	case CPP_DOT:
7265 	case CPP_DEREF:
7266 	  /* postfix-expression . template [opt] id-expression
7267 	     postfix-expression . pseudo-destructor-name
7268 	     postfix-expression -> template [opt] id-expression
7269 	     postfix-expression -> pseudo-destructor-name */
7270 
7271 	  /* Consume the `.' or `->' operator.  */
7272 	  cp_lexer_consume_token (parser->lexer);
7273 
7274 	  postfix_expression
7275 	    = cp_parser_postfix_dot_deref_expression (parser, token->type,
7276 						      postfix_expression,
7277 						      false, &idk, loc);
7278 
7279           is_member_access = true;
7280 	  break;
7281 
7282 	case CPP_PLUS_PLUS:
7283 	  /* postfix-expression ++  */
7284 	  /* Consume the `++' token.  */
7285 	  cp_lexer_consume_token (parser->lexer);
7286 	  /* Generate a representation for the complete expression.  */
7287 	  postfix_expression
7288 	    = finish_increment_expr (postfix_expression,
7289 				     POSTINCREMENT_EXPR);
7290 	  /* Increments may not appear in constant-expressions.  */
7291 	  if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7292 	    postfix_expression = error_mark_node;
7293 	  idk = CP_ID_KIND_NONE;
7294           is_member_access = false;
7295 	  break;
7296 
7297 	case CPP_MINUS_MINUS:
7298 	  /* postfix-expression -- */
7299 	  /* Consume the `--' token.  */
7300 	  cp_lexer_consume_token (parser->lexer);
7301 	  /* Generate a representation for the complete expression.  */
7302 	  postfix_expression
7303 	    = finish_increment_expr (postfix_expression,
7304 				     POSTDECREMENT_EXPR);
7305 	  /* Decrements may not appear in constant-expressions.  */
7306 	  if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7307 	    postfix_expression = error_mark_node;
7308 	  idk = CP_ID_KIND_NONE;
7309           is_member_access = false;
7310 	  break;
7311 
7312 	default:
7313 	  if (pidk_return != NULL)
7314 	    * pidk_return = idk;
7315           if (member_access_only_p)
7316             return is_member_access
7317               ? postfix_expression
7318               : cp_expr (error_mark_node);
7319           else
7320             return postfix_expression;
7321 	}
7322     }
7323 
7324   /* We should never get here.  */
7325   gcc_unreachable ();
7326   return error_mark_node;
7327 }
7328 
7329 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7330    by cp_parser_builtin_offsetof.  We're looking for
7331 
7332      postfix-expression [ expression ]
7333      postfix-expression [ braced-init-list ] (C++11)
7334 
7335    FOR_OFFSETOF is set if we're being called in that context, which
7336    changes how we deal with integer constant expressions.  */
7337 
7338 static tree
cp_parser_postfix_open_square_expression(cp_parser * parser,tree postfix_expression,bool for_offsetof,bool decltype_p)7339 cp_parser_postfix_open_square_expression (cp_parser *parser,
7340 					  tree postfix_expression,
7341 					  bool for_offsetof,
7342 					  bool decltype_p)
7343 {
7344   tree index = NULL_TREE;
7345   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7346   bool saved_greater_than_is_operator_p;
7347 
7348   /* Consume the `[' token.  */
7349   cp_lexer_consume_token (parser->lexer);
7350 
7351   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7352   parser->greater_than_is_operator_p = true;
7353 
7354   /* Parse the index expression.  */
7355   /* ??? For offsetof, there is a question of what to allow here.  If
7356      offsetof is not being used in an integral constant expression context,
7357      then we *could* get the right answer by computing the value at runtime.
7358      If we are in an integral constant expression context, then we might
7359      could accept any constant expression; hard to say without analysis.
7360      Rather than open the barn door too wide right away, allow only integer
7361      constant expressions here.  */
7362   if (for_offsetof)
7363     index = cp_parser_constant_expression (parser);
7364   else
7365     {
7366       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7367 	{
7368 	  bool expr_nonconst_p;
7369 	  cp_lexer_set_source_position (parser->lexer);
7370 	  maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7371 	  index = cp_parser_braced_list (parser, &expr_nonconst_p);
7372 	}
7373       else
7374 	index = cp_parser_expression (parser);
7375     }
7376 
7377   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7378 
7379   /* Look for the closing `]'.  */
7380   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7381 
7382   /* Build the ARRAY_REF.  */
7383   postfix_expression = grok_array_decl (loc, postfix_expression,
7384 					index, decltype_p);
7385 
7386   /* When not doing offsetof, array references are not permitted in
7387      constant-expressions.  */
7388   if (!for_offsetof
7389       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7390     postfix_expression = error_mark_node;
7391 
7392   return postfix_expression;
7393 }
7394 
7395 /* A subroutine of cp_parser_postfix_dot_deref_expression.  Handle dot
7396    dereference of incomplete type, returns true if error_mark_node should
7397    be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7398    and *DEPENDENT_P.  */
7399 
7400 bool
cp_parser_dot_deref_incomplete(tree * scope,cp_expr * postfix_expression,bool * dependent_p)7401 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7402 				bool *dependent_p)
7403 {
7404   /* In a template, be permissive by treating an object expression
7405      of incomplete type as dependent (after a pedwarn).  */
7406   diagnostic_t kind = (processing_template_decl
7407 		       && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7408 
7409   switch (TREE_CODE (*postfix_expression))
7410     {
7411     case CAST_EXPR:
7412     case REINTERPRET_CAST_EXPR:
7413     case CONST_CAST_EXPR:
7414     case STATIC_CAST_EXPR:
7415     case DYNAMIC_CAST_EXPR:
7416     case IMPLICIT_CONV_EXPR:
7417     case VIEW_CONVERT_EXPR:
7418     case NON_LVALUE_EXPR:
7419       kind = DK_ERROR;
7420       break;
7421     case OVERLOAD:
7422       /* Don't emit any diagnostic for OVERLOADs.  */
7423       kind = DK_IGNORED;
7424       break;
7425     default:
7426       /* Avoid clobbering e.g. DECLs.  */
7427       if (!EXPR_P (*postfix_expression))
7428 	kind = DK_ERROR;
7429       break;
7430     }
7431 
7432   if (kind == DK_IGNORED)
7433     return false;
7434 
7435   location_t exploc = location_of (*postfix_expression);
7436   cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7437   if (!MAYBE_CLASS_TYPE_P (*scope))
7438     return true;
7439   if (kind == DK_ERROR)
7440     *scope = *postfix_expression = error_mark_node;
7441   else if (processing_template_decl)
7442     {
7443       *dependent_p = true;
7444       *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7445     }
7446   return false;
7447 }
7448 
7449 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7450    by cp_parser_builtin_offsetof.  We're looking for
7451 
7452      postfix-expression . template [opt] id-expression
7453      postfix-expression . pseudo-destructor-name
7454      postfix-expression -> template [opt] id-expression
7455      postfix-expression -> pseudo-destructor-name
7456 
7457    FOR_OFFSETOF is set if we're being called in that context.  That sorta
7458    limits what of the above we'll actually accept, but nevermind.
7459    TOKEN_TYPE is the "." or "->" token, which will already have been
7460    removed from the stream.  */
7461 
7462 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)7463 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7464 					enum cpp_ttype token_type,
7465 					cp_expr postfix_expression,
7466 					bool for_offsetof, cp_id_kind *idk,
7467 					location_t location)
7468 {
7469   tree name;
7470   bool dependent_p;
7471   bool pseudo_destructor_p;
7472   tree scope = NULL_TREE;
7473   location_t start_loc = postfix_expression.get_start ();
7474 
7475   /* If this is a `->' operator, dereference the pointer.  */
7476   if (token_type == CPP_DEREF)
7477     postfix_expression = build_x_arrow (location, postfix_expression,
7478 					tf_warning_or_error);
7479   /* Check to see whether or not the expression is type-dependent and
7480      not the current instantiation.  */
7481   dependent_p = type_dependent_object_expression_p (postfix_expression);
7482   /* The identifier following the `->' or `.' is not qualified.  */
7483   parser->scope = NULL_TREE;
7484   parser->qualifying_scope = NULL_TREE;
7485   parser->object_scope = NULL_TREE;
7486   *idk = CP_ID_KIND_NONE;
7487 
7488   /* Enter the scope corresponding to the type of the object
7489      given by the POSTFIX_EXPRESSION.  */
7490   if (!dependent_p)
7491     {
7492       scope = TREE_TYPE (postfix_expression);
7493       /* According to the standard, no expression should ever have
7494 	 reference type.  Unfortunately, we do not currently match
7495 	 the standard in this respect in that our internal representation
7496 	 of an expression may have reference type even when the standard
7497 	 says it does not.  Therefore, we have to manually obtain the
7498 	 underlying type here.  */
7499       scope = non_reference (scope);
7500       /* The type of the POSTFIX_EXPRESSION must be complete.  */
7501       /* Unlike the object expression in other contexts, *this is not
7502 	 required to be of complete type for purposes of class member
7503 	 access (5.2.5) outside the member function body.  */
7504       if (postfix_expression != current_class_ref
7505 	  && scope != error_mark_node
7506 	  && !currently_open_class (scope))
7507 	{
7508 	  scope = complete_type (scope);
7509 	  if (!COMPLETE_TYPE_P (scope)
7510 	      && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7511 						 &dependent_p))
7512 	    return error_mark_node;
7513 	}
7514 
7515       if (!dependent_p)
7516 	{
7517 	  /* Let the name lookup machinery know that we are processing a
7518 	     class member access expression.  */
7519 	  parser->context->object_type = scope;
7520 	  /* If something went wrong, we want to be able to discern that case,
7521 	     as opposed to the case where there was no SCOPE due to the type
7522 	     of expression being dependent.  */
7523 	  if (!scope)
7524 	    scope = error_mark_node;
7525 	  /* If the SCOPE was erroneous, make the various semantic analysis
7526 	     functions exit quickly -- and without issuing additional error
7527 	     messages.  */
7528 	  if (scope == error_mark_node)
7529 	    postfix_expression = error_mark_node;
7530 	}
7531     }
7532 
7533   if (dependent_p)
7534     /* Tell cp_parser_lookup_name that there was an object, even though it's
7535        type-dependent.  */
7536     parser->context->object_type = unknown_type_node;
7537 
7538   /* Assume this expression is not a pseudo-destructor access.  */
7539   pseudo_destructor_p = false;
7540 
7541   /* If the SCOPE is a scalar type, then, if this is a valid program,
7542      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
7543      is type dependent, it can be pseudo-destructor-name or something else.
7544      Try to parse it as pseudo-destructor-name first.  */
7545   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7546     {
7547       tree s;
7548       tree type;
7549 
7550       cp_parser_parse_tentatively (parser);
7551       /* Parse the pseudo-destructor-name.  */
7552       s = NULL_TREE;
7553       cp_parser_pseudo_destructor_name (parser, postfix_expression,
7554 					&s, &type);
7555       if (dependent_p
7556 	  && (cp_parser_error_occurred (parser)
7557 	      || !SCALAR_TYPE_P (type)))
7558 	cp_parser_abort_tentative_parse (parser);
7559       else if (cp_parser_parse_definitely (parser))
7560 	{
7561 	  pseudo_destructor_p = true;
7562 	  postfix_expression
7563 	    = finish_pseudo_destructor_expr (postfix_expression,
7564 					     s, type, location);
7565 	}
7566     }
7567 
7568   if (!pseudo_destructor_p)
7569     {
7570       /* If the SCOPE is not a scalar type, we are looking at an
7571 	 ordinary class member access expression, rather than a
7572 	 pseudo-destructor-name.  */
7573       bool template_p;
7574       cp_token *token = cp_lexer_peek_token (parser->lexer);
7575       /* Parse the id-expression.  */
7576       name = (cp_parser_id_expression
7577 	      (parser,
7578 	       cp_parser_optional_template_keyword (parser),
7579 	       /*check_dependency_p=*/true,
7580 	       &template_p,
7581 	       /*declarator_p=*/false,
7582 	       /*optional_p=*/false));
7583       /* In general, build a SCOPE_REF if the member name is qualified.
7584 	 However, if the name was not dependent and has already been
7585 	 resolved; there is no need to build the SCOPE_REF.  For example;
7586 
7587 	     struct X { void f(); };
7588 	     template <typename T> void f(T* t) { t->X::f(); }
7589 
7590 	 Even though "t" is dependent, "X::f" is not and has been resolved
7591 	 to a BASELINK; there is no need to include scope information.  */
7592 
7593       /* But we do need to remember that there was an explicit scope for
7594 	 virtual function calls.  */
7595       if (parser->scope)
7596 	*idk = CP_ID_KIND_QUALIFIED;
7597 
7598       /* If the name is a template-id that names a type, we will get a
7599 	 TYPE_DECL here.  That is invalid code.  */
7600       if (TREE_CODE (name) == TYPE_DECL)
7601 	{
7602 	  error_at (token->location, "invalid use of %qD", name);
7603 	  postfix_expression = error_mark_node;
7604 	}
7605       else
7606 	{
7607 	  if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7608 	    {
7609 	      if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7610 		{
7611 		  error_at (token->location, "%<%D::%D%> is not a class member",
7612 			    parser->scope, name);
7613 		  postfix_expression = error_mark_node;
7614 		}
7615 	      else
7616 		name = build_qualified_name (/*type=*/NULL_TREE,
7617 					     parser->scope,
7618 					     name,
7619 					     template_p);
7620 	      parser->scope = NULL_TREE;
7621 	      parser->qualifying_scope = NULL_TREE;
7622 	      parser->object_scope = NULL_TREE;
7623 	    }
7624 	  if (parser->scope && name && BASELINK_P (name))
7625 	    adjust_result_of_qualified_name_lookup
7626 	      (name, parser->scope, scope);
7627 	  postfix_expression
7628 	    = finish_class_member_access_expr (postfix_expression, name,
7629 					       template_p,
7630 					       tf_warning_or_error);
7631 	  /* Build a location e.g.:
7632 	       ptr->access_expr
7633 	       ~~~^~~~~~~~~~~~~
7634 	     where the caret is at the deref token, ranging from
7635 	     the start of postfix_expression to the end of the access expr.  */
7636 	  location_t end_loc
7637 	    = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7638 	  location_t combined_loc
7639 	    = make_location (input_location, start_loc, end_loc);
7640 	  protected_set_expr_location (postfix_expression, combined_loc);
7641 	}
7642     }
7643 
7644   /* We no longer need to look up names in the scope of the object on
7645      the left-hand side of the `.' or `->' operator.  */
7646   parser->context->object_type = NULL_TREE;
7647 
7648   /* Outside of offsetof, these operators may not appear in
7649      constant-expressions.  */
7650   if (!for_offsetof
7651       && (cp_parser_non_integral_constant_expression
7652 	  (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7653     postfix_expression = error_mark_node;
7654 
7655   return postfix_expression;
7656 }
7657 
7658 /* Parse a parenthesized expression-list.
7659 
7660    expression-list:
7661      assignment-expression
7662      expression-list, assignment-expression
7663 
7664    attribute-list:
7665      expression-list
7666      identifier
7667      identifier, expression-list
7668 
7669    CAST_P is true if this expression is the target of a cast.
7670 
7671    ALLOW_EXPANSION_P is true if this expression allows expansion of an
7672    argument pack.
7673 
7674    WRAP_LOCATIONS_P is true if expressions within this list for which
7675    CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7676    their source locations.
7677 
7678    Returns a vector of trees.  Each element is a representation of an
7679    assignment-expression.  NULL is returned if the ( and or ) are
7680    missing.  An empty, but allocated, vector is returned on no
7681    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
7682    if we are parsing an attribute list for an attribute that wants a
7683    plain identifier argument, normal_attr for an attribute that wants
7684    an expression, or non_attr if we aren't parsing an attribute list.  If
7685    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7686    not all of the expressions in the list were constant.
7687    If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7688    will be written to with the location of the closing parenthesis.  If
7689    an error occurs, it may or may not be written to.  */
7690 
7691 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)7692 cp_parser_parenthesized_expression_list (cp_parser* parser,
7693 					 int is_attribute_list,
7694 					 bool cast_p,
7695                                          bool allow_expansion_p,
7696 					 bool *non_constant_p,
7697 					 location_t *close_paren_loc,
7698 					 bool wrap_locations_p)
7699 {
7700   vec<tree, va_gc> *expression_list;
7701   bool fold_expr_p = is_attribute_list != non_attr;
7702   tree identifier = NULL_TREE;
7703   bool saved_greater_than_is_operator_p;
7704 
7705   /* Assume all the expressions will be constant.  */
7706   if (non_constant_p)
7707     *non_constant_p = false;
7708 
7709   matching_parens parens;
7710   if (!parens.require_open (parser))
7711     return NULL;
7712 
7713   expression_list = make_tree_vector ();
7714 
7715   /* Within a parenthesized expression, a `>' token is always
7716      the greater-than operator.  */
7717   saved_greater_than_is_operator_p
7718     = parser->greater_than_is_operator_p;
7719   parser->greater_than_is_operator_p = true;
7720 
7721   cp_expr expr (NULL_TREE);
7722 
7723   /* Consume expressions until there are no more.  */
7724   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7725     while (true)
7726       {
7727 	/* At the beginning of attribute lists, check to see if the
7728 	   next token is an identifier.  */
7729 	if (is_attribute_list == id_attr
7730 	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7731 	  {
7732 	    cp_token *token;
7733 
7734 	    /* Consume the identifier.  */
7735 	    token = cp_lexer_consume_token (parser->lexer);
7736 	    /* Save the identifier.  */
7737 	    identifier = token->u.value;
7738 	  }
7739 	else
7740 	  {
7741 	    bool expr_non_constant_p;
7742 
7743 	    /* Parse the next assignment-expression.  */
7744 	    if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7745 	      {
7746 		/* A braced-init-list.  */
7747 		cp_lexer_set_source_position (parser->lexer);
7748 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7749 		expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7750 		if (non_constant_p && expr_non_constant_p)
7751 		  *non_constant_p = true;
7752 	      }
7753 	    else if (non_constant_p)
7754 	      {
7755 		expr = (cp_parser_constant_expression
7756 			(parser, /*allow_non_constant_p=*/true,
7757 			 &expr_non_constant_p));
7758 		if (expr_non_constant_p)
7759 		  *non_constant_p = true;
7760 	      }
7761 	    else
7762 	      expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7763 						      cast_p);
7764 
7765 	    if (fold_expr_p)
7766 	      expr = instantiate_non_dependent_expr (expr);
7767 
7768             /* If we have an ellipsis, then this is an expression
7769 	       expansion.  */
7770             if (allow_expansion_p
7771                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7772               {
7773                 /* Consume the `...'.  */
7774                 cp_lexer_consume_token (parser->lexer);
7775 
7776                 /* Build the argument pack.  */
7777                 expr = make_pack_expansion (expr);
7778               }
7779 
7780 	    if (wrap_locations_p)
7781 	      expr.maybe_add_location_wrapper ();
7782 
7783 	     /* Add it to the list.  We add error_mark_node
7784 		expressions to the list, so that we can still tell if
7785 		the correct form for a parenthesized expression-list
7786 		is found. That gives better errors.  */
7787 	    vec_safe_push (expression_list, expr.get_value ());
7788 
7789 	    if (expr == error_mark_node)
7790 	      goto skip_comma;
7791 	  }
7792 
7793 	/* After the first item, attribute lists look the same as
7794 	   expression lists.  */
7795 	is_attribute_list = non_attr;
7796 
7797       get_comma:;
7798 	/* If the next token isn't a `,', then we are done.  */
7799 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7800 	  break;
7801 
7802 	/* Otherwise, consume the `,' and keep going.  */
7803 	cp_lexer_consume_token (parser->lexer);
7804       }
7805 
7806   if (close_paren_loc)
7807     *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7808 
7809   if (!parens.require_close (parser))
7810     {
7811       int ending;
7812 
7813     skip_comma:;
7814       /* We try and resync to an unnested comma, as that will give the
7815 	 user better diagnostics.  */
7816       ending = cp_parser_skip_to_closing_parenthesis (parser,
7817 						      /*recovering=*/true,
7818 						      /*or_comma=*/true,
7819 						      /*consume_paren=*/true);
7820       if (ending < 0)
7821 	goto get_comma;
7822       if (!ending)
7823 	{
7824 	  parser->greater_than_is_operator_p
7825 	    = saved_greater_than_is_operator_p;
7826 	  return NULL;
7827 	}
7828     }
7829 
7830   parser->greater_than_is_operator_p
7831     = saved_greater_than_is_operator_p;
7832 
7833   if (identifier)
7834     vec_safe_insert (expression_list, 0, identifier);
7835 
7836   return expression_list;
7837 }
7838 
7839 /* Parse a pseudo-destructor-name.
7840 
7841    pseudo-destructor-name:
7842      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7843      :: [opt] nested-name-specifier template template-id :: ~ type-name
7844      :: [opt] nested-name-specifier [opt] ~ type-name
7845 
7846    If either of the first two productions is used, sets *SCOPE to the
7847    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7848    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7849    or ERROR_MARK_NODE if the parse fails.  */
7850 
7851 static void
cp_parser_pseudo_destructor_name(cp_parser * parser,tree object,tree * scope,tree * type)7852 cp_parser_pseudo_destructor_name (cp_parser* parser,
7853 				  tree object,
7854 				  tree* scope,
7855 				  tree* type)
7856 {
7857   bool nested_name_specifier_p;
7858 
7859   /* Handle ~auto.  */
7860   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7861       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7862       && !type_dependent_expression_p (object))
7863     {
7864       if (cxx_dialect < cxx14)
7865 	pedwarn (input_location, 0,
7866 		 "%<~auto%> only available with "
7867 		 "-std=c++14 or -std=gnu++14");
7868       cp_lexer_consume_token (parser->lexer);
7869       cp_lexer_consume_token (parser->lexer);
7870       *scope = NULL_TREE;
7871       *type = TREE_TYPE (object);
7872       return;
7873     }
7874 
7875   /* Assume that things will not work out.  */
7876   *type = error_mark_node;
7877 
7878   /* Look for the optional `::' operator.  */
7879   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7880   /* Look for the optional nested-name-specifier.  */
7881   nested_name_specifier_p
7882     = (cp_parser_nested_name_specifier_opt (parser,
7883 					    /*typename_keyword_p=*/false,
7884 					    /*check_dependency_p=*/true,
7885 					    /*type_p=*/false,
7886 					    /*is_declaration=*/false)
7887        != NULL_TREE);
7888   /* Now, if we saw a nested-name-specifier, we might be doing the
7889      second production.  */
7890   if (nested_name_specifier_p
7891       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7892     {
7893       /* Consume the `template' keyword.  */
7894       cp_lexer_consume_token (parser->lexer);
7895       /* Parse the template-id.  */
7896       cp_parser_template_id (parser,
7897 			     /*template_keyword_p=*/true,
7898 			     /*check_dependency_p=*/false,
7899 			     class_type,
7900 			     /*is_declaration=*/true);
7901       /* Look for the `::' token.  */
7902       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7903     }
7904   /* If the next token is not a `~', then there might be some
7905      additional qualification.  */
7906   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7907     {
7908       /* At this point, we're looking for "type-name :: ~".  The type-name
7909 	 must not be a class-name, since this is a pseudo-destructor.  So,
7910 	 it must be either an enum-name, or a typedef-name -- both of which
7911 	 are just identifiers.  So, we peek ahead to check that the "::"
7912 	 and "~" tokens are present; if they are not, then we can avoid
7913 	 calling type_name.  */
7914       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7915 	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7916 	  || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7917 	{
7918 	  cp_parser_error (parser, "non-scalar type");
7919 	  return;
7920 	}
7921 
7922       /* Look for the type-name.  */
7923       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7924       if (*scope == error_mark_node)
7925 	return;
7926 
7927       /* Look for the `::' token.  */
7928       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7929     }
7930   else
7931     *scope = NULL_TREE;
7932 
7933   /* Look for the `~'.  */
7934   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7935 
7936   /* Once we see the ~, this has to be a pseudo-destructor.  */
7937   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7938     cp_parser_commit_to_topmost_tentative_parse (parser);
7939 
7940   /* Look for the type-name again.  We are not responsible for
7941      checking that it matches the first type-name.  */
7942   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7943 }
7944 
7945 /* Parse a unary-expression.
7946 
7947    unary-expression:
7948      postfix-expression
7949      ++ cast-expression
7950      -- cast-expression
7951      unary-operator cast-expression
7952      sizeof unary-expression
7953      sizeof ( type-id )
7954      alignof ( type-id )  [C++0x]
7955      new-expression
7956      delete-expression
7957 
7958    GNU Extensions:
7959 
7960    unary-expression:
7961      __extension__ cast-expression
7962      __alignof__ unary-expression
7963      __alignof__ ( type-id )
7964      alignof unary-expression  [C++0x]
7965      __real__ cast-expression
7966      __imag__ cast-expression
7967      && identifier
7968      sizeof ( type-id ) { initializer-list , [opt] }
7969      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7970      __alignof__ ( type-id ) { initializer-list , [opt] }
7971 
7972    ADDRESS_P is true iff the unary-expression is appearing as the
7973    operand of the `&' operator.   CAST_P is true if this expression is
7974    the target of a cast.
7975 
7976    Returns a representation of the expression.  */
7977 
7978 static cp_expr
cp_parser_unary_expression(cp_parser * parser,cp_id_kind * pidk,bool address_p,bool cast_p,bool decltype_p)7979 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7980 			    bool address_p, bool cast_p, bool decltype_p)
7981 {
7982   cp_token *token;
7983   enum tree_code unary_operator;
7984 
7985   /* Peek at the next token.  */
7986   token = cp_lexer_peek_token (parser->lexer);
7987   /* Some keywords give away the kind of expression.  */
7988   if (token->type == CPP_KEYWORD)
7989     {
7990       enum rid keyword = token->keyword;
7991 
7992       switch (keyword)
7993 	{
7994 	case RID_ALIGNOF:
7995 	case RID_SIZEOF:
7996 	  {
7997 	    tree operand, ret;
7998 	    enum tree_code op;
7999 	    location_t start_loc = token->location;
8000 
8001 	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8002 	    bool std_alignof = id_equal (token->u.value, "alignof");
8003 
8004 	    /* Consume the token.  */
8005 	    cp_lexer_consume_token (parser->lexer);
8006 	    /* Parse the operand.  */
8007 	    operand = cp_parser_sizeof_operand (parser, keyword);
8008 
8009 	    if (TYPE_P (operand))
8010 	      ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8011 						true);
8012 	    else
8013 	      {
8014 		/* ISO C++ defines alignof only with types, not with
8015 		   expressions. So pedwarn if alignof is used with a non-
8016 		   type expression. However, __alignof__ is ok.  */
8017 		if (std_alignof)
8018 		  pedwarn (token->location, OPT_Wpedantic,
8019 			   "ISO C++ does not allow %<alignof%> "
8020 			   "with a non-type");
8021 
8022 		ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8023 	      }
8024 	    /* For SIZEOF_EXPR, just issue diagnostics, but keep
8025 	       SIZEOF_EXPR with the original operand.  */
8026 	    if (op == SIZEOF_EXPR && ret != error_mark_node)
8027 	      {
8028 		if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8029 		  {
8030 		    if (!processing_template_decl && TYPE_P (operand))
8031 		      {
8032 			ret = build_min (SIZEOF_EXPR, size_type_node,
8033 					 build1 (NOP_EXPR, operand,
8034 						 error_mark_node));
8035 			SIZEOF_EXPR_TYPE_P (ret) = 1;
8036 		      }
8037 		    else
8038 		      ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8039 		    TREE_SIDE_EFFECTS (ret) = 0;
8040 		    TREE_READONLY (ret) = 1;
8041 		  }
8042 	      }
8043 
8044 	    /* Construct a location e.g. :
8045 	       alignof (expr)
8046 	       ^~~~~~~~~~~~~~
8047 	       with start == caret at the start of the "alignof"/"sizeof"
8048 	       token, with the endpoint at the final closing paren.  */
8049 	    location_t finish_loc
8050 	      = cp_lexer_previous_token (parser->lexer)->location;
8051 	    location_t compound_loc
8052 	      = make_location (start_loc, start_loc, finish_loc);
8053 
8054 	    cp_expr ret_expr (ret);
8055 	    ret_expr.set_location (compound_loc);
8056 	    ret_expr = ret_expr.maybe_add_location_wrapper ();
8057 	    return ret_expr;
8058 	  }
8059 
8060 	case RID_NEW:
8061 	  return cp_parser_new_expression (parser);
8062 
8063 	case RID_DELETE:
8064 	  return cp_parser_delete_expression (parser);
8065 
8066 	case RID_EXTENSION:
8067 	  {
8068 	    /* The saved value of the PEDANTIC flag.  */
8069 	    int saved_pedantic;
8070 	    tree expr;
8071 
8072 	    /* Save away the PEDANTIC flag.  */
8073 	    cp_parser_extension_opt (parser, &saved_pedantic);
8074 	    /* Parse the cast-expression.  */
8075 	    expr = cp_parser_simple_cast_expression (parser);
8076 	    /* Restore the PEDANTIC flag.  */
8077 	    pedantic = saved_pedantic;
8078 
8079 	    return expr;
8080 	  }
8081 
8082 	case RID_REALPART:
8083 	case RID_IMAGPART:
8084 	  {
8085 	    tree expression;
8086 
8087 	    /* Consume the `__real__' or `__imag__' token.  */
8088 	    cp_lexer_consume_token (parser->lexer);
8089 	    /* Parse the cast-expression.  */
8090 	    expression = cp_parser_simple_cast_expression (parser);
8091 	    /* Create the complete representation.  */
8092 	    return build_x_unary_op (token->location,
8093 				     (keyword == RID_REALPART
8094 				      ? REALPART_EXPR : IMAGPART_EXPR),
8095 				     expression,
8096                                      tf_warning_or_error);
8097 	  }
8098 	  break;
8099 
8100 	case RID_TRANSACTION_ATOMIC:
8101 	case RID_TRANSACTION_RELAXED:
8102 	  return cp_parser_transaction_expression (parser, keyword);
8103 
8104 	case RID_NOEXCEPT:
8105 	  {
8106 	    tree expr;
8107 	    const char *saved_message;
8108 	    bool saved_integral_constant_expression_p;
8109 	    bool saved_non_integral_constant_expression_p;
8110 	    bool saved_greater_than_is_operator_p;
8111 
8112 	    location_t start_loc = token->location;
8113 
8114 	    cp_lexer_consume_token (parser->lexer);
8115 	    matching_parens parens;
8116 	    parens.require_open (parser);
8117 
8118 	    saved_message = parser->type_definition_forbidden_message;
8119 	    parser->type_definition_forbidden_message
8120 	      = G_("types may not be defined in %<noexcept%> expressions");
8121 
8122 	    saved_integral_constant_expression_p
8123 	      = parser->integral_constant_expression_p;
8124 	    saved_non_integral_constant_expression_p
8125 	      = parser->non_integral_constant_expression_p;
8126 	    parser->integral_constant_expression_p = false;
8127 
8128 	    saved_greater_than_is_operator_p
8129 	      = parser->greater_than_is_operator_p;
8130 	    parser->greater_than_is_operator_p = true;
8131 
8132 	    ++cp_unevaluated_operand;
8133 	    ++c_inhibit_evaluation_warnings;
8134 	    ++cp_noexcept_operand;
8135 	    expr = cp_parser_expression (parser);
8136 	    --cp_noexcept_operand;
8137 	    --c_inhibit_evaluation_warnings;
8138 	    --cp_unevaluated_operand;
8139 
8140 	    parser->greater_than_is_operator_p
8141 	      = saved_greater_than_is_operator_p;
8142 
8143 	    parser->integral_constant_expression_p
8144 	      = saved_integral_constant_expression_p;
8145 	    parser->non_integral_constant_expression_p
8146 	      = saved_non_integral_constant_expression_p;
8147 
8148 	    parser->type_definition_forbidden_message = saved_message;
8149 
8150 	    location_t finish_loc
8151 	      = cp_lexer_peek_token (parser->lexer)->location;
8152 	    parens.require_close (parser);
8153 
8154 	    /* Construct a location of the form:
8155 	       noexcept (expr)
8156 	       ^~~~~~~~~~~~~~~
8157 	       with start == caret, finishing at the close-paren.  */
8158 	    location_t noexcept_loc
8159 	      = make_location (start_loc, start_loc, finish_loc);
8160 
8161 	    return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8162 			    noexcept_loc);
8163 	  }
8164 
8165 	default:
8166 	  break;
8167 	}
8168     }
8169 
8170   /* Look for the `:: new' and `:: delete', which also signal the
8171      beginning of a new-expression, or delete-expression,
8172      respectively.  If the next token is `::', then it might be one of
8173      these.  */
8174   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8175     {
8176       enum rid keyword;
8177 
8178       /* See if the token after the `::' is one of the keywords in
8179 	 which we're interested.  */
8180       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8181       /* If it's `new', we have a new-expression.  */
8182       if (keyword == RID_NEW)
8183 	return cp_parser_new_expression (parser);
8184       /* Similarly, for `delete'.  */
8185       else if (keyword == RID_DELETE)
8186 	return cp_parser_delete_expression (parser);
8187     }
8188 
8189   /* Look for a unary operator.  */
8190   unary_operator = cp_parser_unary_operator (token);
8191   /* The `++' and `--' operators can be handled similarly, even though
8192      they are not technically unary-operators in the grammar.  */
8193   if (unary_operator == ERROR_MARK)
8194     {
8195       if (token->type == CPP_PLUS_PLUS)
8196 	unary_operator = PREINCREMENT_EXPR;
8197       else if (token->type == CPP_MINUS_MINUS)
8198 	unary_operator = PREDECREMENT_EXPR;
8199       /* Handle the GNU address-of-label extension.  */
8200       else if (cp_parser_allow_gnu_extensions_p (parser)
8201 	       && token->type == CPP_AND_AND)
8202 	{
8203 	  tree identifier;
8204 	  tree expression;
8205 	  location_t start_loc = token->location;
8206 
8207 	  /* Consume the '&&' token.  */
8208 	  cp_lexer_consume_token (parser->lexer);
8209 	  /* Look for the identifier.  */
8210 	  location_t finish_loc
8211 	    = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8212 	  identifier = cp_parser_identifier (parser);
8213 	  /* Construct a location of the form:
8214 	       &&label
8215 	       ^~~~~~~
8216 	     with caret==start at the "&&", finish at the end of the label.  */
8217 	  location_t combined_loc
8218 	    = make_location (start_loc, start_loc, finish_loc);
8219 	  /* Create an expression representing the address.  */
8220 	  expression = finish_label_address_expr (identifier, combined_loc);
8221 	  if (cp_parser_non_integral_constant_expression (parser,
8222 							  NIC_ADDR_LABEL))
8223 	    expression = error_mark_node;
8224 	  return expression;
8225 	}
8226     }
8227   if (unary_operator != ERROR_MARK)
8228     {
8229       cp_expr cast_expression;
8230       cp_expr expression = error_mark_node;
8231       non_integral_constant non_constant_p = NIC_NONE;
8232       location_t loc = token->location;
8233       tsubst_flags_t complain = complain_flags (decltype_p);
8234 
8235       /* Consume the operator token.  */
8236       token = cp_lexer_consume_token (parser->lexer);
8237       enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8238 
8239       /* Parse the cast-expression.  */
8240       cast_expression
8241 	= cp_parser_cast_expression (parser,
8242 				     unary_operator == ADDR_EXPR,
8243 				     /*cast_p=*/false,
8244 				     /*decltype*/false,
8245 				     pidk);
8246 
8247       /* Make a location:
8248 	    OP_TOKEN  CAST_EXPRESSION
8249 	    ^~~~~~~~~~~~~~~~~~~~~~~~~
8250 	 with start==caret at the operator token, and
8251 	 extending to the end of the cast_expression.  */
8252       loc = make_location (loc, loc, cast_expression.get_finish ());
8253 
8254       /* Now, build an appropriate representation.  */
8255       switch (unary_operator)
8256 	{
8257 	case INDIRECT_REF:
8258 	  non_constant_p = NIC_STAR;
8259 	  expression = build_x_indirect_ref (loc, cast_expression,
8260 					     RO_UNARY_STAR,
8261                                              complain);
8262           /* TODO: build_x_indirect_ref does not always honor the
8263              location, so ensure it is set.  */
8264           expression.set_location (loc);
8265 	  break;
8266 
8267 	case ADDR_EXPR:
8268 	   non_constant_p = NIC_ADDR;
8269 	  /* Fall through.  */
8270 	case BIT_NOT_EXPR:
8271 	  expression = build_x_unary_op (loc, unary_operator,
8272 					 cast_expression,
8273                                          complain);
8274           /* TODO: build_x_unary_op does not always honor the location,
8275              so ensure it is set.  */
8276           expression.set_location (loc);
8277 	  break;
8278 
8279 	case PREINCREMENT_EXPR:
8280 	case PREDECREMENT_EXPR:
8281 	  non_constant_p = unary_operator == PREINCREMENT_EXPR
8282 			   ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8283 	  /* Fall through.  */
8284 	case NEGATE_EXPR:
8285 	  /* Immediately fold negation of a constant, unless the constant is 0
8286 	     (since -0 == 0) or it would overflow.  */
8287 	  if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8288 	      && CONSTANT_CLASS_P (cast_expression)
8289 	      && !integer_zerop (cast_expression)
8290 	      && !TREE_OVERFLOW (cast_expression))
8291 	    {
8292 	      tree folded = fold_build1 (unary_operator,
8293 					 TREE_TYPE (cast_expression),
8294 					 cast_expression);
8295 	      if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8296 		{
8297 		  expression = cp_expr (folded, loc);
8298 		  break;
8299 		}
8300 	    }
8301 	  /* Fall through.  */
8302 	case UNARY_PLUS_EXPR:
8303 	case TRUTH_NOT_EXPR:
8304 	  expression = finish_unary_op_expr (loc, unary_operator,
8305 					     cast_expression, complain);
8306 	  break;
8307 
8308 	default:
8309 	  gcc_unreachable ();
8310 	}
8311 
8312       if (non_constant_p != NIC_NONE
8313 	  && cp_parser_non_integral_constant_expression (parser,
8314 							 non_constant_p))
8315 	expression = error_mark_node;
8316 
8317       return expression;
8318     }
8319 
8320   return cp_parser_postfix_expression (parser, address_p, cast_p,
8321                                        /*member_access_only_p=*/false,
8322 				       decltype_p,
8323 				       pidk);
8324 }
8325 
8326 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
8327    unary-operator, the corresponding tree code is returned.  */
8328 
8329 static enum tree_code
cp_parser_unary_operator(cp_token * token)8330 cp_parser_unary_operator (cp_token* token)
8331 {
8332   switch (token->type)
8333     {
8334     case CPP_MULT:
8335       return INDIRECT_REF;
8336 
8337     case CPP_AND:
8338       return ADDR_EXPR;
8339 
8340     case CPP_PLUS:
8341       return UNARY_PLUS_EXPR;
8342 
8343     case CPP_MINUS:
8344       return NEGATE_EXPR;
8345 
8346     case CPP_NOT:
8347       return TRUTH_NOT_EXPR;
8348 
8349     case CPP_COMPL:
8350       return BIT_NOT_EXPR;
8351 
8352     default:
8353       return ERROR_MARK;
8354     }
8355 }
8356 
8357 /* Parse a new-expression.
8358 
8359    new-expression:
8360      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8361      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8362 
8363    Returns a representation of the expression.  */
8364 
8365 static tree
cp_parser_new_expression(cp_parser * parser)8366 cp_parser_new_expression (cp_parser* parser)
8367 {
8368   bool global_scope_p;
8369   vec<tree, va_gc> *placement;
8370   tree type;
8371   vec<tree, va_gc> *initializer;
8372   tree nelts = NULL_TREE;
8373   tree ret;
8374 
8375   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8376 
8377   /* Look for the optional `::' operator.  */
8378   global_scope_p
8379     = (cp_parser_global_scope_opt (parser,
8380 				   /*current_scope_valid_p=*/false)
8381        != NULL_TREE);
8382   /* Look for the `new' operator.  */
8383   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8384   /* There's no easy way to tell a new-placement from the
8385      `( type-id )' construct.  */
8386   cp_parser_parse_tentatively (parser);
8387   /* Look for a new-placement.  */
8388   placement = cp_parser_new_placement (parser);
8389   /* If that didn't work out, there's no new-placement.  */
8390   if (!cp_parser_parse_definitely (parser))
8391     {
8392       if (placement != NULL)
8393 	release_tree_vector (placement);
8394       placement = NULL;
8395     }
8396 
8397   /* If the next token is a `(', then we have a parenthesized
8398      type-id.  */
8399   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8400     {
8401       cp_token *token;
8402       const char *saved_message = parser->type_definition_forbidden_message;
8403 
8404       /* Consume the `('.  */
8405       matching_parens parens;
8406       parens.consume_open (parser);
8407 
8408       /* Parse the type-id.  */
8409       parser->type_definition_forbidden_message
8410 	= G_("types may not be defined in a new-expression");
8411       {
8412 	type_id_in_expr_sentinel s (parser);
8413 	type = cp_parser_type_id (parser);
8414       }
8415       parser->type_definition_forbidden_message = saved_message;
8416 
8417       /* Look for the closing `)'.  */
8418       parens.require_close (parser);
8419       token = cp_lexer_peek_token (parser->lexer);
8420       /* There should not be a direct-new-declarator in this production,
8421 	 but GCC used to allowed this, so we check and emit a sensible error
8422 	 message for this case.  */
8423       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8424 	{
8425 	  error_at (token->location,
8426 		    "array bound forbidden after parenthesized type-id");
8427 	  inform (token->location,
8428 		  "try removing the parentheses around the type-id");
8429 	  cp_parser_direct_new_declarator (parser);
8430 	}
8431     }
8432   /* Otherwise, there must be a new-type-id.  */
8433   else
8434     type = cp_parser_new_type_id (parser, &nelts);
8435 
8436   /* If the next token is a `(' or '{', then we have a new-initializer.  */
8437   cp_token *token = cp_lexer_peek_token (parser->lexer);
8438   if (token->type == CPP_OPEN_PAREN
8439       || token->type == CPP_OPEN_BRACE)
8440     initializer = cp_parser_new_initializer (parser);
8441   else
8442     initializer = NULL;
8443 
8444   /* A new-expression may not appear in an integral constant
8445      expression.  */
8446   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8447     ret = error_mark_node;
8448   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8449      of a new-type-id or type-id of a new-expression, the new-expression shall
8450      contain a new-initializer of the form ( assignment-expression )".
8451      Additionally, consistently with the spirit of DR 1467, we want to accept
8452      'new auto { 2 }' too.  */
8453   else if ((ret = type_uses_auto (type))
8454 	   && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8455 	   && (vec_safe_length (initializer) != 1
8456 	       || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8457 		   && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8458     {
8459       error_at (token->location,
8460 		"initialization of new-expression for type %<auto%> "
8461 		"requires exactly one element");
8462       ret = error_mark_node;
8463     }
8464   else
8465     {
8466       /* Construct a location e.g.:
8467            ptr = new int[100]
8468                  ^~~~~~~~~~~~
8469          with caret == start at the start of the "new" token, and the end
8470          at the end of the final token we consumed.  */
8471       cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8472       location_t end_loc = get_finish (end_tok->location);
8473       location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8474 
8475       /* Create a representation of the new-expression.  */
8476       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8477 		       tf_warning_or_error);
8478       protected_set_expr_location (ret, combined_loc);
8479     }
8480 
8481   if (placement != NULL)
8482     release_tree_vector (placement);
8483   if (initializer != NULL)
8484     release_tree_vector (initializer);
8485 
8486   return ret;
8487 }
8488 
8489 /* Parse a new-placement.
8490 
8491    new-placement:
8492      ( expression-list )
8493 
8494    Returns the same representation as for an expression-list.  */
8495 
8496 static vec<tree, va_gc> *
cp_parser_new_placement(cp_parser * parser)8497 cp_parser_new_placement (cp_parser* parser)
8498 {
8499   vec<tree, va_gc> *expression_list;
8500 
8501   /* Parse the expression-list.  */
8502   expression_list = (cp_parser_parenthesized_expression_list
8503 		     (parser, non_attr, /*cast_p=*/false,
8504 		      /*allow_expansion_p=*/true,
8505 		      /*non_constant_p=*/NULL));
8506 
8507   if (expression_list && expression_list->is_empty ())
8508     error ("expected expression-list or type-id");
8509 
8510   return expression_list;
8511 }
8512 
8513 /* Parse a new-type-id.
8514 
8515    new-type-id:
8516      type-specifier-seq new-declarator [opt]
8517 
8518    Returns the TYPE allocated.  If the new-type-id indicates an array
8519    type, *NELTS is set to the number of elements in the last array
8520    bound; the TYPE will not include the last array bound.  */
8521 
8522 static tree
cp_parser_new_type_id(cp_parser * parser,tree * nelts)8523 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8524 {
8525   cp_decl_specifier_seq type_specifier_seq;
8526   cp_declarator *new_declarator;
8527   cp_declarator *declarator;
8528   cp_declarator *outer_declarator;
8529   const char *saved_message;
8530 
8531   /* The type-specifier sequence must not contain type definitions.
8532      (It cannot contain declarations of new types either, but if they
8533      are not definitions we will catch that because they are not
8534      complete.)  */
8535   saved_message = parser->type_definition_forbidden_message;
8536   parser->type_definition_forbidden_message
8537     = G_("types may not be defined in a new-type-id");
8538   /* Parse the type-specifier-seq.  */
8539   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8540 				/*is_trailing_return=*/false,
8541 				&type_specifier_seq);
8542   /* Restore the old message.  */
8543   parser->type_definition_forbidden_message = saved_message;
8544 
8545   if (type_specifier_seq.type == error_mark_node)
8546     return error_mark_node;
8547 
8548   /* Parse the new-declarator.  */
8549   new_declarator = cp_parser_new_declarator_opt (parser);
8550 
8551   /* Determine the number of elements in the last array dimension, if
8552      any.  */
8553   *nelts = NULL_TREE;
8554   /* Skip down to the last array dimension.  */
8555   declarator = new_declarator;
8556   outer_declarator = NULL;
8557   while (declarator && (declarator->kind == cdk_pointer
8558 			|| declarator->kind == cdk_ptrmem))
8559     {
8560       outer_declarator = declarator;
8561       declarator = declarator->declarator;
8562     }
8563   while (declarator
8564 	 && declarator->kind == cdk_array
8565 	 && declarator->declarator
8566 	 && declarator->declarator->kind == cdk_array)
8567     {
8568       outer_declarator = declarator;
8569       declarator = declarator->declarator;
8570     }
8571 
8572   if (declarator && declarator->kind == cdk_array)
8573     {
8574       *nelts = declarator->u.array.bounds;
8575       if (*nelts == error_mark_node)
8576 	*nelts = integer_one_node;
8577 
8578       if (outer_declarator)
8579 	outer_declarator->declarator = declarator->declarator;
8580       else
8581 	new_declarator = NULL;
8582     }
8583 
8584   return groktypename (&type_specifier_seq, new_declarator, false);
8585 }
8586 
8587 /* Parse an (optional) new-declarator.
8588 
8589    new-declarator:
8590      ptr-operator new-declarator [opt]
8591      direct-new-declarator
8592 
8593    Returns the declarator.  */
8594 
8595 static cp_declarator *
cp_parser_new_declarator_opt(cp_parser * parser)8596 cp_parser_new_declarator_opt (cp_parser* parser)
8597 {
8598   enum tree_code code;
8599   tree type, std_attributes = NULL_TREE;
8600   cp_cv_quals cv_quals;
8601 
8602   /* We don't know if there's a ptr-operator next, or not.  */
8603   cp_parser_parse_tentatively (parser);
8604   /* Look for a ptr-operator.  */
8605   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8606   /* If that worked, look for more new-declarators.  */
8607   if (cp_parser_parse_definitely (parser))
8608     {
8609       cp_declarator *declarator;
8610 
8611       /* Parse another optional declarator.  */
8612       declarator = cp_parser_new_declarator_opt (parser);
8613 
8614       declarator = cp_parser_make_indirect_declarator
8615 	(code, type, cv_quals, declarator, std_attributes);
8616 
8617       return declarator;
8618     }
8619 
8620   /* If the next token is a `[', there is a direct-new-declarator.  */
8621   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8622     return cp_parser_direct_new_declarator (parser);
8623 
8624   return NULL;
8625 }
8626 
8627 /* Parse a direct-new-declarator.
8628 
8629    direct-new-declarator:
8630      [ expression ]
8631      direct-new-declarator [constant-expression]
8632 
8633    */
8634 
8635 static cp_declarator *
cp_parser_direct_new_declarator(cp_parser * parser)8636 cp_parser_direct_new_declarator (cp_parser* parser)
8637 {
8638   cp_declarator *declarator = NULL;
8639 
8640   while (true)
8641     {
8642       tree expression;
8643       cp_token *token;
8644 
8645       /* Look for the opening `['.  */
8646       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8647 
8648       token = cp_lexer_peek_token (parser->lexer);
8649       expression = cp_parser_expression (parser);
8650       /* The standard requires that the expression have integral
8651 	 type.  DR 74 adds enumeration types.  We believe that the
8652 	 real intent is that these expressions be handled like the
8653 	 expression in a `switch' condition, which also allows
8654 	 classes with a single conversion to integral or
8655 	 enumeration type.  */
8656       if (!processing_template_decl)
8657 	{
8658 	  expression
8659 	    = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8660 					  expression,
8661 					  /*complain=*/true);
8662 	  if (!expression)
8663 	    {
8664 	      error_at (token->location,
8665 			"expression in new-declarator must have integral "
8666 			"or enumeration type");
8667 	      expression = error_mark_node;
8668 	    }
8669 	}
8670 
8671       /* Look for the closing `]'.  */
8672       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8673 
8674       /* Add this bound to the declarator.  */
8675       declarator = make_array_declarator (declarator, expression);
8676 
8677       /* If the next token is not a `[', then there are no more
8678 	 bounds.  */
8679       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8680 	break;
8681     }
8682 
8683   return declarator;
8684 }
8685 
8686 /* Parse a new-initializer.
8687 
8688    new-initializer:
8689      ( expression-list [opt] )
8690      braced-init-list
8691 
8692    Returns a representation of the expression-list.  */
8693 
8694 static vec<tree, va_gc> *
cp_parser_new_initializer(cp_parser * parser)8695 cp_parser_new_initializer (cp_parser* parser)
8696 {
8697   vec<tree, va_gc> *expression_list;
8698 
8699   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8700     {
8701       tree t;
8702       bool expr_non_constant_p;
8703       cp_lexer_set_source_position (parser->lexer);
8704       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8705       t = cp_parser_braced_list (parser, &expr_non_constant_p);
8706       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8707       expression_list = make_tree_vector_single (t);
8708     }
8709   else
8710     expression_list = (cp_parser_parenthesized_expression_list
8711 		       (parser, non_attr, /*cast_p=*/false,
8712 			/*allow_expansion_p=*/true,
8713 			/*non_constant_p=*/NULL));
8714 
8715   return expression_list;
8716 }
8717 
8718 /* Parse a delete-expression.
8719 
8720    delete-expression:
8721      :: [opt] delete cast-expression
8722      :: [opt] delete [ ] cast-expression
8723 
8724    Returns a representation of the expression.  */
8725 
8726 static tree
cp_parser_delete_expression(cp_parser * parser)8727 cp_parser_delete_expression (cp_parser* parser)
8728 {
8729   bool global_scope_p;
8730   bool array_p;
8731   tree expression;
8732 
8733   /* Look for the optional `::' operator.  */
8734   global_scope_p
8735     = (cp_parser_global_scope_opt (parser,
8736 				   /*current_scope_valid_p=*/false)
8737        != NULL_TREE);
8738   /* Look for the `delete' keyword.  */
8739   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8740   /* See if the array syntax is in use.  */
8741   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8742     {
8743       /* Consume the `[' token.  */
8744       cp_lexer_consume_token (parser->lexer);
8745       /* Look for the `]' token.  */
8746       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8747       /* Remember that this is the `[]' construct.  */
8748       array_p = true;
8749     }
8750   else
8751     array_p = false;
8752 
8753   /* Parse the cast-expression.  */
8754   expression = cp_parser_simple_cast_expression (parser);
8755 
8756   /* A delete-expression may not appear in an integral constant
8757      expression.  */
8758   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8759     return error_mark_node;
8760 
8761   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8762 			tf_warning_or_error);
8763 }
8764 
8765 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8766    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8767    0 otherwise.  */
8768 
8769 static int
cp_parser_tokens_start_cast_expression(cp_parser * parser)8770 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8771 {
8772   cp_token *token = cp_lexer_peek_token (parser->lexer);
8773   switch (token->type)
8774     {
8775     case CPP_COMMA:
8776     case CPP_SEMICOLON:
8777     case CPP_QUERY:
8778     case CPP_COLON:
8779     case CPP_CLOSE_SQUARE:
8780     case CPP_CLOSE_PAREN:
8781     case CPP_CLOSE_BRACE:
8782     case CPP_OPEN_BRACE:
8783     case CPP_DOT:
8784     case CPP_DOT_STAR:
8785     case CPP_DEREF:
8786     case CPP_DEREF_STAR:
8787     case CPP_DIV:
8788     case CPP_MOD:
8789     case CPP_LSHIFT:
8790     case CPP_RSHIFT:
8791     case CPP_LESS:
8792     case CPP_GREATER:
8793     case CPP_LESS_EQ:
8794     case CPP_GREATER_EQ:
8795     case CPP_EQ_EQ:
8796     case CPP_NOT_EQ:
8797     case CPP_EQ:
8798     case CPP_MULT_EQ:
8799     case CPP_DIV_EQ:
8800     case CPP_MOD_EQ:
8801     case CPP_PLUS_EQ:
8802     case CPP_MINUS_EQ:
8803     case CPP_RSHIFT_EQ:
8804     case CPP_LSHIFT_EQ:
8805     case CPP_AND_EQ:
8806     case CPP_XOR_EQ:
8807     case CPP_OR_EQ:
8808     case CPP_XOR:
8809     case CPP_OR:
8810     case CPP_OR_OR:
8811     case CPP_EOF:
8812     case CPP_ELLIPSIS:
8813       return 0;
8814 
8815     case CPP_OPEN_PAREN:
8816       /* In ((type ()) () the last () isn't a valid cast-expression,
8817 	 so the whole must be parsed as postfix-expression.  */
8818       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8819 	     != CPP_CLOSE_PAREN;
8820 
8821     case CPP_OPEN_SQUARE:
8822       /* '[' may start a primary-expression in obj-c++ and in C++11,
8823 	 as a lambda-expression, eg, '(void)[]{}'.  */
8824       if (cxx_dialect >= cxx11)
8825 	return -1;
8826       return c_dialect_objc ();
8827 
8828     case CPP_PLUS_PLUS:
8829     case CPP_MINUS_MINUS:
8830       /* '++' and '--' may or may not start a cast-expression:
8831 
8832 	 struct T { void operator++(int); };
8833 	 void f() { (T())++; }
8834 
8835 	 vs
8836 
8837 	 int a;
8838 	 (int)++a;  */
8839       return -1;
8840 
8841     default:
8842       return 1;
8843     }
8844 }
8845 
8846 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8847    in the order: const_cast, static_cast, reinterpret_cast.
8848 
8849    Don't suggest dynamic_cast.
8850 
8851    Return the first legal cast kind found, or NULL otherwise.  */
8852 
8853 static const char *
get_cast_suggestion(tree dst_type,tree orig_expr)8854 get_cast_suggestion (tree dst_type, tree orig_expr)
8855 {
8856   tree trial;
8857 
8858   /* Reuse the parser logic by attempting to build the various kinds of
8859      cast, with "complain" disabled.
8860      Identify the first such cast that is valid.  */
8861 
8862   /* Don't attempt to run such logic within template processing.  */
8863   if (processing_template_decl)
8864     return NULL;
8865 
8866   /* First try const_cast.  */
8867   trial = build_const_cast (dst_type, orig_expr, tf_none);
8868   if (trial != error_mark_node)
8869     return "const_cast";
8870 
8871   /* If that fails, try static_cast.  */
8872   trial = build_static_cast (dst_type, orig_expr, tf_none);
8873   if (trial != error_mark_node)
8874     return "static_cast";
8875 
8876   /* Finally, try reinterpret_cast.  */
8877   trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8878   if (trial != error_mark_node)
8879     return "reinterpret_cast";
8880 
8881   /* No such cast possible.  */
8882   return NULL;
8883 }
8884 
8885 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8886    suggesting how to convert a C-style cast of the form:
8887 
8888      (DST_TYPE)ORIG_EXPR
8889 
8890    to a C++-style cast.
8891 
8892    The primary range of RICHLOC is asssumed to be that of the original
8893    expression.  OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8894    of the parens in the C-style cast.  */
8895 
8896 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)8897 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8898 		      location_t close_paren_loc, tree orig_expr,
8899 		      tree dst_type)
8900 {
8901   /* This function is non-trivial, so bail out now if the warning isn't
8902      going to be emitted.  */
8903   if (!warn_old_style_cast)
8904     return;
8905 
8906   /* Try to find a legal C++ cast, trying them in order:
8907      const_cast, static_cast, reinterpret_cast.  */
8908   const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8909   if (!cast_suggestion)
8910     return;
8911 
8912   /* Replace the open paren with "CAST_SUGGESTION<".  */
8913   pretty_printer pp;
8914   pp_printf (&pp, "%s<", cast_suggestion);
8915   rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8916 
8917   /* Replace the close paren with "> (".  */
8918   rich_loc->add_fixit_replace (close_paren_loc, "> (");
8919 
8920   /* Add a closing paren after the expr (the primary range of RICH_LOC).  */
8921   rich_loc->add_fixit_insert_after (")");
8922 }
8923 
8924 
8925 /* Parse a cast-expression.
8926 
8927    cast-expression:
8928      unary-expression
8929      ( type-id ) cast-expression
8930 
8931    ADDRESS_P is true iff the unary-expression is appearing as the
8932    operand of the `&' operator.   CAST_P is true if this expression is
8933    the target of a cast.
8934 
8935    Returns a representation of the expression.  */
8936 
8937 static cp_expr
cp_parser_cast_expression(cp_parser * parser,bool address_p,bool cast_p,bool decltype_p,cp_id_kind * pidk)8938 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8939 			   bool decltype_p, cp_id_kind * pidk)
8940 {
8941   /* If it's a `(', then we might be looking at a cast.  */
8942   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8943     {
8944       tree type = NULL_TREE;
8945       cp_expr expr (NULL_TREE);
8946       int cast_expression = 0;
8947       const char *saved_message;
8948 
8949       /* There's no way to know yet whether or not this is a cast.
8950 	 For example, `(int (3))' is a unary-expression, while `(int)
8951 	 3' is a cast.  So, we resort to parsing tentatively.  */
8952       cp_parser_parse_tentatively (parser);
8953       /* Types may not be defined in a cast.  */
8954       saved_message = parser->type_definition_forbidden_message;
8955       parser->type_definition_forbidden_message
8956 	= G_("types may not be defined in casts");
8957       /* Consume the `('.  */
8958       matching_parens parens;
8959       cp_token *open_paren = parens.consume_open (parser);
8960       location_t open_paren_loc = open_paren->location;
8961       location_t close_paren_loc = UNKNOWN_LOCATION;
8962 
8963       /* A very tricky bit is that `(struct S) { 3 }' is a
8964 	 compound-literal (which we permit in C++ as an extension).
8965 	 But, that construct is not a cast-expression -- it is a
8966 	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
8967 	 is legal; if the compound-literal were a cast-expression,
8968 	 you'd need an extra set of parentheses.)  But, if we parse
8969 	 the type-id, and it happens to be a class-specifier, then we
8970 	 will commit to the parse at that point, because we cannot
8971 	 undo the action that is done when creating a new class.  So,
8972 	 then we cannot back up and do a postfix-expression.
8973 
8974 	 Another tricky case is the following (c++/29234):
8975 
8976          struct S { void operator () (); };
8977 
8978          void foo ()
8979          {
8980            ( S()() );
8981          }
8982 
8983 	 As a type-id we parse the parenthesized S()() as a function
8984 	 returning a function, groktypename complains and we cannot
8985 	 back up in this case either.
8986 
8987 	 Therefore, we scan ahead to the closing `)', and check to see
8988 	 if the tokens after the `)' can start a cast-expression.  Otherwise
8989 	 we are dealing with an unary-expression, a postfix-expression
8990 	 or something else.
8991 
8992 	 Yet another tricky case, in C++11, is the following (c++/54891):
8993 
8994 	 (void)[]{};
8995 
8996          The issue is that usually, besides the case of lambda-expressions,
8997 	 the parenthesized type-id cannot be followed by '[', and, eg, we
8998 	 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8999 	 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9000 	 we don't commit, we try a cast-expression, then an unary-expression.
9001 
9002 	 Save tokens so that we can put them back.  */
9003       cp_lexer_save_tokens (parser->lexer);
9004 
9005       /* We may be looking at a cast-expression.  */
9006       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9007 						 /*consume_paren=*/true))
9008 	cast_expression
9009 	  = cp_parser_tokens_start_cast_expression (parser);
9010 
9011       /* Roll back the tokens we skipped.  */
9012       cp_lexer_rollback_tokens (parser->lexer);
9013       /* If we aren't looking at a cast-expression, simulate an error so
9014 	 that the call to cp_parser_error_occurred below returns true.  */
9015       if (!cast_expression)
9016 	cp_parser_simulate_error (parser);
9017       else
9018 	{
9019 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9020 	  parser->in_type_id_in_expr_p = true;
9021 	  /* Look for the type-id.  */
9022 	  type = cp_parser_type_id (parser);
9023 	  /* Look for the closing `)'.  */
9024 	  cp_token *close_paren = parens.require_close (parser);
9025 	  if (close_paren)
9026 	    close_paren_loc = close_paren->location;
9027 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9028 	}
9029 
9030       /* Restore the saved message.  */
9031       parser->type_definition_forbidden_message = saved_message;
9032 
9033       /* At this point this can only be either a cast or a
9034 	 parenthesized ctor such as `(T ())' that looks like a cast to
9035 	 function returning T.  */
9036       if (!cp_parser_error_occurred (parser))
9037 	{
9038 	  /* Only commit if the cast-expression doesn't start with
9039 	     '++', '--', or '[' in C++11.  */
9040 	  if (cast_expression > 0)
9041 	    cp_parser_commit_to_topmost_tentative_parse (parser);
9042 
9043 	  expr = cp_parser_cast_expression (parser,
9044 					    /*address_p=*/false,
9045 					    /*cast_p=*/true,
9046 					    /*decltype_p=*/false,
9047 					    pidk);
9048 
9049 	  if (cp_parser_parse_definitely (parser))
9050 	    {
9051 	      /* Warn about old-style casts, if so requested.  */
9052 	      if (warn_old_style_cast
9053 		  && !in_system_header_at (input_location)
9054 		  && !VOID_TYPE_P (type)
9055 		  && current_lang_name != lang_name_c)
9056 		{
9057 		  gcc_rich_location rich_loc (input_location);
9058 		  maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9059 					expr, type);
9060 		  warning_at (&rich_loc, OPT_Wold_style_cast,
9061 			      "use of old-style cast to %q#T", type);
9062 		}
9063 
9064 	      /* Only type conversions to integral or enumeration types
9065 		 can be used in constant-expressions.  */
9066 	      if (!cast_valid_in_integral_constant_expression_p (type)
9067 		  && cp_parser_non_integral_constant_expression (parser,
9068 								 NIC_CAST))
9069 		return error_mark_node;
9070 
9071 	      /* Perform the cast.  */
9072 	      /* Make a location:
9073 		   (TYPE) EXPR
9074 		   ^~~~~~~~~~~
9075 		 with start==caret at the open paren, extending to the
9076 		 end of "expr".  */
9077 	      location_t cast_loc = make_location (open_paren_loc,
9078 						   open_paren_loc,
9079 						   expr.get_finish ());
9080 	      expr = build_c_cast (cast_loc, type, expr);
9081 	      return expr;
9082 	    }
9083 	}
9084       else
9085         cp_parser_abort_tentative_parse (parser);
9086     }
9087 
9088   /* If we get here, then it's not a cast, so it must be a
9089      unary-expression.  */
9090   return cp_parser_unary_expression (parser, pidk, address_p,
9091 				     cast_p, decltype_p);
9092 }
9093 
9094 /* Parse a binary expression of the general form:
9095 
9096    pm-expression:
9097      cast-expression
9098      pm-expression .* cast-expression
9099      pm-expression ->* cast-expression
9100 
9101    multiplicative-expression:
9102      pm-expression
9103      multiplicative-expression * pm-expression
9104      multiplicative-expression / pm-expression
9105      multiplicative-expression % pm-expression
9106 
9107    additive-expression:
9108      multiplicative-expression
9109      additive-expression + multiplicative-expression
9110      additive-expression - multiplicative-expression
9111 
9112    shift-expression:
9113      additive-expression
9114      shift-expression << additive-expression
9115      shift-expression >> additive-expression
9116 
9117    relational-expression:
9118      shift-expression
9119      relational-expression < shift-expression
9120      relational-expression > shift-expression
9121      relational-expression <= shift-expression
9122      relational-expression >= shift-expression
9123 
9124   GNU Extension:
9125 
9126    relational-expression:
9127      relational-expression <? shift-expression
9128      relational-expression >? shift-expression
9129 
9130    equality-expression:
9131      relational-expression
9132      equality-expression == relational-expression
9133      equality-expression != relational-expression
9134 
9135    and-expression:
9136      equality-expression
9137      and-expression & equality-expression
9138 
9139    exclusive-or-expression:
9140      and-expression
9141      exclusive-or-expression ^ and-expression
9142 
9143    inclusive-or-expression:
9144      exclusive-or-expression
9145      inclusive-or-expression | exclusive-or-expression
9146 
9147    logical-and-expression:
9148      inclusive-or-expression
9149      logical-and-expression && inclusive-or-expression
9150 
9151    logical-or-expression:
9152      logical-and-expression
9153      logical-or-expression || logical-and-expression
9154 
9155    All these are implemented with a single function like:
9156 
9157    binary-expression:
9158      simple-cast-expression
9159      binary-expression <token> binary-expression
9160 
9161    CAST_P is true if this expression is the target of a cast.
9162 
9163    The binops_by_token map is used to get the tree codes for each <token> type.
9164    binary-expressions are associated according to a precedence table.  */
9165 
9166 #define TOKEN_PRECEDENCE(token)				     \
9167 (((token->type == CPP_GREATER				     \
9168    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9169   && !parser->greater_than_is_operator_p)		     \
9170  ? PREC_NOT_OPERATOR					     \
9171  : binops_by_token[token->type].prec)
9172 
9173 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)9174 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9175 			     bool no_toplevel_fold_p,
9176 			     bool decltype_p,
9177 			     enum cp_parser_prec prec,
9178 			     cp_id_kind * pidk)
9179 {
9180   cp_parser_expression_stack stack;
9181   cp_parser_expression_stack_entry *sp = &stack[0];
9182   cp_parser_expression_stack_entry current;
9183   cp_expr rhs;
9184   cp_token *token;
9185   enum tree_code rhs_type;
9186   enum cp_parser_prec new_prec, lookahead_prec;
9187   tree overload;
9188 
9189   /* Parse the first expression.  */
9190   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9191 		      ? TRUTH_NOT_EXPR : ERROR_MARK);
9192   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9193 					   cast_p, decltype_p, pidk);
9194   current.prec = prec;
9195 
9196   if (cp_parser_error_occurred (parser))
9197     return error_mark_node;
9198 
9199   for (;;)
9200     {
9201       /* Get an operator token.  */
9202       token = cp_lexer_peek_token (parser->lexer);
9203 
9204       if (warn_cxx11_compat
9205           && token->type == CPP_RSHIFT
9206           && !parser->greater_than_is_operator_p)
9207         {
9208           if (warning_at (token->location, OPT_Wc__11_compat,
9209 			  "%<>>%> operator is treated"
9210 			  " as two right angle brackets in C++11"))
9211 	    inform (token->location,
9212 		    "suggest parentheses around %<>>%> expression");
9213         }
9214 
9215       new_prec = TOKEN_PRECEDENCE (token);
9216       if (new_prec != PREC_NOT_OPERATOR
9217 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9218 	/* This is a fold-expression; handle it later.  */
9219 	new_prec = PREC_NOT_OPERATOR;
9220 
9221       /* Popping an entry off the stack means we completed a subexpression:
9222 	 - either we found a token which is not an operator (`>' where it is not
9223 	   an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9224 	   will happen repeatedly;
9225 	 - or, we found an operator which has lower priority.  This is the case
9226 	   where the recursive descent *ascends*, as in `3 * 4 + 5' after
9227 	   parsing `3 * 4'.  */
9228       if (new_prec <= current.prec)
9229 	{
9230 	  if (sp == stack)
9231 	    break;
9232 	  else
9233 	    goto pop;
9234 	}
9235 
9236      get_rhs:
9237       current.tree_type = binops_by_token[token->type].tree_type;
9238       current.loc = token->location;
9239 
9240       /* We used the operator token.  */
9241       cp_lexer_consume_token (parser->lexer);
9242 
9243       /* For "false && x" or "true || x", x will never be executed;
9244 	 disable warnings while evaluating it.  */
9245       if (current.tree_type == TRUTH_ANDIF_EXPR)
9246 	c_inhibit_evaluation_warnings +=
9247 	  cp_fully_fold (current.lhs) == truthvalue_false_node;
9248       else if (current.tree_type == TRUTH_ORIF_EXPR)
9249 	c_inhibit_evaluation_warnings +=
9250 	  cp_fully_fold (current.lhs) == truthvalue_true_node;
9251 
9252       /* Extract another operand.  It may be the RHS of this expression
9253 	 or the LHS of a new, higher priority expression.  */
9254       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9255 		  ? TRUTH_NOT_EXPR : ERROR_MARK);
9256       rhs = cp_parser_simple_cast_expression (parser);
9257 
9258       /* Get another operator token.  Look up its precedence to avoid
9259 	 building a useless (immediately popped) stack entry for common
9260 	 cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
9261       token = cp_lexer_peek_token (parser->lexer);
9262       lookahead_prec = TOKEN_PRECEDENCE (token);
9263       if (lookahead_prec != PREC_NOT_OPERATOR
9264 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9265 	lookahead_prec = PREC_NOT_OPERATOR;
9266       if (lookahead_prec > new_prec)
9267 	{
9268 	  /* ... and prepare to parse the RHS of the new, higher priority
9269 	     expression.  Since precedence levels on the stack are
9270 	     monotonically increasing, we do not have to care about
9271 	     stack overflows.  */
9272 	  *sp = current;
9273 	  ++sp;
9274 	  current.lhs = rhs;
9275 	  current.lhs_type = rhs_type;
9276 	  current.prec = new_prec;
9277 	  new_prec = lookahead_prec;
9278 	  goto get_rhs;
9279 
9280 	 pop:
9281 	  lookahead_prec = new_prec;
9282 	  /* If the stack is not empty, we have parsed into LHS the right side
9283 	     (`4' in the example above) of an expression we had suspended.
9284 	     We can use the information on the stack to recover the LHS (`3')
9285 	     from the stack together with the tree code (`MULT_EXPR'), and
9286 	     the precedence of the higher level subexpression
9287 	     (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
9288 	     which will be used to actually build the additive expression.  */
9289 	  rhs = current.lhs;
9290 	  rhs_type = current.lhs_type;
9291 	  --sp;
9292 	  current = *sp;
9293 	}
9294 
9295       /* Undo the disabling of warnings done above.  */
9296       if (current.tree_type == TRUTH_ANDIF_EXPR)
9297 	c_inhibit_evaluation_warnings -=
9298 	  cp_fully_fold (current.lhs) == truthvalue_false_node;
9299       else if (current.tree_type == TRUTH_ORIF_EXPR)
9300 	c_inhibit_evaluation_warnings -=
9301 	  cp_fully_fold (current.lhs) == truthvalue_true_node;
9302 
9303       if (warn_logical_not_paren
9304 	  && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9305 	  && current.lhs_type == TRUTH_NOT_EXPR
9306 	  /* Avoid warning for !!x == y.  */
9307 	  && (TREE_CODE (current.lhs) != NE_EXPR
9308 	      || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9309 	  && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9310 	      || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9311 		  /* Avoid warning for !b == y where b is boolean.  */
9312 		  && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9313 		      || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9314 			  != BOOLEAN_TYPE))))
9315 	  /* Avoid warning for !!b == y where b is boolean.  */
9316 	  && (!DECL_P (current.lhs)
9317 	      || TREE_TYPE (current.lhs) == NULL_TREE
9318 	      || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9319 	warn_logical_not_parentheses (current.loc, current.tree_type,
9320 				      current.lhs, maybe_constant_value (rhs));
9321 
9322       overload = NULL;
9323 
9324       location_t combined_loc = make_location (current.loc,
9325 					       current.lhs.get_start (),
9326 					       rhs.get_finish ());
9327 
9328       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9329 	 ERROR_MARK for everything that is not a binary expression.
9330 	 This makes warn_about_parentheses miss some warnings that
9331 	 involve unary operators.  For unary expressions we should
9332 	 pass the correct tree_code unless the unary expression was
9333 	 surrounded by parentheses.
9334       */
9335       if (no_toplevel_fold_p
9336 	  && lookahead_prec <= current.prec
9337 	  && sp == stack)
9338 	{
9339 	  if (current.lhs == error_mark_node || rhs == error_mark_node)
9340 	    current.lhs = error_mark_node;
9341 	  else
9342 	    {
9343 	      current.lhs
9344 		= build_min (current.tree_type,
9345 			     TREE_CODE_CLASS (current.tree_type)
9346 			     == tcc_comparison
9347 			     ? boolean_type_node : TREE_TYPE (current.lhs),
9348 			     current.lhs.get_value (), rhs.get_value ());
9349 	      SET_EXPR_LOCATION (current.lhs, combined_loc);
9350 	    }
9351 	}
9352       else
9353         {
9354           current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9355                                            current.lhs, current.lhs_type,
9356                                            rhs, rhs_type, &overload,
9357                                            complain_flags (decltype_p));
9358           /* TODO: build_x_binary_op doesn't always honor the location.  */
9359           current.lhs.set_location (combined_loc);
9360         }
9361       current.lhs_type = current.tree_type;
9362 
9363       /* If the binary operator required the use of an overloaded operator,
9364 	 then this expression cannot be an integral constant-expression.
9365 	 An overloaded operator can be used even if both operands are
9366 	 otherwise permissible in an integral constant-expression if at
9367 	 least one of the operands is of enumeration type.  */
9368 
9369       if (overload
9370 	  && cp_parser_non_integral_constant_expression (parser,
9371 							 NIC_OVERLOADED))
9372 	return error_mark_node;
9373     }
9374 
9375   return current.lhs;
9376 }
9377 
9378 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)9379 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9380 			     bool no_toplevel_fold_p,
9381 			     enum cp_parser_prec prec,
9382 			     cp_id_kind * pidk)
9383 {
9384   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9385 				      /*decltype*/false, prec, pidk);
9386 }
9387 
9388 /* Parse the `? expression : assignment-expression' part of a
9389    conditional-expression.  The LOGICAL_OR_EXPR is the
9390    logical-or-expression that started the conditional-expression.
9391    Returns a representation of the entire conditional-expression.
9392 
9393    This routine is used by cp_parser_assignment_expression.
9394 
9395      ? expression : assignment-expression
9396 
9397    GNU Extensions:
9398 
9399      ? : assignment-expression */
9400 
9401 static tree
cp_parser_question_colon_clause(cp_parser * parser,cp_expr logical_or_expr)9402 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9403 {
9404   tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9405   cp_expr assignment_expr;
9406   struct cp_token *token;
9407   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9408 
9409   /* Consume the `?' token.  */
9410   cp_lexer_consume_token (parser->lexer);
9411   token = cp_lexer_peek_token (parser->lexer);
9412   if (cp_parser_allow_gnu_extensions_p (parser)
9413       && token->type == CPP_COLON)
9414     {
9415       pedwarn (token->location, OPT_Wpedantic,
9416                "ISO C++ does not allow ?: with omitted middle operand");
9417       /* Implicit true clause.  */
9418       expr = NULL_TREE;
9419       c_inhibit_evaluation_warnings +=
9420 	folded_logical_or_expr == truthvalue_true_node;
9421       warn_for_omitted_condop (token->location, logical_or_expr);
9422     }
9423   else
9424     {
9425       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9426       parser->colon_corrects_to_scope_p = false;
9427       /* Parse the expression.  */
9428       c_inhibit_evaluation_warnings +=
9429 	folded_logical_or_expr == truthvalue_false_node;
9430       expr = cp_parser_expression (parser);
9431       c_inhibit_evaluation_warnings +=
9432 	((folded_logical_or_expr == truthvalue_true_node)
9433 	 - (folded_logical_or_expr == truthvalue_false_node));
9434       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9435     }
9436 
9437   /* The next token should be a `:'.  */
9438   cp_parser_require (parser, CPP_COLON, RT_COLON);
9439   /* Parse the assignment-expression.  */
9440   assignment_expr = cp_parser_assignment_expression (parser);
9441   c_inhibit_evaluation_warnings -=
9442     folded_logical_or_expr == truthvalue_true_node;
9443 
9444   /* Make a location:
9445        LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9446        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9447      with the caret at the "?", ranging from the start of
9448      the logical_or_expr to the end of the assignment_expr.  */
9449   loc = make_location (loc,
9450 		       logical_or_expr.get_start (),
9451 		       assignment_expr.get_finish ());
9452 
9453   /* Build the conditional-expression.  */
9454   return build_x_conditional_expr (loc, logical_or_expr,
9455 				   expr,
9456 				   assignment_expr,
9457                                    tf_warning_or_error);
9458 }
9459 
9460 /* Parse an assignment-expression.
9461 
9462    assignment-expression:
9463      conditional-expression
9464      logical-or-expression assignment-operator assignment_expression
9465      throw-expression
9466 
9467    CAST_P is true if this expression is the target of a cast.
9468    DECLTYPE_P is true if this expression is the operand of decltype.
9469 
9470    Returns a representation for the expression.  */
9471 
9472 static cp_expr
cp_parser_assignment_expression(cp_parser * parser,cp_id_kind * pidk,bool cast_p,bool decltype_p)9473 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9474 				 bool cast_p, bool decltype_p)
9475 {
9476   cp_expr expr;
9477 
9478   /* If the next token is the `throw' keyword, then we're looking at
9479      a throw-expression.  */
9480   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9481     expr = cp_parser_throw_expression (parser);
9482   /* Otherwise, it must be that we are looking at a
9483      logical-or-expression.  */
9484   else
9485     {
9486       /* Parse the binary expressions (logical-or-expression).  */
9487       expr = cp_parser_binary_expression (parser, cast_p, false,
9488 					  decltype_p,
9489 					  PREC_NOT_OPERATOR, pidk);
9490       /* If the next token is a `?' then we're actually looking at a
9491 	 conditional-expression.  */
9492       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9493 	return cp_parser_question_colon_clause (parser, expr);
9494       else
9495 	{
9496 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9497 
9498 	  /* If it's an assignment-operator, we're using the second
9499 	     production.  */
9500 	  enum tree_code assignment_operator
9501 	    = cp_parser_assignment_operator_opt (parser);
9502 	  if (assignment_operator != ERROR_MARK)
9503 	    {
9504 	      bool non_constant_p;
9505 
9506 	      /* Parse the right-hand side of the assignment.  */
9507 	      cp_expr rhs = cp_parser_initializer_clause (parser,
9508 							  &non_constant_p);
9509 
9510 	      if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9511 		maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9512 
9513 	      /* An assignment may not appear in a
9514 		 constant-expression.  */
9515 	      if (cp_parser_non_integral_constant_expression (parser,
9516 							      NIC_ASSIGNMENT))
9517 		return error_mark_node;
9518 	      /* Build the assignment expression.  Its default
9519 		 location:
9520 		   LHS = RHS
9521 		   ~~~~^~~~~
9522 		 is the location of the '=' token as the
9523 		 caret, ranging from the start of the lhs to the
9524 		 end of the rhs.  */
9525 	      loc = make_location (loc,
9526 				   expr.get_start (),
9527 				   rhs.get_finish ());
9528 	      expr = build_x_modify_expr (loc, expr,
9529 					  assignment_operator,
9530 					  rhs,
9531 					  complain_flags (decltype_p));
9532               /* TODO: build_x_modify_expr doesn't honor the location,
9533                  so we must set it here.  */
9534               expr.set_location (loc);
9535 	    }
9536 	}
9537     }
9538 
9539   return expr;
9540 }
9541 
9542 /* Parse an (optional) assignment-operator.
9543 
9544    assignment-operator: one of
9545      = *= /= %= += -= >>= <<= &= ^= |=
9546 
9547    GNU Extension:
9548 
9549    assignment-operator: one of
9550      <?= >?=
9551 
9552    If the next token is an assignment operator, the corresponding tree
9553    code is returned, and the token is consumed.  For example, for
9554    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
9555    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
9556    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
9557    operator, ERROR_MARK is returned.  */
9558 
9559 static enum tree_code
cp_parser_assignment_operator_opt(cp_parser * parser)9560 cp_parser_assignment_operator_opt (cp_parser* parser)
9561 {
9562   enum tree_code op;
9563   cp_token *token;
9564 
9565   /* Peek at the next token.  */
9566   token = cp_lexer_peek_token (parser->lexer);
9567 
9568   switch (token->type)
9569     {
9570     case CPP_EQ:
9571       op = NOP_EXPR;
9572       break;
9573 
9574     case CPP_MULT_EQ:
9575       op = MULT_EXPR;
9576       break;
9577 
9578     case CPP_DIV_EQ:
9579       op = TRUNC_DIV_EXPR;
9580       break;
9581 
9582     case CPP_MOD_EQ:
9583       op = TRUNC_MOD_EXPR;
9584       break;
9585 
9586     case CPP_PLUS_EQ:
9587       op = PLUS_EXPR;
9588       break;
9589 
9590     case CPP_MINUS_EQ:
9591       op = MINUS_EXPR;
9592       break;
9593 
9594     case CPP_RSHIFT_EQ:
9595       op = RSHIFT_EXPR;
9596       break;
9597 
9598     case CPP_LSHIFT_EQ:
9599       op = LSHIFT_EXPR;
9600       break;
9601 
9602     case CPP_AND_EQ:
9603       op = BIT_AND_EXPR;
9604       break;
9605 
9606     case CPP_XOR_EQ:
9607       op = BIT_XOR_EXPR;
9608       break;
9609 
9610     case CPP_OR_EQ:
9611       op = BIT_IOR_EXPR;
9612       break;
9613 
9614     default:
9615       /* Nothing else is an assignment operator.  */
9616       op = ERROR_MARK;
9617     }
9618 
9619   /* An operator followed by ... is a fold-expression, handled elsewhere.  */
9620   if (op != ERROR_MARK
9621       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9622     op = ERROR_MARK;
9623 
9624   /* If it was an assignment operator, consume it.  */
9625   if (op != ERROR_MARK)
9626     cp_lexer_consume_token (parser->lexer);
9627 
9628   return op;
9629 }
9630 
9631 /* Parse an expression.
9632 
9633    expression:
9634      assignment-expression
9635      expression , assignment-expression
9636 
9637    CAST_P is true if this expression is the target of a cast.
9638    DECLTYPE_P is true if this expression is the immediate operand of decltype,
9639      except possibly parenthesized or on the RHS of a comma (N3276).
9640 
9641    Returns a representation of the expression.  */
9642 
9643 static cp_expr
cp_parser_expression(cp_parser * parser,cp_id_kind * pidk,bool cast_p,bool decltype_p)9644 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9645 		      bool cast_p, bool decltype_p)
9646 {
9647   cp_expr expression = NULL_TREE;
9648   location_t loc = UNKNOWN_LOCATION;
9649 
9650   while (true)
9651     {
9652       cp_expr assignment_expression;
9653 
9654       /* Parse the next assignment-expression.  */
9655       assignment_expression
9656 	= cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9657 
9658       /* We don't create a temporary for a call that is the immediate operand
9659 	 of decltype or on the RHS of a comma.  But when we see a comma, we
9660 	 need to create a temporary for a call on the LHS.  */
9661       if (decltype_p && !processing_template_decl
9662 	  && TREE_CODE (assignment_expression) == CALL_EXPR
9663 	  && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9664 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9665 	assignment_expression
9666 	  = build_cplus_new (TREE_TYPE (assignment_expression),
9667 			     assignment_expression, tf_warning_or_error);
9668 
9669       /* If this is the first assignment-expression, we can just
9670 	 save it away.  */
9671       if (!expression)
9672 	expression = assignment_expression;
9673       else
9674 	{
9675 	  /* Create a location with caret at the comma, ranging
9676 	     from the start of the LHS to the end of the RHS.  */
9677 	  loc = make_location (loc,
9678 			       expression.get_start (),
9679 			       assignment_expression.get_finish ());
9680 	  expression = build_x_compound_expr (loc, expression,
9681 					      assignment_expression,
9682 					      complain_flags (decltype_p));
9683 	  expression.set_location (loc);
9684 	}
9685       /* If the next token is not a comma, or we're in a fold-expression, then
9686 	 we are done with the expression.  */
9687       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9688 	  || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9689 	break;
9690       /* Consume the `,'.  */
9691       loc = cp_lexer_peek_token (parser->lexer)->location;
9692       cp_lexer_consume_token (parser->lexer);
9693       /* A comma operator cannot appear in a constant-expression.  */
9694       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9695 	expression = error_mark_node;
9696     }
9697 
9698   return expression;
9699 }
9700 
9701 /* Parse a constant-expression.
9702 
9703    constant-expression:
9704      conditional-expression
9705 
9706   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9707   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
9708   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
9709   is false, NON_CONSTANT_P should be NULL.  If STRICT_P is true,
9710   only parse a conditional-expression, otherwise parse an
9711   assignment-expression.  See below for rationale.  */
9712 
9713 static cp_expr
cp_parser_constant_expression(cp_parser * parser,bool allow_non_constant_p,bool * non_constant_p,bool strict_p)9714 cp_parser_constant_expression (cp_parser* parser,
9715 			       bool allow_non_constant_p,
9716 			       bool *non_constant_p,
9717 			       bool strict_p)
9718 {
9719   bool saved_integral_constant_expression_p;
9720   bool saved_allow_non_integral_constant_expression_p;
9721   bool saved_non_integral_constant_expression_p;
9722   cp_expr expression;
9723 
9724   /* It might seem that we could simply parse the
9725      conditional-expression, and then check to see if it were
9726      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
9727      one that the compiler can figure out is constant, possibly after
9728      doing some simplifications or optimizations.  The standard has a
9729      precise definition of constant-expression, and we must honor
9730      that, even though it is somewhat more restrictive.
9731 
9732      For example:
9733 
9734        int i[(2, 3)];
9735 
9736      is not a legal declaration, because `(2, 3)' is not a
9737      constant-expression.  The `,' operator is forbidden in a
9738      constant-expression.  However, GCC's constant-folding machinery
9739      will fold this operation to an INTEGER_CST for `3'.  */
9740 
9741   /* Save the old settings.  */
9742   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9743   saved_allow_non_integral_constant_expression_p
9744     = parser->allow_non_integral_constant_expression_p;
9745   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9746   /* We are now parsing a constant-expression.  */
9747   parser->integral_constant_expression_p = true;
9748   parser->allow_non_integral_constant_expression_p
9749     = (allow_non_constant_p || cxx_dialect >= cxx11);
9750   parser->non_integral_constant_expression_p = false;
9751   /* Although the grammar says "conditional-expression", when not STRICT_P,
9752      we parse an "assignment-expression", which also permits
9753      "throw-expression" and the use of assignment operators.  In the case
9754      that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9755      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
9756      actually essential that we look for an assignment-expression.
9757      For example, cp_parser_initializer_clauses uses this function to
9758      determine whether a particular assignment-expression is in fact
9759      constant.  */
9760   if (strict_p)
9761     {
9762       /* Parse the binary expressions (logical-or-expression).  */
9763       expression = cp_parser_binary_expression (parser, false, false, false,
9764 						PREC_NOT_OPERATOR, NULL);
9765       /* If the next token is a `?' then we're actually looking at
9766 	 a conditional-expression; otherwise we're done.  */
9767       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9768 	expression = cp_parser_question_colon_clause (parser, expression);
9769     }
9770   else
9771     expression = cp_parser_assignment_expression (parser);
9772   /* Restore the old settings.  */
9773   parser->integral_constant_expression_p
9774     = saved_integral_constant_expression_p;
9775   parser->allow_non_integral_constant_expression_p
9776     = saved_allow_non_integral_constant_expression_p;
9777   if (cxx_dialect >= cxx11)
9778     {
9779       /* Require an rvalue constant expression here; that's what our
9780 	 callers expect.  Reference constant expressions are handled
9781 	 separately in e.g. cp_parser_template_argument.  */
9782       tree decay = expression;
9783       if (TREE_TYPE (expression)
9784 	  && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9785 	decay = build_address (expression);
9786       bool is_const = potential_rvalue_constant_expression (decay);
9787       parser->non_integral_constant_expression_p = !is_const;
9788       if (!is_const && !allow_non_constant_p)
9789 	require_potential_rvalue_constant_expression (decay);
9790     }
9791   if (allow_non_constant_p)
9792     *non_constant_p = parser->non_integral_constant_expression_p;
9793   parser->non_integral_constant_expression_p
9794     = saved_non_integral_constant_expression_p;
9795 
9796   return expression;
9797 }
9798 
9799 /* Parse __builtin_offsetof.
9800 
9801    offsetof-expression:
9802      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9803 
9804    offsetof-member-designator:
9805      id-expression
9806      | offsetof-member-designator "." id-expression
9807      | offsetof-member-designator "[" expression "]"
9808      | offsetof-member-designator "->" id-expression  */
9809 
9810 static cp_expr
cp_parser_builtin_offsetof(cp_parser * parser)9811 cp_parser_builtin_offsetof (cp_parser *parser)
9812 {
9813   int save_ice_p, save_non_ice_p;
9814   tree type;
9815   cp_expr expr;
9816   cp_id_kind dummy;
9817   cp_token *token;
9818   location_t finish_loc;
9819 
9820   /* We're about to accept non-integral-constant things, but will
9821      definitely yield an integral constant expression.  Save and
9822      restore these values around our local parsing.  */
9823   save_ice_p = parser->integral_constant_expression_p;
9824   save_non_ice_p = parser->non_integral_constant_expression_p;
9825 
9826   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9827 
9828   /* Consume the "__builtin_offsetof" token.  */
9829   cp_lexer_consume_token (parser->lexer);
9830   /* Consume the opening `('.  */
9831   matching_parens parens;
9832   parens.require_open (parser);
9833   /* Parse the type-id.  */
9834   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9835   {
9836     const char *saved_message = parser->type_definition_forbidden_message;
9837     parser->type_definition_forbidden_message
9838       = G_("types may not be defined within __builtin_offsetof");
9839     type = cp_parser_type_id (parser);
9840     parser->type_definition_forbidden_message = saved_message;
9841   }
9842   /* Look for the `,'.  */
9843   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9844   token = cp_lexer_peek_token (parser->lexer);
9845 
9846   /* Build the (type *)null that begins the traditional offsetof macro.  */
9847   tree object_ptr
9848     = build_static_cast (build_pointer_type (type), null_pointer_node,
9849 			 tf_warning_or_error);
9850 
9851   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
9852   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9853 						 true, &dummy, token->location);
9854   while (true)
9855     {
9856       token = cp_lexer_peek_token (parser->lexer);
9857       switch (token->type)
9858 	{
9859 	case CPP_OPEN_SQUARE:
9860 	  /* offsetof-member-designator "[" expression "]" */
9861 	  expr = cp_parser_postfix_open_square_expression (parser, expr,
9862 							   true, false);
9863 	  break;
9864 
9865 	case CPP_DEREF:
9866 	  /* offsetof-member-designator "->" identifier */
9867 	  expr = grok_array_decl (token->location, expr,
9868 				  integer_zero_node, false);
9869 	  /* FALLTHRU */
9870 
9871 	case CPP_DOT:
9872 	  /* offsetof-member-designator "." identifier */
9873 	  cp_lexer_consume_token (parser->lexer);
9874 	  expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9875 							 expr, true, &dummy,
9876 							 token->location);
9877 	  break;
9878 
9879 	case CPP_CLOSE_PAREN:
9880 	  /* Consume the ")" token.  */
9881 	  finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9882 	  cp_lexer_consume_token (parser->lexer);
9883 	  goto success;
9884 
9885 	default:
9886 	  /* Error.  We know the following require will fail, but
9887 	     that gives the proper error message.  */
9888 	  parens.require_close (parser);
9889 	  cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9890 	  expr = error_mark_node;
9891 	  goto failure;
9892 	}
9893     }
9894 
9895  success:
9896   /* Make a location of the form:
9897        __builtin_offsetof (struct s, f)
9898        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9899      with caret at the type-id, ranging from the start of the
9900      "_builtin_offsetof" token to the close paren.  */
9901   loc = make_location (loc, start_loc, finish_loc);
9902   /* The result will be an INTEGER_CST, so we need to explicitly
9903      preserve the location.  */
9904   expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9905 
9906  failure:
9907   parser->integral_constant_expression_p = save_ice_p;
9908   parser->non_integral_constant_expression_p = save_non_ice_p;
9909 
9910   expr = expr.maybe_add_location_wrapper ();
9911   return expr;
9912 }
9913 
9914 /* Parse a trait expression.
9915 
9916    Returns a representation of the expression, the underlying type
9917    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
9918 
9919 static cp_expr
cp_parser_trait_expr(cp_parser * parser,enum rid keyword)9920 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9921 {
9922   cp_trait_kind kind;
9923   tree type1, type2 = NULL_TREE;
9924   bool binary = false;
9925   bool variadic = false;
9926 
9927   switch (keyword)
9928     {
9929     case RID_HAS_NOTHROW_ASSIGN:
9930       kind = CPTK_HAS_NOTHROW_ASSIGN;
9931       break;
9932     case RID_HAS_NOTHROW_CONSTRUCTOR:
9933       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9934       break;
9935     case RID_HAS_NOTHROW_COPY:
9936       kind = CPTK_HAS_NOTHROW_COPY;
9937       break;
9938     case RID_HAS_TRIVIAL_ASSIGN:
9939       kind = CPTK_HAS_TRIVIAL_ASSIGN;
9940       break;
9941     case RID_HAS_TRIVIAL_CONSTRUCTOR:
9942       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9943       break;
9944     case RID_HAS_TRIVIAL_COPY:
9945       kind = CPTK_HAS_TRIVIAL_COPY;
9946       break;
9947     case RID_HAS_TRIVIAL_DESTRUCTOR:
9948       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9949       break;
9950     case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9951       kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9952       break;
9953     case RID_HAS_VIRTUAL_DESTRUCTOR:
9954       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9955       break;
9956     case RID_IS_ABSTRACT:
9957       kind = CPTK_IS_ABSTRACT;
9958       break;
9959     case RID_IS_AGGREGATE:
9960       kind = CPTK_IS_AGGREGATE;
9961       break;
9962     case RID_IS_BASE_OF:
9963       kind = CPTK_IS_BASE_OF;
9964       binary = true;
9965       break;
9966     case RID_IS_CLASS:
9967       kind = CPTK_IS_CLASS;
9968       break;
9969     case RID_IS_EMPTY:
9970       kind = CPTK_IS_EMPTY;
9971       break;
9972     case RID_IS_ENUM:
9973       kind = CPTK_IS_ENUM;
9974       break;
9975     case RID_IS_FINAL:
9976       kind = CPTK_IS_FINAL;
9977       break;
9978     case RID_IS_LITERAL_TYPE:
9979       kind = CPTK_IS_LITERAL_TYPE;
9980       break;
9981     case RID_IS_POD:
9982       kind = CPTK_IS_POD;
9983       break;
9984     case RID_IS_POLYMORPHIC:
9985       kind = CPTK_IS_POLYMORPHIC;
9986       break;
9987     case RID_IS_SAME_AS:
9988       kind = CPTK_IS_SAME_AS;
9989       binary = true;
9990       break;
9991     case RID_IS_STD_LAYOUT:
9992       kind = CPTK_IS_STD_LAYOUT;
9993       break;
9994     case RID_IS_TRIVIAL:
9995       kind = CPTK_IS_TRIVIAL;
9996       break;
9997     case RID_IS_TRIVIALLY_ASSIGNABLE:
9998       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9999       binary = true;
10000       break;
10001     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10002       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10003       variadic = true;
10004       break;
10005     case RID_IS_TRIVIALLY_COPYABLE:
10006       kind = CPTK_IS_TRIVIALLY_COPYABLE;
10007       break;
10008     case RID_IS_UNION:
10009       kind = CPTK_IS_UNION;
10010       break;
10011     case RID_UNDERLYING_TYPE:
10012       kind = CPTK_UNDERLYING_TYPE;
10013       break;
10014     case RID_BASES:
10015       kind = CPTK_BASES;
10016       break;
10017     case RID_DIRECT_BASES:
10018       kind = CPTK_DIRECT_BASES;
10019       break;
10020     case RID_IS_ASSIGNABLE:
10021       kind = CPTK_IS_ASSIGNABLE;
10022       binary = true;
10023       break;
10024     case RID_IS_CONSTRUCTIBLE:
10025       kind = CPTK_IS_CONSTRUCTIBLE;
10026       variadic = true;
10027       break;
10028     default:
10029       gcc_unreachable ();
10030     }
10031 
10032   /* Get location of initial token.  */
10033   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10034 
10035   /* Consume the token.  */
10036   cp_lexer_consume_token (parser->lexer);
10037 
10038   matching_parens parens;
10039   parens.require_open (parser);
10040 
10041   {
10042     type_id_in_expr_sentinel s (parser);
10043     type1 = cp_parser_type_id (parser);
10044   }
10045 
10046   if (type1 == error_mark_node)
10047     return error_mark_node;
10048 
10049   if (binary)
10050     {
10051       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10052 
10053       {
10054 	type_id_in_expr_sentinel s (parser);
10055 	type2 = cp_parser_type_id (parser);
10056       }
10057 
10058       if (type2 == error_mark_node)
10059 	return error_mark_node;
10060     }
10061   else if (variadic)
10062     {
10063       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10064 	{
10065 	  cp_lexer_consume_token (parser->lexer);
10066 	  tree elt = cp_parser_type_id (parser);
10067 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10068 	    {
10069 	      cp_lexer_consume_token (parser->lexer);
10070 	      elt = make_pack_expansion (elt);
10071 	    }
10072 	  if (elt == error_mark_node)
10073 	    return error_mark_node;
10074 	  type2 = tree_cons (NULL_TREE, elt, type2);
10075 	}
10076     }
10077 
10078   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10079   parens.require_close (parser);
10080 
10081   /* Construct a location of the form:
10082        __is_trivially_copyable(_Tp)
10083        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10084      with start == caret, finishing at the close-paren.  */
10085   location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10086 
10087   /* Complete the trait expression, which may mean either processing
10088      the trait expr now or saving it for template instantiation.  */
10089   switch (kind)
10090     {
10091     case CPTK_UNDERLYING_TYPE:
10092       return cp_expr (finish_underlying_type (type1), trait_loc);
10093     case CPTK_BASES:
10094       return cp_expr (finish_bases (type1, false), trait_loc);
10095     case CPTK_DIRECT_BASES:
10096       return cp_expr (finish_bases (type1, true), trait_loc);
10097     default:
10098       return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10099     }
10100 }
10101 
10102 /* Parse a lambda expression.
10103 
10104    lambda-expression:
10105      lambda-introducer lambda-declarator [opt] compound-statement
10106 
10107    Returns a representation of the expression.  */
10108 
10109 static cp_expr
cp_parser_lambda_expression(cp_parser * parser)10110 cp_parser_lambda_expression (cp_parser* parser)
10111 {
10112   tree lambda_expr = build_lambda_expr ();
10113   tree type;
10114   bool ok = true;
10115   cp_token *token = cp_lexer_peek_token (parser->lexer);
10116   cp_token_position start = 0;
10117 
10118   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10119 
10120   if (cp_unevaluated_operand)
10121     {
10122       if (!token->error_reported)
10123 	{
10124 	  error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10125 		    "lambda-expression in unevaluated context");
10126 	  token->error_reported = true;
10127 	}
10128       ok = false;
10129     }
10130   else if (parser->in_template_argument_list_p)
10131     {
10132       if (!token->error_reported)
10133 	{
10134 	  error_at (token->location, "lambda-expression in template-argument");
10135 	  token->error_reported = true;
10136 	}
10137       ok = false;
10138     }
10139 
10140   /* We may be in the middle of deferred access check.  Disable
10141      it now.  */
10142   push_deferring_access_checks (dk_no_deferred);
10143 
10144   cp_parser_lambda_introducer (parser, lambda_expr);
10145 
10146   type = begin_lambda_type (lambda_expr);
10147   if (type == error_mark_node)
10148     return error_mark_node;
10149 
10150   record_lambda_scope (lambda_expr);
10151 
10152   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
10153   determine_visibility (TYPE_NAME (type));
10154 
10155   /* Now that we've started the type, add the capture fields for any
10156      explicit captures.  */
10157   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10158 
10159   {
10160     /* Inside the class, surrounding template-parameter-lists do not apply.  */
10161     unsigned int saved_num_template_parameter_lists
10162         = parser->num_template_parameter_lists;
10163     unsigned char in_statement = parser->in_statement;
10164     bool in_switch_statement_p = parser->in_switch_statement_p;
10165     bool fully_implicit_function_template_p
10166         = parser->fully_implicit_function_template_p;
10167     tree implicit_template_parms = parser->implicit_template_parms;
10168     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10169     bool auto_is_implicit_function_template_parm_p
10170         = parser->auto_is_implicit_function_template_parm_p;
10171 
10172     parser->num_template_parameter_lists = 0;
10173     parser->in_statement = 0;
10174     parser->in_switch_statement_p = false;
10175     parser->fully_implicit_function_template_p = false;
10176     parser->implicit_template_parms = 0;
10177     parser->implicit_template_scope = 0;
10178     parser->auto_is_implicit_function_template_parm_p = false;
10179 
10180     /* By virtue of defining a local class, a lambda expression has access to
10181        the private variables of enclosing classes.  */
10182 
10183     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10184 
10185     if (ok && cp_parser_error_occurred (parser))
10186       ok = false;
10187 
10188     if (ok)
10189       {
10190 	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10191 	    && cp_parser_start_tentative_firewall (parser))
10192 	  start = token;
10193 	cp_parser_lambda_body (parser, lambda_expr);
10194       }
10195     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10196       {
10197 	if (cp_parser_skip_to_closing_brace (parser))
10198 	  cp_lexer_consume_token (parser->lexer);
10199       }
10200 
10201     /* The capture list was built up in reverse order; fix that now.  */
10202     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10203       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10204 
10205     if (ok)
10206       maybe_add_lambda_conv_op (type);
10207 
10208     type = finish_struct (type, /*attributes=*/NULL_TREE);
10209 
10210     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10211     parser->in_statement = in_statement;
10212     parser->in_switch_statement_p = in_switch_statement_p;
10213     parser->fully_implicit_function_template_p
10214 	= fully_implicit_function_template_p;
10215     parser->implicit_template_parms = implicit_template_parms;
10216     parser->implicit_template_scope = implicit_template_scope;
10217     parser->auto_is_implicit_function_template_parm_p
10218 	= auto_is_implicit_function_template_parm_p;
10219   }
10220 
10221   /* This field is only used during parsing of the lambda.  */
10222   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10223 
10224   /* This lambda shouldn't have any proxies left at this point.  */
10225   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10226   /* And now that we're done, push proxies for an enclosing lambda.  */
10227   insert_pending_capture_proxies ();
10228 
10229   if (ok)
10230     lambda_expr = build_lambda_object (lambda_expr);
10231   else
10232     lambda_expr = error_mark_node;
10233 
10234   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10235 
10236   pop_deferring_access_checks ();
10237 
10238   return lambda_expr;
10239 }
10240 
10241 /* Parse the beginning of a lambda expression.
10242 
10243    lambda-introducer:
10244      [ lambda-capture [opt] ]
10245 
10246    LAMBDA_EXPR is the current representation of the lambda expression.  */
10247 
10248 static void
cp_parser_lambda_introducer(cp_parser * parser,tree lambda_expr)10249 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10250 {
10251   /* Need commas after the first capture.  */
10252   bool first = true;
10253 
10254   /* Eat the leading `['.  */
10255   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10256 
10257   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
10258   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10259       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10260     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10261   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10262     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10263 
10264   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10265     {
10266       cp_lexer_consume_token (parser->lexer);
10267       first = false;
10268     }
10269 
10270   hash_set<tree> *ids = NULL;
10271 #if GCC_VERSION >= 8000
10272   char ids_buf[sizeof (hash_set<tree>) + __alignof__ (hash_set<tree>) - 1];
10273 #endif
10274   tree first_capture_id = NULL_TREE;
10275   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10276     {
10277       cp_token* capture_token;
10278       tree capture_id;
10279       tree capture_init_expr;
10280       cp_id_kind idk = CP_ID_KIND_NONE;
10281       bool explicit_init_p = false;
10282 
10283       enum capture_kind_type
10284       {
10285 	BY_COPY,
10286 	BY_REFERENCE
10287       };
10288       enum capture_kind_type capture_kind = BY_COPY;
10289 
10290       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10291 	{
10292 	  error ("expected end of capture-list");
10293 	  return;
10294 	}
10295 
10296       if (first)
10297 	first = false;
10298       else
10299 	cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10300 
10301       /* Possibly capture `this'.  */
10302       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10303 	{
10304 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10305 	  if (cxx_dialect < cxx2a
10306 	      && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10307 	    pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10308 		     "with by-copy capture default");
10309 	  cp_lexer_consume_token (parser->lexer);
10310 	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10311 	    pedwarn (input_location, 0,
10312 		     "already captured %qD in lambda expression",
10313 		     this_identifier);
10314 	  else
10315 	    add_capture (lambda_expr, /*id=*/this_identifier,
10316 			 /*initializer=*/finish_this_expr (),
10317 			 /*by_reference_p=*/true, explicit_init_p);
10318 	  continue;
10319 	}
10320 
10321       /* Possibly capture `*this'.  */
10322       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10323 	  && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10324 	{
10325 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10326 	  if (cxx_dialect < cxx17)
10327 	    pedwarn (loc, 0, "%<*this%> capture only available with "
10328 			     "-std=c++17 or -std=gnu++17");
10329 	  cp_lexer_consume_token (parser->lexer);
10330 	  cp_lexer_consume_token (parser->lexer);
10331 	  if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
10332 	    pedwarn (input_location, 0,
10333 		     "already captured %qD in lambda expression",
10334 		     this_identifier);
10335 	  else
10336 	    add_capture (lambda_expr, /*id=*/this_identifier,
10337 			 /*initializer=*/finish_this_expr (),
10338 			 /*by_reference_p=*/false, explicit_init_p);
10339 	  continue;
10340 	}
10341 
10342       /* Remember whether we want to capture as a reference or not.  */
10343       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10344 	{
10345 	  capture_kind = BY_REFERENCE;
10346 	  cp_lexer_consume_token (parser->lexer);
10347 	}
10348 
10349       /* Get the identifier.  */
10350       capture_token = cp_lexer_peek_token (parser->lexer);
10351       capture_id = cp_parser_identifier (parser);
10352 
10353       if (capture_id == error_mark_node)
10354 	/* Would be nice to have a cp_parser_skip_to_closing_x for general
10355            delimiters, but I modified this to stop on unnested ']' as well.  It
10356            was already changed to stop on unnested '}', so the
10357            "closing_parenthesis" name is no more misleading with my change.  */
10358 	{
10359 	  cp_parser_skip_to_closing_parenthesis (parser,
10360 						 /*recovering=*/true,
10361 						 /*or_comma=*/true,
10362 						 /*consume_paren=*/true);
10363 	  break;
10364 	}
10365 
10366       /* Find the initializer for this capture.  */
10367       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10368 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10369 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10370 	{
10371 	  bool direct, non_constant;
10372 	  /* An explicit initializer exists.  */
10373 	  if (cxx_dialect < cxx14)
10374 	    pedwarn (input_location, 0,
10375 		     "lambda capture initializers "
10376 		     "only available with -std=c++14 or -std=gnu++14");
10377 	  capture_init_expr = cp_parser_initializer (parser, &direct,
10378 						     &non_constant, true);
10379 	  explicit_init_p = true;
10380 	  if (capture_init_expr == NULL_TREE)
10381 	    {
10382 	      error ("empty initializer for lambda init-capture");
10383 	      capture_init_expr = error_mark_node;
10384 	    }
10385 	}
10386       else
10387 	{
10388 	  const char* error_msg;
10389 
10390 	  /* Turn the identifier into an id-expression.  */
10391 	  capture_init_expr
10392 	    = cp_parser_lookup_name_simple (parser, capture_id,
10393 					    capture_token->location);
10394 
10395 	  if (capture_init_expr == error_mark_node)
10396 	    {
10397 	      unqualified_name_lookup_error (capture_id);
10398 	      continue;
10399 	    }
10400 	  else if (!VAR_P (capture_init_expr)
10401 		   && TREE_CODE (capture_init_expr) != PARM_DECL)
10402 	    {
10403 	      error_at (capture_token->location,
10404 			"capture of non-variable %qE",
10405 			capture_init_expr);
10406 	      if (DECL_P (capture_init_expr))
10407 		inform (DECL_SOURCE_LOCATION (capture_init_expr),
10408 			"%q#D declared here", capture_init_expr);
10409 	      continue;
10410 	    }
10411 	  if (VAR_P (capture_init_expr)
10412 	      && decl_storage_duration (capture_init_expr) != dk_auto)
10413 	    {
10414 	      if (pedwarn (capture_token->location, 0, "capture of variable "
10415 			   "%qD with non-automatic storage duration",
10416 			   capture_init_expr))
10417 		inform (DECL_SOURCE_LOCATION (capture_init_expr),
10418 			"%q#D declared here", capture_init_expr);
10419 	      continue;
10420 	    }
10421 
10422 	  capture_init_expr
10423             = finish_id_expression
10424                 (capture_id,
10425 		 capture_init_expr,
10426                  parser->scope,
10427                  &idk,
10428                  /*integral_constant_expression_p=*/false,
10429                  /*allow_non_integral_constant_expression_p=*/false,
10430                  /*non_integral_constant_expression_p=*/NULL,
10431                  /*template_p=*/false,
10432                  /*done=*/true,
10433                  /*address_p=*/false,
10434                  /*template_arg_p=*/false,
10435                  &error_msg,
10436                  capture_token->location);
10437 
10438 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10439 	    {
10440 	      cp_lexer_consume_token (parser->lexer);
10441 	      capture_init_expr = make_pack_expansion (capture_init_expr);
10442 	    }
10443 	}
10444 
10445       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10446 	  && !explicit_init_p)
10447 	{
10448 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10449 	      && capture_kind == BY_COPY)
10450 	    pedwarn (capture_token->location, 0, "explicit by-copy capture "
10451 		     "of %qD redundant with by-copy capture default",
10452 		     capture_id);
10453 	  if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10454 	      && capture_kind == BY_REFERENCE)
10455 	    pedwarn (capture_token->location, 0, "explicit by-reference "
10456 		     "capture of %qD redundant with by-reference capture "
10457 		     "default", capture_id);
10458 	}
10459 
10460       /* Check for duplicates.
10461 	 Optimize for the zero or one explicit captures cases and only create
10462 	 the hash_set after adding second capture.  */
10463       bool found = false;
10464       if (ids && ids->elements ())
10465 	found = ids->add (capture_id);
10466       else if (first_capture_id == NULL_TREE)
10467 	first_capture_id = capture_id;
10468       else if (capture_id == first_capture_id)
10469 	found = true;
10470       else
10471 	{
10472 #if GCC_VERSION >= 8000
10473 	  ids = new (ids_buf
10474 		     + (-(uintptr_t) ids_buf
10475 			& (__alignof__ (hash_set <tree>) - 1))) hash_set <tree>;
10476 #else
10477 	  ids = new hash_set <tree>;
10478 #endif
10479 	  ids->add (first_capture_id);
10480 	  ids->add (capture_id);
10481 	}
10482       if (found)
10483 	pedwarn (input_location, 0,
10484 		 "already captured %qD in lambda expression", capture_id);
10485       else
10486 	add_capture (lambda_expr, capture_id, capture_init_expr,
10487 		     /*by_reference_p=*/capture_kind == BY_REFERENCE,
10488 		     explicit_init_p);
10489 
10490       /* If there is any qualification still in effect, clear it
10491 	 now; we will be starting fresh with the next capture.  */
10492       parser->scope = NULL_TREE;
10493       parser->qualifying_scope = NULL_TREE;
10494       parser->object_scope = NULL_TREE;
10495     }
10496 
10497   if (ids)
10498 #if GCC_VERSION >= 8000
10499     ids->~hash_set <tree> ();
10500 #else
10501     delete ids;
10502 #endif
10503 
10504   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10505 }
10506 
10507 /* Parse the (optional) middle of a lambda expression.
10508 
10509    lambda-declarator:
10510      < template-parameter-list [opt] >
10511      ( parameter-declaration-clause [opt] )
10512        attribute-specifier [opt]
10513        decl-specifier-seq [opt]
10514        exception-specification [opt]
10515        lambda-return-type-clause [opt]
10516 
10517    LAMBDA_EXPR is the current representation of the lambda expression.  */
10518 
10519 static bool
cp_parser_lambda_declarator_opt(cp_parser * parser,tree lambda_expr)10520 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10521 {
10522   /* 5.1.1.4 of the standard says:
10523        If a lambda-expression does not include a lambda-declarator, it is as if
10524        the lambda-declarator were ().
10525      This means an empty parameter list, no attributes, and no exception
10526      specification.  */
10527   tree param_list = void_list_node;
10528   tree attributes = NULL_TREE;
10529   tree exception_spec = NULL_TREE;
10530   tree template_param_list = NULL_TREE;
10531   tree tx_qual = NULL_TREE;
10532   tree return_type = NULL_TREE;
10533   cp_decl_specifier_seq lambda_specs;
10534   clear_decl_specs (&lambda_specs);
10535 
10536   /* The template-parameter-list is optional, but must begin with
10537      an opening angle if present.  */
10538   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10539     {
10540       if (cxx_dialect < cxx14)
10541 	pedwarn (parser->lexer->next_token->location, 0,
10542 		 "lambda templates are only available with "
10543 		 "-std=c++14 or -std=gnu++14");
10544       else if (cxx_dialect < cxx2a)
10545 	pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10546 		 "lambda templates are only available with "
10547 		 "-std=c++2a or -std=gnu++2a");
10548 
10549       cp_lexer_consume_token (parser->lexer);
10550 
10551       template_param_list = cp_parser_template_parameter_list (parser);
10552 
10553       cp_parser_skip_to_end_of_template_parameter_list (parser);
10554 
10555       /* We just processed one more parameter list.  */
10556       ++parser->num_template_parameter_lists;
10557     }
10558 
10559   /* The parameter-declaration-clause is optional (unless
10560      template-parameter-list was given), but must begin with an
10561      opening parenthesis if present.  */
10562   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10563     {
10564       matching_parens parens;
10565       parens.consume_open (parser);
10566 
10567       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10568 
10569       /* Parse parameters.  */
10570       param_list = cp_parser_parameter_declaration_clause (parser);
10571 
10572       /* Default arguments shall not be specified in the
10573 	 parameter-declaration-clause of a lambda-declarator.  */
10574       if (cxx_dialect < cxx14)
10575 	for (tree t = param_list; t; t = TREE_CHAIN (t))
10576 	  if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10577 	    pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10578 		     "default argument specified for lambda parameter");
10579 
10580       parens.require_close (parser);
10581 
10582       attributes = cp_parser_attributes_opt (parser);
10583 
10584       /* In the decl-specifier-seq of the lambda-declarator, each
10585 	 decl-specifier shall either be mutable or constexpr.  */
10586       int declares_class_or_enum;
10587       if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10588 	cp_parser_decl_specifier_seq (parser,
10589 				      CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10590 				      &lambda_specs, &declares_class_or_enum);
10591       if (lambda_specs.storage_class == sc_mutable)
10592 	{
10593 	  LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10594 	  if (lambda_specs.conflicting_specifiers_p)
10595 	    error_at (lambda_specs.locations[ds_storage_class],
10596 		      "duplicate %<mutable%>");
10597 	}
10598 
10599       tx_qual = cp_parser_tx_qualifier_opt (parser);
10600 
10601       /* Parse optional exception specification.  */
10602       exception_spec = cp_parser_exception_specification_opt (parser);
10603 
10604       /* Parse optional trailing return type.  */
10605       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10606         {
10607           cp_lexer_consume_token (parser->lexer);
10608           return_type = cp_parser_trailing_type_id (parser);
10609         }
10610 
10611       /* The function parameters must be in scope all the way until after the
10612          trailing-return-type in case of decltype.  */
10613       pop_bindings_and_leave_scope ();
10614     }
10615   else if (template_param_list != NULL_TREE) // generate diagnostic
10616     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10617 
10618   /* Create the function call operator.
10619 
10620      Messing with declarators like this is no uglier than building up the
10621      FUNCTION_DECL by hand, and this is less likely to get out of sync with
10622      other code.  */
10623   {
10624     cp_decl_specifier_seq return_type_specs;
10625     cp_declarator* declarator;
10626     tree fco;
10627     int quals;
10628     void *p;
10629 
10630     clear_decl_specs (&return_type_specs);
10631     if (return_type)
10632       return_type_specs.type = return_type;
10633     else
10634       /* Maybe we will deduce the return type later.  */
10635       return_type_specs.type = make_auto ();
10636 
10637     if (lambda_specs.locations[ds_constexpr])
10638       {
10639 	if (cxx_dialect >= cxx17)
10640 	  return_type_specs.locations[ds_constexpr]
10641 	    = lambda_specs.locations[ds_constexpr];
10642 	else
10643 	  error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10644 		    "lambda only available with -std=c++17 or -std=gnu++17");
10645       }
10646 
10647     p = obstack_alloc (&declarator_obstack, 0);
10648 
10649     declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10650 
10651     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10652 	     ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10653     declarator = make_call_declarator (declarator, param_list, quals,
10654 				       VIRT_SPEC_UNSPECIFIED,
10655                                        REF_QUAL_NONE,
10656 				       tx_qual,
10657 				       exception_spec,
10658                                        /*late_return_type=*/NULL_TREE,
10659                                        /*requires_clause*/NULL_TREE);
10660     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10661 
10662     fco = grokmethod (&return_type_specs,
10663 		      declarator,
10664 		      attributes);
10665     if (fco != error_mark_node)
10666       {
10667 	DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10668 	DECL_ARTIFICIAL (fco) = 1;
10669 	/* Give the object parameter a different name.  */
10670 	DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10671 	DECL_LAMBDA_FUNCTION (fco) = 1;
10672 	if (return_type)
10673 	  TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10674       }
10675     if (template_param_list)
10676       {
10677 	fco = finish_member_template_decl (fco);
10678 	finish_template_decl (template_param_list);
10679 	--parser->num_template_parameter_lists;
10680       }
10681     else if (parser->fully_implicit_function_template_p)
10682       fco = finish_fully_implicit_template (parser, fco);
10683 
10684     finish_member_declaration (fco);
10685 
10686     obstack_free (&declarator_obstack, p);
10687 
10688     return (fco != error_mark_node);
10689   }
10690 }
10691 
10692 /* Parse the body of a lambda expression, which is simply
10693 
10694    compound-statement
10695 
10696    but which requires special handling.
10697    LAMBDA_EXPR is the current representation of the lambda expression.  */
10698 
10699 static void
cp_parser_lambda_body(cp_parser * parser,tree lambda_expr)10700 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10701 {
10702   bool nested = (current_function_decl != NULL_TREE);
10703   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10704   bool in_function_body = parser->in_function_body;
10705 
10706   if (nested)
10707     push_function_context ();
10708   else
10709     /* Still increment function_depth so that we don't GC in the
10710        middle of an expression.  */
10711     ++function_depth;
10712 
10713   vec<tree> omp_privatization_save;
10714   save_omp_privatization_clauses (omp_privatization_save);
10715   /* Clear this in case we're in the middle of a default argument.  */
10716   parser->local_variables_forbidden_p = false;
10717   parser->in_function_body = true;
10718 
10719   {
10720     local_specialization_stack s (lss_copy);
10721     tree fco = lambda_function (lambda_expr);
10722     tree body = start_lambda_function (fco, lambda_expr);
10723     matching_braces braces;
10724 
10725     if (braces.require_open (parser))
10726       {
10727 	tree compound_stmt = begin_compound_stmt (0);
10728 
10729 	/* Originally C++11 required us to peek for 'return expr'; and
10730 	   process it specially here to deduce the return type.  N3638
10731 	   removed the need for that.  */
10732 
10733 	while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10734 	  cp_parser_label_declaration (parser);
10735 	cp_parser_statement_seq_opt (parser, NULL_TREE);
10736 	braces.require_close (parser);
10737 
10738 	finish_compound_stmt (compound_stmt);
10739       }
10740 
10741     finish_lambda_function (body);
10742   }
10743 
10744   restore_omp_privatization_clauses (omp_privatization_save);
10745   parser->local_variables_forbidden_p = local_variables_forbidden_p;
10746   parser->in_function_body = in_function_body;
10747   if (nested)
10748     pop_function_context();
10749   else
10750     --function_depth;
10751 }
10752 
10753 /* Statements [gram.stmt.stmt]  */
10754 
10755 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
10756 
10757 static void
add_debug_begin_stmt(location_t loc)10758 add_debug_begin_stmt (location_t loc)
10759 {
10760   if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10761     return;
10762   if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10763     /* A concept is never expanded normally.  */
10764     return;
10765 
10766   tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10767   SET_EXPR_LOCATION (stmt, loc);
10768   add_stmt (stmt);
10769 }
10770 
10771 /* Parse a statement.
10772 
10773    statement:
10774      labeled-statement
10775      expression-statement
10776      compound-statement
10777      selection-statement
10778      iteration-statement
10779      jump-statement
10780      declaration-statement
10781      try-block
10782 
10783   C++11:
10784 
10785   statement:
10786     labeled-statement
10787     attribute-specifier-seq (opt) expression-statement
10788     attribute-specifier-seq (opt) compound-statement
10789     attribute-specifier-seq (opt) selection-statement
10790     attribute-specifier-seq (opt) iteration-statement
10791     attribute-specifier-seq (opt) jump-statement
10792     declaration-statement
10793     attribute-specifier-seq (opt) try-block
10794 
10795   init-statement:
10796     expression-statement
10797     simple-declaration
10798 
10799   TM Extension:
10800 
10801    statement:
10802      atomic-statement
10803 
10804   IN_COMPOUND is true when the statement is nested inside a
10805   cp_parser_compound_statement; this matters for certain pragmas.
10806 
10807   If IF_P is not NULL, *IF_P is set to indicate whether the statement
10808   is a (possibly labeled) if statement which is not enclosed in braces
10809   and has an else clause.  This is used to implement -Wparentheses.
10810 
10811   CHAIN is a vector of if-else-if conditions.  */
10812 
10813 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)10814 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10815 		     bool in_compound, bool *if_p, vec<tree> *chain,
10816 		     location_t *loc_after_labels)
10817 {
10818   tree statement, std_attrs = NULL_TREE;
10819   cp_token *token;
10820   location_t statement_location, attrs_location;
10821 
10822  restart:
10823   if (if_p != NULL)
10824     *if_p = false;
10825   /* There is no statement yet.  */
10826   statement = NULL_TREE;
10827 
10828   saved_token_sentinel saved_tokens (parser->lexer);
10829   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10830   if (c_dialect_objc ())
10831     /* In obj-c++, seeing '[[' might be the either the beginning of
10832        c++11 attributes, or a nested objc-message-expression.  So
10833        let's parse the c++11 attributes tentatively.  */
10834     cp_parser_parse_tentatively (parser);
10835   std_attrs = cp_parser_std_attribute_spec_seq (parser);
10836   if (c_dialect_objc ())
10837     {
10838       if (!cp_parser_parse_definitely (parser))
10839 	std_attrs = NULL_TREE;
10840     }
10841 
10842   /* Peek at the next token.  */
10843   token = cp_lexer_peek_token (parser->lexer);
10844   /* Remember the location of the first token in the statement.  */
10845   statement_location = token->location;
10846   add_debug_begin_stmt (statement_location);
10847   /* If this is a keyword, then that will often determine what kind of
10848      statement we have.  */
10849   if (token->type == CPP_KEYWORD)
10850     {
10851       enum rid keyword = token->keyword;
10852 
10853       switch (keyword)
10854 	{
10855 	case RID_CASE:
10856 	case RID_DEFAULT:
10857 	  /* Looks like a labeled-statement with a case label.
10858 	     Parse the label, and then use tail recursion to parse
10859 	     the statement.  */
10860 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
10861 	  in_compound = false;
10862 	  goto restart;
10863 
10864 	case RID_IF:
10865 	case RID_SWITCH:
10866 	  statement = cp_parser_selection_statement (parser, if_p, chain);
10867 	  break;
10868 
10869 	case RID_WHILE:
10870 	case RID_DO:
10871 	case RID_FOR:
10872 	  statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10873 	  break;
10874 
10875 	case RID_BREAK:
10876 	case RID_CONTINUE:
10877 	case RID_RETURN:
10878 	case RID_GOTO:
10879 	  statement = cp_parser_jump_statement (parser);
10880 	  break;
10881 
10882 	  /* Objective-C++ exception-handling constructs.  */
10883 	case RID_AT_TRY:
10884 	case RID_AT_CATCH:
10885 	case RID_AT_FINALLY:
10886 	case RID_AT_SYNCHRONIZED:
10887 	case RID_AT_THROW:
10888 	  statement = cp_parser_objc_statement (parser);
10889 	  break;
10890 
10891 	case RID_TRY:
10892 	  statement = cp_parser_try_block (parser);
10893 	  break;
10894 
10895 	case RID_NAMESPACE:
10896 	  /* This must be a namespace alias definition.  */
10897 	  cp_parser_declaration_statement (parser);
10898 	  return;
10899 
10900 	case RID_TRANSACTION_ATOMIC:
10901 	case RID_TRANSACTION_RELAXED:
10902 	case RID_SYNCHRONIZED:
10903 	case RID_ATOMIC_NOEXCEPT:
10904 	case RID_ATOMIC_CANCEL:
10905 	  statement = cp_parser_transaction (parser, token);
10906 	  break;
10907 	case RID_TRANSACTION_CANCEL:
10908 	  statement = cp_parser_transaction_cancel (parser);
10909 	  break;
10910 
10911 	default:
10912 	  /* It might be a keyword like `int' that can start a
10913 	     declaration-statement.  */
10914 	  break;
10915 	}
10916     }
10917   else if (token->type == CPP_NAME)
10918     {
10919       /* If the next token is a `:', then we are looking at a
10920 	 labeled-statement.  */
10921       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10922       if (token->type == CPP_COLON)
10923 	{
10924 	  /* Looks like a labeled-statement with an ordinary label.
10925 	     Parse the label, and then use tail recursion to parse
10926 	     the statement.  */
10927 
10928 	  cp_parser_label_for_labeled_statement (parser, std_attrs);
10929 	  in_compound = false;
10930 	  goto restart;
10931 	}
10932     }
10933   /* Anything that starts with a `{' must be a compound-statement.  */
10934   else if (token->type == CPP_OPEN_BRACE)
10935     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10936   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10937      a statement all its own.  */
10938   else if (token->type == CPP_PRAGMA)
10939     {
10940       /* Only certain OpenMP pragmas are attached to statements, and thus
10941 	 are considered statements themselves.  All others are not.  In
10942 	 the context of a compound, accept the pragma as a "statement" and
10943 	 return so that we can check for a close brace.  Otherwise we
10944 	 require a real statement and must go back and read one.  */
10945       if (in_compound)
10946 	cp_parser_pragma (parser, pragma_compound, if_p);
10947       else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10948 	goto restart;
10949       return;
10950     }
10951   else if (token->type == CPP_EOF)
10952     {
10953       cp_parser_error (parser, "expected statement");
10954       return;
10955     }
10956 
10957   /* Everything else must be a declaration-statement or an
10958      expression-statement.  Try for the declaration-statement
10959      first, unless we are looking at a `;', in which case we know that
10960      we have an expression-statement.  */
10961   if (!statement)
10962     {
10963       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10964 	{
10965 	  if (std_attrs != NULL_TREE)
10966 	    {
10967 	      /*  Attributes should be parsed as part of the the
10968 		  declaration, so let's un-parse them.  */
10969 	      saved_tokens.rollback();
10970 	      std_attrs = NULL_TREE;
10971 	    }
10972 
10973 	  cp_parser_parse_tentatively (parser);
10974 	  /* Try to parse the declaration-statement.  */
10975 	  cp_parser_declaration_statement (parser);
10976 	  /* If that worked, we're done.  */
10977 	  if (cp_parser_parse_definitely (parser))
10978 	    return;
10979 	}
10980       /* All preceding labels have been parsed at this point.  */
10981       if (loc_after_labels != NULL)
10982 	*loc_after_labels = statement_location;
10983 
10984       /* Look for an expression-statement instead.  */
10985       statement = cp_parser_expression_statement (parser, in_statement_expr);
10986 
10987       /* Handle [[fallthrough]];.  */
10988       if (attribute_fallthrough_p (std_attrs))
10989 	{
10990 	  /* The next token after the fallthrough attribute is ';'.  */
10991 	  if (statement == NULL_TREE)
10992 	    {
10993 	      /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
10994 	      statement = build_call_expr_internal_loc (statement_location,
10995 							IFN_FALLTHROUGH,
10996 							void_type_node, 0);
10997 	      finish_expr_stmt (statement);
10998 	    }
10999 	  else
11000 	    warning_at (statement_location, OPT_Wattributes,
11001 			"%<fallthrough%> attribute not followed by %<;%>");
11002 	  std_attrs = NULL_TREE;
11003 	}
11004     }
11005 
11006   /* Set the line number for the statement.  */
11007   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11008     SET_EXPR_LOCATION (statement, statement_location);
11009 
11010   /* Allow "[[fallthrough]];", but warn otherwise.  */
11011   if (std_attrs != NULL_TREE)
11012     warning_at (attrs_location,
11013 		OPT_Wattributes,
11014 		"attributes at the beginning of statement are ignored");
11015 }
11016 
11017 /* Append ATTR to attribute list ATTRS.  */
11018 
11019 static tree
attr_chainon(tree attrs,tree attr)11020 attr_chainon (tree attrs, tree attr)
11021 {
11022   if (attrs == error_mark_node)
11023     return error_mark_node;
11024   if (attr == error_mark_node)
11025     return error_mark_node;
11026   return chainon (attrs, attr);
11027 }
11028 
11029 /* Parse the label for a labeled-statement, i.e.
11030 
11031    identifier :
11032    case constant-expression :
11033    default :
11034 
11035    GNU Extension:
11036    case constant-expression ... constant-expression : statement
11037 
11038    When a label is parsed without errors, the label is added to the
11039    parse tree by the finish_* functions, so this function doesn't
11040    have to return the label.  */
11041 
11042 static void
cp_parser_label_for_labeled_statement(cp_parser * parser,tree attributes)11043 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11044 {
11045   cp_token *token;
11046   tree label = NULL_TREE;
11047   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11048 
11049   /* The next token should be an identifier.  */
11050   token = cp_lexer_peek_token (parser->lexer);
11051   if (token->type != CPP_NAME
11052       && token->type != CPP_KEYWORD)
11053     {
11054       cp_parser_error (parser, "expected labeled-statement");
11055       return;
11056     }
11057 
11058   /* Remember whether this case or a user-defined label is allowed to fall
11059      through to.  */
11060   bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11061 
11062   parser->colon_corrects_to_scope_p = false;
11063   switch (token->keyword)
11064     {
11065     case RID_CASE:
11066       {
11067 	tree expr, expr_hi;
11068 	cp_token *ellipsis;
11069 
11070 	/* Consume the `case' token.  */
11071 	cp_lexer_consume_token (parser->lexer);
11072 	/* Parse the constant-expression.  */
11073 	expr = cp_parser_constant_expression (parser);
11074 	if (check_for_bare_parameter_packs (expr))
11075 	  expr = error_mark_node;
11076 
11077 	ellipsis = cp_lexer_peek_token (parser->lexer);
11078 	if (ellipsis->type == CPP_ELLIPSIS)
11079 	  {
11080 	    /* Consume the `...' token.  */
11081 	    cp_lexer_consume_token (parser->lexer);
11082 	    expr_hi = cp_parser_constant_expression (parser);
11083 	    if (check_for_bare_parameter_packs (expr_hi))
11084 	      expr_hi = error_mark_node;
11085 
11086 	    /* We don't need to emit warnings here, as the common code
11087 	       will do this for us.  */
11088 	  }
11089 	else
11090 	  expr_hi = NULL_TREE;
11091 
11092 	if (parser->in_switch_statement_p)
11093 	  {
11094 	    tree l = finish_case_label (token->location, expr, expr_hi);
11095 	    if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11096 	      FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11097 	  }
11098 	else
11099 	  error_at (token->location,
11100 		    "case label %qE not within a switch statement",
11101 		    expr);
11102       }
11103       break;
11104 
11105     case RID_DEFAULT:
11106       /* Consume the `default' token.  */
11107       cp_lexer_consume_token (parser->lexer);
11108 
11109       if (parser->in_switch_statement_p)
11110 	{
11111 	  tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11112 	  if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11113 	    FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11114 	}
11115       else
11116 	error_at (token->location, "case label not within a switch statement");
11117       break;
11118 
11119     default:
11120       /* Anything else must be an ordinary label.  */
11121       label = finish_label_stmt (cp_parser_identifier (parser));
11122       if (label && TREE_CODE (label) == LABEL_DECL)
11123 	FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11124       break;
11125     }
11126 
11127   /* Require the `:' token.  */
11128   cp_parser_require (parser, CPP_COLON, RT_COLON);
11129 
11130   /* An ordinary label may optionally be followed by attributes.
11131      However, this is only permitted if the attributes are then
11132      followed by a semicolon.  This is because, for backward
11133      compatibility, when parsing
11134        lab: __attribute__ ((unused)) int i;
11135      we want the attribute to attach to "i", not "lab".  */
11136   if (label != NULL_TREE
11137       && cp_next_tokens_can_be_gnu_attribute_p (parser))
11138     {
11139       tree attrs;
11140       cp_parser_parse_tentatively (parser);
11141       attrs = cp_parser_gnu_attributes_opt (parser);
11142       if (attrs == NULL_TREE
11143 	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11144 	cp_parser_abort_tentative_parse (parser);
11145       else if (!cp_parser_parse_definitely (parser))
11146 	;
11147       else
11148 	attributes = attr_chainon (attributes, attrs);
11149     }
11150 
11151   if (attributes != NULL_TREE)
11152     cplus_decl_attributes (&label, attributes, 0);
11153 
11154   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11155 }
11156 
11157 /* Parse an expression-statement.
11158 
11159    expression-statement:
11160      expression [opt] ;
11161 
11162    Returns the new EXPR_STMT -- or NULL_TREE if the expression
11163    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11164    indicates whether this expression-statement is part of an
11165    expression statement.  */
11166 
11167 static tree
cp_parser_expression_statement(cp_parser * parser,tree in_statement_expr)11168 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11169 {
11170   tree statement = NULL_TREE;
11171   cp_token *token = cp_lexer_peek_token (parser->lexer);
11172   location_t loc = token->location;
11173 
11174   /* There might be attribute fallthrough.  */
11175   tree attr = cp_parser_gnu_attributes_opt (parser);
11176 
11177   /* If the next token is a ';', then there is no expression
11178      statement.  */
11179   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11180     {
11181       statement = cp_parser_expression (parser);
11182       if (statement == error_mark_node
11183 	  && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11184 	{
11185 	  cp_parser_skip_to_end_of_block_or_statement (parser);
11186 	  return error_mark_node;
11187 	}
11188     }
11189 
11190   /* Handle [[fallthrough]];.  */
11191   if (attribute_fallthrough_p (attr))
11192     {
11193       /* The next token after the fallthrough attribute is ';'.  */
11194       if (statement == NULL_TREE)
11195 	/* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
11196 	statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11197 						  void_type_node, 0);
11198       else
11199 	warning_at (loc, OPT_Wattributes,
11200 		    "%<fallthrough%> attribute not followed by %<;%>");
11201       attr = NULL_TREE;
11202     }
11203 
11204   /* Allow "[[fallthrough]];", but warn otherwise.  */
11205   if (attr != NULL_TREE)
11206     warning_at (loc, OPT_Wattributes,
11207 		"attributes at the beginning of statement are ignored");
11208 
11209   /* Give a helpful message for "A<T>::type t;" and the like.  */
11210   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11211       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11212     {
11213       if (TREE_CODE (statement) == SCOPE_REF)
11214 	error_at (token->location, "need %<typename%> before %qE because "
11215 		  "%qT is a dependent scope",
11216 		  statement, TREE_OPERAND (statement, 0));
11217       else if (is_overloaded_fn (statement)
11218 	       && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11219 	{
11220 	  /* A::A a; */
11221 	  tree fn = get_first_fn (statement);
11222 	  error_at (token->location,
11223 		    "%<%T::%D%> names the constructor, not the type",
11224 		    DECL_CONTEXT (fn), DECL_NAME (fn));
11225 	}
11226     }
11227 
11228   /* Consume the final `;'.  */
11229   cp_parser_consume_semicolon_at_end_of_statement (parser);
11230 
11231   if (in_statement_expr
11232       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11233     /* This is the final expression statement of a statement
11234        expression.  */
11235     statement = finish_stmt_expr_expr (statement, in_statement_expr);
11236   else if (statement)
11237     statement = finish_expr_stmt (statement);
11238 
11239   return statement;
11240 }
11241 
11242 /* Parse a compound-statement.
11243 
11244    compound-statement:
11245      { statement-seq [opt] }
11246 
11247    GNU extension:
11248 
11249    compound-statement:
11250      { label-declaration-seq [opt] statement-seq [opt] }
11251 
11252    label-declaration-seq:
11253      label-declaration
11254      label-declaration-seq label-declaration
11255 
11256    Returns a tree representing the statement.  */
11257 
11258 static tree
cp_parser_compound_statement(cp_parser * parser,tree in_statement_expr,int bcs_flags,bool function_body)11259 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11260 			      int bcs_flags, bool function_body)
11261 {
11262   tree compound_stmt;
11263   matching_braces braces;
11264 
11265   /* Consume the `{'.  */
11266   if (!braces.require_open (parser))
11267     return error_mark_node;
11268   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11269       && !function_body && cxx_dialect < cxx14)
11270     pedwarn (input_location, OPT_Wpedantic,
11271 	     "compound-statement in %<constexpr%> function");
11272   /* Begin the compound-statement.  */
11273   compound_stmt = begin_compound_stmt (bcs_flags);
11274   /* If the next keyword is `__label__' we have a label declaration.  */
11275   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11276     cp_parser_label_declaration (parser);
11277   /* Parse an (optional) statement-seq.  */
11278   cp_parser_statement_seq_opt (parser, in_statement_expr);
11279   /* Finish the compound-statement.  */
11280   finish_compound_stmt (compound_stmt);
11281   /* Consume the `}'.  */
11282   braces.require_close (parser);
11283 
11284   return compound_stmt;
11285 }
11286 
11287 /* Parse an (optional) statement-seq.
11288 
11289    statement-seq:
11290      statement
11291      statement-seq [opt] statement  */
11292 
11293 static void
cp_parser_statement_seq_opt(cp_parser * parser,tree in_statement_expr)11294 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11295 {
11296   /* Scan statements until there aren't any more.  */
11297   while (true)
11298     {
11299       cp_token *token = cp_lexer_peek_token (parser->lexer);
11300 
11301       /* If we are looking at a `}', then we have run out of
11302 	 statements; the same is true if we have reached the end
11303 	 of file, or have stumbled upon a stray '@end'.  */
11304       if (token->type == CPP_CLOSE_BRACE
11305 	  || token->type == CPP_EOF
11306 	  || token->type == CPP_PRAGMA_EOL
11307 	  || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11308 	break;
11309 
11310       /* If we are in a compound statement and find 'else' then
11311 	 something went wrong.  */
11312       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11313 	{
11314 	  if (parser->in_statement & IN_IF_STMT)
11315 	    break;
11316 	  else
11317 	    {
11318 	      token = cp_lexer_consume_token (parser->lexer);
11319 	      error_at (token->location, "%<else%> without a previous %<if%>");
11320 	    }
11321 	}
11322 
11323       /* Parse the statement.  */
11324       cp_parser_statement (parser, in_statement_expr, true, NULL);
11325     }
11326 }
11327 
11328 /* Return true if we're looking at (init; cond), false otherwise.  */
11329 
11330 static bool
cp_parser_init_statement_p(cp_parser * parser)11331 cp_parser_init_statement_p (cp_parser *parser)
11332 {
11333   /* Save tokens so that we can put them back.  */
11334   cp_lexer_save_tokens (parser->lexer);
11335 
11336   /* Look for ';' that is not nested in () or {}.  */
11337   int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11338 						     /*recovering=*/false,
11339 						     CPP_SEMICOLON,
11340 						     /*consume_paren=*/false);
11341 
11342   /* Roll back the tokens we skipped.  */
11343   cp_lexer_rollback_tokens (parser->lexer);
11344 
11345   return ret == -1;
11346 }
11347 
11348 /* Parse a selection-statement.
11349 
11350    selection-statement:
11351      if ( init-statement [opt] condition ) statement
11352      if ( init-statement [opt] condition ) statement else statement
11353      switch ( init-statement [opt] condition ) statement
11354 
11355    Returns the new IF_STMT or SWITCH_STMT.
11356 
11357    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11358    is a (possibly labeled) if statement which is not enclosed in
11359    braces and has an else clause.  This is used to implement
11360    -Wparentheses.
11361 
11362    CHAIN is a vector of if-else-if conditions.  This is used to implement
11363    -Wduplicated-cond.  */
11364 
11365 static tree
cp_parser_selection_statement(cp_parser * parser,bool * if_p,vec<tree> * chain)11366 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11367 			       vec<tree> *chain)
11368 {
11369   cp_token *token;
11370   enum rid keyword;
11371   token_indent_info guard_tinfo;
11372 
11373   if (if_p != NULL)
11374     *if_p = false;
11375 
11376   /* Peek at the next token.  */
11377   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11378   guard_tinfo = get_token_indent_info (token);
11379 
11380   /* See what kind of keyword it is.  */
11381   keyword = token->keyword;
11382   switch (keyword)
11383     {
11384     case RID_IF:
11385     case RID_SWITCH:
11386       {
11387 	tree statement;
11388 	tree condition;
11389 
11390 	bool cx = false;
11391 	if (keyword == RID_IF
11392 	    && cp_lexer_next_token_is_keyword (parser->lexer,
11393 					       RID_CONSTEXPR))
11394 	  {
11395 	    cx = true;
11396 	    cp_token *tok = cp_lexer_consume_token (parser->lexer);
11397 	    if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11398 	      pedwarn (tok->location, 0, "%<if constexpr%> only available "
11399 		       "with -std=c++17 or -std=gnu++17");
11400 	  }
11401 
11402 	/* Look for the `('.  */
11403 	matching_parens parens;
11404 	if (!parens.require_open (parser))
11405 	  {
11406 	    cp_parser_skip_to_end_of_statement (parser);
11407 	    return error_mark_node;
11408 	  }
11409 
11410 	/* Begin the selection-statement.  */
11411 	if (keyword == RID_IF)
11412 	  {
11413 	    statement = begin_if_stmt ();
11414 	    IF_STMT_CONSTEXPR_P (statement) = cx;
11415 	  }
11416 	else
11417 	  statement = begin_switch_stmt ();
11418 
11419 	/* Parse the optional init-statement.  */
11420 	if (cp_parser_init_statement_p (parser))
11421 	  {
11422 	    tree decl;
11423 	    if (cxx_dialect < cxx17)
11424 	      pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11425 		       "init-statement in selection statements only available "
11426 		       "with -std=c++17 or -std=gnu++17");
11427 	    cp_parser_init_statement (parser, &decl);
11428 	  }
11429 
11430 	/* Parse the condition.  */
11431 	condition = cp_parser_condition (parser);
11432 	/* Look for the `)'.  */
11433 	if (!parens.require_close (parser))
11434 	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
11435 						 /*consume_paren=*/true);
11436 
11437 	if (keyword == RID_IF)
11438 	  {
11439 	    bool nested_if;
11440 	    unsigned char in_statement;
11441 
11442 	    /* Add the condition.  */
11443 	    condition = finish_if_stmt_cond (condition, statement);
11444 
11445 	    if (warn_duplicated_cond)
11446 	      warn_duplicated_cond_add_or_warn (token->location, condition,
11447 						&chain);
11448 
11449 	    /* Parse the then-clause.  */
11450 	    in_statement = parser->in_statement;
11451 	    parser->in_statement |= IN_IF_STMT;
11452 
11453 	    /* Outside a template, the non-selected branch of a constexpr
11454 	       if is a 'discarded statement', i.e. unevaluated.  */
11455 	    bool was_discarded = in_discarded_stmt;
11456 	    bool discard_then = (cx && !processing_template_decl
11457 				 && integer_zerop (condition));
11458 	    if (discard_then)
11459 	      {
11460 		in_discarded_stmt = true;
11461 		++c_inhibit_evaluation_warnings;
11462 	      }
11463 
11464 	    cp_parser_implicitly_scoped_statement (parser, &nested_if,
11465 						   guard_tinfo);
11466 
11467 	    parser->in_statement = in_statement;
11468 
11469 	    finish_then_clause (statement);
11470 
11471 	    if (discard_then)
11472 	      {
11473 		THEN_CLAUSE (statement) = NULL_TREE;
11474 		in_discarded_stmt = was_discarded;
11475 		--c_inhibit_evaluation_warnings;
11476 	      }
11477 
11478 	    /* If the next token is `else', parse the else-clause.  */
11479 	    if (cp_lexer_next_token_is_keyword (parser->lexer,
11480 						RID_ELSE))
11481 	      {
11482 		bool discard_else = (cx && !processing_template_decl
11483 				     && integer_nonzerop (condition));
11484 		if (discard_else)
11485 		  {
11486 		    in_discarded_stmt = true;
11487 		    ++c_inhibit_evaluation_warnings;
11488 		  }
11489 
11490 		guard_tinfo
11491 		  = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11492 		/* Consume the `else' keyword.  */
11493 		cp_lexer_consume_token (parser->lexer);
11494 		if (warn_duplicated_cond)
11495 		  {
11496 		    if (cp_lexer_next_token_is_keyword (parser->lexer,
11497 							RID_IF)
11498 			&& chain == NULL)
11499 		      {
11500 			/* We've got "if (COND) else if (COND2)".  Start
11501 			   the condition chain and add COND as the first
11502 			   element.  */
11503 			chain = new vec<tree> ();
11504 			if (!CONSTANT_CLASS_P (condition)
11505 			    && !TREE_SIDE_EFFECTS (condition))
11506 			{
11507 			  /* Wrap it in a NOP_EXPR so that we can set the
11508 			     location of the condition.  */
11509 			  tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11510 					   condition);
11511 			  SET_EXPR_LOCATION (e, token->location);
11512 			  chain->safe_push (e);
11513 			}
11514 		      }
11515 		    else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11516 							      RID_IF))
11517 		      {
11518 			/* This is if-else without subsequent if.  Zap the
11519 			   condition chain; we would have already warned at
11520 			   this point.  */
11521 			delete chain;
11522 			chain = NULL;
11523 		      }
11524 		  }
11525 		begin_else_clause (statement);
11526 		/* Parse the else-clause.  */
11527 		cp_parser_implicitly_scoped_statement (parser, NULL,
11528 						       guard_tinfo, chain);
11529 
11530 		finish_else_clause (statement);
11531 
11532 		/* If we are currently parsing a then-clause, then
11533 		   IF_P will not be NULL.  We set it to true to
11534 		   indicate that this if statement has an else clause.
11535 		   This may trigger the Wparentheses warning below
11536 		   when we get back up to the parent if statement.  */
11537 		if (if_p != NULL)
11538 		  *if_p = true;
11539 
11540 		if (discard_else)
11541 		  {
11542 		    ELSE_CLAUSE (statement) = NULL_TREE;
11543 		    in_discarded_stmt = was_discarded;
11544 		    --c_inhibit_evaluation_warnings;
11545 		  }
11546 	      }
11547 	    else
11548 	      {
11549 		/* This if statement does not have an else clause.  If
11550 		   NESTED_IF is true, then the then-clause has an if
11551 		   statement which does have an else clause.  We warn
11552 		   about the potential ambiguity.  */
11553 		if (nested_if)
11554 		  warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11555 			      "suggest explicit braces to avoid ambiguous"
11556 			      " %<else%>");
11557 		if (warn_duplicated_cond)
11558 		  {
11559 		    /* We don't need the condition chain anymore.  */
11560 		    delete chain;
11561 		    chain = NULL;
11562 		  }
11563 	      }
11564 
11565 	    /* Now we're all done with the if-statement.  */
11566 	    finish_if_stmt (statement);
11567 	  }
11568 	else
11569 	  {
11570 	    bool in_switch_statement_p;
11571 	    unsigned char in_statement;
11572 
11573 	    /* Add the condition.  */
11574 	    finish_switch_cond (condition, statement);
11575 
11576 	    /* Parse the body of the switch-statement.  */
11577 	    in_switch_statement_p = parser->in_switch_statement_p;
11578 	    in_statement = parser->in_statement;
11579 	    parser->in_switch_statement_p = true;
11580 	    parser->in_statement |= IN_SWITCH_STMT;
11581 	    cp_parser_implicitly_scoped_statement (parser, if_p,
11582 						   guard_tinfo);
11583 	    parser->in_switch_statement_p = in_switch_statement_p;
11584 	    parser->in_statement = in_statement;
11585 
11586 	    /* Now we're all done with the switch-statement.  */
11587 	    finish_switch_stmt (statement);
11588 	  }
11589 
11590 	return statement;
11591       }
11592       break;
11593 
11594     default:
11595       cp_parser_error (parser, "expected selection-statement");
11596       return error_mark_node;
11597     }
11598 }
11599 
11600 /* Parse a condition.
11601 
11602    condition:
11603      expression
11604      type-specifier-seq declarator = initializer-clause
11605      type-specifier-seq declarator braced-init-list
11606 
11607    GNU Extension:
11608 
11609    condition:
11610      type-specifier-seq declarator asm-specification [opt]
11611        attributes [opt] = assignment-expression
11612 
11613    Returns the expression that should be tested.  */
11614 
11615 static tree
cp_parser_condition(cp_parser * parser)11616 cp_parser_condition (cp_parser* parser)
11617 {
11618   cp_decl_specifier_seq type_specifiers;
11619   const char *saved_message;
11620   int declares_class_or_enum;
11621 
11622   /* Try the declaration first.  */
11623   cp_parser_parse_tentatively (parser);
11624   /* New types are not allowed in the type-specifier-seq for a
11625      condition.  */
11626   saved_message = parser->type_definition_forbidden_message;
11627   parser->type_definition_forbidden_message
11628     = G_("types may not be defined in conditions");
11629   /* Parse the type-specifier-seq.  */
11630   cp_parser_decl_specifier_seq (parser,
11631 				CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11632 				&type_specifiers,
11633 				&declares_class_or_enum);
11634   /* Restore the saved message.  */
11635   parser->type_definition_forbidden_message = saved_message;
11636   /* If all is well, we might be looking at a declaration.  */
11637   if (!cp_parser_error_occurred (parser))
11638     {
11639       tree decl;
11640       tree asm_specification;
11641       tree attributes;
11642       cp_declarator *declarator;
11643       tree initializer = NULL_TREE;
11644 
11645       /* Parse the declarator.  */
11646       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11647 					 /*ctor_dtor_or_conv_p=*/NULL,
11648 					 /*parenthesized_p=*/NULL,
11649 					 /*member_p=*/false,
11650 					 /*friend_p=*/false);
11651       /* Parse the attributes.  */
11652       attributes = cp_parser_attributes_opt (parser);
11653       /* Parse the asm-specification.  */
11654       asm_specification = cp_parser_asm_specification_opt (parser);
11655       /* If the next token is not an `=' or '{', then we might still be
11656 	 looking at an expression.  For example:
11657 
11658 	   if (A(a).x)
11659 
11660 	 looks like a decl-specifier-seq and a declarator -- but then
11661 	 there is no `=', so this is an expression.  */
11662       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11663 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11664 	cp_parser_simulate_error (parser);
11665 
11666       /* If we did see an `=' or '{', then we are looking at a declaration
11667 	 for sure.  */
11668       if (cp_parser_parse_definitely (parser))
11669 	{
11670 	  tree pushed_scope;
11671 	  bool non_constant_p;
11672 	  int flags = LOOKUP_ONLYCONVERTING;
11673 
11674 	  /* Create the declaration.  */
11675 	  decl = start_decl (declarator, &type_specifiers,
11676 			     /*initialized_p=*/true,
11677 			     attributes, /*prefix_attributes=*/NULL_TREE,
11678 			     &pushed_scope);
11679 
11680 	  /* Parse the initializer.  */
11681 	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11682 	    {
11683 	      initializer = cp_parser_braced_list (parser, &non_constant_p);
11684 	      CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11685 	      flags = 0;
11686 	    }
11687 	  else
11688 	    {
11689 	      /* Consume the `='.  */
11690 	      cp_parser_require (parser, CPP_EQ, RT_EQ);
11691 	      initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11692 	    }
11693 	  if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11694 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11695 
11696 	  /* Process the initializer.  */
11697 	  cp_finish_decl (decl,
11698 			  initializer, !non_constant_p,
11699 			  asm_specification,
11700 			  flags);
11701 
11702 	  if (pushed_scope)
11703 	    pop_scope (pushed_scope);
11704 
11705 	  return convert_from_reference (decl);
11706 	}
11707     }
11708   /* If we didn't even get past the declarator successfully, we are
11709      definitely not looking at a declaration.  */
11710   else
11711     cp_parser_abort_tentative_parse (parser);
11712 
11713   /* Otherwise, we are looking at an expression.  */
11714   return cp_parser_expression (parser);
11715 }
11716 
11717 /* Parses a for-statement or range-for-statement until the closing ')',
11718    not included. */
11719 
11720 static tree
cp_parser_for(cp_parser * parser,bool ivdep,unsigned short unroll)11721 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11722 {
11723   tree init, scope, decl;
11724   bool is_range_for;
11725 
11726   /* Begin the for-statement.  */
11727   scope = begin_for_scope (&init);
11728 
11729   /* Parse the initialization.  */
11730   is_range_for = cp_parser_init_statement (parser, &decl);
11731 
11732   if (is_range_for)
11733     return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11734   else
11735     return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11736 }
11737 
11738 static tree
cp_parser_c_for(cp_parser * parser,tree scope,tree init,bool ivdep,unsigned short unroll)11739 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11740 		 unsigned short unroll)
11741 {
11742   /* Normal for loop */
11743   tree condition = NULL_TREE;
11744   tree expression = NULL_TREE;
11745   tree stmt;
11746 
11747   stmt = begin_for_stmt (scope, init);
11748   /* The init-statement has already been parsed in
11749      cp_parser_init_statement, so no work is needed here.  */
11750   finish_init_stmt (stmt);
11751 
11752   /* If there's a condition, process it.  */
11753   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11754     condition = cp_parser_condition (parser);
11755   else if (ivdep)
11756     {
11757       cp_parser_error (parser, "missing loop condition in loop with "
11758 		       "%<GCC ivdep%> pragma");
11759       condition = error_mark_node;
11760     }
11761   else if (unroll)
11762     {
11763       cp_parser_error (parser, "missing loop condition in loop with "
11764 		       "%<GCC unroll%> pragma");
11765       condition = error_mark_node;
11766     }
11767   finish_for_cond (condition, stmt, ivdep, unroll);
11768   /* Look for the `;'.  */
11769   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11770 
11771   /* If there's an expression, process it.  */
11772   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11773     expression = cp_parser_expression (parser);
11774   finish_for_expr (expression, stmt);
11775 
11776   return stmt;
11777 }
11778 
11779 /* Tries to parse a range-based for-statement:
11780 
11781   range-based-for:
11782     decl-specifier-seq declarator : expression
11783 
11784   The decl-specifier-seq declarator and the `:' are already parsed by
11785   cp_parser_init_statement.  If processing_template_decl it returns a
11786   newly created RANGE_FOR_STMT; if not, it is converted to a
11787   regular FOR_STMT.  */
11788 
11789 static tree
cp_parser_range_for(cp_parser * parser,tree scope,tree init,tree range_decl,bool ivdep,unsigned short unroll)11790 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11791 		     bool ivdep, unsigned short unroll)
11792 {
11793   tree stmt, range_expr;
11794   auto_vec <cxx_binding *, 16> bindings;
11795   auto_vec <tree, 16> names;
11796   tree decomp_first_name = NULL_TREE;
11797   unsigned int decomp_cnt = 0;
11798 
11799   /* Get the range declaration momentarily out of the way so that
11800      the range expression doesn't clash with it. */
11801   if (range_decl != error_mark_node)
11802     {
11803       if (DECL_HAS_VALUE_EXPR_P (range_decl))
11804 	{
11805 	  tree v = DECL_VALUE_EXPR (range_decl);
11806 	  /* For decomposition declaration get all of the corresponding
11807 	     declarations out of the way.  */
11808 	  if (TREE_CODE (v) == ARRAY_REF
11809 	      && VAR_P (TREE_OPERAND (v, 0))
11810 	      && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11811 	    {
11812 	      tree d = range_decl;
11813 	      range_decl = TREE_OPERAND (v, 0);
11814 	      decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11815 	      decomp_first_name = d;
11816 	      for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11817 		{
11818 		  tree name = DECL_NAME (d);
11819 		  names.safe_push (name);
11820 		  bindings.safe_push (IDENTIFIER_BINDING (name));
11821 		  IDENTIFIER_BINDING (name)
11822 		    = IDENTIFIER_BINDING (name)->previous;
11823 		}
11824 	    }
11825 	}
11826       if (names.is_empty ())
11827 	{
11828 	  tree name = DECL_NAME (range_decl);
11829 	  names.safe_push (name);
11830 	  bindings.safe_push (IDENTIFIER_BINDING (name));
11831 	  IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11832 	}
11833     }
11834 
11835   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11836     {
11837       bool expr_non_constant_p;
11838       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11839     }
11840   else
11841     range_expr = cp_parser_expression (parser);
11842 
11843   /* Put the range declaration(s) back into scope. */
11844   for (unsigned int i = 0; i < names.length (); i++)
11845     {
11846       cxx_binding *binding = bindings[i];
11847       binding->previous = IDENTIFIER_BINDING (names[i]);
11848       IDENTIFIER_BINDING (names[i]) = binding;
11849     }
11850 
11851   /* If in template, STMT is converted to a normal for-statement
11852      at instantiation. If not, it is done just ahead. */
11853   if (processing_template_decl)
11854     {
11855       if (check_for_bare_parameter_packs (range_expr))
11856 	range_expr = error_mark_node;
11857       stmt = begin_range_for_stmt (scope, init);
11858       if (ivdep)
11859 	RANGE_FOR_IVDEP (stmt) = 1;
11860       if (unroll)
11861 	RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11862       finish_range_for_decl (stmt, range_decl, range_expr);
11863       if (!type_dependent_expression_p (range_expr)
11864 	  /* do_auto_deduction doesn't mess with template init-lists.  */
11865 	  && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11866 	do_range_for_auto_deduction (range_decl, range_expr);
11867     }
11868   else
11869     {
11870       stmt = begin_for_stmt (scope, init);
11871       stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11872 				   decomp_first_name, decomp_cnt, ivdep,
11873 				   unroll);
11874     }
11875   return stmt;
11876 }
11877 
11878 /* Subroutine of cp_convert_range_for: given the initializer expression,
11879    builds up the range temporary.  */
11880 
11881 static tree
build_range_temp(tree range_expr)11882 build_range_temp (tree range_expr)
11883 {
11884   tree range_type, range_temp;
11885 
11886   /* Find out the type deduced by the declaration
11887      `auto &&__range = range_expr'.  */
11888   range_type = cp_build_reference_type (make_auto (), true);
11889   range_type = do_auto_deduction (range_type, range_expr,
11890 				  type_uses_auto (range_type));
11891 
11892   /* Create the __range variable.  */
11893   range_temp = build_decl (input_location, VAR_DECL,
11894 			   get_identifier ("__for_range"), range_type);
11895   TREE_USED (range_temp) = 1;
11896   DECL_ARTIFICIAL (range_temp) = 1;
11897 
11898   return range_temp;
11899 }
11900 
11901 /* Used by cp_parser_range_for in template context: we aren't going to
11902    do a full conversion yet, but we still need to resolve auto in the
11903    type of the for-range-declaration if present.  This is basically
11904    a shortcut version of cp_convert_range_for.  */
11905 
11906 static void
do_range_for_auto_deduction(tree decl,tree range_expr)11907 do_range_for_auto_deduction (tree decl, tree range_expr)
11908 {
11909   tree auto_node = type_uses_auto (TREE_TYPE (decl));
11910   if (auto_node)
11911     {
11912       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11913       range_temp = convert_from_reference (build_range_temp (range_expr));
11914       iter_type = (cp_parser_perform_range_for_lookup
11915 		   (range_temp, &begin_dummy, &end_dummy));
11916       if (iter_type)
11917 	{
11918 	  iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11919 				  iter_type);
11920 	  iter_decl = build_x_indirect_ref (input_location, iter_decl,
11921 					    RO_UNARY_STAR,
11922 					    tf_warning_or_error);
11923 	  TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11924 						iter_decl, auto_node);
11925 	}
11926     }
11927 }
11928 
11929 /* Converts a range-based for-statement into a normal
11930    for-statement, as per the definition.
11931 
11932       for (RANGE_DECL : RANGE_EXPR)
11933 	BLOCK
11934 
11935    should be equivalent to:
11936 
11937       {
11938 	auto &&__range = RANGE_EXPR;
11939 	for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11940 	      __begin != __end;
11941 	      ++__begin)
11942 	  {
11943 	      RANGE_DECL = *__begin;
11944 	      BLOCK
11945 	  }
11946       }
11947 
11948    If RANGE_EXPR is an array:
11949 	BEGIN_EXPR = __range
11950 	END_EXPR = __range + ARRAY_SIZE(__range)
11951    Else if RANGE_EXPR has a member 'begin' or 'end':
11952 	BEGIN_EXPR = __range.begin()
11953 	END_EXPR = __range.end()
11954    Else:
11955 	BEGIN_EXPR = begin(__range)
11956 	END_EXPR = end(__range);
11957 
11958    If __range has a member 'begin' but not 'end', or vice versa, we must
11959    still use the second alternative (it will surely fail, however).
11960    When calling begin()/end() in the third alternative we must use
11961    argument dependent lookup, but always considering 'std' as an associated
11962    namespace.  */
11963 
11964 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)11965 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11966 		      tree decomp_first_name, unsigned int decomp_cnt,
11967 		      bool ivdep, unsigned short unroll)
11968 {
11969   tree begin, end;
11970   tree iter_type, begin_expr, end_expr;
11971   tree condition, expression;
11972 
11973   range_expr = mark_lvalue_use (range_expr);
11974 
11975   if (range_decl == error_mark_node || range_expr == error_mark_node)
11976     /* If an error happened previously do nothing or else a lot of
11977        unhelpful errors would be issued.  */
11978     begin_expr = end_expr = iter_type = error_mark_node;
11979   else
11980     {
11981       tree range_temp;
11982 
11983       if (VAR_P (range_expr)
11984 	  && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11985 	/* Can't bind a reference to an array of runtime bound.  */
11986 	range_temp = range_expr;
11987       else
11988 	{
11989 	  range_temp = build_range_temp (range_expr);
11990 	  pushdecl (range_temp);
11991 	  cp_finish_decl (range_temp, range_expr,
11992 			  /*is_constant_init*/false, NULL_TREE,
11993 			  LOOKUP_ONLYCONVERTING);
11994 	  range_temp = convert_from_reference (range_temp);
11995 	}
11996       iter_type = cp_parser_perform_range_for_lookup (range_temp,
11997 						      &begin_expr, &end_expr);
11998     }
11999 
12000   /* The new for initialization statement.  */
12001   begin = build_decl (input_location, VAR_DECL,
12002 		      get_identifier ("__for_begin"), iter_type);
12003   TREE_USED (begin) = 1;
12004   DECL_ARTIFICIAL (begin) = 1;
12005   pushdecl (begin);
12006   cp_finish_decl (begin, begin_expr,
12007 		  /*is_constant_init*/false, NULL_TREE,
12008 		  LOOKUP_ONLYCONVERTING);
12009 
12010   if (cxx_dialect >= cxx17)
12011     iter_type = cv_unqualified (TREE_TYPE (end_expr));
12012   end = build_decl (input_location, VAR_DECL,
12013 		    get_identifier ("__for_end"), iter_type);
12014   TREE_USED (end) = 1;
12015   DECL_ARTIFICIAL (end) = 1;
12016   pushdecl (end);
12017   cp_finish_decl (end, end_expr,
12018 		  /*is_constant_init*/false, NULL_TREE,
12019 		  LOOKUP_ONLYCONVERTING);
12020 
12021   finish_init_stmt (statement);
12022 
12023   /* The new for condition.  */
12024   condition = build_x_binary_op (input_location, NE_EXPR,
12025 				 begin, ERROR_MARK,
12026 				 end, ERROR_MARK,
12027 				 NULL, tf_warning_or_error);
12028   finish_for_cond (condition, statement, ivdep, unroll);
12029 
12030   /* The new increment expression.  */
12031   expression = finish_unary_op_expr (input_location,
12032 				     PREINCREMENT_EXPR, begin,
12033 				     tf_warning_or_error);
12034   finish_for_expr (expression, statement);
12035 
12036   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12037     cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12038 
12039   /* The declaration is initialized with *__begin inside the loop body.  */
12040   cp_finish_decl (range_decl,
12041 		  build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12042 					tf_warning_or_error),
12043 		  /*is_constant_init*/false, NULL_TREE,
12044 		  LOOKUP_ONLYCONVERTING);
12045   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12046     cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12047 
12048   return statement;
12049 }
12050 
12051 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12052    We need to solve both at the same time because the method used
12053    depends on the existence of members begin or end.
12054    Returns the type deduced for the iterator expression.  */
12055 
12056 static tree
cp_parser_perform_range_for_lookup(tree range,tree * begin,tree * end)12057 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12058 {
12059   if (error_operand_p (range))
12060     {
12061       *begin = *end = error_mark_node;
12062       return error_mark_node;
12063     }
12064 
12065   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12066     {
12067       error ("range-based %<for%> expression of type %qT "
12068 	     "has incomplete type", TREE_TYPE (range));
12069       *begin = *end = error_mark_node;
12070       return error_mark_node;
12071     }
12072   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12073     {
12074       /* If RANGE is an array, we will use pointer arithmetic.  */
12075       *begin = decay_conversion (range, tf_warning_or_error);
12076       *end = build_binary_op (input_location, PLUS_EXPR,
12077 			      range,
12078 			      array_type_nelts_top (TREE_TYPE (range)),
12079 			      false);
12080       return TREE_TYPE (*begin);
12081     }
12082   else
12083     {
12084       /* If it is not an array, we must do a bit of magic.  */
12085       tree id_begin, id_end;
12086       tree member_begin, member_end;
12087 
12088       *begin = *end = error_mark_node;
12089 
12090       id_begin = get_identifier ("begin");
12091       id_end = get_identifier ("end");
12092       member_begin = lookup_member (TREE_TYPE (range), id_begin,
12093 				    /*protect=*/2, /*want_type=*/false,
12094 				    tf_warning_or_error);
12095       member_end = lookup_member (TREE_TYPE (range), id_end,
12096 				  /*protect=*/2, /*want_type=*/false,
12097 				  tf_warning_or_error);
12098 
12099       if (member_begin != NULL_TREE && member_end != NULL_TREE)
12100 	{
12101 	  /* Use the member functions.  */
12102 	  *begin = cp_parser_range_for_member_function (range, id_begin);
12103 	  *end = cp_parser_range_for_member_function (range, id_end);
12104 	}
12105       else
12106 	{
12107 	  /* Use global functions with ADL.  */
12108 	  vec<tree, va_gc> *vec;
12109 	  vec = make_tree_vector ();
12110 
12111 	  vec_safe_push (vec, range);
12112 
12113 	  member_begin = perform_koenig_lookup (id_begin, vec,
12114 						tf_warning_or_error);
12115 	  *begin = finish_call_expr (member_begin, &vec, false, true,
12116 				     tf_warning_or_error);
12117 	  member_end = perform_koenig_lookup (id_end, vec,
12118 					      tf_warning_or_error);
12119 	  *end = finish_call_expr (member_end, &vec, false, true,
12120 				   tf_warning_or_error);
12121 
12122 	  release_tree_vector (vec);
12123 	}
12124 
12125       /* Last common checks.  */
12126       if (*begin == error_mark_node || *end == error_mark_node)
12127 	{
12128 	  /* If one of the expressions is an error do no more checks.  */
12129 	  *begin = *end = error_mark_node;
12130 	  return error_mark_node;
12131 	}
12132       else if (type_dependent_expression_p (*begin)
12133 	       || type_dependent_expression_p (*end))
12134 	/* Can happen, when, eg, in a template context, Koenig lookup
12135 	   can't resolve begin/end (c++/58503).  */
12136 	return NULL_TREE;
12137       else
12138 	{
12139 	  tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12140 	  /* The unqualified type of the __begin and __end temporaries should
12141 	     be the same, as required by the multiple auto declaration.  */
12142 	  if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12143 	    {
12144 	      if (cxx_dialect >= cxx17
12145 		  && (build_x_binary_op (input_location, NE_EXPR,
12146 					 *begin, ERROR_MARK,
12147 					 *end, ERROR_MARK,
12148 					 NULL, tf_none)
12149 		      != error_mark_node))
12150 		/* P0184R0 allows __begin and __end to have different types,
12151 		   but make sure they are comparable so we can give a better
12152 		   diagnostic.  */;
12153 	      else
12154 		error ("inconsistent begin/end types in range-based %<for%> "
12155 		       "statement: %qT and %qT",
12156 		       TREE_TYPE (*begin), TREE_TYPE (*end));
12157 	    }
12158 	  return iter_type;
12159 	}
12160     }
12161 }
12162 
12163 /* Helper function for cp_parser_perform_range_for_lookup.
12164    Builds a tree for RANGE.IDENTIFIER().  */
12165 
12166 static tree
cp_parser_range_for_member_function(tree range,tree identifier)12167 cp_parser_range_for_member_function (tree range, tree identifier)
12168 {
12169   tree member, res;
12170   vec<tree, va_gc> *vec;
12171 
12172   member = finish_class_member_access_expr (range, identifier,
12173 					    false, tf_warning_or_error);
12174   if (member == error_mark_node)
12175     return error_mark_node;
12176 
12177   vec = make_tree_vector ();
12178   res = finish_call_expr (member, &vec,
12179 			  /*disallow_virtual=*/false,
12180 			  /*koenig_p=*/false,
12181 			  tf_warning_or_error);
12182   release_tree_vector (vec);
12183   return res;
12184 }
12185 
12186 /* Parse an iteration-statement.
12187 
12188    iteration-statement:
12189      while ( condition ) statement
12190      do statement while ( expression ) ;
12191      for ( init-statement condition [opt] ; expression [opt] )
12192        statement
12193 
12194    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
12195 
12196 static tree
cp_parser_iteration_statement(cp_parser * parser,bool * if_p,bool ivdep,unsigned short unroll)12197 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12198 			       unsigned short unroll)
12199 {
12200   cp_token *token;
12201   enum rid keyword;
12202   tree statement;
12203   unsigned char in_statement;
12204   token_indent_info guard_tinfo;
12205 
12206   /* Peek at the next token.  */
12207   token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12208   if (!token)
12209     return error_mark_node;
12210 
12211   guard_tinfo = get_token_indent_info (token);
12212 
12213   /* Remember whether or not we are already within an iteration
12214      statement.  */
12215   in_statement = parser->in_statement;
12216 
12217   /* See what kind of keyword it is.  */
12218   keyword = token->keyword;
12219   switch (keyword)
12220     {
12221     case RID_WHILE:
12222       {
12223 	tree condition;
12224 
12225 	/* Begin the while-statement.  */
12226 	statement = begin_while_stmt ();
12227 	/* Look for the `('.  */
12228 	matching_parens parens;
12229 	parens.require_open (parser);
12230 	/* Parse the condition.  */
12231 	condition = cp_parser_condition (parser);
12232 	finish_while_stmt_cond (condition, statement, ivdep, unroll);
12233 	/* Look for the `)'.  */
12234 	parens.require_close (parser);
12235 	/* Parse the dependent statement.  */
12236 	parser->in_statement = IN_ITERATION_STMT;
12237 	bool prev = note_iteration_stmt_body_start ();
12238 	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12239 	note_iteration_stmt_body_end (prev);
12240 	parser->in_statement = in_statement;
12241 	/* We're done with the while-statement.  */
12242 	finish_while_stmt (statement);
12243       }
12244       break;
12245 
12246     case RID_DO:
12247       {
12248 	tree expression;
12249 
12250 	/* Begin the do-statement.  */
12251 	statement = begin_do_stmt ();
12252 	/* Parse the body of the do-statement.  */
12253 	parser->in_statement = IN_ITERATION_STMT;
12254 	bool prev = note_iteration_stmt_body_start ();
12255 	cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12256 	note_iteration_stmt_body_end (prev);
12257 	parser->in_statement = in_statement;
12258 	finish_do_body (statement);
12259 	/* Look for the `while' keyword.  */
12260 	cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12261 	/* Look for the `('.  */
12262 	matching_parens parens;
12263 	parens.require_open (parser);
12264 	/* Parse the expression.  */
12265 	expression = cp_parser_expression (parser);
12266 	/* We're done with the do-statement.  */
12267 	finish_do_stmt (expression, statement, ivdep, unroll);
12268 	/* Look for the `)'.  */
12269 	parens.require_close (parser);
12270 	/* Look for the `;'.  */
12271 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12272       }
12273       break;
12274 
12275     case RID_FOR:
12276       {
12277 	/* Look for the `('.  */
12278 	matching_parens parens;
12279 	parens.require_open (parser);
12280 
12281 	statement = cp_parser_for (parser, ivdep, unroll);
12282 
12283 	/* Look for the `)'.  */
12284 	parens.require_close (parser);
12285 
12286 	/* Parse the body of the for-statement.  */
12287 	parser->in_statement = IN_ITERATION_STMT;
12288 	bool prev = note_iteration_stmt_body_start ();
12289 	cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12290 	note_iteration_stmt_body_end (prev);
12291 	parser->in_statement = in_statement;
12292 
12293 	/* We're done with the for-statement.  */
12294 	finish_for_stmt (statement);
12295       }
12296       break;
12297 
12298     default:
12299       cp_parser_error (parser, "expected iteration-statement");
12300       statement = error_mark_node;
12301       break;
12302     }
12303 
12304   return statement;
12305 }
12306 
12307 /* Parse a init-statement or the declarator of a range-based-for.
12308    Returns true if a range-based-for declaration is seen.
12309 
12310    init-statement:
12311      expression-statement
12312      simple-declaration  */
12313 
12314 static bool
cp_parser_init_statement(cp_parser * parser,tree * decl)12315 cp_parser_init_statement (cp_parser* parser, tree *decl)
12316 {
12317   /* If the next token is a `;', then we have an empty
12318      expression-statement.  Grammatically, this is also a
12319      simple-declaration, but an invalid one, because it does not
12320      declare anything.  Therefore, if we did not handle this case
12321      specially, we would issue an error message about an invalid
12322      declaration.  */
12323   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12324     {
12325       bool is_range_for = false;
12326       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12327 
12328       /* A colon is used in range-based for.  */
12329       parser->colon_corrects_to_scope_p = false;
12330 
12331       /* We're going to speculatively look for a declaration, falling back
12332 	 to an expression, if necessary.  */
12333       cp_parser_parse_tentatively (parser);
12334       /* Parse the declaration.  */
12335       cp_parser_simple_declaration (parser,
12336 				    /*function_definition_allowed_p=*/false,
12337 				    decl);
12338       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12339       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12340 	{
12341 	  /* It is a range-for, consume the ':' */
12342 	  cp_lexer_consume_token (parser->lexer);
12343 	  is_range_for = true;
12344 	  if (cxx_dialect < cxx11)
12345 	    pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12346 		     "range-based %<for%> loops only available with "
12347 		     "-std=c++11 or -std=gnu++11");
12348 	}
12349       else
12350 	  /* The ';' is not consumed yet because we told
12351 	     cp_parser_simple_declaration not to.  */
12352 	  cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12353 
12354       if (cp_parser_parse_definitely (parser))
12355 	return is_range_for;
12356       /* If the tentative parse failed, then we shall need to look for an
12357 	 expression-statement.  */
12358     }
12359   /* If we are here, it is an expression-statement.  */
12360   cp_parser_expression_statement (parser, NULL_TREE);
12361   return false;
12362 }
12363 
12364 /* Parse a jump-statement.
12365 
12366    jump-statement:
12367      break ;
12368      continue ;
12369      return expression [opt] ;
12370      return braced-init-list ;
12371      goto identifier ;
12372 
12373    GNU extension:
12374 
12375    jump-statement:
12376      goto * expression ;
12377 
12378    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
12379 
12380 static tree
cp_parser_jump_statement(cp_parser * parser)12381 cp_parser_jump_statement (cp_parser* parser)
12382 {
12383   tree statement = error_mark_node;
12384   cp_token *token;
12385   enum rid keyword;
12386   unsigned char in_statement;
12387 
12388   /* Peek at the next token.  */
12389   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12390   if (!token)
12391     return error_mark_node;
12392 
12393   /* See what kind of keyword it is.  */
12394   keyword = token->keyword;
12395   switch (keyword)
12396     {
12397     case RID_BREAK:
12398       in_statement = parser->in_statement & ~IN_IF_STMT;
12399       switch (in_statement)
12400 	{
12401 	case 0:
12402 	  error_at (token->location, "break statement not within loop or switch");
12403 	  break;
12404 	default:
12405 	  gcc_assert ((in_statement & IN_SWITCH_STMT)
12406 		      || in_statement == IN_ITERATION_STMT);
12407 	  statement = finish_break_stmt ();
12408 	  if (in_statement == IN_ITERATION_STMT)
12409 	    break_maybe_infinite_loop ();
12410 	  break;
12411 	case IN_OMP_BLOCK:
12412 	  error_at (token->location, "invalid exit from OpenMP structured block");
12413 	  break;
12414 	case IN_OMP_FOR:
12415 	  error_at (token->location, "break statement used with OpenMP for loop");
12416 	  break;
12417 	}
12418       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12419       break;
12420 
12421     case RID_CONTINUE:
12422       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12423 	{
12424 	case 0:
12425 	  error_at (token->location, "continue statement not within a loop");
12426 	  break;
12427 	  /* Fall through.  */
12428 	case IN_ITERATION_STMT:
12429 	case IN_OMP_FOR:
12430 	  statement = finish_continue_stmt ();
12431 	  break;
12432 	case IN_OMP_BLOCK:
12433 	  error_at (token->location, "invalid exit from OpenMP structured block");
12434 	  break;
12435 	default:
12436 	  gcc_unreachable ();
12437 	}
12438       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12439       break;
12440 
12441     case RID_RETURN:
12442       {
12443 	tree expr;
12444 	bool expr_non_constant_p;
12445 
12446 	if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12447 	  {
12448 	    cp_lexer_set_source_position (parser->lexer);
12449 	    maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12450 	    expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12451 	  }
12452 	else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12453 	  expr = cp_parser_expression (parser);
12454 	else
12455 	  /* If the next token is a `;', then there is no
12456 	     expression.  */
12457 	  expr = NULL_TREE;
12458 	/* Build the return-statement.  */
12459 	if (current_function_auto_return_pattern && in_discarded_stmt)
12460 	  /* Don't deduce from a discarded return statement.  */;
12461 	else
12462 	  statement = finish_return_stmt (expr);
12463 	/* Look for the final `;'.  */
12464 	cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12465       }
12466       break;
12467 
12468     case RID_GOTO:
12469       if (parser->in_function_body
12470 	  && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12471 	{
12472 	  error ("%<goto%> in %<constexpr%> function");
12473 	  cp_function_chain->invalid_constexpr = true;
12474 	}
12475 
12476       /* Create the goto-statement.  */
12477       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12478 	{
12479 	  /* Issue a warning about this use of a GNU extension.  */
12480 	  pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12481 	  /* Consume the '*' token.  */
12482 	  cp_lexer_consume_token (parser->lexer);
12483 	  /* Parse the dependent expression.  */
12484 	  finish_goto_stmt (cp_parser_expression (parser));
12485 	}
12486       else
12487 	finish_goto_stmt (cp_parser_identifier (parser));
12488       /* Look for the final `;'.  */
12489       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12490       break;
12491 
12492     default:
12493       cp_parser_error (parser, "expected jump-statement");
12494       break;
12495     }
12496 
12497   return statement;
12498 }
12499 
12500 /* Parse a declaration-statement.
12501 
12502    declaration-statement:
12503      block-declaration  */
12504 
12505 static void
cp_parser_declaration_statement(cp_parser * parser)12506 cp_parser_declaration_statement (cp_parser* parser)
12507 {
12508   void *p;
12509 
12510   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12511   p = obstack_alloc (&declarator_obstack, 0);
12512 
12513  /* Parse the block-declaration.  */
12514   cp_parser_block_declaration (parser, /*statement_p=*/true);
12515 
12516   /* Free any declarators allocated.  */
12517   obstack_free (&declarator_obstack, p);
12518 }
12519 
12520 /* Some dependent statements (like `if (cond) statement'), are
12521    implicitly in their own scope.  In other words, if the statement is
12522    a single statement (as opposed to a compound-statement), it is
12523    none-the-less treated as if it were enclosed in braces.  Any
12524    declarations appearing in the dependent statement are out of scope
12525    after control passes that point.  This function parses a statement,
12526    but ensures that is in its own scope, even if it is not a
12527    compound-statement.
12528 
12529    If IF_P is not NULL, *IF_P is set to indicate whether the statement
12530    is a (possibly labeled) if statement which is not enclosed in
12531    braces and has an else clause.  This is used to implement
12532    -Wparentheses.
12533 
12534    CHAIN is a vector of if-else-if conditions.  This is used to implement
12535    -Wduplicated-cond.
12536 
12537    Returns the new statement.  */
12538 
12539 static tree
cp_parser_implicitly_scoped_statement(cp_parser * parser,bool * if_p,const token_indent_info & guard_tinfo,vec<tree> * chain)12540 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12541 				       const token_indent_info &guard_tinfo,
12542 				       vec<tree> *chain)
12543 {
12544   tree statement;
12545   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12546   location_t body_loc_after_labels = UNKNOWN_LOCATION;
12547   token_indent_info body_tinfo
12548     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12549 
12550   if (if_p != NULL)
12551     *if_p = false;
12552 
12553   /* Mark if () ; with a special NOP_EXPR.  */
12554   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12555     {
12556       cp_lexer_consume_token (parser->lexer);
12557       statement = add_stmt (build_empty_stmt (body_loc));
12558 
12559       if (guard_tinfo.keyword == RID_IF
12560 	  && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12561 	warning_at (body_loc, OPT_Wempty_body,
12562 		    "suggest braces around empty body in an %<if%> statement");
12563       else if (guard_tinfo.keyword == RID_ELSE)
12564 	warning_at (body_loc, OPT_Wempty_body,
12565 		    "suggest braces around empty body in an %<else%> statement");
12566     }
12567   /* if a compound is opened, we simply parse the statement directly.  */
12568   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12569     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12570   /* If the token is not a `{', then we must take special action.  */
12571   else
12572     {
12573       /* Create a compound-statement.  */
12574       statement = begin_compound_stmt (0);
12575       /* Parse the dependent-statement.  */
12576       cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12577 			   &body_loc_after_labels);
12578       /* Finish the dummy compound-statement.  */
12579       finish_compound_stmt (statement);
12580     }
12581 
12582   token_indent_info next_tinfo
12583     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12584   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12585 
12586   if (body_loc_after_labels != UNKNOWN_LOCATION
12587       && next_tinfo.type != CPP_SEMICOLON)
12588     warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12589 				    guard_tinfo.location, guard_tinfo.keyword);
12590 
12591   /* Return the statement.  */
12592   return statement;
12593 }
12594 
12595 /* For some dependent statements (like `while (cond) statement'), we
12596    have already created a scope.  Therefore, even if the dependent
12597    statement is a compound-statement, we do not want to create another
12598    scope.  */
12599 
12600 static void
cp_parser_already_scoped_statement(cp_parser * parser,bool * if_p,const token_indent_info & guard_tinfo)12601 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12602 				    const token_indent_info &guard_tinfo)
12603 {
12604   /* If the token is a `{', then we must take special action.  */
12605   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12606     {
12607       token_indent_info body_tinfo
12608 	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12609       location_t loc_after_labels = UNKNOWN_LOCATION;
12610 
12611       cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12612 			   &loc_after_labels);
12613       token_indent_info next_tinfo
12614 	= get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12615       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12616 
12617       if (loc_after_labels != UNKNOWN_LOCATION
12618 	  && next_tinfo.type != CPP_SEMICOLON)
12619 	warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12620 					guard_tinfo.location,
12621 					guard_tinfo.keyword);
12622     }
12623   else
12624     {
12625       /* Avoid calling cp_parser_compound_statement, so that we
12626 	 don't create a new scope.  Do everything else by hand.  */
12627       matching_braces braces;
12628       braces.require_open (parser);
12629       /* If the next keyword is `__label__' we have a label declaration.  */
12630       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12631 	cp_parser_label_declaration (parser);
12632       /* Parse an (optional) statement-seq.  */
12633       cp_parser_statement_seq_opt (parser, NULL_TREE);
12634       braces.require_close (parser);
12635     }
12636 }
12637 
12638 /* Declarations [gram.dcl.dcl] */
12639 
12640 /* Parse an optional declaration-sequence.
12641 
12642    declaration-seq:
12643      declaration
12644      declaration-seq declaration  */
12645 
12646 static void
cp_parser_declaration_seq_opt(cp_parser * parser)12647 cp_parser_declaration_seq_opt (cp_parser* parser)
12648 {
12649   while (true)
12650     {
12651       cp_token *token;
12652 
12653       token = cp_lexer_peek_token (parser->lexer);
12654 
12655       if (token->type == CPP_CLOSE_BRACE
12656 	  || token->type == CPP_EOF
12657 	  || token->type == CPP_PRAGMA_EOL)
12658 	break;
12659 
12660       if (token->type == CPP_SEMICOLON)
12661 	{
12662 	  /* A declaration consisting of a single semicolon is
12663 	     invalid.  Allow it unless we're being pedantic.  */
12664 	  cp_lexer_consume_token (parser->lexer);
12665 	  if (!in_system_header_at (input_location))
12666 	    pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12667 	  continue;
12668 	}
12669 
12670       /* If we're entering or exiting a region that's implicitly
12671 	 extern "C", modify the lang context appropriately.  */
12672       if (!parser->implicit_extern_c && token->implicit_extern_c)
12673 	{
12674 	  push_lang_context (lang_name_c);
12675 	  parser->implicit_extern_c = true;
12676 	}
12677       else if (parser->implicit_extern_c && !token->implicit_extern_c)
12678 	{
12679 	  pop_lang_context ();
12680 	  parser->implicit_extern_c = false;
12681 	}
12682 
12683       if (token->type == CPP_PRAGMA)
12684 	{
12685 	  /* A top-level declaration can consist solely of a #pragma.
12686 	     A nested declaration cannot, so this is done here and not
12687 	     in cp_parser_declaration.  (A #pragma at block scope is
12688 	     handled in cp_parser_statement.)  */
12689 	  cp_parser_pragma (parser, pragma_external, NULL);
12690 	  continue;
12691 	}
12692 
12693       /* Parse the declaration itself.  */
12694       cp_parser_declaration (parser);
12695     }
12696 }
12697 
12698 /* Parse a declaration.
12699 
12700    declaration:
12701      block-declaration
12702      function-definition
12703      template-declaration
12704      explicit-instantiation
12705      explicit-specialization
12706      linkage-specification
12707      namespace-definition
12708 
12709    C++17:
12710      deduction-guide
12711 
12712    GNU extension:
12713 
12714    declaration:
12715       __extension__ declaration */
12716 
12717 static void
cp_parser_declaration(cp_parser * parser)12718 cp_parser_declaration (cp_parser* parser)
12719 {
12720   cp_token token1;
12721   cp_token token2;
12722   int saved_pedantic;
12723   void *p;
12724   tree attributes = NULL_TREE;
12725 
12726   /* Check for the `__extension__' keyword.  */
12727   if (cp_parser_extension_opt (parser, &saved_pedantic))
12728     {
12729       /* Parse the qualified declaration.  */
12730       cp_parser_declaration (parser);
12731       /* Restore the PEDANTIC flag.  */
12732       pedantic = saved_pedantic;
12733 
12734       return;
12735     }
12736 
12737   /* Try to figure out what kind of declaration is present.  */
12738   token1 = *cp_lexer_peek_token (parser->lexer);
12739 
12740   if (token1.type != CPP_EOF)
12741     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12742   else
12743     {
12744       token2.type = CPP_EOF;
12745       token2.keyword = RID_MAX;
12746     }
12747 
12748   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12749   p = obstack_alloc (&declarator_obstack, 0);
12750 
12751   /* If the next token is `extern' and the following token is a string
12752      literal, then we have a linkage specification.  */
12753   if (token1.keyword == RID_EXTERN
12754       && cp_parser_is_pure_string_literal (&token2))
12755     cp_parser_linkage_specification (parser);
12756   /* If the next token is `template', then we have either a template
12757      declaration, an explicit instantiation, or an explicit
12758      specialization.  */
12759   else if (token1.keyword == RID_TEMPLATE)
12760     {
12761       /* `template <>' indicates a template specialization.  */
12762       if (token2.type == CPP_LESS
12763 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12764 	cp_parser_explicit_specialization (parser);
12765       /* `template <' indicates a template declaration.  */
12766       else if (token2.type == CPP_LESS)
12767 	cp_parser_template_declaration (parser, /*member_p=*/false);
12768       /* Anything else must be an explicit instantiation.  */
12769       else
12770 	cp_parser_explicit_instantiation (parser);
12771     }
12772   /* If the next token is `export', then we have a template
12773      declaration.  */
12774   else if (token1.keyword == RID_EXPORT)
12775     cp_parser_template_declaration (parser, /*member_p=*/false);
12776   /* If the next token is `extern', 'static' or 'inline' and the one
12777      after that is `template', we have a GNU extended explicit
12778      instantiation directive.  */
12779   else if (cp_parser_allow_gnu_extensions_p (parser)
12780 	   && (token1.keyword == RID_EXTERN
12781 	       || token1.keyword == RID_STATIC
12782 	       || token1.keyword == RID_INLINE)
12783 	   && token2.keyword == RID_TEMPLATE)
12784     cp_parser_explicit_instantiation (parser);
12785   /* If the next token is `namespace', check for a named or unnamed
12786      namespace definition.  */
12787   else if (token1.keyword == RID_NAMESPACE
12788 	   && (/* A named namespace definition.  */
12789 	       (token2.type == CPP_NAME
12790 		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12791 		    != CPP_EQ))
12792                || (token2.type == CPP_OPEN_SQUARE
12793                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12794                    == CPP_OPEN_SQUARE)
12795 	       /* An unnamed namespace definition.  */
12796 	       || token2.type == CPP_OPEN_BRACE
12797 	       || token2.keyword == RID_ATTRIBUTE))
12798     cp_parser_namespace_definition (parser);
12799   /* An inline (associated) namespace definition.  */
12800   else if (token1.keyword == RID_INLINE
12801 	   && token2.keyword == RID_NAMESPACE)
12802     cp_parser_namespace_definition (parser);
12803   /* Objective-C++ declaration/definition.  */
12804   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12805     cp_parser_objc_declaration (parser, NULL_TREE);
12806   else if (c_dialect_objc ()
12807 	   && token1.keyword == RID_ATTRIBUTE
12808 	   && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12809     cp_parser_objc_declaration (parser, attributes);
12810   /* At this point we may have a template declared by a concept
12811      introduction.  */
12812   else if (flag_concepts
12813 	   && cp_parser_template_declaration_after_export (parser,
12814 							   /*member_p=*/false))
12815     /* We did.  */;
12816   else
12817     /* Try to parse a block-declaration, or a function-definition.  */
12818     cp_parser_block_declaration (parser, /*statement_p=*/false);
12819 
12820   /* Free any declarators allocated.  */
12821   obstack_free (&declarator_obstack, p);
12822 }
12823 
12824 /* Parse a block-declaration.
12825 
12826    block-declaration:
12827      simple-declaration
12828      asm-definition
12829      namespace-alias-definition
12830      using-declaration
12831      using-directive
12832 
12833    GNU Extension:
12834 
12835    block-declaration:
12836      __extension__ block-declaration
12837 
12838    C++0x Extension:
12839 
12840    block-declaration:
12841      static_assert-declaration
12842 
12843    If STATEMENT_P is TRUE, then this block-declaration is occurring as
12844    part of a declaration-statement.  */
12845 
12846 static void
cp_parser_block_declaration(cp_parser * parser,bool statement_p)12847 cp_parser_block_declaration (cp_parser *parser,
12848 			     bool      statement_p)
12849 {
12850   cp_token *token1;
12851   int saved_pedantic;
12852 
12853   /* Check for the `__extension__' keyword.  */
12854   if (cp_parser_extension_opt (parser, &saved_pedantic))
12855     {
12856       /* Parse the qualified declaration.  */
12857       cp_parser_block_declaration (parser, statement_p);
12858       /* Restore the PEDANTIC flag.  */
12859       pedantic = saved_pedantic;
12860 
12861       return;
12862     }
12863 
12864   /* Peek at the next token to figure out which kind of declaration is
12865      present.  */
12866   token1 = cp_lexer_peek_token (parser->lexer);
12867 
12868   /* If the next keyword is `asm', we have an asm-definition.  */
12869   if (token1->keyword == RID_ASM)
12870     {
12871       if (statement_p)
12872 	cp_parser_commit_to_tentative_parse (parser);
12873       cp_parser_asm_definition (parser);
12874     }
12875   /* If the next keyword is `namespace', we have a
12876      namespace-alias-definition.  */
12877   else if (token1->keyword == RID_NAMESPACE)
12878     cp_parser_namespace_alias_definition (parser);
12879   /* If the next keyword is `using', we have a
12880      using-declaration, a using-directive, or an alias-declaration.  */
12881   else if (token1->keyword == RID_USING)
12882     {
12883       cp_token *token2;
12884 
12885       if (statement_p)
12886 	cp_parser_commit_to_tentative_parse (parser);
12887       /* If the token after `using' is `namespace', then we have a
12888 	 using-directive.  */
12889       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12890       if (token2->keyword == RID_NAMESPACE)
12891 	cp_parser_using_directive (parser);
12892       /* If the second token after 'using' is '=', then we have an
12893 	 alias-declaration.  */
12894       else if (cxx_dialect >= cxx11
12895 	       && token2->type == CPP_NAME
12896 	       && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12897 		   || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12898 	cp_parser_alias_declaration (parser);
12899       /* Otherwise, it's a using-declaration.  */
12900       else
12901 	cp_parser_using_declaration (parser,
12902 				     /*access_declaration_p=*/false);
12903     }
12904   /* If the next keyword is `__label__' we have a misplaced label
12905      declaration.  */
12906   else if (token1->keyword == RID_LABEL)
12907     {
12908       cp_lexer_consume_token (parser->lexer);
12909       error_at (token1->location, "%<__label__%> not at the beginning of a block");
12910       cp_parser_skip_to_end_of_statement (parser);
12911       /* If the next token is now a `;', consume it.  */
12912       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12913 	cp_lexer_consume_token (parser->lexer);
12914     }
12915   /* If the next token is `static_assert' we have a static assertion.  */
12916   else if (token1->keyword == RID_STATIC_ASSERT)
12917     cp_parser_static_assert (parser, /*member_p=*/false);
12918   /* Anything else must be a simple-declaration.  */
12919   else
12920     cp_parser_simple_declaration (parser, !statement_p,
12921 				  /*maybe_range_for_decl*/NULL);
12922 }
12923 
12924 /* Parse a simple-declaration.
12925 
12926    simple-declaration:
12927      decl-specifier-seq [opt] init-declarator-list [opt] ;
12928      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12929        brace-or-equal-initializer ;
12930 
12931    init-declarator-list:
12932      init-declarator
12933      init-declarator-list , init-declarator
12934 
12935    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12936    function-definition as a simple-declaration.
12937 
12938    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12939    parsed declaration if it is an uninitialized single declarator not followed
12940    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12941    if present, will not be consumed.  */
12942 
12943 static void
cp_parser_simple_declaration(cp_parser * parser,bool function_definition_allowed_p,tree * maybe_range_for_decl)12944 cp_parser_simple_declaration (cp_parser* parser,
12945 			      bool function_definition_allowed_p,
12946 			      tree *maybe_range_for_decl)
12947 {
12948   cp_decl_specifier_seq decl_specifiers;
12949   int declares_class_or_enum;
12950   bool saw_declarator;
12951   location_t comma_loc = UNKNOWN_LOCATION;
12952   location_t init_loc = UNKNOWN_LOCATION;
12953 
12954   if (maybe_range_for_decl)
12955     *maybe_range_for_decl = NULL_TREE;
12956 
12957   /* Defer access checks until we know what is being declared; the
12958      checks for names appearing in the decl-specifier-seq should be
12959      done as if we were in the scope of the thing being declared.  */
12960   push_deferring_access_checks (dk_deferred);
12961 
12962   /* Parse the decl-specifier-seq.  We have to keep track of whether
12963      or not the decl-specifier-seq declares a named class or
12964      enumeration type, since that is the only case in which the
12965      init-declarator-list is allowed to be empty.
12966 
12967      [dcl.dcl]
12968 
12969      In a simple-declaration, the optional init-declarator-list can be
12970      omitted only when declaring a class or enumeration, that is when
12971      the decl-specifier-seq contains either a class-specifier, an
12972      elaborated-type-specifier, or an enum-specifier.  */
12973   cp_parser_decl_specifier_seq (parser,
12974 				CP_PARSER_FLAGS_OPTIONAL,
12975 				&decl_specifiers,
12976 				&declares_class_or_enum);
12977   /* We no longer need to defer access checks.  */
12978   stop_deferring_access_checks ();
12979 
12980   /* In a block scope, a valid declaration must always have a
12981      decl-specifier-seq.  By not trying to parse declarators, we can
12982      resolve the declaration/expression ambiguity more quickly.  */
12983   if (!function_definition_allowed_p
12984       && !decl_specifiers.any_specifiers_p)
12985     {
12986       cp_parser_error (parser, "expected declaration");
12987       goto done;
12988     }
12989 
12990   /* If the next two tokens are both identifiers, the code is
12991      erroneous. The usual cause of this situation is code like:
12992 
12993        T t;
12994 
12995      where "T" should name a type -- but does not.  */
12996   if (!decl_specifiers.any_type_specifiers_p
12997       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12998     {
12999       /* If parsing tentatively, we should commit; we really are
13000 	 looking at a declaration.  */
13001       cp_parser_commit_to_tentative_parse (parser);
13002       /* Give up.  */
13003       goto done;
13004     }
13005 
13006   /* If we have seen at least one decl-specifier, and the next token
13007      is not a parenthesis, then we must be looking at a declaration.
13008      (After "int (" we might be looking at a functional cast.)  */
13009   if (decl_specifiers.any_specifiers_p
13010       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13011       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13012       && !cp_parser_error_occurred (parser))
13013     cp_parser_commit_to_tentative_parse (parser);
13014 
13015   /* Look for C++17 decomposition declaration.  */
13016   for (size_t n = 1; ; n++)
13017     if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13018 	|| cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13019       continue;
13020     else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13021 	     && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13022 	     && decl_specifiers.any_specifiers_p)
13023       {
13024 	tree decl
13025 	  = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13026 						 maybe_range_for_decl,
13027 						 &init_loc);
13028 
13029 	/* The next token should be either a `,' or a `;'.  */
13030 	cp_token *token = cp_lexer_peek_token (parser->lexer);
13031 	/* If it's a `;', we are done.  */
13032 	if (token->type == CPP_SEMICOLON)
13033 	  goto finish;
13034 	else if (maybe_range_for_decl)
13035 	  {
13036 	    if (*maybe_range_for_decl == NULL_TREE)
13037 	      *maybe_range_for_decl = error_mark_node;
13038 	    goto finish;
13039 	  }
13040 	/* Anything else is an error.  */
13041 	else
13042 	  {
13043 	    /* If we have already issued an error message we don't need
13044 	       to issue another one.  */
13045 	    if ((decl != error_mark_node
13046 		 && DECL_INITIAL (decl) != error_mark_node)
13047 		|| cp_parser_uncommitted_to_tentative_parse_p (parser))
13048 	      cp_parser_error (parser, "expected %<,%> or %<;%>");
13049 	    /* Skip tokens until we reach the end of the statement.  */
13050 	    cp_parser_skip_to_end_of_statement (parser);
13051 	    /* If the next token is now a `;', consume it.  */
13052 	    if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13053 	      cp_lexer_consume_token (parser->lexer);
13054 	    goto done;
13055 	  }
13056       }
13057     else
13058       break;
13059 
13060   tree last_type;
13061   bool auto_specifier_p;
13062   /* NULL_TREE if both variable and function declaration are allowed,
13063      error_mark_node if function declaration are not allowed and
13064      a FUNCTION_DECL that should be diagnosed if it is followed by
13065      variable declarations.  */
13066   tree auto_function_declaration;
13067 
13068   last_type = NULL_TREE;
13069   auto_specifier_p
13070     = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13071   auto_function_declaration = NULL_TREE;
13072 
13073   /* Keep going until we hit the `;' at the end of the simple
13074      declaration.  */
13075   saw_declarator = false;
13076   while (cp_lexer_next_token_is_not (parser->lexer,
13077 				     CPP_SEMICOLON))
13078     {
13079       cp_token *token;
13080       bool function_definition_p;
13081       tree decl;
13082       tree auto_result = NULL_TREE;
13083 
13084       if (saw_declarator)
13085 	{
13086 	  /* If we are processing next declarator, comma is expected */
13087 	  token = cp_lexer_peek_token (parser->lexer);
13088 	  gcc_assert (token->type == CPP_COMMA);
13089 	  cp_lexer_consume_token (parser->lexer);
13090 	  if (maybe_range_for_decl)
13091 	    {
13092 	      *maybe_range_for_decl = error_mark_node;
13093 	      if (comma_loc == UNKNOWN_LOCATION)
13094 		comma_loc = token->location;
13095 	    }
13096 	}
13097       else
13098 	saw_declarator = true;
13099 
13100       /* Parse the init-declarator.  */
13101       decl = cp_parser_init_declarator (parser, &decl_specifiers,
13102 					/*checks=*/NULL,
13103 					function_definition_allowed_p,
13104 					/*member_p=*/false,
13105 					declares_class_or_enum,
13106 					&function_definition_p,
13107 					maybe_range_for_decl,
13108 					&init_loc,
13109 					&auto_result);
13110       /* If an error occurred while parsing tentatively, exit quickly.
13111 	 (That usually happens when in the body of a function; each
13112 	 statement is treated as a declaration-statement until proven
13113 	 otherwise.)  */
13114       if (cp_parser_error_occurred (parser))
13115 	goto done;
13116 
13117       if (auto_specifier_p && cxx_dialect >= cxx14)
13118 	{
13119 	  /* If the init-declarator-list contains more than one
13120 	     init-declarator, they shall all form declarations of
13121 	     variables.  */
13122 	  if (auto_function_declaration == NULL_TREE)
13123 	    auto_function_declaration
13124 	      = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13125 	  else if (TREE_CODE (decl) == FUNCTION_DECL
13126 		   || auto_function_declaration != error_mark_node)
13127 	    {
13128 	      error_at (decl_specifiers.locations[ds_type_spec],
13129 			"non-variable %qD in declaration with more than one "
13130 			"declarator with placeholder type",
13131 			TREE_CODE (decl) == FUNCTION_DECL
13132 			? decl : auto_function_declaration);
13133 	      auto_function_declaration = error_mark_node;
13134 	    }
13135 	}
13136 
13137       if (auto_result
13138 	  && (!processing_template_decl || !type_uses_auto (auto_result)))
13139 	{
13140 	  if (last_type
13141 	      && last_type != error_mark_node
13142 	      && !same_type_p (auto_result, last_type))
13143 	    {
13144 	      /* If the list of declarators contains more than one declarator,
13145 		 the type of each declared variable is determined as described
13146 		 above. If the type deduced for the template parameter U is not
13147 		 the same in each deduction, the program is ill-formed.  */
13148 	      error_at (decl_specifiers.locations[ds_type_spec],
13149 			"inconsistent deduction for %qT: %qT and then %qT",
13150 			decl_specifiers.type, last_type, auto_result);
13151 	      last_type = error_mark_node;
13152 	    }
13153 	  else
13154 	    last_type = auto_result;
13155 	}
13156 
13157       /* Handle function definitions specially.  */
13158       if (function_definition_p)
13159 	{
13160 	  /* If the next token is a `,', then we are probably
13161 	     processing something like:
13162 
13163 	       void f() {}, *p;
13164 
13165 	     which is erroneous.  */
13166 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13167 	    {
13168 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
13169 	      error_at (token->location,
13170 			"mixing"
13171 			" declarations and function-definitions is forbidden");
13172 	    }
13173 	  /* Otherwise, we're done with the list of declarators.  */
13174 	  else
13175 	    {
13176 	      pop_deferring_access_checks ();
13177 	      return;
13178 	    }
13179 	}
13180       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13181 	*maybe_range_for_decl = decl;
13182       /* The next token should be either a `,' or a `;'.  */
13183       token = cp_lexer_peek_token (parser->lexer);
13184       /* If it's a `,', there are more declarators to come.  */
13185       if (token->type == CPP_COMMA)
13186 	/* will be consumed next time around */;
13187       /* If it's a `;', we are done.  */
13188       else if (token->type == CPP_SEMICOLON)
13189 	break;
13190       else if (maybe_range_for_decl)
13191 	{
13192 	  if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13193 	    permerror (decl_specifiers.locations[ds_type_spec],
13194 		       "types may not be defined in a for-range-declaration");
13195 	  break;
13196 	}
13197       /* Anything else is an error.  */
13198       else
13199 	{
13200 	  /* If we have already issued an error message we don't need
13201 	     to issue another one.  */
13202 	  if ((decl != error_mark_node
13203 	       && DECL_INITIAL (decl) != error_mark_node)
13204 	      || cp_parser_uncommitted_to_tentative_parse_p (parser))
13205 	    cp_parser_error (parser, "expected %<,%> or %<;%>");
13206 	  /* Skip tokens until we reach the end of the statement.  */
13207 	  cp_parser_skip_to_end_of_statement (parser);
13208 	  /* If the next token is now a `;', consume it.  */
13209 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13210 	    cp_lexer_consume_token (parser->lexer);
13211 	  goto done;
13212 	}
13213       /* After the first time around, a function-definition is not
13214 	 allowed -- even if it was OK at first.  For example:
13215 
13216 	   int i, f() {}
13217 
13218 	 is not valid.  */
13219       function_definition_allowed_p = false;
13220     }
13221 
13222   /* Issue an error message if no declarators are present, and the
13223      decl-specifier-seq does not itself declare a class or
13224      enumeration: [dcl.dcl]/3.  */
13225   if (!saw_declarator)
13226     {
13227       if (cp_parser_declares_only_class_p (parser))
13228 	{
13229 	  if (!declares_class_or_enum
13230 	      && decl_specifiers.type
13231 	      && OVERLOAD_TYPE_P (decl_specifiers.type))
13232 	    /* Ensure an error is issued anyway when finish_decltype_type,
13233 	       called via cp_parser_decl_specifier_seq, returns a class or
13234 	       an enumeration (c++/51786).  */
13235 	    decl_specifiers.type = NULL_TREE;
13236 	  shadow_tag (&decl_specifiers);
13237 	}
13238       /* Perform any deferred access checks.  */
13239       perform_deferred_access_checks (tf_warning_or_error);
13240     }
13241 
13242   /* Consume the `;'.  */
13243  finish:
13244   if (!maybe_range_for_decl)
13245     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13246   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13247     {
13248       if (init_loc != UNKNOWN_LOCATION)
13249 	error_at (init_loc, "initializer in range-based %<for%> loop");
13250       if (comma_loc != UNKNOWN_LOCATION)
13251 	error_at (comma_loc,
13252 		  "multiple declarations in range-based %<for%> loop");
13253     }
13254 
13255  done:
13256   pop_deferring_access_checks ();
13257 }
13258 
13259 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13260      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13261        initializer ;  */
13262 
13263 static tree
cp_parser_decomposition_declaration(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,tree * maybe_range_for_decl,location_t * init_loc)13264 cp_parser_decomposition_declaration (cp_parser *parser,
13265 				     cp_decl_specifier_seq *decl_specifiers,
13266 				     tree *maybe_range_for_decl,
13267 				     location_t *init_loc)
13268 {
13269   cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13270   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13271   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13272 
13273   /* Parse the identifier-list.  */
13274   auto_vec<cp_expr, 10> v;
13275   if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13276     while (true)
13277       {
13278 	cp_expr e = cp_parser_identifier (parser);
13279 	if (e.get_value () == error_mark_node)
13280 	  break;
13281 	v.safe_push (e);
13282 	if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13283 	  break;
13284 	cp_lexer_consume_token (parser->lexer);
13285       }
13286 
13287   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13288   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13289     {
13290       end_loc = UNKNOWN_LOCATION;
13291       cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13292 					       false);
13293       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13294 	cp_lexer_consume_token (parser->lexer);
13295       else
13296 	{
13297 	  cp_parser_skip_to_end_of_statement (parser);
13298 	  return error_mark_node;
13299 	}
13300     }
13301 
13302   if (cxx_dialect < cxx17)
13303     pedwarn (loc, 0, "structured bindings only available with "
13304 		     "-std=c++17 or -std=gnu++17");
13305 
13306   tree pushed_scope;
13307   cp_declarator *declarator = make_declarator (cdk_decomp);
13308   loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13309   declarator->id_loc = loc;
13310   if (ref_qual != REF_QUAL_NONE)
13311     declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13312 					    ref_qual == REF_QUAL_RVALUE,
13313 					    NULL_TREE);
13314   tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13315 			  NULL_TREE, decl_specifiers->attributes,
13316 			  &pushed_scope);
13317   tree orig_decl = decl;
13318 
13319   unsigned int i;
13320   cp_expr e;
13321   cp_decl_specifier_seq decl_specs;
13322   clear_decl_specs (&decl_specs);
13323   decl_specs.type = make_auto ();
13324   tree prev = decl;
13325   FOR_EACH_VEC_ELT (v, i, e)
13326     {
13327       if (i == 0)
13328 	declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13329       else
13330 	declarator->u.id.unqualified_name = e.get_value ();
13331       declarator->id_loc = e.get_location ();
13332       tree elt_pushed_scope;
13333       tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13334 			       NULL_TREE, NULL_TREE, &elt_pushed_scope);
13335       if (decl2 == error_mark_node)
13336 	decl = error_mark_node;
13337       else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13338 	{
13339 	  /* Ensure we've diagnosed redeclaration if we aren't creating
13340 	     a new VAR_DECL.  */
13341 	  gcc_assert (errorcount);
13342 	  decl = error_mark_node;
13343 	}
13344       else
13345 	prev = decl2;
13346       if (elt_pushed_scope)
13347 	pop_scope (elt_pushed_scope);
13348     }
13349 
13350   if (v.is_empty ())
13351     {
13352       error_at (loc, "empty structured binding declaration");
13353       decl = error_mark_node;
13354     }
13355 
13356   if (maybe_range_for_decl == NULL
13357       || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13358     {
13359       bool non_constant_p = false, is_direct_init = false;
13360       *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13361       tree initializer = cp_parser_initializer (parser, &is_direct_init,
13362 						&non_constant_p);
13363       if (initializer == NULL_TREE
13364 	  || (TREE_CODE (initializer) == TREE_LIST
13365 	      && TREE_CHAIN (initializer))
13366 	  || (is_direct_init
13367 	      && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13368 	      && CONSTRUCTOR_NELTS (initializer) != 1))
13369 	{
13370 	  error_at (loc, "invalid initializer for structured binding "
13371 		    "declaration");
13372 	  initializer = error_mark_node;
13373 	}
13374 
13375       if (decl != error_mark_node)
13376 	{
13377 	  cp_maybe_mangle_decomp (decl, prev, v.length ());
13378 	  cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13379 			  is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13380 	  cp_finish_decomp (decl, prev, v.length ());
13381 	}
13382     }
13383   else if (decl != error_mark_node)
13384     {
13385       *maybe_range_for_decl = prev;
13386       /* Ensure DECL_VALUE_EXPR is created for all the decls but
13387 	 the underlying DECL.  */
13388       cp_finish_decomp (decl, prev, v.length ());
13389     }
13390 
13391   if (pushed_scope)
13392     pop_scope (pushed_scope);
13393 
13394   if (decl == error_mark_node && DECL_P (orig_decl))
13395     {
13396       if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13397 	SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13398     }
13399 
13400   return decl;
13401 }
13402 
13403 /* Parse a decl-specifier-seq.
13404 
13405    decl-specifier-seq:
13406      decl-specifier-seq [opt] decl-specifier
13407      decl-specifier attribute-specifier-seq [opt] (C++11)
13408 
13409    decl-specifier:
13410      storage-class-specifier
13411      type-specifier
13412      function-specifier
13413      friend
13414      typedef
13415 
13416    GNU Extension:
13417 
13418    decl-specifier:
13419      attributes
13420 
13421    Concepts Extension:
13422 
13423    decl-specifier:
13424      concept
13425 
13426    Set *DECL_SPECS to a representation of the decl-specifier-seq.
13427 
13428    The parser flags FLAGS is used to control type-specifier parsing.
13429 
13430    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13431    flags:
13432 
13433      1: one of the decl-specifiers is an elaborated-type-specifier
13434 	(i.e., a type declaration)
13435      2: one of the decl-specifiers is an enum-specifier or a
13436 	class-specifier (i.e., a type definition)
13437 
13438    */
13439 
13440 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)13441 cp_parser_decl_specifier_seq (cp_parser* parser,
13442 			      cp_parser_flags flags,
13443 			      cp_decl_specifier_seq *decl_specs,
13444 			      int* declares_class_or_enum)
13445 {
13446   bool constructor_possible_p = !parser->in_declarator_p;
13447   bool found_decl_spec = false;
13448   cp_token *start_token = NULL;
13449   cp_decl_spec ds;
13450 
13451   /* Clear DECL_SPECS.  */
13452   clear_decl_specs (decl_specs);
13453 
13454   /* Assume no class or enumeration type is declared.  */
13455   *declares_class_or_enum = 0;
13456 
13457   /* Keep reading specifiers until there are no more to read.  */
13458   while (true)
13459     {
13460       bool constructor_p;
13461       cp_token *token;
13462       ds = ds_last;
13463 
13464       /* Peek at the next token.  */
13465       token = cp_lexer_peek_token (parser->lexer);
13466 
13467       /* Save the first token of the decl spec list for error
13468          reporting.  */
13469       if (!start_token)
13470 	start_token = token;
13471       /* Handle attributes.  */
13472       if (cp_next_tokens_can_be_attribute_p (parser))
13473 	{
13474 	  /* Parse the attributes.  */
13475 	  tree attrs = cp_parser_attributes_opt (parser);
13476 
13477 	  /* In a sequence of declaration specifiers, c++11 attributes
13478 	     appertain to the type that precede them. In that case
13479 	     [dcl.spec]/1 says:
13480 
13481 	         The attribute-specifier-seq affects the type only for
13482 		 the declaration it appears in, not other declarations
13483 		 involving the same type.
13484 
13485              But for now let's force the user to position the
13486              attribute either at the beginning of the declaration or
13487              after the declarator-id, which would clearly mean that it
13488              applies to the declarator.  */
13489 	  if (cxx11_attribute_p (attrs))
13490 	    {
13491 	      if (!found_decl_spec)
13492 		/* The c++11 attribute is at the beginning of the
13493 		   declaration.  It appertains to the entity being
13494 		   declared.  */;
13495 	      else
13496 		{
13497 		  if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13498 		    {
13499 		      /*  This is an attribute following a
13500 			  class-specifier.  */
13501 		      if (decl_specs->type_definition_p)
13502 			warn_misplaced_attr_for_class_type (token->location,
13503 							    decl_specs->type);
13504 		      attrs = NULL_TREE;
13505 		    }
13506 		  else
13507 		    {
13508 		      decl_specs->std_attributes
13509 			= attr_chainon (decl_specs->std_attributes, attrs);
13510 		      if (decl_specs->locations[ds_std_attribute] == 0)
13511 			decl_specs->locations[ds_std_attribute] = token->location;
13512 		    }
13513 		  continue;
13514 		}
13515 	    }
13516 
13517 	  decl_specs->attributes
13518 	    = attr_chainon (decl_specs->attributes, attrs);
13519 	  if (decl_specs->locations[ds_attribute] == 0)
13520 	    decl_specs->locations[ds_attribute] = token->location;
13521 	  continue;
13522 	}
13523       /* Assume we will find a decl-specifier keyword.  */
13524       found_decl_spec = true;
13525       /* If the next token is an appropriate keyword, we can simply
13526 	 add it to the list.  */
13527       switch (token->keyword)
13528 	{
13529 	  /* decl-specifier:
13530 	       friend
13531                constexpr */
13532 	case RID_FRIEND:
13533 	  if (!at_class_scope_p ())
13534 	    {
13535 	      gcc_rich_location richloc (token->location);
13536 	      richloc.add_fixit_remove ();
13537 	      error_at (&richloc, "%<friend%> used outside of class");
13538 	      cp_lexer_purge_token (parser->lexer);
13539 	    }
13540 	  else
13541 	    {
13542 	      ds = ds_friend;
13543 	      /* Consume the token.  */
13544 	      cp_lexer_consume_token (parser->lexer);
13545 	    }
13546 	  break;
13547 
13548         case RID_CONSTEXPR:
13549 	  ds = ds_constexpr;
13550           cp_lexer_consume_token (parser->lexer);
13551           break;
13552 
13553         case RID_CONCEPT:
13554           ds = ds_concept;
13555           cp_lexer_consume_token (parser->lexer);
13556 
13557 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13558 	    break;
13559 
13560 	  /* In C++20 a concept definition is just 'concept name = expr;'
13561 	     Support that syntax by pretending we've seen 'bool'.  */
13562 	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13563 	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
13564 	    {
13565 	      cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
13566 					    token, /*type_definition*/false);
13567 	      decl_specs->any_type_specifiers_p = true;
13568 	    }
13569           break;
13570 
13571 	  /* function-specifier:
13572 	       inline
13573 	       virtual
13574 	       explicit  */
13575 	case RID_INLINE:
13576 	case RID_VIRTUAL:
13577 	case RID_EXPLICIT:
13578 	  cp_parser_function_specifier_opt (parser, decl_specs);
13579 	  break;
13580 
13581 	  /* decl-specifier:
13582 	       typedef  */
13583 	case RID_TYPEDEF:
13584 	  ds = ds_typedef;
13585 	  /* Consume the token.  */
13586 	  cp_lexer_consume_token (parser->lexer);
13587 
13588 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13589 	    break;
13590 
13591 	  /* A constructor declarator cannot appear in a typedef.  */
13592 	  constructor_possible_p = false;
13593 	  /* The "typedef" keyword can only occur in a declaration; we
13594 	     may as well commit at this point.  */
13595 	  cp_parser_commit_to_tentative_parse (parser);
13596 
13597           if (decl_specs->storage_class != sc_none)
13598             decl_specs->conflicting_specifiers_p = true;
13599 	  break;
13600 
13601 	  /* storage-class-specifier:
13602 	       auto
13603 	       register
13604 	       static
13605 	       extern
13606 	       mutable
13607 
13608 	     GNU Extension:
13609 	       thread  */
13610 	case RID_AUTO:
13611           if (cxx_dialect == cxx98)
13612             {
13613 	      /* Consume the token.  */
13614 	      cp_lexer_consume_token (parser->lexer);
13615 
13616 	      /* Complain about `auto' as a storage specifier, if
13617 		 we're complaining about C++0x compatibility.  */
13618 	      gcc_rich_location richloc (token->location);
13619 	      richloc.add_fixit_remove ();
13620 	      warning_at (&richloc, OPT_Wc__11_compat,
13621 			  "%<auto%> changes meaning in C++11; "
13622 			  "please remove it");
13623 
13624               /* Set the storage class anyway.  */
13625               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13626 					   token);
13627             }
13628           else
13629 	    /* C++0x auto type-specifier.  */
13630 	    found_decl_spec = false;
13631           break;
13632 
13633 	case RID_REGISTER:
13634 	case RID_STATIC:
13635 	case RID_EXTERN:
13636 	case RID_MUTABLE:
13637 	  /* Consume the token.  */
13638 	  cp_lexer_consume_token (parser->lexer);
13639           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13640 				       token);
13641 	  break;
13642 	case RID_THREAD:
13643 	  /* Consume the token.  */
13644 	  ds = ds_thread;
13645 	  cp_lexer_consume_token (parser->lexer);
13646 	  break;
13647 
13648 	default:
13649 	  /* We did not yet find a decl-specifier yet.  */
13650 	  found_decl_spec = false;
13651 	  break;
13652 	}
13653 
13654       if (found_decl_spec
13655 	  && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13656 	  && token->keyword != RID_CONSTEXPR)
13657 	error ("decl-specifier invalid in condition");
13658 
13659       if (found_decl_spec
13660 	  && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13661 	  && token->keyword != RID_MUTABLE
13662 	  && token->keyword != RID_CONSTEXPR)
13663 	error_at (token->location, "%qD invalid in lambda",
13664 		  ridpointers[token->keyword]);
13665 
13666       if (ds != ds_last)
13667 	set_and_check_decl_spec_loc (decl_specs, ds, token);
13668 
13669       /* Constructors are a special case.  The `S' in `S()' is not a
13670 	 decl-specifier; it is the beginning of the declarator.  */
13671       constructor_p
13672 	= (!found_decl_spec
13673 	   && constructor_possible_p
13674 	   && (cp_parser_constructor_declarator_p
13675 	       (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13676 
13677       /* If we don't have a DECL_SPEC yet, then we must be looking at
13678 	 a type-specifier.  */
13679       if (!found_decl_spec && !constructor_p)
13680 	{
13681 	  int decl_spec_declares_class_or_enum;
13682 	  bool is_cv_qualifier;
13683 	  tree type_spec;
13684 
13685 	  if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13686 	    flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
13687 
13688 	  type_spec
13689 	    = cp_parser_type_specifier (parser, flags,
13690 					decl_specs,
13691 					/*is_declaration=*/true,
13692 					&decl_spec_declares_class_or_enum,
13693 					&is_cv_qualifier);
13694 	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13695 
13696 	  /* If this type-specifier referenced a user-defined type
13697 	     (a typedef, class-name, etc.), then we can't allow any
13698 	     more such type-specifiers henceforth.
13699 
13700 	     [dcl.spec]
13701 
13702 	     The longest sequence of decl-specifiers that could
13703 	     possibly be a type name is taken as the
13704 	     decl-specifier-seq of a declaration.  The sequence shall
13705 	     be self-consistent as described below.
13706 
13707 	     [dcl.type]
13708 
13709 	     As a general rule, at most one type-specifier is allowed
13710 	     in the complete decl-specifier-seq of a declaration.  The
13711 	     only exceptions are the following:
13712 
13713 	     -- const or volatile can be combined with any other
13714 		type-specifier.
13715 
13716 	     -- signed or unsigned can be combined with char, long,
13717 		short, or int.
13718 
13719 	     -- ..
13720 
13721 	     Example:
13722 
13723 	       typedef char* Pc;
13724 	       void g (const int Pc);
13725 
13726 	     Here, Pc is *not* part of the decl-specifier seq; it's
13727 	     the declarator.  Therefore, once we see a type-specifier
13728 	     (other than a cv-qualifier), we forbid any additional
13729 	     user-defined types.  We *do* still allow things like `int
13730 	     int' to be considered a decl-specifier-seq, and issue the
13731 	     error message later.  */
13732 	  if (type_spec && !is_cv_qualifier)
13733 	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13734 	  /* A constructor declarator cannot follow a type-specifier.  */
13735 	  if (type_spec)
13736 	    {
13737 	      constructor_possible_p = false;
13738 	      found_decl_spec = true;
13739 	      if (!is_cv_qualifier)
13740 		decl_specs->any_type_specifiers_p = true;
13741 	    }
13742 	}
13743 
13744       /* If we still do not have a DECL_SPEC, then there are no more
13745 	 decl-specifiers.  */
13746       if (!found_decl_spec)
13747 	break;
13748 
13749       decl_specs->any_specifiers_p = true;
13750       /* After we see one decl-specifier, further decl-specifiers are
13751 	 always optional.  */
13752       flags |= CP_PARSER_FLAGS_OPTIONAL;
13753     }
13754 
13755   /* Don't allow a friend specifier with a class definition.  */
13756   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13757       && (*declares_class_or_enum & 2))
13758     error_at (decl_specs->locations[ds_friend],
13759 	      "class definition may not be declared a friend");
13760 }
13761 
13762 /* Parse an (optional) storage-class-specifier.
13763 
13764    storage-class-specifier:
13765      auto
13766      register
13767      static
13768      extern
13769      mutable
13770 
13771    GNU Extension:
13772 
13773    storage-class-specifier:
13774      thread
13775 
13776    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
13777 
13778 static tree
cp_parser_storage_class_specifier_opt(cp_parser * parser)13779 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13780 {
13781   switch (cp_lexer_peek_token (parser->lexer)->keyword)
13782     {
13783     case RID_AUTO:
13784       if (cxx_dialect != cxx98)
13785         return NULL_TREE;
13786       /* Fall through for C++98.  */
13787       gcc_fallthrough ();
13788 
13789     case RID_REGISTER:
13790     case RID_STATIC:
13791     case RID_EXTERN:
13792     case RID_MUTABLE:
13793     case RID_THREAD:
13794       /* Consume the token.  */
13795       return cp_lexer_consume_token (parser->lexer)->u.value;
13796 
13797     default:
13798       return NULL_TREE;
13799     }
13800 }
13801 
13802 /* Parse an (optional) function-specifier.
13803 
13804    function-specifier:
13805      inline
13806      virtual
13807      explicit
13808 
13809    Returns an IDENTIFIER_NODE corresponding to the keyword used.
13810    Updates DECL_SPECS, if it is non-NULL.  */
13811 
13812 static tree
cp_parser_function_specifier_opt(cp_parser * parser,cp_decl_specifier_seq * decl_specs)13813 cp_parser_function_specifier_opt (cp_parser* parser,
13814 				  cp_decl_specifier_seq *decl_specs)
13815 {
13816   cp_token *token = cp_lexer_peek_token (parser->lexer);
13817   switch (token->keyword)
13818     {
13819     case RID_INLINE:
13820       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13821       break;
13822 
13823     case RID_VIRTUAL:
13824       /* 14.5.2.3 [temp.mem]
13825 
13826 	 A member function template shall not be virtual.  */
13827       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13828 	  && current_class_type)
13829 	error_at (token->location, "templates may not be %<virtual%>");
13830       else
13831 	set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13832       break;
13833 
13834     case RID_EXPLICIT:
13835       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13836       break;
13837 
13838     default:
13839       return NULL_TREE;
13840     }
13841 
13842   /* Consume the token.  */
13843   return cp_lexer_consume_token (parser->lexer)->u.value;
13844 }
13845 
13846 /* Parse a linkage-specification.
13847 
13848    linkage-specification:
13849      extern string-literal { declaration-seq [opt] }
13850      extern string-literal declaration  */
13851 
13852 static void
cp_parser_linkage_specification(cp_parser * parser)13853 cp_parser_linkage_specification (cp_parser* parser)
13854 {
13855   tree linkage;
13856 
13857   /* Look for the `extern' keyword.  */
13858   cp_token *extern_token
13859     = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13860 
13861   /* Look for the string-literal.  */
13862   cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13863   linkage = cp_parser_string_literal (parser, false, false);
13864 
13865   /* Transform the literal into an identifier.  If the literal is a
13866      wide-character string, or contains embedded NULs, then we can't
13867      handle it as the user wants.  */
13868   if (strlen (TREE_STRING_POINTER (linkage))
13869       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13870     {
13871       cp_parser_error (parser, "invalid linkage-specification");
13872       /* Assume C++ linkage.  */
13873       linkage = lang_name_cplusplus;
13874     }
13875   else
13876     linkage = get_identifier (TREE_STRING_POINTER (linkage));
13877 
13878   /* We're now using the new linkage.  */
13879   push_lang_context (linkage);
13880 
13881   /* Preserve the location of the the innermost linkage specification,
13882      tracking the locations of nested specifications via a local.  */
13883   location_t saved_location
13884     = parser->innermost_linkage_specification_location;
13885   /* Construct a location ranging from the start of the "extern" to
13886      the end of the string-literal, with the caret at the start, e.g.:
13887        extern "C" {
13888        ^~~~~~~~~~
13889   */
13890   parser->innermost_linkage_specification_location
13891     = make_location (extern_token->location,
13892 		     extern_token->location,
13893 		     get_finish (string_token->location));
13894 
13895   /* If the next token is a `{', then we're using the first
13896      production.  */
13897   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13898     {
13899       cp_ensure_no_omp_declare_simd (parser);
13900       cp_ensure_no_oacc_routine (parser);
13901 
13902       /* Consume the `{' token.  */
13903       matching_braces braces;
13904       braces.consume_open (parser)->location;
13905       /* Parse the declarations.  */
13906       cp_parser_declaration_seq_opt (parser);
13907       /* Look for the closing `}'.  */
13908       braces.require_close (parser);
13909     }
13910   /* Otherwise, there's just one declaration.  */
13911   else
13912     {
13913       bool saved_in_unbraced_linkage_specification_p;
13914 
13915       saved_in_unbraced_linkage_specification_p
13916 	= parser->in_unbraced_linkage_specification_p;
13917       parser->in_unbraced_linkage_specification_p = true;
13918       cp_parser_declaration (parser);
13919       parser->in_unbraced_linkage_specification_p
13920 	= saved_in_unbraced_linkage_specification_p;
13921     }
13922 
13923   /* We're done with the linkage-specification.  */
13924   pop_lang_context ();
13925 
13926   /* Restore location of parent linkage specification, if any.  */
13927   parser->innermost_linkage_specification_location = saved_location;
13928 }
13929 
13930 /* Parse a static_assert-declaration.
13931 
13932    static_assert-declaration:
13933      static_assert ( constant-expression , string-literal ) ;
13934      static_assert ( constant-expression ) ; (C++17)
13935 
13936    If MEMBER_P, this static_assert is a class member.  */
13937 
13938 static void
cp_parser_static_assert(cp_parser * parser,bool member_p)13939 cp_parser_static_assert(cp_parser *parser, bool member_p)
13940 {
13941   cp_expr condition;
13942   location_t token_loc;
13943   tree message;
13944   bool dummy;
13945 
13946   /* Peek at the `static_assert' token so we can keep track of exactly
13947      where the static assertion started.  */
13948   token_loc = cp_lexer_peek_token (parser->lexer)->location;
13949 
13950   /* Look for the `static_assert' keyword.  */
13951   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13952                                   RT_STATIC_ASSERT))
13953     return;
13954 
13955   /*  We know we are in a static assertion; commit to any tentative
13956       parse.  */
13957   if (cp_parser_parsing_tentatively (parser))
13958     cp_parser_commit_to_tentative_parse (parser);
13959 
13960   /* Parse the `(' starting the static assertion condition.  */
13961   matching_parens parens;
13962   parens.require_open (parser);
13963 
13964   /* Parse the constant-expression.  Allow a non-constant expression
13965      here in order to give better diagnostics in finish_static_assert.  */
13966   condition =
13967     cp_parser_constant_expression (parser,
13968                                    /*allow_non_constant_p=*/true,
13969                                    /*non_constant_p=*/&dummy);
13970 
13971   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13972     {
13973       if (cxx_dialect < cxx17)
13974 	pedwarn (input_location, OPT_Wpedantic,
13975 		 "static_assert without a message "
13976 		 "only available with -std=c++17 or -std=gnu++17");
13977       /* Eat the ')'  */
13978       cp_lexer_consume_token (parser->lexer);
13979       message = build_string (1, "");
13980       TREE_TYPE (message) = char_array_type_node;
13981       fix_string_type (message);
13982     }
13983   else
13984     {
13985       /* Parse the separating `,'.  */
13986       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13987 
13988       /* Parse the string-literal message.  */
13989       message = cp_parser_string_literal (parser,
13990                                 	  /*translate=*/false,
13991                                 	  /*wide_ok=*/true);
13992 
13993       /* A `)' completes the static assertion.  */
13994       if (!parens.require_close (parser))
13995 	cp_parser_skip_to_closing_parenthesis (parser,
13996                                                /*recovering=*/true,
13997                                                /*or_comma=*/false,
13998 					       /*consume_paren=*/true);
13999     }
14000 
14001   /* A semicolon terminates the declaration.  */
14002   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14003 
14004   /* Get the location for the static assertion.  Use that of the
14005      condition if available, otherwise, use that of the "static_assert"
14006      token.  */
14007   location_t assert_loc = condition.get_location ();
14008   if (assert_loc == UNKNOWN_LOCATION)
14009     assert_loc = token_loc;
14010 
14011   /* Complete the static assertion, which may mean either processing
14012      the static assert now or saving it for template instantiation.  */
14013   finish_static_assert (condition, message, assert_loc, member_p);
14014 }
14015 
14016 /* Parse the expression in decltype ( expression ).  */
14017 
14018 static tree
cp_parser_decltype_expr(cp_parser * parser,bool & id_expression_or_member_access_p)14019 cp_parser_decltype_expr (cp_parser *parser,
14020 			 bool &id_expression_or_member_access_p)
14021 {
14022   cp_token *id_expr_start_token;
14023   tree expr;
14024 
14025   /* Since we're going to preserve any side-effects from this parse, set up a
14026      firewall to protect our callers from cp_parser_commit_to_tentative_parse
14027      in the expression.  */
14028   tentative_firewall firewall (parser);
14029 
14030   /* First, try parsing an id-expression.  */
14031   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14032   cp_parser_parse_tentatively (parser);
14033   expr = cp_parser_id_expression (parser,
14034                                   /*template_keyword_p=*/false,
14035                                   /*check_dependency_p=*/true,
14036                                   /*template_p=*/NULL,
14037                                   /*declarator_p=*/false,
14038                                   /*optional_p=*/false);
14039 
14040   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14041     {
14042       bool non_integral_constant_expression_p = false;
14043       tree id_expression = expr;
14044       cp_id_kind idk;
14045       const char *error_msg;
14046 
14047       if (identifier_p (expr))
14048 	/* Lookup the name we got back from the id-expression.  */
14049 	expr = cp_parser_lookup_name_simple (parser, expr,
14050 					     id_expr_start_token->location);
14051 
14052       if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14053 	/* A template without args is not a complete id-expression.  */
14054 	expr = error_mark_node;
14055 
14056       if (expr
14057           && expr != error_mark_node
14058           && TREE_CODE (expr) != TYPE_DECL
14059 	  && (TREE_CODE (expr) != BIT_NOT_EXPR
14060 	      || !TYPE_P (TREE_OPERAND (expr, 0)))
14061           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14062         {
14063           /* Complete lookup of the id-expression.  */
14064           expr = (finish_id_expression
14065                   (id_expression, expr, parser->scope, &idk,
14066                    /*integral_constant_expression_p=*/false,
14067                    /*allow_non_integral_constant_expression_p=*/true,
14068                    &non_integral_constant_expression_p,
14069                    /*template_p=*/false,
14070                    /*done=*/true,
14071                    /*address_p=*/false,
14072                    /*template_arg_p=*/false,
14073                    &error_msg,
14074 		   id_expr_start_token->location));
14075 
14076           if (expr == error_mark_node)
14077             /* We found an id-expression, but it was something that we
14078                should not have found. This is an error, not something
14079                we can recover from, so note that we found an
14080                id-expression and we'll recover as gracefully as
14081                possible.  */
14082             id_expression_or_member_access_p = true;
14083         }
14084 
14085       if (expr
14086           && expr != error_mark_node
14087           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14088         /* We have an id-expression.  */
14089         id_expression_or_member_access_p = true;
14090     }
14091 
14092   if (!id_expression_or_member_access_p)
14093     {
14094       /* Abort the id-expression parse.  */
14095       cp_parser_abort_tentative_parse (parser);
14096 
14097       /* Parsing tentatively, again.  */
14098       cp_parser_parse_tentatively (parser);
14099 
14100       /* Parse a class member access.  */
14101       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14102                                            /*cast_p=*/false, /*decltype*/true,
14103                                            /*member_access_only_p=*/true, NULL);
14104 
14105       if (expr
14106           && expr != error_mark_node
14107           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14108         /* We have an id-expression.  */
14109         id_expression_or_member_access_p = true;
14110     }
14111 
14112   if (id_expression_or_member_access_p)
14113     /* We have parsed the complete id-expression or member access.  */
14114     cp_parser_parse_definitely (parser);
14115   else
14116     {
14117       /* Abort our attempt to parse an id-expression or member access
14118          expression.  */
14119       cp_parser_abort_tentative_parse (parser);
14120 
14121       /* Commit to the tentative_firewall so we get syntax errors.  */
14122       cp_parser_commit_to_tentative_parse (parser);
14123 
14124       /* Parse a full expression.  */
14125       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14126 				   /*decltype_p=*/true);
14127     }
14128 
14129   return expr;
14130 }
14131 
14132 /* Parse a `decltype' type. Returns the type.
14133 
14134    simple-type-specifier:
14135      decltype ( expression )
14136    C++14 proposal:
14137      decltype ( auto )  */
14138 
14139 static tree
cp_parser_decltype(cp_parser * parser)14140 cp_parser_decltype (cp_parser *parser)
14141 {
14142   bool id_expression_or_member_access_p = false;
14143   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14144 
14145   if (start_token->type == CPP_DECLTYPE)
14146     {
14147       /* Already parsed.  */
14148       cp_lexer_consume_token (parser->lexer);
14149       return saved_checks_value (start_token->u.tree_check_value);
14150     }
14151 
14152   /* Look for the `decltype' token.  */
14153   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14154     return error_mark_node;
14155 
14156   /* Parse the opening `('.  */
14157   matching_parens parens;
14158   if (!parens.require_open (parser))
14159     return error_mark_node;
14160 
14161   push_deferring_access_checks (dk_deferred);
14162 
14163   tree expr = NULL_TREE;
14164 
14165   if (cxx_dialect >= cxx14
14166       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14167     /* decltype (auto) */
14168     cp_lexer_consume_token (parser->lexer);
14169   else
14170     {
14171       /* decltype (expression)  */
14172 
14173       /* Types cannot be defined in a `decltype' expression.  Save away the
14174 	 old message and set the new one.  */
14175       const char *saved_message = parser->type_definition_forbidden_message;
14176       parser->type_definition_forbidden_message
14177 	= G_("types may not be defined in %<decltype%> expressions");
14178 
14179       /* The restrictions on constant-expressions do not apply inside
14180 	 decltype expressions.  */
14181       bool saved_integral_constant_expression_p
14182 	= parser->integral_constant_expression_p;
14183       bool saved_non_integral_constant_expression_p
14184 	= parser->non_integral_constant_expression_p;
14185       parser->integral_constant_expression_p = false;
14186 
14187       /* Within a parenthesized expression, a `>' token is always
14188 	 the greater-than operator.  */
14189       bool saved_greater_than_is_operator_p
14190 	= parser->greater_than_is_operator_p;
14191       parser->greater_than_is_operator_p = true;
14192 
14193       /* Do not actually evaluate the expression.  */
14194       ++cp_unevaluated_operand;
14195 
14196       /* Do not warn about problems with the expression.  */
14197       ++c_inhibit_evaluation_warnings;
14198 
14199       expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14200 
14201       /* Go back to evaluating expressions.  */
14202       --cp_unevaluated_operand;
14203       --c_inhibit_evaluation_warnings;
14204 
14205       /* The `>' token might be the end of a template-id or
14206 	 template-parameter-list now.  */
14207       parser->greater_than_is_operator_p
14208 	= saved_greater_than_is_operator_p;
14209 
14210       /* Restore the old message and the integral constant expression
14211 	 flags.  */
14212       parser->type_definition_forbidden_message = saved_message;
14213       parser->integral_constant_expression_p
14214 	= saved_integral_constant_expression_p;
14215       parser->non_integral_constant_expression_p
14216 	= saved_non_integral_constant_expression_p;
14217     }
14218 
14219   /* Parse to the closing `)'.  */
14220   if (!parens.require_close (parser))
14221     {
14222       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14223 					     /*consume_paren=*/true);
14224       pop_deferring_access_checks ();
14225       return error_mark_node;
14226     }
14227 
14228   if (!expr)
14229     {
14230       /* Build auto.  */
14231       expr = make_decltype_auto ();
14232       AUTO_IS_DECLTYPE (expr) = true;
14233     }
14234   else
14235     expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14236 				 tf_warning_or_error);
14237 
14238   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14239      it again.  */
14240   start_token->type = CPP_DECLTYPE;
14241   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14242   start_token->u.tree_check_value->value = expr;
14243   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14244   start_token->keyword = RID_MAX;
14245   cp_lexer_purge_tokens_after (parser->lexer, start_token);
14246 
14247   pop_to_parent_deferring_access_checks ();
14248 
14249   return expr;
14250 }
14251 
14252 /* Special member functions [gram.special] */
14253 
14254 /* Parse a conversion-function-id.
14255 
14256    conversion-function-id:
14257      operator conversion-type-id
14258 
14259    Returns an IDENTIFIER_NODE representing the operator.  */
14260 
14261 static tree
cp_parser_conversion_function_id(cp_parser * parser)14262 cp_parser_conversion_function_id (cp_parser* parser)
14263 {
14264   tree type;
14265   tree saved_scope;
14266   tree saved_qualifying_scope;
14267   tree saved_object_scope;
14268   tree pushed_scope = NULL_TREE;
14269 
14270   /* Look for the `operator' token.  */
14271   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14272     return error_mark_node;
14273   /* When we parse the conversion-type-id, the current scope will be
14274      reset.  However, we need that information in able to look up the
14275      conversion function later, so we save it here.  */
14276   saved_scope = parser->scope;
14277   saved_qualifying_scope = parser->qualifying_scope;
14278   saved_object_scope = parser->object_scope;
14279   /* We must enter the scope of the class so that the names of
14280      entities declared within the class are available in the
14281      conversion-type-id.  For example, consider:
14282 
14283        struct S {
14284 	 typedef int I;
14285 	 operator I();
14286        };
14287 
14288        S::operator I() { ... }
14289 
14290      In order to see that `I' is a type-name in the definition, we
14291      must be in the scope of `S'.  */
14292   if (saved_scope)
14293     pushed_scope = push_scope (saved_scope);
14294   /* Parse the conversion-type-id.  */
14295   type = cp_parser_conversion_type_id (parser);
14296   /* Leave the scope of the class, if any.  */
14297   if (pushed_scope)
14298     pop_scope (pushed_scope);
14299   /* Restore the saved scope.  */
14300   parser->scope = saved_scope;
14301   parser->qualifying_scope = saved_qualifying_scope;
14302   parser->object_scope = saved_object_scope;
14303   /* If the TYPE is invalid, indicate failure.  */
14304   if (type == error_mark_node)
14305     return error_mark_node;
14306   return make_conv_op_name (type);
14307 }
14308 
14309 /* Parse a conversion-type-id:
14310 
14311    conversion-type-id:
14312      type-specifier-seq conversion-declarator [opt]
14313 
14314    Returns the TYPE specified.  */
14315 
14316 static tree
cp_parser_conversion_type_id(cp_parser * parser)14317 cp_parser_conversion_type_id (cp_parser* parser)
14318 {
14319   tree attributes;
14320   cp_decl_specifier_seq type_specifiers;
14321   cp_declarator *declarator;
14322   tree type_specified;
14323   const char *saved_message;
14324 
14325   /* Parse the attributes.  */
14326   attributes = cp_parser_attributes_opt (parser);
14327 
14328   saved_message = parser->type_definition_forbidden_message;
14329   parser->type_definition_forbidden_message
14330     = G_("types may not be defined in a conversion-type-id");
14331 
14332   /* Parse the type-specifiers.  */
14333   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14334 				/*is_trailing_return=*/false,
14335 				&type_specifiers);
14336 
14337   parser->type_definition_forbidden_message = saved_message;
14338 
14339   /* If that didn't work, stop.  */
14340   if (type_specifiers.type == error_mark_node)
14341     return error_mark_node;
14342   /* Parse the conversion-declarator.  */
14343   declarator = cp_parser_conversion_declarator_opt (parser);
14344 
14345   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
14346 				    /*initialized=*/0, &attributes);
14347   if (attributes)
14348     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14349 
14350   /* Don't give this error when parsing tentatively.  This happens to
14351      work because we always parse this definitively once.  */
14352   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14353       && type_uses_auto (type_specified))
14354     {
14355       if (cxx_dialect < cxx14)
14356 	{
14357 	  error ("invalid use of %<auto%> in conversion operator");
14358 	  return error_mark_node;
14359 	}
14360       else if (template_parm_scope_p ())
14361 	warning (0, "use of %<auto%> in member template "
14362 		 "conversion operator can never be deduced");
14363     }
14364 
14365   return type_specified;
14366 }
14367 
14368 /* Parse an (optional) conversion-declarator.
14369 
14370    conversion-declarator:
14371      ptr-operator conversion-declarator [opt]
14372 
14373    */
14374 
14375 static cp_declarator *
cp_parser_conversion_declarator_opt(cp_parser * parser)14376 cp_parser_conversion_declarator_opt (cp_parser* parser)
14377 {
14378   enum tree_code code;
14379   tree class_type, std_attributes = NULL_TREE;
14380   cp_cv_quals cv_quals;
14381 
14382   /* We don't know if there's a ptr-operator next, or not.  */
14383   cp_parser_parse_tentatively (parser);
14384   /* Try the ptr-operator.  */
14385   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14386 				 &std_attributes);
14387   /* If it worked, look for more conversion-declarators.  */
14388   if (cp_parser_parse_definitely (parser))
14389     {
14390       cp_declarator *declarator;
14391 
14392       /* Parse another optional declarator.  */
14393       declarator = cp_parser_conversion_declarator_opt (parser);
14394 
14395       declarator = cp_parser_make_indirect_declarator
14396 	(code, class_type, cv_quals, declarator, std_attributes);
14397 
14398       return declarator;
14399    }
14400 
14401   return NULL;
14402 }
14403 
14404 /* Parse an (optional) ctor-initializer.
14405 
14406    ctor-initializer:
14407      : mem-initializer-list  */
14408 
14409 static void
cp_parser_ctor_initializer_opt(cp_parser * parser)14410 cp_parser_ctor_initializer_opt (cp_parser* parser)
14411 {
14412   /* If the next token is not a `:', then there is no
14413      ctor-initializer.  */
14414   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14415     {
14416       /* Do default initialization of any bases and members.  */
14417       if (DECL_CONSTRUCTOR_P (current_function_decl))
14418 	finish_mem_initializers (NULL_TREE);
14419       return;
14420     }
14421 
14422   /* Consume the `:' token.  */
14423   cp_lexer_consume_token (parser->lexer);
14424   /* And the mem-initializer-list.  */
14425   cp_parser_mem_initializer_list (parser);
14426 }
14427 
14428 /* Parse a mem-initializer-list.
14429 
14430    mem-initializer-list:
14431      mem-initializer ... [opt]
14432      mem-initializer ... [opt] , mem-initializer-list  */
14433 
14434 static void
cp_parser_mem_initializer_list(cp_parser * parser)14435 cp_parser_mem_initializer_list (cp_parser* parser)
14436 {
14437   tree mem_initializer_list = NULL_TREE;
14438   tree target_ctor = error_mark_node;
14439   cp_token *token = cp_lexer_peek_token (parser->lexer);
14440 
14441   /* Let the semantic analysis code know that we are starting the
14442      mem-initializer-list.  */
14443   if (!DECL_CONSTRUCTOR_P (current_function_decl))
14444     error_at (token->location,
14445 	      "only constructors take member initializers");
14446 
14447   /* Loop through the list.  */
14448   while (true)
14449     {
14450       tree mem_initializer;
14451 
14452       token = cp_lexer_peek_token (parser->lexer);
14453       /* Parse the mem-initializer.  */
14454       mem_initializer = cp_parser_mem_initializer (parser);
14455       /* If the next token is a `...', we're expanding member initializers. */
14456       bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14457       if (ellipsis
14458 	  || (mem_initializer != error_mark_node
14459 	      && check_for_bare_parameter_packs (TREE_PURPOSE
14460 						 (mem_initializer))))
14461         {
14462           /* Consume the `...'. */
14463 	  if (ellipsis)
14464 	    cp_lexer_consume_token (parser->lexer);
14465 
14466           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14467              can be expanded but members cannot. */
14468           if (mem_initializer != error_mark_node
14469               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14470             {
14471               error_at (token->location,
14472 			"cannot expand initializer for member %qD",
14473 			TREE_PURPOSE (mem_initializer));
14474               mem_initializer = error_mark_node;
14475             }
14476 
14477           /* Construct the pack expansion type. */
14478           if (mem_initializer != error_mark_node)
14479             mem_initializer = make_pack_expansion (mem_initializer);
14480         }
14481       if (target_ctor != error_mark_node
14482 	  && mem_initializer != error_mark_node)
14483 	{
14484 	  error ("mem-initializer for %qD follows constructor delegation",
14485 		 TREE_PURPOSE (mem_initializer));
14486 	  mem_initializer = error_mark_node;
14487 	}
14488       /* Look for a target constructor. */
14489       if (mem_initializer != error_mark_node
14490 	  && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14491 	  && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14492 	{
14493 	  maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14494 	  if (mem_initializer_list)
14495 	    {
14496 	      error ("constructor delegation follows mem-initializer for %qD",
14497 		     TREE_PURPOSE (mem_initializer_list));
14498 	      mem_initializer = error_mark_node;
14499 	    }
14500 	  target_ctor = mem_initializer;
14501 	}
14502       /* Add it to the list, unless it was erroneous.  */
14503       if (mem_initializer != error_mark_node)
14504 	{
14505 	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
14506 	  mem_initializer_list = mem_initializer;
14507 	}
14508       /* If the next token is not a `,', we're done.  */
14509       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14510 	break;
14511       /* Consume the `,' token.  */
14512       cp_lexer_consume_token (parser->lexer);
14513     }
14514 
14515   /* Perform semantic analysis.  */
14516   if (DECL_CONSTRUCTOR_P (current_function_decl))
14517     finish_mem_initializers (mem_initializer_list);
14518 }
14519 
14520 /* Parse a mem-initializer.
14521 
14522    mem-initializer:
14523      mem-initializer-id ( expression-list [opt] )
14524      mem-initializer-id braced-init-list
14525 
14526    GNU extension:
14527 
14528    mem-initializer:
14529      ( expression-list [opt] )
14530 
14531    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
14532    class) or FIELD_DECL (for a non-static data member) to initialize;
14533    the TREE_VALUE is the expression-list.  An empty initialization
14534    list is represented by void_list_node.  */
14535 
14536 static tree
cp_parser_mem_initializer(cp_parser * parser)14537 cp_parser_mem_initializer (cp_parser* parser)
14538 {
14539   tree mem_initializer_id;
14540   tree expression_list;
14541   tree member;
14542   cp_token *token = cp_lexer_peek_token (parser->lexer);
14543 
14544   /* Find out what is being initialized.  */
14545   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14546     {
14547       permerror (token->location,
14548 		 "anachronistic old-style base class initializer");
14549       mem_initializer_id = NULL_TREE;
14550     }
14551   else
14552     {
14553       mem_initializer_id = cp_parser_mem_initializer_id (parser);
14554       if (mem_initializer_id == error_mark_node)
14555 	return mem_initializer_id;
14556     }
14557   member = expand_member_init (mem_initializer_id);
14558   if (member && !DECL_P (member))
14559     in_base_initializer = 1;
14560 
14561   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14562     {
14563       bool expr_non_constant_p;
14564       cp_lexer_set_source_position (parser->lexer);
14565       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14566       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14567       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14568       expression_list = build_tree_list (NULL_TREE, expression_list);
14569     }
14570   else
14571     {
14572       vec<tree, va_gc> *vec;
14573       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14574 						     /*cast_p=*/false,
14575 						     /*allow_expansion_p=*/true,
14576 						     /*non_constant_p=*/NULL);
14577       if (vec == NULL)
14578 	return error_mark_node;
14579       expression_list = build_tree_list_vec (vec);
14580       release_tree_vector (vec);
14581     }
14582 
14583   if (expression_list == error_mark_node)
14584     return error_mark_node;
14585   if (!expression_list)
14586     expression_list = void_type_node;
14587 
14588   in_base_initializer = 0;
14589 
14590   return member ? build_tree_list (member, expression_list) : error_mark_node;
14591 }
14592 
14593 /* Parse a mem-initializer-id.
14594 
14595    mem-initializer-id:
14596      :: [opt] nested-name-specifier [opt] class-name
14597      decltype-specifier (C++11)
14598      identifier
14599 
14600    Returns a TYPE indicating the class to be initialized for the first
14601    production (and the second in C++11).  Returns an IDENTIFIER_NODE
14602    indicating the data member to be initialized for the last production.  */
14603 
14604 static tree
cp_parser_mem_initializer_id(cp_parser * parser)14605 cp_parser_mem_initializer_id (cp_parser* parser)
14606 {
14607   bool global_scope_p;
14608   bool nested_name_specifier_p;
14609   bool template_p = false;
14610   tree id;
14611 
14612   cp_token *token = cp_lexer_peek_token (parser->lexer);
14613 
14614   /* `typename' is not allowed in this context ([temp.res]).  */
14615   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14616     {
14617       error_at (token->location,
14618 		"keyword %<typename%> not allowed in this context (a qualified "
14619 		"member initializer is implicitly a type)");
14620       cp_lexer_consume_token (parser->lexer);
14621     }
14622   /* Look for the optional `::' operator.  */
14623   global_scope_p
14624     = (cp_parser_global_scope_opt (parser,
14625 				   /*current_scope_valid_p=*/false)
14626        != NULL_TREE);
14627   /* Look for the optional nested-name-specifier.  The simplest way to
14628      implement:
14629 
14630        [temp.res]
14631 
14632        The keyword `typename' is not permitted in a base-specifier or
14633        mem-initializer; in these contexts a qualified name that
14634        depends on a template-parameter is implicitly assumed to be a
14635        type name.
14636 
14637      is to assume that we have seen the `typename' keyword at this
14638      point.  */
14639   nested_name_specifier_p
14640     = (cp_parser_nested_name_specifier_opt (parser,
14641 					    /*typename_keyword_p=*/true,
14642 					    /*check_dependency_p=*/true,
14643 					    /*type_p=*/true,
14644 					    /*is_declaration=*/true)
14645        != NULL_TREE);
14646   if (nested_name_specifier_p)
14647     template_p = cp_parser_optional_template_keyword (parser);
14648   /* If there is a `::' operator or a nested-name-specifier, then we
14649      are definitely looking for a class-name.  */
14650   if (global_scope_p || nested_name_specifier_p)
14651     return cp_parser_class_name (parser,
14652 				 /*typename_keyword_p=*/true,
14653 				 /*template_keyword_p=*/template_p,
14654 				 typename_type,
14655 				 /*check_dependency_p=*/true,
14656 				 /*class_head_p=*/false,
14657 				 /*is_declaration=*/true);
14658   /* Otherwise, we could also be looking for an ordinary identifier.  */
14659   cp_parser_parse_tentatively (parser);
14660   if (cp_lexer_next_token_is_decltype (parser->lexer))
14661     /* Try a decltype-specifier.  */
14662     id = cp_parser_decltype (parser);
14663   else
14664     /* Otherwise, try a class-name.  */
14665     id = cp_parser_class_name (parser,
14666 			       /*typename_keyword_p=*/true,
14667 			       /*template_keyword_p=*/false,
14668 			       none_type,
14669 			       /*check_dependency_p=*/true,
14670 			       /*class_head_p=*/false,
14671 			       /*is_declaration=*/true);
14672   /* If we found one, we're done.  */
14673   if (cp_parser_parse_definitely (parser))
14674     return id;
14675   /* Otherwise, look for an ordinary identifier.  */
14676   return cp_parser_identifier (parser);
14677 }
14678 
14679 /* Overloading [gram.over] */
14680 
14681 /* Parse an operator-function-id.
14682 
14683    operator-function-id:
14684      operator operator
14685 
14686    Returns an IDENTIFIER_NODE for the operator which is a
14687    human-readable spelling of the identifier, e.g., `operator +'.  */
14688 
14689 static cp_expr
cp_parser_operator_function_id(cp_parser * parser)14690 cp_parser_operator_function_id (cp_parser* parser)
14691 {
14692   /* Look for the `operator' keyword.  */
14693   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14694     return error_mark_node;
14695   /* And then the name of the operator itself.  */
14696   return cp_parser_operator (parser);
14697 }
14698 
14699 /* Return an identifier node for a user-defined literal operator.
14700    The suffix identifier is chained to the operator name identifier.  */
14701 
14702 tree
cp_literal_operator_id(const char * name)14703 cp_literal_operator_id (const char* name)
14704 {
14705   tree identifier;
14706   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14707 			      + strlen (name) + 10);
14708   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14709   identifier = get_identifier (buffer);
14710 
14711   return identifier;
14712 }
14713 
14714 /* Parse an operator.
14715 
14716    operator:
14717      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14718      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14719      || ++ -- , ->* -> () []
14720 
14721    GNU Extensions:
14722 
14723    operator:
14724      <? >? <?= >?=
14725 
14726    Returns an IDENTIFIER_NODE for the operator which is a
14727    human-readable spelling of the identifier, e.g., `operator +'.  */
14728 
14729 static cp_expr
cp_parser_operator(cp_parser * parser)14730 cp_parser_operator (cp_parser* parser)
14731 {
14732   tree id = NULL_TREE;
14733   cp_token *token;
14734   bool utf8 = false;
14735 
14736   /* Peek at the next token.  */
14737   token = cp_lexer_peek_token (parser->lexer);
14738 
14739   location_t start_loc = token->location;
14740 
14741   /* Figure out which operator we have.  */
14742   enum tree_code op = ERROR_MARK;
14743   bool assop = false;
14744   bool consumed = false;
14745   switch (token->type)
14746     {
14747     case CPP_KEYWORD:
14748       {
14749 	/* The keyword should be either `new' or `delete'.  */
14750 	if (token->keyword == RID_NEW)
14751 	  op = NEW_EXPR;
14752 	else if (token->keyword == RID_DELETE)
14753 	  op = DELETE_EXPR;
14754 	else
14755 	  break;
14756 
14757 	/* Consume the `new' or `delete' token.  */
14758 	location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14759 
14760 	/* Peek at the next token.  */
14761 	token = cp_lexer_peek_token (parser->lexer);
14762 	/* If it's a `[' token then this is the array variant of the
14763 	   operator.  */
14764 	if (token->type == CPP_OPEN_SQUARE)
14765 	  {
14766 	    /* Consume the `[' token.  */
14767 	    cp_lexer_consume_token (parser->lexer);
14768 	    /* Look for the `]' token.  */
14769 	    if (cp_token *close_token
14770 		= cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14771 	      end_loc = close_token->location;
14772 	    op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14773 	  }
14774 	start_loc = make_location (start_loc, start_loc, end_loc);
14775 	consumed = true;
14776 	break;
14777       }
14778 
14779     case CPP_PLUS:
14780       op = PLUS_EXPR;
14781       break;
14782 
14783     case CPP_MINUS:
14784       op = MINUS_EXPR;
14785       break;
14786 
14787     case CPP_MULT:
14788       op = MULT_EXPR;
14789       break;
14790 
14791     case CPP_DIV:
14792       op = TRUNC_DIV_EXPR;
14793       break;
14794 
14795     case CPP_MOD:
14796       op = TRUNC_MOD_EXPR;
14797       break;
14798 
14799     case CPP_XOR:
14800       op = BIT_XOR_EXPR;
14801       break;
14802 
14803     case CPP_AND:
14804       op = BIT_AND_EXPR;
14805       break;
14806 
14807     case CPP_OR:
14808       op = BIT_IOR_EXPR;
14809       break;
14810 
14811     case CPP_COMPL:
14812       op = BIT_NOT_EXPR;
14813       break;
14814 
14815     case CPP_NOT:
14816       op = TRUTH_NOT_EXPR;
14817       break;
14818 
14819     case CPP_EQ:
14820       assop = true;
14821       op = NOP_EXPR;
14822       break;
14823 
14824     case CPP_LESS:
14825       op = LT_EXPR;
14826       break;
14827 
14828     case CPP_GREATER:
14829       op = GT_EXPR;
14830       break;
14831 
14832     case CPP_PLUS_EQ:
14833       assop = true;
14834       op = PLUS_EXPR;
14835       break;
14836 
14837     case CPP_MINUS_EQ:
14838       assop = true;
14839       op = MINUS_EXPR;
14840       break;
14841 
14842     case CPP_MULT_EQ:
14843       assop = true;
14844       op = MULT_EXPR;
14845       break;
14846 
14847     case CPP_DIV_EQ:
14848       assop = true;
14849       op = TRUNC_DIV_EXPR;
14850       break;
14851 
14852     case CPP_MOD_EQ:
14853       assop = true;
14854       op = TRUNC_MOD_EXPR;
14855       break;
14856 
14857     case CPP_XOR_EQ:
14858       assop = true;
14859       op = BIT_XOR_EXPR;
14860       break;
14861 
14862     case CPP_AND_EQ:
14863       assop = true;
14864       op = BIT_AND_EXPR;
14865       break;
14866 
14867     case CPP_OR_EQ:
14868       assop = true;
14869       op = BIT_IOR_EXPR;
14870       break;
14871 
14872     case CPP_LSHIFT:
14873       op = LSHIFT_EXPR;
14874       break;
14875 
14876     case CPP_RSHIFT:
14877       op = RSHIFT_EXPR;
14878       break;
14879 
14880     case CPP_LSHIFT_EQ:
14881       assop = true;
14882       op = LSHIFT_EXPR;
14883       break;
14884 
14885     case CPP_RSHIFT_EQ:
14886       assop = true;
14887       op = RSHIFT_EXPR;
14888       break;
14889 
14890     case CPP_EQ_EQ:
14891       op = EQ_EXPR;
14892       break;
14893 
14894     case CPP_NOT_EQ:
14895       op = NE_EXPR;
14896       break;
14897 
14898     case CPP_LESS_EQ:
14899       op = LE_EXPR;
14900       break;
14901 
14902     case CPP_GREATER_EQ:
14903       op = GE_EXPR;
14904       break;
14905 
14906     case CPP_AND_AND:
14907       op = TRUTH_ANDIF_EXPR;
14908       break;
14909 
14910     case CPP_OR_OR:
14911       op = TRUTH_ORIF_EXPR;
14912       break;
14913 
14914     case CPP_PLUS_PLUS:
14915       op = POSTINCREMENT_EXPR;
14916       break;
14917 
14918     case CPP_MINUS_MINUS:
14919       op = PREDECREMENT_EXPR;
14920       break;
14921 
14922     case CPP_COMMA:
14923       op = COMPOUND_EXPR;
14924       break;
14925 
14926     case CPP_DEREF_STAR:
14927       op = MEMBER_REF;
14928       break;
14929 
14930     case CPP_DEREF:
14931       op = COMPONENT_REF;
14932       break;
14933 
14934     case CPP_OPEN_PAREN:
14935       {
14936         /* Consume the `('.  */
14937         matching_parens parens;
14938         parens.consume_open (parser);
14939         /* Look for the matching `)'.  */
14940         parens.require_close (parser);
14941 	op = CALL_EXPR;
14942 	consumed = true;
14943 	break;
14944       }
14945 
14946     case CPP_OPEN_SQUARE:
14947       /* Consume the `['.  */
14948       cp_lexer_consume_token (parser->lexer);
14949       /* Look for the matching `]'.  */
14950       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14951       op = ARRAY_REF;
14952       consumed = true;
14953       break;
14954 
14955     case CPP_UTF8STRING:
14956     case CPP_UTF8STRING_USERDEF:
14957       utf8 = true;
14958       /* FALLTHRU */
14959     case CPP_STRING:
14960     case CPP_WSTRING:
14961     case CPP_STRING16:
14962     case CPP_STRING32:
14963     case CPP_STRING_USERDEF:
14964     case CPP_WSTRING_USERDEF:
14965     case CPP_STRING16_USERDEF:
14966     case CPP_STRING32_USERDEF:
14967       {
14968 	tree str, string_tree;
14969 	int sz, len;
14970 
14971 	if (cxx_dialect == cxx98)
14972 	  maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14973 
14974 	/* Consume the string.  */
14975 	str = cp_parser_string_literal (parser, /*translate=*/true,
14976 				      /*wide_ok=*/true, /*lookup_udlit=*/false);
14977 	if (str == error_mark_node)
14978 	  return error_mark_node;
14979 	else if (TREE_CODE (str) == USERDEF_LITERAL)
14980 	  {
14981 	    string_tree = USERDEF_LITERAL_VALUE (str);
14982 	    id = USERDEF_LITERAL_SUFFIX_ID (str);
14983 	  }
14984 	else
14985 	  {
14986 	    string_tree = str;
14987 	    /* Look for the suffix identifier.  */
14988 	    token = cp_lexer_peek_token (parser->lexer);
14989 	    if (token->type == CPP_NAME)
14990 	      id = cp_parser_identifier (parser);
14991 	    else if (token->type == CPP_KEYWORD)
14992 	      {
14993 		error ("unexpected keyword;"
14994 		       " remove space between quotes and suffix identifier");
14995 		return error_mark_node;
14996 	      }
14997 	    else
14998 	      {
14999 		error ("expected suffix identifier");
15000 		return error_mark_node;
15001 	      }
15002 	  }
15003 	sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15004 			       (TREE_TYPE (TREE_TYPE (string_tree))));
15005 	len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15006 	if (len != 0)
15007 	  {
15008 	    error ("expected empty string after %<operator%> keyword");
15009 	    return error_mark_node;
15010 	  }
15011 	if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15012 	    != char_type_node)
15013 	  {
15014 	    error ("invalid encoding prefix in literal operator");
15015 	    return error_mark_node;
15016 	  }
15017 	if (id != error_mark_node)
15018 	  {
15019 	    const char *name = IDENTIFIER_POINTER (id);
15020 	    id = cp_literal_operator_id (name);
15021 	  }
15022 	return id;
15023       }
15024 
15025     default:
15026       /* Anything else is an error.  */
15027       break;
15028     }
15029 
15030   /* If we have selected an identifier, we need to consume the
15031      operator token.  */
15032   if (op != ERROR_MARK)
15033     {
15034       id = ovl_op_identifier (assop, op);
15035       if (!consumed)
15036 	cp_lexer_consume_token (parser->lexer);
15037     }
15038   /* Otherwise, no valid operator name was present.  */
15039   else
15040     {
15041       cp_parser_error (parser, "expected operator");
15042       id = error_mark_node;
15043     }
15044 
15045   return cp_expr (id, start_loc);
15046 }
15047 
15048 /* Parse a template-declaration.
15049 
15050    template-declaration:
15051      export [opt] template < template-parameter-list > declaration
15052 
15053    If MEMBER_P is TRUE, this template-declaration occurs within a
15054    class-specifier.
15055 
15056    The grammar rule given by the standard isn't correct.  What
15057    is really meant is:
15058 
15059    template-declaration:
15060      export [opt] template-parameter-list-seq
15061        decl-specifier-seq [opt] init-declarator [opt] ;
15062      export [opt] template-parameter-list-seq
15063        function-definition
15064 
15065    template-parameter-list-seq:
15066      template-parameter-list-seq [opt]
15067      template < template-parameter-list >
15068 
15069    Concept Extensions:
15070 
15071    template-parameter-list-seq:
15072      template < template-parameter-list > requires-clause [opt]
15073 
15074    requires-clause:
15075      requires logical-or-expression  */
15076 
15077 static void
cp_parser_template_declaration(cp_parser * parser,bool member_p)15078 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15079 {
15080   /* Check for `export'.  */
15081   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15082     {
15083       /* Consume the `export' token.  */
15084       cp_lexer_consume_token (parser->lexer);
15085       /* Warn that we do not support `export'.  */
15086       warning (0, "keyword %<export%> not implemented, and will be ignored");
15087     }
15088 
15089   cp_parser_template_declaration_after_export (parser, member_p);
15090 }
15091 
15092 /* Parse a template-parameter-list.
15093 
15094    template-parameter-list:
15095      template-parameter
15096      template-parameter-list , template-parameter
15097 
15098    Returns a TREE_LIST.  Each node represents a template parameter.
15099    The nodes are connected via their TREE_CHAINs.  */
15100 
15101 static tree
cp_parser_template_parameter_list(cp_parser * parser)15102 cp_parser_template_parameter_list (cp_parser* parser)
15103 {
15104   tree parameter_list = NULL_TREE;
15105 
15106   begin_template_parm_list ();
15107 
15108   /* The loop below parses the template parms.  We first need to know
15109      the total number of template parms to be able to compute proper
15110      canonical types of each dependent type. So after the loop, when
15111      we know the total number of template parms,
15112      end_template_parm_list computes the proper canonical types and
15113      fixes up the dependent types accordingly.  */
15114   while (true)
15115     {
15116       tree parameter;
15117       bool is_non_type;
15118       bool is_parameter_pack;
15119       location_t parm_loc;
15120 
15121       /* Parse the template-parameter.  */
15122       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15123       parameter = cp_parser_template_parameter (parser,
15124                                                 &is_non_type,
15125                                                 &is_parameter_pack);
15126       /* Add it to the list.  */
15127       if (parameter != error_mark_node)
15128 	parameter_list = process_template_parm (parameter_list,
15129 						parm_loc,
15130 						parameter,
15131 						is_non_type,
15132 						is_parameter_pack);
15133       else
15134        {
15135          tree err_parm = build_tree_list (parameter, parameter);
15136          parameter_list = chainon (parameter_list, err_parm);
15137        }
15138 
15139       /* If the next token is not a `,', we're done.  */
15140       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15141 	break;
15142       /* Otherwise, consume the `,' token.  */
15143       cp_lexer_consume_token (parser->lexer);
15144     }
15145 
15146   return end_template_parm_list (parameter_list);
15147 }
15148 
15149 /* Parse a introduction-list.
15150 
15151    introduction-list:
15152      introduced-parameter
15153      introduction-list , introduced-parameter
15154 
15155    introduced-parameter:
15156      ...[opt] identifier
15157 
15158    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
15159    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
15160    WILDCARD_DECL will also have DECL_NAME set and token location in
15161    DECL_SOURCE_LOCATION.  */
15162 
15163 static tree
cp_parser_introduction_list(cp_parser * parser)15164 cp_parser_introduction_list (cp_parser *parser)
15165 {
15166   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15167 
15168   while (true)
15169     {
15170       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15171       if (is_pack)
15172 	cp_lexer_consume_token (parser->lexer);
15173 
15174       /* Build placeholder. */
15175       tree parm = build_nt (WILDCARD_DECL);
15176       DECL_SOURCE_LOCATION (parm)
15177 	= cp_lexer_peek_token (parser->lexer)->location;
15178       DECL_NAME (parm) = cp_parser_identifier (parser);
15179       WILDCARD_PACK_P (parm) = is_pack;
15180       vec_safe_push (introduction_vec, parm);
15181 
15182       /* If the next token is not a `,', we're done.  */
15183       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15184 	break;
15185       /* Otherwise, consume the `,' token.  */
15186       cp_lexer_consume_token (parser->lexer);
15187     }
15188 
15189   /* Convert the vec into a TREE_VEC.  */
15190   tree introduction_list = make_tree_vec (introduction_vec->length ());
15191   unsigned int n;
15192   tree parm;
15193   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15194     TREE_VEC_ELT (introduction_list, n) = parm;
15195 
15196   release_tree_vector (introduction_vec);
15197   return introduction_list;
15198 }
15199 
15200 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15201    is an abstract declarator. */
15202 
15203 static inline cp_declarator*
get_id_declarator(cp_declarator * declarator)15204 get_id_declarator (cp_declarator *declarator)
15205 {
15206   cp_declarator *d = declarator;
15207   while (d && d->kind != cdk_id)
15208     d = d->declarator;
15209   return d;
15210 }
15211 
15212 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15213    is an abstract declarator. */
15214 
15215 static inline tree
get_unqualified_id(cp_declarator * declarator)15216 get_unqualified_id (cp_declarator *declarator)
15217 {
15218   declarator = get_id_declarator (declarator);
15219   if (declarator)
15220     return declarator->u.id.unqualified_name;
15221   else
15222     return NULL_TREE;
15223 }
15224 
15225 /* Returns true if DECL represents a constrained-parameter.  */
15226 
15227 static inline bool
is_constrained_parameter(tree decl)15228 is_constrained_parameter (tree decl)
15229 {
15230   return (decl
15231           && TREE_CODE (decl) == TYPE_DECL
15232           && CONSTRAINED_PARM_CONCEPT (decl)
15233           && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15234 }
15235 
15236 /* Returns true if PARM declares a constrained-parameter. */
15237 
15238 static inline bool
is_constrained_parameter(cp_parameter_declarator * parm)15239 is_constrained_parameter (cp_parameter_declarator *parm)
15240 {
15241   return is_constrained_parameter (parm->decl_specifiers.type);
15242 }
15243 
15244 /* Check that the type parameter is only a declarator-id, and that its
15245    type is not cv-qualified. */
15246 
15247 bool
cp_parser_check_constrained_type_parm(cp_parser * parser,cp_parameter_declarator * parm)15248 cp_parser_check_constrained_type_parm (cp_parser *parser,
15249 				       cp_parameter_declarator *parm)
15250 {
15251   if (!parm->declarator)
15252     return true;
15253 
15254   if (parm->declarator->kind != cdk_id)
15255     {
15256       cp_parser_error (parser, "invalid constrained type parameter");
15257       return false;
15258     }
15259 
15260   /* Don't allow cv-qualified type parameters.  */
15261   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15262       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15263     {
15264       cp_parser_error (parser, "cv-qualified type parameter");
15265       return false;
15266     }
15267 
15268   return true;
15269 }
15270 
15271 /* Finish parsing/processing a template type parameter and checking
15272    various restrictions. */
15273 
15274 static inline tree
cp_parser_constrained_type_template_parm(cp_parser * parser,tree id,cp_parameter_declarator * parmdecl)15275 cp_parser_constrained_type_template_parm (cp_parser *parser,
15276                                           tree id,
15277                                           cp_parameter_declarator* parmdecl)
15278 {
15279   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15280     return finish_template_type_parm (class_type_node, id);
15281   else
15282     return error_mark_node;
15283 }
15284 
15285 static tree
finish_constrained_template_template_parm(tree proto,tree id)15286 finish_constrained_template_template_parm (tree proto, tree id)
15287 {
15288   /* FIXME: This should probably be copied, and we may need to adjust
15289      the template parameter depths.  */
15290   tree saved_parms = current_template_parms;
15291   begin_template_parm_list ();
15292   current_template_parms = DECL_TEMPLATE_PARMS (proto);
15293   end_template_parm_list ();
15294 
15295   tree parm = finish_template_template_parm (class_type_node, id);
15296   current_template_parms = saved_parms;
15297 
15298   return parm;
15299 }
15300 
15301 /* Finish parsing/processing a template template parameter by borrowing
15302    the template parameter list from the prototype parameter.  */
15303 
15304 static tree
cp_parser_constrained_template_template_parm(cp_parser * parser,tree proto,tree id,cp_parameter_declarator * parmdecl)15305 cp_parser_constrained_template_template_parm (cp_parser *parser,
15306                                               tree proto,
15307                                               tree id,
15308                                               cp_parameter_declarator *parmdecl)
15309 {
15310   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15311     return error_mark_node;
15312   return finish_constrained_template_template_parm (proto, id);
15313 }
15314 
15315 /* Create a new non-type template parameter from the given PARM
15316    declarator.  */
15317 
15318 static tree
constrained_non_type_template_parm(bool * is_non_type,cp_parameter_declarator * parm)15319 constrained_non_type_template_parm (bool *is_non_type,
15320                                     cp_parameter_declarator *parm)
15321 {
15322   *is_non_type = true;
15323   cp_declarator *decl = parm->declarator;
15324   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15325   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15326   return grokdeclarator (decl, specs, TPARM, 0, NULL);
15327 }
15328 
15329 /* Build a constrained template parameter based on the PARMDECL
15330    declarator. The type of PARMDECL is the constrained type, which
15331    refers to the prototype template parameter that ultimately
15332    specifies the type of the declared parameter. */
15333 
15334 static tree
finish_constrained_parameter(cp_parser * parser,cp_parameter_declarator * parmdecl,bool * is_non_type,bool * is_parameter_pack)15335 finish_constrained_parameter (cp_parser *parser,
15336                               cp_parameter_declarator *parmdecl,
15337                               bool *is_non_type,
15338                               bool *is_parameter_pack)
15339 {
15340   tree decl = parmdecl->decl_specifiers.type;
15341   tree id = get_unqualified_id (parmdecl->declarator);
15342   tree def = parmdecl->default_argument;
15343   tree proto = DECL_INITIAL (decl);
15344 
15345   /* A template parameter constrained by a variadic concept shall also
15346      be declared as a template parameter pack.  */
15347   bool is_variadic = template_parameter_pack_p (proto);
15348   if (is_variadic && !*is_parameter_pack)
15349     cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15350 
15351   /* Build the parameter. Return an error if the declarator was invalid. */
15352   tree parm;
15353   if (TREE_CODE (proto) == TYPE_DECL)
15354     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15355   else if (TREE_CODE (proto) == TEMPLATE_DECL)
15356     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15357 							 parmdecl);
15358   else
15359     parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15360   if (parm == error_mark_node)
15361     return error_mark_node;
15362 
15363   /* Finish the parameter decl and create a node attaching the
15364      default argument and constraint.  */
15365   parm = build_tree_list (def, parm);
15366   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15367 
15368   return parm;
15369 }
15370 
15371 /* Returns true if the parsed type actually represents the declaration
15372    of a type template-parameter.  */
15373 
15374 static inline bool
declares_constrained_type_template_parameter(tree type)15375 declares_constrained_type_template_parameter (tree type)
15376 {
15377   return (is_constrained_parameter (type)
15378 	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15379 }
15380 
15381 
15382 /* Returns true if the parsed type actually represents the declaration of
15383    a template template-parameter.  */
15384 
15385 static bool
declares_constrained_template_template_parameter(tree type)15386 declares_constrained_template_template_parameter (tree type)
15387 {
15388   return (is_constrained_parameter (type)
15389 	  && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15390 }
15391 
15392 /* Parse a default argument for a type template-parameter.
15393    Note that diagnostics are handled in cp_parser_template_parameter.  */
15394 
15395 static tree
cp_parser_default_type_template_argument(cp_parser * parser)15396 cp_parser_default_type_template_argument (cp_parser *parser)
15397 {
15398   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15399 
15400   /* Consume the `=' token.  */
15401   cp_lexer_consume_token (parser->lexer);
15402 
15403   cp_token *token = cp_lexer_peek_token (parser->lexer);
15404 
15405   /* Parse the default-argument.  */
15406   push_deferring_access_checks (dk_no_deferred);
15407   tree default_argument = cp_parser_type_id (parser);
15408   pop_deferring_access_checks ();
15409 
15410   if (flag_concepts && type_uses_auto (default_argument))
15411     {
15412       error_at (token->location,
15413 		"invalid use of %<auto%> in default template argument");
15414       return error_mark_node;
15415     }
15416 
15417   return default_argument;
15418 }
15419 
15420 /* Parse a default argument for a template template-parameter.  */
15421 
15422 static tree
cp_parser_default_template_template_argument(cp_parser * parser)15423 cp_parser_default_template_template_argument (cp_parser *parser)
15424 {
15425   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15426 
15427   bool is_template;
15428 
15429   /* Consume the `='.  */
15430   cp_lexer_consume_token (parser->lexer);
15431   /* Parse the id-expression.  */
15432   push_deferring_access_checks (dk_no_deferred);
15433   /* save token before parsing the id-expression, for error
15434      reporting */
15435   const cp_token* token = cp_lexer_peek_token (parser->lexer);
15436   tree default_argument
15437     = cp_parser_id_expression (parser,
15438                                /*template_keyword_p=*/false,
15439                                /*check_dependency_p=*/true,
15440                                /*template_p=*/&is_template,
15441                                /*declarator_p=*/false,
15442                                /*optional_p=*/false);
15443   if (TREE_CODE (default_argument) == TYPE_DECL)
15444     /* If the id-expression was a template-id that refers to
15445        a template-class, we already have the declaration here,
15446        so no further lookup is needed.  */
15447     ;
15448   else
15449     /* Look up the name.  */
15450     default_argument
15451       = cp_parser_lookup_name (parser, default_argument,
15452                                none_type,
15453                                /*is_template=*/is_template,
15454                                /*is_namespace=*/false,
15455                                /*check_dependency=*/true,
15456                                /*ambiguous_decls=*/NULL,
15457                                token->location);
15458   /* See if the default argument is valid.  */
15459   default_argument = check_template_template_default_arg (default_argument);
15460   pop_deferring_access_checks ();
15461   return default_argument;
15462 }
15463 
15464 /* Parse a template-parameter.
15465 
15466    template-parameter:
15467      type-parameter
15468      parameter-declaration
15469 
15470    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
15471    the parameter.  The TREE_PURPOSE is the default value, if any.
15472    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
15473    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
15474    set to true iff this parameter is a parameter pack. */
15475 
15476 static tree
cp_parser_template_parameter(cp_parser * parser,bool * is_non_type,bool * is_parameter_pack)15477 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15478                               bool *is_parameter_pack)
15479 {
15480   cp_token *token;
15481   cp_parameter_declarator *parameter_declarator;
15482   tree parm;
15483 
15484   /* Assume it is a type parameter or a template parameter.  */
15485   *is_non_type = false;
15486   /* Assume it not a parameter pack. */
15487   *is_parameter_pack = false;
15488   /* Peek at the next token.  */
15489   token = cp_lexer_peek_token (parser->lexer);
15490   /* If it is `template', we have a type-parameter.  */
15491   if (token->keyword == RID_TEMPLATE)
15492     return cp_parser_type_parameter (parser, is_parameter_pack);
15493   /* If it is `class' or `typename' we do not know yet whether it is a
15494      type parameter or a non-type parameter.  Consider:
15495 
15496        template <typename T, typename T::X X> ...
15497 
15498      or:
15499 
15500        template <class C, class D*> ...
15501 
15502      Here, the first parameter is a type parameter, and the second is
15503      a non-type parameter.  We can tell by looking at the token after
15504      the identifier -- if it is a `,', `=', or `>' then we have a type
15505      parameter.  */
15506   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15507     {
15508       /* Peek at the token after `class' or `typename'.  */
15509       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15510       /* If it's an ellipsis, we have a template type parameter
15511          pack. */
15512       if (token->type == CPP_ELLIPSIS)
15513         return cp_parser_type_parameter (parser, is_parameter_pack);
15514       /* If it's an identifier, skip it.  */
15515       if (token->type == CPP_NAME)
15516 	token = cp_lexer_peek_nth_token (parser->lexer, 3);
15517       /* Now, see if the token looks like the end of a template
15518 	 parameter.  */
15519       if (token->type == CPP_COMMA
15520 	  || token->type == CPP_EQ
15521 	  || token->type == CPP_GREATER)
15522 	return cp_parser_type_parameter (parser, is_parameter_pack);
15523     }
15524 
15525   /* Otherwise, it is a non-type parameter or a constrained parameter.
15526 
15527      [temp.param]
15528 
15529      When parsing a default template-argument for a non-type
15530      template-parameter, the first non-nested `>' is taken as the end
15531      of the template parameter-list rather than a greater-than
15532      operator.  */
15533   parameter_declarator
15534      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15535 					/*parenthesized_p=*/NULL);
15536 
15537   if (!parameter_declarator)
15538     return error_mark_node;
15539 
15540   /* If the parameter declaration is marked as a parameter pack, set
15541    *IS_PARAMETER_PACK to notify the caller.  */
15542   if (parameter_declarator->template_parameter_pack_p)
15543     *is_parameter_pack = true;
15544 
15545   if (parameter_declarator->default_argument)
15546     {
15547       /* Can happen in some cases of erroneous input (c++/34892).  */
15548       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15549 	/* Consume the `...' for better error recovery.  */
15550 	cp_lexer_consume_token (parser->lexer);
15551     }
15552 
15553   // The parameter may have been constrained.
15554   if (is_constrained_parameter (parameter_declarator))
15555     return finish_constrained_parameter (parser,
15556                                          parameter_declarator,
15557                                          is_non_type,
15558                                          is_parameter_pack);
15559 
15560   // Now we're sure that the parameter is a non-type parameter.
15561   *is_non_type = true;
15562 
15563   parm = grokdeclarator (parameter_declarator->declarator,
15564 			 &parameter_declarator->decl_specifiers,
15565 			 TPARM, /*initialized=*/0,
15566 			 /*attrlist=*/NULL);
15567   if (parm == error_mark_node)
15568     return error_mark_node;
15569 
15570   return build_tree_list (parameter_declarator->default_argument, parm);
15571 }
15572 
15573 /* Parse a type-parameter.
15574 
15575    type-parameter:
15576      class identifier [opt]
15577      class identifier [opt] = type-id
15578      typename identifier [opt]
15579      typename identifier [opt] = type-id
15580      template < template-parameter-list > class identifier [opt]
15581      template < template-parameter-list > class identifier [opt]
15582        = id-expression
15583 
15584    GNU Extension (variadic templates):
15585 
15586    type-parameter:
15587      class ... identifier [opt]
15588      typename ... identifier [opt]
15589 
15590    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
15591    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
15592    the declaration of the parameter.
15593 
15594    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15595 
15596 static tree
cp_parser_type_parameter(cp_parser * parser,bool * is_parameter_pack)15597 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15598 {
15599   cp_token *token;
15600   tree parameter;
15601 
15602   /* Look for a keyword to tell us what kind of parameter this is.  */
15603   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15604   if (!token)
15605     return error_mark_node;
15606 
15607   switch (token->keyword)
15608     {
15609     case RID_CLASS:
15610     case RID_TYPENAME:
15611       {
15612 	tree identifier;
15613 	tree default_argument;
15614 
15615         /* If the next token is an ellipsis, we have a template
15616            argument pack. */
15617         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15618           {
15619             /* Consume the `...' token. */
15620             cp_lexer_consume_token (parser->lexer);
15621             maybe_warn_variadic_templates ();
15622 
15623             *is_parameter_pack = true;
15624           }
15625 
15626 	/* If the next token is an identifier, then it names the
15627 	   parameter.  */
15628 	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15629 	  identifier = cp_parser_identifier (parser);
15630 	else
15631 	  identifier = NULL_TREE;
15632 
15633 	/* Create the parameter.  */
15634 	parameter = finish_template_type_parm (class_type_node, identifier);
15635 
15636 	/* If the next token is an `=', we have a default argument.  */
15637 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15638 	  {
15639 	    default_argument
15640 	      = cp_parser_default_type_template_argument (parser);
15641 
15642             /* Template parameter packs cannot have default
15643                arguments. */
15644             if (*is_parameter_pack)
15645               {
15646                 if (identifier)
15647                   error_at (token->location,
15648 			    "template parameter pack %qD cannot have a "
15649 			    "default argument", identifier);
15650                 else
15651                   error_at (token->location,
15652 			    "template parameter packs cannot have "
15653 			    "default arguments");
15654                 default_argument = NULL_TREE;
15655               }
15656 	    else if (check_for_bare_parameter_packs (default_argument))
15657 	      default_argument = error_mark_node;
15658 	  }
15659 	else
15660 	  default_argument = NULL_TREE;
15661 
15662 	/* Create the combined representation of the parameter and the
15663 	   default argument.  */
15664 	parameter = build_tree_list (default_argument, parameter);
15665       }
15666       break;
15667 
15668     case RID_TEMPLATE:
15669       {
15670 	tree identifier;
15671 	tree default_argument;
15672 
15673 	/* Look for the `<'.  */
15674 	cp_parser_require (parser, CPP_LESS, RT_LESS);
15675 	/* Parse the template-parameter-list.  */
15676 	cp_parser_template_parameter_list (parser);
15677 	/* Look for the `>'.  */
15678 	cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15679 
15680         // If template requirements are present, parse them.
15681 	if (flag_concepts)
15682           {
15683 	    tree reqs = get_shorthand_constraints (current_template_parms);
15684 	    if (tree r = cp_parser_requires_clause_opt (parser))
15685               reqs = conjoin_constraints (reqs, normalize_expression (r));
15686 	    TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15687           }
15688 
15689 	/* Look for the `class' or 'typename' keywords.  */
15690 	cp_parser_type_parameter_key (parser);
15691         /* If the next token is an ellipsis, we have a template
15692            argument pack. */
15693         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15694           {
15695             /* Consume the `...' token. */
15696             cp_lexer_consume_token (parser->lexer);
15697             maybe_warn_variadic_templates ();
15698 
15699             *is_parameter_pack = true;
15700           }
15701 	/* If the next token is an `=', then there is a
15702 	   default-argument.  If the next token is a `>', we are at
15703 	   the end of the parameter-list.  If the next token is a `,',
15704 	   then we are at the end of this parameter.  */
15705 	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15706 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15707 	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15708 	  {
15709 	    identifier = cp_parser_identifier (parser);
15710 	    /* Treat invalid names as if the parameter were nameless.  */
15711 	    if (identifier == error_mark_node)
15712 	      identifier = NULL_TREE;
15713 	  }
15714 	else
15715 	  identifier = NULL_TREE;
15716 
15717 	/* Create the template parameter.  */
15718 	parameter = finish_template_template_parm (class_type_node,
15719 						   identifier);
15720 
15721 	/* If the next token is an `=', then there is a
15722 	   default-argument.  */
15723 	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15724 	  {
15725 	    default_argument
15726 	      = cp_parser_default_template_template_argument (parser);
15727 
15728             /* Template parameter packs cannot have default
15729                arguments. */
15730             if (*is_parameter_pack)
15731               {
15732                 if (identifier)
15733                   error_at (token->location,
15734 			    "template parameter pack %qD cannot "
15735 			    "have a default argument",
15736 			    identifier);
15737                 else
15738                   error_at (token->location, "template parameter packs cannot "
15739 			    "have default arguments");
15740                 default_argument = NULL_TREE;
15741               }
15742 	  }
15743 	else
15744 	  default_argument = NULL_TREE;
15745 
15746 	/* Create the combined representation of the parameter and the
15747 	   default argument.  */
15748 	parameter = build_tree_list (default_argument, parameter);
15749       }
15750       break;
15751 
15752     default:
15753       gcc_unreachable ();
15754       break;
15755     }
15756 
15757   return parameter;
15758 }
15759 
15760 /* Parse a template-id.
15761 
15762    template-id:
15763      template-name < template-argument-list [opt] >
15764 
15765    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15766    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
15767    returned.  Otherwise, if the template-name names a function, or set
15768    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
15769    names a class, returns a TYPE_DECL for the specialization.
15770 
15771    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15772    uninstantiated templates.  */
15773 
15774 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)15775 cp_parser_template_id (cp_parser *parser,
15776 		       bool template_keyword_p,
15777 		       bool check_dependency_p,
15778 		       enum tag_types tag_type,
15779 		       bool is_declaration)
15780 {
15781   tree templ;
15782   tree arguments;
15783   tree template_id;
15784   cp_token_position start_of_id = 0;
15785   cp_token *next_token = NULL, *next_token_2 = NULL;
15786   bool is_identifier;
15787 
15788   /* If the next token corresponds to a template-id, there is no need
15789      to reparse it.  */
15790   cp_token *token = cp_lexer_peek_token (parser->lexer);
15791   if (token->type == CPP_TEMPLATE_ID)
15792     {
15793       cp_lexer_consume_token (parser->lexer);
15794       return saved_checks_value (token->u.tree_check_value);
15795     }
15796 
15797   /* Avoid performing name lookup if there is no possibility of
15798      finding a template-id.  */
15799   if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15800       || (token->type == CPP_NAME
15801 	  && !cp_parser_nth_token_starts_template_argument_list_p
15802 	       (parser, 2)))
15803     {
15804       cp_parser_error (parser, "expected template-id");
15805       return error_mark_node;
15806     }
15807 
15808   /* Remember where the template-id starts.  */
15809   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15810     start_of_id = cp_lexer_token_position (parser->lexer, false);
15811 
15812   push_deferring_access_checks (dk_deferred);
15813 
15814   /* Parse the template-name.  */
15815   is_identifier = false;
15816   templ = cp_parser_template_name (parser, template_keyword_p,
15817 				   check_dependency_p,
15818 				   is_declaration,
15819 				   tag_type,
15820 				   &is_identifier);
15821   if (templ == error_mark_node || is_identifier)
15822     {
15823       pop_deferring_access_checks ();
15824       return templ;
15825     }
15826 
15827   /* Since we're going to preserve any side-effects from this parse, set up a
15828      firewall to protect our callers from cp_parser_commit_to_tentative_parse
15829      in the template arguments.  */
15830   tentative_firewall firewall (parser);
15831 
15832   /* If we find the sequence `[:' after a template-name, it's probably
15833      a digraph-typo for `< ::'. Substitute the tokens and check if we can
15834      parse correctly the argument list.  */
15835   if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15836        == CPP_OPEN_SQUARE)
15837       && next_token->flags & DIGRAPH
15838       && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15839 	  == CPP_COLON)
15840       && !(next_token_2->flags & PREV_WHITE))
15841     {
15842       cp_parser_parse_tentatively (parser);
15843       /* Change `:' into `::'.  */
15844       next_token_2->type = CPP_SCOPE;
15845       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15846 	 CPP_LESS.  */
15847       cp_lexer_consume_token (parser->lexer);
15848 
15849       /* Parse the arguments.  */
15850       arguments = cp_parser_enclosed_template_argument_list (parser);
15851       if (!cp_parser_parse_definitely (parser))
15852 	{
15853 	  /* If we couldn't parse an argument list, then we revert our changes
15854 	     and return simply an error. Maybe this is not a template-id
15855 	     after all.  */
15856 	  next_token_2->type = CPP_COLON;
15857 	  cp_parser_error (parser, "expected %<<%>");
15858 	  pop_deferring_access_checks ();
15859 	  return error_mark_node;
15860 	}
15861       /* Otherwise, emit an error about the invalid digraph, but continue
15862 	 parsing because we got our argument list.  */
15863       if (permerror (next_token->location,
15864 		     "%<<::%> cannot begin a template-argument list"))
15865 	{
15866 	  static bool hint = false;
15867 	  inform (next_token->location,
15868 		  "%<<:%> is an alternate spelling for %<[%>."
15869 		  " Insert whitespace between %<<%> and %<::%>");
15870 	  if (!hint && !flag_permissive)
15871 	    {
15872 	      inform (next_token->location, "(if you use %<-fpermissive%> "
15873 		      "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15874 		      "accept your code)");
15875 	      hint = true;
15876 	    }
15877 	}
15878     }
15879   else
15880     {
15881       /* Look for the `<' that starts the template-argument-list.  */
15882       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15883 	{
15884 	  pop_deferring_access_checks ();
15885 	  return error_mark_node;
15886 	}
15887       /* Parse the arguments.  */
15888       arguments = cp_parser_enclosed_template_argument_list (parser);
15889     }
15890 
15891   /* Set the location to be of the form:
15892      template-name < template-argument-list [opt] >
15893      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15894      with caret == start at the start of the template-name,
15895      ranging until the closing '>'.  */
15896   location_t finish_loc
15897     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15898   location_t combined_loc
15899     = make_location (token->location, token->location, finish_loc);
15900 
15901   /* Check for concepts autos where they don't belong.  We could
15902      identify types in some cases of idnetifier TEMPL, looking ahead
15903      for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15904      types.  We reject them in functions, but if what we have is an
15905      identifier, even with none_type we can't conclude it's NOT a
15906      type, we have to wait for template substitution.  */
15907   if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15908     template_id = error_mark_node;
15909   /* Build a representation of the specialization.  */
15910   else if (identifier_p (templ))
15911     template_id = build_min_nt_loc (combined_loc,
15912 				    TEMPLATE_ID_EXPR,
15913 				    templ, arguments);
15914   else if (DECL_TYPE_TEMPLATE_P (templ)
15915 	   || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15916     {
15917       bool entering_scope;
15918       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15919 	 template (rather than some instantiation thereof) only if
15920 	 is not nested within some other construct.  For example, in
15921 	 "template <typename T> void f(T) { A<T>::", A<T> is just an
15922 	 instantiation of A.  */
15923       entering_scope = (template_parm_scope_p ()
15924 			&& cp_lexer_next_token_is (parser->lexer,
15925 						   CPP_SCOPE));
15926       template_id
15927 	= finish_template_type (templ, arguments, entering_scope);
15928     }
15929   /* A template-like identifier may be a partial concept id. */
15930   else if (flag_concepts
15931            && (template_id = (cp_parser_maybe_partial_concept_id
15932 			      (parser, templ, arguments))))
15933     return template_id;
15934   else if (variable_template_p (templ))
15935     {
15936       template_id = lookup_template_variable (templ, arguments);
15937       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15938 	SET_EXPR_LOCATION (template_id, combined_loc);
15939     }
15940   else
15941     {
15942       /* If it's not a class-template or a template-template, it should be
15943 	 a function-template.  */
15944       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15945 		   || TREE_CODE (templ) == OVERLOAD
15946 		   || BASELINK_P (templ)));
15947 
15948       template_id = lookup_template_function (templ, arguments);
15949       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15950 	SET_EXPR_LOCATION (template_id, combined_loc);
15951     }
15952 
15953   /* If parsing tentatively, replace the sequence of tokens that makes
15954      up the template-id with a CPP_TEMPLATE_ID token.  That way,
15955      should we re-parse the token stream, we will not have to repeat
15956      the effort required to do the parse, nor will we issue duplicate
15957      error messages about problems during instantiation of the
15958      template.  */
15959   if (start_of_id
15960       /* Don't do this if we had a parse error in a declarator; re-parsing
15961 	 might succeed if a name changes meaning (60361).  */
15962       && !(cp_parser_error_occurred (parser)
15963 	   && cp_parser_parsing_tentatively (parser)
15964 	   && parser->in_declarator_p))
15965     {
15966       /* Reset the contents of the START_OF_ID token.  */
15967       token->type = CPP_TEMPLATE_ID;
15968       token->location = combined_loc;
15969 
15970       /* We must mark the lookup as kept, so we don't throw it away on
15971 	 the first parse.  */
15972       if (is_overloaded_fn (template_id))
15973 	lookup_keep (get_fns (template_id), true);
15974 
15975       /* Retrieve any deferred checks.  Do not pop this access checks yet
15976 	 so the memory will not be reclaimed during token replacing below.  */
15977       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15978       token->u.tree_check_value->value = template_id;
15979       token->u.tree_check_value->checks = get_deferred_access_checks ();
15980       token->keyword = RID_MAX;
15981 
15982       /* Purge all subsequent tokens.  */
15983       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15984 
15985       /* ??? Can we actually assume that, if template_id ==
15986 	 error_mark_node, we will have issued a diagnostic to the
15987 	 user, as opposed to simply marking the tentative parse as
15988 	 failed?  */
15989       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15990 	error_at (token->location, "parse error in template argument list");
15991     }
15992 
15993   pop_to_parent_deferring_access_checks ();
15994   return template_id;
15995 }
15996 
15997 /* Parse a template-name.
15998 
15999    template-name:
16000      identifier
16001 
16002    The standard should actually say:
16003 
16004    template-name:
16005      identifier
16006      operator-function-id
16007 
16008    A defect report has been filed about this issue.
16009 
16010    A conversion-function-id cannot be a template name because they cannot
16011    be part of a template-id. In fact, looking at this code:
16012 
16013    a.operator K<int>()
16014 
16015    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16016    It is impossible to call a templated conversion-function-id with an
16017    explicit argument list, since the only allowed template parameter is
16018    the type to which it is converting.
16019 
16020    If TEMPLATE_KEYWORD_P is true, then we have just seen the
16021    `template' keyword, in a construction like:
16022 
16023      T::template f<3>()
16024 
16025    In that case `f' is taken to be a template-name, even though there
16026    is no way of knowing for sure.
16027 
16028    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16029    name refers to a set of overloaded functions, at least one of which
16030    is a template, or an IDENTIFIER_NODE with the name of the template,
16031    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
16032    names are looked up inside uninstantiated templates.  */
16033 
16034 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)16035 cp_parser_template_name (cp_parser* parser,
16036 			 bool template_keyword_p,
16037 			 bool check_dependency_p,
16038 			 bool is_declaration,
16039 			 enum tag_types tag_type,
16040 			 bool *is_identifier)
16041 {
16042   tree identifier;
16043   tree decl;
16044   cp_token *token = cp_lexer_peek_token (parser->lexer);
16045 
16046   /* If the next token is `operator', then we have either an
16047      operator-function-id or a conversion-function-id.  */
16048   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16049     {
16050       /* We don't know whether we're looking at an
16051 	 operator-function-id or a conversion-function-id.  */
16052       cp_parser_parse_tentatively (parser);
16053       /* Try an operator-function-id.  */
16054       identifier = cp_parser_operator_function_id (parser);
16055       /* If that didn't work, try a conversion-function-id.  */
16056       if (!cp_parser_parse_definitely (parser))
16057 	{
16058 	  cp_parser_error (parser, "expected template-name");
16059 	  return error_mark_node;
16060 	}
16061     }
16062   /* Look for the identifier.  */
16063   else
16064     identifier = cp_parser_identifier (parser);
16065 
16066   /* If we didn't find an identifier, we don't have a template-id.  */
16067   if (identifier == error_mark_node)
16068     return error_mark_node;
16069 
16070   /* If the name immediately followed the `template' keyword, then it
16071      is a template-name.  However, if the next token is not `<', then
16072      we do not treat it as a template-name, since it is not being used
16073      as part of a template-id.  This enables us to handle constructs
16074      like:
16075 
16076        template <typename T> struct S { S(); };
16077        template <typename T> S<T>::S();
16078 
16079      correctly.  We would treat `S' as a template -- if it were `S<T>'
16080      -- but we do not if there is no `<'.  */
16081 
16082   if (processing_template_decl
16083       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16084     {
16085       /* In a declaration, in a dependent context, we pretend that the
16086 	 "template" keyword was present in order to improve error
16087 	 recovery.  For example, given:
16088 
16089 	   template <typename T> void f(T::X<int>);
16090 
16091 	 we want to treat "X<int>" as a template-id.  */
16092       if (is_declaration
16093 	  && !template_keyword_p
16094 	  && parser->scope && TYPE_P (parser->scope)
16095 	  && check_dependency_p
16096 	  && dependent_scope_p (parser->scope)
16097 	  /* Do not do this for dtors (or ctors), since they never
16098 	     need the template keyword before their name.  */
16099 	  && !constructor_name_p (identifier, parser->scope))
16100 	{
16101 	  cp_token_position start = 0;
16102 
16103 	  /* Explain what went wrong.  */
16104 	  error_at (token->location, "non-template %qD used as template",
16105 		    identifier);
16106 	  inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16107 		  parser->scope, identifier);
16108 	  /* If parsing tentatively, find the location of the "<" token.  */
16109 	  if (cp_parser_simulate_error (parser))
16110 	    start = cp_lexer_token_position (parser->lexer, true);
16111 	  /* Parse the template arguments so that we can issue error
16112 	     messages about them.  */
16113 	  cp_lexer_consume_token (parser->lexer);
16114 	  cp_parser_enclosed_template_argument_list (parser);
16115 	  /* Skip tokens until we find a good place from which to
16116 	     continue parsing.  */
16117 	  cp_parser_skip_to_closing_parenthesis (parser,
16118 						 /*recovering=*/true,
16119 						 /*or_comma=*/true,
16120 						 /*consume_paren=*/false);
16121 	  /* If parsing tentatively, permanently remove the
16122 	     template argument list.  That will prevent duplicate
16123 	     error messages from being issued about the missing
16124 	     "template" keyword.  */
16125 	  if (start)
16126 	    cp_lexer_purge_tokens_after (parser->lexer, start);
16127 	  if (is_identifier)
16128 	    *is_identifier = true;
16129 	  parser->context->object_type = NULL_TREE;
16130 	  return identifier;
16131 	}
16132 
16133       /* If the "template" keyword is present, then there is generally
16134 	 no point in doing name-lookup, so we just return IDENTIFIER.
16135 	 But, if the qualifying scope is non-dependent then we can
16136 	 (and must) do name-lookup normally.  */
16137       if (template_keyword_p)
16138 	{
16139 	  tree scope = (parser->scope ? parser->scope
16140 			: parser->context->object_type);
16141 	  if (scope && TYPE_P (scope)
16142 	      && (!CLASS_TYPE_P (scope)
16143 		  || (check_dependency_p && dependent_type_p (scope))))
16144 	    {
16145 	      /* We're optimizing away the call to cp_parser_lookup_name, but
16146 		 we still need to do this.  */
16147 	      parser->context->object_type = NULL_TREE;
16148 	      return identifier;
16149 	    }
16150 	}
16151     }
16152 
16153   /* Look up the name.  */
16154   decl = cp_parser_lookup_name (parser, identifier,
16155 				tag_type,
16156 				/*is_template=*/true,
16157 				/*is_namespace=*/false,
16158 				check_dependency_p,
16159 				/*ambiguous_decls=*/NULL,
16160 				token->location);
16161 
16162   decl = strip_using_decl (decl);
16163 
16164   /* If DECL is a template, then the name was a template-name.  */
16165   if (TREE_CODE (decl) == TEMPLATE_DECL)
16166     {
16167       if (TREE_DEPRECATED (decl)
16168 	  && deprecated_state != DEPRECATED_SUPPRESS)
16169 	{
16170 	  tree d = DECL_TEMPLATE_RESULT (decl);
16171 	  tree attr;
16172 	  if (TREE_CODE (d) == TYPE_DECL)
16173 	    attr = lookup_attribute ("deprecated",
16174 				     TYPE_ATTRIBUTES (TREE_TYPE (d)));
16175 	  else
16176 	    attr = lookup_attribute ("deprecated",
16177 				     DECL_ATTRIBUTES (d));
16178 	  warn_deprecated_use (decl, attr);
16179 	}
16180     }
16181   else
16182     {
16183       /* The standard does not explicitly indicate whether a name that
16184 	 names a set of overloaded declarations, some of which are
16185 	 templates, is a template-name.  However, such a name should
16186 	 be a template-name; otherwise, there is no way to form a
16187 	 template-id for the overloaded templates.  */
16188       bool found = false;
16189 
16190       for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16191 	   !found && iter; ++iter)
16192 	if (TREE_CODE (*iter) == TEMPLATE_DECL)
16193 	  found = true;
16194 
16195       if (!found)
16196 	{
16197 	  /* The name does not name a template.  */
16198 	  cp_parser_error (parser, "expected template-name");
16199 	  return error_mark_node;
16200 	}
16201     }
16202 
16203   /* If DECL is dependent, and refers to a function, then just return
16204      its name; we will look it up again during template instantiation.  */
16205   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16206     {
16207       tree scope = ovl_scope (decl);
16208       if (TYPE_P (scope) && dependent_type_p (scope))
16209 	return identifier;
16210     }
16211 
16212   return decl;
16213 }
16214 
16215 /* Parse a template-argument-list.
16216 
16217    template-argument-list:
16218      template-argument ... [opt]
16219      template-argument-list , template-argument ... [opt]
16220 
16221    Returns a TREE_VEC containing the arguments.  */
16222 
16223 static tree
cp_parser_template_argument_list(cp_parser * parser)16224 cp_parser_template_argument_list (cp_parser* parser)
16225 {
16226   tree fixed_args[10];
16227   unsigned n_args = 0;
16228   unsigned alloced = 10;
16229   tree *arg_ary = fixed_args;
16230   tree vec;
16231   bool saved_in_template_argument_list_p;
16232   bool saved_ice_p;
16233   bool saved_non_ice_p;
16234 
16235   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16236   parser->in_template_argument_list_p = true;
16237   /* Even if the template-id appears in an integral
16238      constant-expression, the contents of the argument list do
16239      not.  */
16240   saved_ice_p = parser->integral_constant_expression_p;
16241   parser->integral_constant_expression_p = false;
16242   saved_non_ice_p = parser->non_integral_constant_expression_p;
16243   parser->non_integral_constant_expression_p = false;
16244 
16245   /* Parse the arguments.  */
16246   do
16247     {
16248       tree argument;
16249 
16250       if (n_args)
16251 	/* Consume the comma.  */
16252 	cp_lexer_consume_token (parser->lexer);
16253 
16254       /* Parse the template-argument.  */
16255       argument = cp_parser_template_argument (parser);
16256 
16257       /* If the next token is an ellipsis, we're expanding a template
16258          argument pack. */
16259       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16260         {
16261 	  if (argument == error_mark_node)
16262 	    {
16263 	      cp_token *token = cp_lexer_peek_token (parser->lexer);
16264 	      error_at (token->location,
16265 			"expected parameter pack before %<...%>");
16266 	    }
16267           /* Consume the `...' token. */
16268           cp_lexer_consume_token (parser->lexer);
16269 
16270           /* Make the argument into a TYPE_PACK_EXPANSION or
16271              EXPR_PACK_EXPANSION. */
16272           argument = make_pack_expansion (argument);
16273         }
16274 
16275       if (n_args == alloced)
16276 	{
16277 	  alloced *= 2;
16278 
16279 	  if (arg_ary == fixed_args)
16280 	    {
16281 	      arg_ary = XNEWVEC (tree, alloced);
16282 	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16283 	    }
16284 	  else
16285 	    arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16286 	}
16287       arg_ary[n_args++] = argument;
16288     }
16289   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16290 
16291   vec = make_tree_vec (n_args);
16292 
16293   while (n_args--)
16294     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16295 
16296   if (arg_ary != fixed_args)
16297     free (arg_ary);
16298   parser->non_integral_constant_expression_p = saved_non_ice_p;
16299   parser->integral_constant_expression_p = saved_ice_p;
16300   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16301   if (CHECKING_P)
16302     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16303   return vec;
16304 }
16305 
16306 /* Parse a template-argument.
16307 
16308    template-argument:
16309      assignment-expression
16310      type-id
16311      id-expression
16312 
16313    The representation is that of an assignment-expression, type-id, or
16314    id-expression -- except that the qualified id-expression is
16315    evaluated, so that the value returned is either a DECL or an
16316    OVERLOAD.
16317 
16318    Although the standard says "assignment-expression", it forbids
16319    throw-expressions or assignments in the template argument.
16320    Therefore, we use "conditional-expression" instead.  */
16321 
16322 static tree
cp_parser_template_argument(cp_parser * parser)16323 cp_parser_template_argument (cp_parser* parser)
16324 {
16325   tree argument;
16326   bool template_p;
16327   bool address_p;
16328   bool maybe_type_id = false;
16329   cp_token *token = NULL, *argument_start_token = NULL;
16330   location_t loc = 0;
16331   cp_id_kind idk;
16332 
16333   /* There's really no way to know what we're looking at, so we just
16334      try each alternative in order.
16335 
16336        [temp.arg]
16337 
16338        In a template-argument, an ambiguity between a type-id and an
16339        expression is resolved to a type-id, regardless of the form of
16340        the corresponding template-parameter.
16341 
16342      Therefore, we try a type-id first.  */
16343   cp_parser_parse_tentatively (parser);
16344   argument = cp_parser_template_type_arg (parser);
16345   /* If there was no error parsing the type-id but the next token is a
16346      '>>', our behavior depends on which dialect of C++ we're
16347      parsing. In C++98, we probably found a typo for '> >'. But there
16348      are type-id which are also valid expressions. For instance:
16349 
16350      struct X { int operator >> (int); };
16351      template <int V> struct Foo {};
16352      Foo<X () >> 5> r;
16353 
16354      Here 'X()' is a valid type-id of a function type, but the user just
16355      wanted to write the expression "X() >> 5". Thus, we remember that we
16356      found a valid type-id, but we still try to parse the argument as an
16357      expression to see what happens.
16358 
16359      In C++0x, the '>>' will be considered two separate '>'
16360      tokens.  */
16361   if (!cp_parser_error_occurred (parser)
16362       && cxx_dialect == cxx98
16363       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16364     {
16365       maybe_type_id = true;
16366       cp_parser_abort_tentative_parse (parser);
16367     }
16368   else
16369     {
16370       /* If the next token isn't a `,' or a `>', then this argument wasn't
16371       really finished. This means that the argument is not a valid
16372       type-id.  */
16373       if (!cp_parser_next_token_ends_template_argument_p (parser))
16374 	cp_parser_error (parser, "expected template-argument");
16375       /* If that worked, we're done.  */
16376       if (cp_parser_parse_definitely (parser))
16377 	return argument;
16378     }
16379   /* We're still not sure what the argument will be.  */
16380   cp_parser_parse_tentatively (parser);
16381   /* Try a template.  */
16382   argument_start_token = cp_lexer_peek_token (parser->lexer);
16383   argument = cp_parser_id_expression (parser,
16384 				      /*template_keyword_p=*/false,
16385 				      /*check_dependency_p=*/true,
16386 				      &template_p,
16387 				      /*declarator_p=*/false,
16388 				      /*optional_p=*/false);
16389   /* If the next token isn't a `,' or a `>', then this argument wasn't
16390      really finished.  */
16391   if (!cp_parser_next_token_ends_template_argument_p (parser))
16392     cp_parser_error (parser, "expected template-argument");
16393   if (!cp_parser_error_occurred (parser))
16394     {
16395       /* Figure out what is being referred to.  If the id-expression
16396 	 was for a class template specialization, then we will have a
16397 	 TYPE_DECL at this point.  There is no need to do name lookup
16398 	 at this point in that case.  */
16399       if (TREE_CODE (argument) != TYPE_DECL)
16400 	argument = cp_parser_lookup_name (parser, argument,
16401 					  none_type,
16402 					  /*is_template=*/template_p,
16403 					  /*is_namespace=*/false,
16404 					  /*check_dependency=*/true,
16405 					  /*ambiguous_decls=*/NULL,
16406 					  argument_start_token->location);
16407       /* Handle a constrained-type-specifier for a non-type template
16408 	 parameter.  */
16409       if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16410 	argument = decl;
16411       else if (TREE_CODE (argument) != TEMPLATE_DECL
16412 	       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16413 	cp_parser_error (parser, "expected template-name");
16414     }
16415   if (cp_parser_parse_definitely (parser))
16416     {
16417       if (TREE_DEPRECATED (argument))
16418 	warn_deprecated_use (argument, NULL_TREE);
16419       return argument;
16420     }
16421   /* It must be a non-type argument.  In C++17 any constant-expression is
16422      allowed.  */
16423   if (cxx_dialect > cxx14)
16424     goto general_expr;
16425 
16426   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16427 
16428      -- an integral constant-expression of integral or enumeration
16429 	type; or
16430 
16431      -- the name of a non-type template-parameter; or
16432 
16433      -- the name of an object or function with external linkage...
16434 
16435      -- the address of an object or function with external linkage...
16436 
16437      -- a pointer to member...  */
16438   /* Look for a non-type template parameter.  */
16439   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16440     {
16441       cp_parser_parse_tentatively (parser);
16442       argument = cp_parser_primary_expression (parser,
16443 					       /*address_p=*/false,
16444 					       /*cast_p=*/false,
16445 					       /*template_arg_p=*/true,
16446 					       &idk);
16447       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16448 	  || !cp_parser_next_token_ends_template_argument_p (parser))
16449 	cp_parser_simulate_error (parser);
16450       if (cp_parser_parse_definitely (parser))
16451 	return argument;
16452     }
16453 
16454   /* If the next token is "&", the argument must be the address of an
16455      object or function with external linkage.  */
16456   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16457   if (address_p)
16458     {
16459       loc = cp_lexer_peek_token (parser->lexer)->location;
16460       cp_lexer_consume_token (parser->lexer);
16461     }
16462   /* See if we might have an id-expression.  */
16463   token = cp_lexer_peek_token (parser->lexer);
16464   if (token->type == CPP_NAME
16465       || token->keyword == RID_OPERATOR
16466       || token->type == CPP_SCOPE
16467       || token->type == CPP_TEMPLATE_ID
16468       || token->type == CPP_NESTED_NAME_SPECIFIER)
16469     {
16470       cp_parser_parse_tentatively (parser);
16471       argument = cp_parser_primary_expression (parser,
16472 					       address_p,
16473 					       /*cast_p=*/false,
16474 					       /*template_arg_p=*/true,
16475 					       &idk);
16476       if (cp_parser_error_occurred (parser)
16477 	  || !cp_parser_next_token_ends_template_argument_p (parser))
16478 	cp_parser_abort_tentative_parse (parser);
16479       else
16480 	{
16481 	  tree probe;
16482 
16483 	  if (INDIRECT_REF_P (argument))
16484 	    {
16485 	      /* Strip the dereference temporarily.  */
16486 	      gcc_assert (REFERENCE_REF_P (argument));
16487 	      argument = TREE_OPERAND (argument, 0);
16488 	    }
16489 
16490 	  /* If we're in a template, we represent a qualified-id referring
16491 	     to a static data member as a SCOPE_REF even if the scope isn't
16492 	     dependent so that we can check access control later.  */
16493 	  probe = argument;
16494 	  if (TREE_CODE (probe) == SCOPE_REF)
16495 	    probe = TREE_OPERAND (probe, 1);
16496 	  if (VAR_P (probe))
16497 	    {
16498 	      /* A variable without external linkage might still be a
16499 		 valid constant-expression, so no error is issued here
16500 		 if the external-linkage check fails.  */
16501 	      if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16502 		cp_parser_simulate_error (parser);
16503 	    }
16504 	  else if (is_overloaded_fn (argument))
16505 	    /* All overloaded functions are allowed; if the external
16506 	       linkage test does not pass, an error will be issued
16507 	       later.  */
16508 	    ;
16509 	  else if (address_p
16510 		   && (TREE_CODE (argument) == OFFSET_REF
16511 		       || TREE_CODE (argument) == SCOPE_REF))
16512 	    /* A pointer-to-member.  */
16513 	    ;
16514 	  else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16515 	    ;
16516 	  else
16517 	    cp_parser_simulate_error (parser);
16518 
16519 	  if (cp_parser_parse_definitely (parser))
16520 	    {
16521 	      if (address_p)
16522 		argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16523 					     tf_warning_or_error);
16524 	      else
16525 		argument = convert_from_reference (argument);
16526 	      return argument;
16527 	    }
16528 	}
16529     }
16530   /* If the argument started with "&", there are no other valid
16531      alternatives at this point.  */
16532   if (address_p)
16533     {
16534       cp_parser_error (parser, "invalid non-type template argument");
16535       return error_mark_node;
16536     }
16537 
16538  general_expr:
16539   /* If the argument wasn't successfully parsed as a type-id followed
16540      by '>>', the argument can only be a constant expression now.
16541      Otherwise, we try parsing the constant-expression tentatively,
16542      because the argument could really be a type-id.  */
16543   if (maybe_type_id)
16544     cp_parser_parse_tentatively (parser);
16545 
16546   if (cxx_dialect <= cxx14)
16547     argument = cp_parser_constant_expression (parser);
16548   else
16549     {
16550       /* With C++17 generalized non-type template arguments we need to handle
16551 	 lvalue constant expressions, too.  */
16552       argument = cp_parser_assignment_expression (parser);
16553       require_potential_constant_expression (argument);
16554     }
16555 
16556   if (!maybe_type_id)
16557     return argument;
16558   if (!cp_parser_next_token_ends_template_argument_p (parser))
16559     cp_parser_error (parser, "expected template-argument");
16560   if (cp_parser_parse_definitely (parser))
16561     return argument;
16562   /* We did our best to parse the argument as a non type-id, but that
16563      was the only alternative that matched (albeit with a '>' after
16564      it). We can assume it's just a typo from the user, and a
16565      diagnostic will then be issued.  */
16566   return cp_parser_template_type_arg (parser);
16567 }
16568 
16569 /* Parse an explicit-instantiation.
16570 
16571    explicit-instantiation:
16572      template declaration
16573 
16574    Although the standard says `declaration', what it really means is:
16575 
16576    explicit-instantiation:
16577      template decl-specifier-seq [opt] declarator [opt] ;
16578 
16579    Things like `template int S<int>::i = 5, int S<double>::j;' are not
16580    supposed to be allowed.  A defect report has been filed about this
16581    issue.
16582 
16583    GNU Extension:
16584 
16585    explicit-instantiation:
16586      storage-class-specifier template
16587        decl-specifier-seq [opt] declarator [opt] ;
16588      function-specifier template
16589        decl-specifier-seq [opt] declarator [opt] ;  */
16590 
16591 static void
cp_parser_explicit_instantiation(cp_parser * parser)16592 cp_parser_explicit_instantiation (cp_parser* parser)
16593 {
16594   int declares_class_or_enum;
16595   cp_decl_specifier_seq decl_specifiers;
16596   tree extension_specifier = NULL_TREE;
16597 
16598   timevar_push (TV_TEMPLATE_INST);
16599 
16600   /* Look for an (optional) storage-class-specifier or
16601      function-specifier.  */
16602   if (cp_parser_allow_gnu_extensions_p (parser))
16603     {
16604       extension_specifier
16605 	= cp_parser_storage_class_specifier_opt (parser);
16606       if (!extension_specifier)
16607 	extension_specifier
16608 	  = cp_parser_function_specifier_opt (parser,
16609 					      /*decl_specs=*/NULL);
16610     }
16611 
16612   /* Look for the `template' keyword.  */
16613   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16614   /* Let the front end know that we are processing an explicit
16615      instantiation.  */
16616   begin_explicit_instantiation ();
16617   /* [temp.explicit] says that we are supposed to ignore access
16618      control while processing explicit instantiation directives.  */
16619   push_deferring_access_checks (dk_no_check);
16620   /* Parse a decl-specifier-seq.  */
16621   cp_parser_decl_specifier_seq (parser,
16622 				CP_PARSER_FLAGS_OPTIONAL,
16623 				&decl_specifiers,
16624 				&declares_class_or_enum);
16625   /* If there was exactly one decl-specifier, and it declared a class,
16626      and there's no declarator, then we have an explicit type
16627      instantiation.  */
16628   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16629     {
16630       tree type;
16631 
16632       type = check_tag_decl (&decl_specifiers,
16633 			     /*explicit_type_instantiation_p=*/true);
16634       /* Turn access control back on for names used during
16635 	 template instantiation.  */
16636       pop_deferring_access_checks ();
16637       if (type)
16638 	do_type_instantiation (type, extension_specifier,
16639 			       /*complain=*/tf_error);
16640     }
16641   else
16642     {
16643       cp_declarator *declarator;
16644       tree decl;
16645 
16646       /* Parse the declarator.  */
16647       declarator
16648 	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16649 				/*ctor_dtor_or_conv_p=*/NULL,
16650 				/*parenthesized_p=*/NULL,
16651 				/*member_p=*/false,
16652 				/*friend_p=*/false);
16653       if (declares_class_or_enum & 2)
16654 	cp_parser_check_for_definition_in_return_type (declarator,
16655 						       decl_specifiers.type,
16656 						       decl_specifiers.locations[ds_type_spec]);
16657       if (declarator != cp_error_declarator)
16658 	{
16659 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16660 	    permerror (decl_specifiers.locations[ds_inline],
16661 		       "explicit instantiation shall not use"
16662 		       " %<inline%> specifier");
16663 	  if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16664 	    permerror (decl_specifiers.locations[ds_constexpr],
16665 		       "explicit instantiation shall not use"
16666 		       " %<constexpr%> specifier");
16667 
16668 	  decl = grokdeclarator (declarator, &decl_specifiers,
16669 				 NORMAL, 0, &decl_specifiers.attributes);
16670 	  /* Turn access control back on for names used during
16671 	     template instantiation.  */
16672 	  pop_deferring_access_checks ();
16673 	  /* Do the explicit instantiation.  */
16674 	  do_decl_instantiation (decl, extension_specifier);
16675 	}
16676       else
16677 	{
16678 	  pop_deferring_access_checks ();
16679 	  /* Skip the body of the explicit instantiation.  */
16680 	  cp_parser_skip_to_end_of_statement (parser);
16681 	}
16682     }
16683   /* We're done with the instantiation.  */
16684   end_explicit_instantiation ();
16685 
16686   cp_parser_consume_semicolon_at_end_of_statement (parser);
16687 
16688   timevar_pop (TV_TEMPLATE_INST);
16689 }
16690 
16691 /* Parse an explicit-specialization.
16692 
16693    explicit-specialization:
16694      template < > declaration
16695 
16696    Although the standard says `declaration', what it really means is:
16697 
16698    explicit-specialization:
16699      template <> decl-specifier [opt] init-declarator [opt] ;
16700      template <> function-definition
16701      template <> explicit-specialization
16702      template <> template-declaration  */
16703 
16704 static void
cp_parser_explicit_specialization(cp_parser * parser)16705 cp_parser_explicit_specialization (cp_parser* parser)
16706 {
16707   bool need_lang_pop;
16708   cp_token *token = cp_lexer_peek_token (parser->lexer);
16709 
16710   /* Look for the `template' keyword.  */
16711   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16712   /* Look for the `<'.  */
16713   cp_parser_require (parser, CPP_LESS, RT_LESS);
16714   /* Look for the `>'.  */
16715   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16716   /* We have processed another parameter list.  */
16717   ++parser->num_template_parameter_lists;
16718   /* [temp]
16719 
16720      A template ... explicit specialization ... shall not have C
16721      linkage.  */
16722   if (current_lang_name == lang_name_c)
16723     {
16724       error_at (token->location, "template specialization with C linkage");
16725       maybe_show_extern_c_location ();
16726       /* Give it C++ linkage to avoid confusing other parts of the
16727 	 front end.  */
16728       push_lang_context (lang_name_cplusplus);
16729       need_lang_pop = true;
16730     }
16731   else
16732     need_lang_pop = false;
16733   /* Let the front end know that we are beginning a specialization.  */
16734   if (!begin_specialization ())
16735     {
16736       end_specialization ();
16737       return;
16738     }
16739 
16740   /* If the next keyword is `template', we need to figure out whether
16741      or not we're looking a template-declaration.  */
16742   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16743     {
16744       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16745 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16746 	cp_parser_template_declaration_after_export (parser,
16747 						     /*member_p=*/false);
16748       else
16749 	cp_parser_explicit_specialization (parser);
16750     }
16751   else
16752     /* Parse the dependent declaration.  */
16753     cp_parser_single_declaration (parser,
16754 				  /*checks=*/NULL,
16755 				  /*member_p=*/false,
16756 				  /*explicit_specialization_p=*/true,
16757 				  /*friend_p=*/NULL);
16758   /* We're done with the specialization.  */
16759   end_specialization ();
16760   /* For the erroneous case of a template with C linkage, we pushed an
16761      implicit C++ linkage scope; exit that scope now.  */
16762   if (need_lang_pop)
16763     pop_lang_context ();
16764   /* We're done with this parameter list.  */
16765   --parser->num_template_parameter_lists;
16766 }
16767 
16768 /* Parse a type-specifier.
16769 
16770    type-specifier:
16771      simple-type-specifier
16772      class-specifier
16773      enum-specifier
16774      elaborated-type-specifier
16775      cv-qualifier
16776 
16777    GNU Extension:
16778 
16779    type-specifier:
16780      __complex__
16781 
16782    Returns a representation of the type-specifier.  For a
16783    class-specifier, enum-specifier, or elaborated-type-specifier, a
16784    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16785 
16786    The parser flags FLAGS is used to control type-specifier parsing.
16787 
16788    If IS_DECLARATION is TRUE, then this type-specifier is appearing
16789    in a decl-specifier-seq.
16790 
16791    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16792    class-specifier, enum-specifier, or elaborated-type-specifier, then
16793    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
16794    if a type is declared; 2 if it is defined.  Otherwise, it is set to
16795    zero.
16796 
16797    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16798    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
16799    is set to FALSE.  */
16800 
16801 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)16802 cp_parser_type_specifier (cp_parser* parser,
16803 			  cp_parser_flags flags,
16804 			  cp_decl_specifier_seq *decl_specs,
16805 			  bool is_declaration,
16806 			  int* declares_class_or_enum,
16807 			  bool* is_cv_qualifier)
16808 {
16809   tree type_spec = NULL_TREE;
16810   cp_token *token;
16811   enum rid keyword;
16812   cp_decl_spec ds = ds_last;
16813 
16814   /* Assume this type-specifier does not declare a new type.  */
16815   if (declares_class_or_enum)
16816     *declares_class_or_enum = 0;
16817   /* And that it does not specify a cv-qualifier.  */
16818   if (is_cv_qualifier)
16819     *is_cv_qualifier = false;
16820   /* Peek at the next token.  */
16821   token = cp_lexer_peek_token (parser->lexer);
16822 
16823   /* If we're looking at a keyword, we can use that to guide the
16824      production we choose.  */
16825   keyword = token->keyword;
16826   switch (keyword)
16827     {
16828     case RID_ENUM:
16829       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16830 	goto elaborated_type_specifier;
16831 
16832       /* Look for the enum-specifier.  */
16833       type_spec = cp_parser_enum_specifier (parser);
16834       /* If that worked, we're done.  */
16835       if (type_spec)
16836 	{
16837 	  if (declares_class_or_enum)
16838 	    *declares_class_or_enum = 2;
16839 	  if (decl_specs)
16840 	    cp_parser_set_decl_spec_type (decl_specs,
16841 					  type_spec,
16842 					  token,
16843 					  /*type_definition_p=*/true);
16844 	  return type_spec;
16845 	}
16846       else
16847 	goto elaborated_type_specifier;
16848 
16849       /* Any of these indicate either a class-specifier, or an
16850 	 elaborated-type-specifier.  */
16851     case RID_CLASS:
16852     case RID_STRUCT:
16853     case RID_UNION:
16854       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16855 	goto elaborated_type_specifier;
16856 
16857       /* Parse tentatively so that we can back up if we don't find a
16858 	 class-specifier.  */
16859       cp_parser_parse_tentatively (parser);
16860       /* Look for the class-specifier.  */
16861       type_spec = cp_parser_class_specifier (parser);
16862       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16863       /* If that worked, we're done.  */
16864       if (cp_parser_parse_definitely (parser))
16865 	{
16866 	  if (declares_class_or_enum)
16867 	    *declares_class_or_enum = 2;
16868 	  if (decl_specs)
16869 	    cp_parser_set_decl_spec_type (decl_specs,
16870 					  type_spec,
16871 					  token,
16872 					  /*type_definition_p=*/true);
16873 	  return type_spec;
16874 	}
16875 
16876       /* Fall through.  */
16877     elaborated_type_specifier:
16878       /* We're declaring (not defining) a class or enum.  */
16879       if (declares_class_or_enum)
16880 	*declares_class_or_enum = 1;
16881 
16882       /* Fall through.  */
16883     case RID_TYPENAME:
16884       /* Look for an elaborated-type-specifier.  */
16885       type_spec
16886 	= (cp_parser_elaborated_type_specifier
16887 	   (parser,
16888 	    decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16889 	    is_declaration));
16890       if (decl_specs)
16891 	cp_parser_set_decl_spec_type (decl_specs,
16892 				      type_spec,
16893 				      token,
16894 				      /*type_definition_p=*/false);
16895       return type_spec;
16896 
16897     case RID_CONST:
16898       ds = ds_const;
16899       if (is_cv_qualifier)
16900 	*is_cv_qualifier = true;
16901       break;
16902 
16903     case RID_VOLATILE:
16904       ds = ds_volatile;
16905       if (is_cv_qualifier)
16906 	*is_cv_qualifier = true;
16907       break;
16908 
16909     case RID_RESTRICT:
16910       ds = ds_restrict;
16911       if (is_cv_qualifier)
16912 	*is_cv_qualifier = true;
16913       break;
16914 
16915     case RID_COMPLEX:
16916       /* The `__complex__' keyword is a GNU extension.  */
16917       ds = ds_complex;
16918       break;
16919 
16920     default:
16921       break;
16922     }
16923 
16924   /* Handle simple keywords.  */
16925   if (ds != ds_last)
16926     {
16927       if (decl_specs)
16928 	{
16929 	  set_and_check_decl_spec_loc (decl_specs, ds, token);
16930 	  decl_specs->any_specifiers_p = true;
16931 	}
16932       return cp_lexer_consume_token (parser->lexer)->u.value;
16933     }
16934 
16935   /* If we do not already have a type-specifier, assume we are looking
16936      at a simple-type-specifier.  */
16937   type_spec = cp_parser_simple_type_specifier (parser,
16938 					       decl_specs,
16939 					       flags);
16940 
16941   /* If we didn't find a type-specifier, and a type-specifier was not
16942      optional in this context, issue an error message.  */
16943   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16944     {
16945       cp_parser_error (parser, "expected type specifier");
16946       return error_mark_node;
16947     }
16948 
16949   return type_spec;
16950 }
16951 
16952 /* Parse a simple-type-specifier.
16953 
16954    simple-type-specifier:
16955      :: [opt] nested-name-specifier [opt] type-name
16956      :: [opt] nested-name-specifier template template-id
16957      char
16958      wchar_t
16959      bool
16960      short
16961      int
16962      long
16963      signed
16964      unsigned
16965      float
16966      double
16967      void
16968 
16969    C++11 Extension:
16970 
16971    simple-type-specifier:
16972      auto
16973      decltype ( expression )
16974      char16_t
16975      char32_t
16976      __underlying_type ( type-id )
16977 
16978    C++17 extension:
16979 
16980      nested-name-specifier(opt) template-name
16981 
16982    GNU Extension:
16983 
16984    simple-type-specifier:
16985      __int128
16986      __typeof__ unary-expression
16987      __typeof__ ( type-id )
16988      __typeof__ ( type-id ) { initializer-list , [opt] }
16989 
16990    Concepts Extension:
16991 
16992    simple-type-specifier:
16993      constrained-type-specifier
16994 
16995    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
16996    appropriately updated.  */
16997 
16998 static tree
cp_parser_simple_type_specifier(cp_parser * parser,cp_decl_specifier_seq * decl_specs,cp_parser_flags flags)16999 cp_parser_simple_type_specifier (cp_parser* parser,
17000 				 cp_decl_specifier_seq *decl_specs,
17001 				 cp_parser_flags flags)
17002 {
17003   tree type = NULL_TREE;
17004   cp_token *token;
17005   int idx;
17006 
17007   /* Peek at the next token.  */
17008   token = cp_lexer_peek_token (parser->lexer);
17009 
17010   /* If we're looking at a keyword, things are easy.  */
17011   switch (token->keyword)
17012     {
17013     case RID_CHAR:
17014       if (decl_specs)
17015 	decl_specs->explicit_char_p = true;
17016       type = char_type_node;
17017       break;
17018     case RID_CHAR16:
17019       type = char16_type_node;
17020       break;
17021     case RID_CHAR32:
17022       type = char32_type_node;
17023       break;
17024     case RID_WCHAR:
17025       type = wchar_type_node;
17026       break;
17027     case RID_BOOL:
17028       type = boolean_type_node;
17029       break;
17030     case RID_SHORT:
17031       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17032       type = short_integer_type_node;
17033       break;
17034     case RID_INT:
17035       if (decl_specs)
17036 	decl_specs->explicit_int_p = true;
17037       type = integer_type_node;
17038       break;
17039     case RID_INT_N_0:
17040     case RID_INT_N_1:
17041     case RID_INT_N_2:
17042     case RID_INT_N_3:
17043       idx = token->keyword - RID_INT_N_0;
17044       if (! int_n_enabled_p [idx])
17045 	break;
17046       if (decl_specs)
17047 	{
17048 	  decl_specs->explicit_intN_p = true;
17049 	  decl_specs->int_n_idx = idx;
17050 	}
17051       type = int_n_trees [idx].signed_type;
17052       break;
17053     case RID_LONG:
17054       if (decl_specs)
17055 	set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17056       type = long_integer_type_node;
17057       break;
17058     case RID_SIGNED:
17059       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17060       type = integer_type_node;
17061       break;
17062     case RID_UNSIGNED:
17063       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17064       type = unsigned_type_node;
17065       break;
17066     case RID_FLOAT:
17067       type = float_type_node;
17068       break;
17069     case RID_DOUBLE:
17070       type = double_type_node;
17071       break;
17072     case RID_VOID:
17073       type = void_type_node;
17074       break;
17075 
17076     case RID_AUTO:
17077       maybe_warn_cpp0x (CPP0X_AUTO);
17078       if (parser->auto_is_implicit_function_template_parm_p)
17079 	{
17080 	  /* The 'auto' might be the placeholder return type for a function decl
17081 	     with trailing return type.  */
17082 	  bool have_trailing_return_fn_decl = false;
17083 
17084 	  cp_parser_parse_tentatively (parser);
17085 	  cp_lexer_consume_token (parser->lexer);
17086 	  while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17087 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17088 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17089 		 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17090 	    {
17091 	      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17092 		{
17093 		  cp_lexer_consume_token (parser->lexer);
17094 		  cp_parser_skip_to_closing_parenthesis (parser,
17095 							 /*recovering*/false,
17096 							 /*or_comma*/false,
17097 							 /*consume_paren*/true);
17098 		  continue;
17099 		}
17100 
17101 	      if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17102 		{
17103 		  have_trailing_return_fn_decl = true;
17104 		  break;
17105 		}
17106 
17107 	      cp_lexer_consume_token (parser->lexer);
17108 	    }
17109 	  cp_parser_abort_tentative_parse (parser);
17110 
17111 	  if (have_trailing_return_fn_decl)
17112 	    {
17113 	      type = make_auto ();
17114 	      break;
17115 	    }
17116 
17117 	  if (cxx_dialect >= cxx14)
17118 	    {
17119 	      type = synthesize_implicit_template_parm (parser, NULL_TREE);
17120 	      type = TREE_TYPE (type);
17121 	    }
17122 	  else
17123 	    type = error_mark_node;
17124 
17125 	  if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17126 	    {
17127 	      if (cxx_dialect < cxx14)
17128 		error_at (token->location,
17129 			 "use of %<auto%> in lambda parameter declaration "
17130 			 "only available with "
17131 			 "-std=c++14 or -std=gnu++14");
17132 	    }
17133 	  else if (cxx_dialect < cxx14)
17134 	    error_at (token->location,
17135 		     "use of %<auto%> in parameter declaration "
17136 		     "only available with "
17137 		     "-std=c++14 or -std=gnu++14");
17138 	  else if (!flag_concepts)
17139 	    pedwarn (token->location, 0,
17140 		     "use of %<auto%> in parameter declaration "
17141 		     "only available with -fconcepts");
17142 	}
17143       else
17144 	type = make_auto ();
17145       break;
17146 
17147     case RID_DECLTYPE:
17148       /* Since DR 743, decltype can either be a simple-type-specifier by
17149 	 itself or begin a nested-name-specifier.  Parsing it will replace
17150 	 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17151 	 handling below decide what to do.  */
17152       cp_parser_decltype (parser);
17153       cp_lexer_set_token_position (parser->lexer, token);
17154       break;
17155 
17156     case RID_TYPEOF:
17157       /* Consume the `typeof' token.  */
17158       cp_lexer_consume_token (parser->lexer);
17159       /* Parse the operand to `typeof'.  */
17160       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17161       /* If it is not already a TYPE, take its type.  */
17162       if (!TYPE_P (type))
17163 	type = finish_typeof (type);
17164 
17165       if (decl_specs)
17166 	cp_parser_set_decl_spec_type (decl_specs, type,
17167 				      token,
17168 				      /*type_definition_p=*/false);
17169 
17170       return type;
17171 
17172     case RID_UNDERLYING_TYPE:
17173       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17174       if (decl_specs)
17175 	cp_parser_set_decl_spec_type (decl_specs, type,
17176 				      token,
17177 				      /*type_definition_p=*/false);
17178 
17179       return type;
17180 
17181     case RID_BASES:
17182     case RID_DIRECT_BASES:
17183       type = cp_parser_trait_expr (parser, token->keyword);
17184       if (decl_specs)
17185        cp_parser_set_decl_spec_type (decl_specs, type,
17186                                      token,
17187                                      /*type_definition_p=*/false);
17188       return type;
17189     default:
17190       break;
17191     }
17192 
17193   /* If token is an already-parsed decltype not followed by ::,
17194      it's a simple-type-specifier.  */
17195   if (token->type == CPP_DECLTYPE
17196       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17197     {
17198       type = saved_checks_value (token->u.tree_check_value);
17199       if (decl_specs)
17200 	{
17201 	  cp_parser_set_decl_spec_type (decl_specs, type,
17202 					token,
17203 					/*type_definition_p=*/false);
17204 	  /* Remember that we are handling a decltype in order to
17205 	     implement the resolution of DR 1510 when the argument
17206 	     isn't instantiation dependent.  */
17207 	  decl_specs->decltype_p = true;
17208 	}
17209       cp_lexer_consume_token (parser->lexer);
17210       return type;
17211     }
17212 
17213   /* If the type-specifier was for a built-in type, we're done.  */
17214   if (type)
17215     {
17216       /* Record the type.  */
17217       if (decl_specs
17218 	  && (token->keyword != RID_SIGNED
17219 	      && token->keyword != RID_UNSIGNED
17220 	      && token->keyword != RID_SHORT
17221 	      && token->keyword != RID_LONG))
17222 	cp_parser_set_decl_spec_type (decl_specs,
17223 				      type,
17224 				      token,
17225 				      /*type_definition_p=*/false);
17226       if (decl_specs)
17227 	decl_specs->any_specifiers_p = true;
17228 
17229       /* Consume the token.  */
17230       cp_lexer_consume_token (parser->lexer);
17231 
17232       if (type == error_mark_node)
17233 	return error_mark_node;
17234 
17235       /* There is no valid C++ program where a non-template type is
17236 	 followed by a "<".  That usually indicates that the user thought
17237 	 that the type was a template.  */
17238       cp_parser_check_for_invalid_template_id (parser, type, none_type,
17239 					       token->location);
17240 
17241       return TYPE_NAME (type);
17242     }
17243 
17244   /* The type-specifier must be a user-defined type.  */
17245   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17246     {
17247       bool qualified_p;
17248       bool global_p;
17249 
17250       /* Don't gobble tokens or issue error messages if this is an
17251 	 optional type-specifier.  */
17252       if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17253 	cp_parser_parse_tentatively (parser);
17254 
17255       token = cp_lexer_peek_token (parser->lexer);
17256 
17257       /* Look for the optional `::' operator.  */
17258       global_p
17259 	= (cp_parser_global_scope_opt (parser,
17260 				       /*current_scope_valid_p=*/false)
17261 	   != NULL_TREE);
17262       /* Look for the nested-name specifier.  */
17263       qualified_p
17264 	= (cp_parser_nested_name_specifier_opt (parser,
17265 						/*typename_keyword_p=*/false,
17266 						/*check_dependency_p=*/true,
17267 						/*type_p=*/false,
17268 						/*is_declaration=*/false)
17269 	   != NULL_TREE);
17270       /* If we have seen a nested-name-specifier, and the next token
17271 	 is `template', then we are using the template-id production.  */
17272       if (parser->scope
17273 	  && cp_parser_optional_template_keyword (parser))
17274 	{
17275 	  /* Look for the template-id.  */
17276 	  type = cp_parser_template_id (parser,
17277 					/*template_keyword_p=*/true,
17278 					/*check_dependency_p=*/true,
17279 					none_type,
17280 					/*is_declaration=*/false);
17281 	  /* If the template-id did not name a type, we are out of
17282 	     luck.  */
17283 	  if (TREE_CODE (type) != TYPE_DECL)
17284 	    {
17285 	      cp_parser_error (parser, "expected template-id for type");
17286 	      type = NULL_TREE;
17287 	    }
17288 	}
17289       /* Otherwise, look for a type-name.  */
17290       else
17291 	type = cp_parser_type_name (parser);
17292       /* Keep track of all name-lookups performed in class scopes.  */
17293       if (type
17294 	  && !global_p
17295 	  && !qualified_p
17296 	  && TREE_CODE (type) == TYPE_DECL
17297 	  && identifier_p (DECL_NAME (type)))
17298 	maybe_note_name_used_in_class (DECL_NAME (type), type);
17299       /* If it didn't work out, we don't have a TYPE.  */
17300       if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17301 	  && !cp_parser_parse_definitely (parser))
17302 	type = NULL_TREE;
17303       if (!type && cxx_dialect >= cxx17)
17304 	{
17305 	  if (flags & CP_PARSER_FLAGS_OPTIONAL)
17306 	    cp_parser_parse_tentatively (parser);
17307 
17308 	  cp_parser_global_scope_opt (parser,
17309 				      /*current_scope_valid_p=*/false);
17310 	  cp_parser_nested_name_specifier_opt (parser,
17311 					       /*typename_keyword_p=*/false,
17312 					       /*check_dependency_p=*/true,
17313 					       /*type_p=*/false,
17314 					       /*is_declaration=*/false);
17315 	  tree name = cp_parser_identifier (parser);
17316 	  if (name && TREE_CODE (name) == IDENTIFIER_NODE
17317 	      && parser->scope != error_mark_node)
17318 	    {
17319 	      tree tmpl = cp_parser_lookup_name (parser, name,
17320 						 none_type,
17321 						 /*is_template=*/false,
17322 						 /*is_namespace=*/false,
17323 						 /*check_dependency=*/true,
17324 						 /*ambiguous_decls=*/NULL,
17325 						 token->location);
17326 	      if (tmpl && tmpl != error_mark_node
17327 		  && (DECL_CLASS_TEMPLATE_P (tmpl)
17328 		      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17329 		type = make_template_placeholder (tmpl);
17330 	      else
17331 		{
17332 		  type = error_mark_node;
17333 		  if (!cp_parser_simulate_error (parser))
17334 		    cp_parser_name_lookup_error (parser, name, tmpl,
17335 						 NLE_TYPE, token->location);
17336 		}
17337 	    }
17338 	  else
17339 	    type = error_mark_node;
17340 
17341 	  if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17342 	      && !cp_parser_parse_definitely (parser))
17343 	    type = NULL_TREE;
17344 	}
17345       if (type && decl_specs)
17346 	cp_parser_set_decl_spec_type (decl_specs, type,
17347 				      token,
17348 				      /*type_definition_p=*/false);
17349     }
17350 
17351   /* If we didn't get a type-name, issue an error message.  */
17352   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17353     {
17354       cp_parser_error (parser, "expected type-name");
17355       return error_mark_node;
17356     }
17357 
17358   if (type && type != error_mark_node)
17359     {
17360       /* See if TYPE is an Objective-C type, and if so, parse and
17361 	 accept any protocol references following it.  Do this before
17362 	 the cp_parser_check_for_invalid_template_id() call, because
17363 	 Objective-C types can be followed by '<...>' which would
17364 	 enclose protocol names rather than template arguments, and so
17365 	 everything is fine.  */
17366       if (c_dialect_objc () && !parser->scope
17367 	  && (objc_is_id (type) || objc_is_class_name (type)))
17368 	{
17369 	  tree protos = cp_parser_objc_protocol_refs_opt (parser);
17370 	  tree qual_type = objc_get_protocol_qualified_type (type, protos);
17371 
17372 	  /* Clobber the "unqualified" type previously entered into
17373 	     DECL_SPECS with the new, improved protocol-qualified version.  */
17374 	  if (decl_specs)
17375 	    decl_specs->type = qual_type;
17376 
17377 	  return qual_type;
17378 	}
17379 
17380       /* There is no valid C++ program where a non-template type is
17381 	 followed by a "<".  That usually indicates that the user
17382 	 thought that the type was a template.  */
17383       cp_parser_check_for_invalid_template_id (parser, type,
17384 					       none_type,
17385 					       token->location);
17386     }
17387 
17388   return type;
17389 }
17390 
17391 /* Parse a type-name.
17392 
17393    type-name:
17394      class-name
17395      enum-name
17396      typedef-name
17397      simple-template-id [in c++0x]
17398 
17399    enum-name:
17400      identifier
17401 
17402    typedef-name:
17403      identifier
17404 
17405   Concepts:
17406 
17407    type-name:
17408      concept-name
17409      partial-concept-id
17410 
17411    concept-name:
17412      identifier
17413 
17414    Returns a TYPE_DECL for the type.  */
17415 
17416 static tree
cp_parser_type_name(cp_parser * parser)17417 cp_parser_type_name (cp_parser* parser)
17418 {
17419   return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17420 }
17421 
17422 /* See above. */
17423 static tree
cp_parser_type_name(cp_parser * parser,bool typename_keyword_p)17424 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17425 {
17426   tree type_decl;
17427 
17428   /* We can't know yet whether it is a class-name or not.  */
17429   cp_parser_parse_tentatively (parser);
17430   /* Try a class-name.  */
17431   type_decl = cp_parser_class_name (parser,
17432 				    typename_keyword_p,
17433 				    /*template_keyword_p=*/false,
17434 				    none_type,
17435 				    /*check_dependency_p=*/true,
17436 				    /*class_head_p=*/false,
17437 				    /*is_declaration=*/false);
17438   /* If it's not a class-name, keep looking.  */
17439   if (!cp_parser_parse_definitely (parser))
17440     {
17441       if (cxx_dialect < cxx11)
17442 	/* It must be a typedef-name or an enum-name.  */
17443 	return cp_parser_nonclass_name (parser);
17444 
17445       cp_parser_parse_tentatively (parser);
17446       /* It is either a simple-template-id representing an
17447 	 instantiation of an alias template...  */
17448       type_decl = cp_parser_template_id (parser,
17449 					 /*template_keyword_p=*/false,
17450 					 /*check_dependency_p=*/true,
17451 					 none_type,
17452 					 /*is_declaration=*/false);
17453       /* Note that this must be an instantiation of an alias template
17454 	 because [temp.names]/6 says:
17455 
17456 	     A template-id that names an alias template specialization
17457 	     is a type-name.
17458 
17459 	 Whereas [temp.names]/7 says:
17460 
17461 	     A simple-template-id that names a class template
17462 	     specialization is a class-name.
17463 
17464          With concepts, this could also be a partial-concept-id that
17465          declares a non-type template parameter. */
17466       if (type_decl != NULL_TREE
17467 	  && TREE_CODE (type_decl) == TYPE_DECL
17468 	  && TYPE_DECL_ALIAS_P (type_decl))
17469 	gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17470       else if (is_constrained_parameter (type_decl))
17471         /* Don't do anything. */ ;
17472       else
17473 	cp_parser_simulate_error (parser);
17474 
17475       if (!cp_parser_parse_definitely (parser))
17476 	/* ... Or a typedef-name or an enum-name.  */
17477 	return cp_parser_nonclass_name (parser);
17478     }
17479 
17480   return type_decl;
17481 }
17482 
17483 /*  Check if DECL and ARGS can form a constrained-type-specifier.
17484     If ARGS is non-null, we try to form a concept check of the
17485     form DECL<?, ARGS> where ? is a wildcard that matches any
17486     kind of template argument. If ARGS is NULL, then we try to
17487     form a concept check of the form DECL<?>. */
17488 
17489 static tree
cp_parser_maybe_constrained_type_specifier(cp_parser * parser,tree decl,tree args)17490 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17491 					    tree decl, tree args)
17492 {
17493   gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17494 
17495   /* If we a constrained-type-specifier cannot be deduced. */
17496   if (parser->prevent_constrained_type_specifiers)
17497     return NULL_TREE;
17498 
17499   /* A constrained type specifier can only be found in an
17500      overload set or as a reference to a template declaration.
17501 
17502      FIXME: This might be masking a bug.  It's possible that
17503      that the deduction below is causing template specializations
17504      to be formed with the wildcard as an argument.  */
17505   if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17506     return NULL_TREE;
17507 
17508   /* Try to build a call expression that evaluates the
17509      concept. This can fail if the overload set refers
17510      only to non-templates. */
17511   tree placeholder = build_nt (WILDCARD_DECL);
17512   tree check = build_concept_check (decl, placeholder, args);
17513   if (check == error_mark_node)
17514     return NULL_TREE;
17515 
17516   /* Deduce the checked constraint and the prototype parameter.
17517 
17518      FIXME: In certain cases, failure to deduce should be a
17519      diagnosable error.  */
17520   tree conc;
17521   tree proto;
17522   if (!deduce_constrained_parameter (check, conc, proto))
17523     return NULL_TREE;
17524 
17525   /* In template parameter scope, this results in a constrained
17526      parameter. Return a descriptor of that parm. */
17527   if (processing_template_parmlist)
17528     return build_constrained_parameter (conc, proto, args);
17529 
17530   /* In a parameter-declaration-clause, constrained-type
17531      specifiers result in invented template parameters.  */
17532   if (parser->auto_is_implicit_function_template_parm_p)
17533     {
17534       tree x = build_constrained_parameter (conc, proto, args);
17535       return synthesize_implicit_template_parm (parser, x);
17536     }
17537   else
17538     {
17539      /* Otherwise, we're in a context where the constrained
17540         type name is deduced and the constraint applies
17541         after deduction. */
17542       return make_constrained_auto (conc, args);
17543     }
17544 
17545   return NULL_TREE;
17546 }
17547 
17548 /* If DECL refers to a concept, return a TYPE_DECL representing
17549    the result of using the constrained type specifier in the
17550    current context.  DECL refers to a concept if
17551 
17552   - it is an overload set containing a function concept taking a single
17553     type argument, or
17554 
17555   - it is a variable concept taking a single type argument.  */
17556 
17557 static tree
cp_parser_maybe_concept_name(cp_parser * parser,tree decl)17558 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17559 {
17560   if (flag_concepts
17561       && (TREE_CODE (decl) == OVERLOAD
17562 	  || BASELINK_P (decl)
17563 	  || variable_concept_p (decl)))
17564     return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17565   else
17566     return NULL_TREE;
17567 }
17568 
17569 /* Check if DECL and ARGS form a partial-concept-id.  If so,
17570    assign ID to the resulting constrained placeholder.
17571 
17572    Returns true if the partial-concept-id designates a placeholder
17573    and false otherwise. Note that *id is set to NULL_TREE in
17574    this case. */
17575 
17576 static tree
cp_parser_maybe_partial_concept_id(cp_parser * parser,tree decl,tree args)17577 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17578 {
17579   return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17580 }
17581 
17582 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17583    or a concept-name.
17584 
17585    enum-name:
17586      identifier
17587 
17588    typedef-name:
17589      identifier
17590 
17591    concept-name:
17592      identifier
17593 
17594    Returns a TYPE_DECL for the type.  */
17595 
17596 static tree
cp_parser_nonclass_name(cp_parser * parser)17597 cp_parser_nonclass_name (cp_parser* parser)
17598 {
17599   tree type_decl;
17600   tree identifier;
17601 
17602   cp_token *token = cp_lexer_peek_token (parser->lexer);
17603   identifier = cp_parser_identifier (parser);
17604   if (identifier == error_mark_node)
17605     return error_mark_node;
17606 
17607   /* Look up the type-name.  */
17608   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17609 
17610   type_decl = strip_using_decl (type_decl);
17611 
17612   /* If we found an overload set, then it may refer to a concept-name. */
17613   if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17614     type_decl = decl;
17615 
17616   if (TREE_CODE (type_decl) != TYPE_DECL
17617       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17618     {
17619       /* See if this is an Objective-C type.  */
17620       tree protos = cp_parser_objc_protocol_refs_opt (parser);
17621       tree type = objc_get_protocol_qualified_type (identifier, protos);
17622       if (type)
17623 	type_decl = TYPE_NAME (type);
17624     }
17625 
17626   /* Issue an error if we did not find a type-name.  */
17627   if (TREE_CODE (type_decl) != TYPE_DECL
17628       /* In Objective-C, we have the complication that class names are
17629 	 normally type names and start declarations (eg, the
17630 	 "NSObject" in "NSObject *object;"), but can be used in an
17631 	 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17632 	 is an expression.  So, a classname followed by a dot is not a
17633 	 valid type-name.  */
17634       || (objc_is_class_name (TREE_TYPE (type_decl))
17635 	  && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17636     {
17637       if (!cp_parser_simulate_error (parser))
17638 	cp_parser_name_lookup_error (parser, identifier, type_decl,
17639 				     NLE_TYPE, token->location);
17640       return error_mark_node;
17641     }
17642   /* Remember that the name was used in the definition of the
17643      current class so that we can check later to see if the
17644      meaning would have been different after the class was
17645      entirely defined.  */
17646   else if (type_decl != error_mark_node
17647 	   && !parser->scope)
17648     maybe_note_name_used_in_class (identifier, type_decl);
17649 
17650   return type_decl;
17651 }
17652 
17653 /* Parse an elaborated-type-specifier.  Note that the grammar given
17654    here incorporates the resolution to DR68.
17655 
17656    elaborated-type-specifier:
17657      class-key :: [opt] nested-name-specifier [opt] identifier
17658      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17659      enum-key :: [opt] nested-name-specifier [opt] identifier
17660      typename :: [opt] nested-name-specifier identifier
17661      typename :: [opt] nested-name-specifier template [opt]
17662        template-id
17663 
17664    GNU extension:
17665 
17666    elaborated-type-specifier:
17667      class-key attributes :: [opt] nested-name-specifier [opt] identifier
17668      class-key attributes :: [opt] nested-name-specifier [opt]
17669 	       template [opt] template-id
17670      enum attributes :: [opt] nested-name-specifier [opt] identifier
17671 
17672    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17673    declared `friend'.  If IS_DECLARATION is TRUE, then this
17674    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17675    something is being declared.
17676 
17677    Returns the TYPE specified.  */
17678 
17679 static tree
cp_parser_elaborated_type_specifier(cp_parser * parser,bool is_friend,bool is_declaration)17680 cp_parser_elaborated_type_specifier (cp_parser* parser,
17681 				     bool is_friend,
17682 				     bool is_declaration)
17683 {
17684   enum tag_types tag_type;
17685   tree identifier;
17686   tree type = NULL_TREE;
17687   tree attributes = NULL_TREE;
17688   tree globalscope;
17689   cp_token *token = NULL;
17690 
17691   /* See if we're looking at the `enum' keyword.  */
17692   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17693     {
17694       /* Consume the `enum' token.  */
17695       cp_lexer_consume_token (parser->lexer);
17696       /* Remember that it's an enumeration type.  */
17697       tag_type = enum_type;
17698       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17699 	 enums) is used here.  */
17700       cp_token *token = cp_lexer_peek_token (parser->lexer);
17701       if (cp_parser_is_keyword (token, RID_CLASS)
17702 	  || cp_parser_is_keyword (token, RID_STRUCT))
17703 	{
17704 	  gcc_rich_location richloc (token->location);
17705 	  richloc.add_range (input_location, false);
17706 	  richloc.add_fixit_remove ();
17707 	  pedwarn (&richloc, 0, "elaborated-type-specifier for "
17708 		   "a scoped enum must not use the %qD keyword",
17709 		   token->u.value);
17710 	  /* Consume the `struct' or `class' and parse it anyway.  */
17711 	  cp_lexer_consume_token (parser->lexer);
17712 	}
17713       /* Parse the attributes.  */
17714       attributes = cp_parser_attributes_opt (parser);
17715     }
17716   /* Or, it might be `typename'.  */
17717   else if (cp_lexer_next_token_is_keyword (parser->lexer,
17718 					   RID_TYPENAME))
17719     {
17720       /* Consume the `typename' token.  */
17721       cp_lexer_consume_token (parser->lexer);
17722       /* Remember that it's a `typename' type.  */
17723       tag_type = typename_type;
17724     }
17725   /* Otherwise it must be a class-key.  */
17726   else
17727     {
17728       tag_type = cp_parser_class_key (parser);
17729       if (tag_type == none_type)
17730 	return error_mark_node;
17731       /* Parse the attributes.  */
17732       attributes = cp_parser_attributes_opt (parser);
17733     }
17734 
17735   /* Look for the `::' operator.  */
17736   globalscope =  cp_parser_global_scope_opt (parser,
17737 					     /*current_scope_valid_p=*/false);
17738   /* Look for the nested-name-specifier.  */
17739   tree nested_name_specifier;
17740   if (tag_type == typename_type && !globalscope)
17741     {
17742       nested_name_specifier
17743 	= cp_parser_nested_name_specifier (parser,
17744 					   /*typename_keyword_p=*/true,
17745 					   /*check_dependency_p=*/true,
17746 					   /*type_p=*/true,
17747 					   is_declaration);
17748       if (!nested_name_specifier)
17749 	return error_mark_node;
17750     }
17751   else
17752     /* Even though `typename' is not present, the proposed resolution
17753        to Core Issue 180 says that in `class A<T>::B', `B' should be
17754        considered a type-name, even if `A<T>' is dependent.  */
17755     nested_name_specifier
17756       = cp_parser_nested_name_specifier_opt (parser,
17757 					     /*typename_keyword_p=*/true,
17758 					     /*check_dependency_p=*/true,
17759 					     /*type_p=*/true,
17760 					     is_declaration);
17761  /* For everything but enumeration types, consider a template-id.
17762     For an enumeration type, consider only a plain identifier.  */
17763   if (tag_type != enum_type)
17764     {
17765       bool template_p = false;
17766       tree decl;
17767 
17768       /* Allow the `template' keyword.  */
17769       template_p = cp_parser_optional_template_keyword (parser);
17770       /* If we didn't see `template', we don't know if there's a
17771 	 template-id or not.  */
17772       if (!template_p)
17773 	cp_parser_parse_tentatively (parser);
17774       /* Parse the template-id.  */
17775       token = cp_lexer_peek_token (parser->lexer);
17776       decl = cp_parser_template_id (parser, template_p,
17777 				    /*check_dependency_p=*/true,
17778 				    tag_type,
17779 				    is_declaration);
17780       /* If we didn't find a template-id, look for an ordinary
17781 	 identifier.  */
17782       if (!template_p && !cp_parser_parse_definitely (parser))
17783 	;
17784       /* We can get here when cp_parser_template_id, called by
17785 	 cp_parser_class_name with tag_type == none_type, succeeds
17786 	 and caches a BASELINK.  Then, when called again here,
17787 	 instead of failing and returning an error_mark_node
17788 	 returns it (see template/typename17.C in C++11).
17789 	 ??? Could we diagnose this earlier?  */
17790       else if (tag_type == typename_type && BASELINK_P (decl))
17791 	{
17792 	  cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17793 	  type = error_mark_node;
17794 	}
17795       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17796 	 in effect, then we must assume that, upon instantiation, the
17797 	 template will correspond to a class.  */
17798       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17799 	       && tag_type == typename_type)
17800 	type = make_typename_type (parser->scope, decl,
17801 				   typename_type,
17802 				   /*complain=*/tf_error);
17803       /* If the `typename' keyword is in effect and DECL is not a type
17804 	 decl, then type is non existent.   */
17805       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17806         ;
17807       else if (TREE_CODE (decl) == TYPE_DECL)
17808 	{
17809 	  type = check_elaborated_type_specifier (tag_type, decl,
17810 						  /*allow_template_p=*/true);
17811 
17812 	  /* If the next token is a semicolon, this must be a specialization,
17813 	     instantiation, or friend declaration.  Check the scope while we
17814 	     still know whether or not we had a nested-name-specifier.  */
17815 	  if (type != error_mark_node
17816 	      && !nested_name_specifier && !is_friend
17817 	      && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17818 	    check_unqualified_spec_or_inst (type, token->location);
17819 	}
17820       else if (decl == error_mark_node)
17821 	type = error_mark_node;
17822     }
17823 
17824   if (!type)
17825     {
17826       token = cp_lexer_peek_token (parser->lexer);
17827       identifier = cp_parser_identifier (parser);
17828 
17829       if (identifier == error_mark_node)
17830 	{
17831 	  parser->scope = NULL_TREE;
17832 	  return error_mark_node;
17833 	}
17834 
17835       /* For a `typename', we needn't call xref_tag.  */
17836       if (tag_type == typename_type
17837 	  && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17838 	return cp_parser_make_typename_type (parser, identifier,
17839 					     token->location);
17840 
17841       /* Template parameter lists apply only if we are not within a
17842 	 function parameter list.  */
17843       bool template_parm_lists_apply
17844 	  = parser->num_template_parameter_lists;
17845       if (template_parm_lists_apply)
17846 	for (cp_binding_level *s = current_binding_level;
17847 	     s && s->kind != sk_template_parms;
17848 	     s = s->level_chain)
17849 	  if (s->kind == sk_function_parms)
17850 	    template_parm_lists_apply = false;
17851 
17852       /* Look up a qualified name in the usual way.  */
17853       if (parser->scope)
17854 	{
17855 	  tree decl;
17856 	  tree ambiguous_decls;
17857 
17858 	  decl = cp_parser_lookup_name (parser, identifier,
17859 					tag_type,
17860 					/*is_template=*/false,
17861 					/*is_namespace=*/false,
17862 					/*check_dependency=*/true,
17863 					&ambiguous_decls,
17864 					token->location);
17865 
17866 	  /* If the lookup was ambiguous, an error will already have been
17867 	     issued.  */
17868 	  if (ambiguous_decls)
17869 	    return error_mark_node;
17870 
17871 	  /* If we are parsing friend declaration, DECL may be a
17872 	     TEMPLATE_DECL tree node here.  However, we need to check
17873 	     whether this TEMPLATE_DECL results in valid code.  Consider
17874 	     the following example:
17875 
17876 	       namespace N {
17877 		 template <class T> class C {};
17878 	       }
17879 	       class X {
17880 		 template <class T> friend class N::C; // #1, valid code
17881 	       };
17882 	       template <class T> class Y {
17883 		 friend class N::C;		       // #2, invalid code
17884 	       };
17885 
17886 	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17887 	     name lookup of `N::C'.  We see that friend declaration must
17888 	     be template for the code to be valid.  Note that
17889 	     processing_template_decl does not work here since it is
17890 	     always 1 for the above two cases.  */
17891 
17892 	  decl = (cp_parser_maybe_treat_template_as_class
17893 		  (decl, /*tag_name_p=*/is_friend
17894 			 && template_parm_lists_apply));
17895 
17896 	  if (TREE_CODE (decl) != TYPE_DECL)
17897 	    {
17898 	      cp_parser_diagnose_invalid_type_name (parser,
17899 						    identifier,
17900 						    token->location);
17901 	      return error_mark_node;
17902 	    }
17903 
17904 	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17905             {
17906               bool allow_template = (template_parm_lists_apply
17907 		                     || DECL_SELF_REFERENCE_P (decl));
17908               type = check_elaborated_type_specifier (tag_type, decl,
17909                                                       allow_template);
17910 
17911               if (type == error_mark_node)
17912                 return error_mark_node;
17913             }
17914 
17915           /* Forward declarations of nested types, such as
17916 
17917                class C1::C2;
17918                class C1::C2::C3;
17919 
17920              are invalid unless all components preceding the final '::'
17921              are complete.  If all enclosing types are complete, these
17922              declarations become merely pointless.
17923 
17924              Invalid forward declarations of nested types are errors
17925              caught elsewhere in parsing.  Those that are pointless arrive
17926              here.  */
17927 
17928           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17929               && !is_friend && !processing_explicit_instantiation)
17930             warning (0, "declaration %qD does not declare anything", decl);
17931 
17932 	  type = TREE_TYPE (decl);
17933 	}
17934       else
17935 	{
17936 	  /* An elaborated-type-specifier sometimes introduces a new type and
17937 	     sometimes names an existing type.  Normally, the rule is that it
17938 	     introduces a new type only if there is not an existing type of
17939 	     the same name already in scope.  For example, given:
17940 
17941 	       struct S {};
17942 	       void f() { struct S s; }
17943 
17944 	     the `struct S' in the body of `f' is the same `struct S' as in
17945 	     the global scope; the existing definition is used.  However, if
17946 	     there were no global declaration, this would introduce a new
17947 	     local class named `S'.
17948 
17949 	     An exception to this rule applies to the following code:
17950 
17951 	       namespace N { struct S; }
17952 
17953 	     Here, the elaborated-type-specifier names a new type
17954 	     unconditionally; even if there is already an `S' in the
17955 	     containing scope this declaration names a new type.
17956 	     This exception only applies if the elaborated-type-specifier
17957 	     forms the complete declaration:
17958 
17959 	       [class.name]
17960 
17961 	       A declaration consisting solely of `class-key identifier ;' is
17962 	       either a redeclaration of the name in the current scope or a
17963 	       forward declaration of the identifier as a class name.  It
17964 	       introduces the name into the current scope.
17965 
17966 	     We are in this situation precisely when the next token is a `;'.
17967 
17968 	     An exception to the exception is that a `friend' declaration does
17969 	     *not* name a new type; i.e., given:
17970 
17971 	       struct S { friend struct T; };
17972 
17973 	     `T' is not a new type in the scope of `S'.
17974 
17975 	     Also, `new struct S' or `sizeof (struct S)' never results in the
17976 	     definition of a new type; a new type can only be declared in a
17977 	     declaration context.  */
17978 
17979 	  tag_scope ts;
17980 	  bool template_p;
17981 
17982 	  if (is_friend)
17983 	    /* Friends have special name lookup rules.  */
17984 	    ts = ts_within_enclosing_non_class;
17985 	  else if (is_declaration
17986 		   && cp_lexer_next_token_is (parser->lexer,
17987 					      CPP_SEMICOLON))
17988 	    /* This is a `class-key identifier ;' */
17989 	    ts = ts_current;
17990 	  else
17991 	    ts = ts_global;
17992 
17993 	  template_p =
17994 	    (template_parm_lists_apply
17995 	     && (cp_parser_next_token_starts_class_definition_p (parser)
17996 		 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17997 	  /* An unqualified name was used to reference this type, so
17998 	     there were no qualifying templates.  */
17999 	  if (template_parm_lists_apply
18000 	      && !cp_parser_check_template_parameters (parser,
18001 						       /*num_templates=*/0,
18002 						       /*template_id*/false,
18003 						       token->location,
18004 						       /*declarator=*/NULL))
18005 	    return error_mark_node;
18006 	  type = xref_tag (tag_type, identifier, ts, template_p);
18007 	}
18008     }
18009 
18010   if (type == error_mark_node)
18011     return error_mark_node;
18012 
18013   /* Allow attributes on forward declarations of classes.  */
18014   if (attributes)
18015     {
18016       if (TREE_CODE (type) == TYPENAME_TYPE)
18017 	warning (OPT_Wattributes,
18018 		 "attributes ignored on uninstantiated type");
18019       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18020 	       && ! processing_explicit_instantiation)
18021 	warning (OPT_Wattributes,
18022 		 "attributes ignored on template instantiation");
18023       else if (is_declaration && cp_parser_declares_only_class_p (parser))
18024 	cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18025       else
18026 	warning (OPT_Wattributes,
18027 		 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18028     }
18029 
18030   if (tag_type != enum_type)
18031     {
18032       /* Indicate whether this class was declared as a `class' or as a
18033 	 `struct'.  */
18034       if (CLASS_TYPE_P (type))
18035 	CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18036       cp_parser_check_class_key (tag_type, type);
18037     }
18038 
18039   /* A "<" cannot follow an elaborated type specifier.  If that
18040      happens, the user was probably trying to form a template-id.  */
18041   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18042 					   token->location);
18043 
18044   return type;
18045 }
18046 
18047 /* Parse an enum-specifier.
18048 
18049    enum-specifier:
18050      enum-head { enumerator-list [opt] }
18051      enum-head { enumerator-list , } [C++0x]
18052 
18053    enum-head:
18054      enum-key identifier [opt] enum-base [opt]
18055      enum-key nested-name-specifier identifier enum-base [opt]
18056 
18057    enum-key:
18058      enum
18059      enum class   [C++0x]
18060      enum struct  [C++0x]
18061 
18062    enum-base:   [C++0x]
18063      : type-specifier-seq
18064 
18065    opaque-enum-specifier:
18066      enum-key identifier enum-base [opt] ;
18067 
18068    GNU Extensions:
18069      enum-key attributes[opt] identifier [opt] enum-base [opt]
18070        { enumerator-list [opt] }attributes[opt]
18071      enum-key attributes[opt] identifier [opt] enum-base [opt]
18072        { enumerator-list, }attributes[opt] [C++0x]
18073 
18074    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18075    if the token stream isn't an enum-specifier after all.  */
18076 
18077 static tree
cp_parser_enum_specifier(cp_parser * parser)18078 cp_parser_enum_specifier (cp_parser* parser)
18079 {
18080   tree identifier;
18081   tree type = NULL_TREE;
18082   tree prev_scope;
18083   tree nested_name_specifier = NULL_TREE;
18084   tree attributes;
18085   bool scoped_enum_p = false;
18086   bool has_underlying_type = false;
18087   bool nested_being_defined = false;
18088   bool new_value_list = false;
18089   bool is_new_type = false;
18090   bool is_unnamed = false;
18091   tree underlying_type = NULL_TREE;
18092   cp_token *type_start_token = NULL;
18093   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18094 
18095   parser->colon_corrects_to_scope_p = false;
18096 
18097   /* Parse tentatively so that we can back up if we don't find a
18098      enum-specifier.  */
18099   cp_parser_parse_tentatively (parser);
18100 
18101   /* Caller guarantees that the current token is 'enum', an identifier
18102      possibly follows, and the token after that is an opening brace.
18103      If we don't have an identifier, fabricate an anonymous name for
18104      the enumeration being defined.  */
18105   cp_lexer_consume_token (parser->lexer);
18106 
18107   /* Parse the "class" or "struct", which indicates a scoped
18108      enumeration type in C++0x.  */
18109   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18110       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18111     {
18112       if (cxx_dialect < cxx11)
18113         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18114 
18115       /* Consume the `struct' or `class' token.  */
18116       cp_lexer_consume_token (parser->lexer);
18117 
18118       scoped_enum_p = true;
18119     }
18120 
18121   attributes = cp_parser_attributes_opt (parser);
18122 
18123   /* Clear the qualification.  */
18124   parser->scope = NULL_TREE;
18125   parser->qualifying_scope = NULL_TREE;
18126   parser->object_scope = NULL_TREE;
18127 
18128   /* Figure out in what scope the declaration is being placed.  */
18129   prev_scope = current_scope ();
18130 
18131   type_start_token = cp_lexer_peek_token (parser->lexer);
18132 
18133   push_deferring_access_checks (dk_no_check);
18134   nested_name_specifier
18135       = cp_parser_nested_name_specifier_opt (parser,
18136 					     /*typename_keyword_p=*/true,
18137 					     /*check_dependency_p=*/false,
18138 					     /*type_p=*/false,
18139 					     /*is_declaration=*/false);
18140 
18141   if (nested_name_specifier)
18142     {
18143       tree name;
18144 
18145       identifier = cp_parser_identifier (parser);
18146       name =  cp_parser_lookup_name (parser, identifier,
18147 				     enum_type,
18148 				     /*is_template=*/false,
18149 				     /*is_namespace=*/false,
18150 				     /*check_dependency=*/true,
18151 				     /*ambiguous_decls=*/NULL,
18152 				     input_location);
18153       if (name && name != error_mark_node)
18154 	{
18155 	  type = TREE_TYPE (name);
18156 	  if (TREE_CODE (type) == TYPENAME_TYPE)
18157 	    {
18158 	      /* Are template enums allowed in ISO? */
18159 	      if (template_parm_scope_p ())
18160 		pedwarn (type_start_token->location, OPT_Wpedantic,
18161 			 "%qD is an enumeration template", name);
18162 	      /* ignore a typename reference, for it will be solved by name
18163 	         in start_enum.  */
18164 	      type = NULL_TREE;
18165 	    }
18166 	}
18167       else if (nested_name_specifier == error_mark_node)
18168 	/* We already issued an error.  */;
18169       else
18170 	{
18171 	  error_at (type_start_token->location,
18172 		    "%qD does not name an enumeration in %qT",
18173 		    identifier, nested_name_specifier);
18174 	  nested_name_specifier = error_mark_node;
18175 	}
18176     }
18177   else
18178     {
18179       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18180 	identifier = cp_parser_identifier (parser);
18181       else
18182 	{
18183 	  identifier = make_anon_name ();
18184 	  is_unnamed = true;
18185 	  if (scoped_enum_p)
18186 	    error_at (type_start_token->location,
18187 		      "unnamed scoped enum is not allowed");
18188 	}
18189     }
18190   pop_deferring_access_checks ();
18191 
18192   /* Check for the `:' that denotes a specified underlying type in C++0x.
18193      Note that a ':' could also indicate a bitfield width, however.  */
18194   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18195     {
18196       cp_decl_specifier_seq type_specifiers;
18197 
18198       /* Consume the `:'.  */
18199       cp_lexer_consume_token (parser->lexer);
18200 
18201       /* Parse the type-specifier-seq.  */
18202       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18203 				    /*is_trailing_return=*/false,
18204                                     &type_specifiers);
18205 
18206       /* At this point this is surely not elaborated type specifier.  */
18207       if (!cp_parser_parse_definitely (parser))
18208 	return NULL_TREE;
18209 
18210       if (cxx_dialect < cxx11)
18211         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18212 
18213       has_underlying_type = true;
18214 
18215       /* If that didn't work, stop.  */
18216       if (type_specifiers.type != error_mark_node)
18217         {
18218           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18219                                             /*initialized=*/0, NULL);
18220           if (underlying_type == error_mark_node
18221 	      || check_for_bare_parameter_packs (underlying_type))
18222             underlying_type = NULL_TREE;
18223         }
18224     }
18225 
18226   /* Look for the `{' but don't consume it yet.  */
18227   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18228     {
18229       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18230 	{
18231 	  cp_parser_error (parser, "expected %<{%>");
18232 	  if (has_underlying_type)
18233 	    {
18234 	      type = NULL_TREE;
18235 	      goto out;
18236 	    }
18237 	}
18238       /* An opaque-enum-specifier must have a ';' here.  */
18239       if ((scoped_enum_p || underlying_type)
18240 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18241 	{
18242 	  cp_parser_error (parser, "expected %<;%> or %<{%>");
18243 	  if (has_underlying_type)
18244 	    {
18245 	      type = NULL_TREE;
18246 	      goto out;
18247 	    }
18248 	}
18249     }
18250 
18251   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18252     return NULL_TREE;
18253 
18254   if (nested_name_specifier)
18255     {
18256       if (CLASS_TYPE_P (nested_name_specifier))
18257 	{
18258 	  nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18259 	  TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18260 	  push_scope (nested_name_specifier);
18261 	}
18262       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18263 	{
18264 	  push_nested_namespace (nested_name_specifier);
18265 	}
18266     }
18267 
18268   /* Issue an error message if type-definitions are forbidden here.  */
18269   if (!cp_parser_check_type_definition (parser))
18270     type = error_mark_node;
18271   else
18272     /* Create the new type.  We do this before consuming the opening
18273        brace so the enum will be recorded as being on the line of its
18274        tag (or the 'enum' keyword, if there is no tag).  */
18275     type = start_enum (identifier, type, underlying_type,
18276 		       attributes, scoped_enum_p, &is_new_type);
18277 
18278   /* If the next token is not '{' it is an opaque-enum-specifier or an
18279      elaborated-type-specifier.  */
18280   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18281     {
18282       timevar_push (TV_PARSE_ENUM);
18283       if (nested_name_specifier
18284 	  && nested_name_specifier != error_mark_node)
18285 	{
18286 	  /* The following catches invalid code such as:
18287 	     enum class S<int>::E { A, B, C }; */
18288 	  if (!processing_specialization
18289 	      && CLASS_TYPE_P (nested_name_specifier)
18290 	      && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18291 	    error_at (type_start_token->location, "cannot add an enumerator "
18292 		      "list to a template instantiation");
18293 
18294 	  if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18295 	    {
18296 	      error_at (type_start_token->location,
18297 			"%<%T::%E%> has not been declared",
18298 			TYPE_CONTEXT (nested_name_specifier),
18299 			nested_name_specifier);
18300 	      type = error_mark_node;
18301 	    }
18302 	  else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18303 		   && !CLASS_TYPE_P (nested_name_specifier))
18304 	    {
18305 	      error_at (type_start_token->location, "nested name specifier "
18306 			"%qT for enum declaration does not name a class "
18307 			"or namespace", nested_name_specifier);
18308 	      type = error_mark_node;
18309 	    }
18310 	  /* If that scope does not contain the scope in which the
18311 	     class was originally declared, the program is invalid.  */
18312 	  else if (prev_scope && !is_ancestor (prev_scope,
18313 					       nested_name_specifier))
18314 	    {
18315 	      if (at_namespace_scope_p ())
18316 		error_at (type_start_token->location,
18317 			  "declaration of %qD in namespace %qD which does not "
18318 			  "enclose %qD",
18319 			  type, prev_scope, nested_name_specifier);
18320 	      else
18321 		error_at (type_start_token->location,
18322 			  "declaration of %qD in %qD which does not "
18323 			  "enclose %qD",
18324 			  type, prev_scope, nested_name_specifier);
18325 	      type = error_mark_node;
18326 	    }
18327 	  /* If that scope is the scope where the declaration is being placed
18328 	     the program is invalid.  */
18329 	  else if (CLASS_TYPE_P (nested_name_specifier)
18330 		   && CLASS_TYPE_P (prev_scope)
18331 		   && same_type_p (nested_name_specifier, prev_scope))
18332 	    {
18333 	      permerror (type_start_token->location,
18334 			 "extra qualification not allowed");
18335 	      nested_name_specifier = NULL_TREE;
18336 	    }
18337 	}
18338 
18339       if (scoped_enum_p)
18340 	begin_scope (sk_scoped_enum, type);
18341 
18342       /* Consume the opening brace.  */
18343       matching_braces braces;
18344       braces.consume_open (parser);
18345 
18346       if (type == error_mark_node)
18347 	; /* Nothing to add */
18348       else if (OPAQUE_ENUM_P (type)
18349 	       || (cxx_dialect > cxx98 && processing_specialization))
18350 	{
18351 	  new_value_list = true;
18352 	  SET_OPAQUE_ENUM_P (type, false);
18353 	  DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18354 	}
18355       else
18356 	{
18357 	  error_at (type_start_token->location,
18358 		    "multiple definition of %q#T", type);
18359 	  inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18360 		  "previous definition here");
18361 	  type = error_mark_node;
18362 	}
18363 
18364       if (type == error_mark_node)
18365 	cp_parser_skip_to_end_of_block_or_statement (parser);
18366       /* If the next token is not '}', then there are some enumerators.  */
18367       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18368 	{
18369 	  if (is_unnamed && !scoped_enum_p)
18370 	    pedwarn (type_start_token->location, OPT_Wpedantic,
18371 		     "ISO C++ forbids empty unnamed enum");
18372 	}
18373       else
18374 	cp_parser_enumerator_list (parser, type);
18375 
18376       /* Consume the final '}'.  */
18377       braces.require_close (parser);
18378 
18379       if (scoped_enum_p)
18380 	finish_scope ();
18381       timevar_pop (TV_PARSE_ENUM);
18382     }
18383   else
18384     {
18385       /* If a ';' follows, then it is an opaque-enum-specifier
18386 	and additional restrictions apply.  */
18387       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18388 	{
18389 	  if (is_unnamed)
18390 	    error_at (type_start_token->location,
18391 		      "opaque-enum-specifier without name");
18392 	  else if (nested_name_specifier)
18393 	    error_at (type_start_token->location,
18394 		      "opaque-enum-specifier must use a simple identifier");
18395 	}
18396     }
18397 
18398   /* Look for trailing attributes to apply to this enumeration, and
18399      apply them if appropriate.  */
18400   if (cp_parser_allow_gnu_extensions_p (parser))
18401     {
18402       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18403       cplus_decl_attributes (&type,
18404 			     trailing_attr,
18405 			     (int) ATTR_FLAG_TYPE_IN_PLACE);
18406     }
18407 
18408   /* Finish up the enumeration.  */
18409   if (type != error_mark_node)
18410     {
18411       if (new_value_list)
18412 	finish_enum_value_list (type);
18413       if (is_new_type)
18414 	finish_enum (type);
18415     }
18416 
18417   if (nested_name_specifier)
18418     {
18419       if (CLASS_TYPE_P (nested_name_specifier))
18420 	{
18421 	  TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18422 	  pop_scope (nested_name_specifier);
18423 	}
18424       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18425 	{
18426 	  pop_nested_namespace (nested_name_specifier);
18427 	}
18428     }
18429  out:
18430   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18431   return type;
18432 }
18433 
18434 /* Parse an enumerator-list.  The enumerators all have the indicated
18435    TYPE.
18436 
18437    enumerator-list:
18438      enumerator-definition
18439      enumerator-list , enumerator-definition  */
18440 
18441 static void
cp_parser_enumerator_list(cp_parser * parser,tree type)18442 cp_parser_enumerator_list (cp_parser* parser, tree type)
18443 {
18444   while (true)
18445     {
18446       /* Parse an enumerator-definition.  */
18447       cp_parser_enumerator_definition (parser, type);
18448 
18449       /* If the next token is not a ',', we've reached the end of
18450 	 the list.  */
18451       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18452 	break;
18453       /* Otherwise, consume the `,' and keep going.  */
18454       cp_lexer_consume_token (parser->lexer);
18455       /* If the next token is a `}', there is a trailing comma.  */
18456       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18457 	{
18458 	  if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18459 	    pedwarn (input_location, OPT_Wpedantic,
18460                      "comma at end of enumerator list");
18461 	  break;
18462 	}
18463     }
18464 }
18465 
18466 /* Parse an enumerator-definition.  The enumerator has the indicated
18467    TYPE.
18468 
18469    enumerator-definition:
18470      enumerator
18471      enumerator = constant-expression
18472 
18473    enumerator:
18474      identifier
18475 
18476    GNU Extensions:
18477 
18478    enumerator-definition:
18479      enumerator attributes [opt]
18480      enumerator attributes [opt] = constant-expression  */
18481 
18482 static void
cp_parser_enumerator_definition(cp_parser * parser,tree type)18483 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18484 {
18485   tree identifier;
18486   tree value;
18487   location_t loc;
18488 
18489   /* Save the input location because we are interested in the location
18490      of the identifier and not the location of the explicit value.  */
18491   loc = cp_lexer_peek_token (parser->lexer)->location;
18492 
18493   /* Look for the identifier.  */
18494   identifier = cp_parser_identifier (parser);
18495   if (identifier == error_mark_node)
18496     return;
18497 
18498   /* Parse any specified attributes.  */
18499   tree attrs = cp_parser_attributes_opt (parser);
18500 
18501   /* If the next token is an '=', then there is an explicit value.  */
18502   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18503     {
18504       /* Consume the `=' token.  */
18505       cp_lexer_consume_token (parser->lexer);
18506       /* Parse the value.  */
18507       value = cp_parser_constant_expression (parser);
18508     }
18509   else
18510     value = NULL_TREE;
18511 
18512   /* If we are processing a template, make sure the initializer of the
18513      enumerator doesn't contain any bare template parameter pack.  */
18514   if (check_for_bare_parameter_packs (value))
18515     value = error_mark_node;
18516 
18517   /* Create the enumerator.  */
18518   build_enumerator (identifier, value, type, attrs, loc);
18519 }
18520 
18521 /* Parse a namespace-name.
18522 
18523    namespace-name:
18524      original-namespace-name
18525      namespace-alias
18526 
18527    Returns the NAMESPACE_DECL for the namespace.  */
18528 
18529 static tree
cp_parser_namespace_name(cp_parser * parser)18530 cp_parser_namespace_name (cp_parser* parser)
18531 {
18532   tree identifier;
18533   tree namespace_decl;
18534 
18535   cp_token *token = cp_lexer_peek_token (parser->lexer);
18536 
18537   /* Get the name of the namespace.  */
18538   identifier = cp_parser_identifier (parser);
18539   if (identifier == error_mark_node)
18540     return error_mark_node;
18541 
18542   /* Look up the identifier in the currently active scope.  Look only
18543      for namespaces, due to:
18544 
18545        [basic.lookup.udir]
18546 
18547        When looking up a namespace-name in a using-directive or alias
18548        definition, only namespace names are considered.
18549 
18550      And:
18551 
18552        [basic.lookup.qual]
18553 
18554        During the lookup of a name preceding the :: scope resolution
18555        operator, object, function, and enumerator names are ignored.
18556 
18557      (Note that cp_parser_qualifying_entity only calls this
18558      function if the token after the name is the scope resolution
18559      operator.)  */
18560   namespace_decl = cp_parser_lookup_name (parser, identifier,
18561 					  none_type,
18562 					  /*is_template=*/false,
18563 					  /*is_namespace=*/true,
18564 					  /*check_dependency=*/true,
18565 					  /*ambiguous_decls=*/NULL,
18566 					  token->location);
18567   /* If it's not a namespace, issue an error.  */
18568   if (namespace_decl == error_mark_node
18569       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18570     {
18571       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18572 	{
18573 	  error_at (token->location, "%qD is not a namespace-name", identifier);
18574 	  if (namespace_decl == error_mark_node
18575 	      && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18576 	    suggest_alternative_in_explicit_scope (token->location, identifier,
18577 						   parser->scope);
18578 	}
18579       cp_parser_error (parser, "expected namespace-name");
18580       namespace_decl = error_mark_node;
18581     }
18582 
18583   return namespace_decl;
18584 }
18585 
18586 /* Parse a namespace-definition.
18587 
18588    namespace-definition:
18589      named-namespace-definition
18590      unnamed-namespace-definition
18591 
18592    named-namespace-definition:
18593      original-namespace-definition
18594      extension-namespace-definition
18595 
18596    original-namespace-definition:
18597      namespace identifier { namespace-body }
18598 
18599    extension-namespace-definition:
18600      namespace original-namespace-name { namespace-body }
18601 
18602    unnamed-namespace-definition:
18603      namespace { namespace-body } */
18604 
18605 static void
cp_parser_namespace_definition(cp_parser * parser)18606 cp_parser_namespace_definition (cp_parser* parser)
18607 {
18608   tree identifier;
18609   int nested_definition_count = 0;
18610 
18611   cp_ensure_no_omp_declare_simd (parser);
18612   cp_ensure_no_oacc_routine (parser);
18613 
18614   bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18615 
18616   if (is_inline)
18617     {
18618       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18619       cp_lexer_consume_token (parser->lexer);
18620     }
18621 
18622   /* Look for the `namespace' keyword.  */
18623   cp_token* token
18624     = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18625 
18626   /* Parse any specified attributes before the identifier.  */
18627   tree attribs = cp_parser_attributes_opt (parser);
18628 
18629   for (;;)
18630     {
18631       identifier = NULL_TREE;
18632 
18633       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18634 	{
18635 	  identifier = cp_parser_identifier (parser);
18636 
18637 	  /* Parse any attributes specified after the identifier.  */
18638 	  attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18639 	}
18640 
18641       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18642 	break;
18643 
18644       if (!nested_definition_count && cxx_dialect < cxx17)
18645         pedwarn (input_location, OPT_Wpedantic,
18646                  "nested namespace definitions only available with "
18647                  "-std=c++17 or -std=gnu++17");
18648 
18649       /* Nested namespace names can create new namespaces (unlike
18650 	 other qualified-ids).  */
18651       if (int count = identifier ? push_namespace (identifier) : 0)
18652 	nested_definition_count += count;
18653       else
18654 	cp_parser_error (parser, "nested namespace name required");
18655       cp_lexer_consume_token (parser->lexer);
18656     }
18657 
18658   if (nested_definition_count && !identifier)
18659     cp_parser_error (parser, "namespace name required");
18660 
18661   if (nested_definition_count && attribs)
18662     error_at (token->location,
18663 	      "a nested namespace definition cannot have attributes");
18664   if (nested_definition_count && is_inline)
18665     error_at (token->location,
18666 	      "a nested namespace definition cannot be inline");
18667 
18668   /* Start the namespace.  */
18669   nested_definition_count += push_namespace (identifier, is_inline);
18670 
18671   bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18672 
18673   warning  (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18674 
18675   /* Look for the `{' to validate starting the namespace.  */
18676   matching_braces braces;
18677   if (braces.require_open (parser))
18678     {
18679       /* Parse the body of the namespace.  */
18680       cp_parser_namespace_body (parser);
18681 
18682       /* Look for the final `}'.  */
18683       braces.require_close (parser);
18684     }
18685 
18686   if (has_visibility)
18687     pop_visibility (1);
18688 
18689   /* Pop the nested namespace definitions.  */
18690   while (nested_definition_count--)
18691     pop_namespace ();
18692 }
18693 
18694 /* Parse a namespace-body.
18695 
18696    namespace-body:
18697      declaration-seq [opt]  */
18698 
18699 static void
cp_parser_namespace_body(cp_parser * parser)18700 cp_parser_namespace_body (cp_parser* parser)
18701 {
18702   cp_parser_declaration_seq_opt (parser);
18703 }
18704 
18705 /* Parse a namespace-alias-definition.
18706 
18707    namespace-alias-definition:
18708      namespace identifier = qualified-namespace-specifier ;  */
18709 
18710 static void
cp_parser_namespace_alias_definition(cp_parser * parser)18711 cp_parser_namespace_alias_definition (cp_parser* parser)
18712 {
18713   tree identifier;
18714   tree namespace_specifier;
18715 
18716   cp_token *token = cp_lexer_peek_token (parser->lexer);
18717 
18718   /* Look for the `namespace' keyword.  */
18719   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18720   /* Look for the identifier.  */
18721   identifier = cp_parser_identifier (parser);
18722   if (identifier == error_mark_node)
18723     return;
18724   /* Look for the `=' token.  */
18725   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18726       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18727     {
18728       error_at (token->location, "%<namespace%> definition is not allowed here");
18729       /* Skip the definition.  */
18730       cp_lexer_consume_token (parser->lexer);
18731       if (cp_parser_skip_to_closing_brace (parser))
18732 	cp_lexer_consume_token (parser->lexer);
18733       return;
18734     }
18735   cp_parser_require (parser, CPP_EQ, RT_EQ);
18736   /* Look for the qualified-namespace-specifier.  */
18737   namespace_specifier
18738     = cp_parser_qualified_namespace_specifier (parser);
18739   /* Look for the `;' token.  */
18740   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18741 
18742   /* Register the alias in the symbol table.  */
18743   do_namespace_alias (identifier, namespace_specifier);
18744 }
18745 
18746 /* Parse a qualified-namespace-specifier.
18747 
18748    qualified-namespace-specifier:
18749      :: [opt] nested-name-specifier [opt] namespace-name
18750 
18751    Returns a NAMESPACE_DECL corresponding to the specified
18752    namespace.  */
18753 
18754 static tree
cp_parser_qualified_namespace_specifier(cp_parser * parser)18755 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18756 {
18757   /* Look for the optional `::'.  */
18758   cp_parser_global_scope_opt (parser,
18759 			      /*current_scope_valid_p=*/false);
18760 
18761   /* Look for the optional nested-name-specifier.  */
18762   cp_parser_nested_name_specifier_opt (parser,
18763 				       /*typename_keyword_p=*/false,
18764 				       /*check_dependency_p=*/true,
18765 				       /*type_p=*/false,
18766 				       /*is_declaration=*/true);
18767 
18768   return cp_parser_namespace_name (parser);
18769 }
18770 
18771 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18772    access declaration.
18773 
18774    using-declaration:
18775      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18776      using :: unqualified-id ;
18777 
18778    access-declaration:
18779      qualified-id ;
18780 
18781    */
18782 
18783 static bool
cp_parser_using_declaration(cp_parser * parser,bool access_declaration_p)18784 cp_parser_using_declaration (cp_parser* parser,
18785 			     bool access_declaration_p)
18786 {
18787   cp_token *token;
18788   bool typename_p = false;
18789   bool global_scope_p;
18790   tree decl;
18791   tree identifier;
18792   tree qscope;
18793   int oldcount = errorcount;
18794   cp_token *diag_token = NULL;
18795 
18796   if (access_declaration_p)
18797     {
18798       diag_token = cp_lexer_peek_token (parser->lexer);
18799       cp_parser_parse_tentatively (parser);
18800     }
18801   else
18802     {
18803       /* Look for the `using' keyword.  */
18804       cp_parser_require_keyword (parser, RID_USING, RT_USING);
18805 
18806  again:
18807       /* Peek at the next token.  */
18808       token = cp_lexer_peek_token (parser->lexer);
18809       /* See if it's `typename'.  */
18810       if (token->keyword == RID_TYPENAME)
18811 	{
18812 	  /* Remember that we've seen it.  */
18813 	  typename_p = true;
18814 	  /* Consume the `typename' token.  */
18815 	  cp_lexer_consume_token (parser->lexer);
18816 	}
18817     }
18818 
18819   /* Look for the optional global scope qualification.  */
18820   global_scope_p
18821     = (cp_parser_global_scope_opt (parser,
18822 				   /*current_scope_valid_p=*/false)
18823        != NULL_TREE);
18824 
18825   /* If we saw `typename', or didn't see `::', then there must be a
18826      nested-name-specifier present.  */
18827   if (typename_p || !global_scope_p)
18828     {
18829       qscope = cp_parser_nested_name_specifier (parser, typename_p,
18830 						/*check_dependency_p=*/true,
18831 						/*type_p=*/false,
18832 						/*is_declaration=*/true);
18833       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18834 	{
18835 	  cp_parser_skip_to_end_of_block_or_statement (parser);
18836 	  return false;
18837 	}
18838     }
18839   /* Otherwise, we could be in either of the two productions.  In that
18840      case, treat the nested-name-specifier as optional.  */
18841   else
18842     qscope = cp_parser_nested_name_specifier_opt (parser,
18843 						  /*typename_keyword_p=*/false,
18844 						  /*check_dependency_p=*/true,
18845 						  /*type_p=*/false,
18846 						  /*is_declaration=*/true);
18847   if (!qscope)
18848     qscope = global_namespace;
18849   else if (UNSCOPED_ENUM_P (qscope)
18850 	   && !TYPE_FUNCTION_SCOPE_P (qscope))
18851     qscope = CP_TYPE_CONTEXT (qscope);
18852 
18853   if (access_declaration_p && cp_parser_error_occurred (parser))
18854     /* Something has already gone wrong; there's no need to parse
18855        further.  Since an error has occurred, the return value of
18856        cp_parser_parse_definitely will be false, as required.  */
18857     return cp_parser_parse_definitely (parser);
18858 
18859   token = cp_lexer_peek_token (parser->lexer);
18860   /* Parse the unqualified-id.  */
18861   identifier = cp_parser_unqualified_id (parser,
18862 					 /*template_keyword_p=*/false,
18863 					 /*check_dependency_p=*/true,
18864 					 /*declarator_p=*/true,
18865 					 /*optional_p=*/false);
18866 
18867   if (access_declaration_p)
18868     {
18869       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18870 	cp_parser_simulate_error (parser);
18871       if (!cp_parser_parse_definitely (parser))
18872 	return false;
18873     }
18874   else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18875     {
18876       cp_token *ell = cp_lexer_consume_token (parser->lexer);
18877       if (cxx_dialect < cxx17
18878 	  && !in_system_header_at (ell->location))
18879 	pedwarn (ell->location, 0,
18880 		 "pack expansion in using-declaration only available "
18881 		 "with -std=c++17 or -std=gnu++17");
18882       qscope = make_pack_expansion (qscope);
18883     }
18884 
18885   /* The function we call to handle a using-declaration is different
18886      depending on what scope we are in.  */
18887   if (qscope == error_mark_node || identifier == error_mark_node)
18888     ;
18889   else if (!identifier_p (identifier)
18890 	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
18891     /* [namespace.udecl]
18892 
18893        A using declaration shall not name a template-id.  */
18894     error_at (token->location,
18895 	      "a template-id may not appear in a using-declaration");
18896   else
18897     {
18898       if (at_class_scope_p ())
18899 	{
18900 	  /* Create the USING_DECL.  */
18901 	  decl = do_class_using_decl (qscope, identifier);
18902 
18903 	  if (decl && typename_p)
18904 	    USING_DECL_TYPENAME_P (decl) = 1;
18905 
18906 	  if (check_for_bare_parameter_packs (decl))
18907 	    {
18908 	      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18909 	      return false;
18910 	    }
18911 	  else
18912 	    /* Add it to the list of members in this class.  */
18913 	    finish_member_declaration (decl);
18914 	}
18915       else
18916 	{
18917 	  decl = cp_parser_lookup_name_simple (parser,
18918 					       identifier,
18919 					       token->location);
18920 	  if (decl == error_mark_node)
18921 	    cp_parser_name_lookup_error (parser, identifier,
18922 					 decl, NLE_NULL,
18923 					 token->location);
18924 	  else if (check_for_bare_parameter_packs (decl))
18925 	    {
18926 	      cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18927 	      return false;
18928 	    }
18929 	  else if (!at_namespace_scope_p ())
18930 	    finish_local_using_decl (decl, qscope, identifier);
18931 	  else
18932 	    finish_namespace_using_decl (decl, qscope, identifier);
18933 	}
18934     }
18935 
18936   if (!access_declaration_p
18937       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18938     {
18939       cp_token *comma = cp_lexer_consume_token (parser->lexer);
18940       if (cxx_dialect < cxx17)
18941 	pedwarn (comma->location, 0,
18942 		 "comma-separated list in using-declaration only available "
18943 		 "with -std=c++17 or -std=gnu++17");
18944       goto again;
18945     }
18946 
18947   /* Look for the final `;'.  */
18948   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18949 
18950   if (access_declaration_p && errorcount == oldcount)
18951     warning_at (diag_token->location, OPT_Wdeprecated,
18952 		"access declarations are deprecated "
18953 		"in favour of using-declarations; "
18954 		"suggestion: add the %<using%> keyword");
18955 
18956   return true;
18957 }
18958 
18959 /* Parse an alias-declaration.
18960 
18961    alias-declaration:
18962      using identifier attribute-specifier-seq [opt] = type-id  */
18963 
18964 static tree
cp_parser_alias_declaration(cp_parser * parser)18965 cp_parser_alias_declaration (cp_parser* parser)
18966 {
18967   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18968   location_t id_location;
18969   cp_declarator *declarator;
18970   cp_decl_specifier_seq decl_specs;
18971   bool member_p;
18972   const char *saved_message = NULL;
18973 
18974   /* Look for the `using' keyword.  */
18975   cp_token *using_token
18976     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18977   if (using_token == NULL)
18978     return error_mark_node;
18979 
18980   id_location = cp_lexer_peek_token (parser->lexer)->location;
18981   id = cp_parser_identifier (parser);
18982   if (id == error_mark_node)
18983     return error_mark_node;
18984 
18985   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18986   attributes = cp_parser_attributes_opt (parser);
18987   if (attributes == error_mark_node)
18988     return error_mark_node;
18989 
18990   cp_parser_require (parser, CPP_EQ, RT_EQ);
18991 
18992   if (cp_parser_error_occurred (parser))
18993     return error_mark_node;
18994 
18995   cp_parser_commit_to_tentative_parse (parser);
18996 
18997   /* Now we are going to parse the type-id of the declaration.  */
18998 
18999   /*
19000     [dcl.type]/3 says:
19001 
19002 	"A type-specifier-seq shall not define a class or enumeration
19003 	 unless it appears in the type-id of an alias-declaration (7.1.3) that
19004 	 is not the declaration of a template-declaration."
19005 
19006     In other words, if we currently are in an alias template, the
19007     type-id should not define a type.
19008 
19009     So let's set parser->type_definition_forbidden_message in that
19010     case; cp_parser_check_type_definition (called by
19011     cp_parser_class_specifier) will then emit an error if a type is
19012     defined in the type-id.  */
19013   if (parser->num_template_parameter_lists)
19014     {
19015       saved_message = parser->type_definition_forbidden_message;
19016       parser->type_definition_forbidden_message =
19017 	G_("types may not be defined in alias template declarations");
19018     }
19019 
19020   type = cp_parser_type_id (parser);
19021 
19022   /* Restore the error message if need be.  */
19023   if (parser->num_template_parameter_lists)
19024     parser->type_definition_forbidden_message = saved_message;
19025 
19026   if (type == error_mark_node
19027       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19028     {
19029       cp_parser_skip_to_end_of_block_or_statement (parser);
19030       return error_mark_node;
19031     }
19032 
19033   /* A typedef-name can also be introduced by an alias-declaration. The
19034      identifier following the using keyword becomes a typedef-name. It has
19035      the same semantics as if it were introduced by the typedef
19036      specifier. In particular, it does not define a new type and it shall
19037      not appear in the type-id.  */
19038 
19039   clear_decl_specs (&decl_specs);
19040   decl_specs.type = type;
19041   if (attributes != NULL_TREE)
19042     {
19043       decl_specs.attributes = attributes;
19044       set_and_check_decl_spec_loc (&decl_specs,
19045 				   ds_attribute,
19046 				   attrs_token);
19047     }
19048   set_and_check_decl_spec_loc (&decl_specs,
19049 			       ds_typedef,
19050 			       using_token);
19051   set_and_check_decl_spec_loc (&decl_specs,
19052 			       ds_alias,
19053 			       using_token);
19054 
19055   if (parser->num_template_parameter_lists
19056       && !cp_parser_check_template_parameters (parser,
19057 					       /*num_templates=*/0,
19058 					       /*template_id*/false,
19059 					       id_location,
19060 					       /*declarator=*/NULL))
19061     return error_mark_node;
19062 
19063   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19064   declarator->id_loc = id_location;
19065 
19066   member_p = at_class_scope_p ();
19067   if (member_p)
19068     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19069 		      NULL_TREE, attributes);
19070   else
19071     decl = start_decl (declarator, &decl_specs, 0,
19072 		       attributes, NULL_TREE, &pushed_scope);
19073   if (decl == error_mark_node)
19074     return decl;
19075 
19076   // Attach constraints to the alias declaration.
19077   if (flag_concepts && current_template_parms)
19078     {
19079       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19080       tree constr = build_constraints (reqs, NULL_TREE);
19081       set_constraints (decl, constr);
19082     }
19083 
19084   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19085 
19086   if (pushed_scope)
19087     pop_scope (pushed_scope);
19088 
19089   /* If decl is a template, return its TEMPLATE_DECL so that it gets
19090      added into the symbol table; otherwise, return the TYPE_DECL.  */
19091   if (DECL_LANG_SPECIFIC (decl)
19092       && DECL_TEMPLATE_INFO (decl)
19093       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19094     {
19095       decl = DECL_TI_TEMPLATE (decl);
19096       if (member_p)
19097 	check_member_template (decl);
19098     }
19099 
19100   return decl;
19101 }
19102 
19103 /* Parse a using-directive.
19104 
19105    using-directive:
19106      using namespace :: [opt] nested-name-specifier [opt]
19107        namespace-name ;  */
19108 
19109 static void
cp_parser_using_directive(cp_parser * parser)19110 cp_parser_using_directive (cp_parser* parser)
19111 {
19112   tree namespace_decl;
19113   tree attribs;
19114 
19115   /* Look for the `using' keyword.  */
19116   cp_parser_require_keyword (parser, RID_USING, RT_USING);
19117   /* And the `namespace' keyword.  */
19118   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19119   /* Look for the optional `::' operator.  */
19120   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19121   /* And the optional nested-name-specifier.  */
19122   cp_parser_nested_name_specifier_opt (parser,
19123 				       /*typename_keyword_p=*/false,
19124 				       /*check_dependency_p=*/true,
19125 				       /*type_p=*/false,
19126 				       /*is_declaration=*/true);
19127   /* Get the namespace being used.  */
19128   namespace_decl = cp_parser_namespace_name (parser);
19129   /* And any specified attributes.  */
19130   attribs = cp_parser_attributes_opt (parser);
19131 
19132   /* Update the symbol table.  */
19133   if (namespace_bindings_p ())
19134     finish_namespace_using_directive (namespace_decl, attribs);
19135   else
19136     finish_local_using_directive (namespace_decl, attribs);
19137 
19138   /* Look for the final `;'.  */
19139   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19140 }
19141 
19142 /* Parse an asm-definition.
19143 
19144   asm-qualifier:
19145     volatile
19146     inline
19147     goto
19148 
19149   asm-qualifier-list:
19150     asm-qualifier
19151     asm-qualifier-list asm-qualifier
19152 
19153    asm-definition:
19154      asm ( string-literal ) ;
19155 
19156    GNU Extension:
19157 
19158    asm-definition:
19159      asm asm-qualifier-list [opt] ( string-literal ) ;
19160      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19161      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19162 				    : asm-operand-list [opt] ) ;
19163      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19164 				    : asm-operand-list [opt]
19165 			  : asm-clobber-list [opt] ) ;
19166      asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19167 				    : asm-clobber-list [opt]
19168 				    : asm-goto-list ) ;
19169 
19170   The form with asm-goto-list is valid if and only if the asm-qualifier-list
19171   contains goto, and is the only allowed form in that case.  No duplicates are
19172   allowed in an asm-qualifier-list.  */
19173 
19174 static void
cp_parser_asm_definition(cp_parser * parser)19175 cp_parser_asm_definition (cp_parser* parser)
19176 {
19177   tree string;
19178   tree outputs = NULL_TREE;
19179   tree inputs = NULL_TREE;
19180   tree clobbers = NULL_TREE;
19181   tree labels = NULL_TREE;
19182   tree asm_stmt;
19183   bool extended_p = false;
19184   bool invalid_inputs_p = false;
19185   bool invalid_outputs_p = false;
19186   required_token missing = RT_NONE;
19187 
19188   /* Look for the `asm' keyword.  */
19189   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19190 
19191   if (parser->in_function_body
19192       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19193     {
19194       error ("%<asm%> in %<constexpr%> function");
19195       cp_function_chain->invalid_constexpr = true;
19196     }
19197 
19198   /* Handle the asm-qualifier-list.  */
19199   location_t volatile_loc = UNKNOWN_LOCATION;
19200   location_t inline_loc = UNKNOWN_LOCATION;
19201   location_t goto_loc = UNKNOWN_LOCATION;
19202   location_t first_loc = UNKNOWN_LOCATION;
19203 
19204   if (cp_parser_allow_gnu_extensions_p (parser))
19205     for (;;)
19206       {
19207 	cp_token *token = cp_lexer_peek_token (parser->lexer);
19208 	location_t loc = token->location;
19209 	switch (cp_lexer_peek_token (parser->lexer)->keyword)
19210 	  {
19211 	  case RID_VOLATILE:
19212 	    if (volatile_loc)
19213 	      {
19214 		error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19215 		inform (volatile_loc, "first seen here");
19216 	      }
19217 	    else
19218 	      volatile_loc = loc;
19219 	    cp_lexer_consume_token (parser->lexer);
19220 	    continue;
19221 
19222 	  case RID_INLINE:
19223 	    if (inline_loc)
19224 	      {
19225 		error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19226 		inform (inline_loc, "first seen here");
19227 	      }
19228 	    else
19229 	      inline_loc = loc;
19230 	    if (!first_loc)
19231 	      first_loc = loc;
19232 	    cp_lexer_consume_token (parser->lexer);
19233 	    continue;
19234 
19235 	  case RID_GOTO:
19236 	    if (goto_loc)
19237 	      {
19238 		error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19239 		inform (goto_loc, "first seen here");
19240 	      }
19241 	    else
19242 	      goto_loc = loc;
19243 	    if (!first_loc)
19244 	      first_loc = loc;
19245 	    cp_lexer_consume_token (parser->lexer);
19246 	    continue;
19247 
19248 	  case RID_CONST:
19249 	  case RID_RESTRICT:
19250 	    error_at (loc, "%qT is not an asm qualifier", token->u.value);
19251 	    cp_lexer_consume_token (parser->lexer);
19252 	    continue;
19253 
19254 	  default:
19255 	    break;
19256 	  }
19257 	break;
19258       }
19259 
19260   bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19261   bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19262   bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19263 
19264   if (!parser->in_function_body && (inline_p || goto_p))
19265     {
19266       error_at (first_loc, "asm qualifier outside of function body");
19267       inline_p = goto_p = false;
19268     }
19269 
19270   /* Look for the opening `('.  */
19271   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19272     return;
19273   /* Look for the string.  */
19274   string = cp_parser_string_literal (parser, false, false);
19275   if (string == error_mark_node)
19276     {
19277       cp_parser_skip_to_closing_parenthesis (parser, true, false,
19278 					     /*consume_paren=*/true);
19279       return;
19280     }
19281 
19282   /* If we're allowing GNU extensions, check for the extended assembly
19283      syntax.  Unfortunately, the `:' tokens need not be separated by
19284      a space in C, and so, for compatibility, we tolerate that here
19285      too.  Doing that means that we have to treat the `::' operator as
19286      two `:' tokens.  */
19287   if (cp_parser_allow_gnu_extensions_p (parser)
19288       && parser->in_function_body
19289       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19290 	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19291     {
19292       bool inputs_p = false;
19293       bool clobbers_p = false;
19294       bool labels_p = false;
19295 
19296       /* The extended syntax was used.  */
19297       extended_p = true;
19298 
19299       /* Look for outputs.  */
19300       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19301 	{
19302 	  /* Consume the `:'.  */
19303 	  cp_lexer_consume_token (parser->lexer);
19304 	  /* Parse the output-operands.  */
19305 	  if (cp_lexer_next_token_is_not (parser->lexer,
19306 					  CPP_COLON)
19307 	      && cp_lexer_next_token_is_not (parser->lexer,
19308 					     CPP_SCOPE)
19309 	      && cp_lexer_next_token_is_not (parser->lexer,
19310 					     CPP_CLOSE_PAREN)
19311 	      && !goto_p)
19312             {
19313               outputs = cp_parser_asm_operand_list (parser);
19314               if (outputs == error_mark_node)
19315                 invalid_outputs_p = true;
19316             }
19317 	}
19318       /* If the next token is `::', there are no outputs, and the
19319 	 next token is the beginning of the inputs.  */
19320       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19321 	/* The inputs are coming next.  */
19322 	inputs_p = true;
19323 
19324       /* Look for inputs.  */
19325       if (inputs_p
19326 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19327 	{
19328 	  /* Consume the `:' or `::'.  */
19329 	  cp_lexer_consume_token (parser->lexer);
19330 	  /* Parse the output-operands.  */
19331 	  if (cp_lexer_next_token_is_not (parser->lexer,
19332 					  CPP_COLON)
19333 	      && cp_lexer_next_token_is_not (parser->lexer,
19334 					     CPP_SCOPE)
19335 	      && cp_lexer_next_token_is_not (parser->lexer,
19336 					     CPP_CLOSE_PAREN))
19337             {
19338               inputs = cp_parser_asm_operand_list (parser);
19339               if (inputs == error_mark_node)
19340                 invalid_inputs_p = true;
19341             }
19342 	}
19343       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19344 	/* The clobbers are coming next.  */
19345 	clobbers_p = true;
19346 
19347       /* Look for clobbers.  */
19348       if (clobbers_p
19349 	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19350 	{
19351 	  clobbers_p = true;
19352 	  /* Consume the `:' or `::'.  */
19353 	  cp_lexer_consume_token (parser->lexer);
19354 	  /* Parse the clobbers.  */
19355 	  if (cp_lexer_next_token_is_not (parser->lexer,
19356 					  CPP_COLON)
19357 	      && cp_lexer_next_token_is_not (parser->lexer,
19358 					     CPP_CLOSE_PAREN))
19359 	    clobbers = cp_parser_asm_clobber_list (parser);
19360 	}
19361       else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19362 	/* The labels are coming next.  */
19363 	labels_p = true;
19364 
19365       /* Look for labels.  */
19366       if (labels_p
19367 	  || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19368 	{
19369 	  labels_p = true;
19370 	  /* Consume the `:' or `::'.  */
19371 	  cp_lexer_consume_token (parser->lexer);
19372 	  /* Parse the labels.  */
19373 	  labels = cp_parser_asm_label_list (parser);
19374 	}
19375 
19376       if (goto_p && !labels_p)
19377 	missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19378     }
19379   else if (goto_p)
19380     missing = RT_COLON_SCOPE;
19381 
19382   /* Look for the closing `)'.  */
19383   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19384 			  missing ? missing : RT_CLOSE_PAREN))
19385     cp_parser_skip_to_closing_parenthesis (parser, true, false,
19386 					   /*consume_paren=*/true);
19387   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19388 
19389   if (!invalid_inputs_p && !invalid_outputs_p)
19390     {
19391       /* Create the ASM_EXPR.  */
19392       if (parser->in_function_body)
19393 	{
19394 	  asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19395 				      inputs, clobbers, labels, inline_p);
19396 	  /* If the extended syntax was not used, mark the ASM_EXPR.  */
19397 	  if (!extended_p)
19398 	    {
19399 	      tree temp = asm_stmt;
19400 	      if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19401 		temp = TREE_OPERAND (temp, 0);
19402 
19403 	      ASM_INPUT_P (temp) = 1;
19404 	    }
19405 	}
19406       else
19407 	symtab->finalize_toplevel_asm (string);
19408     }
19409 }
19410 
19411 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19412    type that comes from the decl-specifier-seq.  */
19413 
19414 static tree
strip_declarator_types(tree type,cp_declarator * declarator)19415 strip_declarator_types (tree type, cp_declarator *declarator)
19416 {
19417   for (cp_declarator *d = declarator; d;)
19418     switch (d->kind)
19419       {
19420       case cdk_id:
19421       case cdk_decomp:
19422       case cdk_error:
19423 	d = NULL;
19424 	break;
19425 
19426       default:
19427 	if (TYPE_PTRMEMFUNC_P (type))
19428 	  type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19429 	type = TREE_TYPE (type);
19430 	d = d->declarator;
19431 	break;
19432       }
19433 
19434   return type;
19435 }
19436 
19437 /* Declarators [gram.dcl.decl] */
19438 
19439 /* Parse an init-declarator.
19440 
19441    init-declarator:
19442      declarator initializer [opt]
19443 
19444    GNU Extension:
19445 
19446    init-declarator:
19447      declarator asm-specification [opt] attributes [opt] initializer [opt]
19448 
19449    function-definition:
19450      decl-specifier-seq [opt] declarator ctor-initializer [opt]
19451        function-body
19452      decl-specifier-seq [opt] declarator function-try-block
19453 
19454    GNU Extension:
19455 
19456    function-definition:
19457      __extension__ function-definition
19458 
19459    TM Extension:
19460 
19461    function-definition:
19462      decl-specifier-seq [opt] declarator function-transaction-block
19463 
19464    The DECL_SPECIFIERS apply to this declarator.  Returns a
19465    representation of the entity declared.  If MEMBER_P is TRUE, then
19466    this declarator appears in a class scope.  The new DECL created by
19467    this declarator is returned.
19468 
19469    The CHECKS are access checks that should be performed once we know
19470    what entity is being declared (and, therefore, what classes have
19471    befriended it).
19472 
19473    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19474    for a function-definition here as well.  If the declarator is a
19475    declarator for a function-definition, *FUNCTION_DEFINITION_P will
19476    be TRUE upon return.  By that point, the function-definition will
19477    have been completely parsed.
19478 
19479    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19480    is FALSE.
19481 
19482    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19483    parsed declaration if it is an uninitialized single declarator not followed
19484    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19485    if present, will not be consumed.  If returned, this declarator will be
19486    created with SD_INITIALIZED but will not call cp_finish_decl.
19487 
19488    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19489    and there is an initializer, the pointed location_t is set to the
19490    location of the '=' or `(', or '{' in C++11 token introducing the
19491    initializer.  */
19492 
19493 static tree
cp_parser_init_declarator(cp_parser * parser,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)19494 cp_parser_init_declarator (cp_parser* parser,
19495 			   cp_decl_specifier_seq *decl_specifiers,
19496 			   vec<deferred_access_check, va_gc> *checks,
19497 			   bool function_definition_allowed_p,
19498 			   bool member_p,
19499 			   int declares_class_or_enum,
19500 			   bool* function_definition_p,
19501 			   tree* maybe_range_for_decl,
19502 			   location_t* init_loc,
19503 			   tree* auto_result)
19504 {
19505   cp_token *token = NULL, *asm_spec_start_token = NULL,
19506            *attributes_start_token = NULL;
19507   cp_declarator *declarator;
19508   tree prefix_attributes;
19509   tree attributes = NULL;
19510   tree asm_specification;
19511   tree initializer;
19512   tree decl = NULL_TREE;
19513   tree scope;
19514   int is_initialized;
19515   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
19516      initialized with "= ..", CPP_OPEN_PAREN if initialized with
19517      "(...)".  */
19518   enum cpp_ttype initialization_kind;
19519   bool is_direct_init = false;
19520   bool is_non_constant_init;
19521   int ctor_dtor_or_conv_p;
19522   bool friend_p = cp_parser_friend_p (decl_specifiers);
19523   tree pushed_scope = NULL_TREE;
19524   bool range_for_decl_p = false;
19525   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19526   location_t tmp_init_loc = UNKNOWN_LOCATION;
19527 
19528   /* Gather the attributes that were provided with the
19529      decl-specifiers.  */
19530   prefix_attributes = decl_specifiers->attributes;
19531 
19532   /* Assume that this is not the declarator for a function
19533      definition.  */
19534   if (function_definition_p)
19535     *function_definition_p = false;
19536 
19537   /* Default arguments are only permitted for function parameters.  */
19538   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19539     parser->default_arg_ok_p = false;
19540 
19541   /* Defer access checks while parsing the declarator; we cannot know
19542      what names are accessible until we know what is being
19543      declared.  */
19544   resume_deferring_access_checks ();
19545 
19546   token = cp_lexer_peek_token (parser->lexer);
19547 
19548   /* Parse the declarator.  */
19549   declarator
19550     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19551 			    &ctor_dtor_or_conv_p,
19552 			    /*parenthesized_p=*/NULL,
19553 			    member_p, friend_p);
19554   /* Gather up the deferred checks.  */
19555   stop_deferring_access_checks ();
19556 
19557   parser->default_arg_ok_p = saved_default_arg_ok_p;
19558 
19559   /* If the DECLARATOR was erroneous, there's no need to go
19560      further.  */
19561   if (declarator == cp_error_declarator)
19562     return error_mark_node;
19563 
19564   /* Check that the number of template-parameter-lists is OK.  */
19565   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19566 						       token->location))
19567     return error_mark_node;
19568 
19569   if (declares_class_or_enum & 2)
19570     cp_parser_check_for_definition_in_return_type (declarator,
19571 						   decl_specifiers->type,
19572 						   decl_specifiers->locations[ds_type_spec]);
19573 
19574   /* Figure out what scope the entity declared by the DECLARATOR is
19575      located in.  `grokdeclarator' sometimes changes the scope, so
19576      we compute it now.  */
19577   scope = get_scope_of_declarator (declarator);
19578 
19579   /* Perform any lookups in the declared type which were thought to be
19580      dependent, but are not in the scope of the declarator.  */
19581   decl_specifiers->type
19582     = maybe_update_decl_type (decl_specifiers->type, scope);
19583 
19584   /* If we're allowing GNU extensions, look for an
19585      asm-specification.  */
19586   if (cp_parser_allow_gnu_extensions_p (parser))
19587     {
19588       /* Look for an asm-specification.  */
19589       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19590       asm_specification = cp_parser_asm_specification_opt (parser);
19591     }
19592   else
19593     asm_specification = NULL_TREE;
19594 
19595   /* Look for attributes.  */
19596   attributes_start_token = cp_lexer_peek_token (parser->lexer);
19597   attributes = cp_parser_attributes_opt (parser);
19598 
19599   /* Peek at the next token.  */
19600   token = cp_lexer_peek_token (parser->lexer);
19601 
19602   bool bogus_implicit_tmpl = false;
19603 
19604   if (function_declarator_p (declarator))
19605     {
19606       /* Handle C++17 deduction guides.  */
19607       if (!decl_specifiers->type
19608 	  && ctor_dtor_or_conv_p <= 0
19609 	  && cxx_dialect >= cxx17)
19610 	{
19611 	  cp_declarator *id = get_id_declarator (declarator);
19612 	  tree name = id->u.id.unqualified_name;
19613 	  parser->scope = id->u.id.qualifying_scope;
19614 	  tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19615 	  if (tmpl
19616 	      && (DECL_CLASS_TEMPLATE_P (tmpl)
19617 		  || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19618 	    {
19619 	      id->u.id.unqualified_name = dguide_name (tmpl);
19620 	      id->u.id.sfk = sfk_deduction_guide;
19621 	      ctor_dtor_or_conv_p = 1;
19622 	    }
19623 	}
19624 
19625       /* Check to see if the token indicates the start of a
19626 	 function-definition.  */
19627       if (cp_parser_token_starts_function_definition_p (token))
19628 	{
19629 	  if (!function_definition_allowed_p)
19630 	    {
19631 	      /* If a function-definition should not appear here, issue an
19632 		 error message.  */
19633 	      cp_parser_error (parser,
19634 			       "a function-definition is not allowed here");
19635 	      return error_mark_node;
19636 	    }
19637 
19638 	  location_t func_brace_location
19639 	    = cp_lexer_peek_token (parser->lexer)->location;
19640 
19641 	  /* Neither attributes nor an asm-specification are allowed
19642 	     on a function-definition.  */
19643 	  if (asm_specification)
19644 	    error_at (asm_spec_start_token->location,
19645 		      "an asm-specification is not allowed "
19646 		      "on a function-definition");
19647 	  if (attributes)
19648 	    error_at (attributes_start_token->location,
19649 		      "attributes are not allowed "
19650 		      "on a function-definition");
19651 	  /* This is a function-definition.  */
19652 	  *function_definition_p = true;
19653 
19654 	  /* Parse the function definition.  */
19655 	  if (member_p)
19656 	    decl = cp_parser_save_member_function_body (parser,
19657 							decl_specifiers,
19658 							declarator,
19659 							prefix_attributes);
19660 	  else
19661 	    decl =
19662 	      (cp_parser_function_definition_from_specifiers_and_declarator
19663 	       (parser, decl_specifiers, prefix_attributes, declarator));
19664 
19665 	  if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19666 	    {
19667 	      /* This is where the prologue starts...  */
19668 	      DECL_STRUCT_FUNCTION (decl)->function_start_locus
19669 		= func_brace_location;
19670 	    }
19671 
19672 	  return decl;
19673 	}
19674     }
19675   else if (parser->fully_implicit_function_template_p)
19676     {
19677       /* A non-template declaration involving a function parameter list
19678 	 containing an implicit template parameter will be made into a
19679 	 template.  If the resulting declaration is not going to be an
19680 	 actual function then finish the template scope here to prevent it.
19681 	 An error message will be issued once we have a decl to talk about.
19682 
19683          FIXME probably we should do type deduction rather than create an
19684          implicit template, but the standard currently doesn't allow it. */
19685       bogus_implicit_tmpl = true;
19686       finish_fully_implicit_template (parser, NULL_TREE);
19687     }
19688 
19689   /* [dcl.dcl]
19690 
19691      Only in function declarations for constructors, destructors, type
19692      conversions, and deduction guides can the decl-specifier-seq be omitted.
19693 
19694      We explicitly postpone this check past the point where we handle
19695      function-definitions because we tolerate function-definitions
19696      that are missing their return types in some modes.  */
19697   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19698     {
19699       cp_parser_error (parser,
19700 		       "expected constructor, destructor, or type conversion");
19701       return error_mark_node;
19702     }
19703 
19704   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
19705   if (token->type == CPP_EQ
19706       || token->type == CPP_OPEN_PAREN
19707       || token->type == CPP_OPEN_BRACE)
19708     {
19709       is_initialized = SD_INITIALIZED;
19710       initialization_kind = token->type;
19711       if (maybe_range_for_decl)
19712 	*maybe_range_for_decl = error_mark_node;
19713       tmp_init_loc = token->location;
19714       if (init_loc && *init_loc == UNKNOWN_LOCATION)
19715 	*init_loc = tmp_init_loc;
19716 
19717       if (token->type == CPP_EQ
19718 	  && function_declarator_p (declarator))
19719 	{
19720 	  cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19721 	  if (t2->keyword == RID_DEFAULT)
19722 	    is_initialized = SD_DEFAULTED;
19723 	  else if (t2->keyword == RID_DELETE)
19724 	    is_initialized = SD_DELETED;
19725 	}
19726     }
19727   else
19728     {
19729       /* If the init-declarator isn't initialized and isn't followed by a
19730 	 `,' or `;', it's not a valid init-declarator.  */
19731       if (token->type != CPP_COMMA
19732 	  && token->type != CPP_SEMICOLON)
19733 	{
19734 	  if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19735 	    range_for_decl_p = true;
19736 	  else
19737 	    {
19738 	      if (!maybe_range_for_decl)
19739 		cp_parser_error (parser, "expected initializer");
19740 	      return error_mark_node;
19741 	    }
19742 	}
19743       is_initialized = SD_UNINITIALIZED;
19744       initialization_kind = CPP_EOF;
19745     }
19746 
19747   /* Because start_decl has side-effects, we should only call it if we
19748      know we're going ahead.  By this point, we know that we cannot
19749      possibly be looking at any other construct.  */
19750   cp_parser_commit_to_tentative_parse (parser);
19751 
19752   /* Enter the newly declared entry in the symbol table.  If we're
19753      processing a declaration in a class-specifier, we wait until
19754      after processing the initializer.  */
19755   if (!member_p)
19756     {
19757       if (parser->in_unbraced_linkage_specification_p)
19758 	decl_specifiers->storage_class = sc_extern;
19759       decl = start_decl (declarator, decl_specifiers,
19760 			 range_for_decl_p? SD_INITIALIZED : is_initialized,
19761 			 attributes, prefix_attributes, &pushed_scope);
19762       cp_finalize_omp_declare_simd (parser, decl);
19763       cp_finalize_oacc_routine (parser, decl, false);
19764       /* Adjust location of decl if declarator->id_loc is more appropriate:
19765 	 set, and decl wasn't merged with another decl, in which case its
19766 	 location would be different from input_location, and more accurate.  */
19767       if (DECL_P (decl)
19768 	  && declarator->id_loc != UNKNOWN_LOCATION
19769 	  && DECL_SOURCE_LOCATION (decl) == input_location)
19770 	DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19771     }
19772   else if (scope)
19773     /* Enter the SCOPE.  That way unqualified names appearing in the
19774        initializer will be looked up in SCOPE.  */
19775     pushed_scope = push_scope (scope);
19776 
19777   /* Perform deferred access control checks, now that we know in which
19778      SCOPE the declared entity resides.  */
19779   if (!member_p && decl)
19780     {
19781       tree saved_current_function_decl = NULL_TREE;
19782 
19783       /* If the entity being declared is a function, pretend that we
19784 	 are in its scope.  If it is a `friend', it may have access to
19785 	 things that would not otherwise be accessible.  */
19786       if (TREE_CODE (decl) == FUNCTION_DECL)
19787 	{
19788 	  saved_current_function_decl = current_function_decl;
19789 	  current_function_decl = decl;
19790 	}
19791 
19792       /* Perform access checks for template parameters.  */
19793       cp_parser_perform_template_parameter_access_checks (checks);
19794 
19795       /* Perform the access control checks for the declarator and the
19796 	 decl-specifiers.  */
19797       perform_deferred_access_checks (tf_warning_or_error);
19798 
19799       /* Restore the saved value.  */
19800       if (TREE_CODE (decl) == FUNCTION_DECL)
19801 	current_function_decl = saved_current_function_decl;
19802     }
19803 
19804   /* Parse the initializer.  */
19805   initializer = NULL_TREE;
19806   is_direct_init = false;
19807   is_non_constant_init = true;
19808   if (is_initialized)
19809     {
19810       if (function_declarator_p (declarator))
19811 	{
19812 	   if (initialization_kind == CPP_EQ)
19813 	     initializer = cp_parser_pure_specifier (parser);
19814 	   else
19815 	     {
19816 	       /* If the declaration was erroneous, we don't really
19817 		  know what the user intended, so just silently
19818 		  consume the initializer.  */
19819 	       if (decl != error_mark_node)
19820 		 error_at (tmp_init_loc, "initializer provided for function");
19821 	       cp_parser_skip_to_closing_parenthesis (parser,
19822 						      /*recovering=*/true,
19823 						      /*or_comma=*/false,
19824 						      /*consume_paren=*/true);
19825 	     }
19826 	}
19827       else
19828 	{
19829 	  /* We want to record the extra mangling scope for in-class
19830 	     initializers of class members and initializers of static data
19831 	     member templates.  The former involves deferring
19832 	     parsing of the initializer until end of class as with default
19833 	     arguments.  So right here we only handle the latter.  */
19834 	  if (!member_p && processing_template_decl && decl != error_mark_node)
19835 	    start_lambda_scope (decl);
19836 	  initializer = cp_parser_initializer (parser,
19837 					       &is_direct_init,
19838 					       &is_non_constant_init);
19839 	  if (!member_p && processing_template_decl && decl != error_mark_node)
19840 	    finish_lambda_scope ();
19841 	  if (initializer == error_mark_node)
19842 	    cp_parser_skip_to_end_of_statement (parser);
19843 	}
19844     }
19845 
19846   /* The old parser allows attributes to appear after a parenthesized
19847      initializer.  Mark Mitchell proposed removing this functionality
19848      on the GCC mailing lists on 2002-08-13.  This parser accepts the
19849      attributes -- but ignores them.  Made a permerror in GCC 8.  */
19850   if (cp_parser_allow_gnu_extensions_p (parser)
19851       && initialization_kind == CPP_OPEN_PAREN
19852       && cp_parser_attributes_opt (parser)
19853       && permerror (input_location,
19854 		    "attributes after parenthesized initializer ignored"))
19855     {
19856       static bool hint;
19857       if (flag_permissive && !hint)
19858 	{
19859 	  hint = true;
19860 	  inform (input_location,
19861 		  "this flexibility is deprecated and will be removed");
19862 	}
19863     }
19864 
19865   /* And now complain about a non-function implicit template.  */
19866   if (bogus_implicit_tmpl && decl != error_mark_node)
19867     error_at (DECL_SOURCE_LOCATION (decl),
19868 	      "non-function %qD declared as implicit template", decl);
19869 
19870   /* For an in-class declaration, use `grokfield' to create the
19871      declaration.  */
19872   if (member_p)
19873     {
19874       if (pushed_scope)
19875 	{
19876 	  pop_scope (pushed_scope);
19877 	  pushed_scope = NULL_TREE;
19878 	}
19879       decl = grokfield (declarator, decl_specifiers,
19880 			initializer, !is_non_constant_init,
19881 			/*asmspec=*/NULL_TREE,
19882 			attr_chainon (attributes, prefix_attributes));
19883       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19884 	cp_parser_save_default_args (parser, decl);
19885       cp_finalize_omp_declare_simd (parser, decl);
19886       cp_finalize_oacc_routine (parser, decl, false);
19887     }
19888 
19889   /* Finish processing the declaration.  But, skip member
19890      declarations.  */
19891   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19892     {
19893       cp_finish_decl (decl,
19894 		      initializer, !is_non_constant_init,
19895 		      asm_specification,
19896 		      /* If the initializer is in parentheses, then this is
19897 			 a direct-initialization, which means that an
19898 			 `explicit' constructor is OK.  Otherwise, an
19899 			 `explicit' constructor cannot be used.  */
19900 		      ((is_direct_init || !is_initialized)
19901 		       ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19902     }
19903   else if ((cxx_dialect != cxx98) && friend_p
19904 	   && decl && TREE_CODE (decl) == FUNCTION_DECL)
19905     /* Core issue #226 (C++0x only): A default template-argument
19906        shall not be specified in a friend class template
19907        declaration. */
19908     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19909                              /*is_partial=*/false, /*is_friend_decl=*/1);
19910 
19911   if (!friend_p && pushed_scope)
19912     pop_scope (pushed_scope);
19913 
19914   if (function_declarator_p (declarator)
19915       && parser->fully_implicit_function_template_p)
19916     {
19917       if (member_p)
19918 	decl = finish_fully_implicit_template (parser, decl);
19919       else
19920 	finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19921     }
19922 
19923   if (auto_result && is_initialized && decl_specifiers->type
19924       && type_uses_auto (decl_specifiers->type))
19925     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19926 
19927   return decl;
19928 }
19929 
19930 /* Parse a declarator.
19931 
19932    declarator:
19933      direct-declarator
19934      ptr-operator declarator
19935 
19936    abstract-declarator:
19937      ptr-operator abstract-declarator [opt]
19938      direct-abstract-declarator
19939 
19940    GNU Extensions:
19941 
19942    declarator:
19943      attributes [opt] direct-declarator
19944      attributes [opt] ptr-operator declarator
19945 
19946    abstract-declarator:
19947      attributes [opt] ptr-operator abstract-declarator [opt]
19948      attributes [opt] direct-abstract-declarator
19949 
19950    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19951    detect constructors, destructors, deduction guides, or conversion operators.
19952    It is set to -1 if the declarator is a name, and +1 if it is a
19953    function. Otherwise it is set to zero. Usually you just want to
19954    test for >0, but internally the negative value is used.
19955 
19956    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19957    a decl-specifier-seq unless it declares a constructor, destructor,
19958    or conversion.  It might seem that we could check this condition in
19959    semantic analysis, rather than parsing, but that makes it difficult
19960    to handle something like `f()'.  We want to notice that there are
19961    no decl-specifiers, and therefore realize that this is an
19962    expression, not a declaration.)
19963 
19964    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19965    the declarator is a direct-declarator of the form "(...)".
19966 
19967    MEMBER_P is true iff this declarator is a member-declarator.
19968 
19969    FRIEND_P is true iff this declarator is a friend.  */
19970 
19971 static cp_declarator *
cp_parser_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p,bool * parenthesized_p,bool member_p,bool friend_p)19972 cp_parser_declarator (cp_parser* parser,
19973 		      cp_parser_declarator_kind dcl_kind,
19974 		      int* ctor_dtor_or_conv_p,
19975 		      bool* parenthesized_p,
19976 		      bool member_p, bool friend_p)
19977 {
19978   cp_declarator *declarator;
19979   enum tree_code code;
19980   cp_cv_quals cv_quals;
19981   tree class_type;
19982   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19983 
19984   /* Assume this is not a constructor, destructor, or type-conversion
19985      operator.  */
19986   if (ctor_dtor_or_conv_p)
19987     *ctor_dtor_or_conv_p = 0;
19988 
19989   if (cp_parser_allow_gnu_extensions_p (parser))
19990     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19991 
19992   /* Check for the ptr-operator production.  */
19993   cp_parser_parse_tentatively (parser);
19994   /* Parse the ptr-operator.  */
19995   code = cp_parser_ptr_operator (parser,
19996 				 &class_type,
19997 				 &cv_quals,
19998 				 &std_attributes);
19999 
20000   /* If that worked, then we have a ptr-operator.  */
20001   if (cp_parser_parse_definitely (parser))
20002     {
20003       /* If a ptr-operator was found, then this declarator was not
20004 	 parenthesized.  */
20005       if (parenthesized_p)
20006 	*parenthesized_p = true;
20007       /* The dependent declarator is optional if we are parsing an
20008 	 abstract-declarator.  */
20009       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20010 	cp_parser_parse_tentatively (parser);
20011 
20012       /* Parse the dependent declarator.  */
20013       declarator = cp_parser_declarator (parser, dcl_kind,
20014 					 /*ctor_dtor_or_conv_p=*/NULL,
20015 					 /*parenthesized_p=*/NULL,
20016 					 /*member_p=*/false,
20017 					 friend_p);
20018 
20019       /* If we are parsing an abstract-declarator, we must handle the
20020 	 case where the dependent declarator is absent.  */
20021       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20022 	  && !cp_parser_parse_definitely (parser))
20023 	declarator = NULL;
20024 
20025       declarator = cp_parser_make_indirect_declarator
20026 	(code, class_type, cv_quals, declarator, std_attributes);
20027     }
20028   /* Everything else is a direct-declarator.  */
20029   else
20030     {
20031       if (parenthesized_p)
20032 	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20033 						   CPP_OPEN_PAREN);
20034       declarator = cp_parser_direct_declarator (parser, dcl_kind,
20035 						ctor_dtor_or_conv_p,
20036 						member_p, friend_p);
20037     }
20038 
20039   if (gnu_attributes && declarator && declarator != cp_error_declarator)
20040     declarator->attributes = gnu_attributes;
20041   return declarator;
20042 }
20043 
20044 /* Parse a direct-declarator or direct-abstract-declarator.
20045 
20046    direct-declarator:
20047      declarator-id
20048      direct-declarator ( parameter-declaration-clause )
20049        cv-qualifier-seq [opt]
20050        ref-qualifier [opt]
20051        exception-specification [opt]
20052      direct-declarator [ constant-expression [opt] ]
20053      ( declarator )
20054 
20055    direct-abstract-declarator:
20056      direct-abstract-declarator [opt]
20057        ( parameter-declaration-clause )
20058        cv-qualifier-seq [opt]
20059        ref-qualifier [opt]
20060        exception-specification [opt]
20061      direct-abstract-declarator [opt] [ constant-expression [opt] ]
20062      ( abstract-declarator )
20063 
20064    Returns a representation of the declarator.  DCL_KIND is
20065    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20066    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
20067    we are parsing a direct-declarator.  It is
20068    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20069    of ambiguity we prefer an abstract declarator, as per
20070    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20071    as for cp_parser_declarator.  */
20072 
20073 static cp_declarator *
cp_parser_direct_declarator(cp_parser * parser,cp_parser_declarator_kind dcl_kind,int * ctor_dtor_or_conv_p,bool member_p,bool friend_p)20074 cp_parser_direct_declarator (cp_parser* parser,
20075 			     cp_parser_declarator_kind dcl_kind,
20076 			     int* ctor_dtor_or_conv_p,
20077 			     bool member_p, bool friend_p)
20078 {
20079   cp_token *token;
20080   cp_declarator *declarator = NULL;
20081   tree scope = NULL_TREE;
20082   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20083   bool saved_in_declarator_p = parser->in_declarator_p;
20084   bool first = true;
20085   tree pushed_scope = NULL_TREE;
20086   cp_token *open_paren = NULL, *close_paren = NULL;
20087 
20088   while (true)
20089     {
20090       /* Peek at the next token.  */
20091       token = cp_lexer_peek_token (parser->lexer);
20092       if (token->type == CPP_OPEN_PAREN)
20093 	{
20094 	  /* This is either a parameter-declaration-clause, or a
20095 	     parenthesized declarator. When we know we are parsing a
20096 	     named declarator, it must be a parenthesized declarator
20097 	     if FIRST is true. For instance, `(int)' is a
20098 	     parameter-declaration-clause, with an omitted
20099 	     direct-abstract-declarator. But `((*))', is a
20100 	     parenthesized abstract declarator. Finally, when T is a
20101 	     template parameter `(T)' is a
20102 	     parameter-declaration-clause, and not a parenthesized
20103 	     named declarator.
20104 
20105 	     We first try and parse a parameter-declaration-clause,
20106 	     and then try a nested declarator (if FIRST is true).
20107 
20108 	     It is not an error for it not to be a
20109 	     parameter-declaration-clause, even when FIRST is
20110 	     false. Consider,
20111 
20112 	       int i (int);
20113 	       int i (3);
20114 
20115 	     The first is the declaration of a function while the
20116 	     second is the definition of a variable, including its
20117 	     initializer.
20118 
20119 	     Having seen only the parenthesis, we cannot know which of
20120 	     these two alternatives should be selected.  Even more
20121 	     complex are examples like:
20122 
20123 	       int i (int (a));
20124 	       int i (int (3));
20125 
20126 	     The former is a function-declaration; the latter is a
20127 	     variable initialization.
20128 
20129 	     Thus again, we try a parameter-declaration-clause, and if
20130 	     that fails, we back out and return.  */
20131 
20132 	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20133 	    {
20134 	      tree params;
20135 	      bool is_declarator = false;
20136 
20137 	      open_paren = NULL;
20138 
20139 	      /* In a member-declarator, the only valid interpretation
20140 		 of a parenthesis is the start of a
20141 		 parameter-declaration-clause.  (It is invalid to
20142 		 initialize a static data member with a parenthesized
20143 		 initializer; only the "=" form of initialization is
20144 		 permitted.)  */
20145 	      if (!member_p)
20146 		cp_parser_parse_tentatively (parser);
20147 
20148 	      /* Consume the `('.  */
20149 	      matching_parens parens;
20150 	      parens.consume_open (parser);
20151 	      if (first)
20152 		{
20153 		  /* If this is going to be an abstract declarator, we're
20154 		     in a declarator and we can't have default args.  */
20155 		  parser->default_arg_ok_p = false;
20156 		  parser->in_declarator_p = true;
20157 		}
20158 
20159 	      begin_scope (sk_function_parms, NULL_TREE);
20160 
20161 	      /* Parse the parameter-declaration-clause.  */
20162 	      params = cp_parser_parameter_declaration_clause (parser);
20163 
20164 	      /* Consume the `)'.  */
20165 	      parens.require_close (parser);
20166 
20167 	      /* If all went well, parse the cv-qualifier-seq,
20168 		 ref-qualifier and the exception-specification.  */
20169 	      if (member_p || cp_parser_parse_definitely (parser))
20170 		{
20171 		  cp_cv_quals cv_quals;
20172 		  cp_virt_specifiers virt_specifiers;
20173 		  cp_ref_qualifier ref_qual;
20174 		  tree exception_specification;
20175 		  tree late_return;
20176 		  tree attrs;
20177 		  bool memfn = (member_p || (pushed_scope
20178 					     && CLASS_TYPE_P (pushed_scope)));
20179 
20180 		  is_declarator = true;
20181 
20182 		  if (ctor_dtor_or_conv_p)
20183 		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20184 		  first = false;
20185 
20186 		  /* Parse the cv-qualifier-seq.  */
20187 		  cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20188 		  /* Parse the ref-qualifier. */
20189 		  ref_qual = cp_parser_ref_qualifier_opt (parser);
20190 		  /* Parse the tx-qualifier.  */
20191 		  tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20192 		  /* And the exception-specification.  */
20193 		  exception_specification
20194 		    = cp_parser_exception_specification_opt (parser);
20195 
20196 		  attrs = cp_parser_std_attribute_spec_seq (parser);
20197 
20198 		  /* In here, we handle cases where attribute is used after
20199 		     the function declaration.  For example:
20200 		     void func (int x) __attribute__((vector(..)));  */
20201 		  tree gnu_attrs = NULL_TREE;
20202 		  tree requires_clause = NULL_TREE;
20203 		  late_return = (cp_parser_late_return_type_opt
20204 				 (parser, declarator, requires_clause,
20205 				  memfn ? cv_quals : -1));
20206 
20207 		  /* Parse the virt-specifier-seq.  */
20208 		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20209 
20210 		  /* Create the function-declarator.  */
20211 		  declarator = make_call_declarator (declarator,
20212 						     params,
20213 						     cv_quals,
20214 						     virt_specifiers,
20215 						     ref_qual,
20216 						     tx_qual,
20217 						     exception_specification,
20218 						     late_return,
20219 						     requires_clause);
20220 		  declarator->std_attributes = attrs;
20221 		  declarator->attributes = gnu_attrs;
20222 		  /* Any subsequent parameter lists are to do with
20223 		     return type, so are not those of the declared
20224 		     function.  */
20225 		  parser->default_arg_ok_p = false;
20226 		}
20227 
20228 	      /* Remove the function parms from scope.  */
20229 	      pop_bindings_and_leave_scope ();
20230 
20231 	      if (is_declarator)
20232 		/* Repeat the main loop.  */
20233 		continue;
20234 	    }
20235 
20236 	  /* If this is the first, we can try a parenthesized
20237 	     declarator.  */
20238 	  if (first)
20239 	    {
20240 	      bool saved_in_type_id_in_expr_p;
20241 
20242 	      parser->default_arg_ok_p = saved_default_arg_ok_p;
20243 	      parser->in_declarator_p = saved_in_declarator_p;
20244 
20245 	      open_paren = token;
20246 	      /* Consume the `('.  */
20247 	      matching_parens parens;
20248 	      parens.consume_open (parser);
20249 	      /* Parse the nested declarator.  */
20250 	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20251 	      parser->in_type_id_in_expr_p = true;
20252 	      declarator
20253 		= cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20254 					/*parenthesized_p=*/NULL,
20255 					member_p, friend_p);
20256 	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20257 	      first = false;
20258 	      /* Expect a `)'.  */
20259 	      close_paren = cp_lexer_peek_token (parser->lexer);
20260 	      if (!parens.require_close (parser))
20261 		declarator = cp_error_declarator;
20262 	      if (declarator == cp_error_declarator)
20263 		break;
20264 
20265 	      goto handle_declarator;
20266 	    }
20267 	  /* Otherwise, we must be done.  */
20268 	  else
20269 	    break;
20270 	}
20271       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20272 	       && token->type == CPP_OPEN_SQUARE
20273 	       && !cp_next_tokens_can_be_attribute_p (parser))
20274 	{
20275 	  /* Parse an array-declarator.  */
20276 	  tree bounds, attrs;
20277 
20278 	  if (ctor_dtor_or_conv_p)
20279 	    *ctor_dtor_or_conv_p = 0;
20280 
20281 	  open_paren = NULL;
20282 	  first = false;
20283 	  parser->default_arg_ok_p = false;
20284 	  parser->in_declarator_p = true;
20285 	  /* Consume the `['.  */
20286 	  cp_lexer_consume_token (parser->lexer);
20287 	  /* Peek at the next token.  */
20288 	  token = cp_lexer_peek_token (parser->lexer);
20289 	  /* If the next token is `]', then there is no
20290 	     constant-expression.  */
20291 	  if (token->type != CPP_CLOSE_SQUARE)
20292 	    {
20293 	      bool non_constant_p;
20294 	      bounds
20295 		= cp_parser_constant_expression (parser,
20296 						 /*allow_non_constant=*/true,
20297 						 &non_constant_p);
20298 	      if (!non_constant_p)
20299 		/* OK */;
20300 	      else if (error_operand_p (bounds))
20301 		/* Already gave an error.  */;
20302 	      else if (!parser->in_function_body
20303 		       || current_binding_level->kind == sk_function_parms)
20304 		{
20305 		  /* Normally, the array bound must be an integral constant
20306 		     expression.  However, as an extension, we allow VLAs
20307 		     in function scopes as long as they aren't part of a
20308 		     parameter declaration.  */
20309 		  cp_parser_error (parser,
20310 				   "array bound is not an integer constant");
20311 		  bounds = error_mark_node;
20312 		}
20313 	      else if (processing_template_decl
20314 		       && !type_dependent_expression_p (bounds))
20315 		{
20316 		  /* Remember this wasn't a constant-expression.  */
20317 		  bounds = build_nop (TREE_TYPE (bounds), bounds);
20318 		  TREE_SIDE_EFFECTS (bounds) = 1;
20319 		}
20320 	    }
20321 	  else
20322 	    bounds = NULL_TREE;
20323 	  /* Look for the closing `]'.  */
20324 	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20325 	    {
20326 	      declarator = cp_error_declarator;
20327 	      break;
20328 	    }
20329 
20330 	  attrs = cp_parser_std_attribute_spec_seq (parser);
20331 	  declarator = make_array_declarator (declarator, bounds);
20332 	  declarator->std_attributes = attrs;
20333 	}
20334       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20335 	{
20336 	  {
20337 	    tree qualifying_scope;
20338 	    tree unqualified_name;
20339 	    tree attrs;
20340 	    special_function_kind sfk;
20341 	    bool abstract_ok;
20342 	    bool pack_expansion_p = false;
20343 	    cp_token *declarator_id_start_token;
20344 
20345 	    /* Parse a declarator-id */
20346 	    abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20347 	    if (abstract_ok)
20348 	      {
20349 		cp_parser_parse_tentatively (parser);
20350 
20351 		/* If we see an ellipsis, we should be looking at a
20352 		   parameter pack. */
20353 		if (token->type == CPP_ELLIPSIS)
20354 		  {
20355 		    /* Consume the `...' */
20356 		    cp_lexer_consume_token (parser->lexer);
20357 
20358 		    pack_expansion_p = true;
20359 		  }
20360 	      }
20361 
20362 	    declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20363 	    unqualified_name
20364 	      = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20365 	    qualifying_scope = parser->scope;
20366 	    if (abstract_ok)
20367 	      {
20368 		bool okay = false;
20369 
20370 		if (!unqualified_name && pack_expansion_p)
20371 		  {
20372 		    /* Check whether an error occurred. */
20373 		    okay = !cp_parser_error_occurred (parser);
20374 
20375 		    /* We already consumed the ellipsis to mark a
20376 		       parameter pack, but we have no way to report it,
20377 		       so abort the tentative parse. We will be exiting
20378 		       immediately anyway. */
20379 		    cp_parser_abort_tentative_parse (parser);
20380 		  }
20381 		else
20382 		  okay = cp_parser_parse_definitely (parser);
20383 
20384 		if (!okay)
20385 		  unqualified_name = error_mark_node;
20386 		else if (unqualified_name
20387 			 && (qualifying_scope
20388 			     || (!identifier_p (unqualified_name))))
20389 		  {
20390 		    cp_parser_error (parser, "expected unqualified-id");
20391 		    unqualified_name = error_mark_node;
20392 		  }
20393 	      }
20394 
20395 	    if (!unqualified_name)
20396 	      return NULL;
20397 	    if (unqualified_name == error_mark_node)
20398 	      {
20399 		declarator = cp_error_declarator;
20400 		pack_expansion_p = false;
20401 		declarator->parameter_pack_p = false;
20402 		break;
20403 	      }
20404 
20405 	    attrs = cp_parser_std_attribute_spec_seq (parser);
20406 
20407 	    if (qualifying_scope && at_namespace_scope_p ()
20408 		&& TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20409 	      {
20410 		/* In the declaration of a member of a template class
20411 		   outside of the class itself, the SCOPE will sometimes
20412 		   be a TYPENAME_TYPE.  For example, given:
20413 
20414 		   template <typename T>
20415 		   int S<T>::R::i = 3;
20416 
20417 		   the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
20418 		   this context, we must resolve S<T>::R to an ordinary
20419 		   type, rather than a typename type.
20420 
20421 		   The reason we normally avoid resolving TYPENAME_TYPEs
20422 		   is that a specialization of `S' might render
20423 		   `S<T>::R' not a type.  However, if `S' is
20424 		   specialized, then this `i' will not be used, so there
20425 		   is no harm in resolving the types here.  */
20426 		tree type;
20427 
20428 		/* Resolve the TYPENAME_TYPE.  */
20429 		type = resolve_typename_type (qualifying_scope,
20430 					      /*only_current_p=*/false);
20431 		/* If that failed, the declarator is invalid.  */
20432 		if (TREE_CODE (type) == TYPENAME_TYPE)
20433 		  {
20434 		    if (typedef_variant_p (type))
20435 		      error_at (declarator_id_start_token->location,
20436 				"cannot define member of dependent typedef "
20437 				"%qT", type);
20438 		    else
20439 		      error_at (declarator_id_start_token->location,
20440 				"%<%T::%E%> is not a type",
20441 				TYPE_CONTEXT (qualifying_scope),
20442 				TYPE_IDENTIFIER (qualifying_scope));
20443 		  }
20444 		qualifying_scope = type;
20445 	      }
20446 
20447 	    sfk = sfk_none;
20448 
20449 	    if (unqualified_name)
20450 	      {
20451 		tree class_type;
20452 
20453 		if (qualifying_scope
20454 		    && CLASS_TYPE_P (qualifying_scope))
20455 		  class_type = qualifying_scope;
20456 		else
20457 		  class_type = current_class_type;
20458 
20459 		if (TREE_CODE (unqualified_name) == TYPE_DECL)
20460 		  {
20461 		    tree name_type = TREE_TYPE (unqualified_name);
20462 
20463 		    if (!class_type || !same_type_p (name_type, class_type))
20464 		      {
20465 			/* We do not attempt to print the declarator
20466 			   here because we do not have enough
20467 			   information about its original syntactic
20468 			   form.  */
20469 			cp_parser_error (parser, "invalid declarator");
20470 			declarator = cp_error_declarator;
20471 			break;
20472 		      }
20473 		    else if (qualifying_scope
20474 			     && CLASSTYPE_USE_TEMPLATE (name_type))
20475 		      {
20476 			error_at (declarator_id_start_token->location,
20477 				  "invalid use of constructor as a template");
20478 			inform (declarator_id_start_token->location,
20479 				"use %<%T::%D%> instead of %<%T::%D%> to "
20480 				"name the constructor in a qualified name",
20481 				class_type,
20482 				DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20483 				class_type, name_type);
20484 			declarator = cp_error_declarator;
20485 			break;
20486 		      }
20487 		    unqualified_name = constructor_name (class_type);
20488 		  }
20489 
20490 		if (class_type)
20491 		  {
20492 		    if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20493 		      sfk = sfk_destructor;
20494 		    else if (identifier_p (unqualified_name)
20495 			     && IDENTIFIER_CONV_OP_P (unqualified_name))
20496 		      sfk = sfk_conversion;
20497 		    else if (/* There's no way to declare a constructor
20498 				for an unnamed type, even if the type
20499 				got a name for linkage purposes.  */
20500 			     !TYPE_WAS_UNNAMED (class_type)
20501 			     /* Handle correctly (c++/19200):
20502 
20503 				struct S {
20504 				  struct T{};
20505 				  friend void S(T);
20506 				};
20507 
20508 				and also:
20509 
20510 				namespace N {
20511 				  void S();
20512 				}
20513 
20514 				struct S {
20515 				  friend void N::S();
20516 				};  */
20517 			     && (!friend_p || class_type == qualifying_scope)
20518 			     && constructor_name_p (unqualified_name,
20519 						    class_type))
20520 		      sfk = sfk_constructor;
20521 		    else if (is_overloaded_fn (unqualified_name)
20522 			     && DECL_CONSTRUCTOR_P (get_first_fn
20523 						    (unqualified_name)))
20524 		      sfk = sfk_constructor;
20525 
20526 		    if (ctor_dtor_or_conv_p && sfk != sfk_none)
20527 		      *ctor_dtor_or_conv_p = -1;
20528 		  }
20529 	      }
20530 	    declarator = make_id_declarator (qualifying_scope,
20531 					     unqualified_name,
20532 					     sfk);
20533 	    declarator->std_attributes = attrs;
20534 	    declarator->id_loc = token->location;
20535 	    declarator->parameter_pack_p = pack_expansion_p;
20536 
20537 	    if (pack_expansion_p)
20538 	      maybe_warn_variadic_templates ();
20539 	  }
20540 
20541 	handle_declarator:;
20542 	  scope = get_scope_of_declarator (declarator);
20543 	  if (scope)
20544 	    {
20545 	      /* Any names that appear after the declarator-id for a
20546 		 member are looked up in the containing scope.  */
20547 	      if (at_function_scope_p ())
20548 		{
20549 		  /* But declarations with qualified-ids can't appear in a
20550 		     function.  */
20551 		  cp_parser_error (parser, "qualified-id in declaration");
20552 		  declarator = cp_error_declarator;
20553 		  break;
20554 		}
20555 	      pushed_scope = push_scope (scope);
20556 	    }
20557 	  parser->in_declarator_p = true;
20558 	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20559 	      || (declarator && declarator->kind == cdk_id))
20560 	    /* Default args are only allowed on function
20561 	       declarations.  */
20562 	    parser->default_arg_ok_p = saved_default_arg_ok_p;
20563 	  else
20564 	    parser->default_arg_ok_p = false;
20565 
20566 	  first = false;
20567 	}
20568       /* We're done.  */
20569       else
20570 	break;
20571     }
20572 
20573   /* For an abstract declarator, we might wind up with nothing at this
20574      point.  That's an error; the declarator is not optional.  */
20575   if (!declarator)
20576     cp_parser_error (parser, "expected declarator");
20577   else if (open_paren)
20578     {
20579       /* Record overly parenthesized declarator so we can give a
20580 	 diagnostic about confusing decl/expr disambiguation.  */
20581       if (declarator->kind == cdk_array)
20582 	{
20583 	  /* If the open and close parens are on different lines, this
20584 	     is probably a formatting thing, so ignore.  */
20585 	  expanded_location open = expand_location (open_paren->location);
20586 	  expanded_location close = expand_location (close_paren->location);
20587 	  if (open.line != close.line || open.file != close.file)
20588 	    open_paren = NULL;
20589 	}
20590       if (open_paren)
20591 	declarator->parenthesized = open_paren->location;
20592     }
20593 
20594   /* If we entered a scope, we must exit it now.  */
20595   if (pushed_scope)
20596     pop_scope (pushed_scope);
20597 
20598   parser->default_arg_ok_p = saved_default_arg_ok_p;
20599   parser->in_declarator_p = saved_in_declarator_p;
20600 
20601   return declarator;
20602 }
20603 
20604 /* Parse a ptr-operator.
20605 
20606    ptr-operator:
20607      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20608      * cv-qualifier-seq [opt]
20609      &
20610      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20611      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20612 
20613    GNU Extension:
20614 
20615    ptr-operator:
20616      & cv-qualifier-seq [opt]
20617 
20618    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20619    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20620    an rvalue reference. In the case of a pointer-to-member, *TYPE is
20621    filled in with the TYPE containing the member.  *CV_QUALS is
20622    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20623    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
20624    Note that the tree codes returned by this function have nothing
20625    to do with the types of trees that will be eventually be created
20626    to represent the pointer or reference type being parsed. They are
20627    just constants with suggestive names. */
20628 static enum tree_code
cp_parser_ptr_operator(cp_parser * parser,tree * type,cp_cv_quals * cv_quals,tree * attributes)20629 cp_parser_ptr_operator (cp_parser* parser,
20630 			tree* type,
20631 			cp_cv_quals *cv_quals,
20632 			tree *attributes)
20633 {
20634   enum tree_code code = ERROR_MARK;
20635   cp_token *token;
20636   tree attrs = NULL_TREE;
20637 
20638   /* Assume that it's not a pointer-to-member.  */
20639   *type = NULL_TREE;
20640   /* And that there are no cv-qualifiers.  */
20641   *cv_quals = TYPE_UNQUALIFIED;
20642 
20643   /* Peek at the next token.  */
20644   token = cp_lexer_peek_token (parser->lexer);
20645 
20646   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
20647   if (token->type == CPP_MULT)
20648     code = INDIRECT_REF;
20649   else if (token->type == CPP_AND)
20650     code = ADDR_EXPR;
20651   else if ((cxx_dialect != cxx98) &&
20652 	   token->type == CPP_AND_AND) /* C++0x only */
20653     code = NON_LVALUE_EXPR;
20654 
20655   if (code != ERROR_MARK)
20656     {
20657       /* Consume the `*', `&' or `&&'.  */
20658       cp_lexer_consume_token (parser->lexer);
20659 
20660       /* A `*' can be followed by a cv-qualifier-seq, and so can a
20661 	 `&', if we are allowing GNU extensions.  (The only qualifier
20662 	 that can legally appear after `&' is `restrict', but that is
20663 	 enforced during semantic analysis.  */
20664       if (code == INDIRECT_REF
20665 	  || cp_parser_allow_gnu_extensions_p (parser))
20666 	*cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20667 
20668       attrs = cp_parser_std_attribute_spec_seq (parser);
20669       if (attributes != NULL)
20670 	*attributes = attrs;
20671     }
20672   else
20673     {
20674       /* Try the pointer-to-member case.  */
20675       cp_parser_parse_tentatively (parser);
20676       /* Look for the optional `::' operator.  */
20677       cp_parser_global_scope_opt (parser,
20678 				  /*current_scope_valid_p=*/false);
20679       /* Look for the nested-name specifier.  */
20680       token = cp_lexer_peek_token (parser->lexer);
20681       cp_parser_nested_name_specifier (parser,
20682 				       /*typename_keyword_p=*/false,
20683 				       /*check_dependency_p=*/true,
20684 				       /*type_p=*/false,
20685 				       /*is_declaration=*/false);
20686       /* If we found it, and the next token is a `*', then we are
20687 	 indeed looking at a pointer-to-member operator.  */
20688       if (!cp_parser_error_occurred (parser)
20689 	  && cp_parser_require (parser, CPP_MULT, RT_MULT))
20690 	{
20691 	  /* Indicate that the `*' operator was used.  */
20692 	  code = INDIRECT_REF;
20693 
20694 	  if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20695 	    error_at (token->location, "%qD is a namespace", parser->scope);
20696 	  else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20697 	    error_at (token->location, "cannot form pointer to member of "
20698 		      "non-class %q#T", parser->scope);
20699 	  else
20700 	    {
20701 	      /* The type of which the member is a member is given by the
20702 		 current SCOPE.  */
20703 	      *type = parser->scope;
20704 	      /* The next name will not be qualified.  */
20705 	      parser->scope = NULL_TREE;
20706 	      parser->qualifying_scope = NULL_TREE;
20707 	      parser->object_scope = NULL_TREE;
20708 	      /* Look for optional c++11 attributes.  */
20709 	      attrs = cp_parser_std_attribute_spec_seq (parser);
20710 	      if (attributes != NULL)
20711 		*attributes = attrs;
20712 	      /* Look for the optional cv-qualifier-seq.  */
20713 	      *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20714 	    }
20715 	}
20716       /* If that didn't work we don't have a ptr-operator.  */
20717       if (!cp_parser_parse_definitely (parser))
20718 	cp_parser_error (parser, "expected ptr-operator");
20719     }
20720 
20721   return code;
20722 }
20723 
20724 /* Parse an (optional) cv-qualifier-seq.
20725 
20726    cv-qualifier-seq:
20727      cv-qualifier cv-qualifier-seq [opt]
20728 
20729    cv-qualifier:
20730      const
20731      volatile
20732 
20733    GNU Extension:
20734 
20735    cv-qualifier:
20736      __restrict__
20737 
20738    Returns a bitmask representing the cv-qualifiers.  */
20739 
20740 static cp_cv_quals
cp_parser_cv_qualifier_seq_opt(cp_parser * parser)20741 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20742 {
20743   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20744 
20745   while (true)
20746     {
20747       cp_token *token;
20748       cp_cv_quals cv_qualifier;
20749 
20750       /* Peek at the next token.  */
20751       token = cp_lexer_peek_token (parser->lexer);
20752       /* See if it's a cv-qualifier.  */
20753       switch (token->keyword)
20754 	{
20755 	case RID_CONST:
20756 	  cv_qualifier = TYPE_QUAL_CONST;
20757 	  break;
20758 
20759 	case RID_VOLATILE:
20760 	  cv_qualifier = TYPE_QUAL_VOLATILE;
20761 	  break;
20762 
20763 	case RID_RESTRICT:
20764 	  cv_qualifier = TYPE_QUAL_RESTRICT;
20765 	  break;
20766 
20767 	default:
20768 	  cv_qualifier = TYPE_UNQUALIFIED;
20769 	  break;
20770 	}
20771 
20772       if (!cv_qualifier)
20773 	break;
20774 
20775       if (cv_quals & cv_qualifier)
20776 	{
20777 	  gcc_rich_location richloc (token->location);
20778 	  richloc.add_fixit_remove ();
20779 	  error_at (&richloc, "duplicate cv-qualifier");
20780 	  cp_lexer_purge_token (parser->lexer);
20781 	}
20782       else
20783 	{
20784 	  cp_lexer_consume_token (parser->lexer);
20785 	  cv_quals |= cv_qualifier;
20786 	}
20787     }
20788 
20789   return cv_quals;
20790 }
20791 
20792 /* Parse an (optional) ref-qualifier
20793 
20794    ref-qualifier:
20795      &
20796      &&
20797 
20798    Returns cp_ref_qualifier representing ref-qualifier. */
20799 
20800 static cp_ref_qualifier
cp_parser_ref_qualifier_opt(cp_parser * parser)20801 cp_parser_ref_qualifier_opt (cp_parser* parser)
20802 {
20803   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20804 
20805   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
20806   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20807     return ref_qual;
20808 
20809   while (true)
20810     {
20811       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20812       cp_token *token = cp_lexer_peek_token (parser->lexer);
20813 
20814       switch (token->type)
20815 	{
20816 	case CPP_AND:
20817 	  curr_ref_qual = REF_QUAL_LVALUE;
20818 	  break;
20819 
20820 	case CPP_AND_AND:
20821 	  curr_ref_qual = REF_QUAL_RVALUE;
20822 	  break;
20823 
20824 	default:
20825 	  curr_ref_qual = REF_QUAL_NONE;
20826 	  break;
20827 	}
20828 
20829       if (!curr_ref_qual)
20830 	break;
20831       else if (ref_qual)
20832 	{
20833 	  error_at (token->location, "multiple ref-qualifiers");
20834 	  cp_lexer_purge_token (parser->lexer);
20835 	}
20836       else
20837 	{
20838 	  ref_qual = curr_ref_qual;
20839 	  cp_lexer_consume_token (parser->lexer);
20840 	}
20841     }
20842 
20843   return ref_qual;
20844 }
20845 
20846 /* Parse an optional tx-qualifier.
20847 
20848    tx-qualifier:
20849      transaction_safe
20850      transaction_safe_dynamic  */
20851 
20852 static tree
cp_parser_tx_qualifier_opt(cp_parser * parser)20853 cp_parser_tx_qualifier_opt (cp_parser *parser)
20854 {
20855   cp_token *token = cp_lexer_peek_token (parser->lexer);
20856   if (token->type == CPP_NAME)
20857     {
20858       tree name = token->u.value;
20859       const char *p = IDENTIFIER_POINTER (name);
20860       const int len = strlen ("transaction_safe");
20861       if (!strncmp (p, "transaction_safe", len))
20862 	{
20863 	  p += len;
20864 	  if (*p == '\0'
20865 	      || !strcmp (p, "_dynamic"))
20866 	    {
20867 	      cp_lexer_consume_token (parser->lexer);
20868 	      if (!flag_tm)
20869 		{
20870 		  error ("%qE requires %<-fgnu-tm%>", name);
20871 		  return NULL_TREE;
20872 		}
20873 	      else
20874 		return name;
20875 	    }
20876 	}
20877     }
20878   return NULL_TREE;
20879 }
20880 
20881 /* Parse an (optional) virt-specifier-seq.
20882 
20883    virt-specifier-seq:
20884      virt-specifier virt-specifier-seq [opt]
20885 
20886    virt-specifier:
20887      override
20888      final
20889 
20890    Returns a bitmask representing the virt-specifiers.  */
20891 
20892 static cp_virt_specifiers
cp_parser_virt_specifier_seq_opt(cp_parser * parser)20893 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20894 {
20895   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20896 
20897   while (true)
20898     {
20899       cp_token *token;
20900       cp_virt_specifiers virt_specifier;
20901 
20902       /* Peek at the next token.  */
20903       token = cp_lexer_peek_token (parser->lexer);
20904       /* See if it's a virt-specifier-qualifier.  */
20905       if (token->type != CPP_NAME)
20906         break;
20907       if (id_equal (token->u.value, "override"))
20908         {
20909           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20910           virt_specifier = VIRT_SPEC_OVERRIDE;
20911         }
20912       else if (id_equal (token->u.value, "final"))
20913         {
20914           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20915           virt_specifier = VIRT_SPEC_FINAL;
20916         }
20917       else if (id_equal (token->u.value, "__final"))
20918         {
20919           virt_specifier = VIRT_SPEC_FINAL;
20920         }
20921       else
20922 	break;
20923 
20924       if (virt_specifiers & virt_specifier)
20925 	{
20926 	  gcc_rich_location richloc (token->location);
20927 	  richloc.add_fixit_remove ();
20928 	  error_at (&richloc, "duplicate virt-specifier");
20929 	  cp_lexer_purge_token (parser->lexer);
20930 	}
20931       else
20932 	{
20933 	  cp_lexer_consume_token (parser->lexer);
20934 	  virt_specifiers |= virt_specifier;
20935 	}
20936     }
20937   return virt_specifiers;
20938 }
20939 
20940 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20941    is in scope even though it isn't real.  */
20942 
20943 void
inject_this_parameter(tree ctype,cp_cv_quals quals)20944 inject_this_parameter (tree ctype, cp_cv_quals quals)
20945 {
20946   tree this_parm;
20947 
20948   if (current_class_ptr)
20949     {
20950       /* We don't clear this between NSDMIs.  Is it already what we want?  */
20951       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20952       if (DECL_P (current_class_ptr)
20953 	  && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20954 	  && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20955 	  && cp_type_quals (type) == quals)
20956 	return;
20957     }
20958 
20959   this_parm = build_this_parm (NULL_TREE, ctype, quals);
20960   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
20961   current_class_ptr = NULL_TREE;
20962   current_class_ref
20963     = cp_build_fold_indirect_ref (this_parm);
20964   current_class_ptr = this_parm;
20965 }
20966 
20967 /* Return true iff our current scope is a non-static data member
20968    initializer.  */
20969 
20970 bool
parsing_nsdmi(void)20971 parsing_nsdmi (void)
20972 {
20973   /* We recognize NSDMI context by the context-less 'this' pointer set up
20974      by the function above.  */
20975   if (current_class_ptr
20976       && TREE_CODE (current_class_ptr) == PARM_DECL
20977       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20978     return true;
20979   return false;
20980 }
20981 
20982 /* Parse a late-specified return type, if any.  This is not a separate
20983    non-terminal, but part of a function declarator, which looks like
20984 
20985    -> trailing-type-specifier-seq abstract-declarator(opt)
20986 
20987    Returns the type indicated by the type-id.
20988 
20989    In addition to this, parse any queued up #pragma omp declare simd
20990    clauses, and #pragma acc routine clauses.
20991 
20992    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20993    function.  */
20994 
20995 static tree
cp_parser_late_return_type_opt(cp_parser * parser,cp_declarator * declarator,tree & requires_clause,cp_cv_quals quals)20996 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20997 				tree& requires_clause, cp_cv_quals quals)
20998 {
20999   cp_token *token;
21000   tree type = NULL_TREE;
21001   bool declare_simd_p = (parser->omp_declare_simd
21002 			 && declarator
21003 			 && declarator->kind == cdk_id);
21004 
21005   bool oacc_routine_p = (parser->oacc_routine
21006 			 && declarator
21007 			 && declarator->kind == cdk_id);
21008 
21009   /* Peek at the next token.  */
21010   token = cp_lexer_peek_token (parser->lexer);
21011   /* A late-specified return type is indicated by an initial '->'. */
21012   if (token->type != CPP_DEREF
21013       && token->keyword != RID_REQUIRES
21014       && !(token->type == CPP_NAME
21015 	   && token->u.value == ridpointers[RID_REQUIRES])
21016       && !(declare_simd_p || oacc_routine_p))
21017     return NULL_TREE;
21018 
21019   tree save_ccp = current_class_ptr;
21020   tree save_ccr = current_class_ref;
21021   if (quals >= 0)
21022     {
21023       /* DR 1207: 'this' is in scope in the trailing return type.  */
21024       inject_this_parameter (current_class_type, quals);
21025     }
21026 
21027   if (token->type == CPP_DEREF)
21028     {
21029       /* Consume the ->.  */
21030       cp_lexer_consume_token (parser->lexer);
21031 
21032       type = cp_parser_trailing_type_id (parser);
21033     }
21034 
21035   /* Function declarations may be followed by a trailing
21036      requires-clause.  */
21037   requires_clause = cp_parser_requires_clause_opt (parser);
21038 
21039   if (declare_simd_p)
21040     declarator->attributes
21041       = cp_parser_late_parsing_omp_declare_simd (parser,
21042 						 declarator->attributes);
21043   if (oacc_routine_p)
21044     declarator->attributes
21045       = cp_parser_late_parsing_oacc_routine (parser,
21046 					     declarator->attributes);
21047 
21048   if (quals >= 0)
21049     {
21050       current_class_ptr = save_ccp;
21051       current_class_ref = save_ccr;
21052     }
21053 
21054   return type;
21055 }
21056 
21057 /* Parse a declarator-id.
21058 
21059    declarator-id:
21060      id-expression
21061      :: [opt] nested-name-specifier [opt] type-name
21062 
21063    In the `id-expression' case, the value returned is as for
21064    cp_parser_id_expression if the id-expression was an unqualified-id.
21065    If the id-expression was a qualified-id, then a SCOPE_REF is
21066    returned.  The first operand is the scope (either a NAMESPACE_DECL
21067    or TREE_TYPE), but the second is still just a representation of an
21068    unqualified-id.  */
21069 
21070 static tree
cp_parser_declarator_id(cp_parser * parser,bool optional_p)21071 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21072 {
21073   tree id;
21074   /* The expression must be an id-expression.  Assume that qualified
21075      names are the names of types so that:
21076 
21077        template <class T>
21078        int S<T>::R::i = 3;
21079 
21080      will work; we must treat `S<T>::R' as the name of a type.
21081      Similarly, assume that qualified names are templates, where
21082      required, so that:
21083 
21084        template <class T>
21085        int S<T>::R<T>::i = 3;
21086 
21087      will work, too.  */
21088   id = cp_parser_id_expression (parser,
21089 				/*template_keyword_p=*/false,
21090 				/*check_dependency_p=*/false,
21091 				/*template_p=*/NULL,
21092 				/*declarator_p=*/true,
21093 				optional_p);
21094   if (id && BASELINK_P (id))
21095     id = BASELINK_FUNCTIONS (id);
21096   return id;
21097 }
21098 
21099 /* Parse a type-id.
21100 
21101    type-id:
21102      type-specifier-seq abstract-declarator [opt]
21103 
21104    Returns the TYPE specified.  */
21105 
21106 static tree
cp_parser_type_id_1(cp_parser * parser,bool is_template_arg,bool is_trailing_return)21107 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21108 		     bool is_trailing_return)
21109 {
21110   cp_decl_specifier_seq type_specifier_seq;
21111   cp_declarator *abstract_declarator;
21112 
21113   /* Parse the type-specifier-seq.  */
21114   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21115 				is_trailing_return,
21116 				&type_specifier_seq);
21117   if (is_template_arg && type_specifier_seq.type
21118       && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21119       && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21120     /* A bare template name as a template argument is a template template
21121        argument, not a placeholder, so fail parsing it as a type argument.  */
21122     {
21123       gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21124       cp_parser_simulate_error (parser);
21125       return error_mark_node;
21126     }
21127   if (type_specifier_seq.type == error_mark_node)
21128     return error_mark_node;
21129 
21130   /* There might or might not be an abstract declarator.  */
21131   cp_parser_parse_tentatively (parser);
21132   /* Look for the declarator.  */
21133   abstract_declarator
21134     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21135 			    /*parenthesized_p=*/NULL,
21136 			    /*member_p=*/false,
21137 			    /*friend_p=*/false);
21138   /* Check to see if there really was a declarator.  */
21139   if (!cp_parser_parse_definitely (parser))
21140     abstract_declarator = NULL;
21141 
21142   if (type_specifier_seq.type
21143       /* The concepts TS allows 'auto' as a type-id.  */
21144       && (!flag_concepts || parser->in_type_id_in_expr_p)
21145       /* None of the valid uses of 'auto' in C++14 involve the type-id
21146 	 nonterminal, but it is valid in a trailing-return-type.  */
21147       && !(cxx_dialect >= cxx14 && is_trailing_return))
21148     if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21149       {
21150 	/* A type-id with type 'auto' is only ok if the abstract declarator
21151 	   is a function declarator with a late-specified return type.
21152 
21153 	   A type-id with 'auto' is also valid in a trailing-return-type
21154 	   in a compound-requirement. */
21155 	if (abstract_declarator
21156 	    && abstract_declarator->kind == cdk_function
21157 	    && abstract_declarator->u.function.late_return_type)
21158 	  /* OK */;
21159 	else if (parser->in_result_type_constraint_p)
21160 	  /* OK */;
21161 	else
21162 	  {
21163 	    location_t loc = type_specifier_seq.locations[ds_type_spec];
21164 	    if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21165 	      {
21166 		error_at (loc, "missing template arguments after %qT",
21167 			  auto_node);
21168 		inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21169 			tmpl);
21170 	      }
21171 	    else
21172 	      error_at (loc, "invalid use of %qT", auto_node);
21173 	    return error_mark_node;
21174 	  }
21175       }
21176 
21177   return groktypename (&type_specifier_seq, abstract_declarator,
21178 		       is_template_arg);
21179 }
21180 
21181 static tree
cp_parser_type_id(cp_parser * parser)21182 cp_parser_type_id (cp_parser *parser)
21183 {
21184   return cp_parser_type_id_1 (parser, false, false);
21185 }
21186 
21187 static tree
cp_parser_template_type_arg(cp_parser * parser)21188 cp_parser_template_type_arg (cp_parser *parser)
21189 {
21190   tree r;
21191   const char *saved_message = parser->type_definition_forbidden_message;
21192   parser->type_definition_forbidden_message
21193     = G_("types may not be defined in template arguments");
21194   r = cp_parser_type_id_1 (parser, true, false);
21195   parser->type_definition_forbidden_message = saved_message;
21196   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21197     {
21198       error ("invalid use of %<auto%> in template argument");
21199       r = error_mark_node;
21200     }
21201   return r;
21202 }
21203 
21204 static tree
cp_parser_trailing_type_id(cp_parser * parser)21205 cp_parser_trailing_type_id (cp_parser *parser)
21206 {
21207   return cp_parser_type_id_1 (parser, false, true);
21208 }
21209 
21210 /* Parse a type-specifier-seq.
21211 
21212    type-specifier-seq:
21213      type-specifier type-specifier-seq [opt]
21214 
21215    GNU extension:
21216 
21217    type-specifier-seq:
21218      attributes type-specifier-seq [opt]
21219 
21220    If IS_DECLARATION is true, we are at the start of a "condition" or
21221    exception-declaration, so we might be followed by a declarator-id.
21222 
21223    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21224    i.e. we've just seen "->".
21225 
21226    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
21227 
21228 static void
cp_parser_type_specifier_seq(cp_parser * parser,bool is_declaration,bool is_trailing_return,cp_decl_specifier_seq * type_specifier_seq)21229 cp_parser_type_specifier_seq (cp_parser* parser,
21230 			      bool is_declaration,
21231 			      bool is_trailing_return,
21232 			      cp_decl_specifier_seq *type_specifier_seq)
21233 {
21234   bool seen_type_specifier = false;
21235   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21236   cp_token *start_token = NULL;
21237 
21238   /* Clear the TYPE_SPECIFIER_SEQ.  */
21239   clear_decl_specs (type_specifier_seq);
21240 
21241   /* In the context of a trailing return type, enum E { } is an
21242      elaborated-type-specifier followed by a function-body, not an
21243      enum-specifier.  */
21244   if (is_trailing_return)
21245     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21246 
21247   /* Parse the type-specifiers and attributes.  */
21248   while (true)
21249     {
21250       tree type_specifier;
21251       bool is_cv_qualifier;
21252 
21253       /* Check for attributes first.  */
21254       if (cp_next_tokens_can_be_attribute_p (parser))
21255 	{
21256 	  type_specifier_seq->attributes
21257 	    = attr_chainon (type_specifier_seq->attributes,
21258 			    cp_parser_attributes_opt (parser));
21259 	  continue;
21260 	}
21261 
21262       /* record the token of the beginning of the type specifier seq,
21263          for error reporting purposes*/
21264      if (!start_token)
21265        start_token = cp_lexer_peek_token (parser->lexer);
21266 
21267       /* Look for the type-specifier.  */
21268       type_specifier = cp_parser_type_specifier (parser,
21269 						 flags,
21270 						 type_specifier_seq,
21271 						 /*is_declaration=*/false,
21272 						 NULL,
21273 						 &is_cv_qualifier);
21274       if (!type_specifier)
21275 	{
21276 	  /* If the first type-specifier could not be found, this is not a
21277 	     type-specifier-seq at all.  */
21278 	  if (!seen_type_specifier)
21279 	    {
21280 	      /* Set in_declarator_p to avoid skipping to the semicolon.  */
21281 	      int in_decl = parser->in_declarator_p;
21282 	      parser->in_declarator_p = true;
21283 
21284 	      if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21285 		  || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21286 		cp_parser_error (parser, "expected type-specifier");
21287 
21288 	      parser->in_declarator_p = in_decl;
21289 
21290 	      type_specifier_seq->type = error_mark_node;
21291 	      return;
21292 	    }
21293 	  /* If subsequent type-specifiers could not be found, the
21294 	     type-specifier-seq is complete.  */
21295 	  break;
21296 	}
21297 
21298       seen_type_specifier = true;
21299       /* The standard says that a condition can be:
21300 
21301 	    type-specifier-seq declarator = assignment-expression
21302 
21303 	 However, given:
21304 
21305 	   struct S {};
21306 	   if (int S = ...)
21307 
21308 	 we should treat the "S" as a declarator, not as a
21309 	 type-specifier.  The standard doesn't say that explicitly for
21310 	 type-specifier-seq, but it does say that for
21311 	 decl-specifier-seq in an ordinary declaration.  Perhaps it
21312 	 would be clearer just to allow a decl-specifier-seq here, and
21313 	 then add a semantic restriction that if any decl-specifiers
21314 	 that are not type-specifiers appear, the program is invalid.  */
21315       if (is_declaration && !is_cv_qualifier)
21316 	flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21317     }
21318 }
21319 
21320 /* Return whether the function currently being declared has an associated
21321    template parameter list.  */
21322 
21323 static bool
function_being_declared_is_template_p(cp_parser * parser)21324 function_being_declared_is_template_p (cp_parser* parser)
21325 {
21326   if (!current_template_parms || processing_template_parmlist)
21327     return false;
21328 
21329   if (parser->implicit_template_scope)
21330     return true;
21331 
21332   if (at_class_scope_p ()
21333       && TYPE_BEING_DEFINED (current_class_type))
21334     return parser->num_template_parameter_lists != 0;
21335 
21336   return ((int) parser->num_template_parameter_lists > template_class_depth
21337 	  (current_class_type));
21338 }
21339 
21340 /* Parse a parameter-declaration-clause.
21341 
21342    parameter-declaration-clause:
21343      parameter-declaration-list [opt] ... [opt]
21344      parameter-declaration-list , ...
21345 
21346    Returns a representation for the parameter declarations.  A return
21347    value of NULL indicates a parameter-declaration-clause consisting
21348    only of an ellipsis.  */
21349 
21350 static tree
cp_parser_parameter_declaration_clause(cp_parser * parser)21351 cp_parser_parameter_declaration_clause (cp_parser* parser)
21352 {
21353   tree parameters;
21354   cp_token *token;
21355   bool ellipsis_p;
21356   bool is_error;
21357 
21358   temp_override<bool> cleanup
21359     (parser->auto_is_implicit_function_template_parm_p);
21360 
21361   if (!processing_specialization
21362       && !processing_template_parmlist
21363       && !processing_explicit_instantiation
21364       /* default_arg_ok_p tracks whether this is a parameter-clause for an
21365          actual function or a random abstract declarator.  */
21366       && parser->default_arg_ok_p)
21367     if (!current_function_decl
21368 	|| (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21369       parser->auto_is_implicit_function_template_parm_p = true;
21370 
21371   /* Peek at the next token.  */
21372   token = cp_lexer_peek_token (parser->lexer);
21373   /* Check for trivial parameter-declaration-clauses.  */
21374   if (token->type == CPP_ELLIPSIS)
21375     {
21376       /* Consume the `...' token.  */
21377       cp_lexer_consume_token (parser->lexer);
21378       return NULL_TREE;
21379     }
21380   else if (token->type == CPP_CLOSE_PAREN)
21381     /* There are no parameters.  */
21382     {
21383 #ifndef NO_IMPLICIT_EXTERN_C
21384       if (in_system_header_at (input_location)
21385 	  && current_class_type == NULL
21386 	  && current_lang_name == lang_name_c)
21387 	return NULL_TREE;
21388       else
21389 #endif
21390 	return void_list_node;
21391     }
21392   /* Check for `(void)', too, which is a special case.  */
21393   else if (token->keyword == RID_VOID
21394 	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21395 	       == CPP_CLOSE_PAREN))
21396     {
21397       /* Consume the `void' token.  */
21398       cp_lexer_consume_token (parser->lexer);
21399       /* There are no parameters.  */
21400       return void_list_node;
21401     }
21402 
21403   /* Parse the parameter-declaration-list.  */
21404   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21405   /* If a parse error occurred while parsing the
21406      parameter-declaration-list, then the entire
21407      parameter-declaration-clause is erroneous.  */
21408   if (is_error)
21409     return NULL;
21410 
21411   /* Peek at the next token.  */
21412   token = cp_lexer_peek_token (parser->lexer);
21413   /* If it's a `,', the clause should terminate with an ellipsis.  */
21414   if (token->type == CPP_COMMA)
21415     {
21416       /* Consume the `,'.  */
21417       cp_lexer_consume_token (parser->lexer);
21418       /* Expect an ellipsis.  */
21419       ellipsis_p
21420 	= (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21421     }
21422   /* It might also be `...' if the optional trailing `,' was
21423      omitted.  */
21424   else if (token->type == CPP_ELLIPSIS)
21425     {
21426       /* Consume the `...' token.  */
21427       cp_lexer_consume_token (parser->lexer);
21428       /* And remember that we saw it.  */
21429       ellipsis_p = true;
21430     }
21431   else
21432     ellipsis_p = false;
21433 
21434   /* Finish the parameter list.  */
21435   if (!ellipsis_p)
21436     parameters = chainon (parameters, void_list_node);
21437 
21438   return parameters;
21439 }
21440 
21441 /* Parse a parameter-declaration-list.
21442 
21443    parameter-declaration-list:
21444      parameter-declaration
21445      parameter-declaration-list , parameter-declaration
21446 
21447    Returns a representation of the parameter-declaration-list, as for
21448    cp_parser_parameter_declaration_clause.  However, the
21449    `void_list_node' is never appended to the list.  Upon return,
21450    *IS_ERROR will be true iff an error occurred.  */
21451 
21452 static tree
cp_parser_parameter_declaration_list(cp_parser * parser,bool * is_error)21453 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21454 {
21455   tree parameters = NULL_TREE;
21456   tree *tail = &parameters;
21457   bool saved_in_unbraced_linkage_specification_p;
21458   int index = 0;
21459 
21460   /* Assume all will go well.  */
21461   *is_error = false;
21462   /* The special considerations that apply to a function within an
21463      unbraced linkage specifications do not apply to the parameters
21464      to the function.  */
21465   saved_in_unbraced_linkage_specification_p
21466     = parser->in_unbraced_linkage_specification_p;
21467   parser->in_unbraced_linkage_specification_p = false;
21468 
21469   /* Look for more parameters.  */
21470   while (true)
21471     {
21472       cp_parameter_declarator *parameter;
21473       tree decl = error_mark_node;
21474       bool parenthesized_p = false;
21475 
21476       /* Parse the parameter.  */
21477       parameter
21478 	= cp_parser_parameter_declaration (parser,
21479 					   /*template_parm_p=*/false,
21480 					   &parenthesized_p);
21481 
21482       /* We don't know yet if the enclosing context is deprecated, so wait
21483 	 and warn in grokparms if appropriate.  */
21484       deprecated_state = DEPRECATED_SUPPRESS;
21485 
21486       if (parameter)
21487 	{
21488 	  decl = grokdeclarator (parameter->declarator,
21489 				 &parameter->decl_specifiers,
21490 				 PARM,
21491 				 parameter->default_argument != NULL_TREE,
21492 				 &parameter->decl_specifiers.attributes);
21493 	  if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21494 	    DECL_SOURCE_LOCATION (decl) = parameter->loc;
21495 	}
21496 
21497       deprecated_state = DEPRECATED_NORMAL;
21498 
21499       /* If a parse error occurred parsing the parameter declaration,
21500 	 then the entire parameter-declaration-list is erroneous.  */
21501       if (decl == error_mark_node)
21502 	{
21503 	  *is_error = true;
21504 	  parameters = error_mark_node;
21505 	  break;
21506 	}
21507 
21508       if (parameter->decl_specifiers.attributes)
21509 	cplus_decl_attributes (&decl,
21510 			       parameter->decl_specifiers.attributes,
21511 			       0);
21512       if (DECL_NAME (decl))
21513 	decl = pushdecl (decl);
21514 
21515       if (decl != error_mark_node)
21516 	{
21517 	  retrofit_lang_decl (decl);
21518 	  DECL_PARM_INDEX (decl) = ++index;
21519 	  DECL_PARM_LEVEL (decl) = function_parm_depth ();
21520 	}
21521 
21522       /* Add the new parameter to the list.  */
21523       *tail = build_tree_list (parameter->default_argument, decl);
21524       tail = &TREE_CHAIN (*tail);
21525 
21526       /* Peek at the next token.  */
21527       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21528 	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21529 	  /* These are for Objective-C++ */
21530 	  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21531 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21532 	/* The parameter-declaration-list is complete.  */
21533 	break;
21534       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21535 	{
21536 	  cp_token *token;
21537 
21538 	  /* Peek at the next token.  */
21539 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
21540 	  /* If it's an ellipsis, then the list is complete.  */
21541 	  if (token->type == CPP_ELLIPSIS)
21542 	    break;
21543 	  /* Otherwise, there must be more parameters.  Consume the
21544 	     `,'.  */
21545 	  cp_lexer_consume_token (parser->lexer);
21546 	  /* When parsing something like:
21547 
21548 		int i(float f, double d)
21549 
21550 	     we can tell after seeing the declaration for "f" that we
21551 	     are not looking at an initialization of a variable "i",
21552 	     but rather at the declaration of a function "i".
21553 
21554 	     Due to the fact that the parsing of template arguments
21555 	     (as specified to a template-id) requires backtracking we
21556 	     cannot use this technique when inside a template argument
21557 	     list.  */
21558 	  if (!parser->in_template_argument_list_p
21559 	      && !parser->in_type_id_in_expr_p
21560 	      && cp_parser_uncommitted_to_tentative_parse_p (parser)
21561 	      /* However, a parameter-declaration of the form
21562 		 "float(f)" (which is a valid declaration of a
21563 		 parameter "f") can also be interpreted as an
21564 		 expression (the conversion of "f" to "float").  */
21565 	      && !parenthesized_p)
21566 	    cp_parser_commit_to_tentative_parse (parser);
21567 	}
21568       else
21569 	{
21570 	  cp_parser_error (parser, "expected %<,%> or %<...%>");
21571 	  if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21572 	    cp_parser_skip_to_closing_parenthesis (parser,
21573 						   /*recovering=*/true,
21574 						   /*or_comma=*/false,
21575 						   /*consume_paren=*/false);
21576 	  break;
21577 	}
21578     }
21579 
21580   parser->in_unbraced_linkage_specification_p
21581     = saved_in_unbraced_linkage_specification_p;
21582 
21583   /* Reset implicit_template_scope if we are about to leave the function
21584      parameter list that introduced it.  Note that for out-of-line member
21585      definitions, there will be one or more class scopes before we get to
21586      the template parameter scope.  */
21587 
21588   if (cp_binding_level *its = parser->implicit_template_scope)
21589     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21590       {
21591 	while (maybe_its->kind == sk_class)
21592 	  maybe_its = maybe_its->level_chain;
21593 	if (maybe_its == its)
21594 	  {
21595 	    parser->implicit_template_parms = 0;
21596 	    parser->implicit_template_scope = 0;
21597 	  }
21598       }
21599 
21600   return parameters;
21601 }
21602 
21603 /* Parse a parameter declaration.
21604 
21605    parameter-declaration:
21606      decl-specifier-seq ... [opt] declarator
21607      decl-specifier-seq declarator = assignment-expression
21608      decl-specifier-seq ... [opt] abstract-declarator [opt]
21609      decl-specifier-seq abstract-declarator [opt] = assignment-expression
21610 
21611    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21612    declares a template parameter.  (In that case, a non-nested `>'
21613    token encountered during the parsing of the assignment-expression
21614    is not interpreted as a greater-than operator.)
21615 
21616    Returns a representation of the parameter, or NULL if an error
21617    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21618    true iff the declarator is of the form "(p)".  */
21619 
21620 static cp_parameter_declarator *
cp_parser_parameter_declaration(cp_parser * parser,bool template_parm_p,bool * parenthesized_p)21621 cp_parser_parameter_declaration (cp_parser *parser,
21622 				 bool template_parm_p,
21623 				 bool *parenthesized_p)
21624 {
21625   int declares_class_or_enum;
21626   cp_decl_specifier_seq decl_specifiers;
21627   cp_declarator *declarator;
21628   tree default_argument;
21629   cp_token *token = NULL, *declarator_token_start = NULL;
21630   const char *saved_message;
21631   bool template_parameter_pack_p = false;
21632 
21633   /* In a template parameter, `>' is not an operator.
21634 
21635      [temp.param]
21636 
21637      When parsing a default template-argument for a non-type
21638      template-parameter, the first non-nested `>' is taken as the end
21639      of the template parameter-list rather than a greater-than
21640      operator.  */
21641 
21642   /* Type definitions may not appear in parameter types.  */
21643   saved_message = parser->type_definition_forbidden_message;
21644   parser->type_definition_forbidden_message
21645     = G_("types may not be defined in parameter types");
21646 
21647   int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21648 			   TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21649 					    (current_template_parms)) : 0);
21650 
21651   /* Parse the declaration-specifiers.  */
21652   cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21653   cp_parser_decl_specifier_seq (parser,
21654 				CP_PARSER_FLAGS_NONE,
21655 				&decl_specifiers,
21656 				&declares_class_or_enum);
21657 
21658   /* Complain about missing 'typename' or other invalid type names.  */
21659   if (!decl_specifiers.any_type_specifiers_p
21660       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21661     decl_specifiers.type = error_mark_node;
21662 
21663   /* If an error occurred, there's no reason to attempt to parse the
21664      rest of the declaration.  */
21665   if (cp_parser_error_occurred (parser))
21666     {
21667       parser->type_definition_forbidden_message = saved_message;
21668       return NULL;
21669     }
21670 
21671   /* Peek at the next token.  */
21672   token = cp_lexer_peek_token (parser->lexer);
21673 
21674   /* If the next token is a `)', `,', `=', `>', or `...', then there
21675      is no declarator. However, when variadic templates are enabled,
21676      there may be a declarator following `...'.  */
21677   if (token->type == CPP_CLOSE_PAREN
21678       || token->type == CPP_COMMA
21679       || token->type == CPP_EQ
21680       || token->type == CPP_GREATER)
21681     {
21682       declarator = NULL;
21683       if (parenthesized_p)
21684 	*parenthesized_p = false;
21685     }
21686   /* Otherwise, there should be a declarator.  */
21687   else
21688     {
21689       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21690       parser->default_arg_ok_p = false;
21691 
21692       /* After seeing a decl-specifier-seq, if the next token is not a
21693 	 "(", there is no possibility that the code is a valid
21694 	 expression.  Therefore, if parsing tentatively, we commit at
21695 	 this point.  */
21696       if (!parser->in_template_argument_list_p
21697 	  /* In an expression context, having seen:
21698 
21699 	       (int((char ...
21700 
21701 	     we cannot be sure whether we are looking at a
21702 	     function-type (taking a "char" as a parameter) or a cast
21703 	     of some object of type "char" to "int".  */
21704 	  && !parser->in_type_id_in_expr_p
21705 	  && cp_parser_uncommitted_to_tentative_parse_p (parser)
21706 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21707 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21708 	cp_parser_commit_to_tentative_parse (parser);
21709       /* Parse the declarator.  */
21710       declarator_token_start = token;
21711       declarator = cp_parser_declarator (parser,
21712 					 CP_PARSER_DECLARATOR_EITHER,
21713 					 /*ctor_dtor_or_conv_p=*/NULL,
21714 					 parenthesized_p,
21715 					 /*member_p=*/false,
21716 					 /*friend_p=*/false);
21717       parser->default_arg_ok_p = saved_default_arg_ok_p;
21718       /* After the declarator, allow more attributes.  */
21719       decl_specifiers.attributes
21720 	= attr_chainon (decl_specifiers.attributes,
21721 			cp_parser_attributes_opt (parser));
21722 
21723       /* If the declarator is a template parameter pack, remember that and
21724 	 clear the flag in the declarator itself so we don't get errors
21725 	 from grokdeclarator.  */
21726       if (template_parm_p && declarator && declarator->parameter_pack_p)
21727 	{
21728 	  declarator->parameter_pack_p = false;
21729 	  template_parameter_pack_p = true;
21730 	}
21731     }
21732 
21733   /* If the next token is an ellipsis, and we have not seen a declarator
21734      name, and if either the type of the declarator contains parameter
21735      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21736      for, eg, abbreviated integral type names), then we actually have a
21737      parameter pack expansion expression. Otherwise, leave the ellipsis
21738      for a C-style variadic function. */
21739   token = cp_lexer_peek_token (parser->lexer);
21740 
21741   /* If a function parameter pack was specified and an implicit template
21742      parameter was introduced during cp_parser_parameter_declaration,
21743      change any implicit parameters introduced into packs.  */
21744   if (parser->implicit_template_parms
21745       && ((token->type == CPP_ELLIPSIS
21746 	   && declarator_can_be_parameter_pack (declarator))
21747 	  || (declarator && declarator->parameter_pack_p)))
21748     {
21749       int latest_template_parm_idx = TREE_VEC_LENGTH
21750 	(INNERMOST_TEMPLATE_PARMS (current_template_parms));
21751 
21752       if (latest_template_parm_idx != template_parm_idx)
21753 	decl_specifiers.type = convert_generic_types_to_packs
21754 	  (decl_specifiers.type,
21755 	   template_parm_idx, latest_template_parm_idx);
21756     }
21757 
21758   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21759     {
21760       tree type = decl_specifiers.type;
21761 
21762       if (type && DECL_P (type))
21763         type = TREE_TYPE (type);
21764 
21765       if (((type
21766 	    && TREE_CODE (type) != TYPE_PACK_EXPANSION
21767 	    && (template_parm_p || uses_parameter_packs (type)))
21768 	   || (!type && template_parm_p))
21769 	  && declarator_can_be_parameter_pack (declarator))
21770 	{
21771 	  /* Consume the `...'. */
21772 	  cp_lexer_consume_token (parser->lexer);
21773 	  maybe_warn_variadic_templates ();
21774 
21775 	  /* Build a pack expansion type */
21776 	  if (template_parm_p)
21777 	    template_parameter_pack_p = true;
21778 	  else if (declarator)
21779 	    declarator->parameter_pack_p = true;
21780 	  else
21781 	    decl_specifiers.type = make_pack_expansion (type);
21782 	}
21783     }
21784 
21785   /* The restriction on defining new types applies only to the type
21786      of the parameter, not to the default argument.  */
21787   parser->type_definition_forbidden_message = saved_message;
21788 
21789   /* If the next token is `=', then process a default argument.  */
21790   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21791     {
21792       tree type = decl_specifiers.type;
21793       token = cp_lexer_peek_token (parser->lexer);
21794       /* If we are defining a class, then the tokens that make up the
21795 	 default argument must be saved and processed later.  */
21796       if (!template_parm_p && at_class_scope_p ()
21797 	  && TYPE_BEING_DEFINED (current_class_type)
21798 	  && !LAMBDA_TYPE_P (current_class_type))
21799 	default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21800 
21801       // A constrained-type-specifier may declare a type template-parameter.
21802       else if (declares_constrained_type_template_parameter (type))
21803         default_argument
21804           = cp_parser_default_type_template_argument (parser);
21805 
21806       // A constrained-type-specifier may declare a template-template-parameter.
21807       else if (declares_constrained_template_template_parameter (type))
21808         default_argument
21809           = cp_parser_default_template_template_argument (parser);
21810 
21811       /* Outside of a class definition, we can just parse the
21812 	 assignment-expression.  */
21813       else
21814 	default_argument
21815 	  = cp_parser_default_argument (parser, template_parm_p);
21816 
21817       if (!parser->default_arg_ok_p)
21818 	{
21819 	  permerror (token->location,
21820 		     "default arguments are only "
21821 		     "permitted for function parameters");
21822 	}
21823       else if ((declarator && declarator->parameter_pack_p)
21824 	       || template_parameter_pack_p
21825 	       || (decl_specifiers.type
21826 		   && PACK_EXPANSION_P (decl_specifiers.type)))
21827 	{
21828 	  /* Find the name of the parameter pack.  */
21829 	  cp_declarator *id_declarator = declarator;
21830 	  while (id_declarator && id_declarator->kind != cdk_id)
21831 	    id_declarator = id_declarator->declarator;
21832 
21833 	  if (id_declarator && id_declarator->kind == cdk_id)
21834 	    error_at (declarator_token_start->location,
21835 		      template_parm_p
21836 		      ? G_("template parameter pack %qD "
21837 			   "cannot have a default argument")
21838 		      : G_("parameter pack %qD cannot have "
21839 			   "a default argument"),
21840 		      id_declarator->u.id.unqualified_name);
21841 	  else
21842 	    error_at (declarator_token_start->location,
21843 		      template_parm_p
21844 		      ? G_("template parameter pack cannot have "
21845 			   "a default argument")
21846 		      : G_("parameter pack cannot have a "
21847 			   "default argument"));
21848 
21849 	  default_argument = NULL_TREE;
21850 	}
21851     }
21852   else
21853     default_argument = NULL_TREE;
21854 
21855   /* Generate a location for the parameter, ranging from the start of the
21856      initial token to the end of the final token (using input_location for
21857      the latter, set up by cp_lexer_set_source_position_from_token when
21858      consuming tokens).
21859 
21860      If we have a identifier, then use it for the caret location, e.g.
21861 
21862        extern int callee (int one, int (*two)(int, int), float three);
21863                                    ~~~~~~^~~~~~~~~~~~~~
21864 
21865      otherwise, reuse the start location for the caret location e.g.:
21866 
21867        extern int callee (int one, int (*)(int, int), float three);
21868                                    ^~~~~~~~~~~~~~~~~
21869 
21870   */
21871   location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21872 			  ? declarator->id_loc
21873 			  : decl_spec_token_start->location);
21874   location_t param_loc = make_location (caret_loc,
21875 					decl_spec_token_start->location,
21876 					input_location);
21877 
21878   return make_parameter_declarator (&decl_specifiers,
21879 				    declarator,
21880 				    default_argument,
21881 				    param_loc,
21882 				    template_parameter_pack_p);
21883 }
21884 
21885 /* Parse a default argument and return it.
21886 
21887    TEMPLATE_PARM_P is true if this is a default argument for a
21888    non-type template parameter.  */
21889 static tree
cp_parser_default_argument(cp_parser * parser,bool template_parm_p)21890 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21891 {
21892   tree default_argument = NULL_TREE;
21893   bool saved_greater_than_is_operator_p;
21894   bool saved_local_variables_forbidden_p;
21895   bool non_constant_p, is_direct_init;
21896 
21897   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21898      set correctly.  */
21899   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21900   parser->greater_than_is_operator_p = !template_parm_p;
21901   /* Local variable names (and the `this' keyword) may not
21902      appear in a default argument.  */
21903   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21904   parser->local_variables_forbidden_p = true;
21905   /* Parse the assignment-expression.  */
21906   if (template_parm_p)
21907     push_deferring_access_checks (dk_no_deferred);
21908   tree saved_class_ptr = NULL_TREE;
21909   tree saved_class_ref = NULL_TREE;
21910   /* The "this" pointer is not valid in a default argument.  */
21911   if (cfun)
21912     {
21913       saved_class_ptr = current_class_ptr;
21914       cp_function_chain->x_current_class_ptr = NULL_TREE;
21915       saved_class_ref = current_class_ref;
21916       cp_function_chain->x_current_class_ref = NULL_TREE;
21917     }
21918   default_argument
21919     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21920   /* Restore the "this" pointer.  */
21921   if (cfun)
21922     {
21923       cp_function_chain->x_current_class_ptr = saved_class_ptr;
21924       cp_function_chain->x_current_class_ref = saved_class_ref;
21925     }
21926   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21927     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21928   if (template_parm_p)
21929     pop_deferring_access_checks ();
21930   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21931   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21932 
21933   return default_argument;
21934 }
21935 
21936 /* Parse a function-body.
21937 
21938    function-body:
21939      compound_statement  */
21940 
21941 static void
cp_parser_function_body(cp_parser * parser,bool in_function_try_block)21942 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21943 {
21944   cp_parser_compound_statement (parser, NULL, (in_function_try_block
21945 					       ? BCS_TRY_BLOCK : BCS_NORMAL),
21946 				true);
21947 }
21948 
21949 /* Parse a ctor-initializer-opt followed by a function-body.  Return
21950    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
21951    is true we are parsing a function-try-block.  */
21952 
21953 static void
cp_parser_ctor_initializer_opt_and_function_body(cp_parser * parser,bool in_function_try_block)21954 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21955 						  bool in_function_try_block)
21956 {
21957   tree body, list;
21958   const bool check_body_p =
21959      DECL_CONSTRUCTOR_P (current_function_decl)
21960      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21961   tree last = NULL;
21962 
21963   /* Begin the function body.  */
21964   body = begin_function_body ();
21965   /* Parse the optional ctor-initializer.  */
21966   cp_parser_ctor_initializer_opt (parser);
21967 
21968   /* If we're parsing a constexpr constructor definition, we need
21969      to check that the constructor body is indeed empty.  However,
21970      before we get to cp_parser_function_body lot of junk has been
21971      generated, so we can't just check that we have an empty block.
21972      Rather we take a snapshot of the outermost block, and check whether
21973      cp_parser_function_body changed its state.  */
21974   if (check_body_p)
21975     {
21976       list = cur_stmt_list;
21977       if (STATEMENT_LIST_TAIL (list))
21978 	last = STATEMENT_LIST_TAIL (list)->stmt;
21979     }
21980   /* Parse the function-body.  */
21981   cp_parser_function_body (parser, in_function_try_block);
21982   if (check_body_p)
21983     check_constexpr_ctor_body (last, list, /*complain=*/true);
21984   /* Finish the function body.  */
21985   finish_function_body (body);
21986 }
21987 
21988 /* Parse an initializer.
21989 
21990    initializer:
21991      = initializer-clause
21992      ( expression-list )
21993 
21994    Returns an expression representing the initializer.  If no
21995    initializer is present, NULL_TREE is returned.
21996 
21997    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21998    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
21999    set to TRUE if there is no initializer present.  If there is an
22000    initializer, and it is not a constant-expression, *NON_CONSTANT_P
22001    is set to true; otherwise it is set to false.  */
22002 
22003 static tree
cp_parser_initializer(cp_parser * parser,bool * is_direct_init,bool * non_constant_p,bool subexpression_p)22004 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22005 		       bool* non_constant_p, bool subexpression_p)
22006 {
22007   cp_token *token;
22008   tree init;
22009 
22010   /* Peek at the next token.  */
22011   token = cp_lexer_peek_token (parser->lexer);
22012 
22013   /* Let our caller know whether or not this initializer was
22014      parenthesized.  */
22015   *is_direct_init = (token->type != CPP_EQ);
22016   /* Assume that the initializer is constant.  */
22017   *non_constant_p = false;
22018 
22019   if (token->type == CPP_EQ)
22020     {
22021       /* Consume the `='.  */
22022       cp_lexer_consume_token (parser->lexer);
22023       /* Parse the initializer-clause.  */
22024       init = cp_parser_initializer_clause (parser, non_constant_p);
22025     }
22026   else if (token->type == CPP_OPEN_PAREN)
22027     {
22028       vec<tree, va_gc> *vec;
22029       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22030 						     /*cast_p=*/false,
22031 						     /*allow_expansion_p=*/true,
22032 						     non_constant_p);
22033       if (vec == NULL)
22034 	return error_mark_node;
22035       init = build_tree_list_vec (vec);
22036       release_tree_vector (vec);
22037     }
22038   else if (token->type == CPP_OPEN_BRACE)
22039     {
22040       cp_lexer_set_source_position (parser->lexer);
22041       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22042       init = cp_parser_braced_list (parser, non_constant_p);
22043       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22044     }
22045   else
22046     {
22047       /* Anything else is an error.  */
22048       cp_parser_error (parser, "expected initializer");
22049       init = error_mark_node;
22050     }
22051 
22052   if (!subexpression_p && check_for_bare_parameter_packs (init))
22053     init = error_mark_node;
22054 
22055   return init;
22056 }
22057 
22058 /* Parse an initializer-clause.
22059 
22060    initializer-clause:
22061      assignment-expression
22062      braced-init-list
22063 
22064    Returns an expression representing the initializer.
22065 
22066    If the `assignment-expression' production is used the value
22067    returned is simply a representation for the expression.
22068 
22069    Otherwise, calls cp_parser_braced_list.  */
22070 
22071 static cp_expr
cp_parser_initializer_clause(cp_parser * parser,bool * non_constant_p)22072 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22073 {
22074   cp_expr initializer;
22075 
22076   /* Assume the expression is constant.  */
22077   *non_constant_p = false;
22078 
22079   /* If it is not a `{', then we are looking at an
22080      assignment-expression.  */
22081   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22082     {
22083       initializer
22084 	= cp_parser_constant_expression (parser,
22085 					/*allow_non_constant_p=*/true,
22086 					non_constant_p);
22087     }
22088   else
22089     initializer = cp_parser_braced_list (parser, non_constant_p);
22090 
22091   return initializer;
22092 }
22093 
22094 /* Parse a brace-enclosed initializer list.
22095 
22096    braced-init-list:
22097      { initializer-list , [opt] }
22098      { designated-initializer-list , [opt] }
22099      { }
22100 
22101    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
22102    the elements of the initializer-list (or NULL, if the last
22103    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
22104    NULL_TREE.  There is no way to detect whether or not the optional
22105    trailing `,' was provided.  NON_CONSTANT_P is as for
22106    cp_parser_initializer.  */
22107 
22108 static cp_expr
cp_parser_braced_list(cp_parser * parser,bool * non_constant_p)22109 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22110 {
22111   tree initializer;
22112   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22113 
22114   /* Consume the `{' token.  */
22115   matching_braces braces;
22116   braces.require_open (parser);
22117   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
22118   initializer = make_node (CONSTRUCTOR);
22119   /* If it's not a `}', then there is a non-trivial initializer.  */
22120   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22121     {
22122       /* Parse the initializer list.  */
22123       CONSTRUCTOR_ELTS (initializer)
22124 	= cp_parser_initializer_list (parser, non_constant_p);
22125       /* A trailing `,' token is allowed.  */
22126       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22127 	cp_lexer_consume_token (parser->lexer);
22128     }
22129   else
22130     *non_constant_p = false;
22131   /* Now, there should be a trailing `}'.  */
22132   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22133   braces.require_close (parser);
22134   TREE_TYPE (initializer) = init_list_type_node;
22135 
22136   cp_expr result (initializer);
22137   /* Build a location of the form:
22138        { ... }
22139        ^~~~~~~
22140      with caret==start at the open brace, finish at the close brace.  */
22141   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22142   result.set_location (combined_loc);
22143   return result;
22144 }
22145 
22146 /* Consume tokens up to, and including, the next non-nested closing `]'.
22147    Returns true iff we found a closing `]'.  */
22148 
22149 static bool
cp_parser_skip_to_closing_square_bracket(cp_parser * parser)22150 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22151 {
22152   unsigned square_depth = 0;
22153 
22154   while (true)
22155     {
22156       cp_token * token = cp_lexer_peek_token (parser->lexer);
22157 
22158       switch (token->type)
22159 	{
22160 	case CPP_EOF:
22161 	case CPP_PRAGMA_EOL:
22162 	  /* If we've run out of tokens, then there is no closing `]'.  */
22163 	  return false;
22164 
22165         case CPP_OPEN_SQUARE:
22166           ++square_depth;
22167           break;
22168 
22169         case CPP_CLOSE_SQUARE:
22170 	  if (!square_depth--)
22171 	    {
22172 	      cp_lexer_consume_token (parser->lexer);
22173 	      return true;
22174 	    }
22175 	  break;
22176 
22177 	default:
22178 	  break;
22179 	}
22180 
22181       /* Consume the token.  */
22182       cp_lexer_consume_token (parser->lexer);
22183     }
22184 }
22185 
22186 /* Return true if we are looking at an array-designator, false otherwise.  */
22187 
22188 static bool
cp_parser_array_designator_p(cp_parser * parser)22189 cp_parser_array_designator_p (cp_parser *parser)
22190 {
22191   /* Consume the `['.  */
22192   cp_lexer_consume_token (parser->lexer);
22193 
22194   cp_lexer_save_tokens (parser->lexer);
22195 
22196   /* Skip tokens until the next token is a closing square bracket.
22197      If we find the closing `]', and the next token is a `=', then
22198      we are looking at an array designator.  */
22199   bool array_designator_p
22200     = (cp_parser_skip_to_closing_square_bracket (parser)
22201        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22202 
22203   /* Roll back the tokens we skipped.  */
22204   cp_lexer_rollback_tokens (parser->lexer);
22205 
22206   return array_designator_p;
22207 }
22208 
22209 /* Parse an initializer-list.
22210 
22211    initializer-list:
22212      initializer-clause ... [opt]
22213      initializer-list , initializer-clause ... [opt]
22214 
22215    C++2A Extension:
22216 
22217    designated-initializer-list:
22218      designated-initializer-clause
22219      designated-initializer-list , designated-initializer-clause
22220 
22221    designated-initializer-clause:
22222      designator brace-or-equal-initializer
22223 
22224    designator:
22225      . identifier
22226 
22227    GNU Extension:
22228 
22229    initializer-list:
22230      designation initializer-clause ...[opt]
22231      initializer-list , designation initializer-clause ...[opt]
22232 
22233    designation:
22234      . identifier =
22235      identifier :
22236      [ constant-expression ] =
22237 
22238    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
22239    for the initializer.  If the INDEX of the elt is non-NULL, it is the
22240    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
22241    as for cp_parser_initializer.  */
22242 
22243 static vec<constructor_elt, va_gc> *
cp_parser_initializer_list(cp_parser * parser,bool * non_constant_p)22244 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22245 {
22246   vec<constructor_elt, va_gc> *v = NULL;
22247   bool first_p = true;
22248   tree first_designator = NULL_TREE;
22249 
22250   /* Assume all of the expressions are constant.  */
22251   *non_constant_p = false;
22252 
22253   /* Parse the rest of the list.  */
22254   while (true)
22255     {
22256       cp_token *token;
22257       tree designator;
22258       tree initializer;
22259       bool clause_non_constant_p;
22260       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22261 
22262       /* Handle the C++2A syntax, '. id ='.  */
22263       if ((cxx_dialect >= cxx2a
22264 	   || cp_parser_allow_gnu_extensions_p (parser))
22265 	  && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22266 	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22267 	  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22268 	      || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22269 		  == CPP_OPEN_BRACE)))
22270 	{
22271 	  if (cxx_dialect < cxx2a)
22272 	    pedwarn (loc, OPT_Wpedantic,
22273 		     "C++ designated initializers only available with "
22274 		     "-std=c++2a or -std=gnu++2a");
22275 	  /* Consume the `.'.  */
22276 	  cp_lexer_consume_token (parser->lexer);
22277 	  /* Consume the identifier.  */
22278 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
22279 	  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22280 	    /* Consume the `='.  */
22281 	    cp_lexer_consume_token (parser->lexer);
22282 	}
22283       /* Also, if the next token is an identifier and the following one is a
22284 	 colon, we are looking at the GNU designated-initializer
22285 	 syntax.  */
22286       else if (cp_parser_allow_gnu_extensions_p (parser)
22287 	       && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22288 	       && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22289 		   == CPP_COLON))
22290 	{
22291 	  /* Warn the user that they are using an extension.  */
22292 	  pedwarn (loc, OPT_Wpedantic,
22293 		   "ISO C++ does not allow GNU designated initializers");
22294 	  /* Consume the identifier.  */
22295 	  designator = cp_lexer_consume_token (parser->lexer)->u.value;
22296 	  /* Consume the `:'.  */
22297 	  cp_lexer_consume_token (parser->lexer);
22298 	}
22299       /* Also handle C99 array designators, '[ const ] ='.  */
22300       else if (cp_parser_allow_gnu_extensions_p (parser)
22301 	       && !c_dialect_objc ()
22302 	       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22303 	{
22304 	  /* In C++11, [ could start a lambda-introducer.  */
22305 	  bool non_const = false;
22306 
22307 	  cp_parser_parse_tentatively (parser);
22308 
22309 	  if (!cp_parser_array_designator_p (parser))
22310 	    {
22311 	      cp_parser_simulate_error (parser);
22312 	      designator = NULL_TREE;
22313 	    }
22314 	  else
22315 	    {
22316 	      designator = cp_parser_constant_expression (parser, true,
22317 							  &non_const);
22318 	      cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22319 	      cp_parser_require (parser, CPP_EQ, RT_EQ);
22320 	    }
22321 
22322 	  if (!cp_parser_parse_definitely (parser))
22323 	    designator = NULL_TREE;
22324 	  else if (non_const
22325 		   && (!require_potential_rvalue_constant_expression
22326 		       (designator)))
22327 	    designator = NULL_TREE;
22328 	  if (designator)
22329 	    /* Warn the user that they are using an extension.  */
22330 	    pedwarn (loc, OPT_Wpedantic,
22331 		     "ISO C++ does not allow C99 designated initializers");
22332 	}
22333       else
22334 	designator = NULL_TREE;
22335 
22336       if (first_p)
22337 	{
22338 	  first_designator = designator;
22339 	  first_p = false;
22340 	}
22341       else if (cxx_dialect >= cxx2a
22342 	       && first_designator != error_mark_node
22343 	       && (!first_designator != !designator))
22344 	{
22345 	  error_at (loc, "either all initializer clauses should be designated "
22346 			 "or none of them should be");
22347 	  first_designator = error_mark_node;
22348 	}
22349       else if (cxx_dialect < cxx2a && !first_designator)
22350 	first_designator = designator;
22351 
22352       /* Parse the initializer.  */
22353       initializer = cp_parser_initializer_clause (parser,
22354 						  &clause_non_constant_p);
22355       /* If any clause is non-constant, so is the entire initializer.  */
22356       if (clause_non_constant_p)
22357 	*non_constant_p = true;
22358 
22359       /* If we have an ellipsis, this is an initializer pack
22360 	 expansion.  */
22361       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22362         {
22363 	  location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22364 
22365           /* Consume the `...'.  */
22366           cp_lexer_consume_token (parser->lexer);
22367 
22368 	  if (designator && cxx_dialect >= cxx2a)
22369 	    error_at (loc,
22370 		      "%<...%> not allowed in designated initializer list");
22371 
22372 	  /* Turn the initializer into an initializer expansion.  */
22373 	  initializer = make_pack_expansion (initializer);
22374         }
22375 
22376       /* Add it to the vector.  */
22377       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22378 
22379       /* If the next token is not a comma, we have reached the end of
22380 	 the list.  */
22381       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22382 	break;
22383 
22384       /* Peek at the next token.  */
22385       token = cp_lexer_peek_nth_token (parser->lexer, 2);
22386       /* If the next token is a `}', then we're still done.  An
22387 	 initializer-clause can have a trailing `,' after the
22388 	 initializer-list and before the closing `}'.  */
22389       if (token->type == CPP_CLOSE_BRACE)
22390 	break;
22391 
22392       /* Consume the `,' token.  */
22393       cp_lexer_consume_token (parser->lexer);
22394     }
22395 
22396   /* The same identifier shall not appear in multiple designators
22397      of a designated-initializer-list.  */
22398   if (first_designator)
22399     {
22400       unsigned int i;
22401       tree designator, val;
22402       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22403 	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22404 	  {
22405 	    if (IDENTIFIER_MARKED (designator))
22406 	      {
22407 		error_at (EXPR_LOC_OR_LOC (val, input_location),
22408 			  "%<.%s%> designator used multiple times in "
22409 			  "the same initializer list",
22410 			  IDENTIFIER_POINTER (designator));
22411 		(*v)[i].index = NULL_TREE;
22412 	      }
22413 	    else
22414 	      IDENTIFIER_MARKED (designator) = 1;
22415 	  }
22416       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22417 	if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22418 	  IDENTIFIER_MARKED (designator) = 0;
22419     }
22420 
22421   return v;
22422 }
22423 
22424 /* Classes [gram.class] */
22425 
22426 /* Parse a class-name.
22427 
22428    class-name:
22429      identifier
22430      template-id
22431 
22432    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22433    to indicate that names looked up in dependent types should be
22434    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
22435    keyword has been used to indicate that the name that appears next
22436    is a template.  TAG_TYPE indicates the explicit tag given before
22437    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
22438    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
22439    is the class being defined in a class-head.  If ENUM_OK is TRUE,
22440    enum-names are also accepted.
22441 
22442    Returns the TYPE_DECL representing the class.  */
22443 
22444 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)22445 cp_parser_class_name (cp_parser *parser,
22446 		      bool typename_keyword_p,
22447 		      bool template_keyword_p,
22448 		      enum tag_types tag_type,
22449 		      bool check_dependency_p,
22450 		      bool class_head_p,
22451 		      bool is_declaration,
22452 		      bool enum_ok)
22453 {
22454   tree decl;
22455   tree scope;
22456   bool typename_p;
22457   cp_token *token;
22458   tree identifier = NULL_TREE;
22459 
22460   /* All class-names start with an identifier.  */
22461   token = cp_lexer_peek_token (parser->lexer);
22462   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22463     {
22464       cp_parser_error (parser, "expected class-name");
22465       return error_mark_node;
22466     }
22467 
22468   /* PARSER->SCOPE can be cleared when parsing the template-arguments
22469      to a template-id, so we save it here.  */
22470   scope = parser->scope;
22471   if (scope == error_mark_node)
22472     return error_mark_node;
22473 
22474   /* Any name names a type if we're following the `typename' keyword
22475      in a qualified name where the enclosing scope is type-dependent.  */
22476   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22477 		&& dependent_type_p (scope));
22478   /* Handle the common case (an identifier, but not a template-id)
22479      efficiently.  */
22480   if (token->type == CPP_NAME
22481       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22482     {
22483       cp_token *identifier_token;
22484       bool ambiguous_p;
22485 
22486       /* Look for the identifier.  */
22487       identifier_token = cp_lexer_peek_token (parser->lexer);
22488       ambiguous_p = identifier_token->error_reported;
22489       identifier = cp_parser_identifier (parser);
22490       /* If the next token isn't an identifier, we are certainly not
22491 	 looking at a class-name.  */
22492       if (identifier == error_mark_node)
22493 	decl = error_mark_node;
22494       /* If we know this is a type-name, there's no need to look it
22495 	 up.  */
22496       else if (typename_p)
22497 	decl = identifier;
22498       else
22499 	{
22500 	  tree ambiguous_decls;
22501 	  /* If we already know that this lookup is ambiguous, then
22502 	     we've already issued an error message; there's no reason
22503 	     to check again.  */
22504 	  if (ambiguous_p)
22505 	    {
22506 	      cp_parser_simulate_error (parser);
22507 	      return error_mark_node;
22508 	    }
22509 	  /* If the next token is a `::', then the name must be a type
22510 	     name.
22511 
22512 	     [basic.lookup.qual]
22513 
22514 	     During the lookup for a name preceding the :: scope
22515 	     resolution operator, object, function, and enumerator
22516 	     names are ignored.  */
22517 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22518 	    tag_type = scope_type;
22519 	  /* Look up the name.  */
22520 	  decl = cp_parser_lookup_name (parser, identifier,
22521 					tag_type,
22522 					/*is_template=*/false,
22523 					/*is_namespace=*/false,
22524 					check_dependency_p,
22525 					&ambiguous_decls,
22526 					identifier_token->location);
22527 	  if (ambiguous_decls)
22528 	    {
22529 	      if (cp_parser_parsing_tentatively (parser))
22530 		cp_parser_simulate_error (parser);
22531 	      return error_mark_node;
22532 	    }
22533 	}
22534     }
22535   else
22536     {
22537       /* Try a template-id.  */
22538       decl = cp_parser_template_id (parser, template_keyword_p,
22539 				    check_dependency_p,
22540 				    tag_type,
22541 				    is_declaration);
22542       if (decl == error_mark_node)
22543 	return error_mark_node;
22544     }
22545 
22546   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22547 
22548   /* If this is a typename, create a TYPENAME_TYPE.  */
22549   if (typename_p && decl != error_mark_node)
22550     {
22551       decl = make_typename_type (scope, decl, typename_type,
22552 				 /*complain=*/tf_error);
22553       if (decl != error_mark_node)
22554 	decl = TYPE_NAME (decl);
22555     }
22556 
22557   decl = strip_using_decl (decl);
22558 
22559   /* Check to see that it is really the name of a class.  */
22560   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22561       && identifier_p (TREE_OPERAND (decl, 0))
22562       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22563     /* Situations like this:
22564 
22565 	 template <typename T> struct A {
22566 	   typename T::template X<int>::I i;
22567 	 };
22568 
22569        are problematic.  Is `T::template X<int>' a class-name?  The
22570        standard does not seem to be definitive, but there is no other
22571        valid interpretation of the following `::'.  Therefore, those
22572        names are considered class-names.  */
22573     {
22574       decl = make_typename_type (scope, decl, tag_type, tf_error);
22575       if (decl != error_mark_node)
22576 	decl = TYPE_NAME (decl);
22577     }
22578   else if (TREE_CODE (decl) != TYPE_DECL
22579 	   || TREE_TYPE (decl) == error_mark_node
22580 	   || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22581 		|| (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22582 	   /* In Objective-C 2.0, a classname followed by '.' starts a
22583 	      dot-syntax expression, and it's not a type-name.  */
22584 	   || (c_dialect_objc ()
22585 	       && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22586 	       && objc_is_class_name (decl)))
22587     decl = error_mark_node;
22588 
22589   if (decl == error_mark_node)
22590     cp_parser_error (parser, "expected class-name");
22591   else if (identifier && !parser->scope)
22592     maybe_note_name_used_in_class (identifier, decl);
22593 
22594   return decl;
22595 }
22596 
22597 /* Parse a class-specifier.
22598 
22599    class-specifier:
22600      class-head { member-specification [opt] }
22601 
22602    Returns the TREE_TYPE representing the class.  */
22603 
22604 static tree
cp_parser_class_specifier_1(cp_parser * parser)22605 cp_parser_class_specifier_1 (cp_parser* parser)
22606 {
22607   tree type;
22608   tree attributes = NULL_TREE;
22609   bool nested_name_specifier_p;
22610   unsigned saved_num_template_parameter_lists;
22611   bool saved_in_function_body;
22612   unsigned char in_statement;
22613   bool in_switch_statement_p;
22614   bool saved_in_unbraced_linkage_specification_p;
22615   tree old_scope = NULL_TREE;
22616   tree scope = NULL_TREE;
22617   cp_token *closing_brace;
22618 
22619   push_deferring_access_checks (dk_no_deferred);
22620 
22621   /* Parse the class-head.  */
22622   type = cp_parser_class_head (parser,
22623 			       &nested_name_specifier_p);
22624   /* If the class-head was a semantic disaster, skip the entire body
22625      of the class.  */
22626   if (!type)
22627     {
22628       cp_parser_skip_to_end_of_block_or_statement (parser);
22629       pop_deferring_access_checks ();
22630       return error_mark_node;
22631     }
22632 
22633   /* Look for the `{'.  */
22634   matching_braces braces;
22635   if (!braces.require_open (parser))
22636     {
22637       pop_deferring_access_checks ();
22638       return error_mark_node;
22639     }
22640 
22641   cp_ensure_no_omp_declare_simd (parser);
22642   cp_ensure_no_oacc_routine (parser);
22643 
22644   /* Issue an error message if type-definitions are forbidden here.  */
22645   bool type_definition_ok_p = cp_parser_check_type_definition (parser);
22646   /* Remember that we are defining one more class.  */
22647   ++parser->num_classes_being_defined;
22648   /* Inside the class, surrounding template-parameter-lists do not
22649      apply.  */
22650   saved_num_template_parameter_lists
22651     = parser->num_template_parameter_lists;
22652   parser->num_template_parameter_lists = 0;
22653   /* We are not in a function body.  */
22654   saved_in_function_body = parser->in_function_body;
22655   parser->in_function_body = false;
22656   /* Or in a loop.  */
22657   in_statement = parser->in_statement;
22658   parser->in_statement = 0;
22659   /* Or in a switch.  */
22660   in_switch_statement_p = parser->in_switch_statement_p;
22661   parser->in_switch_statement_p = false;
22662   /* We are not immediately inside an extern "lang" block.  */
22663   saved_in_unbraced_linkage_specification_p
22664     = parser->in_unbraced_linkage_specification_p;
22665   parser->in_unbraced_linkage_specification_p = false;
22666 
22667   // Associate constraints with the type.
22668   if (flag_concepts)
22669     type = associate_classtype_constraints (type);
22670 
22671   /* Start the class.  */
22672   if (nested_name_specifier_p)
22673     {
22674       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22675       old_scope = push_inner_scope (scope);
22676     }
22677   type = begin_class_definition (type);
22678 
22679   if (type == error_mark_node)
22680     /* If the type is erroneous, skip the entire body of the class.  */
22681     cp_parser_skip_to_closing_brace (parser);
22682   else
22683     /* Parse the member-specification.  */
22684     cp_parser_member_specification_opt (parser);
22685 
22686   /* Look for the trailing `}'.  */
22687   closing_brace = braces.require_close (parser);
22688   /* Look for trailing attributes to apply to this class.  */
22689   if (cp_parser_allow_gnu_extensions_p (parser))
22690     attributes = cp_parser_gnu_attributes_opt (parser);
22691   if (type != error_mark_node)
22692     type = finish_struct (type, attributes);
22693   if (nested_name_specifier_p)
22694     pop_inner_scope (old_scope, scope);
22695 
22696   /* We've finished a type definition.  Check for the common syntax
22697      error of forgetting a semicolon after the definition.  We need to
22698      be careful, as we can't just check for not-a-semicolon and be done
22699      with it; the user might have typed:
22700 
22701      class X { } c = ...;
22702      class X { } *p = ...;
22703 
22704      and so forth.  Instead, enumerate all the possible tokens that
22705      might follow this production; if we don't see one of them, then
22706      complain and silently insert the semicolon.  */
22707   {
22708     cp_token *token = cp_lexer_peek_token (parser->lexer);
22709     bool want_semicolon = true;
22710 
22711     if (cp_next_tokens_can_be_std_attribute_p (parser))
22712       /* Don't try to parse c++11 attributes here.  As per the
22713 	 grammar, that should be a task for
22714 	 cp_parser_decl_specifier_seq.  */
22715       want_semicolon = false;
22716 
22717     switch (token->type)
22718       {
22719       case CPP_NAME:
22720       case CPP_SEMICOLON:
22721       case CPP_MULT:
22722       case CPP_AND:
22723       case CPP_OPEN_PAREN:
22724       case CPP_CLOSE_PAREN:
22725       case CPP_COMMA:
22726         want_semicolon = false;
22727         break;
22728 
22729         /* While it's legal for type qualifiers and storage class
22730            specifiers to follow type definitions in the grammar, only
22731            compiler testsuites contain code like that.  Assume that if
22732            we see such code, then what we're really seeing is a case
22733            like:
22734 
22735            class X { }
22736            const <type> var = ...;
22737 
22738            or
22739 
22740            class Y { }
22741            static <type> func (...) ...
22742 
22743            i.e. the qualifier or specifier applies to the next
22744            declaration.  To do so, however, we need to look ahead one
22745            more token to see if *that* token is a type specifier.
22746 
22747 	   This code could be improved to handle:
22748 
22749 	   class Z { }
22750 	   static const <type> var = ...;  */
22751       case CPP_KEYWORD:
22752 	if (keyword_is_decl_specifier (token->keyword))
22753 	  {
22754 	    cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22755 
22756 	    /* Handling user-defined types here would be nice, but very
22757 	       tricky.  */
22758 	    want_semicolon
22759 	      = (lookahead->type == CPP_KEYWORD
22760 		 && keyword_begins_type_specifier (lookahead->keyword));
22761 	  }
22762 	break;
22763       default:
22764 	break;
22765       }
22766 
22767     /* If we don't have a type, then something is very wrong and we
22768        shouldn't try to do anything clever.  Likewise for not seeing the
22769        closing brace.  */
22770     if (closing_brace && TYPE_P (type) && want_semicolon)
22771       {
22772 	/* Locate the closing brace.  */
22773 	cp_token_position prev
22774 	  = cp_lexer_previous_token_position (parser->lexer);
22775 	cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22776 	location_t loc = prev_token->location;
22777 
22778 	/* We want to suggest insertion of a ';' immediately *after* the
22779 	   closing brace, so, if we can, offset the location by 1 column.  */
22780 	location_t next_loc = loc;
22781 	if (!linemap_location_from_macro_expansion_p (line_table, loc))
22782 	  next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22783 
22784 	rich_location richloc (line_table, next_loc);
22785 
22786 	/* If we successfully offset the location, suggest the fix-it.  */
22787 	if (next_loc != loc)
22788 	  richloc.add_fixit_insert_before (next_loc, ";");
22789 
22790 	if (CLASSTYPE_DECLARED_CLASS (type))
22791 	  error_at (&richloc,
22792 		    "expected %<;%> after class definition");
22793 	else if (TREE_CODE (type) == RECORD_TYPE)
22794 	  error_at (&richloc,
22795 		    "expected %<;%> after struct definition");
22796 	else if (TREE_CODE (type) == UNION_TYPE)
22797 	  error_at (&richloc,
22798 		    "expected %<;%> after union definition");
22799 	else
22800 	  gcc_unreachable ();
22801 
22802 	/* Unget one token and smash it to look as though we encountered
22803 	   a semicolon in the input stream.  */
22804 	cp_lexer_set_token_position (parser->lexer, prev);
22805 	token = cp_lexer_peek_token (parser->lexer);
22806 	token->type = CPP_SEMICOLON;
22807 	token->keyword = RID_MAX;
22808       }
22809   }
22810 
22811   /* If this class is not itself within the scope of another class,
22812      then we need to parse the bodies of all of the queued function
22813      definitions.  Note that the queued functions defined in a class
22814      are not always processed immediately following the
22815      class-specifier for that class.  Consider:
22816 
22817        struct A {
22818 	 struct B { void f() { sizeof (A); } };
22819        };
22820 
22821      If `f' were processed before the processing of `A' were
22822      completed, there would be no way to compute the size of `A'.
22823      Note that the nesting we are interested in here is lexical --
22824      not the semantic nesting given by TYPE_CONTEXT.  In particular,
22825      for:
22826 
22827        struct A { struct B; };
22828        struct A::B { void f() { } };
22829 
22830      there is no need to delay the parsing of `A::B::f'.  */
22831   if (--parser->num_classes_being_defined == 0)
22832     {
22833       tree decl;
22834       tree class_type = NULL_TREE;
22835       tree pushed_scope = NULL_TREE;
22836       unsigned ix;
22837       cp_default_arg_entry *e;
22838       tree save_ccp, save_ccr;
22839 
22840       if (!type_definition_ok_p || any_erroneous_template_args_p (type))
22841 	{
22842 	  /* Skip default arguments, NSDMIs, etc, in order to improve
22843 	     error recovery (c++/71169, c++/71832).  */
22844 	  vec_safe_truncate (unparsed_funs_with_default_args, 0);
22845 	  vec_safe_truncate (unparsed_nsdmis, 0);
22846 	  vec_safe_truncate (unparsed_classes, 0);
22847 	  vec_safe_truncate (unparsed_funs_with_definitions, 0);
22848 	}
22849 
22850       /* In a first pass, parse default arguments to the functions.
22851 	 Then, in a second pass, parse the bodies of the functions.
22852 	 This two-phased approach handles cases like:
22853 
22854 	    struct S {
22855 	      void f() { g(); }
22856 	      void g(int i = 3);
22857 	    };
22858 
22859 	 */
22860       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22861 	{
22862 	  decl = e->decl;
22863 	  /* If there are default arguments that have not yet been processed,
22864 	     take care of them now.  */
22865 	  if (class_type != e->class_type)
22866 	    {
22867 	      if (pushed_scope)
22868 		pop_scope (pushed_scope);
22869 	      class_type = e->class_type;
22870 	      pushed_scope = push_scope (class_type);
22871 	    }
22872 	  /* Make sure that any template parameters are in scope.  */
22873 	  maybe_begin_member_template_processing (decl);
22874 	  /* Parse the default argument expressions.  */
22875 	  cp_parser_late_parsing_default_args (parser, decl);
22876 	  /* Remove any template parameters from the symbol table.  */
22877 	  maybe_end_member_template_processing ();
22878 	}
22879       vec_safe_truncate (unparsed_funs_with_default_args, 0);
22880       /* Now parse any NSDMIs.  */
22881       save_ccp = current_class_ptr;
22882       save_ccr = current_class_ref;
22883       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22884 	{
22885 	  if (class_type != DECL_CONTEXT (decl))
22886 	    {
22887 	      if (pushed_scope)
22888 		pop_scope (pushed_scope);
22889 	      class_type = DECL_CONTEXT (decl);
22890 	      pushed_scope = push_scope (class_type);
22891 	    }
22892 	  inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22893 	  cp_parser_late_parsing_nsdmi (parser, decl);
22894 	}
22895       vec_safe_truncate (unparsed_nsdmis, 0);
22896       current_class_ptr = save_ccp;
22897       current_class_ref = save_ccr;
22898       if (pushed_scope)
22899 	pop_scope (pushed_scope);
22900 
22901       /* Now do some post-NSDMI bookkeeping.  */
22902       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22903 	after_nsdmi_defaulted_late_checks (class_type);
22904       vec_safe_truncate (unparsed_classes, 0);
22905       after_nsdmi_defaulted_late_checks (type);
22906 
22907       /* Now parse the body of the functions.  */
22908       if (flag_openmp)
22909 	{
22910 	  /* OpenMP UDRs need to be parsed before all other functions.  */
22911 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22912 	    if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22913 	      cp_parser_late_parsing_for_member (parser, decl);
22914 	  FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22915 	    if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22916 	      cp_parser_late_parsing_for_member (parser, decl);
22917 	}
22918       else
22919 	FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22920 	  cp_parser_late_parsing_for_member (parser, decl);
22921       vec_safe_truncate (unparsed_funs_with_definitions, 0);
22922     }
22923   else
22924     vec_safe_push (unparsed_classes, type);
22925 
22926   /* Put back any saved access checks.  */
22927   pop_deferring_access_checks ();
22928 
22929   /* Restore saved state.  */
22930   parser->in_switch_statement_p = in_switch_statement_p;
22931   parser->in_statement = in_statement;
22932   parser->in_function_body = saved_in_function_body;
22933   parser->num_template_parameter_lists
22934     = saved_num_template_parameter_lists;
22935   parser->in_unbraced_linkage_specification_p
22936     = saved_in_unbraced_linkage_specification_p;
22937 
22938   return type;
22939 }
22940 
22941 static tree
cp_parser_class_specifier(cp_parser * parser)22942 cp_parser_class_specifier (cp_parser* parser)
22943 {
22944   tree ret;
22945   timevar_push (TV_PARSE_STRUCT);
22946   ret = cp_parser_class_specifier_1 (parser);
22947   timevar_pop (TV_PARSE_STRUCT);
22948   return ret;
22949 }
22950 
22951 /* Parse a class-head.
22952 
22953    class-head:
22954      class-key identifier [opt] base-clause [opt]
22955      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22956      class-key nested-name-specifier [opt] template-id
22957        base-clause [opt]
22958 
22959    class-virt-specifier:
22960      final
22961 
22962    GNU Extensions:
22963      class-key attributes identifier [opt] base-clause [opt]
22964      class-key attributes nested-name-specifier identifier base-clause [opt]
22965      class-key attributes nested-name-specifier [opt] template-id
22966        base-clause [opt]
22967 
22968    Upon return BASES is initialized to the list of base classes (or
22969    NULL, if there are none) in the same form returned by
22970    cp_parser_base_clause.
22971 
22972    Returns the TYPE of the indicated class.  Sets
22973    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22974    involving a nested-name-specifier was used, and FALSE otherwise.
22975 
22976    Returns error_mark_node if this is not a class-head.
22977 
22978    Returns NULL_TREE if the class-head is syntactically valid, but
22979    semantically invalid in a way that means we should skip the entire
22980    body of the class.  */
22981 
22982 static tree
cp_parser_class_head(cp_parser * parser,bool * nested_name_specifier_p)22983 cp_parser_class_head (cp_parser* parser,
22984 		      bool* nested_name_specifier_p)
22985 {
22986   tree nested_name_specifier;
22987   enum tag_types class_key;
22988   tree id = NULL_TREE;
22989   tree type = NULL_TREE;
22990   tree attributes;
22991   tree bases;
22992   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22993   bool template_id_p = false;
22994   bool qualified_p = false;
22995   bool invalid_nested_name_p = false;
22996   bool invalid_explicit_specialization_p = false;
22997   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22998   tree pushed_scope = NULL_TREE;
22999   unsigned num_templates;
23000   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23001   /* Assume no nested-name-specifier will be present.  */
23002   *nested_name_specifier_p = false;
23003   /* Assume no template parameter lists will be used in defining the
23004      type.  */
23005   num_templates = 0;
23006   parser->colon_corrects_to_scope_p = false;
23007 
23008   /* Look for the class-key.  */
23009   class_key = cp_parser_class_key (parser);
23010   if (class_key == none_type)
23011     return error_mark_node;
23012 
23013   location_t class_head_start_location = input_location;
23014 
23015   /* Parse the attributes.  */
23016   attributes = cp_parser_attributes_opt (parser);
23017 
23018   /* If the next token is `::', that is invalid -- but sometimes
23019      people do try to write:
23020 
23021        struct ::S {};
23022 
23023      Handle this gracefully by accepting the extra qualifier, and then
23024      issuing an error about it later if this really is a
23025      class-head.  If it turns out just to be an elaborated type
23026      specifier, remain silent.  */
23027   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23028     qualified_p = true;
23029 
23030   push_deferring_access_checks (dk_no_check);
23031 
23032   /* Determine the name of the class.  Begin by looking for an
23033      optional nested-name-specifier.  */
23034   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23035   nested_name_specifier
23036     = cp_parser_nested_name_specifier_opt (parser,
23037 					   /*typename_keyword_p=*/false,
23038 					   /*check_dependency_p=*/false,
23039 					   /*type_p=*/true,
23040 					   /*is_declaration=*/false);
23041   /* If there was a nested-name-specifier, then there *must* be an
23042      identifier.  */
23043 
23044   cp_token *bad_template_keyword = NULL;
23045 
23046   if (nested_name_specifier)
23047     {
23048       type_start_token = cp_lexer_peek_token (parser->lexer);
23049       /* Although the grammar says `identifier', it really means
23050 	 `class-name' or `template-name'.  You are only allowed to
23051 	 define a class that has already been declared with this
23052 	 syntax.
23053 
23054 	 The proposed resolution for Core Issue 180 says that wherever
23055 	 you see `class T::X' you should treat `X' as a type-name.
23056 
23057 	 It is OK to define an inaccessible class; for example:
23058 
23059 	   class A { class B; };
23060 	   class A::B {};
23061 
23062 	 We do not know if we will see a class-name, or a
23063 	 template-name.  We look for a class-name first, in case the
23064 	 class-name is a template-id; if we looked for the
23065 	 template-name first we would stop after the template-name.  */
23066       cp_parser_parse_tentatively (parser);
23067       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23068 	bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23069       type = cp_parser_class_name (parser,
23070 				   /*typename_keyword_p=*/false,
23071 				   /*template_keyword_p=*/false,
23072 				   class_type,
23073 				   /*check_dependency_p=*/false,
23074 				   /*class_head_p=*/true,
23075 				   /*is_declaration=*/false);
23076       /* If that didn't work, ignore the nested-name-specifier.  */
23077       if (!cp_parser_parse_definitely (parser))
23078 	{
23079 	  invalid_nested_name_p = true;
23080 	  type_start_token = cp_lexer_peek_token (parser->lexer);
23081 	  id = cp_parser_identifier (parser);
23082 	  if (id == error_mark_node)
23083 	    id = NULL_TREE;
23084 	}
23085       /* If we could not find a corresponding TYPE, treat this
23086 	 declaration like an unqualified declaration.  */
23087       if (type == error_mark_node)
23088 	nested_name_specifier = NULL_TREE;
23089       /* Otherwise, count the number of templates used in TYPE and its
23090 	 containing scopes.  */
23091       else
23092 	{
23093 	  tree scope;
23094 
23095 	  for (scope = TREE_TYPE (type);
23096 	       scope && TREE_CODE (scope) != NAMESPACE_DECL;
23097 	       scope = get_containing_scope (scope))
23098 	    if (TYPE_P (scope)
23099 		&& CLASS_TYPE_P (scope)
23100 		&& CLASSTYPE_TEMPLATE_INFO (scope)
23101 		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
23102 		&& (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
23103 		    || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
23104 	      ++num_templates;
23105 	}
23106     }
23107   /* Otherwise, the identifier is optional.  */
23108   else
23109     {
23110       /* We don't know whether what comes next is a template-id,
23111 	 an identifier, or nothing at all.  */
23112       cp_parser_parse_tentatively (parser);
23113       /* Check for a template-id.  */
23114       type_start_token = cp_lexer_peek_token (parser->lexer);
23115       id = cp_parser_template_id (parser,
23116 				  /*template_keyword_p=*/false,
23117 				  /*check_dependency_p=*/true,
23118 				  class_key,
23119 				  /*is_declaration=*/true);
23120       /* If that didn't work, it could still be an identifier.  */
23121       if (!cp_parser_parse_definitely (parser))
23122 	{
23123 	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23124 	    {
23125 	      type_start_token = cp_lexer_peek_token (parser->lexer);
23126 	      id = cp_parser_identifier (parser);
23127 	    }
23128 	  else
23129 	    id = NULL_TREE;
23130 	}
23131       else
23132 	{
23133 	  template_id_p = true;
23134 	  ++num_templates;
23135 	}
23136     }
23137 
23138   pop_deferring_access_checks ();
23139 
23140   if (id)
23141     {
23142       cp_parser_check_for_invalid_template_id (parser, id,
23143 					       class_key,
23144                                                type_start_token->location);
23145     }
23146   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23147 
23148   /* If it's not a `:' or a `{' then we can't really be looking at a
23149      class-head, since a class-head only appears as part of a
23150      class-specifier.  We have to detect this situation before calling
23151      xref_tag, since that has irreversible side-effects.  */
23152   if (!cp_parser_next_token_starts_class_definition_p (parser))
23153     {
23154       cp_parser_error (parser, "expected %<{%> or %<:%>");
23155       type = error_mark_node;
23156       goto out;
23157     }
23158 
23159   /* At this point, we're going ahead with the class-specifier, even
23160      if some other problem occurs.  */
23161   cp_parser_commit_to_tentative_parse (parser);
23162   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23163     {
23164       cp_parser_error (parser,
23165                        "cannot specify %<override%> for a class");
23166       type = error_mark_node;
23167       goto out;
23168     }
23169   /* Issue the error about the overly-qualified name now.  */
23170   if (qualified_p)
23171     {
23172       cp_parser_error (parser,
23173 		       "global qualification of class name is invalid");
23174       type = error_mark_node;
23175       goto out;
23176     }
23177   else if (invalid_nested_name_p)
23178     {
23179       cp_parser_error (parser,
23180 		       "qualified name does not name a class");
23181       type = error_mark_node;
23182       goto out;
23183     }
23184   else if (nested_name_specifier)
23185     {
23186       tree scope;
23187 
23188       if (bad_template_keyword)
23189 	/* [temp.names]: in a qualified-id formed by a class-head-name, the
23190 	   keyword template shall not appear at the top level.  */
23191 	pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23192 		 "keyword %<template%> not allowed in class-head-name");
23193 
23194       /* Reject typedef-names in class heads.  */
23195       if (!DECL_IMPLICIT_TYPEDEF_P (type))
23196 	{
23197 	  error_at (type_start_token->location,
23198 		    "invalid class name in declaration of %qD",
23199 		    type);
23200 	  type = NULL_TREE;
23201 	  goto done;
23202 	}
23203 
23204       /* Figure out in what scope the declaration is being placed.  */
23205       scope = current_scope ();
23206       /* If that scope does not contain the scope in which the
23207 	 class was originally declared, the program is invalid.  */
23208       if (scope && !is_ancestor (scope, nested_name_specifier))
23209 	{
23210 	  if (at_namespace_scope_p ())
23211 	    error_at (type_start_token->location,
23212 		      "declaration of %qD in namespace %qD which does not "
23213 		      "enclose %qD",
23214 		      type, scope, nested_name_specifier);
23215 	  else
23216 	    error_at (type_start_token->location,
23217 		      "declaration of %qD in %qD which does not enclose %qD",
23218 		      type, scope, nested_name_specifier);
23219 	  type = NULL_TREE;
23220 	  goto done;
23221 	}
23222       /* [dcl.meaning]
23223 
23224 	 A declarator-id shall not be qualified except for the
23225 	 definition of a ... nested class outside of its class
23226 	 ... [or] the definition or explicit instantiation of a
23227 	 class member of a namespace outside of its namespace.  */
23228       if (scope == nested_name_specifier)
23229 	{
23230 	  permerror (nested_name_specifier_token_start->location,
23231 		     "extra qualification not allowed");
23232 	  nested_name_specifier = NULL_TREE;
23233 	  num_templates = 0;
23234 	}
23235     }
23236   /* An explicit-specialization must be preceded by "template <>".  If
23237      it is not, try to recover gracefully.  */
23238   if (at_namespace_scope_p ()
23239       && parser->num_template_parameter_lists == 0
23240       && !processing_template_parmlist
23241       && template_id_p)
23242     {
23243       /* Build a location of this form:
23244            struct typename <ARGS>
23245            ^~~~~~~~~~~~~~~~~~~~~~
23246          with caret==start at the start token, and
23247          finishing at the end of the type.  */
23248       location_t reported_loc
23249         = make_location (class_head_start_location,
23250                          class_head_start_location,
23251                          get_finish (type_start_token->location));
23252       rich_location richloc (line_table, reported_loc);
23253       richloc.add_fixit_insert_before (class_head_start_location,
23254                                        "template <> ");
23255       error_at (&richloc,
23256 		"an explicit specialization must be preceded by"
23257 		" %<template <>%>");
23258       invalid_explicit_specialization_p = true;
23259       /* Take the same action that would have been taken by
23260 	 cp_parser_explicit_specialization.  */
23261       ++parser->num_template_parameter_lists;
23262       begin_specialization ();
23263     }
23264   /* There must be no "return" statements between this point and the
23265      end of this function; set "type "to the correct return value and
23266      use "goto done;" to return.  */
23267   /* Make sure that the right number of template parameters were
23268      present.  */
23269   if (!cp_parser_check_template_parameters (parser, num_templates,
23270 					    template_id_p,
23271 					    type_start_token->location,
23272 					    /*declarator=*/NULL))
23273     {
23274       /* If something went wrong, there is no point in even trying to
23275 	 process the class-definition.  */
23276       type = NULL_TREE;
23277       goto done;
23278     }
23279 
23280   /* Look up the type.  */
23281   if (template_id_p)
23282     {
23283       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23284 	  && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23285 	      || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23286 	{
23287 	  error_at (type_start_token->location,
23288 		    "function template %qD redeclared as a class template", id);
23289 	  type = error_mark_node;
23290 	}
23291       else
23292 	{
23293 	  type = TREE_TYPE (id);
23294 	  type = maybe_process_partial_specialization (type);
23295 
23296 	  /* Check the scope while we still know whether or not we had a
23297 	     nested-name-specifier.  */
23298 	  if (type != error_mark_node)
23299 	    check_unqualified_spec_or_inst (type, type_start_token->location);
23300 	}
23301       if (nested_name_specifier)
23302 	pushed_scope = push_scope (nested_name_specifier);
23303     }
23304   else if (nested_name_specifier)
23305     {
23306       tree class_type;
23307 
23308       /* Given:
23309 
23310 	    template <typename T> struct S { struct T };
23311 	    template <typename T> struct S<T>::T { };
23312 
23313 	 we will get a TYPENAME_TYPE when processing the definition of
23314 	 `S::T'.  We need to resolve it to the actual type before we
23315 	 try to define it.  */
23316       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23317 	{
23318 	  class_type = resolve_typename_type (TREE_TYPE (type),
23319 					      /*only_current_p=*/false);
23320 	  if (TREE_CODE (class_type) != TYPENAME_TYPE)
23321 	    type = TYPE_NAME (class_type);
23322 	  else
23323 	    {
23324 	      cp_parser_error (parser, "could not resolve typename type");
23325 	      type = error_mark_node;
23326 	    }
23327 	}
23328 
23329       if (maybe_process_partial_specialization (TREE_TYPE (type))
23330 	  == error_mark_node)
23331 	{
23332 	  type = NULL_TREE;
23333 	  goto done;
23334 	}
23335 
23336       class_type = current_class_type;
23337       /* Enter the scope indicated by the nested-name-specifier.  */
23338       pushed_scope = push_scope (nested_name_specifier);
23339       /* Get the canonical version of this type.  */
23340       type = TYPE_MAIN_DECL (TREE_TYPE (type));
23341       /* Call push_template_decl if it seems like we should be defining a
23342 	 template either from the template headers or the type we're
23343 	 defining, so that we diagnose both extra and missing headers.  */
23344       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23345 	   || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23346 	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23347 	{
23348 	  type = push_template_decl (type);
23349 	  if (type == error_mark_node)
23350 	    {
23351 	      type = NULL_TREE;
23352 	      goto done;
23353 	    }
23354 	}
23355 
23356       type = TREE_TYPE (type);
23357       *nested_name_specifier_p = true;
23358     }
23359   else      /* The name is not a nested name.  */
23360     {
23361       /* If the class was unnamed, create a dummy name.  */
23362       if (!id)
23363 	id = make_anon_name ();
23364       tag_scope tag_scope = (parser->in_type_id_in_expr_p
23365 			     ? ts_within_enclosing_non_class
23366 			     : ts_current);
23367       type = xref_tag (class_key, id, tag_scope,
23368 		       parser->num_template_parameter_lists);
23369     }
23370 
23371   /* Indicate whether this class was declared as a `class' or as a
23372      `struct'.  */
23373   if (TREE_CODE (type) == RECORD_TYPE)
23374     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23375   cp_parser_check_class_key (class_key, type);
23376 
23377   /* If this type was already complete, and we see another definition,
23378      that's an error.  */
23379   if (type != error_mark_node && COMPLETE_TYPE_P (type))
23380     {
23381       error_at (type_start_token->location, "redefinition of %q#T",
23382 		type);
23383       inform (location_of (type), "previous definition of %q#T",
23384 	      type);
23385       type = NULL_TREE;
23386       goto done;
23387     }
23388   else if (type == error_mark_node)
23389     type = NULL_TREE;
23390 
23391   if (type)
23392     {
23393       /* Apply attributes now, before any use of the class as a template
23394 	 argument in its base list.  */
23395       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23396       fixup_attribute_variants (type);
23397     }
23398 
23399   /* We will have entered the scope containing the class; the names of
23400      base classes should be looked up in that context.  For example:
23401 
23402        struct A { struct B {}; struct C; };
23403        struct A::C : B {};
23404 
23405      is valid.  */
23406 
23407   /* Get the list of base-classes, if there is one.  */
23408   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23409     {
23410       /* PR59482: enter the class scope so that base-specifiers are looked
23411 	 up correctly.  */
23412       if (type)
23413 	pushclass (type);
23414       bases = cp_parser_base_clause (parser);
23415       /* PR59482: get out of the previously pushed class scope so that the
23416 	 subsequent pops pop the right thing.  */
23417       if (type)
23418 	popclass ();
23419     }
23420   else
23421     bases = NULL_TREE;
23422 
23423   /* If we're really defining a class, process the base classes.
23424      If they're invalid, fail.  */
23425   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23426     xref_basetypes (type, bases);
23427 
23428  done:
23429   /* Leave the scope given by the nested-name-specifier.  We will
23430      enter the class scope itself while processing the members.  */
23431   if (pushed_scope)
23432     pop_scope (pushed_scope);
23433 
23434   if (invalid_explicit_specialization_p)
23435     {
23436       end_specialization ();
23437       --parser->num_template_parameter_lists;
23438     }
23439 
23440   if (type)
23441     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23442   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23443     CLASSTYPE_FINAL (type) = 1;
23444  out:
23445   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23446   return type;
23447 }
23448 
23449 /* Parse a class-key.
23450 
23451    class-key:
23452      class
23453      struct
23454      union
23455 
23456    Returns the kind of class-key specified, or none_type to indicate
23457    error.  */
23458 
23459 static enum tag_types
cp_parser_class_key(cp_parser * parser)23460 cp_parser_class_key (cp_parser* parser)
23461 {
23462   cp_token *token;
23463   enum tag_types tag_type;
23464 
23465   /* Look for the class-key.  */
23466   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23467   if (!token)
23468     return none_type;
23469 
23470   /* Check to see if the TOKEN is a class-key.  */
23471   tag_type = cp_parser_token_is_class_key (token);
23472   if (!tag_type)
23473     cp_parser_error (parser, "expected class-key");
23474   return tag_type;
23475 }
23476 
23477 /* Parse a type-parameter-key.
23478 
23479    type-parameter-key:
23480      class
23481      typename
23482  */
23483 
23484 static void
cp_parser_type_parameter_key(cp_parser * parser)23485 cp_parser_type_parameter_key (cp_parser* parser)
23486 {
23487   /* Look for the type-parameter-key.  */
23488   enum tag_types tag_type = none_type;
23489   cp_token *token = cp_lexer_peek_token (parser->lexer);
23490   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23491     {
23492       cp_lexer_consume_token (parser->lexer);
23493       if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23494 	/* typename is not allowed in a template template parameter
23495 	   by the standard until C++17.  */
23496 	pedwarn (token->location, OPT_Wpedantic,
23497 		 "ISO C++ forbids typename key in template template parameter;"
23498 		 " use -std=c++17 or -std=gnu++17");
23499     }
23500   else
23501     cp_parser_error (parser, "expected %<class%> or %<typename%>");
23502 
23503   return;
23504 }
23505 
23506 /* Parse an (optional) member-specification.
23507 
23508    member-specification:
23509      member-declaration member-specification [opt]
23510      access-specifier : member-specification [opt]  */
23511 
23512 static void
cp_parser_member_specification_opt(cp_parser * parser)23513 cp_parser_member_specification_opt (cp_parser* parser)
23514 {
23515   while (true)
23516     {
23517       cp_token *token;
23518       enum rid keyword;
23519 
23520       /* Peek at the next token.  */
23521       token = cp_lexer_peek_token (parser->lexer);
23522       /* If it's a `}', or EOF then we've seen all the members.  */
23523       if (token->type == CPP_CLOSE_BRACE
23524 	  || token->type == CPP_EOF
23525 	  || token->type == CPP_PRAGMA_EOL)
23526 	break;
23527 
23528       /* See if this token is a keyword.  */
23529       keyword = token->keyword;
23530       switch (keyword)
23531 	{
23532 	case RID_PUBLIC:
23533 	case RID_PROTECTED:
23534 	case RID_PRIVATE:
23535 	  /* Consume the access-specifier.  */
23536 	  cp_lexer_consume_token (parser->lexer);
23537 	  /* Remember which access-specifier is active.  */
23538 	  current_access_specifier = token->u.value;
23539 	  /* Look for the `:'.  */
23540 	  cp_parser_require (parser, CPP_COLON, RT_COLON);
23541 	  break;
23542 
23543 	default:
23544 	  /* Accept #pragmas at class scope.  */
23545 	  if (token->type == CPP_PRAGMA)
23546 	    {
23547 	      cp_parser_pragma (parser, pragma_member, NULL);
23548 	      break;
23549 	    }
23550 
23551 	  /* Otherwise, the next construction must be a
23552 	     member-declaration.  */
23553 	  cp_parser_member_declaration (parser);
23554 	}
23555     }
23556 }
23557 
23558 /* Parse a member-declaration.
23559 
23560    member-declaration:
23561      decl-specifier-seq [opt] member-declarator-list [opt] ;
23562      function-definition ; [opt]
23563      :: [opt] nested-name-specifier template [opt] unqualified-id ;
23564      using-declaration
23565      template-declaration
23566      alias-declaration
23567 
23568    member-declarator-list:
23569      member-declarator
23570      member-declarator-list , member-declarator
23571 
23572    member-declarator:
23573      declarator pure-specifier [opt]
23574      declarator constant-initializer [opt]
23575      identifier [opt] : constant-expression
23576 
23577    GNU Extensions:
23578 
23579    member-declaration:
23580      __extension__ member-declaration
23581 
23582    member-declarator:
23583      declarator attributes [opt] pure-specifier [opt]
23584      declarator attributes [opt] constant-initializer [opt]
23585      identifier [opt] attributes [opt] : constant-expression
23586 
23587    C++0x Extensions:
23588 
23589    member-declaration:
23590      static_assert-declaration  */
23591 
23592 static void
cp_parser_member_declaration(cp_parser * parser)23593 cp_parser_member_declaration (cp_parser* parser)
23594 {
23595   cp_decl_specifier_seq decl_specifiers;
23596   tree prefix_attributes;
23597   tree decl;
23598   int declares_class_or_enum;
23599   bool friend_p;
23600   cp_token *token = NULL;
23601   cp_token *decl_spec_token_start = NULL;
23602   cp_token *initializer_token_start = NULL;
23603   int saved_pedantic;
23604   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23605 
23606   /* Check for the `__extension__' keyword.  */
23607   if (cp_parser_extension_opt (parser, &saved_pedantic))
23608     {
23609       /* Recurse.  */
23610       cp_parser_member_declaration (parser);
23611       /* Restore the old value of the PEDANTIC flag.  */
23612       pedantic = saved_pedantic;
23613 
23614       return;
23615     }
23616 
23617   /* Check for a template-declaration.  */
23618   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23619     {
23620       /* An explicit specialization here is an error condition, and we
23621 	 expect the specialization handler to detect and report this.  */
23622       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23623 	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23624 	cp_parser_explicit_specialization (parser);
23625       else
23626 	cp_parser_template_declaration (parser, /*member_p=*/true);
23627 
23628       return;
23629     }
23630   /* Check for a template introduction.  */
23631   else if (cp_parser_template_declaration_after_export (parser, true))
23632     return;
23633 
23634   /* Check for a using-declaration.  */
23635   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23636     {
23637       if (cxx_dialect < cxx11)
23638 	{
23639 	  /* Parse the using-declaration.  */
23640 	  cp_parser_using_declaration (parser,
23641 				       /*access_declaration_p=*/false);
23642 	  return;
23643 	}
23644       else
23645 	{
23646 	  tree decl;
23647 	  bool alias_decl_expected;
23648 	  cp_parser_parse_tentatively (parser);
23649 	  decl = cp_parser_alias_declaration (parser);
23650 	  /* Note that if we actually see the '=' token after the
23651 	     identifier, cp_parser_alias_declaration commits the
23652 	     tentative parse.  In that case, we really expect an
23653 	     alias-declaration.  Otherwise, we expect a using
23654 	     declaration.  */
23655 	  alias_decl_expected =
23656 	    !cp_parser_uncommitted_to_tentative_parse_p (parser);
23657 	  cp_parser_parse_definitely (parser);
23658 
23659 	  if (alias_decl_expected)
23660 	    finish_member_declaration (decl);
23661 	  else
23662 	    cp_parser_using_declaration (parser,
23663 					 /*access_declaration_p=*/false);
23664 	  return;
23665 	}
23666     }
23667 
23668   /* Check for @defs.  */
23669   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23670     {
23671       tree ivar, member;
23672       tree ivar_chains = cp_parser_objc_defs_expression (parser);
23673       ivar = ivar_chains;
23674       while (ivar)
23675 	{
23676 	  member = ivar;
23677 	  ivar = TREE_CHAIN (member);
23678 	  TREE_CHAIN (member) = NULL_TREE;
23679 	  finish_member_declaration (member);
23680 	}
23681       return;
23682     }
23683 
23684   /* If the next token is `static_assert' we have a static assertion.  */
23685   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23686     {
23687       cp_parser_static_assert (parser, /*member_p=*/true);
23688       return;
23689     }
23690 
23691   parser->colon_corrects_to_scope_p = false;
23692 
23693   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23694       goto out;
23695 
23696   /* Parse the decl-specifier-seq.  */
23697   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23698   cp_parser_decl_specifier_seq (parser,
23699 				CP_PARSER_FLAGS_OPTIONAL,
23700 				&decl_specifiers,
23701 				&declares_class_or_enum);
23702   /* Check for an invalid type-name.  */
23703   if (!decl_specifiers.any_type_specifiers_p
23704       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23705     goto out;
23706   /* If there is no declarator, then the decl-specifier-seq should
23707      specify a type.  */
23708   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23709     {
23710       /* If there was no decl-specifier-seq, and the next token is a
23711 	 `;', then we have something like:
23712 
23713 	   struct S { ; };
23714 
23715 	 [class.mem]
23716 
23717 	 Each member-declaration shall declare at least one member
23718 	 name of the class.  */
23719       if (!decl_specifiers.any_specifiers_p)
23720 	{
23721 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
23722 	  if (!in_system_header_at (token->location))
23723 	    {
23724 	      gcc_rich_location richloc (token->location);
23725 	      richloc.add_fixit_remove ();
23726 	      pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23727 	    }
23728 	}
23729       else
23730 	{
23731 	  tree type;
23732 
23733 	  /* See if this declaration is a friend.  */
23734 	  friend_p = cp_parser_friend_p (&decl_specifiers);
23735 	  /* If there were decl-specifiers, check to see if there was
23736 	     a class-declaration.  */
23737 	  type = check_tag_decl (&decl_specifiers,
23738 				 /*explicit_type_instantiation_p=*/false);
23739 	  /* Nested classes have already been added to the class, but
23740 	     a `friend' needs to be explicitly registered.  */
23741 	  if (friend_p)
23742 	    {
23743 	      /* If the `friend' keyword was present, the friend must
23744 		 be introduced with a class-key.  */
23745 	       if (!declares_class_or_enum && cxx_dialect < cxx11)
23746 		 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23747 			  "in C++03 a class-key must be used "
23748 			  "when declaring a friend");
23749 	       /* In this case:
23750 
23751 		    template <typename T> struct A {
23752 		      friend struct A<T>::B;
23753 		    };
23754 
23755 		  A<T>::B will be represented by a TYPENAME_TYPE, and
23756 		  therefore not recognized by check_tag_decl.  */
23757 	       if (!type)
23758 		 {
23759 		   type = decl_specifiers.type;
23760 		   if (type && TREE_CODE (type) == TYPE_DECL)
23761 		     type = TREE_TYPE (type);
23762 		 }
23763 	       if (!type || !TYPE_P (type))
23764 		 error_at (decl_spec_token_start->location,
23765 			   "friend declaration does not name a class or "
23766 			   "function");
23767 	       else
23768 		 make_friend_class (current_class_type, type,
23769 				    /*complain=*/true);
23770 	    }
23771 	  /* If there is no TYPE, an error message will already have
23772 	     been issued.  */
23773 	  else if (!type || type == error_mark_node)
23774 	    ;
23775 	  /* An anonymous aggregate has to be handled specially; such
23776 	     a declaration really declares a data member (with a
23777 	     particular type), as opposed to a nested class.  */
23778 	  else if (ANON_AGGR_TYPE_P (type))
23779 	    {
23780 	      /* C++11 9.5/6.  */
23781 	      if (decl_specifiers.storage_class != sc_none)
23782 		error_at (decl_spec_token_start->location,
23783 			  "a storage class on an anonymous aggregate "
23784 			  "in class scope is not allowed");
23785 
23786 	      /* Remove constructors and such from TYPE, now that we
23787 		 know it is an anonymous aggregate.  */
23788 	      fixup_anonymous_aggr (type);
23789 	      /* And make the corresponding data member.  */
23790 	      decl = build_decl (decl_spec_token_start->location,
23791 				 FIELD_DECL, NULL_TREE, type);
23792 	      /* Add it to the class.  */
23793 	      finish_member_declaration (decl);
23794 	    }
23795 	  else
23796 	    cp_parser_check_access_in_redeclaration
23797 					      (TYPE_NAME (type),
23798 					       decl_spec_token_start->location);
23799 	}
23800     }
23801   else
23802     {
23803       bool assume_semicolon = false;
23804 
23805       /* Clear attributes from the decl_specifiers but keep them
23806 	 around as prefix attributes that apply them to the entity
23807 	 being declared.  */
23808       prefix_attributes = decl_specifiers.attributes;
23809       decl_specifiers.attributes = NULL_TREE;
23810 
23811       /* See if these declarations will be friends.  */
23812       friend_p = cp_parser_friend_p (&decl_specifiers);
23813 
23814       /* Keep going until we hit the `;' at the end of the
23815 	 declaration.  */
23816       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23817 	{
23818 	  tree attributes = NULL_TREE;
23819 	  tree first_attribute;
23820 	  tree initializer;
23821 	  bool named_bitfld = false;
23822 
23823 	  /* Peek at the next token.  */
23824 	  token = cp_lexer_peek_token (parser->lexer);
23825 
23826 	  /* The following code wants to know early if it is a bit-field
23827 	     or some other declaration.  Attributes can appear before
23828 	     the `:' token.  Skip over them without consuming any tokens
23829 	     to peek if they are followed by `:'.  */
23830 	  if (cp_next_tokens_can_be_attribute_p (parser)
23831 	      || (token->type == CPP_NAME
23832 		  && cp_nth_tokens_can_be_attribute_p (parser, 2)
23833 		  && (named_bitfld = true)))
23834 	    {
23835 	      size_t n
23836 		= cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23837 	      token = cp_lexer_peek_nth_token (parser->lexer, n);
23838 	    }
23839 
23840 	  /* Check for a bitfield declaration.  */
23841 	  if (token->type == CPP_COLON
23842 	      || (token->type == CPP_NAME
23843 		  && token == cp_lexer_peek_token (parser->lexer)
23844 		  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23845 		  && (named_bitfld = true)))
23846 	    {
23847 	      tree identifier;
23848 	      tree width;
23849 	      tree late_attributes = NULL_TREE;
23850 
23851 	      if (named_bitfld)
23852 		identifier = cp_parser_identifier (parser);
23853 	      else
23854 		identifier = NULL_TREE;
23855 
23856 	      /* Look for attributes that apply to the bitfield.  */
23857 	      attributes = cp_parser_attributes_opt (parser);
23858 
23859 	      /* Consume the `:' token.  */
23860 	      cp_lexer_consume_token (parser->lexer);
23861 
23862 	      /* Get the width of the bitfield.  */
23863 	      width = cp_parser_constant_expression (parser, false, NULL,
23864 						     cxx_dialect >= cxx11);
23865 
23866 	      /* In C++2A and as extension for C++11 and above we allow
23867 		 default member initializers for bit-fields.  */
23868 	      initializer = NULL_TREE;
23869 	      if (cxx_dialect >= cxx11
23870 		  && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23871 		      || cp_lexer_next_token_is (parser->lexer,
23872 						 CPP_OPEN_BRACE)))
23873 		{
23874 		  location_t loc
23875 		    = cp_lexer_peek_token (parser->lexer)->location;
23876 		  if (cxx_dialect < cxx2a
23877 		      && !in_system_header_at (loc)
23878 		      && identifier != NULL_TREE)
23879 		    pedwarn (loc, 0,
23880 			     "default member initializers for bit-fields "
23881 			     "only available with -std=c++2a or "
23882 			     "-std=gnu++2a");
23883 
23884 		  initializer = cp_parser_save_nsdmi (parser);
23885 		  if (identifier == NULL_TREE)
23886 		    {
23887 		      error_at (loc, "default member initializer for "
23888 				     "unnamed bit-field");
23889 		      initializer = NULL_TREE;
23890 		    }
23891 		}
23892 	      else
23893 		{
23894 		  /* Look for attributes that apply to the bitfield after
23895 		     the `:' token and width.  This is where GCC used to
23896 		     parse attributes in the past, pedwarn if there is
23897 		     a std attribute.  */
23898 		  if (cp_next_tokens_can_be_std_attribute_p (parser))
23899 		    pedwarn (input_location, OPT_Wpedantic,
23900 			     "ISO C++ allows bit-field attributes only "
23901 			     "before the %<:%> token");
23902 
23903 		  late_attributes = cp_parser_attributes_opt (parser);
23904 		}
23905 
23906 	      attributes = attr_chainon (attributes, late_attributes);
23907 
23908 	      /* Remember which attributes are prefix attributes and
23909 		 which are not.  */
23910 	      first_attribute = attributes;
23911 	      /* Combine the attributes.  */
23912 	      attributes = attr_chainon (prefix_attributes, attributes);
23913 
23914 	      /* Create the bitfield declaration.  */
23915 	      decl = grokbitfield (identifier
23916 				   ? make_id_declarator (NULL_TREE,
23917 							 identifier,
23918 							 sfk_none)
23919 				   : NULL,
23920 				   &decl_specifiers,
23921 				   width, initializer,
23922 				   attributes);
23923 	    }
23924 	  else
23925 	    {
23926 	      cp_declarator *declarator;
23927 	      tree asm_specification;
23928 	      int ctor_dtor_or_conv_p;
23929 
23930 	      /* Parse the declarator.  */
23931 	      declarator
23932 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23933 					&ctor_dtor_or_conv_p,
23934 					/*parenthesized_p=*/NULL,
23935 					/*member_p=*/true,
23936 					friend_p);
23937 
23938 	      /* If something went wrong parsing the declarator, make sure
23939 		 that we at least consume some tokens.  */
23940 	      if (declarator == cp_error_declarator)
23941 		{
23942 		  /* Skip to the end of the statement.  */
23943 		  cp_parser_skip_to_end_of_statement (parser);
23944 		  /* If the next token is not a semicolon, that is
23945 		     probably because we just skipped over the body of
23946 		     a function.  So, we consume a semicolon if
23947 		     present, but do not issue an error message if it
23948 		     is not present.  */
23949 		  if (cp_lexer_next_token_is (parser->lexer,
23950 					      CPP_SEMICOLON))
23951 		    cp_lexer_consume_token (parser->lexer);
23952 		  goto out;
23953 		}
23954 
23955 	      if (declares_class_or_enum & 2)
23956 		cp_parser_check_for_definition_in_return_type
23957 					    (declarator, decl_specifiers.type,
23958 					     decl_specifiers.locations[ds_type_spec]);
23959 
23960 	      /* Look for an asm-specification.  */
23961 	      asm_specification = cp_parser_asm_specification_opt (parser);
23962 	      /* Look for attributes that apply to the declaration.  */
23963 	      attributes = cp_parser_attributes_opt (parser);
23964 	      /* Remember which attributes are prefix attributes and
23965 		 which are not.  */
23966 	      first_attribute = attributes;
23967 	      /* Combine the attributes.  */
23968 	      attributes = attr_chainon (prefix_attributes, attributes);
23969 
23970 	      /* If it's an `=', then we have a constant-initializer or a
23971 		 pure-specifier.  It is not correct to parse the
23972 		 initializer before registering the member declaration
23973 		 since the member declaration should be in scope while
23974 		 its initializer is processed.  However, the rest of the
23975 		 front end does not yet provide an interface that allows
23976 		 us to handle this correctly.  */
23977 	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23978 		{
23979 		  /* In [class.mem]:
23980 
23981 		     A pure-specifier shall be used only in the declaration of
23982 		     a virtual function.
23983 
23984 		     A member-declarator can contain a constant-initializer
23985 		     only if it declares a static member of integral or
23986 		     enumeration type.
23987 
23988 		     Therefore, if the DECLARATOR is for a function, we look
23989 		     for a pure-specifier; otherwise, we look for a
23990 		     constant-initializer.  When we call `grokfield', it will
23991 		     perform more stringent semantics checks.  */
23992 		  initializer_token_start = cp_lexer_peek_token (parser->lexer);
23993 		  if (function_declarator_p (declarator)
23994 		      || (decl_specifiers.type
23995 			  && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23996 			  && declarator->kind == cdk_id
23997 			  && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23998 			      == FUNCTION_TYPE)))
23999 		    initializer = cp_parser_pure_specifier (parser);
24000 		  else if (decl_specifiers.storage_class != sc_static)
24001 		    initializer = cp_parser_save_nsdmi (parser);
24002 		  else if (cxx_dialect >= cxx11)
24003 		    {
24004 		      bool nonconst;
24005 		      /* Don't require a constant rvalue in C++11, since we
24006 			 might want a reference constant.  We'll enforce
24007 		         constancy later.  */
24008 		      cp_lexer_consume_token (parser->lexer);
24009 		      /* Parse the initializer.  */
24010 		      initializer = cp_parser_initializer_clause (parser,
24011 								  &nonconst);
24012 		    }
24013 		  else
24014 		    /* Parse the initializer.  */
24015 		    initializer = cp_parser_constant_initializer (parser);
24016 		}
24017 	      else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24018 		       && !function_declarator_p (declarator))
24019 		{
24020 		  bool x;
24021 		  if (decl_specifiers.storage_class != sc_static)
24022 		    initializer = cp_parser_save_nsdmi (parser);
24023 		  else
24024 		    initializer = cp_parser_initializer (parser, &x, &x);
24025 		}
24026 	      /* Otherwise, there is no initializer.  */
24027 	      else
24028 		initializer = NULL_TREE;
24029 
24030 	      /* See if we are probably looking at a function
24031 		 definition.  We are certainly not looking at a
24032 		 member-declarator.  Calling `grokfield' has
24033 		 side-effects, so we must not do it unless we are sure
24034 		 that we are looking at a member-declarator.  */
24035 	      if (cp_parser_token_starts_function_definition_p
24036 		  (cp_lexer_peek_token (parser->lexer)))
24037 		{
24038 		  /* The grammar does not allow a pure-specifier to be
24039 		     used when a member function is defined.  (It is
24040 		     possible that this fact is an oversight in the
24041 		     standard, since a pure function may be defined
24042 		     outside of the class-specifier.  */
24043 		  if (initializer && initializer_token_start)
24044 		    error_at (initializer_token_start->location,
24045 			      "pure-specifier on function-definition");
24046 		  decl = cp_parser_save_member_function_body (parser,
24047 							      &decl_specifiers,
24048 							      declarator,
24049 							      attributes);
24050 		  if (parser->fully_implicit_function_template_p)
24051 		    decl = finish_fully_implicit_template (parser, decl);
24052 		  /* If the member was not a friend, declare it here.  */
24053 		  if (!friend_p)
24054 		    finish_member_declaration (decl);
24055 		  /* Peek at the next token.  */
24056 		  token = cp_lexer_peek_token (parser->lexer);
24057 		  /* If the next token is a semicolon, consume it.  */
24058 		  if (token->type == CPP_SEMICOLON)
24059 		    {
24060 		      location_t semicolon_loc
24061 			= cp_lexer_consume_token (parser->lexer)->location;
24062 		      gcc_rich_location richloc (semicolon_loc);
24063 		      richloc.add_fixit_remove ();
24064 		      warning_at (&richloc, OPT_Wextra_semi,
24065 				  "extra %<;%> after in-class "
24066 				  "function definition");
24067 		    }
24068 		  goto out;
24069 		}
24070 	      else
24071 		if (declarator->kind == cdk_function)
24072 		  declarator->id_loc = token->location;
24073 	      /* Create the declaration.  */
24074 	      decl = grokfield (declarator, &decl_specifiers,
24075 				initializer, /*init_const_expr_p=*/true,
24076 				asm_specification, attributes);
24077 	      if (parser->fully_implicit_function_template_p)
24078 		{
24079 		  if (friend_p)
24080 		    finish_fully_implicit_template (parser, 0);
24081 		  else
24082 		    decl = finish_fully_implicit_template (parser, decl);
24083 		}
24084 	    }
24085 
24086 	  cp_finalize_omp_declare_simd (parser, decl);
24087 	  cp_finalize_oacc_routine (parser, decl, false);
24088 
24089 	  /* Reset PREFIX_ATTRIBUTES.  */
24090 	  if (attributes != error_mark_node)
24091 	    {
24092 	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
24093 		attributes = TREE_CHAIN (attributes);
24094 	      if (attributes)
24095 		TREE_CHAIN (attributes) = NULL_TREE;
24096 	    }
24097 
24098 	  /* If there is any qualification still in effect, clear it
24099 	     now; we will be starting fresh with the next declarator.  */
24100 	  parser->scope = NULL_TREE;
24101 	  parser->qualifying_scope = NULL_TREE;
24102 	  parser->object_scope = NULL_TREE;
24103 	  /* If it's a `,', then there are more declarators.  */
24104 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24105 	    {
24106 	      cp_lexer_consume_token (parser->lexer);
24107 	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24108 		{
24109 		  cp_token *token = cp_lexer_previous_token (parser->lexer);
24110 		  gcc_rich_location richloc (token->location);
24111 		  richloc.add_fixit_remove ();
24112 		  error_at (&richloc, "stray %<,%> at end of "
24113 			    "member declaration");
24114 		}
24115 	    }
24116 	  /* If the next token isn't a `;', then we have a parse error.  */
24117 	  else if (cp_lexer_next_token_is_not (parser->lexer,
24118 					       CPP_SEMICOLON))
24119 	    {
24120 	      /* The next token might be a ways away from where the
24121 		 actual semicolon is missing.  Find the previous token
24122 		 and use that for our error position.  */
24123 	      cp_token *token = cp_lexer_previous_token (parser->lexer);
24124 	      gcc_rich_location richloc (token->location);
24125 	      richloc.add_fixit_insert_after (";");
24126 	      error_at (&richloc, "expected %<;%> at end of "
24127 			"member declaration");
24128 
24129 	      /* Assume that the user meant to provide a semicolon.  If
24130 		 we were to cp_parser_skip_to_end_of_statement, we might
24131 		 skip to a semicolon inside a member function definition
24132 		 and issue nonsensical error messages.  */
24133 	      assume_semicolon = true;
24134 	    }
24135 
24136 	  if (decl)
24137 	    {
24138 	      /* Add DECL to the list of members.  */
24139 	      if (!friend_p
24140 		  /* Explicitly include, eg, NSDMIs, for better error
24141 		     recovery (c++/58650).  */
24142 		  || !DECL_DECLARES_FUNCTION_P (decl))
24143 		finish_member_declaration (decl);
24144 
24145 	      if (TREE_CODE (decl) == FUNCTION_DECL)
24146 		cp_parser_save_default_args (parser, decl);
24147 	      else if (TREE_CODE (decl) == FIELD_DECL
24148 		       && DECL_INITIAL (decl))
24149 		/* Add DECL to the queue of NSDMI to be parsed later.  */
24150 		vec_safe_push (unparsed_nsdmis, decl);
24151 	    }
24152 
24153 	  if (assume_semicolon)
24154 	    goto out;
24155 	}
24156     }
24157 
24158   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24159  out:
24160   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24161 }
24162 
24163 /* Parse a pure-specifier.
24164 
24165    pure-specifier:
24166      = 0
24167 
24168    Returns INTEGER_ZERO_NODE if a pure specifier is found.
24169    Otherwise, ERROR_MARK_NODE is returned.  */
24170 
24171 static tree
cp_parser_pure_specifier(cp_parser * parser)24172 cp_parser_pure_specifier (cp_parser* parser)
24173 {
24174   cp_token *token;
24175 
24176   /* Look for the `=' token.  */
24177   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24178     return error_mark_node;
24179   /* Look for the `0' token.  */
24180   token = cp_lexer_peek_token (parser->lexer);
24181 
24182   if (token->type == CPP_EOF
24183       || token->type == CPP_PRAGMA_EOL)
24184     return error_mark_node;
24185 
24186   cp_lexer_consume_token (parser->lexer);
24187 
24188   /* Accept = default or = delete in c++0x mode.  */
24189   if (token->keyword == RID_DEFAULT
24190       || token->keyword == RID_DELETE)
24191     {
24192       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24193       return token->u.value;
24194     }
24195 
24196   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
24197   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24198     {
24199       cp_parser_error (parser,
24200 		       "invalid pure specifier (only %<= 0%> is allowed)");
24201       cp_parser_skip_to_end_of_statement (parser);
24202       return error_mark_node;
24203     }
24204   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24205     {
24206       error_at (token->location, "templates may not be %<virtual%>");
24207       return error_mark_node;
24208     }
24209 
24210   return integer_zero_node;
24211 }
24212 
24213 /* Parse a constant-initializer.
24214 
24215    constant-initializer:
24216      = constant-expression
24217 
24218    Returns a representation of the constant-expression.  */
24219 
24220 static tree
cp_parser_constant_initializer(cp_parser * parser)24221 cp_parser_constant_initializer (cp_parser* parser)
24222 {
24223   /* Look for the `=' token.  */
24224   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24225     return error_mark_node;
24226 
24227   /* It is invalid to write:
24228 
24229        struct S { static const int i = { 7 }; };
24230 
24231      */
24232   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24233     {
24234       cp_parser_error (parser,
24235 		       "a brace-enclosed initializer is not allowed here");
24236       /* Consume the opening brace.  */
24237       matching_braces braces;
24238       braces.consume_open (parser);
24239       /* Skip the initializer.  */
24240       cp_parser_skip_to_closing_brace (parser);
24241       /* Look for the trailing `}'.  */
24242       braces.require_close (parser);
24243 
24244       return error_mark_node;
24245     }
24246 
24247   return cp_parser_constant_expression (parser);
24248 }
24249 
24250 /* Derived classes [gram.class.derived] */
24251 
24252 /* Parse a base-clause.
24253 
24254    base-clause:
24255      : base-specifier-list
24256 
24257    base-specifier-list:
24258      base-specifier ... [opt]
24259      base-specifier-list , base-specifier ... [opt]
24260 
24261    Returns a TREE_LIST representing the base-classes, in the order in
24262    which they were declared.  The representation of each node is as
24263    described by cp_parser_base_specifier.
24264 
24265    In the case that no bases are specified, this function will return
24266    NULL_TREE, not ERROR_MARK_NODE.  */
24267 
24268 static tree
cp_parser_base_clause(cp_parser * parser)24269 cp_parser_base_clause (cp_parser* parser)
24270 {
24271   tree bases = NULL_TREE;
24272 
24273   /* Look for the `:' that begins the list.  */
24274   cp_parser_require (parser, CPP_COLON, RT_COLON);
24275 
24276   /* Scan the base-specifier-list.  */
24277   while (true)
24278     {
24279       cp_token *token;
24280       tree base;
24281       bool pack_expansion_p = false;
24282 
24283       /* Look for the base-specifier.  */
24284       base = cp_parser_base_specifier (parser);
24285       /* Look for the (optional) ellipsis. */
24286       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24287         {
24288           /* Consume the `...'. */
24289           cp_lexer_consume_token (parser->lexer);
24290 
24291           pack_expansion_p = true;
24292         }
24293 
24294       /* Add BASE to the front of the list.  */
24295       if (base && base != error_mark_node)
24296 	{
24297           if (pack_expansion_p)
24298             /* Make this a pack expansion type. */
24299             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24300 
24301           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24302             {
24303               TREE_CHAIN (base) = bases;
24304               bases = base;
24305             }
24306 	}
24307       /* Peek at the next token.  */
24308       token = cp_lexer_peek_token (parser->lexer);
24309       /* If it's not a comma, then the list is complete.  */
24310       if (token->type != CPP_COMMA)
24311 	break;
24312       /* Consume the `,'.  */
24313       cp_lexer_consume_token (parser->lexer);
24314     }
24315 
24316   /* PARSER->SCOPE may still be non-NULL at this point, if the last
24317      base class had a qualified name.  However, the next name that
24318      appears is certainly not qualified.  */
24319   parser->scope = NULL_TREE;
24320   parser->qualifying_scope = NULL_TREE;
24321   parser->object_scope = NULL_TREE;
24322 
24323   return nreverse (bases);
24324 }
24325 
24326 /* Parse a base-specifier.
24327 
24328    base-specifier:
24329      :: [opt] nested-name-specifier [opt] class-name
24330      virtual access-specifier [opt] :: [opt] nested-name-specifier
24331        [opt] class-name
24332      access-specifier virtual [opt] :: [opt] nested-name-specifier
24333        [opt] class-name
24334 
24335    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
24336    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24337    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
24338    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
24339 
24340 static tree
cp_parser_base_specifier(cp_parser * parser)24341 cp_parser_base_specifier (cp_parser* parser)
24342 {
24343   cp_token *token;
24344   bool done = false;
24345   bool virtual_p = false;
24346   bool duplicate_virtual_error_issued_p = false;
24347   bool duplicate_access_error_issued_p = false;
24348   bool class_scope_p, template_p;
24349   tree access = access_default_node;
24350   tree type;
24351 
24352   /* Process the optional `virtual' and `access-specifier'.  */
24353   while (!done)
24354     {
24355       /* Peek at the next token.  */
24356       token = cp_lexer_peek_token (parser->lexer);
24357       /* Process `virtual'.  */
24358       switch (token->keyword)
24359 	{
24360 	case RID_VIRTUAL:
24361 	  /* If `virtual' appears more than once, issue an error.  */
24362 	  if (virtual_p && !duplicate_virtual_error_issued_p)
24363 	    {
24364 	      cp_parser_error (parser,
24365 			       "%<virtual%> specified more than once in base-specifier");
24366 	      duplicate_virtual_error_issued_p = true;
24367 	    }
24368 
24369 	  virtual_p = true;
24370 
24371 	  /* Consume the `virtual' token.  */
24372 	  cp_lexer_consume_token (parser->lexer);
24373 
24374 	  break;
24375 
24376 	case RID_PUBLIC:
24377 	case RID_PROTECTED:
24378 	case RID_PRIVATE:
24379 	  /* If more than one access specifier appears, issue an
24380 	     error.  */
24381 	  if (access != access_default_node
24382 	      && !duplicate_access_error_issued_p)
24383 	    {
24384 	      cp_parser_error (parser,
24385 			       "more than one access specifier in base-specifier");
24386 	      duplicate_access_error_issued_p = true;
24387 	    }
24388 
24389 	  access = ridpointers[(int) token->keyword];
24390 
24391 	  /* Consume the access-specifier.  */
24392 	  cp_lexer_consume_token (parser->lexer);
24393 
24394 	  break;
24395 
24396 	default:
24397 	  done = true;
24398 	  break;
24399 	}
24400     }
24401   /* It is not uncommon to see programs mechanically, erroneously, use
24402      the 'typename' keyword to denote (dependent) qualified types
24403      as base classes.  */
24404   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24405     {
24406       token = cp_lexer_peek_token (parser->lexer);
24407       if (!processing_template_decl)
24408 	error_at (token->location,
24409 		  "keyword %<typename%> not allowed outside of templates");
24410       else
24411 	error_at (token->location,
24412 		  "keyword %<typename%> not allowed in this context "
24413 		  "(the base class is implicitly a type)");
24414       cp_lexer_consume_token (parser->lexer);
24415     }
24416 
24417   /* Look for the optional `::' operator.  */
24418   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24419   /* Look for the nested-name-specifier.  The simplest way to
24420      implement:
24421 
24422        [temp.res]
24423 
24424        The keyword `typename' is not permitted in a base-specifier or
24425        mem-initializer; in these contexts a qualified name that
24426        depends on a template-parameter is implicitly assumed to be a
24427        type name.
24428 
24429      is to pretend that we have seen the `typename' keyword at this
24430      point.  */
24431   cp_parser_nested_name_specifier_opt (parser,
24432 				       /*typename_keyword_p=*/true,
24433 				       /*check_dependency_p=*/true,
24434 				       /*type_p=*/true,
24435 				       /*is_declaration=*/true);
24436   /* If the base class is given by a qualified name, assume that names
24437      we see are type names or templates, as appropriate.  */
24438   class_scope_p = (parser->scope && TYPE_P (parser->scope));
24439   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24440 
24441   if (!parser->scope
24442       && cp_lexer_next_token_is_decltype (parser->lexer))
24443     /* DR 950 allows decltype as a base-specifier.  */
24444     type = cp_parser_decltype (parser);
24445   else
24446     {
24447       /* Otherwise, look for the class-name.  */
24448       type = cp_parser_class_name (parser,
24449 				   class_scope_p,
24450 				   template_p,
24451 				   typename_type,
24452 				   /*check_dependency_p=*/true,
24453 				   /*class_head_p=*/false,
24454 				   /*is_declaration=*/true);
24455       type = TREE_TYPE (type);
24456     }
24457 
24458   if (type == error_mark_node)
24459     return error_mark_node;
24460 
24461   return finish_base_specifier (type, access, virtual_p);
24462 }
24463 
24464 /* Exception handling [gram.exception] */
24465 
24466 /* Parse an (optional) noexcept-specification.
24467 
24468    noexcept-specification:
24469      noexcept ( constant-expression ) [opt]
24470 
24471    If no noexcept-specification is present, returns NULL_TREE.
24472    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24473    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24474    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
24475    Otherwise, returns a noexcept specification unless RETURN_COND is true,
24476    in which case a boolean condition is returned instead.  */
24477 
24478 static tree
cp_parser_noexcept_specification_opt(cp_parser * parser,bool require_constexpr,bool * consumed_expr,bool return_cond)24479 cp_parser_noexcept_specification_opt (cp_parser* parser,
24480 				      bool require_constexpr,
24481 				      bool* consumed_expr,
24482 				      bool return_cond)
24483 {
24484   cp_token *token;
24485   const char *saved_message;
24486 
24487   /* Peek at the next token.  */
24488   token = cp_lexer_peek_token (parser->lexer);
24489 
24490   /* Is it a noexcept-specification?  */
24491   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24492     {
24493       tree expr;
24494       cp_lexer_consume_token (parser->lexer);
24495 
24496       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24497 	{
24498 	  matching_parens parens;
24499 	  parens.consume_open (parser);
24500 
24501 	  if (require_constexpr)
24502 	    {
24503 	      /* Types may not be defined in an exception-specification.  */
24504 	      saved_message = parser->type_definition_forbidden_message;
24505 	      parser->type_definition_forbidden_message
24506 	      = G_("types may not be defined in an exception-specification");
24507 
24508 	      expr = cp_parser_constant_expression (parser);
24509 
24510 	      /* Restore the saved message.  */
24511 	      parser->type_definition_forbidden_message = saved_message;
24512 	    }
24513 	  else
24514 	    {
24515 	      expr = cp_parser_expression (parser);
24516 	      *consumed_expr = true;
24517 	    }
24518 
24519 	  parens.require_close (parser);
24520 	}
24521       else
24522 	{
24523 	  expr = boolean_true_node;
24524 	  if (!require_constexpr)
24525 	    *consumed_expr = false;
24526 	}
24527 
24528       /* We cannot build a noexcept-spec right away because this will check
24529 	 that expr is a constexpr.  */
24530       if (!return_cond)
24531 	return build_noexcept_spec (expr, tf_warning_or_error);
24532       else
24533 	return expr;
24534     }
24535   else
24536     return NULL_TREE;
24537 }
24538 
24539 /* Parse an (optional) exception-specification.
24540 
24541    exception-specification:
24542      throw ( type-id-list [opt] )
24543 
24544    Returns a TREE_LIST representing the exception-specification.  The
24545    TREE_VALUE of each node is a type.  */
24546 
24547 static tree
cp_parser_exception_specification_opt(cp_parser * parser)24548 cp_parser_exception_specification_opt (cp_parser* parser)
24549 {
24550   cp_token *token;
24551   tree type_id_list;
24552   const char *saved_message;
24553 
24554   /* Peek at the next token.  */
24555   token = cp_lexer_peek_token (parser->lexer);
24556 
24557   /* Is it a noexcept-specification?  */
24558   type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24559 						       false);
24560   if (type_id_list != NULL_TREE)
24561     return type_id_list;
24562 
24563   /* If it's not `throw', then there's no exception-specification.  */
24564   if (!cp_parser_is_keyword (token, RID_THROW))
24565     return NULL_TREE;
24566 
24567   location_t loc = token->location;
24568 
24569   /* Consume the `throw'.  */
24570   cp_lexer_consume_token (parser->lexer);
24571 
24572   /* Look for the `('.  */
24573   matching_parens parens;
24574   parens.require_open (parser);
24575 
24576   /* Peek at the next token.  */
24577   token = cp_lexer_peek_token (parser->lexer);
24578   /* If it's not a `)', then there is a type-id-list.  */
24579   if (token->type != CPP_CLOSE_PAREN)
24580     {
24581       /* Types may not be defined in an exception-specification.  */
24582       saved_message = parser->type_definition_forbidden_message;
24583       parser->type_definition_forbidden_message
24584 	= G_("types may not be defined in an exception-specification");
24585       /* Parse the type-id-list.  */
24586       type_id_list = cp_parser_type_id_list (parser);
24587       /* Restore the saved message.  */
24588       parser->type_definition_forbidden_message = saved_message;
24589 
24590       if (cxx_dialect >= cxx17)
24591 	{
24592 	  error_at (loc, "ISO C++17 does not allow dynamic exception "
24593 			 "specifications");
24594 	  type_id_list = NULL_TREE;
24595 	}
24596       else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24597 	warning_at (loc, OPT_Wdeprecated,
24598 		    "dynamic exception specifications are deprecated in "
24599 		    "C++11");
24600     }
24601   /* In C++17, throw() is equivalent to noexcept (true).  throw()
24602      is deprecated in C++11 and above as well, but is still widely used,
24603      so don't warn about it yet.  */
24604   else if (cxx_dialect >= cxx17)
24605     type_id_list = noexcept_true_spec;
24606   else
24607     type_id_list = empty_except_spec;
24608 
24609   /* Look for the `)'.  */
24610   parens.require_close (parser);
24611 
24612   return type_id_list;
24613 }
24614 
24615 /* Parse an (optional) type-id-list.
24616 
24617    type-id-list:
24618      type-id ... [opt]
24619      type-id-list , type-id ... [opt]
24620 
24621    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
24622    in the order that the types were presented.  */
24623 
24624 static tree
cp_parser_type_id_list(cp_parser * parser)24625 cp_parser_type_id_list (cp_parser* parser)
24626 {
24627   tree types = NULL_TREE;
24628 
24629   while (true)
24630     {
24631       cp_token *token;
24632       tree type;
24633 
24634       token = cp_lexer_peek_token (parser->lexer);
24635 
24636       /* Get the next type-id.  */
24637       type = cp_parser_type_id (parser);
24638       /* Check for invalid 'auto'.  */
24639       if (flag_concepts && type_uses_auto (type))
24640 	{
24641 	  error_at (token->location,
24642 		    "invalid use of %<auto%> in exception-specification");
24643 	  type = error_mark_node;
24644 	}
24645       /* Parse the optional ellipsis. */
24646       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24647         {
24648           /* Consume the `...'. */
24649           cp_lexer_consume_token (parser->lexer);
24650 
24651           /* Turn the type into a pack expansion expression. */
24652           type = make_pack_expansion (type);
24653         }
24654       /* Add it to the list.  */
24655       types = add_exception_specifier (types, type, /*complain=*/1);
24656       /* Peek at the next token.  */
24657       token = cp_lexer_peek_token (parser->lexer);
24658       /* If it is not a `,', we are done.  */
24659       if (token->type != CPP_COMMA)
24660 	break;
24661       /* Consume the `,'.  */
24662       cp_lexer_consume_token (parser->lexer);
24663     }
24664 
24665   return nreverse (types);
24666 }
24667 
24668 /* Parse a try-block.
24669 
24670    try-block:
24671      try compound-statement handler-seq  */
24672 
24673 static tree
cp_parser_try_block(cp_parser * parser)24674 cp_parser_try_block (cp_parser* parser)
24675 {
24676   tree try_block;
24677 
24678   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24679   if (parser->in_function_body
24680       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24681     error ("%<try%> in %<constexpr%> function");
24682 
24683   try_block = begin_try_block ();
24684   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24685   finish_try_block (try_block);
24686   cp_parser_handler_seq (parser);
24687   finish_handler_sequence (try_block);
24688 
24689   return try_block;
24690 }
24691 
24692 /* Parse a function-try-block.
24693 
24694    function-try-block:
24695      try ctor-initializer [opt] function-body handler-seq  */
24696 
24697 static void
cp_parser_function_try_block(cp_parser * parser)24698 cp_parser_function_try_block (cp_parser* parser)
24699 {
24700   tree compound_stmt;
24701   tree try_block;
24702 
24703   /* Look for the `try' keyword.  */
24704   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24705     return;
24706   /* Let the rest of the front end know where we are.  */
24707   try_block = begin_function_try_block (&compound_stmt);
24708   /* Parse the function-body.  */
24709   cp_parser_ctor_initializer_opt_and_function_body
24710     (parser, /*in_function_try_block=*/true);
24711   /* We're done with the `try' part.  */
24712   finish_function_try_block (try_block);
24713   /* Parse the handlers.  */
24714   cp_parser_handler_seq (parser);
24715   /* We're done with the handlers.  */
24716   finish_function_handler_sequence (try_block, compound_stmt);
24717 }
24718 
24719 /* Parse a handler-seq.
24720 
24721    handler-seq:
24722      handler handler-seq [opt]  */
24723 
24724 static void
cp_parser_handler_seq(cp_parser * parser)24725 cp_parser_handler_seq (cp_parser* parser)
24726 {
24727   while (true)
24728     {
24729       cp_token *token;
24730 
24731       /* Parse the handler.  */
24732       cp_parser_handler (parser);
24733       /* Peek at the next token.  */
24734       token = cp_lexer_peek_token (parser->lexer);
24735       /* If it's not `catch' then there are no more handlers.  */
24736       if (!cp_parser_is_keyword (token, RID_CATCH))
24737 	break;
24738     }
24739 }
24740 
24741 /* Parse a handler.
24742 
24743    handler:
24744      catch ( exception-declaration ) compound-statement  */
24745 
24746 static void
cp_parser_handler(cp_parser * parser)24747 cp_parser_handler (cp_parser* parser)
24748 {
24749   tree handler;
24750   tree declaration;
24751 
24752   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24753   handler = begin_handler ();
24754   matching_parens parens;
24755   parens.require_open (parser);
24756   declaration = cp_parser_exception_declaration (parser);
24757   finish_handler_parms (declaration, handler);
24758   parens.require_close (parser);
24759   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24760   finish_handler (handler);
24761 }
24762 
24763 /* Parse an exception-declaration.
24764 
24765    exception-declaration:
24766      type-specifier-seq declarator
24767      type-specifier-seq abstract-declarator
24768      type-specifier-seq
24769      ...
24770 
24771    Returns a VAR_DECL for the declaration, or NULL_TREE if the
24772    ellipsis variant is used.  */
24773 
24774 static tree
cp_parser_exception_declaration(cp_parser * parser)24775 cp_parser_exception_declaration (cp_parser* parser)
24776 {
24777   cp_decl_specifier_seq type_specifiers;
24778   cp_declarator *declarator;
24779   const char *saved_message;
24780 
24781   /* If it's an ellipsis, it's easy to handle.  */
24782   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24783     {
24784       /* Consume the `...' token.  */
24785       cp_lexer_consume_token (parser->lexer);
24786       return NULL_TREE;
24787     }
24788 
24789   /* Types may not be defined in exception-declarations.  */
24790   saved_message = parser->type_definition_forbidden_message;
24791   parser->type_definition_forbidden_message
24792     = G_("types may not be defined in exception-declarations");
24793 
24794   /* Parse the type-specifier-seq.  */
24795   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24796 				/*is_trailing_return=*/false,
24797 				&type_specifiers);
24798   /* If it's a `)', then there is no declarator.  */
24799   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24800     declarator = NULL;
24801   else
24802     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24803 				       /*ctor_dtor_or_conv_p=*/NULL,
24804 				       /*parenthesized_p=*/NULL,
24805 				       /*member_p=*/false,
24806 				       /*friend_p=*/false);
24807 
24808   /* Restore the saved message.  */
24809   parser->type_definition_forbidden_message = saved_message;
24810 
24811   if (!type_specifiers.any_specifiers_p)
24812     return error_mark_node;
24813 
24814   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24815 }
24816 
24817 /* Parse a throw-expression.
24818 
24819    throw-expression:
24820      throw assignment-expression [opt]
24821 
24822    Returns a THROW_EXPR representing the throw-expression.  */
24823 
24824 static tree
cp_parser_throw_expression(cp_parser * parser)24825 cp_parser_throw_expression (cp_parser* parser)
24826 {
24827   tree expression;
24828   cp_token* token;
24829 
24830   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24831   token = cp_lexer_peek_token (parser->lexer);
24832   /* Figure out whether or not there is an assignment-expression
24833      following the "throw" keyword.  */
24834   if (token->type == CPP_COMMA
24835       || token->type == CPP_SEMICOLON
24836       || token->type == CPP_CLOSE_PAREN
24837       || token->type == CPP_CLOSE_SQUARE
24838       || token->type == CPP_CLOSE_BRACE
24839       || token->type == CPP_COLON)
24840     expression = NULL_TREE;
24841   else
24842     expression = cp_parser_assignment_expression (parser);
24843 
24844   return build_throw (expression);
24845 }
24846 
24847 /* GNU Extensions */
24848 
24849 /* Parse an (optional) asm-specification.
24850 
24851    asm-specification:
24852      asm ( string-literal )
24853 
24854    If the asm-specification is present, returns a STRING_CST
24855    corresponding to the string-literal.  Otherwise, returns
24856    NULL_TREE.  */
24857 
24858 static tree
cp_parser_asm_specification_opt(cp_parser * parser)24859 cp_parser_asm_specification_opt (cp_parser* parser)
24860 {
24861   cp_token *token;
24862   tree asm_specification;
24863 
24864   /* Peek at the next token.  */
24865   token = cp_lexer_peek_token (parser->lexer);
24866   /* If the next token isn't the `asm' keyword, then there's no
24867      asm-specification.  */
24868   if (!cp_parser_is_keyword (token, RID_ASM))
24869     return NULL_TREE;
24870 
24871   /* Consume the `asm' token.  */
24872   cp_lexer_consume_token (parser->lexer);
24873   /* Look for the `('.  */
24874   matching_parens parens;
24875   parens.require_open (parser);
24876 
24877   /* Look for the string-literal.  */
24878   asm_specification = cp_parser_string_literal (parser, false, false);
24879 
24880   /* Look for the `)'.  */
24881   parens.require_close (parser);
24882 
24883   return asm_specification;
24884 }
24885 
24886 /* Parse an asm-operand-list.
24887 
24888    asm-operand-list:
24889      asm-operand
24890      asm-operand-list , asm-operand
24891 
24892    asm-operand:
24893      string-literal ( expression )
24894      [ string-literal ] string-literal ( expression )
24895 
24896    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
24897    each node is the expression.  The TREE_PURPOSE is itself a
24898    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24899    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24900    is a STRING_CST for the string literal before the parenthesis. Returns
24901    ERROR_MARK_NODE if any of the operands are invalid.  */
24902 
24903 static tree
cp_parser_asm_operand_list(cp_parser * parser)24904 cp_parser_asm_operand_list (cp_parser* parser)
24905 {
24906   tree asm_operands = NULL_TREE;
24907   bool invalid_operands = false;
24908 
24909   while (true)
24910     {
24911       tree string_literal;
24912       tree expression;
24913       tree name;
24914 
24915       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24916 	{
24917 	  /* Consume the `[' token.  */
24918 	  cp_lexer_consume_token (parser->lexer);
24919 	  /* Read the operand name.  */
24920 	  name = cp_parser_identifier (parser);
24921 	  if (name != error_mark_node)
24922 	    name = build_string (IDENTIFIER_LENGTH (name),
24923 				 IDENTIFIER_POINTER (name));
24924 	  /* Look for the closing `]'.  */
24925 	  cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24926 	}
24927       else
24928 	name = NULL_TREE;
24929       /* Look for the string-literal.  */
24930       string_literal = cp_parser_string_literal (parser, false, false);
24931 
24932       /* Look for the `('.  */
24933       matching_parens parens;
24934       parens.require_open (parser);
24935       /* Parse the expression.  */
24936       expression = cp_parser_expression (parser);
24937       /* Look for the `)'.  */
24938       parens.require_close (parser);
24939 
24940       if (name == error_mark_node
24941 	  || string_literal == error_mark_node
24942 	  || expression == error_mark_node)
24943         invalid_operands = true;
24944 
24945       /* Add this operand to the list.  */
24946       asm_operands = tree_cons (build_tree_list (name, string_literal),
24947 				expression,
24948 				asm_operands);
24949       /* If the next token is not a `,', there are no more
24950 	 operands.  */
24951       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24952 	break;
24953       /* Consume the `,'.  */
24954       cp_lexer_consume_token (parser->lexer);
24955     }
24956 
24957   return invalid_operands ? error_mark_node : nreverse (asm_operands);
24958 }
24959 
24960 /* Parse an asm-clobber-list.
24961 
24962    asm-clobber-list:
24963      string-literal
24964      asm-clobber-list , string-literal
24965 
24966    Returns a TREE_LIST, indicating the clobbers in the order that they
24967    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
24968 
24969 static tree
cp_parser_asm_clobber_list(cp_parser * parser)24970 cp_parser_asm_clobber_list (cp_parser* parser)
24971 {
24972   tree clobbers = NULL_TREE;
24973 
24974   while (true)
24975     {
24976       tree string_literal;
24977 
24978       /* Look for the string literal.  */
24979       string_literal = cp_parser_string_literal (parser, false, false);
24980       /* Add it to the list.  */
24981       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24982       /* If the next token is not a `,', then the list is
24983 	 complete.  */
24984       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24985 	break;
24986       /* Consume the `,' token.  */
24987       cp_lexer_consume_token (parser->lexer);
24988     }
24989 
24990   return clobbers;
24991 }
24992 
24993 /* Parse an asm-label-list.
24994 
24995    asm-label-list:
24996      identifier
24997      asm-label-list , identifier
24998 
24999    Returns a TREE_LIST, indicating the labels in the order that they
25000    appeared.  The TREE_VALUE of each node is a label.  */
25001 
25002 static tree
cp_parser_asm_label_list(cp_parser * parser)25003 cp_parser_asm_label_list (cp_parser* parser)
25004 {
25005   tree labels = NULL_TREE;
25006 
25007   while (true)
25008     {
25009       tree identifier, label, name;
25010 
25011       /* Look for the identifier.  */
25012       identifier = cp_parser_identifier (parser);
25013       if (!error_operand_p (identifier))
25014         {
25015 	  label = lookup_label (identifier);
25016 	  if (TREE_CODE (label) == LABEL_DECL)
25017 	    {
25018 	      TREE_USED (label) = 1;
25019 	      check_goto (label);
25020 	      name = build_string (IDENTIFIER_LENGTH (identifier),
25021 				   IDENTIFIER_POINTER (identifier));
25022 	      labels = tree_cons (name, label, labels);
25023 	    }
25024 	}
25025       /* If the next token is not a `,', then the list is
25026 	 complete.  */
25027       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25028 	break;
25029       /* Consume the `,' token.  */
25030       cp_lexer_consume_token (parser->lexer);
25031     }
25032 
25033   return nreverse (labels);
25034 }
25035 
25036 /* Return TRUE iff the next tokens in the stream are possibly the
25037    beginning of a GNU extension attribute. */
25038 
25039 static bool
cp_next_tokens_can_be_gnu_attribute_p(cp_parser * parser)25040 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25041 {
25042   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25043 }
25044 
25045 /* Return TRUE iff the next tokens in the stream are possibly the
25046    beginning of a standard C++-11 attribute specifier.  */
25047 
25048 static bool
cp_next_tokens_can_be_std_attribute_p(cp_parser * parser)25049 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25050 {
25051   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25052 }
25053 
25054 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25055    beginning of a standard C++-11 attribute specifier.  */
25056 
25057 static bool
cp_nth_tokens_can_be_std_attribute_p(cp_parser * parser,size_t n)25058 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25059 {
25060   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25061 
25062   return (cxx_dialect >= cxx11
25063 	  && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25064 	      || (token->type == CPP_OPEN_SQUARE
25065 		  && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25066 		  && token->type == CPP_OPEN_SQUARE)));
25067 }
25068 
25069 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25070    beginning of a GNU extension attribute.  */
25071 
25072 static bool
cp_nth_tokens_can_be_gnu_attribute_p(cp_parser * parser,size_t n)25073 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25074 {
25075   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25076 
25077   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25078 }
25079 
25080 /* Return true iff the next tokens can be the beginning of either a
25081    GNU attribute list, or a standard C++11 attribute sequence.  */
25082 
25083 static bool
cp_next_tokens_can_be_attribute_p(cp_parser * parser)25084 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25085 {
25086   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25087 	  || cp_next_tokens_can_be_std_attribute_p (parser));
25088 }
25089 
25090 /* Return true iff the next Nth tokens can be the beginning of either
25091    a GNU attribute list, or a standard C++11 attribute sequence.  */
25092 
25093 static bool
cp_nth_tokens_can_be_attribute_p(cp_parser * parser,size_t n)25094 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25095 {
25096   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25097 	  || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25098 }
25099 
25100 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25101    of GNU attributes, or return NULL.  */
25102 
25103 static tree
cp_parser_attributes_opt(cp_parser * parser)25104 cp_parser_attributes_opt (cp_parser *parser)
25105 {
25106   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25107     return cp_parser_gnu_attributes_opt (parser);
25108   return cp_parser_std_attribute_spec_seq (parser);
25109 }
25110 
25111 /* Parse an (optional) series of attributes.
25112 
25113    attributes:
25114      attributes attribute
25115 
25116    attribute:
25117      __attribute__ (( attribute-list [opt] ))
25118 
25119    The return value is as for cp_parser_gnu_attribute_list.  */
25120 
25121 static tree
cp_parser_gnu_attributes_opt(cp_parser * parser)25122 cp_parser_gnu_attributes_opt (cp_parser* parser)
25123 {
25124   tree attributes = NULL_TREE;
25125 
25126   temp_override<bool> cleanup
25127     (parser->auto_is_implicit_function_template_parm_p, false);
25128 
25129   while (true)
25130     {
25131       cp_token *token;
25132       tree attribute_list;
25133       bool ok = true;
25134 
25135       /* Peek at the next token.  */
25136       token = cp_lexer_peek_token (parser->lexer);
25137       /* If it's not `__attribute__', then we're done.  */
25138       if (token->keyword != RID_ATTRIBUTE)
25139 	break;
25140 
25141       /* Consume the `__attribute__' keyword.  */
25142       cp_lexer_consume_token (parser->lexer);
25143       /* Look for the two `(' tokens.  */
25144       matching_parens outer_parens;
25145       outer_parens.require_open (parser);
25146       matching_parens inner_parens;
25147       inner_parens.require_open (parser);
25148 
25149       /* Peek at the next token.  */
25150       token = cp_lexer_peek_token (parser->lexer);
25151       if (token->type != CPP_CLOSE_PAREN)
25152 	/* Parse the attribute-list.  */
25153 	attribute_list = cp_parser_gnu_attribute_list (parser);
25154       else
25155 	/* If the next token is a `)', then there is no attribute
25156 	   list.  */
25157 	attribute_list = NULL;
25158 
25159       /* Look for the two `)' tokens.  */
25160       if (!inner_parens.require_close (parser))
25161 	ok = false;
25162       if (!outer_parens.require_close (parser))
25163 	ok = false;
25164       if (!ok)
25165 	cp_parser_skip_to_end_of_statement (parser);
25166 
25167       /* Add these new attributes to the list.  */
25168       attributes = attr_chainon (attributes, attribute_list);
25169     }
25170 
25171   return attributes;
25172 }
25173 
25174 /* Parse a GNU attribute-list.
25175 
25176    attribute-list:
25177      attribute
25178      attribute-list , attribute
25179 
25180    attribute:
25181      identifier
25182      identifier ( identifier )
25183      identifier ( identifier , expression-list )
25184      identifier ( expression-list )
25185 
25186    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
25187    to an attribute.  The TREE_PURPOSE of each node is the identifier
25188    indicating which attribute is in use.  The TREE_VALUE represents
25189    the arguments, if any.  */
25190 
25191 static tree
cp_parser_gnu_attribute_list(cp_parser * parser)25192 cp_parser_gnu_attribute_list (cp_parser* parser)
25193 {
25194   tree attribute_list = NULL_TREE;
25195   bool save_translate_strings_p = parser->translate_strings_p;
25196 
25197   parser->translate_strings_p = false;
25198   while (true)
25199     {
25200       cp_token *token;
25201       tree identifier;
25202       tree attribute;
25203 
25204       /* Look for the identifier.  We also allow keywords here; for
25205 	 example `__attribute__ ((const))' is legal.  */
25206       token = cp_lexer_peek_token (parser->lexer);
25207       if (token->type == CPP_NAME
25208 	  || token->type == CPP_KEYWORD)
25209 	{
25210 	  tree arguments = NULL_TREE;
25211 
25212 	  /* Consume the token, but save it since we need it for the
25213 	     SIMD enabled function parsing.  */
25214 	  cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25215 
25216 	  /* Save away the identifier that indicates which attribute
25217 	     this is.  */
25218 	  identifier = (token->type == CPP_KEYWORD)
25219 	    /* For keywords, use the canonical spelling, not the
25220 	       parsed identifier.  */
25221 	    ? ridpointers[(int) token->keyword]
25222 	    : id_token->u.value;
25223 
25224 	  identifier = canonicalize_attr_name (identifier);
25225 	  attribute = build_tree_list (identifier, NULL_TREE);
25226 
25227 	  /* Peek at the next token.  */
25228 	  token = cp_lexer_peek_token (parser->lexer);
25229 	  /* If it's an `(', then parse the attribute arguments.  */
25230 	  if (token->type == CPP_OPEN_PAREN)
25231 	    {
25232 	      vec<tree, va_gc> *vec;
25233 	      int attr_flag = (attribute_takes_identifier_p (identifier)
25234 			       ? id_attr : normal_attr);
25235 	      vec = cp_parser_parenthesized_expression_list
25236 		    (parser, attr_flag, /*cast_p=*/false,
25237 		    /*allow_expansion_p=*/false,
25238 		    /*non_constant_p=*/NULL);
25239 	      if (vec == NULL)
25240 		arguments = error_mark_node;
25241 	      else
25242 		{
25243 		  arguments = build_tree_list_vec (vec);
25244 		  release_tree_vector (vec);
25245 		}
25246 	      /* Save the arguments away.  */
25247 	      TREE_VALUE (attribute) = arguments;
25248 	    }
25249 
25250 	  if (arguments != error_mark_node)
25251 	    {
25252 	      /* Add this attribute to the list.  */
25253 	      TREE_CHAIN (attribute) = attribute_list;
25254 	      attribute_list = attribute;
25255 	    }
25256 
25257 	  token = cp_lexer_peek_token (parser->lexer);
25258 	}
25259       /* Now, look for more attributes.  If the next token isn't a
25260 	 `,', we're done.  */
25261       if (token->type != CPP_COMMA)
25262 	break;
25263 
25264       /* Consume the comma and keep going.  */
25265       cp_lexer_consume_token (parser->lexer);
25266     }
25267   parser->translate_strings_p = save_translate_strings_p;
25268 
25269   /* We built up the list in reverse order.  */
25270   return nreverse (attribute_list);
25271 }
25272 
25273 /*  Parse a standard C++11 attribute.
25274 
25275     The returned representation is a TREE_LIST which TREE_PURPOSE is
25276     the scoped name of the attribute, and the TREE_VALUE is its
25277     arguments list.
25278 
25279     Note that the scoped name of the attribute is itself a TREE_LIST
25280     which TREE_PURPOSE is the namespace of the attribute, and
25281     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
25282     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25283     and which TREE_PURPOSE is directly the attribute name.
25284 
25285     Clients of the attribute code should use get_attribute_namespace
25286     and get_attribute_name to get the actual namespace and name of
25287     attributes, regardless of their being GNU or C++11 attributes.
25288 
25289     attribute:
25290       attribute-token attribute-argument-clause [opt]
25291 
25292     attribute-token:
25293       identifier
25294       attribute-scoped-token
25295 
25296     attribute-scoped-token:
25297       attribute-namespace :: identifier
25298 
25299     attribute-namespace:
25300       identifier
25301 
25302     attribute-argument-clause:
25303       ( balanced-token-seq )
25304 
25305     balanced-token-seq:
25306       balanced-token [opt]
25307       balanced-token-seq balanced-token
25308 
25309     balanced-token:
25310       ( balanced-token-seq )
25311       [ balanced-token-seq ]
25312       { balanced-token-seq }.  */
25313 
25314 static tree
cp_parser_std_attribute(cp_parser * parser,tree attr_ns)25315 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25316 {
25317   tree attribute, attr_id = NULL_TREE, arguments;
25318   cp_token *token;
25319 
25320   temp_override<bool> cleanup
25321     (parser->auto_is_implicit_function_template_parm_p, false);
25322 
25323   /* First, parse name of the attribute, a.k.a attribute-token.  */
25324 
25325   token = cp_lexer_peek_token (parser->lexer);
25326   if (token->type == CPP_NAME)
25327     attr_id = token->u.value;
25328   else if (token->type == CPP_KEYWORD)
25329     attr_id = ridpointers[(int) token->keyword];
25330   else if (token->flags & NAMED_OP)
25331     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25332 
25333   if (attr_id == NULL_TREE)
25334     return NULL_TREE;
25335 
25336   cp_lexer_consume_token (parser->lexer);
25337 
25338   token = cp_lexer_peek_token (parser->lexer);
25339   if (token->type == CPP_SCOPE)
25340     {
25341       /* We are seeing a scoped attribute token.  */
25342 
25343       cp_lexer_consume_token (parser->lexer);
25344       if (attr_ns)
25345 	error_at (token->location, "attribute using prefix used together "
25346 				   "with scoped attribute token");
25347       attr_ns = attr_id;
25348 
25349       token = cp_lexer_consume_token (parser->lexer);
25350       if (token->type == CPP_NAME)
25351 	attr_id = token->u.value;
25352       else if (token->type == CPP_KEYWORD)
25353 	attr_id = ridpointers[(int) token->keyword];
25354       else if (token->flags & NAMED_OP)
25355 	attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25356       else
25357 	{
25358 	  error_at (token->location,
25359 		    "expected an identifier for the attribute name");
25360 	  return error_mark_node;
25361 	}
25362 
25363       attr_ns = canonicalize_attr_name (attr_ns);
25364       attr_id = canonicalize_attr_name (attr_id);
25365       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25366 				   NULL_TREE);
25367       token = cp_lexer_peek_token (parser->lexer);
25368     }
25369   else if (attr_ns)
25370     {
25371       attr_ns = canonicalize_attr_name (attr_ns);
25372       attr_id = canonicalize_attr_name (attr_id);
25373       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25374 				   NULL_TREE);
25375     }
25376   else
25377     {
25378       attr_id = canonicalize_attr_name (attr_id);
25379       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25380 				   NULL_TREE);
25381       /* C++11 noreturn attribute is equivalent to GNU's.  */
25382       if (is_attribute_p ("noreturn", attr_id))
25383 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25384       /* C++14 deprecated attribute is equivalent to GNU's.  */
25385       else if (is_attribute_p ("deprecated", attr_id))
25386 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25387       /* C++17 fallthrough attribute is equivalent to GNU's.  */
25388       else if (is_attribute_p ("fallthrough", attr_id))
25389 	TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25390       /* Transactional Memory TS optimize_for_synchronized attribute is
25391 	 equivalent to GNU transaction_callable.  */
25392       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25393 	TREE_PURPOSE (attribute)
25394 	  = get_identifier ("transaction_callable");
25395       /* Transactional Memory attributes are GNU attributes.  */
25396       else if (tm_attr_to_mask (attr_id))
25397 	TREE_PURPOSE (attribute) = attr_id;
25398     }
25399 
25400   /* Now parse the optional argument clause of the attribute.  */
25401 
25402   if (token->type != CPP_OPEN_PAREN)
25403     return attribute;
25404 
25405   {
25406     vec<tree, va_gc> *vec;
25407     int attr_flag = normal_attr;
25408 
25409     if (attr_ns == get_identifier ("gnu")
25410 	&& attribute_takes_identifier_p (attr_id))
25411       /* A GNU attribute that takes an identifier in parameter.  */
25412       attr_flag = id_attr;
25413 
25414     const attribute_spec *as
25415       = lookup_attribute_spec (TREE_PURPOSE (attribute));
25416     if (as == NULL)
25417       {
25418 	/* For unknown attributes, just skip balanced tokens instead of
25419 	   trying to parse the arguments.  */
25420 	for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
25421 	  cp_lexer_consume_token (parser->lexer);
25422 	return attribute;
25423       }
25424 
25425     vec = cp_parser_parenthesized_expression_list
25426       (parser, attr_flag, /*cast_p=*/false,
25427        /*allow_expansion_p=*/true,
25428        /*non_constant_p=*/NULL);
25429     if (vec == NULL)
25430       arguments = error_mark_node;
25431     else
25432       {
25433 	arguments = build_tree_list_vec (vec);
25434 	release_tree_vector (vec);
25435       }
25436 
25437     if (arguments == error_mark_node)
25438       attribute = error_mark_node;
25439     else
25440       TREE_VALUE (attribute) = arguments;
25441   }
25442 
25443   return attribute;
25444 }
25445 
25446 /* Check that the attribute ATTRIBUTE appears at most once in the
25447    attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3)
25448    and deprecated (7.6.5).  Note that carries_dependency (7.6.4)
25449    isn't implemented yet in GCC.  */
25450 
25451 static void
cp_parser_check_std_attribute(tree attributes,tree attribute)25452 cp_parser_check_std_attribute (tree attributes, tree attribute)
25453 {
25454   if (attributes)
25455     {
25456       tree name = get_attribute_name (attribute);
25457       if (is_attribute_p ("noreturn", name)
25458 	  && lookup_attribute ("noreturn", attributes))
25459 	error ("attribute %<noreturn%> can appear at most once "
25460 	       "in an attribute-list");
25461       else if (is_attribute_p ("deprecated", name)
25462 	       && lookup_attribute ("deprecated", attributes))
25463 	error ("attribute %<deprecated%> can appear at most once "
25464 	       "in an attribute-list");
25465     }
25466 }
25467 
25468 /* Parse a list of standard C++-11 attributes.
25469 
25470    attribute-list:
25471      attribute [opt]
25472      attribute-list , attribute[opt]
25473      attribute ...
25474      attribute-list , attribute ...
25475 */
25476 
25477 static tree
cp_parser_std_attribute_list(cp_parser * parser,tree attr_ns)25478 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25479 {
25480   tree attributes = NULL_TREE, attribute = NULL_TREE;
25481   cp_token *token = NULL;
25482 
25483   while (true)
25484     {
25485       attribute = cp_parser_std_attribute (parser, attr_ns);
25486       if (attribute == error_mark_node)
25487 	break;
25488       if (attribute != NULL_TREE)
25489 	{
25490 	  cp_parser_check_std_attribute (attributes, attribute);
25491 	  TREE_CHAIN (attribute) = attributes;
25492 	  attributes = attribute;
25493 	}
25494       token = cp_lexer_peek_token (parser->lexer);
25495       if (token->type == CPP_ELLIPSIS)
25496 	{
25497 	  cp_lexer_consume_token (parser->lexer);
25498 	  if (attribute == NULL_TREE)
25499 	    error_at (token->location,
25500 		      "expected attribute before %<...%>");
25501 	  else
25502 	    {
25503 	      tree pack = make_pack_expansion (TREE_VALUE (attribute));
25504 	      if (pack == error_mark_node)
25505 		return error_mark_node;
25506 	      TREE_VALUE (attribute) = pack;
25507 	    }
25508 	  token = cp_lexer_peek_token (parser->lexer);
25509 	}
25510       if (token->type != CPP_COMMA)
25511 	break;
25512       cp_lexer_consume_token (parser->lexer);
25513     }
25514   attributes = nreverse (attributes);
25515   return attributes;
25516 }
25517 
25518 /* Parse a standard C++-11 attribute specifier.
25519 
25520    attribute-specifier:
25521      [ [ attribute-using-prefix [opt] attribute-list ] ]
25522      alignment-specifier
25523 
25524    attribute-using-prefix:
25525      using attribute-namespace :
25526 
25527    alignment-specifier:
25528      alignas ( type-id ... [opt] )
25529      alignas ( alignment-expression ... [opt] ).  */
25530 
25531 static tree
cp_parser_std_attribute_spec(cp_parser * parser)25532 cp_parser_std_attribute_spec (cp_parser *parser)
25533 {
25534   tree attributes = NULL_TREE;
25535   cp_token *token = cp_lexer_peek_token (parser->lexer);
25536 
25537   if (token->type == CPP_OPEN_SQUARE
25538       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25539     {
25540       tree attr_ns = NULL_TREE;
25541 
25542       cp_lexer_consume_token (parser->lexer);
25543       cp_lexer_consume_token (parser->lexer);
25544 
25545       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25546 	{
25547 	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
25548 	  if (token->type == CPP_NAME)
25549 	    attr_ns = token->u.value;
25550 	  else if (token->type == CPP_KEYWORD)
25551 	    attr_ns = ridpointers[(int) token->keyword];
25552 	  else if (token->flags & NAMED_OP)
25553 	    attr_ns = get_identifier (cpp_type2name (token->type,
25554 						     token->flags));
25555 	  if (attr_ns
25556 	      && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25557 	    {
25558 	      if (cxx_dialect < cxx17
25559 		  && !in_system_header_at (input_location))
25560 		pedwarn (input_location, 0,
25561 			 "attribute using prefix only available "
25562 			 "with -std=c++17 or -std=gnu++17");
25563 
25564 	      cp_lexer_consume_token (parser->lexer);
25565 	      cp_lexer_consume_token (parser->lexer);
25566 	      cp_lexer_consume_token (parser->lexer);
25567 	    }
25568 	  else
25569 	    attr_ns = NULL_TREE;
25570 	}
25571 
25572       attributes = cp_parser_std_attribute_list (parser, attr_ns);
25573 
25574       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25575 	  || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25576 	cp_parser_skip_to_end_of_statement (parser);
25577       else
25578 	/* Warn about parsing c++11 attribute in non-c++11 mode, only
25579 	   when we are sure that we have actually parsed them.  */
25580 	maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25581     }
25582   else
25583     {
25584       tree alignas_expr;
25585 
25586       /* Look for an alignment-specifier.  */
25587 
25588       token = cp_lexer_peek_token (parser->lexer);
25589 
25590       if (token->type != CPP_KEYWORD
25591 	  || token->keyword != RID_ALIGNAS)
25592 	return NULL_TREE;
25593 
25594       cp_lexer_consume_token (parser->lexer);
25595       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25596 
25597       matching_parens parens;
25598       if (!parens.require_open (parser))
25599 	return error_mark_node;
25600 
25601       cp_parser_parse_tentatively (parser);
25602       alignas_expr = cp_parser_type_id (parser);
25603 
25604       if (!cp_parser_parse_definitely (parser))
25605 	{
25606 	  alignas_expr = cp_parser_assignment_expression (parser);
25607 	  if (alignas_expr == error_mark_node)
25608 	    cp_parser_skip_to_end_of_statement (parser);
25609 	  if (alignas_expr == NULL_TREE
25610 	      || alignas_expr == error_mark_node)
25611 	    return alignas_expr;
25612 	}
25613 
25614       alignas_expr = cxx_alignas_expr (alignas_expr);
25615       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25616 
25617       /* Handle alignas (pack...).  */
25618       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25619 	{
25620 	  cp_lexer_consume_token (parser->lexer);
25621 	  alignas_expr = make_pack_expansion (alignas_expr);
25622 	}
25623 
25624       /* Something went wrong, so don't build the attribute.  */
25625       if (alignas_expr == error_mark_node)
25626 	return error_mark_node;
25627 
25628       if (!parens.require_close (parser))
25629 	return error_mark_node;
25630 
25631       /* Build the C++-11 representation of an 'aligned'
25632 	 attribute.  */
25633       attributes =
25634 	build_tree_list (build_tree_list (get_identifier ("gnu"),
25635 					  get_identifier ("aligned")),
25636 			 alignas_expr);
25637     }
25638 
25639   return attributes;
25640 }
25641 
25642 /* Parse a standard C++-11 attribute-specifier-seq.
25643 
25644    attribute-specifier-seq:
25645      attribute-specifier-seq [opt] attribute-specifier
25646  */
25647 
25648 static tree
cp_parser_std_attribute_spec_seq(cp_parser * parser)25649 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25650 {
25651   tree attr_specs = NULL_TREE;
25652   tree attr_last = NULL_TREE;
25653 
25654   while (true)
25655     {
25656       tree attr_spec = cp_parser_std_attribute_spec (parser);
25657       if (attr_spec == NULL_TREE)
25658 	break;
25659       if (attr_spec == error_mark_node)
25660 	return error_mark_node;
25661 
25662       if (attr_last)
25663 	TREE_CHAIN (attr_last) = attr_spec;
25664       else
25665 	attr_specs = attr_last = attr_spec;
25666       attr_last = tree_last (attr_last);
25667     }
25668 
25669   return attr_specs;
25670 }
25671 
25672 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25673    return index of the first token after balanced-token, or N on failure.  */
25674 
25675 static size_t
cp_parser_skip_balanced_tokens(cp_parser * parser,size_t n)25676 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25677 {
25678   size_t orig_n = n;
25679   int nparens = 0, nbraces = 0, nsquares = 0;
25680   do
25681     switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25682       {
25683       case CPP_EOF:
25684       case CPP_PRAGMA_EOL:
25685 	/* Ran out of tokens.  */
25686 	return orig_n;
25687       case CPP_OPEN_PAREN:
25688 	++nparens;
25689 	break;
25690       case CPP_OPEN_BRACE:
25691 	++nbraces;
25692 	break;
25693       case CPP_OPEN_SQUARE:
25694 	++nsquares;
25695 	break;
25696       case CPP_CLOSE_PAREN:
25697 	--nparens;
25698 	break;
25699       case CPP_CLOSE_BRACE:
25700 	--nbraces;
25701 	break;
25702       case CPP_CLOSE_SQUARE:
25703 	--nsquares;
25704 	break;
25705       default:
25706 	break;
25707       }
25708   while (nparens || nbraces || nsquares);
25709   return n;
25710 }
25711 
25712 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25713    return index of the first token after the GNU attribute tokens, or N on
25714    failure.  */
25715 
25716 static size_t
cp_parser_skip_gnu_attributes_opt(cp_parser * parser,size_t n)25717 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25718 {
25719   while (true)
25720     {
25721       if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25722 	  || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25723 	  || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25724 	break;
25725 
25726       size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25727       if (n2 == n + 2)
25728 	break;
25729       if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25730 	break;
25731       n = n2 + 1;
25732     }
25733   return n;
25734 }
25735 
25736 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25737    next token), return index of the first token after the standard C++11
25738    attribute tokens, or N on failure.  */
25739 
25740 static size_t
cp_parser_skip_std_attribute_spec_seq(cp_parser * parser,size_t n)25741 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25742 {
25743   while (true)
25744     {
25745       if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25746 	  && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25747 	{
25748 	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25749 	  if (n2 == n + 1)
25750 	    break;
25751 	  if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25752 	    break;
25753 	  n = n2 + 1;
25754 	}
25755       else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25756 	       && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25757 	{
25758 	  size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25759 	  if (n2 == n + 1)
25760 	    break;
25761 	  n = n2;
25762 	}
25763       else
25764 	break;
25765     }
25766   return n;
25767 }
25768 
25769 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25770    as the next token), return index of the first token after the attribute
25771    tokens, or N on failure.  */
25772 
25773 static size_t
cp_parser_skip_attributes_opt(cp_parser * parser,size_t n)25774 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25775 {
25776   if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25777     return cp_parser_skip_gnu_attributes_opt (parser, n);
25778   return cp_parser_skip_std_attribute_spec_seq (parser, n);
25779 }
25780 
25781 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
25782    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
25783    current value of the PEDANTIC flag, regardless of whether or not
25784    the `__extension__' keyword is present.  The caller is responsible
25785    for restoring the value of the PEDANTIC flag.  */
25786 
25787 static bool
cp_parser_extension_opt(cp_parser * parser,int * saved_pedantic)25788 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25789 {
25790   /* Save the old value of the PEDANTIC flag.  */
25791   *saved_pedantic = pedantic;
25792 
25793   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25794     {
25795       /* Consume the `__extension__' token.  */
25796       cp_lexer_consume_token (parser->lexer);
25797       /* We're not being pedantic while the `__extension__' keyword is
25798 	 in effect.  */
25799       pedantic = 0;
25800 
25801       return true;
25802     }
25803 
25804   return false;
25805 }
25806 
25807 /* Parse a label declaration.
25808 
25809    label-declaration:
25810      __label__ label-declarator-seq ;
25811 
25812    label-declarator-seq:
25813      identifier , label-declarator-seq
25814      identifier  */
25815 
25816 static void
cp_parser_label_declaration(cp_parser * parser)25817 cp_parser_label_declaration (cp_parser* parser)
25818 {
25819   /* Look for the `__label__' keyword.  */
25820   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25821 
25822   while (true)
25823     {
25824       tree identifier;
25825 
25826       /* Look for an identifier.  */
25827       identifier = cp_parser_identifier (parser);
25828       /* If we failed, stop.  */
25829       if (identifier == error_mark_node)
25830 	break;
25831       /* Declare it as a label.  */
25832       finish_label_decl (identifier);
25833       /* If the next token is a `;', stop.  */
25834       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25835 	break;
25836       /* Look for the `,' separating the label declarations.  */
25837       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25838     }
25839 
25840   /* Look for the final `;'.  */
25841   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25842 }
25843 
25844 // -------------------------------------------------------------------------- //
25845 // Requires Clause
25846 
25847 // Parse a requires clause.
25848 //
25849 //    requires-clause:
25850 //      'requires' logical-or-expression
25851 //
25852 // The required logical-or-expression must be a constant expression. Note
25853 // that we don't check that the expression is constepxr here. We defer until
25854 // we analyze constraints and then, we only check atomic constraints.
25855 static tree
cp_parser_requires_clause(cp_parser * parser)25856 cp_parser_requires_clause (cp_parser *parser)
25857 {
25858   // Parse the requires clause so that it is not automatically folded.
25859   ++processing_template_decl;
25860   tree expr = cp_parser_binary_expression (parser, false, false,
25861 					   PREC_NOT_OPERATOR, NULL);
25862   if (check_for_bare_parameter_packs (expr))
25863     expr = error_mark_node;
25864   --processing_template_decl;
25865   return expr;
25866 }
25867 
25868 // Optionally parse a requires clause:
25869 static tree
cp_parser_requires_clause_opt(cp_parser * parser)25870 cp_parser_requires_clause_opt (cp_parser *parser)
25871 {
25872   cp_token *tok = cp_lexer_peek_token (parser->lexer);
25873   if (tok->keyword != RID_REQUIRES)
25874     {
25875       if (!flag_concepts && tok->type == CPP_NAME
25876 	  && tok->u.value == ridpointers[RID_REQUIRES])
25877 	{
25878 	  error_at (cp_lexer_peek_token (parser->lexer)->location,
25879 		    "%<requires%> only available with -fconcepts");
25880 	  /* Parse and discard the requires-clause.  */
25881 	  cp_lexer_consume_token (parser->lexer);
25882 	  cp_parser_requires_clause (parser);
25883 	}
25884       return NULL_TREE;
25885     }
25886   cp_lexer_consume_token (parser->lexer);
25887   return cp_parser_requires_clause (parser);
25888 }
25889 
25890 
25891 /*---------------------------------------------------------------------------
25892                            Requires expressions
25893 ---------------------------------------------------------------------------*/
25894 
25895 /* Parse a requires expression
25896 
25897    requirement-expression:
25898        'requires' requirement-parameter-list [opt] requirement-body */
25899 static tree
cp_parser_requires_expression(cp_parser * parser)25900 cp_parser_requires_expression (cp_parser *parser)
25901 {
25902   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25903   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25904 
25905   /* A requires-expression shall appear only within a concept
25906      definition or a requires-clause.
25907 
25908      TODO: Implement this diagnostic correctly. */
25909   if (!processing_template_decl)
25910     {
25911       error_at (loc, "a requires expression cannot appear outside a template");
25912       cp_parser_skip_to_end_of_statement (parser);
25913       return error_mark_node;
25914     }
25915 
25916   tree parms, reqs;
25917   {
25918     /* Local parameters are delared as variables within the scope
25919        of the expression.  They are not visible past the end of
25920        the expression.  Expressions within the requires-expression
25921        are unevaluated.  */
25922     struct scope_sentinel
25923     {
25924       scope_sentinel ()
25925       {
25926 	++cp_unevaluated_operand;
25927 	begin_scope (sk_block, NULL_TREE);
25928       }
25929 
25930       ~scope_sentinel ()
25931       {
25932 	pop_bindings_and_leave_scope ();
25933 	--cp_unevaluated_operand;
25934       }
25935     } s;
25936 
25937     /* Parse the optional parameter list. */
25938     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25939       {
25940 	parms = cp_parser_requirement_parameter_list (parser);
25941 	if (parms == error_mark_node)
25942 	  return error_mark_node;
25943       }
25944     else
25945       parms = NULL_TREE;
25946 
25947     /* Parse the requirement body. */
25948     reqs = cp_parser_requirement_body (parser);
25949     if (reqs == error_mark_node)
25950       return error_mark_node;
25951   }
25952 
25953   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25954      the parm chain.  */
25955   grokparms (parms, &parms);
25956   return finish_requires_expr (parms, reqs);
25957 }
25958 
25959 /* Parse a parameterized requirement.
25960 
25961    requirement-parameter-list:
25962        '(' parameter-declaration-clause ')' */
25963 static tree
cp_parser_requirement_parameter_list(cp_parser * parser)25964 cp_parser_requirement_parameter_list (cp_parser *parser)
25965 {
25966   matching_parens parens;
25967   if (!parens.require_open (parser))
25968     return error_mark_node;
25969 
25970   tree parms = cp_parser_parameter_declaration_clause (parser);
25971 
25972   if (!parens.require_close (parser))
25973     return error_mark_node;
25974 
25975   return parms;
25976 }
25977 
25978 /* Parse the body of a requirement.
25979 
25980    requirement-body:
25981        '{' requirement-list '}' */
25982 static tree
cp_parser_requirement_body(cp_parser * parser)25983 cp_parser_requirement_body (cp_parser *parser)
25984 {
25985   matching_braces braces;
25986   if (!braces.require_open (parser))
25987     return error_mark_node;
25988 
25989   tree reqs = cp_parser_requirement_list (parser);
25990 
25991   if (!braces.require_close (parser))
25992     return error_mark_node;
25993 
25994   return reqs;
25995 }
25996 
25997 /* Parse a list of requirements.
25998 
25999    requirement-list:
26000        requirement
26001        requirement-list ';' requirement[opt] */
26002 static tree
cp_parser_requirement_list(cp_parser * parser)26003 cp_parser_requirement_list (cp_parser *parser)
26004 {
26005   tree result = NULL_TREE;
26006   while (true)
26007     {
26008       tree req = cp_parser_requirement (parser);
26009       if (req == error_mark_node)
26010         return error_mark_node;
26011 
26012       result = tree_cons (NULL_TREE, req, result);
26013 
26014       /* If we see a semi-colon, consume it. */
26015       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26016 	cp_lexer_consume_token (parser->lexer);
26017 
26018       /* Stop processing at the end of the list. */
26019       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26020         break;
26021     }
26022 
26023   /* Reverse the order of requirements so they are analyzed in
26024      declaration order. */
26025   return nreverse (result);
26026 }
26027 
26028 /* Parse a syntactic requirement or type requirement.
26029 
26030      requirement:
26031        simple-requirement
26032        compound-requirement
26033        type-requirement
26034        nested-requirement */
26035 static tree
cp_parser_requirement(cp_parser * parser)26036 cp_parser_requirement (cp_parser *parser)
26037 {
26038   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26039     return cp_parser_compound_requirement (parser);
26040   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26041     return cp_parser_type_requirement (parser);
26042   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26043     return cp_parser_nested_requirement (parser);
26044   else
26045     return cp_parser_simple_requirement (parser);
26046 }
26047 
26048 /* Parse a simple requirement.
26049 
26050      simple-requirement:
26051        expression ';' */
26052 static tree
cp_parser_simple_requirement(cp_parser * parser)26053 cp_parser_simple_requirement (cp_parser *parser)
26054 {
26055   tree expr = cp_parser_expression (parser, NULL, false, false);
26056   if (!expr || expr == error_mark_node)
26057     return error_mark_node;
26058 
26059   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26060     return error_mark_node;
26061 
26062   return finish_simple_requirement (expr);
26063 }
26064 
26065 /* Parse a type requirement
26066 
26067      type-requirement
26068          nested-name-specifier [opt] required-type-name ';'
26069 
26070      required-type-name:
26071          type-name
26072          'template' [opt] simple-template-id  */
26073 static tree
cp_parser_type_requirement(cp_parser * parser)26074 cp_parser_type_requirement (cp_parser *parser)
26075 {
26076   cp_lexer_consume_token (parser->lexer);
26077 
26078   // Save the scope before parsing name specifiers.
26079   tree saved_scope = parser->scope;
26080   tree saved_object_scope = parser->object_scope;
26081   tree saved_qualifying_scope = parser->qualifying_scope;
26082   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26083   cp_parser_nested_name_specifier_opt (parser,
26084                                        /*typename_keyword_p=*/true,
26085                                        /*check_dependency_p=*/false,
26086                                        /*type_p=*/true,
26087                                        /*is_declaration=*/false);
26088 
26089   tree type;
26090   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26091     {
26092       cp_lexer_consume_token (parser->lexer);
26093       type = cp_parser_template_id (parser,
26094                                     /*template_keyword_p=*/true,
26095                                     /*check_dependency=*/false,
26096                                     /*tag_type=*/none_type,
26097                                     /*is_declaration=*/false);
26098       type = make_typename_type (parser->scope, type, typename_type,
26099                                  /*complain=*/tf_error);
26100     }
26101   else
26102    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26103 
26104   if (TREE_CODE (type) == TYPE_DECL)
26105     type = TREE_TYPE (type);
26106 
26107   parser->scope = saved_scope;
26108   parser->object_scope = saved_object_scope;
26109   parser->qualifying_scope = saved_qualifying_scope;
26110 
26111   if (type == error_mark_node)
26112     cp_parser_skip_to_end_of_statement (parser);
26113 
26114   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26115     return error_mark_node;
26116   if (type == error_mark_node)
26117     return error_mark_node;
26118 
26119   return finish_type_requirement (type);
26120 }
26121 
26122 /* Parse a compound requirement
26123 
26124      compound-requirement:
26125          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26126 static tree
cp_parser_compound_requirement(cp_parser * parser)26127 cp_parser_compound_requirement (cp_parser *parser)
26128 {
26129   /* Parse an expression enclosed in '{ }'s. */
26130   matching_braces braces;
26131   if (!braces.require_open (parser))
26132     return error_mark_node;
26133 
26134   tree expr = cp_parser_expression (parser, NULL, false, false);
26135   if (!expr || expr == error_mark_node)
26136     return error_mark_node;
26137 
26138   if (!braces.require_close (parser))
26139     return error_mark_node;
26140 
26141   /* Parse the optional noexcept. */
26142   bool noexcept_p = false;
26143   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26144     {
26145       cp_lexer_consume_token (parser->lexer);
26146       noexcept_p = true;
26147     }
26148 
26149   /* Parse the optional trailing return type. */
26150   tree type = NULL_TREE;
26151   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26152     {
26153       cp_lexer_consume_token (parser->lexer);
26154       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26155       parser->in_result_type_constraint_p = true;
26156       type = cp_parser_trailing_type_id (parser);
26157       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26158       if (type == error_mark_node)
26159         return error_mark_node;
26160     }
26161 
26162   return finish_compound_requirement (expr, type, noexcept_p);
26163 }
26164 
26165 /* Parse a nested requirement. This is the same as a requires clause.
26166 
26167    nested-requirement:
26168      requires-clause */
26169 static tree
cp_parser_nested_requirement(cp_parser * parser)26170 cp_parser_nested_requirement (cp_parser *parser)
26171 {
26172   cp_lexer_consume_token (parser->lexer);
26173   tree req = cp_parser_requires_clause (parser);
26174   if (req == error_mark_node)
26175     return error_mark_node;
26176   return finish_nested_requirement (req);
26177 }
26178 
26179 /* Support Functions */
26180 
26181 /* Return the appropriate prefer_type argument for lookup_name_real based on
26182    tag_type and template_mem_access.  */
26183 
26184 static inline int
26185 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26186 {
26187   /* DR 141: When looking in the current enclosing context for a template-name
26188      after -> or ., only consider class templates.  */
26189   if (template_mem_access)
26190     return 2;
26191   switch (tag_type)
26192     {
26193     case none_type:  return 0;	// No preference.
26194     case scope_type: return 1;	// Type or namespace.
26195     default:         return 2;	// Type only.
26196     }
26197 }
26198 
26199 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26200    NAME should have one of the representations used for an
26201    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26202    is returned.  If PARSER->SCOPE is a dependent type, then a
26203    SCOPE_REF is returned.
26204 
26205    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26206    returned; the name was already resolved when the TEMPLATE_ID_EXPR
26207    was formed.  Abstractly, such entities should not be passed to this
26208    function, because they do not need to be looked up, but it is
26209    simpler to check for this special case here, rather than at the
26210    call-sites.
26211 
26212    In cases not explicitly covered above, this function returns a
26213    DECL, OVERLOAD, or baselink representing the result of the lookup.
26214    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26215    is returned.
26216 
26217    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26218    (e.g., "struct") that was used.  In that case bindings that do not
26219    refer to types are ignored.
26220 
26221    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26222    ignored.
26223 
26224    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26225    are ignored.
26226 
26227    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26228    types.
26229 
26230    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26231    TREE_LIST of candidates if name-lookup results in an ambiguity, and
26232    NULL_TREE otherwise.  */
26233 
26234 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)26235 cp_parser_lookup_name (cp_parser *parser, tree name,
26236 		       enum tag_types tag_type,
26237 		       bool is_template,
26238 		       bool is_namespace,
26239 		       bool check_dependency,
26240 		       tree *ambiguous_decls,
26241 		       location_t name_location)
26242 {
26243   tree decl;
26244   tree object_type = parser->context->object_type;
26245 
26246   /* Assume that the lookup will be unambiguous.  */
26247   if (ambiguous_decls)
26248     *ambiguous_decls = NULL_TREE;
26249 
26250   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26251      no longer valid.  Note that if we are parsing tentatively, and
26252      the parse fails, OBJECT_TYPE will be automatically restored.  */
26253   parser->context->object_type = NULL_TREE;
26254 
26255   if (name == error_mark_node)
26256     return error_mark_node;
26257 
26258   /* A template-id has already been resolved; there is no lookup to
26259      do.  */
26260   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26261     return name;
26262   if (BASELINK_P (name))
26263     {
26264       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26265 		  == TEMPLATE_ID_EXPR);
26266       return name;
26267     }
26268 
26269   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
26270      it should already have been checked to make sure that the name
26271      used matches the type being destroyed.  */
26272   if (TREE_CODE (name) == BIT_NOT_EXPR)
26273     {
26274       tree type;
26275 
26276       /* Figure out to which type this destructor applies.  */
26277       if (parser->scope)
26278 	type = parser->scope;
26279       else if (object_type)
26280 	type = object_type;
26281       else
26282 	type = current_class_type;
26283       /* If that's not a class type, there is no destructor.  */
26284       if (!type || !CLASS_TYPE_P (type))
26285 	return error_mark_node;
26286 
26287       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26288 	lazily_declare_fn (sfk_destructor, type);
26289 
26290       if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26291 	return dtor;
26292 
26293       return error_mark_node;
26294     }
26295 
26296   /* By this point, the NAME should be an ordinary identifier.  If
26297      the id-expression was a qualified name, the qualifying scope is
26298      stored in PARSER->SCOPE at this point.  */
26299   gcc_assert (identifier_p (name));
26300 
26301   /* Perform the lookup.  */
26302   if (parser->scope)
26303     {
26304       bool dependent_p;
26305 
26306       if (parser->scope == error_mark_node)
26307 	return error_mark_node;
26308 
26309       /* If the SCOPE is dependent, the lookup must be deferred until
26310 	 the template is instantiated -- unless we are explicitly
26311 	 looking up names in uninstantiated templates.  Even then, we
26312 	 cannot look up the name if the scope is not a class type; it
26313 	 might, for example, be a template type parameter.  */
26314       dependent_p = (TYPE_P (parser->scope)
26315 		     && dependent_scope_p (parser->scope));
26316       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26317 	  && dependent_p)
26318 	/* Defer lookup.  */
26319 	decl = error_mark_node;
26320       else
26321 	{
26322 	  tree pushed_scope = NULL_TREE;
26323 
26324 	  /* If PARSER->SCOPE is a dependent type, then it must be a
26325 	     class type, and we must not be checking dependencies;
26326 	     otherwise, we would have processed this lookup above.  So
26327 	     that PARSER->SCOPE is not considered a dependent base by
26328 	     lookup_member, we must enter the scope here.  */
26329 	  if (dependent_p)
26330 	    pushed_scope = push_scope (parser->scope);
26331 
26332 	  /* If the PARSER->SCOPE is a template specialization, it
26333 	     may be instantiated during name lookup.  In that case,
26334 	     errors may be issued.  Even if we rollback the current
26335 	     tentative parse, those errors are valid.  */
26336 	  decl = lookup_qualified_name (parser->scope, name,
26337 					prefer_type_arg (tag_type),
26338 					/*complain=*/true);
26339 
26340 	  /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26341 	     lookup result and the nested-name-specifier nominates a class C:
26342 	       * if the name specified after the nested-name-specifier, when
26343 	       looked up in C, is the injected-class-name of C (Clause 9), or
26344 	       * if the name specified after the nested-name-specifier is the
26345 	       same as the identifier or the simple-template-id's template-
26346 	       name in the last component of the nested-name-specifier,
26347 	     the name is instead considered to name the constructor of
26348 	     class C. [ Note: for example, the constructor is not an
26349 	     acceptable lookup result in an elaborated-type-specifier so
26350 	     the constructor would not be used in place of the
26351 	     injected-class-name. --end note ] Such a constructor name
26352 	     shall be used only in the declarator-id of a declaration that
26353 	     names a constructor or in a using-declaration.  */
26354 	  if (tag_type == none_type
26355 	      && DECL_SELF_REFERENCE_P (decl)
26356 	      && same_type_p (DECL_CONTEXT (decl), parser->scope))
26357 	    decl = lookup_qualified_name (parser->scope, ctor_identifier,
26358 					  prefer_type_arg (tag_type),
26359 					  /*complain=*/true);
26360 
26361 	  /* If we have a single function from a using decl, pull it out.  */
26362 	  if (TREE_CODE (decl) == OVERLOAD
26363 	      && !really_overloaded_fn (decl))
26364 	    decl = OVL_FUNCTION (decl);
26365 
26366 	  if (pushed_scope)
26367 	    pop_scope (pushed_scope);
26368 	}
26369 
26370       /* If the scope is a dependent type and either we deferred lookup or
26371 	 we did lookup but didn't find the name, rememeber the name.  */
26372       if (decl == error_mark_node && TYPE_P (parser->scope)
26373 	  && dependent_type_p (parser->scope))
26374 	{
26375 	  if (tag_type)
26376 	    {
26377 	      tree type;
26378 
26379 	      /* The resolution to Core Issue 180 says that `struct
26380 		 A::B' should be considered a type-name, even if `A'
26381 		 is dependent.  */
26382 	      type = make_typename_type (parser->scope, name, tag_type,
26383 					 /*complain=*/tf_error);
26384 	      if (type != error_mark_node)
26385 		decl = TYPE_NAME (type);
26386 	    }
26387 	  else if (is_template
26388 		   && (cp_parser_next_token_ends_template_argument_p (parser)
26389 		       || cp_lexer_next_token_is (parser->lexer,
26390 						  CPP_CLOSE_PAREN)))
26391 	    decl = make_unbound_class_template (parser->scope,
26392 						name, NULL_TREE,
26393 						/*complain=*/tf_error);
26394 	  else
26395 	    decl = build_qualified_name (/*type=*/NULL_TREE,
26396 					 parser->scope, name,
26397 					 is_template);
26398 	}
26399       parser->qualifying_scope = parser->scope;
26400       parser->object_scope = NULL_TREE;
26401     }
26402   else if (object_type)
26403     {
26404       /* Look up the name in the scope of the OBJECT_TYPE, unless the
26405 	 OBJECT_TYPE is not a class.  */
26406       if (CLASS_TYPE_P (object_type))
26407 	/* If the OBJECT_TYPE is a template specialization, it may
26408 	   be instantiated during name lookup.  In that case, errors
26409 	   may be issued.  Even if we rollback the current tentative
26410 	   parse, those errors are valid.  */
26411 	decl = lookup_member (object_type,
26412 			      name,
26413 			      /*protect=*/0,
26414 			      prefer_type_arg (tag_type),
26415 			      tf_warning_or_error);
26416       else
26417 	decl = NULL_TREE;
26418 
26419       if (!decl)
26420 	/* Look it up in the enclosing context.  DR 141: When looking for a
26421 	   template-name after -> or ., only consider class templates.  */
26422 	decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26423 				 /*nonclass=*/0,
26424 				 /*block_p=*/true, is_namespace, 0);
26425       if (object_type == unknown_type_node)
26426 	/* The object is type-dependent, so we can't look anything up; we used
26427 	   this to get the DR 141 behavior.  */
26428 	object_type = NULL_TREE;
26429       parser->object_scope = object_type;
26430       parser->qualifying_scope = NULL_TREE;
26431     }
26432   else
26433     {
26434       decl = lookup_name_real (name, prefer_type_arg (tag_type),
26435 			       /*nonclass=*/0,
26436 			       /*block_p=*/true, is_namespace, 0);
26437       parser->qualifying_scope = NULL_TREE;
26438       parser->object_scope = NULL_TREE;
26439     }
26440 
26441   /* If the lookup failed, let our caller know.  */
26442   if (!decl || decl == error_mark_node)
26443     return error_mark_node;
26444 
26445   /* Pull out the template from an injected-class-name (or multiple).  */
26446   if (is_template)
26447     decl = maybe_get_template_decl_from_type_decl (decl);
26448 
26449   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
26450   if (TREE_CODE (decl) == TREE_LIST)
26451     {
26452       if (ambiguous_decls)
26453 	*ambiguous_decls = decl;
26454       /* The error message we have to print is too complicated for
26455 	 cp_parser_error, so we incorporate its actions directly.  */
26456       if (!cp_parser_simulate_error (parser))
26457 	{
26458 	  error_at (name_location, "reference to %qD is ambiguous",
26459 		    name);
26460 	  print_candidates (decl);
26461 	}
26462       return error_mark_node;
26463     }
26464 
26465   gcc_assert (DECL_P (decl)
26466 	      || TREE_CODE (decl) == OVERLOAD
26467 	      || TREE_CODE (decl) == SCOPE_REF
26468 	      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26469 	      || BASELINK_P (decl));
26470 
26471   /* If we have resolved the name of a member declaration, check to
26472      see if the declaration is accessible.  When the name resolves to
26473      set of overloaded functions, accessibility is checked when
26474      overload resolution is done.
26475 
26476      During an explicit instantiation, access is not checked at all,
26477      as per [temp.explicit].  */
26478   if (DECL_P (decl))
26479     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26480 
26481   maybe_record_typedef_use (decl);
26482 
26483   return cp_expr (decl, name_location);
26484 }
26485 
26486 /* Like cp_parser_lookup_name, but for use in the typical case where
26487    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26488    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
26489 
26490 static tree
cp_parser_lookup_name_simple(cp_parser * parser,tree name,location_t location)26491 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26492 {
26493   return cp_parser_lookup_name (parser, name,
26494 				none_type,
26495 				/*is_template=*/false,
26496 				/*is_namespace=*/false,
26497 				/*check_dependency=*/true,
26498 				/*ambiguous_decls=*/NULL,
26499 				location);
26500 }
26501 
26502 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26503    the current context, return the TYPE_DECL.  If TAG_NAME_P is
26504    true, the DECL indicates the class being defined in a class-head,
26505    or declared in an elaborated-type-specifier.
26506 
26507    Otherwise, return DECL.  */
26508 
26509 static tree
cp_parser_maybe_treat_template_as_class(tree decl,bool tag_name_p)26510 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26511 {
26512   /* If the TEMPLATE_DECL is being declared as part of a class-head,
26513      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26514 
26515        struct A {
26516 	 template <typename T> struct B;
26517        };
26518 
26519        template <typename T> struct A::B {};
26520 
26521      Similarly, in an elaborated-type-specifier:
26522 
26523        namespace N { struct X{}; }
26524 
26525        struct A {
26526 	 template <typename T> friend struct N::X;
26527        };
26528 
26529      However, if the DECL refers to a class type, and we are in
26530      the scope of the class, then the name lookup automatically
26531      finds the TYPE_DECL created by build_self_reference rather
26532      than a TEMPLATE_DECL.  For example, in:
26533 
26534        template <class T> struct S {
26535 	 S s;
26536        };
26537 
26538      there is no need to handle such case.  */
26539 
26540   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26541     return DECL_TEMPLATE_RESULT (decl);
26542 
26543   return decl;
26544 }
26545 
26546 /* If too many, or too few, template-parameter lists apply to the
26547    declarator, issue an error message.  Returns TRUE if all went well,
26548    and FALSE otherwise.  */
26549 
26550 static bool
cp_parser_check_declarator_template_parameters(cp_parser * parser,cp_declarator * declarator,location_t declarator_location)26551 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26552 						cp_declarator *declarator,
26553 						location_t declarator_location)
26554 {
26555   switch (declarator->kind)
26556     {
26557     case cdk_id:
26558       {
26559 	unsigned num_templates = 0;
26560 	tree scope = declarator->u.id.qualifying_scope;
26561 	bool template_id_p = false;
26562 
26563 	if (scope)
26564 	  num_templates = num_template_headers_for_class (scope);
26565 	else if (TREE_CODE (declarator->u.id.unqualified_name)
26566 		 == TEMPLATE_ID_EXPR)
26567 	  {
26568 	    /* If the DECLARATOR has the form `X<y>' then it uses one
26569 	       additional level of template parameters.  */
26570 	    ++num_templates;
26571 	    template_id_p = true;
26572 	  }
26573 
26574 	return cp_parser_check_template_parameters
26575 	  (parser, num_templates, template_id_p, declarator_location,
26576 	   declarator);
26577       }
26578 
26579     case cdk_function:
26580     case cdk_array:
26581     case cdk_pointer:
26582     case cdk_reference:
26583     case cdk_ptrmem:
26584       return (cp_parser_check_declarator_template_parameters
26585 	      (parser, declarator->declarator, declarator_location));
26586 
26587     case cdk_decomp:
26588     case cdk_error:
26589       return true;
26590 
26591     default:
26592       gcc_unreachable ();
26593     }
26594   return false;
26595 }
26596 
26597 /* NUM_TEMPLATES were used in the current declaration.  If that is
26598    invalid, return FALSE and issue an error messages.  Otherwise,
26599    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
26600    declarator and we can print more accurate diagnostics.  */
26601 
26602 static bool
cp_parser_check_template_parameters(cp_parser * parser,unsigned num_templates,bool template_id_p,location_t location,cp_declarator * declarator)26603 cp_parser_check_template_parameters (cp_parser* parser,
26604 				     unsigned num_templates,
26605 				     bool template_id_p,
26606 				     location_t location,
26607 				     cp_declarator *declarator)
26608 {
26609   /* If there are the same number of template classes and parameter
26610      lists, that's OK.  */
26611   if (parser->num_template_parameter_lists == num_templates)
26612     return true;
26613   /* If there are more, but only one more, and the name ends in an identifier,
26614      then we are declaring a primary template.  That's OK too.  */
26615   if (!template_id_p
26616       && parser->num_template_parameter_lists == num_templates + 1)
26617     return true;
26618   /* If there are more template classes than parameter lists, we have
26619      something like:
26620 
26621        template <class T> void S<T>::R<T>::f ();  */
26622   if (parser->num_template_parameter_lists < num_templates)
26623     {
26624       if (declarator && !current_function_decl)
26625 	error_at (location, "specializing member %<%T::%E%> "
26626 		  "requires %<template<>%> syntax",
26627 		  declarator->u.id.qualifying_scope,
26628 		  declarator->u.id.unqualified_name);
26629       else if (declarator)
26630 	error_at (location, "invalid declaration of %<%T::%E%>",
26631 		  declarator->u.id.qualifying_scope,
26632 		  declarator->u.id.unqualified_name);
26633       else
26634 	error_at (location, "too few template-parameter-lists");
26635       return false;
26636     }
26637   /* Otherwise, there are too many template parameter lists.  We have
26638      something like:
26639 
26640      template <class T> template <class U> void S::f();  */
26641   error_at (location, "too many template-parameter-lists");
26642   return false;
26643 }
26644 
26645 /* Parse an optional `::' token indicating that the following name is
26646    from the global namespace.  If so, PARSER->SCOPE is set to the
26647    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26648    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26649    Returns the new value of PARSER->SCOPE, if the `::' token is
26650    present, and NULL_TREE otherwise.  */
26651 
26652 static tree
cp_parser_global_scope_opt(cp_parser * parser,bool current_scope_valid_p)26653 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26654 {
26655   cp_token *token;
26656 
26657   /* Peek at the next token.  */
26658   token = cp_lexer_peek_token (parser->lexer);
26659   /* If we're looking at a `::' token then we're starting from the
26660      global namespace, not our current location.  */
26661   if (token->type == CPP_SCOPE)
26662     {
26663       /* Consume the `::' token.  */
26664       cp_lexer_consume_token (parser->lexer);
26665       /* Set the SCOPE so that we know where to start the lookup.  */
26666       parser->scope = global_namespace;
26667       parser->qualifying_scope = global_namespace;
26668       parser->object_scope = NULL_TREE;
26669 
26670       return parser->scope;
26671     }
26672   else if (!current_scope_valid_p)
26673     {
26674       parser->scope = NULL_TREE;
26675       parser->qualifying_scope = NULL_TREE;
26676       parser->object_scope = NULL_TREE;
26677     }
26678 
26679   return NULL_TREE;
26680 }
26681 
26682 /* Returns TRUE if the upcoming token sequence is the start of a
26683    constructor declarator or C++17 deduction guide.  If FRIEND_P is true, the
26684    declarator is preceded by the `friend' specifier.  */
26685 
26686 static bool
cp_parser_constructor_declarator_p(cp_parser * parser,bool friend_p)26687 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26688 {
26689   bool constructor_p;
26690   bool outside_class_specifier_p;
26691   tree nested_name_specifier;
26692   cp_token *next_token;
26693 
26694   /* The common case is that this is not a constructor declarator, so
26695      try to avoid doing lots of work if at all possible.  It's not
26696      valid declare a constructor at function scope.  */
26697   if (parser->in_function_body)
26698     return false;
26699   /* And only certain tokens can begin a constructor declarator.  */
26700   next_token = cp_lexer_peek_token (parser->lexer);
26701   if (next_token->type != CPP_NAME
26702       && next_token->type != CPP_SCOPE
26703       && next_token->type != CPP_NESTED_NAME_SPECIFIER
26704       && next_token->type != CPP_TEMPLATE_ID)
26705     return false;
26706 
26707   /* Parse tentatively; we are going to roll back all of the tokens
26708      consumed here.  */
26709   cp_parser_parse_tentatively (parser);
26710   /* Assume that we are looking at a constructor declarator.  */
26711   constructor_p = true;
26712 
26713   /* Look for the optional `::' operator.  */
26714   cp_parser_global_scope_opt (parser,
26715 			      /*current_scope_valid_p=*/false);
26716   /* Look for the nested-name-specifier.  */
26717   nested_name_specifier
26718     = (cp_parser_nested_name_specifier_opt (parser,
26719 					    /*typename_keyword_p=*/false,
26720 					    /*check_dependency_p=*/false,
26721 					    /*type_p=*/false,
26722 					    /*is_declaration=*/false));
26723 
26724   outside_class_specifier_p = (!at_class_scope_p ()
26725 			       || !TYPE_BEING_DEFINED (current_class_type)
26726 			       || friend_p);
26727 
26728   /* Outside of a class-specifier, there must be a
26729      nested-name-specifier.  Except in C++17 mode, where we
26730      might be declaring a guiding declaration.  */
26731   if (!nested_name_specifier && outside_class_specifier_p
26732       && cxx_dialect < cxx17)
26733     constructor_p = false;
26734   else if (nested_name_specifier == error_mark_node)
26735     constructor_p = false;
26736 
26737   /* If we have a class scope, this is easy; DR 147 says that S::S always
26738      names the constructor, and no other qualified name could.  */
26739   if (constructor_p && nested_name_specifier
26740       && CLASS_TYPE_P (nested_name_specifier))
26741     {
26742       tree id = cp_parser_unqualified_id (parser,
26743 					  /*template_keyword_p=*/false,
26744 					  /*check_dependency_p=*/false,
26745 					  /*declarator_p=*/true,
26746 					  /*optional_p=*/false);
26747       if (is_overloaded_fn (id))
26748 	id = DECL_NAME (get_first_fn (id));
26749       if (!constructor_name_p (id, nested_name_specifier))
26750 	constructor_p = false;
26751     }
26752   /* If we still think that this might be a constructor-declarator,
26753      look for a class-name.  */
26754   else if (constructor_p)
26755     {
26756       /* If we have:
26757 
26758 	   template <typename T> struct S {
26759 	     S();
26760 	   };
26761 
26762 	 we must recognize that the nested `S' names a class.  */
26763       if (cxx_dialect >= cxx17)
26764 	cp_parser_parse_tentatively (parser);
26765 
26766       tree type_decl;
26767       type_decl = cp_parser_class_name (parser,
26768 					/*typename_keyword_p=*/false,
26769 					/*template_keyword_p=*/false,
26770 					none_type,
26771 					/*check_dependency_p=*/false,
26772 					/*class_head_p=*/false,
26773 					/*is_declaration=*/false);
26774 
26775       if (cxx_dialect >= cxx17
26776 	  && !cp_parser_parse_definitely (parser))
26777 	{
26778 	  type_decl = NULL_TREE;
26779 	  tree tmpl = cp_parser_template_name (parser,
26780 					       /*template_keyword*/false,
26781 					       /*check_dependency_p*/false,
26782 					       /*is_declaration*/false,
26783 					       none_type,
26784 					       /*is_identifier*/NULL);
26785 	  if (DECL_CLASS_TEMPLATE_P (tmpl)
26786 	      || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26787 	    /* It's a deduction guide, return true.  */;
26788 	  else
26789 	    cp_parser_simulate_error (parser);
26790 	}
26791 
26792       /* If there was no class-name, then this is not a constructor.
26793 	 Otherwise, if we are in a class-specifier and we aren't
26794 	 handling a friend declaration, check that its type matches
26795 	 current_class_type (c++/38313).  Note: error_mark_node
26796 	 is left alone for error recovery purposes.  */
26797       constructor_p = (!cp_parser_error_occurred (parser)
26798 		       && (outside_class_specifier_p
26799 			   || type_decl == NULL_TREE
26800 			   || type_decl == error_mark_node
26801 			   || same_type_p (current_class_type,
26802 					   TREE_TYPE (type_decl))));
26803 
26804       /* If we're still considering a constructor, we have to see a `(',
26805 	 to begin the parameter-declaration-clause, followed by either a
26806 	 `)', an `...', or a decl-specifier.  We need to check for a
26807 	 type-specifier to avoid being fooled into thinking that:
26808 
26809 	   S (f) (int);
26810 
26811 	 is a constructor.  (It is actually a function named `f' that
26812 	 takes one parameter (of type `int') and returns a value of type
26813 	 `S'.  */
26814       if (constructor_p
26815 	  && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26816 	constructor_p = false;
26817 
26818       if (constructor_p
26819 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26820 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26821 	  /* A parameter declaration begins with a decl-specifier,
26822 	     which is either the "attribute" keyword, a storage class
26823 	     specifier, or (usually) a type-specifier.  */
26824 	  && (!cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
26825 	      /* GNU attributes can actually appear both at the start of
26826 		 a parameter and parenthesized declarator.
26827 		 S (__attribute__((unused)) int);
26828 		 is a constructor, but
26829 		 S (__attribute__((unused)) foo) (int);
26830 		 is a function declaration.  */
26831 	      || (cp_parser_allow_gnu_extensions_p (parser)
26832 		  && cp_next_tokens_can_be_gnu_attribute_p (parser))))
26833 	{
26834 	  tree type;
26835 	  tree pushed_scope = NULL_TREE;
26836 	  unsigned saved_num_template_parameter_lists;
26837 
26838 	  if (cp_next_tokens_can_be_gnu_attribute_p (parser))
26839 	    {
26840 	      unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
26841 	      while (--n)
26842 		cp_lexer_consume_token (parser->lexer);
26843 	    }
26844 
26845 	  /* Names appearing in the type-specifier should be looked up
26846 	     in the scope of the class.  */
26847 	  if (current_class_type)
26848 	    type = NULL_TREE;
26849 	  else if (type_decl)
26850 	    {
26851 	      type = TREE_TYPE (type_decl);
26852 	      if (TREE_CODE (type) == TYPENAME_TYPE)
26853 		{
26854 		  type = resolve_typename_type (type,
26855 						/*only_current_p=*/false);
26856 		  if (TREE_CODE (type) == TYPENAME_TYPE)
26857 		    {
26858 		      cp_parser_abort_tentative_parse (parser);
26859 		      return false;
26860 		    }
26861 		}
26862 	      pushed_scope = push_scope (type);
26863 	    }
26864 
26865 	  /* Inside the constructor parameter list, surrounding
26866 	     template-parameter-lists do not apply.  */
26867 	  saved_num_template_parameter_lists
26868 	    = parser->num_template_parameter_lists;
26869 	  parser->num_template_parameter_lists = 0;
26870 
26871 	  /* Look for the type-specifier.  */
26872 	  cp_parser_type_specifier (parser,
26873 				    CP_PARSER_FLAGS_NONE,
26874 				    /*decl_specs=*/NULL,
26875 				    /*is_declarator=*/true,
26876 				    /*declares_class_or_enum=*/NULL,
26877 				    /*is_cv_qualifier=*/NULL);
26878 
26879 	  parser->num_template_parameter_lists
26880 	    = saved_num_template_parameter_lists;
26881 
26882 	  /* Leave the scope of the class.  */
26883 	  if (pushed_scope)
26884 	    pop_scope (pushed_scope);
26885 
26886 	  constructor_p = !cp_parser_error_occurred (parser);
26887 	}
26888     }
26889 
26890   /* We did not really want to consume any tokens.  */
26891   cp_parser_abort_tentative_parse (parser);
26892 
26893   return constructor_p;
26894 }
26895 
26896 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26897    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
26898    they must be performed once we are in the scope of the function.
26899 
26900    Returns the function defined.  */
26901 
26902 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)26903 cp_parser_function_definition_from_specifiers_and_declarator
26904   (cp_parser* parser,
26905    cp_decl_specifier_seq *decl_specifiers,
26906    tree attributes,
26907    const cp_declarator *declarator)
26908 {
26909   tree fn;
26910   bool success_p;
26911 
26912   /* Begin the function-definition.  */
26913   success_p = start_function (decl_specifiers, declarator, attributes);
26914 
26915   /* The things we're about to see are not directly qualified by any
26916      template headers we've seen thus far.  */
26917   reset_specialization ();
26918 
26919   /* If there were names looked up in the decl-specifier-seq that we
26920      did not check, check them now.  We must wait until we are in the
26921      scope of the function to perform the checks, since the function
26922      might be a friend.  */
26923   perform_deferred_access_checks (tf_warning_or_error);
26924 
26925   if (success_p)
26926     {
26927       cp_finalize_omp_declare_simd (parser, current_function_decl);
26928       parser->omp_declare_simd = NULL;
26929       cp_finalize_oacc_routine (parser, current_function_decl, true);
26930       parser->oacc_routine = NULL;
26931     }
26932 
26933   if (!success_p)
26934     {
26935       /* Skip the entire function.  */
26936       cp_parser_skip_to_end_of_block_or_statement (parser);
26937       fn = error_mark_node;
26938     }
26939   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26940     {
26941       /* Seen already, skip it.  An error message has already been output.  */
26942       cp_parser_skip_to_end_of_block_or_statement (parser);
26943       fn = current_function_decl;
26944       current_function_decl = NULL_TREE;
26945       /* If this is a function from a class, pop the nested class.  */
26946       if (current_class_name)
26947 	pop_nested_class ();
26948     }
26949   else
26950     {
26951       timevar_id_t tv;
26952       if (DECL_DECLARED_INLINE_P (current_function_decl))
26953         tv = TV_PARSE_INLINE;
26954       else
26955         tv = TV_PARSE_FUNC;
26956       timevar_push (tv);
26957       fn = cp_parser_function_definition_after_declarator (parser,
26958 							 /*inline_p=*/false);
26959       timevar_pop (tv);
26960     }
26961 
26962   return fn;
26963 }
26964 
26965 /* Parse the part of a function-definition that follows the
26966    declarator.  INLINE_P is TRUE iff this function is an inline
26967    function defined within a class-specifier.
26968 
26969    Returns the function defined.  */
26970 
26971 static tree
cp_parser_function_definition_after_declarator(cp_parser * parser,bool inline_p)26972 cp_parser_function_definition_after_declarator (cp_parser* parser,
26973 						bool inline_p)
26974 {
26975   tree fn;
26976   bool saved_in_unbraced_linkage_specification_p;
26977   bool saved_in_function_body;
26978   unsigned saved_num_template_parameter_lists;
26979   cp_token *token;
26980   bool fully_implicit_function_template_p
26981     = parser->fully_implicit_function_template_p;
26982   parser->fully_implicit_function_template_p = false;
26983   tree implicit_template_parms
26984     = parser->implicit_template_parms;
26985   parser->implicit_template_parms = 0;
26986   cp_binding_level* implicit_template_scope
26987     = parser->implicit_template_scope;
26988   parser->implicit_template_scope = 0;
26989 
26990   saved_in_function_body = parser->in_function_body;
26991   parser->in_function_body = true;
26992   /* If the next token is `return', then the code may be trying to
26993      make use of the "named return value" extension that G++ used to
26994      support.  */
26995   token = cp_lexer_peek_token (parser->lexer);
26996   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26997     {
26998       /* Consume the `return' keyword.  */
26999       cp_lexer_consume_token (parser->lexer);
27000       /* Look for the identifier that indicates what value is to be
27001 	 returned.  */
27002       cp_parser_identifier (parser);
27003       /* Issue an error message.  */
27004       error_at (token->location,
27005 		"named return values are no longer supported");
27006       /* Skip tokens until we reach the start of the function body.  */
27007       while (true)
27008 	{
27009 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
27010 	  if (token->type == CPP_OPEN_BRACE
27011 	      || token->type == CPP_EOF
27012 	      || token->type == CPP_PRAGMA_EOL)
27013 	    break;
27014 	  cp_lexer_consume_token (parser->lexer);
27015 	}
27016     }
27017   /* The `extern' in `extern "C" void f () { ... }' does not apply to
27018      anything declared inside `f'.  */
27019   saved_in_unbraced_linkage_specification_p
27020     = parser->in_unbraced_linkage_specification_p;
27021   parser->in_unbraced_linkage_specification_p = false;
27022   /* Inside the function, surrounding template-parameter-lists do not
27023      apply.  */
27024   saved_num_template_parameter_lists
27025     = parser->num_template_parameter_lists;
27026   parser->num_template_parameter_lists = 0;
27027 
27028   /* If the next token is `try', `__transaction_atomic', or
27029      `__transaction_relaxed`, then we are looking at either function-try-block
27030      or function-transaction-block.  Note that all of these include the
27031      function-body.  */
27032   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27033     cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27034   else if (cp_lexer_next_token_is_keyword (parser->lexer,
27035       RID_TRANSACTION_RELAXED))
27036     cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27037   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27038     cp_parser_function_try_block (parser);
27039   else
27040     cp_parser_ctor_initializer_opt_and_function_body
27041       (parser, /*in_function_try_block=*/false);
27042 
27043   /* Finish the function.  */
27044   fn = finish_function (inline_p);
27045   /* Generate code for it, if necessary.  */
27046   expand_or_defer_fn (fn);
27047   /* Restore the saved values.  */
27048   parser->in_unbraced_linkage_specification_p
27049     = saved_in_unbraced_linkage_specification_p;
27050   parser->num_template_parameter_lists
27051     = saved_num_template_parameter_lists;
27052   parser->in_function_body = saved_in_function_body;
27053 
27054   parser->fully_implicit_function_template_p
27055     = fully_implicit_function_template_p;
27056   parser->implicit_template_parms
27057     = implicit_template_parms;
27058   parser->implicit_template_scope
27059     = implicit_template_scope;
27060 
27061   if (parser->fully_implicit_function_template_p)
27062     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27063 
27064   return fn;
27065 }
27066 
27067 /* Parse a template-declaration body (following argument list).  */
27068 
27069 static void
cp_parser_template_declaration_after_parameters(cp_parser * parser,tree parameter_list,bool member_p)27070 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27071 						 tree parameter_list,
27072 						 bool member_p)
27073 {
27074   tree decl = NULL_TREE;
27075   bool friend_p = false;
27076 
27077   /* We just processed one more parameter list.  */
27078   ++parser->num_template_parameter_lists;
27079 
27080   /* Get the deferred access checks from the parameter list.  These
27081      will be checked once we know what is being declared, as for a
27082      member template the checks must be performed in the scope of the
27083      class containing the member.  */
27084   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27085 
27086   /* Tentatively parse for a new template parameter list, which can either be
27087      the template keyword or a template introduction.  */
27088   if (cp_parser_template_declaration_after_export (parser, member_p))
27089     /* OK */;
27090   else if (cxx_dialect >= cxx11
27091 	   && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27092     decl = cp_parser_alias_declaration (parser);
27093   else
27094     {
27095       /* There are no access checks when parsing a template, as we do not
27096 	 know if a specialization will be a friend.  */
27097       push_deferring_access_checks (dk_no_check);
27098       cp_token *token = cp_lexer_peek_token (parser->lexer);
27099       decl = cp_parser_single_declaration (parser,
27100 					   checks,
27101 					   member_p,
27102                                            /*explicit_specialization_p=*/false,
27103 					   &friend_p);
27104       pop_deferring_access_checks ();
27105 
27106       /* If this is a member template declaration, let the front
27107 	 end know.  */
27108       if (member_p && !friend_p && decl)
27109 	{
27110 	  if (TREE_CODE (decl) == TYPE_DECL)
27111 	    cp_parser_check_access_in_redeclaration (decl, token->location);
27112 
27113 	  decl = finish_member_template_decl (decl);
27114 	}
27115       else if (friend_p && decl
27116 	       && DECL_DECLARES_TYPE_P (decl))
27117 	make_friend_class (current_class_type, TREE_TYPE (decl),
27118 			   /*complain=*/true);
27119     }
27120   /* We are done with the current parameter list.  */
27121   --parser->num_template_parameter_lists;
27122 
27123   pop_deferring_access_checks ();
27124 
27125   /* Finish up.  */
27126   finish_template_decl (parameter_list);
27127 
27128   /* Check the template arguments for a literal operator template.  */
27129   if (decl
27130       && DECL_DECLARES_FUNCTION_P (decl)
27131       && UDLIT_OPER_P (DECL_NAME (decl)))
27132     {
27133       bool ok = true;
27134       if (parameter_list == NULL_TREE)
27135 	ok = false;
27136       else
27137 	{
27138 	  int num_parms = TREE_VEC_LENGTH (parameter_list);
27139 	  if (num_parms == 1)
27140 	    {
27141 	      tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27142 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27143 	      if (TREE_TYPE (parm) != char_type_node
27144 		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27145 		ok = false;
27146 	    }
27147 	  else if (num_parms == 2 && cxx_dialect >= cxx14)
27148 	    {
27149 	      tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27150 	      tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27151 	      tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27152 	      tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27153 	      if (parm == error_mark_node
27154 		  || TREE_TYPE (parm) != TREE_TYPE (type)
27155 		  || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27156 		ok = false;
27157 	    }
27158 	  else
27159 	    ok = false;
27160 	}
27161       if (!ok)
27162 	{
27163 	  if (cxx_dialect >= cxx14)
27164 	    error ("literal operator template %qD has invalid parameter list."
27165 		   "  Expected non-type template argument pack <char...>"
27166 		   " or <typename CharT, CharT...>",
27167 		   decl);
27168 	  else
27169 	    error ("literal operator template %qD has invalid parameter list."
27170 		   "  Expected non-type template argument pack <char...>",
27171 		   decl);
27172 	}
27173     }
27174 
27175   /* Register member declarations.  */
27176   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27177     finish_member_declaration (decl);
27178   /* If DECL is a function template, we must return to parse it later.
27179      (Even though there is no definition, there might be default
27180      arguments that need handling.)  */
27181   if (member_p && decl
27182       && DECL_DECLARES_FUNCTION_P (decl))
27183     vec_safe_push (unparsed_funs_with_definitions, decl);
27184 }
27185 
27186 /* Parse a template introduction header for a template-declaration.  Returns
27187    false if tentative parse fails.  */
27188 
27189 static bool
cp_parser_template_introduction(cp_parser * parser,bool member_p)27190 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27191 {
27192   cp_parser_parse_tentatively (parser);
27193 
27194   tree saved_scope = parser->scope;
27195   tree saved_object_scope = parser->object_scope;
27196   tree saved_qualifying_scope = parser->qualifying_scope;
27197 
27198   /* Look for the optional `::' operator.  */
27199   cp_parser_global_scope_opt (parser,
27200 			      /*current_scope_valid_p=*/false);
27201   /* Look for the nested-name-specifier.  */
27202   cp_parser_nested_name_specifier_opt (parser,
27203 				       /*typename_keyword_p=*/false,
27204 				       /*check_dependency_p=*/true,
27205 				       /*type_p=*/false,
27206 				       /*is_declaration=*/false);
27207 
27208   cp_token *token = cp_lexer_peek_token (parser->lexer);
27209   tree concept_name = cp_parser_identifier (parser);
27210 
27211   /* Look up the concept for which we will be matching
27212      template parameters.  */
27213   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27214 						 token->location);
27215   parser->scope = saved_scope;
27216   parser->object_scope = saved_object_scope;
27217   parser->qualifying_scope = saved_qualifying_scope;
27218 
27219   if (concept_name == error_mark_node)
27220     cp_parser_simulate_error (parser);
27221 
27222   /* Look for opening brace for introduction.  */
27223   matching_braces braces;
27224   braces.require_open (parser);
27225 
27226   if (!cp_parser_parse_definitely (parser))
27227     return false;
27228 
27229   push_deferring_access_checks (dk_deferred);
27230 
27231   /* Build vector of placeholder parameters and grab
27232      matching identifiers.  */
27233   tree introduction_list = cp_parser_introduction_list (parser);
27234 
27235   /* The introduction-list shall not be empty.  */
27236   int nargs = TREE_VEC_LENGTH (introduction_list);
27237   if (nargs == 0)
27238     {
27239       error ("empty introduction-list");
27240       return true;
27241     }
27242 
27243   /* Look for closing brace for introduction.  */
27244   if (!braces.require_close (parser))
27245     return true;
27246 
27247   if (tmpl_decl == error_mark_node)
27248     {
27249       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27250 				   token->location);
27251       return true;
27252     }
27253 
27254   /* Build and associate the constraint.  */
27255   tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27256   if (parms && parms != error_mark_node)
27257     {
27258       cp_parser_template_declaration_after_parameters (parser, parms,
27259 						       member_p);
27260       return true;
27261     }
27262 
27263   error_at (token->location, "no matching concept for template-introduction");
27264   return true;
27265 }
27266 
27267 /* Parse a normal template-declaration following the template keyword.  */
27268 
27269 static void
cp_parser_explicit_template_declaration(cp_parser * parser,bool member_p)27270 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27271 {
27272   tree parameter_list;
27273   bool need_lang_pop;
27274   location_t location = input_location;
27275 
27276   /* Look for the `<' token.  */
27277   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27278     return;
27279   if (at_class_scope_p () && current_function_decl)
27280     {
27281       /* 14.5.2.2 [temp.mem]
27282 
27283          A local class shall not have member templates.  */
27284       error_at (location,
27285                 "invalid declaration of member template in local class");
27286       cp_parser_skip_to_end_of_block_or_statement (parser);
27287       return;
27288     }
27289   /* [temp]
27290 
27291      A template ... shall not have C linkage.  */
27292   if (current_lang_name == lang_name_c)
27293     {
27294       error_at (location, "template with C linkage");
27295       maybe_show_extern_c_location ();
27296       /* Give it C++ linkage to avoid confusing other parts of the
27297          front end.  */
27298       push_lang_context (lang_name_cplusplus);
27299       need_lang_pop = true;
27300     }
27301   else
27302     need_lang_pop = false;
27303 
27304   /* We cannot perform access checks on the template parameter
27305      declarations until we know what is being declared, just as we
27306      cannot check the decl-specifier list.  */
27307   push_deferring_access_checks (dk_deferred);
27308 
27309   /* If the next token is `>', then we have an invalid
27310      specialization.  Rather than complain about an invalid template
27311      parameter, issue an error message here.  */
27312   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27313     {
27314       cp_parser_error (parser, "invalid explicit specialization");
27315       begin_specialization ();
27316       parameter_list = NULL_TREE;
27317     }
27318   else
27319     {
27320       /* Parse the template parameters.  */
27321       parameter_list = cp_parser_template_parameter_list (parser);
27322     }
27323 
27324   /* Look for the `>'.  */
27325   cp_parser_skip_to_end_of_template_parameter_list (parser);
27326 
27327   /* Manage template requirements */
27328   if (flag_concepts)
27329   {
27330     tree reqs = get_shorthand_constraints (current_template_parms);
27331     if (tree r = cp_parser_requires_clause_opt (parser))
27332       reqs = conjoin_constraints (reqs, normalize_expression (r));
27333     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27334   }
27335 
27336   cp_parser_template_declaration_after_parameters (parser, parameter_list,
27337 						   member_p);
27338 
27339   /* For the erroneous case of a template with C linkage, we pushed an
27340      implicit C++ linkage scope; exit that scope now.  */
27341   if (need_lang_pop)
27342     pop_lang_context ();
27343 }
27344 
27345 /* Parse a template-declaration, assuming that the `export' (and
27346    `extern') keywords, if present, has already been scanned.  MEMBER_P
27347    is as for cp_parser_template_declaration.  */
27348 
27349 static bool
cp_parser_template_declaration_after_export(cp_parser * parser,bool member_p)27350 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27351 {
27352   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27353     {
27354       cp_lexer_consume_token (parser->lexer);
27355       cp_parser_explicit_template_declaration (parser, member_p);
27356       return true;
27357     }
27358   else if (flag_concepts)
27359     return cp_parser_template_introduction (parser, member_p);
27360 
27361   return false;
27362 }
27363 
27364 /* Perform the deferred access checks from a template-parameter-list.
27365    CHECKS is a TREE_LIST of access checks, as returned by
27366    get_deferred_access_checks.  */
27367 
27368 static void
cp_parser_perform_template_parameter_access_checks(vec<deferred_access_check,va_gc> * checks)27369 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27370 {
27371   ++processing_template_parmlist;
27372   perform_access_checks (checks, tf_warning_or_error);
27373   --processing_template_parmlist;
27374 }
27375 
27376 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27377    `function-definition' sequence that follows a template header.
27378    If MEMBER_P is true, this declaration appears in a class scope.
27379 
27380    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
27381    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
27382 
27383 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)27384 cp_parser_single_declaration (cp_parser* parser,
27385 			      vec<deferred_access_check, va_gc> *checks,
27386 			      bool member_p,
27387                               bool explicit_specialization_p,
27388 			      bool* friend_p)
27389 {
27390   int declares_class_or_enum;
27391   tree decl = NULL_TREE;
27392   cp_decl_specifier_seq decl_specifiers;
27393   bool function_definition_p = false;
27394   cp_token *decl_spec_token_start;
27395 
27396   /* This function is only used when processing a template
27397      declaration.  */
27398   gcc_assert (innermost_scope_kind () == sk_template_parms
27399 	      || innermost_scope_kind () == sk_template_spec);
27400 
27401   /* Defer access checks until we know what is being declared.  */
27402   push_deferring_access_checks (dk_deferred);
27403 
27404   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27405      alternative.  */
27406   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27407   cp_parser_decl_specifier_seq (parser,
27408 				CP_PARSER_FLAGS_OPTIONAL,
27409 				&decl_specifiers,
27410 				&declares_class_or_enum);
27411   if (friend_p)
27412     *friend_p = cp_parser_friend_p (&decl_specifiers);
27413 
27414   /* There are no template typedefs.  */
27415   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27416     {
27417       error_at (decl_spec_token_start->location,
27418 		"template declaration of %<typedef%>");
27419       decl = error_mark_node;
27420     }
27421 
27422   /* Gather up the access checks that occurred the
27423      decl-specifier-seq.  */
27424   stop_deferring_access_checks ();
27425 
27426   /* Check for the declaration of a template class.  */
27427   if (declares_class_or_enum)
27428     {
27429       if (cp_parser_declares_only_class_p (parser)
27430 	  || (declares_class_or_enum & 2))
27431 	{
27432 	  // If this is a declaration, but not a definition, associate
27433 	  // any constraints with the type declaration. Constraints
27434 	  // are associated with definitions in cp_parser_class_specifier.
27435 	  if (declares_class_or_enum == 1)
27436 	    associate_classtype_constraints (decl_specifiers.type);
27437 
27438 	  decl = shadow_tag (&decl_specifiers);
27439 
27440 	  /* In this case:
27441 
27442 	       struct C {
27443 		 friend template <typename T> struct A<T>::B;
27444 	       };
27445 
27446 	     A<T>::B will be represented by a TYPENAME_TYPE, and
27447 	     therefore not recognized by shadow_tag.  */
27448 	  if (friend_p && *friend_p
27449 	      && !decl
27450 	      && decl_specifiers.type
27451 	      && TYPE_P (decl_specifiers.type))
27452 	    decl = decl_specifiers.type;
27453 
27454 	  if (decl && decl != error_mark_node)
27455 	    decl = TYPE_NAME (decl);
27456 	  else
27457 	    decl = error_mark_node;
27458 
27459 	  /* Perform access checks for template parameters.  */
27460 	  cp_parser_perform_template_parameter_access_checks (checks);
27461 
27462 	  /* Give a helpful diagnostic for
27463 	       template <class T> struct A { } a;
27464 	     if we aren't already recovering from an error.  */
27465 	  if (!cp_parser_declares_only_class_p (parser)
27466 	      && !seen_error ())
27467 	    {
27468 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
27469 			"a class template declaration must not declare "
27470 			"anything else");
27471 	      cp_parser_skip_to_end_of_block_or_statement (parser);
27472 	      goto out;
27473 	    }
27474 	}
27475     }
27476 
27477   /* Complain about missing 'typename' or other invalid type names.  */
27478   if (!decl_specifiers.any_type_specifiers_p
27479       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27480     {
27481       /* cp_parser_parse_and_diagnose_invalid_type_name calls
27482 	 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27483 	 the rest of this declaration.  */
27484       decl = error_mark_node;
27485       goto out;
27486     }
27487 
27488   /* If it's not a template class, try for a template function.  If
27489      the next token is a `;', then this declaration does not declare
27490      anything.  But, if there were errors in the decl-specifiers, then
27491      the error might well have come from an attempted class-specifier.
27492      In that case, there's no need to warn about a missing declarator.  */
27493   if (!decl
27494       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27495 	  || decl_specifiers.type != error_mark_node))
27496     {
27497       decl = cp_parser_init_declarator (parser,
27498 				        &decl_specifiers,
27499 				        checks,
27500 				        /*function_definition_allowed_p=*/true,
27501 				        member_p,
27502 				        declares_class_or_enum,
27503 				        &function_definition_p,
27504 					NULL, NULL, NULL);
27505 
27506     /* 7.1.1-1 [dcl.stc]
27507 
27508        A storage-class-specifier shall not be specified in an explicit
27509        specialization...  */
27510     if (decl
27511         && explicit_specialization_p
27512         && decl_specifiers.storage_class != sc_none)
27513       {
27514         error_at (decl_spec_token_start->location,
27515 		  "explicit template specialization cannot have a storage class");
27516         decl = error_mark_node;
27517       }
27518 
27519     if (decl && VAR_P (decl))
27520       check_template_variable (decl);
27521     }
27522 
27523   /* Look for a trailing `;' after the declaration.  */
27524   if (!function_definition_p
27525       && (decl == error_mark_node
27526 	  || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27527     cp_parser_skip_to_end_of_block_or_statement (parser);
27528 
27529  out:
27530   pop_deferring_access_checks ();
27531 
27532   /* Clear any current qualification; whatever comes next is the start
27533      of something new.  */
27534   parser->scope = NULL_TREE;
27535   parser->qualifying_scope = NULL_TREE;
27536   parser->object_scope = NULL_TREE;
27537 
27538   return decl;
27539 }
27540 
27541 /* Parse a cast-expression that is not the operand of a unary "&".  */
27542 
27543 static cp_expr
cp_parser_simple_cast_expression(cp_parser * parser)27544 cp_parser_simple_cast_expression (cp_parser *parser)
27545 {
27546   return cp_parser_cast_expression (parser, /*address_p=*/false,
27547 				    /*cast_p=*/false, /*decltype*/false, NULL);
27548 }
27549 
27550 /* Parse a functional cast to TYPE.  Returns an expression
27551    representing the cast.  */
27552 
27553 static cp_expr
cp_parser_functional_cast(cp_parser * parser,tree type)27554 cp_parser_functional_cast (cp_parser* parser, tree type)
27555 {
27556   vec<tree, va_gc> *vec;
27557   tree expression_list;
27558   cp_expr cast;
27559   bool nonconst_p;
27560 
27561   location_t start_loc = input_location;
27562 
27563   if (!type)
27564     type = error_mark_node;
27565 
27566   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27567     {
27568       cp_lexer_set_source_position (parser->lexer);
27569       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27570       expression_list = cp_parser_braced_list (parser, &nonconst_p);
27571       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27572       if (TREE_CODE (type) == TYPE_DECL)
27573 	type = TREE_TYPE (type);
27574 
27575       cast = finish_compound_literal (type, expression_list,
27576 				      tf_warning_or_error, fcl_functional);
27577       /* Create a location of the form:
27578 	    type_name{i, f}
27579 	    ^~~~~~~~~~~~~~~
27580 	 with caret == start at the start of the type name,
27581 	 finishing at the closing brace.  */
27582       location_t finish_loc
27583 	= get_finish (cp_lexer_previous_token (parser->lexer)->location);
27584       location_t combined_loc = make_location (start_loc, start_loc,
27585 					       finish_loc);
27586       cast.set_location (combined_loc);
27587       return cast;
27588    }
27589 
27590 
27591   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27592 						 /*cast_p=*/true,
27593 						 /*allow_expansion_p=*/true,
27594 						 /*non_constant_p=*/NULL);
27595   if (vec == NULL)
27596     expression_list = error_mark_node;
27597   else
27598     {
27599       expression_list = build_tree_list_vec (vec);
27600       release_tree_vector (vec);
27601     }
27602 
27603   cast = build_functional_cast (type, expression_list,
27604                                 tf_warning_or_error);
27605   /* [expr.const]/1: In an integral constant expression "only type
27606      conversions to integral or enumeration type can be used".  */
27607   if (TREE_CODE (type) == TYPE_DECL)
27608     type = TREE_TYPE (type);
27609   if (cast != error_mark_node
27610       && !cast_valid_in_integral_constant_expression_p (type)
27611       && cp_parser_non_integral_constant_expression (parser,
27612 						     NIC_CONSTRUCTOR))
27613     return error_mark_node;
27614 
27615   /* Create a location of the form:
27616        float(i)
27617        ^~~~~~~~
27618      with caret == start at the start of the type name,
27619      finishing at the closing paren.  */
27620   location_t finish_loc
27621     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27622   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27623   cast.set_location (combined_loc);
27624   return cast;
27625 }
27626 
27627 /* Save the tokens that make up the body of a member function defined
27628    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
27629    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
27630    specifiers applied to the declaration.  Returns the FUNCTION_DECL
27631    for the member function.  */
27632 
27633 static tree
cp_parser_save_member_function_body(cp_parser * parser,cp_decl_specifier_seq * decl_specifiers,cp_declarator * declarator,tree attributes)27634 cp_parser_save_member_function_body (cp_parser* parser,
27635 				     cp_decl_specifier_seq *decl_specifiers,
27636 				     cp_declarator *declarator,
27637 				     tree attributes)
27638 {
27639   cp_token *first;
27640   cp_token *last;
27641   tree fn;
27642   bool function_try_block = false;
27643 
27644   /* Create the FUNCTION_DECL.  */
27645   fn = grokmethod (decl_specifiers, declarator, attributes);
27646   cp_finalize_omp_declare_simd (parser, fn);
27647   cp_finalize_oacc_routine (parser, fn, true);
27648   /* If something went badly wrong, bail out now.  */
27649   if (fn == error_mark_node)
27650     {
27651       /* If there's a function-body, skip it.  */
27652       if (cp_parser_token_starts_function_definition_p
27653 	  (cp_lexer_peek_token (parser->lexer)))
27654 	cp_parser_skip_to_end_of_block_or_statement (parser);
27655       return error_mark_node;
27656     }
27657 
27658   /* Remember it, if there default args to post process.  */
27659   cp_parser_save_default_args (parser, fn);
27660 
27661   /* Save away the tokens that make up the body of the
27662      function.  */
27663   first = parser->lexer->next_token;
27664 
27665   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27666     cp_lexer_consume_token (parser->lexer);
27667   else if (cp_lexer_next_token_is_keyword (parser->lexer,
27668 					   RID_TRANSACTION_ATOMIC))
27669     {
27670       cp_lexer_consume_token (parser->lexer);
27671       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
27672       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27673 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27674 	  && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27675 	      || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27676 	  && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27677 	  && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27678 	{
27679 	  cp_lexer_consume_token (parser->lexer);
27680 	  cp_lexer_consume_token (parser->lexer);
27681 	  cp_lexer_consume_token (parser->lexer);
27682 	  cp_lexer_consume_token (parser->lexer);
27683 	  cp_lexer_consume_token (parser->lexer);
27684 	}
27685       else
27686 	while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27687 	       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27688 	  {
27689 	    cp_lexer_consume_token (parser->lexer);
27690 	    if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27691 	      break;
27692 	  }
27693     }
27694 
27695   /* Handle function try blocks.  */
27696   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27697     {
27698       cp_lexer_consume_token (parser->lexer);
27699       function_try_block = true;
27700     }
27701   /* We can have braced-init-list mem-initializers before the fn body.  */
27702   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27703     {
27704       cp_lexer_consume_token (parser->lexer);
27705       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27706 	{
27707 	  /* cache_group will stop after an un-nested { } pair, too.  */
27708 	  if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27709 	    break;
27710 
27711 	  /* variadic mem-inits have ... after the ')'.  */
27712 	  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27713 	    cp_lexer_consume_token (parser->lexer);
27714 	}
27715     }
27716   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27717   /* Handle function try blocks.  */
27718   if (function_try_block)
27719     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27720       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27721   last = parser->lexer->next_token;
27722 
27723   /* Save away the inline definition; we will process it when the
27724      class is complete.  */
27725   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27726   DECL_PENDING_INLINE_P (fn) = 1;
27727 
27728   /* We need to know that this was defined in the class, so that
27729      friend templates are handled correctly.  */
27730   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27731 
27732   /* Add FN to the queue of functions to be parsed later.  */
27733   vec_safe_push (unparsed_funs_with_definitions, fn);
27734 
27735   return fn;
27736 }
27737 
27738 /* Save the tokens that make up the in-class initializer for a non-static
27739    data member.  Returns a DEFAULT_ARG.  */
27740 
27741 static tree
cp_parser_save_nsdmi(cp_parser * parser)27742 cp_parser_save_nsdmi (cp_parser* parser)
27743 {
27744   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27745 }
27746 
27747 /* Parse a template-argument-list, as well as the trailing ">" (but
27748    not the opening "<").  See cp_parser_template_argument_list for the
27749    return value.  */
27750 
27751 static tree
cp_parser_enclosed_template_argument_list(cp_parser * parser)27752 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27753 {
27754   tree arguments;
27755   tree saved_scope;
27756   tree saved_qualifying_scope;
27757   tree saved_object_scope;
27758   bool saved_greater_than_is_operator_p;
27759   int saved_unevaluated_operand;
27760   int saved_inhibit_evaluation_warnings;
27761 
27762   /* [temp.names]
27763 
27764      When parsing a template-id, the first non-nested `>' is taken as
27765      the end of the template-argument-list rather than a greater-than
27766      operator.  */
27767   saved_greater_than_is_operator_p
27768     = parser->greater_than_is_operator_p;
27769   parser->greater_than_is_operator_p = false;
27770   /* Parsing the argument list may modify SCOPE, so we save it
27771      here.  */
27772   saved_scope = parser->scope;
27773   saved_qualifying_scope = parser->qualifying_scope;
27774   saved_object_scope = parser->object_scope;
27775   /* We need to evaluate the template arguments, even though this
27776      template-id may be nested within a "sizeof".  */
27777   saved_unevaluated_operand = cp_unevaluated_operand;
27778   cp_unevaluated_operand = 0;
27779   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27780   c_inhibit_evaluation_warnings = 0;
27781   /* Parse the template-argument-list itself.  */
27782   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27783       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27784     arguments = NULL_TREE;
27785   else
27786     arguments = cp_parser_template_argument_list (parser);
27787   /* Look for the `>' that ends the template-argument-list. If we find
27788      a '>>' instead, it's probably just a typo.  */
27789   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27790     {
27791       if (cxx_dialect != cxx98)
27792         {
27793           /* In C++0x, a `>>' in a template argument list or cast
27794              expression is considered to be two separate `>'
27795              tokens. So, change the current token to a `>', but don't
27796              consume it: it will be consumed later when the outer
27797              template argument list (or cast expression) is parsed.
27798              Note that this replacement of `>' for `>>' is necessary
27799              even if we are parsing tentatively: in the tentative
27800              case, after calling
27801              cp_parser_enclosed_template_argument_list we will always
27802              throw away all of the template arguments and the first
27803              closing `>', either because the template argument list
27804              was erroneous or because we are replacing those tokens
27805              with a CPP_TEMPLATE_ID token.  The second `>' (which will
27806              not have been thrown away) is needed either to close an
27807              outer template argument list or to complete a new-style
27808              cast.  */
27809 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
27810           token->type = CPP_GREATER;
27811         }
27812       else if (!saved_greater_than_is_operator_p)
27813 	{
27814 	  /* If we're in a nested template argument list, the '>>' has
27815 	    to be a typo for '> >'. We emit the error message, but we
27816 	    continue parsing and we push a '>' as next token, so that
27817 	    the argument list will be parsed correctly.  Note that the
27818 	    global source location is still on the token before the
27819 	    '>>', so we need to say explicitly where we want it.  */
27820 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
27821 	  gcc_rich_location richloc (token->location);
27822 	  richloc.add_fixit_replace ("> >");
27823 	  error_at (&richloc, "%<>>%> should be %<> >%> "
27824 		    "within a nested template argument list");
27825 
27826 	  token->type = CPP_GREATER;
27827 	}
27828       else
27829 	{
27830 	  /* If this is not a nested template argument list, the '>>'
27831 	    is a typo for '>'. Emit an error message and continue.
27832 	    Same deal about the token location, but here we can get it
27833 	    right by consuming the '>>' before issuing the diagnostic.  */
27834 	  cp_token *token = cp_lexer_consume_token (parser->lexer);
27835 	  error_at (token->location,
27836 		    "spurious %<>>%>, use %<>%> to terminate "
27837 		    "a template argument list");
27838 	}
27839     }
27840   else
27841     cp_parser_skip_to_end_of_template_parameter_list (parser);
27842   /* The `>' token might be a greater-than operator again now.  */
27843   parser->greater_than_is_operator_p
27844     = saved_greater_than_is_operator_p;
27845   /* Restore the SAVED_SCOPE.  */
27846   parser->scope = saved_scope;
27847   parser->qualifying_scope = saved_qualifying_scope;
27848   parser->object_scope = saved_object_scope;
27849   cp_unevaluated_operand = saved_unevaluated_operand;
27850   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27851 
27852   return arguments;
27853 }
27854 
27855 /* MEMBER_FUNCTION is a member function, or a friend.  If default
27856    arguments, or the body of the function have not yet been parsed,
27857    parse them now.  */
27858 
27859 static void
cp_parser_late_parsing_for_member(cp_parser * parser,tree member_function)27860 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27861 {
27862   timevar_push (TV_PARSE_INMETH);
27863   /* If this member is a template, get the underlying
27864      FUNCTION_DECL.  */
27865   if (DECL_FUNCTION_TEMPLATE_P (member_function))
27866     member_function = DECL_TEMPLATE_RESULT (member_function);
27867 
27868   /* There should not be any class definitions in progress at this
27869      point; the bodies of members are only parsed outside of all class
27870      definitions.  */
27871   gcc_assert (parser->num_classes_being_defined == 0);
27872   /* While we're parsing the member functions we might encounter more
27873      classes.  We want to handle them right away, but we don't want
27874      them getting mixed up with functions that are currently in the
27875      queue.  */
27876   push_unparsed_function_queues (parser);
27877 
27878   /* Make sure that any template parameters are in scope.  */
27879   maybe_begin_member_template_processing (member_function);
27880 
27881   /* If the body of the function has not yet been parsed, parse it
27882      now.  */
27883   if (DECL_PENDING_INLINE_P (member_function))
27884     {
27885       tree function_scope;
27886       cp_token_cache *tokens;
27887 
27888       /* The function is no longer pending; we are processing it.  */
27889       tokens = DECL_PENDING_INLINE_INFO (member_function);
27890       DECL_PENDING_INLINE_INFO (member_function) = NULL;
27891       DECL_PENDING_INLINE_P (member_function) = 0;
27892 
27893       /* If this is a local class, enter the scope of the containing
27894 	 function.  */
27895       function_scope = current_function_decl;
27896       if (function_scope)
27897 	push_function_context ();
27898 
27899       /* Push the body of the function onto the lexer stack.  */
27900       cp_parser_push_lexer_for_tokens (parser, tokens);
27901 
27902       /* Let the front end know that we going to be defining this
27903 	 function.  */
27904       start_preparsed_function (member_function, NULL_TREE,
27905 				SF_PRE_PARSED | SF_INCLASS_INLINE);
27906 
27907       /* Don't do access checking if it is a templated function.  */
27908       if (processing_template_decl)
27909 	push_deferring_access_checks (dk_no_check);
27910 
27911       /* #pragma omp declare reduction needs special parsing.  */
27912       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27913 	{
27914 	  parser->lexer->in_pragma = true;
27915 	  cp_parser_omp_declare_reduction_exprs (member_function, parser);
27916 	  finish_function (/*inline_p=*/true);
27917 	  cp_check_omp_declare_reduction (member_function);
27918 	}
27919       else
27920 	/* Now, parse the body of the function.  */
27921 	cp_parser_function_definition_after_declarator (parser,
27922 							/*inline_p=*/true);
27923 
27924       if (processing_template_decl)
27925 	pop_deferring_access_checks ();
27926 
27927       /* Leave the scope of the containing function.  */
27928       if (function_scope)
27929 	pop_function_context ();
27930       cp_parser_pop_lexer (parser);
27931     }
27932 
27933   /* Remove any template parameters from the symbol table.  */
27934   maybe_end_member_template_processing ();
27935 
27936   /* Restore the queue.  */
27937   pop_unparsed_function_queues (parser);
27938   timevar_pop (TV_PARSE_INMETH);
27939 }
27940 
27941 /* If DECL contains any default args, remember it on the unparsed
27942    functions queue.  */
27943 
27944 static void
cp_parser_save_default_args(cp_parser * parser,tree decl)27945 cp_parser_save_default_args (cp_parser* parser, tree decl)
27946 {
27947   tree probe;
27948 
27949   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27950        probe;
27951        probe = TREE_CHAIN (probe))
27952     if (TREE_PURPOSE (probe))
27953       {
27954 	cp_default_arg_entry entry = {current_class_type, decl};
27955 	vec_safe_push (unparsed_funs_with_default_args, entry);
27956 	break;
27957       }
27958 }
27959 
27960 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27961    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
27962    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
27963    from the parameter-type-list.  */
27964 
27965 static tree
cp_parser_late_parse_one_default_arg(cp_parser * parser,tree decl,tree default_arg,tree parmtype)27966 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27967 				      tree default_arg, tree parmtype)
27968 {
27969   cp_token_cache *tokens;
27970   tree parsed_arg;
27971   bool dummy;
27972 
27973   if (default_arg == error_mark_node)
27974     return error_mark_node;
27975 
27976   /* Push the saved tokens for the default argument onto the parser's
27977      lexer stack.  */
27978   tokens = DEFARG_TOKENS (default_arg);
27979   cp_parser_push_lexer_for_tokens (parser, tokens);
27980 
27981   start_lambda_scope (decl);
27982 
27983   /* Parse the default argument.  */
27984   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27985   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27986     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27987 
27988   finish_lambda_scope ();
27989 
27990   if (parsed_arg == error_mark_node)
27991     cp_parser_skip_to_end_of_statement (parser);
27992 
27993   if (!processing_template_decl)
27994     {
27995       /* In a non-template class, check conversions now.  In a template,
27996 	 we'll wait and instantiate these as needed.  */
27997       if (TREE_CODE (decl) == PARM_DECL)
27998 	parsed_arg = check_default_argument (parmtype, parsed_arg,
27999 					     tf_warning_or_error);
28000       else if (maybe_reject_flexarray_init (decl, parsed_arg))
28001 	parsed_arg = error_mark_node;
28002       else
28003 	parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28004     }
28005 
28006   /* If the token stream has not been completely used up, then
28007      there was extra junk after the end of the default
28008      argument.  */
28009   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28010     {
28011       if (TREE_CODE (decl) == PARM_DECL)
28012 	cp_parser_error (parser, "expected %<,%>");
28013       else
28014 	cp_parser_error (parser, "expected %<;%>");
28015     }
28016 
28017   /* Revert to the main lexer.  */
28018   cp_parser_pop_lexer (parser);
28019 
28020   return parsed_arg;
28021 }
28022 
28023 /* FIELD is a non-static data member with an initializer which we saved for
28024    later; parse it now.  */
28025 
28026 static void
cp_parser_late_parsing_nsdmi(cp_parser * parser,tree field)28027 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28028 {
28029   tree def;
28030 
28031   maybe_begin_member_template_processing (field);
28032 
28033   push_unparsed_function_queues (parser);
28034   def = cp_parser_late_parse_one_default_arg (parser, field,
28035 					      DECL_INITIAL (field),
28036 					      NULL_TREE);
28037   pop_unparsed_function_queues (parser);
28038 
28039   maybe_end_member_template_processing ();
28040 
28041   DECL_INITIAL (field) = def;
28042 }
28043 
28044 /* FN is a FUNCTION_DECL which may contains a parameter with an
28045    unparsed DEFAULT_ARG.  Parse the default args now.  This function
28046    assumes that the current scope is the scope in which the default
28047    argument should be processed.  */
28048 
28049 static void
cp_parser_late_parsing_default_args(cp_parser * parser,tree fn)28050 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28051 {
28052   bool saved_local_variables_forbidden_p;
28053   tree parm, parmdecl;
28054 
28055   /* While we're parsing the default args, we might (due to the
28056      statement expression extension) encounter more classes.  We want
28057      to handle them right away, but we don't want them getting mixed
28058      up with default args that are currently in the queue.  */
28059   push_unparsed_function_queues (parser);
28060 
28061   /* Local variable names (and the `this' keyword) may not appear
28062      in a default argument.  */
28063   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28064   parser->local_variables_forbidden_p = true;
28065 
28066   push_defarg_context (fn);
28067 
28068   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28069 	 parmdecl = DECL_ARGUMENTS (fn);
28070        parm && parm != void_list_node;
28071        parm = TREE_CHAIN (parm),
28072 	 parmdecl = DECL_CHAIN (parmdecl))
28073     {
28074       tree default_arg = TREE_PURPOSE (parm);
28075       tree parsed_arg;
28076       vec<tree, va_gc> *insts;
28077       tree copy;
28078       unsigned ix;
28079 
28080       if (!default_arg)
28081 	continue;
28082 
28083       if (TREE_CODE (default_arg) != DEFAULT_ARG)
28084 	/* This can happen for a friend declaration for a function
28085 	   already declared with default arguments.  */
28086 	continue;
28087 
28088       parsed_arg
28089 	= cp_parser_late_parse_one_default_arg (parser, parmdecl,
28090 						default_arg,
28091 						TREE_VALUE (parm));
28092       TREE_PURPOSE (parm) = parsed_arg;
28093 
28094       /* Update any instantiations we've already created.  */
28095       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28096 	   vec_safe_iterate (insts, ix, &copy); ix++)
28097 	TREE_PURPOSE (copy) = parsed_arg;
28098     }
28099 
28100   pop_defarg_context ();
28101 
28102   /* Make sure no default arg is missing.  */
28103   check_default_args (fn);
28104 
28105   /* Restore the state of local_variables_forbidden_p.  */
28106   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28107 
28108   /* Restore the queue.  */
28109   pop_unparsed_function_queues (parser);
28110 }
28111 
28112 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28113 
28114      sizeof ... ( identifier )
28115 
28116    where the 'sizeof' token has already been consumed.  */
28117 
28118 static tree
cp_parser_sizeof_pack(cp_parser * parser)28119 cp_parser_sizeof_pack (cp_parser *parser)
28120 {
28121   /* Consume the `...'.  */
28122   cp_lexer_consume_token (parser->lexer);
28123   maybe_warn_variadic_templates ();
28124 
28125   matching_parens parens;
28126   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28127   if (paren)
28128     parens.consume_open (parser);
28129   else
28130     permerror (cp_lexer_peek_token (parser->lexer)->location,
28131 	       "%<sizeof...%> argument must be surrounded by parentheses");
28132 
28133   cp_token *token = cp_lexer_peek_token (parser->lexer);
28134   tree name = cp_parser_identifier (parser);
28135   if (name == error_mark_node)
28136     return error_mark_node;
28137   /* The name is not qualified.  */
28138   parser->scope = NULL_TREE;
28139   parser->qualifying_scope = NULL_TREE;
28140   parser->object_scope = NULL_TREE;
28141   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28142   if (expr == error_mark_node)
28143     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28144 				 token->location);
28145   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28146     expr = TREE_TYPE (expr);
28147   else if (TREE_CODE (expr) == CONST_DECL)
28148     expr = DECL_INITIAL (expr);
28149   expr = make_pack_expansion (expr);
28150   PACK_EXPANSION_SIZEOF_P (expr) = true;
28151 
28152   if (paren)
28153     parens.require_close (parser);
28154 
28155   return expr;
28156 }
28157 
28158 /* Parse the operand of `sizeof' (or a similar operator).  Returns
28159    either a TYPE or an expression, depending on the form of the
28160    input.  The KEYWORD indicates which kind of expression we have
28161    encountered.  */
28162 
28163 static tree
cp_parser_sizeof_operand(cp_parser * parser,enum rid keyword)28164 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28165 {
28166   tree expr = NULL_TREE;
28167   const char *saved_message;
28168   char *tmp;
28169   bool saved_integral_constant_expression_p;
28170   bool saved_non_integral_constant_expression_p;
28171 
28172   /* If it's a `...', then we are computing the length of a parameter
28173      pack.  */
28174   if (keyword == RID_SIZEOF
28175       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28176     return cp_parser_sizeof_pack (parser);
28177 
28178   /* Types cannot be defined in a `sizeof' expression.  Save away the
28179      old message.  */
28180   saved_message = parser->type_definition_forbidden_message;
28181   /* And create the new one.  */
28182   tmp = concat ("types may not be defined in %<",
28183 		IDENTIFIER_POINTER (ridpointers[keyword]),
28184 		"%> expressions", NULL);
28185   parser->type_definition_forbidden_message = tmp;
28186 
28187   /* The restrictions on constant-expressions do not apply inside
28188      sizeof expressions.  */
28189   saved_integral_constant_expression_p
28190     = parser->integral_constant_expression_p;
28191   saved_non_integral_constant_expression_p
28192     = parser->non_integral_constant_expression_p;
28193   parser->integral_constant_expression_p = false;
28194 
28195   /* Do not actually evaluate the expression.  */
28196   ++cp_unevaluated_operand;
28197   ++c_inhibit_evaluation_warnings;
28198   /* If it's a `(', then we might be looking at the type-id
28199      construction.  */
28200   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28201     {
28202       tree type = NULL_TREE;
28203 
28204       /* We can't be sure yet whether we're looking at a type-id or an
28205 	 expression.  */
28206       cp_parser_parse_tentatively (parser);
28207 
28208       matching_parens parens;
28209       parens.consume_open (parser);
28210 
28211       /* Note: as a GNU Extension, compound literals are considered
28212 	 postfix-expressions as they are in C99, so they are valid
28213 	 arguments to sizeof.  See comment in cp_parser_cast_expression
28214 	 for details.  */
28215       if (cp_parser_compound_literal_p (parser))
28216 	cp_parser_simulate_error (parser);
28217       else
28218 	{
28219 	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28220 	  parser->in_type_id_in_expr_p = true;
28221 	  /* Look for the type-id.  */
28222 	  type = cp_parser_type_id (parser);
28223 	  /* Look for the closing `)'.  */
28224 	  parens.require_close (parser);
28225 	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28226 	}
28227 
28228       /* If all went well, then we're done.  */
28229       if (cp_parser_parse_definitely (parser))
28230 	{
28231 	  cp_decl_specifier_seq decl_specs;
28232 
28233 	  /* Build a trivial decl-specifier-seq.  */
28234 	  clear_decl_specs (&decl_specs);
28235 	  decl_specs.type = type;
28236 
28237 	  /* Call grokdeclarator to figure out what type this is.  */
28238 	  expr = grokdeclarator (NULL,
28239 				 &decl_specs,
28240 				 TYPENAME,
28241 				 /*initialized=*/0,
28242 				 /*attrlist=*/NULL);
28243 	}
28244     }
28245 
28246   /* If the type-id production did not work out, then we must be
28247      looking at the unary-expression production.  */
28248   if (!expr)
28249     expr = cp_parser_unary_expression (parser);
28250 
28251   /* Go back to evaluating expressions.  */
28252   --cp_unevaluated_operand;
28253   --c_inhibit_evaluation_warnings;
28254 
28255   /* Free the message we created.  */
28256   free (tmp);
28257   /* And restore the old one.  */
28258   parser->type_definition_forbidden_message = saved_message;
28259   parser->integral_constant_expression_p
28260     = saved_integral_constant_expression_p;
28261   parser->non_integral_constant_expression_p
28262     = saved_non_integral_constant_expression_p;
28263 
28264   return expr;
28265 }
28266 
28267 /* If the current declaration has no declarator, return true.  */
28268 
28269 static bool
cp_parser_declares_only_class_p(cp_parser * parser)28270 cp_parser_declares_only_class_p (cp_parser *parser)
28271 {
28272   /* If the next token is a `;' or a `,' then there is no
28273      declarator.  */
28274   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28275 	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28276 }
28277 
28278 /* Update the DECL_SPECS to reflect the storage class indicated by
28279    KEYWORD.  */
28280 
28281 static void
cp_parser_set_storage_class(cp_parser * parser,cp_decl_specifier_seq * decl_specs,enum rid keyword,cp_token * token)28282 cp_parser_set_storage_class (cp_parser *parser,
28283 			     cp_decl_specifier_seq *decl_specs,
28284 			     enum rid keyword,
28285 			     cp_token *token)
28286 {
28287   cp_storage_class storage_class;
28288 
28289   if (parser->in_unbraced_linkage_specification_p)
28290     {
28291       error_at (token->location, "invalid use of %qD in linkage specification",
28292 		ridpointers[keyword]);
28293       return;
28294     }
28295   else if (decl_specs->storage_class != sc_none)
28296     {
28297       decl_specs->conflicting_specifiers_p = true;
28298       return;
28299     }
28300 
28301   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28302       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28303       && decl_specs->gnu_thread_keyword_p)
28304     {
28305       pedwarn (decl_specs->locations[ds_thread], 0,
28306 		"%<__thread%> before %qD", ridpointers[keyword]);
28307     }
28308 
28309   switch (keyword)
28310     {
28311     case RID_AUTO:
28312       storage_class = sc_auto;
28313       break;
28314     case RID_REGISTER:
28315       storage_class = sc_register;
28316       break;
28317     case RID_STATIC:
28318       storage_class = sc_static;
28319       break;
28320     case RID_EXTERN:
28321       storage_class = sc_extern;
28322       break;
28323     case RID_MUTABLE:
28324       storage_class = sc_mutable;
28325       break;
28326     default:
28327       gcc_unreachable ();
28328     }
28329   decl_specs->storage_class = storage_class;
28330   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28331 
28332   /* A storage class specifier cannot be applied alongside a typedef
28333      specifier. If there is a typedef specifier present then set
28334      conflicting_specifiers_p which will trigger an error later
28335      on in grokdeclarator. */
28336   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28337     decl_specs->conflicting_specifiers_p = true;
28338 }
28339 
28340 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
28341    is true, the type is a class or enum definition.  */
28342 
28343 static void
cp_parser_set_decl_spec_type(cp_decl_specifier_seq * decl_specs,tree type_spec,cp_token * token,bool type_definition_p)28344 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28345 			      tree type_spec,
28346 			      cp_token *token,
28347 			      bool type_definition_p)
28348 {
28349   decl_specs->any_specifiers_p = true;
28350 
28351   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28352      (with, for example, in "typedef int wchar_t;") we remember that
28353      this is what happened.  In system headers, we ignore these
28354      declarations so that G++ can work with system headers that are not
28355      C++-safe.  */
28356   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28357       && !type_definition_p
28358       && (type_spec == boolean_type_node
28359 	  || type_spec == char16_type_node
28360 	  || type_spec == char32_type_node
28361 	  || type_spec == wchar_type_node)
28362       && (decl_specs->type
28363 	  || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28364 	  || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28365 	  || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28366 	  || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28367     {
28368       decl_specs->redefined_builtin_type = type_spec;
28369       set_and_check_decl_spec_loc (decl_specs,
28370 				   ds_redefined_builtin_type_spec,
28371 				   token);
28372       if (!decl_specs->type)
28373 	{
28374 	  decl_specs->type = type_spec;
28375 	  decl_specs->type_definition_p = false;
28376 	  set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28377 	}
28378     }
28379   else if (decl_specs->type)
28380     decl_specs->multiple_types_p = true;
28381   else
28382     {
28383       decl_specs->type = type_spec;
28384       decl_specs->type_definition_p = type_definition_p;
28385       decl_specs->redefined_builtin_type = NULL_TREE;
28386       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28387     }
28388 }
28389 
28390 /* True iff TOKEN is the GNU keyword __thread.  */
28391 
28392 static bool
token_is__thread(cp_token * token)28393 token_is__thread (cp_token *token)
28394 {
28395   gcc_assert (token->keyword == RID_THREAD);
28396   return id_equal (token->u.value, "__thread");
28397 }
28398 
28399 /* Set the location for a declarator specifier and check if it is
28400    duplicated.
28401 
28402    DECL_SPECS is the sequence of declarator specifiers onto which to
28403    set the location.
28404 
28405    DS is the single declarator specifier to set which location  is to
28406    be set onto the existing sequence of declarators.
28407 
28408    LOCATION is the location for the declarator specifier to
28409    consider.  */
28410 
28411 static void
set_and_check_decl_spec_loc(cp_decl_specifier_seq * decl_specs,cp_decl_spec ds,cp_token * token)28412 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28413 			     cp_decl_spec ds, cp_token *token)
28414 {
28415   gcc_assert (ds < ds_last);
28416 
28417   if (decl_specs == NULL)
28418     return;
28419 
28420   source_location location = token->location;
28421 
28422   if (decl_specs->locations[ds] == 0)
28423     {
28424       decl_specs->locations[ds] = location;
28425       if (ds == ds_thread)
28426 	decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28427     }
28428   else
28429     {
28430       if (ds == ds_long)
28431 	{
28432 	  if (decl_specs->locations[ds_long_long] != 0)
28433 	    error_at (location,
28434 		      "%<long long long%> is too long for GCC");
28435 	  else
28436 	    {
28437 	      decl_specs->locations[ds_long_long] = location;
28438 	      pedwarn_cxx98 (location,
28439 			     OPT_Wlong_long,
28440 			     "ISO C++ 1998 does not support %<long long%>");
28441 	    }
28442 	}
28443       else if (ds == ds_thread)
28444 	{
28445 	  bool gnu = token_is__thread (token);
28446 	  if (gnu != decl_specs->gnu_thread_keyword_p)
28447 	    error_at (location,
28448 		      "both %<__thread%> and %<thread_local%> specified");
28449 	  else
28450 	    {
28451 	      gcc_rich_location richloc (location);
28452 	      richloc.add_fixit_remove ();
28453 	      error_at (&richloc, "duplicate %qD", token->u.value);
28454 	    }
28455 	}
28456       else
28457 	{
28458 	  static const char *const decl_spec_names[] = {
28459 	    "signed",
28460 	    "unsigned",
28461 	    "short",
28462 	    "long",
28463 	    "const",
28464 	    "volatile",
28465 	    "restrict",
28466 	    "inline",
28467 	    "virtual",
28468 	    "explicit",
28469 	    "friend",
28470 	    "typedef",
28471 	    "using",
28472             "constexpr",
28473 	    "__complex"
28474 	  };
28475 	  gcc_rich_location richloc (location);
28476 	  richloc.add_fixit_remove ();
28477 	  error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28478 	}
28479     }
28480 }
28481 
28482 /* Return true iff the declarator specifier DS is present in the
28483    sequence of declarator specifiers DECL_SPECS.  */
28484 
28485 bool
decl_spec_seq_has_spec_p(const cp_decl_specifier_seq * decl_specs,cp_decl_spec ds)28486 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28487 			  cp_decl_spec ds)
28488 {
28489   gcc_assert (ds < ds_last);
28490 
28491   if (decl_specs == NULL)
28492     return false;
28493 
28494   return decl_specs->locations[ds] != 0;
28495 }
28496 
28497 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28498    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
28499 
28500 static bool
cp_parser_friend_p(const cp_decl_specifier_seq * decl_specifiers)28501 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28502 {
28503   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28504 }
28505 
28506 /* Issue an error message indicating that TOKEN_DESC was expected.
28507    If KEYWORD is true, it indicated this function is called by
28508    cp_parser_require_keword and the required token can only be
28509    a indicated keyword.
28510 
28511    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28512    within any error as the location of an "opening" token matching
28513    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28514    RT_CLOSE_PAREN).  */
28515 
28516 static void
cp_parser_required_error(cp_parser * parser,required_token token_desc,bool keyword,location_t matching_location)28517 cp_parser_required_error (cp_parser *parser,
28518 			  required_token token_desc,
28519 			  bool keyword,
28520 			  location_t matching_location)
28521 {
28522   if (cp_parser_simulate_error (parser))
28523     return;
28524 
28525   const char *gmsgid = NULL;
28526   switch (token_desc)
28527     {
28528       case RT_NEW:
28529 	gmsgid = G_("expected %<new%>");
28530 	break;
28531       case RT_DELETE:
28532 	gmsgid = G_("expected %<delete%>");
28533 	break;
28534       case RT_RETURN:
28535 	gmsgid = G_("expected %<return%>");
28536 	break;
28537       case RT_WHILE:
28538 	gmsgid = G_("expected %<while%>");
28539 	break;
28540       case RT_EXTERN:
28541 	gmsgid = G_("expected %<extern%>");
28542 	break;
28543       case RT_STATIC_ASSERT:
28544 	gmsgid = G_("expected %<static_assert%>");
28545 	break;
28546       case RT_DECLTYPE:
28547 	gmsgid = G_("expected %<decltype%>");
28548 	break;
28549       case RT_OPERATOR:
28550 	gmsgid = G_("expected %<operator%>");
28551 	break;
28552       case RT_CLASS:
28553 	gmsgid = G_("expected %<class%>");
28554 	break;
28555       case RT_TEMPLATE:
28556 	gmsgid = G_("expected %<template%>");
28557 	break;
28558       case RT_NAMESPACE:
28559 	gmsgid = G_("expected %<namespace%>");
28560 	break;
28561       case RT_USING:
28562 	gmsgid = G_("expected %<using%>");
28563 	break;
28564       case RT_ASM:
28565 	gmsgid = G_("expected %<asm%>");
28566 	break;
28567       case RT_TRY:
28568 	gmsgid = G_("expected %<try%>");
28569 	break;
28570       case RT_CATCH:
28571 	gmsgid = G_("expected %<catch%>");
28572 	break;
28573       case RT_THROW:
28574 	gmsgid = G_("expected %<throw%>");
28575 	break;
28576       case RT_LABEL:
28577 	gmsgid = G_("expected %<__label__%>");
28578 	break;
28579       case RT_AT_TRY:
28580 	gmsgid = G_("expected %<@try%>");
28581 	break;
28582       case RT_AT_SYNCHRONIZED:
28583 	gmsgid = G_("expected %<@synchronized%>");
28584 	break;
28585       case RT_AT_THROW:
28586 	gmsgid = G_("expected %<@throw%>");
28587 	break;
28588       case RT_TRANSACTION_ATOMIC:
28589 	gmsgid = G_("expected %<__transaction_atomic%>");
28590 	break;
28591       case RT_TRANSACTION_RELAXED:
28592 	gmsgid = G_("expected %<__transaction_relaxed%>");
28593 	break;
28594       default:
28595 	break;
28596     }
28597 
28598   if (!gmsgid && !keyword)
28599     {
28600       switch (token_desc)
28601         {
28602 	  case RT_SEMICOLON:
28603 	    gmsgid = G_("expected %<;%>");
28604 	    break;
28605 	  case RT_OPEN_PAREN:
28606 	    gmsgid = G_("expected %<(%>");
28607 	    break;
28608 	  case RT_CLOSE_BRACE:
28609 	    gmsgid = G_("expected %<}%>");
28610 	    break;
28611 	  case RT_OPEN_BRACE:
28612 	    gmsgid = G_("expected %<{%>");
28613 	    break;
28614 	  case RT_CLOSE_SQUARE:
28615 	    gmsgid = G_("expected %<]%>");
28616 	    break;
28617 	  case RT_OPEN_SQUARE:
28618 	    gmsgid = G_("expected %<[%>");
28619 	    break;
28620 	  case RT_COMMA:
28621 	    gmsgid = G_("expected %<,%>");
28622 	    break;
28623 	  case RT_SCOPE:
28624 	    gmsgid = G_("expected %<::%>");
28625 	    break;
28626 	  case RT_LESS:
28627 	    gmsgid = G_("expected %<<%>");
28628 	    break;
28629 	  case RT_GREATER:
28630 	    gmsgid = G_("expected %<>%>");
28631 	    break;
28632 	  case RT_EQ:
28633 	    gmsgid = G_("expected %<=%>");
28634 	    break;
28635 	  case RT_ELLIPSIS:
28636 	    gmsgid = G_("expected %<...%>");
28637 	    break;
28638 	  case RT_MULT:
28639 	    gmsgid = G_("expected %<*%>");
28640 	    break;
28641 	  case RT_COMPL:
28642 	    gmsgid = G_("expected %<~%>");
28643 	    break;
28644 	  case RT_COLON:
28645 	    gmsgid = G_("expected %<:%>");
28646 	    break;
28647 	  case RT_COLON_SCOPE:
28648 	    gmsgid = G_("expected %<:%> or %<::%>");
28649 	    break;
28650 	  case RT_CLOSE_PAREN:
28651 	    gmsgid = G_("expected %<)%>");
28652 	    break;
28653 	  case RT_COMMA_CLOSE_PAREN:
28654 	    gmsgid = G_("expected %<,%> or %<)%>");
28655 	    break;
28656 	  case RT_PRAGMA_EOL:
28657 	    gmsgid = G_("expected end of line");
28658 	    break;
28659 	  case RT_NAME:
28660 	    gmsgid = G_("expected identifier");
28661 	    break;
28662 	  case RT_SELECT:
28663 	    gmsgid = G_("expected selection-statement");
28664 	    break;
28665 	  case RT_ITERATION:
28666 	    gmsgid = G_("expected iteration-statement");
28667 	    break;
28668 	  case RT_JUMP:
28669 	    gmsgid = G_("expected jump-statement");
28670 	    break;
28671 	  case RT_CLASS_KEY:
28672 	    gmsgid = G_("expected class-key");
28673 	    break;
28674 	  case RT_CLASS_TYPENAME_TEMPLATE:
28675 	    gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28676 	    break;
28677 	  default:
28678 	    gcc_unreachable ();
28679 	}
28680     }
28681 
28682   if (gmsgid)
28683     cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28684 }
28685 
28686 
28687 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
28688    issue an error message indicating that TOKEN_DESC was expected.
28689 
28690    Returns the token consumed, if the token had the appropriate type.
28691    Otherwise, returns NULL.
28692 
28693    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28694    within any error as the location of an "opening" token matching
28695    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28696    RT_CLOSE_PAREN).  */
28697 
28698 static cp_token *
cp_parser_require(cp_parser * parser,enum cpp_ttype type,required_token token_desc,location_t matching_location)28699 cp_parser_require (cp_parser* parser,
28700 		   enum cpp_ttype type,
28701 		   required_token token_desc,
28702 		   location_t matching_location)
28703 {
28704   if (cp_lexer_next_token_is (parser->lexer, type))
28705     return cp_lexer_consume_token (parser->lexer);
28706   else
28707     {
28708       /* Output the MESSAGE -- unless we're parsing tentatively.  */
28709       if (!cp_parser_simulate_error (parser))
28710 	cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28711 				  matching_location);
28712       return NULL;
28713     }
28714 }
28715 
28716 /* An error message is produced if the next token is not '>'.
28717    All further tokens are skipped until the desired token is
28718    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
28719 
28720 static void
cp_parser_skip_to_end_of_template_parameter_list(cp_parser * parser)28721 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28722 {
28723   /* Current level of '< ... >'.  */
28724   unsigned level = 0;
28725   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
28726   unsigned nesting_depth = 0;
28727 
28728   /* Are we ready, yet?  If not, issue error message.  */
28729   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28730     return;
28731 
28732   /* Skip tokens until the desired token is found.  */
28733   while (true)
28734     {
28735       /* Peek at the next token.  */
28736       switch (cp_lexer_peek_token (parser->lexer)->type)
28737 	{
28738 	case CPP_LESS:
28739 	  if (!nesting_depth)
28740 	    ++level;
28741 	  break;
28742 
28743         case CPP_RSHIFT:
28744           if (cxx_dialect == cxx98)
28745             /* C++0x views the `>>' operator as two `>' tokens, but
28746                C++98 does not. */
28747             break;
28748           else if (!nesting_depth && level-- == 0)
28749 	    {
28750               /* We've hit a `>>' where the first `>' closes the
28751                  template argument list, and the second `>' is
28752                  spurious.  Just consume the `>>' and stop; we've
28753                  already produced at least one error.  */
28754 	      cp_lexer_consume_token (parser->lexer);
28755 	      return;
28756 	    }
28757           /* Fall through for C++0x, so we handle the second `>' in
28758              the `>>'.  */
28759 	  gcc_fallthrough ();
28760 
28761 	case CPP_GREATER:
28762 	  if (!nesting_depth && level-- == 0)
28763 	    {
28764 	      /* We've reached the token we want, consume it and stop.  */
28765 	      cp_lexer_consume_token (parser->lexer);
28766 	      return;
28767 	    }
28768 	  break;
28769 
28770 	case CPP_OPEN_PAREN:
28771 	case CPP_OPEN_SQUARE:
28772 	  ++nesting_depth;
28773 	  break;
28774 
28775 	case CPP_CLOSE_PAREN:
28776 	case CPP_CLOSE_SQUARE:
28777 	  if (nesting_depth-- == 0)
28778 	    return;
28779 	  break;
28780 
28781 	case CPP_EOF:
28782 	case CPP_PRAGMA_EOL:
28783 	case CPP_SEMICOLON:
28784 	case CPP_OPEN_BRACE:
28785 	case CPP_CLOSE_BRACE:
28786 	  /* The '>' was probably forgotten, don't look further.  */
28787 	  return;
28788 
28789 	default:
28790 	  break;
28791 	}
28792 
28793       /* Consume this token.  */
28794       cp_lexer_consume_token (parser->lexer);
28795     }
28796 }
28797 
28798 /* If the next token is the indicated keyword, consume it.  Otherwise,
28799    issue an error message indicating that TOKEN_DESC was expected.
28800 
28801    Returns the token consumed, if the token had the appropriate type.
28802    Otherwise, returns NULL.  */
28803 
28804 static cp_token *
cp_parser_require_keyword(cp_parser * parser,enum rid keyword,required_token token_desc)28805 cp_parser_require_keyword (cp_parser* parser,
28806 			   enum rid keyword,
28807 			   required_token token_desc)
28808 {
28809   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28810 
28811   if (token && token->keyword != keyword)
28812     {
28813       cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28814                                 UNKNOWN_LOCATION);
28815       return NULL;
28816     }
28817 
28818   return token;
28819 }
28820 
28821 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28822    function-definition.  */
28823 
28824 static bool
cp_parser_token_starts_function_definition_p(cp_token * token)28825 cp_parser_token_starts_function_definition_p (cp_token* token)
28826 {
28827   return (/* An ordinary function-body begins with an `{'.  */
28828 	  token->type == CPP_OPEN_BRACE
28829 	  /* A ctor-initializer begins with a `:'.  */
28830 	  || token->type == CPP_COLON
28831 	  /* A function-try-block begins with `try'.  */
28832 	  || token->keyword == RID_TRY
28833 	  /* A function-transaction-block begins with `__transaction_atomic'
28834 	     or `__transaction_relaxed'.  */
28835 	  || token->keyword == RID_TRANSACTION_ATOMIC
28836 	  || token->keyword == RID_TRANSACTION_RELAXED
28837 	  /* The named return value extension begins with `return'.  */
28838 	  || token->keyword == RID_RETURN);
28839 }
28840 
28841 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28842    definition.  */
28843 
28844 static bool
cp_parser_next_token_starts_class_definition_p(cp_parser * parser)28845 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28846 {
28847   cp_token *token;
28848 
28849   token = cp_lexer_peek_token (parser->lexer);
28850   return (token->type == CPP_OPEN_BRACE
28851 	  || (token->type == CPP_COLON
28852 	      && !parser->colon_doesnt_start_class_def_p));
28853 }
28854 
28855 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28856    C++0x) ending a template-argument.  */
28857 
28858 static bool
cp_parser_next_token_ends_template_argument_p(cp_parser * parser)28859 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28860 {
28861   cp_token *token;
28862 
28863   token = cp_lexer_peek_token (parser->lexer);
28864   return (token->type == CPP_COMMA
28865           || token->type == CPP_GREATER
28866           || token->type == CPP_ELLIPSIS
28867 	  || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28868 }
28869 
28870 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28871    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
28872 
28873 static bool
cp_parser_nth_token_starts_template_argument_list_p(cp_parser * parser,size_t n)28874 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28875 						     size_t n)
28876 {
28877   cp_token *token;
28878 
28879   token = cp_lexer_peek_nth_token (parser->lexer, n);
28880   if (token->type == CPP_LESS)
28881     return true;
28882   /* Check for the sequence `<::' in the original code. It would be lexed as
28883      `[:', where `[' is a digraph, and there is no whitespace before
28884      `:'.  */
28885   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28886     {
28887       cp_token *token2;
28888       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28889       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28890 	return true;
28891     }
28892   return false;
28893 }
28894 
28895 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28896    or none_type otherwise.  */
28897 
28898 static enum tag_types
cp_parser_token_is_class_key(cp_token * token)28899 cp_parser_token_is_class_key (cp_token* token)
28900 {
28901   switch (token->keyword)
28902     {
28903     case RID_CLASS:
28904       return class_type;
28905     case RID_STRUCT:
28906       return record_type;
28907     case RID_UNION:
28908       return union_type;
28909 
28910     default:
28911       return none_type;
28912     }
28913 }
28914 
28915 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28916    or none_type otherwise or if the token is null.  */
28917 
28918 static enum tag_types
cp_parser_token_is_type_parameter_key(cp_token * token)28919 cp_parser_token_is_type_parameter_key (cp_token* token)
28920 {
28921   if (!token)
28922     return none_type;
28923 
28924   switch (token->keyword)
28925     {
28926     case RID_CLASS:
28927       return class_type;
28928     case RID_TYPENAME:
28929       return typename_type;
28930 
28931     default:
28932       return none_type;
28933     }
28934 }
28935 
28936 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
28937 
28938 static void
cp_parser_check_class_key(enum tag_types class_key,tree type)28939 cp_parser_check_class_key (enum tag_types class_key, tree type)
28940 {
28941   if (type == error_mark_node)
28942     return;
28943   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28944     {
28945       if (permerror (input_location, "%qs tag used in naming %q#T",
28946 		     class_key == union_type ? "union"
28947 		     : class_key == record_type ? "struct" : "class",
28948 		     type))
28949 	inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28950 		"%q#T was previously declared here", type);
28951     }
28952 }
28953 
28954 /* Issue an error message if DECL is redeclared with different
28955    access than its original declaration [class.access.spec/3].
28956    This applies to nested classes, nested class templates and
28957    enumerations [class.mem/1].  */
28958 
28959 static void
cp_parser_check_access_in_redeclaration(tree decl,location_t location)28960 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28961 {
28962   if (!decl
28963       || (!CLASS_TYPE_P (TREE_TYPE (decl))
28964 	  && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28965     return;
28966 
28967   if ((TREE_PRIVATE (decl)
28968        != (current_access_specifier == access_private_node))
28969       || (TREE_PROTECTED (decl)
28970 	  != (current_access_specifier == access_protected_node)))
28971     error_at (location, "%qD redeclared with different access", decl);
28972 }
28973 
28974 /* Look for the `template' keyword, as a syntactic disambiguator.
28975    Return TRUE iff it is present, in which case it will be
28976    consumed.  */
28977 
28978 static bool
cp_parser_optional_template_keyword(cp_parser * parser)28979 cp_parser_optional_template_keyword (cp_parser *parser)
28980 {
28981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28982     {
28983       /* In C++98 the `template' keyword can only be used within templates;
28984 	 outside templates the parser can always figure out what is a
28985 	 template and what is not.  In C++11,  per the resolution of DR 468,
28986 	 `template' is allowed in cases where it is not strictly necessary.  */
28987       if (!processing_template_decl
28988 	  && pedantic && cxx_dialect == cxx98)
28989 	{
28990 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
28991 	  pedwarn (token->location, OPT_Wpedantic,
28992 		   "in C++98 %<template%> (as a disambiguator) is only "
28993 		   "allowed within templates");
28994 	  /* If this part of the token stream is rescanned, the same
28995 	     error message would be generated.  So, we purge the token
28996 	     from the stream.  */
28997 	  cp_lexer_purge_token (parser->lexer);
28998 	  return false;
28999 	}
29000       else
29001 	{
29002 	  /* Consume the `template' keyword.  */
29003 	  cp_lexer_consume_token (parser->lexer);
29004 	  return true;
29005 	}
29006     }
29007   return false;
29008 }
29009 
29010 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
29011    set PARSER->SCOPE, and perform other related actions.  */
29012 
29013 static void
cp_parser_pre_parsed_nested_name_specifier(cp_parser * parser)29014 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29015 {
29016   struct tree_check *check_value;
29017 
29018   /* Get the stored value.  */
29019   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29020   /* Set the scope from the stored value.  */
29021   parser->scope = saved_checks_value (check_value);
29022   parser->qualifying_scope = check_value->qualifying_scope;
29023   parser->object_scope = NULL_TREE;
29024 }
29025 
29026 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
29027    encounter the end of a block before what we were looking for.  */
29028 
29029 static bool
cp_parser_cache_group(cp_parser * parser,enum cpp_ttype end,unsigned depth)29030 cp_parser_cache_group (cp_parser *parser,
29031 		       enum cpp_ttype end,
29032 		       unsigned depth)
29033 {
29034   while (true)
29035     {
29036       cp_token *token = cp_lexer_peek_token (parser->lexer);
29037 
29038       /* Abort a parenthesized expression if we encounter a semicolon.  */
29039       if ((end == CPP_CLOSE_PAREN || depth == 0)
29040 	  && token->type == CPP_SEMICOLON)
29041 	return true;
29042       /* If we've reached the end of the file, stop.  */
29043       if (token->type == CPP_EOF
29044 	  || (end != CPP_PRAGMA_EOL
29045 	      && token->type == CPP_PRAGMA_EOL))
29046 	return true;
29047       if (token->type == CPP_CLOSE_BRACE && depth == 0)
29048 	/* We've hit the end of an enclosing block, so there's been some
29049 	   kind of syntax error.  */
29050 	return true;
29051 
29052       /* Consume the token.  */
29053       cp_lexer_consume_token (parser->lexer);
29054       /* See if it starts a new group.  */
29055       if (token->type == CPP_OPEN_BRACE)
29056 	{
29057 	  cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29058 	  /* In theory this should probably check end == '}', but
29059 	     cp_parser_save_member_function_body needs it to exit
29060 	     after either '}' or ')' when called with ')'.  */
29061 	  if (depth == 0)
29062 	    return false;
29063 	}
29064       else if (token->type == CPP_OPEN_PAREN)
29065 	{
29066 	  cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29067 	  if (depth == 0 && end == CPP_CLOSE_PAREN)
29068 	    return false;
29069 	}
29070       else if (token->type == CPP_PRAGMA)
29071 	cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29072       else if (token->type == end)
29073 	return false;
29074     }
29075 }
29076 
29077 /* Like above, for caching a default argument or NSDMI.  Both of these are
29078    terminated by a non-nested comma, but it can be unclear whether or not a
29079    comma is nested in a template argument list unless we do more parsing.
29080    In order to handle this ambiguity, when we encounter a ',' after a '<'
29081    we try to parse what follows as a parameter-declaration-list (in the
29082    case of a default argument) or a member-declarator (in the case of an
29083    NSDMI).  If that succeeds, then we stop caching.  */
29084 
29085 static tree
cp_parser_cache_defarg(cp_parser * parser,bool nsdmi)29086 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29087 {
29088   unsigned depth = 0;
29089   int maybe_template_id = 0;
29090   cp_token *first_token;
29091   cp_token *token;
29092   tree default_argument;
29093 
29094   /* Add tokens until we have processed the entire default
29095      argument.  We add the range [first_token, token).  */
29096   first_token = cp_lexer_peek_token (parser->lexer);
29097   if (first_token->type == CPP_OPEN_BRACE)
29098     {
29099       /* For list-initialization, this is straightforward.  */
29100       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29101       token = cp_lexer_peek_token (parser->lexer);
29102     }
29103   else while (true)
29104     {
29105       bool done = false;
29106 
29107       /* Peek at the next token.  */
29108       token = cp_lexer_peek_token (parser->lexer);
29109       /* What we do depends on what token we have.  */
29110       switch (token->type)
29111 	{
29112 	  /* In valid code, a default argument must be
29113 	     immediately followed by a `,' `)', or `...'.  */
29114 	case CPP_COMMA:
29115 	  if (depth == 0 && maybe_template_id)
29116 	    {
29117 	      /* If we've seen a '<', we might be in a
29118 		 template-argument-list.  Until Core issue 325 is
29119 		 resolved, we don't know how this situation ought
29120 		 to be handled, so try to DTRT.  We check whether
29121 		 what comes after the comma is a valid parameter
29122 		 declaration list.  If it is, then the comma ends
29123 		 the default argument; otherwise the default
29124 		 argument continues.  */
29125 	      bool error = false;
29126 	      cp_token *peek;
29127 
29128 	      /* Set ITALP so cp_parser_parameter_declaration_list
29129 		 doesn't decide to commit to this parse.  */
29130 	      bool saved_italp = parser->in_template_argument_list_p;
29131 	      parser->in_template_argument_list_p = true;
29132 
29133 	      cp_parser_parse_tentatively (parser);
29134 
29135 	      if (nsdmi)
29136 		{
29137 		  /* Parse declarators until we reach a non-comma or
29138 		     somthing that cannot be an initializer.
29139 		     Just checking whether we're looking at a single
29140 		     declarator is insufficient.  Consider:
29141 		       int var = tuple<T,U>::x;
29142 		     The template parameter 'U' looks exactly like a
29143 		     declarator.  */
29144 		  do
29145 		    {
29146 		      int ctor_dtor_or_conv_p;
29147 		      cp_lexer_consume_token (parser->lexer);
29148 		      cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29149 					    &ctor_dtor_or_conv_p,
29150 					    /*parenthesized_p=*/NULL,
29151 					    /*member_p=*/true,
29152 					    /*friend_p=*/false);
29153 		      peek = cp_lexer_peek_token (parser->lexer);
29154 		      if (cp_parser_error_occurred (parser))
29155 			break;
29156 		    }
29157 		  while (peek->type == CPP_COMMA);
29158 		  /* If we met an '=' or ';' then the original comma
29159 		     was the end of the NSDMI.  Otherwise assume
29160 		     we're still in the NSDMI.  */
29161 		  error = (peek->type != CPP_EQ
29162 			   && peek->type != CPP_SEMICOLON);
29163 		}
29164 	      else
29165 		{
29166 		  cp_lexer_consume_token (parser->lexer);
29167 		  begin_scope (sk_function_parms, NULL_TREE);
29168 		  cp_parser_parameter_declaration_list (parser, &error);
29169 		  pop_bindings_and_leave_scope ();
29170 		}
29171 	      if (!cp_parser_error_occurred (parser) && !error)
29172 		done = true;
29173 	      cp_parser_abort_tentative_parse (parser);
29174 
29175 	      parser->in_template_argument_list_p = saved_italp;
29176 	      break;
29177 	    }
29178 	  /* FALLTHRU */
29179 	case CPP_CLOSE_PAREN:
29180 	case CPP_ELLIPSIS:
29181 	  /* If we run into a non-nested `;', `}', or `]',
29182 	     then the code is invalid -- but the default
29183 	     argument is certainly over.  */
29184 	case CPP_SEMICOLON:
29185 	case CPP_CLOSE_BRACE:
29186 	case CPP_CLOSE_SQUARE:
29187 	  if (depth == 0
29188 	      /* Handle correctly int n = sizeof ... ( p );  */
29189 	      && token->type != CPP_ELLIPSIS)
29190 	    done = true;
29191 	  /* Update DEPTH, if necessary.  */
29192 	  else if (token->type == CPP_CLOSE_PAREN
29193 		   || token->type == CPP_CLOSE_BRACE
29194 		   || token->type == CPP_CLOSE_SQUARE)
29195 	    --depth;
29196 	  break;
29197 
29198 	case CPP_OPEN_PAREN:
29199 	case CPP_OPEN_SQUARE:
29200 	case CPP_OPEN_BRACE:
29201 	  ++depth;
29202 	  break;
29203 
29204 	case CPP_LESS:
29205 	  if (depth == 0)
29206 	    /* This might be the comparison operator, or it might
29207 	       start a template argument list.  */
29208 	    ++maybe_template_id;
29209 	  break;
29210 
29211 	case CPP_RSHIFT:
29212 	  if (cxx_dialect == cxx98)
29213 	    break;
29214 	  /* Fall through for C++0x, which treats the `>>'
29215 	     operator like two `>' tokens in certain
29216 	     cases.  */
29217 	  gcc_fallthrough ();
29218 
29219 	case CPP_GREATER:
29220 	  if (depth == 0)
29221 	    {
29222 	      /* This might be an operator, or it might close a
29223 		 template argument list.  But if a previous '<'
29224 		 started a template argument list, this will have
29225 		 closed it, so we can't be in one anymore.  */
29226 	      maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29227 	      if (maybe_template_id < 0)
29228 		maybe_template_id = 0;
29229 	    }
29230 	  break;
29231 
29232 	  /* If we run out of tokens, issue an error message.  */
29233 	case CPP_EOF:
29234 	case CPP_PRAGMA_EOL:
29235 	  error_at (token->location, "file ends in default argument");
29236 	  return error_mark_node;
29237 
29238 	case CPP_NAME:
29239 	case CPP_SCOPE:
29240 	  /* In these cases, we should look for template-ids.
29241 	     For example, if the default argument is
29242 	     `X<int, double>()', we need to do name lookup to
29243 	     figure out whether or not `X' is a template; if
29244 	     so, the `,' does not end the default argument.
29245 
29246 	     That is not yet done.  */
29247 	  break;
29248 
29249 	default:
29250 	  break;
29251 	}
29252 
29253       /* If we've reached the end, stop.  */
29254       if (done)
29255 	break;
29256 
29257       /* Add the token to the token block.  */
29258       token = cp_lexer_consume_token (parser->lexer);
29259     }
29260 
29261   /* Create a DEFAULT_ARG to represent the unparsed default
29262      argument.  */
29263   default_argument = make_node (DEFAULT_ARG);
29264   DEFARG_TOKENS (default_argument)
29265     = cp_token_cache_new (first_token, token);
29266   DEFARG_INSTANTIATIONS (default_argument) = NULL;
29267 
29268   return default_argument;
29269 }
29270 
29271 /* A location to use for diagnostics about an unparsed DEFAULT_ARG.  */
29272 
29273 location_t
defarg_location(tree default_argument)29274 defarg_location (tree default_argument)
29275 {
29276   cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29277   location_t start = tokens->first->location;
29278   location_t end = tokens->last->location;
29279   return make_location (start, start, end);
29280 }
29281 
29282 /* Begin parsing tentatively.  We always save tokens while parsing
29283    tentatively so that if the tentative parsing fails we can restore the
29284    tokens.  */
29285 
29286 static void
cp_parser_parse_tentatively(cp_parser * parser)29287 cp_parser_parse_tentatively (cp_parser* parser)
29288 {
29289   /* Enter a new parsing context.  */
29290   parser->context = cp_parser_context_new (parser->context);
29291   /* Begin saving tokens.  */
29292   cp_lexer_save_tokens (parser->lexer);
29293   /* In order to avoid repetitive access control error messages,
29294      access checks are queued up until we are no longer parsing
29295      tentatively.  */
29296   push_deferring_access_checks (dk_deferred);
29297 }
29298 
29299 /* Commit to the currently active tentative parse.  */
29300 
29301 static void
cp_parser_commit_to_tentative_parse(cp_parser * parser)29302 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29303 {
29304   cp_parser_context *context;
29305   cp_lexer *lexer;
29306 
29307   /* Mark all of the levels as committed.  */
29308   lexer = parser->lexer;
29309   for (context = parser->context; context->next; context = context->next)
29310     {
29311       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29312 	break;
29313       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29314       while (!cp_lexer_saving_tokens (lexer))
29315 	lexer = lexer->next;
29316       cp_lexer_commit_tokens (lexer);
29317     }
29318 }
29319 
29320 /* Commit to the topmost currently active tentative parse.
29321 
29322    Note that this function shouldn't be called when there are
29323    irreversible side-effects while in a tentative state.  For
29324    example, we shouldn't create a permanent entry in the symbol
29325    table, or issue an error message that might not apply if the
29326    tentative parse is aborted.  */
29327 
29328 static void
cp_parser_commit_to_topmost_tentative_parse(cp_parser * parser)29329 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29330 {
29331   cp_parser_context *context = parser->context;
29332   cp_lexer *lexer = parser->lexer;
29333 
29334   if (context)
29335     {
29336       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29337 	return;
29338       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29339 
29340       while (!cp_lexer_saving_tokens (lexer))
29341 	lexer = lexer->next;
29342       cp_lexer_commit_tokens (lexer);
29343     }
29344 }
29345 
29346 /* Abort the currently active tentative parse.  All consumed tokens
29347    will be rolled back, and no diagnostics will be issued.  */
29348 
29349 static void
cp_parser_abort_tentative_parse(cp_parser * parser)29350 cp_parser_abort_tentative_parse (cp_parser* parser)
29351 {
29352   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29353 	      || errorcount > 0);
29354   cp_parser_simulate_error (parser);
29355   /* Now, pretend that we want to see if the construct was
29356      successfully parsed.  */
29357   cp_parser_parse_definitely (parser);
29358 }
29359 
29360 /* Stop parsing tentatively.  If a parse error has occurred, restore the
29361    token stream.  Otherwise, commit to the tokens we have consumed.
29362    Returns true if no error occurred; false otherwise.  */
29363 
29364 static bool
cp_parser_parse_definitely(cp_parser * parser)29365 cp_parser_parse_definitely (cp_parser* parser)
29366 {
29367   bool error_occurred;
29368   cp_parser_context *context;
29369 
29370   /* Remember whether or not an error occurred, since we are about to
29371      destroy that information.  */
29372   error_occurred = cp_parser_error_occurred (parser);
29373   /* Remove the topmost context from the stack.  */
29374   context = parser->context;
29375   parser->context = context->next;
29376   /* If no parse errors occurred, commit to the tentative parse.  */
29377   if (!error_occurred)
29378     {
29379       /* Commit to the tokens read tentatively, unless that was
29380 	 already done.  */
29381       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29382 	cp_lexer_commit_tokens (parser->lexer);
29383 
29384       pop_to_parent_deferring_access_checks ();
29385     }
29386   /* Otherwise, if errors occurred, roll back our state so that things
29387      are just as they were before we began the tentative parse.  */
29388   else
29389     {
29390       cp_lexer_rollback_tokens (parser->lexer);
29391       pop_deferring_access_checks ();
29392     }
29393   /* Add the context to the front of the free list.  */
29394   context->next = cp_parser_context_free_list;
29395   cp_parser_context_free_list = context;
29396 
29397   return !error_occurred;
29398 }
29399 
29400 /* Returns true if we are parsing tentatively and are not committed to
29401    this tentative parse.  */
29402 
29403 static bool
cp_parser_uncommitted_to_tentative_parse_p(cp_parser * parser)29404 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29405 {
29406   return (cp_parser_parsing_tentatively (parser)
29407 	  && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29408 }
29409 
29410 /* Returns nonzero iff an error has occurred during the most recent
29411    tentative parse.  */
29412 
29413 static bool
cp_parser_error_occurred(cp_parser * parser)29414 cp_parser_error_occurred (cp_parser* parser)
29415 {
29416   return (cp_parser_parsing_tentatively (parser)
29417 	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29418 }
29419 
29420 /* Returns nonzero if GNU extensions are allowed.  */
29421 
29422 static bool
cp_parser_allow_gnu_extensions_p(cp_parser * parser)29423 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29424 {
29425   return parser->allow_gnu_extensions_p;
29426 }
29427 
29428 /* Objective-C++ Productions */
29429 
29430 
29431 /* Parse an Objective-C expression, which feeds into a primary-expression
29432    above.
29433 
29434    objc-expression:
29435      objc-message-expression
29436      objc-string-literal
29437      objc-encode-expression
29438      objc-protocol-expression
29439      objc-selector-expression
29440 
29441   Returns a tree representation of the expression.  */
29442 
29443 static cp_expr
cp_parser_objc_expression(cp_parser * parser)29444 cp_parser_objc_expression (cp_parser* parser)
29445 {
29446   /* Try to figure out what kind of declaration is present.  */
29447   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29448 
29449   switch (kwd->type)
29450     {
29451     case CPP_OPEN_SQUARE:
29452       return cp_parser_objc_message_expression (parser);
29453 
29454     case CPP_OBJC_STRING:
29455       kwd = cp_lexer_consume_token (parser->lexer);
29456       return objc_build_string_object (kwd->u.value);
29457 
29458     case CPP_KEYWORD:
29459       switch (kwd->keyword)
29460 	{
29461 	case RID_AT_ENCODE:
29462 	  return cp_parser_objc_encode_expression (parser);
29463 
29464 	case RID_AT_PROTOCOL:
29465 	  return cp_parser_objc_protocol_expression (parser);
29466 
29467 	case RID_AT_SELECTOR:
29468 	  return cp_parser_objc_selector_expression (parser);
29469 
29470 	default:
29471 	  break;
29472 	}
29473       /* FALLTHRU */
29474     default:
29475       error_at (kwd->location,
29476 		"misplaced %<@%D%> Objective-C++ construct",
29477 		kwd->u.value);
29478       cp_parser_skip_to_end_of_block_or_statement (parser);
29479     }
29480 
29481   return error_mark_node;
29482 }
29483 
29484 /* Parse an Objective-C message expression.
29485 
29486    objc-message-expression:
29487      [ objc-message-receiver objc-message-args ]
29488 
29489    Returns a representation of an Objective-C message.  */
29490 
29491 static tree
cp_parser_objc_message_expression(cp_parser * parser)29492 cp_parser_objc_message_expression (cp_parser* parser)
29493 {
29494   tree receiver, messageargs;
29495 
29496   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29497   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
29498   receiver = cp_parser_objc_message_receiver (parser);
29499   messageargs = cp_parser_objc_message_args (parser);
29500   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29501   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29502 
29503   tree result = objc_build_message_expr (receiver, messageargs);
29504 
29505   /* Construct a location e.g.
29506        [self func1:5]
29507        ^~~~~~~~~~~~~~
29508      ranging from the '[' to the ']', with the caret at the start.  */
29509   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29510   protected_set_expr_location (result, combined_loc);
29511 
29512   return result;
29513 }
29514 
29515 /* Parse an objc-message-receiver.
29516 
29517    objc-message-receiver:
29518      expression
29519      simple-type-specifier
29520 
29521   Returns a representation of the type or expression.  */
29522 
29523 static tree
cp_parser_objc_message_receiver(cp_parser * parser)29524 cp_parser_objc_message_receiver (cp_parser* parser)
29525 {
29526   tree rcv;
29527 
29528   /* An Objective-C message receiver may be either (1) a type
29529      or (2) an expression.  */
29530   cp_parser_parse_tentatively (parser);
29531   rcv = cp_parser_expression (parser);
29532 
29533   /* If that worked out, fine.  */
29534   if (cp_parser_parse_definitely (parser))
29535     return rcv;
29536 
29537   cp_parser_parse_tentatively (parser);
29538   rcv = cp_parser_simple_type_specifier (parser,
29539 					 /*decl_specs=*/NULL,
29540 					 CP_PARSER_FLAGS_NONE);
29541 
29542   if (cp_parser_parse_definitely (parser))
29543     return objc_get_class_reference (rcv);
29544 
29545   cp_parser_error (parser, "objective-c++ message receiver expected");
29546   return error_mark_node;
29547 }
29548 
29549 /* Parse the arguments and selectors comprising an Objective-C message.
29550 
29551    objc-message-args:
29552      objc-selector
29553      objc-selector-args
29554      objc-selector-args , objc-comma-args
29555 
29556    objc-selector-args:
29557      objc-selector [opt] : assignment-expression
29558      objc-selector-args objc-selector [opt] : assignment-expression
29559 
29560    objc-comma-args:
29561      assignment-expression
29562      objc-comma-args , assignment-expression
29563 
29564    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29565    selector arguments and TREE_VALUE containing a list of comma
29566    arguments.  */
29567 
29568 static tree
cp_parser_objc_message_args(cp_parser * parser)29569 cp_parser_objc_message_args (cp_parser* parser)
29570 {
29571   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29572   bool maybe_unary_selector_p = true;
29573   cp_token *token = cp_lexer_peek_token (parser->lexer);
29574 
29575   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29576     {
29577       tree selector = NULL_TREE, arg;
29578 
29579       if (token->type != CPP_COLON)
29580 	selector = cp_parser_objc_selector (parser);
29581 
29582       /* Detect if we have a unary selector.  */
29583       if (maybe_unary_selector_p
29584 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29585 	return build_tree_list (selector, NULL_TREE);
29586 
29587       maybe_unary_selector_p = false;
29588       cp_parser_require (parser, CPP_COLON, RT_COLON);
29589       arg = cp_parser_assignment_expression (parser);
29590 
29591       sel_args
29592 	= chainon (sel_args,
29593 		   build_tree_list (selector, arg));
29594 
29595       token = cp_lexer_peek_token (parser->lexer);
29596     }
29597 
29598   /* Handle non-selector arguments, if any. */
29599   while (token->type == CPP_COMMA)
29600     {
29601       tree arg;
29602 
29603       cp_lexer_consume_token (parser->lexer);
29604       arg = cp_parser_assignment_expression (parser);
29605 
29606       addl_args
29607 	= chainon (addl_args,
29608 		   build_tree_list (NULL_TREE, arg));
29609 
29610       token = cp_lexer_peek_token (parser->lexer);
29611     }
29612 
29613   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29614     {
29615       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29616       return build_tree_list (error_mark_node, error_mark_node);
29617     }
29618 
29619   return build_tree_list (sel_args, addl_args);
29620 }
29621 
29622 /* Parse an Objective-C encode expression.
29623 
29624    objc-encode-expression:
29625      @encode objc-typename
29626 
29627    Returns an encoded representation of the type argument.  */
29628 
29629 static cp_expr
cp_parser_objc_encode_expression(cp_parser * parser)29630 cp_parser_objc_encode_expression (cp_parser* parser)
29631 {
29632   tree type;
29633   cp_token *token;
29634   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29635 
29636   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
29637   matching_parens parens;
29638   parens.require_open (parser);
29639   token = cp_lexer_peek_token (parser->lexer);
29640   type = complete_type (cp_parser_type_id (parser));
29641   parens.require_close (parser);
29642 
29643   if (!type)
29644     {
29645       error_at (token->location,
29646 		"%<@encode%> must specify a type as an argument");
29647       return error_mark_node;
29648     }
29649 
29650   /* This happens if we find @encode(T) (where T is a template
29651      typename or something dependent on a template typename) when
29652      parsing a template.  In that case, we can't compile it
29653      immediately, but we rather create an AT_ENCODE_EXPR which will
29654      need to be instantiated when the template is used.
29655   */
29656   if (dependent_type_p (type))
29657     {
29658       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29659       TREE_READONLY (value) = 1;
29660       return value;
29661     }
29662 
29663 
29664   /* Build a location of the form:
29665        @encode(int)
29666        ^~~~~~~~~~~~
29667      with caret==start at the @ token, finishing at the close paren.  */
29668   location_t combined_loc
29669     = make_location (start_loc, start_loc,
29670                      cp_lexer_previous_token (parser->lexer)->location);
29671 
29672   return cp_expr (objc_build_encode_expr (type), combined_loc);
29673 }
29674 
29675 /* Parse an Objective-C @defs expression.  */
29676 
29677 static tree
cp_parser_objc_defs_expression(cp_parser * parser)29678 cp_parser_objc_defs_expression (cp_parser *parser)
29679 {
29680   tree name;
29681 
29682   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
29683   matching_parens parens;
29684   parens.require_open (parser);
29685   name = cp_parser_identifier (parser);
29686   parens.require_close (parser);
29687 
29688   return objc_get_class_ivars (name);
29689 }
29690 
29691 /* Parse an Objective-C protocol expression.
29692 
29693   objc-protocol-expression:
29694     @protocol ( identifier )
29695 
29696   Returns a representation of the protocol expression.  */
29697 
29698 static tree
cp_parser_objc_protocol_expression(cp_parser * parser)29699 cp_parser_objc_protocol_expression (cp_parser* parser)
29700 {
29701   tree proto;
29702   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29703 
29704   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
29705   matching_parens parens;
29706   parens.require_open (parser);
29707   proto = cp_parser_identifier (parser);
29708   parens.require_close (parser);
29709 
29710   /* Build a location of the form:
29711        @protocol(prot)
29712        ^~~~~~~~~~~~~~~
29713      with caret==start at the @ token, finishing at the close paren.  */
29714   location_t combined_loc
29715     = make_location (start_loc, start_loc,
29716                      cp_lexer_previous_token (parser->lexer)->location);
29717   tree result = objc_build_protocol_expr (proto);
29718   protected_set_expr_location (result, combined_loc);
29719   return result;
29720 }
29721 
29722 /* Parse an Objective-C selector expression.
29723 
29724    objc-selector-expression:
29725      @selector ( objc-method-signature )
29726 
29727    objc-method-signature:
29728      objc-selector
29729      objc-selector-seq
29730 
29731    objc-selector-seq:
29732      objc-selector :
29733      objc-selector-seq objc-selector :
29734 
29735   Returns a representation of the method selector.  */
29736 
29737 static tree
cp_parser_objc_selector_expression(cp_parser * parser)29738 cp_parser_objc_selector_expression (cp_parser* parser)
29739 {
29740   tree sel_seq = NULL_TREE;
29741   bool maybe_unary_selector_p = true;
29742   cp_token *token;
29743   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29744 
29745   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
29746   matching_parens parens;
29747   parens.require_open (parser);
29748   token = cp_lexer_peek_token (parser->lexer);
29749 
29750   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29751 	 || token->type == CPP_SCOPE)
29752     {
29753       tree selector = NULL_TREE;
29754 
29755       if (token->type != CPP_COLON
29756 	  || token->type == CPP_SCOPE)
29757 	selector = cp_parser_objc_selector (parser);
29758 
29759       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29760 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29761 	{
29762 	  /* Detect if we have a unary selector.  */
29763 	  if (maybe_unary_selector_p)
29764 	    {
29765 	      sel_seq = selector;
29766 	      goto finish_selector;
29767 	    }
29768 	  else
29769 	    {
29770 	      cp_parser_error (parser, "expected %<:%>");
29771 	    }
29772 	}
29773       maybe_unary_selector_p = false;
29774       token = cp_lexer_consume_token (parser->lexer);
29775 
29776       if (token->type == CPP_SCOPE)
29777 	{
29778 	  sel_seq
29779 	    = chainon (sel_seq,
29780 		       build_tree_list (selector, NULL_TREE));
29781 	  sel_seq
29782 	    = chainon (sel_seq,
29783 		       build_tree_list (NULL_TREE, NULL_TREE));
29784 	}
29785       else
29786 	sel_seq
29787 	  = chainon (sel_seq,
29788 		     build_tree_list (selector, NULL_TREE));
29789 
29790       token = cp_lexer_peek_token (parser->lexer);
29791     }
29792 
29793  finish_selector:
29794   parens.require_close (parser);
29795 
29796 
29797   /* Build a location of the form:
29798        @selector(func)
29799        ^~~~~~~~~~~~~~~
29800      with caret==start at the @ token, finishing at the close paren.  */
29801   location_t combined_loc
29802     = make_location (loc, loc,
29803                      cp_lexer_previous_token (parser->lexer)->location);
29804   tree result = objc_build_selector_expr (combined_loc, sel_seq);
29805   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
29806   protected_set_expr_location (result, combined_loc);
29807   return result;
29808 }
29809 
29810 /* Parse a list of identifiers.
29811 
29812    objc-identifier-list:
29813      identifier
29814      objc-identifier-list , identifier
29815 
29816    Returns a TREE_LIST of identifier nodes.  */
29817 
29818 static tree
cp_parser_objc_identifier_list(cp_parser * parser)29819 cp_parser_objc_identifier_list (cp_parser* parser)
29820 {
29821   tree identifier;
29822   tree list;
29823   cp_token *sep;
29824 
29825   identifier = cp_parser_identifier (parser);
29826   if (identifier == error_mark_node)
29827     return error_mark_node;
29828 
29829   list = build_tree_list (NULL_TREE, identifier);
29830   sep = cp_lexer_peek_token (parser->lexer);
29831 
29832   while (sep->type == CPP_COMMA)
29833     {
29834       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
29835       identifier = cp_parser_identifier (parser);
29836       if (identifier == error_mark_node)
29837 	return list;
29838 
29839       list = chainon (list, build_tree_list (NULL_TREE,
29840 					     identifier));
29841       sep = cp_lexer_peek_token (parser->lexer);
29842     }
29843 
29844   return list;
29845 }
29846 
29847 /* Parse an Objective-C alias declaration.
29848 
29849    objc-alias-declaration:
29850      @compatibility_alias identifier identifier ;
29851 
29852    This function registers the alias mapping with the Objective-C front end.
29853    It returns nothing.  */
29854 
29855 static void
cp_parser_objc_alias_declaration(cp_parser * parser)29856 cp_parser_objc_alias_declaration (cp_parser* parser)
29857 {
29858   tree alias, orig;
29859 
29860   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
29861   alias = cp_parser_identifier (parser);
29862   orig = cp_parser_identifier (parser);
29863   objc_declare_alias (alias, orig);
29864   cp_parser_consume_semicolon_at_end_of_statement (parser);
29865 }
29866 
29867 /* Parse an Objective-C class forward-declaration.
29868 
29869    objc-class-declaration:
29870      @class objc-identifier-list ;
29871 
29872    The function registers the forward declarations with the Objective-C
29873    front end.  It returns nothing.  */
29874 
29875 static void
cp_parser_objc_class_declaration(cp_parser * parser)29876 cp_parser_objc_class_declaration (cp_parser* parser)
29877 {
29878   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
29879   while (true)
29880     {
29881       tree id;
29882 
29883       id = cp_parser_identifier (parser);
29884       if (id == error_mark_node)
29885 	break;
29886 
29887       objc_declare_class (id);
29888 
29889       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29890 	cp_lexer_consume_token (parser->lexer);
29891       else
29892 	break;
29893     }
29894   cp_parser_consume_semicolon_at_end_of_statement (parser);
29895 }
29896 
29897 /* Parse a list of Objective-C protocol references.
29898 
29899    objc-protocol-refs-opt:
29900      objc-protocol-refs [opt]
29901 
29902    objc-protocol-refs:
29903      < objc-identifier-list >
29904 
29905    Returns a TREE_LIST of identifiers, if any.  */
29906 
29907 static tree
cp_parser_objc_protocol_refs_opt(cp_parser * parser)29908 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29909 {
29910   tree protorefs = NULL_TREE;
29911 
29912   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29913     {
29914       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
29915       protorefs = cp_parser_objc_identifier_list (parser);
29916       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29917     }
29918 
29919   return protorefs;
29920 }
29921 
29922 /* Parse a Objective-C visibility specification.  */
29923 
29924 static void
cp_parser_objc_visibility_spec(cp_parser * parser)29925 cp_parser_objc_visibility_spec (cp_parser* parser)
29926 {
29927   cp_token *vis = cp_lexer_peek_token (parser->lexer);
29928 
29929   switch (vis->keyword)
29930     {
29931     case RID_AT_PRIVATE:
29932       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29933       break;
29934     case RID_AT_PROTECTED:
29935       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29936       break;
29937     case RID_AT_PUBLIC:
29938       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29939       break;
29940     case RID_AT_PACKAGE:
29941       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29942       break;
29943     default:
29944       return;
29945     }
29946 
29947   /* Eat '@private'/'@protected'/'@public'.  */
29948   cp_lexer_consume_token (parser->lexer);
29949 }
29950 
29951 /* Parse an Objective-C method type.  Return 'true' if it is a class
29952    (+) method, and 'false' if it is an instance (-) method.  */
29953 
29954 static inline bool
cp_parser_objc_method_type(cp_parser * parser)29955 cp_parser_objc_method_type (cp_parser* parser)
29956 {
29957   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29958     return true;
29959   else
29960     return false;
29961 }
29962 
29963 /* Parse an Objective-C protocol qualifier.  */
29964 
29965 static tree
cp_parser_objc_protocol_qualifiers(cp_parser * parser)29966 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29967 {
29968   tree quals = NULL_TREE, node;
29969   cp_token *token = cp_lexer_peek_token (parser->lexer);
29970 
29971   node = token->u.value;
29972 
29973   while (node && identifier_p (node)
29974 	 && (node == ridpointers [(int) RID_IN]
29975 	     || node == ridpointers [(int) RID_OUT]
29976 	     || node == ridpointers [(int) RID_INOUT]
29977 	     || node == ridpointers [(int) RID_BYCOPY]
29978 	     || node == ridpointers [(int) RID_BYREF]
29979 	     || node == ridpointers [(int) RID_ONEWAY]))
29980     {
29981       quals = tree_cons (NULL_TREE, node, quals);
29982       cp_lexer_consume_token (parser->lexer);
29983       token = cp_lexer_peek_token (parser->lexer);
29984       node = token->u.value;
29985     }
29986 
29987   return quals;
29988 }
29989 
29990 /* Parse an Objective-C typename.  */
29991 
29992 static tree
cp_parser_objc_typename(cp_parser * parser)29993 cp_parser_objc_typename (cp_parser* parser)
29994 {
29995   tree type_name = NULL_TREE;
29996 
29997   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29998     {
29999       tree proto_quals, cp_type = NULL_TREE;
30000 
30001       matching_parens parens;
30002       parens.consume_open (parser); /* Eat '('.  */
30003       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30004 
30005       /* An ObjC type name may consist of just protocol qualifiers, in which
30006 	 case the type shall default to 'id'.  */
30007       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30008 	{
30009 	  cp_type = cp_parser_type_id (parser);
30010 
30011 	  /* If the type could not be parsed, an error has already
30012 	     been produced.  For error recovery, behave as if it had
30013 	     not been specified, which will use the default type
30014 	     'id'.  */
30015 	  if (cp_type == error_mark_node)
30016 	    {
30017 	      cp_type = NULL_TREE;
30018 	      /* We need to skip to the closing parenthesis as
30019 		 cp_parser_type_id() does not seem to do it for
30020 		 us.  */
30021 	      cp_parser_skip_to_closing_parenthesis (parser,
30022 						     /*recovering=*/true,
30023 						     /*or_comma=*/false,
30024 						     /*consume_paren=*/false);
30025 	    }
30026 	}
30027 
30028       parens.require_close (parser);
30029       type_name = build_tree_list (proto_quals, cp_type);
30030     }
30031 
30032   return type_name;
30033 }
30034 
30035 /* Check to see if TYPE refers to an Objective-C selector name.  */
30036 
30037 static bool
cp_parser_objc_selector_p(enum cpp_ttype type)30038 cp_parser_objc_selector_p (enum cpp_ttype type)
30039 {
30040   return (type == CPP_NAME || type == CPP_KEYWORD
30041 	  || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30042 	  || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30043 	  || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30044 	  || type == CPP_XOR || type == CPP_XOR_EQ);
30045 }
30046 
30047 /* Parse an Objective-C selector.  */
30048 
30049 static tree
cp_parser_objc_selector(cp_parser * parser)30050 cp_parser_objc_selector (cp_parser* parser)
30051 {
30052   cp_token *token = cp_lexer_consume_token (parser->lexer);
30053 
30054   if (!cp_parser_objc_selector_p (token->type))
30055     {
30056       error_at (token->location, "invalid Objective-C++ selector name");
30057       return error_mark_node;
30058     }
30059 
30060   /* C++ operator names are allowed to appear in ObjC selectors.  */
30061   switch (token->type)
30062     {
30063     case CPP_AND_AND: return get_identifier ("and");
30064     case CPP_AND_EQ: return get_identifier ("and_eq");
30065     case CPP_AND: return get_identifier ("bitand");
30066     case CPP_OR: return get_identifier ("bitor");
30067     case CPP_COMPL: return get_identifier ("compl");
30068     case CPP_NOT: return get_identifier ("not");
30069     case CPP_NOT_EQ: return get_identifier ("not_eq");
30070     case CPP_OR_OR: return get_identifier ("or");
30071     case CPP_OR_EQ: return get_identifier ("or_eq");
30072     case CPP_XOR: return get_identifier ("xor");
30073     case CPP_XOR_EQ: return get_identifier ("xor_eq");
30074     default: return token->u.value;
30075     }
30076 }
30077 
30078 /* Parse an Objective-C params list.  */
30079 
30080 static tree
cp_parser_objc_method_keyword_params(cp_parser * parser,tree * attributes)30081 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30082 {
30083   tree params = NULL_TREE;
30084   bool maybe_unary_selector_p = true;
30085   cp_token *token = cp_lexer_peek_token (parser->lexer);
30086 
30087   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30088     {
30089       tree selector = NULL_TREE, type_name, identifier;
30090       tree parm_attr = NULL_TREE;
30091 
30092       if (token->keyword == RID_ATTRIBUTE)
30093 	break;
30094 
30095       if (token->type != CPP_COLON)
30096 	selector = cp_parser_objc_selector (parser);
30097 
30098       /* Detect if we have a unary selector.  */
30099       if (maybe_unary_selector_p
30100 	  && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30101 	{
30102 	  params = selector; /* Might be followed by attributes.  */
30103 	  break;
30104 	}
30105 
30106       maybe_unary_selector_p = false;
30107       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30108 	{
30109 	  /* Something went quite wrong.  There should be a colon
30110 	     here, but there is not.  Stop parsing parameters.  */
30111 	  break;
30112 	}
30113       type_name = cp_parser_objc_typename (parser);
30114       /* New ObjC allows attributes on parameters too.  */
30115       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30116 	parm_attr = cp_parser_attributes_opt (parser);
30117       identifier = cp_parser_identifier (parser);
30118 
30119       params
30120 	= chainon (params,
30121 		   objc_build_keyword_decl (selector,
30122 					    type_name,
30123 					    identifier,
30124 					    parm_attr));
30125 
30126       token = cp_lexer_peek_token (parser->lexer);
30127     }
30128 
30129   if (params == NULL_TREE)
30130     {
30131       cp_parser_error (parser, "objective-c++ method declaration is expected");
30132       return error_mark_node;
30133     }
30134 
30135   /* We allow tail attributes for the method.  */
30136   if (token->keyword == RID_ATTRIBUTE)
30137     {
30138       *attributes = cp_parser_attributes_opt (parser);
30139       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30140 	  || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30141 	return params;
30142       cp_parser_error (parser,
30143 		       "method attributes must be specified at the end");
30144       return error_mark_node;
30145     }
30146 
30147   if (params == NULL_TREE)
30148     {
30149       cp_parser_error (parser, "objective-c++ method declaration is expected");
30150       return error_mark_node;
30151     }
30152   return params;
30153 }
30154 
30155 /* Parse the non-keyword Objective-C params.  */
30156 
30157 static tree
cp_parser_objc_method_tail_params_opt(cp_parser * parser,bool * ellipsisp,tree * attributes)30158 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30159 				       tree* attributes)
30160 {
30161   tree params = make_node (TREE_LIST);
30162   cp_token *token = cp_lexer_peek_token (parser->lexer);
30163   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
30164 
30165   while (token->type == CPP_COMMA)
30166     {
30167       cp_parameter_declarator *parmdecl;
30168       tree parm;
30169 
30170       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30171       token = cp_lexer_peek_token (parser->lexer);
30172 
30173       if (token->type == CPP_ELLIPSIS)
30174 	{
30175 	  cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
30176 	  *ellipsisp = true;
30177 	  token = cp_lexer_peek_token (parser->lexer);
30178 	  break;
30179 	}
30180 
30181       /* TODO: parse attributes for tail parameters.  */
30182       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30183       parm = grokdeclarator (parmdecl->declarator,
30184 			     &parmdecl->decl_specifiers,
30185 			     PARM, /*initialized=*/0,
30186 			     /*attrlist=*/NULL);
30187 
30188       chainon (params, build_tree_list (NULL_TREE, parm));
30189       token = cp_lexer_peek_token (parser->lexer);
30190     }
30191 
30192   /* We allow tail attributes for the method.  */
30193   if (token->keyword == RID_ATTRIBUTE)
30194     {
30195       if (*attributes == NULL_TREE)
30196 	{
30197 	  *attributes = cp_parser_attributes_opt (parser);
30198 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30199 	      || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30200 	    return params;
30201 	}
30202       else
30203 	/* We have an error, but parse the attributes, so that we can
30204 	   carry on.  */
30205 	*attributes = cp_parser_attributes_opt (parser);
30206 
30207       cp_parser_error (parser,
30208 		       "method attributes must be specified at the end");
30209       return error_mark_node;
30210     }
30211 
30212   return params;
30213 }
30214 
30215 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
30216 
30217 static void
cp_parser_objc_interstitial_code(cp_parser * parser)30218 cp_parser_objc_interstitial_code (cp_parser* parser)
30219 {
30220   cp_token *token = cp_lexer_peek_token (parser->lexer);
30221 
30222   /* If the next token is `extern' and the following token is a string
30223      literal, then we have a linkage specification.  */
30224   if (token->keyword == RID_EXTERN
30225       && cp_parser_is_pure_string_literal
30226 	 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30227     cp_parser_linkage_specification (parser);
30228   /* Handle #pragma, if any.  */
30229   else if (token->type == CPP_PRAGMA)
30230     cp_parser_pragma (parser, pragma_objc_icode, NULL);
30231   /* Allow stray semicolons.  */
30232   else if (token->type == CPP_SEMICOLON)
30233     cp_lexer_consume_token (parser->lexer);
30234   /* Mark methods as optional or required, when building protocols.  */
30235   else if (token->keyword == RID_AT_OPTIONAL)
30236     {
30237       cp_lexer_consume_token (parser->lexer);
30238       objc_set_method_opt (true);
30239     }
30240   else if (token->keyword == RID_AT_REQUIRED)
30241     {
30242       cp_lexer_consume_token (parser->lexer);
30243       objc_set_method_opt (false);
30244     }
30245   else if (token->keyword == RID_NAMESPACE)
30246     cp_parser_namespace_definition (parser);
30247   /* Other stray characters must generate errors.  */
30248   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30249     {
30250       cp_lexer_consume_token (parser->lexer);
30251       error ("stray %qs between Objective-C++ methods",
30252 	     token->type == CPP_OPEN_BRACE ? "{" : "}");
30253     }
30254   /* Finally, try to parse a block-declaration, or a function-definition.  */
30255   else
30256     cp_parser_block_declaration (parser, /*statement_p=*/false);
30257 }
30258 
30259 /* Parse a method signature.  */
30260 
30261 static tree
cp_parser_objc_method_signature(cp_parser * parser,tree * attributes)30262 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30263 {
30264   tree rettype, kwdparms, optparms;
30265   bool ellipsis = false;
30266   bool is_class_method;
30267 
30268   is_class_method = cp_parser_objc_method_type (parser);
30269   rettype = cp_parser_objc_typename (parser);
30270   *attributes = NULL_TREE;
30271   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30272   if (kwdparms == error_mark_node)
30273     return error_mark_node;
30274   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30275   if (optparms == error_mark_node)
30276     return error_mark_node;
30277 
30278   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30279 }
30280 
30281 static bool
cp_parser_objc_method_maybe_bad_prefix_attributes(cp_parser * parser)30282 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30283 {
30284   tree tattr;
30285   cp_lexer_save_tokens (parser->lexer);
30286   tattr = cp_parser_attributes_opt (parser);
30287   gcc_assert (tattr) ;
30288 
30289   /* If the attributes are followed by a method introducer, this is not allowed.
30290      Dump the attributes and flag the situation.  */
30291   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30292       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30293     return true;
30294 
30295   /* Otherwise, the attributes introduce some interstitial code, possibly so
30296      rewind to allow that check.  */
30297   cp_lexer_rollback_tokens (parser->lexer);
30298   return false;
30299 }
30300 
30301 /* Parse an Objective-C method prototype list.  */
30302 
30303 static void
cp_parser_objc_method_prototype_list(cp_parser * parser)30304 cp_parser_objc_method_prototype_list (cp_parser* parser)
30305 {
30306   cp_token *token = cp_lexer_peek_token (parser->lexer);
30307 
30308   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30309     {
30310       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30311 	{
30312 	  tree attributes, sig;
30313 	  bool is_class_method;
30314 	  if (token->type == CPP_PLUS)
30315 	    is_class_method = true;
30316 	  else
30317 	    is_class_method = false;
30318 	  sig = cp_parser_objc_method_signature (parser, &attributes);
30319 	  if (sig == error_mark_node)
30320 	    {
30321 	      cp_parser_skip_to_end_of_block_or_statement (parser);
30322 	      token = cp_lexer_peek_token (parser->lexer);
30323 	      continue;
30324 	    }
30325 	  objc_add_method_declaration (is_class_method, sig, attributes);
30326 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
30327 	}
30328       else if (token->keyword == RID_AT_PROPERTY)
30329 	cp_parser_objc_at_property_declaration (parser);
30330       else if (token->keyword == RID_ATTRIBUTE
30331       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30332 	warning_at (cp_lexer_peek_token (parser->lexer)->location,
30333 		    OPT_Wattributes,
30334 		    "prefix attributes are ignored for methods");
30335       else
30336 	/* Allow for interspersed non-ObjC++ code.  */
30337 	cp_parser_objc_interstitial_code (parser);
30338 
30339       token = cp_lexer_peek_token (parser->lexer);
30340     }
30341 
30342   if (token->type != CPP_EOF)
30343     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30344   else
30345     cp_parser_error (parser, "expected %<@end%>");
30346 
30347   objc_finish_interface ();
30348 }
30349 
30350 /* Parse an Objective-C method definition list.  */
30351 
30352 static void
cp_parser_objc_method_definition_list(cp_parser * parser)30353 cp_parser_objc_method_definition_list (cp_parser* parser)
30354 {
30355   cp_token *token = cp_lexer_peek_token (parser->lexer);
30356 
30357   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30358     {
30359       tree meth;
30360 
30361       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30362 	{
30363 	  cp_token *ptk;
30364 	  tree sig, attribute;
30365 	  bool is_class_method;
30366 	  if (token->type == CPP_PLUS)
30367 	    is_class_method = true;
30368 	  else
30369 	    is_class_method = false;
30370 	  push_deferring_access_checks (dk_deferred);
30371 	  sig = cp_parser_objc_method_signature (parser, &attribute);
30372 	  if (sig == error_mark_node)
30373 	    {
30374 	      cp_parser_skip_to_end_of_block_or_statement (parser);
30375 	      token = cp_lexer_peek_token (parser->lexer);
30376 	      continue;
30377 	    }
30378 	  objc_start_method_definition (is_class_method, sig, attribute,
30379 					NULL_TREE);
30380 
30381 	  /* For historical reasons, we accept an optional semicolon.  */
30382 	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30383 	    cp_lexer_consume_token (parser->lexer);
30384 
30385 	  ptk = cp_lexer_peek_token (parser->lexer);
30386 	  if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30387 		|| ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30388 	    {
30389 	      perform_deferred_access_checks (tf_warning_or_error);
30390 	      stop_deferring_access_checks ();
30391 	      meth = cp_parser_function_definition_after_declarator (parser,
30392 								     false);
30393 	      pop_deferring_access_checks ();
30394 	      objc_finish_method_definition (meth);
30395 	    }
30396 	}
30397       /* The following case will be removed once @synthesize is
30398 	 completely implemented.  */
30399       else if (token->keyword == RID_AT_PROPERTY)
30400 	cp_parser_objc_at_property_declaration (parser);
30401       else if (token->keyword == RID_AT_SYNTHESIZE)
30402 	cp_parser_objc_at_synthesize_declaration (parser);
30403       else if (token->keyword == RID_AT_DYNAMIC)
30404 	cp_parser_objc_at_dynamic_declaration (parser);
30405       else if (token->keyword == RID_ATTRIBUTE
30406       	       && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30407 	warning_at (token->location, OPT_Wattributes,
30408 	       	    "prefix attributes are ignored for methods");
30409       else
30410 	/* Allow for interspersed non-ObjC++ code.  */
30411 	cp_parser_objc_interstitial_code (parser);
30412 
30413       token = cp_lexer_peek_token (parser->lexer);
30414     }
30415 
30416   if (token->type != CPP_EOF)
30417     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30418   else
30419     cp_parser_error (parser, "expected %<@end%>");
30420 
30421   objc_finish_implementation ();
30422 }
30423 
30424 /* Parse Objective-C ivars.  */
30425 
30426 static void
cp_parser_objc_class_ivars(cp_parser * parser)30427 cp_parser_objc_class_ivars (cp_parser* parser)
30428 {
30429   cp_token *token = cp_lexer_peek_token (parser->lexer);
30430 
30431   if (token->type != CPP_OPEN_BRACE)
30432     return;	/* No ivars specified.  */
30433 
30434   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
30435   token = cp_lexer_peek_token (parser->lexer);
30436 
30437   while (token->type != CPP_CLOSE_BRACE
30438 	&& token->keyword != RID_AT_END && token->type != CPP_EOF)
30439     {
30440       cp_decl_specifier_seq declspecs;
30441       int decl_class_or_enum_p;
30442       tree prefix_attributes;
30443 
30444       cp_parser_objc_visibility_spec (parser);
30445 
30446       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30447 	break;
30448 
30449       cp_parser_decl_specifier_seq (parser,
30450 				    CP_PARSER_FLAGS_OPTIONAL,
30451 				    &declspecs,
30452 				    &decl_class_or_enum_p);
30453 
30454       /* auto, register, static, extern, mutable.  */
30455       if (declspecs.storage_class != sc_none)
30456 	{
30457 	  cp_parser_error (parser, "invalid type for instance variable");
30458 	  declspecs.storage_class = sc_none;
30459 	}
30460 
30461       /* thread_local.  */
30462       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30463 	{
30464 	  cp_parser_error (parser, "invalid type for instance variable");
30465 	  declspecs.locations[ds_thread] = 0;
30466 	}
30467 
30468       /* typedef.  */
30469       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30470 	{
30471 	  cp_parser_error (parser, "invalid type for instance variable");
30472 	  declspecs.locations[ds_typedef] = 0;
30473 	}
30474 
30475       prefix_attributes = declspecs.attributes;
30476       declspecs.attributes = NULL_TREE;
30477 
30478       /* Keep going until we hit the `;' at the end of the
30479 	 declaration.  */
30480       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30481 	{
30482 	  tree width = NULL_TREE, attributes, first_attribute, decl;
30483 	  cp_declarator *declarator = NULL;
30484 	  int ctor_dtor_or_conv_p;
30485 
30486 	  /* Check for a (possibly unnamed) bitfield declaration.  */
30487 	  token = cp_lexer_peek_token (parser->lexer);
30488 	  if (token->type == CPP_COLON)
30489 	    goto eat_colon;
30490 
30491 	  if (token->type == CPP_NAME
30492 	      && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30493 		  == CPP_COLON))
30494 	    {
30495 	      /* Get the name of the bitfield.  */
30496 	      declarator = make_id_declarator (NULL_TREE,
30497 					       cp_parser_identifier (parser),
30498 					       sfk_none);
30499 
30500 	     eat_colon:
30501 	      cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
30502 	      /* Get the width of the bitfield.  */
30503 	      width
30504 		= cp_parser_constant_expression (parser);
30505 	    }
30506 	  else
30507 	    {
30508 	      /* Parse the declarator.  */
30509 	      declarator
30510 		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30511 					&ctor_dtor_or_conv_p,
30512 					/*parenthesized_p=*/NULL,
30513 					/*member_p=*/false,
30514 					/*friend_p=*/false);
30515 	    }
30516 
30517 	  /* Look for attributes that apply to the ivar.  */
30518 	  attributes = cp_parser_attributes_opt (parser);
30519 	  /* Remember which attributes are prefix attributes and
30520 	     which are not.  */
30521 	  first_attribute = attributes;
30522 	  /* Combine the attributes.  */
30523 	  attributes = attr_chainon (prefix_attributes, attributes);
30524 
30525 	  if (width)
30526 	    /* Create the bitfield declaration.  */
30527 	    decl = grokbitfield (declarator, &declspecs,
30528 				 width, NULL_TREE, attributes);
30529 	  else
30530 	    decl = grokfield (declarator, &declspecs,
30531 			      NULL_TREE, /*init_const_expr_p=*/false,
30532 			      NULL_TREE, attributes);
30533 
30534 	  /* Add the instance variable.  */
30535 	  if (decl != error_mark_node && decl != NULL_TREE)
30536 	    objc_add_instance_variable (decl);
30537 
30538 	  /* Reset PREFIX_ATTRIBUTES.  */
30539 	  if (attributes != error_mark_node)
30540 	    {
30541 	      while (attributes && TREE_CHAIN (attributes) != first_attribute)
30542 		attributes = TREE_CHAIN (attributes);
30543 	      if (attributes)
30544 		TREE_CHAIN (attributes) = NULL_TREE;
30545 	    }
30546 
30547 	  token = cp_lexer_peek_token (parser->lexer);
30548 
30549 	  if (token->type == CPP_COMMA)
30550 	    {
30551 	      cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30552 	      continue;
30553 	    }
30554 	  break;
30555 	}
30556 
30557       cp_parser_consume_semicolon_at_end_of_statement (parser);
30558       token = cp_lexer_peek_token (parser->lexer);
30559     }
30560 
30561   if (token->keyword == RID_AT_END)
30562     cp_parser_error (parser, "expected %<}%>");
30563 
30564   /* Do not consume the RID_AT_END, so it will be read again as terminating
30565      the @interface of @implementation.  */
30566   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30567     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
30568 
30569   /* For historical reasons, we accept an optional semicolon.  */
30570   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30571     cp_lexer_consume_token (parser->lexer);
30572 }
30573 
30574 /* Parse an Objective-C protocol declaration.  */
30575 
30576 static void
cp_parser_objc_protocol_declaration(cp_parser * parser,tree attributes)30577 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30578 {
30579   tree proto, protorefs;
30580   cp_token *tok;
30581 
30582   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
30583   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30584     {
30585       tok = cp_lexer_peek_token (parser->lexer);
30586       error_at (tok->location, "identifier expected after %<@protocol%>");
30587       cp_parser_consume_semicolon_at_end_of_statement (parser);
30588       return;
30589     }
30590 
30591   /* See if we have a forward declaration or a definition.  */
30592   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30593 
30594   /* Try a forward declaration first.  */
30595   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30596     {
30597       while (true)
30598 	{
30599 	  tree id;
30600 
30601 	  id = cp_parser_identifier (parser);
30602 	  if (id == error_mark_node)
30603 	    break;
30604 
30605 	  objc_declare_protocol (id, attributes);
30606 
30607 	  if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30608 	    cp_lexer_consume_token (parser->lexer);
30609 	  else
30610 	    break;
30611 	}
30612       cp_parser_consume_semicolon_at_end_of_statement (parser);
30613     }
30614 
30615   /* Ok, we got a full-fledged definition (or at least should).  */
30616   else
30617     {
30618       proto = cp_parser_identifier (parser);
30619       protorefs = cp_parser_objc_protocol_refs_opt (parser);
30620       objc_start_protocol (proto, protorefs, attributes);
30621       cp_parser_objc_method_prototype_list (parser);
30622     }
30623 }
30624 
30625 /* Parse an Objective-C superclass or category.  */
30626 
30627 static void
cp_parser_objc_superclass_or_category(cp_parser * parser,bool iface_p,tree * super,tree * categ,bool * is_class_extension)30628 cp_parser_objc_superclass_or_category (cp_parser *parser,
30629 				       bool iface_p,
30630 				       tree *super,
30631 				       tree *categ, bool *is_class_extension)
30632 {
30633   cp_token *next = cp_lexer_peek_token (parser->lexer);
30634 
30635   *super = *categ = NULL_TREE;
30636   *is_class_extension = false;
30637   if (next->type == CPP_COLON)
30638     {
30639       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
30640       *super = cp_parser_identifier (parser);
30641     }
30642   else if (next->type == CPP_OPEN_PAREN)
30643     {
30644       matching_parens parens;
30645       parens.consume_open (parser);  /* Eat '('.  */
30646 
30647       /* If there is no category name, and this is an @interface, we
30648 	 have a class extension.  */
30649       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30650 	{
30651 	  *categ = NULL_TREE;
30652 	  *is_class_extension = true;
30653 	}
30654       else
30655 	*categ = cp_parser_identifier (parser);
30656 
30657       parens.require_close (parser);
30658     }
30659 }
30660 
30661 /* Parse an Objective-C class interface.  */
30662 
30663 static void
cp_parser_objc_class_interface(cp_parser * parser,tree attributes)30664 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30665 {
30666   tree name, super, categ, protos;
30667   bool is_class_extension;
30668 
30669   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
30670   name = cp_parser_identifier (parser);
30671   if (name == error_mark_node)
30672     {
30673       /* It's hard to recover because even if valid @interface stuff
30674 	 is to follow, we can't compile it (or validate it) if we
30675 	 don't even know which class it refers to.  Let's assume this
30676 	 was a stray '@interface' token in the stream and skip it.
30677       */
30678       return;
30679     }
30680   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30681 					 &is_class_extension);
30682   protos = cp_parser_objc_protocol_refs_opt (parser);
30683 
30684   /* We have either a class or a category on our hands.  */
30685   if (categ || is_class_extension)
30686     objc_start_category_interface (name, categ, protos, attributes);
30687   else
30688     {
30689       objc_start_class_interface (name, super, protos, attributes);
30690       /* Handle instance variable declarations, if any.  */
30691       cp_parser_objc_class_ivars (parser);
30692       objc_continue_interface ();
30693     }
30694 
30695   cp_parser_objc_method_prototype_list (parser);
30696 }
30697 
30698 /* Parse an Objective-C class implementation.  */
30699 
30700 static void
cp_parser_objc_class_implementation(cp_parser * parser)30701 cp_parser_objc_class_implementation (cp_parser* parser)
30702 {
30703   tree name, super, categ;
30704   bool is_class_extension;
30705 
30706   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
30707   name = cp_parser_identifier (parser);
30708   if (name == error_mark_node)
30709     {
30710       /* It's hard to recover because even if valid @implementation
30711 	 stuff is to follow, we can't compile it (or validate it) if
30712 	 we don't even know which class it refers to.  Let's assume
30713 	 this was a stray '@implementation' token in the stream and
30714 	 skip it.
30715       */
30716       return;
30717     }
30718   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30719 					 &is_class_extension);
30720 
30721   /* We have either a class or a category on our hands.  */
30722   if (categ)
30723     objc_start_category_implementation (name, categ);
30724   else
30725     {
30726       objc_start_class_implementation (name, super);
30727       /* Handle instance variable declarations, if any.  */
30728       cp_parser_objc_class_ivars (parser);
30729       objc_continue_implementation ();
30730     }
30731 
30732   cp_parser_objc_method_definition_list (parser);
30733 }
30734 
30735 /* Consume the @end token and finish off the implementation.  */
30736 
30737 static void
cp_parser_objc_end_implementation(cp_parser * parser)30738 cp_parser_objc_end_implementation (cp_parser* parser)
30739 {
30740   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30741   objc_finish_implementation ();
30742 }
30743 
30744 /* Parse an Objective-C declaration.  */
30745 
30746 static void
cp_parser_objc_declaration(cp_parser * parser,tree attributes)30747 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30748 {
30749   /* Try to figure out what kind of declaration is present.  */
30750   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30751 
30752   if (attributes)
30753     switch (kwd->keyword)
30754       {
30755 	case RID_AT_ALIAS:
30756 	case RID_AT_CLASS:
30757 	case RID_AT_END:
30758 	  error_at (kwd->location, "attributes may not be specified before"
30759 	            " the %<@%D%> Objective-C++ keyword",
30760 		    kwd->u.value);
30761 	  attributes = NULL;
30762 	  break;
30763 	case RID_AT_IMPLEMENTATION:
30764 	  warning_at (kwd->location, OPT_Wattributes,
30765 		      "prefix attributes are ignored before %<@%D%>",
30766 		      kwd->u.value);
30767 	  attributes = NULL;
30768 	default:
30769 	  break;
30770       }
30771 
30772   switch (kwd->keyword)
30773     {
30774     case RID_AT_ALIAS:
30775       cp_parser_objc_alias_declaration (parser);
30776       break;
30777     case RID_AT_CLASS:
30778       cp_parser_objc_class_declaration (parser);
30779       break;
30780     case RID_AT_PROTOCOL:
30781       cp_parser_objc_protocol_declaration (parser, attributes);
30782       break;
30783     case RID_AT_INTERFACE:
30784       cp_parser_objc_class_interface (parser, attributes);
30785       break;
30786     case RID_AT_IMPLEMENTATION:
30787       cp_parser_objc_class_implementation (parser);
30788       break;
30789     case RID_AT_END:
30790       cp_parser_objc_end_implementation (parser);
30791       break;
30792     default:
30793       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30794 		kwd->u.value);
30795       cp_parser_skip_to_end_of_block_or_statement (parser);
30796     }
30797 }
30798 
30799 /* Parse an Objective-C try-catch-finally statement.
30800 
30801    objc-try-catch-finally-stmt:
30802      @try compound-statement objc-catch-clause-seq [opt]
30803        objc-finally-clause [opt]
30804 
30805    objc-catch-clause-seq:
30806      objc-catch-clause objc-catch-clause-seq [opt]
30807 
30808    objc-catch-clause:
30809      @catch ( objc-exception-declaration ) compound-statement
30810 
30811    objc-finally-clause:
30812      @finally compound-statement
30813 
30814    objc-exception-declaration:
30815      parameter-declaration
30816      '...'
30817 
30818    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30819 
30820    Returns NULL_TREE.
30821 
30822    PS: This function is identical to c_parser_objc_try_catch_finally_statement
30823    for C.  Keep them in sync.  */
30824 
30825 static tree
cp_parser_objc_try_catch_finally_statement(cp_parser * parser)30826 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30827 {
30828   location_t location;
30829   tree stmt;
30830 
30831   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30832   location = cp_lexer_peek_token (parser->lexer)->location;
30833   objc_maybe_warn_exceptions (location);
30834   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30835      node, lest it get absorbed into the surrounding block.  */
30836   stmt = push_stmt_list ();
30837   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30838   objc_begin_try_stmt (location, pop_stmt_list (stmt));
30839 
30840   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30841     {
30842       cp_parameter_declarator *parm;
30843       tree parameter_declaration = error_mark_node;
30844       bool seen_open_paren = false;
30845       matching_parens parens;
30846 
30847       cp_lexer_consume_token (parser->lexer);
30848       if (parens.require_open (parser))
30849 	seen_open_paren = true;
30850       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30851 	{
30852 	  /* We have "@catch (...)" (where the '...' are literally
30853 	     what is in the code).  Skip the '...'.
30854 	     parameter_declaration is set to NULL_TREE, and
30855 	     objc_being_catch_clauses() knows that that means
30856 	     '...'.  */
30857 	  cp_lexer_consume_token (parser->lexer);
30858 	  parameter_declaration = NULL_TREE;
30859 	}
30860       else
30861 	{
30862 	  /* We have "@catch (NSException *exception)" or something
30863 	     like that.  Parse the parameter declaration.  */
30864 	  parm = cp_parser_parameter_declaration (parser, false, NULL);
30865 	  if (parm == NULL)
30866 	    parameter_declaration = error_mark_node;
30867 	  else
30868 	    parameter_declaration = grokdeclarator (parm->declarator,
30869 						    &parm->decl_specifiers,
30870 						    PARM, /*initialized=*/0,
30871 						    /*attrlist=*/NULL);
30872 	}
30873       if (seen_open_paren)
30874 	parens.require_close (parser);
30875       else
30876 	{
30877 	  /* If there was no open parenthesis, we are recovering from
30878 	     an error, and we are trying to figure out what mistake
30879 	     the user has made.  */
30880 
30881 	  /* If there is an immediate closing parenthesis, the user
30882 	     probably forgot the opening one (ie, they typed "@catch
30883 	     NSException *e)".  Parse the closing parenthesis and keep
30884 	     going.  */
30885 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30886 	    cp_lexer_consume_token (parser->lexer);
30887 
30888 	  /* If these is no immediate closing parenthesis, the user
30889 	     probably doesn't know that parenthesis are required at
30890 	     all (ie, they typed "@catch NSException *e").  So, just
30891 	     forget about the closing parenthesis and keep going.  */
30892 	}
30893       objc_begin_catch_clause (parameter_declaration);
30894       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30895       objc_finish_catch_clause ();
30896     }
30897   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30898     {
30899       cp_lexer_consume_token (parser->lexer);
30900       location = cp_lexer_peek_token (parser->lexer)->location;
30901       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30902 	 node, lest it get absorbed into the surrounding block.  */
30903       stmt = push_stmt_list ();
30904       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30905       objc_build_finally_clause (location, pop_stmt_list (stmt));
30906     }
30907 
30908   return objc_finish_try_stmt ();
30909 }
30910 
30911 /* Parse an Objective-C synchronized statement.
30912 
30913    objc-synchronized-stmt:
30914      @synchronized ( expression ) compound-statement
30915 
30916    Returns NULL_TREE.  */
30917 
30918 static tree
cp_parser_objc_synchronized_statement(cp_parser * parser)30919 cp_parser_objc_synchronized_statement (cp_parser *parser)
30920 {
30921   location_t location;
30922   tree lock, stmt;
30923 
30924   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30925 
30926   location = cp_lexer_peek_token (parser->lexer)->location;
30927   objc_maybe_warn_exceptions (location);
30928   matching_parens parens;
30929   parens.require_open (parser);
30930   lock = cp_parser_expression (parser);
30931   parens.require_close (parser);
30932 
30933   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30934      node, lest it get absorbed into the surrounding block.  */
30935   stmt = push_stmt_list ();
30936   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30937 
30938   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30939 }
30940 
30941 /* Parse an Objective-C throw statement.
30942 
30943    objc-throw-stmt:
30944      @throw assignment-expression [opt] ;
30945 
30946    Returns a constructed '@throw' statement.  */
30947 
30948 static tree
cp_parser_objc_throw_statement(cp_parser * parser)30949 cp_parser_objc_throw_statement (cp_parser *parser)
30950 {
30951   tree expr = NULL_TREE;
30952   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30953 
30954   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30955 
30956   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30957     expr = cp_parser_expression (parser);
30958 
30959   cp_parser_consume_semicolon_at_end_of_statement (parser);
30960 
30961   return objc_build_throw_stmt (loc, expr);
30962 }
30963 
30964 /* Parse an Objective-C statement.  */
30965 
30966 static tree
cp_parser_objc_statement(cp_parser * parser)30967 cp_parser_objc_statement (cp_parser * parser)
30968 {
30969   /* Try to figure out what kind of declaration is present.  */
30970   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30971 
30972   switch (kwd->keyword)
30973     {
30974     case RID_AT_TRY:
30975       return cp_parser_objc_try_catch_finally_statement (parser);
30976     case RID_AT_SYNCHRONIZED:
30977       return cp_parser_objc_synchronized_statement (parser);
30978     case RID_AT_THROW:
30979       return cp_parser_objc_throw_statement (parser);
30980     default:
30981       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30982 	       kwd->u.value);
30983       cp_parser_skip_to_end_of_block_or_statement (parser);
30984     }
30985 
30986   return error_mark_node;
30987 }
30988 
30989 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30990    look ahead to see if an objc keyword follows the attributes.  This
30991    is to detect the use of prefix attributes on ObjC @interface and
30992    @protocol.  */
30993 
30994 static bool
cp_parser_objc_valid_prefix_attributes(cp_parser * parser,tree * attrib)30995 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30996 {
30997   cp_lexer_save_tokens (parser->lexer);
30998   *attrib = cp_parser_attributes_opt (parser);
30999   gcc_assert (*attrib);
31000   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31001     {
31002       cp_lexer_commit_tokens (parser->lexer);
31003       return true;
31004     }
31005   cp_lexer_rollback_tokens (parser->lexer);
31006   return false;
31007 }
31008 
31009 /* This routine is a minimal replacement for
31010    c_parser_struct_declaration () used when parsing the list of
31011    types/names or ObjC++ properties.  For example, when parsing the
31012    code
31013 
31014    @property (readonly) int a, b, c;
31015 
31016    this function is responsible for parsing "int a, int b, int c" and
31017    returning the declarations as CHAIN of DECLs.
31018 
31019    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
31020    similar parsing.  */
31021 static tree
cp_parser_objc_struct_declaration(cp_parser * parser)31022 cp_parser_objc_struct_declaration (cp_parser *parser)
31023 {
31024   tree decls = NULL_TREE;
31025   cp_decl_specifier_seq declspecs;
31026   int decl_class_or_enum_p;
31027   tree prefix_attributes;
31028 
31029   cp_parser_decl_specifier_seq (parser,
31030 				CP_PARSER_FLAGS_NONE,
31031 				&declspecs,
31032 				&decl_class_or_enum_p);
31033 
31034   if (declspecs.type == error_mark_node)
31035     return error_mark_node;
31036 
31037   /* auto, register, static, extern, mutable.  */
31038   if (declspecs.storage_class != sc_none)
31039     {
31040       cp_parser_error (parser, "invalid type for property");
31041       declspecs.storage_class = sc_none;
31042     }
31043 
31044   /* thread_local.  */
31045   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31046     {
31047       cp_parser_error (parser, "invalid type for property");
31048       declspecs.locations[ds_thread] = 0;
31049     }
31050 
31051   /* typedef.  */
31052   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31053     {
31054       cp_parser_error (parser, "invalid type for property");
31055       declspecs.locations[ds_typedef] = 0;
31056     }
31057 
31058   prefix_attributes = declspecs.attributes;
31059   declspecs.attributes = NULL_TREE;
31060 
31061   /* Keep going until we hit the `;' at the end of the declaration. */
31062   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31063     {
31064       tree attributes, first_attribute, decl;
31065       cp_declarator *declarator;
31066       cp_token *token;
31067 
31068       /* Parse the declarator.  */
31069       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31070 					 NULL, NULL, false, false);
31071 
31072       /* Look for attributes that apply to the ivar.  */
31073       attributes = cp_parser_attributes_opt (parser);
31074       /* Remember which attributes are prefix attributes and
31075 	 which are not.  */
31076       first_attribute = attributes;
31077       /* Combine the attributes.  */
31078       attributes = attr_chainon (prefix_attributes, attributes);
31079 
31080       decl = grokfield (declarator, &declspecs,
31081 			NULL_TREE, /*init_const_expr_p=*/false,
31082 			NULL_TREE, attributes);
31083 
31084       if (decl == error_mark_node || decl == NULL_TREE)
31085 	return error_mark_node;
31086 
31087       /* Reset PREFIX_ATTRIBUTES.  */
31088       if (attributes != error_mark_node)
31089 	{
31090 	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
31091 	    attributes = TREE_CHAIN (attributes);
31092 	  if (attributes)
31093 	    TREE_CHAIN (attributes) = NULL_TREE;
31094 	}
31095 
31096       DECL_CHAIN (decl) = decls;
31097       decls = decl;
31098 
31099       token = cp_lexer_peek_token (parser->lexer);
31100       if (token->type == CPP_COMMA)
31101 	{
31102 	  cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
31103 	  continue;
31104 	}
31105       else
31106 	break;
31107     }
31108   return decls;
31109 }
31110 
31111 /* Parse an Objective-C @property declaration.  The syntax is:
31112 
31113    objc-property-declaration:
31114      '@property' objc-property-attributes[opt] struct-declaration ;
31115 
31116    objc-property-attributes:
31117     '(' objc-property-attribute-list ')'
31118 
31119    objc-property-attribute-list:
31120      objc-property-attribute
31121      objc-property-attribute-list, objc-property-attribute
31122 
31123    objc-property-attribute
31124      'getter' = identifier
31125      'setter' = identifier
31126      'readonly'
31127      'readwrite'
31128      'assign'
31129      'retain'
31130      'copy'
31131      'nonatomic'
31132 
31133   For example:
31134     @property NSString *name;
31135     @property (readonly) id object;
31136     @property (retain, nonatomic, getter=getTheName) id name;
31137     @property int a, b, c;
31138 
31139    PS: This function is identical to
31140    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
31141 static void
cp_parser_objc_at_property_declaration(cp_parser * parser)31142 cp_parser_objc_at_property_declaration (cp_parser *parser)
31143 {
31144   /* The following variables hold the attributes of the properties as
31145      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
31146      seen.  When we see an attribute, we set them to 'true' (if they
31147      are boolean properties) or to the identifier (if they have an
31148      argument, ie, for getter and setter).  Note that here we only
31149      parse the list of attributes, check the syntax and accumulate the
31150      attributes that we find.  objc_add_property_declaration() will
31151      then process the information.  */
31152   bool property_assign = false;
31153   bool property_copy = false;
31154   tree property_getter_ident = NULL_TREE;
31155   bool property_nonatomic = false;
31156   bool property_readonly = false;
31157   bool property_readwrite = false;
31158   bool property_retain = false;
31159   tree property_setter_ident = NULL_TREE;
31160 
31161   /* 'properties' is the list of properties that we read.  Usually a
31162      single one, but maybe more (eg, in "@property int a, b, c;" there
31163      are three).  */
31164   tree properties;
31165   location_t loc;
31166 
31167   loc = cp_lexer_peek_token (parser->lexer)->location;
31168 
31169   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
31170 
31171   /* Parse the optional attribute list...  */
31172   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31173     {
31174       /* Eat the '('.  */
31175       matching_parens parens;
31176       parens.consume_open (parser);
31177 
31178       while (true)
31179 	{
31180 	  bool syntax_error = false;
31181 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
31182       	  enum rid keyword;
31183 
31184 	  if (token->type != CPP_NAME)
31185 	    {
31186 	      cp_parser_error (parser, "expected identifier");
31187 	      break;
31188 	    }
31189 	  keyword = C_RID_CODE (token->u.value);
31190 	  cp_lexer_consume_token (parser->lexer);
31191 	  switch (keyword)
31192 	    {
31193 	    case RID_ASSIGN:    property_assign = true;    break;
31194 	    case RID_COPY:      property_copy = true;      break;
31195 	    case RID_NONATOMIC: property_nonatomic = true; break;
31196 	    case RID_READONLY:  property_readonly = true;  break;
31197 	    case RID_READWRITE: property_readwrite = true; break;
31198 	    case RID_RETAIN:    property_retain = true;    break;
31199 
31200 	    case RID_GETTER:
31201 	    case RID_SETTER:
31202 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31203 		{
31204 		  if (keyword == RID_GETTER)
31205 		    cp_parser_error (parser,
31206 				     "missing %<=%> (after %<getter%> attribute)");
31207 		  else
31208 		    cp_parser_error (parser,
31209 				     "missing %<=%> (after %<setter%> attribute)");
31210 		  syntax_error = true;
31211 		  break;
31212 		}
31213 	      cp_lexer_consume_token (parser->lexer); /* eat the = */
31214 	      if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31215 		{
31216 		  cp_parser_error (parser, "expected identifier");
31217 		  syntax_error = true;
31218 		  break;
31219 		}
31220 	      if (keyword == RID_SETTER)
31221 		{
31222 		  if (property_setter_ident != NULL_TREE)
31223 		    {
31224 		      cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31225 		      cp_lexer_consume_token (parser->lexer);
31226 		    }
31227 		  else
31228 		    property_setter_ident = cp_parser_objc_selector (parser);
31229 		  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31230 		    cp_parser_error (parser, "setter name must terminate with %<:%>");
31231 		  else
31232 		    cp_lexer_consume_token (parser->lexer);
31233 		}
31234 	      else
31235 		{
31236 		  if (property_getter_ident != NULL_TREE)
31237 		    {
31238 		      cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31239 		      cp_lexer_consume_token (parser->lexer);
31240 		    }
31241 		  else
31242 		    property_getter_ident = cp_parser_objc_selector (parser);
31243 		}
31244 	      break;
31245 	    default:
31246 	      cp_parser_error (parser, "unknown property attribute");
31247 	      syntax_error = true;
31248 	      break;
31249 	    }
31250 
31251 	  if (syntax_error)
31252 	    break;
31253 
31254 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31255 	    cp_lexer_consume_token (parser->lexer);
31256 	  else
31257 	    break;
31258 	}
31259 
31260       /* FIXME: "@property (setter, assign);" will generate a spurious
31261 	 "error: expected ‘)’ before ‘,’ token".  This is because
31262 	 cp_parser_require, unlike the C counterpart, will produce an
31263 	 error even if we are in error recovery.  */
31264       if (!parens.require_close (parser))
31265 	{
31266 	  cp_parser_skip_to_closing_parenthesis (parser,
31267 						 /*recovering=*/true,
31268 						 /*or_comma=*/false,
31269 						 /*consume_paren=*/true);
31270 	}
31271     }
31272 
31273   /* ... and the property declaration(s).  */
31274   properties = cp_parser_objc_struct_declaration (parser);
31275 
31276   if (properties == error_mark_node)
31277     {
31278       cp_parser_skip_to_end_of_statement (parser);
31279       /* If the next token is now a `;', consume it.  */
31280       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31281 	cp_lexer_consume_token (parser->lexer);
31282       return;
31283     }
31284 
31285   if (properties == NULL_TREE)
31286     cp_parser_error (parser, "expected identifier");
31287   else
31288     {
31289       /* Comma-separated properties are chained together in
31290 	 reverse order; add them one by one.  */
31291       properties = nreverse (properties);
31292 
31293       for (; properties; properties = TREE_CHAIN (properties))
31294 	objc_add_property_declaration (loc, copy_node (properties),
31295 				       property_readonly, property_readwrite,
31296 				       property_assign, property_retain,
31297 				       property_copy, property_nonatomic,
31298 				       property_getter_ident, property_setter_ident);
31299     }
31300 
31301   cp_parser_consume_semicolon_at_end_of_statement (parser);
31302 }
31303 
31304 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
31305 
31306    objc-synthesize-declaration:
31307      @synthesize objc-synthesize-identifier-list ;
31308 
31309    objc-synthesize-identifier-list:
31310      objc-synthesize-identifier
31311      objc-synthesize-identifier-list, objc-synthesize-identifier
31312 
31313    objc-synthesize-identifier
31314      identifier
31315      identifier = identifier
31316 
31317   For example:
31318     @synthesize MyProperty;
31319     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31320 
31321   PS: This function is identical to c_parser_objc_at_synthesize_declaration
31322   for C.  Keep them in sync.
31323 */
31324 static void
cp_parser_objc_at_synthesize_declaration(cp_parser * parser)31325 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31326 {
31327   tree list = NULL_TREE;
31328   location_t loc;
31329   loc = cp_lexer_peek_token (parser->lexer)->location;
31330 
31331   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
31332   while (true)
31333     {
31334       tree property, ivar;
31335       property = cp_parser_identifier (parser);
31336       if (property == error_mark_node)
31337 	{
31338 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
31339 	  return;
31340 	}
31341       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31342 	{
31343 	  cp_lexer_consume_token (parser->lexer);
31344 	  ivar = cp_parser_identifier (parser);
31345 	  if (ivar == error_mark_node)
31346 	    {
31347 	      cp_parser_consume_semicolon_at_end_of_statement (parser);
31348 	      return;
31349 	    }
31350 	}
31351       else
31352 	ivar = NULL_TREE;
31353       list = chainon (list, build_tree_list (ivar, property));
31354       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31355 	cp_lexer_consume_token (parser->lexer);
31356       else
31357 	break;
31358     }
31359   cp_parser_consume_semicolon_at_end_of_statement (parser);
31360   objc_add_synthesize_declaration (loc, list);
31361 }
31362 
31363 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
31364 
31365    objc-dynamic-declaration:
31366      @dynamic identifier-list ;
31367 
31368    For example:
31369      @dynamic MyProperty;
31370      @dynamic MyProperty, AnotherProperty;
31371 
31372   PS: This function is identical to c_parser_objc_at_dynamic_declaration
31373   for C.  Keep them in sync.
31374 */
31375 static void
cp_parser_objc_at_dynamic_declaration(cp_parser * parser)31376 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31377 {
31378   tree list = NULL_TREE;
31379   location_t loc;
31380   loc = cp_lexer_peek_token (parser->lexer)->location;
31381 
31382   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
31383   while (true)
31384     {
31385       tree property;
31386       property = cp_parser_identifier (parser);
31387       if (property == error_mark_node)
31388 	{
31389 	  cp_parser_consume_semicolon_at_end_of_statement (parser);
31390 	  return;
31391 	}
31392       list = chainon (list, build_tree_list (NULL, property));
31393       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31394 	cp_lexer_consume_token (parser->lexer);
31395       else
31396 	break;
31397     }
31398   cp_parser_consume_semicolon_at_end_of_statement (parser);
31399   objc_add_dynamic_declaration (loc, list);
31400 }
31401 
31402 
31403 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
31404 
31405 /* Returns name of the next clause.
31406    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31407    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
31408    returned and the token is consumed.  */
31409 
31410 static pragma_omp_clause
cp_parser_omp_clause_name(cp_parser * parser)31411 cp_parser_omp_clause_name (cp_parser *parser)
31412 {
31413   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31414 
31415   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31416     result = PRAGMA_OACC_CLAUSE_AUTO;
31417   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31418     result = PRAGMA_OMP_CLAUSE_IF;
31419   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31420     result = PRAGMA_OMP_CLAUSE_DEFAULT;
31421   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31422     result = PRAGMA_OACC_CLAUSE_DELETE;
31423   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31424     result = PRAGMA_OMP_CLAUSE_PRIVATE;
31425   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31426     result = PRAGMA_OMP_CLAUSE_FOR;
31427   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31428     {
31429       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31430       const char *p = IDENTIFIER_POINTER (id);
31431 
31432       switch (p[0])
31433 	{
31434 	case 'a':
31435 	  if (!strcmp ("aligned", p))
31436 	    result = PRAGMA_OMP_CLAUSE_ALIGNED;
31437 	  else if (!strcmp ("async", p))
31438 	    result = PRAGMA_OACC_CLAUSE_ASYNC;
31439 	  break;
31440 	case 'c':
31441 	  if (!strcmp ("collapse", p))
31442 	    result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31443 	  else if (!strcmp ("copy", p))
31444 	    result = PRAGMA_OACC_CLAUSE_COPY;
31445 	  else if (!strcmp ("copyin", p))
31446 	    result = PRAGMA_OMP_CLAUSE_COPYIN;
31447 	  else if (!strcmp ("copyout", p))
31448 	    result = PRAGMA_OACC_CLAUSE_COPYOUT;
31449 	  else if (!strcmp ("copyprivate", p))
31450 	    result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31451 	  else if (!strcmp ("create", p))
31452 	    result = PRAGMA_OACC_CLAUSE_CREATE;
31453 	  break;
31454 	case 'd':
31455 	  if (!strcmp ("defaultmap", p))
31456 	    result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31457 	  else if (!strcmp ("depend", p))
31458 	    result = PRAGMA_OMP_CLAUSE_DEPEND;
31459 	  else if (!strcmp ("device", p))
31460 	    result = PRAGMA_OMP_CLAUSE_DEVICE;
31461 	  else if (!strcmp ("deviceptr", p))
31462 	    result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31463 	  else if (!strcmp ("device_resident", p))
31464 	    result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31465 	  else if (!strcmp ("dist_schedule", p))
31466 	    result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31467 	  break;
31468 	case 'f':
31469 	  if (!strcmp ("final", p))
31470 	    result = PRAGMA_OMP_CLAUSE_FINAL;
31471 	  else if (!strcmp ("firstprivate", p))
31472 	    result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31473 	  else if (!strcmp ("from", p))
31474 	    result = PRAGMA_OMP_CLAUSE_FROM;
31475 	  break;
31476 	case 'g':
31477 	  if (!strcmp ("gang", p))
31478 	    result = PRAGMA_OACC_CLAUSE_GANG;
31479 	  else if (!strcmp ("grainsize", p))
31480 	    result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31481 	  break;
31482 	case 'h':
31483 	  if (!strcmp ("hint", p))
31484 	    result = PRAGMA_OMP_CLAUSE_HINT;
31485 	  else if (!strcmp ("host", p))
31486 	    result = PRAGMA_OACC_CLAUSE_HOST;
31487 	  break;
31488 	case 'i':
31489 	  if (!strcmp ("inbranch", p))
31490 	    result = PRAGMA_OMP_CLAUSE_INBRANCH;
31491 	  else if (!strcmp ("independent", p))
31492 	    result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31493 	  else if (!strcmp ("is_device_ptr", p))
31494 	    result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31495 	  break;
31496 	case 'l':
31497 	  if (!strcmp ("lastprivate", p))
31498 	    result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31499 	  else if (!strcmp ("linear", p))
31500 	    result = PRAGMA_OMP_CLAUSE_LINEAR;
31501 	  else if (!strcmp ("link", p))
31502 	    result = PRAGMA_OMP_CLAUSE_LINK;
31503 	  break;
31504 	case 'm':
31505 	  if (!strcmp ("map", p))
31506 	    result = PRAGMA_OMP_CLAUSE_MAP;
31507 	  else if (!strcmp ("mergeable", p))
31508 	    result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31509 	  break;
31510 	case 'n':
31511 	  if (!strcmp ("nogroup", p))
31512 	    result = PRAGMA_OMP_CLAUSE_NOGROUP;
31513 	  else if (!strcmp ("notinbranch", p))
31514 	    result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31515 	  else if (!strcmp ("nowait", p))
31516 	    result = PRAGMA_OMP_CLAUSE_NOWAIT;
31517 	  else if (!strcmp ("num_gangs", p))
31518 	    result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31519 	  else if (!strcmp ("num_tasks", p))
31520 	    result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31521 	  else if (!strcmp ("num_teams", p))
31522 	    result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31523 	  else if (!strcmp ("num_threads", p))
31524 	    result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31525 	  else if (!strcmp ("num_workers", p))
31526 	    result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31527 	  break;
31528 	case 'o':
31529 	  if (!strcmp ("ordered", p))
31530 	    result = PRAGMA_OMP_CLAUSE_ORDERED;
31531 	  break;
31532 	case 'p':
31533 	  if (!strcmp ("parallel", p))
31534 	    result = PRAGMA_OMP_CLAUSE_PARALLEL;
31535 	  else if (!strcmp ("present", p))
31536 	    result = PRAGMA_OACC_CLAUSE_PRESENT;
31537 	  else if (!strcmp ("present_or_copy", p)
31538 		   || !strcmp ("pcopy", p))
31539 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31540 	  else if (!strcmp ("present_or_copyin", p)
31541 		   || !strcmp ("pcopyin", p))
31542 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31543 	  else if (!strcmp ("present_or_copyout", p)
31544 		   || !strcmp ("pcopyout", p))
31545 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31546 	  else if (!strcmp ("present_or_create", p)
31547 		   || !strcmp ("pcreate", p))
31548 	    result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31549 	  else if (!strcmp ("priority", p))
31550 	    result = PRAGMA_OMP_CLAUSE_PRIORITY;
31551 	  else if (!strcmp ("proc_bind", p))
31552 	    result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31553 	  break;
31554 	case 'r':
31555 	  if (!strcmp ("reduction", p))
31556 	    result = PRAGMA_OMP_CLAUSE_REDUCTION;
31557 	  break;
31558 	case 's':
31559 	  if (!strcmp ("safelen", p))
31560 	    result = PRAGMA_OMP_CLAUSE_SAFELEN;
31561 	  else if (!strcmp ("schedule", p))
31562 	    result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31563 	  else if (!strcmp ("sections", p))
31564 	    result = PRAGMA_OMP_CLAUSE_SECTIONS;
31565 	  else if (!strcmp ("self", p))
31566 	    result = PRAGMA_OACC_CLAUSE_SELF;
31567 	  else if (!strcmp ("seq", p))
31568 	    result = PRAGMA_OACC_CLAUSE_SEQ;
31569 	  else if (!strcmp ("shared", p))
31570 	    result = PRAGMA_OMP_CLAUSE_SHARED;
31571 	  else if (!strcmp ("simd", p))
31572 	    result = PRAGMA_OMP_CLAUSE_SIMD;
31573 	  else if (!strcmp ("simdlen", p))
31574 	    result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31575 	  break;
31576 	case 't':
31577 	  if (!strcmp ("taskgroup", p))
31578 	    result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31579 	  else if (!strcmp ("thread_limit", p))
31580 	    result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31581 	  else if (!strcmp ("threads", p))
31582 	    result = PRAGMA_OMP_CLAUSE_THREADS;
31583 	  else if (!strcmp ("tile", p))
31584 	    result = PRAGMA_OACC_CLAUSE_TILE;
31585 	  else if (!strcmp ("to", p))
31586 	    result = PRAGMA_OMP_CLAUSE_TO;
31587 	  break;
31588 	case 'u':
31589 	  if (!strcmp ("uniform", p))
31590 	    result = PRAGMA_OMP_CLAUSE_UNIFORM;
31591 	  else if (!strcmp ("untied", p))
31592 	    result = PRAGMA_OMP_CLAUSE_UNTIED;
31593 	  else if (!strcmp ("use_device", p))
31594 	    result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31595 	  else if (!strcmp ("use_device_ptr", p))
31596 	    result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31597 	  break;
31598 	case 'v':
31599 	  if (!strcmp ("vector", p))
31600 	    result = PRAGMA_OACC_CLAUSE_VECTOR;
31601 	  else if (!strcmp ("vector_length", p))
31602 	    result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31603 	  break;
31604 	case 'w':
31605 	  if (!strcmp ("wait", p))
31606 	    result = PRAGMA_OACC_CLAUSE_WAIT;
31607 	  else if (!strcmp ("worker", p))
31608 	    result = PRAGMA_OACC_CLAUSE_WORKER;
31609 	  break;
31610 	}
31611     }
31612 
31613   if (result != PRAGMA_OMP_CLAUSE_NONE)
31614     cp_lexer_consume_token (parser->lexer);
31615 
31616   return result;
31617 }
31618 
31619 /* Validate that a clause of the given type does not already exist.  */
31620 
31621 static void
check_no_duplicate_clause(tree clauses,enum omp_clause_code code,const char * name,location_t location)31622 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31623 			   const char *name, location_t location)
31624 {
31625   tree c;
31626 
31627   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31628     if (OMP_CLAUSE_CODE (c) == code)
31629       {
31630 	error_at (location, "too many %qs clauses", name);
31631 	break;
31632       }
31633 }
31634 
31635 /* OpenMP 2.5:
31636    variable-list:
31637      identifier
31638      variable-list , identifier
31639 
31640    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31641    colon).  An opening parenthesis will have been consumed by the caller.
31642 
31643    If KIND is nonzero, create the appropriate node and install the decl
31644    in OMP_CLAUSE_DECL and add the node to the head of the list.
31645 
31646    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31647    return the list created.
31648 
31649    COLON can be NULL if only closing parenthesis should end the list,
31650    or pointer to bool which will receive false if the list is terminated
31651    by closing parenthesis or true if the list is terminated by colon.  */
31652 
31653 static tree
cp_parser_omp_var_list_no_open(cp_parser * parser,enum omp_clause_code kind,tree list,bool * colon)31654 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31655 				tree list, bool *colon)
31656 {
31657   cp_token *token;
31658   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31659   if (colon)
31660     {
31661       parser->colon_corrects_to_scope_p = false;
31662       *colon = false;
31663     }
31664   while (1)
31665     {
31666       tree name, decl;
31667 
31668       token = cp_lexer_peek_token (parser->lexer);
31669       if (kind != 0
31670 	  && current_class_ptr
31671 	  && cp_parser_is_keyword (token, RID_THIS))
31672 	{
31673 	  decl = finish_this_expr ();
31674 	  if (TREE_CODE (decl) == NON_LVALUE_EXPR
31675 	      || CONVERT_EXPR_P (decl))
31676 	    decl = TREE_OPERAND (decl, 0);
31677 	  cp_lexer_consume_token (parser->lexer);
31678 	}
31679       else
31680 	{
31681 	  name = cp_parser_id_expression (parser, /*template_p=*/false,
31682 					  /*check_dependency_p=*/true,
31683 					  /*template_p=*/NULL,
31684 					  /*declarator_p=*/false,
31685 					  /*optional_p=*/false);
31686 	  if (name == error_mark_node)
31687 	    goto skip_comma;
31688 
31689 	  if (identifier_p (name))
31690 	    decl = cp_parser_lookup_name_simple (parser, name, token->location);
31691 	  else
31692 	    decl = name;
31693 	  if (decl == error_mark_node)
31694 	    cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31695 					 token->location);
31696 	}
31697       if (decl == error_mark_node)
31698 	;
31699       else if (kind != 0)
31700 	{
31701 	  switch (kind)
31702 	    {
31703 	    case OMP_CLAUSE__CACHE_:
31704 	      /* The OpenACC cache directive explicitly only allows "array
31705 		 elements or subarrays".  */
31706 	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31707 		{
31708 		  error_at (token->location, "expected %<[%>");
31709 		  decl = error_mark_node;
31710 		  break;
31711 		}
31712 	      /* FALLTHROUGH.  */
31713 	    case OMP_CLAUSE_MAP:
31714 	    case OMP_CLAUSE_FROM:
31715 	    case OMP_CLAUSE_TO:
31716 	      while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31717 		{
31718 		  location_t loc
31719 		    = cp_lexer_peek_token (parser->lexer)->location;
31720 		  cp_id_kind idk = CP_ID_KIND_NONE;
31721 		  cp_lexer_consume_token (parser->lexer);
31722 		  decl = convert_from_reference (decl);
31723 		  decl
31724 		    = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31725 							      decl, false,
31726 							      &idk, loc);
31727 		}
31728 	      /* FALLTHROUGH.  */
31729 	    case OMP_CLAUSE_DEPEND:
31730 	    case OMP_CLAUSE_REDUCTION:
31731 	      while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31732 		{
31733 		  tree low_bound = NULL_TREE, length = NULL_TREE;
31734 
31735 		  parser->colon_corrects_to_scope_p = false;
31736 		  cp_lexer_consume_token (parser->lexer);
31737 		  if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31738 		    low_bound = cp_parser_expression (parser);
31739 		  if (!colon)
31740 		    parser->colon_corrects_to_scope_p
31741 		      = saved_colon_corrects_to_scope_p;
31742 		  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31743 		    length = integer_one_node;
31744 		  else
31745 		    {
31746 		      /* Look for `:'.  */
31747 		      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31748 			goto skip_comma;
31749 		      if (!cp_lexer_next_token_is (parser->lexer,
31750 						   CPP_CLOSE_SQUARE))
31751 			length = cp_parser_expression (parser);
31752 		    }
31753 		  /* Look for the closing `]'.  */
31754 		  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31755 					  RT_CLOSE_SQUARE))
31756 		    goto skip_comma;
31757 
31758 		  decl = tree_cons (low_bound, length, decl);
31759 		}
31760 	      break;
31761 	    default:
31762 	      break;
31763 	    }
31764 
31765 	  tree u = build_omp_clause (token->location, kind);
31766 	  OMP_CLAUSE_DECL (u) = decl;
31767 	  OMP_CLAUSE_CHAIN (u) = list;
31768 	  list = u;
31769 	}
31770       else
31771 	list = tree_cons (decl, NULL_TREE, list);
31772 
31773     get_comma:
31774       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31775 	break;
31776       cp_lexer_consume_token (parser->lexer);
31777     }
31778 
31779   if (colon)
31780     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31781 
31782   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31783     {
31784       *colon = true;
31785       cp_parser_require (parser, CPP_COLON, RT_COLON);
31786       return list;
31787     }
31788 
31789   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31790     {
31791       int ending;
31792 
31793       /* Try to resync to an unnested comma.  Copied from
31794 	 cp_parser_parenthesized_expression_list.  */
31795     skip_comma:
31796       if (colon)
31797 	parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31798       ending = cp_parser_skip_to_closing_parenthesis (parser,
31799 						      /*recovering=*/true,
31800 						      /*or_comma=*/true,
31801 						      /*consume_paren=*/true);
31802       if (ending < 0)
31803 	goto get_comma;
31804     }
31805 
31806   return list;
31807 }
31808 
31809 /* Similarly, but expect leading and trailing parenthesis.  This is a very
31810    common case for omp clauses.  */
31811 
31812 static tree
cp_parser_omp_var_list(cp_parser * parser,enum omp_clause_code kind,tree list)31813 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31814 {
31815   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31816     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31817   return list;
31818 }
31819 
31820 /* OpenACC 2.0:
31821    copy ( variable-list )
31822    copyin ( variable-list )
31823    copyout ( variable-list )
31824    create ( variable-list )
31825    delete ( variable-list )
31826    present ( variable-list )
31827    present_or_copy ( variable-list )
31828      pcopy ( variable-list )
31829    present_or_copyin ( variable-list )
31830      pcopyin ( variable-list )
31831    present_or_copyout ( variable-list )
31832      pcopyout ( variable-list )
31833    present_or_create ( variable-list )
31834      pcreate ( variable-list ) */
31835 
31836 static tree
cp_parser_oacc_data_clause(cp_parser * parser,pragma_omp_clause c_kind,tree list)31837 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31838 			    tree list)
31839 {
31840   enum gomp_map_kind kind;
31841   switch (c_kind)
31842     {
31843     case PRAGMA_OACC_CLAUSE_COPY:
31844       kind = GOMP_MAP_FORCE_TOFROM;
31845       break;
31846     case PRAGMA_OACC_CLAUSE_COPYIN:
31847       kind = GOMP_MAP_FORCE_TO;
31848       break;
31849     case PRAGMA_OACC_CLAUSE_COPYOUT:
31850       kind = GOMP_MAP_FORCE_FROM;
31851       break;
31852     case PRAGMA_OACC_CLAUSE_CREATE:
31853       kind = GOMP_MAP_FORCE_ALLOC;
31854       break;
31855     case PRAGMA_OACC_CLAUSE_DELETE:
31856       kind = GOMP_MAP_DELETE;
31857       break;
31858     case PRAGMA_OACC_CLAUSE_DEVICE:
31859       kind = GOMP_MAP_FORCE_TO;
31860       break;
31861     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31862       kind = GOMP_MAP_DEVICE_RESIDENT;
31863       break;
31864     case PRAGMA_OACC_CLAUSE_HOST:
31865     case PRAGMA_OACC_CLAUSE_SELF:
31866       kind = GOMP_MAP_FORCE_FROM;
31867       break;
31868     case PRAGMA_OACC_CLAUSE_LINK:
31869       kind = GOMP_MAP_LINK;
31870       break;
31871     case PRAGMA_OACC_CLAUSE_PRESENT:
31872       kind = GOMP_MAP_FORCE_PRESENT;
31873       break;
31874     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31875       kind = GOMP_MAP_TOFROM;
31876       break;
31877     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31878       kind = GOMP_MAP_TO;
31879       break;
31880     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31881       kind = GOMP_MAP_FROM;
31882       break;
31883     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31884       kind = GOMP_MAP_ALLOC;
31885       break;
31886     default:
31887       gcc_unreachable ();
31888     }
31889   tree nl, c;
31890   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31891 
31892   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31893     OMP_CLAUSE_SET_MAP_KIND (c, kind);
31894 
31895   return nl;
31896 }
31897 
31898 /* OpenACC 2.0:
31899    deviceptr ( variable-list ) */
31900 
31901 static tree
cp_parser_oacc_data_clause_deviceptr(cp_parser * parser,tree list)31902 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31903 {
31904   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31905   tree vars, t;
31906 
31907   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31908      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31909      variable-list must only allow for pointer variables.  */
31910   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31911   for (t = vars; t; t = TREE_CHAIN (t))
31912     {
31913       tree v = TREE_PURPOSE (t);
31914       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31915       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31916       OMP_CLAUSE_DECL (u) = v;
31917       OMP_CLAUSE_CHAIN (u) = list;
31918       list = u;
31919     }
31920 
31921   return list;
31922 }
31923 
31924 /* OpenACC 2.0:
31925    auto
31926    independent
31927    nohost
31928    seq */
31929 
31930 static tree
cp_parser_oacc_simple_clause(cp_parser *,enum omp_clause_code code,tree list,location_t location)31931 cp_parser_oacc_simple_clause (cp_parser * /* parser  */,
31932 			      enum omp_clause_code code,
31933 			      tree list, location_t location)
31934 {
31935   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31936   tree c = build_omp_clause (location, code);
31937   OMP_CLAUSE_CHAIN (c) = list;
31938   return c;
31939 }
31940 
31941  /* OpenACC:
31942    num_gangs ( expression )
31943    num_workers ( expression )
31944    vector_length ( expression )  */
31945 
31946 static tree
cp_parser_oacc_single_int_clause(cp_parser * parser,omp_clause_code code,const char * str,tree list)31947 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31948 				  const char *str, tree list)
31949 {
31950   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31951 
31952   matching_parens parens;
31953   if (!parens.require_open (parser))
31954     return list;
31955 
31956   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31957 
31958   if (t == error_mark_node
31959       || !parens.require_close (parser))
31960     {
31961       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31962 					     /*or_comma=*/false,
31963 					     /*consume_paren=*/true);
31964       return list;
31965     }
31966 
31967   check_no_duplicate_clause (list, code, str, loc);
31968 
31969   tree c = build_omp_clause (loc, code);
31970   OMP_CLAUSE_OPERAND (c, 0) = t;
31971   OMP_CLAUSE_CHAIN (c) = list;
31972   return c;
31973 }
31974 
31975 /* OpenACC:
31976 
31977     gang [( gang-arg-list )]
31978     worker [( [num:] int-expr )]
31979     vector [( [length:] int-expr )]
31980 
31981   where gang-arg is one of:
31982 
31983     [num:] int-expr
31984     static: size-expr
31985 
31986   and size-expr may be:
31987 
31988     *
31989     int-expr
31990 */
31991 
31992 static tree
cp_parser_oacc_shape_clause(cp_parser * parser,omp_clause_code kind,const char * str,tree list)31993 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31994 			     const char *str, tree list)
31995 {
31996   const char *id = "num";
31997   cp_lexer *lexer = parser->lexer;
31998   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31999   location_t loc = cp_lexer_peek_token (lexer)->location;
32000 
32001   if (kind == OMP_CLAUSE_VECTOR)
32002     id = "length";
32003 
32004   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32005     {
32006       matching_parens parens;
32007       parens.consume_open (parser);
32008 
32009       do
32010 	{
32011 	  cp_token *next = cp_lexer_peek_token (lexer);
32012 	  int idx = 0;
32013 
32014 	  /* Gang static argument.  */
32015 	  if (kind == OMP_CLAUSE_GANG
32016 	      && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32017 	    {
32018 	      cp_lexer_consume_token (lexer);
32019 
32020 	      if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32021 		goto cleanup_error;
32022 
32023 	      idx = 1;
32024 	      if (ops[idx] != NULL)
32025 		{
32026 		  cp_parser_error (parser, "too many %<static%> arguments");
32027 		  goto cleanup_error;
32028 		}
32029 
32030 	      /* Check for the '*' argument.  */
32031 	      if (cp_lexer_next_token_is (lexer, CPP_MULT)
32032 		  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32033 		      || cp_lexer_nth_token_is (parser->lexer, 2,
32034 						CPP_CLOSE_PAREN)))
32035 		{
32036 		  cp_lexer_consume_token (lexer);
32037 		  ops[idx] = integer_minus_one_node;
32038 
32039 		  if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32040 		    {
32041 		      cp_lexer_consume_token (lexer);
32042 		      continue;
32043 		    }
32044 		  else break;
32045 		}
32046 	    }
32047 	  /* Worker num: argument and vector length: arguments.  */
32048 	  else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32049 		   && id_equal (next->u.value, id)
32050 		   && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32051 	    {
32052 	      cp_lexer_consume_token (lexer);  /* id  */
32053 	      cp_lexer_consume_token (lexer);  /* ':'  */
32054 	    }
32055 
32056 	  /* Now collect the actual argument.  */
32057 	  if (ops[idx] != NULL_TREE)
32058 	    {
32059 	      cp_parser_error (parser, "unexpected argument");
32060 	      goto cleanup_error;
32061 	    }
32062 
32063 	  tree expr = cp_parser_assignment_expression (parser, NULL, false,
32064 						       false);
32065 	  if (expr == error_mark_node)
32066 	    goto cleanup_error;
32067 
32068 	  mark_exp_read (expr);
32069 	  ops[idx] = expr;
32070 
32071 	  if (kind == OMP_CLAUSE_GANG
32072 	      && cp_lexer_next_token_is (lexer, CPP_COMMA))
32073 	    {
32074 	      cp_lexer_consume_token (lexer);
32075 	      continue;
32076 	    }
32077 	  break;
32078 	}
32079       while (1);
32080 
32081       if (!parens.require_close (parser))
32082 	goto cleanup_error;
32083     }
32084 
32085   check_no_duplicate_clause (list, kind, str, loc);
32086 
32087   c = build_omp_clause (loc, kind);
32088 
32089   if (ops[1])
32090     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32091 
32092   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32093   OMP_CLAUSE_CHAIN (c) = list;
32094 
32095   return c;
32096 
32097  cleanup_error:
32098   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32099   return list;
32100 }
32101 
32102 /* OpenACC 2.0:
32103    tile ( size-expr-list ) */
32104 
32105 static tree
cp_parser_oacc_clause_tile(cp_parser * parser,location_t clause_loc,tree list)32106 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32107 {
32108   tree c, expr = error_mark_node;
32109   tree tile = NULL_TREE;
32110 
32111   /* Collapse and tile are mutually exclusive.  (The spec doesn't say
32112      so, but the spec authors never considered such a case and have
32113      differing opinions on what it might mean, including 'not
32114      allowed'.)  */
32115   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32116   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32117 			     clause_loc);
32118 
32119   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32120     return list;
32121 
32122   do
32123     {
32124       if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32125 	return list;
32126 
32127       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32128 	  && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32129 	      || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32130 	{
32131 	  cp_lexer_consume_token (parser->lexer);
32132 	  expr = integer_zero_node;
32133 	}
32134       else
32135 	expr = cp_parser_constant_expression (parser);
32136 
32137       tile = tree_cons (NULL_TREE, expr, tile);
32138     }
32139   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32140 
32141   /* Consume the trailing ')'.  */
32142   cp_lexer_consume_token (parser->lexer);
32143 
32144   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32145   tile = nreverse (tile);
32146   OMP_CLAUSE_TILE_LIST (c) = tile;
32147   OMP_CLAUSE_CHAIN (c) = list;
32148   return c;
32149 }
32150 
32151 /* OpenACC 2.0
32152    Parse wait clause or directive parameters.  */
32153 
32154 static tree
cp_parser_oacc_wait_list(cp_parser * parser,location_t clause_loc,tree list)32155 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32156 {
32157   vec<tree, va_gc> *args;
32158   tree t, args_tree;
32159 
32160   args = cp_parser_parenthesized_expression_list (parser, non_attr,
32161 						  /*cast_p=*/false,
32162 						  /*allow_expansion_p=*/true,
32163 						  /*non_constant_p=*/NULL);
32164 
32165   if (args == NULL || args->length () == 0)
32166     {
32167       cp_parser_error (parser, "expected integer expression before ')'");
32168       if (args != NULL)
32169 	release_tree_vector (args);
32170       return list;
32171     }
32172 
32173   args_tree = build_tree_list_vec (args);
32174 
32175   release_tree_vector (args);
32176 
32177   for (t = args_tree; t; t = TREE_CHAIN (t))
32178     {
32179       tree targ = TREE_VALUE (t);
32180 
32181       if (targ != error_mark_node)
32182 	{
32183 	  if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32184 	    error ("%<wait%> expression must be integral");
32185 	  else
32186 	    {
32187 	      tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32188 
32189 	      targ = mark_rvalue_use (targ);
32190 	      OMP_CLAUSE_DECL (c) = targ;
32191 	      OMP_CLAUSE_CHAIN (c) = list;
32192 	      list = c;
32193 	    }
32194 	}
32195     }
32196 
32197   return list;
32198 }
32199 
32200 /* OpenACC:
32201    wait ( int-expr-list ) */
32202 
32203 static tree
cp_parser_oacc_clause_wait(cp_parser * parser,tree list)32204 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32205 {
32206   location_t location = cp_lexer_peek_token (parser->lexer)->location;
32207 
32208   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32209     return list;
32210 
32211   list = cp_parser_oacc_wait_list (parser, location, list);
32212 
32213   return list;
32214 }
32215 
32216 /* OpenMP 3.0:
32217    collapse ( constant-expression ) */
32218 
32219 static tree
cp_parser_omp_clause_collapse(cp_parser * parser,tree list,location_t location)32220 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32221 {
32222   tree c, num;
32223   location_t loc;
32224   HOST_WIDE_INT n;
32225 
32226   loc = cp_lexer_peek_token (parser->lexer)->location;
32227   matching_parens parens;
32228   if (!parens.require_open (parser))
32229     return list;
32230 
32231   num = cp_parser_constant_expression (parser);
32232 
32233   if (!parens.require_close (parser))
32234     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32235 					   /*or_comma=*/false,
32236 					   /*consume_paren=*/true);
32237 
32238   if (num == error_mark_node)
32239     return list;
32240   num = fold_non_dependent_expr (num);
32241   if (!tree_fits_shwi_p (num)
32242       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32243       || (n = tree_to_shwi (num)) <= 0
32244       || (int) n != n)
32245     {
32246       error_at (loc, "collapse argument needs positive constant integer expression");
32247       return list;
32248     }
32249 
32250   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32251   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32252   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32253   OMP_CLAUSE_CHAIN (c) = list;
32254   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32255 
32256   return c;
32257 }
32258 
32259 /* OpenMP 2.5:
32260    default ( none | shared )
32261 
32262    OpenACC:
32263    default ( none | present ) */
32264 
32265 static tree
cp_parser_omp_clause_default(cp_parser * parser,tree list,location_t location,bool is_oacc)32266 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32267 			      location_t location, bool is_oacc)
32268 {
32269   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32270   tree c;
32271 
32272   matching_parens parens;
32273   if (!parens.require_open (parser))
32274     return list;
32275   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32276     {
32277       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32278       const char *p = IDENTIFIER_POINTER (id);
32279 
32280       switch (p[0])
32281 	{
32282 	case 'n':
32283 	  if (strcmp ("none", p) != 0)
32284 	    goto invalid_kind;
32285 	  kind = OMP_CLAUSE_DEFAULT_NONE;
32286 	  break;
32287 
32288 	case 'p':
32289 	  if (strcmp ("present", p) != 0 || !is_oacc)
32290 	    goto invalid_kind;
32291 	  kind = OMP_CLAUSE_DEFAULT_PRESENT;
32292 	  break;
32293 
32294 	case 's':
32295 	  if (strcmp ("shared", p) != 0 || is_oacc)
32296 	    goto invalid_kind;
32297 	  kind = OMP_CLAUSE_DEFAULT_SHARED;
32298 	  break;
32299 
32300 	default:
32301 	  goto invalid_kind;
32302 	}
32303 
32304       cp_lexer_consume_token (parser->lexer);
32305     }
32306   else
32307     {
32308     invalid_kind:
32309       if (is_oacc)
32310 	cp_parser_error (parser, "expected %<none%> or %<present%>");
32311       else
32312 	cp_parser_error (parser, "expected %<none%> or %<shared%>");
32313     }
32314 
32315   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32316       || !parens.require_close (parser))
32317     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32318 					   /*or_comma=*/false,
32319 					   /*consume_paren=*/true);
32320 
32321   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32322     return list;
32323 
32324   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32325   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32326   OMP_CLAUSE_CHAIN (c) = list;
32327   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32328 
32329   return c;
32330 }
32331 
32332 /* OpenMP 3.1:
32333    final ( expression ) */
32334 
32335 static tree
cp_parser_omp_clause_final(cp_parser * parser,tree list,location_t location)32336 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32337 {
32338   tree t, c;
32339 
32340   matching_parens parens;
32341   if (!parens.require_open (parser))
32342     return list;
32343 
32344   t = cp_parser_condition (parser);
32345 
32346   if (t == error_mark_node
32347       || !parens.require_close (parser))
32348     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32349 					   /*or_comma=*/false,
32350 					   /*consume_paren=*/true);
32351 
32352   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32353 
32354   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32355   OMP_CLAUSE_FINAL_EXPR (c) = t;
32356   OMP_CLAUSE_CHAIN (c) = list;
32357 
32358   return c;
32359 }
32360 
32361 /* OpenMP 2.5:
32362    if ( expression )
32363 
32364    OpenMP 4.5:
32365    if ( directive-name-modifier : expression )
32366 
32367    directive-name-modifier:
32368      parallel | task | taskloop | target data | target | target update
32369      | target enter data | target exit data  */
32370 
32371 static tree
cp_parser_omp_clause_if(cp_parser * parser,tree list,location_t location,bool is_omp)32372 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32373 			 bool is_omp)
32374 {
32375   tree t, c;
32376   enum tree_code if_modifier = ERROR_MARK;
32377 
32378   matching_parens parens;
32379   if (!parens.require_open (parser))
32380     return list;
32381 
32382   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32383     {
32384       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32385       const char *p = IDENTIFIER_POINTER (id);
32386       int n = 2;
32387 
32388       if (strcmp ("parallel", p) == 0)
32389 	if_modifier = OMP_PARALLEL;
32390       else if (strcmp ("task", p) == 0)
32391 	if_modifier = OMP_TASK;
32392       else if (strcmp ("taskloop", p) == 0)
32393 	if_modifier = OMP_TASKLOOP;
32394       else if (strcmp ("target", p) == 0)
32395 	{
32396 	  if_modifier = OMP_TARGET;
32397 	  if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32398 	    {
32399 	      id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32400 	      p = IDENTIFIER_POINTER (id);
32401 	      if (strcmp ("data", p) == 0)
32402 		if_modifier = OMP_TARGET_DATA;
32403 	      else if (strcmp ("update", p) == 0)
32404 		if_modifier = OMP_TARGET_UPDATE;
32405 	      else if (strcmp ("enter", p) == 0)
32406 		if_modifier = OMP_TARGET_ENTER_DATA;
32407 	      else if (strcmp ("exit", p) == 0)
32408 		if_modifier = OMP_TARGET_EXIT_DATA;
32409 	      if (if_modifier != OMP_TARGET)
32410 		n = 3;
32411 	      else
32412 		{
32413 		  location_t loc
32414 		    = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32415 		  error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32416 				 "or %<exit%>");
32417 		  if_modifier = ERROR_MARK;
32418 		}
32419 	      if (if_modifier == OMP_TARGET_ENTER_DATA
32420 		  || if_modifier == OMP_TARGET_EXIT_DATA)
32421 		{
32422 		  if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32423 		    {
32424 		      id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32425 		      p = IDENTIFIER_POINTER (id);
32426 		      if (strcmp ("data", p) == 0)
32427 			n = 4;
32428 		    }
32429 		  if (n != 4)
32430 		    {
32431 		      location_t loc
32432 			= cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32433 		      error_at (loc, "expected %<data%>");
32434 		      if_modifier = ERROR_MARK;
32435 		    }
32436 		}
32437 	    }
32438 	}
32439       if (if_modifier != ERROR_MARK)
32440 	{
32441 	  if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32442 	    {
32443 	      while (n-- > 0)
32444 		cp_lexer_consume_token (parser->lexer);
32445 	    }
32446 	  else
32447 	    {
32448 	      if (n > 2)
32449 		{
32450 		  location_t loc
32451 		    = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32452 		  error_at (loc, "expected %<:%>");
32453 		}
32454 	      if_modifier = ERROR_MARK;
32455 	    }
32456 	}
32457     }
32458 
32459   t = cp_parser_condition (parser);
32460 
32461   if (t == error_mark_node
32462       || !parens.require_close (parser))
32463     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32464 					   /*or_comma=*/false,
32465 					   /*consume_paren=*/true);
32466 
32467   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32468     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32469       {
32470 	if (if_modifier != ERROR_MARK
32471 	    && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32472 	  {
32473 	    const char *p = NULL;
32474 	    switch (if_modifier)
32475 	      {
32476 	      case OMP_PARALLEL: p = "parallel"; break;
32477 	      case OMP_TASK: p = "task"; break;
32478 	      case OMP_TASKLOOP: p = "taskloop"; break;
32479 	      case OMP_TARGET_DATA: p = "target data"; break;
32480 	      case OMP_TARGET: p = "target"; break;
32481 	      case OMP_TARGET_UPDATE: p = "target update"; break;
32482 	      case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32483 	      case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32484 	      default: gcc_unreachable ();
32485 	      }
32486 	    error_at (location, "too many %<if%> clauses with %qs modifier",
32487 		      p);
32488 	    return list;
32489 	  }
32490 	else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32491 	  {
32492 	    if (!is_omp)
32493 	      error_at (location, "too many %<if%> clauses");
32494 	    else
32495 	      error_at (location, "too many %<if%> clauses without modifier");
32496 	    return list;
32497 	  }
32498 	else if (if_modifier == ERROR_MARK
32499 		 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32500 	  {
32501 	    error_at (location, "if any %<if%> clause has modifier, then all "
32502 				"%<if%> clauses have to use modifier");
32503 	    return list;
32504 	  }
32505       }
32506 
32507   c = build_omp_clause (location, OMP_CLAUSE_IF);
32508   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32509   OMP_CLAUSE_IF_EXPR (c) = t;
32510   OMP_CLAUSE_CHAIN (c) = list;
32511 
32512   return c;
32513 }
32514 
32515 /* OpenMP 3.1:
32516    mergeable */
32517 
32518 static tree
cp_parser_omp_clause_mergeable(cp_parser *,tree list,location_t location)32519 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32520 				tree list, location_t location)
32521 {
32522   tree c;
32523 
32524   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32525 			     location);
32526 
32527   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32528   OMP_CLAUSE_CHAIN (c) = list;
32529   return c;
32530 }
32531 
32532 /* OpenMP 2.5:
32533    nowait */
32534 
32535 static tree
cp_parser_omp_clause_nowait(cp_parser *,tree list,location_t location)32536 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32537 			     tree list, location_t location)
32538 {
32539   tree c;
32540 
32541   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32542 
32543   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32544   OMP_CLAUSE_CHAIN (c) = list;
32545   return c;
32546 }
32547 
32548 /* OpenMP 2.5:
32549    num_threads ( expression ) */
32550 
32551 static tree
cp_parser_omp_clause_num_threads(cp_parser * parser,tree list,location_t location)32552 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32553 				  location_t location)
32554 {
32555   tree t, c;
32556 
32557   matching_parens parens;
32558   if (!parens.require_open (parser))
32559     return list;
32560 
32561   t = cp_parser_expression (parser);
32562 
32563   if (t == error_mark_node
32564       || !parens.require_close (parser))
32565     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32566 					   /*or_comma=*/false,
32567 					   /*consume_paren=*/true);
32568 
32569   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32570 			     "num_threads", location);
32571 
32572   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32573   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32574   OMP_CLAUSE_CHAIN (c) = list;
32575 
32576   return c;
32577 }
32578 
32579 /* OpenMP 4.5:
32580    num_tasks ( expression ) */
32581 
32582 static tree
cp_parser_omp_clause_num_tasks(cp_parser * parser,tree list,location_t location)32583 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32584 				location_t location)
32585 {
32586   tree t, c;
32587 
32588   matching_parens parens;
32589   if (!parens.require_open (parser))
32590     return list;
32591 
32592   t = cp_parser_expression (parser);
32593 
32594   if (t == error_mark_node
32595       || !parens.require_close (parser))
32596     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32597 					   /*or_comma=*/false,
32598 					   /*consume_paren=*/true);
32599 
32600   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32601 			     "num_tasks", location);
32602 
32603   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32604   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32605   OMP_CLAUSE_CHAIN (c) = list;
32606 
32607   return c;
32608 }
32609 
32610 /* OpenMP 4.5:
32611    grainsize ( expression ) */
32612 
32613 static tree
cp_parser_omp_clause_grainsize(cp_parser * parser,tree list,location_t location)32614 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32615 				location_t location)
32616 {
32617   tree t, c;
32618 
32619   matching_parens parens;
32620   if (!parens.require_open (parser))
32621     return list;
32622 
32623   t = cp_parser_expression (parser);
32624 
32625   if (t == error_mark_node
32626       || !parens.require_close (parser))
32627     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32628 					   /*or_comma=*/false,
32629 					   /*consume_paren=*/true);
32630 
32631   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32632 			     "grainsize", location);
32633 
32634   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32635   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32636   OMP_CLAUSE_CHAIN (c) = list;
32637 
32638   return c;
32639 }
32640 
32641 /* OpenMP 4.5:
32642    priority ( expression ) */
32643 
32644 static tree
cp_parser_omp_clause_priority(cp_parser * parser,tree list,location_t location)32645 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32646 			       location_t location)
32647 {
32648   tree t, c;
32649 
32650   matching_parens parens;
32651   if (!parens.require_open (parser))
32652     return list;
32653 
32654   t = cp_parser_expression (parser);
32655 
32656   if (t == error_mark_node
32657       || !parens.require_close (parser))
32658     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32659 					   /*or_comma=*/false,
32660 					   /*consume_paren=*/true);
32661 
32662   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32663 			     "priority", location);
32664 
32665   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32666   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32667   OMP_CLAUSE_CHAIN (c) = list;
32668 
32669   return c;
32670 }
32671 
32672 /* OpenMP 4.5:
32673    hint ( expression ) */
32674 
32675 static tree
cp_parser_omp_clause_hint(cp_parser * parser,tree list,location_t location)32676 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32677 			   location_t location)
32678 {
32679   tree t, c;
32680 
32681   matching_parens parens;
32682   if (!parens.require_open (parser))
32683     return list;
32684 
32685   t = cp_parser_expression (parser);
32686 
32687   if (t == error_mark_node
32688       || !parens.require_close (parser))
32689     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32690 					   /*or_comma=*/false,
32691 					   /*consume_paren=*/true);
32692 
32693   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32694 
32695   c = build_omp_clause (location, OMP_CLAUSE_HINT);
32696   OMP_CLAUSE_HINT_EXPR (c) = t;
32697   OMP_CLAUSE_CHAIN (c) = list;
32698 
32699   return c;
32700 }
32701 
32702 /* OpenMP 4.5:
32703    defaultmap ( tofrom : scalar ) */
32704 
32705 static tree
cp_parser_omp_clause_defaultmap(cp_parser * parser,tree list,location_t location)32706 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32707 				 location_t location)
32708 {
32709   tree c, id;
32710   const char *p;
32711 
32712   matching_parens parens;
32713   if (!parens.require_open (parser))
32714     return list;
32715 
32716   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32717     {
32718       cp_parser_error (parser, "expected %<tofrom%>");
32719       goto out_err;
32720     }
32721   id = cp_lexer_peek_token (parser->lexer)->u.value;
32722   p = IDENTIFIER_POINTER (id);
32723   if (strcmp (p, "tofrom") != 0)
32724     {
32725       cp_parser_error (parser, "expected %<tofrom%>");
32726       goto out_err;
32727     }
32728   cp_lexer_consume_token (parser->lexer);
32729   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32730     goto out_err;
32731 
32732   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32733     {
32734       cp_parser_error (parser, "expected %<scalar%>");
32735       goto out_err;
32736     }
32737   id = cp_lexer_peek_token (parser->lexer)->u.value;
32738   p = IDENTIFIER_POINTER (id);
32739   if (strcmp (p, "scalar") != 0)
32740     {
32741       cp_parser_error (parser, "expected %<scalar%>");
32742       goto out_err;
32743     }
32744   cp_lexer_consume_token (parser->lexer);
32745   if (!parens.require_close (parser))
32746     goto out_err;
32747 
32748   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32749 			     location);
32750 
32751   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32752   OMP_CLAUSE_CHAIN (c) = list;
32753   return c;
32754 
32755  out_err:
32756   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32757 					 /*or_comma=*/false,
32758 					 /*consume_paren=*/true);
32759   return list;
32760 }
32761 
32762 /* OpenMP 2.5:
32763    ordered
32764 
32765    OpenMP 4.5:
32766    ordered ( constant-expression ) */
32767 
32768 static tree
cp_parser_omp_clause_ordered(cp_parser * parser,tree list,location_t location)32769 cp_parser_omp_clause_ordered (cp_parser *parser,
32770 			      tree list, location_t location)
32771 {
32772   tree c, num = NULL_TREE;
32773   HOST_WIDE_INT n;
32774 
32775   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32776 			     "ordered", location);
32777 
32778   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32779     {
32780       matching_parens parens;
32781       parens.consume_open (parser);
32782 
32783       num = cp_parser_constant_expression (parser);
32784 
32785       if (!parens.require_close (parser))
32786 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32787 					       /*or_comma=*/false,
32788 					       /*consume_paren=*/true);
32789 
32790       if (num == error_mark_node)
32791 	return list;
32792       num = fold_non_dependent_expr (num);
32793       if (!tree_fits_shwi_p (num)
32794 	  || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32795 	  || (n = tree_to_shwi (num)) <= 0
32796 	  || (int) n != n)
32797 	{
32798 	  error_at (location,
32799 		    "ordered argument needs positive constant integer "
32800 		    "expression");
32801 	  return list;
32802 	}
32803     }
32804 
32805   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32806   OMP_CLAUSE_ORDERED_EXPR (c) = num;
32807   OMP_CLAUSE_CHAIN (c) = list;
32808   return c;
32809 }
32810 
32811 /* OpenMP 2.5:
32812    reduction ( reduction-operator : variable-list )
32813 
32814    reduction-operator:
32815      One of: + * - & ^ | && ||
32816 
32817    OpenMP 3.1:
32818 
32819    reduction-operator:
32820      One of: + * - & ^ | && || min max
32821 
32822    OpenMP 4.0:
32823 
32824    reduction-operator:
32825      One of: + * - & ^ | && ||
32826      id-expression  */
32827 
32828 static tree
cp_parser_omp_clause_reduction(cp_parser * parser,tree list)32829 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32830 {
32831   enum tree_code code = ERROR_MARK;
32832   tree nlist, c, id = NULL_TREE;
32833 
32834   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32835     return list;
32836 
32837   switch (cp_lexer_peek_token (parser->lexer)->type)
32838     {
32839     case CPP_PLUS: code = PLUS_EXPR; break;
32840     case CPP_MULT: code = MULT_EXPR; break;
32841     case CPP_MINUS: code = MINUS_EXPR; break;
32842     case CPP_AND: code = BIT_AND_EXPR; break;
32843     case CPP_XOR: code = BIT_XOR_EXPR; break;
32844     case CPP_OR: code = BIT_IOR_EXPR; break;
32845     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32846     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32847     default: break;
32848     }
32849 
32850   if (code != ERROR_MARK)
32851     cp_lexer_consume_token (parser->lexer);
32852   else
32853     {
32854       bool saved_colon_corrects_to_scope_p;
32855       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32856       parser->colon_corrects_to_scope_p = false;
32857       id = cp_parser_id_expression (parser, /*template_p=*/false,
32858 				    /*check_dependency_p=*/true,
32859 				    /*template_p=*/NULL,
32860 				    /*declarator_p=*/false,
32861 				    /*optional_p=*/false);
32862       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32863       if (identifier_p (id))
32864 	{
32865 	  const char *p = IDENTIFIER_POINTER (id);
32866 
32867 	  if (strcmp (p, "min") == 0)
32868 	    code = MIN_EXPR;
32869 	  else if (strcmp (p, "max") == 0)
32870 	    code = MAX_EXPR;
32871 	  else if (id == ovl_op_identifier (false, PLUS_EXPR))
32872 	    code = PLUS_EXPR;
32873 	  else if (id == ovl_op_identifier (false, MULT_EXPR))
32874 	    code = MULT_EXPR;
32875 	  else if (id == ovl_op_identifier (false, MINUS_EXPR))
32876 	    code = MINUS_EXPR;
32877 	  else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32878 	    code = BIT_AND_EXPR;
32879 	  else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32880 	    code = BIT_IOR_EXPR;
32881 	  else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32882 	    code = BIT_XOR_EXPR;
32883 	  else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32884 	    code = TRUTH_ANDIF_EXPR;
32885 	  else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32886 	    code = TRUTH_ORIF_EXPR;
32887 	  id = omp_reduction_id (code, id, NULL_TREE);
32888 	  tree scope = parser->scope;
32889 	  if (scope)
32890 	    id = build_qualified_name (NULL_TREE, scope, id, false);
32891 	  parser->scope = NULL_TREE;
32892 	  parser->qualifying_scope = NULL_TREE;
32893 	  parser->object_scope = NULL_TREE;
32894 	}
32895       else
32896 	{
32897 	  error ("invalid reduction-identifier");
32898 	 resync_fail:
32899 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32900 						 /*or_comma=*/false,
32901 						 /*consume_paren=*/true);
32902 	  return list;
32903 	}
32904     }
32905 
32906   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32907     goto resync_fail;
32908 
32909   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32910 					  NULL);
32911   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32912     {
32913       OMP_CLAUSE_REDUCTION_CODE (c) = code;
32914       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32915     }
32916 
32917   return nlist;
32918 }
32919 
32920 /* OpenMP 2.5:
32921    schedule ( schedule-kind )
32922    schedule ( schedule-kind , expression )
32923 
32924    schedule-kind:
32925      static | dynamic | guided | runtime | auto
32926 
32927    OpenMP 4.5:
32928    schedule ( schedule-modifier : schedule-kind )
32929    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32930 
32931    schedule-modifier:
32932      simd
32933      monotonic
32934      nonmonotonic  */
32935 
32936 static tree
cp_parser_omp_clause_schedule(cp_parser * parser,tree list,location_t location)32937 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32938 {
32939   tree c, t;
32940   int modifiers = 0, nmodifiers = 0;
32941 
32942   matching_parens parens;
32943   if (!parens.require_open (parser))
32944     return list;
32945 
32946   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32947 
32948   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32949     {
32950       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32951       const char *p = IDENTIFIER_POINTER (id);
32952       if (strcmp ("simd", p) == 0)
32953 	OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32954       else if (strcmp ("monotonic", p) == 0)
32955 	modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32956       else if (strcmp ("nonmonotonic", p) == 0)
32957 	modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32958       else
32959 	break;
32960       cp_lexer_consume_token (parser->lexer);
32961       if (nmodifiers++ == 0
32962 	  && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32963 	cp_lexer_consume_token (parser->lexer);
32964       else
32965 	{
32966 	  cp_parser_require (parser, CPP_COLON, RT_COLON);
32967 	  break;
32968 	}
32969     }
32970 
32971   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32972     {
32973       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32974       const char *p = IDENTIFIER_POINTER (id);
32975 
32976       switch (p[0])
32977 	{
32978 	case 'd':
32979 	  if (strcmp ("dynamic", p) != 0)
32980 	    goto invalid_kind;
32981 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32982 	  break;
32983 
32984 	case 'g':
32985 	  if (strcmp ("guided", p) != 0)
32986 	    goto invalid_kind;
32987 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32988 	  break;
32989 
32990 	case 'r':
32991 	  if (strcmp ("runtime", p) != 0)
32992 	    goto invalid_kind;
32993 	  OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32994 	  break;
32995 
32996 	default:
32997 	  goto invalid_kind;
32998 	}
32999     }
33000   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33001     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33002   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33003     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33004   else
33005     goto invalid_kind;
33006   cp_lexer_consume_token (parser->lexer);
33007 
33008   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33009 		    | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33010       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33011 	  | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33012     {
33013       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33014 			  "specified");
33015       modifiers = 0;
33016     }
33017 
33018   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33019     {
33020       cp_token *token;
33021       cp_lexer_consume_token (parser->lexer);
33022 
33023       token = cp_lexer_peek_token (parser->lexer);
33024       t = cp_parser_assignment_expression (parser);
33025 
33026       if (t == error_mark_node)
33027 	goto resync_fail;
33028       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33029 	error_at (token->location, "schedule %<runtime%> does not take "
33030 		  "a %<chunk_size%> parameter");
33031       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33032 	error_at (token->location, "schedule %<auto%> does not take "
33033 		  "a %<chunk_size%> parameter");
33034       else
33035 	OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33036 
33037       if (!parens.require_close (parser))
33038 	goto resync_fail;
33039     }
33040   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33041     goto resync_fail;
33042 
33043   OMP_CLAUSE_SCHEDULE_KIND (c)
33044     = (enum omp_clause_schedule_kind)
33045       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33046 
33047   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33048   OMP_CLAUSE_CHAIN (c) = list;
33049   return c;
33050 
33051  invalid_kind:
33052   cp_parser_error (parser, "invalid schedule kind");
33053  resync_fail:
33054   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33055 					 /*or_comma=*/false,
33056 					 /*consume_paren=*/true);
33057   return list;
33058 }
33059 
33060 /* OpenMP 3.0:
33061    untied */
33062 
33063 static tree
cp_parser_omp_clause_untied(cp_parser *,tree list,location_t location)33064 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33065 			     tree list, location_t location)
33066 {
33067   tree c;
33068 
33069   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33070 
33071   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33072   OMP_CLAUSE_CHAIN (c) = list;
33073   return c;
33074 }
33075 
33076 /* OpenMP 4.0:
33077    inbranch
33078    notinbranch */
33079 
33080 static tree
cp_parser_omp_clause_branch(cp_parser *,enum omp_clause_code code,tree list,location_t location)33081 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33082 			     tree list, location_t location)
33083 {
33084   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33085   tree c = build_omp_clause (location, code);
33086   OMP_CLAUSE_CHAIN (c) = list;
33087   return c;
33088 }
33089 
33090 /* OpenMP 4.0:
33091    parallel
33092    for
33093    sections
33094    taskgroup */
33095 
33096 static tree
cp_parser_omp_clause_cancelkind(cp_parser *,enum omp_clause_code code,tree list,location_t location)33097 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33098 				 enum omp_clause_code code,
33099 				 tree list, location_t location)
33100 {
33101   tree c = build_omp_clause (location, code);
33102   OMP_CLAUSE_CHAIN (c) = list;
33103   return c;
33104 }
33105 
33106 /* OpenMP 4.5:
33107    nogroup */
33108 
33109 static tree
cp_parser_omp_clause_nogroup(cp_parser *,tree list,location_t location)33110 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33111 			      tree list, location_t location)
33112 {
33113   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33114   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33115   OMP_CLAUSE_CHAIN (c) = list;
33116   return c;
33117 }
33118 
33119 /* OpenMP 4.5:
33120    simd
33121    threads */
33122 
33123 static tree
cp_parser_omp_clause_orderedkind(cp_parser *,enum omp_clause_code code,tree list,location_t location)33124 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33125 				  enum omp_clause_code code,
33126 				  tree list, location_t location)
33127 {
33128   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33129   tree c = build_omp_clause (location, code);
33130   OMP_CLAUSE_CHAIN (c) = list;
33131   return c;
33132 }
33133 
33134 /* OpenMP 4.0:
33135    num_teams ( expression ) */
33136 
33137 static tree
cp_parser_omp_clause_num_teams(cp_parser * parser,tree list,location_t location)33138 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33139 				location_t location)
33140 {
33141   tree t, c;
33142 
33143   matching_parens parens;
33144   if (!parens.require_open (parser))
33145     return list;
33146 
33147   t = cp_parser_expression (parser);
33148 
33149   if (t == error_mark_node
33150       || !parens.require_close (parser))
33151     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33152 					   /*or_comma=*/false,
33153 					   /*consume_paren=*/true);
33154 
33155   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33156 			     "num_teams", location);
33157 
33158   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33159   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33160   OMP_CLAUSE_CHAIN (c) = list;
33161 
33162   return c;
33163 }
33164 
33165 /* OpenMP 4.0:
33166    thread_limit ( expression ) */
33167 
33168 static tree
cp_parser_omp_clause_thread_limit(cp_parser * parser,tree list,location_t location)33169 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33170 				   location_t location)
33171 {
33172   tree t, c;
33173 
33174   matching_parens parens;
33175   if (!parens.require_open (parser))
33176     return list;
33177 
33178   t = cp_parser_expression (parser);
33179 
33180   if (t == error_mark_node
33181       || !parens.require_close (parser))
33182     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33183 					   /*or_comma=*/false,
33184 					   /*consume_paren=*/true);
33185 
33186   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33187 			     "thread_limit", location);
33188 
33189   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33190   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33191   OMP_CLAUSE_CHAIN (c) = list;
33192 
33193   return c;
33194 }
33195 
33196 /* OpenMP 4.0:
33197    aligned ( variable-list )
33198    aligned ( variable-list : constant-expression )  */
33199 
33200 static tree
cp_parser_omp_clause_aligned(cp_parser * parser,tree list)33201 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33202 {
33203   tree nlist, c, alignment = NULL_TREE;
33204   bool colon;
33205 
33206   matching_parens parens;
33207   if (!parens.require_open (parser))
33208     return list;
33209 
33210   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33211 					  &colon);
33212 
33213   if (colon)
33214     {
33215       alignment = cp_parser_constant_expression (parser);
33216 
33217       if (!parens.require_close (parser))
33218 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33219 					       /*or_comma=*/false,
33220 					       /*consume_paren=*/true);
33221 
33222       if (alignment == error_mark_node)
33223 	alignment = NULL_TREE;
33224     }
33225 
33226   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33227     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33228 
33229   return nlist;
33230 }
33231 
33232 /* OpenMP 4.0:
33233    linear ( variable-list )
33234    linear ( variable-list : expression )
33235 
33236    OpenMP 4.5:
33237    linear ( modifier ( variable-list ) )
33238    linear ( modifier ( variable-list ) : expression ) */
33239 
33240 static tree
cp_parser_omp_clause_linear(cp_parser * parser,tree list,bool declare_simd)33241 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33242 			     bool declare_simd)
33243 {
33244   tree nlist, c, step = integer_one_node;
33245   bool colon;
33246   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33247 
33248   matching_parens parens;
33249   if (!parens.require_open (parser))
33250     return list;
33251 
33252   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33253     {
33254       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33255       const char *p = IDENTIFIER_POINTER (id);
33256 
33257       if (strcmp ("ref", p) == 0)
33258 	kind = OMP_CLAUSE_LINEAR_REF;
33259       else if (strcmp ("val", p) == 0)
33260 	kind = OMP_CLAUSE_LINEAR_VAL;
33261       else if (strcmp ("uval", p) == 0)
33262 	kind = OMP_CLAUSE_LINEAR_UVAL;
33263       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33264 	cp_lexer_consume_token (parser->lexer);
33265       else
33266 	kind = OMP_CLAUSE_LINEAR_DEFAULT;
33267     }
33268 
33269   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33270     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33271 					    &colon);
33272   else
33273     {
33274       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33275       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33276       if (colon)
33277 	cp_parser_require (parser, CPP_COLON, RT_COLON);
33278       else if (!parens.require_close (parser))
33279 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33280 					       /*or_comma=*/false,
33281 					       /*consume_paren=*/true);
33282     }
33283 
33284   if (colon)
33285     {
33286       step = NULL_TREE;
33287       if (declare_simd
33288 	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33289 	  && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33290 	{
33291 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
33292 	  cp_parser_parse_tentatively (parser);
33293 	  step = cp_parser_id_expression (parser, /*template_p=*/false,
33294 					  /*check_dependency_p=*/true,
33295 					  /*template_p=*/NULL,
33296 					  /*declarator_p=*/false,
33297 					  /*optional_p=*/false);
33298 	  if (step != error_mark_node)
33299 	    step = cp_parser_lookup_name_simple (parser, step, token->location);
33300 	  if (step == error_mark_node)
33301 	    {
33302 	      step = NULL_TREE;
33303 	      cp_parser_abort_tentative_parse (parser);
33304 	    }
33305 	  else if (!cp_parser_parse_definitely (parser))
33306 	    step = NULL_TREE;
33307 	}
33308       if (!step)
33309 	step = cp_parser_expression (parser);
33310 
33311       if (!parens.require_close (parser))
33312 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33313 					       /*or_comma=*/false,
33314 					       /*consume_paren=*/true);
33315 
33316       if (step == error_mark_node)
33317 	return list;
33318     }
33319 
33320   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33321     {
33322       OMP_CLAUSE_LINEAR_STEP (c) = step;
33323       OMP_CLAUSE_LINEAR_KIND (c) = kind;
33324     }
33325 
33326   return nlist;
33327 }
33328 
33329 /* OpenMP 4.0:
33330    safelen ( constant-expression )  */
33331 
33332 static tree
cp_parser_omp_clause_safelen(cp_parser * parser,tree list,location_t location)33333 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33334 			      location_t location)
33335 {
33336   tree t, c;
33337 
33338   matching_parens parens;
33339   if (!parens.require_open (parser))
33340     return list;
33341 
33342   t = cp_parser_constant_expression (parser);
33343 
33344   if (t == error_mark_node
33345       || !parens.require_close (parser))
33346     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33347 					   /*or_comma=*/false,
33348 					   /*consume_paren=*/true);
33349 
33350   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33351 
33352   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33353   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33354   OMP_CLAUSE_CHAIN (c) = list;
33355 
33356   return c;
33357 }
33358 
33359 /* OpenMP 4.0:
33360    simdlen ( constant-expression )  */
33361 
33362 static tree
cp_parser_omp_clause_simdlen(cp_parser * parser,tree list,location_t location)33363 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33364 			      location_t location)
33365 {
33366   tree t, c;
33367 
33368   matching_parens parens;
33369   if (!parens.require_open (parser))
33370     return list;
33371 
33372   t = cp_parser_constant_expression (parser);
33373 
33374   if (t == error_mark_node
33375       || !parens.require_close (parser))
33376     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33377 					   /*or_comma=*/false,
33378 					   /*consume_paren=*/true);
33379 
33380   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33381 
33382   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33383   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33384   OMP_CLAUSE_CHAIN (c) = list;
33385 
33386   return c;
33387 }
33388 
33389 /* OpenMP 4.5:
33390    vec:
33391      identifier [+/- integer]
33392      vec , identifier [+/- integer]
33393 */
33394 
33395 static tree
cp_parser_omp_clause_depend_sink(cp_parser * parser,location_t clause_loc,tree list)33396 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33397 				  tree list)
33398 {
33399   tree vec = NULL;
33400 
33401   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33402     {
33403       cp_parser_error (parser, "expected identifier");
33404       return list;
33405     }
33406 
33407   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33408     {
33409       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33410       tree t, identifier = cp_parser_identifier (parser);
33411       tree addend = NULL;
33412 
33413       if (identifier == error_mark_node)
33414 	t = error_mark_node;
33415       else
33416 	{
33417 	  t = cp_parser_lookup_name_simple
33418 		(parser, identifier,
33419 		 cp_lexer_peek_token (parser->lexer)->location);
33420 	  if (t == error_mark_node)
33421 	    cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33422 					 id_loc);
33423 	}
33424 
33425       bool neg = false;
33426       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33427 	neg = true;
33428       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33429 	{
33430 	  addend = integer_zero_node;
33431 	  goto add_to_vector;
33432 	}
33433       cp_lexer_consume_token (parser->lexer);
33434 
33435       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33436 	{
33437 	  cp_parser_error (parser, "expected integer");
33438 	  return list;
33439 	}
33440 
33441       addend = cp_lexer_peek_token (parser->lexer)->u.value;
33442       if (TREE_CODE (addend) != INTEGER_CST)
33443 	{
33444 	  cp_parser_error (parser, "expected integer");
33445 	  return list;
33446 	}
33447       cp_lexer_consume_token (parser->lexer);
33448 
33449     add_to_vector:
33450       if (t != error_mark_node)
33451 	{
33452 	  vec = tree_cons (addend, t, vec);
33453 	  if (neg)
33454 	    OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33455 	}
33456 
33457       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33458 	break;
33459 
33460       cp_lexer_consume_token (parser->lexer);
33461     }
33462 
33463   if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33464     {
33465       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33466       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33467       OMP_CLAUSE_DECL (u) = nreverse (vec);
33468       OMP_CLAUSE_CHAIN (u) = list;
33469       return u;
33470     }
33471   return list;
33472 }
33473 
33474 /* OpenMP 4.0:
33475    depend ( depend-kind : variable-list )
33476 
33477    depend-kind:
33478      in | out | inout
33479 
33480    OpenMP 4.5:
33481    depend ( source )
33482 
33483    depend ( sink : vec ) */
33484 
33485 static tree
cp_parser_omp_clause_depend(cp_parser * parser,tree list,location_t loc)33486 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33487 {
33488   tree nlist, c;
33489   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33490 
33491   matching_parens parens;
33492   if (!parens.require_open (parser))
33493     return list;
33494 
33495   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33496     {
33497       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33498       const char *p = IDENTIFIER_POINTER (id);
33499 
33500       if (strcmp ("in", p) == 0)
33501 	kind = OMP_CLAUSE_DEPEND_IN;
33502       else if (strcmp ("inout", p) == 0)
33503 	kind = OMP_CLAUSE_DEPEND_INOUT;
33504       else if (strcmp ("out", p) == 0)
33505 	kind = OMP_CLAUSE_DEPEND_OUT;
33506       else if (strcmp ("source", p) == 0)
33507 	kind = OMP_CLAUSE_DEPEND_SOURCE;
33508       else if (strcmp ("sink", p) == 0)
33509 	kind = OMP_CLAUSE_DEPEND_SINK;
33510       else
33511 	goto invalid_kind;
33512     }
33513   else
33514     goto invalid_kind;
33515 
33516   cp_lexer_consume_token (parser->lexer);
33517 
33518   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33519     {
33520       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33521       OMP_CLAUSE_DEPEND_KIND (c) = kind;
33522       OMP_CLAUSE_DECL (c) = NULL_TREE;
33523       OMP_CLAUSE_CHAIN (c) = list;
33524       if (!parens.require_close (parser))
33525 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33526 					       /*or_comma=*/false,
33527 					       /*consume_paren=*/true);
33528       return c;
33529     }
33530 
33531   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33532     goto resync_fail;
33533 
33534   if (kind == OMP_CLAUSE_DEPEND_SINK)
33535     nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33536   else
33537     {
33538       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33539 					      list, NULL);
33540 
33541       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33542 	OMP_CLAUSE_DEPEND_KIND (c) = kind;
33543     }
33544   return nlist;
33545 
33546  invalid_kind:
33547   cp_parser_error (parser, "invalid depend kind");
33548  resync_fail:
33549   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33550 					 /*or_comma=*/false,
33551 					 /*consume_paren=*/true);
33552   return list;
33553 }
33554 
33555 /* OpenMP 4.0:
33556    map ( map-kind : variable-list )
33557    map ( variable-list )
33558 
33559    map-kind:
33560      alloc | to | from | tofrom
33561 
33562    OpenMP 4.5:
33563    map-kind:
33564      alloc | to | from | tofrom | release | delete
33565 
33566    map ( always [,] map-kind: variable-list ) */
33567 
33568 static tree
cp_parser_omp_clause_map(cp_parser * parser,tree list)33569 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33570 {
33571   tree nlist, c;
33572   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33573   bool always = false;
33574 
33575   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33576     return list;
33577 
33578   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33579     {
33580       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33581       const char *p = IDENTIFIER_POINTER (id);
33582 
33583       if (strcmp ("always", p) == 0)
33584 	{
33585 	  int nth = 2;
33586 	  if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33587 	    nth++;
33588 	  if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33589 	       || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33590 		   == RID_DELETE))
33591 	      && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33592 		  == CPP_COLON))
33593 	    {
33594 	      always = true;
33595 	      cp_lexer_consume_token (parser->lexer);
33596 	      if (nth == 3)
33597 		cp_lexer_consume_token (parser->lexer);
33598 	    }
33599 	}
33600     }
33601 
33602   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33603       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33604     {
33605       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33606       const char *p = IDENTIFIER_POINTER (id);
33607 
33608       if (strcmp ("alloc", p) == 0)
33609 	kind = GOMP_MAP_ALLOC;
33610       else if (strcmp ("to", p) == 0)
33611 	kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33612       else if (strcmp ("from", p) == 0)
33613 	kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33614       else if (strcmp ("tofrom", p) == 0)
33615 	kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33616       else if (strcmp ("release", p) == 0)
33617 	kind = GOMP_MAP_RELEASE;
33618       else
33619 	{
33620 	  cp_parser_error (parser, "invalid map kind");
33621 	  cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33622 						 /*or_comma=*/false,
33623 						 /*consume_paren=*/true);
33624 	  return list;
33625 	}
33626       cp_lexer_consume_token (parser->lexer);
33627       cp_lexer_consume_token (parser->lexer);
33628     }
33629   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33630 	   && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33631     {
33632       kind = GOMP_MAP_DELETE;
33633       cp_lexer_consume_token (parser->lexer);
33634       cp_lexer_consume_token (parser->lexer);
33635     }
33636 
33637   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33638 					  NULL);
33639 
33640   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33641     OMP_CLAUSE_SET_MAP_KIND (c, kind);
33642 
33643   return nlist;
33644 }
33645 
33646 /* OpenMP 4.0:
33647    device ( expression ) */
33648 
33649 static tree
cp_parser_omp_clause_device(cp_parser * parser,tree list,location_t location)33650 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33651 			     location_t location)
33652 {
33653   tree t, c;
33654 
33655   matching_parens parens;
33656   if (!parens.require_open (parser))
33657     return list;
33658 
33659   t = cp_parser_expression (parser);
33660 
33661   if (t == error_mark_node
33662       || !parens.require_close (parser))
33663     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33664 					   /*or_comma=*/false,
33665 					   /*consume_paren=*/true);
33666 
33667   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33668 			     "device", location);
33669 
33670   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33671   OMP_CLAUSE_DEVICE_ID (c) = t;
33672   OMP_CLAUSE_CHAIN (c) = list;
33673 
33674   return c;
33675 }
33676 
33677 /* OpenMP 4.0:
33678    dist_schedule ( static )
33679    dist_schedule ( static , expression )  */
33680 
33681 static tree
cp_parser_omp_clause_dist_schedule(cp_parser * parser,tree list,location_t location)33682 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33683 				    location_t location)
33684 {
33685   tree c, t;
33686 
33687   matching_parens parens;
33688   if (!parens.require_open (parser))
33689     return list;
33690 
33691   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33692 
33693   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33694     goto invalid_kind;
33695   cp_lexer_consume_token (parser->lexer);
33696 
33697   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33698     {
33699       cp_lexer_consume_token (parser->lexer);
33700 
33701       t = cp_parser_assignment_expression (parser);
33702 
33703       if (t == error_mark_node)
33704 	goto resync_fail;
33705       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33706 
33707       if (!parens.require_close (parser))
33708 	goto resync_fail;
33709     }
33710   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33711     goto resync_fail;
33712 
33713   /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
33714 				"dist_schedule", location); */
33715   if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
33716     warning_at (location, 0, "too many %qs clauses", "dist_schedule");
33717   OMP_CLAUSE_CHAIN (c) = list;
33718   return c;
33719 
33720  invalid_kind:
33721   cp_parser_error (parser, "invalid dist_schedule kind");
33722  resync_fail:
33723   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33724 					 /*or_comma=*/false,
33725 					 /*consume_paren=*/true);
33726   return list;
33727 }
33728 
33729 /* OpenMP 4.0:
33730    proc_bind ( proc-bind-kind )
33731 
33732    proc-bind-kind:
33733      master | close | spread  */
33734 
33735 static tree
cp_parser_omp_clause_proc_bind(cp_parser * parser,tree list,location_t location)33736 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33737 				location_t location)
33738 {
33739   tree c;
33740   enum omp_clause_proc_bind_kind kind;
33741 
33742   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33743     return list;
33744 
33745   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33746     {
33747       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33748       const char *p = IDENTIFIER_POINTER (id);
33749 
33750       if (strcmp ("master", p) == 0)
33751 	kind = OMP_CLAUSE_PROC_BIND_MASTER;
33752       else if (strcmp ("close", p) == 0)
33753 	kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33754       else if (strcmp ("spread", p) == 0)
33755 	kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33756       else
33757 	goto invalid_kind;
33758     }
33759   else
33760     goto invalid_kind;
33761 
33762   cp_lexer_consume_token (parser->lexer);
33763   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33764     goto resync_fail;
33765 
33766   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33767   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33768 			     location);
33769   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33770   OMP_CLAUSE_CHAIN (c) = list;
33771   return c;
33772 
33773  invalid_kind:
33774   cp_parser_error (parser, "invalid depend kind");
33775  resync_fail:
33776   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33777 					 /*or_comma=*/false,
33778 					 /*consume_paren=*/true);
33779   return list;
33780 }
33781 
33782 /* OpenACC:
33783    async [( int-expr )] */
33784 
33785 static tree
cp_parser_oacc_clause_async(cp_parser * parser,tree list)33786 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33787 {
33788   tree c, t;
33789   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33790 
33791   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33792 
33793   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33794     {
33795       matching_parens parens;
33796       parens.consume_open (parser);
33797 
33798       t = cp_parser_expression (parser);
33799       if (t == error_mark_node
33800 	  || !parens.require_close (parser))
33801 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33802 						/*or_comma=*/false,
33803 						/*consume_paren=*/true);
33804     }
33805 
33806   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33807 
33808   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33809   OMP_CLAUSE_ASYNC_EXPR (c) = t;
33810   OMP_CLAUSE_CHAIN (c) = list;
33811   list = c;
33812 
33813   return list;
33814 }
33815 
33816 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
33817    is a bitmask in MASK.  Return the list of clauses found.  */
33818 
33819 static tree
33820 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33821 			   const char *where, cp_token *pragma_tok,
33822 			   bool finish_p = true)
33823 {
33824   tree clauses = NULL;
33825   bool first = true;
33826 
33827   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33828     {
33829       location_t here;
33830       pragma_omp_clause c_kind;
33831       omp_clause_code code;
33832       const char *c_name;
33833       tree prev = clauses;
33834 
33835       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33836 	cp_lexer_consume_token (parser->lexer);
33837 
33838       here = cp_lexer_peek_token (parser->lexer)->location;
33839       c_kind = cp_parser_omp_clause_name (parser);
33840 
33841       switch (c_kind)
33842 	{
33843 	case PRAGMA_OACC_CLAUSE_ASYNC:
33844 	  clauses = cp_parser_oacc_clause_async (parser, clauses);
33845 	  c_name = "async";
33846 	  break;
33847 	case PRAGMA_OACC_CLAUSE_AUTO:
33848 	  clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33849 						 clauses, here);
33850 	  c_name = "auto";
33851 	  break;
33852 	case PRAGMA_OACC_CLAUSE_COLLAPSE:
33853 	  clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33854 	  c_name = "collapse";
33855 	  break;
33856 	case PRAGMA_OACC_CLAUSE_COPY:
33857 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33858 	  c_name = "copy";
33859 	  break;
33860 	case PRAGMA_OACC_CLAUSE_COPYIN:
33861 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33862 	  c_name = "copyin";
33863 	  break;
33864 	case PRAGMA_OACC_CLAUSE_COPYOUT:
33865 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33866 	  c_name = "copyout";
33867 	  break;
33868 	case PRAGMA_OACC_CLAUSE_CREATE:
33869 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33870 	  c_name = "create";
33871 	  break;
33872 	case PRAGMA_OACC_CLAUSE_DELETE:
33873 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33874 	  c_name = "delete";
33875 	  break;
33876 	case PRAGMA_OMP_CLAUSE_DEFAULT:
33877 	  clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33878 	  c_name = "default";
33879 	  break;
33880 	case PRAGMA_OACC_CLAUSE_DEVICE:
33881 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33882 	  c_name = "device";
33883 	  break;
33884 	case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33885 	  clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33886 	  c_name = "deviceptr";
33887 	  break;
33888 	case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33889 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33890 	  c_name = "device_resident";
33891 	  break;
33892 	case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33893 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33894 					    clauses);
33895 	  c_name = "firstprivate";
33896 	  break;
33897 	case PRAGMA_OACC_CLAUSE_GANG:
33898 	  c_name = "gang";
33899 	  clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33900 						 c_name, clauses);
33901 	  break;
33902 	case PRAGMA_OACC_CLAUSE_HOST:
33903 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33904 	  c_name = "host";
33905 	  break;
33906 	case PRAGMA_OACC_CLAUSE_IF:
33907 	  clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33908 	  c_name = "if";
33909 	  break;
33910 	case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33911 	  clauses = cp_parser_oacc_simple_clause (parser,
33912 						  OMP_CLAUSE_INDEPENDENT,
33913 						  clauses, here);
33914 	  c_name = "independent";
33915 	  break;
33916 	case PRAGMA_OACC_CLAUSE_LINK:
33917 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33918 	  c_name = "link";
33919 	  break;
33920 	case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33921 	  code = OMP_CLAUSE_NUM_GANGS;
33922 	  c_name = "num_gangs";
33923 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33924 						      clauses);
33925 	  break;
33926 	case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33927 	  c_name = "num_workers";
33928 	  code = OMP_CLAUSE_NUM_WORKERS;
33929 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33930 						      clauses);
33931 	  break;
33932 	case PRAGMA_OACC_CLAUSE_PRESENT:
33933 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33934 	  c_name = "present";
33935 	  break;
33936 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33937 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33938 	  c_name = "present_or_copy";
33939 	  break;
33940 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33941 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33942 	  c_name = "present_or_copyin";
33943 	  break;
33944 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33945 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33946 	  c_name = "present_or_copyout";
33947 	  break;
33948 	case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33949 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33950 	  c_name = "present_or_create";
33951 	  break;
33952 	case PRAGMA_OACC_CLAUSE_PRIVATE:
33953 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33954 					    clauses);
33955 	  c_name = "private";
33956 	  break;
33957 	case PRAGMA_OACC_CLAUSE_REDUCTION:
33958 	  clauses = cp_parser_omp_clause_reduction (parser, clauses);
33959 	  c_name = "reduction";
33960 	  break;
33961 	case PRAGMA_OACC_CLAUSE_SELF:
33962 	  clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33963 	  c_name = "self";
33964 	  break;
33965 	case PRAGMA_OACC_CLAUSE_SEQ:
33966 	  clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33967 						 clauses, here);
33968 	  c_name = "seq";
33969 	  break;
33970 	case PRAGMA_OACC_CLAUSE_TILE:
33971 	  clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33972 	  c_name = "tile";
33973 	  break;
33974 	case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33975 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33976 					    clauses);
33977 	  c_name = "use_device";
33978 	  break;
33979 	case PRAGMA_OACC_CLAUSE_VECTOR:
33980 	  c_name = "vector";
33981 	  clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33982 						 c_name, clauses);
33983 	  break;
33984 	case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33985 	  c_name = "vector_length";
33986 	  code = OMP_CLAUSE_VECTOR_LENGTH;
33987 	  clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33988 						      clauses);
33989 	  break;
33990 	case PRAGMA_OACC_CLAUSE_WAIT:
33991 	  clauses = cp_parser_oacc_clause_wait (parser, clauses);
33992 	  c_name = "wait";
33993 	  break;
33994 	case PRAGMA_OACC_CLAUSE_WORKER:
33995 	  c_name = "worker";
33996 	  clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33997 						 c_name, clauses);
33998 	  break;
33999 	default:
34000 	  cp_parser_error (parser, "expected %<#pragma acc%> clause");
34001 	  goto saw_error;
34002 	}
34003 
34004       first = false;
34005 
34006       if (((mask >> c_kind) & 1) == 0)
34007 	{
34008 	  /* Remove the invalid clause(s) from the list to avoid
34009 	     confusing the rest of the compiler.  */
34010 	  clauses = prev;
34011 	  error_at (here, "%qs is not valid for %qs", c_name, where);
34012 	}
34013     }
34014 
34015  saw_error:
34016   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34017 
34018   if (finish_p)
34019     return finish_omp_clauses (clauses, C_ORT_ACC);
34020 
34021   return clauses;
34022 }
34023 
34024 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
34025    is a bitmask in MASK.  Return the list of clauses found; the result
34026    of clause default goes in *pdefault.  */
34027 
34028 static tree
34029 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34030 			   const char *where, cp_token *pragma_tok,
34031 			   bool finish_p = true)
34032 {
34033   tree clauses = NULL;
34034   bool first = true;
34035   cp_token *token = NULL;
34036 
34037   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34038     {
34039       pragma_omp_clause c_kind;
34040       const char *c_name;
34041       tree prev = clauses;
34042 
34043       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34044 	cp_lexer_consume_token (parser->lexer);
34045 
34046       token = cp_lexer_peek_token (parser->lexer);
34047       c_kind = cp_parser_omp_clause_name (parser);
34048 
34049       switch (c_kind)
34050 	{
34051 	case PRAGMA_OMP_CLAUSE_COLLAPSE:
34052 	  clauses = cp_parser_omp_clause_collapse (parser, clauses,
34053 						   token->location);
34054 	  c_name = "collapse";
34055 	  break;
34056 	case PRAGMA_OMP_CLAUSE_COPYIN:
34057 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34058 	  c_name = "copyin";
34059 	  break;
34060 	case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34061 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34062 					    clauses);
34063 	  c_name = "copyprivate";
34064 	  break;
34065 	case PRAGMA_OMP_CLAUSE_DEFAULT:
34066 	  clauses = cp_parser_omp_clause_default (parser, clauses,
34067 						  token->location, false);
34068 	  c_name = "default";
34069 	  break;
34070 	case PRAGMA_OMP_CLAUSE_FINAL:
34071 	  clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34072 	  c_name = "final";
34073 	  break;
34074 	case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34075 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34076 					    clauses);
34077 	  c_name = "firstprivate";
34078 	  break;
34079 	case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34080 	  clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34081 						    token->location);
34082 	  c_name = "grainsize";
34083 	  break;
34084 	case PRAGMA_OMP_CLAUSE_HINT:
34085 	  clauses = cp_parser_omp_clause_hint (parser, clauses,
34086 					       token->location);
34087 	  c_name = "hint";
34088 	  break;
34089 	case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34090 	  clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34091 						     token->location);
34092 	  c_name = "defaultmap";
34093 	  break;
34094 	case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34095 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34096 					    clauses);
34097 	  c_name = "use_device_ptr";
34098 	  break;
34099 	case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34100 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34101 					    clauses);
34102 	  c_name = "is_device_ptr";
34103 	  break;
34104 	case PRAGMA_OMP_CLAUSE_IF:
34105 	  clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34106 					     true);
34107 	  c_name = "if";
34108 	  break;
34109 	case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34110 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34111 					    clauses);
34112 	  c_name = "lastprivate";
34113 	  break;
34114 	case PRAGMA_OMP_CLAUSE_MERGEABLE:
34115 	  clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34116 						    token->location);
34117 	  c_name = "mergeable";
34118 	  break;
34119 	case PRAGMA_OMP_CLAUSE_NOWAIT:
34120 	  clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34121 	  c_name = "nowait";
34122 	  break;
34123 	case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34124 	  clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34125 						    token->location);
34126 	  c_name = "num_tasks";
34127 	  break;
34128 	case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34129 	  clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34130 						      token->location);
34131 	  c_name = "num_threads";
34132 	  break;
34133 	case PRAGMA_OMP_CLAUSE_ORDERED:
34134 	  clauses = cp_parser_omp_clause_ordered (parser, clauses,
34135 						  token->location);
34136 	  c_name = "ordered";
34137 	  break;
34138 	case PRAGMA_OMP_CLAUSE_PRIORITY:
34139 	  clauses = cp_parser_omp_clause_priority (parser, clauses,
34140 						   token->location);
34141 	  c_name = "priority";
34142 	  break;
34143 	case PRAGMA_OMP_CLAUSE_PRIVATE:
34144 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34145 					    clauses);
34146 	  c_name = "private";
34147 	  break;
34148 	case PRAGMA_OMP_CLAUSE_REDUCTION:
34149 	  clauses = cp_parser_omp_clause_reduction (parser, clauses);
34150 	  c_name = "reduction";
34151 	  break;
34152 	case PRAGMA_OMP_CLAUSE_SCHEDULE:
34153 	  clauses = cp_parser_omp_clause_schedule (parser, clauses,
34154 						   token->location);
34155 	  c_name = "schedule";
34156 	  break;
34157 	case PRAGMA_OMP_CLAUSE_SHARED:
34158 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34159 					    clauses);
34160 	  c_name = "shared";
34161 	  break;
34162 	case PRAGMA_OMP_CLAUSE_UNTIED:
34163 	  clauses = cp_parser_omp_clause_untied (parser, clauses,
34164 						 token->location);
34165 	  c_name = "untied";
34166 	  break;
34167 	case PRAGMA_OMP_CLAUSE_INBRANCH:
34168 	  clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34169 						 clauses, token->location);
34170 	  c_name = "inbranch";
34171 	  break;
34172 	case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34173 	  clauses = cp_parser_omp_clause_branch (parser,
34174 						 OMP_CLAUSE_NOTINBRANCH,
34175 						 clauses, token->location);
34176 	  c_name = "notinbranch";
34177 	  break;
34178 	case PRAGMA_OMP_CLAUSE_PARALLEL:
34179 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34180 						     clauses, token->location);
34181 	  c_name = "parallel";
34182 	  if (!first)
34183 	    {
34184 	     clause_not_first:
34185 	      error_at (token->location, "%qs must be the first clause of %qs",
34186 			c_name, where);
34187 	      clauses = prev;
34188 	    }
34189 	  break;
34190 	case PRAGMA_OMP_CLAUSE_FOR:
34191 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34192 						     clauses, token->location);
34193 	  c_name = "for";
34194 	  if (!first)
34195 	    goto clause_not_first;
34196 	  break;
34197 	case PRAGMA_OMP_CLAUSE_SECTIONS:
34198 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34199 						     clauses, token->location);
34200 	  c_name = "sections";
34201 	  if (!first)
34202 	    goto clause_not_first;
34203 	  break;
34204 	case PRAGMA_OMP_CLAUSE_TASKGROUP:
34205 	  clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34206 						     clauses, token->location);
34207 	  c_name = "taskgroup";
34208 	  if (!first)
34209 	    goto clause_not_first;
34210 	  break;
34211 	case PRAGMA_OMP_CLAUSE_LINK:
34212 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34213 	  c_name = "to";
34214 	  break;
34215 	case PRAGMA_OMP_CLAUSE_TO:
34216 	  if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34217 	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34218 					      clauses);
34219 	  else
34220 	    clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34221 	  c_name = "to";
34222 	  break;
34223 	case PRAGMA_OMP_CLAUSE_FROM:
34224 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34225 	  c_name = "from";
34226 	  break;
34227 	case PRAGMA_OMP_CLAUSE_UNIFORM:
34228 	  clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34229 					    clauses);
34230 	  c_name = "uniform";
34231 	  break;
34232 	case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34233 	  clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34234 						    token->location);
34235 	  c_name = "num_teams";
34236 	  break;
34237 	case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34238 	  clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34239 						       token->location);
34240 	  c_name = "thread_limit";
34241 	  break;
34242 	case PRAGMA_OMP_CLAUSE_ALIGNED:
34243 	  clauses = cp_parser_omp_clause_aligned (parser, clauses);
34244 	  c_name = "aligned";
34245 	  break;
34246 	case PRAGMA_OMP_CLAUSE_LINEAR:
34247 	  {
34248 	    bool declare_simd = false;
34249 	    if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34250 	      declare_simd = true;
34251 	    clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34252 	  }
34253 	  c_name = "linear";
34254 	  break;
34255 	case PRAGMA_OMP_CLAUSE_DEPEND:
34256 	  clauses = cp_parser_omp_clause_depend (parser, clauses,
34257 						 token->location);
34258 	  c_name = "depend";
34259 	  break;
34260 	case PRAGMA_OMP_CLAUSE_MAP:
34261 	  clauses = cp_parser_omp_clause_map (parser, clauses);
34262 	  c_name = "map";
34263 	  break;
34264 	case PRAGMA_OMP_CLAUSE_DEVICE:
34265 	  clauses = cp_parser_omp_clause_device (parser, clauses,
34266 						 token->location);
34267 	  c_name = "device";
34268 	  break;
34269 	case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34270 	  clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34271 							token->location);
34272 	  c_name = "dist_schedule";
34273 	  break;
34274 	case PRAGMA_OMP_CLAUSE_PROC_BIND:
34275 	  clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34276 						    token->location);
34277 	  c_name = "proc_bind";
34278 	  break;
34279 	case PRAGMA_OMP_CLAUSE_SAFELEN:
34280 	  clauses = cp_parser_omp_clause_safelen (parser, clauses,
34281 						  token->location);
34282 	  c_name = "safelen";
34283 	  break;
34284 	case PRAGMA_OMP_CLAUSE_SIMDLEN:
34285 	  clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34286 						  token->location);
34287 	  c_name = "simdlen";
34288 	  break;
34289 	case PRAGMA_OMP_CLAUSE_NOGROUP:
34290 	  clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34291 						  token->location);
34292 	  c_name = "nogroup";
34293 	  break;
34294 	case PRAGMA_OMP_CLAUSE_THREADS:
34295 	  clauses
34296 	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34297 						clauses, token->location);
34298 	  c_name = "threads";
34299 	  break;
34300 	case PRAGMA_OMP_CLAUSE_SIMD:
34301 	  clauses
34302 	    = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34303 						clauses, token->location);
34304 	  c_name = "simd";
34305 	  break;
34306 	default:
34307 	  cp_parser_error (parser, "expected %<#pragma omp%> clause");
34308 	  goto saw_error;
34309 	}
34310 
34311       first = false;
34312 
34313       if (((mask >> c_kind) & 1) == 0)
34314 	{
34315 	  /* Remove the invalid clause(s) from the list to avoid
34316 	     confusing the rest of the compiler.  */
34317 	  clauses = prev;
34318 	  error_at (token->location, "%qs is not valid for %qs", c_name, where);
34319 	}
34320     }
34321  saw_error:
34322   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34323   if (finish_p)
34324     {
34325       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34326 	return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34327       else
34328 	return finish_omp_clauses (clauses, C_ORT_OMP);
34329     }
34330   return clauses;
34331 }
34332 
34333 /* OpenMP 2.5:
34334    structured-block:
34335      statement
34336 
34337    In practice, we're also interested in adding the statement to an
34338    outer node.  So it is convenient if we work around the fact that
34339    cp_parser_statement calls add_stmt.  */
34340 
34341 static unsigned
cp_parser_begin_omp_structured_block(cp_parser * parser)34342 cp_parser_begin_omp_structured_block (cp_parser *parser)
34343 {
34344   unsigned save = parser->in_statement;
34345 
34346   /* Only move the values to IN_OMP_BLOCK if they weren't false.
34347      This preserves the "not within loop or switch" style error messages
34348      for nonsense cases like
34349 	void foo() {
34350 	#pragma omp single
34351 	  break;
34352 	}
34353   */
34354   if (parser->in_statement)
34355     parser->in_statement = IN_OMP_BLOCK;
34356 
34357   return save;
34358 }
34359 
34360 static void
cp_parser_end_omp_structured_block(cp_parser * parser,unsigned save)34361 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34362 {
34363   parser->in_statement = save;
34364 }
34365 
34366 static tree
cp_parser_omp_structured_block(cp_parser * parser,bool * if_p)34367 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34368 {
34369   tree stmt = begin_omp_structured_block ();
34370   unsigned int save = cp_parser_begin_omp_structured_block (parser);
34371 
34372   cp_parser_statement (parser, NULL_TREE, false, if_p);
34373 
34374   cp_parser_end_omp_structured_block (parser, save);
34375   return finish_omp_structured_block (stmt);
34376 }
34377 
34378 /* OpenMP 2.5:
34379    # pragma omp atomic new-line
34380      expression-stmt
34381 
34382    expression-stmt:
34383      x binop= expr | x++ | ++x | x-- | --x
34384    binop:
34385      +, *, -, /, &, ^, |, <<, >>
34386 
34387   where x is an lvalue expression with scalar type.
34388 
34389    OpenMP 3.1:
34390    # pragma omp atomic new-line
34391      update-stmt
34392 
34393    # pragma omp atomic read new-line
34394      read-stmt
34395 
34396    # pragma omp atomic write new-line
34397      write-stmt
34398 
34399    # pragma omp atomic update new-line
34400      update-stmt
34401 
34402    # pragma omp atomic capture new-line
34403      capture-stmt
34404 
34405    # pragma omp atomic capture new-line
34406      capture-block
34407 
34408    read-stmt:
34409      v = x
34410    write-stmt:
34411      x = expr
34412    update-stmt:
34413      expression-stmt | x = x binop expr
34414    capture-stmt:
34415      v = expression-stmt
34416    capture-block:
34417      { v = x; update-stmt; } | { update-stmt; v = x; }
34418 
34419    OpenMP 4.0:
34420    update-stmt:
34421      expression-stmt | x = x binop expr | x = expr binop x
34422    capture-stmt:
34423      v = update-stmt
34424    capture-block:
34425      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34426 
34427   where x and v are lvalue expressions with scalar type.  */
34428 
34429 static void
cp_parser_omp_atomic(cp_parser * parser,cp_token * pragma_tok)34430 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34431 {
34432   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34433   tree rhs1 = NULL_TREE, orig_lhs;
34434   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34435   bool structured_block = false;
34436   bool seq_cst = false;
34437 
34438   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34439     {
34440       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34441       const char *p = IDENTIFIER_POINTER (id);
34442 
34443       if (!strcmp (p, "seq_cst"))
34444 	{
34445 	  seq_cst = true;
34446 	  cp_lexer_consume_token (parser->lexer);
34447 	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34448 	      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34449 	    cp_lexer_consume_token (parser->lexer);
34450 	}
34451     }
34452   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34453     {
34454       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34455       const char *p = IDENTIFIER_POINTER (id);
34456 
34457       if (!strcmp (p, "read"))
34458 	code = OMP_ATOMIC_READ;
34459       else if (!strcmp (p, "write"))
34460 	code = NOP_EXPR;
34461       else if (!strcmp (p, "update"))
34462 	code = OMP_ATOMIC;
34463       else if (!strcmp (p, "capture"))
34464 	code = OMP_ATOMIC_CAPTURE_NEW;
34465       else
34466 	p = NULL;
34467       if (p)
34468 	cp_lexer_consume_token (parser->lexer);
34469     }
34470   if (!seq_cst)
34471     {
34472       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34473 	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34474 	cp_lexer_consume_token (parser->lexer);
34475 
34476       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34477 	{
34478 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34479 	  const char *p = IDENTIFIER_POINTER (id);
34480 
34481 	  if (!strcmp (p, "seq_cst"))
34482 	    {
34483 	      seq_cst = true;
34484 	      cp_lexer_consume_token (parser->lexer);
34485 	    }
34486 	}
34487     }
34488   cp_parser_require_pragma_eol (parser, pragma_tok);
34489 
34490   switch (code)
34491     {
34492     case OMP_ATOMIC_READ:
34493     case NOP_EXPR: /* atomic write */
34494       v = cp_parser_unary_expression (parser);
34495       if (v == error_mark_node)
34496 	goto saw_error;
34497       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34498 	goto saw_error;
34499       if (code == NOP_EXPR)
34500 	lhs = cp_parser_expression (parser);
34501       else
34502 	lhs = cp_parser_unary_expression (parser);
34503       if (lhs == error_mark_node)
34504 	goto saw_error;
34505       if (code == NOP_EXPR)
34506 	{
34507 	  /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34508 	     opcode.  */
34509 	  code = OMP_ATOMIC;
34510 	  rhs = lhs;
34511 	  lhs = v;
34512 	  v = NULL_TREE;
34513 	}
34514       goto done;
34515     case OMP_ATOMIC_CAPTURE_NEW:
34516       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34517 	{
34518 	  cp_lexer_consume_token (parser->lexer);
34519 	  structured_block = true;
34520 	}
34521       else
34522 	{
34523 	  v = cp_parser_unary_expression (parser);
34524 	  if (v == error_mark_node)
34525 	    goto saw_error;
34526 	  if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34527 	    goto saw_error;
34528 	}
34529     default:
34530       break;
34531     }
34532 
34533 restart:
34534   lhs = cp_parser_unary_expression (parser);
34535   orig_lhs = lhs;
34536   switch (TREE_CODE (lhs))
34537     {
34538     case ERROR_MARK:
34539       goto saw_error;
34540 
34541     case POSTINCREMENT_EXPR:
34542       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34543 	code = OMP_ATOMIC_CAPTURE_OLD;
34544       /* FALLTHROUGH */
34545     case PREINCREMENT_EXPR:
34546       lhs = TREE_OPERAND (lhs, 0);
34547       opcode = PLUS_EXPR;
34548       rhs = integer_one_node;
34549       break;
34550 
34551     case POSTDECREMENT_EXPR:
34552       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34553 	code = OMP_ATOMIC_CAPTURE_OLD;
34554       /* FALLTHROUGH */
34555     case PREDECREMENT_EXPR:
34556       lhs = TREE_OPERAND (lhs, 0);
34557       opcode = MINUS_EXPR;
34558       rhs = integer_one_node;
34559       break;
34560 
34561     case COMPOUND_EXPR:
34562       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34563 	 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34564 	 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34565 	 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34566 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34567 					     (TREE_OPERAND (lhs, 1), 0), 0)))
34568 	    == BOOLEAN_TYPE)
34569        /* Undo effects of boolean_increment for post {in,de}crement.  */
34570        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34571       /* FALLTHRU */
34572     case MODIFY_EXPR:
34573       if (TREE_CODE (lhs) == MODIFY_EXPR
34574 	 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34575 	{
34576 	  /* Undo effects of boolean_increment.  */
34577 	  if (integer_onep (TREE_OPERAND (lhs, 1)))
34578 	    {
34579 	      /* This is pre or post increment.  */
34580 	      rhs = TREE_OPERAND (lhs, 1);
34581 	      lhs = TREE_OPERAND (lhs, 0);
34582 	      opcode = NOP_EXPR;
34583 	      if (code == OMP_ATOMIC_CAPTURE_NEW
34584 		  && !structured_block
34585 		  && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34586 		code = OMP_ATOMIC_CAPTURE_OLD;
34587 	      break;
34588 	    }
34589 	}
34590       /* FALLTHRU */
34591     default:
34592       switch (cp_lexer_peek_token (parser->lexer)->type)
34593 	{
34594 	case CPP_MULT_EQ:
34595 	  opcode = MULT_EXPR;
34596 	  break;
34597 	case CPP_DIV_EQ:
34598 	  opcode = TRUNC_DIV_EXPR;
34599 	  break;
34600 	case CPP_PLUS_EQ:
34601 	  opcode = PLUS_EXPR;
34602 	  break;
34603 	case CPP_MINUS_EQ:
34604 	  opcode = MINUS_EXPR;
34605 	  break;
34606 	case CPP_LSHIFT_EQ:
34607 	  opcode = LSHIFT_EXPR;
34608 	  break;
34609 	case CPP_RSHIFT_EQ:
34610 	  opcode = RSHIFT_EXPR;
34611 	  break;
34612 	case CPP_AND_EQ:
34613 	  opcode = BIT_AND_EXPR;
34614 	  break;
34615 	case CPP_OR_EQ:
34616 	  opcode = BIT_IOR_EXPR;
34617 	  break;
34618 	case CPP_XOR_EQ:
34619 	  opcode = BIT_XOR_EXPR;
34620 	  break;
34621 	case CPP_EQ:
34622 	  enum cp_parser_prec oprec;
34623 	  cp_token *token;
34624 	  cp_lexer_consume_token (parser->lexer);
34625 	  cp_parser_parse_tentatively (parser);
34626 	  rhs1 = cp_parser_simple_cast_expression (parser);
34627 	  if (rhs1 == error_mark_node)
34628 	    {
34629 	      cp_parser_abort_tentative_parse (parser);
34630 	      cp_parser_simple_cast_expression (parser);
34631 	      goto saw_error;
34632 	    }
34633 	  token = cp_lexer_peek_token (parser->lexer);
34634 	  if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34635 	    {
34636 	      cp_parser_abort_tentative_parse (parser);
34637 	      cp_parser_parse_tentatively (parser);
34638 	      rhs = cp_parser_binary_expression (parser, false, true,
34639 						 PREC_NOT_OPERATOR, NULL);
34640 	      if (rhs == error_mark_node)
34641 		{
34642 		  cp_parser_abort_tentative_parse (parser);
34643 		  cp_parser_binary_expression (parser, false, true,
34644 					       PREC_NOT_OPERATOR, NULL);
34645 		  goto saw_error;
34646 		}
34647 	      switch (TREE_CODE (rhs))
34648 		{
34649 		case MULT_EXPR:
34650 		case TRUNC_DIV_EXPR:
34651 		case RDIV_EXPR:
34652 		case PLUS_EXPR:
34653 		case MINUS_EXPR:
34654 		case LSHIFT_EXPR:
34655 		case RSHIFT_EXPR:
34656 		case BIT_AND_EXPR:
34657 		case BIT_IOR_EXPR:
34658 		case BIT_XOR_EXPR:
34659 		  if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34660 		    {
34661 		      if (cp_parser_parse_definitely (parser))
34662 			{
34663 			  opcode = TREE_CODE (rhs);
34664 			  rhs1 = TREE_OPERAND (rhs, 0);
34665 			  rhs = TREE_OPERAND (rhs, 1);
34666 			  goto stmt_done;
34667 			}
34668 		      else
34669 			goto saw_error;
34670 		    }
34671 		  break;
34672 		default:
34673 		  break;
34674 		}
34675 	      cp_parser_abort_tentative_parse (parser);
34676 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34677 		{
34678 		  rhs = cp_parser_expression (parser);
34679 		  if (rhs == error_mark_node)
34680 		    goto saw_error;
34681 		  opcode = NOP_EXPR;
34682 		  rhs1 = NULL_TREE;
34683 		  goto stmt_done;
34684 		}
34685 	      cp_parser_error (parser,
34686 			       "invalid form of %<#pragma omp atomic%>");
34687 	      goto saw_error;
34688 	    }
34689 	  if (!cp_parser_parse_definitely (parser))
34690 	    goto saw_error;
34691 	  switch (token->type)
34692 	    {
34693 	    case CPP_SEMICOLON:
34694 	      if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34695 		{
34696 		  code = OMP_ATOMIC_CAPTURE_OLD;
34697 		  v = lhs;
34698 		  lhs = NULL_TREE;
34699 		  lhs1 = rhs1;
34700 		  rhs1 = NULL_TREE;
34701 		  cp_lexer_consume_token (parser->lexer);
34702 		  goto restart;
34703 		}
34704 	      else if (structured_block)
34705 		{
34706 		  opcode = NOP_EXPR;
34707 		  rhs = rhs1;
34708 		  rhs1 = NULL_TREE;
34709 		  goto stmt_done;
34710 		}
34711 	      cp_parser_error (parser,
34712 			       "invalid form of %<#pragma omp atomic%>");
34713 	      goto saw_error;
34714 	    case CPP_MULT:
34715 	      opcode = MULT_EXPR;
34716 	      break;
34717 	    case CPP_DIV:
34718 	      opcode = TRUNC_DIV_EXPR;
34719 	      break;
34720 	    case CPP_PLUS:
34721 	      opcode = PLUS_EXPR;
34722 	      break;
34723 	    case CPP_MINUS:
34724 	      opcode = MINUS_EXPR;
34725 	      break;
34726 	    case CPP_LSHIFT:
34727 	      opcode = LSHIFT_EXPR;
34728 	      break;
34729 	    case CPP_RSHIFT:
34730 	      opcode = RSHIFT_EXPR;
34731 	      break;
34732 	    case CPP_AND:
34733 	      opcode = BIT_AND_EXPR;
34734 	      break;
34735 	    case CPP_OR:
34736 	      opcode = BIT_IOR_EXPR;
34737 	      break;
34738 	    case CPP_XOR:
34739 	      opcode = BIT_XOR_EXPR;
34740 	      break;
34741 	    default:
34742 	      cp_parser_error (parser,
34743 			       "invalid operator for %<#pragma omp atomic%>");
34744 	      goto saw_error;
34745 	    }
34746 	  oprec = TOKEN_PRECEDENCE (token);
34747 	  gcc_assert (oprec != PREC_NOT_OPERATOR);
34748 	  if (commutative_tree_code (opcode))
34749 	    oprec = (enum cp_parser_prec) (oprec - 1);
34750 	  cp_lexer_consume_token (parser->lexer);
34751 	  rhs = cp_parser_binary_expression (parser, false, false,
34752 					     oprec, NULL);
34753 	  if (rhs == error_mark_node)
34754 	    goto saw_error;
34755 	  goto stmt_done;
34756 	  /* FALLTHROUGH */
34757 	default:
34758 	  cp_parser_error (parser,
34759 			   "invalid operator for %<#pragma omp atomic%>");
34760 	  goto saw_error;
34761 	}
34762       cp_lexer_consume_token (parser->lexer);
34763 
34764       rhs = cp_parser_expression (parser);
34765       if (rhs == error_mark_node)
34766 	goto saw_error;
34767       break;
34768     }
34769 stmt_done:
34770   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34771     {
34772       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34773 	goto saw_error;
34774       v = cp_parser_unary_expression (parser);
34775       if (v == error_mark_node)
34776 	goto saw_error;
34777       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34778 	goto saw_error;
34779       lhs1 = cp_parser_unary_expression (parser);
34780       if (lhs1 == error_mark_node)
34781 	goto saw_error;
34782     }
34783   if (structured_block)
34784     {
34785       cp_parser_consume_semicolon_at_end_of_statement (parser);
34786       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34787     }
34788 done:
34789   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34790   if (!structured_block)
34791     cp_parser_consume_semicolon_at_end_of_statement (parser);
34792   return;
34793 
34794  saw_error:
34795   cp_parser_skip_to_end_of_block_or_statement (parser);
34796   if (structured_block)
34797     {
34798       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34799         cp_lexer_consume_token (parser->lexer);
34800       else if (code == OMP_ATOMIC_CAPTURE_NEW)
34801 	{
34802 	  cp_parser_skip_to_end_of_block_or_statement (parser);
34803 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34804 	    cp_lexer_consume_token (parser->lexer);
34805 	}
34806     }
34807 }
34808 
34809 
34810 /* OpenMP 2.5:
34811    # pragma omp barrier new-line  */
34812 
34813 static void
cp_parser_omp_barrier(cp_parser * parser,cp_token * pragma_tok)34814 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34815 {
34816   cp_parser_require_pragma_eol (parser, pragma_tok);
34817   finish_omp_barrier ();
34818 }
34819 
34820 /* OpenMP 2.5:
34821    # pragma omp critical [(name)] new-line
34822      structured-block
34823 
34824    OpenMP 4.5:
34825    # pragma omp critical [(name) [hint(expression)]] new-line
34826      structured-block  */
34827 
34828 #define OMP_CRITICAL_CLAUSE_MASK		\
34829 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34830 
34831 static tree
cp_parser_omp_critical(cp_parser * parser,cp_token * pragma_tok,bool * if_p)34832 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34833 {
34834   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34835 
34836   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34837     {
34838       matching_parens parens;
34839       parens.consume_open (parser);
34840 
34841       name = cp_parser_identifier (parser);
34842 
34843       if (name == error_mark_node
34844 	  || !parens.require_close (parser))
34845 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34846 					       /*or_comma=*/false,
34847 					       /*consume_paren=*/true);
34848       if (name == error_mark_node)
34849 	name = NULL;
34850 
34851       clauses = cp_parser_omp_all_clauses (parser,
34852 					   OMP_CRITICAL_CLAUSE_MASK,
34853 					   "#pragma omp critical", pragma_tok);
34854     }
34855   else
34856     cp_parser_require_pragma_eol (parser, pragma_tok);
34857 
34858   stmt = cp_parser_omp_structured_block (parser, if_p);
34859   return c_finish_omp_critical (input_location, stmt, name, clauses);
34860 }
34861 
34862 /* OpenMP 2.5:
34863    # pragma omp flush flush-vars[opt] new-line
34864 
34865    flush-vars:
34866      ( variable-list ) */
34867 
34868 static void
cp_parser_omp_flush(cp_parser * parser,cp_token * pragma_tok)34869 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34870 {
34871   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34872     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34873   cp_parser_require_pragma_eol (parser, pragma_tok);
34874 
34875   finish_omp_flush ();
34876 }
34877 
34878 /* Helper function, to parse omp for increment expression.  */
34879 
34880 static tree
cp_parser_omp_for_cond(cp_parser * parser,tree decl)34881 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34882 {
34883   tree cond = cp_parser_binary_expression (parser, false, true,
34884 					   PREC_NOT_OPERATOR, NULL);
34885   if (cond == error_mark_node
34886       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34887     {
34888       cp_parser_skip_to_end_of_statement (parser);
34889       return error_mark_node;
34890     }
34891 
34892   switch (TREE_CODE (cond))
34893     {
34894     case GT_EXPR:
34895     case GE_EXPR:
34896     case LT_EXPR:
34897     case LE_EXPR:
34898       break;
34899     case NE_EXPR:
34900       /* Fall through: OpenMP disallows NE_EXPR.  */
34901       gcc_fallthrough ();
34902     default:
34903       return error_mark_node;
34904     }
34905 
34906   /* If decl is an iterator, preserve LHS and RHS of the relational
34907      expr until finish_omp_for.  */
34908   if (decl
34909       && (type_dependent_expression_p (decl)
34910 	  || CLASS_TYPE_P (TREE_TYPE (decl))))
34911     return cond;
34912 
34913   return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34914 			    TREE_CODE (cond),
34915 			    TREE_OPERAND (cond, 0), ERROR_MARK,
34916 			    TREE_OPERAND (cond, 1), ERROR_MARK,
34917 			    /*overload=*/NULL, tf_warning_or_error);
34918 }
34919 
34920 /* Helper function, to parse omp for increment expression.  */
34921 
34922 static tree
cp_parser_omp_for_incr(cp_parser * parser,tree decl)34923 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34924 {
34925   cp_token *token = cp_lexer_peek_token (parser->lexer);
34926   enum tree_code op;
34927   tree lhs, rhs;
34928   cp_id_kind idk;
34929   bool decl_first;
34930 
34931   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34932     {
34933       op = (token->type == CPP_PLUS_PLUS
34934 	    ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34935       cp_lexer_consume_token (parser->lexer);
34936       lhs = cp_parser_simple_cast_expression (parser);
34937       if (lhs != decl
34938 	  && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34939 	return error_mark_node;
34940       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34941     }
34942 
34943   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34944   if (lhs != decl
34945       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34946     return error_mark_node;
34947 
34948   token = cp_lexer_peek_token (parser->lexer);
34949   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34950     {
34951       op = (token->type == CPP_PLUS_PLUS
34952 	    ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34953       cp_lexer_consume_token (parser->lexer);
34954       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34955     }
34956 
34957   op = cp_parser_assignment_operator_opt (parser);
34958   if (op == ERROR_MARK)
34959     return error_mark_node;
34960 
34961   if (op != NOP_EXPR)
34962     {
34963       rhs = cp_parser_assignment_expression (parser);
34964       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34965       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34966     }
34967 
34968   lhs = cp_parser_binary_expression (parser, false, false,
34969 				     PREC_ADDITIVE_EXPRESSION, NULL);
34970   token = cp_lexer_peek_token (parser->lexer);
34971   decl_first = (lhs == decl
34972 		|| (processing_template_decl && cp_tree_equal (lhs, decl)));
34973   if (decl_first)
34974     lhs = NULL_TREE;
34975   if (token->type != CPP_PLUS
34976       && token->type != CPP_MINUS)
34977     return error_mark_node;
34978 
34979   do
34980     {
34981       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34982       cp_lexer_consume_token (parser->lexer);
34983       rhs = cp_parser_binary_expression (parser, false, false,
34984 					 PREC_ADDITIVE_EXPRESSION, NULL);
34985       token = cp_lexer_peek_token (parser->lexer);
34986       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34987 	{
34988 	  if (lhs == NULL_TREE)
34989 	    {
34990 	      if (op == PLUS_EXPR)
34991 		lhs = rhs;
34992 	      else
34993 		lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34994 					tf_warning_or_error);
34995 	    }
34996 	  else
34997 	    lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34998 				     ERROR_MARK, NULL, tf_warning_or_error);
34999 	}
35000     }
35001   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35002 
35003   if (!decl_first)
35004     {
35005       if ((rhs != decl
35006 	   && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35007 	  || op == MINUS_EXPR)
35008 	return error_mark_node;
35009       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35010     }
35011   else
35012     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35013 
35014   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35015 }
35016 
35017 /* Parse the initialization statement of an OpenMP for loop.
35018 
35019    Return true if the resulting construct should have an
35020    OMP_CLAUSE_PRIVATE added to it.  */
35021 
35022 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)35023 cp_parser_omp_for_loop_init (cp_parser *parser,
35024 			     tree &this_pre_body,
35025 			     vec<tree, va_gc> *&for_block,
35026 			     tree &init,
35027 			     tree &orig_init,
35028 			     tree &decl,
35029 			     tree &real_decl)
35030 {
35031   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35032     return NULL_TREE;
35033 
35034   tree add_private_clause = NULL_TREE;
35035 
35036   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35037 
35038      init-expr:
35039      var = lb
35040      integer-type var = lb
35041      random-access-iterator-type var = lb
35042      pointer-type var = lb
35043   */
35044   cp_decl_specifier_seq type_specifiers;
35045 
35046   /* First, try to parse as an initialized declaration.  See
35047      cp_parser_condition, from whence the bulk of this is copied.  */
35048 
35049   cp_parser_parse_tentatively (parser);
35050   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35051 				/*is_trailing_return=*/false,
35052 				&type_specifiers);
35053   if (cp_parser_parse_definitely (parser))
35054     {
35055       /* If parsing a type specifier seq succeeded, then this
35056 	 MUST be a initialized declaration.  */
35057       tree asm_specification, attributes;
35058       cp_declarator *declarator;
35059 
35060       declarator = cp_parser_declarator (parser,
35061 					 CP_PARSER_DECLARATOR_NAMED,
35062 					 /*ctor_dtor_or_conv_p=*/NULL,
35063 					 /*parenthesized_p=*/NULL,
35064 					 /*member_p=*/false,
35065 					 /*friend_p=*/false);
35066       attributes = cp_parser_attributes_opt (parser);
35067       asm_specification = cp_parser_asm_specification_opt (parser);
35068 
35069       if (declarator == cp_error_declarator)
35070 	cp_parser_skip_to_end_of_statement (parser);
35071 
35072       else
35073 	{
35074 	  tree pushed_scope, auto_node;
35075 
35076 	  decl = start_decl (declarator, &type_specifiers,
35077 			     SD_INITIALIZED, attributes,
35078 			     /*prefix_attributes=*/NULL_TREE,
35079 			     &pushed_scope);
35080 
35081 	  auto_node = type_uses_auto (TREE_TYPE (decl));
35082 	  if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35083 	    {
35084 	      if (cp_lexer_next_token_is (parser->lexer,
35085 					  CPP_OPEN_PAREN))
35086 	        error ("parenthesized initialization is not allowed in "
35087 		       "OpenMP %<for%> loop");
35088 	      else
35089 		/* Trigger an error.  */
35090 		cp_parser_require (parser, CPP_EQ, RT_EQ);
35091 
35092 	      init = error_mark_node;
35093 	      cp_parser_skip_to_end_of_statement (parser);
35094 	    }
35095 	  else if (CLASS_TYPE_P (TREE_TYPE (decl))
35096 		   || type_dependent_expression_p (decl)
35097 		   || auto_node)
35098 	    {
35099 	      bool is_direct_init, is_non_constant_init;
35100 
35101 	      init = cp_parser_initializer (parser,
35102 					    &is_direct_init,
35103 					    &is_non_constant_init);
35104 
35105 	      if (auto_node)
35106 		{
35107 		  TREE_TYPE (decl)
35108 		    = do_auto_deduction (TREE_TYPE (decl), init,
35109 					 auto_node);
35110 
35111 		  if (!CLASS_TYPE_P (TREE_TYPE (decl))
35112 		      && !type_dependent_expression_p (decl))
35113 		    goto non_class;
35114 		}
35115 
35116 	      cp_finish_decl (decl, init, !is_non_constant_init,
35117 			      asm_specification,
35118 			      LOOKUP_ONLYCONVERTING);
35119 	      orig_init = init;
35120 	      if (CLASS_TYPE_P (TREE_TYPE (decl)))
35121 		{
35122 		  vec_safe_push (for_block, this_pre_body);
35123 		  init = NULL_TREE;
35124 		}
35125 	      else
35126 		{
35127 		  init = pop_stmt_list (this_pre_body);
35128 		  if (init && TREE_CODE (init) == STATEMENT_LIST)
35129 		    {
35130 		      tree_stmt_iterator i = tsi_start (init);
35131 		      /* Move lambda DECL_EXPRs to FOR_BLOCK.  */
35132 		      while (!tsi_end_p (i))
35133 			{
35134 			  tree t = tsi_stmt (i);
35135 			  if (TREE_CODE (t) == DECL_EXPR
35136 			      && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35137 			    {
35138 			      tsi_delink (&i);
35139 			      vec_safe_push (for_block, t);
35140 			      continue;
35141 			    }
35142 			  break;
35143 			}
35144 		      if (tsi_one_before_end_p (i))
35145 			{
35146 			  tree t = tsi_stmt (i);
35147 			  tsi_delink (&i);
35148 			  free_stmt_list (init);
35149 			  init = t;
35150 			}
35151 		    }
35152 		}
35153 	      this_pre_body = NULL_TREE;
35154 	    }
35155 	  else
35156 	    {
35157 	      /* Consume '='.  */
35158 	      cp_lexer_consume_token (parser->lexer);
35159 	      init = cp_parser_assignment_expression (parser);
35160 
35161 	    non_class:
35162 	      if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
35163 		init = error_mark_node;
35164 	      else
35165 		cp_finish_decl (decl, NULL_TREE,
35166 				/*init_const_expr_p=*/false,
35167 				asm_specification,
35168 				LOOKUP_ONLYCONVERTING);
35169 	    }
35170 
35171 	  if (pushed_scope)
35172 	    pop_scope (pushed_scope);
35173 	}
35174     }
35175   else
35176     {
35177       cp_id_kind idk;
35178       /* If parsing a type specifier sequence failed, then
35179 	 this MUST be a simple expression.  */
35180       cp_parser_parse_tentatively (parser);
35181       decl = cp_parser_primary_expression (parser, false, false,
35182 					   false, &idk);
35183       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35184       if (!cp_parser_error_occurred (parser)
35185 	  && decl
35186 	  && (TREE_CODE (decl) == COMPONENT_REF
35187 	      || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35188 	{
35189 	  cp_parser_abort_tentative_parse (parser);
35190 	  cp_parser_parse_tentatively (parser);
35191 	  cp_token *token = cp_lexer_peek_token (parser->lexer);
35192 	  tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35193 					       /*check_dependency_p=*/true,
35194 					       /*template_p=*/NULL,
35195 					       /*declarator_p=*/false,
35196 					       /*optional_p=*/false);
35197 	  if (name != error_mark_node
35198 	      && last_tok == cp_lexer_peek_token (parser->lexer))
35199 	    {
35200 	      decl = cp_parser_lookup_name_simple (parser, name,
35201 						   token->location);
35202 	      if (TREE_CODE (decl) == FIELD_DECL)
35203 		add_private_clause = omp_privatize_field (decl, false);
35204 	    }
35205 	  cp_parser_abort_tentative_parse (parser);
35206 	  cp_parser_parse_tentatively (parser);
35207 	  decl = cp_parser_primary_expression (parser, false, false,
35208 					       false, &idk);
35209 	}
35210       if (!cp_parser_error_occurred (parser)
35211 	  && decl
35212 	  && DECL_P (decl)
35213 	  && CLASS_TYPE_P (TREE_TYPE (decl)))
35214 	{
35215 	  tree rhs;
35216 
35217 	  cp_parser_parse_definitely (parser);
35218 	  cp_parser_require (parser, CPP_EQ, RT_EQ);
35219 	  rhs = cp_parser_assignment_expression (parser);
35220 	  orig_init = rhs;
35221 	  finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35222 						 decl, NOP_EXPR,
35223 						 rhs,
35224 						 tf_warning_or_error));
35225 	  if (!add_private_clause)
35226 	    add_private_clause = decl;
35227 	}
35228       else
35229 	{
35230 	  decl = NULL;
35231 	  cp_parser_abort_tentative_parse (parser);
35232 	  init = cp_parser_expression (parser);
35233 	  if (init)
35234 	    {
35235 	      if (TREE_CODE (init) == MODIFY_EXPR
35236 		  || TREE_CODE (init) == MODOP_EXPR)
35237 		real_decl = TREE_OPERAND (init, 0);
35238 	    }
35239 	}
35240     }
35241   return add_private_clause;
35242 }
35243 
35244 /* Parse the restricted form of the for statement allowed by OpenMP.  */
35245 
35246 static tree
cp_parser_omp_for_loop(cp_parser * parser,enum tree_code code,tree clauses,tree * cclauses,bool * if_p)35247 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35248 			tree *cclauses, bool *if_p)
35249 {
35250   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35251   tree real_decl, initv, condv, incrv, declv;
35252   tree this_pre_body, cl, ordered_cl = NULL_TREE;
35253   location_t loc_first;
35254   bool collapse_err = false;
35255   int i, collapse = 1, ordered = 0, count, nbraces = 0;
35256   vec<tree, va_gc> *for_block = make_tree_vector ();
35257   auto_vec<tree, 4> orig_inits;
35258   bool tiling = false;
35259 
35260   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35261     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35262       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35263     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35264       {
35265 	tiling = true;
35266 	collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35267       }
35268     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35269 	     && OMP_CLAUSE_ORDERED_EXPR (cl))
35270       {
35271 	ordered_cl = cl;
35272 	ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35273       }
35274 
35275   if (ordered && ordered < collapse)
35276     {
35277       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35278 		"%<ordered%> clause parameter is less than %<collapse%>");
35279       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35280 	= build_int_cst (NULL_TREE, collapse);
35281       ordered = collapse;
35282     }
35283   if (ordered)
35284     {
35285       for (tree *pc = &clauses; *pc; )
35286 	if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35287 	  {
35288 	    error_at (OMP_CLAUSE_LOCATION (*pc),
35289 		      "%<linear%> clause may not be specified together "
35290 		      "with %<ordered%> clause with a parameter");
35291 	    *pc = OMP_CLAUSE_CHAIN (*pc);
35292 	  }
35293 	else
35294 	  pc = &OMP_CLAUSE_CHAIN (*pc);
35295     }
35296 
35297   gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35298   count = ordered ? ordered : collapse;
35299 
35300   declv = make_tree_vec (count);
35301   initv = make_tree_vec (count);
35302   condv = make_tree_vec (count);
35303   incrv = make_tree_vec (count);
35304 
35305   loc_first = cp_lexer_peek_token (parser->lexer)->location;
35306 
35307   for (i = 0; i < count; i++)
35308     {
35309       int bracecount = 0;
35310       tree add_private_clause = NULL_TREE;
35311       location_t loc;
35312 
35313       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35314 	{
35315 	  if (!collapse_err)
35316 	    cp_parser_error (parser, "for statement expected");
35317 	  return NULL;
35318 	}
35319       loc = cp_lexer_consume_token (parser->lexer)->location;
35320 
35321       matching_parens parens;
35322       if (!parens.require_open (parser))
35323 	return NULL;
35324 
35325       init = orig_init = decl = real_decl = NULL;
35326       this_pre_body = push_stmt_list ();
35327 
35328       add_private_clause
35329 	= cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35330 				       init, orig_init, decl, real_decl);
35331 
35332       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35333       if (this_pre_body)
35334 	{
35335 	  this_pre_body = pop_stmt_list (this_pre_body);
35336 	  if (pre_body)
35337 	    {
35338 	      tree t = pre_body;
35339 	      pre_body = push_stmt_list ();
35340 	      add_stmt (t);
35341 	      add_stmt (this_pre_body);
35342 	      pre_body = pop_stmt_list (pre_body);
35343 	    }
35344 	  else
35345 	    pre_body = this_pre_body;
35346 	}
35347 
35348       if (decl)
35349 	real_decl = decl;
35350       if (cclauses != NULL
35351 	  && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35352 	  && real_decl != NULL_TREE)
35353 	{
35354 	  tree *c;
35355 	  for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35356 	    if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35357 		&& OMP_CLAUSE_DECL (*c) == real_decl)
35358 	      {
35359 		error_at (loc, "iteration variable %qD"
35360 			  " should not be firstprivate", real_decl);
35361 		*c = OMP_CLAUSE_CHAIN (*c);
35362 	      }
35363 	    else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35364 		     && OMP_CLAUSE_DECL (*c) == real_decl)
35365 	      {
35366 		/* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
35367 		tree l = *c;
35368 		*c = OMP_CLAUSE_CHAIN (*c);
35369 		if (code == OMP_SIMD)
35370 		  {
35371 		    OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35372 		    cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35373 		  }
35374 		else
35375 		  {
35376 		    OMP_CLAUSE_CHAIN (l) = clauses;
35377 		    clauses = l;
35378 		  }
35379 		add_private_clause = NULL_TREE;
35380 	      }
35381 	    else
35382 	      {
35383 		if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35384 		    && OMP_CLAUSE_DECL (*c) == real_decl)
35385 		  add_private_clause = NULL_TREE;
35386 		c = &OMP_CLAUSE_CHAIN (*c);
35387 	      }
35388 	}
35389 
35390       if (add_private_clause)
35391 	{
35392 	  tree c;
35393 	  for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35394 	    {
35395 	      if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35396 		   || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35397 		  && OMP_CLAUSE_DECL (c) == decl)
35398 		break;
35399 	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35400 		       && OMP_CLAUSE_DECL (c) == decl)
35401 		error_at (loc, "iteration variable %qD "
35402 			  "should not be firstprivate",
35403 			  decl);
35404 	      else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35405 		       && OMP_CLAUSE_DECL (c) == decl)
35406 		error_at (loc, "iteration variable %qD should not be reduction",
35407 			  decl);
35408 	    }
35409 	  if (c == NULL)
35410 	    {
35411 	      if (code != OMP_SIMD)
35412 		c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35413 	      else if (collapse == 1)
35414 		c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35415 	      else
35416 		c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35417 	      OMP_CLAUSE_DECL (c) = add_private_clause;
35418 	      c = finish_omp_clauses (c, C_ORT_OMP);
35419 	      if (c)
35420 		{
35421 		  OMP_CLAUSE_CHAIN (c) = clauses;
35422 		  clauses = c;
35423 		  /* For linear, signal that we need to fill up
35424 		     the so far unknown linear step.  */
35425 		  if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35426 		    OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35427 		}
35428 	    }
35429 	}
35430 
35431       cond = NULL;
35432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35433 	cond = cp_parser_omp_for_cond (parser, decl);
35434       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35435 
35436       incr = NULL;
35437       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35438 	{
35439 	  /* If decl is an iterator, preserve the operator on decl
35440 	     until finish_omp_for.  */
35441 	  if (real_decl
35442 	      && ((processing_template_decl
35443 		   && (TREE_TYPE (real_decl) == NULL_TREE
35444 		       || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35445 		  || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35446 	    incr = cp_parser_omp_for_incr (parser, real_decl);
35447 	  else
35448 	    incr = cp_parser_expression (parser);
35449 	  if (!EXPR_HAS_LOCATION (incr))
35450 	    protected_set_expr_location (incr, input_location);
35451 	}
35452 
35453       if (!parens.require_close (parser))
35454 	cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35455 					       /*or_comma=*/false,
35456 					       /*consume_paren=*/true);
35457 
35458       TREE_VEC_ELT (declv, i) = decl;
35459       TREE_VEC_ELT (initv, i) = init;
35460       TREE_VEC_ELT (condv, i) = cond;
35461       TREE_VEC_ELT (incrv, i) = incr;
35462       if (orig_init)
35463 	{
35464 	  orig_inits.safe_grow_cleared (i + 1);
35465 	  orig_inits[i] = orig_init;
35466 	}
35467 
35468       if (i == count - 1)
35469 	break;
35470 
35471       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35472 	 in between the collapsed for loops to be still considered perfectly
35473 	 nested.  Hopefully the final version clarifies this.
35474 	 For now handle (multiple) {'s and empty statements.  */
35475       cp_parser_parse_tentatively (parser);
35476       for (;;)
35477 	{
35478 	  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35479 	    break;
35480 	  else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35481 	    {
35482 	      cp_lexer_consume_token (parser->lexer);
35483 	      bracecount++;
35484 	    }
35485 	  else if (bracecount
35486 		   && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35487 	    cp_lexer_consume_token (parser->lexer);
35488 	  else
35489 	    {
35490 	      loc = cp_lexer_peek_token (parser->lexer)->location;
35491 	      error_at (loc, "not enough for loops to collapse");
35492 	      collapse_err = true;
35493 	      cp_parser_abort_tentative_parse (parser);
35494 	      declv = NULL_TREE;
35495 	      break;
35496 	    }
35497 	}
35498 
35499       if (declv)
35500 	{
35501 	  cp_parser_parse_definitely (parser);
35502 	  nbraces += bracecount;
35503 	}
35504     }
35505 
35506   if (nbraces)
35507     if_p = NULL;
35508 
35509   /* Note that we saved the original contents of this flag when we entered
35510      the structured block, and so we don't need to re-save it here.  */
35511   parser->in_statement = IN_OMP_FOR;
35512 
35513   /* Note that the grammar doesn't call for a structured block here,
35514      though the loop as a whole is a structured block.  */
35515   body = push_stmt_list ();
35516   cp_parser_statement (parser, NULL_TREE, false, if_p);
35517   body = pop_stmt_list (body);
35518 
35519   if (declv == NULL_TREE)
35520     ret = NULL_TREE;
35521   else
35522     ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35523 			  body, pre_body, &orig_inits, clauses);
35524 
35525   while (nbraces)
35526     {
35527       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35528 	{
35529 	  cp_lexer_consume_token (parser->lexer);
35530 	  nbraces--;
35531 	}
35532       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35533 	cp_lexer_consume_token (parser->lexer);
35534       else
35535 	{
35536 	  if (!collapse_err)
35537 	    {
35538 	      error_at (cp_lexer_peek_token (parser->lexer)->location,
35539 			"collapsed loops not perfectly nested");
35540 	    }
35541 	  collapse_err = true;
35542 	  cp_parser_statement_seq_opt (parser, NULL);
35543 	  if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35544 	    break;
35545 	}
35546     }
35547 
35548   while (!for_block->is_empty ())
35549     {
35550       tree t = for_block->pop ();
35551       if (TREE_CODE (t) == STATEMENT_LIST)
35552 	add_stmt (pop_stmt_list (t));
35553       else
35554 	add_stmt (t);
35555     }
35556   release_tree_vector (for_block);
35557 
35558   return ret;
35559 }
35560 
35561 /* Helper function for OpenMP parsing, split clauses and call
35562    finish_omp_clauses on each of the set of clauses afterwards.  */
35563 
35564 static void
cp_omp_split_clauses(location_t loc,enum tree_code code,omp_clause_mask mask,tree clauses,tree * cclauses)35565 cp_omp_split_clauses (location_t loc, enum tree_code code,
35566 		      omp_clause_mask mask, tree clauses, tree *cclauses)
35567 {
35568   int i;
35569   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35570   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35571     if (cclauses[i])
35572       cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35573 }
35574 
35575 /* OpenMP 4.0:
35576    #pragma omp simd simd-clause[optseq] new-line
35577      for-loop  */
35578 
35579 #define OMP_SIMD_CLAUSE_MASK					\
35580 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)	\
35581 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
35582 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
35583 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
35584 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
35585 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
35586 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
35587 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35588 
35589 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)35590 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35591 		    char *p_name, omp_clause_mask mask, tree *cclauses,
35592 		    bool *if_p)
35593 {
35594   tree clauses, sb, ret;
35595   unsigned int save;
35596   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35597 
35598   strcat (p_name, " simd");
35599   mask |= OMP_SIMD_CLAUSE_MASK;
35600 
35601   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35602 				       cclauses == NULL);
35603   if (cclauses)
35604     {
35605       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35606       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35607       tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35608 				OMP_CLAUSE_ORDERED);
35609       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35610 	{
35611 	  error_at (OMP_CLAUSE_LOCATION (c),
35612 		    "%<ordered%> clause with parameter may not be specified "
35613 		    "on %qs construct", p_name);
35614 	  OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35615 	}
35616     }
35617 
35618   sb = begin_omp_structured_block ();
35619   save = cp_parser_begin_omp_structured_block (parser);
35620 
35621   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35622 
35623   cp_parser_end_omp_structured_block (parser, save);
35624   add_stmt (finish_omp_structured_block (sb));
35625 
35626   return ret;
35627 }
35628 
35629 /* OpenMP 2.5:
35630    #pragma omp for for-clause[optseq] new-line
35631      for-loop
35632 
35633    OpenMP 4.0:
35634    #pragma omp for simd for-simd-clause[optseq] new-line
35635      for-loop  */
35636 
35637 #define OMP_FOR_CLAUSE_MASK					\
35638 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
35639 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
35640 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
35641 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
35642 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
35643 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)	\
35644 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)	\
35645 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
35646 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35647 
35648 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)35649 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35650 		   char *p_name, omp_clause_mask mask, tree *cclauses,
35651 		   bool *if_p)
35652 {
35653   tree clauses, sb, ret;
35654   unsigned int save;
35655   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35656 
35657   strcat (p_name, " for");
35658   mask |= OMP_FOR_CLAUSE_MASK;
35659   /* parallel for{, simd} disallows nowait clause, but for
35660      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
35661   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35662     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35663   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
35664   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35665     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35666 
35667   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35668     {
35669       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35670       const char *p = IDENTIFIER_POINTER (id);
35671 
35672       if (strcmp (p, "simd") == 0)
35673 	{
35674 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35675 	  if (cclauses == NULL)
35676 	    cclauses = cclauses_buf;
35677 
35678 	  cp_lexer_consume_token (parser->lexer);
35679 	  if (!flag_openmp)  /* flag_openmp_simd  */
35680 	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35681 				       cclauses, if_p);
35682 	  sb = begin_omp_structured_block ();
35683 	  save = cp_parser_begin_omp_structured_block (parser);
35684 	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35685 				    cclauses, if_p);
35686 	  cp_parser_end_omp_structured_block (parser, save);
35687 	  tree body = finish_omp_structured_block (sb);
35688 	  if (ret == NULL)
35689 	    return ret;
35690 	  ret = make_node (OMP_FOR);
35691 	  TREE_TYPE (ret) = void_type_node;
35692 	  OMP_FOR_BODY (ret) = body;
35693 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35694 	  SET_EXPR_LOCATION (ret, loc);
35695 	  add_stmt (ret);
35696 	  return ret;
35697 	}
35698     }
35699   if (!flag_openmp)  /* flag_openmp_simd  */
35700     {
35701       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35702       return NULL_TREE;
35703     }
35704 
35705   /* Composite distribute parallel for disallows linear clause.  */
35706   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35707     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35708 
35709   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35710 				       cclauses == NULL);
35711   if (cclauses)
35712     {
35713       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35714       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35715     }
35716 
35717   sb = begin_omp_structured_block ();
35718   save = cp_parser_begin_omp_structured_block (parser);
35719 
35720   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35721 
35722   cp_parser_end_omp_structured_block (parser, save);
35723   add_stmt (finish_omp_structured_block (sb));
35724 
35725   return ret;
35726 }
35727 
35728 /* OpenMP 2.5:
35729    # pragma omp master new-line
35730      structured-block  */
35731 
35732 static tree
cp_parser_omp_master(cp_parser * parser,cp_token * pragma_tok,bool * if_p)35733 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35734 {
35735   cp_parser_require_pragma_eol (parser, pragma_tok);
35736   return c_finish_omp_master (input_location,
35737 			      cp_parser_omp_structured_block (parser, if_p));
35738 }
35739 
35740 /* OpenMP 2.5:
35741    # pragma omp ordered new-line
35742      structured-block
35743 
35744    OpenMP 4.5:
35745    # pragma omp ordered ordered-clauses new-line
35746      structured-block  */
35747 
35748 #define OMP_ORDERED_CLAUSE_MASK					\
35749 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)	\
35750 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35751 
35752 #define OMP_ORDERED_DEPEND_CLAUSE_MASK				\
35753 	(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35754 
35755 static bool
cp_parser_omp_ordered(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context,bool * if_p)35756 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35757 		       enum pragma_context context, bool *if_p)
35758 {
35759   location_t loc = pragma_tok->location;
35760 
35761   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35762     {
35763       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35764       const char *p = IDENTIFIER_POINTER (id);
35765 
35766       if (strcmp (p, "depend") == 0)
35767 	{
35768 	  if (!flag_openmp)	/* flag_openmp_simd */
35769 	    {
35770 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35771 	      return false;
35772 	    }
35773 	  if (context == pragma_stmt)
35774 	    {
35775 	      error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35776 			"%<depend%> clause may only be used in compound "
35777 			"statements");
35778 	      cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35779 	      return false;
35780 	    }
35781 	  tree clauses
35782 	    = cp_parser_omp_all_clauses (parser,
35783 					 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35784 					 "#pragma omp ordered", pragma_tok);
35785 	  c_finish_omp_ordered (loc, clauses, NULL_TREE);
35786 	  return false;
35787 	}
35788     }
35789 
35790   tree clauses
35791     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35792 				 "#pragma omp ordered", pragma_tok);
35793 
35794   if (!flag_openmp     /* flag_openmp_simd  */
35795       && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35796     return false;
35797 
35798   c_finish_omp_ordered (loc, clauses,
35799 			cp_parser_omp_structured_block (parser, if_p));
35800   return true;
35801 }
35802 
35803 /* OpenMP 2.5:
35804 
35805    section-scope:
35806      { section-sequence }
35807 
35808    section-sequence:
35809      section-directive[opt] structured-block
35810      section-sequence section-directive structured-block  */
35811 
35812 static tree
cp_parser_omp_sections_scope(cp_parser * parser)35813 cp_parser_omp_sections_scope (cp_parser *parser)
35814 {
35815   tree stmt, substmt;
35816   bool error_suppress = false;
35817   cp_token *tok;
35818 
35819   matching_braces braces;
35820   if (!braces.require_open (parser))
35821     return NULL_TREE;
35822 
35823   stmt = push_stmt_list ();
35824 
35825   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35826       != PRAGMA_OMP_SECTION)
35827     {
35828       substmt = cp_parser_omp_structured_block (parser, NULL);
35829       substmt = build1 (OMP_SECTION, void_type_node, substmt);
35830       add_stmt (substmt);
35831     }
35832 
35833   while (1)
35834     {
35835       tok = cp_lexer_peek_token (parser->lexer);
35836       if (tok->type == CPP_CLOSE_BRACE)
35837 	break;
35838       if (tok->type == CPP_EOF)
35839 	break;
35840 
35841       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35842 	{
35843 	  cp_lexer_consume_token (parser->lexer);
35844 	  cp_parser_require_pragma_eol (parser, tok);
35845 	  error_suppress = false;
35846 	}
35847       else if (!error_suppress)
35848 	{
35849 	  cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35850 	  error_suppress = true;
35851 	}
35852 
35853       substmt = cp_parser_omp_structured_block (parser, NULL);
35854       substmt = build1 (OMP_SECTION, void_type_node, substmt);
35855       add_stmt (substmt);
35856     }
35857   braces.require_close (parser);
35858 
35859   substmt = pop_stmt_list (stmt);
35860 
35861   stmt = make_node (OMP_SECTIONS);
35862   TREE_TYPE (stmt) = void_type_node;
35863   OMP_SECTIONS_BODY (stmt) = substmt;
35864 
35865   add_stmt (stmt);
35866   return stmt;
35867 }
35868 
35869 /* OpenMP 2.5:
35870    # pragma omp sections sections-clause[optseq] newline
35871      sections-scope  */
35872 
35873 #define OMP_SECTIONS_CLAUSE_MASK				\
35874 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
35875 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
35876 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
35877 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
35878 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35879 
35880 static tree
cp_parser_omp_sections(cp_parser * parser,cp_token * pragma_tok,char * p_name,omp_clause_mask mask,tree * cclauses)35881 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35882 			char *p_name, omp_clause_mask mask, tree *cclauses)
35883 {
35884   tree clauses, ret;
35885   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35886 
35887   strcat (p_name, " sections");
35888   mask |= OMP_SECTIONS_CLAUSE_MASK;
35889   if (cclauses)
35890     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35891 
35892   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35893 				       cclauses == NULL);
35894   if (cclauses)
35895     {
35896       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35897       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35898     }
35899 
35900   ret = cp_parser_omp_sections_scope (parser);
35901   if (ret)
35902     OMP_SECTIONS_CLAUSES (ret) = clauses;
35903 
35904   return ret;
35905 }
35906 
35907 /* OpenMP 2.5:
35908    # pragma omp parallel parallel-clause[optseq] new-line
35909      structured-block
35910    # pragma omp parallel for parallel-for-clause[optseq] new-line
35911      structured-block
35912    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35913      structured-block
35914 
35915    OpenMP 4.0:
35916    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35917      structured-block */
35918 
35919 #define OMP_PARALLEL_CLAUSE_MASK				\
35920 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
35921 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
35922 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
35923 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
35924 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
35925 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)	\
35926 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
35927 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)	\
35928 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35929 
35930 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)35931 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35932 			char *p_name, omp_clause_mask mask, tree *cclauses,
35933 			bool *if_p)
35934 {
35935   tree stmt, clauses, block;
35936   unsigned int save;
35937   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35938 
35939   strcat (p_name, " parallel");
35940   mask |= OMP_PARALLEL_CLAUSE_MASK;
35941   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
35942   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35943       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35944     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35945 
35946   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35947     {
35948       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35949       if (cclauses == NULL)
35950 	cclauses = cclauses_buf;
35951 
35952       cp_lexer_consume_token (parser->lexer);
35953       if (!flag_openmp)  /* flag_openmp_simd  */
35954 	return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35955 				  if_p);
35956       block = begin_omp_parallel ();
35957       save = cp_parser_begin_omp_structured_block (parser);
35958       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35959 				    if_p);
35960       cp_parser_end_omp_structured_block (parser, save);
35961       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35962 				  block);
35963       if (ret == NULL_TREE)
35964 	return ret;
35965       OMP_PARALLEL_COMBINED (stmt) = 1;
35966       return stmt;
35967     }
35968   /* When combined with distribute, parallel has to be followed by for.
35969      #pragma omp target parallel is allowed though.  */
35970   else if (cclauses
35971 	   && (mask & (OMP_CLAUSE_MASK_1
35972 		       << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35973     {
35974       error_at (loc, "expected %<for%> after %qs", p_name);
35975       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35976       return NULL_TREE;
35977     }
35978   else if (!flag_openmp)  /* flag_openmp_simd  */
35979     {
35980       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35981       return NULL_TREE;
35982     }
35983   else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35984     {
35985       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35986       const char *p = IDENTIFIER_POINTER (id);
35987       if (strcmp (p, "sections") == 0)
35988 	{
35989 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35990 	  cclauses = cclauses_buf;
35991 
35992 	  cp_lexer_consume_token (parser->lexer);
35993 	  block = begin_omp_parallel ();
35994 	  save = cp_parser_begin_omp_structured_block (parser);
35995 	  cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35996 	  cp_parser_end_omp_structured_block (parser, save);
35997 	  stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35998 				      block);
35999 	  OMP_PARALLEL_COMBINED (stmt) = 1;
36000 	  return stmt;
36001 	}
36002     }
36003 
36004   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36005 				       cclauses == NULL);
36006   if (cclauses)
36007     {
36008       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
36009       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
36010     }
36011 
36012   block = begin_omp_parallel ();
36013   save = cp_parser_begin_omp_structured_block (parser);
36014   cp_parser_statement (parser, NULL_TREE, false, if_p);
36015   cp_parser_end_omp_structured_block (parser, save);
36016   stmt = finish_omp_parallel (clauses, block);
36017   return stmt;
36018 }
36019 
36020 /* OpenMP 2.5:
36021    # pragma omp single single-clause[optseq] new-line
36022      structured-block  */
36023 
36024 #define OMP_SINGLE_CLAUSE_MASK					\
36025 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
36026 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
36027 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)	\
36028 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36029 
36030 static tree
cp_parser_omp_single(cp_parser * parser,cp_token * pragma_tok,bool * if_p)36031 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36032 {
36033   tree stmt = make_node (OMP_SINGLE);
36034   TREE_TYPE (stmt) = void_type_node;
36035 
36036   OMP_SINGLE_CLAUSES (stmt)
36037     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
36038 				 "#pragma omp single", pragma_tok);
36039   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36040 
36041   return add_stmt (stmt);
36042 }
36043 
36044 /* OpenMP 3.0:
36045    # pragma omp task task-clause[optseq] new-line
36046      structured-block  */
36047 
36048 #define OMP_TASK_CLAUSE_MASK					\
36049 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
36050 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
36051 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
36052 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
36053 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
36054 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
36055 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
36056 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
36057 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
36058 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36059 
36060 static tree
cp_parser_omp_task(cp_parser * parser,cp_token * pragma_tok,bool * if_p)36061 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36062 {
36063   tree clauses, block;
36064   unsigned int save;
36065 
36066   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
36067 				       "#pragma omp task", pragma_tok);
36068   block = begin_omp_task ();
36069   save = cp_parser_begin_omp_structured_block (parser);
36070   cp_parser_statement (parser, NULL_TREE, false, if_p);
36071   cp_parser_end_omp_structured_block (parser, save);
36072   return finish_omp_task (clauses, block);
36073 }
36074 
36075 /* OpenMP 3.0:
36076    # pragma omp taskwait new-line  */
36077 
36078 static void
cp_parser_omp_taskwait(cp_parser * parser,cp_token * pragma_tok)36079 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
36080 {
36081   cp_parser_require_pragma_eol (parser, pragma_tok);
36082   finish_omp_taskwait ();
36083 }
36084 
36085 /* OpenMP 3.1:
36086    # pragma omp taskyield new-line  */
36087 
36088 static void
cp_parser_omp_taskyield(cp_parser * parser,cp_token * pragma_tok)36089 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36090 {
36091   cp_parser_require_pragma_eol (parser, pragma_tok);
36092   finish_omp_taskyield ();
36093 }
36094 
36095 /* OpenMP 4.0:
36096    # pragma omp taskgroup new-line
36097      structured-block  */
36098 
36099 static tree
cp_parser_omp_taskgroup(cp_parser * parser,cp_token * pragma_tok,bool * if_p)36100 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36101 {
36102   cp_parser_require_pragma_eol (parser, pragma_tok);
36103   return c_finish_omp_taskgroup (input_location,
36104 				 cp_parser_omp_structured_block (parser,
36105 								 if_p));
36106 }
36107 
36108 
36109 /* OpenMP 2.5:
36110    # pragma omp threadprivate (variable-list) */
36111 
36112 static void
cp_parser_omp_threadprivate(cp_parser * parser,cp_token * pragma_tok)36113 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36114 {
36115   tree vars;
36116 
36117   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36118   cp_parser_require_pragma_eol (parser, pragma_tok);
36119 
36120   finish_omp_threadprivate (vars);
36121 }
36122 
36123 /* OpenMP 4.0:
36124    # pragma omp cancel cancel-clause[optseq] new-line  */
36125 
36126 #define OMP_CANCEL_CLAUSE_MASK					\
36127 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
36128 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
36129 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
36130 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)	\
36131 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36132 
36133 static void
cp_parser_omp_cancel(cp_parser * parser,cp_token * pragma_tok)36134 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36135 {
36136   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36137 					    "#pragma omp cancel", pragma_tok);
36138   finish_omp_cancel (clauses);
36139 }
36140 
36141 /* OpenMP 4.0:
36142    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
36143 
36144 #define OMP_CANCELLATION_POINT_CLAUSE_MASK			\
36145 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)	\
36146 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)		\
36147 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)	\
36148 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36149 
36150 static void
cp_parser_omp_cancellation_point(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)36151 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36152 				  enum pragma_context context)
36153 {
36154   tree clauses;
36155   bool point_seen = false;
36156 
36157   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36158     {
36159       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36160       const char *p = IDENTIFIER_POINTER (id);
36161 
36162       if (strcmp (p, "point") == 0)
36163 	{
36164 	  cp_lexer_consume_token (parser->lexer);
36165 	  point_seen = true;
36166 	}
36167     }
36168   if (!point_seen)
36169     {
36170       cp_parser_error (parser, "expected %<point%>");
36171       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36172       return;
36173     }
36174 
36175   if (context != pragma_compound)
36176     {
36177       if (context == pragma_stmt)
36178 	error_at (pragma_tok->location,
36179 		  "%<#pragma %s%> may only be used in compound statements",
36180 		  "omp cancellation point");
36181       else
36182 	cp_parser_error (parser, "expected declaration specifiers");
36183       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36184       return;
36185     }
36186 
36187   clauses = cp_parser_omp_all_clauses (parser,
36188 				       OMP_CANCELLATION_POINT_CLAUSE_MASK,
36189 				       "#pragma omp cancellation point",
36190 				       pragma_tok);
36191   finish_omp_cancellation_point (clauses);
36192 }
36193 
36194 /* OpenMP 4.0:
36195    #pragma omp distribute distribute-clause[optseq] new-line
36196      for-loop  */
36197 
36198 #define OMP_DISTRIBUTE_CLAUSE_MASK				\
36199 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
36200 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
36201 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
36202 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36203 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36204 
36205 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)36206 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36207 			  char *p_name, omp_clause_mask mask, tree *cclauses,
36208 			  bool *if_p)
36209 {
36210   tree clauses, sb, ret;
36211   unsigned int save;
36212   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36213 
36214   strcat (p_name, " distribute");
36215   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36216 
36217   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36218     {
36219       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36220       const char *p = IDENTIFIER_POINTER (id);
36221       bool simd = false;
36222       bool parallel = false;
36223 
36224       if (strcmp (p, "simd") == 0)
36225 	simd = true;
36226       else
36227 	parallel = strcmp (p, "parallel") == 0;
36228       if (parallel || simd)
36229 	{
36230 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36231 	  if (cclauses == NULL)
36232 	    cclauses = cclauses_buf;
36233 	  cp_lexer_consume_token (parser->lexer);
36234 	  if (!flag_openmp)  /* flag_openmp_simd  */
36235 	    {
36236 	      if (simd)
36237 		return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36238 					   cclauses, if_p);
36239 	      else
36240 		return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36241 					       cclauses, if_p);
36242 	    }
36243 	  sb = begin_omp_structured_block ();
36244 	  save = cp_parser_begin_omp_structured_block (parser);
36245 	  if (simd)
36246 	    ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36247 				      cclauses, if_p);
36248 	  else
36249 	    ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36250 					  cclauses, if_p);
36251 	  cp_parser_end_omp_structured_block (parser, save);
36252 	  tree body = finish_omp_structured_block (sb);
36253 	  if (ret == NULL)
36254 	    return ret;
36255 	  ret = make_node (OMP_DISTRIBUTE);
36256 	  TREE_TYPE (ret) = void_type_node;
36257 	  OMP_FOR_BODY (ret) = body;
36258 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36259 	  SET_EXPR_LOCATION (ret, loc);
36260 	  add_stmt (ret);
36261 	  return ret;
36262 	}
36263     }
36264   if (!flag_openmp)  /* flag_openmp_simd  */
36265     {
36266       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36267       return NULL_TREE;
36268     }
36269 
36270   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36271 				       cclauses == NULL);
36272   if (cclauses)
36273     {
36274       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36275       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36276     }
36277 
36278   sb = begin_omp_structured_block ();
36279   save = cp_parser_begin_omp_structured_block (parser);
36280 
36281   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36282 
36283   cp_parser_end_omp_structured_block (parser, save);
36284   add_stmt (finish_omp_structured_block (sb));
36285 
36286   return ret;
36287 }
36288 
36289 /* OpenMP 4.0:
36290    # pragma omp teams teams-clause[optseq] new-line
36291      structured-block  */
36292 
36293 #define OMP_TEAMS_CLAUSE_MASK					\
36294 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
36295 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
36296 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
36297 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)	\
36298 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)	\
36299 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT)	\
36300 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36301 
36302 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)36303 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36304 		     char *p_name, omp_clause_mask mask, tree *cclauses,
36305 		     bool *if_p)
36306 {
36307   tree clauses, sb, ret;
36308   unsigned int save;
36309   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36310 
36311   strcat (p_name, " teams");
36312   mask |= OMP_TEAMS_CLAUSE_MASK;
36313 
36314   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36315     {
36316       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36317       const char *p = IDENTIFIER_POINTER (id);
36318       if (strcmp (p, "distribute") == 0)
36319 	{
36320 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36321 	  if (cclauses == NULL)
36322 	    cclauses = cclauses_buf;
36323 
36324 	  cp_lexer_consume_token (parser->lexer);
36325 	  if (!flag_openmp)  /* flag_openmp_simd  */
36326 	    return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36327 					     cclauses, if_p);
36328 	  sb = begin_omp_structured_block ();
36329 	  save = cp_parser_begin_omp_structured_block (parser);
36330 	  ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36331 					  cclauses, if_p);
36332 	  cp_parser_end_omp_structured_block (parser, save);
36333 	  tree body = finish_omp_structured_block (sb);
36334 	  if (ret == NULL)
36335 	    return ret;
36336 	  clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36337 	  ret = make_node (OMP_TEAMS);
36338 	  TREE_TYPE (ret) = void_type_node;
36339 	  OMP_TEAMS_CLAUSES (ret) = clauses;
36340 	  OMP_TEAMS_BODY (ret) = body;
36341 	  OMP_TEAMS_COMBINED (ret) = 1;
36342 	  SET_EXPR_LOCATION (ret, loc);
36343 	  return add_stmt (ret);
36344 	}
36345     }
36346   if (!flag_openmp)  /* flag_openmp_simd  */
36347     {
36348       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36349       return NULL_TREE;
36350     }
36351 
36352   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36353 				       cclauses == NULL);
36354   if (cclauses)
36355     {
36356       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36357       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36358     }
36359 
36360   tree stmt = make_node (OMP_TEAMS);
36361   TREE_TYPE (stmt) = void_type_node;
36362   OMP_TEAMS_CLAUSES (stmt) = clauses;
36363   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36364   SET_EXPR_LOCATION (stmt, loc);
36365 
36366   return add_stmt (stmt);
36367 }
36368 
36369 /* OpenMP 4.0:
36370    # pragma omp target data target-data-clause[optseq] new-line
36371      structured-block  */
36372 
36373 #define OMP_TARGET_DATA_CLAUSE_MASK				\
36374 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
36375 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
36376 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
36377 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36378 
36379 static tree
cp_parser_omp_target_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)36380 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36381 {
36382   tree clauses
36383     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36384 				 "#pragma omp target data", pragma_tok);
36385   int map_seen = 0;
36386   for (tree *pc = &clauses; *pc;)
36387     {
36388       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36389 	switch (OMP_CLAUSE_MAP_KIND (*pc))
36390 	  {
36391 	  case GOMP_MAP_TO:
36392 	  case GOMP_MAP_ALWAYS_TO:
36393 	  case GOMP_MAP_FROM:
36394 	  case GOMP_MAP_ALWAYS_FROM:
36395 	  case GOMP_MAP_TOFROM:
36396 	  case GOMP_MAP_ALWAYS_TOFROM:
36397 	  case GOMP_MAP_ALLOC:
36398 	    map_seen = 3;
36399 	    break;
36400 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
36401 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36402 	  case GOMP_MAP_ALWAYS_POINTER:
36403 	    break;
36404 	  default:
36405 	    map_seen |= 1;
36406 	    error_at (OMP_CLAUSE_LOCATION (*pc),
36407 		      "%<#pragma omp target data%> with map-type other "
36408 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36409 		      "on %<map%> clause");
36410 	    *pc = OMP_CLAUSE_CHAIN (*pc);
36411 	    continue;
36412 	  }
36413       pc = &OMP_CLAUSE_CHAIN (*pc);
36414     }
36415 
36416   if (map_seen != 3)
36417     {
36418       if (map_seen == 0)
36419 	error_at (pragma_tok->location,
36420 		  "%<#pragma omp target data%> must contain at least "
36421 		  "one %<map%> clause");
36422       return NULL_TREE;
36423     }
36424 
36425   tree stmt = make_node (OMP_TARGET_DATA);
36426   TREE_TYPE (stmt) = void_type_node;
36427   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36428 
36429   keep_next_level (true);
36430   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36431 
36432   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36433   return add_stmt (stmt);
36434 }
36435 
36436 /* OpenMP 4.5:
36437    # pragma omp target enter data target-enter-data-clause[optseq] new-line
36438      structured-block  */
36439 
36440 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK			\
36441 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
36442 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
36443 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
36444 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
36445 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36446 
36447 static tree
cp_parser_omp_target_enter_data(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)36448 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36449 				 enum pragma_context context)
36450 {
36451   bool data_seen = false;
36452   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36453     {
36454       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36455       const char *p = IDENTIFIER_POINTER (id);
36456 
36457       if (strcmp (p, "data") == 0)
36458 	{
36459 	  cp_lexer_consume_token (parser->lexer);
36460 	  data_seen = true;
36461 	}
36462     }
36463   if (!data_seen)
36464     {
36465       cp_parser_error (parser, "expected %<data%>");
36466       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36467       return NULL_TREE;
36468     }
36469 
36470   if (context == pragma_stmt)
36471     {
36472       error_at (pragma_tok->location,
36473 		"%<#pragma %s%> may only be used in compound statements",
36474 		"omp target enter data");
36475       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36476       return NULL_TREE;
36477     }
36478 
36479   tree clauses
36480     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36481 				 "#pragma omp target enter data", pragma_tok);
36482   int map_seen = 0;
36483   for (tree *pc = &clauses; *pc;)
36484     {
36485       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36486 	switch (OMP_CLAUSE_MAP_KIND (*pc))
36487 	  {
36488 	  case GOMP_MAP_TO:
36489 	  case GOMP_MAP_ALWAYS_TO:
36490 	  case GOMP_MAP_ALLOC:
36491 	    map_seen = 3;
36492 	    break;
36493 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
36494 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36495 	  case GOMP_MAP_ALWAYS_POINTER:
36496 	    break;
36497 	  default:
36498 	    map_seen |= 1;
36499 	    error_at (OMP_CLAUSE_LOCATION (*pc),
36500 		      "%<#pragma omp target enter data%> with map-type other "
36501 		      "than %<to%> or %<alloc%> on %<map%> clause");
36502 	    *pc = OMP_CLAUSE_CHAIN (*pc);
36503 	    continue;
36504 	  }
36505       pc = &OMP_CLAUSE_CHAIN (*pc);
36506     }
36507 
36508   if (map_seen != 3)
36509     {
36510       if (map_seen == 0)
36511 	error_at (pragma_tok->location,
36512 		  "%<#pragma omp target enter data%> must contain at least "
36513 		  "one %<map%> clause");
36514       return NULL_TREE;
36515     }
36516 
36517   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36518   TREE_TYPE (stmt) = void_type_node;
36519   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36520   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36521   return add_stmt (stmt);
36522 }
36523 
36524 /* OpenMP 4.5:
36525    # pragma omp target exit data target-enter-data-clause[optseq] new-line
36526      structured-block  */
36527 
36528 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK			\
36529 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
36530 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
36531 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
36532 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
36533 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36534 
36535 static tree
cp_parser_omp_target_exit_data(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)36536 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36537 				enum pragma_context context)
36538 {
36539   bool data_seen = false;
36540   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36541     {
36542       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36543       const char *p = IDENTIFIER_POINTER (id);
36544 
36545       if (strcmp (p, "data") == 0)
36546 	{
36547 	  cp_lexer_consume_token (parser->lexer);
36548 	  data_seen = true;
36549 	}
36550     }
36551   if (!data_seen)
36552     {
36553       cp_parser_error (parser, "expected %<data%>");
36554       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36555       return NULL_TREE;
36556     }
36557 
36558   if (context == pragma_stmt)
36559     {
36560       error_at (pragma_tok->location,
36561 		"%<#pragma %s%> may only be used in compound statements",
36562 		"omp target exit data");
36563       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36564       return NULL_TREE;
36565     }
36566 
36567   tree clauses
36568     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36569 				 "#pragma omp target exit data", pragma_tok);
36570   int map_seen = 0;
36571   for (tree *pc = &clauses; *pc;)
36572     {
36573       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36574 	switch (OMP_CLAUSE_MAP_KIND (*pc))
36575 	  {
36576 	  case GOMP_MAP_FROM:
36577 	  case GOMP_MAP_ALWAYS_FROM:
36578 	  case GOMP_MAP_RELEASE:
36579 	  case GOMP_MAP_DELETE:
36580 	    map_seen = 3;
36581 	    break;
36582 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
36583 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36584 	  case GOMP_MAP_ALWAYS_POINTER:
36585 	    break;
36586 	  default:
36587 	    map_seen |= 1;
36588 	    error_at (OMP_CLAUSE_LOCATION (*pc),
36589 		      "%<#pragma omp target exit data%> with map-type other "
36590 		      "than %<from%>, %<release%> or %<delete%> on %<map%>"
36591 		      " clause");
36592 	    *pc = OMP_CLAUSE_CHAIN (*pc);
36593 	    continue;
36594 	  }
36595       pc = &OMP_CLAUSE_CHAIN (*pc);
36596     }
36597 
36598   if (map_seen != 3)
36599     {
36600       if (map_seen == 0)
36601 	error_at (pragma_tok->location,
36602 		  "%<#pragma omp target exit data%> must contain at least "
36603 		  "one %<map%> clause");
36604       return NULL_TREE;
36605     }
36606 
36607   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36608   TREE_TYPE (stmt) = void_type_node;
36609   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36610   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36611   return add_stmt (stmt);
36612 }
36613 
36614 /* OpenMP 4.0:
36615    # pragma omp target update target-update-clause[optseq] new-line */
36616 
36617 #define OMP_TARGET_UPDATE_CLAUSE_MASK				\
36618 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)		\
36619 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
36620 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
36621 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
36622 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
36623 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36624 
36625 static bool
cp_parser_omp_target_update(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)36626 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36627 			     enum pragma_context context)
36628 {
36629   if (context == pragma_stmt)
36630     {
36631       error_at (pragma_tok->location,
36632 		"%<#pragma %s%> may only be used in compound statements",
36633 		"omp target update");
36634       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36635       return false;
36636     }
36637 
36638   tree clauses
36639     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36640 				 "#pragma omp target update", pragma_tok);
36641   if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36642       && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36643     {
36644       error_at (pragma_tok->location,
36645 		"%<#pragma omp target update%> must contain at least one "
36646 		"%<from%> or %<to%> clauses");
36647       return false;
36648     }
36649 
36650   tree stmt = make_node (OMP_TARGET_UPDATE);
36651   TREE_TYPE (stmt) = void_type_node;
36652   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36653   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36654   add_stmt (stmt);
36655   return false;
36656 }
36657 
36658 /* OpenMP 4.0:
36659    # pragma omp target target-clause[optseq] new-line
36660      structured-block  */
36661 
36662 #define OMP_TARGET_CLAUSE_MASK					\
36663 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)	\
36664 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)		\
36665 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
36666 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)	\
36667 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)	\
36668 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
36669 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
36670 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)	\
36671 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36672 
36673 static bool
cp_parser_omp_target(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context,bool * if_p)36674 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36675 		      enum pragma_context context, bool *if_p)
36676 {
36677   tree *pc = NULL, stmt;
36678 
36679   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36680     {
36681       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36682       const char *p = IDENTIFIER_POINTER (id);
36683       enum tree_code ccode = ERROR_MARK;
36684 
36685       if (strcmp (p, "teams") == 0)
36686 	ccode = OMP_TEAMS;
36687       else if (strcmp (p, "parallel") == 0)
36688 	ccode = OMP_PARALLEL;
36689       else if (strcmp (p, "simd") == 0)
36690 	ccode = OMP_SIMD;
36691       if (ccode != ERROR_MARK)
36692 	{
36693 	  tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36694 	  char p_name[sizeof ("#pragma omp target teams distribute "
36695 			      "parallel for simd")];
36696 
36697 	  cp_lexer_consume_token (parser->lexer);
36698 	  strcpy (p_name, "#pragma omp target");
36699 	  if (!flag_openmp)  /* flag_openmp_simd  */
36700 	    {
36701 	      tree stmt;
36702 	      switch (ccode)
36703 		{
36704 		case OMP_TEAMS:
36705 		  stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36706 					      OMP_TARGET_CLAUSE_MASK,
36707 					      cclauses, if_p);
36708 		  break;
36709 		case OMP_PARALLEL:
36710 		  stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36711 						 OMP_TARGET_CLAUSE_MASK,
36712 						 cclauses, if_p);
36713 		  break;
36714 		case OMP_SIMD:
36715 		  stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36716 					     OMP_TARGET_CLAUSE_MASK,
36717 					     cclauses, if_p);
36718 		  break;
36719 		default:
36720 		  gcc_unreachable ();
36721 		}
36722 	      return stmt != NULL_TREE;
36723 	    }
36724 	  keep_next_level (true);
36725 	  tree sb = begin_omp_structured_block (), ret;
36726 	  unsigned save = cp_parser_begin_omp_structured_block (parser);
36727 	  switch (ccode)
36728 	    {
36729 	    case OMP_TEAMS:
36730 	      ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36731 					 OMP_TARGET_CLAUSE_MASK, cclauses,
36732 					 if_p);
36733 	      break;
36734 	    case OMP_PARALLEL:
36735 	      ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36736 					    OMP_TARGET_CLAUSE_MASK, cclauses,
36737 					    if_p);
36738 	      break;
36739 	    case OMP_SIMD:
36740 	      ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36741 					OMP_TARGET_CLAUSE_MASK, cclauses,
36742 					if_p);
36743 	      break;
36744 	    default:
36745 	      gcc_unreachable ();
36746 	    }
36747 	  cp_parser_end_omp_structured_block (parser, save);
36748 	  tree body = finish_omp_structured_block (sb);
36749 	  if (ret == NULL_TREE)
36750 	    return false;
36751 	  if (ccode == OMP_TEAMS && !processing_template_decl)
36752 	    {
36753 	      /* For combined target teams, ensure the num_teams and
36754 		 thread_limit clause expressions are evaluated on the host,
36755 		 before entering the target construct.  */
36756 	      tree c;
36757 	      for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36758 		   c; c = OMP_CLAUSE_CHAIN (c))
36759 		if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36760 		     || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36761 		    && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36762 		  {
36763 		    tree expr = OMP_CLAUSE_OPERAND (c, 0);
36764 		    expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36765 		    if (expr == error_mark_node)
36766 		      continue;
36767 		    tree tmp = TARGET_EXPR_SLOT (expr);
36768 		    add_stmt (expr);
36769 		    OMP_CLAUSE_OPERAND (c, 0) = expr;
36770 		    tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36771 						OMP_CLAUSE_FIRSTPRIVATE);
36772 		    OMP_CLAUSE_DECL (tc) = tmp;
36773 		    OMP_CLAUSE_CHAIN (tc)
36774 		      = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36775 		    cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36776 		  }
36777 	    }
36778 	  tree stmt = make_node (OMP_TARGET);
36779 	  TREE_TYPE (stmt) = void_type_node;
36780 	  OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36781 	  OMP_TARGET_BODY (stmt) = body;
36782 	  OMP_TARGET_COMBINED (stmt) = 1;
36783 	  SET_EXPR_LOCATION (stmt, pragma_tok->location);
36784 	  add_stmt (stmt);
36785 	  pc = &OMP_TARGET_CLAUSES (stmt);
36786 	  goto check_clauses;
36787 	}
36788       else if (!flag_openmp)  /* flag_openmp_simd  */
36789 	{
36790 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36791 	  return false;
36792 	}
36793       else if (strcmp (p, "data") == 0)
36794 	{
36795 	  cp_lexer_consume_token (parser->lexer);
36796 	  cp_parser_omp_target_data (parser, pragma_tok, if_p);
36797 	  return true;
36798 	}
36799       else if (strcmp (p, "enter") == 0)
36800 	{
36801 	  cp_lexer_consume_token (parser->lexer);
36802 	  cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36803 	  return false;
36804 	}
36805       else if (strcmp (p, "exit") == 0)
36806 	{
36807 	  cp_lexer_consume_token (parser->lexer);
36808 	  cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36809 	  return false;
36810 	}
36811       else if (strcmp (p, "update") == 0)
36812 	{
36813 	  cp_lexer_consume_token (parser->lexer);
36814 	  return cp_parser_omp_target_update (parser, pragma_tok, context);
36815 	}
36816     }
36817   if (!flag_openmp)  /* flag_openmp_simd  */
36818     {
36819       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36820       return false;
36821     }
36822 
36823   stmt = make_node (OMP_TARGET);
36824   TREE_TYPE (stmt) = void_type_node;
36825 
36826   OMP_TARGET_CLAUSES (stmt)
36827     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36828 				 "#pragma omp target", pragma_tok);
36829   pc = &OMP_TARGET_CLAUSES (stmt);
36830   keep_next_level (true);
36831   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36832 
36833   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36834   add_stmt (stmt);
36835 
36836 check_clauses:
36837   while (*pc)
36838     {
36839       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36840 	switch (OMP_CLAUSE_MAP_KIND (*pc))
36841 	  {
36842 	  case GOMP_MAP_TO:
36843 	  case GOMP_MAP_ALWAYS_TO:
36844 	  case GOMP_MAP_FROM:
36845 	  case GOMP_MAP_ALWAYS_FROM:
36846 	  case GOMP_MAP_TOFROM:
36847 	  case GOMP_MAP_ALWAYS_TOFROM:
36848 	  case GOMP_MAP_ALLOC:
36849 	  case GOMP_MAP_FIRSTPRIVATE_POINTER:
36850 	  case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36851 	  case GOMP_MAP_ALWAYS_POINTER:
36852 	    break;
36853 	  default:
36854 	    error_at (OMP_CLAUSE_LOCATION (*pc),
36855 		      "%<#pragma omp target%> with map-type other "
36856 		      "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36857 		      "on %<map%> clause");
36858 	    *pc = OMP_CLAUSE_CHAIN (*pc);
36859 	    continue;
36860 	  }
36861       pc = &OMP_CLAUSE_CHAIN (*pc);
36862     }
36863   return true;
36864 }
36865 
36866 /* OpenACC 2.0:
36867    # pragma acc cache (variable-list) new-line
36868 */
36869 
36870 static tree
cp_parser_oacc_cache(cp_parser * parser,cp_token * pragma_tok)36871 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36872 {
36873   tree stmt, clauses;
36874 
36875   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36876   clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36877 
36878   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36879 
36880   stmt = make_node (OACC_CACHE);
36881   TREE_TYPE (stmt) = void_type_node;
36882   OACC_CACHE_CLAUSES (stmt) = clauses;
36883   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36884   add_stmt (stmt);
36885 
36886   return stmt;
36887 }
36888 
36889 /* OpenACC 2.0:
36890    # pragma acc data oacc-data-clause[optseq] new-line
36891      structured-block  */
36892 
36893 #define OACC_DATA_CLAUSE_MASK						\
36894 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
36895 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
36896 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
36897 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
36898 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
36899 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
36900 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
36901 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
36902 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
36903 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
36904 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36905 
36906 static tree
cp_parser_oacc_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)36907 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36908 {
36909   tree stmt, clauses, block;
36910   unsigned int save;
36911 
36912   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36913 					"#pragma acc data", pragma_tok);
36914 
36915   block = begin_omp_parallel ();
36916   save = cp_parser_begin_omp_structured_block (parser);
36917   cp_parser_statement (parser, NULL_TREE, false, if_p);
36918   cp_parser_end_omp_structured_block (parser, save);
36919   stmt = finish_oacc_data (clauses, block);
36920   return stmt;
36921 }
36922 
36923 /* OpenACC 2.0:
36924   # pragma acc host_data <clauses> new-line
36925   structured-block  */
36926 
36927 #define OACC_HOST_DATA_CLAUSE_MASK					\
36928   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36929 
36930 static tree
cp_parser_oacc_host_data(cp_parser * parser,cp_token * pragma_tok,bool * if_p)36931 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36932 {
36933   tree stmt, clauses, block;
36934   unsigned int save;
36935 
36936   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36937 					"#pragma acc host_data", pragma_tok);
36938 
36939   block = begin_omp_parallel ();
36940   save = cp_parser_begin_omp_structured_block (parser);
36941   cp_parser_statement (parser, NULL_TREE, false, if_p);
36942   cp_parser_end_omp_structured_block (parser, save);
36943   stmt = finish_oacc_host_data (clauses, block);
36944   return stmt;
36945 }
36946 
36947 /* OpenACC 2.0:
36948    # pragma acc declare oacc-data-clause[optseq] new-line
36949 */
36950 
36951 #define OACC_DECLARE_CLAUSE_MASK					\
36952 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
36953 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
36954 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
36955 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
36956 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
36957 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)	\
36958 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)		\
36959 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
36960 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
36961 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
36962 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
36963 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36964 
36965 static tree
cp_parser_oacc_declare(cp_parser * parser,cp_token * pragma_tok)36966 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36967 {
36968   tree clauses, stmt;
36969   bool error = false;
36970 
36971   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36972 					"#pragma acc declare", pragma_tok, true);
36973 
36974 
36975   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36976     {
36977       error_at (pragma_tok->location,
36978 		"no valid clauses specified in %<#pragma acc declare%>");
36979       return NULL_TREE;
36980     }
36981 
36982   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36983     {
36984       location_t loc = OMP_CLAUSE_LOCATION (t);
36985       tree decl = OMP_CLAUSE_DECL (t);
36986       if (!DECL_P (decl))
36987 	{
36988 	  error_at (loc, "array section in %<#pragma acc declare%>");
36989 	  error = true;
36990 	  continue;
36991 	}
36992       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36993       switch (OMP_CLAUSE_MAP_KIND (t))
36994 	{
36995 	case GOMP_MAP_FIRSTPRIVATE_POINTER:
36996 	case GOMP_MAP_FORCE_ALLOC:
36997 	case GOMP_MAP_FORCE_TO:
36998 	case GOMP_MAP_FORCE_DEVICEPTR:
36999 	case GOMP_MAP_DEVICE_RESIDENT:
37000 	  break;
37001 
37002 	case GOMP_MAP_LINK:
37003 	  if (!global_bindings_p ()
37004 	      && (TREE_STATIC (decl)
37005 	       || !DECL_EXTERNAL (decl)))
37006 	    {
37007 	      error_at (loc,
37008 			"%qD must be a global variable in "
37009 			"%<#pragma acc declare link%>",
37010 			decl);
37011 	      error = true;
37012 	      continue;
37013 	    }
37014 	  break;
37015 
37016 	default:
37017 	  if (global_bindings_p ())
37018 	    {
37019 	      error_at (loc, "invalid OpenACC clause at file scope");
37020 	      error = true;
37021 	      continue;
37022 	    }
37023 	  if (DECL_EXTERNAL (decl))
37024 	    {
37025 	      error_at (loc,
37026 			"invalid use of %<extern%> variable %qD "
37027 			"in %<#pragma acc declare%>", decl);
37028 	      error = true;
37029 	      continue;
37030 	    }
37031 	  else if (TREE_PUBLIC (decl))
37032 	    {
37033 	      error_at (loc,
37034 			"invalid use of %<global%> variable %qD "
37035 			"in %<#pragma acc declare%>", decl);
37036 	      error = true;
37037 	      continue;
37038 	    }
37039 	  break;
37040 	}
37041 
37042       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
37043 	  || lookup_attribute ("omp declare target link",
37044 			       DECL_ATTRIBUTES (decl)))
37045 	{
37046 	  error_at (loc, "variable %qD used more than once with "
37047 		    "%<#pragma acc declare%>", decl);
37048 	  error = true;
37049 	  continue;
37050 	}
37051 
37052       if (!error)
37053 	{
37054 	  tree id;
37055 
37056 	  if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
37057 	    id = get_identifier ("omp declare target link");
37058 	  else
37059 	    id = get_identifier ("omp declare target");
37060 
37061 	  DECL_ATTRIBUTES (decl)
37062 	    = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
37063 	  if (global_bindings_p ())
37064 	    {
37065 	      symtab_node *node = symtab_node::get (decl);
37066 	      if (node != NULL)
37067 		{
37068 		  node->offloadable = 1;
37069 		  if (ENABLE_OFFLOADING)
37070 		    {
37071 		      g->have_offload = true;
37072 		      if (is_a <varpool_node *> (node))
37073 			vec_safe_push (offload_vars, decl);
37074 		    }
37075 		}
37076 	    }
37077 	}
37078     }
37079 
37080   if (error || global_bindings_p ())
37081     return NULL_TREE;
37082 
37083   stmt = make_node (OACC_DECLARE);
37084   TREE_TYPE (stmt) = void_type_node;
37085   OACC_DECLARE_CLAUSES (stmt) = clauses;
37086   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37087 
37088   add_stmt (stmt);
37089 
37090   return NULL_TREE;
37091 }
37092 
37093 /* OpenACC 2.0:
37094    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37095 
37096    or
37097 
37098    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37099 
37100    LOC is the location of the #pragma token.
37101 */
37102 
37103 #define OACC_ENTER_DATA_CLAUSE_MASK					\
37104 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
37105 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
37106 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
37107 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
37108 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
37109 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)	\
37110 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37111 
37112 #define OACC_EXIT_DATA_CLAUSE_MASK					\
37113 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
37114 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
37115 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
37116 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) 		\
37117 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37118 
37119 static tree
cp_parser_oacc_enter_exit_data(cp_parser * parser,cp_token * pragma_tok,bool enter)37120 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37121 				bool enter)
37122 {
37123   location_t loc = pragma_tok->location;
37124   tree stmt, clauses;
37125   const char *p = "";
37126 
37127   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37128     p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37129 
37130   if (strcmp (p, "data") != 0)
37131     {
37132       error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37133 		enter ? "enter" : "exit");
37134       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37135       return NULL_TREE;
37136     }
37137 
37138   cp_lexer_consume_token (parser->lexer);
37139 
37140   if (enter)
37141     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37142 					 "#pragma acc enter data", pragma_tok);
37143   else
37144     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37145 					 "#pragma acc exit data", pragma_tok);
37146 
37147   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37148     {
37149       error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37150 		enter ? "enter" : "exit");
37151       return NULL_TREE;
37152     }
37153 
37154   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37155   TREE_TYPE (stmt) = void_type_node;
37156   OMP_STANDALONE_CLAUSES (stmt) = clauses;
37157   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37158   add_stmt (stmt);
37159   return stmt;
37160 }
37161 
37162 /* OpenACC 2.0:
37163    # pragma acc loop oacc-loop-clause[optseq] new-line
37164      structured-block  */
37165 
37166 #define OACC_LOOP_CLAUSE_MASK						\
37167 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)		\
37168 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
37169 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
37170 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
37171 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
37172 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
37173 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)		\
37174 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)		\
37175 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)			\
37176 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37177 
37178 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)37179 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37180 		     omp_clause_mask mask, tree *cclauses, bool *if_p)
37181 {
37182   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37183 
37184   strcat (p_name, " loop");
37185   mask |= OACC_LOOP_CLAUSE_MASK;
37186 
37187   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37188 					     cclauses == NULL);
37189   if (cclauses)
37190     {
37191       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37192       if (*cclauses)
37193 	*cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37194       if (clauses)
37195 	clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37196     }
37197 
37198   tree block = begin_omp_structured_block ();
37199   int save = cp_parser_begin_omp_structured_block (parser);
37200   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37201   cp_parser_end_omp_structured_block (parser, save);
37202   add_stmt (finish_omp_structured_block (block));
37203 
37204   return stmt;
37205 }
37206 
37207 /* OpenACC 2.0:
37208    # pragma acc kernels oacc-kernels-clause[optseq] new-line
37209      structured-block
37210 
37211    or
37212 
37213    # pragma acc parallel oacc-parallel-clause[optseq] new-line
37214      structured-block
37215 */
37216 
37217 #define OACC_KERNELS_CLAUSE_MASK					\
37218 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
37219 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
37220 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
37221 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
37222 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
37223 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
37224 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
37225 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
37226 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
37227 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
37228 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
37229 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
37230 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
37231 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
37232 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)	\
37233 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)	\
37234 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37235 
37236 #define OACC_PARALLEL_CLAUSE_MASK					\
37237 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
37238 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)		\
37239 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)		\
37240 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)		\
37241 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)		\
37242 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)		\
37243 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)		\
37244 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)       	\
37245 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
37246 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)		\
37247 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)		\
37248 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)		\
37249 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)	\
37250 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)	\
37251 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)	\
37252 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
37253 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)		\
37254 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)		\
37255 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
37256 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37257 
37258 static tree
cp_parser_oacc_kernels_parallel(cp_parser * parser,cp_token * pragma_tok,char * p_name,bool * if_p)37259 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37260 				 char *p_name, bool *if_p)
37261 {
37262   omp_clause_mask mask;
37263   enum tree_code code;
37264   switch (cp_parser_pragma_kind (pragma_tok))
37265     {
37266     case PRAGMA_OACC_KERNELS:
37267       strcat (p_name, " kernels");
37268       mask = OACC_KERNELS_CLAUSE_MASK;
37269       code = OACC_KERNELS;
37270       break;
37271     case PRAGMA_OACC_PARALLEL:
37272       strcat (p_name, " parallel");
37273       mask = OACC_PARALLEL_CLAUSE_MASK;
37274       code = OACC_PARALLEL;
37275       break;
37276     default:
37277       gcc_unreachable ();
37278     }
37279 
37280   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37281     {
37282       const char *p
37283 	= IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37284       if (strcmp (p, "loop") == 0)
37285 	{
37286 	  cp_lexer_consume_token (parser->lexer);
37287 	  tree block = begin_omp_parallel ();
37288 	  tree clauses;
37289 	  cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37290 			       if_p);
37291 	  return finish_omp_construct (code, block, clauses);
37292 	}
37293     }
37294 
37295   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37296 
37297   tree block = begin_omp_parallel ();
37298   unsigned int save = cp_parser_begin_omp_structured_block (parser);
37299   cp_parser_statement (parser, NULL_TREE, false, if_p);
37300   cp_parser_end_omp_structured_block (parser, save);
37301   return finish_omp_construct (code, block, clauses);
37302 }
37303 
37304 /* OpenACC 2.0:
37305    # pragma acc update oacc-update-clause[optseq] new-line
37306 */
37307 
37308 #define OACC_UPDATE_CLAUSE_MASK						\
37309 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)		\
37310 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)		\
37311 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)		\
37312 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)			\
37313 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)		\
37314 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37315 
37316 static tree
cp_parser_oacc_update(cp_parser * parser,cp_token * pragma_tok)37317 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37318 {
37319   tree stmt, clauses;
37320 
37321   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37322 					 "#pragma acc update", pragma_tok);
37323 
37324   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37325     {
37326       error_at (pragma_tok->location,
37327 		"%<#pragma acc update%> must contain at least one "
37328 		"%<device%> or %<host%> or %<self%> clause");
37329       return NULL_TREE;
37330     }
37331 
37332   stmt = make_node (OACC_UPDATE);
37333   TREE_TYPE (stmt) = void_type_node;
37334   OACC_UPDATE_CLAUSES (stmt) = clauses;
37335   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37336   add_stmt (stmt);
37337   return stmt;
37338 }
37339 
37340 /* OpenACC 2.0:
37341    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37342 
37343    LOC is the location of the #pragma token.
37344 */
37345 
37346 #define OACC_WAIT_CLAUSE_MASK					\
37347 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37348 
37349 static tree
cp_parser_oacc_wait(cp_parser * parser,cp_token * pragma_tok)37350 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37351 {
37352   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37353   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37354 
37355   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37356     list = cp_parser_oacc_wait_list (parser, loc, list);
37357 
37358   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37359 					"#pragma acc wait", pragma_tok);
37360 
37361   stmt = c_finish_oacc_wait (loc, list, clauses);
37362   stmt = finish_expr_stmt (stmt);
37363 
37364   return stmt;
37365 }
37366 
37367 /* OpenMP 4.0:
37368    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
37369 
37370 #define OMP_DECLARE_SIMD_CLAUSE_MASK				\
37371 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)	\
37372 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)	\
37373 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)	\
37374 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)	\
37375 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)	\
37376 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37377 
37378 static void
cp_parser_omp_declare_simd(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)37379 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37380 			    enum pragma_context context)
37381 {
37382   bool first_p = parser->omp_declare_simd == NULL;
37383   cp_omp_declare_simd_data data;
37384   if (first_p)
37385     {
37386       data.error_seen = false;
37387       data.fndecl_seen = false;
37388       data.tokens = vNULL;
37389       data.clauses = NULL_TREE;
37390       /* It is safe to take the address of a local variable; it will only be
37391 	 used while this scope is live.  */
37392       parser->omp_declare_simd = &data;
37393     }
37394 
37395   /* Store away all pragma tokens.  */
37396   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37397 	 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37398     cp_lexer_consume_token (parser->lexer);
37399   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37400     parser->omp_declare_simd->error_seen = true;
37401   cp_parser_require_pragma_eol (parser, pragma_tok);
37402   struct cp_token_cache *cp
37403     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37404   parser->omp_declare_simd->tokens.safe_push (cp);
37405 
37406   if (first_p)
37407     {
37408       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37409 	cp_parser_pragma (parser, context, NULL);
37410       switch (context)
37411 	{
37412 	case pragma_external:
37413 	  cp_parser_declaration (parser);
37414 	  break;
37415 	case pragma_member:
37416 	  cp_parser_member_declaration (parser);
37417 	  break;
37418 	case pragma_objc_icode:
37419 	  cp_parser_block_declaration (parser, /*statement_p=*/false);
37420 	  break;
37421 	default:
37422 	  cp_parser_declaration_statement (parser);
37423 	  break;
37424 	}
37425       if (parser->omp_declare_simd
37426 	  && !parser->omp_declare_simd->error_seen
37427 	  && !parser->omp_declare_simd->fndecl_seen)
37428 	error_at (pragma_tok->location,
37429 		  "%<#pragma omp declare simd%> not immediately followed by "
37430 		  "function declaration or definition");
37431       data.tokens.release ();
37432       parser->omp_declare_simd = NULL;
37433     }
37434 }
37435 
37436 /* Finalize #pragma omp declare simd clauses after direct declarator has
37437    been parsed, and put that into "omp declare simd" attribute.  */
37438 
37439 static tree
cp_parser_late_parsing_omp_declare_simd(cp_parser * parser,tree attrs)37440 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37441 {
37442   struct cp_token_cache *ce;
37443   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37444   int i;
37445 
37446   if (!data->error_seen && data->fndecl_seen)
37447     {
37448       error ("%<#pragma omp declare simd%> not immediately followed by "
37449 	     "a single function declaration or definition");
37450       data->error_seen = true;
37451     }
37452   if (data->error_seen)
37453     return attrs;
37454 
37455   FOR_EACH_VEC_ELT (data->tokens, i, ce)
37456     {
37457       tree c, cl;
37458 
37459       cp_parser_push_lexer_for_tokens (parser, ce);
37460       parser->lexer->in_pragma = true;
37461       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37462       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37463       cp_lexer_consume_token (parser->lexer);
37464       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37465 				      "#pragma omp declare simd", pragma_tok);
37466       cp_parser_pop_lexer (parser);
37467       if (cl)
37468 	cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37469       c = build_tree_list (get_identifier ("omp declare simd"), cl);
37470       TREE_CHAIN (c) = attrs;
37471       if (processing_template_decl)
37472 	ATTR_IS_DEPENDENT (c) = 1;
37473       attrs = c;
37474     }
37475 
37476   data->fndecl_seen = true;
37477   return attrs;
37478 }
37479 
37480 
37481 /* OpenMP 4.0:
37482    # pragma omp declare target new-line
37483    declarations and definitions
37484    # pragma omp end declare target new-line
37485 
37486    OpenMP 4.5:
37487    # pragma omp declare target ( extended-list ) new-line
37488 
37489    # pragma omp declare target declare-target-clauses[seq] new-line  */
37490 
37491 #define OMP_DECLARE_TARGET_CLAUSE_MASK				\
37492 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)		\
37493 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37494 
37495 static void
cp_parser_omp_declare_target(cp_parser * parser,cp_token * pragma_tok)37496 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37497 {
37498   tree clauses = NULL_TREE;
37499   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37500     clauses
37501       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37502 				   "#pragma omp declare target", pragma_tok);
37503   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37504     {
37505       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37506 					clauses);
37507       clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37508       cp_parser_require_pragma_eol (parser, pragma_tok);
37509     }
37510   else
37511     {
37512       cp_parser_require_pragma_eol (parser, pragma_tok);
37513       scope_chain->omp_declare_target_attribute++;
37514       return;
37515     }
37516   if (scope_chain->omp_declare_target_attribute)
37517     error_at (pragma_tok->location,
37518 	      "%<#pragma omp declare target%> with clauses in between "
37519 	      "%<#pragma omp declare target%> without clauses and "
37520 	      "%<#pragma omp end declare target%>");
37521   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37522     {
37523       tree t = OMP_CLAUSE_DECL (c), id;
37524       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37525       tree at2 = lookup_attribute ("omp declare target link",
37526 				   DECL_ATTRIBUTES (t));
37527       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37528 	{
37529 	  id = get_identifier ("omp declare target link");
37530 	  std::swap (at1, at2);
37531 	}
37532       else
37533 	id = get_identifier ("omp declare target");
37534       if (at2)
37535 	{
37536 	  error_at (OMP_CLAUSE_LOCATION (c),
37537 		    "%qD specified both in declare target %<link%> and %<to%>"
37538 		    " clauses", t);
37539 	  continue;
37540 	}
37541       if (!at1)
37542 	{
37543 	  DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37544 	  if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37545 	    continue;
37546 
37547 	  symtab_node *node = symtab_node::get (t);
37548 	  if (node != NULL)
37549 	    {
37550 	      node->offloadable = 1;
37551 	      if (ENABLE_OFFLOADING)
37552 		{
37553 		  g->have_offload = true;
37554 		  if (is_a <varpool_node *> (node))
37555 		    vec_safe_push (offload_vars, t);
37556 		}
37557 	    }
37558 	}
37559     }
37560 }
37561 
37562 static void
cp_parser_omp_end_declare_target(cp_parser * parser,cp_token * pragma_tok)37563 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37564 {
37565   const char *p = "";
37566   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37567     {
37568       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37569       p = IDENTIFIER_POINTER (id);
37570     }
37571   if (strcmp (p, "declare") == 0)
37572     {
37573       cp_lexer_consume_token (parser->lexer);
37574       p = "";
37575       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37576 	{
37577 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37578 	  p = IDENTIFIER_POINTER (id);
37579 	}
37580       if (strcmp (p, "target") == 0)
37581 	cp_lexer_consume_token (parser->lexer);
37582       else
37583 	{
37584 	  cp_parser_error (parser, "expected %<target%>");
37585 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37586 	  return;
37587 	}
37588     }
37589   else
37590     {
37591       cp_parser_error (parser, "expected %<declare%>");
37592       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37593       return;
37594     }
37595   cp_parser_require_pragma_eol (parser, pragma_tok);
37596   if (!scope_chain->omp_declare_target_attribute)
37597     error_at (pragma_tok->location,
37598 	      "%<#pragma omp end declare target%> without corresponding "
37599 	      "%<#pragma omp declare target%>");
37600   else
37601     scope_chain->omp_declare_target_attribute--;
37602 }
37603 
37604 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
37605    expression and optional initializer clause of
37606    #pragma omp declare reduction.  We store the expression(s) as
37607    either 3, 6 or 7 special statements inside of the artificial function's
37608    body.  The first two statements are DECL_EXPRs for the artificial
37609    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37610    expression that uses those variables.
37611    If there was any INITIALIZER clause, this is followed by further statements,
37612    the fourth and fifth statements are DECL_EXPRs for the artificial
37613    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
37614    constructor variant (first token after open paren is not omp_priv),
37615    then the sixth statement is a statement with the function call expression
37616    that uses the OMP_PRIV and optionally OMP_ORIG variable.
37617    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37618    to initialize the OMP_PRIV artificial variable and there is seventh
37619    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
37620 
37621 static bool
cp_parser_omp_declare_reduction_exprs(tree fndecl,cp_parser * parser)37622 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37623 {
37624   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37625   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37626   type = TREE_TYPE (type);
37627   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37628   DECL_ARTIFICIAL (omp_out) = 1;
37629   pushdecl (omp_out);
37630   add_decl_expr (omp_out);
37631   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37632   DECL_ARTIFICIAL (omp_in) = 1;
37633   pushdecl (omp_in);
37634   add_decl_expr (omp_in);
37635   tree combiner;
37636   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37637 
37638   keep_next_level (true);
37639   tree block = begin_omp_structured_block ();
37640   combiner = cp_parser_expression (parser);
37641   finish_expr_stmt (combiner);
37642   block = finish_omp_structured_block (block);
37643   if (processing_template_decl)
37644     block = build_stmt (input_location, EXPR_STMT, block);
37645   add_stmt (block);
37646 
37647   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37648     return false;
37649 
37650   const char *p = "";
37651   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37652     {
37653       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37654       p = IDENTIFIER_POINTER (id);
37655     }
37656 
37657   if (strcmp (p, "initializer") == 0)
37658     {
37659       cp_lexer_consume_token (parser->lexer);
37660       matching_parens parens;
37661       if (!parens.require_open (parser))
37662 	return false;
37663 
37664       p = "";
37665       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37666 	{
37667 	  tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37668 	  p = IDENTIFIER_POINTER (id);
37669 	}
37670 
37671       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37672       DECL_ARTIFICIAL (omp_priv) = 1;
37673       pushdecl (omp_priv);
37674       add_decl_expr (omp_priv);
37675       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37676       DECL_ARTIFICIAL (omp_orig) = 1;
37677       pushdecl (omp_orig);
37678       add_decl_expr (omp_orig);
37679 
37680       keep_next_level (true);
37681       block = begin_omp_structured_block ();
37682 
37683       bool ctor = false;
37684       if (strcmp (p, "omp_priv") == 0)
37685 	{
37686 	  bool is_direct_init, is_non_constant_init;
37687 	  ctor = true;
37688 	  cp_lexer_consume_token (parser->lexer);
37689 	  /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
37690 	  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37691 	      || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37692 		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37693 		     == CPP_CLOSE_PAREN
37694 		  && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37695 		     == CPP_CLOSE_PAREN))
37696 	    {
37697 	      finish_omp_structured_block (block);
37698 	      error ("invalid initializer clause");
37699 	      return false;
37700 	    }
37701 	  initializer = cp_parser_initializer (parser, &is_direct_init,
37702 					       &is_non_constant_init);
37703 	  cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37704 			  NULL_TREE, LOOKUP_ONLYCONVERTING);
37705 	}
37706       else
37707 	{
37708 	  cp_parser_parse_tentatively (parser);
37709 	  tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37710 						  /*check_dependency_p=*/true,
37711 						  /*template_p=*/NULL,
37712 						  /*declarator_p=*/false,
37713 						  /*optional_p=*/false);
37714 	  vec<tree, va_gc> *args;
37715 	  if (fn_name == error_mark_node
37716 	      || cp_parser_error_occurred (parser)
37717 	      || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37718 	      || ((args = cp_parser_parenthesized_expression_list
37719 				(parser, non_attr, /*cast_p=*/false,
37720 				 /*allow_expansion_p=*/true,
37721 				 /*non_constant_p=*/NULL)),
37722 		  cp_parser_error_occurred (parser)))
37723 	    {
37724 	      finish_omp_structured_block (block);
37725 	      cp_parser_abort_tentative_parse (parser);
37726 	      cp_parser_error (parser, "expected id-expression (arguments)");
37727 	      return false;
37728 	    }
37729 	  unsigned int i;
37730 	  tree arg;
37731 	  FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37732 	    if (arg == omp_priv
37733 		|| (TREE_CODE (arg) == ADDR_EXPR
37734 		    && TREE_OPERAND (arg, 0) == omp_priv))
37735 	      break;
37736 	  cp_parser_abort_tentative_parse (parser);
37737 	  if (arg == NULL_TREE)
37738 	    error ("one of the initializer call arguments should be %<omp_priv%>"
37739 		   " or %<&omp_priv%>");
37740 	  initializer = cp_parser_postfix_expression (parser, false, false, false,
37741 						      false, NULL);
37742 	  finish_expr_stmt (initializer);
37743 	}
37744 
37745       block = finish_omp_structured_block (block);
37746       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37747       if (processing_template_decl)
37748 	block = build_stmt (input_location, EXPR_STMT, block);
37749       add_stmt (block);
37750 
37751       if (ctor)
37752 	add_decl_expr (omp_orig);
37753 
37754       if (!parens.require_close (parser))
37755 	return false;
37756     }
37757 
37758   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37759     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37760                               UNKNOWN_LOCATION);
37761 
37762   return true;
37763 }
37764 
37765 /* OpenMP 4.0
37766    #pragma omp declare reduction (reduction-id : typename-list : expression) \
37767       initializer-clause[opt] new-line
37768 
37769    initializer-clause:
37770       initializer (omp_priv initializer)
37771       initializer (function-name (argument-list))  */
37772 
37773 static void
cp_parser_omp_declare_reduction(cp_parser * parser,cp_token * pragma_tok,enum pragma_context)37774 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37775 				 enum pragma_context)
37776 {
37777   auto_vec<tree> types;
37778   enum tree_code reduc_code = ERROR_MARK;
37779   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37780   unsigned int i;
37781   cp_token *first_token;
37782   cp_token_cache *cp;
37783   int errs;
37784   void *p;
37785 
37786   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
37787   p = obstack_alloc (&declarator_obstack, 0);
37788 
37789   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37790     goto fail;
37791 
37792   switch (cp_lexer_peek_token (parser->lexer)->type)
37793     {
37794     case CPP_PLUS:
37795       reduc_code = PLUS_EXPR;
37796       break;
37797     case CPP_MULT:
37798       reduc_code = MULT_EXPR;
37799       break;
37800     case CPP_MINUS:
37801       reduc_code = MINUS_EXPR;
37802       break;
37803     case CPP_AND:
37804       reduc_code = BIT_AND_EXPR;
37805       break;
37806     case CPP_XOR:
37807       reduc_code = BIT_XOR_EXPR;
37808       break;
37809     case CPP_OR:
37810       reduc_code = BIT_IOR_EXPR;
37811       break;
37812     case CPP_AND_AND:
37813       reduc_code = TRUTH_ANDIF_EXPR;
37814       break;
37815     case CPP_OR_OR:
37816       reduc_code = TRUTH_ORIF_EXPR;
37817       break;
37818     case CPP_NAME:
37819       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37820       break;
37821     default:
37822       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37823 			       "%<|%>, %<&&%>, %<||%> or identifier");
37824       goto fail;
37825     }
37826 
37827   if (reduc_code != ERROR_MARK)
37828     cp_lexer_consume_token (parser->lexer);
37829 
37830   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37831   if (reduc_id == error_mark_node)
37832     goto fail;
37833 
37834   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37835     goto fail;
37836 
37837   /* Types may not be defined in declare reduction type list.  */
37838   const char *saved_message;
37839   saved_message = parser->type_definition_forbidden_message;
37840   parser->type_definition_forbidden_message
37841     = G_("types may not be defined in declare reduction type list");
37842   bool saved_colon_corrects_to_scope_p;
37843   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37844   parser->colon_corrects_to_scope_p = false;
37845   bool saved_colon_doesnt_start_class_def_p;
37846   saved_colon_doesnt_start_class_def_p
37847     = parser->colon_doesnt_start_class_def_p;
37848   parser->colon_doesnt_start_class_def_p = true;
37849 
37850   while (true)
37851     {
37852       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37853       type = cp_parser_type_id (parser);
37854       if (type == error_mark_node)
37855 	;
37856       else if (ARITHMETIC_TYPE_P (type)
37857 	       && (orig_reduc_id == NULL_TREE
37858 		   || (TREE_CODE (type) != COMPLEX_TYPE
37859 		       && (id_equal (orig_reduc_id, "min")
37860 			   || id_equal (orig_reduc_id, "max")))))
37861 	error_at (loc, "predeclared arithmetic type %qT in "
37862 		       "%<#pragma omp declare reduction%>", type);
37863       else if (TREE_CODE (type) == FUNCTION_TYPE
37864 	       || TREE_CODE (type) == METHOD_TYPE
37865 	       || TREE_CODE (type) == ARRAY_TYPE)
37866 	error_at (loc, "function or array type %qT in "
37867 		       "%<#pragma omp declare reduction%>", type);
37868       else if (TREE_CODE (type) == REFERENCE_TYPE)
37869 	error_at (loc, "reference type %qT in "
37870 		       "%<#pragma omp declare reduction%>", type);
37871       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37872 	error_at (loc, "const, volatile or __restrict qualified type %qT in "
37873 		       "%<#pragma omp declare reduction%>", type);
37874       else
37875 	types.safe_push (type);
37876 
37877       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37878 	cp_lexer_consume_token (parser->lexer);
37879       else
37880 	break;
37881     }
37882 
37883   /* Restore the saved message.  */
37884   parser->type_definition_forbidden_message = saved_message;
37885   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37886   parser->colon_doesnt_start_class_def_p
37887     = saved_colon_doesnt_start_class_def_p;
37888 
37889   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37890       || types.is_empty ())
37891     {
37892      fail:
37893       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37894       goto done;
37895     }
37896 
37897   first_token = cp_lexer_peek_token (parser->lexer);
37898   cp = NULL;
37899   errs = errorcount;
37900   FOR_EACH_VEC_ELT (types, i, type)
37901     {
37902       tree fntype
37903 	= build_function_type_list (void_type_node,
37904 				    cp_build_reference_type (type, false),
37905 				    NULL_TREE);
37906       tree this_reduc_id = reduc_id;
37907       if (!dependent_type_p (type))
37908 	this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37909       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37910       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37911       DECL_ARTIFICIAL (fndecl) = 1;
37912       DECL_EXTERNAL (fndecl) = 1;
37913       DECL_DECLARED_INLINE_P (fndecl) = 1;
37914       DECL_IGNORED_P (fndecl) = 1;
37915       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37916       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37917       DECL_ATTRIBUTES (fndecl)
37918 	= tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37919 		     DECL_ATTRIBUTES (fndecl));
37920       if (processing_template_decl)
37921 	fndecl = push_template_decl (fndecl);
37922       bool block_scope = false;
37923       tree block = NULL_TREE;
37924       if (current_function_decl)
37925 	{
37926 	  block_scope = true;
37927 	  DECL_CONTEXT (fndecl) = global_namespace;
37928 	  if (!processing_template_decl)
37929 	    pushdecl (fndecl);
37930 	}
37931       else if (current_class_type)
37932 	{
37933 	  if (cp == NULL)
37934 	    {
37935 	      while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37936 		     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37937 		cp_lexer_consume_token (parser->lexer);
37938 	      if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37939 		goto fail;
37940 	      cp = cp_token_cache_new (first_token,
37941 				       cp_lexer_peek_nth_token (parser->lexer,
37942 								2));
37943 	    }
37944 	  DECL_STATIC_FUNCTION_P (fndecl) = 1;
37945 	  finish_member_declaration (fndecl);
37946 	  DECL_PENDING_INLINE_INFO (fndecl) = cp;
37947 	  DECL_PENDING_INLINE_P (fndecl) = 1;
37948 	  vec_safe_push (unparsed_funs_with_definitions, fndecl);
37949 	  continue;
37950 	}
37951       else
37952 	{
37953 	  DECL_CONTEXT (fndecl) = current_namespace;
37954 	  pushdecl (fndecl);
37955 	}
37956       if (!block_scope)
37957 	start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37958       else
37959 	block = begin_omp_structured_block ();
37960       if (cp)
37961 	{
37962 	  cp_parser_push_lexer_for_tokens (parser, cp);
37963 	  parser->lexer->in_pragma = true;
37964 	}
37965       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37966 	{
37967 	  if (!block_scope)
37968 	    finish_function (/*inline_p=*/false);
37969 	  else
37970 	    DECL_CONTEXT (fndecl) = current_function_decl;
37971 	  if (cp)
37972 	    cp_parser_pop_lexer (parser);
37973 	  goto fail;
37974 	}
37975       if (cp)
37976 	cp_parser_pop_lexer (parser);
37977       if (!block_scope)
37978 	finish_function (/*inline_p=*/false);
37979       else
37980 	{
37981 	  DECL_CONTEXT (fndecl) = current_function_decl;
37982 	  block = finish_omp_structured_block (block);
37983 	  if (TREE_CODE (block) == BIND_EXPR)
37984 	    DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37985 	  else if (TREE_CODE (block) == STATEMENT_LIST)
37986 	    DECL_SAVED_TREE (fndecl) = block;
37987 	  if (processing_template_decl)
37988 	    add_decl_expr (fndecl);
37989 	}
37990       cp_check_omp_declare_reduction (fndecl);
37991       if (cp == NULL && types.length () > 1)
37992 	cp = cp_token_cache_new (first_token,
37993 				 cp_lexer_peek_nth_token (parser->lexer, 2));
37994       if (errs != errorcount)
37995 	break;
37996     }
37997 
37998   cp_parser_require_pragma_eol (parser, pragma_tok);
37999 
38000  done:
38001   /* Free any declarators allocated.  */
38002   obstack_free (&declarator_obstack, p);
38003 }
38004 
38005 /* OpenMP 4.0
38006    #pragma omp declare simd declare-simd-clauses[optseq] new-line
38007    #pragma omp declare reduction (reduction-id : typename-list : expression) \
38008       initializer-clause[opt] new-line
38009    #pragma omp declare target new-line  */
38010 
38011 static bool
cp_parser_omp_declare(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)38012 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
38013 		       enum pragma_context context)
38014 {
38015   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38016     {
38017       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38018       const char *p = IDENTIFIER_POINTER (id);
38019 
38020       if (strcmp (p, "simd") == 0)
38021 	{
38022 	  cp_lexer_consume_token (parser->lexer);
38023 	  cp_parser_omp_declare_simd (parser, pragma_tok,
38024 				      context);
38025 	  return true;
38026 	}
38027       cp_ensure_no_omp_declare_simd (parser);
38028       if (strcmp (p, "reduction") == 0)
38029 	{
38030 	  cp_lexer_consume_token (parser->lexer);
38031 	  cp_parser_omp_declare_reduction (parser, pragma_tok,
38032 					   context);
38033 	  return false;
38034 	}
38035       if (!flag_openmp)  /* flag_openmp_simd  */
38036 	{
38037 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38038 	  return false;
38039 	}
38040       if (strcmp (p, "target") == 0)
38041 	{
38042 	  cp_lexer_consume_token (parser->lexer);
38043 	  cp_parser_omp_declare_target (parser, pragma_tok);
38044 	  return false;
38045 	}
38046     }
38047   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
38048 			   "or %<target%>");
38049   cp_parser_require_pragma_eol (parser, pragma_tok);
38050   return false;
38051 }
38052 
38053 /* OpenMP 4.5:
38054    #pragma omp taskloop taskloop-clause[optseq] new-line
38055      for-loop
38056 
38057    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
38058      for-loop  */
38059 
38060 #define OMP_TASKLOOP_CLAUSE_MASK				\
38061 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)	\
38062 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)	\
38063 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)	\
38064 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)	\
38065 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)	\
38066 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)	\
38067 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)	\
38068 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)	\
38069 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)	\
38070 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)		\
38071 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)	\
38072 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)	\
38073 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)	\
38074 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
38075 
38076 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)38077 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
38078 			char *p_name, omp_clause_mask mask, tree *cclauses,
38079 			bool *if_p)
38080 {
38081   tree clauses, sb, ret;
38082   unsigned int save;
38083   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38084 
38085   strcat (p_name, " taskloop");
38086   mask |= OMP_TASKLOOP_CLAUSE_MASK;
38087 
38088   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38089     {
38090       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38091       const char *p = IDENTIFIER_POINTER (id);
38092 
38093       if (strcmp (p, "simd") == 0)
38094 	{
38095 	  tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
38096 	  if (cclauses == NULL)
38097 	    cclauses = cclauses_buf;
38098 
38099 	  cp_lexer_consume_token (parser->lexer);
38100 	  if (!flag_openmp)  /* flag_openmp_simd  */
38101 	    return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38102 				       cclauses, if_p);
38103 	  sb = begin_omp_structured_block ();
38104 	  save = cp_parser_begin_omp_structured_block (parser);
38105 	  ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38106 				    cclauses, if_p);
38107 	  cp_parser_end_omp_structured_block (parser, save);
38108 	  tree body = finish_omp_structured_block (sb);
38109 	  if (ret == NULL)
38110 	    return ret;
38111 	  ret = make_node (OMP_TASKLOOP);
38112 	  TREE_TYPE (ret) = void_type_node;
38113 	  OMP_FOR_BODY (ret) = body;
38114 	  OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38115 	  SET_EXPR_LOCATION (ret, loc);
38116 	  add_stmt (ret);
38117 	  return ret;
38118 	}
38119     }
38120   if (!flag_openmp)  /* flag_openmp_simd  */
38121     {
38122       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38123       return NULL_TREE;
38124     }
38125 
38126   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38127 				       cclauses == NULL);
38128   if (cclauses)
38129     {
38130       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38131       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38132     }
38133 
38134   sb = begin_omp_structured_block ();
38135   save = cp_parser_begin_omp_structured_block (parser);
38136 
38137   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38138 				if_p);
38139 
38140   cp_parser_end_omp_structured_block (parser, save);
38141   add_stmt (finish_omp_structured_block (sb));
38142 
38143   return ret;
38144 }
38145 
38146 
38147 /* OpenACC 2.0:
38148    # pragma acc routine oacc-routine-clause[optseq] new-line
38149      function-definition
38150 
38151    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38152 */
38153 
38154 #define OACC_ROUTINE_CLAUSE_MASK					\
38155 	( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)		\
38156 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)		\
38157 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)		\
38158 	| (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38159 
38160 
38161 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
38162    component, which must resolve to a declared namespace-scope
38163    function.  The clauses are either processed directly (for a named
38164    function), or defered until the immediatley following declaration
38165    is parsed.  */
38166 
38167 static void
cp_parser_oacc_routine(cp_parser * parser,cp_token * pragma_tok,enum pragma_context context)38168 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38169 			enum pragma_context context)
38170 {
38171   gcc_checking_assert (context == pragma_external);
38172   /* The checking for "another pragma following this one" in the "no optional
38173      '( name )'" case makes sure that we dont re-enter.  */
38174   gcc_checking_assert (parser->oacc_routine == NULL);
38175 
38176   cp_oacc_routine_data data;
38177   data.error_seen = false;
38178   data.fndecl_seen = false;
38179   data.tokens = vNULL;
38180   data.clauses = NULL_TREE;
38181   data.loc = pragma_tok->location;
38182   /* It is safe to take the address of a local variable; it will only be
38183      used while this scope is live.  */
38184   parser->oacc_routine = &data;
38185 
38186   /* Look for optional '( name )'.  */
38187   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38188     {
38189       matching_parens parens;
38190       parens.consume_open (parser); /* '(' */
38191 
38192       /* We parse the name as an id-expression.  If it resolves to
38193 	 anything other than a non-overloaded function at namespace
38194 	 scope, it's an error.  */
38195       location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38196       tree name = cp_parser_id_expression (parser,
38197 					   /*template_keyword_p=*/false,
38198 					   /*check_dependency_p=*/false,
38199 					   /*template_p=*/NULL,
38200 					   /*declarator_p=*/false,
38201 					   /*optional_p=*/false);
38202       tree decl = (identifier_p (name)
38203 		   ? cp_parser_lookup_name_simple (parser, name, name_loc)
38204 		   : name);
38205       if (name != error_mark_node && decl == error_mark_node)
38206 	cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38207 
38208       if (decl == error_mark_node
38209 	  || !parens.require_close (parser))
38210 	{
38211 	  cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38212 	  parser->oacc_routine = NULL;
38213 	  return;
38214 	}
38215 
38216       data.clauses
38217 	= cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38218 				      "#pragma acc routine",
38219 				      cp_lexer_peek_token (parser->lexer));
38220 
38221       if (decl && is_overloaded_fn (decl)
38222 	  && (TREE_CODE (decl) != FUNCTION_DECL
38223 	      || DECL_FUNCTION_TEMPLATE_P  (decl)))
38224 	{
38225 	  error_at (name_loc,
38226 		    "%<#pragma acc routine%> names a set of overloads");
38227 	  parser->oacc_routine = NULL;
38228 	  return;
38229 	}
38230 
38231       /* Perhaps we should use the same rule as declarations in different
38232 	 namespaces?  */
38233       if (!DECL_NAMESPACE_SCOPE_P (decl))
38234 	{
38235 	  error_at (name_loc,
38236 		    "%qD does not refer to a namespace scope function", decl);
38237 	  parser->oacc_routine = NULL;
38238 	  return;
38239 	}
38240 
38241       if (TREE_CODE (decl) != FUNCTION_DECL)
38242 	{
38243 	  error_at (name_loc, "%qD does not refer to a function", decl);
38244 	  parser->oacc_routine = NULL;
38245 	  return;
38246 	}
38247 
38248       cp_finalize_oacc_routine (parser, decl, false);
38249       parser->oacc_routine = NULL;
38250     }
38251   else /* No optional '( name )'.  */
38252     {
38253       /* Store away all pragma tokens.  */
38254       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38255 	     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38256 	cp_lexer_consume_token (parser->lexer);
38257       if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38258 	parser->oacc_routine->error_seen = true;
38259       cp_parser_require_pragma_eol (parser, pragma_tok);
38260       struct cp_token_cache *cp
38261 	= cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38262       parser->oacc_routine->tokens.safe_push (cp);
38263 
38264       /* Emit a helpful diagnostic if there's another pragma following this
38265 	 one.  */
38266       if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38267 	{
38268 	  cp_ensure_no_oacc_routine (parser);
38269 	  data.tokens.release ();
38270 	  /* ..., and then just keep going.  */
38271 	  return;
38272 	}
38273 
38274       /* We only have to consider the pragma_external case here.  */
38275       cp_parser_declaration (parser);
38276       if (parser->oacc_routine
38277 	  && !parser->oacc_routine->fndecl_seen)
38278 	cp_ensure_no_oacc_routine (parser);
38279       else
38280 	parser->oacc_routine = NULL;
38281       data.tokens.release ();
38282     }
38283 }
38284 
38285 /* Finalize #pragma acc routine clauses after direct declarator has
38286    been parsed.  */
38287 
38288 static tree
cp_parser_late_parsing_oacc_routine(cp_parser * parser,tree attrs)38289 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38290 {
38291   struct cp_token_cache *ce;
38292   cp_oacc_routine_data *data = parser->oacc_routine;
38293 
38294   if (!data->error_seen && data->fndecl_seen)
38295     {
38296       error_at (data->loc,
38297 		"%<#pragma acc routine%> not immediately followed by "
38298 		"a single function declaration or definition");
38299       data->error_seen = true;
38300     }
38301   if (data->error_seen)
38302     return attrs;
38303 
38304   gcc_checking_assert (data->tokens.length () == 1);
38305   ce = data->tokens[0];
38306 
38307   cp_parser_push_lexer_for_tokens (parser, ce);
38308   parser->lexer->in_pragma = true;
38309   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38310 
38311   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38312   gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38313   parser->oacc_routine->clauses
38314     = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38315 				  "#pragma acc routine", pragma_tok);
38316   cp_parser_pop_lexer (parser);
38317   /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38318      fndecl_seen.  */
38319 
38320   return attrs;
38321 }
38322 
38323 /* Apply any saved OpenACC routine clauses to a just-parsed
38324    declaration.  */
38325 
38326 static void
cp_finalize_oacc_routine(cp_parser * parser,tree fndecl,bool is_defn)38327 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38328 {
38329   if (__builtin_expect (parser->oacc_routine != NULL, 0))
38330     {
38331       /* Keep going if we're in error reporting mode.  */
38332       if (parser->oacc_routine->error_seen
38333 	  || fndecl == error_mark_node)
38334 	return;
38335 
38336       if (parser->oacc_routine->fndecl_seen)
38337 	{
38338 	  error_at (parser->oacc_routine->loc,
38339 		    "%<#pragma acc routine%> not immediately followed by"
38340 		    " a single function declaration or definition");
38341 	  parser->oacc_routine = NULL;
38342 	  return;
38343 	}
38344       if (TREE_CODE (fndecl) != FUNCTION_DECL)
38345 	{
38346 	  cp_ensure_no_oacc_routine (parser);
38347 	  return;
38348 	}
38349 
38350       if (oacc_get_fn_attrib (fndecl))
38351 	{
38352 	  error_at (parser->oacc_routine->loc,
38353 		    "%<#pragma acc routine%> already applied to %qD", fndecl);
38354 	  parser->oacc_routine = NULL;
38355 	  return;
38356 	}
38357 
38358       if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38359 	{
38360 	  error_at (parser->oacc_routine->loc,
38361 		    TREE_USED (fndecl)
38362 		    ? G_("%<#pragma acc routine%> must be applied before use")
38363 		    : G_("%<#pragma acc routine%> must be applied before "
38364 			 "definition"));
38365 	  parser->oacc_routine = NULL;
38366 	  return;
38367 	}
38368 
38369       /* Process the routine's dimension clauses.  */
38370       tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38371       oacc_replace_fn_attrib (fndecl, dims);
38372 
38373       /* Add an "omp declare target" attribute.  */
38374       DECL_ATTRIBUTES (fndecl)
38375 	= tree_cons (get_identifier ("omp declare target"),
38376 		     NULL_TREE, DECL_ATTRIBUTES (fndecl));
38377 
38378       /* Don't unset parser->oacc_routine here: we may still need it to
38379 	 diagnose wrong usage.  But, remember that we've used this "#pragma acc
38380 	 routine".  */
38381       parser->oacc_routine->fndecl_seen = true;
38382     }
38383 }
38384 
38385 /* Main entry point to OpenMP statement pragmas.  */
38386 
38387 static void
cp_parser_omp_construct(cp_parser * parser,cp_token * pragma_tok,bool * if_p)38388 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38389 {
38390   tree stmt;
38391   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38392   omp_clause_mask mask (0);
38393 
38394   switch (cp_parser_pragma_kind (pragma_tok))
38395     {
38396     case PRAGMA_OACC_ATOMIC:
38397       cp_parser_omp_atomic (parser, pragma_tok);
38398       return;
38399     case PRAGMA_OACC_CACHE:
38400       stmt = cp_parser_oacc_cache (parser, pragma_tok);
38401       break;
38402     case PRAGMA_OACC_DATA:
38403       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38404       break;
38405     case PRAGMA_OACC_ENTER_DATA:
38406       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38407       break;
38408     case PRAGMA_OACC_EXIT_DATA:
38409       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38410       break;
38411     case PRAGMA_OACC_HOST_DATA:
38412       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38413       break;
38414     case PRAGMA_OACC_KERNELS:
38415     case PRAGMA_OACC_PARALLEL:
38416       strcpy (p_name, "#pragma acc");
38417       stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38418 					      if_p);
38419       break;
38420     case PRAGMA_OACC_LOOP:
38421       strcpy (p_name, "#pragma acc");
38422       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38423 				  if_p);
38424       break;
38425     case PRAGMA_OACC_UPDATE:
38426       stmt = cp_parser_oacc_update (parser, pragma_tok);
38427       break;
38428     case PRAGMA_OACC_WAIT:
38429       stmt = cp_parser_oacc_wait (parser, pragma_tok);
38430       break;
38431     case PRAGMA_OMP_ATOMIC:
38432       cp_parser_omp_atomic (parser, pragma_tok);
38433       return;
38434     case PRAGMA_OMP_CRITICAL:
38435       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38436       break;
38437     case PRAGMA_OMP_DISTRIBUTE:
38438       strcpy (p_name, "#pragma omp");
38439       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38440 				       if_p);
38441       break;
38442     case PRAGMA_OMP_FOR:
38443       strcpy (p_name, "#pragma omp");
38444       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38445 				if_p);
38446       break;
38447     case PRAGMA_OMP_MASTER:
38448       stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38449       break;
38450     case PRAGMA_OMP_PARALLEL:
38451       strcpy (p_name, "#pragma omp");
38452       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38453 				     if_p);
38454       break;
38455     case PRAGMA_OMP_SECTIONS:
38456       strcpy (p_name, "#pragma omp");
38457       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38458       break;
38459     case PRAGMA_OMP_SIMD:
38460       strcpy (p_name, "#pragma omp");
38461       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38462 				 if_p);
38463       break;
38464     case PRAGMA_OMP_SINGLE:
38465       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38466       break;
38467     case PRAGMA_OMP_TASK:
38468       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38469       break;
38470     case PRAGMA_OMP_TASKGROUP:
38471       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38472       break;
38473     case PRAGMA_OMP_TASKLOOP:
38474       strcpy (p_name, "#pragma omp");
38475       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38476 				     if_p);
38477       break;
38478     case PRAGMA_OMP_TEAMS:
38479       strcpy (p_name, "#pragma omp");
38480       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38481 				  if_p);
38482       break;
38483     default:
38484       gcc_unreachable ();
38485     }
38486 
38487   protected_set_expr_location (stmt, pragma_tok->location);
38488 }
38489 
38490 /* Transactional Memory parsing routines.  */
38491 
38492 /* Parse a transaction attribute.
38493 
38494    txn-attribute:
38495 	attribute
38496 	[ [ identifier ] ]
38497 
38498    We use this instead of cp_parser_attributes_opt for transactions to avoid
38499    the pedwarn in C++98 mode.  */
38500 
38501 static tree
cp_parser_txn_attribute_opt(cp_parser * parser)38502 cp_parser_txn_attribute_opt (cp_parser *parser)
38503 {
38504   cp_token *token;
38505   tree attr_name, attr = NULL;
38506 
38507   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38508     return cp_parser_attributes_opt (parser);
38509 
38510   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38511     return NULL_TREE;
38512   cp_lexer_consume_token (parser->lexer);
38513   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38514     goto error1;
38515 
38516   token = cp_lexer_peek_token (parser->lexer);
38517   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38518     {
38519       token = cp_lexer_consume_token (parser->lexer);
38520 
38521       attr_name = (token->type == CPP_KEYWORD
38522 		   /* For keywords, use the canonical spelling,
38523 		      not the parsed identifier.  */
38524 		   ? ridpointers[(int) token->keyword]
38525 		   : token->u.value);
38526       attr = build_tree_list (attr_name, NULL_TREE);
38527     }
38528   else
38529     cp_parser_error (parser, "expected identifier");
38530 
38531   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38532  error1:
38533   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38534   return attr;
38535 }
38536 
38537 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38538 
38539    transaction-statement:
38540      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38541        compound-statement
38542      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38543 */
38544 
38545 static tree
cp_parser_transaction(cp_parser * parser,cp_token * token)38546 cp_parser_transaction (cp_parser *parser, cp_token *token)
38547 {
38548   unsigned char old_in = parser->in_transaction;
38549   unsigned char this_in = 1, new_in;
38550   enum rid keyword = token->keyword;
38551   tree stmt, attrs, noex;
38552 
38553   cp_lexer_consume_token (parser->lexer);
38554 
38555   if (keyword == RID_TRANSACTION_RELAXED
38556       || keyword == RID_SYNCHRONIZED)
38557     this_in |= TM_STMT_ATTR_RELAXED;
38558   else
38559     {
38560       attrs = cp_parser_txn_attribute_opt (parser);
38561       if (attrs)
38562 	this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38563     }
38564 
38565   /* Parse a noexcept specification.  */
38566   if (keyword == RID_ATOMIC_NOEXCEPT)
38567     noex = boolean_true_node;
38568   else if (keyword == RID_ATOMIC_CANCEL)
38569     {
38570       /* cancel-and-throw is unimplemented.  */
38571       sorry ("atomic_cancel");
38572       noex = NULL_TREE;
38573     }
38574   else
38575     noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38576 
38577   /* Keep track if we're in the lexical scope of an outer transaction.  */
38578   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38579 
38580   stmt = begin_transaction_stmt (token->location, NULL, this_in);
38581 
38582   parser->in_transaction = new_in;
38583   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38584   parser->in_transaction = old_in;
38585 
38586   finish_transaction_stmt (stmt, NULL, this_in, noex);
38587 
38588   return stmt;
38589 }
38590 
38591 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38592 
38593    transaction-expression:
38594      __transaction_atomic txn-noexcept-spec[opt] ( expression )
38595      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38596 */
38597 
38598 static tree
cp_parser_transaction_expression(cp_parser * parser,enum rid keyword)38599 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38600 {
38601   unsigned char old_in = parser->in_transaction;
38602   unsigned char this_in = 1;
38603   cp_token *token;
38604   tree expr, noex;
38605   bool noex_expr;
38606   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38607 
38608   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38609       || keyword == RID_TRANSACTION_RELAXED);
38610 
38611   if (!flag_tm)
38612     error_at (loc,
38613 	      keyword == RID_TRANSACTION_RELAXED
38614 	      ? G_("%<__transaction_relaxed%> without transactional memory "
38615 		  "support enabled")
38616 	      : G_("%<__transaction_atomic%> without transactional memory "
38617 		   "support enabled"));
38618 
38619   token = cp_parser_require_keyword (parser, keyword,
38620       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38621 	  : RT_TRANSACTION_RELAXED));
38622   gcc_assert (token != NULL);
38623 
38624   if (keyword == RID_TRANSACTION_RELAXED)
38625     this_in |= TM_STMT_ATTR_RELAXED;
38626 
38627   /* Set this early.  This might mean that we allow transaction_cancel in
38628      an expression that we find out later actually has to be a constexpr.
38629      However, we expect that cxx_constant_value will be able to deal with
38630      this; also, if the noexcept has no constexpr, then what we parse next
38631      really is a transaction's body.  */
38632   parser->in_transaction = this_in;
38633 
38634   /* Parse a noexcept specification.  */
38635   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38636 					       true);
38637 
38638   if (!noex || !noex_expr
38639       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38640     {
38641       matching_parens parens;
38642       parens.require_open (parser);
38643 
38644       expr = cp_parser_expression (parser);
38645       expr = finish_parenthesized_expr (expr);
38646 
38647       parens.require_close (parser);
38648     }
38649   else
38650     {
38651       /* The only expression that is available got parsed for the noexcept
38652          already.  noexcept is true then.  */
38653       expr = noex;
38654       noex = boolean_true_node;
38655     }
38656 
38657   expr = build_transaction_expr (token->location, expr, this_in, noex);
38658   parser->in_transaction = old_in;
38659 
38660   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38661     return error_mark_node;
38662 
38663   return (flag_tm ? expr : error_mark_node);
38664 }
38665 
38666 /* Parse a function-transaction-block.
38667 
38668    function-transaction-block:
38669      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38670 	 function-body
38671      __transaction_atomic txn-attribute[opt] function-try-block
38672      __transaction_relaxed ctor-initializer[opt] function-body
38673      __transaction_relaxed function-try-block
38674 */
38675 
38676 static void
cp_parser_function_transaction(cp_parser * parser,enum rid keyword)38677 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38678 {
38679   unsigned char old_in = parser->in_transaction;
38680   unsigned char new_in = 1;
38681   tree compound_stmt, stmt, attrs;
38682   cp_token *token;
38683 
38684   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38685       || keyword == RID_TRANSACTION_RELAXED);
38686   token = cp_parser_require_keyword (parser, keyword,
38687       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38688 	  : RT_TRANSACTION_RELAXED));
38689   gcc_assert (token != NULL);
38690 
38691   if (keyword == RID_TRANSACTION_RELAXED)
38692     new_in |= TM_STMT_ATTR_RELAXED;
38693   else
38694     {
38695       attrs = cp_parser_txn_attribute_opt (parser);
38696       if (attrs)
38697 	new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38698     }
38699 
38700   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38701 
38702   parser->in_transaction = new_in;
38703 
38704   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38705     cp_parser_function_try_block (parser);
38706   else
38707     cp_parser_ctor_initializer_opt_and_function_body
38708       (parser, /*in_function_try_block=*/false);
38709 
38710   parser->in_transaction = old_in;
38711 
38712   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38713 }
38714 
38715 /* Parse a __transaction_cancel statement.
38716 
38717    cancel-statement:
38718      __transaction_cancel txn-attribute[opt] ;
38719      __transaction_cancel txn-attribute[opt] throw-expression ;
38720 
38721    ??? Cancel and throw is not yet implemented.  */
38722 
38723 static tree
cp_parser_transaction_cancel(cp_parser * parser)38724 cp_parser_transaction_cancel (cp_parser *parser)
38725 {
38726   cp_token *token;
38727   bool is_outer = false;
38728   tree stmt, attrs;
38729 
38730   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38731 				     RT_TRANSACTION_CANCEL);
38732   gcc_assert (token != NULL);
38733 
38734   attrs = cp_parser_txn_attribute_opt (parser);
38735   if (attrs)
38736     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38737 
38738   /* ??? Parse cancel-and-throw here.  */
38739 
38740   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38741 
38742   if (!flag_tm)
38743     {
38744       error_at (token->location, "%<__transaction_cancel%> without "
38745 		"transactional memory support enabled");
38746       return error_mark_node;
38747     }
38748   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38749     {
38750       error_at (token->location, "%<__transaction_cancel%> within a "
38751 		"%<__transaction_relaxed%>");
38752       return error_mark_node;
38753     }
38754   else if (is_outer)
38755     {
38756       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38757 	  && !is_tm_may_cancel_outer (current_function_decl))
38758 	{
38759 	  error_at (token->location, "outer %<__transaction_cancel%> not "
38760 		    "within outer %<__transaction_atomic%>");
38761 	  error_at (token->location,
38762 		    "  or a %<transaction_may_cancel_outer%> function");
38763 	  return error_mark_node;
38764 	}
38765     }
38766   else if (parser->in_transaction == 0)
38767     {
38768       error_at (token->location, "%<__transaction_cancel%> not within "
38769 		"%<__transaction_atomic%>");
38770       return error_mark_node;
38771     }
38772 
38773   stmt = build_tm_abort_call (token->location, is_outer);
38774   add_stmt (stmt);
38775 
38776   return stmt;
38777 }
38778 
38779 /* The parser.  */
38780 
38781 static GTY (()) cp_parser *the_parser;
38782 
38783 
38784 /* Special handling for the first token or line in the file.  The first
38785    thing in the file might be #pragma GCC pch_preprocess, which loads a
38786    PCH file, which is a GC collection point.  So we need to handle this
38787    first pragma without benefit of an existing lexer structure.
38788 
38789    Always returns one token to the caller in *FIRST_TOKEN.  This is
38790    either the true first token of the file, or the first token after
38791    the initial pragma.  */
38792 
38793 static void
cp_parser_initial_pragma(cp_token * first_token)38794 cp_parser_initial_pragma (cp_token *first_token)
38795 {
38796   tree name = NULL;
38797 
38798   cp_lexer_get_preprocessor_token (NULL, first_token);
38799   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38800     {
38801       c_common_no_more_pch ();
38802       return;
38803     }
38804 
38805   cp_lexer_get_preprocessor_token (NULL, first_token);
38806   if (first_token->type == CPP_STRING)
38807     {
38808       name = first_token->u.value;
38809 
38810       cp_lexer_get_preprocessor_token (NULL, first_token);
38811       if (first_token->type != CPP_PRAGMA_EOL)
38812 	error_at (first_token->location,
38813 		  "junk at end of %<#pragma GCC pch_preprocess%>");
38814     }
38815   else
38816     error_at (first_token->location, "expected string literal");
38817 
38818   /* Skip to the end of the pragma.  */
38819   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38820     cp_lexer_get_preprocessor_token (NULL, first_token);
38821 
38822   /* Now actually load the PCH file.  */
38823   if (name)
38824     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38825 
38826   /* Read one more token to return to our caller.  We have to do this
38827      after reading the PCH file in, since its pointers have to be
38828      live.  */
38829   cp_lexer_get_preprocessor_token (NULL, first_token);
38830 }
38831 
38832 /* Parse a pragma GCC ivdep.  */
38833 
38834 static bool
cp_parser_pragma_ivdep(cp_parser * parser,cp_token * pragma_tok)38835 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38836 {
38837   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38838   return true;
38839 }
38840 
38841 /* Parse a pragma GCC unroll.  */
38842 
38843 static unsigned short
cp_parser_pragma_unroll(cp_parser * parser,cp_token * pragma_tok)38844 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38845 {
38846   location_t location = cp_lexer_peek_token (parser->lexer)->location;
38847   tree expr = cp_parser_constant_expression (parser);
38848   unsigned short unroll;
38849   expr = maybe_constant_value (expr);
38850   HOST_WIDE_INT lunroll = 0;
38851   if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38852       || TREE_CODE (expr) != INTEGER_CST
38853       || (lunroll = tree_to_shwi (expr)) < 0
38854       || lunroll >= USHRT_MAX)
38855     {
38856       error_at (location, "%<#pragma GCC unroll%> requires an"
38857 		" assignment-expression that evaluates to a non-negative"
38858 		" integral constant less than %u", USHRT_MAX);
38859       unroll = 0;
38860     }
38861   else
38862     {
38863       unroll = (unsigned short)lunroll;
38864       if (unroll == 0)
38865 	unroll = 1;
38866     }
38867   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38868   return unroll;
38869 }
38870 
38871 /* Normal parsing of a pragma token.  Here we can (and must) use the
38872    regular lexer.  */
38873 
38874 static bool
cp_parser_pragma(cp_parser * parser,enum pragma_context context,bool * if_p)38875 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38876 {
38877   cp_token *pragma_tok;
38878   unsigned int id;
38879   tree stmt;
38880   bool ret;
38881 
38882   pragma_tok = cp_lexer_consume_token (parser->lexer);
38883   gcc_assert (pragma_tok->type == CPP_PRAGMA);
38884   parser->lexer->in_pragma = true;
38885 
38886   id = cp_parser_pragma_kind (pragma_tok);
38887   if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38888     cp_ensure_no_omp_declare_simd (parser);
38889   switch (id)
38890     {
38891     case PRAGMA_GCC_PCH_PREPROCESS:
38892       error_at (pragma_tok->location,
38893 		"%<#pragma GCC pch_preprocess%> must be first");
38894       break;
38895 
38896     case PRAGMA_OMP_BARRIER:
38897       switch (context)
38898 	{
38899 	case pragma_compound:
38900 	  cp_parser_omp_barrier (parser, pragma_tok);
38901 	  return false;
38902 	case pragma_stmt:
38903 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38904 		    "used in compound statements", "omp barrier");
38905 	  break;
38906 	default:
38907 	  goto bad_stmt;
38908 	}
38909       break;
38910 
38911     case PRAGMA_OMP_FLUSH:
38912       switch (context)
38913 	{
38914 	case pragma_compound:
38915 	  cp_parser_omp_flush (parser, pragma_tok);
38916 	  return false;
38917 	case pragma_stmt:
38918 	  error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38919 		    "used in compound statements", "omp flush");
38920 	  break;
38921 	default:
38922 	  goto bad_stmt;
38923 	}
38924       break;
38925 
38926     case PRAGMA_OMP_TASKWAIT:
38927       switch (context)
38928 	{
38929 	case pragma_compound:
38930 	  cp_parser_omp_taskwait (parser, pragma_tok);
38931 	  return false;
38932 	case pragma_stmt:
38933 	  error_at (pragma_tok->location,
38934 		    "%<#pragma %s%> may only be used in compound statements",
38935 		    "omp taskwait");
38936 	  break;
38937 	default:
38938 	  goto bad_stmt;
38939 	}
38940       break;
38941 
38942     case PRAGMA_OMP_TASKYIELD:
38943       switch (context)
38944 	{
38945 	case pragma_compound:
38946 	  cp_parser_omp_taskyield (parser, pragma_tok);
38947 	  return false;
38948 	case pragma_stmt:
38949 	  error_at (pragma_tok->location,
38950 		    "%<#pragma %s%> may only be used in compound statements",
38951 		    "omp taskyield");
38952 	  break;
38953 	default:
38954 	  goto bad_stmt;
38955 	}
38956       break;
38957 
38958     case PRAGMA_OMP_CANCEL:
38959       switch (context)
38960 	{
38961 	case pragma_compound:
38962 	  cp_parser_omp_cancel (parser, pragma_tok);
38963 	  return false;
38964 	case pragma_stmt:
38965 	  error_at (pragma_tok->location,
38966 		    "%<#pragma %s%> may only be used in compound statements",
38967 		    "omp cancel");
38968 	  break;
38969 	default:
38970 	  goto bad_stmt;
38971 	}
38972       break;
38973 
38974     case PRAGMA_OMP_CANCELLATION_POINT:
38975       cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38976       return false;
38977 
38978     case PRAGMA_OMP_THREADPRIVATE:
38979       cp_parser_omp_threadprivate (parser, pragma_tok);
38980       return false;
38981 
38982     case PRAGMA_OMP_DECLARE:
38983       return cp_parser_omp_declare (parser, pragma_tok, context);
38984 
38985     case PRAGMA_OACC_DECLARE:
38986       cp_parser_oacc_declare (parser, pragma_tok);
38987       return false;
38988 
38989     case PRAGMA_OACC_ENTER_DATA:
38990       if (context == pragma_stmt)
38991 	{
38992 	  error_at (pragma_tok->location,
38993 		    "%<#pragma %s%> may only be used in compound statements",
38994 		    "acc enter data");
38995 	  break;
38996 	}
38997       else if (context != pragma_compound)
38998 	goto bad_stmt;
38999       cp_parser_omp_construct (parser, pragma_tok, if_p);
39000       return true;
39001 
39002     case PRAGMA_OACC_EXIT_DATA:
39003       if (context == pragma_stmt)
39004 	{
39005 	  error_at (pragma_tok->location,
39006 		    "%<#pragma %s%> may only be used in compound statements",
39007 		    "acc exit data");
39008 	  break;
39009 	}
39010       else if (context != pragma_compound)
39011 	goto bad_stmt;
39012       cp_parser_omp_construct (parser, pragma_tok, if_p);
39013       return true;
39014 
39015     case PRAGMA_OACC_ROUTINE:
39016       if (context != pragma_external)
39017 	{
39018 	  error_at (pragma_tok->location,
39019 		    "%<#pragma acc routine%> must be at file scope");
39020 	  break;
39021 	}
39022       cp_parser_oacc_routine (parser, pragma_tok, context);
39023       return false;
39024 
39025     case PRAGMA_OACC_UPDATE:
39026       if (context == pragma_stmt)
39027 	{
39028 	  error_at (pragma_tok->location,
39029 		    "%<#pragma %s%> may only be used in compound statements",
39030 		    "acc update");
39031 	  break;
39032 	}
39033       else if (context != pragma_compound)
39034 	goto bad_stmt;
39035       cp_parser_omp_construct (parser, pragma_tok, if_p);
39036       return true;
39037 
39038     case PRAGMA_OACC_WAIT:
39039       if (context == pragma_stmt)
39040 	{
39041 	  error_at (pragma_tok->location,
39042 		    "%<#pragma %s%> may only be used in compound statements",
39043 		    "acc wait");
39044 	  break;
39045 	}
39046       else if (context != pragma_compound)
39047 	goto bad_stmt;
39048       cp_parser_omp_construct (parser, pragma_tok, if_p);
39049       return true;
39050 
39051     case PRAGMA_OACC_ATOMIC:
39052     case PRAGMA_OACC_CACHE:
39053     case PRAGMA_OACC_DATA:
39054     case PRAGMA_OACC_HOST_DATA:
39055     case PRAGMA_OACC_KERNELS:
39056     case PRAGMA_OACC_PARALLEL:
39057     case PRAGMA_OACC_LOOP:
39058     case PRAGMA_OMP_ATOMIC:
39059     case PRAGMA_OMP_CRITICAL:
39060     case PRAGMA_OMP_DISTRIBUTE:
39061     case PRAGMA_OMP_FOR:
39062     case PRAGMA_OMP_MASTER:
39063     case PRAGMA_OMP_PARALLEL:
39064     case PRAGMA_OMP_SECTIONS:
39065     case PRAGMA_OMP_SIMD:
39066     case PRAGMA_OMP_SINGLE:
39067     case PRAGMA_OMP_TASK:
39068     case PRAGMA_OMP_TASKGROUP:
39069     case PRAGMA_OMP_TASKLOOP:
39070     case PRAGMA_OMP_TEAMS:
39071       if (context != pragma_stmt && context != pragma_compound)
39072 	goto bad_stmt;
39073       stmt = push_omp_privatization_clauses (false);
39074       cp_parser_omp_construct (parser, pragma_tok, if_p);
39075       pop_omp_privatization_clauses (stmt);
39076       return true;
39077 
39078     case PRAGMA_OMP_ORDERED:
39079       if (context != pragma_stmt && context != pragma_compound)
39080 	goto bad_stmt;
39081       stmt = push_omp_privatization_clauses (false);
39082       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
39083       pop_omp_privatization_clauses (stmt);
39084       return ret;
39085 
39086     case PRAGMA_OMP_TARGET:
39087       if (context != pragma_stmt && context != pragma_compound)
39088 	goto bad_stmt;
39089       stmt = push_omp_privatization_clauses (false);
39090       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
39091       pop_omp_privatization_clauses (stmt);
39092       return ret;
39093 
39094     case PRAGMA_OMP_END_DECLARE_TARGET:
39095       cp_parser_omp_end_declare_target (parser, pragma_tok);
39096       return false;
39097 
39098     case PRAGMA_OMP_SECTION:
39099       error_at (pragma_tok->location,
39100 		"%<#pragma omp section%> may only be used in "
39101 		"%<#pragma omp sections%> construct");
39102       break;
39103 
39104     case PRAGMA_IVDEP:
39105       {
39106 	if (context == pragma_external)
39107 	  {
39108 	    error_at (pragma_tok->location,
39109 		      "%<#pragma GCC ivdep%> must be inside a function");
39110 	    break;
39111 	  }
39112 	const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39113 	unsigned short unroll;
39114 	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39115 	if (tok->type == CPP_PRAGMA
39116 	    && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39117 	  {
39118 	    tok = cp_lexer_consume_token (parser->lexer);
39119 	    unroll = cp_parser_pragma_unroll (parser, tok);
39120 	    tok = cp_lexer_peek_token (the_parser->lexer);
39121 	  }
39122 	else
39123 	  unroll = 0;
39124 	if (tok->type != CPP_KEYWORD
39125 	    || (tok->keyword != RID_FOR
39126 		&& tok->keyword != RID_WHILE
39127 		&& tok->keyword != RID_DO))
39128 	  {
39129 	    cp_parser_error (parser, "for, while or do statement expected");
39130 	    return false;
39131 	  }
39132 	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39133 	return true;
39134       }
39135 
39136     case PRAGMA_UNROLL:
39137       {
39138 	if (context == pragma_external)
39139 	  {
39140 	    error_at (pragma_tok->location,
39141 		      "%<#pragma GCC unroll%> must be inside a function");
39142 	    break;
39143 	  }
39144 	const unsigned short unroll
39145 	  = cp_parser_pragma_unroll (parser, pragma_tok);
39146 	bool ivdep;
39147 	cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39148 	if (tok->type == CPP_PRAGMA
39149 	    && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39150 	  {
39151 	    tok = cp_lexer_consume_token (parser->lexer);
39152 	    ivdep = cp_parser_pragma_ivdep (parser, tok);
39153 	    tok = cp_lexer_peek_token (the_parser->lexer);
39154 	  }
39155 	else
39156 	  ivdep = false;
39157 	if (tok->type != CPP_KEYWORD
39158 	    || (tok->keyword != RID_FOR
39159 		&& tok->keyword != RID_WHILE
39160 		&& tok->keyword != RID_DO))
39161 	  {
39162 	    cp_parser_error (parser, "for, while or do statement expected");
39163 	    return false;
39164 	  }
39165 	cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39166 	return true;
39167       }
39168 
39169     default:
39170       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39171       c_invoke_pragma_handler (id);
39172       break;
39173 
39174     bad_stmt:
39175       cp_parser_error (parser, "expected declaration specifiers");
39176       break;
39177     }
39178 
39179   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39180   return false;
39181 }
39182 
39183 /* The interface the pragma parsers have to the lexer.  */
39184 
39185 enum cpp_ttype
pragma_lex(tree * value,location_t * loc)39186 pragma_lex (tree *value, location_t *loc)
39187 {
39188   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39189   enum cpp_ttype ret = tok->type;
39190 
39191   *value = tok->u.value;
39192   if (loc)
39193     *loc = tok->location;
39194 
39195   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39196     ret = CPP_EOF;
39197   else if (ret == CPP_STRING)
39198     *value = cp_parser_string_literal (the_parser, false, false);
39199   else
39200     {
39201       if (ret == CPP_KEYWORD)
39202 	ret = CPP_NAME;
39203       cp_lexer_consume_token (the_parser->lexer);
39204     }
39205 
39206   return ret;
39207 }
39208 
39209 
39210 /* External interface.  */
39211 
39212 /* Parse one entire translation unit.  */
39213 
39214 void
c_parse_file(void)39215 c_parse_file (void)
39216 {
39217   static bool already_called = false;
39218 
39219   if (already_called)
39220     fatal_error (input_location,
39221 		 "inter-module optimizations not implemented for C++");
39222   already_called = true;
39223 
39224   the_parser = cp_parser_new ();
39225   push_deferring_access_checks (flag_access_control
39226 				? dk_no_deferred : dk_no_check);
39227   cp_parser_translation_unit (the_parser);
39228   the_parser = NULL;
39229 }
39230 
39231 /* Create an identifier for a generic parameter type (a synthesized
39232    template parameter implied by `auto' or a concept identifier). */
39233 
39234 static GTY(()) int generic_parm_count;
39235 static tree
make_generic_type_name()39236 make_generic_type_name ()
39237 {
39238   char buf[32];
39239   sprintf (buf, "auto:%d", ++generic_parm_count);
39240   return get_identifier (buf);
39241 }
39242 
39243 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39244    (creating a new template parameter list if necessary).  Returns the newly
39245    created template type parm.  */
39246 
39247 static tree
synthesize_implicit_template_parm(cp_parser * parser,tree constr)39248 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
39249 {
39250   gcc_assert (current_binding_level->kind == sk_function_parms);
39251 
39252    /* Before committing to modifying any scope, if we're in an
39253       implicit template scope, and we're trying to synthesize a
39254       constrained parameter, try to find a previous parameter with
39255       the same name.  This is the same-type rule for abbreviated
39256       function templates.
39257 
39258       NOTE: We can generate implicit parameters when tentatively
39259       parsing a nested name specifier, only to reject that parse
39260       later. However, matching the same template-id as part of a
39261       direct-declarator should generate an identical template
39262       parameter, so this rule will merge them. */
39263   if (parser->implicit_template_scope && constr)
39264     {
39265       tree t = parser->implicit_template_parms;
39266       while (t)
39267         {
39268           if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39269 	    {
39270 	      tree d = TREE_VALUE (t);
39271 	      if (TREE_CODE (d) == PARM_DECL)
39272 		/* Return the TEMPLATE_PARM_INDEX.  */
39273 		d = DECL_INITIAL (d);
39274 	      return d;
39275 	    }
39276           t = TREE_CHAIN (t);
39277         }
39278     }
39279 
39280   /* We are either continuing a function template that already contains implicit
39281      template parameters, creating a new fully-implicit function template, or
39282      extending an existing explicit function template with implicit template
39283      parameters.  */
39284 
39285   cp_binding_level *const entry_scope = current_binding_level;
39286 
39287   bool become_template = false;
39288   cp_binding_level *parent_scope = 0;
39289 
39290   if (parser->implicit_template_scope)
39291     {
39292       gcc_assert (parser->implicit_template_parms);
39293 
39294       current_binding_level = parser->implicit_template_scope;
39295     }
39296   else
39297     {
39298       /* Roll back to the existing template parameter scope (in the case of
39299 	 extending an explicit function template) or introduce a new template
39300 	 parameter scope ahead of the function parameter scope (or class scope
39301 	 in the case of out-of-line member definitions).  The function scope is
39302 	 added back after template parameter synthesis below.  */
39303 
39304       cp_binding_level *scope = entry_scope;
39305 
39306       while (scope->kind == sk_function_parms)
39307 	{
39308 	  parent_scope = scope;
39309 	  scope = scope->level_chain;
39310 	}
39311       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39312 	{
39313 	  /* If not defining a class, then any class scope is a scope level in
39314 	     an out-of-line member definition.  In this case simply wind back
39315 	     beyond the first such scope to inject the template parameter list.
39316 	     Otherwise wind back to the class being defined.  The latter can
39317 	     occur in class member friend declarations such as:
39318 
39319 	       class A {
39320 		 void foo (auto);
39321 	       };
39322 	       class B {
39323 		 friend void A::foo (auto);
39324 	       };
39325 
39326 	    The template parameter list synthesized for the friend declaration
39327 	    must be injected in the scope of 'B'.  This can also occur in
39328 	    erroneous cases such as:
39329 
39330 	       struct A {
39331 	         struct B {
39332 		   void foo (auto);
39333 		 };
39334 		 void B::foo (auto) {}
39335 	       };
39336 
39337 	    Here the attempted definition of 'B::foo' within 'A' is ill-formed
39338 	    but, nevertheless, the template parameter list synthesized for the
39339 	    declarator should be injected into the scope of 'A' as if the
39340 	    ill-formed template was specified explicitly.  */
39341 
39342 	  while (scope->kind == sk_class && !scope->defining_class_p)
39343 	    {
39344 	      parent_scope = scope;
39345 	      scope = scope->level_chain;
39346 	    }
39347 	}
39348 
39349       current_binding_level = scope;
39350 
39351       if (scope->kind != sk_template_parms
39352 	  || !function_being_declared_is_template_p (parser))
39353 	{
39354 	  /* Introduce a new template parameter list for implicit template
39355 	     parameters.  */
39356 
39357 	  become_template = true;
39358 
39359 	  parser->implicit_template_scope
39360 	      = begin_scope (sk_template_parms, NULL);
39361 
39362 	  ++processing_template_decl;
39363 
39364 	  parser->fully_implicit_function_template_p = true;
39365 	  ++parser->num_template_parameter_lists;
39366 	}
39367       else
39368 	{
39369 	  /* Synthesize implicit template parameters at the end of the explicit
39370 	     template parameter list.  */
39371 
39372 	  gcc_assert (current_template_parms);
39373 
39374 	  parser->implicit_template_scope = scope;
39375 
39376 	  tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39377 	  parser->implicit_template_parms
39378 	    = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39379 	}
39380     }
39381 
39382   /* Synthesize a new template parameter and track the current template
39383      parameter chain with implicit_template_parms.  */
39384 
39385   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39386   tree synth_id = make_generic_type_name ();
39387   tree synth_tmpl_parm;
39388   bool non_type = false;
39389 
39390   if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39391     synth_tmpl_parm
39392       = finish_template_type_parm (class_type_node, synth_id);
39393   else if (TREE_CODE (proto) == TEMPLATE_DECL)
39394     synth_tmpl_parm
39395       = finish_constrained_template_template_parm (proto, synth_id);
39396   else
39397     {
39398       synth_tmpl_parm = copy_decl (proto);
39399       DECL_NAME (synth_tmpl_parm) = synth_id;
39400       non_type = true;
39401     }
39402 
39403   // Attach the constraint to the parm before processing.
39404   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39405   TREE_TYPE (node) = constr;
39406   tree new_parm
39407     = process_template_parm (parser->implicit_template_parms,
39408 			     input_location,
39409 			     node,
39410 			     /*non_type=*/non_type,
39411 			     /*param_pack=*/false);
39412 
39413   // Chain the new parameter to the list of implicit parameters.
39414   if (parser->implicit_template_parms)
39415     parser->implicit_template_parms
39416       = TREE_CHAIN (parser->implicit_template_parms);
39417   else
39418     parser->implicit_template_parms = new_parm;
39419 
39420   tree new_decl = get_local_decls ();
39421   if (non_type)
39422     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
39423     new_decl = DECL_INITIAL (new_decl);
39424 
39425   /* If creating a fully implicit function template, start the new implicit
39426      template parameter list with this synthesized type, otherwise grow the
39427      current template parameter list.  */
39428 
39429   if (become_template)
39430     {
39431       parent_scope->level_chain = current_binding_level;
39432 
39433       tree new_parms = make_tree_vec (1);
39434       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39435       current_template_parms = tree_cons (size_int (processing_template_decl),
39436 					  new_parms, current_template_parms);
39437     }
39438   else
39439     {
39440       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39441       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39442       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39443       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39444     }
39445 
39446   // If the new parameter was constrained, we need to add that to the
39447   // constraints in the template parameter list.
39448   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39449     {
39450       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39451       reqs = conjoin_constraints (reqs, req);
39452       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39453     }
39454 
39455   current_binding_level = entry_scope;
39456 
39457   return new_decl;
39458 }
39459 
39460 /* Finish the declaration of a fully implicit function template.  Such a
39461    template has no explicit template parameter list so has not been through the
39462    normal template head and tail processing.  synthesize_implicit_template_parm
39463    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
39464    provided if the declaration is a class member such that its template
39465    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
39466    form is returned.  Otherwise NULL_TREE is returned. */
39467 
39468 static tree
finish_fully_implicit_template(cp_parser * parser,tree member_decl_opt)39469 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39470 {
39471   gcc_assert (parser->fully_implicit_function_template_p);
39472 
39473   if (member_decl_opt && member_decl_opt != error_mark_node
39474       && DECL_VIRTUAL_P (member_decl_opt))
39475     {
39476       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39477 		"implicit templates may not be %<virtual%>");
39478       DECL_VIRTUAL_P (member_decl_opt) = false;
39479     }
39480 
39481   if (member_decl_opt)
39482     member_decl_opt = finish_member_template_decl (member_decl_opt);
39483   end_template_decl ();
39484 
39485   parser->fully_implicit_function_template_p = false;
39486   parser->implicit_template_parms = 0;
39487   parser->implicit_template_scope = 0;
39488   --parser->num_template_parameter_lists;
39489 
39490   return member_decl_opt;
39491 }
39492 
39493 /* Like finish_fully_implicit_template, but to be used in error
39494    recovery, rearranging scopes so that we restore the state we had
39495    before synthesize_implicit_template_parm inserted the implement
39496    template parms scope.  */
39497 
39498 static void
abort_fully_implicit_template(cp_parser * parser)39499 abort_fully_implicit_template (cp_parser *parser)
39500 {
39501   cp_binding_level *return_to_scope = current_binding_level;
39502 
39503   if (parser->implicit_template_scope
39504       && return_to_scope != parser->implicit_template_scope)
39505     {
39506       cp_binding_level *child = return_to_scope;
39507       for (cp_binding_level *scope = child->level_chain;
39508 	   scope != parser->implicit_template_scope;
39509 	   scope = child->level_chain)
39510 	child = scope;
39511       child->level_chain = parser->implicit_template_scope->level_chain;
39512       parser->implicit_template_scope->level_chain = return_to_scope;
39513       current_binding_level = parser->implicit_template_scope;
39514     }
39515   else
39516     return_to_scope = return_to_scope->level_chain;
39517 
39518   finish_fully_implicit_template (parser, NULL);
39519 
39520   gcc_assert (current_binding_level == return_to_scope);
39521 }
39522 
39523 /* Helper function for diagnostics that have complained about things
39524    being used with 'extern "C"' linkage.
39525 
39526    Attempt to issue a note showing where the 'extern "C"' linkage began.  */
39527 
39528 void
maybe_show_extern_c_location(void)39529 maybe_show_extern_c_location (void)
39530 {
39531   if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39532     inform (the_parser->innermost_linkage_specification_location,
39533 	    "%<extern \"C\"%> linkage started here");
39534 }
39535 
39536 #include "gt-cp-parser.h"
39537