Update GCC80 to version 8.3
[dragonfly.git] / contrib / gcc-8.0 / gcc / cp / parser.c
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 \f
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)
195     : parser (parser),
196       saved (parser->in_type_id_in_expr_p)
197   { parser->in_type_id_in_expr_p = set; }
198   ~type_id_in_expr_sentinel ()
199   { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes.  */
203
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
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
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
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
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
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
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
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
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
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
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
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
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
590 debug (cp_parser &ref)
591 {
592   cp_debug_parser (stderr, &ref);
593 }
594
595 DEBUG_FUNCTION void
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 *
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 *
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 *
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
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
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
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 *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733   return pos;
734 }
735
736 static inline void
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
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 *
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
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
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
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
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 *
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
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
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
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
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
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
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
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
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
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
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
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 *
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 *
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
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
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
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
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
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;
1216   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1217   {
1218     len = lexer->saved_tokens.length ();
1219     cp_lexer_save_tokens (lexer);
1220   }
1221   void rollback ()
1222   {
1223     cp_lexer_rollback_tokens (lexer);
1224     commit = false;
1225   }
1226   ~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
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
1291 debug (cp_token &ref)
1292 {
1293   cp_lexer_print_token (stderr, &ref);
1294   fprintf (stderr, "\n");
1295 }
1296
1297 DEBUG_FUNCTION void
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
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
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 *
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
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
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
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 \f
1394 /* Decl-specifiers.  */
1395
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1397
1398 static void
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 *
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 
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
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 *
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
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
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
2650 /* Concept-related syntactic transformations */
2651
2652 static tree cp_parser_maybe_concept_name       (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2654
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2657 //
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2660 {
2661   ++cp_unevaluated_operand;
2662   ++c_inhibit_evaluation_warnings;
2663 }
2664
2665 cp_unevaluated::~cp_unevaluated ()
2666 {
2667   --c_inhibit_evaluation_warnings;
2668   --cp_unevaluated_operand;
2669 }
2670
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2673
2674 /* Returns nonzero if we are parsing tentatively.  */
2675
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2678 {
2679   return parser->context->next != NULL;
2680 }
2681
2682 /* Returns nonzero if TOKEN is a string literal.  */
2683
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2686 {
2687   return (token->type == CPP_STRING ||
2688           token->type == CPP_STRING16 ||
2689           token->type == CPP_STRING32 ||
2690           token->type == CPP_WSTRING ||
2691           token->type == CPP_UTF8STRING);
2692 }
2693
2694 /* Returns nonzero if TOKEN is a string literal
2695    of a user-defined string literal.  */
2696
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2699 {
2700   return (cp_parser_is_pure_string_literal (token) ||
2701           token->type == CPP_STRING_USERDEF ||
2702           token->type == CPP_STRING16_USERDEF ||
2703           token->type == CPP_STRING32_USERDEF ||
2704           token->type == CPP_WSTRING_USERDEF ||
2705           token->type == CPP_UTF8STRING_USERDEF);
2706 }
2707
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2709
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2712 {
2713   return token->keyword == keyword;
2714 }
2715
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717    PRAGMA_NONE.  */
2718
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2721 {
2722   if (token->type != CPP_PRAGMA)
2723     return PRAGMA_NONE;
2724   /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
2725   return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 }
2727
2728 /* Helper function for cp_parser_error.
2729    Having peeked a token of kind TOK1_KIND that might signify
2730    a conflict marker, peek successor tokens to determine
2731    if we actually do have a conflict marker.
2732    Specifically, we consider a run of 7 '<', '=' or '>' characters
2733    at the start of a line as a conflict marker.
2734    These come through the lexer as three pairs and a single,
2735    e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736    If it returns true, *OUT_LOC is written to with the location/range
2737    of the marker.  */
2738
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741                                location_t *out_loc)
2742 {
2743   cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744   if (token2->type != tok1_kind)
2745     return false;
2746   cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747   if (token3->type != tok1_kind)
2748     return false;
2749   cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750   if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751     return false;
2752
2753   /* It must be at the start of the line.  */
2754   location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755   if (LOCATION_COLUMN (start_loc) != 1)
2756     return false;
2757
2758   /* We have a conflict marker.  Construct a location of the form:
2759        <<<<<<<
2760        ^~~~~~~
2761      with start == caret, finishing at the end of the marker.  */
2762   location_t finish_loc = get_finish (token4->location);
2763   *out_loc = make_location (start_loc, start_loc, finish_loc);
2764
2765   return true;
2766 }
2767
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769    RT_CLOSE_PAREN.  */
2770
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2773 {
2774   switch (token_desc)
2775     {
2776     default:
2777       gcc_unreachable ();
2778       return "";
2779     case RT_CLOSE_BRACE:
2780       return "{";
2781     case RT_CLOSE_PAREN:
2782       return "(";
2783     }
2784 }
2785
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787    enum cpp_ttype, returning CPP_EOF if there is no good conversion.  */
2788
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2791 {
2792   switch (token_desc)
2793     {
2794     case RT_SEMICOLON:
2795       return CPP_SEMICOLON;
2796     case RT_OPEN_PAREN:
2797       return CPP_OPEN_PAREN;
2798     case RT_CLOSE_BRACE:
2799       return CPP_CLOSE_BRACE;
2800     case RT_OPEN_BRACE:
2801       return CPP_OPEN_BRACE;
2802     case RT_CLOSE_SQUARE:
2803       return CPP_CLOSE_SQUARE;
2804     case RT_OPEN_SQUARE:
2805       return CPP_OPEN_SQUARE;
2806     case RT_COMMA:
2807       return CPP_COMMA;
2808     case RT_COLON:
2809       return CPP_COLON;
2810     case RT_CLOSE_PAREN:
2811       return CPP_CLOSE_PAREN;
2812
2813     default:
2814       /* Use CPP_EOF as a "no completions possible" code.  */
2815       return CPP_EOF;
2816     }
2817 }
2818
2819
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2821
2822    Issue a diagnostic of the form
2823       FILE:LINE: MESSAGE before TOKEN
2824    where TOKEN is the next token in the input stream.  MESSAGE
2825    (specified by the caller) is usually of the form "expected
2826    OTHER-TOKEN".
2827
2828    This bypasses the check for tentative passing, and potentially
2829    adds material needed by cp_parser_required_error.
2830
2831    If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832    suggesting insertion of the missing token.
2833
2834    Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835    have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836    location.  */
2837
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840                    required_token missing_token_desc,
2841                    location_t matching_location)
2842 {
2843   cp_token *token = cp_lexer_peek_token (parser->lexer);
2844   /* This diagnostic makes more sense if it is tagged to the line
2845      of the token we just peeked at.  */
2846   cp_lexer_set_source_position_from_token (token);
2847
2848   if (token->type == CPP_PRAGMA)
2849     {
2850       error_at (token->location,
2851                 "%<#pragma%> is not allowed here");
2852       cp_parser_skip_to_pragma_eol (parser, token);
2853       return;
2854     }
2855
2856   /* If this is actually a conflict marker, report it as such.  */
2857   if (token->type == CPP_LSHIFT
2858       || token->type == CPP_RSHIFT
2859       || token->type == CPP_EQ_EQ)
2860     {
2861       location_t loc;
2862       if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2863         {
2864           error_at (loc, "version control conflict marker in file");
2865           return;
2866         }
2867     }
2868
2869   gcc_rich_location richloc (input_location);
2870
2871   bool added_matching_location = false;
2872
2873   if (missing_token_desc != RT_NONE)
2874     {
2875       /* Potentially supply a fix-it hint, suggesting to add the
2876          missing token immediately after the *previous* token.
2877          This may move the primary location within richloc.  */
2878       enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2879       location_t prev_token_loc
2880         = cp_lexer_previous_token (parser->lexer)->location;
2881       maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2882
2883       /* If matching_location != UNKNOWN_LOCATION, highlight it.
2884          Attempt to consolidate diagnostics by printing it as a
2885         secondary range within the main diagnostic.  */
2886       if (matching_location != UNKNOWN_LOCATION)
2887         added_matching_location
2888           = richloc.add_location_if_nearby (matching_location);
2889     }
2890
2891   /* Actually emit the error.  */
2892   c_parse_error (gmsgid,
2893                  /* Because c_parser_error does not understand
2894                     CPP_KEYWORD, keywords are treated like
2895                     identifiers.  */
2896                  (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2897                  token->u.value, token->flags, &richloc);
2898
2899   if (missing_token_desc != RT_NONE)
2900     {
2901       /* If we weren't able to consolidate matching_location, then
2902          print it as a secondary diagnostic.  */
2903       if (matching_location != UNKNOWN_LOCATION
2904           && !added_matching_location)
2905         inform (matching_location, "to match this %qs",
2906                 get_matching_symbol (missing_token_desc));
2907     }
2908 }
2909
2910 /* If not parsing tentatively, issue a diagnostic of the form
2911       FILE:LINE: MESSAGE before TOKEN
2912    where TOKEN is the next token in the input stream.  MESSAGE
2913    (specified by the caller) is usually of the form "expected
2914    OTHER-TOKEN".  */
2915
2916 static void
2917 cp_parser_error (cp_parser* parser, const char* gmsgid)
2918 {
2919   if (!cp_parser_simulate_error (parser))
2920     cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2921 }
2922
2923 /* Issue an error about name-lookup failing.  NAME is the
2924    IDENTIFIER_NODE DECL is the result of
2925    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2926    the thing that we hoped to find.  */
2927
2928 static void
2929 cp_parser_name_lookup_error (cp_parser* parser,
2930                              tree name,
2931                              tree decl,
2932                              name_lookup_error desired,
2933                              location_t location)
2934 {
2935   /* If name lookup completely failed, tell the user that NAME was not
2936      declared.  */
2937   if (decl == error_mark_node)
2938     {
2939       if (parser->scope && parser->scope != global_namespace)
2940         error_at (location, "%<%E::%E%> has not been declared",
2941                   parser->scope, name);
2942       else if (parser->scope == global_namespace)
2943         error_at (location, "%<::%E%> has not been declared", name);
2944       else if (parser->object_scope
2945                && !CLASS_TYPE_P (parser->object_scope))
2946         error_at (location, "request for member %qE in non-class type %qT",
2947                   name, parser->object_scope);
2948       else if (parser->object_scope)
2949         error_at (location, "%<%T::%E%> has not been declared",
2950                   parser->object_scope, name);
2951       else
2952         error_at (location, "%qE has not been declared", name);
2953     }
2954   else if (parser->scope && parser->scope != global_namespace)
2955     {
2956       switch (desired)
2957         {
2958           case NLE_TYPE:
2959             error_at (location, "%<%E::%E%> is not a type",
2960                                 parser->scope, name);
2961             break;
2962           case NLE_CXX98:
2963             error_at (location, "%<%E::%E%> is not a class or namespace",
2964                                 parser->scope, name);
2965             break;
2966           case NLE_NOT_CXX98:
2967             error_at (location,
2968                       "%<%E::%E%> is not a class, namespace, or enumeration",
2969                       parser->scope, name);
2970             break;
2971           default:
2972             gcc_unreachable ();
2973             
2974         }
2975     }
2976   else if (parser->scope == global_namespace)
2977     {
2978       switch (desired)
2979         {
2980           case NLE_TYPE:
2981             error_at (location, "%<::%E%> is not a type", name);
2982             break;
2983           case NLE_CXX98:
2984             error_at (location, "%<::%E%> is not a class or namespace", name);
2985             break;
2986           case NLE_NOT_CXX98:
2987             error_at (location,
2988                       "%<::%E%> is not a class, namespace, or enumeration",
2989                       name);
2990             break;
2991           default:
2992             gcc_unreachable ();
2993         }
2994     }
2995   else
2996     {
2997       switch (desired)
2998         {
2999           case NLE_TYPE:
3000             error_at (location, "%qE is not a type", name);
3001             break;
3002           case NLE_CXX98:
3003             error_at (location, "%qE is not a class or namespace", name);
3004             break;
3005           case NLE_NOT_CXX98:
3006             error_at (location,
3007                       "%qE is not a class, namespace, or enumeration", name);
3008             break;
3009           default:
3010             gcc_unreachable ();
3011         }
3012     }
3013 }
3014
3015 /* If we are parsing tentatively, remember that an error has occurred
3016    during this tentative parse.  Returns true if the error was
3017    simulated; false if a message should be issued by the caller.  */
3018
3019 static bool
3020 cp_parser_simulate_error (cp_parser* parser)
3021 {
3022   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3023     {
3024       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3025       return true;
3026     }
3027   return false;
3028 }
3029
3030 /* This function is called when a type is defined.  If type
3031    definitions are forbidden at this point, an error message is
3032    issued.  */
3033
3034 static bool
3035 cp_parser_check_type_definition (cp_parser* parser)
3036 {
3037   /* If types are forbidden here, issue a message.  */
3038   if (parser->type_definition_forbidden_message)
3039     {
3040       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3041          in the message need to be interpreted.  */
3042       error (parser->type_definition_forbidden_message);
3043       return false;
3044     }
3045   return true;
3046 }
3047
3048 /* This function is called when the DECLARATOR is processed.  The TYPE
3049    was a type defined in the decl-specifiers.  If it is invalid to
3050    define a type in the decl-specifiers for DECLARATOR, an error is
3051    issued. TYPE_LOCATION is the location of TYPE and is used
3052    for error reporting.  */
3053
3054 static void
3055 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3056                                                tree type, location_t type_location)
3057 {
3058   /* [dcl.fct] forbids type definitions in return types.
3059      Unfortunately, it's not easy to know whether or not we are
3060      processing a return type until after the fact.  */
3061   while (declarator
3062          && (declarator->kind == cdk_pointer
3063              || declarator->kind == cdk_reference
3064              || declarator->kind == cdk_ptrmem))
3065     declarator = declarator->declarator;
3066   if (declarator
3067       && declarator->kind == cdk_function)
3068     {
3069       error_at (type_location,
3070                 "new types may not be defined in a return type");
3071       inform (type_location, 
3072               "(perhaps a semicolon is missing after the definition of %qT)",
3073               type);
3074     }
3075 }
3076
3077 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3078    "<" in any valid C++ program.  If the next token is indeed "<",
3079    issue a message warning the user about what appears to be an
3080    invalid attempt to form a template-id. LOCATION is the location
3081    of the type-specifier (TYPE) */
3082
3083 static void
3084 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3085                                          tree type,
3086                                          enum tag_types tag_type,
3087                                          location_t location)
3088 {
3089   cp_token_position start = 0;
3090
3091   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3092     {
3093       if (TREE_CODE (type) == TYPE_DECL)
3094         type = TREE_TYPE (type);
3095       if (TYPE_P (type) && !template_placeholder_p (type))
3096         error_at (location, "%qT is not a template", type);
3097       else if (identifier_p (type))
3098         {
3099           if (tag_type != none_type)
3100             error_at (location, "%qE is not a class template", type);
3101           else
3102             error_at (location, "%qE is not a template", type);
3103         }
3104       else
3105         error_at (location, "invalid template-id");
3106       /* Remember the location of the invalid "<".  */
3107       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3108         start = cp_lexer_token_position (parser->lexer, true);
3109       /* Consume the "<".  */
3110       cp_lexer_consume_token (parser->lexer);
3111       /* Parse the template arguments.  */
3112       cp_parser_enclosed_template_argument_list (parser);
3113       /* Permanently remove the invalid template arguments so that
3114          this error message is not issued again.  */
3115       if (start)
3116         cp_lexer_purge_tokens_after (parser->lexer, start);
3117     }
3118 }
3119
3120 /* If parsing an integral constant-expression, issue an error message
3121    about the fact that THING appeared and return true.  Otherwise,
3122    return false.  In either case, set
3123    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
3124
3125 static bool
3126 cp_parser_non_integral_constant_expression (cp_parser  *parser,
3127                                             non_integral_constant thing)
3128 {
3129   parser->non_integral_constant_expression_p = true;
3130   if (parser->integral_constant_expression_p)
3131     {
3132       if (!parser->allow_non_integral_constant_expression_p)
3133         {
3134           const char *msg = NULL;
3135           switch (thing)
3136             {
3137               case NIC_FLOAT:
3138                 pedwarn (input_location, OPT_Wpedantic,
3139                          "ISO C++ forbids using a floating-point literal "
3140                          "in a constant-expression");
3141                 return true;
3142               case NIC_CAST:
3143                 error ("a cast to a type other than an integral or "
3144                        "enumeration type cannot appear in a "
3145                        "constant-expression");
3146                 return true;
3147               case NIC_TYPEID:
3148                 error ("%<typeid%> operator "
3149                        "cannot appear in a constant-expression");
3150                 return true;
3151               case NIC_NCC:
3152                 error ("non-constant compound literals "
3153                        "cannot appear in a constant-expression");
3154                 return true;
3155               case NIC_FUNC_CALL:
3156                 error ("a function call "
3157                        "cannot appear in a constant-expression");
3158                 return true;
3159               case NIC_INC:
3160                 error ("an increment "
3161                        "cannot appear in a constant-expression");
3162                 return true;
3163               case NIC_DEC:
3164                 error ("an decrement "
3165                        "cannot appear in a constant-expression");
3166                 return true;
3167               case NIC_ARRAY_REF:
3168                 error ("an array reference "
3169                        "cannot appear in a constant-expression");
3170                 return true;
3171               case NIC_ADDR_LABEL:
3172                 error ("the address of a label "
3173                        "cannot appear in a constant-expression");
3174                 return true;
3175               case NIC_OVERLOADED:
3176                 error ("calls to overloaded operators "
3177                        "cannot appear in a constant-expression");
3178                 return true;
3179               case NIC_ASSIGNMENT:
3180                 error ("an assignment cannot appear in a constant-expression");
3181                 return true;
3182               case NIC_COMMA:
3183                 error ("a comma operator "
3184                        "cannot appear in a constant-expression");
3185                 return true;
3186               case NIC_CONSTRUCTOR:
3187                 error ("a call to a constructor "
3188                        "cannot appear in a constant-expression");
3189                 return true;
3190               case NIC_TRANSACTION:
3191                 error ("a transaction expression "
3192                        "cannot appear in a constant-expression");
3193                 return true;
3194               case NIC_THIS:
3195                 msg = "this";
3196                 break;
3197               case NIC_FUNC_NAME:
3198                 msg = "__FUNCTION__";
3199                 break;
3200               case NIC_PRETTY_FUNC:
3201                 msg = "__PRETTY_FUNCTION__";
3202                 break;
3203               case NIC_C99_FUNC:
3204                 msg = "__func__";
3205                 break;
3206               case NIC_VA_ARG:
3207                 msg = "va_arg";
3208                 break;
3209               case NIC_ARROW:
3210                 msg = "->";
3211                 break;
3212               case NIC_POINT:
3213                 msg = ".";
3214                 break;
3215               case NIC_STAR:
3216                 msg = "*";
3217                 break;
3218               case NIC_ADDR:
3219                 msg = "&";
3220                 break;
3221               case NIC_PREINCREMENT:
3222                 msg = "++";
3223                 break;
3224               case NIC_PREDECREMENT:
3225                 msg = "--";
3226                 break;
3227               case NIC_NEW:
3228                 msg = "new";
3229                 break;
3230               case NIC_DEL:
3231                 msg = "delete";
3232                 break;
3233               default:
3234                 gcc_unreachable ();
3235             }
3236           if (msg)
3237             error ("%qs cannot appear in a constant-expression", msg);
3238           return true;
3239         }
3240     }
3241   return false;
3242 }
3243
3244 /* Emit a diagnostic for an invalid type name.  This function commits
3245    to the current active tentative parse, if any.  (Otherwise, the
3246    problematic construct might be encountered again later, resulting
3247    in duplicate error messages.) LOCATION is the location of ID.  */
3248
3249 static void
3250 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3251                                       location_t location)
3252 {
3253   tree decl, ambiguous_decls;
3254   cp_parser_commit_to_tentative_parse (parser);
3255   /* Try to lookup the identifier.  */
3256   decl = cp_parser_lookup_name (parser, id, none_type,
3257                                 /*is_template=*/false,
3258                                 /*is_namespace=*/false,
3259                                 /*check_dependency=*/true,
3260                                 &ambiguous_decls, location);
3261   if (ambiguous_decls)
3262     /* If the lookup was ambiguous, an error will already have
3263        been issued.  */
3264     return;
3265   /* If the lookup found a template-name, it means that the user forgot
3266   to specify an argument list. Emit a useful error message.  */
3267   if (DECL_TYPE_TEMPLATE_P (decl))
3268     {
3269       error_at (location,
3270                 "invalid use of template-name %qE without an argument list",
3271                 decl);
3272       if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3273         inform (location, "class template argument deduction is only available "
3274                 "with -std=c++17 or -std=gnu++17");
3275       inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3276     }
3277   else if (TREE_CODE (id) == BIT_NOT_EXPR)
3278     error_at (location, "invalid use of destructor %qD as a type", id);
3279   else if (TREE_CODE (decl) == TYPE_DECL)
3280     /* Something like 'unsigned A a;'  */
3281     error_at (location, "invalid combination of multiple type-specifiers");
3282   else if (!parser->scope)
3283     {
3284       /* Issue an error message.  */
3285       name_hint hint;
3286       if (TREE_CODE (id) == IDENTIFIER_NODE)
3287         hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3288       if (hint)
3289         {
3290           gcc_rich_location richloc (location);
3291           richloc.add_fixit_replace (hint.suggestion ());
3292           error_at (&richloc,
3293                     "%qE does not name a type; did you mean %qs?",
3294                     id, hint.suggestion ());
3295         }
3296       else
3297         error_at (location, "%qE does not name a type", id);
3298       /* If we're in a template class, it's possible that the user was
3299          referring to a type from a base class.  For example:
3300
3301            template <typename T> struct A { typedef T X; };
3302            template <typename T> struct B : public A<T> { X x; };
3303
3304          The user should have said "typename A<T>::X".  */
3305       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3306         inform (location, "C++11 %<constexpr%> only available with "
3307                 "-std=c++11 or -std=gnu++11");
3308       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3309         inform (location, "C++11 %<noexcept%> only available with "
3310                 "-std=c++11 or -std=gnu++11");
3311       else if (cxx_dialect < cxx11
3312                && TREE_CODE (id) == IDENTIFIER_NODE
3313                && id_equal (id, "thread_local"))
3314         inform (location, "C++11 %<thread_local%> only available with "
3315                 "-std=c++11 or -std=gnu++11");
3316       else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3317         inform (location, "%<concept%> only available with -fconcepts");
3318       else if (processing_template_decl && current_class_type
3319                && TYPE_BINFO (current_class_type))
3320         {
3321           tree b;
3322
3323           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3324                b;
3325                b = TREE_CHAIN (b))
3326             {
3327               tree base_type = BINFO_TYPE (b);
3328               if (CLASS_TYPE_P (base_type)
3329                   && dependent_type_p (base_type))
3330                 {
3331                   tree field;
3332                   /* Go from a particular instantiation of the
3333                      template (which will have an empty TYPE_FIELDs),
3334                      to the main version.  */
3335                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3336                   for (field = TYPE_FIELDS (base_type);
3337                        field;
3338                        field = DECL_CHAIN (field))
3339                     if (TREE_CODE (field) == TYPE_DECL
3340                         && DECL_NAME (field) == id)
3341                       {
3342                         inform (location, 
3343                                 "(perhaps %<typename %T::%E%> was intended)",
3344                                 BINFO_TYPE (b), id);
3345                         break;
3346                       }
3347                   if (field)
3348                     break;
3349                 }
3350             }
3351         }
3352     }
3353   /* Here we diagnose qualified-ids where the scope is actually correct,
3354      but the identifier does not resolve to a valid type name.  */
3355   else if (parser->scope != error_mark_node)
3356     {
3357       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3358         {
3359           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3360             error_at (location_of (id),
3361                       "%qE in namespace %qE does not name a template type",
3362                       id, parser->scope);
3363           else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3364             error_at (location_of (id),
3365                       "%qE in namespace %qE does not name a template type",
3366                       TREE_OPERAND (id, 0), parser->scope);
3367           else
3368             error_at (location_of (id),
3369                       "%qE in namespace %qE does not name a type",
3370                       id, parser->scope);
3371           if (DECL_P (decl))
3372             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3373           else if (decl == error_mark_node)
3374             suggest_alternative_in_explicit_scope (location, id,
3375                                                    parser->scope);
3376         }
3377       else if (CLASS_TYPE_P (parser->scope)
3378                && constructor_name_p (id, parser->scope))
3379         {
3380           /* A<T>::A<T>() */
3381           error_at (location, "%<%T::%E%> names the constructor, not"
3382                     " the type", parser->scope, id);
3383           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3384             error_at (location, "and %qT has no template constructors",
3385                       parser->scope);
3386         }
3387       else if (TYPE_P (parser->scope)
3388                && dependent_scope_p (parser->scope))
3389         {
3390           if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3391             error_at (location,
3392                       "need %<typename%> before %<%T::%D::%E%> because "
3393                       "%<%T::%D%> is a dependent scope",
3394                       TYPE_CONTEXT (parser->scope),
3395                       TYPENAME_TYPE_FULLNAME (parser->scope),
3396                       id,
3397                       TYPE_CONTEXT (parser->scope),
3398                       TYPENAME_TYPE_FULLNAME (parser->scope));
3399           else
3400             error_at (location, "need %<typename%> before %<%T::%E%> because "
3401                       "%qT is a dependent scope",
3402                       parser->scope, id, parser->scope);
3403         }
3404       else if (TYPE_P (parser->scope))
3405         {
3406           if (!COMPLETE_TYPE_P (parser->scope))
3407             cxx_incomplete_type_error (location_of (id), NULL_TREE,
3408                                        parser->scope);
3409           else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3410             error_at (location_of (id),
3411                       "%qE in %q#T does not name a template type",
3412                       id, parser->scope);
3413           else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3414             error_at (location_of (id),
3415                       "%qE in %q#T does not name a template type",
3416                       TREE_OPERAND (id, 0), parser->scope);
3417           else
3418             error_at (location_of (id),
3419                       "%qE in %q#T does not name a type",
3420                       id, parser->scope);
3421           if (DECL_P (decl))
3422             inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3423         }
3424       else
3425         gcc_unreachable ();
3426     }
3427 }
3428
3429 /* Check for a common situation where a type-name should be present,
3430    but is not, and issue a sensible error message.  Returns true if an
3431    invalid type-name was detected.
3432
3433    The situation handled by this function are variable declarations of the
3434    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3435    Usually, `ID' should name a type, but if we got here it means that it
3436    does not. We try to emit the best possible error message depending on
3437    how exactly the id-expression looks like.  */
3438
3439 static bool
3440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3441 {
3442   tree id;
3443   cp_token *token = cp_lexer_peek_token (parser->lexer);
3444
3445   /* Avoid duplicate error about ambiguous lookup.  */
3446   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3447     {
3448       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3449       if (next->type == CPP_NAME && next->error_reported)
3450         goto out;
3451     }
3452
3453   cp_parser_parse_tentatively (parser);
3454   id = cp_parser_id_expression (parser,
3455                                 /*template_keyword_p=*/false,
3456                                 /*check_dependency_p=*/true,
3457                                 /*template_p=*/NULL,
3458                                 /*declarator_p=*/false,
3459                                 /*optional_p=*/false);
3460   /* If the next token is a (, this is a function with no explicit return
3461      type, i.e. constructor, destructor or conversion op.  */
3462   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3463       || TREE_CODE (id) == TYPE_DECL)
3464     {
3465       cp_parser_abort_tentative_parse (parser);
3466       return false;
3467     }
3468   if (!cp_parser_parse_definitely (parser))
3469     return false;
3470
3471   /* Emit a diagnostic for the invalid type.  */
3472   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3473  out:
3474   /* If we aren't in the middle of a declarator (i.e. in a
3475      parameter-declaration-clause), skip to the end of the declaration;
3476      there's no point in trying to process it.  */
3477   if (!parser->in_declarator_p)
3478     cp_parser_skip_to_end_of_block_or_statement (parser);
3479   return true;
3480 }
3481
3482 /* Consume tokens up to, and including, the next non-nested closing `)'.
3483    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3484    are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3485    found an unnested token of that type.  */
3486
3487 static int
3488 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3489                                          bool recovering,
3490                                          cpp_ttype or_ttype,
3491                                          bool consume_paren)
3492 {
3493   unsigned paren_depth = 0;
3494   unsigned brace_depth = 0;
3495   unsigned square_depth = 0;
3496
3497   if (recovering && or_ttype == CPP_EOF
3498       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3499     return 0;
3500
3501   while (true)
3502     {
3503       cp_token * token = cp_lexer_peek_token (parser->lexer);
3504
3505       /* Have we found what we're looking for before the closing paren?  */
3506       if (token->type == or_ttype && or_ttype != CPP_EOF
3507           && !brace_depth && !paren_depth && !square_depth)
3508         return -1;
3509
3510       switch (token->type)
3511         {
3512         case CPP_EOF:
3513         case CPP_PRAGMA_EOL:
3514           /* If we've run out of tokens, then there is no closing `)'.  */
3515           return 0;
3516
3517         /* This is good for lambda expression capture-lists.  */
3518         case CPP_OPEN_SQUARE:
3519           ++square_depth;
3520           break;
3521         case CPP_CLOSE_SQUARE:
3522           if (!square_depth--)
3523             return 0;
3524           break;
3525
3526         case CPP_SEMICOLON:
3527           /* This matches the processing in skip_to_end_of_statement.  */
3528           if (!brace_depth)
3529             return 0;
3530           break;
3531
3532         case CPP_OPEN_BRACE:
3533           ++brace_depth;
3534           break;
3535         case CPP_CLOSE_BRACE:
3536           if (!brace_depth--)
3537             return 0;
3538           break;
3539
3540         case CPP_OPEN_PAREN:
3541           if (!brace_depth)
3542             ++paren_depth;
3543           break;
3544
3545         case CPP_CLOSE_PAREN:
3546           if (!brace_depth && !paren_depth--)
3547             {
3548               if (consume_paren)
3549                 cp_lexer_consume_token (parser->lexer);
3550               return 1;
3551             }
3552           break;
3553
3554         default:
3555           break;
3556         }
3557
3558       /* Consume the token.  */
3559       cp_lexer_consume_token (parser->lexer);
3560     }
3561 }
3562
3563 /* Consume tokens up to, and including, the next non-nested closing `)'.
3564    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3565    are doing error recovery. Returns -1 if OR_COMMA is true and we
3566    found an unnested token of that type.  */
3567
3568 static int
3569 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3570                                        bool recovering,
3571                                        bool or_comma,
3572                                        bool consume_paren)
3573 {
3574   cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3575   return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3576                                                   ttype, consume_paren);
3577 }
3578
3579 /* Consume tokens until we reach the end of the current statement.
3580    Normally, that will be just before consuming a `;'.  However, if a
3581    non-nested `}' comes first, then we stop before consuming that.  */
3582
3583 static void
3584 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3585 {
3586   unsigned nesting_depth = 0;
3587
3588   /* Unwind generic function template scope if necessary.  */
3589   if (parser->fully_implicit_function_template_p)
3590     abort_fully_implicit_template (parser);
3591
3592   while (true)
3593     {
3594       cp_token *token = cp_lexer_peek_token (parser->lexer);
3595
3596       switch (token->type)
3597         {
3598         case CPP_EOF:
3599         case CPP_PRAGMA_EOL:
3600           /* If we've run out of tokens, stop.  */
3601           return;
3602
3603         case CPP_SEMICOLON:
3604           /* If the next token is a `;', we have reached the end of the
3605              statement.  */
3606           if (!nesting_depth)
3607             return;
3608           break;
3609
3610         case CPP_CLOSE_BRACE:
3611           /* If this is a non-nested '}', stop before consuming it.
3612              That way, when confronted with something like:
3613
3614                { 3 + }
3615
3616              we stop before consuming the closing '}', even though we
3617              have not yet reached a `;'.  */
3618           if (nesting_depth == 0)
3619             return;
3620
3621           /* If it is the closing '}' for a block that we have
3622              scanned, stop -- but only after consuming the token.
3623              That way given:
3624
3625                 void f g () { ... }
3626                 typedef int I;
3627
3628              we will stop after the body of the erroneously declared
3629              function, but before consuming the following `typedef'
3630              declaration.  */
3631           if (--nesting_depth == 0)
3632             {
3633               cp_lexer_consume_token (parser->lexer);
3634               return;
3635             }
3636           break;
3637
3638         case CPP_OPEN_BRACE:
3639           ++nesting_depth;
3640           break;
3641
3642         default:
3643           break;
3644         }
3645
3646       /* Consume the token.  */
3647       cp_lexer_consume_token (parser->lexer);
3648     }
3649 }
3650
3651 /* This function is called at the end of a statement or declaration.
3652    If the next token is a semicolon, it is consumed; otherwise, error
3653    recovery is attempted.  */
3654
3655 static void
3656 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3657 {
3658   /* Look for the trailing `;'.  */
3659   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3660     {
3661       /* If there is additional (erroneous) input, skip to the end of
3662          the statement.  */
3663       cp_parser_skip_to_end_of_statement (parser);
3664       /* If the next token is now a `;', consume it.  */
3665       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3666         cp_lexer_consume_token (parser->lexer);
3667     }
3668 }
3669
3670 /* Skip tokens until we have consumed an entire block, or until we
3671    have consumed a non-nested `;'.  */
3672
3673 static void
3674 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3675 {
3676   int nesting_depth = 0;
3677
3678   /* Unwind generic function template scope if necessary.  */
3679   if (parser->fully_implicit_function_template_p)
3680     abort_fully_implicit_template (parser);
3681
3682   while (nesting_depth >= 0)
3683     {
3684       cp_token *token = cp_lexer_peek_token (parser->lexer);
3685
3686       switch (token->type)
3687         {
3688         case CPP_EOF:
3689         case CPP_PRAGMA_EOL:
3690           /* If we've run out of tokens, stop.  */
3691           return;
3692
3693         case CPP_SEMICOLON:
3694           /* Stop if this is an unnested ';'. */
3695           if (!nesting_depth)
3696             nesting_depth = -1;
3697           break;
3698
3699         case CPP_CLOSE_BRACE:
3700           /* Stop if this is an unnested '}', or closes the outermost
3701              nesting level.  */
3702           nesting_depth--;
3703           if (nesting_depth < 0)
3704             return;
3705           if (!nesting_depth)
3706             nesting_depth = -1;
3707           break;
3708
3709         case CPP_OPEN_BRACE:
3710           /* Nest. */
3711           nesting_depth++;
3712           break;
3713
3714         default:
3715           break;
3716         }
3717
3718       /* Consume the token.  */
3719       cp_lexer_consume_token (parser->lexer);
3720     }
3721 }
3722
3723 /* Skip tokens until a non-nested closing curly brace is the next
3724    token, or there are no more tokens. Return true in the first case,
3725    false otherwise.  */
3726
3727 static bool
3728 cp_parser_skip_to_closing_brace (cp_parser *parser)
3729 {
3730   unsigned nesting_depth = 0;
3731
3732   while (true)
3733     {
3734       cp_token *token = cp_lexer_peek_token (parser->lexer);
3735
3736       switch (token->type)
3737         {
3738         case CPP_EOF:
3739         case CPP_PRAGMA_EOL:
3740           /* If we've run out of tokens, stop.  */
3741           return false;
3742
3743         case CPP_CLOSE_BRACE:
3744           /* If the next token is a non-nested `}', then we have reached
3745              the end of the current block.  */
3746           if (nesting_depth-- == 0)
3747             return true;
3748           break;
3749
3750         case CPP_OPEN_BRACE:
3751           /* If it the next token is a `{', then we are entering a new
3752              block.  Consume the entire block.  */
3753           ++nesting_depth;
3754           break;
3755
3756         default:
3757           break;
3758         }
3759
3760       /* Consume the token.  */
3761       cp_lexer_consume_token (parser->lexer);
3762     }
3763 }
3764
3765 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3766    parameter is the PRAGMA token, allowing us to purge the entire pragma
3767    sequence.  */
3768
3769 static void
3770 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3771 {
3772   cp_token *token;
3773
3774   parser->lexer->in_pragma = false;
3775
3776   do
3777     token = cp_lexer_consume_token (parser->lexer);
3778   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3779
3780   /* Ensure that the pragma is not parsed again.  */
3781   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3782 }
3783
3784 /* Require pragma end of line, resyncing with it as necessary.  The
3785    arguments are as for cp_parser_skip_to_pragma_eol.  */
3786
3787 static void
3788 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3789 {
3790   parser->lexer->in_pragma = false;
3791   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3792     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3793 }
3794
3795 /* This is a simple wrapper around make_typename_type. When the id is
3796    an unresolved identifier node, we can provide a superior diagnostic
3797    using cp_parser_diagnose_invalid_type_name.  */
3798
3799 static tree
3800 cp_parser_make_typename_type (cp_parser *parser, tree id,
3801                               location_t id_location)
3802 {
3803   tree result;
3804   if (identifier_p (id))
3805     {
3806       result = make_typename_type (parser->scope, id, typename_type,
3807                                    /*complain=*/tf_none);
3808       if (result == error_mark_node)
3809         cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3810       return result;
3811     }
3812   return make_typename_type (parser->scope, id, typename_type, tf_error);
3813 }
3814
3815 /* This is a wrapper around the
3816    make_{pointer,ptrmem,reference}_declarator functions that decides
3817    which one to call based on the CODE and CLASS_TYPE arguments. The
3818    CODE argument should be one of the values returned by
3819    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3820    appertain to the pointer or reference.  */
3821
3822 static cp_declarator *
3823 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3824                                     cp_cv_quals cv_qualifiers,
3825                                     cp_declarator *target,
3826                                     tree attributes)
3827 {
3828   if (code == ERROR_MARK || target == cp_error_declarator)
3829     return cp_error_declarator;
3830
3831   if (code == INDIRECT_REF)
3832     if (class_type == NULL_TREE)
3833       return make_pointer_declarator (cv_qualifiers, target, attributes);
3834     else
3835       return make_ptrmem_declarator (cv_qualifiers, class_type,
3836                                      target, attributes);
3837   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3838     return make_reference_declarator (cv_qualifiers, target,
3839                                       false, attributes);
3840   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3841     return make_reference_declarator (cv_qualifiers, target,
3842                                       true, attributes);
3843   gcc_unreachable ();
3844 }
3845
3846 /* Create a new C++ parser.  */
3847
3848 static cp_parser *
3849 cp_parser_new (void)
3850 {
3851   cp_parser *parser;
3852   cp_lexer *lexer;
3853   unsigned i;
3854
3855   /* cp_lexer_new_main is called before doing GC allocation because
3856      cp_lexer_new_main might load a PCH file.  */
3857   lexer = cp_lexer_new_main ();
3858
3859   /* Initialize the binops_by_token so that we can get the tree
3860      directly from the token.  */
3861   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3862     binops_by_token[binops[i].token_type] = binops[i];
3863
3864   parser = ggc_cleared_alloc<cp_parser> ();
3865   parser->lexer = lexer;
3866   parser->context = cp_parser_context_new (NULL);
3867
3868   /* For now, we always accept GNU extensions.  */
3869   parser->allow_gnu_extensions_p = 1;
3870
3871   /* The `>' token is a greater-than operator, not the end of a
3872      template-id.  */
3873   parser->greater_than_is_operator_p = true;
3874
3875   parser->default_arg_ok_p = true;
3876
3877   /* We are not parsing a constant-expression.  */
3878   parser->integral_constant_expression_p = false;
3879   parser->allow_non_integral_constant_expression_p = false;
3880   parser->non_integral_constant_expression_p = false;
3881
3882   /* Local variable names are not forbidden.  */
3883   parser->local_variables_forbidden_p = false;
3884
3885   /* We are not processing an `extern "C"' declaration.  */
3886   parser->in_unbraced_linkage_specification_p = false;
3887
3888   /* We are not processing a declarator.  */
3889   parser->in_declarator_p = false;
3890
3891   /* We are not processing a template-argument-list.  */
3892   parser->in_template_argument_list_p = false;
3893
3894   /* We are not in an iteration statement.  */
3895   parser->in_statement = 0;
3896
3897   /* We are not in a switch statement.  */
3898   parser->in_switch_statement_p = false;
3899
3900   /* We are not parsing a type-id inside an expression.  */
3901   parser->in_type_id_in_expr_p = false;
3902
3903   /* Declarations aren't implicitly extern "C".  */
3904   parser->implicit_extern_c = false;
3905
3906   /* String literals should be translated to the execution character set.  */
3907   parser->translate_strings_p = true;
3908
3909   /* We are not parsing a function body.  */
3910   parser->in_function_body = false;
3911
3912   /* We can correct until told otherwise.  */
3913   parser->colon_corrects_to_scope_p = true;
3914
3915   /* The unparsed function queue is empty.  */
3916   push_unparsed_function_queues (parser);
3917
3918   /* There are no classes being defined.  */
3919   parser->num_classes_being_defined = 0;
3920
3921   /* No template parameters apply.  */
3922   parser->num_template_parameter_lists = 0;
3923
3924   /* Special parsing data structures.  */
3925   parser->omp_declare_simd = NULL;
3926   parser->oacc_routine = NULL;
3927
3928   /* Not declaring an implicit function template.  */
3929   parser->auto_is_implicit_function_template_parm_p = false;
3930   parser->fully_implicit_function_template_p = false;
3931   parser->implicit_template_parms = 0;
3932   parser->implicit_template_scope = 0;
3933
3934   /* Allow constrained-type-specifiers. */
3935   parser->prevent_constrained_type_specifiers = 0;
3936
3937   /* We haven't yet seen an 'extern "C"'.  */
3938   parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3939
3940   return parser;
3941 }
3942
3943 /* Create a cp_lexer structure which will emit the tokens in CACHE
3944    and push it onto the parser's lexer stack.  This is used for delayed
3945    parsing of in-class method bodies and default arguments, and should
3946    not be confused with tentative parsing.  */
3947 static void
3948 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3949 {
3950   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3951   lexer->next = parser->lexer;
3952   parser->lexer = lexer;
3953
3954   /* Move the current source position to that of the first token in the
3955      new lexer.  */
3956   cp_lexer_set_source_position_from_token (lexer->next_token);
3957 }
3958
3959 /* Pop the top lexer off the parser stack.  This is never used for the
3960    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3961 static void
3962 cp_parser_pop_lexer (cp_parser *parser)
3963 {
3964   cp_lexer *lexer = parser->lexer;
3965   parser->lexer = lexer->next;
3966   cp_lexer_destroy (lexer);
3967
3968   /* Put the current source position back where it was before this
3969      lexer was pushed.  */
3970   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3971 }
3972
3973 /* Lexical conventions [gram.lex]  */
3974
3975 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3976    identifier.  */
3977
3978 static cp_expr
3979 cp_parser_identifier (cp_parser* parser)
3980 {
3981   cp_token *token;
3982
3983   /* Look for the identifier.  */
3984   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3985   /* Return the value.  */
3986   if (token)
3987     return cp_expr (token->u.value, token->location);
3988   else
3989     return error_mark_node;
3990 }
3991
3992 /* Parse a sequence of adjacent string constants.  Returns a
3993    TREE_STRING representing the combined, nul-terminated string
3994    constant.  If TRANSLATE is true, translate the string to the
3995    execution character set.  If WIDE_OK is true, a wide string is
3996    invalid here.
3997
3998    C++98 [lex.string] says that if a narrow string literal token is
3999    adjacent to a wide string literal token, the behavior is undefined.
4000    However, C99 6.4.5p4 says that this results in a wide string literal.
4001    We follow C99 here, for consistency with the C front end.
4002
4003    This code is largely lifted from lex_string() in c-lex.c.
4004
4005    FUTURE: ObjC++ will need to handle @-strings here.  */
4006 static cp_expr
4007 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4008                           bool lookup_udlit = true)
4009 {
4010   tree value;
4011   size_t count;
4012   struct obstack str_ob;
4013   cpp_string str, istr, *strs;
4014   cp_token *tok;
4015   enum cpp_ttype type, curr_type;
4016   int have_suffix_p = 0;
4017   tree string_tree;
4018   tree suffix_id = NULL_TREE;
4019   bool curr_tok_is_userdef_p = false;
4020
4021   tok = cp_lexer_peek_token (parser->lexer);
4022   if (!cp_parser_is_string_literal (tok))
4023     {
4024       cp_parser_error (parser, "expected string-literal");
4025       return error_mark_node;
4026     }
4027
4028   location_t loc = tok->location;
4029
4030   if (cpp_userdef_string_p (tok->type))
4031     {
4032       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4033       curr_type = cpp_userdef_string_remove_type (tok->type);
4034       curr_tok_is_userdef_p = true;
4035     }
4036   else
4037     {
4038       string_tree = tok->u.value;
4039       curr_type = tok->type;
4040     }
4041   type = curr_type;
4042
4043   /* Try to avoid the overhead of creating and destroying an obstack
4044      for the common case of just one string.  */
4045   if (!cp_parser_is_string_literal
4046       (cp_lexer_peek_nth_token (parser->lexer, 2)))
4047     {
4048       cp_lexer_consume_token (parser->lexer);
4049
4050       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4051       str.len = TREE_STRING_LENGTH (string_tree);
4052       count = 1;
4053
4054       if (curr_tok_is_userdef_p)
4055         {
4056           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4057           have_suffix_p = 1;
4058           curr_type = cpp_userdef_string_remove_type (tok->type);
4059         }
4060       else
4061         curr_type = tok->type;
4062
4063       strs = &str;
4064     }
4065   else
4066     {
4067       location_t last_tok_loc = tok->location;
4068       gcc_obstack_init (&str_ob);
4069       count = 0;
4070
4071       do
4072         {
4073           cp_lexer_consume_token (parser->lexer);
4074           count++;
4075           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4076           str.len = TREE_STRING_LENGTH (string_tree);
4077
4078           if (curr_tok_is_userdef_p)
4079             {
4080               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4081               if (have_suffix_p == 0)
4082                 {
4083                   suffix_id = curr_suffix_id;
4084                   have_suffix_p = 1;
4085                 }
4086               else if (have_suffix_p == 1
4087                        && curr_suffix_id != suffix_id)
4088                 {
4089                   error ("inconsistent user-defined literal suffixes"
4090                          " %qD and %qD in string literal",
4091                          suffix_id, curr_suffix_id);
4092                   have_suffix_p = -1;
4093                 }
4094               curr_type = cpp_userdef_string_remove_type (tok->type);
4095             }
4096           else
4097             curr_type = tok->type;
4098
4099           if (type != curr_type)
4100             {
4101               if (type == CPP_STRING)
4102                 type = curr_type;
4103               else if (curr_type != CPP_STRING)
4104                 {
4105                   rich_location rich_loc (line_table, tok->location);
4106                   rich_loc.add_range (last_tok_loc, false);
4107                   error_at (&rich_loc,
4108                             "unsupported non-standard concatenation "
4109                             "of string literals");
4110                 }
4111             }
4112
4113           obstack_grow (&str_ob, &str, sizeof (cpp_string));
4114
4115           last_tok_loc = tok->location;
4116
4117           tok = cp_lexer_peek_token (parser->lexer);
4118           if (cpp_userdef_string_p (tok->type))
4119             {
4120               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4121               curr_type = cpp_userdef_string_remove_type (tok->type);
4122               curr_tok_is_userdef_p = true;
4123             }
4124           else
4125             {
4126               string_tree = tok->u.value;
4127               curr_type = tok->type;
4128               curr_tok_is_userdef_p = false;
4129             }
4130         }
4131       while (cp_parser_is_string_literal (tok));
4132
4133       /* A string literal built by concatenation has its caret=start at
4134          the start of the initial string, and its finish at the finish of
4135          the final string literal.  */
4136       loc = make_location (loc, loc, get_finish (last_tok_loc));
4137
4138       strs = (cpp_string *) obstack_finish (&str_ob);
4139     }
4140
4141   if (type != CPP_STRING && !wide_ok)
4142     {
4143       cp_parser_error (parser, "a wide string is invalid in this context");
4144       type = CPP_STRING;
4145     }
4146
4147   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4148       (parse_in, strs, count, &istr, type))
4149     {
4150       value = build_string (istr.len, (const char *)istr.text);
4151       free (CONST_CAST (unsigned char *, istr.text));
4152
4153       switch (type)
4154         {
4155         default:
4156         case CPP_STRING:
4157         case CPP_UTF8STRING:
4158           TREE_TYPE (value) = char_array_type_node;
4159           break;
4160         case CPP_STRING16:
4161           TREE_TYPE (value) = char16_array_type_node;
4162           break;
4163         case CPP_STRING32:
4164           TREE_TYPE (value) = char32_array_type_node;
4165           break;
4166         case CPP_WSTRING:
4167           TREE_TYPE (value) = wchar_array_type_node;
4168           break;
4169         }
4170
4171       value = fix_string_type (value);
4172
4173       if (have_suffix_p)
4174         {
4175           tree literal = build_userdef_literal (suffix_id, value,
4176                                                 OT_NONE, NULL_TREE);
4177           if (lookup_udlit)
4178             value = cp_parser_userdef_string_literal (literal);
4179           else
4180             value = literal;
4181         }
4182     }
4183   else
4184     /* cpp_interpret_string has issued an error.  */
4185     value = error_mark_node;
4186
4187   if (count > 1)
4188     obstack_free (&str_ob, 0);
4189
4190   return cp_expr (value, loc);
4191 }
4192
4193 /* Look up a literal operator with the name and the exact arguments.  */
4194
4195 static tree
4196 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4197 {
4198   tree decl;
4199   decl = lookup_name (name);
4200   if (!decl || !is_overloaded_fn (decl))
4201     return error_mark_node;
4202
4203   for (lkp_iterator iter (decl); iter; ++iter)
4204     {
4205       unsigned int ix;
4206       bool found = true;
4207       tree fn = *iter;
4208       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4209       if (parmtypes != NULL_TREE)
4210         {
4211           for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4212                ++ix, parmtypes = TREE_CHAIN (parmtypes))
4213             {
4214               tree tparm = TREE_VALUE (parmtypes);
4215               tree targ = TREE_TYPE ((*args)[ix]);
4216               bool ptr = TYPE_PTR_P (tparm);
4217               bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4218               if ((ptr || arr || !same_type_p (tparm, targ))
4219                   && (!ptr || !arr
4220                       || !same_type_p (TREE_TYPE (tparm),
4221                                        TREE_TYPE (targ))))
4222                 found = false;
4223             }
4224           if (found
4225               && ix == vec_safe_length (args)
4226               /* May be this should be sufficient_parms_p instead,
4227                  depending on how exactly should user-defined literals
4228                  work in presence of default arguments on the literal
4229                  operator parameters.  */
4230               && parmtypes == void_list_node)
4231             return decl;
4232         }
4233     }
4234
4235   return error_mark_node;
4236 }
4237
4238 /* Parse a user-defined char constant.  Returns a call to a user-defined
4239    literal operator taking the character as an argument.  */
4240
4241 static cp_expr
4242 cp_parser_userdef_char_literal (cp_parser *parser)
4243 {
4244   cp_token *token = cp_lexer_consume_token (parser->lexer);
4245   tree literal = token->u.value;
4246   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4247   tree value = USERDEF_LITERAL_VALUE (literal);
4248   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4249   tree decl, result;
4250
4251   /* Build up a call to the user-defined operator  */
4252   /* Lookup the name we got back from the id-expression.  */
4253   vec<tree, va_gc> *args = make_tree_vector ();
4254   vec_safe_push (args, value);
4255   decl = lookup_literal_operator (name, args);
4256   if (!decl || decl == error_mark_node)
4257     {
4258       error ("unable to find character literal operator %qD with %qT argument",
4259              name, TREE_TYPE (value));
4260       release_tree_vector (args);
4261       return error_mark_node;
4262     }
4263   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4264   release_tree_vector (args);
4265   return result;
4266 }
4267
4268 /* A subroutine of cp_parser_userdef_numeric_literal to
4269    create a char... template parameter pack from a string node.  */
4270
4271 static tree
4272 make_char_string_pack (tree value)
4273 {
4274   tree charvec;
4275   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4276   const char *str = TREE_STRING_POINTER (value);
4277   int i, len = TREE_STRING_LENGTH (value) - 1;
4278   tree argvec = make_tree_vec (1);
4279
4280   /* Fill in CHARVEC with all of the parameters.  */
4281   charvec = make_tree_vec (len);
4282   for (i = 0; i < len; ++i)
4283     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4284
4285   /* Build the argument packs.  */
4286   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4287
4288   TREE_VEC_ELT (argvec, 0) = argpack;
4289
4290   return argvec;
4291 }
4292
4293 /* A subroutine of cp_parser_userdef_numeric_literal to
4294    create a char... template parameter pack from a string node.  */
4295
4296 static tree
4297 make_string_pack (tree value)
4298 {
4299   tree charvec;
4300   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4301   const unsigned char *str
4302     = (const unsigned char *) TREE_STRING_POINTER (value);
4303   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4304   int len = TREE_STRING_LENGTH (value) / sz - 1;
4305   tree argvec = make_tree_vec (2);
4306
4307   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4308   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4309
4310   /* First template parm is character type.  */
4311   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4312
4313   /* Fill in CHARVEC with all of the parameters.  */
4314   charvec = make_tree_vec (len);
4315   for (int i = 0; i < len; ++i)
4316     TREE_VEC_ELT (charvec, i)
4317       = double_int_to_tree (str_char_type_node,
4318                             double_int::from_buffer (str + i * sz, sz));
4319
4320   /* Build the argument packs.  */
4321   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4322
4323   TREE_VEC_ELT (argvec, 1) = argpack;
4324
4325   return argvec;
4326 }
4327
4328 /* Parse a user-defined numeric constant.  returns a call to a user-defined
4329    literal operator.  */
4330
4331 static cp_expr
4332 cp_parser_userdef_numeric_literal (cp_parser *parser)
4333 {
4334   cp_token *token = cp_lexer_consume_token (parser->lexer);
4335   tree literal = token->u.value;
4336   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4337   tree value = USERDEF_LITERAL_VALUE (literal);
4338   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4339   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4340   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341   tree decl, result;
4342   vec<tree, va_gc> *args;
4343
4344   /* Look for a literal operator taking the exact type of numeric argument
4345      as the literal value.  */
4346   args = make_tree_vector ();
4347   vec_safe_push (args, value);
4348   decl = lookup_literal_operator (name, args);
4349   if (decl && decl != error_mark_node)
4350     {
4351       result = finish_call_expr (decl, &args, false, true,
4352                                  tf_warning_or_error);
4353
4354       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4355         {
4356           warning_at (token->location, OPT_Woverflow,
4357                       "integer literal exceeds range of %qT type",
4358                       long_long_unsigned_type_node);
4359         }
4360       else
4361         {
4362           if (overflow > 0)
4363             warning_at (token->location, OPT_Woverflow,
4364                         "floating literal exceeds range of %qT type",
4365                         long_double_type_node);
4366           else if (overflow < 0)
4367             warning_at (token->location, OPT_Woverflow,
4368                         "floating literal truncated to zero");
4369         }
4370
4371       release_tree_vector (args);
4372       return result;
4373     }
4374   release_tree_vector (args);
4375
4376   /* If the numeric argument didn't work, look for a raw literal
4377      operator taking a const char* argument consisting of the number
4378      in string format.  */
4379   args = make_tree_vector ();
4380   vec_safe_push (args, num_string);
4381   decl = lookup_literal_operator (name, args);
4382   if (decl && decl != error_mark_node)
4383     {
4384       result = finish_call_expr (decl, &args, false, true,
4385                                  tf_warning_or_error);
4386       release_tree_vector (args);
4387       return result;
4388     }
4389   release_tree_vector (args);
4390
4391   /* If the raw literal didn't work, look for a non-type template
4392      function with parameter pack char....  Call the function with
4393      template parameter characters representing the number.  */
4394   args = make_tree_vector ();
4395   decl = lookup_literal_operator (name, args);
4396   if (decl && decl != error_mark_node)
4397     {
4398       tree tmpl_args = make_char_string_pack (num_string);
4399       decl = lookup_template_function (decl, tmpl_args);
4400       result = finish_call_expr (decl, &args, false, true,
4401                                  tf_warning_or_error);
4402       release_tree_vector (args);
4403       return result;
4404     }
4405
4406   release_tree_vector (args);
4407
4408   /* In C++14 the standard library defines complex number suffixes that
4409      conflict with GNU extensions.  Prefer them if <complex> is #included.  */
4410   bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4411   bool i14 = (cxx_dialect > cxx11
4412               && (id_equal (suffix_id, "i")
4413                   || id_equal (suffix_id, "if")
4414                   || id_equal (suffix_id, "il")));
4415   diagnostic_t kind = DK_ERROR;
4416   int opt = 0;
4417
4418   if (i14 && ext)
4419     {
4420       tree cxlit = lookup_qualified_name (std_node,
4421                                           get_identifier ("complex_literals"),
4422                                           0, false, false);
4423       if (cxlit == error_mark_node)
4424         {
4425           /* No <complex>, so pedwarn and use GNU semantics.  */
4426           kind = DK_PEDWARN;
4427           opt = OPT_Wpedantic;
4428         }
4429     }
4430
4431   bool complained
4432     = emit_diagnostic (kind, input_location, opt,
4433                        "unable to find numeric literal operator %qD", name);
4434
4435   if (!complained)
4436     /* Don't inform either.  */;
4437   else if (i14)
4438     {
4439       inform (token->location, "add %<using namespace std::complex_literals%> "
4440               "(from <complex>) to enable the C++14 user-defined literal "
4441               "suffixes");
4442       if (ext)
4443         inform (token->location, "or use %<j%> instead of %<i%> for the "
4444                 "GNU built-in suffix");
4445     }
4446   else if (!ext)
4447     inform (token->location, "use -fext-numeric-literals "
4448             "to enable more built-in suffixes");
4449
4450   if (kind == DK_ERROR)
4451     value = error_mark_node;
4452   else
4453     {
4454       /* Use the built-in semantics.  */
4455       tree type;
4456       if (id_equal (suffix_id, "i"))
4457         {
4458           if (TREE_CODE (value) == INTEGER_CST)
4459             type = integer_type_node;
4460           else
4461             type = double_type_node;
4462         }
4463       else if (id_equal (suffix_id, "if"))
4464         type = float_type_node;
4465       else /* if (id_equal (suffix_id, "il")) */
4466         type = long_double_type_node;
4467
4468       value = build_complex (build_complex_type (type),
4469                              fold_convert (type, integer_zero_node),
4470                              fold_convert (type, value));
4471     }
4472
4473   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4474     /* Avoid repeated diagnostics.  */
4475     token->u.value = value;
4476   return value;
4477 }
4478
4479 /* Parse a user-defined string constant.  Returns a call to a user-defined
4480    literal operator taking a character pointer and the length of the string
4481    as arguments.  */
4482
4483 static tree
4484 cp_parser_userdef_string_literal (tree literal)
4485 {
4486   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4487   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4488   tree value = USERDEF_LITERAL_VALUE (literal);
4489   int len = TREE_STRING_LENGTH (value)
4490         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4491   tree decl, result;
4492   vec<tree, va_gc> *args;
4493
4494   /* Build up a call to the user-defined operator.  */
4495   /* Lookup the name we got back from the id-expression.  */
4496   args = make_tree_vector ();
4497   vec_safe_push (args, value);
4498   vec_safe_push (args, build_int_cst (size_type_node, len));
4499   decl = lookup_literal_operator (name, args);
4500
4501   if (decl && decl != error_mark_node)
4502     {
4503       result = finish_call_expr (decl, &args, false, true,
4504                                  tf_warning_or_error);
4505       release_tree_vector (args);
4506       return result;
4507     }
4508   release_tree_vector (args);
4509
4510   /* Look for a template function with typename parameter CharT
4511      and parameter pack CharT...  Call the function with
4512      template parameter characters representing the string.  */
4513   args = make_tree_vector ();
4514   decl = lookup_literal_operator (name, args);
4515   if (decl && decl != error_mark_node)
4516     {
4517       tree tmpl_args = make_string_pack (value);
4518       decl = lookup_template_function (decl, tmpl_args);
4519       result = finish_call_expr (decl, &args, false, true,
4520                                  tf_warning_or_error);
4521       release_tree_vector (args);
4522       return result;
4523     }
4524   release_tree_vector (args);
4525
4526   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4527          name, TREE_TYPE (value), size_type_node);
4528   return error_mark_node;
4529 }
4530
4531
4532 /* Basic concepts [gram.basic]  */
4533
4534 /* Parse a translation-unit.
4535
4536    translation-unit:
4537      declaration-seq [opt]
4538
4539    Returns TRUE if all went well.  */
4540
4541 static bool
4542 cp_parser_translation_unit (cp_parser* parser)
4543 {
4544   /* The address of the first non-permanent object on the declarator
4545      obstack.  */
4546   static void *declarator_obstack_base;
4547
4548   bool success;
4549
4550   /* Create the declarator obstack, if necessary.  */
4551   if (!cp_error_declarator)
4552     {
4553       gcc_obstack_init (&declarator_obstack);
4554       /* Create the error declarator.  */
4555       cp_error_declarator = make_declarator (cdk_error);
4556       /* Create the empty parameter list.  */
4557       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4558                                                  UNKNOWN_LOCATION);
4559       /* Remember where the base of the declarator obstack lies.  */
4560       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4561     }
4562
4563   cp_parser_declaration_seq_opt (parser);
4564
4565   /* If there are no tokens left then all went well.  */
4566   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4567     {
4568       /* Get rid of the token array; we don't need it any more.  */
4569       cp_lexer_destroy (parser->lexer);
4570       parser->lexer = NULL;
4571
4572       /* This file might have been a context that's implicitly extern
4573          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4574       if (parser->implicit_extern_c)
4575         {
4576           pop_lang_context ();
4577           parser->implicit_extern_c = false;
4578         }
4579
4580       /* Finish up.  */
4581       finish_translation_unit ();
4582
4583       success = true;
4584     }
4585   else
4586     {
4587       cp_parser_error (parser, "expected declaration");
4588       success = false;
4589     }
4590
4591   /* Make sure the declarator obstack was fully cleaned up.  */
4592   gcc_assert (obstack_next_free (&declarator_obstack)
4593               == declarator_obstack_base);
4594
4595   /* All went well.  */
4596   return success;
4597 }
4598
4599 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4600    decltype context.  */
4601
4602 static inline tsubst_flags_t
4603 complain_flags (bool decltype_p)
4604 {
4605   tsubst_flags_t complain = tf_warning_or_error;
4606   if (decltype_p)
4607     complain |= tf_decltype;
4608   return complain;
4609 }
4610
4611 /* We're about to parse a collection of statements.  If we're currently
4612    parsing tentatively, set up a firewall so that any nested
4613    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4614
4615 static cp_token_position
4616 cp_parser_start_tentative_firewall (cp_parser *parser)
4617 {
4618   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4619     return 0;
4620
4621   cp_parser_parse_tentatively (parser);
4622   cp_parser_commit_to_topmost_tentative_parse (parser);
4623   return cp_lexer_token_position (parser->lexer, false);
4624 }
4625
4626 /* We've finished parsing the collection of statements.  Wrap up the
4627    firewall and replace the relevant tokens with the parsed form.  */
4628
4629 static void
4630 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4631                                   tree expr)
4632 {
4633   if (!start)
4634     return;
4635
4636   /* Finish the firewall level.  */
4637   cp_parser_parse_definitely (parser);
4638   /* And remember the result of the parse for when we try again.  */
4639   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4640   token->type = CPP_PREPARSED_EXPR;
4641   token->u.value = expr;
4642   token->keyword = RID_MAX;
4643   cp_lexer_purge_tokens_after (parser->lexer, start);
4644 }
4645
4646 /* Like the above functions, but let the user modify the tokens.  Used by
4647    CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4648    later parses, so it makes sense to localize the effects of
4649    cp_parser_commit_to_tentative_parse.  */
4650
4651 struct tentative_firewall
4652 {
4653   cp_parser *parser;
4654   bool set;
4655
4656   tentative_firewall (cp_parser *p): parser(p)
4657   {
4658     /* If we're currently parsing tentatively, start a committed level as a
4659        firewall and then an inner tentative parse.  */
4660     if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4661       {
4662         cp_parser_parse_tentatively (parser);
4663         cp_parser_commit_to_topmost_tentative_parse (parser);
4664         cp_parser_parse_tentatively (parser);
4665       }
4666   }
4667
4668   ~tentative_firewall()
4669   {
4670     if (set)
4671       {
4672         /* Finish the inner tentative parse and the firewall, propagating any
4673            uncommitted error state to the outer tentative parse.  */
4674         bool err = cp_parser_error_occurred (parser);
4675         cp_parser_parse_definitely (parser);
4676         cp_parser_parse_definitely (parser);
4677         if (err)
4678           cp_parser_simulate_error (parser);
4679       }
4680   }
4681 };
4682
4683 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4684    This class is for tracking such a matching pair of symbols.
4685    In particular, it tracks the location of the first token,
4686    so that if the second token is missing, we can highlight the
4687    location of the first token when notifying the user about the
4688    problem.  */
4689
4690 template <typename traits_t>
4691 class token_pair
4692 {
4693  public:
4694   /* token_pair's ctor.  */
4695   token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4696
4697   /* If the next token is the opening symbol for this pair, consume it and
4698      return true.
4699      Otherwise, issue an error and return false.
4700      In either case, record the location of the opening token.  */
4701
4702   bool require_open (cp_parser *parser)
4703   {
4704     m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4705     return cp_parser_require (parser, traits_t::open_token_type,
4706                               traits_t::required_token_open);
4707   }
4708
4709   /* Consume the next token from PARSER, recording its location as
4710      that of the opening token within the pair.  */
4711
4712   cp_token * consume_open (cp_parser *parser)
4713   {
4714     cp_token *tok = cp_lexer_consume_token (parser->lexer);
4715     gcc_assert (tok->type == traits_t::open_token_type);
4716     m_open_loc = tok->location;
4717     return tok;
4718   }
4719
4720   /* If the next token is the closing symbol for this pair, consume it
4721      and return it.
4722      Otherwise, issue an error, highlighting the location of the
4723      corresponding opening token, and return NULL.  */
4724
4725   cp_token *require_close (cp_parser *parser) const
4726   {
4727     return cp_parser_require (parser, traits_t::close_token_type,
4728                               traits_t::required_token_close,
4729                               m_open_loc);
4730   }
4731
4732  private:
4733   location_t m_open_loc;
4734 };
4735
4736 /* Traits for token_pair<T> for tracking matching pairs of parentheses.  */
4737
4738 struct matching_paren_traits
4739 {
4740   static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4741   static const enum required_token required_token_open  = RT_OPEN_PAREN;
4742   static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4743   static const enum required_token required_token_close = RT_CLOSE_PAREN;
4744 };
4745
4746 /* "matching_parens" is a token_pair<T> class for tracking matching
4747    pairs of parentheses.  */
4748
4749 typedef token_pair<matching_paren_traits> matching_parens;
4750
4751 /* Traits for token_pair<T> for tracking matching pairs of braces.  */
4752
4753 struct matching_brace_traits
4754 {
4755   static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4756   static const enum required_token required_token_open = RT_OPEN_BRACE;
4757   static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4758   static const enum required_token required_token_close = RT_CLOSE_BRACE;
4759 };
4760
4761 /* "matching_braces" is a token_pair<T> class for tracking matching
4762    pairs of braces.  */
4763
4764 typedef token_pair<matching_brace_traits> matching_braces;
4765
4766
4767 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4768    enclosing parentheses.  */
4769
4770 static cp_expr
4771 cp_parser_statement_expr (cp_parser *parser)
4772 {
4773   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4774
4775   /* Consume the '('.  */
4776   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4777   matching_parens parens;
4778   parens.consume_open (parser);
4779   /* Start the statement-expression.  */
4780   tree expr = begin_stmt_expr ();
4781   /* Parse the compound-statement.  */
4782   cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4783   /* Finish up.  */
4784   expr = finish_stmt_expr (expr, false);
4785   /* Consume the ')'.  */
4786   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4787   if (!parens.require_close (parser))
4788     cp_parser_skip_to_end_of_statement (parser);
4789
4790   cp_parser_end_tentative_firewall (parser, start, expr);
4791   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4792   return cp_expr (expr, combined_loc);
4793 }
4794
4795 /* Expressions [gram.expr] */
4796
4797 /* Parse a fold-operator.
4798
4799     fold-operator:
4800         -  *  /  %  ^  &  |  =  <  >  <<  >>
4801       =  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=
4802       ==  !=  <=  >=  &&  ||  ,  .*  ->*
4803
4804    This returns the tree code corresponding to the matched operator
4805    as an int. When the current token matches a compound assignment
4806    opertor, the resulting tree code is the negative value of the
4807    non-assignment operator. */
4808
4809 static int
4810 cp_parser_fold_operator (cp_token *token)
4811 {
4812   switch (token->type)
4813     {
4814     case CPP_PLUS: return PLUS_EXPR;
4815     case CPP_MINUS: return MINUS_EXPR;
4816     case CPP_MULT: return MULT_EXPR;
4817     case CPP_DIV: return TRUNC_DIV_EXPR;
4818     case CPP_MOD: return TRUNC_MOD_EXPR;
4819     case CPP_XOR: return BIT_XOR_EXPR;
4820     case CPP_AND: return BIT_AND_EXPR;
4821     case CPP_OR: return BIT_IOR_EXPR;
4822     case CPP_LSHIFT: return LSHIFT_EXPR;
4823     case CPP_RSHIFT: return RSHIFT_EXPR;
4824
4825     case CPP_EQ: return -NOP_EXPR;
4826     case CPP_PLUS_EQ: return -PLUS_EXPR;
4827     case CPP_MINUS_EQ: return -MINUS_EXPR;
4828     case CPP_MULT_EQ: return -MULT_EXPR;
4829     case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4830     case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4831     case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4832     case CPP_AND_EQ: return -BIT_AND_EXPR;
4833     case CPP_OR_EQ: return -BIT_IOR_EXPR;
4834     case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4835     case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4836
4837     case CPP_EQ_EQ: return EQ_EXPR;
4838     case CPP_NOT_EQ: return NE_EXPR;
4839     case CPP_LESS: return LT_EXPR;
4840     case CPP_GREATER: return GT_EXPR;
4841     case CPP_LESS_EQ: return LE_EXPR;
4842     case CPP_GREATER_EQ: return GE_EXPR;
4843
4844     case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4845     case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4846
4847     case CPP_COMMA: return COMPOUND_EXPR;
4848
4849     case CPP_DOT_STAR: return DOTSTAR_EXPR;
4850     case CPP_DEREF_STAR: return MEMBER_REF;
4851
4852     default: return ERROR_MARK;
4853     }
4854 }
4855
4856 /* Returns true if CODE indicates a binary expression, which is not allowed in
4857    the LHS of a fold-expression.  More codes will need to be added to use this
4858    function in other contexts.  */
4859
4860 static bool
4861 is_binary_op (tree_code code)
4862 {
4863   switch (code)
4864     {
4865     case PLUS_EXPR:
4866     case POINTER_PLUS_EXPR:
4867     case MINUS_EXPR:
4868     case MULT_EXPR:
4869     case TRUNC_DIV_EXPR:
4870     case TRUNC_MOD_EXPR:
4871     case BIT_XOR_EXPR:
4872     case BIT_AND_EXPR:
4873     case BIT_IOR_EXPR:
4874     case LSHIFT_EXPR:
4875     case RSHIFT_EXPR:
4876
4877     case MODOP_EXPR:
4878
4879     case EQ_EXPR:
4880     case NE_EXPR:
4881     case LE_EXPR:
4882     case GE_EXPR:
4883     case LT_EXPR:
4884     case GT_EXPR:
4885
4886     case TRUTH_ANDIF_EXPR:
4887     case TRUTH_ORIF_EXPR:
4888
4889     case COMPOUND_EXPR:
4890
4891     case DOTSTAR_EXPR:
4892     case MEMBER_REF:
4893       return true;
4894
4895     default:
4896       return false;
4897     }
4898 }
4899
4900 /* If the next token is a suitable fold operator, consume it and return as
4901    the function above.  */
4902
4903 static int
4904 cp_parser_fold_operator (cp_parser *parser)
4905 {
4906   cp_token* token = cp_lexer_peek_token (parser->lexer);
4907   int code = cp_parser_fold_operator (token);
4908   if (code != ERROR_MARK)
4909     cp_lexer_consume_token (parser->lexer);
4910   return code;
4911 }
4912
4913 /* Parse a fold-expression.
4914
4915      fold-expression:
4916        ( ... folding-operator cast-expression)
4917        ( cast-expression folding-operator ... )
4918        ( cast-expression folding operator ... folding-operator cast-expression)
4919
4920    Note that the '(' and ')' are matched in primary expression. */
4921
4922 static cp_expr
4923 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4924 {
4925   cp_id_kind pidk;
4926
4927   // Left fold.
4928   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4929     {
4930       cp_lexer_consume_token (parser->lexer);
4931       int op = cp_parser_fold_operator (parser);
4932       if (op == ERROR_MARK)
4933         {
4934           cp_parser_error (parser, "expected binary operator");
4935           return error_mark_node;
4936         }
4937
4938       tree expr = cp_parser_cast_expression (parser, false, false,
4939                                              false, &pidk);
4940       if (expr == error_mark_node)
4941         return error_mark_node;
4942       return finish_left_unary_fold_expr (expr, op);
4943     }
4944
4945   const cp_token* token = cp_lexer_peek_token (parser->lexer);
4946   int op = cp_parser_fold_operator (parser);
4947   if (op == ERROR_MARK)
4948     {
4949       cp_parser_error (parser, "expected binary operator");
4950       return error_mark_node;
4951     }
4952
4953   if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4954     {
4955       cp_parser_error (parser, "expected ...");
4956       return error_mark_node;
4957     }
4958   cp_lexer_consume_token (parser->lexer);
4959
4960   /* The operands of a fold-expression are cast-expressions, so binary or
4961      conditional expressions are not allowed.  We check this here to avoid
4962      tentative parsing.  */
4963   if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4964     /* OK, the expression was parenthesized.  */;
4965   else if (is_binary_op (TREE_CODE (expr1)))
4966     error_at (location_of (expr1),
4967               "binary expression in operand of fold-expression");
4968   else if (TREE_CODE (expr1) == COND_EXPR
4969            || (REFERENCE_REF_P (expr1)
4970                && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
4971     error_at (location_of (expr1),
4972               "conditional expression in operand of fold-expression");
4973
4974   // Right fold.
4975   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4976     return finish_right_unary_fold_expr (expr1, op);
4977
4978   if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4979     {
4980       cp_parser_error (parser, "mismatched operator in fold-expression");
4981       return error_mark_node;
4982     }
4983   cp_lexer_consume_token (parser->lexer);
4984
4985   // Binary left or right fold.
4986   tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4987   if (expr2 == error_mark_node)
4988     return error_mark_node;
4989   return finish_binary_fold_expr (expr1, expr2, op);
4990 }
4991
4992 /* Parse a primary-expression.
4993
4994    primary-expression:
4995      literal
4996      this
4997      ( expression )
4998      id-expression
4999      lambda-expression (C++11)
5000
5001    GNU Extensions:
5002
5003    primary-expression:
5004      ( compound-statement )
5005      __builtin_va_arg ( assignment-expression , type-id )
5006      __builtin_offsetof ( type-id , offsetof-expression )
5007
5008    C++ Extensions:
5009      __has_nothrow_assign ( type-id )   
5010      __has_nothrow_constructor ( type-id )
5011      __has_nothrow_copy ( type-id )
5012      __has_trivial_assign ( type-id )   
5013      __has_trivial_constructor ( type-id )
5014      __has_trivial_copy ( type-id )
5015      __has_trivial_destructor ( type-id )
5016      __has_virtual_destructor ( type-id )     
5017      __is_abstract ( type-id )
5018      __is_base_of ( type-id , type-id )
5019      __is_class ( type-id )
5020      __is_empty ( type-id )
5021      __is_enum ( type-id )
5022      __is_final ( type-id )
5023      __is_literal_type ( type-id )
5024      __is_pod ( type-id )
5025      __is_polymorphic ( type-id )
5026      __is_std_layout ( type-id )
5027      __is_trivial ( type-id )
5028      __is_union ( type-id )
5029
5030    Objective-C++ Extension:
5031
5032    primary-expression:
5033      objc-expression
5034
5035    literal:
5036      __null
5037
5038    ADDRESS_P is true iff this expression was immediately preceded by
5039    "&" and therefore might denote a pointer-to-member.  CAST_P is true
5040    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
5041    true iff this expression is a template argument.
5042
5043    Returns a representation of the expression.  Upon return, *IDK
5044    indicates what kind of id-expression (if any) was present.  */
5045
5046 static cp_expr
5047 cp_parser_primary_expression (cp_parser *parser,
5048                               bool address_p,
5049                               bool cast_p,
5050                               bool template_arg_p,
5051                               bool decltype_p,
5052                               cp_id_kind *idk)
5053 {
5054   cp_token *token = NULL;
5055
5056   /* Assume the primary expression is not an id-expression.  */
5057   *idk = CP_ID_KIND_NONE;
5058
5059   /* Peek at the next token.  */
5060   token = cp_lexer_peek_token (parser->lexer);
5061   switch ((int) token->type)
5062     {
5063       /* literal:
5064            integer-literal
5065            character-literal
5066            floating-literal
5067            string-literal
5068            boolean-literal
5069            pointer-literal
5070            user-defined-literal  */
5071     case CPP_CHAR:
5072     case CPP_CHAR16:
5073     case CPP_CHAR32:
5074     case CPP_WCHAR:
5075     case CPP_UTF8CHAR:
5076     case CPP_NUMBER:
5077     case CPP_PREPARSED_EXPR:
5078       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5079         return cp_parser_userdef_numeric_literal (parser);
5080       token = cp_lexer_consume_token (parser->lexer);
5081       if (TREE_CODE (token->u.value) == FIXED_CST)
5082         {
5083           error_at (token->location,
5084                     "fixed-point types not supported in C++");
5085           return error_mark_node;
5086         }
5087       /* Floating-point literals are only allowed in an integral
5088          constant expression if they are cast to an integral or
5089          enumeration type.  */
5090       if (TREE_CODE (token->u.value) == REAL_CST
5091           && parser->integral_constant_expression_p
5092           && pedantic)
5093         {
5094           /* CAST_P will be set even in invalid code like "int(2.7 +
5095              ...)".   Therefore, we have to check that the next token
5096              is sure to end the cast.  */
5097           if (cast_p)
5098             {
5099               cp_token *next_token;
5100
5101               next_token = cp_lexer_peek_token (parser->lexer);
5102               if (/* The comma at the end of an
5103                      enumerator-definition.  */
5104                   next_token->type != CPP_COMMA
5105                   /* The curly brace at the end of an enum-specifier.  */
5106                   && next_token->type != CPP_CLOSE_BRACE
5107                   /* The end of a statement.  */
5108                   && next_token->type != CPP_SEMICOLON
5109                   /* The end of the cast-expression.  */
5110                   && next_token->type != CPP_CLOSE_PAREN
5111                   /* The end of an array bound.  */
5112                   && next_token->type != CPP_CLOSE_SQUARE
5113                   /* The closing ">" in a template-argument-list.  */
5114                   && (next_token->type != CPP_GREATER
5115                       || parser->greater_than_is_operator_p)
5116                   /* C++0x only: A ">>" treated like two ">" tokens,
5117                      in a template-argument-list.  */
5118                   && (next_token->type != CPP_RSHIFT
5119                       || (cxx_dialect == cxx98)
5120                       || parser->greater_than_is_operator_p))
5121                 cast_p = false;
5122             }
5123
5124           /* If we are within a cast, then the constraint that the
5125              cast is to an integral or enumeration type will be
5126              checked at that point.  If we are not within a cast, then
5127              this code is invalid.  */
5128           if (!cast_p)
5129             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5130         }
5131       return cp_expr (token->u.value, token->location);
5132
5133     case CPP_CHAR_USERDEF:
5134     case CPP_CHAR16_USERDEF:
5135     case CPP_CHAR32_USERDEF:
5136     case CPP_WCHAR_USERDEF:
5137     case CPP_UTF8CHAR_USERDEF:
5138       return cp_parser_userdef_char_literal (parser);
5139
5140     case CPP_STRING:
5141     case CPP_STRING16:
5142     case CPP_STRING32:
5143     case CPP_WSTRING:
5144     case CPP_UTF8STRING:
5145     case CPP_STRING_USERDEF:
5146     case CPP_STRING16_USERDEF:
5147     case CPP_STRING32_USERDEF:
5148     case CPP_WSTRING_USERDEF:
5149     case CPP_UTF8STRING_USERDEF:
5150       /* ??? Should wide strings be allowed when parser->translate_strings_p
5151          is false (i.e. in attributes)?  If not, we can kill the third
5152          argument to cp_parser_string_literal.  */
5153       return cp_parser_string_literal (parser,
5154                                        parser->translate_strings_p,
5155                                        true);
5156
5157     case CPP_OPEN_PAREN:
5158       /* If we see `( { ' then we are looking at the beginning of
5159          a GNU statement-expression.  */
5160       if (cp_parser_allow_gnu_extensions_p (parser)
5161           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5162         {
5163           /* Statement-expressions are not allowed by the standard.  */
5164           pedwarn (token->location, OPT_Wpedantic,
5165                    "ISO C++ forbids braced-groups within expressions");
5166
5167           /* And they're not allowed outside of a function-body; you
5168              cannot, for example, write:
5169
5170              int i = ({ int j = 3; j + 1; });
5171
5172              at class or namespace scope.  */
5173           if (!parser->in_function_body
5174               || parser->in_template_argument_list_p)
5175             {
5176               error_at (token->location,
5177                         "statement-expressions are not allowed outside "
5178                         "functions nor in template-argument lists");
5179               cp_parser_skip_to_end_of_block_or_statement (parser);
5180               if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5181                 cp_lexer_consume_token (parser->lexer);
5182               return error_mark_node;
5183             }
5184           else
5185             return cp_parser_statement_expr (parser);
5186         }
5187       /* Otherwise it's a normal parenthesized expression.  */
5188       {
5189         cp_expr expr;
5190         bool saved_greater_than_is_operator_p;
5191
5192         location_t open_paren_loc = token->location;
5193
5194         /* Consume the `('.  */
5195         matching_parens parens;
5196         parens.consume_open (parser);
5197         /* Within a parenthesized expression, a `>' token is always
5198            the greater-than operator.  */
5199         saved_greater_than_is_operator_p
5200           = parser->greater_than_is_operator_p;
5201         parser->greater_than_is_operator_p = true;
5202
5203         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5204           /* Left fold expression. */
5205           expr = NULL_TREE;
5206         else
5207           /* Parse the parenthesized expression.  */
5208           expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5209
5210         token = cp_lexer_peek_token (parser->lexer);
5211         if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5212           {
5213             expr = cp_parser_fold_expression (parser, expr);
5214             if (expr != error_mark_node
5215                 && cxx_dialect < cxx17
5216                 && !in_system_header_at (input_location))
5217               pedwarn (input_location, 0, "fold-expressions only available "
5218                        "with -std=c++17 or -std=gnu++17");
5219           }
5220         else
5221           /* Let the front end know that this expression was
5222              enclosed in parentheses. This matters in case, for
5223              example, the expression is of the form `A::B', since
5224              `&A::B' might be a pointer-to-member, but `&(A::B)' is
5225              not.  */
5226           expr = finish_parenthesized_expr (expr);
5227
5228         /* DR 705: Wrapping an unqualified name in parentheses
5229            suppresses arg-dependent lookup.  We want to pass back
5230            CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5231            (c++/37862), but none of the others.  */
5232         if (*idk != CP_ID_KIND_QUALIFIED)
5233           *idk = CP_ID_KIND_NONE;
5234
5235         /* The `>' token might be the end of a template-id or
5236            template-parameter-list now.  */
5237         parser->greater_than_is_operator_p
5238           = saved_greater_than_is_operator_p;
5239
5240         /* Consume the `)'.  */
5241         token = cp_lexer_peek_token (parser->lexer);
5242         location_t close_paren_loc = token->location;
5243         expr.set_range (open_paren_loc, close_paren_loc);
5244         if (!parens.require_close (parser)
5245             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5246           cp_parser_skip_to_end_of_statement (parser);
5247
5248         return expr;
5249       }
5250
5251     case CPP_OPEN_SQUARE:
5252       {
5253         if (c_dialect_objc ())
5254           {
5255             /* We might have an Objective-C++ message. */
5256             cp_parser_parse_tentatively (parser);
5257             tree msg = cp_parser_objc_message_expression (parser);
5258             /* If that works out, we're done ... */
5259             if (cp_parser_parse_definitely (parser))
5260               return msg;
5261             /* ... else, fall though to see if it's a lambda.  */
5262           }
5263         cp_expr lam = cp_parser_lambda_expression (parser);
5264         /* Don't warn about a failed tentative parse.  */
5265         if (cp_parser_error_occurred (parser))
5266           return error_mark_node;
5267         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5268         return lam;
5269       }
5270
5271     case CPP_OBJC_STRING:
5272       if (c_dialect_objc ())
5273         /* We have an Objective-C++ string literal. */
5274         return cp_parser_objc_expression (parser);
5275       cp_parser_error (parser, "expected primary-expression");
5276       return error_mark_node;
5277
5278     case CPP_KEYWORD:
5279       switch (token->keyword)
5280         {
5281           /* These two are the boolean literals.  */
5282         case RID_TRUE:
5283           cp_lexer_consume_token (parser->lexer);
5284           return cp_expr (boolean_true_node, token->location);
5285         case RID_FALSE:
5286           cp_lexer_consume_token (parser->lexer);
5287           return cp_expr (boolean_false_node, token->location);
5288
5289           /* The `__null' literal.  */
5290         case RID_NULL:
5291           cp_lexer_consume_token (parser->lexer);
5292           return cp_expr (null_node, token->location);
5293
5294           /* The `nullptr' literal.  */
5295         case RID_NULLPTR:
5296           cp_lexer_consume_token (parser->lexer);
5297           return cp_expr (nullptr_node, token->location);
5298
5299           /* Recognize the `this' keyword.  */
5300         case RID_THIS:
5301           cp_lexer_consume_token (parser->lexer);
5302           if (parser->local_variables_forbidden_p)
5303             {
5304               error_at (token->location,
5305                         "%<this%> may not be used in this context");
5306               return error_mark_node;
5307             }
5308           /* Pointers cannot appear in constant-expressions.  */
5309           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5310             return error_mark_node;
5311           return cp_expr (finish_this_expr (), token->location);
5312
5313           /* The `operator' keyword can be the beginning of an
5314              id-expression.  */
5315         case RID_OPERATOR:
5316           goto id_expression;
5317
5318         case RID_FUNCTION_NAME:
5319         case RID_PRETTY_FUNCTION_NAME:
5320         case RID_C99_FUNCTION_NAME:
5321           {
5322             non_integral_constant name;
5323
5324             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5325                __func__ are the names of variables -- but they are
5326                treated specially.  Therefore, they are handled here,
5327                rather than relying on the generic id-expression logic
5328                below.  Grammatically, these names are id-expressions.
5329
5330                Consume the token.  */
5331             token = cp_lexer_consume_token (parser->lexer);
5332
5333             switch (token->keyword)
5334               {
5335               case RID_FUNCTION_NAME:
5336                 name = NIC_FUNC_NAME;
5337                 break;
5338               case RID_PRETTY_FUNCTION_NAME:
5339                 name = NIC_PRETTY_FUNC;
5340                 break;
5341               case RID_C99_FUNCTION_NAME:
5342                 name = NIC_C99_FUNC;
5343                 break;
5344               default:
5345                 gcc_unreachable ();
5346               }
5347
5348             if (cp_parser_non_integral_constant_expression (parser, name))
5349               return error_mark_node;
5350
5351             /* Look up the name.  */
5352             return finish_fname (token->u.value);
5353           }
5354
5355         case RID_VA_ARG:
5356           {
5357             tree expression;
5358             tree type;
5359             source_location type_location;
5360             location_t start_loc
5361               = cp_lexer_peek_token (parser->lexer)->location;
5362             /* The `__builtin_va_arg' construct is used to handle
5363                `va_arg'.  Consume the `__builtin_va_arg' token.  */
5364             cp_lexer_consume_token (parser->lexer);
5365             /* Look for the opening `('.  */
5366             matching_parens parens;
5367             parens.require_open (parser);
5368             /* Now, parse the assignment-expression.  */
5369             expression = cp_parser_assignment_expression (parser);
5370             /* Look for the `,'.  */
5371             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5372             type_location = cp_lexer_peek_token (parser->lexer)->location;
5373             /* Parse the type-id.  */
5374             {
5375               type_id_in_expr_sentinel s (parser);
5376               type = cp_parser_type_id (parser);
5377             }
5378             /* Look for the closing `)'.  */
5379             location_t finish_loc
5380               = cp_lexer_peek_token (parser->lexer)->location;
5381             parens.require_close (parser);
5382             /* Using `va_arg' in a constant-expression is not
5383                allowed.  */
5384             if (cp_parser_non_integral_constant_expression (parser,
5385                                                             NIC_VA_ARG))
5386               return error_mark_node;
5387             /* Construct a location of the form:
5388                  __builtin_va_arg (v, int)
5389                  ~~~~~~~~~~~~~~~~~~~~~^~~~
5390                with the caret at the type, ranging from the start of the
5391                "__builtin_va_arg" token to the close paren.  */
5392             location_t combined_loc
5393               = make_location (type_location, start_loc, finish_loc);
5394             return build_x_va_arg (combined_loc, expression, type);
5395           }
5396
5397         case RID_OFFSETOF:
5398           return cp_parser_builtin_offsetof (parser);
5399
5400         case RID_HAS_NOTHROW_ASSIGN:
5401         case RID_HAS_NOTHROW_CONSTRUCTOR:
5402         case RID_HAS_NOTHROW_COPY:        
5403         case RID_HAS_TRIVIAL_ASSIGN:
5404         case RID_HAS_TRIVIAL_CONSTRUCTOR:
5405         case RID_HAS_TRIVIAL_COPY:        
5406         case RID_HAS_TRIVIAL_DESTRUCTOR:
5407         case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5408         case RID_HAS_VIRTUAL_DESTRUCTOR:
5409         case RID_IS_ABSTRACT:
5410         case RID_IS_AGGREGATE:
5411         case RID_IS_BASE_OF:
5412         case RID_IS_CLASS:
5413         case RID_IS_EMPTY:
5414         case RID_IS_ENUM:
5415         case RID_IS_FINAL:
5416         case RID_IS_LITERAL_TYPE:
5417         case RID_IS_POD:
5418         case RID_IS_POLYMORPHIC:
5419         case RID_IS_SAME_AS:
5420         case RID_IS_STD_LAYOUT:
5421         case RID_IS_TRIVIAL:
5422         case RID_IS_TRIVIALLY_ASSIGNABLE:
5423         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5424         case RID_IS_TRIVIALLY_COPYABLE:
5425         case RID_IS_UNION:
5426         case RID_IS_ASSIGNABLE:
5427         case RID_IS_CONSTRUCTIBLE:
5428           return cp_parser_trait_expr (parser, token->keyword);
5429
5430         // C++ concepts
5431         case RID_REQUIRES:
5432           return cp_parser_requires_expression (parser);
5433
5434         /* Objective-C++ expressions.  */
5435         case RID_AT_ENCODE:
5436         case RID_AT_PROTOCOL:
5437         case RID_AT_SELECTOR:
5438           return cp_parser_objc_expression (parser);
5439
5440         case RID_TEMPLATE:
5441           if (parser->in_function_body
5442               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5443                   == CPP_LESS))
5444             {
5445               error_at (token->location,
5446                         "a template declaration cannot appear at block scope");
5447               cp_parser_skip_to_end_of_block_or_statement (parser);
5448               return error_mark_node;
5449             }
5450           /* FALLTHRU */
5451         default:
5452           cp_parser_error (parser, "expected primary-expression");
5453           return error_mark_node;
5454         }
5455
5456       /* An id-expression can start with either an identifier, a
5457          `::' as the beginning of a qualified-id, or the "operator"
5458          keyword.  */
5459     case CPP_NAME:
5460     case CPP_SCOPE:
5461     case CPP_TEMPLATE_ID:
5462     case CPP_NESTED_NAME_SPECIFIER:
5463       {
5464       id_expression:
5465         cp_expr id_expression;
5466         cp_expr decl;
5467         const char *error_msg;
5468         bool template_p;
5469         bool done;
5470         cp_token *id_expr_token;
5471
5472         /* Parse the id-expression.  */
5473         id_expression
5474           = cp_parser_id_expression (parser,
5475                                      /*template_keyword_p=*/false,
5476                                      /*check_dependency_p=*/true,
5477                                      &template_p,
5478                                      /*declarator_p=*/false,
5479                                      /*optional_p=*/false);
5480         if (id_expression == error_mark_node)
5481           return error_mark_node;
5482         id_expr_token = token;
5483         token = cp_lexer_peek_token (parser->lexer);
5484         done = (token->type != CPP_OPEN_SQUARE
5485                 && token->type != CPP_OPEN_PAREN
5486                 && token->type != CPP_DOT
5487                 && token->type != CPP_DEREF
5488                 && token->type != CPP_PLUS_PLUS
5489                 && token->type != CPP_MINUS_MINUS);
5490         /* If we have a template-id, then no further lookup is
5491            required.  If the template-id was for a template-class, we
5492            will sometimes have a TYPE_DECL at this point.  */
5493         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5494                  || TREE_CODE (id_expression) == TYPE_DECL)
5495           decl = id_expression;
5496         /* Look up the name.  */
5497         else
5498           {
5499             tree ambiguous_decls;
5500
5501             /* If we already know that this lookup is ambiguous, then
5502                we've already issued an error message; there's no reason
5503                to check again.  */
5504             if (id_expr_token->type == CPP_NAME
5505                 && id_expr_token->error_reported)
5506               {
5507                 cp_parser_simulate_error (parser);
5508                 return error_mark_node;
5509               }
5510
5511             decl = cp_parser_lookup_name (parser, id_expression,
5512                                           none_type,
5513                                           template_p,
5514                                           /*is_namespace=*/false,
5515                                           /*check_dependency=*/true,
5516                                           &ambiguous_decls,
5517                                           id_expr_token->location);
5518             /* If the lookup was ambiguous, an error will already have
5519                been issued.  */
5520             if (ambiguous_decls)
5521               return error_mark_node;
5522
5523             /* In Objective-C++, we may have an Objective-C 2.0
5524                dot-syntax for classes here.  */
5525             if (c_dialect_objc ()
5526                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5527                 && TREE_CODE (decl) == TYPE_DECL
5528                 && objc_is_class_name (decl))
5529               {
5530                 tree component;
5531                 cp_lexer_consume_token (parser->lexer);
5532                 component = cp_parser_identifier (parser);
5533                 if (component == error_mark_node)
5534                   return error_mark_node;
5535
5536                 tree result = objc_build_class_component_ref (id_expression,
5537                                                               component);
5538                 /* Build a location of the form:
5539                      expr.component
5540                      ~~~~~^~~~~~~~~
5541                    with caret at the start of the component name (at
5542                    input_location), ranging from the start of the id_expression
5543                    to the end of the component name.  */
5544                 location_t combined_loc
5545                   = make_location (input_location, id_expression.get_start (),
5546                                    get_finish (input_location));
5547                 protected_set_expr_location (result, combined_loc);
5548                 return result;
5549               }
5550
5551             /* In Objective-C++, an instance variable (ivar) may be preferred
5552                to whatever cp_parser_lookup_name() found.
5553                Call objc_lookup_ivar.  To avoid exposing cp_expr to the
5554                rest of c-family, we have to do a little extra work to preserve
5555                any location information in cp_expr "decl".  Given that
5556                objc_lookup_ivar is implemented in "c-family" and "objc", we
5557                have a trip through the pure "tree" type, rather than cp_expr.
5558                Naively copying it back to "decl" would implicitly give the
5559                new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5560                store an EXPR_LOCATION.  Hence we only update "decl" (and
5561                hence its location_t) if we get back a different tree node.  */
5562             tree decl_tree = objc_lookup_ivar (decl.get_value (),
5563                                                id_expression);
5564             if (decl_tree != decl.get_value ())
5565               decl = cp_expr (decl_tree);
5566
5567             /* If name lookup gives us a SCOPE_REF, then the
5568                qualifying scope was dependent.  */
5569             if (TREE_CODE (decl) == SCOPE_REF)
5570               {
5571                 /* At this point, we do not know if DECL is a valid
5572                    integral constant expression.  We assume that it is
5573                    in fact such an expression, so that code like:
5574
5575                       template <int N> struct A {
5576                         int a[B<N>::i];
5577                       };
5578                      
5579                    is accepted.  At template-instantiation time, we
5580                    will check that B<N>::i is actually a constant.  */
5581                 return decl;
5582               }
5583             /* Check to see if DECL is a local variable in a context
5584                where that is forbidden.  */
5585             if (parser->local_variables_forbidden_p
5586                 && local_variable_p (decl))
5587               {
5588                 /* It might be that we only found DECL because we are
5589                    trying to be generous with pre-ISO scoping rules.
5590                    For example, consider:
5591
5592                      int i;
5593                      void g() {
5594                        for (int i = 0; i < 10; ++i) {}
5595                        extern void f(int j = i);
5596                      }
5597
5598                    Here, name look up will originally find the out
5599                    of scope `i'.  We need to issue a warning message,
5600                    but then use the global `i'.  */
5601                 decl = check_for_out_of_scope_variable (decl);
5602                 if (local_variable_p (decl))
5603                   {
5604                     error_at (id_expr_token->location,
5605                               "local variable %qD may not appear in this context",
5606                               decl.get_value ());
5607                     return error_mark_node;
5608                   }
5609               }
5610           }
5611
5612         if (processing_template_decl && is_overloaded_fn (decl))
5613           lookup_keep (get_fns (decl), true);
5614
5615         decl = (finish_id_expression
5616                 (id_expression, decl, parser->scope,
5617                  idk,
5618                  parser->integral_constant_expression_p,
5619                  parser->allow_non_integral_constant_expression_p,
5620                  &parser->non_integral_constant_expression_p,
5621                  template_p, done, address_p,
5622                  template_arg_p,
5623                  &error_msg,
5624                  id_expression.get_location ()));
5625         if (error_msg)
5626           cp_parser_error (parser, error_msg);
5627         decl.set_location (id_expr_token->location);
5628         return decl;
5629       }
5630
5631       /* Anything else is an error.  */
5632     default:
5633       cp_parser_error (parser, "expected primary-expression");
5634       return error_mark_node;
5635     }
5636 }
5637
5638 static inline cp_expr
5639 cp_parser_primary_expression (cp_parser *parser,
5640                               bool address_p,
5641                               bool cast_p,
5642                               bool template_arg_p,
5643                               cp_id_kind *idk)
5644 {
5645   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5646                                        /*decltype*/false, idk);
5647 }
5648
5649 /* Parse an id-expression.
5650
5651    id-expression:
5652      unqualified-id
5653      qualified-id
5654
5655    qualified-id:
5656      :: [opt] nested-name-specifier template [opt] unqualified-id
5657      :: identifier
5658      :: operator-function-id
5659      :: template-id
5660
5661    Return a representation of the unqualified portion of the
5662    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
5663    a `::' or nested-name-specifier.
5664
5665    Often, if the id-expression was a qualified-id, the caller will
5666    want to make a SCOPE_REF to represent the qualified-id.  This
5667    function does not do this in order to avoid wastefully creating
5668    SCOPE_REFs when they are not required.
5669
5670    If TEMPLATE_KEYWORD_P is true, then we have just seen the
5671    `template' keyword.
5672
5673    If CHECK_DEPENDENCY_P is false, then names are looked up inside
5674    uninstantiated templates.
5675
5676    If *TEMPLATE_P is non-NULL, it is set to true iff the
5677    `template' keyword is used to explicitly indicate that the entity
5678    named is a template.
5679
5680    If DECLARATOR_P is true, the id-expression is appearing as part of
5681    a declarator, rather than as part of an expression.  */
5682
5683 static cp_expr
5684 cp_parser_id_expression (cp_parser *parser,
5685                          bool template_keyword_p,
5686                          bool check_dependency_p,
5687                          bool *template_p,
5688                          bool declarator_p,
5689                          bool optional_p)
5690 {
5691   bool global_scope_p;
5692   bool nested_name_specifier_p;
5693
5694   /* Assume the `template' keyword was not used.  */
5695   if (template_p)
5696     *template_p = template_keyword_p;
5697
5698   /* Look for the optional `::' operator.  */
5699   global_scope_p
5700     = (!template_keyword_p
5701        && (cp_parser_global_scope_opt (parser,
5702                                        /*current_scope_valid_p=*/false)
5703            != NULL_TREE));
5704
5705   /* Look for the optional nested-name-specifier.  */
5706   nested_name_specifier_p
5707     = (cp_parser_nested_name_specifier_opt (parser,
5708                                             /*typename_keyword_p=*/false,
5709                                             check_dependency_p,
5710                                             /*type_p=*/false,
5711                                             declarator_p,
5712                                             template_keyword_p)
5713        != NULL_TREE);
5714
5715   /* If there is a nested-name-specifier, then we are looking at
5716      the first qualified-id production.  */
5717   if (nested_name_specifier_p)
5718     {
5719       tree saved_scope;
5720       tree saved_object_scope;
5721       tree saved_qualifying_scope;
5722       cp_expr unqualified_id;
5723       bool is_template;
5724
5725       /* See if the next token is the `template' keyword.  */
5726       if (!template_p)
5727         template_p = &is_template;
5728       *template_p = cp_parser_optional_template_keyword (parser);
5729       /* Name lookup we do during the processing of the
5730          unqualified-id might obliterate SCOPE.  */
5731       saved_scope = parser->scope;
5732       saved_object_scope = parser->object_scope;
5733       saved_qualifying_scope = parser->qualifying_scope;
5734       /* Process the final unqualified-id.  */
5735       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5736                                                  check_dependency_p,
5737                                                  declarator_p,
5738                                                  /*optional_p=*/false);
5739       /* Restore the SAVED_SCOPE for our caller.  */
5740       parser->scope = saved_scope;
5741       parser->object_scope = saved_object_scope;
5742       parser->qualifying_scope = saved_qualifying_scope;
5743
5744       return unqualified_id;
5745     }
5746   /* Otherwise, if we are in global scope, then we are looking at one
5747      of the other qualified-id productions.  */
5748   else if (global_scope_p)
5749     {
5750       cp_token *token;
5751       tree id;
5752
5753       /* Peek at the next token.  */
5754       token = cp_lexer_peek_token (parser->lexer);
5755
5756       /* If it's an identifier, and the next token is not a "<", then
5757          we can avoid the template-id case.  This is an optimization
5758          for this common case.  */
5759       if (token->type == CPP_NAME
5760           && !cp_parser_nth_token_starts_template_argument_list_p
5761                (parser, 2))
5762         return cp_parser_identifier (parser);
5763
5764       cp_parser_parse_tentatively (parser);
5765       /* Try a template-id.  */
5766       id = cp_parser_template_id (parser,
5767                                   /*template_keyword_p=*/false,
5768                                   /*check_dependency_p=*/true,
5769                                   none_type,
5770                                   declarator_p);
5771       /* If that worked, we're done.  */
5772       if (cp_parser_parse_definitely (parser))
5773         return id;
5774
5775       /* Peek at the next token.  (Changes in the token buffer may
5776          have invalidated the pointer obtained above.)  */
5777       token = cp_lexer_peek_token (parser->lexer);
5778
5779       switch (token->type)
5780         {
5781         case CPP_NAME:
5782           return cp_parser_identifier (parser);
5783
5784         case CPP_KEYWORD:
5785           if (token->keyword == RID_OPERATOR)
5786             return cp_parser_operator_function_id (parser);
5787           /* Fall through.  */
5788
5789         default:
5790           cp_parser_error (parser, "expected id-expression");
5791           return error_mark_node;
5792         }
5793     }
5794   else
5795     return cp_parser_unqualified_id (parser, template_keyword_p,
5796                                      /*check_dependency_p=*/true,
5797                                      declarator_p,
5798                                      optional_p);
5799 }
5800
5801 /* Parse an unqualified-id.
5802
5803    unqualified-id:
5804      identifier
5805      operator-function-id
5806      conversion-function-id
5807      ~ class-name
5808      template-id
5809
5810    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5811    keyword, in a construct like `A::template ...'.
5812
5813    Returns a representation of unqualified-id.  For the `identifier'
5814    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
5815    production a BIT_NOT_EXPR is returned; the operand of the
5816    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
5817    other productions, see the documentation accompanying the
5818    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
5819    names are looked up in uninstantiated templates.  If DECLARATOR_P
5820    is true, the unqualified-id is appearing as part of a declarator,
5821    rather than as part of an expression.  */
5822
5823 static cp_expr
5824 cp_parser_unqualified_id (cp_parser* parser,
5825                           bool template_keyword_p,
5826                           bool check_dependency_p,
5827                           bool declarator_p,
5828                           bool optional_p)
5829 {
5830   cp_token *token;
5831
5832   /* Peek at the next token.  */
5833   token = cp_lexer_peek_token (parser->lexer);
5834
5835   switch ((int) token->type)
5836     {
5837     case CPP_NAME:
5838       {
5839         tree id;
5840
5841         /* We don't know yet whether or not this will be a
5842            template-id.  */
5843         cp_parser_parse_tentatively (parser);
5844         /* Try a template-id.  */
5845         id = cp_parser_template_id (parser, template_keyword_p,
5846                                     check_dependency_p,
5847                                     none_type,
5848                                     declarator_p);
5849         /* If it worked, we're done.  */
5850         if (cp_parser_parse_definitely (parser))
5851           return id;
5852         /* Otherwise, it's an ordinary identifier.  */
5853         return cp_parser_identifier (parser);
5854       }
5855
5856     case CPP_TEMPLATE_ID:
5857       return cp_parser_template_id (parser, template_keyword_p,
5858                                     check_dependency_p,
5859                                     none_type,
5860                                     declarator_p);
5861
5862     case CPP_COMPL:
5863       {
5864         tree type_decl;
5865         tree qualifying_scope;
5866         tree object_scope;
5867         tree scope;
5868         bool done;
5869
5870         /* Consume the `~' token.  */
5871         cp_lexer_consume_token (parser->lexer);
5872         /* Parse the class-name.  The standard, as written, seems to
5873            say that:
5874
5875              template <typename T> struct S { ~S (); };
5876              template <typename T> S<T>::~S() {}
5877
5878            is invalid, since `~' must be followed by a class-name, but
5879            `S<T>' is dependent, and so not known to be a class.
5880            That's not right; we need to look in uninstantiated
5881            templates.  A further complication arises from:
5882
5883              template <typename T> void f(T t) {
5884                t.T::~T();
5885              }
5886
5887            Here, it is not possible to look up `T' in the scope of `T'
5888            itself.  We must look in both the current scope, and the
5889            scope of the containing complete expression.
5890
5891            Yet another issue is:
5892
5893              struct S {
5894                int S;
5895                ~S();
5896              };
5897
5898              S::~S() {}
5899
5900            The standard does not seem to say that the `S' in `~S'
5901            should refer to the type `S' and not the data member
5902            `S::S'.  */
5903
5904         /* DR 244 says that we look up the name after the "~" in the
5905            same scope as we looked up the qualifying name.  That idea
5906            isn't fully worked out; it's more complicated than that.  */
5907         scope = parser->scope;
5908         object_scope = parser->object_scope;
5909         qualifying_scope = parser->qualifying_scope;
5910
5911         /* Check for invalid scopes.  */
5912         if (scope == error_mark_node)
5913           {
5914             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5915               cp_lexer_consume_token (parser->lexer);
5916             return error_mark_node;
5917           }
5918         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5919           {
5920             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5921               error_at (token->location,
5922                         "scope %qT before %<~%> is not a class-name",
5923                         scope);
5924             cp_parser_simulate_error (parser);
5925             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5926               cp_lexer_consume_token (parser->lexer);
5927             return error_mark_node;
5928           }
5929         gcc_assert (!scope || TYPE_P (scope));
5930
5931         /* If the name is of the form "X::~X" it's OK even if X is a
5932            typedef.  */
5933         token = cp_lexer_peek_token (parser->lexer);
5934         if (scope
5935             && token->type == CPP_NAME
5936             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5937                 != CPP_LESS)
5938             && (token->u.value == TYPE_IDENTIFIER (scope)
5939                 || (CLASS_TYPE_P (scope)
5940                     && constructor_name_p (token->u.value, scope))))
5941           {
5942             cp_lexer_consume_token (parser->lexer);
5943             return build_nt (BIT_NOT_EXPR, scope);
5944           }
5945
5946         /* ~auto means the destructor of whatever the object is.  */
5947         if (cp_parser_is_keyword (token, RID_AUTO))
5948           {
5949             if (cxx_dialect < cxx14)
5950               pedwarn (input_location, 0,
5951                        "%<~auto%> only available with "
5952                        "-std=c++14 or -std=gnu++14");
5953             cp_lexer_consume_token (parser->lexer);
5954             return build_nt (BIT_NOT_EXPR, make_auto ());
5955           }
5956
5957         /* If there was an explicit qualification (S::~T), first look
5958            in the scope given by the qualification (i.e., S).
5959
5960            Note: in the calls to cp_parser_class_name below we pass
5961            typename_type so that lookup finds the injected-class-name
5962            rather than the constructor.  */
5963         done = false;
5964         type_decl = NULL_TREE;
5965         if (scope)
5966           {
5967             cp_parser_parse_tentatively (parser);
5968             type_decl = cp_parser_class_name (parser,
5969                                               /*typename_keyword_p=*/false,
5970                                               /*template_keyword_p=*/false,
5971                                               typename_type,
5972                                               /*check_dependency=*/false,
5973                                               /*class_head_p=*/false,
5974                                               declarator_p);
5975             if (cp_parser_parse_definitely (parser))
5976               done = true;
5977           }
5978         /* In "N::S::~S", look in "N" as well.  */
5979         if (!done && scope && qualifying_scope)
5980           {
5981             cp_parser_parse_tentatively (parser);
5982             parser->scope = qualifying_scope;
5983             parser->object_scope = NULL_TREE;
5984             parser->qualifying_scope = NULL_TREE;
5985             type_decl
5986               = cp_parser_class_name (parser,
5987                                       /*typename_keyword_p=*/false,
5988                                       /*template_keyword_p=*/false,
5989                                       typename_type,
5990                                       /*check_dependency=*/false,
5991                                       /*class_head_p=*/false,
5992                                       declarator_p);
5993             if (cp_parser_parse_definitely (parser))
5994               done = true;
5995           }
5996         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5997         else if (!done && object_scope)
5998           {
5999             cp_parser_parse_tentatively (parser);
6000             parser->scope = object_scope;
6001             parser->object_scope = NULL_TREE;
6002             parser->qualifying_scope = NULL_TREE;
6003             type_decl
6004               = cp_parser_class_name (parser,
6005                                       /*typename_keyword_p=*/false,
6006                                       /*template_keyword_p=*/false,
6007                                       typename_type,
6008                                       /*check_dependency=*/false,
6009                                       /*class_head_p=*/false,
6010                                       declarator_p);
6011             if (cp_parser_parse_definitely (parser))
6012               done = true;
6013           }
6014         /* Look in the surrounding context.  */
6015         if (!done)
6016           {
6017             parser->scope = NULL_TREE;
6018             parser->object_scope = NULL_TREE;
6019             parser->qualifying_scope = NULL_TREE;
6020             if (processing_template_decl)
6021               cp_parser_parse_tentatively (parser);
6022             type_decl
6023               = cp_parser_class_name (parser,
6024                                       /*typename_keyword_p=*/false,
6025                                       /*template_keyword_p=*/false,
6026                                       typename_type,
6027                                       /*check_dependency=*/false,
6028                                       /*class_head_p=*/false,
6029                                       declarator_p);
6030             if (processing_template_decl
6031                 && ! cp_parser_parse_definitely (parser))
6032               {
6033                 /* We couldn't find a type with this name.  If we're parsing
6034                    tentatively, fail and try something else.  */
6035                 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6036                   {
6037                     cp_parser_simulate_error (parser);
6038                     return error_mark_node;
6039                   }
6040                 /* Otherwise, accept it and check for a match at instantiation
6041                    time.  */
6042                 type_decl = cp_parser_identifier (parser);
6043                 if (type_decl != error_mark_node)
6044                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6045                 return type_decl;
6046               }
6047           }
6048         /* If an error occurred, assume that the name of the
6049            destructor is the same as the name of the qualifying
6050            class.  That allows us to keep parsing after running
6051            into ill-formed destructor names.  */
6052         if (type_decl == error_mark_node && scope)
6053           return build_nt (BIT_NOT_EXPR, scope);
6054         else if (type_decl == error_mark_node)
6055           return error_mark_node;
6056
6057         /* Check that destructor name and scope match.  */
6058         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6059           {
6060             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6061               error_at (token->location,
6062                         "declaration of %<~%T%> as member of %qT",
6063                         type_decl, scope);
6064             cp_parser_simulate_error (parser);
6065             return error_mark_node;
6066           }
6067
6068         /* [class.dtor]
6069
6070            A typedef-name that names a class shall not be used as the
6071            identifier in the declarator for a destructor declaration.  */
6072         if (declarator_p
6073             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6074             && !DECL_SELF_REFERENCE_P (type_decl)
6075             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6076           error_at (token->location,
6077                     "typedef-name %qD used as destructor declarator",
6078                     type_decl);
6079
6080         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6081       }
6082
6083     case CPP_KEYWORD:
6084       if (token->keyword == RID_OPERATOR)
6085         {
6086           cp_expr id;
6087
6088           /* This could be a template-id, so we try that first.  */
6089           cp_parser_parse_tentatively (parser);
6090           /* Try a template-id.  */
6091           id = cp_parser_template_id (parser, template_keyword_p,
6092                                       /*check_dependency_p=*/true,
6093                                       none_type,
6094                                       declarator_p);
6095           /* If that worked, we're done.  */
6096           if (cp_parser_parse_definitely (parser))
6097             return id;
6098           /* We still don't know whether we're looking at an
6099              operator-function-id or a conversion-function-id.  */
6100           cp_parser_parse_tentatively (parser);
6101           /* Try an operator-function-id.  */
6102           id = cp_parser_operator_function_id (parser);
6103           /* If that didn't work, try a conversion-function-id.  */
6104           if (!cp_parser_parse_definitely (parser))
6105             id = cp_parser_conversion_function_id (parser);
6106
6107           return id;
6108         }
6109       /* Fall through.  */
6110
6111     default:
6112       if (optional_p)
6113         return NULL_TREE;
6114       cp_parser_error (parser, "expected unqualified-id");
6115       return error_mark_node;
6116     }
6117 }
6118
6119 /* Parse an (optional) nested-name-specifier.
6120
6121    nested-name-specifier: [C++98]
6122      class-or-namespace-name :: nested-name-specifier [opt]
6123      class-or-namespace-name :: template nested-name-specifier [opt]
6124
6125    nested-name-specifier: [C++0x]
6126      type-name ::
6127      namespace-name ::
6128      nested-name-specifier identifier ::
6129      nested-name-specifier template [opt] simple-template-id ::
6130
6131    PARSER->SCOPE should be set appropriately before this function is
6132    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6133    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
6134    in name lookups.
6135
6136    Sets PARSER->SCOPE to the class (TYPE) or namespace
6137    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6138    it unchanged if there is no nested-name-specifier.  Returns the new
6139    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6140
6141    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6142    part of a declaration and/or decl-specifier.  */
6143
6144 static tree
6145 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6146                                      bool typename_keyword_p,
6147                                      bool check_dependency_p,
6148                                      bool type_p,
6149                                      bool is_declaration,
6150                                      bool template_keyword_p /* = false */)
6151 {
6152   bool success = false;
6153   cp_token_position start = 0;
6154   cp_token *token;
6155
6156   /* Remember where the nested-name-specifier starts.  */
6157   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6158     {
6159       start = cp_lexer_token_position (parser->lexer, false);
6160       push_deferring_access_checks (dk_deferred);
6161     }
6162
6163   while (true)
6164     {
6165       tree new_scope;
6166       tree old_scope;
6167       tree saved_qualifying_scope;
6168
6169       /* Spot cases that cannot be the beginning of a
6170          nested-name-specifier.  */
6171       token = cp_lexer_peek_token (parser->lexer);
6172
6173       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6174          the already parsed nested-name-specifier.  */
6175       if (token->type == CPP_NESTED_NAME_SPECIFIER)
6176         {
6177           /* Grab the nested-name-specifier and continue the loop.  */
6178           cp_parser_pre_parsed_nested_name_specifier (parser);
6179           /* If we originally encountered this nested-name-specifier
6180              with IS_DECLARATION set to false, we will not have
6181              resolved TYPENAME_TYPEs, so we must do so here.  */
6182           if (is_declaration
6183               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6184             {
6185               new_scope = resolve_typename_type (parser->scope,
6186                                                  /*only_current_p=*/false);
6187               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6188                 parser->scope = new_scope;
6189             }
6190           success = true;
6191           continue;
6192         }
6193
6194       /* Spot cases that cannot be the beginning of a
6195          nested-name-specifier.  On the second and subsequent times
6196          through the loop, we look for the `template' keyword.  */
6197       if (success && token->keyword == RID_TEMPLATE)
6198         ;
6199       /* A template-id can start a nested-name-specifier.  */
6200       else if (token->type == CPP_TEMPLATE_ID)
6201         ;
6202       /* DR 743: decltype can be used in a nested-name-specifier.  */
6203       else if (token_is_decltype (token))
6204         ;
6205       else
6206         {
6207           /* If the next token is not an identifier, then it is
6208              definitely not a type-name or namespace-name.  */
6209           if (token->type != CPP_NAME)
6210             break;
6211           /* If the following token is neither a `<' (to begin a
6212              template-id), nor a `::', then we are not looking at a
6213              nested-name-specifier.  */
6214           token = cp_lexer_peek_nth_token (parser->lexer, 2);
6215
6216           if (token->type == CPP_COLON
6217               && parser->colon_corrects_to_scope_p
6218               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6219             {
6220               gcc_rich_location richloc (token->location);
6221               richloc.add_fixit_replace ("::");
6222               error_at (&richloc,
6223                         "found %<:%> in nested-name-specifier, "
6224                         "expected %<::%>");
6225               token->type = CPP_SCOPE;
6226             }
6227
6228           if (token->type != CPP_SCOPE
6229               && !cp_parser_nth_token_starts_template_argument_list_p
6230                   (parser, 2))
6231             break;
6232         }
6233
6234       /* The nested-name-specifier is optional, so we parse
6235          tentatively.  */
6236       cp_parser_parse_tentatively (parser);
6237
6238       /* Look for the optional `template' keyword, if this isn't the
6239          first time through the loop.  */
6240       if (success)
6241         template_keyword_p = cp_parser_optional_template_keyword (parser);
6242
6243       /* Save the old scope since the name lookup we are about to do
6244          might destroy it.  */
6245       old_scope = parser->scope;
6246       saved_qualifying_scope = parser->qualifying_scope;
6247       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6248          look up names in "X<T>::I" in order to determine that "Y" is
6249          a template.  So, if we have a typename at this point, we make
6250          an effort to look through it.  */
6251       if (is_declaration
6252           && !typename_keyword_p
6253           && parser->scope
6254           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6255         parser->scope = resolve_typename_type (parser->scope,
6256                                                /*only_current_p=*/false);
6257       /* Parse the qualifying entity.  */
6258       new_scope
6259         = cp_parser_qualifying_entity (parser,
6260                                        typename_keyword_p,
6261                                        template_keyword_p,
6262                                        check_dependency_p,
6263                                        type_p,
6264                                        is_declaration);
6265       /* Look for the `::' token.  */
6266       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6267
6268       /* If we found what we wanted, we keep going; otherwise, we're
6269          done.  */
6270       if (!cp_parser_parse_definitely (parser))
6271         {
6272           bool error_p = false;
6273
6274           /* Restore the OLD_SCOPE since it was valid before the
6275              failed attempt at finding the last
6276              class-or-namespace-name.  */
6277           parser->scope = old_scope;
6278           parser->qualifying_scope = saved_qualifying_scope;
6279
6280           /* If the next token is a decltype, and the one after that is a
6281              `::', then the decltype has failed to resolve to a class or
6282              enumeration type.  Give this error even when parsing
6283              tentatively since it can't possibly be valid--and we're going
6284              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6285              won't get another chance.*/
6286           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6287               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6288                   == CPP_SCOPE))
6289             {
6290               token = cp_lexer_consume_token (parser->lexer);
6291               error_at (token->location, "decltype evaluates to %qT, "
6292                         "which is not a class or enumeration type",
6293                         token->u.tree_check_value->value);
6294               parser->scope = error_mark_node;
6295               error_p = true;
6296               /* As below.  */
6297               success = true;
6298               cp_lexer_consume_token (parser->lexer);
6299             }
6300
6301           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6302               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6303             {
6304               /* If we have a non-type template-id followed by ::, it can't
6305                  possibly be valid.  */
6306               token = cp_lexer_peek_token (parser->lexer);
6307               tree tid = token->u.tree_check_value->value;
6308               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6309                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6310                 {
6311                   tree tmpl = NULL_TREE;
6312                   if (is_overloaded_fn (tid))
6313                     {
6314                       tree fns = get_fns (tid);
6315                       if (OVL_SINGLE_P (fns))
6316                         tmpl = OVL_FIRST (fns);
6317                       error_at (token->location, "function template-id %qD "
6318                                 "in nested-name-specifier", tid);
6319                     }
6320                   else
6321                     {
6322                       /* Variable template.  */
6323                       tmpl = TREE_OPERAND (tid, 0);
6324                       gcc_assert (variable_template_p (tmpl));
6325                       error_at (token->location, "variable template-id %qD "
6326                                 "in nested-name-specifier", tid);
6327                     }
6328                   if (tmpl)
6329                     inform (DECL_SOURCE_LOCATION (tmpl),
6330                             "%qD declared here", tmpl);
6331
6332                   parser->scope = error_mark_node;
6333                   error_p = true;
6334                   /* As below.  */
6335                   success = true;
6336                   cp_lexer_consume_token (parser->lexer);
6337                   cp_lexer_consume_token (parser->lexer);
6338                 }
6339             }
6340
6341           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6342             break;
6343           /* If the next token is an identifier, and the one after
6344              that is a `::', then any valid interpretation would have
6345              found a class-or-namespace-name.  */
6346           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6347                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6348                      == CPP_SCOPE)
6349                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6350                      != CPP_COMPL))
6351             {
6352               token = cp_lexer_consume_token (parser->lexer);
6353               if (!error_p)
6354                 {
6355                   if (!token->error_reported)
6356                     {
6357                       tree decl;
6358                       tree ambiguous_decls;
6359
6360                       decl = cp_parser_lookup_name (parser, token->u.value,
6361                                                     none_type,
6362                                                     /*is_template=*/false,
6363                                                     /*is_namespace=*/false,
6364                                                     /*check_dependency=*/true,
6365                                                     &ambiguous_decls,
6366                                                     token->location);
6367                       if (TREE_CODE (decl) == TEMPLATE_DECL)
6368                         error_at (token->location,
6369                                   "%qD used without template parameters",
6370                                   decl);
6371                       else if (ambiguous_decls)
6372                         {
6373                           // cp_parser_lookup_name has the same diagnostic,
6374                           // thus make sure to emit it at most once.
6375                           if (cp_parser_uncommitted_to_tentative_parse_p
6376                               (parser))
6377                             {
6378                               error_at (token->location,
6379                                         "reference to %qD is ambiguous",
6380                                         token->u.value);
6381                               print_candidates (ambiguous_decls);
6382                             }
6383                           decl = error_mark_node;
6384                         }
6385                       else
6386                         {
6387                           if (cxx_dialect != cxx98)
6388                             cp_parser_name_lookup_error
6389                             (parser, token->u.value, decl, NLE_NOT_CXX98,
6390                              token->location);
6391                           else
6392                             cp_parser_name_lookup_error
6393                             (parser, token->u.value, decl, NLE_CXX98,
6394                              token->location);
6395                         }
6396                     }
6397                   parser->scope = error_mark_node;
6398                   error_p = true;
6399                   /* Treat this as a successful nested-name-specifier
6400                      due to:
6401
6402                      [basic.lookup.qual]
6403
6404                      If the name found is not a class-name (clause
6405                      _class_) or namespace-name (_namespace.def_), the
6406                      program is ill-formed.  */
6407                   success = true;
6408                 }
6409               cp_lexer_consume_token (parser->lexer);
6410             }
6411           break;
6412         }
6413       /* We've found one valid nested-name-specifier.  */
6414       success = true;
6415       /* Name lookup always gives us a DECL.  */
6416       if (TREE_CODE (new_scope) == TYPE_DECL)
6417         new_scope = TREE_TYPE (new_scope);
6418       /* Uses of "template" must be followed by actual templates.  */
6419       if (template_keyword_p
6420           && !(CLASS_TYPE_P (new_scope)
6421                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6422                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6423                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
6424           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6425                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6426                    == TEMPLATE_ID_EXPR)))
6427         permerror (input_location, TYPE_P (new_scope)
6428                    ? G_("%qT is not a template")
6429                    : G_("%qD is not a template"),
6430                    new_scope);
6431       /* If it is a class scope, try to complete it; we are about to
6432          be looking up names inside the class.  */
6433       if (TYPE_P (new_scope)
6434           /* Since checking types for dependency can be expensive,
6435              avoid doing it if the type is already complete.  */
6436           && !COMPLETE_TYPE_P (new_scope)
6437           /* Do not try to complete dependent types.  */
6438           && !dependent_type_p (new_scope))
6439         {
6440           new_scope = complete_type (new_scope);
6441           /* If it is a typedef to current class, use the current
6442              class instead, as the typedef won't have any names inside
6443              it yet.  */
6444           if (!COMPLETE_TYPE_P (new_scope)
6445               && currently_open_class (new_scope))
6446             new_scope = TYPE_MAIN_VARIANT (new_scope);
6447         }
6448       /* Make sure we look in the right scope the next time through
6449          the loop.  */
6450       parser->scope = new_scope;
6451     }
6452
6453   /* If parsing tentatively, replace the sequence of tokens that makes
6454      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6455      token.  That way, should we re-parse the token stream, we will
6456      not have to repeat the effort required to do the parse, nor will
6457      we issue duplicate error messages.  */
6458   if (success && start)
6459     {
6460       cp_token *token;
6461
6462       token = cp_lexer_token_at (parser->lexer, start);
6463       /* Reset the contents of the START token.  */
6464       token->type = CPP_NESTED_NAME_SPECIFIER;
6465       /* Retrieve any deferred checks.  Do not pop this access checks yet
6466          so the memory will not be reclaimed during token replacing below.  */
6467       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6468       token->u.tree_check_value->value = parser->scope;
6469       token->u.tree_check_value->checks = get_deferred_access_checks ();
6470       token->u.tree_check_value->qualifying_scope =
6471         parser->qualifying_scope;
6472       token->keyword = RID_MAX;
6473
6474       /* Purge all subsequent tokens.  */
6475       cp_lexer_purge_tokens_after (parser->lexer, start);
6476     }
6477
6478   if (start)
6479     pop_to_parent_deferring_access_checks ();
6480
6481   return success ? parser->scope : NULL_TREE;
6482 }
6483
6484 /* Parse a nested-name-specifier.  See
6485    cp_parser_nested_name_specifier_opt for details.  This function
6486    behaves identically, except that it will an issue an error if no
6487    nested-name-specifier is present.  */
6488
6489 static tree
6490 cp_parser_nested_name_specifier (cp_parser *parser,
6491                                  bool typename_keyword_p,
6492                                  bool check_dependency_p,
6493                                  bool type_p,
6494                                  bool is_declaration)
6495 {
6496   tree scope;
6497
6498   /* Look for the nested-name-specifier.  */
6499   scope = cp_parser_nested_name_specifier_opt (parser,
6500                                                typename_keyword_p,
6501                                                check_dependency_p,
6502                                                type_p,
6503                                                is_declaration);
6504   /* If it was not present, issue an error message.  */
6505   if (!scope)
6506     {
6507       cp_parser_error (parser, "expected nested-name-specifier");
6508       parser->scope = NULL_TREE;
6509     }
6510
6511   return scope;
6512 }
6513
6514 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6515    this is either a class-name or a namespace-name (which corresponds
6516    to the class-or-namespace-name production in the grammar). For
6517    C++0x, it can also be a type-name that refers to an enumeration
6518    type or a simple-template-id.
6519
6520    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6521    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6522    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6523    TYPE_P is TRUE iff the next name should be taken as a class-name,
6524    even the same name is declared to be another entity in the same
6525    scope.
6526
6527    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6528    specified by the class-or-namespace-name.  If neither is found the
6529    ERROR_MARK_NODE is returned.  */
6530
6531 static tree
6532 cp_parser_qualifying_entity (cp_parser *parser,
6533                              bool typename_keyword_p,
6534                              bool template_keyword_p,
6535                              bool check_dependency_p,
6536                              bool type_p,
6537                              bool is_declaration)
6538 {
6539   tree saved_scope;
6540   tree saved_qualifying_scope;
6541   tree saved_object_scope;
6542   tree scope;
6543   bool only_class_p;
6544   bool successful_parse_p;
6545
6546   /* DR 743: decltype can appear in a nested-name-specifier.  */
6547   if (cp_lexer_next_token_is_decltype (parser->lexer))
6548     {
6549       scope = cp_parser_decltype (parser);
6550       if (TREE_CODE (scope) != ENUMERAL_TYPE
6551           && !MAYBE_CLASS_TYPE_P (scope))
6552         {
6553           cp_parser_simulate_error (parser);
6554           return error_mark_node;
6555         }
6556       if (TYPE_NAME (scope))
6557         scope = TYPE_NAME (scope);
6558       return scope;
6559     }
6560
6561   /* Before we try to parse the class-name, we must save away the
6562      current PARSER->SCOPE since cp_parser_class_name will destroy
6563      it.  */
6564   saved_scope = parser->scope;
6565   saved_qualifying_scope = parser->qualifying_scope;
6566   saved_object_scope = parser->object_scope;
6567   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
6568      there is no need to look for a namespace-name.  */
6569   only_class_p = template_keyword_p 
6570     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6571   if (!only_class_p)
6572     cp_parser_parse_tentatively (parser);
6573   scope = cp_parser_class_name (parser,
6574                                 typename_keyword_p,
6575                                 template_keyword_p,
6576                                 type_p ? class_type : none_type,
6577                                 check_dependency_p,
6578                                 /*class_head_p=*/false,
6579                                 is_declaration,
6580                                 /*enum_ok=*/cxx_dialect > cxx98);
6581   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6582   /* If that didn't work, try for a namespace-name.  */
6583   if (!only_class_p && !successful_parse_p)
6584     {
6585       /* Restore the saved scope.  */
6586       parser->scope = saved_scope;
6587       parser->qualifying_scope = saved_qualifying_scope;
6588       parser->object_scope = saved_object_scope;
6589       /* If we are not looking at an identifier followed by the scope
6590          resolution operator, then this is not part of a
6591          nested-name-specifier.  (Note that this function is only used
6592          to parse the components of a nested-name-specifier.)  */
6593       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6594           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6595         return error_mark_node;
6596       scope = cp_parser_namespace_name (parser);
6597     }
6598
6599   return scope;
6600 }
6601
6602 /* Return true if we are looking at a compound-literal, false otherwise.  */
6603
6604 static bool
6605 cp_parser_compound_literal_p (cp_parser *parser)
6606 {
6607   cp_lexer_save_tokens (parser->lexer);
6608
6609   /* Skip tokens until the next token is a closing parenthesis.
6610      If we find the closing `)', and the next token is a `{', then
6611      we are looking at a compound-literal.  */
6612   bool compound_literal_p
6613     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6614                                               /*consume_paren=*/true)
6615        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6616   
6617   /* Roll back the tokens we skipped.  */
6618   cp_lexer_rollback_tokens (parser->lexer);
6619
6620   return compound_literal_p;
6621 }
6622
6623 /* Return true if EXPR is the integer constant zero or a complex constant
6624    of zero, without any folding, but ignoring location wrappers.  */
6625
6626 static bool
6627 literal_integer_zerop (const_tree expr)
6628 {
6629   STRIP_ANY_LOCATION_WRAPPER (expr);
6630   return integer_zerop (expr);
6631 }
6632
6633 /* Parse a postfix-expression.
6634
6635    postfix-expression:
6636      primary-expression
6637      postfix-expression [ expression ]
6638      postfix-expression ( expression-list [opt] )
6639      simple-type-specifier ( expression-list [opt] )
6640      typename :: [opt] nested-name-specifier identifier
6641        ( expression-list [opt] )
6642      typename :: [opt] nested-name-specifier template [opt] template-id
6643        ( expression-list [opt] )
6644      postfix-expression . template [opt] id-expression
6645      postfix-expression -> template [opt] id-expression
6646      postfix-expression . pseudo-destructor-name
6647      postfix-expression -> pseudo-destructor-name
6648      postfix-expression ++
6649      postfix-expression --
6650      dynamic_cast < type-id > ( expression )
6651      static_cast < type-id > ( expression )
6652      reinterpret_cast < type-id > ( expression )
6653      const_cast < type-id > ( expression )
6654      typeid ( expression )
6655      typeid ( type-id )
6656
6657    GNU Extension:
6658
6659    postfix-expression:
6660      ( type-id ) { initializer-list , [opt] }
6661
6662    This extension is a GNU version of the C99 compound-literal
6663    construct.  (The C99 grammar uses `type-name' instead of `type-id',
6664    but they are essentially the same concept.)
6665
6666    If ADDRESS_P is true, the postfix expression is the operand of the
6667    `&' operator.  CAST_P is true if this expression is the target of a
6668    cast.
6669
6670    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6671    class member access expressions [expr.ref].
6672
6673    Returns a representation of the expression.  */
6674
6675 static cp_expr
6676 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6677                               bool member_access_only_p, bool decltype_p,
6678                               cp_id_kind * pidk_return)
6679 {
6680   cp_token *token;
6681   location_t loc;
6682   enum rid keyword;
6683   cp_id_kind idk = CP_ID_KIND_NONE;
6684   cp_expr postfix_expression = NULL_TREE;
6685   bool is_member_access = false;
6686
6687   /* Peek at the next token.  */
6688   token = cp_lexer_peek_token (parser->lexer);
6689   loc = token->location;
6690   location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6691
6692   /* Some of the productions are determined by keywords.  */
6693   keyword = token->keyword;
6694   switch (keyword)
6695     {
6696     case RID_DYNCAST:
6697     case RID_STATCAST:
6698     case RID_REINTCAST:
6699     case RID_CONSTCAST:
6700       {
6701         tree type;
6702         cp_expr expression;
6703         const char *saved_message;
6704         bool saved_in_type_id_in_expr_p;
6705
6706         /* All of these can be handled in the same way from the point
6707            of view of parsing.  Begin by consuming the token
6708            identifying the cast.  */
6709         cp_lexer_consume_token (parser->lexer);
6710
6711         /* New types cannot be defined in the cast.  */
6712         saved_message = parser->type_definition_forbidden_message;
6713         parser->type_definition_forbidden_message
6714           = G_("types may not be defined in casts");
6715
6716         /* Look for the opening `<'.  */
6717         cp_parser_require (parser, CPP_LESS, RT_LESS);
6718         /* Parse the type to which we are casting.  */
6719         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6720         parser->in_type_id_in_expr_p = true;
6721         type = cp_parser_type_id (parser);
6722         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6723         /* Look for the closing `>'.  */
6724         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6725         /* Restore the old message.  */
6726         parser->type_definition_forbidden_message = saved_message;
6727
6728         bool saved_greater_than_is_operator_p
6729           = parser->greater_than_is_operator_p;
6730         parser->greater_than_is_operator_p = true;
6731
6732         /* And the expression which is being cast.  */
6733         matching_parens parens;
6734         parens.require_open (parser);
6735         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6736         cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6737                                                    RT_CLOSE_PAREN);
6738         location_t end_loc = close_paren ?
6739           close_paren->location : UNKNOWN_LOCATION;
6740
6741         parser->greater_than_is_operator_p
6742           = saved_greater_than_is_operator_p;
6743
6744         /* Only type conversions to integral or enumeration types
6745            can be used in constant-expressions.  */
6746         if (!cast_valid_in_integral_constant_expression_p (type)
6747             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6748           {
6749             postfix_expression = error_mark_node;
6750             break;
6751           }
6752
6753         switch (keyword)
6754           {
6755           case RID_DYNCAST:
6756             postfix_expression
6757               = build_dynamic_cast (type, expression, tf_warning_or_error);
6758             break;
6759           case RID_STATCAST:
6760             postfix_expression
6761               = build_static_cast (type, expression, tf_warning_or_error);
6762             break;
6763           case RID_REINTCAST:
6764             postfix_expression
6765               = build_reinterpret_cast (type, expression, 
6766                                         tf_warning_or_error);
6767             break;
6768           case RID_CONSTCAST:
6769             postfix_expression
6770               = build_const_cast (type, expression, tf_warning_or_error);
6771             break;
6772           default:
6773             gcc_unreachable ();
6774           }
6775
6776         /* Construct a location e.g. :
6777              reinterpret_cast <int *> (expr)
6778              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6779            ranging from the start of the "*_cast" token to the final closing
6780            paren, with the caret at the start.  */
6781         location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6782         postfix_expression.set_location (cp_cast_loc);
6783       }
6784       break;
6785
6786     case RID_TYPEID:
6787       {
6788         tree type;
6789         const char *saved_message;
6790         bool saved_in_type_id_in_expr_p;
6791
6792         /* Consume the `typeid' token.  */
6793         cp_lexer_consume_token (parser->lexer);
6794         /* Look for the `(' token.  */
6795         matching_parens parens;
6796         parens.require_open (parser);
6797         /* Types cannot be defined in a `typeid' expression.  */
6798         saved_message = parser->type_definition_forbidden_message;
6799         parser->type_definition_forbidden_message
6800           = G_("types may not be defined in a %<typeid%> expression");
6801         /* We can't be sure yet whether we're looking at a type-id or an
6802            expression.  */
6803         cp_parser_parse_tentatively (parser);
6804         /* Try a type-id first.  */
6805         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6806         parser->in_type_id_in_expr_p = true;
6807         type = cp_parser_type_id (parser);
6808         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6809         /* Look for the `)' token.  Otherwise, we can't be sure that
6810            we're not looking at an expression: consider `typeid (int
6811            (3))', for example.  */
6812         cp_token *close_paren = parens.require_close (parser);
6813         /* If all went well, simply lookup the type-id.  */
6814         if (cp_parser_parse_definitely (parser))
6815           postfix_expression = get_typeid (type, tf_warning_or_error);
6816         /* Otherwise, fall back to the expression variant.  */
6817         else
6818           {
6819             tree expression;
6820
6821             /* Look for an expression.  */
6822             expression = cp_parser_expression (parser, & idk);
6823             /* Compute its typeid.  */
6824             postfix_expression = build_typeid (expression, tf_warning_or_error);
6825             /* Look for the `)' token.  */
6826             close_paren = parens.require_close (parser);
6827           }
6828         /* Restore the saved message.  */
6829         parser->type_definition_forbidden_message = saved_message;
6830         /* `typeid' may not appear in an integral constant expression.  */
6831         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6832           postfix_expression = error_mark_node;
6833
6834         /* Construct a location e.g. :
6835              typeid (expr)
6836              ^~~~~~~~~~~~~
6837            ranging from the start of the "typeid" token to the final closing
6838            paren, with the caret at the start.  */
6839         if (close_paren)
6840           {
6841             location_t typeid_loc
6842               = make_location (start_loc, start_loc, close_paren->location);
6843             postfix_expression.set_location (typeid_loc);
6844             postfix_expression.maybe_add_location_wrapper ();
6845           }
6846       }
6847       break;
6848
6849     case RID_TYPENAME:
6850       {
6851         tree type;
6852         /* The syntax permitted here is the same permitted for an
6853            elaborated-type-specifier.  */
6854         ++parser->prevent_constrained_type_specifiers;
6855         type = cp_parser_elaborated_type_specifier (parser,
6856                                                     /*is_friend=*/false,
6857                                                     /*is_declaration=*/false);
6858         --parser->prevent_constrained_type_specifiers;
6859         postfix_expression = cp_parser_functional_cast (parser, type);
6860       }
6861       break;
6862
6863     case RID_ADDRESSOF:
6864     case RID_BUILTIN_SHUFFLE:
6865     case RID_BUILTIN_LAUNDER:
6866       {
6867         vec<tree, va_gc> *vec;
6868         unsigned int i;
6869         tree p;
6870
6871         cp_lexer_consume_token (parser->lexer);
6872         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6873                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6874                     /*non_constant_p=*/NULL);
6875         if (vec == NULL)
6876           {
6877             postfix_expression = error_mark_node;
6878             break;
6879           }
6880
6881         FOR_EACH_VEC_ELT (*vec, i, p)
6882           mark_exp_read (p);
6883
6884         switch (keyword)
6885           {
6886           case RID_ADDRESSOF:
6887             if (vec->length () == 1)
6888               postfix_expression
6889                 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6890             else
6891               {
6892                 error_at (loc, "wrong number of arguments to "
6893                                "%<__builtin_addressof%>");
6894                 postfix_expression = error_mark_node;
6895               }
6896             break;
6897
6898           case RID_BUILTIN_LAUNDER:
6899             if (vec->length () == 1)
6900               postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6901                                                            tf_warning_or_error);
6902             else
6903               {
6904                 error_at (loc, "wrong number of arguments to "
6905                                "%<__builtin_launder%>");
6906                 postfix_expression = error_mark_node;
6907               }
6908             break;
6909
6910           case RID_BUILTIN_SHUFFLE:
6911             if (vec->length () == 2)
6912               postfix_expression
6913                 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6914                                          (*vec)[1], tf_warning_or_error);
6915             else if (vec->length () == 3)
6916               postfix_expression
6917                 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6918                                          (*vec)[2], tf_warning_or_error);
6919             else
6920               {
6921                 error_at (loc, "wrong number of arguments to "
6922                                "%<__builtin_shuffle%>");
6923                 postfix_expression = error_mark_node;
6924               }
6925             break;
6926
6927           default:
6928             gcc_unreachable ();
6929           }
6930         break;
6931       }
6932
6933     default:
6934       {
6935         tree type;
6936
6937         /* If the next thing is a simple-type-specifier, we may be
6938            looking at a functional cast.  We could also be looking at
6939            an id-expression.  So, we try the functional cast, and if
6940            that doesn't work we fall back to the primary-expression.  */
6941         cp_parser_parse_tentatively (parser);
6942         /* Look for the simple-type-specifier.  */
6943         ++parser->prevent_constrained_type_specifiers;
6944         type = cp_parser_simple_type_specifier (parser,
6945                                                 /*decl_specs=*/NULL,
6946                                                 CP_PARSER_FLAGS_NONE);
6947         --parser->prevent_constrained_type_specifiers;
6948         /* Parse the cast itself.  */
6949         if (!cp_parser_error_occurred (parser))
6950           postfix_expression
6951             = cp_parser_functional_cast (parser, type);
6952         /* If that worked, we're done.  */
6953         if (cp_parser_parse_definitely (parser))
6954           break;
6955
6956         /* If the functional-cast didn't work out, try a
6957            compound-literal.  */
6958         if (cp_parser_allow_gnu_extensions_p (parser)
6959             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6960           {
6961             cp_expr initializer = NULL_TREE;
6962
6963             cp_parser_parse_tentatively (parser);
6964
6965             matching_parens parens;
6966             parens.consume_open (parser);
6967
6968             /* Avoid calling cp_parser_type_id pointlessly, see comment
6969                in cp_parser_cast_expression about c++/29234.  */
6970             if (!cp_parser_compound_literal_p (parser))
6971               cp_parser_simulate_error (parser);
6972             else
6973               {
6974                 /* Parse the type.  */
6975                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6976                 parser->in_type_id_in_expr_p = true;
6977                 type = cp_parser_type_id (parser);
6978                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6979                 parens.require_close (parser);
6980               }
6981
6982             /* If things aren't going well, there's no need to
6983                keep going.  */
6984             if (!cp_parser_error_occurred (parser))
6985               {
6986                 bool non_constant_p;
6987                 /* Parse the brace-enclosed initializer list.  */
6988                 initializer = cp_parser_braced_list (parser,
6989                                                      &non_constant_p);
6990               }
6991             /* If that worked, we're definitely looking at a
6992                compound-literal expression.  */
6993             if (cp_parser_parse_definitely (parser))
6994               {
6995                 /* Warn the user that a compound literal is not
6996                    allowed in standard C++.  */
6997                 pedwarn (input_location, OPT_Wpedantic,
6998                          "ISO C++ forbids compound-literals");
6999                 /* For simplicity, we disallow compound literals in
7000                    constant-expressions.  We could
7001                    allow compound literals of integer type, whose
7002                    initializer was a constant, in constant
7003                    expressions.  Permitting that usage, as a further
7004                    extension, would not change the meaning of any
7005                    currently accepted programs.  (Of course, as
7006                    compound literals are not part of ISO C++, the
7007                    standard has nothing to say.)  */
7008                 if (cp_parser_non_integral_constant_expression (parser,
7009                                                                 NIC_NCC))
7010                   {
7011                     postfix_expression = error_mark_node;
7012                     break;
7013                   }
7014                 /* Form the representation of the compound-literal.  */
7015                 postfix_expression
7016                   = finish_compound_literal (type, initializer,
7017                                              tf_warning_or_error, fcl_c99);
7018                 postfix_expression.set_location (initializer.get_location ());
7019                 break;
7020               }
7021           }
7022
7023         /* It must be a primary-expression.  */
7024         postfix_expression
7025           = cp_parser_primary_expression (parser, address_p, cast_p,
7026                                           /*template_arg_p=*/false,
7027                                           decltype_p,
7028                                           &idk);
7029       }
7030       break;
7031     }
7032
7033   /* Note that we don't need to worry about calling build_cplus_new on a
7034      class-valued CALL_EXPR in decltype when it isn't the end of the
7035      postfix-expression; unary_complex_lvalue will take care of that for
7036      all these cases.  */
7037
7038   /* Keep looping until the postfix-expression is complete.  */
7039   while (true)
7040     {
7041       if (idk == CP_ID_KIND_UNQUALIFIED
7042           && identifier_p (postfix_expression)
7043           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7044         /* It is not a Koenig lookup function call.  */
7045         postfix_expression
7046           = unqualified_name_lookup_error (postfix_expression);
7047
7048       /* Peek at the next token.  */
7049       token = cp_lexer_peek_token (parser->lexer);
7050
7051       switch (token->type)
7052         {
7053         case CPP_OPEN_SQUARE:
7054           if (cp_next_tokens_can_be_std_attribute_p (parser))
7055             {
7056               cp_parser_error (parser,
7057                                "two consecutive %<[%> shall "
7058                                "only introduce an attribute");
7059               return error_mark_node;
7060             }
7061           postfix_expression
7062             = cp_parser_postfix_open_square_expression (parser,
7063                                                         postfix_expression,
7064                                                         false,
7065                                                         decltype_p);
7066           postfix_expression.set_range (start_loc,
7067                                         postfix_expression.get_location ());
7068
7069           idk = CP_ID_KIND_NONE;
7070           is_member_access = false;
7071           break;
7072
7073         case CPP_OPEN_PAREN:
7074           /* postfix-expression ( expression-list [opt] ) */
7075           {
7076             bool koenig_p;
7077             bool is_builtin_constant_p;
7078             bool saved_integral_constant_expression_p = false;
7079             bool saved_non_integral_constant_expression_p = false;
7080             tsubst_flags_t complain = complain_flags (decltype_p);
7081             vec<tree, va_gc> *args;
7082             location_t close_paren_loc = UNKNOWN_LOCATION;
7083
7084             is_member_access = false;
7085
7086             is_builtin_constant_p
7087               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7088             if (is_builtin_constant_p)
7089               {
7090                 /* The whole point of __builtin_constant_p is to allow
7091                    non-constant expressions to appear as arguments.  */
7092                 saved_integral_constant_expression_p
7093                   = parser->integral_constant_expression_p;
7094                 saved_non_integral_constant_expression_p
7095                   = parser->non_integral_constant_expression_p;
7096                 parser->integral_constant_expression_p = false;
7097               }
7098             args = (cp_parser_parenthesized_expression_list
7099                     (parser, non_attr,
7100                      /*cast_p=*/false, /*allow_expansion_p=*/true,
7101                      /*non_constant_p=*/NULL,
7102                      /*close_paren_loc=*/&close_paren_loc,
7103                      /*wrap_locations_p=*/true));
7104             if (is_builtin_constant_p)
7105               {
7106                 parser->integral_constant_expression_p
7107                   = saved_integral_constant_expression_p;
7108                 parser->non_integral_constant_expression_p
7109                   = saved_non_integral_constant_expression_p;
7110               }
7111
7112             if (args == NULL)
7113               {
7114                 postfix_expression = error_mark_node;
7115                 break;
7116               }
7117
7118             /* Function calls are not permitted in
7119                constant-expressions.  */
7120             if (! builtin_valid_in_constant_expr_p (postfix_expression)
7121                 && cp_parser_non_integral_constant_expression (parser,
7122                                                                NIC_FUNC_CALL))
7123               {
7124                 postfix_expression = error_mark_node;
7125                 release_tree_vector (args);
7126                 break;
7127               }
7128
7129             koenig_p = false;
7130             if (idk == CP_ID_KIND_UNQUALIFIED
7131                 || idk == CP_ID_KIND_TEMPLATE_ID)
7132               {
7133                 if (identifier_p (postfix_expression))
7134                   {
7135                     if (!args->is_empty ())
7136                       {
7137                         koenig_p = true;
7138                         if (!any_type_dependent_arguments_p (args))
7139                           postfix_expression
7140                             = perform_koenig_lookup (postfix_expression, args,
7141                                                      complain);
7142                       }
7143                     else
7144                       postfix_expression
7145                         = unqualified_fn_lookup_error (postfix_expression);
7146                   }
7147                 /* We do not perform argument-dependent lookup if
7148                    normal lookup finds a non-function, in accordance
7149                    with the expected resolution of DR 218.  */
7150                 else if (!args->is_empty ()
7151                          && is_overloaded_fn (postfix_expression))
7152                   {
7153                     /* We only need to look at the first function,
7154                        because all the fns share the attribute we're
7155                        concerned with (all member fns or all local
7156                        fns).  */
7157                     tree fn = get_first_fn (postfix_expression);
7158                     fn = STRIP_TEMPLATE (fn);
7159
7160                     /* Do not do argument dependent lookup if regular
7161                        lookup finds a member function or a block-scope
7162                        function declaration.  [basic.lookup.argdep]/3  */
7163                     if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7164                           || DECL_FUNCTION_MEMBER_P (fn)
7165                           || DECL_LOCAL_FUNCTION_P (fn)))
7166                       {
7167                         koenig_p = true;
7168                         if (!any_type_dependent_arguments_p (args))
7169                           postfix_expression
7170                             = perform_koenig_lookup (postfix_expression, args,
7171                                                      complain);
7172                       }
7173                   }
7174               }
7175
7176             if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7177                 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7178                 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7179                 && vec_safe_length (args) == 3)
7180               {
7181                 tree arg0 = (*args)[0];
7182                 tree arg1 = (*args)[1];
7183                 tree arg2 = (*args)[2];
7184                 int literal_mask = ((literal_integer_zerop (arg1) << 1)
7185                                     | (literal_integer_zerop (arg2) << 2));
7186                 warn_for_memset (input_location, arg0, arg2, literal_mask);
7187               }
7188
7189             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7190               {
7191                 tree instance = TREE_OPERAND (postfix_expression, 0);
7192                 tree fn = TREE_OPERAND (postfix_expression, 1);
7193
7194                 if (processing_template_decl
7195                     && (type_dependent_object_expression_p (instance)
7196                         || (!BASELINK_P (fn)
7197                             && TREE_CODE (fn) != FIELD_DECL)
7198                         || type_dependent_expression_p (fn)
7199                         || any_type_dependent_arguments_p (args)))
7200                   {
7201                     maybe_generic_this_capture (instance, fn);
7202                     postfix_expression
7203                       = build_min_nt_call_vec (postfix_expression, args);
7204                     release_tree_vector (args);
7205                     break;
7206                   }
7207
7208                 if (BASELINK_P (fn))
7209                   {
7210                   postfix_expression
7211                     = (build_new_method_call
7212                        (instance, fn, &args, NULL_TREE,
7213                         (idk == CP_ID_KIND_QUALIFIED
7214                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7215                          : LOOKUP_NORMAL),
7216                         /*fn_p=*/NULL,
7217                         complain));
7218                   }
7219                 else
7220                   postfix_expression
7221                     = finish_call_expr (postfix_expression, &args,
7222                                         /*disallow_virtual=*/false,
7223                                         /*koenig_p=*/false,
7224                                         complain);
7225               }
7226             else if (TREE_CODE (postfix_expression) == OFFSET_REF
7227                      || TREE_CODE (postfix_expression) == MEMBER_REF
7228                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7229               postfix_expression = (build_offset_ref_call_from_tree
7230                                     (postfix_expression, &args,
7231                                      complain));
7232             else if (idk == CP_ID_KIND_QUALIFIED)
7233               /* A call to a static class member, or a namespace-scope
7234                  function.  */
7235               postfix_expression
7236                 = finish_call_expr (postfix_expression, &args,
7237                                     /*disallow_virtual=*/true,
7238                                     koenig_p,
7239                                     complain);
7240             else
7241               /* All other function calls.  */
7242               postfix_expression
7243                 = finish_call_expr (postfix_expression, &args,
7244                                     /*disallow_virtual=*/false,
7245                                     koenig_p,
7246                                     complain);
7247
7248             if (close_paren_loc != UNKNOWN_LOCATION)
7249               {
7250                 location_t combined_loc = make_location (token->location,
7251                                                          start_loc,
7252                                                          close_paren_loc);
7253                 postfix_expression.set_location (combined_loc);
7254               }
7255
7256             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
7257             idk = CP_ID_KIND_NONE;
7258
7259             release_tree_vector (args);
7260           }
7261           break;
7262
7263         case CPP_DOT:
7264         case CPP_DEREF:
7265           /* postfix-expression . template [opt] id-expression
7266              postfix-expression . pseudo-destructor-name
7267              postfix-expression -> template [opt] id-expression
7268              postfix-expression -> pseudo-destructor-name */
7269
7270           /* Consume the `.' or `->' operator.  */
7271           cp_lexer_consume_token (parser->lexer);
7272
7273           postfix_expression
7274             = cp_parser_postfix_dot_deref_expression (parser, token->type,
7275                                                       postfix_expression,
7276                                                       false, &idk, loc);
7277
7278           is_member_access = true;
7279           break;
7280
7281         case CPP_PLUS_PLUS:
7282           /* postfix-expression ++  */
7283           /* Consume the `++' token.  */
7284           cp_lexer_consume_token (parser->lexer);
7285           /* Generate a representation for the complete expression.  */
7286           postfix_expression
7287             = finish_increment_expr (postfix_expression,
7288                                      POSTINCREMENT_EXPR);
7289           /* Increments may not appear in constant-expressions.  */
7290           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7291             postfix_expression = error_mark_node;
7292           idk = CP_ID_KIND_NONE;
7293           is_member_access = false;
7294           break;
7295
7296         case CPP_MINUS_MINUS:
7297           /* postfix-expression -- */
7298           /* Consume the `--' token.  */
7299           cp_lexer_consume_token (parser->lexer);
7300           /* Generate a representation for the complete expression.  */
7301           postfix_expression
7302             = finish_increment_expr (postfix_expression,
7303                                      POSTDECREMENT_EXPR);
7304           /* Decrements may not appear in constant-expressions.  */
7305           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7306             postfix_expression = error_mark_node;
7307           idk = CP_ID_KIND_NONE;
7308           is_member_access = false;
7309           break;
7310
7311         default:
7312           if (pidk_return != NULL)
7313             * pidk_return = idk;
7314           if (member_access_only_p)
7315             return is_member_access
7316               ? postfix_expression
7317               : cp_expr (error_mark_node);
7318           else
7319             return postfix_expression;
7320         }
7321     }
7322
7323   /* We should never get here.  */
7324   gcc_unreachable ();
7325   return error_mark_node;
7326 }
7327
7328 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7329    by cp_parser_builtin_offsetof.  We're looking for
7330
7331      postfix-expression [ expression ]
7332      postfix-expression [ braced-init-list ] (C++11)
7333
7334    FOR_OFFSETOF is set if we're being called in that context, which
7335    changes how we deal with integer constant expressions.  */
7336
7337 static tree
7338 cp_parser_postfix_open_square_expression (cp_parser *parser,
7339                                           tree postfix_expression,
7340                                           bool for_offsetof,
7341                                           bool decltype_p)
7342 {
7343   tree index = NULL_TREE;
7344   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7345   bool saved_greater_than_is_operator_p;
7346
7347   /* Consume the `[' token.  */
7348   cp_lexer_consume_token (parser->lexer);
7349
7350   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7351   parser->greater_than_is_operator_p = true;
7352
7353   /* Parse the index expression.  */
7354   /* ??? For offsetof, there is a question of what to allow here.  If
7355      offsetof is not being used in an integral constant expression context,
7356      then we *could* get the right answer by computing the value at runtime.
7357      If we are in an integral constant expression context, then we might
7358      could accept any constant expression; hard to say without analysis.
7359      Rather than open the barn door too wide right away, allow only integer
7360      constant expressions here.  */
7361   if (for_offsetof)
7362     index = cp_parser_constant_expression (parser);
7363   else
7364     {
7365       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7366         {
7367           bool expr_nonconst_p;
7368           cp_lexer_set_source_position (parser->lexer);
7369           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7370           index = cp_parser_braced_list (parser, &expr_nonconst_p);
7371         }
7372       else
7373         index = cp_parser_expression (parser);
7374     }
7375
7376   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7377
7378   /* Look for the closing `]'.  */
7379   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7380
7381   /* Build the ARRAY_REF.  */
7382   postfix_expression = grok_array_decl (loc, postfix_expression,
7383                                         index, decltype_p);
7384
7385   /* When not doing offsetof, array references are not permitted in
7386      constant-expressions.  */
7387   if (!for_offsetof
7388       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7389     postfix_expression = error_mark_node;
7390
7391   return postfix_expression;
7392 }
7393
7394 /* A subroutine of cp_parser_postfix_dot_deref_expression.  Handle dot
7395    dereference of incomplete type, returns true if error_mark_node should
7396    be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7397    and *DEPENDENT_P.  */
7398
7399 bool
7400 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7401                                 bool *dependent_p)
7402 {
7403   /* In a template, be permissive by treating an object expression
7404      of incomplete type as dependent (after a pedwarn).  */
7405   diagnostic_t kind = (processing_template_decl
7406                        && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7407
7408   switch (TREE_CODE (*postfix_expression))
7409     {
7410     case CAST_EXPR:
7411     case REINTERPRET_CAST_EXPR:
7412     case CONST_CAST_EXPR:
7413     case STATIC_CAST_EXPR:
7414     case DYNAMIC_CAST_EXPR:
7415     case IMPLICIT_CONV_EXPR:
7416     case VIEW_CONVERT_EXPR:
7417     case NON_LVALUE_EXPR:
7418       kind = DK_ERROR;
7419       break;
7420     case OVERLOAD:
7421       /* Don't emit any diagnostic for OVERLOADs.  */
7422       kind = DK_IGNORED;
7423       break;
7424     default:
7425       /* Avoid clobbering e.g. DECLs.  */
7426       if (!EXPR_P (*postfix_expression))
7427         kind = DK_ERROR;
7428       break;
7429     }
7430
7431   if (kind == DK_IGNORED)
7432     return false;
7433
7434   location_t exploc = location_of (*postfix_expression);
7435   cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7436   if (!MAYBE_CLASS_TYPE_P (*scope))
7437     return true;
7438   if (kind == DK_ERROR)
7439     *scope = *postfix_expression = error_mark_node;
7440   else if (processing_template_decl)
7441     {
7442       *dependent_p = true;
7443       *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7444     }
7445   return false;
7446 }
7447
7448 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7449    by cp_parser_builtin_offsetof.  We're looking for
7450
7451      postfix-expression . template [opt] id-expression
7452      postfix-expression . pseudo-destructor-name
7453      postfix-expression -> template [opt] id-expression
7454      postfix-expression -> pseudo-destructor-name
7455
7456    FOR_OFFSETOF is set if we're being called in that context.  That sorta
7457    limits what of the above we'll actually accept, but nevermind.
7458    TOKEN_TYPE is the "." or "->" token, which will already have been
7459    removed from the stream.  */
7460
7461 static tree
7462 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7463                                         enum cpp_ttype token_type,
7464                                         cp_expr postfix_expression,
7465                                         bool for_offsetof, cp_id_kind *idk,
7466                                         location_t location)
7467 {
7468   tree name;
7469   bool dependent_p;
7470   bool pseudo_destructor_p;
7471   tree scope = NULL_TREE;
7472   location_t start_loc = postfix_expression.get_start ();
7473
7474   /* If this is a `->' operator, dereference the pointer.  */
7475   if (token_type == CPP_DEREF)
7476     postfix_expression = build_x_arrow (location, postfix_expression,
7477                                         tf_warning_or_error);
7478   /* Check to see whether or not the expression is type-dependent and
7479      not the current instantiation.  */
7480   dependent_p = type_dependent_object_expression_p (postfix_expression);
7481   /* The identifier following the `->' or `.' is not qualified.  */
7482   parser->scope = NULL_TREE;
7483   parser->qualifying_scope = NULL_TREE;
7484   parser->object_scope = NULL_TREE;
7485   *idk = CP_ID_KIND_NONE;
7486
7487   /* Enter the scope corresponding to the type of the object
7488      given by the POSTFIX_EXPRESSION.  */
7489   if (!dependent_p)
7490     {
7491       scope = TREE_TYPE (postfix_expression);
7492       /* According to the standard, no expression should ever have
7493          reference type.  Unfortunately, we do not currently match
7494          the standard in this respect in that our internal representation
7495          of an expression may have reference type even when the standard
7496          says it does not.  Therefore, we have to manually obtain the
7497          underlying type here.  */
7498       scope = non_reference (scope);
7499       /* The type of the POSTFIX_EXPRESSION must be complete.  */
7500       /* Unlike the object expression in other contexts, *this is not
7501          required to be of complete type for purposes of class member
7502          access (5.2.5) outside the member function body.  */
7503       if (postfix_expression != current_class_ref
7504           && scope != error_mark_node
7505           && !currently_open_class (scope))
7506         {
7507           scope = complete_type (scope);
7508           if (!COMPLETE_TYPE_P (scope)
7509               && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7510                                                  &dependent_p))
7511             return error_mark_node;
7512         }
7513
7514       if (!dependent_p)
7515         {
7516           /* Let the name lookup machinery know that we are processing a
7517              class member access expression.  */
7518           parser->context->object_type = scope;
7519           /* If something went wrong, we want to be able to discern that case,
7520              as opposed to the case where there was no SCOPE due to the type
7521              of expression being dependent.  */
7522           if (!scope)
7523             scope = error_mark_node;
7524           /* If the SCOPE was erroneous, make the various semantic analysis
7525              functions exit quickly -- and without issuing additional error
7526              messages.  */
7527           if (scope == error_mark_node)
7528             postfix_expression = error_mark_node;
7529         }
7530     }
7531
7532   if (dependent_p)
7533     /* Tell cp_parser_lookup_name that there was an object, even though it's
7534        type-dependent.  */
7535     parser->context->object_type = unknown_type_node;
7536
7537   /* Assume this expression is not a pseudo-destructor access.  */
7538   pseudo_destructor_p = false;
7539
7540   /* If the SCOPE is a scalar type, then, if this is a valid program,
7541      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
7542      is type dependent, it can be pseudo-destructor-name or something else.
7543      Try to parse it as pseudo-destructor-name first.  */
7544   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7545     {
7546       tree s;
7547       tree type;
7548
7549       cp_parser_parse_tentatively (parser);
7550       /* Parse the pseudo-destructor-name.  */
7551       s = NULL_TREE;
7552       cp_parser_pseudo_destructor_name (parser, postfix_expression,
7553                                         &s, &type);
7554       if (dependent_p
7555           && (cp_parser_error_occurred (parser)
7556               || !SCALAR_TYPE_P (type)))
7557         cp_parser_abort_tentative_parse (parser);
7558       else if (cp_parser_parse_definitely (parser))
7559         {
7560           pseudo_destructor_p = true;
7561           postfix_expression
7562             = finish_pseudo_destructor_expr (postfix_expression,
7563                                              s, type, location);
7564         }
7565     }
7566
7567   if (!pseudo_destructor_p)
7568     {
7569       /* If the SCOPE is not a scalar type, we are looking at an
7570          ordinary class member access expression, rather than a
7571          pseudo-destructor-name.  */
7572       bool template_p;
7573       cp_token *token = cp_lexer_peek_token (parser->lexer);
7574       /* Parse the id-expression.  */
7575       name = (cp_parser_id_expression
7576               (parser,
7577                cp_parser_optional_template_keyword (parser),
7578                /*check_dependency_p=*/true,
7579                &template_p,
7580                /*declarator_p=*/false,
7581                /*optional_p=*/false));
7582       /* In general, build a SCOPE_REF if the member name is qualified.
7583          However, if the name was not dependent and has already been
7584          resolved; there is no need to build the SCOPE_REF.  For example;
7585
7586              struct X { void f(); };
7587              template <typename T> void f(T* t) { t->X::f(); }
7588
7589          Even though "t" is dependent, "X::f" is not and has been resolved
7590          to a BASELINK; there is no need to include scope information.  */
7591
7592       /* But we do need to remember that there was an explicit scope for
7593          virtual function calls.  */
7594       if (parser->scope)
7595         *idk = CP_ID_KIND_QUALIFIED;
7596
7597       /* If the name is a template-id that names a type, we will get a
7598          TYPE_DECL here.  That is invalid code.  */
7599       if (TREE_CODE (name) == TYPE_DECL)
7600         {
7601           error_at (token->location, "invalid use of %qD", name);
7602           postfix_expression = error_mark_node;
7603         }
7604       else
7605         {
7606           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7607             {
7608               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7609                 {
7610                   error_at (token->location, "%<%D::%D%> is not a class member",
7611                             parser->scope, name);
7612                   postfix_expression = error_mark_node;
7613                 }
7614               else
7615                 name = build_qualified_name (/*type=*/NULL_TREE,
7616                                              parser->scope,
7617                                              name,
7618                                              template_p);
7619               parser->scope = NULL_TREE;
7620               parser->qualifying_scope = NULL_TREE;
7621               parser->object_scope = NULL_TREE;
7622             }
7623           if (parser->scope && name && BASELINK_P (name))
7624             adjust_result_of_qualified_name_lookup
7625               (name, parser->scope, scope);
7626           postfix_expression
7627             = finish_class_member_access_expr (postfix_expression, name,
7628                                                template_p, 
7629                                                tf_warning_or_error);
7630           /* Build a location e.g.:
7631                ptr->access_expr
7632                ~~~^~~~~~~~~~~~~
7633              where the caret is at the deref token, ranging from
7634              the start of postfix_expression to the end of the access expr.  */
7635           location_t end_loc
7636             = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7637           location_t combined_loc
7638             = make_location (input_location, start_loc, end_loc);
7639           protected_set_expr_location (postfix_expression, combined_loc);
7640         }
7641     }
7642
7643   /* We no longer need to look up names in the scope of the object on
7644      the left-hand side of the `.' or `->' operator.  */
7645   parser->context->object_type = NULL_TREE;
7646
7647   /* Outside of offsetof, these operators may not appear in
7648      constant-expressions.  */
7649   if (!for_offsetof
7650       && (cp_parser_non_integral_constant_expression
7651           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7652     postfix_expression = error_mark_node;
7653
7654   return postfix_expression;
7655 }
7656
7657 /* Parse a parenthesized expression-list.
7658
7659    expression-list:
7660      assignment-expression
7661      expression-list, assignment-expression
7662
7663    attribute-list:
7664      expression-list
7665      identifier
7666      identifier, expression-list
7667
7668    CAST_P is true if this expression is the target of a cast.
7669
7670    ALLOW_EXPANSION_P is true if this expression allows expansion of an
7671    argument pack.
7672
7673    WRAP_LOCATIONS_P is true if expressions within this list for which
7674    CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7675    their source locations.
7676
7677    Returns a vector of trees.  Each element is a representation of an
7678    assignment-expression.  NULL is returned if the ( and or ) are
7679    missing.  An empty, but allocated, vector is returned on no
7680    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
7681    if we are parsing an attribute list for an attribute that wants a
7682    plain identifier argument, normal_attr for an attribute that wants
7683    an expression, or non_attr if we aren't parsing an attribute list.  If
7684    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7685    not all of the expressions in the list were constant.
7686    If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7687    will be written to with the location of the closing parenthesis.  If
7688    an error occurs, it may or may not be written to.  */
7689
7690 static vec<tree, va_gc> *
7691 cp_parser_parenthesized_expression_list (cp_parser* parser,
7692                                          int is_attribute_list,
7693                                          bool cast_p,
7694                                          bool allow_expansion_p,
7695                                          bool *non_constant_p,
7696                                          location_t *close_paren_loc,
7697                                          bool wrap_locations_p)
7698 {
7699   vec<tree, va_gc> *expression_list;
7700   bool fold_expr_p = is_attribute_list != non_attr;
7701   tree identifier = NULL_TREE;
7702   bool saved_greater_than_is_operator_p;
7703
7704   /* Assume all the expressions will be constant.  */
7705   if (non_constant_p)
7706     *non_constant_p = false;
7707
7708   matching_parens parens;
7709   if (!parens.require_open (parser))
7710     return NULL;
7711
7712   expression_list = make_tree_vector ();
7713
7714   /* Within a parenthesized expression, a `>' token is always
7715      the greater-than operator.  */
7716   saved_greater_than_is_operator_p
7717     = parser->greater_than_is_operator_p;
7718   parser->greater_than_is_operator_p = true;
7719
7720   cp_expr expr (NULL_TREE);
7721
7722   /* Consume expressions until there are no more.  */
7723   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7724     while (true)
7725       {
7726         /* At the beginning of attribute lists, check to see if the
7727            next token is an identifier.  */
7728         if (is_attribute_list == id_attr
7729             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7730           {
7731             cp_token *token;
7732
7733             /* Consume the identifier.  */
7734             token = cp_lexer_consume_token (parser->lexer);
7735             /* Save the identifier.  */
7736             identifier = token->u.value;
7737           }
7738         else
7739           {
7740             bool expr_non_constant_p;
7741
7742             /* Parse the next assignment-expression.  */
7743             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7744               {
7745                 /* A braced-init-list.  */
7746                 cp_lexer_set_source_position (parser->lexer);
7747                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7748                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7749                 if (non_constant_p && expr_non_constant_p)
7750                   *non_constant_p = true;
7751               }
7752             else if (non_constant_p)
7753               {
7754                 expr = (cp_parser_constant_expression
7755                         (parser, /*allow_non_constant_p=*/true,
7756                          &expr_non_constant_p));
7757                 if (expr_non_constant_p)
7758                   *non_constant_p = true;
7759               }
7760             else
7761               expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7762                                                       cast_p);
7763
7764             if (fold_expr_p)
7765               expr = instantiate_non_dependent_expr (expr);
7766
7767             /* If we have an ellipsis, then this is an expression
7768                expansion.  */
7769             if (allow_expansion_p
7770                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7771               {
7772                 /* Consume the `...'.  */
7773                 cp_lexer_consume_token (parser->lexer);
7774
7775                 /* Build the argument pack.  */
7776                 expr = make_pack_expansion (expr);
7777               }
7778
7779             if (wrap_locations_p)
7780               expr.maybe_add_location_wrapper ();
7781
7782              /* Add it to the list.  We add error_mark_node
7783                 expressions to the list, so that we can still tell if
7784                 the correct form for a parenthesized expression-list
7785                 is found. That gives better errors.  */
7786             vec_safe_push (expression_list, expr.get_value ());
7787
7788             if (expr == error_mark_node)
7789               goto skip_comma;
7790           }
7791
7792         /* After the first item, attribute lists look the same as
7793            expression lists.  */
7794         is_attribute_list = non_attr;
7795
7796       get_comma:;
7797         /* If the next token isn't a `,', then we are done.  */
7798         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7799           break;
7800
7801         /* Otherwise, consume the `,' and keep going.  */
7802         cp_lexer_consume_token (parser->lexer);
7803       }
7804
7805   if (close_paren_loc)
7806     *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7807
7808   if (!parens.require_close (parser))
7809     {
7810       int ending;
7811
7812     skip_comma:;
7813       /* We try and resync to an unnested comma, as that will give the
7814          user better diagnostics.  */
7815       ending = cp_parser_skip_to_closing_parenthesis (parser,
7816                                                       /*recovering=*/true,
7817                                                       /*or_comma=*/true,
7818                                                       /*consume_paren=*/true);
7819       if (ending < 0)
7820         goto get_comma;
7821       if (!ending)
7822         {
7823           parser->greater_than_is_operator_p
7824             = saved_greater_than_is_operator_p;
7825           return NULL;
7826         }
7827     }
7828
7829   parser->greater_than_is_operator_p
7830     = saved_greater_than_is_operator_p;
7831
7832   if (identifier)
7833     vec_safe_insert (expression_list, 0, identifier);
7834
7835   return expression_list;
7836 }
7837
7838 /* Parse a pseudo-destructor-name.
7839
7840    pseudo-destructor-name:
7841      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7842      :: [opt] nested-name-specifier template template-id :: ~ type-name
7843      :: [opt] nested-name-specifier [opt] ~ type-name
7844
7845    If either of the first two productions is used, sets *SCOPE to the
7846    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7847    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7848    or ERROR_MARK_NODE if the parse fails.  */
7849
7850 static void
7851 cp_parser_pseudo_destructor_name (cp_parser* parser,
7852                                   tree object,
7853                                   tree* scope,
7854                                   tree* type)
7855 {
7856   bool nested_name_specifier_p;
7857
7858   /* Handle ~auto.  */
7859   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7860       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7861       && !type_dependent_expression_p (object))
7862     {
7863       if (cxx_dialect < cxx14)
7864         pedwarn (input_location, 0,
7865                  "%<~auto%> only available with "
7866                  "-std=c++14 or -std=gnu++14");
7867       cp_lexer_consume_token (parser->lexer);
7868       cp_lexer_consume_token (parser->lexer);
7869       *scope = NULL_TREE;
7870       *type = TREE_TYPE (object);
7871       return;
7872     }
7873
7874   /* Assume that things will not work out.  */
7875   *type = error_mark_node;
7876
7877   /* Look for the optional `::' operator.  */
7878   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7879   /* Look for the optional nested-name-specifier.  */
7880   nested_name_specifier_p
7881     = (cp_parser_nested_name_specifier_opt (parser,
7882                                             /*typename_keyword_p=*/false,
7883                                             /*check_dependency_p=*/true,
7884                                             /*type_p=*/false,
7885                                             /*is_declaration=*/false)
7886        != NULL_TREE);
7887   /* Now, if we saw a nested-name-specifier, we might be doing the
7888      second production.  */
7889   if (nested_name_specifier_p
7890       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7891     {
7892       /* Consume the `template' keyword.  */
7893       cp_lexer_consume_token (parser->lexer);
7894       /* Parse the template-id.  */
7895       cp_parser_template_id (parser,
7896                              /*template_keyword_p=*/true,
7897                              /*check_dependency_p=*/false,
7898                              class_type,
7899                              /*is_declaration=*/true);
7900       /* Look for the `::' token.  */
7901       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7902     }
7903   /* If the next token is not a `~', then there might be some
7904      additional qualification.  */
7905   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7906     {
7907       /* At this point, we're looking for "type-name :: ~".  The type-name
7908          must not be a class-name, since this is a pseudo-destructor.  So,
7909          it must be either an enum-name, or a typedef-name -- both of which
7910          are just identifiers.  So, we peek ahead to check that the "::"
7911          and "~" tokens are present; if they are not, then we can avoid
7912          calling type_name.  */
7913       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7914           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7915           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7916         {
7917           cp_parser_error (parser, "non-scalar type");
7918           return;
7919         }
7920
7921       /* Look for the type-name.  */
7922       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7923       if (*scope == error_mark_node)
7924         return;
7925
7926       /* Look for the `::' token.  */
7927       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7928     }
7929   else
7930     *scope = NULL_TREE;
7931
7932   /* Look for the `~'.  */
7933   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7934
7935   /* Once we see the ~, this has to be a pseudo-destructor.  */
7936   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7937     cp_parser_commit_to_topmost_tentative_parse (parser);
7938
7939   /* Look for the type-name again.  We are not responsible for
7940      checking that it matches the first type-name.  */
7941   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7942 }
7943
7944 /* Parse a unary-expression.
7945
7946    unary-expression:
7947      postfix-expression
7948      ++ cast-expression
7949      -- cast-expression
7950      unary-operator cast-expression
7951      sizeof unary-expression
7952      sizeof ( type-id )
7953      alignof ( type-id )  [C++0x]
7954      new-expression
7955      delete-expression
7956
7957    GNU Extensions:
7958
7959    unary-expression:
7960      __extension__ cast-expression
7961      __alignof__ unary-expression
7962      __alignof__ ( type-id )
7963      alignof unary-expression  [C++0x]
7964      __real__ cast-expression
7965      __imag__ cast-expression
7966      && identifier
7967      sizeof ( type-id ) { initializer-list , [opt] }
7968      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7969      __alignof__ ( type-id ) { initializer-list , [opt] }
7970
7971    ADDRESS_P is true iff the unary-expression is appearing as the
7972    operand of the `&' operator.   CAST_P is true if this expression is
7973    the target of a cast.
7974
7975    Returns a representation of the expression.  */
7976
7977 static cp_expr
7978 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7979                             bool address_p, bool cast_p, bool decltype_p)
7980 {
7981   cp_token *token;
7982   enum tree_code unary_operator;
7983
7984   /* Peek at the next token.  */
7985   token = cp_lexer_peek_token (parser->lexer);
7986   /* Some keywords give away the kind of expression.  */
7987   if (token->type == CPP_KEYWORD)
7988     {
7989       enum rid keyword = token->keyword;
7990
7991       switch (keyword)
7992         {
7993         case RID_ALIGNOF:
7994         case RID_SIZEOF:
7995           {
7996             tree operand, ret;
7997             enum tree_code op;
7998             location_t start_loc = token->location;
7999
8000             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8001             bool std_alignof = id_equal (token->u.value, "alignof");
8002
8003             /* Consume the token.  */
8004             cp_lexer_consume_token (parser->lexer);
8005             /* Parse the operand.  */
8006             operand = cp_parser_sizeof_operand (parser, keyword);
8007
8008             if (TYPE_P (operand))
8009               ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8010                                                 true);
8011             else
8012               {
8013                 /* ISO C++ defines alignof only with types, not with
8014                    expressions. So pedwarn if alignof is used with a non-
8015                    type expression. However, __alignof__ is ok.  */
8016                 if (std_alignof)
8017                   pedwarn (token->location, OPT_Wpedantic,
8018                            "ISO C++ does not allow %<alignof%> "
8019                            "with a non-type");
8020
8021                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8022               }
8023             /* For SIZEOF_EXPR, just issue diagnostics, but keep
8024                SIZEOF_EXPR with the original operand.  */
8025             if (op == SIZEOF_EXPR && ret != error_mark_node)
8026               {
8027                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8028                   {
8029                     if (!processing_template_decl && TYPE_P (operand))
8030                       {
8031                         ret = build_min (SIZEOF_EXPR, size_type_node,
8032                                          build1 (NOP_EXPR, operand,
8033                                                  error_mark_node));
8034                         SIZEOF_EXPR_TYPE_P (ret) = 1;
8035                       }
8036                     else
8037                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8038                     TREE_SIDE_EFFECTS (ret) = 0;
8039                     TREE_READONLY (ret) = 1;
8040                   }
8041               }
8042
8043             /* Construct a location e.g. :
8044                alignof (expr)
8045                ^~~~~~~~~~~~~~
8046                with start == caret at the start of the "alignof"/"sizeof"
8047                token, with the endpoint at the final closing paren.  */
8048             location_t finish_loc
8049               = cp_lexer_previous_token (parser->lexer)->location;
8050             location_t compound_loc
8051               = make_location (start_loc, start_loc, finish_loc);
8052
8053             cp_expr ret_expr (ret);
8054             ret_expr.set_location (compound_loc);
8055             ret_expr = ret_expr.maybe_add_location_wrapper ();
8056             return ret_expr;
8057           }
8058
8059         case RID_NEW:
8060           return cp_parser_new_expression (parser);
8061
8062         case RID_DELETE:
8063           return cp_parser_delete_expression (parser);
8064
8065         case RID_EXTENSION:
8066           {
8067             /* The saved value of the PEDANTIC flag.  */
8068             int saved_pedantic;
8069             tree expr;
8070
8071             /* Save away the PEDANTIC flag.  */
8072             cp_parser_extension_opt (parser, &saved_pedantic);
8073             /* Parse the cast-expression.  */
8074             expr = cp_parser_simple_cast_expression (parser);
8075             /* Restore the PEDANTIC flag.  */
8076             pedantic = saved_pedantic;
8077
8078             return expr;
8079           }
8080
8081         case RID_REALPART:
8082         case RID_IMAGPART:
8083           {
8084             tree expression;
8085
8086             /* Consume the `__real__' or `__imag__' token.  */
8087             cp_lexer_consume_token (parser->lexer);
8088             /* Parse the cast-expression.  */
8089             expression = cp_parser_simple_cast_expression (parser);
8090             /* Create the complete representation.  */
8091             return build_x_unary_op (token->location,
8092                                      (keyword == RID_REALPART
8093                                       ? REALPART_EXPR : IMAGPART_EXPR),
8094                                      expression,
8095                                      tf_warning_or_error);
8096           }
8097           break;
8098
8099         case RID_TRANSACTION_ATOMIC:
8100         case RID_TRANSACTION_RELAXED:
8101           return cp_parser_transaction_expression (parser, keyword);
8102
8103         case RID_NOEXCEPT:
8104           {
8105             tree expr;
8106             const char *saved_message;
8107             bool saved_integral_constant_expression_p;
8108             bool saved_non_integral_constant_expression_p;
8109             bool saved_greater_than_is_operator_p;
8110
8111             location_t start_loc = token->location;
8112
8113             cp_lexer_consume_token (parser->lexer);
8114             matching_parens parens;
8115             parens.require_open (parser);
8116
8117             saved_message = parser->type_definition_forbidden_message;
8118             parser->type_definition_forbidden_message
8119               = G_("types may not be defined in %<noexcept%> expressions");
8120
8121             saved_integral_constant_expression_p
8122               = parser->integral_constant_expression_p;
8123             saved_non_integral_constant_expression_p
8124               = parser->non_integral_constant_expression_p;
8125             parser->integral_constant_expression_p = false;
8126
8127             saved_greater_than_is_operator_p
8128               = parser->greater_than_is_operator_p;
8129             parser->greater_than_is_operator_p = true;
8130
8131             ++cp_unevaluated_operand;
8132             ++c_inhibit_evaluation_warnings;
8133             ++cp_noexcept_operand;
8134             expr = cp_parser_expression (parser);
8135             --cp_noexcept_operand;
8136             --c_inhibit_evaluation_warnings;
8137             --cp_unevaluated_operand;
8138
8139             parser->greater_than_is_operator_p
8140               = saved_greater_than_is_operator_p;
8141
8142             parser->integral_constant_expression_p
8143               = saved_integral_constant_expression_p;
8144             parser->non_integral_constant_expression_p
8145               = saved_non_integral_constant_expression_p;
8146
8147             parser->type_definition_forbidden_message = saved_message;
8148
8149             location_t finish_loc
8150               = cp_lexer_peek_token (parser->lexer)->location;
8151             parens.require_close (parser);
8152
8153             /* Construct a location of the form:
8154                noexcept (expr)
8155                ^~~~~~~~~~~~~~~
8156                with start == caret, finishing at the close-paren.  */
8157             location_t noexcept_loc
8158               = make_location (start_loc, start_loc, finish_loc);
8159
8160             return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8161                             noexcept_loc);
8162           }
8163
8164         default:
8165           break;
8166         }
8167     }
8168
8169   /* Look for the `:: new' and `:: delete', which also signal the
8170      beginning of a new-expression, or delete-expression,
8171      respectively.  If the next token is `::', then it might be one of
8172      these.  */
8173   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8174     {
8175       enum rid keyword;
8176
8177       /* See if the token after the `::' is one of the keywords in
8178          which we're interested.  */
8179       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8180       /* If it's `new', we have a new-expression.  */
8181       if (keyword == RID_NEW)
8182         return cp_parser_new_expression (parser);
8183       /* Similarly, for `delete'.  */
8184       else if (keyword == RID_DELETE)
8185         return cp_parser_delete_expression (parser);
8186     }
8187
8188   /* Look for a unary operator.  */
8189   unary_operator = cp_parser_unary_operator (token);
8190   /* The `++' and `--' operators can be handled similarly, even though
8191      they are not technically unary-operators in the grammar.  */
8192   if (unary_operator == ERROR_MARK)
8193     {
8194       if (token->type == CPP_PLUS_PLUS)
8195         unary_operator = PREINCREMENT_EXPR;
8196       else if (token->type == CPP_MINUS_MINUS)
8197         unary_operator = PREDECREMENT_EXPR;
8198       /* Handle the GNU address-of-label extension.  */
8199       else if (cp_parser_allow_gnu_extensions_p (parser)
8200                && token->type == CPP_AND_AND)
8201         {
8202           tree identifier;
8203           tree expression;
8204           location_t start_loc = token->location;
8205
8206           /* Consume the '&&' token.  */
8207           cp_lexer_consume_token (parser->lexer);
8208           /* Look for the identifier.  */
8209           location_t finish_loc
8210             = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8211           identifier = cp_parser_identifier (parser);
8212           /* Construct a location of the form:
8213                &&label
8214                ^~~~~~~
8215              with caret==start at the "&&", finish at the end of the label.  */
8216           location_t combined_loc
8217             = make_location (start_loc, start_loc, finish_loc);
8218           /* Create an expression representing the address.  */
8219           expression = finish_label_address_expr (identifier, combined_loc);
8220           if (cp_parser_non_integral_constant_expression (parser,
8221                                                           NIC_ADDR_LABEL))
8222             expression = error_mark_node;
8223           return expression;
8224         }
8225     }
8226   if (unary_operator != ERROR_MARK)
8227     {
8228       cp_expr cast_expression;
8229       cp_expr expression = error_mark_node;
8230       non_integral_constant non_constant_p = NIC_NONE;
8231       location_t loc = token->location;
8232       tsubst_flags_t complain = complain_flags (decltype_p);
8233
8234       /* Consume the operator token.  */
8235       token = cp_lexer_consume_token (parser->lexer);
8236       enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8237
8238       /* Parse the cast-expression.  */
8239       cast_expression
8240         = cp_parser_cast_expression (parser,
8241                                      unary_operator == ADDR_EXPR,
8242                                      /*cast_p=*/false,
8243                                      /*decltype*/false,
8244                                      pidk);
8245
8246       /* Make a location:
8247             OP_TOKEN  CAST_EXPRESSION
8248             ^~~~~~~~~~~~~~~~~~~~~~~~~
8249          with start==caret at the operator token, and
8250          extending to the end of the cast_expression.  */
8251       loc = make_location (loc, loc, cast_expression.get_finish ());
8252
8253       /* Now, build an appropriate representation.  */
8254       switch (unary_operator)
8255         {
8256         case INDIRECT_REF:
8257           non_constant_p = NIC_STAR;
8258           expression = build_x_indirect_ref (loc, cast_expression,
8259                                              RO_UNARY_STAR,
8260                                              complain);
8261           /* TODO: build_x_indirect_ref does not always honor the
8262              location, so ensure it is set.  */
8263           expression.set_location (loc);
8264           break;
8265
8266         case ADDR_EXPR:
8267            non_constant_p = NIC_ADDR;
8268           /* Fall through.  */
8269         case BIT_NOT_EXPR:
8270           expression = build_x_unary_op (loc, unary_operator,
8271                                          cast_expression,
8272                                          complain);
8273           /* TODO: build_x_unary_op does not always honor the location,
8274              so ensure it is set.  */
8275           expression.set_location (loc);
8276           break;
8277
8278         case PREINCREMENT_EXPR:
8279         case PREDECREMENT_EXPR:
8280           non_constant_p = unary_operator == PREINCREMENT_EXPR
8281                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8282           /* Fall through.  */
8283         case NEGATE_EXPR:
8284           /* Immediately fold negation of a constant, unless the constant is 0
8285              (since -0 == 0) or it would overflow.  */
8286           if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8287               && CONSTANT_CLASS_P (cast_expression)
8288               && !integer_zerop (cast_expression)
8289               && !TREE_OVERFLOW (cast_expression))
8290             {
8291               tree folded = fold_build1 (unary_operator,
8292                                          TREE_TYPE (cast_expression),
8293                                          cast_expression);
8294               if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8295                 {
8296                   expression = cp_expr (folded, loc);
8297                   break;
8298                 }
8299             }
8300           /* Fall through.  */
8301         case UNARY_PLUS_EXPR:
8302         case TRUTH_NOT_EXPR:
8303           expression = finish_unary_op_expr (loc, unary_operator,
8304                                              cast_expression, complain);
8305           break;
8306
8307         default:
8308           gcc_unreachable ();
8309         }
8310
8311       if (non_constant_p != NIC_NONE
8312           && cp_parser_non_integral_constant_expression (parser,
8313                                                          non_constant_p))
8314         expression = error_mark_node;
8315
8316       return expression;
8317     }
8318
8319   return cp_parser_postfix_expression (parser, address_p, cast_p,
8320                                        /*member_access_only_p=*/false,
8321                                        decltype_p,
8322                                        pidk);
8323 }
8324
8325 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
8326    unary-operator, the corresponding tree code is returned.  */
8327
8328 static enum tree_code
8329 cp_parser_unary_operator (cp_token* token)
8330 {
8331   switch (token->type)
8332     {
8333     case CPP_MULT:
8334       return INDIRECT_REF;
8335
8336     case CPP_AND:
8337       return ADDR_EXPR;
8338
8339     case CPP_PLUS:
8340       return UNARY_PLUS_EXPR;
8341
8342     case CPP_MINUS:
8343       return NEGATE_EXPR;
8344
8345     case CPP_NOT:
8346       return TRUTH_NOT_EXPR;
8347
8348     case CPP_COMPL:
8349       return BIT_NOT_EXPR;
8350
8351     default:
8352       return ERROR_MARK;
8353     }
8354 }
8355
8356 /* Parse a new-expression.
8357
8358    new-expression:
8359      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8360      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8361
8362    Returns a representation of the expression.  */
8363
8364 static tree
8365 cp_parser_new_expression (cp_parser* parser)
8366 {
8367   bool global_scope_p;
8368   vec<tree, va_gc> *placement;
8369   tree type;
8370   vec<tree, va_gc> *initializer;
8371   tree nelts = NULL_TREE;
8372   tree ret;
8373
8374   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8375
8376   /* Look for the optional `::' operator.  */
8377   global_scope_p
8378     = (cp_parser_global_scope_opt (parser,
8379                                    /*current_scope_valid_p=*/false)
8380        != NULL_TREE);
8381   /* Look for the `new' operator.  */
8382   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8383   /* There's no easy way to tell a new-placement from the
8384      `( type-id )' construct.  */
8385   cp_parser_parse_tentatively (parser);
8386   /* Look for a new-placement.  */
8387   placement = cp_parser_new_placement (parser);
8388   /* If that didn't work out, there's no new-placement.  */
8389   if (!cp_parser_parse_definitely (parser))
8390     {
8391       if (placement != NULL)
8392         release_tree_vector (placement);
8393       placement = NULL;
8394     }
8395
8396   /* If the next token is a `(', then we have a parenthesized
8397      type-id.  */
8398   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8399     {
8400       cp_token *token;
8401       const char *saved_message = parser->type_definition_forbidden_message;
8402
8403       /* Consume the `('.  */
8404       matching_parens parens;
8405       parens.consume_open (parser);
8406
8407       /* Parse the type-id.  */
8408       parser->type_definition_forbidden_message
8409         = G_("types may not be defined in a new-expression");
8410       {
8411         type_id_in_expr_sentinel s (parser);
8412         type = cp_parser_type_id (parser);
8413       }
8414       parser->type_definition_forbidden_message = saved_message;
8415
8416       /* Look for the closing `)'.  */
8417       parens.require_close (parser);
8418       token = cp_lexer_peek_token (parser->lexer);
8419       /* There should not be a direct-new-declarator in this production,
8420          but GCC used to allowed this, so we check and emit a sensible error
8421          message for this case.  */
8422       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8423         {
8424           error_at (token->location,
8425                     "array bound forbidden after parenthesized type-id");
8426           inform (token->location, 
8427                   "try removing the parentheses around the type-id");
8428           cp_parser_direct_new_declarator (parser);
8429         }
8430     }
8431   /* Otherwise, there must be a new-type-id.  */
8432   else
8433     type = cp_parser_new_type_id (parser, &nelts);
8434
8435   /* If the next token is a `(' or '{', then we have a new-initializer.  */
8436   cp_token *token = cp_lexer_peek_token (parser->lexer);
8437   if (token->type == CPP_OPEN_PAREN
8438       || token->type == CPP_OPEN_BRACE)
8439     initializer = cp_parser_new_initializer (parser);
8440   else
8441     initializer = NULL;
8442
8443   /* A new-expression may not appear in an integral constant
8444      expression.  */
8445   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8446     ret = error_mark_node;
8447   /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8448      of a new-type-id or type-id of a new-expression, the new-expression shall
8449      contain a new-initializer of the form ( assignment-expression )".
8450      Additionally, consistently with the spirit of DR 1467, we want to accept
8451      'new auto { 2 }' too.  */
8452   else if ((ret = type_uses_auto (type))
8453            && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8454            && (vec_safe_length (initializer) != 1
8455                || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8456                    && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8457     {
8458       error_at (token->location,
8459                 "initialization of new-expression for type %<auto%> "
8460                 "requires exactly one element");
8461       ret = error_mark_node;
8462     }
8463   else
8464     {
8465       /* Construct a location e.g.:
8466            ptr = new int[100]
8467                  ^~~~~~~~~~~~
8468          with caret == start at the start of the "new" token, and the end
8469          at the end of the final token we consumed.  */
8470       cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8471       location_t end_loc = get_finish (end_tok->location);
8472       location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8473
8474       /* Create a representation of the new-expression.  */
8475       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8476                        tf_warning_or_error);
8477       protected_set_expr_location (ret, combined_loc);
8478     }
8479
8480   if (placement != NULL)
8481     release_tree_vector (placement);
8482   if (initializer != NULL)
8483     release_tree_vector (initializer);
8484
8485   return ret;
8486 }
8487
8488 /* Parse a new-placement.
8489
8490    new-placement:
8491      ( expression-list )
8492
8493    Returns the same representation as for an expression-list.  */
8494
8495 static vec<tree, va_gc> *
8496 cp_parser_new_placement (cp_parser* parser)
8497 {
8498   vec<tree, va_gc> *expression_list;
8499
8500   /* Parse the expression-list.  */
8501   expression_list = (cp_parser_parenthesized_expression_list
8502                      (parser, non_attr, /*cast_p=*/false,
8503                       /*allow_expansion_p=*/true,
8504                       /*non_constant_p=*/NULL));
8505
8506   if (expression_list && expression_list->is_empty ())
8507     error ("expected expression-list or type-id");
8508
8509   return expression_list;
8510 }
8511
8512 /* Parse a new-type-id.
8513
8514    new-type-id:
8515      type-specifier-seq new-declarator [opt]
8516
8517    Returns the TYPE allocated.  If the new-type-id indicates an array
8518    type, *NELTS is set to the number of elements in the last array
8519    bound; the TYPE will not include the last array bound.  */
8520
8521 static tree
8522 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8523 {
8524   cp_decl_specifier_seq type_specifier_seq;
8525   cp_declarator *new_declarator;
8526   cp_declarator *declarator;
8527   cp_declarator *outer_declarator;
8528   const char *saved_message;
8529
8530   /* The type-specifier sequence must not contain type definitions.
8531      (It cannot contain declarations of new types either, but if they
8532      are not definitions we will catch that because they are not
8533      complete.)  */
8534   saved_message = parser->type_definition_forbidden_message;
8535   parser->type_definition_forbidden_message
8536     = G_("types may not be defined in a new-type-id");
8537   /* Parse the type-specifier-seq.  */
8538   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8539                                 /*is_trailing_return=*/false,
8540                                 &type_specifier_seq);
8541   /* Restore the old message.  */
8542   parser->type_definition_forbidden_message = saved_message;
8543
8544   if (type_specifier_seq.type == error_mark_node)
8545     return error_mark_node;
8546
8547   /* Parse the new-declarator.  */
8548   new_declarator = cp_parser_new_declarator_opt (parser);
8549
8550   /* Determine the number of elements in the last array dimension, if
8551      any.  */
8552   *nelts = NULL_TREE;
8553   /* Skip down to the last array dimension.  */
8554   declarator = new_declarator;
8555   outer_declarator = NULL;
8556   while (declarator && (declarator->kind == cdk_pointer
8557                         || declarator->kind == cdk_ptrmem))
8558     {
8559       outer_declarator = declarator;
8560       declarator = declarator->declarator;
8561     }
8562   while (declarator
8563          && declarator->kind == cdk_array
8564          && declarator->declarator
8565          && declarator->declarator->kind == cdk_array)
8566     {
8567       outer_declarator = declarator;
8568       declarator = declarator->declarator;
8569     }
8570
8571   if (declarator && declarator->kind == cdk_array)
8572     {
8573       *nelts = declarator->u.array.bounds;
8574       if (*nelts == error_mark_node)
8575         *nelts = integer_one_node;
8576
8577       if (outer_declarator)
8578         outer_declarator->declarator = declarator->declarator;
8579       else
8580         new_declarator = NULL;
8581     }
8582
8583   return groktypename (&type_specifier_seq, new_declarator, false);
8584 }
8585
8586 /* Parse an (optional) new-declarator.
8587
8588    new-declarator:
8589      ptr-operator new-declarator [opt]
8590      direct-new-declarator
8591
8592    Returns the declarator.  */
8593
8594 static cp_declarator *
8595 cp_parser_new_declarator_opt (cp_parser* parser)
8596 {
8597   enum tree_code code;
8598   tree type, std_attributes = NULL_TREE;
8599   cp_cv_quals cv_quals;  
8600
8601   /* We don't know if there's a ptr-operator next, or not.  */
8602   cp_parser_parse_tentatively (parser);
8603   /* Look for a ptr-operator.  */
8604   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8605   /* If that worked, look for more new-declarators.  */
8606   if (cp_parser_parse_definitely (parser))
8607     {
8608       cp_declarator *declarator;
8609
8610       /* Parse another optional declarator.  */
8611       declarator = cp_parser_new_declarator_opt (parser);
8612
8613       declarator = cp_parser_make_indirect_declarator
8614         (code, type, cv_quals, declarator, std_attributes);
8615
8616       return declarator;
8617     }
8618
8619   /* If the next token is a `[', there is a direct-new-declarator.  */
8620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8621     return cp_parser_direct_new_declarator (parser);
8622
8623   return NULL;
8624 }
8625
8626 /* Parse a direct-new-declarator.
8627
8628    direct-new-declarator:
8629      [ expression ]
8630      direct-new-declarator [constant-expression]
8631
8632    */
8633
8634 static cp_declarator *
8635 cp_parser_direct_new_declarator (cp_parser* parser)
8636 {
8637   cp_declarator *declarator = NULL;
8638
8639   while (true)
8640     {
8641       tree expression;
8642       cp_token *token;
8643
8644       /* Look for the opening `['.  */
8645       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8646
8647       token = cp_lexer_peek_token (parser->lexer);
8648       expression = cp_parser_expression (parser);
8649       /* The standard requires that the expression have integral
8650          type.  DR 74 adds enumeration types.  We believe that the
8651          real intent is that these expressions be handled like the
8652          expression in a `switch' condition, which also allows
8653          classes with a single conversion to integral or
8654          enumeration type.  */
8655       if (!processing_template_decl)
8656         {
8657           expression
8658             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8659                                           expression,
8660                                           /*complain=*/true);
8661           if (!expression)
8662             {
8663               error_at (token->location,
8664                         "expression in new-declarator must have integral "
8665                         "or enumeration type");
8666               expression = error_mark_node;
8667             }
8668         }
8669
8670       /* Look for the closing `]'.  */
8671       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8672
8673       /* Add this bound to the declarator.  */
8674       declarator = make_array_declarator (declarator, expression);
8675
8676       /* If the next token is not a `[', then there are no more
8677          bounds.  */
8678       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8679         break;
8680     }
8681
8682   return declarator;
8683 }
8684
8685 /* Parse a new-initializer.
8686
8687    new-initializer:
8688      ( expression-list [opt] )
8689      braced-init-list
8690
8691    Returns a representation of the expression-list.  */
8692
8693 static vec<tree, va_gc> *
8694 cp_parser_new_initializer (cp_parser* parser)
8695 {
8696   vec<tree, va_gc> *expression_list;
8697
8698   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8699     {
8700       tree t;
8701       bool expr_non_constant_p;
8702       cp_lexer_set_source_position (parser->lexer);
8703       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8704       t = cp_parser_braced_list (parser, &expr_non_constant_p);
8705       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8706       expression_list = make_tree_vector_single (t);
8707     }
8708   else
8709     expression_list = (cp_parser_parenthesized_expression_list
8710                        (parser, non_attr, /*cast_p=*/false,
8711                         /*allow_expansion_p=*/true,
8712                         /*non_constant_p=*/NULL));
8713
8714   return expression_list;
8715 }
8716
8717 /* Parse a delete-expression.
8718
8719    delete-expression:
8720      :: [opt] delete cast-expression
8721      :: [opt] delete [ ] cast-expression
8722
8723    Returns a representation of the expression.  */
8724
8725 static tree
8726 cp_parser_delete_expression (cp_parser* parser)
8727 {
8728   bool global_scope_p;
8729   bool array_p;
8730   tree expression;
8731
8732   /* Look for the optional `::' operator.  */
8733   global_scope_p
8734     = (cp_parser_global_scope_opt (parser,
8735                                    /*current_scope_valid_p=*/false)
8736        != NULL_TREE);
8737   /* Look for the `delete' keyword.  */
8738   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8739   /* See if the array syntax is in use.  */
8740   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8741     {
8742       /* Consume the `[' token.  */
8743       cp_lexer_consume_token (parser->lexer);
8744       /* Look for the `]' token.  */
8745       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8746       /* Remember that this is the `[]' construct.  */
8747       array_p = true;
8748     }
8749   else
8750     array_p = false;
8751
8752   /* Parse the cast-expression.  */
8753   expression = cp_parser_simple_cast_expression (parser);
8754
8755   /* A delete-expression may not appear in an integral constant
8756      expression.  */
8757   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8758     return error_mark_node;
8759
8760   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8761                         tf_warning_or_error);
8762 }
8763
8764 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8765    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8766    0 otherwise.  */
8767
8768 static int
8769 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8770 {
8771   cp_token *token = cp_lexer_peek_token (parser->lexer);
8772   switch (token->type)
8773     {
8774     case CPP_COMMA:
8775     case CPP_SEMICOLON:
8776     case CPP_QUERY:
8777     case CPP_COLON:
8778     case CPP_CLOSE_SQUARE:
8779     case CPP_CLOSE_PAREN:
8780     case CPP_CLOSE_BRACE:
8781     case CPP_OPEN_BRACE:
8782     case CPP_DOT:
8783     case CPP_DOT_STAR:
8784     case CPP_DEREF:
8785     case CPP_DEREF_STAR:
8786     case CPP_DIV:
8787     case CPP_MOD:
8788     case CPP_LSHIFT:
8789     case CPP_RSHIFT:
8790     case CPP_LESS:
8791     case CPP_GREATER:
8792     case CPP_LESS_EQ:
8793     case CPP_GREATER_EQ:
8794     case CPP_EQ_EQ:
8795     case CPP_NOT_EQ:
8796     case CPP_EQ:
8797     case CPP_MULT_EQ:
8798     case CPP_DIV_EQ:
8799     case CPP_MOD_EQ:
8800     case CPP_PLUS_EQ:
8801     case CPP_MINUS_EQ:
8802     case CPP_RSHIFT_EQ:
8803     case CPP_LSHIFT_EQ:
8804     case CPP_AND_EQ:
8805     case CPP_XOR_EQ:
8806     case CPP_OR_EQ:
8807     case CPP_XOR:
8808     case CPP_OR:
8809     case CPP_OR_OR:
8810     case CPP_EOF:
8811     case CPP_ELLIPSIS:
8812       return 0;
8813
8814     case CPP_OPEN_PAREN:
8815       /* In ((type ()) () the last () isn't a valid cast-expression,
8816          so the whole must be parsed as postfix-expression.  */
8817       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8818              != CPP_CLOSE_PAREN;
8819
8820     case CPP_OPEN_SQUARE:
8821       /* '[' may start a primary-expression in obj-c++ and in C++11,
8822          as a lambda-expression, eg, '(void)[]{}'.  */
8823       if (cxx_dialect >= cxx11)
8824         return -1;
8825       return c_dialect_objc ();
8826
8827     case CPP_PLUS_PLUS:
8828     case CPP_MINUS_MINUS:
8829       /* '++' and '--' may or may not start a cast-expression:
8830
8831          struct T { void operator++(int); };
8832          void f() { (T())++; }
8833
8834          vs
8835
8836          int a;
8837          (int)++a;  */
8838       return -1;
8839
8840     default:
8841       return 1;
8842     }
8843 }
8844
8845 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8846    in the order: const_cast, static_cast, reinterpret_cast.
8847
8848    Don't suggest dynamic_cast.
8849
8850    Return the first legal cast kind found, or NULL otherwise.  */
8851
8852 static const char *
8853 get_cast_suggestion (tree dst_type, tree orig_expr)
8854 {
8855   tree trial;
8856
8857   /* Reuse the parser logic by attempting to build the various kinds of
8858      cast, with "complain" disabled.
8859      Identify the first such cast that is valid.  */
8860
8861   /* Don't attempt to run such logic within template processing.  */
8862   if (processing_template_decl)
8863     return NULL;
8864
8865   /* First try const_cast.  */
8866   trial = build_const_cast (dst_type, orig_expr, tf_none);
8867   if (trial != error_mark_node)
8868     return "const_cast";
8869
8870   /* If that fails, try static_cast.  */
8871   trial = build_static_cast (dst_type, orig_expr, tf_none);
8872   if (trial != error_mark_node)
8873     return "static_cast";
8874
8875   /* Finally, try reinterpret_cast.  */
8876   trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8877   if (trial != error_mark_node)
8878     return "reinterpret_cast";
8879
8880   /* No such cast possible.  */
8881   return NULL;
8882 }
8883
8884 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8885    suggesting how to convert a C-style cast of the form:
8886
8887      (DST_TYPE)ORIG_EXPR
8888
8889    to a C++-style cast.
8890
8891    The primary range of RICHLOC is asssumed to be that of the original
8892    expression.  OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8893    of the parens in the C-style cast.  */
8894
8895 static void
8896 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8897                       location_t close_paren_loc, tree orig_expr,
8898                       tree dst_type)
8899 {
8900   /* This function is non-trivial, so bail out now if the warning isn't
8901      going to be emitted.  */
8902   if (!warn_old_style_cast)
8903     return;
8904
8905   /* Try to find a legal C++ cast, trying them in order:
8906      const_cast, static_cast, reinterpret_cast.  */
8907   const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8908   if (!cast_suggestion)
8909     return;
8910
8911   /* Replace the open paren with "CAST_SUGGESTION<".  */
8912   pretty_printer pp;
8913   pp_printf (&pp, "%s<", cast_suggestion);
8914   rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8915
8916   /* Replace the close paren with "> (".  */
8917   rich_loc->add_fixit_replace (close_paren_loc, "> (");
8918
8919   /* Add a closing paren after the expr (the primary range of RICH_LOC).  */
8920   rich_loc->add_fixit_insert_after (")");
8921 }
8922
8923
8924 /* Parse a cast-expression.
8925
8926    cast-expression:
8927      unary-expression
8928      ( type-id ) cast-expression
8929
8930    ADDRESS_P is true iff the unary-expression is appearing as the
8931    operand of the `&' operator.   CAST_P is true if this expression is
8932    the target of a cast.
8933
8934    Returns a representation of the expression.  */
8935
8936 static cp_expr
8937 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8938                            bool decltype_p, cp_id_kind * pidk)
8939 {
8940   /* If it's a `(', then we might be looking at a cast.  */
8941   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8942     {
8943       tree type = NULL_TREE;
8944       cp_expr expr (NULL_TREE);
8945       int cast_expression = 0;
8946       const char *saved_message;
8947
8948       /* There's no way to know yet whether or not this is a cast.
8949          For example, `(int (3))' is a unary-expression, while `(int)
8950          3' is a cast.  So, we resort to parsing tentatively.  */
8951       cp_parser_parse_tentatively (parser);
8952       /* Types may not be defined in a cast.  */
8953       saved_message = parser->type_definition_forbidden_message;
8954       parser->type_definition_forbidden_message
8955         = G_("types may not be defined in casts");
8956       /* Consume the `('.  */
8957       matching_parens parens;
8958       cp_token *open_paren = parens.consume_open (parser);
8959       location_t open_paren_loc = open_paren->location;
8960       location_t close_paren_loc = UNKNOWN_LOCATION;
8961
8962       /* A very tricky bit is that `(struct S) { 3 }' is a
8963          compound-literal (which we permit in C++ as an extension).
8964          But, that construct is not a cast-expression -- it is a
8965          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
8966          is legal; if the compound-literal were a cast-expression,
8967          you'd need an extra set of parentheses.)  But, if we parse
8968          the type-id, and it happens to be a class-specifier, then we
8969          will commit to the parse at that point, because we cannot
8970          undo the action that is done when creating a new class.  So,
8971          then we cannot back up and do a postfix-expression.
8972
8973          Another tricky case is the following (c++/29234):
8974
8975          struct S { void operator () (); };
8976
8977          void foo ()
8978          {
8979            ( S()() );
8980          }
8981
8982          As a type-id we parse the parenthesized S()() as a function
8983          returning a function, groktypename complains and we cannot
8984          back up in this case either.
8985
8986          Therefore, we scan ahead to the closing `)', and check to see
8987          if the tokens after the `)' can start a cast-expression.  Otherwise
8988          we are dealing with an unary-expression, a postfix-expression
8989          or something else.
8990
8991          Yet another tricky case, in C++11, is the following (c++/54891):
8992
8993          (void)[]{};
8994
8995          The issue is that usually, besides the case of lambda-expressions,
8996          the parenthesized type-id cannot be followed by '[', and, eg, we
8997          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8998          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8999          we don't commit, we try a cast-expression, then an unary-expression.
9000
9001          Save tokens so that we can put them back.  */
9002       cp_lexer_save_tokens (parser->lexer);
9003
9004       /* We may be looking at a cast-expression.  */
9005       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9006                                                  /*consume_paren=*/true))
9007         cast_expression
9008           = cp_parser_tokens_start_cast_expression (parser);
9009
9010       /* Roll back the tokens we skipped.  */
9011       cp_lexer_rollback_tokens (parser->lexer);
9012       /* If we aren't looking at a cast-expression, simulate an error so
9013          that the call to cp_parser_error_occurred below returns true.  */
9014       if (!cast_expression)
9015         cp_parser_simulate_error (parser);
9016       else
9017         {
9018           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9019           parser->in_type_id_in_expr_p = true;
9020           /* Look for the type-id.  */
9021           type = cp_parser_type_id (parser);
9022           /* Look for the closing `)'.  */
9023           cp_token *close_paren = parens.require_close (parser);
9024           if (close_paren)
9025             close_paren_loc = close_paren->location;
9026           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9027         }
9028
9029       /* Restore the saved message.  */
9030       parser->type_definition_forbidden_message = saved_message;
9031
9032       /* At this point this can only be either a cast or a
9033          parenthesized ctor such as `(T ())' that looks like a cast to
9034          function returning T.  */
9035       if (!cp_parser_error_occurred (parser))
9036         {
9037           /* Only commit if the cast-expression doesn't start with
9038              '++', '--', or '[' in C++11.  */
9039           if (cast_expression > 0)
9040             cp_parser_commit_to_topmost_tentative_parse (parser);
9041
9042           expr = cp_parser_cast_expression (parser,
9043                                             /*address_p=*/false,
9044                                             /*cast_p=*/true,
9045                                             /*decltype_p=*/false,
9046                                             pidk);
9047
9048           if (cp_parser_parse_definitely (parser))
9049             {
9050               /* Warn about old-style casts, if so requested.  */
9051               if (warn_old_style_cast
9052                   && !in_system_header_at (input_location)
9053                   && !VOID_TYPE_P (type)
9054                   && current_lang_name != lang_name_c)
9055                 {
9056                   gcc_rich_location rich_loc (input_location);
9057                   maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9058                                         expr, type);
9059                   warning_at (&rich_loc, OPT_Wold_style_cast,
9060                               "use of old-style cast to %q#T", type);
9061                 }
9062
9063               /* Only type conversions to integral or enumeration types
9064                  can be used in constant-expressions.  */
9065               if (!cast_valid_in_integral_constant_expression_p (type)
9066                   && cp_parser_non_integral_constant_expression (parser,
9067                                                                  NIC_CAST))
9068                 return error_mark_node;
9069
9070               /* Perform the cast.  */
9071               /* Make a location:
9072                    (TYPE) EXPR
9073                    ^~~~~~~~~~~
9074                  with start==caret at the open paren, extending to the
9075                  end of "expr".  */
9076               location_t cast_loc = make_location (open_paren_loc,
9077                                                    open_paren_loc,
9078                                                    expr.get_finish ());
9079               expr = build_c_cast (cast_loc, type, expr);
9080               return expr;
9081             }
9082         }
9083       else 
9084         cp_parser_abort_tentative_parse (parser);
9085     }
9086
9087   /* If we get here, then it's not a cast, so it must be a
9088      unary-expression.  */
9089   return cp_parser_unary_expression (parser, pidk, address_p,
9090                                      cast_p, decltype_p);
9091 }
9092
9093 /* Parse a binary expression of the general form:
9094
9095    pm-expression:
9096      cast-expression
9097      pm-expression .* cast-expression
9098      pm-expression ->* cast-expression
9099
9100    multiplicative-expression:
9101      pm-expression
9102      multiplicative-expression * pm-expression
9103      multiplicative-expression / pm-expression
9104      multiplicative-expression % pm-expression
9105
9106    additive-expression:
9107      multiplicative-expression
9108      additive-expression + multiplicative-expression
9109      additive-expression - multiplicative-expression
9110
9111    shift-expression:
9112      additive-expression
9113      shift-expression << additive-expression
9114      shift-expression >> additive-expression
9115
9116    relational-expression:
9117      shift-expression
9118      relational-expression < shift-expression
9119      relational-expression > shift-expression
9120      relational-expression <= shift-expression
9121      relational-expression >= shift-expression
9122
9123   GNU Extension:
9124
9125    relational-expression:
9126      relational-expression <? shift-expression
9127      relational-expression >? shift-expression
9128
9129    equality-expression:
9130      relational-expression
9131      equality-expression == relational-expression
9132      equality-expression != relational-expression
9133
9134    and-expression:
9135      equality-expression
9136      and-expression & equality-expression
9137
9138    exclusive-or-expression:
9139      and-expression
9140      exclusive-or-expression ^ and-expression
9141
9142    inclusive-or-expression:
9143      exclusive-or-expression
9144      inclusive-or-expression | exclusive-or-expression
9145
9146    logical-and-expression:
9147      inclusive-or-expression
9148      logical-and-expression && inclusive-or-expression
9149
9150    logical-or-expression:
9151      logical-and-expression
9152      logical-or-expression || logical-and-expression
9153
9154    All these are implemented with a single function like:
9155
9156    binary-expression:
9157      simple-cast-expression
9158      binary-expression <token> binary-expression
9159
9160    CAST_P is true if this expression is the target of a cast.
9161
9162    The binops_by_token map is used to get the tree codes for each <token> type.
9163    binary-expressions are associated according to a precedence table.  */
9164
9165 #define TOKEN_PRECEDENCE(token)                              \
9166 (((token->type == CPP_GREATER                                \
9167    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9168   && !parser->greater_than_is_operator_p)                    \
9169  ? PREC_NOT_OPERATOR                                         \
9170  : binops_by_token[token->type].prec)
9171
9172 static cp_expr
9173 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9174                              bool no_toplevel_fold_p,
9175                              bool decltype_p,
9176                              enum cp_parser_prec prec,
9177                              cp_id_kind * pidk)
9178 {
9179   cp_parser_expression_stack stack;
9180   cp_parser_expression_stack_entry *sp = &stack[0];
9181   cp_parser_expression_stack_entry current;
9182   cp_expr rhs;
9183   cp_token *token;
9184   enum tree_code rhs_type;
9185   enum cp_parser_prec new_prec, lookahead_prec;
9186   tree overload;
9187
9188   /* Parse the first expression.  */
9189   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9190                       ? TRUTH_NOT_EXPR : ERROR_MARK);
9191   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9192                                            cast_p, decltype_p, pidk);
9193   current.prec = prec;
9194
9195   if (cp_parser_error_occurred (parser))
9196     return error_mark_node;
9197
9198   for (;;)
9199     {
9200       /* Get an operator token.  */
9201       token = cp_lexer_peek_token (parser->lexer);
9202
9203       if (warn_cxx11_compat
9204           && token->type == CPP_RSHIFT
9205           && !parser->greater_than_is_operator_p)
9206         {
9207           if (warning_at (token->location, OPT_Wc__11_compat,
9208                           "%<>>%> operator is treated"
9209                           " as two right angle brackets in C++11"))
9210             inform (token->location,
9211                     "suggest parentheses around %<>>%> expression");
9212         }
9213
9214       new_prec = TOKEN_PRECEDENCE (token);
9215       if (new_prec != PREC_NOT_OPERATOR
9216           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9217         /* This is a fold-expression; handle it later.  */
9218         new_prec = PREC_NOT_OPERATOR;
9219
9220       /* Popping an entry off the stack means we completed a subexpression:
9221          - either we found a token which is not an operator (`>' where it is not
9222            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9223            will happen repeatedly;
9224          - or, we found an operator which has lower priority.  This is the case
9225            where the recursive descent *ascends*, as in `3 * 4 + 5' after
9226            parsing `3 * 4'.  */
9227       if (new_prec <= current.prec)
9228         {
9229           if (sp == stack)
9230             break;
9231           else
9232             goto pop;
9233         }
9234
9235      get_rhs:
9236       current.tree_type = binops_by_token[token->type].tree_type;
9237       current.loc = token->location;
9238
9239       /* We used the operator token.  */
9240       cp_lexer_consume_token (parser->lexer);
9241
9242       /* For "false && x" or "true || x", x will never be executed;
9243          disable warnings while evaluating it.  */
9244       if (current.tree_type == TRUTH_ANDIF_EXPR)
9245         c_inhibit_evaluation_warnings +=
9246           cp_fully_fold (current.lhs) == truthvalue_false_node;
9247       else if (current.tree_type == TRUTH_ORIF_EXPR)
9248         c_inhibit_evaluation_warnings +=
9249           cp_fully_fold (current.lhs) == truthvalue_true_node;
9250
9251       /* Extract another operand.  It may be the RHS of this expression
9252          or the LHS of a new, higher priority expression.  */
9253       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9254                   ? TRUTH_NOT_EXPR : ERROR_MARK);
9255       rhs = cp_parser_simple_cast_expression (parser);
9256
9257       /* Get another operator token.  Look up its precedence to avoid
9258          building a useless (immediately popped) stack entry for common
9259          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
9260       token = cp_lexer_peek_token (parser->lexer);
9261       lookahead_prec = TOKEN_PRECEDENCE (token);
9262       if (lookahead_prec != PREC_NOT_OPERATOR
9263           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9264         lookahead_prec = PREC_NOT_OPERATOR;
9265       if (lookahead_prec > new_prec)
9266         {
9267           /* ... and prepare to parse the RHS of the new, higher priority
9268              expression.  Since precedence levels on the stack are
9269              monotonically increasing, we do not have to care about
9270              stack overflows.  */
9271           *sp = current;
9272           ++sp;
9273           current.lhs = rhs;
9274           current.lhs_type = rhs_type;
9275           current.prec = new_prec;
9276           new_prec = lookahead_prec;
9277           goto get_rhs;
9278
9279          pop:
9280           lookahead_prec = new_prec;
9281           /* If the stack is not empty, we have parsed into LHS the right side
9282              (`4' in the example above) of an expression we had suspended.
9283              We can use the information on the stack to recover the LHS (`3')
9284              from the stack together with the tree code (`MULT_EXPR'), and
9285              the precedence of the higher level subexpression
9286              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
9287              which will be used to actually build the additive expression.  */
9288           rhs = current.lhs;
9289           rhs_type = current.lhs_type;
9290           --sp;
9291           current = *sp;
9292         }
9293
9294       /* Undo the disabling of warnings done above.  */
9295       if (current.tree_type == TRUTH_ANDIF_EXPR)
9296         c_inhibit_evaluation_warnings -=
9297           cp_fully_fold (current.lhs) == truthvalue_false_node;
9298       else if (current.tree_type == TRUTH_ORIF_EXPR)
9299         c_inhibit_evaluation_warnings -=
9300           cp_fully_fold (current.lhs) == truthvalue_true_node;
9301
9302       if (warn_logical_not_paren
9303           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9304           && current.lhs_type == TRUTH_NOT_EXPR
9305           /* Avoid warning for !!x == y.  */
9306           && (TREE_CODE (current.lhs) != NE_EXPR
9307               || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9308           && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9309               || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9310                   /* Avoid warning for !b == y where b is boolean.  */
9311                   && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9312                       || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9313                           != BOOLEAN_TYPE))))
9314           /* Avoid warning for !!b == y where b is boolean.  */
9315           && (!DECL_P (current.lhs)
9316               || TREE_TYPE (current.lhs) == NULL_TREE
9317               || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9318         warn_logical_not_parentheses (current.loc, current.tree_type,
9319                                       current.lhs, maybe_constant_value (rhs));
9320
9321       overload = NULL;
9322
9323       location_t combined_loc = make_location (current.loc,
9324                                                current.lhs.get_start (),
9325                                                rhs.get_finish ());
9326
9327       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9328          ERROR_MARK for everything that is not a binary expression.
9329          This makes warn_about_parentheses miss some warnings that
9330          involve unary operators.  For unary expressions we should
9331          pass the correct tree_code unless the unary expression was
9332          surrounded by parentheses.
9333       */
9334       if (no_toplevel_fold_p
9335           && lookahead_prec <= current.prec
9336           && sp == stack)
9337         {
9338           if (current.lhs == error_mark_node || rhs == error_mark_node)
9339             current.lhs = error_mark_node;
9340           else
9341             {
9342               current.lhs
9343                 = build_min (current.tree_type,
9344                              TREE_CODE_CLASS (current.tree_type)
9345                              == tcc_comparison
9346                              ? boolean_type_node : TREE_TYPE (current.lhs),
9347                              current.lhs.get_value (), rhs.get_value ());
9348               SET_EXPR_LOCATION (current.lhs, combined_loc);
9349             }
9350         }
9351       else
9352         {
9353           current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9354                                            current.lhs, current.lhs_type,
9355                                            rhs, rhs_type, &overload,
9356                                            complain_flags (decltype_p));
9357           /* TODO: build_x_binary_op doesn't always honor the location.  */
9358           current.lhs.set_location (combined_loc);
9359         }
9360       current.lhs_type = current.tree_type;
9361
9362       /* If the binary operator required the use of an overloaded operator,
9363          then this expression cannot be an integral constant-expression.
9364          An overloaded operator can be used even if both operands are
9365          otherwise permissible in an integral constant-expression if at
9366          least one of the operands is of enumeration type.  */
9367
9368       if (overload
9369           && cp_parser_non_integral_constant_expression (parser,
9370                                                          NIC_OVERLOADED))
9371         return error_mark_node;
9372     }
9373
9374   return current.lhs;
9375 }
9376
9377 static cp_expr
9378 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9379                              bool no_toplevel_fold_p,
9380                              enum cp_parser_prec prec,
9381                              cp_id_kind * pidk)
9382 {
9383   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9384                                       /*decltype*/false, prec, pidk);
9385 }
9386
9387 /* Parse the `? expression : assignment-expression' part of a
9388    conditional-expression.  The LOGICAL_OR_EXPR is the
9389    logical-or-expression that started the conditional-expression.
9390    Returns a representation of the entire conditional-expression.
9391
9392    This routine is used by cp_parser_assignment_expression.
9393
9394      ? expression : assignment-expression
9395
9396    GNU Extensions:
9397
9398      ? : assignment-expression */
9399
9400 static tree
9401 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9402 {
9403   tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9404   cp_expr assignment_expr;
9405   struct cp_token *token;
9406   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9407
9408   /* Consume the `?' token.  */
9409   cp_lexer_consume_token (parser->lexer);
9410   token = cp_lexer_peek_token (parser->lexer);
9411   if (cp_parser_allow_gnu_extensions_p (parser)
9412       && token->type == CPP_COLON)
9413     {
9414       pedwarn (token->location, OPT_Wpedantic, 
9415                "ISO C++ does not allow ?: with omitted middle operand");
9416       /* Implicit true clause.  */
9417       expr = NULL_TREE;
9418       c_inhibit_evaluation_warnings +=
9419         folded_logical_or_expr == truthvalue_true_node;
9420       warn_for_omitted_condop (token->location, logical_or_expr);
9421     }
9422   else
9423     {
9424       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9425       parser->colon_corrects_to_scope_p = false;
9426       /* Parse the expression.  */
9427       c_inhibit_evaluation_warnings +=
9428         folded_logical_or_expr == truthvalue_false_node;
9429       expr = cp_parser_expression (parser);
9430       c_inhibit_evaluation_warnings +=
9431         ((folded_logical_or_expr == truthvalue_true_node)
9432          - (folded_logical_or_expr == truthvalue_false_node));
9433       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9434     }
9435
9436   /* The next token should be a `:'.  */
9437   cp_parser_require (parser, CPP_COLON, RT_COLON);
9438   /* Parse the assignment-expression.  */
9439   assignment_expr = cp_parser_assignment_expression (parser);
9440   c_inhibit_evaluation_warnings -=
9441     folded_logical_or_expr == truthvalue_true_node;
9442
9443   /* Make a location:
9444        LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9445        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9446      with the caret at the "?", ranging from the start of
9447      the logical_or_expr to the end of the assignment_expr.  */
9448   loc = make_location (loc,
9449                        logical_or_expr.get_start (),
9450                        assignment_expr.get_finish ());
9451
9452   /* Build the conditional-expression.  */
9453   return build_x_conditional_expr (loc, logical_or_expr,
9454                                    expr,
9455                                    assignment_expr,
9456                                    tf_warning_or_error);
9457 }
9458
9459 /* Parse an assignment-expression.
9460
9461    assignment-expression:
9462      conditional-expression
9463      logical-or-expression assignment-operator assignment_expression
9464      throw-expression
9465
9466    CAST_P is true if this expression is the target of a cast.
9467    DECLTYPE_P is true if this expression is the operand of decltype.
9468
9469    Returns a representation for the expression.  */
9470
9471 static cp_expr
9472 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9473                                  bool cast_p, bool decltype_p)
9474 {
9475   cp_expr expr;
9476
9477   /* If the next token is the `throw' keyword, then we're looking at
9478      a throw-expression.  */
9479   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9480     expr = cp_parser_throw_expression (parser);
9481   /* Otherwise, it must be that we are looking at a
9482      logical-or-expression.  */
9483   else
9484     {
9485       /* Parse the binary expressions (logical-or-expression).  */
9486       expr = cp_parser_binary_expression (parser, cast_p, false,
9487                                           decltype_p,
9488                                           PREC_NOT_OPERATOR, pidk);
9489       /* If the next token is a `?' then we're actually looking at a
9490          conditional-expression.  */
9491       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9492         return cp_parser_question_colon_clause (parser, expr);
9493       else
9494         {
9495           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9496
9497           /* If it's an assignment-operator, we're using the second
9498              production.  */
9499           enum tree_code assignment_operator
9500             = cp_parser_assignment_operator_opt (parser);
9501           if (assignment_operator != ERROR_MARK)
9502             {
9503               bool non_constant_p;
9504
9505               /* Parse the right-hand side of the assignment.  */
9506               cp_expr rhs = cp_parser_initializer_clause (parser,
9507                                                           &non_constant_p);
9508
9509               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9510                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9511
9512               /* An assignment may not appear in a
9513                  constant-expression.  */
9514               if (cp_parser_non_integral_constant_expression (parser,
9515                                                               NIC_ASSIGNMENT))
9516                 return error_mark_node;
9517               /* Build the assignment expression.  Its default
9518                  location:
9519                    LHS = RHS
9520                    ~~~~^~~~~
9521                  is the location of the '=' token as the
9522                  caret, ranging from the start of the lhs to the
9523                  end of the rhs.  */
9524               loc = make_location (loc,
9525                                    expr.get_start (),
9526                                    rhs.get_finish ());
9527               expr = build_x_modify_expr (loc, expr,
9528                                           assignment_operator,
9529                                           rhs,
9530                                           complain_flags (decltype_p));
9531               /* TODO: build_x_modify_expr doesn't honor the location,
9532                  so we must set it here.  */
9533               expr.set_location (loc);
9534             }
9535         }
9536     }
9537
9538   return expr;
9539 }
9540
9541 /* Parse an (optional) assignment-operator.
9542
9543    assignment-operator: one of
9544      = *= /= %= += -= >>= <<= &= ^= |=
9545
9546    GNU Extension:
9547
9548    assignment-operator: one of
9549      <?= >?=
9550
9551    If the next token is an assignment operator, the corresponding tree
9552    code is returned, and the token is consumed.  For example, for
9553    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
9554    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
9555    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
9556    operator, ERROR_MARK is returned.  */
9557
9558 static enum tree_code
9559 cp_parser_assignment_operator_opt (cp_parser* parser)
9560 {
9561   enum tree_code op;
9562   cp_token *token;
9563
9564   /* Peek at the next token.  */
9565   token = cp_lexer_peek_token (parser->lexer);
9566
9567   switch (token->type)
9568     {
9569     case CPP_EQ:
9570       op = NOP_EXPR;
9571       break;
9572
9573     case CPP_MULT_EQ:
9574       op = MULT_EXPR;
9575       break;
9576
9577     case CPP_DIV_EQ:
9578       op = TRUNC_DIV_EXPR;
9579       break;
9580
9581     case CPP_MOD_EQ:
9582       op = TRUNC_MOD_EXPR;
9583       break;
9584
9585     case CPP_PLUS_EQ:
9586       op = PLUS_EXPR;
9587       break;
9588
9589     case CPP_MINUS_EQ:
9590       op = MINUS_EXPR;
9591       break;
9592
9593     case CPP_RSHIFT_EQ:
9594       op = RSHIFT_EXPR;
9595       break;
9596
9597     case CPP_LSHIFT_EQ:
9598       op = LSHIFT_EXPR;
9599       break;
9600
9601     case CPP_AND_EQ:
9602       op = BIT_AND_EXPR;
9603       break;
9604
9605     case CPP_XOR_EQ:
9606       op = BIT_XOR_EXPR;
9607       break;
9608
9609     case CPP_OR_EQ:
9610       op = BIT_IOR_EXPR;
9611       break;
9612
9613     default:
9614       /* Nothing else is an assignment operator.  */
9615       op = ERROR_MARK;
9616     }
9617
9618   /* An operator followed by ... is a fold-expression, handled elsewhere.  */
9619   if (op != ERROR_MARK
9620       && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9621     op = ERROR_MARK;
9622
9623   /* If it was an assignment operator, consume it.  */
9624   if (op != ERROR_MARK)
9625     cp_lexer_consume_token (parser->lexer);
9626
9627   return op;
9628 }
9629
9630 /* Parse an expression.
9631
9632    expression:
9633      assignment-expression
9634      expression , assignment-expression
9635
9636    CAST_P is true if this expression is the target of a cast.
9637    DECLTYPE_P is true if this expression is the immediate operand of decltype,
9638      except possibly parenthesized or on the RHS of a comma (N3276).
9639
9640    Returns a representation of the expression.  */
9641
9642 static cp_expr
9643 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9644                       bool cast_p, bool decltype_p)
9645 {
9646   cp_expr expression = NULL_TREE;
9647   location_t loc = UNKNOWN_LOCATION;
9648
9649   while (true)
9650     {
9651       cp_expr assignment_expression;
9652
9653       /* Parse the next assignment-expression.  */
9654       assignment_expression
9655         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9656
9657       /* We don't create a temporary for a call that is the immediate operand
9658          of decltype or on the RHS of a comma.  But when we see a comma, we
9659          need to create a temporary for a call on the LHS.  */
9660       if (decltype_p && !processing_template_decl
9661           && TREE_CODE (assignment_expression) == CALL_EXPR
9662           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9663           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9664         assignment_expression
9665           = build_cplus_new (TREE_TYPE (assignment_expression),
9666                              assignment_expression, tf_warning_or_error);
9667
9668       /* If this is the first assignment-expression, we can just
9669          save it away.  */
9670       if (!expression)
9671         expression = assignment_expression;
9672       else
9673         {
9674           /* Create a location with caret at the comma, ranging
9675              from the start of the LHS to the end of the RHS.  */
9676           loc = make_location (loc,
9677                                expression.get_start (),
9678                                assignment_expression.get_finish ());
9679           expression = build_x_compound_expr (loc, expression,
9680                                               assignment_expression,
9681                                               complain_flags (decltype_p));
9682           expression.set_location (loc);
9683         }
9684       /* If the next token is not a comma, or we're in a fold-expression, then
9685          we are done with the expression.  */
9686       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9687           || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9688         break;
9689       /* Consume the `,'.  */
9690       loc = cp_lexer_peek_token (parser->lexer)->location;
9691       cp_lexer_consume_token (parser->lexer);
9692       /* A comma operator cannot appear in a constant-expression.  */
9693       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9694         expression = error_mark_node;
9695     }
9696
9697   return expression;
9698 }
9699
9700 /* Parse a constant-expression.
9701
9702    constant-expression:
9703      conditional-expression
9704
9705   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9706   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
9707   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
9708   is false, NON_CONSTANT_P should be NULL.  If STRICT_P is true,
9709   only parse a conditional-expression, otherwise parse an
9710   assignment-expression.  See below for rationale.  */
9711
9712 static cp_expr
9713 cp_parser_constant_expression (cp_parser* parser,
9714                                bool allow_non_constant_p,
9715                                bool *non_constant_p,
9716                                bool strict_p)
9717 {
9718   bool saved_integral_constant_expression_p;
9719   bool saved_allow_non_integral_constant_expression_p;
9720   bool saved_non_integral_constant_expression_p;
9721   cp_expr expression;
9722
9723   /* It might seem that we could simply parse the
9724      conditional-expression, and then check to see if it were
9725      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
9726      one that the compiler can figure out is constant, possibly after
9727      doing some simplifications or optimizations.  The standard has a
9728      precise definition of constant-expression, and we must honor
9729      that, even though it is somewhat more restrictive.
9730
9731      For example:
9732
9733        int i[(2, 3)];
9734
9735      is not a legal declaration, because `(2, 3)' is not a
9736      constant-expression.  The `,' operator is forbidden in a
9737      constant-expression.  However, GCC's constant-folding machinery
9738      will fold this operation to an INTEGER_CST for `3'.  */
9739
9740   /* Save the old settings.  */
9741   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9742   saved_allow_non_integral_constant_expression_p
9743     = parser->allow_non_integral_constant_expression_p;
9744   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9745   /* We are now parsing a constant-expression.  */
9746   parser->integral_constant_expression_p = true;
9747   parser->allow_non_integral_constant_expression_p
9748     = (allow_non_constant_p || cxx_dialect >= cxx11);
9749   parser->non_integral_constant_expression_p = false;
9750   /* Although the grammar says "conditional-expression", when not STRICT_P,
9751      we parse an "assignment-expression", which also permits
9752      "throw-expression" and the use of assignment operators.  In the case
9753      that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9754      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
9755      actually essential that we look for an assignment-expression.
9756      For example, cp_parser_initializer_clauses uses this function to
9757      determine whether a particular assignment-expression is in fact
9758      constant.  */
9759   if (strict_p)
9760     {
9761       /* Parse the binary expressions (logical-or-expression).  */
9762       expression = cp_parser_binary_expression (parser, false, false, false,
9763                                                 PREC_NOT_OPERATOR, NULL);
9764       /* If the next token is a `?' then we're actually looking at
9765          a conditional-expression; otherwise we're done.  */
9766       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9767         expression = cp_parser_question_colon_clause (parser, expression);
9768     }
9769   else
9770     expression = cp_parser_assignment_expression (parser);
9771   /* Restore the old settings.  */
9772   parser->integral_constant_expression_p
9773     = saved_integral_constant_expression_p;
9774   parser->allow_non_integral_constant_expression_p
9775     = saved_allow_non_integral_constant_expression_p;
9776   if (cxx_dialect >= cxx11)
9777     {
9778       /* Require an rvalue constant expression here; that's what our
9779          callers expect.  Reference constant expressions are handled
9780          separately in e.g. cp_parser_template_argument.  */
9781       tree decay = expression;
9782       if (TREE_TYPE (expression)
9783           && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9784         decay = build_address (expression);
9785       bool is_const = potential_rvalue_constant_expression (decay);
9786       parser->non_integral_constant_expression_p = !is_const;
9787       if (!is_const && !allow_non_constant_p)
9788         require_potential_rvalue_constant_expression (decay);
9789     }
9790   if (allow_non_constant_p)
9791     *non_constant_p = parser->non_integral_constant_expression_p;
9792   parser->non_integral_constant_expression_p
9793     = saved_non_integral_constant_expression_p;
9794
9795   return expression;
9796 }
9797
9798 /* Parse __builtin_offsetof.
9799
9800    offsetof-expression:
9801      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9802
9803    offsetof-member-designator:
9804      id-expression
9805      | offsetof-member-designator "." id-expression
9806      | offsetof-member-designator "[" expression "]"
9807      | offsetof-member-designator "->" id-expression  */
9808
9809 static cp_expr
9810 cp_parser_builtin_offsetof (cp_parser *parser)
9811 {
9812   int save_ice_p, save_non_ice_p;
9813   tree type;
9814   cp_expr expr;
9815   cp_id_kind dummy;
9816   cp_token *token;
9817   location_t finish_loc;
9818
9819   /* We're about to accept non-integral-constant things, but will
9820      definitely yield an integral constant expression.  Save and
9821      restore these values around our local parsing.  */
9822   save_ice_p = parser->integral_constant_expression_p;
9823   save_non_ice_p = parser->non_integral_constant_expression_p;
9824
9825   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9826
9827   /* Consume the "__builtin_offsetof" token.  */
9828   cp_lexer_consume_token (parser->lexer);
9829   /* Consume the opening `('.  */
9830   matching_parens parens;
9831   parens.require_open (parser);
9832   /* Parse the type-id.  */
9833   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9834   {
9835     const char *saved_message = parser->type_definition_forbidden_message;
9836     parser->type_definition_forbidden_message
9837       = G_("types may not be defined within __builtin_offsetof");
9838     type = cp_parser_type_id (parser);
9839     parser->type_definition_forbidden_message = saved_message;
9840   }
9841   /* Look for the `,'.  */
9842   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9843   token = cp_lexer_peek_token (parser->lexer);
9844
9845   /* Build the (type *)null that begins the traditional offsetof macro.  */
9846   tree object_ptr
9847     = build_static_cast (build_pointer_type (type), null_pointer_node,
9848                          tf_warning_or_error);
9849
9850   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
9851   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9852                                                  true, &dummy, token->location);
9853   while (true)
9854     {
9855       token = cp_lexer_peek_token (parser->lexer);
9856       switch (token->type)
9857         {
9858         case CPP_OPEN_SQUARE:
9859           /* offsetof-member-designator "[" expression "]" */
9860           expr = cp_parser_postfix_open_square_expression (parser, expr,
9861                                                            true, false);
9862           break;
9863
9864         case CPP_DEREF:
9865           /* offsetof-member-designator "->" identifier */
9866           expr = grok_array_decl (token->location, expr,
9867                                   integer_zero_node, false);
9868           /* FALLTHRU */
9869
9870         case CPP_DOT:
9871           /* offsetof-member-designator "." identifier */
9872           cp_lexer_consume_token (parser->lexer);
9873           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9874                                                          expr, true, &dummy,
9875                                                          token->location);
9876           break;
9877
9878         case CPP_CLOSE_PAREN:
9879           /* Consume the ")" token.  */
9880           finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9881           cp_lexer_consume_token (parser->lexer);
9882           goto success;
9883
9884         default:
9885           /* Error.  We know the following require will fail, but
9886              that gives the proper error message.  */
9887           parens.require_close (parser);
9888           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9889           expr = error_mark_node;
9890           goto failure;
9891         }
9892     }
9893
9894  success:
9895   /* Make a location of the form:
9896        __builtin_offsetof (struct s, f)
9897        ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9898      with caret at the type-id, ranging from the start of the
9899      "_builtin_offsetof" token to the close paren.  */
9900   loc = make_location (loc, start_loc, finish_loc);
9901   /* The result will be an INTEGER_CST, so we need to explicitly
9902      preserve the location.  */
9903   expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9904
9905  failure:
9906   parser->integral_constant_expression_p = save_ice_p;
9907   parser->non_integral_constant_expression_p = save_non_ice_p;
9908
9909   expr = expr.maybe_add_location_wrapper ();
9910   return expr;
9911 }
9912
9913 /* Parse a trait expression.
9914
9915    Returns a representation of the expression, the underlying type
9916    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
9917
9918 static cp_expr
9919 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9920 {
9921   cp_trait_kind kind;
9922   tree type1, type2 = NULL_TREE;
9923   bool binary = false;
9924   bool variadic = false;
9925
9926   switch (keyword)
9927     {
9928     case RID_HAS_NOTHROW_ASSIGN:
9929       kind = CPTK_HAS_NOTHROW_ASSIGN;
9930       break;
9931     case RID_HAS_NOTHROW_CONSTRUCTOR:
9932       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9933       break;
9934     case RID_HAS_NOTHROW_COPY:
9935       kind = CPTK_HAS_NOTHROW_COPY;
9936       break;
9937     case RID_HAS_TRIVIAL_ASSIGN:
9938       kind = CPTK_HAS_TRIVIAL_ASSIGN;
9939       break;
9940     case RID_HAS_TRIVIAL_CONSTRUCTOR:
9941       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9942       break;
9943     case RID_HAS_TRIVIAL_COPY:
9944       kind = CPTK_HAS_TRIVIAL_COPY;
9945       break;
9946     case RID_HAS_TRIVIAL_DESTRUCTOR:
9947       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9948       break;
9949     case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9950       kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9951       break;
9952     case RID_HAS_VIRTUAL_DESTRUCTOR:
9953       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9954       break;
9955     case RID_IS_ABSTRACT:
9956       kind = CPTK_IS_ABSTRACT;
9957       break;
9958     case RID_IS_AGGREGATE:
9959       kind = CPTK_IS_AGGREGATE;
9960       break;
9961     case RID_IS_BASE_OF:
9962       kind = CPTK_IS_BASE_OF;
9963       binary = true;
9964       break;
9965     case RID_IS_CLASS:
9966       kind = CPTK_IS_CLASS;
9967       break;
9968     case RID_IS_EMPTY:
9969       kind = CPTK_IS_EMPTY;
9970       break;
9971     case RID_IS_ENUM:
9972       kind = CPTK_IS_ENUM;
9973       break;
9974     case RID_IS_FINAL:
9975       kind = CPTK_IS_FINAL;
9976       break;
9977     case RID_IS_LITERAL_TYPE:
9978       kind = CPTK_IS_LITERAL_TYPE;
9979       break;
9980     case RID_IS_POD:
9981       kind = CPTK_IS_POD;
9982       break;
9983     case RID_IS_POLYMORPHIC:
9984       kind = CPTK_IS_POLYMORPHIC;
9985       break;
9986     case RID_IS_SAME_AS:
9987       kind = CPTK_IS_SAME_AS;
9988       binary = true;
9989       break;
9990     case RID_IS_STD_LAYOUT:
9991       kind = CPTK_IS_STD_LAYOUT;
9992       break;
9993     case RID_IS_TRIVIAL:
9994       kind = CPTK_IS_TRIVIAL;
9995       break;
9996     case RID_IS_TRIVIALLY_ASSIGNABLE:
9997       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9998       binary = true;
9999       break;
10000     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10001       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10002       variadic = true;
10003       break;
10004     case RID_IS_TRIVIALLY_COPYABLE:
10005       kind = CPTK_IS_TRIVIALLY_COPYABLE;
10006       break;
10007     case RID_IS_UNION:
10008       kind = CPTK_IS_UNION;
10009       break;
10010     case RID_UNDERLYING_TYPE:
10011       kind = CPTK_UNDERLYING_TYPE;
10012       break;
10013     case RID_BASES:
10014       kind = CPTK_BASES;
10015       break;
10016     case RID_DIRECT_BASES:
10017       kind = CPTK_DIRECT_BASES;
10018       break;
10019     case RID_IS_ASSIGNABLE:
10020       kind = CPTK_IS_ASSIGNABLE;
10021       binary = true;
10022       break;
10023     case RID_IS_CONSTRUCTIBLE:
10024       kind = CPTK_IS_CONSTRUCTIBLE;
10025       variadic = true;
10026       break;
10027     default:
10028       gcc_unreachable ();
10029     }
10030
10031   /* Get location of initial token.  */
10032   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10033
10034   /* Consume the token.  */
10035   cp_lexer_consume_token (parser->lexer);
10036
10037   matching_parens parens;
10038   parens.require_open (parser);
10039
10040   {
10041     type_id_in_expr_sentinel s (parser);
10042     type1 = cp_parser_type_id (parser);
10043   }
10044
10045   if (type1 == error_mark_node)
10046     return error_mark_node;
10047
10048   if (binary)
10049     {
10050       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10051  
10052       {
10053         type_id_in_expr_sentinel s (parser);
10054         type2 = cp_parser_type_id (parser);
10055       }
10056
10057       if (type2 == error_mark_node)
10058         return error_mark_node;
10059     }
10060   else if (variadic)
10061     {
10062       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10063         {
10064           cp_lexer_consume_token (parser->lexer);
10065           tree elt = cp_parser_type_id (parser);
10066           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10067             {
10068               cp_lexer_consume_token (parser->lexer);
10069               elt = make_pack_expansion (elt);
10070             }
10071           if (elt == error_mark_node)
10072             return error_mark_node;
10073           type2 = tree_cons (NULL_TREE, elt, type2);
10074         }
10075     }
10076
10077   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10078   parens.require_close (parser);
10079
10080   /* Construct a location of the form:
10081        __is_trivially_copyable(_Tp)
10082        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10083      with start == caret, finishing at the close-paren.  */
10084   location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10085
10086   /* Complete the trait expression, which may mean either processing
10087      the trait expr now or saving it for template instantiation.  */
10088   switch (kind)
10089     {
10090     case CPTK_UNDERLYING_TYPE:
10091       return cp_expr (finish_underlying_type (type1), trait_loc);
10092     case CPTK_BASES:
10093       return cp_expr (finish_bases (type1, false), trait_loc);
10094     case CPTK_DIRECT_BASES:
10095       return cp_expr (finish_bases (type1, true), trait_loc);
10096     default:
10097       return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10098     }
10099 }
10100
10101 /* Parse a lambda expression.
10102
10103    lambda-expression:
10104      lambda-introducer lambda-declarator [opt] compound-statement
10105
10106    Returns a representation of the expression.  */
10107
10108 static cp_expr
10109 cp_parser_lambda_expression (cp_parser* parser)
10110 {
10111   tree lambda_expr = build_lambda_expr ();
10112   tree type;
10113   bool ok = true;
10114   cp_token *token = cp_lexer_peek_token (parser->lexer);
10115   cp_token_position start = 0;
10116
10117   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10118
10119   if (cp_unevaluated_operand)
10120     {
10121       if (!token->error_reported)
10122         {
10123           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10124                     "lambda-expression in unevaluated context");
10125           token->error_reported = true;
10126         }
10127       ok = false;
10128     }
10129   else if (parser->in_template_argument_list_p)
10130     {
10131       if (!token->error_reported)
10132         {
10133           error_at (token->location, "lambda-expression in template-argument");
10134           token->error_reported = true;
10135         }
10136       ok = false;
10137     }
10138
10139   /* We may be in the middle of deferred access check.  Disable
10140      it now.  */
10141   push_deferring_access_checks (dk_no_deferred);
10142
10143   cp_parser_lambda_introducer (parser, lambda_expr);
10144
10145   type = begin_lambda_type (lambda_expr);
10146   if (type == error_mark_node)
10147     return error_mark_node;
10148
10149   record_lambda_scope (lambda_expr);
10150
10151   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
10152   determine_visibility (TYPE_NAME (type));
10153
10154   /* Now that we've started the type, add the capture fields for any
10155      explicit captures.  */
10156   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10157
10158   {
10159     /* Inside the class, surrounding template-parameter-lists do not apply.  */
10160     unsigned int saved_num_template_parameter_lists
10161         = parser->num_template_parameter_lists;
10162     unsigned char in_statement = parser->in_statement;
10163     bool in_switch_statement_p = parser->in_switch_statement_p;
10164     bool fully_implicit_function_template_p
10165         = parser->fully_implicit_function_template_p;
10166     tree implicit_template_parms = parser->implicit_template_parms;
10167     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10168     bool auto_is_implicit_function_template_parm_p
10169         = parser->auto_is_implicit_function_template_parm_p;
10170
10171     parser->num_template_parameter_lists = 0;
10172     parser->in_statement = 0;
10173     parser->in_switch_statement_p = false;
10174     parser->fully_implicit_function_template_p = false;
10175     parser->implicit_template_parms = 0;
10176     parser->implicit_template_scope = 0;
10177     parser->auto_is_implicit_function_template_parm_p = false;
10178
10179     /* By virtue of defining a local class, a lambda expression has access to
10180        the private variables of enclosing classes.  */
10181
10182     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10183
10184     if (ok && cp_parser_error_occurred (parser))
10185       ok = false;
10186
10187     if (ok)
10188       {
10189         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10190             && cp_parser_start_tentative_firewall (parser))
10191           start = token;
10192         cp_parser_lambda_body (parser, lambda_expr);
10193       }
10194     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10195       {
10196         if (cp_parser_skip_to_closing_brace (parser))
10197           cp_lexer_consume_token (parser->lexer);
10198       }
10199
10200     /* The capture list was built up in reverse order; fix that now.  */
10201     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10202       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10203
10204     if (ok)
10205       maybe_add_lambda_conv_op (type);
10206
10207     type = finish_struct (type, /*attributes=*/NULL_TREE);
10208
10209     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10210     parser->in_statement = in_statement;
10211     parser->in_switch_statement_p = in_switch_statement_p;
10212     parser->fully_implicit_function_template_p
10213         = fully_implicit_function_template_p;
10214     parser->implicit_template_parms = implicit_template_parms;
10215     parser->implicit_template_scope = implicit_template_scope;
10216     parser->auto_is_implicit_function_template_parm_p
10217         = auto_is_implicit_function_template_parm_p;
10218   }
10219
10220   /* This field is only used during parsing of the lambda.  */
10221   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10222
10223   /* This lambda shouldn't have any proxies left at this point.  */
10224   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10225   /* And now that we're done, push proxies for an enclosing lambda.  */
10226   insert_pending_capture_proxies ();
10227
10228   if (ok)
10229     lambda_expr = build_lambda_object (lambda_expr);
10230   else
10231     lambda_expr = error_mark_node;
10232
10233   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10234
10235   pop_deferring_access_checks ();
10236
10237   return lambda_expr;
10238 }
10239
10240 /* Parse the beginning of a lambda expression.
10241
10242    lambda-introducer:
10243      [ lambda-capture [opt] ]
10244
10245    LAMBDA_EXPR is the current representation of the lambda expression.  */
10246
10247 static void
10248 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10249 {
10250   /* Need commas after the first capture.  */
10251   bool first = true;
10252
10253   /* Eat the leading `['.  */
10254   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10255
10256   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
10257   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10258       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10259     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10260   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10261     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10262
10263   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10264     {
10265       cp_lexer_consume_token (parser->lexer);
10266       first = false;
10267     }
10268
10269   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10270     {
10271       cp_token* capture_token;
10272       tree capture_id;
10273       tree capture_init_expr;
10274       cp_id_kind idk = CP_ID_KIND_NONE;
10275       bool explicit_init_p = false;
10276
10277       enum capture_kind_type
10278       {
10279         BY_COPY,
10280         BY_REFERENCE
10281       };
10282       enum capture_kind_type capture_kind = BY_COPY;
10283
10284       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10285         {
10286           error ("expected end of capture-list");
10287           return;
10288         }
10289
10290       if (first)
10291         first = false;
10292       else
10293         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10294
10295       /* Possibly capture `this'.  */
10296       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10297         {
10298           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10299           if (cxx_dialect < cxx2a
10300               && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10301             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10302                      "with by-copy capture default");
10303           cp_lexer_consume_token (parser->lexer);
10304           add_capture (lambda_expr,
10305                        /*id=*/this_identifier,
10306                        /*initializer=*/finish_this_expr (),
10307                        /*by_reference_p=*/true,
10308                        explicit_init_p);
10309           continue;
10310         }
10311
10312       /* Possibly capture `*this'.  */
10313       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10314           && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10315         {
10316           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10317           if (cxx_dialect < cxx17)
10318             pedwarn (loc, 0, "%<*this%> capture only available with "
10319                              "-std=c++17 or -std=gnu++17");
10320           cp_lexer_consume_token (parser->lexer);
10321           cp_lexer_consume_token (parser->lexer);
10322           add_capture (lambda_expr,
10323                        /*id=*/this_identifier,
10324                        /*initializer=*/finish_this_expr (),
10325                        /*by_reference_p=*/false,
10326                        explicit_init_p);
10327           continue;
10328         }
10329
10330       /* Remember whether we want to capture as a reference or not.  */
10331       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10332         {
10333           capture_kind = BY_REFERENCE;
10334           cp_lexer_consume_token (parser->lexer);
10335         }
10336
10337       /* Get the identifier.  */
10338       capture_token = cp_lexer_peek_token (parser->lexer);
10339       capture_id = cp_parser_identifier (parser);
10340
10341       if (capture_id == error_mark_node)
10342         /* Would be nice to have a cp_parser_skip_to_closing_x for general
10343            delimiters, but I modified this to stop on unnested ']' as well.  It
10344            was already changed to stop on unnested '}', so the
10345            "closing_parenthesis" name is no more misleading with my change.  */
10346         {
10347           cp_parser_skip_to_closing_parenthesis (parser,
10348                                                  /*recovering=*/true,
10349                                                  /*or_comma=*/true,
10350                                                  /*consume_paren=*/true);
10351           break;
10352         }
10353
10354       /* Find the initializer for this capture.  */
10355       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10356           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10357           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10358         {
10359           bool direct, non_constant;
10360           /* An explicit initializer exists.  */
10361           if (cxx_dialect < cxx14)
10362             pedwarn (input_location, 0,
10363                      "lambda capture initializers "
10364                      "only available with -std=c++14 or -std=gnu++14");
10365           capture_init_expr = cp_parser_initializer (parser, &direct,
10366                                                      &non_constant, true);
10367           explicit_init_p = true;
10368           if (capture_init_expr == NULL_TREE)
10369             {
10370               error ("empty initializer for lambda init-capture");
10371               capture_init_expr = error_mark_node;
10372             }
10373         }
10374       else
10375         {
10376           const char* error_msg;
10377
10378           /* Turn the identifier into an id-expression.  */
10379           capture_init_expr
10380             = cp_parser_lookup_name_simple (parser, capture_id,
10381                                             capture_token->location);
10382
10383           if (capture_init_expr == error_mark_node)
10384             {
10385               unqualified_name_lookup_error (capture_id);
10386               continue;
10387             }
10388           else if (!VAR_P (capture_init_expr)
10389                    && TREE_CODE (capture_init_expr) != PARM_DECL)
10390             {
10391               error_at (capture_token->location,
10392                         "capture of non-variable %qE",
10393                         capture_init_expr);
10394               if (DECL_P (capture_init_expr))
10395                 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10396                         "%q#D declared here", capture_init_expr);
10397               continue;
10398             }
10399           if (VAR_P (capture_init_expr)
10400               && decl_storage_duration (capture_init_expr) != dk_auto)
10401             {
10402               if (pedwarn (capture_token->location, 0, "capture of variable "
10403                            "%qD with non-automatic storage duration",
10404                            capture_init_expr))
10405                 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10406                         "%q#D declared here", capture_init_expr);
10407               continue;
10408             }
10409
10410           capture_init_expr
10411             = finish_id_expression
10412                 (capture_id,
10413                  capture_init_expr,
10414                  parser->scope,
10415                  &idk,
10416                  /*integral_constant_expression_p=*/false,
10417                  /*allow_non_integral_constant_expression_p=*/false,
10418                  /*non_integral_constant_expression_p=*/NULL,
10419                  /*template_p=*/false,
10420                  /*done=*/true,
10421                  /*address_p=*/false,
10422                  /*template_arg_p=*/false,
10423                  &error_msg,
10424                  capture_token->location);
10425
10426           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10427             {
10428               cp_lexer_consume_token (parser->lexer);
10429               capture_init_expr = make_pack_expansion (capture_init_expr);
10430             }
10431         }
10432
10433       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10434           && !explicit_init_p)
10435         {
10436           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10437               && capture_kind == BY_COPY)
10438             pedwarn (capture_token->location, 0, "explicit by-copy capture "
10439                      "of %qD redundant with by-copy capture default",
10440                      capture_id);
10441           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10442               && capture_kind == BY_REFERENCE)
10443             pedwarn (capture_token->location, 0, "explicit by-reference "
10444                      "capture of %qD redundant with by-reference capture "
10445                      "default", capture_id);
10446         }
10447
10448       add_capture (lambda_expr,
10449                    capture_id,
10450                    capture_init_expr,
10451                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
10452                    explicit_init_p);
10453
10454       /* If there is any qualification still in effect, clear it
10455          now; we will be starting fresh with the next capture.  */
10456       parser->scope = NULL_TREE;
10457       parser->qualifying_scope = NULL_TREE;
10458       parser->object_scope = NULL_TREE;
10459     }
10460
10461   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10462 }
10463
10464 /* Parse the (optional) middle of a lambda expression.
10465
10466    lambda-declarator:
10467      < template-parameter-list [opt] >
10468      ( parameter-declaration-clause [opt] )
10469        attribute-specifier [opt]
10470        decl-specifier-seq [opt]
10471        exception-specification [opt]
10472        lambda-return-type-clause [opt]
10473
10474    LAMBDA_EXPR is the current representation of the lambda expression.  */
10475
10476 static bool
10477 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10478 {
10479   /* 5.1.1.4 of the standard says:
10480        If a lambda-expression does not include a lambda-declarator, it is as if
10481        the lambda-declarator were ().
10482      This means an empty parameter list, no attributes, and no exception
10483      specification.  */
10484   tree param_list = void_list_node;
10485   tree attributes = NULL_TREE;
10486   tree exception_spec = NULL_TREE;
10487   tree template_param_list = NULL_TREE;
10488   tree tx_qual = NULL_TREE;
10489   tree return_type = NULL_TREE;
10490   cp_decl_specifier_seq lambda_specs;
10491   clear_decl_specs (&lambda_specs);
10492
10493   /* The template-parameter-list is optional, but must begin with
10494      an opening angle if present.  */
10495   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10496     {
10497       if (cxx_dialect < cxx14)
10498         pedwarn (parser->lexer->next_token->location, 0,
10499                  "lambda templates are only available with "
10500                  "-std=c++14 or -std=gnu++14");
10501       else if (cxx_dialect < cxx2a)
10502         pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10503                  "lambda templates are only available with "
10504                  "-std=c++2a or -std=gnu++2a");
10505
10506       cp_lexer_consume_token (parser->lexer);
10507
10508       template_param_list = cp_parser_template_parameter_list (parser);
10509
10510       cp_parser_skip_to_end_of_template_parameter_list (parser);
10511
10512       /* We just processed one more parameter list.  */
10513       ++parser->num_template_parameter_lists;
10514     }
10515
10516   /* The parameter-declaration-clause is optional (unless
10517      template-parameter-list was given), but must begin with an
10518      opening parenthesis if present.  */
10519   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10520     {
10521       matching_parens parens;
10522       parens.consume_open (parser);
10523
10524       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10525
10526       /* Parse parameters.  */
10527       param_list = cp_parser_parameter_declaration_clause (parser);
10528
10529       /* Default arguments shall not be specified in the
10530          parameter-declaration-clause of a lambda-declarator.  */
10531       if (cxx_dialect < cxx14)
10532         for (tree t = param_list; t; t = TREE_CHAIN (t))
10533           if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10534             pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10535                      "default argument specified for lambda parameter");
10536
10537       parens.require_close (parser);
10538
10539       attributes = cp_parser_attributes_opt (parser);
10540
10541       /* In the decl-specifier-seq of the lambda-declarator, each
10542          decl-specifier shall either be mutable or constexpr.  */
10543       int declares_class_or_enum;
10544       if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10545         cp_parser_decl_specifier_seq (parser,
10546                                       CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10547                                       &lambda_specs, &declares_class_or_enum);
10548       if (lambda_specs.storage_class == sc_mutable)
10549         {
10550           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10551           if (lambda_specs.conflicting_specifiers_p)
10552             error_at (lambda_specs.locations[ds_storage_class],
10553                       "duplicate %<mutable%>");
10554         }
10555
10556       tx_qual = cp_parser_tx_qualifier_opt (parser);
10557
10558       /* Parse optional exception specification.  */
10559       exception_spec = cp_parser_exception_specification_opt (parser);
10560
10561       /* Parse optional trailing return type.  */
10562       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10563         {
10564           cp_lexer_consume_token (parser->lexer);
10565           return_type = cp_parser_trailing_type_id (parser);
10566         }
10567
10568       /* The function parameters must be in scope all the way until after the
10569          trailing-return-type in case of decltype.  */
10570       pop_bindings_and_leave_scope ();
10571     }
10572   else if (template_param_list != NULL_TREE) // generate diagnostic
10573     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10574
10575   /* Create the function call operator.
10576
10577      Messing with declarators like this is no uglier than building up the
10578      FUNCTION_DECL by hand, and this is less likely to get out of sync with
10579      other code.  */
10580   {
10581     cp_decl_specifier_seq return_type_specs;
10582     cp_declarator* declarator;
10583     tree fco;
10584     int quals;
10585     void *p;
10586
10587     clear_decl_specs (&return_type_specs);
10588     if (return_type)
10589       return_type_specs.type = return_type;
10590     else
10591       /* Maybe we will deduce the return type later.  */
10592       return_type_specs.type = make_auto ();
10593
10594     if (lambda_specs.locations[ds_constexpr])
10595       {
10596         if (cxx_dialect >= cxx17)
10597           return_type_specs.locations[ds_constexpr]
10598             = lambda_specs.locations[ds_constexpr];
10599         else
10600           error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10601                     "lambda only available with -std=c++17 or -std=gnu++17");
10602       }
10603
10604     p = obstack_alloc (&declarator_obstack, 0);
10605
10606     declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10607
10608     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10609              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10610     declarator = make_call_declarator (declarator, param_list, quals,
10611                                        VIRT_SPEC_UNSPECIFIED,
10612                                        REF_QUAL_NONE,
10613                                        tx_qual,
10614                                        exception_spec,
10615                                        /*late_return_type=*/NULL_TREE,
10616                                        /*requires_clause*/NULL_TREE);
10617     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10618
10619     fco = grokmethod (&return_type_specs,
10620                       declarator,
10621                       attributes);
10622     if (fco != error_mark_node)
10623       {
10624         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10625         DECL_ARTIFICIAL (fco) = 1;
10626         /* Give the object parameter a different name.  */
10627         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10628         DECL_LAMBDA_FUNCTION (fco) = 1;
10629         if (return_type)
10630           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10631       }
10632     if (template_param_list)
10633       {
10634         fco = finish_member_template_decl (fco);
10635         finish_template_decl (template_param_list);
10636         --parser->num_template_parameter_lists;
10637       }
10638     else if (parser->fully_implicit_function_template_p)
10639       fco = finish_fully_implicit_template (parser, fco);
10640
10641     finish_member_declaration (fco);
10642
10643     obstack_free (&declarator_obstack, p);
10644
10645     return (fco != error_mark_node);
10646   }
10647 }
10648
10649 /* Parse the body of a lambda expression, which is simply
10650
10651    compound-statement
10652
10653    but which requires special handling.
10654    LAMBDA_EXPR is the current representation of the lambda expression.  */
10655
10656 static void
10657 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10658 {
10659   bool nested = (current_function_decl != NULL_TREE);
10660   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10661   bool in_function_body = parser->in_function_body;
10662
10663   if (nested)
10664     push_function_context ();
10665   else
10666     /* Still increment function_depth so that we don't GC in the
10667        middle of an expression.  */
10668     ++function_depth;
10669
10670   vec<tree> omp_privatization_save;
10671   save_omp_privatization_clauses (omp_privatization_save);
10672   /* Clear this in case we're in the middle of a default argument.  */
10673   parser->local_variables_forbidden_p = false;
10674   parser->in_function_body = true;
10675
10676   {
10677     local_specialization_stack s (lss_copy);
10678     tree fco = lambda_function (lambda_expr);
10679     tree body = start_lambda_function (fco, lambda_expr);
10680     matching_braces braces;
10681
10682     if (braces.require_open (parser))
10683       {
10684         tree compound_stmt = begin_compound_stmt (0);
10685
10686         /* Originally C++11 required us to peek for 'return expr'; and
10687            process it specially here to deduce the return type.  N3638
10688            removed the need for that.  */
10689
10690         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10691           cp_parser_label_declaration (parser);
10692         cp_parser_statement_seq_opt (parser, NULL_TREE);
10693         braces.require_close (parser);
10694
10695         finish_compound_stmt (compound_stmt);
10696       }
10697
10698     finish_lambda_function (body);
10699   }
10700
10701   restore_omp_privatization_clauses (omp_privatization_save);
10702   parser->local_variables_forbidden_p = local_variables_forbidden_p;
10703   parser->in_function_body = in_function_body;
10704   if (nested)
10705     pop_function_context();
10706   else
10707     --function_depth;
10708 }
10709
10710 /* Statements [gram.stmt.stmt]  */
10711
10712 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC.  */
10713
10714 static void
10715 add_debug_begin_stmt (location_t loc)
10716 {
10717   if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10718     return;
10719   if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10720     /* A concept is never expanded normally.  */
10721     return;
10722
10723   tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10724   SET_EXPR_LOCATION (stmt, loc);
10725   add_stmt (stmt);
10726 }
10727
10728 /* Parse a statement.
10729
10730    statement:
10731      labeled-statement
10732      expression-statement
10733      compound-statement
10734      selection-statement
10735      iteration-statement
10736      jump-statement
10737      declaration-statement
10738      try-block
10739
10740   C++11:
10741
10742   statement:
10743     labeled-statement
10744     attribute-specifier-seq (opt) expression-statement
10745     attribute-specifier-seq (opt) compound-statement
10746     attribute-specifier-seq (opt) selection-statement
10747     attribute-specifier-seq (opt) iteration-statement
10748     attribute-specifier-seq (opt) jump-statement
10749     declaration-statement
10750     attribute-specifier-seq (opt) try-block
10751
10752   init-statement:
10753     expression-statement
10754     simple-declaration
10755
10756   TM Extension:
10757
10758    statement:
10759      atomic-statement
10760
10761   IN_COMPOUND is true when the statement is nested inside a
10762   cp_parser_compound_statement; this matters for certain pragmas.
10763
10764   If IF_P is not NULL, *IF_P is set to indicate whether the statement
10765   is a (possibly labeled) if statement which is not enclosed in braces
10766   and has an else clause.  This is used to implement -Wparentheses.
10767
10768   CHAIN is a vector of if-else-if conditions.  */
10769
10770 static void
10771 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10772                      bool in_compound, bool *if_p, vec<tree> *chain,
10773                      location_t *loc_after_labels)
10774 {
10775   tree statement, std_attrs = NULL_TREE;
10776   cp_token *token;
10777   location_t statement_location, attrs_location;
10778
10779  restart:
10780   if (if_p != NULL)
10781     *if_p = false;
10782   /* There is no statement yet.  */
10783   statement = NULL_TREE;
10784
10785   saved_token_sentinel saved_tokens (parser->lexer);
10786   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10787   if (c_dialect_objc ())
10788     /* In obj-c++, seeing '[[' might be the either the beginning of
10789        c++11 attributes, or a nested objc-message-expression.  So
10790        let's parse the c++11 attributes tentatively.  */
10791     cp_parser_parse_tentatively (parser);
10792   std_attrs = cp_parser_std_attribute_spec_seq (parser);
10793   if (c_dialect_objc ())
10794     {
10795       if (!cp_parser_parse_definitely (parser))
10796         std_attrs = NULL_TREE;
10797     }
10798
10799   /* Peek at the next token.  */
10800   token = cp_lexer_peek_token (parser->lexer);
10801   /* Remember the location of the first token in the statement.  */
10802   statement_location = token->location;
10803   add_debug_begin_stmt (statement_location);
10804   /* If this is a keyword, then that will often determine what kind of
10805      statement we have.  */
10806   if (token->type == CPP_KEYWORD)
10807     {
10808       enum rid keyword = token->keyword;
10809
10810       switch (keyword)
10811         {
10812         case RID_CASE:
10813         case RID_DEFAULT:
10814           /* Looks like a labeled-statement with a case label.
10815              Parse the label, and then use tail recursion to parse
10816              the statement.  */
10817           cp_parser_label_for_labeled_statement (parser, std_attrs);
10818           in_compound = false;
10819           goto restart;
10820
10821         case RID_IF:
10822         case RID_SWITCH:
10823           statement = cp_parser_selection_statement (parser, if_p, chain);
10824           break;
10825
10826         case RID_WHILE:
10827         case RID_DO:
10828         case RID_FOR:
10829           statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10830           break;
10831
10832         case RID_BREAK:
10833         case RID_CONTINUE:
10834         case RID_RETURN:
10835         case RID_GOTO:
10836           statement = cp_parser_jump_statement (parser);
10837           break;
10838
10839           /* Objective-C++ exception-handling constructs.  */
10840         case RID_AT_TRY:
10841         case RID_AT_CATCH:
10842         case RID_AT_FINALLY:
10843         case RID_AT_SYNCHRONIZED:
10844         case RID_AT_THROW:
10845           statement = cp_parser_objc_statement (parser);
10846           break;
10847
10848         case RID_TRY:
10849           statement = cp_parser_try_block (parser);
10850           break;
10851
10852         case RID_NAMESPACE:
10853           /* This must be a namespace alias definition.  */
10854           cp_parser_declaration_statement (parser);
10855           return;
10856           
10857         case RID_TRANSACTION_ATOMIC:
10858         case RID_TRANSACTION_RELAXED:
10859         case RID_SYNCHRONIZED:
10860         case RID_ATOMIC_NOEXCEPT:
10861         case RID_ATOMIC_CANCEL:
10862           statement = cp_parser_transaction (parser, token);
10863           break;
10864         case RID_TRANSACTION_CANCEL:
10865           statement = cp_parser_transaction_cancel (parser);
10866           break;
10867
10868         default:
10869           /* It might be a keyword like `int' that can start a
10870              declaration-statement.  */
10871           break;
10872         }
10873     }
10874   else if (token->type == CPP_NAME)
10875     {
10876       /* If the next token is a `:', then we are looking at a
10877          labeled-statement.  */
10878       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10879       if (token->type == CPP_COLON)
10880         {
10881           /* Looks like a labeled-statement with an ordinary label.
10882              Parse the label, and then use tail recursion to parse
10883              the statement.  */
10884
10885           cp_parser_label_for_labeled_statement (parser, std_attrs);
10886           in_compound = false;
10887           goto restart;
10888         }
10889     }
10890   /* Anything that starts with a `{' must be a compound-statement.  */
10891   else if (token->type == CPP_OPEN_BRACE)
10892     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10893   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10894      a statement all its own.  */
10895   else if (token->type == CPP_PRAGMA)
10896     {
10897       /* Only certain OpenMP pragmas are attached to statements, and thus
10898          are considered statements themselves.  All others are not.  In
10899          the context of a compound, accept the pragma as a "statement" and
10900          return so that we can check for a close brace.  Otherwise we
10901          require a real statement and must go back and read one.  */
10902       if (in_compound)
10903         cp_parser_pragma (parser, pragma_compound, if_p);
10904       else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10905         goto restart;
10906       return;
10907     }
10908   else if (token->type == CPP_EOF)
10909     {
10910       cp_parser_error (parser, "expected statement");
10911       return;
10912     }
10913
10914   /* Everything else must be a declaration-statement or an
10915      expression-statement.  Try for the declaration-statement
10916      first, unless we are looking at a `;', in which case we know that
10917      we have an expression-statement.  */
10918   if (!statement)
10919     {
10920       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10921         {
10922           if (std_attrs != NULL_TREE)
10923             {
10924               /*  Attributes should be parsed as part of the the
10925                   declaration, so let's un-parse them.  */
10926               saved_tokens.rollback();
10927               std_attrs = NULL_TREE;
10928             }
10929
10930           cp_parser_parse_tentatively (parser);
10931           /* Try to parse the declaration-statement.  */
10932           cp_parser_declaration_statement (parser);
10933           /* If that worked, we're done.  */
10934           if (cp_parser_parse_definitely (parser))
10935             return;
10936         }
10937       /* All preceding labels have been parsed at this point.  */
10938       if (loc_after_labels != NULL)
10939         *loc_after_labels = statement_location;
10940
10941       /* Look for an expression-statement instead.  */
10942       statement = cp_parser_expression_statement (parser, in_statement_expr);
10943
10944       /* Handle [[fallthrough]];.  */
10945       if (attribute_fallthrough_p (std_attrs))
10946         {
10947           /* The next token after the fallthrough attribute is ';'.  */
10948           if (statement == NULL_TREE)
10949             {
10950               /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
10951               statement = build_call_expr_internal_loc (statement_location,
10952                                                         IFN_FALLTHROUGH,
10953                                                         void_type_node, 0);
10954               finish_expr_stmt (statement);
10955             }
10956           else
10957             warning_at (statement_location, OPT_Wattributes,
10958                         "%<fallthrough%> attribute not followed by %<;%>");
10959           std_attrs = NULL_TREE;
10960         }
10961     }
10962
10963   /* Set the line number for the statement.  */
10964   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10965     SET_EXPR_LOCATION (statement, statement_location);
10966
10967   /* Allow "[[fallthrough]];", but warn otherwise.  */
10968   if (std_attrs != NULL_TREE)
10969     warning_at (attrs_location,
10970                 OPT_Wattributes,
10971                 "attributes at the beginning of statement are ignored");
10972 }
10973
10974 /* Append ATTR to attribute list ATTRS.  */
10975
10976 static tree
10977 attr_chainon (tree attrs, tree attr)
10978 {
10979   if (attrs == error_mark_node)
10980     return error_mark_node;
10981   if (attr == error_mark_node)
10982     return error_mark_node;
10983   return chainon (attrs, attr);
10984 }
10985
10986 /* Parse the label for a labeled-statement, i.e.
10987
10988    identifier :
10989    case constant-expression :
10990    default :
10991
10992    GNU Extension:
10993    case constant-expression ... constant-expression : statement
10994
10995    When a label is parsed without errors, the label is added to the
10996    parse tree by the finish_* functions, so this function doesn't
10997    have to return the label.  */
10998
10999 static void
11000 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11001 {
11002   cp_token *token;
11003   tree label = NULL_TREE;
11004   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11005
11006   /* The next token should be an identifier.  */
11007   token = cp_lexer_peek_token (parser->lexer);
11008   if (token->type != CPP_NAME
11009       && token->type != CPP_KEYWORD)
11010     {
11011       cp_parser_error (parser, "expected labeled-statement");
11012       return;
11013     }
11014
11015   /* Remember whether this case or a user-defined label is allowed to fall
11016      through to.  */
11017   bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11018
11019   parser->colon_corrects_to_scope_p = false;
11020   switch (token->keyword)
11021     {
11022     case RID_CASE:
11023       {
11024         tree expr, expr_hi;
11025         cp_token *ellipsis;
11026
11027         /* Consume the `case' token.  */
11028         cp_lexer_consume_token (parser->lexer);
11029         /* Parse the constant-expression.  */
11030         expr = cp_parser_constant_expression (parser);
11031         if (check_for_bare_parameter_packs (expr))
11032           expr = error_mark_node;
11033
11034         ellipsis = cp_lexer_peek_token (parser->lexer);
11035         if (ellipsis->type == CPP_ELLIPSIS)
11036           {
11037             /* Consume the `...' token.  */
11038             cp_lexer_consume_token (parser->lexer);
11039             expr_hi = cp_parser_constant_expression (parser);
11040             if (check_for_bare_parameter_packs (expr_hi))
11041               expr_hi = error_mark_node;
11042
11043             /* We don't need to emit warnings here, as the common code
11044                will do this for us.  */
11045           }
11046         else
11047           expr_hi = NULL_TREE;
11048
11049         if (parser->in_switch_statement_p)
11050           {
11051             tree l = finish_case_label (token->location, expr, expr_hi);
11052             if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11053               FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11054           }
11055         else
11056           error_at (token->location,
11057                     "case label %qE not within a switch statement",
11058                     expr);
11059       }
11060       break;
11061
11062     case RID_DEFAULT:
11063       /* Consume the `default' token.  */
11064       cp_lexer_consume_token (parser->lexer);
11065
11066       if (parser->in_switch_statement_p)
11067         {
11068           tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11069           if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11070             FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11071         }
11072       else
11073         error_at (token->location, "case label not within a switch statement");
11074       break;
11075
11076     default:
11077       /* Anything else must be an ordinary label.  */
11078       label = finish_label_stmt (cp_parser_identifier (parser));
11079       if (label && TREE_CODE (label) == LABEL_DECL)
11080         FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11081       break;
11082     }
11083
11084   /* Require the `:' token.  */
11085   cp_parser_require (parser, CPP_COLON, RT_COLON);
11086
11087   /* An ordinary label may optionally be followed by attributes.
11088      However, this is only permitted if the attributes are then
11089      followed by a semicolon.  This is because, for backward
11090      compatibility, when parsing
11091        lab: __attribute__ ((unused)) int i;
11092      we want the attribute to attach to "i", not "lab".  */
11093   if (label != NULL_TREE
11094       && cp_next_tokens_can_be_gnu_attribute_p (parser))
11095     {
11096       tree attrs;
11097       cp_parser_parse_tentatively (parser);
11098       attrs = cp_parser_gnu_attributes_opt (parser);
11099       if (attrs == NULL_TREE
11100           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11101         cp_parser_abort_tentative_parse (parser);
11102       else if (!cp_parser_parse_definitely (parser))
11103         ;
11104       else
11105         attributes = attr_chainon (attributes, attrs);
11106     }
11107
11108   if (attributes != NULL_TREE)
11109     cplus_decl_attributes (&label, attributes, 0);
11110
11111   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11112 }
11113
11114 /* Parse an expression-statement.
11115
11116    expression-statement:
11117      expression [opt] ;
11118
11119    Returns the new EXPR_STMT -- or NULL_TREE if the expression
11120    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11121    indicates whether this expression-statement is part of an
11122    expression statement.  */
11123
11124 static tree
11125 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11126 {
11127   tree statement = NULL_TREE;
11128   cp_token *token = cp_lexer_peek_token (parser->lexer);
11129   location_t loc = token->location;
11130
11131   /* There might be attribute fallthrough.  */
11132   tree attr = cp_parser_gnu_attributes_opt (parser);
11133
11134   /* If the next token is a ';', then there is no expression
11135      statement.  */
11136   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11137     {
11138       statement = cp_parser_expression (parser);
11139       if (statement == error_mark_node
11140           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11141         {
11142           cp_parser_skip_to_end_of_block_or_statement (parser);
11143           return error_mark_node;
11144         }
11145     }
11146
11147   /* Handle [[fallthrough]];.  */
11148   if (attribute_fallthrough_p (attr))
11149     {
11150       /* The next token after the fallthrough attribute is ';'.  */
11151       if (statement == NULL_TREE)
11152         /* Turn [[fallthrough]]; into FALLTHROUGH ();.  */
11153         statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11154                                                   void_type_node, 0);
11155       else
11156         warning_at (loc, OPT_Wattributes,
11157                     "%<fallthrough%> attribute not followed by %<;%>");
11158       attr = NULL_TREE;
11159     }
11160
11161   /* Allow "[[fallthrough]];", but warn otherwise.  */
11162   if (attr != NULL_TREE)
11163     warning_at (loc, OPT_Wattributes,
11164                 "attributes at the beginning of statement are ignored");
11165
11166   /* Give a helpful message for "A<T>::type t;" and the like.  */
11167   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11168       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11169     {
11170       if (TREE_CODE (statement) == SCOPE_REF)
11171         error_at (token->location, "need %<typename%> before %qE because "
11172                   "%qT is a dependent scope",
11173                   statement, TREE_OPERAND (statement, 0));
11174       else if (is_overloaded_fn (statement)
11175                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11176         {
11177           /* A::A a; */
11178           tree fn = get_first_fn (statement);
11179           error_at (token->location,
11180                     "%<%T::%D%> names the constructor, not the type",
11181                     DECL_CONTEXT (fn), DECL_NAME (fn));
11182         }
11183     }
11184
11185   /* Consume the final `;'.  */
11186   cp_parser_consume_semicolon_at_end_of_statement (parser);
11187
11188   if (in_statement_expr
11189       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11190     /* This is the final expression statement of a statement
11191        expression.  */
11192     statement = finish_stmt_expr_expr (statement, in_statement_expr);
11193   else if (statement)
11194     statement = finish_expr_stmt (statement);
11195
11196   return statement;
11197 }
11198
11199 /* Parse a compound-statement.
11200
11201    compound-statement:
11202      { statement-seq [opt] }
11203
11204    GNU extension:
11205
11206    compound-statement:
11207      { label-declaration-seq [opt] statement-seq [opt] }
11208
11209    label-declaration-seq:
11210      label-declaration
11211      label-declaration-seq label-declaration
11212
11213    Returns a tree representing the statement.  */
11214
11215 static tree
11216 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11217                               int bcs_flags, bool function_body)
11218 {
11219   tree compound_stmt;
11220   matching_braces braces;
11221
11222   /* Consume the `{'.  */
11223   if (!braces.require_open (parser))
11224     return error_mark_node;
11225   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11226       && !function_body && cxx_dialect < cxx14)
11227     pedwarn (input_location, OPT_Wpedantic,
11228              "compound-statement in %<constexpr%> function");
11229   /* Begin the compound-statement.  */
11230   compound_stmt = begin_compound_stmt (bcs_flags);
11231   /* If the next keyword is `__label__' we have a label declaration.  */
11232   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11233     cp_parser_label_declaration (parser);
11234   /* Parse an (optional) statement-seq.  */
11235   cp_parser_statement_seq_opt (parser, in_statement_expr);
11236   /* Finish the compound-statement.  */
11237   finish_compound_stmt (compound_stmt);
11238   /* Consume the `}'.  */
11239   braces.require_close (parser);
11240
11241   return compound_stmt;
11242 }
11243
11244 /* Parse an (optional) statement-seq.
11245
11246    statement-seq:
11247      statement
11248      statement-seq [opt] statement  */
11249
11250 static void
11251 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11252 {
11253   /* Scan statements until there aren't any more.  */
11254   while (true)
11255     {
11256       cp_token *token = cp_lexer_peek_token (parser->lexer);
11257
11258       /* If we are looking at a `}', then we have run out of
11259          statements; the same is true if we have reached the end
11260          of file, or have stumbled upon a stray '@end'.  */
11261       if (token->type == CPP_CLOSE_BRACE
11262           || token->type == CPP_EOF
11263           || token->type == CPP_PRAGMA_EOL
11264           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11265         break;
11266       
11267       /* If we are in a compound statement and find 'else' then
11268          something went wrong.  */
11269       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11270         {
11271           if (parser->in_statement & IN_IF_STMT) 
11272             break;
11273           else
11274             {
11275               token = cp_lexer_consume_token (parser->lexer);
11276               error_at (token->location, "%<else%> without a previous %<if%>");
11277             }
11278         }
11279
11280       /* Parse the statement.  */
11281       cp_parser_statement (parser, in_statement_expr, true, NULL);
11282     }
11283 }
11284
11285 /* Return true if we're looking at (init; cond), false otherwise.  */
11286
11287 static bool
11288 cp_parser_init_statement_p (cp_parser *parser)
11289 {
11290   /* Save tokens so that we can put them back.  */
11291   cp_lexer_save_tokens (parser->lexer);
11292
11293   /* Look for ';' that is not nested in () or {}.  */
11294   int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11295                                                      /*recovering=*/false,
11296                                                      CPP_SEMICOLON,
11297                                                      /*consume_paren=*/false);
11298
11299   /* Roll back the tokens we skipped.  */
11300   cp_lexer_rollback_tokens (parser->lexer);
11301
11302   return ret == -1;
11303 }
11304
11305 /* Parse a selection-statement.
11306
11307    selection-statement:
11308      if ( init-statement [opt] condition ) statement
11309      if ( init-statement [opt] condition ) statement else statement
11310      switch ( init-statement [opt] condition ) statement
11311
11312    Returns the new IF_STMT or SWITCH_STMT.
11313
11314    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11315    is a (possibly labeled) if statement which is not enclosed in
11316    braces and has an else clause.  This is used to implement
11317    -Wparentheses.
11318
11319    CHAIN is a vector of if-else-if conditions.  This is used to implement
11320    -Wduplicated-cond.  */
11321
11322 static tree
11323 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11324                                vec<tree> *chain)
11325 {
11326   cp_token *token;
11327   enum rid keyword;
11328   token_indent_info guard_tinfo;
11329
11330   if (if_p != NULL)
11331     *if_p = false;
11332
11333   /* Peek at the next token.  */
11334   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11335   guard_tinfo = get_token_indent_info (token);
11336
11337   /* See what kind of keyword it is.  */
11338   keyword = token->keyword;
11339   switch (keyword)
11340     {
11341     case RID_IF:
11342     case RID_SWITCH:
11343       {
11344         tree statement;
11345         tree condition;
11346
11347         bool cx = false;
11348         if (keyword == RID_IF
11349             && cp_lexer_next_token_is_keyword (parser->lexer,
11350                                                RID_CONSTEXPR))
11351           {
11352             cx = true;
11353             cp_token *tok = cp_lexer_consume_token (parser->lexer);
11354             if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11355               pedwarn (tok->location, 0, "%<if constexpr%> only available "
11356                        "with -std=c++17 or -std=gnu++17");
11357           }
11358
11359         /* Look for the `('.  */
11360         matching_parens parens;
11361         if (!parens.require_open (parser))
11362           {
11363             cp_parser_skip_to_end_of_statement (parser);
11364             return error_mark_node;
11365           }
11366
11367         /* Begin the selection-statement.  */
11368         if (keyword == RID_IF)
11369           {
11370             statement = begin_if_stmt ();
11371             IF_STMT_CONSTEXPR_P (statement) = cx;
11372           }
11373         else
11374           statement = begin_switch_stmt ();
11375
11376         /* Parse the optional init-statement.  */
11377         if (cp_parser_init_statement_p (parser))
11378           {
11379             tree decl;
11380             if (cxx_dialect < cxx17)
11381               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11382                        "init-statement in selection statements only available "
11383                        "with -std=c++17 or -std=gnu++17");
11384             cp_parser_init_statement (parser, &decl);
11385           }
11386
11387         /* Parse the condition.  */
11388         condition = cp_parser_condition (parser);
11389         /* Look for the `)'.  */
11390         if (!parens.require_close (parser))
11391           cp_parser_skip_to_closing_parenthesis (parser, true, false,
11392                                                  /*consume_paren=*/true);
11393
11394         if (keyword == RID_IF)
11395           {
11396             bool nested_if;
11397             unsigned char in_statement;
11398
11399             /* Add the condition.  */
11400             condition = finish_if_stmt_cond (condition, statement);
11401
11402             if (warn_duplicated_cond)
11403               warn_duplicated_cond_add_or_warn (token->location, condition,
11404                                                 &chain);
11405
11406             /* Parse the then-clause.  */
11407             in_statement = parser->in_statement;
11408             parser->in_statement |= IN_IF_STMT;
11409
11410             /* Outside a template, the non-selected branch of a constexpr
11411                if is a 'discarded statement', i.e. unevaluated.  */
11412             bool was_discarded = in_discarded_stmt;
11413             bool discard_then = (cx && !processing_template_decl
11414                                  && integer_zerop (condition));
11415             if (discard_then)
11416               {
11417                 in_discarded_stmt = true;
11418                 ++c_inhibit_evaluation_warnings;
11419               }
11420
11421             cp_parser_implicitly_scoped_statement (parser, &nested_if,
11422                                                    guard_tinfo);
11423
11424             parser->in_statement = in_statement;
11425
11426             finish_then_clause (statement);
11427
11428             if (discard_then)
11429               {
11430                 THEN_CLAUSE (statement) = NULL_TREE;
11431                 in_discarded_stmt = was_discarded;
11432                 --c_inhibit_evaluation_warnings;
11433               }
11434
11435             /* If the next token is `else', parse the else-clause.  */
11436             if (cp_lexer_next_token_is_keyword (parser->lexer,
11437                                                 RID_ELSE))
11438               {
11439                 bool discard_else = (cx && !processing_template_decl
11440                                      && integer_nonzerop (condition));
11441                 if (discard_else)
11442                   {
11443                     in_discarded_stmt = true;
11444                     ++c_inhibit_evaluation_warnings;
11445                   }
11446
11447                 guard_tinfo
11448                   = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11449                 /* Consume the `else' keyword.  */
11450                 cp_lexer_consume_token (parser->lexer);
11451                 if (warn_duplicated_cond)
11452                   {
11453                     if (cp_lexer_next_token_is_keyword (parser->lexer,
11454                                                         RID_IF)
11455                         && chain == NULL)
11456                       {
11457                         /* We've got "if (COND) else if (COND2)".  Start
11458                            the condition chain and add COND as the first
11459                            element.  */
11460                         chain = new vec<tree> ();
11461                         if (!CONSTANT_CLASS_P (condition)
11462                             && !TREE_SIDE_EFFECTS (condition))
11463                         {
11464                           /* Wrap it in a NOP_EXPR so that we can set the
11465                              location of the condition.  */
11466                           tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11467                                            condition);
11468                           SET_EXPR_LOCATION (e, token->location);
11469                           chain->safe_push (e);
11470                         }
11471                       }
11472                     else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11473                                                               RID_IF))
11474                       {
11475                         /* This is if-else without subsequent if.  Zap the
11476                            condition chain; we would have already warned at
11477                            this point.  */
11478                         delete chain;
11479                         chain = NULL;
11480                       }
11481                   }
11482                 begin_else_clause (statement);
11483                 /* Parse the else-clause.  */
11484                 cp_parser_implicitly_scoped_statement (parser, NULL,
11485                                                        guard_tinfo, chain);
11486
11487                 finish_else_clause (statement);
11488
11489                 /* If we are currently parsing a then-clause, then
11490                    IF_P will not be NULL.  We set it to true to
11491                    indicate that this if statement has an else clause.
11492                    This may trigger the Wparentheses warning below
11493                    when we get back up to the parent if statement.  */
11494                 if (if_p != NULL)
11495                   *if_p = true;
11496
11497                 if (discard_else)
11498                   {
11499                     ELSE_CLAUSE (statement) = NULL_TREE;
11500                     in_discarded_stmt = was_discarded;
11501                     --c_inhibit_evaluation_warnings;
11502                   }
11503               }
11504             else
11505               {
11506                 /* This if statement does not have an else clause.  If
11507                    NESTED_IF is true, then the then-clause has an if
11508                    statement which does have an else clause.  We warn
11509                    about the potential ambiguity.  */
11510                 if (nested_if)
11511                   warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11512                               "suggest explicit braces to avoid ambiguous"
11513                               " %<else%>");
11514                 if (warn_duplicated_cond)
11515                   {
11516                     /* We don't need the condition chain anymore.  */
11517                     delete chain;
11518                     chain = NULL;
11519                   }
11520               }
11521
11522             /* Now we're all done with the if-statement.  */
11523             finish_if_stmt (statement);
11524           }
11525         else
11526           {
11527             bool in_switch_statement_p;
11528             unsigned char in_statement;
11529
11530             /* Add the condition.  */
11531             finish_switch_cond (condition, statement);
11532
11533             /* Parse the body of the switch-statement.  */
11534             in_switch_statement_p = parser->in_switch_statement_p;
11535             in_statement = parser->in_statement;
11536             parser->in_switch_statement_p = true;
11537             parser->in_statement |= IN_SWITCH_STMT;
11538             cp_parser_implicitly_scoped_statement (parser, if_p,
11539                                                    guard_tinfo);
11540             parser->in_switch_statement_p = in_switch_statement_p;
11541             parser->in_statement = in_statement;
11542
11543             /* Now we're all done with the switch-statement.  */
11544             finish_switch_stmt (statement);
11545           }
11546
11547         return statement;
11548       }
11549       break;
11550
11551     default:
11552       cp_parser_error (parser, "expected selection-statement");
11553       return error_mark_node;
11554     }
11555 }
11556
11557 /* Parse a condition.
11558
11559    condition:
11560      expression
11561      type-specifier-seq declarator = initializer-clause
11562      type-specifier-seq declarator braced-init-list
11563
11564    GNU Extension:
11565
11566    condition:
11567      type-specifier-seq declarator asm-specification [opt]
11568        attributes [opt] = assignment-expression
11569
11570    Returns the expression that should be tested.  */
11571
11572 static tree
11573 cp_parser_condition (cp_parser* parser)
11574 {
11575   cp_decl_specifier_seq type_specifiers;
11576   const char *saved_message;
11577   int declares_class_or_enum;
11578
11579   /* Try the declaration first.  */
11580   cp_parser_parse_tentatively (parser);
11581   /* New types are not allowed in the type-specifier-seq for a
11582      condition.  */
11583   saved_message = parser->type_definition_forbidden_message;
11584   parser->type_definition_forbidden_message
11585     = G_("types may not be defined in conditions");
11586   /* Parse the type-specifier-seq.  */
11587   cp_parser_decl_specifier_seq (parser,
11588                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11589                                 &type_specifiers,
11590                                 &declares_class_or_enum);
11591   /* Restore the saved message.  */
11592   parser->type_definition_forbidden_message = saved_message;
11593   /* If all is well, we might be looking at a declaration.  */
11594   if (!cp_parser_error_occurred (parser))
11595     {
11596       tree decl;
11597       tree asm_specification;
11598       tree attributes;
11599       cp_declarator *declarator;
11600       tree initializer = NULL_TREE;
11601
11602       /* Parse the declarator.  */
11603       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11604                                          /*ctor_dtor_or_conv_p=*/NULL,
11605                                          /*parenthesized_p=*/NULL,
11606                                          /*member_p=*/false,
11607                                          /*friend_p=*/false);
11608       /* Parse the attributes.  */
11609       attributes = cp_parser_attributes_opt (parser);
11610       /* Parse the asm-specification.  */
11611       asm_specification = cp_parser_asm_specification_opt (parser);
11612       /* If the next token is not an `=' or '{', then we might still be
11613          looking at an expression.  For example:
11614
11615            if (A(a).x)
11616
11617          looks like a decl-specifier-seq and a declarator -- but then
11618          there is no `=', so this is an expression.  */
11619       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11620           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11621         cp_parser_simulate_error (parser);
11622         
11623       /* If we did see an `=' or '{', then we are looking at a declaration
11624          for sure.  */
11625       if (cp_parser_parse_definitely (parser))
11626         {
11627           tree pushed_scope;
11628           bool non_constant_p;
11629           int flags = LOOKUP_ONLYCONVERTING;
11630
11631           /* Create the declaration.  */
11632           decl = start_decl (declarator, &type_specifiers,
11633                              /*initialized_p=*/true,
11634                              attributes, /*prefix_attributes=*/NULL_TREE,
11635                              &pushed_scope);
11636
11637           /* Parse the initializer.  */
11638           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11639             {
11640               initializer = cp_parser_braced_list (parser, &non_constant_p);
11641               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11642               flags = 0;
11643             }
11644           else
11645             {
11646               /* Consume the `='.  */
11647               cp_parser_require (parser, CPP_EQ, RT_EQ);
11648               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11649             }
11650           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11651             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11652
11653           /* Process the initializer.  */
11654           cp_finish_decl (decl,
11655                           initializer, !non_constant_p,
11656                           asm_specification,
11657                           flags);
11658
11659           if (pushed_scope)
11660             pop_scope (pushed_scope);
11661
11662           return convert_from_reference (decl);
11663         }
11664     }
11665   /* If we didn't even get past the declarator successfully, we are
11666      definitely not looking at a declaration.  */
11667   else
11668     cp_parser_abort_tentative_parse (parser);
11669
11670   /* Otherwise, we are looking at an expression.  */
11671   return cp_parser_expression (parser);
11672 }
11673
11674 /* Parses a for-statement or range-for-statement until the closing ')',
11675    not included. */
11676
11677 static tree
11678 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11679 {
11680   tree init, scope, decl;
11681   bool is_range_for;
11682
11683   /* Begin the for-statement.  */
11684   scope = begin_for_scope (&init);
11685
11686   /* Parse the initialization.  */
11687   is_range_for = cp_parser_init_statement (parser, &decl);
11688
11689   if (is_range_for)
11690     return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11691   else
11692     return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11693 }
11694
11695 static tree
11696 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11697                  unsigned short unroll)
11698 {
11699   /* Normal for loop */
11700   tree condition = NULL_TREE;
11701   tree expression = NULL_TREE;
11702   tree stmt;
11703
11704   stmt = begin_for_stmt (scope, init);
11705   /* The init-statement has already been parsed in
11706      cp_parser_init_statement, so no work is needed here.  */
11707   finish_init_stmt (stmt);
11708
11709   /* If there's a condition, process it.  */
11710   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11711     condition = cp_parser_condition (parser);
11712   else if (ivdep)
11713     {
11714       cp_parser_error (parser, "missing loop condition in loop with "
11715                        "%<GCC ivdep%> pragma");
11716       condition = error_mark_node;
11717     }
11718   else if (unroll)
11719     {
11720       cp_parser_error (parser, "missing loop condition in loop with "
11721                        "%<GCC unroll%> pragma");
11722       condition = error_mark_node;
11723     }
11724   finish_for_cond (condition, stmt, ivdep, unroll);
11725   /* Look for the `;'.  */
11726   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11727
11728   /* If there's an expression, process it.  */
11729   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11730     expression = cp_parser_expression (parser);
11731   finish_for_expr (expression, stmt);
11732
11733   return stmt;
11734 }
11735
11736 /* Tries to parse a range-based for-statement:
11737
11738   range-based-for:
11739     decl-specifier-seq declarator : expression
11740
11741   The decl-specifier-seq declarator and the `:' are already parsed by
11742   cp_parser_init_statement.  If processing_template_decl it returns a
11743   newly created RANGE_FOR_STMT; if not, it is converted to a
11744   regular FOR_STMT.  */
11745
11746 static tree
11747 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11748                      bool ivdep, unsigned short unroll)
11749 {
11750   tree stmt, range_expr;
11751   auto_vec <cxx_binding *, 16> bindings;
11752   auto_vec <tree, 16> names;
11753   tree decomp_first_name = NULL_TREE;
11754   unsigned int decomp_cnt = 0;
11755
11756   /* Get the range declaration momentarily out of the way so that
11757      the range expression doesn't clash with it. */
11758   if (range_decl != error_mark_node)
11759     {
11760       if (DECL_HAS_VALUE_EXPR_P (range_decl))
11761         {
11762           tree v = DECL_VALUE_EXPR (range_decl);
11763           /* For decomposition declaration get all of the corresponding
11764              declarations out of the way.  */
11765           if (TREE_CODE (v) == ARRAY_REF
11766               && VAR_P (TREE_OPERAND (v, 0))
11767               && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11768             {
11769               tree d = range_decl;
11770               range_decl = TREE_OPERAND (v, 0);
11771               decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11772               decomp_first_name = d;
11773               for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11774                 {
11775                   tree name = DECL_NAME (d);
11776                   names.safe_push (name);
11777                   bindings.safe_push (IDENTIFIER_BINDING (name));
11778                   IDENTIFIER_BINDING (name)
11779                     = IDENTIFIER_BINDING (name)->previous;
11780                 }
11781             }
11782         }
11783       if (names.is_empty ())
11784         {
11785           tree name = DECL_NAME (range_decl);
11786           names.safe_push (name);
11787           bindings.safe_push (IDENTIFIER_BINDING (name));
11788           IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11789         }
11790     }
11791
11792   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11793     {
11794       bool expr_non_constant_p;
11795       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11796     }
11797   else
11798     range_expr = cp_parser_expression (parser);
11799
11800   /* Put the range declaration(s) back into scope. */
11801   for (unsigned int i = 0; i < names.length (); i++)
11802     {
11803       cxx_binding *binding = bindings[i];
11804       binding->previous = IDENTIFIER_BINDING (names[i]);
11805       IDENTIFIER_BINDING (names[i]) = binding;
11806     }
11807
11808   /* If in template, STMT is converted to a normal for-statement
11809      at instantiation. If not, it is done just ahead. */
11810   if (processing_template_decl)
11811     {
11812       if (check_for_bare_parameter_packs (range_expr))
11813         range_expr = error_mark_node;
11814       stmt = begin_range_for_stmt (scope, init);
11815       if (ivdep)
11816         RANGE_FOR_IVDEP (stmt) = 1;
11817       if (unroll)
11818         RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11819       finish_range_for_decl (stmt, range_decl, range_expr);
11820       if (!type_dependent_expression_p (range_expr)
11821           /* do_auto_deduction doesn't mess with template init-lists.  */
11822           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11823         do_range_for_auto_deduction (range_decl, range_expr);
11824     }
11825   else
11826     {
11827       stmt = begin_for_stmt (scope, init);
11828       stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11829                                    decomp_first_name, decomp_cnt, ivdep,
11830                                    unroll);
11831     }
11832   return stmt;
11833 }
11834
11835 /* Subroutine of cp_convert_range_for: given the initializer expression,
11836    builds up the range temporary.  */
11837
11838 static tree
11839 build_range_temp (tree range_expr)
11840 {
11841   tree range_type, range_temp;
11842
11843   /* Find out the type deduced by the declaration
11844      `auto &&__range = range_expr'.  */
11845   range_type = cp_build_reference_type (make_auto (), true);
11846   range_type = do_auto_deduction (range_type, range_expr,
11847                                   type_uses_auto (range_type));
11848
11849   /* Create the __range variable.  */
11850   range_temp = build_decl (input_location, VAR_DECL,
11851                            get_identifier ("__for_range"), range_type);
11852   TREE_USED (range_temp) = 1;
11853   DECL_ARTIFICIAL (range_temp) = 1;
11854
11855   return range_temp;
11856 }
11857
11858 /* Used by cp_parser_range_for in template context: we aren't going to
11859    do a full conversion yet, but we still need to resolve auto in the
11860    type of the for-range-declaration if present.  This is basically
11861    a shortcut version of cp_convert_range_for.  */
11862
11863 static void
11864 do_range_for_auto_deduction (tree decl, tree range_expr)
11865 {
11866   tree auto_node = type_uses_auto (TREE_TYPE (decl));
11867   if (auto_node)
11868     {
11869       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11870       range_temp = convert_from_reference (build_range_temp (range_expr));
11871       iter_type = (cp_parser_perform_range_for_lookup
11872                    (range_temp, &begin_dummy, &end_dummy));
11873       if (iter_type)
11874         {
11875           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11876                                   iter_type);
11877           iter_decl = build_x_indirect_ref (input_location, iter_decl,
11878                                             RO_UNARY_STAR,
11879                                             tf_warning_or_error);
11880           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11881                                                 iter_decl, auto_node);
11882         }
11883     }
11884 }
11885
11886 /* Converts a range-based for-statement into a normal
11887    for-statement, as per the definition.
11888
11889       for (RANGE_DECL : RANGE_EXPR)
11890         BLOCK
11891
11892    should be equivalent to:
11893
11894       {
11895         auto &&__range = RANGE_EXPR;
11896         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11897               __begin != __end;
11898               ++__begin)
11899           {
11900               RANGE_DECL = *__begin;
11901               BLOCK
11902           }
11903       }
11904
11905    If RANGE_EXPR is an array:
11906         BEGIN_EXPR = __range
11907         END_EXPR = __range + ARRAY_SIZE(__range)
11908    Else if RANGE_EXPR has a member 'begin' or 'end':
11909         BEGIN_EXPR = __range.begin()
11910         END_EXPR = __range.end()
11911    Else:
11912         BEGIN_EXPR = begin(__range)
11913         END_EXPR = end(__range);
11914
11915    If __range has a member 'begin' but not 'end', or vice versa, we must
11916    still use the second alternative (it will surely fail, however).
11917    When calling begin()/end() in the third alternative we must use
11918    argument dependent lookup, but always considering 'std' as an associated
11919    namespace.  */
11920
11921 tree
11922 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11923                       tree decomp_first_name, unsigned int decomp_cnt,
11924                       bool ivdep, unsigned short unroll)
11925 {
11926   tree begin, end;
11927   tree iter_type, begin_expr, end_expr;
11928   tree condition, expression;
11929
11930   range_expr = mark_lvalue_use (range_expr);
11931
11932   if (range_decl == error_mark_node || range_expr == error_mark_node)
11933     /* If an error happened previously do nothing or else a lot of
11934        unhelpful errors would be issued.  */
11935     begin_expr = end_expr = iter_type = error_mark_node;
11936   else
11937     {
11938       tree range_temp;
11939
11940       if (VAR_P (range_expr)
11941           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11942         /* Can't bind a reference to an array of runtime bound.  */
11943         range_temp = range_expr;
11944       else
11945         {
11946           range_temp = build_range_temp (range_expr);
11947           pushdecl (range_temp);
11948           cp_finish_decl (range_temp, range_expr,
11949                           /*is_constant_init*/false, NULL_TREE,
11950                           LOOKUP_ONLYCONVERTING);
11951           range_temp = convert_from_reference (range_temp);
11952         }
11953       iter_type = cp_parser_perform_range_for_lookup (range_temp,
11954                                                       &begin_expr, &end_expr);
11955     }
11956
11957   /* The new for initialization statement.  */
11958   begin = build_decl (input_location, VAR_DECL,
11959                       get_identifier ("__for_begin"), iter_type);
11960   TREE_USED (begin) = 1;
11961   DECL_ARTIFICIAL (begin) = 1;
11962   pushdecl (begin);
11963   cp_finish_decl (begin, begin_expr,
11964                   /*is_constant_init*/false, NULL_TREE,
11965                   LOOKUP_ONLYCONVERTING);
11966
11967   if (cxx_dialect >= cxx17)
11968     iter_type = cv_unqualified (TREE_TYPE (end_expr));
11969   end = build_decl (input_location, VAR_DECL,
11970                     get_identifier ("__for_end"), iter_type);
11971   TREE_USED (end) = 1;
11972   DECL_ARTIFICIAL (end) = 1;
11973   pushdecl (end);
11974   cp_finish_decl (end, end_expr,
11975                   /*is_constant_init*/false, NULL_TREE,
11976                   LOOKUP_ONLYCONVERTING);
11977
11978   finish_init_stmt (statement);
11979
11980   /* The new for condition.  */
11981   condition = build_x_binary_op (input_location, NE_EXPR,
11982                                  begin, ERROR_MARK,
11983                                  end, ERROR_MARK,
11984                                  NULL, tf_warning_or_error);
11985   finish_for_cond (condition, statement, ivdep, unroll);
11986
11987   /* The new increment expression.  */
11988   expression = finish_unary_op_expr (input_location,
11989                                      PREINCREMENT_EXPR, begin,
11990                                      tf_warning_or_error);
11991   finish_for_expr (expression, statement);
11992
11993   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11994     cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11995
11996   /* The declaration is initialized with *__begin inside the loop body.  */
11997   cp_finish_decl (range_decl,
11998                   build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11999                                         tf_warning_or_error),
12000                   /*is_constant_init*/false, NULL_TREE,
12001                   LOOKUP_ONLYCONVERTING);
12002   if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12003     cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12004
12005   return statement;
12006 }
12007
12008 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12009    We need to solve both at the same time because the method used
12010    depends on the existence of members begin or end.
12011    Returns the type deduced for the iterator expression.  */
12012
12013 static tree
12014 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12015 {
12016   if (error_operand_p (range))
12017     {
12018       *begin = *end = error_mark_node;
12019       return error_mark_node;
12020     }
12021
12022   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12023     {
12024       error ("range-based %<for%> expression of type %qT "
12025              "has incomplete type", TREE_TYPE (range));
12026       *begin = *end = error_mark_node;
12027       return error_mark_node;
12028     }
12029   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12030     {
12031       /* If RANGE is an array, we will use pointer arithmetic.  */
12032       *begin = decay_conversion (range, tf_warning_or_error);
12033       *end = build_binary_op (input_location, PLUS_EXPR,
12034                               range,
12035                               array_type_nelts_top (TREE_TYPE (range)),
12036                               false);
12037       return TREE_TYPE (*begin);
12038     }
12039   else
12040     {
12041       /* If it is not an array, we must do a bit of magic.  */
12042       tree id_begin, id_end;
12043       tree member_begin, member_end;
12044
12045       *begin = *end = error_mark_node;
12046
12047       id_begin = get_identifier ("begin");
12048       id_end = get_identifier ("end");
12049       member_begin = lookup_member (TREE_TYPE (range), id_begin,
12050                                     /*protect=*/2, /*want_type=*/false,
12051                                     tf_warning_or_error);
12052       member_end = lookup_member (TREE_TYPE (range), id_end,
12053                                   /*protect=*/2, /*want_type=*/false,
12054                                   tf_warning_or_error);
12055
12056       if (member_begin != NULL_TREE && member_end != NULL_TREE)
12057         {
12058           /* Use the member functions.  */
12059           *begin = cp_parser_range_for_member_function (range, id_begin);
12060           *end = cp_parser_range_for_member_function (range, id_end);
12061         }
12062       else
12063         {
12064           /* Use global functions with ADL.  */
12065           vec<tree, va_gc> *vec;
12066           vec = make_tree_vector ();
12067
12068           vec_safe_push (vec, range);
12069
12070           member_begin = perform_koenig_lookup (id_begin, vec,
12071                                                 tf_warning_or_error);
12072           *begin = finish_call_expr (member_begin, &vec, false, true,
12073                                      tf_warning_or_error);
12074           member_end = perform_koenig_lookup (id_end, vec,
12075                                               tf_warning_or_error);
12076           *end = finish_call_expr (member_end, &vec, false, true,
12077                                    tf_warning_or_error);
12078
12079           release_tree_vector (vec);
12080         }
12081
12082       /* Last common checks.  */
12083       if (*begin == error_mark_node || *end == error_mark_node)
12084         {
12085           /* If one of the expressions is an error do no more checks.  */
12086           *begin = *end = error_mark_node;
12087           return error_mark_node;
12088         }
12089       else if (type_dependent_expression_p (*begin)
12090                || type_dependent_expression_p (*end))
12091         /* Can happen, when, eg, in a template context, Koenig lookup
12092            can't resolve begin/end (c++/58503).  */
12093         return NULL_TREE;
12094       else
12095         {
12096           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12097           /* The unqualified type of the __begin and __end temporaries should
12098              be the same, as required by the multiple auto declaration.  */
12099           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12100             {
12101               if (cxx_dialect >= cxx17
12102                   && (build_x_binary_op (input_location, NE_EXPR,
12103                                          *begin, ERROR_MARK,
12104                                          *end, ERROR_MARK,
12105                                          NULL, tf_none)
12106                       != error_mark_node))
12107                 /* P0184R0 allows __begin and __end to have different types,
12108                    but make sure they are comparable so we can give a better
12109                    diagnostic.  */;
12110               else
12111                 error ("inconsistent begin/end types in range-based %<for%> "
12112                        "statement: %qT and %qT",
12113                        TREE_TYPE (*begin), TREE_TYPE (*end));
12114             }
12115           return iter_type;
12116         }
12117     }
12118 }
12119
12120 /* Helper function for cp_parser_perform_range_for_lookup.
12121    Builds a tree for RANGE.IDENTIFIER().  */
12122
12123 static tree
12124 cp_parser_range_for_member_function (tree range, tree identifier)
12125 {
12126   tree member, res;
12127   vec<tree, va_gc> *vec;
12128
12129   member = finish_class_member_access_expr (range, identifier,
12130                                             false, tf_warning_or_error);
12131   if (member == error_mark_node)
12132     return error_mark_node;
12133
12134   vec = make_tree_vector ();
12135   res = finish_call_expr (member, &vec,
12136                           /*disallow_virtual=*/false,
12137                           /*koenig_p=*/false,
12138                           tf_warning_or_error);
12139   release_tree_vector (vec);
12140   return res;
12141 }
12142
12143 /* Parse an iteration-statement.
12144
12145    iteration-statement:
12146      while ( condition ) statement
12147      do statement while ( expression ) ;
12148      for ( init-statement condition [opt] ; expression [opt] )
12149        statement
12150
12151    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
12152
12153 static tree
12154 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12155                                unsigned short unroll)
12156 {
12157   cp_token *token;
12158   enum rid keyword;
12159   tree statement;
12160   unsigned char in_statement;
12161   token_indent_info guard_tinfo;
12162
12163   /* Peek at the next token.  */
12164   token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12165   if (!token)
12166     return error_mark_node;
12167
12168   guard_tinfo = get_token_indent_info (token);
12169
12170   /* Remember whether or not we are already within an iteration
12171      statement.  */
12172   in_statement = parser->in_statement;
12173
12174   /* See what kind of keyword it is.  */
12175   keyword = token->keyword;
12176   switch (keyword)
12177     {
12178     case RID_WHILE:
12179       {
12180         tree condition;
12181
12182         /* Begin the while-statement.  */
12183         statement = begin_while_stmt ();
12184         /* Look for the `('.  */
12185         matching_parens parens;
12186         parens.require_open (parser);
12187         /* Parse the condition.  */
12188         condition = cp_parser_condition (parser);
12189         finish_while_stmt_cond (condition, statement, ivdep, unroll);
12190         /* Look for the `)'.  */
12191         parens.require_close (parser);
12192         /* Parse the dependent statement.  */
12193         parser->in_statement = IN_ITERATION_STMT;
12194         bool prev = note_iteration_stmt_body_start ();
12195         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12196         note_iteration_stmt_body_end (prev);
12197         parser->in_statement = in_statement;
12198         /* We're done with the while-statement.  */
12199         finish_while_stmt (statement);
12200       }
12201       break;
12202
12203     case RID_DO:
12204       {
12205         tree expression;
12206
12207         /* Begin the do-statement.  */
12208         statement = begin_do_stmt ();
12209         /* Parse the body of the do-statement.  */
12210         parser->in_statement = IN_ITERATION_STMT;
12211         bool prev = note_iteration_stmt_body_start ();
12212         cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12213         note_iteration_stmt_body_end (prev);
12214         parser->in_statement = in_statement;
12215         finish_do_body (statement);
12216         /* Look for the `while' keyword.  */
12217         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12218         /* Look for the `('.  */
12219         matching_parens parens;
12220         parens.require_open (parser);
12221         /* Parse the expression.  */
12222         expression = cp_parser_expression (parser);
12223         /* We're done with the do-statement.  */
12224         finish_do_stmt (expression, statement, ivdep, unroll);
12225         /* Look for the `)'.  */
12226         parens.require_close (parser);
12227         /* Look for the `;'.  */
12228         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12229       }
12230       break;
12231
12232     case RID_FOR:
12233       {
12234         /* Look for the `('.  */
12235         matching_parens parens;
12236         parens.require_open (parser);
12237
12238         statement = cp_parser_for (parser, ivdep, unroll);
12239
12240         /* Look for the `)'.  */
12241         parens.require_close (parser);
12242
12243         /* Parse the body of the for-statement.  */
12244         parser->in_statement = IN_ITERATION_STMT;
12245         bool prev = note_iteration_stmt_body_start ();
12246         cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12247         note_iteration_stmt_body_end (prev);
12248         parser->in_statement = in_statement;
12249
12250         /* We're done with the for-statement.  */
12251         finish_for_stmt (statement);
12252       }
12253       break;
12254
12255     default:
12256       cp_parser_error (parser, "expected iteration-statement");
12257       statement = error_mark_node;
12258       break;
12259     }
12260
12261   return statement;
12262 }
12263
12264 /* Parse a init-statement or the declarator of a range-based-for.
12265    Returns true if a range-based-for declaration is seen.
12266
12267    init-statement:
12268      expression-statement
12269      simple-declaration  */
12270
12271 static bool
12272 cp_parser_init_statement (cp_parser* parser, tree *decl)
12273 {
12274   /* If the next token is a `;', then we have an empty
12275      expression-statement.  Grammatically, this is also a
12276      simple-declaration, but an invalid one, because it does not
12277      declare anything.  Therefore, if we did not handle this case
12278      specially, we would issue an error message about an invalid
12279      declaration.  */
12280   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12281     {
12282       bool is_range_for = false;
12283       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12284
12285       /* A colon is used in range-based for.  */
12286       parser->colon_corrects_to_scope_p = false;
12287
12288       /* We're going to speculatively look for a declaration, falling back
12289          to an expression, if necessary.  */
12290       cp_parser_parse_tentatively (parser);
12291       /* Parse the declaration.  */
12292       cp_parser_simple_declaration (parser,
12293                                     /*function_definition_allowed_p=*/false,
12294                                     decl);
12295       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12296       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12297         {
12298           /* It is a range-for, consume the ':' */
12299           cp_lexer_consume_token (parser->lexer);
12300           is_range_for = true;
12301           if (cxx_dialect < cxx11)
12302             pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12303                      "range-based %<for%> loops only available with "
12304                      "-std=c++11 or -std=gnu++11");
12305         }
12306       else
12307           /* The ';' is not consumed yet because we told
12308              cp_parser_simple_declaration not to.  */
12309           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12310
12311       if (cp_parser_parse_definitely (parser))
12312         return is_range_for;
12313       /* If the tentative parse failed, then we shall need to look for an
12314          expression-statement.  */
12315     }
12316   /* If we are here, it is an expression-statement.  */
12317   cp_parser_expression_statement (parser, NULL_TREE);
12318   return false;
12319 }
12320
12321 /* Parse a jump-statement.
12322
12323    jump-statement:
12324      break ;
12325      continue ;
12326      return expression [opt] ;
12327      return braced-init-list ;
12328      goto identifier ;
12329
12330    GNU extension:
12331
12332    jump-statement:
12333      goto * expression ;
12334
12335    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
12336
12337 static tree
12338 cp_parser_jump_statement (cp_parser* parser)
12339 {
12340   tree statement = error_mark_node;
12341   cp_token *token;
12342   enum rid keyword;
12343   unsigned char in_statement;
12344
12345   /* Peek at the next token.  */
12346   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12347   if (!token)
12348     return error_mark_node;
12349
12350   /* See what kind of keyword it is.  */
12351   keyword = token->keyword;
12352   switch (keyword)
12353     {
12354     case RID_BREAK:
12355       in_statement = parser->in_statement & ~IN_IF_STMT;      
12356       switch (in_statement)
12357         {
12358         case 0:
12359           error_at (token->location, "break statement not within loop or switch");
12360           break;
12361         default:
12362           gcc_assert ((in_statement & IN_SWITCH_STMT)
12363                       || in_statement == IN_ITERATION_STMT);
12364           statement = finish_break_stmt ();
12365           if (in_statement == IN_ITERATION_STMT)
12366             break_maybe_infinite_loop ();
12367           break;
12368         case IN_OMP_BLOCK:
12369           error_at (token->location, "invalid exit from OpenMP structured block");
12370           break;
12371         case IN_OMP_FOR:
12372           error_at (token->location, "break statement used with OpenMP for loop");
12373           break;
12374         }
12375       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12376       break;
12377
12378     case RID_CONTINUE:
12379       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12380         {
12381         case 0:
12382           error_at (token->location, "continue statement not within a loop");
12383           break;
12384           /* Fall through.  */
12385         case IN_ITERATION_STMT:
12386         case IN_OMP_FOR:
12387           statement = finish_continue_stmt ();
12388           break;
12389         case IN_OMP_BLOCK:
12390           error_at (token->location, "invalid exit from OpenMP structured block");
12391           break;
12392         default:
12393           gcc_unreachable ();
12394         }
12395       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12396       break;
12397
12398     case RID_RETURN:
12399       {
12400         tree expr;
12401         bool expr_non_constant_p;
12402
12403         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12404           {
12405             cp_lexer_set_source_position (parser->lexer);
12406             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12407             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12408           }
12409         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12410           expr = cp_parser_expression (parser);
12411         else
12412           /* If the next token is a `;', then there is no
12413              expression.  */
12414           expr = NULL_TREE;
12415         /* Build the return-statement.  */
12416         if (current_function_auto_return_pattern && in_discarded_stmt)
12417           /* Don't deduce from a discarded return statement.  */;
12418         else
12419           statement = finish_return_stmt (expr);
12420         /* Look for the final `;'.  */
12421         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12422       }
12423       break;
12424
12425     case RID_GOTO:
12426       if (parser->in_function_body
12427           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12428         {
12429           error ("%<goto%> in %<constexpr%> function");
12430           cp_function_chain->invalid_constexpr = true;
12431         }
12432
12433       /* Create the goto-statement.  */
12434       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12435         {
12436           /* Issue a warning about this use of a GNU extension.  */
12437           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12438           /* Consume the '*' token.  */
12439           cp_lexer_consume_token (parser->lexer);
12440           /* Parse the dependent expression.  */
12441           finish_goto_stmt (cp_parser_expression (parser));
12442         }
12443       else
12444         finish_goto_stmt (cp_parser_identifier (parser));
12445       /* Look for the final `;'.  */
12446       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12447       break;
12448
12449     default:
12450       cp_parser_error (parser, "expected jump-statement");
12451       break;
12452     }
12453
12454   return statement;
12455 }
12456
12457 /* Parse a declaration-statement.
12458
12459    declaration-statement:
12460      block-declaration  */
12461
12462 static void
12463 cp_parser_declaration_statement (cp_parser* parser)
12464 {
12465   void *p;
12466
12467   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12468   p = obstack_alloc (&declarator_obstack, 0);
12469
12470  /* Parse the block-declaration.  */
12471   cp_parser_block_declaration (parser, /*statement_p=*/true);
12472
12473   /* Free any declarators allocated.  */
12474   obstack_free (&declarator_obstack, p);
12475 }
12476
12477 /* Some dependent statements (like `if (cond) statement'), are
12478    implicitly in their own scope.  In other words, if the statement is
12479    a single statement (as opposed to a compound-statement), it is
12480    none-the-less treated as if it were enclosed in braces.  Any
12481    declarations appearing in the dependent statement are out of scope
12482    after control passes that point.  This function parses a statement,
12483    but ensures that is in its own scope, even if it is not a
12484    compound-statement.
12485
12486    If IF_P is not NULL, *IF_P is set to indicate whether the statement
12487    is a (possibly labeled) if statement which is not enclosed in
12488    braces and has an else clause.  This is used to implement
12489    -Wparentheses.
12490
12491    CHAIN is a vector of if-else-if conditions.  This is used to implement
12492    -Wduplicated-cond.
12493
12494    Returns the new statement.  */
12495
12496 static tree
12497 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12498                                        const token_indent_info &guard_tinfo,
12499                                        vec<tree> *chain)
12500 {
12501   tree statement;
12502   location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12503   location_t body_loc_after_labels = UNKNOWN_LOCATION;
12504   token_indent_info body_tinfo
12505     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12506
12507   if (if_p != NULL)
12508     *if_p = false;
12509
12510   /* Mark if () ; with a special NOP_EXPR.  */
12511   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12512     {
12513       cp_lexer_consume_token (parser->lexer);
12514       statement = add_stmt (build_empty_stmt (body_loc));
12515
12516       if (guard_tinfo.keyword == RID_IF
12517           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12518         warning_at (body_loc, OPT_Wempty_body,
12519                     "suggest braces around empty body in an %<if%> statement");
12520       else if (guard_tinfo.keyword == RID_ELSE)
12521         warning_at (body_loc, OPT_Wempty_body,
12522                     "suggest braces around empty body in an %<else%> statement");
12523     }
12524   /* if a compound is opened, we simply parse the statement directly.  */
12525   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12526     statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12527   /* If the token is not a `{', then we must take special action.  */
12528   else
12529     {
12530       /* Create a compound-statement.  */
12531       statement = begin_compound_stmt (0);
12532       /* Parse the dependent-statement.  */
12533       cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12534                            &body_loc_after_labels);
12535       /* Finish the dummy compound-statement.  */
12536       finish_compound_stmt (statement);
12537     }
12538
12539   token_indent_info next_tinfo
12540     = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12541   warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12542
12543   if (body_loc_after_labels != UNKNOWN_LOCATION
12544       && next_tinfo.type != CPP_SEMICOLON)
12545     warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12546                                     guard_tinfo.location, guard_tinfo.keyword);
12547
12548   /* Return the statement.  */
12549   return statement;
12550 }
12551
12552 /* For some dependent statements (like `while (cond) statement'), we
12553    have already created a scope.  Therefore, even if the dependent
12554    statement is a compound-statement, we do not want to create another
12555    scope.  */
12556
12557 static void
12558 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12559                                     const token_indent_info &guard_tinfo)
12560 {
12561   /* If the token is a `{', then we must take special action.  */
12562   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12563     {
12564       token_indent_info body_tinfo
12565         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12566       location_t loc_after_labels = UNKNOWN_LOCATION;
12567
12568       cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12569                            &loc_after_labels);
12570       token_indent_info next_tinfo
12571         = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12572       warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12573
12574       if (loc_after_labels != UNKNOWN_LOCATION
12575           && next_tinfo.type != CPP_SEMICOLON)
12576         warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12577                                         guard_tinfo.location,
12578                                         guard_tinfo.keyword);
12579     }
12580   else
12581     {
12582       /* Avoid calling cp_parser_compound_statement, so that we
12583          don't create a new scope.  Do everything else by hand.  */
12584       matching_braces braces;
12585       braces.require_open (parser);
12586       /* If the next keyword is `__label__' we have a label declaration.  */
12587       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12588         cp_parser_label_declaration (parser);
12589       /* Parse an (optional) statement-seq.  */
12590       cp_parser_statement_seq_opt (parser, NULL_TREE);
12591       braces.require_close (parser);
12592     }
12593 }
12594
12595 /* Declarations [gram.dcl.dcl] */
12596
12597 /* Parse an optional declaration-sequence.
12598
12599    declaration-seq:
12600      declaration
12601      declaration-seq declaration  */
12602
12603 static void
12604 cp_parser_declaration_seq_opt (cp_parser* parser)
12605 {
12606   while (true)
12607     {
12608       cp_token *token;
12609
12610       token = cp_lexer_peek_token (parser->lexer);
12611
12612       if (token->type == CPP_CLOSE_BRACE
12613           || token->type == CPP_EOF
12614           || token->type == CPP_PRAGMA_EOL)
12615         break;
12616
12617       if (token->type == CPP_SEMICOLON)
12618         {
12619           /* A declaration consisting of a single semicolon is
12620              invalid.  Allow it unless we're being pedantic.  */
12621           cp_lexer_consume_token (parser->lexer);
12622           if (!in_system_header_at (input_location))
12623             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12624           continue;
12625         }
12626
12627       /* If we're entering or exiting a region that's implicitly
12628          extern "C", modify the lang context appropriately.  */
12629       if (!parser->implicit_extern_c && token->implicit_extern_c)
12630         {
12631           push_lang_context (lang_name_c);
12632           parser->implicit_extern_c = true;
12633         }
12634       else if (parser->implicit_extern_c && !token->implicit_extern_c)
12635         {
12636           pop_lang_context ();
12637           parser->implicit_extern_c = false;
12638         }
12639
12640       if (token->type == CPP_PRAGMA)
12641         {
12642           /* A top-level declaration can consist solely of a #pragma.
12643              A nested declaration cannot, so this is done here and not
12644              in cp_parser_declaration.  (A #pragma at block scope is
12645              handled in cp_parser_statement.)  */
12646           cp_parser_pragma (parser, pragma_external, NULL);
12647           continue;
12648         }
12649
12650       /* Parse the declaration itself.  */
12651       cp_parser_declaration (parser);
12652     }
12653 }
12654
12655 /* Parse a declaration.
12656
12657    declaration:
12658      block-declaration
12659      function-definition
12660      template-declaration
12661      explicit-instantiation
12662      explicit-specialization
12663      linkage-specification
12664      namespace-definition
12665
12666    C++17:
12667      deduction-guide
12668
12669    GNU extension:
12670
12671    declaration:
12672       __extension__ declaration */
12673
12674 static void
12675 cp_parser_declaration (cp_parser* parser)
12676 {
12677   cp_token token1;
12678   cp_token token2;
12679   int saved_pedantic;
12680   void *p;
12681   tree attributes = NULL_TREE;
12682
12683   /* Check for the `__extension__' keyword.  */
12684   if (cp_parser_extension_opt (parser, &saved_pedantic))
12685     {
12686       /* Parse the qualified declaration.  */
12687       cp_parser_declaration (parser);
12688       /* Restore the PEDANTIC flag.  */
12689       pedantic = saved_pedantic;
12690
12691       return;
12692     }
12693
12694   /* Try to figure out what kind of declaration is present.  */
12695   token1 = *cp_lexer_peek_token (parser->lexer);
12696
12697   if (token1.type != CPP_EOF)
12698     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12699   else
12700     {
12701       token2.type = CPP_EOF;
12702       token2.keyword = RID_MAX;
12703     }
12704
12705   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
12706   p = obstack_alloc (&declarator_obstack, 0);
12707
12708   /* If the next token is `extern' and the following token is a string
12709      literal, then we have a linkage specification.  */
12710   if (token1.keyword == RID_EXTERN
12711       && cp_parser_is_pure_string_literal (&token2))
12712     cp_parser_linkage_specification (parser);
12713   /* If the next token is `template', then we have either a template
12714      declaration, an explicit instantiation, or an explicit
12715      specialization.  */
12716   else if (token1.keyword == RID_TEMPLATE)
12717     {
12718       /* `template <>' indicates a template specialization.  */
12719       if (token2.type == CPP_LESS
12720           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12721         cp_parser_explicit_specialization (parser);
12722       /* `template <' indicates a template declaration.  */
12723       else if (token2.type == CPP_LESS)
12724         cp_parser_template_declaration (parser, /*member_p=*/false);
12725       /* Anything else must be an explicit instantiation.  */
12726       else
12727         cp_parser_explicit_instantiation (parser);
12728     }
12729   /* If the next token is `export', then we have a template
12730      declaration.  */
12731   else if (token1.keyword == RID_EXPORT)
12732     cp_parser_template_declaration (parser, /*member_p=*/false);
12733   /* If the next token is `extern', 'static' or 'inline' and the one
12734      after that is `template', we have a GNU extended explicit
12735      instantiation directive.  */
12736   else if (cp_parser_allow_gnu_extensions_p (parser)
12737            && (token1.keyword == RID_EXTERN
12738                || token1.keyword == RID_STATIC
12739                || token1.keyword == RID_INLINE)
12740            && token2.keyword == RID_TEMPLATE)
12741     cp_parser_explicit_instantiation (parser);
12742   /* If the next token is `namespace', check for a named or unnamed
12743      namespace definition.  */
12744   else if (token1.keyword == RID_NAMESPACE
12745            && (/* A named namespace definition.  */
12746                (token2.type == CPP_NAME
12747                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12748                     != CPP_EQ))
12749                || (token2.type == CPP_OPEN_SQUARE
12750                    && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12751                    == CPP_OPEN_SQUARE)
12752                /* An unnamed namespace definition.  */
12753                || token2.type == CPP_OPEN_BRACE
12754                || token2.keyword == RID_ATTRIBUTE))
12755     cp_parser_namespace_definition (parser);
12756   /* An inline (associated) namespace definition.  */
12757   else if (token1.keyword == RID_INLINE
12758            && token2.keyword == RID_NAMESPACE)
12759     cp_parser_namespace_definition (parser);
12760   /* Objective-C++ declaration/definition.  */
12761   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12762     cp_parser_objc_declaration (parser, NULL_TREE);
12763   else if (c_dialect_objc ()
12764            && token1.keyword == RID_ATTRIBUTE
12765            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12766     cp_parser_objc_declaration (parser, attributes);
12767   /* At this point we may have a template declared by a concept
12768      introduction.  */
12769   else if (flag_concepts
12770            && cp_parser_template_declaration_after_export (parser,
12771                                                            /*member_p=*/false))
12772     /* We did.  */;
12773   else
12774     /* Try to parse a block-declaration, or a function-definition.  */
12775     cp_parser_block_declaration (parser, /*statement_p=*/false);
12776
12777   /* Free any declarators allocated.  */
12778   obstack_free (&declarator_obstack, p);
12779 }
12780
12781 /* Parse a block-declaration.
12782
12783    block-declaration:
12784      simple-declaration
12785      asm-definition
12786      namespace-alias-definition
12787      using-declaration
12788      using-directive
12789
12790    GNU Extension:
12791
12792    block-declaration:
12793      __extension__ block-declaration
12794
12795    C++0x Extension:
12796
12797    block-declaration:
12798      static_assert-declaration
12799
12800    If STATEMENT_P is TRUE, then this block-declaration is occurring as
12801    part of a declaration-statement.  */
12802
12803 static void
12804 cp_parser_block_declaration (cp_parser *parser,
12805                              bool      statement_p)
12806 {
12807   cp_token *token1;
12808   int saved_pedantic;
12809
12810   /* Check for the `__extension__' keyword.  */
12811   if (cp_parser_extension_opt (parser, &saved_pedantic))
12812     {
12813       /* Parse the qualified declaration.  */
12814       cp_parser_block_declaration (parser, statement_p);
12815       /* Restore the PEDANTIC flag.  */
12816       pedantic = saved_pedantic;
12817
12818       return;
12819     }
12820
12821   /* Peek at the next token to figure out which kind of declaration is
12822      present.  */
12823   token1 = cp_lexer_peek_token (parser->lexer);
12824
12825   /* If the next keyword is `asm', we have an asm-definition.  */
12826   if (token1->keyword == RID_ASM)
12827     {
12828       if (statement_p)
12829         cp_parser_commit_to_tentative_parse (parser);
12830       cp_parser_asm_definition (parser);
12831     }
12832   /* If the next keyword is `namespace', we have a
12833      namespace-alias-definition.  */
12834   else if (token1->keyword == RID_NAMESPACE)
12835     cp_parser_namespace_alias_definition (parser);
12836   /* If the next keyword is `using', we have a
12837      using-declaration, a using-directive, or an alias-declaration.  */
12838   else if (token1->keyword == RID_USING)
12839     {
12840       cp_token *token2;
12841
12842       if (statement_p)
12843         cp_parser_commit_to_tentative_parse (parser);
12844       /* If the token after `using' is `namespace', then we have a
12845          using-directive.  */
12846       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12847       if (token2->keyword == RID_NAMESPACE)
12848         cp_parser_using_directive (parser);
12849       /* If the second token after 'using' is '=', then we have an
12850          alias-declaration.  */
12851       else if (cxx_dialect >= cxx11
12852                && token2->type == CPP_NAME
12853                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12854                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12855         cp_parser_alias_declaration (parser);
12856       /* Otherwise, it's a using-declaration.  */
12857       else
12858         cp_parser_using_declaration (parser,
12859                                      /*access_declaration_p=*/false);
12860     }
12861   /* If the next keyword is `__label__' we have a misplaced label
12862      declaration.  */
12863   else if (token1->keyword == RID_LABEL)
12864     {
12865       cp_lexer_consume_token (parser->lexer);
12866       error_at (token1->location, "%<__label__%> not at the beginning of a block");
12867       cp_parser_skip_to_end_of_statement (parser);
12868       /* If the next token is now a `;', consume it.  */
12869       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12870         cp_lexer_consume_token (parser->lexer);
12871     }
12872   /* If the next token is `static_assert' we have a static assertion.  */
12873   else if (token1->keyword == RID_STATIC_ASSERT)
12874     cp_parser_static_assert (parser, /*member_p=*/false);
12875   /* Anything else must be a simple-declaration.  */
12876   else
12877     cp_parser_simple_declaration (parser, !statement_p,
12878                                   /*maybe_range_for_decl*/NULL);
12879 }
12880
12881 /* Parse a simple-declaration.
12882
12883    simple-declaration:
12884      decl-specifier-seq [opt] init-declarator-list [opt] ;
12885      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12886        brace-or-equal-initializer ;
12887
12888    init-declarator-list:
12889      init-declarator
12890      init-declarator-list , init-declarator
12891
12892    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12893    function-definition as a simple-declaration.
12894
12895    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12896    parsed declaration if it is an uninitialized single declarator not followed
12897    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12898    if present, will not be consumed.  */
12899
12900 static void
12901 cp_parser_simple_declaration (cp_parser* parser,
12902                               bool function_definition_allowed_p,
12903                               tree *maybe_range_for_decl)
12904 {
12905   cp_decl_specifier_seq decl_specifiers;
12906   int declares_class_or_enum;
12907   bool saw_declarator;
12908   location_t comma_loc = UNKNOWN_LOCATION;
12909   location_t init_loc = UNKNOWN_LOCATION;
12910
12911   if (maybe_range_for_decl)
12912     *maybe_range_for_decl = NULL_TREE;
12913
12914   /* Defer access checks until we know what is being declared; the
12915      checks for names appearing in the decl-specifier-seq should be
12916      done as if we were in the scope of the thing being declared.  */
12917   push_deferring_access_checks (dk_deferred);
12918
12919   /* Parse the decl-specifier-seq.  We have to keep track of whether
12920      or not the decl-specifier-seq declares a named class or
12921      enumeration type, since that is the only case in which the
12922      init-declarator-list is allowed to be empty.
12923
12924      [dcl.dcl]
12925
12926      In a simple-declaration, the optional init-declarator-list can be
12927      omitted only when declaring a class or enumeration, that is when
12928      the decl-specifier-seq contains either a class-specifier, an
12929      elaborated-type-specifier, or an enum-specifier.  */
12930   cp_parser_decl_specifier_seq (parser,
12931                                 CP_PARSER_FLAGS_OPTIONAL,
12932                                 &decl_specifiers,
12933                                 &declares_class_or_enum);
12934   /* We no longer need to defer access checks.  */
12935   stop_deferring_access_checks ();
12936
12937   /* In a block scope, a valid declaration must always have a
12938      decl-specifier-seq.  By not trying to parse declarators, we can
12939      resolve the declaration/expression ambiguity more quickly.  */
12940   if (!function_definition_allowed_p
12941       && !decl_specifiers.any_specifiers_p)
12942     {
12943       cp_parser_error (parser, "expected declaration");
12944       goto done;
12945     }
12946
12947   /* If the next two tokens are both identifiers, the code is
12948      erroneous. The usual cause of this situation is code like:
12949
12950        T t;
12951
12952      where "T" should name a type -- but does not.  */
12953   if (!decl_specifiers.any_type_specifiers_p
12954       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12955     {
12956       /* If parsing tentatively, we should commit; we really are
12957          looking at a declaration.  */
12958       cp_parser_commit_to_tentative_parse (parser);
12959       /* Give up.  */
12960       goto done;
12961     }
12962
12963   /* If we have seen at least one decl-specifier, and the next token
12964      is not a parenthesis, then we must be looking at a declaration.
12965      (After "int (" we might be looking at a functional cast.)  */
12966   if (decl_specifiers.any_specifiers_p
12967       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12968       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12969       && !cp_parser_error_occurred (parser))
12970     cp_parser_commit_to_tentative_parse (parser);
12971
12972   /* Look for C++17 decomposition declaration.  */
12973   for (size_t n = 1; ; n++)
12974     if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12975         || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12976       continue;
12977     else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12978              && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12979              && decl_specifiers.any_specifiers_p)
12980       {
12981         tree decl
12982           = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12983                                                  maybe_range_for_decl,
12984                                                  &init_loc);
12985
12986         /* The next token should be either a `,' or a `;'.  */
12987         cp_token *token = cp_lexer_peek_token (parser->lexer);
12988         /* If it's a `;', we are done.  */
12989         if (token->type == CPP_SEMICOLON)
12990           goto finish;
12991         else if (maybe_range_for_decl)
12992           {
12993             if (*maybe_range_for_decl == NULL_TREE)
12994               *maybe_range_for_decl = error_mark_node;
12995             goto finish;
12996           }
12997         /* Anything else is an error.  */
12998         else
12999           {
13000             /* If we have already issued an error message we don't need
13001                to issue another one.  */
13002             if ((decl != error_mark_node
13003                  && DECL_INITIAL (decl) != error_mark_node)
13004                 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13005               cp_parser_error (parser, "expected %<,%> or %<;%>");
13006             /* Skip tokens until we reach the end of the statement.  */
13007             cp_parser_skip_to_end_of_statement (parser);
13008             /* If the next token is now a `;', consume it.  */
13009             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13010               cp_lexer_consume_token (parser->lexer);
13011             goto done;
13012           }
13013       }
13014     else
13015       break;
13016
13017   tree last_type;
13018   bool auto_specifier_p;
13019   /* NULL_TREE if both variable and function declaration are allowed,
13020      error_mark_node if function declaration are not allowed and
13021      a FUNCTION_DECL that should be diagnosed if it is followed by
13022      variable declarations.  */
13023   tree auto_function_declaration;
13024
13025   last_type = NULL_TREE;
13026   auto_specifier_p
13027     = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13028   auto_function_declaration = NULL_TREE;
13029
13030   /* Keep going until we hit the `;' at the end of the simple
13031      declaration.  */
13032   saw_declarator = false;
13033   while (cp_lexer_next_token_is_not (parser->lexer,
13034                                      CPP_SEMICOLON))
13035     {
13036       cp_token *token;
13037       bool function_definition_p;
13038       tree decl;
13039       tree auto_result = NULL_TREE;
13040
13041       if (saw_declarator)
13042         {
13043           /* If we are processing next declarator, comma is expected */
13044           token = cp_lexer_peek_token (parser->lexer);
13045           gcc_assert (token->type == CPP_COMMA);
13046           cp_lexer_consume_token (parser->lexer);
13047           if (maybe_range_for_decl)
13048             {
13049               *maybe_range_for_decl = error_mark_node;
13050               if (comma_loc == UNKNOWN_LOCATION)
13051                 comma_loc = token->location;
13052             }
13053         }
13054       else
13055         saw_declarator = true;
13056
13057       /* Parse the init-declarator.  */
13058       decl = cp_parser_init_declarator (parser, &decl_specifiers,
13059                                         /*checks=*/NULL,
13060                                         function_definition_allowed_p,
13061                                         /*member_p=*/false,
13062                                         declares_class_or_enum,
13063                                         &function_definition_p,
13064                                         maybe_range_for_decl,
13065                                         &init_loc,
13066                                         &auto_result);
13067       /* If an error occurred while parsing tentatively, exit quickly.
13068          (That usually happens when in the body of a function; each
13069          statement is treated as a declaration-statement until proven
13070          otherwise.)  */
13071       if (cp_parser_error_occurred (parser))
13072         goto done;
13073
13074       if (auto_specifier_p && cxx_dialect >= cxx14)
13075         {
13076           /* If the init-declarator-list contains more than one
13077              init-declarator, they shall all form declarations of
13078              variables.  */
13079           if (auto_function_declaration == NULL_TREE)
13080             auto_function_declaration
13081               = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13082           else if (TREE_CODE (decl) == FUNCTION_DECL
13083                    || auto_function_declaration != error_mark_node)
13084             {
13085               error_at (decl_specifiers.locations[ds_type_spec],
13086                         "non-variable %qD in declaration with more than one "
13087                         "declarator with placeholder type",
13088                         TREE_CODE (decl) == FUNCTION_DECL
13089                         ? decl : auto_function_declaration);
13090               auto_function_declaration = error_mark_node;
13091             }
13092         }
13093
13094       if (auto_result
13095           && (!processing_template_decl || !type_uses_auto (auto_result)))
13096         {
13097           if (last_type
13098               && last_type != error_mark_node
13099               && !same_type_p (auto_result, last_type))
13100             {
13101               /* If the list of declarators contains more than one declarator,
13102                  the type of each declared variable is determined as described
13103                  above. If the type deduced for the template parameter U is not
13104                  the same in each deduction, the program is ill-formed.  */
13105               error_at (decl_specifiers.locations[ds_type_spec],
13106                         "inconsistent deduction for %qT: %qT and then %qT",
13107                         decl_specifiers.type, last_type, auto_result);
13108               last_type = error_mark_node;
13109             }
13110           else
13111             last_type = auto_result;
13112         }
13113
13114       /* Handle function definitions specially.  */
13115       if (function_definition_p)
13116         {
13117           /* If the next token is a `,', then we are probably
13118              processing something like:
13119
13120                void f() {}, *p;
13121
13122              which is erroneous.  */
13123           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13124             {
13125               cp_token *token = cp_lexer_peek_token (parser->lexer);
13126               error_at (token->location,
13127                         "mixing"
13128                         " declarations and function-definitions is forbidden");
13129             }
13130           /* Otherwise, we're done with the list of declarators.  */
13131           else
13132             {
13133               pop_deferring_access_checks ();
13134               return;
13135             }
13136         }
13137       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13138         *maybe_range_for_decl = decl;
13139       /* The next token should be either a `,' or a `;'.  */
13140       token = cp_lexer_peek_token (parser->lexer);
13141       /* If it's a `,', there are more declarators to come.  */
13142       if (token->type == CPP_COMMA)
13143         /* will be consumed next time around */;
13144       /* If it's a `;', we are done.  */
13145       else if (token->type == CPP_SEMICOLON)
13146         break;
13147       else if (maybe_range_for_decl)
13148         {
13149           if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13150             permerror (decl_specifiers.locations[ds_type_spec],
13151                        "types may not be defined in a for-range-declaration");
13152           break;
13153         }
13154       /* Anything else is an error.  */
13155       else
13156         {
13157           /* If we have already issued an error message we don't need
13158              to issue another one.  */
13159           if ((decl != error_mark_node
13160                && DECL_INITIAL (decl) != error_mark_node)
13161               || cp_parser_uncommitted_to_tentative_parse_p (parser))
13162             cp_parser_error (parser, "expected %<,%> or %<;%>");
13163           /* Skip tokens until we reach the end of the statement.  */
13164           cp_parser_skip_to_end_of_statement (parser);
13165           /* If the next token is now a `;', consume it.  */
13166           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13167             cp_lexer_consume_token (parser->lexer);
13168           goto done;
13169         }
13170       /* After the first time around, a function-definition is not
13171          allowed -- even if it was OK at first.  For example:
13172
13173            int i, f() {}
13174
13175          is not valid.  */
13176       function_definition_allowed_p = false;
13177     }
13178
13179   /* Issue an error message if no declarators are present, and the
13180      decl-specifier-seq does not itself declare a class or
13181      enumeration: [dcl.dcl]/3.  */
13182   if (!saw_declarator)
13183     {
13184       if (cp_parser_declares_only_class_p (parser))
13185         {
13186           if (!declares_class_or_enum
13187               && decl_specifiers.type
13188               && OVERLOAD_TYPE_P (decl_specifiers.type))
13189             /* Ensure an error is issued anyway when finish_decltype_type,
13190                called via cp_parser_decl_specifier_seq, returns a class or
13191                an enumeration (c++/51786).  */
13192             decl_specifiers.type = NULL_TREE;
13193           shadow_tag (&decl_specifiers);
13194         }
13195       /* Perform any deferred access checks.  */
13196       perform_deferred_access_checks (tf_warning_or_error);
13197     }
13198
13199   /* Consume the `;'.  */
13200  finish:
13201   if (!maybe_range_for_decl)
13202     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13203   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13204     {
13205       if (init_loc != UNKNOWN_LOCATION)
13206         error_at (init_loc, "initializer in range-based %<for%> loop");
13207       if (comma_loc != UNKNOWN_LOCATION)
13208         error_at (comma_loc,
13209                   "multiple declarations in range-based %<for%> loop");
13210     }
13211
13212  done:
13213   pop_deferring_access_checks ();
13214 }
13215
13216 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13217      decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13218        initializer ;  */
13219
13220 static tree
13221 cp_parser_decomposition_declaration (cp_parser *parser,
13222                                      cp_decl_specifier_seq *decl_specifiers,
13223                                      tree *maybe_range_for_decl,
13224                                      location_t *init_loc)
13225 {
13226   cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13227   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13228   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13229
13230   /* Parse the identifier-list.  */
13231   auto_vec<cp_expr, 10> v;
13232   if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13233     while (true)
13234       {
13235         cp_expr e = cp_parser_identifier (parser);
13236         if (e.get_value () == error_mark_node)
13237           break;
13238         v.safe_push (e);
13239         if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13240           break;
13241         cp_lexer_consume_token (parser->lexer);
13242       }
13243
13244   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13245   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13246     {
13247       end_loc = UNKNOWN_LOCATION;
13248       cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13249                                                false);
13250       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13251         cp_lexer_consume_token (parser->lexer);
13252       else
13253         {
13254           cp_parser_skip_to_end_of_statement (parser);
13255           return error_mark_node;
13256         }
13257     }
13258
13259   if (cxx_dialect < cxx17)
13260     pedwarn (loc, 0, "structured bindings only available with "
13261                      "-std=c++17 or -std=gnu++17");
13262
13263   tree pushed_scope;
13264   cp_declarator *declarator = make_declarator (cdk_decomp);
13265   loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13266   declarator->id_loc = loc;
13267   if (ref_qual != REF_QUAL_NONE)
13268     declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13269                                             ref_qual == REF_QUAL_RVALUE,
13270                                             NULL_TREE);
13271   tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13272                           NULL_TREE, decl_specifiers->attributes,
13273                           &pushed_scope);
13274   tree orig_decl = decl;
13275
13276   unsigned int i;
13277   cp_expr e;
13278   cp_decl_specifier_seq decl_specs;
13279   clear_decl_specs (&decl_specs);
13280   decl_specs.type = make_auto ();
13281   tree prev = decl;
13282   FOR_EACH_VEC_ELT (v, i, e)
13283     {
13284       if (i == 0)
13285         declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13286       else
13287         declarator->u.id.unqualified_name = e.get_value ();
13288       declarator->id_loc = e.get_location ();
13289       tree elt_pushed_scope;
13290       tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13291                                NULL_TREE, NULL_TREE, &elt_pushed_scope);
13292       if (decl2 == error_mark_node)
13293         decl = error_mark_node;
13294       else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13295         {
13296           /* Ensure we've diagnosed redeclaration if we aren't creating
13297              a new VAR_DECL.  */
13298           gcc_assert (errorcount);
13299           decl = error_mark_node;
13300         }
13301       else
13302         prev = decl2;
13303       if (elt_pushed_scope)
13304         pop_scope (elt_pushed_scope);
13305     }
13306
13307   if (v.is_empty ())
13308     {
13309       error_at (loc, "empty structured binding declaration");
13310       decl = error_mark_node;
13311     }
13312
13313   if (maybe_range_for_decl == NULL
13314       || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13315     {
13316       bool non_constant_p = false, is_direct_init = false;
13317       *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13318       tree initializer = cp_parser_initializer (parser, &is_direct_init,
13319                                                 &non_constant_p);
13320       if (initializer == NULL_TREE
13321           || (TREE_CODE (initializer) == TREE_LIST
13322               && TREE_CHAIN (initializer))
13323           || (is_direct_init
13324               && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13325               && CONSTRUCTOR_NELTS (initializer) != 1))
13326         {
13327           error_at (loc, "invalid initializer for structured binding "
13328                     "declaration");
13329           initializer = error_mark_node;
13330         }
13331
13332       if (decl != error_mark_node)
13333         {
13334           cp_maybe_mangle_decomp (decl, prev, v.length ());
13335           cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13336                           is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13337           cp_finish_decomp (decl, prev, v.length ());
13338         }
13339     }
13340   else if (decl != error_mark_node)
13341     {
13342       *maybe_range_for_decl = prev;
13343       /* Ensure DECL_VALUE_EXPR is created for all the decls but
13344          the underlying DECL.  */
13345       cp_finish_decomp (decl, prev, v.length ());
13346     }
13347
13348   if (pushed_scope)
13349     pop_scope (pushed_scope);
13350
13351   if (decl == error_mark_node && DECL_P (orig_decl))
13352     {
13353       if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13354         SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13355     }
13356
13357   return decl;
13358 }
13359
13360 /* Parse a decl-specifier-seq.
13361
13362    decl-specifier-seq:
13363      decl-specifier-seq [opt] decl-specifier
13364      decl-specifier attribute-specifier-seq [opt] (C++11)
13365
13366    decl-specifier:
13367      storage-class-specifier
13368      type-specifier
13369      function-specifier
13370      friend
13371      typedef
13372
13373    GNU Extension:
13374
13375    decl-specifier:
13376      attributes
13377
13378    Concepts Extension:
13379
13380    decl-specifier:
13381      concept
13382
13383    Set *DECL_SPECS to a representation of the decl-specifier-seq.
13384
13385    The parser flags FLAGS is used to control type-specifier parsing.
13386
13387    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13388    flags:
13389
13390      1: one of the decl-specifiers is an elaborated-type-specifier
13391         (i.e., a type declaration)
13392      2: one of the decl-specifiers is an enum-specifier or a
13393         class-specifier (i.e., a type definition)
13394
13395    */
13396
13397 static void
13398 cp_parser_decl_specifier_seq (cp_parser* parser,
13399                               cp_parser_flags flags,
13400                               cp_decl_specifier_seq *decl_specs,
13401                               int* declares_class_or_enum)
13402 {
13403   bool constructor_possible_p = !parser->in_declarator_p;
13404   bool found_decl_spec = false;
13405   cp_token *start_token = NULL;
13406   cp_decl_spec ds;
13407
13408   /* Clear DECL_SPECS.  */
13409   clear_decl_specs (decl_specs);
13410
13411   /* Assume no class or enumeration type is declared.  */
13412   *declares_class_or_enum = 0;
13413
13414   /* Keep reading specifiers until there are no more to read.  */
13415   while (true)
13416     {
13417       bool constructor_p;
13418       cp_token *token;
13419       ds = ds_last;
13420
13421       /* Peek at the next token.  */
13422       token = cp_lexer_peek_token (parser->lexer);
13423
13424       /* Save the first token of the decl spec list for error
13425          reporting.  */
13426       if (!start_token)
13427         start_token = token;
13428       /* Handle attributes.  */
13429       if (cp_next_tokens_can_be_attribute_p (parser))
13430         {
13431           /* Parse the attributes.  */
13432           tree attrs = cp_parser_attributes_opt (parser);
13433
13434           /* In a sequence of declaration specifiers, c++11 attributes
13435              appertain to the type that precede them. In that case
13436              [dcl.spec]/1 says:
13437
13438                  The attribute-specifier-seq affects the type only for
13439                  the declaration it appears in, not other declarations
13440                  involving the same type.
13441
13442              But for now let's force the user to position the
13443              attribute either at the beginning of the declaration or
13444              after the declarator-id, which would clearly mean that it
13445              applies to the declarator.  */
13446           if (cxx11_attribute_p (attrs))
13447             {
13448               if (!found_decl_spec)
13449                 /* The c++11 attribute is at the beginning of the
13450                    declaration.  It appertains to the entity being
13451                    declared.  */;
13452               else
13453                 {
13454                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13455                     {
13456                       /*  This is an attribute following a
13457                           class-specifier.  */
13458                       if (decl_specs->type_definition_p)
13459                         warn_misplaced_attr_for_class_type (token->location,
13460                                                             decl_specs->type);
13461                       attrs = NULL_TREE;
13462                     }
13463                   else
13464                     {
13465                       decl_specs->std_attributes
13466                         = attr_chainon (decl_specs->std_attributes, attrs);
13467                       if (decl_specs->locations[ds_std_attribute] == 0)
13468                         decl_specs->locations[ds_std_attribute] = token->location;
13469                     }
13470                   continue;
13471                 }
13472             }
13473
13474           decl_specs->attributes
13475             = attr_chainon (decl_specs->attributes, attrs);
13476           if (decl_specs->locations[ds_attribute] == 0)
13477             decl_specs->locations[ds_attribute] = token->location;
13478           continue;
13479         }
13480       /* Assume we will find a decl-specifier keyword.  */
13481       found_decl_spec = true;
13482       /* If the next token is an appropriate keyword, we can simply
13483          add it to the list.  */
13484       switch (token->keyword)
13485         {
13486           /* decl-specifier:
13487                friend
13488                constexpr */
13489         case RID_FRIEND:
13490           if (!at_class_scope_p ())
13491             {
13492               gcc_rich_location richloc (token->location);
13493               richloc.add_fixit_remove ();
13494               error_at (&richloc, "%<friend%> used outside of class");
13495               cp_lexer_purge_token (parser->lexer);
13496             }
13497           else
13498             {
13499               ds = ds_friend;
13500               /* Consume the token.  */
13501               cp_lexer_consume_token (parser->lexer);
13502             }
13503           break;
13504
13505         case RID_CONSTEXPR:
13506           ds = ds_constexpr;
13507           cp_lexer_consume_token (parser->lexer);
13508           break;
13509
13510         case RID_CONCEPT:
13511           ds = ds_concept;
13512           cp_lexer_consume_token (parser->lexer);
13513           break;
13514
13515           /* function-specifier:
13516                inline
13517                virtual
13518                explicit  */
13519         case RID_INLINE:
13520         case RID_VIRTUAL:
13521         case RID_EXPLICIT:
13522           cp_parser_function_specifier_opt (parser, decl_specs);
13523           break;
13524
13525           /* decl-specifier:
13526                typedef  */
13527         case RID_TYPEDEF:
13528           ds = ds_typedef;
13529           /* Consume the token.  */
13530           cp_lexer_consume_token (parser->lexer);
13531           /* A constructor declarator cannot appear in a typedef.  */
13532           constructor_possible_p = false;
13533           /* The "typedef" keyword can only occur in a declaration; we
13534              may as well commit at this point.  */
13535           cp_parser_commit_to_tentative_parse (parser);
13536
13537           if (decl_specs->storage_class != sc_none)
13538             decl_specs->conflicting_specifiers_p = true;
13539           break;
13540
13541           /* storage-class-specifier:
13542                auto
13543                register
13544                static
13545                extern
13546                mutable
13547
13548              GNU Extension:
13549                thread  */
13550         case RID_AUTO:
13551           if (cxx_dialect == cxx98) 
13552             {
13553               /* Consume the token.  */
13554               cp_lexer_consume_token (parser->lexer);
13555
13556               /* Complain about `auto' as a storage specifier, if
13557                  we're complaining about C++0x compatibility.  */
13558               gcc_rich_location richloc (token->location);
13559               richloc.add_fixit_remove ();
13560               warning_at (&richloc, OPT_Wc__11_compat,
13561                           "%<auto%> changes meaning in C++11; "
13562                           "please remove it");
13563
13564               /* Set the storage class anyway.  */
13565               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13566                                            token);
13567             }
13568           else
13569             /* C++0x auto type-specifier.  */
13570             found_decl_spec = false;
13571           break;
13572
13573         case RID_REGISTER:
13574         case RID_STATIC:
13575         case RID_EXTERN:
13576         case RID_MUTABLE:
13577           /* Consume the token.  */
13578           cp_lexer_consume_token (parser->lexer);
13579           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13580                                        token);
13581           break;
13582         case RID_THREAD:
13583           /* Consume the token.  */
13584           ds = ds_thread;
13585           cp_lexer_consume_token (parser->lexer);
13586           break;
13587
13588         default:
13589           /* We did not yet find a decl-specifier yet.  */
13590           found_decl_spec = false;
13591           break;
13592         }
13593
13594       if (found_decl_spec
13595           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13596           && token->keyword != RID_CONSTEXPR)
13597         error ("decl-specifier invalid in condition");
13598
13599       if (found_decl_spec
13600           && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13601           && token->keyword != RID_MUTABLE
13602           && token->keyword != RID_CONSTEXPR)
13603         error_at (token->location, "%qD invalid in lambda",
13604                   ridpointers[token->keyword]);
13605
13606       if (ds != ds_last)
13607         set_and_check_decl_spec_loc (decl_specs, ds, token);
13608
13609       /* Constructors are a special case.  The `S' in `S()' is not a
13610          decl-specifier; it is the beginning of the declarator.  */
13611       constructor_p
13612         = (!found_decl_spec
13613            && constructor_possible_p
13614            && (cp_parser_constructor_declarator_p
13615                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13616
13617       /* If we don't have a DECL_SPEC yet, then we must be looking at
13618          a type-specifier.  */
13619       if (!found_decl_spec && !constructor_p)
13620         {
13621           int decl_spec_declares_class_or_enum;
13622           bool is_cv_qualifier;
13623           tree type_spec;
13624
13625           type_spec
13626             = cp_parser_type_specifier (parser, flags,
13627                                         decl_specs,
13628                                         /*is_declaration=*/true,
13629                                         &decl_spec_declares_class_or_enum,
13630                                         &is_cv_qualifier);
13631           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13632
13633           /* If this type-specifier referenced a user-defined type
13634              (a typedef, class-name, etc.), then we can't allow any
13635              more such type-specifiers henceforth.
13636
13637              [dcl.spec]
13638
13639              The longest sequence of decl-specifiers that could
13640              possibly be a type name is taken as the
13641              decl-specifier-seq of a declaration.  The sequence shall
13642              be self-consistent as described below.
13643
13644              [dcl.type]
13645
13646              As a general rule, at most one type-specifier is allowed
13647              in the complete decl-specifier-seq of a declaration.  The
13648              only exceptions are the following:
13649
13650              -- const or volatile can be combined with any other
13651                 type-specifier.
13652
13653              -- signed or unsigned can be combined with char, long,
13654                 short, or int.
13655
13656              -- ..
13657
13658              Example:
13659
13660                typedef char* Pc;
13661                void g (const int Pc);
13662
13663              Here, Pc is *not* part of the decl-specifier seq; it's
13664              the declarator.  Therefore, once we see a type-specifier
13665              (other than a cv-qualifier), we forbid any additional
13666              user-defined types.  We *do* still allow things like `int
13667              int' to be considered a decl-specifier-seq, and issue the
13668              error message later.  */
13669           if (type_spec && !is_cv_qualifier)
13670             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13671           /* A constructor declarator cannot follow a type-specifier.  */
13672           if (type_spec)
13673             {
13674               constructor_possible_p = false;
13675               found_decl_spec = true;
13676               if (!is_cv_qualifier)
13677                 decl_specs->any_type_specifiers_p = true;
13678             }
13679         }
13680
13681       /* If we still do not have a DECL_SPEC, then there are no more
13682          decl-specifiers.  */
13683       if (!found_decl_spec)
13684         break;
13685
13686       decl_specs->any_specifiers_p = true;
13687       /* After we see one decl-specifier, further decl-specifiers are
13688          always optional.  */
13689       flags |= CP_PARSER_FLAGS_OPTIONAL;
13690     }
13691
13692   /* Don't allow a friend specifier with a class definition.  */
13693   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13694       && (*declares_class_or_enum & 2))
13695     error_at (decl_specs->locations[ds_friend],
13696               "class definition may not be declared a friend");
13697 }
13698
13699 /* Parse an (optional) storage-class-specifier.
13700
13701    storage-class-specifier:
13702      auto
13703      register
13704      static
13705      extern
13706      mutable
13707
13708    GNU Extension:
13709
13710    storage-class-specifier:
13711      thread
13712
13713    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
13714
13715 static tree
13716 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13717 {
13718   switch (cp_lexer_peek_token (parser->lexer)->keyword)
13719     {
13720     case RID_AUTO:
13721       if (cxx_dialect != cxx98)
13722         return NULL_TREE;
13723       /* Fall through for C++98.  */
13724       gcc_fallthrough ();
13725
13726     case RID_REGISTER:
13727     case RID_STATIC:
13728     case RID_EXTERN:
13729     case RID_MUTABLE:
13730     case RID_THREAD:
13731       /* Consume the token.  */
13732       return cp_lexer_consume_token (parser->lexer)->u.value;
13733
13734     default:
13735       return NULL_TREE;
13736     }
13737 }
13738
13739 /* Parse an (optional) function-specifier.
13740
13741    function-specifier:
13742      inline
13743      virtual
13744      explicit
13745
13746    Returns an IDENTIFIER_NODE corresponding to the keyword used.
13747    Updates DECL_SPECS, if it is non-NULL.  */
13748
13749 static tree
13750 cp_parser_function_specifier_opt (cp_parser* parser,
13751                                   cp_decl_specifier_seq *decl_specs)
13752 {
13753   cp_token *token = cp_lexer_peek_token (parser->lexer);
13754   switch (token->keyword)
13755     {
13756     case RID_INLINE:
13757       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13758       break;
13759
13760     case RID_VIRTUAL:
13761       /* 14.5.2.3 [temp.mem]
13762
13763          A member function template shall not be virtual.  */
13764       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13765           && current_class_type)
13766         error_at (token->location, "templates may not be %<virtual%>");
13767       else
13768         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13769       break;
13770
13771     case RID_EXPLICIT:
13772       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13773       break;
13774
13775     default:
13776       return NULL_TREE;
13777     }
13778
13779   /* Consume the token.  */
13780   return cp_lexer_consume_token (parser->lexer)->u.value;
13781 }
13782
13783 /* Parse a linkage-specification.
13784
13785    linkage-specification:
13786      extern string-literal { declaration-seq [opt] }
13787      extern string-literal declaration  */
13788
13789 static void
13790 cp_parser_linkage_specification (cp_parser* parser)
13791 {
13792   tree linkage;
13793
13794   /* Look for the `extern' keyword.  */
13795   cp_token *extern_token
13796     = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13797
13798   /* Look for the string-literal.  */
13799   cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13800   linkage = cp_parser_string_literal (parser, false, false);
13801
13802   /* Transform the literal into an identifier.  If the literal is a
13803      wide-character string, or contains embedded NULs, then we can't
13804      handle it as the user wants.  */
13805   if (strlen (TREE_STRING_POINTER (linkage))
13806       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13807     {
13808       cp_parser_error (parser, "invalid linkage-specification");
13809       /* Assume C++ linkage.  */
13810       linkage = lang_name_cplusplus;
13811     }
13812   else
13813     linkage = get_identifier (TREE_STRING_POINTER (linkage));
13814
13815   /* We're now using the new linkage.  */
13816   push_lang_context (linkage);
13817
13818   /* Preserve the location of the the innermost linkage specification,
13819      tracking the locations of nested specifications via a local.  */
13820   location_t saved_location
13821     = parser->innermost_linkage_specification_location;
13822   /* Construct a location ranging from the start of the "extern" to
13823      the end of the string-literal, with the caret at the start, e.g.:
13824        extern "C" {
13825        ^~~~~~~~~~
13826   */
13827   parser->innermost_linkage_specification_location
13828     = make_location (extern_token->location,
13829                      extern_token->location,
13830                      get_finish (string_token->location));
13831
13832   /* If the next token is a `{', then we're using the first
13833      production.  */
13834   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13835     {
13836       cp_ensure_no_omp_declare_simd (parser);
13837       cp_ensure_no_oacc_routine (parser);
13838
13839       /* Consume the `{' token.  */
13840       matching_braces braces;
13841       braces.consume_open (parser)->location;
13842       /* Parse the declarations.  */
13843       cp_parser_declaration_seq_opt (parser);
13844       /* Look for the closing `}'.  */
13845       braces.require_close (parser);
13846     }
13847   /* Otherwise, there's just one declaration.  */
13848   else
13849     {
13850       bool saved_in_unbraced_linkage_specification_p;
13851
13852       saved_in_unbraced_linkage_specification_p
13853         = parser->in_unbraced_linkage_specification_p;
13854       parser->in_unbraced_linkage_specification_p = true;
13855       cp_parser_declaration (parser);
13856       parser->in_unbraced_linkage_specification_p
13857         = saved_in_unbraced_linkage_specification_p;
13858     }
13859
13860   /* We're done with the linkage-specification.  */
13861   pop_lang_context ();
13862
13863   /* Restore location of parent linkage specification, if any.  */
13864   parser->innermost_linkage_specification_location = saved_location;
13865 }
13866
13867 /* Parse a static_assert-declaration.
13868
13869    static_assert-declaration:
13870      static_assert ( constant-expression , string-literal ) ; 
13871      static_assert ( constant-expression ) ; (C++17)
13872
13873    If MEMBER_P, this static_assert is a class member.  */
13874
13875 static void 
13876 cp_parser_static_assert(cp_parser *parser, bool member_p)
13877 {
13878   cp_expr condition;
13879   location_t token_loc;
13880   tree message;
13881   bool dummy;
13882
13883   /* Peek at the `static_assert' token so we can keep track of exactly
13884      where the static assertion started.  */
13885   token_loc = cp_lexer_peek_token (parser->lexer)->location;
13886
13887   /* Look for the `static_assert' keyword.  */
13888   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
13889                                   RT_STATIC_ASSERT))
13890     return;
13891
13892   /*  We know we are in a static assertion; commit to any tentative
13893       parse.  */
13894   if (cp_parser_parsing_tentatively (parser))
13895     cp_parser_commit_to_tentative_parse (parser);
13896
13897   /* Parse the `(' starting the static assertion condition.  */
13898   matching_parens parens;
13899   parens.require_open (parser);
13900
13901   /* Parse the constant-expression.  Allow a non-constant expression
13902      here in order to give better diagnostics in finish_static_assert.  */
13903   condition = 
13904     cp_parser_constant_expression (parser,
13905                                    /*allow_non_constant_p=*/true,
13906                                    /*non_constant_p=*/&dummy);
13907
13908   if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13909     {
13910       if (cxx_dialect < cxx17)
13911         pedwarn (input_location, OPT_Wpedantic,
13912                  "static_assert without a message "
13913                  "only available with -std=c++17 or -std=gnu++17");
13914       /* Eat the ')'  */
13915       cp_lexer_consume_token (parser->lexer);
13916       message = build_string (1, "");
13917       TREE_TYPE (message) = char_array_type_node;
13918       fix_string_type (message);
13919     }
13920   else
13921     {
13922       /* Parse the separating `,'.  */
13923       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13924
13925       /* Parse the string-literal message.  */
13926       message = cp_parser_string_literal (parser, 
13927                                           /*translate=*/false,
13928                                           /*wide_ok=*/true);
13929
13930       /* A `)' completes the static assertion.  */
13931       if (!parens.require_close (parser))
13932         cp_parser_skip_to_closing_parenthesis (parser, 
13933                                                /*recovering=*/true, 
13934                                                /*or_comma=*/false,
13935                                                /*consume_paren=*/true);
13936     }
13937
13938   /* A semicolon terminates the declaration.  */
13939   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13940
13941   /* Get the location for the static assertion.  Use that of the
13942      condition if available, otherwise, use that of the "static_assert"
13943      token.  */
13944   location_t assert_loc = condition.get_location ();
13945   if (assert_loc == UNKNOWN_LOCATION)
13946     assert_loc = token_loc;
13947
13948   /* Complete the static assertion, which may mean either processing 
13949      the static assert now or saving it for template instantiation.  */
13950   finish_static_assert (condition, message, assert_loc, member_p);
13951 }
13952
13953 /* Parse the expression in decltype ( expression ).  */
13954
13955 static tree
13956 cp_parser_decltype_expr (cp_parser *parser,
13957                          bool &id_expression_or_member_access_p)
13958 {
13959   cp_token *id_expr_start_token;
13960   tree expr;
13961
13962   /* Since we're going to preserve any side-effects from this parse, set up a
13963      firewall to protect our callers from cp_parser_commit_to_tentative_parse
13964      in the expression.  */
13965   tentative_firewall firewall (parser);
13966
13967   /* First, try parsing an id-expression.  */
13968   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13969   cp_parser_parse_tentatively (parser);
13970   expr = cp_parser_id_expression (parser,
13971                                   /*template_keyword_p=*/false,
13972                                   /*check_dependency_p=*/true,
13973                                   /*template_p=*/NULL,
13974                                   /*declarator_p=*/false,
13975                                   /*optional_p=*/false);
13976
13977   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13978     {
13979       bool non_integral_constant_expression_p = false;
13980       tree id_expression = expr;
13981       cp_id_kind idk;
13982       const char *error_msg;
13983
13984       if (identifier_p (expr))
13985         /* Lookup the name we got back from the id-expression.  */
13986         expr = cp_parser_lookup_name_simple (parser, expr,
13987                                              id_expr_start_token->location);
13988
13989       if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13990         /* A template without args is not a complete id-expression.  */
13991         expr = error_mark_node;
13992
13993       if (expr
13994           && expr != error_mark_node
13995           && TREE_CODE (expr) != TYPE_DECL
13996           && (TREE_CODE (expr) != BIT_NOT_EXPR
13997               || !TYPE_P (TREE_OPERAND (expr, 0)))
13998           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13999         {
14000           /* Complete lookup of the id-expression.  */
14001           expr = (finish_id_expression
14002                   (id_expression, expr, parser->scope, &idk,
14003                    /*integral_constant_expression_p=*/false,
14004                    /*allow_non_integral_constant_expression_p=*/true,
14005                    &non_integral_constant_expression_p,
14006                    /*template_p=*/false,
14007                    /*done=*/true,
14008                    /*address_p=*/false,
14009                    /*template_arg_p=*/false,
14010                    &error_msg,
14011                    id_expr_start_token->location));
14012
14013           if (expr == error_mark_node)
14014             /* We found an id-expression, but it was something that we
14015                should not have found. This is an error, not something
14016                we can recover from, so note that we found an
14017                id-expression and we'll recover as gracefully as
14018                possible.  */
14019             id_expression_or_member_access_p = true;
14020         }
14021
14022       if (expr 
14023           && expr != error_mark_node
14024           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14025         /* We have an id-expression.  */
14026         id_expression_or_member_access_p = true;
14027     }
14028
14029   if (!id_expression_or_member_access_p)
14030     {
14031       /* Abort the id-expression parse.  */
14032       cp_parser_abort_tentative_parse (parser);
14033
14034       /* Parsing tentatively, again.  */
14035       cp_parser_parse_tentatively (parser);
14036
14037       /* Parse a class member access.  */
14038       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14039                                            /*cast_p=*/false, /*decltype*/true,
14040                                            /*member_access_only_p=*/true, NULL);
14041
14042       if (expr 
14043           && expr != error_mark_node
14044           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14045         /* We have an id-expression.  */
14046         id_expression_or_member_access_p = true;
14047     }
14048
14049   if (id_expression_or_member_access_p)
14050     /* We have parsed the complete id-expression or member access.  */
14051     cp_parser_parse_definitely (parser);
14052   else
14053     {
14054       /* Abort our attempt to parse an id-expression or member access
14055          expression.  */
14056       cp_parser_abort_tentative_parse (parser);
14057
14058       /* Commit to the tentative_firewall so we get syntax errors.  */
14059       cp_parser_commit_to_tentative_parse (parser);
14060
14061       /* Parse a full expression.  */
14062       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14063                                    /*decltype_p=*/true);
14064     }
14065
14066   return expr;
14067 }
14068
14069 /* Parse a `decltype' type. Returns the type.
14070
14071    simple-type-specifier:
14072      decltype ( expression )
14073    C++14 proposal:
14074      decltype ( auto )  */
14075
14076 static tree
14077 cp_parser_decltype (cp_parser *parser)
14078 {
14079   bool id_expression_or_member_access_p = false;
14080   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14081
14082   if (start_token->type == CPP_DECLTYPE)
14083     {
14084       /* Already parsed.  */
14085       cp_lexer_consume_token (parser->lexer);
14086       return saved_checks_value (start_token->u.tree_check_value);
14087     }
14088
14089   /* Look for the `decltype' token.  */
14090   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14091     return error_mark_node;
14092
14093   /* Parse the opening `('.  */
14094   matching_parens parens;
14095   if (!parens.require_open (parser))
14096     return error_mark_node;
14097
14098   push_deferring_access_checks (dk_deferred);
14099
14100   tree expr = NULL_TREE;
14101   
14102   if (cxx_dialect >= cxx14
14103       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14104     /* decltype (auto) */
14105     cp_lexer_consume_token (parser->lexer);
14106   else
14107     {
14108       /* decltype (expression)  */
14109
14110       /* Types cannot be defined in a `decltype' expression.  Save away the
14111          old message and set the new one.  */
14112       const char *saved_message = parser->type_definition_forbidden_message;
14113       parser->type_definition_forbidden_message
14114         = G_("types may not be defined in %<decltype%> expressions");
14115
14116       /* The restrictions on constant-expressions do not apply inside
14117          decltype expressions.  */
14118       bool saved_integral_constant_expression_p
14119         = parser->integral_constant_expression_p;
14120       bool saved_non_integral_constant_expression_p
14121         = parser->non_integral_constant_expression_p;
14122       parser->integral_constant_expression_p = false;
14123
14124       /* Within a parenthesized expression, a `>' token is always
14125          the greater-than operator.  */
14126       bool saved_greater_than_is_operator_p
14127         = parser->greater_than_is_operator_p;
14128       parser->greater_than_is_operator_p = true;
14129
14130       /* Do not actually evaluate the expression.  */
14131       ++cp_unevaluated_operand;
14132
14133       /* Do not warn about problems with the expression.  */
14134       ++c_inhibit_evaluation_warnings;
14135
14136       expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14137
14138       /* Go back to evaluating expressions.  */
14139       --cp_unevaluated_operand;
14140       --c_inhibit_evaluation_warnings;
14141
14142       /* The `>' token might be the end of a template-id or
14143          template-parameter-list now.  */
14144       parser->greater_than_is_operator_p
14145         = saved_greater_than_is_operator_p;
14146
14147       /* Restore the old message and the integral constant expression
14148          flags.  */
14149       parser->type_definition_forbidden_message = saved_message;
14150       parser->integral_constant_expression_p
14151         = saved_integral_constant_expression_p;
14152       parser->non_integral_constant_expression_p
14153         = saved_non_integral_constant_expression_p;
14154     }
14155
14156   /* Parse to the closing `)'.  */
14157   if (!parens.require_close (parser))
14158     {
14159       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14160                                              /*consume_paren=*/true);
14161       pop_deferring_access_checks ();
14162       return error_mark_node;
14163     }
14164
14165   if (!expr)
14166     {
14167       /* Build auto.  */
14168       expr = make_decltype_auto ();
14169       AUTO_IS_DECLTYPE (expr) = true;
14170     }
14171   else
14172     expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14173                                  tf_warning_or_error);
14174
14175   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14176      it again.  */
14177   start_token->type = CPP_DECLTYPE;
14178   start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14179   start_token->u.tree_check_value->value = expr;
14180   start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14181   start_token->keyword = RID_MAX;
14182   cp_lexer_purge_tokens_after (parser->lexer, start_token);
14183
14184   pop_to_parent_deferring_access_checks ();
14185   
14186   return expr;
14187 }
14188
14189 /* Special member functions [gram.special] */
14190
14191 /* Parse a conversion-function-id.
14192
14193    conversion-function-id:
14194      operator conversion-type-id
14195
14196    Returns an IDENTIFIER_NODE representing the operator.  */
14197
14198 static tree
14199 cp_parser_conversion_function_id (cp_parser* parser)
14200 {
14201   tree type;
14202   tree saved_scope;
14203   tree saved_qualifying_scope;
14204   tree saved_object_scope;
14205   tree pushed_scope = NULL_TREE;
14206
14207   /* Look for the `operator' token.  */
14208   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14209     return error_mark_node;
14210   /* When we parse the conversion-type-id, the current scope will be
14211      reset.  However, we need that information in able to look up the
14212      conversion function later, so we save it here.  */
14213   saved_scope = parser->scope;
14214   saved_qualifying_scope = parser->qualifying_scope;
14215   saved_object_scope = parser->object_scope;
14216   /* We must enter the scope of the class so that the names of
14217      entities declared within the class are available in the
14218      conversion-type-id.  For example, consider:
14219
14220        struct S {
14221          typedef int I;
14222          operator I();
14223        };
14224
14225        S::operator I() { ... }
14226
14227      In order to see that `I' is a type-name in the definition, we
14228      must be in the scope of `S'.  */
14229   if (saved_scope)
14230     pushed_scope = push_scope (saved_scope);
14231   /* Parse the conversion-type-id.  */
14232   type = cp_parser_conversion_type_id (parser);
14233   /* Leave the scope of the class, if any.  */
14234   if (pushed_scope)
14235     pop_scope (pushed_scope);
14236   /* Restore the saved scope.  */
14237   parser->scope = saved_scope;
14238   parser->qualifying_scope = saved_qualifying_scope;
14239   parser->object_scope = saved_object_scope;
14240   /* If the TYPE is invalid, indicate failure.  */
14241   if (type == error_mark_node)
14242     return error_mark_node;
14243   return make_conv_op_name (type);
14244 }
14245
14246 /* Parse a conversion-type-id:
14247
14248    conversion-type-id:
14249      type-specifier-seq conversion-declarator [opt]
14250
14251    Returns the TYPE specified.  */
14252
14253 static tree
14254 cp_parser_conversion_type_id (cp_parser* parser)
14255 {
14256   tree attributes;
14257   cp_decl_specifier_seq type_specifiers;
14258   cp_declarator *declarator;
14259   tree type_specified;
14260   const char *saved_message;
14261
14262   /* Parse the attributes.  */
14263   attributes = cp_parser_attributes_opt (parser);
14264
14265   saved_message = parser->type_definition_forbidden_message;
14266   parser->type_definition_forbidden_message
14267     = G_("types may not be defined in a conversion-type-id");
14268
14269   /* Parse the type-specifiers.  */
14270   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14271                                 /*is_trailing_return=*/false,
14272                                 &type_specifiers);
14273
14274   parser->type_definition_forbidden_message = saved_message;
14275
14276   /* If that didn't work, stop.  */
14277   if (type_specifiers.type == error_mark_node)
14278     return error_mark_node;
14279   /* Parse the conversion-declarator.  */
14280   declarator = cp_parser_conversion_declarator_opt (parser);
14281
14282   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
14283                                     /*initialized=*/0, &attributes);
14284   if (attributes)
14285     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14286
14287   /* Don't give this error when parsing tentatively.  This happens to
14288      work because we always parse this definitively once.  */
14289   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14290       && type_uses_auto (type_specified))
14291     {
14292       if (cxx_dialect < cxx14)
14293         {
14294           error ("invalid use of %<auto%> in conversion operator");
14295           return error_mark_node;
14296         }
14297       else if (template_parm_scope_p ())
14298         warning (0, "use of %<auto%> in member template "
14299                  "conversion operator can never be deduced");
14300     }
14301
14302   return type_specified;
14303 }
14304
14305 /* Parse an (optional) conversion-declarator.
14306
14307    conversion-declarator:
14308      ptr-operator conversion-declarator [opt]
14309
14310    */
14311
14312 static cp_declarator *
14313 cp_parser_conversion_declarator_opt (cp_parser* parser)
14314 {
14315   enum tree_code code;
14316   tree class_type, std_attributes = NULL_TREE;
14317   cp_cv_quals cv_quals;
14318
14319   /* We don't know if there's a ptr-operator next, or not.  */
14320   cp_parser_parse_tentatively (parser);
14321   /* Try the ptr-operator.  */
14322   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14323                                  &std_attributes);
14324   /* If it worked, look for more conversion-declarators.  */
14325   if (cp_parser_parse_definitely (parser))
14326     {
14327       cp_declarator *declarator;
14328
14329       /* Parse another optional declarator.  */
14330       declarator = cp_parser_conversion_declarator_opt (parser);
14331
14332       declarator = cp_parser_make_indirect_declarator
14333         (code, class_type, cv_quals, declarator, std_attributes);
14334
14335       return declarator;
14336    }
14337
14338   return NULL;
14339 }
14340
14341 /* Parse an (optional) ctor-initializer.
14342
14343    ctor-initializer:
14344      : mem-initializer-list  */
14345
14346 static void
14347 cp_parser_ctor_initializer_opt (cp_parser* parser)
14348 {
14349   /* If the next token is not a `:', then there is no
14350      ctor-initializer.  */
14351   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14352     {
14353       /* Do default initialization of any bases and members.  */
14354       if (DECL_CONSTRUCTOR_P (current_function_decl))
14355         finish_mem_initializers (NULL_TREE);
14356       return;
14357     }
14358
14359   /* Consume the `:' token.  */
14360   cp_lexer_consume_token (parser->lexer);
14361   /* And the mem-initializer-list.  */
14362   cp_parser_mem_initializer_list (parser);
14363 }
14364
14365 /* Parse a mem-initializer-list.
14366
14367    mem-initializer-list:
14368      mem-initializer ... [opt]
14369      mem-initializer ... [opt] , mem-initializer-list  */
14370
14371 static void
14372 cp_parser_mem_initializer_list (cp_parser* parser)
14373 {
14374   tree mem_initializer_list = NULL_TREE;
14375   tree target_ctor = error_mark_node;
14376   cp_token *token = cp_lexer_peek_token (parser->lexer);
14377
14378   /* Let the semantic analysis code know that we are starting the
14379      mem-initializer-list.  */
14380   if (!DECL_CONSTRUCTOR_P (current_function_decl))
14381     error_at (token->location,
14382               "only constructors take member initializers");
14383
14384   /* Loop through the list.  */
14385   while (true)
14386     {
14387       tree mem_initializer;
14388
14389       token = cp_lexer_peek_token (parser->lexer);
14390       /* Parse the mem-initializer.  */
14391       mem_initializer = cp_parser_mem_initializer (parser);
14392       /* If the next token is a `...', we're expanding member initializers. */
14393       bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14394       if (ellipsis
14395           || (mem_initializer != error_mark_node
14396               && check_for_bare_parameter_packs (TREE_PURPOSE
14397                                                  (mem_initializer))))
14398         {
14399           /* Consume the `...'. */
14400           if (ellipsis)
14401             cp_lexer_consume_token (parser->lexer);
14402
14403           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14404              can be expanded but members cannot. */
14405           if (mem_initializer != error_mark_node
14406               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14407             {
14408               error_at (token->location,
14409                         "cannot expand initializer for member %qD",
14410                         TREE_PURPOSE (mem_initializer));
14411               mem_initializer = error_mark_node;
14412             }
14413
14414           /* Construct the pack expansion type. */
14415           if (mem_initializer != error_mark_node)
14416             mem_initializer = make_pack_expansion (mem_initializer);
14417         }
14418       if (target_ctor != error_mark_node
14419           && mem_initializer != error_mark_node)
14420         {
14421           error ("mem-initializer for %qD follows constructor delegation",
14422                  TREE_PURPOSE (mem_initializer));
14423           mem_initializer = error_mark_node;
14424         }
14425       /* Look for a target constructor. */
14426       if (mem_initializer != error_mark_node
14427           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14428           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14429         {
14430           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14431           if (mem_initializer_list)
14432             {
14433               error ("constructor delegation follows mem-initializer for %qD",
14434                      TREE_PURPOSE (mem_initializer_list));
14435               mem_initializer = error_mark_node;
14436             }
14437           target_ctor = mem_initializer;
14438         }
14439       /* Add it to the list, unless it was erroneous.  */
14440       if (mem_initializer != error_mark_node)
14441         {
14442           TREE_CHAIN (mem_initializer) = mem_initializer_list;
14443           mem_initializer_list = mem_initializer;
14444         }
14445       /* If the next token is not a `,', we're done.  */
14446       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14447         break;
14448       /* Consume the `,' token.  */
14449       cp_lexer_consume_token (parser->lexer);
14450     }
14451
14452   /* Perform semantic analysis.  */
14453   if (DECL_CONSTRUCTOR_P (current_function_decl))
14454     finish_mem_initializers (mem_initializer_list);
14455 }
14456
14457 /* Parse a mem-initializer.
14458
14459    mem-initializer:
14460      mem-initializer-id ( expression-list [opt] )
14461      mem-initializer-id braced-init-list
14462
14463    GNU extension:
14464
14465    mem-initializer:
14466      ( expression-list [opt] )
14467
14468    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
14469    class) or FIELD_DECL (for a non-static data member) to initialize;
14470    the TREE_VALUE is the expression-list.  An empty initialization
14471    list is represented by void_list_node.  */
14472
14473 static tree
14474 cp_parser_mem_initializer (cp_parser* parser)
14475 {
14476   tree mem_initializer_id;
14477   tree expression_list;
14478   tree member;
14479   cp_token *token = cp_lexer_peek_token (parser->lexer);
14480
14481   /* Find out what is being initialized.  */
14482   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14483     {
14484       permerror (token->location,
14485                  "anachronistic old-style base class initializer");
14486       mem_initializer_id = NULL_TREE;
14487     }
14488   else
14489     {
14490       mem_initializer_id = cp_parser_mem_initializer_id (parser);
14491       if (mem_initializer_id == error_mark_node)
14492         return mem_initializer_id;
14493     }
14494   member = expand_member_init (mem_initializer_id);
14495   if (member && !DECL_P (member))
14496     in_base_initializer = 1;
14497
14498   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14499     {
14500       bool expr_non_constant_p;
14501       cp_lexer_set_source_position (parser->lexer);
14502       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14503       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14504       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14505       expression_list = build_tree_list (NULL_TREE, expression_list);
14506     }
14507   else
14508     {
14509       vec<tree, va_gc> *vec;
14510       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14511                                                      /*cast_p=*/false,
14512                                                      /*allow_expansion_p=*/true,
14513                                                      /*non_constant_p=*/NULL);
14514       if (vec == NULL)
14515         return error_mark_node;
14516       expression_list = build_tree_list_vec (vec);
14517       release_tree_vector (vec);
14518     }
14519
14520   if (expression_list == error_mark_node)
14521     return error_mark_node;
14522   if (!expression_list)
14523     expression_list = void_type_node;
14524
14525   in_base_initializer = 0;
14526
14527   return member ? build_tree_list (member, expression_list) : error_mark_node;
14528 }
14529
14530 /* Parse a mem-initializer-id.
14531
14532    mem-initializer-id:
14533      :: [opt] nested-name-specifier [opt] class-name
14534      decltype-specifier (C++11)
14535      identifier
14536
14537    Returns a TYPE indicating the class to be initialized for the first
14538    production (and the second in C++11).  Returns an IDENTIFIER_NODE
14539    indicating the data member to be initialized for the last production.  */
14540
14541 static tree
14542 cp_parser_mem_initializer_id (cp_parser* parser)
14543 {
14544   bool global_scope_p;
14545   bool nested_name_specifier_p;
14546   bool template_p = false;
14547   tree id;
14548
14549   cp_token *token = cp_lexer_peek_token (parser->lexer);
14550
14551   /* `typename' is not allowed in this context ([temp.res]).  */
14552   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14553     {
14554       error_at (token->location, 
14555                 "keyword %<typename%> not allowed in this context (a qualified "
14556                 "member initializer is implicitly a type)");
14557       cp_lexer_consume_token (parser->lexer);
14558     }
14559   /* Look for the optional `::' operator.  */
14560   global_scope_p
14561     = (cp_parser_global_scope_opt (parser,
14562                                    /*current_scope_valid_p=*/false)
14563        != NULL_TREE);
14564   /* Look for the optional nested-name-specifier.  The simplest way to
14565      implement:
14566
14567        [temp.res]
14568
14569        The keyword `typename' is not permitted in a base-specifier or
14570        mem-initializer; in these contexts a qualified name that
14571        depends on a template-parameter is implicitly assumed to be a
14572        type name.
14573
14574      is to assume that we have seen the `typename' keyword at this
14575      point.  */
14576   nested_name_specifier_p
14577     = (cp_parser_nested_name_specifier_opt (parser,
14578                                             /*typename_keyword_p=*/true,
14579                                             /*check_dependency_p=*/true,
14580                                             /*type_p=*/true,
14581                                             /*is_declaration=*/true)
14582        != NULL_TREE);
14583   if (nested_name_specifier_p)
14584     template_p = cp_parser_optional_template_keyword (parser);
14585   /* If there is a `::' operator or a nested-name-specifier, then we
14586      are definitely looking for a class-name.  */
14587   if (global_scope_p || nested_name_specifier_p)
14588     return cp_parser_class_name (parser,
14589                                  /*typename_keyword_p=*/true,
14590                                  /*template_keyword_p=*/template_p,
14591                                  typename_type,
14592                                  /*check_dependency_p=*/true,
14593                                  /*class_head_p=*/false,
14594                                  /*is_declaration=*/true);
14595   /* Otherwise, we could also be looking for an ordinary identifier.  */
14596   cp_parser_parse_tentatively (parser);
14597   if (cp_lexer_next_token_is_decltype (parser->lexer))
14598     /* Try a decltype-specifier.  */
14599     id = cp_parser_decltype (parser);
14600   else
14601     /* Otherwise, try a class-name.  */
14602     id = cp_parser_class_name (parser,
14603                                /*typename_keyword_p=*/true,
14604                                /*template_keyword_p=*/false,
14605                                none_type,
14606                                /*check_dependency_p=*/true,
14607                                /*class_head_p=*/false,
14608                                /*is_declaration=*/true);
14609   /* If we found one, we're done.  */
14610   if (cp_parser_parse_definitely (parser))
14611     return id;
14612   /* Otherwise, look for an ordinary identifier.  */
14613   return cp_parser_identifier (parser);
14614 }
14615
14616 /* Overloading [gram.over] */
14617
14618 /* Parse an operator-function-id.
14619
14620    operator-function-id:
14621      operator operator
14622
14623    Returns an IDENTIFIER_NODE for the operator which is a
14624    human-readable spelling of the identifier, e.g., `operator +'.  */
14625
14626 static cp_expr
14627 cp_parser_operator_function_id (cp_parser* parser)
14628 {
14629   /* Look for the `operator' keyword.  */
14630   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14631     return error_mark_node;
14632   /* And then the name of the operator itself.  */
14633   return cp_parser_operator (parser);
14634 }
14635
14636 /* Return an identifier node for a user-defined literal operator.
14637    The suffix identifier is chained to the operator name identifier.  */
14638
14639 tree
14640 cp_literal_operator_id (const char* name)
14641 {
14642   tree identifier;
14643   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14644                               + strlen (name) + 10);
14645   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14646   identifier = get_identifier (buffer);
14647
14648   return identifier;
14649 }
14650
14651 /* Parse an operator.
14652
14653    operator:
14654      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14655      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14656      || ++ -- , ->* -> () []
14657
14658    GNU Extensions:
14659
14660    operator:
14661      <? >? <?= >?=
14662
14663    Returns an IDENTIFIER_NODE for the operator which is a
14664    human-readable spelling of the identifier, e.g., `operator +'.  */
14665
14666 static cp_expr
14667 cp_parser_operator (cp_parser* parser)
14668 {
14669   tree id = NULL_TREE;
14670   cp_token *token;
14671   bool utf8 = false;
14672
14673   /* Peek at the next token.  */
14674   token = cp_lexer_peek_token (parser->lexer);
14675
14676   location_t start_loc = token->location;
14677
14678   /* Figure out which operator we have.  */
14679   enum tree_code op = ERROR_MARK;
14680   bool assop = false;
14681   bool consumed = false;
14682   switch (token->type)
14683     {
14684     case CPP_KEYWORD:
14685       {
14686         /* The keyword should be either `new' or `delete'.  */
14687         if (token->keyword == RID_NEW)
14688           op = NEW_EXPR;
14689         else if (token->keyword == RID_DELETE)
14690           op = DELETE_EXPR;
14691         else
14692           break;
14693
14694         /* Consume the `new' or `delete' token.  */
14695         location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14696
14697         /* Peek at the next token.  */
14698         token = cp_lexer_peek_token (parser->lexer);
14699         /* If it's a `[' token then this is the array variant of the
14700            operator.  */
14701         if (token->type == CPP_OPEN_SQUARE)
14702           {
14703             /* Consume the `[' token.  */
14704             cp_lexer_consume_token (parser->lexer);
14705             /* Look for the `]' token.  */
14706             if (cp_token *close_token
14707                 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14708               end_loc = close_token->location;
14709             op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14710           }
14711         start_loc = make_location (start_loc, start_loc, end_loc);
14712         consumed = true;
14713         break;
14714       }
14715
14716     case CPP_PLUS:
14717       op = PLUS_EXPR;
14718       break;
14719
14720     case CPP_MINUS:
14721       op = MINUS_EXPR;
14722       break;
14723
14724     case CPP_MULT:
14725       op = MULT_EXPR;
14726       break;
14727
14728     case CPP_DIV:
14729       op = TRUNC_DIV_EXPR;
14730       break;
14731
14732     case CPP_MOD:
14733       op = TRUNC_MOD_EXPR;
14734       break;
14735
14736     case CPP_XOR:
14737       op = BIT_XOR_EXPR;
14738       break;
14739
14740     case CPP_AND:
14741       op = BIT_AND_EXPR;
14742       break;
14743
14744     case CPP_OR:
14745       op = BIT_IOR_EXPR;
14746       break;
14747
14748     case CPP_COMPL:
14749       op = BIT_NOT_EXPR;
14750       break;
14751
14752     case CPP_NOT:
14753       op = TRUTH_NOT_EXPR;
14754       break;
14755
14756     case CPP_EQ:
14757       assop = true;
14758       op = NOP_EXPR;
14759       break;
14760
14761     case CPP_LESS:
14762       op = LT_EXPR;
14763       break;
14764
14765     case CPP_GREATER:
14766       op = GT_EXPR;
14767       break;
14768
14769     case CPP_PLUS_EQ:
14770       assop = true;
14771       op = PLUS_EXPR;
14772       break;
14773
14774     case CPP_MINUS_EQ:
14775       assop = true;
14776       op = MINUS_EXPR;
14777       break;
14778
14779     case CPP_MULT_EQ:
14780       assop = true;
14781       op = MULT_EXPR;
14782       break;
14783
14784     case CPP_DIV_EQ:
14785       assop = true;
14786       op = TRUNC_DIV_EXPR;
14787       break;
14788
14789     case CPP_MOD_EQ:
14790       assop = true;
14791       op = TRUNC_MOD_EXPR;
14792       break;
14793
14794     case CPP_XOR_EQ:
14795       assop = true;
14796       op = BIT_XOR_EXPR;
14797       break;
14798
14799     case CPP_AND_EQ:
14800       assop = true;
14801       op = BIT_AND_EXPR;
14802       break;
14803
14804     case CPP_OR_EQ:
14805       assop = true;
14806       op = BIT_IOR_EXPR;
14807       break;
14808
14809     case CPP_LSHIFT:
14810       op = LSHIFT_EXPR;
14811       break;
14812
14813     case CPP_RSHIFT:
14814       op = RSHIFT_EXPR;
14815       break;
14816
14817     case CPP_LSHIFT_EQ:
14818       assop = true;
14819       op = LSHIFT_EXPR;
14820       break;
14821
14822     case CPP_RSHIFT_EQ:
14823       assop = true;
14824       op = RSHIFT_EXPR;
14825       break;
14826
14827     case CPP_EQ_EQ:
14828       op = EQ_EXPR;
14829       break;
14830
14831     case CPP_NOT_EQ:
14832       op = NE_EXPR;
14833       break;
14834
14835     case CPP_LESS_EQ:
14836       op = LE_EXPR;
14837       break;
14838
14839     case CPP_GREATER_EQ:
14840       op = GE_EXPR;
14841       break;
14842
14843     case CPP_AND_AND:
14844       op = TRUTH_ANDIF_EXPR;
14845       break;
14846
14847     case CPP_OR_OR:
14848       op = TRUTH_ORIF_EXPR;
14849       break;
14850
14851     case CPP_PLUS_PLUS:
14852       op = POSTINCREMENT_EXPR;
14853       break;
14854
14855     case CPP_MINUS_MINUS:
14856       op = PREDECREMENT_EXPR;
14857       break;
14858
14859     case CPP_COMMA:
14860       op = COMPOUND_EXPR;
14861       break;
14862
14863     case CPP_DEREF_STAR:
14864       op = MEMBER_REF;
14865       break;
14866
14867     case CPP_DEREF:
14868       op = COMPONENT_REF;
14869       break;
14870
14871     case CPP_OPEN_PAREN:
14872       {
14873         /* Consume the `('.  */
14874         matching_parens parens;
14875         parens.consume_open (parser);
14876         /* Look for the matching `)'.  */
14877         parens.require_close (parser);
14878         op = CALL_EXPR;
14879         consumed = true;
14880         break;
14881       }
14882
14883     case CPP_OPEN_SQUARE:
14884       /* Consume the `['.  */
14885       cp_lexer_consume_token (parser->lexer);
14886       /* Look for the matching `]'.  */
14887       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14888       op = ARRAY_REF;
14889       consumed = true;
14890       break;
14891
14892     case CPP_UTF8STRING:
14893     case CPP_UTF8STRING_USERDEF:
14894       utf8 = true;
14895       /* FALLTHRU */
14896     case CPP_STRING:
14897     case CPP_WSTRING:
14898     case CPP_STRING16:
14899     case CPP_STRING32:
14900     case CPP_STRING_USERDEF:
14901     case CPP_WSTRING_USERDEF:
14902     case CPP_STRING16_USERDEF:
14903     case CPP_STRING32_USERDEF:
14904       {
14905         tree str, string_tree;
14906         int sz, len;
14907
14908         if (cxx_dialect == cxx98)
14909           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14910
14911         /* Consume the string.  */
14912         str = cp_parser_string_literal (parser, /*translate=*/true,
14913                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
14914         if (str == error_mark_node)
14915           return error_mark_node;
14916         else if (TREE_CODE (str) == USERDEF_LITERAL)
14917           {
14918             string_tree = USERDEF_LITERAL_VALUE (str);
14919             id = USERDEF_LITERAL_SUFFIX_ID (str);
14920           }
14921         else
14922           {
14923             string_tree = str;
14924             /* Look for the suffix identifier.  */
14925             token = cp_lexer_peek_token (parser->lexer);
14926             if (token->type == CPP_NAME)
14927               id = cp_parser_identifier (parser);
14928             else if (token->type == CPP_KEYWORD)
14929               {
14930                 error ("unexpected keyword;"
14931                        " remove space between quotes and suffix identifier");
14932                 return error_mark_node;
14933               }
14934             else
14935               {
14936                 error ("expected suffix identifier");
14937                 return error_mark_node;
14938               }
14939           }
14940         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14941                                (TREE_TYPE (TREE_TYPE (string_tree))));
14942         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14943         if (len != 0)
14944           {
14945             error ("expected empty string after %<operator%> keyword");
14946             return error_mark_node;
14947           }
14948         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14949             != char_type_node)
14950           {
14951             error ("invalid encoding prefix in literal operator");
14952             return error_mark_node;
14953           }
14954         if (id != error_mark_node)
14955           {
14956             const char *name = IDENTIFIER_POINTER (id);
14957             id = cp_literal_operator_id (name);
14958           }
14959         return id;
14960       }
14961
14962     default:
14963       /* Anything else is an error.  */
14964       break;
14965     }
14966
14967   /* If we have selected an identifier, we need to consume the
14968      operator token.  */
14969   if (op != ERROR_MARK)
14970     {
14971       id = ovl_op_identifier (assop, op);
14972       if (!consumed)
14973         cp_lexer_consume_token (parser->lexer);
14974     }
14975   /* Otherwise, no valid operator name was present.  */
14976   else
14977     {
14978       cp_parser_error (parser, "expected operator");
14979       id = error_mark_node;
14980     }
14981
14982   return cp_expr (id, start_loc);
14983 }
14984
14985 /* Parse a template-declaration.
14986
14987    template-declaration:
14988      export [opt] template < template-parameter-list > declaration
14989
14990    If MEMBER_P is TRUE, this template-declaration occurs within a
14991    class-specifier.
14992
14993    The grammar rule given by the standard isn't correct.  What
14994    is really meant is:
14995
14996    template-declaration:
14997      export [opt] template-parameter-list-seq
14998        decl-specifier-seq [opt] init-declarator [opt] ;
14999      export [opt] template-parameter-list-seq
15000        function-definition
15001
15002    template-parameter-list-seq:
15003      template-parameter-list-seq [opt]
15004      template < template-parameter-list >
15005
15006    Concept Extensions:
15007
15008    template-parameter-list-seq:
15009      template < template-parameter-list > requires-clause [opt]
15010
15011    requires-clause:
15012      requires logical-or-expression  */
15013
15014 static void
15015 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15016 {
15017   /* Check for `export'.  */
15018   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15019     {
15020       /* Consume the `export' token.  */
15021       cp_lexer_consume_token (parser->lexer);
15022       /* Warn that we do not support `export'.  */
15023       warning (0, "keyword %<export%> not implemented, and will be ignored");
15024     }
15025
15026   cp_parser_template_declaration_after_export (parser, member_p);
15027 }
15028
15029 /* Parse a template-parameter-list.
15030
15031    template-parameter-list:
15032      template-parameter
15033      template-parameter-list , template-parameter
15034
15035    Returns a TREE_LIST.  Each node represents a template parameter.
15036    The nodes are connected via their TREE_CHAINs.  */
15037
15038 static tree
15039 cp_parser_template_parameter_list (cp_parser* parser)
15040 {
15041   tree parameter_list = NULL_TREE;
15042
15043   begin_template_parm_list ();
15044
15045   /* The loop below parses the template parms.  We first need to know
15046      the total number of template parms to be able to compute proper
15047      canonical types of each dependent type. So after the loop, when
15048      we know the total number of template parms,
15049      end_template_parm_list computes the proper canonical types and
15050      fixes up the dependent types accordingly.  */
15051   while (true)
15052     {
15053       tree parameter;
15054       bool is_non_type;
15055       bool is_parameter_pack;
15056       location_t parm_loc;
15057
15058       /* Parse the template-parameter.  */
15059       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15060       parameter = cp_parser_template_parameter (parser, 
15061                                                 &is_non_type,
15062                                                 &is_parameter_pack);
15063       /* Add it to the list.  */
15064       if (parameter != error_mark_node)
15065         parameter_list = process_template_parm (parameter_list,
15066                                                 parm_loc,
15067                                                 parameter,
15068                                                 is_non_type,
15069                                                 is_parameter_pack);
15070       else
15071        {
15072          tree err_parm = build_tree_list (parameter, parameter);
15073          parameter_list = chainon (parameter_list, err_parm);
15074        }
15075
15076       /* If the next token is not a `,', we're done.  */
15077       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15078         break;
15079       /* Otherwise, consume the `,' token.  */
15080       cp_lexer_consume_token (parser->lexer);
15081     }
15082
15083   return end_template_parm_list (parameter_list);
15084 }
15085
15086 /* Parse a introduction-list.
15087
15088    introduction-list:
15089      introduced-parameter
15090      introduction-list , introduced-parameter
15091
15092    introduced-parameter:
15093      ...[opt] identifier
15094
15095    Returns a TREE_VEC of WILDCARD_DECLs.  If the parameter is a pack
15096    then the introduced parm will have WILDCARD_PACK_P set.  In addition, the
15097    WILDCARD_DECL will also have DECL_NAME set and token location in
15098    DECL_SOURCE_LOCATION.  */
15099
15100 static tree
15101 cp_parser_introduction_list (cp_parser *parser)
15102 {
15103   vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15104
15105   while (true)
15106     {
15107       bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15108       if (is_pack)
15109         cp_lexer_consume_token (parser->lexer);
15110
15111       /* Build placeholder. */
15112       tree parm = build_nt (WILDCARD_DECL);
15113       DECL_SOURCE_LOCATION (parm)
15114         = cp_lexer_peek_token (parser->lexer)->location;
15115       DECL_NAME (parm) = cp_parser_identifier (parser);
15116       WILDCARD_PACK_P (parm) = is_pack;
15117       vec_safe_push (introduction_vec, parm);
15118
15119       /* If the next token is not a `,', we're done.  */
15120       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15121         break;
15122       /* Otherwise, consume the `,' token.  */
15123       cp_lexer_consume_token (parser->lexer);
15124     }
15125
15126   /* Convert the vec into a TREE_VEC.  */
15127   tree introduction_list = make_tree_vec (introduction_vec->length ());
15128   unsigned int n;
15129   tree parm;
15130   FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15131     TREE_VEC_ELT (introduction_list, n) = parm;
15132
15133   release_tree_vector (introduction_vec);
15134   return introduction_list;
15135 }
15136
15137 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15138    is an abstract declarator. */
15139
15140 static inline cp_declarator*
15141 get_id_declarator (cp_declarator *declarator)
15142 {
15143   cp_declarator *d = declarator;
15144   while (d && d->kind != cdk_id)
15145     d = d->declarator;
15146   return d;
15147 }
15148
15149 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15150    is an abstract declarator. */
15151
15152 static inline tree
15153 get_unqualified_id (cp_declarator *declarator)
15154 {
15155   declarator = get_id_declarator (declarator);
15156   if (declarator)
15157     return declarator->u.id.unqualified_name;
15158   else
15159     return NULL_TREE;
15160 }
15161
15162 /* Returns true if DECL represents a constrained-parameter.  */
15163
15164 static inline bool
15165 is_constrained_parameter (tree decl)
15166 {
15167   return (decl
15168           && TREE_CODE (decl) == TYPE_DECL
15169           && CONSTRAINED_PARM_CONCEPT (decl)
15170           && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15171 }
15172
15173 /* Returns true if PARM declares a constrained-parameter. */
15174
15175 static inline bool
15176 is_constrained_parameter (cp_parameter_declarator *parm)
15177 {
15178   return is_constrained_parameter (parm->decl_specifiers.type);
15179 }
15180
15181 /* Check that the type parameter is only a declarator-id, and that its
15182    type is not cv-qualified. */
15183
15184 bool
15185 cp_parser_check_constrained_type_parm (cp_parser *parser,
15186                                        cp_parameter_declarator *parm)
15187 {
15188   if (!parm->declarator)
15189     return true;
15190
15191   if (parm->declarator->kind != cdk_id)
15192     {
15193       cp_parser_error (parser, "invalid constrained type parameter");
15194       return false;
15195     }
15196
15197   /* Don't allow cv-qualified type parameters.  */
15198   if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15199       || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15200     {
15201       cp_parser_error (parser, "cv-qualified type parameter");
15202       return false;
15203     }
15204
15205   return true;
15206 }
15207
15208 /* Finish parsing/processing a template type parameter and checking
15209    various restrictions. */
15210
15211 static inline tree
15212 cp_parser_constrained_type_template_parm (cp_parser *parser,
15213                                           tree id,
15214                                           cp_parameter_declarator* parmdecl)
15215 {
15216   if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15217     return finish_template_type_parm (class_type_node, id);
15218   else
15219     return error_mark_node;
15220 }
15221
15222 static tree
15223 finish_constrained_template_template_parm (tree proto, tree id)
15224 {
15225   /* FIXME: This should probably be copied, and we may need to adjust
15226      the template parameter depths.  */
15227   tree saved_parms = current_template_parms;
15228   begin_template_parm_list ();
15229   current_template_parms = DECL_TEMPLATE_PARMS (proto);
15230   end_template_parm_list ();
15231
15232   tree parm = finish_template_template_parm (class_type_node, id);
15233   current_template_parms = saved_parms;
15234
15235   return parm;
15236 }
15237
15238 /* Finish parsing/processing a template template parameter by borrowing
15239    the template parameter list from the prototype parameter.  */
15240
15241 static tree
15242 cp_parser_constrained_template_template_parm (cp_parser *parser,
15243                                               tree proto,
15244                                               tree id,
15245                                               cp_parameter_declarator *parmdecl)
15246 {
15247   if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15248     return error_mark_node;
15249   return finish_constrained_template_template_parm (proto, id);
15250 }
15251
15252 /* Create a new non-type template parameter from the given PARM
15253    declarator.  */
15254
15255 static tree
15256 constrained_non_type_template_parm (bool *is_non_type,
15257                                     cp_parameter_declarator *parm)
15258 {
15259   *is_non_type = true;
15260   cp_declarator *decl = parm->declarator;
15261   cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15262   specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15263   return grokdeclarator (decl, specs, TPARM, 0, NULL);
15264 }
15265
15266 /* Build a constrained template parameter based on the PARMDECL
15267    declarator. The type of PARMDECL is the constrained type, which
15268    refers to the prototype template parameter that ultimately
15269    specifies the type of the declared parameter. */
15270
15271 static tree
15272 finish_constrained_parameter (cp_parser *parser,
15273                               cp_parameter_declarator *parmdecl,
15274                               bool *is_non_type,
15275                               bool *is_parameter_pack)
15276 {
15277   tree decl = parmdecl->decl_specifiers.type;
15278   tree id = get_unqualified_id (parmdecl->declarator);
15279   tree def = parmdecl->default_argument;
15280   tree proto = DECL_INITIAL (decl);
15281
15282   /* A template parameter constrained by a variadic concept shall also
15283      be declared as a template parameter pack.  */
15284   bool is_variadic = template_parameter_pack_p (proto);
15285   if (is_variadic && !*is_parameter_pack)
15286     cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15287
15288   /* Build the parameter. Return an error if the declarator was invalid. */
15289   tree parm;
15290   if (TREE_CODE (proto) == TYPE_DECL)
15291     parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15292   else if (TREE_CODE (proto) == TEMPLATE_DECL)
15293     parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15294                                                          parmdecl);
15295   else
15296     parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15297   if (parm == error_mark_node)
15298     return error_mark_node;
15299
15300   /* Finish the parameter decl and create a node attaching the
15301      default argument and constraint.  */
15302   parm = build_tree_list (def, parm);
15303   TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15304
15305   return parm;
15306 }
15307
15308 /* Returns true if the parsed type actually represents the declaration
15309    of a type template-parameter.  */
15310
15311 static inline bool
15312 declares_constrained_type_template_parameter (tree type)
15313 {
15314   return (is_constrained_parameter (type)
15315           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15316 }
15317
15318
15319 /* Returns true if the parsed type actually represents the declaration of
15320    a template template-parameter.  */
15321
15322 static bool
15323 declares_constrained_template_template_parameter (tree type)
15324 {
15325   return (is_constrained_parameter (type)
15326           && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15327 }
15328
15329 /* Parse a default argument for a type template-parameter.
15330    Note that diagnostics are handled in cp_parser_template_parameter.  */
15331
15332 static tree
15333 cp_parser_default_type_template_argument (cp_parser *parser)
15334 {
15335   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15336
15337   /* Consume the `=' token.  */
15338   cp_lexer_consume_token (parser->lexer);
15339
15340   cp_token *token = cp_lexer_peek_token (parser->lexer);
15341
15342   /* Parse the default-argument.  */
15343   push_deferring_access_checks (dk_no_deferred);
15344   tree default_argument = cp_parser_type_id (parser);
15345   pop_deferring_access_checks ();
15346
15347   if (flag_concepts && type_uses_auto (default_argument))
15348     {
15349       error_at (token->location,
15350                 "invalid use of %<auto%> in default template argument");
15351       return error_mark_node;
15352     }
15353
15354   return default_argument;
15355 }
15356
15357 /* Parse a default argument for a template template-parameter.  */
15358
15359 static tree
15360 cp_parser_default_template_template_argument (cp_parser *parser)
15361 {
15362   gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15363
15364   bool is_template;
15365
15366   /* Consume the `='.  */
15367   cp_lexer_consume_token (parser->lexer);
15368   /* Parse the id-expression.  */
15369   push_deferring_access_checks (dk_no_deferred);
15370   /* save token before parsing the id-expression, for error
15371      reporting */
15372   const cp_token* token = cp_lexer_peek_token (parser->lexer);
15373   tree default_argument
15374     = cp_parser_id_expression (parser,
15375                                /*template_keyword_p=*/false,
15376                                /*check_dependency_p=*/true,
15377                                /*template_p=*/&is_template,
15378                                /*declarator_p=*/false,
15379                                /*optional_p=*/false);
15380   if (TREE_CODE (default_argument) == TYPE_DECL)
15381     /* If the id-expression was a template-id that refers to
15382        a template-class, we already have the declaration here,
15383        so no further lookup is needed.  */
15384     ;
15385   else
15386     /* Look up the name.  */
15387     default_argument
15388       = cp_parser_lookup_name (parser, default_argument,
15389                                none_type,
15390                                /*is_template=*/is_template,
15391                                /*is_namespace=*/false,
15392                                /*check_dependency=*/true,
15393                                /*ambiguous_decls=*/NULL,
15394                                token->location);
15395   /* See if the default argument is valid.  */
15396   default_argument = check_template_template_default_arg (default_argument);
15397   pop_deferring_access_checks ();
15398   return default_argument;
15399 }
15400
15401 /* Parse a template-parameter.
15402
15403    template-parameter:
15404      type-parameter
15405      parameter-declaration
15406
15407    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
15408    the parameter.  The TREE_PURPOSE is the default value, if any.
15409    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
15410    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
15411    set to true iff this parameter is a parameter pack. */
15412
15413 static tree
15414 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15415                               bool *is_parameter_pack)
15416 {
15417   cp_token *token;
15418   cp_parameter_declarator *parameter_declarator;
15419   tree parm;
15420
15421   /* Assume it is a type parameter or a template parameter.  */
15422   *is_non_type = false;
15423   /* Assume it not a parameter pack. */
15424   *is_parameter_pack = false;
15425   /* Peek at the next token.  */
15426   token = cp_lexer_peek_token (parser->lexer);
15427   /* If it is `template', we have a type-parameter.  */
15428   if (token->keyword == RID_TEMPLATE)
15429     return cp_parser_type_parameter (parser, is_parameter_pack);
15430   /* If it is `class' or `typename' we do not know yet whether it is a
15431      type parameter or a non-type parameter.  Consider:
15432
15433        template <typename T, typename T::X X> ...
15434
15435      or:
15436
15437        template <class C, class D*> ...
15438
15439      Here, the first parameter is a type parameter, and the second is
15440      a non-type parameter.  We can tell by looking at the token after
15441      the identifier -- if it is a `,', `=', or `>' then we have a type
15442      parameter.  */
15443   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15444     {
15445       /* Peek at the token after `class' or `typename'.  */
15446       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15447       /* If it's an ellipsis, we have a template type parameter
15448          pack. */
15449       if (token->type == CPP_ELLIPSIS)
15450         return cp_parser_type_parameter (parser, is_parameter_pack);
15451       /* If it's an identifier, skip it.  */
15452       if (token->type == CPP_NAME)
15453         token = cp_lexer_peek_nth_token (parser->lexer, 3);
15454       /* Now, see if the token looks like the end of a template
15455          parameter.  */
15456       if (token->type == CPP_COMMA
15457           || token->type == CPP_EQ
15458           || token->type == CPP_GREATER)
15459         return cp_parser_type_parameter (parser, is_parameter_pack);
15460     }
15461
15462   /* Otherwise, it is a non-type parameter or a constrained parameter.
15463
15464      [temp.param]
15465
15466      When parsing a default template-argument for a non-type
15467      template-parameter, the first non-nested `>' is taken as the end
15468      of the template parameter-list rather than a greater-than
15469      operator.  */
15470   parameter_declarator
15471      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15472                                         /*parenthesized_p=*/NULL);
15473
15474   if (!parameter_declarator)
15475     return error_mark_node;
15476
15477   /* If the parameter declaration is marked as a parameter pack, set
15478    *IS_PARAMETER_PACK to notify the caller.  */
15479   if (parameter_declarator->template_parameter_pack_p)
15480     *is_parameter_pack = true;
15481
15482   if (parameter_declarator->default_argument)
15483     {
15484       /* Can happen in some cases of erroneous input (c++/34892).  */
15485       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15486         /* Consume the `...' for better error recovery.  */
15487         cp_lexer_consume_token (parser->lexer);
15488     }
15489
15490   // The parameter may have been constrained.
15491   if (is_constrained_parameter (parameter_declarator))
15492     return finish_constrained_parameter (parser,
15493                                          parameter_declarator,
15494                                          is_non_type,
15495                                          is_parameter_pack);
15496
15497   // Now we're sure that the parameter is a non-type parameter.
15498   *is_non_type = true;
15499
15500   parm = grokdeclarator (parameter_declarator->declarator,
15501                          &parameter_declarator->decl_specifiers,
15502                          TPARM, /*initialized=*/0,
15503                          /*attrlist=*/NULL);
15504   if (parm == error_mark_node)
15505     return error_mark_node;
15506
15507   return build_tree_list (parameter_declarator->default_argument, parm);
15508 }
15509
15510 /* Parse a type-parameter.
15511
15512    type-parameter:
15513      class identifier [opt]
15514      class identifier [opt] = type-id
15515      typename identifier [opt]
15516      typename identifier [opt] = type-id
15517      template < template-parameter-list > class identifier [opt]
15518      template < template-parameter-list > class identifier [opt]
15519        = id-expression
15520
15521    GNU Extension (variadic templates):
15522
15523    type-parameter:
15524      class ... identifier [opt]
15525      typename ... identifier [opt]
15526
15527    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
15528    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
15529    the declaration of the parameter.
15530
15531    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15532
15533 static tree
15534 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15535 {
15536   cp_token *token;
15537   tree parameter;
15538
15539   /* Look for a keyword to tell us what kind of parameter this is.  */
15540   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15541   if (!token)
15542     return error_mark_node;
15543
15544   switch (token->keyword)
15545     {
15546     case RID_CLASS:
15547     case RID_TYPENAME:
15548       {
15549         tree identifier;
15550         tree default_argument;
15551
15552         /* If the next token is an ellipsis, we have a template
15553            argument pack. */
15554         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15555           {
15556             /* Consume the `...' token. */
15557             cp_lexer_consume_token (parser->lexer);
15558             maybe_warn_variadic_templates ();
15559
15560             *is_parameter_pack = true;
15561           }
15562
15563         /* If the next token is an identifier, then it names the
15564            parameter.  */
15565         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15566           identifier = cp_parser_identifier (parser);
15567         else
15568           identifier = NULL_TREE;
15569
15570         /* Create the parameter.  */
15571         parameter = finish_template_type_parm (class_type_node, identifier);
15572
15573         /* If the next token is an `=', we have a default argument.  */
15574         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15575           {
15576             default_argument
15577               = cp_parser_default_type_template_argument (parser);
15578
15579             /* Template parameter packs cannot have default
15580                arguments. */
15581             if (*is_parameter_pack)
15582               {
15583                 if (identifier)
15584                   error_at (token->location,
15585                             "template parameter pack %qD cannot have a "
15586                             "default argument", identifier);
15587                 else
15588                   error_at (token->location,
15589                             "template parameter packs cannot have "
15590                             "default arguments");
15591                 default_argument = NULL_TREE;
15592               }
15593             else if (check_for_bare_parameter_packs (default_argument))
15594               default_argument = error_mark_node;
15595           }
15596         else
15597           default_argument = NULL_TREE;
15598
15599         /* Create the combined representation of the parameter and the
15600            default argument.  */
15601         parameter = build_tree_list (default_argument, parameter);
15602       }
15603       break;
15604
15605     case RID_TEMPLATE:
15606       {
15607         tree identifier;
15608         tree default_argument;
15609
15610         /* Look for the `<'.  */
15611         cp_parser_require (parser, CPP_LESS, RT_LESS);
15612         /* Parse the template-parameter-list.  */
15613         cp_parser_template_parameter_list (parser);
15614         /* Look for the `>'.  */
15615         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15616
15617         // If template requirements are present, parse them.
15618         if (flag_concepts)
15619           {
15620             tree reqs = get_shorthand_constraints (current_template_parms);
15621             if (tree r = cp_parser_requires_clause_opt (parser))
15622               reqs = conjoin_constraints (reqs, normalize_expression (r));
15623             TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15624           }
15625
15626         /* Look for the `class' or 'typename' keywords.  */
15627         cp_parser_type_parameter_key (parser);
15628         /* If the next token is an ellipsis, we have a template
15629            argument pack. */
15630         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15631           {
15632             /* Consume the `...' token. */
15633             cp_lexer_consume_token (parser->lexer);
15634             maybe_warn_variadic_templates ();
15635
15636             *is_parameter_pack = true;
15637           }
15638         /* If the next token is an `=', then there is a
15639            default-argument.  If the next token is a `>', we are at
15640            the end of the parameter-list.  If the next token is a `,',
15641            then we are at the end of this parameter.  */
15642         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15643             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15644             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15645           {
15646             identifier = cp_parser_identifier (parser);
15647             /* Treat invalid names as if the parameter were nameless.  */
15648             if (identifier == error_mark_node)
15649               identifier = NULL_TREE;
15650           }
15651         else
15652           identifier = NULL_TREE;
15653
15654         /* Create the template parameter.  */
15655         parameter = finish_template_template_parm (class_type_node,
15656                                                    identifier);
15657
15658         /* If the next token is an `=', then there is a
15659            default-argument.  */
15660         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15661           {
15662             default_argument
15663               = cp_parser_default_template_template_argument (parser);
15664
15665             /* Template parameter packs cannot have default
15666                arguments. */
15667             if (*is_parameter_pack)
15668               {
15669                 if (identifier)
15670                   error_at (token->location,
15671                             "template parameter pack %qD cannot "
15672                             "have a default argument",
15673                             identifier);
15674                 else
15675                   error_at (token->location, "template parameter packs cannot "
15676                             "have default arguments");
15677                 default_argument = NULL_TREE;
15678               }
15679           }
15680         else
15681           default_argument = NULL_TREE;
15682
15683         /* Create the combined representation of the parameter and the
15684            default argument.  */
15685         parameter = build_tree_list (default_argument, parameter);
15686       }
15687       break;
15688
15689     default:
15690       gcc_unreachable ();
15691       break;
15692     }
15693
15694   return parameter;
15695 }
15696
15697 /* Parse a template-id.
15698
15699    template-id:
15700      template-name < template-argument-list [opt] >
15701
15702    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15703    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
15704    returned.  Otherwise, if the template-name names a function, or set
15705    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
15706    names a class, returns a TYPE_DECL for the specialization.
15707
15708    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15709    uninstantiated templates.  */
15710
15711 static tree
15712 cp_parser_template_id (cp_parser *parser,
15713                        bool template_keyword_p,
15714                        bool check_dependency_p,
15715                        enum tag_types tag_type,
15716                        bool is_declaration)
15717 {
15718   tree templ;
15719   tree arguments;
15720   tree template_id;
15721   cp_token_position start_of_id = 0;
15722   cp_token *next_token = NULL, *next_token_2 = NULL;
15723   bool is_identifier;
15724
15725   /* If the next token corresponds to a template-id, there is no need
15726      to reparse it.  */
15727   cp_token *token = cp_lexer_peek_token (parser->lexer);
15728   if (token->type == CPP_TEMPLATE_ID)
15729     {
15730       cp_lexer_consume_token (parser->lexer);
15731       return saved_checks_value (token->u.tree_check_value);
15732     }
15733
15734   /* Avoid performing name lookup if there is no possibility of
15735      finding a template-id.  */
15736   if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15737       || (token->type == CPP_NAME
15738           && !cp_parser_nth_token_starts_template_argument_list_p
15739                (parser, 2)))
15740     {
15741       cp_parser_error (parser, "expected template-id");
15742       return error_mark_node;
15743     }
15744
15745   /* Remember where the template-id starts.  */
15746   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15747     start_of_id = cp_lexer_token_position (parser->lexer, false);
15748
15749   push_deferring_access_checks (dk_deferred);
15750
15751   /* Parse the template-name.  */
15752   is_identifier = false;
15753   templ = cp_parser_template_name (parser, template_keyword_p,
15754                                    check_dependency_p,
15755                                    is_declaration,
15756                                    tag_type,
15757                                    &is_identifier);
15758   if (templ == error_mark_node || is_identifier)
15759     {
15760       pop_deferring_access_checks ();
15761       return templ;
15762     }
15763
15764   /* Since we're going to preserve any side-effects from this parse, set up a
15765      firewall to protect our callers from cp_parser_commit_to_tentative_parse
15766      in the template arguments.  */
15767   tentative_firewall firewall (parser);
15768
15769   /* If we find the sequence `[:' after a template-name, it's probably
15770      a digraph-typo for `< ::'. Substitute the tokens and check if we can
15771      parse correctly the argument list.  */
15772   if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15773        == CPP_OPEN_SQUARE)
15774       && next_token->flags & DIGRAPH
15775       && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15776           == CPP_COLON)
15777       && !(next_token_2->flags & PREV_WHITE))
15778     {
15779       cp_parser_parse_tentatively (parser);
15780       /* Change `:' into `::'.  */
15781       next_token_2->type = CPP_SCOPE;
15782       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15783          CPP_LESS.  */
15784       cp_lexer_consume_token (parser->lexer);
15785
15786       /* Parse the arguments.  */
15787       arguments = cp_parser_enclosed_template_argument_list (parser);
15788       if (!cp_parser_parse_definitely (parser))
15789         {
15790           /* If we couldn't parse an argument list, then we revert our changes
15791              and return simply an error. Maybe this is not a template-id
15792              after all.  */
15793           next_token_2->type = CPP_COLON;
15794           cp_parser_error (parser, "expected %<<%>");
15795           pop_deferring_access_checks ();
15796           return error_mark_node;
15797         }
15798       /* Otherwise, emit an error about the invalid digraph, but continue
15799          parsing because we got our argument list.  */
15800       if (permerror (next_token->location,
15801                      "%<<::%> cannot begin a template-argument list"))
15802         {
15803           static bool hint = false;
15804           inform (next_token->location,
15805                   "%<<:%> is an alternate spelling for %<[%>."
15806                   " Insert whitespace between %<<%> and %<::%>");
15807           if (!hint && !flag_permissive)
15808             {
15809               inform (next_token->location, "(if you use %<-fpermissive%> "
15810                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15811                       "accept your code)");
15812               hint = true;
15813             }
15814         }
15815     }
15816   else
15817     {
15818       /* Look for the `<' that starts the template-argument-list.  */
15819       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15820         {
15821           pop_deferring_access_checks ();
15822           return error_mark_node;
15823         }
15824       /* Parse the arguments.  */
15825       arguments = cp_parser_enclosed_template_argument_list (parser);
15826     }
15827
15828   /* Set the location to be of the form:
15829      template-name < template-argument-list [opt] >
15830      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15831      with caret == start at the start of the template-name,
15832      ranging until the closing '>'.  */
15833   location_t finish_loc
15834     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15835   location_t combined_loc
15836     = make_location (token->location, token->location, finish_loc);
15837
15838   /* Check for concepts autos where they don't belong.  We could
15839      identify types in some cases of idnetifier TEMPL, looking ahead
15840      for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15841      types.  We reject them in functions, but if what we have is an
15842      identifier, even with none_type we can't conclude it's NOT a
15843      type, we have to wait for template substitution.  */
15844   if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15845     template_id = error_mark_node;
15846   /* Build a representation of the specialization.  */
15847   else if (identifier_p (templ))
15848     template_id = build_min_nt_loc (combined_loc,
15849                                     TEMPLATE_ID_EXPR,
15850                                     templ, arguments);
15851   else if (DECL_TYPE_TEMPLATE_P (templ)
15852            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15853     {
15854       bool entering_scope;
15855       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15856          template (rather than some instantiation thereof) only if
15857          is not nested within some other construct.  For example, in
15858          "template <typename T> void f(T) { A<T>::", A<T> is just an
15859          instantiation of A.  */
15860       entering_scope = (template_parm_scope_p ()
15861                         && cp_lexer_next_token_is (parser->lexer,
15862                                                    CPP_SCOPE));
15863       template_id
15864         = finish_template_type (templ, arguments, entering_scope);
15865     }
15866   /* A template-like identifier may be a partial concept id. */
15867   else if (flag_concepts
15868            && (template_id = (cp_parser_maybe_partial_concept_id
15869                               (parser, templ, arguments))))
15870     return template_id;
15871   else if (variable_template_p (templ))
15872     {
15873       template_id = lookup_template_variable (templ, arguments);
15874       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15875         SET_EXPR_LOCATION (template_id, combined_loc);
15876     }
15877   else
15878     {
15879       /* If it's not a class-template or a template-template, it should be
15880          a function-template.  */
15881       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15882                    || TREE_CODE (templ) == OVERLOAD
15883                    || BASELINK_P (templ)));
15884
15885       template_id = lookup_template_function (templ, arguments);
15886       if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15887         SET_EXPR_LOCATION (template_id, combined_loc);
15888     }
15889
15890   /* If parsing tentatively, replace the sequence of tokens that makes
15891      up the template-id with a CPP_TEMPLATE_ID token.  That way,
15892      should we re-parse the token stream, we will not have to repeat
15893      the effort required to do the parse, nor will we issue duplicate
15894      error messages about problems during instantiation of the
15895      template.  */
15896   if (start_of_id
15897       /* Don't do this if we had a parse error in a declarator; re-parsing
15898          might succeed if a name changes meaning (60361).  */
15899       && !(cp_parser_error_occurred (parser)
15900            && cp_parser_parsing_tentatively (parser)
15901            && parser->in_declarator_p))
15902     {
15903       /* Reset the contents of the START_OF_ID token.  */
15904       token->type = CPP_TEMPLATE_ID;
15905       token->location = combined_loc;
15906
15907       /* We must mark the lookup as kept, so we don't throw it away on
15908          the first parse.  */
15909       if (is_overloaded_fn (template_id))
15910         lookup_keep (get_fns (template_id), true);
15911
15912       /* Retrieve any deferred checks.  Do not pop this access checks yet
15913          so the memory will not be reclaimed during token replacing below.  */
15914       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15915       token->u.tree_check_value->value = template_id;
15916       token->u.tree_check_value->checks = get_deferred_access_checks ();
15917       token->keyword = RID_MAX;
15918
15919       /* Purge all subsequent tokens.  */
15920       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15921
15922       /* ??? Can we actually assume that, if template_id ==
15923          error_mark_node, we will have issued a diagnostic to the
15924          user, as opposed to simply marking the tentative parse as
15925          failed?  */
15926       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15927         error_at (token->location, "parse error in template argument list");
15928     }
15929
15930   pop_to_parent_deferring_access_checks ();
15931   return template_id;
15932 }
15933
15934 /* Parse a template-name.
15935
15936    template-name:
15937      identifier
15938
15939    The standard should actually say:
15940
15941    template-name:
15942      identifier
15943      operator-function-id
15944
15945    A defect report has been filed about this issue.
15946
15947    A conversion-function-id cannot be a template name because they cannot
15948    be part of a template-id. In fact, looking at this code:
15949
15950    a.operator K<int>()
15951
15952    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15953    It is impossible to call a templated conversion-function-id with an
15954    explicit argument list, since the only allowed template parameter is
15955    the type to which it is converting.
15956
15957    If TEMPLATE_KEYWORD_P is true, then we have just seen the
15958    `template' keyword, in a construction like:
15959
15960      T::template f<3>()
15961
15962    In that case `f' is taken to be a template-name, even though there
15963    is no way of knowing for sure.
15964
15965    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15966    name refers to a set of overloaded functions, at least one of which
15967    is a template, or an IDENTIFIER_NODE with the name of the template,
15968    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
15969    names are looked up inside uninstantiated templates.  */
15970
15971 static tree
15972 cp_parser_template_name (cp_parser* parser,
15973                          bool template_keyword_p,
15974                          bool check_dependency_p,
15975                          bool is_declaration,
15976                          enum tag_types tag_type,
15977                          bool *is_identifier)
15978 {
15979   tree identifier;
15980   tree decl;
15981   cp_token *token = cp_lexer_peek_token (parser->lexer);
15982
15983   /* If the next token is `operator', then we have either an
15984      operator-function-id or a conversion-function-id.  */
15985   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15986     {
15987       /* We don't know whether we're looking at an
15988          operator-function-id or a conversion-function-id.  */
15989       cp_parser_parse_tentatively (parser);
15990       /* Try an operator-function-id.  */
15991       identifier = cp_parser_operator_function_id (parser);
15992       /* If that didn't work, try a conversion-function-id.  */
15993       if (!cp_parser_parse_definitely (parser))
15994         {
15995           cp_parser_error (parser, "expected template-name");
15996           return error_mark_node;
15997         }
15998     }
15999   /* Look for the identifier.  */
16000   else
16001     identifier = cp_parser_identifier (parser);
16002
16003   /* If we didn't find an identifier, we don't have a template-id.  */
16004   if (identifier == error_mark_node)
16005     return error_mark_node;
16006
16007   /* If the name immediately followed the `template' keyword, then it
16008      is a template-name.  However, if the next token is not `<', then
16009      we do not treat it as a template-name, since it is not being used
16010      as part of a template-id.  This enables us to handle constructs
16011      like:
16012
16013        template <typename T> struct S { S(); };
16014        template <typename T> S<T>::S();
16015
16016      correctly.  We would treat `S' as a template -- if it were `S<T>'
16017      -- but we do not if there is no `<'.  */
16018
16019   if (processing_template_decl
16020       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16021     {
16022       /* In a declaration, in a dependent context, we pretend that the
16023          "template" keyword was present in order to improve error
16024          recovery.  For example, given:
16025
16026            template <typename T> void f(T::X<int>);
16027
16028          we want to treat "X<int>" as a template-id.  */
16029       if (is_declaration
16030           && !template_keyword_p
16031           && parser->scope && TYPE_P (parser->scope)
16032           && check_dependency_p
16033           && dependent_scope_p (parser->scope)
16034           /* Do not do this for dtors (or ctors), since they never
16035              need the template keyword before their name.  */
16036           && !constructor_name_p (identifier, parser->scope))
16037         {
16038           cp_token_position start = 0;
16039
16040           /* Explain what went wrong.  */
16041           error_at (token->location, "non-template %qD used as template",
16042                     identifier);
16043           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16044                   parser->scope, identifier);
16045           /* If parsing tentatively, find the location of the "<" token.  */
16046           if (cp_parser_simulate_error (parser))
16047             start = cp_lexer_token_position (parser->lexer, true);
16048           /* Parse the template arguments so that we can issue error
16049              messages about them.  */
16050           cp_lexer_consume_token (parser->lexer);
16051           cp_parser_enclosed_template_argument_list (parser);
16052           /* Skip tokens until we find a good place from which to
16053              continue parsing.  */
16054           cp_parser_skip_to_closing_parenthesis (parser,
16055                                                  /*recovering=*/true,
16056                                                  /*or_comma=*/true,
16057                                                  /*consume_paren=*/false);
16058           /* If parsing tentatively, permanently remove the
16059              template argument list.  That will prevent duplicate
16060              error messages from being issued about the missing
16061              "template" keyword.  */
16062           if (start)
16063             cp_lexer_purge_tokens_after (parser->lexer, start);
16064           if (is_identifier)
16065             *is_identifier = true;
16066           parser->context->object_type = NULL_TREE;
16067           return identifier;
16068         }
16069
16070       /* If the "template" keyword is present, then there is generally
16071          no point in doing name-lookup, so we just return IDENTIFIER.
16072          But, if the qualifying scope is non-dependent then we can
16073          (and must) do name-lookup normally.  */
16074       if (template_keyword_p)
16075         {
16076           tree scope = (parser->scope ? parser->scope
16077                         : parser->context->object_type);
16078           if (scope && TYPE_P (scope)
16079               && (!CLASS_TYPE_P (scope)
16080                   || (check_dependency_p && dependent_type_p (scope))))
16081             {
16082               /* We're optimizing away the call to cp_parser_lookup_name, but
16083                  we still need to do this.  */
16084               parser->context->object_type = NULL_TREE;
16085               return identifier;
16086             }
16087         }
16088     }
16089
16090   /* Look up the name.  */
16091   decl = cp_parser_lookup_name (parser, identifier,
16092                                 tag_type,
16093                                 /*is_template=*/true,
16094                                 /*is_namespace=*/false,
16095                                 check_dependency_p,
16096                                 /*ambiguous_decls=*/NULL,
16097                                 token->location);
16098
16099   decl = strip_using_decl (decl);
16100
16101   /* If DECL is a template, then the name was a template-name.  */
16102   if (TREE_CODE (decl) == TEMPLATE_DECL)
16103     {
16104       if (TREE_DEPRECATED (decl)
16105           && deprecated_state != DEPRECATED_SUPPRESS)
16106         warn_deprecated_use (decl, NULL_TREE);
16107     }
16108   else
16109     {
16110       /* The standard does not explicitly indicate whether a name that
16111          names a set of overloaded declarations, some of which are
16112          templates, is a template-name.  However, such a name should
16113          be a template-name; otherwise, there is no way to form a
16114          template-id for the overloaded templates.  */
16115       bool found = false;
16116
16117       for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16118            !found && iter; ++iter)
16119         if (TREE_CODE (*iter) == TEMPLATE_DECL)
16120           found = true;
16121
16122       if (!found)
16123         {
16124           /* The name does not name a template.  */
16125           cp_parser_error (parser, "expected template-name");
16126           return error_mark_node;
16127         }
16128     }
16129
16130   /* If DECL is dependent, and refers to a function, then just return
16131      its name; we will look it up again during template instantiation.  */
16132   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16133     {
16134       tree scope = ovl_scope (decl);
16135       if (TYPE_P (scope) && dependent_type_p (scope))
16136         return identifier;
16137     }
16138
16139   return decl;
16140 }
16141
16142 /* Parse a template-argument-list.
16143
16144    template-argument-list:
16145      template-argument ... [opt]
16146      template-argument-list , template-argument ... [opt]
16147
16148    Returns a TREE_VEC containing the arguments.  */
16149
16150 static tree
16151 cp_parser_template_argument_list (cp_parser* parser)
16152 {
16153   tree fixed_args[10];
16154   unsigned n_args = 0;
16155   unsigned alloced = 10;
16156   tree *arg_ary = fixed_args;
16157   tree vec;
16158   bool saved_in_template_argument_list_p;
16159   bool saved_ice_p;
16160   bool saved_non_ice_p;
16161
16162   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16163   parser->in_template_argument_list_p = true;
16164   /* Even if the template-id appears in an integral
16165      constant-expression, the contents of the argument list do
16166      not.  */
16167   saved_ice_p = parser->integral_constant_expression_p;
16168   parser->integral_constant_expression_p = false;
16169   saved_non_ice_p = parser->non_integral_constant_expression_p;
16170   parser->non_integral_constant_expression_p = false;
16171
16172   /* Parse the arguments.  */
16173   do
16174     {
16175       tree argument;
16176
16177       if (n_args)
16178         /* Consume the comma.  */
16179         cp_lexer_consume_token (parser->lexer);
16180
16181       /* Parse the template-argument.  */
16182       argument = cp_parser_template_argument (parser);
16183
16184       /* If the next token is an ellipsis, we're expanding a template
16185          argument pack. */
16186       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16187         {
16188           if (argument == error_mark_node)
16189             {
16190               cp_token *token = cp_lexer_peek_token (parser->lexer);
16191               error_at (token->location,
16192                         "expected parameter pack before %<...%>");
16193             }
16194           /* Consume the `...' token. */
16195           cp_lexer_consume_token (parser->lexer);
16196
16197           /* Make the argument into a TYPE_PACK_EXPANSION or
16198              EXPR_PACK_EXPANSION. */
16199           argument = make_pack_expansion (argument);
16200         }
16201
16202       if (n_args == alloced)
16203         {
16204           alloced *= 2;
16205
16206           if (arg_ary == fixed_args)
16207             {
16208               arg_ary = XNEWVEC (tree, alloced);
16209               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16210             }
16211           else
16212             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16213         }
16214       arg_ary[n_args++] = argument;
16215     }
16216   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16217
16218   vec = make_tree_vec (n_args);
16219
16220   while (n_args--)
16221     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16222
16223   if (arg_ary != fixed_args)
16224     free (arg_ary);
16225   parser->non_integral_constant_expression_p = saved_non_ice_p;
16226   parser->integral_constant_expression_p = saved_ice_p;
16227   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16228   if (CHECKING_P)
16229     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16230   return vec;
16231 }
16232
16233 /* Parse a template-argument.
16234
16235    template-argument:
16236      assignment-expression
16237      type-id
16238      id-expression
16239
16240    The representation is that of an assignment-expression, type-id, or
16241    id-expression -- except that the qualified id-expression is
16242    evaluated, so that the value returned is either a DECL or an
16243    OVERLOAD.
16244
16245    Although the standard says "assignment-expression", it forbids
16246    throw-expressions or assignments in the template argument.
16247    Therefore, we use "conditional-expression" instead.  */
16248
16249 static tree
16250 cp_parser_template_argument (cp_parser* parser)
16251 {
16252   tree argument;
16253   bool template_p;
16254   bool address_p;
16255   bool maybe_type_id = false;
16256   cp_token *token = NULL, *argument_start_token = NULL;
16257   location_t loc = 0;
16258   cp_id_kind idk;
16259
16260   /* There's really no way to know what we're looking at, so we just
16261      try each alternative in order.
16262
16263        [temp.arg]
16264
16265        In a template-argument, an ambiguity between a type-id and an
16266        expression is resolved to a type-id, regardless of the form of
16267        the corresponding template-parameter.
16268
16269      Therefore, we try a type-id first.  */
16270   cp_parser_parse_tentatively (parser);
16271   argument = cp_parser_template_type_arg (parser);
16272   /* If there was no error parsing the type-id but the next token is a
16273      '>>', our behavior depends on which dialect of C++ we're
16274      parsing. In C++98, we probably found a typo for '> >'. But there
16275      are type-id which are also valid expressions. For instance:
16276
16277      struct X { int operator >> (int); };
16278      template <int V> struct Foo {};
16279      Foo<X () >> 5> r;
16280
16281      Here 'X()' is a valid type-id of a function type, but the user just
16282      wanted to write the expression "X() >> 5". Thus, we remember that we
16283      found a valid type-id, but we still try to parse the argument as an
16284      expression to see what happens. 
16285
16286      In C++0x, the '>>' will be considered two separate '>'
16287      tokens.  */
16288   if (!cp_parser_error_occurred (parser)
16289       && cxx_dialect == cxx98
16290       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16291     {
16292       maybe_type_id = true;
16293       cp_parser_abort_tentative_parse (parser);
16294     }
16295   else
16296     {
16297       /* If the next token isn't a `,' or a `>', then this argument wasn't
16298       really finished. This means that the argument is not a valid
16299       type-id.  */
16300       if (!cp_parser_next_token_ends_template_argument_p (parser))
16301         cp_parser_error (parser, "expected template-argument");
16302       /* If that worked, we're done.  */
16303       if (cp_parser_parse_definitely (parser))
16304         return argument;
16305     }
16306   /* We're still not sure what the argument will be.  */
16307   cp_parser_parse_tentatively (parser);
16308   /* Try a template.  */
16309   argument_start_token = cp_lexer_peek_token (parser->lexer);
16310   argument = cp_parser_id_expression (parser,
16311                                       /*template_keyword_p=*/false,
16312                                       /*check_dependency_p=*/true,
16313                                       &template_p,
16314                                       /*declarator_p=*/false,
16315                                       /*optional_p=*/false);
16316   /* If the next token isn't a `,' or a `>', then this argument wasn't
16317      really finished.  */
16318   if (!cp_parser_next_token_ends_template_argument_p (parser))
16319     cp_parser_error (parser, "expected template-argument");
16320   if (!cp_parser_error_occurred (parser))
16321     {
16322       /* Figure out what is being referred to.  If the id-expression
16323          was for a class template specialization, then we will have a
16324          TYPE_DECL at this point.  There is no need to do name lookup
16325          at this point in that case.  */
16326       if (TREE_CODE (argument) != TYPE_DECL)
16327         argument = cp_parser_lookup_name (parser, argument,
16328                                           none_type,
16329                                           /*is_template=*/template_p,
16330                                           /*is_namespace=*/false,
16331                                           /*check_dependency=*/true,
16332                                           /*ambiguous_decls=*/NULL,
16333                                           argument_start_token->location);
16334       /* Handle a constrained-type-specifier for a non-type template
16335          parameter.  */
16336       if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16337         argument = decl;
16338       else if (TREE_CODE (argument) != TEMPLATE_DECL
16339                && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16340         cp_parser_error (parser, "expected template-name");
16341     }
16342   if (cp_parser_parse_definitely (parser))
16343     {
16344       if (TREE_DEPRECATED (argument))
16345         warn_deprecated_use (argument, NULL_TREE);
16346       return argument;
16347     }
16348   /* It must be a non-type argument.  In C++17 any constant-expression is
16349      allowed.  */
16350   if (cxx_dialect > cxx14)
16351     goto general_expr;
16352
16353   /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16354
16355      -- an integral constant-expression of integral or enumeration
16356         type; or
16357
16358      -- the name of a non-type template-parameter; or
16359
16360      -- the name of an object or function with external linkage...
16361
16362      -- the address of an object or function with external linkage...
16363
16364      -- a pointer to member...  */
16365   /* Look for a non-type template parameter.  */
16366   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16367     {
16368       cp_parser_parse_tentatively (parser);
16369       argument = cp_parser_primary_expression (parser,
16370                                                /*address_p=*/false,
16371                                                /*cast_p=*/false,
16372                                                /*template_arg_p=*/true,
16373                                                &idk);
16374       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16375           || !cp_parser_next_token_ends_template_argument_p (parser))
16376         cp_parser_simulate_error (parser);
16377       if (cp_parser_parse_definitely (parser))
16378         return argument;
16379     }
16380
16381   /* If the next token is "&", the argument must be the address of an
16382      object or function with external linkage.  */
16383   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16384   if (address_p)
16385     {
16386       loc = cp_lexer_peek_token (parser->lexer)->location;
16387       cp_lexer_consume_token (parser->lexer);
16388     }
16389   /* See if we might have an id-expression.  */
16390   token = cp_lexer_peek_token (parser->lexer);
16391   if (token->type == CPP_NAME
16392       || token->keyword == RID_OPERATOR
16393       || token->type == CPP_SCOPE
16394       || token->type == CPP_TEMPLATE_ID
16395       || token->type == CPP_NESTED_NAME_SPECIFIER)
16396     {
16397       cp_parser_parse_tentatively (parser);
16398       argument = cp_parser_primary_expression (parser,
16399                                                address_p,
16400                                                /*cast_p=*/false,
16401                                                /*template_arg_p=*/true,
16402                                                &idk);
16403       if (cp_parser_error_occurred (parser)
16404           || !cp_parser_next_token_ends_template_argument_p (parser))
16405         cp_parser_abort_tentative_parse (parser);
16406       else
16407         {
16408           tree probe;
16409
16410           if (INDIRECT_REF_P (argument))
16411             {
16412               /* Strip the dereference temporarily.  */
16413               gcc_assert (REFERENCE_REF_P (argument));
16414               argument = TREE_OPERAND (argument, 0);
16415             }
16416
16417           /* If we're in a template, we represent a qualified-id referring
16418              to a static data member as a SCOPE_REF even if the scope isn't
16419              dependent so that we can check access control later.  */
16420           probe = argument;
16421           if (TREE_CODE (probe) == SCOPE_REF)
16422             probe = TREE_OPERAND (probe, 1);
16423           if (VAR_P (probe))
16424             {
16425               /* A variable without external linkage might still be a
16426                  valid constant-expression, so no error is issued here
16427                  if the external-linkage check fails.  */
16428               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16429                 cp_parser_simulate_error (parser);
16430             }
16431           else if (is_overloaded_fn (argument))
16432             /* All overloaded functions are allowed; if the external
16433                linkage test does not pass, an error will be issued
16434                later.  */
16435             ;
16436           else if (address_p
16437                    && (TREE_CODE (argument) == OFFSET_REF
16438                        || TREE_CODE (argument) == SCOPE_REF))
16439             /* A pointer-to-member.  */
16440             ;
16441           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16442             ;
16443           else
16444             cp_parser_simulate_error (parser);
16445
16446           if (cp_parser_parse_definitely (parser))
16447             {
16448               if (address_p)
16449                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16450                                              tf_warning_or_error);
16451               else
16452                 argument = convert_from_reference (argument);
16453               return argument;
16454             }
16455         }
16456     }
16457   /* If the argument started with "&", there are no other valid
16458      alternatives at this point.  */
16459   if (address_p)
16460     {
16461       cp_parser_error (parser, "invalid non-type template argument");
16462       return error_mark_node;
16463     }
16464
16465  general_expr:
16466   /* If the argument wasn't successfully parsed as a type-id followed
16467      by '>>', the argument can only be a constant expression now.
16468      Otherwise, we try parsing the constant-expression tentatively,
16469      because the argument could really be a type-id.  */
16470   if (maybe_type_id)
16471     cp_parser_parse_tentatively (parser);
16472
16473   if (cxx_dialect <= cxx14)
16474     argument = cp_parser_constant_expression (parser);
16475   else
16476     {
16477       /* With C++17 generalized non-type template arguments we need to handle
16478          lvalue constant expressions, too.  */
16479       argument = cp_parser_assignment_expression (parser);
16480       require_potential_constant_expression (argument);
16481     }
16482
16483   if (!maybe_type_id)
16484     return argument;
16485   if (!cp_parser_next_token_ends_template_argument_p (parser))
16486     cp_parser_error (parser, "expected template-argument");
16487   if (cp_parser_parse_definitely (parser))
16488     return argument;
16489   /* We did our best to parse the argument as a non type-id, but that
16490      was the only alternative that matched (albeit with a '>' after
16491      it). We can assume it's just a typo from the user, and a
16492      diagnostic will then be issued.  */
16493   return cp_parser_template_type_arg (parser);
16494 }
16495
16496 /* Parse an explicit-instantiation.
16497
16498    explicit-instantiation:
16499      template declaration
16500
16501    Although the standard says `declaration', what it really means is:
16502
16503    explicit-instantiation:
16504      template decl-specifier-seq [opt] declarator [opt] ;
16505
16506    Things like `template int S<int>::i = 5, int S<double>::j;' are not
16507    supposed to be allowed.  A defect report has been filed about this
16508    issue.
16509
16510    GNU Extension:
16511
16512    explicit-instantiation:
16513      storage-class-specifier template
16514        decl-specifier-seq [opt] declarator [opt] ;
16515      function-specifier template
16516        decl-specifier-seq [opt] declarator [opt] ;  */
16517
16518 static void
16519 cp_parser_explicit_instantiation (cp_parser* parser)
16520 {
16521   int declares_class_or_enum;
16522   cp_decl_specifier_seq decl_specifiers;
16523   tree extension_specifier = NULL_TREE;
16524
16525   timevar_push (TV_TEMPLATE_INST);
16526
16527   /* Look for an (optional) storage-class-specifier or
16528      function-specifier.  */
16529   if (cp_parser_allow_gnu_extensions_p (parser))
16530     {
16531       extension_specifier
16532         = cp_parser_storage_class_specifier_opt (parser);
16533       if (!extension_specifier)
16534         extension_specifier
16535           = cp_parser_function_specifier_opt (parser,
16536                                               /*decl_specs=*/NULL);
16537     }
16538
16539   /* Look for the `template' keyword.  */
16540   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16541   /* Let the front end know that we are processing an explicit
16542      instantiation.  */
16543   begin_explicit_instantiation ();
16544   /* [temp.explicit] says that we are supposed to ignore access
16545      control while processing explicit instantiation directives.  */
16546   push_deferring_access_checks (dk_no_check);
16547   /* Parse a decl-specifier-seq.  */
16548   cp_parser_decl_specifier_seq (parser,
16549                                 CP_PARSER_FLAGS_OPTIONAL,
16550                                 &decl_specifiers,
16551                                 &declares_class_or_enum);
16552   /* If there was exactly one decl-specifier, and it declared a class,
16553      and there's no declarator, then we have an explicit type
16554      instantiation.  */
16555   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16556     {
16557       tree type;
16558
16559       type = check_tag_decl (&decl_specifiers,
16560                              /*explicit_type_instantiation_p=*/true);
16561       /* Turn access control back on for names used during
16562          template instantiation.  */
16563       pop_deferring_access_checks ();
16564       if (type)
16565         do_type_instantiation (type, extension_specifier,
16566                                /*complain=*/tf_error);
16567     }
16568   else
16569     {
16570       cp_declarator *declarator;
16571       tree decl;
16572
16573       /* Parse the declarator.  */
16574       declarator
16575         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16576                                 /*ctor_dtor_or_conv_p=*/NULL,
16577                                 /*parenthesized_p=*/NULL,
16578                                 /*member_p=*/false,
16579                                 /*friend_p=*/false);
16580       if (declares_class_or_enum & 2)
16581         cp_parser_check_for_definition_in_return_type (declarator,
16582                                                        decl_specifiers.type,
16583                                                        decl_specifiers.locations[ds_type_spec]);
16584       if (declarator != cp_error_declarator)
16585         {
16586           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16587             permerror (decl_specifiers.locations[ds_inline],
16588                        "explicit instantiation shall not use"
16589                        " %<inline%> specifier");
16590           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16591             permerror (decl_specifiers.locations[ds_constexpr],
16592                        "explicit instantiation shall not use"
16593                        " %<constexpr%> specifier");
16594
16595           decl = grokdeclarator (declarator, &decl_specifiers,
16596                                  NORMAL, 0, &decl_specifiers.attributes);
16597           /* Turn access control back on for names used during
16598              template instantiation.  */
16599           pop_deferring_access_checks ();
16600           /* Do the explicit instantiation.  */
16601           do_decl_instantiation (decl, extension_specifier);
16602         }
16603       else
16604         {
16605           pop_deferring_access_checks ();
16606           /* Skip the body of the explicit instantiation.  */
16607           cp_parser_skip_to_end_of_statement (parser);
16608         }
16609     }
16610   /* We're done with the instantiation.  */
16611   end_explicit_instantiation ();
16612
16613   cp_parser_consume_semicolon_at_end_of_statement (parser);
16614
16615   timevar_pop (TV_TEMPLATE_INST);
16616 }
16617
16618 /* Parse an explicit-specialization.
16619
16620    explicit-specialization:
16621      template < > declaration
16622
16623    Although the standard says `declaration', what it really means is:
16624
16625    explicit-specialization:
16626      template <> decl-specifier [opt] init-declarator [opt] ;
16627      template <> function-definition
16628      template <> explicit-specialization
16629      template <> template-declaration  */
16630
16631 static void
16632 cp_parser_explicit_specialization (cp_parser* parser)
16633 {
16634   bool need_lang_pop;
16635   cp_token *token = cp_lexer_peek_token (parser->lexer);
16636
16637   /* Look for the `template' keyword.  */
16638   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16639   /* Look for the `<'.  */
16640   cp_parser_require (parser, CPP_LESS, RT_LESS);
16641   /* Look for the `>'.  */
16642   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16643   /* We have processed another parameter list.  */
16644   ++parser->num_template_parameter_lists;
16645   /* [temp]
16646
16647      A template ... explicit specialization ... shall not have C
16648      linkage.  */
16649   if (current_lang_name == lang_name_c)
16650     {
16651       error_at (token->location, "template specialization with C linkage");
16652       maybe_show_extern_c_location ();
16653       /* Give it C++ linkage to avoid confusing other parts of the
16654          front end.  */
16655       push_lang_context (lang_name_cplusplus);
16656       need_lang_pop = true;
16657     }
16658   else
16659     need_lang_pop = false;
16660   /* Let the front end know that we are beginning a specialization.  */
16661   if (!begin_specialization ())
16662     {
16663       end_specialization ();
16664       return;
16665     }
16666
16667   /* If the next keyword is `template', we need to figure out whether
16668      or not we're looking a template-declaration.  */
16669   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16670     {
16671       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16672           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16673         cp_parser_template_declaration_after_export (parser,
16674                                                      /*member_p=*/false);
16675       else
16676         cp_parser_explicit_specialization (parser);
16677     }
16678   else
16679     /* Parse the dependent declaration.  */
16680     cp_parser_single_declaration (parser,
16681                                   /*checks=*/NULL,
16682                                   /*member_p=*/false,
16683                                   /*explicit_specialization_p=*/true,
16684                                   /*friend_p=*/NULL);
16685   /* We're done with the specialization.  */
16686   end_specialization ();
16687   /* For the erroneous case of a template with C linkage, we pushed an
16688      implicit C++ linkage scope; exit that scope now.  */
16689   if (need_lang_pop)
16690     pop_lang_context ();
16691   /* We're done with this parameter list.  */
16692   --parser->num_template_parameter_lists;
16693 }
16694
16695 /* Parse a type-specifier.
16696
16697    type-specifier:
16698      simple-type-specifier
16699      class-specifier
16700      enum-specifier
16701      elaborated-type-specifier
16702      cv-qualifier
16703
16704    GNU Extension:
16705
16706    type-specifier:
16707      __complex__
16708
16709    Returns a representation of the type-specifier.  For a
16710    class-specifier, enum-specifier, or elaborated-type-specifier, a
16711    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16712
16713    The parser flags FLAGS is used to control type-specifier parsing.
16714
16715    If IS_DECLARATION is TRUE, then this type-specifier is appearing
16716    in a decl-specifier-seq.
16717
16718    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16719    class-specifier, enum-specifier, or elaborated-type-specifier, then
16720    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
16721    if a type is declared; 2 if it is defined.  Otherwise, it is set to
16722    zero.
16723
16724    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16725    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
16726    is set to FALSE.  */
16727
16728 static tree
16729 cp_parser_type_specifier (cp_parser* parser,
16730                           cp_parser_flags flags,
16731                           cp_decl_specifier_seq *decl_specs,
16732                           bool is_declaration,
16733                           int* declares_class_or_enum,
16734                           bool* is_cv_qualifier)
16735 {
16736   tree type_spec = NULL_TREE;
16737   cp_token *token;
16738   enum rid keyword;
16739   cp_decl_spec ds = ds_last;
16740
16741   /* Assume this type-specifier does not declare a new type.  */
16742   if (declares_class_or_enum)
16743     *declares_class_or_enum = 0;
16744   /* And that it does not specify a cv-qualifier.  */
16745   if (is_cv_qualifier)
16746     *is_cv_qualifier = false;
16747   /* Peek at the next token.  */
16748   token = cp_lexer_peek_token (parser->lexer);
16749
16750   /* If we're looking at a keyword, we can use that to guide the
16751      production we choose.  */
16752   keyword = token->keyword;
16753   switch (keyword)
16754     {
16755     case RID_ENUM:
16756       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16757         goto elaborated_type_specifier;
16758
16759       /* Look for the enum-specifier.  */
16760       type_spec = cp_parser_enum_specifier (parser);
16761       /* If that worked, we're done.  */
16762       if (type_spec)
16763         {
16764           if (declares_class_or_enum)
16765             *declares_class_or_enum = 2;
16766           if (decl_specs)
16767             cp_parser_set_decl_spec_type (decl_specs,
16768                                           type_spec,
16769                                           token,
16770                                           /*type_definition_p=*/true);
16771           return type_spec;
16772         }
16773       else
16774         goto elaborated_type_specifier;
16775
16776       /* Any of these indicate either a class-specifier, or an
16777          elaborated-type-specifier.  */
16778     case RID_CLASS:
16779     case RID_STRUCT:
16780     case RID_UNION:
16781       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16782         goto elaborated_type_specifier;
16783
16784       /* Parse tentatively so that we can back up if we don't find a
16785          class-specifier.  */
16786       cp_parser_parse_tentatively (parser);
16787       /* Look for the class-specifier.  */
16788       type_spec = cp_parser_class_specifier (parser);
16789       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16790       /* If that worked, we're done.  */
16791       if (cp_parser_parse_definitely (parser))
16792         {
16793           if (declares_class_or_enum)
16794             *declares_class_or_enum = 2;
16795           if (decl_specs)
16796             cp_parser_set_decl_spec_type (decl_specs,
16797                                           type_spec,
16798                                           token,
16799                                           /*type_definition_p=*/true);
16800           return type_spec;
16801         }
16802
16803       /* Fall through.  */
16804     elaborated_type_specifier:
16805       /* We're declaring (not defining) a class or enum.  */
16806       if (declares_class_or_enum)
16807         *declares_class_or_enum = 1;
16808
16809       /* Fall through.  */
16810     case RID_TYPENAME:
16811       /* Look for an elaborated-type-specifier.  */
16812       type_spec
16813         = (cp_parser_elaborated_type_specifier
16814            (parser,
16815             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16816             is_declaration));
16817       if (decl_specs)
16818         cp_parser_set_decl_spec_type (decl_specs,
16819                                       type_spec,
16820                                       token,
16821                                       /*type_definition_p=*/false);
16822       return type_spec;
16823
16824     case RID_CONST:
16825       ds = ds_const;
16826       if (is_cv_qualifier)
16827         *is_cv_qualifier = true;
16828       break;
16829
16830     case RID_VOLATILE:
16831       ds = ds_volatile;
16832       if (is_cv_qualifier)
16833         *is_cv_qualifier = true;
16834       break;
16835
16836     case RID_RESTRICT:
16837       ds = ds_restrict;
16838       if (is_cv_qualifier)
16839         *is_cv_qualifier = true;
16840       break;
16841
16842     case RID_COMPLEX:
16843       /* The `__complex__' keyword is a GNU extension.  */
16844       ds = ds_complex;
16845       break;
16846
16847     default:
16848       break;
16849     }
16850
16851   /* Handle simple keywords.  */
16852   if (ds != ds_last)
16853     {
16854       if (decl_specs)
16855         {
16856           set_and_check_decl_spec_loc (decl_specs, ds, token);
16857           decl_specs->any_specifiers_p = true;
16858         }
16859       return cp_lexer_consume_token (parser->lexer)->u.value;
16860     }
16861
16862   /* If we do not already have a type-specifier, assume we are looking
16863      at a simple-type-specifier.  */
16864   type_spec = cp_parser_simple_type_specifier (parser,
16865                                                decl_specs,
16866                                                flags);
16867
16868   /* If we didn't find a type-specifier, and a type-specifier was not
16869      optional in this context, issue an error message.  */
16870   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16871     {
16872       cp_parser_error (parser, "expected type specifier");
16873       return error_mark_node;
16874     }
16875
16876   return type_spec;
16877 }
16878
16879 /* Parse a simple-type-specifier.
16880
16881    simple-type-specifier:
16882      :: [opt] nested-name-specifier [opt] type-name
16883      :: [opt] nested-name-specifier template template-id
16884      char
16885      wchar_t
16886      bool
16887      short
16888      int
16889      long
16890      signed
16891      unsigned
16892      float
16893      double
16894      void
16895
16896    C++11 Extension:
16897
16898    simple-type-specifier:
16899      auto
16900      decltype ( expression )   
16901      char16_t
16902      char32_t
16903      __underlying_type ( type-id )
16904
16905    C++17 extension:
16906
16907      nested-name-specifier(opt) template-name
16908
16909    GNU Extension:
16910
16911    simple-type-specifier:
16912      __int128
16913      __typeof__ unary-expression
16914      __typeof__ ( type-id )
16915      __typeof__ ( type-id ) { initializer-list , [opt] }
16916
16917    Concepts Extension:
16918
16919    simple-type-specifier:
16920      constrained-type-specifier
16921
16922    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
16923    appropriately updated.  */
16924
16925 static tree
16926 cp_parser_simple_type_specifier (cp_parser* parser,
16927                                  cp_decl_specifier_seq *decl_specs,
16928                                  cp_parser_flags flags)
16929 {
16930   tree type = NULL_TREE;
16931   cp_token *token;
16932   int idx;
16933
16934   /* Peek at the next token.  */
16935   token = cp_lexer_peek_token (parser->lexer);
16936
16937   /* If we're looking at a keyword, things are easy.  */
16938   switch (token->keyword)
16939     {
16940     case RID_CHAR:
16941       if (decl_specs)
16942         decl_specs->explicit_char_p = true;
16943       type = char_type_node;
16944       break;
16945     case RID_CHAR16:
16946       type = char16_type_node;
16947       break;
16948     case RID_CHAR32:
16949       type = char32_type_node;
16950       break;
16951     case RID_WCHAR:
16952       type = wchar_type_node;
16953       break;
16954     case RID_BOOL:
16955       type = boolean_type_node;
16956       break;
16957     case RID_SHORT:
16958       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16959       type = short_integer_type_node;
16960       break;
16961     case RID_INT:
16962       if (decl_specs)
16963         decl_specs->explicit_int_p = true;
16964       type = integer_type_node;
16965       break;
16966     case RID_INT_N_0:
16967     case RID_INT_N_1:
16968     case RID_INT_N_2:
16969     case RID_INT_N_3:
16970       idx = token->keyword - RID_INT_N_0;
16971       if (! int_n_enabled_p [idx])
16972         break;
16973       if (decl_specs)
16974         {
16975           decl_specs->explicit_intN_p = true;
16976           decl_specs->int_n_idx = idx;
16977         }
16978       type = int_n_trees [idx].signed_type;
16979       break;
16980     case RID_LONG:
16981       if (decl_specs)
16982         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16983       type = long_integer_type_node;
16984       break;
16985     case RID_SIGNED:
16986       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16987       type = integer_type_node;
16988       break;
16989     case RID_UNSIGNED:
16990       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16991       type = unsigned_type_node;
16992       break;
16993     case RID_FLOAT:
16994       type = float_type_node;
16995       break;
16996     case RID_DOUBLE:
16997       type = double_type_node;
16998       break;
16999     case RID_VOID:
17000       type = void_type_node;
17001       break;
17002
17003     case RID_AUTO:
17004       maybe_warn_cpp0x (CPP0X_AUTO);
17005       if (parser->auto_is_implicit_function_template_parm_p)
17006         {
17007           /* The 'auto' might be the placeholder return type for a function decl
17008              with trailing return type.  */
17009           bool have_trailing_return_fn_decl = false;
17010
17011           cp_parser_parse_tentatively (parser);
17012           cp_lexer_consume_token (parser->lexer);
17013           while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17014                  && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17015                  && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17016                  && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17017             {
17018               if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17019                 {
17020                   cp_lexer_consume_token (parser->lexer);
17021                   cp_parser_skip_to_closing_parenthesis (parser,
17022                                                          /*recovering*/false,
17023                                                          /*or_comma*/false,
17024                                                          /*consume_paren*/true);
17025                   continue;
17026                 }
17027
17028               if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17029                 {
17030                   have_trailing_return_fn_decl = true;
17031                   break;
17032                 }
17033
17034               cp_lexer_consume_token (parser->lexer);
17035             }
17036           cp_parser_abort_tentative_parse (parser);
17037
17038           if (have_trailing_return_fn_decl)
17039             {
17040               type = make_auto ();
17041               break;
17042             }
17043
17044           if (cxx_dialect >= cxx14)
17045             {
17046               type = synthesize_implicit_template_parm (parser, NULL_TREE);
17047               type = TREE_TYPE (type);
17048             }
17049           else
17050             type = error_mark_node;
17051
17052           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17053             {
17054               if (cxx_dialect < cxx14)
17055                 error_at (token->location,
17056                          "use of %<auto%> in lambda parameter declaration "
17057                          "only available with "
17058                          "-std=c++14 or -std=gnu++14");
17059             }
17060           else if (cxx_dialect < cxx14)
17061             error_at (token->location,
17062                      "use of %<auto%> in parameter declaration "
17063                      "only available with "
17064                      "-std=c++14 or -std=gnu++14");
17065           else if (!flag_concepts)
17066             pedwarn (token->location, 0,
17067                      "use of %<auto%> in parameter declaration "
17068                      "only available with -fconcepts");
17069         }
17070       else
17071         type = make_auto ();
17072       break;
17073
17074     case RID_DECLTYPE:
17075       /* Since DR 743, decltype can either be a simple-type-specifier by
17076          itself or begin a nested-name-specifier.  Parsing it will replace
17077          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17078          handling below decide what to do.  */
17079       cp_parser_decltype (parser);
17080       cp_lexer_set_token_position (parser->lexer, token);
17081       break;
17082
17083     case RID_TYPEOF:
17084       /* Consume the `typeof' token.  */
17085       cp_lexer_consume_token (parser->lexer);
17086       /* Parse the operand to `typeof'.  */
17087       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17088       /* If it is not already a TYPE, take its type.  */
17089       if (!TYPE_P (type))
17090         type = finish_typeof (type);
17091
17092       if (decl_specs)
17093         cp_parser_set_decl_spec_type (decl_specs, type,
17094                                       token,
17095                                       /*type_definition_p=*/false);
17096
17097       return type;
17098
17099     case RID_UNDERLYING_TYPE:
17100       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17101       if (decl_specs)
17102         cp_parser_set_decl_spec_type (decl_specs, type,
17103                                       token,
17104                                       /*type_definition_p=*/false);
17105
17106       return type;
17107
17108     case RID_BASES:
17109     case RID_DIRECT_BASES:
17110       type = cp_parser_trait_expr (parser, token->keyword);
17111       if (decl_specs)
17112        cp_parser_set_decl_spec_type (decl_specs, type,
17113                                      token,
17114                                      /*type_definition_p=*/false);
17115       return type;
17116     default:
17117       break;
17118     }
17119
17120   /* If token is an already-parsed decltype not followed by ::,
17121      it's a simple-type-specifier.  */
17122   if (token->type == CPP_DECLTYPE
17123       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17124     {
17125       type = saved_checks_value (token->u.tree_check_value);
17126       if (decl_specs)
17127         {
17128           cp_parser_set_decl_spec_type (decl_specs, type,
17129                                         token,
17130                                         /*type_definition_p=*/false);
17131           /* Remember that we are handling a decltype in order to
17132              implement the resolution of DR 1510 when the argument
17133              isn't instantiation dependent.  */
17134           decl_specs->decltype_p = true;
17135         }
17136       cp_lexer_consume_token (parser->lexer);
17137       return type;
17138     }
17139
17140   /* If the type-specifier was for a built-in type, we're done.  */
17141   if (type)
17142     {
17143       /* Record the type.  */
17144       if (decl_specs
17145           && (token->keyword != RID_SIGNED
17146               && token->keyword != RID_UNSIGNED
17147               && token->keyword != RID_SHORT
17148               && token->keyword != RID_LONG))
17149         cp_parser_set_decl_spec_type (decl_specs,
17150                                       type,
17151                                       token,
17152                                       /*type_definition_p=*/false);
17153       if (decl_specs)
17154         decl_specs->any_specifiers_p = true;
17155
17156       /* Consume the token.  */
17157       cp_lexer_consume_token (parser->lexer);
17158
17159       if (type == error_mark_node)
17160         return error_mark_node;
17161
17162       /* There is no valid C++ program where a non-template type is
17163          followed by a "<".  That usually indicates that the user thought
17164          that the type was a template.  */
17165       cp_parser_check_for_invalid_template_id (parser, type, none_type,
17166                                                token->location);
17167
17168       return TYPE_NAME (type);
17169     }
17170
17171   /* The type-specifier must be a user-defined type.  */
17172   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17173     {
17174       bool qualified_p;
17175       bool global_p;
17176
17177       /* Don't gobble tokens or issue error messages if this is an
17178          optional type-specifier.  */
17179       if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17180         cp_parser_parse_tentatively (parser);
17181
17182       token = cp_lexer_peek_token (parser->lexer);
17183
17184       /* Look for the optional `::' operator.  */
17185       global_p
17186         = (cp_parser_global_scope_opt (parser,
17187                                        /*current_scope_valid_p=*/false)
17188            != NULL_TREE);
17189       /* Look for the nested-name specifier.  */
17190       qualified_p
17191         = (cp_parser_nested_name_specifier_opt (parser,
17192                                                 /*typename_keyword_p=*/false,
17193                                                 /*check_dependency_p=*/true,
17194                                                 /*type_p=*/false,
17195                                                 /*is_declaration=*/false)
17196            != NULL_TREE);
17197       /* If we have seen a nested-name-specifier, and the next token
17198          is `template', then we are using the template-id production.  */
17199       if (parser->scope
17200           && cp_parser_optional_template_keyword (parser))
17201         {
17202           /* Look for the template-id.  */
17203           type = cp_parser_template_id (parser,
17204                                         /*template_keyword_p=*/true,
17205                                         /*check_dependency_p=*/true,
17206                                         none_type,
17207                                         /*is_declaration=*/false);
17208           /* If the template-id did not name a type, we are out of
17209              luck.  */
17210           if (TREE_CODE (type) != TYPE_DECL)
17211             {
17212               cp_parser_error (parser, "expected template-id for type");
17213               type = NULL_TREE;
17214             }
17215         }
17216       /* Otherwise, look for a type-name.  */
17217       else
17218         type = cp_parser_type_name (parser);
17219       /* Keep track of all name-lookups performed in class scopes.  */
17220       if (type
17221           && !global_p
17222           && !qualified_p
17223           && TREE_CODE (type) == TYPE_DECL
17224           && identifier_p (DECL_NAME (type)))
17225         maybe_note_name_used_in_class (DECL_NAME (type), type);
17226       /* If it didn't work out, we don't have a TYPE.  */
17227       if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17228           && !cp_parser_parse_definitely (parser))
17229         type = NULL_TREE;
17230       if (!type && cxx_dialect >= cxx17)
17231         {
17232           if (flags & CP_PARSER_FLAGS_OPTIONAL)
17233             cp_parser_parse_tentatively (parser);
17234
17235           cp_parser_global_scope_opt (parser,
17236                                       /*current_scope_valid_p=*/false);
17237           cp_parser_nested_name_specifier_opt (parser,
17238                                                /*typename_keyword_p=*/false,
17239                                                /*check_dependency_p=*/true,
17240                                                /*type_p=*/false,
17241                                                /*is_declaration=*/false);
17242           tree name = cp_parser_identifier (parser);
17243           if (name && TREE_CODE (name) == IDENTIFIER_NODE
17244               && parser->scope != error_mark_node)
17245             {
17246               tree tmpl = cp_parser_lookup_name (parser, name,
17247                                                  none_type,
17248                                                  /*is_template=*/false,
17249                                                  /*is_namespace=*/false,
17250                                                  /*check_dependency=*/true,
17251                                                  /*ambiguous_decls=*/NULL,
17252                                                  token->location);
17253               if (tmpl && tmpl != error_mark_node
17254                   && (DECL_CLASS_TEMPLATE_P (tmpl)
17255                       || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17256                 type = make_template_placeholder (tmpl);
17257               else
17258                 {
17259                   type = error_mark_node;
17260                   if (!cp_parser_simulate_error (parser))
17261                     cp_parser_name_lookup_error (parser, name, tmpl,
17262                                                  NLE_TYPE, token->location);
17263                 }
17264             }
17265           else
17266             type = error_mark_node;
17267
17268           if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17269               && !cp_parser_parse_definitely (parser))
17270             type = NULL_TREE;
17271         }
17272       if (type && decl_specs)
17273         cp_parser_set_decl_spec_type (decl_specs, type,
17274                                       token,
17275                                       /*type_definition_p=*/false);
17276     }
17277
17278   /* If we didn't get a type-name, issue an error message.  */
17279   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17280     {
17281       cp_parser_error (parser, "expected type-name");
17282       return error_mark_node;
17283     }
17284
17285   if (type && type != error_mark_node)
17286     {
17287       /* See if TYPE is an Objective-C type, and if so, parse and
17288          accept any protocol references following it.  Do this before
17289          the cp_parser_check_for_invalid_template_id() call, because
17290          Objective-C types can be followed by '<...>' which would
17291          enclose protocol names rather than template arguments, and so
17292          everything is fine.  */
17293       if (c_dialect_objc () && !parser->scope
17294           && (objc_is_id (type) || objc_is_class_name (type)))
17295         {
17296           tree protos = cp_parser_objc_protocol_refs_opt (parser);
17297           tree qual_type = objc_get_protocol_qualified_type (type, protos);
17298
17299           /* Clobber the "unqualified" type previously entered into
17300              DECL_SPECS with the new, improved protocol-qualified version.  */
17301           if (decl_specs)
17302             decl_specs->type = qual_type;
17303
17304           return qual_type;
17305         }
17306
17307       /* There is no valid C++ program where a non-template type is
17308          followed by a "<".  That usually indicates that the user
17309          thought that the type was a template.  */
17310       cp_parser_check_for_invalid_template_id (parser, type,
17311                                                none_type,
17312                                                token->location);
17313     }
17314
17315   return type;
17316 }
17317
17318 /* Parse a type-name.
17319
17320    type-name:
17321      class-name
17322      enum-name
17323      typedef-name
17324      simple-template-id [in c++0x]
17325
17326    enum-name:
17327      identifier
17328
17329    typedef-name:
17330      identifier
17331
17332   Concepts:
17333
17334    type-name:
17335      concept-name
17336      partial-concept-id
17337
17338    concept-name:
17339      identifier
17340
17341    Returns a TYPE_DECL for the type.  */
17342
17343 static tree
17344 cp_parser_type_name (cp_parser* parser)
17345 {
17346   return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17347 }
17348
17349 /* See above. */
17350 static tree
17351 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17352 {
17353   tree type_decl;
17354
17355   /* We can't know yet whether it is a class-name or not.  */
17356   cp_parser_parse_tentatively (parser);
17357   /* Try a class-name.  */
17358   type_decl = cp_parser_class_name (parser,
17359                                     typename_keyword_p,
17360                                     /*template_keyword_p=*/false,
17361                                     none_type,
17362                                     /*check_dependency_p=*/true,
17363                                     /*class_head_p=*/false,
17364                                     /*is_declaration=*/false);
17365   /* If it's not a class-name, keep looking.  */
17366   if (!cp_parser_parse_definitely (parser))
17367     {
17368       if (cxx_dialect < cxx11)
17369         /* It must be a typedef-name or an enum-name.  */
17370         return cp_parser_nonclass_name (parser);
17371
17372       cp_parser_parse_tentatively (parser);
17373       /* It is either a simple-template-id representing an
17374          instantiation of an alias template...  */
17375       type_decl = cp_parser_template_id (parser,
17376                                          /*template_keyword_p=*/false,
17377                                          /*check_dependency_p=*/true,
17378                                          none_type,
17379                                          /*is_declaration=*/false);
17380       /* Note that this must be an instantiation of an alias template
17381          because [temp.names]/6 says:
17382          
17383              A template-id that names an alias template specialization
17384              is a type-name.
17385
17386          Whereas [temp.names]/7 says:
17387          
17388              A simple-template-id that names a class template
17389              specialization is a class-name.
17390
17391          With concepts, this could also be a partial-concept-id that
17392          declares a non-type template parameter. */
17393       if (type_decl != NULL_TREE
17394           && TREE_CODE (type_decl) == TYPE_DECL
17395           && TYPE_DECL_ALIAS_P (type_decl))
17396         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17397       else if (is_constrained_parameter (type_decl))
17398         /* Don't do anything. */ ;
17399       else
17400         cp_parser_simulate_error (parser);
17401
17402       if (!cp_parser_parse_definitely (parser))
17403         /* ... Or a typedef-name or an enum-name.  */
17404         return cp_parser_nonclass_name (parser);
17405     }
17406
17407   return type_decl;
17408 }
17409
17410 /*  Check if DECL and ARGS can form a constrained-type-specifier.
17411     If ARGS is non-null, we try to form a concept check of the
17412     form DECL<?, ARGS> where ? is a wildcard that matches any
17413     kind of template argument. If ARGS is NULL, then we try to
17414     form a concept check of the form DECL<?>. */
17415
17416 static tree
17417 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17418                                             tree decl, tree args)
17419 {
17420   gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17421
17422   /* If we a constrained-type-specifier cannot be deduced. */
17423   if (parser->prevent_constrained_type_specifiers)
17424     return NULL_TREE;
17425
17426   /* A constrained type specifier can only be found in an
17427      overload set or as a reference to a template declaration.
17428
17429      FIXME: This might be masking a bug.  It's possible that
17430      that the deduction below is causing template specializations
17431      to be formed with the wildcard as an argument.  */
17432   if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17433     return NULL_TREE;
17434
17435   /* Try to build a call expression that evaluates the
17436      concept. This can fail if the overload set refers
17437      only to non-templates. */
17438   tree placeholder = build_nt (WILDCARD_DECL);
17439   tree check = build_concept_check (decl, placeholder, args);
17440   if (check == error_mark_node)
17441     return NULL_TREE;
17442
17443   /* Deduce the checked constraint and the prototype parameter.
17444
17445      FIXME: In certain cases, failure to deduce should be a
17446      diagnosable error.  */
17447   tree conc;
17448   tree proto;
17449   if (!deduce_constrained_parameter (check, conc, proto))
17450     return NULL_TREE;
17451
17452   /* In template parameter scope, this results in a constrained
17453      parameter. Return a descriptor of that parm. */
17454   if (processing_template_parmlist)
17455     return build_constrained_parameter (conc, proto, args);
17456
17457   /* In a parameter-declaration-clause, constrained-type
17458      specifiers result in invented template parameters.  */
17459   if (parser->auto_is_implicit_function_template_parm_p)
17460     {
17461       tree x = build_constrained_parameter (conc, proto, args);
17462       return synthesize_implicit_template_parm (parser, x);
17463     }
17464   else
17465     {
17466      /* Otherwise, we're in a context where the constrained
17467         type name is deduced and the constraint applies
17468         after deduction. */
17469       return make_constrained_auto (conc, args);
17470     }
17471
17472   return NULL_TREE;
17473 }
17474
17475 /* If DECL refers to a concept, return a TYPE_DECL representing
17476    the result of using the constrained type specifier in the
17477    current context.  DECL refers to a concept if
17478
17479   - it is an overload set containing a function concept taking a single
17480     type argument, or
17481
17482   - it is a variable concept taking a single type argument.  */
17483
17484 static tree
17485 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17486 {
17487   if (flag_concepts
17488       && (TREE_CODE (decl) == OVERLOAD
17489           || BASELINK_P (decl)
17490           || variable_concept_p (decl)))
17491     return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17492   else
17493     return NULL_TREE;
17494 }
17495
17496 /* Check if DECL and ARGS form a partial-concept-id.  If so,
17497    assign ID to the resulting constrained placeholder.
17498
17499    Returns true if the partial-concept-id designates a placeholder
17500    and false otherwise. Note that *id is set to NULL_TREE in
17501    this case. */
17502
17503 static tree
17504 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17505 {
17506   return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17507 }
17508
17509 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17510    or a concept-name.
17511
17512    enum-name:
17513      identifier
17514
17515    typedef-name:
17516      identifier
17517
17518    concept-name:
17519      identifier
17520
17521    Returns a TYPE_DECL for the type.  */
17522
17523 static tree
17524 cp_parser_nonclass_name (cp_parser* parser)
17525 {
17526   tree type_decl;
17527   tree identifier;
17528
17529   cp_token *token = cp_lexer_peek_token (parser->lexer);
17530   identifier = cp_parser_identifier (parser);
17531   if (identifier == error_mark_node)
17532     return error_mark_node;
17533
17534   /* Look up the type-name.  */
17535   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17536
17537   type_decl = strip_using_decl (type_decl);
17538   
17539   /* If we found an overload set, then it may refer to a concept-name. */
17540   if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17541     type_decl = decl;
17542
17543   if (TREE_CODE (type_decl) != TYPE_DECL
17544       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17545     {
17546       /* See if this is an Objective-C type.  */
17547       tree protos = cp_parser_objc_protocol_refs_opt (parser);
17548       tree type = objc_get_protocol_qualified_type (identifier, protos);
17549       if (type)
17550         type_decl = TYPE_NAME (type);
17551     }
17552
17553   /* Issue an error if we did not find a type-name.  */
17554   if (TREE_CODE (type_decl) != TYPE_DECL
17555       /* In Objective-C, we have the complication that class names are
17556          normally type names and start declarations (eg, the
17557          "NSObject" in "NSObject *object;"), but can be used in an
17558          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17559          is an expression.  So, a classname followed by a dot is not a
17560          valid type-name.  */
17561       || (objc_is_class_name (TREE_TYPE (type_decl))
17562           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17563     {
17564       if (!cp_parser_simulate_error (parser))
17565         cp_parser_name_lookup_error (parser, identifier, type_decl,
17566                                      NLE_TYPE, token->location);
17567       return error_mark_node;
17568     }
17569   /* Remember that the name was used in the definition of the
17570      current class so that we can check later to see if the
17571      meaning would have been different after the class was
17572      entirely defined.  */
17573   else if (type_decl != error_mark_node
17574            && !parser->scope)
17575     maybe_note_name_used_in_class (identifier, type_decl);
17576   
17577   return type_decl;
17578 }
17579
17580 /* Parse an elaborated-type-specifier.  Note that the grammar given
17581    here incorporates the resolution to DR68.
17582
17583    elaborated-type-specifier:
17584      class-key :: [opt] nested-name-specifier [opt] identifier
17585      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17586      enum-key :: [opt] nested-name-specifier [opt] identifier
17587      typename :: [opt] nested-name-specifier identifier
17588      typename :: [opt] nested-name-specifier template [opt]
17589        template-id
17590
17591    GNU extension:
17592
17593    elaborated-type-specifier:
17594      class-key attributes :: [opt] nested-name-specifier [opt] identifier
17595      class-key attributes :: [opt] nested-name-specifier [opt]
17596                template [opt] template-id
17597      enum attributes :: [opt] nested-name-specifier [opt] identifier
17598
17599    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17600    declared `friend'.  If IS_DECLARATION is TRUE, then this
17601    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17602    something is being declared.
17603
17604    Returns the TYPE specified.  */
17605
17606 static tree
17607 cp_parser_elaborated_type_specifier (cp_parser* parser,
17608                                      bool is_friend,
17609                                      bool is_declaration)
17610 {
17611   enum tag_types tag_type;
17612   tree identifier;
17613   tree type = NULL_TREE;
17614   tree attributes = NULL_TREE;
17615   tree globalscope;
17616   cp_token *token = NULL;
17617
17618   /* See if we're looking at the `enum' keyword.  */
17619   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17620     {
17621       /* Consume the `enum' token.  */
17622       cp_lexer_consume_token (parser->lexer);
17623       /* Remember that it's an enumeration type.  */
17624       tag_type = enum_type;
17625       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17626          enums) is used here.  */
17627       cp_token *token = cp_lexer_peek_token (parser->lexer);
17628       if (cp_parser_is_keyword (token, RID_CLASS)
17629           || cp_parser_is_keyword (token, RID_STRUCT))
17630         {
17631           gcc_rich_location richloc (token->location);
17632           richloc.add_range (input_location, false);
17633           richloc.add_fixit_remove ();
17634           pedwarn (&richloc, 0, "elaborated-type-specifier for "
17635                    "a scoped enum must not use the %qD keyword",
17636                    token->u.value);
17637           /* Consume the `struct' or `class' and parse it anyway.  */
17638           cp_lexer_consume_token (parser->lexer);
17639         }
17640       /* Parse the attributes.  */
17641       attributes = cp_parser_attributes_opt (parser);
17642     }
17643   /* Or, it might be `typename'.  */
17644   else if (cp_lexer_next_token_is_keyword (parser->lexer,
17645                                            RID_TYPENAME))
17646     {
17647       /* Consume the `typename' token.  */
17648       cp_lexer_consume_token (parser->lexer);
17649       /* Remember that it's a `typename' type.  */
17650       tag_type = typename_type;
17651     }
17652   /* Otherwise it must be a class-key.  */
17653   else
17654     {
17655       tag_type = cp_parser_class_key (parser);
17656       if (tag_type == none_type)
17657         return error_mark_node;
17658       /* Parse the attributes.  */
17659       attributes = cp_parser_attributes_opt (parser);
17660     }
17661
17662   /* Look for the `::' operator.  */
17663   globalscope =  cp_parser_global_scope_opt (parser,
17664                                              /*current_scope_valid_p=*/false);
17665   /* Look for the nested-name-specifier.  */
17666   tree nested_name_specifier;
17667   if (tag_type == typename_type && !globalscope)
17668     {
17669       nested_name_specifier
17670         = cp_parser_nested_name_specifier (parser,
17671                                            /*typename_keyword_p=*/true,
17672                                            /*check_dependency_p=*/true,
17673                                            /*type_p=*/true,
17674                                            is_declaration);
17675       if (!nested_name_specifier)
17676         return error_mark_node;
17677     }
17678   else
17679     /* Even though `typename' is not present, the proposed resolution
17680        to Core Issue 180 says that in `class A<T>::B', `B' should be
17681        considered a type-name, even if `A<T>' is dependent.  */
17682     nested_name_specifier
17683       = cp_parser_nested_name_specifier_opt (parser,
17684                                              /*typename_keyword_p=*/true,
17685                                              /*check_dependency_p=*/true,
17686                                              /*type_p=*/true,
17687                                              is_declaration);
17688  /* For everything but enumeration types, consider a template-id.
17689     For an enumeration type, consider only a plain identifier.  */
17690   if (tag_type != enum_type)
17691     {
17692       bool template_p = false;
17693       tree decl;
17694
17695       /* Allow the `template' keyword.  */
17696       template_p = cp_parser_optional_template_keyword (parser);
17697       /* If we didn't see `template', we don't know if there's a
17698          template-id or not.  */
17699       if (!template_p)
17700         cp_parser_parse_tentatively (parser);
17701       /* Parse the template-id.  */
17702       token = cp_lexer_peek_token (parser->lexer);
17703       decl = cp_parser_template_id (parser, template_p,
17704                                     /*check_dependency_p=*/true,
17705                                     tag_type,
17706                                     is_declaration);
17707       /* If we didn't find a template-id, look for an ordinary
17708          identifier.  */
17709       if (!template_p && !cp_parser_parse_definitely (parser))
17710         ;
17711       /* We can get here when cp_parser_template_id, called by
17712          cp_parser_class_name with tag_type == none_type, succeeds
17713          and caches a BASELINK.  Then, when called again here,
17714          instead of failing and returning an error_mark_node
17715          returns it (see template/typename17.C in C++11).
17716          ??? Could we diagnose this earlier?  */
17717       else if (tag_type == typename_type && BASELINK_P (decl))
17718         {
17719           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17720           type = error_mark_node;
17721         }
17722       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17723          in effect, then we must assume that, upon instantiation, the
17724          template will correspond to a class.  */
17725       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17726                && tag_type == typename_type)
17727         type = make_typename_type (parser->scope, decl,
17728                                    typename_type,
17729                                    /*complain=*/tf_error);
17730       /* If the `typename' keyword is in effect and DECL is not a type
17731          decl, then type is non existent.   */
17732       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17733         ; 
17734       else if (TREE_CODE (decl) == TYPE_DECL)
17735         {
17736           type = check_elaborated_type_specifier (tag_type, decl,
17737                                                   /*allow_template_p=*/true);
17738
17739           /* If the next token is a semicolon, this must be a specialization,
17740              instantiation, or friend declaration.  Check the scope while we
17741              still know whether or not we had a nested-name-specifier.  */
17742           if (type != error_mark_node
17743               && !nested_name_specifier && !is_friend
17744               && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17745             check_unqualified_spec_or_inst (type, token->location);
17746         }
17747       else if (decl == error_mark_node)
17748         type = error_mark_node; 
17749     }
17750
17751   if (!type)
17752     {
17753       token = cp_lexer_peek_token (parser->lexer);
17754       identifier = cp_parser_identifier (parser);
17755
17756       if (identifier == error_mark_node)
17757         {
17758           parser->scope = NULL_TREE;
17759           return error_mark_node;
17760         }
17761
17762       /* For a `typename', we needn't call xref_tag.  */
17763       if (tag_type == typename_type
17764           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17765         return cp_parser_make_typename_type (parser, identifier,
17766                                              token->location);
17767
17768       /* Template parameter lists apply only if we are not within a
17769          function parameter list.  */
17770       bool template_parm_lists_apply
17771           = parser->num_template_parameter_lists;
17772       if (template_parm_lists_apply)
17773         for (cp_binding_level *s = current_binding_level;
17774              s && s->kind != sk_template_parms;
17775              s = s->level_chain)
17776           if (s->kind == sk_function_parms)
17777             template_parm_lists_apply = false;
17778
17779       /* Look up a qualified name in the usual way.  */
17780       if (parser->scope)
17781         {
17782           tree decl;
17783           tree ambiguous_decls;
17784
17785           decl = cp_parser_lookup_name (parser, identifier,
17786                                         tag_type,
17787                                         /*is_template=*/false,
17788                                         /*is_namespace=*/false,
17789                                         /*check_dependency=*/true,
17790                                         &ambiguous_decls,
17791                                         token->location);
17792
17793           /* If the lookup was ambiguous, an error will already have been
17794              issued.  */
17795           if (ambiguous_decls)
17796             return error_mark_node;
17797
17798           /* If we are parsing friend declaration, DECL may be a
17799              TEMPLATE_DECL tree node here.  However, we need to check
17800              whether this TEMPLATE_DECL results in valid code.  Consider
17801              the following example:
17802
17803                namespace N {
17804                  template <class T> class C {};
17805                }
17806                class X {
17807                  template <class T> friend class N::C; // #1, valid code
17808                };
17809                template <class T> class Y {
17810                  friend class N::C;                    // #2, invalid code
17811                };
17812
17813              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17814              name lookup of `N::C'.  We see that friend declaration must
17815              be template for the code to be valid.  Note that
17816              processing_template_decl does not work here since it is
17817              always 1 for the above two cases.  */
17818
17819           decl = (cp_parser_maybe_treat_template_as_class
17820                   (decl, /*tag_name_p=*/is_friend
17821                          && template_parm_lists_apply));
17822
17823           if (TREE_CODE (decl) != TYPE_DECL)
17824             {
17825               cp_parser_diagnose_invalid_type_name (parser,
17826                                                     identifier,
17827                                                     token->location);
17828               return error_mark_node;
17829             }
17830
17831           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17832             {
17833               bool allow_template = (template_parm_lists_apply
17834                                      || DECL_SELF_REFERENCE_P (decl));
17835               type = check_elaborated_type_specifier (tag_type, decl,
17836                                                       allow_template);
17837
17838               if (type == error_mark_node)
17839                 return error_mark_node;
17840             }
17841
17842           /* Forward declarations of nested types, such as
17843
17844                class C1::C2;
17845                class C1::C2::C3;
17846
17847              are invalid unless all components preceding the final '::'
17848              are complete.  If all enclosing types are complete, these
17849              declarations become merely pointless.
17850
17851              Invalid forward declarations of nested types are errors
17852              caught elsewhere in parsing.  Those that are pointless arrive
17853              here.  */
17854
17855           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17856               && !is_friend && !processing_explicit_instantiation)
17857             warning (0, "declaration %qD does not declare anything", decl);
17858
17859           type = TREE_TYPE (decl);
17860         }
17861       else
17862         {
17863           /* An elaborated-type-specifier sometimes introduces a new type and
17864              sometimes names an existing type.  Normally, the rule is that it
17865              introduces a new type only if there is not an existing type of
17866              the same name already in scope.  For example, given:
17867
17868                struct S {};
17869                void f() { struct S s; }
17870
17871              the `struct S' in the body of `f' is the same `struct S' as in
17872              the global scope; the existing definition is used.  However, if
17873              there were no global declaration, this would introduce a new
17874              local class named `S'.
17875
17876              An exception to this rule applies to the following code:
17877
17878                namespace N { struct S; }
17879
17880              Here, the elaborated-type-specifier names a new type
17881              unconditionally; even if there is already an `S' in the
17882              containing scope this declaration names a new type.
17883              This exception only applies if the elaborated-type-specifier
17884              forms the complete declaration:
17885
17886                [class.name]
17887
17888                A declaration consisting solely of `class-key identifier ;' is
17889                either a redeclaration of the name in the current scope or a
17890                forward declaration of the identifier as a class name.  It
17891                introduces the name into the current scope.
17892
17893              We are in this situation precisely when the next token is a `;'.
17894
17895              An exception to the exception is that a `friend' declaration does
17896              *not* name a new type; i.e., given:
17897
17898                struct S { friend struct T; };
17899
17900              `T' is not a new type in the scope of `S'.
17901
17902              Also, `new struct S' or `sizeof (struct S)' never results in the
17903              definition of a new type; a new type can only be declared in a
17904              declaration context.  */
17905
17906           tag_scope ts;
17907           bool template_p;
17908
17909           if (is_friend)
17910             /* Friends have special name lookup rules.  */
17911             ts = ts_within_enclosing_non_class;
17912           else if (is_declaration
17913                    && cp_lexer_next_token_is (parser->lexer,
17914                                               CPP_SEMICOLON))
17915             /* This is a `class-key identifier ;' */
17916             ts = ts_current;
17917           else
17918             ts = ts_global;
17919
17920           template_p =
17921             (template_parm_lists_apply
17922              && (cp_parser_next_token_starts_class_definition_p (parser)
17923                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17924           /* An unqualified name was used to reference this type, so
17925              there were no qualifying templates.  */
17926           if (template_parm_lists_apply
17927               && !cp_parser_check_template_parameters (parser,
17928                                                        /*num_templates=*/0,
17929                                                        /*template_id*/false,
17930                                                        token->location,
17931                                                        /*declarator=*/NULL))
17932             return error_mark_node;
17933           type = xref_tag (tag_type, identifier, ts, template_p);
17934         }
17935     }
17936
17937   if (type == error_mark_node)
17938     return error_mark_node;
17939
17940   /* Allow attributes on forward declarations of classes.  */
17941   if (attributes)
17942     {
17943       if (TREE_CODE (type) == TYPENAME_TYPE)
17944         warning (OPT_Wattributes,
17945                  "attributes ignored on uninstantiated type");
17946       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17947                && ! processing_explicit_instantiation)
17948         warning (OPT_Wattributes,
17949                  "attributes ignored on template instantiation");
17950       else if (is_declaration && cp_parser_declares_only_class_p (parser))
17951         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17952       else
17953         warning (OPT_Wattributes,
17954                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17955     }
17956
17957   if (tag_type != enum_type)
17958     {
17959       /* Indicate whether this class was declared as a `class' or as a
17960          `struct'.  */
17961       if (CLASS_TYPE_P (type))
17962         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17963       cp_parser_check_class_key (tag_type, type);
17964     }
17965
17966   /* A "<" cannot follow an elaborated type specifier.  If that
17967      happens, the user was probably trying to form a template-id.  */
17968   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17969                                            token->location);
17970
17971   return type;
17972 }
17973
17974 /* Parse an enum-specifier.
17975
17976    enum-specifier:
17977      enum-head { enumerator-list [opt] }
17978      enum-head { enumerator-list , } [C++0x]
17979
17980    enum-head:
17981      enum-key identifier [opt] enum-base [opt]
17982      enum-key nested-name-specifier identifier enum-base [opt]
17983
17984    enum-key:
17985      enum
17986      enum class   [C++0x]
17987      enum struct  [C++0x]
17988
17989    enum-base:   [C++0x]
17990      : type-specifier-seq
17991
17992    opaque-enum-specifier:
17993      enum-key identifier enum-base [opt] ;
17994
17995    GNU Extensions:
17996      enum-key attributes[opt] identifier [opt] enum-base [opt] 
17997        { enumerator-list [opt] }attributes[opt]
17998      enum-key attributes[opt] identifier [opt] enum-base [opt]
17999        { enumerator-list, }attributes[opt] [C++0x]
18000
18001    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18002    if the token stream isn't an enum-specifier after all.  */
18003
18004 static tree
18005 cp_parser_enum_specifier (cp_parser* parser)
18006 {
18007   tree identifier;
18008   tree type = NULL_TREE;
18009   tree prev_scope;
18010   tree nested_name_specifier = NULL_TREE;
18011   tree attributes;
18012   bool scoped_enum_p = false;
18013   bool has_underlying_type = false;
18014   bool nested_being_defined = false;
18015   bool new_value_list = false;
18016   bool is_new_type = false;
18017   bool is_unnamed = false;
18018   tree underlying_type = NULL_TREE;
18019   cp_token *type_start_token = NULL;
18020   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18021
18022   parser->colon_corrects_to_scope_p = false;
18023
18024   /* Parse tentatively so that we can back up if we don't find a
18025      enum-specifier.  */
18026   cp_parser_parse_tentatively (parser);
18027
18028   /* Caller guarantees that the current token is 'enum', an identifier
18029      possibly follows, and the token after that is an opening brace.
18030      If we don't have an identifier, fabricate an anonymous name for
18031      the enumeration being defined.  */
18032   cp_lexer_consume_token (parser->lexer);
18033
18034   /* Parse the "class" or "struct", which indicates a scoped
18035      enumeration type in C++0x.  */
18036   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18037       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18038     {
18039       if (cxx_dialect < cxx11)
18040         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18041
18042       /* Consume the `struct' or `class' token.  */
18043       cp_lexer_consume_token (parser->lexer);
18044
18045       scoped_enum_p = true;
18046     }
18047
18048   attributes = cp_parser_attributes_opt (parser);
18049
18050   /* Clear the qualification.  */
18051   parser->scope = NULL_TREE;
18052   parser->qualifying_scope = NULL_TREE;
18053   parser->object_scope = NULL_TREE;
18054
18055   /* Figure out in what scope the declaration is being placed.  */
18056   prev_scope = current_scope ();
18057
18058   type_start_token = cp_lexer_peek_token (parser->lexer);
18059
18060   push_deferring_access_checks (dk_no_check);
18061   nested_name_specifier
18062       = cp_parser_nested_name_specifier_opt (parser,
18063                                              /*typename_keyword_p=*/true,
18064                                              /*check_dependency_p=*/false,
18065                                              /*type_p=*/false,
18066                                              /*is_declaration=*/false);
18067
18068   if (nested_name_specifier)
18069     {
18070       tree name;
18071
18072       identifier = cp_parser_identifier (parser);
18073       name =  cp_parser_lookup_name (parser, identifier,
18074                                      enum_type,
18075                                      /*is_template=*/false,
18076                                      /*is_namespace=*/false,
18077                                      /*check_dependency=*/true,
18078                                      /*ambiguous_decls=*/NULL,
18079                                      input_location);
18080       if (name && name != error_mark_node)
18081         {
18082           type = TREE_TYPE (name);
18083           if (TREE_CODE (type) == TYPENAME_TYPE)
18084             {
18085               /* Are template enums allowed in ISO? */
18086               if (template_parm_scope_p ())
18087                 pedwarn (type_start_token->location, OPT_Wpedantic,
18088                          "%qD is an enumeration template", name);
18089               /* ignore a typename reference, for it will be solved by name
18090                  in start_enum.  */
18091               type = NULL_TREE;
18092             }
18093         }
18094       else if (nested_name_specifier == error_mark_node)
18095         /* We already issued an error.  */;
18096       else
18097         {
18098           error_at (type_start_token->location,
18099                     "%qD does not name an enumeration in %qT",
18100                     identifier, nested_name_specifier);
18101           nested_name_specifier = error_mark_node;
18102         }
18103     }
18104   else
18105     {
18106       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18107         identifier = cp_parser_identifier (parser);
18108       else
18109         {
18110           identifier = make_anon_name ();
18111           is_unnamed = true;
18112           if (scoped_enum_p)
18113             error_at (type_start_token->location,
18114                       "unnamed scoped enum is not allowed");
18115         }
18116     }
18117   pop_deferring_access_checks ();
18118
18119   /* Check for the `:' that denotes a specified underlying type in C++0x.
18120      Note that a ':' could also indicate a bitfield width, however.  */
18121   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18122     {
18123       cp_decl_specifier_seq type_specifiers;
18124
18125       /* Consume the `:'.  */
18126       cp_lexer_consume_token (parser->lexer);
18127
18128       /* Parse the type-specifier-seq.  */
18129       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18130                                     /*is_trailing_return=*/false,
18131                                     &type_specifiers);
18132
18133       /* At this point this is surely not elaborated type specifier.  */
18134       if (!cp_parser_parse_definitely (parser))
18135         return NULL_TREE;
18136
18137       if (cxx_dialect < cxx11)
18138         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18139
18140       has_underlying_type = true;
18141
18142       /* If that didn't work, stop.  */
18143       if (type_specifiers.type != error_mark_node)
18144         {
18145           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18146                                             /*initialized=*/0, NULL);
18147           if (underlying_type == error_mark_node
18148               || check_for_bare_parameter_packs (underlying_type))
18149             underlying_type = NULL_TREE;
18150         }
18151     }
18152
18153   /* Look for the `{' but don't consume it yet.  */
18154   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18155     {
18156       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18157         {
18158           cp_parser_error (parser, "expected %<{%>");
18159           if (has_underlying_type)
18160             {
18161               type = NULL_TREE;
18162               goto out;
18163             }
18164         }
18165       /* An opaque-enum-specifier must have a ';' here.  */
18166       if ((scoped_enum_p || underlying_type)
18167           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18168         {
18169           cp_parser_error (parser, "expected %<;%> or %<{%>");
18170           if (has_underlying_type)
18171             {
18172               type = NULL_TREE;
18173               goto out;
18174             }
18175         }
18176     }
18177
18178   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18179     return NULL_TREE;
18180
18181   if (nested_name_specifier)
18182     {
18183       if (CLASS_TYPE_P (nested_name_specifier))
18184         {
18185           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18186           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18187           push_scope (nested_name_specifier);
18188         }
18189       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18190         {
18191           push_nested_namespace (nested_name_specifier);
18192         }
18193     }
18194
18195   /* Issue an error message if type-definitions are forbidden here.  */
18196   if (!cp_parser_check_type_definition (parser))
18197     type = error_mark_node;
18198   else
18199     /* Create the new type.  We do this before consuming the opening
18200        brace so the enum will be recorded as being on the line of its
18201        tag (or the 'enum' keyword, if there is no tag).  */
18202     type = start_enum (identifier, type, underlying_type,
18203                        attributes, scoped_enum_p, &is_new_type);
18204
18205   /* If the next token is not '{' it is an opaque-enum-specifier or an
18206      elaborated-type-specifier.  */
18207   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18208     {
18209       timevar_push (TV_PARSE_ENUM);
18210       if (nested_name_specifier
18211           && nested_name_specifier != error_mark_node)
18212         {
18213           /* The following catches invalid code such as:
18214              enum class S<int>::E { A, B, C }; */
18215           if (!processing_specialization
18216               && CLASS_TYPE_P (nested_name_specifier)
18217               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18218             error_at (type_start_token->location, "cannot add an enumerator "
18219                       "list to a template instantiation");
18220
18221           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18222             {
18223               error_at (type_start_token->location,
18224                         "%<%T::%E%> has not been declared",
18225                         TYPE_CONTEXT (nested_name_specifier),
18226                         nested_name_specifier);
18227               type = error_mark_node;
18228             }
18229           else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18230                    && !CLASS_TYPE_P (nested_name_specifier))
18231             {
18232               error_at (type_start_token->location, "nested name specifier "
18233                         "%qT for enum declaration does not name a class "
18234                         "or namespace", nested_name_specifier);
18235               type = error_mark_node;
18236             }
18237           /* If that scope does not contain the scope in which the
18238              class was originally declared, the program is invalid.  */
18239           else if (prev_scope && !is_ancestor (prev_scope,
18240                                                nested_name_specifier))
18241             {
18242               if (at_namespace_scope_p ())
18243                 error_at (type_start_token->location,
18244                           "declaration of %qD in namespace %qD which does not "
18245                           "enclose %qD",
18246                           type, prev_scope, nested_name_specifier);
18247               else
18248                 error_at (type_start_token->location,
18249                           "declaration of %qD in %qD which does not "
18250                           "enclose %qD",
18251                           type, prev_scope, nested_name_specifier);
18252               type = error_mark_node;
18253             }
18254           /* If that scope is the scope where the declaration is being placed
18255              the program is invalid.  */
18256           else if (CLASS_TYPE_P (nested_name_specifier)
18257                    && CLASS_TYPE_P (prev_scope)
18258                    && same_type_p (nested_name_specifier, prev_scope))
18259             {
18260               permerror (type_start_token->location,
18261                          "extra qualification not allowed");
18262               nested_name_specifier = NULL_TREE;
18263             }
18264         }
18265
18266       if (scoped_enum_p)
18267         begin_scope (sk_scoped_enum, type);
18268
18269       /* Consume the opening brace.  */
18270       matching_braces braces;
18271       braces.consume_open (parser);
18272
18273       if (type == error_mark_node)
18274         ; /* Nothing to add */
18275       else if (OPAQUE_ENUM_P (type)
18276                || (cxx_dialect > cxx98 && processing_specialization))
18277         {
18278           new_value_list = true;
18279           SET_OPAQUE_ENUM_P (type, false);
18280           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18281         }
18282       else
18283         {
18284           error_at (type_start_token->location,
18285                     "multiple definition of %q#T", type);
18286           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18287                   "previous definition here");
18288           type = error_mark_node;
18289         }
18290
18291       if (type == error_mark_node)
18292         cp_parser_skip_to_end_of_block_or_statement (parser);
18293       /* If the next token is not '}', then there are some enumerators.  */
18294       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18295         {
18296           if (is_unnamed && !scoped_enum_p)
18297             pedwarn (type_start_token->location, OPT_Wpedantic,
18298                      "ISO C++ forbids empty unnamed enum");
18299         }
18300       else
18301         cp_parser_enumerator_list (parser, type);
18302
18303       /* Consume the final '}'.  */
18304       braces.require_close (parser);
18305
18306       if (scoped_enum_p)
18307         finish_scope ();
18308       timevar_pop (TV_PARSE_ENUM);
18309     }
18310   else
18311     {
18312       /* If a ';' follows, then it is an opaque-enum-specifier
18313         and additional restrictions apply.  */
18314       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18315         {
18316           if (is_unnamed)
18317             error_at (type_start_token->location,
18318                       "opaque-enum-specifier without name");
18319           else if (nested_name_specifier)
18320             error_at (type_start_token->location,
18321                       "opaque-enum-specifier must use a simple identifier");
18322         }
18323     }
18324
18325   /* Look for trailing attributes to apply to this enumeration, and
18326      apply them if appropriate.  */
18327   if (cp_parser_allow_gnu_extensions_p (parser))
18328     {
18329       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18330       cplus_decl_attributes (&type,
18331                              trailing_attr,
18332                              (int) ATTR_FLAG_TYPE_IN_PLACE);
18333     }
18334
18335   /* Finish up the enumeration.  */
18336   if (type != error_mark_node)
18337     {
18338       if (new_value_list)
18339         finish_enum_value_list (type);
18340       if (is_new_type)
18341         finish_enum (type);
18342     }
18343
18344   if (nested_name_specifier)
18345     {
18346       if (CLASS_TYPE_P (nested_name_specifier))
18347         {
18348           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18349           pop_scope (nested_name_specifier);
18350         }
18351       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18352         {
18353           pop_nested_namespace (nested_name_specifier);
18354         }
18355     }
18356  out:
18357   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18358   return type;
18359 }
18360
18361 /* Parse an enumerator-list.  The enumerators all have the indicated
18362    TYPE.
18363
18364    enumerator-list:
18365      enumerator-definition
18366      enumerator-list , enumerator-definition  */
18367
18368 static void
18369 cp_parser_enumerator_list (cp_parser* parser, tree type)
18370 {
18371   while (true)
18372     {
18373       /* Parse an enumerator-definition.  */
18374       cp_parser_enumerator_definition (parser, type);
18375
18376       /* If the next token is not a ',', we've reached the end of
18377          the list.  */
18378       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18379         break;
18380       /* Otherwise, consume the `,' and keep going.  */
18381       cp_lexer_consume_token (parser->lexer);
18382       /* If the next token is a `}', there is a trailing comma.  */
18383       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18384         {
18385           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18386             pedwarn (input_location, OPT_Wpedantic,
18387                      "comma at end of enumerator list");
18388           break;
18389         }
18390     }
18391 }
18392
18393 /* Parse an enumerator-definition.  The enumerator has the indicated
18394    TYPE.
18395
18396    enumerator-definition:
18397      enumerator
18398      enumerator = constant-expression
18399
18400    enumerator:
18401      identifier
18402
18403    GNU Extensions:
18404
18405    enumerator-definition:
18406      enumerator attributes [opt]
18407      enumerator attributes [opt] = constant-expression  */
18408
18409 static void
18410 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18411 {
18412   tree identifier;
18413   tree value;
18414   location_t loc;
18415
18416   /* Save the input location because we are interested in the location
18417      of the identifier and not the location of the explicit value.  */
18418   loc = cp_lexer_peek_token (parser->lexer)->location;
18419
18420   /* Look for the identifier.  */
18421   identifier = cp_parser_identifier (parser);
18422   if (identifier == error_mark_node)
18423     return;
18424
18425   /* Parse any specified attributes.  */
18426   tree attrs = cp_parser_attributes_opt (parser);
18427
18428   /* If the next token is an '=', then there is an explicit value.  */
18429   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18430     {
18431       /* Consume the `=' token.  */
18432       cp_lexer_consume_token (parser->lexer);
18433       /* Parse the value.  */
18434       value = cp_parser_constant_expression (parser);
18435     }
18436   else
18437     value = NULL_TREE;
18438
18439   /* If we are processing a template, make sure the initializer of the
18440      enumerator doesn't contain any bare template parameter pack.  */
18441   if (check_for_bare_parameter_packs (value))
18442     value = error_mark_node;
18443
18444   /* Create the enumerator.  */
18445   build_enumerator (identifier, value, type, attrs, loc);
18446 }
18447
18448 /* Parse a namespace-name.
18449
18450    namespace-name:
18451      original-namespace-name
18452      namespace-alias
18453
18454    Returns the NAMESPACE_DECL for the namespace.  */
18455
18456 static tree
18457 cp_parser_namespace_name (cp_parser* parser)
18458 {
18459   tree identifier;
18460   tree namespace_decl;
18461
18462   cp_token *token = cp_lexer_peek_token (parser->lexer);
18463
18464   /* Get the name of the namespace.  */
18465   identifier = cp_parser_identifier (parser);
18466   if (identifier == error_mark_node)
18467     return error_mark_node;
18468
18469   /* Look up the identifier in the currently active scope.  Look only
18470      for namespaces, due to:
18471
18472        [basic.lookup.udir]
18473
18474        When looking up a namespace-name in a using-directive or alias
18475        definition, only namespace names are considered.
18476
18477      And:
18478
18479        [basic.lookup.qual]
18480
18481        During the lookup of a name preceding the :: scope resolution
18482        operator, object, function, and enumerator names are ignored.
18483
18484      (Note that cp_parser_qualifying_entity only calls this
18485      function if the token after the name is the scope resolution
18486      operator.)  */
18487   namespace_decl = cp_parser_lookup_name (parser, identifier,
18488                                           none_type,
18489                                           /*is_template=*/false,
18490                                           /*is_namespace=*/true,
18491                                           /*check_dependency=*/true,
18492                                           /*ambiguous_decls=*/NULL,
18493                                           token->location);
18494   /* If it's not a namespace, issue an error.  */
18495   if (namespace_decl == error_mark_node
18496       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18497     {
18498       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18499         {
18500           error_at (token->location, "%qD is not a namespace-name", identifier);
18501           if (namespace_decl == error_mark_node
18502               && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18503             suggest_alternative_in_explicit_scope (token->location, identifier,
18504                                                    parser->scope);
18505         }
18506       cp_parser_error (parser, "expected namespace-name");
18507       namespace_decl = error_mark_node;
18508     }
18509
18510   return namespace_decl;
18511 }
18512
18513 /* Parse a namespace-definition.
18514
18515    namespace-definition:
18516      named-namespace-definition
18517      unnamed-namespace-definition
18518
18519    named-namespace-definition:
18520      original-namespace-definition
18521      extension-namespace-definition
18522
18523    original-namespace-definition:
18524      namespace identifier { namespace-body }
18525
18526    extension-namespace-definition:
18527      namespace original-namespace-name { namespace-body }
18528
18529    unnamed-namespace-definition:
18530      namespace { namespace-body } */
18531
18532 static void
18533 cp_parser_namespace_definition (cp_parser* parser)
18534 {
18535   tree identifier;
18536   int nested_definition_count = 0;
18537
18538   cp_ensure_no_omp_declare_simd (parser);
18539   cp_ensure_no_oacc_routine (parser);
18540
18541   bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18542
18543   if (is_inline)
18544     {
18545       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18546       cp_lexer_consume_token (parser->lexer);
18547     }
18548
18549   /* Look for the `namespace' keyword.  */
18550   cp_token* token
18551     = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18552
18553   /* Parse any specified attributes before the identifier.  */
18554   tree attribs = cp_parser_attributes_opt (parser);
18555
18556   for (;;)
18557     {
18558       identifier = NULL_TREE;
18559       
18560       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18561         {
18562           identifier = cp_parser_identifier (parser);
18563
18564           /* Parse any attributes specified after the identifier.  */
18565           attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18566         }
18567
18568       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18569         break;
18570   
18571       if (!nested_definition_count && cxx_dialect < cxx17)
18572         pedwarn (input_location, OPT_Wpedantic,
18573                  "nested namespace definitions only available with "
18574                  "-std=c++17 or -std=gnu++17");
18575
18576       /* Nested namespace names can create new namespaces (unlike
18577          other qualified-ids).  */
18578       if (int count = identifier ? push_namespace (identifier) : 0)
18579         nested_definition_count += count;
18580       else
18581         cp_parser_error (parser, "nested namespace name required");
18582       cp_lexer_consume_token (parser->lexer);
18583     }
18584
18585   if (nested_definition_count && !identifier)
18586     cp_parser_error (parser, "namespace name required");
18587   
18588   if (nested_definition_count && attribs)
18589     error_at (token->location,
18590               "a nested namespace definition cannot have attributes");
18591   if (nested_definition_count && is_inline)
18592     error_at (token->location,
18593               "a nested namespace definition cannot be inline");
18594
18595   /* Start the namespace.  */
18596   nested_definition_count += push_namespace (identifier, is_inline);
18597
18598   bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18599
18600   warning  (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18601
18602   /* Look for the `{' to validate starting the namespace.  */
18603   matching_braces braces;
18604   if (braces.require_open (parser))
18605     {
18606       /* Parse the body of the namespace.  */
18607       cp_parser_namespace_body (parser);
18608
18609       /* Look for the final `}'.  */
18610       braces.require_close (parser);
18611     }
18612
18613   if (has_visibility)
18614     pop_visibility (1);
18615
18616   /* Pop the nested namespace definitions.  */
18617   while (nested_definition_count--)
18618     pop_namespace ();
18619 }
18620
18621 /* Parse a namespace-body.
18622
18623    namespace-body:
18624      declaration-seq [opt]  */
18625
18626 static void
18627 cp_parser_namespace_body (cp_parser* parser)
18628 {
18629   cp_parser_declaration_seq_opt (parser);
18630 }
18631
18632 /* Parse a namespace-alias-definition.
18633
18634    namespace-alias-definition:
18635      namespace identifier = qualified-namespace-specifier ;  */
18636
18637 static void
18638 cp_parser_namespace_alias_definition (cp_parser* parser)
18639 {
18640   tree identifier;
18641   tree namespace_specifier;
18642
18643   cp_token *token = cp_lexer_peek_token (parser->lexer);
18644
18645   /* Look for the `namespace' keyword.  */
18646   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18647   /* Look for the identifier.  */
18648   identifier = cp_parser_identifier (parser);
18649   if (identifier == error_mark_node)
18650     return;
18651   /* Look for the `=' token.  */
18652   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18653       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
18654     {
18655       error_at (token->location, "%<namespace%> definition is not allowed here");
18656       /* Skip the definition.  */
18657       cp_lexer_consume_token (parser->lexer);
18658       if (cp_parser_skip_to_closing_brace (parser))
18659         cp_lexer_consume_token (parser->lexer);
18660       return;
18661     }
18662   cp_parser_require (parser, CPP_EQ, RT_EQ);
18663   /* Look for the qualified-namespace-specifier.  */
18664   namespace_specifier
18665     = cp_parser_qualified_namespace_specifier (parser);
18666   /* Look for the `;' token.  */
18667   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18668
18669   /* Register the alias in the symbol table.  */
18670   do_namespace_alias (identifier, namespace_specifier);
18671 }
18672
18673 /* Parse a qualified-namespace-specifier.
18674
18675    qualified-namespace-specifier:
18676      :: [opt] nested-name-specifier [opt] namespace-name
18677
18678    Returns a NAMESPACE_DECL corresponding to the specified
18679    namespace.  */
18680
18681 static tree
18682 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18683 {
18684   /* Look for the optional `::'.  */
18685   cp_parser_global_scope_opt (parser,
18686                               /*current_scope_valid_p=*/false);
18687
18688   /* Look for the optional nested-name-specifier.  */
18689   cp_parser_nested_name_specifier_opt (parser,
18690                                        /*typename_keyword_p=*/false,
18691                                        /*check_dependency_p=*/true,
18692                                        /*type_p=*/false,
18693                                        /*is_declaration=*/true);
18694
18695   return cp_parser_namespace_name (parser);
18696 }
18697
18698 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18699    access declaration.
18700
18701    using-declaration:
18702      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18703      using :: unqualified-id ;  
18704
18705    access-declaration:
18706      qualified-id ;  
18707
18708    */
18709
18710 static bool
18711 cp_parser_using_declaration (cp_parser* parser, 
18712                              bool access_declaration_p)
18713 {
18714   cp_token *token;
18715   bool typename_p = false;
18716   bool global_scope_p;
18717   tree decl;
18718   tree identifier;
18719   tree qscope;
18720   int oldcount = errorcount;
18721   cp_token *diag_token = NULL;
18722
18723   if (access_declaration_p)
18724     {
18725       diag_token = cp_lexer_peek_token (parser->lexer);
18726       cp_parser_parse_tentatively (parser);
18727     }
18728   else
18729     {
18730       /* Look for the `using' keyword.  */
18731       cp_parser_require_keyword (parser, RID_USING, RT_USING);
18732       
18733  again:
18734       /* Peek at the next token.  */
18735       token = cp_lexer_peek_token (parser->lexer);
18736       /* See if it's `typename'.  */
18737       if (token->keyword == RID_TYPENAME)
18738         {
18739           /* Remember that we've seen it.  */
18740           typename_p = true;
18741           /* Consume the `typename' token.  */
18742           cp_lexer_consume_token (parser->lexer);
18743         }
18744     }
18745
18746   /* Look for the optional global scope qualification.  */
18747   global_scope_p
18748     = (cp_parser_global_scope_opt (parser,
18749                                    /*current_scope_valid_p=*/false)
18750        != NULL_TREE);
18751
18752   /* If we saw `typename', or didn't see `::', then there must be a
18753      nested-name-specifier present.  */
18754   if (typename_p || !global_scope_p)
18755     {
18756       qscope = cp_parser_nested_name_specifier (parser, typename_p,
18757                                                 /*check_dependency_p=*/true,
18758                                                 /*type_p=*/false,
18759                                                 /*is_declaration=*/true);
18760       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18761         {
18762           cp_parser_skip_to_end_of_block_or_statement (parser);
18763           return false;
18764         }
18765     }
18766   /* Otherwise, we could be in either of the two productions.  In that
18767      case, treat the nested-name-specifier as optional.  */
18768   else
18769     qscope = cp_parser_nested_name_specifier_opt (parser,
18770                                                   /*typename_keyword_p=*/false,
18771                                                   /*check_dependency_p=*/true,
18772                                                   /*type_p=*/false,
18773                                                   /*is_declaration=*/true);
18774   if (!qscope)
18775     qscope = global_namespace;
18776   else if (UNSCOPED_ENUM_P (qscope))
18777     qscope = CP_TYPE_CONTEXT (qscope);
18778
18779   if (access_declaration_p && cp_parser_error_occurred (parser))
18780     /* Something has already gone wrong; there's no need to parse
18781        further.  Since an error has occurred, the return value of
18782        cp_parser_parse_definitely will be false, as required.  */
18783     return cp_parser_parse_definitely (parser);
18784
18785   token = cp_lexer_peek_token (parser->lexer);
18786   /* Parse the unqualified-id.  */
18787   identifier = cp_parser_unqualified_id (parser,
18788                                          /*template_keyword_p=*/false,
18789                                          /*check_dependency_p=*/true,
18790                                          /*declarator_p=*/true,
18791                                          /*optional_p=*/false);
18792
18793   if (access_declaration_p)
18794     {
18795       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18796         cp_parser_simulate_error (parser);
18797       if (!cp_parser_parse_definitely (parser))
18798         return false;
18799     }
18800   else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18801     {
18802       cp_token *ell = cp_lexer_consume_token (parser->lexer);
18803       if (cxx_dialect < cxx17
18804           && !in_system_header_at (ell->location))
18805         pedwarn (ell->location, 0,
18806                  "pack expansion in using-declaration only available "
18807                  "with -std=c++17 or -std=gnu++17");
18808       qscope = make_pack_expansion (qscope);
18809     }
18810
18811   /* The function we call to handle a using-declaration is different
18812      depending on what scope we are in.  */
18813   if (qscope == error_mark_node || identifier == error_mark_node)
18814     ;
18815   else if (!identifier_p (identifier)
18816            && TREE_CODE (identifier) != BIT_NOT_EXPR)
18817     /* [namespace.udecl]
18818
18819        A using declaration shall not name a template-id.  */
18820     error_at (token->location,
18821               "a template-id may not appear in a using-declaration");
18822   else
18823     {
18824       if (at_class_scope_p ())
18825         {
18826           /* Create the USING_DECL.  */
18827           decl = do_class_using_decl (qscope, identifier);
18828
18829           if (decl && typename_p)
18830             USING_DECL_TYPENAME_P (decl) = 1;
18831
18832           if (check_for_bare_parameter_packs (decl))
18833             {
18834               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18835               return false;
18836             }
18837           else
18838             /* Add it to the list of members in this class.  */
18839             finish_member_declaration (decl);
18840         }
18841       else
18842         {
18843           decl = cp_parser_lookup_name_simple (parser,
18844                                                identifier,
18845                                                token->location);
18846           if (decl == error_mark_node)
18847             cp_parser_name_lookup_error (parser, identifier,
18848                                          decl, NLE_NULL,
18849                                          token->location);
18850           else if (check_for_bare_parameter_packs (decl))
18851             {
18852               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18853               return false;
18854             }
18855           else if (!at_namespace_scope_p ())
18856             finish_local_using_decl (decl, qscope, identifier);
18857           else
18858             finish_namespace_using_decl (decl, qscope, identifier);
18859         }
18860     }
18861
18862   if (!access_declaration_p
18863       && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18864     {
18865       cp_token *comma = cp_lexer_consume_token (parser->lexer);
18866       if (cxx_dialect < cxx17)
18867         pedwarn (comma->location, 0,
18868                  "comma-separated list in using-declaration only available "
18869                  "with -std=c++17 or -std=gnu++17");
18870       goto again;
18871     }
18872
18873   /* Look for the final `;'.  */
18874   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18875
18876   if (access_declaration_p && errorcount == oldcount)
18877     warning_at (diag_token->location, OPT_Wdeprecated,
18878                 "access declarations are deprecated "
18879                 "in favour of using-declarations; "
18880                 "suggestion: add the %<using%> keyword");
18881
18882   return true;
18883 }
18884
18885 /* Parse an alias-declaration.
18886
18887    alias-declaration:
18888      using identifier attribute-specifier-seq [opt] = type-id  */
18889
18890 static tree
18891 cp_parser_alias_declaration (cp_parser* parser)
18892 {
18893   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18894   location_t id_location;
18895   cp_declarator *declarator;
18896   cp_decl_specifier_seq decl_specs;
18897   bool member_p;
18898   const char *saved_message = NULL;
18899
18900   /* Look for the `using' keyword.  */
18901   cp_token *using_token
18902     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18903   if (using_token == NULL)
18904     return error_mark_node;
18905
18906   id_location = cp_lexer_peek_token (parser->lexer)->location;
18907   id = cp_parser_identifier (parser);
18908   if (id == error_mark_node)
18909     return error_mark_node;
18910
18911   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18912   attributes = cp_parser_attributes_opt (parser);
18913   if (attributes == error_mark_node)
18914     return error_mark_node;
18915
18916   cp_parser_require (parser, CPP_EQ, RT_EQ);
18917
18918   if (cp_parser_error_occurred (parser))
18919     return error_mark_node;
18920
18921   cp_parser_commit_to_tentative_parse (parser);
18922
18923   /* Now we are going to parse the type-id of the declaration.  */
18924
18925   /*
18926     [dcl.type]/3 says:
18927
18928         "A type-specifier-seq shall not define a class or enumeration
18929          unless it appears in the type-id of an alias-declaration (7.1.3) that
18930          is not the declaration of a template-declaration."
18931
18932     In other words, if we currently are in an alias template, the
18933     type-id should not define a type.
18934
18935     So let's set parser->type_definition_forbidden_message in that
18936     case; cp_parser_check_type_definition (called by
18937     cp_parser_class_specifier) will then emit an error if a type is
18938     defined in the type-id.  */
18939   if (parser->num_template_parameter_lists)
18940     {
18941       saved_message = parser->type_definition_forbidden_message;
18942       parser->type_definition_forbidden_message =
18943         G_("types may not be defined in alias template declarations");
18944     }
18945
18946   type = cp_parser_type_id (parser);
18947
18948   /* Restore the error message if need be.  */
18949   if (parser->num_template_parameter_lists)
18950     parser->type_definition_forbidden_message = saved_message;
18951
18952   if (type == error_mark_node
18953       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18954     {
18955       cp_parser_skip_to_end_of_block_or_statement (parser);
18956       return error_mark_node;
18957     }
18958
18959   /* A typedef-name can also be introduced by an alias-declaration. The
18960      identifier following the using keyword becomes a typedef-name. It has
18961      the same semantics as if it were introduced by the typedef
18962      specifier. In particular, it does not define a new type and it shall
18963      not appear in the type-id.  */
18964
18965   clear_decl_specs (&decl_specs);
18966   decl_specs.type = type;
18967   if (attributes != NULL_TREE)
18968     {
18969       decl_specs.attributes = attributes;
18970       set_and_check_decl_spec_loc (&decl_specs,
18971                                    ds_attribute,
18972                                    attrs_token);
18973     }
18974   set_and_check_decl_spec_loc (&decl_specs,
18975                                ds_typedef,
18976                                using_token);
18977   set_and_check_decl_spec_loc (&decl_specs,
18978                                ds_alias,
18979                                using_token);
18980
18981   if (parser->num_template_parameter_lists
18982       && !cp_parser_check_template_parameters (parser,
18983                                                /*num_templates=*/0,
18984                                                /*template_id*/false,
18985                                                id_location,
18986                                                /*declarator=*/NULL))
18987     return error_mark_node;
18988
18989   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18990   declarator->id_loc = id_location;
18991
18992   member_p = at_class_scope_p ();
18993   if (member_p)
18994     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18995                       NULL_TREE, attributes);
18996   else
18997     decl = start_decl (declarator, &decl_specs, 0,
18998                        attributes, NULL_TREE, &pushed_scope);
18999   if (decl == error_mark_node)
19000     return decl;
19001
19002   // Attach constraints to the alias declaration.
19003   if (flag_concepts && current_template_parms)
19004     {
19005       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19006       tree constr = build_constraints (reqs, NULL_TREE);
19007       set_constraints (decl, constr);
19008     }
19009
19010   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19011
19012   if (pushed_scope)
19013     pop_scope (pushed_scope);
19014
19015   /* If decl is a template, return its TEMPLATE_DECL so that it gets
19016      added into the symbol table; otherwise, return the TYPE_DECL.  */
19017   if (DECL_LANG_SPECIFIC (decl)
19018       && DECL_TEMPLATE_INFO (decl)
19019       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19020     {
19021       decl = DECL_TI_TEMPLATE (decl);
19022       if (member_p)
19023         check_member_template (decl);
19024     }
19025
19026   return decl;
19027 }
19028
19029 /* Parse a using-directive.
19030
19031    using-directive:
19032      using namespace :: [opt] nested-name-specifier [opt]
19033        namespace-name ;  */
19034
19035 static void
19036 cp_parser_using_directive (cp_parser* parser)
19037 {
19038   tree namespace_decl;
19039   tree attribs;
19040
19041   /* Look for the `using' keyword.  */
19042   cp_parser_require_keyword (parser, RID_USING, RT_USING);
19043   /* And the `namespace' keyword.  */
19044   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19045   /* Look for the optional `::' operator.  */
19046   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19047   /* And the optional nested-name-specifier.  */
19048   cp_parser_nested_name_specifier_opt (parser,
19049                                        /*typename_keyword_p=*/false,
19050                                        /*check_dependency_p=*/true,
19051                                        /*type_p=*/false,
19052                                        /*is_declaration=*/true);
19053   /* Get the namespace being used.  */
19054   namespace_decl = cp_parser_namespace_name (parser);
19055   /* And any specified attributes.  */
19056   attribs = cp_parser_attributes_opt (parser);
19057
19058   /* Update the symbol table.  */
19059   if (namespace_bindings_p ())
19060     finish_namespace_using_directive (namespace_decl, attribs);
19061   else
19062     finish_local_using_directive (namespace_decl, attribs);
19063
19064   /* Look for the final `;'.  */
19065   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19066 }
19067
19068 /* Parse an asm-definition.
19069
19070   asm-qualifier:
19071     volatile
19072     inline
19073     goto
19074
19075   asm-qualifier-list:
19076     asm-qualifier
19077     asm-qualifier-list asm-qualifier
19078
19079    asm-definition:
19080      asm ( string-literal ) ;
19081
19082    GNU Extension:
19083
19084    asm-definition:
19085      asm asm-qualifier-list [opt] ( string-literal ) ;
19086      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19087      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19088                                     : asm-operand-list [opt] ) ;
19089      asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19090                                     : asm-operand-list [opt]
19091                           : asm-clobber-list [opt] ) ;
19092      asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19093                                     : asm-clobber-list [opt]
19094                                     : asm-goto-list ) ;
19095
19096   The form with asm-goto-list is valid if and only if the asm-qualifier-list
19097   contains goto, and is the only allowed form in that case.  No duplicates are
19098   allowed in an asm-qualifier-list.  */
19099
19100 static void
19101 cp_parser_asm_definition (cp_parser* parser)
19102 {
19103   tree string;
19104   tree outputs = NULL_TREE;
19105   tree inputs = NULL_TREE;
19106   tree clobbers = NULL_TREE;
19107   tree labels = NULL_TREE;
19108   tree asm_stmt;
19109   bool extended_p = false;
19110   bool invalid_inputs_p = false;
19111   bool invalid_outputs_p = false;
19112   required_token missing = RT_NONE;
19113
19114   /* Look for the `asm' keyword.  */
19115   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19116
19117   if (parser->in_function_body
19118       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19119     {
19120       error ("%<asm%> in %<constexpr%> function");
19121       cp_function_chain->invalid_constexpr = true;
19122     }
19123
19124   /* Handle the asm-qualifier-list.  */
19125   location_t volatile_loc = UNKNOWN_LOCATION;
19126   location_t inline_loc = UNKNOWN_LOCATION;
19127   location_t goto_loc = UNKNOWN_LOCATION;
19128
19129   if (cp_parser_allow_gnu_extensions_p (parser) && parser->in_function_body)
19130     for (;;)
19131       {
19132         cp_token *token = cp_lexer_peek_token (parser->lexer);
19133         location_t loc = token->location;
19134         switch (cp_lexer_peek_token (parser->lexer)->keyword)
19135           {
19136           case RID_VOLATILE:
19137             if (volatile_loc)
19138               {
19139                 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19140                 inform (volatile_loc, "first seen here");
19141               }
19142             else
19143               volatile_loc = loc;
19144             cp_lexer_consume_token (parser->lexer);
19145             continue;
19146
19147           case RID_INLINE:
19148             if (inline_loc)
19149               {
19150                 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19151                 inform (inline_loc, "first seen here");
19152               }
19153             else
19154               inline_loc = loc;
19155             cp_lexer_consume_token (parser->lexer);
19156             continue;
19157
19158           case RID_GOTO:
19159             if (goto_loc)
19160               {
19161                 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19162                 inform (goto_loc, "first seen here");
19163               }
19164             else
19165               goto_loc = loc;
19166             cp_lexer_consume_token (parser->lexer);
19167             continue;
19168
19169           case RID_CONST:
19170           case RID_RESTRICT:
19171             error_at (loc, "%qT is not an asm qualifier", token->u.value);
19172             cp_lexer_consume_token (parser->lexer);
19173             continue;
19174
19175           default:
19176             break;
19177           }
19178         break;
19179       }
19180
19181   bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19182   bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19183   bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19184
19185   /* Look for the opening `('.  */
19186   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19187     return;
19188   /* Look for the string.  */
19189   string = cp_parser_string_literal (parser, false, false);
19190   if (string == error_mark_node)
19191     {
19192       cp_parser_skip_to_closing_parenthesis (parser, true, false,
19193                                              /*consume_paren=*/true);
19194       return;
19195     }
19196
19197   /* If we're allowing GNU extensions, check for the extended assembly
19198      syntax.  Unfortunately, the `:' tokens need not be separated by
19199      a space in C, and so, for compatibility, we tolerate that here
19200      too.  Doing that means that we have to treat the `::' operator as
19201      two `:' tokens.  */
19202   if (cp_parser_allow_gnu_extensions_p (parser)
19203       && parser->in_function_body
19204       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19205           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19206     {
19207       bool inputs_p = false;
19208       bool clobbers_p = false;
19209       bool labels_p = false;
19210
19211       /* The extended syntax was used.  */
19212       extended_p = true;
19213
19214       /* Look for outputs.  */
19215       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19216         {
19217           /* Consume the `:'.  */
19218           cp_lexer_consume_token (parser->lexer);
19219           /* Parse the output-operands.  */
19220           if (cp_lexer_next_token_is_not (parser->lexer,
19221                                           CPP_COLON)
19222               && cp_lexer_next_token_is_not (parser->lexer,
19223                                              CPP_SCOPE)
19224               && cp_lexer_next_token_is_not (parser->lexer,
19225                                              CPP_CLOSE_PAREN)
19226               && !goto_p)
19227             {
19228               outputs = cp_parser_asm_operand_list (parser);
19229               if (outputs == error_mark_node)
19230                 invalid_outputs_p = true;
19231             }
19232         }
19233       /* If the next token is `::', there are no outputs, and the
19234          next token is the beginning of the inputs.  */
19235       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19236         /* The inputs are coming next.  */
19237         inputs_p = true;
19238
19239       /* Look for inputs.  */
19240       if (inputs_p
19241           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19242         {
19243           /* Consume the `:' or `::'.  */
19244           cp_lexer_consume_token (parser->lexer);
19245           /* Parse the output-operands.  */
19246           if (cp_lexer_next_token_is_not (parser->lexer,
19247                                           CPP_COLON)
19248               && cp_lexer_next_token_is_not (parser->lexer,
19249                                              CPP_SCOPE)
19250               && cp_lexer_next_token_is_not (parser->lexer,
19251                                              CPP_CLOSE_PAREN))
19252             {
19253               inputs = cp_parser_asm_operand_list (parser);
19254               if (inputs == error_mark_node)
19255                 invalid_inputs_p = true;
19256             }
19257         }
19258       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19259         /* The clobbers are coming next.  */
19260         clobbers_p = true;
19261
19262       /* Look for clobbers.  */
19263       if (clobbers_p
19264           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19265         {
19266           clobbers_p = true;
19267           /* Consume the `:' or `::'.  */
19268           cp_lexer_consume_token (parser->lexer);
19269           /* Parse the clobbers.  */
19270           if (cp_lexer_next_token_is_not (parser->lexer,
19271                                           CPP_COLON)
19272               && cp_lexer_next_token_is_not (parser->lexer,
19273                                              CPP_CLOSE_PAREN))
19274             clobbers = cp_parser_asm_clobber_list (parser);
19275         }
19276       else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19277         /* The labels are coming next.  */
19278         labels_p = true;
19279
19280       /* Look for labels.  */
19281       if (labels_p
19282           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19283         {
19284           labels_p = true;
19285           /* Consume the `:' or `::'.  */
19286           cp_lexer_consume_token (parser->lexer);
19287           /* Parse the labels.  */
19288           labels = cp_parser_asm_label_list (parser);
19289         }
19290
19291       if (goto_p && !labels_p)
19292         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19293     }
19294   else if (goto_p)
19295     missing = RT_COLON_SCOPE;
19296
19297   /* Look for the closing `)'.  */
19298   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19299                           missing ? missing : RT_CLOSE_PAREN))
19300     cp_parser_skip_to_closing_parenthesis (parser, true, false,
19301                                            /*consume_paren=*/true);
19302   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19303
19304   if (!invalid_inputs_p && !invalid_outputs_p)
19305     {
19306       /* Create the ASM_EXPR.  */
19307       if (parser->in_function_body)
19308         {
19309           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19310                                       inputs, clobbers, labels, inline_p);
19311           /* If the extended syntax was not used, mark the ASM_EXPR.  */
19312           if (!extended_p)
19313             {
19314               tree temp = asm_stmt;
19315               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19316                 temp = TREE_OPERAND (temp, 0);
19317
19318               ASM_INPUT_P (temp) = 1;
19319             }
19320         }
19321       else
19322         symtab->finalize_toplevel_asm (string);
19323     }
19324 }
19325
19326 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19327    type that comes from the decl-specifier-seq.  */
19328
19329 static tree
19330 strip_declarator_types (tree type, cp_declarator *declarator)
19331 {
19332   for (cp_declarator *d = declarator; d;)
19333     switch (d->kind)
19334       {
19335       case cdk_id:
19336       case cdk_decomp:
19337       case cdk_error:
19338         d = NULL;
19339         break;
19340
19341       default:
19342         if (TYPE_PTRMEMFUNC_P (type))
19343           type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19344         type = TREE_TYPE (type);
19345         d = d->declarator;
19346         break;
19347       }
19348
19349   return type;
19350 }
19351
19352 /* Declarators [gram.dcl.decl] */
19353
19354 /* Parse an init-declarator.
19355
19356    init-declarator:
19357      declarator initializer [opt]
19358
19359    GNU Extension:
19360
19361    init-declarator:
19362      declarator asm-specification [opt] attributes [opt] initializer [opt]
19363
19364    function-definition:
19365      decl-specifier-seq [opt] declarator ctor-initializer [opt]
19366        function-body
19367      decl-specifier-seq [opt] declarator function-try-block
19368
19369    GNU Extension:
19370
19371    function-definition:
19372      __extension__ function-definition
19373
19374    TM Extension:
19375
19376    function-definition:
19377      decl-specifier-seq [opt] declarator function-transaction-block
19378
19379    The DECL_SPECIFIERS apply to this declarator.  Returns a
19380    representation of the entity declared.  If MEMBER_P is TRUE, then
19381    this declarator appears in a class scope.  The new DECL created by
19382    this declarator is returned.
19383
19384    The CHECKS are access checks that should be performed once we know
19385    what entity is being declared (and, therefore, what classes have
19386    befriended it).
19387
19388    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19389    for a function-definition here as well.  If the declarator is a
19390    declarator for a function-definition, *FUNCTION_DEFINITION_P will
19391    be TRUE upon return.  By that point, the function-definition will
19392    have been completely parsed.
19393
19394    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19395    is FALSE.
19396
19397    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19398    parsed declaration if it is an uninitialized single declarator not followed
19399    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19400    if present, will not be consumed.  If returned, this declarator will be
19401    created with SD_INITIALIZED but will not call cp_finish_decl.
19402
19403    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19404    and there is an initializer, the pointed location_t is set to the
19405    location of the '=' or `(', or '{' in C++11 token introducing the
19406    initializer.  */
19407
19408 static tree
19409 cp_parser_init_declarator (cp_parser* parser,
19410                            cp_decl_specifier_seq *decl_specifiers,
19411                            vec<deferred_access_check, va_gc> *checks,
19412                            bool function_definition_allowed_p,
19413                            bool member_p,
19414                            int declares_class_or_enum,
19415                            bool* function_definition_p,
19416                            tree* maybe_range_for_decl,
19417                            location_t* init_loc,
19418                            tree* auto_result)
19419 {
19420   cp_token *token = NULL, *asm_spec_start_token = NULL,
19421            *attributes_start_token = NULL;
19422   cp_declarator *declarator;
19423   tree prefix_attributes;
19424   tree attributes = NULL;
19425   tree asm_specification;
19426   tree initializer;
19427   tree decl = NULL_TREE;
19428   tree scope;
19429   int is_initialized;
19430   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
19431      initialized with "= ..", CPP_OPEN_PAREN if initialized with
19432      "(...)".  */
19433   enum cpp_ttype initialization_kind;
19434   bool is_direct_init = false;
19435   bool is_non_constant_init;
19436   int ctor_dtor_or_conv_p;
19437   bool friend_p = cp_parser_friend_p (decl_specifiers);
19438   tree pushed_scope = NULL_TREE;
19439   bool range_for_decl_p = false;
19440   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19441   location_t tmp_init_loc = UNKNOWN_LOCATION;
19442
19443   /* Gather the attributes that were provided with the
19444      decl-specifiers.  */
19445   prefix_attributes = decl_specifiers->attributes;
19446
19447   /* Assume that this is not the declarator for a function
19448      definition.  */
19449   if (function_definition_p)
19450     *function_definition_p = false;
19451
19452   /* Default arguments are only permitted for function parameters.  */
19453   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19454     parser->default_arg_ok_p = false;
19455
19456   /* Defer access checks while parsing the declarator; we cannot know
19457      what names are accessible until we know what is being
19458      declared.  */
19459   resume_deferring_access_checks ();
19460
19461   token = cp_lexer_peek_token (parser->lexer);
19462
19463   /* Parse the declarator.  */
19464   declarator
19465     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19466                             &ctor_dtor_or_conv_p,
19467                             /*parenthesized_p=*/NULL,
19468                             member_p, friend_p);
19469   /* Gather up the deferred checks.  */
19470   stop_deferring_access_checks ();
19471
19472   parser->default_arg_ok_p = saved_default_arg_ok_p;
19473
19474   /* If the DECLARATOR was erroneous, there's no need to go
19475      further.  */
19476   if (declarator == cp_error_declarator)
19477     return error_mark_node;
19478
19479   /* Check that the number of template-parameter-lists is OK.  */
19480   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19481                                                        token->location))
19482     return error_mark_node;
19483
19484   if (declares_class_or_enum & 2)
19485     cp_parser_check_for_definition_in_return_type (declarator,
19486                                                    decl_specifiers->type,
19487                                                    decl_specifiers->locations[ds_type_spec]);
19488
19489   /* Figure out what scope the entity declared by the DECLARATOR is
19490      located in.  `grokdeclarator' sometimes changes the scope, so
19491      we compute it now.  */
19492   scope = get_scope_of_declarator (declarator);
19493
19494   /* Perform any lookups in the declared type which were thought to be
19495      dependent, but are not in the scope of the declarator.  */
19496   decl_specifiers->type
19497     = maybe_update_decl_type (decl_specifiers->type, scope);
19498
19499   /* If we're allowing GNU extensions, look for an
19500      asm-specification.  */
19501   if (cp_parser_allow_gnu_extensions_p (parser))
19502     {
19503       /* Look for an asm-specification.  */
19504       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19505       asm_specification = cp_parser_asm_specification_opt (parser);
19506     }
19507   else
19508     asm_specification = NULL_TREE;
19509
19510   /* Look for attributes.  */
19511   attributes_start_token = cp_lexer_peek_token (parser->lexer);
19512   attributes = cp_parser_attributes_opt (parser);
19513
19514   /* Peek at the next token.  */
19515   token = cp_lexer_peek_token (parser->lexer);
19516
19517   bool bogus_implicit_tmpl = false;
19518
19519   if (function_declarator_p (declarator))
19520     {
19521       /* Handle C++17 deduction guides.  */
19522       if (!decl_specifiers->type
19523           && ctor_dtor_or_conv_p <= 0
19524           && cxx_dialect >= cxx17)
19525         {
19526           cp_declarator *id = get_id_declarator (declarator);
19527           tree name = id->u.id.unqualified_name;
19528           parser->scope = id->u.id.qualifying_scope;
19529           tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19530           if (tmpl
19531               && (DECL_CLASS_TEMPLATE_P (tmpl)
19532                   || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19533             {
19534               id->u.id.unqualified_name = dguide_name (tmpl);
19535               id->u.id.sfk = sfk_deduction_guide;
19536               ctor_dtor_or_conv_p = 1;
19537             }
19538         }
19539
19540       /* Check to see if the token indicates the start of a
19541          function-definition.  */
19542       if (cp_parser_token_starts_function_definition_p (token))
19543         {
19544           if (!function_definition_allowed_p)
19545             {
19546               /* If a function-definition should not appear here, issue an
19547                  error message.  */
19548               cp_parser_error (parser,
19549                                "a function-definition is not allowed here");
19550               return error_mark_node;
19551             }
19552
19553           location_t func_brace_location
19554             = cp_lexer_peek_token (parser->lexer)->location;
19555
19556           /* Neither attributes nor an asm-specification are allowed
19557              on a function-definition.  */
19558           if (asm_specification)
19559             error_at (asm_spec_start_token->location,
19560                       "an asm-specification is not allowed "
19561                       "on a function-definition");
19562           if (attributes)
19563             error_at (attributes_start_token->location,
19564                       "attributes are not allowed "
19565                       "on a function-definition");
19566           /* This is a function-definition.  */
19567           *function_definition_p = true;
19568
19569           /* Parse the function definition.  */
19570           if (member_p)
19571             decl = cp_parser_save_member_function_body (parser,
19572                                                         decl_specifiers,
19573                                                         declarator,
19574                                                         prefix_attributes);
19575           else
19576             decl =
19577               (cp_parser_function_definition_from_specifiers_and_declarator
19578                (parser, decl_specifiers, prefix_attributes, declarator));
19579
19580           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19581             {
19582               /* This is where the prologue starts...  */
19583               DECL_STRUCT_FUNCTION (decl)->function_start_locus
19584                 = func_brace_location;
19585             }
19586
19587           return decl;
19588         }
19589     }
19590   else if (parser->fully_implicit_function_template_p)
19591     {
19592       /* A non-template declaration involving a function parameter list
19593          containing an implicit template parameter will be made into a
19594          template.  If the resulting declaration is not going to be an
19595          actual function then finish the template scope here to prevent it.
19596          An error message will be issued once we have a decl to talk about.
19597
19598          FIXME probably we should do type deduction rather than create an
19599          implicit template, but the standard currently doesn't allow it. */
19600       bogus_implicit_tmpl = true;
19601       finish_fully_implicit_template (parser, NULL_TREE);
19602     }
19603
19604   /* [dcl.dcl]
19605
19606      Only in function declarations for constructors, destructors, type
19607      conversions, and deduction guides can the decl-specifier-seq be omitted.
19608
19609      We explicitly postpone this check past the point where we handle
19610      function-definitions because we tolerate function-definitions
19611      that are missing their return types in some modes.  */
19612   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19613     {
19614       cp_parser_error (parser,
19615                        "expected constructor, destructor, or type conversion");
19616       return error_mark_node;
19617     }
19618
19619   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
19620   if (token->type == CPP_EQ
19621       || token->type == CPP_OPEN_PAREN
19622       || token->type == CPP_OPEN_BRACE)
19623     {
19624       is_initialized = SD_INITIALIZED;
19625       initialization_kind = token->type;
19626       if (maybe_range_for_decl)
19627         *maybe_range_for_decl = error_mark_node;
19628       tmp_init_loc = token->location;
19629       if (init_loc && *init_loc == UNKNOWN_LOCATION)
19630         *init_loc = tmp_init_loc;
19631
19632       if (token->type == CPP_EQ
19633           && function_declarator_p (declarator))
19634         {
19635           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19636           if (t2->keyword == RID_DEFAULT)
19637             is_initialized = SD_DEFAULTED;
19638           else if (t2->keyword == RID_DELETE)
19639             is_initialized = SD_DELETED;
19640         }
19641     }
19642   else
19643     {
19644       /* If the init-declarator isn't initialized and isn't followed by a
19645          `,' or `;', it's not a valid init-declarator.  */
19646       if (token->type != CPP_COMMA
19647           && token->type != CPP_SEMICOLON)
19648         {
19649           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19650             range_for_decl_p = true;
19651           else
19652             {
19653               if (!maybe_range_for_decl)
19654                 cp_parser_error (parser, "expected initializer");
19655               return error_mark_node;
19656             }
19657         }
19658       is_initialized = SD_UNINITIALIZED;
19659       initialization_kind = CPP_EOF;
19660     }
19661
19662   /* Because start_decl has side-effects, we should only call it if we
19663      know we're going ahead.  By this point, we know that we cannot
19664      possibly be looking at any other construct.  */
19665   cp_parser_commit_to_tentative_parse (parser);
19666
19667   /* Enter the newly declared entry in the symbol table.  If we're
19668      processing a declaration in a class-specifier, we wait until
19669      after processing the initializer.  */
19670   if (!member_p)
19671     {
19672       if (parser->in_unbraced_linkage_specification_p)
19673         decl_specifiers->storage_class = sc_extern;
19674       decl = start_decl (declarator, decl_specifiers,
19675                          range_for_decl_p? SD_INITIALIZED : is_initialized,
19676                          attributes, prefix_attributes, &pushed_scope);
19677       cp_finalize_omp_declare_simd (parser, decl);
19678       cp_finalize_oacc_routine (parser, decl, false);
19679       /* Adjust location of decl if declarator->id_loc is more appropriate:
19680          set, and decl wasn't merged with another decl, in which case its
19681          location would be different from input_location, and more accurate.  */
19682       if (DECL_P (decl)
19683           && declarator->id_loc != UNKNOWN_LOCATION
19684           && DECL_SOURCE_LOCATION (decl) == input_location)
19685         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19686     }
19687   else if (scope)
19688     /* Enter the SCOPE.  That way unqualified names appearing in the
19689        initializer will be looked up in SCOPE.  */
19690     pushed_scope = push_scope (scope);
19691
19692   /* Perform deferred access control checks, now that we know in which
19693      SCOPE the declared entity resides.  */
19694   if (!member_p && decl)
19695     {
19696       tree saved_current_function_decl = NULL_TREE;
19697
19698       /* If the entity being declared is a function, pretend that we
19699          are in its scope.  If it is a `friend', it may have access to
19700          things that would not otherwise be accessible.  */
19701       if (TREE_CODE (decl) == FUNCTION_DECL)
19702         {
19703           saved_current_function_decl = current_function_decl;
19704           current_function_decl = decl;
19705         }
19706
19707       /* Perform access checks for template parameters.  */
19708       cp_parser_perform_template_parameter_access_checks (checks);
19709
19710       /* Perform the access control checks for the declarator and the
19711          decl-specifiers.  */
19712       perform_deferred_access_checks (tf_warning_or_error);
19713
19714       /* Restore the saved value.  */
19715       if (TREE_CODE (decl) == FUNCTION_DECL)
19716         current_function_decl = saved_current_function_decl;
19717     }
19718
19719   /* Parse the initializer.  */
19720   initializer = NULL_TREE;
19721   is_direct_init = false;
19722   is_non_constant_init = true;
19723   if (is_initialized)
19724     {
19725       if (function_declarator_p (declarator))
19726         {
19727            if (initialization_kind == CPP_EQ)
19728              initializer = cp_parser_pure_specifier (parser);
19729            else
19730              {
19731                /* If the declaration was erroneous, we don't really
19732                   know what the user intended, so just silently
19733                   consume the initializer.  */
19734                if (decl != error_mark_node)
19735                  error_at (tmp_init_loc, "initializer provided for function");
19736                cp_parser_skip_to_closing_parenthesis (parser,
19737                                                       /*recovering=*/true,
19738                                                       /*or_comma=*/false,
19739                                                       /*consume_paren=*/true);
19740              }
19741         }
19742       else
19743         {
19744           /* We want to record the extra mangling scope for in-class
19745              initializers of class members and initializers of static data
19746              member templates.  The former involves deferring
19747              parsing of the initializer until end of class as with default
19748              arguments.  So right here we only handle the latter.  */
19749           if (!member_p && processing_template_decl && decl != error_mark_node)
19750             start_lambda_scope (decl);
19751           initializer = cp_parser_initializer (parser,
19752                                                &is_direct_init,
19753                                                &is_non_constant_init);
19754           if (!member_p && processing_template_decl && decl != error_mark_node)
19755             finish_lambda_scope ();
19756           if (initializer == error_mark_node)
19757             cp_parser_skip_to_end_of_statement (parser);
19758         }
19759     }
19760
19761   /* The old parser allows attributes to appear after a parenthesized
19762      initializer.  Mark Mitchell proposed removing this functionality
19763      on the GCC mailing lists on 2002-08-13.  This parser accepts the
19764      attributes -- but ignores them.  Made a permerror in GCC 8.  */
19765   if (cp_parser_allow_gnu_extensions_p (parser)
19766       && initialization_kind == CPP_OPEN_PAREN
19767       && cp_parser_attributes_opt (parser)
19768       && permerror (input_location,
19769                     "attributes after parenthesized initializer ignored"))
19770     {
19771       static bool hint;
19772       if (flag_permissive && !hint)
19773         {
19774           hint = true;
19775           inform (input_location,
19776                   "this flexibility is deprecated and will be removed");
19777         }
19778     }
19779
19780   /* And now complain about a non-function implicit template.  */
19781   if (bogus_implicit_tmpl && decl != error_mark_node)
19782     error_at (DECL_SOURCE_LOCATION (decl),
19783               "non-function %qD declared as implicit template", decl);
19784
19785   /* For an in-class declaration, use `grokfield' to create the
19786      declaration.  */
19787   if (member_p)
19788     {
19789       if (pushed_scope)
19790         {
19791           pop_scope (pushed_scope);
19792           pushed_scope = NULL_TREE;
19793         }
19794       decl = grokfield (declarator, decl_specifiers,
19795                         initializer, !is_non_constant_init,
19796                         /*asmspec=*/NULL_TREE,
19797                         attr_chainon (attributes, prefix_attributes));
19798       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19799         cp_parser_save_default_args (parser, decl);
19800       cp_finalize_omp_declare_simd (parser, decl);
19801       cp_finalize_oacc_routine (parser, decl, false);
19802     }
19803
19804   /* Finish processing the declaration.  But, skip member
19805      declarations.  */
19806   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19807     {
19808       cp_finish_decl (decl,
19809                       initializer, !is_non_constant_init,
19810                       asm_specification,
19811                       /* If the initializer is in parentheses, then this is
19812                          a direct-initialization, which means that an
19813                          `explicit' constructor is OK.  Otherwise, an
19814                          `explicit' constructor cannot be used.  */
19815                       ((is_direct_init || !is_initialized)
19816                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19817     }
19818   else if ((cxx_dialect != cxx98) && friend_p
19819            && decl && TREE_CODE (decl) == FUNCTION_DECL)
19820     /* Core issue #226 (C++0x only): A default template-argument
19821        shall not be specified in a friend class template
19822        declaration. */
19823     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
19824                              /*is_partial=*/false, /*is_friend_decl=*/1);
19825
19826   if (!friend_p && pushed_scope)
19827     pop_scope (pushed_scope);
19828
19829   if (function_declarator_p (declarator)
19830       && parser->fully_implicit_function_template_p)
19831     {
19832       if (member_p)
19833         decl = finish_fully_implicit_template (parser, decl);
19834       else
19835         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19836     }
19837
19838   if (auto_result && is_initialized && decl_specifiers->type
19839       && type_uses_auto (decl_specifiers->type))
19840     *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19841
19842   return decl;
19843 }
19844
19845 /* Parse a declarator.
19846
19847    declarator:
19848      direct-declarator
19849      ptr-operator declarator
19850
19851    abstract-declarator:
19852      ptr-operator abstract-declarator [opt]
19853      direct-abstract-declarator
19854
19855    GNU Extensions:
19856
19857    declarator:
19858      attributes [opt] direct-declarator
19859      attributes [opt] ptr-operator declarator
19860
19861    abstract-declarator:
19862      attributes [opt] ptr-operator abstract-declarator [opt]
19863      attributes [opt] direct-abstract-declarator
19864
19865    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19866    detect constructors, destructors, deduction guides, or conversion operators.
19867    It is set to -1 if the declarator is a name, and +1 if it is a
19868    function. Otherwise it is set to zero. Usually you just want to
19869    test for >0, but internally the negative value is used.
19870
19871    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19872    a decl-specifier-seq unless it declares a constructor, destructor,
19873    or conversion.  It might seem that we could check this condition in
19874    semantic analysis, rather than parsing, but that makes it difficult
19875    to handle something like `f()'.  We want to notice that there are
19876    no decl-specifiers, and therefore realize that this is an
19877    expression, not a declaration.)
19878
19879    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19880    the declarator is a direct-declarator of the form "(...)".
19881
19882    MEMBER_P is true iff this declarator is a member-declarator.
19883
19884    FRIEND_P is true iff this declarator is a friend.  */
19885
19886 static cp_declarator *
19887 cp_parser_declarator (cp_parser* parser,
19888                       cp_parser_declarator_kind dcl_kind,
19889                       int* ctor_dtor_or_conv_p,
19890                       bool* parenthesized_p,
19891                       bool member_p, bool friend_p)
19892 {
19893   cp_declarator *declarator;
19894   enum tree_code code;
19895   cp_cv_quals cv_quals;
19896   tree class_type;
19897   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19898
19899   /* Assume this is not a constructor, destructor, or type-conversion
19900      operator.  */
19901   if (ctor_dtor_or_conv_p)
19902     *ctor_dtor_or_conv_p = 0;
19903
19904   if (cp_parser_allow_gnu_extensions_p (parser))
19905     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19906
19907   /* Check for the ptr-operator production.  */
19908   cp_parser_parse_tentatively (parser);
19909   /* Parse the ptr-operator.  */
19910   code = cp_parser_ptr_operator (parser,
19911                                  &class_type,
19912                                  &cv_quals,
19913                                  &std_attributes);
19914
19915   /* If that worked, then we have a ptr-operator.  */
19916   if (cp_parser_parse_definitely (parser))
19917     {
19918       /* If a ptr-operator was found, then this declarator was not
19919          parenthesized.  */
19920       if (parenthesized_p)
19921         *parenthesized_p = true;
19922       /* The dependent declarator is optional if we are parsing an
19923          abstract-declarator.  */
19924       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19925         cp_parser_parse_tentatively (parser);
19926
19927       /* Parse the dependent declarator.  */
19928       declarator = cp_parser_declarator (parser, dcl_kind,
19929                                          /*ctor_dtor_or_conv_p=*/NULL,
19930                                          /*parenthesized_p=*/NULL,
19931                                          /*member_p=*/false,
19932                                          friend_p);
19933
19934       /* If we are parsing an abstract-declarator, we must handle the
19935          case where the dependent declarator is absent.  */
19936       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19937           && !cp_parser_parse_definitely (parser))
19938         declarator = NULL;
19939
19940       declarator = cp_parser_make_indirect_declarator
19941         (code, class_type, cv_quals, declarator, std_attributes);
19942     }
19943   /* Everything else is a direct-declarator.  */
19944   else
19945     {
19946       if (parenthesized_p)
19947         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19948                                                    CPP_OPEN_PAREN);
19949       declarator = cp_parser_direct_declarator (parser, dcl_kind,
19950                                                 ctor_dtor_or_conv_p,
19951                                                 member_p, friend_p);
19952     }
19953
19954   if (gnu_attributes && declarator && declarator != cp_error_declarator)
19955     declarator->attributes = gnu_attributes;
19956   return declarator;
19957 }
19958
19959 /* Parse a direct-declarator or direct-abstract-declarator.
19960
19961    direct-declarator:
19962      declarator-id
19963      direct-declarator ( parameter-declaration-clause )
19964        cv-qualifier-seq [opt]
19965        ref-qualifier [opt]
19966        exception-specification [opt]
19967      direct-declarator [ constant-expression [opt] ]
19968      ( declarator )
19969
19970    direct-abstract-declarator:
19971      direct-abstract-declarator [opt]
19972        ( parameter-declaration-clause )
19973        cv-qualifier-seq [opt]
19974        ref-qualifier [opt]
19975        exception-specification [opt]
19976      direct-abstract-declarator [opt] [ constant-expression [opt] ]
19977      ( abstract-declarator )
19978
19979    Returns a representation of the declarator.  DCL_KIND is
19980    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19981    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
19982    we are parsing a direct-declarator.  It is
19983    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19984    of ambiguity we prefer an abstract declarator, as per
19985    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19986    as for cp_parser_declarator.  */
19987
19988 static cp_declarator *
19989 cp_parser_direct_declarator (cp_parser* parser,
19990                              cp_parser_declarator_kind dcl_kind,
19991                              int* ctor_dtor_or_conv_p,
19992                              bool member_p, bool friend_p)
19993 {
19994   cp_token *token;
19995   cp_declarator *declarator = NULL;
19996   tree scope = NULL_TREE;
19997   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19998   bool saved_in_declarator_p = parser->in_declarator_p;
19999   bool first = true;
20000   tree pushed_scope = NULL_TREE;
20001   cp_token *open_paren = NULL, *close_paren = NULL;
20002
20003   while (true)
20004     {
20005       /* Peek at the next token.  */
20006       token = cp_lexer_peek_token (parser->lexer);
20007       if (token->type == CPP_OPEN_PAREN)
20008         {
20009           /* This is either a parameter-declaration-clause, or a
20010              parenthesized declarator. When we know we are parsing a
20011              named declarator, it must be a parenthesized declarator
20012              if FIRST is true. For instance, `(int)' is a
20013              parameter-declaration-clause, with an omitted
20014              direct-abstract-declarator. But `((*))', is a
20015              parenthesized abstract declarator. Finally, when T is a
20016              template parameter `(T)' is a
20017              parameter-declaration-clause, and not a parenthesized
20018              named declarator.
20019
20020              We first try and parse a parameter-declaration-clause,
20021              and then try a nested declarator (if FIRST is true).
20022
20023              It is not an error for it not to be a
20024              parameter-declaration-clause, even when FIRST is
20025              false. Consider,
20026
20027                int i (int);
20028                int i (3);
20029
20030              The first is the declaration of a function while the
20031              second is the definition of a variable, including its
20032              initializer.
20033
20034              Having seen only the parenthesis, we cannot know which of
20035              these two alternatives should be selected.  Even more
20036              complex are examples like:
20037
20038                int i (int (a));
20039                int i (int (3));
20040
20041              The former is a function-declaration; the latter is a
20042              variable initialization.
20043
20044              Thus again, we try a parameter-declaration-clause, and if
20045              that fails, we back out and return.  */
20046
20047           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20048             {
20049               tree params;
20050               bool is_declarator = false;
20051
20052               open_paren = NULL;
20053
20054               /* In a member-declarator, the only valid interpretation
20055                  of a parenthesis is the start of a
20056                  parameter-declaration-clause.  (It is invalid to
20057                  initialize a static data member with a parenthesized
20058                  initializer; only the "=" form of initialization is
20059                  permitted.)  */
20060               if (!member_p)
20061                 cp_parser_parse_tentatively (parser);
20062
20063               /* Consume the `('.  */
20064               matching_parens parens;
20065               parens.consume_open (parser);
20066               if (first)
20067                 {
20068                   /* If this is going to be an abstract declarator, we're
20069                      in a declarator and we can't have default args.  */
20070                   parser->default_arg_ok_p = false;
20071                   parser->in_declarator_p = true;
20072                 }
20073
20074               begin_scope (sk_function_parms, NULL_TREE);
20075
20076               /* Parse the parameter-declaration-clause.  */
20077               params = cp_parser_parameter_declaration_clause (parser);
20078
20079               /* Consume the `)'.  */
20080               parens.require_close (parser);
20081
20082               /* If all went well, parse the cv-qualifier-seq,
20083                  ref-qualifier and the exception-specification.  */
20084               if (member_p || cp_parser_parse_definitely (parser))
20085                 {
20086                   cp_cv_quals cv_quals;
20087                   cp_virt_specifiers virt_specifiers;
20088                   cp_ref_qualifier ref_qual;
20089                   tree exception_specification;
20090                   tree late_return;
20091                   tree attrs;
20092                   bool memfn = (member_p || (pushed_scope
20093                                              && CLASS_TYPE_P (pushed_scope)));
20094
20095                   is_declarator = true;
20096
20097                   if (ctor_dtor_or_conv_p)
20098                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20099                   first = false;
20100
20101                   /* Parse the cv-qualifier-seq.  */
20102                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20103                   /* Parse the ref-qualifier. */
20104                   ref_qual = cp_parser_ref_qualifier_opt (parser);
20105                   /* Parse the tx-qualifier.  */
20106                   tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20107                   /* And the exception-specification.  */
20108                   exception_specification
20109                     = cp_parser_exception_specification_opt (parser);
20110
20111                   attrs = cp_parser_std_attribute_spec_seq (parser);
20112
20113                   /* In here, we handle cases where attribute is used after
20114                      the function declaration.  For example:
20115                      void func (int x) __attribute__((vector(..)));  */
20116                   tree gnu_attrs = NULL_TREE;
20117                   tree requires_clause = NULL_TREE;
20118                   late_return = (cp_parser_late_return_type_opt
20119                                  (parser, declarator, requires_clause,
20120                                   memfn ? cv_quals : -1));
20121
20122                   /* Parse the virt-specifier-seq.  */
20123                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20124
20125                   /* Create the function-declarator.  */
20126                   declarator = make_call_declarator (declarator,
20127                                                      params,
20128                                                      cv_quals,
20129                                                      virt_specifiers,
20130                                                      ref_qual,
20131                                                      tx_qual,
20132                                                      exception_specification,
20133                                                      late_return,
20134                                                      requires_clause);
20135                   declarator->std_attributes = attrs;
20136                   declarator->attributes = gnu_attrs;
20137                   /* Any subsequent parameter lists are to do with
20138                      return type, so are not those of the declared
20139                      function.  */
20140                   parser->default_arg_ok_p = false;
20141                 }
20142
20143               /* Remove the function parms from scope.  */
20144               pop_bindings_and_leave_scope ();
20145
20146               if (is_declarator)
20147                 /* Repeat the main loop.  */
20148                 continue;
20149             }
20150
20151           /* If this is the first, we can try a parenthesized
20152              declarator.  */
20153           if (first)
20154             {
20155               bool saved_in_type_id_in_expr_p;
20156
20157               parser->default_arg_ok_p = saved_default_arg_ok_p;
20158               parser->in_declarator_p = saved_in_declarator_p;
20159
20160               open_paren = token;
20161               /* Consume the `('.  */
20162               matching_parens parens;
20163               parens.consume_open (parser);
20164               /* Parse the nested declarator.  */
20165               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20166               parser->in_type_id_in_expr_p = true;
20167               declarator
20168                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20169                                         /*parenthesized_p=*/NULL,
20170                                         member_p, friend_p);
20171               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20172               first = false;
20173               /* Expect a `)'.  */
20174               close_paren = cp_lexer_peek_token (parser->lexer);
20175               if (!parens.require_close (parser))
20176                 declarator = cp_error_declarator;
20177               if (declarator == cp_error_declarator)
20178                 break;
20179
20180               goto handle_declarator;
20181             }
20182           /* Otherwise, we must be done.  */
20183           else
20184             break;
20185         }
20186       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20187                && token->type == CPP_OPEN_SQUARE
20188                && !cp_next_tokens_can_be_attribute_p (parser))
20189         {
20190           /* Parse an array-declarator.  */
20191           tree bounds, attrs;
20192
20193           if (ctor_dtor_or_conv_p)
20194             *ctor_dtor_or_conv_p = 0;
20195
20196           open_paren = NULL;
20197           first = false;
20198           parser->default_arg_ok_p = false;
20199           parser->in_declarator_p = true;
20200           /* Consume the `['.  */
20201           cp_lexer_consume_token (parser->lexer);
20202           /* Peek at the next token.  */
20203           token = cp_lexer_peek_token (parser->lexer);
20204           /* If the next token is `]', then there is no
20205              constant-expression.  */
20206           if (token->type != CPP_CLOSE_SQUARE)
20207             {
20208               bool non_constant_p;
20209               bounds
20210                 = cp_parser_constant_expression (parser,
20211                                                  /*allow_non_constant=*/true,
20212                                                  &non_constant_p);
20213               if (!non_constant_p)
20214                 /* OK */;
20215               else if (error_operand_p (bounds))
20216                 /* Already gave an error.  */;
20217               else if (!parser->in_function_body
20218                        || current_binding_level->kind == sk_function_parms)
20219                 {
20220                   /* Normally, the array bound must be an integral constant
20221                      expression.  However, as an extension, we allow VLAs
20222                      in function scopes as long as they aren't part of a
20223                      parameter declaration.  */
20224                   cp_parser_error (parser,
20225                                    "array bound is not an integer constant");
20226                   bounds = error_mark_node;
20227                 }
20228               else if (processing_template_decl
20229                        && !type_dependent_expression_p (bounds))
20230                 {
20231                   /* Remember this wasn't a constant-expression.  */
20232                   bounds = build_nop (TREE_TYPE (bounds), bounds);
20233                   TREE_SIDE_EFFECTS (bounds) = 1;
20234                 }
20235             }
20236           else
20237             bounds = NULL_TREE;
20238           /* Look for the closing `]'.  */
20239           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20240             {
20241               declarator = cp_error_declarator;
20242               break;
20243             }
20244
20245           attrs = cp_parser_std_attribute_spec_seq (parser);
20246           declarator = make_array_declarator (declarator, bounds);
20247           declarator->std_attributes = attrs;
20248         }
20249       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20250         {
20251           {
20252             tree qualifying_scope;
20253             tree unqualified_name;
20254             tree attrs;
20255             special_function_kind sfk;
20256             bool abstract_ok;
20257             bool pack_expansion_p = false;
20258             cp_token *declarator_id_start_token;
20259
20260             /* Parse a declarator-id */
20261             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20262             if (abstract_ok)
20263               {
20264                 cp_parser_parse_tentatively (parser);
20265
20266                 /* If we see an ellipsis, we should be looking at a
20267                    parameter pack. */
20268                 if (token->type == CPP_ELLIPSIS)
20269                   {
20270                     /* Consume the `...' */
20271                     cp_lexer_consume_token (parser->lexer);
20272
20273                     pack_expansion_p = true;
20274                   }
20275               }
20276
20277             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20278             unqualified_name
20279               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20280             qualifying_scope = parser->scope;
20281             if (abstract_ok)
20282               {
20283                 bool okay = false;
20284
20285                 if (!unqualified_name && pack_expansion_p)
20286                   {
20287                     /* Check whether an error occurred. */
20288                     okay = !cp_parser_error_occurred (parser);
20289
20290                     /* We already consumed the ellipsis to mark a
20291                        parameter pack, but we have no way to report it,
20292                        so abort the tentative parse. We will be exiting
20293                        immediately anyway. */
20294                     cp_parser_abort_tentative_parse (parser);
20295                   }
20296                 else
20297                   okay = cp_parser_parse_definitely (parser);
20298
20299                 if (!okay)
20300                   unqualified_name = error_mark_node;
20301                 else if (unqualified_name
20302                          && (qualifying_scope
20303                              || (!identifier_p (unqualified_name))))
20304                   {
20305                     cp_parser_error (parser, "expected unqualified-id");
20306                     unqualified_name = error_mark_node;
20307                   }
20308               }
20309
20310             if (!unqualified_name)
20311               return NULL;
20312             if (unqualified_name == error_mark_node)
20313               {
20314                 declarator = cp_error_declarator;
20315                 pack_expansion_p = false;
20316                 declarator->parameter_pack_p = false;
20317                 break;
20318               }
20319
20320             attrs = cp_parser_std_attribute_spec_seq (parser);
20321
20322             if (qualifying_scope && at_namespace_scope_p ()
20323                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20324               {
20325                 /* In the declaration of a member of a template class
20326                    outside of the class itself, the SCOPE will sometimes
20327                    be a TYPENAME_TYPE.  For example, given:
20328
20329                    template <typename T>
20330                    int S<T>::R::i = 3;
20331
20332                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
20333                    this context, we must resolve S<T>::R to an ordinary
20334                    type, rather than a typename type.
20335
20336                    The reason we normally avoid resolving TYPENAME_TYPEs
20337                    is that a specialization of `S' might render
20338                    `S<T>::R' not a type.  However, if `S' is
20339                    specialized, then this `i' will not be used, so there
20340                    is no harm in resolving the types here.  */
20341                 tree type;
20342
20343                 /* Resolve the TYPENAME_TYPE.  */
20344                 type = resolve_typename_type (qualifying_scope,
20345                                               /*only_current_p=*/false);
20346                 /* If that failed, the declarator is invalid.  */
20347                 if (TREE_CODE (type) == TYPENAME_TYPE)
20348                   {
20349                     if (typedef_variant_p (type))
20350                       error_at (declarator_id_start_token->location,
20351                                 "cannot define member of dependent typedef "
20352                                 "%qT", type);
20353                     else
20354                       error_at (declarator_id_start_token->location,
20355                                 "%<%T::%E%> is not a type",
20356                                 TYPE_CONTEXT (qualifying_scope),
20357                                 TYPE_IDENTIFIER (qualifying_scope));
20358                   }
20359                 qualifying_scope = type;
20360               }
20361
20362             sfk = sfk_none;
20363
20364             if (unqualified_name)
20365               {
20366                 tree class_type;
20367
20368                 if (qualifying_scope
20369                     && CLASS_TYPE_P (qualifying_scope))
20370                   class_type = qualifying_scope;
20371                 else
20372                   class_type = current_class_type;
20373
20374                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20375                   {
20376                     tree name_type = TREE_TYPE (unqualified_name);
20377
20378                     if (!class_type || !same_type_p (name_type, class_type))
20379                       {
20380                         /* We do not attempt to print the declarator
20381                            here because we do not have enough
20382                            information about its original syntactic
20383                            form.  */
20384                         cp_parser_error (parser, "invalid declarator");
20385                         declarator = cp_error_declarator;
20386                         break;
20387                       }
20388                     else if (qualifying_scope
20389                              && CLASSTYPE_USE_TEMPLATE (name_type))
20390                       {
20391                         error_at (declarator_id_start_token->location,
20392                                   "invalid use of constructor as a template");
20393                         inform (declarator_id_start_token->location,
20394                                 "use %<%T::%D%> instead of %<%T::%D%> to "
20395                                 "name the constructor in a qualified name",
20396                                 class_type,
20397                                 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20398                                 class_type, name_type);
20399                         declarator = cp_error_declarator;
20400                         break;
20401                       }
20402                     unqualified_name = constructor_name (class_type);
20403                   }
20404
20405                 if (class_type)
20406                   {
20407                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20408                       sfk = sfk_destructor;
20409                     else if (identifier_p (unqualified_name)
20410                              && IDENTIFIER_CONV_OP_P (unqualified_name))
20411                       sfk = sfk_conversion;
20412                     else if (/* There's no way to declare a constructor
20413                                 for an unnamed type, even if the type
20414                                 got a name for linkage purposes.  */
20415                              !TYPE_WAS_UNNAMED (class_type)
20416                              /* Handle correctly (c++/19200):
20417
20418                                 struct S {
20419                                   struct T{};
20420                                   friend void S(T);
20421                                 };
20422
20423                                 and also:
20424
20425                                 namespace N {
20426                                   void S();
20427                                 }
20428
20429                                 struct S {
20430                                   friend void N::S();
20431                                 };  */
20432                              && (!friend_p || class_type == qualifying_scope)
20433                              && constructor_name_p (unqualified_name,
20434                                                     class_type))
20435                       sfk = sfk_constructor;
20436                     else if (is_overloaded_fn (unqualified_name)
20437                              && DECL_CONSTRUCTOR_P (get_first_fn
20438                                                     (unqualified_name)))
20439                       sfk = sfk_constructor;
20440
20441                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
20442                       *ctor_dtor_or_conv_p = -1;
20443                   }
20444               }
20445             declarator = make_id_declarator (qualifying_scope,
20446                                              unqualified_name,
20447                                              sfk);
20448             declarator->std_attributes = attrs;
20449             declarator->id_loc = token->location;
20450             declarator->parameter_pack_p = pack_expansion_p;
20451
20452             if (pack_expansion_p)
20453               maybe_warn_variadic_templates ();
20454           }
20455
20456         handle_declarator:;
20457           scope = get_scope_of_declarator (declarator);
20458           if (scope)
20459             {
20460               /* Any names that appear after the declarator-id for a
20461                  member are looked up in the containing scope.  */
20462               if (at_function_scope_p ())
20463                 {
20464                   /* But declarations with qualified-ids can't appear in a
20465                      function.  */
20466                   cp_parser_error (parser, "qualified-id in declaration");
20467                   declarator = cp_error_declarator;
20468                   break;
20469                 }
20470               pushed_scope = push_scope (scope);
20471             }
20472           parser->in_declarator_p = true;
20473           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20474               || (declarator && declarator->kind == cdk_id))
20475             /* Default args are only allowed on function
20476                declarations.  */
20477             parser->default_arg_ok_p = saved_default_arg_ok_p;
20478           else
20479             parser->default_arg_ok_p = false;
20480
20481           first = false;
20482         }
20483       /* We're done.  */
20484       else
20485         break;
20486     }
20487
20488   /* For an abstract declarator, we might wind up with nothing at this
20489      point.  That's an error; the declarator is not optional.  */
20490   if (!declarator)
20491     cp_parser_error (parser, "expected declarator");
20492   else if (open_paren)
20493     {
20494       /* Record overly parenthesized declarator so we can give a
20495          diagnostic about confusing decl/expr disambiguation.  */
20496       if (declarator->kind == cdk_array)
20497         {
20498           /* If the open and close parens are on different lines, this
20499              is probably a formatting thing, so ignore.  */
20500           expanded_location open = expand_location (open_paren->location);
20501           expanded_location close = expand_location (close_paren->location);
20502           if (open.line != close.line || open.file != close.file)
20503             open_paren = NULL;
20504         }
20505       if (open_paren)
20506         declarator->parenthesized = open_paren->location;
20507     }
20508
20509   /* If we entered a scope, we must exit it now.  */
20510   if (pushed_scope)
20511     pop_scope (pushed_scope);
20512
20513   parser->default_arg_ok_p = saved_default_arg_ok_p;
20514   parser->in_declarator_p = saved_in_declarator_p;
20515
20516   return declarator;
20517 }
20518
20519 /* Parse a ptr-operator.
20520
20521    ptr-operator:
20522      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20523      * cv-qualifier-seq [opt]
20524      &
20525      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20526      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20527
20528    GNU Extension:
20529
20530    ptr-operator:
20531      & cv-qualifier-seq [opt]
20532
20533    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20534    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20535    an rvalue reference. In the case of a pointer-to-member, *TYPE is
20536    filled in with the TYPE containing the member.  *CV_QUALS is
20537    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20538    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
20539    Note that the tree codes returned by this function have nothing
20540    to do with the types of trees that will be eventually be created
20541    to represent the pointer or reference type being parsed. They are
20542    just constants with suggestive names. */
20543 static enum tree_code
20544 cp_parser_ptr_operator (cp_parser* parser,
20545                         tree* type,
20546                         cp_cv_quals *cv_quals,
20547                         tree *attributes)
20548 {
20549   enum tree_code code = ERROR_MARK;
20550   cp_token *token;
20551   tree attrs = NULL_TREE;
20552
20553   /* Assume that it's not a pointer-to-member.  */
20554   *type = NULL_TREE;
20555   /* And that there are no cv-qualifiers.  */
20556   *cv_quals = TYPE_UNQUALIFIED;
20557
20558   /* Peek at the next token.  */
20559   token = cp_lexer_peek_token (parser->lexer);
20560
20561   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
20562   if (token->type == CPP_MULT)
20563     code = INDIRECT_REF;
20564   else if (token->type == CPP_AND)
20565     code = ADDR_EXPR;
20566   else if ((cxx_dialect != cxx98) &&
20567            token->type == CPP_AND_AND) /* C++0x only */
20568     code = NON_LVALUE_EXPR;
20569
20570   if (code != ERROR_MARK)
20571     {
20572       /* Consume the `*', `&' or `&&'.  */
20573       cp_lexer_consume_token (parser->lexer);
20574
20575       /* A `*' can be followed by a cv-qualifier-seq, and so can a
20576          `&', if we are allowing GNU extensions.  (The only qualifier
20577          that can legally appear after `&' is `restrict', but that is
20578          enforced during semantic analysis.  */
20579       if (code == INDIRECT_REF
20580           || cp_parser_allow_gnu_extensions_p (parser))
20581         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20582
20583       attrs = cp_parser_std_attribute_spec_seq (parser);
20584       if (attributes != NULL)
20585         *attributes = attrs;
20586     }
20587   else
20588     {
20589       /* Try the pointer-to-member case.  */
20590       cp_parser_parse_tentatively (parser);
20591       /* Look for the optional `::' operator.  */
20592       cp_parser_global_scope_opt (parser,
20593                                   /*current_scope_valid_p=*/false);
20594       /* Look for the nested-name specifier.  */
20595       token = cp_lexer_peek_token (parser->lexer);
20596       cp_parser_nested_name_specifier (parser,
20597                                        /*typename_keyword_p=*/false,
20598                                        /*check_dependency_p=*/true,
20599                                        /*type_p=*/false,
20600                                        /*is_declaration=*/false);
20601       /* If we found it, and the next token is a `*', then we are
20602          indeed looking at a pointer-to-member operator.  */
20603       if (!cp_parser_error_occurred (parser)
20604           && cp_parser_require (parser, CPP_MULT, RT_MULT))
20605         {
20606           /* Indicate that the `*' operator was used.  */
20607           code = INDIRECT_REF;
20608
20609           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20610             error_at (token->location, "%qD is a namespace", parser->scope);
20611           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20612             error_at (token->location, "cannot form pointer to member of "
20613                       "non-class %q#T", parser->scope);
20614           else
20615             {
20616               /* The type of which the member is a member is given by the
20617                  current SCOPE.  */
20618               *type = parser->scope;
20619               /* The next name will not be qualified.  */
20620               parser->scope = NULL_TREE;
20621               parser->qualifying_scope = NULL_TREE;
20622               parser->object_scope = NULL_TREE;
20623               /* Look for optional c++11 attributes.  */
20624               attrs = cp_parser_std_attribute_spec_seq (parser);
20625               if (attributes != NULL)
20626                 *attributes = attrs;
20627               /* Look for the optional cv-qualifier-seq.  */
20628               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20629             }
20630         }
20631       /* If that didn't work we don't have a ptr-operator.  */
20632       if (!cp_parser_parse_definitely (parser))
20633         cp_parser_error (parser, "expected ptr-operator");
20634     }
20635
20636   return code;
20637 }
20638
20639 /* Parse an (optional) cv-qualifier-seq.
20640
20641    cv-qualifier-seq:
20642      cv-qualifier cv-qualifier-seq [opt]
20643
20644    cv-qualifier:
20645      const
20646      volatile
20647
20648    GNU Extension:
20649
20650    cv-qualifier:
20651      __restrict__
20652
20653    Returns a bitmask representing the cv-qualifiers.  */
20654
20655 static cp_cv_quals
20656 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20657 {
20658   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20659
20660   while (true)
20661     {
20662       cp_token *token;
20663       cp_cv_quals cv_qualifier;
20664
20665       /* Peek at the next token.  */
20666       token = cp_lexer_peek_token (parser->lexer);
20667       /* See if it's a cv-qualifier.  */
20668       switch (token->keyword)
20669         {
20670         case RID_CONST:
20671           cv_qualifier = TYPE_QUAL_CONST;
20672           break;
20673
20674         case RID_VOLATILE:
20675           cv_qualifier = TYPE_QUAL_VOLATILE;
20676           break;
20677
20678         case RID_RESTRICT:
20679           cv_qualifier = TYPE_QUAL_RESTRICT;
20680           break;
20681
20682         default:
20683           cv_qualifier = TYPE_UNQUALIFIED;
20684           break;
20685         }
20686
20687       if (!cv_qualifier)
20688         break;
20689
20690       if (cv_quals & cv_qualifier)
20691         {
20692           gcc_rich_location richloc (token->location);
20693           richloc.add_fixit_remove ();
20694           error_at (&richloc, "duplicate cv-qualifier");
20695           cp_lexer_purge_token (parser->lexer);
20696         }
20697       else
20698         {
20699           cp_lexer_consume_token (parser->lexer);
20700           cv_quals |= cv_qualifier;
20701         }
20702     }
20703
20704   return cv_quals;
20705 }
20706
20707 /* Parse an (optional) ref-qualifier
20708
20709    ref-qualifier:
20710      &
20711      &&
20712
20713    Returns cp_ref_qualifier representing ref-qualifier. */
20714
20715 static cp_ref_qualifier
20716 cp_parser_ref_qualifier_opt (cp_parser* parser)
20717 {
20718   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20719
20720   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
20721   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20722     return ref_qual;
20723
20724   while (true)
20725     {
20726       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20727       cp_token *token = cp_lexer_peek_token (parser->lexer);
20728
20729       switch (token->type)
20730         {
20731         case CPP_AND:
20732           curr_ref_qual = REF_QUAL_LVALUE;
20733           break;
20734
20735         case CPP_AND_AND:
20736           curr_ref_qual = REF_QUAL_RVALUE;
20737           break;
20738
20739         default:
20740           curr_ref_qual = REF_QUAL_NONE;
20741           break;
20742         }
20743
20744       if (!curr_ref_qual)
20745         break;
20746       else if (ref_qual)
20747         {
20748           error_at (token->location, "multiple ref-qualifiers");
20749           cp_lexer_purge_token (parser->lexer);
20750         }
20751       else
20752         {
20753           ref_qual = curr_ref_qual;
20754           cp_lexer_consume_token (parser->lexer);
20755         }
20756     }
20757
20758   return ref_qual;
20759 }
20760
20761 /* Parse an optional tx-qualifier.
20762
20763    tx-qualifier:
20764      transaction_safe
20765      transaction_safe_dynamic  */
20766
20767 static tree
20768 cp_parser_tx_qualifier_opt (cp_parser *parser)
20769 {
20770   cp_token *token = cp_lexer_peek_token (parser->lexer);
20771   if (token->type == CPP_NAME)
20772     {
20773       tree name = token->u.value;
20774       const char *p = IDENTIFIER_POINTER (name);
20775       const int len = strlen ("transaction_safe");
20776       if (!strncmp (p, "transaction_safe", len))
20777         {
20778           p += len;
20779           if (*p == '\0'
20780               || !strcmp (p, "_dynamic"))
20781             {
20782               cp_lexer_consume_token (parser->lexer);
20783               if (!flag_tm)
20784                 {
20785                   error ("%qE requires %<-fgnu-tm%>", name);
20786                   return NULL_TREE;
20787                 }
20788               else
20789                 return name;
20790             }
20791         }
20792     }
20793   return NULL_TREE;
20794 }
20795
20796 /* Parse an (optional) virt-specifier-seq.
20797
20798    virt-specifier-seq:
20799      virt-specifier virt-specifier-seq [opt]
20800
20801    virt-specifier:
20802      override
20803      final
20804
20805    Returns a bitmask representing the virt-specifiers.  */
20806
20807 static cp_virt_specifiers
20808 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20809 {
20810   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20811
20812   while (true)
20813     {
20814       cp_token *token;
20815       cp_virt_specifiers virt_specifier;
20816
20817       /* Peek at the next token.  */
20818       token = cp_lexer_peek_token (parser->lexer);
20819       /* See if it's a virt-specifier-qualifier.  */
20820       if (token->type != CPP_NAME)
20821         break;
20822       if (id_equal (token->u.value, "override"))
20823         {
20824           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20825           virt_specifier = VIRT_SPEC_OVERRIDE;
20826         }
20827       else if (id_equal (token->u.value, "final"))
20828         {
20829           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20830           virt_specifier = VIRT_SPEC_FINAL;
20831         }
20832       else if (id_equal (token->u.value, "__final"))
20833         {
20834           virt_specifier = VIRT_SPEC_FINAL;
20835         }
20836       else
20837         break;
20838
20839       if (virt_specifiers & virt_specifier)
20840         {
20841           gcc_rich_location richloc (token->location);
20842           richloc.add_fixit_remove ();
20843           error_at (&richloc, "duplicate virt-specifier");
20844           cp_lexer_purge_token (parser->lexer);
20845         }
20846       else
20847         {
20848           cp_lexer_consume_token (parser->lexer);
20849           virt_specifiers |= virt_specifier;
20850         }
20851     }
20852   return virt_specifiers;
20853 }
20854
20855 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20856    is in scope even though it isn't real.  */
20857
20858 void
20859 inject_this_parameter (tree ctype, cp_cv_quals quals)
20860 {
20861   tree this_parm;
20862
20863   if (current_class_ptr)
20864     {
20865       /* We don't clear this between NSDMIs.  Is it already what we want?  */
20866       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20867       if (DECL_P (current_class_ptr)
20868           && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20869           && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20870           && cp_type_quals (type) == quals)
20871         return;
20872     }
20873
20874   this_parm = build_this_parm (NULL_TREE, ctype, quals);
20875   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
20876   current_class_ptr = NULL_TREE;
20877   current_class_ref
20878     = cp_build_fold_indirect_ref (this_parm);
20879   current_class_ptr = this_parm;
20880 }
20881
20882 /* Return true iff our current scope is a non-static data member
20883    initializer.  */
20884
20885 bool
20886 parsing_nsdmi (void)
20887 {
20888   /* We recognize NSDMI context by the context-less 'this' pointer set up
20889      by the function above.  */
20890   if (current_class_ptr
20891       && TREE_CODE (current_class_ptr) == PARM_DECL
20892       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20893     return true;
20894   return false;
20895 }
20896
20897 /* Parse a late-specified return type, if any.  This is not a separate
20898    non-terminal, but part of a function declarator, which looks like
20899
20900    -> trailing-type-specifier-seq abstract-declarator(opt)
20901
20902    Returns the type indicated by the type-id.
20903
20904    In addition to this, parse any queued up #pragma omp declare simd
20905    clauses, and #pragma acc routine clauses.
20906
20907    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20908    function.  */
20909
20910 static tree
20911 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20912                                 tree& requires_clause, cp_cv_quals quals)
20913 {
20914   cp_token *token;
20915   tree type = NULL_TREE;
20916   bool declare_simd_p = (parser->omp_declare_simd
20917                          && declarator
20918                          && declarator->kind == cdk_id);
20919
20920   bool oacc_routine_p = (parser->oacc_routine
20921                          && declarator
20922                          && declarator->kind == cdk_id);
20923
20924   /* Peek at the next token.  */
20925   token = cp_lexer_peek_token (parser->lexer);
20926   /* A late-specified return type is indicated by an initial '->'. */
20927   if (token->type != CPP_DEREF
20928       && token->keyword != RID_REQUIRES
20929       && !(token->type == CPP_NAME
20930            && token->u.value == ridpointers[RID_REQUIRES])
20931       && !(declare_simd_p || oacc_routine_p))
20932     return NULL_TREE;
20933
20934   tree save_ccp = current_class_ptr;
20935   tree save_ccr = current_class_ref;
20936   if (quals >= 0)
20937     {
20938       /* DR 1207: 'this' is in scope in the trailing return type.  */
20939       inject_this_parameter (current_class_type, quals);
20940     }
20941
20942   if (token->type == CPP_DEREF)
20943     {
20944       /* Consume the ->.  */
20945       cp_lexer_consume_token (parser->lexer);
20946
20947       type = cp_parser_trailing_type_id (parser);
20948     }
20949
20950   /* Function declarations may be followed by a trailing
20951      requires-clause.  */
20952   requires_clause = cp_parser_requires_clause_opt (parser);
20953
20954   if (declare_simd_p)
20955     declarator->attributes
20956       = cp_parser_late_parsing_omp_declare_simd (parser,
20957                                                  declarator->attributes);
20958   if (oacc_routine_p)
20959     declarator->attributes
20960       = cp_parser_late_parsing_oacc_routine (parser,
20961                                              declarator->attributes);
20962
20963   if (quals >= 0)
20964     {
20965       current_class_ptr = save_ccp;
20966       current_class_ref = save_ccr;
20967     }
20968
20969   return type;
20970 }
20971
20972 /* Parse a declarator-id.
20973
20974    declarator-id:
20975      id-expression
20976      :: [opt] nested-name-specifier [opt] type-name
20977
20978    In the `id-expression' case, the value returned is as for
20979    cp_parser_id_expression if the id-expression was an unqualified-id.
20980    If the id-expression was a qualified-id, then a SCOPE_REF is
20981    returned.  The first operand is the scope (either a NAMESPACE_DECL
20982    or TREE_TYPE), but the second is still just a representation of an
20983    unqualified-id.  */
20984
20985 static tree
20986 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20987 {
20988   tree id;
20989   /* The expression must be an id-expression.  Assume that qualified
20990      names are the names of types so that:
20991
20992        template <class T>
20993        int S<T>::R::i = 3;
20994
20995      will work; we must treat `S<T>::R' as the name of a type.
20996      Similarly, assume that qualified names are templates, where
20997      required, so that:
20998
20999        template <class T>
21000        int S<T>::R<T>::i = 3;
21001
21002      will work, too.  */
21003   id = cp_parser_id_expression (parser,
21004                                 /*template_keyword_p=*/false,
21005                                 /*check_dependency_p=*/false,
21006                                 /*template_p=*/NULL,
21007                                 /*declarator_p=*/true,
21008                                 optional_p);
21009   if (id && BASELINK_P (id))
21010     id = BASELINK_FUNCTIONS (id);
21011   return id;
21012 }
21013
21014 /* Parse a type-id.
21015
21016    type-id:
21017      type-specifier-seq abstract-declarator [opt]
21018
21019    Returns the TYPE specified.  */
21020
21021 static tree
21022 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21023                      bool is_trailing_return)
21024 {
21025   cp_decl_specifier_seq type_specifier_seq;
21026   cp_declarator *abstract_declarator;
21027
21028   /* Parse the type-specifier-seq.  */
21029   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21030                                 is_trailing_return,
21031                                 &type_specifier_seq);
21032   if (is_template_arg && type_specifier_seq.type
21033       && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21034       && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21035     /* A bare template name as a template argument is a template template
21036        argument, not a placeholder, so fail parsing it as a type argument.  */
21037     {
21038       gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21039       cp_parser_simulate_error (parser);
21040       return error_mark_node;
21041     }
21042   if (type_specifier_seq.type == error_mark_node)
21043     return error_mark_node;
21044
21045   /* There might or might not be an abstract declarator.  */
21046   cp_parser_parse_tentatively (parser);
21047   /* Look for the declarator.  */
21048   abstract_declarator
21049     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21050                             /*parenthesized_p=*/NULL,
21051                             /*member_p=*/false,
21052                             /*friend_p=*/false);
21053   /* Check to see if there really was a declarator.  */
21054   if (!cp_parser_parse_definitely (parser))
21055     abstract_declarator = NULL;
21056
21057   if (type_specifier_seq.type
21058       /* The concepts TS allows 'auto' as a type-id.  */
21059       && (!flag_concepts || parser->in_type_id_in_expr_p)
21060       /* None of the valid uses of 'auto' in C++14 involve the type-id
21061          nonterminal, but it is valid in a trailing-return-type.  */
21062       && !(cxx_dialect >= cxx14 && is_trailing_return))
21063     if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21064       {
21065         /* A type-id with type 'auto' is only ok if the abstract declarator
21066            is a function declarator with a late-specified return type.
21067
21068            A type-id with 'auto' is also valid in a trailing-return-type
21069            in a compound-requirement. */
21070         if (abstract_declarator
21071             && abstract_declarator->kind == cdk_function
21072             && abstract_declarator->u.function.late_return_type)
21073           /* OK */;
21074         else if (parser->in_result_type_constraint_p)
21075           /* OK */;
21076         else
21077           {
21078             location_t loc = type_specifier_seq.locations[ds_type_spec];
21079             if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21080               {
21081                 error_at (loc, "missing template arguments after %qT",
21082                           auto_node);
21083                 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21084                         tmpl);
21085               }
21086             else
21087               error_at (loc, "invalid use of %qT", auto_node);
21088             return error_mark_node;
21089           }
21090       }
21091   
21092   return groktypename (&type_specifier_seq, abstract_declarator,
21093                        is_template_arg);
21094 }
21095
21096 static tree
21097 cp_parser_type_id (cp_parser *parser)
21098 {
21099   return cp_parser_type_id_1 (parser, false, false);
21100 }
21101
21102 static tree
21103 cp_parser_template_type_arg (cp_parser *parser)
21104 {
21105   tree r;
21106   const char *saved_message = parser->type_definition_forbidden_message;
21107   parser->type_definition_forbidden_message
21108     = G_("types may not be defined in template arguments");
21109   r = cp_parser_type_id_1 (parser, true, false);
21110   parser->type_definition_forbidden_message = saved_message;
21111   if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21112     {
21113       error ("invalid use of %<auto%> in template argument");
21114       r = error_mark_node;
21115     }
21116   return r;
21117 }
21118
21119 static tree
21120 cp_parser_trailing_type_id (cp_parser *parser)
21121 {
21122   return cp_parser_type_id_1 (parser, false, true);
21123 }
21124
21125 /* Parse a type-specifier-seq.
21126
21127    type-specifier-seq:
21128      type-specifier type-specifier-seq [opt]
21129
21130    GNU extension:
21131
21132    type-specifier-seq:
21133      attributes type-specifier-seq [opt]
21134
21135    If IS_DECLARATION is true, we are at the start of a "condition" or
21136    exception-declaration, so we might be followed by a declarator-id.
21137
21138    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21139    i.e. we've just seen "->".
21140
21141    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
21142
21143 static void
21144 cp_parser_type_specifier_seq (cp_parser* parser,
21145                               bool is_declaration,
21146                               bool is_trailing_return,
21147                               cp_decl_specifier_seq *type_specifier_seq)
21148 {
21149   bool seen_type_specifier = false;
21150   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21151   cp_token *start_token = NULL;
21152
21153   /* Clear the TYPE_SPECIFIER_SEQ.  */
21154   clear_decl_specs (type_specifier_seq);
21155
21156   /* In the context of a trailing return type, enum E { } is an
21157      elaborated-type-specifier followed by a function-body, not an
21158      enum-specifier.  */
21159   if (is_trailing_return)
21160     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21161
21162   /* Parse the type-specifiers and attributes.  */
21163   while (true)
21164     {
21165       tree type_specifier;
21166       bool is_cv_qualifier;
21167
21168       /* Check for attributes first.  */
21169       if (cp_next_tokens_can_be_attribute_p (parser))
21170         {
21171           type_specifier_seq->attributes
21172             = attr_chainon (type_specifier_seq->attributes,
21173                             cp_parser_attributes_opt (parser));
21174           continue;
21175         }
21176
21177       /* record the token of the beginning of the type specifier seq,
21178          for error reporting purposes*/
21179      if (!start_token)
21180        start_token = cp_lexer_peek_token (parser->lexer);
21181
21182       /* Look for the type-specifier.  */
21183       type_specifier = cp_parser_type_specifier (parser,
21184                                                  flags,
21185                                                  type_specifier_seq,
21186                                                  /*is_declaration=*/false,
21187                                                  NULL,
21188                                                  &is_cv_qualifier);
21189       if (!type_specifier)
21190         {
21191           /* If the first type-specifier could not be found, this is not a
21192              type-specifier-seq at all.  */
21193           if (!seen_type_specifier)
21194             {
21195               /* Set in_declarator_p to avoid skipping to the semicolon.  */
21196               int in_decl = parser->in_declarator_p;
21197               parser->in_declarator_p = true;
21198
21199               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21200                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21201                 cp_parser_error (parser, "expected type-specifier");
21202
21203               parser->in_declarator_p = in_decl;
21204
21205               type_specifier_seq->type = error_mark_node;
21206               return;
21207             }
21208           /* If subsequent type-specifiers could not be found, the
21209              type-specifier-seq is complete.  */
21210           break;
21211         }
21212
21213       seen_type_specifier = true;
21214       /* The standard says that a condition can be:
21215
21216             type-specifier-seq declarator = assignment-expression
21217
21218          However, given:
21219
21220            struct S {};
21221            if (int S = ...)
21222
21223          we should treat the "S" as a declarator, not as a
21224          type-specifier.  The standard doesn't say that explicitly for
21225          type-specifier-seq, but it does say that for
21226          decl-specifier-seq in an ordinary declaration.  Perhaps it
21227          would be clearer just to allow a decl-specifier-seq here, and
21228          then add a semantic restriction that if any decl-specifiers
21229          that are not type-specifiers appear, the program is invalid.  */
21230       if (is_declaration && !is_cv_qualifier)
21231         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21232     }
21233 }
21234
21235 /* Return whether the function currently being declared has an associated
21236    template parameter list.  */
21237
21238 static bool
21239 function_being_declared_is_template_p (cp_parser* parser)
21240 {
21241   if (!current_template_parms || processing_template_parmlist)
21242     return false;
21243
21244   if (parser->implicit_template_scope)
21245     return true;
21246
21247   if (at_class_scope_p ()
21248       && TYPE_BEING_DEFINED (current_class_type))
21249     return parser->num_template_parameter_lists != 0;
21250
21251   return ((int) parser->num_template_parameter_lists > template_class_depth
21252           (current_class_type));
21253 }
21254
21255 /* Parse a parameter-declaration-clause.
21256
21257    parameter-declaration-clause:
21258      parameter-declaration-list [opt] ... [opt]
21259      parameter-declaration-list , ...
21260
21261    Returns a representation for the parameter declarations.  A return
21262    value of NULL indicates a parameter-declaration-clause consisting
21263    only of an ellipsis.  */
21264
21265 static tree
21266 cp_parser_parameter_declaration_clause (cp_parser* parser)
21267 {
21268   tree parameters;
21269   cp_token *token;
21270   bool ellipsis_p;
21271   bool is_error;
21272
21273   temp_override<bool> cleanup
21274     (parser->auto_is_implicit_function_template_parm_p);
21275
21276   if (!processing_specialization
21277       && !processing_template_parmlist
21278       && !processing_explicit_instantiation
21279       /* default_arg_ok_p tracks whether this is a parameter-clause for an
21280          actual function or a random abstract declarator.  */
21281       && parser->default_arg_ok_p)
21282     if (!current_function_decl
21283         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21284       parser->auto_is_implicit_function_template_parm_p = true;
21285
21286   /* Peek at the next token.  */
21287   token = cp_lexer_peek_token (parser->lexer);
21288   /* Check for trivial parameter-declaration-clauses.  */
21289   if (token->type == CPP_ELLIPSIS)
21290     {
21291       /* Consume the `...' token.  */
21292       cp_lexer_consume_token (parser->lexer);
21293       return NULL_TREE;
21294     }
21295   else if (token->type == CPP_CLOSE_PAREN)
21296     /* There are no parameters.  */
21297     {
21298 #ifndef NO_IMPLICIT_EXTERN_C
21299       if (in_system_header_at (input_location)
21300           && current_class_type == NULL
21301           && current_lang_name == lang_name_c)
21302         return NULL_TREE;
21303       else
21304 #endif
21305         return void_list_node;
21306     }
21307   /* Check for `(void)', too, which is a special case.  */
21308   else if (token->keyword == RID_VOID
21309            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21310                == CPP_CLOSE_PAREN))
21311     {
21312       /* Consume the `void' token.  */
21313       cp_lexer_consume_token (parser->lexer);
21314       /* There are no parameters.  */
21315       return void_list_node;
21316     }
21317
21318   /* Parse the parameter-declaration-list.  */
21319   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21320   /* If a parse error occurred while parsing the
21321      parameter-declaration-list, then the entire
21322      parameter-declaration-clause is erroneous.  */
21323   if (is_error)
21324     return NULL;
21325
21326   /* Peek at the next token.  */
21327   token = cp_lexer_peek_token (parser->lexer);
21328   /* If it's a `,', the clause should terminate with an ellipsis.  */
21329   if (token->type == CPP_COMMA)
21330     {
21331       /* Consume the `,'.  */
21332       cp_lexer_consume_token (parser->lexer);
21333       /* Expect an ellipsis.  */
21334       ellipsis_p
21335         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21336     }
21337   /* It might also be `...' if the optional trailing `,' was
21338      omitted.  */
21339   else if (token->type == CPP_ELLIPSIS)
21340     {
21341       /* Consume the `...' token.  */
21342       cp_lexer_consume_token (parser->lexer);
21343       /* And remember that we saw it.  */
21344       ellipsis_p = true;
21345     }
21346   else
21347     ellipsis_p = false;
21348
21349   /* Finish the parameter list.  */
21350   if (!ellipsis_p)
21351     parameters = chainon (parameters, void_list_node);
21352
21353   return parameters;
21354 }
21355
21356 /* Parse a parameter-declaration-list.
21357
21358    parameter-declaration-list:
21359      parameter-declaration
21360      parameter-declaration-list , parameter-declaration
21361
21362    Returns a representation of the parameter-declaration-list, as for
21363    cp_parser_parameter_declaration_clause.  However, the
21364    `void_list_node' is never appended to the list.  Upon return,
21365    *IS_ERROR will be true iff an error occurred.  */
21366
21367 static tree
21368 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21369 {
21370   tree parameters = NULL_TREE;
21371   tree *tail = &parameters;
21372   bool saved_in_unbraced_linkage_specification_p;
21373   int index = 0;
21374
21375   /* Assume all will go well.  */
21376   *is_error = false;
21377   /* The special considerations that apply to a function within an
21378      unbraced linkage specifications do not apply to the parameters
21379      to the function.  */
21380   saved_in_unbraced_linkage_specification_p
21381     = parser->in_unbraced_linkage_specification_p;
21382   parser->in_unbraced_linkage_specification_p = false;
21383
21384   /* Look for more parameters.  */
21385   while (true)
21386     {
21387       cp_parameter_declarator *parameter;
21388       tree decl = error_mark_node;
21389       bool parenthesized_p = false;
21390
21391       /* Parse the parameter.  */
21392       parameter
21393         = cp_parser_parameter_declaration (parser,
21394                                            /*template_parm_p=*/false,
21395                                            &parenthesized_p);
21396
21397       /* We don't know yet if the enclosing context is deprecated, so wait
21398          and warn in grokparms if appropriate.  */
21399       deprecated_state = DEPRECATED_SUPPRESS;
21400
21401       if (parameter)
21402         {
21403           decl = grokdeclarator (parameter->declarator,
21404                                  &parameter->decl_specifiers,
21405                                  PARM,
21406                                  parameter->default_argument != NULL_TREE,
21407                                  &parameter->decl_specifiers.attributes);
21408           if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21409             DECL_SOURCE_LOCATION (decl) = parameter->loc;
21410         }
21411
21412       deprecated_state = DEPRECATED_NORMAL;
21413
21414       /* If a parse error occurred parsing the parameter declaration,
21415          then the entire parameter-declaration-list is erroneous.  */
21416       if (decl == error_mark_node)
21417         {
21418           *is_error = true;
21419           parameters = error_mark_node;
21420           break;
21421         }
21422
21423       if (parameter->decl_specifiers.attributes)
21424         cplus_decl_attributes (&decl,
21425                                parameter->decl_specifiers.attributes,
21426                                0);
21427       if (DECL_NAME (decl))
21428         decl = pushdecl (decl);
21429
21430       if (decl != error_mark_node)
21431         {
21432           retrofit_lang_decl (decl);
21433           DECL_PARM_INDEX (decl) = ++index;
21434           DECL_PARM_LEVEL (decl) = function_parm_depth ();
21435         }
21436
21437       /* Add the new parameter to the list.  */
21438       *tail = build_tree_list (parameter->default_argument, decl);
21439       tail = &TREE_CHAIN (*tail);
21440
21441       /* Peek at the next token.  */
21442       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21443           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21444           /* These are for Objective-C++ */
21445           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21446           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21447         /* The parameter-declaration-list is complete.  */
21448         break;
21449       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21450         {
21451           cp_token *token;
21452
21453           /* Peek at the next token.  */
21454           token = cp_lexer_peek_nth_token (parser->lexer, 2);
21455           /* If it's an ellipsis, then the list is complete.  */
21456           if (token->type == CPP_ELLIPSIS)
21457             break;
21458           /* Otherwise, there must be more parameters.  Consume the
21459              `,'.  */
21460           cp_lexer_consume_token (parser->lexer);
21461           /* When parsing something like:
21462
21463                 int i(float f, double d)
21464
21465              we can tell after seeing the declaration for "f" that we
21466              are not looking at an initialization of a variable "i",
21467              but rather at the declaration of a function "i".
21468
21469              Due to the fact that the parsing of template arguments
21470              (as specified to a template-id) requires backtracking we
21471              cannot use this technique when inside a template argument
21472              list.  */
21473           if (!parser->in_template_argument_list_p
21474               && !parser->in_type_id_in_expr_p
21475               && cp_parser_uncommitted_to_tentative_parse_p (parser)
21476               /* However, a parameter-declaration of the form
21477                  "float(f)" (which is a valid declaration of a
21478                  parameter "f") can also be interpreted as an
21479                  expression (the conversion of "f" to "float").  */
21480               && !parenthesized_p)
21481             cp_parser_commit_to_tentative_parse (parser);
21482         }
21483       else
21484         {
21485           cp_parser_error (parser, "expected %<,%> or %<...%>");
21486           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21487             cp_parser_skip_to_closing_parenthesis (parser,
21488                                                    /*recovering=*/true,
21489                                                    /*or_comma=*/false,
21490                                                    /*consume_paren=*/false);
21491           break;
21492         }
21493     }
21494
21495   parser->in_unbraced_linkage_specification_p
21496     = saved_in_unbraced_linkage_specification_p;
21497
21498   /* Reset implicit_template_scope if we are about to leave the function
21499      parameter list that introduced it.  Note that for out-of-line member
21500      definitions, there will be one or more class scopes before we get to
21501      the template parameter scope.  */
21502
21503   if (cp_binding_level *its = parser->implicit_template_scope)
21504     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21505       {
21506         while (maybe_its->kind == sk_class)
21507           maybe_its = maybe_its->level_chain;
21508         if (maybe_its == its)
21509           {
21510             parser->implicit_template_parms = 0;
21511             parser->implicit_template_scope = 0;
21512           }
21513       }
21514
21515   return parameters;
21516 }
21517
21518 /* Parse a parameter declaration.
21519
21520    parameter-declaration:
21521      decl-specifier-seq ... [opt] declarator
21522      decl-specifier-seq declarator = assignment-expression
21523      decl-specifier-seq ... [opt] abstract-declarator [opt]
21524      decl-specifier-seq abstract-declarator [opt] = assignment-expression
21525
21526    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21527    declares a template parameter.  (In that case, a non-nested `>'
21528    token encountered during the parsing of the assignment-expression
21529    is not interpreted as a greater-than operator.)
21530
21531    Returns a representation of the parameter, or NULL if an error
21532    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21533    true iff the declarator is of the form "(p)".  */
21534
21535 static cp_parameter_declarator *
21536 cp_parser_parameter_declaration (cp_parser *parser,
21537                                  bool template_parm_p,
21538                                  bool *parenthesized_p)
21539 {
21540   int declares_class_or_enum;
21541   cp_decl_specifier_seq decl_specifiers;
21542   cp_declarator *declarator;
21543   tree default_argument;
21544   cp_token *token = NULL, *declarator_token_start = NULL;
21545   const char *saved_message;
21546   bool template_parameter_pack_p = false;
21547
21548   /* In a template parameter, `>' is not an operator.
21549
21550      [temp.param]
21551
21552      When parsing a default template-argument for a non-type
21553      template-parameter, the first non-nested `>' is taken as the end
21554      of the template parameter-list rather than a greater-than
21555      operator.  */
21556
21557   /* Type definitions may not appear in parameter types.  */
21558   saved_message = parser->type_definition_forbidden_message;
21559   parser->type_definition_forbidden_message
21560     = G_("types may not be defined in parameter types");
21561
21562   int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21563                            TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21564                                             (current_template_parms)) : 0);
21565
21566   /* Parse the declaration-specifiers.  */
21567   cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21568   cp_parser_decl_specifier_seq (parser,
21569                                 CP_PARSER_FLAGS_NONE,
21570                                 &decl_specifiers,
21571                                 &declares_class_or_enum);
21572
21573   /* Complain about missing 'typename' or other invalid type names.  */
21574   if (!decl_specifiers.any_type_specifiers_p
21575       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21576     decl_specifiers.type = error_mark_node;
21577
21578   /* If an error occurred, there's no reason to attempt to parse the
21579      rest of the declaration.  */
21580   if (cp_parser_error_occurred (parser))
21581     {
21582       parser->type_definition_forbidden_message = saved_message;
21583       return NULL;
21584     }
21585
21586   /* Peek at the next token.  */
21587   token = cp_lexer_peek_token (parser->lexer);
21588
21589   /* If the next token is a `)', `,', `=', `>', or `...', then there
21590      is no declarator. However, when variadic templates are enabled,
21591      there may be a declarator following `...'.  */
21592   if (token->type == CPP_CLOSE_PAREN
21593       || token->type == CPP_COMMA
21594       || token->type == CPP_EQ
21595       || token->type == CPP_GREATER)
21596     {
21597       declarator = NULL;
21598       if (parenthesized_p)
21599         *parenthesized_p = false;
21600     }
21601   /* Otherwise, there should be a declarator.  */
21602   else
21603     {
21604       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21605       parser->default_arg_ok_p = false;
21606
21607       /* After seeing a decl-specifier-seq, if the next token is not a
21608          "(", there is no possibility that the code is a valid
21609          expression.  Therefore, if parsing tentatively, we commit at
21610          this point.  */
21611       if (!parser->in_template_argument_list_p
21612           /* In an expression context, having seen:
21613
21614                (int((char ...
21615
21616              we cannot be sure whether we are looking at a
21617              function-type (taking a "char" as a parameter) or a cast
21618              of some object of type "char" to "int".  */
21619           && !parser->in_type_id_in_expr_p
21620           && cp_parser_uncommitted_to_tentative_parse_p (parser)
21621           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21622           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21623         cp_parser_commit_to_tentative_parse (parser);
21624       /* Parse the declarator.  */
21625       declarator_token_start = token;
21626       declarator = cp_parser_declarator (parser,
21627                                          CP_PARSER_DECLARATOR_EITHER,
21628                                          /*ctor_dtor_or_conv_p=*/NULL,
21629                                          parenthesized_p,
21630                                          /*member_p=*/false,
21631                                          /*friend_p=*/false);
21632       parser->default_arg_ok_p = saved_default_arg_ok_p;
21633       /* After the declarator, allow more attributes.  */
21634       decl_specifiers.attributes
21635         = attr_chainon (decl_specifiers.attributes,
21636                         cp_parser_attributes_opt (parser));
21637
21638       /* If the declarator is a template parameter pack, remember that and
21639          clear the flag in the declarator itself so we don't get errors
21640          from grokdeclarator.  */
21641       if (template_parm_p && declarator && declarator->parameter_pack_p)
21642         {
21643           declarator->parameter_pack_p = false;
21644           template_parameter_pack_p = true;
21645         }
21646     }
21647
21648   /* If the next token is an ellipsis, and we have not seen a declarator
21649      name, and if either the type of the declarator contains parameter
21650      packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21651      for, eg, abbreviated integral type names), then we actually have a
21652      parameter pack expansion expression. Otherwise, leave the ellipsis
21653      for a C-style variadic function. */
21654   token = cp_lexer_peek_token (parser->lexer);
21655
21656   /* If a function parameter pack was specified and an implicit template
21657      parameter was introduced during cp_parser_parameter_declaration,
21658      change any implicit parameters introduced into packs.  */
21659   if (parser->implicit_template_parms
21660       && ((token->type == CPP_ELLIPSIS
21661            && declarator_can_be_parameter_pack (declarator))
21662           || (declarator && declarator->parameter_pack_p)))
21663     {
21664       int latest_template_parm_idx = TREE_VEC_LENGTH
21665         (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21666
21667       if (latest_template_parm_idx != template_parm_idx)
21668         decl_specifiers.type = convert_generic_types_to_packs
21669           (decl_specifiers.type,
21670            template_parm_idx, latest_template_parm_idx);
21671     }
21672
21673   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21674     {
21675       tree type = decl_specifiers.type;
21676
21677       if (type && DECL_P (type))
21678         type = TREE_TYPE (type);
21679
21680       if (((type
21681             && TREE_CODE (type) != TYPE_PACK_EXPANSION
21682             && (template_parm_p || uses_parameter_packs (type)))
21683            || (!type && template_parm_p))
21684           && declarator_can_be_parameter_pack (declarator))
21685         {
21686           /* Consume the `...'. */
21687           cp_lexer_consume_token (parser->lexer);
21688           maybe_warn_variadic_templates ();
21689           
21690           /* Build a pack expansion type */
21691           if (template_parm_p)
21692             template_parameter_pack_p = true;
21693           else if (declarator)
21694             declarator->parameter_pack_p = true;
21695           else
21696             decl_specifiers.type = make_pack_expansion (type);
21697         }
21698     }
21699
21700   /* The restriction on defining new types applies only to the type
21701      of the parameter, not to the default argument.  */
21702   parser->type_definition_forbidden_message = saved_message;
21703
21704   /* If the next token is `=', then process a default argument.  */
21705   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21706     {
21707       tree type = decl_specifiers.type;
21708       token = cp_lexer_peek_token (parser->lexer);
21709       /* If we are defining a class, then the tokens that make up the
21710          default argument must be saved and processed later.  */
21711       if (!template_parm_p && at_class_scope_p ()
21712           && TYPE_BEING_DEFINED (current_class_type)
21713           && !LAMBDA_TYPE_P (current_class_type))
21714         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21715
21716       // A constrained-type-specifier may declare a type template-parameter.
21717       else if (declares_constrained_type_template_parameter (type))
21718         default_argument
21719           = cp_parser_default_type_template_argument (parser);
21720
21721       // A constrained-type-specifier may declare a template-template-parameter.
21722       else if (declares_constrained_template_template_parameter (type))
21723         default_argument
21724           = cp_parser_default_template_template_argument (parser);
21725
21726       /* Outside of a class definition, we can just parse the
21727          assignment-expression.  */
21728       else
21729         default_argument
21730           = cp_parser_default_argument (parser, template_parm_p);
21731
21732       if (!parser->default_arg_ok_p)
21733         {
21734           permerror (token->location,
21735                      "default arguments are only "
21736                      "permitted for function parameters");
21737         }
21738       else if ((declarator && declarator->parameter_pack_p)
21739                || template_parameter_pack_p
21740                || (decl_specifiers.type
21741                    && PACK_EXPANSION_P (decl_specifiers.type)))
21742         {
21743           /* Find the name of the parameter pack.  */     
21744           cp_declarator *id_declarator = declarator;
21745           while (id_declarator && id_declarator->kind != cdk_id)
21746             id_declarator = id_declarator->declarator;
21747           
21748           if (id_declarator && id_declarator->kind == cdk_id)
21749             error_at (declarator_token_start->location,
21750                       template_parm_p
21751                       ? G_("template parameter pack %qD "
21752                            "cannot have a default argument")
21753                       : G_("parameter pack %qD cannot have "
21754                            "a default argument"),
21755                       id_declarator->u.id.unqualified_name);
21756           else
21757             error_at (declarator_token_start->location,
21758                       template_parm_p
21759                       ? G_("template parameter pack cannot have "
21760                            "a default argument")
21761                       : G_("parameter pack cannot have a "
21762                            "default argument"));
21763
21764           default_argument = NULL_TREE;
21765         }
21766     }
21767   else
21768     default_argument = NULL_TREE;
21769
21770   /* Generate a location for the parameter, ranging from the start of the
21771      initial token to the end of the final token (using input_location for
21772      the latter, set up by cp_lexer_set_source_position_from_token when
21773      consuming tokens).
21774
21775      If we have a identifier, then use it for the caret location, e.g.
21776
21777        extern int callee (int one, int (*two)(int, int), float three);
21778                                    ~~~~~~^~~~~~~~~~~~~~
21779
21780      otherwise, reuse the start location for the caret location e.g.:
21781
21782        extern int callee (int one, int (*)(int, int), float three);
21783                                    ^~~~~~~~~~~~~~~~~
21784
21785   */
21786   location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21787                           ? declarator->id_loc
21788                           : decl_spec_token_start->location);
21789   location_t param_loc = make_location (caret_loc,
21790                                         decl_spec_token_start->location,
21791                                         input_location);
21792
21793   return make_parameter_declarator (&decl_specifiers,
21794                                     declarator,
21795                                     default_argument,
21796                                     param_loc,
21797                                     template_parameter_pack_p);
21798 }
21799
21800 /* Parse a default argument and return it.
21801
21802    TEMPLATE_PARM_P is true if this is a default argument for a
21803    non-type template parameter.  */
21804 static tree
21805 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21806 {
21807   tree default_argument = NULL_TREE;
21808   bool saved_greater_than_is_operator_p;
21809   bool saved_local_variables_forbidden_p;
21810   bool non_constant_p, is_direct_init;
21811
21812   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21813      set correctly.  */
21814   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21815   parser->greater_than_is_operator_p = !template_parm_p;
21816   /* Local variable names (and the `this' keyword) may not
21817      appear in a default argument.  */
21818   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21819   parser->local_variables_forbidden_p = true;
21820   /* Parse the assignment-expression.  */
21821   if (template_parm_p)
21822     push_deferring_access_checks (dk_no_deferred);
21823   tree saved_class_ptr = NULL_TREE;
21824   tree saved_class_ref = NULL_TREE;
21825   /* The "this" pointer is not valid in a default argument.  */
21826   if (cfun)
21827     {
21828       saved_class_ptr = current_class_ptr;
21829       cp_function_chain->x_current_class_ptr = NULL_TREE;
21830       saved_class_ref = current_class_ref;
21831       cp_function_chain->x_current_class_ref = NULL_TREE;
21832     }
21833   default_argument
21834     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21835   /* Restore the "this" pointer.  */
21836   if (cfun)
21837     {
21838       cp_function_chain->x_current_class_ptr = saved_class_ptr;
21839       cp_function_chain->x_current_class_ref = saved_class_ref;
21840     }
21841   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21842     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21843   if (template_parm_p)
21844     pop_deferring_access_checks ();
21845   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21846   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21847
21848   return default_argument;
21849 }
21850
21851 /* Parse a function-body.
21852
21853    function-body:
21854      compound_statement  */
21855
21856 static void
21857 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21858 {
21859   cp_parser_compound_statement (parser, NULL, (in_function_try_block
21860                                                ? BCS_TRY_BLOCK : BCS_NORMAL),
21861                                 true);
21862 }
21863
21864 /* Parse a ctor-initializer-opt followed by a function-body.  Return
21865    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
21866    is true we are parsing a function-try-block.  */
21867
21868 static void
21869 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21870                                                   bool in_function_try_block)
21871 {
21872   tree body, list;
21873   const bool check_body_p =
21874      DECL_CONSTRUCTOR_P (current_function_decl)
21875      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21876   tree last = NULL;
21877
21878   /* Begin the function body.  */
21879   body = begin_function_body ();
21880   /* Parse the optional ctor-initializer.  */
21881   cp_parser_ctor_initializer_opt (parser);
21882
21883   /* If we're parsing a constexpr constructor definition, we need
21884      to check that the constructor body is indeed empty.  However,
21885      before we get to cp_parser_function_body lot of junk has been
21886      generated, so we can't just check that we have an empty block.
21887      Rather we take a snapshot of the outermost block, and check whether
21888      cp_parser_function_body changed its state.  */
21889   if (check_body_p)
21890     {
21891       list = cur_stmt_list;
21892       if (STATEMENT_LIST_TAIL (list))
21893         last = STATEMENT_LIST_TAIL (list)->stmt;
21894     }
21895   /* Parse the function-body.  */
21896   cp_parser_function_body (parser, in_function_try_block);
21897   if (check_body_p)
21898     check_constexpr_ctor_body (last, list, /*complain=*/true);
21899   /* Finish the function body.  */
21900   finish_function_body (body);
21901 }
21902
21903 /* Parse an initializer.
21904
21905    initializer:
21906      = initializer-clause
21907      ( expression-list )
21908
21909    Returns an expression representing the initializer.  If no
21910    initializer is present, NULL_TREE is returned.
21911
21912    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21913    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
21914    set to TRUE if there is no initializer present.  If there is an
21915    initializer, and it is not a constant-expression, *NON_CONSTANT_P
21916    is set to true; otherwise it is set to false.  */
21917
21918 static tree
21919 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21920                        bool* non_constant_p, bool subexpression_p)
21921 {
21922   cp_token *token;
21923   tree init;
21924
21925   /* Peek at the next token.  */
21926   token = cp_lexer_peek_token (parser->lexer);
21927
21928   /* Let our caller know whether or not this initializer was
21929      parenthesized.  */
21930   *is_direct_init = (token->type != CPP_EQ);
21931   /* Assume that the initializer is constant.  */
21932   *non_constant_p = false;
21933
21934   if (token->type == CPP_EQ)
21935     {
21936       /* Consume the `='.  */
21937       cp_lexer_consume_token (parser->lexer);
21938       /* Parse the initializer-clause.  */
21939       init = cp_parser_initializer_clause (parser, non_constant_p);
21940     }
21941   else if (token->type == CPP_OPEN_PAREN)
21942     {
21943       vec<tree, va_gc> *vec;
21944       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21945                                                      /*cast_p=*/false,
21946                                                      /*allow_expansion_p=*/true,
21947                                                      non_constant_p);
21948       if (vec == NULL)
21949         return error_mark_node;
21950       init = build_tree_list_vec (vec);
21951       release_tree_vector (vec);
21952     }
21953   else if (token->type == CPP_OPEN_BRACE)
21954     {
21955       cp_lexer_set_source_position (parser->lexer);
21956       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21957       init = cp_parser_braced_list (parser, non_constant_p);
21958       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21959     }
21960   else
21961     {
21962       /* Anything else is an error.  */
21963       cp_parser_error (parser, "expected initializer");
21964       init = error_mark_node;
21965     }
21966
21967   if (!subexpression_p && check_for_bare_parameter_packs (init))
21968     init = error_mark_node;
21969
21970   return init;
21971 }
21972
21973 /* Parse an initializer-clause.
21974
21975    initializer-clause:
21976      assignment-expression
21977      braced-init-list
21978
21979    Returns an expression representing the initializer.
21980
21981    If the `assignment-expression' production is used the value
21982    returned is simply a representation for the expression.
21983
21984    Otherwise, calls cp_parser_braced_list.  */
21985
21986 static cp_expr
21987 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21988 {
21989   cp_expr initializer;
21990
21991   /* Assume the expression is constant.  */
21992   *non_constant_p = false;
21993
21994   /* If it is not a `{', then we are looking at an
21995      assignment-expression.  */
21996   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21997     {
21998       initializer
21999         = cp_parser_constant_expression (parser,
22000                                         /*allow_non_constant_p=*/true,
22001                                         non_constant_p);
22002     }
22003   else
22004     initializer = cp_parser_braced_list (parser, non_constant_p);
22005
22006   return initializer;
22007 }
22008
22009 /* Parse a brace-enclosed initializer list.
22010
22011    braced-init-list:
22012      { initializer-list , [opt] }
22013      { designated-initializer-list , [opt] }
22014      { }
22015
22016    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
22017    the elements of the initializer-list (or NULL, if the last
22018    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
22019    NULL_TREE.  There is no way to detect whether or not the optional
22020    trailing `,' was provided.  NON_CONSTANT_P is as for
22021    cp_parser_initializer.  */     
22022
22023 static cp_expr
22024 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22025 {
22026   tree initializer;
22027   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22028
22029   /* Consume the `{' token.  */
22030   matching_braces braces;
22031   braces.require_open (parser);
22032   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
22033   initializer = make_node (CONSTRUCTOR);
22034   /* If it's not a `}', then there is a non-trivial initializer.  */
22035   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22036     {
22037       /* Parse the initializer list.  */
22038       CONSTRUCTOR_ELTS (initializer)
22039         = cp_parser_initializer_list (parser, non_constant_p);
22040       /* A trailing `,' token is allowed.  */
22041       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22042         cp_lexer_consume_token (parser->lexer);
22043     }
22044   else
22045     *non_constant_p = false;
22046   /* Now, there should be a trailing `}'.  */
22047   location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22048   braces.require_close (parser);
22049   TREE_TYPE (initializer) = init_list_type_node;
22050
22051   cp_expr result (initializer);
22052   /* Build a location of the form:
22053        { ... }
22054        ^~~~~~~
22055      with caret==start at the open brace, finish at the close brace.  */
22056   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22057   result.set_location (combined_loc);
22058   return result;
22059 }
22060
22061 /* Consume tokens up to, and including, the next non-nested closing `]'.
22062    Returns true iff we found a closing `]'.  */
22063
22064 static bool
22065 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22066 {
22067   unsigned square_depth = 0;
22068
22069   while (true)
22070     {
22071       cp_token * token = cp_lexer_peek_token (parser->lexer);
22072
22073       switch (token->type)
22074         {
22075         case CPP_EOF:
22076         case CPP_PRAGMA_EOL:
22077           /* If we've run out of tokens, then there is no closing `]'.  */
22078           return false;
22079
22080         case CPP_OPEN_SQUARE:
22081           ++square_depth;
22082           break;
22083
22084         case CPP_CLOSE_SQUARE:
22085           if (!square_depth--)
22086             {
22087               cp_lexer_consume_token (parser->lexer);
22088               return true;
22089             }
22090           break;
22091
22092         default:
22093           break;
22094         }
22095
22096       /* Consume the token.  */
22097       cp_lexer_consume_token (parser->lexer);
22098     }
22099 }
22100
22101 /* Return true if we are looking at an array-designator, false otherwise.  */
22102
22103 static bool
22104 cp_parser_array_designator_p (cp_parser *parser)
22105 {
22106   /* Consume the `['.  */
22107   cp_lexer_consume_token (parser->lexer);
22108
22109   cp_lexer_save_tokens (parser->lexer);
22110
22111   /* Skip tokens until the next token is a closing square bracket.
22112      If we find the closing `]', and the next token is a `=', then
22113      we are looking at an array designator.  */
22114   bool array_designator_p
22115     = (cp_parser_skip_to_closing_square_bracket (parser)
22116        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22117   
22118   /* Roll back the tokens we skipped.  */
22119   cp_lexer_rollback_tokens (parser->lexer);
22120
22121   return array_designator_p;
22122 }
22123
22124 /* Parse an initializer-list.
22125
22126    initializer-list:
22127      initializer-clause ... [opt]
22128      initializer-list , initializer-clause ... [opt]
22129
22130    C++2A Extension:
22131
22132    designated-initializer-list:
22133      designated-initializer-clause
22134      designated-initializer-list , designated-initializer-clause
22135
22136    designated-initializer-clause:
22137      designator brace-or-equal-initializer
22138
22139    designator:
22140      . identifier
22141
22142    GNU Extension:
22143
22144    initializer-list:
22145      designation initializer-clause ...[opt]
22146      initializer-list , designation initializer-clause ...[opt]
22147
22148    designation:
22149      . identifier =
22150      identifier :
22151      [ constant-expression ] =
22152
22153    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
22154    for the initializer.  If the INDEX of the elt is non-NULL, it is the
22155    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
22156    as for cp_parser_initializer.  */
22157
22158 static vec<constructor_elt, va_gc> *
22159 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22160 {
22161   vec<constructor_elt, va_gc> *v = NULL;
22162   bool first_p = true;
22163   tree first_designator = NULL_TREE;
22164
22165   /* Assume all of the expressions are constant.  */
22166   *non_constant_p = false;
22167
22168   /* Parse the rest of the list.  */
22169   while (true)
22170     {
22171       cp_token *token;
22172       tree designator;
22173       tree initializer;
22174       bool clause_non_constant_p;
22175       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22176
22177       /* Handle the C++2A syntax, '. id ='.  */
22178       if ((cxx_dialect >= cxx2a
22179            || cp_parser_allow_gnu_extensions_p (parser))
22180           && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22181           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22182           && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22183               || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22184                   == CPP_OPEN_BRACE)))
22185         {
22186           if (cxx_dialect < cxx2a)
22187             pedwarn (loc, OPT_Wpedantic,
22188                      "C++ designated initializers only available with "
22189                      "-std=c++2a or -std=gnu++2a");
22190           /* Consume the `.'.  */
22191           cp_lexer_consume_token (parser->lexer);
22192           /* Consume the identifier.  */
22193           designator = cp_lexer_consume_token (parser->lexer)->u.value;
22194           if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22195             /* Consume the `='.  */
22196             cp_lexer_consume_token (parser->lexer);
22197         }
22198       /* Also, if the next token is an identifier and the following one is a
22199          colon, we are looking at the GNU designated-initializer
22200          syntax.  */
22201       else if (cp_parser_allow_gnu_extensions_p (parser)
22202                && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22203                && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22204                    == CPP_COLON))
22205         {
22206           /* Warn the user that they are using an extension.  */
22207           pedwarn (loc, OPT_Wpedantic,
22208                    "ISO C++ does not allow GNU designated initializers");
22209           /* Consume the identifier.  */
22210           designator = cp_lexer_consume_token (parser->lexer)->u.value;
22211           /* Consume the `:'.  */
22212           cp_lexer_consume_token (parser->lexer);
22213         }
22214       /* Also handle C99 array designators, '[ const ] ='.  */
22215       else if (cp_parser_allow_gnu_extensions_p (parser)
22216                && !c_dialect_objc ()
22217                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22218         {
22219           /* In C++11, [ could start a lambda-introducer.  */
22220           bool non_const = false;
22221
22222           cp_parser_parse_tentatively (parser);
22223
22224           if (!cp_parser_array_designator_p (parser))
22225             {
22226               cp_parser_simulate_error (parser);
22227               designator = NULL_TREE;
22228             }
22229           else
22230             {
22231               designator = cp_parser_constant_expression (parser, true,
22232                                                           &non_const);
22233               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22234               cp_parser_require (parser, CPP_EQ, RT_EQ);
22235             }
22236
22237           if (!cp_parser_parse_definitely (parser))
22238             designator = NULL_TREE;
22239           else if (non_const
22240                    && (!require_potential_rvalue_constant_expression
22241                        (designator)))
22242             designator = NULL_TREE;
22243           if (designator)
22244             /* Warn the user that they are using an extension.  */
22245             pedwarn (loc, OPT_Wpedantic,
22246                      "ISO C++ does not allow C99 designated initializers");
22247         }
22248       else
22249         designator = NULL_TREE;
22250
22251       if (first_p)
22252         {
22253           first_designator = designator;
22254           first_p = false;
22255         }
22256       else if (cxx_dialect >= cxx2a
22257                && first_designator != error_mark_node
22258                && (!first_designator != !designator))
22259         {
22260           error_at (loc, "either all initializer clauses should be designated "
22261                          "or none of them should be");
22262           first_designator = error_mark_node;
22263         }
22264       else if (cxx_dialect < cxx2a && !first_designator)
22265         first_designator = designator;
22266
22267       /* Parse the initializer.  */
22268       initializer = cp_parser_initializer_clause (parser,
22269                                                   &clause_non_constant_p);
22270       /* If any clause is non-constant, so is the entire initializer.  */
22271       if (clause_non_constant_p)
22272         *non_constant_p = true;
22273
22274       /* If we have an ellipsis, this is an initializer pack
22275          expansion.  */
22276       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22277         {
22278           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22279
22280           /* Consume the `...'.  */
22281           cp_lexer_consume_token (parser->lexer);
22282
22283           if (designator && cxx_dialect >= cxx2a)
22284             error_at (loc,
22285                       "%<...%> not allowed in designated initializer list");
22286
22287           /* Turn the initializer into an initializer expansion.  */
22288           initializer = make_pack_expansion (initializer);
22289         }
22290
22291       /* Add it to the vector.  */
22292       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22293
22294       /* If the next token is not a comma, we have reached the end of
22295          the list.  */
22296       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22297         break;
22298
22299       /* Peek at the next token.  */
22300       token = cp_lexer_peek_nth_token (parser->lexer, 2);
22301       /* If the next token is a `}', then we're still done.  An
22302          initializer-clause can have a trailing `,' after the
22303          initializer-list and before the closing `}'.  */
22304       if (token->type == CPP_CLOSE_BRACE)
22305         break;
22306
22307       /* Consume the `,' token.  */
22308       cp_lexer_consume_token (parser->lexer);
22309     }
22310
22311   /* The same identifier shall not appear in multiple designators
22312      of a designated-initializer-list.  */
22313   if (first_designator)
22314     {
22315       unsigned int i;
22316       tree designator, val;
22317       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22318         if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22319           {
22320             if (IDENTIFIER_MARKED (designator))
22321               {
22322                 error_at (EXPR_LOC_OR_LOC (val, input_location),
22323                           "%<.%s%> designator used multiple times in "
22324                           "the same initializer list",
22325                           IDENTIFIER_POINTER (designator));
22326                 (*v)[i].index = NULL_TREE;
22327               }
22328             else
22329               IDENTIFIER_MARKED (designator) = 1;
22330           }
22331       FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22332         if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22333           IDENTIFIER_MARKED (designator) = 0;
22334     }
22335
22336   return v;
22337 }
22338
22339 /* Classes [gram.class] */
22340
22341 /* Parse a class-name.
22342
22343    class-name:
22344      identifier
22345      template-id
22346
22347    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22348    to indicate that names looked up in dependent types should be
22349    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
22350    keyword has been used to indicate that the name that appears next
22351    is a template.  TAG_TYPE indicates the explicit tag given before
22352    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
22353    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
22354    is the class being defined in a class-head.  If ENUM_OK is TRUE,
22355    enum-names are also accepted.
22356
22357    Returns the TYPE_DECL representing the class.  */
22358
22359 static tree
22360 cp_parser_class_name (cp_parser *parser,
22361                       bool typename_keyword_p,
22362                       bool template_keyword_p,
22363                       enum tag_types tag_type,
22364                       bool check_dependency_p,
22365                       bool class_head_p,
22366                       bool is_declaration,
22367                       bool enum_ok)
22368 {
22369   tree decl;
22370   tree scope;
22371   bool typename_p;
22372   cp_token *token;
22373   tree identifier = NULL_TREE;
22374
22375   /* All class-names start with an identifier.  */
22376   token = cp_lexer_peek_token (parser->lexer);
22377   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22378     {
22379       cp_parser_error (parser, "expected class-name");
22380       return error_mark_node;
22381     }
22382
22383   /* PARSER->SCOPE can be cleared when parsing the template-arguments
22384      to a template-id, so we save it here.  */
22385   scope = parser->scope;
22386   if (scope == error_mark_node)
22387     return error_mark_node;
22388
22389   /* Any name names a type if we're following the `typename' keyword
22390      in a qualified name where the enclosing scope is type-dependent.  */
22391   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22392                 && dependent_type_p (scope));
22393   /* Handle the common case (an identifier, but not a template-id)
22394      efficiently.  */
22395   if (token->type == CPP_NAME
22396       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22397     {
22398       cp_token *identifier_token;
22399       bool ambiguous_p;
22400
22401       /* Look for the identifier.  */
22402       identifier_token = cp_lexer_peek_token (parser->lexer);
22403       ambiguous_p = identifier_token->error_reported;
22404       identifier = cp_parser_identifier (parser);
22405       /* If the next token isn't an identifier, we are certainly not
22406          looking at a class-name.  */
22407       if (identifier == error_mark_node)
22408         decl = error_mark_node;
22409       /* If we know this is a type-name, there's no need to look it
22410          up.  */
22411       else if (typename_p)
22412         decl = identifier;
22413       else
22414         {
22415           tree ambiguous_decls;
22416           /* If we already know that this lookup is ambiguous, then
22417              we've already issued an error message; there's no reason
22418              to check again.  */
22419           if (ambiguous_p)
22420             {
22421               cp_parser_simulate_error (parser);
22422               return error_mark_node;
22423             }
22424           /* If the next token is a `::', then the name must be a type
22425              name.
22426
22427              [basic.lookup.qual]
22428
22429              During the lookup for a name preceding the :: scope
22430              resolution operator, object, function, and enumerator
22431              names are ignored.  */
22432           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22433             tag_type = scope_type;
22434           /* Look up the name.  */
22435           decl = cp_parser_lookup_name (parser, identifier,
22436                                         tag_type,
22437                                         /*is_template=*/false,
22438                                         /*is_namespace=*/false,
22439                                         check_dependency_p,
22440                                         &ambiguous_decls,
22441                                         identifier_token->location);
22442           if (ambiguous_decls)
22443             {
22444               if (cp_parser_parsing_tentatively (parser))
22445                 cp_parser_simulate_error (parser);
22446               return error_mark_node;
22447             }
22448         }
22449     }
22450   else
22451     {
22452       /* Try a template-id.  */
22453       decl = cp_parser_template_id (parser, template_keyword_p,
22454                                     check_dependency_p,
22455                                     tag_type,
22456                                     is_declaration);
22457       if (decl == error_mark_node)
22458         return error_mark_node;
22459     }
22460
22461   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22462
22463   /* If this is a typename, create a TYPENAME_TYPE.  */
22464   if (typename_p && decl != error_mark_node)
22465     {
22466       decl = make_typename_type (scope, decl, typename_type,
22467                                  /*complain=*/tf_error);
22468       if (decl != error_mark_node)
22469         decl = TYPE_NAME (decl);
22470     }
22471
22472   decl = strip_using_decl (decl);
22473
22474   /* Check to see that it is really the name of a class.  */
22475   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22476       && identifier_p (TREE_OPERAND (decl, 0))
22477       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22478     /* Situations like this:
22479
22480          template <typename T> struct A {
22481            typename T::template X<int>::I i;
22482          };
22483
22484        are problematic.  Is `T::template X<int>' a class-name?  The
22485        standard does not seem to be definitive, but there is no other
22486        valid interpretation of the following `::'.  Therefore, those
22487        names are considered class-names.  */
22488     {
22489       decl = make_typename_type (scope, decl, tag_type, tf_error);
22490       if (decl != error_mark_node)
22491         decl = TYPE_NAME (decl);
22492     }
22493   else if (TREE_CODE (decl) != TYPE_DECL
22494            || TREE_TYPE (decl) == error_mark_node
22495            || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22496                 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22497            /* In Objective-C 2.0, a classname followed by '.' starts a
22498               dot-syntax expression, and it's not a type-name.  */
22499            || (c_dialect_objc ()
22500                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
22501                && objc_is_class_name (decl)))
22502     decl = error_mark_node;
22503
22504   if (decl == error_mark_node)
22505     cp_parser_error (parser, "expected class-name");
22506   else if (identifier && !parser->scope)
22507     maybe_note_name_used_in_class (identifier, decl);
22508
22509   return decl;
22510 }
22511
22512 /* Parse a class-specifier.
22513
22514    class-specifier:
22515      class-head { member-specification [opt] }
22516
22517    Returns the TREE_TYPE representing the class.  */
22518
22519 static tree
22520 cp_parser_class_specifier_1 (cp_parser* parser)
22521 {
22522   tree type;
22523   tree attributes = NULL_TREE;
22524   bool nested_name_specifier_p;
22525   unsigned saved_num_template_parameter_lists;
22526   bool saved_in_function_body;
22527   unsigned char in_statement;
22528   bool in_switch_statement_p;
22529   bool saved_in_unbraced_linkage_specification_p;
22530   tree old_scope = NULL_TREE;
22531   tree scope = NULL_TREE;
22532   cp_token *closing_brace;
22533
22534   push_deferring_access_checks (dk_no_deferred);
22535
22536   /* Parse the class-head.  */
22537   type = cp_parser_class_head (parser,
22538                                &nested_name_specifier_p);
22539   /* If the class-head was a semantic disaster, skip the entire body
22540      of the class.  */
22541   if (!type)
22542     {
22543       cp_parser_skip_to_end_of_block_or_statement (parser);
22544       pop_deferring_access_checks ();
22545       return error_mark_node;
22546     }
22547
22548   /* Look for the `{'.  */
22549   matching_braces braces;
22550   if (!braces.require_open (parser))
22551     {
22552       pop_deferring_access_checks ();
22553       return error_mark_node;
22554     }
22555
22556   cp_ensure_no_omp_declare_simd (parser);
22557   cp_ensure_no_oacc_routine (parser);
22558
22559   /* Issue an error message if type-definitions are forbidden here.  */
22560   bool type_definition_ok_p = cp_parser_check_type_definition (parser);
22561   /* Remember that we are defining one more class.  */
22562   ++parser->num_classes_being_defined;
22563   /* Inside the class, surrounding template-parameter-lists do not
22564      apply.  */
22565   saved_num_template_parameter_lists
22566     = parser->num_template_parameter_lists;
22567   parser->num_template_parameter_lists = 0;
22568   /* We are not in a function body.  */
22569   saved_in_function_body = parser->in_function_body;
22570   parser->in_function_body = false;
22571   /* Or in a loop.  */
22572   in_statement = parser->in_statement;
22573   parser->in_statement = 0;
22574   /* Or in a switch.  */
22575   in_switch_statement_p = parser->in_switch_statement_p;
22576   parser->in_switch_statement_p = false;
22577   /* We are not immediately inside an extern "lang" block.  */
22578   saved_in_unbraced_linkage_specification_p
22579     = parser->in_unbraced_linkage_specification_p;
22580   parser->in_unbraced_linkage_specification_p = false;
22581
22582   // Associate constraints with the type.
22583   if (flag_concepts)
22584     type = associate_classtype_constraints (type);
22585
22586   /* Start the class.  */
22587   if (nested_name_specifier_p)
22588     {
22589       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22590       old_scope = push_inner_scope (scope);
22591     }
22592   type = begin_class_definition (type);
22593
22594   if (type == error_mark_node)
22595     /* If the type is erroneous, skip the entire body of the class.  */
22596     cp_parser_skip_to_closing_brace (parser);
22597   else
22598     /* Parse the member-specification.  */
22599     cp_parser_member_specification_opt (parser);
22600
22601   /* Look for the trailing `}'.  */
22602   closing_brace = braces.require_close (parser);
22603   /* Look for trailing attributes to apply to this class.  */
22604   if (cp_parser_allow_gnu_extensions_p (parser))
22605     attributes = cp_parser_gnu_attributes_opt (parser);
22606   if (type != error_mark_node)
22607     type = finish_struct (type, attributes);
22608   if (nested_name_specifier_p)
22609     pop_inner_scope (old_scope, scope);
22610
22611   /* We've finished a type definition.  Check for the common syntax
22612      error of forgetting a semicolon after the definition.  We need to
22613      be careful, as we can't just check for not-a-semicolon and be done
22614      with it; the user might have typed:
22615
22616      class X { } c = ...;
22617      class X { } *p = ...;
22618
22619      and so forth.  Instead, enumerate all the possible tokens that
22620      might follow this production; if we don't see one of them, then
22621      complain and silently insert the semicolon.  */
22622   {
22623     cp_token *token = cp_lexer_peek_token (parser->lexer);
22624     bool want_semicolon = true;
22625
22626     if (cp_next_tokens_can_be_std_attribute_p (parser))
22627       /* Don't try to parse c++11 attributes here.  As per the
22628          grammar, that should be a task for
22629          cp_parser_decl_specifier_seq.  */
22630       want_semicolon = false;
22631
22632     switch (token->type)
22633       {
22634       case CPP_NAME:
22635       case CPP_SEMICOLON:
22636       case CPP_MULT:
22637       case CPP_AND:
22638       case CPP_OPEN_PAREN:
22639       case CPP_CLOSE_PAREN:
22640       case CPP_COMMA:
22641         want_semicolon = false;
22642         break;
22643
22644         /* While it's legal for type qualifiers and storage class
22645            specifiers to follow type definitions in the grammar, only
22646            compiler testsuites contain code like that.  Assume that if
22647            we see such code, then what we're really seeing is a case
22648            like:
22649
22650            class X { }
22651            const <type> var = ...;
22652
22653            or
22654
22655            class Y { }
22656            static <type> func (...) ...
22657
22658            i.e. the qualifier or specifier applies to the next
22659            declaration.  To do so, however, we need to look ahead one
22660            more token to see if *that* token is a type specifier.
22661
22662            This code could be improved to handle:
22663
22664            class Z { }
22665            static const <type> var = ...;  */
22666       case CPP_KEYWORD:
22667         if (keyword_is_decl_specifier (token->keyword))
22668           {
22669             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22670
22671             /* Handling user-defined types here would be nice, but very
22672                tricky.  */
22673             want_semicolon
22674               = (lookahead->type == CPP_KEYWORD
22675                  && keyword_begins_type_specifier (lookahead->keyword));
22676           }
22677         break;
22678       default:
22679         break;
22680       }
22681
22682     /* If we don't have a type, then something is very wrong and we
22683        shouldn't try to do anything clever.  Likewise for not seeing the
22684        closing brace.  */
22685     if (closing_brace && TYPE_P (type) && want_semicolon)
22686       {
22687         /* Locate the closing brace.  */
22688         cp_token_position prev
22689           = cp_lexer_previous_token_position (parser->lexer);
22690         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22691         location_t loc = prev_token->location;
22692
22693         /* We want to suggest insertion of a ';' immediately *after* the
22694            closing brace, so, if we can, offset the location by 1 column.  */
22695         location_t next_loc = loc;
22696         if (!linemap_location_from_macro_expansion_p (line_table, loc))
22697           next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22698
22699         rich_location richloc (line_table, next_loc);
22700
22701         /* If we successfully offset the location, suggest the fix-it.  */
22702         if (next_loc != loc)
22703           richloc.add_fixit_insert_before (next_loc, ";");
22704
22705         if (CLASSTYPE_DECLARED_CLASS (type))
22706           error_at (&richloc,
22707                     "expected %<;%> after class definition");
22708         else if (TREE_CODE (type) == RECORD_TYPE)
22709           error_at (&richloc,
22710                     "expected %<;%> after struct definition");
22711         else if (TREE_CODE (type) == UNION_TYPE)
22712           error_at (&richloc,
22713                     "expected %<;%> after union definition");
22714         else
22715           gcc_unreachable ();
22716
22717         /* Unget one token and smash it to look as though we encountered
22718            a semicolon in the input stream.  */
22719         cp_lexer_set_token_position (parser->lexer, prev);
22720         token = cp_lexer_peek_token (parser->lexer);
22721         token->type = CPP_SEMICOLON;
22722         token->keyword = RID_MAX;
22723       }
22724   }
22725
22726   /* If this class is not itself within the scope of another class,
22727      then we need to parse the bodies of all of the queued function
22728      definitions.  Note that the queued functions defined in a class
22729      are not always processed immediately following the
22730      class-specifier for that class.  Consider:
22731
22732        struct A {
22733          struct B { void f() { sizeof (A); } };
22734        };
22735
22736      If `f' were processed before the processing of `A' were
22737      completed, there would be no way to compute the size of `A'.
22738      Note that the nesting we are interested in here is lexical --
22739      not the semantic nesting given by TYPE_CONTEXT.  In particular,
22740      for:
22741
22742        struct A { struct B; };
22743        struct A::B { void f() { } };
22744
22745      there is no need to delay the parsing of `A::B::f'.  */
22746   if (--parser->num_classes_being_defined == 0)
22747     {
22748       tree decl;
22749       tree class_type = NULL_TREE;
22750       tree pushed_scope = NULL_TREE;
22751       unsigned ix;
22752       cp_default_arg_entry *e;
22753       tree save_ccp, save_ccr;
22754
22755       if (!type_definition_ok_p || any_erroneous_template_args_p (type))
22756         {
22757           /* Skip default arguments, NSDMIs, etc, in order to improve
22758              error recovery (c++/71169, c++/71832).  */
22759           vec_safe_truncate (unparsed_funs_with_default_args, 0);
22760           vec_safe_truncate (unparsed_nsdmis, 0);
22761           vec_safe_truncate (unparsed_classes, 0);
22762           vec_safe_truncate (unparsed_funs_with_definitions, 0);
22763         }
22764
22765       /* In a first pass, parse default arguments to the functions.
22766          Then, in a second pass, parse the bodies of the functions.
22767          This two-phased approach handles cases like:
22768
22769             struct S {
22770               void f() { g(); }
22771               void g(int i = 3);
22772             };
22773
22774          */
22775       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22776         {
22777           decl = e->decl;
22778           /* If there are default arguments that have not yet been processed,
22779              take care of them now.  */
22780           if (class_type != e->class_type)
22781             {
22782               if (pushed_scope)
22783                 pop_scope (pushed_scope);
22784               class_type = e->class_type;
22785               pushed_scope = push_scope (class_type);
22786             }
22787           /* Make sure that any template parameters are in scope.  */
22788           maybe_begin_member_template_processing (decl);
22789           /* Parse the default argument expressions.  */
22790           cp_parser_late_parsing_default_args (parser, decl);
22791           /* Remove any template parameters from the symbol table.  */
22792           maybe_end_member_template_processing ();
22793         }
22794       vec_safe_truncate (unparsed_funs_with_default_args, 0);
22795       /* Now parse any NSDMIs.  */
22796       save_ccp = current_class_ptr;
22797       save_ccr = current_class_ref;
22798       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22799         {
22800           if (class_type != DECL_CONTEXT (decl))
22801             {
22802               if (pushed_scope)
22803                 pop_scope (pushed_scope);
22804               class_type = DECL_CONTEXT (decl);
22805               pushed_scope = push_scope (class_type);
22806             }
22807           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22808           cp_parser_late_parsing_nsdmi (parser, decl);
22809         }
22810       vec_safe_truncate (unparsed_nsdmis, 0);
22811       current_class_ptr = save_ccp;
22812       current_class_ref = save_ccr;
22813       if (pushed_scope)
22814         pop_scope (pushed_scope);
22815
22816       /* Now do some post-NSDMI bookkeeping.  */
22817       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22818         after_nsdmi_defaulted_late_checks (class_type);
22819       vec_safe_truncate (unparsed_classes, 0);
22820       after_nsdmi_defaulted_late_checks (type);
22821
22822       /* Now parse the body of the functions.  */
22823       if (flag_openmp)
22824         {
22825           /* OpenMP UDRs need to be parsed before all other functions.  */
22826           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22827             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22828               cp_parser_late_parsing_for_member (parser, decl);
22829           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22830             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22831               cp_parser_late_parsing_for_member (parser, decl);
22832         }
22833       else
22834         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22835           cp_parser_late_parsing_for_member (parser, decl);
22836       vec_safe_truncate (unparsed_funs_with_definitions, 0);
22837     }
22838   else
22839     vec_safe_push (unparsed_classes, type);
22840
22841   /* Put back any saved access checks.  */
22842   pop_deferring_access_checks ();
22843
22844   /* Restore saved state.  */
22845   parser->in_switch_statement_p = in_switch_statement_p;
22846   parser->in_statement = in_statement;
22847   parser->in_function_body = saved_in_function_body;
22848   parser->num_template_parameter_lists
22849     = saved_num_template_parameter_lists;
22850   parser->in_unbraced_linkage_specification_p
22851     = saved_in_unbraced_linkage_specification_p;
22852
22853   return type;
22854 }
22855
22856 static tree
22857 cp_parser_class_specifier (cp_parser* parser)
22858 {
22859   tree ret;
22860   timevar_push (TV_PARSE_STRUCT);
22861   ret = cp_parser_class_specifier_1 (parser);
22862   timevar_pop (TV_PARSE_STRUCT);
22863   return ret;
22864 }
22865
22866 /* Parse a class-head.
22867
22868    class-head:
22869      class-key identifier [opt] base-clause [opt]
22870      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22871      class-key nested-name-specifier [opt] template-id
22872        base-clause [opt]
22873
22874    class-virt-specifier:
22875      final
22876
22877    GNU Extensions:
22878      class-key attributes identifier [opt] base-clause [opt]
22879      class-key attributes nested-name-specifier identifier base-clause [opt]
22880      class-key attributes nested-name-specifier [opt] template-id
22881        base-clause [opt]
22882
22883    Upon return BASES is initialized to the list of base classes (or
22884    NULL, if there are none) in the same form returned by
22885    cp_parser_base_clause.
22886
22887    Returns the TYPE of the indicated class.  Sets
22888    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22889    involving a nested-name-specifier was used, and FALSE otherwise.
22890
22891    Returns error_mark_node if this is not a class-head.
22892
22893    Returns NULL_TREE if the class-head is syntactically valid, but
22894    semantically invalid in a way that means we should skip the entire
22895    body of the class.  */
22896
22897 static tree
22898 cp_parser_class_head (cp_parser* parser,
22899                       bool* nested_name_specifier_p)
22900 {
22901   tree nested_name_specifier;
22902   enum tag_types class_key;
22903   tree id = NULL_TREE;
22904   tree type = NULL_TREE;
22905   tree attributes;
22906   tree bases;
22907   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22908   bool template_id_p = false;
22909   bool qualified_p = false;
22910   bool invalid_nested_name_p = false;
22911   bool invalid_explicit_specialization_p = false;
22912   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22913   tree pushed_scope = NULL_TREE;
22914   unsigned num_templates;
22915   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22916   /* Assume no nested-name-specifier will be present.  */
22917   *nested_name_specifier_p = false;
22918   /* Assume no template parameter lists will be used in defining the
22919      type.  */
22920   num_templates = 0;
22921   parser->colon_corrects_to_scope_p = false;
22922
22923   /* Look for the class-key.  */
22924   class_key = cp_parser_class_key (parser);
22925   if (class_key == none_type)
22926     return error_mark_node;
22927
22928   location_t class_head_start_location = input_location;
22929
22930   /* Parse the attributes.  */
22931   attributes = cp_parser_attributes_opt (parser);
22932
22933   /* If the next token is `::', that is invalid -- but sometimes
22934      people do try to write:
22935
22936        struct ::S {};
22937
22938      Handle this gracefully by accepting the extra qualifier, and then
22939      issuing an error about it later if this really is a
22940      class-head.  If it turns out just to be an elaborated type
22941      specifier, remain silent.  */
22942   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22943     qualified_p = true;
22944
22945   push_deferring_access_checks (dk_no_check);
22946
22947   /* Determine the name of the class.  Begin by looking for an
22948      optional nested-name-specifier.  */
22949   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22950   nested_name_specifier
22951     = cp_parser_nested_name_specifier_opt (parser,
22952                                            /*typename_keyword_p=*/false,
22953                                            /*check_dependency_p=*/false,
22954                                            /*type_p=*/true,
22955                                            /*is_declaration=*/false);
22956   /* If there was a nested-name-specifier, then there *must* be an
22957      identifier.  */
22958
22959   cp_token *bad_template_keyword = NULL;
22960
22961   if (nested_name_specifier)
22962     {
22963       type_start_token = cp_lexer_peek_token (parser->lexer);
22964       /* Although the grammar says `identifier', it really means
22965          `class-name' or `template-name'.  You are only allowed to
22966          define a class that has already been declared with this
22967          syntax.
22968
22969          The proposed resolution for Core Issue 180 says that wherever
22970          you see `class T::X' you should treat `X' as a type-name.
22971
22972          It is OK to define an inaccessible class; for example:
22973
22974            class A { class B; };
22975            class A::B {};
22976
22977          We do not know if we will see a class-name, or a
22978          template-name.  We look for a class-name first, in case the
22979          class-name is a template-id; if we looked for the
22980          template-name first we would stop after the template-name.  */
22981       cp_parser_parse_tentatively (parser);
22982       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22983         bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22984       type = cp_parser_class_name (parser,
22985                                    /*typename_keyword_p=*/false,
22986                                    /*template_keyword_p=*/false,
22987                                    class_type,
22988                                    /*check_dependency_p=*/false,
22989                                    /*class_head_p=*/true,
22990                                    /*is_declaration=*/false);
22991       /* If that didn't work, ignore the nested-name-specifier.  */
22992       if (!cp_parser_parse_definitely (parser))
22993         {
22994           invalid_nested_name_p = true;
22995           type_start_token = cp_lexer_peek_token (parser->lexer);
22996           id = cp_parser_identifier (parser);
22997           if (id == error_mark_node)
22998             id = NULL_TREE;
22999         }
23000       /* If we could not find a corresponding TYPE, treat this
23001          declaration like an unqualified declaration.  */
23002       if (type == error_mark_node)
23003         nested_name_specifier = NULL_TREE;
23004       /* Otherwise, count the number of templates used in TYPE and its
23005          containing scopes.  */
23006       else
23007         {
23008           tree scope;
23009
23010           for (scope = TREE_TYPE (type);
23011                scope && TREE_CODE (scope) != NAMESPACE_DECL;
23012                scope = get_containing_scope (scope))
23013             if (TYPE_P (scope)
23014                 && CLASS_TYPE_P (scope)
23015                 && CLASSTYPE_TEMPLATE_INFO (scope)
23016                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
23017                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
23018                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
23019               ++num_templates;
23020         }
23021     }
23022   /* Otherwise, the identifier is optional.  */
23023   else
23024     {
23025       /* We don't know whether what comes next is a template-id,
23026          an identifier, or nothing at all.  */
23027       cp_parser_parse_tentatively (parser);
23028       /* Check for a template-id.  */
23029       type_start_token = cp_lexer_peek_token (parser->lexer);
23030       id = cp_parser_template_id (parser,
23031                                   /*template_keyword_p=*/false,
23032                                   /*check_dependency_p=*/true,
23033                                   class_key,
23034                                   /*is_declaration=*/true);
23035       /* If that didn't work, it could still be an identifier.  */
23036       if (!cp_parser_parse_definitely (parser))
23037         {
23038           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23039             {
23040               type_start_token = cp_lexer_peek_token (parser->lexer);
23041               id = cp_parser_identifier (parser);
23042             }
23043           else
23044             id = NULL_TREE;
23045         }
23046       else
23047         {
23048           template_id_p = true;
23049           ++num_templates;
23050         }
23051     }
23052
23053   pop_deferring_access_checks ();
23054
23055   if (id)
23056     {
23057       cp_parser_check_for_invalid_template_id (parser, id,
23058                                                class_key,
23059                                                type_start_token->location);
23060     }
23061   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23062
23063   /* If it's not a `:' or a `{' then we can't really be looking at a
23064      class-head, since a class-head only appears as part of a
23065      class-specifier.  We have to detect this situation before calling
23066      xref_tag, since that has irreversible side-effects.  */
23067   if (!cp_parser_next_token_starts_class_definition_p (parser))
23068     {
23069       cp_parser_error (parser, "expected %<{%> or %<:%>");
23070       type = error_mark_node;
23071       goto out;
23072     }
23073
23074   /* At this point, we're going ahead with the class-specifier, even
23075      if some other problem occurs.  */
23076   cp_parser_commit_to_tentative_parse (parser);
23077   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23078     {
23079       cp_parser_error (parser,
23080                        "cannot specify %<override%> for a class");
23081       type = error_mark_node;
23082       goto out;
23083     }
23084   /* Issue the error about the overly-qualified name now.  */
23085   if (qualified_p)
23086     {
23087       cp_parser_error (parser,
23088                        "global qualification of class name is invalid");
23089       type = error_mark_node;
23090       goto out;
23091     }
23092   else if (invalid_nested_name_p)
23093     {
23094       cp_parser_error (parser,
23095                        "qualified name does not name a class");
23096       type = error_mark_node;
23097       goto out;
23098     }
23099   else if (nested_name_specifier)
23100     {
23101       tree scope;
23102
23103       if (bad_template_keyword)
23104         /* [temp.names]: in a qualified-id formed by a class-head-name, the
23105            keyword template shall not appear at the top level.  */
23106         pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23107                  "keyword %<template%> not allowed in class-head-name");
23108
23109       /* Reject typedef-names in class heads.  */
23110       if (!DECL_IMPLICIT_TYPEDEF_P (type))
23111         {
23112           error_at (type_start_token->location,
23113                     "invalid class name in declaration of %qD",
23114                     type);
23115           type = NULL_TREE;
23116           goto done;
23117         }
23118
23119       /* Figure out in what scope the declaration is being placed.  */
23120       scope = current_scope ();
23121       /* If that scope does not contain the scope in which the
23122          class was originally declared, the program is invalid.  */
23123       if (scope && !is_ancestor (scope, nested_name_specifier))
23124         {
23125           if (at_namespace_scope_p ())
23126             error_at (type_start_token->location,
23127                       "declaration of %qD in namespace %qD which does not "
23128                       "enclose %qD",
23129                       type, scope, nested_name_specifier);
23130           else
23131             error_at (type_start_token->location,
23132                       "declaration of %qD in %qD which does not enclose %qD",
23133                       type, scope, nested_name_specifier);
23134           type = NULL_TREE;
23135           goto done;
23136         }
23137       /* [dcl.meaning]
23138
23139          A declarator-id shall not be qualified except for the
23140          definition of a ... nested class outside of its class
23141          ... [or] the definition or explicit instantiation of a
23142          class member of a namespace outside of its namespace.  */
23143       if (scope == nested_name_specifier)
23144         {
23145           permerror (nested_name_specifier_token_start->location,
23146                      "extra qualification not allowed");
23147           nested_name_specifier = NULL_TREE;
23148           num_templates = 0;
23149         }
23150     }
23151   /* An explicit-specialization must be preceded by "template <>".  If
23152      it is not, try to recover gracefully.  */
23153   if (at_namespace_scope_p ()
23154       && parser->num_template_parameter_lists == 0
23155       && !processing_template_parmlist
23156       && template_id_p)
23157     {
23158       /* Build a location of this form:
23159            struct typename <ARGS>
23160            ^~~~~~~~~~~~~~~~~~~~~~
23161          with caret==start at the start token, and
23162          finishing at the end of the type.  */
23163       location_t reported_loc
23164         = make_location (class_head_start_location,
23165                          class_head_start_location,
23166                          get_finish (type_start_token->location));
23167       rich_location richloc (line_table, reported_loc);
23168       richloc.add_fixit_insert_before (class_head_start_location,
23169                                        "template <> ");
23170       error_at (&richloc,
23171                 "an explicit specialization must be preceded by"
23172                 " %<template <>%>");
23173       invalid_explicit_specialization_p = true;
23174       /* Take the same action that would have been taken by
23175          cp_parser_explicit_specialization.  */
23176       ++parser->num_template_parameter_lists;
23177       begin_specialization ();
23178     }
23179   /* There must be no "return" statements between this point and the
23180      end of this function; set "type "to the correct return value and
23181      use "goto done;" to return.  */
23182   /* Make sure that the right number of template parameters were
23183      present.  */
23184   if (!cp_parser_check_template_parameters (parser, num_templates,
23185                                             template_id_p,
23186                                             type_start_token->location,
23187                                             /*declarator=*/NULL))
23188     {
23189       /* If something went wrong, there is no point in even trying to
23190          process the class-definition.  */
23191       type = NULL_TREE;
23192       goto done;
23193     }
23194
23195   /* Look up the type.  */
23196   if (template_id_p)
23197     {
23198       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23199           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23200               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23201         {
23202           error_at (type_start_token->location,
23203                     "function template %qD redeclared as a class template", id);
23204           type = error_mark_node;
23205         }
23206       else
23207         {
23208           type = TREE_TYPE (id);
23209           type = maybe_process_partial_specialization (type);
23210
23211           /* Check the scope while we still know whether or not we had a
23212              nested-name-specifier.  */
23213           if (type != error_mark_node)
23214             check_unqualified_spec_or_inst (type, type_start_token->location);
23215         }
23216       if (nested_name_specifier)
23217         pushed_scope = push_scope (nested_name_specifier);
23218     }
23219   else if (nested_name_specifier)
23220     {
23221       tree class_type;
23222
23223       /* Given:
23224
23225             template <typename T> struct S { struct T };
23226             template <typename T> struct S<T>::T { };
23227
23228          we will get a TYPENAME_TYPE when processing the definition of
23229          `S::T'.  We need to resolve it to the actual type before we
23230          try to define it.  */
23231       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23232         {
23233           class_type = resolve_typename_type (TREE_TYPE (type),
23234                                               /*only_current_p=*/false);
23235           if (TREE_CODE (class_type) != TYPENAME_TYPE)
23236             type = TYPE_NAME (class_type);
23237           else
23238             {
23239               cp_parser_error (parser, "could not resolve typename type");
23240               type = error_mark_node;
23241             }
23242         }
23243
23244       if (maybe_process_partial_specialization (TREE_TYPE (type))
23245           == error_mark_node)
23246         {
23247           type = NULL_TREE;
23248           goto done;
23249         }
23250
23251       class_type = current_class_type;
23252       /* Enter the scope indicated by the nested-name-specifier.  */
23253       pushed_scope = push_scope (nested_name_specifier);
23254       /* Get the canonical version of this type.  */
23255       type = TYPE_MAIN_DECL (TREE_TYPE (type));
23256       /* Call push_template_decl if it seems like we should be defining a
23257          template either from the template headers or the type we're
23258          defining, so that we diagnose both extra and missing headers.  */
23259       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23260            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23261           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23262         {
23263           type = push_template_decl (type);
23264           if (type == error_mark_node)
23265             {
23266               type = NULL_TREE;
23267               goto done;
23268             }
23269         }
23270
23271       type = TREE_TYPE (type);
23272       *nested_name_specifier_p = true;
23273     }
23274   else      /* The name is not a nested name.  */
23275     {
23276       /* If the class was unnamed, create a dummy name.  */
23277       if (!id)
23278         id = make_anon_name ();
23279       tag_scope tag_scope = (parser->in_type_id_in_expr_p
23280                              ? ts_within_enclosing_non_class
23281                              : ts_current);
23282       type = xref_tag (class_key, id, tag_scope,
23283                        parser->num_template_parameter_lists);
23284     }
23285
23286   /* Indicate whether this class was declared as a `class' or as a
23287      `struct'.  */
23288   if (TREE_CODE (type) == RECORD_TYPE)
23289     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23290   cp_parser_check_class_key (class_key, type);
23291
23292   /* If this type was already complete, and we see another definition,
23293      that's an error.  */
23294   if (type != error_mark_node && COMPLETE_TYPE_P (type))
23295     {
23296       error_at (type_start_token->location, "redefinition of %q#T",
23297                 type);
23298       inform (location_of (type), "previous definition of %q#T",
23299               type);
23300       type = NULL_TREE;
23301       goto done;
23302     }
23303   else if (type == error_mark_node)
23304     type = NULL_TREE;
23305
23306   if (type)
23307     {
23308       /* Apply attributes now, before any use of the class as a template
23309          argument in its base list.  */
23310       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23311       fixup_attribute_variants (type);
23312     }
23313
23314   /* We will have entered the scope containing the class; the names of
23315      base classes should be looked up in that context.  For example:
23316
23317        struct A { struct B {}; struct C; };
23318        struct A::C : B {};
23319
23320      is valid.  */
23321
23322   /* Get the list of base-classes, if there is one.  */
23323   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23324     {
23325       /* PR59482: enter the class scope so that base-specifiers are looked
23326          up correctly.  */
23327       if (type)
23328         pushclass (type);
23329       bases = cp_parser_base_clause (parser);
23330       /* PR59482: get out of the previously pushed class scope so that the
23331          subsequent pops pop the right thing.  */
23332       if (type)
23333         popclass ();
23334     }
23335   else
23336     bases = NULL_TREE;
23337
23338   /* If we're really defining a class, process the base classes.
23339      If they're invalid, fail.  */
23340   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23341     xref_basetypes (type, bases);
23342
23343  done:
23344   /* Leave the scope given by the nested-name-specifier.  We will
23345      enter the class scope itself while processing the members.  */
23346   if (pushed_scope)
23347     pop_scope (pushed_scope);
23348
23349   if (invalid_explicit_specialization_p)
23350     {
23351       end_specialization ();
23352       --parser->num_template_parameter_lists;
23353     }
23354
23355   if (type)
23356     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23357   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23358     CLASSTYPE_FINAL (type) = 1;
23359  out:
23360   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23361   return type;
23362 }
23363
23364 /* Parse a class-key.
23365
23366    class-key:
23367      class
23368      struct
23369      union
23370
23371    Returns the kind of class-key specified, or none_type to indicate
23372    error.  */
23373
23374 static enum tag_types
23375 cp_parser_class_key (cp_parser* parser)
23376 {
23377   cp_token *token;
23378   enum tag_types tag_type;
23379
23380   /* Look for the class-key.  */
23381   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23382   if (!token)
23383     return none_type;
23384
23385   /* Check to see if the TOKEN is a class-key.  */
23386   tag_type = cp_parser_token_is_class_key (token);
23387   if (!tag_type)
23388     cp_parser_error (parser, "expected class-key");
23389   return tag_type;
23390 }
23391
23392 /* Parse a type-parameter-key.
23393
23394    type-parameter-key:
23395      class
23396      typename
23397  */
23398
23399 static void
23400 cp_parser_type_parameter_key (cp_parser* parser)
23401 {
23402   /* Look for the type-parameter-key.  */
23403   enum tag_types tag_type = none_type;
23404   cp_token *token = cp_lexer_peek_token (parser->lexer);
23405   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23406     {
23407       cp_lexer_consume_token (parser->lexer);
23408       if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23409         /* typename is not allowed in a template template parameter
23410            by the standard until C++17.  */
23411         pedwarn (token->location, OPT_Wpedantic, 
23412                  "ISO C++ forbids typename key in template template parameter;"
23413                  " use -std=c++17 or -std=gnu++17");
23414     }
23415   else
23416     cp_parser_error (parser, "expected %<class%> or %<typename%>");
23417
23418   return;
23419 }
23420
23421 /* Parse an (optional) member-specification.
23422
23423    member-specification:
23424      member-declaration member-specification [opt]
23425      access-specifier : member-specification [opt]  */
23426
23427 static void
23428 cp_parser_member_specification_opt (cp_parser* parser)
23429 {
23430   while (true)
23431     {
23432       cp_token *token;
23433       enum rid keyword;
23434
23435       /* Peek at the next token.  */
23436       token = cp_lexer_peek_token (parser->lexer);
23437       /* If it's a `}', or EOF then we've seen all the members.  */
23438       if (token->type == CPP_CLOSE_BRACE
23439           || token->type == CPP_EOF
23440           || token->type == CPP_PRAGMA_EOL)
23441         break;
23442
23443       /* See if this token is a keyword.  */
23444       keyword = token->keyword;
23445       switch (keyword)
23446         {
23447         case RID_PUBLIC:
23448         case RID_PROTECTED:
23449         case RID_PRIVATE:
23450           /* Consume the access-specifier.  */
23451           cp_lexer_consume_token (parser->lexer);
23452           /* Remember which access-specifier is active.  */
23453           current_access_specifier = token->u.value;
23454           /* Look for the `:'.  */
23455           cp_parser_require (parser, CPP_COLON, RT_COLON);
23456           break;
23457
23458         default:
23459           /* Accept #pragmas at class scope.  */
23460           if (token->type == CPP_PRAGMA)
23461             {
23462               cp_parser_pragma (parser, pragma_member, NULL);
23463               break;
23464             }
23465
23466           /* Otherwise, the next construction must be a
23467              member-declaration.  */
23468           cp_parser_member_declaration (parser);
23469         }
23470     }
23471 }
23472
23473 /* Parse a member-declaration.
23474
23475    member-declaration:
23476      decl-specifier-seq [opt] member-declarator-list [opt] ;
23477      function-definition ; [opt]
23478      :: [opt] nested-name-specifier template [opt] unqualified-id ;
23479      using-declaration
23480      template-declaration
23481      alias-declaration
23482
23483    member-declarator-list:
23484      member-declarator
23485      member-declarator-list , member-declarator
23486
23487    member-declarator:
23488      declarator pure-specifier [opt]
23489      declarator constant-initializer [opt]
23490      identifier [opt] : constant-expression
23491
23492    GNU Extensions:
23493
23494    member-declaration:
23495      __extension__ member-declaration
23496
23497    member-declarator:
23498      declarator attributes [opt] pure-specifier [opt]
23499      declarator attributes [opt] constant-initializer [opt]
23500      identifier [opt] attributes [opt] : constant-expression  
23501
23502    C++0x Extensions:
23503
23504    member-declaration:
23505      static_assert-declaration  */
23506
23507 static void
23508 cp_parser_member_declaration (cp_parser* parser)
23509 {
23510   cp_decl_specifier_seq decl_specifiers;
23511   tree prefix_attributes;
23512   tree decl;
23513   int declares_class_or_enum;
23514   bool friend_p;
23515   cp_token *token = NULL;
23516   cp_token *decl_spec_token_start = NULL;
23517   cp_token *initializer_token_start = NULL;
23518   int saved_pedantic;
23519   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23520
23521   /* Check for the `__extension__' keyword.  */
23522   if (cp_parser_extension_opt (parser, &saved_pedantic))
23523     {
23524       /* Recurse.  */
23525       cp_parser_member_declaration (parser);
23526       /* Restore the old value of the PEDANTIC flag.  */
23527       pedantic = saved_pedantic;
23528
23529       return;
23530     }
23531
23532   /* Check for a template-declaration.  */
23533   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23534     {
23535       /* An explicit specialization here is an error condition, and we
23536          expect the specialization handler to detect and report this.  */
23537       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23538           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23539         cp_parser_explicit_specialization (parser);
23540       else
23541         cp_parser_template_declaration (parser, /*member_p=*/true);
23542
23543       return;
23544     }
23545   /* Check for a template introduction.  */
23546   else if (cp_parser_template_declaration_after_export (parser, true))
23547     return;
23548
23549   /* Check for a using-declaration.  */
23550   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23551     {
23552       if (cxx_dialect < cxx11)
23553         {
23554           /* Parse the using-declaration.  */
23555           cp_parser_using_declaration (parser,
23556                                        /*access_declaration_p=*/false);
23557           return;
23558         }
23559       else
23560         {
23561           tree decl;
23562           bool alias_decl_expected;
23563           cp_parser_parse_tentatively (parser);
23564           decl = cp_parser_alias_declaration (parser);
23565           /* Note that if we actually see the '=' token after the
23566              identifier, cp_parser_alias_declaration commits the
23567              tentative parse.  In that case, we really expect an
23568              alias-declaration.  Otherwise, we expect a using
23569              declaration.  */
23570           alias_decl_expected =
23571             !cp_parser_uncommitted_to_tentative_parse_p (parser);
23572           cp_parser_parse_definitely (parser);
23573
23574           if (alias_decl_expected)
23575             finish_member_declaration (decl);
23576           else
23577             cp_parser_using_declaration (parser,
23578                                          /*access_declaration_p=*/false);
23579           return;
23580         }
23581     }
23582
23583   /* Check for @defs.  */
23584   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23585     {
23586       tree ivar, member;
23587       tree ivar_chains = cp_parser_objc_defs_expression (parser);
23588       ivar = ivar_chains;
23589       while (ivar)
23590         {
23591           member = ivar;
23592           ivar = TREE_CHAIN (member);
23593           TREE_CHAIN (member) = NULL_TREE;
23594           finish_member_declaration (member);
23595         }
23596       return;
23597     }
23598
23599   /* If the next token is `static_assert' we have a static assertion.  */
23600   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23601     {
23602       cp_parser_static_assert (parser, /*member_p=*/true);
23603       return;
23604     }
23605
23606   parser->colon_corrects_to_scope_p = false;
23607
23608   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23609       goto out;
23610
23611   /* Parse the decl-specifier-seq.  */
23612   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23613   cp_parser_decl_specifier_seq (parser,
23614                                 CP_PARSER_FLAGS_OPTIONAL,
23615                                 &decl_specifiers,
23616                                 &declares_class_or_enum);
23617   /* Check for an invalid type-name.  */
23618   if (!decl_specifiers.any_type_specifiers_p
23619       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23620     goto out;
23621   /* If there is no declarator, then the decl-specifier-seq should
23622      specify a type.  */
23623   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23624     {
23625       /* If there was no decl-specifier-seq, and the next token is a
23626          `;', then we have something like:
23627
23628            struct S { ; };
23629
23630          [class.mem]
23631
23632          Each member-declaration shall declare at least one member
23633          name of the class.  */
23634       if (!decl_specifiers.any_specifiers_p)
23635         {
23636           cp_token *token = cp_lexer_peek_token (parser->lexer);
23637           if (!in_system_header_at (token->location))
23638             {
23639               gcc_rich_location richloc (token->location);
23640               richloc.add_fixit_remove ();
23641               pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23642             }
23643         }
23644       else
23645         {
23646           tree type;
23647
23648           /* See if this declaration is a friend.  */
23649           friend_p = cp_parser_friend_p (&decl_specifiers);
23650           /* If there were decl-specifiers, check to see if there was
23651              a class-declaration.  */
23652           type = check_tag_decl (&decl_specifiers,
23653                                  /*explicit_type_instantiation_p=*/false);
23654           /* Nested classes have already been added to the class, but
23655              a `friend' needs to be explicitly registered.  */
23656           if (friend_p)
23657             {
23658               /* If the `friend' keyword was present, the friend must
23659                  be introduced with a class-key.  */
23660                if (!declares_class_or_enum && cxx_dialect < cxx11)
23661                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23662                           "in C++03 a class-key must be used "
23663                           "when declaring a friend");
23664                /* In this case:
23665
23666                     template <typename T> struct A {
23667                       friend struct A<T>::B;
23668                     };
23669
23670                   A<T>::B will be represented by a TYPENAME_TYPE, and
23671                   therefore not recognized by check_tag_decl.  */
23672                if (!type)
23673                  {
23674                    type = decl_specifiers.type;
23675                    if (type && TREE_CODE (type) == TYPE_DECL)
23676                      type = TREE_TYPE (type);
23677                  }
23678                if (!type || !TYPE_P (type))
23679                  error_at (decl_spec_token_start->location,
23680                            "friend declaration does not name a class or "
23681                            "function");
23682                else
23683                  make_friend_class (current_class_type, type,
23684                                     /*complain=*/true);
23685             }
23686           /* If there is no TYPE, an error message will already have
23687              been issued.  */
23688           else if (!type || type == error_mark_node)
23689             ;
23690           /* An anonymous aggregate has to be handled specially; such
23691              a declaration really declares a data member (with a
23692              particular type), as opposed to a nested class.  */
23693           else if (ANON_AGGR_TYPE_P (type))
23694             {
23695               /* C++11 9.5/6.  */
23696               if (decl_specifiers.storage_class != sc_none)
23697                 error_at (decl_spec_token_start->location,
23698                           "a storage class on an anonymous aggregate "
23699                           "in class scope is not allowed");
23700
23701               /* Remove constructors and such from TYPE, now that we
23702                  know it is an anonymous aggregate.  */
23703               fixup_anonymous_aggr (type);
23704               /* And make the corresponding data member.  */
23705               decl = build_decl (decl_spec_token_start->location,
23706                                  FIELD_DECL, NULL_TREE, type);
23707               /* Add it to the class.  */
23708               finish_member_declaration (decl);
23709             }
23710           else
23711             cp_parser_check_access_in_redeclaration
23712                                               (TYPE_NAME (type),
23713                                                decl_spec_token_start->location);
23714         }
23715     }
23716   else
23717     {
23718       bool assume_semicolon = false;
23719
23720       /* Clear attributes from the decl_specifiers but keep them
23721          around as prefix attributes that apply them to the entity
23722          being declared.  */
23723       prefix_attributes = decl_specifiers.attributes;
23724       decl_specifiers.attributes = NULL_TREE;
23725
23726       /* See if these declarations will be friends.  */
23727       friend_p = cp_parser_friend_p (&decl_specifiers);
23728
23729       /* Keep going until we hit the `;' at the end of the
23730          declaration.  */
23731       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23732         {
23733           tree attributes = NULL_TREE;
23734           tree first_attribute;
23735           tree initializer;
23736           bool named_bitfld = false;
23737
23738           /* Peek at the next token.  */
23739           token = cp_lexer_peek_token (parser->lexer);
23740
23741           /* The following code wants to know early if it is a bit-field
23742              or some other declaration.  Attributes can appear before
23743              the `:' token.  Skip over them without consuming any tokens
23744              to peek if they are followed by `:'.  */
23745           if (cp_next_tokens_can_be_attribute_p (parser)
23746               || (token->type == CPP_NAME
23747                   && cp_nth_tokens_can_be_attribute_p (parser, 2)
23748                   && (named_bitfld = true)))
23749             {
23750               size_t n
23751                 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23752               token = cp_lexer_peek_nth_token (parser->lexer, n);
23753             }
23754
23755           /* Check for a bitfield declaration.  */
23756           if (token->type == CPP_COLON
23757               || (token->type == CPP_NAME
23758                   && token == cp_lexer_peek_token (parser->lexer)
23759                   && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23760                   && (named_bitfld = true)))
23761             {
23762               tree identifier;
23763               tree width;
23764               tree late_attributes = NULL_TREE;
23765
23766               if (named_bitfld)
23767                 identifier = cp_parser_identifier (parser);
23768               else
23769                 identifier = NULL_TREE;
23770
23771               /* Look for attributes that apply to the bitfield.  */
23772               attributes = cp_parser_attributes_opt (parser);
23773
23774               /* Consume the `:' token.  */
23775               cp_lexer_consume_token (parser->lexer);
23776
23777               /* Get the width of the bitfield.  */
23778               width = cp_parser_constant_expression (parser, false, NULL,
23779                                                      cxx_dialect >= cxx11);
23780
23781               /* In C++2A and as extension for C++11 and above we allow
23782                  default member initializers for bit-fields.  */
23783               initializer = NULL_TREE;
23784               if (cxx_dialect >= cxx11
23785                   && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23786                       || cp_lexer_next_token_is (parser->lexer,
23787                                                  CPP_OPEN_BRACE)))
23788                 {
23789                   location_t loc
23790                     = cp_lexer_peek_token (parser->lexer)->location;
23791                   if (cxx_dialect < cxx2a
23792                       && !in_system_header_at (loc)
23793                       && identifier != NULL_TREE)
23794                     pedwarn (loc, 0,
23795                              "default member initializers for bit-fields "
23796                              "only available with -std=c++2a or "
23797                              "-std=gnu++2a");
23798
23799                   initializer = cp_parser_save_nsdmi (parser);
23800                   if (identifier == NULL_TREE)
23801                     {
23802                       error_at (loc, "default member initializer for "
23803                                      "unnamed bit-field");
23804                       initializer = NULL_TREE;
23805                     }
23806                 }
23807               else
23808                 { 
23809                   /* Look for attributes that apply to the bitfield after
23810                      the `:' token and width.  This is where GCC used to
23811                      parse attributes in the past, pedwarn if there is
23812                      a std attribute.  */
23813                   if (cp_next_tokens_can_be_std_attribute_p (parser))
23814                     pedwarn (input_location, OPT_Wpedantic,
23815                              "ISO C++ allows bit-field attributes only "
23816                              "before the %<:%> token");
23817
23818                   late_attributes = cp_parser_attributes_opt (parser);
23819                 }
23820
23821               attributes = attr_chainon (attributes, late_attributes);
23822
23823               /* Remember which attributes are prefix attributes and
23824                  which are not.  */
23825               first_attribute = attributes;
23826               /* Combine the attributes.  */
23827               attributes = attr_chainon (prefix_attributes, attributes);
23828
23829               /* Create the bitfield declaration.  */
23830               decl = grokbitfield (identifier
23831                                    ? make_id_declarator (NULL_TREE,
23832                                                          identifier,
23833                                                          sfk_none)
23834                                    : NULL,
23835                                    &decl_specifiers,
23836                                    width, initializer,
23837                                    attributes);
23838             }
23839           else
23840             {
23841               cp_declarator *declarator;
23842               tree asm_specification;
23843               int ctor_dtor_or_conv_p;
23844
23845               /* Parse the declarator.  */
23846               declarator
23847                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23848                                         &ctor_dtor_or_conv_p,
23849                                         /*parenthesized_p=*/NULL,
23850                                         /*member_p=*/true,
23851                                         friend_p);
23852
23853               /* If something went wrong parsing the declarator, make sure
23854                  that we at least consume some tokens.  */
23855               if (declarator == cp_error_declarator)
23856                 {
23857                   /* Skip to the end of the statement.  */
23858                   cp_parser_skip_to_end_of_statement (parser);
23859                   /* If the next token is not a semicolon, that is
23860                      probably because we just skipped over the body of
23861                      a function.  So, we consume a semicolon if
23862                      present, but do not issue an error message if it
23863                      is not present.  */
23864                   if (cp_lexer_next_token_is (parser->lexer,
23865                                               CPP_SEMICOLON))
23866                     cp_lexer_consume_token (parser->lexer);
23867                   goto out;
23868                 }
23869
23870               if (declares_class_or_enum & 2)
23871                 cp_parser_check_for_definition_in_return_type
23872                                             (declarator, decl_specifiers.type,
23873                                              decl_specifiers.locations[ds_type_spec]);
23874
23875               /* Look for an asm-specification.  */
23876               asm_specification = cp_parser_asm_specification_opt (parser);
23877               /* Look for attributes that apply to the declaration.  */
23878               attributes = cp_parser_attributes_opt (parser);
23879               /* Remember which attributes are prefix attributes and
23880                  which are not.  */
23881               first_attribute = attributes;
23882               /* Combine the attributes.  */
23883               attributes = attr_chainon (prefix_attributes, attributes);
23884
23885               /* If it's an `=', then we have a constant-initializer or a
23886                  pure-specifier.  It is not correct to parse the
23887                  initializer before registering the member declaration
23888                  since the member declaration should be in scope while
23889                  its initializer is processed.  However, the rest of the
23890                  front end does not yet provide an interface that allows
23891                  us to handle this correctly.  */
23892               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23893                 {
23894                   /* In [class.mem]:
23895
23896                      A pure-specifier shall be used only in the declaration of
23897                      a virtual function.
23898
23899                      A member-declarator can contain a constant-initializer
23900                      only if it declares a static member of integral or
23901                      enumeration type.
23902
23903                      Therefore, if the DECLARATOR is for a function, we look
23904                      for a pure-specifier; otherwise, we look for a
23905                      constant-initializer.  When we call `grokfield', it will
23906                      perform more stringent semantics checks.  */
23907                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
23908                   if (function_declarator_p (declarator)
23909                       || (decl_specifiers.type
23910                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23911                           && declarator->kind == cdk_id
23912                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23913                               == FUNCTION_TYPE)))
23914                     initializer = cp_parser_pure_specifier (parser);
23915                   else if (decl_specifiers.storage_class != sc_static)
23916                     initializer = cp_parser_save_nsdmi (parser);
23917                   else if (cxx_dialect >= cxx11)
23918                     {
23919                       bool nonconst;
23920                       /* Don't require a constant rvalue in C++11, since we
23921                          might want a reference constant.  We'll enforce
23922                          constancy later.  */
23923                       cp_lexer_consume_token (parser->lexer);
23924                       /* Parse the initializer.  */
23925                       initializer = cp_parser_initializer_clause (parser,
23926                                                                   &nonconst);
23927                     }
23928                   else
23929                     /* Parse the initializer.  */
23930                     initializer = cp_parser_constant_initializer (parser);
23931                 }
23932               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23933                        && !function_declarator_p (declarator))
23934                 {
23935                   bool x;
23936                   if (decl_specifiers.storage_class != sc_static)
23937                     initializer = cp_parser_save_nsdmi (parser);
23938                   else
23939                     initializer = cp_parser_initializer (parser, &x, &x);
23940                 }
23941               /* Otherwise, there is no initializer.  */
23942               else
23943                 initializer = NULL_TREE;
23944
23945               /* See if we are probably looking at a function
23946                  definition.  We are certainly not looking at a
23947                  member-declarator.  Calling `grokfield' has
23948                  side-effects, so we must not do it unless we are sure
23949                  that we are looking at a member-declarator.  */
23950               if (cp_parser_token_starts_function_definition_p
23951                   (cp_lexer_peek_token (parser->lexer)))
23952                 {
23953                   /* The grammar does not allow a pure-specifier to be
23954                      used when a member function is defined.  (It is
23955                      possible that this fact is an oversight in the
23956                      standard, since a pure function may be defined
23957                      outside of the class-specifier.  */
23958                   if (initializer && initializer_token_start)
23959                     error_at (initializer_token_start->location,
23960                               "pure-specifier on function-definition");
23961                   decl = cp_parser_save_member_function_body (parser,
23962                                                               &decl_specifiers,
23963                                                               declarator,
23964                                                               attributes);
23965                   if (parser->fully_implicit_function_template_p)
23966                     decl = finish_fully_implicit_template (parser, decl);
23967                   /* If the member was not a friend, declare it here.  */
23968                   if (!friend_p)
23969                     finish_member_declaration (decl);
23970                   /* Peek at the next token.  */
23971                   token = cp_lexer_peek_token (parser->lexer);
23972                   /* If the next token is a semicolon, consume it.  */
23973                   if (token->type == CPP_SEMICOLON)
23974                     {
23975                       location_t semicolon_loc
23976                         = cp_lexer_consume_token (parser->lexer)->location;
23977                       gcc_rich_location richloc (semicolon_loc);
23978                       richloc.add_fixit_remove ();
23979                       warning_at (&richloc, OPT_Wextra_semi,
23980                                   "extra %<;%> after in-class "
23981                                   "function definition");
23982                     }
23983                   goto out;
23984                 }
23985               else
23986                 if (declarator->kind == cdk_function)
23987                   declarator->id_loc = token->location;
23988               /* Create the declaration.  */
23989               decl = grokfield (declarator, &decl_specifiers,
23990                                 initializer, /*init_const_expr_p=*/true,
23991                                 asm_specification, attributes);
23992               if (parser->fully_implicit_function_template_p)
23993                 {
23994                   if (friend_p)
23995                     finish_fully_implicit_template (parser, 0);
23996                   else
23997                     decl = finish_fully_implicit_template (parser, decl);
23998                 }
23999             }
24000
24001           cp_finalize_omp_declare_simd (parser, decl);
24002           cp_finalize_oacc_routine (parser, decl, false);
24003
24004           /* Reset PREFIX_ATTRIBUTES.  */
24005           if (attributes != error_mark_node)
24006             {
24007               while (attributes && TREE_CHAIN (attributes) != first_attribute)
24008                 attributes = TREE_CHAIN (attributes);
24009               if (attributes)
24010                 TREE_CHAIN (attributes) = NULL_TREE;
24011             }
24012
24013           /* If there is any qualification still in effect, clear it
24014              now; we will be starting fresh with the next declarator.  */
24015           parser->scope = NULL_TREE;
24016           parser->qualifying_scope = NULL_TREE;
24017           parser->object_scope = NULL_TREE;
24018           /* If it's a `,', then there are more declarators.  */
24019           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24020             {
24021               cp_lexer_consume_token (parser->lexer);
24022               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24023                 {
24024                   cp_token *token = cp_lexer_previous_token (parser->lexer);
24025                   gcc_rich_location richloc (token->location);
24026                   richloc.add_fixit_remove ();
24027                   error_at (&richloc, "stray %<,%> at end of "
24028                             "member declaration");
24029                 }
24030             }
24031           /* If the next token isn't a `;', then we have a parse error.  */
24032           else if (cp_lexer_next_token_is_not (parser->lexer,
24033                                                CPP_SEMICOLON))
24034             {
24035               /* The next token might be a ways away from where the
24036                  actual semicolon is missing.  Find the previous token
24037                  and use that for our error position.  */
24038               cp_token *token = cp_lexer_previous_token (parser->lexer);
24039               gcc_rich_location richloc (token->location);
24040               richloc.add_fixit_insert_after (";");
24041               error_at (&richloc, "expected %<;%> at end of "
24042                         "member declaration");
24043
24044               /* Assume that the user meant to provide a semicolon.  If
24045                  we were to cp_parser_skip_to_end_of_statement, we might
24046                  skip to a semicolon inside a member function definition
24047                  and issue nonsensical error messages.  */
24048               assume_semicolon = true;
24049             }
24050
24051           if (decl)
24052             {
24053               /* Add DECL to the list of members.  */
24054               if (!friend_p
24055                   /* Explicitly include, eg, NSDMIs, for better error
24056                      recovery (c++/58650).  */
24057                   || !DECL_DECLARES_FUNCTION_P (decl))
24058                 finish_member_declaration (decl);
24059
24060               if (TREE_CODE (decl) == FUNCTION_DECL)
24061                 cp_parser_save_default_args (parser, decl);
24062               else if (TREE_CODE (decl) == FIELD_DECL
24063                        && DECL_INITIAL (decl))
24064                 /* Add DECL to the queue of NSDMI to be parsed later.  */
24065                 vec_safe_push (unparsed_nsdmis, decl);
24066             }
24067
24068           if (assume_semicolon)
24069             goto out;
24070         }
24071     }
24072
24073   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24074  out:
24075   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24076 }
24077
24078 /* Parse a pure-specifier.
24079
24080    pure-specifier:
24081      = 0
24082
24083    Returns INTEGER_ZERO_NODE if a pure specifier is found.
24084    Otherwise, ERROR_MARK_NODE is returned.  */
24085
24086 static tree
24087 cp_parser_pure_specifier (cp_parser* parser)
24088 {
24089   cp_token *token;
24090
24091   /* Look for the `=' token.  */
24092   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24093     return error_mark_node;
24094   /* Look for the `0' token.  */
24095   token = cp_lexer_peek_token (parser->lexer);
24096
24097   if (token->type == CPP_EOF
24098       || token->type == CPP_PRAGMA_EOL)
24099     return error_mark_node;
24100
24101   cp_lexer_consume_token (parser->lexer);
24102
24103   /* Accept = default or = delete in c++0x mode.  */
24104   if (token->keyword == RID_DEFAULT
24105       || token->keyword == RID_DELETE)
24106     {
24107       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24108       return token->u.value;
24109     }
24110
24111   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
24112   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24113     {
24114       cp_parser_error (parser,
24115                        "invalid pure specifier (only %<= 0%> is allowed)");
24116       cp_parser_skip_to_end_of_statement (parser);
24117       return error_mark_node;
24118     }
24119   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24120     {
24121       error_at (token->location, "templates may not be %<virtual%>");
24122       return error_mark_node;
24123     }
24124
24125   return integer_zero_node;
24126 }
24127
24128 /* Parse a constant-initializer.
24129
24130    constant-initializer:
24131      = constant-expression
24132
24133    Returns a representation of the constant-expression.  */
24134
24135 static tree
24136 cp_parser_constant_initializer (cp_parser* parser)
24137 {
24138   /* Look for the `=' token.  */
24139   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24140     return error_mark_node;
24141
24142   /* It is invalid to write:
24143
24144        struct S { static const int i = { 7 }; };
24145
24146      */
24147   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24148     {
24149       cp_parser_error (parser,
24150                        "a brace-enclosed initializer is not allowed here");
24151       /* Consume the opening brace.  */
24152       matching_braces braces;
24153       braces.consume_open (parser);
24154       /* Skip the initializer.  */
24155       cp_parser_skip_to_closing_brace (parser);
24156       /* Look for the trailing `}'.  */
24157       braces.require_close (parser);
24158
24159       return error_mark_node;
24160     }
24161
24162   return cp_parser_constant_expression (parser);
24163 }
24164
24165 /* Derived classes [gram.class.derived] */
24166
24167 /* Parse a base-clause.
24168
24169    base-clause:
24170      : base-specifier-list
24171
24172    base-specifier-list:
24173      base-specifier ... [opt]
24174      base-specifier-list , base-specifier ... [opt]
24175
24176    Returns a TREE_LIST representing the base-classes, in the order in
24177    which they were declared.  The representation of each node is as
24178    described by cp_parser_base_specifier.
24179
24180    In the case that no bases are specified, this function will return
24181    NULL_TREE, not ERROR_MARK_NODE.  */
24182
24183 static tree
24184 cp_parser_base_clause (cp_parser* parser)
24185 {
24186   tree bases = NULL_TREE;
24187
24188   /* Look for the `:' that begins the list.  */
24189   cp_parser_require (parser, CPP_COLON, RT_COLON);
24190
24191   /* Scan the base-specifier-list.  */
24192   while (true)
24193     {
24194       cp_token *token;
24195       tree base;
24196       bool pack_expansion_p = false;
24197
24198       /* Look for the base-specifier.  */
24199       base = cp_parser_base_specifier (parser);
24200       /* Look for the (optional) ellipsis. */
24201       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24202         {
24203           /* Consume the `...'. */
24204           cp_lexer_consume_token (parser->lexer);
24205
24206           pack_expansion_p = true;
24207         }
24208
24209       /* Add BASE to the front of the list.  */
24210       if (base && base != error_mark_node)
24211         {
24212           if (pack_expansion_p)
24213             /* Make this a pack expansion type. */
24214             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24215
24216           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24217             {
24218               TREE_CHAIN (base) = bases;
24219               bases = base;
24220             }
24221         }
24222       /* Peek at the next token.  */
24223       token = cp_lexer_peek_token (parser->lexer);
24224       /* If it's not a comma, then the list is complete.  */
24225       if (token->type != CPP_COMMA)
24226         break;
24227       /* Consume the `,'.  */
24228       cp_lexer_consume_token (parser->lexer);
24229     }
24230
24231   /* PARSER->SCOPE may still be non-NULL at this point, if the last
24232      base class had a qualified name.  However, the next name that
24233      appears is certainly not qualified.  */
24234   parser->scope = NULL_TREE;
24235   parser->qualifying_scope = NULL_TREE;
24236   parser->object_scope = NULL_TREE;
24237
24238   return nreverse (bases);
24239 }
24240
24241 /* Parse a base-specifier.
24242
24243    base-specifier:
24244      :: [opt] nested-name-specifier [opt] class-name
24245      virtual access-specifier [opt] :: [opt] nested-name-specifier
24246        [opt] class-name
24247      access-specifier virtual [opt] :: [opt] nested-name-specifier
24248        [opt] class-name
24249
24250    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
24251    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24252    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
24253    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
24254
24255 static tree
24256 cp_parser_base_specifier (cp_parser* parser)
24257 {
24258   cp_token *token;
24259   bool done = false;
24260   bool virtual_p = false;
24261   bool duplicate_virtual_error_issued_p = false;
24262   bool duplicate_access_error_issued_p = false;
24263   bool class_scope_p, template_p;
24264   tree access = access_default_node;
24265   tree type;
24266
24267   /* Process the optional `virtual' and `access-specifier'.  */
24268   while (!done)
24269     {
24270       /* Peek at the next token.  */
24271       token = cp_lexer_peek_token (parser->lexer);
24272       /* Process `virtual'.  */
24273       switch (token->keyword)
24274         {
24275         case RID_VIRTUAL:
24276           /* If `virtual' appears more than once, issue an error.  */
24277           if (virtual_p && !duplicate_virtual_error_issued_p)
24278             {
24279               cp_parser_error (parser,
24280                                "%<virtual%> specified more than once in base-specifier");
24281               duplicate_virtual_error_issued_p = true;
24282             }
24283
24284           virtual_p = true;
24285
24286           /* Consume the `virtual' token.  */
24287           cp_lexer_consume_token (parser->lexer);
24288
24289           break;
24290
24291         case RID_PUBLIC:
24292         case RID_PROTECTED:
24293         case RID_PRIVATE:
24294           /* If more than one access specifier appears, issue an
24295              error.  */
24296           if (access != access_default_node
24297               && !duplicate_access_error_issued_p)
24298             {
24299               cp_parser_error (parser,
24300                                "more than one access specifier in base-specifier");
24301               duplicate_access_error_issued_p = true;
24302             }
24303
24304           access = ridpointers[(int) token->keyword];
24305
24306           /* Consume the access-specifier.  */
24307           cp_lexer_consume_token (parser->lexer);
24308
24309           break;
24310
24311         default:
24312           done = true;
24313           break;
24314         }
24315     }
24316   /* It is not uncommon to see programs mechanically, erroneously, use
24317      the 'typename' keyword to denote (dependent) qualified types
24318      as base classes.  */
24319   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24320     {
24321       token = cp_lexer_peek_token (parser->lexer);
24322       if (!processing_template_decl)
24323         error_at (token->location,
24324                   "keyword %<typename%> not allowed outside of templates");
24325       else
24326         error_at (token->location,
24327                   "keyword %<typename%> not allowed in this context "
24328                   "(the base class is implicitly a type)");
24329       cp_lexer_consume_token (parser->lexer);
24330     }
24331
24332   /* Look for the optional `::' operator.  */
24333   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24334   /* Look for the nested-name-specifier.  The simplest way to
24335      implement:
24336
24337        [temp.res]
24338
24339        The keyword `typename' is not permitted in a base-specifier or
24340        mem-initializer; in these contexts a qualified name that
24341        depends on a template-parameter is implicitly assumed to be a
24342        type name.
24343
24344      is to pretend that we have seen the `typename' keyword at this
24345      point.  */
24346   cp_parser_nested_name_specifier_opt (parser,
24347                                        /*typename_keyword_p=*/true,
24348                                        /*check_dependency_p=*/true,
24349                                        /*type_p=*/true,
24350                                        /*is_declaration=*/true);
24351   /* If the base class is given by a qualified name, assume that names
24352      we see are type names or templates, as appropriate.  */
24353   class_scope_p = (parser->scope && TYPE_P (parser->scope));
24354   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24355
24356   if (!parser->scope
24357       && cp_lexer_next_token_is_decltype (parser->lexer))
24358     /* DR 950 allows decltype as a base-specifier.  */
24359     type = cp_parser_decltype (parser);
24360   else
24361     {
24362       /* Otherwise, look for the class-name.  */
24363       type = cp_parser_class_name (parser,
24364                                    class_scope_p,
24365                                    template_p,
24366                                    typename_type,
24367                                    /*check_dependency_p=*/true,
24368                                    /*class_head_p=*/false,
24369                                    /*is_declaration=*/true);
24370       type = TREE_TYPE (type);
24371     }
24372
24373   if (type == error_mark_node)
24374     return error_mark_node;
24375
24376   return finish_base_specifier (type, access, virtual_p);
24377 }
24378
24379 /* Exception handling [gram.exception] */
24380
24381 /* Parse an (optional) noexcept-specification.
24382
24383    noexcept-specification:
24384      noexcept ( constant-expression ) [opt]
24385
24386    If no noexcept-specification is present, returns NULL_TREE.
24387    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24388    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24389    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
24390    Otherwise, returns a noexcept specification unless RETURN_COND is true,
24391    in which case a boolean condition is returned instead.  */
24392
24393 static tree
24394 cp_parser_noexcept_specification_opt (cp_parser* parser,
24395                                       bool require_constexpr,
24396                                       bool* consumed_expr,
24397                                       bool return_cond)
24398 {
24399   cp_token *token;
24400   const char *saved_message;
24401
24402   /* Peek at the next token.  */
24403   token = cp_lexer_peek_token (parser->lexer);
24404
24405   /* Is it a noexcept-specification?  */
24406   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24407     {
24408       tree expr;
24409       cp_lexer_consume_token (parser->lexer);
24410
24411       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24412         {
24413           matching_parens parens;
24414           parens.consume_open (parser);
24415
24416           if (require_constexpr)
24417             {
24418               /* Types may not be defined in an exception-specification.  */
24419               saved_message = parser->type_definition_forbidden_message;
24420               parser->type_definition_forbidden_message
24421               = G_("types may not be defined in an exception-specification");
24422
24423               expr = cp_parser_constant_expression (parser);
24424
24425               /* Restore the saved message.  */
24426               parser->type_definition_forbidden_message = saved_message;
24427             }
24428           else
24429             {
24430               expr = cp_parser_expression (parser);
24431               *consumed_expr = true;
24432             }
24433
24434           parens.require_close (parser);
24435         }
24436       else
24437         {
24438           expr = boolean_true_node;
24439           if (!require_constexpr)
24440             *consumed_expr = false;
24441         }
24442
24443       /* We cannot build a noexcept-spec right away because this will check
24444          that expr is a constexpr.  */
24445       if (!return_cond)
24446         return build_noexcept_spec (expr, tf_warning_or_error);
24447       else
24448         return expr;
24449     }
24450   else
24451     return NULL_TREE;
24452 }
24453
24454 /* Parse an (optional) exception-specification.
24455
24456    exception-specification:
24457      throw ( type-id-list [opt] )
24458
24459    Returns a TREE_LIST representing the exception-specification.  The
24460    TREE_VALUE of each node is a type.  */
24461
24462 static tree
24463 cp_parser_exception_specification_opt (cp_parser* parser)
24464 {
24465   cp_token *token;
24466   tree type_id_list;
24467   const char *saved_message;
24468
24469   /* Peek at the next token.  */
24470   token = cp_lexer_peek_token (parser->lexer);
24471
24472   /* Is it a noexcept-specification?  */
24473   type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24474                                                        false);
24475   if (type_id_list != NULL_TREE)
24476     return type_id_list;
24477
24478   /* If it's not `throw', then there's no exception-specification.  */
24479   if (!cp_parser_is_keyword (token, RID_THROW))
24480     return NULL_TREE;
24481
24482   location_t loc = token->location;
24483
24484   /* Consume the `throw'.  */
24485   cp_lexer_consume_token (parser->lexer);
24486
24487   /* Look for the `('.  */
24488   matching_parens parens;
24489   parens.require_open (parser);
24490
24491   /* Peek at the next token.  */
24492   token = cp_lexer_peek_token (parser->lexer);
24493   /* If it's not a `)', then there is a type-id-list.  */
24494   if (token->type != CPP_CLOSE_PAREN)
24495     {
24496       /* Types may not be defined in an exception-specification.  */
24497       saved_message = parser->type_definition_forbidden_message;
24498       parser->type_definition_forbidden_message
24499         = G_("types may not be defined in an exception-specification");
24500       /* Parse the type-id-list.  */
24501       type_id_list = cp_parser_type_id_list (parser);
24502       /* Restore the saved message.  */
24503       parser->type_definition_forbidden_message = saved_message;
24504
24505       if (cxx_dialect >= cxx17)
24506         {
24507           error_at (loc, "ISO C++17 does not allow dynamic exception "
24508                          "specifications");
24509           type_id_list = NULL_TREE;
24510         }
24511       else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24512         warning_at (loc, OPT_Wdeprecated,
24513                     "dynamic exception specifications are deprecated in "
24514                     "C++11");
24515     }
24516   /* In C++17, throw() is equivalent to noexcept (true).  throw()
24517      is deprecated in C++11 and above as well, but is still widely used,
24518      so don't warn about it yet.  */
24519   else if (cxx_dialect >= cxx17)
24520     type_id_list = noexcept_true_spec;
24521   else
24522     type_id_list = empty_except_spec;
24523
24524   /* Look for the `)'.  */
24525   parens.require_close (parser);
24526
24527   return type_id_list;
24528 }
24529
24530 /* Parse an (optional) type-id-list.
24531
24532    type-id-list:
24533      type-id ... [opt]
24534      type-id-list , type-id ... [opt]
24535
24536    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
24537    in the order that the types were presented.  */
24538
24539 static tree
24540 cp_parser_type_id_list (cp_parser* parser)
24541 {
24542   tree types = NULL_TREE;
24543
24544   while (true)
24545     {
24546       cp_token *token;
24547       tree type;
24548
24549       token = cp_lexer_peek_token (parser->lexer);
24550
24551       /* Get the next type-id.  */
24552       type = cp_parser_type_id (parser);
24553       /* Check for invalid 'auto'.  */
24554       if (flag_concepts && type_uses_auto (type))
24555         {
24556           error_at (token->location,
24557                     "invalid use of %<auto%> in exception-specification");
24558           type = error_mark_node;
24559         }
24560       /* Parse the optional ellipsis. */
24561       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24562         {
24563           /* Consume the `...'. */
24564           cp_lexer_consume_token (parser->lexer);
24565
24566           /* Turn the type into a pack expansion expression. */
24567           type = make_pack_expansion (type);
24568         }
24569       /* Add it to the list.  */
24570       types = add_exception_specifier (types, type, /*complain=*/1);
24571       /* Peek at the next token.  */
24572       token = cp_lexer_peek_token (parser->lexer);
24573       /* If it is not a `,', we are done.  */
24574       if (token->type != CPP_COMMA)
24575         break;
24576       /* Consume the `,'.  */
24577       cp_lexer_consume_token (parser->lexer);
24578     }
24579
24580   return nreverse (types);
24581 }
24582
24583 /* Parse a try-block.
24584
24585    try-block:
24586      try compound-statement handler-seq  */
24587
24588 static tree
24589 cp_parser_try_block (cp_parser* parser)
24590 {
24591   tree try_block;
24592
24593   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24594   if (parser->in_function_body
24595       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24596     error ("%<try%> in %<constexpr%> function");
24597
24598   try_block = begin_try_block ();
24599   cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24600   finish_try_block (try_block);
24601   cp_parser_handler_seq (parser);
24602   finish_handler_sequence (try_block);
24603
24604   return try_block;
24605 }
24606
24607 /* Parse a function-try-block.
24608
24609    function-try-block:
24610      try ctor-initializer [opt] function-body handler-seq  */
24611
24612 static void
24613 cp_parser_function_try_block (cp_parser* parser)
24614 {
24615   tree compound_stmt;
24616   tree try_block;
24617
24618   /* Look for the `try' keyword.  */
24619   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24620     return;
24621   /* Let the rest of the front end know where we are.  */
24622   try_block = begin_function_try_block (&compound_stmt);
24623   /* Parse the function-body.  */
24624   cp_parser_ctor_initializer_opt_and_function_body
24625     (parser, /*in_function_try_block=*/true);
24626   /* We're done with the `try' part.  */
24627   finish_function_try_block (try_block);
24628   /* Parse the handlers.  */
24629   cp_parser_handler_seq (parser);
24630   /* We're done with the handlers.  */
24631   finish_function_handler_sequence (try_block, compound_stmt);
24632 }
24633
24634 /* Parse a handler-seq.
24635
24636    handler-seq:
24637      handler handler-seq [opt]  */
24638
24639 static void
24640 cp_parser_handler_seq (cp_parser* parser)
24641 {
24642   while (true)
24643     {
24644       cp_token *token;
24645
24646       /* Parse the handler.  */
24647       cp_parser_handler (parser);
24648       /* Peek at the next token.  */
24649       token = cp_lexer_peek_token (parser->lexer);
24650       /* If it's not `catch' then there are no more handlers.  */
24651       if (!cp_parser_is_keyword (token, RID_CATCH))
24652         break;
24653     }
24654 }
24655
24656 /* Parse a handler.
24657
24658    handler:
24659      catch ( exception-declaration ) compound-statement  */
24660
24661 static void
24662 cp_parser_handler (cp_parser* parser)
24663 {
24664   tree handler;
24665   tree declaration;
24666
24667   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24668   handler = begin_handler ();
24669   matching_parens parens;
24670   parens.require_open (parser);
24671   declaration = cp_parser_exception_declaration (parser);
24672   finish_handler_parms (declaration, handler);
24673   parens.require_close (parser);
24674   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24675   finish_handler (handler);
24676 }
24677
24678 /* Parse an exception-declaration.
24679
24680    exception-declaration:
24681      type-specifier-seq declarator
24682      type-specifier-seq abstract-declarator
24683      type-specifier-seq
24684      ...
24685
24686    Returns a VAR_DECL for the declaration, or NULL_TREE if the
24687    ellipsis variant is used.  */
24688
24689 static tree
24690 cp_parser_exception_declaration (cp_parser* parser)
24691 {
24692   cp_decl_specifier_seq type_specifiers;
24693   cp_declarator *declarator;
24694   const char *saved_message;
24695
24696   /* If it's an ellipsis, it's easy to handle.  */
24697   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24698     {
24699       /* Consume the `...' token.  */
24700       cp_lexer_consume_token (parser->lexer);
24701       return NULL_TREE;
24702     }
24703
24704   /* Types may not be defined in exception-declarations.  */
24705   saved_message = parser->type_definition_forbidden_message;
24706   parser->type_definition_forbidden_message
24707     = G_("types may not be defined in exception-declarations");
24708
24709   /* Parse the type-specifier-seq.  */
24710   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24711                                 /*is_trailing_return=*/false,
24712                                 &type_specifiers);
24713   /* If it's a `)', then there is no declarator.  */
24714   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24715     declarator = NULL;
24716   else
24717     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24718                                        /*ctor_dtor_or_conv_p=*/NULL,
24719                                        /*parenthesized_p=*/NULL,
24720                                        /*member_p=*/false,
24721                                        /*friend_p=*/false);
24722
24723   /* Restore the saved message.  */
24724   parser->type_definition_forbidden_message = saved_message;
24725
24726   if (!type_specifiers.any_specifiers_p)
24727     return error_mark_node;
24728
24729   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24730 }
24731
24732 /* Parse a throw-expression.
24733
24734    throw-expression:
24735      throw assignment-expression [opt]
24736
24737    Returns a THROW_EXPR representing the throw-expression.  */
24738
24739 static tree
24740 cp_parser_throw_expression (cp_parser* parser)
24741 {
24742   tree expression;
24743   cp_token* token;
24744
24745   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24746   token = cp_lexer_peek_token (parser->lexer);
24747   /* Figure out whether or not there is an assignment-expression
24748      following the "throw" keyword.  */
24749   if (token->type == CPP_COMMA
24750       || token->type == CPP_SEMICOLON
24751       || token->type == CPP_CLOSE_PAREN
24752       || token->type == CPP_CLOSE_SQUARE
24753       || token->type == CPP_CLOSE_BRACE
24754       || token->type == CPP_COLON)
24755     expression = NULL_TREE;
24756   else
24757     expression = cp_parser_assignment_expression (parser);
24758
24759   return build_throw (expression);
24760 }
24761
24762 /* GNU Extensions */
24763
24764 /* Parse an (optional) asm-specification.
24765
24766    asm-specification:
24767      asm ( string-literal )
24768
24769    If the asm-specification is present, returns a STRING_CST
24770    corresponding to the string-literal.  Otherwise, returns
24771    NULL_TREE.  */
24772
24773 static tree
24774 cp_parser_asm_specification_opt (cp_parser* parser)
24775 {
24776   cp_token *token;
24777   tree asm_specification;
24778
24779   /* Peek at the next token.  */
24780   token = cp_lexer_peek_token (parser->lexer);
24781   /* If the next token isn't the `asm' keyword, then there's no
24782      asm-specification.  */
24783   if (!cp_parser_is_keyword (token, RID_ASM))
24784     return NULL_TREE;
24785
24786   /* Consume the `asm' token.  */
24787   cp_lexer_consume_token (parser->lexer);
24788   /* Look for the `('.  */
24789   matching_parens parens;
24790   parens.require_open (parser);
24791
24792   /* Look for the string-literal.  */
24793   asm_specification = cp_parser_string_literal (parser, false, false);
24794
24795   /* Look for the `)'.  */
24796   parens.require_close (parser);
24797
24798   return asm_specification;
24799 }
24800
24801 /* Parse an asm-operand-list.
24802
24803    asm-operand-list:
24804      asm-operand
24805      asm-operand-list , asm-operand
24806
24807    asm-operand:
24808      string-literal ( expression )
24809      [ string-literal ] string-literal ( expression )
24810
24811    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
24812    each node is the expression.  The TREE_PURPOSE is itself a
24813    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24814    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24815    is a STRING_CST for the string literal before the parenthesis. Returns
24816    ERROR_MARK_NODE if any of the operands are invalid.  */
24817
24818 static tree
24819 cp_parser_asm_operand_list (cp_parser* parser)
24820 {
24821   tree asm_operands = NULL_TREE;
24822   bool invalid_operands = false;
24823
24824   while (true)
24825     {
24826       tree string_literal;
24827       tree expression;
24828       tree name;
24829
24830       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24831         {
24832           /* Consume the `[' token.  */
24833           cp_lexer_consume_token (parser->lexer);
24834           /* Read the operand name.  */
24835           name = cp_parser_identifier (parser);
24836           if (name != error_mark_node)
24837             name = build_string (IDENTIFIER_LENGTH (name),
24838                                  IDENTIFIER_POINTER (name));
24839           /* Look for the closing `]'.  */
24840           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24841         }
24842       else
24843         name = NULL_TREE;
24844       /* Look for the string-literal.  */
24845       string_literal = cp_parser_string_literal (parser, false, false);
24846
24847       /* Look for the `('.  */
24848       matching_parens parens;
24849       parens.require_open (parser);
24850       /* Parse the expression.  */
24851       expression = cp_parser_expression (parser);
24852       /* Look for the `)'.  */
24853       parens.require_close (parser);
24854
24855       if (name == error_mark_node 
24856           || string_literal == error_mark_node 
24857           || expression == error_mark_node)
24858         invalid_operands = true;
24859
24860       /* Add this operand to the list.  */
24861       asm_operands = tree_cons (build_tree_list (name, string_literal),
24862                                 expression,
24863                                 asm_operands);
24864       /* If the next token is not a `,', there are no more
24865          operands.  */
24866       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24867         break;
24868       /* Consume the `,'.  */
24869       cp_lexer_consume_token (parser->lexer);
24870     }
24871
24872   return invalid_operands ? error_mark_node : nreverse (asm_operands);
24873 }
24874
24875 /* Parse an asm-clobber-list.
24876
24877    asm-clobber-list:
24878      string-literal
24879      asm-clobber-list , string-literal
24880
24881    Returns a TREE_LIST, indicating the clobbers in the order that they
24882    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
24883
24884 static tree
24885 cp_parser_asm_clobber_list (cp_parser* parser)
24886 {
24887   tree clobbers = NULL_TREE;
24888
24889   while (true)
24890     {
24891       tree string_literal;
24892
24893       /* Look for the string literal.  */
24894       string_literal = cp_parser_string_literal (parser, false, false);
24895       /* Add it to the list.  */
24896       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24897       /* If the next token is not a `,', then the list is
24898          complete.  */
24899       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24900         break;
24901       /* Consume the `,' token.  */
24902       cp_lexer_consume_token (parser->lexer);
24903     }
24904
24905   return clobbers;
24906 }
24907
24908 /* Parse an asm-label-list.
24909
24910    asm-label-list:
24911      identifier
24912      asm-label-list , identifier
24913
24914    Returns a TREE_LIST, indicating the labels in the order that they
24915    appeared.  The TREE_VALUE of each node is a label.  */
24916
24917 static tree
24918 cp_parser_asm_label_list (cp_parser* parser)
24919 {
24920   tree labels = NULL_TREE;
24921
24922   while (true)
24923     {
24924       tree identifier, label, name;
24925
24926       /* Look for the identifier.  */
24927       identifier = cp_parser_identifier (parser);
24928       if (!error_operand_p (identifier))
24929         {
24930           label = lookup_label (identifier);
24931           if (TREE_CODE (label) == LABEL_DECL)
24932             {
24933               TREE_USED (label) = 1;
24934               check_goto (label);
24935               name = build_string (IDENTIFIER_LENGTH (identifier),
24936                                    IDENTIFIER_POINTER (identifier));
24937               labels = tree_cons (name, label, labels);
24938             }
24939         }
24940       /* If the next token is not a `,', then the list is
24941          complete.  */
24942       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24943         break;
24944       /* Consume the `,' token.  */
24945       cp_lexer_consume_token (parser->lexer);
24946     }
24947
24948   return nreverse (labels);
24949 }
24950
24951 /* Return TRUE iff the next tokens in the stream are possibly the
24952    beginning of a GNU extension attribute. */
24953
24954 static bool
24955 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24956 {
24957   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24958 }
24959
24960 /* Return TRUE iff the next tokens in the stream are possibly the
24961    beginning of a standard C++-11 attribute specifier.  */
24962
24963 static bool
24964 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24965 {
24966   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24967 }
24968
24969 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24970    beginning of a standard C++-11 attribute specifier.  */
24971
24972 static bool
24973 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24974 {
24975   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24976
24977   return (cxx_dialect >= cxx11
24978           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24979               || (token->type == CPP_OPEN_SQUARE
24980                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24981                   && token->type == CPP_OPEN_SQUARE)));
24982 }
24983
24984 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24985    beginning of a GNU extension attribute.  */
24986
24987 static bool
24988 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24989 {
24990   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24991
24992   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24993 }
24994
24995 /* Return true iff the next tokens can be the beginning of either a
24996    GNU attribute list, or a standard C++11 attribute sequence.  */
24997
24998 static bool
24999 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25000 {
25001   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25002           || cp_next_tokens_can_be_std_attribute_p (parser));
25003 }
25004
25005 /* Return true iff the next Nth tokens can be the beginning of either
25006    a GNU attribute list, or a standard C++11 attribute sequence.  */
25007
25008 static bool
25009 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25010 {
25011   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25012           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25013 }
25014
25015 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25016    of GNU attributes, or return NULL.  */
25017
25018 static tree
25019 cp_parser_attributes_opt (cp_parser *parser)
25020 {
25021   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25022     return cp_parser_gnu_attributes_opt (parser);
25023   return cp_parser_std_attribute_spec_seq (parser);
25024 }
25025
25026 /* Parse an (optional) series of attributes.
25027
25028    attributes:
25029      attributes attribute
25030
25031    attribute:
25032      __attribute__ (( attribute-list [opt] ))
25033
25034    The return value is as for cp_parser_gnu_attribute_list.  */
25035
25036 static tree
25037 cp_parser_gnu_attributes_opt (cp_parser* parser)
25038 {
25039   tree attributes = NULL_TREE;
25040
25041   temp_override<bool> cleanup
25042     (parser->auto_is_implicit_function_template_parm_p, false);
25043
25044   while (true)
25045     {
25046       cp_token *token;
25047       tree attribute_list;
25048       bool ok = true;
25049
25050       /* Peek at the next token.  */
25051       token = cp_lexer_peek_token (parser->lexer);
25052       /* If it's not `__attribute__', then we're done.  */
25053       if (token->keyword != RID_ATTRIBUTE)
25054         break;
25055
25056       /* Consume the `__attribute__' keyword.  */
25057       cp_lexer_consume_token (parser->lexer);
25058       /* Look for the two `(' tokens.  */
25059       matching_parens outer_parens;
25060       outer_parens.require_open (parser);
25061       matching_parens inner_parens;
25062       inner_parens.require_open (parser);
25063
25064       /* Peek at the next token.  */
25065       token = cp_lexer_peek_token (parser->lexer);
25066       if (token->type != CPP_CLOSE_PAREN)
25067         /* Parse the attribute-list.  */
25068         attribute_list = cp_parser_gnu_attribute_list (parser);
25069       else
25070         /* If the next token is a `)', then there is no attribute
25071            list.  */
25072         attribute_list = NULL;
25073
25074       /* Look for the two `)' tokens.  */
25075       if (!inner_parens.require_close (parser))
25076         ok = false;
25077       if (!outer_parens.require_close (parser))
25078         ok = false;
25079       if (!ok)
25080         cp_parser_skip_to_end_of_statement (parser);
25081
25082       /* Add these new attributes to the list.  */
25083       attributes = attr_chainon (attributes, attribute_list);
25084     }
25085
25086   return attributes;
25087 }
25088
25089 /* Parse a GNU attribute-list.
25090
25091    attribute-list:
25092      attribute
25093      attribute-list , attribute
25094
25095    attribute:
25096      identifier
25097      identifier ( identifier )
25098      identifier ( identifier , expression-list )
25099      identifier ( expression-list )
25100
25101    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
25102    to an attribute.  The TREE_PURPOSE of each node is the identifier
25103    indicating which attribute is in use.  The TREE_VALUE represents
25104    the arguments, if any.  */
25105
25106 static tree
25107 cp_parser_gnu_attribute_list (cp_parser* parser)
25108 {
25109   tree attribute_list = NULL_TREE;
25110   bool save_translate_strings_p = parser->translate_strings_p;
25111
25112   parser->translate_strings_p = false;
25113   while (true)
25114     {
25115       cp_token *token;
25116       tree identifier;
25117       tree attribute;
25118
25119       /* Look for the identifier.  We also allow keywords here; for
25120          example `__attribute__ ((const))' is legal.  */
25121       token = cp_lexer_peek_token (parser->lexer);
25122       if (token->type == CPP_NAME
25123           || token->type == CPP_KEYWORD)
25124         {
25125           tree arguments = NULL_TREE;
25126
25127           /* Consume the token, but save it since we need it for the
25128              SIMD enabled function parsing.  */
25129           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25130
25131           /* Save away the identifier that indicates which attribute
25132              this is.  */
25133           identifier = (token->type == CPP_KEYWORD) 
25134             /* For keywords, use the canonical spelling, not the
25135                parsed identifier.  */
25136             ? ridpointers[(int) token->keyword]
25137             : id_token->u.value;
25138
25139           identifier = canonicalize_attr_name (identifier);
25140           attribute = build_tree_list (identifier, NULL_TREE);
25141
25142           /* Peek at the next token.  */
25143           token = cp_lexer_peek_token (parser->lexer);
25144           /* If it's an `(', then parse the attribute arguments.  */
25145           if (token->type == CPP_OPEN_PAREN)
25146             {
25147               vec<tree, va_gc> *vec;
25148               int attr_flag = (attribute_takes_identifier_p (identifier)
25149                                ? id_attr : normal_attr);
25150               vec = cp_parser_parenthesized_expression_list 
25151                     (parser, attr_flag, /*cast_p=*/false, 
25152                     /*allow_expansion_p=*/false, 
25153                     /*non_constant_p=*/NULL);
25154               if (vec == NULL)
25155                 arguments = error_mark_node;
25156               else
25157                 {
25158                   arguments = build_tree_list_vec (vec);
25159                   release_tree_vector (vec);
25160                 }
25161               /* Save the arguments away.  */
25162               TREE_VALUE (attribute) = arguments;
25163             }
25164
25165           if (arguments != error_mark_node)
25166             {
25167               /* Add this attribute to the list.  */
25168               TREE_CHAIN (attribute) = attribute_list;
25169               attribute_list = attribute;
25170             }
25171
25172           token = cp_lexer_peek_token (parser->lexer);
25173         }
25174       /* Now, look for more attributes.  If the next token isn't a
25175          `,', we're done.  */
25176       if (token->type != CPP_COMMA)
25177         break;
25178
25179       /* Consume the comma and keep going.  */
25180       cp_lexer_consume_token (parser->lexer);
25181     }
25182   parser->translate_strings_p = save_translate_strings_p;
25183
25184   /* We built up the list in reverse order.  */
25185   return nreverse (attribute_list);
25186 }
25187
25188 /*  Parse a standard C++11 attribute.
25189
25190     The returned representation is a TREE_LIST which TREE_PURPOSE is
25191     the scoped name of the attribute, and the TREE_VALUE is its
25192     arguments list.
25193
25194     Note that the scoped name of the attribute is itself a TREE_LIST
25195     which TREE_PURPOSE is the namespace of the attribute, and
25196     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
25197     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25198     and which TREE_PURPOSE is directly the attribute name.
25199
25200     Clients of the attribute code should use get_attribute_namespace
25201     and get_attribute_name to get the actual namespace and name of
25202     attributes, regardless of their being GNU or C++11 attributes.
25203
25204     attribute:
25205       attribute-token attribute-argument-clause [opt]
25206
25207     attribute-token:
25208       identifier
25209       attribute-scoped-token
25210
25211     attribute-scoped-token:
25212       attribute-namespace :: identifier
25213
25214     attribute-namespace:
25215       identifier
25216
25217     attribute-argument-clause:
25218       ( balanced-token-seq )
25219
25220     balanced-token-seq:
25221       balanced-token [opt]
25222       balanced-token-seq balanced-token
25223
25224     balanced-token:
25225       ( balanced-token-seq )
25226       [ balanced-token-seq ]
25227       { balanced-token-seq }.  */
25228
25229 static tree
25230 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25231 {
25232   tree attribute, attr_id = NULL_TREE, arguments;
25233   cp_token *token;
25234
25235   temp_override<bool> cleanup
25236     (parser->auto_is_implicit_function_template_parm_p, false);
25237
25238   /* First, parse name of the attribute, a.k.a attribute-token.  */
25239
25240   token = cp_lexer_peek_token (parser->lexer);
25241   if (token->type == CPP_NAME)
25242     attr_id = token->u.value;
25243   else if (token->type == CPP_KEYWORD)
25244     attr_id = ridpointers[(int) token->keyword];
25245   else if (token->flags & NAMED_OP)
25246     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25247
25248   if (attr_id == NULL_TREE)
25249     return NULL_TREE;
25250
25251   cp_lexer_consume_token (parser->lexer);
25252
25253   token = cp_lexer_peek_token (parser->lexer);
25254   if (token->type == CPP_SCOPE)
25255     {
25256       /* We are seeing a scoped attribute token.  */
25257
25258       cp_lexer_consume_token (parser->lexer);
25259       if (attr_ns)
25260         error_at (token->location, "attribute using prefix used together "
25261                                    "with scoped attribute token");
25262       attr_ns = attr_id;
25263
25264       token = cp_lexer_consume_token (parser->lexer);
25265       if (token->type == CPP_NAME)
25266         attr_id = token->u.value;
25267       else if (token->type == CPP_KEYWORD)
25268         attr_id = ridpointers[(int) token->keyword];
25269       else if (token->flags & NAMED_OP)
25270         attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25271       else
25272         {
25273           error_at (token->location,
25274                     "expected an identifier for the attribute name");
25275           return error_mark_node;
25276         }
25277
25278       attr_ns = canonicalize_attr_name (attr_ns);
25279       attr_id = canonicalize_attr_name (attr_id);
25280       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25281                                    NULL_TREE);
25282       token = cp_lexer_peek_token (parser->lexer);
25283     }
25284   else if (attr_ns)
25285     {
25286       attr_ns = canonicalize_attr_name (attr_ns);
25287       attr_id = canonicalize_attr_name (attr_id);
25288       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25289                                    NULL_TREE);
25290     }
25291   else
25292     {
25293       attr_id = canonicalize_attr_name (attr_id);
25294       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25295                                    NULL_TREE);
25296       /* C++11 noreturn attribute is equivalent to GNU's.  */
25297       if (is_attribute_p ("noreturn", attr_id))
25298         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25299       /* C++14 deprecated attribute is equivalent to GNU's.  */
25300       else if (is_attribute_p ("deprecated", attr_id))
25301         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25302       /* C++17 fallthrough attribute is equivalent to GNU's.  */
25303       else if (is_attribute_p ("fallthrough", attr_id))
25304         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25305       /* Transactional Memory TS optimize_for_synchronized attribute is
25306          equivalent to GNU transaction_callable.  */
25307       else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25308         TREE_PURPOSE (attribute)
25309           = get_identifier ("transaction_callable");
25310       /* Transactional Memory attributes are GNU attributes.  */
25311       else if (tm_attr_to_mask (attr_id))
25312         TREE_PURPOSE (attribute) = attr_id;
25313     }
25314
25315   /* Now parse the optional argument clause of the attribute.  */
25316
25317   if (token->type != CPP_OPEN_PAREN)
25318     return attribute;
25319
25320   {
25321     vec<tree, va_gc> *vec;
25322     int attr_flag = normal_attr;
25323
25324     if (attr_ns == get_identifier ("gnu")
25325         && attribute_takes_identifier_p (attr_id))
25326       /* A GNU attribute that takes an identifier in parameter.  */
25327       attr_flag = id_attr;
25328
25329     vec = cp_parser_parenthesized_expression_list
25330       (parser, attr_flag, /*cast_p=*/false,
25331        /*allow_expansion_p=*/true,
25332        /*non_constant_p=*/NULL);
25333     if (vec == NULL)
25334       arguments = error_mark_node;
25335     else
25336       {
25337         arguments = build_tree_list_vec (vec);
25338         release_tree_vector (vec);
25339       }
25340
25341     if (arguments == error_mark_node)
25342       attribute = error_mark_node;
25343     else
25344       TREE_VALUE (attribute) = arguments;
25345   }
25346
25347   return attribute;
25348 }
25349
25350 /* Check that the attribute ATTRIBUTE appears at most once in the
25351    attribute-list ATTRIBUTES.  This is enforced for noreturn (7.6.3)
25352    and deprecated (7.6.5).  Note that carries_dependency (7.6.4)
25353    isn't implemented yet in GCC.  */
25354
25355 static void
25356 cp_parser_check_std_attribute (tree attributes, tree attribute)
25357 {
25358   if (attributes)
25359     {
25360       tree name = get_attribute_name (attribute);
25361       if (is_attribute_p ("noreturn", name)
25362           && lookup_attribute ("noreturn", attributes))
25363         error ("attribute %<noreturn%> can appear at most once "
25364                "in an attribute-list");
25365       else if (is_attribute_p ("deprecated", name)
25366                && lookup_attribute ("deprecated", attributes))
25367         error ("attribute %<deprecated%> can appear at most once "
25368                "in an attribute-list");
25369     }
25370 }
25371
25372 /* Parse a list of standard C++-11 attributes.
25373
25374    attribute-list:
25375      attribute [opt]
25376      attribute-list , attribute[opt]
25377      attribute ...
25378      attribute-list , attribute ...
25379 */
25380
25381 static tree
25382 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25383 {
25384   tree attributes = NULL_TREE, attribute = NULL_TREE;
25385   cp_token *token = NULL;
25386
25387   while (true)
25388     {
25389       attribute = cp_parser_std_attribute (parser, attr_ns);
25390       if (attribute == error_mark_node)
25391         break;
25392       if (attribute != NULL_TREE)
25393         {
25394           cp_parser_check_std_attribute (attributes, attribute);
25395           TREE_CHAIN (attribute) = attributes;
25396           attributes = attribute;
25397         }
25398       token = cp_lexer_peek_token (parser->lexer);
25399       if (token->type == CPP_ELLIPSIS)
25400         {
25401           cp_lexer_consume_token (parser->lexer);
25402           if (attribute == NULL_TREE)
25403             error_at (token->location,
25404                       "expected attribute before %<...%>");
25405           else
25406             {
25407               tree pack = make_pack_expansion (TREE_VALUE (attribute));
25408               if (pack == error_mark_node)
25409                 return error_mark_node;
25410               TREE_VALUE (attribute) = pack;
25411             }
25412           token = cp_lexer_peek_token (parser->lexer);
25413         }
25414       if (token->type != CPP_COMMA)
25415         break;
25416       cp_lexer_consume_token (parser->lexer);
25417     }
25418   attributes = nreverse (attributes);
25419   return attributes;
25420 }
25421
25422 /* Parse a standard C++-11 attribute specifier.
25423
25424    attribute-specifier:
25425      [ [ attribute-using-prefix [opt] attribute-list ] ]
25426      alignment-specifier
25427
25428    attribute-using-prefix:
25429      using attribute-namespace :
25430
25431    alignment-specifier:
25432      alignas ( type-id ... [opt] )
25433      alignas ( alignment-expression ... [opt] ).  */
25434
25435 static tree
25436 cp_parser_std_attribute_spec (cp_parser *parser)
25437 {
25438   tree attributes = NULL_TREE;
25439   cp_token *token = cp_lexer_peek_token (parser->lexer);
25440
25441   if (token->type == CPP_OPEN_SQUARE
25442       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25443     {
25444       tree attr_ns = NULL_TREE;
25445
25446       cp_lexer_consume_token (parser->lexer);
25447       cp_lexer_consume_token (parser->lexer);
25448
25449       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25450         {
25451           token = cp_lexer_peek_nth_token (parser->lexer, 2);
25452           if (token->type == CPP_NAME)
25453             attr_ns = token->u.value;
25454           else if (token->type == CPP_KEYWORD)
25455             attr_ns = ridpointers[(int) token->keyword];
25456           else if (token->flags & NAMED_OP)
25457             attr_ns = get_identifier (cpp_type2name (token->type,
25458                                                      token->flags));
25459           if (attr_ns
25460               && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25461             {
25462               if (cxx_dialect < cxx17
25463                   && !in_system_header_at (input_location))
25464                 pedwarn (input_location, 0,
25465                          "attribute using prefix only available "
25466                          "with -std=c++17 or -std=gnu++17");
25467
25468               cp_lexer_consume_token (parser->lexer);
25469               cp_lexer_consume_token (parser->lexer);
25470               cp_lexer_consume_token (parser->lexer);
25471             }
25472           else
25473             attr_ns = NULL_TREE;
25474         }
25475
25476       attributes = cp_parser_std_attribute_list (parser, attr_ns);
25477
25478       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25479           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25480         cp_parser_skip_to_end_of_statement (parser);
25481       else
25482         /* Warn about parsing c++11 attribute in non-c++11 mode, only
25483            when we are sure that we have actually parsed them.  */
25484         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25485     }
25486   else
25487     {
25488       tree alignas_expr;
25489
25490       /* Look for an alignment-specifier.  */
25491
25492       token = cp_lexer_peek_token (parser->lexer);
25493
25494       if (token->type != CPP_KEYWORD
25495           || token->keyword != RID_ALIGNAS)
25496         return NULL_TREE;
25497
25498       cp_lexer_consume_token (parser->lexer);
25499       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25500
25501       matching_parens parens;
25502       if (!parens.require_open (parser))
25503         return error_mark_node;
25504
25505       cp_parser_parse_tentatively (parser);
25506       alignas_expr = cp_parser_type_id (parser);
25507
25508       if (!cp_parser_parse_definitely (parser))
25509         {
25510           alignas_expr = cp_parser_assignment_expression (parser);
25511           if (alignas_expr == error_mark_node)
25512             cp_parser_skip_to_end_of_statement (parser);
25513           if (alignas_expr == NULL_TREE
25514               || alignas_expr == error_mark_node)
25515             return alignas_expr;
25516         }
25517
25518       alignas_expr = cxx_alignas_expr (alignas_expr);
25519       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25520
25521       /* Handle alignas (pack...).  */
25522       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25523         {
25524           cp_lexer_consume_token (parser->lexer);
25525           alignas_expr = make_pack_expansion (alignas_expr);
25526         }
25527
25528       /* Something went wrong, so don't build the attribute.  */
25529       if (alignas_expr == error_mark_node)
25530         return error_mark_node;
25531
25532       if (!parens.require_close (parser))
25533         return error_mark_node;
25534
25535       /* Build the C++-11 representation of an 'aligned'
25536          attribute.  */
25537       attributes =
25538         build_tree_list (build_tree_list (get_identifier ("gnu"),
25539                                           get_identifier ("aligned")),
25540                          alignas_expr);
25541     }
25542
25543   return attributes;
25544 }
25545
25546 /* Parse a standard C++-11 attribute-specifier-seq.
25547
25548    attribute-specifier-seq:
25549      attribute-specifier-seq [opt] attribute-specifier
25550  */
25551
25552 static tree
25553 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25554 {
25555   tree attr_specs = NULL_TREE;
25556   tree attr_last = NULL_TREE;
25557
25558   while (true)
25559     {
25560       tree attr_spec = cp_parser_std_attribute_spec (parser);
25561       if (attr_spec == NULL_TREE)
25562         break;
25563       if (attr_spec == error_mark_node)
25564         return error_mark_node;
25565
25566       if (attr_last)
25567         TREE_CHAIN (attr_last) = attr_spec;
25568       else
25569         attr_specs = attr_last = attr_spec;
25570       attr_last = tree_last (attr_last);
25571     }
25572
25573   return attr_specs;
25574 }
25575
25576 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25577    return index of the first token after balanced-token, or N on failure.  */
25578
25579 static size_t
25580 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25581 {
25582   size_t orig_n = n;
25583   int nparens = 0, nbraces = 0, nsquares = 0;
25584   do
25585     switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25586       {
25587       case CPP_EOF:
25588       case CPP_PRAGMA_EOL:
25589         /* Ran out of tokens.  */
25590         return orig_n;
25591       case CPP_OPEN_PAREN:
25592         ++nparens;
25593         break;
25594       case CPP_OPEN_BRACE:
25595         ++nbraces;
25596         break;
25597       case CPP_OPEN_SQUARE:
25598         ++nsquares;
25599         break;
25600       case CPP_CLOSE_PAREN:
25601         --nparens;
25602         break;
25603       case CPP_CLOSE_BRACE:
25604         --nbraces;
25605         break;
25606       case CPP_CLOSE_SQUARE:
25607         --nsquares;
25608         break;
25609       default:
25610         break;
25611       }
25612   while (nparens || nbraces || nsquares);
25613   return n;
25614 }
25615
25616 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25617    return index of the first token after the GNU attribute tokens, or N on
25618    failure.  */
25619
25620 static size_t
25621 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25622 {
25623   while (true)
25624     {
25625       if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25626           || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25627           || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25628         break;
25629
25630       size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25631       if (n2 == n + 2)
25632         break;
25633       if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25634         break;
25635       n = n2 + 1;
25636     }
25637   return n;
25638 }
25639
25640 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25641    next token), return index of the first token after the standard C++11
25642    attribute tokens, or N on failure.  */
25643
25644 static size_t
25645 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25646 {
25647   while (true)
25648     {
25649       if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25650           && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25651         {
25652           size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25653           if (n2 == n + 1)
25654             break;
25655           if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25656             break;
25657           n = n2 + 1;
25658         }
25659       else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25660                && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25661         {
25662           size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25663           if (n2 == n + 1)
25664             break;
25665           n = n2;
25666         }
25667       else
25668         break;
25669     }
25670   return n;
25671 }
25672
25673 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25674    as the next token), return index of the first token after the attribute
25675    tokens, or N on failure.  */
25676
25677 static size_t
25678 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25679 {
25680   if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25681     return cp_parser_skip_gnu_attributes_opt (parser, n);
25682   return cp_parser_skip_std_attribute_spec_seq (parser, n);
25683 }
25684
25685 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
25686    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
25687    current value of the PEDANTIC flag, regardless of whether or not
25688    the `__extension__' keyword is present.  The caller is responsible
25689    for restoring the value of the PEDANTIC flag.  */
25690
25691 static bool
25692 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25693 {
25694   /* Save the old value of the PEDANTIC flag.  */
25695   *saved_pedantic = pedantic;
25696
25697   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25698     {
25699       /* Consume the `__extension__' token.  */
25700       cp_lexer_consume_token (parser->lexer);
25701       /* We're not being pedantic while the `__extension__' keyword is
25702          in effect.  */
25703       pedantic = 0;
25704
25705       return true;
25706     }
25707
25708   return false;
25709 }
25710
25711 /* Parse a label declaration.
25712
25713    label-declaration:
25714      __label__ label-declarator-seq ;
25715
25716    label-declarator-seq:
25717      identifier , label-declarator-seq
25718      identifier  */
25719
25720 static void
25721 cp_parser_label_declaration (cp_parser* parser)
25722 {
25723   /* Look for the `__label__' keyword.  */
25724   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25725
25726   while (true)
25727     {
25728       tree identifier;
25729
25730       /* Look for an identifier.  */
25731       identifier = cp_parser_identifier (parser);
25732       /* If we failed, stop.  */
25733       if (identifier == error_mark_node)
25734         break;
25735       /* Declare it as a label.  */
25736       finish_label_decl (identifier);
25737       /* If the next token is a `;', stop.  */
25738       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25739         break;
25740       /* Look for the `,' separating the label declarations.  */
25741       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25742     }
25743
25744   /* Look for the final `;'.  */
25745   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25746 }
25747
25748 // -------------------------------------------------------------------------- //
25749 // Requires Clause
25750
25751 // Parse a requires clause.
25752 //
25753 //    requires-clause:
25754 //      'requires' logical-or-expression
25755 //
25756 // The required logical-or-expression must be a constant expression. Note
25757 // that we don't check that the expression is constepxr here. We defer until
25758 // we analyze constraints and then, we only check atomic constraints.
25759 static tree
25760 cp_parser_requires_clause (cp_parser *parser)
25761 {
25762   // Parse the requires clause so that it is not automatically folded.
25763   ++processing_template_decl;
25764   tree expr = cp_parser_binary_expression (parser, false, false,
25765                                            PREC_NOT_OPERATOR, NULL);
25766   if (check_for_bare_parameter_packs (expr))
25767     expr = error_mark_node;
25768   --processing_template_decl;
25769   return expr;
25770 }
25771
25772 // Optionally parse a requires clause:
25773 static tree
25774 cp_parser_requires_clause_opt (cp_parser *parser)
25775 {
25776   cp_token *tok = cp_lexer_peek_token (parser->lexer);
25777   if (tok->keyword != RID_REQUIRES)
25778     {
25779       if (!flag_concepts && tok->type == CPP_NAME
25780           && tok->u.value == ridpointers[RID_REQUIRES])
25781         {
25782           error_at (cp_lexer_peek_token (parser->lexer)->location,
25783                     "%<requires%> only available with -fconcepts");
25784           /* Parse and discard the requires-clause.  */
25785           cp_lexer_consume_token (parser->lexer);
25786           cp_parser_requires_clause (parser);
25787         }
25788       return NULL_TREE;
25789     }
25790   cp_lexer_consume_token (parser->lexer);
25791   return cp_parser_requires_clause (parser);
25792 }
25793
25794
25795 /*---------------------------------------------------------------------------
25796                            Requires expressions
25797 ---------------------------------------------------------------------------*/
25798
25799 /* Parse a requires expression
25800
25801    requirement-expression:
25802        'requires' requirement-parameter-list [opt] requirement-body */
25803 static tree
25804 cp_parser_requires_expression (cp_parser *parser)
25805 {
25806   gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25807   location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25808
25809   /* A requires-expression shall appear only within a concept
25810      definition or a requires-clause.
25811
25812      TODO: Implement this diagnostic correctly. */
25813   if (!processing_template_decl)
25814     {
25815       error_at (loc, "a requires expression cannot appear outside a template");
25816       cp_parser_skip_to_end_of_statement (parser);
25817       return error_mark_node;
25818     }
25819
25820   tree parms, reqs;
25821   {
25822     /* Local parameters are delared as variables within the scope
25823        of the expression.  They are not visible past the end of
25824        the expression.  Expressions within the requires-expression
25825        are unevaluated.  */
25826     struct scope_sentinel
25827     {
25828       scope_sentinel ()
25829       {
25830         ++cp_unevaluated_operand;
25831         begin_scope (sk_block, NULL_TREE);
25832       }
25833
25834       ~scope_sentinel ()
25835       {
25836         pop_bindings_and_leave_scope ();
25837         --cp_unevaluated_operand;
25838       }
25839     } s;
25840
25841     /* Parse the optional parameter list. */
25842     if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25843       {
25844         parms = cp_parser_requirement_parameter_list (parser);
25845         if (parms == error_mark_node)
25846           return error_mark_node;
25847       }
25848     else
25849       parms = NULL_TREE;
25850
25851     /* Parse the requirement body. */
25852     reqs = cp_parser_requirement_body (parser);
25853     if (reqs == error_mark_node)
25854       return error_mark_node;
25855   }
25856
25857   /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25858      the parm chain.  */
25859   grokparms (parms, &parms);
25860   return finish_requires_expr (parms, reqs);
25861 }
25862
25863 /* Parse a parameterized requirement.
25864
25865    requirement-parameter-list:
25866        '(' parameter-declaration-clause ')' */
25867 static tree
25868 cp_parser_requirement_parameter_list (cp_parser *parser)
25869 {
25870   matching_parens parens;
25871   if (!parens.require_open (parser))
25872     return error_mark_node;
25873
25874   tree parms = cp_parser_parameter_declaration_clause (parser);
25875
25876   if (!parens.require_close (parser))
25877     return error_mark_node;
25878
25879   return parms;
25880 }
25881
25882 /* Parse the body of a requirement.
25883
25884    requirement-body:
25885        '{' requirement-list '}' */
25886 static tree
25887 cp_parser_requirement_body (cp_parser *parser)
25888 {
25889   matching_braces braces;
25890   if (!braces.require_open (parser))
25891     return error_mark_node;
25892
25893   tree reqs = cp_parser_requirement_list (parser);
25894
25895   if (!braces.require_close (parser))
25896     return error_mark_node;
25897
25898   return reqs;
25899 }
25900
25901 /* Parse a list of requirements.
25902
25903    requirement-list:
25904        requirement
25905        requirement-list ';' requirement[opt] */
25906 static tree
25907 cp_parser_requirement_list (cp_parser *parser)
25908 {
25909   tree result = NULL_TREE;
25910   while (true)
25911     {
25912       tree req = cp_parser_requirement (parser);
25913       if (req == error_mark_node)
25914         return error_mark_node;
25915
25916       result = tree_cons (NULL_TREE, req, result);
25917
25918       /* If we see a semi-colon, consume it. */
25919       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25920         cp_lexer_consume_token (parser->lexer);
25921
25922       /* Stop processing at the end of the list. */
25923       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25924         break;
25925     }
25926
25927   /* Reverse the order of requirements so they are analyzed in
25928      declaration order. */
25929   return nreverse (result);
25930 }
25931
25932 /* Parse a syntactic requirement or type requirement.
25933
25934      requirement:
25935        simple-requirement
25936        compound-requirement
25937        type-requirement
25938        nested-requirement */
25939 static tree
25940 cp_parser_requirement (cp_parser *parser)
25941 {
25942   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25943     return cp_parser_compound_requirement (parser);
25944   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25945     return cp_parser_type_requirement (parser);
25946   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25947     return cp_parser_nested_requirement (parser);
25948   else
25949     return cp_parser_simple_requirement (parser);
25950 }
25951
25952 /* Parse a simple requirement.
25953
25954      simple-requirement:
25955        expression ';' */
25956 static tree
25957 cp_parser_simple_requirement (cp_parser *parser)
25958 {
25959   tree expr = cp_parser_expression (parser, NULL, false, false);
25960   if (!expr || expr == error_mark_node)
25961     return error_mark_node;
25962
25963   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25964     return error_mark_node;
25965
25966   return finish_simple_requirement (expr);
25967 }
25968
25969 /* Parse a type requirement
25970
25971      type-requirement
25972          nested-name-specifier [opt] required-type-name ';'
25973
25974      required-type-name:
25975          type-name
25976          'template' [opt] simple-template-id  */
25977 static tree
25978 cp_parser_type_requirement (cp_parser *parser)
25979 {
25980   cp_lexer_consume_token (parser->lexer);
25981
25982   // Save the scope before parsing name specifiers.
25983   tree saved_scope = parser->scope;
25984   tree saved_object_scope = parser->object_scope;
25985   tree saved_qualifying_scope = parser->qualifying_scope;
25986   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25987   cp_parser_nested_name_specifier_opt (parser,
25988                                        /*typename_keyword_p=*/true,
25989                                        /*check_dependency_p=*/false,
25990                                        /*type_p=*/true,
25991                                        /*is_declaration=*/false);
25992
25993   tree type;
25994   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25995     {
25996       cp_lexer_consume_token (parser->lexer);
25997       type = cp_parser_template_id (parser,
25998                                     /*template_keyword_p=*/true,
25999                                     /*check_dependency=*/false,
26000                                     /*tag_type=*/none_type,
26001                                     /*is_declaration=*/false);
26002       type = make_typename_type (parser->scope, type, typename_type,
26003                                  /*complain=*/tf_error);
26004     }
26005   else
26006    type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26007
26008   if (TREE_CODE (type) == TYPE_DECL)
26009     type = TREE_TYPE (type);
26010
26011   parser->scope = saved_scope;
26012   parser->object_scope = saved_object_scope;
26013   parser->qualifying_scope = saved_qualifying_scope;
26014
26015   if (type == error_mark_node)
26016     cp_parser_skip_to_end_of_statement (parser);
26017
26018   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26019     return error_mark_node;
26020   if (type == error_mark_node)
26021     return error_mark_node;
26022
26023   return finish_type_requirement (type);
26024 }
26025
26026 /* Parse a compound requirement
26027
26028      compound-requirement:
26029          '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26030 static tree
26031 cp_parser_compound_requirement (cp_parser *parser)
26032 {
26033   /* Parse an expression enclosed in '{ }'s. */
26034   matching_braces braces;
26035   if (!braces.require_open (parser))
26036     return error_mark_node;
26037
26038   tree expr = cp_parser_expression (parser, NULL, false, false);
26039   if (!expr || expr == error_mark_node)
26040     return error_mark_node;
26041
26042   if (!braces.require_close (parser))
26043     return error_mark_node;
26044
26045   /* Parse the optional noexcept. */
26046   bool noexcept_p = false;
26047   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26048     {
26049       cp_lexer_consume_token (parser->lexer);
26050       noexcept_p = true;
26051     }
26052
26053   /* Parse the optional trailing return type. */
26054   tree type = NULL_TREE;
26055   if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26056     {
26057       cp_lexer_consume_token (parser->lexer);
26058       bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26059       parser->in_result_type_constraint_p = true;
26060       type = cp_parser_trailing_type_id (parser);
26061       parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26062       if (type == error_mark_node)
26063         return error_mark_node;
26064     }
26065
26066   return finish_compound_requirement (expr, type, noexcept_p);
26067 }
26068
26069 /* Parse a nested requirement. This is the same as a requires clause.
26070
26071    nested-requirement:
26072      requires-clause */
26073 static tree
26074 cp_parser_nested_requirement (cp_parser *parser)
26075 {
26076   cp_lexer_consume_token (parser->lexer);
26077   tree req = cp_parser_requires_clause (parser);
26078   if (req == error_mark_node)
26079     return error_mark_node;
26080   return finish_nested_requirement (req);
26081 }
26082
26083 /* Support Functions */
26084
26085 /* Return the appropriate prefer_type argument for lookup_name_real based on
26086    tag_type and template_mem_access.  */
26087
26088 static inline int
26089 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26090 {
26091   /* DR 141: When looking in the current enclosing context for a template-name
26092      after -> or ., only consider class templates.  */
26093   if (template_mem_access)
26094     return 2;
26095   switch (tag_type)
26096     {
26097     case none_type:  return 0;  // No preference.
26098     case scope_type: return 1;  // Type or namespace.
26099     default:         return 2;  // Type only.
26100     }
26101 }
26102
26103 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26104    NAME should have one of the representations used for an
26105    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26106    is returned.  If PARSER->SCOPE is a dependent type, then a
26107    SCOPE_REF is returned.
26108
26109    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26110    returned; the name was already resolved when the TEMPLATE_ID_EXPR
26111    was formed.  Abstractly, such entities should not be passed to this
26112    function, because they do not need to be looked up, but it is
26113    simpler to check for this special case here, rather than at the
26114    call-sites.
26115
26116    In cases not explicitly covered above, this function returns a
26117    DECL, OVERLOAD, or baselink representing the result of the lookup.
26118    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26119    is returned.
26120
26121    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26122    (e.g., "struct") that was used.  In that case bindings that do not
26123    refer to types are ignored.
26124
26125    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26126    ignored.
26127
26128    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26129    are ignored.
26130
26131    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26132    types.
26133
26134    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26135    TREE_LIST of candidates if name-lookup results in an ambiguity, and
26136    NULL_TREE otherwise.  */
26137
26138 static cp_expr
26139 cp_parser_lookup_name (cp_parser *parser, tree name,
26140                        enum tag_types tag_type,
26141                        bool is_template,
26142                        bool is_namespace,
26143                        bool check_dependency,
26144                        tree *ambiguous_decls,
26145                        location_t name_location)
26146 {
26147   tree decl;
26148   tree object_type = parser->context->object_type;
26149
26150   /* Assume that the lookup will be unambiguous.  */
26151   if (ambiguous_decls)
26152     *ambiguous_decls = NULL_TREE;
26153
26154   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26155      no longer valid.  Note that if we are parsing tentatively, and
26156      the parse fails, OBJECT_TYPE will be automatically restored.  */
26157   parser->context->object_type = NULL_TREE;
26158
26159   if (name == error_mark_node)
26160     return error_mark_node;
26161
26162   /* A template-id has already been resolved; there is no lookup to
26163      do.  */
26164   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26165     return name;
26166   if (BASELINK_P (name))
26167     {
26168       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26169                   == TEMPLATE_ID_EXPR);
26170       return name;
26171     }
26172
26173   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
26174      it should already have been checked to make sure that the name
26175      used matches the type being destroyed.  */
26176   if (TREE_CODE (name) == BIT_NOT_EXPR)
26177     {
26178       tree type;
26179
26180       /* Figure out to which type this destructor applies.  */
26181       if (parser->scope)
26182         type = parser->scope;
26183       else if (object_type)
26184         type = object_type;
26185       else
26186         type = current_class_type;
26187       /* If that's not a class type, there is no destructor.  */
26188       if (!type || !CLASS_TYPE_P (type))
26189         return error_mark_node;
26190
26191       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26192         lazily_declare_fn (sfk_destructor, type);
26193
26194       if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26195         return dtor;
26196
26197       return error_mark_node;
26198     }
26199
26200   /* By this point, the NAME should be an ordinary identifier.  If
26201      the id-expression was a qualified name, the qualifying scope is
26202      stored in PARSER->SCOPE at this point.  */
26203   gcc_assert (identifier_p (name));
26204
26205   /* Perform the lookup.  */
26206   if (parser->scope)
26207     {
26208       bool dependent_p;
26209
26210       if (parser->scope == error_mark_node)
26211         return error_mark_node;
26212
26213       /* If the SCOPE is dependent, the lookup must be deferred until
26214          the template is instantiated -- unless we are explicitly
26215          looking up names in uninstantiated templates.  Even then, we
26216          cannot look up the name if the scope is not a class type; it
26217          might, for example, be a template type parameter.  */
26218       dependent_p = (TYPE_P (parser->scope)
26219                      && dependent_scope_p (parser->scope));
26220       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26221           && dependent_p)
26222         /* Defer lookup.  */
26223         decl = error_mark_node;
26224       else
26225         {
26226           tree pushed_scope = NULL_TREE;
26227
26228           /* If PARSER->SCOPE is a dependent type, then it must be a
26229              class type, and we must not be checking dependencies;
26230              otherwise, we would have processed this lookup above.  So
26231              that PARSER->SCOPE is not considered a dependent base by
26232              lookup_member, we must enter the scope here.  */
26233           if (dependent_p)
26234             pushed_scope = push_scope (parser->scope);
26235
26236           /* If the PARSER->SCOPE is a template specialization, it
26237              may be instantiated during name lookup.  In that case,
26238              errors may be issued.  Even if we rollback the current
26239              tentative parse, those errors are valid.  */
26240           decl = lookup_qualified_name (parser->scope, name,
26241                                         prefer_type_arg (tag_type),
26242                                         /*complain=*/true);
26243
26244           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26245              lookup result and the nested-name-specifier nominates a class C:
26246                * if the name specified after the nested-name-specifier, when
26247                looked up in C, is the injected-class-name of C (Clause 9), or
26248                * if the name specified after the nested-name-specifier is the
26249                same as the identifier or the simple-template-id's template-
26250                name in the last component of the nested-name-specifier,
26251              the name is instead considered to name the constructor of
26252              class C. [ Note: for example, the constructor is not an
26253              acceptable lookup result in an elaborated-type-specifier so
26254              the constructor would not be used in place of the
26255              injected-class-name. --end note ] Such a constructor name
26256              shall be used only in the declarator-id of a declaration that
26257              names a constructor or in a using-declaration.  */
26258           if (tag_type == none_type
26259               && DECL_SELF_REFERENCE_P (decl)
26260               && same_type_p (DECL_CONTEXT (decl), parser->scope))
26261             decl = lookup_qualified_name (parser->scope, ctor_identifier,
26262                                           prefer_type_arg (tag_type),
26263                                           /*complain=*/true);
26264
26265           /* If we have a single function from a using decl, pull it out.  */
26266           if (TREE_CODE (decl) == OVERLOAD
26267               && !really_overloaded_fn (decl))
26268             decl = OVL_FUNCTION (decl);
26269
26270           if (pushed_scope)
26271             pop_scope (pushed_scope);
26272         }
26273
26274       /* If the scope is a dependent type and either we deferred lookup or
26275          we did lookup but didn't find the name, rememeber the name.  */
26276       if (decl == error_mark_node && TYPE_P (parser->scope)
26277           && dependent_type_p (parser->scope))
26278         {
26279           if (tag_type)
26280             {
26281               tree type;
26282
26283               /* The resolution to Core Issue 180 says that `struct
26284                  A::B' should be considered a type-name, even if `A'
26285                  is dependent.  */
26286               type = make_typename_type (parser->scope, name, tag_type,
26287                                          /*complain=*/tf_error);
26288               if (type != error_mark_node)
26289                 decl = TYPE_NAME (type);
26290             }
26291           else if (is_template
26292                    && (cp_parser_next_token_ends_template_argument_p (parser)
26293                        || cp_lexer_next_token_is (parser->lexer,
26294                                                   CPP_CLOSE_PAREN)))
26295             decl = make_unbound_class_template (parser->scope,
26296                                                 name, NULL_TREE,
26297                                                 /*complain=*/tf_error);
26298           else
26299             decl = build_qualified_name (/*type=*/NULL_TREE,
26300                                          parser->scope, name,
26301                                          is_template);
26302         }
26303       parser->qualifying_scope = parser->scope;
26304       parser->object_scope = NULL_TREE;
26305     }
26306   else if (object_type)
26307     {
26308       /* Look up the name in the scope of the OBJECT_TYPE, unless the
26309          OBJECT_TYPE is not a class.  */
26310       if (CLASS_TYPE_P (object_type))
26311         /* If the OBJECT_TYPE is a template specialization, it may
26312            be instantiated during name lookup.  In that case, errors
26313            may be issued.  Even if we rollback the current tentative
26314            parse, those errors are valid.  */
26315         decl = lookup_member (object_type,
26316                               name,
26317                               /*protect=*/0,
26318                               prefer_type_arg (tag_type),
26319                               tf_warning_or_error);
26320       else
26321         decl = NULL_TREE;
26322
26323       if (!decl)
26324         /* Look it up in the enclosing context.  DR 141: When looking for a
26325            template-name after -> or ., only consider class templates.  */
26326         decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26327                                  /*nonclass=*/0,
26328                                  /*block_p=*/true, is_namespace, 0);
26329       if (object_type == unknown_type_node)
26330         /* The object is type-dependent, so we can't look anything up; we used
26331            this to get the DR 141 behavior.  */
26332         object_type = NULL_TREE;
26333       parser->object_scope = object_type;
26334       parser->qualifying_scope = NULL_TREE;
26335     }
26336   else
26337     {
26338       decl = lookup_name_real (name, prefer_type_arg (tag_type),
26339                                /*nonclass=*/0,
26340                                /*block_p=*/true, is_namespace, 0);
26341       parser->qualifying_scope = NULL_TREE;
26342       parser->object_scope = NULL_TREE;
26343     }
26344
26345   /* If the lookup failed, let our caller know.  */
26346   if (!decl || decl == error_mark_node)
26347     return error_mark_node;
26348
26349   /* Pull out the template from an injected-class-name (or multiple).  */
26350   if (is_template)
26351     decl = maybe_get_template_decl_from_type_decl (decl);
26352
26353   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
26354   if (TREE_CODE (decl) == TREE_LIST)
26355     {
26356       if (ambiguous_decls)
26357         *ambiguous_decls = decl;
26358       /* The error message we have to print is too complicated for
26359          cp_parser_error, so we incorporate its actions directly.  */
26360       if (!cp_parser_simulate_error (parser))
26361         {
26362           error_at (name_location, "reference to %qD is ambiguous",
26363                     name);
26364           print_candidates (decl);
26365         }
26366       return error_mark_node;
26367     }
26368
26369   gcc_assert (DECL_P (decl)
26370               || TREE_CODE (decl) == OVERLOAD
26371               || TREE_CODE (decl) == SCOPE_REF
26372               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26373               || BASELINK_P (decl));
26374
26375   /* If we have resolved the name of a member declaration, check to
26376      see if the declaration is accessible.  When the name resolves to
26377      set of overloaded functions, accessibility is checked when
26378      overload resolution is done.
26379
26380      During an explicit instantiation, access is not checked at all,
26381      as per [temp.explicit].  */
26382   if (DECL_P (decl))
26383     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26384
26385   maybe_record_typedef_use (decl);
26386
26387   return cp_expr (decl, name_location);
26388 }
26389
26390 /* Like cp_parser_lookup_name, but for use in the typical case where
26391    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26392    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
26393
26394 static tree
26395 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26396 {
26397   return cp_parser_lookup_name (parser, name,
26398                                 none_type,
26399                                 /*is_template=*/false,
26400                                 /*is_namespace=*/false,
26401                                 /*check_dependency=*/true,
26402                                 /*ambiguous_decls=*/NULL,
26403                                 location);
26404 }
26405
26406 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26407    the current context, return the TYPE_DECL.  If TAG_NAME_P is
26408    true, the DECL indicates the class being defined in a class-head,
26409    or declared in an elaborated-type-specifier.
26410
26411    Otherwise, return DECL.  */
26412
26413 static tree
26414 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26415 {
26416   /* If the TEMPLATE_DECL is being declared as part of a class-head,
26417      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26418
26419        struct A {
26420          template <typename T> struct B;
26421        };
26422
26423        template <typename T> struct A::B {};
26424
26425      Similarly, in an elaborated-type-specifier:
26426
26427        namespace N { struct X{}; }
26428
26429        struct A {
26430          template <typename T> friend struct N::X;
26431        };
26432
26433      However, if the DECL refers to a class type, and we are in
26434      the scope of the class, then the name lookup automatically
26435      finds the TYPE_DECL created by build_self_reference rather
26436      than a TEMPLATE_DECL.  For example, in:
26437
26438        template <class T> struct S {
26439          S s;
26440        };
26441
26442      there is no need to handle such case.  */
26443
26444   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26445     return DECL_TEMPLATE_RESULT (decl);
26446
26447   return decl;
26448 }
26449
26450 /* If too many, or too few, template-parameter lists apply to the
26451    declarator, issue an error message.  Returns TRUE if all went well,
26452    and FALSE otherwise.  */
26453
26454 static bool
26455 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26456                                                 cp_declarator *declarator,
26457                                                 location_t declarator_location)
26458 {
26459   switch (declarator->kind)
26460     {
26461     case cdk_id:
26462       {
26463         unsigned num_templates = 0;
26464         tree scope = declarator->u.id.qualifying_scope;
26465         bool template_id_p = false;
26466
26467         if (scope)
26468           num_templates = num_template_headers_for_class (scope);
26469         else if (TREE_CODE (declarator->u.id.unqualified_name)
26470                  == TEMPLATE_ID_EXPR)
26471           {
26472             /* If the DECLARATOR has the form `X<y>' then it uses one
26473                additional level of template parameters.  */
26474             ++num_templates;
26475             template_id_p = true;
26476           }
26477
26478         return cp_parser_check_template_parameters 
26479           (parser, num_templates, template_id_p, declarator_location,
26480            declarator);
26481       }
26482
26483     case cdk_function:
26484     case cdk_array:
26485     case cdk_pointer:
26486     case cdk_reference:
26487     case cdk_ptrmem:
26488       return (cp_parser_check_declarator_template_parameters
26489               (parser, declarator->declarator, declarator_location));
26490
26491     case cdk_decomp:
26492     case cdk_error:
26493       return true;
26494
26495     default:
26496       gcc_unreachable ();
26497     }
26498   return false;
26499 }
26500
26501 /* NUM_TEMPLATES were used in the current declaration.  If that is
26502    invalid, return FALSE and issue an error messages.  Otherwise,
26503    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
26504    declarator and we can print more accurate diagnostics.  */
26505
26506 static bool
26507 cp_parser_check_template_parameters (cp_parser* parser,
26508                                      unsigned num_templates,
26509                                      bool template_id_p,
26510                                      location_t location,
26511                                      cp_declarator *declarator)
26512 {
26513   /* If there are the same number of template classes and parameter
26514      lists, that's OK.  */
26515   if (parser->num_template_parameter_lists == num_templates)
26516     return true;
26517   /* If there are more, but only one more, and the name ends in an identifier,
26518      then we are declaring a primary template.  That's OK too.  */
26519   if (!template_id_p
26520       && parser->num_template_parameter_lists == num_templates + 1)
26521     return true;
26522   /* If there are more template classes than parameter lists, we have
26523      something like:
26524
26525        template <class T> void S<T>::R<T>::f ();  */
26526   if (parser->num_template_parameter_lists < num_templates)
26527     {
26528       if (declarator && !current_function_decl)
26529         error_at (location, "specializing member %<%T::%E%> "
26530                   "requires %<template<>%> syntax", 
26531                   declarator->u.id.qualifying_scope,
26532                   declarator->u.id.unqualified_name);
26533       else if (declarator)
26534         error_at (location, "invalid declaration of %<%T::%E%>",
26535                   declarator->u.id.qualifying_scope,
26536                   declarator->u.id.unqualified_name);
26537       else 
26538         error_at (location, "too few template-parameter-lists");
26539       return false;
26540     }
26541   /* Otherwise, there are too many template parameter lists.  We have
26542      something like:
26543
26544      template <class T> template <class U> void S::f();  */
26545   error_at (location, "too many template-parameter-lists");
26546   return false;
26547 }
26548
26549 /* Parse an optional `::' token indicating that the following name is
26550    from the global namespace.  If so, PARSER->SCOPE is set to the
26551    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26552    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26553    Returns the new value of PARSER->SCOPE, if the `::' token is
26554    present, and NULL_TREE otherwise.  */
26555
26556 static tree
26557 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26558 {
26559   cp_token *token;
26560
26561   /* Peek at the next token.  */
26562   token = cp_lexer_peek_token (parser->lexer);
26563   /* If we're looking at a `::' token then we're starting from the
26564      global namespace, not our current location.  */
26565   if (token->type == CPP_SCOPE)
26566     {
26567       /* Consume the `::' token.  */
26568       cp_lexer_consume_token (parser->lexer);
26569       /* Set the SCOPE so that we know where to start the lookup.  */
26570       parser->scope = global_namespace;
26571       parser->qualifying_scope = global_namespace;
26572       parser->object_scope = NULL_TREE;
26573
26574       return parser->scope;
26575     }
26576   else if (!current_scope_valid_p)
26577     {
26578       parser->scope = NULL_TREE;
26579       parser->qualifying_scope = NULL_TREE;
26580       parser->object_scope = NULL_TREE;
26581     }
26582
26583   return NULL_TREE;
26584 }
26585
26586 /* Returns TRUE if the upcoming token sequence is the start of a
26587    constructor declarator or C++17 deduction guide.  If FRIEND_P is true, the
26588    declarator is preceded by the `friend' specifier.  */
26589
26590 static bool
26591 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26592 {
26593   bool constructor_p;
26594   bool outside_class_specifier_p;
26595   tree nested_name_specifier;
26596   cp_token *next_token;
26597
26598   /* The common case is that this is not a constructor declarator, so
26599      try to avoid doing lots of work if at all possible.  It's not
26600      valid declare a constructor at function scope.  */
26601   if (parser->in_function_body)
26602     return false;
26603   /* And only certain tokens can begin a constructor declarator.  */
26604   next_token = cp_lexer_peek_token (parser->lexer);
26605   if (next_token->type != CPP_NAME
26606       && next_token->type != CPP_SCOPE
26607       && next_token->type != CPP_NESTED_NAME_SPECIFIER
26608       && next_token->type != CPP_TEMPLATE_ID)
26609     return false;
26610
26611   /* Parse tentatively; we are going to roll back all of the tokens
26612      consumed here.  */
26613   cp_parser_parse_tentatively (parser);
26614   /* Assume that we are looking at a constructor declarator.  */
26615   constructor_p = true;
26616
26617   /* Look for the optional `::' operator.  */
26618   cp_parser_global_scope_opt (parser,
26619                               /*current_scope_valid_p=*/false);
26620   /* Look for the nested-name-specifier.  */
26621   nested_name_specifier
26622     = (cp_parser_nested_name_specifier_opt (parser,
26623                                             /*typename_keyword_p=*/false,
26624                                             /*check_dependency_p=*/false,
26625                                             /*type_p=*/false,
26626                                             /*is_declaration=*/false));
26627
26628   outside_class_specifier_p = (!at_class_scope_p ()
26629                                || !TYPE_BEING_DEFINED (current_class_type)
26630                                || friend_p);
26631
26632   /* Outside of a class-specifier, there must be a
26633      nested-name-specifier.  Except in C++17 mode, where we
26634      might be declaring a guiding declaration.  */
26635   if (!nested_name_specifier && outside_class_specifier_p
26636       && cxx_dialect < cxx17)
26637     constructor_p = false;
26638   else if (nested_name_specifier == error_mark_node)
26639     constructor_p = false;
26640
26641   /* If we have a class scope, this is easy; DR 147 says that S::S always
26642      names the constructor, and no other qualified name could.  */
26643   if (constructor_p && nested_name_specifier
26644       && CLASS_TYPE_P (nested_name_specifier))
26645     {
26646       tree id = cp_parser_unqualified_id (parser,
26647                                           /*template_keyword_p=*/false,
26648                                           /*check_dependency_p=*/false,
26649                                           /*declarator_p=*/true,
26650                                           /*optional_p=*/false);
26651       if (is_overloaded_fn (id))
26652         id = DECL_NAME (get_first_fn (id));
26653       if (!constructor_name_p (id, nested_name_specifier))
26654         constructor_p = false;
26655     }
26656   /* If we still think that this might be a constructor-declarator,
26657      look for a class-name.  */
26658   else if (constructor_p)
26659     {
26660       /* If we have:
26661
26662            template <typename T> struct S {
26663              S();
26664            };
26665
26666          we must recognize that the nested `S' names a class.  */
26667       if (cxx_dialect >= cxx17)
26668         cp_parser_parse_tentatively (parser);
26669
26670       tree type_decl;
26671       type_decl = cp_parser_class_name (parser,
26672                                         /*typename_keyword_p=*/false,
26673                                         /*template_keyword_p=*/false,
26674                                         none_type,
26675                                         /*check_dependency_p=*/false,
26676                                         /*class_head_p=*/false,
26677                                         /*is_declaration=*/false);
26678
26679       if (cxx_dialect >= cxx17
26680           && !cp_parser_parse_definitely (parser))
26681         {
26682           type_decl = NULL_TREE;
26683           tree tmpl = cp_parser_template_name (parser,
26684                                                /*template_keyword*/false,
26685                                                /*check_dependency_p*/false,
26686                                                /*is_declaration*/false,
26687                                                none_type,
26688                                                /*is_identifier*/NULL);
26689           if (DECL_CLASS_TEMPLATE_P (tmpl)
26690               || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26691             /* It's a deduction guide, return true.  */;
26692           else
26693             cp_parser_simulate_error (parser);
26694         }
26695
26696       /* If there was no class-name, then this is not a constructor.
26697          Otherwise, if we are in a class-specifier and we aren't
26698          handling a friend declaration, check that its type matches
26699          current_class_type (c++/38313).  Note: error_mark_node
26700          is left alone for error recovery purposes.  */
26701       constructor_p = (!cp_parser_error_occurred (parser)
26702                        && (outside_class_specifier_p
26703                            || type_decl == NULL_TREE
26704                            || type_decl == error_mark_node
26705                            || same_type_p (current_class_type,
26706                                            TREE_TYPE (type_decl))));
26707
26708       /* If we're still considering a constructor, we have to see a `(',
26709          to begin the parameter-declaration-clause, followed by either a
26710          `)', an `...', or a decl-specifier.  We need to check for a
26711          type-specifier to avoid being fooled into thinking that:
26712
26713            S (f) (int);
26714
26715          is a constructor.  (It is actually a function named `f' that
26716          takes one parameter (of type `int') and returns a value of type
26717          `S'.  */
26718       if (constructor_p
26719           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26720         constructor_p = false;
26721
26722       if (constructor_p
26723           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26724           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26725           /* A parameter declaration begins with a decl-specifier,
26726              which is either the "attribute" keyword, a storage class
26727              specifier, or (usually) a type-specifier.  */
26728           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26729         {
26730           tree type;
26731           tree pushed_scope = NULL_TREE;
26732           unsigned saved_num_template_parameter_lists;
26733
26734           /* Names appearing in the type-specifier should be looked up
26735              in the scope of the class.  */
26736           if (current_class_type)
26737             type = NULL_TREE;
26738           else if (type_decl)
26739             {
26740               type = TREE_TYPE (type_decl);
26741               if (TREE_CODE (type) == TYPENAME_TYPE)
26742                 {
26743                   type = resolve_typename_type (type,
26744                                                 /*only_current_p=*/false);
26745                   if (TREE_CODE (type) == TYPENAME_TYPE)
26746                     {
26747                       cp_parser_abort_tentative_parse (parser);
26748                       return false;
26749                     }
26750                 }
26751               pushed_scope = push_scope (type);
26752             }
26753
26754           /* Inside the constructor parameter list, surrounding
26755              template-parameter-lists do not apply.  */
26756           saved_num_template_parameter_lists
26757             = parser->num_template_parameter_lists;
26758           parser->num_template_parameter_lists = 0;
26759
26760           /* Look for the type-specifier.  */
26761           cp_parser_type_specifier (parser,
26762                                     CP_PARSER_FLAGS_NONE,
26763                                     /*decl_specs=*/NULL,
26764                                     /*is_declarator=*/true,
26765                                     /*declares_class_or_enum=*/NULL,
26766                                     /*is_cv_qualifier=*/NULL);
26767
26768           parser->num_template_parameter_lists
26769             = saved_num_template_parameter_lists;
26770
26771           /* Leave the scope of the class.  */
26772           if (pushed_scope)
26773             pop_scope (pushed_scope);
26774
26775           constructor_p = !cp_parser_error_occurred (parser);
26776         }
26777     }
26778
26779   /* We did not really want to consume any tokens.  */
26780   cp_parser_abort_tentative_parse (parser);
26781
26782   return constructor_p;
26783 }
26784
26785 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26786    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
26787    they must be performed once we are in the scope of the function.
26788
26789    Returns the function defined.  */
26790
26791 static tree
26792 cp_parser_function_definition_from_specifiers_and_declarator
26793   (cp_parser* parser,
26794    cp_decl_specifier_seq *decl_specifiers,
26795    tree attributes,
26796    const cp_declarator *declarator)
26797 {
26798   tree fn;
26799   bool success_p;
26800
26801   /* Begin the function-definition.  */
26802   success_p = start_function (decl_specifiers, declarator, attributes);
26803
26804   /* The things we're about to see are not directly qualified by any
26805      template headers we've seen thus far.  */
26806   reset_specialization ();
26807
26808   /* If there were names looked up in the decl-specifier-seq that we
26809      did not check, check them now.  We must wait until we are in the
26810      scope of the function to perform the checks, since the function
26811      might be a friend.  */
26812   perform_deferred_access_checks (tf_warning_or_error);
26813
26814   if (success_p)
26815     {
26816       cp_finalize_omp_declare_simd (parser, current_function_decl);
26817       parser->omp_declare_simd = NULL;
26818       cp_finalize_oacc_routine (parser, current_function_decl, true);
26819       parser->oacc_routine = NULL;
26820     }
26821
26822   if (!success_p)
26823     {
26824       /* Skip the entire function.  */
26825       cp_parser_skip_to_end_of_block_or_statement (parser);
26826       fn = error_mark_node;
26827     }
26828   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26829     {
26830       /* Seen already, skip it.  An error message has already been output.  */
26831       cp_parser_skip_to_end_of_block_or_statement (parser);
26832       fn = current_function_decl;
26833       current_function_decl = NULL_TREE;
26834       /* If this is a function from a class, pop the nested class.  */
26835       if (current_class_name)
26836         pop_nested_class ();
26837     }
26838   else
26839     {
26840       timevar_id_t tv;
26841       if (DECL_DECLARED_INLINE_P (current_function_decl))
26842         tv = TV_PARSE_INLINE;
26843       else
26844         tv = TV_PARSE_FUNC;
26845       timevar_push (tv);
26846       fn = cp_parser_function_definition_after_declarator (parser,
26847                                                          /*inline_p=*/false);
26848       timevar_pop (tv);
26849     }
26850
26851   return fn;
26852 }
26853
26854 /* Parse the part of a function-definition that follows the
26855    declarator.  INLINE_P is TRUE iff this function is an inline
26856    function defined within a class-specifier.
26857
26858    Returns the function defined.  */
26859
26860 static tree
26861 cp_parser_function_definition_after_declarator (cp_parser* parser,
26862                                                 bool inline_p)
26863 {
26864   tree fn;
26865   bool saved_in_unbraced_linkage_specification_p;
26866   bool saved_in_function_body;
26867   unsigned saved_num_template_parameter_lists;
26868   cp_token *token;
26869   bool fully_implicit_function_template_p
26870     = parser->fully_implicit_function_template_p;
26871   parser->fully_implicit_function_template_p = false;
26872   tree implicit_template_parms
26873     = parser->implicit_template_parms;
26874   parser->implicit_template_parms = 0;
26875   cp_binding_level* implicit_template_scope
26876     = parser->implicit_template_scope;
26877   parser->implicit_template_scope = 0;
26878
26879   saved_in_function_body = parser->in_function_body;
26880   parser->in_function_body = true;
26881   /* If the next token is `return', then the code may be trying to
26882      make use of the "named return value" extension that G++ used to
26883      support.  */
26884   token = cp_lexer_peek_token (parser->lexer);
26885   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26886     {
26887       /* Consume the `return' keyword.  */
26888       cp_lexer_consume_token (parser->lexer);
26889       /* Look for the identifier that indicates what value is to be
26890          returned.  */
26891       cp_parser_identifier (parser);
26892       /* Issue an error message.  */
26893       error_at (token->location,
26894                 "named return values are no longer supported");
26895       /* Skip tokens until we reach the start of the function body.  */
26896       while (true)
26897         {
26898           cp_token *token = cp_lexer_peek_token (parser->lexer);
26899           if (token->type == CPP_OPEN_BRACE
26900               || token->type == CPP_EOF
26901               || token->type == CPP_PRAGMA_EOL)
26902             break;
26903           cp_lexer_consume_token (parser->lexer);
26904         }
26905     }
26906   /* The `extern' in `extern "C" void f () { ... }' does not apply to
26907      anything declared inside `f'.  */
26908   saved_in_unbraced_linkage_specification_p
26909     = parser->in_unbraced_linkage_specification_p;
26910   parser->in_unbraced_linkage_specification_p = false;
26911   /* Inside the function, surrounding template-parameter-lists do not
26912      apply.  */
26913   saved_num_template_parameter_lists
26914     = parser->num_template_parameter_lists;
26915   parser->num_template_parameter_lists = 0;
26916
26917   /* If the next token is `try', `__transaction_atomic', or
26918      `__transaction_relaxed`, then we are looking at either function-try-block
26919      or function-transaction-block.  Note that all of these include the
26920      function-body.  */
26921   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26922     cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26923   else if (cp_lexer_next_token_is_keyword (parser->lexer,
26924       RID_TRANSACTION_RELAXED))
26925     cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26926   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26927     cp_parser_function_try_block (parser);
26928   else
26929     cp_parser_ctor_initializer_opt_and_function_body
26930       (parser, /*in_function_try_block=*/false);
26931
26932   /* Finish the function.  */
26933   fn = finish_function (inline_p);
26934   /* Generate code for it, if necessary.  */
26935   expand_or_defer_fn (fn);
26936   /* Restore the saved values.  */
26937   parser->in_unbraced_linkage_specification_p
26938     = saved_in_unbraced_linkage_specification_p;
26939   parser->num_template_parameter_lists
26940     = saved_num_template_parameter_lists;
26941   parser->in_function_body = saved_in_function_body;
26942
26943   parser->fully_implicit_function_template_p
26944     = fully_implicit_function_template_p;
26945   parser->implicit_template_parms
26946     = implicit_template_parms;
26947   parser->implicit_template_scope
26948     = implicit_template_scope;
26949
26950   if (parser->fully_implicit_function_template_p)
26951     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26952
26953   return fn;
26954 }
26955
26956 /* Parse a template-declaration body (following argument list).  */
26957
26958 static void
26959 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26960                                                  tree parameter_list,
26961                                                  bool member_p)
26962 {
26963   tree decl = NULL_TREE;
26964   bool friend_p = false;
26965
26966   /* We just processed one more parameter list.  */
26967   ++parser->num_template_parameter_lists;
26968
26969   /* Get the deferred access checks from the parameter list.  These
26970      will be checked once we know what is being declared, as for a
26971      member template the checks must be performed in the scope of the
26972      class containing the member.  */
26973   vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26974
26975   /* Tentatively parse for a new template parameter list, which can either be
26976      the template keyword or a template introduction.  */
26977   if (cp_parser_template_declaration_after_export (parser, member_p))
26978     /* OK */;
26979   else if (cxx_dialect >= cxx11
26980            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26981     decl = cp_parser_alias_declaration (parser);
26982   else
26983     {
26984       /* There are no access checks when parsing a template, as we do not
26985          know if a specialization will be a friend.  */
26986       push_deferring_access_checks (dk_no_check);
26987       cp_token *token = cp_lexer_peek_token (parser->lexer);
26988       decl = cp_parser_single_declaration (parser,
26989                                            checks,
26990                                            member_p,
26991                                            /*explicit_specialization_p=*/false,
26992                                            &friend_p);
26993       pop_deferring_access_checks ();
26994
26995       /* If this is a member template declaration, let the front
26996          end know.  */
26997       if (member_p && !friend_p && decl)
26998         {
26999           if (TREE_CODE (decl) == TYPE_DECL)
27000             cp_parser_check_access_in_redeclaration (decl, token->location);
27001
27002           decl = finish_member_template_decl (decl);
27003         }
27004       else if (friend_p && decl
27005                && DECL_DECLARES_TYPE_P (decl))
27006         make_friend_class (current_class_type, TREE_TYPE (decl),
27007                            /*complain=*/true);
27008     }
27009   /* We are done with the current parameter list.  */
27010   --parser->num_template_parameter_lists;
27011
27012   pop_deferring_access_checks ();
27013
27014   /* Finish up.  */
27015   finish_template_decl (parameter_list);
27016
27017   /* Check the template arguments for a literal operator template.  */
27018   if (decl
27019       && DECL_DECLARES_FUNCTION_P (decl)
27020       && UDLIT_OPER_P (DECL_NAME (decl)))
27021     {
27022       bool ok = true;
27023       if (parameter_list == NULL_TREE)
27024         ok = false;
27025       else
27026         {
27027           int num_parms = TREE_VEC_LENGTH (parameter_list);
27028           if (num_parms == 1)
27029             {
27030               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27031               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27032               if (TREE_TYPE (parm) != char_type_node
27033                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27034                 ok = false;
27035             }
27036           else if (num_parms == 2 && cxx_dialect >= cxx14)
27037             {
27038               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27039               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27040               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27041               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27042               if (parm == error_mark_node
27043                   || TREE_TYPE (parm) != TREE_TYPE (type)
27044                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27045                 ok = false;
27046             }
27047           else
27048             ok = false;
27049         }
27050       if (!ok)
27051         {
27052           if (cxx_dialect >= cxx14)
27053             error ("literal operator template %qD has invalid parameter list."
27054                    "  Expected non-type template argument pack <char...>"
27055                    " or <typename CharT, CharT...>",
27056                    decl);
27057           else
27058             error ("literal operator template %qD has invalid parameter list."
27059                    "  Expected non-type template argument pack <char...>",
27060                    decl);
27061         }
27062     }
27063
27064   /* Register member declarations.  */
27065   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27066     finish_member_declaration (decl);
27067   /* If DECL is a function template, we must return to parse it later.
27068      (Even though there is no definition, there might be default
27069      arguments that need handling.)  */
27070   if (member_p && decl
27071       && DECL_DECLARES_FUNCTION_P (decl))
27072     vec_safe_push (unparsed_funs_with_definitions, decl);
27073 }
27074
27075 /* Parse a template introduction header for a template-declaration.  Returns
27076    false if tentative parse fails.  */
27077
27078 static bool
27079 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27080 {
27081   cp_parser_parse_tentatively (parser);
27082
27083   tree saved_scope = parser->scope;
27084   tree saved_object_scope = parser->object_scope;
27085   tree saved_qualifying_scope = parser->qualifying_scope;
27086
27087   /* Look for the optional `::' operator.  */
27088   cp_parser_global_scope_opt (parser,
27089                               /*current_scope_valid_p=*/false);
27090   /* Look for the nested-name-specifier.  */
27091   cp_parser_nested_name_specifier_opt (parser,
27092                                        /*typename_keyword_p=*/false,
27093                                        /*check_dependency_p=*/true,
27094                                        /*type_p=*/false,
27095                                        /*is_declaration=*/false);
27096
27097   cp_token *token = cp_lexer_peek_token (parser->lexer);
27098   tree concept_name = cp_parser_identifier (parser);
27099
27100   /* Look up the concept for which we will be matching
27101      template parameters.  */
27102   tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27103                                                  token->location);
27104   parser->scope = saved_scope;
27105   parser->object_scope = saved_object_scope;
27106   parser->qualifying_scope = saved_qualifying_scope;
27107
27108   if (concept_name == error_mark_node)
27109     cp_parser_simulate_error (parser);
27110
27111   /* Look for opening brace for introduction.  */
27112   matching_braces braces;
27113   braces.require_open (parser);
27114
27115   if (!cp_parser_parse_definitely (parser))
27116     return false;
27117
27118   push_deferring_access_checks (dk_deferred);
27119
27120   /* Build vector of placeholder parameters and grab
27121      matching identifiers.  */
27122   tree introduction_list = cp_parser_introduction_list (parser);
27123
27124   /* The introduction-list shall not be empty.  */
27125   int nargs = TREE_VEC_LENGTH (introduction_list);
27126   if (nargs == 0)
27127     {
27128       error ("empty introduction-list");
27129       return true;
27130     }
27131
27132   /* Look for closing brace for introduction.  */
27133   if (!braces.require_close (parser))
27134     return true;
27135
27136   if (tmpl_decl == error_mark_node)
27137     {
27138       cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27139                                    token->location);
27140       return true;
27141     }
27142
27143   /* Build and associate the constraint.  */
27144   tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27145   if (parms && parms != error_mark_node)
27146     {
27147       cp_parser_template_declaration_after_parameters (parser, parms,
27148                                                        member_p);
27149       return true;
27150     }
27151
27152   error_at (token->location, "no matching concept for template-introduction");
27153   return true;
27154 }
27155
27156 /* Parse a normal template-declaration following the template keyword.  */
27157
27158 static void
27159 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27160 {
27161   tree parameter_list;
27162   bool need_lang_pop;
27163   location_t location = input_location;
27164
27165   /* Look for the `<' token.  */
27166   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27167     return;
27168   if (at_class_scope_p () && current_function_decl)
27169     {
27170       /* 14.5.2.2 [temp.mem]
27171
27172          A local class shall not have member templates.  */
27173       error_at (location,
27174                 "invalid declaration of member template in local class");
27175       cp_parser_skip_to_end_of_block_or_statement (parser);
27176       return;
27177     }
27178   /* [temp]
27179
27180      A template ... shall not have C linkage.  */
27181   if (current_lang_name == lang_name_c)
27182     {
27183       error_at (location, "template with C linkage");
27184       maybe_show_extern_c_location ();
27185       /* Give it C++ linkage to avoid confusing other parts of the
27186          front end.  */
27187       push_lang_context (lang_name_cplusplus);
27188       need_lang_pop = true;
27189     }
27190   else
27191     need_lang_pop = false;
27192
27193   /* We cannot perform access checks on the template parameter
27194      declarations until we know what is being declared, just as we
27195      cannot check the decl-specifier list.  */
27196   push_deferring_access_checks (dk_deferred);
27197
27198   /* If the next token is `>', then we have an invalid
27199      specialization.  Rather than complain about an invalid template
27200      parameter, issue an error message here.  */
27201   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27202     {
27203       cp_parser_error (parser, "invalid explicit specialization");
27204       begin_specialization ();
27205       parameter_list = NULL_TREE;
27206     }
27207   else
27208     {
27209       /* Parse the template parameters.  */
27210       parameter_list = cp_parser_template_parameter_list (parser);
27211     }
27212
27213   /* Look for the `>'.  */
27214   cp_parser_skip_to_end_of_template_parameter_list (parser);
27215
27216   /* Manage template requirements */
27217   if (flag_concepts)
27218   {
27219     tree reqs = get_shorthand_constraints (current_template_parms);
27220     if (tree r = cp_parser_requires_clause_opt (parser))
27221       reqs = conjoin_constraints (reqs, normalize_expression (r));
27222     TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27223   }
27224
27225   cp_parser_template_declaration_after_parameters (parser, parameter_list,
27226                                                    member_p);
27227
27228   /* For the erroneous case of a template with C linkage, we pushed an
27229      implicit C++ linkage scope; exit that scope now.  */
27230   if (need_lang_pop)
27231     pop_lang_context ();
27232 }
27233
27234 /* Parse a template-declaration, assuming that the `export' (and
27235    `extern') keywords, if present, has already been scanned.  MEMBER_P
27236    is as for cp_parser_template_declaration.  */
27237
27238 static bool
27239 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27240 {
27241   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27242     {
27243       cp_lexer_consume_token (parser->lexer);
27244       cp_parser_explicit_template_declaration (parser, member_p);
27245       return true;
27246     }
27247   else if (flag_concepts)
27248     return cp_parser_template_introduction (parser, member_p);
27249
27250   return false;
27251 }
27252
27253 /* Perform the deferred access checks from a template-parameter-list.
27254    CHECKS is a TREE_LIST of access checks, as returned by
27255    get_deferred_access_checks.  */
27256
27257 static void
27258 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27259 {
27260   ++processing_template_parmlist;
27261   perform_access_checks (checks, tf_warning_or_error);
27262   --processing_template_parmlist;
27263 }
27264
27265 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27266    `function-definition' sequence that follows a template header.
27267    If MEMBER_P is true, this declaration appears in a class scope.
27268
27269    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
27270    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
27271
27272 static tree
27273 cp_parser_single_declaration (cp_parser* parser,
27274                               vec<deferred_access_check, va_gc> *checks,
27275                               bool member_p,
27276                               bool explicit_specialization_p,
27277                               bool* friend_p)
27278 {
27279   int declares_class_or_enum;
27280   tree decl = NULL_TREE;
27281   cp_decl_specifier_seq decl_specifiers;
27282   bool function_definition_p = false;
27283   cp_token *decl_spec_token_start;
27284
27285   /* This function is only used when processing a template
27286      declaration.  */
27287   gcc_assert (innermost_scope_kind () == sk_template_parms
27288               || innermost_scope_kind () == sk_template_spec);
27289
27290   /* Defer access checks until we know what is being declared.  */
27291   push_deferring_access_checks (dk_deferred);
27292
27293   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27294      alternative.  */
27295   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27296   cp_parser_decl_specifier_seq (parser,
27297                                 CP_PARSER_FLAGS_OPTIONAL,
27298                                 &decl_specifiers,
27299                                 &declares_class_or_enum);
27300   if (friend_p)
27301     *friend_p = cp_parser_friend_p (&decl_specifiers);
27302
27303   /* There are no template typedefs.  */
27304   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27305     {
27306       error_at (decl_spec_token_start->location,
27307                 "template declaration of %<typedef%>");
27308       decl = error_mark_node;
27309     }
27310
27311   /* Gather up the access checks that occurred the
27312      decl-specifier-seq.  */
27313   stop_deferring_access_checks ();
27314
27315   /* Check for the declaration of a template class.  */
27316   if (declares_class_or_enum)
27317     {
27318       if (cp_parser_declares_only_class_p (parser)
27319           || (declares_class_or_enum & 2))
27320         {
27321           // If this is a declaration, but not a definition, associate
27322           // any constraints with the type declaration. Constraints
27323           // are associated with definitions in cp_parser_class_specifier.
27324           if (declares_class_or_enum == 1)
27325             associate_classtype_constraints (decl_specifiers.type);
27326
27327           decl = shadow_tag (&decl_specifiers);
27328
27329           /* In this case:
27330
27331                struct C {
27332                  friend template <typename T> struct A<T>::B;
27333                };
27334
27335              A<T>::B will be represented by a TYPENAME_TYPE, and
27336              therefore not recognized by shadow_tag.  */
27337           if (friend_p && *friend_p
27338               && !decl
27339               && decl_specifiers.type
27340               && TYPE_P (decl_specifiers.type))
27341             decl = decl_specifiers.type;
27342
27343           if (decl && decl != error_mark_node)
27344             decl = TYPE_NAME (decl);
27345           else
27346             decl = error_mark_node;
27347
27348           /* Perform access checks for template parameters.  */
27349           cp_parser_perform_template_parameter_access_checks (checks);
27350
27351           /* Give a helpful diagnostic for
27352                template <class T> struct A { } a;
27353              if we aren't already recovering from an error.  */
27354           if (!cp_parser_declares_only_class_p (parser)
27355               && !seen_error ())
27356             {
27357               error_at (cp_lexer_peek_token (parser->lexer)->location,
27358                         "a class template declaration must not declare "
27359                         "anything else");
27360               cp_parser_skip_to_end_of_block_or_statement (parser);
27361               goto out;
27362             }
27363         }
27364     }
27365
27366   /* Complain about missing 'typename' or other invalid type names.  */
27367   if (!decl_specifiers.any_type_specifiers_p
27368       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27369     {
27370       /* cp_parser_parse_and_diagnose_invalid_type_name calls
27371          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27372          the rest of this declaration.  */
27373       decl = error_mark_node;
27374       goto out;
27375     }
27376
27377   /* If it's not a template class, try for a template function.  If
27378      the next token is a `;', then this declaration does not declare
27379      anything.  But, if there were errors in the decl-specifiers, then
27380      the error might well have come from an attempted class-specifier.
27381      In that case, there's no need to warn about a missing declarator.  */
27382   if (!decl
27383       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27384           || decl_specifiers.type != error_mark_node))
27385     {
27386       decl = cp_parser_init_declarator (parser,
27387                                         &decl_specifiers,
27388                                         checks,
27389                                         /*function_definition_allowed_p=*/true,
27390                                         member_p,
27391                                         declares_class_or_enum,
27392                                         &function_definition_p,
27393                                         NULL, NULL, NULL);
27394
27395     /* 7.1.1-1 [dcl.stc]
27396
27397        A storage-class-specifier shall not be specified in an explicit
27398        specialization...  */
27399     if (decl
27400         && explicit_specialization_p
27401         && decl_specifiers.storage_class != sc_none)
27402       {
27403         error_at (decl_spec_token_start->location,
27404                   "explicit template specialization cannot have a storage class");
27405         decl = error_mark_node;
27406       }
27407
27408     if (decl && VAR_P (decl))
27409       check_template_variable (decl);
27410     }
27411
27412   /* Look for a trailing `;' after the declaration.  */
27413   if (!function_definition_p
27414       && (decl == error_mark_node
27415           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27416     cp_parser_skip_to_end_of_block_or_statement (parser);
27417
27418  out:
27419   pop_deferring_access_checks ();
27420
27421   /* Clear any current qualification; whatever comes next is the start
27422      of something new.  */
27423   parser->scope = NULL_TREE;
27424   parser->qualifying_scope = NULL_TREE;
27425   parser->object_scope = NULL_TREE;
27426
27427   return decl;
27428 }
27429
27430 /* Parse a cast-expression that is not the operand of a unary "&".  */
27431
27432 static cp_expr
27433 cp_parser_simple_cast_expression (cp_parser *parser)
27434 {
27435   return cp_parser_cast_expression (parser, /*address_p=*/false,
27436                                     /*cast_p=*/false, /*decltype*/false, NULL);
27437 }
27438
27439 /* Parse a functional cast to TYPE.  Returns an expression
27440    representing the cast.  */
27441
27442 static cp_expr
27443 cp_parser_functional_cast (cp_parser* parser, tree type)
27444 {
27445   vec<tree, va_gc> *vec;
27446   tree expression_list;
27447   cp_expr cast;
27448   bool nonconst_p;
27449
27450   location_t start_loc = input_location;
27451
27452   if (!type)
27453     type = error_mark_node;
27454
27455   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27456     {
27457       cp_lexer_set_source_position (parser->lexer);
27458       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27459       expression_list = cp_parser_braced_list (parser, &nonconst_p);
27460       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27461       if (TREE_CODE (type) == TYPE_DECL)
27462         type = TREE_TYPE (type);
27463
27464       cast = finish_compound_literal (type, expression_list,
27465                                       tf_warning_or_error, fcl_functional);
27466       /* Create a location of the form:
27467             type_name{i, f}
27468             ^~~~~~~~~~~~~~~
27469          with caret == start at the start of the type name,
27470          finishing at the closing brace.  */
27471       location_t finish_loc
27472         = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27473       location_t combined_loc = make_location (start_loc, start_loc,
27474                                                finish_loc);
27475       cast.set_location (combined_loc);
27476       return cast;
27477    }
27478
27479
27480   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27481                                                  /*cast_p=*/true,
27482                                                  /*allow_expansion_p=*/true,
27483                                                  /*non_constant_p=*/NULL);
27484   if (vec == NULL)
27485     expression_list = error_mark_node;
27486   else
27487     {
27488       expression_list = build_tree_list_vec (vec);
27489       release_tree_vector (vec);
27490     }
27491
27492   cast = build_functional_cast (type, expression_list,
27493                                 tf_warning_or_error);
27494   /* [expr.const]/1: In an integral constant expression "only type
27495      conversions to integral or enumeration type can be used".  */
27496   if (TREE_CODE (type) == TYPE_DECL)
27497     type = TREE_TYPE (type);
27498   if (cast != error_mark_node
27499       && !cast_valid_in_integral_constant_expression_p (type)
27500       && cp_parser_non_integral_constant_expression (parser,
27501                                                      NIC_CONSTRUCTOR))
27502     return error_mark_node;
27503
27504   /* Create a location of the form:
27505        float(i)
27506        ^~~~~~~~
27507      with caret == start at the start of the type name,
27508      finishing at the closing paren.  */
27509   location_t finish_loc
27510     = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27511   location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27512   cast.set_location (combined_loc);
27513   return cast;
27514 }
27515
27516 /* Save the tokens that make up the body of a member function defined
27517    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
27518    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
27519    specifiers applied to the declaration.  Returns the FUNCTION_DECL
27520    for the member function.  */
27521
27522 static tree
27523 cp_parser_save_member_function_body (cp_parser* parser,
27524                                      cp_decl_specifier_seq *decl_specifiers,
27525                                      cp_declarator *declarator,
27526                                      tree attributes)
27527 {
27528   cp_token *first;
27529   cp_token *last;
27530   tree fn;
27531   bool function_try_block = false;
27532
27533   /* Create the FUNCTION_DECL.  */
27534   fn = grokmethod (decl_specifiers, declarator, attributes);
27535   cp_finalize_omp_declare_simd (parser, fn);
27536   cp_finalize_oacc_routine (parser, fn, true);
27537   /* If something went badly wrong, bail out now.  */
27538   if (fn == error_mark_node)
27539     {
27540       /* If there's a function-body, skip it.  */
27541       if (cp_parser_token_starts_function_definition_p
27542           (cp_lexer_peek_token (parser->lexer)))
27543         cp_parser_skip_to_end_of_block_or_statement (parser);
27544       return error_mark_node;
27545     }
27546
27547   /* Remember it, if there default args to post process.  */
27548   cp_parser_save_default_args (parser, fn);
27549
27550   /* Save away the tokens that make up the body of the
27551      function.  */
27552   first = parser->lexer->next_token;
27553
27554   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27555     cp_lexer_consume_token (parser->lexer);
27556   else if (cp_lexer_next_token_is_keyword (parser->lexer,
27557                                            RID_TRANSACTION_ATOMIC))
27558     {
27559       cp_lexer_consume_token (parser->lexer);
27560       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
27561       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27562           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27563           && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27564               || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27565           && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27566           && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27567         {
27568           cp_lexer_consume_token (parser->lexer);
27569           cp_lexer_consume_token (parser->lexer);
27570           cp_lexer_consume_token (parser->lexer);
27571           cp_lexer_consume_token (parser->lexer);
27572           cp_lexer_consume_token (parser->lexer);
27573         }
27574       else
27575         while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27576                && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27577           {
27578             cp_lexer_consume_token (parser->lexer);
27579             if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27580               break;
27581           }
27582     }
27583
27584   /* Handle function try blocks.  */
27585   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27586     {
27587       cp_lexer_consume_token (parser->lexer);
27588       function_try_block = true;
27589     }
27590   /* We can have braced-init-list mem-initializers before the fn body.  */
27591   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27592     {
27593       cp_lexer_consume_token (parser->lexer);
27594       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27595         {
27596           /* cache_group will stop after an un-nested { } pair, too.  */
27597           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27598             break;
27599
27600           /* variadic mem-inits have ... after the ')'.  */
27601           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27602             cp_lexer_consume_token (parser->lexer);
27603         }
27604     }
27605   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27606   /* Handle function try blocks.  */
27607   if (function_try_block)
27608     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27609       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27610   last = parser->lexer->next_token;
27611
27612   /* Save away the inline definition; we will process it when the
27613      class is complete.  */
27614   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27615   DECL_PENDING_INLINE_P (fn) = 1;
27616
27617   /* We need to know that this was defined in the class, so that
27618      friend templates are handled correctly.  */
27619   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27620
27621   /* Add FN to the queue of functions to be parsed later.  */
27622   vec_safe_push (unparsed_funs_with_definitions, fn);
27623
27624   return fn;
27625 }
27626
27627 /* Save the tokens that make up the in-class initializer for a non-static
27628    data member.  Returns a DEFAULT_ARG.  */
27629
27630 static tree
27631 cp_parser_save_nsdmi (cp_parser* parser)
27632 {
27633   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27634 }
27635
27636 /* Parse a template-argument-list, as well as the trailing ">" (but
27637    not the opening "<").  See cp_parser_template_argument_list for the
27638    return value.  */
27639
27640 static tree
27641 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27642 {
27643   tree arguments;
27644   tree saved_scope;
27645   tree saved_qualifying_scope;
27646   tree saved_object_scope;
27647   bool saved_greater_than_is_operator_p;
27648   int saved_unevaluated_operand;
27649   int saved_inhibit_evaluation_warnings;
27650
27651   /* [temp.names]
27652
27653      When parsing a template-id, the first non-nested `>' is taken as
27654      the end of the template-argument-list rather than a greater-than
27655      operator.  */
27656   saved_greater_than_is_operator_p
27657     = parser->greater_than_is_operator_p;
27658   parser->greater_than_is_operator_p = false;
27659   /* Parsing the argument list may modify SCOPE, so we save it
27660      here.  */
27661   saved_scope = parser->scope;
27662   saved_qualifying_scope = parser->qualifying_scope;
27663   saved_object_scope = parser->object_scope;
27664   /* We need to evaluate the template arguments, even though this
27665      template-id may be nested within a "sizeof".  */
27666   saved_unevaluated_operand = cp_unevaluated_operand;
27667   cp_unevaluated_operand = 0;
27668   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27669   c_inhibit_evaluation_warnings = 0;
27670   /* Parse the template-argument-list itself.  */
27671   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27672       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27673     arguments = NULL_TREE;
27674   else
27675     arguments = cp_parser_template_argument_list (parser);
27676   /* Look for the `>' that ends the template-argument-list. If we find
27677      a '>>' instead, it's probably just a typo.  */
27678   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27679     {
27680       if (cxx_dialect != cxx98)
27681         {
27682           /* In C++0x, a `>>' in a template argument list or cast
27683              expression is considered to be two separate `>'
27684              tokens. So, change the current token to a `>', but don't
27685              consume it: it will be consumed later when the outer
27686              template argument list (or cast expression) is parsed.
27687              Note that this replacement of `>' for `>>' is necessary
27688              even if we are parsing tentatively: in the tentative
27689              case, after calling
27690              cp_parser_enclosed_template_argument_list we will always
27691              throw away all of the template arguments and the first
27692              closing `>', either because the template argument list
27693              was erroneous or because we are replacing those tokens
27694              with a CPP_TEMPLATE_ID token.  The second `>' (which will
27695              not have been thrown away) is needed either to close an
27696              outer template argument list or to complete a new-style
27697              cast.  */
27698           cp_token *token = cp_lexer_peek_token (parser->lexer);
27699           token->type = CPP_GREATER;
27700         }
27701       else if (!saved_greater_than_is_operator_p)
27702         {
27703           /* If we're in a nested template argument list, the '>>' has
27704             to be a typo for '> >'. We emit the error message, but we
27705             continue parsing and we push a '>' as next token, so that
27706             the argument list will be parsed correctly.  Note that the
27707             global source location is still on the token before the
27708             '>>', so we need to say explicitly where we want it.  */
27709           cp_token *token = cp_lexer_peek_token (parser->lexer);
27710           gcc_rich_location richloc (token->location);
27711           richloc.add_fixit_replace ("> >");
27712           error_at (&richloc, "%<>>%> should be %<> >%> "
27713                     "within a nested template argument list");
27714
27715           token->type = CPP_GREATER;
27716         }
27717       else
27718         {
27719           /* If this is not a nested template argument list, the '>>'
27720             is a typo for '>'. Emit an error message and continue.
27721             Same deal about the token location, but here we can get it
27722             right by consuming the '>>' before issuing the diagnostic.  */
27723           cp_token *token = cp_lexer_consume_token (parser->lexer);
27724           error_at (token->location,
27725                     "spurious %<>>%>, use %<>%> to terminate "
27726                     "a template argument list");
27727         }
27728     }
27729   else
27730     cp_parser_skip_to_end_of_template_parameter_list (parser);
27731   /* The `>' token might be a greater-than operator again now.  */
27732   parser->greater_than_is_operator_p
27733     = saved_greater_than_is_operator_p;
27734   /* Restore the SAVED_SCOPE.  */
27735   parser->scope = saved_scope;
27736   parser->qualifying_scope = saved_qualifying_scope;
27737   parser->object_scope = saved_object_scope;
27738   cp_unevaluated_operand = saved_unevaluated_operand;
27739   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27740
27741   return arguments;
27742 }
27743
27744 /* MEMBER_FUNCTION is a member function, or a friend.  If default
27745    arguments, or the body of the function have not yet been parsed,
27746    parse them now.  */
27747
27748 static void
27749 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27750 {
27751   timevar_push (TV_PARSE_INMETH);
27752   /* If this member is a template, get the underlying
27753      FUNCTION_DECL.  */
27754   if (DECL_FUNCTION_TEMPLATE_P (member_function))
27755     member_function = DECL_TEMPLATE_RESULT (member_function);
27756
27757   /* There should not be any class definitions in progress at this
27758      point; the bodies of members are only parsed outside of all class
27759      definitions.  */
27760   gcc_assert (parser->num_classes_being_defined == 0);
27761   /* While we're parsing the member functions we might encounter more
27762      classes.  We want to handle them right away, but we don't want
27763      them getting mixed up with functions that are currently in the
27764      queue.  */
27765   push_unparsed_function_queues (parser);
27766
27767   /* Make sure that any template parameters are in scope.  */
27768   maybe_begin_member_template_processing (member_function);
27769
27770   /* If the body of the function has not yet been parsed, parse it
27771      now.  */
27772   if (DECL_PENDING_INLINE_P (member_function))
27773     {
27774       tree function_scope;
27775       cp_token_cache *tokens;
27776
27777       /* The function is no longer pending; we are processing it.  */
27778       tokens = DECL_PENDING_INLINE_INFO (member_function);
27779       DECL_PENDING_INLINE_INFO (member_function) = NULL;
27780       DECL_PENDING_INLINE_P (member_function) = 0;
27781
27782       /* If this is a local class, enter the scope of the containing
27783          function.  */
27784       function_scope = current_function_decl;
27785       if (function_scope)
27786         push_function_context ();
27787
27788       /* Push the body of the function onto the lexer stack.  */
27789       cp_parser_push_lexer_for_tokens (parser, tokens);
27790
27791       /* Let the front end know that we going to be defining this
27792          function.  */
27793       start_preparsed_function (member_function, NULL_TREE,
27794                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
27795
27796       /* Don't do access checking if it is a templated function.  */
27797       if (processing_template_decl)
27798         push_deferring_access_checks (dk_no_check);
27799
27800       /* #pragma omp declare reduction needs special parsing.  */
27801       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27802         {
27803           parser->lexer->in_pragma = true;
27804           cp_parser_omp_declare_reduction_exprs (member_function, parser);
27805           finish_function (/*inline_p=*/true);
27806           cp_check_omp_declare_reduction (member_function);
27807         }
27808       else
27809         /* Now, parse the body of the function.  */
27810         cp_parser_function_definition_after_declarator (parser,
27811                                                         /*inline_p=*/true);
27812
27813       if (processing_template_decl)
27814         pop_deferring_access_checks ();
27815
27816       /* Leave the scope of the containing function.  */
27817       if (function_scope)
27818         pop_function_context ();
27819       cp_parser_pop_lexer (parser);
27820     }
27821
27822   /* Remove any template parameters from the symbol table.  */
27823   maybe_end_member_template_processing ();
27824
27825   /* Restore the queue.  */
27826   pop_unparsed_function_queues (parser);
27827   timevar_pop (TV_PARSE_INMETH);
27828 }
27829
27830 /* If DECL contains any default args, remember it on the unparsed
27831    functions queue.  */
27832
27833 static void
27834 cp_parser_save_default_args (cp_parser* parser, tree decl)
27835 {
27836   tree probe;
27837
27838   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27839        probe;
27840        probe = TREE_CHAIN (probe))
27841     if (TREE_PURPOSE (probe))
27842       {
27843         cp_default_arg_entry entry = {current_class_type, decl};
27844         vec_safe_push (unparsed_funs_with_default_args, entry);
27845         break;
27846       }
27847 }
27848
27849 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27850    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
27851    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
27852    from the parameter-type-list.  */
27853
27854 static tree
27855 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27856                                       tree default_arg, tree parmtype)
27857 {
27858   cp_token_cache *tokens;
27859   tree parsed_arg;
27860   bool dummy;
27861
27862   if (default_arg == error_mark_node)
27863     return error_mark_node;
27864
27865   /* Push the saved tokens for the default argument onto the parser's
27866      lexer stack.  */
27867   tokens = DEFARG_TOKENS (default_arg);
27868   cp_parser_push_lexer_for_tokens (parser, tokens);
27869
27870   start_lambda_scope (decl);
27871
27872   /* Parse the default argument.  */
27873   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27874   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27875     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27876
27877   finish_lambda_scope ();
27878
27879   if (parsed_arg == error_mark_node)
27880     cp_parser_skip_to_end_of_statement (parser);
27881
27882   if (!processing_template_decl)
27883     {
27884       /* In a non-template class, check conversions now.  In a template,
27885          we'll wait and instantiate these as needed.  */
27886       if (TREE_CODE (decl) == PARM_DECL)
27887         parsed_arg = check_default_argument (parmtype, parsed_arg,
27888                                              tf_warning_or_error);
27889       else if (maybe_reject_flexarray_init (decl, parsed_arg))
27890         parsed_arg = error_mark_node;
27891       else
27892         parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27893     }
27894
27895   /* If the token stream has not been completely used up, then
27896      there was extra junk after the end of the default
27897      argument.  */
27898   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27899     {
27900       if (TREE_CODE (decl) == PARM_DECL)
27901         cp_parser_error (parser, "expected %<,%>");
27902       else
27903         cp_parser_error (parser, "expected %<;%>");
27904     }
27905
27906   /* Revert to the main lexer.  */
27907   cp_parser_pop_lexer (parser);
27908
27909   return parsed_arg;
27910 }
27911
27912 /* FIELD is a non-static data member with an initializer which we saved for
27913    later; parse it now.  */
27914
27915 static void
27916 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27917 {
27918   tree def;
27919
27920   maybe_begin_member_template_processing (field);
27921
27922   push_unparsed_function_queues (parser);
27923   def = cp_parser_late_parse_one_default_arg (parser, field,
27924                                               DECL_INITIAL (field),
27925                                               NULL_TREE);
27926   pop_unparsed_function_queues (parser);
27927
27928   maybe_end_member_template_processing ();
27929
27930   DECL_INITIAL (field) = def;
27931 }
27932
27933 /* FN is a FUNCTION_DECL which may contains a parameter with an
27934    unparsed DEFAULT_ARG.  Parse the default args now.  This function
27935    assumes that the current scope is the scope in which the default
27936    argument should be processed.  */
27937
27938 static void
27939 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27940 {
27941   bool saved_local_variables_forbidden_p;
27942   tree parm, parmdecl;
27943
27944   /* While we're parsing the default args, we might (due to the
27945      statement expression extension) encounter more classes.  We want
27946      to handle them right away, but we don't want them getting mixed
27947      up with default args that are currently in the queue.  */
27948   push_unparsed_function_queues (parser);
27949
27950   /* Local variable names (and the `this' keyword) may not appear
27951      in a default argument.  */
27952   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27953   parser->local_variables_forbidden_p = true;
27954
27955   push_defarg_context (fn);
27956
27957   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27958          parmdecl = DECL_ARGUMENTS (fn);
27959        parm && parm != void_list_node;
27960        parm = TREE_CHAIN (parm),
27961          parmdecl = DECL_CHAIN (parmdecl))
27962     {
27963       tree default_arg = TREE_PURPOSE (parm);
27964       tree parsed_arg;
27965       vec<tree, va_gc> *insts;
27966       tree copy;
27967       unsigned ix;
27968
27969       if (!default_arg)
27970         continue;
27971
27972       if (TREE_CODE (default_arg) != DEFAULT_ARG)
27973         /* This can happen for a friend declaration for a function
27974            already declared with default arguments.  */
27975         continue;
27976
27977       parsed_arg
27978         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27979                                                 default_arg,
27980                                                 TREE_VALUE (parm));
27981       TREE_PURPOSE (parm) = parsed_arg;
27982
27983       /* Update any instantiations we've already created.  */
27984       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27985            vec_safe_iterate (insts, ix, &copy); ix++)
27986         TREE_PURPOSE (copy) = parsed_arg;
27987     }
27988
27989   pop_defarg_context ();
27990
27991   /* Make sure no default arg is missing.  */
27992   check_default_args (fn);
27993
27994   /* Restore the state of local_variables_forbidden_p.  */
27995   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27996
27997   /* Restore the queue.  */
27998   pop_unparsed_function_queues (parser);
27999 }
28000
28001 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28002
28003      sizeof ... ( identifier )
28004
28005    where the 'sizeof' token has already been consumed.  */
28006
28007 static tree
28008 cp_parser_sizeof_pack (cp_parser *parser)
28009 {
28010   /* Consume the `...'.  */
28011   cp_lexer_consume_token (parser->lexer);
28012   maybe_warn_variadic_templates ();
28013
28014   matching_parens parens;
28015   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28016   if (paren)
28017     parens.consume_open (parser);
28018   else
28019     permerror (cp_lexer_peek_token (parser->lexer)->location,
28020                "%<sizeof...%> argument must be surrounded by parentheses");
28021
28022   cp_token *token = cp_lexer_peek_token (parser->lexer);
28023   tree name = cp_parser_identifier (parser);
28024   if (name == error_mark_node)
28025     return error_mark_node;
28026   /* The name is not qualified.  */
28027   parser->scope = NULL_TREE;
28028   parser->qualifying_scope = NULL_TREE;
28029   parser->object_scope = NULL_TREE;
28030   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28031   if (expr == error_mark_node)
28032     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28033                                  token->location);
28034   if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28035     expr = TREE_TYPE (expr);
28036   else if (TREE_CODE (expr) == CONST_DECL)
28037     expr = DECL_INITIAL (expr);
28038   expr = make_pack_expansion (expr);
28039   PACK_EXPANSION_SIZEOF_P (expr) = true;
28040
28041   if (paren)
28042     parens.require_close (parser);
28043
28044   return expr;
28045 }
28046
28047 /* Parse the operand of `sizeof' (or a similar operator).  Returns
28048    either a TYPE or an expression, depending on the form of the
28049    input.  The KEYWORD indicates which kind of expression we have
28050    encountered.  */
28051
28052 static tree
28053 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28054 {
28055   tree expr = NULL_TREE;
28056   const char *saved_message;
28057   char *tmp;
28058   bool saved_integral_constant_expression_p;
28059   bool saved_non_integral_constant_expression_p;
28060
28061   /* If it's a `...', then we are computing the length of a parameter
28062      pack.  */
28063   if (keyword == RID_SIZEOF
28064       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28065     return cp_parser_sizeof_pack (parser);
28066
28067   /* Types cannot be defined in a `sizeof' expression.  Save away the
28068      old message.  */
28069   saved_message = parser->type_definition_forbidden_message;
28070   /* And create the new one.  */
28071   tmp = concat ("types may not be defined in %<",
28072                 IDENTIFIER_POINTER (ridpointers[keyword]),
28073                 "%> expressions", NULL);
28074   parser->type_definition_forbidden_message = tmp;
28075
28076   /* The restrictions on constant-expressions do not apply inside
28077      sizeof expressions.  */
28078   saved_integral_constant_expression_p
28079     = parser->integral_constant_expression_p;
28080   saved_non_integral_constant_expression_p
28081     = parser->non_integral_constant_expression_p;
28082   parser->integral_constant_expression_p = false;
28083
28084   /* Do not actually evaluate the expression.  */
28085   ++cp_unevaluated_operand;
28086   ++c_inhibit_evaluation_warnings;
28087   /* If it's a `(', then we might be looking at the type-id
28088      construction.  */
28089   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28090     {
28091       tree type = NULL_TREE;
28092
28093       /* We can't be sure yet whether we're looking at a type-id or an
28094          expression.  */
28095       cp_parser_parse_tentatively (parser);
28096
28097       matching_parens parens;
28098       parens.consume_open (parser);
28099
28100       /* Note: as a GNU Extension, compound literals are considered
28101          postfix-expressions as they are in C99, so they are valid
28102          arguments to sizeof.  See comment in cp_parser_cast_expression
28103          for details.  */
28104       if (cp_parser_compound_literal_p (parser))
28105         cp_parser_simulate_error (parser);
28106       else
28107         {
28108           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28109           parser->in_type_id_in_expr_p = true;
28110           /* Look for the type-id.  */
28111           type = cp_parser_type_id (parser);
28112           /* Look for the closing `)'.  */
28113           parens.require_close (parser);
28114           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28115         }
28116
28117       /* If all went well, then we're done.  */
28118       if (cp_parser_parse_definitely (parser))
28119         {
28120           cp_decl_specifier_seq decl_specs;
28121
28122           /* Build a trivial decl-specifier-seq.  */
28123           clear_decl_specs (&decl_specs);
28124           decl_specs.type = type;
28125
28126           /* Call grokdeclarator to figure out what type this is.  */
28127           expr = grokdeclarator (NULL,
28128                                  &decl_specs,
28129                                  TYPENAME,
28130                                  /*initialized=*/0,
28131                                  /*attrlist=*/NULL);
28132         }
28133     }
28134
28135   /* If the type-id production did not work out, then we must be
28136      looking at the unary-expression production.  */
28137   if (!expr)
28138     expr = cp_parser_unary_expression (parser);
28139
28140   /* Go back to evaluating expressions.  */
28141   --cp_unevaluated_operand;
28142   --c_inhibit_evaluation_warnings;
28143
28144   /* Free the message we created.  */
28145   free (tmp);
28146   /* And restore the old one.  */
28147   parser->type_definition_forbidden_message = saved_message;
28148   parser->integral_constant_expression_p
28149     = saved_integral_constant_expression_p;
28150   parser->non_integral_constant_expression_p
28151     = saved_non_integral_constant_expression_p;
28152
28153   return expr;
28154 }
28155
28156 /* If the current declaration has no declarator, return true.  */
28157
28158 static bool
28159 cp_parser_declares_only_class_p (cp_parser *parser)
28160 {
28161   /* If the next token is a `;' or a `,' then there is no
28162      declarator.  */
28163   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28164           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28165 }
28166
28167 /* Update the DECL_SPECS to reflect the storage class indicated by
28168    KEYWORD.  */
28169
28170 static void
28171 cp_parser_set_storage_class (cp_parser *parser,
28172                              cp_decl_specifier_seq *decl_specs,
28173                              enum rid keyword,
28174                              cp_token *token)
28175 {
28176   cp_storage_class storage_class;
28177
28178   if (parser->in_unbraced_linkage_specification_p)
28179     {
28180       error_at (token->location, "invalid use of %qD in linkage specification",
28181                 ridpointers[keyword]);
28182       return;
28183     }
28184   else if (decl_specs->storage_class != sc_none)
28185     {
28186       decl_specs->conflicting_specifiers_p = true;
28187       return;
28188     }
28189
28190   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28191       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28192       && decl_specs->gnu_thread_keyword_p)
28193     {
28194       pedwarn (decl_specs->locations[ds_thread], 0,
28195                 "%<__thread%> before %qD", ridpointers[keyword]);
28196     }
28197
28198   switch (keyword)
28199     {
28200     case RID_AUTO:
28201       storage_class = sc_auto;
28202       break;
28203     case RID_REGISTER:
28204       storage_class = sc_register;
28205       break;
28206     case RID_STATIC:
28207       storage_class = sc_static;
28208       break;
28209     case RID_EXTERN:
28210       storage_class = sc_extern;
28211       break;
28212     case RID_MUTABLE:
28213       storage_class = sc_mutable;
28214       break;
28215     default:
28216       gcc_unreachable ();
28217     }
28218   decl_specs->storage_class = storage_class;
28219   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28220
28221   /* A storage class specifier cannot be applied alongside a typedef 
28222      specifier. If there is a typedef specifier present then set 
28223      conflicting_specifiers_p which will trigger an error later
28224      on in grokdeclarator. */
28225   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28226     decl_specs->conflicting_specifiers_p = true;
28227 }
28228
28229 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
28230    is true, the type is a class or enum definition.  */
28231
28232 static void
28233 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28234                               tree type_spec,
28235                               cp_token *token,
28236                               bool type_definition_p)
28237 {
28238   decl_specs->any_specifiers_p = true;
28239
28240   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28241      (with, for example, in "typedef int wchar_t;") we remember that
28242      this is what happened.  In system headers, we ignore these
28243      declarations so that G++ can work with system headers that are not
28244      C++-safe.  */
28245   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28246       && !type_definition_p
28247       && (type_spec == boolean_type_node
28248           || type_spec == char16_type_node
28249           || type_spec == char32_type_node
28250           || type_spec == wchar_type_node)
28251       && (decl_specs->type
28252           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28253           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28254           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28255           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28256     {
28257       decl_specs->redefined_builtin_type = type_spec;
28258       set_and_check_decl_spec_loc (decl_specs,
28259                                    ds_redefined_builtin_type_spec,
28260                                    token);
28261       if (!decl_specs->type)
28262         {
28263           decl_specs->type = type_spec;
28264           decl_specs->type_definition_p = false;
28265           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28266         }
28267     }
28268   else if (decl_specs->type)
28269     decl_specs->multiple_types_p = true;
28270   else
28271     {
28272       decl_specs->type = type_spec;
28273       decl_specs->type_definition_p = type_definition_p;
28274       decl_specs->redefined_builtin_type = NULL_TREE;
28275       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28276     }
28277 }
28278
28279 /* True iff TOKEN is the GNU keyword __thread.  */
28280
28281 static bool
28282 token_is__thread (cp_token *token)
28283 {
28284   gcc_assert (token->keyword == RID_THREAD);
28285   return id_equal (token->u.value, "__thread");
28286 }
28287
28288 /* Set the location for a declarator specifier and check if it is
28289    duplicated.
28290
28291    DECL_SPECS is the sequence of declarator specifiers onto which to
28292    set the location.
28293
28294    DS is the single declarator specifier to set which location  is to
28295    be set onto the existing sequence of declarators.
28296
28297    LOCATION is the location for the declarator specifier to
28298    consider.  */
28299
28300 static void
28301 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28302                              cp_decl_spec ds, cp_token *token)
28303 {
28304   gcc_assert (ds < ds_last);
28305
28306   if (decl_specs == NULL)
28307     return;
28308
28309   source_location location = token->location;
28310
28311   if (decl_specs->locations[ds] == 0)
28312     {
28313       decl_specs->locations[ds] = location;
28314       if (ds == ds_thread)
28315         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28316     }
28317   else
28318     {
28319       if (ds == ds_long)
28320         {
28321           if (decl_specs->locations[ds_long_long] != 0)
28322             error_at (location,
28323                       "%<long long long%> is too long for GCC");
28324           else
28325             {
28326               decl_specs->locations[ds_long_long] = location;
28327               pedwarn_cxx98 (location,
28328                              OPT_Wlong_long, 
28329                              "ISO C++ 1998 does not support %<long long%>");
28330             }
28331         }
28332       else if (ds == ds_thread)
28333         {
28334           bool gnu = token_is__thread (token);
28335           if (gnu != decl_specs->gnu_thread_keyword_p)
28336             error_at (location,
28337                       "both %<__thread%> and %<thread_local%> specified");
28338           else
28339             {
28340               gcc_rich_location richloc (location);
28341               richloc.add_fixit_remove ();
28342               error_at (&richloc, "duplicate %qD", token->u.value);
28343             }
28344         }
28345       else
28346         {
28347           static const char *const decl_spec_names[] = {
28348             "signed",
28349             "unsigned",
28350             "short",
28351             "long",
28352             "const",
28353             "volatile",
28354             "restrict",
28355             "inline",
28356             "virtual",
28357             "explicit",
28358             "friend",
28359             "typedef",
28360             "using",
28361             "constexpr",
28362             "__complex"
28363           };
28364           gcc_rich_location richloc (location);
28365           richloc.add_fixit_remove ();
28366           error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28367         }
28368     }
28369 }
28370
28371 /* Return true iff the declarator specifier DS is present in the
28372    sequence of declarator specifiers DECL_SPECS.  */
28373
28374 bool
28375 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28376                           cp_decl_spec ds)
28377 {
28378   gcc_assert (ds < ds_last);
28379
28380   if (decl_specs == NULL)
28381     return false;
28382
28383   return decl_specs->locations[ds] != 0;
28384 }
28385
28386 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28387    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
28388
28389 static bool
28390 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28391 {
28392   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28393 }
28394
28395 /* Issue an error message indicating that TOKEN_DESC was expected.
28396    If KEYWORD is true, it indicated this function is called by
28397    cp_parser_require_keword and the required token can only be
28398    a indicated keyword.
28399
28400    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28401    within any error as the location of an "opening" token matching
28402    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28403    RT_CLOSE_PAREN).  */
28404
28405 static void
28406 cp_parser_required_error (cp_parser *parser,
28407                           required_token token_desc,
28408                           bool keyword,
28409                           location_t matching_location)
28410 {
28411   if (cp_parser_simulate_error (parser))
28412     return;
28413
28414   const char *gmsgid = NULL;
28415   switch (token_desc)
28416     {
28417       case RT_NEW:
28418         gmsgid = G_("expected %<new%>");
28419         break;
28420       case RT_DELETE:
28421         gmsgid = G_("expected %<delete%>");
28422         break;
28423       case RT_RETURN:
28424         gmsgid = G_("expected %<return%>");
28425         break;
28426       case RT_WHILE:
28427         gmsgid = G_("expected %<while%>");
28428         break;
28429       case RT_EXTERN:
28430         gmsgid = G_("expected %<extern%>");
28431         break;
28432       case RT_STATIC_ASSERT:
28433         gmsgid = G_("expected %<static_assert%>");
28434         break;
28435       case RT_DECLTYPE:
28436         gmsgid = G_("expected %<decltype%>");
28437         break;
28438       case RT_OPERATOR:
28439         gmsgid = G_("expected %<operator%>");
28440         break;
28441       case RT_CLASS:
28442         gmsgid = G_("expected %<class%>");
28443         break;
28444       case RT_TEMPLATE:
28445         gmsgid = G_("expected %<template%>");
28446         break;
28447       case RT_NAMESPACE:
28448         gmsgid = G_("expected %<namespace%>");
28449         break;
28450       case RT_USING:
28451         gmsgid = G_("expected %<using%>");
28452         break;
28453       case RT_ASM:
28454         gmsgid = G_("expected %<asm%>");
28455         break;
28456       case RT_TRY:
28457         gmsgid = G_("expected %<try%>");
28458         break;
28459       case RT_CATCH:
28460         gmsgid = G_("expected %<catch%>");
28461         break;
28462       case RT_THROW:
28463         gmsgid = G_("expected %<throw%>");
28464         break;
28465       case RT_LABEL:
28466         gmsgid = G_("expected %<__label__%>");
28467         break;
28468       case RT_AT_TRY:
28469         gmsgid = G_("expected %<@try%>");
28470         break;
28471       case RT_AT_SYNCHRONIZED:
28472         gmsgid = G_("expected %<@synchronized%>");
28473         break;
28474       case RT_AT_THROW:
28475         gmsgid = G_("expected %<@throw%>");
28476         break;
28477       case RT_TRANSACTION_ATOMIC:
28478         gmsgid = G_("expected %<__transaction_atomic%>");
28479         break;
28480       case RT_TRANSACTION_RELAXED:
28481         gmsgid = G_("expected %<__transaction_relaxed%>");
28482         break;
28483       default:
28484         break;
28485     }
28486
28487   if (!gmsgid && !keyword)
28488     {
28489       switch (token_desc)
28490         {
28491           case RT_SEMICOLON:
28492             gmsgid = G_("expected %<;%>");
28493             break;
28494           case RT_OPEN_PAREN:
28495             gmsgid = G_("expected %<(%>");
28496             break;
28497           case RT_CLOSE_BRACE:
28498             gmsgid = G_("expected %<}%>");
28499             break;
28500           case RT_OPEN_BRACE:
28501             gmsgid = G_("expected %<{%>");
28502             break;
28503           case RT_CLOSE_SQUARE:
28504             gmsgid = G_("expected %<]%>");
28505             break;
28506           case RT_OPEN_SQUARE:
28507             gmsgid = G_("expected %<[%>");
28508             break;
28509           case RT_COMMA:
28510             gmsgid = G_("expected %<,%>");
28511             break;
28512           case RT_SCOPE:
28513             gmsgid = G_("expected %<::%>");
28514             break;
28515           case RT_LESS:
28516             gmsgid = G_("expected %<<%>");
28517             break;
28518           case RT_GREATER:
28519             gmsgid = G_("expected %<>%>");
28520             break;
28521           case RT_EQ:
28522             gmsgid = G_("expected %<=%>");
28523             break;
28524           case RT_ELLIPSIS:
28525             gmsgid = G_("expected %<...%>");
28526             break;
28527           case RT_MULT:
28528             gmsgid = G_("expected %<*%>");
28529             break;
28530           case RT_COMPL:
28531             gmsgid = G_("expected %<~%>");
28532             break;
28533           case RT_COLON:
28534             gmsgid = G_("expected %<:%>");
28535             break;
28536           case RT_COLON_SCOPE:
28537             gmsgid = G_("expected %<:%> or %<::%>");
28538             break;
28539           case RT_CLOSE_PAREN:
28540             gmsgid = G_("expected %<)%>");
28541             break;
28542           case RT_COMMA_CLOSE_PAREN:
28543             gmsgid = G_("expected %<,%> or %<)%>");
28544             break;
28545           case RT_PRAGMA_EOL:
28546             gmsgid = G_("expected end of line");
28547             break;
28548           case RT_NAME:
28549             gmsgid = G_("expected identifier");
28550             break;
28551           case RT_SELECT:
28552             gmsgid = G_("expected selection-statement");
28553             break;
28554           case RT_ITERATION:
28555             gmsgid = G_("expected iteration-statement");
28556             break;
28557           case RT_JUMP:
28558             gmsgid = G_("expected jump-statement");
28559             break;
28560           case RT_CLASS_KEY:
28561             gmsgid = G_("expected class-key");
28562             break;
28563           case RT_CLASS_TYPENAME_TEMPLATE:
28564             gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28565             break;
28566           default:
28567             gcc_unreachable ();
28568         }
28569     }
28570
28571   if (gmsgid)
28572     cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28573 }
28574
28575
28576 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
28577    issue an error message indicating that TOKEN_DESC was expected.
28578
28579    Returns the token consumed, if the token had the appropriate type.
28580    Otherwise, returns NULL.
28581
28582    If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28583    within any error as the location of an "opening" token matching
28584    the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28585    RT_CLOSE_PAREN).  */
28586
28587 static cp_token *
28588 cp_parser_require (cp_parser* parser,
28589                    enum cpp_ttype type,
28590                    required_token token_desc,
28591                    location_t matching_location)
28592 {
28593   if (cp_lexer_next_token_is (parser->lexer, type))
28594     return cp_lexer_consume_token (parser->lexer);
28595   else
28596     {
28597       /* Output the MESSAGE -- unless we're parsing tentatively.  */
28598       if (!cp_parser_simulate_error (parser))
28599         cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28600                                   matching_location);
28601       return NULL;
28602     }
28603 }
28604
28605 /* An error message is produced if the next token is not '>'.
28606    All further tokens are skipped until the desired token is
28607    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
28608
28609 static void
28610 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28611 {
28612   /* Current level of '< ... >'.  */
28613   unsigned level = 0;
28614   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
28615   unsigned nesting_depth = 0;
28616
28617   /* Are we ready, yet?  If not, issue error message.  */
28618   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28619     return;
28620
28621   /* Skip tokens until the desired token is found.  */
28622   while (true)
28623     {
28624       /* Peek at the next token.  */
28625       switch (cp_lexer_peek_token (parser->lexer)->type)
28626         {
28627         case CPP_LESS:
28628           if (!nesting_depth)
28629             ++level;
28630           break;
28631
28632         case CPP_RSHIFT:
28633           if (cxx_dialect == cxx98)
28634             /* C++0x views the `>>' operator as two `>' tokens, but
28635                C++98 does not. */
28636             break;
28637           else if (!nesting_depth && level-- == 0)
28638             {
28639               /* We've hit a `>>' where the first `>' closes the
28640                  template argument list, and the second `>' is
28641                  spurious.  Just consume the `>>' and stop; we've
28642                  already produced at least one error.  */
28643               cp_lexer_consume_token (parser->lexer);
28644               return;
28645             }
28646           /* Fall through for C++0x, so we handle the second `>' in
28647              the `>>'.  */
28648           gcc_fallthrough ();
28649
28650         case CPP_GREATER:
28651           if (!nesting_depth && level-- == 0)
28652             {
28653               /* We've reached the token we want, consume it and stop.  */
28654               cp_lexer_consume_token (parser->lexer);
28655               return;
28656             }
28657           break;
28658
28659         case CPP_OPEN_PAREN:
28660         case CPP_OPEN_SQUARE:
28661           ++nesting_depth;
28662           break;
28663
28664         case CPP_CLOSE_PAREN:
28665         case CPP_CLOSE_SQUARE:
28666           if (nesting_depth-- == 0)
28667             return;
28668           break;
28669
28670         case CPP_EOF:
28671         case CPP_PRAGMA_EOL:
28672         case CPP_SEMICOLON:
28673         case CPP_OPEN_BRACE:
28674         case CPP_CLOSE_BRACE:
28675           /* The '>' was probably forgotten, don't look further.  */
28676           return;
28677
28678         default:
28679           break;
28680         }
28681
28682       /* Consume this token.  */
28683       cp_lexer_consume_token (parser->lexer);
28684     }
28685 }
28686
28687 /* If the next token is the indicated keyword, 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 static cp_token *
28694 cp_parser_require_keyword (cp_parser* parser,
28695                            enum rid keyword,
28696                            required_token token_desc)
28697 {
28698   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28699
28700   if (token && token->keyword != keyword)
28701     {
28702       cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28703                                 UNKNOWN_LOCATION);
28704       return NULL;
28705     }
28706
28707   return token;
28708 }
28709
28710 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28711    function-definition.  */
28712
28713 static bool
28714 cp_parser_token_starts_function_definition_p (cp_token* token)
28715 {
28716   return (/* An ordinary function-body begins with an `{'.  */
28717           token->type == CPP_OPEN_BRACE
28718           /* A ctor-initializer begins with a `:'.  */
28719           || token->type == CPP_COLON
28720           /* A function-try-block begins with `try'.  */
28721           || token->keyword == RID_TRY
28722           /* A function-transaction-block begins with `__transaction_atomic'
28723              or `__transaction_relaxed'.  */
28724           || token->keyword == RID_TRANSACTION_ATOMIC
28725           || token->keyword == RID_TRANSACTION_RELAXED
28726           /* The named return value extension begins with `return'.  */
28727           || token->keyword == RID_RETURN);
28728 }
28729
28730 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28731    definition.  */
28732
28733 static bool
28734 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28735 {
28736   cp_token *token;
28737
28738   token = cp_lexer_peek_token (parser->lexer);
28739   return (token->type == CPP_OPEN_BRACE
28740           || (token->type == CPP_COLON
28741               && !parser->colon_doesnt_start_class_def_p));
28742 }
28743
28744 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28745    C++0x) ending a template-argument.  */
28746
28747 static bool
28748 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28749 {
28750   cp_token *token;
28751
28752   token = cp_lexer_peek_token (parser->lexer);
28753   return (token->type == CPP_COMMA 
28754           || token->type == CPP_GREATER
28755           || token->type == CPP_ELLIPSIS
28756           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28757 }
28758
28759 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28760    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
28761
28762 static bool
28763 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28764                                                      size_t n)
28765 {
28766   cp_token *token;
28767
28768   token = cp_lexer_peek_nth_token (parser->lexer, n);
28769   if (token->type == CPP_LESS)
28770     return true;
28771   /* Check for the sequence `<::' in the original code. It would be lexed as
28772      `[:', where `[' is a digraph, and there is no whitespace before
28773      `:'.  */
28774   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28775     {
28776       cp_token *token2;
28777       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28778       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28779         return true;
28780     }
28781   return false;
28782 }
28783
28784 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28785    or none_type otherwise.  */
28786
28787 static enum tag_types
28788 cp_parser_token_is_class_key (cp_token* token)
28789 {
28790   switch (token->keyword)
28791     {
28792     case RID_CLASS:
28793       return class_type;
28794     case RID_STRUCT:
28795       return record_type;
28796     case RID_UNION:
28797       return union_type;
28798
28799     default:
28800       return none_type;
28801     }
28802 }
28803
28804 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28805    or none_type otherwise or if the token is null.  */
28806
28807 static enum tag_types
28808 cp_parser_token_is_type_parameter_key (cp_token* token)
28809 {
28810   if (!token)
28811     return none_type;
28812
28813   switch (token->keyword)
28814     {
28815     case RID_CLASS:
28816       return class_type;
28817     case RID_TYPENAME:
28818       return typename_type;
28819
28820     default:
28821       return none_type;
28822     }
28823 }
28824
28825 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
28826
28827 static void
28828 cp_parser_check_class_key (enum tag_types class_key, tree type)
28829 {
28830   if (type == error_mark_node)
28831     return;
28832   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28833     {
28834       if (permerror (input_location, "%qs tag used in naming %q#T",
28835                      class_key == union_type ? "union"
28836                      : class_key == record_type ? "struct" : "class",
28837                      type))
28838         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28839                 "%q#T was previously declared here", type);
28840     }
28841 }
28842
28843 /* Issue an error message if DECL is redeclared with different
28844    access than its original declaration [class.access.spec/3].
28845    This applies to nested classes, nested class templates and
28846    enumerations [class.mem/1].  */
28847
28848 static void
28849 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28850 {
28851   if (!decl
28852       || (!CLASS_TYPE_P (TREE_TYPE (decl))
28853           && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28854     return;
28855
28856   if ((TREE_PRIVATE (decl)
28857        != (current_access_specifier == access_private_node))
28858       || (TREE_PROTECTED (decl)
28859           != (current_access_specifier == access_protected_node)))
28860     error_at (location, "%qD redeclared with different access", decl);
28861 }
28862
28863 /* Look for the `template' keyword, as a syntactic disambiguator.
28864    Return TRUE iff it is present, in which case it will be
28865    consumed.  */
28866
28867 static bool
28868 cp_parser_optional_template_keyword (cp_parser *parser)
28869 {
28870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28871     {
28872       /* In C++98 the `template' keyword can only be used within templates;
28873          outside templates the parser can always figure out what is a
28874          template and what is not.  In C++11,  per the resolution of DR 468,
28875          `template' is allowed in cases where it is not strictly necessary.  */
28876       if (!processing_template_decl
28877           && pedantic && cxx_dialect == cxx98)
28878         {
28879           cp_token *token = cp_lexer_peek_token (parser->lexer);
28880           pedwarn (token->location, OPT_Wpedantic,
28881                    "in C++98 %<template%> (as a disambiguator) is only "
28882                    "allowed within templates");
28883           /* If this part of the token stream is rescanned, the same
28884              error message would be generated.  So, we purge the token
28885              from the stream.  */
28886           cp_lexer_purge_token (parser->lexer);
28887           return false;
28888         }
28889       else
28890         {
28891           /* Consume the `template' keyword.  */
28892           cp_lexer_consume_token (parser->lexer);
28893           return true;
28894         }
28895     }
28896   return false;
28897 }
28898
28899 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
28900    set PARSER->SCOPE, and perform other related actions.  */
28901
28902 static void
28903 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28904 {
28905   struct tree_check *check_value;
28906
28907   /* Get the stored value.  */
28908   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28909   /* Set the scope from the stored value.  */
28910   parser->scope = saved_checks_value (check_value);
28911   parser->qualifying_scope = check_value->qualifying_scope;
28912   parser->object_scope = NULL_TREE;
28913 }
28914
28915 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
28916    encounter the end of a block before what we were looking for.  */
28917
28918 static bool
28919 cp_parser_cache_group (cp_parser *parser,
28920                        enum cpp_ttype end,
28921                        unsigned depth)
28922 {
28923   while (true)
28924     {
28925       cp_token *token = cp_lexer_peek_token (parser->lexer);
28926
28927       /* Abort a parenthesized expression if we encounter a semicolon.  */
28928       if ((end == CPP_CLOSE_PAREN || depth == 0)
28929           && token->type == CPP_SEMICOLON)
28930         return true;
28931       /* If we've reached the end of the file, stop.  */
28932       if (token->type == CPP_EOF
28933           || (end != CPP_PRAGMA_EOL
28934               && token->type == CPP_PRAGMA_EOL))
28935         return true;
28936       if (token->type == CPP_CLOSE_BRACE && depth == 0)
28937         /* We've hit the end of an enclosing block, so there's been some
28938            kind of syntax error.  */
28939         return true;
28940
28941       /* Consume the token.  */
28942       cp_lexer_consume_token (parser->lexer);
28943       /* See if it starts a new group.  */
28944       if (token->type == CPP_OPEN_BRACE)
28945         {
28946           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28947           /* In theory this should probably check end == '}', but
28948              cp_parser_save_member_function_body needs it to exit
28949              after either '}' or ')' when called with ')'.  */
28950           if (depth == 0)
28951             return false;
28952         }
28953       else if (token->type == CPP_OPEN_PAREN)
28954         {
28955           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28956           if (depth == 0 && end == CPP_CLOSE_PAREN)
28957             return false;
28958         }
28959       else if (token->type == CPP_PRAGMA)
28960         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28961       else if (token->type == end)
28962         return false;
28963     }
28964 }
28965
28966 /* Like above, for caching a default argument or NSDMI.  Both of these are
28967    terminated by a non-nested comma, but it can be unclear whether or not a
28968    comma is nested in a template argument list unless we do more parsing.
28969    In order to handle this ambiguity, when we encounter a ',' after a '<'
28970    we try to parse what follows as a parameter-declaration-list (in the
28971    case of a default argument) or a member-declarator (in the case of an
28972    NSDMI).  If that succeeds, then we stop caching.  */
28973
28974 static tree
28975 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28976 {
28977   unsigned depth = 0;
28978   int maybe_template_id = 0;
28979   cp_token *first_token;
28980   cp_token *token;
28981   tree default_argument;
28982
28983   /* Add tokens until we have processed the entire default
28984      argument.  We add the range [first_token, token).  */
28985   first_token = cp_lexer_peek_token (parser->lexer);
28986   if (first_token->type == CPP_OPEN_BRACE)
28987     {
28988       /* For list-initialization, this is straightforward.  */
28989       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28990       token = cp_lexer_peek_token (parser->lexer);
28991     }
28992   else while (true)
28993     {
28994       bool done = false;
28995
28996       /* Peek at the next token.  */
28997       token = cp_lexer_peek_token (parser->lexer);
28998       /* What we do depends on what token we have.  */
28999       switch (token->type)
29000         {
29001           /* In valid code, a default argument must be
29002              immediately followed by a `,' `)', or `...'.  */
29003         case CPP_COMMA:
29004           if (depth == 0 && maybe_template_id)
29005             {
29006               /* If we've seen a '<', we might be in a
29007                  template-argument-list.  Until Core issue 325 is
29008                  resolved, we don't know how this situation ought
29009                  to be handled, so try to DTRT.  We check whether
29010                  what comes after the comma is a valid parameter
29011                  declaration list.  If it is, then the comma ends
29012                  the default argument; otherwise the default
29013                  argument continues.  */
29014               bool error = false;
29015               cp_token *peek;
29016
29017               /* Set ITALP so cp_parser_parameter_declaration_list
29018                  doesn't decide to commit to this parse.  */
29019               bool saved_italp = parser->in_template_argument_list_p;
29020               parser->in_template_argument_list_p = true;
29021
29022               cp_parser_parse_tentatively (parser);
29023
29024               if (nsdmi)
29025                 {
29026                   /* Parse declarators until we reach a non-comma or
29027                      somthing that cannot be an initializer.
29028                      Just checking whether we're looking at a single
29029                      declarator is insufficient.  Consider:
29030                        int var = tuple<T,U>::x;
29031                      The template parameter 'U' looks exactly like a
29032                      declarator.  */
29033                   do
29034                     {
29035                       int ctor_dtor_or_conv_p;
29036                       cp_lexer_consume_token (parser->lexer);
29037                       cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29038                                             &ctor_dtor_or_conv_p,
29039                                             /*parenthesized_p=*/NULL,
29040                                             /*member_p=*/true,
29041                                             /*friend_p=*/false);
29042                       peek = cp_lexer_peek_token (parser->lexer);
29043                       if (cp_parser_error_occurred (parser))
29044                         break;
29045                     }
29046                   while (peek->type == CPP_COMMA);
29047                   /* If we met an '=' or ';' then the original comma
29048                      was the end of the NSDMI.  Otherwise assume
29049                      we're still in the NSDMI.  */
29050                   error = (peek->type != CPP_EQ
29051                            && peek->type != CPP_SEMICOLON);
29052                 }
29053               else
29054                 {
29055                   cp_lexer_consume_token (parser->lexer);
29056                   begin_scope (sk_function_parms, NULL_TREE);
29057                   cp_parser_parameter_declaration_list (parser, &error);
29058                   pop_bindings_and_leave_scope ();
29059                 }
29060               if (!cp_parser_error_occurred (parser) && !error)
29061                 done = true;
29062               cp_parser_abort_tentative_parse (parser);
29063
29064               parser->in_template_argument_list_p = saved_italp;
29065               break;
29066             }
29067           /* FALLTHRU */
29068         case CPP_CLOSE_PAREN:
29069         case CPP_ELLIPSIS:
29070           /* If we run into a non-nested `;', `}', or `]',
29071              then the code is invalid -- but the default
29072              argument is certainly over.  */
29073         case CPP_SEMICOLON:
29074         case CPP_CLOSE_BRACE:
29075         case CPP_CLOSE_SQUARE:
29076           if (depth == 0
29077               /* Handle correctly int n = sizeof ... ( p );  */
29078               && token->type != CPP_ELLIPSIS)
29079             done = true;
29080           /* Update DEPTH, if necessary.  */
29081           else if (token->type == CPP_CLOSE_PAREN
29082                    || token->type == CPP_CLOSE_BRACE
29083                    || token->type == CPP_CLOSE_SQUARE)
29084             --depth;
29085           break;
29086
29087         case CPP_OPEN_PAREN:
29088         case CPP_OPEN_SQUARE:
29089         case CPP_OPEN_BRACE:
29090           ++depth;
29091           break;
29092
29093         case CPP_LESS:
29094           if (depth == 0)
29095             /* This might be the comparison operator, or it might
29096                start a template argument list.  */
29097             ++maybe_template_id;
29098           break;
29099
29100         case CPP_RSHIFT:
29101           if (cxx_dialect == cxx98)
29102             break;
29103           /* Fall through for C++0x, which treats the `>>'
29104              operator like two `>' tokens in certain
29105              cases.  */
29106           gcc_fallthrough ();
29107
29108         case CPP_GREATER:
29109           if (depth == 0)
29110             {
29111               /* This might be an operator, or it might close a
29112                  template argument list.  But if a previous '<'
29113                  started a template argument list, this will have
29114                  closed it, so we can't be in one anymore.  */
29115               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29116               if (maybe_template_id < 0)
29117                 maybe_template_id = 0;
29118             }
29119           break;
29120
29121           /* If we run out of tokens, issue an error message.  */
29122         case CPP_EOF:
29123         case CPP_PRAGMA_EOL:
29124           error_at (token->location, "file ends in default argument");
29125           return error_mark_node;
29126
29127         case CPP_NAME:
29128         case CPP_SCOPE:
29129           /* In these cases, we should look for template-ids.
29130              For example, if the default argument is
29131              `X<int, double>()', we need to do name lookup to
29132              figure out whether or not `X' is a template; if
29133              so, the `,' does not end the default argument.
29134
29135              That is not yet done.  */
29136           break;
29137
29138         default:
29139           break;
29140         }
29141
29142       /* If we've reached the end, stop.  */
29143       if (done)
29144         break;
29145
29146       /* Add the token to the token block.  */
29147       token = cp_lexer_consume_token (parser->lexer);
29148     }
29149
29150   /* Create a DEFAULT_ARG to represent the unparsed default
29151      argument.  */
29152   default_argument = make_node (DEFAULT_ARG);
29153   DEFARG_TOKENS (default_argument)
29154     = cp_token_cache_new (first_token, token);
29155   DEFARG_INSTANTIATIONS (default_argument) = NULL;
29156
29157   return default_argument;
29158 }
29159
29160 /* A location to use for diagnostics about an unparsed DEFAULT_ARG.  */
29161
29162 location_t
29163 defarg_location (tree default_argument)
29164 {
29165   cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29166   location_t start = tokens->first->location;
29167   location_t end = tokens->last->location;
29168   return make_location (start, start, end);
29169 }
29170
29171 /* Begin parsing tentatively.  We always save tokens while parsing
29172    tentatively so that if the tentative parsing fails we can restore the
29173    tokens.  */
29174
29175 static void
29176 cp_parser_parse_tentatively (cp_parser* parser)
29177 {
29178   /* Enter a new parsing context.  */
29179   parser->context = cp_parser_context_new (parser->context);
29180   /* Begin saving tokens.  */
29181   cp_lexer_save_tokens (parser->lexer);
29182   /* In order to avoid repetitive access control error messages,
29183      access checks are queued up until we are no longer parsing
29184      tentatively.  */
29185   push_deferring_access_checks (dk_deferred);
29186 }
29187
29188 /* Commit to the currently active tentative parse.  */
29189
29190 static void
29191 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29192 {
29193   cp_parser_context *context;
29194   cp_lexer *lexer;
29195
29196   /* Mark all of the levels as committed.  */
29197   lexer = parser->lexer;
29198   for (context = parser->context; context->next; context = context->next)
29199     {
29200       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29201         break;
29202       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29203       while (!cp_lexer_saving_tokens (lexer))
29204         lexer = lexer->next;
29205       cp_lexer_commit_tokens (lexer);
29206     }
29207 }
29208
29209 /* Commit to the topmost currently active tentative parse.
29210
29211    Note that this function shouldn't be called when there are
29212    irreversible side-effects while in a tentative state.  For
29213    example, we shouldn't create a permanent entry in the symbol
29214    table, or issue an error message that might not apply if the
29215    tentative parse is aborted.  */
29216
29217 static void
29218 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29219 {
29220   cp_parser_context *context = parser->context;
29221   cp_lexer *lexer = parser->lexer;
29222
29223   if (context)
29224     {
29225       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29226         return;
29227       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29228
29229       while (!cp_lexer_saving_tokens (lexer))
29230         lexer = lexer->next;
29231       cp_lexer_commit_tokens (lexer);
29232     }
29233 }
29234
29235 /* Abort the currently active tentative parse.  All consumed tokens
29236    will be rolled back, and no diagnostics will be issued.  */
29237
29238 static void
29239 cp_parser_abort_tentative_parse (cp_parser* parser)
29240 {
29241   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29242               || errorcount > 0);
29243   cp_parser_simulate_error (parser);
29244   /* Now, pretend that we want to see if the construct was
29245      successfully parsed.  */
29246   cp_parser_parse_definitely (parser);
29247 }
29248
29249 /* Stop parsing tentatively.  If a parse error has occurred, restore the
29250    token stream.  Otherwise, commit to the tokens we have consumed.
29251    Returns true if no error occurred; false otherwise.  */
29252
29253 static bool
29254 cp_parser_parse_definitely (cp_parser* parser)
29255 {
29256   bool error_occurred;
29257   cp_parser_context *context;
29258
29259   /* Remember whether or not an error occurred, since we are about to
29260      destroy that information.  */
29261   error_occurred = cp_parser_error_occurred (parser);
29262   /* Remove the topmost context from the stack.  */
29263   context = parser->context;
29264   parser->context = context->next;
29265   /* If no parse errors occurred, commit to the tentative parse.  */
29266   if (!error_occurred)
29267     {
29268       /* Commit to the tokens read tentatively, unless that was
29269          already done.  */
29270       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29271         cp_lexer_commit_tokens (parser->lexer);
29272
29273       pop_to_parent_deferring_access_checks ();
29274     }
29275   /* Otherwise, if errors occurred, roll back our state so that things
29276      are just as they were before we began the tentative parse.  */
29277   else
29278     {
29279       cp_lexer_rollback_tokens (parser->lexer);
29280       pop_deferring_access_checks ();
29281     }
29282   /* Add the context to the front of the free list.  */
29283   context->next = cp_parser_context_free_list;
29284   cp_parser_context_free_list = context;
29285
29286   return !error_occurred;
29287 }
29288
29289 /* Returns true if we are parsing tentatively and are not committed to
29290    this tentative parse.  */
29291
29292 static bool
29293 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29294 {
29295   return (cp_parser_parsing_tentatively (parser)
29296           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29297 }
29298
29299 /* Returns nonzero iff an error has occurred during the most recent
29300    tentative parse.  */
29301
29302 static bool
29303 cp_parser_error_occurred (cp_parser* parser)
29304 {
29305   return (cp_parser_parsing_tentatively (parser)
29306           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29307 }
29308
29309 /* Returns nonzero if GNU extensions are allowed.  */
29310
29311 static bool
29312 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29313 {
29314   return parser->allow_gnu_extensions_p;
29315 }
29316 \f
29317 /* Objective-C++ Productions */
29318
29319
29320 /* Parse an Objective-C expression, which feeds into a primary-expression
29321    above.
29322
29323    objc-expression:
29324      objc-message-expression
29325      objc-string-literal
29326      objc-encode-expression
29327      objc-protocol-expression
29328      objc-selector-expression
29329
29330   Returns a tree representation of the expression.  */
29331
29332 static cp_expr
29333 cp_parser_objc_expression (cp_parser* parser)
29334 {
29335   /* Try to figure out what kind of declaration is present.  */
29336   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29337
29338   switch (kwd->type)
29339     {
29340     case CPP_OPEN_SQUARE:
29341       return cp_parser_objc_message_expression (parser);
29342
29343     case CPP_OBJC_STRING:
29344       kwd = cp_lexer_consume_token (parser->lexer);
29345       return objc_build_string_object (kwd->u.value);
29346
29347     case CPP_KEYWORD:
29348       switch (kwd->keyword)
29349         {
29350         case RID_AT_ENCODE:
29351           return cp_parser_objc_encode_expression (parser);
29352
29353         case RID_AT_PROTOCOL:
29354           return cp_parser_objc_protocol_expression (parser);
29355
29356         case RID_AT_SELECTOR:
29357           return cp_parser_objc_selector_expression (parser);
29358
29359         default:
29360           break;
29361         }
29362       /* FALLTHRU */
29363     default:
29364       error_at (kwd->location,
29365                 "misplaced %<@%D%> Objective-C++ construct",
29366                 kwd->u.value);
29367       cp_parser_skip_to_end_of_block_or_statement (parser);
29368     }
29369
29370   return error_mark_node;
29371 }
29372
29373 /* Parse an Objective-C message expression.
29374
29375    objc-message-expression:
29376      [ objc-message-receiver objc-message-args ]
29377
29378    Returns a representation of an Objective-C message.  */
29379
29380 static tree
29381 cp_parser_objc_message_expression (cp_parser* parser)
29382 {
29383   tree receiver, messageargs;
29384
29385   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29386   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
29387   receiver = cp_parser_objc_message_receiver (parser);
29388   messageargs = cp_parser_objc_message_args (parser);
29389   location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29390   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29391
29392   tree result = objc_build_message_expr (receiver, messageargs);
29393
29394   /* Construct a location e.g.
29395        [self func1:5]
29396        ^~~~~~~~~~~~~~
29397      ranging from the '[' to the ']', with the caret at the start.  */
29398   location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29399   protected_set_expr_location (result, combined_loc);
29400
29401   return result;
29402 }
29403
29404 /* Parse an objc-message-receiver.
29405
29406    objc-message-receiver:
29407      expression
29408      simple-type-specifier
29409
29410   Returns a representation of the type or expression.  */
29411
29412 static tree
29413 cp_parser_objc_message_receiver (cp_parser* parser)
29414 {
29415   tree rcv;
29416
29417   /* An Objective-C message receiver may be either (1) a type
29418      or (2) an expression.  */
29419   cp_parser_parse_tentatively (parser);
29420   rcv = cp_parser_expression (parser);
29421
29422   /* If that worked out, fine.  */
29423   if (cp_parser_parse_definitely (parser))
29424     return rcv;
29425
29426   cp_parser_parse_tentatively (parser);
29427   rcv = cp_parser_simple_type_specifier (parser,
29428                                          /*decl_specs=*/NULL,
29429                                          CP_PARSER_FLAGS_NONE);
29430
29431   if (cp_parser_parse_definitely (parser))
29432     return objc_get_class_reference (rcv);
29433   
29434   cp_parser_error (parser, "objective-c++ message receiver expected");
29435   return error_mark_node;
29436 }
29437
29438 /* Parse the arguments and selectors comprising an Objective-C message.
29439
29440    objc-message-args:
29441      objc-selector
29442      objc-selector-args
29443      objc-selector-args , objc-comma-args
29444
29445    objc-selector-args:
29446      objc-selector [opt] : assignment-expression
29447      objc-selector-args objc-selector [opt] : assignment-expression
29448
29449    objc-comma-args:
29450      assignment-expression
29451      objc-comma-args , assignment-expression
29452
29453    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29454    selector arguments and TREE_VALUE containing a list of comma
29455    arguments.  */
29456
29457 static tree
29458 cp_parser_objc_message_args (cp_parser* parser)
29459 {
29460   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29461   bool maybe_unary_selector_p = true;
29462   cp_token *token = cp_lexer_peek_token (parser->lexer);
29463
29464   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29465     {
29466       tree selector = NULL_TREE, arg;
29467
29468       if (token->type != CPP_COLON)
29469         selector = cp_parser_objc_selector (parser);
29470
29471       /* Detect if we have a unary selector.  */
29472       if (maybe_unary_selector_p
29473           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29474         return build_tree_list (selector, NULL_TREE);
29475
29476       maybe_unary_selector_p = false;
29477       cp_parser_require (parser, CPP_COLON, RT_COLON);
29478       arg = cp_parser_assignment_expression (parser);
29479
29480       sel_args
29481         = chainon (sel_args,
29482                    build_tree_list (selector, arg));
29483
29484       token = cp_lexer_peek_token (parser->lexer);
29485     }
29486
29487   /* Handle non-selector arguments, if any. */
29488   while (token->type == CPP_COMMA)
29489     {
29490       tree arg;
29491
29492       cp_lexer_consume_token (parser->lexer);
29493       arg = cp_parser_assignment_expression (parser);
29494
29495       addl_args
29496         = chainon (addl_args,
29497                    build_tree_list (NULL_TREE, arg));
29498
29499       token = cp_lexer_peek_token (parser->lexer);
29500     }
29501
29502   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29503     {
29504       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29505       return build_tree_list (error_mark_node, error_mark_node);
29506     }
29507
29508   return build_tree_list (sel_args, addl_args);
29509 }
29510
29511 /* Parse an Objective-C encode expression.
29512
29513    objc-encode-expression:
29514      @encode objc-typename
29515
29516    Returns an encoded representation of the type argument.  */
29517
29518 static cp_expr
29519 cp_parser_objc_encode_expression (cp_parser* parser)
29520 {
29521   tree type;
29522   cp_token *token;
29523   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29524
29525   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
29526   matching_parens parens;
29527   parens.require_open (parser);
29528   token = cp_lexer_peek_token (parser->lexer);
29529   type = complete_type (cp_parser_type_id (parser));
29530   parens.require_close (parser);
29531
29532   if (!type)
29533     {
29534       error_at (token->location, 
29535                 "%<@encode%> must specify a type as an argument");
29536       return error_mark_node;
29537     }
29538
29539   /* This happens if we find @encode(T) (where T is a template
29540      typename or something dependent on a template typename) when
29541      parsing a template.  In that case, we can't compile it
29542      immediately, but we rather create an AT_ENCODE_EXPR which will
29543      need to be instantiated when the template is used.
29544   */
29545   if (dependent_type_p (type))
29546     {
29547       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29548       TREE_READONLY (value) = 1;
29549       return value;
29550     }
29551
29552
29553   /* Build a location of the form:
29554        @encode(int)
29555        ^~~~~~~~~~~~
29556      with caret==start at the @ token, finishing at the close paren.  */
29557   location_t combined_loc
29558     = make_location (start_loc, start_loc,
29559                      cp_lexer_previous_token (parser->lexer)->location);
29560
29561   return cp_expr (objc_build_encode_expr (type), combined_loc);
29562 }
29563
29564 /* Parse an Objective-C @defs expression.  */
29565
29566 static tree
29567 cp_parser_objc_defs_expression (cp_parser *parser)
29568 {
29569   tree name;
29570
29571   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
29572   matching_parens parens;
29573   parens.require_open (parser);
29574   name = cp_parser_identifier (parser);
29575   parens.require_close (parser);
29576
29577   return objc_get_class_ivars (name);
29578 }
29579
29580 /* Parse an Objective-C protocol expression.
29581
29582   objc-protocol-expression:
29583     @protocol ( identifier )
29584
29585   Returns a representation of the protocol expression.  */
29586
29587 static tree
29588 cp_parser_objc_protocol_expression (cp_parser* parser)
29589 {
29590   tree proto;
29591   location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29592
29593   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
29594   matching_parens parens;
29595   parens.require_open (parser);
29596   proto = cp_parser_identifier (parser);
29597   parens.require_close (parser);
29598
29599   /* Build a location of the form:
29600        @protocol(prot)
29601        ^~~~~~~~~~~~~~~
29602      with caret==start at the @ token, finishing at the close paren.  */
29603   location_t combined_loc
29604     = make_location (start_loc, start_loc,
29605                      cp_lexer_previous_token (parser->lexer)->location);
29606   tree result = objc_build_protocol_expr (proto);
29607   protected_set_expr_location (result, combined_loc);
29608   return result;
29609 }
29610
29611 /* Parse an Objective-C selector expression.
29612
29613    objc-selector-expression:
29614      @selector ( objc-method-signature )
29615
29616    objc-method-signature:
29617      objc-selector
29618      objc-selector-seq
29619
29620    objc-selector-seq:
29621      objc-selector :
29622      objc-selector-seq objc-selector :
29623
29624   Returns a representation of the method selector.  */
29625
29626 static tree
29627 cp_parser_objc_selector_expression (cp_parser* parser)
29628 {
29629   tree sel_seq = NULL_TREE;
29630   bool maybe_unary_selector_p = true;
29631   cp_token *token;
29632   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29633
29634   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
29635   matching_parens parens;
29636   parens.require_open (parser);
29637   token = cp_lexer_peek_token (parser->lexer);
29638
29639   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29640          || token->type == CPP_SCOPE)
29641     {
29642       tree selector = NULL_TREE;
29643
29644       if (token->type != CPP_COLON
29645           || token->type == CPP_SCOPE)
29646         selector = cp_parser_objc_selector (parser);
29647
29648       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29649           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29650         {
29651           /* Detect if we have a unary selector.  */
29652           if (maybe_unary_selector_p)
29653             {
29654               sel_seq = selector;
29655               goto finish_selector;
29656             }
29657           else
29658             {
29659               cp_parser_error (parser, "expected %<:%>");
29660             }
29661         }
29662       maybe_unary_selector_p = false;
29663       token = cp_lexer_consume_token (parser->lexer);
29664
29665       if (token->type == CPP_SCOPE)
29666         {
29667           sel_seq
29668             = chainon (sel_seq,
29669                        build_tree_list (selector, NULL_TREE));
29670           sel_seq
29671             = chainon (sel_seq,
29672                        build_tree_list (NULL_TREE, NULL_TREE));
29673         }
29674       else
29675         sel_seq
29676           = chainon (sel_seq,
29677                      build_tree_list (selector, NULL_TREE));
29678
29679       token = cp_lexer_peek_token (parser->lexer);
29680     }
29681
29682  finish_selector:
29683   parens.require_close (parser);
29684
29685
29686   /* Build a location of the form:
29687        @selector(func)
29688        ^~~~~~~~~~~~~~~
29689      with caret==start at the @ token, finishing at the close paren.  */
29690   location_t combined_loc
29691     = make_location (loc, loc,
29692                      cp_lexer_previous_token (parser->lexer)->location);
29693   tree result = objc_build_selector_expr (combined_loc, sel_seq);
29694   /* TODO: objc_build_selector_expr doesn't always honor the location.  */
29695   protected_set_expr_location (result, combined_loc);
29696   return result;
29697 }
29698
29699 /* Parse a list of identifiers.
29700
29701    objc-identifier-list:
29702      identifier
29703      objc-identifier-list , identifier
29704
29705    Returns a TREE_LIST of identifier nodes.  */
29706
29707 static tree
29708 cp_parser_objc_identifier_list (cp_parser* parser)
29709 {
29710   tree identifier;
29711   tree list;
29712   cp_token *sep;
29713
29714   identifier = cp_parser_identifier (parser);
29715   if (identifier == error_mark_node)
29716     return error_mark_node;      
29717
29718   list = build_tree_list (NULL_TREE, identifier);
29719   sep = cp_lexer_peek_token (parser->lexer);
29720
29721   while (sep->type == CPP_COMMA)
29722     {
29723       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
29724       identifier = cp_parser_identifier (parser);
29725       if (identifier == error_mark_node)
29726         return list;
29727
29728       list = chainon (list, build_tree_list (NULL_TREE,
29729                                              identifier));
29730       sep = cp_lexer_peek_token (parser->lexer);
29731     }
29732   
29733   return list;
29734 }
29735
29736 /* Parse an Objective-C alias declaration.
29737
29738    objc-alias-declaration:
29739      @compatibility_alias identifier identifier ;
29740
29741    This function registers the alias mapping with the Objective-C front end.
29742    It returns nothing.  */
29743
29744 static void
29745 cp_parser_objc_alias_declaration (cp_parser* parser)
29746 {
29747   tree alias, orig;
29748
29749   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
29750   alias = cp_parser_identifier (parser);
29751   orig = cp_parser_identifier (parser);
29752   objc_declare_alias (alias, orig);
29753   cp_parser_consume_semicolon_at_end_of_statement (parser);
29754 }
29755
29756 /* Parse an Objective-C class forward-declaration.
29757
29758    objc-class-declaration:
29759      @class objc-identifier-list ;
29760
29761    The function registers the forward declarations with the Objective-C
29762    front end.  It returns nothing.  */
29763
29764 static void
29765 cp_parser_objc_class_declaration (cp_parser* parser)
29766 {
29767   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
29768   while (true)
29769     {
29770       tree id;
29771       
29772       id = cp_parser_identifier (parser);
29773       if (id == error_mark_node)
29774         break;
29775       
29776       objc_declare_class (id);
29777
29778       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29779         cp_lexer_consume_token (parser->lexer);
29780       else
29781         break;
29782     }
29783   cp_parser_consume_semicolon_at_end_of_statement (parser);
29784 }
29785
29786 /* Parse a list of Objective-C protocol references.
29787
29788    objc-protocol-refs-opt:
29789      objc-protocol-refs [opt]
29790
29791    objc-protocol-refs:
29792      < objc-identifier-list >
29793
29794    Returns a TREE_LIST of identifiers, if any.  */
29795
29796 static tree
29797 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29798 {
29799   tree protorefs = NULL_TREE;
29800
29801   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29802     {
29803       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
29804       protorefs = cp_parser_objc_identifier_list (parser);
29805       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29806     }
29807
29808   return protorefs;
29809 }
29810
29811 /* Parse a Objective-C visibility specification.  */
29812
29813 static void
29814 cp_parser_objc_visibility_spec (cp_parser* parser)
29815 {
29816   cp_token *vis = cp_lexer_peek_token (parser->lexer);
29817
29818   switch (vis->keyword)
29819     {
29820     case RID_AT_PRIVATE:
29821       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29822       break;
29823     case RID_AT_PROTECTED:
29824       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29825       break;
29826     case RID_AT_PUBLIC:
29827       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29828       break;
29829     case RID_AT_PACKAGE:
29830       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29831       break;
29832     default:
29833       return;
29834     }
29835
29836   /* Eat '@private'/'@protected'/'@public'.  */
29837   cp_lexer_consume_token (parser->lexer);
29838 }
29839
29840 /* Parse an Objective-C method type.  Return 'true' if it is a class
29841    (+) method, and 'false' if it is an instance (-) method.  */
29842
29843 static inline bool
29844 cp_parser_objc_method_type (cp_parser* parser)
29845 {
29846   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29847     return true;
29848   else
29849     return false;
29850 }
29851
29852 /* Parse an Objective-C protocol qualifier.  */
29853
29854 static tree
29855 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29856 {
29857   tree quals = NULL_TREE, node;
29858   cp_token *token = cp_lexer_peek_token (parser->lexer);
29859
29860   node = token->u.value;
29861
29862   while (node && identifier_p (node)
29863          && (node == ridpointers [(int) RID_IN]
29864              || node == ridpointers [(int) RID_OUT]
29865              || node == ridpointers [(int) RID_INOUT]
29866              || node == ridpointers [(int) RID_BYCOPY]
29867              || node == ridpointers [(int) RID_BYREF]
29868              || node == ridpointers [(int) RID_ONEWAY]))
29869     {
29870       quals = tree_cons (NULL_TREE, node, quals);
29871       cp_lexer_consume_token (parser->lexer);
29872       token = cp_lexer_peek_token (parser->lexer);
29873       node = token->u.value;
29874     }
29875
29876   return quals;
29877 }
29878
29879 /* Parse an Objective-C typename.  */
29880
29881 static tree
29882 cp_parser_objc_typename (cp_parser* parser)
29883 {
29884   tree type_name = NULL_TREE;
29885
29886   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29887     {
29888       tree proto_quals, cp_type = NULL_TREE;
29889
29890       matching_parens parens;
29891       parens.consume_open (parser); /* Eat '('.  */
29892       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29893
29894       /* An ObjC type name may consist of just protocol qualifiers, in which
29895          case the type shall default to 'id'.  */
29896       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29897         {
29898           cp_type = cp_parser_type_id (parser);
29899           
29900           /* If the type could not be parsed, an error has already
29901              been produced.  For error recovery, behave as if it had
29902              not been specified, which will use the default type
29903              'id'.  */
29904           if (cp_type == error_mark_node)
29905             {
29906               cp_type = NULL_TREE;
29907               /* We need to skip to the closing parenthesis as
29908                  cp_parser_type_id() does not seem to do it for
29909                  us.  */
29910               cp_parser_skip_to_closing_parenthesis (parser,
29911                                                      /*recovering=*/true,
29912                                                      /*or_comma=*/false,
29913                                                      /*consume_paren=*/false);
29914             }
29915         }
29916
29917       parens.require_close (parser);
29918       type_name = build_tree_list (proto_quals, cp_type);
29919     }
29920
29921   return type_name;
29922 }
29923
29924 /* Check to see if TYPE refers to an Objective-C selector name.  */
29925
29926 static bool
29927 cp_parser_objc_selector_p (enum cpp_ttype type)
29928 {
29929   return (type == CPP_NAME || type == CPP_KEYWORD
29930           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29931           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29932           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29933           || type == CPP_XOR || type == CPP_XOR_EQ);
29934 }
29935
29936 /* Parse an Objective-C selector.  */
29937
29938 static tree
29939 cp_parser_objc_selector (cp_parser* parser)
29940 {
29941   cp_token *token = cp_lexer_consume_token (parser->lexer);
29942
29943   if (!cp_parser_objc_selector_p (token->type))
29944     {
29945       error_at (token->location, "invalid Objective-C++ selector name");
29946       return error_mark_node;
29947     }
29948
29949   /* C++ operator names are allowed to appear in ObjC selectors.  */
29950   switch (token->type)
29951     {
29952     case CPP_AND_AND: return get_identifier ("and");
29953     case CPP_AND_EQ: return get_identifier ("and_eq");
29954     case CPP_AND: return get_identifier ("bitand");
29955     case CPP_OR: return get_identifier ("bitor");
29956     case CPP_COMPL: return get_identifier ("compl");
29957     case CPP_NOT: return get_identifier ("not");
29958     case CPP_NOT_EQ: return get_identifier ("not_eq");
29959     case CPP_OR_OR: return get_identifier ("or");
29960     case CPP_OR_EQ: return get_identifier ("or_eq");
29961     case CPP_XOR: return get_identifier ("xor");
29962     case CPP_XOR_EQ: return get_identifier ("xor_eq");
29963     default: return token->u.value;
29964     }
29965 }
29966
29967 /* Parse an Objective-C params list.  */
29968
29969 static tree
29970 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29971 {
29972   tree params = NULL_TREE;
29973   bool maybe_unary_selector_p = true;
29974   cp_token *token = cp_lexer_peek_token (parser->lexer);
29975
29976   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29977     {
29978       tree selector = NULL_TREE, type_name, identifier;
29979       tree parm_attr = NULL_TREE;
29980
29981       if (token->keyword == RID_ATTRIBUTE)
29982         break;
29983
29984       if (token->type != CPP_COLON)
29985         selector = cp_parser_objc_selector (parser);
29986
29987       /* Detect if we have a unary selector.  */
29988       if (maybe_unary_selector_p
29989           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29990         {
29991           params = selector; /* Might be followed by attributes.  */
29992           break;
29993         }
29994
29995       maybe_unary_selector_p = false;
29996       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29997         {
29998           /* Something went quite wrong.  There should be a colon
29999              here, but there is not.  Stop parsing parameters.  */
30000           break;
30001         }
30002       type_name = cp_parser_objc_typename (parser);
30003       /* New ObjC allows attributes on parameters too.  */
30004       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30005         parm_attr = cp_parser_attributes_opt (parser);
30006       identifier = cp_parser_identifier (parser);
30007
30008       params
30009         = chainon (params,
30010                    objc_build_keyword_decl (selector,
30011                                             type_name,
30012                                             identifier,
30013                                             parm_attr));
30014
30015       token = cp_lexer_peek_token (parser->lexer);
30016     }
30017
30018   if (params == NULL_TREE)
30019     {
30020       cp_parser_error (parser, "objective-c++ method declaration is expected");
30021       return error_mark_node;
30022     }
30023
30024   /* We allow tail attributes for the method.  */
30025   if (token->keyword == RID_ATTRIBUTE)
30026     {
30027       *attributes = cp_parser_attributes_opt (parser);
30028       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30029           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30030         return params;
30031       cp_parser_error (parser, 
30032                        "method attributes must be specified at the end");
30033       return error_mark_node;
30034     }
30035
30036   if (params == NULL_TREE)
30037     {
30038       cp_parser_error (parser, "objective-c++ method declaration is expected");
30039       return error_mark_node;
30040     }
30041   return params;
30042 }
30043
30044 /* Parse the non-keyword Objective-C params.  */
30045
30046 static tree
30047 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
30048                                        tree* attributes)
30049 {
30050   tree params = make_node (TREE_LIST);
30051   cp_token *token = cp_lexer_peek_token (parser->lexer);
30052   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
30053
30054   while (token->type == CPP_COMMA)
30055     {
30056       cp_parameter_declarator *parmdecl;
30057       tree parm;
30058
30059       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30060       token = cp_lexer_peek_token (parser->lexer);
30061
30062       if (token->type == CPP_ELLIPSIS)
30063         {
30064           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
30065           *ellipsisp = true;
30066           token = cp_lexer_peek_token (parser->lexer);
30067           break;
30068         }
30069
30070       /* TODO: parse attributes for tail parameters.  */
30071       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30072       parm = grokdeclarator (parmdecl->declarator,
30073                              &parmdecl->decl_specifiers,
30074                              PARM, /*initialized=*/0,
30075                              /*attrlist=*/NULL);
30076
30077       chainon (params, build_tree_list (NULL_TREE, parm));
30078       token = cp_lexer_peek_token (parser->lexer);
30079     }
30080
30081   /* We allow tail attributes for the method.  */
30082   if (token->keyword == RID_ATTRIBUTE)
30083     {
30084       if (*attributes == NULL_TREE)
30085         {
30086           *attributes = cp_parser_attributes_opt (parser);
30087           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30088               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30089             return params;
30090         }
30091       else        
30092         /* We have an error, but parse the attributes, so that we can 
30093            carry on.  */
30094         *attributes = cp_parser_attributes_opt (parser);
30095
30096       cp_parser_error (parser, 
30097                        "method attributes must be specified at the end");
30098       return error_mark_node;
30099     }
30100
30101   return params;
30102 }
30103
30104 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
30105
30106 static void
30107 cp_parser_objc_interstitial_code (cp_parser* parser)
30108 {
30109   cp_token *token = cp_lexer_peek_token (parser->lexer);
30110
30111   /* If the next token is `extern' and the following token is a string
30112      literal, then we have a linkage specification.  */
30113   if (token->keyword == RID_EXTERN
30114       && cp_parser_is_pure_string_literal
30115          (cp_lexer_peek_nth_token (parser->lexer, 2)))
30116     cp_parser_linkage_specification (parser);
30117   /* Handle #pragma, if any.  */
30118   else if (token->type == CPP_PRAGMA)
30119     cp_parser_pragma (parser, pragma_objc_icode, NULL);
30120   /* Allow stray semicolons.  */
30121   else if (token->type == CPP_SEMICOLON)
30122     cp_lexer_consume_token (parser->lexer);
30123   /* Mark methods as optional or required, when building protocols.  */
30124   else if (token->keyword == RID_AT_OPTIONAL)
30125     {
30126       cp_lexer_consume_token (parser->lexer);
30127       objc_set_method_opt (true);
30128     }
30129   else if (token->keyword == RID_AT_REQUIRED)
30130     {
30131       cp_lexer_consume_token (parser->lexer);
30132       objc_set_method_opt (false);
30133     }
30134   else if (token->keyword == RID_NAMESPACE)
30135     cp_parser_namespace_definition (parser);
30136   /* Other stray characters must generate errors.  */
30137   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30138     {
30139       cp_lexer_consume_token (parser->lexer);
30140       error ("stray %qs between Objective-C++ methods",
30141              token->type == CPP_OPEN_BRACE ? "{" : "}");
30142     }
30143   /* Finally, try to parse a block-declaration, or a function-definition.  */
30144   else
30145     cp_parser_block_declaration (parser, /*statement_p=*/false);
30146 }
30147
30148 /* Parse a method signature.  */
30149
30150 static tree
30151 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30152 {
30153   tree rettype, kwdparms, optparms;
30154   bool ellipsis = false;
30155   bool is_class_method;
30156
30157   is_class_method = cp_parser_objc_method_type (parser);
30158   rettype = cp_parser_objc_typename (parser);
30159   *attributes = NULL_TREE;
30160   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30161   if (kwdparms == error_mark_node)
30162     return error_mark_node;
30163   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30164   if (optparms == error_mark_node)
30165     return error_mark_node;
30166
30167   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30168 }
30169
30170 static bool
30171 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30172 {
30173   tree tattr;  
30174   cp_lexer_save_tokens (parser->lexer);
30175   tattr = cp_parser_attributes_opt (parser);
30176   gcc_assert (tattr) ;
30177   
30178   /* If the attributes are followed by a method introducer, this is not allowed.
30179      Dump the attributes and flag the situation.  */
30180   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30181       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30182     return true;
30183
30184   /* Otherwise, the attributes introduce some interstitial code, possibly so
30185      rewind to allow that check.  */
30186   cp_lexer_rollback_tokens (parser->lexer);
30187   return false;  
30188 }
30189
30190 /* Parse an Objective-C method prototype list.  */
30191
30192 static void
30193 cp_parser_objc_method_prototype_list (cp_parser* parser)
30194 {
30195   cp_token *token = cp_lexer_peek_token (parser->lexer);
30196
30197   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30198     {
30199       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30200         {
30201           tree attributes, sig;
30202           bool is_class_method;
30203           if (token->type == CPP_PLUS)
30204             is_class_method = true;
30205           else
30206             is_class_method = false;
30207           sig = cp_parser_objc_method_signature (parser, &attributes);
30208           if (sig == error_mark_node)
30209             {
30210               cp_parser_skip_to_end_of_block_or_statement (parser);
30211               token = cp_lexer_peek_token (parser->lexer);
30212               continue;
30213             }
30214           objc_add_method_declaration (is_class_method, sig, attributes);
30215           cp_parser_consume_semicolon_at_end_of_statement (parser);
30216         }
30217       else if (token->keyword == RID_AT_PROPERTY)
30218         cp_parser_objc_at_property_declaration (parser);
30219       else if (token->keyword == RID_ATTRIBUTE 
30220                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30221         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
30222                     OPT_Wattributes, 
30223                     "prefix attributes are ignored for methods");
30224       else
30225         /* Allow for interspersed non-ObjC++ code.  */
30226         cp_parser_objc_interstitial_code (parser);
30227
30228       token = cp_lexer_peek_token (parser->lexer);
30229     }
30230
30231   if (token->type != CPP_EOF)
30232     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30233   else
30234     cp_parser_error (parser, "expected %<@end%>");
30235
30236   objc_finish_interface ();
30237 }
30238
30239 /* Parse an Objective-C method definition list.  */
30240
30241 static void
30242 cp_parser_objc_method_definition_list (cp_parser* parser)
30243 {
30244   cp_token *token = cp_lexer_peek_token (parser->lexer);
30245
30246   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30247     {
30248       tree meth;
30249
30250       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30251         {
30252           cp_token *ptk;
30253           tree sig, attribute;
30254           bool is_class_method;
30255           if (token->type == CPP_PLUS)
30256             is_class_method = true;
30257           else
30258             is_class_method = false;
30259           push_deferring_access_checks (dk_deferred);
30260           sig = cp_parser_objc_method_signature (parser, &attribute);
30261           if (sig == error_mark_node)
30262             {
30263               cp_parser_skip_to_end_of_block_or_statement (parser);
30264               token = cp_lexer_peek_token (parser->lexer);
30265               continue;
30266             }
30267           objc_start_method_definition (is_class_method, sig, attribute,
30268                                         NULL_TREE);
30269
30270           /* For historical reasons, we accept an optional semicolon.  */
30271           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30272             cp_lexer_consume_token (parser->lexer);
30273
30274           ptk = cp_lexer_peek_token (parser->lexer);
30275           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
30276                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30277             {
30278               perform_deferred_access_checks (tf_warning_or_error);
30279               stop_deferring_access_checks ();
30280               meth = cp_parser_function_definition_after_declarator (parser,
30281                                                                      false);
30282               pop_deferring_access_checks ();
30283               objc_finish_method_definition (meth);
30284             }
30285         }
30286       /* The following case will be removed once @synthesize is
30287          completely implemented.  */
30288       else if (token->keyword == RID_AT_PROPERTY)
30289         cp_parser_objc_at_property_declaration (parser);
30290       else if (token->keyword == RID_AT_SYNTHESIZE)
30291         cp_parser_objc_at_synthesize_declaration (parser);
30292       else if (token->keyword == RID_AT_DYNAMIC)
30293         cp_parser_objc_at_dynamic_declaration (parser);
30294       else if (token->keyword == RID_ATTRIBUTE 
30295                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30296         warning_at (token->location, OPT_Wattributes,
30297                     "prefix attributes are ignored for methods");
30298       else
30299         /* Allow for interspersed non-ObjC++ code.  */
30300         cp_parser_objc_interstitial_code (parser);
30301
30302       token = cp_lexer_peek_token (parser->lexer);
30303     }
30304
30305   if (token->type != CPP_EOF)
30306     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30307   else
30308     cp_parser_error (parser, "expected %<@end%>");
30309
30310   objc_finish_implementation ();
30311 }
30312
30313 /* Parse Objective-C ivars.  */
30314
30315 static void
30316 cp_parser_objc_class_ivars (cp_parser* parser)
30317 {
30318   cp_token *token = cp_lexer_peek_token (parser->lexer);
30319
30320   if (token->type != CPP_OPEN_BRACE)
30321     return;     /* No ivars specified.  */
30322
30323   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
30324   token = cp_lexer_peek_token (parser->lexer);
30325
30326   while (token->type != CPP_CLOSE_BRACE 
30327         && token->keyword != RID_AT_END && token->type != CPP_EOF)
30328     {
30329       cp_decl_specifier_seq declspecs;
30330       int decl_class_or_enum_p;
30331       tree prefix_attributes;
30332
30333       cp_parser_objc_visibility_spec (parser);
30334
30335       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30336         break;
30337
30338       cp_parser_decl_specifier_seq (parser,
30339                                     CP_PARSER_FLAGS_OPTIONAL,
30340                                     &declspecs,
30341                                     &decl_class_or_enum_p);
30342
30343       /* auto, register, static, extern, mutable.  */
30344       if (declspecs.storage_class != sc_none)
30345         {
30346           cp_parser_error (parser, "invalid type for instance variable");         
30347           declspecs.storage_class = sc_none;
30348         }
30349
30350       /* thread_local.  */
30351       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30352         {
30353           cp_parser_error (parser, "invalid type for instance variable");
30354           declspecs.locations[ds_thread] = 0;
30355         }
30356       
30357       /* typedef.  */
30358       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30359         {
30360           cp_parser_error (parser, "invalid type for instance variable");
30361           declspecs.locations[ds_typedef] = 0;
30362         }
30363
30364       prefix_attributes = declspecs.attributes;
30365       declspecs.attributes = NULL_TREE;
30366
30367       /* Keep going until we hit the `;' at the end of the
30368          declaration.  */
30369       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30370         {
30371           tree width = NULL_TREE, attributes, first_attribute, decl;
30372           cp_declarator *declarator = NULL;
30373           int ctor_dtor_or_conv_p;
30374
30375           /* Check for a (possibly unnamed) bitfield declaration.  */
30376           token = cp_lexer_peek_token (parser->lexer);
30377           if (token->type == CPP_COLON)
30378             goto eat_colon;
30379
30380           if (token->type == CPP_NAME
30381               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30382                   == CPP_COLON))
30383             {
30384               /* Get the name of the bitfield.  */
30385               declarator = make_id_declarator (NULL_TREE,
30386                                                cp_parser_identifier (parser),
30387                                                sfk_none);
30388
30389              eat_colon:
30390               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
30391               /* Get the width of the bitfield.  */
30392               width
30393                 = cp_parser_constant_expression (parser);
30394             }
30395           else
30396             {
30397               /* Parse the declarator.  */
30398               declarator
30399                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30400                                         &ctor_dtor_or_conv_p,
30401                                         /*parenthesized_p=*/NULL,
30402                                         /*member_p=*/false,
30403                                         /*friend_p=*/false);
30404             }
30405
30406           /* Look for attributes that apply to the ivar.  */
30407           attributes = cp_parser_attributes_opt (parser);
30408           /* Remember which attributes are prefix attributes and
30409              which are not.  */
30410           first_attribute = attributes;
30411           /* Combine the attributes.  */
30412           attributes = attr_chainon (prefix_attributes, attributes);
30413
30414           if (width)
30415             /* Create the bitfield declaration.  */
30416             decl = grokbitfield (declarator, &declspecs,
30417                                  width, NULL_TREE, attributes);
30418           else
30419             decl = grokfield (declarator, &declspecs,
30420                               NULL_TREE, /*init_const_expr_p=*/false,
30421                               NULL_TREE, attributes);
30422
30423           /* Add the instance variable.  */
30424           if (decl != error_mark_node && decl != NULL_TREE)
30425             objc_add_instance_variable (decl);
30426
30427           /* Reset PREFIX_ATTRIBUTES.  */
30428           if (attributes != error_mark_node)
30429             {
30430               while (attributes && TREE_CHAIN (attributes) != first_attribute)
30431                 attributes = TREE_CHAIN (attributes);
30432               if (attributes)
30433                 TREE_CHAIN (attributes) = NULL_TREE;
30434             }
30435
30436           token = cp_lexer_peek_token (parser->lexer);
30437
30438           if (token->type == CPP_COMMA)
30439             {
30440               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30441               continue;
30442             }
30443           break;
30444         }
30445
30446       cp_parser_consume_semicolon_at_end_of_statement (parser);
30447       token = cp_lexer_peek_token (parser->lexer);
30448     }
30449
30450   if (token->keyword == RID_AT_END)
30451     cp_parser_error (parser, "expected %<}%>");
30452
30453   /* Do not consume the RID_AT_END, so it will be read again as terminating
30454      the @interface of @implementation.  */ 
30455   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30456     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
30457     
30458   /* For historical reasons, we accept an optional semicolon.  */
30459   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30460     cp_lexer_consume_token (parser->lexer);
30461 }
30462
30463 /* Parse an Objective-C protocol declaration.  */
30464
30465 static void
30466 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30467 {
30468   tree proto, protorefs;
30469   cp_token *tok;
30470
30471   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
30472   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30473     {
30474       tok = cp_lexer_peek_token (parser->lexer);
30475       error_at (tok->location, "identifier expected after %<@protocol%>");
30476       cp_parser_consume_semicolon_at_end_of_statement (parser);
30477       return;
30478     }
30479
30480   /* See if we have a forward declaration or a definition.  */
30481   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30482
30483   /* Try a forward declaration first.  */
30484   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30485     {
30486       while (true)
30487         {
30488           tree id;
30489           
30490           id = cp_parser_identifier (parser);
30491           if (id == error_mark_node)
30492             break;
30493           
30494           objc_declare_protocol (id, attributes);
30495           
30496           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30497             cp_lexer_consume_token (parser->lexer);
30498           else
30499             break;
30500         }
30501       cp_parser_consume_semicolon_at_end_of_statement (parser);
30502     }
30503
30504   /* Ok, we got a full-fledged definition (or at least should).  */
30505   else
30506     {
30507       proto = cp_parser_identifier (parser);
30508       protorefs = cp_parser_objc_protocol_refs_opt (parser);
30509       objc_start_protocol (proto, protorefs, attributes);
30510       cp_parser_objc_method_prototype_list (parser);
30511     }
30512 }
30513
30514 /* Parse an Objective-C superclass or category.  */
30515
30516 static void
30517 cp_parser_objc_superclass_or_category (cp_parser *parser, 
30518                                        bool iface_p,
30519                                        tree *super,
30520                                        tree *categ, bool *is_class_extension)
30521 {
30522   cp_token *next = cp_lexer_peek_token (parser->lexer);
30523
30524   *super = *categ = NULL_TREE;
30525   *is_class_extension = false;
30526   if (next->type == CPP_COLON)
30527     {
30528       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
30529       *super = cp_parser_identifier (parser);
30530     }
30531   else if (next->type == CPP_OPEN_PAREN)
30532     {
30533       matching_parens parens;
30534       parens.consume_open (parser);  /* Eat '('.  */
30535
30536       /* If there is no category name, and this is an @interface, we
30537          have a class extension.  */
30538       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30539         {
30540           *categ = NULL_TREE;
30541           *is_class_extension = true;
30542         }
30543       else
30544         *categ = cp_parser_identifier (parser);
30545
30546       parens.require_close (parser);
30547     }
30548 }
30549
30550 /* Parse an Objective-C class interface.  */
30551
30552 static void
30553 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30554 {
30555   tree name, super, categ, protos;
30556   bool is_class_extension;
30557
30558   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
30559   name = cp_parser_identifier (parser);
30560   if (name == error_mark_node)
30561     {
30562       /* It's hard to recover because even if valid @interface stuff
30563          is to follow, we can't compile it (or validate it) if we
30564          don't even know which class it refers to.  Let's assume this
30565          was a stray '@interface' token in the stream and skip it.
30566       */
30567       return;
30568     }
30569   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30570                                          &is_class_extension);
30571   protos = cp_parser_objc_protocol_refs_opt (parser);
30572
30573   /* We have either a class or a category on our hands.  */
30574   if (categ || is_class_extension)
30575     objc_start_category_interface (name, categ, protos, attributes);
30576   else
30577     {
30578       objc_start_class_interface (name, super, protos, attributes);
30579       /* Handle instance variable declarations, if any.  */
30580       cp_parser_objc_class_ivars (parser);
30581       objc_continue_interface ();
30582     }
30583
30584   cp_parser_objc_method_prototype_list (parser);
30585 }
30586
30587 /* Parse an Objective-C class implementation.  */
30588
30589 static void
30590 cp_parser_objc_class_implementation (cp_parser* parser)
30591 {
30592   tree name, super, categ;
30593   bool is_class_extension;
30594
30595   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
30596   name = cp_parser_identifier (parser);
30597   if (name == error_mark_node)
30598     {
30599       /* It's hard to recover because even if valid @implementation
30600          stuff is to follow, we can't compile it (or validate it) if
30601          we don't even know which class it refers to.  Let's assume
30602          this was a stray '@implementation' token in the stream and
30603          skip it.
30604       */
30605       return;
30606     }
30607   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30608                                          &is_class_extension);
30609
30610   /* We have either a class or a category on our hands.  */
30611   if (categ)
30612     objc_start_category_implementation (name, categ);
30613   else
30614     {
30615       objc_start_class_implementation (name, super);
30616       /* Handle instance variable declarations, if any.  */
30617       cp_parser_objc_class_ivars (parser);
30618       objc_continue_implementation ();
30619     }
30620
30621   cp_parser_objc_method_definition_list (parser);
30622 }
30623
30624 /* Consume the @end token and finish off the implementation.  */
30625
30626 static void
30627 cp_parser_objc_end_implementation (cp_parser* parser)
30628 {
30629   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
30630   objc_finish_implementation ();
30631 }
30632
30633 /* Parse an Objective-C declaration.  */
30634
30635 static void
30636 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30637 {
30638   /* Try to figure out what kind of declaration is present.  */
30639   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30640
30641   if (attributes)
30642     switch (kwd->keyword)
30643       {
30644         case RID_AT_ALIAS:
30645         case RID_AT_CLASS:
30646         case RID_AT_END:
30647           error_at (kwd->location, "attributes may not be specified before"
30648                     " the %<@%D%> Objective-C++ keyword",
30649                     kwd->u.value);
30650           attributes = NULL;
30651           break;
30652         case RID_AT_IMPLEMENTATION:
30653           warning_at (kwd->location, OPT_Wattributes,
30654                       "prefix attributes are ignored before %<@%D%>",
30655                       kwd->u.value);
30656           attributes = NULL;
30657         default:
30658           break;
30659       }
30660
30661   switch (kwd->keyword)
30662     {
30663     case RID_AT_ALIAS:
30664       cp_parser_objc_alias_declaration (parser);
30665       break;
30666     case RID_AT_CLASS:
30667       cp_parser_objc_class_declaration (parser);
30668       break;
30669     case RID_AT_PROTOCOL:
30670       cp_parser_objc_protocol_declaration (parser, attributes);
30671       break;
30672     case RID_AT_INTERFACE:
30673       cp_parser_objc_class_interface (parser, attributes);
30674       break;
30675     case RID_AT_IMPLEMENTATION:
30676       cp_parser_objc_class_implementation (parser);
30677       break;
30678     case RID_AT_END:
30679       cp_parser_objc_end_implementation (parser);
30680       break;
30681     default:
30682       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30683                 kwd->u.value);
30684       cp_parser_skip_to_end_of_block_or_statement (parser);
30685     }
30686 }
30687
30688 /* Parse an Objective-C try-catch-finally statement.
30689
30690    objc-try-catch-finally-stmt:
30691      @try compound-statement objc-catch-clause-seq [opt]
30692        objc-finally-clause [opt]
30693
30694    objc-catch-clause-seq:
30695      objc-catch-clause objc-catch-clause-seq [opt]
30696
30697    objc-catch-clause:
30698      @catch ( objc-exception-declaration ) compound-statement
30699
30700    objc-finally-clause:
30701      @finally compound-statement
30702
30703    objc-exception-declaration:
30704      parameter-declaration
30705      '...'
30706
30707    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30708
30709    Returns NULL_TREE.
30710
30711    PS: This function is identical to c_parser_objc_try_catch_finally_statement
30712    for C.  Keep them in sync.  */   
30713
30714 static tree
30715 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30716 {
30717   location_t location;
30718   tree stmt;
30719
30720   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30721   location = cp_lexer_peek_token (parser->lexer)->location;
30722   objc_maybe_warn_exceptions (location);
30723   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30724      node, lest it get absorbed into the surrounding block.  */
30725   stmt = push_stmt_list ();
30726   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30727   objc_begin_try_stmt (location, pop_stmt_list (stmt));
30728
30729   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30730     {
30731       cp_parameter_declarator *parm;
30732       tree parameter_declaration = error_mark_node;
30733       bool seen_open_paren = false;
30734       matching_parens parens;
30735
30736       cp_lexer_consume_token (parser->lexer);
30737       if (parens.require_open (parser))
30738         seen_open_paren = true;
30739       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30740         {
30741           /* We have "@catch (...)" (where the '...' are literally
30742              what is in the code).  Skip the '...'.
30743              parameter_declaration is set to NULL_TREE, and
30744              objc_being_catch_clauses() knows that that means
30745              '...'.  */
30746           cp_lexer_consume_token (parser->lexer);
30747           parameter_declaration = NULL_TREE;
30748         }
30749       else
30750         {
30751           /* We have "@catch (NSException *exception)" or something
30752              like that.  Parse the parameter declaration.  */
30753           parm = cp_parser_parameter_declaration (parser, false, NULL);
30754           if (parm == NULL)
30755             parameter_declaration = error_mark_node;
30756           else
30757             parameter_declaration = grokdeclarator (parm->declarator,
30758                                                     &parm->decl_specifiers,
30759                                                     PARM, /*initialized=*/0,
30760                                                     /*attrlist=*/NULL);
30761         }
30762       if (seen_open_paren)
30763         parens.require_close (parser);
30764       else
30765         {
30766           /* If there was no open parenthesis, we are recovering from
30767              an error, and we are trying to figure out what mistake
30768              the user has made.  */
30769
30770           /* If there is an immediate closing parenthesis, the user
30771              probably forgot the opening one (ie, they typed "@catch
30772              NSException *e)".  Parse the closing parenthesis and keep
30773              going.  */
30774           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30775             cp_lexer_consume_token (parser->lexer);
30776           
30777           /* If these is no immediate closing parenthesis, the user
30778              probably doesn't know that parenthesis are required at
30779              all (ie, they typed "@catch NSException *e").  So, just
30780              forget about the closing parenthesis and keep going.  */
30781         }
30782       objc_begin_catch_clause (parameter_declaration);
30783       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30784       objc_finish_catch_clause ();
30785     }
30786   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30787     {
30788       cp_lexer_consume_token (parser->lexer);
30789       location = cp_lexer_peek_token (parser->lexer)->location;
30790       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30791          node, lest it get absorbed into the surrounding block.  */
30792       stmt = push_stmt_list ();
30793       cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30794       objc_build_finally_clause (location, pop_stmt_list (stmt));
30795     }
30796
30797   return objc_finish_try_stmt ();
30798 }
30799
30800 /* Parse an Objective-C synchronized statement.
30801
30802    objc-synchronized-stmt:
30803      @synchronized ( expression ) compound-statement
30804
30805    Returns NULL_TREE.  */
30806
30807 static tree
30808 cp_parser_objc_synchronized_statement (cp_parser *parser)
30809 {
30810   location_t location;
30811   tree lock, stmt;
30812
30813   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30814
30815   location = cp_lexer_peek_token (parser->lexer)->location;
30816   objc_maybe_warn_exceptions (location);
30817   matching_parens parens;
30818   parens.require_open (parser);
30819   lock = cp_parser_expression (parser);
30820   parens.require_close (parser);
30821
30822   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30823      node, lest it get absorbed into the surrounding block.  */
30824   stmt = push_stmt_list ();
30825   cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30826
30827   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30828 }
30829
30830 /* Parse an Objective-C throw statement.
30831
30832    objc-throw-stmt:
30833      @throw assignment-expression [opt] ;
30834
30835    Returns a constructed '@throw' statement.  */
30836
30837 static tree
30838 cp_parser_objc_throw_statement (cp_parser *parser)
30839 {
30840   tree expr = NULL_TREE;
30841   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30842
30843   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30844
30845   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30846     expr = cp_parser_expression (parser);
30847
30848   cp_parser_consume_semicolon_at_end_of_statement (parser);
30849
30850   return objc_build_throw_stmt (loc, expr);
30851 }
30852
30853 /* Parse an Objective-C statement.  */
30854
30855 static tree
30856 cp_parser_objc_statement (cp_parser * parser)
30857 {
30858   /* Try to figure out what kind of declaration is present.  */
30859   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30860
30861   switch (kwd->keyword)
30862     {
30863     case RID_AT_TRY:
30864       return cp_parser_objc_try_catch_finally_statement (parser);
30865     case RID_AT_SYNCHRONIZED:
30866       return cp_parser_objc_synchronized_statement (parser);
30867     case RID_AT_THROW:
30868       return cp_parser_objc_throw_statement (parser);
30869     default:
30870       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30871                kwd->u.value);
30872       cp_parser_skip_to_end_of_block_or_statement (parser);
30873     }
30874
30875   return error_mark_node;
30876 }
30877
30878 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
30879    look ahead to see if an objc keyword follows the attributes.  This
30880    is to detect the use of prefix attributes on ObjC @interface and 
30881    @protocol.  */
30882
30883 static bool
30884 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30885 {
30886   cp_lexer_save_tokens (parser->lexer);
30887   *attrib = cp_parser_attributes_opt (parser);
30888   gcc_assert (*attrib);
30889   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30890     {
30891       cp_lexer_commit_tokens (parser->lexer);
30892       return true;
30893     }
30894   cp_lexer_rollback_tokens (parser->lexer);
30895   return false;  
30896 }
30897
30898 /* This routine is a minimal replacement for
30899    c_parser_struct_declaration () used when parsing the list of
30900    types/names or ObjC++ properties.  For example, when parsing the
30901    code
30902
30903    @property (readonly) int a, b, c;
30904
30905    this function is responsible for parsing "int a, int b, int c" and
30906    returning the declarations as CHAIN of DECLs.
30907
30908    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
30909    similar parsing.  */
30910 static tree
30911 cp_parser_objc_struct_declaration (cp_parser *parser)
30912 {
30913   tree decls = NULL_TREE;
30914   cp_decl_specifier_seq declspecs;
30915   int decl_class_or_enum_p;
30916   tree prefix_attributes;
30917
30918   cp_parser_decl_specifier_seq (parser,
30919                                 CP_PARSER_FLAGS_NONE,
30920                                 &declspecs,
30921                                 &decl_class_or_enum_p);
30922
30923   if (declspecs.type == error_mark_node)
30924     return error_mark_node;
30925
30926   /* auto, register, static, extern, mutable.  */
30927   if (declspecs.storage_class != sc_none)
30928     {
30929       cp_parser_error (parser, "invalid type for property");
30930       declspecs.storage_class = sc_none;
30931     }
30932   
30933   /* thread_local.  */
30934   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30935     {
30936       cp_parser_error (parser, "invalid type for property");
30937       declspecs.locations[ds_thread] = 0;
30938     }
30939   
30940   /* typedef.  */
30941   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30942     {
30943       cp_parser_error (parser, "invalid type for property");
30944       declspecs.locations[ds_typedef] = 0;
30945     }
30946
30947   prefix_attributes = declspecs.attributes;
30948   declspecs.attributes = NULL_TREE;
30949
30950   /* Keep going until we hit the `;' at the end of the declaration. */
30951   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30952     {
30953       tree attributes, first_attribute, decl;
30954       cp_declarator *declarator;
30955       cp_token *token;
30956
30957       /* Parse the declarator.  */
30958       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30959                                          NULL, NULL, false, false);
30960
30961       /* Look for attributes that apply to the ivar.  */
30962       attributes = cp_parser_attributes_opt (parser);
30963       /* Remember which attributes are prefix attributes and
30964          which are not.  */
30965       first_attribute = attributes;
30966       /* Combine the attributes.  */
30967       attributes = attr_chainon (prefix_attributes, attributes);
30968
30969       decl = grokfield (declarator, &declspecs,
30970                         NULL_TREE, /*init_const_expr_p=*/false,
30971                         NULL_TREE, attributes);
30972
30973       if (decl == error_mark_node || decl == NULL_TREE)
30974         return error_mark_node;
30975       
30976       /* Reset PREFIX_ATTRIBUTES.  */
30977       if (attributes != error_mark_node)
30978         {
30979           while (attributes && TREE_CHAIN (attributes) != first_attribute)
30980             attributes = TREE_CHAIN (attributes);
30981           if (attributes)
30982             TREE_CHAIN (attributes) = NULL_TREE;
30983         }
30984
30985       DECL_CHAIN (decl) = decls;
30986       decls = decl;
30987
30988       token = cp_lexer_peek_token (parser->lexer);
30989       if (token->type == CPP_COMMA)
30990         {
30991           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
30992           continue;
30993         }
30994       else
30995         break;
30996     }
30997   return decls;
30998 }
30999
31000 /* Parse an Objective-C @property declaration.  The syntax is:
31001
31002    objc-property-declaration:
31003      '@property' objc-property-attributes[opt] struct-declaration ;
31004
31005    objc-property-attributes:
31006     '(' objc-property-attribute-list ')'
31007
31008    objc-property-attribute-list:
31009      objc-property-attribute
31010      objc-property-attribute-list, objc-property-attribute
31011
31012    objc-property-attribute
31013      'getter' = identifier
31014      'setter' = identifier
31015      'readonly'
31016      'readwrite'
31017      'assign'
31018      'retain'
31019      'copy'
31020      'nonatomic'
31021
31022   For example:
31023     @property NSString *name;
31024     @property (readonly) id object;
31025     @property (retain, nonatomic, getter=getTheName) id name;
31026     @property int a, b, c;
31027
31028    PS: This function is identical to
31029    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
31030 static void 
31031 cp_parser_objc_at_property_declaration (cp_parser *parser)
31032 {
31033   /* The following variables hold the attributes of the properties as
31034      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
31035      seen.  When we see an attribute, we set them to 'true' (if they
31036      are boolean properties) or to the identifier (if they have an
31037      argument, ie, for getter and setter).  Note that here we only
31038      parse the list of attributes, check the syntax and accumulate the
31039      attributes that we find.  objc_add_property_declaration() will
31040      then process the information.  */
31041   bool property_assign = false;
31042   bool property_copy = false;
31043   tree property_getter_ident = NULL_TREE;
31044   bool property_nonatomic = false;
31045   bool property_readonly = false;
31046   bool property_readwrite = false;
31047   bool property_retain = false;
31048   tree property_setter_ident = NULL_TREE;
31049
31050   /* 'properties' is the list of properties that we read.  Usually a
31051      single one, but maybe more (eg, in "@property int a, b, c;" there
31052      are three).  */
31053   tree properties;
31054   location_t loc;
31055
31056   loc = cp_lexer_peek_token (parser->lexer)->location;
31057
31058   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
31059
31060   /* Parse the optional attribute list...  */
31061   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31062     {
31063       /* Eat the '('.  */
31064       matching_parens parens;
31065       parens.consume_open (parser);
31066
31067       while (true)
31068         {
31069           bool syntax_error = false;
31070           cp_token *token = cp_lexer_peek_token (parser->lexer);
31071           enum rid keyword;
31072
31073           if (token->type != CPP_NAME)
31074             {
31075               cp_parser_error (parser, "expected identifier");
31076               break;
31077             }
31078           keyword = C_RID_CODE (token->u.value);
31079           cp_lexer_consume_token (parser->lexer);
31080           switch (keyword)
31081             {
31082             case RID_ASSIGN:    property_assign = true;    break;
31083             case RID_COPY:      property_copy = true;      break;
31084             case RID_NONATOMIC: property_nonatomic = true; break;
31085             case RID_READONLY:  property_readonly = true;  break;
31086             case RID_READWRITE: property_readwrite = true; break;
31087             case RID_RETAIN:    property_retain = true;    break;
31088
31089             case RID_GETTER:
31090             case RID_SETTER:
31091               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31092                 {
31093                   if (keyword == RID_GETTER)
31094                     cp_parser_error (parser,
31095                                      "missing %<=%> (after %<getter%> attribute)");
31096                   else
31097                     cp_parser_error (parser,
31098                                      "missing %<=%> (after %<setter%> attribute)");
31099                   syntax_error = true;
31100                   break;
31101                 }
31102               cp_lexer_consume_token (parser->lexer); /* eat the = */
31103               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31104                 {
31105                   cp_parser_error (parser, "expected identifier");
31106                   syntax_error = true;
31107                   break;
31108                 }
31109               if (keyword == RID_SETTER)
31110                 {
31111                   if (property_setter_ident != NULL_TREE)
31112                     {
31113                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31114                       cp_lexer_consume_token (parser->lexer);
31115                     }
31116                   else
31117                     property_setter_ident = cp_parser_objc_selector (parser);
31118                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31119                     cp_parser_error (parser, "setter name must terminate with %<:%>");
31120                   else
31121                     cp_lexer_consume_token (parser->lexer);
31122                 }
31123               else
31124                 {
31125                   if (property_getter_ident != NULL_TREE)
31126                     {
31127                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31128                       cp_lexer_consume_token (parser->lexer);
31129                     }
31130                   else
31131                     property_getter_ident = cp_parser_objc_selector (parser);
31132                 }
31133               break;
31134             default:
31135               cp_parser_error (parser, "unknown property attribute");
31136               syntax_error = true;
31137               break;
31138             }
31139
31140           if (syntax_error)
31141             break;
31142
31143           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31144             cp_lexer_consume_token (parser->lexer);
31145           else
31146             break;
31147         }
31148
31149       /* FIXME: "@property (setter, assign);" will generate a spurious
31150          "error: expected â€˜)’ before â€˜,’ token".  This is because
31151          cp_parser_require, unlike the C counterpart, will produce an
31152          error even if we are in error recovery.  */
31153       if (!parens.require_close (parser))
31154         {
31155           cp_parser_skip_to_closing_parenthesis (parser,
31156                                                  /*recovering=*/true,
31157                                                  /*or_comma=*/false,
31158                                                  /*consume_paren=*/true);
31159         }
31160     }
31161
31162   /* ... and the property declaration(s).  */
31163   properties = cp_parser_objc_struct_declaration (parser);
31164
31165   if (properties == error_mark_node)
31166     {
31167       cp_parser_skip_to_end_of_statement (parser);
31168       /* If the next token is now a `;', consume it.  */
31169       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31170         cp_lexer_consume_token (parser->lexer);
31171       return;
31172     }
31173
31174   if (properties == NULL_TREE)
31175     cp_parser_error (parser, "expected identifier");
31176   else
31177     {
31178       /* Comma-separated properties are chained together in
31179          reverse order; add them one by one.  */
31180       properties = nreverse (properties);
31181       
31182       for (; properties; properties = TREE_CHAIN (properties))
31183         objc_add_property_declaration (loc, copy_node (properties),
31184                                        property_readonly, property_readwrite,
31185                                        property_assign, property_retain,
31186                                        property_copy, property_nonatomic,
31187                                        property_getter_ident, property_setter_ident);
31188     }
31189   
31190   cp_parser_consume_semicolon_at_end_of_statement (parser);
31191 }
31192
31193 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
31194
31195    objc-synthesize-declaration:
31196      @synthesize objc-synthesize-identifier-list ;
31197
31198    objc-synthesize-identifier-list:
31199      objc-synthesize-identifier
31200      objc-synthesize-identifier-list, objc-synthesize-identifier
31201
31202    objc-synthesize-identifier
31203      identifier
31204      identifier = identifier
31205
31206   For example:
31207     @synthesize MyProperty;
31208     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31209
31210   PS: This function is identical to c_parser_objc_at_synthesize_declaration
31211   for C.  Keep them in sync.
31212 */
31213 static void 
31214 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31215 {
31216   tree list = NULL_TREE;
31217   location_t loc;
31218   loc = cp_lexer_peek_token (parser->lexer)->location;
31219
31220   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
31221   while (true)
31222     {
31223       tree property, ivar;
31224       property = cp_parser_identifier (parser);
31225       if (property == error_mark_node)
31226         {
31227           cp_parser_consume_semicolon_at_end_of_statement (parser);
31228           return;
31229         }
31230       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31231         {
31232           cp_lexer_consume_token (parser->lexer);
31233           ivar = cp_parser_identifier (parser);
31234           if (ivar == error_mark_node)
31235             {
31236               cp_parser_consume_semicolon_at_end_of_statement (parser);
31237               return;
31238             }
31239         }
31240       else
31241         ivar = NULL_TREE;
31242       list = chainon (list, build_tree_list (ivar, property));
31243       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31244         cp_lexer_consume_token (parser->lexer);
31245       else
31246         break;
31247     }
31248   cp_parser_consume_semicolon_at_end_of_statement (parser);
31249   objc_add_synthesize_declaration (loc, list);
31250 }
31251
31252 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
31253
31254    objc-dynamic-declaration:
31255      @dynamic identifier-list ;
31256
31257    For example:
31258      @dynamic MyProperty;
31259      @dynamic MyProperty, AnotherProperty;
31260
31261   PS: This function is identical to c_parser_objc_at_dynamic_declaration
31262   for C.  Keep them in sync.
31263 */
31264 static void 
31265 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31266 {
31267   tree list = NULL_TREE;
31268   location_t loc;
31269   loc = cp_lexer_peek_token (parser->lexer)->location;
31270
31271   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
31272   while (true)
31273     {
31274       tree property;
31275       property = cp_parser_identifier (parser);
31276       if (property == error_mark_node)
31277         {
31278           cp_parser_consume_semicolon_at_end_of_statement (parser);
31279           return;
31280         }
31281       list = chainon (list, build_tree_list (NULL, property));
31282       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31283         cp_lexer_consume_token (parser->lexer);
31284       else
31285         break;
31286     }
31287   cp_parser_consume_semicolon_at_end_of_statement (parser);
31288   objc_add_dynamic_declaration (loc, list);
31289 }
31290
31291 \f
31292 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
31293
31294 /* Returns name of the next clause.
31295    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31296    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
31297    returned and the token is consumed.  */
31298
31299 static pragma_omp_clause
31300 cp_parser_omp_clause_name (cp_parser *parser)
31301 {
31302   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31303
31304   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31305     result = PRAGMA_OACC_CLAUSE_AUTO;
31306   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31307     result = PRAGMA_OMP_CLAUSE_IF;
31308   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31309     result = PRAGMA_OMP_CLAUSE_DEFAULT;
31310   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31311     result = PRAGMA_OACC_CLAUSE_DELETE;
31312   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31313     result = PRAGMA_OMP_CLAUSE_PRIVATE;
31314   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31315     result = PRAGMA_OMP_CLAUSE_FOR;
31316   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31317     {
31318       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31319       const char *p = IDENTIFIER_POINTER (id);
31320
31321       switch (p[0])
31322         {
31323         case 'a':
31324           if (!strcmp ("aligned", p))
31325             result = PRAGMA_OMP_CLAUSE_ALIGNED;
31326           else if (!strcmp ("async", p))
31327             result = PRAGMA_OACC_CLAUSE_ASYNC;
31328           break;
31329         case 'c':
31330           if (!strcmp ("collapse", p))
31331             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31332           else if (!strcmp ("copy", p))
31333             result = PRAGMA_OACC_CLAUSE_COPY;
31334           else if (!strcmp ("copyin", p))
31335             result = PRAGMA_OMP_CLAUSE_COPYIN;
31336           else if (!strcmp ("copyout", p))
31337             result = PRAGMA_OACC_CLAUSE_COPYOUT;
31338           else if (!strcmp ("copyprivate", p))
31339             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31340           else if (!strcmp ("create", p))
31341             result = PRAGMA_OACC_CLAUSE_CREATE;
31342           break;
31343         case 'd':
31344           if (!strcmp ("defaultmap", p))
31345             result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31346           else if (!strcmp ("depend", p))
31347             result = PRAGMA_OMP_CLAUSE_DEPEND;
31348           else if (!strcmp ("device", p))
31349             result = PRAGMA_OMP_CLAUSE_DEVICE;
31350           else if (!strcmp ("deviceptr", p))
31351             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31352           else if (!strcmp ("device_resident", p))
31353             result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31354           else if (!strcmp ("dist_schedule", p))
31355             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31356           break;
31357         case 'f':
31358           if (!strcmp ("final", p))
31359             result = PRAGMA_OMP_CLAUSE_FINAL;
31360           else if (!strcmp ("firstprivate", p))
31361             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31362           else if (!strcmp ("from", p))
31363             result = PRAGMA_OMP_CLAUSE_FROM;
31364           break;
31365         case 'g':
31366           if (!strcmp ("gang", p))
31367             result = PRAGMA_OACC_CLAUSE_GANG;
31368           else if (!strcmp ("grainsize", p))
31369             result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31370           break;
31371         case 'h':
31372           if (!strcmp ("hint", p))
31373             result = PRAGMA_OMP_CLAUSE_HINT;
31374           else if (!strcmp ("host", p))
31375             result = PRAGMA_OACC_CLAUSE_HOST;
31376           break;
31377         case 'i':
31378           if (!strcmp ("inbranch", p))
31379             result = PRAGMA_OMP_CLAUSE_INBRANCH;
31380           else if (!strcmp ("independent", p))
31381             result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31382           else if (!strcmp ("is_device_ptr", p))
31383             result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31384           break;
31385         case 'l':
31386           if (!strcmp ("lastprivate", p))
31387             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31388           else if (!strcmp ("linear", p))
31389             result = PRAGMA_OMP_CLAUSE_LINEAR;
31390           else if (!strcmp ("link", p))
31391             result = PRAGMA_OMP_CLAUSE_LINK;
31392           break;
31393         case 'm':
31394           if (!strcmp ("map", p))
31395             result = PRAGMA_OMP_CLAUSE_MAP;
31396           else if (!strcmp ("mergeable", p))
31397             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31398           break;
31399         case 'n':
31400           if (!strcmp ("nogroup", p))
31401             result = PRAGMA_OMP_CLAUSE_NOGROUP;
31402           else if (!strcmp ("notinbranch", p))
31403             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31404           else if (!strcmp ("nowait", p))
31405             result = PRAGMA_OMP_CLAUSE_NOWAIT;
31406           else if (!strcmp ("num_gangs", p))
31407             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31408           else if (!strcmp ("num_tasks", p))
31409             result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31410           else if (!strcmp ("num_teams", p))
31411             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31412           else if (!strcmp ("num_threads", p))
31413             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31414           else if (!strcmp ("num_workers", p))
31415             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31416           break;
31417         case 'o':
31418           if (!strcmp ("ordered", p))
31419             result = PRAGMA_OMP_CLAUSE_ORDERED;
31420           break;
31421         case 'p':
31422           if (!strcmp ("parallel", p))
31423             result = PRAGMA_OMP_CLAUSE_PARALLEL;
31424           else if (!strcmp ("present", p))
31425             result = PRAGMA_OACC_CLAUSE_PRESENT;
31426           else if (!strcmp ("present_or_copy", p)
31427                    || !strcmp ("pcopy", p))
31428             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31429           else if (!strcmp ("present_or_copyin", p)
31430                    || !strcmp ("pcopyin", p))
31431             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31432           else if (!strcmp ("present_or_copyout", p)
31433                    || !strcmp ("pcopyout", p))
31434             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31435           else if (!strcmp ("present_or_create", p)
31436                    || !strcmp ("pcreate", p))
31437             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31438           else if (!strcmp ("priority", p))
31439             result = PRAGMA_OMP_CLAUSE_PRIORITY;
31440           else if (!strcmp ("proc_bind", p))
31441             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31442           break;
31443         case 'r':
31444           if (!strcmp ("reduction", p))
31445             result = PRAGMA_OMP_CLAUSE_REDUCTION;
31446           break;
31447         case 's':
31448           if (!strcmp ("safelen", p))
31449             result = PRAGMA_OMP_CLAUSE_SAFELEN;
31450           else if (!strcmp ("schedule", p))
31451             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31452           else if (!strcmp ("sections", p))
31453             result = PRAGMA_OMP_CLAUSE_SECTIONS;
31454           else if (!strcmp ("self", p))
31455             result = PRAGMA_OACC_CLAUSE_SELF;
31456           else if (!strcmp ("seq", p))
31457             result = PRAGMA_OACC_CLAUSE_SEQ;
31458           else if (!strcmp ("shared", p))
31459             result = PRAGMA_OMP_CLAUSE_SHARED;
31460           else if (!strcmp ("simd", p))
31461             result = PRAGMA_OMP_CLAUSE_SIMD;
31462           else if (!strcmp ("simdlen", p))
31463             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31464           break;
31465         case 't':
31466           if (!strcmp ("taskgroup", p))
31467             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31468           else if (!strcmp ("thread_limit", p))
31469             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31470           else if (!strcmp ("threads", p))
31471             result = PRAGMA_OMP_CLAUSE_THREADS;
31472           else if (!strcmp ("tile", p))
31473             result = PRAGMA_OACC_CLAUSE_TILE;
31474           else if (!strcmp ("to", p))
31475             result = PRAGMA_OMP_CLAUSE_TO;
31476           break;
31477         case 'u':
31478           if (!strcmp ("uniform", p))
31479             result = PRAGMA_OMP_CLAUSE_UNIFORM;
31480           else if (!strcmp ("untied", p))
31481             result = PRAGMA_OMP_CLAUSE_UNTIED;
31482           else if (!strcmp ("use_device", p))
31483             result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31484           else if (!strcmp ("use_device_ptr", p))
31485             result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31486           break;
31487         case 'v':
31488           if (!strcmp ("vector", p))
31489             result = PRAGMA_OACC_CLAUSE_VECTOR;
31490           else if (!strcmp ("vector_length", p))
31491             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31492           break;
31493         case 'w':
31494           if (!strcmp ("wait", p))
31495             result = PRAGMA_OACC_CLAUSE_WAIT;
31496           else if (!strcmp ("worker", p))
31497             result = PRAGMA_OACC_CLAUSE_WORKER;
31498           break;
31499         }
31500     }
31501
31502   if (result != PRAGMA_OMP_CLAUSE_NONE)
31503     cp_lexer_consume_token (parser->lexer);
31504
31505   return result;
31506 }
31507
31508 /* Validate that a clause of the given type does not already exist.  */
31509
31510 static void
31511 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31512                            const char *name, location_t location)
31513 {
31514   tree c;
31515
31516   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31517     if (OMP_CLAUSE_CODE (c) == code)
31518       {
31519         error_at (location, "too many %qs clauses", name);
31520         break;
31521       }
31522 }
31523
31524 /* OpenMP 2.5:
31525    variable-list:
31526      identifier
31527      variable-list , identifier
31528
31529    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31530    colon).  An opening parenthesis will have been consumed by the caller.
31531
31532    If KIND is nonzero, create the appropriate node and install the decl
31533    in OMP_CLAUSE_DECL and add the node to the head of the list.
31534
31535    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31536    return the list created.
31537
31538    COLON can be NULL if only closing parenthesis should end the list,
31539    or pointer to bool which will receive false if the list is terminated
31540    by closing parenthesis or true if the list is terminated by colon.  */
31541
31542 static tree
31543 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31544                                 tree list, bool *colon)
31545 {
31546   cp_token *token;
31547   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31548   if (colon)
31549     {
31550       parser->colon_corrects_to_scope_p = false;
31551       *colon = false;
31552     }
31553   while (1)
31554     {
31555       tree name, decl;
31556
31557       token = cp_lexer_peek_token (parser->lexer);
31558       if (kind != 0
31559           && current_class_ptr
31560           && cp_parser_is_keyword (token, RID_THIS))
31561         {
31562           decl = finish_this_expr ();
31563           if (TREE_CODE (decl) == NON_LVALUE_EXPR
31564               || CONVERT_EXPR_P (decl))
31565             decl = TREE_OPERAND (decl, 0);
31566           cp_lexer_consume_token (parser->lexer);
31567         }
31568       else
31569         {
31570           name = cp_parser_id_expression (parser, /*template_p=*/false,
31571                                           /*check_dependency_p=*/true,
31572                                           /*template_p=*/NULL,
31573                                           /*declarator_p=*/false,
31574                                           /*optional_p=*/false);
31575           if (name == error_mark_node)
31576             goto skip_comma;
31577
31578           if (identifier_p (name))
31579             decl = cp_parser_lookup_name_simple (parser, name, token->location);
31580           else
31581             decl = name;
31582           if (decl == error_mark_node)
31583             cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31584                                          token->location);
31585         }
31586       if (decl == error_mark_node)
31587         ;
31588       else if (kind != 0)
31589         {
31590           switch (kind)
31591             {
31592             case OMP_CLAUSE__CACHE_:
31593               /* The OpenACC cache directive explicitly only allows "array
31594                  elements or subarrays".  */
31595               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31596                 {
31597                   error_at (token->location, "expected %<[%>");
31598                   decl = error_mark_node;
31599                   break;
31600                 }
31601               /* FALLTHROUGH.  */
31602             case OMP_CLAUSE_MAP:
31603             case OMP_CLAUSE_FROM:
31604             case OMP_CLAUSE_TO:
31605               while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31606                 {
31607                   location_t loc
31608                     = cp_lexer_peek_token (parser->lexer)->location;
31609                   cp_id_kind idk = CP_ID_KIND_NONE;
31610                   cp_lexer_consume_token (parser->lexer);
31611                   decl = convert_from_reference (decl);
31612                   decl
31613                     = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31614                                                               decl, false,
31615                                                               &idk, loc);
31616                 }
31617               /* FALLTHROUGH.  */
31618             case OMP_CLAUSE_DEPEND:
31619             case OMP_CLAUSE_REDUCTION:
31620               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31621                 {
31622                   tree low_bound = NULL_TREE, length = NULL_TREE;
31623
31624                   parser->colon_corrects_to_scope_p = false;
31625                   cp_lexer_consume_token (parser->lexer);
31626                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31627                     low_bound = cp_parser_expression (parser);
31628                   if (!colon)
31629                     parser->colon_corrects_to_scope_p
31630                       = saved_colon_corrects_to_scope_p;
31631                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31632                     length = integer_one_node;
31633                   else
31634                     {
31635                       /* Look for `:'.  */
31636                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31637                         goto skip_comma;
31638                       if (!cp_lexer_next_token_is (parser->lexer,
31639                                                    CPP_CLOSE_SQUARE))
31640                         length = cp_parser_expression (parser);
31641                     }
31642                   /* Look for the closing `]'.  */
31643                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31644                                           RT_CLOSE_SQUARE))
31645                     goto skip_comma;
31646
31647                   decl = tree_cons (low_bound, length, decl);
31648                 }
31649               break;
31650             default:
31651               break;
31652             }
31653
31654           tree u = build_omp_clause (token->location, kind);
31655           OMP_CLAUSE_DECL (u) = decl;
31656           OMP_CLAUSE_CHAIN (u) = list;
31657           list = u;
31658         }
31659       else
31660         list = tree_cons (decl, NULL_TREE, list);
31661
31662     get_comma:
31663       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31664         break;
31665       cp_lexer_consume_token (parser->lexer);
31666     }
31667
31668   if (colon)
31669     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31670
31671   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31672     {
31673       *colon = true;
31674       cp_parser_require (parser, CPP_COLON, RT_COLON);
31675       return list;
31676     }
31677
31678   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31679     {
31680       int ending;
31681
31682       /* Try to resync to an unnested comma.  Copied from
31683          cp_parser_parenthesized_expression_list.  */
31684     skip_comma:
31685       if (colon)
31686         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31687       ending = cp_parser_skip_to_closing_parenthesis (parser,
31688                                                       /*recovering=*/true,
31689                                                       /*or_comma=*/true,
31690                                                       /*consume_paren=*/true);
31691       if (ending < 0)
31692         goto get_comma;
31693     }
31694
31695   return list;
31696 }
31697
31698 /* Similarly, but expect leading and trailing parenthesis.  This is a very
31699    common case for omp clauses.  */
31700
31701 static tree
31702 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31703 {
31704   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31705     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31706   return list;
31707 }
31708
31709 /* OpenACC 2.0:
31710    copy ( variable-list )
31711    copyin ( variable-list )
31712    copyout ( variable-list )
31713    create ( variable-list )
31714    delete ( variable-list )
31715    present ( variable-list )
31716    present_or_copy ( variable-list )
31717      pcopy ( variable-list )
31718    present_or_copyin ( variable-list )
31719      pcopyin ( variable-list )
31720    present_or_copyout ( variable-list )
31721      pcopyout ( variable-list )
31722    present_or_create ( variable-list )
31723      pcreate ( variable-list ) */
31724
31725 static tree
31726 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31727                             tree list)
31728 {
31729   enum gomp_map_kind kind;
31730   switch (c_kind)
31731     {
31732     case PRAGMA_OACC_CLAUSE_COPY:
31733       kind = GOMP_MAP_FORCE_TOFROM;
31734       break;
31735     case PRAGMA_OACC_CLAUSE_COPYIN:
31736       kind = GOMP_MAP_FORCE_TO;
31737       break;
31738     case PRAGMA_OACC_CLAUSE_COPYOUT:
31739       kind = GOMP_MAP_FORCE_FROM;
31740       break;
31741     case PRAGMA_OACC_CLAUSE_CREATE:
31742       kind = GOMP_MAP_FORCE_ALLOC;
31743       break;
31744     case PRAGMA_OACC_CLAUSE_DELETE:
31745       kind = GOMP_MAP_DELETE;
31746       break;
31747     case PRAGMA_OACC_CLAUSE_DEVICE:
31748       kind = GOMP_MAP_FORCE_TO;
31749       break;
31750     case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31751       kind = GOMP_MAP_DEVICE_RESIDENT;
31752       break;
31753     case PRAGMA_OACC_CLAUSE_HOST:
31754     case PRAGMA_OACC_CLAUSE_SELF:
31755       kind = GOMP_MAP_FORCE_FROM;
31756       break;
31757     case PRAGMA_OACC_CLAUSE_LINK:
31758       kind = GOMP_MAP_LINK;
31759       break;
31760     case PRAGMA_OACC_CLAUSE_PRESENT:
31761       kind = GOMP_MAP_FORCE_PRESENT;
31762       break;
31763     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31764       kind = GOMP_MAP_TOFROM;
31765       break;
31766     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31767       kind = GOMP_MAP_TO;
31768       break;
31769     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31770       kind = GOMP_MAP_FROM;
31771       break;
31772     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31773       kind = GOMP_MAP_ALLOC;
31774       break;
31775     default:
31776       gcc_unreachable ();
31777     }
31778   tree nl, c;
31779   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31780
31781   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31782     OMP_CLAUSE_SET_MAP_KIND (c, kind);
31783
31784   return nl;
31785 }
31786
31787 /* OpenACC 2.0:
31788    deviceptr ( variable-list ) */
31789
31790 static tree
31791 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31792 {
31793   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31794   tree vars, t;
31795
31796   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31797      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31798      variable-list must only allow for pointer variables.  */
31799   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31800   for (t = vars; t; t = TREE_CHAIN (t))
31801     {
31802       tree v = TREE_PURPOSE (t);
31803       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31804       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31805       OMP_CLAUSE_DECL (u) = v;
31806       OMP_CLAUSE_CHAIN (u) = list;
31807       list = u;
31808     }
31809
31810   return list;
31811 }
31812
31813 /* OpenACC 2.0:
31814    auto
31815    independent
31816    nohost
31817    seq */
31818
31819 static tree
31820 cp_parser_oacc_simple_clause (cp_parser * /* parser  */,
31821                               enum omp_clause_code code,
31822                               tree list, location_t location)
31823 {
31824   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31825   tree c = build_omp_clause (location, code);
31826   OMP_CLAUSE_CHAIN (c) = list;
31827   return c;
31828 }
31829
31830  /* OpenACC:
31831    num_gangs ( expression )
31832    num_workers ( expression )
31833    vector_length ( expression )  */
31834
31835 static tree
31836 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31837                                   const char *str, tree list)
31838 {
31839   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31840
31841   matching_parens parens;
31842   if (!parens.require_open (parser))
31843     return list;
31844
31845   tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31846
31847   if (t == error_mark_node
31848       || !parens.require_close (parser))
31849     {
31850       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31851                                              /*or_comma=*/false,
31852                                              /*consume_paren=*/true);
31853       return list;
31854     }
31855
31856   check_no_duplicate_clause (list, code, str, loc);
31857
31858   tree c = build_omp_clause (loc, code);
31859   OMP_CLAUSE_OPERAND (c, 0) = t;
31860   OMP_CLAUSE_CHAIN (c) = list;
31861   return c;
31862 }
31863
31864 /* OpenACC:
31865
31866     gang [( gang-arg-list )]
31867     worker [( [num:] int-expr )]
31868     vector [( [length:] int-expr )]
31869
31870   where gang-arg is one of:
31871
31872     [num:] int-expr
31873     static: size-expr
31874
31875   and size-expr may be:
31876
31877     *
31878     int-expr
31879 */
31880
31881 static tree
31882 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31883                              const char *str, tree list)
31884 {
31885   const char *id = "num";
31886   cp_lexer *lexer = parser->lexer;
31887   tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31888   location_t loc = cp_lexer_peek_token (lexer)->location;
31889
31890   if (kind == OMP_CLAUSE_VECTOR)
31891     id = "length";
31892
31893   if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31894     {
31895       matching_parens parens;
31896       parens.consume_open (parser);
31897
31898       do
31899         {
31900           cp_token *next = cp_lexer_peek_token (lexer);
31901           int idx = 0;
31902
31903           /* Gang static argument.  */
31904           if (kind == OMP_CLAUSE_GANG
31905               && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31906             {
31907               cp_lexer_consume_token (lexer);
31908
31909               if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31910                 goto cleanup_error;
31911
31912               idx = 1;
31913               if (ops[idx] != NULL)
31914                 {
31915                   cp_parser_error (parser, "too many %<static%> arguments");
31916                   goto cleanup_error;
31917                 }
31918
31919               /* Check for the '*' argument.  */
31920               if (cp_lexer_next_token_is (lexer, CPP_MULT)
31921                   && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31922                       || cp_lexer_nth_token_is (parser->lexer, 2,
31923                                                 CPP_CLOSE_PAREN)))
31924                 {
31925                   cp_lexer_consume_token (lexer);
31926                   ops[idx] = integer_minus_one_node;
31927
31928                   if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31929                     {
31930                       cp_lexer_consume_token (lexer);
31931                       continue;
31932                     }
31933                   else break;
31934                 }
31935             }
31936           /* Worker num: argument and vector length: arguments.  */
31937           else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31938                    && id_equal (next->u.value, id)
31939                    && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31940             {
31941               cp_lexer_consume_token (lexer);  /* id  */
31942               cp_lexer_consume_token (lexer);  /* ':'  */
31943             }
31944
31945           /* Now collect the actual argument.  */
31946           if (ops[idx] != NULL_TREE)
31947             {
31948               cp_parser_error (parser, "unexpected argument");
31949               goto cleanup_error;
31950             }
31951
31952           tree expr = cp_parser_assignment_expression (parser, NULL, false,
31953                                                        false);
31954           if (expr == error_mark_node)
31955             goto cleanup_error;
31956
31957           mark_exp_read (expr);
31958           ops[idx] = expr;
31959
31960           if (kind == OMP_CLAUSE_GANG
31961               && cp_lexer_next_token_is (lexer, CPP_COMMA))
31962             {
31963               cp_lexer_consume_token (lexer);
31964               continue;
31965             }
31966           break;
31967         }
31968       while (1);
31969
31970       if (!parens.require_close (parser))
31971         goto cleanup_error;
31972     }
31973
31974   check_no_duplicate_clause (list, kind, str, loc);
31975
31976   c = build_omp_clause (loc, kind);
31977
31978   if (ops[1])
31979     OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31980
31981   OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31982   OMP_CLAUSE_CHAIN (c) = list;
31983
31984   return c;
31985
31986  cleanup_error:
31987   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31988   return list;
31989 }
31990
31991 /* OpenACC 2.0:
31992    tile ( size-expr-list ) */
31993
31994 static tree
31995 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31996 {
31997   tree c, expr = error_mark_node;
31998   tree tile = NULL_TREE;
31999
32000   /* Collapse and tile are mutually exclusive.  (The spec doesn't say
32001      so, but the spec authors never considered such a case and have
32002      differing opinions on what it might mean, including 'not
32003      allowed'.)  */
32004   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32005   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32006                              clause_loc);
32007
32008   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32009     return list;
32010
32011   do
32012     {
32013       if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32014         return list;
32015       
32016       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32017           && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32018               || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32019         {
32020           cp_lexer_consume_token (parser->lexer);
32021           expr = integer_zero_node;
32022         }
32023       else
32024         expr = cp_parser_constant_expression (parser);
32025
32026       tile = tree_cons (NULL_TREE, expr, tile);
32027     }
32028   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32029
32030   /* Consume the trailing ')'.  */
32031   cp_lexer_consume_token (parser->lexer);
32032
32033   c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32034   tile = nreverse (tile);
32035   OMP_CLAUSE_TILE_LIST (c) = tile;
32036   OMP_CLAUSE_CHAIN (c) = list;
32037   return c;
32038 }
32039
32040 /* OpenACC 2.0
32041    Parse wait clause or directive parameters.  */
32042
32043 static tree
32044 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32045 {
32046   vec<tree, va_gc> *args;
32047   tree t, args_tree;
32048
32049   args = cp_parser_parenthesized_expression_list (parser, non_attr,
32050                                                   /*cast_p=*/false,
32051                                                   /*allow_expansion_p=*/true,
32052                                                   /*non_constant_p=*/NULL);
32053
32054   if (args == NULL || args->length () == 0)
32055     {
32056       cp_parser_error (parser, "expected integer expression before ')'");
32057       if (args != NULL)
32058         release_tree_vector (args);
32059       return list;
32060     }
32061
32062   args_tree = build_tree_list_vec (args);
32063
32064   release_tree_vector (args);
32065
32066   for (t = args_tree; t; t = TREE_CHAIN (t))
32067     {
32068       tree targ = TREE_VALUE (t);
32069
32070       if (targ != error_mark_node)
32071         {
32072           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32073             error ("%<wait%> expression must be integral");
32074           else
32075             {
32076               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32077
32078               targ = mark_rvalue_use (targ);
32079               OMP_CLAUSE_DECL (c) = targ;
32080               OMP_CLAUSE_CHAIN (c) = list;
32081               list = c;
32082             }
32083         }
32084     }
32085
32086   return list;
32087 }
32088
32089 /* OpenACC:
32090    wait ( int-expr-list ) */
32091
32092 static tree
32093 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32094 {
32095   location_t location = cp_lexer_peek_token (parser->lexer)->location;
32096
32097   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32098     return list;
32099
32100   list = cp_parser_oacc_wait_list (parser, location, list);
32101
32102   return list;
32103 }
32104
32105 /* OpenMP 3.0:
32106    collapse ( constant-expression ) */
32107
32108 static tree
32109 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32110 {
32111   tree c, num;
32112   location_t loc;
32113   HOST_WIDE_INT n;
32114
32115   loc = cp_lexer_peek_token (parser->lexer)->location;
32116   matching_parens parens;
32117   if (!parens.require_open (parser))
32118     return list;
32119
32120   num = cp_parser_constant_expression (parser);
32121
32122   if (!parens.require_close (parser))
32123     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32124                                            /*or_comma=*/false,
32125                                            /*consume_paren=*/true);
32126
32127   if (num == error_mark_node)
32128     return list;
32129   num = fold_non_dependent_expr (num);
32130   if (!tree_fits_shwi_p (num)
32131       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32132       || (n = tree_to_shwi (num)) <= 0
32133       || (int) n != n)
32134     {
32135       error_at (loc, "collapse argument needs positive constant integer expression");
32136       return list;
32137     }
32138
32139   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32140   check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32141   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32142   OMP_CLAUSE_CHAIN (c) = list;
32143   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32144
32145   return c;
32146 }
32147
32148 /* OpenMP 2.5:
32149    default ( none | shared )
32150
32151    OpenACC:
32152    default ( none | present ) */
32153
32154 static tree
32155 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32156                               location_t location, bool is_oacc)
32157 {
32158   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32159   tree c;
32160
32161   matching_parens parens;
32162   if (!parens.require_open (parser))
32163     return list;
32164   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32165     {
32166       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32167       const char *p = IDENTIFIER_POINTER (id);
32168
32169       switch (p[0])
32170         {
32171         case 'n':
32172           if (strcmp ("none", p) != 0)
32173             goto invalid_kind;
32174           kind = OMP_CLAUSE_DEFAULT_NONE;
32175           break;
32176
32177         case 'p':
32178           if (strcmp ("present", p) != 0 || !is_oacc)
32179             goto invalid_kind;
32180           kind = OMP_CLAUSE_DEFAULT_PRESENT;
32181           break;
32182
32183         case 's':
32184           if (strcmp ("shared", p) != 0 || is_oacc)
32185             goto invalid_kind;
32186           kind = OMP_CLAUSE_DEFAULT_SHARED;
32187           break;
32188
32189         default:
32190           goto invalid_kind;
32191         }
32192
32193       cp_lexer_consume_token (parser->lexer);
32194     }
32195   else
32196     {
32197     invalid_kind:
32198       if (is_oacc)
32199         cp_parser_error (parser, "expected %<none%> or %<present%>");
32200       else
32201         cp_parser_error (parser, "expected %<none%> or %<shared%>");
32202     }
32203
32204   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32205       || !parens.require_close (parser))
32206     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32207                                            /*or_comma=*/false,
32208                                            /*consume_paren=*/true);
32209
32210   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32211     return list;
32212
32213   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32214   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32215   OMP_CLAUSE_CHAIN (c) = list;
32216   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32217
32218   return c;
32219 }
32220
32221 /* OpenMP 3.1:
32222    final ( expression ) */
32223
32224 static tree
32225 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32226 {
32227   tree t, c;
32228
32229   matching_parens parens;
32230   if (!parens.require_open (parser))
32231     return list;
32232
32233   t = cp_parser_condition (parser);
32234
32235   if (t == error_mark_node
32236       || !parens.require_close (parser))
32237     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32238                                            /*or_comma=*/false,
32239                                            /*consume_paren=*/true);
32240
32241   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32242
32243   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32244   OMP_CLAUSE_FINAL_EXPR (c) = t;
32245   OMP_CLAUSE_CHAIN (c) = list;
32246
32247   return c;
32248 }
32249
32250 /* OpenMP 2.5:
32251    if ( expression )
32252
32253    OpenMP 4.5:
32254    if ( directive-name-modifier : expression )
32255
32256    directive-name-modifier:
32257      parallel | task | taskloop | target data | target | target update
32258      | target enter data | target exit data  */
32259
32260 static tree
32261 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32262                          bool is_omp)
32263 {
32264   tree t, c;
32265   enum tree_code if_modifier = ERROR_MARK;
32266
32267   matching_parens parens;
32268   if (!parens.require_open (parser))
32269     return list;
32270
32271   if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32272     {
32273       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32274       const char *p = IDENTIFIER_POINTER (id);
32275       int n = 2;
32276
32277       if (strcmp ("parallel", p) == 0)
32278         if_modifier = OMP_PARALLEL;
32279       else if (strcmp ("task", p) == 0)
32280         if_modifier = OMP_TASK;
32281       else if (strcmp ("taskloop", p) == 0)
32282         if_modifier = OMP_TASKLOOP;
32283       else if (strcmp ("target", p) == 0)
32284         {
32285           if_modifier = OMP_TARGET;
32286           if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32287             {
32288               id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32289               p = IDENTIFIER_POINTER (id);
32290               if (strcmp ("data", p) == 0)
32291                 if_modifier = OMP_TARGET_DATA;
32292               else if (strcmp ("update", p) == 0)
32293                 if_modifier = OMP_TARGET_UPDATE;
32294               else if (strcmp ("enter", p) == 0)
32295                 if_modifier = OMP_TARGET_ENTER_DATA;
32296               else if (strcmp ("exit", p) == 0)
32297                 if_modifier = OMP_TARGET_EXIT_DATA;
32298               if (if_modifier != OMP_TARGET)
32299                 n = 3;
32300               else
32301                 {
32302                   location_t loc
32303                     = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32304                   error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32305                                  "or %<exit%>");
32306                   if_modifier = ERROR_MARK;
32307                 }
32308               if (if_modifier == OMP_TARGET_ENTER_DATA
32309                   || if_modifier == OMP_TARGET_EXIT_DATA)
32310                 {
32311                   if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32312                     {
32313                       id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32314                       p = IDENTIFIER_POINTER (id);
32315                       if (strcmp ("data", p) == 0)
32316                         n = 4;
32317                     }
32318                   if (n != 4)
32319                     {
32320                       location_t loc
32321                         = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32322                       error_at (loc, "expected %<data%>");
32323                       if_modifier = ERROR_MARK;
32324                     }
32325                 }
32326             }
32327         }
32328       if (if_modifier != ERROR_MARK)
32329         {
32330           if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32331             {
32332               while (n-- > 0)
32333                 cp_lexer_consume_token (parser->lexer);
32334             }
32335           else
32336             {
32337               if (n > 2)
32338                 {
32339                   location_t loc
32340                     = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32341                   error_at (loc, "expected %<:%>");
32342                 }
32343               if_modifier = ERROR_MARK;
32344             }
32345         }
32346     }
32347
32348   t = cp_parser_condition (parser);
32349
32350   if (t == error_mark_node
32351       || !parens.require_close (parser))
32352     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32353                                            /*or_comma=*/false,
32354                                            /*consume_paren=*/true);
32355
32356   for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32357     if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32358       {
32359         if (if_modifier != ERROR_MARK
32360             && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32361           {
32362             const char *p = NULL;
32363             switch (if_modifier)
32364               {
32365               case OMP_PARALLEL: p = "parallel"; break;
32366               case OMP_TASK: p = "task"; break;
32367               case OMP_TASKLOOP: p = "taskloop"; break;
32368               case OMP_TARGET_DATA: p = "target data"; break;
32369               case OMP_TARGET: p = "target"; break;
32370               case OMP_TARGET_UPDATE: p = "target update"; break;
32371               case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32372               case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32373               default: gcc_unreachable ();
32374               }
32375             error_at (location, "too many %<if%> clauses with %qs modifier",
32376                       p);
32377             return list;
32378           }
32379         else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32380           {
32381             if (!is_omp)
32382               error_at (location, "too many %<if%> clauses");
32383             else
32384               error_at (location, "too many %<if%> clauses without modifier");
32385             return list;
32386           }
32387         else if (if_modifier == ERROR_MARK
32388                  || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32389           {
32390             error_at (location, "if any %<if%> clause has modifier, then all "
32391                                 "%<if%> clauses have to use modifier");
32392             return list;
32393           }
32394       }
32395
32396   c = build_omp_clause (location, OMP_CLAUSE_IF);
32397   OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32398   OMP_CLAUSE_IF_EXPR (c) = t;
32399   OMP_CLAUSE_CHAIN (c) = list;
32400
32401   return c;
32402 }
32403
32404 /* OpenMP 3.1:
32405    mergeable */
32406
32407 static tree
32408 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32409                                 tree list, location_t location)
32410 {
32411   tree c;
32412
32413   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32414                              location);
32415
32416   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32417   OMP_CLAUSE_CHAIN (c) = list;
32418   return c;
32419 }
32420
32421 /* OpenMP 2.5:
32422    nowait */
32423
32424 static tree
32425 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32426                              tree list, location_t location)
32427 {
32428   tree c;
32429
32430   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32431
32432   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32433   OMP_CLAUSE_CHAIN (c) = list;
32434   return c;
32435 }
32436
32437 /* OpenMP 2.5:
32438    num_threads ( expression ) */
32439
32440 static tree
32441 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32442                                   location_t location)
32443 {
32444   tree t, c;
32445
32446   matching_parens parens;
32447   if (!parens.require_open (parser))
32448     return list;
32449
32450   t = cp_parser_expression (parser);
32451
32452   if (t == error_mark_node
32453       || !parens.require_close (parser))
32454     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32455                                            /*or_comma=*/false,
32456                                            /*consume_paren=*/true);
32457
32458   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32459                              "num_threads", location);
32460
32461   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32462   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32463   OMP_CLAUSE_CHAIN (c) = list;
32464
32465   return c;
32466 }
32467
32468 /* OpenMP 4.5:
32469    num_tasks ( expression ) */
32470
32471 static tree
32472 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32473                                 location_t location)
32474 {
32475   tree t, c;
32476
32477   matching_parens parens;
32478   if (!parens.require_open (parser))
32479     return list;
32480
32481   t = cp_parser_expression (parser);
32482
32483   if (t == error_mark_node
32484       || !parens.require_close (parser))
32485     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32486                                            /*or_comma=*/false,
32487                                            /*consume_paren=*/true);
32488
32489   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32490                              "num_tasks", location);
32491
32492   c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32493   OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32494   OMP_CLAUSE_CHAIN (c) = list;
32495
32496   return c;
32497 }
32498
32499 /* OpenMP 4.5:
32500    grainsize ( expression ) */
32501
32502 static tree
32503 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32504                                 location_t location)
32505 {
32506   tree t, c;
32507
32508   matching_parens parens;
32509   if (!parens.require_open (parser))
32510     return list;
32511
32512   t = cp_parser_expression (parser);
32513
32514   if (t == error_mark_node
32515       || !parens.require_close (parser))
32516     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32517                                            /*or_comma=*/false,
32518                                            /*consume_paren=*/true);
32519
32520   check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32521                              "grainsize", location);
32522
32523   c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32524   OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32525   OMP_CLAUSE_CHAIN (c) = list;
32526
32527   return c;
32528 }
32529
32530 /* OpenMP 4.5:
32531    priority ( expression ) */
32532
32533 static tree
32534 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32535                                location_t location)
32536 {
32537   tree t, c;
32538
32539   matching_parens parens;
32540   if (!parens.require_open (parser))
32541     return list;
32542
32543   t = cp_parser_expression (parser);
32544
32545   if (t == error_mark_node
32546       || !parens.require_close (parser))
32547     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32548                                            /*or_comma=*/false,
32549                                            /*consume_paren=*/true);
32550
32551   check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32552                              "priority", location);
32553
32554   c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32555   OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32556   OMP_CLAUSE_CHAIN (c) = list;
32557
32558   return c;
32559 }
32560
32561 /* OpenMP 4.5:
32562    hint ( expression ) */
32563
32564 static tree
32565 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32566                            location_t location)
32567 {
32568   tree t, c;
32569
32570   matching_parens parens;
32571   if (!parens.require_open (parser))
32572     return list;
32573
32574   t = cp_parser_expression (parser);
32575
32576   if (t == error_mark_node
32577       || !parens.require_close (parser))
32578     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32579                                            /*or_comma=*/false,
32580                                            /*consume_paren=*/true);
32581
32582   check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32583
32584   c = build_omp_clause (location, OMP_CLAUSE_HINT);
32585   OMP_CLAUSE_HINT_EXPR (c) = t;
32586   OMP_CLAUSE_CHAIN (c) = list;
32587
32588   return c;
32589 }
32590
32591 /* OpenMP 4.5:
32592    defaultmap ( tofrom : scalar ) */
32593
32594 static tree
32595 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32596                                  location_t location)
32597 {
32598   tree c, id;
32599   const char *p;
32600
32601   matching_parens parens;
32602   if (!parens.require_open (parser))
32603     return list;
32604
32605   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32606     {
32607       cp_parser_error (parser, "expected %<tofrom%>");
32608       goto out_err;
32609     }
32610   id = cp_lexer_peek_token (parser->lexer)->u.value;
32611   p = IDENTIFIER_POINTER (id);
32612   if (strcmp (p, "tofrom") != 0)
32613     {
32614       cp_parser_error (parser, "expected %<tofrom%>");
32615       goto out_err;
32616     }
32617   cp_lexer_consume_token (parser->lexer);
32618   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32619     goto out_err;
32620
32621   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32622     {
32623       cp_parser_error (parser, "expected %<scalar%>");
32624       goto out_err;
32625     }
32626   id = cp_lexer_peek_token (parser->lexer)->u.value;
32627   p = IDENTIFIER_POINTER (id);
32628   if (strcmp (p, "scalar") != 0)
32629     {
32630       cp_parser_error (parser, "expected %<scalar%>");
32631       goto out_err;
32632     }
32633   cp_lexer_consume_token (parser->lexer);
32634   if (!parens.require_close (parser))
32635     goto out_err;
32636
32637   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32638                              location);
32639
32640   c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32641   OMP_CLAUSE_CHAIN (c) = list;
32642   return c;
32643
32644  out_err:
32645   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32646                                          /*or_comma=*/false,
32647                                          /*consume_paren=*/true);
32648   return list;
32649 }
32650
32651 /* OpenMP 2.5:
32652    ordered
32653
32654    OpenMP 4.5:
32655    ordered ( constant-expression ) */
32656
32657 static tree
32658 cp_parser_omp_clause_ordered (cp_parser *parser,
32659                               tree list, location_t location)
32660 {
32661   tree c, num = NULL_TREE;
32662   HOST_WIDE_INT n;
32663
32664   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32665                              "ordered", location);
32666
32667   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32668     {
32669       matching_parens parens;
32670       parens.consume_open (parser);
32671
32672       num = cp_parser_constant_expression (parser);
32673
32674       if (!parens.require_close (parser))
32675         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32676                                                /*or_comma=*/false,
32677                                                /*consume_paren=*/true);
32678
32679       if (num == error_mark_node)
32680         return list;
32681       num = fold_non_dependent_expr (num);
32682       if (!tree_fits_shwi_p (num)
32683           || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32684           || (n = tree_to_shwi (num)) <= 0
32685           || (int) n != n)
32686         {
32687           error_at (location,
32688                     "ordered argument needs positive constant integer "
32689                     "expression");
32690           return list;
32691         }
32692     }
32693
32694   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32695   OMP_CLAUSE_ORDERED_EXPR (c) = num;
32696   OMP_CLAUSE_CHAIN (c) = list;
32697   return c;
32698 }
32699
32700 /* OpenMP 2.5:
32701    reduction ( reduction-operator : variable-list )
32702
32703    reduction-operator:
32704      One of: + * - & ^ | && ||
32705
32706    OpenMP 3.1:
32707
32708    reduction-operator:
32709      One of: + * - & ^ | && || min max
32710
32711    OpenMP 4.0:
32712
32713    reduction-operator:
32714      One of: + * - & ^ | && ||
32715      id-expression  */
32716
32717 static tree
32718 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32719 {
32720   enum tree_code code = ERROR_MARK;
32721   tree nlist, c, id = NULL_TREE;
32722
32723   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32724     return list;
32725
32726   switch (cp_lexer_peek_token (parser->lexer)->type)
32727     {
32728     case CPP_PLUS: code = PLUS_EXPR; break;
32729     case CPP_MULT: code = MULT_EXPR; break;
32730     case CPP_MINUS: code = MINUS_EXPR; break;
32731     case CPP_AND: code = BIT_AND_EXPR; break;
32732     case CPP_XOR: code = BIT_XOR_EXPR; break;
32733     case CPP_OR: code = BIT_IOR_EXPR; break;
32734     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32735     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32736     default: break;
32737     }
32738
32739   if (code != ERROR_MARK)
32740     cp_lexer_consume_token (parser->lexer);
32741   else
32742     {
32743       bool saved_colon_corrects_to_scope_p;
32744       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32745       parser->colon_corrects_to_scope_p = false;
32746       id = cp_parser_id_expression (parser, /*template_p=*/false,
32747                                     /*check_dependency_p=*/true,
32748                                     /*template_p=*/NULL,
32749                                     /*declarator_p=*/false,
32750                                     /*optional_p=*/false);
32751       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32752       if (identifier_p (id))
32753         {
32754           const char *p = IDENTIFIER_POINTER (id);
32755
32756           if (strcmp (p, "min") == 0)
32757             code = MIN_EXPR;
32758           else if (strcmp (p, "max") == 0)
32759             code = MAX_EXPR;
32760           else if (id == ovl_op_identifier (false, PLUS_EXPR))
32761             code = PLUS_EXPR;
32762           else if (id == ovl_op_identifier (false, MULT_EXPR))
32763             code = MULT_EXPR;
32764           else if (id == ovl_op_identifier (false, MINUS_EXPR))
32765             code = MINUS_EXPR;
32766           else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32767             code = BIT_AND_EXPR;
32768           else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32769             code = BIT_IOR_EXPR;
32770           else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32771             code = BIT_XOR_EXPR;
32772           else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32773             code = TRUTH_ANDIF_EXPR;
32774           else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32775             code = TRUTH_ORIF_EXPR;
32776           id = omp_reduction_id (code, id, NULL_TREE);
32777           tree scope = parser->scope;
32778           if (scope)
32779             id = build_qualified_name (NULL_TREE, scope, id, false);
32780           parser->scope = NULL_TREE;
32781           parser->qualifying_scope = NULL_TREE;
32782           parser->object_scope = NULL_TREE;
32783         }
32784       else
32785         {
32786           error ("invalid reduction-identifier");
32787          resync_fail:
32788           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32789                                                  /*or_comma=*/false,
32790                                                  /*consume_paren=*/true);
32791           return list;
32792         }
32793     }
32794
32795   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32796     goto resync_fail;
32797
32798   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32799                                           NULL);
32800   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32801     {
32802       OMP_CLAUSE_REDUCTION_CODE (c) = code;
32803       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32804     }
32805
32806   return nlist;
32807 }
32808
32809 /* OpenMP 2.5:
32810    schedule ( schedule-kind )
32811    schedule ( schedule-kind , expression )
32812
32813    schedule-kind:
32814      static | dynamic | guided | runtime | auto
32815
32816    OpenMP 4.5:
32817    schedule ( schedule-modifier : schedule-kind )
32818    schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32819
32820    schedule-modifier:
32821      simd
32822      monotonic
32823      nonmonotonic  */
32824
32825 static tree
32826 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32827 {
32828   tree c, t;
32829   int modifiers = 0, nmodifiers = 0;
32830
32831   matching_parens parens;
32832   if (!parens.require_open (parser))
32833     return list;
32834
32835   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32836
32837   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32838     {
32839       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32840       const char *p = IDENTIFIER_POINTER (id);
32841       if (strcmp ("simd", p) == 0)
32842         OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32843       else if (strcmp ("monotonic", p) == 0)
32844         modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32845       else if (strcmp ("nonmonotonic", p) == 0)
32846         modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32847       else
32848         break;
32849       cp_lexer_consume_token (parser->lexer);
32850       if (nmodifiers++ == 0
32851           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32852         cp_lexer_consume_token (parser->lexer);
32853       else
32854         {
32855           cp_parser_require (parser, CPP_COLON, RT_COLON);
32856           break;
32857         }
32858     }
32859
32860   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32861     {
32862       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32863       const char *p = IDENTIFIER_POINTER (id);
32864
32865       switch (p[0])
32866         {
32867         case 'd':
32868           if (strcmp ("dynamic", p) != 0)
32869             goto invalid_kind;
32870           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32871           break;
32872
32873         case 'g':
32874           if (strcmp ("guided", p) != 0)
32875             goto invalid_kind;
32876           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32877           break;
32878
32879         case 'r':
32880           if (strcmp ("runtime", p) != 0)
32881             goto invalid_kind;
32882           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32883           break;
32884
32885         default:
32886           goto invalid_kind;
32887         }
32888     }
32889   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32890     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32891   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32892     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32893   else
32894     goto invalid_kind;
32895   cp_lexer_consume_token (parser->lexer);
32896
32897   if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32898                     | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32899       == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32900           | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32901     {
32902       error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32903                           "specified");
32904       modifiers = 0;
32905     }
32906
32907   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32908     {
32909       cp_token *token;
32910       cp_lexer_consume_token (parser->lexer);
32911
32912       token = cp_lexer_peek_token (parser->lexer);
32913       t = cp_parser_assignment_expression (parser);
32914
32915       if (t == error_mark_node)
32916         goto resync_fail;
32917       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32918         error_at (token->location, "schedule %<runtime%> does not take "
32919                   "a %<chunk_size%> parameter");
32920       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32921         error_at (token->location, "schedule %<auto%> does not take "
32922                   "a %<chunk_size%> parameter");
32923       else
32924         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32925
32926       if (!parens.require_close (parser))
32927         goto resync_fail;
32928     }
32929   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32930     goto resync_fail;
32931
32932   OMP_CLAUSE_SCHEDULE_KIND (c)
32933     = (enum omp_clause_schedule_kind)
32934       (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32935
32936   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32937   OMP_CLAUSE_CHAIN (c) = list;
32938   return c;
32939
32940  invalid_kind:
32941   cp_parser_error (parser, "invalid schedule kind");
32942  resync_fail:
32943   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32944                                          /*or_comma=*/false,
32945                                          /*consume_paren=*/true);
32946   return list;
32947 }
32948
32949 /* OpenMP 3.0:
32950    untied */
32951
32952 static tree
32953 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32954                              tree list, location_t location)
32955 {
32956   tree c;
32957
32958   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32959
32960   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32961   OMP_CLAUSE_CHAIN (c) = list;
32962   return c;
32963 }
32964
32965 /* OpenMP 4.0:
32966    inbranch
32967    notinbranch */
32968
32969 static tree
32970 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32971                              tree list, location_t location)
32972 {
32973   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32974   tree c = build_omp_clause (location, code);
32975   OMP_CLAUSE_CHAIN (c) = list;
32976   return c;
32977 }
32978
32979 /* OpenMP 4.0:
32980    parallel
32981    for
32982    sections
32983    taskgroup */
32984
32985 static tree
32986 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32987                                  enum omp_clause_code code,
32988                                  tree list, location_t location)
32989 {
32990   tree c = build_omp_clause (location, code);
32991   OMP_CLAUSE_CHAIN (c) = list;
32992   return c;
32993 }
32994
32995 /* OpenMP 4.5:
32996    nogroup */
32997
32998 static tree
32999 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33000                               tree list, location_t location)
33001 {
33002   check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33003   tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33004   OMP_CLAUSE_CHAIN (c) = list;
33005   return c;
33006 }
33007
33008 /* OpenMP 4.5:
33009    simd
33010    threads */
33011
33012 static tree
33013 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33014                                   enum omp_clause_code code,
33015                                   tree list, location_t location)
33016 {
33017   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33018   tree c = build_omp_clause (location, code);
33019   OMP_CLAUSE_CHAIN (c) = list;
33020   return c;
33021 }
33022
33023 /* OpenMP 4.0:
33024    num_teams ( expression ) */
33025
33026 static tree
33027 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33028                                 location_t location)
33029 {
33030   tree t, c;
33031
33032   matching_parens parens;
33033   if (!parens.require_open (parser))
33034     return list;
33035
33036   t = cp_parser_expression (parser);
33037
33038   if (t == error_mark_node
33039       || !parens.require_close (parser))
33040     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33041                                            /*or_comma=*/false,
33042                                            /*consume_paren=*/true);
33043
33044   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33045                              "num_teams", location);
33046
33047   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33048   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33049   OMP_CLAUSE_CHAIN (c) = list;
33050
33051   return c;
33052 }
33053
33054 /* OpenMP 4.0:
33055    thread_limit ( expression ) */
33056
33057 static tree
33058 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33059                                    location_t location)
33060 {
33061   tree t, c;
33062
33063   matching_parens parens;
33064   if (!parens.require_open (parser))
33065     return list;
33066
33067   t = cp_parser_expression (parser);
33068
33069   if (t == error_mark_node
33070       || !parens.require_close (parser))
33071     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33072                                            /*or_comma=*/false,
33073                                            /*consume_paren=*/true);
33074
33075   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33076                              "thread_limit", location);
33077
33078   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33079   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33080   OMP_CLAUSE_CHAIN (c) = list;
33081
33082   return c;
33083 }
33084
33085 /* OpenMP 4.0:
33086    aligned ( variable-list )
33087    aligned ( variable-list : constant-expression )  */
33088
33089 static tree
33090 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33091 {
33092   tree nlist, c, alignment = NULL_TREE;
33093   bool colon;
33094
33095   matching_parens parens;
33096   if (!parens.require_open (parser))
33097     return list;
33098
33099   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33100                                           &colon);
33101
33102   if (colon)
33103     {
33104       alignment = cp_parser_constant_expression (parser);
33105
33106       if (!parens.require_close (parser))
33107         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33108                                                /*or_comma=*/false,
33109                                                /*consume_paren=*/true);
33110
33111       if (alignment == error_mark_node)
33112         alignment = NULL_TREE;
33113     }
33114
33115   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33116     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33117
33118   return nlist;
33119 }
33120
33121 /* OpenMP 4.0:
33122    linear ( variable-list )
33123    linear ( variable-list : expression )
33124
33125    OpenMP 4.5:
33126    linear ( modifier ( variable-list ) )
33127    linear ( modifier ( variable-list ) : expression ) */
33128
33129 static tree
33130 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
33131                              bool declare_simd)
33132 {
33133   tree nlist, c, step = integer_one_node;
33134   bool colon;
33135   enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33136
33137   matching_parens parens;
33138   if (!parens.require_open (parser))
33139     return list;
33140
33141   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33142     {
33143       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33144       const char *p = IDENTIFIER_POINTER (id);
33145
33146       if (strcmp ("ref", p) == 0)
33147         kind = OMP_CLAUSE_LINEAR_REF;
33148       else if (strcmp ("val", p) == 0)
33149         kind = OMP_CLAUSE_LINEAR_VAL;
33150       else if (strcmp ("uval", p) == 0)
33151         kind = OMP_CLAUSE_LINEAR_UVAL;
33152       if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33153         cp_lexer_consume_token (parser->lexer);
33154       else
33155         kind = OMP_CLAUSE_LINEAR_DEFAULT;
33156     }
33157
33158   if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33159     nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33160                                             &colon);
33161   else
33162     {
33163       nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33164       colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33165       if (colon)
33166         cp_parser_require (parser, CPP_COLON, RT_COLON);
33167       else if (!parens.require_close (parser))
33168         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33169                                                /*or_comma=*/false,
33170                                                /*consume_paren=*/true);
33171     }
33172
33173   if (colon)
33174     {
33175       step = NULL_TREE;
33176       if (declare_simd
33177           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33178           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33179         {
33180           cp_token *token = cp_lexer_peek_token (parser->lexer);
33181           cp_parser_parse_tentatively (parser);
33182           step = cp_parser_id_expression (parser, /*template_p=*/false,
33183                                           /*check_dependency_p=*/true,
33184                                           /*template_p=*/NULL,
33185                                           /*declarator_p=*/false,
33186                                           /*optional_p=*/false);
33187           if (step != error_mark_node)
33188             step = cp_parser_lookup_name_simple (parser, step, token->location);
33189           if (step == error_mark_node)
33190             {
33191               step = NULL_TREE;
33192               cp_parser_abort_tentative_parse (parser);
33193             }
33194           else if (!cp_parser_parse_definitely (parser))
33195             step = NULL_TREE;
33196         }
33197       if (!step)
33198         step = cp_parser_expression (parser);
33199
33200       if (!parens.require_close (parser))
33201         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33202                                                /*or_comma=*/false,
33203                                                /*consume_paren=*/true);
33204
33205       if (step == error_mark_node)
33206         return list;
33207     }
33208
33209   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33210     {
33211       OMP_CLAUSE_LINEAR_STEP (c) = step;
33212       OMP_CLAUSE_LINEAR_KIND (c) = kind;
33213     }
33214
33215   return nlist;
33216 }
33217
33218 /* OpenMP 4.0:
33219    safelen ( constant-expression )  */
33220
33221 static tree
33222 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33223                               location_t location)
33224 {
33225   tree t, c;
33226
33227   matching_parens parens;
33228   if (!parens.require_open (parser))
33229     return list;
33230
33231   t = cp_parser_constant_expression (parser);
33232
33233   if (t == error_mark_node
33234       || !parens.require_close (parser))
33235     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33236                                            /*or_comma=*/false,
33237                                            /*consume_paren=*/true);
33238
33239   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33240
33241   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33242   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33243   OMP_CLAUSE_CHAIN (c) = list;
33244
33245   return c;
33246 }
33247
33248 /* OpenMP 4.0:
33249    simdlen ( constant-expression )  */
33250
33251 static tree
33252 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33253                               location_t location)
33254 {
33255   tree t, c;
33256
33257   matching_parens parens;
33258   if (!parens.require_open (parser))
33259     return list;
33260
33261   t = cp_parser_constant_expression (parser);
33262
33263   if (t == error_mark_node
33264       || !parens.require_close (parser))
33265     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33266                                            /*or_comma=*/false,
33267                                            /*consume_paren=*/true);
33268
33269   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33270
33271   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33272   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33273   OMP_CLAUSE_CHAIN (c) = list;
33274
33275   return c;
33276 }
33277
33278 /* OpenMP 4.5:
33279    vec:
33280      identifier [+/- integer]
33281      vec , identifier [+/- integer]
33282 */
33283
33284 static tree
33285 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33286                                   tree list)
33287 {
33288   tree vec = NULL;
33289
33290   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33291     {
33292       cp_parser_error (parser, "expected identifier");
33293       return list;
33294     }
33295
33296   while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33297     {
33298       location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33299       tree t, identifier = cp_parser_identifier (parser);
33300       tree addend = NULL;
33301
33302       if (identifier == error_mark_node)
33303         t = error_mark_node;
33304       else
33305         {
33306           t = cp_parser_lookup_name_simple
33307                 (parser, identifier,
33308                  cp_lexer_peek_token (parser->lexer)->location);
33309           if (t == error_mark_node)
33310             cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33311                                          id_loc);
33312         }
33313
33314       bool neg = false;
33315       if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33316         neg = true;
33317       else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33318         {
33319           addend = integer_zero_node;
33320           goto add_to_vector;
33321         }
33322       cp_lexer_consume_token (parser->lexer);
33323
33324       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33325         {
33326           cp_parser_error (parser, "expected integer");
33327           return list;
33328         }
33329
33330       addend = cp_lexer_peek_token (parser->lexer)->u.value;
33331       if (TREE_CODE (addend) != INTEGER_CST)
33332         {
33333           cp_parser_error (parser, "expected integer");
33334           return list;
33335         }
33336       cp_lexer_consume_token (parser->lexer);
33337
33338     add_to_vector:
33339       if (t != error_mark_node)
33340         {
33341           vec = tree_cons (addend, t, vec);
33342           if (neg)
33343             OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33344         }
33345
33346       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33347         break;
33348
33349       cp_lexer_consume_token (parser->lexer);
33350     }
33351
33352   if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33353     {
33354       tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33355       OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33356       OMP_CLAUSE_DECL (u) = nreverse (vec);
33357       OMP_CLAUSE_CHAIN (u) = list;
33358       return u;
33359     }
33360   return list;
33361 }
33362
33363 /* OpenMP 4.0:
33364    depend ( depend-kind : variable-list )
33365
33366    depend-kind:
33367      in | out | inout
33368
33369    OpenMP 4.5:
33370    depend ( source )
33371
33372    depend ( sink : vec ) */
33373
33374 static tree
33375 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33376 {
33377   tree nlist, c;
33378   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33379
33380   matching_parens parens;
33381   if (!parens.require_open (parser))
33382     return list;
33383
33384   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33385     {
33386       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33387       const char *p = IDENTIFIER_POINTER (id);
33388
33389       if (strcmp ("in", p) == 0)
33390         kind = OMP_CLAUSE_DEPEND_IN;
33391       else if (strcmp ("inout", p) == 0)
33392         kind = OMP_CLAUSE_DEPEND_INOUT;
33393       else if (strcmp ("out", p) == 0)
33394         kind = OMP_CLAUSE_DEPEND_OUT;
33395       else if (strcmp ("source", p) == 0)
33396         kind = OMP_CLAUSE_DEPEND_SOURCE;
33397       else if (strcmp ("sink", p) == 0)
33398         kind = OMP_CLAUSE_DEPEND_SINK;
33399       else
33400         goto invalid_kind;
33401     }
33402   else
33403     goto invalid_kind;
33404
33405   cp_lexer_consume_token (parser->lexer);
33406
33407   if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33408     {
33409       c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33410       OMP_CLAUSE_DEPEND_KIND (c) = kind;
33411       OMP_CLAUSE_DECL (c) = NULL_TREE;
33412       OMP_CLAUSE_CHAIN (c) = list;
33413       if (!parens.require_close (parser))
33414         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33415                                                /*or_comma=*/false,
33416                                                /*consume_paren=*/true);
33417       return c;
33418     }
33419
33420   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33421     goto resync_fail;
33422
33423   if (kind == OMP_CLAUSE_DEPEND_SINK)
33424     nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33425   else
33426     {
33427       nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33428                                               list, NULL);
33429
33430       for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33431         OMP_CLAUSE_DEPEND_KIND (c) = kind;
33432     }
33433   return nlist;
33434
33435  invalid_kind:
33436   cp_parser_error (parser, "invalid depend kind");
33437  resync_fail:
33438   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33439                                          /*or_comma=*/false,
33440                                          /*consume_paren=*/true);
33441   return list;
33442 }
33443
33444 /* OpenMP 4.0:
33445    map ( map-kind : variable-list )
33446    map ( variable-list )
33447
33448    map-kind:
33449      alloc | to | from | tofrom
33450
33451    OpenMP 4.5:
33452    map-kind:
33453      alloc | to | from | tofrom | release | delete
33454
33455    map ( always [,] map-kind: variable-list ) */
33456
33457 static tree
33458 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33459 {
33460   tree nlist, c;
33461   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33462   bool always = false;
33463
33464   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33465     return list;
33466
33467   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33468     {
33469       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33470       const char *p = IDENTIFIER_POINTER (id);
33471
33472       if (strcmp ("always", p) == 0)
33473         {
33474           int nth = 2;
33475           if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33476             nth++;
33477           if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33478                || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33479                    == RID_DELETE))
33480               && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33481                   == CPP_COLON))
33482             {
33483               always = true;
33484               cp_lexer_consume_token (parser->lexer);
33485               if (nth == 3)
33486                 cp_lexer_consume_token (parser->lexer);
33487             }
33488         }
33489     }
33490
33491   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33492       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33493     {
33494       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33495       const char *p = IDENTIFIER_POINTER (id);
33496
33497       if (strcmp ("alloc", p) == 0)
33498         kind = GOMP_MAP_ALLOC;
33499       else if (strcmp ("to", p) == 0)
33500         kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33501       else if (strcmp ("from", p) == 0)
33502         kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33503       else if (strcmp ("tofrom", p) == 0)
33504         kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33505       else if (strcmp ("release", p) == 0)
33506         kind = GOMP_MAP_RELEASE;
33507       else
33508         {
33509           cp_parser_error (parser, "invalid map kind");
33510           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33511                                                  /*or_comma=*/false,
33512                                                  /*consume_paren=*/true);
33513           return list;
33514         }
33515       cp_lexer_consume_token (parser->lexer);
33516       cp_lexer_consume_token (parser->lexer);
33517     }
33518   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33519            && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33520     {
33521       kind = GOMP_MAP_DELETE;
33522       cp_lexer_consume_token (parser->lexer);
33523       cp_lexer_consume_token (parser->lexer);
33524     }
33525
33526   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33527                                           NULL);
33528
33529   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33530     OMP_CLAUSE_SET_MAP_KIND (c, kind);
33531
33532   return nlist;
33533 }
33534
33535 /* OpenMP 4.0:
33536    device ( expression ) */
33537
33538 static tree
33539 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33540                              location_t location)
33541 {
33542   tree t, c;
33543
33544   matching_parens parens;
33545   if (!parens.require_open (parser))
33546     return list;
33547
33548   t = cp_parser_expression (parser);
33549
33550   if (t == error_mark_node
33551       || !parens.require_close (parser))
33552     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33553                                            /*or_comma=*/false,
33554                                            /*consume_paren=*/true);
33555
33556   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33557                              "device", location);
33558
33559   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33560   OMP_CLAUSE_DEVICE_ID (c) = t;
33561   OMP_CLAUSE_CHAIN (c) = list;
33562
33563   return c;
33564 }
33565
33566 /* OpenMP 4.0:
33567    dist_schedule ( static )
33568    dist_schedule ( static , expression )  */
33569
33570 static tree
33571 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33572                                     location_t location)
33573 {
33574   tree c, t;
33575
33576   matching_parens parens;
33577   if (!parens.require_open (parser))
33578     return list;
33579
33580   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33581
33582   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33583     goto invalid_kind;
33584   cp_lexer_consume_token (parser->lexer);
33585
33586   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33587     {
33588       cp_lexer_consume_token (parser->lexer);
33589
33590       t = cp_parser_assignment_expression (parser);
33591
33592       if (t == error_mark_node)
33593         goto resync_fail;
33594       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33595
33596       if (!parens.require_close (parser))
33597         goto resync_fail;
33598     }
33599   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33600     goto resync_fail;
33601
33602   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33603                              location);
33604   OMP_CLAUSE_CHAIN (c) = list;
33605   return c;
33606
33607  invalid_kind:
33608   cp_parser_error (parser, "invalid dist_schedule kind");
33609  resync_fail:
33610   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33611                                          /*or_comma=*/false,
33612                                          /*consume_paren=*/true);
33613   return list;
33614 }
33615
33616 /* OpenMP 4.0:
33617    proc_bind ( proc-bind-kind )
33618
33619    proc-bind-kind:
33620      master | close | spread  */
33621
33622 static tree
33623 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33624                                 location_t location)
33625 {
33626   tree c;
33627   enum omp_clause_proc_bind_kind kind;
33628
33629   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33630     return list;
33631
33632   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33633     {
33634       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33635       const char *p = IDENTIFIER_POINTER (id);
33636
33637       if (strcmp ("master", p) == 0)
33638         kind = OMP_CLAUSE_PROC_BIND_MASTER;
33639       else if (strcmp ("close", p) == 0)
33640         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33641       else if (strcmp ("spread", p) == 0)
33642         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33643       else
33644         goto invalid_kind;
33645     }
33646   else
33647     goto invalid_kind;
33648
33649   cp_lexer_consume_token (parser->lexer);
33650   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33651     goto resync_fail;
33652
33653   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33654   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33655                              location);
33656   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33657   OMP_CLAUSE_CHAIN (c) = list;
33658   return c;
33659
33660  invalid_kind:
33661   cp_parser_error (parser, "invalid depend kind");
33662  resync_fail:
33663   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33664                                          /*or_comma=*/false,
33665                                          /*consume_paren=*/true);
33666   return list;
33667 }
33668
33669 /* OpenACC:
33670    async [( int-expr )] */
33671
33672 static tree
33673 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33674 {
33675   tree c, t;
33676   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33677
33678   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33679
33680   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33681     {
33682       matching_parens parens;
33683       parens.consume_open (parser);
33684
33685       t = cp_parser_expression (parser);
33686       if (t == error_mark_node
33687           || !parens.require_close (parser))
33688         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33689                                                 /*or_comma=*/false,
33690                                                 /*consume_paren=*/true);
33691     }
33692
33693   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33694
33695   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33696   OMP_CLAUSE_ASYNC_EXPR (c) = t;
33697   OMP_CLAUSE_CHAIN (c) = list;
33698   list = c;
33699
33700   return list;
33701 }
33702
33703 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
33704    is a bitmask in MASK.  Return the list of clauses found.  */
33705
33706 static tree
33707 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33708                            const char *where, cp_token *pragma_tok,
33709                            bool finish_p = true)
33710 {
33711   tree clauses = NULL;
33712   bool first = true;
33713
33714   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33715     {
33716       location_t here;
33717       pragma_omp_clause c_kind;
33718       omp_clause_code code;
33719       const char *c_name;
33720       tree prev = clauses;
33721
33722       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33723         cp_lexer_consume_token (parser->lexer);
33724
33725       here = cp_lexer_peek_token (parser->lexer)->location;
33726       c_kind = cp_parser_omp_clause_name (parser);
33727
33728       switch (c_kind)
33729         {
33730         case PRAGMA_OACC_CLAUSE_ASYNC:
33731           clauses = cp_parser_oacc_clause_async (parser, clauses);
33732           c_name = "async";
33733           break;
33734         case PRAGMA_OACC_CLAUSE_AUTO:
33735           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33736                                                  clauses, here);
33737           c_name = "auto";
33738           break;
33739         case PRAGMA_OACC_CLAUSE_COLLAPSE:
33740           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33741           c_name = "collapse";
33742           break;
33743         case PRAGMA_OACC_CLAUSE_COPY:
33744           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33745           c_name = "copy";
33746           break;
33747         case PRAGMA_OACC_CLAUSE_COPYIN:
33748           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33749           c_name = "copyin";
33750           break;
33751         case PRAGMA_OACC_CLAUSE_COPYOUT:
33752           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33753           c_name = "copyout";
33754           break;
33755         case PRAGMA_OACC_CLAUSE_CREATE:
33756           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33757           c_name = "create";
33758           break;
33759         case PRAGMA_OACC_CLAUSE_DELETE:
33760           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33761           c_name = "delete";
33762           break;
33763         case PRAGMA_OMP_CLAUSE_DEFAULT:
33764           clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33765           c_name = "default";
33766           break;
33767         case PRAGMA_OACC_CLAUSE_DEVICE:
33768           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33769           c_name = "device";
33770           break;
33771         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33772           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33773           c_name = "deviceptr";
33774           break;
33775         case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33776           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33777           c_name = "device_resident";
33778           break;
33779         case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33780           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33781                                             clauses);
33782           c_name = "firstprivate";
33783           break;
33784         case PRAGMA_OACC_CLAUSE_GANG:
33785           c_name = "gang";
33786           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33787                                                  c_name, clauses);
33788           break;
33789         case PRAGMA_OACC_CLAUSE_HOST:
33790           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33791           c_name = "host";
33792           break;
33793         case PRAGMA_OACC_CLAUSE_IF:
33794           clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33795           c_name = "if";
33796           break;
33797         case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33798           clauses = cp_parser_oacc_simple_clause (parser,
33799                                                   OMP_CLAUSE_INDEPENDENT,
33800                                                   clauses, here);
33801           c_name = "independent";
33802           break;
33803         case PRAGMA_OACC_CLAUSE_LINK:
33804           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33805           c_name = "link";
33806           break;
33807         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33808           code = OMP_CLAUSE_NUM_GANGS;
33809           c_name = "num_gangs";
33810           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33811                                                       clauses);
33812           break;
33813         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33814           c_name = "num_workers";
33815           code = OMP_CLAUSE_NUM_WORKERS;
33816           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33817                                                       clauses);
33818           break;
33819         case PRAGMA_OACC_CLAUSE_PRESENT:
33820           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33821           c_name = "present";
33822           break;
33823         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33824           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33825           c_name = "present_or_copy";
33826           break;
33827         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33828           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33829           c_name = "present_or_copyin";
33830           break;
33831         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33832           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33833           c_name = "present_or_copyout";
33834           break;
33835         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33836           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33837           c_name = "present_or_create";
33838           break;
33839         case PRAGMA_OACC_CLAUSE_PRIVATE:
33840           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33841                                             clauses);
33842           c_name = "private";
33843           break;
33844         case PRAGMA_OACC_CLAUSE_REDUCTION:
33845           clauses = cp_parser_omp_clause_reduction (parser, clauses);
33846           c_name = "reduction";
33847           break;
33848         case PRAGMA_OACC_CLAUSE_SELF:
33849           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33850           c_name = "self";
33851           break;
33852         case PRAGMA_OACC_CLAUSE_SEQ:
33853           clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33854                                                  clauses, here);
33855           c_name = "seq";
33856           break;
33857         case PRAGMA_OACC_CLAUSE_TILE:
33858           clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33859           c_name = "tile";
33860           break;
33861         case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33862           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33863                                             clauses);
33864           c_name = "use_device";
33865           break;
33866         case PRAGMA_OACC_CLAUSE_VECTOR:
33867           c_name = "vector";
33868           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33869                                                  c_name, clauses);
33870           break;
33871         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33872           c_name = "vector_length";
33873           code = OMP_CLAUSE_VECTOR_LENGTH;
33874           clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33875                                                       clauses);
33876           break;
33877         case PRAGMA_OACC_CLAUSE_WAIT:
33878           clauses = cp_parser_oacc_clause_wait (parser, clauses);
33879           c_name = "wait";
33880           break;
33881         case PRAGMA_OACC_CLAUSE_WORKER:
33882           c_name = "worker";
33883           clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33884                                                  c_name, clauses);
33885           break;
33886         default:
33887           cp_parser_error (parser, "expected %<#pragma acc%> clause");
33888           goto saw_error;
33889         }
33890
33891       first = false;
33892
33893       if (((mask >> c_kind) & 1) == 0)
33894         {
33895           /* Remove the invalid clause(s) from the list to avoid
33896              confusing the rest of the compiler.  */
33897           clauses = prev;
33898           error_at (here, "%qs is not valid for %qs", c_name, where);
33899         }
33900     }
33901
33902  saw_error:
33903   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33904
33905   if (finish_p)
33906     return finish_omp_clauses (clauses, C_ORT_ACC);
33907
33908   return clauses;
33909 }
33910
33911 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
33912    is a bitmask in MASK.  Return the list of clauses found; the result
33913    of clause default goes in *pdefault.  */
33914
33915 static tree
33916 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33917                            const char *where, cp_token *pragma_tok,
33918                            bool finish_p = true)
33919 {
33920   tree clauses = NULL;
33921   bool first = true;
33922   cp_token *token = NULL;
33923
33924   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33925     {
33926       pragma_omp_clause c_kind;
33927       const char *c_name;
33928       tree prev = clauses;
33929
33930       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33931         cp_lexer_consume_token (parser->lexer);
33932
33933       token = cp_lexer_peek_token (parser->lexer);
33934       c_kind = cp_parser_omp_clause_name (parser);
33935
33936       switch (c_kind)
33937         {
33938         case PRAGMA_OMP_CLAUSE_COLLAPSE:
33939           clauses = cp_parser_omp_clause_collapse (parser, clauses,
33940                                                    token->location);
33941           c_name = "collapse";
33942           break;
33943         case PRAGMA_OMP_CLAUSE_COPYIN:
33944           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33945           c_name = "copyin";
33946           break;
33947         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33948           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33949                                             clauses);
33950           c_name = "copyprivate";
33951           break;
33952         case PRAGMA_OMP_CLAUSE_DEFAULT:
33953           clauses = cp_parser_omp_clause_default (parser, clauses,
33954                                                   token->location, false);
33955           c_name = "default";
33956           break;
33957         case PRAGMA_OMP_CLAUSE_FINAL:
33958           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33959           c_name = "final";
33960           break;
33961         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33962           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33963                                             clauses);
33964           c_name = "firstprivate";
33965           break;
33966         case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33967           clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33968                                                     token->location);
33969           c_name = "grainsize";
33970           break;
33971         case PRAGMA_OMP_CLAUSE_HINT:
33972           clauses = cp_parser_omp_clause_hint (parser, clauses,
33973                                                token->location);
33974           c_name = "hint";
33975           break;
33976         case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33977           clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33978                                                      token->location);
33979           c_name = "defaultmap";
33980           break;
33981         case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33982           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33983                                             clauses);
33984           c_name = "use_device_ptr";
33985           break;
33986         case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33987           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33988                                             clauses);
33989           c_name = "is_device_ptr";
33990           break;
33991         case PRAGMA_OMP_CLAUSE_IF:
33992           clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33993                                              true);
33994           c_name = "if";
33995           break;
33996         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33997           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33998                                             clauses);
33999           c_name = "lastprivate";
34000           break;
34001         case PRAGMA_OMP_CLAUSE_MERGEABLE:
34002           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34003                                                     token->location);
34004           c_name = "mergeable";
34005           break;
34006         case PRAGMA_OMP_CLAUSE_NOWAIT:
34007           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34008           c_name = "nowait";
34009           break;
34010         case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34011           clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34012                                                     token->location);
34013           c_name = "num_tasks";
34014           break;
34015         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34016           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34017                                                       token->location);
34018           c_name = "num_threads";
34019           break;
34020         case PRAGMA_OMP_CLAUSE_ORDERED:
34021           clauses = cp_parser_omp_clause_ordered (parser, clauses,
34022                                                   token->location);
34023           c_name = "ordered";
34024           break;
34025         case PRAGMA_OMP_CLAUSE_PRIORITY:
34026           clauses = cp_parser_omp_clause_priority (parser, clauses,
34027                                                    token->location);
34028           c_name = "priority";
34029           break;
34030         case PRAGMA_OMP_CLAUSE_PRIVATE:
34031           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34032                                             clauses);
34033           c_name = "private";
34034           break;
34035         case PRAGMA_OMP_CLAUSE_REDUCTION:
34036           clauses = cp_parser_omp_clause_reduction (parser, clauses);
34037           c_name = "reduction";
34038           break;
34039         case PRAGMA_OMP_CLAUSE_SCHEDULE:
34040           clauses = cp_parser_omp_clause_schedule (parser, clauses,
34041                                                    token->location);
34042           c_name = "schedule";
34043           break;
34044         case PRAGMA_OMP_CLAUSE_SHARED:
34045           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34046                                             clauses);
34047           c_name = "shared";
34048           break;
34049         case PRAGMA_OMP_CLAUSE_UNTIED:
34050           clauses = cp_parser_omp_clause_untied (parser, clauses,
34051                                                  token->location);
34052           c_name = "untied";
34053           break;
34054         case PRAGMA_OMP_CLAUSE_INBRANCH:
34055           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34056                                                  clauses, token->location);
34057           c_name = "inbranch";
34058           break;
34059         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34060           clauses = cp_parser_omp_clause_branch (parser,
34061                                                  OMP_CLAUSE_NOTINBRANCH,
34062                                                  clauses, token->location);
34063           c_name = "notinbranch";
34064           break;
34065         case PRAGMA_OMP_CLAUSE_PARALLEL:
34066           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34067                                                      clauses, token->location);
34068           c_name = "parallel";
34069           if (!first)
34070             {
34071              clause_not_first:
34072               error_at (token->location, "%qs must be the first clause of %qs",
34073                         c_name, where);
34074               clauses = prev;
34075             }
34076           break;
34077         case PRAGMA_OMP_CLAUSE_FOR:
34078           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34079                                                      clauses, token->location);
34080           c_name = "for";
34081           if (!first)
34082             goto clause_not_first;
34083           break;
34084         case PRAGMA_OMP_CLAUSE_SECTIONS:
34085           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34086                                                      clauses, token->location);
34087           c_name = "sections";
34088           if (!first)
34089             goto clause_not_first;
34090           break;
34091         case PRAGMA_OMP_CLAUSE_TASKGROUP:
34092           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34093                                                      clauses, token->location);
34094           c_name = "taskgroup";
34095           if (!first)
34096             goto clause_not_first;
34097           break;
34098         case PRAGMA_OMP_CLAUSE_LINK:
34099           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34100           c_name = "to";
34101           break;
34102         case PRAGMA_OMP_CLAUSE_TO:
34103           if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34104             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34105                                               clauses);
34106           else
34107             clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34108           c_name = "to";
34109           break;
34110         case PRAGMA_OMP_CLAUSE_FROM:
34111           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34112           c_name = "from";
34113           break;
34114         case PRAGMA_OMP_CLAUSE_UNIFORM:
34115           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34116                                             clauses);
34117           c_name = "uniform";
34118           break;
34119         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34120           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34121                                                     token->location);
34122           c_name = "num_teams";
34123           break;
34124         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34125           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34126                                                        token->location);
34127           c_name = "thread_limit";
34128           break;
34129         case PRAGMA_OMP_CLAUSE_ALIGNED:
34130           clauses = cp_parser_omp_clause_aligned (parser, clauses);
34131           c_name = "aligned";
34132           break;
34133         case PRAGMA_OMP_CLAUSE_LINEAR:
34134           {
34135             bool declare_simd = false;
34136             if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34137               declare_simd = true;
34138             clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34139           }
34140           c_name = "linear";
34141           break;
34142         case PRAGMA_OMP_CLAUSE_DEPEND:
34143           clauses = cp_parser_omp_clause_depend (parser, clauses,
34144                                                  token->location);
34145           c_name = "depend";
34146           break;
34147         case PRAGMA_OMP_CLAUSE_MAP:
34148           clauses = cp_parser_omp_clause_map (parser, clauses);
34149           c_name = "map";
34150           break;
34151         case PRAGMA_OMP_CLAUSE_DEVICE:
34152           clauses = cp_parser_omp_clause_device (parser, clauses,
34153                                                  token->location);
34154           c_name = "device";
34155           break;
34156         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34157           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34158                                                         token->location);
34159           c_name = "dist_schedule";
34160           break;
34161         case PRAGMA_OMP_CLAUSE_PROC_BIND:
34162           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34163                                                     token->location);
34164           c_name = "proc_bind";
34165           break;
34166         case PRAGMA_OMP_CLAUSE_SAFELEN:
34167           clauses = cp_parser_omp_clause_safelen (parser, clauses,
34168                                                   token->location);
34169           c_name = "safelen";
34170           break;
34171         case PRAGMA_OMP_CLAUSE_SIMDLEN:
34172           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34173                                                   token->location);
34174           c_name = "simdlen";
34175           break;
34176         case PRAGMA_OMP_CLAUSE_NOGROUP:
34177           clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34178                                                   token->location);
34179           c_name = "nogroup";
34180           break;
34181         case PRAGMA_OMP_CLAUSE_THREADS:
34182           clauses
34183             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34184                                                 clauses, token->location);
34185           c_name = "threads";
34186           break;
34187         case PRAGMA_OMP_CLAUSE_SIMD:
34188           clauses
34189             = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34190                                                 clauses, token->location);
34191           c_name = "simd";
34192           break;
34193         default:
34194           cp_parser_error (parser, "expected %<#pragma omp%> clause");
34195           goto saw_error;
34196         }
34197
34198       first = false;
34199
34200       if (((mask >> c_kind) & 1) == 0)
34201         {
34202           /* Remove the invalid clause(s) from the list to avoid
34203              confusing the rest of the compiler.  */
34204           clauses = prev;
34205           error_at (token->location, "%qs is not valid for %qs", c_name, where);
34206         }
34207     }
34208  saw_error:
34209   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34210   if (finish_p)
34211     {
34212       if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34213         return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34214       else
34215         return finish_omp_clauses (clauses, C_ORT_OMP);
34216     }
34217   return clauses;
34218 }
34219
34220 /* OpenMP 2.5:
34221    structured-block:
34222      statement
34223
34224    In practice, we're also interested in adding the statement to an
34225    outer node.  So it is convenient if we work around the fact that
34226    cp_parser_statement calls add_stmt.  */
34227
34228 static unsigned
34229 cp_parser_begin_omp_structured_block (cp_parser *parser)
34230 {
34231   unsigned save = parser->in_statement;
34232
34233   /* Only move the values to IN_OMP_BLOCK if they weren't false.
34234      This preserves the "not within loop or switch" style error messages
34235      for nonsense cases like
34236         void foo() {
34237         #pragma omp single
34238           break;
34239         }
34240   */
34241   if (parser->in_statement)
34242     parser->in_statement = IN_OMP_BLOCK;
34243
34244   return save;
34245 }
34246
34247 static void
34248 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34249 {
34250   parser->in_statement = save;
34251 }
34252
34253 static tree
34254 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34255 {
34256   tree stmt = begin_omp_structured_block ();
34257   unsigned int save = cp_parser_begin_omp_structured_block (parser);
34258
34259   cp_parser_statement (parser, NULL_TREE, false, if_p);
34260
34261   cp_parser_end_omp_structured_block (parser, save);
34262   return finish_omp_structured_block (stmt);
34263 }
34264
34265 /* OpenMP 2.5:
34266    # pragma omp atomic new-line
34267      expression-stmt
34268
34269    expression-stmt:
34270      x binop= expr | x++ | ++x | x-- | --x
34271    binop:
34272      +, *, -, /, &, ^, |, <<, >>
34273
34274   where x is an lvalue expression with scalar type.
34275
34276    OpenMP 3.1:
34277    # pragma omp atomic new-line
34278      update-stmt
34279
34280    # pragma omp atomic read new-line
34281      read-stmt
34282
34283    # pragma omp atomic write new-line
34284      write-stmt
34285
34286    # pragma omp atomic update new-line
34287      update-stmt
34288
34289    # pragma omp atomic capture new-line
34290      capture-stmt
34291
34292    # pragma omp atomic capture new-line
34293      capture-block
34294
34295    read-stmt:
34296      v = x
34297    write-stmt:
34298      x = expr
34299    update-stmt:
34300      expression-stmt | x = x binop expr
34301    capture-stmt:
34302      v = expression-stmt
34303    capture-block:
34304      { v = x; update-stmt; } | { update-stmt; v = x; }
34305
34306    OpenMP 4.0:
34307    update-stmt:
34308      expression-stmt | x = x binop expr | x = expr binop x
34309    capture-stmt:
34310      v = update-stmt
34311    capture-block:
34312      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34313
34314   where x and v are lvalue expressions with scalar type.  */
34315
34316 static void
34317 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34318 {
34319   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34320   tree rhs1 = NULL_TREE, orig_lhs;
34321   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34322   bool structured_block = false;
34323   bool seq_cst = false;
34324
34325   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34326     {
34327       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34328       const char *p = IDENTIFIER_POINTER (id);
34329
34330       if (!strcmp (p, "seq_cst"))
34331         {
34332           seq_cst = true;
34333           cp_lexer_consume_token (parser->lexer);
34334           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34335               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34336             cp_lexer_consume_token (parser->lexer);
34337         }
34338     }
34339   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34340     {
34341       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34342       const char *p = IDENTIFIER_POINTER (id);
34343
34344       if (!strcmp (p, "read"))
34345         code = OMP_ATOMIC_READ;
34346       else if (!strcmp (p, "write"))
34347         code = NOP_EXPR;
34348       else if (!strcmp (p, "update"))
34349         code = OMP_ATOMIC;
34350       else if (!strcmp (p, "capture"))
34351         code = OMP_ATOMIC_CAPTURE_NEW;
34352       else
34353         p = NULL;
34354       if (p)
34355         cp_lexer_consume_token (parser->lexer);
34356     }
34357   if (!seq_cst)
34358     {
34359       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34360           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34361         cp_lexer_consume_token (parser->lexer);
34362
34363       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34364         {
34365           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34366           const char *p = IDENTIFIER_POINTER (id);
34367
34368           if (!strcmp (p, "seq_cst"))
34369             {
34370               seq_cst = true;
34371               cp_lexer_consume_token (parser->lexer);
34372             }
34373         }
34374     }
34375   cp_parser_require_pragma_eol (parser, pragma_tok);
34376
34377   switch (code)
34378     {
34379     case OMP_ATOMIC_READ:
34380     case NOP_EXPR: /* atomic write */
34381       v = cp_parser_unary_expression (parser);
34382       if (v == error_mark_node)
34383         goto saw_error;
34384       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34385         goto saw_error;
34386       if (code == NOP_EXPR)
34387         lhs = cp_parser_expression (parser);
34388       else
34389         lhs = cp_parser_unary_expression (parser);
34390       if (lhs == error_mark_node)
34391         goto saw_error;
34392       if (code == NOP_EXPR)
34393         {
34394           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34395              opcode.  */
34396           code = OMP_ATOMIC;
34397           rhs = lhs;
34398           lhs = v;
34399           v = NULL_TREE;
34400         }
34401       goto done;
34402     case OMP_ATOMIC_CAPTURE_NEW:
34403       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34404         {
34405           cp_lexer_consume_token (parser->lexer);
34406           structured_block = true;
34407         }
34408       else
34409         {
34410           v = cp_parser_unary_expression (parser);
34411           if (v == error_mark_node)
34412             goto saw_error;
34413           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34414             goto saw_error;
34415         }
34416     default:
34417       break;
34418     }
34419
34420 restart:
34421   lhs = cp_parser_unary_expression (parser);
34422   orig_lhs = lhs;
34423   switch (TREE_CODE (lhs))
34424     {
34425     case ERROR_MARK:
34426       goto saw_error;
34427
34428     case POSTINCREMENT_EXPR:
34429       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34430         code = OMP_ATOMIC_CAPTURE_OLD;
34431       /* FALLTHROUGH */
34432     case PREINCREMENT_EXPR:
34433       lhs = TREE_OPERAND (lhs, 0);
34434       opcode = PLUS_EXPR;
34435       rhs = integer_one_node;
34436       break;
34437
34438     case POSTDECREMENT_EXPR:
34439       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34440         code = OMP_ATOMIC_CAPTURE_OLD;
34441       /* FALLTHROUGH */
34442     case PREDECREMENT_EXPR:
34443       lhs = TREE_OPERAND (lhs, 0);
34444       opcode = MINUS_EXPR;
34445       rhs = integer_one_node;
34446       break;
34447
34448     case COMPOUND_EXPR:
34449       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34450          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34451          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34452          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34453          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34454                                              (TREE_OPERAND (lhs, 1), 0), 0)))
34455             == BOOLEAN_TYPE)
34456        /* Undo effects of boolean_increment for post {in,de}crement.  */
34457        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34458       /* FALLTHRU */
34459     case MODIFY_EXPR:
34460       if (TREE_CODE (lhs) == MODIFY_EXPR
34461          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34462         {
34463           /* Undo effects of boolean_increment.  */
34464           if (integer_onep (TREE_OPERAND (lhs, 1)))
34465             {
34466               /* This is pre or post increment.  */
34467               rhs = TREE_OPERAND (lhs, 1);
34468               lhs = TREE_OPERAND (lhs, 0);
34469               opcode = NOP_EXPR;
34470               if (code == OMP_ATOMIC_CAPTURE_NEW
34471                   && !structured_block
34472                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34473                 code = OMP_ATOMIC_CAPTURE_OLD;
34474               break;
34475             }
34476         }
34477       /* FALLTHRU */
34478     default:
34479       switch (cp_lexer_peek_token (parser->lexer)->type)
34480         {
34481         case CPP_MULT_EQ:
34482           opcode = MULT_EXPR;
34483           break;
34484         case CPP_DIV_EQ:
34485           opcode = TRUNC_DIV_EXPR;
34486           break;
34487         case CPP_PLUS_EQ:
34488           opcode = PLUS_EXPR;
34489           break;
34490         case CPP_MINUS_EQ:
34491           opcode = MINUS_EXPR;
34492           break;
34493         case CPP_LSHIFT_EQ:
34494           opcode = LSHIFT_EXPR;
34495           break;
34496         case CPP_RSHIFT_EQ:
34497           opcode = RSHIFT_EXPR;
34498           break;
34499         case CPP_AND_EQ:
34500           opcode = BIT_AND_EXPR;
34501           break;
34502         case CPP_OR_EQ:
34503           opcode = BIT_IOR_EXPR;
34504           break;
34505         case CPP_XOR_EQ:
34506           opcode = BIT_XOR_EXPR;
34507           break;
34508         case CPP_EQ:
34509           enum cp_parser_prec oprec;
34510           cp_token *token;
34511           cp_lexer_consume_token (parser->lexer);
34512           cp_parser_parse_tentatively (parser);
34513           rhs1 = cp_parser_simple_cast_expression (parser);
34514           if (rhs1 == error_mark_node)
34515             {
34516               cp_parser_abort_tentative_parse (parser);
34517               cp_parser_simple_cast_expression (parser);
34518               goto saw_error;
34519             }
34520           token = cp_lexer_peek_token (parser->lexer);
34521           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34522             {
34523               cp_parser_abort_tentative_parse (parser);
34524               cp_parser_parse_tentatively (parser);
34525               rhs = cp_parser_binary_expression (parser, false, true,
34526                                                  PREC_NOT_OPERATOR, NULL);
34527               if (rhs == error_mark_node)
34528                 {
34529                   cp_parser_abort_tentative_parse (parser);
34530                   cp_parser_binary_expression (parser, false, true,
34531                                                PREC_NOT_OPERATOR, NULL);
34532                   goto saw_error;
34533                 }
34534               switch (TREE_CODE (rhs))
34535                 {
34536                 case MULT_EXPR:
34537                 case TRUNC_DIV_EXPR:
34538                 case RDIV_EXPR:
34539                 case PLUS_EXPR:
34540                 case MINUS_EXPR:
34541                 case LSHIFT_EXPR:
34542                 case RSHIFT_EXPR:
34543                 case BIT_AND_EXPR:
34544                 case BIT_IOR_EXPR:
34545                 case BIT_XOR_EXPR:
34546                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34547                     {
34548                       if (cp_parser_parse_definitely (parser))
34549                         {
34550                           opcode = TREE_CODE (rhs);
34551                           rhs1 = TREE_OPERAND (rhs, 0);
34552                           rhs = TREE_OPERAND (rhs, 1);
34553                           goto stmt_done;
34554                         }
34555                       else
34556                         goto saw_error;
34557                     }
34558                   break;
34559                 default:
34560                   break;
34561                 }
34562               cp_parser_abort_tentative_parse (parser);
34563               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34564                 {
34565                   rhs = cp_parser_expression (parser);
34566                   if (rhs == error_mark_node)
34567                     goto saw_error;
34568                   opcode = NOP_EXPR;
34569                   rhs1 = NULL_TREE;
34570                   goto stmt_done;
34571                 }
34572               cp_parser_error (parser,
34573                                "invalid form of %<#pragma omp atomic%>");
34574               goto saw_error;
34575             }
34576           if (!cp_parser_parse_definitely (parser))
34577             goto saw_error;
34578           switch (token->type)
34579             {
34580             case CPP_SEMICOLON:
34581               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34582                 {
34583                   code = OMP_ATOMIC_CAPTURE_OLD;
34584                   v = lhs;
34585                   lhs = NULL_TREE;
34586                   lhs1 = rhs1;
34587                   rhs1 = NULL_TREE;
34588                   cp_lexer_consume_token (parser->lexer);
34589                   goto restart;
34590                 }
34591               else if (structured_block)
34592                 {
34593                   opcode = NOP_EXPR;
34594                   rhs = rhs1;
34595                   rhs1 = NULL_TREE;
34596                   goto stmt_done;
34597                 }
34598               cp_parser_error (parser,
34599                                "invalid form of %<#pragma omp atomic%>");
34600               goto saw_error;
34601             case CPP_MULT:
34602               opcode = MULT_EXPR;
34603               break;
34604             case CPP_DIV:
34605               opcode = TRUNC_DIV_EXPR;
34606               break;
34607             case CPP_PLUS:
34608               opcode = PLUS_EXPR;
34609               break;
34610             case CPP_MINUS:
34611               opcode = MINUS_EXPR;
34612               break;
34613             case CPP_LSHIFT:
34614               opcode = LSHIFT_EXPR;
34615               break;
34616             case CPP_RSHIFT:
34617               opcode = RSHIFT_EXPR;
34618               break;
34619             case CPP_AND:
34620               opcode = BIT_AND_EXPR;
34621               break;
34622             case CPP_OR:
34623               opcode = BIT_IOR_EXPR;
34624               break;
34625             case CPP_XOR:
34626               opcode = BIT_XOR_EXPR;
34627               break;
34628             default:
34629               cp_parser_error (parser,
34630                                "invalid operator for %<#pragma omp atomic%>");
34631               goto saw_error;
34632             }
34633           oprec = TOKEN_PRECEDENCE (token);
34634           gcc_assert (oprec != PREC_NOT_OPERATOR);
34635           if (commutative_tree_code (opcode))
34636             oprec = (enum cp_parser_prec) (oprec - 1);
34637           cp_lexer_consume_token (parser->lexer);
34638           rhs = cp_parser_binary_expression (parser, false, false,
34639                                              oprec, NULL);
34640           if (rhs == error_mark_node)
34641             goto saw_error;
34642           goto stmt_done;
34643           /* FALLTHROUGH */
34644         default:
34645           cp_parser_error (parser,
34646                            "invalid operator for %<#pragma omp atomic%>");
34647           goto saw_error;
34648         }
34649       cp_lexer_consume_token (parser->lexer);
34650
34651       rhs = cp_parser_expression (parser);
34652       if (rhs == error_mark_node)
34653         goto saw_error;
34654       break;
34655     }
34656 stmt_done:
34657   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34658     {
34659       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34660         goto saw_error;
34661       v = cp_parser_unary_expression (parser);
34662       if (v == error_mark_node)
34663         goto saw_error;
34664       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34665         goto saw_error;
34666       lhs1 = cp_parser_unary_expression (parser);
34667       if (lhs1 == error_mark_node)
34668         goto saw_error;
34669     }
34670   if (structured_block)
34671     {
34672       cp_parser_consume_semicolon_at_end_of_statement (parser);
34673       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34674     }
34675 done:
34676   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34677   if (!structured_block)
34678     cp_parser_consume_semicolon_at_end_of_statement (parser);
34679   return;
34680
34681  saw_error:
34682   cp_parser_skip_to_end_of_block_or_statement (parser);
34683   if (structured_block)
34684     {
34685       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34686         cp_lexer_consume_token (parser->lexer);
34687       else if (code == OMP_ATOMIC_CAPTURE_NEW)
34688         {
34689           cp_parser_skip_to_end_of_block_or_statement (parser);
34690           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34691             cp_lexer_consume_token (parser->lexer);
34692         }
34693     }
34694 }
34695
34696
34697 /* OpenMP 2.5:
34698    # pragma omp barrier new-line  */
34699
34700 static void
34701 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34702 {
34703   cp_parser_require_pragma_eol (parser, pragma_tok);
34704   finish_omp_barrier ();
34705 }
34706
34707 /* OpenMP 2.5:
34708    # pragma omp critical [(name)] new-line
34709      structured-block
34710
34711    OpenMP 4.5:
34712    # pragma omp critical [(name) [hint(expression)]] new-line
34713      structured-block  */
34714
34715 #define OMP_CRITICAL_CLAUSE_MASK                \
34716         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34717
34718 static tree
34719 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34720 {
34721   tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34722
34723   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34724     {
34725       matching_parens parens;
34726       parens.consume_open (parser);
34727
34728       name = cp_parser_identifier (parser);
34729
34730       if (name == error_mark_node
34731           || !parens.require_close (parser))
34732         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34733                                                /*or_comma=*/false,
34734                                                /*consume_paren=*/true);
34735       if (name == error_mark_node)
34736         name = NULL;
34737
34738       clauses = cp_parser_omp_all_clauses (parser,
34739                                            OMP_CRITICAL_CLAUSE_MASK,
34740                                            "#pragma omp critical", pragma_tok);
34741     }
34742   else
34743     cp_parser_require_pragma_eol (parser, pragma_tok);
34744
34745   stmt = cp_parser_omp_structured_block (parser, if_p);
34746   return c_finish_omp_critical (input_location, stmt, name, clauses);
34747 }
34748
34749 /* OpenMP 2.5:
34750    # pragma omp flush flush-vars[opt] new-line
34751
34752    flush-vars:
34753      ( variable-list ) */
34754
34755 static void
34756 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34757 {
34758   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34759     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34760   cp_parser_require_pragma_eol (parser, pragma_tok);
34761
34762   finish_omp_flush ();
34763 }
34764
34765 /* Helper function, to parse omp for increment expression.  */
34766
34767 static tree
34768 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34769 {
34770   tree cond = cp_parser_binary_expression (parser, false, true,
34771                                            PREC_NOT_OPERATOR, NULL);
34772   if (cond == error_mark_node
34773       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34774     {
34775       cp_parser_skip_to_end_of_statement (parser);
34776       return error_mark_node;
34777     }
34778
34779   switch (TREE_CODE (cond))
34780     {
34781     case GT_EXPR:
34782     case GE_EXPR:
34783     case LT_EXPR:
34784     case LE_EXPR:
34785       break;
34786     case NE_EXPR:
34787       /* Fall through: OpenMP disallows NE_EXPR.  */
34788       gcc_fallthrough ();
34789     default:
34790       return error_mark_node;
34791     }
34792
34793   /* If decl is an iterator, preserve LHS and RHS of the relational
34794      expr until finish_omp_for.  */
34795   if (decl
34796       && (type_dependent_expression_p (decl)
34797           || CLASS_TYPE_P (TREE_TYPE (decl))))
34798     return cond;
34799
34800   return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34801                             TREE_CODE (cond),
34802                             TREE_OPERAND (cond, 0), ERROR_MARK,
34803                             TREE_OPERAND (cond, 1), ERROR_MARK,
34804                             /*overload=*/NULL, tf_warning_or_error);
34805 }
34806
34807 /* Helper function, to parse omp for increment expression.  */
34808
34809 static tree
34810 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34811 {
34812   cp_token *token = cp_lexer_peek_token (parser->lexer);
34813   enum tree_code op;
34814   tree lhs, rhs;
34815   cp_id_kind idk;
34816   bool decl_first;
34817
34818   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34819     {
34820       op = (token->type == CPP_PLUS_PLUS
34821             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34822       cp_lexer_consume_token (parser->lexer);
34823       lhs = cp_parser_simple_cast_expression (parser);
34824       if (lhs != decl
34825           && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34826         return error_mark_node;
34827       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34828     }
34829
34830   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34831   if (lhs != decl
34832       && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34833     return error_mark_node;
34834
34835   token = cp_lexer_peek_token (parser->lexer);
34836   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34837     {
34838       op = (token->type == CPP_PLUS_PLUS
34839             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34840       cp_lexer_consume_token (parser->lexer);
34841       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34842     }
34843
34844   op = cp_parser_assignment_operator_opt (parser);
34845   if (op == ERROR_MARK)
34846     return error_mark_node;
34847
34848   if (op != NOP_EXPR)
34849     {
34850       rhs = cp_parser_assignment_expression (parser);
34851       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34852       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34853     }
34854
34855   lhs = cp_parser_binary_expression (parser, false, false,
34856                                      PREC_ADDITIVE_EXPRESSION, NULL);
34857   token = cp_lexer_peek_token (parser->lexer);
34858   decl_first = (lhs == decl
34859                 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34860   if (decl_first)
34861     lhs = NULL_TREE;
34862   if (token->type != CPP_PLUS
34863       && token->type != CPP_MINUS)
34864     return error_mark_node;
34865
34866   do
34867     {
34868       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34869       cp_lexer_consume_token (parser->lexer);
34870       rhs = cp_parser_binary_expression (parser, false, false,
34871                                          PREC_ADDITIVE_EXPRESSION, NULL);
34872       token = cp_lexer_peek_token (parser->lexer);
34873       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34874         {
34875           if (lhs == NULL_TREE)
34876             {
34877               if (op == PLUS_EXPR)
34878                 lhs = rhs;
34879               else
34880                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34881                                         tf_warning_or_error);
34882             }
34883           else
34884             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34885                                      ERROR_MARK, NULL, tf_warning_or_error);
34886         }
34887     }
34888   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34889
34890   if (!decl_first)
34891     {
34892       if ((rhs != decl
34893            && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34894           || op == MINUS_EXPR)
34895         return error_mark_node;
34896       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34897     }
34898   else
34899     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34900
34901   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34902 }
34903
34904 /* Parse the initialization statement of an OpenMP for loop.
34905
34906    Return true if the resulting construct should have an
34907    OMP_CLAUSE_PRIVATE added to it.  */
34908
34909 static tree
34910 cp_parser_omp_for_loop_init (cp_parser *parser,
34911                              tree &this_pre_body,
34912                              vec<tree, va_gc> *&for_block,
34913                              tree &init,
34914                              tree &orig_init,
34915                              tree &decl,
34916                              tree &real_decl)
34917 {
34918   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34919     return NULL_TREE;
34920
34921   tree add_private_clause = NULL_TREE;
34922
34923   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34924
34925      init-expr:
34926      var = lb
34927      integer-type var = lb
34928      random-access-iterator-type var = lb
34929      pointer-type var = lb
34930   */
34931   cp_decl_specifier_seq type_specifiers;
34932
34933   /* First, try to parse as an initialized declaration.  See
34934      cp_parser_condition, from whence the bulk of this is copied.  */
34935
34936   cp_parser_parse_tentatively (parser);
34937   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34938                                 /*is_trailing_return=*/false,
34939                                 &type_specifiers);
34940   if (cp_parser_parse_definitely (parser))
34941     {
34942       /* If parsing a type specifier seq succeeded, then this
34943          MUST be a initialized declaration.  */
34944       tree asm_specification, attributes;
34945       cp_declarator *declarator;
34946
34947       declarator = cp_parser_declarator (parser,
34948                                          CP_PARSER_DECLARATOR_NAMED,
34949                                          /*ctor_dtor_or_conv_p=*/NULL,
34950                                          /*parenthesized_p=*/NULL,
34951                                          /*member_p=*/false,
34952                                          /*friend_p=*/false);
34953       attributes = cp_parser_attributes_opt (parser);
34954       asm_specification = cp_parser_asm_specification_opt (parser);
34955
34956       if (declarator == cp_error_declarator) 
34957         cp_parser_skip_to_end_of_statement (parser);
34958
34959       else 
34960         {
34961           tree pushed_scope, auto_node;
34962
34963           decl = start_decl (declarator, &type_specifiers,
34964                              SD_INITIALIZED, attributes,
34965                              /*prefix_attributes=*/NULL_TREE,
34966                              &pushed_scope);
34967
34968           auto_node = type_uses_auto (TREE_TYPE (decl));
34969           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34970             {
34971               if (cp_lexer_next_token_is (parser->lexer, 
34972                                           CPP_OPEN_PAREN))
34973                 error ("parenthesized initialization is not allowed in "
34974                        "OpenMP %<for%> loop");
34975               else
34976                 /* Trigger an error.  */
34977                 cp_parser_require (parser, CPP_EQ, RT_EQ);
34978
34979               init = error_mark_node;
34980               cp_parser_skip_to_end_of_statement (parser);
34981             }
34982           else if (CLASS_TYPE_P (TREE_TYPE (decl))
34983                    || type_dependent_expression_p (decl)
34984                    || auto_node)
34985             {
34986               bool is_direct_init, is_non_constant_init;
34987
34988               init = cp_parser_initializer (parser,
34989                                             &is_direct_init,
34990                                             &is_non_constant_init);
34991
34992               if (auto_node)
34993                 {
34994                   TREE_TYPE (decl)
34995                     = do_auto_deduction (TREE_TYPE (decl), init,
34996                                          auto_node);
34997
34998                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
34999                       && !type_dependent_expression_p (decl))
35000                     goto non_class;
35001                 }
35002                       
35003               cp_finish_decl (decl, init, !is_non_constant_init,
35004                               asm_specification,
35005                               LOOKUP_ONLYCONVERTING);
35006               orig_init = init;
35007               if (CLASS_TYPE_P (TREE_TYPE (decl)))
35008                 {
35009                   vec_safe_push (for_block, this_pre_body);
35010                   init = NULL_TREE;
35011                 }
35012               else
35013                 {
35014                   init = pop_stmt_list (this_pre_body);
35015                   if (init && TREE_CODE (init) == STATEMENT_LIST)
35016                     {
35017                       tree_stmt_iterator i = tsi_start (init);
35018                       /* Move lambda DECL_EXPRs to FOR_BLOCK.  */
35019                       while (!tsi_end_p (i))
35020                         {
35021                           tree t = tsi_stmt (i);
35022                           if (TREE_CODE (t) == DECL_EXPR
35023                               && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35024                             {
35025                               tsi_delink (&i);
35026                               vec_safe_push (for_block, t);
35027                               continue;
35028                             }
35029                           break;
35030                         }
35031                       if (tsi_one_before_end_p (i))
35032                         {
35033                           tree t = tsi_stmt (i);
35034                           tsi_delink (&i);
35035                           free_stmt_list (init);
35036                           init = t;
35037                         }
35038                     }
35039                 }
35040               this_pre_body = NULL_TREE;
35041             }
35042           else
35043             {
35044               /* Consume '='.  */
35045               cp_lexer_consume_token (parser->lexer);
35046               init = cp_parser_assignment_expression (parser);
35047
35048             non_class:
35049               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
35050                 init = error_mark_node;
35051               else
35052                 cp_finish_decl (decl, NULL_TREE,
35053                                 /*init_const_expr_p=*/false,
35054                                 asm_specification,
35055                                 LOOKUP_ONLYCONVERTING);
35056             }
35057
35058           if (pushed_scope)
35059             pop_scope (pushed_scope);
35060         }
35061     }
35062   else 
35063     {
35064       cp_id_kind idk;
35065       /* If parsing a type specifier sequence failed, then
35066          this MUST be a simple expression.  */
35067       cp_parser_parse_tentatively (parser);
35068       decl = cp_parser_primary_expression (parser, false, false,
35069                                            false, &idk);
35070       cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35071       if (!cp_parser_error_occurred (parser)
35072           && decl
35073           && (TREE_CODE (decl) == COMPONENT_REF
35074               || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35075         {
35076           cp_parser_abort_tentative_parse (parser);
35077           cp_parser_parse_tentatively (parser);
35078           cp_token *token = cp_lexer_peek_token (parser->lexer);
35079           tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35080                                                /*check_dependency_p=*/true,
35081                                                /*template_p=*/NULL,
35082                                                /*declarator_p=*/false,
35083                                                /*optional_p=*/false);
35084           if (name != error_mark_node
35085               && last_tok == cp_lexer_peek_token (parser->lexer))
35086             {
35087               decl = cp_parser_lookup_name_simple (parser, name,
35088                                                    token->location);
35089               if (TREE_CODE (decl) == FIELD_DECL)
35090                 add_private_clause = omp_privatize_field (decl, false);
35091             }
35092           cp_parser_abort_tentative_parse (parser);
35093           cp_parser_parse_tentatively (parser);
35094           decl = cp_parser_primary_expression (parser, false, false,
35095                                                false, &idk);
35096         }
35097       if (!cp_parser_error_occurred (parser)
35098           && decl
35099           && DECL_P (decl)
35100           && CLASS_TYPE_P (TREE_TYPE (decl)))
35101         {
35102           tree rhs;
35103
35104           cp_parser_parse_definitely (parser);
35105           cp_parser_require (parser, CPP_EQ, RT_EQ);
35106           rhs = cp_parser_assignment_expression (parser);
35107           orig_init = rhs;
35108           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35109                                                  decl, NOP_EXPR,
35110                                                  rhs,
35111                                                  tf_warning_or_error));
35112           if (!add_private_clause)
35113             add_private_clause = decl;
35114         }
35115       else
35116         {
35117           decl = NULL;
35118           cp_parser_abort_tentative_parse (parser);
35119           init = cp_parser_expression (parser);
35120           if (init)
35121             {
35122               if (TREE_CODE (init) == MODIFY_EXPR
35123                   || TREE_CODE (init) == MODOP_EXPR)
35124                 real_decl = TREE_OPERAND (init, 0);
35125             }
35126         }
35127     }
35128   return add_private_clause;
35129 }
35130
35131 /* Parse the restricted form of the for statement allowed by OpenMP.  */
35132
35133 static tree
35134 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35135                         tree *cclauses, bool *if_p)
35136 {
35137   tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35138   tree real_decl, initv, condv, incrv, declv;
35139   tree this_pre_body, cl, ordered_cl = NULL_TREE;
35140   location_t loc_first;
35141   bool collapse_err = false;
35142   int i, collapse = 1, ordered = 0, count, nbraces = 0;
35143   vec<tree, va_gc> *for_block = make_tree_vector ();
35144   auto_vec<tree, 4> orig_inits;
35145   bool tiling = false;
35146
35147   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35148     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35149       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35150     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35151       {
35152         tiling = true;
35153         collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35154       }
35155     else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35156              && OMP_CLAUSE_ORDERED_EXPR (cl))
35157       {
35158         ordered_cl = cl;
35159         ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35160       }
35161
35162   if (ordered && ordered < collapse)
35163     {
35164       error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35165                 "%<ordered%> clause parameter is less than %<collapse%>");
35166       OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35167         = build_int_cst (NULL_TREE, collapse);
35168       ordered = collapse;
35169     }
35170   if (ordered)
35171     {
35172       for (tree *pc = &clauses; *pc; )
35173         if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35174           {
35175             error_at (OMP_CLAUSE_LOCATION (*pc),
35176                       "%<linear%> clause may not be specified together "
35177                       "with %<ordered%> clause with a parameter");
35178             *pc = OMP_CLAUSE_CHAIN (*pc);
35179           }
35180         else
35181           pc = &OMP_CLAUSE_CHAIN (*pc);
35182     }
35183
35184   gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35185   count = ordered ? ordered : collapse;
35186
35187   declv = make_tree_vec (count);
35188   initv = make_tree_vec (count);
35189   condv = make_tree_vec (count);
35190   incrv = make_tree_vec (count);
35191
35192   loc_first = cp_lexer_peek_token (parser->lexer)->location;
35193
35194   for (i = 0; i < count; i++)
35195     {
35196       int bracecount = 0;
35197       tree add_private_clause = NULL_TREE;
35198       location_t loc;
35199
35200       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35201         {
35202           if (!collapse_err)
35203             cp_parser_error (parser, "for statement expected");
35204           return NULL;
35205         }
35206       loc = cp_lexer_consume_token (parser->lexer)->location;
35207
35208       matching_parens parens;
35209       if (!parens.require_open (parser))
35210         return NULL;
35211
35212       init = orig_init = decl = real_decl = NULL;
35213       this_pre_body = push_stmt_list ();
35214
35215       add_private_clause
35216         = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35217                                        init, orig_init, decl, real_decl);
35218
35219       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35220       if (this_pre_body)
35221         {
35222           this_pre_body = pop_stmt_list (this_pre_body);
35223           if (pre_body)
35224             {
35225               tree t = pre_body;
35226               pre_body = push_stmt_list ();
35227               add_stmt (t);
35228               add_stmt (this_pre_body);
35229               pre_body = pop_stmt_list (pre_body);
35230             }
35231           else
35232             pre_body = this_pre_body;
35233         }
35234
35235       if (decl)
35236         real_decl = decl;
35237       if (cclauses != NULL
35238           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35239           && real_decl != NULL_TREE)
35240         {
35241           tree *c;
35242           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35243             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35244                 && OMP_CLAUSE_DECL (*c) == real_decl)
35245               {
35246                 error_at (loc, "iteration variable %qD"
35247                           " should not be firstprivate", real_decl);
35248                 *c = OMP_CLAUSE_CHAIN (*c);
35249               }
35250             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35251                      && OMP_CLAUSE_DECL (*c) == real_decl)
35252               {
35253                 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
35254                 tree l = *c;
35255                 *c = OMP_CLAUSE_CHAIN (*c);
35256                 if (code == OMP_SIMD)
35257                   {
35258                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35259                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35260                   }
35261                 else
35262                   {
35263                     OMP_CLAUSE_CHAIN (l) = clauses;
35264                     clauses = l;
35265                   }
35266                 add_private_clause = NULL_TREE;
35267               }
35268             else
35269               {
35270                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35271                     && OMP_CLAUSE_DECL (*c) == real_decl)
35272                   add_private_clause = NULL_TREE;
35273                 c = &OMP_CLAUSE_CHAIN (*c);
35274               }
35275         }
35276
35277       if (add_private_clause)
35278         {
35279           tree c;
35280           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35281             {
35282               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35283                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35284                   && OMP_CLAUSE_DECL (c) == decl)
35285                 break;
35286               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35287                        && OMP_CLAUSE_DECL (c) == decl)
35288                 error_at (loc, "iteration variable %qD "
35289                           "should not be firstprivate",
35290                           decl);
35291               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35292                        && OMP_CLAUSE_DECL (c) == decl)
35293                 error_at (loc, "iteration variable %qD should not be reduction",
35294                           decl);
35295             }
35296           if (c == NULL)
35297             {
35298               if (code != OMP_SIMD)
35299                 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35300               else if (collapse == 1)
35301                 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35302               else
35303                 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35304               OMP_CLAUSE_DECL (c) = add_private_clause;
35305               c = finish_omp_clauses (c, C_ORT_OMP);
35306               if (c)
35307                 {
35308                   OMP_CLAUSE_CHAIN (c) = clauses;
35309                   clauses = c;
35310                   /* For linear, signal that we need to fill up
35311                      the so far unknown linear step.  */
35312                   if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35313                     OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35314                 }
35315             }
35316         }
35317
35318       cond = NULL;
35319       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35320         cond = cp_parser_omp_for_cond (parser, decl);
35321       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35322
35323       incr = NULL;
35324       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35325         {
35326           /* If decl is an iterator, preserve the operator on decl
35327              until finish_omp_for.  */
35328           if (real_decl
35329               && ((processing_template_decl
35330                    && (TREE_TYPE (real_decl) == NULL_TREE
35331                        || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35332                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35333             incr = cp_parser_omp_for_incr (parser, real_decl);
35334           else
35335             incr = cp_parser_expression (parser);
35336           if (!EXPR_HAS_LOCATION (incr))
35337             protected_set_expr_location (incr, input_location);
35338         }
35339
35340       if (!parens.require_close (parser))
35341         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35342                                                /*or_comma=*/false,
35343                                                /*consume_paren=*/true);
35344
35345       TREE_VEC_ELT (declv, i) = decl;
35346       TREE_VEC_ELT (initv, i) = init;
35347       TREE_VEC_ELT (condv, i) = cond;
35348       TREE_VEC_ELT (incrv, i) = incr;
35349       if (orig_init)
35350         {
35351           orig_inits.safe_grow_cleared (i + 1);
35352           orig_inits[i] = orig_init;
35353         }
35354
35355       if (i == count - 1)
35356         break;
35357
35358       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35359          in between the collapsed for loops to be still considered perfectly
35360          nested.  Hopefully the final version clarifies this.
35361          For now handle (multiple) {'s and empty statements.  */
35362       cp_parser_parse_tentatively (parser);
35363       for (;;)
35364         {
35365           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35366             break;
35367           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35368             {
35369               cp_lexer_consume_token (parser->lexer);
35370               bracecount++;
35371             }
35372           else if (bracecount
35373                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35374             cp_lexer_consume_token (parser->lexer);
35375           else
35376             {
35377               loc = cp_lexer_peek_token (parser->lexer)->location;
35378               error_at (loc, "not enough for loops to collapse");
35379               collapse_err = true;
35380               cp_parser_abort_tentative_parse (parser);
35381               declv = NULL_TREE;
35382               break;
35383             }
35384         }
35385
35386       if (declv)
35387         {
35388           cp_parser_parse_definitely (parser);
35389           nbraces += bracecount;
35390         }
35391     }
35392
35393   if (nbraces)
35394     if_p = NULL;
35395
35396   /* Note that we saved the original contents of this flag when we entered
35397      the structured block, and so we don't need to re-save it here.  */
35398   parser->in_statement = IN_OMP_FOR;
35399
35400   /* Note that the grammar doesn't call for a structured block here,
35401      though the loop as a whole is a structured block.  */
35402   body = push_stmt_list ();
35403   cp_parser_statement (parser, NULL_TREE, false, if_p);
35404   body = pop_stmt_list (body);
35405
35406   if (declv == NULL_TREE)
35407     ret = NULL_TREE;
35408   else
35409     ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35410                           body, pre_body, &orig_inits, clauses);
35411
35412   while (nbraces)
35413     {
35414       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35415         {
35416           cp_lexer_consume_token (parser->lexer);
35417           nbraces--;
35418         }
35419       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35420         cp_lexer_consume_token (parser->lexer);
35421       else
35422         {
35423           if (!collapse_err)
35424             {
35425               error_at (cp_lexer_peek_token (parser->lexer)->location,
35426                         "collapsed loops not perfectly nested");
35427             }
35428           collapse_err = true;
35429           cp_parser_statement_seq_opt (parser, NULL);
35430           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35431             break;
35432         }
35433     }
35434
35435   while (!for_block->is_empty ())
35436     {
35437       tree t = for_block->pop ();
35438       if (TREE_CODE (t) == STATEMENT_LIST)
35439         add_stmt (pop_stmt_list (t));
35440       else
35441         add_stmt (t);
35442     }
35443   release_tree_vector (for_block);
35444
35445   return ret;
35446 }
35447
35448 /* Helper function for OpenMP parsing, split clauses and call
35449    finish_omp_clauses on each of the set of clauses afterwards.  */
35450
35451 static void
35452 cp_omp_split_clauses (location_t loc, enum tree_code code,
35453                       omp_clause_mask mask, tree clauses, tree *cclauses)
35454 {
35455   int i;
35456   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35457   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35458     if (cclauses[i])
35459       cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35460 }
35461
35462 /* OpenMP 4.0:
35463    #pragma omp simd simd-clause[optseq] new-line
35464      for-loop  */
35465
35466 #define OMP_SIMD_CLAUSE_MASK                                    \
35467         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
35468         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
35469         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
35470         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
35471         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35472         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
35473         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35474         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35475
35476 static tree
35477 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35478                     char *p_name, omp_clause_mask mask, tree *cclauses,
35479                     bool *if_p)
35480 {
35481   tree clauses, sb, ret;
35482   unsigned int save;
35483   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35484
35485   strcat (p_name, " simd");
35486   mask |= OMP_SIMD_CLAUSE_MASK;
35487
35488   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35489                                        cclauses == NULL);
35490   if (cclauses)
35491     {
35492       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35493       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35494       tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35495                                 OMP_CLAUSE_ORDERED);
35496       if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35497         {
35498           error_at (OMP_CLAUSE_LOCATION (c),
35499                     "%<ordered%> clause with parameter may not be specified "
35500                     "on %qs construct", p_name);
35501           OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35502         }
35503     }
35504
35505   sb = begin_omp_structured_block ();
35506   save = cp_parser_begin_omp_structured_block (parser);
35507
35508   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35509
35510   cp_parser_end_omp_structured_block (parser, save);
35511   add_stmt (finish_omp_structured_block (sb));
35512
35513   return ret;
35514 }
35515
35516 /* OpenMP 2.5:
35517    #pragma omp for for-clause[optseq] new-line
35518      for-loop
35519
35520    OpenMP 4.0:
35521    #pragma omp for simd for-simd-clause[optseq] new-line
35522      for-loop  */
35523
35524 #define OMP_FOR_CLAUSE_MASK                                     \
35525         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35526         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35527         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
35528         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
35529         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35530         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
35531         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
35532         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
35533         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35534
35535 static tree
35536 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35537                    char *p_name, omp_clause_mask mask, tree *cclauses,
35538                    bool *if_p)
35539 {
35540   tree clauses, sb, ret;
35541   unsigned int save;
35542   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35543
35544   strcat (p_name, " for");
35545   mask |= OMP_FOR_CLAUSE_MASK;
35546   /* parallel for{, simd} disallows nowait clause, but for
35547      target {teams distribute ,}parallel for{, simd} it should be accepted.  */
35548   if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35549     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35550   /* Composite distribute parallel for{, simd} disallows ordered clause.  */
35551   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35552     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35553
35554   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35555     {
35556       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35557       const char *p = IDENTIFIER_POINTER (id);
35558
35559       if (strcmp (p, "simd") == 0)
35560         {
35561           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35562           if (cclauses == NULL)
35563             cclauses = cclauses_buf;
35564
35565           cp_lexer_consume_token (parser->lexer);
35566           if (!flag_openmp)  /* flag_openmp_simd  */
35567             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35568                                        cclauses, if_p);
35569           sb = begin_omp_structured_block ();
35570           save = cp_parser_begin_omp_structured_block (parser);
35571           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35572                                     cclauses, if_p);
35573           cp_parser_end_omp_structured_block (parser, save);
35574           tree body = finish_omp_structured_block (sb);
35575           if (ret == NULL)
35576             return ret;
35577           ret = make_node (OMP_FOR);
35578           TREE_TYPE (ret) = void_type_node;
35579           OMP_FOR_BODY (ret) = body;
35580           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35581           SET_EXPR_LOCATION (ret, loc);
35582           add_stmt (ret);
35583           return ret;
35584         }
35585     }
35586   if (!flag_openmp)  /* flag_openmp_simd  */
35587     {
35588       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35589       return NULL_TREE;
35590     }
35591
35592   /* Composite distribute parallel for disallows linear clause.  */
35593   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35594     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35595
35596   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35597                                        cclauses == NULL);
35598   if (cclauses)
35599     {
35600       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35601       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35602     }
35603
35604   sb = begin_omp_structured_block ();
35605   save = cp_parser_begin_omp_structured_block (parser);
35606
35607   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35608
35609   cp_parser_end_omp_structured_block (parser, save);
35610   add_stmt (finish_omp_structured_block (sb));
35611
35612   return ret;
35613 }
35614
35615 /* OpenMP 2.5:
35616    # pragma omp master new-line
35617      structured-block  */
35618
35619 static tree
35620 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35621 {
35622   cp_parser_require_pragma_eol (parser, pragma_tok);
35623   return c_finish_omp_master (input_location,
35624                               cp_parser_omp_structured_block (parser, if_p));
35625 }
35626
35627 /* OpenMP 2.5:
35628    # pragma omp ordered new-line
35629      structured-block
35630
35631    OpenMP 4.5:
35632    # pragma omp ordered ordered-clauses new-line
35633      structured-block  */
35634
35635 #define OMP_ORDERED_CLAUSE_MASK                                 \
35636         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS)      \
35637         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35638
35639 #define OMP_ORDERED_DEPEND_CLAUSE_MASK                          \
35640         (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35641
35642 static bool
35643 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35644                        enum pragma_context context, bool *if_p)
35645 {
35646   location_t loc = pragma_tok->location;
35647
35648   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35649     {
35650       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35651       const char *p = IDENTIFIER_POINTER (id);
35652
35653       if (strcmp (p, "depend") == 0)
35654         {
35655           if (!flag_openmp)     /* flag_openmp_simd */
35656             {
35657               cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35658               return false;
35659             }
35660           if (context == pragma_stmt)
35661             {
35662               error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35663                         "%<depend%> clause may only be used in compound "
35664                         "statements");
35665               cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35666               return false;
35667             }
35668           tree clauses
35669             = cp_parser_omp_all_clauses (parser,
35670                                          OMP_ORDERED_DEPEND_CLAUSE_MASK,
35671                                          "#pragma omp ordered", pragma_tok);
35672           c_finish_omp_ordered (loc, clauses, NULL_TREE);
35673           return false;
35674         }
35675     }
35676
35677   tree clauses
35678     = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35679                                  "#pragma omp ordered", pragma_tok);
35680
35681   if (!flag_openmp     /* flag_openmp_simd  */
35682       && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35683     return false;
35684
35685   c_finish_omp_ordered (loc, clauses,
35686                         cp_parser_omp_structured_block (parser, if_p));
35687   return true;
35688 }
35689
35690 /* OpenMP 2.5:
35691
35692    section-scope:
35693      { section-sequence }
35694
35695    section-sequence:
35696      section-directive[opt] structured-block
35697      section-sequence section-directive structured-block  */
35698
35699 static tree
35700 cp_parser_omp_sections_scope (cp_parser *parser)
35701 {
35702   tree stmt, substmt;
35703   bool error_suppress = false;
35704   cp_token *tok;
35705
35706   matching_braces braces;
35707   if (!braces.require_open (parser))
35708     return NULL_TREE;
35709
35710   stmt = push_stmt_list ();
35711
35712   if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35713       != PRAGMA_OMP_SECTION)
35714     {
35715       substmt = cp_parser_omp_structured_block (parser, NULL);
35716       substmt = build1 (OMP_SECTION, void_type_node, substmt);
35717       add_stmt (substmt);
35718     }
35719
35720   while (1)
35721     {
35722       tok = cp_lexer_peek_token (parser->lexer);
35723       if (tok->type == CPP_CLOSE_BRACE)
35724         break;
35725       if (tok->type == CPP_EOF)
35726         break;
35727
35728       if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35729         {
35730           cp_lexer_consume_token (parser->lexer);
35731           cp_parser_require_pragma_eol (parser, tok);
35732           error_suppress = false;
35733         }
35734       else if (!error_suppress)
35735         {
35736           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35737           error_suppress = true;
35738         }
35739
35740       substmt = cp_parser_omp_structured_block (parser, NULL);
35741       substmt = build1 (OMP_SECTION, void_type_node, substmt);
35742       add_stmt (substmt);
35743     }
35744   braces.require_close (parser);
35745
35746   substmt = pop_stmt_list (stmt);
35747
35748   stmt = make_node (OMP_SECTIONS);
35749   TREE_TYPE (stmt) = void_type_node;
35750   OMP_SECTIONS_BODY (stmt) = substmt;
35751
35752   add_stmt (stmt);
35753   return stmt;
35754 }
35755
35756 /* OpenMP 2.5:
35757    # pragma omp sections sections-clause[optseq] newline
35758      sections-scope  */
35759
35760 #define OMP_SECTIONS_CLAUSE_MASK                                \
35761         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35762         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35763         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
35764         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35765         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35766
35767 static tree
35768 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35769                         char *p_name, omp_clause_mask mask, tree *cclauses)
35770 {
35771   tree clauses, ret;
35772   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35773
35774   strcat (p_name, " sections");
35775   mask |= OMP_SECTIONS_CLAUSE_MASK;
35776   if (cclauses)
35777     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35778
35779   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35780                                        cclauses == NULL);
35781   if (cclauses)
35782     {
35783       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35784       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35785     }
35786
35787   ret = cp_parser_omp_sections_scope (parser);
35788   if (ret)
35789     OMP_SECTIONS_CLAUSES (ret) = clauses;
35790
35791   return ret;
35792 }
35793
35794 /* OpenMP 2.5:
35795    # pragma omp parallel parallel-clause[optseq] new-line
35796      structured-block
35797    # pragma omp parallel for parallel-for-clause[optseq] new-line
35798      structured-block
35799    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35800      structured-block
35801
35802    OpenMP 4.0:
35803    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35804      structured-block */
35805
35806 #define OMP_PARALLEL_CLAUSE_MASK                                \
35807         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
35808         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35809         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35810         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
35811         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
35812         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
35813         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
35814         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
35815         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35816
35817 static tree
35818 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35819                         char *p_name, omp_clause_mask mask, tree *cclauses,
35820                         bool *if_p)
35821 {
35822   tree stmt, clauses, block;
35823   unsigned int save;
35824   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35825
35826   strcat (p_name, " parallel");
35827   mask |= OMP_PARALLEL_CLAUSE_MASK;
35828   /* #pragma omp target parallel{, for, for simd} disallow copyin clause.  */
35829   if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35830       && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35831     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35832
35833   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35834     {
35835       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35836       if (cclauses == NULL)
35837         cclauses = cclauses_buf;
35838
35839       cp_lexer_consume_token (parser->lexer);
35840       if (!flag_openmp)  /* flag_openmp_simd  */
35841         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35842                                   if_p);
35843       block = begin_omp_parallel ();
35844       save = cp_parser_begin_omp_structured_block (parser);
35845       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35846                                     if_p);
35847       cp_parser_end_omp_structured_block (parser, save);
35848       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35849                                   block);
35850       if (ret == NULL_TREE)
35851         return ret;
35852       OMP_PARALLEL_COMBINED (stmt) = 1;
35853       return stmt;
35854     }
35855   /* When combined with distribute, parallel has to be followed by for.
35856      #pragma omp target parallel is allowed though.  */
35857   else if (cclauses
35858            && (mask & (OMP_CLAUSE_MASK_1
35859                        << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35860     {
35861       error_at (loc, "expected %<for%> after %qs", p_name);
35862       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35863       return NULL_TREE;
35864     }
35865   else if (!flag_openmp)  /* flag_openmp_simd  */
35866     {
35867       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35868       return NULL_TREE;
35869     }
35870   else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35871     {
35872       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35873       const char *p = IDENTIFIER_POINTER (id);
35874       if (strcmp (p, "sections") == 0)
35875         {
35876           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35877           cclauses = cclauses_buf;
35878
35879           cp_lexer_consume_token (parser->lexer);
35880           block = begin_omp_parallel ();
35881           save = cp_parser_begin_omp_structured_block (parser);
35882           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35883           cp_parser_end_omp_structured_block (parser, save);
35884           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35885                                       block);
35886           OMP_PARALLEL_COMBINED (stmt) = 1;
35887           return stmt;
35888         }
35889     }
35890
35891   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35892                                        cclauses == NULL);
35893   if (cclauses)
35894     {
35895       cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35896       clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35897     }
35898
35899   block = begin_omp_parallel ();
35900   save = cp_parser_begin_omp_structured_block (parser);
35901   cp_parser_statement (parser, NULL_TREE, false, if_p);
35902   cp_parser_end_omp_structured_block (parser, save);
35903   stmt = finish_omp_parallel (clauses, block);
35904   return stmt;
35905 }
35906
35907 /* OpenMP 2.5:
35908    # pragma omp single single-clause[optseq] new-line
35909      structured-block  */
35910
35911 #define OMP_SINGLE_CLAUSE_MASK                                  \
35912         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35913         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35914         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
35915         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35916
35917 static tree
35918 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35919 {
35920   tree stmt = make_node (OMP_SINGLE);
35921   TREE_TYPE (stmt) = void_type_node;
35922
35923   OMP_SINGLE_CLAUSES (stmt)
35924     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35925                                  "#pragma omp single", pragma_tok);
35926   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35927
35928   return add_stmt (stmt);
35929 }
35930
35931 /* OpenMP 3.0:
35932    # pragma omp task task-clause[optseq] new-line
35933      structured-block  */
35934
35935 #define OMP_TASK_CLAUSE_MASK                                    \
35936         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
35937         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
35938         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
35939         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
35940         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35941         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
35942         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
35943         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
35944         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
35945         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35946
35947 static tree
35948 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35949 {
35950   tree clauses, block;
35951   unsigned int save;
35952
35953   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35954                                        "#pragma omp task", pragma_tok);
35955   block = begin_omp_task ();
35956   save = cp_parser_begin_omp_structured_block (parser);
35957   cp_parser_statement (parser, NULL_TREE, false, if_p);
35958   cp_parser_end_omp_structured_block (parser, save);
35959   return finish_omp_task (clauses, block);
35960 }
35961
35962 /* OpenMP 3.0:
35963    # pragma omp taskwait new-line  */
35964
35965 static void
35966 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35967 {
35968   cp_parser_require_pragma_eol (parser, pragma_tok);
35969   finish_omp_taskwait ();
35970 }
35971
35972 /* OpenMP 3.1:
35973    # pragma omp taskyield new-line  */
35974
35975 static void
35976 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35977 {
35978   cp_parser_require_pragma_eol (parser, pragma_tok);
35979   finish_omp_taskyield ();
35980 }
35981
35982 /* OpenMP 4.0:
35983    # pragma omp taskgroup new-line
35984      structured-block  */
35985
35986 static tree
35987 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35988 {
35989   cp_parser_require_pragma_eol (parser, pragma_tok);
35990   return c_finish_omp_taskgroup (input_location,
35991                                  cp_parser_omp_structured_block (parser,
35992                                                                  if_p));
35993 }
35994
35995
35996 /* OpenMP 2.5:
35997    # pragma omp threadprivate (variable-list) */
35998
35999 static void
36000 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36001 {
36002   tree vars;
36003
36004   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36005   cp_parser_require_pragma_eol (parser, pragma_tok);
36006
36007   finish_omp_threadprivate (vars);
36008 }
36009
36010 /* OpenMP 4.0:
36011    # pragma omp cancel cancel-clause[optseq] new-line  */
36012
36013 #define OMP_CANCEL_CLAUSE_MASK                                  \
36014         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
36015         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
36016         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
36017         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
36018         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36019
36020 static void
36021 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36022 {
36023   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36024                                             "#pragma omp cancel", pragma_tok);
36025   finish_omp_cancel (clauses);
36026 }
36027
36028 /* OpenMP 4.0:
36029    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
36030
36031 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
36032         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
36033         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
36034         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
36035         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36036
36037 static void
36038 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36039                                   enum pragma_context context)
36040 {
36041   tree clauses;
36042   bool point_seen = false;
36043
36044   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36045     {
36046       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36047       const char *p = IDENTIFIER_POINTER (id);
36048
36049       if (strcmp (p, "point") == 0)
36050         {
36051           cp_lexer_consume_token (parser->lexer);
36052           point_seen = true;
36053         }
36054     }
36055   if (!point_seen)
36056     {
36057       cp_parser_error (parser, "expected %<point%>");
36058       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36059       return;
36060     }
36061
36062   if (context != pragma_compound)
36063     {
36064       if (context == pragma_stmt)
36065         error_at (pragma_tok->location,
36066                   "%<#pragma %s%> may only be used in compound statements",
36067                   "omp cancellation point");
36068       else
36069         cp_parser_error (parser, "expected declaration specifiers");
36070       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36071       return;
36072     }
36073
36074   clauses = cp_parser_omp_all_clauses (parser,
36075                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
36076                                        "#pragma omp cancellation point",
36077                                        pragma_tok);
36078   finish_omp_cancellation_point (clauses);
36079 }
36080
36081 /* OpenMP 4.0:
36082    #pragma omp distribute distribute-clause[optseq] new-line
36083      for-loop  */
36084
36085 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
36086         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36087         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36088         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
36089         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36090         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36091
36092 static tree
36093 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36094                           char *p_name, omp_clause_mask mask, tree *cclauses,
36095                           bool *if_p)
36096 {
36097   tree clauses, sb, ret;
36098   unsigned int save;
36099   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36100
36101   strcat (p_name, " distribute");
36102   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36103
36104   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36105     {
36106       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36107       const char *p = IDENTIFIER_POINTER (id);
36108       bool simd = false;
36109       bool parallel = false;
36110
36111       if (strcmp (p, "simd") == 0)
36112         simd = true;
36113       else
36114         parallel = strcmp (p, "parallel") == 0;
36115       if (parallel || simd)
36116         {
36117           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36118           if (cclauses == NULL)
36119             cclauses = cclauses_buf;
36120           cp_lexer_consume_token (parser->lexer);
36121           if (!flag_openmp)  /* flag_openmp_simd  */
36122             {
36123               if (simd)
36124                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36125                                            cclauses, if_p);
36126               else
36127                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36128                                                cclauses, if_p);
36129             }
36130           sb = begin_omp_structured_block ();
36131           save = cp_parser_begin_omp_structured_block (parser);
36132           if (simd)
36133             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36134                                       cclauses, if_p);
36135           else
36136             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36137                                           cclauses, if_p);
36138           cp_parser_end_omp_structured_block (parser, save);
36139           tree body = finish_omp_structured_block (sb);
36140           if (ret == NULL)
36141             return ret;
36142           ret = make_node (OMP_DISTRIBUTE);
36143           TREE_TYPE (ret) = void_type_node;
36144           OMP_FOR_BODY (ret) = body;
36145           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36146           SET_EXPR_LOCATION (ret, loc);
36147           add_stmt (ret);
36148           return ret;
36149         }
36150     }
36151   if (!flag_openmp)  /* flag_openmp_simd  */
36152     {
36153       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36154       return NULL_TREE;
36155     }
36156
36157   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36158                                        cclauses == NULL);
36159   if (cclauses)
36160     {
36161       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36162       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36163     }
36164
36165   sb = begin_omp_structured_block ();
36166   save = cp_parser_begin_omp_structured_block (parser);
36167
36168   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36169
36170   cp_parser_end_omp_structured_block (parser, save);
36171   add_stmt (finish_omp_structured_block (sb));
36172
36173   return ret;
36174 }
36175
36176 /* OpenMP 4.0:
36177    # pragma omp teams teams-clause[optseq] new-line
36178      structured-block  */
36179
36180 #define OMP_TEAMS_CLAUSE_MASK                                   \
36181         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36182         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36183         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
36184         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
36185         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
36186         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36187         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36188
36189 static tree
36190 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36191                      char *p_name, omp_clause_mask mask, tree *cclauses,
36192                      bool *if_p)
36193 {
36194   tree clauses, sb, ret;
36195   unsigned int save;
36196   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36197
36198   strcat (p_name, " teams");
36199   mask |= OMP_TEAMS_CLAUSE_MASK;
36200
36201   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36202     {
36203       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36204       const char *p = IDENTIFIER_POINTER (id);
36205       if (strcmp (p, "distribute") == 0)
36206         {
36207           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36208           if (cclauses == NULL)
36209             cclauses = cclauses_buf;
36210
36211           cp_lexer_consume_token (parser->lexer);
36212           if (!flag_openmp)  /* flag_openmp_simd  */
36213             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36214                                              cclauses, if_p);
36215           sb = begin_omp_structured_block ();
36216           save = cp_parser_begin_omp_structured_block (parser);
36217           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36218                                           cclauses, if_p);
36219           cp_parser_end_omp_structured_block (parser, save);
36220           tree body = finish_omp_structured_block (sb);
36221           if (ret == NULL)
36222             return ret;
36223           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36224           ret = make_node (OMP_TEAMS);
36225           TREE_TYPE (ret) = void_type_node;
36226           OMP_TEAMS_CLAUSES (ret) = clauses;
36227           OMP_TEAMS_BODY (ret) = body;
36228           OMP_TEAMS_COMBINED (ret) = 1;
36229           SET_EXPR_LOCATION (ret, loc);
36230           return add_stmt (ret);
36231         }
36232     }
36233   if (!flag_openmp)  /* flag_openmp_simd  */
36234     {
36235       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36236       return NULL_TREE;
36237     }
36238
36239   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36240                                        cclauses == NULL);
36241   if (cclauses)
36242     {
36243       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36244       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36245     }
36246
36247   tree stmt = make_node (OMP_TEAMS);
36248   TREE_TYPE (stmt) = void_type_node;
36249   OMP_TEAMS_CLAUSES (stmt) = clauses;
36250   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36251   SET_EXPR_LOCATION (stmt, loc);
36252
36253   return add_stmt (stmt);
36254 }
36255
36256 /* OpenMP 4.0:
36257    # pragma omp target data target-data-clause[optseq] new-line
36258      structured-block  */
36259
36260 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
36261         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36262         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36263         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36264         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36265
36266 static tree
36267 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36268 {
36269   tree clauses
36270     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36271                                  "#pragma omp target data", pragma_tok);
36272   int map_seen = 0;
36273   for (tree *pc = &clauses; *pc;)
36274     {
36275       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36276         switch (OMP_CLAUSE_MAP_KIND (*pc))
36277           {
36278           case GOMP_MAP_TO:
36279           case GOMP_MAP_ALWAYS_TO:
36280           case GOMP_MAP_FROM:
36281           case GOMP_MAP_ALWAYS_FROM:
36282           case GOMP_MAP_TOFROM:
36283           case GOMP_MAP_ALWAYS_TOFROM:
36284           case GOMP_MAP_ALLOC:
36285             map_seen = 3;
36286             break;
36287           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36288           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36289           case GOMP_MAP_ALWAYS_POINTER:
36290             break;
36291           default:
36292             map_seen |= 1;
36293             error_at (OMP_CLAUSE_LOCATION (*pc),
36294                       "%<#pragma omp target data%> with map-type other "
36295                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36296                       "on %<map%> clause");
36297             *pc = OMP_CLAUSE_CHAIN (*pc);
36298             continue;
36299           }
36300       pc = &OMP_CLAUSE_CHAIN (*pc);
36301     }
36302
36303   if (map_seen != 3)
36304     {
36305       if (map_seen == 0)
36306         error_at (pragma_tok->location,
36307                   "%<#pragma omp target data%> must contain at least "
36308                   "one %<map%> clause");
36309       return NULL_TREE;
36310     }
36311
36312   tree stmt = make_node (OMP_TARGET_DATA);
36313   TREE_TYPE (stmt) = void_type_node;
36314   OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36315
36316   keep_next_level (true);
36317   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36318
36319   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36320   return add_stmt (stmt);
36321 }
36322
36323 /* OpenMP 4.5:
36324    # pragma omp target enter data target-enter-data-clause[optseq] new-line
36325      structured-block  */
36326
36327 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK                       \
36328         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36329         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36330         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36331         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36332         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36333
36334 static tree
36335 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36336                                  enum pragma_context context)
36337 {
36338   bool data_seen = false;
36339   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36340     {
36341       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36342       const char *p = IDENTIFIER_POINTER (id);
36343
36344       if (strcmp (p, "data") == 0)
36345         {
36346           cp_lexer_consume_token (parser->lexer);
36347           data_seen = true;
36348         }
36349     }
36350   if (!data_seen)
36351     {
36352       cp_parser_error (parser, "expected %<data%>");
36353       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36354       return NULL_TREE;
36355     }
36356
36357   if (context == pragma_stmt)
36358     {
36359       error_at (pragma_tok->location,
36360                 "%<#pragma %s%> may only be used in compound statements",
36361                 "omp target enter data");
36362       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36363       return NULL_TREE;
36364     }
36365
36366   tree clauses
36367     = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36368                                  "#pragma omp target enter data", pragma_tok);
36369   int map_seen = 0;
36370   for (tree *pc = &clauses; *pc;)
36371     {
36372       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36373         switch (OMP_CLAUSE_MAP_KIND (*pc))
36374           {
36375           case GOMP_MAP_TO:
36376           case GOMP_MAP_ALWAYS_TO:
36377           case GOMP_MAP_ALLOC:
36378             map_seen = 3;
36379             break;
36380           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36381           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36382           case GOMP_MAP_ALWAYS_POINTER:
36383             break;
36384           default:
36385             map_seen |= 1;
36386             error_at (OMP_CLAUSE_LOCATION (*pc),
36387                       "%<#pragma omp target enter data%> with map-type other "
36388                       "than %<to%> or %<alloc%> on %<map%> clause");
36389             *pc = OMP_CLAUSE_CHAIN (*pc);
36390             continue;
36391           }
36392       pc = &OMP_CLAUSE_CHAIN (*pc);
36393     }
36394
36395   if (map_seen != 3)
36396     {
36397       if (map_seen == 0)
36398         error_at (pragma_tok->location,
36399                   "%<#pragma omp target enter data%> must contain at least "
36400                   "one %<map%> clause");
36401       return NULL_TREE;
36402     }
36403
36404   tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36405   TREE_TYPE (stmt) = void_type_node;
36406   OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36407   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36408   return add_stmt (stmt);
36409 }
36410
36411 /* OpenMP 4.5:
36412    # pragma omp target exit data target-enter-data-clause[optseq] new-line
36413      structured-block  */
36414
36415 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK                        \
36416         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36417         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36418         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36419         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36420         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36421
36422 static tree
36423 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36424                                 enum pragma_context context)
36425 {
36426   bool data_seen = false;
36427   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36428     {
36429       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36430       const char *p = IDENTIFIER_POINTER (id);
36431
36432       if (strcmp (p, "data") == 0)
36433         {
36434           cp_lexer_consume_token (parser->lexer);
36435           data_seen = true;
36436         }
36437     }
36438   if (!data_seen)
36439     {
36440       cp_parser_error (parser, "expected %<data%>");
36441       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36442       return NULL_TREE;
36443     }
36444
36445   if (context == pragma_stmt)
36446     {
36447       error_at (pragma_tok->location,
36448                 "%<#pragma %s%> may only be used in compound statements",
36449                 "omp target exit data");
36450       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36451       return NULL_TREE;
36452     }
36453
36454   tree clauses
36455     = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36456                                  "#pragma omp target exit data", pragma_tok);
36457   int map_seen = 0;
36458   for (tree *pc = &clauses; *pc;)
36459     {
36460       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36461         switch (OMP_CLAUSE_MAP_KIND (*pc))
36462           {
36463           case GOMP_MAP_FROM:
36464           case GOMP_MAP_ALWAYS_FROM:
36465           case GOMP_MAP_RELEASE:
36466           case GOMP_MAP_DELETE:
36467             map_seen = 3;
36468             break;
36469           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36470           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36471           case GOMP_MAP_ALWAYS_POINTER:
36472             break;
36473           default:
36474             map_seen |= 1;
36475             error_at (OMP_CLAUSE_LOCATION (*pc),
36476                       "%<#pragma omp target exit data%> with map-type other "
36477                       "than %<from%>, %<release%> or %<delete%> on %<map%>"
36478                       " clause");
36479             *pc = OMP_CLAUSE_CHAIN (*pc);
36480             continue;
36481           }
36482       pc = &OMP_CLAUSE_CHAIN (*pc);
36483     }
36484
36485   if (map_seen != 3)
36486     {
36487       if (map_seen == 0)
36488         error_at (pragma_tok->location,
36489                   "%<#pragma omp target exit data%> must contain at least "
36490                   "one %<map%> clause");
36491       return NULL_TREE;
36492     }
36493
36494   tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36495   TREE_TYPE (stmt) = void_type_node;
36496   OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36497   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36498   return add_stmt (stmt);
36499 }
36500
36501 /* OpenMP 4.0:
36502    # pragma omp target update target-update-clause[optseq] new-line */
36503
36504 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
36505         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
36506         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
36507         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36508         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36509         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36510         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36511
36512 static bool
36513 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36514                              enum pragma_context context)
36515 {
36516   if (context == pragma_stmt)
36517     {
36518       error_at (pragma_tok->location,
36519                 "%<#pragma %s%> may only be used in compound statements",
36520                 "omp target update");
36521       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36522       return false;
36523     }
36524
36525   tree clauses
36526     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36527                                  "#pragma omp target update", pragma_tok);
36528   if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36529       && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36530     {
36531       error_at (pragma_tok->location,
36532                 "%<#pragma omp target update%> must contain at least one "
36533                 "%<from%> or %<to%> clauses");
36534       return false;
36535     }
36536
36537   tree stmt = make_node (OMP_TARGET_UPDATE);
36538   TREE_TYPE (stmt) = void_type_node;
36539   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36540   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36541   add_stmt (stmt);
36542   return false;
36543 }
36544
36545 /* OpenMP 4.0:
36546    # pragma omp target target-clause[optseq] new-line
36547      structured-block  */
36548
36549 #define OMP_TARGET_CLAUSE_MASK                                  \
36550         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
36551         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
36552         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
36553         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)       \
36554         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
36555         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
36556         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36557         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP)   \
36558         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36559
36560 static bool
36561 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36562                       enum pragma_context context, bool *if_p)
36563 {
36564   tree *pc = NULL, stmt;
36565
36566   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36567     {
36568       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36569       const char *p = IDENTIFIER_POINTER (id);
36570       enum tree_code ccode = ERROR_MARK;
36571
36572       if (strcmp (p, "teams") == 0)
36573         ccode = OMP_TEAMS;
36574       else if (strcmp (p, "parallel") == 0)
36575         ccode = OMP_PARALLEL;
36576       else if (strcmp (p, "simd") == 0)
36577         ccode = OMP_SIMD;
36578       if (ccode != ERROR_MARK)
36579         {
36580           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36581           char p_name[sizeof ("#pragma omp target teams distribute "
36582                               "parallel for simd")];
36583
36584           cp_lexer_consume_token (parser->lexer);
36585           strcpy (p_name, "#pragma omp target");
36586           if (!flag_openmp)  /* flag_openmp_simd  */
36587             {
36588               tree stmt;
36589               switch (ccode)
36590                 {
36591                 case OMP_TEAMS:
36592                   stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36593                                               OMP_TARGET_CLAUSE_MASK,
36594                                               cclauses, if_p);
36595                   break;
36596                 case OMP_PARALLEL:
36597                   stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36598                                                  OMP_TARGET_CLAUSE_MASK,
36599                                                  cclauses, if_p);
36600                   break;
36601                 case OMP_SIMD:
36602                   stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36603                                              OMP_TARGET_CLAUSE_MASK,
36604                                              cclauses, if_p);
36605                   break;
36606                 default:
36607                   gcc_unreachable ();
36608                 }
36609               return stmt != NULL_TREE;
36610             }
36611           keep_next_level (true);
36612           tree sb = begin_omp_structured_block (), ret;
36613           unsigned save = cp_parser_begin_omp_structured_block (parser);
36614           switch (ccode)
36615             {
36616             case OMP_TEAMS:
36617               ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36618                                          OMP_TARGET_CLAUSE_MASK, cclauses,
36619                                          if_p);
36620               break;
36621             case OMP_PARALLEL:
36622               ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36623                                             OMP_TARGET_CLAUSE_MASK, cclauses,
36624                                             if_p);
36625               break;
36626             case OMP_SIMD:
36627               ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36628                                         OMP_TARGET_CLAUSE_MASK, cclauses,
36629                                         if_p);
36630               break;
36631             default:
36632               gcc_unreachable ();
36633             }
36634           cp_parser_end_omp_structured_block (parser, save);
36635           tree body = finish_omp_structured_block (sb);
36636           if (ret == NULL_TREE)
36637             return false;
36638           if (ccode == OMP_TEAMS && !processing_template_decl)
36639             {
36640               /* For combined target teams, ensure the num_teams and
36641                  thread_limit clause expressions are evaluated on the host,
36642                  before entering the target construct.  */
36643               tree c;
36644               for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36645                    c; c = OMP_CLAUSE_CHAIN (c))
36646                 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36647                      || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36648                     && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36649                   {
36650                     tree expr = OMP_CLAUSE_OPERAND (c, 0);
36651                     expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36652                     if (expr == error_mark_node)
36653                       continue;
36654                     tree tmp = TARGET_EXPR_SLOT (expr);
36655                     add_stmt (expr);
36656                     OMP_CLAUSE_OPERAND (c, 0) = expr;
36657                     tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36658                                                 OMP_CLAUSE_FIRSTPRIVATE);
36659                     OMP_CLAUSE_DECL (tc) = tmp;
36660                     OMP_CLAUSE_CHAIN (tc)
36661                       = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36662                     cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36663                   }
36664             }
36665           tree stmt = make_node (OMP_TARGET);
36666           TREE_TYPE (stmt) = void_type_node;
36667           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36668           OMP_TARGET_BODY (stmt) = body;
36669           OMP_TARGET_COMBINED (stmt) = 1;
36670           SET_EXPR_LOCATION (stmt, pragma_tok->location);
36671           add_stmt (stmt);
36672           pc = &OMP_TARGET_CLAUSES (stmt);
36673           goto check_clauses;
36674         }
36675       else if (!flag_openmp)  /* flag_openmp_simd  */
36676         {
36677           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36678           return false;
36679         }
36680       else if (strcmp (p, "data") == 0)
36681         {
36682           cp_lexer_consume_token (parser->lexer);
36683           cp_parser_omp_target_data (parser, pragma_tok, if_p);
36684           return true;
36685         }
36686       else if (strcmp (p, "enter") == 0)
36687         {
36688           cp_lexer_consume_token (parser->lexer);
36689           cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36690           return false;
36691         }
36692       else if (strcmp (p, "exit") == 0)
36693         {
36694           cp_lexer_consume_token (parser->lexer);
36695           cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36696           return false;
36697         }
36698       else if (strcmp (p, "update") == 0)
36699         {
36700           cp_lexer_consume_token (parser->lexer);
36701           return cp_parser_omp_target_update (parser, pragma_tok, context);
36702         }
36703     }
36704   if (!flag_openmp)  /* flag_openmp_simd  */
36705     {
36706       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36707       return false;
36708     }
36709
36710   stmt = make_node (OMP_TARGET);
36711   TREE_TYPE (stmt) = void_type_node;
36712
36713   OMP_TARGET_CLAUSES (stmt)
36714     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36715                                  "#pragma omp target", pragma_tok);
36716   pc = &OMP_TARGET_CLAUSES (stmt);
36717   keep_next_level (true);
36718   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36719
36720   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36721   add_stmt (stmt);
36722
36723 check_clauses:
36724   while (*pc)
36725     {
36726       if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36727         switch (OMP_CLAUSE_MAP_KIND (*pc))
36728           {
36729           case GOMP_MAP_TO:
36730           case GOMP_MAP_ALWAYS_TO:
36731           case GOMP_MAP_FROM:
36732           case GOMP_MAP_ALWAYS_FROM:
36733           case GOMP_MAP_TOFROM:
36734           case GOMP_MAP_ALWAYS_TOFROM:
36735           case GOMP_MAP_ALLOC:
36736           case GOMP_MAP_FIRSTPRIVATE_POINTER:
36737           case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36738           case GOMP_MAP_ALWAYS_POINTER:
36739             break;
36740           default:
36741             error_at (OMP_CLAUSE_LOCATION (*pc),
36742                       "%<#pragma omp target%> with map-type other "
36743                       "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36744                       "on %<map%> clause");
36745             *pc = OMP_CLAUSE_CHAIN (*pc);
36746             continue;
36747           }
36748       pc = &OMP_CLAUSE_CHAIN (*pc);
36749     }
36750   return true;
36751 }
36752
36753 /* OpenACC 2.0:
36754    # pragma acc cache (variable-list) new-line
36755 */
36756
36757 static tree
36758 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36759 {
36760   tree stmt, clauses;
36761
36762   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36763   clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36764
36765   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36766
36767   stmt = make_node (OACC_CACHE);
36768   TREE_TYPE (stmt) = void_type_node;
36769   OACC_CACHE_CLAUSES (stmt) = clauses;
36770   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36771   add_stmt (stmt);
36772
36773   return stmt;
36774 }
36775
36776 /* OpenACC 2.0:
36777    # pragma acc data oacc-data-clause[optseq] new-line
36778      structured-block  */
36779
36780 #define OACC_DATA_CLAUSE_MASK                                           \
36781         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
36782         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
36783         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
36784         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
36785         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
36786         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
36787         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
36788         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
36789         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
36790         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
36791         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36792
36793 static tree
36794 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36795 {
36796   tree stmt, clauses, block;
36797   unsigned int save;
36798
36799   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36800                                         "#pragma acc data", pragma_tok);
36801
36802   block = begin_omp_parallel ();
36803   save = cp_parser_begin_omp_structured_block (parser);
36804   cp_parser_statement (parser, NULL_TREE, false, if_p);
36805   cp_parser_end_omp_structured_block (parser, save);
36806   stmt = finish_oacc_data (clauses, block);
36807   return stmt;
36808 }
36809
36810 /* OpenACC 2.0:
36811   # pragma acc host_data <clauses> new-line
36812   structured-block  */
36813
36814 #define OACC_HOST_DATA_CLAUSE_MASK                                      \
36815   ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36816
36817 static tree
36818 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36819 {
36820   tree stmt, clauses, block;
36821   unsigned int save;
36822
36823   clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36824                                         "#pragma acc host_data", pragma_tok);
36825
36826   block = begin_omp_parallel ();
36827   save = cp_parser_begin_omp_structured_block (parser);
36828   cp_parser_statement (parser, NULL_TREE, false, if_p);
36829   cp_parser_end_omp_structured_block (parser, save);
36830   stmt = finish_oacc_host_data (clauses, block);
36831   return stmt;
36832 }
36833
36834 /* OpenACC 2.0:
36835    # pragma acc declare oacc-data-clause[optseq] new-line
36836 */
36837
36838 #define OACC_DECLARE_CLAUSE_MASK                                        \
36839         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
36840         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
36841         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
36842         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
36843         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
36844         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT)     \
36845         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK)                \
36846         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
36847         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
36848         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
36849         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
36850         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36851
36852 static tree
36853 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36854 {
36855   tree clauses, stmt;
36856   bool error = false;
36857
36858   clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36859                                         "#pragma acc declare", pragma_tok, true);
36860
36861
36862   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36863     {
36864       error_at (pragma_tok->location,
36865                 "no valid clauses specified in %<#pragma acc declare%>");
36866       return NULL_TREE;
36867     }
36868
36869   for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36870     {
36871       location_t loc = OMP_CLAUSE_LOCATION (t);
36872       tree decl = OMP_CLAUSE_DECL (t);
36873       if (!DECL_P (decl))
36874         {
36875           error_at (loc, "array section in %<#pragma acc declare%>");
36876           error = true;
36877           continue;
36878         }
36879       gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36880       switch (OMP_CLAUSE_MAP_KIND (t))
36881         {
36882         case GOMP_MAP_FIRSTPRIVATE_POINTER:
36883         case GOMP_MAP_FORCE_ALLOC:
36884         case GOMP_MAP_FORCE_TO:
36885         case GOMP_MAP_FORCE_DEVICEPTR:
36886         case GOMP_MAP_DEVICE_RESIDENT:
36887           break;
36888
36889         case GOMP_MAP_LINK:
36890           if (!global_bindings_p ()
36891               && (TREE_STATIC (decl)
36892                || !DECL_EXTERNAL (decl)))
36893             {
36894               error_at (loc,
36895                         "%qD must be a global variable in "
36896                         "%<#pragma acc declare link%>",
36897                         decl);
36898               error = true;
36899               continue;
36900             }
36901           break;
36902
36903         default:
36904           if (global_bindings_p ())
36905             {
36906               error_at (loc, "invalid OpenACC clause at file scope");
36907               error = true;
36908               continue;
36909             }
36910           if (DECL_EXTERNAL (decl))
36911             {
36912               error_at (loc,
36913                         "invalid use of %<extern%> variable %qD "
36914                         "in %<#pragma acc declare%>", decl);
36915               error = true;
36916               continue;
36917             }
36918           else if (TREE_PUBLIC (decl))
36919             {
36920               error_at (loc,
36921                         "invalid use of %<global%> variable %qD "
36922                         "in %<#pragma acc declare%>", decl);
36923               error = true;
36924               continue;
36925             }
36926           break;
36927         }
36928
36929       if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36930           || lookup_attribute ("omp declare target link",
36931                                DECL_ATTRIBUTES (decl)))
36932         {
36933           error_at (loc, "variable %qD used more than once with "
36934                     "%<#pragma acc declare%>", decl);
36935           error = true;
36936           continue;
36937         }
36938
36939       if (!error)
36940         {
36941           tree id;
36942
36943           if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36944             id = get_identifier ("omp declare target link");
36945           else
36946             id = get_identifier ("omp declare target");
36947
36948           DECL_ATTRIBUTES (decl)
36949             = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36950           if (global_bindings_p ())
36951             {
36952               symtab_node *node = symtab_node::get (decl);
36953               if (node != NULL)
36954                 {
36955                   node->offloadable = 1;
36956                   if (ENABLE_OFFLOADING)
36957                     {
36958                       g->have_offload = true;
36959                       if (is_a <varpool_node *> (node))
36960                         vec_safe_push (offload_vars, decl);
36961                     }
36962                 }
36963             }
36964         }
36965     }
36966
36967   if (error || global_bindings_p ())
36968     return NULL_TREE;
36969
36970   stmt = make_node (OACC_DECLARE);
36971   TREE_TYPE (stmt) = void_type_node;
36972   OACC_DECLARE_CLAUSES (stmt) = clauses;
36973   SET_EXPR_LOCATION (stmt, pragma_tok->location);
36974
36975   add_stmt (stmt);
36976
36977   return NULL_TREE;
36978 }
36979
36980 /* OpenACC 2.0:
36981    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36982
36983    or
36984
36985    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36986
36987    LOC is the location of the #pragma token.
36988 */
36989
36990 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
36991         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
36992         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
36993         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
36994         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
36995         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
36996         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
36997         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36998
36999 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
37000         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37001         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37002         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
37003         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
37004         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37005
37006 static tree
37007 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37008                                 bool enter)
37009 {
37010   location_t loc = pragma_tok->location;
37011   tree stmt, clauses;
37012   const char *p = "";
37013
37014   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37015     p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37016
37017   if (strcmp (p, "data") != 0)
37018     {
37019       error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37020                 enter ? "enter" : "exit");
37021       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37022       return NULL_TREE;
37023     }
37024
37025   cp_lexer_consume_token (parser->lexer);
37026
37027   if (enter)
37028     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37029                                          "#pragma acc enter data", pragma_tok);
37030   else
37031     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37032                                          "#pragma acc exit data", pragma_tok);
37033
37034   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37035     {
37036       error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37037                 enter ? "enter" : "exit");
37038       return NULL_TREE;
37039     }
37040
37041   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37042   TREE_TYPE (stmt) = void_type_node;
37043   OMP_STANDALONE_CLAUSES (stmt) = clauses;
37044   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37045   add_stmt (stmt);
37046   return stmt;
37047 }
37048
37049 /* OpenACC 2.0:
37050    # pragma acc loop oacc-loop-clause[optseq] new-line
37051      structured-block  */
37052
37053 #define OACC_LOOP_CLAUSE_MASK                                           \
37054         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
37055         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
37056         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
37057         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
37058         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
37059         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
37060         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO)                \
37061         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT)         \
37062         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ)                 \
37063         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37064
37065 static tree
37066 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37067                      omp_clause_mask mask, tree *cclauses, bool *if_p)
37068 {
37069   bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37070
37071   strcat (p_name, " loop");
37072   mask |= OACC_LOOP_CLAUSE_MASK;
37073
37074   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37075                                              cclauses == NULL);
37076   if (cclauses)
37077     {
37078       clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37079       if (*cclauses)
37080         *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37081       if (clauses)
37082         clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37083     }
37084
37085   tree block = begin_omp_structured_block ();
37086   int save = cp_parser_begin_omp_structured_block (parser);
37087   tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37088   cp_parser_end_omp_structured_block (parser, save);
37089   add_stmt (finish_omp_structured_block (block));
37090
37091   return stmt;
37092 }
37093
37094 /* OpenACC 2.0:
37095    # pragma acc kernels oacc-kernels-clause[optseq] new-line
37096      structured-block
37097
37098    or
37099
37100    # pragma acc parallel oacc-parallel-clause[optseq] new-line
37101      structured-block
37102 */
37103
37104 #define OACC_KERNELS_CLAUSE_MASK                                        \
37105         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37106         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
37107         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
37108         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
37109         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
37110         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
37111         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
37112         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37113         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
37114         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
37115         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
37116         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
37117         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
37118         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
37119         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
37120         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
37121         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37122
37123 #define OACC_PARALLEL_CLAUSE_MASK                                       \
37124         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37125         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
37126         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
37127         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
37128         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
37129         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT)             \
37130         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
37131         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE)        \
37132         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37133         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
37134         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
37135         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
37136         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
37137         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
37138         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
37139         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
37140         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE)             \
37141         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
37142         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
37143         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37144
37145 static tree
37146 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37147                                  char *p_name, bool *if_p)
37148 {
37149   omp_clause_mask mask;
37150   enum tree_code code;
37151   switch (cp_parser_pragma_kind (pragma_tok))
37152     {
37153     case PRAGMA_OACC_KERNELS:
37154       strcat (p_name, " kernels");
37155       mask = OACC_KERNELS_CLAUSE_MASK;
37156       code = OACC_KERNELS;
37157       break;
37158     case PRAGMA_OACC_PARALLEL:
37159       strcat (p_name, " parallel");
37160       mask = OACC_PARALLEL_CLAUSE_MASK;
37161       code = OACC_PARALLEL;
37162       break;
37163     default:
37164       gcc_unreachable ();
37165     }
37166
37167   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37168     {
37169       const char *p
37170         = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37171       if (strcmp (p, "loop") == 0)
37172         {
37173           cp_lexer_consume_token (parser->lexer);
37174           tree block = begin_omp_parallel ();
37175           tree clauses;
37176           cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37177                                if_p);
37178           return finish_omp_construct (code, block, clauses);
37179         }
37180     }
37181
37182   tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37183
37184   tree block = begin_omp_parallel ();
37185   unsigned int save = cp_parser_begin_omp_structured_block (parser);
37186   cp_parser_statement (parser, NULL_TREE, false, if_p);
37187   cp_parser_end_omp_structured_block (parser, save);
37188   return finish_omp_construct (code, block, clauses);
37189 }
37190
37191 /* OpenACC 2.0:
37192    # pragma acc update oacc-update-clause[optseq] new-line
37193 */
37194
37195 #define OACC_UPDATE_CLAUSE_MASK                                         \
37196         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
37197         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
37198         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
37199         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
37200         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
37201         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37202
37203 static tree
37204 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37205 {
37206   tree stmt, clauses;
37207
37208   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37209                                          "#pragma acc update", pragma_tok);
37210
37211   if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37212     {
37213       error_at (pragma_tok->location,
37214                 "%<#pragma acc update%> must contain at least one "
37215                 "%<device%> or %<host%> or %<self%> clause");
37216       return NULL_TREE;
37217     }
37218
37219   stmt = make_node (OACC_UPDATE);
37220   TREE_TYPE (stmt) = void_type_node;
37221   OACC_UPDATE_CLAUSES (stmt) = clauses;
37222   SET_EXPR_LOCATION (stmt, pragma_tok->location);
37223   add_stmt (stmt);
37224   return stmt;
37225 }
37226
37227 /* OpenACC 2.0:
37228    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37229
37230    LOC is the location of the #pragma token.
37231 */
37232
37233 #define OACC_WAIT_CLAUSE_MASK                                   \
37234         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37235
37236 static tree
37237 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37238 {
37239   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37240   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37241
37242   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37243     list = cp_parser_oacc_wait_list (parser, loc, list);
37244
37245   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37246                                         "#pragma acc wait", pragma_tok);
37247
37248   stmt = c_finish_oacc_wait (loc, list, clauses);
37249   stmt = finish_expr_stmt (stmt);
37250
37251   return stmt;
37252 }
37253
37254 /* OpenMP 4.0:
37255    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
37256
37257 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
37258         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
37259         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
37260         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
37261         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
37262         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
37263         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37264
37265 static void
37266 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37267                             enum pragma_context context)
37268 {
37269   bool first_p = parser->omp_declare_simd == NULL;
37270   cp_omp_declare_simd_data data;
37271   if (first_p)
37272     {
37273       data.error_seen = false;
37274       data.fndecl_seen = false;
37275       data.tokens = vNULL;
37276       data.clauses = NULL_TREE;
37277       /* It is safe to take the address of a local variable; it will only be
37278          used while this scope is live.  */
37279       parser->omp_declare_simd = &data;
37280     }
37281
37282   /* Store away all pragma tokens.  */
37283   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37284          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37285     cp_lexer_consume_token (parser->lexer);
37286   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37287     parser->omp_declare_simd->error_seen = true;
37288   cp_parser_require_pragma_eol (parser, pragma_tok);
37289   struct cp_token_cache *cp
37290     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37291   parser->omp_declare_simd->tokens.safe_push (cp);
37292
37293   if (first_p)
37294     {
37295       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37296         cp_parser_pragma (parser, context, NULL);
37297       switch (context)
37298         {
37299         case pragma_external:
37300           cp_parser_declaration (parser);
37301           break;
37302         case pragma_member:
37303           cp_parser_member_declaration (parser);
37304           break;
37305         case pragma_objc_icode:
37306           cp_parser_block_declaration (parser, /*statement_p=*/false);
37307           break;
37308         default:
37309           cp_parser_declaration_statement (parser);
37310           break;
37311         }
37312       if (parser->omp_declare_simd
37313           && !parser->omp_declare_simd->error_seen
37314           && !parser->omp_declare_simd->fndecl_seen)
37315         error_at (pragma_tok->location,
37316                   "%<#pragma omp declare simd%> not immediately followed by "
37317                   "function declaration or definition");
37318       data.tokens.release ();
37319       parser->omp_declare_simd = NULL;
37320     }
37321 }
37322
37323 /* Finalize #pragma omp declare simd clauses after direct declarator has
37324    been parsed, and put that into "omp declare simd" attribute.  */
37325
37326 static tree
37327 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37328 {
37329   struct cp_token_cache *ce;
37330   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37331   int i;
37332
37333   if (!data->error_seen && data->fndecl_seen)
37334     {
37335       error ("%<#pragma omp declare simd%> not immediately followed by "
37336              "a single function declaration or definition");
37337       data->error_seen = true;
37338     }
37339   if (data->error_seen)
37340     return attrs;
37341
37342   FOR_EACH_VEC_ELT (data->tokens, i, ce)
37343     {
37344       tree c, cl;
37345
37346       cp_parser_push_lexer_for_tokens (parser, ce);
37347       parser->lexer->in_pragma = true;
37348       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37349       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37350       cp_lexer_consume_token (parser->lexer);
37351       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37352                                       "#pragma omp declare simd", pragma_tok);
37353       cp_parser_pop_lexer (parser);
37354       if (cl)
37355         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37356       c = build_tree_list (get_identifier ("omp declare simd"), cl);
37357       TREE_CHAIN (c) = attrs;
37358       if (processing_template_decl)
37359         ATTR_IS_DEPENDENT (c) = 1;
37360       attrs = c;
37361     }
37362
37363   data->fndecl_seen = true;
37364   return attrs;
37365 }
37366
37367
37368 /* OpenMP 4.0:
37369    # pragma omp declare target new-line
37370    declarations and definitions
37371    # pragma omp end declare target new-line
37372
37373    OpenMP 4.5:
37374    # pragma omp declare target ( extended-list ) new-line
37375
37376    # pragma omp declare target declare-target-clauses[seq] new-line  */
37377
37378 #define OMP_DECLARE_TARGET_CLAUSE_MASK                          \
37379         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
37380         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37381
37382 static void
37383 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37384 {
37385   tree clauses = NULL_TREE;
37386   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37387     clauses
37388       = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37389                                    "#pragma omp declare target", pragma_tok);
37390   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37391     {
37392       clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37393                                         clauses);
37394       clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37395       cp_parser_require_pragma_eol (parser, pragma_tok);
37396     }
37397   else
37398     {
37399       cp_parser_require_pragma_eol (parser, pragma_tok);
37400       scope_chain->omp_declare_target_attribute++;
37401       return;
37402     }
37403   if (scope_chain->omp_declare_target_attribute)
37404     error_at (pragma_tok->location,
37405               "%<#pragma omp declare target%> with clauses in between "
37406               "%<#pragma omp declare target%> without clauses and "
37407               "%<#pragma omp end declare target%>");
37408   for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37409     {
37410       tree t = OMP_CLAUSE_DECL (c), id;
37411       tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37412       tree at2 = lookup_attribute ("omp declare target link",
37413                                    DECL_ATTRIBUTES (t));
37414       if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37415         {
37416           id = get_identifier ("omp declare target link");
37417           std::swap (at1, at2);
37418         }
37419       else
37420         id = get_identifier ("omp declare target");
37421       if (at2)
37422         {
37423           error_at (OMP_CLAUSE_LOCATION (c),
37424                     "%qD specified both in declare target %<link%> and %<to%>"
37425                     " clauses", t);
37426           continue;
37427         }
37428       if (!at1)
37429         {
37430           DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37431           if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37432             continue;
37433
37434           symtab_node *node = symtab_node::get (t);
37435           if (node != NULL)
37436             {
37437               node->offloadable = 1;
37438               if (ENABLE_OFFLOADING)
37439                 {
37440                   g->have_offload = true;
37441                   if (is_a <varpool_node *> (node))
37442                     vec_safe_push (offload_vars, t);
37443                 }
37444             }
37445         }
37446     }
37447 }
37448
37449 static void
37450 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37451 {
37452   const char *p = "";
37453   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37454     {
37455       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37456       p = IDENTIFIER_POINTER (id);
37457     }
37458   if (strcmp (p, "declare") == 0)
37459     {
37460       cp_lexer_consume_token (parser->lexer);
37461       p = "";
37462       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37463         {
37464           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37465           p = IDENTIFIER_POINTER (id);
37466         }
37467       if (strcmp (p, "target") == 0)
37468         cp_lexer_consume_token (parser->lexer);
37469       else
37470         {
37471           cp_parser_error (parser, "expected %<target%>");
37472           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37473           return;
37474         }
37475     }
37476   else
37477     {
37478       cp_parser_error (parser, "expected %<declare%>");
37479       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37480       return;
37481     }
37482   cp_parser_require_pragma_eol (parser, pragma_tok);
37483   if (!scope_chain->omp_declare_target_attribute)
37484     error_at (pragma_tok->location,
37485               "%<#pragma omp end declare target%> without corresponding "
37486               "%<#pragma omp declare target%>");
37487   else
37488     scope_chain->omp_declare_target_attribute--;
37489 }
37490
37491 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
37492    expression and optional initializer clause of
37493    #pragma omp declare reduction.  We store the expression(s) as
37494    either 3, 6 or 7 special statements inside of the artificial function's
37495    body.  The first two statements are DECL_EXPRs for the artificial
37496    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37497    expression that uses those variables.
37498    If there was any INITIALIZER clause, this is followed by further statements,
37499    the fourth and fifth statements are DECL_EXPRs for the artificial
37500    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
37501    constructor variant (first token after open paren is not omp_priv),
37502    then the sixth statement is a statement with the function call expression
37503    that uses the OMP_PRIV and optionally OMP_ORIG variable.
37504    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37505    to initialize the OMP_PRIV artificial variable and there is seventh
37506    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
37507
37508 static bool
37509 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37510 {
37511   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37512   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37513   type = TREE_TYPE (type);
37514   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37515   DECL_ARTIFICIAL (omp_out) = 1;
37516   pushdecl (omp_out);
37517   add_decl_expr (omp_out);
37518   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37519   DECL_ARTIFICIAL (omp_in) = 1;
37520   pushdecl (omp_in);
37521   add_decl_expr (omp_in);
37522   tree combiner;
37523   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37524
37525   keep_next_level (true);
37526   tree block = begin_omp_structured_block ();
37527   combiner = cp_parser_expression (parser);
37528   finish_expr_stmt (combiner);
37529   block = finish_omp_structured_block (block);
37530   add_stmt (block);
37531
37532   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37533     return false;
37534
37535   const char *p = "";
37536   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37537     {
37538       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37539       p = IDENTIFIER_POINTER (id);
37540     }
37541
37542   if (strcmp (p, "initializer") == 0)
37543     {
37544       cp_lexer_consume_token (parser->lexer);
37545       matching_parens parens;
37546       if (!parens.require_open (parser))
37547         return false;
37548
37549       p = "";
37550       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37551         {
37552           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37553           p = IDENTIFIER_POINTER (id);
37554         }
37555
37556       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37557       DECL_ARTIFICIAL (omp_priv) = 1;
37558       pushdecl (omp_priv);
37559       add_decl_expr (omp_priv);
37560       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37561       DECL_ARTIFICIAL (omp_orig) = 1;
37562       pushdecl (omp_orig);
37563       add_decl_expr (omp_orig);
37564
37565       keep_next_level (true);
37566       block = begin_omp_structured_block ();
37567
37568       bool ctor = false;
37569       if (strcmp (p, "omp_priv") == 0)
37570         {
37571           bool is_direct_init, is_non_constant_init;
37572           ctor = true;
37573           cp_lexer_consume_token (parser->lexer);
37574           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
37575           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37576               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37577                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37578                      == CPP_CLOSE_PAREN
37579                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37580                      == CPP_CLOSE_PAREN))
37581             {
37582               finish_omp_structured_block (block);
37583               error ("invalid initializer clause");
37584               return false;
37585             }
37586           initializer = cp_parser_initializer (parser, &is_direct_init,
37587                                                &is_non_constant_init);
37588           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37589                           NULL_TREE, LOOKUP_ONLYCONVERTING);
37590         }
37591       else
37592         {
37593           cp_parser_parse_tentatively (parser);
37594           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37595                                                   /*check_dependency_p=*/true,
37596                                                   /*template_p=*/NULL,
37597                                                   /*declarator_p=*/false,
37598                                                   /*optional_p=*/false);
37599           vec<tree, va_gc> *args;
37600           if (fn_name == error_mark_node
37601               || cp_parser_error_occurred (parser)
37602               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37603               || ((args = cp_parser_parenthesized_expression_list
37604                                 (parser, non_attr, /*cast_p=*/false,
37605                                  /*allow_expansion_p=*/true,
37606                                  /*non_constant_p=*/NULL)),
37607                   cp_parser_error_occurred (parser)))
37608             {
37609               finish_omp_structured_block (block);
37610               cp_parser_abort_tentative_parse (parser);
37611               cp_parser_error (parser, "expected id-expression (arguments)");
37612               return false;
37613             }
37614           unsigned int i;
37615           tree arg;
37616           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37617             if (arg == omp_priv
37618                 || (TREE_CODE (arg) == ADDR_EXPR
37619                     && TREE_OPERAND (arg, 0) == omp_priv))
37620               break;
37621           cp_parser_abort_tentative_parse (parser);
37622           if (arg == NULL_TREE)
37623             error ("one of the initializer call arguments should be %<omp_priv%>"
37624                    " or %<&omp_priv%>");
37625           initializer = cp_parser_postfix_expression (parser, false, false, false,
37626                                                       false, NULL);
37627           finish_expr_stmt (initializer);
37628         }
37629
37630       block = finish_omp_structured_block (block);
37631       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37632       add_stmt (block);
37633
37634       if (ctor)
37635         add_decl_expr (omp_orig);
37636
37637       if (!parens.require_close (parser))
37638         return false;
37639     }
37640
37641   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37642     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37643                               UNKNOWN_LOCATION);
37644
37645   return true;
37646 }
37647
37648 /* OpenMP 4.0
37649    #pragma omp declare reduction (reduction-id : typename-list : expression) \
37650       initializer-clause[opt] new-line
37651
37652    initializer-clause:
37653       initializer (omp_priv initializer)
37654       initializer (function-name (argument-list))  */
37655
37656 static void
37657 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37658                                  enum pragma_context)
37659 {
37660   auto_vec<tree> types;
37661   enum tree_code reduc_code = ERROR_MARK;
37662   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37663   unsigned int i;
37664   cp_token *first_token;
37665   cp_token_cache *cp;
37666   int errs;
37667   void *p;
37668     
37669   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
37670   p = obstack_alloc (&declarator_obstack, 0);
37671
37672   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37673     goto fail;
37674
37675   switch (cp_lexer_peek_token (parser->lexer)->type)
37676     {
37677     case CPP_PLUS:
37678       reduc_code = PLUS_EXPR;
37679       break;
37680     case CPP_MULT:
37681       reduc_code = MULT_EXPR;
37682       break;
37683     case CPP_MINUS:
37684       reduc_code = MINUS_EXPR;
37685       break;
37686     case CPP_AND:
37687       reduc_code = BIT_AND_EXPR;
37688       break;
37689     case CPP_XOR:
37690       reduc_code = BIT_XOR_EXPR;
37691       break;
37692     case CPP_OR:
37693       reduc_code = BIT_IOR_EXPR;
37694       break;
37695     case CPP_AND_AND:
37696       reduc_code = TRUTH_ANDIF_EXPR;
37697       break;
37698     case CPP_OR_OR:
37699       reduc_code = TRUTH_ORIF_EXPR;
37700       break;
37701     case CPP_NAME:
37702       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37703       break;
37704     default:
37705       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37706                                "%<|%>, %<&&%>, %<||%> or identifier");
37707       goto fail;
37708     }
37709
37710   if (reduc_code != ERROR_MARK)
37711     cp_lexer_consume_token (parser->lexer);
37712
37713   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37714   if (reduc_id == error_mark_node)
37715     goto fail;
37716
37717   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37718     goto fail;
37719
37720   /* Types may not be defined in declare reduction type list.  */
37721   const char *saved_message;
37722   saved_message = parser->type_definition_forbidden_message;
37723   parser->type_definition_forbidden_message
37724     = G_("types may not be defined in declare reduction type list");
37725   bool saved_colon_corrects_to_scope_p;
37726   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37727   parser->colon_corrects_to_scope_p = false;
37728   bool saved_colon_doesnt_start_class_def_p;
37729   saved_colon_doesnt_start_class_def_p
37730     = parser->colon_doesnt_start_class_def_p;
37731   parser->colon_doesnt_start_class_def_p = true;
37732
37733   while (true)
37734     {
37735       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37736       type = cp_parser_type_id (parser);
37737       if (type == error_mark_node)
37738         ;
37739       else if (ARITHMETIC_TYPE_P (type)
37740                && (orig_reduc_id == NULL_TREE
37741                    || (TREE_CODE (type) != COMPLEX_TYPE
37742                        && (id_equal (orig_reduc_id, "min")
37743                            || id_equal (orig_reduc_id, "max")))))
37744         error_at (loc, "predeclared arithmetic type %qT in "
37745                        "%<#pragma omp declare reduction%>", type);
37746       else if (TREE_CODE (type) == FUNCTION_TYPE
37747                || TREE_CODE (type) == METHOD_TYPE
37748                || TREE_CODE (type) == ARRAY_TYPE)
37749         error_at (loc, "function or array type %qT in "
37750                        "%<#pragma omp declare reduction%>", type);
37751       else if (TREE_CODE (type) == REFERENCE_TYPE)
37752         error_at (loc, "reference type %qT in "
37753                        "%<#pragma omp declare reduction%>", type);
37754       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37755         error_at (loc, "const, volatile or __restrict qualified type %qT in "
37756                        "%<#pragma omp declare reduction%>", type);
37757       else
37758         types.safe_push (type);
37759
37760       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37761         cp_lexer_consume_token (parser->lexer);
37762       else
37763         break;
37764     }
37765
37766   /* Restore the saved message.  */
37767   parser->type_definition_forbidden_message = saved_message;
37768   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37769   parser->colon_doesnt_start_class_def_p
37770     = saved_colon_doesnt_start_class_def_p;
37771
37772   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37773       || types.is_empty ())
37774     {
37775      fail:
37776       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37777       goto done;
37778     }
37779
37780   first_token = cp_lexer_peek_token (parser->lexer);
37781   cp = NULL;
37782   errs = errorcount;
37783   FOR_EACH_VEC_ELT (types, i, type)
37784     {
37785       tree fntype
37786         = build_function_type_list (void_type_node,
37787                                     cp_build_reference_type (type, false),
37788                                     NULL_TREE);
37789       tree this_reduc_id = reduc_id;
37790       if (!dependent_type_p (type))
37791         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37792       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37793       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37794       DECL_ARTIFICIAL (fndecl) = 1;
37795       DECL_EXTERNAL (fndecl) = 1;
37796       DECL_DECLARED_INLINE_P (fndecl) = 1;
37797       DECL_IGNORED_P (fndecl) = 1;
37798       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37799       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37800       DECL_ATTRIBUTES (fndecl)
37801         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37802                      DECL_ATTRIBUTES (fndecl));
37803       if (processing_template_decl)
37804         fndecl = push_template_decl (fndecl);
37805       bool block_scope = false;
37806       tree block = NULL_TREE;
37807       if (current_function_decl)
37808         {
37809           block_scope = true;
37810           DECL_CONTEXT (fndecl) = global_namespace;
37811           if (!processing_template_decl)
37812             pushdecl (fndecl);
37813         }
37814       else if (current_class_type)
37815         {
37816           if (cp == NULL)
37817             {
37818               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37819                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37820                 cp_lexer_consume_token (parser->lexer);
37821               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37822                 goto fail;
37823               cp = cp_token_cache_new (first_token,
37824                                        cp_lexer_peek_nth_token (parser->lexer,
37825                                                                 2));
37826             }
37827           DECL_STATIC_FUNCTION_P (fndecl) = 1;
37828           finish_member_declaration (fndecl);
37829           DECL_PENDING_INLINE_INFO (fndecl) = cp;
37830           DECL_PENDING_INLINE_P (fndecl) = 1;
37831           vec_safe_push (unparsed_funs_with_definitions, fndecl);
37832           continue;
37833         }
37834       else
37835         {
37836           DECL_CONTEXT (fndecl) = current_namespace;
37837           pushdecl (fndecl);
37838         }
37839       if (!block_scope)
37840         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37841       else
37842         block = begin_omp_structured_block ();
37843       if (cp)
37844         {
37845           cp_parser_push_lexer_for_tokens (parser, cp);
37846           parser->lexer->in_pragma = true;
37847         }
37848       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37849         {
37850           if (!block_scope)
37851             finish_function (/*inline_p=*/false);
37852           else
37853             DECL_CONTEXT (fndecl) = current_function_decl;
37854           if (cp)
37855             cp_parser_pop_lexer (parser);
37856           goto fail;
37857         }
37858       if (cp)
37859         cp_parser_pop_lexer (parser);
37860       if (!block_scope)
37861         finish_function (/*inline_p=*/false);
37862       else
37863         {
37864           DECL_CONTEXT (fndecl) = current_function_decl;
37865           block = finish_omp_structured_block (block);
37866           if (TREE_CODE (block) == BIND_EXPR)
37867             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37868           else if (TREE_CODE (block) == STATEMENT_LIST)
37869             DECL_SAVED_TREE (fndecl) = block;
37870           if (processing_template_decl)
37871             add_decl_expr (fndecl);
37872         }
37873       cp_check_omp_declare_reduction (fndecl);
37874       if (cp == NULL && types.length () > 1)
37875         cp = cp_token_cache_new (first_token,
37876                                  cp_lexer_peek_nth_token (parser->lexer, 2));
37877       if (errs != errorcount)
37878         break;
37879     }
37880
37881   cp_parser_require_pragma_eol (parser, pragma_tok);
37882
37883  done:
37884   /* Free any declarators allocated.  */
37885   obstack_free (&declarator_obstack, p);
37886 }
37887
37888 /* OpenMP 4.0
37889    #pragma omp declare simd declare-simd-clauses[optseq] new-line
37890    #pragma omp declare reduction (reduction-id : typename-list : expression) \
37891       initializer-clause[opt] new-line
37892    #pragma omp declare target new-line  */
37893
37894 static bool
37895 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37896                        enum pragma_context context)
37897 {
37898   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37899     {
37900       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37901       const char *p = IDENTIFIER_POINTER (id);
37902
37903       if (strcmp (p, "simd") == 0)
37904         {
37905           cp_lexer_consume_token (parser->lexer);
37906           cp_parser_omp_declare_simd (parser, pragma_tok,
37907                                       context);
37908           return true;
37909         }
37910       cp_ensure_no_omp_declare_simd (parser);
37911       if (strcmp (p, "reduction") == 0)
37912         {
37913           cp_lexer_consume_token (parser->lexer);
37914           cp_parser_omp_declare_reduction (parser, pragma_tok,
37915                                            context);
37916           return false;
37917         }
37918       if (!flag_openmp)  /* flag_openmp_simd  */
37919         {
37920           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37921           return false;
37922         }
37923       if (strcmp (p, "target") == 0)
37924         {
37925           cp_lexer_consume_token (parser->lexer);
37926           cp_parser_omp_declare_target (parser, pragma_tok);
37927           return false;
37928         }
37929     }
37930   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37931                            "or %<target%>");
37932   cp_parser_require_pragma_eol (parser, pragma_tok);
37933   return false;
37934 }
37935
37936 /* OpenMP 4.5:
37937    #pragma omp taskloop taskloop-clause[optseq] new-line
37938      for-loop
37939
37940    #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37941      for-loop  */
37942
37943 #define OMP_TASKLOOP_CLAUSE_MASK                                \
37944         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
37945         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
37946         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37947         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
37948         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
37949         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE)    \
37950         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS)    \
37951         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE)     \
37952         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
37953         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
37954         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
37955         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
37956         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP)      \
37957         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37958
37959 static tree
37960 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37961                         char *p_name, omp_clause_mask mask, tree *cclauses,
37962                         bool *if_p)
37963 {
37964   tree clauses, sb, ret;
37965   unsigned int save;
37966   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37967
37968   strcat (p_name, " taskloop");
37969   mask |= OMP_TASKLOOP_CLAUSE_MASK;
37970
37971   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37972     {
37973       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37974       const char *p = IDENTIFIER_POINTER (id);
37975
37976       if (strcmp (p, "simd") == 0)
37977         {
37978           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37979           if (cclauses == NULL)
37980             cclauses = cclauses_buf;
37981
37982           cp_lexer_consume_token (parser->lexer);
37983           if (!flag_openmp)  /* flag_openmp_simd  */
37984             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37985                                        cclauses, if_p);
37986           sb = begin_omp_structured_block ();
37987           save = cp_parser_begin_omp_structured_block (parser);
37988           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37989                                     cclauses, if_p);
37990           cp_parser_end_omp_structured_block (parser, save);
37991           tree body = finish_omp_structured_block (sb);
37992           if (ret == NULL)
37993             return ret;
37994           ret = make_node (OMP_TASKLOOP);
37995           TREE_TYPE (ret) = void_type_node;
37996           OMP_FOR_BODY (ret) = body;
37997           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37998           SET_EXPR_LOCATION (ret, loc);
37999           add_stmt (ret);
38000           return ret;
38001         }
38002     }
38003   if (!flag_openmp)  /* flag_openmp_simd  */
38004     {
38005       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38006       return NULL_TREE;
38007     }
38008
38009   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38010                                        cclauses == NULL);
38011   if (cclauses)
38012     {
38013       cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38014       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38015     }
38016
38017   sb = begin_omp_structured_block ();
38018   save = cp_parser_begin_omp_structured_block (parser);
38019
38020   ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38021                                 if_p);
38022
38023   cp_parser_end_omp_structured_block (parser, save);
38024   add_stmt (finish_omp_structured_block (sb));
38025
38026   return ret;
38027 }
38028
38029
38030 /* OpenACC 2.0:
38031    # pragma acc routine oacc-routine-clause[optseq] new-line
38032      function-definition
38033
38034    # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38035 */
38036
38037 #define OACC_ROUTINE_CLAUSE_MASK                                        \
38038         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG)                \
38039         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER)              \
38040         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR)              \
38041         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38042
38043
38044 /* Parse the OpenACC routine pragma.  This has an optional '( name )'
38045    component, which must resolve to a declared namespace-scope
38046    function.  The clauses are either processed directly (for a named
38047    function), or defered until the immediatley following declaration
38048    is parsed.  */
38049
38050 static void
38051 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38052                         enum pragma_context context)
38053 {
38054   gcc_checking_assert (context == pragma_external);
38055   /* The checking for "another pragma following this one" in the "no optional
38056      '( name )'" case makes sure that we dont re-enter.  */
38057   gcc_checking_assert (parser->oacc_routine == NULL);
38058
38059   cp_oacc_routine_data data;
38060   data.error_seen = false;
38061   data.fndecl_seen = false;
38062   data.tokens = vNULL;
38063   data.clauses = NULL_TREE;
38064   data.loc = pragma_tok->location;
38065   /* It is safe to take the address of a local variable; it will only be
38066      used while this scope is live.  */
38067   parser->oacc_routine = &data;
38068
38069   /* Look for optional '( name )'.  */
38070   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38071     {
38072       matching_parens parens;
38073       parens.consume_open (parser); /* '(' */
38074
38075       /* We parse the name as an id-expression.  If it resolves to
38076          anything other than a non-overloaded function at namespace
38077          scope, it's an error.  */
38078       location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38079       tree name = cp_parser_id_expression (parser,
38080                                            /*template_keyword_p=*/false,
38081                                            /*check_dependency_p=*/false,
38082                                            /*template_p=*/NULL,
38083                                            /*declarator_p=*/false,
38084                                            /*optional_p=*/false);
38085       tree decl = (identifier_p (name)
38086                    ? cp_parser_lookup_name_simple (parser, name, name_loc)
38087                    : name);
38088       if (name != error_mark_node && decl == error_mark_node)
38089         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38090
38091       if (decl == error_mark_node
38092           || !parens.require_close (parser))
38093         {
38094           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38095           parser->oacc_routine = NULL;
38096           return;
38097         }
38098
38099       data.clauses
38100         = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38101                                       "#pragma acc routine",
38102                                       cp_lexer_peek_token (parser->lexer));
38103
38104       if (decl && is_overloaded_fn (decl)
38105           && (TREE_CODE (decl) != FUNCTION_DECL
38106               || DECL_FUNCTION_TEMPLATE_P  (decl)))
38107         {
38108           error_at (name_loc,
38109                     "%<#pragma acc routine%> names a set of overloads");
38110           parser->oacc_routine = NULL;
38111           return;
38112         }
38113
38114       /* Perhaps we should use the same rule as declarations in different
38115          namespaces?  */
38116       if (!DECL_NAMESPACE_SCOPE_P (decl))
38117         {
38118           error_at (name_loc,
38119                     "%qD does not refer to a namespace scope function", decl);
38120           parser->oacc_routine = NULL;
38121           return;
38122         }
38123
38124       if (TREE_CODE (decl) != FUNCTION_DECL)
38125         {
38126           error_at (name_loc, "%qD does not refer to a function", decl);
38127           parser->oacc_routine = NULL;
38128           return;
38129         }
38130
38131       cp_finalize_oacc_routine (parser, decl, false);
38132       parser->oacc_routine = NULL;
38133     }
38134   else /* No optional '( name )'.  */
38135     {
38136       /* Store away all pragma tokens.  */
38137       while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38138              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38139         cp_lexer_consume_token (parser->lexer);
38140       if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38141         parser->oacc_routine->error_seen = true;
38142       cp_parser_require_pragma_eol (parser, pragma_tok);
38143       struct cp_token_cache *cp
38144         = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38145       parser->oacc_routine->tokens.safe_push (cp);
38146
38147       /* Emit a helpful diagnostic if there's another pragma following this
38148          one.  */
38149       if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38150         {
38151           cp_ensure_no_oacc_routine (parser);
38152           data.tokens.release ();
38153           /* ..., and then just keep going.  */
38154           return;
38155         }
38156
38157       /* We only have to consider the pragma_external case here.  */
38158       cp_parser_declaration (parser);
38159       if (parser->oacc_routine
38160           && !parser->oacc_routine->fndecl_seen)
38161         cp_ensure_no_oacc_routine (parser);
38162       else
38163         parser->oacc_routine = NULL;
38164       data.tokens.release ();
38165     }
38166 }
38167
38168 /* Finalize #pragma acc routine clauses after direct declarator has
38169    been parsed.  */
38170
38171 static tree
38172 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38173 {
38174   struct cp_token_cache *ce;
38175   cp_oacc_routine_data *data = parser->oacc_routine;
38176
38177   if (!data->error_seen && data->fndecl_seen)
38178     {
38179       error_at (data->loc,
38180                 "%<#pragma acc routine%> not immediately followed by "
38181                 "a single function declaration or definition");
38182       data->error_seen = true;
38183     }
38184   if (data->error_seen)
38185     return attrs;
38186
38187   gcc_checking_assert (data->tokens.length () == 1);
38188   ce = data->tokens[0];
38189
38190   cp_parser_push_lexer_for_tokens (parser, ce);
38191   parser->lexer->in_pragma = true;
38192   gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38193
38194   cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38195   gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38196   parser->oacc_routine->clauses
38197     = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38198                                   "#pragma acc routine", pragma_tok);
38199   cp_parser_pop_lexer (parser);
38200   /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38201      fndecl_seen.  */
38202
38203   return attrs;
38204 }
38205
38206 /* Apply any saved OpenACC routine clauses to a just-parsed
38207    declaration.  */
38208
38209 static void
38210 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38211 {
38212   if (__builtin_expect (parser->oacc_routine != NULL, 0))
38213     {
38214       /* Keep going if we're in error reporting mode.  */
38215       if (parser->oacc_routine->error_seen
38216           || fndecl == error_mark_node)
38217         return;
38218
38219       if (parser->oacc_routine->fndecl_seen)
38220         {
38221           error_at (parser->oacc_routine->loc,
38222                     "%<#pragma acc routine%> not immediately followed by"
38223                     " a single function declaration or definition");
38224           parser->oacc_routine = NULL;
38225           return;
38226         }
38227       if (TREE_CODE (fndecl) != FUNCTION_DECL)
38228         {
38229           cp_ensure_no_oacc_routine (parser);
38230           return;
38231         }
38232
38233       if (oacc_get_fn_attrib (fndecl))
38234         {
38235           error_at (parser->oacc_routine->loc,
38236                     "%<#pragma acc routine%> already applied to %qD", fndecl);
38237           parser->oacc_routine = NULL;
38238           return;
38239         }
38240
38241       if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38242         {
38243           error_at (parser->oacc_routine->loc,
38244                     TREE_USED (fndecl)
38245                     ? G_("%<#pragma acc routine%> must be applied before use")
38246                     : G_("%<#pragma acc routine%> must be applied before "
38247                          "definition"));
38248           parser->oacc_routine = NULL;
38249           return;
38250         }
38251
38252       /* Process the routine's dimension clauses.  */
38253       tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38254       oacc_replace_fn_attrib (fndecl, dims);
38255
38256       /* Add an "omp declare target" attribute.  */
38257       DECL_ATTRIBUTES (fndecl)
38258         = tree_cons (get_identifier ("omp declare target"),
38259                      NULL_TREE, DECL_ATTRIBUTES (fndecl));
38260
38261       /* Don't unset parser->oacc_routine here: we may still need it to
38262          diagnose wrong usage.  But, remember that we've used this "#pragma acc
38263          routine".  */
38264       parser->oacc_routine->fndecl_seen = true;
38265     }
38266 }
38267
38268 /* Main entry point to OpenMP statement pragmas.  */
38269
38270 static void
38271 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38272 {
38273   tree stmt;
38274   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38275   omp_clause_mask mask (0);
38276
38277   switch (cp_parser_pragma_kind (pragma_tok))
38278     {
38279     case PRAGMA_OACC_ATOMIC:
38280       cp_parser_omp_atomic (parser, pragma_tok);
38281       return;
38282     case PRAGMA_OACC_CACHE:
38283       stmt = cp_parser_oacc_cache (parser, pragma_tok);
38284       break;
38285     case PRAGMA_OACC_DATA:
38286       stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38287       break;
38288     case PRAGMA_OACC_ENTER_DATA:
38289       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38290       break;
38291     case PRAGMA_OACC_EXIT_DATA:
38292       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38293       break;
38294     case PRAGMA_OACC_HOST_DATA:
38295       stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38296       break;
38297     case PRAGMA_OACC_KERNELS:
38298     case PRAGMA_OACC_PARALLEL:
38299       strcpy (p_name, "#pragma acc");
38300       stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38301                                               if_p);
38302       break;
38303     case PRAGMA_OACC_LOOP:
38304       strcpy (p_name, "#pragma acc");
38305       stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38306                                   if_p);
38307       break;
38308     case PRAGMA_OACC_UPDATE:
38309       stmt = cp_parser_oacc_update (parser, pragma_tok);
38310       break;
38311     case PRAGMA_OACC_WAIT:
38312       stmt = cp_parser_oacc_wait (parser, pragma_tok);
38313       break;
38314     case PRAGMA_OMP_ATOMIC:
38315       cp_parser_omp_atomic (parser, pragma_tok);
38316       return;
38317     case PRAGMA_OMP_CRITICAL:
38318       stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38319       break;
38320     case PRAGMA_OMP_DISTRIBUTE:
38321       strcpy (p_name, "#pragma omp");
38322       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38323                                        if_p);
38324       break;
38325     case PRAGMA_OMP_FOR:
38326       strcpy (p_name, "#pragma omp");
38327       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38328                                 if_p);
38329       break;
38330     case PRAGMA_OMP_MASTER:
38331       stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38332       break;
38333     case PRAGMA_OMP_PARALLEL:
38334       strcpy (p_name, "#pragma omp");
38335       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38336                                      if_p);
38337       break;
38338     case PRAGMA_OMP_SECTIONS:
38339       strcpy (p_name, "#pragma omp");
38340       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38341       break;
38342     case PRAGMA_OMP_SIMD:
38343       strcpy (p_name, "#pragma omp");
38344       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38345                                  if_p);
38346       break;
38347     case PRAGMA_OMP_SINGLE:
38348       stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38349       break;
38350     case PRAGMA_OMP_TASK:
38351       stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38352       break;
38353     case PRAGMA_OMP_TASKGROUP:
38354       stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38355       break;
38356     case PRAGMA_OMP_TASKLOOP:
38357       strcpy (p_name, "#pragma omp");
38358       stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38359                                      if_p);
38360       break;
38361     case PRAGMA_OMP_TEAMS:
38362       strcpy (p_name, "#pragma omp");
38363       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38364                                   if_p);
38365       break;
38366     default:
38367       gcc_unreachable ();
38368     }
38369
38370   protected_set_expr_location (stmt, pragma_tok->location);
38371 }
38372 \f
38373 /* Transactional Memory parsing routines.  */
38374
38375 /* Parse a transaction attribute.
38376
38377    txn-attribute:
38378         attribute
38379         [ [ identifier ] ]
38380
38381    We use this instead of cp_parser_attributes_opt for transactions to avoid
38382    the pedwarn in C++98 mode.  */
38383
38384 static tree
38385 cp_parser_txn_attribute_opt (cp_parser *parser)
38386 {
38387   cp_token *token;
38388   tree attr_name, attr = NULL;
38389
38390   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38391     return cp_parser_attributes_opt (parser);
38392
38393   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38394     return NULL_TREE;
38395   cp_lexer_consume_token (parser->lexer);
38396   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38397     goto error1;
38398
38399   token = cp_lexer_peek_token (parser->lexer);
38400   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38401     {
38402       token = cp_lexer_consume_token (parser->lexer);
38403
38404       attr_name = (token->type == CPP_KEYWORD
38405                    /* For keywords, use the canonical spelling,
38406                       not the parsed identifier.  */
38407                    ? ridpointers[(int) token->keyword]
38408                    : token->u.value);
38409       attr = build_tree_list (attr_name, NULL_TREE);
38410     }
38411   else
38412     cp_parser_error (parser, "expected identifier");
38413
38414   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38415  error1:
38416   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38417   return attr;
38418 }
38419
38420 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38421
38422    transaction-statement:
38423      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38424        compound-statement
38425      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38426 */
38427
38428 static tree
38429 cp_parser_transaction (cp_parser *parser, cp_token *token)
38430 {
38431   unsigned char old_in = parser->in_transaction;
38432   unsigned char this_in = 1, new_in;
38433   enum rid keyword = token->keyword;
38434   tree stmt, attrs, noex;
38435
38436   cp_lexer_consume_token (parser->lexer);
38437
38438   if (keyword == RID_TRANSACTION_RELAXED
38439       || keyword == RID_SYNCHRONIZED)
38440     this_in |= TM_STMT_ATTR_RELAXED;
38441   else
38442     {
38443       attrs = cp_parser_txn_attribute_opt (parser);
38444       if (attrs)
38445         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38446     }
38447
38448   /* Parse a noexcept specification.  */
38449   if (keyword == RID_ATOMIC_NOEXCEPT)
38450     noex = boolean_true_node;
38451   else if (keyword == RID_ATOMIC_CANCEL)
38452     {
38453       /* cancel-and-throw is unimplemented.  */
38454       sorry ("atomic_cancel");
38455       noex = NULL_TREE;
38456     }
38457   else
38458     noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38459
38460   /* Keep track if we're in the lexical scope of an outer transaction.  */
38461   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38462
38463   stmt = begin_transaction_stmt (token->location, NULL, this_in);
38464
38465   parser->in_transaction = new_in;
38466   cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38467   parser->in_transaction = old_in;
38468
38469   finish_transaction_stmt (stmt, NULL, this_in, noex);
38470
38471   return stmt;
38472 }
38473
38474 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38475
38476    transaction-expression:
38477      __transaction_atomic txn-noexcept-spec[opt] ( expression )
38478      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38479 */
38480
38481 static tree
38482 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38483 {
38484   unsigned char old_in = parser->in_transaction;
38485   unsigned char this_in = 1;
38486   cp_token *token;
38487   tree expr, noex;
38488   bool noex_expr;
38489   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38490
38491   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38492       || keyword == RID_TRANSACTION_RELAXED);
38493
38494   if (!flag_tm)
38495     error_at (loc,
38496               keyword == RID_TRANSACTION_RELAXED
38497               ? G_("%<__transaction_relaxed%> without transactional memory "
38498                   "support enabled")
38499               : G_("%<__transaction_atomic%> without transactional memory "
38500                    "support enabled"));
38501
38502   token = cp_parser_require_keyword (parser, keyword,
38503       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38504           : RT_TRANSACTION_RELAXED));
38505   gcc_assert (token != NULL);
38506
38507   if (keyword == RID_TRANSACTION_RELAXED)
38508     this_in |= TM_STMT_ATTR_RELAXED;
38509
38510   /* Set this early.  This might mean that we allow transaction_cancel in
38511      an expression that we find out later actually has to be a constexpr.
38512      However, we expect that cxx_constant_value will be able to deal with
38513      this; also, if the noexcept has no constexpr, then what we parse next
38514      really is a transaction's body.  */
38515   parser->in_transaction = this_in;
38516
38517   /* Parse a noexcept specification.  */
38518   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38519                                                true);
38520
38521   if (!noex || !noex_expr
38522       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38523     {
38524       matching_parens parens;
38525       parens.require_open (parser);
38526
38527       expr = cp_parser_expression (parser);
38528       expr = finish_parenthesized_expr (expr);
38529
38530       parens.require_close (parser);
38531     }
38532   else
38533     {
38534       /* The only expression that is available got parsed for the noexcept
38535          already.  noexcept is true then.  */
38536       expr = noex;
38537       noex = boolean_true_node;
38538     }
38539
38540   expr = build_transaction_expr (token->location, expr, this_in, noex);
38541   parser->in_transaction = old_in;
38542
38543   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38544     return error_mark_node;
38545
38546   return (flag_tm ? expr : error_mark_node);
38547 }
38548
38549 /* Parse a function-transaction-block.
38550
38551    function-transaction-block:
38552      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38553          function-body
38554      __transaction_atomic txn-attribute[opt] function-try-block
38555      __transaction_relaxed ctor-initializer[opt] function-body
38556      __transaction_relaxed function-try-block
38557 */
38558
38559 static void
38560 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38561 {
38562   unsigned char old_in = parser->in_transaction;
38563   unsigned char new_in = 1;
38564   tree compound_stmt, stmt, attrs;
38565   cp_token *token;
38566
38567   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38568       || keyword == RID_TRANSACTION_RELAXED);
38569   token = cp_parser_require_keyword (parser, keyword,
38570       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38571           : RT_TRANSACTION_RELAXED));
38572   gcc_assert (token != NULL);
38573
38574   if (keyword == RID_TRANSACTION_RELAXED)
38575     new_in |= TM_STMT_ATTR_RELAXED;
38576   else
38577     {
38578       attrs = cp_parser_txn_attribute_opt (parser);
38579       if (attrs)
38580         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38581     }
38582
38583   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38584
38585   parser->in_transaction = new_in;
38586
38587   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38588     cp_parser_function_try_block (parser);
38589   else
38590     cp_parser_ctor_initializer_opt_and_function_body
38591       (parser, /*in_function_try_block=*/false);
38592
38593   parser->in_transaction = old_in;
38594
38595   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38596 }
38597
38598 /* Parse a __transaction_cancel statement.
38599
38600    cancel-statement:
38601      __transaction_cancel txn-attribute[opt] ;
38602      __transaction_cancel txn-attribute[opt] throw-expression ;
38603
38604    ??? Cancel and throw is not yet implemented.  */
38605
38606 static tree
38607 cp_parser_transaction_cancel (cp_parser *parser)
38608 {
38609   cp_token *token;
38610   bool is_outer = false;
38611   tree stmt, attrs;
38612
38613   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38614                                      RT_TRANSACTION_CANCEL);
38615   gcc_assert (token != NULL);
38616
38617   attrs = cp_parser_txn_attribute_opt (parser);
38618   if (attrs)
38619     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38620
38621   /* ??? Parse cancel-and-throw here.  */
38622
38623   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38624
38625   if (!flag_tm)
38626     {
38627       error_at (token->location, "%<__transaction_cancel%> without "
38628                 "transactional memory support enabled");
38629       return error_mark_node;
38630     }
38631   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38632     {
38633       error_at (token->location, "%<__transaction_cancel%> within a "
38634                 "%<__transaction_relaxed%>");
38635       return error_mark_node;
38636     }
38637   else if (is_outer)
38638     {
38639       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38640           && !is_tm_may_cancel_outer (current_function_decl))
38641         {
38642           error_at (token->location, "outer %<__transaction_cancel%> not "
38643                     "within outer %<__transaction_atomic%>");
38644           error_at (token->location,
38645                     "  or a %<transaction_may_cancel_outer%> function");
38646           return error_mark_node;
38647         }
38648     }
38649   else if (parser->in_transaction == 0)
38650     {
38651       error_at (token->location, "%<__transaction_cancel%> not within "
38652                 "%<__transaction_atomic%>");
38653       return error_mark_node;
38654     }
38655
38656   stmt = build_tm_abort_call (token->location, is_outer);
38657   add_stmt (stmt);
38658
38659   return stmt;
38660 }
38661 \f
38662 /* The parser.  */
38663
38664 static GTY (()) cp_parser *the_parser;
38665
38666 \f
38667 /* Special handling for the first token or line in the file.  The first
38668    thing in the file might be #pragma GCC pch_preprocess, which loads a
38669    PCH file, which is a GC collection point.  So we need to handle this
38670    first pragma without benefit of an existing lexer structure.
38671
38672    Always returns one token to the caller in *FIRST_TOKEN.  This is
38673    either the true first token of the file, or the first token after
38674    the initial pragma.  */
38675
38676 static void
38677 cp_parser_initial_pragma (cp_token *first_token)
38678 {
38679   tree name = NULL;
38680
38681   cp_lexer_get_preprocessor_token (NULL, first_token);
38682   if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38683     return;
38684
38685   cp_lexer_get_preprocessor_token (NULL, first_token);
38686   if (first_token->type == CPP_STRING)
38687     {
38688       name = first_token->u.value;
38689
38690       cp_lexer_get_preprocessor_token (NULL, first_token);
38691       if (first_token->type != CPP_PRAGMA_EOL)
38692         error_at (first_token->location,
38693                   "junk at end of %<#pragma GCC pch_preprocess%>");
38694     }
38695   else
38696     error_at (first_token->location, "expected string literal");
38697
38698   /* Skip to the end of the pragma.  */
38699   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38700     cp_lexer_get_preprocessor_token (NULL, first_token);
38701
38702   /* Now actually load the PCH file.  */
38703   if (name)
38704     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38705
38706   /* Read one more token to return to our caller.  We have to do this
38707      after reading the PCH file in, since its pointers have to be
38708      live.  */
38709   cp_lexer_get_preprocessor_token (NULL, first_token);
38710 }
38711
38712 /* Parse a pragma GCC ivdep.  */
38713
38714 static bool
38715 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38716 {
38717   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38718   return true;
38719 }
38720
38721 /* Parse a pragma GCC unroll.  */
38722
38723 static unsigned short
38724 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38725 {
38726   location_t location = cp_lexer_peek_token (parser->lexer)->location;
38727   tree expr = cp_parser_constant_expression (parser);
38728   unsigned short unroll;
38729   expr = maybe_constant_value (expr);
38730   HOST_WIDE_INT lunroll = 0;
38731   if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38732       || TREE_CODE (expr) != INTEGER_CST
38733       || (lunroll = tree_to_shwi (expr)) < 0
38734       || lunroll >= USHRT_MAX)
38735     {
38736       error_at (location, "%<#pragma GCC unroll%> requires an"
38737                 " assignment-expression that evaluates to a non-negative"
38738                 " integral constant less than %u", USHRT_MAX);
38739       unroll = 0;
38740     }
38741   else
38742     {
38743       unroll = (unsigned short)lunroll;
38744       if (unroll == 0)
38745         unroll = 1;
38746     }
38747   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38748   return unroll;
38749 }
38750
38751 /* Normal parsing of a pragma token.  Here we can (and must) use the
38752    regular lexer.  */
38753
38754 static bool
38755 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38756 {
38757   cp_token *pragma_tok;
38758   unsigned int id;
38759   tree stmt;
38760   bool ret;
38761
38762   pragma_tok = cp_lexer_consume_token (parser->lexer);
38763   gcc_assert (pragma_tok->type == CPP_PRAGMA);
38764   parser->lexer->in_pragma = true;
38765
38766   id = cp_parser_pragma_kind (pragma_tok);
38767   if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38768     cp_ensure_no_omp_declare_simd (parser);
38769   switch (id)
38770     {
38771     case PRAGMA_GCC_PCH_PREPROCESS:
38772       error_at (pragma_tok->location,
38773                 "%<#pragma GCC pch_preprocess%> must be first");
38774       break;
38775
38776     case PRAGMA_OMP_BARRIER:
38777       switch (context)
38778         {
38779         case pragma_compound:
38780           cp_parser_omp_barrier (parser, pragma_tok);
38781           return false;
38782         case pragma_stmt:
38783           error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38784                     "used in compound statements", "omp barrier");
38785           break;
38786         default:
38787           goto bad_stmt;
38788         }
38789       break;
38790
38791     case PRAGMA_OMP_FLUSH:
38792       switch (context)
38793         {
38794         case pragma_compound:
38795           cp_parser_omp_flush (parser, pragma_tok);
38796           return false;
38797         case pragma_stmt:
38798           error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38799                     "used in compound statements", "omp flush");
38800           break;
38801         default:
38802           goto bad_stmt;
38803         }
38804       break;
38805
38806     case PRAGMA_OMP_TASKWAIT:
38807       switch (context)
38808         {
38809         case pragma_compound:
38810           cp_parser_omp_taskwait (parser, pragma_tok);
38811           return false;
38812         case pragma_stmt:
38813           error_at (pragma_tok->location,
38814                     "%<#pragma %s%> may only be used in compound statements",
38815                     "omp taskwait");
38816           break;
38817         default:
38818           goto bad_stmt;
38819         }
38820       break;
38821
38822     case PRAGMA_OMP_TASKYIELD:
38823       switch (context)
38824         {
38825         case pragma_compound:
38826           cp_parser_omp_taskyield (parser, pragma_tok);
38827           return false;
38828         case pragma_stmt:
38829           error_at (pragma_tok->location,
38830                     "%<#pragma %s%> may only be used in compound statements",
38831                     "omp taskyield");
38832           break;
38833         default:
38834           goto bad_stmt;
38835         }
38836       break;
38837
38838     case PRAGMA_OMP_CANCEL:
38839       switch (context)
38840         {
38841         case pragma_compound:
38842           cp_parser_omp_cancel (parser, pragma_tok);
38843           return false;
38844         case pragma_stmt:
38845           error_at (pragma_tok->location,
38846                     "%<#pragma %s%> may only be used in compound statements",
38847                     "omp cancel");
38848           break;
38849         default:
38850           goto bad_stmt;
38851         }
38852       break;
38853
38854     case PRAGMA_OMP_CANCELLATION_POINT:
38855       cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38856       return false;
38857
38858     case PRAGMA_OMP_THREADPRIVATE:
38859       cp_parser_omp_threadprivate (parser, pragma_tok);
38860       return false;
38861
38862     case PRAGMA_OMP_DECLARE:
38863       return cp_parser_omp_declare (parser, pragma_tok, context);
38864
38865     case PRAGMA_OACC_DECLARE:
38866       cp_parser_oacc_declare (parser, pragma_tok);
38867       return false;
38868
38869     case PRAGMA_OACC_ENTER_DATA:
38870       if (context == pragma_stmt)
38871         {
38872           error_at (pragma_tok->location,
38873                     "%<#pragma %s%> may only be used in compound statements",
38874                     "acc enter data");
38875           break;
38876         }
38877       else if (context != pragma_compound)
38878         goto bad_stmt;
38879       cp_parser_omp_construct (parser, pragma_tok, if_p);
38880       return true;
38881
38882     case PRAGMA_OACC_EXIT_DATA:
38883       if (context == pragma_stmt)
38884         {
38885           error_at (pragma_tok->location,
38886                     "%<#pragma %s%> may only be used in compound statements",
38887                     "acc exit data");
38888           break;
38889         }
38890       else if (context != pragma_compound)
38891         goto bad_stmt;
38892       cp_parser_omp_construct (parser, pragma_tok, if_p);
38893       return true;
38894
38895     case PRAGMA_OACC_ROUTINE:
38896       if (context != pragma_external)
38897         {
38898           error_at (pragma_tok->location,
38899                     "%<#pragma acc routine%> must be at file scope");
38900           break;
38901         }
38902       cp_parser_oacc_routine (parser, pragma_tok, context);
38903       return false;
38904
38905     case PRAGMA_OACC_UPDATE:
38906       if (context == pragma_stmt)
38907         {
38908           error_at (pragma_tok->location,
38909                     "%<#pragma %s%> may only be used in compound statements",
38910                     "acc update");
38911           break;
38912         }
38913       else if (context != pragma_compound)
38914         goto bad_stmt;
38915       cp_parser_omp_construct (parser, pragma_tok, if_p);
38916       return true;
38917
38918     case PRAGMA_OACC_WAIT:
38919       if (context == pragma_stmt)
38920         {
38921           error_at (pragma_tok->location,
38922                     "%<#pragma %s%> may only be used in compound statements",
38923                     "acc wait");
38924           break;
38925         }
38926       else if (context != pragma_compound)
38927         goto bad_stmt;
38928       cp_parser_omp_construct (parser, pragma_tok, if_p);
38929       return true;
38930
38931     case PRAGMA_OACC_ATOMIC:
38932     case PRAGMA_OACC_CACHE:
38933     case PRAGMA_OACC_DATA:
38934     case PRAGMA_OACC_HOST_DATA:
38935     case PRAGMA_OACC_KERNELS:
38936     case PRAGMA_OACC_PARALLEL:
38937     case PRAGMA_OACC_LOOP:
38938     case PRAGMA_OMP_ATOMIC:
38939     case PRAGMA_OMP_CRITICAL:
38940     case PRAGMA_OMP_DISTRIBUTE:
38941     case PRAGMA_OMP_FOR:
38942     case PRAGMA_OMP_MASTER:
38943     case PRAGMA_OMP_PARALLEL:
38944     case PRAGMA_OMP_SECTIONS:
38945     case PRAGMA_OMP_SIMD:
38946     case PRAGMA_OMP_SINGLE:
38947     case PRAGMA_OMP_TASK:
38948     case PRAGMA_OMP_TASKGROUP:
38949     case PRAGMA_OMP_TASKLOOP:
38950     case PRAGMA_OMP_TEAMS:
38951       if (context != pragma_stmt && context != pragma_compound)
38952         goto bad_stmt;
38953       stmt = push_omp_privatization_clauses (false);
38954       cp_parser_omp_construct (parser, pragma_tok, if_p);
38955       pop_omp_privatization_clauses (stmt);
38956       return true;
38957
38958     case PRAGMA_OMP_ORDERED:
38959       if (context != pragma_stmt && context != pragma_compound)
38960         goto bad_stmt;
38961       stmt = push_omp_privatization_clauses (false);
38962       ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38963       pop_omp_privatization_clauses (stmt);
38964       return ret;
38965
38966     case PRAGMA_OMP_TARGET:
38967       if (context != pragma_stmt && context != pragma_compound)
38968         goto bad_stmt;
38969       stmt = push_omp_privatization_clauses (false);
38970       ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38971       pop_omp_privatization_clauses (stmt);
38972       return ret;
38973
38974     case PRAGMA_OMP_END_DECLARE_TARGET:
38975       cp_parser_omp_end_declare_target (parser, pragma_tok);
38976       return false;
38977
38978     case PRAGMA_OMP_SECTION:
38979       error_at (pragma_tok->location, 
38980                 "%<#pragma omp section%> may only be used in "
38981                 "%<#pragma omp sections%> construct");
38982       break;
38983
38984     case PRAGMA_IVDEP:
38985       {
38986         if (context == pragma_external)
38987           {
38988             error_at (pragma_tok->location,
38989                       "%<#pragma GCC ivdep%> must be inside a function");
38990             break;
38991           }
38992         const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
38993         unsigned short unroll;
38994         cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38995         if (tok->type == CPP_PRAGMA
38996             && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
38997           {
38998             tok = cp_lexer_consume_token (parser->lexer);
38999             unroll = cp_parser_pragma_unroll (parser, tok);
39000             tok = cp_lexer_peek_token (the_parser->lexer);
39001           }
39002         else
39003           unroll = 0;
39004         if (tok->type != CPP_KEYWORD
39005             || (tok->keyword != RID_FOR
39006                 && tok->keyword != RID_WHILE
39007                 && tok->keyword != RID_DO))
39008           {
39009             cp_parser_error (parser, "for, while or do statement expected");
39010             return false;
39011           }
39012         cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39013         return true;
39014       }
39015
39016     case PRAGMA_UNROLL:
39017       {
39018         if (context == pragma_external)
39019           {
39020             error_at (pragma_tok->location,
39021                       "%<#pragma GCC unroll%> must be inside a function");
39022             break;
39023           }
39024         const unsigned short unroll
39025           = cp_parser_pragma_unroll (parser, pragma_tok);
39026         bool ivdep;
39027         cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39028         if (tok->type == CPP_PRAGMA
39029             && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39030           {
39031             tok = cp_lexer_consume_token (parser->lexer);
39032             ivdep = cp_parser_pragma_ivdep (parser, tok);
39033             tok = cp_lexer_peek_token (the_parser->lexer);
39034           }
39035         else
39036           ivdep = false;
39037         if (tok->type != CPP_KEYWORD
39038             || (tok->keyword != RID_FOR
39039                 && tok->keyword != RID_WHILE
39040                 && tok->keyword != RID_DO))
39041           {
39042             cp_parser_error (parser, "for, while or do statement expected");
39043             return false;
39044           }
39045         cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39046         return true;
39047       }
39048
39049     default:
39050       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39051       c_invoke_pragma_handler (id);
39052       break;
39053
39054     bad_stmt:
39055       cp_parser_error (parser, "expected declaration specifiers");
39056       break;
39057     }
39058
39059   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39060   return false;
39061 }
39062
39063 /* The interface the pragma parsers have to the lexer.  */
39064
39065 enum cpp_ttype
39066 pragma_lex (tree *value, location_t *loc)
39067 {
39068   cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39069   enum cpp_ttype ret = tok->type;
39070
39071   *value = tok->u.value;
39072   if (loc)
39073     *loc = tok->location;
39074
39075   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39076     ret = CPP_EOF;
39077   else if (ret == CPP_STRING)
39078     *value = cp_parser_string_literal (the_parser, false, false);
39079   else
39080     {
39081       if (ret == CPP_KEYWORD)
39082         ret = CPP_NAME;
39083       cp_lexer_consume_token (the_parser->lexer);
39084     }
39085
39086   return ret;
39087 }
39088
39089 \f
39090 /* External interface.  */
39091
39092 /* Parse one entire translation unit.  */
39093
39094 void
39095 c_parse_file (void)
39096 {
39097   static bool already_called = false;
39098
39099   if (already_called)
39100     fatal_error (input_location,
39101                  "inter-module optimizations not implemented for C++");
39102   already_called = true;
39103
39104   the_parser = cp_parser_new ();
39105   push_deferring_access_checks (flag_access_control
39106                                 ? dk_no_deferred : dk_no_check);
39107   cp_parser_translation_unit (the_parser);
39108   the_parser = NULL;
39109 }
39110
39111 /* Create an identifier for a generic parameter type (a synthesized
39112    template parameter implied by `auto' or a concept identifier). */
39113
39114 static GTY(()) int generic_parm_count;
39115 static tree
39116 make_generic_type_name ()
39117 {
39118   char buf[32];
39119   sprintf (buf, "auto:%d", ++generic_parm_count);
39120   return get_identifier (buf);
39121 }
39122
39123 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39124    (creating a new template parameter list if necessary).  Returns the newly
39125    created template type parm.  */
39126
39127 static tree
39128 synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
39129 {
39130   gcc_assert (current_binding_level->kind == sk_function_parms);
39131
39132    /* Before committing to modifying any scope, if we're in an
39133       implicit template scope, and we're trying to synthesize a
39134       constrained parameter, try to find a previous parameter with
39135       the same name.  This is the same-type rule for abbreviated
39136       function templates.
39137
39138       NOTE: We can generate implicit parameters when tentatively
39139       parsing a nested name specifier, only to reject that parse
39140       later. However, matching the same template-id as part of a
39141       direct-declarator should generate an identical template
39142       parameter, so this rule will merge them. */
39143   if (parser->implicit_template_scope && constr)
39144     {
39145       tree t = parser->implicit_template_parms;
39146       while (t)
39147         {
39148           if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39149             {
39150               tree d = TREE_VALUE (t);
39151               if (TREE_CODE (d) == PARM_DECL)
39152                 /* Return the TEMPLATE_PARM_INDEX.  */
39153                 d = DECL_INITIAL (d);
39154               return d;
39155             }
39156           t = TREE_CHAIN (t);
39157         }
39158     }
39159
39160   /* We are either continuing a function template that already contains implicit
39161      template parameters, creating a new fully-implicit function template, or
39162      extending an existing explicit function template with implicit template
39163      parameters.  */
39164
39165   cp_binding_level *const entry_scope = current_binding_level;
39166
39167   bool become_template = false;
39168   cp_binding_level *parent_scope = 0;
39169
39170   if (parser->implicit_template_scope)
39171     {
39172       gcc_assert (parser->implicit_template_parms);
39173
39174       current_binding_level = parser->implicit_template_scope;
39175     }
39176   else
39177     {
39178       /* Roll back to the existing template parameter scope (in the case of
39179          extending an explicit function template) or introduce a new template
39180          parameter scope ahead of the function parameter scope (or class scope
39181          in the case of out-of-line member definitions).  The function scope is
39182          added back after template parameter synthesis below.  */
39183
39184       cp_binding_level *scope = entry_scope;
39185
39186       while (scope->kind == sk_function_parms)
39187         {
39188           parent_scope = scope;
39189           scope = scope->level_chain;
39190         }
39191       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39192         {
39193           /* If not defining a class, then any class scope is a scope level in
39194              an out-of-line member definition.  In this case simply wind back
39195              beyond the first such scope to inject the template parameter list.
39196              Otherwise wind back to the class being defined.  The latter can
39197              occur in class member friend declarations such as:
39198
39199                class A {
39200                  void foo (auto);
39201                };
39202                class B {
39203                  friend void A::foo (auto);
39204                };
39205
39206             The template parameter list synthesized for the friend declaration
39207             must be injected in the scope of 'B'.  This can also occur in
39208             erroneous cases such as:
39209
39210                struct A {
39211                  struct B {
39212                    void foo (auto);
39213                  };
39214                  void B::foo (auto) {}
39215                };
39216
39217             Here the attempted definition of 'B::foo' within 'A' is ill-formed
39218             but, nevertheless, the template parameter list synthesized for the
39219             declarator should be injected into the scope of 'A' as if the
39220             ill-formed template was specified explicitly.  */
39221
39222           while (scope->kind == sk_class && !scope->defining_class_p)
39223             {
39224               parent_scope = scope;
39225               scope = scope->level_chain;
39226             }
39227         }
39228
39229       current_binding_level = scope;
39230
39231       if (scope->kind != sk_template_parms
39232           || !function_being_declared_is_template_p (parser))
39233         {
39234           /* Introduce a new template parameter list for implicit template
39235              parameters.  */
39236
39237           become_template = true;
39238
39239           parser->implicit_template_scope
39240               = begin_scope (sk_template_parms, NULL);
39241
39242           ++processing_template_decl;
39243
39244           parser->fully_implicit_function_template_p = true;
39245           ++parser->num_template_parameter_lists;
39246         }
39247       else
39248         {
39249           /* Synthesize implicit template parameters at the end of the explicit
39250              template parameter list.  */
39251
39252           gcc_assert (current_template_parms);
39253
39254           parser->implicit_template_scope = scope;
39255
39256           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39257           parser->implicit_template_parms
39258             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39259         }
39260     }
39261
39262   /* Synthesize a new template parameter and track the current template
39263      parameter chain with implicit_template_parms.  */
39264
39265   tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39266   tree synth_id = make_generic_type_name ();
39267   tree synth_tmpl_parm;
39268   bool non_type = false;
39269
39270   if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39271     synth_tmpl_parm
39272       = finish_template_type_parm (class_type_node, synth_id);
39273   else if (TREE_CODE (proto) == TEMPLATE_DECL)
39274     synth_tmpl_parm
39275       = finish_constrained_template_template_parm (proto, synth_id);
39276   else
39277     {
39278       synth_tmpl_parm = copy_decl (proto);
39279       DECL_NAME (synth_tmpl_parm) = synth_id;
39280       non_type = true;
39281     }
39282
39283   // Attach the constraint to the parm before processing.
39284   tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39285   TREE_TYPE (node) = constr;
39286   tree new_parm
39287     = process_template_parm (parser->implicit_template_parms,
39288                              input_location,
39289                              node,
39290                              /*non_type=*/non_type,
39291                              /*param_pack=*/false);
39292
39293   // Chain the new parameter to the list of implicit parameters.
39294   if (parser->implicit_template_parms)
39295     parser->implicit_template_parms
39296       = TREE_CHAIN (parser->implicit_template_parms);
39297   else
39298     parser->implicit_template_parms = new_parm;
39299
39300   tree new_decl = get_local_decls ();
39301   if (non_type)
39302     /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL.  */
39303     new_decl = DECL_INITIAL (new_decl);
39304
39305   /* If creating a fully implicit function template, start the new implicit
39306      template parameter list with this synthesized type, otherwise grow the
39307      current template parameter list.  */
39308
39309   if (become_template)
39310     {
39311       parent_scope->level_chain = current_binding_level;
39312
39313       tree new_parms = make_tree_vec (1);
39314       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39315       current_template_parms = tree_cons (size_int (processing_template_decl),
39316                                           new_parms, current_template_parms);
39317     }
39318   else
39319     {
39320       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39321       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39322       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39323       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39324     }
39325
39326   // If the new parameter was constrained, we need to add that to the
39327   // constraints in the template parameter list.
39328   if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39329     {
39330       tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39331       reqs = conjoin_constraints (reqs, req);
39332       TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39333     }
39334
39335   current_binding_level = entry_scope;
39336
39337   return new_decl;
39338 }
39339
39340 /* Finish the declaration of a fully implicit function template.  Such a
39341    template has no explicit template parameter list so has not been through the
39342    normal template head and tail processing.  synthesize_implicit_template_parm
39343    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
39344    provided if the declaration is a class member such that its template
39345    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
39346    form is returned.  Otherwise NULL_TREE is returned. */
39347
39348 static tree
39349 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39350 {
39351   gcc_assert (parser->fully_implicit_function_template_p);
39352
39353   if (member_decl_opt && member_decl_opt != error_mark_node
39354       && DECL_VIRTUAL_P (member_decl_opt))
39355     {
39356       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39357                 "implicit templates may not be %<virtual%>");
39358       DECL_VIRTUAL_P (member_decl_opt) = false;
39359     }
39360
39361   if (member_decl_opt)
39362     member_decl_opt = finish_member_template_decl (member_decl_opt);
39363   end_template_decl ();
39364
39365   parser->fully_implicit_function_template_p = false;
39366   parser->implicit_template_parms = 0;
39367   parser->implicit_template_scope = 0;
39368   --parser->num_template_parameter_lists;
39369
39370   return member_decl_opt;
39371 }
39372
39373 /* Like finish_fully_implicit_template, but to be used in error
39374    recovery, rearranging scopes so that we restore the state we had
39375    before synthesize_implicit_template_parm inserted the implement
39376    template parms scope.  */
39377
39378 static void
39379 abort_fully_implicit_template (cp_parser *parser)
39380 {
39381   cp_binding_level *return_to_scope = current_binding_level;
39382
39383   if (parser->implicit_template_scope
39384       && return_to_scope != parser->implicit_template_scope)
39385     {
39386       cp_binding_level *child = return_to_scope;
39387       for (cp_binding_level *scope = child->level_chain;
39388            scope != parser->implicit_template_scope;
39389            scope = child->level_chain)
39390         child = scope;
39391       child->level_chain = parser->implicit_template_scope->level_chain;
39392       parser->implicit_template_scope->level_chain = return_to_scope;
39393       current_binding_level = parser->implicit_template_scope;
39394     }
39395   else
39396     return_to_scope = return_to_scope->level_chain;
39397
39398   finish_fully_implicit_template (parser, NULL);
39399
39400   gcc_assert (current_binding_level == return_to_scope);
39401 }
39402
39403 /* Helper function for diagnostics that have complained about things
39404    being used with 'extern "C"' linkage.
39405
39406    Attempt to issue a note showing where the 'extern "C"' linkage began.  */
39407
39408 void
39409 maybe_show_extern_c_location (void)
39410 {
39411   if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39412     inform (the_parser->innermost_linkage_specification_location,
39413             "%<extern \"C\"%> linkage started here");
39414 }
39415
39416 #include "gt-cp-parser.h"