twe(4): Add two missing error checks.
[dragonfly.git] / contrib / gcc-5.0 / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2    Copyright (C) 2000-2015 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 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "print-tree.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "trans-mem.h"
41 #include "cp-tree.h"
42 #include "intl.h"
43 #include "c-family/c-pragma.h"
44 #include "decl.h"
45 #include "flags.h"
46 #include "diagnostic-core.h"
47 #include "target.h"
48 #include "hash-map.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "hard-reg-set.h"
52 #include "input.h"
53 #include "function.h"
54 #include "ipa-ref.h"
55 #include "cgraph.h"
56 #include "c-family/c-common.h"
57 #include "c-family/c-objc.h"
58 #include "plugin.h"
59 #include "tree-pretty-print.h"
60 #include "parser.h"
61 #include "type-utils.h"
62 #include "omp-low.h"
63 #include "gomp-constants.h"
64
65 \f
66 /* The lexer.  */
67
68 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
69    and c-lex.c) and the C++ parser.  */
70
71 static cp_token eof_token =
72 {
73   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
74 };
75
76 /* The various kinds of non integral constant we encounter. */
77 typedef enum non_integral_constant {
78   NIC_NONE,
79   /* floating-point literal */
80   NIC_FLOAT,
81   /* %<this%> */
82   NIC_THIS,
83   /* %<__FUNCTION__%> */
84   NIC_FUNC_NAME,
85   /* %<__PRETTY_FUNCTION__%> */
86   NIC_PRETTY_FUNC,
87   /* %<__func__%> */
88   NIC_C99_FUNC,
89   /* "%<va_arg%> */
90   NIC_VA_ARG,
91   /* a cast */
92   NIC_CAST,
93   /* %<typeid%> operator */
94   NIC_TYPEID,
95   /* non-constant compound literals */
96   NIC_NCC,
97   /* a function call */
98   NIC_FUNC_CALL,
99   /* an increment */
100   NIC_INC,
101   /* an decrement */
102   NIC_DEC,
103   /* an array reference */
104   NIC_ARRAY_REF,
105   /* %<->%> */
106   NIC_ARROW,
107   /* %<.%> */
108   NIC_POINT,
109   /* the address of a label */
110   NIC_ADDR_LABEL,
111   /* %<*%> */
112   NIC_STAR,
113   /* %<&%> */
114   NIC_ADDR,
115   /* %<++%> */
116   NIC_PREINCREMENT,
117   /* %<--%> */
118   NIC_PREDECREMENT,
119   /* %<new%> */
120   NIC_NEW,
121   /* %<delete%> */
122   NIC_DEL,
123   /* calls to overloaded operators */
124   NIC_OVERLOADED,
125   /* an assignment */
126   NIC_ASSIGNMENT,
127   /* a comma operator */
128   NIC_COMMA,
129   /* a call to a constructor */
130   NIC_CONSTRUCTOR,
131   /* a transaction expression */
132   NIC_TRANSACTION
133 } non_integral_constant;
134
135 /* The various kinds of errors about name-lookup failing. */
136 typedef enum name_lookup_error {
137   /* NULL */
138   NLE_NULL,
139   /* is not a type */
140   NLE_TYPE,
141   /* is not a class or namespace */
142   NLE_CXX98,
143   /* is not a class, namespace, or enumeration */
144   NLE_NOT_CXX98
145 } name_lookup_error;
146
147 /* The various kinds of required token */
148 typedef enum required_token {
149   RT_NONE,
150   RT_SEMICOLON,  /* ';' */
151   RT_OPEN_PAREN, /* '(' */
152   RT_CLOSE_BRACE, /* '}' */
153   RT_OPEN_BRACE,  /* '{' */
154   RT_CLOSE_SQUARE, /* ']' */
155   RT_OPEN_SQUARE,  /* '[' */
156   RT_COMMA, /* ',' */
157   RT_SCOPE, /* '::' */
158   RT_LESS, /* '<' */
159   RT_GREATER, /* '>' */
160   RT_EQ, /* '=' */
161   RT_ELLIPSIS, /* '...' */
162   RT_MULT, /* '*' */
163   RT_COMPL, /* '~' */
164   RT_COLON, /* ':' */
165   RT_COLON_SCOPE, /* ':' or '::' */
166   RT_CLOSE_PAREN, /* ')' */
167   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
168   RT_PRAGMA_EOL, /* end of line */
169   RT_NAME, /* identifier */
170
171   /* The type is CPP_KEYWORD */
172   RT_NEW, /* new */
173   RT_DELETE, /* delete */
174   RT_RETURN, /* return */
175   RT_WHILE, /* while */
176   RT_EXTERN, /* extern */
177   RT_STATIC_ASSERT, /* static_assert */
178   RT_DECLTYPE, /* decltype */
179   RT_OPERATOR, /* operator */
180   RT_CLASS, /* class */
181   RT_TEMPLATE, /* template */
182   RT_NAMESPACE, /* namespace */
183   RT_USING, /* using */
184   RT_ASM, /* asm */
185   RT_TRY, /* try */
186   RT_CATCH, /* catch */
187   RT_THROW, /* throw */
188   RT_LABEL, /* __label__ */
189   RT_AT_TRY, /* @try */
190   RT_AT_SYNCHRONIZED, /* @synchronized */
191   RT_AT_THROW, /* @throw */
192
193   RT_SELECT,  /* selection-statement */
194   RT_INTERATION, /* iteration-statement */
195   RT_JUMP, /* jump-statement */
196   RT_CLASS_KEY, /* class-key */
197   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
198   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
199   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
200   RT_TRANSACTION_CANCEL /* __transaction_cancel */
201 } required_token;
202
203 /* Prototypes.  */
204
205 static cp_lexer *cp_lexer_new_main
206   (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208   (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210   (cp_lexer *);
211 static int cp_lexer_saving_tokens
212   (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214   (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216   (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218   (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220   (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222   (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224   (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226   (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228   (cp_lexer *);
229 static void cp_lexer_purge_token
230   (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232   (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234   (cp_lexer *);
235 static void cp_lexer_commit_tokens
236   (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238   (cp_lexer *);
239 static void cp_lexer_print_token
240   (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242   (cp_lexer *);
243 static void cp_lexer_start_debugging
244   (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246   (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249   (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252   (cp_token *);
253
254 static tree cp_literal_operator_id
255   (const char *);
256
257 static void cp_parser_cilk_simd
258   (cp_parser *, cp_token *);
259 static tree cp_parser_cilk_for
260   (cp_parser *, tree);
261 static bool cp_parser_omp_declare_reduction_exprs
262   (tree, cp_parser *);
263 static tree cp_parser_cilk_simd_vectorlength 
264   (cp_parser *, tree, bool);
265
266 /* Manifest constants.  */
267 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
268 #define CP_SAVED_TOKEN_STACK 5
269
270 /* Variables.  */
271
272 /* The stream to which debugging output should be written.  */
273 static FILE *cp_lexer_debug_stream;
274
275 /* Nonzero if we are parsing an unevaluated operand: an operand to
276    sizeof, typeof, or alignof.  */
277 int cp_unevaluated_operand;
278
279 /* Dump up to NUM tokens in BUFFER to FILE starting with token
280    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
281    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
282    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
283    highlighted by surrounding it in [[ ]].  */
284
285 static void
286 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
287                       cp_token *start_token, unsigned num,
288                       cp_token *curr_token)
289 {
290   unsigned i, nprinted;
291   cp_token *token;
292   bool do_print;
293
294   fprintf (file, "%u tokens\n", vec_safe_length (buffer));
295
296   if (buffer == NULL)
297     return;
298
299   if (num == 0)
300     num = buffer->length ();
301
302   if (start_token == NULL)
303     start_token = buffer->address ();
304
305   if (start_token > buffer->address ())
306     {
307       cp_lexer_print_token (file, &(*buffer)[0]);
308       fprintf (file, " ... ");
309     }
310
311   do_print = false;
312   nprinted = 0;
313   for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
314     {
315       if (token == start_token)
316         do_print = true;
317
318       if (!do_print)
319         continue;
320
321       nprinted++;
322       if (token == curr_token)
323         fprintf (file, "[[");
324
325       cp_lexer_print_token (file, token);
326
327       if (token == curr_token)
328         fprintf (file, "]]");
329
330       switch (token->type)
331         {
332           case CPP_SEMICOLON:
333           case CPP_OPEN_BRACE:
334           case CPP_CLOSE_BRACE:
335           case CPP_EOF:
336             fputc ('\n', file);
337             break;
338
339           default:
340             fputc (' ', file);
341         }
342     }
343
344   if (i == num && i < buffer->length ())
345     {
346       fprintf (file, " ... ");
347       cp_lexer_print_token (file, &buffer->last ());
348     }
349
350   fprintf (file, "\n");
351 }
352
353
354 /* Dump all tokens in BUFFER to stderr.  */
355
356 void
357 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
358 {
359   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
360 }
361
362 DEBUG_FUNCTION void
363 debug (vec<cp_token, va_gc> &ref)
364 {
365   cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
366 }
367
368 DEBUG_FUNCTION void
369 debug (vec<cp_token, va_gc> *ptr)
370 {
371   if (ptr)
372     debug (*ptr);
373   else
374     fprintf (stderr, "<nil>\n");
375 }
376
377
378 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
379    description for T.  */
380
381 static void
382 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
383 {
384   if (t)
385     {
386       fprintf (file, "%s: ", desc);
387       print_node_brief (file, "", t, 0);
388     }
389 }
390
391
392 /* Dump parser context C to FILE.  */
393
394 static void
395 cp_debug_print_context (FILE *file, cp_parser_context *c)
396 {
397   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
398   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
399   print_node_brief (file, "", c->object_type, 0);
400   fprintf (file, "}\n");
401 }
402
403
404 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
405
406 static void
407 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
408 {
409   unsigned i;
410   cp_parser_context *c;
411
412   fprintf (file, "Parsing context stack:\n");
413   for (i = 0, c = first; c; c = c->next, i++)
414     {
415       fprintf (file, "\t#%u: ", i);
416       cp_debug_print_context (file, c);
417     }
418 }
419
420
421 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
422
423 static void
424 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
425 {
426   if (flag)
427     fprintf (file, "%s: true\n", desc);
428 }
429
430
431 /* Print an unparsed function entry UF to FILE.  */
432
433 static void
434 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
435 {
436   unsigned i;
437   cp_default_arg_entry *default_arg_fn;
438   tree fn;
439
440   fprintf (file, "\tFunctions with default args:\n");
441   for (i = 0;
442        vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
443        i++)
444     {
445       fprintf (file, "\t\tClass type: ");
446       print_node_brief (file, "", default_arg_fn->class_type, 0);
447       fprintf (file, "\t\tDeclaration: ");
448       print_node_brief (file, "", default_arg_fn->decl, 0);
449       fprintf (file, "\n");
450     }
451
452   fprintf (file, "\n\tFunctions with definitions that require "
453            "post-processing\n\t\t");
454   for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
455     {
456       print_node_brief (file, "", fn, 0);
457       fprintf (file, " ");
458     }
459   fprintf (file, "\n");
460
461   fprintf (file, "\n\tNon-static data members with initializers that require "
462            "post-processing\n\t\t");
463   for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
464     {
465       print_node_brief (file, "", fn, 0);
466       fprintf (file, " ");
467     }
468   fprintf (file, "\n");
469 }
470
471
472 /* Print the stack of unparsed member functions S to FILE.  */
473
474 static void
475 cp_debug_print_unparsed_queues (FILE *file,
476                                 vec<cp_unparsed_functions_entry, va_gc> *s)
477 {
478   unsigned i;
479   cp_unparsed_functions_entry *uf;
480
481   fprintf (file, "Unparsed functions\n");
482   for (i = 0; vec_safe_iterate (s, i, &uf); i++)
483     {
484       fprintf (file, "#%u:\n", i);
485       cp_debug_print_unparsed_function (file, uf);
486     }
487 }
488
489
490 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
491    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
492
493 static void
494 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
495 {
496   cp_token *next_token, *first_token, *start_token;
497
498   if (file == NULL)
499     file = stderr;
500
501   next_token = parser->lexer->next_token;
502   first_token = parser->lexer->buffer->address ();
503   start_token = (next_token > first_token + window_size / 2)
504                 ? next_token - window_size / 2
505                 : first_token;
506   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
507                         next_token);
508 }
509
510
511 /* Dump debugging information for the given PARSER.  If FILE is NULL,
512    the output is printed on stderr.  */
513
514 void
515 cp_debug_parser (FILE *file, cp_parser *parser)
516 {
517   const size_t window_size = 20;
518   cp_token *token;
519   expanded_location eloc;
520
521   if (file == NULL)
522     file = stderr;
523
524   fprintf (file, "Parser state\n\n");
525   fprintf (file, "Number of tokens: %u\n",
526            vec_safe_length (parser->lexer->buffer));
527   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
528   cp_debug_print_tree_if_set (file, "Object scope",
529                                      parser->object_scope);
530   cp_debug_print_tree_if_set (file, "Qualifying scope",
531                                      parser->qualifying_scope);
532   cp_debug_print_context_stack (file, parser->context);
533   cp_debug_print_flag (file, "Allow GNU extensions",
534                               parser->allow_gnu_extensions_p);
535   cp_debug_print_flag (file, "'>' token is greater-than",
536                               parser->greater_than_is_operator_p);
537   cp_debug_print_flag (file, "Default args allowed in current "
538                               "parameter list", parser->default_arg_ok_p);
539   cp_debug_print_flag (file, "Parsing integral constant-expression",
540                               parser->integral_constant_expression_p);
541   cp_debug_print_flag (file, "Allow non-constant expression in current "
542                               "constant-expression",
543                               parser->allow_non_integral_constant_expression_p);
544   cp_debug_print_flag (file, "Seen non-constant expression",
545                               parser->non_integral_constant_expression_p);
546   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
547                               "current context",
548                               parser->local_variables_forbidden_p);
549   cp_debug_print_flag (file, "In unbraced linkage specification",
550                               parser->in_unbraced_linkage_specification_p);
551   cp_debug_print_flag (file, "Parsing a declarator",
552                               parser->in_declarator_p);
553   cp_debug_print_flag (file, "In template argument list",
554                               parser->in_template_argument_list_p);
555   cp_debug_print_flag (file, "Parsing an iteration statement",
556                               parser->in_statement & IN_ITERATION_STMT);
557   cp_debug_print_flag (file, "Parsing a switch statement",
558                               parser->in_statement & IN_SWITCH_STMT);
559   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
560                               parser->in_statement & IN_OMP_BLOCK);
561   cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
562                               parser->in_statement & IN_CILK_SIMD_FOR);
563   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
564                               parser->in_statement & IN_OMP_FOR);
565   cp_debug_print_flag (file, "Parsing an if statement",
566                               parser->in_statement & IN_IF_STMT);
567   cp_debug_print_flag (file, "Parsing a type-id in an expression "
568                               "context", parser->in_type_id_in_expr_p);
569   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
570                               parser->implicit_extern_c);
571   cp_debug_print_flag (file, "String expressions should be translated "
572                               "to execution character set",
573                               parser->translate_strings_p);
574   cp_debug_print_flag (file, "Parsing function body outside of a "
575                               "local class", parser->in_function_body);
576   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
577                               parser->colon_corrects_to_scope_p);
578   cp_debug_print_flag (file, "Colon doesn't start a class definition",
579                               parser->colon_doesnt_start_class_def_p);
580   if (parser->type_definition_forbidden_message)
581     fprintf (file, "Error message for forbidden type definitions: %s\n",
582              parser->type_definition_forbidden_message);
583   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584   fprintf (file, "Number of class definitions in progress: %u\n",
585            parser->num_classes_being_defined);
586   fprintf (file, "Number of template parameter lists for the current "
587            "declaration: %u\n", parser->num_template_parameter_lists);
588   cp_debug_parser_tokens (file, parser, window_size);
589   token = parser->lexer->next_token;
590   fprintf (file, "Next token to parse:\n");
591   fprintf (file, "\tToken:  ");
592   cp_lexer_print_token (file, token);
593   eloc = expand_location (token->location);
594   fprintf (file, "\n\tFile:   %s\n", eloc.file);
595   fprintf (file, "\tLine:   %d\n", eloc.line);
596   fprintf (file, "\tColumn: %d\n", eloc.column);
597 }
598
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
601 {
602   cp_debug_parser (stderr, &ref);
603 }
604
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
607 {
608   if (ptr)
609     debug (*ptr);
610   else
611     fprintf (stderr, "<nil>\n");
612 }
613
614 /* Allocate memory for a new lexer object and return it.  */
615
616 static cp_lexer *
617 cp_lexer_alloc (void)
618 {
619   cp_lexer *lexer;
620
621   c_common_no_more_pch ();
622
623   /* Allocate the memory.  */
624   lexer = ggc_cleared_alloc<cp_lexer> ();
625
626   /* Initially we are not debugging.  */
627   lexer->debugging_p = false;
628
629   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
630
631   /* Create the buffer.  */
632   vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
633
634   return lexer;
635 }
636
637
638 /* Create a new main C++ lexer, the lexer that gets tokens from the
639    preprocessor.  */
640
641 static cp_lexer *
642 cp_lexer_new_main (void)
643 {
644   cp_lexer *lexer;
645   cp_token token;
646
647   /* It's possible that parsing the first pragma will load a PCH file,
648      which is a GC collection point.  So we have to do that before
649      allocating any memory.  */
650   cp_parser_initial_pragma (&token);
651
652   lexer = cp_lexer_alloc ();
653
654   /* Put the first token in the buffer.  */
655   lexer->buffer->quick_push (token);
656
657   /* Get the remaining tokens from the preprocessor.  */
658   while (token.type != CPP_EOF)
659     {
660       cp_lexer_get_preprocessor_token (lexer, &token);
661       vec_safe_push (lexer->buffer, token);
662     }
663
664   lexer->last_token = lexer->buffer->address ()
665                       + lexer->buffer->length ()
666                       - 1;
667   lexer->next_token = lexer->buffer->length ()
668                       ? lexer->buffer->address ()
669                       : &eof_token;
670
671   /* Subsequent preprocessor diagnostics should use compiler
672      diagnostic functions to get the compiler source location.  */
673   done_lexing = true;
674
675   gcc_assert (!lexer->next_token->purged_p);
676   return lexer;
677 }
678
679 /* Create a new lexer whose token stream is primed with the tokens in
680    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
681
682 static cp_lexer *
683 cp_lexer_new_from_tokens (cp_token_cache *cache)
684 {
685   cp_token *first = cache->first;
686   cp_token *last = cache->last;
687   cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
688
689   /* We do not own the buffer.  */
690   lexer->buffer = NULL;
691   lexer->next_token = first == last ? &eof_token : first;
692   lexer->last_token = last;
693
694   lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
695
696   /* Initially we are not debugging.  */
697   lexer->debugging_p = false;
698
699   gcc_assert (!lexer->next_token->purged_p);
700   return lexer;
701 }
702
703 /* Frees all resources associated with LEXER.  */
704
705 static void
706 cp_lexer_destroy (cp_lexer *lexer)
707 {
708   vec_free (lexer->buffer);
709   lexer->saved_tokens.release ();
710   ggc_free (lexer);
711 }
712
713 /* Returns nonzero if debugging information should be output.  */
714
715 static inline bool
716 cp_lexer_debugging_p (cp_lexer *lexer)
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   return cp_lexer_token_at (lexer, tp);
757 }
758
759 /* nonzero if we are presently saving tokens.  */
760
761 static inline int
762 cp_lexer_saving_tokens (const cp_lexer* lexer)
763 {
764   return lexer->saved_tokens.length () != 0;
765 }
766
767 /* Store the next token from the preprocessor in *TOKEN.  Return true
768    if we reach EOF.  If LEXER is NULL, assume we are handling an
769    initial #pragma pch_preprocess, and thus want the lexer to return
770    processed strings.  */
771
772 static void
773 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
774 {
775   static int is_extern_c = 0;
776
777    /* Get a new token from the preprocessor.  */
778   token->type
779     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
780                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
781   token->keyword = RID_MAX;
782   token->pragma_kind = PRAGMA_NONE;
783   token->purged_p = false;
784   token->error_reported = false;
785
786   /* On some systems, some header files are surrounded by an
787      implicit extern "C" block.  Set a flag in the token if it
788      comes from such a header.  */
789   is_extern_c += pending_lang_change;
790   pending_lang_change = 0;
791   token->implicit_extern_c = is_extern_c > 0;
792
793   /* Check to see if this token is a keyword.  */
794   if (token->type == CPP_NAME)
795     {
796       if (C_IS_RESERVED_WORD (token->u.value))
797         {
798           /* Mark this token as a keyword.  */
799           token->type = CPP_KEYWORD;
800           /* Record which keyword.  */
801           token->keyword = C_RID_CODE (token->u.value);
802         }
803       else
804         {
805           if (warn_cxx0x_compat
806               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
807               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
808             {
809               /* Warn about the C++0x keyword (but still treat it as
810                  an identifier).  */
811               warning (OPT_Wc__0x_compat, 
812                        "identifier %qE is a keyword in C++11",
813                        token->u.value);
814
815               /* Clear out the C_RID_CODE so we don't warn about this
816                  particular identifier-turned-keyword again.  */
817               C_SET_RID_CODE (token->u.value, RID_MAX);
818             }
819
820           token->keyword = RID_MAX;
821         }
822     }
823   else if (token->type == CPP_AT_NAME)
824     {
825       /* This only happens in Objective-C++; it must be a keyword.  */
826       token->type = CPP_KEYWORD;
827       switch (C_RID_CODE (token->u.value))
828         {
829           /* Replace 'class' with '@class', 'private' with '@private',
830              etc.  This prevents confusion with the C++ keyword
831              'class', and makes the tokens consistent with other
832              Objective-C 'AT' keywords.  For example '@class' is
833              reported as RID_AT_CLASS which is consistent with
834              '@synchronized', which is reported as
835              RID_AT_SYNCHRONIZED.
836           */
837         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
838         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
839         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
840         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
841         case RID_THROW:     token->keyword = RID_AT_THROW; break;
842         case RID_TRY:       token->keyword = RID_AT_TRY; break;
843         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
844         default:            token->keyword = C_RID_CODE (token->u.value);
845         }
846     }
847   else if (token->type == CPP_PRAGMA)
848     {
849       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
850       token->pragma_kind = ((enum pragma_kind)
851                             TREE_INT_CST_LOW (token->u.value));
852       token->u.value = NULL_TREE;
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 the next token is a keyword for a decl-specifier.  */
934
935 static bool
936 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
937 {
938   cp_token *token;
939
940   token = cp_lexer_peek_token (lexer);
941   switch (token->keyword) 
942     {
943       /* auto specifier: storage-class-specifier in C++,
944          simple-type-specifier in C++0x.  */
945     case RID_AUTO:
946       /* Storage classes.  */
947     case RID_REGISTER:
948     case RID_STATIC:
949     case RID_EXTERN:
950     case RID_MUTABLE:
951     case RID_THREAD:
952       /* Elaborated type specifiers.  */
953     case RID_ENUM:
954     case RID_CLASS:
955     case RID_STRUCT:
956     case RID_UNION:
957     case RID_TYPENAME:
958       /* Simple type specifiers.  */
959     case RID_CHAR:
960     case RID_CHAR16:
961     case RID_CHAR32:
962     case RID_WCHAR:
963     case RID_BOOL:
964     case RID_SHORT:
965     case RID_INT:
966     case RID_LONG:
967     case RID_SIGNED:
968     case RID_UNSIGNED:
969     case RID_FLOAT:
970     case RID_DOUBLE:
971     case RID_VOID:
972       /* GNU extensions.  */ 
973     case RID_ATTRIBUTE:
974     case RID_TYPEOF:
975       /* C++0x extensions.  */
976     case RID_DECLTYPE:
977     case RID_UNDERLYING_TYPE:
978       return true;
979
980     default:
981       if (token->keyword >= RID_FIRST_INT_N
982           && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
983           && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
984         return true;
985       return false;
986     }
987 }
988
989 /* Returns TRUE iff the token T begins a decltype type.  */
990
991 static bool
992 token_is_decltype (cp_token *t)
993 {
994   return (t->keyword == RID_DECLTYPE
995           || t->type == CPP_DECLTYPE);
996 }
997
998 /* Returns TRUE iff the next token begins a decltype type.  */
999
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1002 {
1003   cp_token *t = cp_lexer_peek_token (lexer);
1004   return token_is_decltype (t);
1005 }
1006
1007 /* Return a pointer to the Nth token in the token stream.  If N is 1,
1008    then this is precisely equivalent to cp_lexer_peek_token (except
1009    that it is not inline).  One would like to disallow that case, but
1010    there is one case (cp_parser_nth_token_starts_template_id) where
1011    the caller passes a variable for N and it might be 1.  */
1012
1013 static cp_token *
1014 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1015 {
1016   cp_token *token;
1017
1018   /* N is 1-based, not zero-based.  */
1019   gcc_assert (n > 0);
1020
1021   if (cp_lexer_debugging_p (lexer))
1022     fprintf (cp_lexer_debug_stream,
1023              "cp_lexer: peeking ahead %ld at token: ", (long)n);
1024
1025   --n;
1026   token = lexer->next_token;
1027   gcc_assert (!n || token != &eof_token);
1028   while (n != 0)
1029     {
1030       ++token;
1031       if (token == lexer->last_token)
1032         {
1033           token = &eof_token;
1034           break;
1035         }
1036
1037       if (!token->purged_p)
1038         --n;
1039     }
1040
1041   if (cp_lexer_debugging_p (lexer))
1042     {
1043       cp_lexer_print_token (cp_lexer_debug_stream, token);
1044       putc ('\n', cp_lexer_debug_stream);
1045     }
1046
1047   return token;
1048 }
1049
1050 /* Return the next token, and advance the lexer's next_token pointer
1051    to point to the next non-purged token.  */
1052
1053 static cp_token *
1054 cp_lexer_consume_token (cp_lexer* lexer)
1055 {
1056   cp_token *token = lexer->next_token;
1057
1058   gcc_assert (token != &eof_token);
1059   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1060
1061   do
1062     {
1063       lexer->next_token++;
1064       if (lexer->next_token == lexer->last_token)
1065         {
1066           lexer->next_token = &eof_token;
1067           break;
1068         }
1069
1070     }
1071   while (lexer->next_token->purged_p);
1072
1073   cp_lexer_set_source_position_from_token (token);
1074
1075   /* Provide debugging output.  */
1076   if (cp_lexer_debugging_p (lexer))
1077     {
1078       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1079       cp_lexer_print_token (cp_lexer_debug_stream, token);
1080       putc ('\n', cp_lexer_debug_stream);
1081     }
1082
1083   return token;
1084 }
1085
1086 /* Permanently remove the next token from the token stream, and
1087    advance the next_token pointer to refer to the next non-purged
1088    token.  */
1089
1090 static void
1091 cp_lexer_purge_token (cp_lexer *lexer)
1092 {
1093   cp_token *tok = lexer->next_token;
1094
1095   gcc_assert (tok != &eof_token);
1096   tok->purged_p = true;
1097   tok->location = UNKNOWN_LOCATION;
1098   tok->u.value = NULL_TREE;
1099   tok->keyword = RID_MAX;
1100
1101   do
1102     {
1103       tok++;
1104       if (tok == lexer->last_token)
1105         {
1106           tok = &eof_token;
1107           break;
1108         }
1109     }
1110   while (tok->purged_p);
1111   lexer->next_token = tok;
1112 }
1113
1114 /* Permanently remove all tokens after TOK, up to, but not
1115    including, the token that will be returned next by
1116    cp_lexer_peek_token.  */
1117
1118 static void
1119 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1120 {
1121   cp_token *peek = lexer->next_token;
1122
1123   if (peek == &eof_token)
1124     peek = lexer->last_token;
1125
1126   gcc_assert (tok < peek);
1127
1128   for ( tok += 1; tok != peek; tok += 1)
1129     {
1130       tok->purged_p = true;
1131       tok->location = UNKNOWN_LOCATION;
1132       tok->u.value = NULL_TREE;
1133       tok->keyword = RID_MAX;
1134     }
1135 }
1136
1137 /* Begin saving tokens.  All tokens consumed after this point will be
1138    preserved.  */
1139
1140 static void
1141 cp_lexer_save_tokens (cp_lexer* lexer)
1142 {
1143   /* Provide debugging output.  */
1144   if (cp_lexer_debugging_p (lexer))
1145     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1146
1147   lexer->saved_tokens.safe_push (lexer->next_token);
1148 }
1149
1150 /* Commit to the portion of the token stream most recently saved.  */
1151
1152 static void
1153 cp_lexer_commit_tokens (cp_lexer* lexer)
1154 {
1155   /* Provide debugging output.  */
1156   if (cp_lexer_debugging_p (lexer))
1157     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1158
1159   lexer->saved_tokens.pop ();
1160 }
1161
1162 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1163    to the token stream.  Stop saving tokens.  */
1164
1165 static void
1166 cp_lexer_rollback_tokens (cp_lexer* lexer)
1167 {
1168   /* Provide debugging output.  */
1169   if (cp_lexer_debugging_p (lexer))
1170     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1171
1172   lexer->next_token = lexer->saved_tokens.pop ();
1173 }
1174
1175 /* RAII wrapper around the above functions, with sanity checking.  Creating
1176    a variable saves tokens, which are committed when the variable is
1177    destroyed unless they are explicitly rolled back by calling the rollback
1178    member function.  */
1179
1180 struct saved_token_sentinel
1181 {
1182   cp_lexer *lexer;
1183   unsigned len;
1184   bool commit;
1185   saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1186   {
1187     len = lexer->saved_tokens.length ();
1188     cp_lexer_save_tokens (lexer);
1189   }
1190   void rollback ()
1191   {
1192     cp_lexer_rollback_tokens (lexer);
1193     commit = false;
1194   }
1195   ~saved_token_sentinel()
1196   {
1197     if (commit)
1198       cp_lexer_commit_tokens (lexer);
1199     gcc_assert (lexer->saved_tokens.length () == len);
1200   }
1201 };
1202
1203 /* Print a representation of the TOKEN on the STREAM.  */
1204
1205 static void
1206 cp_lexer_print_token (FILE * stream, cp_token *token)
1207 {
1208   /* We don't use cpp_type2name here because the parser defines
1209      a few tokens of its own.  */
1210   static const char *const token_names[] = {
1211     /* cpplib-defined token types */
1212 #define OP(e, s) #e,
1213 #define TK(e, s) #e,
1214     TTYPE_TABLE
1215 #undef OP
1216 #undef TK
1217     /* C++ parser token types - see "Manifest constants", above.  */
1218     "KEYWORD",
1219     "TEMPLATE_ID",
1220     "NESTED_NAME_SPECIFIER",
1221   };
1222
1223   /* For some tokens, print the associated data.  */
1224   switch (token->type)
1225     {
1226     case CPP_KEYWORD:
1227       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1228          For example, `struct' is mapped to an INTEGER_CST.  */
1229       if (!identifier_p (token->u.value))
1230         break;
1231       /* else fall through */
1232     case CPP_NAME:
1233       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1234       break;
1235
1236     case CPP_STRING:
1237     case CPP_STRING16:
1238     case CPP_STRING32:
1239     case CPP_WSTRING:
1240     case CPP_UTF8STRING:
1241       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1242       break;
1243
1244     case CPP_NUMBER:
1245       print_generic_expr (stream, token->u.value, 0);
1246       break;
1247
1248     default:
1249       /* If we have a name for the token, print it out.  Otherwise, we
1250          simply give the numeric code.  */
1251       if (token->type < ARRAY_SIZE(token_names))
1252         fputs (token_names[token->type], stream);
1253       else
1254         fprintf (stream, "[%d]", token->type);
1255       break;
1256     }
1257 }
1258
1259 DEBUG_FUNCTION void
1260 debug (cp_token &ref)
1261 {
1262   cp_lexer_print_token (stderr, &ref);
1263   fprintf (stderr, "\n");
1264 }
1265
1266 DEBUG_FUNCTION void
1267 debug (cp_token *ptr)
1268 {
1269   if (ptr)
1270     debug (*ptr);
1271   else
1272     fprintf (stderr, "<nil>\n");
1273 }
1274
1275
1276 /* Start emitting debugging information.  */
1277
1278 static void
1279 cp_lexer_start_debugging (cp_lexer* lexer)
1280 {
1281   lexer->debugging_p = true;
1282   cp_lexer_debug_stream = stderr;
1283 }
1284
1285 /* Stop emitting debugging information.  */
1286
1287 static void
1288 cp_lexer_stop_debugging (cp_lexer* lexer)
1289 {
1290   lexer->debugging_p = false;
1291   cp_lexer_debug_stream = NULL;
1292 }
1293
1294 /* Create a new cp_token_cache, representing a range of tokens.  */
1295
1296 static cp_token_cache *
1297 cp_token_cache_new (cp_token *first, cp_token *last)
1298 {
1299   cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1300   cache->first = first;
1301   cache->last = last;
1302   return cache;
1303 }
1304
1305 /* Diagnose if #pragma omp declare simd isn't followed immediately
1306    by function declaration or definition.  */
1307
1308 static inline void
1309 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1310 {
1311   if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1312     {
1313       error ("%<#pragma omp declare simd%> not immediately followed by "
1314              "function declaration or definition");
1315       parser->omp_declare_simd = NULL;
1316     }
1317 }
1318
1319 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1320    and put that into "omp declare simd" attribute.  */
1321
1322 static inline void
1323 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1324 {
1325   if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1326     {
1327       if (fndecl == error_mark_node)
1328         {
1329           parser->omp_declare_simd = NULL;
1330           return;
1331         }
1332       if (TREE_CODE (fndecl) != FUNCTION_DECL)
1333         {
1334           cp_ensure_no_omp_declare_simd (parser);
1335           return;
1336         }
1337     }
1338 }
1339 \f
1340 /* Decl-specifiers.  */
1341
1342 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1343
1344 static void
1345 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1346 {
1347   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1348 }
1349
1350 /* Declarators.  */
1351
1352 /* Nothing other than the parser should be creating declarators;
1353    declarators are a semi-syntactic representation of C++ entities.
1354    Other parts of the front end that need to create entities (like
1355    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1356
1357 static cp_declarator *make_call_declarator
1358   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1359 static cp_declarator *make_array_declarator
1360   (cp_declarator *, tree);
1361 static cp_declarator *make_pointer_declarator
1362   (cp_cv_quals, cp_declarator *, tree);
1363 static cp_declarator *make_reference_declarator
1364   (cp_cv_quals, cp_declarator *, bool, tree);
1365 static cp_parameter_declarator *make_parameter_declarator
1366   (cp_decl_specifier_seq *, cp_declarator *, tree);
1367 static cp_declarator *make_ptrmem_declarator
1368   (cp_cv_quals, tree, cp_declarator *, tree);
1369
1370 /* An erroneous declarator.  */
1371 static cp_declarator *cp_error_declarator;
1372
1373 /* The obstack on which declarators and related data structures are
1374    allocated.  */
1375 static struct obstack declarator_obstack;
1376
1377 /* Alloc BYTES from the declarator memory pool.  */
1378
1379 static inline void *
1380 alloc_declarator (size_t bytes)
1381 {
1382   return obstack_alloc (&declarator_obstack, bytes);
1383 }
1384
1385 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1386    common to all declarators.  */
1387
1388 static cp_declarator *
1389 make_declarator (cp_declarator_kind kind)
1390 {
1391   cp_declarator *declarator;
1392
1393   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1394   declarator->kind = kind;
1395   declarator->attributes = NULL_TREE;
1396   declarator->std_attributes = NULL_TREE;
1397   declarator->declarator = NULL;
1398   declarator->parameter_pack_p = false;
1399   declarator->id_loc = UNKNOWN_LOCATION;
1400
1401   return declarator;
1402 }
1403
1404 /* Make a declarator for a generalized identifier.  If
1405    QUALIFYING_SCOPE is non-NULL, the identifier is
1406    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1407    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1408    is, if any.   */
1409
1410 static cp_declarator *
1411 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1412                     special_function_kind sfk)
1413 {
1414   cp_declarator *declarator;
1415
1416   /* It is valid to write:
1417
1418        class C { void f(); };
1419        typedef C D;
1420        void D::f();
1421
1422      The standard is not clear about whether `typedef const C D' is
1423      legal; as of 2002-09-15 the committee is considering that
1424      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1425      well.  */
1426   if (qualifying_scope && TYPE_P (qualifying_scope))
1427     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1428
1429   gcc_assert (identifier_p (unqualified_name)
1430               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1431               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1432
1433   declarator = make_declarator (cdk_id);
1434   declarator->u.id.qualifying_scope = qualifying_scope;
1435   declarator->u.id.unqualified_name = unqualified_name;
1436   declarator->u.id.sfk = sfk;
1437   
1438   return declarator;
1439 }
1440
1441 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1442    of modifiers such as const or volatile to apply to the pointer
1443    type, represented as identifiers.  ATTRIBUTES represent the attributes that
1444    appertain to the pointer or reference.  */
1445
1446 cp_declarator *
1447 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1448                          tree attributes)
1449 {
1450   cp_declarator *declarator;
1451
1452   declarator = make_declarator (cdk_pointer);
1453   declarator->declarator = target;
1454   declarator->u.pointer.qualifiers = cv_qualifiers;
1455   declarator->u.pointer.class_type = NULL_TREE;
1456   if (target)
1457     {
1458       declarator->id_loc = target->id_loc;
1459       declarator->parameter_pack_p = target->parameter_pack_p;
1460       target->parameter_pack_p = false;
1461     }
1462   else
1463     declarator->parameter_pack_p = false;
1464
1465   declarator->std_attributes = attributes;
1466
1467   return declarator;
1468 }
1469
1470 /* Like make_pointer_declarator -- but for references.  ATTRIBUTES
1471    represent the attributes that appertain to the pointer or
1472    reference.  */
1473
1474 cp_declarator *
1475 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1476                            bool rvalue_ref, tree attributes)
1477 {
1478   cp_declarator *declarator;
1479
1480   declarator = make_declarator (cdk_reference);
1481   declarator->declarator = target;
1482   declarator->u.reference.qualifiers = cv_qualifiers;
1483   declarator->u.reference.rvalue_ref = rvalue_ref;
1484   if (target)
1485     {
1486       declarator->id_loc = target->id_loc;
1487       declarator->parameter_pack_p = target->parameter_pack_p;
1488       target->parameter_pack_p = false;
1489     }
1490   else
1491     declarator->parameter_pack_p = false;
1492
1493   declarator->std_attributes = attributes;
1494
1495   return declarator;
1496 }
1497
1498 /* Like make_pointer_declarator -- but for a pointer to a non-static
1499    member of CLASS_TYPE.  ATTRIBUTES represent the attributes that
1500    appertain to the pointer or reference.  */
1501
1502 cp_declarator *
1503 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1504                         cp_declarator *pointee,
1505                         tree attributes)
1506 {
1507   cp_declarator *declarator;
1508
1509   declarator = make_declarator (cdk_ptrmem);
1510   declarator->declarator = pointee;
1511   declarator->u.pointer.qualifiers = cv_qualifiers;
1512   declarator->u.pointer.class_type = class_type;
1513
1514   if (pointee)
1515     {
1516       declarator->parameter_pack_p = pointee->parameter_pack_p;
1517       pointee->parameter_pack_p = false;
1518     }
1519   else
1520     declarator->parameter_pack_p = false;
1521
1522   declarator->std_attributes = attributes;
1523
1524   return declarator;
1525 }
1526
1527 /* Make a declarator for the function given by TARGET, with the
1528    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1529    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1530    indicates what exceptions can be thrown.  */
1531
1532 cp_declarator *
1533 make_call_declarator (cp_declarator *target,
1534                       tree parms,
1535                       cp_cv_quals cv_qualifiers,
1536                       cp_virt_specifiers virt_specifiers,
1537                       cp_ref_qualifier ref_qualifier,
1538                       tree exception_specification,
1539                       tree late_return_type)
1540 {
1541   cp_declarator *declarator;
1542
1543   declarator = make_declarator (cdk_function);
1544   declarator->declarator = target;
1545   declarator->u.function.parameters = parms;
1546   declarator->u.function.qualifiers = cv_qualifiers;
1547   declarator->u.function.virt_specifiers = virt_specifiers;
1548   declarator->u.function.ref_qualifier = ref_qualifier;
1549   declarator->u.function.exception_specification = exception_specification;
1550   declarator->u.function.late_return_type = late_return_type;
1551   if (target)
1552     {
1553       declarator->id_loc = target->id_loc;
1554       declarator->parameter_pack_p = target->parameter_pack_p;
1555       target->parameter_pack_p = false;
1556     }
1557   else
1558     declarator->parameter_pack_p = false;
1559
1560   return declarator;
1561 }
1562
1563 /* Make a declarator for an array of BOUNDS elements, each of which is
1564    defined by ELEMENT.  */
1565
1566 cp_declarator *
1567 make_array_declarator (cp_declarator *element, tree bounds)
1568 {
1569   cp_declarator *declarator;
1570
1571   declarator = make_declarator (cdk_array);
1572   declarator->declarator = element;
1573   declarator->u.array.bounds = bounds;
1574   if (element)
1575     {
1576       declarator->id_loc = element->id_loc;
1577       declarator->parameter_pack_p = element->parameter_pack_p;
1578       element->parameter_pack_p = false;
1579     }
1580   else
1581     declarator->parameter_pack_p = false;
1582
1583   return declarator;
1584 }
1585
1586 /* Determine whether the declarator we've seen so far can be a
1587    parameter pack, when followed by an ellipsis.  */
1588 static bool 
1589 declarator_can_be_parameter_pack (cp_declarator *declarator)
1590 {
1591   /* Search for a declarator name, or any other declarator that goes
1592      after the point where the ellipsis could appear in a parameter
1593      pack. If we find any of these, then this declarator can not be
1594      made into a parameter pack.  */
1595   bool found = false;
1596   while (declarator && !found)
1597     {
1598       switch ((int)declarator->kind)
1599         {
1600         case cdk_id:
1601         case cdk_array:
1602           found = true;
1603           break;
1604
1605         case cdk_error:
1606           return true;
1607
1608         default:
1609           declarator = declarator->declarator;
1610           break;
1611         }
1612     }
1613
1614   return !found;
1615 }
1616
1617 cp_parameter_declarator *no_parameters;
1618
1619 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1620    DECLARATOR and DEFAULT_ARGUMENT.  */
1621
1622 cp_parameter_declarator *
1623 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1624                            cp_declarator *declarator,
1625                            tree default_argument)
1626 {
1627   cp_parameter_declarator *parameter;
1628
1629   parameter = ((cp_parameter_declarator *)
1630                alloc_declarator (sizeof (cp_parameter_declarator)));
1631   parameter->next = NULL;
1632   if (decl_specifiers)
1633     parameter->decl_specifiers = *decl_specifiers;
1634   else
1635     clear_decl_specs (&parameter->decl_specifiers);
1636   parameter->declarator = declarator;
1637   parameter->default_argument = default_argument;
1638   parameter->ellipsis_p = false;
1639
1640   return parameter;
1641 }
1642
1643 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1644
1645 static bool
1646 function_declarator_p (const cp_declarator *declarator)
1647 {
1648   while (declarator)
1649     {
1650       if (declarator->kind == cdk_function
1651           && declarator->declarator->kind == cdk_id)
1652         return true;
1653       if (declarator->kind == cdk_id
1654           || declarator->kind == cdk_error)
1655         return false;
1656       declarator = declarator->declarator;
1657     }
1658   return false;
1659 }
1660  
1661 /* The parser.  */
1662
1663 /* Overview
1664    --------
1665
1666    A cp_parser parses the token stream as specified by the C++
1667    grammar.  Its job is purely parsing, not semantic analysis.  For
1668    example, the parser breaks the token stream into declarators,
1669    expressions, statements, and other similar syntactic constructs.
1670    It does not check that the types of the expressions on either side
1671    of an assignment-statement are compatible, or that a function is
1672    not declared with a parameter of type `void'.
1673
1674    The parser invokes routines elsewhere in the compiler to perform
1675    semantic analysis and to build up the abstract syntax tree for the
1676    code processed.
1677
1678    The parser (and the template instantiation code, which is, in a
1679    way, a close relative of parsing) are the only parts of the
1680    compiler that should be calling push_scope and pop_scope, or
1681    related functions.  The parser (and template instantiation code)
1682    keeps track of what scope is presently active; everything else
1683    should simply honor that.  (The code that generates static
1684    initializers may also need to set the scope, in order to check
1685    access control correctly when emitting the initializers.)
1686
1687    Methodology
1688    -----------
1689
1690    The parser is of the standard recursive-descent variety.  Upcoming
1691    tokens in the token stream are examined in order to determine which
1692    production to use when parsing a non-terminal.  Some C++ constructs
1693    require arbitrary look ahead to disambiguate.  For example, it is
1694    impossible, in the general case, to tell whether a statement is an
1695    expression or declaration without scanning the entire statement.
1696    Therefore, the parser is capable of "parsing tentatively."  When the
1697    parser is not sure what construct comes next, it enters this mode.
1698    Then, while we attempt to parse the construct, the parser queues up
1699    error messages, rather than issuing them immediately, and saves the
1700    tokens it consumes.  If the construct is parsed successfully, the
1701    parser "commits", i.e., it issues any queued error messages and
1702    the tokens that were being preserved are permanently discarded.
1703    If, however, the construct is not parsed successfully, the parser
1704    rolls back its state completely so that it can resume parsing using
1705    a different alternative.
1706
1707    Future Improvements
1708    -------------------
1709
1710    The performance of the parser could probably be improved substantially.
1711    We could often eliminate the need to parse tentatively by looking ahead
1712    a little bit.  In some places, this approach might not entirely eliminate
1713    the need to parse tentatively, but it might still speed up the average
1714    case.  */
1715
1716 /* Flags that are passed to some parsing functions.  These values can
1717    be bitwise-ored together.  */
1718
1719 enum
1720 {
1721   /* No flags.  */
1722   CP_PARSER_FLAGS_NONE = 0x0,
1723   /* The construct is optional.  If it is not present, then no error
1724      should be issued.  */
1725   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1726   /* When parsing a type-specifier, treat user-defined type-names
1727      as non-type identifiers.  */
1728   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1729   /* When parsing a type-specifier, do not try to parse a class-specifier
1730      or enum-specifier.  */
1731   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1732   /* When parsing a decl-specifier-seq, only allow type-specifier or
1733      constexpr.  */
1734   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1735 };
1736
1737 /* This type is used for parameters and variables which hold
1738    combinations of the above flags.  */
1739 typedef int cp_parser_flags;
1740
1741 /* The different kinds of declarators we want to parse.  */
1742
1743 typedef enum cp_parser_declarator_kind
1744 {
1745   /* We want an abstract declarator.  */
1746   CP_PARSER_DECLARATOR_ABSTRACT,
1747   /* We want a named declarator.  */
1748   CP_PARSER_DECLARATOR_NAMED,
1749   /* We don't mind, but the name must be an unqualified-id.  */
1750   CP_PARSER_DECLARATOR_EITHER
1751 } cp_parser_declarator_kind;
1752
1753 /* The precedence values used to parse binary expressions.  The minimum value
1754    of PREC must be 1, because zero is reserved to quickly discriminate
1755    binary operators from other tokens.  */
1756
1757 enum cp_parser_prec
1758 {
1759   PREC_NOT_OPERATOR,
1760   PREC_LOGICAL_OR_EXPRESSION,
1761   PREC_LOGICAL_AND_EXPRESSION,
1762   PREC_INCLUSIVE_OR_EXPRESSION,
1763   PREC_EXCLUSIVE_OR_EXPRESSION,
1764   PREC_AND_EXPRESSION,
1765   PREC_EQUALITY_EXPRESSION,
1766   PREC_RELATIONAL_EXPRESSION,
1767   PREC_SHIFT_EXPRESSION,
1768   PREC_ADDITIVE_EXPRESSION,
1769   PREC_MULTIPLICATIVE_EXPRESSION,
1770   PREC_PM_EXPRESSION,
1771   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1772 };
1773
1774 /* A mapping from a token type to a corresponding tree node type, with a
1775    precedence value.  */
1776
1777 typedef struct cp_parser_binary_operations_map_node
1778 {
1779   /* The token type.  */
1780   enum cpp_ttype token_type;
1781   /* The corresponding tree code.  */
1782   enum tree_code tree_type;
1783   /* The precedence of this operator.  */
1784   enum cp_parser_prec prec;
1785 } cp_parser_binary_operations_map_node;
1786
1787 typedef struct cp_parser_expression_stack_entry
1788 {
1789   /* Left hand side of the binary operation we are currently
1790      parsing.  */
1791   tree lhs;
1792   /* Original tree code for left hand side, if it was a binary
1793      expression itself (used for -Wparentheses).  */
1794   enum tree_code lhs_type;
1795   /* Tree code for the binary operation we are parsing.  */
1796   enum tree_code tree_type;
1797   /* Precedence of the binary operation we are parsing.  */
1798   enum cp_parser_prec prec;
1799   /* Location of the binary operation we are parsing.  */
1800   location_t loc;
1801 } cp_parser_expression_stack_entry;
1802
1803 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1804    entries because precedence levels on the stack are monotonically
1805    increasing.  */
1806 typedef struct cp_parser_expression_stack_entry
1807   cp_parser_expression_stack[NUM_PREC_VALUES];
1808
1809 /* Prototypes.  */
1810
1811 /* Constructors and destructors.  */
1812
1813 static cp_parser_context *cp_parser_context_new
1814   (cp_parser_context *);
1815
1816 /* Class variables.  */
1817
1818 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1819
1820 /* The operator-precedence table used by cp_parser_binary_expression.
1821    Transformed into an associative array (binops_by_token) by
1822    cp_parser_new.  */
1823
1824 static const cp_parser_binary_operations_map_node binops[] = {
1825   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1826   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1827
1828   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1829   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1830   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1831
1832   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1833   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1834
1835   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1836   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1837
1838   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1839   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1840   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1841   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1842
1843   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1844   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1845
1846   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1847
1848   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1849
1850   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1851
1852   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1853
1854   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1855 };
1856
1857 /* The same as binops, but initialized by cp_parser_new so that
1858    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1859    for speed.  */
1860 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1861
1862 /* Constructors and destructors.  */
1863
1864 /* Construct a new context.  The context below this one on the stack
1865    is given by NEXT.  */
1866
1867 static cp_parser_context *
1868 cp_parser_context_new (cp_parser_context* next)
1869 {
1870   cp_parser_context *context;
1871
1872   /* Allocate the storage.  */
1873   if (cp_parser_context_free_list != NULL)
1874     {
1875       /* Pull the first entry from the free list.  */
1876       context = cp_parser_context_free_list;
1877       cp_parser_context_free_list = context->next;
1878       memset (context, 0, sizeof (*context));
1879     }
1880   else
1881     context = ggc_cleared_alloc<cp_parser_context> ();
1882
1883   /* No errors have occurred yet in this context.  */
1884   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1885   /* If this is not the bottommost context, copy information that we
1886      need from the previous context.  */
1887   if (next)
1888     {
1889       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1890          expression, then we are parsing one in this context, too.  */
1891       context->object_type = next->object_type;
1892       /* Thread the stack.  */
1893       context->next = next;
1894     }
1895
1896   return context;
1897 }
1898
1899 /* Managing the unparsed function queues.  */
1900
1901 #define unparsed_funs_with_default_args \
1902   parser->unparsed_queues->last ().funs_with_default_args
1903 #define unparsed_funs_with_definitions \
1904   parser->unparsed_queues->last ().funs_with_definitions
1905 #define unparsed_nsdmis \
1906   parser->unparsed_queues->last ().nsdmis
1907 #define unparsed_classes \
1908   parser->unparsed_queues->last ().classes
1909
1910 static void
1911 push_unparsed_function_queues (cp_parser *parser)
1912 {
1913   cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1914   vec_safe_push (parser->unparsed_queues, e);
1915 }
1916
1917 static void
1918 pop_unparsed_function_queues (cp_parser *parser)
1919 {
1920   release_tree_vector (unparsed_funs_with_definitions);
1921   parser->unparsed_queues->pop ();
1922 }
1923
1924 /* Prototypes.  */
1925
1926 /* Constructors and destructors.  */
1927
1928 static cp_parser *cp_parser_new
1929   (void);
1930
1931 /* Routines to parse various constructs.
1932
1933    Those that return `tree' will return the error_mark_node (rather
1934    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1935    Sometimes, they will return an ordinary node if error-recovery was
1936    attempted, even though a parse error occurred.  So, to check
1937    whether or not a parse error occurred, you should always use
1938    cp_parser_error_occurred.  If the construct is optional (indicated
1939    either by an `_opt' in the name of the function that does the
1940    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1941    the construct is not present.  */
1942
1943 /* Lexical conventions [gram.lex]  */
1944
1945 static tree cp_parser_identifier
1946   (cp_parser *);
1947 static tree cp_parser_string_literal
1948   (cp_parser *, bool, bool, bool);
1949 static tree cp_parser_userdef_char_literal
1950   (cp_parser *);
1951 static tree cp_parser_userdef_string_literal
1952   (tree);
1953 static tree cp_parser_userdef_numeric_literal
1954   (cp_parser *);
1955
1956 /* Basic concepts [gram.basic]  */
1957
1958 static bool cp_parser_translation_unit
1959   (cp_parser *);
1960
1961 /* Expressions [gram.expr]  */
1962
1963 static tree cp_parser_primary_expression
1964   (cp_parser *, bool, bool, bool, cp_id_kind *);
1965 static tree cp_parser_id_expression
1966   (cp_parser *, bool, bool, bool *, bool, bool);
1967 static tree cp_parser_unqualified_id
1968   (cp_parser *, bool, bool, bool, bool);
1969 static tree cp_parser_nested_name_specifier_opt
1970   (cp_parser *, bool, bool, bool, bool);
1971 static tree cp_parser_nested_name_specifier
1972   (cp_parser *, bool, bool, bool, bool);
1973 static tree cp_parser_qualifying_entity
1974   (cp_parser *, bool, bool, bool, bool, bool);
1975 static tree cp_parser_postfix_expression
1976   (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1977 static tree cp_parser_postfix_open_square_expression
1978   (cp_parser *, tree, bool, bool);
1979 static tree cp_parser_postfix_dot_deref_expression
1980   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1981 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1982   (cp_parser *, int, bool, bool, bool *, bool = false);
1983 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1984 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1985 static void cp_parser_pseudo_destructor_name
1986   (cp_parser *, tree, tree *, tree *);
1987 static tree cp_parser_unary_expression
1988   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
1989 static enum tree_code cp_parser_unary_operator
1990   (cp_token *);
1991 static tree cp_parser_new_expression
1992   (cp_parser *);
1993 static vec<tree, va_gc> *cp_parser_new_placement
1994   (cp_parser *);
1995 static tree cp_parser_new_type_id
1996   (cp_parser *, tree *);
1997 static cp_declarator *cp_parser_new_declarator_opt
1998   (cp_parser *);
1999 static cp_declarator *cp_parser_direct_new_declarator
2000   (cp_parser *);
2001 static vec<tree, va_gc> *cp_parser_new_initializer
2002   (cp_parser *);
2003 static tree cp_parser_delete_expression
2004   (cp_parser *);
2005 static tree cp_parser_cast_expression
2006   (cp_parser *, bool, bool, bool, cp_id_kind *);
2007 static tree cp_parser_binary_expression
2008   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2009 static tree cp_parser_question_colon_clause
2010   (cp_parser *, tree);
2011 static tree cp_parser_assignment_expression
2012   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2013 static enum tree_code cp_parser_assignment_operator_opt
2014   (cp_parser *);
2015 static tree cp_parser_expression
2016   (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2017 static tree cp_parser_constant_expression
2018   (cp_parser *, bool = false, bool * = NULL);
2019 static tree cp_parser_builtin_offsetof
2020   (cp_parser *);
2021 static tree cp_parser_lambda_expression
2022   (cp_parser *);
2023 static void cp_parser_lambda_introducer
2024   (cp_parser *, tree);
2025 static bool cp_parser_lambda_declarator_opt
2026   (cp_parser *, tree);
2027 static void cp_parser_lambda_body
2028   (cp_parser *, tree);
2029
2030 /* Statements [gram.stmt.stmt]  */
2031
2032 static void cp_parser_statement
2033   (cp_parser *, tree, bool, bool *);
2034 static void cp_parser_label_for_labeled_statement
2035 (cp_parser *, tree);
2036 static tree cp_parser_expression_statement
2037   (cp_parser *, tree);
2038 static tree cp_parser_compound_statement
2039   (cp_parser *, tree, bool, bool);
2040 static void cp_parser_statement_seq_opt
2041   (cp_parser *, tree);
2042 static tree cp_parser_selection_statement
2043   (cp_parser *, bool *);
2044 static tree cp_parser_condition
2045   (cp_parser *);
2046 static tree cp_parser_iteration_statement
2047   (cp_parser *, bool);
2048 static bool cp_parser_for_init_statement
2049   (cp_parser *, tree *decl);
2050 static tree cp_parser_for
2051   (cp_parser *, bool);
2052 static tree cp_parser_c_for
2053   (cp_parser *, tree, tree, bool);
2054 static tree cp_parser_range_for
2055   (cp_parser *, tree, tree, tree, bool);
2056 static void do_range_for_auto_deduction
2057   (tree, tree);
2058 static tree cp_parser_perform_range_for_lookup
2059   (tree, tree *, tree *);
2060 static tree cp_parser_range_for_member_function
2061   (tree, tree);
2062 static tree cp_parser_jump_statement
2063   (cp_parser *);
2064 static void cp_parser_declaration_statement
2065   (cp_parser *);
2066
2067 static tree cp_parser_implicitly_scoped_statement
2068   (cp_parser *, bool *);
2069 static void cp_parser_already_scoped_statement
2070   (cp_parser *);
2071
2072 /* Declarations [gram.dcl.dcl] */
2073
2074 static void cp_parser_declaration_seq_opt
2075   (cp_parser *);
2076 static void cp_parser_declaration
2077   (cp_parser *);
2078 static void cp_parser_block_declaration
2079   (cp_parser *, bool);
2080 static void cp_parser_simple_declaration
2081   (cp_parser *, bool, tree *);
2082 static void cp_parser_decl_specifier_seq
2083   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2084 static tree cp_parser_storage_class_specifier_opt
2085   (cp_parser *);
2086 static tree cp_parser_function_specifier_opt
2087   (cp_parser *, cp_decl_specifier_seq *);
2088 static tree cp_parser_type_specifier
2089   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2090    int *, bool *);
2091 static tree cp_parser_simple_type_specifier
2092   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2093 static tree cp_parser_type_name
2094   (cp_parser *);
2095 static tree cp_parser_nonclass_name 
2096   (cp_parser* parser);
2097 static tree cp_parser_elaborated_type_specifier
2098   (cp_parser *, bool, bool);
2099 static tree cp_parser_enum_specifier
2100   (cp_parser *);
2101 static void cp_parser_enumerator_list
2102   (cp_parser *, tree);
2103 static void cp_parser_enumerator_definition
2104   (cp_parser *, tree);
2105 static tree cp_parser_namespace_name
2106   (cp_parser *);
2107 static void cp_parser_namespace_definition
2108   (cp_parser *);
2109 static void cp_parser_namespace_body
2110   (cp_parser *);
2111 static tree cp_parser_qualified_namespace_specifier
2112   (cp_parser *);
2113 static void cp_parser_namespace_alias_definition
2114   (cp_parser *);
2115 static bool cp_parser_using_declaration
2116   (cp_parser *, bool);
2117 static void cp_parser_using_directive
2118   (cp_parser *);
2119 static tree cp_parser_alias_declaration
2120   (cp_parser *);
2121 static void cp_parser_asm_definition
2122   (cp_parser *);
2123 static void cp_parser_linkage_specification
2124   (cp_parser *);
2125 static void cp_parser_static_assert
2126   (cp_parser *, bool);
2127 static tree cp_parser_decltype
2128   (cp_parser *);
2129
2130 /* Declarators [gram.dcl.decl] */
2131
2132 static tree cp_parser_init_declarator
2133   (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2134    bool, bool, int, bool *, tree *, location_t *);
2135 static cp_declarator *cp_parser_declarator
2136   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2137 static cp_declarator *cp_parser_direct_declarator
2138   (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2139 static enum tree_code cp_parser_ptr_operator
2140   (cp_parser *, tree *, cp_cv_quals *, tree *);
2141 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2142   (cp_parser *);
2143 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2144   (cp_parser *);
2145 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2146   (cp_parser *);
2147 static tree cp_parser_late_return_type_opt
2148   (cp_parser *, cp_declarator *, cp_cv_quals);
2149 static tree cp_parser_declarator_id
2150   (cp_parser *, bool);
2151 static tree cp_parser_type_id
2152   (cp_parser *);
2153 static tree cp_parser_template_type_arg
2154   (cp_parser *);
2155 static tree cp_parser_trailing_type_id (cp_parser *);
2156 static tree cp_parser_type_id_1
2157   (cp_parser *, bool, bool);
2158 static void cp_parser_type_specifier_seq
2159   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2160 static tree cp_parser_parameter_declaration_clause
2161   (cp_parser *);
2162 static tree cp_parser_parameter_declaration_list
2163   (cp_parser *, bool *);
2164 static cp_parameter_declarator *cp_parser_parameter_declaration
2165   (cp_parser *, bool, bool *);
2166 static tree cp_parser_default_argument 
2167   (cp_parser *, bool);
2168 static void cp_parser_function_body
2169   (cp_parser *, bool);
2170 static tree cp_parser_initializer
2171   (cp_parser *, bool *, bool *);
2172 static tree cp_parser_initializer_clause
2173   (cp_parser *, bool *);
2174 static tree cp_parser_braced_list
2175   (cp_parser*, bool*);
2176 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2177   (cp_parser *, bool *);
2178
2179 static bool cp_parser_ctor_initializer_opt_and_function_body
2180   (cp_parser *, bool);
2181
2182 static tree cp_parser_late_parsing_omp_declare_simd
2183   (cp_parser *, tree);
2184
2185 static tree cp_parser_late_parsing_cilk_simd_fn_info
2186   (cp_parser *, tree);
2187
2188 static tree synthesize_implicit_template_parm
2189   (cp_parser *);
2190 static tree finish_fully_implicit_template
2191   (cp_parser *, tree);
2192
2193 /* Classes [gram.class] */
2194
2195 static tree cp_parser_class_name
2196   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2197 static tree cp_parser_class_specifier
2198   (cp_parser *);
2199 static tree cp_parser_class_head
2200   (cp_parser *, bool *);
2201 static enum tag_types cp_parser_class_key
2202   (cp_parser *);
2203 static void cp_parser_type_parameter_key
2204   (cp_parser* parser);
2205 static void cp_parser_member_specification_opt
2206   (cp_parser *);
2207 static void cp_parser_member_declaration
2208   (cp_parser *);
2209 static tree cp_parser_pure_specifier
2210   (cp_parser *);
2211 static tree cp_parser_constant_initializer
2212   (cp_parser *);
2213
2214 /* Derived classes [gram.class.derived] */
2215
2216 static tree cp_parser_base_clause
2217   (cp_parser *);
2218 static tree cp_parser_base_specifier
2219   (cp_parser *);
2220
2221 /* Special member functions [gram.special] */
2222
2223 static tree cp_parser_conversion_function_id
2224   (cp_parser *);
2225 static tree cp_parser_conversion_type_id
2226   (cp_parser *);
2227 static cp_declarator *cp_parser_conversion_declarator_opt
2228   (cp_parser *);
2229 static bool cp_parser_ctor_initializer_opt
2230   (cp_parser *);
2231 static void cp_parser_mem_initializer_list
2232   (cp_parser *);
2233 static tree cp_parser_mem_initializer
2234   (cp_parser *);
2235 static tree cp_parser_mem_initializer_id
2236   (cp_parser *);
2237
2238 /* Overloading [gram.over] */
2239
2240 static tree cp_parser_operator_function_id
2241   (cp_parser *);
2242 static tree cp_parser_operator
2243   (cp_parser *);
2244
2245 /* Templates [gram.temp] */
2246
2247 static void cp_parser_template_declaration
2248   (cp_parser *, bool);
2249 static tree cp_parser_template_parameter_list
2250   (cp_parser *);
2251 static tree cp_parser_template_parameter
2252   (cp_parser *, bool *, bool *);
2253 static tree cp_parser_type_parameter
2254   (cp_parser *, bool *);
2255 static tree cp_parser_template_id
2256   (cp_parser *, bool, bool, enum tag_types, bool);
2257 static tree cp_parser_template_name
2258   (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2259 static tree cp_parser_template_argument_list
2260   (cp_parser *);
2261 static tree cp_parser_template_argument
2262   (cp_parser *);
2263 static void cp_parser_explicit_instantiation
2264   (cp_parser *);
2265 static void cp_parser_explicit_specialization
2266   (cp_parser *);
2267
2268 /* Exception handling [gram.exception] */
2269
2270 static tree cp_parser_try_block
2271   (cp_parser *);
2272 static bool cp_parser_function_try_block
2273   (cp_parser *);
2274 static void cp_parser_handler_seq
2275   (cp_parser *);
2276 static void cp_parser_handler
2277   (cp_parser *);
2278 static tree cp_parser_exception_declaration
2279   (cp_parser *);
2280 static tree cp_parser_throw_expression
2281   (cp_parser *);
2282 static tree cp_parser_exception_specification_opt
2283   (cp_parser *);
2284 static tree cp_parser_type_id_list
2285   (cp_parser *);
2286
2287 /* GNU Extensions */
2288
2289 static tree cp_parser_asm_specification_opt
2290   (cp_parser *);
2291 static tree cp_parser_asm_operand_list
2292   (cp_parser *);
2293 static tree cp_parser_asm_clobber_list
2294   (cp_parser *);
2295 static tree cp_parser_asm_label_list
2296   (cp_parser *);
2297 static bool cp_next_tokens_can_be_attribute_p
2298   (cp_parser *);
2299 static bool cp_next_tokens_can_be_gnu_attribute_p
2300   (cp_parser *);
2301 static bool cp_next_tokens_can_be_std_attribute_p
2302   (cp_parser *);
2303 static bool cp_nth_tokens_can_be_std_attribute_p
2304   (cp_parser *, size_t);
2305 static bool cp_nth_tokens_can_be_gnu_attribute_p
2306   (cp_parser *, size_t);
2307 static bool cp_nth_tokens_can_be_attribute_p
2308   (cp_parser *, size_t);
2309 static tree cp_parser_attributes_opt
2310   (cp_parser *);
2311 static tree cp_parser_gnu_attributes_opt
2312   (cp_parser *);
2313 static tree cp_parser_gnu_attribute_list
2314   (cp_parser *);
2315 static tree cp_parser_std_attribute
2316   (cp_parser *);
2317 static tree cp_parser_std_attribute_spec
2318   (cp_parser *);
2319 static tree cp_parser_std_attribute_spec_seq
2320   (cp_parser *);
2321 static bool cp_parser_extension_opt
2322   (cp_parser *, int *);
2323 static void cp_parser_label_declaration
2324   (cp_parser *);
2325
2326 /* Transactional Memory Extensions */
2327
2328 static tree cp_parser_transaction
2329   (cp_parser *, enum rid);
2330 static tree cp_parser_transaction_expression
2331   (cp_parser *, enum rid);
2332 static bool cp_parser_function_transaction
2333   (cp_parser *, enum rid);
2334 static tree cp_parser_transaction_cancel
2335   (cp_parser *);
2336
2337 enum pragma_context {
2338   pragma_external,
2339   pragma_member,
2340   pragma_objc_icode,
2341   pragma_stmt,
2342   pragma_compound
2343 };
2344 static bool cp_parser_pragma
2345   (cp_parser *, enum pragma_context);
2346
2347 /* Objective-C++ Productions */
2348
2349 static tree cp_parser_objc_message_receiver
2350   (cp_parser *);
2351 static tree cp_parser_objc_message_args
2352   (cp_parser *);
2353 static tree cp_parser_objc_message_expression
2354   (cp_parser *);
2355 static tree cp_parser_objc_encode_expression
2356   (cp_parser *);
2357 static tree cp_parser_objc_defs_expression
2358   (cp_parser *);
2359 static tree cp_parser_objc_protocol_expression
2360   (cp_parser *);
2361 static tree cp_parser_objc_selector_expression
2362   (cp_parser *);
2363 static tree cp_parser_objc_expression
2364   (cp_parser *);
2365 static bool cp_parser_objc_selector_p
2366   (enum cpp_ttype);
2367 static tree cp_parser_objc_selector
2368   (cp_parser *);
2369 static tree cp_parser_objc_protocol_refs_opt
2370   (cp_parser *);
2371 static void cp_parser_objc_declaration
2372   (cp_parser *, tree);
2373 static tree cp_parser_objc_statement
2374   (cp_parser *);
2375 static bool cp_parser_objc_valid_prefix_attributes
2376   (cp_parser *, tree *);
2377 static void cp_parser_objc_at_property_declaration 
2378   (cp_parser *) ;
2379 static void cp_parser_objc_at_synthesize_declaration 
2380   (cp_parser *) ;
2381 static void cp_parser_objc_at_dynamic_declaration
2382   (cp_parser *) ;
2383 static tree cp_parser_objc_struct_declaration
2384   (cp_parser *) ;
2385
2386 /* Utility Routines */
2387
2388 static tree cp_parser_lookup_name
2389   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2390 static tree cp_parser_lookup_name_simple
2391   (cp_parser *, tree, location_t);
2392 static tree cp_parser_maybe_treat_template_as_class
2393   (tree, bool);
2394 static bool cp_parser_check_declarator_template_parameters
2395   (cp_parser *, cp_declarator *, location_t);
2396 static bool cp_parser_check_template_parameters
2397   (cp_parser *, unsigned, location_t, cp_declarator *);
2398 static tree cp_parser_simple_cast_expression
2399   (cp_parser *);
2400 static tree cp_parser_global_scope_opt
2401   (cp_parser *, bool);
2402 static bool cp_parser_constructor_declarator_p
2403   (cp_parser *, bool);
2404 static tree cp_parser_function_definition_from_specifiers_and_declarator
2405   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2406 static tree cp_parser_function_definition_after_declarator
2407   (cp_parser *, bool);
2408 static void cp_parser_template_declaration_after_export
2409   (cp_parser *, bool);
2410 static void cp_parser_perform_template_parameter_access_checks
2411   (vec<deferred_access_check, va_gc> *);
2412 static tree cp_parser_single_declaration
2413   (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2414 static tree cp_parser_functional_cast
2415   (cp_parser *, tree);
2416 static tree cp_parser_save_member_function_body
2417   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2418 static tree cp_parser_save_nsdmi
2419   (cp_parser *);
2420 static tree cp_parser_enclosed_template_argument_list
2421   (cp_parser *);
2422 static void cp_parser_save_default_args
2423   (cp_parser *, tree);
2424 static void cp_parser_late_parsing_for_member
2425   (cp_parser *, tree);
2426 static tree cp_parser_late_parse_one_default_arg
2427   (cp_parser *, tree, tree, tree);
2428 static void cp_parser_late_parsing_nsdmi
2429   (cp_parser *, tree);
2430 static void cp_parser_late_parsing_default_args
2431   (cp_parser *, tree);
2432 static tree cp_parser_sizeof_operand
2433   (cp_parser *, enum rid);
2434 static tree cp_parser_trait_expr
2435   (cp_parser *, enum rid);
2436 static bool cp_parser_declares_only_class_p
2437   (cp_parser *);
2438 static void cp_parser_set_storage_class
2439   (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2440 static void cp_parser_set_decl_spec_type
2441   (cp_decl_specifier_seq *, tree, cp_token *, bool);
2442 static void set_and_check_decl_spec_loc
2443   (cp_decl_specifier_seq *decl_specs,
2444    cp_decl_spec ds, cp_token *);
2445 static bool cp_parser_friend_p
2446   (const cp_decl_specifier_seq *);
2447 static void cp_parser_required_error
2448   (cp_parser *, required_token, bool);
2449 static cp_token *cp_parser_require
2450   (cp_parser *, enum cpp_ttype, required_token);
2451 static cp_token *cp_parser_require_keyword
2452   (cp_parser *, enum rid, required_token);
2453 static bool cp_parser_token_starts_function_definition_p
2454   (cp_token *);
2455 static bool cp_parser_next_token_starts_class_definition_p
2456   (cp_parser *);
2457 static bool cp_parser_next_token_ends_template_argument_p
2458   (cp_parser *);
2459 static bool cp_parser_nth_token_starts_template_argument_list_p
2460   (cp_parser *, size_t);
2461 static enum tag_types cp_parser_token_is_class_key
2462   (cp_token *);
2463 static enum tag_types cp_parser_token_is_type_parameter_key
2464   (cp_token *);
2465 static void cp_parser_check_class_key
2466   (enum tag_types, tree type);
2467 static void cp_parser_check_access_in_redeclaration
2468   (tree type, location_t location);
2469 static bool cp_parser_optional_template_keyword
2470   (cp_parser *);
2471 static void cp_parser_pre_parsed_nested_name_specifier
2472   (cp_parser *);
2473 static bool cp_parser_cache_group
2474   (cp_parser *, enum cpp_ttype, unsigned);
2475 static tree cp_parser_cache_defarg
2476   (cp_parser *parser, bool nsdmi);
2477 static void cp_parser_parse_tentatively
2478   (cp_parser *);
2479 static void cp_parser_commit_to_tentative_parse
2480   (cp_parser *);
2481 static void cp_parser_commit_to_topmost_tentative_parse
2482   (cp_parser *);
2483 static void cp_parser_abort_tentative_parse
2484   (cp_parser *);
2485 static bool cp_parser_parse_definitely
2486   (cp_parser *);
2487 static inline bool cp_parser_parsing_tentatively
2488   (cp_parser *);
2489 static bool cp_parser_uncommitted_to_tentative_parse_p
2490   (cp_parser *);
2491 static void cp_parser_error
2492   (cp_parser *, const char *);
2493 static void cp_parser_name_lookup_error
2494   (cp_parser *, tree, tree, name_lookup_error, location_t);
2495 static bool cp_parser_simulate_error
2496   (cp_parser *);
2497 static bool cp_parser_check_type_definition
2498   (cp_parser *);
2499 static void cp_parser_check_for_definition_in_return_type
2500   (cp_declarator *, tree, location_t type_location);
2501 static void cp_parser_check_for_invalid_template_id
2502   (cp_parser *, tree, enum tag_types, location_t location);
2503 static bool cp_parser_non_integral_constant_expression
2504   (cp_parser *, non_integral_constant);
2505 static void cp_parser_diagnose_invalid_type_name
2506   (cp_parser *, tree, location_t);
2507 static bool cp_parser_parse_and_diagnose_invalid_type_name
2508   (cp_parser *);
2509 static int cp_parser_skip_to_closing_parenthesis
2510   (cp_parser *, bool, bool, bool);
2511 static void cp_parser_skip_to_end_of_statement
2512   (cp_parser *);
2513 static void cp_parser_consume_semicolon_at_end_of_statement
2514   (cp_parser *);
2515 static void cp_parser_skip_to_end_of_block_or_statement
2516   (cp_parser *);
2517 static bool cp_parser_skip_to_closing_brace
2518   (cp_parser *);
2519 static void cp_parser_skip_to_end_of_template_parameter_list
2520   (cp_parser *);
2521 static void cp_parser_skip_to_pragma_eol
2522   (cp_parser*, cp_token *);
2523 static bool cp_parser_error_occurred
2524   (cp_parser *);
2525 static bool cp_parser_allow_gnu_extensions_p
2526   (cp_parser *);
2527 static bool cp_parser_is_pure_string_literal
2528   (cp_token *);
2529 static bool cp_parser_is_string_literal
2530   (cp_token *);
2531 static bool cp_parser_is_keyword
2532   (cp_token *, enum rid);
2533 static tree cp_parser_make_typename_type
2534   (cp_parser *, tree, location_t location);
2535 static cp_declarator * cp_parser_make_indirect_declarator
2536   (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2537 static bool cp_parser_compound_literal_p
2538   (cp_parser *);
2539 static bool cp_parser_array_designator_p
2540   (cp_parser *);
2541 static bool cp_parser_skip_to_closing_square_bracket
2542   (cp_parser *);
2543
2544 /* Returns nonzero if we are parsing tentatively.  */
2545
2546 static inline bool
2547 cp_parser_parsing_tentatively (cp_parser* parser)
2548 {
2549   return parser->context->next != NULL;
2550 }
2551
2552 /* Returns nonzero if TOKEN is a string literal.  */
2553
2554 static bool
2555 cp_parser_is_pure_string_literal (cp_token* token)
2556 {
2557   return (token->type == CPP_STRING ||
2558           token->type == CPP_STRING16 ||
2559           token->type == CPP_STRING32 ||
2560           token->type == CPP_WSTRING ||
2561           token->type == CPP_UTF8STRING);
2562 }
2563
2564 /* Returns nonzero if TOKEN is a string literal
2565    of a user-defined string literal.  */
2566
2567 static bool
2568 cp_parser_is_string_literal (cp_token* token)
2569 {
2570   return (cp_parser_is_pure_string_literal (token) ||
2571           token->type == CPP_STRING_USERDEF ||
2572           token->type == CPP_STRING16_USERDEF ||
2573           token->type == CPP_STRING32_USERDEF ||
2574           token->type == CPP_WSTRING_USERDEF ||
2575           token->type == CPP_UTF8STRING_USERDEF);
2576 }
2577
2578 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2579
2580 static bool
2581 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2582 {
2583   return token->keyword == keyword;
2584 }
2585
2586 /* If not parsing tentatively, issue a diagnostic of the form
2587       FILE:LINE: MESSAGE before TOKEN
2588    where TOKEN is the next token in the input stream.  MESSAGE
2589    (specified by the caller) is usually of the form "expected
2590    OTHER-TOKEN".  */
2591
2592 static void
2593 cp_parser_error (cp_parser* parser, const char* gmsgid)
2594 {
2595   if (!cp_parser_simulate_error (parser))
2596     {
2597       cp_token *token = cp_lexer_peek_token (parser->lexer);
2598       /* This diagnostic makes more sense if it is tagged to the line
2599          of the token we just peeked at.  */
2600       cp_lexer_set_source_position_from_token (token);
2601
2602       if (token->type == CPP_PRAGMA)
2603         {
2604           error_at (token->location,
2605                     "%<#pragma%> is not allowed here");
2606           cp_parser_skip_to_pragma_eol (parser, token);
2607           return;
2608         }
2609
2610       c_parse_error (gmsgid,
2611                      /* Because c_parser_error does not understand
2612                         CPP_KEYWORD, keywords are treated like
2613                         identifiers.  */
2614                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2615                      token->u.value, token->flags);
2616     }
2617 }
2618
2619 /* Issue an error about name-lookup failing.  NAME is the
2620    IDENTIFIER_NODE DECL is the result of
2621    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2622    the thing that we hoped to find.  */
2623
2624 static void
2625 cp_parser_name_lookup_error (cp_parser* parser,
2626                              tree name,
2627                              tree decl,
2628                              name_lookup_error desired,
2629                              location_t location)
2630 {
2631   /* If name lookup completely failed, tell the user that NAME was not
2632      declared.  */
2633   if (decl == error_mark_node)
2634     {
2635       if (parser->scope && parser->scope != global_namespace)
2636         error_at (location, "%<%E::%E%> has not been declared",
2637                   parser->scope, name);
2638       else if (parser->scope == global_namespace)
2639         error_at (location, "%<::%E%> has not been declared", name);
2640       else if (parser->object_scope
2641                && !CLASS_TYPE_P (parser->object_scope))
2642         error_at (location, "request for member %qE in non-class type %qT",
2643                   name, parser->object_scope);
2644       else if (parser->object_scope)
2645         error_at (location, "%<%T::%E%> has not been declared",
2646                   parser->object_scope, name);
2647       else
2648         error_at (location, "%qE has not been declared", name);
2649     }
2650   else if (parser->scope && parser->scope != global_namespace)
2651     {
2652       switch (desired)
2653         {
2654           case NLE_TYPE:
2655             error_at (location, "%<%E::%E%> is not a type",
2656                                 parser->scope, name);
2657             break;
2658           case NLE_CXX98:
2659             error_at (location, "%<%E::%E%> is not a class or namespace",
2660                                 parser->scope, name);
2661             break;
2662           case NLE_NOT_CXX98:
2663             error_at (location,
2664                       "%<%E::%E%> is not a class, namespace, or enumeration",
2665                       parser->scope, name);
2666             break;
2667           default:
2668             gcc_unreachable ();
2669             
2670         }
2671     }
2672   else if (parser->scope == global_namespace)
2673     {
2674       switch (desired)
2675         {
2676           case NLE_TYPE:
2677             error_at (location, "%<::%E%> is not a type", name);
2678             break;
2679           case NLE_CXX98:
2680             error_at (location, "%<::%E%> is not a class or namespace", name);
2681             break;
2682           case NLE_NOT_CXX98:
2683             error_at (location,
2684                       "%<::%E%> is not a class, namespace, or enumeration",
2685                       name);
2686             break;
2687           default:
2688             gcc_unreachable ();
2689         }
2690     }
2691   else
2692     {
2693       switch (desired)
2694         {
2695           case NLE_TYPE:
2696             error_at (location, "%qE is not a type", name);
2697             break;
2698           case NLE_CXX98:
2699             error_at (location, "%qE is not a class or namespace", name);
2700             break;
2701           case NLE_NOT_CXX98:
2702             error_at (location,
2703                       "%qE is not a class, namespace, or enumeration", name);
2704             break;
2705           default:
2706             gcc_unreachable ();
2707         }
2708     }
2709 }
2710
2711 /* If we are parsing tentatively, remember that an error has occurred
2712    during this tentative parse.  Returns true if the error was
2713    simulated; false if a message should be issued by the caller.  */
2714
2715 static bool
2716 cp_parser_simulate_error (cp_parser* parser)
2717 {
2718   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2719     {
2720       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2721       return true;
2722     }
2723   return false;
2724 }
2725
2726 /* This function is called when a type is defined.  If type
2727    definitions are forbidden at this point, an error message is
2728    issued.  */
2729
2730 static bool
2731 cp_parser_check_type_definition (cp_parser* parser)
2732 {
2733   /* If types are forbidden here, issue a message.  */
2734   if (parser->type_definition_forbidden_message)
2735     {
2736       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2737          in the message need to be interpreted.  */
2738       error (parser->type_definition_forbidden_message);
2739       return false;
2740     }
2741   return true;
2742 }
2743
2744 /* This function is called when the DECLARATOR is processed.  The TYPE
2745    was a type defined in the decl-specifiers.  If it is invalid to
2746    define a type in the decl-specifiers for DECLARATOR, an error is
2747    issued. TYPE_LOCATION is the location of TYPE and is used
2748    for error reporting.  */
2749
2750 static void
2751 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2752                                                tree type, location_t type_location)
2753 {
2754   /* [dcl.fct] forbids type definitions in return types.
2755      Unfortunately, it's not easy to know whether or not we are
2756      processing a return type until after the fact.  */
2757   while (declarator
2758          && (declarator->kind == cdk_pointer
2759              || declarator->kind == cdk_reference
2760              || declarator->kind == cdk_ptrmem))
2761     declarator = declarator->declarator;
2762   if (declarator
2763       && declarator->kind == cdk_function)
2764     {
2765       error_at (type_location,
2766                 "new types may not be defined in a return type");
2767       inform (type_location, 
2768               "(perhaps a semicolon is missing after the definition of %qT)",
2769               type);
2770     }
2771 }
2772
2773 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2774    "<" in any valid C++ program.  If the next token is indeed "<",
2775    issue a message warning the user about what appears to be an
2776    invalid attempt to form a template-id. LOCATION is the location
2777    of the type-specifier (TYPE) */
2778
2779 static void
2780 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2781                                          tree type,
2782                                          enum tag_types tag_type,
2783                                          location_t location)
2784 {
2785   cp_token_position start = 0;
2786
2787   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2788     {
2789       if (TYPE_P (type))
2790         error_at (location, "%qT is not a template", type);
2791       else if (identifier_p (type))
2792         {
2793           if (tag_type != none_type)
2794             error_at (location, "%qE is not a class template", type);
2795           else
2796             error_at (location, "%qE is not a template", type);
2797         }
2798       else
2799         error_at (location, "invalid template-id");
2800       /* Remember the location of the invalid "<".  */
2801       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2802         start = cp_lexer_token_position (parser->lexer, true);
2803       /* Consume the "<".  */
2804       cp_lexer_consume_token (parser->lexer);
2805       /* Parse the template arguments.  */
2806       cp_parser_enclosed_template_argument_list (parser);
2807       /* Permanently remove the invalid template arguments so that
2808          this error message is not issued again.  */
2809       if (start)
2810         cp_lexer_purge_tokens_after (parser->lexer, start);
2811     }
2812 }
2813
2814 /* If parsing an integral constant-expression, issue an error message
2815    about the fact that THING appeared and return true.  Otherwise,
2816    return false.  In either case, set
2817    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2818
2819 static bool
2820 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2821                                             non_integral_constant thing)
2822 {
2823   parser->non_integral_constant_expression_p = true;
2824   if (parser->integral_constant_expression_p)
2825     {
2826       if (!parser->allow_non_integral_constant_expression_p)
2827         {
2828           const char *msg = NULL;
2829           switch (thing)
2830             {
2831               case NIC_FLOAT:
2832                 error ("floating-point literal "
2833                        "cannot appear in a constant-expression");
2834                 return true;
2835               case NIC_CAST:
2836                 error ("a cast to a type other than an integral or "
2837                        "enumeration type cannot appear in a "
2838                        "constant-expression");
2839                 return true;
2840               case NIC_TYPEID:
2841                 error ("%<typeid%> operator "
2842                        "cannot appear in a constant-expression");
2843                 return true;
2844               case NIC_NCC:
2845                 error ("non-constant compound literals "
2846                        "cannot appear in a constant-expression");
2847                 return true;
2848               case NIC_FUNC_CALL:
2849                 error ("a function call "
2850                        "cannot appear in a constant-expression");
2851                 return true;
2852               case NIC_INC:
2853                 error ("an increment "
2854                        "cannot appear in a constant-expression");
2855                 return true;
2856               case NIC_DEC:
2857                 error ("an decrement "
2858                        "cannot appear in a constant-expression");
2859                 return true;
2860               case NIC_ARRAY_REF:
2861                 error ("an array reference "
2862                        "cannot appear in a constant-expression");
2863                 return true;
2864               case NIC_ADDR_LABEL:
2865                 error ("the address of a label "
2866                        "cannot appear in a constant-expression");
2867                 return true;
2868               case NIC_OVERLOADED:
2869                 error ("calls to overloaded operators "
2870                        "cannot appear in a constant-expression");
2871                 return true;
2872               case NIC_ASSIGNMENT:
2873                 error ("an assignment cannot appear in a constant-expression");
2874                 return true;
2875               case NIC_COMMA:
2876                 error ("a comma operator "
2877                        "cannot appear in a constant-expression");
2878                 return true;
2879               case NIC_CONSTRUCTOR:
2880                 error ("a call to a constructor "
2881                        "cannot appear in a constant-expression");
2882                 return true;
2883               case NIC_TRANSACTION:
2884                 error ("a transaction expression "
2885                        "cannot appear in a constant-expression");
2886                 return true;
2887               case NIC_THIS:
2888                 msg = "this";
2889                 break;
2890               case NIC_FUNC_NAME:
2891                 msg = "__FUNCTION__";
2892                 break;
2893               case NIC_PRETTY_FUNC:
2894                 msg = "__PRETTY_FUNCTION__";
2895                 break;
2896               case NIC_C99_FUNC:
2897                 msg = "__func__";
2898                 break;
2899               case NIC_VA_ARG:
2900                 msg = "va_arg";
2901                 break;
2902               case NIC_ARROW:
2903                 msg = "->";
2904                 break;
2905               case NIC_POINT:
2906                 msg = ".";
2907                 break;
2908               case NIC_STAR:
2909                 msg = "*";
2910                 break;
2911               case NIC_ADDR:
2912                 msg = "&";
2913                 break;
2914               case NIC_PREINCREMENT:
2915                 msg = "++";
2916                 break;
2917               case NIC_PREDECREMENT:
2918                 msg = "--";
2919                 break;
2920               case NIC_NEW:
2921                 msg = "new";
2922                 break;
2923               case NIC_DEL:
2924                 msg = "delete";
2925                 break;
2926               default:
2927                 gcc_unreachable ();
2928             }
2929           if (msg)
2930             error ("%qs cannot appear in a constant-expression", msg);
2931           return true;
2932         }
2933     }
2934   return false;
2935 }
2936
2937 /* Emit a diagnostic for an invalid type name.  This function commits
2938    to the current active tentative parse, if any.  (Otherwise, the
2939    problematic construct might be encountered again later, resulting
2940    in duplicate error messages.) LOCATION is the location of ID.  */
2941
2942 static void
2943 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2944                                       location_t location)
2945 {
2946   tree decl, ambiguous_decls;
2947   cp_parser_commit_to_tentative_parse (parser);
2948   /* Try to lookup the identifier.  */
2949   decl = cp_parser_lookup_name (parser, id, none_type,
2950                                 /*is_template=*/false,
2951                                 /*is_namespace=*/false,
2952                                 /*check_dependency=*/true,
2953                                 &ambiguous_decls, location);
2954   if (ambiguous_decls)
2955     /* If the lookup was ambiguous, an error will already have
2956        been issued.  */
2957     return;
2958   /* If the lookup found a template-name, it means that the user forgot
2959   to specify an argument list. Emit a useful error message.  */
2960   if (TREE_CODE (decl) == TEMPLATE_DECL)
2961     error_at (location,
2962               "invalid use of template-name %qE without an argument list",
2963               decl);
2964   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2965     error_at (location, "invalid use of destructor %qD as a type", id);
2966   else if (TREE_CODE (decl) == TYPE_DECL)
2967     /* Something like 'unsigned A a;'  */
2968     error_at (location, "invalid combination of multiple type-specifiers");
2969   else if (!parser->scope)
2970     {
2971       /* Issue an error message.  */
2972       error_at (location, "%qE does not name a type", id);
2973       /* If we're in a template class, it's possible that the user was
2974          referring to a type from a base class.  For example:
2975
2976            template <typename T> struct A { typedef T X; };
2977            template <typename T> struct B : public A<T> { X x; };
2978
2979          The user should have said "typename A<T>::X".  */
2980       if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2981         inform (location, "C++11 %<constexpr%> only available with "
2982                 "-std=c++11 or -std=gnu++11");
2983       else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2984         inform (location, "C++11 %<noexcept%> only available with "
2985                 "-std=c++11 or -std=gnu++11");
2986       else if (cxx_dialect < cxx11
2987                && TREE_CODE (id) == IDENTIFIER_NODE
2988                && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2989         inform (location, "C++11 %<thread_local%> only available with "
2990                 "-std=c++11 or -std=gnu++11");
2991       else if (processing_template_decl && current_class_type
2992                && TYPE_BINFO (current_class_type))
2993         {
2994           tree b;
2995
2996           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2997                b;
2998                b = TREE_CHAIN (b))
2999             {
3000               tree base_type = BINFO_TYPE (b);
3001               if (CLASS_TYPE_P (base_type)
3002                   && dependent_type_p (base_type))
3003                 {
3004                   tree field;
3005                   /* Go from a particular instantiation of the
3006                      template (which will have an empty TYPE_FIELDs),
3007                      to the main version.  */
3008                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3009                   for (field = TYPE_FIELDS (base_type);
3010                        field;
3011                        field = DECL_CHAIN (field))
3012                     if (TREE_CODE (field) == TYPE_DECL
3013                         && DECL_NAME (field) == id)
3014                       {
3015                         inform (location, 
3016                                 "(perhaps %<typename %T::%E%> was intended)",
3017                                 BINFO_TYPE (b), id);
3018                         break;
3019                       }
3020                   if (field)
3021                     break;
3022                 }
3023             }
3024         }
3025     }
3026   /* Here we diagnose qualified-ids where the scope is actually correct,
3027      but the identifier does not resolve to a valid type name.  */
3028   else if (parser->scope != error_mark_node)
3029     {
3030       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3031         {
3032           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3033             error_at (location_of (id),
3034                       "%qE in namespace %qE does not name a template type",
3035                       id, parser->scope);
3036           else
3037             error_at (location_of (id),
3038                       "%qE in namespace %qE does not name a type",
3039                       id, parser->scope);
3040         }
3041       else if (CLASS_TYPE_P (parser->scope)
3042                && constructor_name_p (id, parser->scope))
3043         {
3044           /* A<T>::A<T>() */
3045           error_at (location, "%<%T::%E%> names the constructor, not"
3046                     " the type", parser->scope, id);
3047           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3048             error_at (location, "and %qT has no template constructors",
3049                       parser->scope);
3050         }
3051       else if (TYPE_P (parser->scope)
3052                && dependent_scope_p (parser->scope))
3053         error_at (location, "need %<typename%> before %<%T::%E%> because "
3054                   "%qT is a dependent scope",
3055                   parser->scope, id, parser->scope);
3056       else if (TYPE_P (parser->scope))
3057         {
3058           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3059             error_at (location_of (id),
3060                       "%qE in %q#T does not name a template type",
3061                       id, parser->scope);
3062           else
3063             error_at (location_of (id),
3064                       "%qE in %q#T does not name a type",
3065                       id, parser->scope);
3066         }
3067       else
3068         gcc_unreachable ();
3069     }
3070 }
3071
3072 /* Check for a common situation where a type-name should be present,
3073    but is not, and issue a sensible error message.  Returns true if an
3074    invalid type-name was detected.
3075
3076    The situation handled by this function are variable declarations of the
3077    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3078    Usually, `ID' should name a type, but if we got here it means that it
3079    does not. We try to emit the best possible error message depending on
3080    how exactly the id-expression looks like.  */
3081
3082 static bool
3083 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3084 {
3085   tree id;
3086   cp_token *token = cp_lexer_peek_token (parser->lexer);
3087
3088   /* Avoid duplicate error about ambiguous lookup.  */
3089   if (token->type == CPP_NESTED_NAME_SPECIFIER)
3090     {
3091       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3092       if (next->type == CPP_NAME && next->error_reported)
3093         goto out;
3094     }
3095
3096   cp_parser_parse_tentatively (parser);
3097   id = cp_parser_id_expression (parser,
3098                                 /*template_keyword_p=*/false,
3099                                 /*check_dependency_p=*/true,
3100                                 /*template_p=*/NULL,
3101                                 /*declarator_p=*/true,
3102                                 /*optional_p=*/false);
3103   /* If the next token is a (, this is a function with no explicit return
3104      type, i.e. constructor, destructor or conversion op.  */
3105   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3106       || TREE_CODE (id) == TYPE_DECL)
3107     {
3108       cp_parser_abort_tentative_parse (parser);
3109       return false;
3110     }
3111   if (!cp_parser_parse_definitely (parser))
3112     return false;
3113
3114   /* Emit a diagnostic for the invalid type.  */
3115   cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3116  out:
3117   /* If we aren't in the middle of a declarator (i.e. in a
3118      parameter-declaration-clause), skip to the end of the declaration;
3119      there's no point in trying to process it.  */
3120   if (!parser->in_declarator_p)
3121     cp_parser_skip_to_end_of_block_or_statement (parser);
3122   return true;
3123 }
3124
3125 /* Consume tokens up to, and including, the next non-nested closing `)'.
3126    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
3127    are doing error recovery. Returns -1 if OR_COMMA is true and we
3128    found an unnested comma.  */
3129
3130 static int
3131 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3132                                        bool recovering,
3133                                        bool or_comma,
3134                                        bool consume_paren)
3135 {
3136   unsigned paren_depth = 0;
3137   unsigned brace_depth = 0;
3138   unsigned square_depth = 0;
3139
3140   if (recovering && !or_comma
3141       && cp_parser_uncommitted_to_tentative_parse_p (parser))
3142     return 0;
3143
3144   while (true)
3145     {
3146       cp_token * token = cp_lexer_peek_token (parser->lexer);
3147
3148       switch (token->type)
3149         {
3150         case CPP_EOF:
3151         case CPP_PRAGMA_EOL:
3152           /* If we've run out of tokens, then there is no closing `)'.  */
3153           return 0;
3154
3155         /* This is good for lambda expression capture-lists.  */
3156         case CPP_OPEN_SQUARE:
3157           ++square_depth;
3158           break;
3159         case CPP_CLOSE_SQUARE:
3160           if (!square_depth--)
3161             return 0;
3162           break;
3163
3164         case CPP_SEMICOLON:
3165           /* This matches the processing in skip_to_end_of_statement.  */
3166           if (!brace_depth)
3167             return 0;
3168           break;
3169
3170         case CPP_OPEN_BRACE:
3171           ++brace_depth;
3172           break;
3173         case CPP_CLOSE_BRACE:
3174           if (!brace_depth--)
3175             return 0;
3176           break;
3177
3178         case CPP_COMMA:
3179           if (recovering && or_comma && !brace_depth && !paren_depth
3180               && !square_depth)
3181             return -1;
3182           break;
3183
3184         case CPP_OPEN_PAREN:
3185           if (!brace_depth)
3186             ++paren_depth;
3187           break;
3188
3189         case CPP_CLOSE_PAREN:
3190           if (!brace_depth && !paren_depth--)
3191             {
3192               if (consume_paren)
3193                 cp_lexer_consume_token (parser->lexer);
3194               return 1;
3195             }
3196           break;
3197
3198         default:
3199           break;
3200         }
3201
3202       /* Consume the token.  */
3203       cp_lexer_consume_token (parser->lexer);
3204     }
3205 }
3206
3207 /* Consume tokens until we reach the end of the current statement.
3208    Normally, that will be just before consuming a `;'.  However, if a
3209    non-nested `}' comes first, then we stop before consuming that.  */
3210
3211 static void
3212 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3213 {
3214   unsigned nesting_depth = 0;
3215
3216   /* Unwind generic function template scope if necessary.  */
3217   if (parser->fully_implicit_function_template_p)
3218     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3219
3220   while (true)
3221     {
3222       cp_token *token = cp_lexer_peek_token (parser->lexer);
3223
3224       switch (token->type)
3225         {
3226         case CPP_EOF:
3227         case CPP_PRAGMA_EOL:
3228           /* If we've run out of tokens, stop.  */
3229           return;
3230
3231         case CPP_SEMICOLON:
3232           /* If the next token is a `;', we have reached the end of the
3233              statement.  */
3234           if (!nesting_depth)
3235             return;
3236           break;
3237
3238         case CPP_CLOSE_BRACE:
3239           /* If this is a non-nested '}', stop before consuming it.
3240              That way, when confronted with something like:
3241
3242                { 3 + }
3243
3244              we stop before consuming the closing '}', even though we
3245              have not yet reached a `;'.  */
3246           if (nesting_depth == 0)
3247             return;
3248
3249           /* If it is the closing '}' for a block that we have
3250              scanned, stop -- but only after consuming the token.
3251              That way given:
3252
3253                 void f g () { ... }
3254                 typedef int I;
3255
3256              we will stop after the body of the erroneously declared
3257              function, but before consuming the following `typedef'
3258              declaration.  */
3259           if (--nesting_depth == 0)
3260             {
3261               cp_lexer_consume_token (parser->lexer);
3262               return;
3263             }
3264
3265         case CPP_OPEN_BRACE:
3266           ++nesting_depth;
3267           break;
3268
3269         default:
3270           break;
3271         }
3272
3273       /* Consume the token.  */
3274       cp_lexer_consume_token (parser->lexer);
3275     }
3276 }
3277
3278 /* This function is called at the end of a statement or declaration.
3279    If the next token is a semicolon, it is consumed; otherwise, error
3280    recovery is attempted.  */
3281
3282 static void
3283 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3284 {
3285   /* Look for the trailing `;'.  */
3286   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3287     {
3288       /* If there is additional (erroneous) input, skip to the end of
3289          the statement.  */
3290       cp_parser_skip_to_end_of_statement (parser);
3291       /* If the next token is now a `;', consume it.  */
3292       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3293         cp_lexer_consume_token (parser->lexer);
3294     }
3295 }
3296
3297 /* Skip tokens until we have consumed an entire block, or until we
3298    have consumed a non-nested `;'.  */
3299
3300 static void
3301 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3302 {
3303   int nesting_depth = 0;
3304
3305   /* Unwind generic function template scope if necessary.  */
3306   if (parser->fully_implicit_function_template_p)
3307     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3308
3309   while (nesting_depth >= 0)
3310     {
3311       cp_token *token = cp_lexer_peek_token (parser->lexer);
3312
3313       switch (token->type)
3314         {
3315         case CPP_EOF:
3316         case CPP_PRAGMA_EOL:
3317           /* If we've run out of tokens, stop.  */
3318           return;
3319
3320         case CPP_SEMICOLON:
3321           /* Stop if this is an unnested ';'. */
3322           if (!nesting_depth)
3323             nesting_depth = -1;
3324           break;
3325
3326         case CPP_CLOSE_BRACE:
3327           /* Stop if this is an unnested '}', or closes the outermost
3328              nesting level.  */
3329           nesting_depth--;
3330           if (nesting_depth < 0)
3331             return;
3332           if (!nesting_depth)
3333             nesting_depth = -1;
3334           break;
3335
3336         case CPP_OPEN_BRACE:
3337           /* Nest. */
3338           nesting_depth++;
3339           break;
3340
3341         default:
3342           break;
3343         }
3344
3345       /* Consume the token.  */
3346       cp_lexer_consume_token (parser->lexer);
3347     }
3348 }
3349
3350 /* Skip tokens until a non-nested closing curly brace is the next
3351    token, or there are no more tokens. Return true in the first case,
3352    false otherwise.  */
3353
3354 static bool
3355 cp_parser_skip_to_closing_brace (cp_parser *parser)
3356 {
3357   unsigned nesting_depth = 0;
3358
3359   while (true)
3360     {
3361       cp_token *token = cp_lexer_peek_token (parser->lexer);
3362
3363       switch (token->type)
3364         {
3365         case CPP_EOF:
3366         case CPP_PRAGMA_EOL:
3367           /* If we've run out of tokens, stop.  */
3368           return false;
3369
3370         case CPP_CLOSE_BRACE:
3371           /* If the next token is a non-nested `}', then we have reached
3372              the end of the current block.  */
3373           if (nesting_depth-- == 0)
3374             return true;
3375           break;
3376
3377         case CPP_OPEN_BRACE:
3378           /* If it the next token is a `{', then we are entering a new
3379              block.  Consume the entire block.  */
3380           ++nesting_depth;
3381           break;
3382
3383         default:
3384           break;
3385         }
3386
3387       /* Consume the token.  */
3388       cp_lexer_consume_token (parser->lexer);
3389     }
3390 }
3391
3392 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3393    parameter is the PRAGMA token, allowing us to purge the entire pragma
3394    sequence.  */
3395
3396 static void
3397 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3398 {
3399   cp_token *token;
3400
3401   parser->lexer->in_pragma = false;
3402
3403   do
3404     token = cp_lexer_consume_token (parser->lexer);
3405   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3406
3407   /* Ensure that the pragma is not parsed again.  */
3408   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3409 }
3410
3411 /* Require pragma end of line, resyncing with it as necessary.  The
3412    arguments are as for cp_parser_skip_to_pragma_eol.  */
3413
3414 static void
3415 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3416 {
3417   parser->lexer->in_pragma = false;
3418   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3419     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3420 }
3421
3422 /* This is a simple wrapper around make_typename_type. When the id is
3423    an unresolved identifier node, we can provide a superior diagnostic
3424    using cp_parser_diagnose_invalid_type_name.  */
3425
3426 static tree
3427 cp_parser_make_typename_type (cp_parser *parser, tree id,
3428                               location_t id_location)
3429 {
3430   tree result;
3431   if (identifier_p (id))
3432     {
3433       result = make_typename_type (parser->scope, id, typename_type,
3434                                    /*complain=*/tf_none);
3435       if (result == error_mark_node)
3436         cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3437       return result;
3438     }
3439   return make_typename_type (parser->scope, id, typename_type, tf_error);
3440 }
3441
3442 /* This is a wrapper around the
3443    make_{pointer,ptrmem,reference}_declarator functions that decides
3444    which one to call based on the CODE and CLASS_TYPE arguments. The
3445    CODE argument should be one of the values returned by
3446    cp_parser_ptr_operator.  ATTRIBUTES represent the attributes that
3447    appertain to the pointer or reference.  */
3448
3449 static cp_declarator *
3450 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3451                                     cp_cv_quals cv_qualifiers,
3452                                     cp_declarator *target,
3453                                     tree attributes)
3454 {
3455   if (code == ERROR_MARK)
3456     return cp_error_declarator;
3457
3458   if (code == INDIRECT_REF)
3459     if (class_type == NULL_TREE)
3460       return make_pointer_declarator (cv_qualifiers, target, attributes);
3461     else
3462       return make_ptrmem_declarator (cv_qualifiers, class_type,
3463                                      target, attributes);
3464   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3465     return make_reference_declarator (cv_qualifiers, target,
3466                                       false, attributes);
3467   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3468     return make_reference_declarator (cv_qualifiers, target,
3469                                       true, attributes);
3470   gcc_unreachable ();
3471 }
3472
3473 /* Create a new C++ parser.  */
3474
3475 static cp_parser *
3476 cp_parser_new (void)
3477 {
3478   cp_parser *parser;
3479   cp_lexer *lexer;
3480   unsigned i;
3481
3482   /* cp_lexer_new_main is called before doing GC allocation because
3483      cp_lexer_new_main might load a PCH file.  */
3484   lexer = cp_lexer_new_main ();
3485
3486   /* Initialize the binops_by_token so that we can get the tree
3487      directly from the token.  */
3488   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3489     binops_by_token[binops[i].token_type] = binops[i];
3490
3491   parser = ggc_cleared_alloc<cp_parser> ();
3492   parser->lexer = lexer;
3493   parser->context = cp_parser_context_new (NULL);
3494
3495   /* For now, we always accept GNU extensions.  */
3496   parser->allow_gnu_extensions_p = 1;
3497
3498   /* The `>' token is a greater-than operator, not the end of a
3499      template-id.  */
3500   parser->greater_than_is_operator_p = true;
3501
3502   parser->default_arg_ok_p = true;
3503
3504   /* We are not parsing a constant-expression.  */
3505   parser->integral_constant_expression_p = false;
3506   parser->allow_non_integral_constant_expression_p = false;
3507   parser->non_integral_constant_expression_p = false;
3508
3509   /* Local variable names are not forbidden.  */
3510   parser->local_variables_forbidden_p = false;
3511
3512   /* We are not processing an `extern "C"' declaration.  */
3513   parser->in_unbraced_linkage_specification_p = false;
3514
3515   /* We are not processing a declarator.  */
3516   parser->in_declarator_p = false;
3517
3518   /* We are not processing a template-argument-list.  */
3519   parser->in_template_argument_list_p = false;
3520
3521   /* We are not in an iteration statement.  */
3522   parser->in_statement = 0;
3523
3524   /* We are not in a switch statement.  */
3525   parser->in_switch_statement_p = false;
3526
3527   /* We are not parsing a type-id inside an expression.  */
3528   parser->in_type_id_in_expr_p = false;
3529
3530   /* Declarations aren't implicitly extern "C".  */
3531   parser->implicit_extern_c = false;
3532
3533   /* String literals should be translated to the execution character set.  */
3534   parser->translate_strings_p = true;
3535
3536   /* We are not parsing a function body.  */
3537   parser->in_function_body = false;
3538
3539   /* We can correct until told otherwise.  */
3540   parser->colon_corrects_to_scope_p = true;
3541
3542   /* The unparsed function queue is empty.  */
3543   push_unparsed_function_queues (parser);
3544
3545   /* There are no classes being defined.  */
3546   parser->num_classes_being_defined = 0;
3547
3548   /* No template parameters apply.  */
3549   parser->num_template_parameter_lists = 0;
3550
3551   /* Not declaring an implicit function template.  */
3552   parser->auto_is_implicit_function_template_parm_p = false;
3553   parser->fully_implicit_function_template_p = false;
3554   parser->implicit_template_parms = 0;
3555   parser->implicit_template_scope = 0;
3556
3557   return parser;
3558 }
3559
3560 /* Create a cp_lexer structure which will emit the tokens in CACHE
3561    and push it onto the parser's lexer stack.  This is used for delayed
3562    parsing of in-class method bodies and default arguments, and should
3563    not be confused with tentative parsing.  */
3564 static void
3565 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3566 {
3567   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3568   lexer->next = parser->lexer;
3569   parser->lexer = lexer;
3570
3571   /* Move the current source position to that of the first token in the
3572      new lexer.  */
3573   cp_lexer_set_source_position_from_token (lexer->next_token);
3574 }
3575
3576 /* Pop the top lexer off the parser stack.  This is never used for the
3577    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3578 static void
3579 cp_parser_pop_lexer (cp_parser *parser)
3580 {
3581   cp_lexer *lexer = parser->lexer;
3582   parser->lexer = lexer->next;
3583   cp_lexer_destroy (lexer);
3584
3585   /* Put the current source position back where it was before this
3586      lexer was pushed.  */
3587   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3588 }
3589
3590 /* Lexical conventions [gram.lex]  */
3591
3592 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3593    identifier.  */
3594
3595 static tree
3596 cp_parser_identifier (cp_parser* parser)
3597 {
3598   cp_token *token;
3599
3600   /* Look for the identifier.  */
3601   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3602   /* Return the value.  */
3603   return token ? token->u.value : error_mark_node;
3604 }
3605
3606 /* Parse a sequence of adjacent string constants.  Returns a
3607    TREE_STRING representing the combined, nul-terminated string
3608    constant.  If TRANSLATE is true, translate the string to the
3609    execution character set.  If WIDE_OK is true, a wide string is
3610    invalid here.
3611
3612    C++98 [lex.string] says that if a narrow string literal token is
3613    adjacent to a wide string literal token, the behavior is undefined.
3614    However, C99 6.4.5p4 says that this results in a wide string literal.
3615    We follow C99 here, for consistency with the C front end.
3616
3617    This code is largely lifted from lex_string() in c-lex.c.
3618
3619    FUTURE: ObjC++ will need to handle @-strings here.  */
3620 static tree
3621 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3622                           bool lookup_udlit = true)
3623 {
3624   tree value;
3625   size_t count;
3626   struct obstack str_ob;
3627   cpp_string str, istr, *strs;
3628   cp_token *tok;
3629   enum cpp_ttype type, curr_type;
3630   int have_suffix_p = 0;
3631   tree string_tree;
3632   tree suffix_id = NULL_TREE;
3633   bool curr_tok_is_userdef_p = false;
3634
3635   tok = cp_lexer_peek_token (parser->lexer);
3636   if (!cp_parser_is_string_literal (tok))
3637     {
3638       cp_parser_error (parser, "expected string-literal");
3639       return error_mark_node;
3640     }
3641
3642   if (cpp_userdef_string_p (tok->type))
3643     {
3644       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3645       curr_type = cpp_userdef_string_remove_type (tok->type);
3646       curr_tok_is_userdef_p = true;
3647     }
3648   else
3649     {
3650       string_tree = tok->u.value;
3651       curr_type = tok->type;
3652     }
3653   type = curr_type;
3654
3655   /* Try to avoid the overhead of creating and destroying an obstack
3656      for the common case of just one string.  */
3657   if (!cp_parser_is_string_literal
3658       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3659     {
3660       cp_lexer_consume_token (parser->lexer);
3661
3662       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3663       str.len = TREE_STRING_LENGTH (string_tree);
3664       count = 1;
3665
3666       if (curr_tok_is_userdef_p)
3667         {
3668           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3669           have_suffix_p = 1;
3670           curr_type = cpp_userdef_string_remove_type (tok->type);
3671         }
3672       else
3673         curr_type = tok->type;
3674
3675       strs = &str;
3676     }
3677   else
3678     {
3679       gcc_obstack_init (&str_ob);
3680       count = 0;
3681
3682       do
3683         {
3684           cp_lexer_consume_token (parser->lexer);
3685           count++;
3686           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3687           str.len = TREE_STRING_LENGTH (string_tree);
3688
3689           if (curr_tok_is_userdef_p)
3690             {
3691               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3692               if (have_suffix_p == 0)
3693                 {
3694                   suffix_id = curr_suffix_id;
3695                   have_suffix_p = 1;
3696                 }
3697               else if (have_suffix_p == 1
3698                        && curr_suffix_id != suffix_id)
3699                 {
3700                   error ("inconsistent user-defined literal suffixes"
3701                          " %qD and %qD in string literal",
3702                          suffix_id, curr_suffix_id);
3703                   have_suffix_p = -1;
3704                 }
3705               curr_type = cpp_userdef_string_remove_type (tok->type);
3706             }
3707           else
3708             curr_type = tok->type;
3709
3710           if (type != curr_type)
3711             {
3712               if (type == CPP_STRING)
3713                 type = curr_type;
3714               else if (curr_type != CPP_STRING)
3715                 error_at (tok->location,
3716                           "unsupported non-standard concatenation "
3717                           "of string literals");
3718             }
3719
3720           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3721
3722           tok = cp_lexer_peek_token (parser->lexer);
3723           if (cpp_userdef_string_p (tok->type))
3724             {
3725               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3726               curr_type = cpp_userdef_string_remove_type (tok->type);
3727               curr_tok_is_userdef_p = true;
3728             }
3729           else
3730             {
3731               string_tree = tok->u.value;
3732               curr_type = tok->type;
3733               curr_tok_is_userdef_p = false;
3734             }
3735         }
3736       while (cp_parser_is_string_literal (tok));
3737
3738       strs = (cpp_string *) obstack_finish (&str_ob);
3739     }
3740
3741   if (type != CPP_STRING && !wide_ok)
3742     {
3743       cp_parser_error (parser, "a wide string is invalid in this context");
3744       type = CPP_STRING;
3745     }
3746
3747   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3748       (parse_in, strs, count, &istr, type))
3749     {
3750       value = build_string (istr.len, (const char *)istr.text);
3751       free (CONST_CAST (unsigned char *, istr.text));
3752
3753       switch (type)
3754         {
3755         default:
3756         case CPP_STRING:
3757         case CPP_UTF8STRING:
3758           TREE_TYPE (value) = char_array_type_node;
3759           break;
3760         case CPP_STRING16:
3761           TREE_TYPE (value) = char16_array_type_node;
3762           break;
3763         case CPP_STRING32:
3764           TREE_TYPE (value) = char32_array_type_node;
3765           break;
3766         case CPP_WSTRING:
3767           TREE_TYPE (value) = wchar_array_type_node;
3768           break;
3769         }
3770
3771       value = fix_string_type (value);
3772
3773       if (have_suffix_p)
3774         {
3775           tree literal = build_userdef_literal (suffix_id, value,
3776                                                 OT_NONE, NULL_TREE);
3777           if (lookup_udlit)
3778             value = cp_parser_userdef_string_literal (literal);
3779           else
3780             value = literal;
3781         }
3782     }
3783   else
3784     /* cpp_interpret_string has issued an error.  */
3785     value = error_mark_node;
3786
3787   if (count > 1)
3788     obstack_free (&str_ob, 0);
3789
3790   return value;
3791 }
3792
3793 /* Look up a literal operator with the name and the exact arguments.  */
3794
3795 static tree
3796 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3797 {
3798   tree decl, fns;
3799   decl = lookup_name (name);
3800   if (!decl || !is_overloaded_fn (decl))
3801     return error_mark_node;
3802
3803   for (fns = decl; fns; fns = OVL_NEXT (fns))
3804     {
3805       unsigned int ix;
3806       bool found = true;
3807       tree fn = OVL_CURRENT (fns);
3808       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3809       if (parmtypes != NULL_TREE)
3810         {
3811           for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3812                ++ix, parmtypes = TREE_CHAIN (parmtypes))
3813             {
3814               tree tparm = TREE_VALUE (parmtypes);
3815               tree targ = TREE_TYPE ((*args)[ix]);
3816               bool ptr = TYPE_PTR_P (tparm);
3817               bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3818               if ((ptr || arr || !same_type_p (tparm, targ))
3819                   && (!ptr || !arr
3820                       || !same_type_p (TREE_TYPE (tparm),
3821                                        TREE_TYPE (targ))))
3822                 found = false;
3823             }
3824           if (found
3825               && ix == vec_safe_length (args)
3826               /* May be this should be sufficient_parms_p instead,
3827                  depending on how exactly should user-defined literals
3828                  work in presence of default arguments on the literal
3829                  operator parameters.  */
3830               && parmtypes == void_list_node)
3831             return decl;
3832         }
3833     }
3834
3835   return error_mark_node;
3836 }
3837
3838 /* Parse a user-defined char constant.  Returns a call to a user-defined
3839    literal operator taking the character as an argument.  */
3840
3841 static tree
3842 cp_parser_userdef_char_literal (cp_parser *parser)
3843 {
3844   cp_token *token = cp_lexer_consume_token (parser->lexer);
3845   tree literal = token->u.value;
3846   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3847   tree value = USERDEF_LITERAL_VALUE (literal);
3848   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3849   tree decl, result;
3850
3851   /* Build up a call to the user-defined operator  */
3852   /* Lookup the name we got back from the id-expression.  */
3853   vec<tree, va_gc> *args = make_tree_vector ();
3854   vec_safe_push (args, value);
3855   decl = lookup_literal_operator (name, args);
3856   if (!decl || decl == error_mark_node)
3857     {
3858       error ("unable to find character literal operator %qD with %qT argument",
3859              name, TREE_TYPE (value));
3860       release_tree_vector (args);
3861       return error_mark_node;
3862     }
3863   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3864   release_tree_vector (args);
3865   return result;
3866 }
3867
3868 /* A subroutine of cp_parser_userdef_numeric_literal to
3869    create a char... template parameter pack from a string node.  */
3870
3871 static tree
3872 make_char_string_pack (tree value)
3873 {
3874   tree charvec;
3875   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3876   const char *str = TREE_STRING_POINTER (value);
3877   int i, len = TREE_STRING_LENGTH (value) - 1;
3878   tree argvec = make_tree_vec (1);
3879
3880   /* Fill in CHARVEC with all of the parameters.  */
3881   charvec = make_tree_vec (len);
3882   for (i = 0; i < len; ++i)
3883     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3884
3885   /* Build the argument packs.  */
3886   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3887   TREE_TYPE (argpack) = char_type_node;
3888
3889   TREE_VEC_ELT (argvec, 0) = argpack;
3890
3891   return argvec;
3892 }
3893
3894 /* A subroutine of cp_parser_userdef_numeric_literal to
3895    create a char... template parameter pack from a string node.  */
3896
3897 static tree
3898 make_string_pack (tree value)
3899 {
3900   tree charvec;
3901   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3902   const unsigned char *str
3903     = (const unsigned char *) TREE_STRING_POINTER (value);
3904   int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3905   int len = TREE_STRING_LENGTH (value) / sz - 1;
3906   tree argvec = make_tree_vec (2);
3907
3908   tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3909   str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3910
3911   /* First template parm is character type.  */
3912   TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3913
3914   /* Fill in CHARVEC with all of the parameters.  */
3915   charvec = make_tree_vec (len);
3916   for (int i = 0; i < len; ++i)
3917     TREE_VEC_ELT (charvec, i)
3918       = double_int_to_tree (str_char_type_node,
3919                             double_int::from_buffer (str + i * sz, sz));
3920
3921   /* Build the argument packs.  */
3922   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3923   TREE_TYPE (argpack) = str_char_type_node;
3924
3925   TREE_VEC_ELT (argvec, 1) = argpack;
3926
3927   return argvec;
3928 }
3929
3930 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3931    literal operator.  */
3932
3933 static tree
3934 cp_parser_userdef_numeric_literal (cp_parser *parser)
3935 {
3936   cp_token *token = cp_lexer_consume_token (parser->lexer);
3937   tree literal = token->u.value;
3938   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3939   tree value = USERDEF_LITERAL_VALUE (literal);
3940   int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3941   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3942   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3943   tree decl, result;
3944   vec<tree, va_gc> *args;
3945
3946   /* Look for a literal operator taking the exact type of numeric argument
3947      as the literal value.  */
3948   args = make_tree_vector ();
3949   vec_safe_push (args, value);
3950   decl = lookup_literal_operator (name, args);
3951   if (decl && decl != error_mark_node)
3952     {
3953       result = finish_call_expr (decl, &args, false, true,
3954                                  tf_warning_or_error);
3955
3956       if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3957         {
3958           warning_at (token->location, OPT_Woverflow,
3959                       "integer literal exceeds range of %qT type",
3960                       long_long_unsigned_type_node);
3961         }
3962       else
3963         {
3964           if (overflow > 0)
3965             warning_at (token->location, OPT_Woverflow,
3966                         "floating literal exceeds range of %qT type",
3967                         long_double_type_node);
3968           else if (overflow < 0)
3969             warning_at (token->location, OPT_Woverflow,
3970                         "floating literal truncated to zero");
3971         }
3972
3973       release_tree_vector (args);
3974       return result;
3975     }
3976   release_tree_vector (args);
3977
3978   /* If the numeric argument didn't work, look for a raw literal
3979      operator taking a const char* argument consisting of the number
3980      in string format.  */
3981   args = make_tree_vector ();
3982   vec_safe_push (args, num_string);
3983   decl = lookup_literal_operator (name, args);
3984   if (decl && decl != error_mark_node)
3985     {
3986       result = finish_call_expr (decl, &args, false, true,
3987                                  tf_warning_or_error);
3988       release_tree_vector (args);
3989       return result;
3990     }
3991   release_tree_vector (args);
3992
3993   /* If the raw literal didn't work, look for a non-type template
3994      function with parameter pack char....  Call the function with
3995      template parameter characters representing the number.  */
3996   args = make_tree_vector ();
3997   decl = lookup_literal_operator (name, args);
3998   if (decl && decl != error_mark_node)
3999     {
4000       tree tmpl_args = make_char_string_pack (num_string);
4001       decl = lookup_template_function (decl, tmpl_args);
4002       result = finish_call_expr (decl, &args, false, true,
4003                                  tf_warning_or_error);
4004       release_tree_vector (args);
4005       return result;
4006     }
4007
4008   release_tree_vector (args);
4009
4010   error ("unable to find numeric literal operator %qD", name);
4011   if (!cpp_get_options (parse_in)->ext_numeric_literals)
4012     inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4013             "to enable more built-in suffixes");
4014   return error_mark_node;
4015 }
4016
4017 /* Parse a user-defined string constant.  Returns a call to a user-defined
4018    literal operator taking a character pointer and the length of the string
4019    as arguments.  */
4020
4021 static tree
4022 cp_parser_userdef_string_literal (tree literal)
4023 {
4024   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4025   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4026   tree value = USERDEF_LITERAL_VALUE (literal);
4027   int len = TREE_STRING_LENGTH (value)
4028         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4029   tree decl, result;
4030   vec<tree, va_gc> *args;
4031
4032   /* Build up a call to the user-defined operator.  */
4033   /* Lookup the name we got back from the id-expression.  */
4034   args = make_tree_vector ();
4035   vec_safe_push (args, value);
4036   vec_safe_push (args, build_int_cst (size_type_node, len));
4037   decl = lookup_literal_operator (name, args);
4038
4039   if (decl && decl != error_mark_node)
4040     {
4041       result = finish_call_expr (decl, &args, false, true,
4042                                  tf_warning_or_error);
4043       release_tree_vector (args);
4044       return result;
4045     }
4046   release_tree_vector (args);
4047
4048   /* Look for a template function with typename parameter CharT
4049      and parameter pack CharT...  Call the function with
4050      template parameter characters representing the string.  */
4051   args = make_tree_vector ();
4052   decl = lookup_literal_operator (name, args);
4053   if (decl && decl != error_mark_node)
4054     {
4055       tree tmpl_args = make_string_pack (value);
4056       decl = lookup_template_function (decl, tmpl_args);
4057       result = finish_call_expr (decl, &args, false, true,
4058                                  tf_warning_or_error);
4059       release_tree_vector (args);
4060       return result;
4061     }
4062   release_tree_vector (args);
4063
4064   error ("unable to find string literal operator %qD with %qT, %qT arguments",
4065          name, TREE_TYPE (value), size_type_node);
4066   return error_mark_node;
4067 }
4068
4069
4070 /* Basic concepts [gram.basic]  */
4071
4072 /* Parse a translation-unit.
4073
4074    translation-unit:
4075      declaration-seq [opt]
4076
4077    Returns TRUE if all went well.  */
4078
4079 static bool
4080 cp_parser_translation_unit (cp_parser* parser)
4081 {
4082   /* The address of the first non-permanent object on the declarator
4083      obstack.  */
4084   static void *declarator_obstack_base;
4085
4086   bool success;
4087
4088   /* Create the declarator obstack, if necessary.  */
4089   if (!cp_error_declarator)
4090     {
4091       gcc_obstack_init (&declarator_obstack);
4092       /* Create the error declarator.  */
4093       cp_error_declarator = make_declarator (cdk_error);
4094       /* Create the empty parameter list.  */
4095       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4096       /* Remember where the base of the declarator obstack lies.  */
4097       declarator_obstack_base = obstack_next_free (&declarator_obstack);
4098     }
4099
4100   cp_parser_declaration_seq_opt (parser);
4101
4102   /* If there are no tokens left then all went well.  */
4103   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4104     {
4105       /* Get rid of the token array; we don't need it any more.  */
4106       cp_lexer_destroy (parser->lexer);
4107       parser->lexer = NULL;
4108
4109       /* This file might have been a context that's implicitly extern
4110          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
4111       if (parser->implicit_extern_c)
4112         {
4113           pop_lang_context ();
4114           parser->implicit_extern_c = false;
4115         }
4116
4117       /* Finish up.  */
4118       finish_translation_unit ();
4119
4120       success = true;
4121     }
4122   else
4123     {
4124       cp_parser_error (parser, "expected declaration");
4125       success = false;
4126     }
4127
4128   /* Make sure the declarator obstack was fully cleaned up.  */
4129   gcc_assert (obstack_next_free (&declarator_obstack)
4130               == declarator_obstack_base);
4131
4132   /* All went well.  */
4133   return success;
4134 }
4135
4136 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4137    decltype context.  */
4138
4139 static inline tsubst_flags_t
4140 complain_flags (bool decltype_p)
4141 {
4142   tsubst_flags_t complain = tf_warning_or_error;
4143   if (decltype_p)
4144     complain |= tf_decltype;
4145   return complain;
4146 }
4147
4148 /* We're about to parse a collection of statements.  If we're currently
4149    parsing tentatively, set up a firewall so that any nested
4150    cp_parser_commit_to_tentative_parse won't affect the current context.  */
4151
4152 static cp_token_position
4153 cp_parser_start_tentative_firewall (cp_parser *parser)
4154 {
4155   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4156     return 0;
4157
4158   cp_parser_parse_tentatively (parser);
4159   cp_parser_commit_to_topmost_tentative_parse (parser);
4160   return cp_lexer_token_position (parser->lexer, false);
4161 }
4162
4163 /* We've finished parsing the collection of statements.  Wrap up the
4164    firewall and replace the relevant tokens with the parsed form.  */
4165
4166 static void
4167 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4168                                   tree expr)
4169 {
4170   if (!start)
4171     return;
4172
4173   /* Finish the firewall level.  */
4174   cp_parser_parse_definitely (parser);
4175   /* And remember the result of the parse for when we try again.  */
4176   cp_token *token = cp_lexer_token_at (parser->lexer, start);
4177   token->type = CPP_PREPARSED_EXPR;
4178   token->u.value = expr;
4179   token->keyword = RID_MAX;
4180   cp_lexer_purge_tokens_after (parser->lexer, start);
4181 }
4182
4183 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4184    enclosing parentheses.  */
4185
4186 static tree
4187 cp_parser_statement_expr (cp_parser *parser)
4188 {
4189   cp_token_position start = cp_parser_start_tentative_firewall (parser);
4190
4191   /* Consume the '('.  */
4192   cp_lexer_consume_token (parser->lexer);
4193   /* Start the statement-expression.  */
4194   tree expr = begin_stmt_expr ();
4195   /* Parse the compound-statement.  */
4196   cp_parser_compound_statement (parser, expr, false, false);
4197   /* Finish up.  */
4198   expr = finish_stmt_expr (expr, false);
4199   /* Consume the ')'.  */
4200   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4201     cp_parser_skip_to_end_of_statement (parser);
4202
4203   cp_parser_end_tentative_firewall (parser, start, expr);
4204   return expr;
4205 }
4206
4207 /* Expressions [gram.expr] */
4208
4209 /* Parse a primary-expression.
4210
4211    primary-expression:
4212      literal
4213      this
4214      ( expression )
4215      id-expression
4216      lambda-expression (C++11)
4217
4218    GNU Extensions:
4219
4220    primary-expression:
4221      ( compound-statement )
4222      __builtin_va_arg ( assignment-expression , type-id )
4223      __builtin_offsetof ( type-id , offsetof-expression )
4224
4225    C++ Extensions:
4226      __has_nothrow_assign ( type-id )   
4227      __has_nothrow_constructor ( type-id )
4228      __has_nothrow_copy ( type-id )
4229      __has_trivial_assign ( type-id )   
4230      __has_trivial_constructor ( type-id )
4231      __has_trivial_copy ( type-id )
4232      __has_trivial_destructor ( type-id )
4233      __has_virtual_destructor ( type-id )     
4234      __is_abstract ( type-id )
4235      __is_base_of ( type-id , type-id )
4236      __is_class ( type-id )
4237      __is_empty ( type-id )
4238      __is_enum ( type-id )
4239      __is_final ( type-id )
4240      __is_literal_type ( type-id )
4241      __is_pod ( type-id )
4242      __is_polymorphic ( type-id )
4243      __is_std_layout ( type-id )
4244      __is_trivial ( type-id )
4245      __is_union ( type-id )
4246
4247    Objective-C++ Extension:
4248
4249    primary-expression:
4250      objc-expression
4251
4252    literal:
4253      __null
4254
4255    ADDRESS_P is true iff this expression was immediately preceded by
4256    "&" and therefore might denote a pointer-to-member.  CAST_P is true
4257    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
4258    true iff this expression is a template argument.
4259
4260    Returns a representation of the expression.  Upon return, *IDK
4261    indicates what kind of id-expression (if any) was present.  */
4262
4263 static tree
4264 cp_parser_primary_expression (cp_parser *parser,
4265                               bool address_p,
4266                               bool cast_p,
4267                               bool template_arg_p,
4268                               bool decltype_p,
4269                               cp_id_kind *idk)
4270 {
4271   cp_token *token = NULL;
4272
4273   /* Assume the primary expression is not an id-expression.  */
4274   *idk = CP_ID_KIND_NONE;
4275
4276   /* Peek at the next token.  */
4277   token = cp_lexer_peek_token (parser->lexer);
4278   switch ((int) token->type)
4279     {
4280       /* literal:
4281            integer-literal
4282            character-literal
4283            floating-literal
4284            string-literal
4285            boolean-literal
4286            pointer-literal
4287            user-defined-literal  */
4288     case CPP_CHAR:
4289     case CPP_CHAR16:
4290     case CPP_CHAR32:
4291     case CPP_WCHAR:
4292     case CPP_NUMBER:
4293     case CPP_PREPARSED_EXPR:
4294       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4295         return cp_parser_userdef_numeric_literal (parser);
4296       token = cp_lexer_consume_token (parser->lexer);
4297       if (TREE_CODE (token->u.value) == FIXED_CST)
4298         {
4299           error_at (token->location,
4300                     "fixed-point types not supported in C++");
4301           return error_mark_node;
4302         }
4303       /* Floating-point literals are only allowed in an integral
4304          constant expression if they are cast to an integral or
4305          enumeration type.  */
4306       if (TREE_CODE (token->u.value) == REAL_CST
4307           && parser->integral_constant_expression_p
4308           && pedantic)
4309         {
4310           /* CAST_P will be set even in invalid code like "int(2.7 +
4311              ...)".   Therefore, we have to check that the next token
4312              is sure to end the cast.  */
4313           if (cast_p)
4314             {
4315               cp_token *next_token;
4316
4317               next_token = cp_lexer_peek_token (parser->lexer);
4318               if (/* The comma at the end of an
4319                      enumerator-definition.  */
4320                   next_token->type != CPP_COMMA
4321                   /* The curly brace at the end of an enum-specifier.  */
4322                   && next_token->type != CPP_CLOSE_BRACE
4323                   /* The end of a statement.  */
4324                   && next_token->type != CPP_SEMICOLON
4325                   /* The end of the cast-expression.  */
4326                   && next_token->type != CPP_CLOSE_PAREN
4327                   /* The end of an array bound.  */
4328                   && next_token->type != CPP_CLOSE_SQUARE
4329                   /* The closing ">" in a template-argument-list.  */
4330                   && (next_token->type != CPP_GREATER
4331                       || parser->greater_than_is_operator_p)
4332                   /* C++0x only: A ">>" treated like two ">" tokens,
4333                      in a template-argument-list.  */
4334                   && (next_token->type != CPP_RSHIFT
4335                       || (cxx_dialect == cxx98)
4336                       || parser->greater_than_is_operator_p))
4337                 cast_p = false;
4338             }
4339
4340           /* If we are within a cast, then the constraint that the
4341              cast is to an integral or enumeration type will be
4342              checked at that point.  If we are not within a cast, then
4343              this code is invalid.  */
4344           if (!cast_p)
4345             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4346         }
4347       return token->u.value;
4348
4349     case CPP_CHAR_USERDEF:
4350     case CPP_CHAR16_USERDEF:
4351     case CPP_CHAR32_USERDEF:
4352     case CPP_WCHAR_USERDEF:
4353       return cp_parser_userdef_char_literal (parser);
4354
4355     case CPP_STRING:
4356     case CPP_STRING16:
4357     case CPP_STRING32:
4358     case CPP_WSTRING:
4359     case CPP_UTF8STRING:
4360     case CPP_STRING_USERDEF:
4361     case CPP_STRING16_USERDEF:
4362     case CPP_STRING32_USERDEF:
4363     case CPP_WSTRING_USERDEF:
4364     case CPP_UTF8STRING_USERDEF:
4365       /* ??? Should wide strings be allowed when parser->translate_strings_p
4366          is false (i.e. in attributes)?  If not, we can kill the third
4367          argument to cp_parser_string_literal.  */
4368       return cp_parser_string_literal (parser,
4369                                        parser->translate_strings_p,
4370                                        true);
4371
4372     case CPP_OPEN_PAREN:
4373       /* If we see `( { ' then we are looking at the beginning of
4374          a GNU statement-expression.  */
4375       if (cp_parser_allow_gnu_extensions_p (parser)
4376           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4377         {
4378           /* Statement-expressions are not allowed by the standard.  */
4379           pedwarn (token->location, OPT_Wpedantic,
4380                    "ISO C++ forbids braced-groups within expressions");
4381
4382           /* And they're not allowed outside of a function-body; you
4383              cannot, for example, write:
4384
4385              int i = ({ int j = 3; j + 1; });
4386
4387              at class or namespace scope.  */
4388           if (!parser->in_function_body
4389               || parser->in_template_argument_list_p)
4390             {
4391               error_at (token->location,
4392                         "statement-expressions are not allowed outside "
4393                         "functions nor in template-argument lists");
4394               cp_parser_skip_to_end_of_block_or_statement (parser);
4395               if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4396                 cp_lexer_consume_token (parser->lexer);
4397               return error_mark_node;
4398             }
4399           else
4400             return cp_parser_statement_expr (parser);
4401         }
4402       /* Otherwise it's a normal parenthesized expression.  */
4403       {
4404         tree expr;
4405         bool saved_greater_than_is_operator_p;
4406
4407         /* Consume the `('.  */
4408         cp_lexer_consume_token (parser->lexer);
4409         /* Within a parenthesized expression, a `>' token is always
4410            the greater-than operator.  */
4411         saved_greater_than_is_operator_p
4412           = parser->greater_than_is_operator_p;
4413         parser->greater_than_is_operator_p = true;
4414
4415         /* Parse the parenthesized expression.  */
4416         expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4417         /* Let the front end know that this expression was
4418            enclosed in parentheses. This matters in case, for
4419            example, the expression is of the form `A::B', since
4420            `&A::B' might be a pointer-to-member, but `&(A::B)' is
4421            not.  */
4422         expr = finish_parenthesized_expr (expr);
4423         /* DR 705: Wrapping an unqualified name in parentheses
4424            suppresses arg-dependent lookup.  We want to pass back
4425            CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4426            (c++/37862), but none of the others.  */
4427         if (*idk != CP_ID_KIND_QUALIFIED)
4428           *idk = CP_ID_KIND_NONE;
4429
4430         /* The `>' token might be the end of a template-id or
4431            template-parameter-list now.  */
4432         parser->greater_than_is_operator_p
4433           = saved_greater_than_is_operator_p;
4434         /* Consume the `)'.  */
4435         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4436           cp_parser_skip_to_end_of_statement (parser);
4437
4438         return expr;
4439       }
4440
4441     case CPP_OPEN_SQUARE:
4442       {
4443         if (c_dialect_objc ())
4444           {
4445             /* We might have an Objective-C++ message. */
4446             cp_parser_parse_tentatively (parser);
4447             tree msg = cp_parser_objc_message_expression (parser);
4448             /* If that works out, we're done ... */
4449             if (cp_parser_parse_definitely (parser))
4450               return msg;
4451             /* ... else, fall though to see if it's a lambda.  */
4452           }
4453         tree lam = cp_parser_lambda_expression (parser);
4454         /* Don't warn about a failed tentative parse.  */
4455         if (cp_parser_error_occurred (parser))
4456           return error_mark_node;
4457         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4458         return lam;
4459       }
4460
4461     case CPP_OBJC_STRING:
4462       if (c_dialect_objc ())
4463         /* We have an Objective-C++ string literal. */
4464         return cp_parser_objc_expression (parser);
4465       cp_parser_error (parser, "expected primary-expression");
4466       return error_mark_node;
4467
4468     case CPP_KEYWORD:
4469       switch (token->keyword)
4470         {
4471           /* These two are the boolean literals.  */
4472         case RID_TRUE:
4473           cp_lexer_consume_token (parser->lexer);
4474           return boolean_true_node;
4475         case RID_FALSE:
4476           cp_lexer_consume_token (parser->lexer);
4477           return boolean_false_node;
4478
4479           /* The `__null' literal.  */
4480         case RID_NULL:
4481           cp_lexer_consume_token (parser->lexer);
4482           return null_node;
4483
4484           /* The `nullptr' literal.  */
4485         case RID_NULLPTR:
4486           cp_lexer_consume_token (parser->lexer);
4487           return nullptr_node;
4488
4489           /* Recognize the `this' keyword.  */
4490         case RID_THIS:
4491           cp_lexer_consume_token (parser->lexer);
4492           if (parser->local_variables_forbidden_p)
4493             {
4494               error_at (token->location,
4495                         "%<this%> may not be used in this context");
4496               return error_mark_node;
4497             }
4498           /* Pointers cannot appear in constant-expressions.  */
4499           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4500             return error_mark_node;
4501           return finish_this_expr ();
4502
4503           /* The `operator' keyword can be the beginning of an
4504              id-expression.  */
4505         case RID_OPERATOR:
4506           goto id_expression;
4507
4508         case RID_FUNCTION_NAME:
4509         case RID_PRETTY_FUNCTION_NAME:
4510         case RID_C99_FUNCTION_NAME:
4511           {
4512             non_integral_constant name;
4513
4514             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4515                __func__ are the names of variables -- but they are
4516                treated specially.  Therefore, they are handled here,
4517                rather than relying on the generic id-expression logic
4518                below.  Grammatically, these names are id-expressions.
4519
4520                Consume the token.  */
4521             token = cp_lexer_consume_token (parser->lexer);
4522
4523             switch (token->keyword)
4524               {
4525               case RID_FUNCTION_NAME:
4526                 name = NIC_FUNC_NAME;
4527                 break;
4528               case RID_PRETTY_FUNCTION_NAME:
4529                 name = NIC_PRETTY_FUNC;
4530                 break;
4531               case RID_C99_FUNCTION_NAME:
4532                 name = NIC_C99_FUNC;
4533                 break;
4534               default:
4535                 gcc_unreachable ();
4536               }
4537
4538             if (cp_parser_non_integral_constant_expression (parser, name))
4539               return error_mark_node;
4540
4541             /* Look up the name.  */
4542             return finish_fname (token->u.value);
4543           }
4544
4545         case RID_VA_ARG:
4546           {
4547             tree expression;
4548             tree type;
4549             source_location type_location;
4550
4551             /* The `__builtin_va_arg' construct is used to handle
4552                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4553             cp_lexer_consume_token (parser->lexer);
4554             /* Look for the opening `('.  */
4555             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4556             /* Now, parse the assignment-expression.  */
4557             expression = cp_parser_assignment_expression (parser);
4558             /* Look for the `,'.  */
4559             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4560             type_location = cp_lexer_peek_token (parser->lexer)->location;
4561             /* Parse the type-id.  */
4562             type = cp_parser_type_id (parser);
4563             /* Look for the closing `)'.  */
4564             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4565             /* Using `va_arg' in a constant-expression is not
4566                allowed.  */
4567             if (cp_parser_non_integral_constant_expression (parser,
4568                                                             NIC_VA_ARG))
4569               return error_mark_node;
4570             return build_x_va_arg (type_location, expression, type);
4571           }
4572
4573         case RID_OFFSETOF:
4574           return cp_parser_builtin_offsetof (parser);
4575
4576         case RID_HAS_NOTHROW_ASSIGN:
4577         case RID_HAS_NOTHROW_CONSTRUCTOR:
4578         case RID_HAS_NOTHROW_COPY:        
4579         case RID_HAS_TRIVIAL_ASSIGN:
4580         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4581         case RID_HAS_TRIVIAL_COPY:        
4582         case RID_HAS_TRIVIAL_DESTRUCTOR:
4583         case RID_HAS_VIRTUAL_DESTRUCTOR:
4584         case RID_IS_ABSTRACT:
4585         case RID_IS_BASE_OF:
4586         case RID_IS_CLASS:
4587         case RID_IS_EMPTY:
4588         case RID_IS_ENUM:
4589         case RID_IS_FINAL:
4590         case RID_IS_LITERAL_TYPE:
4591         case RID_IS_POD:
4592         case RID_IS_POLYMORPHIC:
4593         case RID_IS_STD_LAYOUT:
4594         case RID_IS_TRIVIAL:
4595         case RID_IS_TRIVIALLY_ASSIGNABLE:
4596         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4597         case RID_IS_TRIVIALLY_COPYABLE:
4598         case RID_IS_UNION:
4599           return cp_parser_trait_expr (parser, token->keyword);
4600
4601         /* Objective-C++ expressions.  */
4602         case RID_AT_ENCODE:
4603         case RID_AT_PROTOCOL:
4604         case RID_AT_SELECTOR:
4605           return cp_parser_objc_expression (parser);
4606
4607         case RID_TEMPLATE:
4608           if (parser->in_function_body
4609               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4610                   == CPP_LESS))
4611             {
4612               error_at (token->location,
4613                         "a template declaration cannot appear at block scope");
4614               cp_parser_skip_to_end_of_block_or_statement (parser);
4615               return error_mark_node;
4616             }
4617         default:
4618           cp_parser_error (parser, "expected primary-expression");
4619           return error_mark_node;
4620         }
4621
4622       /* An id-expression can start with either an identifier, a
4623          `::' as the beginning of a qualified-id, or the "operator"
4624          keyword.  */
4625     case CPP_NAME:
4626     case CPP_SCOPE:
4627     case CPP_TEMPLATE_ID:
4628     case CPP_NESTED_NAME_SPECIFIER:
4629       {
4630         tree id_expression;
4631         tree decl;
4632         const char *error_msg;
4633         bool template_p;
4634         bool done;
4635         cp_token *id_expr_token;
4636
4637       id_expression:
4638         /* Parse the id-expression.  */
4639         id_expression
4640           = cp_parser_id_expression (parser,
4641                                      /*template_keyword_p=*/false,
4642                                      /*check_dependency_p=*/true,
4643                                      &template_p,
4644                                      /*declarator_p=*/false,
4645                                      /*optional_p=*/false);
4646         if (id_expression == error_mark_node)
4647           return error_mark_node;
4648         id_expr_token = token;
4649         token = cp_lexer_peek_token (parser->lexer);
4650         done = (token->type != CPP_OPEN_SQUARE
4651                 && token->type != CPP_OPEN_PAREN
4652                 && token->type != CPP_DOT
4653                 && token->type != CPP_DEREF
4654                 && token->type != CPP_PLUS_PLUS
4655                 && token->type != CPP_MINUS_MINUS);
4656         /* If we have a template-id, then no further lookup is
4657            required.  If the template-id was for a template-class, we
4658            will sometimes have a TYPE_DECL at this point.  */
4659         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4660                  || TREE_CODE (id_expression) == TYPE_DECL)
4661           decl = id_expression;
4662         /* Look up the name.  */
4663         else
4664           {
4665             tree ambiguous_decls;
4666
4667             /* If we already know that this lookup is ambiguous, then
4668                we've already issued an error message; there's no reason
4669                to check again.  */
4670             if (id_expr_token->type == CPP_NAME
4671                 && id_expr_token->error_reported)
4672               {
4673                 cp_parser_simulate_error (parser);
4674                 return error_mark_node;
4675               }
4676
4677             decl = cp_parser_lookup_name (parser, id_expression,
4678                                           none_type,
4679                                           template_p,
4680                                           /*is_namespace=*/false,
4681                                           /*check_dependency=*/true,
4682                                           &ambiguous_decls,
4683                                           id_expr_token->location);
4684             /* If the lookup was ambiguous, an error will already have
4685                been issued.  */
4686             if (ambiguous_decls)
4687               return error_mark_node;
4688
4689             /* In Objective-C++, we may have an Objective-C 2.0
4690                dot-syntax for classes here.  */
4691             if (c_dialect_objc ()
4692                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4693                 && TREE_CODE (decl) == TYPE_DECL
4694                 && objc_is_class_name (decl))
4695               {
4696                 tree component;
4697                 cp_lexer_consume_token (parser->lexer);
4698                 component = cp_parser_identifier (parser);
4699                 if (component == error_mark_node)
4700                   return error_mark_node;
4701
4702                 return objc_build_class_component_ref (id_expression, component);
4703               }
4704
4705             /* In Objective-C++, an instance variable (ivar) may be preferred
4706                to whatever cp_parser_lookup_name() found.  */
4707             decl = objc_lookup_ivar (decl, id_expression);
4708
4709             /* If name lookup gives us a SCOPE_REF, then the
4710                qualifying scope was dependent.  */
4711             if (TREE_CODE (decl) == SCOPE_REF)
4712               {
4713                 /* At this point, we do not know if DECL is a valid
4714                    integral constant expression.  We assume that it is
4715                    in fact such an expression, so that code like:
4716
4717                       template <int N> struct A {
4718                         int a[B<N>::i];
4719                       };
4720                      
4721                    is accepted.  At template-instantiation time, we
4722                    will check that B<N>::i is actually a constant.  */
4723                 return decl;
4724               }
4725             /* Check to see if DECL is a local variable in a context
4726                where that is forbidden.  */
4727             if (parser->local_variables_forbidden_p
4728                 && local_variable_p (decl))
4729               {
4730                 /* It might be that we only found DECL because we are
4731                    trying to be generous with pre-ISO scoping rules.
4732                    For example, consider:
4733
4734                      int i;
4735                      void g() {
4736                        for (int i = 0; i < 10; ++i) {}
4737                        extern void f(int j = i);
4738                      }
4739
4740                    Here, name look up will originally find the out
4741                    of scope `i'.  We need to issue a warning message,
4742                    but then use the global `i'.  */
4743                 decl = check_for_out_of_scope_variable (decl);
4744                 if (local_variable_p (decl))
4745                   {
4746                     error_at (id_expr_token->location,
4747                               "local variable %qD may not appear in this context",
4748                               decl);
4749                     return error_mark_node;
4750                   }
4751               }
4752           }
4753
4754         decl = (finish_id_expression
4755                 (id_expression, decl, parser->scope,
4756                  idk,
4757                  parser->integral_constant_expression_p,
4758                  parser->allow_non_integral_constant_expression_p,
4759                  &parser->non_integral_constant_expression_p,
4760                  template_p, done, address_p,
4761                  template_arg_p,
4762                  &error_msg,
4763                  id_expr_token->location));
4764         if (error_msg)
4765           cp_parser_error (parser, error_msg);
4766         return decl;
4767       }
4768
4769       /* Anything else is an error.  */
4770     default:
4771       cp_parser_error (parser, "expected primary-expression");
4772       return error_mark_node;
4773     }
4774 }
4775
4776 static inline tree
4777 cp_parser_primary_expression (cp_parser *parser,
4778                               bool address_p,
4779                               bool cast_p,
4780                               bool template_arg_p,
4781                               cp_id_kind *idk)
4782 {
4783   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4784                                        /*decltype*/false, idk);
4785 }
4786
4787 /* Parse an id-expression.
4788
4789    id-expression:
4790      unqualified-id
4791      qualified-id
4792
4793    qualified-id:
4794      :: [opt] nested-name-specifier template [opt] unqualified-id
4795      :: identifier
4796      :: operator-function-id
4797      :: template-id
4798
4799    Return a representation of the unqualified portion of the
4800    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4801    a `::' or nested-name-specifier.
4802
4803    Often, if the id-expression was a qualified-id, the caller will
4804    want to make a SCOPE_REF to represent the qualified-id.  This
4805    function does not do this in order to avoid wastefully creating
4806    SCOPE_REFs when they are not required.
4807
4808    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4809    `template' keyword.
4810
4811    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4812    uninstantiated templates.
4813
4814    If *TEMPLATE_P is non-NULL, it is set to true iff the
4815    `template' keyword is used to explicitly indicate that the entity
4816    named is a template.
4817
4818    If DECLARATOR_P is true, the id-expression is appearing as part of
4819    a declarator, rather than as part of an expression.  */
4820
4821 static tree
4822 cp_parser_id_expression (cp_parser *parser,
4823                          bool template_keyword_p,
4824                          bool check_dependency_p,
4825                          bool *template_p,
4826                          bool declarator_p,
4827                          bool optional_p)
4828 {
4829   bool global_scope_p;
4830   bool nested_name_specifier_p;
4831
4832   /* Assume the `template' keyword was not used.  */
4833   if (template_p)
4834     *template_p = template_keyword_p;
4835
4836   /* Look for the optional `::' operator.  */
4837   global_scope_p
4838     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4839        != NULL_TREE);
4840   /* Look for the optional nested-name-specifier.  */
4841   nested_name_specifier_p
4842     = (cp_parser_nested_name_specifier_opt (parser,
4843                                             /*typename_keyword_p=*/false,
4844                                             check_dependency_p,
4845                                             /*type_p=*/false,
4846                                             declarator_p)
4847        != NULL_TREE);
4848   /* If there is a nested-name-specifier, then we are looking at
4849      the first qualified-id production.  */
4850   if (nested_name_specifier_p)
4851     {
4852       tree saved_scope;
4853       tree saved_object_scope;
4854       tree saved_qualifying_scope;
4855       tree unqualified_id;
4856       bool is_template;
4857
4858       /* See if the next token is the `template' keyword.  */
4859       if (!template_p)
4860         template_p = &is_template;
4861       *template_p = cp_parser_optional_template_keyword (parser);
4862       /* Name lookup we do during the processing of the
4863          unqualified-id might obliterate SCOPE.  */
4864       saved_scope = parser->scope;
4865       saved_object_scope = parser->object_scope;
4866       saved_qualifying_scope = parser->qualifying_scope;
4867       /* Process the final unqualified-id.  */
4868       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4869                                                  check_dependency_p,
4870                                                  declarator_p,
4871                                                  /*optional_p=*/false);
4872       /* Restore the SAVED_SCOPE for our caller.  */
4873       parser->scope = saved_scope;
4874       parser->object_scope = saved_object_scope;
4875       parser->qualifying_scope = saved_qualifying_scope;
4876
4877       return unqualified_id;
4878     }
4879   /* Otherwise, if we are in global scope, then we are looking at one
4880      of the other qualified-id productions.  */
4881   else if (global_scope_p)
4882     {
4883       cp_token *token;
4884       tree id;
4885
4886       /* Peek at the next token.  */
4887       token = cp_lexer_peek_token (parser->lexer);
4888
4889       /* If it's an identifier, and the next token is not a "<", then
4890          we can avoid the template-id case.  This is an optimization
4891          for this common case.  */
4892       if (token->type == CPP_NAME
4893           && !cp_parser_nth_token_starts_template_argument_list_p
4894                (parser, 2))
4895         return cp_parser_identifier (parser);
4896
4897       cp_parser_parse_tentatively (parser);
4898       /* Try a template-id.  */
4899       id = cp_parser_template_id (parser,
4900                                   /*template_keyword_p=*/false,
4901                                   /*check_dependency_p=*/true,
4902                                   none_type,
4903                                   declarator_p);
4904       /* If that worked, we're done.  */
4905       if (cp_parser_parse_definitely (parser))
4906         return id;
4907
4908       /* Peek at the next token.  (Changes in the token buffer may
4909          have invalidated the pointer obtained above.)  */
4910       token = cp_lexer_peek_token (parser->lexer);
4911
4912       switch (token->type)
4913         {
4914         case CPP_NAME:
4915           return cp_parser_identifier (parser);
4916
4917         case CPP_KEYWORD:
4918           if (token->keyword == RID_OPERATOR)
4919             return cp_parser_operator_function_id (parser);
4920           /* Fall through.  */
4921
4922         default:
4923           cp_parser_error (parser, "expected id-expression");
4924           return error_mark_node;
4925         }
4926     }
4927   else
4928     return cp_parser_unqualified_id (parser, template_keyword_p,
4929                                      /*check_dependency_p=*/true,
4930                                      declarator_p,
4931                                      optional_p);
4932 }
4933
4934 /* Parse an unqualified-id.
4935
4936    unqualified-id:
4937      identifier
4938      operator-function-id
4939      conversion-function-id
4940      ~ class-name
4941      template-id
4942
4943    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4944    keyword, in a construct like `A::template ...'.
4945
4946    Returns a representation of unqualified-id.  For the `identifier'
4947    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4948    production a BIT_NOT_EXPR is returned; the operand of the
4949    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4950    other productions, see the documentation accompanying the
4951    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4952    names are looked up in uninstantiated templates.  If DECLARATOR_P
4953    is true, the unqualified-id is appearing as part of a declarator,
4954    rather than as part of an expression.  */
4955
4956 static tree
4957 cp_parser_unqualified_id (cp_parser* parser,
4958                           bool template_keyword_p,
4959                           bool check_dependency_p,
4960                           bool declarator_p,
4961                           bool optional_p)
4962 {
4963   cp_token *token;
4964
4965   /* Peek at the next token.  */
4966   token = cp_lexer_peek_token (parser->lexer);
4967
4968   switch ((int) token->type)
4969     {
4970     case CPP_NAME:
4971       {
4972         tree id;
4973
4974         /* We don't know yet whether or not this will be a
4975            template-id.  */
4976         cp_parser_parse_tentatively (parser);
4977         /* Try a template-id.  */
4978         id = cp_parser_template_id (parser, template_keyword_p,
4979                                     check_dependency_p,
4980                                     none_type,
4981                                     declarator_p);
4982         /* If it worked, we're done.  */
4983         if (cp_parser_parse_definitely (parser))
4984           return id;
4985         /* Otherwise, it's an ordinary identifier.  */
4986         return cp_parser_identifier (parser);
4987       }
4988
4989     case CPP_TEMPLATE_ID:
4990       return cp_parser_template_id (parser, template_keyword_p,
4991                                     check_dependency_p,
4992                                     none_type,
4993                                     declarator_p);
4994
4995     case CPP_COMPL:
4996       {
4997         tree type_decl;
4998         tree qualifying_scope;
4999         tree object_scope;
5000         tree scope;
5001         bool done;
5002
5003         /* Consume the `~' token.  */
5004         cp_lexer_consume_token (parser->lexer);
5005         /* Parse the class-name.  The standard, as written, seems to
5006            say that:
5007
5008              template <typename T> struct S { ~S (); };
5009              template <typename T> S<T>::~S() {}
5010
5011            is invalid, since `~' must be followed by a class-name, but
5012            `S<T>' is dependent, and so not known to be a class.
5013            That's not right; we need to look in uninstantiated
5014            templates.  A further complication arises from:
5015
5016              template <typename T> void f(T t) {
5017                t.T::~T();
5018              }
5019
5020            Here, it is not possible to look up `T' in the scope of `T'
5021            itself.  We must look in both the current scope, and the
5022            scope of the containing complete expression.
5023
5024            Yet another issue is:
5025
5026              struct S {
5027                int S;
5028                ~S();
5029              };
5030
5031              S::~S() {}
5032
5033            The standard does not seem to say that the `S' in `~S'
5034            should refer to the type `S' and not the data member
5035            `S::S'.  */
5036
5037         /* DR 244 says that we look up the name after the "~" in the
5038            same scope as we looked up the qualifying name.  That idea
5039            isn't fully worked out; it's more complicated than that.  */
5040         scope = parser->scope;
5041         object_scope = parser->object_scope;
5042         qualifying_scope = parser->qualifying_scope;
5043
5044         /* Check for invalid scopes.  */
5045         if (scope == error_mark_node)
5046           {
5047             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5048               cp_lexer_consume_token (parser->lexer);
5049             return error_mark_node;
5050           }
5051         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5052           {
5053             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5054               error_at (token->location,
5055                         "scope %qT before %<~%> is not a class-name",
5056                         scope);
5057             cp_parser_simulate_error (parser);
5058             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5059               cp_lexer_consume_token (parser->lexer);
5060             return error_mark_node;
5061           }
5062         gcc_assert (!scope || TYPE_P (scope));
5063
5064         /* If the name is of the form "X::~X" it's OK even if X is a
5065            typedef.  */
5066         token = cp_lexer_peek_token (parser->lexer);
5067         if (scope
5068             && token->type == CPP_NAME
5069             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5070                 != CPP_LESS)
5071             && (token->u.value == TYPE_IDENTIFIER (scope)
5072                 || (CLASS_TYPE_P (scope)
5073                     && constructor_name_p (token->u.value, scope))))
5074           {
5075             cp_lexer_consume_token (parser->lexer);
5076             return build_nt (BIT_NOT_EXPR, scope);
5077           }
5078
5079         /* ~auto means the destructor of whatever the object is.  */
5080         if (cp_parser_is_keyword (token, RID_AUTO))
5081           {
5082             if (cxx_dialect < cxx14)
5083               pedwarn (input_location, 0,
5084                        "%<~auto%> only available with "
5085                        "-std=c++14 or -std=gnu++14");
5086             cp_lexer_consume_token (parser->lexer);
5087             return build_nt (BIT_NOT_EXPR, make_auto ());
5088           }
5089
5090         /* If there was an explicit qualification (S::~T), first look
5091            in the scope given by the qualification (i.e., S).
5092
5093            Note: in the calls to cp_parser_class_name below we pass
5094            typename_type so that lookup finds the injected-class-name
5095            rather than the constructor.  */
5096         done = false;
5097         type_decl = NULL_TREE;
5098         if (scope)
5099           {
5100             cp_parser_parse_tentatively (parser);
5101             type_decl = cp_parser_class_name (parser,
5102                                               /*typename_keyword_p=*/false,
5103                                               /*template_keyword_p=*/false,
5104                                               typename_type,
5105                                               /*check_dependency=*/false,
5106                                               /*class_head_p=*/false,
5107                                               declarator_p);
5108             if (cp_parser_parse_definitely (parser))
5109               done = true;
5110           }
5111         /* In "N::S::~S", look in "N" as well.  */
5112         if (!done && scope && qualifying_scope)
5113           {
5114             cp_parser_parse_tentatively (parser);
5115             parser->scope = qualifying_scope;
5116             parser->object_scope = NULL_TREE;
5117             parser->qualifying_scope = NULL_TREE;
5118             type_decl
5119               = cp_parser_class_name (parser,
5120                                       /*typename_keyword_p=*/false,
5121                                       /*template_keyword_p=*/false,
5122                                       typename_type,
5123                                       /*check_dependency=*/false,
5124                                       /*class_head_p=*/false,
5125                                       declarator_p);
5126             if (cp_parser_parse_definitely (parser))
5127               done = true;
5128           }
5129         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5130         else if (!done && object_scope)
5131           {
5132             cp_parser_parse_tentatively (parser);
5133             parser->scope = object_scope;
5134             parser->object_scope = NULL_TREE;
5135             parser->qualifying_scope = NULL_TREE;
5136             type_decl
5137               = cp_parser_class_name (parser,
5138                                       /*typename_keyword_p=*/false,
5139                                       /*template_keyword_p=*/false,
5140                                       typename_type,
5141                                       /*check_dependency=*/false,
5142                                       /*class_head_p=*/false,
5143                                       declarator_p);
5144             if (cp_parser_parse_definitely (parser))
5145               done = true;
5146           }
5147         /* Look in the surrounding context.  */
5148         if (!done)
5149           {
5150             parser->scope = NULL_TREE;
5151             parser->object_scope = NULL_TREE;
5152             parser->qualifying_scope = NULL_TREE;
5153             if (processing_template_decl)
5154               cp_parser_parse_tentatively (parser);
5155             type_decl
5156               = cp_parser_class_name (parser,
5157                                       /*typename_keyword_p=*/false,
5158                                       /*template_keyword_p=*/false,
5159                                       typename_type,
5160                                       /*check_dependency=*/false,
5161                                       /*class_head_p=*/false,
5162                                       declarator_p);
5163             if (processing_template_decl
5164                 && ! cp_parser_parse_definitely (parser))
5165               {
5166                 /* We couldn't find a type with this name, so just accept
5167                    it and check for a match at instantiation time.  */
5168                 type_decl = cp_parser_identifier (parser);
5169                 if (type_decl != error_mark_node)
5170                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5171                 return type_decl;
5172               }
5173           }
5174         /* If an error occurred, assume that the name of the
5175            destructor is the same as the name of the qualifying
5176            class.  That allows us to keep parsing after running
5177            into ill-formed destructor names.  */
5178         if (type_decl == error_mark_node && scope)
5179           return build_nt (BIT_NOT_EXPR, scope);
5180         else if (type_decl == error_mark_node)
5181           return error_mark_node;
5182
5183         /* Check that destructor name and scope match.  */
5184         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5185           {
5186             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5187               error_at (token->location,
5188                         "declaration of %<~%T%> as member of %qT",
5189                         type_decl, scope);
5190             cp_parser_simulate_error (parser);
5191             return error_mark_node;
5192           }
5193
5194         /* [class.dtor]
5195
5196            A typedef-name that names a class shall not be used as the
5197            identifier in the declarator for a destructor declaration.  */
5198         if (declarator_p
5199             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5200             && !DECL_SELF_REFERENCE_P (type_decl)
5201             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5202           error_at (token->location,
5203                     "typedef-name %qD used as destructor declarator",
5204                     type_decl);
5205
5206         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5207       }
5208
5209     case CPP_KEYWORD:
5210       if (token->keyword == RID_OPERATOR)
5211         {
5212           tree id;
5213
5214           /* This could be a template-id, so we try that first.  */
5215           cp_parser_parse_tentatively (parser);
5216           /* Try a template-id.  */
5217           id = cp_parser_template_id (parser, template_keyword_p,
5218                                       /*check_dependency_p=*/true,
5219                                       none_type,
5220                                       declarator_p);
5221           /* If that worked, we're done.  */
5222           if (cp_parser_parse_definitely (parser))
5223             return id;
5224           /* We still don't know whether we're looking at an
5225              operator-function-id or a conversion-function-id.  */
5226           cp_parser_parse_tentatively (parser);
5227           /* Try an operator-function-id.  */
5228           id = cp_parser_operator_function_id (parser);
5229           /* If that didn't work, try a conversion-function-id.  */
5230           if (!cp_parser_parse_definitely (parser))
5231             id = cp_parser_conversion_function_id (parser);
5232           else if (UDLIT_OPER_P (id))
5233             {
5234               /* 17.6.3.3.5  */
5235               const char *name = UDLIT_OP_SUFFIX (id);
5236               if (name[0] != '_' && !in_system_header_at (input_location)
5237                   && declarator_p)
5238                 warning (0, "literal operator suffixes not preceded by %<_%>"
5239                             " are reserved for future standardization");
5240             }
5241
5242           return id;
5243         }
5244       /* Fall through.  */
5245
5246     default:
5247       if (optional_p)
5248         return NULL_TREE;
5249       cp_parser_error (parser, "expected unqualified-id");
5250       return error_mark_node;
5251     }
5252 }
5253
5254 /* Parse an (optional) nested-name-specifier.
5255
5256    nested-name-specifier: [C++98]
5257      class-or-namespace-name :: nested-name-specifier [opt]
5258      class-or-namespace-name :: template nested-name-specifier [opt]
5259
5260    nested-name-specifier: [C++0x]
5261      type-name ::
5262      namespace-name ::
5263      nested-name-specifier identifier ::
5264      nested-name-specifier template [opt] simple-template-id ::
5265
5266    PARSER->SCOPE should be set appropriately before this function is
5267    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5268    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
5269    in name lookups.
5270
5271    Sets PARSER->SCOPE to the class (TYPE) or namespace
5272    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5273    it unchanged if there is no nested-name-specifier.  Returns the new
5274    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5275
5276    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5277    part of a declaration and/or decl-specifier.  */
5278
5279 static tree
5280 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5281                                      bool typename_keyword_p,
5282                                      bool check_dependency_p,
5283                                      bool type_p,
5284                                      bool is_declaration)
5285 {
5286   bool success = false;
5287   cp_token_position start = 0;
5288   cp_token *token;
5289
5290   /* Remember where the nested-name-specifier starts.  */
5291   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5292     {
5293       start = cp_lexer_token_position (parser->lexer, false);
5294       push_deferring_access_checks (dk_deferred);
5295     }
5296
5297   while (true)
5298     {
5299       tree new_scope;
5300       tree old_scope;
5301       tree saved_qualifying_scope;
5302       bool template_keyword_p;
5303
5304       /* Spot cases that cannot be the beginning of a
5305          nested-name-specifier.  */
5306       token = cp_lexer_peek_token (parser->lexer);
5307
5308       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5309          the already parsed nested-name-specifier.  */
5310       if (token->type == CPP_NESTED_NAME_SPECIFIER)
5311         {
5312           /* Grab the nested-name-specifier and continue the loop.  */
5313           cp_parser_pre_parsed_nested_name_specifier (parser);
5314           /* If we originally encountered this nested-name-specifier
5315              with IS_DECLARATION set to false, we will not have
5316              resolved TYPENAME_TYPEs, so we must do so here.  */
5317           if (is_declaration
5318               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5319             {
5320               new_scope = resolve_typename_type (parser->scope,
5321                                                  /*only_current_p=*/false);
5322               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5323                 parser->scope = new_scope;
5324             }
5325           success = true;
5326           continue;
5327         }
5328
5329       /* Spot cases that cannot be the beginning of a
5330          nested-name-specifier.  On the second and subsequent times
5331          through the loop, we look for the `template' keyword.  */
5332       if (success && token->keyword == RID_TEMPLATE)
5333         ;
5334       /* A template-id can start a nested-name-specifier.  */
5335       else if (token->type == CPP_TEMPLATE_ID)
5336         ;
5337       /* DR 743: decltype can be used in a nested-name-specifier.  */
5338       else if (token_is_decltype (token))
5339         ;
5340       else
5341         {
5342           /* If the next token is not an identifier, then it is
5343              definitely not a type-name or namespace-name.  */
5344           if (token->type != CPP_NAME)
5345             break;
5346           /* If the following token is neither a `<' (to begin a
5347              template-id), nor a `::', then we are not looking at a
5348              nested-name-specifier.  */
5349           token = cp_lexer_peek_nth_token (parser->lexer, 2);
5350
5351           if (token->type == CPP_COLON
5352               && parser->colon_corrects_to_scope_p
5353               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5354             {
5355               error_at (token->location,
5356                         "found %<:%> in nested-name-specifier, expected %<::%>");
5357               token->type = CPP_SCOPE;
5358             }
5359
5360           if (token->type != CPP_SCOPE
5361               && !cp_parser_nth_token_starts_template_argument_list_p
5362                   (parser, 2))
5363             break;
5364         }
5365
5366       /* The nested-name-specifier is optional, so we parse
5367          tentatively.  */
5368       cp_parser_parse_tentatively (parser);
5369
5370       /* Look for the optional `template' keyword, if this isn't the
5371          first time through the loop.  */
5372       if (success)
5373         template_keyword_p = cp_parser_optional_template_keyword (parser);
5374       else
5375         template_keyword_p = false;
5376
5377       /* Save the old scope since the name lookup we are about to do
5378          might destroy it.  */
5379       old_scope = parser->scope;
5380       saved_qualifying_scope = parser->qualifying_scope;
5381       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5382          look up names in "X<T>::I" in order to determine that "Y" is
5383          a template.  So, if we have a typename at this point, we make
5384          an effort to look through it.  */
5385       if (is_declaration
5386           && !typename_keyword_p
5387           && parser->scope
5388           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5389         parser->scope = resolve_typename_type (parser->scope,
5390                                                /*only_current_p=*/false);
5391       /* Parse the qualifying entity.  */
5392       new_scope
5393         = cp_parser_qualifying_entity (parser,
5394                                        typename_keyword_p,
5395                                        template_keyword_p,
5396                                        check_dependency_p,
5397                                        type_p,
5398                                        is_declaration);
5399       /* Look for the `::' token.  */
5400       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5401
5402       /* If we found what we wanted, we keep going; otherwise, we're
5403          done.  */
5404       if (!cp_parser_parse_definitely (parser))
5405         {
5406           bool error_p = false;
5407
5408           /* Restore the OLD_SCOPE since it was valid before the
5409              failed attempt at finding the last
5410              class-or-namespace-name.  */
5411           parser->scope = old_scope;
5412           parser->qualifying_scope = saved_qualifying_scope;
5413
5414           /* If the next token is a decltype, and the one after that is a
5415              `::', then the decltype has failed to resolve to a class or
5416              enumeration type.  Give this error even when parsing
5417              tentatively since it can't possibly be valid--and we're going
5418              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5419              won't get another chance.*/
5420           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5421               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5422                   == CPP_SCOPE))
5423             {
5424               token = cp_lexer_consume_token (parser->lexer);
5425               error_at (token->location, "decltype evaluates to %qT, "
5426                         "which is not a class or enumeration type",
5427                         token->u.value);
5428               parser->scope = error_mark_node;
5429               error_p = true;
5430               /* As below.  */
5431               success = true;
5432               cp_lexer_consume_token (parser->lexer);
5433             }
5434
5435           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5436               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5437             {
5438               /* If we have a non-type template-id followed by ::, it can't
5439                  possibly be valid.  */
5440               token = cp_lexer_peek_token (parser->lexer);
5441               tree tid = token->u.tree_check_value->value;
5442               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5443                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5444                 {
5445                   tree tmpl = NULL_TREE;
5446                   if (is_overloaded_fn (tid))
5447                     {
5448                       tree fns = get_fns (tid);
5449                       if (!OVL_CHAIN (fns))
5450                         tmpl = OVL_CURRENT (fns);
5451                       error_at (token->location, "function template-id %qD "
5452                                 "in nested-name-specifier", tid);
5453                     }
5454                   else
5455                     {
5456                       /* Variable template.  */
5457                       tmpl = TREE_OPERAND (tid, 0);
5458                       gcc_assert (variable_template_p (tmpl));
5459                       error_at (token->location, "variable template-id %qD "
5460                                 "in nested-name-specifier", tid);
5461                     }
5462                   if (tmpl)
5463                     inform (DECL_SOURCE_LOCATION (tmpl),
5464                             "%qD declared here", tmpl);
5465
5466                   parser->scope = error_mark_node;
5467                   error_p = true;
5468                   /* As below.  */
5469                   success = true;
5470                   cp_lexer_consume_token (parser->lexer);
5471                   cp_lexer_consume_token (parser->lexer);
5472                 }
5473             }
5474
5475           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5476             break;
5477           /* If the next token is an identifier, and the one after
5478              that is a `::', then any valid interpretation would have
5479              found a class-or-namespace-name.  */
5480           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5481                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5482                      == CPP_SCOPE)
5483                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5484                      != CPP_COMPL))
5485             {
5486               token = cp_lexer_consume_token (parser->lexer);
5487               if (!error_p)
5488                 {
5489                   if (!token->error_reported)
5490                     {
5491                       tree decl;
5492                       tree ambiguous_decls;
5493
5494                       decl = cp_parser_lookup_name (parser, token->u.value,
5495                                                     none_type,
5496                                                     /*is_template=*/false,
5497                                                     /*is_namespace=*/false,
5498                                                     /*check_dependency=*/true,
5499                                                     &ambiguous_decls,
5500                                                     token->location);
5501                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5502                         error_at (token->location,
5503                                   "%qD used without template parameters",
5504                                   decl);
5505                       else if (ambiguous_decls)
5506                         {
5507                           // cp_parser_lookup_name has the same diagnostic,
5508                           // thus make sure to emit it at most once.
5509                           if (cp_parser_uncommitted_to_tentative_parse_p
5510                               (parser))
5511                             {
5512                               error_at (token->location,
5513                                         "reference to %qD is ambiguous",
5514                                         token->u.value);
5515                               print_candidates (ambiguous_decls);
5516                             }
5517                           decl = error_mark_node;
5518                         }
5519                       else
5520                         {
5521                           if (cxx_dialect != cxx98)
5522                             cp_parser_name_lookup_error
5523                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5524                              token->location);
5525                           else
5526                             cp_parser_name_lookup_error
5527                             (parser, token->u.value, decl, NLE_CXX98,
5528                              token->location);
5529                         }
5530                     }
5531                   parser->scope = error_mark_node;
5532                   error_p = true;
5533                   /* Treat this as a successful nested-name-specifier
5534                      due to:
5535
5536                      [basic.lookup.qual]
5537
5538                      If the name found is not a class-name (clause
5539                      _class_) or namespace-name (_namespace.def_), the
5540                      program is ill-formed.  */
5541                   success = true;
5542                 }
5543               cp_lexer_consume_token (parser->lexer);
5544             }
5545           break;
5546         }
5547       /* We've found one valid nested-name-specifier.  */
5548       success = true;
5549       /* Name lookup always gives us a DECL.  */
5550       if (TREE_CODE (new_scope) == TYPE_DECL)
5551         new_scope = TREE_TYPE (new_scope);
5552       /* Uses of "template" must be followed by actual templates.  */
5553       if (template_keyword_p
5554           && !(CLASS_TYPE_P (new_scope)
5555                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5556                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5557                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5558           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5559                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5560                    == TEMPLATE_ID_EXPR)))
5561         permerror (input_location, TYPE_P (new_scope)
5562                    ? G_("%qT is not a template")
5563                    : G_("%qD is not a template"),
5564                    new_scope);
5565       /* If it is a class scope, try to complete it; we are about to
5566          be looking up names inside the class.  */
5567       if (TYPE_P (new_scope)
5568           /* Since checking types for dependency can be expensive,
5569              avoid doing it if the type is already complete.  */
5570           && !COMPLETE_TYPE_P (new_scope)
5571           /* Do not try to complete dependent types.  */
5572           && !dependent_type_p (new_scope))
5573         {
5574           new_scope = complete_type (new_scope);
5575           /* If it is a typedef to current class, use the current
5576              class instead, as the typedef won't have any names inside
5577              it yet.  */
5578           if (!COMPLETE_TYPE_P (new_scope)
5579               && currently_open_class (new_scope))
5580             new_scope = TYPE_MAIN_VARIANT (new_scope);
5581         }
5582       /* Make sure we look in the right scope the next time through
5583          the loop.  */
5584       parser->scope = new_scope;
5585     }
5586
5587   /* If parsing tentatively, replace the sequence of tokens that makes
5588      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5589      token.  That way, should we re-parse the token stream, we will
5590      not have to repeat the effort required to do the parse, nor will
5591      we issue duplicate error messages.  */
5592   if (success && start)
5593     {
5594       cp_token *token;
5595
5596       token = cp_lexer_token_at (parser->lexer, start);
5597       /* Reset the contents of the START token.  */
5598       token->type = CPP_NESTED_NAME_SPECIFIER;
5599       /* Retrieve any deferred checks.  Do not pop this access checks yet
5600          so the memory will not be reclaimed during token replacing below.  */
5601       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5602       token->u.tree_check_value->value = parser->scope;
5603       token->u.tree_check_value->checks = get_deferred_access_checks ();
5604       token->u.tree_check_value->qualifying_scope =
5605         parser->qualifying_scope;
5606       token->keyword = RID_MAX;
5607
5608       /* Purge all subsequent tokens.  */
5609       cp_lexer_purge_tokens_after (parser->lexer, start);
5610     }
5611
5612   if (start)
5613     pop_to_parent_deferring_access_checks ();
5614
5615   return success ? parser->scope : NULL_TREE;
5616 }
5617
5618 /* Parse a nested-name-specifier.  See
5619    cp_parser_nested_name_specifier_opt for details.  This function
5620    behaves identically, except that it will an issue an error if no
5621    nested-name-specifier is present.  */
5622
5623 static tree
5624 cp_parser_nested_name_specifier (cp_parser *parser,
5625                                  bool typename_keyword_p,
5626                                  bool check_dependency_p,
5627                                  bool type_p,
5628                                  bool is_declaration)
5629 {
5630   tree scope;
5631
5632   /* Look for the nested-name-specifier.  */
5633   scope = cp_parser_nested_name_specifier_opt (parser,
5634                                                typename_keyword_p,
5635                                                check_dependency_p,
5636                                                type_p,
5637                                                is_declaration);
5638   /* If it was not present, issue an error message.  */
5639   if (!scope)
5640     {
5641       cp_parser_error (parser, "expected nested-name-specifier");
5642       parser->scope = NULL_TREE;
5643     }
5644
5645   return scope;
5646 }
5647
5648 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5649    this is either a class-name or a namespace-name (which corresponds
5650    to the class-or-namespace-name production in the grammar). For
5651    C++0x, it can also be a type-name that refers to an enumeration
5652    type or a simple-template-id.
5653
5654    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5655    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5656    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5657    TYPE_P is TRUE iff the next name should be taken as a class-name,
5658    even the same name is declared to be another entity in the same
5659    scope.
5660
5661    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5662    specified by the class-or-namespace-name.  If neither is found the
5663    ERROR_MARK_NODE is returned.  */
5664
5665 static tree
5666 cp_parser_qualifying_entity (cp_parser *parser,
5667                              bool typename_keyword_p,
5668                              bool template_keyword_p,
5669                              bool check_dependency_p,
5670                              bool type_p,
5671                              bool is_declaration)
5672 {
5673   tree saved_scope;
5674   tree saved_qualifying_scope;
5675   tree saved_object_scope;
5676   tree scope;
5677   bool only_class_p;
5678   bool successful_parse_p;
5679
5680   /* DR 743: decltype can appear in a nested-name-specifier.  */
5681   if (cp_lexer_next_token_is_decltype (parser->lexer))
5682     {
5683       scope = cp_parser_decltype (parser);
5684       if (TREE_CODE (scope) != ENUMERAL_TYPE
5685           && !MAYBE_CLASS_TYPE_P (scope))
5686         {
5687           cp_parser_simulate_error (parser);
5688           return error_mark_node;
5689         }
5690       if (TYPE_NAME (scope))
5691         scope = TYPE_NAME (scope);
5692       return scope;
5693     }
5694
5695   /* Before we try to parse the class-name, we must save away the
5696      current PARSER->SCOPE since cp_parser_class_name will destroy
5697      it.  */
5698   saved_scope = parser->scope;
5699   saved_qualifying_scope = parser->qualifying_scope;
5700   saved_object_scope = parser->object_scope;
5701   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5702      there is no need to look for a namespace-name.  */
5703   only_class_p = template_keyword_p 
5704     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5705   if (!only_class_p)
5706     cp_parser_parse_tentatively (parser);
5707   scope = cp_parser_class_name (parser,
5708                                 typename_keyword_p,
5709                                 template_keyword_p,
5710                                 type_p ? class_type : none_type,
5711                                 check_dependency_p,
5712                                 /*class_head_p=*/false,
5713                                 is_declaration);
5714   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5715   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5716   if (!only_class_p 
5717       && cxx_dialect != cxx98
5718       && !successful_parse_p)
5719     {
5720       /* Restore the saved scope.  */
5721       parser->scope = saved_scope;
5722       parser->qualifying_scope = saved_qualifying_scope;
5723       parser->object_scope = saved_object_scope;
5724
5725       /* Parse tentatively.  */
5726       cp_parser_parse_tentatively (parser);
5727      
5728       /* Parse a type-name  */
5729       scope = cp_parser_type_name (parser);
5730
5731       /* "If the name found does not designate a namespace or a class,
5732          enumeration, or dependent type, the program is ill-formed."
5733
5734          We cover classes and dependent types above and namespaces below,
5735          so this code is only looking for enums.  */
5736       if (!scope || TREE_CODE (scope) != TYPE_DECL
5737           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5738         cp_parser_simulate_error (parser);
5739
5740       successful_parse_p = cp_parser_parse_definitely (parser);
5741     }
5742   /* If that didn't work, try for a namespace-name.  */
5743   if (!only_class_p && !successful_parse_p)
5744     {
5745       /* Restore the saved scope.  */
5746       parser->scope = saved_scope;
5747       parser->qualifying_scope = saved_qualifying_scope;
5748       parser->object_scope = saved_object_scope;
5749       /* If we are not looking at an identifier followed by the scope
5750          resolution operator, then this is not part of a
5751          nested-name-specifier.  (Note that this function is only used
5752          to parse the components of a nested-name-specifier.)  */
5753       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5754           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5755         return error_mark_node;
5756       scope = cp_parser_namespace_name (parser);
5757     }
5758
5759   return scope;
5760 }
5761
5762 /* Return true if we are looking at a compound-literal, false otherwise.  */
5763
5764 static bool
5765 cp_parser_compound_literal_p (cp_parser *parser)
5766 {
5767   /* Consume the `('.  */
5768   cp_lexer_consume_token (parser->lexer);
5769
5770   cp_lexer_save_tokens (parser->lexer);
5771
5772   /* Skip tokens until the next token is a closing parenthesis.
5773      If we find the closing `)', and the next token is a `{', then
5774      we are looking at a compound-literal.  */
5775   bool compound_literal_p
5776     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5777                                               /*consume_paren=*/true)
5778        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5779   
5780   /* Roll back the tokens we skipped.  */
5781   cp_lexer_rollback_tokens (parser->lexer);
5782
5783   return compound_literal_p;
5784 }
5785
5786 /* Parse a postfix-expression.
5787
5788    postfix-expression:
5789      primary-expression
5790      postfix-expression [ expression ]
5791      postfix-expression ( expression-list [opt] )
5792      simple-type-specifier ( expression-list [opt] )
5793      typename :: [opt] nested-name-specifier identifier
5794        ( expression-list [opt] )
5795      typename :: [opt] nested-name-specifier template [opt] template-id
5796        ( expression-list [opt] )
5797      postfix-expression . template [opt] id-expression
5798      postfix-expression -> template [opt] id-expression
5799      postfix-expression . pseudo-destructor-name
5800      postfix-expression -> pseudo-destructor-name
5801      postfix-expression ++
5802      postfix-expression --
5803      dynamic_cast < type-id > ( expression )
5804      static_cast < type-id > ( expression )
5805      reinterpret_cast < type-id > ( expression )
5806      const_cast < type-id > ( expression )
5807      typeid ( expression )
5808      typeid ( type-id )
5809
5810    GNU Extension:
5811
5812    postfix-expression:
5813      ( type-id ) { initializer-list , [opt] }
5814
5815    This extension is a GNU version of the C99 compound-literal
5816    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5817    but they are essentially the same concept.)
5818
5819    If ADDRESS_P is true, the postfix expression is the operand of the
5820    `&' operator.  CAST_P is true if this expression is the target of a
5821    cast.
5822
5823    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5824    class member access expressions [expr.ref].
5825
5826    Returns a representation of the expression.  */
5827
5828 static tree
5829 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5830                               bool member_access_only_p, bool decltype_p,
5831                               cp_id_kind * pidk_return)
5832 {
5833   cp_token *token;
5834   location_t loc;
5835   enum rid keyword;
5836   cp_id_kind idk = CP_ID_KIND_NONE;
5837   tree postfix_expression = NULL_TREE;
5838   bool is_member_access = false;
5839   int saved_in_statement = -1;
5840
5841   /* Peek at the next token.  */
5842   token = cp_lexer_peek_token (parser->lexer);
5843   loc = token->location;
5844   /* Some of the productions are determined by keywords.  */
5845   keyword = token->keyword;
5846   switch (keyword)
5847     {
5848     case RID_DYNCAST:
5849     case RID_STATCAST:
5850     case RID_REINTCAST:
5851     case RID_CONSTCAST:
5852       {
5853         tree type;
5854         tree expression;
5855         const char *saved_message;
5856         bool saved_in_type_id_in_expr_p;
5857
5858         /* All of these can be handled in the same way from the point
5859            of view of parsing.  Begin by consuming the token
5860            identifying the cast.  */
5861         cp_lexer_consume_token (parser->lexer);
5862
5863         /* New types cannot be defined in the cast.  */
5864         saved_message = parser->type_definition_forbidden_message;
5865         parser->type_definition_forbidden_message
5866           = G_("types may not be defined in casts");
5867
5868         /* Look for the opening `<'.  */
5869         cp_parser_require (parser, CPP_LESS, RT_LESS);
5870         /* Parse the type to which we are casting.  */
5871         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5872         parser->in_type_id_in_expr_p = true;
5873         type = cp_parser_type_id (parser);
5874         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5875         /* Look for the closing `>'.  */
5876         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5877         /* Restore the old message.  */
5878         parser->type_definition_forbidden_message = saved_message;
5879
5880         bool saved_greater_than_is_operator_p
5881           = parser->greater_than_is_operator_p;
5882         parser->greater_than_is_operator_p = true;
5883
5884         /* And the expression which is being cast.  */
5885         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5886         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5887         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5888
5889         parser->greater_than_is_operator_p
5890           = saved_greater_than_is_operator_p;
5891
5892         /* Only type conversions to integral or enumeration types
5893            can be used in constant-expressions.  */
5894         if (!cast_valid_in_integral_constant_expression_p (type)
5895             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5896           return error_mark_node;
5897
5898         switch (keyword)
5899           {
5900           case RID_DYNCAST:
5901             postfix_expression
5902               = build_dynamic_cast (type, expression, tf_warning_or_error);
5903             break;
5904           case RID_STATCAST:
5905             postfix_expression
5906               = build_static_cast (type, expression, tf_warning_or_error);
5907             break;
5908           case RID_REINTCAST:
5909             postfix_expression
5910               = build_reinterpret_cast (type, expression, 
5911                                         tf_warning_or_error);
5912             break;
5913           case RID_CONSTCAST:
5914             postfix_expression
5915               = build_const_cast (type, expression, tf_warning_or_error);
5916             break;
5917           default:
5918             gcc_unreachable ();
5919           }
5920       }
5921       break;
5922
5923     case RID_TYPEID:
5924       {
5925         tree type;
5926         const char *saved_message;
5927         bool saved_in_type_id_in_expr_p;
5928
5929         /* Consume the `typeid' token.  */
5930         cp_lexer_consume_token (parser->lexer);
5931         /* Look for the `(' token.  */
5932         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5933         /* Types cannot be defined in a `typeid' expression.  */
5934         saved_message = parser->type_definition_forbidden_message;
5935         parser->type_definition_forbidden_message
5936           = G_("types may not be defined in a %<typeid%> expression");
5937         /* We can't be sure yet whether we're looking at a type-id or an
5938            expression.  */
5939         cp_parser_parse_tentatively (parser);
5940         /* Try a type-id first.  */
5941         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5942         parser->in_type_id_in_expr_p = true;
5943         type = cp_parser_type_id (parser);
5944         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5945         /* Look for the `)' token.  Otherwise, we can't be sure that
5946            we're not looking at an expression: consider `typeid (int
5947            (3))', for example.  */
5948         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5949         /* If all went well, simply lookup the type-id.  */
5950         if (cp_parser_parse_definitely (parser))
5951           postfix_expression = get_typeid (type, tf_warning_or_error);
5952         /* Otherwise, fall back to the expression variant.  */
5953         else
5954           {
5955             tree expression;
5956
5957             /* Look for an expression.  */
5958             expression = cp_parser_expression (parser, & idk);
5959             /* Compute its typeid.  */
5960             postfix_expression = build_typeid (expression, tf_warning_or_error);
5961             /* Look for the `)' token.  */
5962             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5963           }
5964         /* Restore the saved message.  */
5965         parser->type_definition_forbidden_message = saved_message;
5966         /* `typeid' may not appear in an integral constant expression.  */
5967         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5968           return error_mark_node;
5969       }
5970       break;
5971
5972     case RID_TYPENAME:
5973       {
5974         tree type;
5975         /* The syntax permitted here is the same permitted for an
5976            elaborated-type-specifier.  */
5977         type = cp_parser_elaborated_type_specifier (parser,
5978                                                     /*is_friend=*/false,
5979                                                     /*is_declaration=*/false);
5980         postfix_expression = cp_parser_functional_cast (parser, type);
5981       }
5982       break;
5983
5984     case RID_CILK_SPAWN:
5985       {
5986         cp_lexer_consume_token (parser->lexer);
5987         token = cp_lexer_peek_token (parser->lexer);
5988         if (token->type == CPP_SEMICOLON)
5989           {
5990             error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5991                       "an expression");
5992             postfix_expression = error_mark_node;
5993             break;
5994           }
5995         else if (!current_function_decl)
5996           {
5997             error_at (token->location, "%<_Cilk_spawn%> may only be used "
5998                       "inside a function");
5999             postfix_expression = error_mark_node;
6000             break;
6001           }
6002         else
6003           {
6004             /* Consecutive _Cilk_spawns are not allowed in a statement.  */
6005             saved_in_statement = parser->in_statement;
6006             parser->in_statement |= IN_CILK_SPAWN;
6007           }
6008         cfun->calls_cilk_spawn = 1;
6009         postfix_expression = 
6010           cp_parser_postfix_expression (parser, false, false, 
6011                                         false, false, &idk);
6012         if (!flag_cilkplus)
6013           {
6014             error_at (token->location, "-fcilkplus must be enabled to use"
6015                       " %<_Cilk_spawn%>");
6016             cfun->calls_cilk_spawn = 0;
6017           }
6018         else if (saved_in_statement & IN_CILK_SPAWN)
6019           {
6020             error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6021                       "are not permitted");
6022             postfix_expression = error_mark_node;
6023             cfun->calls_cilk_spawn = 0; 
6024           }
6025         else
6026           {
6027             postfix_expression = build_cilk_spawn (token->location, 
6028                                                    postfix_expression);
6029             if (postfix_expression != error_mark_node) 
6030               SET_EXPR_LOCATION (postfix_expression, input_location);
6031             parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6032           }
6033         break;
6034       }
6035
6036     case RID_BUILTIN_SHUFFLE:
6037       {
6038         vec<tree, va_gc> *vec;
6039         unsigned int i;
6040         tree p;
6041
6042         cp_lexer_consume_token (parser->lexer);
6043         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6044                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6045                     /*non_constant_p=*/NULL);
6046         if (vec == NULL)
6047           return error_mark_node;
6048
6049         FOR_EACH_VEC_ELT (*vec, i, p)
6050           mark_exp_read (p);
6051
6052         if (vec->length () == 2)
6053           return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6054                                          tf_warning_or_error);
6055         else if (vec->length () == 3)
6056           return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6057                                          tf_warning_or_error);
6058         else
6059         {
6060           error_at (loc, "wrong number of arguments to "
6061               "%<__builtin_shuffle%>");
6062           return error_mark_node;
6063         }
6064         break;
6065       }
6066
6067     default:
6068       {
6069         tree type;
6070
6071         /* If the next thing is a simple-type-specifier, we may be
6072            looking at a functional cast.  We could also be looking at
6073            an id-expression.  So, we try the functional cast, and if
6074            that doesn't work we fall back to the primary-expression.  */
6075         cp_parser_parse_tentatively (parser);
6076         /* Look for the simple-type-specifier.  */
6077         type = cp_parser_simple_type_specifier (parser,
6078                                                 /*decl_specs=*/NULL,
6079                                                 CP_PARSER_FLAGS_NONE);
6080         /* Parse the cast itself.  */
6081         if (!cp_parser_error_occurred (parser))
6082           postfix_expression
6083             = cp_parser_functional_cast (parser, type);
6084         /* If that worked, we're done.  */
6085         if (cp_parser_parse_definitely (parser))
6086           break;
6087
6088         /* If the functional-cast didn't work out, try a
6089            compound-literal.  */
6090         if (cp_parser_allow_gnu_extensions_p (parser)
6091             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6092           {
6093             tree initializer = NULL_TREE;
6094
6095             cp_parser_parse_tentatively (parser);
6096
6097             /* Avoid calling cp_parser_type_id pointlessly, see comment
6098                in cp_parser_cast_expression about c++/29234.  */
6099             if (!cp_parser_compound_literal_p (parser))
6100               cp_parser_simulate_error (parser);
6101             else
6102               {
6103                 /* Parse the type.  */
6104                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6105                 parser->in_type_id_in_expr_p = true;
6106                 type = cp_parser_type_id (parser);
6107                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6108                 /* Look for the `)'.  */
6109                 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6110               }
6111
6112             /* If things aren't going well, there's no need to
6113                keep going.  */
6114             if (!cp_parser_error_occurred (parser))
6115               {
6116                 bool non_constant_p;
6117                 /* Parse the brace-enclosed initializer list.  */
6118                 initializer = cp_parser_braced_list (parser,
6119                                                      &non_constant_p);
6120               }
6121             /* If that worked, we're definitely looking at a
6122                compound-literal expression.  */
6123             if (cp_parser_parse_definitely (parser))
6124               {
6125                 /* Warn the user that a compound literal is not
6126                    allowed in standard C++.  */
6127                 pedwarn (input_location, OPT_Wpedantic,
6128                          "ISO C++ forbids compound-literals");
6129                 /* For simplicity, we disallow compound literals in
6130                    constant-expressions.  We could
6131                    allow compound literals of integer type, whose
6132                    initializer was a constant, in constant
6133                    expressions.  Permitting that usage, as a further
6134                    extension, would not change the meaning of any
6135                    currently accepted programs.  (Of course, as
6136                    compound literals are not part of ISO C++, the
6137                    standard has nothing to say.)  */
6138                 if (cp_parser_non_integral_constant_expression (parser,
6139                                                                 NIC_NCC))
6140                   {
6141                     postfix_expression = error_mark_node;
6142                     break;
6143                   }
6144                 /* Form the representation of the compound-literal.  */
6145                 postfix_expression
6146                   = finish_compound_literal (type, initializer,
6147                                              tf_warning_or_error);
6148                 break;
6149               }
6150           }
6151
6152         /* It must be a primary-expression.  */
6153         postfix_expression
6154           = cp_parser_primary_expression (parser, address_p, cast_p,
6155                                           /*template_arg_p=*/false,
6156                                           decltype_p,
6157                                           &idk);
6158       }
6159       break;
6160     }
6161
6162   /* Note that we don't need to worry about calling build_cplus_new on a
6163      class-valued CALL_EXPR in decltype when it isn't the end of the
6164      postfix-expression; unary_complex_lvalue will take care of that for
6165      all these cases.  */
6166
6167   /* Keep looping until the postfix-expression is complete.  */
6168   while (true)
6169     {
6170       if (idk == CP_ID_KIND_UNQUALIFIED
6171           && identifier_p (postfix_expression)
6172           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6173         /* It is not a Koenig lookup function call.  */
6174         postfix_expression
6175           = unqualified_name_lookup_error (postfix_expression);
6176
6177       /* Peek at the next token.  */
6178       token = cp_lexer_peek_token (parser->lexer);
6179
6180       switch (token->type)
6181         {
6182         case CPP_OPEN_SQUARE:
6183           if (cp_next_tokens_can_be_std_attribute_p (parser))
6184             {
6185               cp_parser_error (parser,
6186                                "two consecutive %<[%> shall "
6187                                "only introduce an attribute");
6188               return error_mark_node;
6189             }
6190           postfix_expression
6191             = cp_parser_postfix_open_square_expression (parser,
6192                                                         postfix_expression,
6193                                                         false,
6194                                                         decltype_p);
6195           idk = CP_ID_KIND_NONE;
6196           is_member_access = false;
6197           break;
6198
6199         case CPP_OPEN_PAREN:
6200           /* postfix-expression ( expression-list [opt] ) */
6201           {
6202             bool koenig_p;
6203             bool is_builtin_constant_p;
6204             bool saved_integral_constant_expression_p = false;
6205             bool saved_non_integral_constant_expression_p = false;
6206             tsubst_flags_t complain = complain_flags (decltype_p);
6207             vec<tree, va_gc> *args;
6208
6209             is_member_access = false;
6210
6211             is_builtin_constant_p
6212               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6213             if (is_builtin_constant_p)
6214               {
6215                 /* The whole point of __builtin_constant_p is to allow
6216                    non-constant expressions to appear as arguments.  */
6217                 saved_integral_constant_expression_p
6218                   = parser->integral_constant_expression_p;
6219                 saved_non_integral_constant_expression_p
6220                   = parser->non_integral_constant_expression_p;
6221                 parser->integral_constant_expression_p = false;
6222               }
6223             args = (cp_parser_parenthesized_expression_list
6224                     (parser, non_attr,
6225                      /*cast_p=*/false, /*allow_expansion_p=*/true,
6226                      /*non_constant_p=*/NULL,
6227                      /*want_literal_zero_p=*/warn_memset_transposed_args));
6228             if (is_builtin_constant_p)
6229               {
6230                 parser->integral_constant_expression_p
6231                   = saved_integral_constant_expression_p;
6232                 parser->non_integral_constant_expression_p
6233                   = saved_non_integral_constant_expression_p;
6234               }
6235
6236             if (args == NULL)
6237               {
6238                 postfix_expression = error_mark_node;
6239                 break;
6240               }
6241
6242             /* Function calls are not permitted in
6243                constant-expressions.  */
6244             if (! builtin_valid_in_constant_expr_p (postfix_expression)
6245                 && cp_parser_non_integral_constant_expression (parser,
6246                                                                NIC_FUNC_CALL))
6247               {
6248                 postfix_expression = error_mark_node;
6249                 release_tree_vector (args);
6250                 break;
6251               }
6252
6253             koenig_p = false;
6254             if (idk == CP_ID_KIND_UNQUALIFIED
6255                 || idk == CP_ID_KIND_TEMPLATE_ID)
6256               {
6257                 if (identifier_p (postfix_expression))
6258                   {
6259                     if (!args->is_empty ())
6260                       {
6261                         koenig_p = true;
6262                         if (!any_type_dependent_arguments_p (args))
6263                           postfix_expression
6264                             = perform_koenig_lookup (postfix_expression, args,
6265                                                      complain);
6266                       }
6267                     else
6268                       postfix_expression
6269                         = unqualified_fn_lookup_error (postfix_expression);
6270                   }
6271                 /* We do not perform argument-dependent lookup if
6272                    normal lookup finds a non-function, in accordance
6273                    with the expected resolution of DR 218.  */
6274                 else if (!args->is_empty ()
6275                          && is_overloaded_fn (postfix_expression))
6276                   {
6277                     tree fn = get_first_fn (postfix_expression);
6278                     fn = STRIP_TEMPLATE (fn);
6279
6280                     /* Do not do argument dependent lookup if regular
6281                        lookup finds a member function or a block-scope
6282                        function declaration.  [basic.lookup.argdep]/3  */
6283                     if (!DECL_FUNCTION_MEMBER_P (fn)
6284                         && !DECL_LOCAL_FUNCTION_P (fn))
6285                       {
6286                         koenig_p = true;
6287                         if (!any_type_dependent_arguments_p (args))
6288                           postfix_expression
6289                             = perform_koenig_lookup (postfix_expression, args,
6290                                                      complain);
6291                       }
6292                   }
6293               }
6294
6295             if (warn_memset_transposed_args)
6296               {
6297                 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6298                     && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6299                     && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6300                     && vec_safe_length (args) == 3
6301                     && integer_zerop ((*args)[2])
6302                     && LITERAL_ZERO_P ((*args)[2])
6303                     && !(integer_zerop ((*args)[1])
6304                          && LITERAL_ZERO_P ((*args)[1])))
6305                   warning (OPT_Wmemset_transposed_args,
6306                            "%<memset%> used with constant zero length "
6307                            "parameter; this could be due to transposed "
6308                            "parameters");
6309
6310                 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6311                    to avoid leaking those into folder and middle-end.  */
6312                 unsigned int i;
6313                 tree arg;
6314                 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6315                   if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6316                     (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6317               }
6318
6319             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6320               {
6321                 tree instance = TREE_OPERAND (postfix_expression, 0);
6322                 tree fn = TREE_OPERAND (postfix_expression, 1);
6323
6324                 if (processing_template_decl
6325                     && (type_dependent_expression_p (instance)
6326                         || (!BASELINK_P (fn)
6327                             && TREE_CODE (fn) != FIELD_DECL)
6328                         || type_dependent_expression_p (fn)
6329                         || any_type_dependent_arguments_p (args)))
6330                   {
6331                     postfix_expression
6332                       = build_nt_call_vec (postfix_expression, args);
6333                     release_tree_vector (args);
6334                     break;
6335                   }
6336
6337                 if (BASELINK_P (fn))
6338                   {
6339                   postfix_expression
6340                     = (build_new_method_call
6341                        (instance, fn, &args, NULL_TREE,
6342                         (idk == CP_ID_KIND_QUALIFIED
6343                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6344                          : LOOKUP_NORMAL),
6345                         /*fn_p=*/NULL,
6346                         complain));
6347                   }
6348                 else
6349                   postfix_expression
6350                     = finish_call_expr (postfix_expression, &args,
6351                                         /*disallow_virtual=*/false,
6352                                         /*koenig_p=*/false,
6353                                         complain);
6354               }
6355             else if (TREE_CODE (postfix_expression) == OFFSET_REF
6356                      || TREE_CODE (postfix_expression) == MEMBER_REF
6357                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6358               postfix_expression = (build_offset_ref_call_from_tree
6359                                     (postfix_expression, &args,
6360                                      complain));
6361             else if (idk == CP_ID_KIND_QUALIFIED)
6362               /* A call to a static class member, or a namespace-scope
6363                  function.  */
6364               postfix_expression
6365                 = finish_call_expr (postfix_expression, &args,
6366                                     /*disallow_virtual=*/true,
6367                                     koenig_p,
6368                                     complain);
6369             else
6370               /* All other function calls.  */
6371               postfix_expression
6372                 = finish_call_expr (postfix_expression, &args,
6373                                     /*disallow_virtual=*/false,
6374                                     koenig_p,
6375                                     complain);
6376
6377             protected_set_expr_location (postfix_expression, token->location);
6378
6379             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
6380             idk = CP_ID_KIND_NONE;
6381
6382             release_tree_vector (args);
6383           }
6384           break;
6385
6386         case CPP_DOT:
6387         case CPP_DEREF:
6388           /* postfix-expression . template [opt] id-expression
6389              postfix-expression . pseudo-destructor-name
6390              postfix-expression -> template [opt] id-expression
6391              postfix-expression -> pseudo-destructor-name */
6392
6393           /* Consume the `.' or `->' operator.  */
6394           cp_lexer_consume_token (parser->lexer);
6395
6396           postfix_expression
6397             = cp_parser_postfix_dot_deref_expression (parser, token->type,
6398                                                       postfix_expression,
6399                                                       false, &idk, loc);
6400
6401           is_member_access = true;
6402           break;
6403
6404         case CPP_PLUS_PLUS:
6405           /* postfix-expression ++  */
6406           /* Consume the `++' token.  */
6407           cp_lexer_consume_token (parser->lexer);
6408           /* Generate a representation for the complete expression.  */
6409           postfix_expression
6410             = finish_increment_expr (postfix_expression,
6411                                      POSTINCREMENT_EXPR);
6412           /* Increments may not appear in constant-expressions.  */
6413           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6414             postfix_expression = error_mark_node;
6415           idk = CP_ID_KIND_NONE;
6416           is_member_access = false;
6417           break;
6418
6419         case CPP_MINUS_MINUS:
6420           /* postfix-expression -- */
6421           /* Consume the `--' token.  */
6422           cp_lexer_consume_token (parser->lexer);
6423           /* Generate a representation for the complete expression.  */
6424           postfix_expression
6425             = finish_increment_expr (postfix_expression,
6426                                      POSTDECREMENT_EXPR);
6427           /* Decrements may not appear in constant-expressions.  */
6428           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6429             postfix_expression = error_mark_node;
6430           idk = CP_ID_KIND_NONE;
6431           is_member_access = false;
6432           break;
6433
6434         default:
6435           if (pidk_return != NULL)
6436             * pidk_return = idk;
6437           if (member_access_only_p)
6438             return is_member_access? postfix_expression : error_mark_node;
6439           else
6440             return postfix_expression;
6441         }
6442     }
6443
6444   /* We should never get here.  */
6445   gcc_unreachable ();
6446   return error_mark_node;
6447 }
6448
6449 /* This function parses Cilk Plus array notations.  If a normal array expr. is
6450    parsed then the array index is passed back to the caller through *INIT_INDEX 
6451    and the function returns a NULL_TREE.  If array notation expr. is parsed, 
6452    then *INIT_INDEX is ignored by the caller and the function returns 
6453    a tree of type ARRAY_NOTATION_REF.  If some error occurred it returns 
6454    error_mark_node.  */
6455
6456 static tree
6457 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6458                           tree array_value)
6459 {
6460   cp_token *token = NULL;
6461   tree length_index, stride = NULL_TREE, value_tree, array_type;
6462   if (!array_value || array_value == error_mark_node)
6463     {
6464       cp_parser_skip_to_end_of_statement (parser);
6465       return error_mark_node;
6466     }
6467
6468   array_type = TREE_TYPE (array_value);
6469   
6470   bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6471   parser->colon_corrects_to_scope_p = false;
6472   token = cp_lexer_peek_token (parser->lexer);
6473   
6474   if (!token)
6475     {
6476       cp_parser_error (parser, "expected %<:%> or numeral");
6477       return error_mark_node;
6478     }
6479   else if (token->type == CPP_COLON)
6480     {
6481       /* Consume the ':'.  */
6482       cp_lexer_consume_token (parser->lexer);
6483       
6484       /* If we are here, then we have a case like this A[:].  */
6485       if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6486         {
6487           cp_parser_error (parser, "expected %<]%>");
6488           cp_parser_skip_to_end_of_statement (parser);
6489           return error_mark_node;
6490         }
6491       *init_index = NULL_TREE;
6492       stride = NULL_TREE;
6493       length_index = NULL_TREE;
6494     }
6495   else
6496     {
6497       /* If we are here, then there are three valid possibilities:
6498          1. ARRAY [ EXP ]
6499          2. ARRAY [ EXP : EXP ]
6500          3. ARRAY [ EXP : EXP : EXP ]  */
6501
6502       *init_index = cp_parser_expression (parser);      
6503       if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6504         {  
6505           /* This indicates that we have a normal array expression.  */
6506           parser->colon_corrects_to_scope_p = saved_colon_corrects;
6507           return NULL_TREE;
6508         }
6509       
6510       /* Consume the ':'.  */
6511       cp_lexer_consume_token (parser->lexer);
6512       length_index = cp_parser_expression (parser);
6513       if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6514         {
6515           cp_lexer_consume_token (parser->lexer);
6516           stride = cp_parser_expression (parser);
6517         }
6518     }
6519   parser->colon_corrects_to_scope_p = saved_colon_corrects;
6520
6521   if (*init_index == error_mark_node || length_index == error_mark_node
6522       || stride == error_mark_node || array_type == error_mark_node)
6523     {
6524       if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6525         cp_lexer_consume_token (parser->lexer);
6526       return error_mark_node;
6527     }
6528   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6529
6530   value_tree = build_array_notation_ref (loc, array_value, *init_index, 
6531                                          length_index, stride, array_type);
6532   return value_tree;
6533 }
6534
6535 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6536    by cp_parser_builtin_offsetof.  We're looking for
6537
6538      postfix-expression [ expression ]
6539      postfix-expression [ braced-init-list ] (C++11)
6540
6541    FOR_OFFSETOF is set if we're being called in that context, which
6542    changes how we deal with integer constant expressions.  */
6543
6544 static tree
6545 cp_parser_postfix_open_square_expression (cp_parser *parser,
6546                                           tree postfix_expression,
6547                                           bool for_offsetof,
6548                                           bool decltype_p)
6549 {
6550   tree index = NULL_TREE;
6551   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6552   bool saved_greater_than_is_operator_p;
6553
6554   /* Consume the `[' token.  */
6555   cp_lexer_consume_token (parser->lexer);
6556
6557   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6558   parser->greater_than_is_operator_p = true;
6559
6560   /* Parse the index expression.  */
6561   /* ??? For offsetof, there is a question of what to allow here.  If
6562      offsetof is not being used in an integral constant expression context,
6563      then we *could* get the right answer by computing the value at runtime.
6564      If we are in an integral constant expression context, then we might
6565      could accept any constant expression; hard to say without analysis.
6566      Rather than open the barn door too wide right away, allow only integer
6567      constant expressions here.  */
6568   if (for_offsetof)
6569     index = cp_parser_constant_expression (parser);
6570   else
6571     {
6572       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6573         {
6574           bool expr_nonconst_p;
6575           cp_lexer_set_source_position (parser->lexer);
6576           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6577           index = cp_parser_braced_list (parser, &expr_nonconst_p);
6578           if (flag_cilkplus
6579               && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6580             {
6581               error_at (cp_lexer_peek_token (parser->lexer)->location,
6582                         "braced list index is not allowed with array "
6583                         "notation");
6584               cp_parser_skip_to_end_of_statement (parser);
6585               return error_mark_node;
6586             }
6587         }
6588       else if (flag_cilkplus)
6589         {
6590           /* Here are have these two options:
6591              ARRAY[EXP : EXP]        - Array notation expr with default
6592              stride of 1.
6593              ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6594              stride.  */
6595           tree an_exp = cp_parser_array_notation (loc, parser, &index, 
6596                                                   postfix_expression);
6597           if (an_exp)
6598             return an_exp;
6599         }
6600       else
6601         index = cp_parser_expression (parser);
6602     }
6603
6604   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6605
6606   /* Look for the closing `]'.  */
6607   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6608
6609   /* Build the ARRAY_REF.  */
6610   postfix_expression = grok_array_decl (loc, postfix_expression,
6611                                         index, decltype_p);
6612
6613   /* When not doing offsetof, array references are not permitted in
6614      constant-expressions.  */
6615   if (!for_offsetof
6616       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6617     postfix_expression = error_mark_node;
6618
6619   return postfix_expression;
6620 }
6621
6622 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6623    by cp_parser_builtin_offsetof.  We're looking for
6624
6625      postfix-expression . template [opt] id-expression
6626      postfix-expression . pseudo-destructor-name
6627      postfix-expression -> template [opt] id-expression
6628      postfix-expression -> pseudo-destructor-name
6629
6630    FOR_OFFSETOF is set if we're being called in that context.  That sorta
6631    limits what of the above we'll actually accept, but nevermind.
6632    TOKEN_TYPE is the "." or "->" token, which will already have been
6633    removed from the stream.  */
6634
6635 static tree
6636 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6637                                         enum cpp_ttype token_type,
6638                                         tree postfix_expression,
6639                                         bool for_offsetof, cp_id_kind *idk,
6640                                         location_t location)
6641 {
6642   tree name;
6643   bool dependent_p;
6644   bool pseudo_destructor_p;
6645   tree scope = NULL_TREE;
6646
6647   /* If this is a `->' operator, dereference the pointer.  */
6648   if (token_type == CPP_DEREF)
6649     postfix_expression = build_x_arrow (location, postfix_expression,
6650                                         tf_warning_or_error);
6651   /* Check to see whether or not the expression is type-dependent.  */
6652   dependent_p = type_dependent_expression_p (postfix_expression);
6653   /* The identifier following the `->' or `.' is not qualified.  */
6654   parser->scope = NULL_TREE;
6655   parser->qualifying_scope = NULL_TREE;
6656   parser->object_scope = NULL_TREE;
6657   *idk = CP_ID_KIND_NONE;
6658
6659   /* Enter the scope corresponding to the type of the object
6660      given by the POSTFIX_EXPRESSION.  */
6661   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6662     {
6663       scope = TREE_TYPE (postfix_expression);
6664       /* According to the standard, no expression should ever have
6665          reference type.  Unfortunately, we do not currently match
6666          the standard in this respect in that our internal representation
6667          of an expression may have reference type even when the standard
6668          says it does not.  Therefore, we have to manually obtain the
6669          underlying type here.  */
6670       scope = non_reference (scope);
6671       /* The type of the POSTFIX_EXPRESSION must be complete.  */
6672       if (scope == unknown_type_node)
6673         {
6674           error_at (location, "%qE does not have class type",
6675                     postfix_expression);
6676           scope = NULL_TREE;
6677         }
6678       /* Unlike the object expression in other contexts, *this is not
6679          required to be of complete type for purposes of class member
6680          access (5.2.5) outside the member function body.  */
6681       else if (postfix_expression != current_class_ref
6682                && !(processing_template_decl && scope == current_class_type))
6683         scope = complete_type_or_else (scope, NULL_TREE);
6684       /* Let the name lookup machinery know that we are processing a
6685          class member access expression.  */
6686       parser->context->object_type = scope;
6687       /* If something went wrong, we want to be able to discern that case,
6688          as opposed to the case where there was no SCOPE due to the type
6689          of expression being dependent.  */
6690       if (!scope)
6691         scope = error_mark_node;
6692       /* If the SCOPE was erroneous, make the various semantic analysis
6693          functions exit quickly -- and without issuing additional error
6694          messages.  */
6695       if (scope == error_mark_node)
6696         postfix_expression = error_mark_node;
6697     }
6698
6699   /* Assume this expression is not a pseudo-destructor access.  */
6700   pseudo_destructor_p = false;
6701
6702   /* If the SCOPE is a scalar type, then, if this is a valid program,
6703      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
6704      is type dependent, it can be pseudo-destructor-name or something else.
6705      Try to parse it as pseudo-destructor-name first.  */
6706   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6707     {
6708       tree s;
6709       tree type;
6710
6711       cp_parser_parse_tentatively (parser);
6712       /* Parse the pseudo-destructor-name.  */
6713       s = NULL_TREE;
6714       cp_parser_pseudo_destructor_name (parser, postfix_expression,
6715                                         &s, &type);
6716       if (dependent_p
6717           && (cp_parser_error_occurred (parser)
6718               || !SCALAR_TYPE_P (type)))
6719         cp_parser_abort_tentative_parse (parser);
6720       else if (cp_parser_parse_definitely (parser))
6721         {
6722           pseudo_destructor_p = true;
6723           postfix_expression
6724             = finish_pseudo_destructor_expr (postfix_expression,
6725                                              s, type, location);
6726         }
6727     }
6728
6729   if (!pseudo_destructor_p)
6730     {
6731       /* If the SCOPE is not a scalar type, we are looking at an
6732          ordinary class member access expression, rather than a
6733          pseudo-destructor-name.  */
6734       bool template_p;
6735       cp_token *token = cp_lexer_peek_token (parser->lexer);
6736       /* Parse the id-expression.  */
6737       name = (cp_parser_id_expression
6738               (parser,
6739                cp_parser_optional_template_keyword (parser),
6740                /*check_dependency_p=*/true,
6741                &template_p,
6742                /*declarator_p=*/false,
6743                /*optional_p=*/false));
6744       /* In general, build a SCOPE_REF if the member name is qualified.
6745          However, if the name was not dependent and has already been
6746          resolved; there is no need to build the SCOPE_REF.  For example;
6747
6748              struct X { void f(); };
6749              template <typename T> void f(T* t) { t->X::f(); }
6750
6751          Even though "t" is dependent, "X::f" is not and has been resolved
6752          to a BASELINK; there is no need to include scope information.  */
6753
6754       /* But we do need to remember that there was an explicit scope for
6755          virtual function calls.  */
6756       if (parser->scope)
6757         *idk = CP_ID_KIND_QUALIFIED;
6758
6759       /* If the name is a template-id that names a type, we will get a
6760          TYPE_DECL here.  That is invalid code.  */
6761       if (TREE_CODE (name) == TYPE_DECL)
6762         {
6763           error_at (token->location, "invalid use of %qD", name);
6764           postfix_expression = error_mark_node;
6765         }
6766       else
6767         {
6768           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6769             {
6770               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6771                 {
6772                   error_at (token->location, "%<%D::%D%> is not a class member",
6773                             parser->scope, name);
6774                   postfix_expression = error_mark_node;
6775                 }
6776               else
6777                 name = build_qualified_name (/*type=*/NULL_TREE,
6778                                              parser->scope,
6779                                              name,
6780                                              template_p);
6781               parser->scope = NULL_TREE;
6782               parser->qualifying_scope = NULL_TREE;
6783               parser->object_scope = NULL_TREE;
6784             }
6785           if (parser->scope && name && BASELINK_P (name))
6786             adjust_result_of_qualified_name_lookup
6787               (name, parser->scope, scope);
6788           postfix_expression
6789             = finish_class_member_access_expr (postfix_expression, name,
6790                                                template_p, 
6791                                                tf_warning_or_error);
6792         }
6793     }
6794
6795   /* We no longer need to look up names in the scope of the object on
6796      the left-hand side of the `.' or `->' operator.  */
6797   parser->context->object_type = NULL_TREE;
6798
6799   /* Outside of offsetof, these operators may not appear in
6800      constant-expressions.  */
6801   if (!for_offsetof
6802       && (cp_parser_non_integral_constant_expression
6803           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6804     postfix_expression = error_mark_node;
6805
6806   return postfix_expression;
6807 }
6808
6809 /* Cache of LITERAL_ZERO_P constants.  */
6810
6811 static GTY(()) tree literal_zeros[itk_none];
6812
6813 /* Parse a parenthesized expression-list.
6814
6815    expression-list:
6816      assignment-expression
6817      expression-list, assignment-expression
6818
6819    attribute-list:
6820      expression-list
6821      identifier
6822      identifier, expression-list
6823
6824    CAST_P is true if this expression is the target of a cast.
6825
6826    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6827    argument pack.
6828
6829    Returns a vector of trees.  Each element is a representation of an
6830    assignment-expression.  NULL is returned if the ( and or ) are
6831    missing.  An empty, but allocated, vector is returned on no
6832    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6833    if we are parsing an attribute list for an attribute that wants a
6834    plain identifier argument, normal_attr for an attribute that wants
6835    an expression, or non_attr if we aren't parsing an attribute list.  If
6836    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6837    not all of the expressions in the list were constant.
6838    WANT_LITERAL_ZERO_P is true if the caller is interested in
6839    LITERAL_ZERO_P INTEGER_CSTs.  FIXME: once we don't fold everything
6840    immediately, this can be removed.  */
6841
6842 static vec<tree, va_gc> *
6843 cp_parser_parenthesized_expression_list (cp_parser* parser,
6844                                          int is_attribute_list,
6845                                          bool cast_p,
6846                                          bool allow_expansion_p,
6847                                          bool *non_constant_p,
6848                                          bool want_literal_zero_p)
6849 {
6850   vec<tree, va_gc> *expression_list;
6851   bool fold_expr_p = is_attribute_list != non_attr;
6852   tree identifier = NULL_TREE;
6853   bool saved_greater_than_is_operator_p;
6854
6855   /* Assume all the expressions will be constant.  */
6856   if (non_constant_p)
6857     *non_constant_p = false;
6858
6859   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6860     return NULL;
6861
6862   expression_list = make_tree_vector ();
6863
6864   /* Within a parenthesized expression, a `>' token is always
6865      the greater-than operator.  */
6866   saved_greater_than_is_operator_p
6867     = parser->greater_than_is_operator_p;
6868   parser->greater_than_is_operator_p = true;
6869
6870   /* Consume expressions until there are no more.  */
6871   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6872     while (true)
6873       {
6874         tree expr;
6875
6876         /* At the beginning of attribute lists, check to see if the
6877            next token is an identifier.  */
6878         if (is_attribute_list == id_attr
6879             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6880           {
6881             cp_token *token;
6882
6883             /* Consume the identifier.  */
6884             token = cp_lexer_consume_token (parser->lexer);
6885             /* Save the identifier.  */
6886             identifier = token->u.value;
6887           }
6888         else
6889           {
6890             bool expr_non_constant_p;
6891
6892             /* Parse the next assignment-expression.  */
6893             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6894               {
6895                 /* A braced-init-list.  */
6896                 cp_lexer_set_source_position (parser->lexer);
6897                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6898                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6899                 if (non_constant_p && expr_non_constant_p)
6900                   *non_constant_p = true;
6901               }
6902             else if (non_constant_p)
6903               {
6904                 expr = (cp_parser_constant_expression
6905                         (parser, /*allow_non_constant_p=*/true,
6906                          &expr_non_constant_p));
6907                 if (expr_non_constant_p)
6908                   *non_constant_p = true;
6909               }
6910             else
6911               {
6912                 expr = NULL_TREE;
6913                 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6914                 switch (tok->type)
6915                   {
6916                   case CPP_NUMBER:
6917                   case CPP_CHAR:
6918                   case CPP_WCHAR:
6919                   case CPP_CHAR16:
6920                   case CPP_CHAR32:
6921                     /* If a parameter is literal zero alone, remember it
6922                        for -Wmemset-transposed-args warning.  */
6923                     if (integer_zerop (tok->u.value)
6924                         && !TREE_OVERFLOW (tok->u.value)
6925                         && want_literal_zero_p
6926                         && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6927                             == CPP_COMMA
6928                             || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6929                                == CPP_CLOSE_PAREN))
6930                       {
6931                         unsigned int i;
6932                         for (i = 0; i < itk_none; ++i)
6933                           if (TREE_TYPE (tok->u.value) == integer_types[i])
6934                             break;
6935                         if (i < itk_none && literal_zeros[i])
6936                           expr = literal_zeros[i];
6937                         else
6938                           {
6939                             expr = copy_node (tok->u.value);
6940                             LITERAL_ZERO_P (expr) = 1;
6941                             if (i < itk_none)
6942                               literal_zeros[i] = expr;
6943                           }
6944                         /* Consume the 0 token (or '\0', 0LL etc.).  */
6945                         cp_lexer_consume_token (parser->lexer);
6946                       }
6947                     break;
6948                   default:
6949                     break;
6950                   }
6951                 if (expr == NULL_TREE)
6952                   expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6953                                                           cast_p);
6954               }
6955
6956             if (fold_expr_p)
6957               expr = instantiate_non_dependent_expr (expr);
6958
6959             /* If we have an ellipsis, then this is an expression
6960                expansion.  */
6961             if (allow_expansion_p
6962                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6963               {
6964                 /* Consume the `...'.  */
6965                 cp_lexer_consume_token (parser->lexer);
6966
6967                 /* Build the argument pack.  */
6968                 expr = make_pack_expansion (expr);
6969               }
6970
6971              /* Add it to the list.  We add error_mark_node
6972                 expressions to the list, so that we can still tell if
6973                 the correct form for a parenthesized expression-list
6974                 is found. That gives better errors.  */
6975             vec_safe_push (expression_list, expr);
6976
6977             if (expr == error_mark_node)
6978               goto skip_comma;
6979           }
6980
6981         /* After the first item, attribute lists look the same as
6982            expression lists.  */
6983         is_attribute_list = non_attr;
6984
6985       get_comma:;
6986         /* If the next token isn't a `,', then we are done.  */
6987         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6988           break;
6989
6990         /* Otherwise, consume the `,' and keep going.  */
6991         cp_lexer_consume_token (parser->lexer);
6992       }
6993
6994   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6995     {
6996       int ending;
6997
6998     skip_comma:;
6999       /* We try and resync to an unnested comma, as that will give the
7000          user better diagnostics.  */
7001       ending = cp_parser_skip_to_closing_parenthesis (parser,
7002                                                       /*recovering=*/true,
7003                                                       /*or_comma=*/true,
7004                                                       /*consume_paren=*/true);
7005       if (ending < 0)
7006         goto get_comma;
7007       if (!ending)
7008         {
7009           parser->greater_than_is_operator_p
7010             = saved_greater_than_is_operator_p;
7011           return NULL;
7012         }
7013     }
7014
7015   parser->greater_than_is_operator_p
7016     = saved_greater_than_is_operator_p;
7017
7018   if (identifier)
7019     vec_safe_insert (expression_list, 0, identifier);
7020
7021   return expression_list;
7022 }
7023
7024 /* Parse a pseudo-destructor-name.
7025
7026    pseudo-destructor-name:
7027      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7028      :: [opt] nested-name-specifier template template-id :: ~ type-name
7029      :: [opt] nested-name-specifier [opt] ~ type-name
7030
7031    If either of the first two productions is used, sets *SCOPE to the
7032    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7033    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7034    or ERROR_MARK_NODE if the parse fails.  */
7035
7036 static void
7037 cp_parser_pseudo_destructor_name (cp_parser* parser,
7038                                   tree object,
7039                                   tree* scope,
7040                                   tree* type)
7041 {
7042   bool nested_name_specifier_p;
7043
7044   /* Handle ~auto.  */
7045   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7046       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7047       && !type_dependent_expression_p (object))
7048     {
7049       if (cxx_dialect < cxx14)
7050         pedwarn (input_location, 0,
7051                  "%<~auto%> only available with "
7052                  "-std=c++14 or -std=gnu++14");
7053       cp_lexer_consume_token (parser->lexer);
7054       cp_lexer_consume_token (parser->lexer);
7055       *scope = NULL_TREE;
7056       *type = TREE_TYPE (object);
7057       return;
7058     }
7059
7060   /* Assume that things will not work out.  */
7061   *type = error_mark_node;
7062
7063   /* Look for the optional `::' operator.  */
7064   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7065   /* Look for the optional nested-name-specifier.  */
7066   nested_name_specifier_p
7067     = (cp_parser_nested_name_specifier_opt (parser,
7068                                             /*typename_keyword_p=*/false,
7069                                             /*check_dependency_p=*/true,
7070                                             /*type_p=*/false,
7071                                             /*is_declaration=*/false)
7072        != NULL_TREE);
7073   /* Now, if we saw a nested-name-specifier, we might be doing the
7074      second production.  */
7075   if (nested_name_specifier_p
7076       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7077     {
7078       /* Consume the `template' keyword.  */
7079       cp_lexer_consume_token (parser->lexer);
7080       /* Parse the template-id.  */
7081       cp_parser_template_id (parser,
7082                              /*template_keyword_p=*/true,
7083                              /*check_dependency_p=*/false,
7084                              class_type,
7085                              /*is_declaration=*/true);
7086       /* Look for the `::' token.  */
7087       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7088     }
7089   /* If the next token is not a `~', then there might be some
7090      additional qualification.  */
7091   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7092     {
7093       /* At this point, we're looking for "type-name :: ~".  The type-name
7094          must not be a class-name, since this is a pseudo-destructor.  So,
7095          it must be either an enum-name, or a typedef-name -- both of which
7096          are just identifiers.  So, we peek ahead to check that the "::"
7097          and "~" tokens are present; if they are not, then we can avoid
7098          calling type_name.  */
7099       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7100           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7101           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7102         {
7103           cp_parser_error (parser, "non-scalar type");
7104           return;
7105         }
7106
7107       /* Look for the type-name.  */
7108       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7109       if (*scope == error_mark_node)
7110         return;
7111
7112       /* Look for the `::' token.  */
7113       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7114     }
7115   else
7116     *scope = NULL_TREE;
7117
7118   /* Look for the `~'.  */
7119   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7120
7121   /* Once we see the ~, this has to be a pseudo-destructor.  */
7122   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7123     cp_parser_commit_to_topmost_tentative_parse (parser);
7124
7125   /* Look for the type-name again.  We are not responsible for
7126      checking that it matches the first type-name.  */
7127   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7128 }
7129
7130 /* Parse a unary-expression.
7131
7132    unary-expression:
7133      postfix-expression
7134      ++ cast-expression
7135      -- cast-expression
7136      unary-operator cast-expression
7137      sizeof unary-expression
7138      sizeof ( type-id )
7139      alignof ( type-id )  [C++0x]
7140      new-expression
7141      delete-expression
7142
7143    GNU Extensions:
7144
7145    unary-expression:
7146      __extension__ cast-expression
7147      __alignof__ unary-expression
7148      __alignof__ ( type-id )
7149      alignof unary-expression  [C++0x]
7150      __real__ cast-expression
7151      __imag__ cast-expression
7152      && identifier
7153      sizeof ( type-id ) { initializer-list , [opt] }
7154      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7155      __alignof__ ( type-id ) { initializer-list , [opt] }
7156
7157    ADDRESS_P is true iff the unary-expression is appearing as the
7158    operand of the `&' operator.   CAST_P is true if this expression is
7159    the target of a cast.
7160
7161    Returns a representation of the expression.  */
7162
7163 static tree
7164 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7165                             bool address_p, bool cast_p, bool decltype_p)
7166 {
7167   cp_token *token;
7168   enum tree_code unary_operator;
7169
7170   /* Peek at the next token.  */
7171   token = cp_lexer_peek_token (parser->lexer);
7172   /* Some keywords give away the kind of expression.  */
7173   if (token->type == CPP_KEYWORD)
7174     {
7175       enum rid keyword = token->keyword;
7176
7177       switch (keyword)
7178         {
7179         case RID_ALIGNOF:
7180         case RID_SIZEOF:
7181           {
7182             tree operand, ret;
7183             enum tree_code op;
7184             location_t first_loc;
7185
7186             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7187             /* Consume the token.  */
7188             cp_lexer_consume_token (parser->lexer);
7189             first_loc = cp_lexer_peek_token (parser->lexer)->location;
7190             /* Parse the operand.  */
7191             operand = cp_parser_sizeof_operand (parser, keyword);
7192
7193             if (TYPE_P (operand))
7194               ret = cxx_sizeof_or_alignof_type (operand, op, true);
7195             else
7196               {
7197                 /* ISO C++ defines alignof only with types, not with
7198                    expressions. So pedwarn if alignof is used with a non-
7199                    type expression. However, __alignof__ is ok.  */
7200                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7201                   pedwarn (token->location, OPT_Wpedantic,
7202                            "ISO C++ does not allow %<alignof%> "
7203                            "with a non-type");
7204
7205                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7206               }
7207             /* For SIZEOF_EXPR, just issue diagnostics, but keep
7208                SIZEOF_EXPR with the original operand.  */
7209             if (op == SIZEOF_EXPR && ret != error_mark_node)
7210               {
7211                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7212                   {
7213                     if (!processing_template_decl && TYPE_P (operand))
7214                       {
7215                         ret = build_min (SIZEOF_EXPR, size_type_node,
7216                                          build1 (NOP_EXPR, operand,
7217                                                  error_mark_node));
7218                         SIZEOF_EXPR_TYPE_P (ret) = 1;
7219                       }
7220                     else
7221                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7222                     TREE_SIDE_EFFECTS (ret) = 0;
7223                     TREE_READONLY (ret) = 1;
7224                   }
7225                 SET_EXPR_LOCATION (ret, first_loc);
7226               }
7227             return ret;
7228           }
7229
7230         case RID_NEW:
7231           return cp_parser_new_expression (parser);
7232
7233         case RID_DELETE:
7234           return cp_parser_delete_expression (parser);
7235
7236         case RID_EXTENSION:
7237           {
7238             /* The saved value of the PEDANTIC flag.  */
7239             int saved_pedantic;
7240             tree expr;
7241
7242             /* Save away the PEDANTIC flag.  */
7243             cp_parser_extension_opt (parser, &saved_pedantic);
7244             /* Parse the cast-expression.  */
7245             expr = cp_parser_simple_cast_expression (parser);
7246             /* Restore the PEDANTIC flag.  */
7247             pedantic = saved_pedantic;
7248
7249             return expr;
7250           }
7251
7252         case RID_REALPART:
7253         case RID_IMAGPART:
7254           {
7255             tree expression;
7256
7257             /* Consume the `__real__' or `__imag__' token.  */
7258             cp_lexer_consume_token (parser->lexer);
7259             /* Parse the cast-expression.  */
7260             expression = cp_parser_simple_cast_expression (parser);
7261             /* Create the complete representation.  */
7262             return build_x_unary_op (token->location,
7263                                      (keyword == RID_REALPART
7264                                       ? REALPART_EXPR : IMAGPART_EXPR),
7265                                      expression,
7266                                      tf_warning_or_error);
7267           }
7268           break;
7269
7270         case RID_TRANSACTION_ATOMIC:
7271         case RID_TRANSACTION_RELAXED:
7272           return cp_parser_transaction_expression (parser, keyword);
7273
7274         case RID_NOEXCEPT:
7275           {
7276             tree expr;
7277             const char *saved_message;
7278             bool saved_integral_constant_expression_p;
7279             bool saved_non_integral_constant_expression_p;
7280             bool saved_greater_than_is_operator_p;
7281
7282             cp_lexer_consume_token (parser->lexer);
7283             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7284
7285             saved_message = parser->type_definition_forbidden_message;
7286             parser->type_definition_forbidden_message
7287               = G_("types may not be defined in %<noexcept%> expressions");
7288
7289             saved_integral_constant_expression_p
7290               = parser->integral_constant_expression_p;
7291             saved_non_integral_constant_expression_p
7292               = parser->non_integral_constant_expression_p;
7293             parser->integral_constant_expression_p = false;
7294
7295             saved_greater_than_is_operator_p
7296               = parser->greater_than_is_operator_p;
7297             parser->greater_than_is_operator_p = true;
7298
7299             ++cp_unevaluated_operand;
7300             ++c_inhibit_evaluation_warnings;
7301             ++cp_noexcept_operand;
7302             expr = cp_parser_expression (parser);
7303             --cp_noexcept_operand;
7304             --c_inhibit_evaluation_warnings;
7305             --cp_unevaluated_operand;
7306
7307             parser->greater_than_is_operator_p
7308               = saved_greater_than_is_operator_p;
7309
7310             parser->integral_constant_expression_p
7311               = saved_integral_constant_expression_p;
7312             parser->non_integral_constant_expression_p
7313               = saved_non_integral_constant_expression_p;
7314
7315             parser->type_definition_forbidden_message = saved_message;
7316
7317             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7318             return finish_noexcept_expr (expr, tf_warning_or_error);
7319           }
7320
7321         default:
7322           break;
7323         }
7324     }
7325
7326   /* Look for the `:: new' and `:: delete', which also signal the
7327      beginning of a new-expression, or delete-expression,
7328      respectively.  If the next token is `::', then it might be one of
7329      these.  */
7330   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7331     {
7332       enum rid keyword;
7333
7334       /* See if the token after the `::' is one of the keywords in
7335          which we're interested.  */
7336       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7337       /* If it's `new', we have a new-expression.  */
7338       if (keyword == RID_NEW)
7339         return cp_parser_new_expression (parser);
7340       /* Similarly, for `delete'.  */
7341       else if (keyword == RID_DELETE)
7342         return cp_parser_delete_expression (parser);
7343     }
7344
7345   /* Look for a unary operator.  */
7346   unary_operator = cp_parser_unary_operator (token);
7347   /* The `++' and `--' operators can be handled similarly, even though
7348      they are not technically unary-operators in the grammar.  */
7349   if (unary_operator == ERROR_MARK)
7350     {
7351       if (token->type == CPP_PLUS_PLUS)
7352         unary_operator = PREINCREMENT_EXPR;
7353       else if (token->type == CPP_MINUS_MINUS)
7354         unary_operator = PREDECREMENT_EXPR;
7355       /* Handle the GNU address-of-label extension.  */
7356       else if (cp_parser_allow_gnu_extensions_p (parser)
7357                && token->type == CPP_AND_AND)
7358         {
7359           tree identifier;
7360           tree expression;
7361           location_t loc = token->location;
7362
7363           /* Consume the '&&' token.  */
7364           cp_lexer_consume_token (parser->lexer);
7365           /* Look for the identifier.  */
7366           identifier = cp_parser_identifier (parser);
7367           /* Create an expression representing the address.  */
7368           expression = finish_label_address_expr (identifier, loc);
7369           if (cp_parser_non_integral_constant_expression (parser,
7370                                                           NIC_ADDR_LABEL))
7371             expression = error_mark_node;
7372           return expression;
7373         }
7374     }
7375   if (unary_operator != ERROR_MARK)
7376     {
7377       tree cast_expression;
7378       tree expression = error_mark_node;
7379       non_integral_constant non_constant_p = NIC_NONE;
7380       location_t loc = token->location;
7381       tsubst_flags_t complain = complain_flags (decltype_p);
7382
7383       /* Consume the operator token.  */
7384       token = cp_lexer_consume_token (parser->lexer);
7385       /* Parse the cast-expression.  */
7386       cast_expression
7387         = cp_parser_cast_expression (parser,
7388                                      unary_operator == ADDR_EXPR,
7389                                      /*cast_p=*/false,
7390                                      /*decltype*/false,
7391                                      pidk);
7392       /* Now, build an appropriate representation.  */
7393       switch (unary_operator)
7394         {
7395         case INDIRECT_REF:
7396           non_constant_p = NIC_STAR;
7397           expression = build_x_indirect_ref (loc, cast_expression,
7398                                              RO_UNARY_STAR,
7399                                              complain);
7400           break;
7401
7402         case ADDR_EXPR:
7403            non_constant_p = NIC_ADDR;
7404           /* Fall through.  */
7405         case BIT_NOT_EXPR:
7406           expression = build_x_unary_op (loc, unary_operator,
7407                                          cast_expression,
7408                                          complain);
7409           break;
7410
7411         case PREINCREMENT_EXPR:
7412         case PREDECREMENT_EXPR:
7413           non_constant_p = unary_operator == PREINCREMENT_EXPR
7414                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7415           /* Fall through.  */
7416         case UNARY_PLUS_EXPR:
7417         case NEGATE_EXPR:
7418         case TRUTH_NOT_EXPR:
7419           expression = finish_unary_op_expr (loc, unary_operator,
7420                                              cast_expression, complain);
7421           break;
7422
7423         default:
7424           gcc_unreachable ();
7425         }
7426
7427       if (non_constant_p != NIC_NONE
7428           && cp_parser_non_integral_constant_expression (parser,
7429                                                          non_constant_p))
7430         expression = error_mark_node;
7431
7432       return expression;
7433     }
7434
7435   return cp_parser_postfix_expression (parser, address_p, cast_p,
7436                                        /*member_access_only_p=*/false,
7437                                        decltype_p,
7438                                        pidk);
7439 }
7440
7441 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
7442    unary-operator, the corresponding tree code is returned.  */
7443
7444 static enum tree_code
7445 cp_parser_unary_operator (cp_token* token)
7446 {
7447   switch (token->type)
7448     {
7449     case CPP_MULT:
7450       return INDIRECT_REF;
7451
7452     case CPP_AND:
7453       return ADDR_EXPR;
7454
7455     case CPP_PLUS:
7456       return UNARY_PLUS_EXPR;
7457
7458     case CPP_MINUS:
7459       return NEGATE_EXPR;
7460
7461     case CPP_NOT:
7462       return TRUTH_NOT_EXPR;
7463
7464     case CPP_COMPL:
7465       return BIT_NOT_EXPR;
7466
7467     default:
7468       return ERROR_MARK;
7469     }
7470 }
7471
7472 /* Parse a new-expression.
7473
7474    new-expression:
7475      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7476      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7477
7478    Returns a representation of the expression.  */
7479
7480 static tree
7481 cp_parser_new_expression (cp_parser* parser)
7482 {
7483   bool global_scope_p;
7484   vec<tree, va_gc> *placement;
7485   tree type;
7486   vec<tree, va_gc> *initializer;
7487   tree nelts = NULL_TREE;
7488   tree ret;
7489
7490   /* Look for the optional `::' operator.  */
7491   global_scope_p
7492     = (cp_parser_global_scope_opt (parser,
7493                                    /*current_scope_valid_p=*/false)
7494        != NULL_TREE);
7495   /* Look for the `new' operator.  */
7496   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7497   /* There's no easy way to tell a new-placement from the
7498      `( type-id )' construct.  */
7499   cp_parser_parse_tentatively (parser);
7500   /* Look for a new-placement.  */
7501   placement = cp_parser_new_placement (parser);
7502   /* If that didn't work out, there's no new-placement.  */
7503   if (!cp_parser_parse_definitely (parser))
7504     {
7505       if (placement != NULL)
7506         release_tree_vector (placement);
7507       placement = NULL;
7508     }
7509
7510   /* If the next token is a `(', then we have a parenthesized
7511      type-id.  */
7512   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7513     {
7514       cp_token *token;
7515       const char *saved_message = parser->type_definition_forbidden_message;
7516
7517       /* Consume the `('.  */
7518       cp_lexer_consume_token (parser->lexer);
7519
7520       /* Parse the type-id.  */
7521       parser->type_definition_forbidden_message
7522         = G_("types may not be defined in a new-expression");
7523       type = cp_parser_type_id (parser);
7524       parser->type_definition_forbidden_message = saved_message;
7525
7526       /* Look for the closing `)'.  */
7527       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7528       token = cp_lexer_peek_token (parser->lexer);
7529       /* There should not be a direct-new-declarator in this production,
7530          but GCC used to allowed this, so we check and emit a sensible error
7531          message for this case.  */
7532       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7533         {
7534           error_at (token->location,
7535                     "array bound forbidden after parenthesized type-id");
7536           inform (token->location, 
7537                   "try removing the parentheses around the type-id");
7538           cp_parser_direct_new_declarator (parser);
7539         }
7540     }
7541   /* Otherwise, there must be a new-type-id.  */
7542   else
7543     type = cp_parser_new_type_id (parser, &nelts);
7544
7545   /* If the next token is a `(' or '{', then we have a new-initializer.  */
7546   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7547       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7548     initializer = cp_parser_new_initializer (parser);
7549   else
7550     initializer = NULL;
7551
7552   /* A new-expression may not appear in an integral constant
7553      expression.  */
7554   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7555     ret = error_mark_node;
7556   else
7557     {
7558       /* Create a representation of the new-expression.  */
7559       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7560                        tf_warning_or_error);
7561     }
7562
7563   if (placement != NULL)
7564     release_tree_vector (placement);
7565   if (initializer != NULL)
7566     release_tree_vector (initializer);
7567
7568   return ret;
7569 }
7570
7571 /* Parse a new-placement.
7572
7573    new-placement:
7574      ( expression-list )
7575
7576    Returns the same representation as for an expression-list.  */
7577
7578 static vec<tree, va_gc> *
7579 cp_parser_new_placement (cp_parser* parser)
7580 {
7581   vec<tree, va_gc> *expression_list;
7582
7583   /* Parse the expression-list.  */
7584   expression_list = (cp_parser_parenthesized_expression_list
7585                      (parser, non_attr, /*cast_p=*/false,
7586                       /*allow_expansion_p=*/true,
7587                       /*non_constant_p=*/NULL));
7588
7589   return expression_list;
7590 }
7591
7592 /* Parse a new-type-id.
7593
7594    new-type-id:
7595      type-specifier-seq new-declarator [opt]
7596
7597    Returns the TYPE allocated.  If the new-type-id indicates an array
7598    type, *NELTS is set to the number of elements in the last array
7599    bound; the TYPE will not include the last array bound.  */
7600
7601 static tree
7602 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7603 {
7604   cp_decl_specifier_seq type_specifier_seq;
7605   cp_declarator *new_declarator;
7606   cp_declarator *declarator;
7607   cp_declarator *outer_declarator;
7608   const char *saved_message;
7609
7610   /* The type-specifier sequence must not contain type definitions.
7611      (It cannot contain declarations of new types either, but if they
7612      are not definitions we will catch that because they are not
7613      complete.)  */
7614   saved_message = parser->type_definition_forbidden_message;
7615   parser->type_definition_forbidden_message
7616     = G_("types may not be defined in a new-type-id");
7617   /* Parse the type-specifier-seq.  */
7618   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7619                                 /*is_trailing_return=*/false,
7620                                 &type_specifier_seq);
7621   /* Restore the old message.  */
7622   parser->type_definition_forbidden_message = saved_message;
7623
7624   if (type_specifier_seq.type == error_mark_node)
7625     return error_mark_node;
7626
7627   /* Parse the new-declarator.  */
7628   new_declarator = cp_parser_new_declarator_opt (parser);
7629
7630   /* Determine the number of elements in the last array dimension, if
7631      any.  */
7632   *nelts = NULL_TREE;
7633   /* Skip down to the last array dimension.  */
7634   declarator = new_declarator;
7635   outer_declarator = NULL;
7636   while (declarator && (declarator->kind == cdk_pointer
7637                         || declarator->kind == cdk_ptrmem))
7638     {
7639       outer_declarator = declarator;
7640       declarator = declarator->declarator;
7641     }
7642   while (declarator
7643          && declarator->kind == cdk_array
7644          && declarator->declarator
7645          && declarator->declarator->kind == cdk_array)
7646     {
7647       outer_declarator = declarator;
7648       declarator = declarator->declarator;
7649     }
7650
7651   if (declarator && declarator->kind == cdk_array)
7652     {
7653       *nelts = declarator->u.array.bounds;
7654       if (*nelts == error_mark_node)
7655         *nelts = integer_one_node;
7656
7657       if (outer_declarator)
7658         outer_declarator->declarator = declarator->declarator;
7659       else
7660         new_declarator = NULL;
7661     }
7662
7663   return groktypename (&type_specifier_seq, new_declarator, false);
7664 }
7665
7666 /* Parse an (optional) new-declarator.
7667
7668    new-declarator:
7669      ptr-operator new-declarator [opt]
7670      direct-new-declarator
7671
7672    Returns the declarator.  */
7673
7674 static cp_declarator *
7675 cp_parser_new_declarator_opt (cp_parser* parser)
7676 {
7677   enum tree_code code;
7678   tree type, std_attributes = NULL_TREE;
7679   cp_cv_quals cv_quals;  
7680
7681   /* We don't know if there's a ptr-operator next, or not.  */
7682   cp_parser_parse_tentatively (parser);
7683   /* Look for a ptr-operator.  */
7684   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7685   /* If that worked, look for more new-declarators.  */
7686   if (cp_parser_parse_definitely (parser))
7687     {
7688       cp_declarator *declarator;
7689
7690       /* Parse another optional declarator.  */
7691       declarator = cp_parser_new_declarator_opt (parser);
7692
7693       declarator = cp_parser_make_indirect_declarator
7694         (code, type, cv_quals, declarator, std_attributes);
7695
7696       return declarator;
7697     }
7698
7699   /* If the next token is a `[', there is a direct-new-declarator.  */
7700   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7701     return cp_parser_direct_new_declarator (parser);
7702
7703   return NULL;
7704 }
7705
7706 /* Parse a direct-new-declarator.
7707
7708    direct-new-declarator:
7709      [ expression ]
7710      direct-new-declarator [constant-expression]
7711
7712    */
7713
7714 static cp_declarator *
7715 cp_parser_direct_new_declarator (cp_parser* parser)
7716 {
7717   cp_declarator *declarator = NULL;
7718
7719   while (true)
7720     {
7721       tree expression;
7722       cp_token *token;
7723
7724       /* Look for the opening `['.  */
7725       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7726
7727       token = cp_lexer_peek_token (parser->lexer);
7728       expression = cp_parser_expression (parser);
7729       /* The standard requires that the expression have integral
7730          type.  DR 74 adds enumeration types.  We believe that the
7731          real intent is that these expressions be handled like the
7732          expression in a `switch' condition, which also allows
7733          classes with a single conversion to integral or
7734          enumeration type.  */
7735       if (!processing_template_decl)
7736         {
7737           expression
7738             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7739                                           expression,
7740                                           /*complain=*/true);
7741           if (!expression)
7742             {
7743               error_at (token->location,
7744                         "expression in new-declarator must have integral "
7745                         "or enumeration type");
7746               expression = error_mark_node;
7747             }
7748         }
7749
7750       /* Look for the closing `]'.  */
7751       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7752
7753       /* Add this bound to the declarator.  */
7754       declarator = make_array_declarator (declarator, expression);
7755
7756       /* If the next token is not a `[', then there are no more
7757          bounds.  */
7758       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7759         break;
7760     }
7761
7762   return declarator;
7763 }
7764
7765 /* Parse a new-initializer.
7766
7767    new-initializer:
7768      ( expression-list [opt] )
7769      braced-init-list
7770
7771    Returns a representation of the expression-list.  */
7772
7773 static vec<tree, va_gc> *
7774 cp_parser_new_initializer (cp_parser* parser)
7775 {
7776   vec<tree, va_gc> *expression_list;
7777
7778   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7779     {
7780       tree t;
7781       bool expr_non_constant_p;
7782       cp_lexer_set_source_position (parser->lexer);
7783       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7784       t = cp_parser_braced_list (parser, &expr_non_constant_p);
7785       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7786       expression_list = make_tree_vector_single (t);
7787     }
7788   else
7789     expression_list = (cp_parser_parenthesized_expression_list
7790                        (parser, non_attr, /*cast_p=*/false,
7791                         /*allow_expansion_p=*/true,
7792                         /*non_constant_p=*/NULL));
7793
7794   return expression_list;
7795 }
7796
7797 /* Parse a delete-expression.
7798
7799    delete-expression:
7800      :: [opt] delete cast-expression
7801      :: [opt] delete [ ] cast-expression
7802
7803    Returns a representation of the expression.  */
7804
7805 static tree
7806 cp_parser_delete_expression (cp_parser* parser)
7807 {
7808   bool global_scope_p;
7809   bool array_p;
7810   tree expression;
7811
7812   /* Look for the optional `::' operator.  */
7813   global_scope_p
7814     = (cp_parser_global_scope_opt (parser,
7815                                    /*current_scope_valid_p=*/false)
7816        != NULL_TREE);
7817   /* Look for the `delete' keyword.  */
7818   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7819   /* See if the array syntax is in use.  */
7820   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7821     {
7822       /* Consume the `[' token.  */
7823       cp_lexer_consume_token (parser->lexer);
7824       /* Look for the `]' token.  */
7825       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7826       /* Remember that this is the `[]' construct.  */
7827       array_p = true;
7828     }
7829   else
7830     array_p = false;
7831
7832   /* Parse the cast-expression.  */
7833   expression = cp_parser_simple_cast_expression (parser);
7834
7835   /* A delete-expression may not appear in an integral constant
7836      expression.  */
7837   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7838     return error_mark_node;
7839
7840   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7841                         tf_warning_or_error);
7842 }
7843
7844 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7845    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7846    0 otherwise.  */
7847
7848 static int
7849 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7850 {
7851   cp_token *token = cp_lexer_peek_token (parser->lexer);
7852   switch (token->type)
7853     {
7854     case CPP_COMMA:
7855     case CPP_SEMICOLON:
7856     case CPP_QUERY:
7857     case CPP_COLON:
7858     case CPP_CLOSE_SQUARE:
7859     case CPP_CLOSE_PAREN:
7860     case CPP_CLOSE_BRACE:
7861     case CPP_OPEN_BRACE:
7862     case CPP_DOT:
7863     case CPP_DOT_STAR:
7864     case CPP_DEREF:
7865     case CPP_DEREF_STAR:
7866     case CPP_DIV:
7867     case CPP_MOD:
7868     case CPP_LSHIFT:
7869     case CPP_RSHIFT:
7870     case CPP_LESS:
7871     case CPP_GREATER:
7872     case CPP_LESS_EQ:
7873     case CPP_GREATER_EQ:
7874     case CPP_EQ_EQ:
7875     case CPP_NOT_EQ:
7876     case CPP_EQ:
7877     case CPP_MULT_EQ:
7878     case CPP_DIV_EQ:
7879     case CPP_MOD_EQ:
7880     case CPP_PLUS_EQ:
7881     case CPP_MINUS_EQ:
7882     case CPP_RSHIFT_EQ:
7883     case CPP_LSHIFT_EQ:
7884     case CPP_AND_EQ:
7885     case CPP_XOR_EQ:
7886     case CPP_OR_EQ:
7887     case CPP_XOR:
7888     case CPP_OR:
7889     case CPP_OR_OR:
7890     case CPP_EOF:
7891     case CPP_ELLIPSIS:
7892       return 0;
7893
7894     case CPP_OPEN_PAREN:
7895       /* In ((type ()) () the last () isn't a valid cast-expression,
7896          so the whole must be parsed as postfix-expression.  */
7897       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7898              != CPP_CLOSE_PAREN;
7899
7900     case CPP_OPEN_SQUARE:
7901       /* '[' may start a primary-expression in obj-c++ and in C++11,
7902          as a lambda-expression, eg, '(void)[]{}'.  */
7903       if (cxx_dialect >= cxx11)
7904         return -1;
7905       return c_dialect_objc ();
7906
7907     case CPP_PLUS_PLUS:
7908     case CPP_MINUS_MINUS:
7909       /* '++' and '--' may or may not start a cast-expression:
7910
7911          struct T { void operator++(int); };
7912          void f() { (T())++; }
7913
7914          vs
7915
7916          int a;
7917          (int)++a;  */
7918       return -1;
7919
7920     default:
7921       return 1;
7922     }
7923 }
7924
7925 /* Parse a cast-expression.
7926
7927    cast-expression:
7928      unary-expression
7929      ( type-id ) cast-expression
7930
7931    ADDRESS_P is true iff the unary-expression is appearing as the
7932    operand of the `&' operator.   CAST_P is true if this expression is
7933    the target of a cast.
7934
7935    Returns a representation of the expression.  */
7936
7937 static tree
7938 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7939                            bool decltype_p, cp_id_kind * pidk)
7940 {
7941   /* If it's a `(', then we might be looking at a cast.  */
7942   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7943     {
7944       tree type = NULL_TREE;
7945       tree expr = NULL_TREE;
7946       int cast_expression = 0;
7947       const char *saved_message;
7948
7949       /* There's no way to know yet whether or not this is a cast.
7950          For example, `(int (3))' is a unary-expression, while `(int)
7951          3' is a cast.  So, we resort to parsing tentatively.  */
7952       cp_parser_parse_tentatively (parser);
7953       /* Types may not be defined in a cast.  */
7954       saved_message = parser->type_definition_forbidden_message;
7955       parser->type_definition_forbidden_message
7956         = G_("types may not be defined in casts");
7957       /* Consume the `('.  */
7958       cp_lexer_consume_token (parser->lexer);
7959       /* A very tricky bit is that `(struct S) { 3 }' is a
7960          compound-literal (which we permit in C++ as an extension).
7961          But, that construct is not a cast-expression -- it is a
7962          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7963          is legal; if the compound-literal were a cast-expression,
7964          you'd need an extra set of parentheses.)  But, if we parse
7965          the type-id, and it happens to be a class-specifier, then we
7966          will commit to the parse at that point, because we cannot
7967          undo the action that is done when creating a new class.  So,
7968          then we cannot back up and do a postfix-expression.
7969
7970          Another tricky case is the following (c++/29234):
7971
7972          struct S { void operator () (); };
7973
7974          void foo ()
7975          {
7976            ( S()() );
7977          }
7978
7979          As a type-id we parse the parenthesized S()() as a function
7980          returning a function, groktypename complains and we cannot
7981          back up in this case either.
7982
7983          Therefore, we scan ahead to the closing `)', and check to see
7984          if the tokens after the `)' can start a cast-expression.  Otherwise
7985          we are dealing with an unary-expression, a postfix-expression
7986          or something else.
7987
7988          Yet another tricky case, in C++11, is the following (c++/54891):
7989
7990          (void)[]{};
7991
7992          The issue is that usually, besides the case of lambda-expressions,
7993          the parenthesized type-id cannot be followed by '[', and, eg, we
7994          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7995          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7996          we don't commit, we try a cast-expression, then an unary-expression.
7997
7998          Save tokens so that we can put them back.  */
7999       cp_lexer_save_tokens (parser->lexer);
8000
8001       /* We may be looking at a cast-expression.  */
8002       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8003                                                  /*consume_paren=*/true))
8004         cast_expression
8005           = cp_parser_tokens_start_cast_expression (parser);
8006
8007       /* Roll back the tokens we skipped.  */
8008       cp_lexer_rollback_tokens (parser->lexer);
8009       /* If we aren't looking at a cast-expression, simulate an error so
8010          that the call to cp_parser_error_occurred below returns true.  */
8011       if (!cast_expression)
8012         cp_parser_simulate_error (parser);
8013       else
8014         {
8015           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8016           parser->in_type_id_in_expr_p = true;
8017           /* Look for the type-id.  */
8018           type = cp_parser_type_id (parser);
8019           /* Look for the closing `)'.  */
8020           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8021           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8022         }
8023
8024       /* Restore the saved message.  */
8025       parser->type_definition_forbidden_message = saved_message;
8026
8027       /* At this point this can only be either a cast or a
8028          parenthesized ctor such as `(T ())' that looks like a cast to
8029          function returning T.  */
8030       if (!cp_parser_error_occurred (parser))
8031         {
8032           /* Only commit if the cast-expression doesn't start with
8033              '++', '--', or '[' in C++11.  */
8034           if (cast_expression > 0)
8035             cp_parser_commit_to_topmost_tentative_parse (parser);
8036
8037           expr = cp_parser_cast_expression (parser,
8038                                             /*address_p=*/false,
8039                                             /*cast_p=*/true,
8040                                             /*decltype_p=*/false,
8041                                             pidk);
8042
8043           if (cp_parser_parse_definitely (parser))
8044             {
8045               /* Warn about old-style casts, if so requested.  */
8046               if (warn_old_style_cast
8047                   && !in_system_header_at (input_location)
8048                   && !VOID_TYPE_P (type)
8049                   && current_lang_name != lang_name_c)
8050                 warning (OPT_Wold_style_cast, "use of old-style cast");
8051
8052               /* Only type conversions to integral or enumeration types
8053                  can be used in constant-expressions.  */
8054               if (!cast_valid_in_integral_constant_expression_p (type)
8055                   && cp_parser_non_integral_constant_expression (parser,
8056                                                                  NIC_CAST))
8057                 return error_mark_node;
8058
8059               /* Perform the cast.  */
8060               expr = build_c_cast (input_location, type, expr);
8061               return expr;
8062             }
8063         }
8064       else 
8065         cp_parser_abort_tentative_parse (parser);
8066     }
8067
8068   /* If we get here, then it's not a cast, so it must be a
8069      unary-expression.  */
8070   return cp_parser_unary_expression (parser, pidk, address_p,
8071                                      cast_p, decltype_p);
8072 }
8073
8074 /* Parse a binary expression of the general form:
8075
8076    pm-expression:
8077      cast-expression
8078      pm-expression .* cast-expression
8079      pm-expression ->* cast-expression
8080
8081    multiplicative-expression:
8082      pm-expression
8083      multiplicative-expression * pm-expression
8084      multiplicative-expression / pm-expression
8085      multiplicative-expression % pm-expression
8086
8087    additive-expression:
8088      multiplicative-expression
8089      additive-expression + multiplicative-expression
8090      additive-expression - multiplicative-expression
8091
8092    shift-expression:
8093      additive-expression
8094      shift-expression << additive-expression
8095      shift-expression >> additive-expression
8096
8097    relational-expression:
8098      shift-expression
8099      relational-expression < shift-expression
8100      relational-expression > shift-expression
8101      relational-expression <= shift-expression
8102      relational-expression >= shift-expression
8103
8104   GNU Extension:
8105
8106    relational-expression:
8107      relational-expression <? shift-expression
8108      relational-expression >? shift-expression
8109
8110    equality-expression:
8111      relational-expression
8112      equality-expression == relational-expression
8113      equality-expression != relational-expression
8114
8115    and-expression:
8116      equality-expression
8117      and-expression & equality-expression
8118
8119    exclusive-or-expression:
8120      and-expression
8121      exclusive-or-expression ^ and-expression
8122
8123    inclusive-or-expression:
8124      exclusive-or-expression
8125      inclusive-or-expression | exclusive-or-expression
8126
8127    logical-and-expression:
8128      inclusive-or-expression
8129      logical-and-expression && inclusive-or-expression
8130
8131    logical-or-expression:
8132      logical-and-expression
8133      logical-or-expression || logical-and-expression
8134
8135    All these are implemented with a single function like:
8136
8137    binary-expression:
8138      simple-cast-expression
8139      binary-expression <token> binary-expression
8140
8141    CAST_P is true if this expression is the target of a cast.
8142
8143    The binops_by_token map is used to get the tree codes for each <token> type.
8144    binary-expressions are associated according to a precedence table.  */
8145
8146 #define TOKEN_PRECEDENCE(token)                              \
8147 (((token->type == CPP_GREATER                                \
8148    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8149   && !parser->greater_than_is_operator_p)                    \
8150  ? PREC_NOT_OPERATOR                                         \
8151  : binops_by_token[token->type].prec)
8152
8153 static tree
8154 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8155                              bool no_toplevel_fold_p,
8156                              bool decltype_p,
8157                              enum cp_parser_prec prec,
8158                              cp_id_kind * pidk)
8159 {
8160   cp_parser_expression_stack stack;
8161   cp_parser_expression_stack_entry *sp = &stack[0];
8162   cp_parser_expression_stack_entry current;
8163   tree rhs;
8164   cp_token *token;
8165   enum tree_code rhs_type;
8166   enum cp_parser_prec new_prec, lookahead_prec;
8167   tree overload;
8168
8169   /* Parse the first expression.  */
8170   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8171                       ? TRUTH_NOT_EXPR : ERROR_MARK);
8172   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8173                                            cast_p, decltype_p, pidk);
8174   current.prec = prec;
8175
8176   if (cp_parser_error_occurred (parser))
8177     return error_mark_node;
8178
8179   for (;;)
8180     {
8181       /* Get an operator token.  */
8182       token = cp_lexer_peek_token (parser->lexer);
8183
8184       if (warn_cxx0x_compat
8185           && token->type == CPP_RSHIFT
8186           && !parser->greater_than_is_operator_p)
8187         {
8188           if (warning_at (token->location, OPT_Wc__0x_compat,
8189                           "%<>>%> operator is treated"
8190                           " as two right angle brackets in C++11"))
8191             inform (token->location,
8192                     "suggest parentheses around %<>>%> expression");
8193         }
8194
8195       new_prec = TOKEN_PRECEDENCE (token);
8196
8197       /* Popping an entry off the stack means we completed a subexpression:
8198          - either we found a token which is not an operator (`>' where it is not
8199            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8200            will happen repeatedly;
8201          - or, we found an operator which has lower priority.  This is the case
8202            where the recursive descent *ascends*, as in `3 * 4 + 5' after
8203            parsing `3 * 4'.  */
8204       if (new_prec <= current.prec)
8205         {
8206           if (sp == stack)
8207             break;
8208           else
8209             goto pop;
8210         }
8211
8212      get_rhs:
8213       current.tree_type = binops_by_token[token->type].tree_type;
8214       current.loc = token->location;
8215
8216       /* We used the operator token.  */
8217       cp_lexer_consume_token (parser->lexer);
8218
8219       /* For "false && x" or "true || x", x will never be executed;
8220          disable warnings while evaluating it.  */
8221       if (current.tree_type == TRUTH_ANDIF_EXPR)
8222         c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8223       else if (current.tree_type == TRUTH_ORIF_EXPR)
8224         c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8225
8226       /* Extract another operand.  It may be the RHS of this expression
8227          or the LHS of a new, higher priority expression.  */
8228       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8229                   ? TRUTH_NOT_EXPR : ERROR_MARK);
8230       rhs = cp_parser_simple_cast_expression (parser);
8231
8232       /* Get another operator token.  Look up its precedence to avoid
8233          building a useless (immediately popped) stack entry for common
8234          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
8235       token = cp_lexer_peek_token (parser->lexer);
8236       lookahead_prec = TOKEN_PRECEDENCE (token);
8237       if (lookahead_prec > new_prec)
8238         {
8239           /* ... and prepare to parse the RHS of the new, higher priority
8240              expression.  Since precedence levels on the stack are
8241              monotonically increasing, we do not have to care about
8242              stack overflows.  */
8243           *sp = current;
8244           ++sp;
8245           current.lhs = rhs;
8246           current.lhs_type = rhs_type;
8247           current.prec = new_prec;
8248           new_prec = lookahead_prec;
8249           goto get_rhs;
8250
8251          pop:
8252           lookahead_prec = new_prec;
8253           /* If the stack is not empty, we have parsed into LHS the right side
8254              (`4' in the example above) of an expression we had suspended.
8255              We can use the information on the stack to recover the LHS (`3')
8256              from the stack together with the tree code (`MULT_EXPR'), and
8257              the precedence of the higher level subexpression
8258              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
8259              which will be used to actually build the additive expression.  */
8260           rhs = current.lhs;
8261           rhs_type = current.lhs_type;
8262           --sp;
8263           current = *sp;
8264         }
8265
8266       /* Undo the disabling of warnings done above.  */
8267       if (current.tree_type == TRUTH_ANDIF_EXPR)
8268         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8269       else if (current.tree_type == TRUTH_ORIF_EXPR)
8270         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8271
8272       if (warn_logical_not_paren
8273           && current.lhs_type == TRUTH_NOT_EXPR)
8274         warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8275
8276       overload = NULL;
8277       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8278          ERROR_MARK for everything that is not a binary expression.
8279          This makes warn_about_parentheses miss some warnings that
8280          involve unary operators.  For unary expressions we should
8281          pass the correct tree_code unless the unary expression was
8282          surrounded by parentheses.
8283       */
8284       if (no_toplevel_fold_p
8285           && lookahead_prec <= current.prec
8286           && sp == stack)
8287         current.lhs = build2 (current.tree_type,
8288                               TREE_CODE_CLASS (current.tree_type)
8289                               == tcc_comparison
8290                               ? boolean_type_node : TREE_TYPE (current.lhs),
8291                               current.lhs, rhs);
8292       else
8293         current.lhs = build_x_binary_op (current.loc, current.tree_type,
8294                                          current.lhs, current.lhs_type,
8295                                          rhs, rhs_type, &overload,
8296                                          complain_flags (decltype_p));
8297       current.lhs_type = current.tree_type;
8298       if (EXPR_P (current.lhs))
8299         SET_EXPR_LOCATION (current.lhs, current.loc);
8300
8301       /* If the binary operator required the use of an overloaded operator,
8302          then this expression cannot be an integral constant-expression.
8303          An overloaded operator can be used even if both operands are
8304          otherwise permissible in an integral constant-expression if at
8305          least one of the operands is of enumeration type.  */
8306
8307       if (overload
8308           && cp_parser_non_integral_constant_expression (parser,
8309                                                          NIC_OVERLOADED))
8310         return error_mark_node;
8311     }
8312
8313   return current.lhs;
8314 }
8315
8316 static tree
8317 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8318                              bool no_toplevel_fold_p,
8319                              enum cp_parser_prec prec,
8320                              cp_id_kind * pidk)
8321 {
8322   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8323                                       /*decltype*/false, prec, pidk);
8324 }
8325
8326 /* Parse the `? expression : assignment-expression' part of a
8327    conditional-expression.  The LOGICAL_OR_EXPR is the
8328    logical-or-expression that started the conditional-expression.
8329    Returns a representation of the entire conditional-expression.
8330
8331    This routine is used by cp_parser_assignment_expression.
8332
8333      ? expression : assignment-expression
8334
8335    GNU Extensions:
8336
8337      ? : assignment-expression */
8338
8339 static tree
8340 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8341 {
8342   tree expr;
8343   tree assignment_expr;
8344   struct cp_token *token;
8345   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8346
8347   /* Consume the `?' token.  */
8348   cp_lexer_consume_token (parser->lexer);
8349   token = cp_lexer_peek_token (parser->lexer);
8350   if (cp_parser_allow_gnu_extensions_p (parser)
8351       && token->type == CPP_COLON)
8352     {
8353       pedwarn (token->location, OPT_Wpedantic, 
8354                "ISO C++ does not allow ?: with omitted middle operand");
8355       /* Implicit true clause.  */
8356       expr = NULL_TREE;
8357       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8358       warn_for_omitted_condop (token->location, logical_or_expr);
8359     }
8360   else
8361     {
8362       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8363       parser->colon_corrects_to_scope_p = false;
8364       /* Parse the expression.  */
8365       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8366       expr = cp_parser_expression (parser);
8367       c_inhibit_evaluation_warnings +=
8368         ((logical_or_expr == truthvalue_true_node)
8369          - (logical_or_expr == truthvalue_false_node));
8370       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8371     }
8372
8373   /* The next token should be a `:'.  */
8374   cp_parser_require (parser, CPP_COLON, RT_COLON);
8375   /* Parse the assignment-expression.  */
8376   assignment_expr = cp_parser_assignment_expression (parser);
8377   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8378
8379   /* Build the conditional-expression.  */
8380   return build_x_conditional_expr (loc, logical_or_expr,
8381                                    expr,
8382                                    assignment_expr,
8383                                    tf_warning_or_error);
8384 }
8385
8386 /* Parse an assignment-expression.
8387
8388    assignment-expression:
8389      conditional-expression
8390      logical-or-expression assignment-operator assignment_expression
8391      throw-expression
8392
8393    CAST_P is true if this expression is the target of a cast.
8394    DECLTYPE_P is true if this expression is the operand of decltype.
8395
8396    Returns a representation for the expression.  */
8397
8398 static tree
8399 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8400                                  bool cast_p, bool decltype_p)
8401 {
8402   tree expr;
8403
8404   /* If the next token is the `throw' keyword, then we're looking at
8405      a throw-expression.  */
8406   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8407     expr = cp_parser_throw_expression (parser);
8408   /* Otherwise, it must be that we are looking at a
8409      logical-or-expression.  */
8410   else
8411     {
8412       /* Parse the binary expressions (logical-or-expression).  */
8413       expr = cp_parser_binary_expression (parser, cast_p, false,
8414                                           decltype_p,
8415                                           PREC_NOT_OPERATOR, pidk);
8416       /* If the next token is a `?' then we're actually looking at a
8417          conditional-expression.  */
8418       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8419         return cp_parser_question_colon_clause (parser, expr);
8420       else
8421         {
8422           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8423
8424           /* If it's an assignment-operator, we're using the second
8425              production.  */
8426           enum tree_code assignment_operator
8427             = cp_parser_assignment_operator_opt (parser);
8428           if (assignment_operator != ERROR_MARK)
8429             {
8430               bool non_constant_p;
8431               location_t saved_input_location;
8432
8433               /* Parse the right-hand side of the assignment.  */
8434               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8435
8436               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8437                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8438
8439               /* An assignment may not appear in a
8440                  constant-expression.  */
8441               if (cp_parser_non_integral_constant_expression (parser,
8442                                                               NIC_ASSIGNMENT))
8443                 return error_mark_node;
8444               /* Build the assignment expression.  Its default
8445                  location is the location of the '=' token.  */
8446               saved_input_location = input_location;
8447               input_location = loc;
8448               expr = build_x_modify_expr (loc, expr,
8449                                           assignment_operator,
8450                                           rhs,
8451                                           complain_flags (decltype_p));
8452               input_location = saved_input_location;
8453             }
8454         }
8455     }
8456
8457   return expr;
8458 }
8459
8460 /* Parse an (optional) assignment-operator.
8461
8462    assignment-operator: one of
8463      = *= /= %= += -= >>= <<= &= ^= |=
8464
8465    GNU Extension:
8466
8467    assignment-operator: one of
8468      <?= >?=
8469
8470    If the next token is an assignment operator, the corresponding tree
8471    code is returned, and the token is consumed.  For example, for
8472    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
8473    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
8474    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
8475    operator, ERROR_MARK is returned.  */
8476
8477 static enum tree_code
8478 cp_parser_assignment_operator_opt (cp_parser* parser)
8479 {
8480   enum tree_code op;
8481   cp_token *token;
8482
8483   /* Peek at the next token.  */
8484   token = cp_lexer_peek_token (parser->lexer);
8485
8486   switch (token->type)
8487     {
8488     case CPP_EQ:
8489       op = NOP_EXPR;
8490       break;
8491
8492     case CPP_MULT_EQ:
8493       op = MULT_EXPR;
8494       break;
8495
8496     case CPP_DIV_EQ:
8497       op = TRUNC_DIV_EXPR;
8498       break;
8499
8500     case CPP_MOD_EQ:
8501       op = TRUNC_MOD_EXPR;
8502       break;
8503
8504     case CPP_PLUS_EQ:
8505       op = PLUS_EXPR;
8506       break;
8507
8508     case CPP_MINUS_EQ:
8509       op = MINUS_EXPR;
8510       break;
8511
8512     case CPP_RSHIFT_EQ:
8513       op = RSHIFT_EXPR;
8514       break;
8515
8516     case CPP_LSHIFT_EQ:
8517       op = LSHIFT_EXPR;
8518       break;
8519
8520     case CPP_AND_EQ:
8521       op = BIT_AND_EXPR;
8522       break;
8523
8524     case CPP_XOR_EQ:
8525       op = BIT_XOR_EXPR;
8526       break;
8527
8528     case CPP_OR_EQ:
8529       op = BIT_IOR_EXPR;
8530       break;
8531
8532     default:
8533       /* Nothing else is an assignment operator.  */
8534       op = ERROR_MARK;
8535     }
8536
8537   /* If it was an assignment operator, consume it.  */
8538   if (op != ERROR_MARK)
8539     cp_lexer_consume_token (parser->lexer);
8540
8541   return op;
8542 }
8543
8544 /* Parse an expression.
8545
8546    expression:
8547      assignment-expression
8548      expression , assignment-expression
8549
8550    CAST_P is true if this expression is the target of a cast.
8551    DECLTYPE_P is true if this expression is the immediate operand of decltype,
8552      except possibly parenthesized or on the RHS of a comma (N3276).
8553
8554    Returns a representation of the expression.  */
8555
8556 static tree
8557 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8558                       bool cast_p, bool decltype_p)
8559 {
8560   tree expression = NULL_TREE;
8561   location_t loc = UNKNOWN_LOCATION;
8562
8563   while (true)
8564     {
8565       tree assignment_expression;
8566
8567       /* Parse the next assignment-expression.  */
8568       assignment_expression
8569         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8570
8571       /* We don't create a temporary for a call that is the immediate operand
8572          of decltype or on the RHS of a comma.  But when we see a comma, we
8573          need to create a temporary for a call on the LHS.  */
8574       if (decltype_p && !processing_template_decl
8575           && TREE_CODE (assignment_expression) == CALL_EXPR
8576           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8577           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8578         assignment_expression
8579           = build_cplus_new (TREE_TYPE (assignment_expression),
8580                              assignment_expression, tf_warning_or_error);
8581
8582       /* If this is the first assignment-expression, we can just
8583          save it away.  */
8584       if (!expression)
8585         expression = assignment_expression;
8586       else
8587         expression = build_x_compound_expr (loc, expression,
8588                                             assignment_expression,
8589                                             complain_flags (decltype_p));
8590       /* If the next token is not a comma, then we are done with the
8591          expression.  */
8592       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8593         break;
8594       /* Consume the `,'.  */
8595       loc = cp_lexer_peek_token (parser->lexer)->location;
8596       cp_lexer_consume_token (parser->lexer);
8597       /* A comma operator cannot appear in a constant-expression.  */
8598       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8599         expression = error_mark_node;
8600     }
8601
8602   return expression;
8603 }
8604
8605 /* Parse a constant-expression.
8606
8607    constant-expression:
8608      conditional-expression
8609
8610   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8611   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
8612   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
8613   is false, NON_CONSTANT_P should be NULL.  */
8614
8615 static tree
8616 cp_parser_constant_expression (cp_parser* parser,
8617                                bool allow_non_constant_p,
8618                                bool *non_constant_p)
8619 {
8620   bool saved_integral_constant_expression_p;
8621   bool saved_allow_non_integral_constant_expression_p;
8622   bool saved_non_integral_constant_expression_p;
8623   tree expression;
8624
8625   /* It might seem that we could simply parse the
8626      conditional-expression, and then check to see if it were
8627      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
8628      one that the compiler can figure out is constant, possibly after
8629      doing some simplifications or optimizations.  The standard has a
8630      precise definition of constant-expression, and we must honor
8631      that, even though it is somewhat more restrictive.
8632
8633      For example:
8634
8635        int i[(2, 3)];
8636
8637      is not a legal declaration, because `(2, 3)' is not a
8638      constant-expression.  The `,' operator is forbidden in a
8639      constant-expression.  However, GCC's constant-folding machinery
8640      will fold this operation to an INTEGER_CST for `3'.  */
8641
8642   /* Save the old settings.  */
8643   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8644   saved_allow_non_integral_constant_expression_p
8645     = parser->allow_non_integral_constant_expression_p;
8646   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8647   /* We are now parsing a constant-expression.  */
8648   parser->integral_constant_expression_p = true;
8649   parser->allow_non_integral_constant_expression_p
8650     = (allow_non_constant_p || cxx_dialect >= cxx11);
8651   parser->non_integral_constant_expression_p = false;
8652   /* Although the grammar says "conditional-expression", we parse an
8653      "assignment-expression", which also permits "throw-expression"
8654      and the use of assignment operators.  In the case that
8655      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8656      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
8657      actually essential that we look for an assignment-expression.
8658      For example, cp_parser_initializer_clauses uses this function to
8659      determine whether a particular assignment-expression is in fact
8660      constant.  */
8661   expression = cp_parser_assignment_expression (parser);
8662   /* Restore the old settings.  */
8663   parser->integral_constant_expression_p
8664     = saved_integral_constant_expression_p;
8665   parser->allow_non_integral_constant_expression_p
8666     = saved_allow_non_integral_constant_expression_p;
8667   if (cxx_dialect >= cxx11)
8668     {
8669       /* Require an rvalue constant expression here; that's what our
8670          callers expect.  Reference constant expressions are handled
8671          separately in e.g. cp_parser_template_argument.  */
8672       bool is_const = potential_rvalue_constant_expression (expression);
8673       parser->non_integral_constant_expression_p = !is_const;
8674       if (!is_const && !allow_non_constant_p)
8675         require_potential_rvalue_constant_expression (expression);
8676     }
8677   if (allow_non_constant_p)
8678     *non_constant_p = parser->non_integral_constant_expression_p;
8679   parser->non_integral_constant_expression_p
8680     = saved_non_integral_constant_expression_p;
8681
8682   return expression;
8683 }
8684
8685 /* Parse __builtin_offsetof.
8686
8687    offsetof-expression:
8688      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8689
8690    offsetof-member-designator:
8691      id-expression
8692      | offsetof-member-designator "." id-expression
8693      | offsetof-member-designator "[" expression "]"
8694      | offsetof-member-designator "->" id-expression  */
8695
8696 static tree
8697 cp_parser_builtin_offsetof (cp_parser *parser)
8698 {
8699   int save_ice_p, save_non_ice_p;
8700   tree type, expr;
8701   cp_id_kind dummy;
8702   cp_token *token;
8703
8704   /* We're about to accept non-integral-constant things, but will
8705      definitely yield an integral constant expression.  Save and
8706      restore these values around our local parsing.  */
8707   save_ice_p = parser->integral_constant_expression_p;
8708   save_non_ice_p = parser->non_integral_constant_expression_p;
8709
8710   /* Consume the "__builtin_offsetof" token.  */
8711   cp_lexer_consume_token (parser->lexer);
8712   /* Consume the opening `('.  */
8713   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8714   /* Parse the type-id.  */
8715   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8716   type = cp_parser_type_id (parser);
8717   /* Look for the `,'.  */
8718   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8719   token = cp_lexer_peek_token (parser->lexer);
8720
8721   /* Build the (type *)null that begins the traditional offsetof macro.  */
8722   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8723                             tf_warning_or_error);
8724
8725   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
8726   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8727                                                  true, &dummy, token->location);
8728   while (true)
8729     {
8730       token = cp_lexer_peek_token (parser->lexer);
8731       switch (token->type)
8732         {
8733         case CPP_OPEN_SQUARE:
8734           /* offsetof-member-designator "[" expression "]" */
8735           expr = cp_parser_postfix_open_square_expression (parser, expr,
8736                                                            true, false);
8737           break;
8738
8739         case CPP_DEREF:
8740           /* offsetof-member-designator "->" identifier */
8741           expr = grok_array_decl (token->location, expr,
8742                                   integer_zero_node, false);
8743           /* FALLTHRU */
8744
8745         case CPP_DOT:
8746           /* offsetof-member-designator "." identifier */
8747           cp_lexer_consume_token (parser->lexer);
8748           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8749                                                          expr, true, &dummy,
8750                                                          token->location);
8751           break;
8752
8753         case CPP_CLOSE_PAREN:
8754           /* Consume the ")" token.  */
8755           cp_lexer_consume_token (parser->lexer);
8756           goto success;
8757
8758         default:
8759           /* Error.  We know the following require will fail, but
8760              that gives the proper error message.  */
8761           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8762           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8763           expr = error_mark_node;
8764           goto failure;
8765         }
8766     }
8767
8768  success:
8769   expr = finish_offsetof (expr, loc);
8770
8771  failure:
8772   parser->integral_constant_expression_p = save_ice_p;
8773   parser->non_integral_constant_expression_p = save_non_ice_p;
8774
8775   return expr;
8776 }
8777
8778 /* Parse a trait expression.
8779
8780    Returns a representation of the expression, the underlying type
8781    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
8782
8783 static tree
8784 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8785 {
8786   cp_trait_kind kind;
8787   tree type1, type2 = NULL_TREE;
8788   bool binary = false;
8789   bool variadic = false;
8790
8791   switch (keyword)
8792     {
8793     case RID_HAS_NOTHROW_ASSIGN:
8794       kind = CPTK_HAS_NOTHROW_ASSIGN;
8795       break;
8796     case RID_HAS_NOTHROW_CONSTRUCTOR:
8797       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8798       break;
8799     case RID_HAS_NOTHROW_COPY:
8800       kind = CPTK_HAS_NOTHROW_COPY;
8801       break;
8802     case RID_HAS_TRIVIAL_ASSIGN:
8803       kind = CPTK_HAS_TRIVIAL_ASSIGN;
8804       break;
8805     case RID_HAS_TRIVIAL_CONSTRUCTOR:
8806       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8807       break;
8808     case RID_HAS_TRIVIAL_COPY:
8809       kind = CPTK_HAS_TRIVIAL_COPY;
8810       break;
8811     case RID_HAS_TRIVIAL_DESTRUCTOR:
8812       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8813       break;
8814     case RID_HAS_VIRTUAL_DESTRUCTOR:
8815       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8816       break;
8817     case RID_IS_ABSTRACT:
8818       kind = CPTK_IS_ABSTRACT;
8819       break;
8820     case RID_IS_BASE_OF:
8821       kind = CPTK_IS_BASE_OF;
8822       binary = true;
8823       break;
8824     case RID_IS_CLASS:
8825       kind = CPTK_IS_CLASS;
8826       break;
8827     case RID_IS_EMPTY:
8828       kind = CPTK_IS_EMPTY;
8829       break;
8830     case RID_IS_ENUM:
8831       kind = CPTK_IS_ENUM;
8832       break;
8833     case RID_IS_FINAL:
8834       kind = CPTK_IS_FINAL;
8835       break;
8836     case RID_IS_LITERAL_TYPE:
8837       kind = CPTK_IS_LITERAL_TYPE;
8838       break;
8839     case RID_IS_POD:
8840       kind = CPTK_IS_POD;
8841       break;
8842     case RID_IS_POLYMORPHIC:
8843       kind = CPTK_IS_POLYMORPHIC;
8844       break;
8845     case RID_IS_STD_LAYOUT:
8846       kind = CPTK_IS_STD_LAYOUT;
8847       break;
8848     case RID_IS_TRIVIAL:
8849       kind = CPTK_IS_TRIVIAL;
8850       break;
8851     case RID_IS_TRIVIALLY_ASSIGNABLE:
8852       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8853       binary = true;
8854       break;
8855     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8856       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8857       variadic = true;
8858       break;
8859     case RID_IS_TRIVIALLY_COPYABLE:
8860       kind = CPTK_IS_TRIVIALLY_COPYABLE;
8861       break;
8862     case RID_IS_UNION:
8863       kind = CPTK_IS_UNION;
8864       break;
8865     case RID_UNDERLYING_TYPE:
8866       kind = CPTK_UNDERLYING_TYPE;
8867       break;
8868     case RID_BASES:
8869       kind = CPTK_BASES;
8870       break;
8871     case RID_DIRECT_BASES:
8872       kind = CPTK_DIRECT_BASES;
8873       break;
8874     default:
8875       gcc_unreachable ();
8876     }
8877
8878   /* Consume the token.  */
8879   cp_lexer_consume_token (parser->lexer);
8880
8881   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8882
8883   type1 = cp_parser_type_id (parser);
8884
8885   if (type1 == error_mark_node)
8886     return error_mark_node;
8887
8888   if (binary)
8889     {
8890       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8891  
8892       type2 = cp_parser_type_id (parser);
8893
8894       if (type2 == error_mark_node)
8895         return error_mark_node;
8896     }
8897   else if (variadic)
8898     {
8899       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8900         {
8901           cp_lexer_consume_token (parser->lexer);
8902           tree elt = cp_parser_type_id (parser);
8903           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8904             {
8905               cp_lexer_consume_token (parser->lexer);
8906               elt = make_pack_expansion (elt);
8907             }
8908           if (elt == error_mark_node)
8909             return error_mark_node;
8910           type2 = tree_cons (NULL_TREE, elt, type2);
8911         }
8912     }
8913
8914   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8915
8916   /* Complete the trait expression, which may mean either processing
8917      the trait expr now or saving it for template instantiation.  */
8918   switch(kind)
8919     {
8920     case CPTK_UNDERLYING_TYPE:
8921       return finish_underlying_type (type1);
8922     case CPTK_BASES:
8923       return finish_bases (type1, false);
8924     case CPTK_DIRECT_BASES:
8925       return finish_bases (type1, true);
8926     default:
8927       return finish_trait_expr (kind, type1, type2);
8928     }
8929 }
8930
8931 /* Lambdas that appear in variable initializer or default argument scope
8932    get that in their mangling, so we need to record it.  We might as well
8933    use the count for function and namespace scopes as well.  */
8934 static GTY(()) tree lambda_scope;
8935 static GTY(()) int lambda_count;
8936 typedef struct GTY(()) tree_int
8937 {
8938   tree t;
8939   int i;
8940 } tree_int;
8941 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8942
8943 static void
8944 start_lambda_scope (tree decl)
8945 {
8946   tree_int ti;
8947   gcc_assert (decl);
8948   /* Once we're inside a function, we ignore other scopes and just push
8949      the function again so that popping works properly.  */
8950   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8951     decl = current_function_decl;
8952   ti.t = lambda_scope;
8953   ti.i = lambda_count;
8954   vec_safe_push (lambda_scope_stack, ti);
8955   if (lambda_scope != decl)
8956     {
8957       /* Don't reset the count if we're still in the same function.  */
8958       lambda_scope = decl;
8959       lambda_count = 0;
8960     }
8961 }
8962
8963 static void
8964 record_lambda_scope (tree lambda)
8965 {
8966   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8967   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8968 }
8969
8970 static void
8971 finish_lambda_scope (void)
8972 {
8973   tree_int *p = &lambda_scope_stack->last ();
8974   if (lambda_scope != p->t)
8975     {
8976       lambda_scope = p->t;
8977       lambda_count = p->i;
8978     }
8979   lambda_scope_stack->pop ();
8980 }
8981
8982 /* Parse a lambda expression.
8983
8984    lambda-expression:
8985      lambda-introducer lambda-declarator [opt] compound-statement
8986
8987    Returns a representation of the expression.  */
8988
8989 static tree
8990 cp_parser_lambda_expression (cp_parser* parser)
8991 {
8992   tree lambda_expr = build_lambda_expr ();
8993   tree type;
8994   bool ok = true;
8995   cp_token *token = cp_lexer_peek_token (parser->lexer);
8996   cp_token_position start = 0;
8997
8998   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8999
9000   if (cp_unevaluated_operand)
9001     {
9002       if (!token->error_reported)
9003         {
9004           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9005                     "lambda-expression in unevaluated context");
9006           token->error_reported = true;
9007         }
9008       ok = false;
9009     }
9010   else if (parser->in_template_argument_list_p)
9011     {
9012       if (!token->error_reported)
9013         {
9014           error_at (token->location, "lambda-expression in template-argument");
9015           token->error_reported = true;
9016         }
9017       ok = false;
9018     }
9019
9020   /* We may be in the middle of deferred access check.  Disable
9021      it now.  */
9022   push_deferring_access_checks (dk_no_deferred);
9023
9024   cp_parser_lambda_introducer (parser, lambda_expr);
9025
9026   type = begin_lambda_type (lambda_expr);
9027   if (type == error_mark_node)
9028     return error_mark_node;
9029
9030   record_lambda_scope (lambda_expr);
9031
9032   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
9033   determine_visibility (TYPE_NAME (type));
9034
9035   /* Now that we've started the type, add the capture fields for any
9036      explicit captures.  */
9037   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9038
9039   {
9040     /* Inside the class, surrounding template-parameter-lists do not apply.  */
9041     unsigned int saved_num_template_parameter_lists
9042         = parser->num_template_parameter_lists;
9043     unsigned char in_statement = parser->in_statement;
9044     bool in_switch_statement_p = parser->in_switch_statement_p;
9045     bool fully_implicit_function_template_p
9046         = parser->fully_implicit_function_template_p;
9047     tree implicit_template_parms = parser->implicit_template_parms;
9048     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9049     bool auto_is_implicit_function_template_parm_p
9050         = parser->auto_is_implicit_function_template_parm_p;
9051
9052     parser->num_template_parameter_lists = 0;
9053     parser->in_statement = 0;
9054     parser->in_switch_statement_p = false;
9055     parser->fully_implicit_function_template_p = false;
9056     parser->implicit_template_parms = 0;
9057     parser->implicit_template_scope = 0;
9058     parser->auto_is_implicit_function_template_parm_p = false;
9059
9060     /* By virtue of defining a local class, a lambda expression has access to
9061        the private variables of enclosing classes.  */
9062
9063     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9064
9065     if (ok)
9066       {
9067         if (!cp_parser_error_occurred (parser)
9068             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9069             && cp_parser_start_tentative_firewall (parser))
9070           start = token;
9071         cp_parser_lambda_body (parser, lambda_expr);
9072       }
9073     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9074       {
9075         if (cp_parser_skip_to_closing_brace (parser))
9076           cp_lexer_consume_token (parser->lexer);
9077       }
9078
9079     /* The capture list was built up in reverse order; fix that now.  */
9080     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9081       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9082
9083     if (ok)
9084       maybe_add_lambda_conv_op (type);
9085
9086     type = finish_struct (type, /*attributes=*/NULL_TREE);
9087
9088     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9089     parser->in_statement = in_statement;
9090     parser->in_switch_statement_p = in_switch_statement_p;
9091     parser->fully_implicit_function_template_p
9092         = fully_implicit_function_template_p;
9093     parser->implicit_template_parms = implicit_template_parms;
9094     parser->implicit_template_scope = implicit_template_scope;
9095     parser->auto_is_implicit_function_template_parm_p
9096         = auto_is_implicit_function_template_parm_p;
9097   }
9098
9099   pop_deferring_access_checks ();
9100
9101   /* This field is only used during parsing of the lambda.  */
9102   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9103
9104   /* This lambda shouldn't have any proxies left at this point.  */
9105   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9106   /* And now that we're done, push proxies for an enclosing lambda.  */
9107   insert_pending_capture_proxies ();
9108
9109   if (ok)
9110     lambda_expr = build_lambda_object (lambda_expr);
9111   else
9112     lambda_expr = error_mark_node;
9113
9114   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9115
9116   return lambda_expr;
9117 }
9118
9119 /* Parse the beginning of a lambda expression.
9120
9121    lambda-introducer:
9122      [ lambda-capture [opt] ]
9123
9124    LAMBDA_EXPR is the current representation of the lambda expression.  */
9125
9126 static void
9127 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9128 {
9129   /* Need commas after the first capture.  */
9130   bool first = true;
9131
9132   /* Eat the leading `['.  */
9133   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9134
9135   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
9136   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9137       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9138     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9139   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9140     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9141
9142   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9143     {
9144       cp_lexer_consume_token (parser->lexer);
9145       first = false;
9146     }
9147
9148   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9149     {
9150       cp_token* capture_token;
9151       tree capture_id;
9152       tree capture_init_expr;
9153       cp_id_kind idk = CP_ID_KIND_NONE;
9154       bool explicit_init_p = false;
9155
9156       enum capture_kind_type
9157       {
9158         BY_COPY,
9159         BY_REFERENCE
9160       };
9161       enum capture_kind_type capture_kind = BY_COPY;
9162
9163       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9164         {
9165           error ("expected end of capture-list");
9166           return;
9167         }
9168
9169       if (first)
9170         first = false;
9171       else
9172         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9173
9174       /* Possibly capture `this'.  */
9175       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9176         {
9177           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9178           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9179             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9180                      "with by-copy capture default");
9181           cp_lexer_consume_token (parser->lexer);
9182           add_capture (lambda_expr,
9183                        /*id=*/this_identifier,
9184                        /*initializer=*/finish_this_expr(),
9185                        /*by_reference_p=*/false,
9186                        explicit_init_p);
9187           continue;
9188         }
9189
9190       /* Remember whether we want to capture as a reference or not.  */
9191       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9192         {
9193           capture_kind = BY_REFERENCE;
9194           cp_lexer_consume_token (parser->lexer);
9195         }
9196
9197       /* Get the identifier.  */
9198       capture_token = cp_lexer_peek_token (parser->lexer);
9199       capture_id = cp_parser_identifier (parser);
9200
9201       if (capture_id == error_mark_node)
9202         /* Would be nice to have a cp_parser_skip_to_closing_x for general
9203            delimiters, but I modified this to stop on unnested ']' as well.  It
9204            was already changed to stop on unnested '}', so the
9205            "closing_parenthesis" name is no more misleading with my change.  */
9206         {
9207           cp_parser_skip_to_closing_parenthesis (parser,
9208                                                  /*recovering=*/true,
9209                                                  /*or_comma=*/true,
9210                                                  /*consume_paren=*/true);
9211           break;
9212         }
9213
9214       /* Find the initializer for this capture.  */
9215       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9216           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9217           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9218         {
9219           bool direct, non_constant;
9220           /* An explicit initializer exists.  */
9221           if (cxx_dialect < cxx14)
9222             pedwarn (input_location, 0,
9223                      "lambda capture initializers "
9224                      "only available with -std=c++14 or -std=gnu++14");
9225           capture_init_expr = cp_parser_initializer (parser, &direct,
9226                                                      &non_constant);
9227           explicit_init_p = true;
9228           if (capture_init_expr == NULL_TREE)
9229             {
9230               error ("empty initializer for lambda init-capture");
9231               capture_init_expr = error_mark_node;
9232             }
9233         }
9234       else
9235         {
9236           const char* error_msg;
9237
9238           /* Turn the identifier into an id-expression.  */
9239           capture_init_expr
9240             = cp_parser_lookup_name_simple (parser, capture_id,
9241                                             capture_token->location);
9242
9243           if (capture_init_expr == error_mark_node)
9244             {
9245               unqualified_name_lookup_error (capture_id);
9246               continue;
9247             }
9248           else if (DECL_P (capture_init_expr)
9249                    && (!VAR_P (capture_init_expr)
9250                        && TREE_CODE (capture_init_expr) != PARM_DECL))
9251             {
9252               error_at (capture_token->location,
9253                         "capture of non-variable %qD ",
9254                         capture_init_expr);
9255               inform (0, "%q+#D declared here", capture_init_expr);
9256               continue;
9257             }
9258           if (VAR_P (capture_init_expr)
9259               && decl_storage_duration (capture_init_expr) != dk_auto)
9260             {
9261               if (pedwarn (capture_token->location, 0, "capture of variable "
9262                            "%qD with non-automatic storage duration",
9263                            capture_init_expr))
9264                 inform (0, "%q+#D declared here", capture_init_expr);
9265               continue;
9266             }
9267
9268           capture_init_expr
9269             = finish_id_expression
9270                 (capture_id,
9271                  capture_init_expr,
9272                  parser->scope,
9273                  &idk,
9274                  /*integral_constant_expression_p=*/false,
9275                  /*allow_non_integral_constant_expression_p=*/false,
9276                  /*non_integral_constant_expression_p=*/NULL,
9277                  /*template_p=*/false,
9278                  /*done=*/true,
9279                  /*address_p=*/false,
9280                  /*template_arg_p=*/false,
9281                  &error_msg,
9282                  capture_token->location);
9283
9284           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9285             {
9286               cp_lexer_consume_token (parser->lexer);
9287               capture_init_expr = make_pack_expansion (capture_init_expr);
9288             }
9289           else
9290             check_for_bare_parameter_packs (capture_init_expr);
9291         }
9292
9293       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9294           && !explicit_init_p)
9295         {
9296           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9297               && capture_kind == BY_COPY)
9298             pedwarn (capture_token->location, 0, "explicit by-copy capture "
9299                      "of %qD redundant with by-copy capture default",
9300                      capture_id);
9301           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9302               && capture_kind == BY_REFERENCE)
9303             pedwarn (capture_token->location, 0, "explicit by-reference "
9304                      "capture of %qD redundant with by-reference capture "
9305                      "default", capture_id);
9306         }
9307
9308       add_capture (lambda_expr,
9309                    capture_id,
9310                    capture_init_expr,
9311                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
9312                    explicit_init_p);
9313     }
9314
9315   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9316 }
9317
9318 /* Parse the (optional) middle of a lambda expression.
9319
9320    lambda-declarator:
9321      < template-parameter-list [opt] >
9322      ( parameter-declaration-clause [opt] )
9323        attribute-specifier [opt]
9324        mutable [opt]
9325        exception-specification [opt]
9326        lambda-return-type-clause [opt]
9327
9328    LAMBDA_EXPR is the current representation of the lambda expression.  */
9329
9330 static bool
9331 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9332 {
9333   /* 5.1.1.4 of the standard says:
9334        If a lambda-expression does not include a lambda-declarator, it is as if
9335        the lambda-declarator were ().
9336      This means an empty parameter list, no attributes, and no exception
9337      specification.  */
9338   tree param_list = void_list_node;
9339   tree attributes = NULL_TREE;
9340   tree exception_spec = NULL_TREE;
9341   tree template_param_list = NULL_TREE;
9342
9343   /* The template-parameter-list is optional, but must begin with
9344      an opening angle if present.  */
9345   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9346     {
9347       if (cxx_dialect < cxx14)
9348         pedwarn (parser->lexer->next_token->location, 0,
9349                  "lambda templates are only available with "
9350                  "-std=c++14 or -std=gnu++14");
9351
9352       cp_lexer_consume_token (parser->lexer);
9353
9354       template_param_list = cp_parser_template_parameter_list (parser);
9355
9356       cp_parser_skip_to_end_of_template_parameter_list (parser);
9357
9358       /* We just processed one more parameter list.  */
9359       ++parser->num_template_parameter_lists;
9360     }
9361
9362   /* The parameter-declaration-clause is optional (unless
9363      template-parameter-list was given), but must begin with an
9364      opening parenthesis if present.  */
9365   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9366     {
9367       cp_lexer_consume_token (parser->lexer);
9368
9369       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9370
9371       /* Parse parameters.  */
9372       param_list = cp_parser_parameter_declaration_clause (parser);
9373
9374       /* Default arguments shall not be specified in the
9375          parameter-declaration-clause of a lambda-declarator.  */
9376       for (tree t = param_list; t; t = TREE_CHAIN (t))
9377         if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9378           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9379                    "default argument specified for lambda parameter");
9380
9381       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9382
9383       attributes = cp_parser_attributes_opt (parser);
9384
9385       /* Parse optional `mutable' keyword.  */
9386       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9387         {
9388           cp_lexer_consume_token (parser->lexer);
9389           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9390         }
9391
9392       /* Parse optional exception specification.  */
9393       exception_spec = cp_parser_exception_specification_opt (parser);
9394
9395       /* Parse optional trailing return type.  */
9396       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9397         {
9398           cp_lexer_consume_token (parser->lexer);
9399           LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9400             = cp_parser_trailing_type_id (parser);
9401         }
9402
9403       /* The function parameters must be in scope all the way until after the
9404          trailing-return-type in case of decltype.  */
9405       pop_bindings_and_leave_scope ();
9406     }
9407   else if (template_param_list != NULL_TREE) // generate diagnostic
9408     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9409
9410   /* Create the function call operator.
9411
9412      Messing with declarators like this is no uglier than building up the
9413      FUNCTION_DECL by hand, and this is less likely to get out of sync with
9414      other code.  */
9415   {
9416     cp_decl_specifier_seq return_type_specs;
9417     cp_declarator* declarator;
9418     tree fco;
9419     int quals;
9420     void *p;
9421
9422     clear_decl_specs (&return_type_specs);
9423     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9424       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9425     else
9426       /* Maybe we will deduce the return type later.  */
9427       return_type_specs.type = make_auto ();
9428
9429     p = obstack_alloc (&declarator_obstack, 0);
9430
9431     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9432                                      sfk_none);
9433
9434     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9435              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9436     declarator = make_call_declarator (declarator, param_list, quals,
9437                                        VIRT_SPEC_UNSPECIFIED,
9438                                        REF_QUAL_NONE,
9439                                        exception_spec,
9440                                        /*late_return_type=*/NULL_TREE);
9441     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9442
9443     fco = grokmethod (&return_type_specs,
9444                       declarator,
9445                       attributes);
9446     if (fco != error_mark_node)
9447       {
9448         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9449         DECL_ARTIFICIAL (fco) = 1;
9450         /* Give the object parameter a different name.  */
9451         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9452         if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9453           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9454       }
9455     if (template_param_list)
9456       {
9457         fco = finish_member_template_decl (fco);
9458         finish_template_decl (template_param_list);
9459         --parser->num_template_parameter_lists;
9460       }
9461     else if (parser->fully_implicit_function_template_p)
9462       fco = finish_fully_implicit_template (parser, fco);
9463
9464     finish_member_declaration (fco);
9465
9466     obstack_free (&declarator_obstack, p);
9467
9468     return (fco != error_mark_node);
9469   }
9470 }
9471
9472 /* Parse the body of a lambda expression, which is simply
9473
9474    compound-statement
9475
9476    but which requires special handling.
9477    LAMBDA_EXPR is the current representation of the lambda expression.  */
9478
9479 static void
9480 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9481 {
9482   bool nested = (current_function_decl != NULL_TREE);
9483   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9484   if (nested)
9485     push_function_context ();
9486   else
9487     /* Still increment function_depth so that we don't GC in the
9488        middle of an expression.  */
9489     ++function_depth;
9490   /* Clear this in case we're in the middle of a default argument.  */
9491   parser->local_variables_forbidden_p = false;
9492
9493   /* Finish the function call operator
9494      - class_specifier
9495      + late_parsing_for_member
9496      + function_definition_after_declarator
9497      + ctor_initializer_opt_and_function_body  */
9498   {
9499     tree fco = lambda_function (lambda_expr);
9500     tree body;
9501     bool done = false;
9502     tree compound_stmt;
9503     tree cap;
9504
9505     /* Let the front end know that we are going to be defining this
9506        function.  */
9507     start_preparsed_function (fco,
9508                               NULL_TREE,
9509                               SF_PRE_PARSED | SF_INCLASS_INLINE);
9510
9511     start_lambda_scope (fco);
9512     body = begin_function_body ();
9513
9514     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9515       goto out;
9516
9517     /* Push the proxies for any explicit captures.  */
9518     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9519          cap = TREE_CHAIN (cap))
9520       build_capture_proxy (TREE_PURPOSE (cap));
9521
9522     compound_stmt = begin_compound_stmt (0);
9523
9524     /* 5.1.1.4 of the standard says:
9525          If a lambda-expression does not include a trailing-return-type, it
9526          is as if the trailing-return-type denotes the following type:
9527           * if the compound-statement is of the form
9528                { return attribute-specifier [opt] expression ; }
9529              the type of the returned expression after lvalue-to-rvalue
9530              conversion (_conv.lval_ 4.1), array-to-pointer conversion
9531              (_conv.array_ 4.2), and function-to-pointer conversion
9532              (_conv.func_ 4.3);
9533           * otherwise, void.  */
9534
9535     /* In a lambda that has neither a lambda-return-type-clause
9536        nor a deducible form, errors should be reported for return statements
9537        in the body.  Since we used void as the placeholder return type, parsing
9538        the body as usual will give such desired behavior.  */
9539     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9540         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9541         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9542       {
9543         tree expr = NULL_TREE;
9544         cp_id_kind idk = CP_ID_KIND_NONE;
9545
9546         /* Parse tentatively in case there's more after the initial return
9547            statement.  */
9548         cp_parser_parse_tentatively (parser);
9549
9550         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9551
9552         expr = cp_parser_expression (parser, &idk);
9553
9554         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9555         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9556
9557         if (cp_parser_parse_definitely (parser))
9558           {
9559             if (!processing_template_decl)
9560               apply_deduced_return_type (fco, lambda_return_type (expr));
9561
9562             /* Will get error here if type not deduced yet.  */
9563             finish_return_stmt (expr);
9564
9565             done = true;
9566           }
9567       }
9568
9569     if (!done)
9570       {
9571         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9572           cp_parser_label_declaration (parser);
9573         cp_parser_statement_seq_opt (parser, NULL_TREE);
9574         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9575       }
9576
9577     finish_compound_stmt (compound_stmt);
9578
9579   out:
9580     finish_function_body (body);
9581     finish_lambda_scope ();
9582
9583     /* Finish the function and generate code for it if necessary.  */
9584     tree fn = finish_function (/*inline*/2);
9585
9586     /* Only expand if the call op is not a template.  */
9587     if (!DECL_TEMPLATE_INFO (fco))
9588       expand_or_defer_fn (fn);
9589   }
9590
9591   parser->local_variables_forbidden_p = local_variables_forbidden_p;
9592   if (nested)
9593     pop_function_context();
9594   else
9595     --function_depth;
9596 }
9597
9598 /* Statements [gram.stmt.stmt]  */
9599
9600 /* Parse a statement.
9601
9602    statement:
9603      labeled-statement
9604      expression-statement
9605      compound-statement
9606      selection-statement
9607      iteration-statement
9608      jump-statement
9609      declaration-statement
9610      try-block
9611
9612   C++11:
9613
9614   statement:
9615     labeled-statement
9616     attribute-specifier-seq (opt) expression-statement
9617     attribute-specifier-seq (opt) compound-statement
9618     attribute-specifier-seq (opt) selection-statement
9619     attribute-specifier-seq (opt) iteration-statement
9620     attribute-specifier-seq (opt) jump-statement
9621     declaration-statement
9622     attribute-specifier-seq (opt) try-block
9623
9624   TM Extension:
9625
9626    statement:
9627      atomic-statement
9628
9629   IN_COMPOUND is true when the statement is nested inside a
9630   cp_parser_compound_statement; this matters for certain pragmas.
9631
9632   If IF_P is not NULL, *IF_P is set to indicate whether the statement
9633   is a (possibly labeled) if statement which is not enclosed in braces
9634   and has an else clause.  This is used to implement -Wparentheses.  */
9635
9636 static void
9637 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9638                      bool in_compound, bool *if_p)
9639 {
9640   tree statement, std_attrs = NULL_TREE;
9641   cp_token *token;
9642   location_t statement_location, attrs_location;
9643
9644  restart:
9645   if (if_p != NULL)
9646     *if_p = false;
9647   /* There is no statement yet.  */
9648   statement = NULL_TREE;
9649
9650   saved_token_sentinel saved_tokens (parser->lexer);
9651   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9652   if (c_dialect_objc ())
9653     /* In obj-c++, seeing '[[' might be the either the beginning of
9654        c++11 attributes, or a nested objc-message-expression.  So
9655        let's parse the c++11 attributes tentatively.  */
9656     cp_parser_parse_tentatively (parser);
9657   std_attrs = cp_parser_std_attribute_spec_seq (parser);
9658   if (c_dialect_objc ())
9659     {
9660       if (!cp_parser_parse_definitely (parser))
9661         std_attrs = NULL_TREE;
9662     }
9663
9664   /* Peek at the next token.  */
9665   token = cp_lexer_peek_token (parser->lexer);
9666   /* Remember the location of the first token in the statement.  */
9667   statement_location = token->location;
9668   /* If this is a keyword, then that will often determine what kind of
9669      statement we have.  */
9670   if (token->type == CPP_KEYWORD)
9671     {
9672       enum rid keyword = token->keyword;
9673
9674       switch (keyword)
9675         {
9676         case RID_CASE:
9677         case RID_DEFAULT:
9678           /* Looks like a labeled-statement with a case label.
9679              Parse the label, and then use tail recursion to parse
9680              the statement.  */
9681           cp_parser_label_for_labeled_statement (parser, std_attrs);
9682           goto restart;
9683
9684         case RID_IF:
9685         case RID_SWITCH:
9686           statement = cp_parser_selection_statement (parser, if_p);
9687           break;
9688
9689         case RID_WHILE:
9690         case RID_DO:
9691         case RID_FOR:
9692           statement = cp_parser_iteration_statement (parser, false);
9693           break;
9694
9695         case RID_CILK_FOR:
9696           if (!flag_cilkplus)
9697             {
9698               error_at (cp_lexer_peek_token (parser->lexer)->location,
9699                         "-fcilkplus must be enabled to use %<_Cilk_for%>");
9700               cp_lexer_consume_token (parser->lexer);
9701               statement = error_mark_node;
9702             }
9703           else
9704             statement = cp_parser_cilk_for (parser, integer_zero_node);
9705           break;
9706
9707         case RID_BREAK:
9708         case RID_CONTINUE:
9709         case RID_RETURN:
9710         case RID_GOTO:
9711           statement = cp_parser_jump_statement (parser);
9712           break;
9713
9714         case RID_CILK_SYNC:
9715           cp_lexer_consume_token (parser->lexer);
9716           if (flag_cilkplus)
9717             {
9718               tree sync_expr = build_cilk_sync ();
9719               SET_EXPR_LOCATION (sync_expr,
9720                                  token->location);
9721               statement = finish_expr_stmt (sync_expr);
9722             }
9723           else
9724             {
9725               error_at (token->location, "-fcilkplus must be enabled to use"
9726                         " %<_Cilk_sync%>");
9727               statement = error_mark_node;
9728             }
9729           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9730           break;
9731
9732           /* Objective-C++ exception-handling constructs.  */
9733         case RID_AT_TRY:
9734         case RID_AT_CATCH:
9735         case RID_AT_FINALLY:
9736         case RID_AT_SYNCHRONIZED:
9737         case RID_AT_THROW:
9738           statement = cp_parser_objc_statement (parser);
9739           break;
9740
9741         case RID_TRY:
9742           statement = cp_parser_try_block (parser);
9743           break;
9744
9745         case RID_NAMESPACE:
9746           /* This must be a namespace alias definition.  */
9747           cp_parser_declaration_statement (parser);
9748           return;
9749           
9750         case RID_TRANSACTION_ATOMIC:
9751         case RID_TRANSACTION_RELAXED:
9752           statement = cp_parser_transaction (parser, keyword);
9753           break;
9754         case RID_TRANSACTION_CANCEL:
9755           statement = cp_parser_transaction_cancel (parser);
9756           break;
9757
9758         default:
9759           /* It might be a keyword like `int' that can start a
9760              declaration-statement.  */
9761           break;
9762         }
9763     }
9764   else if (token->type == CPP_NAME)
9765     {
9766       /* If the next token is a `:', then we are looking at a
9767          labeled-statement.  */
9768       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9769       if (token->type == CPP_COLON)
9770         {
9771           /* Looks like a labeled-statement with an ordinary label.
9772              Parse the label, and then use tail recursion to parse
9773              the statement.  */
9774
9775           cp_parser_label_for_labeled_statement (parser, std_attrs);
9776           goto restart;
9777         }
9778     }
9779   /* Anything that starts with a `{' must be a compound-statement.  */
9780   else if (token->type == CPP_OPEN_BRACE)
9781     statement = cp_parser_compound_statement (parser, NULL, false, false);
9782   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9783      a statement all its own.  */
9784   else if (token->type == CPP_PRAGMA)
9785     {
9786       /* Only certain OpenMP pragmas are attached to statements, and thus
9787          are considered statements themselves.  All others are not.  In
9788          the context of a compound, accept the pragma as a "statement" and
9789          return so that we can check for a close brace.  Otherwise we
9790          require a real statement and must go back and read one.  */
9791       if (in_compound)
9792         cp_parser_pragma (parser, pragma_compound);
9793       else if (!cp_parser_pragma (parser, pragma_stmt))
9794         goto restart;
9795       return;
9796     }
9797   else if (token->type == CPP_EOF)
9798     {
9799       cp_parser_error (parser, "expected statement");
9800       return;
9801     }
9802
9803   /* Everything else must be a declaration-statement or an
9804      expression-statement.  Try for the declaration-statement
9805      first, unless we are looking at a `;', in which case we know that
9806      we have an expression-statement.  */
9807   if (!statement)
9808     {
9809       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9810         {
9811           if (std_attrs != NULL_TREE)
9812             {
9813               /*  Attributes should be parsed as part of the the
9814                   declaration, so let's un-parse them.  */
9815               saved_tokens.rollback();
9816               std_attrs = NULL_TREE;
9817             }
9818
9819           cp_parser_parse_tentatively (parser);
9820           /* Try to parse the declaration-statement.  */
9821           cp_parser_declaration_statement (parser);
9822           /* If that worked, we're done.  */
9823           if (cp_parser_parse_definitely (parser))
9824             return;
9825         }
9826       /* Look for an expression-statement instead.  */
9827       statement = cp_parser_expression_statement (parser, in_statement_expr);
9828     }
9829
9830   /* Set the line number for the statement.  */
9831   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9832     SET_EXPR_LOCATION (statement, statement_location);
9833
9834   /* Note that for now, we don't do anything with c++11 statements
9835      parsed at this level.  */
9836   if (std_attrs != NULL_TREE)
9837     warning_at (attrs_location,
9838                 OPT_Wattributes,
9839                 "attributes at the beginning of statement are ignored");
9840 }
9841
9842 /* Parse the label for a labeled-statement, i.e.
9843
9844    identifier :
9845    case constant-expression :
9846    default :
9847
9848    GNU Extension:
9849    case constant-expression ... constant-expression : statement
9850
9851    When a label is parsed without errors, the label is added to the
9852    parse tree by the finish_* functions, so this function doesn't
9853    have to return the label.  */
9854
9855 static void
9856 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9857 {
9858   cp_token *token;
9859   tree label = NULL_TREE;
9860   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9861
9862   /* The next token should be an identifier.  */
9863   token = cp_lexer_peek_token (parser->lexer);
9864   if (token->type != CPP_NAME
9865       && token->type != CPP_KEYWORD)
9866     {
9867       cp_parser_error (parser, "expected labeled-statement");
9868       return;
9869     }
9870
9871   parser->colon_corrects_to_scope_p = false;
9872   switch (token->keyword)
9873     {
9874     case RID_CASE:
9875       {
9876         tree expr, expr_hi;
9877         cp_token *ellipsis;
9878
9879         /* Consume the `case' token.  */
9880         cp_lexer_consume_token (parser->lexer);
9881         /* Parse the constant-expression.  */
9882         expr = cp_parser_constant_expression (parser);
9883         if (check_for_bare_parameter_packs (expr))
9884           expr = error_mark_node;
9885
9886         ellipsis = cp_lexer_peek_token (parser->lexer);
9887         if (ellipsis->type == CPP_ELLIPSIS)
9888           {
9889             /* Consume the `...' token.  */
9890             cp_lexer_consume_token (parser->lexer);
9891             expr_hi = cp_parser_constant_expression (parser);
9892             if (check_for_bare_parameter_packs (expr_hi))
9893               expr_hi = error_mark_node;
9894
9895             /* We don't need to emit warnings here, as the common code
9896                will do this for us.  */
9897           }
9898         else
9899           expr_hi = NULL_TREE;
9900
9901         if (parser->in_switch_statement_p)
9902           finish_case_label (token->location, expr, expr_hi);
9903         else
9904           error_at (token->location,
9905                     "case label %qE not within a switch statement",
9906                     expr);
9907       }
9908       break;
9909
9910     case RID_DEFAULT:
9911       /* Consume the `default' token.  */
9912       cp_lexer_consume_token (parser->lexer);
9913
9914       if (parser->in_switch_statement_p)
9915         finish_case_label (token->location, NULL_TREE, NULL_TREE);
9916       else
9917         error_at (token->location, "case label not within a switch statement");
9918       break;
9919
9920     default:
9921       /* Anything else must be an ordinary label.  */
9922       label = finish_label_stmt (cp_parser_identifier (parser));
9923       break;
9924     }
9925
9926   /* Require the `:' token.  */
9927   cp_parser_require (parser, CPP_COLON, RT_COLON);
9928
9929   /* An ordinary label may optionally be followed by attributes.
9930      However, this is only permitted if the attributes are then
9931      followed by a semicolon.  This is because, for backward
9932      compatibility, when parsing
9933        lab: __attribute__ ((unused)) int i;
9934      we want the attribute to attach to "i", not "lab".  */
9935   if (label != NULL_TREE
9936       && cp_next_tokens_can_be_gnu_attribute_p (parser))
9937     {
9938       tree attrs;
9939       cp_parser_parse_tentatively (parser);
9940       attrs = cp_parser_gnu_attributes_opt (parser);
9941       if (attrs == NULL_TREE
9942           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9943         cp_parser_abort_tentative_parse (parser);
9944       else if (!cp_parser_parse_definitely (parser))
9945         ;
9946       else
9947         attributes = chainon (attributes, attrs);
9948     }
9949
9950   if (attributes != NULL_TREE)
9951     cplus_decl_attributes (&label, attributes, 0);
9952
9953   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9954 }
9955
9956 /* Parse an expression-statement.
9957
9958    expression-statement:
9959      expression [opt] ;
9960
9961    Returns the new EXPR_STMT -- or NULL_TREE if the expression
9962    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9963    indicates whether this expression-statement is part of an
9964    expression statement.  */
9965
9966 static tree
9967 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9968 {
9969   tree statement = NULL_TREE;
9970   cp_token *token = cp_lexer_peek_token (parser->lexer);
9971
9972   /* If the next token is a ';', then there is no expression
9973      statement.  */
9974   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9975     {
9976       statement = cp_parser_expression (parser);
9977       if (statement == error_mark_node
9978           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9979         {
9980           cp_parser_skip_to_end_of_block_or_statement (parser);
9981           return error_mark_node;
9982         }
9983     }
9984
9985   /* Give a helpful message for "A<T>::type t;" and the like.  */
9986   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9987       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9988     {
9989       if (TREE_CODE (statement) == SCOPE_REF)
9990         error_at (token->location, "need %<typename%> before %qE because "
9991                   "%qT is a dependent scope",
9992                   statement, TREE_OPERAND (statement, 0));
9993       else if (is_overloaded_fn (statement)
9994                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9995         {
9996           /* A::A a; */
9997           tree fn = get_first_fn (statement);
9998           error_at (token->location,
9999                     "%<%T::%D%> names the constructor, not the type",
10000                     DECL_CONTEXT (fn), DECL_NAME (fn));
10001         }
10002     }
10003
10004   /* Consume the final `;'.  */
10005   cp_parser_consume_semicolon_at_end_of_statement (parser);
10006
10007   if (in_statement_expr
10008       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10009     /* This is the final expression statement of a statement
10010        expression.  */
10011     statement = finish_stmt_expr_expr (statement, in_statement_expr);
10012   else if (statement)
10013     statement = finish_expr_stmt (statement);
10014
10015   return statement;
10016 }
10017
10018 /* Parse a compound-statement.
10019
10020    compound-statement:
10021      { statement-seq [opt] }
10022
10023    GNU extension:
10024
10025    compound-statement:
10026      { label-declaration-seq [opt] statement-seq [opt] }
10027
10028    label-declaration-seq:
10029      label-declaration
10030      label-declaration-seq label-declaration
10031
10032    Returns a tree representing the statement.  */
10033
10034 static tree
10035 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10036                               bool in_try, bool function_body)
10037 {
10038   tree compound_stmt;
10039
10040   /* Consume the `{'.  */
10041   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10042     return error_mark_node;
10043   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10044       && !function_body && cxx_dialect < cxx14)
10045     pedwarn (input_location, OPT_Wpedantic,
10046              "compound-statement in constexpr function");
10047   /* Begin the compound-statement.  */
10048   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10049   /* If the next keyword is `__label__' we have a label declaration.  */
10050   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10051     cp_parser_label_declaration (parser);
10052   /* Parse an (optional) statement-seq.  */
10053   cp_parser_statement_seq_opt (parser, in_statement_expr);
10054   /* Finish the compound-statement.  */
10055   finish_compound_stmt (compound_stmt);
10056   /* Consume the `}'.  */
10057   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10058
10059   return compound_stmt;
10060 }
10061
10062 /* Parse an (optional) statement-seq.
10063
10064    statement-seq:
10065      statement
10066      statement-seq [opt] statement  */
10067
10068 static void
10069 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10070 {
10071   /* Scan statements until there aren't any more.  */
10072   while (true)
10073     {
10074       cp_token *token = cp_lexer_peek_token (parser->lexer);
10075
10076       /* If we are looking at a `}', then we have run out of
10077          statements; the same is true if we have reached the end
10078          of file, or have stumbled upon a stray '@end'.  */
10079       if (token->type == CPP_CLOSE_BRACE
10080           || token->type == CPP_EOF
10081           || token->type == CPP_PRAGMA_EOL
10082           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10083         break;
10084       
10085       /* If we are in a compound statement and find 'else' then
10086          something went wrong.  */
10087       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10088         {
10089           if (parser->in_statement & IN_IF_STMT) 
10090             break;
10091           else
10092             {
10093               token = cp_lexer_consume_token (parser->lexer);
10094               error_at (token->location, "%<else%> without a previous %<if%>");
10095             }
10096         }
10097
10098       /* Parse the statement.  */
10099       cp_parser_statement (parser, in_statement_expr, true, NULL);
10100     }
10101 }
10102
10103 /* Parse a selection-statement.
10104
10105    selection-statement:
10106      if ( condition ) statement
10107      if ( condition ) statement else statement
10108      switch ( condition ) statement
10109
10110    Returns the new IF_STMT or SWITCH_STMT.
10111
10112    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10113    is a (possibly labeled) if statement which is not enclosed in
10114    braces and has an else clause.  This is used to implement
10115    -Wparentheses.  */
10116
10117 static tree
10118 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10119 {
10120   cp_token *token;
10121   enum rid keyword;
10122
10123   if (if_p != NULL)
10124     *if_p = false;
10125
10126   /* Peek at the next token.  */
10127   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10128
10129   /* See what kind of keyword it is.  */
10130   keyword = token->keyword;
10131   switch (keyword)
10132     {
10133     case RID_IF:
10134     case RID_SWITCH:
10135       {
10136         tree statement;
10137         tree condition;
10138
10139         /* Look for the `('.  */
10140         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10141           {
10142             cp_parser_skip_to_end_of_statement (parser);
10143             return error_mark_node;
10144           }
10145
10146         /* Begin the selection-statement.  */
10147         if (keyword == RID_IF)
10148           statement = begin_if_stmt ();
10149         else
10150           statement = begin_switch_stmt ();
10151
10152         /* Parse the condition.  */
10153         condition = cp_parser_condition (parser);
10154         /* Look for the `)'.  */
10155         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10156           cp_parser_skip_to_closing_parenthesis (parser, true, false,
10157                                                  /*consume_paren=*/true);
10158
10159         if (keyword == RID_IF)
10160           {
10161             bool nested_if;
10162             unsigned char in_statement;
10163
10164             /* Add the condition.  */
10165             finish_if_stmt_cond (condition, statement);
10166
10167             /* Parse the then-clause.  */
10168             in_statement = parser->in_statement;
10169             parser->in_statement |= IN_IF_STMT;
10170             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10171               {
10172                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10173                 add_stmt (build_empty_stmt (loc));
10174                 cp_lexer_consume_token (parser->lexer);
10175                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10176                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
10177                               "empty body in an %<if%> statement");
10178                 nested_if = false;
10179               }
10180             else
10181               cp_parser_implicitly_scoped_statement (parser, &nested_if);
10182             parser->in_statement = in_statement;
10183
10184             finish_then_clause (statement);
10185
10186             /* If the next token is `else', parse the else-clause.  */
10187             if (cp_lexer_next_token_is_keyword (parser->lexer,
10188                                                 RID_ELSE))
10189               {
10190                 /* Consume the `else' keyword.  */
10191                 cp_lexer_consume_token (parser->lexer);
10192                 begin_else_clause (statement);
10193                 /* Parse the else-clause.  */
10194                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10195                   {
10196                     location_t loc;
10197                     loc = cp_lexer_peek_token (parser->lexer)->location;
10198                     warning_at (loc,
10199                                 OPT_Wempty_body, "suggest braces around "
10200                                 "empty body in an %<else%> statement");
10201                     add_stmt (build_empty_stmt (loc));
10202                     cp_lexer_consume_token (parser->lexer);
10203                   }
10204                 else
10205                   cp_parser_implicitly_scoped_statement (parser, NULL);
10206
10207                 finish_else_clause (statement);
10208
10209                 /* If we are currently parsing a then-clause, then
10210                    IF_P will not be NULL.  We set it to true to
10211                    indicate that this if statement has an else clause.
10212                    This may trigger the Wparentheses warning below
10213                    when we get back up to the parent if statement.  */
10214                 if (if_p != NULL)
10215                   *if_p = true;
10216               }
10217             else
10218               {
10219                 /* This if statement does not have an else clause.  If
10220                    NESTED_IF is true, then the then-clause is an if
10221                    statement which does have an else clause.  We warn
10222                    about the potential ambiguity.  */
10223                 if (nested_if)
10224                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10225                               "suggest explicit braces to avoid ambiguous"
10226                               " %<else%>");
10227               }
10228
10229             /* Now we're all done with the if-statement.  */
10230             finish_if_stmt (statement);
10231           }
10232         else
10233           {
10234             bool in_switch_statement_p;
10235             unsigned char in_statement;
10236
10237             /* Add the condition.  */
10238             finish_switch_cond (condition, statement);
10239
10240             /* Parse the body of the switch-statement.  */
10241             in_switch_statement_p = parser->in_switch_statement_p;
10242             in_statement = parser->in_statement;
10243             parser->in_switch_statement_p = true;
10244             parser->in_statement |= IN_SWITCH_STMT;
10245             cp_parser_implicitly_scoped_statement (parser, NULL);
10246             parser->in_switch_statement_p = in_switch_statement_p;
10247             parser->in_statement = in_statement;
10248
10249             /* Now we're all done with the switch-statement.  */
10250             finish_switch_stmt (statement);
10251           }
10252
10253         return statement;
10254       }
10255       break;
10256
10257     default:
10258       cp_parser_error (parser, "expected selection-statement");
10259       return error_mark_node;
10260     }
10261 }
10262
10263 /* Parse a condition.
10264
10265    condition:
10266      expression
10267      type-specifier-seq declarator = initializer-clause
10268      type-specifier-seq declarator braced-init-list
10269
10270    GNU Extension:
10271
10272    condition:
10273      type-specifier-seq declarator asm-specification [opt]
10274        attributes [opt] = assignment-expression
10275
10276    Returns the expression that should be tested.  */
10277
10278 static tree
10279 cp_parser_condition (cp_parser* parser)
10280 {
10281   cp_decl_specifier_seq type_specifiers;
10282   const char *saved_message;
10283   int declares_class_or_enum;
10284
10285   /* Try the declaration first.  */
10286   cp_parser_parse_tentatively (parser);
10287   /* New types are not allowed in the type-specifier-seq for a
10288      condition.  */
10289   saved_message = parser->type_definition_forbidden_message;
10290   parser->type_definition_forbidden_message
10291     = G_("types may not be defined in conditions");
10292   /* Parse the type-specifier-seq.  */
10293   cp_parser_decl_specifier_seq (parser,
10294                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10295                                 &type_specifiers,
10296                                 &declares_class_or_enum);
10297   /* Restore the saved message.  */
10298   parser->type_definition_forbidden_message = saved_message;
10299   /* If all is well, we might be looking at a declaration.  */
10300   if (!cp_parser_error_occurred (parser))
10301     {
10302       tree decl;
10303       tree asm_specification;
10304       tree attributes;
10305       cp_declarator *declarator;
10306       tree initializer = NULL_TREE;
10307
10308       /* Parse the declarator.  */
10309       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10310                                          /*ctor_dtor_or_conv_p=*/NULL,
10311                                          /*parenthesized_p=*/NULL,
10312                                          /*member_p=*/false,
10313                                          /*friend_p=*/false);
10314       /* Parse the attributes.  */
10315       attributes = cp_parser_attributes_opt (parser);
10316       /* Parse the asm-specification.  */
10317       asm_specification = cp_parser_asm_specification_opt (parser);
10318       /* If the next token is not an `=' or '{', then we might still be
10319          looking at an expression.  For example:
10320
10321            if (A(a).x)
10322
10323          looks like a decl-specifier-seq and a declarator -- but then
10324          there is no `=', so this is an expression.  */
10325       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10326           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10327         cp_parser_simulate_error (parser);
10328         
10329       /* If we did see an `=' or '{', then we are looking at a declaration
10330          for sure.  */
10331       if (cp_parser_parse_definitely (parser))
10332         {
10333           tree pushed_scope;
10334           bool non_constant_p;
10335           bool flags = LOOKUP_ONLYCONVERTING;
10336
10337           /* Create the declaration.  */
10338           decl = start_decl (declarator, &type_specifiers,
10339                              /*initialized_p=*/true,
10340                              attributes, /*prefix_attributes=*/NULL_TREE,
10341                              &pushed_scope);
10342
10343           /* Parse the initializer.  */
10344           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10345             {
10346               initializer = cp_parser_braced_list (parser, &non_constant_p);
10347               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10348               flags = 0;
10349             }
10350           else
10351             {
10352               /* Consume the `='.  */
10353               cp_parser_require (parser, CPP_EQ, RT_EQ);
10354               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10355             }
10356           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10357             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10358
10359           /* Process the initializer.  */
10360           cp_finish_decl (decl,
10361                           initializer, !non_constant_p,
10362                           asm_specification,
10363                           flags);
10364
10365           if (pushed_scope)
10366             pop_scope (pushed_scope);
10367
10368           return convert_from_reference (decl);
10369         }
10370     }
10371   /* If we didn't even get past the declarator successfully, we are
10372      definitely not looking at a declaration.  */
10373   else
10374     cp_parser_abort_tentative_parse (parser);
10375
10376   /* Otherwise, we are looking at an expression.  */
10377   return cp_parser_expression (parser);
10378 }
10379
10380 /* Parses a for-statement or range-for-statement until the closing ')',
10381    not included. */
10382
10383 static tree
10384 cp_parser_for (cp_parser *parser, bool ivdep)
10385 {
10386   tree init, scope, decl;
10387   bool is_range_for;
10388
10389   /* Begin the for-statement.  */
10390   scope = begin_for_scope (&init);
10391
10392   /* Parse the initialization.  */
10393   is_range_for = cp_parser_for_init_statement (parser, &decl);
10394
10395   if (is_range_for)
10396     return cp_parser_range_for (parser, scope, init, decl, ivdep);
10397   else
10398     return cp_parser_c_for (parser, scope, init, ivdep);
10399 }
10400
10401 static tree
10402 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10403 {
10404   /* Normal for loop */
10405   tree condition = NULL_TREE;
10406   tree expression = NULL_TREE;
10407   tree stmt;
10408
10409   stmt = begin_for_stmt (scope, init);
10410   /* The for-init-statement has already been parsed in
10411      cp_parser_for_init_statement, so no work is needed here.  */
10412   finish_for_init_stmt (stmt);
10413
10414   /* If there's a condition, process it.  */
10415   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10416     condition = cp_parser_condition (parser);
10417   else if (ivdep)
10418     {
10419       cp_parser_error (parser, "missing loop condition in loop with "
10420                        "%<GCC ivdep%> pragma");
10421       condition = error_mark_node;
10422     }
10423   finish_for_cond (condition, stmt, ivdep);
10424   /* Look for the `;'.  */
10425   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10426
10427   /* If there's an expression, process it.  */
10428   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10429     expression = cp_parser_expression (parser);
10430   finish_for_expr (expression, stmt);
10431
10432   return stmt;
10433 }
10434
10435 /* Tries to parse a range-based for-statement:
10436
10437   range-based-for:
10438     decl-specifier-seq declarator : expression
10439
10440   The decl-specifier-seq declarator and the `:' are already parsed by
10441   cp_parser_for_init_statement. If processing_template_decl it returns a
10442   newly created RANGE_FOR_STMT; if not, it is converted to a
10443   regular FOR_STMT.  */
10444
10445 static tree
10446 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10447                      bool ivdep)
10448 {
10449   tree stmt, range_expr;
10450
10451   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10452     {
10453       bool expr_non_constant_p;
10454       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10455     }
10456   else
10457     range_expr = cp_parser_expression (parser);
10458
10459   /* If in template, STMT is converted to a normal for-statement
10460      at instantiation. If not, it is done just ahead. */
10461   if (processing_template_decl)
10462     {
10463       if (check_for_bare_parameter_packs (range_expr))
10464         range_expr = error_mark_node;
10465       stmt = begin_range_for_stmt (scope, init);
10466       if (ivdep)
10467         RANGE_FOR_IVDEP (stmt) = 1;
10468       finish_range_for_decl (stmt, range_decl, range_expr);
10469       if (!type_dependent_expression_p (range_expr)
10470           /* do_auto_deduction doesn't mess with template init-lists.  */
10471           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10472         do_range_for_auto_deduction (range_decl, range_expr);
10473     }
10474   else
10475     {
10476       stmt = begin_for_stmt (scope, init);
10477       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10478     }
10479   return stmt;
10480 }
10481
10482 /* Subroutine of cp_convert_range_for: given the initializer expression,
10483    builds up the range temporary.  */
10484
10485 static tree
10486 build_range_temp (tree range_expr)
10487 {
10488   tree range_type, range_temp;
10489
10490   /* Find out the type deduced by the declaration
10491      `auto &&__range = range_expr'.  */
10492   range_type = cp_build_reference_type (make_auto (), true);
10493   range_type = do_auto_deduction (range_type, range_expr,
10494                                   type_uses_auto (range_type));
10495
10496   /* Create the __range variable.  */
10497   range_temp = build_decl (input_location, VAR_DECL,
10498                            get_identifier ("__for_range"), range_type);
10499   TREE_USED (range_temp) = 1;
10500   DECL_ARTIFICIAL (range_temp) = 1;
10501
10502   return range_temp;
10503 }
10504
10505 /* Used by cp_parser_range_for in template context: we aren't going to
10506    do a full conversion yet, but we still need to resolve auto in the
10507    type of the for-range-declaration if present.  This is basically
10508    a shortcut version of cp_convert_range_for.  */
10509
10510 static void
10511 do_range_for_auto_deduction (tree decl, tree range_expr)
10512 {
10513   tree auto_node = type_uses_auto (TREE_TYPE (decl));
10514   if (auto_node)
10515     {
10516       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10517       range_temp = convert_from_reference (build_range_temp (range_expr));
10518       iter_type = (cp_parser_perform_range_for_lookup
10519                    (range_temp, &begin_dummy, &end_dummy));
10520       if (iter_type)
10521         {
10522           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10523                                   iter_type);
10524           iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10525                                             tf_warning_or_error);
10526           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10527                                                 iter_decl, auto_node);
10528         }
10529     }
10530 }
10531
10532 /* Converts a range-based for-statement into a normal
10533    for-statement, as per the definition.
10534
10535       for (RANGE_DECL : RANGE_EXPR)
10536         BLOCK
10537
10538    should be equivalent to:
10539
10540       {
10541         auto &&__range = RANGE_EXPR;
10542         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10543               __begin != __end;
10544               ++__begin)
10545           {
10546               RANGE_DECL = *__begin;
10547               BLOCK
10548           }
10549       }
10550
10551    If RANGE_EXPR is an array:
10552         BEGIN_EXPR = __range
10553         END_EXPR = __range + ARRAY_SIZE(__range)
10554    Else if RANGE_EXPR has a member 'begin' or 'end':
10555         BEGIN_EXPR = __range.begin()
10556         END_EXPR = __range.end()
10557    Else:
10558         BEGIN_EXPR = begin(__range)
10559         END_EXPR = end(__range);
10560
10561    If __range has a member 'begin' but not 'end', or vice versa, we must
10562    still use the second alternative (it will surely fail, however).
10563    When calling begin()/end() in the third alternative we must use
10564    argument dependent lookup, but always considering 'std' as an associated
10565    namespace.  */
10566
10567 tree
10568 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10569                       bool ivdep)
10570 {
10571   tree begin, end;
10572   tree iter_type, begin_expr, end_expr;
10573   tree condition, expression;
10574
10575   if (range_decl == error_mark_node || range_expr == error_mark_node)
10576     /* If an error happened previously do nothing or else a lot of
10577        unhelpful errors would be issued.  */
10578     begin_expr = end_expr = iter_type = error_mark_node;
10579   else
10580     {
10581       tree range_temp;
10582
10583       if (TREE_CODE (range_expr) == VAR_DECL
10584           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10585         /* Can't bind a reference to an array of runtime bound.  */
10586         range_temp = range_expr;
10587       else
10588         {
10589           range_temp = build_range_temp (range_expr);
10590           pushdecl (range_temp);
10591           cp_finish_decl (range_temp, range_expr,
10592                           /*is_constant_init*/false, NULL_TREE,
10593                           LOOKUP_ONLYCONVERTING);
10594           range_temp = convert_from_reference (range_temp);
10595         }
10596       iter_type = cp_parser_perform_range_for_lookup (range_temp,
10597                                                       &begin_expr, &end_expr);
10598     }
10599
10600   /* The new for initialization statement.  */
10601   begin = build_decl (input_location, VAR_DECL,
10602                       get_identifier ("__for_begin"), iter_type);
10603   TREE_USED (begin) = 1;
10604   DECL_ARTIFICIAL (begin) = 1;
10605   pushdecl (begin);
10606   cp_finish_decl (begin, begin_expr,
10607                   /*is_constant_init*/false, NULL_TREE,
10608                   LOOKUP_ONLYCONVERTING);
10609
10610   end = build_decl (input_location, VAR_DECL,
10611                     get_identifier ("__for_end"), iter_type);
10612   TREE_USED (end) = 1;
10613   DECL_ARTIFICIAL (end) = 1;
10614   pushdecl (end);
10615   cp_finish_decl (end, end_expr,
10616                   /*is_constant_init*/false, NULL_TREE,
10617                   LOOKUP_ONLYCONVERTING);
10618
10619   finish_for_init_stmt (statement);
10620
10621   /* The new for condition.  */
10622   condition = build_x_binary_op (input_location, NE_EXPR,
10623                                  begin, ERROR_MARK,
10624                                  end, ERROR_MARK,
10625                                  NULL, tf_warning_or_error);
10626   finish_for_cond (condition, statement, ivdep);
10627
10628   /* The new increment expression.  */
10629   expression = finish_unary_op_expr (input_location,
10630                                      PREINCREMENT_EXPR, begin,
10631                                      tf_warning_or_error);
10632   finish_for_expr (expression, statement);
10633
10634   /* The declaration is initialized with *__begin inside the loop body.  */
10635   cp_finish_decl (range_decl,
10636                   build_x_indirect_ref (input_location, begin, RO_NULL,
10637                                         tf_warning_or_error),
10638                   /*is_constant_init*/false, NULL_TREE,
10639                   LOOKUP_ONLYCONVERTING);
10640
10641   return statement;
10642 }
10643
10644 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10645    We need to solve both at the same time because the method used
10646    depends on the existence of members begin or end.
10647    Returns the type deduced for the iterator expression.  */
10648
10649 static tree
10650 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10651 {
10652   if (error_operand_p (range))
10653     {
10654       *begin = *end = error_mark_node;
10655       return error_mark_node;
10656     }
10657
10658   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10659     {
10660       error ("range-based %<for%> expression of type %qT "
10661              "has incomplete type", TREE_TYPE (range));
10662       *begin = *end = error_mark_node;
10663       return error_mark_node;
10664     }
10665   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10666     {
10667       /* If RANGE is an array, we will use pointer arithmetic.  */
10668       *begin = range;
10669       *end = build_binary_op (input_location, PLUS_EXPR,
10670                               range,
10671                               array_type_nelts_top (TREE_TYPE (range)),
10672                               0);
10673       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10674     }
10675   else
10676     {
10677       /* If it is not an array, we must do a bit of magic.  */
10678       tree id_begin, id_end;
10679       tree member_begin, member_end;
10680
10681       *begin = *end = error_mark_node;
10682
10683       id_begin = get_identifier ("begin");
10684       id_end = get_identifier ("end");
10685       member_begin = lookup_member (TREE_TYPE (range), id_begin,
10686                                     /*protect=*/2, /*want_type=*/false,
10687                                     tf_warning_or_error);
10688       member_end = lookup_member (TREE_TYPE (range), id_end,
10689                                   /*protect=*/2, /*want_type=*/false,
10690                                   tf_warning_or_error);
10691
10692       if (member_begin != NULL_TREE || member_end != NULL_TREE)
10693         {
10694           /* Use the member functions.  */
10695           if (member_begin != NULL_TREE)
10696             *begin = cp_parser_range_for_member_function (range, id_begin);
10697           else
10698             error ("range-based %<for%> expression of type %qT has an "
10699                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10700
10701           if (member_end != NULL_TREE)
10702             *end = cp_parser_range_for_member_function (range, id_end);
10703           else
10704             error ("range-based %<for%> expression of type %qT has a "
10705                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10706         }
10707       else
10708         {
10709           /* Use global functions with ADL.  */
10710           vec<tree, va_gc> *vec;
10711           vec = make_tree_vector ();
10712
10713           vec_safe_push (vec, range);
10714
10715           member_begin = perform_koenig_lookup (id_begin, vec,
10716                                                 tf_warning_or_error);
10717           *begin = finish_call_expr (member_begin, &vec, false, true,
10718                                      tf_warning_or_error);
10719           member_end = perform_koenig_lookup (id_end, vec,
10720                                               tf_warning_or_error);
10721           *end = finish_call_expr (member_end, &vec, false, true,
10722                                    tf_warning_or_error);
10723
10724           release_tree_vector (vec);
10725         }
10726
10727       /* Last common checks.  */
10728       if (*begin == error_mark_node || *end == error_mark_node)
10729         {
10730           /* If one of the expressions is an error do no more checks.  */
10731           *begin = *end = error_mark_node;
10732           return error_mark_node;
10733         }
10734       else if (type_dependent_expression_p (*begin)
10735                || type_dependent_expression_p (*end))
10736         /* Can happen, when, eg, in a template context, Koenig lookup
10737            can't resolve begin/end (c++/58503).  */
10738         return NULL_TREE;
10739       else
10740         {
10741           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10742           /* The unqualified type of the __begin and __end temporaries should
10743              be the same, as required by the multiple auto declaration.  */
10744           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10745             error ("inconsistent begin/end types in range-based %<for%> "
10746                    "statement: %qT and %qT",
10747                    TREE_TYPE (*begin), TREE_TYPE (*end));
10748           return iter_type;
10749         }
10750     }
10751 }
10752
10753 /* Helper function for cp_parser_perform_range_for_lookup.
10754    Builds a tree for RANGE.IDENTIFIER().  */
10755
10756 static tree
10757 cp_parser_range_for_member_function (tree range, tree identifier)
10758 {
10759   tree member, res;
10760   vec<tree, va_gc> *vec;
10761
10762   member = finish_class_member_access_expr (range, identifier,
10763                                             false, tf_warning_or_error);
10764   if (member == error_mark_node)
10765     return error_mark_node;
10766
10767   vec = make_tree_vector ();
10768   res = finish_call_expr (member, &vec,
10769                           /*disallow_virtual=*/false,
10770                           /*koenig_p=*/false,
10771                           tf_warning_or_error);
10772   release_tree_vector (vec);
10773   return res;
10774 }
10775
10776 /* Parse an iteration-statement.
10777
10778    iteration-statement:
10779      while ( condition ) statement
10780      do statement while ( expression ) ;
10781      for ( for-init-statement condition [opt] ; expression [opt] )
10782        statement
10783
10784    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
10785
10786 static tree
10787 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10788 {
10789   cp_token *token;
10790   enum rid keyword;
10791   tree statement;
10792   unsigned char in_statement;
10793
10794   /* Peek at the next token.  */
10795   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10796   if (!token)
10797     return error_mark_node;
10798
10799   /* Remember whether or not we are already within an iteration
10800      statement.  */
10801   in_statement = parser->in_statement;
10802
10803   /* See what kind of keyword it is.  */
10804   keyword = token->keyword;
10805   switch (keyword)
10806     {
10807     case RID_WHILE:
10808       {
10809         tree condition;
10810
10811         /* Begin the while-statement.  */
10812         statement = begin_while_stmt ();
10813         /* Look for the `('.  */
10814         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10815         /* Parse the condition.  */
10816         condition = cp_parser_condition (parser);
10817         finish_while_stmt_cond (condition, statement, ivdep);
10818         /* Look for the `)'.  */
10819         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10820         /* Parse the dependent statement.  */
10821         parser->in_statement = IN_ITERATION_STMT;
10822         cp_parser_already_scoped_statement (parser);
10823         parser->in_statement = in_statement;
10824         /* We're done with the while-statement.  */
10825         finish_while_stmt (statement);
10826       }
10827       break;
10828
10829     case RID_DO:
10830       {
10831         tree expression;
10832
10833         /* Begin the do-statement.  */
10834         statement = begin_do_stmt ();
10835         /* Parse the body of the do-statement.  */
10836         parser->in_statement = IN_ITERATION_STMT;
10837         cp_parser_implicitly_scoped_statement (parser, NULL);
10838         parser->in_statement = in_statement;
10839         finish_do_body (statement);
10840         /* Look for the `while' keyword.  */
10841         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10842         /* Look for the `('.  */
10843         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10844         /* Parse the expression.  */
10845         expression = cp_parser_expression (parser);
10846         /* We're done with the do-statement.  */
10847         finish_do_stmt (expression, statement, ivdep);
10848         /* Look for the `)'.  */
10849         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10850         /* Look for the `;'.  */
10851         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10852       }
10853       break;
10854
10855     case RID_FOR:
10856       {
10857         /* Look for the `('.  */
10858         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10859
10860         statement = cp_parser_for (parser, ivdep);
10861
10862         /* Look for the `)'.  */
10863         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10864
10865         /* Parse the body of the for-statement.  */
10866         parser->in_statement = IN_ITERATION_STMT;
10867         cp_parser_already_scoped_statement (parser);
10868         parser->in_statement = in_statement;
10869
10870         /* We're done with the for-statement.  */
10871         finish_for_stmt (statement);
10872       }
10873       break;
10874
10875     default:
10876       cp_parser_error (parser, "expected iteration-statement");
10877       statement = error_mark_node;
10878       break;
10879     }
10880
10881   return statement;
10882 }
10883
10884 /* Parse a for-init-statement or the declarator of a range-based-for.
10885    Returns true if a range-based-for declaration is seen.
10886
10887    for-init-statement:
10888      expression-statement
10889      simple-declaration  */
10890
10891 static bool
10892 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10893 {
10894   /* If the next token is a `;', then we have an empty
10895      expression-statement.  Grammatically, this is also a
10896      simple-declaration, but an invalid one, because it does not
10897      declare anything.  Therefore, if we did not handle this case
10898      specially, we would issue an error message about an invalid
10899      declaration.  */
10900   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10901     {
10902       bool is_range_for = false;
10903       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10904
10905       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10906           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10907         {
10908           /* N3994 -- for (id : init) ... */
10909           if (cxx_dialect < cxx1z)
10910             pedwarn (input_location, 0, "range-based for loop without a "
10911                      "type-specifier only available with "
10912                      "-std=c++1z or -std=gnu++1z");
10913           tree name = cp_parser_identifier (parser);
10914           tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10915           *decl = build_decl (input_location, VAR_DECL, name, type);
10916           pushdecl (*decl);
10917           cp_lexer_consume_token (parser->lexer);
10918           return true;
10919         }
10920
10921       /* A colon is used in range-based for.  */
10922       parser->colon_corrects_to_scope_p = false;
10923
10924       /* We're going to speculatively look for a declaration, falling back
10925          to an expression, if necessary.  */
10926       cp_parser_parse_tentatively (parser);
10927       /* Parse the declaration.  */
10928       cp_parser_simple_declaration (parser,
10929                                     /*function_definition_allowed_p=*/false,
10930                                     decl);
10931       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10932       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10933         {
10934           /* It is a range-for, consume the ':' */
10935           cp_lexer_consume_token (parser->lexer);
10936           is_range_for = true;
10937           if (cxx_dialect < cxx11)
10938             {
10939               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10940                        "range-based %<for%> loops only available with "
10941                        "-std=c++11 or -std=gnu++11");
10942               *decl = error_mark_node;
10943             }
10944         }
10945       else
10946           /* The ';' is not consumed yet because we told
10947              cp_parser_simple_declaration not to.  */
10948           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10949
10950       if (cp_parser_parse_definitely (parser))
10951         return is_range_for;
10952       /* If the tentative parse failed, then we shall need to look for an
10953          expression-statement.  */
10954     }
10955   /* If we are here, it is an expression-statement.  */
10956   cp_parser_expression_statement (parser, NULL_TREE);
10957   return false;
10958 }
10959
10960 /* Parse a jump-statement.
10961
10962    jump-statement:
10963      break ;
10964      continue ;
10965      return expression [opt] ;
10966      return braced-init-list ;
10967      goto identifier ;
10968
10969    GNU extension:
10970
10971    jump-statement:
10972      goto * expression ;
10973
10974    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
10975
10976 static tree
10977 cp_parser_jump_statement (cp_parser* parser)
10978 {
10979   tree statement = error_mark_node;
10980   cp_token *token;
10981   enum rid keyword;
10982   unsigned char in_statement;
10983
10984   /* Peek at the next token.  */
10985   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10986   if (!token)
10987     return error_mark_node;
10988
10989   /* See what kind of keyword it is.  */
10990   keyword = token->keyword;
10991   switch (keyword)
10992     {
10993     case RID_BREAK:
10994       in_statement = parser->in_statement & ~IN_IF_STMT;      
10995       switch (in_statement)
10996         {
10997         case 0:
10998           error_at (token->location, "break statement not within loop or switch");
10999           break;
11000         default:
11001           gcc_assert ((in_statement & IN_SWITCH_STMT)
11002                       || in_statement == IN_ITERATION_STMT);
11003           statement = finish_break_stmt ();
11004           if (in_statement == IN_ITERATION_STMT)
11005             break_maybe_infinite_loop ();
11006           break;
11007         case IN_OMP_BLOCK:
11008           error_at (token->location, "invalid exit from OpenMP structured block");
11009           break;
11010         case IN_OMP_FOR:
11011           error_at (token->location, "break statement used with OpenMP for loop");
11012           break;
11013         case IN_CILK_SIMD_FOR:
11014           error_at (token->location, "break statement used with Cilk Plus for loop");
11015           break;
11016         }
11017       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11018       break;
11019
11020     case RID_CONTINUE:
11021       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11022         {
11023         case 0:
11024           error_at (token->location, "continue statement not within a loop");
11025           break;
11026         case IN_CILK_SIMD_FOR:
11027           error_at (token->location,
11028                     "continue statement within %<#pragma simd%> loop body");
11029           /* Fall through.  */
11030         case IN_ITERATION_STMT:
11031         case IN_OMP_FOR:
11032           statement = finish_continue_stmt ();
11033           break;
11034         case IN_OMP_BLOCK:
11035           error_at (token->location, "invalid exit from OpenMP structured block");
11036           break;
11037         default:
11038           gcc_unreachable ();
11039         }
11040       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11041       break;
11042
11043     case RID_RETURN:
11044       {
11045         tree expr;
11046         bool expr_non_constant_p;
11047
11048         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11049           {
11050             cp_lexer_set_source_position (parser->lexer);
11051             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11052             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11053           }
11054         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11055           expr = cp_parser_expression (parser);
11056         else
11057           /* If the next token is a `;', then there is no
11058              expression.  */
11059           expr = NULL_TREE;
11060         /* Build the return-statement.  */
11061         statement = finish_return_stmt (expr);
11062         /* Look for the final `;'.  */
11063         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11064       }
11065       break;
11066
11067     case RID_GOTO:
11068       if (parser->in_function_body
11069           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11070         {
11071           error ("%<goto%> in %<constexpr%> function");
11072           cp_function_chain->invalid_constexpr = true;
11073         }
11074
11075       /* Create the goto-statement.  */
11076       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11077         {
11078           /* Issue a warning about this use of a GNU extension.  */
11079           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11080           /* Consume the '*' token.  */
11081           cp_lexer_consume_token (parser->lexer);
11082           /* Parse the dependent expression.  */
11083           finish_goto_stmt (cp_parser_expression (parser));
11084         }
11085       else
11086         finish_goto_stmt (cp_parser_identifier (parser));
11087       /* Look for the final `;'.  */
11088       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11089       break;
11090
11091     default:
11092       cp_parser_error (parser, "expected jump-statement");
11093       break;
11094     }
11095
11096   return statement;
11097 }
11098
11099 /* Parse a declaration-statement.
11100
11101    declaration-statement:
11102      block-declaration  */
11103
11104 static void
11105 cp_parser_declaration_statement (cp_parser* parser)
11106 {
11107   void *p;
11108
11109   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11110   p = obstack_alloc (&declarator_obstack, 0);
11111
11112  /* Parse the block-declaration.  */
11113   cp_parser_block_declaration (parser, /*statement_p=*/true);
11114
11115   /* Free any declarators allocated.  */
11116   obstack_free (&declarator_obstack, p);
11117 }
11118
11119 /* Some dependent statements (like `if (cond) statement'), are
11120    implicitly in their own scope.  In other words, if the statement is
11121    a single statement (as opposed to a compound-statement), it is
11122    none-the-less treated as if it were enclosed in braces.  Any
11123    declarations appearing in the dependent statement are out of scope
11124    after control passes that point.  This function parses a statement,
11125    but ensures that is in its own scope, even if it is not a
11126    compound-statement.
11127
11128    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11129    is a (possibly labeled) if statement which is not enclosed in
11130    braces and has an else clause.  This is used to implement
11131    -Wparentheses.
11132
11133    Returns the new statement.  */
11134
11135 static tree
11136 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11137 {
11138   tree statement;
11139
11140   if (if_p != NULL)
11141     *if_p = false;
11142
11143   /* Mark if () ; with a special NOP_EXPR.  */
11144   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11145     {
11146       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11147       cp_lexer_consume_token (parser->lexer);
11148       statement = add_stmt (build_empty_stmt (loc));
11149     }
11150   /* if a compound is opened, we simply parse the statement directly.  */
11151   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11152     statement = cp_parser_compound_statement (parser, NULL, false, false);
11153   /* If the token is not a `{', then we must take special action.  */
11154   else
11155     {
11156       /* Create a compound-statement.  */
11157       statement = begin_compound_stmt (0);
11158       /* Parse the dependent-statement.  */
11159       cp_parser_statement (parser, NULL_TREE, false, if_p);
11160       /* Finish the dummy compound-statement.  */
11161       finish_compound_stmt (statement);
11162     }
11163
11164   /* Return the statement.  */
11165   return statement;
11166 }
11167
11168 /* For some dependent statements (like `while (cond) statement'), we
11169    have already created a scope.  Therefore, even if the dependent
11170    statement is a compound-statement, we do not want to create another
11171    scope.  */
11172
11173 static void
11174 cp_parser_already_scoped_statement (cp_parser* parser)
11175 {
11176   /* If the token is a `{', then we must take special action.  */
11177   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11178     cp_parser_statement (parser, NULL_TREE, false, NULL);
11179   else
11180     {
11181       /* Avoid calling cp_parser_compound_statement, so that we
11182          don't create a new scope.  Do everything else by hand.  */
11183       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11184       /* If the next keyword is `__label__' we have a label declaration.  */
11185       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11186         cp_parser_label_declaration (parser);
11187       /* Parse an (optional) statement-seq.  */
11188       cp_parser_statement_seq_opt (parser, NULL_TREE);
11189       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11190     }
11191 }
11192
11193 /* Declarations [gram.dcl.dcl] */
11194
11195 /* Parse an optional declaration-sequence.
11196
11197    declaration-seq:
11198      declaration
11199      declaration-seq declaration  */
11200
11201 static void
11202 cp_parser_declaration_seq_opt (cp_parser* parser)
11203 {
11204   while (true)
11205     {
11206       cp_token *token;
11207
11208       token = cp_lexer_peek_token (parser->lexer);
11209
11210       if (token->type == CPP_CLOSE_BRACE
11211           || token->type == CPP_EOF
11212           || token->type == CPP_PRAGMA_EOL)
11213         break;
11214
11215       if (token->type == CPP_SEMICOLON)
11216         {
11217           /* A declaration consisting of a single semicolon is
11218              invalid.  Allow it unless we're being pedantic.  */
11219           cp_lexer_consume_token (parser->lexer);
11220           if (!in_system_header_at (input_location))
11221             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11222           continue;
11223         }
11224
11225       /* If we're entering or exiting a region that's implicitly
11226          extern "C", modify the lang context appropriately.  */
11227       if (!parser->implicit_extern_c && token->implicit_extern_c)
11228         {
11229           push_lang_context (lang_name_c);
11230           parser->implicit_extern_c = true;
11231         }
11232       else if (parser->implicit_extern_c && !token->implicit_extern_c)
11233         {
11234           pop_lang_context ();
11235           parser->implicit_extern_c = false;
11236         }
11237
11238       if (token->type == CPP_PRAGMA)
11239         {
11240           /* A top-level declaration can consist solely of a #pragma.
11241              A nested declaration cannot, so this is done here and not
11242              in cp_parser_declaration.  (A #pragma at block scope is
11243              handled in cp_parser_statement.)  */
11244           cp_parser_pragma (parser, pragma_external);
11245           continue;
11246         }
11247
11248       /* Parse the declaration itself.  */
11249       cp_parser_declaration (parser);
11250     }
11251 }
11252
11253 /* Parse a declaration.
11254
11255    declaration:
11256      block-declaration
11257      function-definition
11258      template-declaration
11259      explicit-instantiation
11260      explicit-specialization
11261      linkage-specification
11262      namespace-definition
11263
11264    GNU extension:
11265
11266    declaration:
11267       __extension__ declaration */
11268
11269 static void
11270 cp_parser_declaration (cp_parser* parser)
11271 {
11272   cp_token token1;
11273   cp_token token2;
11274   int saved_pedantic;
11275   void *p;
11276   tree attributes = NULL_TREE;
11277
11278   /* Check for the `__extension__' keyword.  */
11279   if (cp_parser_extension_opt (parser, &saved_pedantic))
11280     {
11281       /* Parse the qualified declaration.  */
11282       cp_parser_declaration (parser);
11283       /* Restore the PEDANTIC flag.  */
11284       pedantic = saved_pedantic;
11285
11286       return;
11287     }
11288
11289   /* Try to figure out what kind of declaration is present.  */
11290   token1 = *cp_lexer_peek_token (parser->lexer);
11291
11292   if (token1.type != CPP_EOF)
11293     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11294   else
11295     {
11296       token2.type = CPP_EOF;
11297       token2.keyword = RID_MAX;
11298     }
11299
11300   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11301   p = obstack_alloc (&declarator_obstack, 0);
11302
11303   /* If the next token is `extern' and the following token is a string
11304      literal, then we have a linkage specification.  */
11305   if (token1.keyword == RID_EXTERN
11306       && cp_parser_is_pure_string_literal (&token2))
11307     cp_parser_linkage_specification (parser);
11308   /* If the next token is `template', then we have either a template
11309      declaration, an explicit instantiation, or an explicit
11310      specialization.  */
11311   else if (token1.keyword == RID_TEMPLATE)
11312     {
11313       /* `template <>' indicates a template specialization.  */
11314       if (token2.type == CPP_LESS
11315           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11316         cp_parser_explicit_specialization (parser);
11317       /* `template <' indicates a template declaration.  */
11318       else if (token2.type == CPP_LESS)
11319         cp_parser_template_declaration (parser, /*member_p=*/false);
11320       /* Anything else must be an explicit instantiation.  */
11321       else
11322         cp_parser_explicit_instantiation (parser);
11323     }
11324   /* If the next token is `export', then we have a template
11325      declaration.  */
11326   else if (token1.keyword == RID_EXPORT)
11327     cp_parser_template_declaration (parser, /*member_p=*/false);
11328   /* If the next token is `extern', 'static' or 'inline' and the one
11329      after that is `template', we have a GNU extended explicit
11330      instantiation directive.  */
11331   else if (cp_parser_allow_gnu_extensions_p (parser)
11332            && (token1.keyword == RID_EXTERN
11333                || token1.keyword == RID_STATIC
11334                || token1.keyword == RID_INLINE)
11335            && token2.keyword == RID_TEMPLATE)
11336     cp_parser_explicit_instantiation (parser);
11337   /* If the next token is `namespace', check for a named or unnamed
11338      namespace definition.  */
11339   else if (token1.keyword == RID_NAMESPACE
11340            && (/* A named namespace definition.  */
11341                (token2.type == CPP_NAME
11342                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11343                     != CPP_EQ))
11344                /* An unnamed namespace definition.  */
11345                || token2.type == CPP_OPEN_BRACE
11346                || token2.keyword == RID_ATTRIBUTE))
11347     cp_parser_namespace_definition (parser);
11348   /* An inline (associated) namespace definition.  */
11349   else if (token1.keyword == RID_INLINE
11350            && token2.keyword == RID_NAMESPACE)
11351     cp_parser_namespace_definition (parser);
11352   /* Objective-C++ declaration/definition.  */
11353   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11354     cp_parser_objc_declaration (parser, NULL_TREE);
11355   else if (c_dialect_objc ()
11356            && token1.keyword == RID_ATTRIBUTE
11357            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11358     cp_parser_objc_declaration (parser, attributes);
11359   /* We must have either a block declaration or a function
11360      definition.  */
11361   else
11362     /* Try to parse a block-declaration, or a function-definition.  */
11363     cp_parser_block_declaration (parser, /*statement_p=*/false);
11364
11365   /* Free any declarators allocated.  */
11366   obstack_free (&declarator_obstack, p);
11367 }
11368
11369 /* Parse a block-declaration.
11370
11371    block-declaration:
11372      simple-declaration
11373      asm-definition
11374      namespace-alias-definition
11375      using-declaration
11376      using-directive
11377
11378    GNU Extension:
11379
11380    block-declaration:
11381      __extension__ block-declaration
11382
11383    C++0x Extension:
11384
11385    block-declaration:
11386      static_assert-declaration
11387
11388    If STATEMENT_P is TRUE, then this block-declaration is occurring as
11389    part of a declaration-statement.  */
11390
11391 static void
11392 cp_parser_block_declaration (cp_parser *parser,
11393                              bool      statement_p)
11394 {
11395   cp_token *token1;
11396   int saved_pedantic;
11397
11398   /* Check for the `__extension__' keyword.  */
11399   if (cp_parser_extension_opt (parser, &saved_pedantic))
11400     {
11401       /* Parse the qualified declaration.  */
11402       cp_parser_block_declaration (parser, statement_p);
11403       /* Restore the PEDANTIC flag.  */
11404       pedantic = saved_pedantic;
11405
11406       return;
11407     }
11408
11409   /* Peek at the next token to figure out which kind of declaration is
11410      present.  */
11411   token1 = cp_lexer_peek_token (parser->lexer);
11412
11413   /* If the next keyword is `asm', we have an asm-definition.  */
11414   if (token1->keyword == RID_ASM)
11415     {
11416       if (statement_p)
11417         cp_parser_commit_to_tentative_parse (parser);
11418       cp_parser_asm_definition (parser);
11419     }
11420   /* If the next keyword is `namespace', we have a
11421      namespace-alias-definition.  */
11422   else if (token1->keyword == RID_NAMESPACE)
11423     cp_parser_namespace_alias_definition (parser);
11424   /* If the next keyword is `using', we have a
11425      using-declaration, a using-directive, or an alias-declaration.  */
11426   else if (token1->keyword == RID_USING)
11427     {
11428       cp_token *token2;
11429
11430       if (statement_p)
11431         cp_parser_commit_to_tentative_parse (parser);
11432       /* If the token after `using' is `namespace', then we have a
11433          using-directive.  */
11434       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11435       if (token2->keyword == RID_NAMESPACE)
11436         cp_parser_using_directive (parser);
11437       /* If the second token after 'using' is '=', then we have an
11438          alias-declaration.  */
11439       else if (cxx_dialect >= cxx11
11440                && token2->type == CPP_NAME
11441                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11442                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11443         cp_parser_alias_declaration (parser);
11444       /* Otherwise, it's a using-declaration.  */
11445       else
11446         cp_parser_using_declaration (parser,
11447                                      /*access_declaration_p=*/false);
11448     }
11449   /* If the next keyword is `__label__' we have a misplaced label
11450      declaration.  */
11451   else if (token1->keyword == RID_LABEL)
11452     {
11453       cp_lexer_consume_token (parser->lexer);
11454       error_at (token1->location, "%<__label__%> not at the beginning of a block");
11455       cp_parser_skip_to_end_of_statement (parser);
11456       /* If the next token is now a `;', consume it.  */
11457       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11458         cp_lexer_consume_token (parser->lexer);
11459     }
11460   /* If the next token is `static_assert' we have a static assertion.  */
11461   else if (token1->keyword == RID_STATIC_ASSERT)
11462     cp_parser_static_assert (parser, /*member_p=*/false);
11463   /* Anything else must be a simple-declaration.  */
11464   else
11465     cp_parser_simple_declaration (parser, !statement_p,
11466                                   /*maybe_range_for_decl*/NULL);
11467 }
11468
11469 /* Parse a simple-declaration.
11470
11471    simple-declaration:
11472      decl-specifier-seq [opt] init-declarator-list [opt] ;
11473
11474    init-declarator-list:
11475      init-declarator
11476      init-declarator-list , init-declarator
11477
11478    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11479    function-definition as a simple-declaration.
11480
11481    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11482    parsed declaration if it is an uninitialized single declarator not followed
11483    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11484    if present, will not be consumed.  */
11485
11486 static void
11487 cp_parser_simple_declaration (cp_parser* parser,
11488                               bool function_definition_allowed_p,
11489                               tree *maybe_range_for_decl)
11490 {
11491   cp_decl_specifier_seq decl_specifiers;
11492   int declares_class_or_enum;
11493   bool saw_declarator;
11494   location_t comma_loc = UNKNOWN_LOCATION;
11495   location_t init_loc = UNKNOWN_LOCATION;
11496
11497   if (maybe_range_for_decl)
11498     *maybe_range_for_decl = NULL_TREE;
11499
11500   /* Defer access checks until we know what is being declared; the
11501      checks for names appearing in the decl-specifier-seq should be
11502      done as if we were in the scope of the thing being declared.  */
11503   push_deferring_access_checks (dk_deferred);
11504
11505   /* Parse the decl-specifier-seq.  We have to keep track of whether
11506      or not the decl-specifier-seq declares a named class or
11507      enumeration type, since that is the only case in which the
11508      init-declarator-list is allowed to be empty.
11509
11510      [dcl.dcl]
11511
11512      In a simple-declaration, the optional init-declarator-list can be
11513      omitted only when declaring a class or enumeration, that is when
11514      the decl-specifier-seq contains either a class-specifier, an
11515      elaborated-type-specifier, or an enum-specifier.  */
11516   cp_parser_decl_specifier_seq (parser,
11517                                 CP_PARSER_FLAGS_OPTIONAL,
11518                                 &decl_specifiers,
11519                                 &declares_class_or_enum);
11520   /* We no longer need to defer access checks.  */
11521   stop_deferring_access_checks ();
11522
11523   /* In a block scope, a valid declaration must always have a
11524      decl-specifier-seq.  By not trying to parse declarators, we can
11525      resolve the declaration/expression ambiguity more quickly.  */
11526   if (!function_definition_allowed_p
11527       && !decl_specifiers.any_specifiers_p)
11528     {
11529       cp_parser_error (parser, "expected declaration");
11530       goto done;
11531     }
11532
11533   /* If the next two tokens are both identifiers, the code is
11534      erroneous. The usual cause of this situation is code like:
11535
11536        T t;
11537
11538      where "T" should name a type -- but does not.  */
11539   if (!decl_specifiers.any_type_specifiers_p
11540       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11541     {
11542       /* If parsing tentatively, we should commit; we really are
11543          looking at a declaration.  */
11544       cp_parser_commit_to_tentative_parse (parser);
11545       /* Give up.  */
11546       goto done;
11547     }
11548
11549   /* If we have seen at least one decl-specifier, and the next token
11550      is not a parenthesis, then we must be looking at a declaration.
11551      (After "int (" we might be looking at a functional cast.)  */
11552   if (decl_specifiers.any_specifiers_p
11553       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11554       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11555       && !cp_parser_error_occurred (parser))
11556     cp_parser_commit_to_tentative_parse (parser);
11557
11558   /* Keep going until we hit the `;' at the end of the simple
11559      declaration.  */
11560   saw_declarator = false;
11561   while (cp_lexer_next_token_is_not (parser->lexer,
11562                                      CPP_SEMICOLON))
11563     {
11564       cp_token *token;
11565       bool function_definition_p;
11566       tree decl;
11567
11568       if (saw_declarator)
11569         {
11570           /* If we are processing next declarator, comma is expected */
11571           token = cp_lexer_peek_token (parser->lexer);
11572           gcc_assert (token->type == CPP_COMMA);
11573           cp_lexer_consume_token (parser->lexer);
11574           if (maybe_range_for_decl)
11575             {
11576               *maybe_range_for_decl = error_mark_node;
11577               if (comma_loc == UNKNOWN_LOCATION)
11578                 comma_loc = token->location;
11579             }
11580         }
11581       else
11582         saw_declarator = true;
11583
11584       /* Parse the init-declarator.  */
11585       decl = cp_parser_init_declarator (parser, &decl_specifiers,
11586                                         /*checks=*/NULL,
11587                                         function_definition_allowed_p,
11588                                         /*member_p=*/false,
11589                                         declares_class_or_enum,
11590                                         &function_definition_p,
11591                                         maybe_range_for_decl,
11592                                         &init_loc);
11593       /* If an error occurred while parsing tentatively, exit quickly.
11594          (That usually happens when in the body of a function; each
11595          statement is treated as a declaration-statement until proven
11596          otherwise.)  */
11597       if (cp_parser_error_occurred (parser))
11598         goto done;
11599       /* Handle function definitions specially.  */
11600       if (function_definition_p)
11601         {
11602           /* If the next token is a `,', then we are probably
11603              processing something like:
11604
11605                void f() {}, *p;
11606
11607              which is erroneous.  */
11608           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11609             {
11610               cp_token *token = cp_lexer_peek_token (parser->lexer);
11611               error_at (token->location,
11612                         "mixing"
11613                         " declarations and function-definitions is forbidden");
11614             }
11615           /* Otherwise, we're done with the list of declarators.  */
11616           else
11617             {
11618               pop_deferring_access_checks ();
11619               return;
11620             }
11621         }
11622       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11623         *maybe_range_for_decl = decl;
11624       /* The next token should be either a `,' or a `;'.  */
11625       token = cp_lexer_peek_token (parser->lexer);
11626       /* If it's a `,', there are more declarators to come.  */
11627       if (token->type == CPP_COMMA)
11628         /* will be consumed next time around */;
11629       /* If it's a `;', we are done.  */
11630       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11631         break;
11632       /* Anything else is an error.  */
11633       else
11634         {
11635           /* If we have already issued an error message we don't need
11636              to issue another one.  */
11637           if (decl != error_mark_node
11638               || cp_parser_uncommitted_to_tentative_parse_p (parser))
11639             cp_parser_error (parser, "expected %<,%> or %<;%>");
11640           /* Skip tokens until we reach the end of the statement.  */
11641           cp_parser_skip_to_end_of_statement (parser);
11642           /* If the next token is now a `;', consume it.  */
11643           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11644             cp_lexer_consume_token (parser->lexer);
11645           goto done;
11646         }
11647       /* After the first time around, a function-definition is not
11648          allowed -- even if it was OK at first.  For example:
11649
11650            int i, f() {}
11651
11652          is not valid.  */
11653       function_definition_allowed_p = false;
11654     }
11655
11656   /* Issue an error message if no declarators are present, and the
11657      decl-specifier-seq does not itself declare a class or
11658      enumeration: [dcl.dcl]/3.  */
11659   if (!saw_declarator)
11660     {
11661       if (cp_parser_declares_only_class_p (parser))
11662         {
11663           if (!declares_class_or_enum
11664               && decl_specifiers.type
11665               && OVERLOAD_TYPE_P (decl_specifiers.type))
11666             /* Ensure an error is issued anyway when finish_decltype_type,
11667                called via cp_parser_decl_specifier_seq, returns a class or
11668                an enumeration (c++/51786).  */
11669             decl_specifiers.type = NULL_TREE;
11670           shadow_tag (&decl_specifiers);
11671         }
11672       /* Perform any deferred access checks.  */
11673       perform_deferred_access_checks (tf_warning_or_error);
11674     }
11675
11676   /* Consume the `;'.  */
11677   if (!maybe_range_for_decl)
11678     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11679   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11680     {
11681       if (init_loc != UNKNOWN_LOCATION)
11682         error_at (init_loc, "initializer in range-based %<for%> loop");
11683       if (comma_loc != UNKNOWN_LOCATION)
11684         error_at (comma_loc,
11685                   "multiple declarations in range-based %<for%> loop");
11686     }
11687
11688  done:
11689   pop_deferring_access_checks ();
11690 }
11691
11692 /* Parse a decl-specifier-seq.
11693
11694    decl-specifier-seq:
11695      decl-specifier-seq [opt] decl-specifier
11696      decl-specifier attribute-specifier-seq [opt] (C++11)
11697
11698    decl-specifier:
11699      storage-class-specifier
11700      type-specifier
11701      function-specifier
11702      friend
11703      typedef
11704
11705    GNU Extension:
11706
11707    decl-specifier:
11708      attributes
11709
11710    Set *DECL_SPECS to a representation of the decl-specifier-seq.
11711
11712    The parser flags FLAGS is used to control type-specifier parsing.
11713
11714    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11715    flags:
11716
11717      1: one of the decl-specifiers is an elaborated-type-specifier
11718         (i.e., a type declaration)
11719      2: one of the decl-specifiers is an enum-specifier or a
11720         class-specifier (i.e., a type definition)
11721
11722    */
11723
11724 static void
11725 cp_parser_decl_specifier_seq (cp_parser* parser,
11726                               cp_parser_flags flags,
11727                               cp_decl_specifier_seq *decl_specs,
11728                               int* declares_class_or_enum)
11729 {
11730   bool constructor_possible_p = !parser->in_declarator_p;
11731   bool found_decl_spec = false;
11732   cp_token *start_token = NULL;
11733   cp_decl_spec ds;
11734
11735   /* Clear DECL_SPECS.  */
11736   clear_decl_specs (decl_specs);
11737
11738   /* Assume no class or enumeration type is declared.  */
11739   *declares_class_or_enum = 0;
11740
11741   /* Keep reading specifiers until there are no more to read.  */
11742   while (true)
11743     {
11744       bool constructor_p;
11745       cp_token *token;
11746       ds = ds_last;
11747
11748       /* Peek at the next token.  */
11749       token = cp_lexer_peek_token (parser->lexer);
11750
11751       /* Save the first token of the decl spec list for error
11752          reporting.  */
11753       if (!start_token)
11754         start_token = token;
11755       /* Handle attributes.  */
11756       if (cp_next_tokens_can_be_attribute_p (parser))
11757         {
11758           /* Parse the attributes.  */
11759           tree attrs = cp_parser_attributes_opt (parser);
11760
11761           /* In a sequence of declaration specifiers, c++11 attributes
11762              appertain to the type that precede them. In that case
11763              [dcl.spec]/1 says:
11764
11765                  The attribute-specifier-seq affects the type only for
11766                  the declaration it appears in, not other declarations
11767                  involving the same type.
11768
11769              But for now let's force the user to position the
11770              attribute either at the beginning of the declaration or
11771              after the declarator-id, which would clearly mean that it
11772              applies to the declarator.  */
11773           if (cxx11_attribute_p (attrs))
11774             {
11775               if (!found_decl_spec)
11776                 /* The c++11 attribute is at the beginning of the
11777                    declaration.  It appertains to the entity being
11778                    declared.  */;
11779               else
11780                 {
11781                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11782                     {
11783                       /*  This is an attribute following a
11784                           class-specifier.  */
11785                       if (decl_specs->type_definition_p)
11786                         warn_misplaced_attr_for_class_type (token->location,
11787                                                             decl_specs->type);
11788                       attrs = NULL_TREE;
11789                     }
11790                   else
11791                     {
11792                       decl_specs->std_attributes
11793                         = chainon (decl_specs->std_attributes,
11794                                    attrs);
11795                       if (decl_specs->locations[ds_std_attribute] == 0)
11796                         decl_specs->locations[ds_std_attribute] = token->location;
11797                     }
11798                   continue;
11799                 }
11800             }
11801
11802             decl_specs->attributes
11803               = chainon (decl_specs->attributes,
11804                          attrs);
11805           if (decl_specs->locations[ds_attribute] == 0)
11806             decl_specs->locations[ds_attribute] = token->location;
11807           continue;
11808         }
11809       /* Assume we will find a decl-specifier keyword.  */
11810       found_decl_spec = true;
11811       /* If the next token is an appropriate keyword, we can simply
11812          add it to the list.  */
11813       switch (token->keyword)
11814         {
11815           /* decl-specifier:
11816                friend
11817                constexpr */
11818         case RID_FRIEND:
11819           if (!at_class_scope_p ())
11820             {
11821               error_at (token->location, "%<friend%> used outside of class");
11822               cp_lexer_purge_token (parser->lexer);
11823             }
11824           else
11825             {
11826               ds = ds_friend;
11827               /* Consume the token.  */
11828               cp_lexer_consume_token (parser->lexer);
11829             }
11830           break;
11831
11832         case RID_CONSTEXPR:
11833           ds = ds_constexpr;
11834           cp_lexer_consume_token (parser->lexer);
11835           break;
11836
11837           /* function-specifier:
11838                inline
11839                virtual
11840                explicit  */
11841         case RID_INLINE:
11842         case RID_VIRTUAL:
11843         case RID_EXPLICIT:
11844           cp_parser_function_specifier_opt (parser, decl_specs);
11845           break;
11846
11847           /* decl-specifier:
11848                typedef  */
11849         case RID_TYPEDEF:
11850           ds = ds_typedef;
11851           /* Consume the token.  */
11852           cp_lexer_consume_token (parser->lexer);
11853           /* A constructor declarator cannot appear in a typedef.  */
11854           constructor_possible_p = false;
11855           /* The "typedef" keyword can only occur in a declaration; we
11856              may as well commit at this point.  */
11857           cp_parser_commit_to_tentative_parse (parser);
11858
11859           if (decl_specs->storage_class != sc_none)
11860             decl_specs->conflicting_specifiers_p = true;
11861           break;
11862
11863           /* storage-class-specifier:
11864                auto
11865                register
11866                static
11867                extern
11868                mutable
11869
11870              GNU Extension:
11871                thread  */
11872         case RID_AUTO:
11873           if (cxx_dialect == cxx98) 
11874             {
11875               /* Consume the token.  */
11876               cp_lexer_consume_token (parser->lexer);
11877
11878               /* Complain about `auto' as a storage specifier, if
11879                  we're complaining about C++0x compatibility.  */
11880               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11881                           " changes meaning in C++11; please remove it");
11882
11883               /* Set the storage class anyway.  */
11884               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11885                                            token);
11886             }
11887           else
11888             /* C++0x auto type-specifier.  */
11889             found_decl_spec = false;
11890           break;
11891
11892         case RID_REGISTER:
11893         case RID_STATIC:
11894         case RID_EXTERN:
11895         case RID_MUTABLE:
11896           /* Consume the token.  */
11897           cp_lexer_consume_token (parser->lexer);
11898           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11899                                        token);
11900           break;
11901         case RID_THREAD:
11902           /* Consume the token.  */
11903           ds = ds_thread;
11904           cp_lexer_consume_token (parser->lexer);
11905           break;
11906
11907         default:
11908           /* We did not yet find a decl-specifier yet.  */
11909           found_decl_spec = false;
11910           break;
11911         }
11912
11913       if (found_decl_spec
11914           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11915           && token->keyword != RID_CONSTEXPR)
11916         error ("decl-specifier invalid in condition");
11917
11918       if (ds != ds_last)
11919         set_and_check_decl_spec_loc (decl_specs, ds, token);
11920
11921       /* Constructors are a special case.  The `S' in `S()' is not a
11922          decl-specifier; it is the beginning of the declarator.  */
11923       constructor_p
11924         = (!found_decl_spec
11925            && constructor_possible_p
11926            && (cp_parser_constructor_declarator_p
11927                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11928
11929       /* If we don't have a DECL_SPEC yet, then we must be looking at
11930          a type-specifier.  */
11931       if (!found_decl_spec && !constructor_p)
11932         {
11933           int decl_spec_declares_class_or_enum;
11934           bool is_cv_qualifier;
11935           tree type_spec;
11936
11937           type_spec
11938             = cp_parser_type_specifier (parser, flags,
11939                                         decl_specs,
11940                                         /*is_declaration=*/true,
11941                                         &decl_spec_declares_class_or_enum,
11942                                         &is_cv_qualifier);
11943           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11944
11945           /* If this type-specifier referenced a user-defined type
11946              (a typedef, class-name, etc.), then we can't allow any
11947              more such type-specifiers henceforth.
11948
11949              [dcl.spec]
11950
11951              The longest sequence of decl-specifiers that could
11952              possibly be a type name is taken as the
11953              decl-specifier-seq of a declaration.  The sequence shall
11954              be self-consistent as described below.
11955
11956              [dcl.type]
11957
11958              As a general rule, at most one type-specifier is allowed
11959              in the complete decl-specifier-seq of a declaration.  The
11960              only exceptions are the following:
11961
11962              -- const or volatile can be combined with any other
11963                 type-specifier.
11964
11965              -- signed or unsigned can be combined with char, long,
11966                 short, or int.
11967
11968              -- ..
11969
11970              Example:
11971
11972                typedef char* Pc;
11973                void g (const int Pc);
11974
11975              Here, Pc is *not* part of the decl-specifier seq; it's
11976              the declarator.  Therefore, once we see a type-specifier
11977              (other than a cv-qualifier), we forbid any additional
11978              user-defined types.  We *do* still allow things like `int
11979              int' to be considered a decl-specifier-seq, and issue the
11980              error message later.  */
11981           if (type_spec && !is_cv_qualifier)
11982             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11983           /* A constructor declarator cannot follow a type-specifier.  */
11984           if (type_spec)
11985             {
11986               constructor_possible_p = false;
11987               found_decl_spec = true;
11988               if (!is_cv_qualifier)
11989                 decl_specs->any_type_specifiers_p = true;
11990             }
11991         }
11992
11993       /* If we still do not have a DECL_SPEC, then there are no more
11994          decl-specifiers.  */
11995       if (!found_decl_spec)
11996         break;
11997
11998       decl_specs->any_specifiers_p = true;
11999       /* After we see one decl-specifier, further decl-specifiers are
12000          always optional.  */
12001       flags |= CP_PARSER_FLAGS_OPTIONAL;
12002     }
12003
12004   /* Don't allow a friend specifier with a class definition.  */
12005   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12006       && (*declares_class_or_enum & 2))
12007     error_at (decl_specs->locations[ds_friend],
12008               "class definition may not be declared a friend");
12009 }
12010
12011 /* Parse an (optional) storage-class-specifier.
12012
12013    storage-class-specifier:
12014      auto
12015      register
12016      static
12017      extern
12018      mutable
12019
12020    GNU Extension:
12021
12022    storage-class-specifier:
12023      thread
12024
12025    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
12026
12027 static tree
12028 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12029 {
12030   switch (cp_lexer_peek_token (parser->lexer)->keyword)
12031     {
12032     case RID_AUTO:
12033       if (cxx_dialect != cxx98)
12034         return NULL_TREE;
12035       /* Fall through for C++98.  */
12036
12037     case RID_REGISTER:
12038     case RID_STATIC:
12039     case RID_EXTERN:
12040     case RID_MUTABLE:
12041     case RID_THREAD:
12042       /* Consume the token.  */
12043       return cp_lexer_consume_token (parser->lexer)->u.value;
12044
12045     default:
12046       return NULL_TREE;
12047     }
12048 }
12049
12050 /* Parse an (optional) function-specifier.
12051
12052    function-specifier:
12053      inline
12054      virtual
12055      explicit
12056
12057    Returns an IDENTIFIER_NODE corresponding to the keyword used.
12058    Updates DECL_SPECS, if it is non-NULL.  */
12059
12060 static tree
12061 cp_parser_function_specifier_opt (cp_parser* parser,
12062                                   cp_decl_specifier_seq *decl_specs)
12063 {
12064   cp_token *token = cp_lexer_peek_token (parser->lexer);
12065   switch (token->keyword)
12066     {
12067     case RID_INLINE:
12068       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12069       break;
12070
12071     case RID_VIRTUAL:
12072       /* 14.5.2.3 [temp.mem]
12073
12074          A member function template shall not be virtual.  */
12075       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12076         error_at (token->location, "templates may not be %<virtual%>");
12077       else
12078         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12079       break;
12080
12081     case RID_EXPLICIT:
12082       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12083       break;
12084
12085     default:
12086       return NULL_TREE;
12087     }
12088
12089   /* Consume the token.  */
12090   return cp_lexer_consume_token (parser->lexer)->u.value;
12091 }
12092
12093 /* Parse a linkage-specification.
12094
12095    linkage-specification:
12096      extern string-literal { declaration-seq [opt] }
12097      extern string-literal declaration  */
12098
12099 static void
12100 cp_parser_linkage_specification (cp_parser* parser)
12101 {
12102   tree linkage;
12103
12104   /* Look for the `extern' keyword.  */
12105   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12106
12107   /* Look for the string-literal.  */
12108   linkage = cp_parser_string_literal (parser, false, false);
12109
12110   /* Transform the literal into an identifier.  If the literal is a
12111      wide-character string, or contains embedded NULs, then we can't
12112      handle it as the user wants.  */
12113   if (strlen (TREE_STRING_POINTER (linkage))
12114       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12115     {
12116       cp_parser_error (parser, "invalid linkage-specification");
12117       /* Assume C++ linkage.  */
12118       linkage = lang_name_cplusplus;
12119     }
12120   else
12121     linkage = get_identifier (TREE_STRING_POINTER (linkage));
12122
12123   /* We're now using the new linkage.  */
12124   push_lang_context (linkage);
12125
12126   /* If the next token is a `{', then we're using the first
12127      production.  */
12128   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12129     {
12130       cp_ensure_no_omp_declare_simd (parser);
12131
12132       /* Consume the `{' token.  */
12133       cp_lexer_consume_token (parser->lexer);
12134       /* Parse the declarations.  */
12135       cp_parser_declaration_seq_opt (parser);
12136       /* Look for the closing `}'.  */
12137       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12138     }
12139   /* Otherwise, there's just one declaration.  */
12140   else
12141     {
12142       bool saved_in_unbraced_linkage_specification_p;
12143
12144       saved_in_unbraced_linkage_specification_p
12145         = parser->in_unbraced_linkage_specification_p;
12146       parser->in_unbraced_linkage_specification_p = true;
12147       cp_parser_declaration (parser);
12148       parser->in_unbraced_linkage_specification_p
12149         = saved_in_unbraced_linkage_specification_p;
12150     }
12151
12152   /* We're done with the linkage-specification.  */
12153   pop_lang_context ();
12154 }
12155
12156 /* Parse a static_assert-declaration.
12157
12158    static_assert-declaration:
12159      static_assert ( constant-expression , string-literal ) ; 
12160
12161    If MEMBER_P, this static_assert is a class member.  */
12162
12163 static void 
12164 cp_parser_static_assert(cp_parser *parser, bool member_p)
12165 {
12166   tree condition;
12167   tree message;
12168   cp_token *token;
12169   location_t saved_loc;
12170   bool dummy;
12171
12172   /* Peek at the `static_assert' token so we can keep track of exactly
12173      where the static assertion started.  */
12174   token = cp_lexer_peek_token (parser->lexer);
12175   saved_loc = token->location;
12176
12177   /* Look for the `static_assert' keyword.  */
12178   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
12179                                   RT_STATIC_ASSERT))
12180     return;
12181
12182   /*  We know we are in a static assertion; commit to any tentative
12183       parse.  */
12184   if (cp_parser_parsing_tentatively (parser))
12185     cp_parser_commit_to_tentative_parse (parser);
12186
12187   /* Parse the `(' starting the static assertion condition.  */
12188   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12189
12190   /* Parse the constant-expression.  Allow a non-constant expression
12191      here in order to give better diagnostics in finish_static_assert.  */
12192   condition = 
12193     cp_parser_constant_expression (parser,
12194                                    /*allow_non_constant_p=*/true,
12195                                    /*non_constant_p=*/&dummy);
12196
12197   /* Parse the separating `,'.  */
12198   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12199
12200   /* Parse the string-literal message.  */
12201   message = cp_parser_string_literal (parser, 
12202                                       /*translate=*/false,
12203                                       /*wide_ok=*/true);
12204
12205   /* A `)' completes the static assertion.  */
12206   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12207     cp_parser_skip_to_closing_parenthesis (parser, 
12208                                            /*recovering=*/true, 
12209                                            /*or_comma=*/false,
12210                                            /*consume_paren=*/true);
12211
12212   /* A semicolon terminates the declaration.  */
12213   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12214
12215   /* Complete the static assertion, which may mean either processing 
12216      the static assert now or saving it for template instantiation.  */
12217   finish_static_assert (condition, message, saved_loc, member_p);
12218 }
12219
12220 /* Parse the expression in decltype ( expression ).  */
12221
12222 static tree
12223 cp_parser_decltype_expr (cp_parser *parser,
12224                          bool &id_expression_or_member_access_p)
12225 {
12226   cp_token *id_expr_start_token;
12227   tree expr;
12228
12229   /* First, try parsing an id-expression.  */
12230   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12231   cp_parser_parse_tentatively (parser);
12232   expr = cp_parser_id_expression (parser,
12233                                   /*template_keyword_p=*/false,
12234                                   /*check_dependency_p=*/true,
12235                                   /*template_p=*/NULL,
12236                                   /*declarator_p=*/false,
12237                                   /*optional_p=*/false);
12238
12239   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12240     {
12241       bool non_integral_constant_expression_p = false;
12242       tree id_expression = expr;
12243       cp_id_kind idk;
12244       const char *error_msg;
12245
12246       if (identifier_p (expr))
12247         /* Lookup the name we got back from the id-expression.  */
12248         expr = cp_parser_lookup_name_simple (parser, expr,
12249                                              id_expr_start_token->location);
12250
12251       if (expr
12252           && expr != error_mark_node
12253           && TREE_CODE (expr) != TYPE_DECL
12254           && (TREE_CODE (expr) != BIT_NOT_EXPR
12255               || !TYPE_P (TREE_OPERAND (expr, 0)))
12256           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12257         {
12258           /* Complete lookup of the id-expression.  */
12259           expr = (finish_id_expression
12260                   (id_expression, expr, parser->scope, &idk,
12261                    /*integral_constant_expression_p=*/false,
12262                    /*allow_non_integral_constant_expression_p=*/true,
12263                    &non_integral_constant_expression_p,
12264                    /*template_p=*/false,
12265                    /*done=*/true,
12266                    /*address_p=*/false,
12267                    /*template_arg_p=*/false,
12268                    &error_msg,
12269                    id_expr_start_token->location));
12270
12271           if (expr == error_mark_node)
12272             /* We found an id-expression, but it was something that we
12273                should not have found. This is an error, not something
12274                we can recover from, so note that we found an
12275                id-expression and we'll recover as gracefully as
12276                possible.  */
12277             id_expression_or_member_access_p = true;
12278         }
12279
12280       if (expr 
12281           && expr != error_mark_node
12282           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12283         /* We have an id-expression.  */
12284         id_expression_or_member_access_p = true;
12285     }
12286
12287   if (!id_expression_or_member_access_p)
12288     {
12289       /* Abort the id-expression parse.  */
12290       cp_parser_abort_tentative_parse (parser);
12291
12292       /* Parsing tentatively, again.  */
12293       cp_parser_parse_tentatively (parser);
12294
12295       /* Parse a class member access.  */
12296       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12297                                            /*cast_p=*/false, /*decltype*/true,
12298                                            /*member_access_only_p=*/true, NULL);
12299
12300       if (expr 
12301           && expr != error_mark_node
12302           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12303         /* We have an id-expression.  */
12304         id_expression_or_member_access_p = true;
12305     }
12306
12307   if (id_expression_or_member_access_p)
12308     /* We have parsed the complete id-expression or member access.  */
12309     cp_parser_parse_definitely (parser);
12310   else
12311     {
12312       /* Abort our attempt to parse an id-expression or member access
12313          expression.  */
12314       cp_parser_abort_tentative_parse (parser);
12315
12316       /* Parse a full expression.  */
12317       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12318                                    /*decltype_p=*/true);
12319     }
12320
12321   return expr;
12322 }
12323
12324 /* Parse a `decltype' type. Returns the type.
12325
12326    simple-type-specifier:
12327      decltype ( expression )
12328    C++14 proposal:
12329      decltype ( auto )  */
12330
12331 static tree
12332 cp_parser_decltype (cp_parser *parser)
12333 {
12334   tree expr;
12335   bool id_expression_or_member_access_p = false;
12336   const char *saved_message;
12337   bool saved_integral_constant_expression_p;
12338   bool saved_non_integral_constant_expression_p;
12339   bool saved_greater_than_is_operator_p;
12340   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12341
12342   if (start_token->type == CPP_DECLTYPE)
12343     {
12344       /* Already parsed.  */
12345       cp_lexer_consume_token (parser->lexer);
12346       return start_token->u.value;
12347     }
12348
12349   /* Look for the `decltype' token.  */
12350   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12351     return error_mark_node;
12352
12353   /* Parse the opening `('.  */
12354   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12355     return error_mark_node;
12356
12357   /* decltype (auto) */
12358   if (cxx_dialect >= cxx14
12359       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12360     {
12361       cp_lexer_consume_token (parser->lexer);
12362       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12363         return error_mark_node;
12364       expr = make_decltype_auto ();
12365       AUTO_IS_DECLTYPE (expr) = true;
12366       goto rewrite;
12367     }
12368
12369   /* Types cannot be defined in a `decltype' expression.  Save away the
12370      old message.  */
12371   saved_message = parser->type_definition_forbidden_message;
12372
12373   /* And create the new one.  */
12374   parser->type_definition_forbidden_message
12375     = G_("types may not be defined in %<decltype%> expressions");
12376
12377   /* The restrictions on constant-expressions do not apply inside
12378      decltype expressions.  */
12379   saved_integral_constant_expression_p
12380     = parser->integral_constant_expression_p;
12381   saved_non_integral_constant_expression_p
12382     = parser->non_integral_constant_expression_p;
12383   parser->integral_constant_expression_p = false;
12384
12385   /* Within a parenthesized expression, a `>' token is always
12386      the greater-than operator.  */
12387   saved_greater_than_is_operator_p
12388     = parser->greater_than_is_operator_p;
12389   parser->greater_than_is_operator_p = true;
12390
12391   /* Do not actually evaluate the expression.  */
12392   ++cp_unevaluated_operand;
12393
12394   /* Do not warn about problems with the expression.  */
12395   ++c_inhibit_evaluation_warnings;
12396
12397   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12398
12399   /* Go back to evaluating expressions.  */
12400   --cp_unevaluated_operand;
12401   --c_inhibit_evaluation_warnings;
12402
12403   /* The `>' token might be the end of a template-id or
12404      template-parameter-list now.  */
12405   parser->greater_than_is_operator_p
12406     = saved_greater_than_is_operator_p;
12407
12408   /* Restore the old message and the integral constant expression
12409      flags.  */
12410   parser->type_definition_forbidden_message = saved_message;
12411   parser->integral_constant_expression_p
12412     = saved_integral_constant_expression_p;
12413   parser->non_integral_constant_expression_p
12414     = saved_non_integral_constant_expression_p;
12415
12416   /* Parse to the closing `)'.  */
12417   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12418     {
12419       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12420                                              /*consume_paren=*/true);
12421       return error_mark_node;
12422     }
12423
12424   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12425                                tf_warning_or_error);
12426
12427  rewrite:
12428   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12429      it again.  */
12430   start_token->type = CPP_DECLTYPE;
12431   start_token->u.value = expr;
12432   start_token->keyword = RID_MAX;
12433   cp_lexer_purge_tokens_after (parser->lexer, start_token);
12434
12435   return expr;
12436 }
12437
12438 /* Special member functions [gram.special] */
12439
12440 /* Parse a conversion-function-id.
12441
12442    conversion-function-id:
12443      operator conversion-type-id
12444
12445    Returns an IDENTIFIER_NODE representing the operator.  */
12446
12447 static tree
12448 cp_parser_conversion_function_id (cp_parser* parser)
12449 {
12450   tree type;
12451   tree saved_scope;
12452   tree saved_qualifying_scope;
12453   tree saved_object_scope;
12454   tree pushed_scope = NULL_TREE;
12455
12456   /* Look for the `operator' token.  */
12457   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12458     return error_mark_node;
12459   /* When we parse the conversion-type-id, the current scope will be
12460      reset.  However, we need that information in able to look up the
12461      conversion function later, so we save it here.  */
12462   saved_scope = parser->scope;
12463   saved_qualifying_scope = parser->qualifying_scope;
12464   saved_object_scope = parser->object_scope;
12465   /* We must enter the scope of the class so that the names of
12466      entities declared within the class are available in the
12467      conversion-type-id.  For example, consider:
12468
12469        struct S {
12470          typedef int I;
12471          operator I();
12472        };
12473
12474        S::operator I() { ... }
12475
12476      In order to see that `I' is a type-name in the definition, we
12477      must be in the scope of `S'.  */
12478   if (saved_scope)
12479     pushed_scope = push_scope (saved_scope);
12480   /* Parse the conversion-type-id.  */
12481   type = cp_parser_conversion_type_id (parser);
12482   /* Leave the scope of the class, if any.  */
12483   if (pushed_scope)
12484     pop_scope (pushed_scope);
12485   /* Restore the saved scope.  */
12486   parser->scope = saved_scope;
12487   parser->qualifying_scope = saved_qualifying_scope;
12488   parser->object_scope = saved_object_scope;
12489   /* If the TYPE is invalid, indicate failure.  */
12490   if (type == error_mark_node)
12491     return error_mark_node;
12492   return mangle_conv_op_name_for_type (type);
12493 }
12494
12495 /* Parse a conversion-type-id:
12496
12497    conversion-type-id:
12498      type-specifier-seq conversion-declarator [opt]
12499
12500    Returns the TYPE specified.  */
12501
12502 static tree
12503 cp_parser_conversion_type_id (cp_parser* parser)
12504 {
12505   tree attributes;
12506   cp_decl_specifier_seq type_specifiers;
12507   cp_declarator *declarator;
12508   tree type_specified;
12509   const char *saved_message;
12510
12511   /* Parse the attributes.  */
12512   attributes = cp_parser_attributes_opt (parser);
12513
12514   saved_message = parser->type_definition_forbidden_message;
12515   parser->type_definition_forbidden_message
12516     = G_("types may not be defined in a conversion-type-id");
12517
12518   /* Parse the type-specifiers.  */
12519   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12520                                 /*is_trailing_return=*/false,
12521                                 &type_specifiers);
12522
12523   parser->type_definition_forbidden_message = saved_message;
12524
12525   /* If that didn't work, stop.  */
12526   if (type_specifiers.type == error_mark_node)
12527     return error_mark_node;
12528   /* Parse the conversion-declarator.  */
12529   declarator = cp_parser_conversion_declarator_opt (parser);
12530
12531   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
12532                                     /*initialized=*/0, &attributes);
12533   if (attributes)
12534     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12535
12536   /* Don't give this error when parsing tentatively.  This happens to
12537      work because we always parse this definitively once.  */
12538   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12539       && type_uses_auto (type_specified))
12540     {
12541       if (cxx_dialect < cxx14)
12542         {
12543           error ("invalid use of %<auto%> in conversion operator");
12544           return error_mark_node;
12545         }
12546       else if (template_parm_scope_p ())
12547         warning (0, "use of %<auto%> in member template "
12548                  "conversion operator can never be deduced");
12549     }
12550
12551   return type_specified;
12552 }
12553
12554 /* Parse an (optional) conversion-declarator.
12555
12556    conversion-declarator:
12557      ptr-operator conversion-declarator [opt]
12558
12559    */
12560
12561 static cp_declarator *
12562 cp_parser_conversion_declarator_opt (cp_parser* parser)
12563 {
12564   enum tree_code code;
12565   tree class_type, std_attributes = NULL_TREE;
12566   cp_cv_quals cv_quals;
12567
12568   /* We don't know if there's a ptr-operator next, or not.  */
12569   cp_parser_parse_tentatively (parser);
12570   /* Try the ptr-operator.  */
12571   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12572                                  &std_attributes);
12573   /* If it worked, look for more conversion-declarators.  */
12574   if (cp_parser_parse_definitely (parser))
12575     {
12576       cp_declarator *declarator;
12577
12578       /* Parse another optional declarator.  */
12579       declarator = cp_parser_conversion_declarator_opt (parser);
12580
12581       declarator = cp_parser_make_indirect_declarator
12582         (code, class_type, cv_quals, declarator, std_attributes);
12583
12584       return declarator;
12585    }
12586
12587   return NULL;
12588 }
12589
12590 /* Parse an (optional) ctor-initializer.
12591
12592    ctor-initializer:
12593      : mem-initializer-list
12594
12595    Returns TRUE iff the ctor-initializer was actually present.  */
12596
12597 static bool
12598 cp_parser_ctor_initializer_opt (cp_parser* parser)
12599 {
12600   /* If the next token is not a `:', then there is no
12601      ctor-initializer.  */
12602   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12603     {
12604       /* Do default initialization of any bases and members.  */
12605       if (DECL_CONSTRUCTOR_P (current_function_decl))
12606         finish_mem_initializers (NULL_TREE);
12607
12608       return false;
12609     }
12610
12611   /* Consume the `:' token.  */
12612   cp_lexer_consume_token (parser->lexer);
12613   /* And the mem-initializer-list.  */
12614   cp_parser_mem_initializer_list (parser);
12615
12616   return true;
12617 }
12618
12619 /* Parse a mem-initializer-list.
12620
12621    mem-initializer-list:
12622      mem-initializer ... [opt]
12623      mem-initializer ... [opt] , mem-initializer-list  */
12624
12625 static void
12626 cp_parser_mem_initializer_list (cp_parser* parser)
12627 {
12628   tree mem_initializer_list = NULL_TREE;
12629   tree target_ctor = error_mark_node;
12630   cp_token *token = cp_lexer_peek_token (parser->lexer);
12631
12632   /* Let the semantic analysis code know that we are starting the
12633      mem-initializer-list.  */
12634   if (!DECL_CONSTRUCTOR_P (current_function_decl))
12635     error_at (token->location,
12636               "only constructors take member initializers");
12637
12638   /* Loop through the list.  */
12639   while (true)
12640     {
12641       tree mem_initializer;
12642
12643       token = cp_lexer_peek_token (parser->lexer);
12644       /* Parse the mem-initializer.  */
12645       mem_initializer = cp_parser_mem_initializer (parser);
12646       /* If the next token is a `...', we're expanding member initializers. */
12647       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12648         {
12649           /* Consume the `...'. */
12650           cp_lexer_consume_token (parser->lexer);
12651
12652           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12653              can be expanded but members cannot. */
12654           if (mem_initializer != error_mark_node
12655               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12656             {
12657               error_at (token->location,
12658                         "cannot expand initializer for member %<%D%>",
12659                         TREE_PURPOSE (mem_initializer));
12660               mem_initializer = error_mark_node;
12661             }
12662
12663           /* Construct the pack expansion type. */
12664           if (mem_initializer != error_mark_node)
12665             mem_initializer = make_pack_expansion (mem_initializer);
12666         }
12667       if (target_ctor != error_mark_node
12668           && mem_initializer != error_mark_node)
12669         {
12670           error ("mem-initializer for %qD follows constructor delegation",
12671                  TREE_PURPOSE (mem_initializer));
12672           mem_initializer = error_mark_node;
12673         }
12674       /* Look for a target constructor. */
12675       if (mem_initializer != error_mark_node
12676           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12677           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12678         {
12679           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12680           if (mem_initializer_list)
12681             {
12682               error ("constructor delegation follows mem-initializer for %qD",
12683                      TREE_PURPOSE (mem_initializer_list));
12684               mem_initializer = error_mark_node;
12685             }
12686           target_ctor = mem_initializer;
12687         }
12688       /* Add it to the list, unless it was erroneous.  */
12689       if (mem_initializer != error_mark_node)
12690         {
12691           TREE_CHAIN (mem_initializer) = mem_initializer_list;
12692           mem_initializer_list = mem_initializer;
12693         }
12694       /* If the next token is not a `,', we're done.  */
12695       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12696         break;
12697       /* Consume the `,' token.  */
12698       cp_lexer_consume_token (parser->lexer);
12699     }
12700
12701   /* Perform semantic analysis.  */
12702   if (DECL_CONSTRUCTOR_P (current_function_decl))
12703     finish_mem_initializers (mem_initializer_list);
12704 }
12705
12706 /* Parse a mem-initializer.
12707
12708    mem-initializer:
12709      mem-initializer-id ( expression-list [opt] )
12710      mem-initializer-id braced-init-list
12711
12712    GNU extension:
12713
12714    mem-initializer:
12715      ( expression-list [opt] )
12716
12717    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
12718    class) or FIELD_DECL (for a non-static data member) to initialize;
12719    the TREE_VALUE is the expression-list.  An empty initialization
12720    list is represented by void_list_node.  */
12721
12722 static tree
12723 cp_parser_mem_initializer (cp_parser* parser)
12724 {
12725   tree mem_initializer_id;
12726   tree expression_list;
12727   tree member;
12728   cp_token *token = cp_lexer_peek_token (parser->lexer);
12729
12730   /* Find out what is being initialized.  */
12731   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12732     {
12733       permerror (token->location,
12734                  "anachronistic old-style base class initializer");
12735       mem_initializer_id = NULL_TREE;
12736     }
12737   else
12738     {
12739       mem_initializer_id = cp_parser_mem_initializer_id (parser);
12740       if (mem_initializer_id == error_mark_node)
12741         return mem_initializer_id;
12742     }
12743   member = expand_member_init (mem_initializer_id);
12744   if (member && !DECL_P (member))
12745     in_base_initializer = 1;
12746
12747   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12748     {
12749       bool expr_non_constant_p;
12750       cp_lexer_set_source_position (parser->lexer);
12751       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12752       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12753       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12754       expression_list = build_tree_list (NULL_TREE, expression_list);
12755     }
12756   else
12757     {
12758       vec<tree, va_gc> *vec;
12759       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12760                                                      /*cast_p=*/false,
12761                                                      /*allow_expansion_p=*/true,
12762                                                      /*non_constant_p=*/NULL);
12763       if (vec == NULL)
12764         return error_mark_node;
12765       expression_list = build_tree_list_vec (vec);
12766       release_tree_vector (vec);
12767     }
12768
12769   if (expression_list == error_mark_node)
12770     return error_mark_node;
12771   if (!expression_list)
12772     expression_list = void_type_node;
12773
12774   in_base_initializer = 0;
12775
12776   return member ? build_tree_list (member, expression_list) : error_mark_node;
12777 }
12778
12779 /* Parse a mem-initializer-id.
12780
12781    mem-initializer-id:
12782      :: [opt] nested-name-specifier [opt] class-name
12783      identifier
12784
12785    Returns a TYPE indicating the class to be initializer for the first
12786    production.  Returns an IDENTIFIER_NODE indicating the data member
12787    to be initialized for the second production.  */
12788
12789 static tree
12790 cp_parser_mem_initializer_id (cp_parser* parser)
12791 {
12792   bool global_scope_p;
12793   bool nested_name_specifier_p;
12794   bool template_p = false;
12795   tree id;
12796
12797   cp_token *token = cp_lexer_peek_token (parser->lexer);
12798
12799   /* `typename' is not allowed in this context ([temp.res]).  */
12800   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12801     {
12802       error_at (token->location, 
12803                 "keyword %<typename%> not allowed in this context (a qualified "
12804                 "member initializer is implicitly a type)");
12805       cp_lexer_consume_token (parser->lexer);
12806     }
12807   /* Look for the optional `::' operator.  */
12808   global_scope_p
12809     = (cp_parser_global_scope_opt (parser,
12810                                    /*current_scope_valid_p=*/false)
12811        != NULL_TREE);
12812   /* Look for the optional nested-name-specifier.  The simplest way to
12813      implement:
12814
12815        [temp.res]
12816
12817        The keyword `typename' is not permitted in a base-specifier or
12818        mem-initializer; in these contexts a qualified name that
12819        depends on a template-parameter is implicitly assumed to be a
12820        type name.
12821
12822      is to assume that we have seen the `typename' keyword at this
12823      point.  */
12824   nested_name_specifier_p
12825     = (cp_parser_nested_name_specifier_opt (parser,
12826                                             /*typename_keyword_p=*/true,
12827                                             /*check_dependency_p=*/true,
12828                                             /*type_p=*/true,
12829                                             /*is_declaration=*/true)
12830        != NULL_TREE);
12831   if (nested_name_specifier_p)
12832     template_p = cp_parser_optional_template_keyword (parser);
12833   /* If there is a `::' operator or a nested-name-specifier, then we
12834      are definitely looking for a class-name.  */
12835   if (global_scope_p || nested_name_specifier_p)
12836     return cp_parser_class_name (parser,
12837                                  /*typename_keyword_p=*/true,
12838                                  /*template_keyword_p=*/template_p,
12839                                  typename_type,
12840                                  /*check_dependency_p=*/true,
12841                                  /*class_head_p=*/false,
12842                                  /*is_declaration=*/true);
12843   /* Otherwise, we could also be looking for an ordinary identifier.  */
12844   cp_parser_parse_tentatively (parser);
12845   /* Try a class-name.  */
12846   id = cp_parser_class_name (parser,
12847                              /*typename_keyword_p=*/true,
12848                              /*template_keyword_p=*/false,
12849                              none_type,
12850                              /*check_dependency_p=*/true,
12851                              /*class_head_p=*/false,
12852                              /*is_declaration=*/true);
12853   /* If we found one, we're done.  */
12854   if (cp_parser_parse_definitely (parser))
12855     return id;
12856   /* Otherwise, look for an ordinary identifier.  */
12857   return cp_parser_identifier (parser);
12858 }
12859
12860 /* Overloading [gram.over] */
12861
12862 /* Parse an operator-function-id.
12863
12864    operator-function-id:
12865      operator operator
12866
12867    Returns an IDENTIFIER_NODE for the operator which is a
12868    human-readable spelling of the identifier, e.g., `operator +'.  */
12869
12870 static tree
12871 cp_parser_operator_function_id (cp_parser* parser)
12872 {
12873   /* Look for the `operator' keyword.  */
12874   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12875     return error_mark_node;
12876   /* And then the name of the operator itself.  */
12877   return cp_parser_operator (parser);
12878 }
12879
12880 /* Return an identifier node for a user-defined literal operator.
12881    The suffix identifier is chained to the operator name identifier.  */
12882
12883 static tree
12884 cp_literal_operator_id (const char* name)
12885 {
12886   tree identifier;
12887   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12888                               + strlen (name) + 10);
12889   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12890   identifier = get_identifier (buffer);
12891
12892   return identifier;
12893 }
12894
12895 /* Parse an operator.
12896
12897    operator:
12898      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12899      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12900      || ++ -- , ->* -> () []
12901
12902    GNU Extensions:
12903
12904    operator:
12905      <? >? <?= >?=
12906
12907    Returns an IDENTIFIER_NODE for the operator which is a
12908    human-readable spelling of the identifier, e.g., `operator +'.  */
12909
12910 static tree
12911 cp_parser_operator (cp_parser* parser)
12912 {
12913   tree id = NULL_TREE;
12914   cp_token *token;
12915   bool utf8 = false;
12916
12917   /* Peek at the next token.  */
12918   token = cp_lexer_peek_token (parser->lexer);
12919   /* Figure out which operator we have.  */
12920   switch (token->type)
12921     {
12922     case CPP_KEYWORD:
12923       {
12924         enum tree_code op;
12925
12926         /* The keyword should be either `new' or `delete'.  */
12927         if (token->keyword == RID_NEW)
12928           op = NEW_EXPR;
12929         else if (token->keyword == RID_DELETE)
12930           op = DELETE_EXPR;
12931         else
12932           break;
12933
12934         /* Consume the `new' or `delete' token.  */
12935         cp_lexer_consume_token (parser->lexer);
12936
12937         /* Peek at the next token.  */
12938         token = cp_lexer_peek_token (parser->lexer);
12939         /* If it's a `[' token then this is the array variant of the
12940            operator.  */
12941         if (token->type == CPP_OPEN_SQUARE)
12942           {
12943             /* Consume the `[' token.  */
12944             cp_lexer_consume_token (parser->lexer);
12945             /* Look for the `]' token.  */
12946             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12947             id = ansi_opname (op == NEW_EXPR
12948                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12949           }
12950         /* Otherwise, we have the non-array variant.  */
12951         else
12952           id = ansi_opname (op);
12953
12954         return id;
12955       }
12956
12957     case CPP_PLUS:
12958       id = ansi_opname (PLUS_EXPR);
12959       break;
12960
12961     case CPP_MINUS:
12962       id = ansi_opname (MINUS_EXPR);
12963       break;
12964
12965     case CPP_MULT:
12966       id = ansi_opname (MULT_EXPR);
12967       break;
12968
12969     case CPP_DIV:
12970       id = ansi_opname (TRUNC_DIV_EXPR);
12971       break;
12972
12973     case CPP_MOD:
12974       id = ansi_opname (TRUNC_MOD_EXPR);
12975       break;
12976
12977     case CPP_XOR:
12978       id = ansi_opname (BIT_XOR_EXPR);
12979       break;
12980
12981     case CPP_AND:
12982       id = ansi_opname (BIT_AND_EXPR);
12983       break;
12984
12985     case CPP_OR:
12986       id = ansi_opname (BIT_IOR_EXPR);
12987       break;
12988
12989     case CPP_COMPL:
12990       id = ansi_opname (BIT_NOT_EXPR);
12991       break;
12992
12993     case CPP_NOT:
12994       id = ansi_opname (TRUTH_NOT_EXPR);
12995       break;
12996
12997     case CPP_EQ:
12998       id = ansi_assopname (NOP_EXPR);
12999       break;
13000
13001     case CPP_LESS:
13002       id = ansi_opname (LT_EXPR);
13003       break;
13004
13005     case CPP_GREATER:
13006       id = ansi_opname (GT_EXPR);
13007       break;
13008
13009     case CPP_PLUS_EQ:
13010       id = ansi_assopname (PLUS_EXPR);
13011       break;
13012
13013     case CPP_MINUS_EQ:
13014       id = ansi_assopname (MINUS_EXPR);
13015       break;
13016
13017     case CPP_MULT_EQ:
13018       id = ansi_assopname (MULT_EXPR);
13019       break;
13020
13021     case CPP_DIV_EQ:
13022       id = ansi_assopname (TRUNC_DIV_EXPR);
13023       break;
13024
13025     case CPP_MOD_EQ:
13026       id = ansi_assopname (TRUNC_MOD_EXPR);
13027       break;
13028
13029     case CPP_XOR_EQ:
13030       id = ansi_assopname (BIT_XOR_EXPR);
13031       break;
13032
13033     case CPP_AND_EQ:
13034       id = ansi_assopname (BIT_AND_EXPR);
13035       break;
13036
13037     case CPP_OR_EQ:
13038       id = ansi_assopname (BIT_IOR_EXPR);
13039       break;
13040
13041     case CPP_LSHIFT:
13042       id = ansi_opname (LSHIFT_EXPR);
13043       break;
13044
13045     case CPP_RSHIFT:
13046       id = ansi_opname (RSHIFT_EXPR);
13047       break;
13048
13049     case CPP_LSHIFT_EQ:
13050       id = ansi_assopname (LSHIFT_EXPR);
13051       break;
13052
13053     case CPP_RSHIFT_EQ:
13054       id = ansi_assopname (RSHIFT_EXPR);
13055       break;
13056
13057     case CPP_EQ_EQ:
13058       id = ansi_opname (EQ_EXPR);
13059       break;
13060
13061     case CPP_NOT_EQ:
13062       id = ansi_opname (NE_EXPR);
13063       break;
13064
13065     case CPP_LESS_EQ:
13066       id = ansi_opname (LE_EXPR);
13067       break;
13068
13069     case CPP_GREATER_EQ:
13070       id = ansi_opname (GE_EXPR);
13071       break;
13072
13073     case CPP_AND_AND:
13074       id = ansi_opname (TRUTH_ANDIF_EXPR);
13075       break;
13076
13077     case CPP_OR_OR:
13078       id = ansi_opname (TRUTH_ORIF_EXPR);
13079       break;
13080
13081     case CPP_PLUS_PLUS:
13082       id = ansi_opname (POSTINCREMENT_EXPR);
13083       break;
13084
13085     case CPP_MINUS_MINUS:
13086       id = ansi_opname (PREDECREMENT_EXPR);
13087       break;
13088
13089     case CPP_COMMA:
13090       id = ansi_opname (COMPOUND_EXPR);
13091       break;
13092
13093     case CPP_DEREF_STAR:
13094       id = ansi_opname (MEMBER_REF);
13095       break;
13096
13097     case CPP_DEREF:
13098       id = ansi_opname (COMPONENT_REF);
13099       break;
13100
13101     case CPP_OPEN_PAREN:
13102       /* Consume the `('.  */
13103       cp_lexer_consume_token (parser->lexer);
13104       /* Look for the matching `)'.  */
13105       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13106       return ansi_opname (CALL_EXPR);
13107
13108     case CPP_OPEN_SQUARE:
13109       /* Consume the `['.  */
13110       cp_lexer_consume_token (parser->lexer);
13111       /* Look for the matching `]'.  */
13112       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13113       return ansi_opname (ARRAY_REF);
13114
13115     case CPP_UTF8STRING:
13116     case CPP_UTF8STRING_USERDEF:
13117       utf8 = true;
13118     case CPP_STRING:
13119     case CPP_WSTRING:
13120     case CPP_STRING16:
13121     case CPP_STRING32:
13122     case CPP_STRING_USERDEF:
13123     case CPP_WSTRING_USERDEF:
13124     case CPP_STRING16_USERDEF:
13125     case CPP_STRING32_USERDEF:
13126       {
13127         tree str, string_tree;
13128         int sz, len;
13129
13130         if (cxx_dialect == cxx98)
13131           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13132
13133         /* Consume the string.  */
13134         str = cp_parser_string_literal (parser, /*translate=*/true,
13135                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
13136         if (str == error_mark_node)
13137           return error_mark_node;
13138         else if (TREE_CODE (str) == USERDEF_LITERAL)
13139           {
13140             string_tree = USERDEF_LITERAL_VALUE (str);
13141             id = USERDEF_LITERAL_SUFFIX_ID (str);
13142           }
13143         else
13144           {
13145             string_tree = str;
13146             /* Look for the suffix identifier.  */
13147             token = cp_lexer_peek_token (parser->lexer);
13148             if (token->type == CPP_NAME)
13149               id = cp_parser_identifier (parser);
13150             else if (token->type == CPP_KEYWORD)
13151               {
13152                 error ("unexpected keyword;"
13153                        " remove space between quotes and suffix identifier");
13154                 return error_mark_node;
13155               }
13156             else
13157               {
13158                 error ("expected suffix identifier");
13159                 return error_mark_node;
13160               }
13161           }
13162         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13163                                (TREE_TYPE (TREE_TYPE (string_tree))));
13164         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13165         if (len != 0)
13166           {
13167             error ("expected empty string after %<operator%> keyword");
13168             return error_mark_node;
13169           }
13170         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13171             != char_type_node)
13172           {
13173             error ("invalid encoding prefix in literal operator");
13174             return error_mark_node;
13175           }
13176         if (id != error_mark_node)
13177           {
13178             const char *name = IDENTIFIER_POINTER (id);
13179             id = cp_literal_operator_id (name);
13180           }
13181         return id;
13182       }
13183
13184     default:
13185       /* Anything else is an error.  */
13186       break;
13187     }
13188
13189   /* If we have selected an identifier, we need to consume the
13190      operator token.  */
13191   if (id)
13192     cp_lexer_consume_token (parser->lexer);
13193   /* Otherwise, no valid operator name was present.  */
13194   else
13195     {
13196       cp_parser_error (parser, "expected operator");
13197       id = error_mark_node;
13198     }
13199
13200   return id;
13201 }
13202
13203 /* Parse a template-declaration.
13204
13205    template-declaration:
13206      export [opt] template < template-parameter-list > declaration
13207
13208    If MEMBER_P is TRUE, this template-declaration occurs within a
13209    class-specifier.
13210
13211    The grammar rule given by the standard isn't correct.  What
13212    is really meant is:
13213
13214    template-declaration:
13215      export [opt] template-parameter-list-seq
13216        decl-specifier-seq [opt] init-declarator [opt] ;
13217      export [opt] template-parameter-list-seq
13218        function-definition
13219
13220    template-parameter-list-seq:
13221      template-parameter-list-seq [opt]
13222      template < template-parameter-list >  */
13223
13224 static void
13225 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13226 {
13227   /* Check for `export'.  */
13228   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13229     {
13230       /* Consume the `export' token.  */
13231       cp_lexer_consume_token (parser->lexer);
13232       /* Warn that we do not support `export'.  */
13233       warning (0, "keyword %<export%> not implemented, and will be ignored");
13234     }
13235
13236   cp_parser_template_declaration_after_export (parser, member_p);
13237 }
13238
13239 /* Parse a template-parameter-list.
13240
13241    template-parameter-list:
13242      template-parameter
13243      template-parameter-list , template-parameter
13244
13245    Returns a TREE_LIST.  Each node represents a template parameter.
13246    The nodes are connected via their TREE_CHAINs.  */
13247
13248 static tree
13249 cp_parser_template_parameter_list (cp_parser* parser)
13250 {
13251   tree parameter_list = NULL_TREE;
13252
13253   begin_template_parm_list ();
13254
13255   /* The loop below parses the template parms.  We first need to know
13256      the total number of template parms to be able to compute proper
13257      canonical types of each dependent type. So after the loop, when
13258      we know the total number of template parms,
13259      end_template_parm_list computes the proper canonical types and
13260      fixes up the dependent types accordingly.  */
13261   while (true)
13262     {
13263       tree parameter;
13264       bool is_non_type;
13265       bool is_parameter_pack;
13266       location_t parm_loc;
13267
13268       /* Parse the template-parameter.  */
13269       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13270       parameter = cp_parser_template_parameter (parser, 
13271                                                 &is_non_type,
13272                                                 &is_parameter_pack);
13273       /* Add it to the list.  */
13274       if (parameter != error_mark_node)
13275         parameter_list = process_template_parm (parameter_list,
13276                                                 parm_loc,
13277                                                 parameter,
13278                                                 is_non_type,
13279                                                 is_parameter_pack);
13280       else
13281        {
13282          tree err_parm = build_tree_list (parameter, parameter);
13283          parameter_list = chainon (parameter_list, err_parm);
13284        }
13285
13286       /* If the next token is not a `,', we're done.  */
13287       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13288         break;
13289       /* Otherwise, consume the `,' token.  */
13290       cp_lexer_consume_token (parser->lexer);
13291     }
13292
13293   return end_template_parm_list (parameter_list);
13294 }
13295
13296 /* Parse a template-parameter.
13297
13298    template-parameter:
13299      type-parameter
13300      parameter-declaration
13301
13302    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
13303    the parameter.  The TREE_PURPOSE is the default value, if any.
13304    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
13305    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
13306    set to true iff this parameter is a parameter pack. */
13307
13308 static tree
13309 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13310                               bool *is_parameter_pack)
13311 {
13312   cp_token *token;
13313   cp_parameter_declarator *parameter_declarator;
13314   cp_declarator *id_declarator;
13315   tree parm;
13316
13317   /* Assume it is a type parameter or a template parameter.  */
13318   *is_non_type = false;
13319   /* Assume it not a parameter pack. */
13320   *is_parameter_pack = false;
13321   /* Peek at the next token.  */
13322   token = cp_lexer_peek_token (parser->lexer);
13323   /* If it is `class' or `template', we have a type-parameter.  */
13324   if (token->keyword == RID_TEMPLATE)
13325     return cp_parser_type_parameter (parser, is_parameter_pack);
13326   /* If it is `class' or `typename' we do not know yet whether it is a
13327      type parameter or a non-type parameter.  Consider:
13328
13329        template <typename T, typename T::X X> ...
13330
13331      or:
13332
13333        template <class C, class D*> ...
13334
13335      Here, the first parameter is a type parameter, and the second is
13336      a non-type parameter.  We can tell by looking at the token after
13337      the identifier -- if it is a `,', `=', or `>' then we have a type
13338      parameter.  */
13339   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13340     {
13341       /* Peek at the token after `class' or `typename'.  */
13342       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13343       /* If it's an ellipsis, we have a template type parameter
13344          pack. */
13345       if (token->type == CPP_ELLIPSIS)
13346         return cp_parser_type_parameter (parser, is_parameter_pack);
13347       /* If it's an identifier, skip it.  */
13348       if (token->type == CPP_NAME)
13349         token = cp_lexer_peek_nth_token (parser->lexer, 3);
13350       /* Now, see if the token looks like the end of a template
13351          parameter.  */
13352       if (token->type == CPP_COMMA
13353           || token->type == CPP_EQ
13354           || token->type == CPP_GREATER)
13355         return cp_parser_type_parameter (parser, is_parameter_pack);
13356     }
13357
13358   /* Otherwise, it is a non-type parameter.
13359
13360      [temp.param]
13361
13362      When parsing a default template-argument for a non-type
13363      template-parameter, the first non-nested `>' is taken as the end
13364      of the template parameter-list rather than a greater-than
13365      operator.  */
13366   *is_non_type = true;
13367   parameter_declarator
13368      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13369                                         /*parenthesized_p=*/NULL);
13370
13371   if (!parameter_declarator)
13372     return error_mark_node;
13373
13374   /* If the parameter declaration is marked as a parameter pack, set
13375      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13376      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13377      grokdeclarator. */
13378   if (parameter_declarator->declarator
13379       && parameter_declarator->declarator->parameter_pack_p)
13380     {
13381       *is_parameter_pack = true;
13382       parameter_declarator->declarator->parameter_pack_p = false;
13383     }
13384
13385   if (parameter_declarator->default_argument)
13386     {
13387       /* Can happen in some cases of erroneous input (c++/34892).  */
13388       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13389         /* Consume the `...' for better error recovery.  */
13390         cp_lexer_consume_token (parser->lexer);
13391     }
13392   /* If the next token is an ellipsis, and we don't already have it
13393      marked as a parameter pack, then we have a parameter pack (that
13394      has no declarator).  */
13395   else if (!*is_parameter_pack
13396            && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13397            && (declarator_can_be_parameter_pack
13398                (parameter_declarator->declarator)))
13399     {
13400       /* Consume the `...'.  */
13401       cp_lexer_consume_token (parser->lexer);
13402       maybe_warn_variadic_templates ();
13403       
13404       *is_parameter_pack = true;
13405     }
13406   /* We might end up with a pack expansion as the type of the non-type
13407      template parameter, in which case this is a non-type template
13408      parameter pack.  */
13409   else if (parameter_declarator->decl_specifiers.type
13410            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13411     {
13412       *is_parameter_pack = true;
13413       parameter_declarator->decl_specifiers.type = 
13414         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13415     }
13416
13417   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13418     {
13419       /* Parameter packs cannot have default arguments.  However, a
13420          user may try to do so, so we'll parse them and give an
13421          appropriate diagnostic here.  */
13422
13423       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13424       
13425       /* Find the name of the parameter pack.  */     
13426       id_declarator = parameter_declarator->declarator;
13427       while (id_declarator && id_declarator->kind != cdk_id)
13428         id_declarator = id_declarator->declarator;
13429       
13430       if (id_declarator && id_declarator->kind == cdk_id)
13431         error_at (start_token->location,
13432                   "template parameter pack %qD cannot have a default argument",
13433                   id_declarator->u.id.unqualified_name);
13434       else
13435         error_at (start_token->location,
13436                   "template parameter pack cannot have a default argument");
13437       
13438       /* Parse the default argument, but throw away the result.  */
13439       cp_parser_default_argument (parser, /*template_parm_p=*/true);
13440     }
13441
13442   parm = grokdeclarator (parameter_declarator->declarator,
13443                          &parameter_declarator->decl_specifiers,
13444                          TPARM, /*initialized=*/0,
13445                          /*attrlist=*/NULL);
13446   if (parm == error_mark_node)
13447     return error_mark_node;
13448
13449   return build_tree_list (parameter_declarator->default_argument, parm);
13450 }
13451
13452 /* Parse a type-parameter.
13453
13454    type-parameter:
13455      class identifier [opt]
13456      class identifier [opt] = type-id
13457      typename identifier [opt]
13458      typename identifier [opt] = type-id
13459      template < template-parameter-list > class identifier [opt]
13460      template < template-parameter-list > class identifier [opt]
13461        = id-expression
13462
13463    GNU Extension (variadic templates):
13464
13465    type-parameter:
13466      class ... identifier [opt]
13467      typename ... identifier [opt]
13468
13469    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
13470    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
13471    the declaration of the parameter.
13472
13473    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13474
13475 static tree
13476 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13477 {
13478   cp_token *token;
13479   tree parameter;
13480
13481   /* Look for a keyword to tell us what kind of parameter this is.  */
13482   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13483   if (!token)
13484     return error_mark_node;
13485
13486   switch (token->keyword)
13487     {
13488     case RID_CLASS:
13489     case RID_TYPENAME:
13490       {
13491         tree identifier;
13492         tree default_argument;
13493
13494         /* If the next token is an ellipsis, we have a template
13495            argument pack. */
13496         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13497           {
13498             /* Consume the `...' token. */
13499             cp_lexer_consume_token (parser->lexer);
13500             maybe_warn_variadic_templates ();
13501
13502             *is_parameter_pack = true;
13503           }
13504
13505         /* If the next token is an identifier, then it names the
13506            parameter.  */
13507         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13508           identifier = cp_parser_identifier (parser);
13509         else
13510           identifier = NULL_TREE;
13511
13512         /* Create the parameter.  */
13513         parameter = finish_template_type_parm (class_type_node, identifier);
13514
13515         /* If the next token is an `=', we have a default argument.  */
13516         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13517           {
13518             /* Consume the `=' token.  */
13519             cp_lexer_consume_token (parser->lexer);
13520             /* Parse the default-argument.  */
13521             push_deferring_access_checks (dk_no_deferred);
13522             default_argument = cp_parser_type_id (parser);
13523
13524             /* Template parameter packs cannot have default
13525                arguments. */
13526             if (*is_parameter_pack)
13527               {
13528                 if (identifier)
13529                   error_at (token->location,
13530                             "template parameter pack %qD cannot have a "
13531                             "default argument", identifier);
13532                 else
13533                   error_at (token->location,
13534                             "template parameter packs cannot have "
13535                             "default arguments");
13536                 default_argument = NULL_TREE;
13537               }
13538             else if (check_for_bare_parameter_packs (default_argument))
13539               default_argument = error_mark_node;
13540             pop_deferring_access_checks ();
13541           }
13542         else
13543           default_argument = NULL_TREE;
13544
13545         /* Create the combined representation of the parameter and the
13546            default argument.  */
13547         parameter = build_tree_list (default_argument, parameter);
13548       }
13549       break;
13550
13551     case RID_TEMPLATE:
13552       {
13553         tree identifier;
13554         tree default_argument;
13555
13556         /* Look for the `<'.  */
13557         cp_parser_require (parser, CPP_LESS, RT_LESS);
13558         /* Parse the template-parameter-list.  */
13559         cp_parser_template_parameter_list (parser);
13560         /* Look for the `>'.  */
13561         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13562         /* Look for the `class' or 'typename' keywords.  */
13563         cp_parser_type_parameter_key (parser);
13564         /* If the next token is an ellipsis, we have a template
13565            argument pack. */
13566         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13567           {
13568             /* Consume the `...' token. */
13569             cp_lexer_consume_token (parser->lexer);
13570             maybe_warn_variadic_templates ();
13571
13572             *is_parameter_pack = true;
13573           }
13574         /* If the next token is an `=', then there is a
13575            default-argument.  If the next token is a `>', we are at
13576            the end of the parameter-list.  If the next token is a `,',
13577            then we are at the end of this parameter.  */
13578         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13579             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13580             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13581           {
13582             identifier = cp_parser_identifier (parser);
13583             /* Treat invalid names as if the parameter were nameless.  */
13584             if (identifier == error_mark_node)
13585               identifier = NULL_TREE;
13586           }
13587         else
13588           identifier = NULL_TREE;
13589
13590         /* Create the template parameter.  */
13591         parameter = finish_template_template_parm (class_type_node,
13592                                                    identifier);
13593
13594         /* If the next token is an `=', then there is a
13595            default-argument.  */
13596         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13597           {
13598             bool is_template;
13599
13600             /* Consume the `='.  */
13601             cp_lexer_consume_token (parser->lexer);
13602             /* Parse the id-expression.  */
13603             push_deferring_access_checks (dk_no_deferred);
13604             /* save token before parsing the id-expression, for error
13605                reporting */
13606             token = cp_lexer_peek_token (parser->lexer);
13607             default_argument
13608               = cp_parser_id_expression (parser,
13609                                          /*template_keyword_p=*/false,
13610                                          /*check_dependency_p=*/true,
13611                                          /*template_p=*/&is_template,
13612                                          /*declarator_p=*/false,
13613                                          /*optional_p=*/false);
13614             if (TREE_CODE (default_argument) == TYPE_DECL)
13615               /* If the id-expression was a template-id that refers to
13616                  a template-class, we already have the declaration here,
13617                  so no further lookup is needed.  */
13618                  ;
13619             else
13620               /* Look up the name.  */
13621               default_argument
13622                 = cp_parser_lookup_name (parser, default_argument,
13623                                          none_type,
13624                                          /*is_template=*/is_template,
13625                                          /*is_namespace=*/false,
13626                                          /*check_dependency=*/true,
13627                                          /*ambiguous_decls=*/NULL,
13628                                          token->location);
13629             /* See if the default argument is valid.  */
13630             default_argument
13631               = check_template_template_default_arg (default_argument);
13632
13633             /* Template parameter packs cannot have default
13634                arguments. */
13635             if (*is_parameter_pack)
13636               {
13637                 if (identifier)
13638                   error_at (token->location,
13639                             "template parameter pack %qD cannot "
13640                             "have a default argument",
13641                             identifier);
13642                 else
13643                   error_at (token->location, "template parameter packs cannot "
13644                             "have default arguments");
13645                 default_argument = NULL_TREE;
13646               }
13647             pop_deferring_access_checks ();
13648           }
13649         else
13650           default_argument = NULL_TREE;
13651
13652         /* Create the combined representation of the parameter and the
13653            default argument.  */
13654         parameter = build_tree_list (default_argument, parameter);
13655       }
13656       break;
13657
13658     default:
13659       gcc_unreachable ();
13660       break;
13661     }
13662
13663   return parameter;
13664 }
13665
13666 /* Parse a template-id.
13667
13668    template-id:
13669      template-name < template-argument-list [opt] >
13670
13671    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13672    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
13673    returned.  Otherwise, if the template-name names a function, or set
13674    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
13675    names a class, returns a TYPE_DECL for the specialization.
13676
13677    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13678    uninstantiated templates.  */
13679
13680 static tree
13681 cp_parser_template_id (cp_parser *parser,
13682                        bool template_keyword_p,
13683                        bool check_dependency_p,
13684                        enum tag_types tag_type,
13685                        bool is_declaration)
13686 {
13687   int i;
13688   tree templ;
13689   tree arguments;
13690   tree template_id;
13691   cp_token_position start_of_id = 0;
13692   deferred_access_check *chk;
13693   vec<deferred_access_check, va_gc> *access_check;
13694   cp_token *next_token = NULL, *next_token_2 = NULL;
13695   bool is_identifier;
13696
13697   /* If the next token corresponds to a template-id, there is no need
13698      to reparse it.  */
13699   next_token = cp_lexer_peek_token (parser->lexer);
13700   if (next_token->type == CPP_TEMPLATE_ID)
13701     {
13702       struct tree_check *check_value;
13703
13704       /* Get the stored value.  */
13705       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13706       /* Perform any access checks that were deferred.  */
13707       access_check = check_value->checks;
13708       if (access_check)
13709         {
13710           FOR_EACH_VEC_ELT (*access_check, i, chk)
13711             perform_or_defer_access_check (chk->binfo,
13712                                            chk->decl,
13713                                            chk->diag_decl,
13714                                            tf_warning_or_error);
13715         }
13716       /* Return the stored value.  */
13717       return check_value->value;
13718     }
13719
13720   /* Avoid performing name lookup if there is no possibility of
13721      finding a template-id.  */
13722   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13723       || (next_token->type == CPP_NAME
13724           && !cp_parser_nth_token_starts_template_argument_list_p
13725                (parser, 2)))
13726     {
13727       cp_parser_error (parser, "expected template-id");
13728       return error_mark_node;
13729     }
13730
13731   /* Remember where the template-id starts.  */
13732   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13733     start_of_id = cp_lexer_token_position (parser->lexer, false);
13734
13735   push_deferring_access_checks (dk_deferred);
13736
13737   /* Parse the template-name.  */
13738   is_identifier = false;
13739   templ = cp_parser_template_name (parser, template_keyword_p,
13740                                    check_dependency_p,
13741                                    is_declaration,
13742                                    tag_type,
13743                                    &is_identifier);
13744   if (templ == error_mark_node || is_identifier)
13745     {
13746       pop_deferring_access_checks ();
13747       return templ;
13748     }
13749
13750   /* If we find the sequence `[:' after a template-name, it's probably
13751      a digraph-typo for `< ::'. Substitute the tokens and check if we can
13752      parse correctly the argument list.  */
13753   next_token = cp_lexer_peek_token (parser->lexer);
13754   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13755   if (next_token->type == CPP_OPEN_SQUARE
13756       && next_token->flags & DIGRAPH
13757       && next_token_2->type == CPP_COLON
13758       && !(next_token_2->flags & PREV_WHITE))
13759     {
13760       cp_parser_parse_tentatively (parser);
13761       /* Change `:' into `::'.  */
13762       next_token_2->type = CPP_SCOPE;
13763       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13764          CPP_LESS.  */
13765       cp_lexer_consume_token (parser->lexer);
13766
13767       /* Parse the arguments.  */
13768       arguments = cp_parser_enclosed_template_argument_list (parser);
13769       if (!cp_parser_parse_definitely (parser))
13770         {
13771           /* If we couldn't parse an argument list, then we revert our changes
13772              and return simply an error. Maybe this is not a template-id
13773              after all.  */
13774           next_token_2->type = CPP_COLON;
13775           cp_parser_error (parser, "expected %<<%>");
13776           pop_deferring_access_checks ();
13777           return error_mark_node;
13778         }
13779       /* Otherwise, emit an error about the invalid digraph, but continue
13780          parsing because we got our argument list.  */
13781       if (permerror (next_token->location,
13782                      "%<<::%> cannot begin a template-argument list"))
13783         {
13784           static bool hint = false;
13785           inform (next_token->location,
13786                   "%<<:%> is an alternate spelling for %<[%>."
13787                   " Insert whitespace between %<<%> and %<::%>");
13788           if (!hint && !flag_permissive)
13789             {
13790               inform (next_token->location, "(if you use %<-fpermissive%> "
13791                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13792                       "accept your code)");
13793               hint = true;
13794             }
13795         }
13796     }
13797   else
13798     {
13799       /* Look for the `<' that starts the template-argument-list.  */
13800       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13801         {
13802           pop_deferring_access_checks ();
13803           return error_mark_node;
13804         }
13805       /* Parse the arguments.  */
13806       arguments = cp_parser_enclosed_template_argument_list (parser);
13807     }
13808
13809   /* Build a representation of the specialization.  */
13810   if (identifier_p (templ))
13811     template_id = build_min_nt_loc (next_token->location,
13812                                     TEMPLATE_ID_EXPR,
13813                                     templ, arguments);
13814   else if (DECL_TYPE_TEMPLATE_P (templ)
13815            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13816     {
13817       bool entering_scope;
13818       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13819          template (rather than some instantiation thereof) only if
13820          is not nested within some other construct.  For example, in
13821          "template <typename T> void f(T) { A<T>::", A<T> is just an
13822          instantiation of A.  */
13823       entering_scope = (template_parm_scope_p ()
13824                         && cp_lexer_next_token_is (parser->lexer,
13825                                                    CPP_SCOPE));
13826       template_id
13827         = finish_template_type (templ, arguments, entering_scope);
13828     }
13829   else if (variable_template_p (templ))
13830     {
13831       template_id = lookup_template_variable (templ, arguments);
13832     }
13833   else
13834     {
13835       /* If it's not a class-template or a template-template, it should be
13836          a function-template.  */
13837       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13838                    || TREE_CODE (templ) == OVERLOAD
13839                    || BASELINK_P (templ)));
13840
13841       template_id = lookup_template_function (templ, arguments);
13842     }
13843
13844   /* If parsing tentatively, replace the sequence of tokens that makes
13845      up the template-id with a CPP_TEMPLATE_ID token.  That way,
13846      should we re-parse the token stream, we will not have to repeat
13847      the effort required to do the parse, nor will we issue duplicate
13848      error messages about problems during instantiation of the
13849      template.  */
13850   if (start_of_id
13851       /* Don't do this if we had a parse error in a declarator; re-parsing
13852          might succeed if a name changes meaning (60361).  */
13853       && !(cp_parser_error_occurred (parser)
13854            && cp_parser_parsing_tentatively (parser)
13855            && parser->in_declarator_p))
13856     {
13857       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13858
13859       /* Reset the contents of the START_OF_ID token.  */
13860       token->type = CPP_TEMPLATE_ID;
13861       /* Retrieve any deferred checks.  Do not pop this access checks yet
13862          so the memory will not be reclaimed during token replacing below.  */
13863       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13864       token->u.tree_check_value->value = template_id;
13865       token->u.tree_check_value->checks = get_deferred_access_checks ();
13866       token->keyword = RID_MAX;
13867
13868       /* Purge all subsequent tokens.  */
13869       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13870
13871       /* ??? Can we actually assume that, if template_id ==
13872          error_mark_node, we will have issued a diagnostic to the
13873          user, as opposed to simply marking the tentative parse as
13874          failed?  */
13875       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13876         error_at (token->location, "parse error in template argument list");
13877     }
13878
13879   pop_to_parent_deferring_access_checks ();
13880   return template_id;
13881 }
13882
13883 /* Parse a template-name.
13884
13885    template-name:
13886      identifier
13887
13888    The standard should actually say:
13889
13890    template-name:
13891      identifier
13892      operator-function-id
13893
13894    A defect report has been filed about this issue.
13895
13896    A conversion-function-id cannot be a template name because they cannot
13897    be part of a template-id. In fact, looking at this code:
13898
13899    a.operator K<int>()
13900
13901    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13902    It is impossible to call a templated conversion-function-id with an
13903    explicit argument list, since the only allowed template parameter is
13904    the type to which it is converting.
13905
13906    If TEMPLATE_KEYWORD_P is true, then we have just seen the
13907    `template' keyword, in a construction like:
13908
13909      T::template f<3>()
13910
13911    In that case `f' is taken to be a template-name, even though there
13912    is no way of knowing for sure.
13913
13914    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13915    name refers to a set of overloaded functions, at least one of which
13916    is a template, or an IDENTIFIER_NODE with the name of the template,
13917    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
13918    names are looked up inside uninstantiated templates.  */
13919
13920 static tree
13921 cp_parser_template_name (cp_parser* parser,
13922                          bool template_keyword_p,
13923                          bool check_dependency_p,
13924                          bool is_declaration,
13925                          enum tag_types tag_type,
13926                          bool *is_identifier)
13927 {
13928   tree identifier;
13929   tree decl;
13930   tree fns;
13931   cp_token *token = cp_lexer_peek_token (parser->lexer);
13932
13933   /* If the next token is `operator', then we have either an
13934      operator-function-id or a conversion-function-id.  */
13935   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13936     {
13937       /* We don't know whether we're looking at an
13938          operator-function-id or a conversion-function-id.  */
13939       cp_parser_parse_tentatively (parser);
13940       /* Try an operator-function-id.  */
13941       identifier = cp_parser_operator_function_id (parser);
13942       /* If that didn't work, try a conversion-function-id.  */
13943       if (!cp_parser_parse_definitely (parser))
13944         {
13945           cp_parser_error (parser, "expected template-name");
13946           return error_mark_node;
13947         }
13948     }
13949   /* Look for the identifier.  */
13950   else
13951     identifier = cp_parser_identifier (parser);
13952
13953   /* If we didn't find an identifier, we don't have a template-id.  */
13954   if (identifier == error_mark_node)
13955     return error_mark_node;
13956
13957   /* If the name immediately followed the `template' keyword, then it
13958      is a template-name.  However, if the next token is not `<', then
13959      we do not treat it as a template-name, since it is not being used
13960      as part of a template-id.  This enables us to handle constructs
13961      like:
13962
13963        template <typename T> struct S { S(); };
13964        template <typename T> S<T>::S();
13965
13966      correctly.  We would treat `S' as a template -- if it were `S<T>'
13967      -- but we do not if there is no `<'.  */
13968
13969   if (processing_template_decl
13970       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13971     {
13972       /* In a declaration, in a dependent context, we pretend that the
13973          "template" keyword was present in order to improve error
13974          recovery.  For example, given:
13975
13976            template <typename T> void f(T::X<int>);
13977
13978          we want to treat "X<int>" as a template-id.  */
13979       if (is_declaration
13980           && !template_keyword_p
13981           && parser->scope && TYPE_P (parser->scope)
13982           && check_dependency_p
13983           && dependent_scope_p (parser->scope)
13984           /* Do not do this for dtors (or ctors), since they never
13985              need the template keyword before their name.  */
13986           && !constructor_name_p (identifier, parser->scope))
13987         {
13988           cp_token_position start = 0;
13989
13990           /* Explain what went wrong.  */
13991           error_at (token->location, "non-template %qD used as template",
13992                     identifier);
13993           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13994                   parser->scope, identifier);
13995           /* If parsing tentatively, find the location of the "<" token.  */
13996           if (cp_parser_simulate_error (parser))
13997             start = cp_lexer_token_position (parser->lexer, true);
13998           /* Parse the template arguments so that we can issue error
13999              messages about them.  */
14000           cp_lexer_consume_token (parser->lexer);
14001           cp_parser_enclosed_template_argument_list (parser);
14002           /* Skip tokens until we find a good place from which to
14003              continue parsing.  */
14004           cp_parser_skip_to_closing_parenthesis (parser,
14005                                                  /*recovering=*/true,
14006                                                  /*or_comma=*/true,
14007                                                  /*consume_paren=*/false);
14008           /* If parsing tentatively, permanently remove the
14009              template argument list.  That will prevent duplicate
14010              error messages from being issued about the missing
14011              "template" keyword.  */
14012           if (start)
14013             cp_lexer_purge_tokens_after (parser->lexer, start);
14014           if (is_identifier)
14015             *is_identifier = true;
14016           return identifier;
14017         }
14018
14019       /* If the "template" keyword is present, then there is generally
14020          no point in doing name-lookup, so we just return IDENTIFIER.
14021          But, if the qualifying scope is non-dependent then we can
14022          (and must) do name-lookup normally.  */
14023       if (template_keyword_p
14024           && (!parser->scope
14025               || (TYPE_P (parser->scope)
14026                   && dependent_type_p (parser->scope))))
14027         return identifier;
14028     }
14029
14030   /* Look up the name.  */
14031   decl = cp_parser_lookup_name (parser, identifier,
14032                                 tag_type,
14033                                 /*is_template=*/true,
14034                                 /*is_namespace=*/false,
14035                                 check_dependency_p,
14036                                 /*ambiguous_decls=*/NULL,
14037                                 token->location);
14038
14039   /* If DECL is a template, then the name was a template-name.  */
14040   if (TREE_CODE (decl) == TEMPLATE_DECL)
14041     {
14042       if (TREE_DEPRECATED (decl)
14043           && deprecated_state != DEPRECATED_SUPPRESS)
14044         warn_deprecated_use (decl, NULL_TREE);
14045     }
14046   else
14047     {
14048       tree fn = NULL_TREE;
14049
14050       /* The standard does not explicitly indicate whether a name that
14051          names a set of overloaded declarations, some of which are
14052          templates, is a template-name.  However, such a name should
14053          be a template-name; otherwise, there is no way to form a
14054          template-id for the overloaded templates.  */
14055       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14056       if (TREE_CODE (fns) == OVERLOAD)
14057         for (fn = fns; fn; fn = OVL_NEXT (fn))
14058           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14059             break;
14060
14061       if (!fn)
14062         {
14063           /* The name does not name a template.  */
14064           cp_parser_error (parser, "expected template-name");
14065           return error_mark_node;
14066         }
14067     }
14068
14069   /* If DECL is dependent, and refers to a function, then just return
14070      its name; we will look it up again during template instantiation.  */
14071   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14072     {
14073       tree scope = ovl_scope (decl);
14074       if (TYPE_P (scope) && dependent_type_p (scope))
14075         return identifier;
14076     }
14077
14078   return decl;
14079 }
14080
14081 /* Parse a template-argument-list.
14082
14083    template-argument-list:
14084      template-argument ... [opt]
14085      template-argument-list , template-argument ... [opt]
14086
14087    Returns a TREE_VEC containing the arguments.  */
14088
14089 static tree
14090 cp_parser_template_argument_list (cp_parser* parser)
14091 {
14092   tree fixed_args[10];
14093   unsigned n_args = 0;
14094   unsigned alloced = 10;
14095   tree *arg_ary = fixed_args;
14096   tree vec;
14097   bool saved_in_template_argument_list_p;
14098   bool saved_ice_p;
14099   bool saved_non_ice_p;
14100
14101   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14102   parser->in_template_argument_list_p = true;
14103   /* Even if the template-id appears in an integral
14104      constant-expression, the contents of the argument list do
14105      not.  */
14106   saved_ice_p = parser->integral_constant_expression_p;
14107   parser->integral_constant_expression_p = false;
14108   saved_non_ice_p = parser->non_integral_constant_expression_p;
14109   parser->non_integral_constant_expression_p = false;
14110
14111   /* Parse the arguments.  */
14112   do
14113     {
14114       tree argument;
14115
14116       if (n_args)
14117         /* Consume the comma.  */
14118         cp_lexer_consume_token (parser->lexer);
14119
14120       /* Parse the template-argument.  */
14121       argument = cp_parser_template_argument (parser);
14122
14123       /* If the next token is an ellipsis, we're expanding a template
14124          argument pack. */
14125       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14126         {
14127           if (argument == error_mark_node)
14128             {
14129               cp_token *token = cp_lexer_peek_token (parser->lexer);
14130               error_at (token->location,
14131                         "expected parameter pack before %<...%>");
14132             }
14133           /* Consume the `...' token. */
14134           cp_lexer_consume_token (parser->lexer);
14135
14136           /* Make the argument into a TYPE_PACK_EXPANSION or
14137              EXPR_PACK_EXPANSION. */
14138           argument = make_pack_expansion (argument);
14139         }
14140
14141       if (n_args == alloced)
14142         {
14143           alloced *= 2;
14144
14145           if (arg_ary == fixed_args)
14146             {
14147               arg_ary = XNEWVEC (tree, alloced);
14148               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14149             }
14150           else
14151             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14152         }
14153       arg_ary[n_args++] = argument;
14154     }
14155   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14156
14157   vec = make_tree_vec (n_args);
14158
14159   while (n_args--)
14160     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14161
14162   if (arg_ary != fixed_args)
14163     free (arg_ary);
14164   parser->non_integral_constant_expression_p = saved_non_ice_p;
14165   parser->integral_constant_expression_p = saved_ice_p;
14166   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14167 #ifdef ENABLE_CHECKING
14168   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14169 #endif
14170   return vec;
14171 }
14172
14173 /* Parse a template-argument.
14174
14175    template-argument:
14176      assignment-expression
14177      type-id
14178      id-expression
14179
14180    The representation is that of an assignment-expression, type-id, or
14181    id-expression -- except that the qualified id-expression is
14182    evaluated, so that the value returned is either a DECL or an
14183    OVERLOAD.
14184
14185    Although the standard says "assignment-expression", it forbids
14186    throw-expressions or assignments in the template argument.
14187    Therefore, we use "conditional-expression" instead.  */
14188
14189 static tree
14190 cp_parser_template_argument (cp_parser* parser)
14191 {
14192   tree argument;
14193   bool template_p;
14194   bool address_p;
14195   bool maybe_type_id = false;
14196   cp_token *token = NULL, *argument_start_token = NULL;
14197   location_t loc = 0;
14198   cp_id_kind idk;
14199
14200   /* There's really no way to know what we're looking at, so we just
14201      try each alternative in order.
14202
14203        [temp.arg]
14204
14205        In a template-argument, an ambiguity between a type-id and an
14206        expression is resolved to a type-id, regardless of the form of
14207        the corresponding template-parameter.
14208
14209      Therefore, we try a type-id first.  */
14210   cp_parser_parse_tentatively (parser);
14211   argument = cp_parser_template_type_arg (parser);
14212   /* If there was no error parsing the type-id but the next token is a
14213      '>>', our behavior depends on which dialect of C++ we're
14214      parsing. In C++98, we probably found a typo for '> >'. But there
14215      are type-id which are also valid expressions. For instance:
14216
14217      struct X { int operator >> (int); };
14218      template <int V> struct Foo {};
14219      Foo<X () >> 5> r;
14220
14221      Here 'X()' is a valid type-id of a function type, but the user just
14222      wanted to write the expression "X() >> 5". Thus, we remember that we
14223      found a valid type-id, but we still try to parse the argument as an
14224      expression to see what happens. 
14225
14226      In C++0x, the '>>' will be considered two separate '>'
14227      tokens.  */
14228   if (!cp_parser_error_occurred (parser)
14229       && cxx_dialect == cxx98
14230       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14231     {
14232       maybe_type_id = true;
14233       cp_parser_abort_tentative_parse (parser);
14234     }
14235   else
14236     {
14237       /* If the next token isn't a `,' or a `>', then this argument wasn't
14238       really finished. This means that the argument is not a valid
14239       type-id.  */
14240       if (!cp_parser_next_token_ends_template_argument_p (parser))
14241         cp_parser_error (parser, "expected template-argument");
14242       /* If that worked, we're done.  */
14243       if (cp_parser_parse_definitely (parser))
14244         return argument;
14245     }
14246   /* We're still not sure what the argument will be.  */
14247   cp_parser_parse_tentatively (parser);
14248   /* Try a template.  */
14249   argument_start_token = cp_lexer_peek_token (parser->lexer);
14250   argument = cp_parser_id_expression (parser,
14251                                       /*template_keyword_p=*/false,
14252                                       /*check_dependency_p=*/true,
14253                                       &template_p,
14254                                       /*declarator_p=*/false,
14255                                       /*optional_p=*/false);
14256   /* If the next token isn't a `,' or a `>', then this argument wasn't
14257      really finished.  */
14258   if (!cp_parser_next_token_ends_template_argument_p (parser))
14259     cp_parser_error (parser, "expected template-argument");
14260   if (!cp_parser_error_occurred (parser))
14261     {
14262       /* Figure out what is being referred to.  If the id-expression
14263          was for a class template specialization, then we will have a
14264          TYPE_DECL at this point.  There is no need to do name lookup
14265          at this point in that case.  */
14266       if (TREE_CODE (argument) != TYPE_DECL)
14267         argument = cp_parser_lookup_name (parser, argument,
14268                                           none_type,
14269                                           /*is_template=*/template_p,
14270                                           /*is_namespace=*/false,
14271                                           /*check_dependency=*/true,
14272                                           /*ambiguous_decls=*/NULL,
14273                                           argument_start_token->location);
14274       if (TREE_CODE (argument) != TEMPLATE_DECL
14275           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14276         cp_parser_error (parser, "expected template-name");
14277     }
14278   if (cp_parser_parse_definitely (parser))
14279     {
14280       if (TREE_DEPRECATED (argument))
14281         warn_deprecated_use (argument, NULL_TREE);
14282       return argument;
14283     }
14284   /* It must be a non-type argument.  There permitted cases are given
14285      in [temp.arg.nontype]:
14286
14287      -- an integral constant-expression of integral or enumeration
14288         type; or
14289
14290      -- the name of a non-type template-parameter; or
14291
14292      -- the name of an object or function with external linkage...
14293
14294      -- the address of an object or function with external linkage...
14295
14296      -- a pointer to member...  */
14297   /* Look for a non-type template parameter.  */
14298   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14299     {
14300       cp_parser_parse_tentatively (parser);
14301       argument = cp_parser_primary_expression (parser,
14302                                                /*address_p=*/false,
14303                                                /*cast_p=*/false,
14304                                                /*template_arg_p=*/true,
14305                                                &idk);
14306       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14307           || !cp_parser_next_token_ends_template_argument_p (parser))
14308         cp_parser_simulate_error (parser);
14309       if (cp_parser_parse_definitely (parser))
14310         return argument;
14311     }
14312
14313   /* If the next token is "&", the argument must be the address of an
14314      object or function with external linkage.  */
14315   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14316   if (address_p)
14317     {
14318       loc = cp_lexer_peek_token (parser->lexer)->location;
14319       cp_lexer_consume_token (parser->lexer);
14320     }
14321   /* See if we might have an id-expression.  */
14322   token = cp_lexer_peek_token (parser->lexer);
14323   if (token->type == CPP_NAME
14324       || token->keyword == RID_OPERATOR
14325       || token->type == CPP_SCOPE
14326       || token->type == CPP_TEMPLATE_ID
14327       || token->type == CPP_NESTED_NAME_SPECIFIER)
14328     {
14329       cp_parser_parse_tentatively (parser);
14330       argument = cp_parser_primary_expression (parser,
14331                                                address_p,
14332                                                /*cast_p=*/false,
14333                                                /*template_arg_p=*/true,
14334                                                &idk);
14335       if (cp_parser_error_occurred (parser)
14336           || !cp_parser_next_token_ends_template_argument_p (parser))
14337         cp_parser_abort_tentative_parse (parser);
14338       else
14339         {
14340           tree probe;
14341
14342           if (INDIRECT_REF_P (argument))
14343             {
14344               /* Strip the dereference temporarily.  */
14345               gcc_assert (REFERENCE_REF_P (argument));
14346               argument = TREE_OPERAND (argument, 0);
14347             }
14348
14349           /* If we're in a template, we represent a qualified-id referring
14350              to a static data member as a SCOPE_REF even if the scope isn't
14351              dependent so that we can check access control later.  */
14352           probe = argument;
14353           if (TREE_CODE (probe) == SCOPE_REF)
14354             probe = TREE_OPERAND (probe, 1);
14355           if (VAR_P (probe))
14356             {
14357               /* A variable without external linkage might still be a
14358                  valid constant-expression, so no error is issued here
14359                  if the external-linkage check fails.  */
14360               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14361                 cp_parser_simulate_error (parser);
14362             }
14363           else if (is_overloaded_fn (argument))
14364             /* All overloaded functions are allowed; if the external
14365                linkage test does not pass, an error will be issued
14366                later.  */
14367             ;
14368           else if (address_p
14369                    && (TREE_CODE (argument) == OFFSET_REF
14370                        || TREE_CODE (argument) == SCOPE_REF))
14371             /* A pointer-to-member.  */
14372             ;
14373           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14374             ;
14375           else
14376             cp_parser_simulate_error (parser);
14377
14378           if (cp_parser_parse_definitely (parser))
14379             {
14380               if (address_p)
14381                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14382                                              tf_warning_or_error);
14383               else
14384                 argument = convert_from_reference (argument);
14385               return argument;
14386             }
14387         }
14388     }
14389   /* If the argument started with "&", there are no other valid
14390      alternatives at this point.  */
14391   if (address_p)
14392     {
14393       cp_parser_error (parser, "invalid non-type template argument");
14394       return error_mark_node;
14395     }
14396
14397   /* If the argument wasn't successfully parsed as a type-id followed
14398      by '>>', the argument can only be a constant expression now.
14399      Otherwise, we try parsing the constant-expression tentatively,
14400      because the argument could really be a type-id.  */
14401   if (maybe_type_id)
14402     cp_parser_parse_tentatively (parser);
14403   argument = cp_parser_constant_expression (parser);
14404
14405   if (!maybe_type_id)
14406     return argument;
14407   if (!cp_parser_next_token_ends_template_argument_p (parser))
14408     cp_parser_error (parser, "expected template-argument");
14409   if (cp_parser_parse_definitely (parser))
14410     return argument;
14411   /* We did our best to parse the argument as a non type-id, but that
14412      was the only alternative that matched (albeit with a '>' after
14413      it). We can assume it's just a typo from the user, and a
14414      diagnostic will then be issued.  */
14415   return cp_parser_template_type_arg (parser);
14416 }
14417
14418 /* Parse an explicit-instantiation.
14419
14420    explicit-instantiation:
14421      template declaration
14422
14423    Although the standard says `declaration', what it really means is:
14424
14425    explicit-instantiation:
14426      template decl-specifier-seq [opt] declarator [opt] ;
14427
14428    Things like `template int S<int>::i = 5, int S<double>::j;' are not
14429    supposed to be allowed.  A defect report has been filed about this
14430    issue.
14431
14432    GNU Extension:
14433
14434    explicit-instantiation:
14435      storage-class-specifier template
14436        decl-specifier-seq [opt] declarator [opt] ;
14437      function-specifier template
14438        decl-specifier-seq [opt] declarator [opt] ;  */
14439
14440 static void
14441 cp_parser_explicit_instantiation (cp_parser* parser)
14442 {
14443   int declares_class_or_enum;
14444   cp_decl_specifier_seq decl_specifiers;
14445   tree extension_specifier = NULL_TREE;
14446
14447   timevar_push (TV_TEMPLATE_INST);
14448
14449   /* Look for an (optional) storage-class-specifier or
14450      function-specifier.  */
14451   if (cp_parser_allow_gnu_extensions_p (parser))
14452     {
14453       extension_specifier
14454         = cp_parser_storage_class_specifier_opt (parser);
14455       if (!extension_specifier)
14456         extension_specifier
14457           = cp_parser_function_specifier_opt (parser,
14458                                               /*decl_specs=*/NULL);
14459     }
14460
14461   /* Look for the `template' keyword.  */
14462   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14463   /* Let the front end know that we are processing an explicit
14464      instantiation.  */
14465   begin_explicit_instantiation ();
14466   /* [temp.explicit] says that we are supposed to ignore access
14467      control while processing explicit instantiation directives.  */
14468   push_deferring_access_checks (dk_no_check);
14469   /* Parse a decl-specifier-seq.  */
14470   cp_parser_decl_specifier_seq (parser,
14471                                 CP_PARSER_FLAGS_OPTIONAL,
14472                                 &decl_specifiers,
14473                                 &declares_class_or_enum);
14474   /* If there was exactly one decl-specifier, and it declared a class,
14475      and there's no declarator, then we have an explicit type
14476      instantiation.  */
14477   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14478     {
14479       tree type;
14480
14481       type = check_tag_decl (&decl_specifiers,
14482                              /*explicit_type_instantiation_p=*/true);
14483       /* Turn access control back on for names used during
14484          template instantiation.  */
14485       pop_deferring_access_checks ();
14486       if (type)
14487         do_type_instantiation (type, extension_specifier,
14488                                /*complain=*/tf_error);
14489     }
14490   else
14491     {
14492       cp_declarator *declarator;
14493       tree decl;
14494
14495       /* Parse the declarator.  */
14496       declarator
14497         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14498                                 /*ctor_dtor_or_conv_p=*/NULL,
14499                                 /*parenthesized_p=*/NULL,
14500                                 /*member_p=*/false,
14501                                 /*friend_p=*/false);
14502       if (declares_class_or_enum & 2)
14503         cp_parser_check_for_definition_in_return_type (declarator,
14504                                                        decl_specifiers.type,
14505                                                        decl_specifiers.locations[ds_type_spec]);
14506       if (declarator != cp_error_declarator)
14507         {
14508           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14509             permerror (decl_specifiers.locations[ds_inline],
14510                        "explicit instantiation shall not use"
14511                        " %<inline%> specifier");
14512           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14513             permerror (decl_specifiers.locations[ds_constexpr],
14514                        "explicit instantiation shall not use"
14515                        " %<constexpr%> specifier");
14516
14517           decl = grokdeclarator (declarator, &decl_specifiers,
14518                                  NORMAL, 0, &decl_specifiers.attributes);
14519           /* Turn access control back on for names used during
14520              template instantiation.  */
14521           pop_deferring_access_checks ();
14522           /* Do the explicit instantiation.  */
14523           do_decl_instantiation (decl, extension_specifier);
14524         }
14525       else
14526         {
14527           pop_deferring_access_checks ();
14528           /* Skip the body of the explicit instantiation.  */
14529           cp_parser_skip_to_end_of_statement (parser);
14530         }
14531     }
14532   /* We're done with the instantiation.  */
14533   end_explicit_instantiation ();
14534
14535   cp_parser_consume_semicolon_at_end_of_statement (parser);
14536
14537   timevar_pop (TV_TEMPLATE_INST);
14538 }
14539
14540 /* Parse an explicit-specialization.
14541
14542    explicit-specialization:
14543      template < > declaration
14544
14545    Although the standard says `declaration', what it really means is:
14546
14547    explicit-specialization:
14548      template <> decl-specifier [opt] init-declarator [opt] ;
14549      template <> function-definition
14550      template <> explicit-specialization
14551      template <> template-declaration  */
14552
14553 static void
14554 cp_parser_explicit_specialization (cp_parser* parser)
14555 {
14556   bool need_lang_pop;
14557   cp_token *token = cp_lexer_peek_token (parser->lexer);
14558
14559   /* Look for the `template' keyword.  */
14560   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14561   /* Look for the `<'.  */
14562   cp_parser_require (parser, CPP_LESS, RT_LESS);
14563   /* Look for the `>'.  */
14564   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14565   /* We have processed another parameter list.  */
14566   ++parser->num_template_parameter_lists;
14567   /* [temp]
14568
14569      A template ... explicit specialization ... shall not have C
14570      linkage.  */
14571   if (current_lang_name == lang_name_c)
14572     {
14573       error_at (token->location, "template specialization with C linkage");
14574       /* Give it C++ linkage to avoid confusing other parts of the
14575          front end.  */
14576       push_lang_context (lang_name_cplusplus);
14577       need_lang_pop = true;
14578     }
14579   else
14580     need_lang_pop = false;
14581   /* Let the front end know that we are beginning a specialization.  */
14582   if (!begin_specialization ())
14583     {
14584       end_specialization ();
14585       return;
14586     }
14587
14588   /* If the next keyword is `template', we need to figure out whether
14589      or not we're looking a template-declaration.  */
14590   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14591     {
14592       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14593           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14594         cp_parser_template_declaration_after_export (parser,
14595                                                      /*member_p=*/false);
14596       else
14597         cp_parser_explicit_specialization (parser);
14598     }
14599   else
14600     /* Parse the dependent declaration.  */
14601     cp_parser_single_declaration (parser,
14602                                   /*checks=*/NULL,
14603                                   /*member_p=*/false,
14604                                   /*explicit_specialization_p=*/true,
14605                                   /*friend_p=*/NULL);
14606   /* We're done with the specialization.  */
14607   end_specialization ();
14608   /* For the erroneous case of a template with C linkage, we pushed an
14609      implicit C++ linkage scope; exit that scope now.  */
14610   if (need_lang_pop)
14611     pop_lang_context ();
14612   /* We're done with this parameter list.  */
14613   --parser->num_template_parameter_lists;
14614 }
14615
14616 /* Parse a type-specifier.
14617
14618    type-specifier:
14619      simple-type-specifier
14620      class-specifier
14621      enum-specifier
14622      elaborated-type-specifier
14623      cv-qualifier
14624
14625    GNU Extension:
14626
14627    type-specifier:
14628      __complex__
14629
14630    Returns a representation of the type-specifier.  For a
14631    class-specifier, enum-specifier, or elaborated-type-specifier, a
14632    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14633
14634    The parser flags FLAGS is used to control type-specifier parsing.
14635
14636    If IS_DECLARATION is TRUE, then this type-specifier is appearing
14637    in a decl-specifier-seq.
14638
14639    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14640    class-specifier, enum-specifier, or elaborated-type-specifier, then
14641    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
14642    if a type is declared; 2 if it is defined.  Otherwise, it is set to
14643    zero.
14644
14645    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14646    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
14647    is set to FALSE.  */
14648
14649 static tree
14650 cp_parser_type_specifier (cp_parser* parser,
14651                           cp_parser_flags flags,
14652                           cp_decl_specifier_seq *decl_specs,
14653                           bool is_declaration,
14654                           int* declares_class_or_enum,
14655                           bool* is_cv_qualifier)
14656 {
14657   tree type_spec = NULL_TREE;
14658   cp_token *token;
14659   enum rid keyword;
14660   cp_decl_spec ds = ds_last;
14661
14662   /* Assume this type-specifier does not declare a new type.  */
14663   if (declares_class_or_enum)
14664     *declares_class_or_enum = 0;
14665   /* And that it does not specify a cv-qualifier.  */
14666   if (is_cv_qualifier)
14667     *is_cv_qualifier = false;
14668   /* Peek at the next token.  */
14669   token = cp_lexer_peek_token (parser->lexer);
14670
14671   /* If we're looking at a keyword, we can use that to guide the
14672      production we choose.  */
14673   keyword = token->keyword;
14674   switch (keyword)
14675     {
14676     case RID_ENUM:
14677       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14678         goto elaborated_type_specifier;
14679
14680       /* Look for the enum-specifier.  */
14681       type_spec = cp_parser_enum_specifier (parser);
14682       /* If that worked, we're done.  */
14683       if (type_spec)
14684         {
14685           if (declares_class_or_enum)
14686             *declares_class_or_enum = 2;
14687           if (decl_specs)
14688             cp_parser_set_decl_spec_type (decl_specs,
14689                                           type_spec,
14690                                           token,
14691                                           /*type_definition_p=*/true);
14692           return type_spec;
14693         }
14694       else
14695         goto elaborated_type_specifier;
14696
14697       /* Any of these indicate either a class-specifier, or an
14698          elaborated-type-specifier.  */
14699     case RID_CLASS:
14700     case RID_STRUCT:
14701     case RID_UNION:
14702       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14703         goto elaborated_type_specifier;
14704
14705       /* Parse tentatively so that we can back up if we don't find a
14706          class-specifier.  */
14707       cp_parser_parse_tentatively (parser);
14708       /* Look for the class-specifier.  */
14709       type_spec = cp_parser_class_specifier (parser);
14710       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14711       /* If that worked, we're done.  */
14712       if (cp_parser_parse_definitely (parser))
14713         {
14714           if (declares_class_or_enum)
14715             *declares_class_or_enum = 2;
14716           if (decl_specs)
14717             cp_parser_set_decl_spec_type (decl_specs,
14718                                           type_spec,
14719                                           token,
14720                                           /*type_definition_p=*/true);
14721           return type_spec;
14722         }
14723
14724       /* Fall through.  */
14725     elaborated_type_specifier:
14726       /* We're declaring (not defining) a class or enum.  */
14727       if (declares_class_or_enum)
14728         *declares_class_or_enum = 1;
14729
14730       /* Fall through.  */
14731     case RID_TYPENAME:
14732       /* Look for an elaborated-type-specifier.  */
14733       type_spec
14734         = (cp_parser_elaborated_type_specifier
14735            (parser,
14736             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14737             is_declaration));
14738       if (decl_specs)
14739         cp_parser_set_decl_spec_type (decl_specs,
14740                                       type_spec,
14741                                       token,
14742                                       /*type_definition_p=*/false);
14743       return type_spec;
14744
14745     case RID_CONST:
14746       ds = ds_const;
14747       if (is_cv_qualifier)
14748         *is_cv_qualifier = true;
14749       break;
14750
14751     case RID_VOLATILE:
14752       ds = ds_volatile;
14753       if (is_cv_qualifier)
14754         *is_cv_qualifier = true;
14755       break;
14756
14757     case RID_RESTRICT:
14758       ds = ds_restrict;
14759       if (is_cv_qualifier)
14760         *is_cv_qualifier = true;
14761       break;
14762
14763     case RID_COMPLEX:
14764       /* The `__complex__' keyword is a GNU extension.  */
14765       ds = ds_complex;
14766       break;
14767
14768     default:
14769       break;
14770     }
14771
14772   /* Handle simple keywords.  */
14773   if (ds != ds_last)
14774     {
14775       if (decl_specs)
14776         {
14777           set_and_check_decl_spec_loc (decl_specs, ds, token);
14778           decl_specs->any_specifiers_p = true;
14779         }
14780       return cp_lexer_consume_token (parser->lexer)->u.value;
14781     }
14782
14783   /* If we do not already have a type-specifier, assume we are looking
14784      at a simple-type-specifier.  */
14785   type_spec = cp_parser_simple_type_specifier (parser,
14786                                                decl_specs,
14787                                                flags);
14788
14789   /* If we didn't find a type-specifier, and a type-specifier was not
14790      optional in this context, issue an error message.  */
14791   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14792     {
14793       cp_parser_error (parser, "expected type specifier");
14794       return error_mark_node;
14795     }
14796
14797   return type_spec;
14798 }
14799
14800 /* Parse a simple-type-specifier.
14801
14802    simple-type-specifier:
14803      :: [opt] nested-name-specifier [opt] type-name
14804      :: [opt] nested-name-specifier template template-id
14805      char
14806      wchar_t
14807      bool
14808      short
14809      int
14810      long
14811      signed
14812      unsigned
14813      float
14814      double
14815      void
14816
14817    C++0x Extension:
14818
14819    simple-type-specifier:
14820      auto
14821      decltype ( expression )   
14822      char16_t
14823      char32_t
14824      __underlying_type ( type-id )
14825
14826    GNU Extension:
14827
14828    simple-type-specifier:
14829      __int128
14830      __typeof__ unary-expression
14831      __typeof__ ( type-id )
14832      __typeof__ ( type-id ) { initializer-list , [opt] }
14833
14834    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
14835    appropriately updated.  */
14836
14837 static tree
14838 cp_parser_simple_type_specifier (cp_parser* parser,
14839                                  cp_decl_specifier_seq *decl_specs,
14840                                  cp_parser_flags flags)
14841 {
14842   tree type = NULL_TREE;
14843   cp_token *token;
14844   int idx;
14845
14846   /* Peek at the next token.  */
14847   token = cp_lexer_peek_token (parser->lexer);
14848
14849   /* If we're looking at a keyword, things are easy.  */
14850   switch (token->keyword)
14851     {
14852     case RID_CHAR:
14853       if (decl_specs)
14854         decl_specs->explicit_char_p = true;
14855       type = char_type_node;
14856       break;
14857     case RID_CHAR16:
14858       type = char16_type_node;
14859       break;
14860     case RID_CHAR32:
14861       type = char32_type_node;
14862       break;
14863     case RID_WCHAR:
14864       type = wchar_type_node;
14865       break;
14866     case RID_BOOL:
14867       type = boolean_type_node;
14868       break;
14869     case RID_SHORT:
14870       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14871       type = short_integer_type_node;
14872       break;
14873     case RID_INT:
14874       if (decl_specs)
14875         decl_specs->explicit_int_p = true;
14876       type = integer_type_node;
14877       break;
14878     case RID_INT_N_0:
14879     case RID_INT_N_1:
14880     case RID_INT_N_2:
14881     case RID_INT_N_3:
14882       idx = token->keyword - RID_INT_N_0;
14883       if (! int_n_enabled_p [idx])
14884         break;
14885       if (decl_specs)
14886         {
14887           decl_specs->explicit_intN_p = true;
14888           decl_specs->int_n_idx = idx;
14889         }
14890       type = int_n_trees [idx].signed_type;
14891       break;
14892     case RID_LONG:
14893       if (decl_specs)
14894         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14895       type = long_integer_type_node;
14896       break;
14897     case RID_SIGNED:
14898       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14899       type = integer_type_node;
14900       break;
14901     case RID_UNSIGNED:
14902       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14903       type = unsigned_type_node;
14904       break;
14905     case RID_FLOAT:
14906       type = float_type_node;
14907       break;
14908     case RID_DOUBLE:
14909       type = double_type_node;
14910       break;
14911     case RID_VOID:
14912       type = void_type_node;
14913       break;
14914
14915     case RID_AUTO:
14916       maybe_warn_cpp0x (CPP0X_AUTO);
14917       if (parser->auto_is_implicit_function_template_parm_p)
14918         {
14919           if (cxx_dialect >= cxx14)
14920             type = synthesize_implicit_template_parm (parser);
14921           else
14922             type = error_mark_node;
14923
14924           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14925             {
14926               if (cxx_dialect < cxx14)
14927                 error_at (token->location,
14928                          "use of %<auto%> in lambda parameter declaration "
14929                          "only available with "
14930                          "-std=c++14 or -std=gnu++14");
14931             }
14932           else if (cxx_dialect < cxx14)
14933             error_at (token->location,
14934                      "use of %<auto%> in parameter declaration "
14935                      "only available with "
14936                      "-std=c++14 or -std=gnu++14");
14937           else
14938             pedwarn (token->location, OPT_Wpedantic,
14939                      "ISO C++ forbids use of %<auto%> in parameter "
14940                      "declaration");
14941         }
14942       else
14943         type = make_auto ();
14944       break;
14945
14946     case RID_DECLTYPE:
14947       /* Since DR 743, decltype can either be a simple-type-specifier by
14948          itself or begin a nested-name-specifier.  Parsing it will replace
14949          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14950          handling below decide what to do.  */
14951       cp_parser_decltype (parser);
14952       cp_lexer_set_token_position (parser->lexer, token);
14953       break;
14954
14955     case RID_TYPEOF:
14956       /* Consume the `typeof' token.  */
14957       cp_lexer_consume_token (parser->lexer);
14958       /* Parse the operand to `typeof'.  */
14959       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14960       /* If it is not already a TYPE, take its type.  */
14961       if (!TYPE_P (type))
14962         type = finish_typeof (type);
14963
14964       if (decl_specs)
14965         cp_parser_set_decl_spec_type (decl_specs, type,
14966                                       token,
14967                                       /*type_definition_p=*/false);
14968
14969       return type;
14970
14971     case RID_UNDERLYING_TYPE:
14972       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14973       if (decl_specs)
14974         cp_parser_set_decl_spec_type (decl_specs, type,
14975                                       token,
14976                                       /*type_definition_p=*/false);
14977
14978       return type;
14979
14980     case RID_BASES:
14981     case RID_DIRECT_BASES:
14982       type = cp_parser_trait_expr (parser, token->keyword);
14983       if (decl_specs)
14984        cp_parser_set_decl_spec_type (decl_specs, type,
14985                                      token,
14986                                      /*type_definition_p=*/false);
14987       return type;
14988     default:
14989       break;
14990     }
14991
14992   /* If token is an already-parsed decltype not followed by ::,
14993      it's a simple-type-specifier.  */
14994   if (token->type == CPP_DECLTYPE
14995       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14996     {
14997       type = token->u.value;
14998       if (decl_specs)
14999         {
15000           cp_parser_set_decl_spec_type (decl_specs, type,
15001                                         token,
15002                                         /*type_definition_p=*/false);
15003           /* Remember that we are handling a decltype in order to
15004              implement the resolution of DR 1510 when the argument
15005              isn't instantiation dependent.  */
15006           decl_specs->decltype_p = true;
15007         }
15008       cp_lexer_consume_token (parser->lexer);
15009       return type;
15010     }
15011
15012   /* If the type-specifier was for a built-in type, we're done.  */
15013   if (type)
15014     {
15015       /* Record the type.  */
15016       if (decl_specs
15017           && (token->keyword != RID_SIGNED
15018               && token->keyword != RID_UNSIGNED
15019               && token->keyword != RID_SHORT
15020               && token->keyword != RID_LONG))
15021         cp_parser_set_decl_spec_type (decl_specs,
15022                                       type,
15023                                       token,
15024                                       /*type_definition_p=*/false);
15025       if (decl_specs)
15026         decl_specs->any_specifiers_p = true;
15027
15028       /* Consume the token.  */
15029       cp_lexer_consume_token (parser->lexer);
15030
15031       if (type == error_mark_node)
15032         return error_mark_node;
15033
15034       /* There is no valid C++ program where a non-template type is
15035          followed by a "<".  That usually indicates that the user thought
15036          that the type was a template.  */
15037       cp_parser_check_for_invalid_template_id (parser, type, none_type,
15038                                                token->location);
15039
15040       return TYPE_NAME (type);
15041     }
15042
15043   /* The type-specifier must be a user-defined type.  */
15044   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15045     {
15046       bool qualified_p;
15047       bool global_p;
15048
15049       /* Don't gobble tokens or issue error messages if this is an
15050          optional type-specifier.  */
15051       if (flags & CP_PARSER_FLAGS_OPTIONAL)
15052         cp_parser_parse_tentatively (parser);
15053
15054       /* Look for the optional `::' operator.  */
15055       global_p
15056         = (cp_parser_global_scope_opt (parser,
15057                                        /*current_scope_valid_p=*/false)
15058            != NULL_TREE);
15059       /* Look for the nested-name specifier.  */
15060       qualified_p
15061         = (cp_parser_nested_name_specifier_opt (parser,
15062                                                 /*typename_keyword_p=*/false,
15063                                                 /*check_dependency_p=*/true,
15064                                                 /*type_p=*/false,
15065                                                 /*is_declaration=*/false)
15066            != NULL_TREE);
15067       token = cp_lexer_peek_token (parser->lexer);
15068       /* If we have seen a nested-name-specifier, and the next token
15069          is `template', then we are using the template-id production.  */
15070       if (parser->scope
15071           && cp_parser_optional_template_keyword (parser))
15072         {
15073           /* Look for the template-id.  */
15074           type = cp_parser_template_id (parser,
15075                                         /*template_keyword_p=*/true,
15076                                         /*check_dependency_p=*/true,
15077                                         none_type,
15078                                         /*is_declaration=*/false);
15079           /* If the template-id did not name a type, we are out of
15080              luck.  */
15081           if (TREE_CODE (type) != TYPE_DECL)
15082             {
15083               cp_parser_error (parser, "expected template-id for type");
15084               type = NULL_TREE;
15085             }
15086         }
15087       /* Otherwise, look for a type-name.  */
15088       else
15089         type = cp_parser_type_name (parser);
15090       /* Keep track of all name-lookups performed in class scopes.  */
15091       if (type
15092           && !global_p
15093           && !qualified_p
15094           && TREE_CODE (type) == TYPE_DECL
15095           && identifier_p (DECL_NAME (type)))
15096         maybe_note_name_used_in_class (DECL_NAME (type), type);
15097       /* If it didn't work out, we don't have a TYPE.  */
15098       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15099           && !cp_parser_parse_definitely (parser))
15100         type = NULL_TREE;
15101       if (type && decl_specs)
15102         cp_parser_set_decl_spec_type (decl_specs, type,
15103                                       token,
15104                                       /*type_definition_p=*/false);
15105     }
15106
15107   /* If we didn't get a type-name, issue an error message.  */
15108   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15109     {
15110       cp_parser_error (parser, "expected type-name");
15111       return error_mark_node;
15112     }
15113
15114   if (type && type != error_mark_node)
15115     {
15116       /* See if TYPE is an Objective-C type, and if so, parse and
15117          accept any protocol references following it.  Do this before
15118          the cp_parser_check_for_invalid_template_id() call, because
15119          Objective-C types can be followed by '<...>' which would
15120          enclose protocol names rather than template arguments, and so
15121          everything is fine.  */
15122       if (c_dialect_objc () && !parser->scope
15123           && (objc_is_id (type) || objc_is_class_name (type)))
15124         {
15125           tree protos = cp_parser_objc_protocol_refs_opt (parser);
15126           tree qual_type = objc_get_protocol_qualified_type (type, protos);
15127
15128           /* Clobber the "unqualified" type previously entered into
15129              DECL_SPECS with the new, improved protocol-qualified version.  */
15130           if (decl_specs)
15131             decl_specs->type = qual_type;
15132
15133           return qual_type;
15134         }
15135
15136       /* There is no valid C++ program where a non-template type is
15137          followed by a "<".  That usually indicates that the user
15138          thought that the type was a template.  */
15139       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15140                                                none_type,
15141                                                token->location);
15142     }
15143
15144   return type;
15145 }
15146
15147 /* Parse a type-name.
15148
15149    type-name:
15150      class-name
15151      enum-name
15152      typedef-name
15153      simple-template-id [in c++0x]
15154
15155    enum-name:
15156      identifier
15157
15158    typedef-name:
15159      identifier
15160
15161    Returns a TYPE_DECL for the type.  */
15162
15163 static tree
15164 cp_parser_type_name (cp_parser* parser)
15165 {
15166   tree type_decl;
15167
15168   /* We can't know yet whether it is a class-name or not.  */
15169   cp_parser_parse_tentatively (parser);
15170   /* Try a class-name.  */
15171   type_decl = cp_parser_class_name (parser,
15172                                     /*typename_keyword_p=*/false,
15173                                     /*template_keyword_p=*/false,
15174                                     none_type,
15175                                     /*check_dependency_p=*/true,
15176                                     /*class_head_p=*/false,
15177                                     /*is_declaration=*/false);
15178   /* If it's not a class-name, keep looking.  */
15179   if (!cp_parser_parse_definitely (parser))
15180     {
15181       if (cxx_dialect < cxx11)
15182         /* It must be a typedef-name or an enum-name.  */
15183         return cp_parser_nonclass_name (parser);
15184
15185       cp_parser_parse_tentatively (parser);
15186       /* It is either a simple-template-id representing an
15187          instantiation of an alias template...  */
15188       type_decl = cp_parser_template_id (parser,
15189                                          /*template_keyword_p=*/false,
15190                                          /*check_dependency_p=*/true,
15191                                          none_type,
15192                                          /*is_declaration=*/false);
15193       /* Note that this must be an instantiation of an alias template
15194          because [temp.names]/6 says:
15195          
15196              A template-id that names an alias template specialization
15197              is a type-name.
15198
15199          Whereas [temp.names]/7 says:
15200          
15201              A simple-template-id that names a class template
15202              specialization is a class-name.  */
15203       if (type_decl != NULL_TREE
15204           && TREE_CODE (type_decl) == TYPE_DECL
15205           && TYPE_DECL_ALIAS_P (type_decl))
15206         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15207       else
15208         cp_parser_simulate_error (parser);
15209
15210       if (!cp_parser_parse_definitely (parser))
15211         /* ... Or a typedef-name or an enum-name.  */
15212         return cp_parser_nonclass_name (parser);
15213     }
15214
15215   return type_decl;
15216 }
15217
15218 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15219
15220    enum-name:
15221      identifier
15222
15223    typedef-name:
15224      identifier
15225
15226    Returns a TYPE_DECL for the type.  */
15227
15228 static tree
15229 cp_parser_nonclass_name (cp_parser* parser)
15230 {
15231   tree type_decl;
15232   tree identifier;
15233
15234   cp_token *token = cp_lexer_peek_token (parser->lexer);
15235   identifier = cp_parser_identifier (parser);
15236   if (identifier == error_mark_node)
15237     return error_mark_node;
15238
15239   /* Look up the type-name.  */
15240   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15241
15242   type_decl = strip_using_decl (type_decl);
15243   
15244   if (TREE_CODE (type_decl) != TYPE_DECL
15245       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15246     {
15247       /* See if this is an Objective-C type.  */
15248       tree protos = cp_parser_objc_protocol_refs_opt (parser);
15249       tree type = objc_get_protocol_qualified_type (identifier, protos);
15250       if (type)
15251         type_decl = TYPE_NAME (type);
15252     }
15253
15254   /* Issue an error if we did not find a type-name.  */
15255   if (TREE_CODE (type_decl) != TYPE_DECL
15256       /* In Objective-C, we have the complication that class names are
15257          normally type names and start declarations (eg, the
15258          "NSObject" in "NSObject *object;"), but can be used in an
15259          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15260          is an expression.  So, a classname followed by a dot is not a
15261          valid type-name.  */
15262       || (objc_is_class_name (TREE_TYPE (type_decl))
15263           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15264     {
15265       if (!cp_parser_simulate_error (parser))
15266         cp_parser_name_lookup_error (parser, identifier, type_decl,
15267                                      NLE_TYPE, token->location);
15268       return error_mark_node;
15269     }
15270   /* Remember that the name was used in the definition of the
15271      current class so that we can check later to see if the
15272      meaning would have been different after the class was
15273      entirely defined.  */
15274   else if (type_decl != error_mark_node
15275            && !parser->scope)
15276     maybe_note_name_used_in_class (identifier, type_decl);
15277   
15278   return type_decl;
15279 }
15280
15281 /* Parse an elaborated-type-specifier.  Note that the grammar given
15282    here incorporates the resolution to DR68.
15283
15284    elaborated-type-specifier:
15285      class-key :: [opt] nested-name-specifier [opt] identifier
15286      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15287      enum-key :: [opt] nested-name-specifier [opt] identifier
15288      typename :: [opt] nested-name-specifier identifier
15289      typename :: [opt] nested-name-specifier template [opt]
15290        template-id
15291
15292    GNU extension:
15293
15294    elaborated-type-specifier:
15295      class-key attributes :: [opt] nested-name-specifier [opt] identifier
15296      class-key attributes :: [opt] nested-name-specifier [opt]
15297                template [opt] template-id
15298      enum attributes :: [opt] nested-name-specifier [opt] identifier
15299
15300    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15301    declared `friend'.  If IS_DECLARATION is TRUE, then this
15302    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15303    something is being declared.
15304
15305    Returns the TYPE specified.  */
15306
15307 static tree
15308 cp_parser_elaborated_type_specifier (cp_parser* parser,
15309                                      bool is_friend,
15310                                      bool is_declaration)
15311 {
15312   enum tag_types tag_type;
15313   tree identifier;
15314   tree type = NULL_TREE;
15315   tree attributes = NULL_TREE;
15316   tree globalscope;
15317   cp_token *token = NULL;
15318
15319   /* See if we're looking at the `enum' keyword.  */
15320   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15321     {
15322       /* Consume the `enum' token.  */
15323       cp_lexer_consume_token (parser->lexer);
15324       /* Remember that it's an enumeration type.  */
15325       tag_type = enum_type;
15326       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15327          enums) is used here.  */
15328       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15329           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15330         {
15331             pedwarn (input_location, 0, "elaborated-type-specifier "
15332                       "for a scoped enum must not use the %<%D%> keyword",
15333                       cp_lexer_peek_token (parser->lexer)->u.value);
15334           /* Consume the `struct' or `class' and parse it anyway.  */
15335           cp_lexer_consume_token (parser->lexer);
15336         }
15337       /* Parse the attributes.  */
15338       attributes = cp_parser_attributes_opt (parser);
15339     }
15340   /* Or, it might be `typename'.  */
15341   else if (cp_lexer_next_token_is_keyword (parser->lexer,
15342                                            RID_TYPENAME))
15343     {
15344       /* Consume the `typename' token.  */
15345       cp_lexer_consume_token (parser->lexer);
15346       /* Remember that it's a `typename' type.  */
15347       tag_type = typename_type;
15348     }
15349   /* Otherwise it must be a class-key.  */
15350   else
15351     {
15352       tag_type = cp_parser_class_key (parser);
15353       if (tag_type == none_type)
15354         return error_mark_node;
15355       /* Parse the attributes.  */
15356       attributes = cp_parser_attributes_opt (parser);
15357     }
15358
15359   /* Look for the `::' operator.  */
15360   globalscope =  cp_parser_global_scope_opt (parser,
15361                                              /*current_scope_valid_p=*/false);
15362   /* Look for the nested-name-specifier.  */
15363   if (tag_type == typename_type && !globalscope)
15364     {
15365       if (!cp_parser_nested_name_specifier (parser,
15366                                            /*typename_keyword_p=*/true,
15367                                            /*check_dependency_p=*/true,
15368                                            /*type_p=*/true,
15369                                             is_declaration))
15370         return error_mark_node;
15371     }
15372   else
15373     /* Even though `typename' is not present, the proposed resolution
15374        to Core Issue 180 says that in `class A<T>::B', `B' should be
15375        considered a type-name, even if `A<T>' is dependent.  */
15376     cp_parser_nested_name_specifier_opt (parser,
15377                                          /*typename_keyword_p=*/true,
15378                                          /*check_dependency_p=*/true,
15379                                          /*type_p=*/true,
15380                                          is_declaration);
15381  /* For everything but enumeration types, consider a template-id.
15382     For an enumeration type, consider only a plain identifier.  */
15383   if (tag_type != enum_type)
15384     {
15385       bool template_p = false;
15386       tree decl;
15387
15388       /* Allow the `template' keyword.  */
15389       template_p = cp_parser_optional_template_keyword (parser);
15390       /* If we didn't see `template', we don't know if there's a
15391          template-id or not.  */
15392       if (!template_p)
15393         cp_parser_parse_tentatively (parser);
15394       /* Parse the template-id.  */
15395       token = cp_lexer_peek_token (parser->lexer);
15396       decl = cp_parser_template_id (parser, template_p,
15397                                     /*check_dependency_p=*/true,
15398                                     tag_type,
15399                                     is_declaration);
15400       /* If we didn't find a template-id, look for an ordinary
15401          identifier.  */
15402       if (!template_p && !cp_parser_parse_definitely (parser))
15403         ;
15404       /* We can get here when cp_parser_template_id, called by
15405          cp_parser_class_name with tag_type == none_type, succeeds
15406          and caches a BASELINK.  Then, when called again here,
15407          instead of failing and returning an error_mark_node
15408          returns it (see template/typename17.C in C++11).
15409          ??? Could we diagnose this earlier?  */
15410       else if (tag_type == typename_type && BASELINK_P (decl))
15411         {
15412           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15413           type = error_mark_node;
15414         }
15415       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15416          in effect, then we must assume that, upon instantiation, the
15417          template will correspond to a class.  */
15418       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15419                && tag_type == typename_type)
15420         type = make_typename_type (parser->scope, decl,
15421                                    typename_type,
15422                                    /*complain=*/tf_error);
15423       /* If the `typename' keyword is in effect and DECL is not a type
15424          decl, then type is non existent.   */
15425       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15426         ; 
15427       else if (TREE_CODE (decl) == TYPE_DECL)
15428         type = check_elaborated_type_specifier (tag_type, decl,
15429                                                 /*allow_template_p=*/true);
15430       else if (decl == error_mark_node)
15431         type = error_mark_node; 
15432     }
15433
15434   if (!type)
15435     {
15436       token = cp_lexer_peek_token (parser->lexer);
15437       identifier = cp_parser_identifier (parser);
15438
15439       if (identifier == error_mark_node)
15440         {
15441           parser->scope = NULL_TREE;
15442           return error_mark_node;
15443         }
15444
15445       /* For a `typename', we needn't call xref_tag.  */
15446       if (tag_type == typename_type
15447           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15448         return cp_parser_make_typename_type (parser, identifier,
15449                                              token->location);
15450
15451       /* Template parameter lists apply only if we are not within a
15452          function parameter list.  */
15453       bool template_parm_lists_apply
15454           = parser->num_template_parameter_lists;
15455       if (template_parm_lists_apply)
15456         for (cp_binding_level *s = current_binding_level;
15457              s && s->kind != sk_template_parms;
15458              s = s->level_chain)
15459           if (s->kind == sk_function_parms)
15460             template_parm_lists_apply = false;
15461
15462       /* Look up a qualified name in the usual way.  */
15463       if (parser->scope)
15464         {
15465           tree decl;
15466           tree ambiguous_decls;
15467
15468           decl = cp_parser_lookup_name (parser, identifier,
15469                                         tag_type,
15470                                         /*is_template=*/false,
15471                                         /*is_namespace=*/false,
15472                                         /*check_dependency=*/true,
15473                                         &ambiguous_decls,
15474                                         token->location);
15475
15476           /* If the lookup was ambiguous, an error will already have been
15477              issued.  */
15478           if (ambiguous_decls)
15479             return error_mark_node;
15480
15481           /* If we are parsing friend declaration, DECL may be a
15482              TEMPLATE_DECL tree node here.  However, we need to check
15483              whether this TEMPLATE_DECL results in valid code.  Consider
15484              the following example:
15485
15486                namespace N {
15487                  template <class T> class C {};
15488                }
15489                class X {
15490                  template <class T> friend class N::C; // #1, valid code
15491                };
15492                template <class T> class Y {
15493                  friend class N::C;                    // #2, invalid code
15494                };
15495
15496              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15497              name lookup of `N::C'.  We see that friend declaration must
15498              be template for the code to be valid.  Note that
15499              processing_template_decl does not work here since it is
15500              always 1 for the above two cases.  */
15501
15502           decl = (cp_parser_maybe_treat_template_as_class
15503                   (decl, /*tag_name_p=*/is_friend
15504                          && template_parm_lists_apply));
15505
15506           if (TREE_CODE (decl) != TYPE_DECL)
15507             {
15508               cp_parser_diagnose_invalid_type_name (parser,
15509                                                     identifier,
15510                                                     token->location);
15511               return error_mark_node;
15512             }
15513
15514           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15515             {
15516               bool allow_template = (template_parm_lists_apply
15517                                      || DECL_SELF_REFERENCE_P (decl));
15518               type = check_elaborated_type_specifier (tag_type, decl,
15519                                                       allow_template);
15520
15521               if (type == error_mark_node)
15522                 return error_mark_node;
15523             }
15524
15525           /* Forward declarations of nested types, such as
15526
15527                class C1::C2;
15528                class C1::C2::C3;
15529
15530              are invalid unless all components preceding the final '::'
15531              are complete.  If all enclosing types are complete, these
15532              declarations become merely pointless.
15533
15534              Invalid forward declarations of nested types are errors
15535              caught elsewhere in parsing.  Those that are pointless arrive
15536              here.  */
15537
15538           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15539               && !is_friend && !processing_explicit_instantiation)
15540             warning (0, "declaration %qD does not declare anything", decl);
15541
15542           type = TREE_TYPE (decl);
15543         }
15544       else
15545         {
15546           /* An elaborated-type-specifier sometimes introduces a new type and
15547              sometimes names an existing type.  Normally, the rule is that it
15548              introduces a new type only if there is not an existing type of
15549              the same name already in scope.  For example, given:
15550
15551                struct S {};
15552                void f() { struct S s; }
15553
15554              the `struct S' in the body of `f' is the same `struct S' as in
15555              the global scope; the existing definition is used.  However, if
15556              there were no global declaration, this would introduce a new
15557              local class named `S'.
15558
15559              An exception to this rule applies to the following code:
15560
15561                namespace N { struct S; }
15562
15563              Here, the elaborated-type-specifier names a new type
15564              unconditionally; even if there is already an `S' in the
15565              containing scope this declaration names a new type.
15566              This exception only applies if the elaborated-type-specifier
15567              forms the complete declaration:
15568
15569                [class.name]
15570
15571                A declaration consisting solely of `class-key identifier ;' is
15572                either a redeclaration of the name in the current scope or a
15573                forward declaration of the identifier as a class name.  It
15574                introduces the name into the current scope.
15575
15576              We are in this situation precisely when the next token is a `;'.
15577
15578              An exception to the exception is that a `friend' declaration does
15579              *not* name a new type; i.e., given:
15580
15581                struct S { friend struct T; };
15582
15583              `T' is not a new type in the scope of `S'.
15584
15585              Also, `new struct S' or `sizeof (struct S)' never results in the
15586              definition of a new type; a new type can only be declared in a
15587              declaration context.  */
15588
15589           tag_scope ts;
15590           bool template_p;
15591
15592           if (is_friend)
15593             /* Friends have special name lookup rules.  */
15594             ts = ts_within_enclosing_non_class;
15595           else if (is_declaration
15596                    && cp_lexer_next_token_is (parser->lexer,
15597                                               CPP_SEMICOLON))
15598             /* This is a `class-key identifier ;' */
15599             ts = ts_current;
15600           else
15601             ts = ts_global;
15602
15603           template_p =
15604             (template_parm_lists_apply
15605              && (cp_parser_next_token_starts_class_definition_p (parser)
15606                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15607           /* An unqualified name was used to reference this type, so
15608              there were no qualifying templates.  */
15609           if (template_parm_lists_apply
15610               && !cp_parser_check_template_parameters (parser,
15611                                                        /*num_templates=*/0,
15612                                                        token->location,
15613                                                        /*declarator=*/NULL))
15614             return error_mark_node;
15615           type = xref_tag (tag_type, identifier, ts, template_p);
15616         }
15617     }
15618
15619   if (type == error_mark_node)
15620     return error_mark_node;
15621
15622   /* Allow attributes on forward declarations of classes.  */
15623   if (attributes)
15624     {
15625       if (TREE_CODE (type) == TYPENAME_TYPE)
15626         warning (OPT_Wattributes,
15627                  "attributes ignored on uninstantiated type");
15628       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15629                && ! processing_explicit_instantiation)
15630         warning (OPT_Wattributes,
15631                  "attributes ignored on template instantiation");
15632       else if (is_declaration && cp_parser_declares_only_class_p (parser))
15633         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15634       else
15635         warning (OPT_Wattributes,
15636                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15637     }
15638
15639   if (tag_type != enum_type)
15640     {
15641       /* Indicate whether this class was declared as a `class' or as a
15642          `struct'.  */
15643       if (TREE_CODE (type) == RECORD_TYPE)
15644         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15645       cp_parser_check_class_key (tag_type, type);
15646     }
15647
15648   /* A "<" cannot follow an elaborated type specifier.  If that
15649      happens, the user was probably trying to form a template-id.  */
15650   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15651                                            token->location);
15652
15653   return type;
15654 }
15655
15656 /* Parse an enum-specifier.
15657
15658    enum-specifier:
15659      enum-head { enumerator-list [opt] }
15660      enum-head { enumerator-list , } [C++0x]
15661
15662    enum-head:
15663      enum-key identifier [opt] enum-base [opt]
15664      enum-key nested-name-specifier identifier enum-base [opt]
15665
15666    enum-key:
15667      enum
15668      enum class   [C++0x]
15669      enum struct  [C++0x]
15670
15671    enum-base:   [C++0x]
15672      : type-specifier-seq
15673
15674    opaque-enum-specifier:
15675      enum-key identifier enum-base [opt] ;
15676
15677    GNU Extensions:
15678      enum-key attributes[opt] identifier [opt] enum-base [opt] 
15679        { enumerator-list [opt] }attributes[opt]
15680      enum-key attributes[opt] identifier [opt] enum-base [opt]
15681        { enumerator-list, }attributes[opt] [C++0x]
15682
15683    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15684    if the token stream isn't an enum-specifier after all.  */
15685
15686 static tree
15687 cp_parser_enum_specifier (cp_parser* parser)
15688 {
15689   tree identifier;
15690   tree type = NULL_TREE;
15691   tree prev_scope;
15692   tree nested_name_specifier = NULL_TREE;
15693   tree attributes;
15694   bool scoped_enum_p = false;
15695   bool has_underlying_type = false;
15696   bool nested_being_defined = false;
15697   bool new_value_list = false;
15698   bool is_new_type = false;
15699   bool is_anonymous = false;
15700   tree underlying_type = NULL_TREE;
15701   cp_token *type_start_token = NULL;
15702   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15703
15704   parser->colon_corrects_to_scope_p = false;
15705
15706   /* Parse tentatively so that we can back up if we don't find a
15707      enum-specifier.  */
15708   cp_parser_parse_tentatively (parser);
15709
15710   /* Caller guarantees that the current token is 'enum', an identifier
15711      possibly follows, and the token after that is an opening brace.
15712      If we don't have an identifier, fabricate an anonymous name for
15713      the enumeration being defined.  */
15714   cp_lexer_consume_token (parser->lexer);
15715
15716   /* Parse the "class" or "struct", which indicates a scoped
15717      enumeration type in C++0x.  */
15718   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15719       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15720     {
15721       if (cxx_dialect < cxx11)
15722         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15723
15724       /* Consume the `struct' or `class' token.  */
15725       cp_lexer_consume_token (parser->lexer);
15726
15727       scoped_enum_p = true;
15728     }
15729
15730   attributes = cp_parser_attributes_opt (parser);
15731
15732   /* Clear the qualification.  */
15733   parser->scope = NULL_TREE;
15734   parser->qualifying_scope = NULL_TREE;
15735   parser->object_scope = NULL_TREE;
15736
15737   /* Figure out in what scope the declaration is being placed.  */
15738   prev_scope = current_scope ();
15739
15740   type_start_token = cp_lexer_peek_token (parser->lexer);
15741
15742   push_deferring_access_checks (dk_no_check);
15743   nested_name_specifier
15744       = cp_parser_nested_name_specifier_opt (parser,
15745                                              /*typename_keyword_p=*/true,
15746                                              /*check_dependency_p=*/false,
15747                                              /*type_p=*/false,
15748                                              /*is_declaration=*/false);
15749
15750   if (nested_name_specifier)
15751     {
15752       tree name;
15753
15754       identifier = cp_parser_identifier (parser);
15755       name =  cp_parser_lookup_name (parser, identifier,
15756                                      enum_type,
15757                                      /*is_template=*/false,
15758                                      /*is_namespace=*/false,
15759                                      /*check_dependency=*/true,
15760                                      /*ambiguous_decls=*/NULL,
15761                                      input_location);
15762       if (name && name != error_mark_node)
15763         {
15764           type = TREE_TYPE (name);
15765           if (TREE_CODE (type) == TYPENAME_TYPE)
15766             {
15767               /* Are template enums allowed in ISO? */
15768               if (template_parm_scope_p ())
15769                 pedwarn (type_start_token->location, OPT_Wpedantic,
15770                          "%qD is an enumeration template", name);
15771               /* ignore a typename reference, for it will be solved by name
15772                  in start_enum.  */
15773               type = NULL_TREE;
15774             }
15775         }
15776       else if (nested_name_specifier == error_mark_node)
15777         /* We already issued an error.  */;
15778       else
15779         error_at (type_start_token->location,
15780                   "%qD is not an enumerator-name", identifier);
15781     }
15782   else
15783     {
15784       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15785         identifier = cp_parser_identifier (parser);
15786       else
15787         {
15788           identifier = make_anon_name ();
15789           is_anonymous = true;
15790           if (scoped_enum_p)
15791             error_at (type_start_token->location,
15792                       "anonymous scoped enum is not allowed");
15793         }
15794     }
15795   pop_deferring_access_checks ();
15796
15797   /* Check for the `:' that denotes a specified underlying type in C++0x.
15798      Note that a ':' could also indicate a bitfield width, however.  */
15799   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15800     {
15801       cp_decl_specifier_seq type_specifiers;
15802
15803       /* Consume the `:'.  */
15804       cp_lexer_consume_token (parser->lexer);
15805
15806       /* Parse the type-specifier-seq.  */
15807       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15808                                     /*is_trailing_return=*/false,
15809                                     &type_specifiers);
15810
15811       /* At this point this is surely not elaborated type specifier.  */
15812       if (!cp_parser_parse_definitely (parser))
15813         return NULL_TREE;
15814
15815       if (cxx_dialect < cxx11)
15816         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15817
15818       has_underlying_type = true;
15819
15820       /* If that didn't work, stop.  */
15821       if (type_specifiers.type != error_mark_node)
15822         {
15823           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15824                                             /*initialized=*/0, NULL);
15825           if (underlying_type == error_mark_node
15826               || check_for_bare_parameter_packs (underlying_type))
15827             underlying_type = NULL_TREE;
15828         }
15829     }
15830
15831   /* Look for the `{' but don't consume it yet.  */
15832   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15833     {
15834       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15835         {
15836           cp_parser_error (parser, "expected %<{%>");
15837           if (has_underlying_type)
15838             {
15839               type = NULL_TREE;
15840               goto out;
15841             }
15842         }
15843       /* An opaque-enum-specifier must have a ';' here.  */
15844       if ((scoped_enum_p || underlying_type)
15845           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15846         {
15847           cp_parser_error (parser, "expected %<;%> or %<{%>");
15848           if (has_underlying_type)
15849             {
15850               type = NULL_TREE;
15851               goto out;
15852             }
15853         }
15854     }
15855
15856   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15857     return NULL_TREE;
15858
15859   if (nested_name_specifier)
15860     {
15861       if (CLASS_TYPE_P (nested_name_specifier))
15862         {
15863           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15864           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15865           push_scope (nested_name_specifier);
15866         }
15867       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15868         {
15869           push_nested_namespace (nested_name_specifier);
15870         }
15871     }
15872
15873   /* Issue an error message if type-definitions are forbidden here.  */
15874   if (!cp_parser_check_type_definition (parser))
15875     type = error_mark_node;
15876   else
15877     /* Create the new type.  We do this before consuming the opening
15878        brace so the enum will be recorded as being on the line of its
15879        tag (or the 'enum' keyword, if there is no tag).  */
15880     type = start_enum (identifier, type, underlying_type,
15881                        scoped_enum_p, &is_new_type);
15882
15883   /* If the next token is not '{' it is an opaque-enum-specifier or an
15884      elaborated-type-specifier.  */
15885   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15886     {
15887       timevar_push (TV_PARSE_ENUM);
15888       if (nested_name_specifier
15889           && nested_name_specifier != error_mark_node)
15890         {
15891           /* The following catches invalid code such as:
15892              enum class S<int>::E { A, B, C }; */
15893           if (!processing_specialization
15894               && CLASS_TYPE_P (nested_name_specifier)
15895               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15896             error_at (type_start_token->location, "cannot add an enumerator "
15897                       "list to a template instantiation");
15898
15899           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15900             {
15901               error_at (type_start_token->location,
15902                         "%<%T::%E%> has not been declared",
15903                         TYPE_CONTEXT (nested_name_specifier),
15904                         nested_name_specifier);
15905               type = error_mark_node;
15906             }
15907           /* If that scope does not contain the scope in which the
15908              class was originally declared, the program is invalid.  */
15909           else if (prev_scope && !is_ancestor (prev_scope,
15910                                                nested_name_specifier))
15911             {
15912               if (at_namespace_scope_p ())
15913                 error_at (type_start_token->location,
15914                           "declaration of %qD in namespace %qD which does not "
15915                           "enclose %qD",
15916                           type, prev_scope, nested_name_specifier);
15917               else
15918                 error_at (type_start_token->location,
15919                           "declaration of %qD in %qD which does not "
15920                           "enclose %qD",
15921                           type, prev_scope, nested_name_specifier);
15922               type = error_mark_node;
15923             }
15924         }
15925
15926       if (scoped_enum_p)
15927         begin_scope (sk_scoped_enum, type);
15928
15929       /* Consume the opening brace.  */
15930       cp_lexer_consume_token (parser->lexer);
15931
15932       if (type == error_mark_node)
15933         ; /* Nothing to add */
15934       else if (OPAQUE_ENUM_P (type)
15935                || (cxx_dialect > cxx98 && processing_specialization))
15936         {
15937           new_value_list = true;
15938           SET_OPAQUE_ENUM_P (type, false);
15939           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15940         }
15941       else
15942         {
15943           error_at (type_start_token->location,
15944                     "multiple definition of %q#T", type);
15945           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15946                   "previous definition here");
15947           type = error_mark_node;
15948         }
15949
15950       if (type == error_mark_node)
15951         cp_parser_skip_to_end_of_block_or_statement (parser);
15952       /* If the next token is not '}', then there are some enumerators.  */
15953       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15954         {
15955           if (is_anonymous && !scoped_enum_p)
15956             pedwarn (type_start_token->location, OPT_Wpedantic,
15957                      "ISO C++ forbids empty anonymous enum");
15958         }
15959       else
15960         cp_parser_enumerator_list (parser, type);
15961
15962       /* Consume the final '}'.  */
15963       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15964
15965       if (scoped_enum_p)
15966         finish_scope ();
15967       timevar_pop (TV_PARSE_ENUM);
15968     }
15969   else
15970     {
15971       /* If a ';' follows, then it is an opaque-enum-specifier
15972         and additional restrictions apply.  */
15973       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15974         {
15975           if (is_anonymous)
15976             error_at (type_start_token->location,
15977                       "opaque-enum-specifier without name");
15978           else if (nested_name_specifier)
15979             error_at (type_start_token->location,
15980                       "opaque-enum-specifier must use a simple identifier");
15981         }
15982     }
15983
15984   /* Look for trailing attributes to apply to this enumeration, and
15985      apply them if appropriate.  */
15986   if (cp_parser_allow_gnu_extensions_p (parser))
15987     {
15988       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15989       trailing_attr = chainon (trailing_attr, attributes);
15990       cplus_decl_attributes (&type,
15991                              trailing_attr,
15992                              (int) ATTR_FLAG_TYPE_IN_PLACE);
15993     }
15994
15995   /* Finish up the enumeration.  */
15996   if (type != error_mark_node)
15997     {
15998       if (new_value_list)
15999         finish_enum_value_list (type);
16000       if (is_new_type)
16001         finish_enum (type);
16002     }
16003
16004   if (nested_name_specifier)
16005     {
16006       if (CLASS_TYPE_P (nested_name_specifier))
16007         {
16008           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16009           pop_scope (nested_name_specifier);
16010         }
16011       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16012         {
16013           pop_nested_namespace (nested_name_specifier);
16014         }
16015     }
16016  out:
16017   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16018   return type;
16019 }
16020
16021 /* Parse an enumerator-list.  The enumerators all have the indicated
16022    TYPE.
16023
16024    enumerator-list:
16025      enumerator-definition
16026      enumerator-list , enumerator-definition  */
16027
16028 static void
16029 cp_parser_enumerator_list (cp_parser* parser, tree type)
16030 {
16031   while (true)
16032     {
16033       /* Parse an enumerator-definition.  */
16034       cp_parser_enumerator_definition (parser, type);
16035
16036       /* If the next token is not a ',', we've reached the end of
16037          the list.  */
16038       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16039         break;
16040       /* Otherwise, consume the `,' and keep going.  */
16041       cp_lexer_consume_token (parser->lexer);
16042       /* If the next token is a `}', there is a trailing comma.  */
16043       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16044         {
16045           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16046             pedwarn (input_location, OPT_Wpedantic,
16047                      "comma at end of enumerator list");
16048           break;
16049         }
16050     }
16051 }
16052
16053 /* Parse an enumerator-definition.  The enumerator has the indicated
16054    TYPE.
16055
16056    enumerator-definition:
16057      enumerator
16058      enumerator = constant-expression
16059
16060    enumerator:
16061      identifier  */
16062
16063 static void
16064 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16065 {
16066   tree identifier;
16067   tree value;
16068   location_t loc;
16069
16070   /* Save the input location because we are interested in the location
16071      of the identifier and not the location of the explicit value.  */
16072   loc = cp_lexer_peek_token (parser->lexer)->location;
16073
16074   /* Look for the identifier.  */
16075   identifier = cp_parser_identifier (parser);
16076   if (identifier == error_mark_node)
16077     return;
16078
16079   /* If the next token is an '=', then there is an explicit value.  */
16080   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16081     {
16082       /* Consume the `=' token.  */
16083       cp_lexer_consume_token (parser->lexer);
16084       /* Parse the value.  */
16085       value = cp_parser_constant_expression (parser);
16086     }
16087   else
16088     value = NULL_TREE;
16089
16090   /* If we are processing a template, make sure the initializer of the
16091      enumerator doesn't contain any bare template parameter pack.  */
16092   if (check_for_bare_parameter_packs (value))
16093     value = error_mark_node;
16094
16095   /* Create the enumerator.  */
16096   build_enumerator (identifier, value, type, loc);
16097 }
16098
16099 /* Parse a namespace-name.
16100
16101    namespace-name:
16102      original-namespace-name
16103      namespace-alias
16104
16105    Returns the NAMESPACE_DECL for the namespace.  */
16106
16107 static tree
16108 cp_parser_namespace_name (cp_parser* parser)
16109 {
16110   tree identifier;
16111   tree namespace_decl;
16112
16113   cp_token *token = cp_lexer_peek_token (parser->lexer);
16114
16115   /* Get the name of the namespace.  */
16116   identifier = cp_parser_identifier (parser);
16117   if (identifier == error_mark_node)
16118     return error_mark_node;
16119
16120   /* Look up the identifier in the currently active scope.  Look only
16121      for namespaces, due to:
16122
16123        [basic.lookup.udir]
16124
16125        When looking up a namespace-name in a using-directive or alias
16126        definition, only namespace names are considered.
16127
16128      And:
16129
16130        [basic.lookup.qual]
16131
16132        During the lookup of a name preceding the :: scope resolution
16133        operator, object, function, and enumerator names are ignored.
16134
16135      (Note that cp_parser_qualifying_entity only calls this
16136      function if the token after the name is the scope resolution
16137      operator.)  */
16138   namespace_decl = cp_parser_lookup_name (parser, identifier,
16139                                           none_type,
16140                                           /*is_template=*/false,
16141                                           /*is_namespace=*/true,
16142                                           /*check_dependency=*/true,
16143                                           /*ambiguous_decls=*/NULL,
16144                                           token->location);
16145   /* If it's not a namespace, issue an error.  */
16146   if (namespace_decl == error_mark_node
16147       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16148     {
16149       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16150         error_at (token->location, "%qD is not a namespace-name", identifier);
16151       cp_parser_error (parser, "expected namespace-name");
16152       namespace_decl = error_mark_node;
16153     }
16154
16155   return namespace_decl;
16156 }
16157
16158 /* Parse a namespace-definition.
16159
16160    namespace-definition:
16161      named-namespace-definition
16162      unnamed-namespace-definition
16163
16164    named-namespace-definition:
16165      original-namespace-definition
16166      extension-namespace-definition
16167
16168    original-namespace-definition:
16169      namespace identifier { namespace-body }
16170
16171    extension-namespace-definition:
16172      namespace original-namespace-name { namespace-body }
16173
16174    unnamed-namespace-definition:
16175      namespace { namespace-body } */
16176
16177 static void
16178 cp_parser_namespace_definition (cp_parser* parser)
16179 {
16180   tree identifier, attribs;
16181   bool has_visibility;
16182   bool is_inline;
16183
16184   cp_ensure_no_omp_declare_simd (parser);
16185   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16186     {
16187       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16188       is_inline = true;
16189       cp_lexer_consume_token (parser->lexer);
16190     }
16191   else
16192     is_inline = false;
16193
16194   /* Look for the `namespace' keyword.  */
16195   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16196
16197   /* Get the name of the namespace.  We do not attempt to distinguish
16198      between an original-namespace-definition and an
16199      extension-namespace-definition at this point.  The semantic
16200      analysis routines are responsible for that.  */
16201   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16202     identifier = cp_parser_identifier (parser);
16203   else
16204     identifier = NULL_TREE;
16205
16206   /* Parse any specified attributes.  */
16207   attribs = cp_parser_attributes_opt (parser);
16208
16209   /* Look for the `{' to start the namespace.  */
16210   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16211   /* Start the namespace.  */
16212   push_namespace (identifier);
16213
16214   /* "inline namespace" is equivalent to a stub namespace definition
16215      followed by a strong using directive.  */
16216   if (is_inline)
16217     {
16218       tree name_space = current_namespace;
16219       /* Set up namespace association.  */
16220       DECL_NAMESPACE_ASSOCIATIONS (name_space)
16221         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16222                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
16223       /* Import the contents of the inline namespace.  */
16224       pop_namespace ();
16225       do_using_directive (name_space);
16226       push_namespace (identifier);
16227     }
16228
16229   has_visibility = handle_namespace_attrs (current_namespace, attribs);
16230
16231   /* Parse the body of the namespace.  */
16232   cp_parser_namespace_body (parser);
16233
16234   if (has_visibility)
16235     pop_visibility (1);
16236
16237   /* Finish the namespace.  */
16238   pop_namespace ();
16239   /* Look for the final `}'.  */
16240   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16241 }
16242
16243 /* Parse a namespace-body.
16244
16245    namespace-body:
16246      declaration-seq [opt]  */
16247
16248 static void
16249 cp_parser_namespace_body (cp_parser* parser)
16250 {
16251   cp_parser_declaration_seq_opt (parser);
16252 }
16253
16254 /* Parse a namespace-alias-definition.
16255
16256    namespace-alias-definition:
16257      namespace identifier = qualified-namespace-specifier ;  */
16258
16259 static void
16260 cp_parser_namespace_alias_definition (cp_parser* parser)
16261 {
16262   tree identifier;
16263   tree namespace_specifier;
16264
16265   cp_token *token = cp_lexer_peek_token (parser->lexer);
16266
16267   /* Look for the `namespace' keyword.  */
16268   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16269   /* Look for the identifier.  */
16270   identifier = cp_parser_identifier (parser);
16271   if (identifier == error_mark_node)
16272     return;
16273   /* Look for the `=' token.  */
16274   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16275       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
16276     {
16277       error_at (token->location, "%<namespace%> definition is not allowed here");
16278       /* Skip the definition.  */
16279       cp_lexer_consume_token (parser->lexer);
16280       if (cp_parser_skip_to_closing_brace (parser))
16281         cp_lexer_consume_token (parser->lexer);
16282       return;
16283     }
16284   cp_parser_require (parser, CPP_EQ, RT_EQ);
16285   /* Look for the qualified-namespace-specifier.  */
16286   namespace_specifier
16287     = cp_parser_qualified_namespace_specifier (parser);
16288   /* Look for the `;' token.  */
16289   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16290
16291   /* Register the alias in the symbol table.  */
16292   do_namespace_alias (identifier, namespace_specifier);
16293 }
16294
16295 /* Parse a qualified-namespace-specifier.
16296
16297    qualified-namespace-specifier:
16298      :: [opt] nested-name-specifier [opt] namespace-name
16299
16300    Returns a NAMESPACE_DECL corresponding to the specified
16301    namespace.  */
16302
16303 static tree
16304 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16305 {
16306   /* Look for the optional `::'.  */
16307   cp_parser_global_scope_opt (parser,
16308                               /*current_scope_valid_p=*/false);
16309
16310   /* Look for the optional nested-name-specifier.  */
16311   cp_parser_nested_name_specifier_opt (parser,
16312                                        /*typename_keyword_p=*/false,
16313                                        /*check_dependency_p=*/true,
16314                                        /*type_p=*/false,
16315                                        /*is_declaration=*/true);
16316
16317   return cp_parser_namespace_name (parser);
16318 }
16319
16320 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16321    access declaration.
16322
16323    using-declaration:
16324      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16325      using :: unqualified-id ;  
16326
16327    access-declaration:
16328      qualified-id ;  
16329
16330    */
16331
16332 static bool
16333 cp_parser_using_declaration (cp_parser* parser, 
16334                              bool access_declaration_p)
16335 {
16336   cp_token *token;
16337   bool typename_p = false;
16338   bool global_scope_p;
16339   tree decl;
16340   tree identifier;
16341   tree qscope;
16342   int oldcount = errorcount;
16343   cp_token *diag_token = NULL;
16344
16345   if (access_declaration_p)
16346     {
16347       diag_token = cp_lexer_peek_token (parser->lexer);
16348       cp_parser_parse_tentatively (parser);
16349     }
16350   else
16351     {
16352       /* Look for the `using' keyword.  */
16353       cp_parser_require_keyword (parser, RID_USING, RT_USING);
16354       
16355       /* Peek at the next token.  */
16356       token = cp_lexer_peek_token (parser->lexer);
16357       /* See if it's `typename'.  */
16358       if (token->keyword == RID_TYPENAME)
16359         {
16360           /* Remember that we've seen it.  */
16361           typename_p = true;
16362           /* Consume the `typename' token.  */
16363           cp_lexer_consume_token (parser->lexer);
16364         }
16365     }
16366
16367   /* Look for the optional global scope qualification.  */
16368   global_scope_p
16369     = (cp_parser_global_scope_opt (parser,
16370                                    /*current_scope_valid_p=*/false)
16371        != NULL_TREE);
16372
16373   /* If we saw `typename', or didn't see `::', then there must be a
16374      nested-name-specifier present.  */
16375   if (typename_p || !global_scope_p)
16376     {
16377       qscope = cp_parser_nested_name_specifier (parser, typename_p,
16378                                                 /*check_dependency_p=*/true,
16379                                                 /*type_p=*/false,
16380                                                 /*is_declaration=*/true);
16381       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16382         {
16383           cp_parser_skip_to_end_of_block_or_statement (parser);
16384           return false;
16385         }
16386     }
16387   /* Otherwise, we could be in either of the two productions.  In that
16388      case, treat the nested-name-specifier as optional.  */
16389   else
16390     qscope = cp_parser_nested_name_specifier_opt (parser,
16391                                                   /*typename_keyword_p=*/false,
16392                                                   /*check_dependency_p=*/true,
16393                                                   /*type_p=*/false,
16394                                                   /*is_declaration=*/true);
16395   if (!qscope)
16396     qscope = global_namespace;
16397   else if (UNSCOPED_ENUM_P (qscope))
16398     qscope = CP_TYPE_CONTEXT (qscope);
16399
16400   if (access_declaration_p && cp_parser_error_occurred (parser))
16401     /* Something has already gone wrong; there's no need to parse
16402        further.  Since an error has occurred, the return value of
16403        cp_parser_parse_definitely will be false, as required.  */
16404     return cp_parser_parse_definitely (parser);
16405
16406   token = cp_lexer_peek_token (parser->lexer);
16407   /* Parse the unqualified-id.  */
16408   identifier = cp_parser_unqualified_id (parser,
16409                                          /*template_keyword_p=*/false,
16410                                          /*check_dependency_p=*/true,
16411                                          /*declarator_p=*/true,
16412                                          /*optional_p=*/false);
16413
16414   if (access_declaration_p)
16415     {
16416       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16417         cp_parser_simulate_error (parser);
16418       if (!cp_parser_parse_definitely (parser))
16419         return false;
16420     }
16421
16422   /* The function we call to handle a using-declaration is different
16423      depending on what scope we are in.  */
16424   if (qscope == error_mark_node || identifier == error_mark_node)
16425     ;
16426   else if (!identifier_p (identifier)
16427            && TREE_CODE (identifier) != BIT_NOT_EXPR)
16428     /* [namespace.udecl]
16429
16430        A using declaration shall not name a template-id.  */
16431     error_at (token->location,
16432               "a template-id may not appear in a using-declaration");
16433   else
16434     {
16435       if (at_class_scope_p ())
16436         {
16437           /* Create the USING_DECL.  */
16438           decl = do_class_using_decl (parser->scope, identifier);
16439
16440           if (decl && typename_p)
16441             USING_DECL_TYPENAME_P (decl) = 1;
16442
16443           if (check_for_bare_parameter_packs (decl))
16444             {
16445               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16446               return false;
16447             }
16448           else
16449             /* Add it to the list of members in this class.  */
16450             finish_member_declaration (decl);
16451         }
16452       else
16453         {
16454           decl = cp_parser_lookup_name_simple (parser,
16455                                                identifier,
16456                                                token->location);
16457           if (decl == error_mark_node)
16458             cp_parser_name_lookup_error (parser, identifier,
16459                                          decl, NLE_NULL,
16460                                          token->location);
16461           else if (check_for_bare_parameter_packs (decl))
16462             {
16463               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16464               return false;
16465             }
16466           else if (!at_namespace_scope_p ())
16467             do_local_using_decl (decl, qscope, identifier);
16468           else
16469             do_toplevel_using_decl (decl, qscope, identifier);
16470         }
16471     }
16472
16473   /* Look for the final `;'.  */
16474   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16475
16476   if (access_declaration_p && errorcount == oldcount)
16477     warning_at (diag_token->location, OPT_Wdeprecated,
16478                 "access declarations are deprecated "
16479                 "in favour of using-declarations; "
16480                 "suggestion: add the %<using%> keyword");
16481
16482   return true;
16483 }
16484
16485 /* Parse an alias-declaration.
16486
16487    alias-declaration:
16488      using identifier attribute-specifier-seq [opt] = type-id  */
16489
16490 static tree
16491 cp_parser_alias_declaration (cp_parser* parser)
16492 {
16493   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16494   location_t id_location;
16495   cp_declarator *declarator;
16496   cp_decl_specifier_seq decl_specs;
16497   bool member_p;
16498   const char *saved_message = NULL;
16499
16500   /* Look for the `using' keyword.  */
16501   cp_token *using_token
16502     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16503   if (using_token == NULL)
16504     return error_mark_node;
16505
16506   id_location = cp_lexer_peek_token (parser->lexer)->location;
16507   id = cp_parser_identifier (parser);
16508   if (id == error_mark_node)
16509     return error_mark_node;
16510
16511   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16512   attributes = cp_parser_attributes_opt (parser);
16513   if (attributes == error_mark_node)
16514     return error_mark_node;
16515
16516   cp_parser_require (parser, CPP_EQ, RT_EQ);
16517
16518   if (cp_parser_error_occurred (parser))
16519     return error_mark_node;
16520
16521   cp_parser_commit_to_tentative_parse (parser);
16522
16523   /* Now we are going to parse the type-id of the declaration.  */
16524
16525   /*
16526     [dcl.type]/3 says:
16527
16528         "A type-specifier-seq shall not define a class or enumeration
16529          unless it appears in the type-id of an alias-declaration (7.1.3) that
16530          is not the declaration of a template-declaration."
16531
16532     In other words, if we currently are in an alias template, the
16533     type-id should not define a type.
16534
16535     So let's set parser->type_definition_forbidden_message in that
16536     case; cp_parser_check_type_definition (called by
16537     cp_parser_class_specifier) will then emit an error if a type is
16538     defined in the type-id.  */
16539   if (parser->num_template_parameter_lists)
16540     {
16541       saved_message = parser->type_definition_forbidden_message;
16542       parser->type_definition_forbidden_message =
16543         G_("types may not be defined in alias template declarations");
16544     }
16545
16546   type = cp_parser_type_id (parser);
16547
16548   /* Restore the error message if need be.  */
16549   if (parser->num_template_parameter_lists)
16550     parser->type_definition_forbidden_message = saved_message;
16551
16552   if (type == error_mark_node
16553       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16554     {
16555       cp_parser_skip_to_end_of_block_or_statement (parser);
16556       return error_mark_node;
16557     }
16558
16559   /* A typedef-name can also be introduced by an alias-declaration. The
16560      identifier following the using keyword becomes a typedef-name. It has
16561      the same semantics as if it were introduced by the typedef
16562      specifier. In particular, it does not define a new type and it shall
16563      not appear in the type-id.  */
16564
16565   clear_decl_specs (&decl_specs);
16566   decl_specs.type = type;
16567   if (attributes != NULL_TREE)
16568     {
16569       decl_specs.attributes = attributes;
16570       set_and_check_decl_spec_loc (&decl_specs,
16571                                    ds_attribute,
16572                                    attrs_token);
16573     }
16574   set_and_check_decl_spec_loc (&decl_specs,
16575                                ds_typedef,
16576                                using_token);
16577   set_and_check_decl_spec_loc (&decl_specs,
16578                                ds_alias,
16579                                using_token);
16580
16581   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16582   declarator->id_loc = id_location;
16583
16584   member_p = at_class_scope_p ();
16585   if (member_p)
16586     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16587                       NULL_TREE, attributes);
16588   else
16589     decl = start_decl (declarator, &decl_specs, 0,
16590                        attributes, NULL_TREE, &pushed_scope);
16591   if (decl == error_mark_node)
16592     return decl;
16593
16594   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16595
16596   if (pushed_scope)
16597     pop_scope (pushed_scope);
16598
16599   /* If decl is a template, return its TEMPLATE_DECL so that it gets
16600      added into the symbol table; otherwise, return the TYPE_DECL.  */
16601   if (DECL_LANG_SPECIFIC (decl)
16602       && DECL_TEMPLATE_INFO (decl)
16603       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16604     {
16605       decl = DECL_TI_TEMPLATE (decl);
16606       if (member_p)
16607         check_member_template (decl);
16608     }
16609
16610   return decl;
16611 }
16612
16613 /* Parse a using-directive.
16614
16615    using-directive:
16616      using namespace :: [opt] nested-name-specifier [opt]
16617        namespace-name ;  */
16618
16619 static void
16620 cp_parser_using_directive (cp_parser* parser)
16621 {
16622   tree namespace_decl;
16623   tree attribs;
16624
16625   /* Look for the `using' keyword.  */
16626   cp_parser_require_keyword (parser, RID_USING, RT_USING);
16627   /* And the `namespace' keyword.  */
16628   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16629   /* Look for the optional `::' operator.  */
16630   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16631   /* And the optional nested-name-specifier.  */
16632   cp_parser_nested_name_specifier_opt (parser,
16633                                        /*typename_keyword_p=*/false,
16634                                        /*check_dependency_p=*/true,
16635                                        /*type_p=*/false,
16636                                        /*is_declaration=*/true);
16637   /* Get the namespace being used.  */
16638   namespace_decl = cp_parser_namespace_name (parser);
16639   /* And any specified attributes.  */
16640   attribs = cp_parser_attributes_opt (parser);
16641   /* Update the symbol table.  */
16642   parse_using_directive (namespace_decl, attribs);
16643   /* Look for the final `;'.  */
16644   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16645 }
16646
16647 /* Parse an asm-definition.
16648
16649    asm-definition:
16650      asm ( string-literal ) ;
16651
16652    GNU Extension:
16653
16654    asm-definition:
16655      asm volatile [opt] ( string-literal ) ;
16656      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16657      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16658                           : asm-operand-list [opt] ) ;
16659      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16660                           : asm-operand-list [opt]
16661                           : asm-clobber-list [opt] ) ;
16662      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16663                                : asm-clobber-list [opt]
16664                                : asm-goto-list ) ;  */
16665
16666 static void
16667 cp_parser_asm_definition (cp_parser* parser)
16668 {
16669   tree string;
16670   tree outputs = NULL_TREE;
16671   tree inputs = NULL_TREE;
16672   tree clobbers = NULL_TREE;
16673   tree labels = NULL_TREE;
16674   tree asm_stmt;
16675   bool volatile_p = false;
16676   bool extended_p = false;
16677   bool invalid_inputs_p = false;
16678   bool invalid_outputs_p = false;
16679   bool goto_p = false;
16680   required_token missing = RT_NONE;
16681
16682   /* Look for the `asm' keyword.  */
16683   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16684
16685   if (parser->in_function_body
16686       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16687     {
16688       error ("%<asm%> in %<constexpr%> function");
16689       cp_function_chain->invalid_constexpr = true;
16690     }
16691
16692   /* See if the next token is `volatile'.  */
16693   if (cp_parser_allow_gnu_extensions_p (parser)
16694       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16695     {
16696       /* Remember that we saw the `volatile' keyword.  */
16697       volatile_p = true;
16698       /* Consume the token.  */
16699       cp_lexer_consume_token (parser->lexer);
16700     }
16701   if (cp_parser_allow_gnu_extensions_p (parser)
16702       && parser->in_function_body
16703       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16704     {
16705       /* Remember that we saw the `goto' keyword.  */
16706       goto_p = true;
16707       /* Consume the token.  */
16708       cp_lexer_consume_token (parser->lexer);
16709     }
16710   /* Look for the opening `('.  */
16711   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16712     return;
16713   /* Look for the string.  */
16714   string = cp_parser_string_literal (parser, false, false);
16715   if (string == error_mark_node)
16716     {
16717       cp_parser_skip_to_closing_parenthesis (parser, true, false,
16718                                              /*consume_paren=*/true);
16719       return;
16720     }
16721
16722   /* If we're allowing GNU extensions, check for the extended assembly
16723      syntax.  Unfortunately, the `:' tokens need not be separated by
16724      a space in C, and so, for compatibility, we tolerate that here
16725      too.  Doing that means that we have to treat the `::' operator as
16726      two `:' tokens.  */
16727   if (cp_parser_allow_gnu_extensions_p (parser)
16728       && parser->in_function_body
16729       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16730           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16731     {
16732       bool inputs_p = false;
16733       bool clobbers_p = false;
16734       bool labels_p = false;
16735
16736       /* The extended syntax was used.  */
16737       extended_p = true;
16738
16739       /* Look for outputs.  */
16740       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16741         {
16742           /* Consume the `:'.  */
16743           cp_lexer_consume_token (parser->lexer);
16744           /* Parse the output-operands.  */
16745           if (cp_lexer_next_token_is_not (parser->lexer,
16746                                           CPP_COLON)
16747               && cp_lexer_next_token_is_not (parser->lexer,
16748                                              CPP_SCOPE)
16749               && cp_lexer_next_token_is_not (parser->lexer,
16750                                              CPP_CLOSE_PAREN)
16751               && !goto_p)
16752             outputs = cp_parser_asm_operand_list (parser);
16753
16754             if (outputs == error_mark_node)
16755               invalid_outputs_p = true;
16756         }
16757       /* If the next token is `::', there are no outputs, and the
16758          next token is the beginning of the inputs.  */
16759       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16760         /* The inputs are coming next.  */
16761         inputs_p = true;
16762
16763       /* Look for inputs.  */
16764       if (inputs_p
16765           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16766         {
16767           /* Consume the `:' or `::'.  */
16768           cp_lexer_consume_token (parser->lexer);
16769           /* Parse the output-operands.  */
16770           if (cp_lexer_next_token_is_not (parser->lexer,
16771                                           CPP_COLON)
16772               && cp_lexer_next_token_is_not (parser->lexer,
16773                                              CPP_SCOPE)
16774               && cp_lexer_next_token_is_not (parser->lexer,
16775                                              CPP_CLOSE_PAREN))
16776             inputs = cp_parser_asm_operand_list (parser);
16777
16778             if (inputs == error_mark_node)
16779               invalid_inputs_p = true;
16780         }
16781       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16782         /* The clobbers are coming next.  */
16783         clobbers_p = true;
16784
16785       /* Look for clobbers.  */
16786       if (clobbers_p
16787           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16788         {
16789           clobbers_p = true;
16790           /* Consume the `:' or `::'.  */
16791           cp_lexer_consume_token (parser->lexer);
16792           /* Parse the clobbers.  */
16793           if (cp_lexer_next_token_is_not (parser->lexer,
16794                                           CPP_COLON)
16795               && cp_lexer_next_token_is_not (parser->lexer,
16796                                              CPP_CLOSE_PAREN))
16797             clobbers = cp_parser_asm_clobber_list (parser);
16798         }
16799       else if (goto_p
16800                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16801         /* The labels are coming next.  */
16802         labels_p = true;
16803
16804       /* Look for labels.  */
16805       if (labels_p
16806           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16807         {
16808           labels_p = true;
16809           /* Consume the `:' or `::'.  */
16810           cp_lexer_consume_token (parser->lexer);
16811           /* Parse the labels.  */
16812           labels = cp_parser_asm_label_list (parser);
16813         }
16814
16815       if (goto_p && !labels_p)
16816         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16817     }
16818   else if (goto_p)
16819     missing = RT_COLON_SCOPE;
16820
16821   /* Look for the closing `)'.  */
16822   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16823                           missing ? missing : RT_CLOSE_PAREN))
16824     cp_parser_skip_to_closing_parenthesis (parser, true, false,
16825                                            /*consume_paren=*/true);
16826   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16827
16828   if (!invalid_inputs_p && !invalid_outputs_p)
16829     {
16830       /* Create the ASM_EXPR.  */
16831       if (parser->in_function_body)
16832         {
16833           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16834                                       inputs, clobbers, labels);
16835           /* If the extended syntax was not used, mark the ASM_EXPR.  */
16836           if (!extended_p)
16837             {
16838               tree temp = asm_stmt;
16839               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16840                 temp = TREE_OPERAND (temp, 0);
16841
16842               ASM_INPUT_P (temp) = 1;
16843             }
16844         }
16845       else
16846         symtab->finalize_toplevel_asm (string);
16847     }
16848 }
16849
16850 /* Declarators [gram.dcl.decl] */
16851
16852 /* Parse an init-declarator.
16853
16854    init-declarator:
16855      declarator initializer [opt]
16856
16857    GNU Extension:
16858
16859    init-declarator:
16860      declarator asm-specification [opt] attributes [opt] initializer [opt]
16861
16862    function-definition:
16863      decl-specifier-seq [opt] declarator ctor-initializer [opt]
16864        function-body
16865      decl-specifier-seq [opt] declarator function-try-block
16866
16867    GNU Extension:
16868
16869    function-definition:
16870      __extension__ function-definition
16871
16872    TM Extension:
16873
16874    function-definition:
16875      decl-specifier-seq [opt] declarator function-transaction-block
16876
16877    The DECL_SPECIFIERS apply to this declarator.  Returns a
16878    representation of the entity declared.  If MEMBER_P is TRUE, then
16879    this declarator appears in a class scope.  The new DECL created by
16880    this declarator is returned.
16881
16882    The CHECKS are access checks that should be performed once we know
16883    what entity is being declared (and, therefore, what classes have
16884    befriended it).
16885
16886    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16887    for a function-definition here as well.  If the declarator is a
16888    declarator for a function-definition, *FUNCTION_DEFINITION_P will
16889    be TRUE upon return.  By that point, the function-definition will
16890    have been completely parsed.
16891
16892    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16893    is FALSE.
16894
16895    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16896    parsed declaration if it is an uninitialized single declarator not followed
16897    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16898    if present, will not be consumed.  If returned, this declarator will be
16899    created with SD_INITIALIZED but will not call cp_finish_decl.
16900
16901    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16902    and there is an initializer, the pointed location_t is set to the
16903    location of the '=' or `(', or '{' in C++11 token introducing the
16904    initializer.  */
16905
16906 static tree
16907 cp_parser_init_declarator (cp_parser* parser,
16908                            cp_decl_specifier_seq *decl_specifiers,
16909                            vec<deferred_access_check, va_gc> *checks,
16910                            bool function_definition_allowed_p,
16911                            bool member_p,
16912                            int declares_class_or_enum,
16913                            bool* function_definition_p,
16914                            tree* maybe_range_for_decl,
16915                            location_t* init_loc)
16916 {
16917   cp_token *token = NULL, *asm_spec_start_token = NULL,
16918            *attributes_start_token = NULL;
16919   cp_declarator *declarator;
16920   tree prefix_attributes;
16921   tree attributes = NULL;
16922   tree asm_specification;
16923   tree initializer;
16924   tree decl = NULL_TREE;
16925   tree scope;
16926   int is_initialized;
16927   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
16928      initialized with "= ..", CPP_OPEN_PAREN if initialized with
16929      "(...)".  */
16930   enum cpp_ttype initialization_kind;
16931   bool is_direct_init = false;
16932   bool is_non_constant_init;
16933   int ctor_dtor_or_conv_p;
16934   bool friend_p = cp_parser_friend_p (decl_specifiers);
16935   tree pushed_scope = NULL_TREE;
16936   bool range_for_decl_p = false;
16937   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16938   location_t tmp_init_loc = UNKNOWN_LOCATION;
16939
16940   /* Gather the attributes that were provided with the
16941      decl-specifiers.  */
16942   prefix_attributes = decl_specifiers->attributes;
16943
16944   /* Assume that this is not the declarator for a function
16945      definition.  */
16946   if (function_definition_p)
16947     *function_definition_p = false;
16948
16949   /* Default arguments are only permitted for function parameters.  */
16950   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16951     parser->default_arg_ok_p = false;
16952
16953   /* Defer access checks while parsing the declarator; we cannot know
16954      what names are accessible until we know what is being
16955      declared.  */
16956   resume_deferring_access_checks ();
16957
16958   /* Parse the declarator.  */
16959   token = cp_lexer_peek_token (parser->lexer);
16960   declarator
16961     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16962                             &ctor_dtor_or_conv_p,
16963                             /*parenthesized_p=*/NULL,
16964                             member_p, friend_p);
16965   /* Gather up the deferred checks.  */
16966   stop_deferring_access_checks ();
16967
16968   parser->default_arg_ok_p = saved_default_arg_ok_p;
16969
16970   /* If the DECLARATOR was erroneous, there's no need to go
16971      further.  */
16972   if (declarator == cp_error_declarator)
16973     return error_mark_node;
16974
16975   /* Check that the number of template-parameter-lists is OK.  */
16976   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16977                                                        token->location))
16978     return error_mark_node;
16979
16980   if (declares_class_or_enum & 2)
16981     cp_parser_check_for_definition_in_return_type (declarator,
16982                                                    decl_specifiers->type,
16983                                                    decl_specifiers->locations[ds_type_spec]);
16984
16985   /* Figure out what scope the entity declared by the DECLARATOR is
16986      located in.  `grokdeclarator' sometimes changes the scope, so
16987      we compute it now.  */
16988   scope = get_scope_of_declarator (declarator);
16989
16990   /* Perform any lookups in the declared type which were thought to be
16991      dependent, but are not in the scope of the declarator.  */
16992   decl_specifiers->type
16993     = maybe_update_decl_type (decl_specifiers->type, scope);
16994
16995   /* If we're allowing GNU extensions, look for an
16996      asm-specification.  */
16997   if (cp_parser_allow_gnu_extensions_p (parser))
16998     {
16999       /* Look for an asm-specification.  */
17000       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17001       asm_specification = cp_parser_asm_specification_opt (parser);
17002     }
17003   else
17004     asm_specification = NULL_TREE;
17005
17006   /* Look for attributes.  */
17007   attributes_start_token = cp_lexer_peek_token (parser->lexer);
17008   attributes = cp_parser_attributes_opt (parser);
17009
17010   /* Peek at the next token.  */
17011   token = cp_lexer_peek_token (parser->lexer);
17012
17013   bool bogus_implicit_tmpl = false;
17014
17015   if (function_declarator_p (declarator))
17016     {
17017       /* Check to see if the token indicates the start of a
17018          function-definition.  */
17019       if (cp_parser_token_starts_function_definition_p (token))
17020         {
17021           if (!function_definition_allowed_p)
17022             {
17023               /* If a function-definition should not appear here, issue an
17024                  error message.  */
17025               cp_parser_error (parser,
17026                                "a function-definition is not allowed here");
17027               return error_mark_node;
17028             }
17029
17030           location_t func_brace_location
17031             = cp_lexer_peek_token (parser->lexer)->location;
17032
17033           /* Neither attributes nor an asm-specification are allowed
17034              on a function-definition.  */
17035           if (asm_specification)
17036             error_at (asm_spec_start_token->location,
17037                       "an asm-specification is not allowed "
17038                       "on a function-definition");
17039           if (attributes)
17040             error_at (attributes_start_token->location,
17041                       "attributes are not allowed "
17042                       "on a function-definition");
17043           /* This is a function-definition.  */
17044           *function_definition_p = true;
17045
17046           /* Parse the function definition.  */
17047           if (member_p)
17048             decl = cp_parser_save_member_function_body (parser,
17049                                                         decl_specifiers,
17050                                                         declarator,
17051                                                         prefix_attributes);
17052           else
17053             decl =
17054               (cp_parser_function_definition_from_specifiers_and_declarator
17055                (parser, decl_specifiers, prefix_attributes, declarator));
17056
17057           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17058             {
17059               /* This is where the prologue starts...  */
17060               DECL_STRUCT_FUNCTION (decl)->function_start_locus
17061                 = func_brace_location;
17062             }
17063
17064           return decl;
17065         }
17066     }
17067   else if (parser->fully_implicit_function_template_p)
17068     {
17069       /* A non-template declaration involving a function parameter list
17070          containing an implicit template parameter will be made into a
17071          template.  If the resulting declaration is not going to be an
17072          actual function then finish the template scope here to prevent it.
17073          An error message will be issued once we have a decl to talk about.
17074
17075          FIXME probably we should do type deduction rather than create an
17076          implicit template, but the standard currently doesn't allow it. */
17077       bogus_implicit_tmpl = true;
17078       finish_fully_implicit_template (parser, NULL_TREE);
17079     }
17080
17081   /* [dcl.dcl]
17082
17083      Only in function declarations for constructors, destructors, and
17084      type conversions can the decl-specifier-seq be omitted.
17085
17086      We explicitly postpone this check past the point where we handle
17087      function-definitions because we tolerate function-definitions
17088      that are missing their return types in some modes.  */
17089   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17090     {
17091       cp_parser_error (parser,
17092                        "expected constructor, destructor, or type conversion");
17093       return error_mark_node;
17094     }
17095
17096   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
17097   if (token->type == CPP_EQ
17098       || token->type == CPP_OPEN_PAREN
17099       || token->type == CPP_OPEN_BRACE)
17100     {
17101       is_initialized = SD_INITIALIZED;
17102       initialization_kind = token->type;
17103       if (maybe_range_for_decl)
17104         *maybe_range_for_decl = error_mark_node;
17105       tmp_init_loc = token->location;
17106       if (init_loc && *init_loc == UNKNOWN_LOCATION)
17107         *init_loc = tmp_init_loc;
17108
17109       if (token->type == CPP_EQ
17110           && function_declarator_p (declarator))
17111         {
17112           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17113           if (t2->keyword == RID_DEFAULT)
17114             is_initialized = SD_DEFAULTED;
17115           else if (t2->keyword == RID_DELETE)
17116             is_initialized = SD_DELETED;
17117         }
17118     }
17119   else
17120     {
17121       /* If the init-declarator isn't initialized and isn't followed by a
17122          `,' or `;', it's not a valid init-declarator.  */
17123       if (token->type != CPP_COMMA
17124           && token->type != CPP_SEMICOLON)
17125         {
17126           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17127             range_for_decl_p = true;
17128           else
17129             {
17130               if (!maybe_range_for_decl)
17131                 cp_parser_error (parser, "expected initializer");
17132               return error_mark_node;
17133             }
17134         }
17135       is_initialized = SD_UNINITIALIZED;
17136       initialization_kind = CPP_EOF;
17137     }
17138
17139   /* Because start_decl has side-effects, we should only call it if we
17140      know we're going ahead.  By this point, we know that we cannot
17141      possibly be looking at any other construct.  */
17142   cp_parser_commit_to_tentative_parse (parser);
17143
17144   /* Enter the newly declared entry in the symbol table.  If we're
17145      processing a declaration in a class-specifier, we wait until
17146      after processing the initializer.  */
17147   if (!member_p)
17148     {
17149       if (parser->in_unbraced_linkage_specification_p)
17150         decl_specifiers->storage_class = sc_extern;
17151       decl = start_decl (declarator, decl_specifiers,
17152                          range_for_decl_p? SD_INITIALIZED : is_initialized,
17153                          attributes, prefix_attributes, &pushed_scope);
17154       cp_finalize_omp_declare_simd (parser, decl);
17155       /* Adjust location of decl if declarator->id_loc is more appropriate:
17156          set, and decl wasn't merged with another decl, in which case its
17157          location would be different from input_location, and more accurate.  */
17158       if (DECL_P (decl)
17159           && declarator->id_loc != UNKNOWN_LOCATION
17160           && DECL_SOURCE_LOCATION (decl) == input_location)
17161         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17162     }
17163   else if (scope)
17164     /* Enter the SCOPE.  That way unqualified names appearing in the
17165        initializer will be looked up in SCOPE.  */
17166     pushed_scope = push_scope (scope);
17167
17168   /* Perform deferred access control checks, now that we know in which
17169      SCOPE the declared entity resides.  */
17170   if (!member_p && decl)
17171     {
17172       tree saved_current_function_decl = NULL_TREE;
17173
17174       /* If the entity being declared is a function, pretend that we
17175          are in its scope.  If it is a `friend', it may have access to
17176          things that would not otherwise be accessible.  */
17177       if (TREE_CODE (decl) == FUNCTION_DECL)
17178         {
17179           saved_current_function_decl = current_function_decl;
17180           current_function_decl = decl;
17181         }
17182
17183       /* Perform access checks for template parameters.  */
17184       cp_parser_perform_template_parameter_access_checks (checks);
17185
17186       /* Perform the access control checks for the declarator and the
17187          decl-specifiers.  */
17188       perform_deferred_access_checks (tf_warning_or_error);
17189
17190       /* Restore the saved value.  */
17191       if (TREE_CODE (decl) == FUNCTION_DECL)
17192         current_function_decl = saved_current_function_decl;
17193     }
17194
17195   /* Parse the initializer.  */
17196   initializer = NULL_TREE;
17197   is_direct_init = false;
17198   is_non_constant_init = true;
17199   if (is_initialized)
17200     {
17201       if (function_declarator_p (declarator))
17202         {
17203            if (initialization_kind == CPP_EQ)
17204              initializer = cp_parser_pure_specifier (parser);
17205            else
17206              {
17207                /* If the declaration was erroneous, we don't really
17208                   know what the user intended, so just silently
17209                   consume the initializer.  */
17210                if (decl != error_mark_node)
17211                  error_at (tmp_init_loc, "initializer provided for function");
17212                cp_parser_skip_to_closing_parenthesis (parser,
17213                                                       /*recovering=*/true,
17214                                                       /*or_comma=*/false,
17215                                                       /*consume_paren=*/true);
17216              }
17217         }
17218       else
17219         {
17220           /* We want to record the extra mangling scope for in-class
17221              initializers of class members and initializers of static data
17222              member templates.  The former involves deferring
17223              parsing of the initializer until end of class as with default
17224              arguments.  So right here we only handle the latter.  */
17225           if (!member_p && processing_template_decl)
17226             start_lambda_scope (decl);
17227           initializer = cp_parser_initializer (parser,
17228                                                &is_direct_init,
17229                                                &is_non_constant_init);
17230           if (!member_p && processing_template_decl)
17231             finish_lambda_scope ();
17232           if (initializer == error_mark_node)
17233             cp_parser_skip_to_end_of_statement (parser);
17234         }
17235     }
17236
17237   /* The old parser allows attributes to appear after a parenthesized
17238      initializer.  Mark Mitchell proposed removing this functionality
17239      on the GCC mailing lists on 2002-08-13.  This parser accepts the
17240      attributes -- but ignores them.  */
17241   if (cp_parser_allow_gnu_extensions_p (parser)
17242       && initialization_kind == CPP_OPEN_PAREN)
17243     if (cp_parser_attributes_opt (parser))
17244       warning (OPT_Wattributes,
17245                "attributes after parenthesized initializer ignored");
17246
17247   /* And now complain about a non-function implicit template.  */
17248   if (bogus_implicit_tmpl)
17249     error_at (DECL_SOURCE_LOCATION (decl),
17250               "non-function %qD declared as implicit template", decl);
17251
17252   /* For an in-class declaration, use `grokfield' to create the
17253      declaration.  */
17254   if (member_p)
17255     {
17256       if (pushed_scope)
17257         {
17258           pop_scope (pushed_scope);
17259           pushed_scope = NULL_TREE;
17260         }
17261       decl = grokfield (declarator, decl_specifiers,
17262                         initializer, !is_non_constant_init,
17263                         /*asmspec=*/NULL_TREE,
17264                         chainon (attributes, prefix_attributes));
17265       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17266         cp_parser_save_default_args (parser, decl);
17267       cp_finalize_omp_declare_simd (parser, decl);
17268     }
17269
17270   /* Finish processing the declaration.  But, skip member
17271      declarations.  */
17272   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17273     {
17274       cp_finish_decl (decl,
17275                       initializer, !is_non_constant_init,
17276                       asm_specification,
17277                       /* If the initializer is in parentheses, then this is
17278                          a direct-initialization, which means that an
17279                          `explicit' constructor is OK.  Otherwise, an
17280                          `explicit' constructor cannot be used.  */
17281                       ((is_direct_init || !is_initialized)
17282                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17283     }
17284   else if ((cxx_dialect != cxx98) && friend_p
17285            && decl && TREE_CODE (decl) == FUNCTION_DECL)
17286     /* Core issue #226 (C++0x only): A default template-argument
17287        shall not be specified in a friend class template
17288        declaration. */
17289     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
17290                              /*is_partial=*/false, /*is_friend_decl=*/1);
17291
17292   if (!friend_p && pushed_scope)
17293     pop_scope (pushed_scope);
17294
17295   if (function_declarator_p (declarator)
17296       && parser->fully_implicit_function_template_p)
17297     {
17298       if (member_p)
17299         decl = finish_fully_implicit_template (parser, decl);
17300       else
17301         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17302     }
17303
17304   return decl;
17305 }
17306
17307 /* Parse a declarator.
17308
17309    declarator:
17310      direct-declarator
17311      ptr-operator declarator
17312
17313    abstract-declarator:
17314      ptr-operator abstract-declarator [opt]
17315      direct-abstract-declarator
17316
17317    GNU Extensions:
17318
17319    declarator:
17320      attributes [opt] direct-declarator
17321      attributes [opt] ptr-operator declarator
17322
17323    abstract-declarator:
17324      attributes [opt] ptr-operator abstract-declarator [opt]
17325      attributes [opt] direct-abstract-declarator
17326
17327    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17328    detect constructor, destructor or conversion operators. It is set
17329    to -1 if the declarator is a name, and +1 if it is a
17330    function. Otherwise it is set to zero. Usually you just want to
17331    test for >0, but internally the negative value is used.
17332
17333    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17334    a decl-specifier-seq unless it declares a constructor, destructor,
17335    or conversion.  It might seem that we could check this condition in
17336    semantic analysis, rather than parsing, but that makes it difficult
17337    to handle something like `f()'.  We want to notice that there are
17338    no decl-specifiers, and therefore realize that this is an
17339    expression, not a declaration.)
17340
17341    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17342    the declarator is a direct-declarator of the form "(...)".
17343
17344    MEMBER_P is true iff this declarator is a member-declarator.
17345
17346    FRIEND_P is true iff this declarator is a friend.  */
17347
17348 static cp_declarator *
17349 cp_parser_declarator (cp_parser* parser,
17350                       cp_parser_declarator_kind dcl_kind,
17351                       int* ctor_dtor_or_conv_p,
17352                       bool* parenthesized_p,
17353                       bool member_p, bool friend_p)
17354 {
17355   cp_declarator *declarator;
17356   enum tree_code code;
17357   cp_cv_quals cv_quals;
17358   tree class_type;
17359   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17360
17361   /* Assume this is not a constructor, destructor, or type-conversion
17362      operator.  */
17363   if (ctor_dtor_or_conv_p)
17364     *ctor_dtor_or_conv_p = 0;
17365
17366   if (cp_parser_allow_gnu_extensions_p (parser))
17367     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17368
17369   /* Check for the ptr-operator production.  */
17370   cp_parser_parse_tentatively (parser);
17371   /* Parse the ptr-operator.  */
17372   code = cp_parser_ptr_operator (parser,
17373                                  &class_type,
17374                                  &cv_quals,
17375                                  &std_attributes);
17376
17377   /* If that worked, then we have a ptr-operator.  */
17378   if (cp_parser_parse_definitely (parser))
17379     {
17380       /* If a ptr-operator was found, then this declarator was not
17381          parenthesized.  */
17382       if (parenthesized_p)
17383         *parenthesized_p = true;
17384       /* The dependent declarator is optional if we are parsing an
17385          abstract-declarator.  */
17386       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17387         cp_parser_parse_tentatively (parser);
17388
17389       /* Parse the dependent declarator.  */
17390       declarator = cp_parser_declarator (parser, dcl_kind,
17391                                          /*ctor_dtor_or_conv_p=*/NULL,
17392                                          /*parenthesized_p=*/NULL,
17393                                          /*member_p=*/false,
17394                                          friend_p);
17395
17396       /* If we are parsing an abstract-declarator, we must handle the
17397          case where the dependent declarator is absent.  */
17398       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17399           && !cp_parser_parse_definitely (parser))
17400         declarator = NULL;
17401
17402       declarator = cp_parser_make_indirect_declarator
17403         (code, class_type, cv_quals, declarator, std_attributes);
17404     }
17405   /* Everything else is a direct-declarator.  */
17406   else
17407     {
17408       if (parenthesized_p)
17409         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17410                                                    CPP_OPEN_PAREN);
17411       declarator = cp_parser_direct_declarator (parser, dcl_kind,
17412                                                 ctor_dtor_or_conv_p,
17413                                                 member_p, friend_p);
17414     }
17415
17416   if (gnu_attributes && declarator && declarator != cp_error_declarator)
17417     declarator->attributes = gnu_attributes;
17418   return declarator;
17419 }
17420
17421 /* Parse a direct-declarator or direct-abstract-declarator.
17422
17423    direct-declarator:
17424      declarator-id
17425      direct-declarator ( parameter-declaration-clause )
17426        cv-qualifier-seq [opt]
17427        ref-qualifier [opt]
17428        exception-specification [opt]
17429      direct-declarator [ constant-expression [opt] ]
17430      ( declarator )
17431
17432    direct-abstract-declarator:
17433      direct-abstract-declarator [opt]
17434        ( parameter-declaration-clause )
17435        cv-qualifier-seq [opt]
17436        ref-qualifier [opt]
17437        exception-specification [opt]
17438      direct-abstract-declarator [opt] [ constant-expression [opt] ]
17439      ( abstract-declarator )
17440
17441    Returns a representation of the declarator.  DCL_KIND is
17442    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17443    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
17444    we are parsing a direct-declarator.  It is
17445    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17446    of ambiguity we prefer an abstract declarator, as per
17447    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17448    as for cp_parser_declarator.  */
17449
17450 static cp_declarator *
17451 cp_parser_direct_declarator (cp_parser* parser,
17452                              cp_parser_declarator_kind dcl_kind,
17453                              int* ctor_dtor_or_conv_p,
17454                              bool member_p, bool friend_p)
17455 {
17456   cp_token *token;
17457   cp_declarator *declarator = NULL;
17458   tree scope = NULL_TREE;
17459   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17460   bool saved_in_declarator_p = parser->in_declarator_p;
17461   bool first = true;
17462   tree pushed_scope = NULL_TREE;
17463
17464   while (true)
17465     {
17466       /* Peek at the next token.  */
17467       token = cp_lexer_peek_token (parser->lexer);
17468       if (token->type == CPP_OPEN_PAREN)
17469         {
17470           /* This is either a parameter-declaration-clause, or a
17471              parenthesized declarator. When we know we are parsing a
17472              named declarator, it must be a parenthesized declarator
17473              if FIRST is true. For instance, `(int)' is a
17474              parameter-declaration-clause, with an omitted
17475              direct-abstract-declarator. But `((*))', is a
17476              parenthesized abstract declarator. Finally, when T is a
17477              template parameter `(T)' is a
17478              parameter-declaration-clause, and not a parenthesized
17479              named declarator.
17480
17481              We first try and parse a parameter-declaration-clause,
17482              and then try a nested declarator (if FIRST is true).
17483
17484              It is not an error for it not to be a
17485              parameter-declaration-clause, even when FIRST is
17486              false. Consider,
17487
17488                int i (int);
17489                int i (3);
17490
17491              The first is the declaration of a function while the
17492              second is the definition of a variable, including its
17493              initializer.
17494
17495              Having seen only the parenthesis, we cannot know which of
17496              these two alternatives should be selected.  Even more
17497              complex are examples like:
17498
17499                int i (int (a));
17500                int i (int (3));
17501
17502              The former is a function-declaration; the latter is a
17503              variable initialization.
17504
17505              Thus again, we try a parameter-declaration-clause, and if
17506              that fails, we back out and return.  */
17507
17508           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17509             {
17510               tree params;
17511               bool is_declarator = false;
17512
17513               /* In a member-declarator, the only valid interpretation
17514                  of a parenthesis is the start of a
17515                  parameter-declaration-clause.  (It is invalid to
17516                  initialize a static data member with a parenthesized
17517                  initializer; only the "=" form of initialization is
17518                  permitted.)  */
17519               if (!member_p)
17520                 cp_parser_parse_tentatively (parser);
17521
17522               /* Consume the `('.  */
17523               cp_lexer_consume_token (parser->lexer);
17524               if (first)
17525                 {
17526                   /* If this is going to be an abstract declarator, we're
17527                      in a declarator and we can't have default args.  */
17528                   parser->default_arg_ok_p = false;
17529                   parser->in_declarator_p = true;
17530                 }
17531
17532               begin_scope (sk_function_parms, NULL_TREE);
17533
17534               /* Parse the parameter-declaration-clause.  */
17535               params = cp_parser_parameter_declaration_clause (parser);
17536
17537               /* Consume the `)'.  */
17538               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17539
17540               /* If all went well, parse the cv-qualifier-seq,
17541                  ref-qualifier and the exception-specification.  */
17542               if (member_p || cp_parser_parse_definitely (parser))
17543                 {
17544                   cp_cv_quals cv_quals;
17545                   cp_virt_specifiers virt_specifiers;
17546                   cp_ref_qualifier ref_qual;
17547                   tree exception_specification;
17548                   tree late_return;
17549                   tree attrs;
17550                   bool memfn = (member_p || (pushed_scope
17551                                              && CLASS_TYPE_P (pushed_scope)));
17552
17553                   is_declarator = true;
17554
17555                   if (ctor_dtor_or_conv_p)
17556                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17557                   first = false;
17558
17559                   /* Parse the cv-qualifier-seq.  */
17560                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17561                   /* Parse the ref-qualifier. */
17562                   ref_qual = cp_parser_ref_qualifier_opt (parser);
17563                   /* And the exception-specification.  */
17564                   exception_specification
17565                     = cp_parser_exception_specification_opt (parser);
17566
17567                   attrs = cp_parser_std_attribute_spec_seq (parser);
17568
17569                   /* In here, we handle cases where attribute is used after
17570                      the function declaration.  For example:
17571                      void func (int x) __attribute__((vector(..)));  */
17572                   if (flag_cilkplus
17573                       && cp_next_tokens_can_be_gnu_attribute_p (parser))
17574                     {
17575                       cp_parser_parse_tentatively (parser);
17576                       tree attr = cp_parser_gnu_attributes_opt (parser);
17577                       if (cp_lexer_next_token_is_not (parser->lexer,
17578                                                       CPP_SEMICOLON)
17579                           && cp_lexer_next_token_is_not (parser->lexer,
17580                                                          CPP_OPEN_BRACE))
17581                         cp_parser_abort_tentative_parse (parser);
17582                       else if (!cp_parser_parse_definitely (parser))
17583                         ;
17584                       else
17585                         attrs = chainon (attr, attrs);
17586                     }
17587                   late_return = (cp_parser_late_return_type_opt
17588                                  (parser, declarator,
17589                                   memfn ? cv_quals : -1));
17590
17591
17592                   /* Parse the virt-specifier-seq.  */
17593                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17594
17595                   /* Create the function-declarator.  */
17596                   declarator = make_call_declarator (declarator,
17597                                                      params,
17598                                                      cv_quals,
17599                                                      virt_specifiers,
17600                                                      ref_qual,
17601                                                      exception_specification,
17602                                                      late_return);
17603                   declarator->std_attributes = attrs;
17604                   /* Any subsequent parameter lists are to do with
17605                      return type, so are not those of the declared
17606                      function.  */
17607                   parser->default_arg_ok_p = false;
17608                 }
17609
17610               /* Remove the function parms from scope.  */
17611               pop_bindings_and_leave_scope ();
17612
17613               if (is_declarator)
17614                 /* Repeat the main loop.  */
17615                 continue;
17616             }
17617
17618           /* If this is the first, we can try a parenthesized
17619              declarator.  */
17620           if (first)
17621             {
17622               bool saved_in_type_id_in_expr_p;
17623
17624               parser->default_arg_ok_p = saved_default_arg_ok_p;
17625               parser->in_declarator_p = saved_in_declarator_p;
17626
17627               /* Consume the `('.  */
17628               cp_lexer_consume_token (parser->lexer);
17629               /* Parse the nested declarator.  */
17630               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17631               parser->in_type_id_in_expr_p = true;
17632               declarator
17633                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17634                                         /*parenthesized_p=*/NULL,
17635                                         member_p, friend_p);
17636               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17637               first = false;
17638               /* Expect a `)'.  */
17639               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17640                 declarator = cp_error_declarator;
17641               if (declarator == cp_error_declarator)
17642                 break;
17643
17644               goto handle_declarator;
17645             }
17646           /* Otherwise, we must be done.  */
17647           else
17648             break;
17649         }
17650       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17651                && token->type == CPP_OPEN_SQUARE
17652                && !cp_next_tokens_can_be_attribute_p (parser))
17653         {
17654           /* Parse an array-declarator.  */
17655           tree bounds, attrs;
17656
17657           if (ctor_dtor_or_conv_p)
17658             *ctor_dtor_or_conv_p = 0;
17659
17660           first = false;
17661           parser->default_arg_ok_p = false;
17662           parser->in_declarator_p = true;
17663           /* Consume the `['.  */
17664           cp_lexer_consume_token (parser->lexer);
17665           /* Peek at the next token.  */
17666           token = cp_lexer_peek_token (parser->lexer);
17667           /* If the next token is `]', then there is no
17668              constant-expression.  */
17669           if (token->type != CPP_CLOSE_SQUARE)
17670             {
17671               bool non_constant_p;
17672               bounds
17673                 = cp_parser_constant_expression (parser,
17674                                                  /*allow_non_constant=*/true,
17675                                                  &non_constant_p);
17676               if (!non_constant_p)
17677                 /* OK */;
17678               else if (error_operand_p (bounds))
17679                 /* Already gave an error.  */;
17680               else if (!parser->in_function_body
17681                        || current_binding_level->kind == sk_function_parms)
17682                 {
17683                   /* Normally, the array bound must be an integral constant
17684                      expression.  However, as an extension, we allow VLAs
17685                      in function scopes as long as they aren't part of a
17686                      parameter declaration.  */
17687                   cp_parser_error (parser,
17688                                    "array bound is not an integer constant");
17689                   bounds = error_mark_node;
17690                 }
17691               else if (processing_template_decl
17692                        && !type_dependent_expression_p (bounds))
17693                 {
17694                   /* Remember this wasn't a constant-expression.  */
17695                   bounds = build_nop (TREE_TYPE (bounds), bounds);
17696                   TREE_SIDE_EFFECTS (bounds) = 1;
17697                 }
17698             }
17699           else
17700             bounds = NULL_TREE;
17701           /* Look for the closing `]'.  */
17702           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17703             {
17704               declarator = cp_error_declarator;
17705               break;
17706             }
17707
17708           attrs = cp_parser_std_attribute_spec_seq (parser);
17709           declarator = make_array_declarator (declarator, bounds);
17710           declarator->std_attributes = attrs;
17711         }
17712       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17713         {
17714           {
17715             tree qualifying_scope;
17716             tree unqualified_name;
17717             tree attrs;
17718             special_function_kind sfk;
17719             bool abstract_ok;
17720             bool pack_expansion_p = false;
17721             cp_token *declarator_id_start_token;
17722
17723             /* Parse a declarator-id */
17724             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17725             if (abstract_ok)
17726               {
17727                 cp_parser_parse_tentatively (parser);
17728
17729                 /* If we see an ellipsis, we should be looking at a
17730                    parameter pack. */
17731                 if (token->type == CPP_ELLIPSIS)
17732                   {
17733                     /* Consume the `...' */
17734                     cp_lexer_consume_token (parser->lexer);
17735
17736                     pack_expansion_p = true;
17737                   }
17738               }
17739
17740             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17741             unqualified_name
17742               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17743             qualifying_scope = parser->scope;
17744             if (abstract_ok)
17745               {
17746                 bool okay = false;
17747
17748                 if (!unqualified_name && pack_expansion_p)
17749                   {
17750                     /* Check whether an error occurred. */
17751                     okay = !cp_parser_error_occurred (parser);
17752
17753                     /* We already consumed the ellipsis to mark a
17754                        parameter pack, but we have no way to report it,
17755                        so abort the tentative parse. We will be exiting
17756                        immediately anyway. */
17757                     cp_parser_abort_tentative_parse (parser);
17758                   }
17759                 else
17760                   okay = cp_parser_parse_definitely (parser);
17761
17762                 if (!okay)
17763                   unqualified_name = error_mark_node;
17764                 else if (unqualified_name
17765                          && (qualifying_scope
17766                              || (!identifier_p (unqualified_name))))
17767                   {
17768                     cp_parser_error (parser, "expected unqualified-id");
17769                     unqualified_name = error_mark_node;
17770                   }
17771               }
17772
17773             if (!unqualified_name)
17774               return NULL;
17775             if (unqualified_name == error_mark_node)
17776               {
17777                 declarator = cp_error_declarator;
17778                 pack_expansion_p = false;
17779                 declarator->parameter_pack_p = false;
17780                 break;
17781               }
17782
17783             attrs = cp_parser_std_attribute_spec_seq (parser);
17784
17785             if (qualifying_scope && at_namespace_scope_p ()
17786                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17787               {
17788                 /* In the declaration of a member of a template class
17789                    outside of the class itself, the SCOPE will sometimes
17790                    be a TYPENAME_TYPE.  For example, given:
17791
17792                    template <typename T>
17793                    int S<T>::R::i = 3;
17794
17795                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
17796                    this context, we must resolve S<T>::R to an ordinary
17797                    type, rather than a typename type.
17798
17799                    The reason we normally avoid resolving TYPENAME_TYPEs
17800                    is that a specialization of `S' might render
17801                    `S<T>::R' not a type.  However, if `S' is
17802                    specialized, then this `i' will not be used, so there
17803                    is no harm in resolving the types here.  */
17804                 tree type;
17805
17806                 /* Resolve the TYPENAME_TYPE.  */
17807                 type = resolve_typename_type (qualifying_scope,
17808                                               /*only_current_p=*/false);
17809                 /* If that failed, the declarator is invalid.  */
17810                 if (TREE_CODE (type) == TYPENAME_TYPE)
17811                   {
17812                     if (typedef_variant_p (type))
17813                       error_at (declarator_id_start_token->location,
17814                                 "cannot define member of dependent typedef "
17815                                 "%qT", type);
17816                     else
17817                       error_at (declarator_id_start_token->location,
17818                                 "%<%T::%E%> is not a type",
17819                                 TYPE_CONTEXT (qualifying_scope),
17820                                 TYPE_IDENTIFIER (qualifying_scope));
17821                   }
17822                 qualifying_scope = type;
17823               }
17824
17825             sfk = sfk_none;
17826
17827             if (unqualified_name)
17828               {
17829                 tree class_type;
17830
17831                 if (qualifying_scope
17832                     && CLASS_TYPE_P (qualifying_scope))
17833                   class_type = qualifying_scope;
17834                 else
17835                   class_type = current_class_type;
17836
17837                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17838                   {
17839                     tree name_type = TREE_TYPE (unqualified_name);
17840                     if (class_type && same_type_p (name_type, class_type))
17841                       {
17842                         if (qualifying_scope
17843                             && CLASSTYPE_USE_TEMPLATE (name_type))
17844                           {
17845                             error_at (declarator_id_start_token->location,
17846                                       "invalid use of constructor as a template");
17847                             inform (declarator_id_start_token->location,
17848                                     "use %<%T::%D%> instead of %<%T::%D%> to "
17849                                     "name the constructor in a qualified name",
17850                                     class_type,
17851                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17852                                     class_type, name_type);
17853                             declarator = cp_error_declarator;
17854                             break;
17855                           }
17856                         else
17857                           unqualified_name = constructor_name (class_type);
17858                       }
17859                     else
17860                       {
17861                         /* We do not attempt to print the declarator
17862                            here because we do not have enough
17863                            information about its original syntactic
17864                            form.  */
17865                         cp_parser_error (parser, "invalid declarator");
17866                         declarator = cp_error_declarator;
17867                         break;
17868                       }
17869                   }
17870
17871                 if (class_type)
17872                   {
17873                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17874                       sfk = sfk_destructor;
17875                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17876                       sfk = sfk_conversion;
17877                     else if (/* There's no way to declare a constructor
17878                                 for an anonymous type, even if the type
17879                                 got a name for linkage purposes.  */
17880                              !TYPE_WAS_ANONYMOUS (class_type)
17881                              /* Handle correctly (c++/19200):
17882
17883                                 struct S {
17884                                   struct T{};
17885                                   friend void S(T);
17886                                 };
17887
17888                                 and also:
17889
17890                                 namespace N {
17891                                   void S();
17892                                 }
17893
17894                                 struct S {
17895                                   friend void N::S();
17896                                 };  */
17897                              && !(friend_p
17898                                   && class_type != qualifying_scope)
17899                              && constructor_name_p (unqualified_name,
17900                                                     class_type))
17901                       {
17902                         unqualified_name = constructor_name (class_type);
17903                         sfk = sfk_constructor;
17904                       }
17905                     else if (is_overloaded_fn (unqualified_name)
17906                              && DECL_CONSTRUCTOR_P (get_first_fn
17907                                                     (unqualified_name)))
17908                       sfk = sfk_constructor;
17909
17910                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
17911                       *ctor_dtor_or_conv_p = -1;
17912                   }
17913               }
17914             declarator = make_id_declarator (qualifying_scope,
17915                                              unqualified_name,
17916                                              sfk);
17917             declarator->std_attributes = attrs;
17918             declarator->id_loc = token->location;
17919             declarator->parameter_pack_p = pack_expansion_p;
17920
17921             if (pack_expansion_p)
17922               maybe_warn_variadic_templates ();
17923           }
17924
17925         handle_declarator:;
17926           scope = get_scope_of_declarator (declarator);
17927           if (scope)
17928             {
17929               /* Any names that appear after the declarator-id for a
17930                  member are looked up in the containing scope.  */
17931               if (at_function_scope_p ())
17932                 {
17933                   /* But declarations with qualified-ids can't appear in a
17934                      function.  */
17935                   cp_parser_error (parser, "qualified-id in declaration");
17936                   declarator = cp_error_declarator;
17937                   break;
17938                 }
17939               pushed_scope = push_scope (scope);
17940             }
17941           parser->in_declarator_p = true;
17942           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17943               || (declarator && declarator->kind == cdk_id))
17944             /* Default args are only allowed on function
17945                declarations.  */
17946             parser->default_arg_ok_p = saved_default_arg_ok_p;
17947           else
17948             parser->default_arg_ok_p = false;
17949
17950           first = false;
17951         }
17952       /* We're done.  */
17953       else
17954         break;
17955     }
17956
17957   /* For an abstract declarator, we might wind up with nothing at this
17958      point.  That's an error; the declarator is not optional.  */
17959   if (!declarator)
17960     cp_parser_error (parser, "expected declarator");
17961
17962   /* If we entered a scope, we must exit it now.  */
17963   if (pushed_scope)
17964     pop_scope (pushed_scope);
17965
17966   parser->default_arg_ok_p = saved_default_arg_ok_p;
17967   parser->in_declarator_p = saved_in_declarator_p;
17968
17969   return declarator;
17970 }
17971
17972 /* Parse a ptr-operator.
17973
17974    ptr-operator:
17975      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17976      * cv-qualifier-seq [opt]
17977      &
17978      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17979      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17980
17981    GNU Extension:
17982
17983    ptr-operator:
17984      & cv-qualifier-seq [opt]
17985
17986    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17987    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17988    an rvalue reference. In the case of a pointer-to-member, *TYPE is
17989    filled in with the TYPE containing the member.  *CV_QUALS is
17990    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17991    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
17992    Note that the tree codes returned by this function have nothing
17993    to do with the types of trees that will be eventually be created
17994    to represent the pointer or reference type being parsed. They are
17995    just constants with suggestive names. */
17996 static enum tree_code
17997 cp_parser_ptr_operator (cp_parser* parser,
17998                         tree* type,
17999                         cp_cv_quals *cv_quals,
18000                         tree *attributes)
18001 {
18002   enum tree_code code = ERROR_MARK;
18003   cp_token *token;
18004   tree attrs = NULL_TREE;
18005
18006   /* Assume that it's not a pointer-to-member.  */
18007   *type = NULL_TREE;
18008   /* And that there are no cv-qualifiers.  */
18009   *cv_quals = TYPE_UNQUALIFIED;
18010
18011   /* Peek at the next token.  */
18012   token = cp_lexer_peek_token (parser->lexer);
18013
18014   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
18015   if (token->type == CPP_MULT)
18016     code = INDIRECT_REF;
18017   else if (token->type == CPP_AND)
18018     code = ADDR_EXPR;
18019   else if ((cxx_dialect != cxx98) &&
18020            token->type == CPP_AND_AND) /* C++0x only */
18021     code = NON_LVALUE_EXPR;
18022
18023   if (code != ERROR_MARK)
18024     {
18025       /* Consume the `*', `&' or `&&'.  */
18026       cp_lexer_consume_token (parser->lexer);
18027
18028       /* A `*' can be followed by a cv-qualifier-seq, and so can a
18029          `&', if we are allowing GNU extensions.  (The only qualifier
18030          that can legally appear after `&' is `restrict', but that is
18031          enforced during semantic analysis.  */
18032       if (code == INDIRECT_REF
18033           || cp_parser_allow_gnu_extensions_p (parser))
18034         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18035
18036       attrs = cp_parser_std_attribute_spec_seq (parser);
18037       if (attributes != NULL)
18038         *attributes = attrs;
18039     }
18040   else
18041     {
18042       /* Try the pointer-to-member case.  */
18043       cp_parser_parse_tentatively (parser);
18044       /* Look for the optional `::' operator.  */
18045       cp_parser_global_scope_opt (parser,
18046                                   /*current_scope_valid_p=*/false);
18047       /* Look for the nested-name specifier.  */
18048       token = cp_lexer_peek_token (parser->lexer);
18049       cp_parser_nested_name_specifier (parser,
18050                                        /*typename_keyword_p=*/false,
18051                                        /*check_dependency_p=*/true,
18052                                        /*type_p=*/false,
18053                                        /*is_declaration=*/false);
18054       /* If we found it, and the next token is a `*', then we are
18055          indeed looking at a pointer-to-member operator.  */
18056       if (!cp_parser_error_occurred (parser)
18057           && cp_parser_require (parser, CPP_MULT, RT_MULT))
18058         {
18059           /* Indicate that the `*' operator was used.  */
18060           code = INDIRECT_REF;
18061
18062           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18063             error_at (token->location, "%qD is a namespace", parser->scope);
18064           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18065             error_at (token->location, "cannot form pointer to member of "
18066                       "non-class %q#T", parser->scope);
18067           else
18068             {
18069               /* The type of which the member is a member is given by the
18070                  current SCOPE.  */
18071               *type = parser->scope;
18072               /* The next name will not be qualified.  */
18073               parser->scope = NULL_TREE;
18074               parser->qualifying_scope = NULL_TREE;
18075               parser->object_scope = NULL_TREE;
18076               /* Look for optional c++11 attributes.  */
18077               attrs = cp_parser_std_attribute_spec_seq (parser);
18078               if (attributes != NULL)
18079                 *attributes = attrs;
18080               /* Look for the optional cv-qualifier-seq.  */
18081               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18082             }
18083         }
18084       /* If that didn't work we don't have a ptr-operator.  */
18085       if (!cp_parser_parse_definitely (parser))
18086         cp_parser_error (parser, "expected ptr-operator");
18087     }
18088
18089   return code;
18090 }
18091
18092 /* Parse an (optional) cv-qualifier-seq.
18093
18094    cv-qualifier-seq:
18095      cv-qualifier cv-qualifier-seq [opt]
18096
18097    cv-qualifier:
18098      const
18099      volatile
18100
18101    GNU Extension:
18102
18103    cv-qualifier:
18104      __restrict__
18105
18106    Returns a bitmask representing the cv-qualifiers.  */
18107
18108 static cp_cv_quals
18109 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18110 {
18111   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18112
18113   while (true)
18114     {
18115       cp_token *token;
18116       cp_cv_quals cv_qualifier;
18117
18118       /* Peek at the next token.  */
18119       token = cp_lexer_peek_token (parser->lexer);
18120       /* See if it's a cv-qualifier.  */
18121       switch (token->keyword)
18122         {
18123         case RID_CONST:
18124           cv_qualifier = TYPE_QUAL_CONST;
18125           break;
18126
18127         case RID_VOLATILE:
18128           cv_qualifier = TYPE_QUAL_VOLATILE;
18129           break;
18130
18131         case RID_RESTRICT:
18132           cv_qualifier = TYPE_QUAL_RESTRICT;
18133           break;
18134
18135         default:
18136           cv_qualifier = TYPE_UNQUALIFIED;
18137           break;
18138         }
18139
18140       if (!cv_qualifier)
18141         break;
18142
18143       if (cv_quals & cv_qualifier)
18144         {
18145           error_at (token->location, "duplicate cv-qualifier");
18146           cp_lexer_purge_token (parser->lexer);
18147         }
18148       else
18149         {
18150           cp_lexer_consume_token (parser->lexer);
18151           cv_quals |= cv_qualifier;
18152         }
18153     }
18154
18155   return cv_quals;
18156 }
18157
18158 /* Parse an (optional) ref-qualifier
18159
18160    ref-qualifier:
18161      &
18162      &&
18163
18164    Returns cp_ref_qualifier representing ref-qualifier. */
18165
18166 static cp_ref_qualifier
18167 cp_parser_ref_qualifier_opt (cp_parser* parser)
18168 {
18169   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18170
18171   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
18172   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18173     return ref_qual;
18174
18175   while (true)
18176     {
18177       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18178       cp_token *token = cp_lexer_peek_token (parser->lexer);
18179
18180       switch (token->type)
18181         {
18182         case CPP_AND:
18183           curr_ref_qual = REF_QUAL_LVALUE;
18184           break;
18185
18186         case CPP_AND_AND:
18187           curr_ref_qual = REF_QUAL_RVALUE;
18188           break;
18189
18190         default:
18191           curr_ref_qual = REF_QUAL_NONE;
18192           break;
18193         }
18194
18195       if (!curr_ref_qual)
18196         break;
18197       else if (ref_qual)
18198         {
18199           error_at (token->location, "multiple ref-qualifiers");
18200           cp_lexer_purge_token (parser->lexer);
18201         }
18202       else
18203         {
18204           ref_qual = curr_ref_qual;
18205           cp_lexer_consume_token (parser->lexer);
18206         }
18207     }
18208
18209   return ref_qual;
18210 }
18211
18212 /* Parse an (optional) virt-specifier-seq.
18213
18214    virt-specifier-seq:
18215      virt-specifier virt-specifier-seq [opt]
18216
18217    virt-specifier:
18218      override
18219      final
18220
18221    Returns a bitmask representing the virt-specifiers.  */
18222
18223 static cp_virt_specifiers
18224 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18225 {
18226   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18227
18228   while (true)
18229     {
18230       cp_token *token;
18231       cp_virt_specifiers virt_specifier;
18232
18233       /* Peek at the next token.  */
18234       token = cp_lexer_peek_token (parser->lexer);
18235       /* See if it's a virt-specifier-qualifier.  */
18236       if (token->type != CPP_NAME)
18237         break;
18238       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18239         {
18240           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18241           virt_specifier = VIRT_SPEC_OVERRIDE;
18242         }
18243       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18244         {
18245           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18246           virt_specifier = VIRT_SPEC_FINAL;
18247         }
18248       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18249         {
18250           virt_specifier = VIRT_SPEC_FINAL;
18251         }
18252       else
18253         break;
18254
18255       if (virt_specifiers & virt_specifier)
18256         {
18257           error_at (token->location, "duplicate virt-specifier");
18258           cp_lexer_purge_token (parser->lexer);
18259         }
18260       else
18261         {
18262           cp_lexer_consume_token (parser->lexer);
18263           virt_specifiers |= virt_specifier;
18264         }
18265     }
18266   return virt_specifiers;
18267 }
18268
18269 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18270    is in scope even though it isn't real.  */
18271
18272 void
18273 inject_this_parameter (tree ctype, cp_cv_quals quals)
18274 {
18275   tree this_parm;
18276
18277   if (current_class_ptr)
18278     {
18279       /* We don't clear this between NSDMIs.  Is it already what we want?  */
18280       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18281       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18282           && cp_type_quals (type) == quals)
18283         return;
18284     }
18285
18286   this_parm = build_this_parm (ctype, quals);
18287   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
18288   current_class_ptr = NULL_TREE;
18289   current_class_ref
18290     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18291   current_class_ptr = this_parm;
18292 }
18293
18294 /* Return true iff our current scope is a non-static data member
18295    initializer.  */
18296
18297 bool
18298 parsing_nsdmi (void)
18299 {
18300   /* We recognize NSDMI context by the context-less 'this' pointer set up
18301      by the function above.  */
18302   if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18303     return true;
18304   return false;
18305 }
18306
18307 /* Parse a late-specified return type, if any.  This is not a separate
18308    non-terminal, but part of a function declarator, which looks like
18309
18310    -> trailing-type-specifier-seq abstract-declarator(opt)
18311
18312    Returns the type indicated by the type-id.
18313
18314    In addition to this this parses any queued up omp declare simd
18315    clauses and Cilk Plus SIMD-enabled function's vector attributes.
18316
18317    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18318    function.  */
18319
18320 static tree
18321 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18322                                 cp_cv_quals quals)
18323 {
18324   cp_token *token;
18325   tree type = NULL_TREE;
18326   bool declare_simd_p = (parser->omp_declare_simd
18327                          && declarator
18328                          && declarator->kind == cdk_id);
18329
18330   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info 
18331                                 && declarator && declarator->kind == cdk_id);
18332   
18333   /* Peek at the next token.  */
18334   token = cp_lexer_peek_token (parser->lexer);
18335   /* A late-specified return type is indicated by an initial '->'. */
18336   if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18337     return NULL_TREE;
18338
18339   tree save_ccp = current_class_ptr;
18340   tree save_ccr = current_class_ref;
18341   if (quals >= 0)
18342     {
18343       /* DR 1207: 'this' is in scope in the trailing return type.  */
18344       inject_this_parameter (current_class_type, quals);
18345     }
18346
18347   if (token->type == CPP_DEREF)
18348     {
18349       /* Consume the ->.  */
18350       cp_lexer_consume_token (parser->lexer);
18351
18352       type = cp_parser_trailing_type_id (parser);
18353     }
18354
18355   if (cilk_simd_fn_vector_p)
18356     declarator->std_attributes
18357       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18358                                                   declarator->std_attributes);
18359   if (declare_simd_p)
18360     declarator->std_attributes
18361       = cp_parser_late_parsing_omp_declare_simd (parser,
18362                                                  declarator->std_attributes);
18363
18364   if (quals >= 0)
18365     {
18366       current_class_ptr = save_ccp;
18367       current_class_ref = save_ccr;
18368     }
18369
18370   return type;
18371 }
18372
18373 /* Parse a declarator-id.
18374
18375    declarator-id:
18376      id-expression
18377      :: [opt] nested-name-specifier [opt] type-name
18378
18379    In the `id-expression' case, the value returned is as for
18380    cp_parser_id_expression if the id-expression was an unqualified-id.
18381    If the id-expression was a qualified-id, then a SCOPE_REF is
18382    returned.  The first operand is the scope (either a NAMESPACE_DECL
18383    or TREE_TYPE), but the second is still just a representation of an
18384    unqualified-id.  */
18385
18386 static tree
18387 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18388 {
18389   tree id;
18390   /* The expression must be an id-expression.  Assume that qualified
18391      names are the names of types so that:
18392
18393        template <class T>
18394        int S<T>::R::i = 3;
18395
18396      will work; we must treat `S<T>::R' as the name of a type.
18397      Similarly, assume that qualified names are templates, where
18398      required, so that:
18399
18400        template <class T>
18401        int S<T>::R<T>::i = 3;
18402
18403      will work, too.  */
18404   id = cp_parser_id_expression (parser,
18405                                 /*template_keyword_p=*/false,
18406                                 /*check_dependency_p=*/false,
18407                                 /*template_p=*/NULL,
18408                                 /*declarator_p=*/true,
18409                                 optional_p);
18410   if (id && BASELINK_P (id))
18411     id = BASELINK_FUNCTIONS (id);
18412   return id;
18413 }
18414
18415 /* Parse a type-id.
18416
18417    type-id:
18418      type-specifier-seq abstract-declarator [opt]
18419
18420    Returns the TYPE specified.  */
18421
18422 static tree
18423 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18424                      bool is_trailing_return)
18425 {
18426   cp_decl_specifier_seq type_specifier_seq;
18427   cp_declarator *abstract_declarator;
18428
18429   /* Parse the type-specifier-seq.  */
18430   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18431                                 is_trailing_return,
18432                                 &type_specifier_seq);
18433   if (type_specifier_seq.type == error_mark_node)
18434     return error_mark_node;
18435
18436   /* There might or might not be an abstract declarator.  */
18437   cp_parser_parse_tentatively (parser);
18438   /* Look for the declarator.  */
18439   abstract_declarator
18440     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18441                             /*parenthesized_p=*/NULL,
18442                             /*member_p=*/false,
18443                             /*friend_p=*/false);
18444   /* Check to see if there really was a declarator.  */
18445   if (!cp_parser_parse_definitely (parser))
18446     abstract_declarator = NULL;
18447
18448   if (type_specifier_seq.type
18449       /* None of the valid uses of 'auto' in C++14 involve the type-id
18450          nonterminal, but it is valid in a trailing-return-type.  */
18451       && !(cxx_dialect >= cxx14 && is_trailing_return)
18452       && type_uses_auto (type_specifier_seq.type))
18453     {
18454       /* A type-id with type 'auto' is only ok if the abstract declarator
18455          is a function declarator with a late-specified return type.  */
18456       if (abstract_declarator
18457           && abstract_declarator->kind == cdk_function
18458           && abstract_declarator->u.function.late_return_type)
18459         /* OK */;
18460       else
18461         {
18462           error ("invalid use of %<auto%>");
18463           return error_mark_node;
18464         }
18465     }
18466   
18467   return groktypename (&type_specifier_seq, abstract_declarator,
18468                        is_template_arg);
18469 }
18470
18471 static tree cp_parser_type_id (cp_parser *parser)
18472 {
18473   return cp_parser_type_id_1 (parser, false, false);
18474 }
18475
18476 static tree cp_parser_template_type_arg (cp_parser *parser)
18477 {
18478   tree r;
18479   const char *saved_message = parser->type_definition_forbidden_message;
18480   parser->type_definition_forbidden_message
18481     = G_("types may not be defined in template arguments");
18482   r = cp_parser_type_id_1 (parser, true, false);
18483   parser->type_definition_forbidden_message = saved_message;
18484   if (cxx_dialect >= cxx14 && type_uses_auto (r))
18485     {
18486       error ("invalid use of %<auto%> in template argument");
18487       r = error_mark_node;
18488     }
18489   return r;
18490 }
18491
18492 static tree cp_parser_trailing_type_id (cp_parser *parser)
18493 {
18494   return cp_parser_type_id_1 (parser, false, true);
18495 }
18496
18497 /* Parse a type-specifier-seq.
18498
18499    type-specifier-seq:
18500      type-specifier type-specifier-seq [opt]
18501
18502    GNU extension:
18503
18504    type-specifier-seq:
18505      attributes type-specifier-seq [opt]
18506
18507    If IS_DECLARATION is true, we are at the start of a "condition" or
18508    exception-declaration, so we might be followed by a declarator-id.
18509
18510    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18511    i.e. we've just seen "->".
18512
18513    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
18514
18515 static void
18516 cp_parser_type_specifier_seq (cp_parser* parser,
18517                               bool is_declaration,
18518                               bool is_trailing_return,
18519                               cp_decl_specifier_seq *type_specifier_seq)
18520 {
18521   bool seen_type_specifier = false;
18522   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18523   cp_token *start_token = NULL;
18524
18525   /* Clear the TYPE_SPECIFIER_SEQ.  */
18526   clear_decl_specs (type_specifier_seq);
18527
18528   /* In the context of a trailing return type, enum E { } is an
18529      elaborated-type-specifier followed by a function-body, not an
18530      enum-specifier.  */
18531   if (is_trailing_return)
18532     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18533
18534   /* Parse the type-specifiers and attributes.  */
18535   while (true)
18536     {
18537       tree type_specifier;
18538       bool is_cv_qualifier;
18539
18540       /* Check for attributes first.  */
18541       if (cp_next_tokens_can_be_attribute_p (parser))
18542         {
18543           type_specifier_seq->attributes =
18544             chainon (type_specifier_seq->attributes,
18545                      cp_parser_attributes_opt (parser));
18546           continue;
18547         }
18548
18549       /* record the token of the beginning of the type specifier seq,
18550          for error reporting purposes*/
18551      if (!start_token)
18552        start_token = cp_lexer_peek_token (parser->lexer);
18553
18554       /* Look for the type-specifier.  */
18555       type_specifier = cp_parser_type_specifier (parser,
18556                                                  flags,
18557                                                  type_specifier_seq,
18558                                                  /*is_declaration=*/false,
18559                                                  NULL,
18560                                                  &is_cv_qualifier);
18561       if (!type_specifier)
18562         {
18563           /* If the first type-specifier could not be found, this is not a
18564              type-specifier-seq at all.  */
18565           if (!seen_type_specifier)
18566             {
18567               /* Set in_declarator_p to avoid skipping to the semicolon.  */
18568               int in_decl = parser->in_declarator_p;
18569               parser->in_declarator_p = true;
18570
18571               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18572                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18573                 cp_parser_error (parser, "expected type-specifier");
18574
18575               parser->in_declarator_p = in_decl;
18576
18577               type_specifier_seq->type = error_mark_node;
18578               return;
18579             }
18580           /* If subsequent type-specifiers could not be found, the
18581              type-specifier-seq is complete.  */
18582           break;
18583         }
18584
18585       seen_type_specifier = true;
18586       /* The standard says that a condition can be:
18587
18588             type-specifier-seq declarator = assignment-expression
18589
18590          However, given:
18591
18592            struct S {};
18593            if (int S = ...)
18594
18595          we should treat the "S" as a declarator, not as a
18596          type-specifier.  The standard doesn't say that explicitly for
18597          type-specifier-seq, but it does say that for
18598          decl-specifier-seq in an ordinary declaration.  Perhaps it
18599          would be clearer just to allow a decl-specifier-seq here, and
18600          then add a semantic restriction that if any decl-specifiers
18601          that are not type-specifiers appear, the program is invalid.  */
18602       if (is_declaration && !is_cv_qualifier)
18603         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18604     }
18605 }
18606
18607 /* Return whether the function currently being declared has an associated
18608    template parameter list.  */
18609
18610 static bool
18611 function_being_declared_is_template_p (cp_parser* parser)
18612 {
18613   if (!current_template_parms || processing_template_parmlist)
18614     return false;
18615
18616   if (parser->implicit_template_scope)
18617     return true;
18618
18619   if (at_class_scope_p ()
18620       && TYPE_BEING_DEFINED (current_class_type))
18621     return parser->num_template_parameter_lists != 0;
18622
18623   return ((int) parser->num_template_parameter_lists > template_class_depth
18624           (current_class_type));
18625 }
18626
18627 /* Parse a parameter-declaration-clause.
18628
18629    parameter-declaration-clause:
18630      parameter-declaration-list [opt] ... [opt]
18631      parameter-declaration-list , ...
18632
18633    Returns a representation for the parameter declarations.  A return
18634    value of NULL indicates a parameter-declaration-clause consisting
18635    only of an ellipsis.  */
18636
18637 static tree
18638 cp_parser_parameter_declaration_clause (cp_parser* parser)
18639 {
18640   tree parameters;
18641   cp_token *token;
18642   bool ellipsis_p;
18643   bool is_error;
18644
18645   struct cleanup {
18646     cp_parser* parser;
18647     int auto_is_implicit_function_template_parm_p;
18648     ~cleanup() {
18649       parser->auto_is_implicit_function_template_parm_p
18650         = auto_is_implicit_function_template_parm_p;
18651     }
18652   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18653
18654   (void) cleanup;
18655
18656   if (!processing_specialization
18657       && !processing_template_parmlist
18658       && !processing_explicit_instantiation)
18659     if (!current_function_decl
18660         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18661       parser->auto_is_implicit_function_template_parm_p = true;
18662
18663   /* Peek at the next token.  */
18664   token = cp_lexer_peek_token (parser->lexer);
18665   /* Check for trivial parameter-declaration-clauses.  */
18666   if (token->type == CPP_ELLIPSIS)
18667     {
18668       /* Consume the `...' token.  */
18669       cp_lexer_consume_token (parser->lexer);
18670       return NULL_TREE;
18671     }
18672   else if (token->type == CPP_CLOSE_PAREN)
18673     /* There are no parameters.  */
18674     {
18675 #ifndef NO_IMPLICIT_EXTERN_C
18676       if (in_system_header_at (input_location)
18677           && current_class_type == NULL
18678           && current_lang_name == lang_name_c)
18679         return NULL_TREE;
18680       else
18681 #endif
18682         return void_list_node;
18683     }
18684   /* Check for `(void)', too, which is a special case.  */
18685   else if (token->keyword == RID_VOID
18686            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18687                == CPP_CLOSE_PAREN))
18688     {
18689       /* Consume the `void' token.  */
18690       cp_lexer_consume_token (parser->lexer);
18691       /* There are no parameters.  */
18692       return void_list_node;
18693     }
18694
18695   /* Parse the parameter-declaration-list.  */
18696   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18697   /* If a parse error occurred while parsing the
18698      parameter-declaration-list, then the entire
18699      parameter-declaration-clause is erroneous.  */
18700   if (is_error)
18701     return NULL;
18702
18703   /* Peek at the next token.  */
18704   token = cp_lexer_peek_token (parser->lexer);
18705   /* If it's a `,', the clause should terminate with an ellipsis.  */
18706   if (token->type == CPP_COMMA)
18707     {
18708       /* Consume the `,'.  */
18709       cp_lexer_consume_token (parser->lexer);
18710       /* Expect an ellipsis.  */
18711       ellipsis_p
18712         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18713     }
18714   /* It might also be `...' if the optional trailing `,' was
18715      omitted.  */
18716   else if (token->type == CPP_ELLIPSIS)
18717     {
18718       /* Consume the `...' token.  */
18719       cp_lexer_consume_token (parser->lexer);
18720       /* And remember that we saw it.  */
18721       ellipsis_p = true;
18722     }
18723   else
18724     ellipsis_p = false;
18725
18726   /* Finish the parameter list.  */
18727   if (!ellipsis_p)
18728     parameters = chainon (parameters, void_list_node);
18729
18730   return parameters;
18731 }
18732
18733 /* Parse a parameter-declaration-list.
18734
18735    parameter-declaration-list:
18736      parameter-declaration
18737      parameter-declaration-list , parameter-declaration
18738
18739    Returns a representation of the parameter-declaration-list, as for
18740    cp_parser_parameter_declaration_clause.  However, the
18741    `void_list_node' is never appended to the list.  Upon return,
18742    *IS_ERROR will be true iff an error occurred.  */
18743
18744 static tree
18745 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18746 {
18747   tree parameters = NULL_TREE;
18748   tree *tail = &parameters;
18749   bool saved_in_unbraced_linkage_specification_p;
18750   int index = 0;
18751
18752   /* Assume all will go well.  */
18753   *is_error = false;
18754   /* The special considerations that apply to a function within an
18755      unbraced linkage specifications do not apply to the parameters
18756      to the function.  */
18757   saved_in_unbraced_linkage_specification_p
18758     = parser->in_unbraced_linkage_specification_p;
18759   parser->in_unbraced_linkage_specification_p = false;
18760
18761   /* Look for more parameters.  */
18762   while (true)
18763     {
18764       cp_parameter_declarator *parameter;
18765       tree decl = error_mark_node;
18766       bool parenthesized_p = false;
18767       int template_parm_idx = (function_being_declared_is_template_p (parser)?
18768                                TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18769                                                 (current_template_parms)) : 0);
18770
18771       /* Parse the parameter.  */
18772       parameter
18773         = cp_parser_parameter_declaration (parser,
18774                                            /*template_parm_p=*/false,
18775                                            &parenthesized_p);
18776
18777       /* We don't know yet if the enclosing context is deprecated, so wait
18778          and warn in grokparms if appropriate.  */
18779       deprecated_state = DEPRECATED_SUPPRESS;
18780
18781       if (parameter)
18782         {
18783           /* If a function parameter pack was specified and an implicit template
18784              parameter was introduced during cp_parser_parameter_declaration,
18785              change any implicit parameters introduced into packs.  */
18786           if (parser->implicit_template_parms
18787               && parameter->declarator
18788               && parameter->declarator->parameter_pack_p)
18789             {
18790               int latest_template_parm_idx = TREE_VEC_LENGTH
18791                 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18792
18793               if (latest_template_parm_idx != template_parm_idx)
18794                 parameter->decl_specifiers.type = convert_generic_types_to_packs
18795                   (parameter->decl_specifiers.type,
18796                    template_parm_idx, latest_template_parm_idx);
18797             }
18798
18799           decl = grokdeclarator (parameter->declarator,
18800                                  &parameter->decl_specifiers,
18801                                  PARM,
18802                                  parameter->default_argument != NULL_TREE,
18803                                  &parameter->decl_specifiers.attributes);
18804         }
18805
18806       deprecated_state = DEPRECATED_NORMAL;
18807
18808       /* If a parse error occurred parsing the parameter declaration,
18809          then the entire parameter-declaration-list is erroneous.  */
18810       if (decl == error_mark_node)
18811         {
18812           *is_error = true;
18813           parameters = error_mark_node;
18814           break;
18815         }
18816
18817       if (parameter->decl_specifiers.attributes)
18818         cplus_decl_attributes (&decl,
18819                                parameter->decl_specifiers.attributes,
18820                                0);
18821       if (DECL_NAME (decl))
18822         decl = pushdecl (decl);
18823
18824       if (decl != error_mark_node)
18825         {
18826           retrofit_lang_decl (decl);
18827           DECL_PARM_INDEX (decl) = ++index;
18828           DECL_PARM_LEVEL (decl) = function_parm_depth ();
18829         }
18830
18831       /* Add the new parameter to the list.  */
18832       *tail = build_tree_list (parameter->default_argument, decl);
18833       tail = &TREE_CHAIN (*tail);
18834
18835       /* Peek at the next token.  */
18836       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18837           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18838           /* These are for Objective-C++ */
18839           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18840           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18841         /* The parameter-declaration-list is complete.  */
18842         break;
18843       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18844         {
18845           cp_token *token;
18846
18847           /* Peek at the next token.  */
18848           token = cp_lexer_peek_nth_token (parser->lexer, 2);
18849           /* If it's an ellipsis, then the list is complete.  */
18850           if (token->type == CPP_ELLIPSIS)
18851             break;
18852           /* Otherwise, there must be more parameters.  Consume the
18853              `,'.  */
18854           cp_lexer_consume_token (parser->lexer);
18855           /* When parsing something like:
18856
18857                 int i(float f, double d)
18858
18859              we can tell after seeing the declaration for "f" that we
18860              are not looking at an initialization of a variable "i",
18861              but rather at the declaration of a function "i".
18862
18863              Due to the fact that the parsing of template arguments
18864              (as specified to a template-id) requires backtracking we
18865              cannot use this technique when inside a template argument
18866              list.  */
18867           if (!parser->in_template_argument_list_p
18868               && !parser->in_type_id_in_expr_p
18869               && cp_parser_uncommitted_to_tentative_parse_p (parser)
18870               /* However, a parameter-declaration of the form
18871                  "float(f)" (which is a valid declaration of a
18872                  parameter "f") can also be interpreted as an
18873                  expression (the conversion of "f" to "float").  */
18874               && !parenthesized_p)
18875             cp_parser_commit_to_tentative_parse (parser);
18876         }
18877       else
18878         {
18879           cp_parser_error (parser, "expected %<,%> or %<...%>");
18880           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18881             cp_parser_skip_to_closing_parenthesis (parser,
18882                                                    /*recovering=*/true,
18883                                                    /*or_comma=*/false,
18884                                                    /*consume_paren=*/false);
18885           break;
18886         }
18887     }
18888
18889   parser->in_unbraced_linkage_specification_p
18890     = saved_in_unbraced_linkage_specification_p;
18891
18892   /* Reset implicit_template_scope if we are about to leave the function
18893      parameter list that introduced it.  Note that for out-of-line member
18894      definitions, there will be one or more class scopes before we get to
18895      the template parameter scope.  */
18896
18897   if (cp_binding_level *its = parser->implicit_template_scope)
18898     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18899       {
18900         while (maybe_its->kind == sk_class)
18901           maybe_its = maybe_its->level_chain;
18902         if (maybe_its == its)
18903           {
18904             parser->implicit_template_parms = 0;
18905             parser->implicit_template_scope = 0;
18906           }
18907       }
18908
18909   return parameters;
18910 }
18911
18912 /* Parse a parameter declaration.
18913
18914    parameter-declaration:
18915      decl-specifier-seq ... [opt] declarator
18916      decl-specifier-seq declarator = assignment-expression
18917      decl-specifier-seq ... [opt] abstract-declarator [opt]
18918      decl-specifier-seq abstract-declarator [opt] = assignment-expression
18919
18920    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18921    declares a template parameter.  (In that case, a non-nested `>'
18922    token encountered during the parsing of the assignment-expression
18923    is not interpreted as a greater-than operator.)
18924
18925    Returns a representation of the parameter, or NULL if an error
18926    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18927    true iff the declarator is of the form "(p)".  */
18928
18929 static cp_parameter_declarator *
18930 cp_parser_parameter_declaration (cp_parser *parser,
18931                                  bool template_parm_p,
18932                                  bool *parenthesized_p)
18933 {
18934   int declares_class_or_enum;
18935   cp_decl_specifier_seq decl_specifiers;
18936   cp_declarator *declarator;
18937   tree default_argument;
18938   cp_token *token = NULL, *declarator_token_start = NULL;
18939   const char *saved_message;
18940
18941   /* In a template parameter, `>' is not an operator.
18942
18943      [temp.param]
18944
18945      When parsing a default template-argument for a non-type
18946      template-parameter, the first non-nested `>' is taken as the end
18947      of the template parameter-list rather than a greater-than
18948      operator.  */
18949
18950   /* Type definitions may not appear in parameter types.  */
18951   saved_message = parser->type_definition_forbidden_message;
18952   parser->type_definition_forbidden_message
18953     = G_("types may not be defined in parameter types");
18954
18955   /* Parse the declaration-specifiers.  */
18956   cp_parser_decl_specifier_seq (parser,
18957                                 CP_PARSER_FLAGS_NONE,
18958                                 &decl_specifiers,
18959                                 &declares_class_or_enum);
18960
18961   /* Complain about missing 'typename' or other invalid type names.  */
18962   if (!decl_specifiers.any_type_specifiers_p
18963       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18964     decl_specifiers.type = error_mark_node;
18965
18966   /* If an error occurred, there's no reason to attempt to parse the
18967      rest of the declaration.  */
18968   if (cp_parser_error_occurred (parser))
18969     {
18970       parser->type_definition_forbidden_message = saved_message;
18971       return NULL;
18972     }
18973
18974   /* Peek at the next token.  */
18975   token = cp_lexer_peek_token (parser->lexer);
18976
18977   /* If the next token is a `)', `,', `=', `>', or `...', then there
18978      is no declarator. However, when variadic templates are enabled,
18979      there may be a declarator following `...'.  */
18980   if (token->type == CPP_CLOSE_PAREN
18981       || token->type == CPP_COMMA
18982       || token->type == CPP_EQ
18983       || token->type == CPP_GREATER)
18984     {
18985       declarator = NULL;
18986       if (parenthesized_p)
18987         *parenthesized_p = false;
18988     }
18989   /* Otherwise, there should be a declarator.  */
18990   else
18991     {
18992       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18993       parser->default_arg_ok_p = false;
18994
18995       /* After seeing a decl-specifier-seq, if the next token is not a
18996          "(", there is no possibility that the code is a valid
18997          expression.  Therefore, if parsing tentatively, we commit at
18998          this point.  */
18999       if (!parser->in_template_argument_list_p
19000           /* In an expression context, having seen:
19001
19002                (int((char ...
19003
19004              we cannot be sure whether we are looking at a
19005              function-type (taking a "char" as a parameter) or a cast
19006              of some object of type "char" to "int".  */
19007           && !parser->in_type_id_in_expr_p
19008           && cp_parser_uncommitted_to_tentative_parse_p (parser)
19009           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19010           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19011         cp_parser_commit_to_tentative_parse (parser);
19012       /* Parse the declarator.  */
19013       declarator_token_start = token;
19014       declarator = cp_parser_declarator (parser,
19015                                          CP_PARSER_DECLARATOR_EITHER,
19016                                          /*ctor_dtor_or_conv_p=*/NULL,
19017                                          parenthesized_p,
19018                                          /*member_p=*/false,
19019                                          /*friend_p=*/false);
19020       parser->default_arg_ok_p = saved_default_arg_ok_p;
19021       /* After the declarator, allow more attributes.  */
19022       decl_specifiers.attributes
19023         = chainon (decl_specifiers.attributes,
19024                    cp_parser_attributes_opt (parser));
19025     }
19026
19027   /* If the next token is an ellipsis, and we have not seen a
19028      declarator name, and the type of the declarator contains parameter
19029      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19030      a parameter pack expansion expression. Otherwise, leave the
19031      ellipsis for a C-style variadic function. */
19032   token = cp_lexer_peek_token (parser->lexer);
19033   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19034     {
19035       tree type = decl_specifiers.type;
19036
19037       if (type && DECL_P (type))
19038         type = TREE_TYPE (type);
19039
19040       if (type
19041           && TREE_CODE (type) != TYPE_PACK_EXPANSION
19042           && declarator_can_be_parameter_pack (declarator)
19043           && (!declarator || !declarator->parameter_pack_p)
19044           && uses_parameter_packs (type))
19045         {
19046           /* Consume the `...'. */
19047           cp_lexer_consume_token (parser->lexer);
19048           maybe_warn_variadic_templates ();
19049           
19050           /* Build a pack expansion type */
19051           if (declarator)
19052             declarator->parameter_pack_p = true;
19053           else
19054             decl_specifiers.type = make_pack_expansion (type);
19055         }
19056     }
19057
19058   /* The restriction on defining new types applies only to the type
19059      of the parameter, not to the default argument.  */
19060   parser->type_definition_forbidden_message = saved_message;
19061
19062   /* If the next token is `=', then process a default argument.  */
19063   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19064     {
19065       token = cp_lexer_peek_token (parser->lexer);
19066       /* If we are defining a class, then the tokens that make up the
19067          default argument must be saved and processed later.  */
19068       if (!template_parm_p && at_class_scope_p ()
19069           && TYPE_BEING_DEFINED (current_class_type)
19070           && !LAMBDA_TYPE_P (current_class_type))
19071         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19072       /* Outside of a class definition, we can just parse the
19073          assignment-expression.  */
19074       else
19075         default_argument
19076           = cp_parser_default_argument (parser, template_parm_p);
19077
19078       if (!parser->default_arg_ok_p)
19079         {
19080           if (flag_permissive)
19081             warning (0, "deprecated use of default argument for parameter of non-function");
19082           else
19083             {
19084               error_at (token->location,
19085                         "default arguments are only "
19086                         "permitted for function parameters");
19087               default_argument = NULL_TREE;
19088             }
19089         }
19090       else if ((declarator && declarator->parameter_pack_p)
19091                || (decl_specifiers.type
19092                    && PACK_EXPANSION_P (decl_specifiers.type)))
19093         {
19094           /* Find the name of the parameter pack.  */     
19095           cp_declarator *id_declarator = declarator;
19096           while (id_declarator && id_declarator->kind != cdk_id)
19097             id_declarator = id_declarator->declarator;
19098           
19099           if (id_declarator && id_declarator->kind == cdk_id)
19100             error_at (declarator_token_start->location,
19101                       template_parm_p
19102                       ? G_("template parameter pack %qD "
19103                            "cannot have a default argument")
19104                       : G_("parameter pack %qD cannot have "
19105                            "a default argument"),
19106                       id_declarator->u.id.unqualified_name);
19107           else
19108             error_at (declarator_token_start->location,
19109                       template_parm_p
19110                       ? G_("template parameter pack cannot have "
19111                            "a default argument")
19112                       : G_("parameter pack cannot have a "
19113                            "default argument"));
19114
19115           default_argument = NULL_TREE;
19116         }
19117     }
19118   else
19119     default_argument = NULL_TREE;
19120
19121   return make_parameter_declarator (&decl_specifiers,
19122                                     declarator,
19123                                     default_argument);
19124 }
19125
19126 /* Parse a default argument and return it.
19127
19128    TEMPLATE_PARM_P is true if this is a default argument for a
19129    non-type template parameter.  */
19130 static tree
19131 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19132 {
19133   tree default_argument = NULL_TREE;
19134   bool saved_greater_than_is_operator_p;
19135   bool saved_local_variables_forbidden_p;
19136   bool non_constant_p, is_direct_init;
19137
19138   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19139      set correctly.  */
19140   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19141   parser->greater_than_is_operator_p = !template_parm_p;
19142   /* Local variable names (and the `this' keyword) may not
19143      appear in a default argument.  */
19144   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19145   parser->local_variables_forbidden_p = true;
19146   /* Parse the assignment-expression.  */
19147   if (template_parm_p)
19148     push_deferring_access_checks (dk_no_deferred);
19149   tree saved_class_ptr = NULL_TREE;
19150   tree saved_class_ref = NULL_TREE;
19151   /* The "this" pointer is not valid in a default argument.  */
19152   if (cfun)
19153     {
19154       saved_class_ptr = current_class_ptr;
19155       cp_function_chain->x_current_class_ptr = NULL_TREE;
19156       saved_class_ref = current_class_ref;
19157       cp_function_chain->x_current_class_ref = NULL_TREE;
19158     }
19159   default_argument
19160     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19161   /* Restore the "this" pointer.  */
19162   if (cfun)
19163     {
19164       cp_function_chain->x_current_class_ptr = saved_class_ptr;
19165       cp_function_chain->x_current_class_ref = saved_class_ref;
19166     }
19167   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19168     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19169   if (template_parm_p)
19170     pop_deferring_access_checks ();
19171   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19172   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19173
19174   return default_argument;
19175 }
19176
19177 /* Parse a function-body.
19178
19179    function-body:
19180      compound_statement  */
19181
19182 static void
19183 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19184 {
19185   cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19186 }
19187
19188 /* Parse a ctor-initializer-opt followed by a function-body.  Return
19189    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
19190    is true we are parsing a function-try-block.  */
19191
19192 static bool
19193 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19194                                                   bool in_function_try_block)
19195 {
19196   tree body, list;
19197   bool ctor_initializer_p;
19198   const bool check_body_p =
19199      DECL_CONSTRUCTOR_P (current_function_decl)
19200      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19201   tree last = NULL;
19202
19203   /* Begin the function body.  */
19204   body = begin_function_body ();
19205   /* Parse the optional ctor-initializer.  */
19206   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19207
19208   /* If we're parsing a constexpr constructor definition, we need
19209      to check that the constructor body is indeed empty.  However,
19210      before we get to cp_parser_function_body lot of junk has been
19211      generated, so we can't just check that we have an empty block.
19212      Rather we take a snapshot of the outermost block, and check whether
19213      cp_parser_function_body changed its state.  */
19214   if (check_body_p)
19215     {
19216       list = cur_stmt_list;
19217       if (STATEMENT_LIST_TAIL (list))
19218         last = STATEMENT_LIST_TAIL (list)->stmt;
19219     }
19220   /* Parse the function-body.  */
19221   cp_parser_function_body (parser, in_function_try_block);
19222   if (check_body_p)
19223     check_constexpr_ctor_body (last, list, /*complain=*/true);
19224   /* Finish the function body.  */
19225   finish_function_body (body);
19226
19227   return ctor_initializer_p;
19228 }
19229
19230 /* Parse an initializer.
19231
19232    initializer:
19233      = initializer-clause
19234      ( expression-list )
19235
19236    Returns an expression representing the initializer.  If no
19237    initializer is present, NULL_TREE is returned.
19238
19239    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19240    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
19241    set to TRUE if there is no initializer present.  If there is an
19242    initializer, and it is not a constant-expression, *NON_CONSTANT_P
19243    is set to true; otherwise it is set to false.  */
19244
19245 static tree
19246 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19247                        bool* non_constant_p)
19248 {
19249   cp_token *token;
19250   tree init;
19251
19252   /* Peek at the next token.  */
19253   token = cp_lexer_peek_token (parser->lexer);
19254
19255   /* Let our caller know whether or not this initializer was
19256      parenthesized.  */
19257   *is_direct_init = (token->type != CPP_EQ);
19258   /* Assume that the initializer is constant.  */
19259   *non_constant_p = false;
19260
19261   if (token->type == CPP_EQ)
19262     {
19263       /* Consume the `='.  */
19264       cp_lexer_consume_token (parser->lexer);
19265       /* Parse the initializer-clause.  */
19266       init = cp_parser_initializer_clause (parser, non_constant_p);
19267     }
19268   else if (token->type == CPP_OPEN_PAREN)
19269     {
19270       vec<tree, va_gc> *vec;
19271       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19272                                                      /*cast_p=*/false,
19273                                                      /*allow_expansion_p=*/true,
19274                                                      non_constant_p);
19275       if (vec == NULL)
19276         return error_mark_node;
19277       init = build_tree_list_vec (vec);
19278       release_tree_vector (vec);
19279     }
19280   else if (token->type == CPP_OPEN_BRACE)
19281     {
19282       cp_lexer_set_source_position (parser->lexer);
19283       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19284       init = cp_parser_braced_list (parser, non_constant_p);
19285       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19286     }
19287   else
19288     {
19289       /* Anything else is an error.  */
19290       cp_parser_error (parser, "expected initializer");
19291       init = error_mark_node;
19292     }
19293
19294   return init;
19295 }
19296
19297 /* Parse an initializer-clause.
19298
19299    initializer-clause:
19300      assignment-expression
19301      braced-init-list
19302
19303    Returns an expression representing the initializer.
19304
19305    If the `assignment-expression' production is used the value
19306    returned is simply a representation for the expression.
19307
19308    Otherwise, calls cp_parser_braced_list.  */
19309
19310 static tree
19311 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19312 {
19313   tree initializer;
19314
19315   /* Assume the expression is constant.  */
19316   *non_constant_p = false;
19317
19318   /* If it is not a `{', then we are looking at an
19319      assignment-expression.  */
19320   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19321     {
19322       initializer
19323         = cp_parser_constant_expression (parser,
19324                                         /*allow_non_constant_p=*/true,
19325                                         non_constant_p);
19326     }
19327   else
19328     initializer = cp_parser_braced_list (parser, non_constant_p);
19329
19330   return initializer;
19331 }
19332
19333 /* Parse a brace-enclosed initializer list.
19334
19335    braced-init-list:
19336      { initializer-list , [opt] }
19337      { }
19338
19339    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
19340    the elements of the initializer-list (or NULL, if the last
19341    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
19342    NULL_TREE.  There is no way to detect whether or not the optional
19343    trailing `,' was provided.  NON_CONSTANT_P is as for
19344    cp_parser_initializer.  */     
19345
19346 static tree
19347 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19348 {
19349   tree initializer;
19350
19351   /* Consume the `{' token.  */
19352   cp_lexer_consume_token (parser->lexer);
19353   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
19354   initializer = make_node (CONSTRUCTOR);
19355   /* If it's not a `}', then there is a non-trivial initializer.  */
19356   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19357     {
19358       /* Parse the initializer list.  */
19359       CONSTRUCTOR_ELTS (initializer)
19360         = cp_parser_initializer_list (parser, non_constant_p);
19361       /* A trailing `,' token is allowed.  */
19362       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19363         cp_lexer_consume_token (parser->lexer);
19364     }
19365   else
19366     *non_constant_p = false;
19367   /* Now, there should be a trailing `}'.  */
19368   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19369   TREE_TYPE (initializer) = init_list_type_node;
19370   return initializer;
19371 }
19372
19373 /* Consume tokens up to, and including, the next non-nested closing `]'.
19374    Returns true iff we found a closing `]'.  */
19375
19376 static bool
19377 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19378 {
19379   unsigned square_depth = 0;
19380
19381   while (true)
19382     {
19383       cp_token * token = cp_lexer_peek_token (parser->lexer);
19384
19385       switch (token->type)
19386         {
19387         case CPP_EOF:
19388         case CPP_PRAGMA_EOL:
19389           /* If we've run out of tokens, then there is no closing `]'.  */
19390           return false;
19391
19392         case CPP_OPEN_SQUARE:
19393           ++square_depth;
19394           break;
19395
19396         case CPP_CLOSE_SQUARE:
19397           if (!square_depth--)
19398             {
19399               cp_lexer_consume_token (parser->lexer);
19400               return true;
19401             }
19402           break;
19403
19404         default:
19405           break;
19406         }
19407
19408       /* Consume the token.  */
19409       cp_lexer_consume_token (parser->lexer);
19410     }
19411 }
19412
19413 /* Return true if we are looking at an array-designator, false otherwise.  */
19414
19415 static bool
19416 cp_parser_array_designator_p (cp_parser *parser)
19417 {
19418   /* Consume the `['.  */
19419   cp_lexer_consume_token (parser->lexer);
19420
19421   cp_lexer_save_tokens (parser->lexer);
19422
19423   /* Skip tokens until the next token is a closing square bracket.
19424      If we find the closing `]', and the next token is a `=', then
19425      we are looking at an array designator.  */
19426   bool array_designator_p
19427     = (cp_parser_skip_to_closing_square_bracket (parser)
19428        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19429   
19430   /* Roll back the tokens we skipped.  */
19431   cp_lexer_rollback_tokens (parser->lexer);
19432
19433   return array_designator_p;
19434 }
19435
19436 /* Parse an initializer-list.
19437
19438    initializer-list:
19439      initializer-clause ... [opt]
19440      initializer-list , initializer-clause ... [opt]
19441
19442    GNU Extension:
19443
19444    initializer-list:
19445      designation initializer-clause ...[opt]
19446      initializer-list , designation initializer-clause ...[opt]
19447
19448    designation:
19449      . identifier =
19450      identifier :
19451      [ constant-expression ] =
19452
19453    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
19454    for the initializer.  If the INDEX of the elt is non-NULL, it is the
19455    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
19456    as for cp_parser_initializer.  */
19457
19458 static vec<constructor_elt, va_gc> *
19459 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19460 {
19461   vec<constructor_elt, va_gc> *v = NULL;
19462
19463   /* Assume all of the expressions are constant.  */
19464   *non_constant_p = false;
19465
19466   /* Parse the rest of the list.  */
19467   while (true)
19468     {
19469       cp_token *token;
19470       tree designator;
19471       tree initializer;
19472       bool clause_non_constant_p;
19473
19474       /* If the next token is an identifier and the following one is a
19475          colon, we are looking at the GNU designated-initializer
19476          syntax.  */
19477       if (cp_parser_allow_gnu_extensions_p (parser)
19478           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19479           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19480         {
19481           /* Warn the user that they are using an extension.  */
19482           pedwarn (input_location, OPT_Wpedantic, 
19483                    "ISO C++ does not allow designated initializers");
19484           /* Consume the identifier.  */
19485           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19486           /* Consume the `:'.  */
19487           cp_lexer_consume_token (parser->lexer);
19488         }
19489       /* Also handle the C99 syntax, '. id ='.  */
19490       else if (cp_parser_allow_gnu_extensions_p (parser)
19491                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19492                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19493                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19494         {
19495           /* Warn the user that they are using an extension.  */
19496           pedwarn (input_location, OPT_Wpedantic,
19497                    "ISO C++ does not allow C99 designated initializers");
19498           /* Consume the `.'.  */
19499           cp_lexer_consume_token (parser->lexer);
19500           /* Consume the identifier.  */
19501           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19502           /* Consume the `='.  */
19503           cp_lexer_consume_token (parser->lexer);
19504         }
19505       /* Also handle C99 array designators, '[ const ] ='.  */
19506       else if (cp_parser_allow_gnu_extensions_p (parser)
19507                && !c_dialect_objc ()
19508                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19509         {
19510           /* In C++11, [ could start a lambda-introducer.  */
19511           bool non_const = false;
19512
19513           cp_parser_parse_tentatively (parser);
19514
19515           if (!cp_parser_array_designator_p (parser))
19516             {
19517               cp_parser_simulate_error (parser);
19518               designator = NULL_TREE;
19519             }
19520           else
19521             {
19522               designator = cp_parser_constant_expression (parser, true,
19523                                                           &non_const);
19524               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19525               cp_parser_require (parser, CPP_EQ, RT_EQ);
19526             }
19527
19528           if (!cp_parser_parse_definitely (parser))
19529             designator = NULL_TREE;
19530           else if (non_const)
19531             require_potential_rvalue_constant_expression (designator);
19532         }
19533       else
19534         designator = NULL_TREE;
19535
19536       /* Parse the initializer.  */
19537       initializer = cp_parser_initializer_clause (parser,
19538                                                   &clause_non_constant_p);
19539       /* If any clause is non-constant, so is the entire initializer.  */
19540       if (clause_non_constant_p)
19541         *non_constant_p = true;
19542
19543       /* If we have an ellipsis, this is an initializer pack
19544          expansion.  */
19545       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19546         {
19547           /* Consume the `...'.  */
19548           cp_lexer_consume_token (parser->lexer);
19549
19550           /* Turn the initializer into an initializer expansion.  */
19551           initializer = make_pack_expansion (initializer);
19552         }
19553
19554       /* Add it to the vector.  */
19555       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19556
19557       /* If the next token is not a comma, we have reached the end of
19558          the list.  */
19559       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19560         break;
19561
19562       /* Peek at the next token.  */
19563       token = cp_lexer_peek_nth_token (parser->lexer, 2);
19564       /* If the next token is a `}', then we're still done.  An
19565          initializer-clause can have a trailing `,' after the
19566          initializer-list and before the closing `}'.  */
19567       if (token->type == CPP_CLOSE_BRACE)
19568         break;
19569
19570       /* Consume the `,' token.  */
19571       cp_lexer_consume_token (parser->lexer);
19572     }
19573
19574   return v;
19575 }
19576
19577 /* Classes [gram.class] */
19578
19579 /* Parse a class-name.
19580
19581    class-name:
19582      identifier
19583      template-id
19584
19585    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19586    to indicate that names looked up in dependent types should be
19587    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
19588    keyword has been used to indicate that the name that appears next
19589    is a template.  TAG_TYPE indicates the explicit tag given before
19590    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
19591    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
19592    is the class being defined in a class-head.
19593
19594    Returns the TYPE_DECL representing the class.  */
19595
19596 static tree
19597 cp_parser_class_name (cp_parser *parser,
19598                       bool typename_keyword_p,
19599                       bool template_keyword_p,
19600                       enum tag_types tag_type,
19601                       bool check_dependency_p,
19602                       bool class_head_p,
19603                       bool is_declaration)
19604 {
19605   tree decl;
19606   tree scope;
19607   bool typename_p;
19608   cp_token *token;
19609   tree identifier = NULL_TREE;
19610
19611   /* All class-names start with an identifier.  */
19612   token = cp_lexer_peek_token (parser->lexer);
19613   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19614     {
19615       cp_parser_error (parser, "expected class-name");
19616       return error_mark_node;
19617     }
19618
19619   /* PARSER->SCOPE can be cleared when parsing the template-arguments
19620      to a template-id, so we save it here.  */
19621   scope = parser->scope;
19622   if (scope == error_mark_node)
19623     return error_mark_node;
19624
19625   /* Any name names a type if we're following the `typename' keyword
19626      in a qualified name where the enclosing scope is type-dependent.  */
19627   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19628                 && dependent_type_p (scope));
19629   /* Handle the common case (an identifier, but not a template-id)
19630      efficiently.  */
19631   if (token->type == CPP_NAME
19632       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19633     {
19634       cp_token *identifier_token;
19635       bool ambiguous_p;
19636
19637       /* Look for the identifier.  */
19638       identifier_token = cp_lexer_peek_token (parser->lexer);
19639       ambiguous_p = identifier_token->error_reported;
19640       identifier = cp_parser_identifier (parser);
19641       /* If the next token isn't an identifier, we are certainly not
19642          looking at a class-name.  */
19643       if (identifier == error_mark_node)
19644         decl = error_mark_node;
19645       /* If we know this is a type-name, there's no need to look it
19646          up.  */
19647       else if (typename_p)
19648         decl = identifier;
19649       else
19650         {
19651           tree ambiguous_decls;
19652           /* If we already know that this lookup is ambiguous, then
19653              we've already issued an error message; there's no reason
19654              to check again.  */
19655           if (ambiguous_p)
19656             {
19657               cp_parser_simulate_error (parser);
19658               return error_mark_node;
19659             }
19660           /* If the next token is a `::', then the name must be a type
19661              name.
19662
19663              [basic.lookup.qual]
19664
19665              During the lookup for a name preceding the :: scope
19666              resolution operator, object, function, and enumerator
19667              names are ignored.  */
19668           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19669             tag_type = typename_type;
19670           /* Look up the name.  */
19671           decl = cp_parser_lookup_name (parser, identifier,
19672                                         tag_type,
19673                                         /*is_template=*/false,
19674                                         /*is_namespace=*/false,
19675                                         check_dependency_p,
19676                                         &ambiguous_decls,
19677                                         identifier_token->location);
19678           if (ambiguous_decls)
19679             {
19680               if (cp_parser_parsing_tentatively (parser))
19681                 cp_parser_simulate_error (parser);
19682               return error_mark_node;
19683             }
19684         }
19685     }
19686   else
19687     {
19688       /* Try a template-id.  */
19689       decl = cp_parser_template_id (parser, template_keyword_p,
19690                                     check_dependency_p,
19691                                     tag_type,
19692                                     is_declaration);
19693       if (decl == error_mark_node)
19694         return error_mark_node;
19695     }
19696
19697   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19698
19699   /* If this is a typename, create a TYPENAME_TYPE.  */
19700   if (typename_p && decl != error_mark_node)
19701     {
19702       decl = make_typename_type (scope, decl, typename_type,
19703                                  /*complain=*/tf_error);
19704       if (decl != error_mark_node)
19705         decl = TYPE_NAME (decl);
19706     }
19707
19708   decl = strip_using_decl (decl);
19709
19710   /* Check to see that it is really the name of a class.  */
19711   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19712       && identifier_p (TREE_OPERAND (decl, 0))
19713       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19714     /* Situations like this:
19715
19716          template <typename T> struct A {
19717            typename T::template X<int>::I i;
19718          };
19719
19720        are problematic.  Is `T::template X<int>' a class-name?  The
19721        standard does not seem to be definitive, but there is no other
19722        valid interpretation of the following `::'.  Therefore, those
19723        names are considered class-names.  */
19724     {
19725       decl = make_typename_type (scope, decl, tag_type, tf_error);
19726       if (decl != error_mark_node)
19727         decl = TYPE_NAME (decl);
19728     }
19729   else if (TREE_CODE (decl) != TYPE_DECL
19730            || TREE_TYPE (decl) == error_mark_node
19731            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19732            /* In Objective-C 2.0, a classname followed by '.' starts a
19733               dot-syntax expression, and it's not a type-name.  */
19734            || (c_dialect_objc ()
19735                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
19736                && objc_is_class_name (decl)))
19737     decl = error_mark_node;
19738
19739   if (decl == error_mark_node)
19740     cp_parser_error (parser, "expected class-name");
19741   else if (identifier && !parser->scope)
19742     maybe_note_name_used_in_class (identifier, decl);
19743
19744   return decl;
19745 }
19746
19747 /* Parse a class-specifier.
19748
19749    class-specifier:
19750      class-head { member-specification [opt] }
19751
19752    Returns the TREE_TYPE representing the class.  */
19753
19754 static tree
19755 cp_parser_class_specifier_1 (cp_parser* parser)
19756 {
19757   tree type;
19758   tree attributes = NULL_TREE;
19759   bool nested_name_specifier_p;
19760   unsigned saved_num_template_parameter_lists;
19761   bool saved_in_function_body;
19762   unsigned char in_statement;
19763   bool in_switch_statement_p;
19764   bool saved_in_unbraced_linkage_specification_p;
19765   tree old_scope = NULL_TREE;
19766   tree scope = NULL_TREE;
19767   cp_token *closing_brace;
19768
19769   push_deferring_access_checks (dk_no_deferred);
19770
19771   /* Parse the class-head.  */
19772   type = cp_parser_class_head (parser,
19773                                &nested_name_specifier_p);
19774   /* If the class-head was a semantic disaster, skip the entire body
19775      of the class.  */
19776   if (!type)
19777     {
19778       cp_parser_skip_to_end_of_block_or_statement (parser);
19779       pop_deferring_access_checks ();
19780       return error_mark_node;
19781     }
19782
19783   /* Look for the `{'.  */
19784   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19785     {
19786       pop_deferring_access_checks ();
19787       return error_mark_node;
19788     }
19789
19790   cp_ensure_no_omp_declare_simd (parser);
19791
19792   /* Issue an error message if type-definitions are forbidden here.  */
19793   cp_parser_check_type_definition (parser);
19794   /* Remember that we are defining one more class.  */
19795   ++parser->num_classes_being_defined;
19796   /* Inside the class, surrounding template-parameter-lists do not
19797      apply.  */
19798   saved_num_template_parameter_lists
19799     = parser->num_template_parameter_lists;
19800   parser->num_template_parameter_lists = 0;
19801   /* We are not in a function body.  */
19802   saved_in_function_body = parser->in_function_body;
19803   parser->in_function_body = false;
19804   /* Or in a loop.  */
19805   in_statement = parser->in_statement;
19806   parser->in_statement = 0;
19807   /* Or in a switch.  */
19808   in_switch_statement_p = parser->in_switch_statement_p;
19809   parser->in_switch_statement_p = false;
19810   /* We are not immediately inside an extern "lang" block.  */
19811   saved_in_unbraced_linkage_specification_p
19812     = parser->in_unbraced_linkage_specification_p;
19813   parser->in_unbraced_linkage_specification_p = false;
19814
19815   /* Start the class.  */
19816   if (nested_name_specifier_p)
19817     {
19818       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19819       old_scope = push_inner_scope (scope);
19820     }
19821   type = begin_class_definition (type);
19822
19823   if (type == error_mark_node)
19824     /* If the type is erroneous, skip the entire body of the class.  */
19825     cp_parser_skip_to_closing_brace (parser);
19826   else
19827     /* Parse the member-specification.  */
19828     cp_parser_member_specification_opt (parser);
19829
19830   /* Look for the trailing `}'.  */
19831   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19832   /* Look for trailing attributes to apply to this class.  */
19833   if (cp_parser_allow_gnu_extensions_p (parser))
19834     attributes = cp_parser_gnu_attributes_opt (parser);
19835   if (type != error_mark_node)
19836     type = finish_struct (type, attributes);
19837   if (nested_name_specifier_p)
19838     pop_inner_scope (old_scope, scope);
19839
19840   /* We've finished a type definition.  Check for the common syntax
19841      error of forgetting a semicolon after the definition.  We need to
19842      be careful, as we can't just check for not-a-semicolon and be done
19843      with it; the user might have typed:
19844
19845      class X { } c = ...;
19846      class X { } *p = ...;
19847
19848      and so forth.  Instead, enumerate all the possible tokens that
19849      might follow this production; if we don't see one of them, then
19850      complain and silently insert the semicolon.  */
19851   {
19852     cp_token *token = cp_lexer_peek_token (parser->lexer);
19853     bool want_semicolon = true;
19854
19855     if (cp_next_tokens_can_be_std_attribute_p (parser))
19856       /* Don't try to parse c++11 attributes here.  As per the
19857          grammar, that should be a task for
19858          cp_parser_decl_specifier_seq.  */
19859       want_semicolon = false;
19860
19861     switch (token->type)
19862       {
19863       case CPP_NAME:
19864       case CPP_SEMICOLON:
19865       case CPP_MULT:
19866       case CPP_AND:
19867       case CPP_OPEN_PAREN:
19868       case CPP_CLOSE_PAREN:
19869       case CPP_COMMA:
19870         want_semicolon = false;
19871         break;
19872
19873         /* While it's legal for type qualifiers and storage class
19874            specifiers to follow type definitions in the grammar, only
19875            compiler testsuites contain code like that.  Assume that if
19876            we see such code, then what we're really seeing is a case
19877            like:
19878
19879            class X { }
19880            const <type> var = ...;
19881
19882            or
19883
19884            class Y { }
19885            static <type> func (...) ...
19886
19887            i.e. the qualifier or specifier applies to the next
19888            declaration.  To do so, however, we need to look ahead one
19889            more token to see if *that* token is a type specifier.
19890
19891            This code could be improved to handle:
19892
19893            class Z { }
19894            static const <type> var = ...;  */
19895       case CPP_KEYWORD:
19896         if (keyword_is_decl_specifier (token->keyword))
19897           {
19898             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19899
19900             /* Handling user-defined types here would be nice, but very
19901                tricky.  */
19902             want_semicolon
19903               = (lookahead->type == CPP_KEYWORD
19904                  && keyword_begins_type_specifier (lookahead->keyword));
19905           }
19906         break;
19907       default:
19908         break;
19909       }
19910
19911     /* If we don't have a type, then something is very wrong and we
19912        shouldn't try to do anything clever.  Likewise for not seeing the
19913        closing brace.  */
19914     if (closing_brace && TYPE_P (type) && want_semicolon)
19915       {
19916         cp_token_position prev
19917           = cp_lexer_previous_token_position (parser->lexer);
19918         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19919         location_t loc = prev_token->location;
19920
19921         if (CLASSTYPE_DECLARED_CLASS (type))
19922           error_at (loc, "expected %<;%> after class definition");
19923         else if (TREE_CODE (type) == RECORD_TYPE)
19924           error_at (loc, "expected %<;%> after struct definition");
19925         else if (TREE_CODE (type) == UNION_TYPE)
19926           error_at (loc, "expected %<;%> after union definition");
19927         else
19928           gcc_unreachable ();
19929
19930         /* Unget one token and smash it to look as though we encountered
19931            a semicolon in the input stream.  */
19932         cp_lexer_set_token_position (parser->lexer, prev);
19933         token = cp_lexer_peek_token (parser->lexer);
19934         token->type = CPP_SEMICOLON;
19935         token->keyword = RID_MAX;
19936       }
19937   }
19938
19939   /* If this class is not itself within the scope of another class,
19940      then we need to parse the bodies of all of the queued function
19941      definitions.  Note that the queued functions defined in a class
19942      are not always processed immediately following the
19943      class-specifier for that class.  Consider:
19944
19945        struct A {
19946          struct B { void f() { sizeof (A); } };
19947        };
19948
19949      If `f' were processed before the processing of `A' were
19950      completed, there would be no way to compute the size of `A'.
19951      Note that the nesting we are interested in here is lexical --
19952      not the semantic nesting given by TYPE_CONTEXT.  In particular,
19953      for:
19954
19955        struct A { struct B; };
19956        struct A::B { void f() { } };
19957
19958      there is no need to delay the parsing of `A::B::f'.  */
19959   if (--parser->num_classes_being_defined == 0)
19960     {
19961       tree decl;
19962       tree class_type = NULL_TREE;
19963       tree pushed_scope = NULL_TREE;
19964       unsigned ix;
19965       cp_default_arg_entry *e;
19966       tree save_ccp, save_ccr;
19967
19968       /* In a first pass, parse default arguments to the functions.
19969          Then, in a second pass, parse the bodies of the functions.
19970          This two-phased approach handles cases like:
19971
19972             struct S {
19973               void f() { g(); }
19974               void g(int i = 3);
19975             };
19976
19977          */
19978       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19979         {
19980           decl = e->decl;
19981           /* If there are default arguments that have not yet been processed,
19982              take care of them now.  */
19983           if (class_type != e->class_type)
19984             {
19985               if (pushed_scope)
19986                 pop_scope (pushed_scope);
19987               class_type = e->class_type;
19988               pushed_scope = push_scope (class_type);
19989             }
19990           /* Make sure that any template parameters are in scope.  */
19991           maybe_begin_member_template_processing (decl);
19992           /* Parse the default argument expressions.  */
19993           cp_parser_late_parsing_default_args (parser, decl);
19994           /* Remove any template parameters from the symbol table.  */
19995           maybe_end_member_template_processing ();
19996         }
19997       vec_safe_truncate (unparsed_funs_with_default_args, 0);
19998       /* Now parse any NSDMIs.  */
19999       save_ccp = current_class_ptr;
20000       save_ccr = current_class_ref;
20001       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20002         {
20003           if (class_type != DECL_CONTEXT (decl))
20004             {
20005               if (pushed_scope)
20006                 pop_scope (pushed_scope);
20007               class_type = DECL_CONTEXT (decl);
20008               pushed_scope = push_scope (class_type);
20009             }
20010           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20011           cp_parser_late_parsing_nsdmi (parser, decl);
20012         }
20013       vec_safe_truncate (unparsed_nsdmis, 0);
20014       current_class_ptr = save_ccp;
20015       current_class_ref = save_ccr;
20016       if (pushed_scope)
20017         pop_scope (pushed_scope);
20018
20019       /* Now do some post-NSDMI bookkeeping.  */
20020       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20021         after_nsdmi_defaulted_late_checks (class_type);
20022       vec_safe_truncate (unparsed_classes, 0);
20023       after_nsdmi_defaulted_late_checks (type);
20024
20025       /* Now parse the body of the functions.  */
20026       if (flag_openmp)
20027         {
20028           /* OpenMP UDRs need to be parsed before all other functions.  */
20029           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20030             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20031               cp_parser_late_parsing_for_member (parser, decl);
20032           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20033             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20034               cp_parser_late_parsing_for_member (parser, decl);
20035         }
20036       else
20037         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20038           cp_parser_late_parsing_for_member (parser, decl);
20039       vec_safe_truncate (unparsed_funs_with_definitions, 0);
20040     }
20041   else
20042     vec_safe_push (unparsed_classes, type);
20043
20044   /* Put back any saved access checks.  */
20045   pop_deferring_access_checks ();
20046
20047   /* Restore saved state.  */
20048   parser->in_switch_statement_p = in_switch_statement_p;
20049   parser->in_statement = in_statement;
20050   parser->in_function_body = saved_in_function_body;
20051   parser->num_template_parameter_lists
20052     = saved_num_template_parameter_lists;
20053   parser->in_unbraced_linkage_specification_p
20054     = saved_in_unbraced_linkage_specification_p;
20055
20056   return type;
20057 }
20058
20059 static tree
20060 cp_parser_class_specifier (cp_parser* parser)
20061 {
20062   tree ret;
20063   timevar_push (TV_PARSE_STRUCT);
20064   ret = cp_parser_class_specifier_1 (parser);
20065   timevar_pop (TV_PARSE_STRUCT);
20066   return ret;
20067 }
20068
20069 /* Parse a class-head.
20070
20071    class-head:
20072      class-key identifier [opt] base-clause [opt]
20073      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20074      class-key nested-name-specifier [opt] template-id
20075        base-clause [opt]
20076
20077    class-virt-specifier:
20078      final
20079
20080    GNU Extensions:
20081      class-key attributes identifier [opt] base-clause [opt]
20082      class-key attributes nested-name-specifier identifier base-clause [opt]
20083      class-key attributes nested-name-specifier [opt] template-id
20084        base-clause [opt]
20085
20086    Upon return BASES is initialized to the list of base classes (or
20087    NULL, if there are none) in the same form returned by
20088    cp_parser_base_clause.
20089
20090    Returns the TYPE of the indicated class.  Sets
20091    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20092    involving a nested-name-specifier was used, and FALSE otherwise.
20093
20094    Returns error_mark_node if this is not a class-head.
20095
20096    Returns NULL_TREE if the class-head is syntactically valid, but
20097    semantically invalid in a way that means we should skip the entire
20098    body of the class.  */
20099
20100 static tree
20101 cp_parser_class_head (cp_parser* parser,
20102                       bool* nested_name_specifier_p)
20103 {
20104   tree nested_name_specifier;
20105   enum tag_types class_key;
20106   tree id = NULL_TREE;
20107   tree type = NULL_TREE;
20108   tree attributes;
20109   tree bases;
20110   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20111   bool template_id_p = false;
20112   bool qualified_p = false;
20113   bool invalid_nested_name_p = false;
20114   bool invalid_explicit_specialization_p = false;
20115   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20116   tree pushed_scope = NULL_TREE;
20117   unsigned num_templates;
20118   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20119   /* Assume no nested-name-specifier will be present.  */
20120   *nested_name_specifier_p = false;
20121   /* Assume no template parameter lists will be used in defining the
20122      type.  */
20123   num_templates = 0;
20124   parser->colon_corrects_to_scope_p = false;
20125
20126   /* Look for the class-key.  */
20127   class_key = cp_parser_class_key (parser);
20128   if (class_key == none_type)
20129     return error_mark_node;
20130
20131   /* Parse the attributes.  */
20132   attributes = cp_parser_attributes_opt (parser);
20133
20134   /* If the next token is `::', that is invalid -- but sometimes
20135      people do try to write:
20136
20137        struct ::S {};
20138
20139      Handle this gracefully by accepting the extra qualifier, and then
20140      issuing an error about it later if this really is a
20141      class-head.  If it turns out just to be an elaborated type
20142      specifier, remain silent.  */
20143   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20144     qualified_p = true;
20145
20146   push_deferring_access_checks (dk_no_check);
20147
20148   /* Determine the name of the class.  Begin by looking for an
20149      optional nested-name-specifier.  */
20150   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20151   nested_name_specifier
20152     = cp_parser_nested_name_specifier_opt (parser,
20153                                            /*typename_keyword_p=*/false,
20154                                            /*check_dependency_p=*/false,
20155                                            /*type_p=*/true,
20156                                            /*is_declaration=*/false);
20157   /* If there was a nested-name-specifier, then there *must* be an
20158      identifier.  */
20159   if (nested_name_specifier)
20160     {
20161       type_start_token = cp_lexer_peek_token (parser->lexer);
20162       /* Although the grammar says `identifier', it really means
20163          `class-name' or `template-name'.  You are only allowed to
20164          define a class that has already been declared with this
20165          syntax.
20166
20167          The proposed resolution for Core Issue 180 says that wherever
20168          you see `class T::X' you should treat `X' as a type-name.
20169
20170          It is OK to define an inaccessible class; for example:
20171
20172            class A { class B; };
20173            class A::B {};
20174
20175          We do not know if we will see a class-name, or a
20176          template-name.  We look for a class-name first, in case the
20177          class-name is a template-id; if we looked for the
20178          template-name first we would stop after the template-name.  */
20179       cp_parser_parse_tentatively (parser);
20180       type = cp_parser_class_name (parser,
20181                                    /*typename_keyword_p=*/false,
20182                                    /*template_keyword_p=*/false,
20183                                    class_type,
20184                                    /*check_dependency_p=*/false,
20185                                    /*class_head_p=*/true,
20186                                    /*is_declaration=*/false);
20187       /* If that didn't work, ignore the nested-name-specifier.  */
20188       if (!cp_parser_parse_definitely (parser))
20189         {
20190           invalid_nested_name_p = true;
20191           type_start_token = cp_lexer_peek_token (parser->lexer);
20192           id = cp_parser_identifier (parser);
20193           if (id == error_mark_node)
20194             id = NULL_TREE;
20195         }
20196       /* If we could not find a corresponding TYPE, treat this
20197          declaration like an unqualified declaration.  */
20198       if (type == error_mark_node)
20199         nested_name_specifier = NULL_TREE;
20200       /* Otherwise, count the number of templates used in TYPE and its
20201          containing scopes.  */
20202       else
20203         {
20204           tree scope;
20205
20206           for (scope = TREE_TYPE (type);
20207                scope && TREE_CODE (scope) != NAMESPACE_DECL;
20208                scope = get_containing_scope (scope))
20209             if (TYPE_P (scope)
20210                 && CLASS_TYPE_P (scope)
20211                 && CLASSTYPE_TEMPLATE_INFO (scope)
20212                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20213                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20214                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20215               ++num_templates;
20216         }
20217     }
20218   /* Otherwise, the identifier is optional.  */
20219   else
20220     {
20221       /* We don't know whether what comes next is a template-id,
20222          an identifier, or nothing at all.  */
20223       cp_parser_parse_tentatively (parser);
20224       /* Check for a template-id.  */
20225       type_start_token = cp_lexer_peek_token (parser->lexer);
20226       id = cp_parser_template_id (parser,
20227                                   /*template_keyword_p=*/false,
20228                                   /*check_dependency_p=*/true,
20229                                   class_key,
20230                                   /*is_declaration=*/true);
20231       /* If that didn't work, it could still be an identifier.  */
20232       if (!cp_parser_parse_definitely (parser))
20233         {
20234           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20235             {
20236               type_start_token = cp_lexer_peek_token (parser->lexer);
20237               id = cp_parser_identifier (parser);
20238             }
20239           else
20240             id = NULL_TREE;
20241         }
20242       else
20243         {
20244           template_id_p = true;
20245           ++num_templates;
20246         }
20247     }
20248
20249   pop_deferring_access_checks ();
20250
20251   if (id)
20252     {
20253       cp_parser_check_for_invalid_template_id (parser, id,
20254                                                class_key,
20255                                                type_start_token->location);
20256     }
20257   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20258
20259   /* If it's not a `:' or a `{' then we can't really be looking at a
20260      class-head, since a class-head only appears as part of a
20261      class-specifier.  We have to detect this situation before calling
20262      xref_tag, since that has irreversible side-effects.  */
20263   if (!cp_parser_next_token_starts_class_definition_p (parser))
20264     {
20265       cp_parser_error (parser, "expected %<{%> or %<:%>");
20266       type = error_mark_node;
20267       goto out;
20268     }
20269
20270   /* At this point, we're going ahead with the class-specifier, even
20271      if some other problem occurs.  */
20272   cp_parser_commit_to_tentative_parse (parser);
20273   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20274     {
20275       cp_parser_error (parser,
20276                        "cannot specify %<override%> for a class");
20277       type = error_mark_node;
20278       goto out;
20279     }
20280   /* Issue the error about the overly-qualified name now.  */
20281   if (qualified_p)
20282     {
20283       cp_parser_error (parser,
20284                        "global qualification of class name is invalid");
20285       type = error_mark_node;
20286       goto out;
20287     }
20288   else if (invalid_nested_name_p)
20289     {
20290       cp_parser_error (parser,
20291                        "qualified name does not name a class");
20292       type = error_mark_node;
20293       goto out;
20294     }
20295   else if (nested_name_specifier)
20296     {
20297       tree scope;
20298
20299       /* Reject typedef-names in class heads.  */
20300       if (!DECL_IMPLICIT_TYPEDEF_P (type))
20301         {
20302           error_at (type_start_token->location,
20303                     "invalid class name in declaration of %qD",
20304                     type);
20305           type = NULL_TREE;
20306           goto done;
20307         }
20308
20309       /* Figure out in what scope the declaration is being placed.  */
20310       scope = current_scope ();
20311       /* If that scope does not contain the scope in which the
20312          class was originally declared, the program is invalid.  */
20313       if (scope && !is_ancestor (scope, nested_name_specifier))
20314         {
20315           if (at_namespace_scope_p ())
20316             error_at (type_start_token->location,
20317                       "declaration of %qD in namespace %qD which does not "
20318                       "enclose %qD",
20319                       type, scope, nested_name_specifier);
20320           else
20321             error_at (type_start_token->location,
20322                       "declaration of %qD in %qD which does not enclose %qD",
20323                       type, scope, nested_name_specifier);
20324           type = NULL_TREE;
20325           goto done;
20326         }
20327       /* [dcl.meaning]
20328
20329          A declarator-id shall not be qualified except for the
20330          definition of a ... nested class outside of its class
20331          ... [or] the definition or explicit instantiation of a
20332          class member of a namespace outside of its namespace.  */
20333       if (scope == nested_name_specifier)
20334         {
20335           permerror (nested_name_specifier_token_start->location,
20336                      "extra qualification not allowed");
20337           nested_name_specifier = NULL_TREE;
20338           num_templates = 0;
20339         }
20340     }
20341   /* An explicit-specialization must be preceded by "template <>".  If
20342      it is not, try to recover gracefully.  */
20343   if (at_namespace_scope_p ()
20344       && parser->num_template_parameter_lists == 0
20345       && template_id_p)
20346     {
20347       error_at (type_start_token->location,
20348                 "an explicit specialization must be preceded by %<template <>%>");
20349       invalid_explicit_specialization_p = true;
20350       /* Take the same action that would have been taken by
20351          cp_parser_explicit_specialization.  */
20352       ++parser->num_template_parameter_lists;
20353       begin_specialization ();
20354     }
20355   /* There must be no "return" statements between this point and the
20356      end of this function; set "type "to the correct return value and
20357      use "goto done;" to return.  */
20358   /* Make sure that the right number of template parameters were
20359      present.  */
20360   if (!cp_parser_check_template_parameters (parser, num_templates,
20361                                             type_start_token->location,
20362                                             /*declarator=*/NULL))
20363     {
20364       /* If something went wrong, there is no point in even trying to
20365          process the class-definition.  */
20366       type = NULL_TREE;
20367       goto done;
20368     }
20369
20370   /* Look up the type.  */
20371   if (template_id_p)
20372     {
20373       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20374           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20375               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20376         {
20377           error_at (type_start_token->location,
20378                     "function template %qD redeclared as a class template", id);
20379           type = error_mark_node;
20380         }
20381       else
20382         {
20383           type = TREE_TYPE (id);
20384           type = maybe_process_partial_specialization (type);
20385         }
20386       if (nested_name_specifier)
20387         pushed_scope = push_scope (nested_name_specifier);
20388     }
20389   else if (nested_name_specifier)
20390     {
20391       tree class_type;
20392
20393       /* Given:
20394
20395             template <typename T> struct S { struct T };
20396             template <typename T> struct S<T>::T { };
20397
20398          we will get a TYPENAME_TYPE when processing the definition of
20399          `S::T'.  We need to resolve it to the actual type before we
20400          try to define it.  */
20401       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20402         {
20403           class_type = resolve_typename_type (TREE_TYPE (type),
20404                                               /*only_current_p=*/false);
20405           if (TREE_CODE (class_type) != TYPENAME_TYPE)
20406             type = TYPE_NAME (class_type);
20407           else
20408             {
20409               cp_parser_error (parser, "could not resolve typename type");
20410               type = error_mark_node;
20411             }
20412         }
20413
20414       if (maybe_process_partial_specialization (TREE_TYPE (type))
20415           == error_mark_node)
20416         {
20417           type = NULL_TREE;
20418           goto done;
20419         }
20420
20421       class_type = current_class_type;
20422       /* Enter the scope indicated by the nested-name-specifier.  */
20423       pushed_scope = push_scope (nested_name_specifier);
20424       /* Get the canonical version of this type.  */
20425       type = TYPE_MAIN_DECL (TREE_TYPE (type));
20426       /* Call push_template_decl if it seems like we should be defining a
20427          template either from the template headers or the type we're
20428          defining, so that we diagnose both extra and missing headers.  */
20429       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20430            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20431           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20432         {
20433           type = push_template_decl (type);
20434           if (type == error_mark_node)
20435             {
20436               type = NULL_TREE;
20437               goto done;
20438             }
20439         }
20440
20441       type = TREE_TYPE (type);
20442       *nested_name_specifier_p = true;
20443     }
20444   else      /* The name is not a nested name.  */
20445     {
20446       /* If the class was unnamed, create a dummy name.  */
20447       if (!id)
20448         id = make_anon_name ();
20449       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20450                        parser->num_template_parameter_lists);
20451     }
20452
20453   /* Indicate whether this class was declared as a `class' or as a
20454      `struct'.  */
20455   if (TREE_CODE (type) == RECORD_TYPE)
20456     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20457   cp_parser_check_class_key (class_key, type);
20458
20459   /* If this type was already complete, and we see another definition,
20460      that's an error.  */
20461   if (type != error_mark_node && COMPLETE_TYPE_P (type))
20462     {
20463       error_at (type_start_token->location, "redefinition of %q#T",
20464                 type);
20465       error_at (type_start_token->location, "previous definition of %q+#T",
20466                 type);
20467       type = NULL_TREE;
20468       goto done;
20469     }
20470   else if (type == error_mark_node)
20471     type = NULL_TREE;
20472
20473   if (type)
20474     {
20475       /* Apply attributes now, before any use of the class as a template
20476          argument in its base list.  */
20477       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20478       fixup_attribute_variants (type);
20479     }
20480
20481   /* We will have entered the scope containing the class; the names of
20482      base classes should be looked up in that context.  For example:
20483
20484        struct A { struct B {}; struct C; };
20485        struct A::C : B {};
20486
20487      is valid.  */
20488
20489   /* Get the list of base-classes, if there is one.  */
20490   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20491     {
20492       /* PR59482: enter the class scope so that base-specifiers are looked
20493          up correctly.  */
20494       if (type)
20495         pushclass (type);
20496       bases = cp_parser_base_clause (parser);
20497       /* PR59482: get out of the previously pushed class scope so that the
20498          subsequent pops pop the right thing.  */
20499       if (type)
20500         popclass ();
20501     }
20502   else
20503     bases = NULL_TREE;
20504
20505   /* If we're really defining a class, process the base classes.
20506      If they're invalid, fail.  */
20507   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20508       && !xref_basetypes (type, bases))
20509     type = NULL_TREE;
20510
20511  done:
20512   /* Leave the scope given by the nested-name-specifier.  We will
20513      enter the class scope itself while processing the members.  */
20514   if (pushed_scope)
20515     pop_scope (pushed_scope);
20516
20517   if (invalid_explicit_specialization_p)
20518     {
20519       end_specialization ();
20520       --parser->num_template_parameter_lists;
20521     }
20522
20523   if (type)
20524     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20525   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20526     CLASSTYPE_FINAL (type) = 1;
20527  out:
20528   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20529   return type;
20530 }
20531
20532 /* Parse a class-key.
20533
20534    class-key:
20535      class
20536      struct
20537      union
20538
20539    Returns the kind of class-key specified, or none_type to indicate
20540    error.  */
20541
20542 static enum tag_types
20543 cp_parser_class_key (cp_parser* parser)
20544 {
20545   cp_token *token;
20546   enum tag_types tag_type;
20547
20548   /* Look for the class-key.  */
20549   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20550   if (!token)
20551     return none_type;
20552
20553   /* Check to see if the TOKEN is a class-key.  */
20554   tag_type = cp_parser_token_is_class_key (token);
20555   if (!tag_type)
20556     cp_parser_error (parser, "expected class-key");
20557   return tag_type;
20558 }
20559
20560 /* Parse a type-parameter-key.
20561
20562    type-parameter-key:
20563      class
20564      typename
20565  */
20566
20567 static void
20568 cp_parser_type_parameter_key (cp_parser* parser)
20569 {
20570   /* Look for the type-parameter-key.  */
20571   enum tag_types tag_type = none_type;
20572   cp_token *token = cp_lexer_peek_token (parser->lexer);
20573   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20574     {
20575       cp_lexer_consume_token (parser->lexer);
20576       if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20577         /* typename is not allowed in a template template parameter
20578            by the standard until C++1Z.  */
20579         pedwarn (token->location, OPT_Wpedantic, 
20580                  "ISO C++ forbids typename key in template template parameter;"
20581                  " use -std=c++1z or -std=gnu++1z");
20582     }
20583   else
20584     cp_parser_error (parser, "expected %<class%> or %<typename%>");
20585
20586   return;
20587 }
20588
20589 /* Parse an (optional) member-specification.
20590
20591    member-specification:
20592      member-declaration member-specification [opt]
20593      access-specifier : member-specification [opt]  */
20594
20595 static void
20596 cp_parser_member_specification_opt (cp_parser* parser)
20597 {
20598   while (true)
20599     {
20600       cp_token *token;
20601       enum rid keyword;
20602
20603       /* Peek at the next token.  */
20604       token = cp_lexer_peek_token (parser->lexer);
20605       /* If it's a `}', or EOF then we've seen all the members.  */
20606       if (token->type == CPP_CLOSE_BRACE
20607           || token->type == CPP_EOF
20608           || token->type == CPP_PRAGMA_EOL)
20609         break;
20610
20611       /* See if this token is a keyword.  */
20612       keyword = token->keyword;
20613       switch (keyword)
20614         {
20615         case RID_PUBLIC:
20616         case RID_PROTECTED:
20617         case RID_PRIVATE:
20618           /* Consume the access-specifier.  */
20619           cp_lexer_consume_token (parser->lexer);
20620           /* Remember which access-specifier is active.  */
20621           current_access_specifier = token->u.value;
20622           /* Look for the `:'.  */
20623           cp_parser_require (parser, CPP_COLON, RT_COLON);
20624           break;
20625
20626         default:
20627           /* Accept #pragmas at class scope.  */
20628           if (token->type == CPP_PRAGMA)
20629             {
20630               cp_parser_pragma (parser, pragma_member);
20631               break;
20632             }
20633
20634           /* Otherwise, the next construction must be a
20635              member-declaration.  */
20636           cp_parser_member_declaration (parser);
20637         }
20638     }
20639 }
20640
20641 /* Parse a member-declaration.
20642
20643    member-declaration:
20644      decl-specifier-seq [opt] member-declarator-list [opt] ;
20645      function-definition ; [opt]
20646      :: [opt] nested-name-specifier template [opt] unqualified-id ;
20647      using-declaration
20648      template-declaration
20649      alias-declaration
20650
20651    member-declarator-list:
20652      member-declarator
20653      member-declarator-list , member-declarator
20654
20655    member-declarator:
20656      declarator pure-specifier [opt]
20657      declarator constant-initializer [opt]
20658      identifier [opt] : constant-expression
20659
20660    GNU Extensions:
20661
20662    member-declaration:
20663      __extension__ member-declaration
20664
20665    member-declarator:
20666      declarator attributes [opt] pure-specifier [opt]
20667      declarator attributes [opt] constant-initializer [opt]
20668      identifier [opt] attributes [opt] : constant-expression  
20669
20670    C++0x Extensions:
20671
20672    member-declaration:
20673      static_assert-declaration  */
20674
20675 static void
20676 cp_parser_member_declaration (cp_parser* parser)
20677 {
20678   cp_decl_specifier_seq decl_specifiers;
20679   tree prefix_attributes;
20680   tree decl;
20681   int declares_class_or_enum;
20682   bool friend_p;
20683   cp_token *token = NULL;
20684   cp_token *decl_spec_token_start = NULL;
20685   cp_token *initializer_token_start = NULL;
20686   int saved_pedantic;
20687   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20688
20689   /* Check for the `__extension__' keyword.  */
20690   if (cp_parser_extension_opt (parser, &saved_pedantic))
20691     {
20692       /* Recurse.  */
20693       cp_parser_member_declaration (parser);
20694       /* Restore the old value of the PEDANTIC flag.  */
20695       pedantic = saved_pedantic;
20696
20697       return;
20698     }
20699
20700   /* Check for a template-declaration.  */
20701   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20702     {
20703       /* An explicit specialization here is an error condition, and we
20704          expect the specialization handler to detect and report this.  */
20705       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20706           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20707         cp_parser_explicit_specialization (parser);
20708       else
20709         cp_parser_template_declaration (parser, /*member_p=*/true);
20710
20711       return;
20712     }
20713
20714   /* Check for a using-declaration.  */
20715   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20716     {
20717       if (cxx_dialect < cxx11)
20718         {
20719           /* Parse the using-declaration.  */
20720           cp_parser_using_declaration (parser,
20721                                        /*access_declaration_p=*/false);
20722           return;
20723         }
20724       else
20725         {
20726           tree decl;
20727           bool alias_decl_expected;
20728           cp_parser_parse_tentatively (parser);
20729           decl = cp_parser_alias_declaration (parser);
20730           /* Note that if we actually see the '=' token after the
20731              identifier, cp_parser_alias_declaration commits the
20732              tentative parse.  In that case, we really expects an
20733              alias-declaration.  Otherwise, we expect a using
20734              declaration.  */
20735           alias_decl_expected =
20736             !cp_parser_uncommitted_to_tentative_parse_p (parser);
20737           cp_parser_parse_definitely (parser);
20738
20739           if (alias_decl_expected)
20740             finish_member_declaration (decl);
20741           else
20742             cp_parser_using_declaration (parser,
20743                                          /*access_declaration_p=*/false);
20744           return;
20745         }
20746     }
20747
20748   /* Check for @defs.  */
20749   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20750     {
20751       tree ivar, member;
20752       tree ivar_chains = cp_parser_objc_defs_expression (parser);
20753       ivar = ivar_chains;
20754       while (ivar)
20755         {
20756           member = ivar;
20757           ivar = TREE_CHAIN (member);
20758           TREE_CHAIN (member) = NULL_TREE;
20759           finish_member_declaration (member);
20760         }
20761       return;
20762     }
20763
20764   /* If the next token is `static_assert' we have a static assertion.  */
20765   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20766     {
20767       cp_parser_static_assert (parser, /*member_p=*/true);
20768       return;
20769     }
20770
20771   parser->colon_corrects_to_scope_p = false;
20772
20773   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20774       goto out;
20775
20776   /* Parse the decl-specifier-seq.  */
20777   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20778   cp_parser_decl_specifier_seq (parser,
20779                                 CP_PARSER_FLAGS_OPTIONAL,
20780                                 &decl_specifiers,
20781                                 &declares_class_or_enum);
20782   /* Check for an invalid type-name.  */
20783   if (!decl_specifiers.any_type_specifiers_p
20784       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20785     goto out;
20786   /* If there is no declarator, then the decl-specifier-seq should
20787      specify a type.  */
20788   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20789     {
20790       /* If there was no decl-specifier-seq, and the next token is a
20791          `;', then we have something like:
20792
20793            struct S { ; };
20794
20795          [class.mem]
20796
20797          Each member-declaration shall declare at least one member
20798          name of the class.  */
20799       if (!decl_specifiers.any_specifiers_p)
20800         {
20801           cp_token *token = cp_lexer_peek_token (parser->lexer);
20802           if (!in_system_header_at (token->location))
20803             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20804         }
20805       else
20806         {
20807           tree type;
20808
20809           /* See if this declaration is a friend.  */
20810           friend_p = cp_parser_friend_p (&decl_specifiers);
20811           /* If there were decl-specifiers, check to see if there was
20812              a class-declaration.  */
20813           type = check_tag_decl (&decl_specifiers,
20814                                  /*explicit_type_instantiation_p=*/false);
20815           /* Nested classes have already been added to the class, but
20816              a `friend' needs to be explicitly registered.  */
20817           if (friend_p)
20818             {
20819               /* If the `friend' keyword was present, the friend must
20820                  be introduced with a class-key.  */
20821                if (!declares_class_or_enum && cxx_dialect < cxx11)
20822                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20823                           "in C++03 a class-key must be used "
20824                           "when declaring a friend");
20825                /* In this case:
20826
20827                     template <typename T> struct A {
20828                       friend struct A<T>::B;
20829                     };
20830
20831                   A<T>::B will be represented by a TYPENAME_TYPE, and
20832                   therefore not recognized by check_tag_decl.  */
20833                if (!type)
20834                  {
20835                    type = decl_specifiers.type;
20836                    if (type && TREE_CODE (type) == TYPE_DECL)
20837                      type = TREE_TYPE (type);
20838                  }
20839                if (!type || !TYPE_P (type))
20840                  error_at (decl_spec_token_start->location,
20841                            "friend declaration does not name a class or "
20842                            "function");
20843                else
20844                  make_friend_class (current_class_type, type,
20845                                     /*complain=*/true);
20846             }
20847           /* If there is no TYPE, an error message will already have
20848              been issued.  */
20849           else if (!type || type == error_mark_node)
20850             ;
20851           /* An anonymous aggregate has to be handled specially; such
20852              a declaration really declares a data member (with a
20853              particular type), as opposed to a nested class.  */
20854           else if (ANON_AGGR_TYPE_P (type))
20855             {
20856               /* C++11 9.5/6.  */
20857               if (decl_specifiers.storage_class != sc_none)
20858                 error_at (decl_spec_token_start->location,
20859                           "a storage class on an anonymous aggregate "
20860                           "in class scope is not allowed");
20861
20862               /* Remove constructors and such from TYPE, now that we
20863                  know it is an anonymous aggregate.  */
20864               fixup_anonymous_aggr (type);
20865               /* And make the corresponding data member.  */
20866               decl = build_decl (decl_spec_token_start->location,
20867                                  FIELD_DECL, NULL_TREE, type);
20868               /* Add it to the class.  */
20869               finish_member_declaration (decl);
20870             }
20871           else
20872             cp_parser_check_access_in_redeclaration
20873                                               (TYPE_NAME (type),
20874                                                decl_spec_token_start->location);
20875         }
20876     }
20877   else
20878     {
20879       bool assume_semicolon = false;
20880
20881       /* Clear attributes from the decl_specifiers but keep them
20882          around as prefix attributes that apply them to the entity
20883          being declared.  */
20884       prefix_attributes = decl_specifiers.attributes;
20885       decl_specifiers.attributes = NULL_TREE;
20886
20887       /* See if these declarations will be friends.  */
20888       friend_p = cp_parser_friend_p (&decl_specifiers);
20889
20890       /* Keep going until we hit the `;' at the end of the
20891          declaration.  */
20892       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20893         {
20894           tree attributes = NULL_TREE;
20895           tree first_attribute;
20896
20897           /* Peek at the next token.  */
20898           token = cp_lexer_peek_token (parser->lexer);
20899
20900           /* Check for a bitfield declaration.  */
20901           if (token->type == CPP_COLON
20902               || (token->type == CPP_NAME
20903                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20904                   == CPP_COLON))
20905             {
20906               tree identifier;
20907               tree width;
20908
20909               /* Get the name of the bitfield.  Note that we cannot just
20910                  check TOKEN here because it may have been invalidated by
20911                  the call to cp_lexer_peek_nth_token above.  */
20912               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20913                 identifier = cp_parser_identifier (parser);
20914               else
20915                 identifier = NULL_TREE;
20916
20917               /* Consume the `:' token.  */
20918               cp_lexer_consume_token (parser->lexer);
20919               /* Get the width of the bitfield.  */
20920               width
20921                 = cp_parser_constant_expression (parser);
20922
20923               /* Look for attributes that apply to the bitfield.  */
20924               attributes = cp_parser_attributes_opt (parser);
20925               /* Remember which attributes are prefix attributes and
20926                  which are not.  */
20927               first_attribute = attributes;
20928               /* Combine the attributes.  */
20929               attributes = chainon (prefix_attributes, attributes);
20930
20931               /* Create the bitfield declaration.  */
20932               decl = grokbitfield (identifier
20933                                    ? make_id_declarator (NULL_TREE,
20934                                                          identifier,
20935                                                          sfk_none)
20936                                    : NULL,
20937                                    &decl_specifiers,
20938                                    width,
20939                                    attributes);
20940             }
20941           else
20942             {
20943               cp_declarator *declarator;
20944               tree initializer;
20945               tree asm_specification;
20946               int ctor_dtor_or_conv_p;
20947
20948               /* Parse the declarator.  */
20949               declarator
20950                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20951                                         &ctor_dtor_or_conv_p,
20952                                         /*parenthesized_p=*/NULL,
20953                                         /*member_p=*/true,
20954                                         friend_p);
20955
20956               /* If something went wrong parsing the declarator, make sure
20957                  that we at least consume some tokens.  */
20958               if (declarator == cp_error_declarator)
20959                 {
20960                   /* Skip to the end of the statement.  */
20961                   cp_parser_skip_to_end_of_statement (parser);
20962                   /* If the next token is not a semicolon, that is
20963                      probably because we just skipped over the body of
20964                      a function.  So, we consume a semicolon if
20965                      present, but do not issue an error message if it
20966                      is not present.  */
20967                   if (cp_lexer_next_token_is (parser->lexer,
20968                                               CPP_SEMICOLON))
20969                     cp_lexer_consume_token (parser->lexer);
20970                   goto out;
20971                 }
20972
20973               if (declares_class_or_enum & 2)
20974                 cp_parser_check_for_definition_in_return_type
20975                                             (declarator, decl_specifiers.type,
20976                                              decl_specifiers.locations[ds_type_spec]);
20977
20978               /* Look for an asm-specification.  */
20979               asm_specification = cp_parser_asm_specification_opt (parser);
20980               /* Look for attributes that apply to the declaration.  */
20981               attributes = cp_parser_attributes_opt (parser);
20982               /* Remember which attributes are prefix attributes and
20983                  which are not.  */
20984               first_attribute = attributes;
20985               /* Combine the attributes.  */
20986               attributes = chainon (prefix_attributes, attributes);
20987
20988               /* If it's an `=', then we have a constant-initializer or a
20989                  pure-specifier.  It is not correct to parse the
20990                  initializer before registering the member declaration
20991                  since the member declaration should be in scope while
20992                  its initializer is processed.  However, the rest of the
20993                  front end does not yet provide an interface that allows
20994                  us to handle this correctly.  */
20995               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20996                 {
20997                   /* In [class.mem]:
20998
20999                      A pure-specifier shall be used only in the declaration of
21000                      a virtual function.
21001
21002                      A member-declarator can contain a constant-initializer
21003                      only if it declares a static member of integral or
21004                      enumeration type.
21005
21006                      Therefore, if the DECLARATOR is for a function, we look
21007                      for a pure-specifier; otherwise, we look for a
21008                      constant-initializer.  When we call `grokfield', it will
21009                      perform more stringent semantics checks.  */
21010                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
21011                   if (function_declarator_p (declarator)
21012                       || (decl_specifiers.type
21013                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21014                           && declarator->kind == cdk_id
21015                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21016                               == FUNCTION_TYPE)))
21017                     initializer = cp_parser_pure_specifier (parser);
21018                   else if (decl_specifiers.storage_class != sc_static)
21019                     initializer = cp_parser_save_nsdmi (parser);
21020                   else if (cxx_dialect >= cxx11)
21021                     {
21022                       bool nonconst;
21023                       /* Don't require a constant rvalue in C++11, since we
21024                          might want a reference constant.  We'll enforce
21025                          constancy later.  */
21026                       cp_lexer_consume_token (parser->lexer);
21027                       /* Parse the initializer.  */
21028                       initializer = cp_parser_initializer_clause (parser,
21029                                                                   &nonconst);
21030                     }
21031                   else
21032                     /* Parse the initializer.  */
21033                     initializer = cp_parser_constant_initializer (parser);
21034                 }
21035               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21036                        && !function_declarator_p (declarator))
21037                 {
21038                   bool x;
21039                   if (decl_specifiers.storage_class != sc_static)
21040                     initializer = cp_parser_save_nsdmi (parser);
21041                   else
21042                     initializer = cp_parser_initializer (parser, &x, &x);
21043                 }
21044               /* Otherwise, there is no initializer.  */
21045               else
21046                 initializer = NULL_TREE;
21047
21048               /* See if we are probably looking at a function
21049                  definition.  We are certainly not looking at a
21050                  member-declarator.  Calling `grokfield' has
21051                  side-effects, so we must not do it unless we are sure
21052                  that we are looking at a member-declarator.  */
21053               if (cp_parser_token_starts_function_definition_p
21054                   (cp_lexer_peek_token (parser->lexer)))
21055                 {
21056                   /* The grammar does not allow a pure-specifier to be
21057                      used when a member function is defined.  (It is
21058                      possible that this fact is an oversight in the
21059                      standard, since a pure function may be defined
21060                      outside of the class-specifier.  */
21061                   if (initializer && initializer_token_start)
21062                     error_at (initializer_token_start->location,
21063                               "pure-specifier on function-definition");
21064                   decl = cp_parser_save_member_function_body (parser,
21065                                                               &decl_specifiers,
21066                                                               declarator,
21067                                                               attributes);
21068                   if (parser->fully_implicit_function_template_p)
21069                     decl = finish_fully_implicit_template (parser, decl);
21070                   /* If the member was not a friend, declare it here.  */
21071                   if (!friend_p)
21072                     finish_member_declaration (decl);
21073                   /* Peek at the next token.  */
21074                   token = cp_lexer_peek_token (parser->lexer);
21075                   /* If the next token is a semicolon, consume it.  */
21076                   if (token->type == CPP_SEMICOLON)
21077                     cp_lexer_consume_token (parser->lexer);
21078                   goto out;
21079                 }
21080               else
21081                 if (declarator->kind == cdk_function)
21082                   declarator->id_loc = token->location;
21083               /* Create the declaration.  */
21084               decl = grokfield (declarator, &decl_specifiers,
21085                                 initializer, /*init_const_expr_p=*/true,
21086                                 asm_specification, attributes);
21087               if (parser->fully_implicit_function_template_p)
21088                 {
21089                   if (friend_p)
21090                     finish_fully_implicit_template (parser, 0);
21091                   else
21092                     decl = finish_fully_implicit_template (parser, decl);
21093                 }
21094             }
21095
21096           cp_finalize_omp_declare_simd (parser, decl);
21097
21098           /* Reset PREFIX_ATTRIBUTES.  */
21099           while (attributes && TREE_CHAIN (attributes) != first_attribute)
21100             attributes = TREE_CHAIN (attributes);
21101           if (attributes)
21102             TREE_CHAIN (attributes) = NULL_TREE;
21103
21104           /* If there is any qualification still in effect, clear it
21105              now; we will be starting fresh with the next declarator.  */
21106           parser->scope = NULL_TREE;
21107           parser->qualifying_scope = NULL_TREE;
21108           parser->object_scope = NULL_TREE;
21109           /* If it's a `,', then there are more declarators.  */
21110           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21111             {
21112               cp_lexer_consume_token (parser->lexer);
21113               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21114                 {
21115                   cp_token *token = cp_lexer_previous_token (parser->lexer);
21116                   error_at (token->location,
21117                             "stray %<,%> at end of member declaration");
21118                 }
21119             }
21120           /* If the next token isn't a `;', then we have a parse error.  */
21121           else if (cp_lexer_next_token_is_not (parser->lexer,
21122                                                CPP_SEMICOLON))
21123             {
21124               /* The next token might be a ways away from where the
21125                  actual semicolon is missing.  Find the previous token
21126                  and use that for our error position.  */
21127               cp_token *token = cp_lexer_previous_token (parser->lexer);
21128               error_at (token->location,
21129                         "expected %<;%> at end of member declaration");
21130
21131               /* Assume that the user meant to provide a semicolon.  If
21132                  we were to cp_parser_skip_to_end_of_statement, we might
21133                  skip to a semicolon inside a member function definition
21134                  and issue nonsensical error messages.  */
21135               assume_semicolon = true;
21136             }
21137
21138           if (decl)
21139             {
21140               /* Add DECL to the list of members.  */
21141               if (!friend_p
21142                   /* Explicitly include, eg, NSDMIs, for better error
21143                      recovery (c++/58650).  */
21144                   || !DECL_DECLARES_FUNCTION_P (decl))
21145                 finish_member_declaration (decl);
21146
21147               if (TREE_CODE (decl) == FUNCTION_DECL)
21148                 cp_parser_save_default_args (parser, decl);
21149               else if (TREE_CODE (decl) == FIELD_DECL
21150                        && !DECL_C_BIT_FIELD (decl)
21151                        && DECL_INITIAL (decl))
21152                 /* Add DECL to the queue of NSDMI to be parsed later.  */
21153                 vec_safe_push (unparsed_nsdmis, decl);
21154             }
21155
21156           if (assume_semicolon)
21157             goto out;
21158         }
21159     }
21160
21161   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21162  out:
21163   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21164 }
21165
21166 /* Parse a pure-specifier.
21167
21168    pure-specifier:
21169      = 0
21170
21171    Returns INTEGER_ZERO_NODE if a pure specifier is found.
21172    Otherwise, ERROR_MARK_NODE is returned.  */
21173
21174 static tree
21175 cp_parser_pure_specifier (cp_parser* parser)
21176 {
21177   cp_token *token;
21178
21179   /* Look for the `=' token.  */
21180   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21181     return error_mark_node;
21182   /* Look for the `0' token.  */
21183   token = cp_lexer_peek_token (parser->lexer);
21184
21185   if (token->type == CPP_EOF
21186       || token->type == CPP_PRAGMA_EOL)
21187     return error_mark_node;
21188
21189   cp_lexer_consume_token (parser->lexer);
21190
21191   /* Accept = default or = delete in c++0x mode.  */
21192   if (token->keyword == RID_DEFAULT
21193       || token->keyword == RID_DELETE)
21194     {
21195       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21196       return token->u.value;
21197     }
21198
21199   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
21200   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21201     {
21202       cp_parser_error (parser,
21203                        "invalid pure specifier (only %<= 0%> is allowed)");
21204       cp_parser_skip_to_end_of_statement (parser);
21205       return error_mark_node;
21206     }
21207   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21208     {
21209       error_at (token->location, "templates may not be %<virtual%>");
21210       return error_mark_node;
21211     }
21212
21213   return integer_zero_node;
21214 }
21215
21216 /* Parse a constant-initializer.
21217
21218    constant-initializer:
21219      = constant-expression
21220
21221    Returns a representation of the constant-expression.  */
21222
21223 static tree
21224 cp_parser_constant_initializer (cp_parser* parser)
21225 {
21226   /* Look for the `=' token.  */
21227   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21228     return error_mark_node;
21229
21230   /* It is invalid to write:
21231
21232        struct S { static const int i = { 7 }; };
21233
21234      */
21235   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21236     {
21237       cp_parser_error (parser,
21238                        "a brace-enclosed initializer is not allowed here");
21239       /* Consume the opening brace.  */
21240       cp_lexer_consume_token (parser->lexer);
21241       /* Skip the initializer.  */
21242       cp_parser_skip_to_closing_brace (parser);
21243       /* Look for the trailing `}'.  */
21244       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21245
21246       return error_mark_node;
21247     }
21248
21249   return cp_parser_constant_expression (parser);
21250 }
21251
21252 /* Derived classes [gram.class.derived] */
21253
21254 /* Parse a base-clause.
21255
21256    base-clause:
21257      : base-specifier-list
21258
21259    base-specifier-list:
21260      base-specifier ... [opt]
21261      base-specifier-list , base-specifier ... [opt]
21262
21263    Returns a TREE_LIST representing the base-classes, in the order in
21264    which they were declared.  The representation of each node is as
21265    described by cp_parser_base_specifier.
21266
21267    In the case that no bases are specified, this function will return
21268    NULL_TREE, not ERROR_MARK_NODE.  */
21269
21270 static tree
21271 cp_parser_base_clause (cp_parser* parser)
21272 {
21273   tree bases = NULL_TREE;
21274
21275   /* Look for the `:' that begins the list.  */
21276   cp_parser_require (parser, CPP_COLON, RT_COLON);
21277
21278   /* Scan the base-specifier-list.  */
21279   while (true)
21280     {
21281       cp_token *token;
21282       tree base;
21283       bool pack_expansion_p = false;
21284
21285       /* Look for the base-specifier.  */
21286       base = cp_parser_base_specifier (parser);
21287       /* Look for the (optional) ellipsis. */
21288       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21289         {
21290           /* Consume the `...'. */
21291           cp_lexer_consume_token (parser->lexer);
21292
21293           pack_expansion_p = true;
21294         }
21295
21296       /* Add BASE to the front of the list.  */
21297       if (base && base != error_mark_node)
21298         {
21299           if (pack_expansion_p)
21300             /* Make this a pack expansion type. */
21301             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21302
21303           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21304             {
21305               TREE_CHAIN (base) = bases;
21306               bases = base;
21307             }
21308         }
21309       /* Peek at the next token.  */
21310       token = cp_lexer_peek_token (parser->lexer);
21311       /* If it's not a comma, then the list is complete.  */
21312       if (token->type != CPP_COMMA)
21313         break;
21314       /* Consume the `,'.  */
21315       cp_lexer_consume_token (parser->lexer);
21316     }
21317
21318   /* PARSER->SCOPE may still be non-NULL at this point, if the last
21319      base class had a qualified name.  However, the next name that
21320      appears is certainly not qualified.  */
21321   parser->scope = NULL_TREE;
21322   parser->qualifying_scope = NULL_TREE;
21323   parser->object_scope = NULL_TREE;
21324
21325   return nreverse (bases);
21326 }
21327
21328 /* Parse a base-specifier.
21329
21330    base-specifier:
21331      :: [opt] nested-name-specifier [opt] class-name
21332      virtual access-specifier [opt] :: [opt] nested-name-specifier
21333        [opt] class-name
21334      access-specifier virtual [opt] :: [opt] nested-name-specifier
21335        [opt] class-name
21336
21337    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
21338    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21339    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
21340    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
21341
21342 static tree
21343 cp_parser_base_specifier (cp_parser* parser)
21344 {
21345   cp_token *token;
21346   bool done = false;
21347   bool virtual_p = false;
21348   bool duplicate_virtual_error_issued_p = false;
21349   bool duplicate_access_error_issued_p = false;
21350   bool class_scope_p, template_p;
21351   tree access = access_default_node;
21352   tree type;
21353
21354   /* Process the optional `virtual' and `access-specifier'.  */
21355   while (!done)
21356     {
21357       /* Peek at the next token.  */
21358       token = cp_lexer_peek_token (parser->lexer);
21359       /* Process `virtual'.  */
21360       switch (token->keyword)
21361         {
21362         case RID_VIRTUAL:
21363           /* If `virtual' appears more than once, issue an error.  */
21364           if (virtual_p && !duplicate_virtual_error_issued_p)
21365             {
21366               cp_parser_error (parser,
21367                                "%<virtual%> specified more than once in base-specified");
21368               duplicate_virtual_error_issued_p = true;
21369             }
21370
21371           virtual_p = true;
21372
21373           /* Consume the `virtual' token.  */
21374           cp_lexer_consume_token (parser->lexer);
21375
21376           break;
21377
21378         case RID_PUBLIC:
21379         case RID_PROTECTED:
21380         case RID_PRIVATE:
21381           /* If more than one access specifier appears, issue an
21382              error.  */
21383           if (access != access_default_node
21384               && !duplicate_access_error_issued_p)
21385             {
21386               cp_parser_error (parser,
21387                                "more than one access specifier in base-specified");
21388               duplicate_access_error_issued_p = true;
21389             }
21390
21391           access = ridpointers[(int) token->keyword];
21392
21393           /* Consume the access-specifier.  */
21394           cp_lexer_consume_token (parser->lexer);
21395
21396           break;
21397
21398         default:
21399           done = true;
21400           break;
21401         }
21402     }
21403   /* It is not uncommon to see programs mechanically, erroneously, use
21404      the 'typename' keyword to denote (dependent) qualified types
21405      as base classes.  */
21406   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21407     {
21408       token = cp_lexer_peek_token (parser->lexer);
21409       if (!processing_template_decl)
21410         error_at (token->location,
21411                   "keyword %<typename%> not allowed outside of templates");
21412       else
21413         error_at (token->location,
21414                   "keyword %<typename%> not allowed in this context "
21415                   "(the base class is implicitly a type)");
21416       cp_lexer_consume_token (parser->lexer);
21417     }
21418
21419   /* Look for the optional `::' operator.  */
21420   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21421   /* Look for the nested-name-specifier.  The simplest way to
21422      implement:
21423
21424        [temp.res]
21425
21426        The keyword `typename' is not permitted in a base-specifier or
21427        mem-initializer; in these contexts a qualified name that
21428        depends on a template-parameter is implicitly assumed to be a
21429        type name.
21430
21431      is to pretend that we have seen the `typename' keyword at this
21432      point.  */
21433   cp_parser_nested_name_specifier_opt (parser,
21434                                        /*typename_keyword_p=*/true,
21435                                        /*check_dependency_p=*/true,
21436                                        typename_type,
21437                                        /*is_declaration=*/true);
21438   /* If the base class is given by a qualified name, assume that names
21439      we see are type names or templates, as appropriate.  */
21440   class_scope_p = (parser->scope && TYPE_P (parser->scope));
21441   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21442
21443   if (!parser->scope
21444       && cp_lexer_next_token_is_decltype (parser->lexer))
21445     /* DR 950 allows decltype as a base-specifier.  */
21446     type = cp_parser_decltype (parser);
21447   else
21448     {
21449       /* Otherwise, look for the class-name.  */
21450       type = cp_parser_class_name (parser,
21451                                    class_scope_p,
21452                                    template_p,
21453                                    typename_type,
21454                                    /*check_dependency_p=*/true,
21455                                    /*class_head_p=*/false,
21456                                    /*is_declaration=*/true);
21457       type = TREE_TYPE (type);
21458     }
21459
21460   if (type == error_mark_node)
21461     return error_mark_node;
21462
21463   return finish_base_specifier (type, access, virtual_p);
21464 }
21465
21466 /* Exception handling [gram.exception] */
21467
21468 /* Parse an (optional) noexcept-specification.
21469
21470    noexcept-specification:
21471      noexcept ( constant-expression ) [opt]
21472
21473    If no noexcept-specification is present, returns NULL_TREE.
21474    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21475    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21476    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
21477    Otherwise, returns a noexcept specification unless RETURN_COND is true,
21478    in which case a boolean condition is returned instead.  */
21479
21480 static tree
21481 cp_parser_noexcept_specification_opt (cp_parser* parser,
21482                                       bool require_constexpr,
21483                                       bool* consumed_expr,
21484                                       bool return_cond)
21485 {
21486   cp_token *token;
21487   const char *saved_message;
21488
21489   /* Peek at the next token.  */
21490   token = cp_lexer_peek_token (parser->lexer);
21491
21492   /* Is it a noexcept-specification?  */
21493   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21494     {
21495       tree expr;
21496       cp_lexer_consume_token (parser->lexer);
21497
21498       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21499         {
21500           cp_lexer_consume_token (parser->lexer);
21501
21502           if (require_constexpr)
21503             {
21504               /* Types may not be defined in an exception-specification.  */
21505               saved_message = parser->type_definition_forbidden_message;
21506               parser->type_definition_forbidden_message
21507               = G_("types may not be defined in an exception-specification");
21508
21509               expr = cp_parser_constant_expression (parser);
21510
21511               /* Restore the saved message.  */
21512               parser->type_definition_forbidden_message = saved_message;
21513             }
21514           else
21515             {
21516               expr = cp_parser_expression (parser);
21517               *consumed_expr = true;
21518             }
21519
21520           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21521         }
21522       else
21523         {
21524           expr = boolean_true_node;
21525           if (!require_constexpr)
21526             *consumed_expr = false;
21527         }
21528
21529       /* We cannot build a noexcept-spec right away because this will check
21530          that expr is a constexpr.  */
21531       if (!return_cond)
21532         return build_noexcept_spec (expr, tf_warning_or_error);
21533       else
21534         return expr;
21535     }
21536   else
21537     return NULL_TREE;
21538 }
21539
21540 /* Parse an (optional) exception-specification.
21541
21542    exception-specification:
21543      throw ( type-id-list [opt] )
21544
21545    Returns a TREE_LIST representing the exception-specification.  The
21546    TREE_VALUE of each node is a type.  */
21547
21548 static tree
21549 cp_parser_exception_specification_opt (cp_parser* parser)
21550 {
21551   cp_token *token;
21552   tree type_id_list;
21553   const char *saved_message;
21554
21555   /* Peek at the next token.  */
21556   token = cp_lexer_peek_token (parser->lexer);
21557
21558   /* Is it a noexcept-specification?  */
21559   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21560                                                       false);
21561   if (type_id_list != NULL_TREE)
21562     return type_id_list;
21563
21564   /* If it's not `throw', then there's no exception-specification.  */
21565   if (!cp_parser_is_keyword (token, RID_THROW))
21566     return NULL_TREE;
21567
21568 #if 0
21569   /* Enable this once a lot of code has transitioned to noexcept?  */
21570   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21571     warning (OPT_Wdeprecated, "dynamic exception specifications are "
21572              "deprecated in C++0x; use %<noexcept%> instead");
21573 #endif
21574
21575   /* Consume the `throw'.  */
21576   cp_lexer_consume_token (parser->lexer);
21577
21578   /* Look for the `('.  */
21579   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21580
21581   /* Peek at the next token.  */
21582   token = cp_lexer_peek_token (parser->lexer);
21583   /* If it's not a `)', then there is a type-id-list.  */
21584   if (token->type != CPP_CLOSE_PAREN)
21585     {
21586       /* Types may not be defined in an exception-specification.  */
21587       saved_message = parser->type_definition_forbidden_message;
21588       parser->type_definition_forbidden_message
21589         = G_("types may not be defined in an exception-specification");
21590       /* Parse the type-id-list.  */
21591       type_id_list = cp_parser_type_id_list (parser);
21592       /* Restore the saved message.  */
21593       parser->type_definition_forbidden_message = saved_message;
21594     }
21595   else
21596     type_id_list = empty_except_spec;
21597
21598   /* Look for the `)'.  */
21599   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21600
21601   return type_id_list;
21602 }
21603
21604 /* Parse an (optional) type-id-list.
21605
21606    type-id-list:
21607      type-id ... [opt]
21608      type-id-list , type-id ... [opt]
21609
21610    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
21611    in the order that the types were presented.  */
21612
21613 static tree
21614 cp_parser_type_id_list (cp_parser* parser)
21615 {
21616   tree types = NULL_TREE;
21617
21618   while (true)
21619     {
21620       cp_token *token;
21621       tree type;
21622
21623       /* Get the next type-id.  */
21624       type = cp_parser_type_id (parser);
21625       /* Parse the optional ellipsis. */
21626       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21627         {
21628           /* Consume the `...'. */
21629           cp_lexer_consume_token (parser->lexer);
21630
21631           /* Turn the type into a pack expansion expression. */
21632           type = make_pack_expansion (type);
21633         }
21634       /* Add it to the list.  */
21635       types = add_exception_specifier (types, type, /*complain=*/1);
21636       /* Peek at the next token.  */
21637       token = cp_lexer_peek_token (parser->lexer);
21638       /* If it is not a `,', we are done.  */
21639       if (token->type != CPP_COMMA)
21640         break;
21641       /* Consume the `,'.  */
21642       cp_lexer_consume_token (parser->lexer);
21643     }
21644
21645   return nreverse (types);
21646 }
21647
21648 /* Parse a try-block.
21649
21650    try-block:
21651      try compound-statement handler-seq  */
21652
21653 static tree
21654 cp_parser_try_block (cp_parser* parser)
21655 {
21656   tree try_block;
21657
21658   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21659   if (parser->in_function_body
21660       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21661     error ("%<try%> in %<constexpr%> function");
21662
21663   try_block = begin_try_block ();
21664   cp_parser_compound_statement (parser, NULL, true, false);
21665   finish_try_block (try_block);
21666   cp_parser_handler_seq (parser);
21667   finish_handler_sequence (try_block);
21668
21669   return try_block;
21670 }
21671
21672 /* Parse a function-try-block.
21673
21674    function-try-block:
21675      try ctor-initializer [opt] function-body handler-seq  */
21676
21677 static bool
21678 cp_parser_function_try_block (cp_parser* parser)
21679 {
21680   tree compound_stmt;
21681   tree try_block;
21682   bool ctor_initializer_p;
21683
21684   /* Look for the `try' keyword.  */
21685   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21686     return false;
21687   /* Let the rest of the front end know where we are.  */
21688   try_block = begin_function_try_block (&compound_stmt);
21689   /* Parse the function-body.  */
21690   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21691     (parser, /*in_function_try_block=*/true);
21692   /* We're done with the `try' part.  */
21693   finish_function_try_block (try_block);
21694   /* Parse the handlers.  */
21695   cp_parser_handler_seq (parser);
21696   /* We're done with the handlers.  */
21697   finish_function_handler_sequence (try_block, compound_stmt);
21698
21699   return ctor_initializer_p;
21700 }
21701
21702 /* Parse a handler-seq.
21703
21704    handler-seq:
21705      handler handler-seq [opt]  */
21706
21707 static void
21708 cp_parser_handler_seq (cp_parser* parser)
21709 {
21710   while (true)
21711     {
21712       cp_token *token;
21713
21714       /* Parse the handler.  */
21715       cp_parser_handler (parser);
21716       /* Peek at the next token.  */
21717       token = cp_lexer_peek_token (parser->lexer);
21718       /* If it's not `catch' then there are no more handlers.  */
21719       if (!cp_parser_is_keyword (token, RID_CATCH))
21720         break;
21721     }
21722 }
21723
21724 /* Parse a handler.
21725
21726    handler:
21727      catch ( exception-declaration ) compound-statement  */
21728
21729 static void
21730 cp_parser_handler (cp_parser* parser)
21731 {
21732   tree handler;
21733   tree declaration;
21734
21735   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21736   handler = begin_handler ();
21737   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21738   declaration = cp_parser_exception_declaration (parser);
21739   finish_handler_parms (declaration, handler);
21740   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21741   cp_parser_compound_statement (parser, NULL, false, false);
21742   finish_handler (handler);
21743 }
21744
21745 /* Parse an exception-declaration.
21746
21747    exception-declaration:
21748      type-specifier-seq declarator
21749      type-specifier-seq abstract-declarator
21750      type-specifier-seq
21751      ...
21752
21753    Returns a VAR_DECL for the declaration, or NULL_TREE if the
21754    ellipsis variant is used.  */
21755
21756 static tree
21757 cp_parser_exception_declaration (cp_parser* parser)
21758 {
21759   cp_decl_specifier_seq type_specifiers;
21760   cp_declarator *declarator;
21761   const char *saved_message;
21762
21763   /* If it's an ellipsis, it's easy to handle.  */
21764   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21765     {
21766       /* Consume the `...' token.  */
21767       cp_lexer_consume_token (parser->lexer);
21768       return NULL_TREE;
21769     }
21770
21771   /* Types may not be defined in exception-declarations.  */
21772   saved_message = parser->type_definition_forbidden_message;
21773   parser->type_definition_forbidden_message
21774     = G_("types may not be defined in exception-declarations");
21775
21776   /* Parse the type-specifier-seq.  */
21777   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21778                                 /*is_trailing_return=*/false,
21779                                 &type_specifiers);
21780   /* If it's a `)', then there is no declarator.  */
21781   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21782     declarator = NULL;
21783   else
21784     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21785                                        /*ctor_dtor_or_conv_p=*/NULL,
21786                                        /*parenthesized_p=*/NULL,
21787                                        /*member_p=*/false,
21788                                        /*friend_p=*/false);
21789
21790   /* Restore the saved message.  */
21791   parser->type_definition_forbidden_message = saved_message;
21792
21793   if (!type_specifiers.any_specifiers_p)
21794     return error_mark_node;
21795
21796   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21797 }
21798
21799 /* Parse a throw-expression.
21800
21801    throw-expression:
21802      throw assignment-expression [opt]
21803
21804    Returns a THROW_EXPR representing the throw-expression.  */
21805
21806 static tree
21807 cp_parser_throw_expression (cp_parser* parser)
21808 {
21809   tree expression;
21810   cp_token* token;
21811
21812   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21813   token = cp_lexer_peek_token (parser->lexer);
21814   /* Figure out whether or not there is an assignment-expression
21815      following the "throw" keyword.  */
21816   if (token->type == CPP_COMMA
21817       || token->type == CPP_SEMICOLON
21818       || token->type == CPP_CLOSE_PAREN
21819       || token->type == CPP_CLOSE_SQUARE
21820       || token->type == CPP_CLOSE_BRACE
21821       || token->type == CPP_COLON)
21822     expression = NULL_TREE;
21823   else
21824     expression = cp_parser_assignment_expression (parser);
21825
21826   return build_throw (expression);
21827 }
21828
21829 /* GNU Extensions */
21830
21831 /* Parse an (optional) asm-specification.
21832
21833    asm-specification:
21834      asm ( string-literal )
21835
21836    If the asm-specification is present, returns a STRING_CST
21837    corresponding to the string-literal.  Otherwise, returns
21838    NULL_TREE.  */
21839
21840 static tree
21841 cp_parser_asm_specification_opt (cp_parser* parser)
21842 {
21843   cp_token *token;
21844   tree asm_specification;
21845
21846   /* Peek at the next token.  */
21847   token = cp_lexer_peek_token (parser->lexer);
21848   /* If the next token isn't the `asm' keyword, then there's no
21849      asm-specification.  */
21850   if (!cp_parser_is_keyword (token, RID_ASM))
21851     return NULL_TREE;
21852
21853   /* Consume the `asm' token.  */
21854   cp_lexer_consume_token (parser->lexer);
21855   /* Look for the `('.  */
21856   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21857
21858   /* Look for the string-literal.  */
21859   asm_specification = cp_parser_string_literal (parser, false, false);
21860
21861   /* Look for the `)'.  */
21862   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21863
21864   return asm_specification;
21865 }
21866
21867 /* Parse an asm-operand-list.
21868
21869    asm-operand-list:
21870      asm-operand
21871      asm-operand-list , asm-operand
21872
21873    asm-operand:
21874      string-literal ( expression )
21875      [ string-literal ] string-literal ( expression )
21876
21877    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
21878    each node is the expression.  The TREE_PURPOSE is itself a
21879    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21880    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21881    is a STRING_CST for the string literal before the parenthesis. Returns
21882    ERROR_MARK_NODE if any of the operands are invalid.  */
21883
21884 static tree
21885 cp_parser_asm_operand_list (cp_parser* parser)
21886 {
21887   tree asm_operands = NULL_TREE;
21888   bool invalid_operands = false;
21889
21890   while (true)
21891     {
21892       tree string_literal;
21893       tree expression;
21894       tree name;
21895
21896       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21897         {
21898           /* Consume the `[' token.  */
21899           cp_lexer_consume_token (parser->lexer);
21900           /* Read the operand name.  */
21901           name = cp_parser_identifier (parser);
21902           if (name != error_mark_node)
21903             name = build_string (IDENTIFIER_LENGTH (name),
21904                                  IDENTIFIER_POINTER (name));
21905           /* Look for the closing `]'.  */
21906           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21907         }
21908       else
21909         name = NULL_TREE;
21910       /* Look for the string-literal.  */
21911       string_literal = cp_parser_string_literal (parser, false, false);
21912
21913       /* Look for the `('.  */
21914       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21915       /* Parse the expression.  */
21916       expression = cp_parser_expression (parser);
21917       /* Look for the `)'.  */
21918       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21919
21920       if (name == error_mark_node 
21921           || string_literal == error_mark_node 
21922           || expression == error_mark_node)
21923         invalid_operands = true;
21924
21925       /* Add this operand to the list.  */
21926       asm_operands = tree_cons (build_tree_list (name, string_literal),
21927                                 expression,
21928                                 asm_operands);
21929       /* If the next token is not a `,', there are no more
21930          operands.  */
21931       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21932         break;
21933       /* Consume the `,'.  */
21934       cp_lexer_consume_token (parser->lexer);
21935     }
21936
21937   return invalid_operands ? error_mark_node : nreverse (asm_operands);
21938 }
21939
21940 /* Parse an asm-clobber-list.
21941
21942    asm-clobber-list:
21943      string-literal
21944      asm-clobber-list , string-literal
21945
21946    Returns a TREE_LIST, indicating the clobbers in the order that they
21947    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
21948
21949 static tree
21950 cp_parser_asm_clobber_list (cp_parser* parser)
21951 {
21952   tree clobbers = NULL_TREE;
21953
21954   while (true)
21955     {
21956       tree string_literal;
21957
21958       /* Look for the string literal.  */
21959       string_literal = cp_parser_string_literal (parser, false, false);
21960       /* Add it to the list.  */
21961       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21962       /* If the next token is not a `,', then the list is
21963          complete.  */
21964       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21965         break;
21966       /* Consume the `,' token.  */
21967       cp_lexer_consume_token (parser->lexer);
21968     }
21969
21970   return clobbers;
21971 }
21972
21973 /* Parse an asm-label-list.
21974
21975    asm-label-list:
21976      identifier
21977      asm-label-list , identifier
21978
21979    Returns a TREE_LIST, indicating the labels in the order that they
21980    appeared.  The TREE_VALUE of each node is a label.  */
21981
21982 static tree
21983 cp_parser_asm_label_list (cp_parser* parser)
21984 {
21985   tree labels = NULL_TREE;
21986
21987   while (true)
21988     {
21989       tree identifier, label, name;
21990
21991       /* Look for the identifier.  */
21992       identifier = cp_parser_identifier (parser);
21993       if (!error_operand_p (identifier))
21994         {
21995           label = lookup_label (identifier);
21996           if (TREE_CODE (label) == LABEL_DECL)
21997             {
21998               TREE_USED (label) = 1;
21999               check_goto (label);
22000               name = build_string (IDENTIFIER_LENGTH (identifier),
22001                                    IDENTIFIER_POINTER (identifier));
22002               labels = tree_cons (name, label, labels);
22003             }
22004         }
22005       /* If the next token is not a `,', then the list is
22006          complete.  */
22007       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22008         break;
22009       /* Consume the `,' token.  */
22010       cp_lexer_consume_token (parser->lexer);
22011     }
22012
22013   return nreverse (labels);
22014 }
22015
22016 /* Return TRUE iff the next tokens in the stream are possibly the
22017    beginning of a GNU extension attribute. */
22018
22019 static bool
22020 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22021 {
22022   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22023 }
22024
22025 /* Return TRUE iff the next tokens in the stream are possibly the
22026    beginning of a standard C++-11 attribute specifier.  */
22027
22028 static bool
22029 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22030 {
22031   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22032 }
22033
22034 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22035    beginning of a standard C++-11 attribute specifier.  */
22036
22037 static bool
22038 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22039 {
22040   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22041
22042   return (cxx_dialect >= cxx11
22043           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22044               || (token->type == CPP_OPEN_SQUARE
22045                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22046                   && token->type == CPP_OPEN_SQUARE)));
22047 }
22048
22049 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22050    beginning of a GNU extension attribute.  */
22051
22052 static bool
22053 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22054 {
22055   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22056
22057   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22058 }
22059
22060 /* Return true iff the next tokens can be the beginning of either a
22061    GNU attribute list, or a standard C++11 attribute sequence.  */
22062
22063 static bool
22064 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22065 {
22066   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22067           || cp_next_tokens_can_be_std_attribute_p (parser));
22068 }
22069
22070 /* Return true iff the next Nth tokens can be the beginning of either
22071    a GNU attribute list, or a standard C++11 attribute sequence.  */
22072
22073 static bool
22074 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22075 {
22076   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22077           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22078 }
22079
22080 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22081    of GNU attributes, or return NULL.  */
22082
22083 static tree
22084 cp_parser_attributes_opt (cp_parser *parser)
22085 {
22086   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22087       return cp_parser_gnu_attributes_opt (parser);
22088   return cp_parser_std_attribute_spec_seq (parser);
22089 }
22090
22091 #define CILK_SIMD_FN_CLAUSE_MASK                                  \
22092         ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)   \
22093          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)       \
22094          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)      \
22095          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)         \
22096          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22097
22098 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
22099    vector [(<clauses>)]  */
22100
22101 static void
22102 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22103 {  
22104   bool first_p = parser->cilk_simd_fn_info == NULL;
22105   cp_token *token = v_token;
22106   if (first_p)
22107     {
22108       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22109       parser->cilk_simd_fn_info->error_seen = false;
22110       parser->cilk_simd_fn_info->fndecl_seen = false;
22111       parser->cilk_simd_fn_info->tokens = vNULL;
22112     }
22113   int paren_scope = 0;
22114   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22115     {
22116       cp_lexer_consume_token (parser->lexer);
22117       v_token = cp_lexer_peek_token (parser->lexer);
22118       paren_scope++;
22119     }
22120   while (paren_scope > 0)
22121     {
22122       token = cp_lexer_peek_token (parser->lexer);
22123       if (token->type == CPP_OPEN_PAREN)
22124         paren_scope++;
22125       else if (token->type == CPP_CLOSE_PAREN)
22126         paren_scope--;
22127       /* Do not push the last ')'  */
22128       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22129         cp_lexer_consume_token (parser->lexer);
22130     }
22131
22132   token->type = CPP_PRAGMA_EOL;
22133   parser->lexer->next_token = token;
22134   cp_lexer_consume_token (parser->lexer);
22135
22136   struct cp_token_cache *cp
22137     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22138   parser->cilk_simd_fn_info->tokens.safe_push (cp);
22139 }
22140
22141 /* Parse an (optional) series of attributes.
22142
22143    attributes:
22144      attributes attribute
22145
22146    attribute:
22147      __attribute__ (( attribute-list [opt] ))
22148
22149    The return value is as for cp_parser_gnu_attribute_list.  */
22150
22151 static tree
22152 cp_parser_gnu_attributes_opt (cp_parser* parser)
22153 {
22154   tree attributes = NULL_TREE;
22155
22156   while (true)
22157     {
22158       cp_token *token;
22159       tree attribute_list;
22160       bool ok = true;
22161
22162       /* Peek at the next token.  */
22163       token = cp_lexer_peek_token (parser->lexer);
22164       /* If it's not `__attribute__', then we're done.  */
22165       if (token->keyword != RID_ATTRIBUTE)
22166         break;
22167
22168       /* Consume the `__attribute__' keyword.  */
22169       cp_lexer_consume_token (parser->lexer);
22170       /* Look for the two `(' tokens.  */
22171       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22172       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22173
22174       /* Peek at the next token.  */
22175       token = cp_lexer_peek_token (parser->lexer);
22176       if (token->type != CPP_CLOSE_PAREN)
22177         /* Parse the attribute-list.  */
22178         attribute_list = cp_parser_gnu_attribute_list (parser);
22179       else
22180         /* If the next token is a `)', then there is no attribute
22181            list.  */
22182         attribute_list = NULL;
22183
22184       /* Look for the two `)' tokens.  */
22185       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22186         ok = false;
22187       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22188         ok = false;
22189       if (!ok)
22190         cp_parser_skip_to_end_of_statement (parser);
22191
22192       /* Add these new attributes to the list.  */
22193       attributes = chainon (attributes, attribute_list);
22194     }
22195
22196   return attributes;
22197 }
22198
22199 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22200    "__vector" or "__vector__."  */
22201
22202 static inline bool
22203 is_cilkplus_vector_p (tree name)
22204
22205   if (flag_cilkplus && is_attribute_p ("vector", name)) 
22206     return true;
22207   return false;
22208 }
22209
22210 /* Parse a GNU attribute-list.
22211
22212    attribute-list:
22213      attribute
22214      attribute-list , attribute
22215
22216    attribute:
22217      identifier
22218      identifier ( identifier )
22219      identifier ( identifier , expression-list )
22220      identifier ( expression-list )
22221
22222    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
22223    to an attribute.  The TREE_PURPOSE of each node is the identifier
22224    indicating which attribute is in use.  The TREE_VALUE represents
22225    the arguments, if any.  */
22226
22227 static tree
22228 cp_parser_gnu_attribute_list (cp_parser* parser)
22229 {
22230   tree attribute_list = NULL_TREE;
22231   bool save_translate_strings_p = parser->translate_strings_p;
22232
22233   parser->translate_strings_p = false;
22234   while (true)
22235     {
22236       cp_token *token;
22237       tree identifier;
22238       tree attribute;
22239
22240       /* Look for the identifier.  We also allow keywords here; for
22241          example `__attribute__ ((const))' is legal.  */
22242       token = cp_lexer_peek_token (parser->lexer);
22243       if (token->type == CPP_NAME
22244           || token->type == CPP_KEYWORD)
22245         {
22246           tree arguments = NULL_TREE;
22247
22248           /* Consume the token, but save it since we need it for the
22249              SIMD enabled function parsing.  */
22250           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22251
22252           /* Save away the identifier that indicates which attribute
22253              this is.  */
22254           identifier = (token->type == CPP_KEYWORD) 
22255             /* For keywords, use the canonical spelling, not the
22256                parsed identifier.  */
22257             ? ridpointers[(int) token->keyword]
22258             : id_token->u.value;
22259           
22260           attribute = build_tree_list (identifier, NULL_TREE);
22261
22262           /* Peek at the next token.  */
22263           token = cp_lexer_peek_token (parser->lexer);
22264           /* If it's an `(', then parse the attribute arguments.  */
22265           if (token->type == CPP_OPEN_PAREN)
22266             {
22267               vec<tree, va_gc> *vec;
22268               int attr_flag = (attribute_takes_identifier_p (identifier)
22269                                ? id_attr : normal_attr);
22270               if (is_cilkplus_vector_p (identifier))
22271                 {
22272                   cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22273                   continue;
22274                 }
22275               else
22276                 vec = cp_parser_parenthesized_expression_list 
22277                   (parser, attr_flag, /*cast_p=*/false, 
22278                    /*allow_expansion_p=*/false, 
22279                    /*non_constant_p=*/NULL);
22280               if (vec == NULL)
22281                 arguments = error_mark_node;
22282               else
22283                 {
22284                   arguments = build_tree_list_vec (vec);
22285                   release_tree_vector (vec);
22286                 }
22287               /* Save the arguments away.  */
22288               TREE_VALUE (attribute) = arguments;
22289             }
22290           else if (is_cilkplus_vector_p (identifier))
22291             {
22292               cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22293               continue;
22294             }
22295
22296           if (arguments != error_mark_node)
22297             {
22298               /* Add this attribute to the list.  */
22299               TREE_CHAIN (attribute) = attribute_list;
22300               attribute_list = attribute;
22301             }
22302
22303           token = cp_lexer_peek_token (parser->lexer);
22304         }
22305       /* Now, look for more attributes.  If the next token isn't a
22306          `,', we're done.  */
22307       if (token->type != CPP_COMMA)
22308         break;
22309
22310       /* Consume the comma and keep going.  */
22311       cp_lexer_consume_token (parser->lexer);
22312     }
22313   parser->translate_strings_p = save_translate_strings_p;
22314
22315   /* We built up the list in reverse order.  */
22316   return nreverse (attribute_list);
22317 }
22318
22319 /*  Parse a standard C++11 attribute.
22320
22321     The returned representation is a TREE_LIST which TREE_PURPOSE is
22322     the scoped name of the attribute, and the TREE_VALUE is its
22323     arguments list.
22324
22325     Note that the scoped name of the attribute is itself a TREE_LIST
22326     which TREE_PURPOSE is the namespace of the attribute, and
22327     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
22328     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22329     and which TREE_PURPOSE is directly the attribute name.
22330
22331     Clients of the attribute code should use get_attribute_namespace
22332     and get_attribute_name to get the actual namespace and name of
22333     attributes, regardless of their being GNU or C++11 attributes.
22334
22335     attribute:
22336       attribute-token attribute-argument-clause [opt]
22337
22338     attribute-token:
22339       identifier
22340       attribute-scoped-token
22341
22342     attribute-scoped-token:
22343       attribute-namespace :: identifier
22344
22345     attribute-namespace:
22346       identifier
22347
22348     attribute-argument-clause:
22349       ( balanced-token-seq )
22350
22351     balanced-token-seq:
22352       balanced-token [opt]
22353       balanced-token-seq balanced-token
22354
22355     balanced-token:
22356       ( balanced-token-seq )
22357       [ balanced-token-seq ]
22358       { balanced-token-seq }.  */
22359
22360 static tree
22361 cp_parser_std_attribute (cp_parser *parser)
22362 {
22363   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22364   cp_token *token;
22365
22366   /* First, parse name of the the attribute, a.k.a
22367      attribute-token.  */
22368
22369   token = cp_lexer_peek_token (parser->lexer);
22370   if (token->type == CPP_NAME)
22371     attr_id = token->u.value;
22372   else if (token->type == CPP_KEYWORD)
22373     attr_id = ridpointers[(int) token->keyword];
22374   else if (token->flags & NAMED_OP)
22375     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22376
22377   if (attr_id == NULL_TREE)
22378     return NULL_TREE;
22379
22380   cp_lexer_consume_token (parser->lexer);
22381
22382   token = cp_lexer_peek_token (parser->lexer);
22383   if (token->type == CPP_SCOPE)
22384     {
22385       /* We are seeing a scoped attribute token.  */
22386
22387       cp_lexer_consume_token (parser->lexer);
22388       attr_ns = attr_id;
22389
22390       token = cp_lexer_consume_token (parser->lexer);
22391       if (token->type == CPP_NAME)
22392         attr_id = token->u.value;
22393       else if (token->type == CPP_KEYWORD)
22394         attr_id = ridpointers[(int) token->keyword];
22395       else
22396         {
22397           error_at (token->location,
22398                     "expected an identifier for the attribute name");
22399           return error_mark_node;
22400         }
22401       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22402                                    NULL_TREE);
22403       token = cp_lexer_peek_token (parser->lexer);
22404     }
22405   else
22406     {
22407       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22408                                    NULL_TREE);
22409       /* C++11 noreturn attribute is equivalent to GNU's.  */
22410       if (is_attribute_p ("noreturn", attr_id))
22411         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22412       /* C++14 deprecated attribute is equivalent to GNU's.  */
22413       else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22414         {
22415           if (cxx_dialect == cxx11)
22416             pedwarn (token->location, OPT_Wpedantic,
22417                      "%<deprecated%> is a C++14 feature;"
22418                      " use %<gnu::deprecated%>");
22419           TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22420         }
22421     }
22422
22423   /* Now parse the optional argument clause of the attribute.  */
22424
22425   if (token->type != CPP_OPEN_PAREN)
22426     return attribute;
22427
22428   {
22429     vec<tree, va_gc> *vec;
22430     int attr_flag = normal_attr;
22431
22432     if (attr_ns == get_identifier ("gnu")
22433         && attribute_takes_identifier_p (attr_id))
22434       /* A GNU attribute that takes an identifier in parameter.  */
22435       attr_flag = id_attr;
22436
22437     vec = cp_parser_parenthesized_expression_list
22438       (parser, attr_flag, /*cast_p=*/false,
22439        /*allow_expansion_p=*/true,
22440        /*non_constant_p=*/NULL);
22441     if (vec == NULL)
22442       arguments = error_mark_node;
22443     else
22444       {
22445         arguments = build_tree_list_vec (vec);
22446         release_tree_vector (vec);
22447       }
22448
22449     if (arguments == error_mark_node)
22450       attribute = error_mark_node;
22451     else
22452       TREE_VALUE (attribute) = arguments;
22453   }
22454
22455   return attribute;
22456 }
22457
22458 /* Parse a list of standard C++-11 attributes.
22459
22460    attribute-list:
22461      attribute [opt]
22462      attribute-list , attribute[opt]
22463      attribute ...
22464      attribute-list , attribute ...
22465 */
22466
22467 static tree
22468 cp_parser_std_attribute_list (cp_parser *parser)
22469 {
22470   tree attributes = NULL_TREE, attribute = NULL_TREE;
22471   cp_token *token = NULL;
22472
22473   while (true)
22474     {
22475       attribute = cp_parser_std_attribute (parser);
22476       if (attribute == error_mark_node)
22477         break;
22478       if (attribute != NULL_TREE)
22479         {
22480           TREE_CHAIN (attribute) = attributes;
22481           attributes = attribute;
22482         }
22483       token = cp_lexer_peek_token (parser->lexer);
22484       if (token->type != CPP_COMMA)
22485         break;
22486       cp_lexer_consume_token (parser->lexer);
22487     }
22488   attributes = nreverse (attributes);
22489   return attributes;
22490 }
22491
22492 /* Parse a standard C++-11 attribute specifier.
22493
22494    attribute-specifier:
22495      [ [ attribute-list ] ]
22496      alignment-specifier
22497
22498    alignment-specifier:
22499      alignas ( type-id ... [opt] )
22500      alignas ( alignment-expression ... [opt] ).  */
22501
22502 static tree
22503 cp_parser_std_attribute_spec (cp_parser *parser)
22504 {
22505   tree attributes = NULL_TREE;
22506   cp_token *token = cp_lexer_peek_token (parser->lexer);
22507
22508   if (token->type == CPP_OPEN_SQUARE
22509       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22510     {
22511       cp_lexer_consume_token (parser->lexer);
22512       cp_lexer_consume_token (parser->lexer);
22513
22514       attributes = cp_parser_std_attribute_list (parser);
22515
22516       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22517           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22518         cp_parser_skip_to_end_of_statement (parser);
22519       else
22520         /* Warn about parsing c++11 attribute in non-c++1 mode, only
22521            when we are sure that we have actually parsed them.  */
22522         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22523     }
22524   else
22525     {
22526       tree alignas_expr;
22527
22528       /* Look for an alignment-specifier.  */
22529
22530       token = cp_lexer_peek_token (parser->lexer);
22531
22532       if (token->type != CPP_KEYWORD
22533           || token->keyword != RID_ALIGNAS)
22534         return NULL_TREE;
22535
22536       cp_lexer_consume_token (parser->lexer);
22537       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22538
22539       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22540         {
22541           cp_parser_error (parser, "expected %<(%>");
22542           return error_mark_node;
22543         }
22544
22545       cp_parser_parse_tentatively (parser);
22546       alignas_expr = cp_parser_type_id (parser);
22547
22548       if (!cp_parser_parse_definitely (parser))
22549         {
22550           gcc_assert (alignas_expr == error_mark_node
22551                       || alignas_expr == NULL_TREE);
22552
22553           alignas_expr =
22554             cp_parser_assignment_expression (parser);
22555           if (alignas_expr == error_mark_node)
22556             cp_parser_skip_to_end_of_statement (parser);
22557           if (alignas_expr == NULL_TREE
22558               || alignas_expr == error_mark_node)
22559             return alignas_expr;
22560         }
22561
22562       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22563         {
22564           cp_parser_error (parser, "expected %<)%>");
22565           return error_mark_node;
22566         }
22567
22568       alignas_expr = cxx_alignas_expr (alignas_expr);
22569
22570       /* Build the C++-11 representation of an 'aligned'
22571          attribute.  */
22572       attributes =
22573         build_tree_list (build_tree_list (get_identifier ("gnu"),
22574                                           get_identifier ("aligned")),
22575                          build_tree_list (NULL_TREE, alignas_expr));
22576     }
22577
22578   return attributes;
22579 }
22580
22581 /* Parse a standard C++-11 attribute-specifier-seq.
22582
22583    attribute-specifier-seq:
22584      attribute-specifier-seq [opt] attribute-specifier
22585  */
22586
22587 static tree
22588 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22589 {
22590   tree attr_specs = NULL;
22591
22592   while (true)
22593     {
22594       tree attr_spec = cp_parser_std_attribute_spec (parser);
22595       if (attr_spec == NULL_TREE)
22596         break;
22597       if (attr_spec == error_mark_node)
22598         return error_mark_node;
22599
22600       TREE_CHAIN (attr_spec) = attr_specs;
22601       attr_specs = attr_spec;
22602     }
22603
22604   attr_specs = nreverse (attr_specs);
22605   return attr_specs;
22606 }
22607
22608 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
22609    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
22610    current value of the PEDANTIC flag, regardless of whether or not
22611    the `__extension__' keyword is present.  The caller is responsible
22612    for restoring the value of the PEDANTIC flag.  */
22613
22614 static bool
22615 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22616 {
22617   /* Save the old value of the PEDANTIC flag.  */
22618   *saved_pedantic = pedantic;
22619
22620   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22621     {
22622       /* Consume the `__extension__' token.  */
22623       cp_lexer_consume_token (parser->lexer);
22624       /* We're not being pedantic while the `__extension__' keyword is
22625          in effect.  */
22626       pedantic = 0;
22627
22628       return true;
22629     }
22630
22631   return false;
22632 }
22633
22634 /* Parse a label declaration.
22635
22636    label-declaration:
22637      __label__ label-declarator-seq ;
22638
22639    label-declarator-seq:
22640      identifier , label-declarator-seq
22641      identifier  */
22642
22643 static void
22644 cp_parser_label_declaration (cp_parser* parser)
22645 {
22646   /* Look for the `__label__' keyword.  */
22647   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22648
22649   while (true)
22650     {
22651       tree identifier;
22652
22653       /* Look for an identifier.  */
22654       identifier = cp_parser_identifier (parser);
22655       /* If we failed, stop.  */
22656       if (identifier == error_mark_node)
22657         break;
22658       /* Declare it as a label.  */
22659       finish_label_decl (identifier);
22660       /* If the next token is a `;', stop.  */
22661       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22662         break;
22663       /* Look for the `,' separating the label declarations.  */
22664       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22665     }
22666
22667   /* Look for the final `;'.  */
22668   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22669 }
22670
22671 /* Support Functions */
22672
22673 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22674    NAME should have one of the representations used for an
22675    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22676    is returned.  If PARSER->SCOPE is a dependent type, then a
22677    SCOPE_REF is returned.
22678
22679    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22680    returned; the name was already resolved when the TEMPLATE_ID_EXPR
22681    was formed.  Abstractly, such entities should not be passed to this
22682    function, because they do not need to be looked up, but it is
22683    simpler to check for this special case here, rather than at the
22684    call-sites.
22685
22686    In cases not explicitly covered above, this function returns a
22687    DECL, OVERLOAD, or baselink representing the result of the lookup.
22688    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22689    is returned.
22690
22691    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22692    (e.g., "struct") that was used.  In that case bindings that do not
22693    refer to types are ignored.
22694
22695    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22696    ignored.
22697
22698    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22699    are ignored.
22700
22701    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22702    types.
22703
22704    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22705    TREE_LIST of candidates if name-lookup results in an ambiguity, and
22706    NULL_TREE otherwise.  */
22707
22708 static tree
22709 cp_parser_lookup_name (cp_parser *parser, tree name,
22710                        enum tag_types tag_type,
22711                        bool is_template,
22712                        bool is_namespace,
22713                        bool check_dependency,
22714                        tree *ambiguous_decls,
22715                        location_t name_location)
22716 {
22717   tree decl;
22718   tree object_type = parser->context->object_type;
22719
22720   /* Assume that the lookup will be unambiguous.  */
22721   if (ambiguous_decls)
22722     *ambiguous_decls = NULL_TREE;
22723
22724   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22725      no longer valid.  Note that if we are parsing tentatively, and
22726      the parse fails, OBJECT_TYPE will be automatically restored.  */
22727   parser->context->object_type = NULL_TREE;
22728
22729   if (name == error_mark_node)
22730     return error_mark_node;
22731
22732   /* A template-id has already been resolved; there is no lookup to
22733      do.  */
22734   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22735     return name;
22736   if (BASELINK_P (name))
22737     {
22738       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22739                   == TEMPLATE_ID_EXPR);
22740       return name;
22741     }
22742
22743   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
22744      it should already have been checked to make sure that the name
22745      used matches the type being destroyed.  */
22746   if (TREE_CODE (name) == BIT_NOT_EXPR)
22747     {
22748       tree type;
22749
22750       /* Figure out to which type this destructor applies.  */
22751       if (parser->scope)
22752         type = parser->scope;
22753       else if (object_type)
22754         type = object_type;
22755       else
22756         type = current_class_type;
22757       /* If that's not a class type, there is no destructor.  */
22758       if (!type || !CLASS_TYPE_P (type))
22759         return error_mark_node;
22760       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22761         lazily_declare_fn (sfk_destructor, type);
22762       if (!CLASSTYPE_DESTRUCTORS (type))
22763           return error_mark_node;
22764       /* If it was a class type, return the destructor.  */
22765       return CLASSTYPE_DESTRUCTORS (type);
22766     }
22767
22768   /* By this point, the NAME should be an ordinary identifier.  If
22769      the id-expression was a qualified name, the qualifying scope is
22770      stored in PARSER->SCOPE at this point.  */
22771   gcc_assert (identifier_p (name));
22772
22773   /* Perform the lookup.  */
22774   if (parser->scope)
22775     {
22776       bool dependent_p;
22777
22778       if (parser->scope == error_mark_node)
22779         return error_mark_node;
22780
22781       /* If the SCOPE is dependent, the lookup must be deferred until
22782          the template is instantiated -- unless we are explicitly
22783          looking up names in uninstantiated templates.  Even then, we
22784          cannot look up the name if the scope is not a class type; it
22785          might, for example, be a template type parameter.  */
22786       dependent_p = (TYPE_P (parser->scope)
22787                      && dependent_scope_p (parser->scope));
22788       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22789           && dependent_p)
22790         /* Defer lookup.  */
22791         decl = error_mark_node;
22792       else
22793         {
22794           tree pushed_scope = NULL_TREE;
22795
22796           /* If PARSER->SCOPE is a dependent type, then it must be a
22797              class type, and we must not be checking dependencies;
22798              otherwise, we would have processed this lookup above.  So
22799              that PARSER->SCOPE is not considered a dependent base by
22800              lookup_member, we must enter the scope here.  */
22801           if (dependent_p)
22802             pushed_scope = push_scope (parser->scope);
22803
22804           /* If the PARSER->SCOPE is a template specialization, it
22805              may be instantiated during name lookup.  In that case,
22806              errors may be issued.  Even if we rollback the current
22807              tentative parse, those errors are valid.  */
22808           decl = lookup_qualified_name (parser->scope, name,
22809                                         tag_type != none_type,
22810                                         /*complain=*/true);
22811
22812           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22813              lookup result and the nested-name-specifier nominates a class C:
22814                * if the name specified after the nested-name-specifier, when
22815                looked up in C, is the injected-class-name of C (Clause 9), or
22816                * if the name specified after the nested-name-specifier is the
22817                same as the identifier or the simple-template-id's template-
22818                name in the last component of the nested-name-specifier,
22819              the name is instead considered to name the constructor of
22820              class C. [ Note: for example, the constructor is not an
22821              acceptable lookup result in an elaborated-type-specifier so
22822              the constructor would not be used in place of the
22823              injected-class-name. --end note ] Such a constructor name
22824              shall be used only in the declarator-id of a declaration that
22825              names a constructor or in a using-declaration.  */
22826           if (tag_type == none_type
22827               && DECL_SELF_REFERENCE_P (decl)
22828               && same_type_p (DECL_CONTEXT (decl), parser->scope))
22829             decl = lookup_qualified_name (parser->scope, ctor_identifier,
22830                                           tag_type != none_type,
22831                                           /*complain=*/true);
22832
22833           /* If we have a single function from a using decl, pull it out.  */
22834           if (TREE_CODE (decl) == OVERLOAD
22835               && !really_overloaded_fn (decl))
22836             decl = OVL_FUNCTION (decl);
22837
22838           if (pushed_scope)
22839             pop_scope (pushed_scope);
22840         }
22841
22842       /* If the scope is a dependent type and either we deferred lookup or
22843          we did lookup but didn't find the name, rememeber the name.  */
22844       if (decl == error_mark_node && TYPE_P (parser->scope)
22845           && dependent_type_p (parser->scope))
22846         {
22847           if (tag_type)
22848             {
22849               tree type;
22850
22851               /* The resolution to Core Issue 180 says that `struct
22852                  A::B' should be considered a type-name, even if `A'
22853                  is dependent.  */
22854               type = make_typename_type (parser->scope, name, tag_type,
22855                                          /*complain=*/tf_error);
22856               if (type != error_mark_node)
22857                 decl = TYPE_NAME (type);
22858             }
22859           else if (is_template
22860                    && (cp_parser_next_token_ends_template_argument_p (parser)
22861                        || cp_lexer_next_token_is (parser->lexer,
22862                                                   CPP_CLOSE_PAREN)))
22863             decl = make_unbound_class_template (parser->scope,
22864                                                 name, NULL_TREE,
22865                                                 /*complain=*/tf_error);
22866           else
22867             decl = build_qualified_name (/*type=*/NULL_TREE,
22868                                          parser->scope, name,
22869                                          is_template);
22870         }
22871       parser->qualifying_scope = parser->scope;
22872       parser->object_scope = NULL_TREE;
22873     }
22874   else if (object_type)
22875     {
22876       /* Look up the name in the scope of the OBJECT_TYPE, unless the
22877          OBJECT_TYPE is not a class.  */
22878       if (CLASS_TYPE_P (object_type))
22879         /* If the OBJECT_TYPE is a template specialization, it may
22880            be instantiated during name lookup.  In that case, errors
22881            may be issued.  Even if we rollback the current tentative
22882            parse, those errors are valid.  */
22883         decl = lookup_member (object_type,
22884                               name,
22885                               /*protect=*/0,
22886                               tag_type != none_type,
22887                               tf_warning_or_error);
22888       else
22889         decl = NULL_TREE;
22890
22891       if (!decl)
22892         /* Look it up in the enclosing context.  */
22893         decl = lookup_name_real (name, tag_type != none_type,
22894                                  /*nonclass=*/0,
22895                                  /*block_p=*/true, is_namespace, 0);
22896       parser->object_scope = object_type;
22897       parser->qualifying_scope = NULL_TREE;
22898     }
22899   else
22900     {
22901       decl = lookup_name_real (name, tag_type != none_type,
22902                                /*nonclass=*/0,
22903                                /*block_p=*/true, is_namespace, 0);
22904       parser->qualifying_scope = NULL_TREE;
22905       parser->object_scope = NULL_TREE;
22906     }
22907
22908   /* If the lookup failed, let our caller know.  */
22909   if (!decl || decl == error_mark_node)
22910     return error_mark_node;
22911
22912   /* Pull out the template from an injected-class-name (or multiple).  */
22913   if (is_template)
22914     decl = maybe_get_template_decl_from_type_decl (decl);
22915
22916   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
22917   if (TREE_CODE (decl) == TREE_LIST)
22918     {
22919       if (ambiguous_decls)
22920         *ambiguous_decls = decl;
22921       /* The error message we have to print is too complicated for
22922          cp_parser_error, so we incorporate its actions directly.  */
22923       if (!cp_parser_simulate_error (parser))
22924         {
22925           error_at (name_location, "reference to %qD is ambiguous",
22926                     name);
22927           print_candidates (decl);
22928         }
22929       return error_mark_node;
22930     }
22931
22932   gcc_assert (DECL_P (decl)
22933               || TREE_CODE (decl) == OVERLOAD
22934               || TREE_CODE (decl) == SCOPE_REF
22935               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22936               || BASELINK_P (decl));
22937
22938   /* If we have resolved the name of a member declaration, check to
22939      see if the declaration is accessible.  When the name resolves to
22940      set of overloaded functions, accessibility is checked when
22941      overload resolution is done.
22942
22943      During an explicit instantiation, access is not checked at all,
22944      as per [temp.explicit].  */
22945   if (DECL_P (decl))
22946     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22947
22948   maybe_record_typedef_use (decl);
22949
22950   return decl;
22951 }
22952
22953 /* Like cp_parser_lookup_name, but for use in the typical case where
22954    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22955    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
22956
22957 static tree
22958 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22959 {
22960   return cp_parser_lookup_name (parser, name,
22961                                 none_type,
22962                                 /*is_template=*/false,
22963                                 /*is_namespace=*/false,
22964                                 /*check_dependency=*/true,
22965                                 /*ambiguous_decls=*/NULL,
22966                                 location);
22967 }
22968
22969 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22970    the current context, return the TYPE_DECL.  If TAG_NAME_P is
22971    true, the DECL indicates the class being defined in a class-head,
22972    or declared in an elaborated-type-specifier.
22973
22974    Otherwise, return DECL.  */
22975
22976 static tree
22977 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22978 {
22979   /* If the TEMPLATE_DECL is being declared as part of a class-head,
22980      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22981
22982        struct A {
22983          template <typename T> struct B;
22984        };
22985
22986        template <typename T> struct A::B {};
22987
22988      Similarly, in an elaborated-type-specifier:
22989
22990        namespace N { struct X{}; }
22991
22992        struct A {
22993          template <typename T> friend struct N::X;
22994        };
22995
22996      However, if the DECL refers to a class type, and we are in
22997      the scope of the class, then the name lookup automatically
22998      finds the TYPE_DECL created by build_self_reference rather
22999      than a TEMPLATE_DECL.  For example, in:
23000
23001        template <class T> struct S {
23002          S s;
23003        };
23004
23005      there is no need to handle such case.  */
23006
23007   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23008     return DECL_TEMPLATE_RESULT (decl);
23009
23010   return decl;
23011 }
23012
23013 /* If too many, or too few, template-parameter lists apply to the
23014    declarator, issue an error message.  Returns TRUE if all went well,
23015    and FALSE otherwise.  */
23016
23017 static bool
23018 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23019                                                 cp_declarator *declarator,
23020                                                 location_t declarator_location)
23021 {
23022   switch (declarator->kind)
23023     {
23024     case cdk_id:
23025       {
23026         unsigned num_templates = 0;
23027         tree scope = declarator->u.id.qualifying_scope;
23028
23029         if (scope)
23030           num_templates = num_template_headers_for_class (scope);
23031         else if (TREE_CODE (declarator->u.id.unqualified_name)
23032                  == TEMPLATE_ID_EXPR)
23033           /* If the DECLARATOR has the form `X<y>' then it uses one
23034              additional level of template parameters.  */
23035           ++num_templates;
23036
23037         return cp_parser_check_template_parameters 
23038           (parser, num_templates, declarator_location, declarator);
23039       }
23040
23041     case cdk_function:
23042     case cdk_array:
23043     case cdk_pointer:
23044     case cdk_reference:
23045     case cdk_ptrmem:
23046       return (cp_parser_check_declarator_template_parameters
23047               (parser, declarator->declarator, declarator_location));
23048
23049     case cdk_error:
23050       return true;
23051
23052     default:
23053       gcc_unreachable ();
23054     }
23055   return false;
23056 }
23057
23058 /* NUM_TEMPLATES were used in the current declaration.  If that is
23059    invalid, return FALSE and issue an error messages.  Otherwise,
23060    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
23061    declarator and we can print more accurate diagnostics.  */
23062
23063 static bool
23064 cp_parser_check_template_parameters (cp_parser* parser,
23065                                      unsigned num_templates,
23066                                      location_t location,
23067                                      cp_declarator *declarator)
23068 {
23069   /* If there are the same number of template classes and parameter
23070      lists, that's OK.  */
23071   if (parser->num_template_parameter_lists == num_templates)
23072     return true;
23073   /* If there are more, but only one more, then we are referring to a
23074      member template.  That's OK too.  */
23075   if (parser->num_template_parameter_lists == num_templates + 1)
23076     return true;
23077   /* If there are more template classes than parameter lists, we have
23078      something like:
23079
23080        template <class T> void S<T>::R<T>::f ();  */
23081   if (parser->num_template_parameter_lists < num_templates)
23082     {
23083       if (declarator && !current_function_decl)
23084         error_at (location, "specializing member %<%T::%E%> "
23085                   "requires %<template<>%> syntax", 
23086                   declarator->u.id.qualifying_scope,
23087                   declarator->u.id.unqualified_name);
23088       else if (declarator)
23089         error_at (location, "invalid declaration of %<%T::%E%>",
23090                   declarator->u.id.qualifying_scope,
23091                   declarator->u.id.unqualified_name);
23092       else 
23093         error_at (location, "too few template-parameter-lists");
23094       return false;
23095     }
23096   /* Otherwise, there are too many template parameter lists.  We have
23097      something like:
23098
23099      template <class T> template <class U> void S::f();  */
23100   error_at (location, "too many template-parameter-lists");
23101   return false;
23102 }
23103
23104 /* Parse an optional `::' token indicating that the following name is
23105    from the global namespace.  If so, PARSER->SCOPE is set to the
23106    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23107    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23108    Returns the new value of PARSER->SCOPE, if the `::' token is
23109    present, and NULL_TREE otherwise.  */
23110
23111 static tree
23112 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23113 {
23114   cp_token *token;
23115
23116   /* Peek at the next token.  */
23117   token = cp_lexer_peek_token (parser->lexer);
23118   /* If we're looking at a `::' token then we're starting from the
23119      global namespace, not our current location.  */
23120   if (token->type == CPP_SCOPE)
23121     {
23122       /* Consume the `::' token.  */
23123       cp_lexer_consume_token (parser->lexer);
23124       /* Set the SCOPE so that we know where to start the lookup.  */
23125       parser->scope = global_namespace;
23126       parser->qualifying_scope = global_namespace;
23127       parser->object_scope = NULL_TREE;
23128
23129       return parser->scope;
23130     }
23131   else if (!current_scope_valid_p)
23132     {
23133       parser->scope = NULL_TREE;
23134       parser->qualifying_scope = NULL_TREE;
23135       parser->object_scope = NULL_TREE;
23136     }
23137
23138   return NULL_TREE;
23139 }
23140
23141 /* Returns TRUE if the upcoming token sequence is the start of a
23142    constructor declarator.  If FRIEND_P is true, the declarator is
23143    preceded by the `friend' specifier.  */
23144
23145 static bool
23146 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23147 {
23148   bool constructor_p;
23149   bool outside_class_specifier_p;
23150   tree nested_name_specifier;
23151   cp_token *next_token;
23152
23153   /* The common case is that this is not a constructor declarator, so
23154      try to avoid doing lots of work if at all possible.  It's not
23155      valid declare a constructor at function scope.  */
23156   if (parser->in_function_body)
23157     return false;
23158   /* And only certain tokens can begin a constructor declarator.  */
23159   next_token = cp_lexer_peek_token (parser->lexer);
23160   if (next_token->type != CPP_NAME
23161       && next_token->type != CPP_SCOPE
23162       && next_token->type != CPP_NESTED_NAME_SPECIFIER
23163       && next_token->type != CPP_TEMPLATE_ID)
23164     return false;
23165
23166   /* Parse tentatively; we are going to roll back all of the tokens
23167      consumed here.  */
23168   cp_parser_parse_tentatively (parser);
23169   /* Assume that we are looking at a constructor declarator.  */
23170   constructor_p = true;
23171
23172   /* Look for the optional `::' operator.  */
23173   cp_parser_global_scope_opt (parser,
23174                               /*current_scope_valid_p=*/false);
23175   /* Look for the nested-name-specifier.  */
23176   nested_name_specifier
23177     = (cp_parser_nested_name_specifier_opt (parser,
23178                                             /*typename_keyword_p=*/false,
23179                                             /*check_dependency_p=*/false,
23180                                             /*type_p=*/false,
23181                                             /*is_declaration=*/false));
23182
23183   outside_class_specifier_p = (!at_class_scope_p ()
23184                                || !TYPE_BEING_DEFINED (current_class_type)
23185                                || friend_p);
23186
23187   /* Outside of a class-specifier, there must be a
23188      nested-name-specifier.  */
23189   if (!nested_name_specifier && outside_class_specifier_p)
23190     constructor_p = false;
23191   else if (nested_name_specifier == error_mark_node)
23192     constructor_p = false;
23193
23194   /* If we have a class scope, this is easy; DR 147 says that S::S always
23195      names the constructor, and no other qualified name could.  */
23196   if (constructor_p && nested_name_specifier
23197       && CLASS_TYPE_P (nested_name_specifier))
23198     {
23199       tree id = cp_parser_unqualified_id (parser,
23200                                           /*template_keyword_p=*/false,
23201                                           /*check_dependency_p=*/false,
23202                                           /*declarator_p=*/true,
23203                                           /*optional_p=*/false);
23204       if (is_overloaded_fn (id))
23205         id = DECL_NAME (get_first_fn (id));
23206       if (!constructor_name_p (id, nested_name_specifier))
23207         constructor_p = false;
23208     }
23209   /* If we still think that this might be a constructor-declarator,
23210      look for a class-name.  */
23211   else if (constructor_p)
23212     {
23213       /* If we have:
23214
23215            template <typename T> struct S {
23216              S();
23217            };
23218
23219          we must recognize that the nested `S' names a class.  */
23220       tree type_decl;
23221       type_decl = cp_parser_class_name (parser,
23222                                         /*typename_keyword_p=*/false,
23223                                         /*template_keyword_p=*/false,
23224                                         none_type,
23225                                         /*check_dependency_p=*/false,
23226                                         /*class_head_p=*/false,
23227                                         /*is_declaration=*/false);
23228       /* If there was no class-name, then this is not a constructor.
23229          Otherwise, if we are in a class-specifier and we aren't
23230          handling a friend declaration, check that its type matches
23231          current_class_type (c++/38313).  Note: error_mark_node
23232          is left alone for error recovery purposes.  */
23233       constructor_p = (!cp_parser_error_occurred (parser)
23234                        && (outside_class_specifier_p
23235                            || type_decl == error_mark_node
23236                            || same_type_p (current_class_type,
23237                                            TREE_TYPE (type_decl))));
23238
23239       /* If we're still considering a constructor, we have to see a `(',
23240          to begin the parameter-declaration-clause, followed by either a
23241          `)', an `...', or a decl-specifier.  We need to check for a
23242          type-specifier to avoid being fooled into thinking that:
23243
23244            S (f) (int);
23245
23246          is a constructor.  (It is actually a function named `f' that
23247          takes one parameter (of type `int') and returns a value of type
23248          `S'.  */
23249       if (constructor_p
23250           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23251         constructor_p = false;
23252
23253       if (constructor_p
23254           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23255           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23256           /* A parameter declaration begins with a decl-specifier,
23257              which is either the "attribute" keyword, a storage class
23258              specifier, or (usually) a type-specifier.  */
23259           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23260         {
23261           tree type;
23262           tree pushed_scope = NULL_TREE;
23263           unsigned saved_num_template_parameter_lists;
23264
23265           /* Names appearing in the type-specifier should be looked up
23266              in the scope of the class.  */
23267           if (current_class_type)
23268             type = NULL_TREE;
23269           else
23270             {
23271               type = TREE_TYPE (type_decl);
23272               if (TREE_CODE (type) == TYPENAME_TYPE)
23273                 {
23274                   type = resolve_typename_type (type,
23275                                                 /*only_current_p=*/false);
23276                   if (TREE_CODE (type) == TYPENAME_TYPE)
23277                     {
23278                       cp_parser_abort_tentative_parse (parser);
23279                       return false;
23280                     }
23281                 }
23282               pushed_scope = push_scope (type);
23283             }
23284
23285           /* Inside the constructor parameter list, surrounding
23286              template-parameter-lists do not apply.  */
23287           saved_num_template_parameter_lists
23288             = parser->num_template_parameter_lists;
23289           parser->num_template_parameter_lists = 0;
23290
23291           /* Look for the type-specifier.  */
23292           cp_parser_type_specifier (parser,
23293                                     CP_PARSER_FLAGS_NONE,
23294                                     /*decl_specs=*/NULL,
23295                                     /*is_declarator=*/true,
23296                                     /*declares_class_or_enum=*/NULL,
23297                                     /*is_cv_qualifier=*/NULL);
23298
23299           parser->num_template_parameter_lists
23300             = saved_num_template_parameter_lists;
23301
23302           /* Leave the scope of the class.  */
23303           if (pushed_scope)
23304             pop_scope (pushed_scope);
23305
23306           constructor_p = !cp_parser_error_occurred (parser);
23307         }
23308     }
23309
23310   /* We did not really want to consume any tokens.  */
23311   cp_parser_abort_tentative_parse (parser);
23312
23313   return constructor_p;
23314 }
23315
23316 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23317    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
23318    they must be performed once we are in the scope of the function.
23319
23320    Returns the function defined.  */
23321
23322 static tree
23323 cp_parser_function_definition_from_specifiers_and_declarator
23324   (cp_parser* parser,
23325    cp_decl_specifier_seq *decl_specifiers,
23326    tree attributes,
23327    const cp_declarator *declarator)
23328 {
23329   tree fn;
23330   bool success_p;
23331
23332   /* Begin the function-definition.  */
23333   success_p = start_function (decl_specifiers, declarator, attributes);
23334
23335   /* The things we're about to see are not directly qualified by any
23336      template headers we've seen thus far.  */
23337   reset_specialization ();
23338
23339   /* If there were names looked up in the decl-specifier-seq that we
23340      did not check, check them now.  We must wait until we are in the
23341      scope of the function to perform the checks, since the function
23342      might be a friend.  */
23343   perform_deferred_access_checks (tf_warning_or_error);
23344
23345   if (success_p)
23346     {
23347       cp_finalize_omp_declare_simd (parser, current_function_decl);
23348       parser->omp_declare_simd = NULL;
23349     }
23350
23351   if (!success_p)
23352     {
23353       /* Skip the entire function.  */
23354       cp_parser_skip_to_end_of_block_or_statement (parser);
23355       fn = error_mark_node;
23356     }
23357   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23358     {
23359       /* Seen already, skip it.  An error message has already been output.  */
23360       cp_parser_skip_to_end_of_block_or_statement (parser);
23361       fn = current_function_decl;
23362       current_function_decl = NULL_TREE;
23363       /* If this is a function from a class, pop the nested class.  */
23364       if (current_class_name)
23365         pop_nested_class ();
23366     }
23367   else
23368     {
23369       timevar_id_t tv;
23370       if (DECL_DECLARED_INLINE_P (current_function_decl))
23371         tv = TV_PARSE_INLINE;
23372       else
23373         tv = TV_PARSE_FUNC;
23374       timevar_push (tv);
23375       fn = cp_parser_function_definition_after_declarator (parser,
23376                                                          /*inline_p=*/false);
23377       timevar_pop (tv);
23378     }
23379
23380   return fn;
23381 }
23382
23383 /* Parse the part of a function-definition that follows the
23384    declarator.  INLINE_P is TRUE iff this function is an inline
23385    function defined within a class-specifier.
23386
23387    Returns the function defined.  */
23388
23389 static tree
23390 cp_parser_function_definition_after_declarator (cp_parser* parser,
23391                                                 bool inline_p)
23392 {
23393   tree fn;
23394   bool ctor_initializer_p = false;
23395   bool saved_in_unbraced_linkage_specification_p;
23396   bool saved_in_function_body;
23397   unsigned saved_num_template_parameter_lists;
23398   cp_token *token;
23399   bool fully_implicit_function_template_p
23400     = parser->fully_implicit_function_template_p;
23401   parser->fully_implicit_function_template_p = false;
23402   tree implicit_template_parms
23403     = parser->implicit_template_parms;
23404   parser->implicit_template_parms = 0;
23405   cp_binding_level* implicit_template_scope
23406     = parser->implicit_template_scope;
23407   parser->implicit_template_scope = 0;
23408
23409   saved_in_function_body = parser->in_function_body;
23410   parser->in_function_body = true;
23411   /* If the next token is `return', then the code may be trying to
23412      make use of the "named return value" extension that G++ used to
23413      support.  */
23414   token = cp_lexer_peek_token (parser->lexer);
23415   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23416     {
23417       /* Consume the `return' keyword.  */
23418       cp_lexer_consume_token (parser->lexer);
23419       /* Look for the identifier that indicates what value is to be
23420          returned.  */
23421       cp_parser_identifier (parser);
23422       /* Issue an error message.  */
23423       error_at (token->location,
23424                 "named return values are no longer supported");
23425       /* Skip tokens until we reach the start of the function body.  */
23426       while (true)
23427         {
23428           cp_token *token = cp_lexer_peek_token (parser->lexer);
23429           if (token->type == CPP_OPEN_BRACE
23430               || token->type == CPP_EOF
23431               || token->type == CPP_PRAGMA_EOL)
23432             break;
23433           cp_lexer_consume_token (parser->lexer);
23434         }
23435     }
23436   /* The `extern' in `extern "C" void f () { ... }' does not apply to
23437      anything declared inside `f'.  */
23438   saved_in_unbraced_linkage_specification_p
23439     = parser->in_unbraced_linkage_specification_p;
23440   parser->in_unbraced_linkage_specification_p = false;
23441   /* Inside the function, surrounding template-parameter-lists do not
23442      apply.  */
23443   saved_num_template_parameter_lists
23444     = parser->num_template_parameter_lists;
23445   parser->num_template_parameter_lists = 0;
23446
23447   start_lambda_scope (current_function_decl);
23448
23449   /* If the next token is `try', `__transaction_atomic', or
23450      `__transaction_relaxed`, then we are looking at either function-try-block
23451      or function-transaction-block.  Note that all of these include the
23452      function-body.  */
23453   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23454     ctor_initializer_p = cp_parser_function_transaction (parser,
23455         RID_TRANSACTION_ATOMIC);
23456   else if (cp_lexer_next_token_is_keyword (parser->lexer,
23457       RID_TRANSACTION_RELAXED))
23458     ctor_initializer_p = cp_parser_function_transaction (parser,
23459         RID_TRANSACTION_RELAXED);
23460   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23461     ctor_initializer_p = cp_parser_function_try_block (parser);
23462   else
23463     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23464       (parser, /*in_function_try_block=*/false);
23465
23466   finish_lambda_scope ();
23467
23468   /* Finish the function.  */
23469   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23470                         (inline_p ? 2 : 0));
23471   /* Generate code for it, if necessary.  */
23472   expand_or_defer_fn (fn);
23473   /* Restore the saved values.  */
23474   parser->in_unbraced_linkage_specification_p
23475     = saved_in_unbraced_linkage_specification_p;
23476   parser->num_template_parameter_lists
23477     = saved_num_template_parameter_lists;
23478   parser->in_function_body = saved_in_function_body;
23479
23480   parser->fully_implicit_function_template_p
23481     = fully_implicit_function_template_p;
23482   parser->implicit_template_parms
23483     = implicit_template_parms;
23484   parser->implicit_template_scope
23485     = implicit_template_scope;
23486
23487   if (parser->fully_implicit_function_template_p)
23488     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23489
23490   return fn;
23491 }
23492
23493 /* Parse a template-declaration, assuming that the `export' (and
23494    `extern') keywords, if present, has already been scanned.  MEMBER_P
23495    is as for cp_parser_template_declaration.  */
23496
23497 static void
23498 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23499 {
23500   tree decl = NULL_TREE;
23501   vec<deferred_access_check, va_gc> *checks;
23502   tree parameter_list;
23503   bool friend_p = false;
23504   bool need_lang_pop;
23505   cp_token *token;
23506
23507   /* Look for the `template' keyword.  */
23508   token = cp_lexer_peek_token (parser->lexer);
23509   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23510     return;
23511
23512   /* And the `<'.  */
23513   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23514     return;
23515   if (at_class_scope_p () && current_function_decl)
23516     {
23517       /* 14.5.2.2 [temp.mem]
23518
23519          A local class shall not have member templates.  */
23520       error_at (token->location,
23521                 "invalid declaration of member template in local class");
23522       cp_parser_skip_to_end_of_block_or_statement (parser);
23523       return;
23524     }
23525   /* [temp]
23526
23527      A template ... shall not have C linkage.  */
23528   if (current_lang_name == lang_name_c)
23529     {
23530       error_at (token->location, "template with C linkage");
23531       /* Give it C++ linkage to avoid confusing other parts of the
23532          front end.  */
23533       push_lang_context (lang_name_cplusplus);
23534       need_lang_pop = true;
23535     }
23536   else
23537     need_lang_pop = false;
23538
23539   /* We cannot perform access checks on the template parameter
23540      declarations until we know what is being declared, just as we
23541      cannot check the decl-specifier list.  */
23542   push_deferring_access_checks (dk_deferred);
23543
23544   /* If the next token is `>', then we have an invalid
23545      specialization.  Rather than complain about an invalid template
23546      parameter, issue an error message here.  */
23547   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23548     {
23549       cp_parser_error (parser, "invalid explicit specialization");
23550       begin_specialization ();
23551       parameter_list = NULL_TREE;
23552     }
23553   else
23554     {
23555       /* Parse the template parameters.  */
23556       parameter_list = cp_parser_template_parameter_list (parser);
23557     }
23558
23559   /* Get the deferred access checks from the parameter list.  These
23560      will be checked once we know what is being declared, as for a
23561      member template the checks must be performed in the scope of the
23562      class containing the member.  */
23563   checks = get_deferred_access_checks ();
23564
23565   /* Look for the `>'.  */
23566   cp_parser_skip_to_end_of_template_parameter_list (parser);
23567   /* We just processed one more parameter list.  */
23568   ++parser->num_template_parameter_lists;
23569   /* If the next token is `template', there are more template
23570      parameters.  */
23571   if (cp_lexer_next_token_is_keyword (parser->lexer,
23572                                       RID_TEMPLATE))
23573     cp_parser_template_declaration_after_export (parser, member_p);
23574   else if (cxx_dialect >= cxx11
23575            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23576     decl = cp_parser_alias_declaration (parser);
23577   else
23578     {
23579       /* There are no access checks when parsing a template, as we do not
23580          know if a specialization will be a friend.  */
23581       push_deferring_access_checks (dk_no_check);
23582       token = cp_lexer_peek_token (parser->lexer);
23583       decl = cp_parser_single_declaration (parser,
23584                                            checks,
23585                                            member_p,
23586                                            /*explicit_specialization_p=*/false,
23587                                            &friend_p);
23588       pop_deferring_access_checks ();
23589
23590       /* If this is a member template declaration, let the front
23591          end know.  */
23592       if (member_p && !friend_p && decl)
23593         {
23594           if (TREE_CODE (decl) == TYPE_DECL)
23595             cp_parser_check_access_in_redeclaration (decl, token->location);
23596
23597           decl = finish_member_template_decl (decl);
23598         }
23599       else if (friend_p && decl
23600                && DECL_DECLARES_TYPE_P (decl))
23601         make_friend_class (current_class_type, TREE_TYPE (decl),
23602                            /*complain=*/true);
23603     }
23604   /* We are done with the current parameter list.  */
23605   --parser->num_template_parameter_lists;
23606
23607   pop_deferring_access_checks ();
23608
23609   /* Finish up.  */
23610   finish_template_decl (parameter_list);
23611
23612   /* Check the template arguments for a literal operator template.  */
23613   if (decl
23614       && DECL_DECLARES_FUNCTION_P (decl)
23615       && UDLIT_OPER_P (DECL_NAME (decl)))
23616     {
23617       bool ok = true;
23618       if (parameter_list == NULL_TREE)
23619         ok = false;
23620       else
23621         {
23622           int num_parms = TREE_VEC_LENGTH (parameter_list);
23623           if (num_parms == 1)
23624             {
23625               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23626               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23627               if (TREE_TYPE (parm) != char_type_node
23628                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23629                 ok = false;
23630             }
23631           else if (num_parms == 2 && cxx_dialect >= cxx14)
23632             {
23633               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23634               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23635               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23636               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23637               if (TREE_TYPE (parm) != TREE_TYPE (type)
23638                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23639                 ok = false;
23640             }
23641           else
23642             ok = false;
23643         }
23644       if (!ok)
23645         {
23646           if (cxx_dialect >= cxx14)
23647             error ("literal operator template %qD has invalid parameter list."
23648                    "  Expected non-type template argument pack <char...>"
23649                    " or <typename CharT, CharT...>",
23650                    decl);
23651           else
23652             error ("literal operator template %qD has invalid parameter list."
23653                    "  Expected non-type template argument pack <char...>",
23654                    decl);
23655         }
23656     }
23657   /* Register member declarations.  */
23658   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23659     finish_member_declaration (decl);
23660   /* For the erroneous case of a template with C linkage, we pushed an
23661      implicit C++ linkage scope; exit that scope now.  */
23662   if (need_lang_pop)
23663     pop_lang_context ();
23664   /* If DECL is a function template, we must return to parse it later.
23665      (Even though there is no definition, there might be default
23666      arguments that need handling.)  */
23667   if (member_p && decl
23668       && DECL_DECLARES_FUNCTION_P (decl))
23669     vec_safe_push (unparsed_funs_with_definitions, decl);
23670 }
23671
23672 /* Perform the deferred access checks from a template-parameter-list.
23673    CHECKS is a TREE_LIST of access checks, as returned by
23674    get_deferred_access_checks.  */
23675
23676 static void
23677 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23678 {
23679   ++processing_template_parmlist;
23680   perform_access_checks (checks, tf_warning_or_error);
23681   --processing_template_parmlist;
23682 }
23683
23684 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23685    `function-definition' sequence that follows a template header.
23686    If MEMBER_P is true, this declaration appears in a class scope.
23687
23688    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
23689    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
23690
23691 static tree
23692 cp_parser_single_declaration (cp_parser* parser,
23693                               vec<deferred_access_check, va_gc> *checks,
23694                               bool member_p,
23695                               bool explicit_specialization_p,
23696                               bool* friend_p)
23697 {
23698   int declares_class_or_enum;
23699   tree decl = NULL_TREE;
23700   cp_decl_specifier_seq decl_specifiers;
23701   bool function_definition_p = false;
23702   cp_token *decl_spec_token_start;
23703
23704   /* This function is only used when processing a template
23705      declaration.  */
23706   gcc_assert (innermost_scope_kind () == sk_template_parms
23707               || innermost_scope_kind () == sk_template_spec);
23708
23709   /* Defer access checks until we know what is being declared.  */
23710   push_deferring_access_checks (dk_deferred);
23711
23712   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23713      alternative.  */
23714   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23715   cp_parser_decl_specifier_seq (parser,
23716                                 CP_PARSER_FLAGS_OPTIONAL,
23717                                 &decl_specifiers,
23718                                 &declares_class_or_enum);
23719   if (friend_p)
23720     *friend_p = cp_parser_friend_p (&decl_specifiers);
23721
23722   /* There are no template typedefs.  */
23723   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23724     {
23725       error_at (decl_spec_token_start->location,
23726                 "template declaration of %<typedef%>");
23727       decl = error_mark_node;
23728     }
23729
23730   /* Gather up the access checks that occurred the
23731      decl-specifier-seq.  */
23732   stop_deferring_access_checks ();
23733
23734   /* Check for the declaration of a template class.  */
23735   if (declares_class_or_enum)
23736     {
23737       if (cp_parser_declares_only_class_p (parser))
23738         {
23739           decl = shadow_tag (&decl_specifiers);
23740
23741           /* In this case:
23742
23743                struct C {
23744                  friend template <typename T> struct A<T>::B;
23745                };
23746
23747              A<T>::B will be represented by a TYPENAME_TYPE, and
23748              therefore not recognized by shadow_tag.  */
23749           if (friend_p && *friend_p
23750               && !decl
23751               && decl_specifiers.type
23752               && TYPE_P (decl_specifiers.type))
23753             decl = decl_specifiers.type;
23754
23755           if (decl && decl != error_mark_node)
23756             decl = TYPE_NAME (decl);
23757           else
23758             decl = error_mark_node;
23759
23760           /* Perform access checks for template parameters.  */
23761           cp_parser_perform_template_parameter_access_checks (checks);
23762         }
23763     }
23764
23765   /* Complain about missing 'typename' or other invalid type names.  */
23766   if (!decl_specifiers.any_type_specifiers_p
23767       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23768     {
23769       /* cp_parser_parse_and_diagnose_invalid_type_name calls
23770          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23771          the rest of this declaration.  */
23772       decl = error_mark_node;
23773       goto out;
23774     }
23775
23776   /* If it's not a template class, try for a template function.  If
23777      the next token is a `;', then this declaration does not declare
23778      anything.  But, if there were errors in the decl-specifiers, then
23779      the error might well have come from an attempted class-specifier.
23780      In that case, there's no need to warn about a missing declarator.  */
23781   if (!decl
23782       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23783           || decl_specifiers.type != error_mark_node))
23784     {
23785       decl = cp_parser_init_declarator (parser,
23786                                         &decl_specifiers,
23787                                         checks,
23788                                         /*function_definition_allowed_p=*/true,
23789                                         member_p,
23790                                         declares_class_or_enum,
23791                                         &function_definition_p,
23792                                         NULL, NULL);
23793
23794     /* 7.1.1-1 [dcl.stc]
23795
23796        A storage-class-specifier shall not be specified in an explicit
23797        specialization...  */
23798     if (decl
23799         && explicit_specialization_p
23800         && decl_specifiers.storage_class != sc_none)
23801       {
23802         error_at (decl_spec_token_start->location,
23803                   "explicit template specialization cannot have a storage class");
23804         decl = error_mark_node;
23805       }
23806
23807     if (decl && VAR_P (decl))
23808       check_template_variable (decl);
23809     }
23810
23811   /* Look for a trailing `;' after the declaration.  */
23812   if (!function_definition_p
23813       && (decl == error_mark_node
23814           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23815     cp_parser_skip_to_end_of_block_or_statement (parser);
23816
23817  out:
23818   pop_deferring_access_checks ();
23819
23820   /* Clear any current qualification; whatever comes next is the start
23821      of something new.  */
23822   parser->scope = NULL_TREE;
23823   parser->qualifying_scope = NULL_TREE;
23824   parser->object_scope = NULL_TREE;
23825
23826   return decl;
23827 }
23828
23829 /* Parse a cast-expression that is not the operand of a unary "&".  */
23830
23831 static tree
23832 cp_parser_simple_cast_expression (cp_parser *parser)
23833 {
23834   return cp_parser_cast_expression (parser, /*address_p=*/false,
23835                                     /*cast_p=*/false, /*decltype*/false, NULL);
23836 }
23837
23838 /* Parse a functional cast to TYPE.  Returns an expression
23839    representing the cast.  */
23840
23841 static tree
23842 cp_parser_functional_cast (cp_parser* parser, tree type)
23843 {
23844   vec<tree, va_gc> *vec;
23845   tree expression_list;
23846   tree cast;
23847   bool nonconst_p;
23848
23849   if (!type)
23850     type = error_mark_node;
23851
23852   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23853     {
23854       cp_lexer_set_source_position (parser->lexer);
23855       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23856       expression_list = cp_parser_braced_list (parser, &nonconst_p);
23857       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23858       if (TREE_CODE (type) == TYPE_DECL)
23859         type = TREE_TYPE (type);
23860       return finish_compound_literal (type, expression_list,
23861                                       tf_warning_or_error);
23862     }
23863
23864
23865   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23866                                                  /*cast_p=*/true,
23867                                                  /*allow_expansion_p=*/true,
23868                                                  /*non_constant_p=*/NULL);
23869   if (vec == NULL)
23870     expression_list = error_mark_node;
23871   else
23872     {
23873       expression_list = build_tree_list_vec (vec);
23874       release_tree_vector (vec);
23875     }
23876
23877   cast = build_functional_cast (type, expression_list,
23878                                 tf_warning_or_error);
23879   /* [expr.const]/1: In an integral constant expression "only type
23880      conversions to integral or enumeration type can be used".  */
23881   if (TREE_CODE (type) == TYPE_DECL)
23882     type = TREE_TYPE (type);
23883   if (cast != error_mark_node
23884       && !cast_valid_in_integral_constant_expression_p (type)
23885       && cp_parser_non_integral_constant_expression (parser,
23886                                                      NIC_CONSTRUCTOR))
23887     return error_mark_node;
23888   return cast;
23889 }
23890
23891 /* Save the tokens that make up the body of a member function defined
23892    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
23893    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
23894    specifiers applied to the declaration.  Returns the FUNCTION_DECL
23895    for the member function.  */
23896
23897 static tree
23898 cp_parser_save_member_function_body (cp_parser* parser,
23899                                      cp_decl_specifier_seq *decl_specifiers,
23900                                      cp_declarator *declarator,
23901                                      tree attributes)
23902 {
23903   cp_token *first;
23904   cp_token *last;
23905   tree fn;
23906
23907   /* Create the FUNCTION_DECL.  */
23908   fn = grokmethod (decl_specifiers, declarator, attributes);
23909   cp_finalize_omp_declare_simd (parser, fn);
23910   /* If something went badly wrong, bail out now.  */
23911   if (fn == error_mark_node)
23912     {
23913       /* If there's a function-body, skip it.  */
23914       if (cp_parser_token_starts_function_definition_p
23915           (cp_lexer_peek_token (parser->lexer)))
23916         cp_parser_skip_to_end_of_block_or_statement (parser);
23917       return error_mark_node;
23918     }
23919
23920   /* Remember it, if there default args to post process.  */
23921   cp_parser_save_default_args (parser, fn);
23922
23923   /* Save away the tokens that make up the body of the
23924      function.  */
23925   first = parser->lexer->next_token;
23926   /* Handle function try blocks.  */
23927   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23928     cp_lexer_consume_token (parser->lexer);
23929   /* We can have braced-init-list mem-initializers before the fn body.  */
23930   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23931     {
23932       cp_lexer_consume_token (parser->lexer);
23933       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23934         {
23935           /* cache_group will stop after an un-nested { } pair, too.  */
23936           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23937             break;
23938
23939           /* variadic mem-inits have ... after the ')'.  */
23940           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23941             cp_lexer_consume_token (parser->lexer);
23942         }
23943     }
23944   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23945   /* Handle function try blocks.  */
23946   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23947     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23948   last = parser->lexer->next_token;
23949
23950   /* Save away the inline definition; we will process it when the
23951      class is complete.  */
23952   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23953   DECL_PENDING_INLINE_P (fn) = 1;
23954
23955   /* We need to know that this was defined in the class, so that
23956      friend templates are handled correctly.  */
23957   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23958
23959   /* Add FN to the queue of functions to be parsed later.  */
23960   vec_safe_push (unparsed_funs_with_definitions, fn);
23961
23962   return fn;
23963 }
23964
23965 /* Save the tokens that make up the in-class initializer for a non-static
23966    data member.  Returns a DEFAULT_ARG.  */
23967
23968 static tree
23969 cp_parser_save_nsdmi (cp_parser* parser)
23970 {
23971   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23972 }
23973
23974 /* Parse a template-argument-list, as well as the trailing ">" (but
23975    not the opening "<").  See cp_parser_template_argument_list for the
23976    return value.  */
23977
23978 static tree
23979 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23980 {
23981   tree arguments;
23982   tree saved_scope;
23983   tree saved_qualifying_scope;
23984   tree saved_object_scope;
23985   bool saved_greater_than_is_operator_p;
23986   int saved_unevaluated_operand;
23987   int saved_inhibit_evaluation_warnings;
23988
23989   /* [temp.names]
23990
23991      When parsing a template-id, the first non-nested `>' is taken as
23992      the end of the template-argument-list rather than a greater-than
23993      operator.  */
23994   saved_greater_than_is_operator_p
23995     = parser->greater_than_is_operator_p;
23996   parser->greater_than_is_operator_p = false;
23997   /* Parsing the argument list may modify SCOPE, so we save it
23998      here.  */
23999   saved_scope = parser->scope;
24000   saved_qualifying_scope = parser->qualifying_scope;
24001   saved_object_scope = parser->object_scope;
24002   /* We need to evaluate the template arguments, even though this
24003      template-id may be nested within a "sizeof".  */
24004   saved_unevaluated_operand = cp_unevaluated_operand;
24005   cp_unevaluated_operand = 0;
24006   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24007   c_inhibit_evaluation_warnings = 0;
24008   /* Parse the template-argument-list itself.  */
24009   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24010       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24011     arguments = NULL_TREE;
24012   else
24013     arguments = cp_parser_template_argument_list (parser);
24014   /* Look for the `>' that ends the template-argument-list. If we find
24015      a '>>' instead, it's probably just a typo.  */
24016   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24017     {
24018       if (cxx_dialect != cxx98)
24019         {
24020           /* In C++0x, a `>>' in a template argument list or cast
24021              expression is considered to be two separate `>'
24022              tokens. So, change the current token to a `>', but don't
24023              consume it: it will be consumed later when the outer
24024              template argument list (or cast expression) is parsed.
24025              Note that this replacement of `>' for `>>' is necessary
24026              even if we are parsing tentatively: in the tentative
24027              case, after calling
24028              cp_parser_enclosed_template_argument_list we will always
24029              throw away all of the template arguments and the first
24030              closing `>', either because the template argument list
24031              was erroneous or because we are replacing those tokens
24032              with a CPP_TEMPLATE_ID token.  The second `>' (which will
24033              not have been thrown away) is needed either to close an
24034              outer template argument list or to complete a new-style
24035              cast.  */
24036           cp_token *token = cp_lexer_peek_token (parser->lexer);
24037           token->type = CPP_GREATER;
24038         }
24039       else if (!saved_greater_than_is_operator_p)
24040         {
24041           /* If we're in a nested template argument list, the '>>' has
24042             to be a typo for '> >'. We emit the error message, but we
24043             continue parsing and we push a '>' as next token, so that
24044             the argument list will be parsed correctly.  Note that the
24045             global source location is still on the token before the
24046             '>>', so we need to say explicitly where we want it.  */
24047           cp_token *token = cp_lexer_peek_token (parser->lexer);
24048           error_at (token->location, "%<>>%> should be %<> >%> "
24049                     "within a nested template argument list");
24050
24051           token->type = CPP_GREATER;
24052         }
24053       else
24054         {
24055           /* If this is not a nested template argument list, the '>>'
24056             is a typo for '>'. Emit an error message and continue.
24057             Same deal about the token location, but here we can get it
24058             right by consuming the '>>' before issuing the diagnostic.  */
24059           cp_token *token = cp_lexer_consume_token (parser->lexer);
24060           error_at (token->location,
24061                     "spurious %<>>%>, use %<>%> to terminate "
24062                     "a template argument list");
24063         }
24064     }
24065   else
24066     cp_parser_skip_to_end_of_template_parameter_list (parser);
24067   /* The `>' token might be a greater-than operator again now.  */
24068   parser->greater_than_is_operator_p
24069     = saved_greater_than_is_operator_p;
24070   /* Restore the SAVED_SCOPE.  */
24071   parser->scope = saved_scope;
24072   parser->qualifying_scope = saved_qualifying_scope;
24073   parser->object_scope = saved_object_scope;
24074   cp_unevaluated_operand = saved_unevaluated_operand;
24075   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24076
24077   return arguments;
24078 }
24079
24080 /* MEMBER_FUNCTION is a member function, or a friend.  If default
24081    arguments, or the body of the function have not yet been parsed,
24082    parse them now.  */
24083
24084 static void
24085 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24086 {
24087   timevar_push (TV_PARSE_INMETH);
24088   /* If this member is a template, get the underlying
24089      FUNCTION_DECL.  */
24090   if (DECL_FUNCTION_TEMPLATE_P (member_function))
24091     member_function = DECL_TEMPLATE_RESULT (member_function);
24092
24093   /* There should not be any class definitions in progress at this
24094      point; the bodies of members are only parsed outside of all class
24095      definitions.  */
24096   gcc_assert (parser->num_classes_being_defined == 0);
24097   /* While we're parsing the member functions we might encounter more
24098      classes.  We want to handle them right away, but we don't want
24099      them getting mixed up with functions that are currently in the
24100      queue.  */
24101   push_unparsed_function_queues (parser);
24102
24103   /* Make sure that any template parameters are in scope.  */
24104   maybe_begin_member_template_processing (member_function);
24105
24106   /* If the body of the function has not yet been parsed, parse it
24107      now.  */
24108   if (DECL_PENDING_INLINE_P (member_function))
24109     {
24110       tree function_scope;
24111       cp_token_cache *tokens;
24112
24113       /* The function is no longer pending; we are processing it.  */
24114       tokens = DECL_PENDING_INLINE_INFO (member_function);
24115       DECL_PENDING_INLINE_INFO (member_function) = NULL;
24116       DECL_PENDING_INLINE_P (member_function) = 0;
24117
24118       /* If this is a local class, enter the scope of the containing
24119          function.  */
24120       function_scope = current_function_decl;
24121       if (function_scope)
24122         push_function_context ();
24123
24124       /* Push the body of the function onto the lexer stack.  */
24125       cp_parser_push_lexer_for_tokens (parser, tokens);
24126
24127       /* Let the front end know that we going to be defining this
24128          function.  */
24129       start_preparsed_function (member_function, NULL_TREE,
24130                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
24131
24132       /* Don't do access checking if it is a templated function.  */
24133       if (processing_template_decl)
24134         push_deferring_access_checks (dk_no_check);
24135
24136       /* #pragma omp declare reduction needs special parsing.  */
24137       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24138         {
24139           parser->lexer->in_pragma = true;
24140           cp_parser_omp_declare_reduction_exprs (member_function, parser);
24141           finish_function (/*inline*/2);
24142           cp_check_omp_declare_reduction (member_function);
24143         }
24144       else
24145         /* Now, parse the body of the function.  */
24146         cp_parser_function_definition_after_declarator (parser,
24147                                                         /*inline_p=*/true);
24148
24149       if (processing_template_decl)
24150         pop_deferring_access_checks ();
24151
24152       /* Leave the scope of the containing function.  */
24153       if (function_scope)
24154         pop_function_context ();
24155       cp_parser_pop_lexer (parser);
24156     }
24157
24158   /* Remove any template parameters from the symbol table.  */
24159   maybe_end_member_template_processing ();
24160
24161   /* Restore the queue.  */
24162   pop_unparsed_function_queues (parser);
24163   timevar_pop (TV_PARSE_INMETH);
24164 }
24165
24166 /* If DECL contains any default args, remember it on the unparsed
24167    functions queue.  */
24168
24169 static void
24170 cp_parser_save_default_args (cp_parser* parser, tree decl)
24171 {
24172   tree probe;
24173
24174   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24175        probe;
24176        probe = TREE_CHAIN (probe))
24177     if (TREE_PURPOSE (probe))
24178       {
24179         cp_default_arg_entry entry = {current_class_type, decl};
24180         vec_safe_push (unparsed_funs_with_default_args, entry);
24181         break;
24182       }
24183 }
24184
24185 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24186    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
24187    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
24188    from the parameter-type-list.  */
24189
24190 static tree
24191 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24192                                       tree default_arg, tree parmtype)
24193 {
24194   cp_token_cache *tokens;
24195   tree parsed_arg;
24196   bool dummy;
24197
24198   if (default_arg == error_mark_node)
24199     return error_mark_node;
24200
24201   /* Push the saved tokens for the default argument onto the parser's
24202      lexer stack.  */
24203   tokens = DEFARG_TOKENS (default_arg);
24204   cp_parser_push_lexer_for_tokens (parser, tokens);
24205
24206   start_lambda_scope (decl);
24207
24208   /* Parse the default argument.  */
24209   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24210   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24211     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24212
24213   finish_lambda_scope ();
24214
24215   if (parsed_arg == error_mark_node)
24216     cp_parser_skip_to_end_of_statement (parser);
24217
24218   if (!processing_template_decl)
24219     {
24220       /* In a non-template class, check conversions now.  In a template,
24221          we'll wait and instantiate these as needed.  */
24222       if (TREE_CODE (decl) == PARM_DECL)
24223         parsed_arg = check_default_argument (parmtype, parsed_arg,
24224                                              tf_warning_or_error);
24225       else
24226         parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24227     }
24228
24229   /* If the token stream has not been completely used up, then
24230      there was extra junk after the end of the default
24231      argument.  */
24232   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24233     {
24234       if (TREE_CODE (decl) == PARM_DECL)
24235         cp_parser_error (parser, "expected %<,%>");
24236       else
24237         cp_parser_error (parser, "expected %<;%>");
24238     }
24239
24240   /* Revert to the main lexer.  */
24241   cp_parser_pop_lexer (parser);
24242
24243   return parsed_arg;
24244 }
24245
24246 /* FIELD is a non-static data member with an initializer which we saved for
24247    later; parse it now.  */
24248
24249 static void
24250 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24251 {
24252   tree def;
24253
24254   maybe_begin_member_template_processing (field);
24255
24256   push_unparsed_function_queues (parser);
24257   def = cp_parser_late_parse_one_default_arg (parser, field,
24258                                               DECL_INITIAL (field),
24259                                               NULL_TREE);
24260   pop_unparsed_function_queues (parser);
24261
24262   maybe_end_member_template_processing ();
24263
24264   DECL_INITIAL (field) = def;
24265 }
24266
24267 /* FN is a FUNCTION_DECL which may contains a parameter with an
24268    unparsed DEFAULT_ARG.  Parse the default args now.  This function
24269    assumes that the current scope is the scope in which the default
24270    argument should be processed.  */
24271
24272 static void
24273 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24274 {
24275   bool saved_local_variables_forbidden_p;
24276   tree parm, parmdecl;
24277
24278   /* While we're parsing the default args, we might (due to the
24279      statement expression extension) encounter more classes.  We want
24280      to handle them right away, but we don't want them getting mixed
24281      up with default args that are currently in the queue.  */
24282   push_unparsed_function_queues (parser);
24283
24284   /* Local variable names (and the `this' keyword) may not appear
24285      in a default argument.  */
24286   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24287   parser->local_variables_forbidden_p = true;
24288
24289   push_defarg_context (fn);
24290
24291   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24292          parmdecl = DECL_ARGUMENTS (fn);
24293        parm && parm != void_list_node;
24294        parm = TREE_CHAIN (parm),
24295          parmdecl = DECL_CHAIN (parmdecl))
24296     {
24297       tree default_arg = TREE_PURPOSE (parm);
24298       tree parsed_arg;
24299       vec<tree, va_gc> *insts;
24300       tree copy;
24301       unsigned ix;
24302
24303       if (!default_arg)
24304         continue;
24305
24306       if (TREE_CODE (default_arg) != DEFAULT_ARG)
24307         /* This can happen for a friend declaration for a function
24308            already declared with default arguments.  */
24309         continue;
24310
24311       parsed_arg
24312         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24313                                                 default_arg,
24314                                                 TREE_VALUE (parm));
24315       if (parsed_arg == error_mark_node)
24316         {
24317           continue;
24318         }
24319
24320       TREE_PURPOSE (parm) = parsed_arg;
24321
24322       /* Update any instantiations we've already created.  */
24323       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24324            vec_safe_iterate (insts, ix, &copy); ix++)
24325         TREE_PURPOSE (copy) = parsed_arg;
24326     }
24327
24328   pop_defarg_context ();
24329
24330   /* Make sure no default arg is missing.  */
24331   check_default_args (fn);
24332
24333   /* Restore the state of local_variables_forbidden_p.  */
24334   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24335
24336   /* Restore the queue.  */
24337   pop_unparsed_function_queues (parser);
24338 }
24339
24340 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24341
24342      sizeof ... ( identifier )
24343
24344    where the 'sizeof' token has already been consumed.  */
24345
24346 static tree
24347 cp_parser_sizeof_pack (cp_parser *parser)
24348 {
24349   /* Consume the `...'.  */
24350   cp_lexer_consume_token (parser->lexer);
24351   maybe_warn_variadic_templates ();
24352
24353   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24354   if (paren)
24355     cp_lexer_consume_token (parser->lexer);
24356   else
24357     permerror (cp_lexer_peek_token (parser->lexer)->location,
24358                "%<sizeof...%> argument must be surrounded by parentheses");
24359
24360   cp_token *token = cp_lexer_peek_token (parser->lexer);
24361   tree name = cp_parser_identifier (parser);
24362   if (name == error_mark_node)
24363     return error_mark_node;
24364   /* The name is not qualified.  */
24365   parser->scope = NULL_TREE;
24366   parser->qualifying_scope = NULL_TREE;
24367   parser->object_scope = NULL_TREE;
24368   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24369   if (expr == error_mark_node)
24370     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24371                                  token->location);
24372   if (TREE_CODE (expr) == TYPE_DECL)
24373     expr = TREE_TYPE (expr);
24374   else if (TREE_CODE (expr) == CONST_DECL)
24375     expr = DECL_INITIAL (expr);
24376   expr = make_pack_expansion (expr);
24377
24378   if (paren)
24379     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24380
24381   return expr;
24382 }
24383
24384 /* Parse the operand of `sizeof' (or a similar operator).  Returns
24385    either a TYPE or an expression, depending on the form of the
24386    input.  The KEYWORD indicates which kind of expression we have
24387    encountered.  */
24388
24389 static tree
24390 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24391 {
24392   tree expr = NULL_TREE;
24393   const char *saved_message;
24394   char *tmp;
24395   bool saved_integral_constant_expression_p;
24396   bool saved_non_integral_constant_expression_p;
24397
24398   /* If it's a `...', then we are computing the length of a parameter
24399      pack.  */
24400   if (keyword == RID_SIZEOF
24401       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24402     return cp_parser_sizeof_pack (parser);
24403
24404   /* Types cannot be defined in a `sizeof' expression.  Save away the
24405      old message.  */
24406   saved_message = parser->type_definition_forbidden_message;
24407   /* And create the new one.  */
24408   tmp = concat ("types may not be defined in %<",
24409                 IDENTIFIER_POINTER (ridpointers[keyword]),
24410                 "%> expressions", NULL);
24411   parser->type_definition_forbidden_message = tmp;
24412
24413   /* The restrictions on constant-expressions do not apply inside
24414      sizeof expressions.  */
24415   saved_integral_constant_expression_p
24416     = parser->integral_constant_expression_p;
24417   saved_non_integral_constant_expression_p
24418     = parser->non_integral_constant_expression_p;
24419   parser->integral_constant_expression_p = false;
24420
24421   /* Do not actually evaluate the expression.  */
24422   ++cp_unevaluated_operand;
24423   ++c_inhibit_evaluation_warnings;
24424   /* If it's a `(', then we might be looking at the type-id
24425      construction.  */
24426   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24427     {
24428       tree type = NULL_TREE;
24429
24430       /* We can't be sure yet whether we're looking at a type-id or an
24431          expression.  */
24432       cp_parser_parse_tentatively (parser);
24433       /* Note: as a GNU Extension, compound literals are considered
24434          postfix-expressions as they are in C99, so they are valid
24435          arguments to sizeof.  See comment in cp_parser_cast_expression
24436          for details.  */
24437       if (cp_parser_compound_literal_p (parser))
24438         cp_parser_simulate_error (parser);
24439       else
24440         {
24441           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24442           parser->in_type_id_in_expr_p = true;
24443           /* Look for the type-id.  */
24444           type = cp_parser_type_id (parser);
24445           /* Look for the closing `)'.  */
24446           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24447           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24448         }
24449
24450       /* If all went well, then we're done.  */
24451       if (cp_parser_parse_definitely (parser))
24452         {
24453           cp_decl_specifier_seq decl_specs;
24454
24455           /* Build a trivial decl-specifier-seq.  */
24456           clear_decl_specs (&decl_specs);
24457           decl_specs.type = type;
24458
24459           /* Call grokdeclarator to figure out what type this is.  */
24460           expr = grokdeclarator (NULL,
24461                                  &decl_specs,
24462                                  TYPENAME,
24463                                  /*initialized=*/0,
24464                                  /*attrlist=*/NULL);
24465         }
24466     }
24467
24468   /* If the type-id production did not work out, then we must be
24469      looking at the unary-expression production.  */
24470   if (!expr)
24471     expr = cp_parser_unary_expression (parser);
24472
24473   /* Go back to evaluating expressions.  */
24474   --cp_unevaluated_operand;
24475   --c_inhibit_evaluation_warnings;
24476
24477   /* Free the message we created.  */
24478   free (tmp);
24479   /* And restore the old one.  */
24480   parser->type_definition_forbidden_message = saved_message;
24481   parser->integral_constant_expression_p
24482     = saved_integral_constant_expression_p;
24483   parser->non_integral_constant_expression_p
24484     = saved_non_integral_constant_expression_p;
24485
24486   return expr;
24487 }
24488
24489 /* If the current declaration has no declarator, return true.  */
24490
24491 static bool
24492 cp_parser_declares_only_class_p (cp_parser *parser)
24493 {
24494   /* If the next token is a `;' or a `,' then there is no
24495      declarator.  */
24496   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24497           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24498 }
24499
24500 /* Update the DECL_SPECS to reflect the storage class indicated by
24501    KEYWORD.  */
24502
24503 static void
24504 cp_parser_set_storage_class (cp_parser *parser,
24505                              cp_decl_specifier_seq *decl_specs,
24506                              enum rid keyword,
24507                              cp_token *token)
24508 {
24509   cp_storage_class storage_class;
24510
24511   if (parser->in_unbraced_linkage_specification_p)
24512     {
24513       error_at (token->location, "invalid use of %qD in linkage specification",
24514                 ridpointers[keyword]);
24515       return;
24516     }
24517   else if (decl_specs->storage_class != sc_none)
24518     {
24519       decl_specs->conflicting_specifiers_p = true;
24520       return;
24521     }
24522
24523   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24524       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24525       && decl_specs->gnu_thread_keyword_p)
24526     {
24527       pedwarn (decl_specs->locations[ds_thread], 0,
24528                 "%<__thread%> before %qD", ridpointers[keyword]);
24529     }
24530
24531   switch (keyword)
24532     {
24533     case RID_AUTO:
24534       storage_class = sc_auto;
24535       break;
24536     case RID_REGISTER:
24537       storage_class = sc_register;
24538       break;
24539     case RID_STATIC:
24540       storage_class = sc_static;
24541       break;
24542     case RID_EXTERN:
24543       storage_class = sc_extern;
24544       break;
24545     case RID_MUTABLE:
24546       storage_class = sc_mutable;
24547       break;
24548     default:
24549       gcc_unreachable ();
24550     }
24551   decl_specs->storage_class = storage_class;
24552   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24553
24554   /* A storage class specifier cannot be applied alongside a typedef 
24555      specifier. If there is a typedef specifier present then set 
24556      conflicting_specifiers_p which will trigger an error later
24557      on in grokdeclarator. */
24558   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24559     decl_specs->conflicting_specifiers_p = true;
24560 }
24561
24562 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
24563    is true, the type is a class or enum definition.  */
24564
24565 static void
24566 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24567                               tree type_spec,
24568                               cp_token *token,
24569                               bool type_definition_p)
24570 {
24571   decl_specs->any_specifiers_p = true;
24572
24573   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24574      (with, for example, in "typedef int wchar_t;") we remember that
24575      this is what happened.  In system headers, we ignore these
24576      declarations so that G++ can work with system headers that are not
24577      C++-safe.  */
24578   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24579       && !type_definition_p
24580       && (type_spec == boolean_type_node
24581           || type_spec == char16_type_node
24582           || type_spec == char32_type_node
24583           || type_spec == wchar_type_node)
24584       && (decl_specs->type
24585           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24586           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24587           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24588           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24589     {
24590       decl_specs->redefined_builtin_type = type_spec;
24591       set_and_check_decl_spec_loc (decl_specs,
24592                                    ds_redefined_builtin_type_spec,
24593                                    token);
24594       if (!decl_specs->type)
24595         {
24596           decl_specs->type = type_spec;
24597           decl_specs->type_definition_p = false;
24598           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24599         }
24600     }
24601   else if (decl_specs->type)
24602     decl_specs->multiple_types_p = true;
24603   else
24604     {
24605       decl_specs->type = type_spec;
24606       decl_specs->type_definition_p = type_definition_p;
24607       decl_specs->redefined_builtin_type = NULL_TREE;
24608       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24609     }
24610 }
24611
24612 /* True iff TOKEN is the GNU keyword __thread.  */
24613
24614 static bool
24615 token_is__thread (cp_token *token)
24616 {
24617   gcc_assert (token->keyword == RID_THREAD);
24618   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24619 }
24620
24621 /* Set the location for a declarator specifier and check if it is
24622    duplicated.
24623
24624    DECL_SPECS is the sequence of declarator specifiers onto which to
24625    set the location.
24626
24627    DS is the single declarator specifier to set which location  is to
24628    be set onto the existing sequence of declarators.
24629
24630    LOCATION is the location for the declarator specifier to
24631    consider.  */
24632
24633 static void
24634 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24635                              cp_decl_spec ds, cp_token *token)
24636 {
24637   gcc_assert (ds < ds_last);
24638
24639   if (decl_specs == NULL)
24640     return;
24641
24642   source_location location = token->location;
24643
24644   if (decl_specs->locations[ds] == 0)
24645     {
24646       decl_specs->locations[ds] = location;
24647       if (ds == ds_thread)
24648         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24649     }
24650   else
24651     {
24652       if (ds == ds_long)
24653         {
24654           if (decl_specs->locations[ds_long_long] != 0)
24655             error_at (location,
24656                       "%<long long long%> is too long for GCC");
24657           else
24658             {
24659               decl_specs->locations[ds_long_long] = location;
24660               pedwarn_cxx98 (location,
24661                              OPT_Wlong_long, 
24662                              "ISO C++ 1998 does not support %<long long%>");
24663             }
24664         }
24665       else if (ds == ds_thread)
24666         {
24667           bool gnu = token_is__thread (token);
24668           if (gnu != decl_specs->gnu_thread_keyword_p)
24669             error_at (location,
24670                       "both %<__thread%> and %<thread_local%> specified");
24671           else
24672             error_at (location, "duplicate %qD", token->u.value);
24673         }
24674       else
24675         {
24676           static const char *const decl_spec_names[] = {
24677             "signed",
24678             "unsigned",
24679             "short",
24680             "long",
24681             "const",
24682             "volatile",
24683             "restrict",
24684             "inline",
24685             "virtual",
24686             "explicit",
24687             "friend",
24688             "typedef",
24689             "using",
24690             "constexpr",
24691             "__complex"
24692           };
24693           error_at (location,
24694                     "duplicate %qs", decl_spec_names[ds]);
24695         }
24696     }
24697 }
24698
24699 /* Return true iff the declarator specifier DS is present in the
24700    sequence of declarator specifiers DECL_SPECS.  */
24701
24702 bool
24703 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24704                           cp_decl_spec ds)
24705 {
24706   gcc_assert (ds < ds_last);
24707
24708   if (decl_specs == NULL)
24709     return false;
24710
24711   return decl_specs->locations[ds] != 0;
24712 }
24713
24714 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24715    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
24716
24717 static bool
24718 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24719 {
24720   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24721 }
24722
24723 /* Issue an error message indicating that TOKEN_DESC was expected.
24724    If KEYWORD is true, it indicated this function is called by
24725    cp_parser_require_keword and the required token can only be
24726    a indicated keyword. */
24727
24728 static void
24729 cp_parser_required_error (cp_parser *parser,
24730                           required_token token_desc,
24731                           bool keyword)
24732 {
24733   switch (token_desc)
24734     {
24735       case RT_NEW:
24736         cp_parser_error (parser, "expected %<new%>");
24737         return;
24738       case RT_DELETE:
24739         cp_parser_error (parser, "expected %<delete%>");
24740         return;
24741       case RT_RETURN:
24742         cp_parser_error (parser, "expected %<return%>");
24743         return;
24744       case RT_WHILE:
24745         cp_parser_error (parser, "expected %<while%>");
24746         return;
24747       case RT_EXTERN:
24748         cp_parser_error (parser, "expected %<extern%>");
24749         return;
24750       case RT_STATIC_ASSERT:
24751         cp_parser_error (parser, "expected %<static_assert%>");
24752         return;
24753       case RT_DECLTYPE:
24754         cp_parser_error (parser, "expected %<decltype%>");
24755         return;
24756       case RT_OPERATOR:
24757         cp_parser_error (parser, "expected %<operator%>");
24758         return;
24759       case RT_CLASS:
24760         cp_parser_error (parser, "expected %<class%>");
24761         return;
24762       case RT_TEMPLATE:
24763         cp_parser_error (parser, "expected %<template%>");
24764         return;
24765       case RT_NAMESPACE:
24766         cp_parser_error (parser, "expected %<namespace%>");
24767         return;
24768       case RT_USING:
24769         cp_parser_error (parser, "expected %<using%>");
24770         return;
24771       case RT_ASM:
24772         cp_parser_error (parser, "expected %<asm%>");
24773         return;
24774       case RT_TRY:
24775         cp_parser_error (parser, "expected %<try%>");
24776         return;
24777       case RT_CATCH:
24778         cp_parser_error (parser, "expected %<catch%>");
24779         return;
24780       case RT_THROW:
24781         cp_parser_error (parser, "expected %<throw%>");
24782         return;
24783       case RT_LABEL:
24784         cp_parser_error (parser, "expected %<__label__%>");
24785         return;
24786       case RT_AT_TRY:
24787         cp_parser_error (parser, "expected %<@try%>");
24788         return;
24789       case RT_AT_SYNCHRONIZED:
24790         cp_parser_error (parser, "expected %<@synchronized%>");
24791         return;
24792       case RT_AT_THROW:
24793         cp_parser_error (parser, "expected %<@throw%>");
24794         return;
24795       case RT_TRANSACTION_ATOMIC:
24796         cp_parser_error (parser, "expected %<__transaction_atomic%>");
24797         return;
24798       case RT_TRANSACTION_RELAXED:
24799         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24800         return;
24801       default:
24802         break;
24803     }
24804   if (!keyword)
24805     {
24806       switch (token_desc)
24807         {
24808           case RT_SEMICOLON:
24809             cp_parser_error (parser, "expected %<;%>");
24810             return;
24811           case RT_OPEN_PAREN:
24812             cp_parser_error (parser, "expected %<(%>");
24813             return;
24814           case RT_CLOSE_BRACE:
24815             cp_parser_error (parser, "expected %<}%>");
24816             return;
24817           case RT_OPEN_BRACE:
24818             cp_parser_error (parser, "expected %<{%>");
24819             return;
24820           case RT_CLOSE_SQUARE:
24821             cp_parser_error (parser, "expected %<]%>");
24822             return;
24823           case RT_OPEN_SQUARE:
24824             cp_parser_error (parser, "expected %<[%>");
24825             return;
24826           case RT_COMMA:
24827             cp_parser_error (parser, "expected %<,%>");
24828             return;
24829           case RT_SCOPE:
24830             cp_parser_error (parser, "expected %<::%>");
24831             return;
24832           case RT_LESS:
24833             cp_parser_error (parser, "expected %<<%>");
24834             return;
24835           case RT_GREATER:
24836             cp_parser_error (parser, "expected %<>%>");
24837             return;
24838           case RT_EQ:
24839             cp_parser_error (parser, "expected %<=%>");
24840             return;
24841           case RT_ELLIPSIS:
24842             cp_parser_error (parser, "expected %<...%>");
24843             return;
24844           case RT_MULT:
24845             cp_parser_error (parser, "expected %<*%>");
24846             return;
24847           case RT_COMPL:
24848             cp_parser_error (parser, "expected %<~%>");
24849             return;
24850           case RT_COLON:
24851             cp_parser_error (parser, "expected %<:%>");
24852             return;
24853           case RT_COLON_SCOPE:
24854             cp_parser_error (parser, "expected %<:%> or %<::%>");
24855             return;
24856           case RT_CLOSE_PAREN:
24857             cp_parser_error (parser, "expected %<)%>");
24858             return;
24859           case RT_COMMA_CLOSE_PAREN:
24860             cp_parser_error (parser, "expected %<,%> or %<)%>");
24861             return;
24862           case RT_PRAGMA_EOL:
24863             cp_parser_error (parser, "expected end of line");
24864             return;
24865           case RT_NAME:
24866             cp_parser_error (parser, "expected identifier");
24867             return;
24868           case RT_SELECT:
24869             cp_parser_error (parser, "expected selection-statement");
24870             return;
24871           case RT_INTERATION:
24872             cp_parser_error (parser, "expected iteration-statement");
24873             return;
24874           case RT_JUMP:
24875             cp_parser_error (parser, "expected jump-statement");
24876             return;
24877           case RT_CLASS_KEY:
24878             cp_parser_error (parser, "expected class-key");
24879             return;
24880           case RT_CLASS_TYPENAME_TEMPLATE:
24881             cp_parser_error (parser,
24882                  "expected %<class%>, %<typename%>, or %<template%>");
24883             return;
24884           default:
24885             gcc_unreachable ();
24886         }
24887     }
24888   else
24889     gcc_unreachable ();
24890 }
24891
24892
24893
24894 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
24895    issue an error message indicating that TOKEN_DESC was expected.
24896
24897    Returns the token consumed, if the token had the appropriate type.
24898    Otherwise, returns NULL.  */
24899
24900 static cp_token *
24901 cp_parser_require (cp_parser* parser,
24902                    enum cpp_ttype type,
24903                    required_token token_desc)
24904 {
24905   if (cp_lexer_next_token_is (parser->lexer, type))
24906     return cp_lexer_consume_token (parser->lexer);
24907   else
24908     {
24909       /* Output the MESSAGE -- unless we're parsing tentatively.  */
24910       if (!cp_parser_simulate_error (parser))
24911         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24912       return NULL;
24913     }
24914 }
24915
24916 /* An error message is produced if the next token is not '>'.
24917    All further tokens are skipped until the desired token is
24918    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
24919
24920 static void
24921 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24922 {
24923   /* Current level of '< ... >'.  */
24924   unsigned level = 0;
24925   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
24926   unsigned nesting_depth = 0;
24927
24928   /* Are we ready, yet?  If not, issue error message.  */
24929   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24930     return;
24931
24932   /* Skip tokens until the desired token is found.  */
24933   while (true)
24934     {
24935       /* Peek at the next token.  */
24936       switch (cp_lexer_peek_token (parser->lexer)->type)
24937         {
24938         case CPP_LESS:
24939           if (!nesting_depth)
24940             ++level;
24941           break;
24942
24943         case CPP_RSHIFT:
24944           if (cxx_dialect == cxx98)
24945             /* C++0x views the `>>' operator as two `>' tokens, but
24946                C++98 does not. */
24947             break;
24948           else if (!nesting_depth && level-- == 0)
24949             {
24950               /* We've hit a `>>' where the first `>' closes the
24951                  template argument list, and the second `>' is
24952                  spurious.  Just consume the `>>' and stop; we've
24953                  already produced at least one error.  */
24954               cp_lexer_consume_token (parser->lexer);
24955               return;
24956             }
24957           /* Fall through for C++0x, so we handle the second `>' in
24958              the `>>'.  */
24959
24960         case CPP_GREATER:
24961           if (!nesting_depth && level-- == 0)
24962             {
24963               /* We've reached the token we want, consume it and stop.  */
24964               cp_lexer_consume_token (parser->lexer);
24965               return;
24966             }
24967           break;
24968
24969         case CPP_OPEN_PAREN:
24970         case CPP_OPEN_SQUARE:
24971           ++nesting_depth;
24972           break;
24973
24974         case CPP_CLOSE_PAREN:
24975         case CPP_CLOSE_SQUARE:
24976           if (nesting_depth-- == 0)
24977             return;
24978           break;
24979
24980         case CPP_EOF:
24981         case CPP_PRAGMA_EOL:
24982         case CPP_SEMICOLON:
24983         case CPP_OPEN_BRACE:
24984         case CPP_CLOSE_BRACE:
24985           /* The '>' was probably forgotten, don't look further.  */
24986           return;
24987
24988         default:
24989           break;
24990         }
24991
24992       /* Consume this token.  */
24993       cp_lexer_consume_token (parser->lexer);
24994     }
24995 }
24996
24997 /* If the next token is the indicated keyword, consume it.  Otherwise,
24998    issue an error message indicating that TOKEN_DESC was expected.
24999
25000    Returns the token consumed, if the token had the appropriate type.
25001    Otherwise, returns NULL.  */
25002
25003 static cp_token *
25004 cp_parser_require_keyword (cp_parser* parser,
25005                            enum rid keyword,
25006                            required_token token_desc)
25007 {
25008   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25009
25010   if (token && token->keyword != keyword)
25011     {
25012       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
25013       return NULL;
25014     }
25015
25016   return token;
25017 }
25018
25019 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25020    function-definition.  */
25021
25022 static bool
25023 cp_parser_token_starts_function_definition_p (cp_token* token)
25024 {
25025   return (/* An ordinary function-body begins with an `{'.  */
25026           token->type == CPP_OPEN_BRACE
25027           /* A ctor-initializer begins with a `:'.  */
25028           || token->type == CPP_COLON
25029           /* A function-try-block begins with `try'.  */
25030           || token->keyword == RID_TRY
25031           /* A function-transaction-block begins with `__transaction_atomic'
25032              or `__transaction_relaxed'.  */
25033           || token->keyword == RID_TRANSACTION_ATOMIC
25034           || token->keyword == RID_TRANSACTION_RELAXED
25035           /* The named return value extension begins with `return'.  */
25036           || token->keyword == RID_RETURN);
25037 }
25038
25039 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25040    definition.  */
25041
25042 static bool
25043 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25044 {
25045   cp_token *token;
25046
25047   token = cp_lexer_peek_token (parser->lexer);
25048   return (token->type == CPP_OPEN_BRACE
25049           || (token->type == CPP_COLON
25050               && !parser->colon_doesnt_start_class_def_p));
25051 }
25052
25053 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25054    C++0x) ending a template-argument.  */
25055
25056 static bool
25057 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25058 {
25059   cp_token *token;
25060
25061   token = cp_lexer_peek_token (parser->lexer);
25062   return (token->type == CPP_COMMA 
25063           || token->type == CPP_GREATER
25064           || token->type == CPP_ELLIPSIS
25065           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25066 }
25067
25068 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25069    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
25070
25071 static bool
25072 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25073                                                      size_t n)
25074 {
25075   cp_token *token;
25076
25077   token = cp_lexer_peek_nth_token (parser->lexer, n);
25078   if (token->type == CPP_LESS)
25079     return true;
25080   /* Check for the sequence `<::' in the original code. It would be lexed as
25081      `[:', where `[' is a digraph, and there is no whitespace before
25082      `:'.  */
25083   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25084     {
25085       cp_token *token2;
25086       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25087       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25088         return true;
25089     }
25090   return false;
25091 }
25092
25093 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25094    or none_type otherwise.  */
25095
25096 static enum tag_types
25097 cp_parser_token_is_class_key (cp_token* token)
25098 {
25099   switch (token->keyword)
25100     {
25101     case RID_CLASS:
25102       return class_type;
25103     case RID_STRUCT:
25104       return record_type;
25105     case RID_UNION:
25106       return union_type;
25107
25108     default:
25109       return none_type;
25110     }
25111 }
25112
25113 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25114    or none_type otherwise or if the token is null.  */
25115
25116 static enum tag_types
25117 cp_parser_token_is_type_parameter_key (cp_token* token)
25118 {
25119   if (!token)
25120     return none_type;
25121
25122   switch (token->keyword)
25123     {
25124     case RID_CLASS:
25125       return class_type;
25126     case RID_TYPENAME:
25127       return typename_type;
25128
25129     default:
25130       return none_type;
25131     }
25132 }
25133
25134 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
25135
25136 static void
25137 cp_parser_check_class_key (enum tag_types class_key, tree type)
25138 {
25139   if (type == error_mark_node)
25140     return;
25141   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25142     {
25143       if (permerror (input_location, "%qs tag used in naming %q#T",
25144                      class_key == union_type ? "union"
25145                      : class_key == record_type ? "struct" : "class",
25146                      type))
25147         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25148                 "%q#T was previously declared here", type);
25149     }
25150 }
25151
25152 /* Issue an error message if DECL is redeclared with different
25153    access than its original declaration [class.access.spec/3].
25154    This applies to nested classes and nested class templates.
25155    [class.mem/1].  */
25156
25157 static void
25158 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25159 {
25160   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25161     return;
25162
25163   if ((TREE_PRIVATE (decl)
25164        != (current_access_specifier == access_private_node))
25165       || (TREE_PROTECTED (decl)
25166           != (current_access_specifier == access_protected_node)))
25167     error_at (location, "%qD redeclared with different access", decl);
25168 }
25169
25170 /* Look for the `template' keyword, as a syntactic disambiguator.
25171    Return TRUE iff it is present, in which case it will be
25172    consumed.  */
25173
25174 static bool
25175 cp_parser_optional_template_keyword (cp_parser *parser)
25176 {
25177   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25178     {
25179       /* In C++98 the `template' keyword can only be used within templates;
25180          outside templates the parser can always figure out what is a
25181          template and what is not.  In C++11,  per the resolution of DR 468,
25182          `template' is allowed in cases where it is not strictly necessary.  */
25183       if (!processing_template_decl
25184           && pedantic && cxx_dialect == cxx98)
25185         {
25186           cp_token *token = cp_lexer_peek_token (parser->lexer);
25187           pedwarn (token->location, OPT_Wpedantic,
25188                    "in C++98 %<template%> (as a disambiguator) is only "
25189                    "allowed within templates");
25190           /* If this part of the token stream is rescanned, the same
25191              error message would be generated.  So, we purge the token
25192              from the stream.  */
25193           cp_lexer_purge_token (parser->lexer);
25194           return false;
25195         }
25196       else
25197         {
25198           /* Consume the `template' keyword.  */
25199           cp_lexer_consume_token (parser->lexer);
25200           return true;
25201         }
25202     }
25203   return false;
25204 }
25205
25206 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
25207    set PARSER->SCOPE, and perform other related actions.  */
25208
25209 static void
25210 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25211 {
25212   int i;
25213   struct tree_check *check_value;
25214   deferred_access_check *chk;
25215   vec<deferred_access_check, va_gc> *checks;
25216
25217   /* Get the stored value.  */
25218   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25219   /* Perform any access checks that were deferred.  */
25220   checks = check_value->checks;
25221   if (checks)
25222     {
25223       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25224         perform_or_defer_access_check (chk->binfo,
25225                                        chk->decl,
25226                                        chk->diag_decl, tf_warning_or_error);
25227     }
25228   /* Set the scope from the stored value.  */
25229   parser->scope = check_value->value;
25230   parser->qualifying_scope = check_value->qualifying_scope;
25231   parser->object_scope = NULL_TREE;
25232 }
25233
25234 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
25235    encounter the end of a block before what we were looking for.  */
25236
25237 static bool
25238 cp_parser_cache_group (cp_parser *parser,
25239                        enum cpp_ttype end,
25240                        unsigned depth)
25241 {
25242   while (true)
25243     {
25244       cp_token *token = cp_lexer_peek_token (parser->lexer);
25245
25246       /* Abort a parenthesized expression if we encounter a semicolon.  */
25247       if ((end == CPP_CLOSE_PAREN || depth == 0)
25248           && token->type == CPP_SEMICOLON)
25249         return true;
25250       /* If we've reached the end of the file, stop.  */
25251       if (token->type == CPP_EOF
25252           || (end != CPP_PRAGMA_EOL
25253               && token->type == CPP_PRAGMA_EOL))
25254         return true;
25255       if (token->type == CPP_CLOSE_BRACE && depth == 0)
25256         /* We've hit the end of an enclosing block, so there's been some
25257            kind of syntax error.  */
25258         return true;
25259
25260       /* Consume the token.  */
25261       cp_lexer_consume_token (parser->lexer);
25262       /* See if it starts a new group.  */
25263       if (token->type == CPP_OPEN_BRACE)
25264         {
25265           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25266           /* In theory this should probably check end == '}', but
25267              cp_parser_save_member_function_body needs it to exit
25268              after either '}' or ')' when called with ')'.  */
25269           if (depth == 0)
25270             return false;
25271         }
25272       else if (token->type == CPP_OPEN_PAREN)
25273         {
25274           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25275           if (depth == 0 && end == CPP_CLOSE_PAREN)
25276             return false;
25277         }
25278       else if (token->type == CPP_PRAGMA)
25279         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25280       else if (token->type == end)
25281         return false;
25282     }
25283 }
25284
25285 /* Like above, for caching a default argument or NSDMI.  Both of these are
25286    terminated by a non-nested comma, but it can be unclear whether or not a
25287    comma is nested in a template argument list unless we do more parsing.
25288    In order to handle this ambiguity, when we encounter a ',' after a '<'
25289    we try to parse what follows as a parameter-declaration-list (in the
25290    case of a default argument) or a member-declarator (in the case of an
25291    NSDMI).  If that succeeds, then we stop caching.  */
25292
25293 static tree
25294 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25295 {
25296   unsigned depth = 0;
25297   int maybe_template_id = 0;
25298   cp_token *first_token;
25299   cp_token *token;
25300   tree default_argument;
25301
25302   /* Add tokens until we have processed the entire default
25303      argument.  We add the range [first_token, token).  */
25304   first_token = cp_lexer_peek_token (parser->lexer);
25305   if (first_token->type == CPP_OPEN_BRACE)
25306     {
25307       /* For list-initialization, this is straightforward.  */
25308       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25309       token = cp_lexer_peek_token (parser->lexer);
25310     }
25311   else while (true)
25312     {
25313       bool done = false;
25314
25315       /* Peek at the next token.  */
25316       token = cp_lexer_peek_token (parser->lexer);
25317       /* What we do depends on what token we have.  */
25318       switch (token->type)
25319         {
25320           /* In valid code, a default argument must be
25321              immediately followed by a `,' `)', or `...'.  */
25322         case CPP_COMMA:
25323           if (depth == 0 && maybe_template_id)
25324             {
25325               /* If we've seen a '<', we might be in a
25326                  template-argument-list.  Until Core issue 325 is
25327                  resolved, we don't know how this situation ought
25328                  to be handled, so try to DTRT.  We check whether
25329                  what comes after the comma is a valid parameter
25330                  declaration list.  If it is, then the comma ends
25331                  the default argument; otherwise the default
25332                  argument continues.  */
25333               bool error = false;
25334
25335               /* Set ITALP so cp_parser_parameter_declaration_list
25336                  doesn't decide to commit to this parse.  */
25337               bool saved_italp = parser->in_template_argument_list_p;
25338               parser->in_template_argument_list_p = true;
25339
25340               cp_parser_parse_tentatively (parser);
25341               cp_lexer_consume_token (parser->lexer);
25342
25343               if (nsdmi)
25344                 {
25345                   int ctor_dtor_or_conv_p;
25346                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25347                                         &ctor_dtor_or_conv_p,
25348                                         /*parenthesized_p=*/NULL,
25349                                         /*member_p=*/true,
25350                                         /*friend_p=*/false);
25351                 }
25352               else
25353                 {
25354                   begin_scope (sk_function_parms, NULL_TREE);
25355                   cp_parser_parameter_declaration_list (parser, &error);
25356                   pop_bindings_and_leave_scope ();
25357                 }
25358               if (!cp_parser_error_occurred (parser) && !error)
25359                 done = true;
25360               cp_parser_abort_tentative_parse (parser);
25361
25362               parser->in_template_argument_list_p = saved_italp;
25363               break;
25364             }
25365         case CPP_CLOSE_PAREN:
25366         case CPP_ELLIPSIS:
25367           /* If we run into a non-nested `;', `}', or `]',
25368              then the code is invalid -- but the default
25369              argument is certainly over.  */
25370         case CPP_SEMICOLON:
25371         case CPP_CLOSE_BRACE:
25372         case CPP_CLOSE_SQUARE:
25373           if (depth == 0
25374               /* Handle correctly int n = sizeof ... ( p );  */
25375               && token->type != CPP_ELLIPSIS)
25376             done = true;
25377           /* Update DEPTH, if necessary.  */
25378           else if (token->type == CPP_CLOSE_PAREN
25379                    || token->type == CPP_CLOSE_BRACE
25380                    || token->type == CPP_CLOSE_SQUARE)
25381             --depth;
25382           break;
25383
25384         case CPP_OPEN_PAREN:
25385         case CPP_OPEN_SQUARE:
25386         case CPP_OPEN_BRACE:
25387           ++depth;
25388           break;
25389
25390         case CPP_LESS:
25391           if (depth == 0)
25392             /* This might be the comparison operator, or it might
25393                start a template argument list.  */
25394             ++maybe_template_id;
25395           break;
25396
25397         case CPP_RSHIFT:
25398           if (cxx_dialect == cxx98)
25399             break;
25400           /* Fall through for C++0x, which treats the `>>'
25401              operator like two `>' tokens in certain
25402              cases.  */
25403
25404         case CPP_GREATER:
25405           if (depth == 0)
25406             {
25407               /* This might be an operator, or it might close a
25408                  template argument list.  But if a previous '<'
25409                  started a template argument list, this will have
25410                  closed it, so we can't be in one anymore.  */
25411               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25412               if (maybe_template_id < 0)
25413                 maybe_template_id = 0;
25414             }
25415           break;
25416
25417           /* If we run out of tokens, issue an error message.  */
25418         case CPP_EOF:
25419         case CPP_PRAGMA_EOL:
25420           error_at (token->location, "file ends in default argument");
25421           done = true;
25422           break;
25423
25424         case CPP_NAME:
25425         case CPP_SCOPE:
25426           /* In these cases, we should look for template-ids.
25427              For example, if the default argument is
25428              `X<int, double>()', we need to do name lookup to
25429              figure out whether or not `X' is a template; if
25430              so, the `,' does not end the default argument.
25431
25432              That is not yet done.  */
25433           break;
25434
25435         default:
25436           break;
25437         }
25438
25439       /* If we've reached the end, stop.  */
25440       if (done)
25441         break;
25442
25443       /* Add the token to the token block.  */
25444       token = cp_lexer_consume_token (parser->lexer);
25445     }
25446
25447   /* Create a DEFAULT_ARG to represent the unparsed default
25448      argument.  */
25449   default_argument = make_node (DEFAULT_ARG);
25450   DEFARG_TOKENS (default_argument)
25451     = cp_token_cache_new (first_token, token);
25452   DEFARG_INSTANTIATIONS (default_argument) = NULL;
25453
25454   return default_argument;
25455 }
25456
25457 /* Begin parsing tentatively.  We always save tokens while parsing
25458    tentatively so that if the tentative parsing fails we can restore the
25459    tokens.  */
25460
25461 static void
25462 cp_parser_parse_tentatively (cp_parser* parser)
25463 {
25464   /* Enter a new parsing context.  */
25465   parser->context = cp_parser_context_new (parser->context);
25466   /* Begin saving tokens.  */
25467   cp_lexer_save_tokens (parser->lexer);
25468   /* In order to avoid repetitive access control error messages,
25469      access checks are queued up until we are no longer parsing
25470      tentatively.  */
25471   push_deferring_access_checks (dk_deferred);
25472 }
25473
25474 /* Commit to the currently active tentative parse.  */
25475
25476 static void
25477 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25478 {
25479   cp_parser_context *context;
25480   cp_lexer *lexer;
25481
25482   /* Mark all of the levels as committed.  */
25483   lexer = parser->lexer;
25484   for (context = parser->context; context->next; context = context->next)
25485     {
25486       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25487         break;
25488       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25489       while (!cp_lexer_saving_tokens (lexer))
25490         lexer = lexer->next;
25491       cp_lexer_commit_tokens (lexer);
25492     }
25493 }
25494
25495 /* Commit to the topmost currently active tentative parse.
25496
25497    Note that this function shouldn't be called when there are
25498    irreversible side-effects while in a tentative state.  For
25499    example, we shouldn't create a permanent entry in the symbol
25500    table, or issue an error message that might not apply if the
25501    tentative parse is aborted.  */
25502
25503 static void
25504 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25505 {
25506   cp_parser_context *context = parser->context;
25507   cp_lexer *lexer = parser->lexer;
25508
25509   if (context)
25510     {
25511       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25512         return;
25513       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25514
25515       while (!cp_lexer_saving_tokens (lexer))
25516         lexer = lexer->next;
25517       cp_lexer_commit_tokens (lexer);
25518     }
25519 }
25520
25521 /* Abort the currently active tentative parse.  All consumed tokens
25522    will be rolled back, and no diagnostics will be issued.  */
25523
25524 static void
25525 cp_parser_abort_tentative_parse (cp_parser* parser)
25526 {
25527   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25528               || errorcount > 0);
25529   cp_parser_simulate_error (parser);
25530   /* Now, pretend that we want to see if the construct was
25531      successfully parsed.  */
25532   cp_parser_parse_definitely (parser);
25533 }
25534
25535 /* Stop parsing tentatively.  If a parse error has occurred, restore the
25536    token stream.  Otherwise, commit to the tokens we have consumed.
25537    Returns true if no error occurred; false otherwise.  */
25538
25539 static bool
25540 cp_parser_parse_definitely (cp_parser* parser)
25541 {
25542   bool error_occurred;
25543   cp_parser_context *context;
25544
25545   /* Remember whether or not an error occurred, since we are about to
25546      destroy that information.  */
25547   error_occurred = cp_parser_error_occurred (parser);
25548   /* Remove the topmost context from the stack.  */
25549   context = parser->context;
25550   parser->context = context->next;
25551   /* If no parse errors occurred, commit to the tentative parse.  */
25552   if (!error_occurred)
25553     {
25554       /* Commit to the tokens read tentatively, unless that was
25555          already done.  */
25556       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25557         cp_lexer_commit_tokens (parser->lexer);
25558
25559       pop_to_parent_deferring_access_checks ();
25560     }
25561   /* Otherwise, if errors occurred, roll back our state so that things
25562      are just as they were before we began the tentative parse.  */
25563   else
25564     {
25565       cp_lexer_rollback_tokens (parser->lexer);
25566       pop_deferring_access_checks ();
25567     }
25568   /* Add the context to the front of the free list.  */
25569   context->next = cp_parser_context_free_list;
25570   cp_parser_context_free_list = context;
25571
25572   return !error_occurred;
25573 }
25574
25575 /* Returns true if we are parsing tentatively and are not committed to
25576    this tentative parse.  */
25577
25578 static bool
25579 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25580 {
25581   return (cp_parser_parsing_tentatively (parser)
25582           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25583 }
25584
25585 /* Returns nonzero iff an error has occurred during the most recent
25586    tentative parse.  */
25587
25588 static bool
25589 cp_parser_error_occurred (cp_parser* parser)
25590 {
25591   return (cp_parser_parsing_tentatively (parser)
25592           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25593 }
25594
25595 /* Returns nonzero if GNU extensions are allowed.  */
25596
25597 static bool
25598 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25599 {
25600   return parser->allow_gnu_extensions_p;
25601 }
25602 \f
25603 /* Objective-C++ Productions */
25604
25605
25606 /* Parse an Objective-C expression, which feeds into a primary-expression
25607    above.
25608
25609    objc-expression:
25610      objc-message-expression
25611      objc-string-literal
25612      objc-encode-expression
25613      objc-protocol-expression
25614      objc-selector-expression
25615
25616   Returns a tree representation of the expression.  */
25617
25618 static tree
25619 cp_parser_objc_expression (cp_parser* parser)
25620 {
25621   /* Try to figure out what kind of declaration is present.  */
25622   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25623
25624   switch (kwd->type)
25625     {
25626     case CPP_OPEN_SQUARE:
25627       return cp_parser_objc_message_expression (parser);
25628
25629     case CPP_OBJC_STRING:
25630       kwd = cp_lexer_consume_token (parser->lexer);
25631       return objc_build_string_object (kwd->u.value);
25632
25633     case CPP_KEYWORD:
25634       switch (kwd->keyword)
25635         {
25636         case RID_AT_ENCODE:
25637           return cp_parser_objc_encode_expression (parser);
25638
25639         case RID_AT_PROTOCOL:
25640           return cp_parser_objc_protocol_expression (parser);
25641
25642         case RID_AT_SELECTOR:
25643           return cp_parser_objc_selector_expression (parser);
25644
25645         default:
25646           break;
25647         }
25648     default:
25649       error_at (kwd->location,
25650                 "misplaced %<@%D%> Objective-C++ construct",
25651                 kwd->u.value);
25652       cp_parser_skip_to_end_of_block_or_statement (parser);
25653     }
25654
25655   return error_mark_node;
25656 }
25657
25658 /* Parse an Objective-C message expression.
25659
25660    objc-message-expression:
25661      [ objc-message-receiver objc-message-args ]
25662
25663    Returns a representation of an Objective-C message.  */
25664
25665 static tree
25666 cp_parser_objc_message_expression (cp_parser* parser)
25667 {
25668   tree receiver, messageargs;
25669
25670   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
25671   receiver = cp_parser_objc_message_receiver (parser);
25672   messageargs = cp_parser_objc_message_args (parser);
25673   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25674
25675   return objc_build_message_expr (receiver, messageargs);
25676 }
25677
25678 /* Parse an objc-message-receiver.
25679
25680    objc-message-receiver:
25681      expression
25682      simple-type-specifier
25683
25684   Returns a representation of the type or expression.  */
25685
25686 static tree
25687 cp_parser_objc_message_receiver (cp_parser* parser)
25688 {
25689   tree rcv;
25690
25691   /* An Objective-C message receiver may be either (1) a type
25692      or (2) an expression.  */
25693   cp_parser_parse_tentatively (parser);
25694   rcv = cp_parser_expression (parser);
25695
25696   /* If that worked out, fine.  */
25697   if (cp_parser_parse_definitely (parser))
25698     return rcv;
25699
25700   cp_parser_parse_tentatively (parser);
25701   rcv = cp_parser_simple_type_specifier (parser,
25702                                          /*decl_specs=*/NULL,
25703                                          CP_PARSER_FLAGS_NONE);
25704
25705   if (cp_parser_parse_definitely (parser))
25706     return objc_get_class_reference (rcv);
25707   
25708   cp_parser_error (parser, "objective-c++ message receiver expected");
25709   return error_mark_node;
25710 }
25711
25712 /* Parse the arguments and selectors comprising an Objective-C message.
25713
25714    objc-message-args:
25715      objc-selector
25716      objc-selector-args
25717      objc-selector-args , objc-comma-args
25718
25719    objc-selector-args:
25720      objc-selector [opt] : assignment-expression
25721      objc-selector-args objc-selector [opt] : assignment-expression
25722
25723    objc-comma-args:
25724      assignment-expression
25725      objc-comma-args , assignment-expression
25726
25727    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25728    selector arguments and TREE_VALUE containing a list of comma
25729    arguments.  */
25730
25731 static tree
25732 cp_parser_objc_message_args (cp_parser* parser)
25733 {
25734   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25735   bool maybe_unary_selector_p = true;
25736   cp_token *token = cp_lexer_peek_token (parser->lexer);
25737
25738   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25739     {
25740       tree selector = NULL_TREE, arg;
25741
25742       if (token->type != CPP_COLON)
25743         selector = cp_parser_objc_selector (parser);
25744
25745       /* Detect if we have a unary selector.  */
25746       if (maybe_unary_selector_p
25747           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25748         return build_tree_list (selector, NULL_TREE);
25749
25750       maybe_unary_selector_p = false;
25751       cp_parser_require (parser, CPP_COLON, RT_COLON);
25752       arg = cp_parser_assignment_expression (parser);
25753
25754       sel_args
25755         = chainon (sel_args,
25756                    build_tree_list (selector, arg));
25757
25758       token = cp_lexer_peek_token (parser->lexer);
25759     }
25760
25761   /* Handle non-selector arguments, if any. */
25762   while (token->type == CPP_COMMA)
25763     {
25764       tree arg;
25765
25766       cp_lexer_consume_token (parser->lexer);
25767       arg = cp_parser_assignment_expression (parser);
25768
25769       addl_args
25770         = chainon (addl_args,
25771                    build_tree_list (NULL_TREE, arg));
25772
25773       token = cp_lexer_peek_token (parser->lexer);
25774     }
25775
25776   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25777     {
25778       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25779       return build_tree_list (error_mark_node, error_mark_node);
25780     }
25781
25782   return build_tree_list (sel_args, addl_args);
25783 }
25784
25785 /* Parse an Objective-C encode expression.
25786
25787    objc-encode-expression:
25788      @encode objc-typename
25789
25790    Returns an encoded representation of the type argument.  */
25791
25792 static tree
25793 cp_parser_objc_encode_expression (cp_parser* parser)
25794 {
25795   tree type;
25796   cp_token *token;
25797
25798   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
25799   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25800   token = cp_lexer_peek_token (parser->lexer);
25801   type = complete_type (cp_parser_type_id (parser));
25802   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25803
25804   if (!type)
25805     {
25806       error_at (token->location, 
25807                 "%<@encode%> must specify a type as an argument");
25808       return error_mark_node;
25809     }
25810
25811   /* This happens if we find @encode(T) (where T is a template
25812      typename or something dependent on a template typename) when
25813      parsing a template.  In that case, we can't compile it
25814      immediately, but we rather create an AT_ENCODE_EXPR which will
25815      need to be instantiated when the template is used.
25816   */
25817   if (dependent_type_p (type))
25818     {
25819       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25820       TREE_READONLY (value) = 1;
25821       return value;
25822     }
25823
25824   return objc_build_encode_expr (type);
25825 }
25826
25827 /* Parse an Objective-C @defs expression.  */
25828
25829 static tree
25830 cp_parser_objc_defs_expression (cp_parser *parser)
25831 {
25832   tree name;
25833
25834   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
25835   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25836   name = cp_parser_identifier (parser);
25837   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25838
25839   return objc_get_class_ivars (name);
25840 }
25841
25842 /* Parse an Objective-C protocol expression.
25843
25844   objc-protocol-expression:
25845     @protocol ( identifier )
25846
25847   Returns a representation of the protocol expression.  */
25848
25849 static tree
25850 cp_parser_objc_protocol_expression (cp_parser* parser)
25851 {
25852   tree proto;
25853
25854   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
25855   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25856   proto = cp_parser_identifier (parser);
25857   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25858
25859   return objc_build_protocol_expr (proto);
25860 }
25861
25862 /* Parse an Objective-C selector expression.
25863
25864    objc-selector-expression:
25865      @selector ( objc-method-signature )
25866
25867    objc-method-signature:
25868      objc-selector
25869      objc-selector-seq
25870
25871    objc-selector-seq:
25872      objc-selector :
25873      objc-selector-seq objc-selector :
25874
25875   Returns a representation of the method selector.  */
25876
25877 static tree
25878 cp_parser_objc_selector_expression (cp_parser* parser)
25879 {
25880   tree sel_seq = NULL_TREE;
25881   bool maybe_unary_selector_p = true;
25882   cp_token *token;
25883   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25884
25885   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
25886   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25887   token = cp_lexer_peek_token (parser->lexer);
25888
25889   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25890          || token->type == CPP_SCOPE)
25891     {
25892       tree selector = NULL_TREE;
25893
25894       if (token->type != CPP_COLON
25895           || token->type == CPP_SCOPE)
25896         selector = cp_parser_objc_selector (parser);
25897
25898       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25899           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25900         {
25901           /* Detect if we have a unary selector.  */
25902           if (maybe_unary_selector_p)
25903             {
25904               sel_seq = selector;
25905               goto finish_selector;
25906             }
25907           else
25908             {
25909               cp_parser_error (parser, "expected %<:%>");
25910             }
25911         }
25912       maybe_unary_selector_p = false;
25913       token = cp_lexer_consume_token (parser->lexer);
25914
25915       if (token->type == CPP_SCOPE)
25916         {
25917           sel_seq
25918             = chainon (sel_seq,
25919                        build_tree_list (selector, NULL_TREE));
25920           sel_seq
25921             = chainon (sel_seq,
25922                        build_tree_list (NULL_TREE, NULL_TREE));
25923         }
25924       else
25925         sel_seq
25926           = chainon (sel_seq,
25927                      build_tree_list (selector, NULL_TREE));
25928
25929       token = cp_lexer_peek_token (parser->lexer);
25930     }
25931
25932  finish_selector:
25933   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25934
25935   return objc_build_selector_expr (loc, sel_seq);
25936 }
25937
25938 /* Parse a list of identifiers.
25939
25940    objc-identifier-list:
25941      identifier
25942      objc-identifier-list , identifier
25943
25944    Returns a TREE_LIST of identifier nodes.  */
25945
25946 static tree
25947 cp_parser_objc_identifier_list (cp_parser* parser)
25948 {
25949   tree identifier;
25950   tree list;
25951   cp_token *sep;
25952
25953   identifier = cp_parser_identifier (parser);
25954   if (identifier == error_mark_node)
25955     return error_mark_node;      
25956
25957   list = build_tree_list (NULL_TREE, identifier);
25958   sep = cp_lexer_peek_token (parser->lexer);
25959
25960   while (sep->type == CPP_COMMA)
25961     {
25962       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
25963       identifier = cp_parser_identifier (parser);
25964       if (identifier == error_mark_node)
25965         return list;
25966
25967       list = chainon (list, build_tree_list (NULL_TREE,
25968                                              identifier));
25969       sep = cp_lexer_peek_token (parser->lexer);
25970     }
25971   
25972   return list;
25973 }
25974
25975 /* Parse an Objective-C alias declaration.
25976
25977    objc-alias-declaration:
25978      @compatibility_alias identifier identifier ;
25979
25980    This function registers the alias mapping with the Objective-C front end.
25981    It returns nothing.  */
25982
25983 static void
25984 cp_parser_objc_alias_declaration (cp_parser* parser)
25985 {
25986   tree alias, orig;
25987
25988   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
25989   alias = cp_parser_identifier (parser);
25990   orig = cp_parser_identifier (parser);
25991   objc_declare_alias (alias, orig);
25992   cp_parser_consume_semicolon_at_end_of_statement (parser);
25993 }
25994
25995 /* Parse an Objective-C class forward-declaration.
25996
25997    objc-class-declaration:
25998      @class objc-identifier-list ;
25999
26000    The function registers the forward declarations with the Objective-C
26001    front end.  It returns nothing.  */
26002
26003 static void
26004 cp_parser_objc_class_declaration (cp_parser* parser)
26005 {
26006   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
26007   while (true)
26008     {
26009       tree id;
26010       
26011       id = cp_parser_identifier (parser);
26012       if (id == error_mark_node)
26013         break;
26014       
26015       objc_declare_class (id);
26016
26017       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26018         cp_lexer_consume_token (parser->lexer);
26019       else
26020         break;
26021     }
26022   cp_parser_consume_semicolon_at_end_of_statement (parser);
26023 }
26024
26025 /* Parse a list of Objective-C protocol references.
26026
26027    objc-protocol-refs-opt:
26028      objc-protocol-refs [opt]
26029
26030    objc-protocol-refs:
26031      < objc-identifier-list >
26032
26033    Returns a TREE_LIST of identifiers, if any.  */
26034
26035 static tree
26036 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26037 {
26038   tree protorefs = NULL_TREE;
26039
26040   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26041     {
26042       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
26043       protorefs = cp_parser_objc_identifier_list (parser);
26044       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26045     }
26046
26047   return protorefs;
26048 }
26049
26050 /* Parse a Objective-C visibility specification.  */
26051
26052 static void
26053 cp_parser_objc_visibility_spec (cp_parser* parser)
26054 {
26055   cp_token *vis = cp_lexer_peek_token (parser->lexer);
26056
26057   switch (vis->keyword)
26058     {
26059     case RID_AT_PRIVATE:
26060       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26061       break;
26062     case RID_AT_PROTECTED:
26063       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26064       break;
26065     case RID_AT_PUBLIC:
26066       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26067       break;
26068     case RID_AT_PACKAGE:
26069       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26070       break;
26071     default:
26072       return;
26073     }
26074
26075   /* Eat '@private'/'@protected'/'@public'.  */
26076   cp_lexer_consume_token (parser->lexer);
26077 }
26078
26079 /* Parse an Objective-C method type.  Return 'true' if it is a class
26080    (+) method, and 'false' if it is an instance (-) method.  */
26081
26082 static inline bool
26083 cp_parser_objc_method_type (cp_parser* parser)
26084 {
26085   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26086     return true;
26087   else
26088     return false;
26089 }
26090
26091 /* Parse an Objective-C protocol qualifier.  */
26092
26093 static tree
26094 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26095 {
26096   tree quals = NULL_TREE, node;
26097   cp_token *token = cp_lexer_peek_token (parser->lexer);
26098
26099   node = token->u.value;
26100
26101   while (node && identifier_p (node)
26102          && (node == ridpointers [(int) RID_IN]
26103              || node == ridpointers [(int) RID_OUT]
26104              || node == ridpointers [(int) RID_INOUT]
26105              || node == ridpointers [(int) RID_BYCOPY]
26106              || node == ridpointers [(int) RID_BYREF]
26107              || node == ridpointers [(int) RID_ONEWAY]))
26108     {
26109       quals = tree_cons (NULL_TREE, node, quals);
26110       cp_lexer_consume_token (parser->lexer);
26111       token = cp_lexer_peek_token (parser->lexer);
26112       node = token->u.value;
26113     }
26114
26115   return quals;
26116 }
26117
26118 /* Parse an Objective-C typename.  */
26119
26120 static tree
26121 cp_parser_objc_typename (cp_parser* parser)
26122 {
26123   tree type_name = NULL_TREE;
26124
26125   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26126     {
26127       tree proto_quals, cp_type = NULL_TREE;
26128
26129       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26130       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26131
26132       /* An ObjC type name may consist of just protocol qualifiers, in which
26133          case the type shall default to 'id'.  */
26134       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26135         {
26136           cp_type = cp_parser_type_id (parser);
26137           
26138           /* If the type could not be parsed, an error has already
26139              been produced.  For error recovery, behave as if it had
26140              not been specified, which will use the default type
26141              'id'.  */
26142           if (cp_type == error_mark_node)
26143             {
26144               cp_type = NULL_TREE;
26145               /* We need to skip to the closing parenthesis as
26146                  cp_parser_type_id() does not seem to do it for
26147                  us.  */
26148               cp_parser_skip_to_closing_parenthesis (parser,
26149                                                      /*recovering=*/true,
26150                                                      /*or_comma=*/false,
26151                                                      /*consume_paren=*/false);
26152             }
26153         }
26154
26155       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26156       type_name = build_tree_list (proto_quals, cp_type);
26157     }
26158
26159   return type_name;
26160 }
26161
26162 /* Check to see if TYPE refers to an Objective-C selector name.  */
26163
26164 static bool
26165 cp_parser_objc_selector_p (enum cpp_ttype type)
26166 {
26167   return (type == CPP_NAME || type == CPP_KEYWORD
26168           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26169           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26170           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26171           || type == CPP_XOR || type == CPP_XOR_EQ);
26172 }
26173
26174 /* Parse an Objective-C selector.  */
26175
26176 static tree
26177 cp_parser_objc_selector (cp_parser* parser)
26178 {
26179   cp_token *token = cp_lexer_consume_token (parser->lexer);
26180
26181   if (!cp_parser_objc_selector_p (token->type))
26182     {
26183       error_at (token->location, "invalid Objective-C++ selector name");
26184       return error_mark_node;
26185     }
26186
26187   /* C++ operator names are allowed to appear in ObjC selectors.  */
26188   switch (token->type)
26189     {
26190     case CPP_AND_AND: return get_identifier ("and");
26191     case CPP_AND_EQ: return get_identifier ("and_eq");
26192     case CPP_AND: return get_identifier ("bitand");
26193     case CPP_OR: return get_identifier ("bitor");
26194     case CPP_COMPL: return get_identifier ("compl");
26195     case CPP_NOT: return get_identifier ("not");
26196     case CPP_NOT_EQ: return get_identifier ("not_eq");
26197     case CPP_OR_OR: return get_identifier ("or");
26198     case CPP_OR_EQ: return get_identifier ("or_eq");
26199     case CPP_XOR: return get_identifier ("xor");
26200     case CPP_XOR_EQ: return get_identifier ("xor_eq");
26201     default: return token->u.value;
26202     }
26203 }
26204
26205 /* Parse an Objective-C params list.  */
26206
26207 static tree
26208 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26209 {
26210   tree params = NULL_TREE;
26211   bool maybe_unary_selector_p = true;
26212   cp_token *token = cp_lexer_peek_token (parser->lexer);
26213
26214   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26215     {
26216       tree selector = NULL_TREE, type_name, identifier;
26217       tree parm_attr = NULL_TREE;
26218
26219       if (token->keyword == RID_ATTRIBUTE)
26220         break;
26221
26222       if (token->type != CPP_COLON)
26223         selector = cp_parser_objc_selector (parser);
26224
26225       /* Detect if we have a unary selector.  */
26226       if (maybe_unary_selector_p
26227           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26228         {
26229           params = selector; /* Might be followed by attributes.  */
26230           break;
26231         }
26232
26233       maybe_unary_selector_p = false;
26234       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26235         {
26236           /* Something went quite wrong.  There should be a colon
26237              here, but there is not.  Stop parsing parameters.  */
26238           break;
26239         }
26240       type_name = cp_parser_objc_typename (parser);
26241       /* New ObjC allows attributes on parameters too.  */
26242       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26243         parm_attr = cp_parser_attributes_opt (parser);
26244       identifier = cp_parser_identifier (parser);
26245
26246       params
26247         = chainon (params,
26248                    objc_build_keyword_decl (selector,
26249                                             type_name,
26250                                             identifier,
26251                                             parm_attr));
26252
26253       token = cp_lexer_peek_token (parser->lexer);
26254     }
26255
26256   if (params == NULL_TREE)
26257     {
26258       cp_parser_error (parser, "objective-c++ method declaration is expected");
26259       return error_mark_node;
26260     }
26261
26262   /* We allow tail attributes for the method.  */
26263   if (token->keyword == RID_ATTRIBUTE)
26264     {
26265       *attributes = cp_parser_attributes_opt (parser);
26266       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26267           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26268         return params;
26269       cp_parser_error (parser, 
26270                        "method attributes must be specified at the end");
26271       return error_mark_node;
26272     }
26273
26274   if (params == NULL_TREE)
26275     {
26276       cp_parser_error (parser, "objective-c++ method declaration is expected");
26277       return error_mark_node;
26278     }
26279   return params;
26280 }
26281
26282 /* Parse the non-keyword Objective-C params.  */
26283
26284 static tree
26285 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
26286                                        tree* attributes)
26287 {
26288   tree params = make_node (TREE_LIST);
26289   cp_token *token = cp_lexer_peek_token (parser->lexer);
26290   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
26291
26292   while (token->type == CPP_COMMA)
26293     {
26294       cp_parameter_declarator *parmdecl;
26295       tree parm;
26296
26297       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26298       token = cp_lexer_peek_token (parser->lexer);
26299
26300       if (token->type == CPP_ELLIPSIS)
26301         {
26302           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
26303           *ellipsisp = true;
26304           token = cp_lexer_peek_token (parser->lexer);
26305           break;
26306         }
26307
26308       /* TODO: parse attributes for tail parameters.  */
26309       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26310       parm = grokdeclarator (parmdecl->declarator,
26311                              &parmdecl->decl_specifiers,
26312                              PARM, /*initialized=*/0,
26313                              /*attrlist=*/NULL);
26314
26315       chainon (params, build_tree_list (NULL_TREE, parm));
26316       token = cp_lexer_peek_token (parser->lexer);
26317     }
26318
26319   /* We allow tail attributes for the method.  */
26320   if (token->keyword == RID_ATTRIBUTE)
26321     {
26322       if (*attributes == NULL_TREE)
26323         {
26324           *attributes = cp_parser_attributes_opt (parser);
26325           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26326               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26327             return params;
26328         }
26329       else        
26330         /* We have an error, but parse the attributes, so that we can 
26331            carry on.  */
26332         *attributes = cp_parser_attributes_opt (parser);
26333
26334       cp_parser_error (parser, 
26335                        "method attributes must be specified at the end");
26336       return error_mark_node;
26337     }
26338
26339   return params;
26340 }
26341
26342 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
26343
26344 static void
26345 cp_parser_objc_interstitial_code (cp_parser* parser)
26346 {
26347   cp_token *token = cp_lexer_peek_token (parser->lexer);
26348
26349   /* If the next token is `extern' and the following token is a string
26350      literal, then we have a linkage specification.  */
26351   if (token->keyword == RID_EXTERN
26352       && cp_parser_is_pure_string_literal
26353          (cp_lexer_peek_nth_token (parser->lexer, 2)))
26354     cp_parser_linkage_specification (parser);
26355   /* Handle #pragma, if any.  */
26356   else if (token->type == CPP_PRAGMA)
26357     cp_parser_pragma (parser, pragma_objc_icode);
26358   /* Allow stray semicolons.  */
26359   else if (token->type == CPP_SEMICOLON)
26360     cp_lexer_consume_token (parser->lexer);
26361   /* Mark methods as optional or required, when building protocols.  */
26362   else if (token->keyword == RID_AT_OPTIONAL)
26363     {
26364       cp_lexer_consume_token (parser->lexer);
26365       objc_set_method_opt (true);
26366     }
26367   else if (token->keyword == RID_AT_REQUIRED)
26368     {
26369       cp_lexer_consume_token (parser->lexer);
26370       objc_set_method_opt (false);
26371     }
26372   else if (token->keyword == RID_NAMESPACE)
26373     cp_parser_namespace_definition (parser);
26374   /* Other stray characters must generate errors.  */
26375   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26376     {
26377       cp_lexer_consume_token (parser->lexer);
26378       error ("stray %qs between Objective-C++ methods",
26379              token->type == CPP_OPEN_BRACE ? "{" : "}");
26380     }
26381   /* Finally, try to parse a block-declaration, or a function-definition.  */
26382   else
26383     cp_parser_block_declaration (parser, /*statement_p=*/false);
26384 }
26385
26386 /* Parse a method signature.  */
26387
26388 static tree
26389 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26390 {
26391   tree rettype, kwdparms, optparms;
26392   bool ellipsis = false;
26393   bool is_class_method;
26394
26395   is_class_method = cp_parser_objc_method_type (parser);
26396   rettype = cp_parser_objc_typename (parser);
26397   *attributes = NULL_TREE;
26398   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26399   if (kwdparms == error_mark_node)
26400     return error_mark_node;
26401   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26402   if (optparms == error_mark_node)
26403     return error_mark_node;
26404
26405   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26406 }
26407
26408 static bool
26409 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26410 {
26411   tree tattr;  
26412   cp_lexer_save_tokens (parser->lexer);
26413   tattr = cp_parser_attributes_opt (parser);
26414   gcc_assert (tattr) ;
26415   
26416   /* If the attributes are followed by a method introducer, this is not allowed.
26417      Dump the attributes and flag the situation.  */
26418   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26419       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26420     return true;
26421
26422   /* Otherwise, the attributes introduce some interstitial code, possibly so
26423      rewind to allow that check.  */
26424   cp_lexer_rollback_tokens (parser->lexer);
26425   return false;  
26426 }
26427
26428 /* Parse an Objective-C method prototype list.  */
26429
26430 static void
26431 cp_parser_objc_method_prototype_list (cp_parser* parser)
26432 {
26433   cp_token *token = cp_lexer_peek_token (parser->lexer);
26434
26435   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26436     {
26437       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26438         {
26439           tree attributes, sig;
26440           bool is_class_method;
26441           if (token->type == CPP_PLUS)
26442             is_class_method = true;
26443           else
26444             is_class_method = false;
26445           sig = cp_parser_objc_method_signature (parser, &attributes);
26446           if (sig == error_mark_node)
26447             {
26448               cp_parser_skip_to_end_of_block_or_statement (parser);
26449               token = cp_lexer_peek_token (parser->lexer);
26450               continue;
26451             }
26452           objc_add_method_declaration (is_class_method, sig, attributes);
26453           cp_parser_consume_semicolon_at_end_of_statement (parser);
26454         }
26455       else if (token->keyword == RID_AT_PROPERTY)
26456         cp_parser_objc_at_property_declaration (parser);
26457       else if (token->keyword == RID_ATTRIBUTE 
26458                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26459         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
26460                     OPT_Wattributes, 
26461                     "prefix attributes are ignored for methods");
26462       else
26463         /* Allow for interspersed non-ObjC++ code.  */
26464         cp_parser_objc_interstitial_code (parser);
26465
26466       token = cp_lexer_peek_token (parser->lexer);
26467     }
26468
26469   if (token->type != CPP_EOF)
26470     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26471   else
26472     cp_parser_error (parser, "expected %<@end%>");
26473
26474   objc_finish_interface ();
26475 }
26476
26477 /* Parse an Objective-C method definition list.  */
26478
26479 static void
26480 cp_parser_objc_method_definition_list (cp_parser* parser)
26481 {
26482   cp_token *token = cp_lexer_peek_token (parser->lexer);
26483
26484   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26485     {
26486       tree meth;
26487
26488       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26489         {
26490           cp_token *ptk;
26491           tree sig, attribute;
26492           bool is_class_method;
26493           if (token->type == CPP_PLUS)
26494             is_class_method = true;
26495           else
26496             is_class_method = false;
26497           push_deferring_access_checks (dk_deferred);
26498           sig = cp_parser_objc_method_signature (parser, &attribute);
26499           if (sig == error_mark_node)
26500             {
26501               cp_parser_skip_to_end_of_block_or_statement (parser);
26502               token = cp_lexer_peek_token (parser->lexer);
26503               continue;
26504             }
26505           objc_start_method_definition (is_class_method, sig, attribute,
26506                                         NULL_TREE);
26507
26508           /* For historical reasons, we accept an optional semicolon.  */
26509           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26510             cp_lexer_consume_token (parser->lexer);
26511
26512           ptk = cp_lexer_peek_token (parser->lexer);
26513           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
26514                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26515             {
26516               perform_deferred_access_checks (tf_warning_or_error);
26517               stop_deferring_access_checks ();
26518               meth = cp_parser_function_definition_after_declarator (parser,
26519                                                                      false);
26520               pop_deferring_access_checks ();
26521               objc_finish_method_definition (meth);
26522             }
26523         }
26524       /* The following case will be removed once @synthesize is
26525          completely implemented.  */
26526       else if (token->keyword == RID_AT_PROPERTY)
26527         cp_parser_objc_at_property_declaration (parser);
26528       else if (token->keyword == RID_AT_SYNTHESIZE)
26529         cp_parser_objc_at_synthesize_declaration (parser);
26530       else if (token->keyword == RID_AT_DYNAMIC)
26531         cp_parser_objc_at_dynamic_declaration (parser);
26532       else if (token->keyword == RID_ATTRIBUTE 
26533                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26534         warning_at (token->location, OPT_Wattributes,
26535                     "prefix attributes are ignored for methods");
26536       else
26537         /* Allow for interspersed non-ObjC++ code.  */
26538         cp_parser_objc_interstitial_code (parser);
26539
26540       token = cp_lexer_peek_token (parser->lexer);
26541     }
26542
26543   if (token->type != CPP_EOF)
26544     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26545   else
26546     cp_parser_error (parser, "expected %<@end%>");
26547
26548   objc_finish_implementation ();
26549 }
26550
26551 /* Parse Objective-C ivars.  */
26552
26553 static void
26554 cp_parser_objc_class_ivars (cp_parser* parser)
26555 {
26556   cp_token *token = cp_lexer_peek_token (parser->lexer);
26557
26558   if (token->type != CPP_OPEN_BRACE)
26559     return;     /* No ivars specified.  */
26560
26561   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
26562   token = cp_lexer_peek_token (parser->lexer);
26563
26564   while (token->type != CPP_CLOSE_BRACE 
26565         && token->keyword != RID_AT_END && token->type != CPP_EOF)
26566     {
26567       cp_decl_specifier_seq declspecs;
26568       int decl_class_or_enum_p;
26569       tree prefix_attributes;
26570
26571       cp_parser_objc_visibility_spec (parser);
26572
26573       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26574         break;
26575
26576       cp_parser_decl_specifier_seq (parser,
26577                                     CP_PARSER_FLAGS_OPTIONAL,
26578                                     &declspecs,
26579                                     &decl_class_or_enum_p);
26580
26581       /* auto, register, static, extern, mutable.  */
26582       if (declspecs.storage_class != sc_none)
26583         {
26584           cp_parser_error (parser, "invalid type for instance variable");         
26585           declspecs.storage_class = sc_none;
26586         }
26587
26588       /* thread_local.  */
26589       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26590         {
26591           cp_parser_error (parser, "invalid type for instance variable");
26592           declspecs.locations[ds_thread] = 0;
26593         }
26594       
26595       /* typedef.  */
26596       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26597         {
26598           cp_parser_error (parser, "invalid type for instance variable");
26599           declspecs.locations[ds_typedef] = 0;
26600         }
26601
26602       prefix_attributes = declspecs.attributes;
26603       declspecs.attributes = NULL_TREE;
26604
26605       /* Keep going until we hit the `;' at the end of the
26606          declaration.  */
26607       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26608         {
26609           tree width = NULL_TREE, attributes, first_attribute, decl;
26610           cp_declarator *declarator = NULL;
26611           int ctor_dtor_or_conv_p;
26612
26613           /* Check for a (possibly unnamed) bitfield declaration.  */
26614           token = cp_lexer_peek_token (parser->lexer);
26615           if (token->type == CPP_COLON)
26616             goto eat_colon;
26617
26618           if (token->type == CPP_NAME
26619               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26620                   == CPP_COLON))
26621             {
26622               /* Get the name of the bitfield.  */
26623               declarator = make_id_declarator (NULL_TREE,
26624                                                cp_parser_identifier (parser),
26625                                                sfk_none);
26626
26627              eat_colon:
26628               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26629               /* Get the width of the bitfield.  */
26630               width
26631                 = cp_parser_constant_expression (parser);
26632             }
26633           else
26634             {
26635               /* Parse the declarator.  */
26636               declarator
26637                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26638                                         &ctor_dtor_or_conv_p,
26639                                         /*parenthesized_p=*/NULL,
26640                                         /*member_p=*/false,
26641                                         /*friend_p=*/false);
26642             }
26643
26644           /* Look for attributes that apply to the ivar.  */
26645           attributes = cp_parser_attributes_opt (parser);
26646           /* Remember which attributes are prefix attributes and
26647              which are not.  */
26648           first_attribute = attributes;
26649           /* Combine the attributes.  */
26650           attributes = chainon (prefix_attributes, attributes);
26651
26652           if (width)
26653               /* Create the bitfield declaration.  */
26654               decl = grokbitfield (declarator, &declspecs,
26655                                    width,
26656                                    attributes);
26657           else
26658             decl = grokfield (declarator, &declspecs,
26659                               NULL_TREE, /*init_const_expr_p=*/false,
26660                               NULL_TREE, attributes);
26661
26662           /* Add the instance variable.  */
26663           if (decl != error_mark_node && decl != NULL_TREE)
26664             objc_add_instance_variable (decl);
26665
26666           /* Reset PREFIX_ATTRIBUTES.  */
26667           while (attributes && TREE_CHAIN (attributes) != first_attribute)
26668             attributes = TREE_CHAIN (attributes);
26669           if (attributes)
26670             TREE_CHAIN (attributes) = NULL_TREE;
26671
26672           token = cp_lexer_peek_token (parser->lexer);
26673
26674           if (token->type == CPP_COMMA)
26675             {
26676               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26677               continue;
26678             }
26679           break;
26680         }
26681
26682       cp_parser_consume_semicolon_at_end_of_statement (parser);
26683       token = cp_lexer_peek_token (parser->lexer);
26684     }
26685
26686   if (token->keyword == RID_AT_END)
26687     cp_parser_error (parser, "expected %<}%>");
26688
26689   /* Do not consume the RID_AT_END, so it will be read again as terminating
26690      the @interface of @implementation.  */ 
26691   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26692     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
26693     
26694   /* For historical reasons, we accept an optional semicolon.  */
26695   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26696     cp_lexer_consume_token (parser->lexer);
26697 }
26698
26699 /* Parse an Objective-C protocol declaration.  */
26700
26701 static void
26702 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26703 {
26704   tree proto, protorefs;
26705   cp_token *tok;
26706
26707   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
26708   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26709     {
26710       tok = cp_lexer_peek_token (parser->lexer);
26711       error_at (tok->location, "identifier expected after %<@protocol%>");
26712       cp_parser_consume_semicolon_at_end_of_statement (parser);
26713       return;
26714     }
26715
26716   /* See if we have a forward declaration or a definition.  */
26717   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26718
26719   /* Try a forward declaration first.  */
26720   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26721     {
26722       while (true)
26723         {
26724           tree id;
26725           
26726           id = cp_parser_identifier (parser);
26727           if (id == error_mark_node)
26728             break;
26729           
26730           objc_declare_protocol (id, attributes);
26731           
26732           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26733             cp_lexer_consume_token (parser->lexer);
26734           else
26735             break;
26736         }
26737       cp_parser_consume_semicolon_at_end_of_statement (parser);
26738     }
26739
26740   /* Ok, we got a full-fledged definition (or at least should).  */
26741   else
26742     {
26743       proto = cp_parser_identifier (parser);
26744       protorefs = cp_parser_objc_protocol_refs_opt (parser);
26745       objc_start_protocol (proto, protorefs, attributes);
26746       cp_parser_objc_method_prototype_list (parser);
26747     }
26748 }
26749
26750 /* Parse an Objective-C superclass or category.  */
26751
26752 static void
26753 cp_parser_objc_superclass_or_category (cp_parser *parser, 
26754                                        bool iface_p,
26755                                        tree *super,
26756                                        tree *categ, bool *is_class_extension)
26757 {
26758   cp_token *next = cp_lexer_peek_token (parser->lexer);
26759
26760   *super = *categ = NULL_TREE;
26761   *is_class_extension = false;
26762   if (next->type == CPP_COLON)
26763     {
26764       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26765       *super = cp_parser_identifier (parser);
26766     }
26767   else if (next->type == CPP_OPEN_PAREN)
26768     {
26769       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26770
26771       /* If there is no category name, and this is an @interface, we
26772          have a class extension.  */
26773       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26774         {
26775           *categ = NULL_TREE;
26776           *is_class_extension = true;
26777         }
26778       else
26779         *categ = cp_parser_identifier (parser);
26780
26781       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26782     }
26783 }
26784
26785 /* Parse an Objective-C class interface.  */
26786
26787 static void
26788 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26789 {
26790   tree name, super, categ, protos;
26791   bool is_class_extension;
26792
26793   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
26794   name = cp_parser_identifier (parser);
26795   if (name == error_mark_node)
26796     {
26797       /* It's hard to recover because even if valid @interface stuff
26798          is to follow, we can't compile it (or validate it) if we
26799          don't even know which class it refers to.  Let's assume this
26800          was a stray '@interface' token in the stream and skip it.
26801       */
26802       return;
26803     }
26804   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26805                                          &is_class_extension);
26806   protos = cp_parser_objc_protocol_refs_opt (parser);
26807
26808   /* We have either a class or a category on our hands.  */
26809   if (categ || is_class_extension)
26810     objc_start_category_interface (name, categ, protos, attributes);
26811   else
26812     {
26813       objc_start_class_interface (name, super, protos, attributes);
26814       /* Handle instance variable declarations, if any.  */
26815       cp_parser_objc_class_ivars (parser);
26816       objc_continue_interface ();
26817     }
26818
26819   cp_parser_objc_method_prototype_list (parser);
26820 }
26821
26822 /* Parse an Objective-C class implementation.  */
26823
26824 static void
26825 cp_parser_objc_class_implementation (cp_parser* parser)
26826 {
26827   tree name, super, categ;
26828   bool is_class_extension;
26829
26830   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
26831   name = cp_parser_identifier (parser);
26832   if (name == error_mark_node)
26833     {
26834       /* It's hard to recover because even if valid @implementation
26835          stuff is to follow, we can't compile it (or validate it) if
26836          we don't even know which class it refers to.  Let's assume
26837          this was a stray '@implementation' token in the stream and
26838          skip it.
26839       */
26840       return;
26841     }
26842   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26843                                          &is_class_extension);
26844
26845   /* We have either a class or a category on our hands.  */
26846   if (categ)
26847     objc_start_category_implementation (name, categ);
26848   else
26849     {
26850       objc_start_class_implementation (name, super);
26851       /* Handle instance variable declarations, if any.  */
26852       cp_parser_objc_class_ivars (parser);
26853       objc_continue_implementation ();
26854     }
26855
26856   cp_parser_objc_method_definition_list (parser);
26857 }
26858
26859 /* Consume the @end token and finish off the implementation.  */
26860
26861 static void
26862 cp_parser_objc_end_implementation (cp_parser* parser)
26863 {
26864   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26865   objc_finish_implementation ();
26866 }
26867
26868 /* Parse an Objective-C declaration.  */
26869
26870 static void
26871 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26872 {
26873   /* Try to figure out what kind of declaration is present.  */
26874   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26875
26876   if (attributes)
26877     switch (kwd->keyword)
26878       {
26879         case RID_AT_ALIAS:
26880         case RID_AT_CLASS:
26881         case RID_AT_END:
26882           error_at (kwd->location, "attributes may not be specified before"
26883                     " the %<@%D%> Objective-C++ keyword",
26884                     kwd->u.value);
26885           attributes = NULL;
26886           break;
26887         case RID_AT_IMPLEMENTATION:
26888           warning_at (kwd->location, OPT_Wattributes,
26889                       "prefix attributes are ignored before %<@%D%>",
26890                       kwd->u.value);
26891           attributes = NULL;
26892         default:
26893           break;
26894       }
26895
26896   switch (kwd->keyword)
26897     {
26898     case RID_AT_ALIAS:
26899       cp_parser_objc_alias_declaration (parser);
26900       break;
26901     case RID_AT_CLASS:
26902       cp_parser_objc_class_declaration (parser);
26903       break;
26904     case RID_AT_PROTOCOL:
26905       cp_parser_objc_protocol_declaration (parser, attributes);
26906       break;
26907     case RID_AT_INTERFACE:
26908       cp_parser_objc_class_interface (parser, attributes);
26909       break;
26910     case RID_AT_IMPLEMENTATION:
26911       cp_parser_objc_class_implementation (parser);
26912       break;
26913     case RID_AT_END:
26914       cp_parser_objc_end_implementation (parser);
26915       break;
26916     default:
26917       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26918                 kwd->u.value);
26919       cp_parser_skip_to_end_of_block_or_statement (parser);
26920     }
26921 }
26922
26923 /* Parse an Objective-C try-catch-finally statement.
26924
26925    objc-try-catch-finally-stmt:
26926      @try compound-statement objc-catch-clause-seq [opt]
26927        objc-finally-clause [opt]
26928
26929    objc-catch-clause-seq:
26930      objc-catch-clause objc-catch-clause-seq [opt]
26931
26932    objc-catch-clause:
26933      @catch ( objc-exception-declaration ) compound-statement
26934
26935    objc-finally-clause:
26936      @finally compound-statement
26937
26938    objc-exception-declaration:
26939      parameter-declaration
26940      '...'
26941
26942    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26943
26944    Returns NULL_TREE.
26945
26946    PS: This function is identical to c_parser_objc_try_catch_finally_statement
26947    for C.  Keep them in sync.  */   
26948
26949 static tree
26950 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26951 {
26952   location_t location;
26953   tree stmt;
26954
26955   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26956   location = cp_lexer_peek_token (parser->lexer)->location;
26957   objc_maybe_warn_exceptions (location);
26958   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26959      node, lest it get absorbed into the surrounding block.  */
26960   stmt = push_stmt_list ();
26961   cp_parser_compound_statement (parser, NULL, false, false);
26962   objc_begin_try_stmt (location, pop_stmt_list (stmt));
26963
26964   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26965     {
26966       cp_parameter_declarator *parm;
26967       tree parameter_declaration = error_mark_node;
26968       bool seen_open_paren = false;
26969
26970       cp_lexer_consume_token (parser->lexer);
26971       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26972         seen_open_paren = true;
26973       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26974         {
26975           /* We have "@catch (...)" (where the '...' are literally
26976              what is in the code).  Skip the '...'.
26977              parameter_declaration is set to NULL_TREE, and
26978              objc_being_catch_clauses() knows that that means
26979              '...'.  */
26980           cp_lexer_consume_token (parser->lexer);
26981           parameter_declaration = NULL_TREE;
26982         }
26983       else
26984         {
26985           /* We have "@catch (NSException *exception)" or something
26986              like that.  Parse the parameter declaration.  */
26987           parm = cp_parser_parameter_declaration (parser, false, NULL);
26988           if (parm == NULL)
26989             parameter_declaration = error_mark_node;
26990           else
26991             parameter_declaration = grokdeclarator (parm->declarator,
26992                                                     &parm->decl_specifiers,
26993                                                     PARM, /*initialized=*/0,
26994                                                     /*attrlist=*/NULL);
26995         }
26996       if (seen_open_paren)
26997         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26998       else
26999         {
27000           /* If there was no open parenthesis, we are recovering from
27001              an error, and we are trying to figure out what mistake
27002              the user has made.  */
27003
27004           /* If there is an immediate closing parenthesis, the user
27005              probably forgot the opening one (ie, they typed "@catch
27006              NSException *e)".  Parse the closing parenthesis and keep
27007              going.  */
27008           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27009             cp_lexer_consume_token (parser->lexer);
27010           
27011           /* If these is no immediate closing parenthesis, the user
27012              probably doesn't know that parenthesis are required at
27013              all (ie, they typed "@catch NSException *e").  So, just
27014              forget about the closing parenthesis and keep going.  */
27015         }
27016       objc_begin_catch_clause (parameter_declaration);
27017       cp_parser_compound_statement (parser, NULL, false, false);
27018       objc_finish_catch_clause ();
27019     }
27020   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27021     {
27022       cp_lexer_consume_token (parser->lexer);
27023       location = cp_lexer_peek_token (parser->lexer)->location;
27024       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27025          node, lest it get absorbed into the surrounding block.  */
27026       stmt = push_stmt_list ();
27027       cp_parser_compound_statement (parser, NULL, false, false);
27028       objc_build_finally_clause (location, pop_stmt_list (stmt));
27029     }
27030
27031   return objc_finish_try_stmt ();
27032 }
27033
27034 /* Parse an Objective-C synchronized statement.
27035
27036    objc-synchronized-stmt:
27037      @synchronized ( expression ) compound-statement
27038
27039    Returns NULL_TREE.  */
27040
27041 static tree
27042 cp_parser_objc_synchronized_statement (cp_parser *parser)
27043 {
27044   location_t location;
27045   tree lock, stmt;
27046
27047   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27048
27049   location = cp_lexer_peek_token (parser->lexer)->location;
27050   objc_maybe_warn_exceptions (location);
27051   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27052   lock = cp_parser_expression (parser);
27053   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27054
27055   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27056      node, lest it get absorbed into the surrounding block.  */
27057   stmt = push_stmt_list ();
27058   cp_parser_compound_statement (parser, NULL, false, false);
27059
27060   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27061 }
27062
27063 /* Parse an Objective-C throw statement.
27064
27065    objc-throw-stmt:
27066      @throw assignment-expression [opt] ;
27067
27068    Returns a constructed '@throw' statement.  */
27069
27070 static tree
27071 cp_parser_objc_throw_statement (cp_parser *parser)
27072 {
27073   tree expr = NULL_TREE;
27074   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27075
27076   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27077
27078   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27079     expr = cp_parser_expression (parser);
27080
27081   cp_parser_consume_semicolon_at_end_of_statement (parser);
27082
27083   return objc_build_throw_stmt (loc, expr);
27084 }
27085
27086 /* Parse an Objective-C statement.  */
27087
27088 static tree
27089 cp_parser_objc_statement (cp_parser * parser)
27090 {
27091   /* Try to figure out what kind of declaration is present.  */
27092   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27093
27094   switch (kwd->keyword)
27095     {
27096     case RID_AT_TRY:
27097       return cp_parser_objc_try_catch_finally_statement (parser);
27098     case RID_AT_SYNCHRONIZED:
27099       return cp_parser_objc_synchronized_statement (parser);
27100     case RID_AT_THROW:
27101       return cp_parser_objc_throw_statement (parser);
27102     default:
27103       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27104                kwd->u.value);
27105       cp_parser_skip_to_end_of_block_or_statement (parser);
27106     }
27107
27108   return error_mark_node;
27109 }
27110
27111 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
27112    look ahead to see if an objc keyword follows the attributes.  This
27113    is to detect the use of prefix attributes on ObjC @interface and 
27114    @protocol.  */
27115
27116 static bool
27117 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27118 {
27119   cp_lexer_save_tokens (parser->lexer);
27120   *attrib = cp_parser_attributes_opt (parser);
27121   gcc_assert (*attrib);
27122   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27123     {
27124       cp_lexer_commit_tokens (parser->lexer);
27125       return true;
27126     }
27127   cp_lexer_rollback_tokens (parser->lexer);
27128   return false;  
27129 }
27130
27131 /* This routine is a minimal replacement for
27132    c_parser_struct_declaration () used when parsing the list of
27133    types/names or ObjC++ properties.  For example, when parsing the
27134    code
27135
27136    @property (readonly) int a, b, c;
27137
27138    this function is responsible for parsing "int a, int b, int c" and
27139    returning the declarations as CHAIN of DECLs.
27140
27141    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
27142    similar parsing.  */
27143 static tree
27144 cp_parser_objc_struct_declaration (cp_parser *parser)
27145 {
27146   tree decls = NULL_TREE;
27147   cp_decl_specifier_seq declspecs;
27148   int decl_class_or_enum_p;
27149   tree prefix_attributes;
27150
27151   cp_parser_decl_specifier_seq (parser,
27152                                 CP_PARSER_FLAGS_NONE,
27153                                 &declspecs,
27154                                 &decl_class_or_enum_p);
27155
27156   if (declspecs.type == error_mark_node)
27157     return error_mark_node;
27158
27159   /* auto, register, static, extern, mutable.  */
27160   if (declspecs.storage_class != sc_none)
27161     {
27162       cp_parser_error (parser, "invalid type for property");
27163       declspecs.storage_class = sc_none;
27164     }
27165   
27166   /* thread_local.  */
27167   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27168     {
27169       cp_parser_error (parser, "invalid type for property");
27170       declspecs.locations[ds_thread] = 0;
27171     }
27172   
27173   /* typedef.  */
27174   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27175     {
27176       cp_parser_error (parser, "invalid type for property");
27177       declspecs.locations[ds_typedef] = 0;
27178     }
27179
27180   prefix_attributes = declspecs.attributes;
27181   declspecs.attributes = NULL_TREE;
27182
27183   /* Keep going until we hit the `;' at the end of the declaration. */
27184   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27185     {
27186       tree attributes, first_attribute, decl;
27187       cp_declarator *declarator;
27188       cp_token *token;
27189
27190       /* Parse the declarator.  */
27191       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27192                                          NULL, NULL, false, false);
27193
27194       /* Look for attributes that apply to the ivar.  */
27195       attributes = cp_parser_attributes_opt (parser);
27196       /* Remember which attributes are prefix attributes and
27197          which are not.  */
27198       first_attribute = attributes;
27199       /* Combine the attributes.  */
27200       attributes = chainon (prefix_attributes, attributes);
27201       
27202       decl = grokfield (declarator, &declspecs,
27203                         NULL_TREE, /*init_const_expr_p=*/false,
27204                         NULL_TREE, attributes);
27205
27206       if (decl == error_mark_node || decl == NULL_TREE)
27207         return error_mark_node;
27208       
27209       /* Reset PREFIX_ATTRIBUTES.  */
27210       while (attributes && TREE_CHAIN (attributes) != first_attribute)
27211         attributes = TREE_CHAIN (attributes);
27212       if (attributes)
27213         TREE_CHAIN (attributes) = NULL_TREE;
27214
27215       DECL_CHAIN (decl) = decls;
27216       decls = decl;
27217
27218       token = cp_lexer_peek_token (parser->lexer);
27219       if (token->type == CPP_COMMA)
27220         {
27221           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
27222           continue;
27223         }
27224       else
27225         break;
27226     }
27227   return decls;
27228 }
27229
27230 /* Parse an Objective-C @property declaration.  The syntax is:
27231
27232    objc-property-declaration:
27233      '@property' objc-property-attributes[opt] struct-declaration ;
27234
27235    objc-property-attributes:
27236     '(' objc-property-attribute-list ')'
27237
27238    objc-property-attribute-list:
27239      objc-property-attribute
27240      objc-property-attribute-list, objc-property-attribute
27241
27242    objc-property-attribute
27243      'getter' = identifier
27244      'setter' = identifier
27245      'readonly'
27246      'readwrite'
27247      'assign'
27248      'retain'
27249      'copy'
27250      'nonatomic'
27251
27252   For example:
27253     @property NSString *name;
27254     @property (readonly) id object;
27255     @property (retain, nonatomic, getter=getTheName) id name;
27256     @property int a, b, c;
27257
27258    PS: This function is identical to
27259    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
27260 static void 
27261 cp_parser_objc_at_property_declaration (cp_parser *parser)
27262 {
27263   /* The following variables hold the attributes of the properties as
27264      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
27265      seen.  When we see an attribute, we set them to 'true' (if they
27266      are boolean properties) or to the identifier (if they have an
27267      argument, ie, for getter and setter).  Note that here we only
27268      parse the list of attributes, check the syntax and accumulate the
27269      attributes that we find.  objc_add_property_declaration() will
27270      then process the information.  */
27271   bool property_assign = false;
27272   bool property_copy = false;
27273   tree property_getter_ident = NULL_TREE;
27274   bool property_nonatomic = false;
27275   bool property_readonly = false;
27276   bool property_readwrite = false;
27277   bool property_retain = false;
27278   tree property_setter_ident = NULL_TREE;
27279
27280   /* 'properties' is the list of properties that we read.  Usually a
27281      single one, but maybe more (eg, in "@property int a, b, c;" there
27282      are three).  */
27283   tree properties;
27284   location_t loc;
27285
27286   loc = cp_lexer_peek_token (parser->lexer)->location;
27287
27288   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
27289
27290   /* Parse the optional attribute list...  */
27291   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27292     {
27293       /* Eat the '('.  */
27294       cp_lexer_consume_token (parser->lexer);
27295
27296       while (true)
27297         {
27298           bool syntax_error = false;
27299           cp_token *token = cp_lexer_peek_token (parser->lexer);
27300           enum rid keyword;
27301
27302           if (token->type != CPP_NAME)
27303             {
27304               cp_parser_error (parser, "expected identifier");
27305               break;
27306             }
27307           keyword = C_RID_CODE (token->u.value);
27308           cp_lexer_consume_token (parser->lexer);
27309           switch (keyword)
27310             {
27311             case RID_ASSIGN:    property_assign = true;    break;
27312             case RID_COPY:      property_copy = true;      break;
27313             case RID_NONATOMIC: property_nonatomic = true; break;
27314             case RID_READONLY:  property_readonly = true;  break;
27315             case RID_READWRITE: property_readwrite = true; break;
27316             case RID_RETAIN:    property_retain = true;    break;
27317
27318             case RID_GETTER:
27319             case RID_SETTER:
27320               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27321                 {
27322                   if (keyword == RID_GETTER)
27323                     cp_parser_error (parser,
27324                                      "missing %<=%> (after %<getter%> attribute)");
27325                   else
27326                     cp_parser_error (parser,
27327                                      "missing %<=%> (after %<setter%> attribute)");
27328                   syntax_error = true;
27329                   break;
27330                 }
27331               cp_lexer_consume_token (parser->lexer); /* eat the = */
27332               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27333                 {
27334                   cp_parser_error (parser, "expected identifier");
27335                   syntax_error = true;
27336                   break;
27337                 }
27338               if (keyword == RID_SETTER)
27339                 {
27340                   if (property_setter_ident != NULL_TREE)
27341                     {
27342                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27343                       cp_lexer_consume_token (parser->lexer);
27344                     }
27345                   else
27346                     property_setter_ident = cp_parser_objc_selector (parser);
27347                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27348                     cp_parser_error (parser, "setter name must terminate with %<:%>");
27349                   else
27350                     cp_lexer_consume_token (parser->lexer);
27351                 }
27352               else
27353                 {
27354                   if (property_getter_ident != NULL_TREE)
27355                     {
27356                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27357                       cp_lexer_consume_token (parser->lexer);
27358                     }
27359                   else
27360                     property_getter_ident = cp_parser_objc_selector (parser);
27361                 }
27362               break;
27363             default:
27364               cp_parser_error (parser, "unknown property attribute");
27365               syntax_error = true;
27366               break;
27367             }
27368
27369           if (syntax_error)
27370             break;
27371
27372           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27373             cp_lexer_consume_token (parser->lexer);
27374           else
27375             break;
27376         }
27377
27378       /* FIXME: "@property (setter, assign);" will generate a spurious
27379          "error: expected â€˜)’ before â€˜,’ token".  This is because
27380          cp_parser_require, unlike the C counterpart, will produce an
27381          error even if we are in error recovery.  */
27382       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27383         {
27384           cp_parser_skip_to_closing_parenthesis (parser,
27385                                                  /*recovering=*/true,
27386                                                  /*or_comma=*/false,
27387                                                  /*consume_paren=*/true);
27388         }
27389     }
27390
27391   /* ... and the property declaration(s).  */
27392   properties = cp_parser_objc_struct_declaration (parser);
27393
27394   if (properties == error_mark_node)
27395     {
27396       cp_parser_skip_to_end_of_statement (parser);
27397       /* If the next token is now a `;', consume it.  */
27398       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27399         cp_lexer_consume_token (parser->lexer);
27400       return;
27401     }
27402
27403   if (properties == NULL_TREE)
27404     cp_parser_error (parser, "expected identifier");
27405   else
27406     {
27407       /* Comma-separated properties are chained together in
27408          reverse order; add them one by one.  */
27409       properties = nreverse (properties);
27410       
27411       for (; properties; properties = TREE_CHAIN (properties))
27412         objc_add_property_declaration (loc, copy_node (properties),
27413                                        property_readonly, property_readwrite,
27414                                        property_assign, property_retain,
27415                                        property_copy, property_nonatomic,
27416                                        property_getter_ident, property_setter_ident);
27417     }
27418   
27419   cp_parser_consume_semicolon_at_end_of_statement (parser);
27420 }
27421
27422 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
27423
27424    objc-synthesize-declaration:
27425      @synthesize objc-synthesize-identifier-list ;
27426
27427    objc-synthesize-identifier-list:
27428      objc-synthesize-identifier
27429      objc-synthesize-identifier-list, objc-synthesize-identifier
27430
27431    objc-synthesize-identifier
27432      identifier
27433      identifier = identifier
27434
27435   For example:
27436     @synthesize MyProperty;
27437     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27438
27439   PS: This function is identical to c_parser_objc_at_synthesize_declaration
27440   for C.  Keep them in sync.
27441 */
27442 static void 
27443 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27444 {
27445   tree list = NULL_TREE;
27446   location_t loc;
27447   loc = cp_lexer_peek_token (parser->lexer)->location;
27448
27449   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
27450   while (true)
27451     {
27452       tree property, ivar;
27453       property = cp_parser_identifier (parser);
27454       if (property == error_mark_node)
27455         {
27456           cp_parser_consume_semicolon_at_end_of_statement (parser);
27457           return;
27458         }
27459       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27460         {
27461           cp_lexer_consume_token (parser->lexer);
27462           ivar = cp_parser_identifier (parser);
27463           if (ivar == error_mark_node)
27464             {
27465               cp_parser_consume_semicolon_at_end_of_statement (parser);
27466               return;
27467             }
27468         }
27469       else
27470         ivar = NULL_TREE;
27471       list = chainon (list, build_tree_list (ivar, property));
27472       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27473         cp_lexer_consume_token (parser->lexer);
27474       else
27475         break;
27476     }
27477   cp_parser_consume_semicolon_at_end_of_statement (parser);
27478   objc_add_synthesize_declaration (loc, list);
27479 }
27480
27481 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
27482
27483    objc-dynamic-declaration:
27484      @dynamic identifier-list ;
27485
27486    For example:
27487      @dynamic MyProperty;
27488      @dynamic MyProperty, AnotherProperty;
27489
27490   PS: This function is identical to c_parser_objc_at_dynamic_declaration
27491   for C.  Keep them in sync.
27492 */
27493 static void 
27494 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27495 {
27496   tree list = NULL_TREE;
27497   location_t loc;
27498   loc = cp_lexer_peek_token (parser->lexer)->location;
27499
27500   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
27501   while (true)
27502     {
27503       tree property;
27504       property = cp_parser_identifier (parser);
27505       if (property == error_mark_node)
27506         {
27507           cp_parser_consume_semicolon_at_end_of_statement (parser);
27508           return;
27509         }
27510       list = chainon (list, build_tree_list (NULL, property));
27511       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27512         cp_lexer_consume_token (parser->lexer);
27513       else
27514         break;
27515     }
27516   cp_parser_consume_semicolon_at_end_of_statement (parser);
27517   objc_add_dynamic_declaration (loc, list);
27518 }
27519
27520 \f
27521 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
27522
27523 /* Returns name of the next clause.
27524    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27525    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
27526    returned and the token is consumed.  */
27527
27528 static pragma_omp_clause
27529 cp_parser_omp_clause_name (cp_parser *parser)
27530 {
27531   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27532
27533   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27534     result = PRAGMA_OMP_CLAUSE_IF;
27535   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27536     result = PRAGMA_OMP_CLAUSE_DEFAULT;
27537   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27538     result = PRAGMA_OACC_CLAUSE_DELETE;
27539   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27540     result = PRAGMA_OMP_CLAUSE_PRIVATE;
27541   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27542     result = PRAGMA_OMP_CLAUSE_FOR;
27543   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27544     {
27545       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27546       const char *p = IDENTIFIER_POINTER (id);
27547
27548       switch (p[0])
27549         {
27550         case 'a':
27551           if (!strcmp ("aligned", p))
27552             result = PRAGMA_OMP_CLAUSE_ALIGNED;
27553           else if (!strcmp ("async", p))
27554             result = PRAGMA_OACC_CLAUSE_ASYNC;
27555           break;
27556         case 'c':
27557           if (!strcmp ("collapse", p))
27558             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27559           else if (!strcmp ("copy", p))
27560             result = PRAGMA_OACC_CLAUSE_COPY;
27561           else if (!strcmp ("copyin", p))
27562             result = PRAGMA_OMP_CLAUSE_COPYIN;
27563           else if (!strcmp ("copyout", p))
27564             result = PRAGMA_OACC_CLAUSE_COPYOUT;
27565           else if (!strcmp ("copyprivate", p))
27566             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27567           else if (!strcmp ("create", p))
27568             result = PRAGMA_OACC_CLAUSE_CREATE;
27569           break;
27570         case 'd':
27571           if (!strcmp ("depend", p))
27572             result = PRAGMA_OMP_CLAUSE_DEPEND;
27573           else if (!strcmp ("device", p))
27574             result = PRAGMA_OMP_CLAUSE_DEVICE;
27575           else if (!strcmp ("deviceptr", p))
27576             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27577           else if (!strcmp ("dist_schedule", p))
27578             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27579           break;
27580         case 'f':
27581           if (!strcmp ("final", p))
27582             result = PRAGMA_OMP_CLAUSE_FINAL;
27583           else if (!strcmp ("firstprivate", p))
27584             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27585           else if (!strcmp ("from", p))
27586             result = PRAGMA_OMP_CLAUSE_FROM;
27587           break;
27588         case 'h':
27589           if (!strcmp ("host", p))
27590             result = PRAGMA_OACC_CLAUSE_HOST;
27591           break;
27592         case 'i':
27593           if (!strcmp ("inbranch", p))
27594             result = PRAGMA_OMP_CLAUSE_INBRANCH;
27595           break;
27596         case 'l':
27597           if (!strcmp ("lastprivate", p))
27598             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27599           else if (!strcmp ("linear", p))
27600             result = PRAGMA_OMP_CLAUSE_LINEAR;
27601           break;
27602         case 'm':
27603           if (!strcmp ("map", p))
27604             result = PRAGMA_OMP_CLAUSE_MAP;
27605           else if (!strcmp ("mergeable", p))
27606             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27607           else if (flag_cilkplus && !strcmp ("mask", p))
27608             result = PRAGMA_CILK_CLAUSE_MASK;
27609           break;
27610         case 'n':
27611           if (!strcmp ("notinbranch", p))
27612             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27613           else if (!strcmp ("nowait", p))
27614             result = PRAGMA_OMP_CLAUSE_NOWAIT;
27615           else if (flag_cilkplus && !strcmp ("nomask", p))
27616             result = PRAGMA_CILK_CLAUSE_NOMASK;
27617           else if (!strcmp ("num_gangs", p))
27618             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27619           else if (!strcmp ("num_teams", p))
27620             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27621           else if (!strcmp ("num_threads", p))
27622             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27623           else if (!strcmp ("num_workers", p))
27624             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27625           break;
27626         case 'o':
27627           if (!strcmp ("ordered", p))
27628             result = PRAGMA_OMP_CLAUSE_ORDERED;
27629           break;
27630         case 'p':
27631           if (!strcmp ("parallel", p))
27632             result = PRAGMA_OMP_CLAUSE_PARALLEL;
27633           else if (!strcmp ("present", p))
27634             result = PRAGMA_OACC_CLAUSE_PRESENT;
27635           else if (!strcmp ("present_or_copy", p)
27636                    || !strcmp ("pcopy", p))
27637             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27638           else if (!strcmp ("present_or_copyin", p)
27639                    || !strcmp ("pcopyin", p))
27640             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27641           else if (!strcmp ("present_or_copyout", p)
27642                    || !strcmp ("pcopyout", p))
27643             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27644           else if (!strcmp ("present_or_create", p)
27645                    || !strcmp ("pcreate", p))
27646             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27647           else if (!strcmp ("proc_bind", p))
27648             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27649           break;
27650         case 'r':
27651           if (!strcmp ("reduction", p))
27652             result = PRAGMA_OMP_CLAUSE_REDUCTION;
27653           break;
27654         case 's':
27655           if (!strcmp ("safelen", p))
27656             result = PRAGMA_OMP_CLAUSE_SAFELEN;
27657           else if (!strcmp ("schedule", p))
27658             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27659           else if (!strcmp ("sections", p))
27660             result = PRAGMA_OMP_CLAUSE_SECTIONS;
27661           else if (!strcmp ("self", p))
27662             result = PRAGMA_OACC_CLAUSE_SELF;
27663           else if (!strcmp ("shared", p))
27664             result = PRAGMA_OMP_CLAUSE_SHARED;
27665           else if (!strcmp ("simdlen", p))
27666             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27667           break;
27668         case 't':
27669           if (!strcmp ("taskgroup", p))
27670             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27671           else if (!strcmp ("thread_limit", p))
27672             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27673           else if (!strcmp ("to", p))
27674             result = PRAGMA_OMP_CLAUSE_TO;
27675           break;
27676         case 'u':
27677           if (!strcmp ("uniform", p))
27678             result = PRAGMA_OMP_CLAUSE_UNIFORM;
27679           else if (!strcmp ("untied", p))
27680             result = PRAGMA_OMP_CLAUSE_UNTIED;
27681           break;
27682         case 'v':
27683           if (!strcmp ("vector_length", p))
27684             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27685           else if (flag_cilkplus && !strcmp ("vectorlength", p))
27686             result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27687           break;
27688         case 'w':
27689           if (!strcmp ("wait", p))
27690             result = PRAGMA_OACC_CLAUSE_WAIT;
27691           break;
27692         }
27693     }
27694
27695   if (result != PRAGMA_OMP_CLAUSE_NONE)
27696     cp_lexer_consume_token (parser->lexer);
27697
27698   return result;
27699 }
27700
27701 /* Validate that a clause of the given type does not already exist.  */
27702
27703 static void
27704 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27705                            const char *name, location_t location)
27706 {
27707   tree c;
27708
27709   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27710     if (OMP_CLAUSE_CODE (c) == code)
27711       {
27712         error_at (location, "too many %qs clauses", name);
27713         break;
27714       }
27715 }
27716
27717 /* OpenMP 2.5:
27718    variable-list:
27719      identifier
27720      variable-list , identifier
27721
27722    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27723    colon).  An opening parenthesis will have been consumed by the caller.
27724
27725    If KIND is nonzero, create the appropriate node and install the decl
27726    in OMP_CLAUSE_DECL and add the node to the head of the list.
27727
27728    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27729    return the list created.
27730
27731    COLON can be NULL if only closing parenthesis should end the list,
27732    or pointer to bool which will receive false if the list is terminated
27733    by closing parenthesis or true if the list is terminated by colon.  */
27734
27735 static tree
27736 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27737                                 tree list, bool *colon)
27738 {
27739   cp_token *token;
27740   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27741   if (colon)
27742     {
27743       parser->colon_corrects_to_scope_p = false;
27744       *colon = false;
27745     }
27746   while (1)
27747     {
27748       tree name, decl;
27749
27750       token = cp_lexer_peek_token (parser->lexer);
27751       name = cp_parser_id_expression (parser, /*template_p=*/false,
27752                                       /*check_dependency_p=*/true,
27753                                       /*template_p=*/NULL,
27754                                       /*declarator_p=*/false,
27755                                       /*optional_p=*/false);
27756       if (name == error_mark_node)
27757         goto skip_comma;
27758
27759       decl = cp_parser_lookup_name_simple (parser, name, token->location);
27760       if (decl == error_mark_node)
27761         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27762                                      token->location);
27763       else if (kind != 0)
27764         {
27765           switch (kind)
27766             {
27767             case OMP_CLAUSE__CACHE_:
27768               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27769                 {
27770                   error_at (token->location, "expected %<[%>");
27771                   decl = error_mark_node;
27772                   break;
27773                 }
27774               /* FALL THROUGH.  */
27775             case OMP_CLAUSE_MAP:
27776             case OMP_CLAUSE_FROM:
27777             case OMP_CLAUSE_TO:
27778             case OMP_CLAUSE_DEPEND:
27779               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27780                 {
27781                   tree low_bound = NULL_TREE, length = NULL_TREE;
27782
27783                   parser->colon_corrects_to_scope_p = false;
27784                   cp_lexer_consume_token (parser->lexer);
27785                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27786                     low_bound = cp_parser_expression (parser);
27787                   if (!colon)
27788                     parser->colon_corrects_to_scope_p
27789                       = saved_colon_corrects_to_scope_p;
27790                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27791                     length = integer_one_node;
27792                   else
27793                     {
27794                       /* Look for `:'.  */
27795                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27796                         goto skip_comma;
27797                       if (!cp_lexer_next_token_is (parser->lexer,
27798                                                    CPP_CLOSE_SQUARE))
27799                         length = cp_parser_expression (parser);
27800                     }
27801                   /* Look for the closing `]'.  */
27802                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27803                                           RT_CLOSE_SQUARE))
27804                     goto skip_comma;
27805
27806                   if (kind == OMP_CLAUSE__CACHE_)
27807                     {
27808                       if (TREE_CODE (low_bound) != INTEGER_CST
27809                           && !TREE_READONLY (low_bound))
27810                         {
27811                           error_at (token->location,
27812                                         "%qD is not a constant", low_bound);
27813                           decl = error_mark_node;
27814                         }
27815
27816                       if (TREE_CODE (length) != INTEGER_CST
27817                           && !TREE_READONLY (length))
27818                         {
27819                           error_at (token->location,
27820                                         "%qD is not a constant", length);
27821                           decl = error_mark_node;
27822                         }
27823                     }
27824
27825                   decl = tree_cons (low_bound, length, decl);
27826                 }
27827               break;
27828             default:
27829               break;
27830             }
27831
27832           tree u = build_omp_clause (token->location, kind);
27833           OMP_CLAUSE_DECL (u) = decl;
27834           OMP_CLAUSE_CHAIN (u) = list;
27835           list = u;
27836         }
27837       else
27838         list = tree_cons (decl, NULL_TREE, list);
27839
27840     get_comma:
27841       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27842         break;
27843       cp_lexer_consume_token (parser->lexer);
27844     }
27845
27846   if (colon)
27847     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27848
27849   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27850     {
27851       *colon = true;
27852       cp_parser_require (parser, CPP_COLON, RT_COLON);
27853       return list;
27854     }
27855
27856   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27857     {
27858       int ending;
27859
27860       /* Try to resync to an unnested comma.  Copied from
27861          cp_parser_parenthesized_expression_list.  */
27862     skip_comma:
27863       if (colon)
27864         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27865       ending = cp_parser_skip_to_closing_parenthesis (parser,
27866                                                       /*recovering=*/true,
27867                                                       /*or_comma=*/true,
27868                                                       /*consume_paren=*/true);
27869       if (ending < 0)
27870         goto get_comma;
27871     }
27872
27873   return list;
27874 }
27875
27876 /* Similarly, but expect leading and trailing parenthesis.  This is a very
27877    common case for omp clauses.  */
27878
27879 static tree
27880 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27881 {
27882   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27883     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27884   return list;
27885 }
27886
27887 /* OpenACC 2.0:
27888    copy ( variable-list )
27889    copyin ( variable-list )
27890    copyout ( variable-list )
27891    create ( variable-list )
27892    delete ( variable-list )
27893    present ( variable-list )
27894    present_or_copy ( variable-list )
27895      pcopy ( variable-list )
27896    present_or_copyin ( variable-list )
27897      pcopyin ( variable-list )
27898    present_or_copyout ( variable-list )
27899      pcopyout ( variable-list )
27900    present_or_create ( variable-list )
27901      pcreate ( variable-list ) */
27902
27903 static tree
27904 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
27905                             tree list)
27906 {
27907   enum gomp_map_kind kind;
27908   switch (c_kind)
27909     {
27910     case PRAGMA_OACC_CLAUSE_COPY:
27911       kind = GOMP_MAP_FORCE_TOFROM;
27912       break;
27913     case PRAGMA_OACC_CLAUSE_COPYIN:
27914       kind = GOMP_MAP_FORCE_TO;
27915       break;
27916     case PRAGMA_OACC_CLAUSE_COPYOUT:
27917       kind = GOMP_MAP_FORCE_FROM;
27918       break;
27919     case PRAGMA_OACC_CLAUSE_CREATE:
27920       kind = GOMP_MAP_FORCE_ALLOC;
27921       break;
27922     case PRAGMA_OACC_CLAUSE_DELETE:
27923       kind = GOMP_MAP_FORCE_DEALLOC;
27924       break;
27925     case PRAGMA_OACC_CLAUSE_DEVICE:
27926       kind = GOMP_MAP_FORCE_TO;
27927       break;
27928     case PRAGMA_OACC_CLAUSE_HOST:
27929     case PRAGMA_OACC_CLAUSE_SELF:
27930       kind = GOMP_MAP_FORCE_FROM;
27931       break;
27932     case PRAGMA_OACC_CLAUSE_PRESENT:
27933       kind = GOMP_MAP_FORCE_PRESENT;
27934       break;
27935     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
27936       kind = GOMP_MAP_TOFROM;
27937       break;
27938     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
27939       kind = GOMP_MAP_TO;
27940       break;
27941     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
27942       kind = GOMP_MAP_FROM;
27943       break;
27944     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
27945       kind = GOMP_MAP_ALLOC;
27946       break;
27947     default:
27948       gcc_unreachable ();
27949     }
27950   tree nl, c;
27951   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
27952
27953   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
27954     OMP_CLAUSE_SET_MAP_KIND (c, kind);
27955
27956   return nl;
27957 }
27958
27959 /* OpenACC 2.0:
27960    deviceptr ( variable-list ) */
27961
27962 static tree
27963 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
27964 {
27965   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27966   tree vars, t;
27967
27968   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
27969      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
27970      variable-list must only allow for pointer variables.  */
27971   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27972   for (t = vars; t; t = TREE_CHAIN (t))
27973     {
27974       tree v = TREE_PURPOSE (t);
27975
27976       /* FIXME diagnostics: Ideally we should keep individual
27977          locations for all the variables in the var list to make the
27978          following errors more precise.  Perhaps
27979          c_parser_omp_var_list_parens should construct a list of
27980          locations to go along with the var list.  */
27981
27982       if (TREE_CODE (v) != VAR_DECL)
27983         error_at (loc, "%qD is not a variable", v);
27984       else if (TREE_TYPE (v) == error_mark_node)
27985         ;
27986       else if (!POINTER_TYPE_P (TREE_TYPE (v)))
27987         error_at (loc, "%qD is not a pointer variable", v);
27988
27989       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
27990       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
27991       OMP_CLAUSE_DECL (u) = v;
27992       OMP_CLAUSE_CHAIN (u) = list;
27993       list = u;
27994     }
27995
27996   return list;
27997 }
27998
27999 /* OpenACC:
28000    vector_length ( expression ) */
28001
28002 static tree
28003 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28004 {
28005   tree t, c;
28006   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28007   bool error = false;
28008
28009   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28010     return list;
28011
28012   t = cp_parser_condition (parser);
28013   if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28014     {
28015       error_at (location, "expected positive integer expression");
28016       error = true;
28017     }
28018
28019   if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28020     {
28021       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28022                                            /*or_comma=*/false,
28023                                            /*consume_paren=*/true);
28024       return list;
28025     }
28026
28027   check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28028                              location);
28029
28030   c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28031   OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28032   OMP_CLAUSE_CHAIN (c) = list;
28033   list = c;
28034
28035   return list;
28036 }
28037
28038 /* OpenACC 2.0
28039    Parse wait clause or directive parameters.  */
28040
28041 static tree
28042 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28043 {
28044   vec<tree, va_gc> *args;
28045   tree t, args_tree;
28046
28047   args = cp_parser_parenthesized_expression_list (parser, non_attr,
28048                                                   /*cast_p=*/false,
28049                                                   /*allow_expansion_p=*/true,
28050                                                   /*non_constant_p=*/NULL);
28051
28052   if (args == NULL || args->length () == 0)
28053     {
28054       cp_parser_error (parser, "expected integer expression before ')'");
28055       if (args != NULL)
28056         release_tree_vector (args);
28057       return list;
28058     }
28059
28060   args_tree = build_tree_list_vec (args);
28061
28062   release_tree_vector (args);
28063
28064   for (t = args_tree; t; t = TREE_CHAIN (t))
28065     {
28066       tree targ = TREE_VALUE (t);
28067
28068       if (targ != error_mark_node)
28069         {
28070           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28071             error ("%<wait%> expression must be integral");
28072           else
28073             {
28074               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28075
28076               mark_rvalue_use (targ);
28077               OMP_CLAUSE_DECL (c) = targ;
28078               OMP_CLAUSE_CHAIN (c) = list;
28079               list = c;
28080             }
28081         }
28082     }
28083
28084   return list;
28085 }
28086
28087 /* OpenACC:
28088    wait ( int-expr-list ) */
28089
28090 static tree
28091 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28092 {
28093   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28094
28095   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28096     return list;
28097
28098   list = cp_parser_oacc_wait_list (parser, location, list);
28099
28100   return list;
28101 }
28102
28103 /* OpenMP 3.0:
28104    collapse ( constant-expression ) */
28105
28106 static tree
28107 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28108 {
28109   tree c, num;
28110   location_t loc;
28111   HOST_WIDE_INT n;
28112
28113   loc = cp_lexer_peek_token (parser->lexer)->location;
28114   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28115     return list;
28116
28117   num = cp_parser_constant_expression (parser);
28118
28119   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28120     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28121                                            /*or_comma=*/false,
28122                                            /*consume_paren=*/true);
28123
28124   if (num == error_mark_node)
28125     return list;
28126   num = fold_non_dependent_expr (num);
28127   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
28128       || !tree_fits_shwi_p (num)
28129       || (n = tree_to_shwi (num)) <= 0
28130       || (int) n != n)
28131     {
28132       error_at (loc, "collapse argument needs positive constant integer expression");
28133       return list;
28134     }
28135
28136   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28137   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28138   OMP_CLAUSE_CHAIN (c) = list;
28139   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28140
28141   return c;
28142 }
28143
28144 /* OpenMP 2.5:
28145    default ( shared | none ) */
28146
28147 static tree
28148 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28149 {
28150   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28151   tree c;
28152
28153   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28154     return list;
28155   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28156     {
28157       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28158       const char *p = IDENTIFIER_POINTER (id);
28159
28160       switch (p[0])
28161         {
28162         case 'n':
28163           if (strcmp ("none", p) != 0)
28164             goto invalid_kind;
28165           kind = OMP_CLAUSE_DEFAULT_NONE;
28166           break;
28167
28168         case 's':
28169           if (strcmp ("shared", p) != 0)
28170             goto invalid_kind;
28171           kind = OMP_CLAUSE_DEFAULT_SHARED;
28172           break;
28173
28174         default:
28175           goto invalid_kind;
28176         }
28177
28178       cp_lexer_consume_token (parser->lexer);
28179     }
28180   else
28181     {
28182     invalid_kind:
28183       cp_parser_error (parser, "expected %<none%> or %<shared%>");
28184     }
28185
28186   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28187     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28188                                            /*or_comma=*/false,
28189                                            /*consume_paren=*/true);
28190
28191   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28192     return list;
28193
28194   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28195   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28196   OMP_CLAUSE_CHAIN (c) = list;
28197   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28198
28199   return c;
28200 }
28201
28202 /* OpenMP 3.1:
28203    final ( expression ) */
28204
28205 static tree
28206 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28207 {
28208   tree t, c;
28209
28210   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28211     return list;
28212
28213   t = cp_parser_condition (parser);
28214
28215   if (t == error_mark_node
28216       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28217     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28218                                            /*or_comma=*/false,
28219                                            /*consume_paren=*/true);
28220
28221   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28222
28223   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28224   OMP_CLAUSE_FINAL_EXPR (c) = t;
28225   OMP_CLAUSE_CHAIN (c) = list;
28226
28227   return c;
28228 }
28229
28230 /* OpenMP 2.5:
28231    if ( expression ) */
28232
28233 static tree
28234 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
28235 {
28236   tree t, c;
28237
28238   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28239     return list;
28240
28241   t = cp_parser_condition (parser);
28242
28243   if (t == error_mark_node
28244       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28245     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28246                                            /*or_comma=*/false,
28247                                            /*consume_paren=*/true);
28248
28249   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
28250
28251   c = build_omp_clause (location, OMP_CLAUSE_IF);
28252   OMP_CLAUSE_IF_EXPR (c) = t;
28253   OMP_CLAUSE_CHAIN (c) = list;
28254
28255   return c;
28256 }
28257
28258 /* OpenMP 3.1:
28259    mergeable */
28260
28261 static tree
28262 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28263                                 tree list, location_t location)
28264 {
28265   tree c;
28266
28267   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28268                              location);
28269
28270   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28271   OMP_CLAUSE_CHAIN (c) = list;
28272   return c;
28273 }
28274
28275 /* OpenMP 2.5:
28276    nowait */
28277
28278 static tree
28279 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28280                              tree list, location_t location)
28281 {
28282   tree c;
28283
28284   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28285
28286   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28287   OMP_CLAUSE_CHAIN (c) = list;
28288   return c;
28289 }
28290
28291 /* OpenACC:
28292    num_gangs ( expression ) */
28293
28294 static tree
28295 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28296 {
28297   tree t, c;
28298   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28299
28300   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28301     return list;
28302
28303   t = cp_parser_condition (parser);
28304
28305   if (t == error_mark_node
28306       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28307     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28308                                            /*or_comma=*/false,
28309                                            /*consume_paren=*/true);
28310
28311   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28312     {
28313       error_at (location, "expected positive integer expression");
28314       return list;
28315     }
28316
28317   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28318
28319   c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28320   OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28321   OMP_CLAUSE_CHAIN (c) = list;
28322   list = c;
28323
28324   return list;
28325 }
28326
28327 /* OpenMP 2.5:
28328    num_threads ( expression ) */
28329
28330 static tree
28331 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28332                                   location_t location)
28333 {
28334   tree t, c;
28335
28336   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28337     return list;
28338
28339   t = cp_parser_expression (parser);
28340
28341   if (t == error_mark_node
28342       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28343     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28344                                            /*or_comma=*/false,
28345                                            /*consume_paren=*/true);
28346
28347   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28348                              "num_threads", location);
28349
28350   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28351   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28352   OMP_CLAUSE_CHAIN (c) = list;
28353
28354   return c;
28355 }
28356
28357 /* OpenACC:
28358    num_workers ( expression ) */
28359
28360 static tree
28361 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28362 {
28363   tree t, c;
28364   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28365
28366   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28367     return list;
28368
28369   t = cp_parser_condition (parser);
28370
28371   if (t == error_mark_node
28372       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28373     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28374                                            /*or_comma=*/false,
28375                                            /*consume_paren=*/true);
28376
28377   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28378     {
28379       error_at (location, "expected positive integer expression");
28380       return list;
28381     }
28382
28383   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28384                                                                 location);
28385
28386   c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28387   OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28388   OMP_CLAUSE_CHAIN (c) = list;
28389   list = c;
28390
28391   return list;
28392 }
28393
28394 /* OpenMP 2.5:
28395    ordered */
28396
28397 static tree
28398 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28399                               tree list, location_t location)
28400 {
28401   tree c;
28402
28403   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28404                              "ordered", location);
28405
28406   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28407   OMP_CLAUSE_CHAIN (c) = list;
28408   return c;
28409 }
28410
28411 /* OpenMP 2.5:
28412    reduction ( reduction-operator : variable-list )
28413
28414    reduction-operator:
28415      One of: + * - & ^ | && ||
28416
28417    OpenMP 3.1:
28418
28419    reduction-operator:
28420      One of: + * - & ^ | && || min max
28421
28422    OpenMP 4.0:
28423
28424    reduction-operator:
28425      One of: + * - & ^ | && ||
28426      id-expression  */
28427
28428 static tree
28429 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28430 {
28431   enum tree_code code = ERROR_MARK;
28432   tree nlist, c, id = NULL_TREE;
28433
28434   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28435     return list;
28436
28437   switch (cp_lexer_peek_token (parser->lexer)->type)
28438     {
28439     case CPP_PLUS: code = PLUS_EXPR; break;
28440     case CPP_MULT: code = MULT_EXPR; break;
28441     case CPP_MINUS: code = MINUS_EXPR; break;
28442     case CPP_AND: code = BIT_AND_EXPR; break;
28443     case CPP_XOR: code = BIT_XOR_EXPR; break;
28444     case CPP_OR: code = BIT_IOR_EXPR; break;
28445     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28446     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28447     default: break;
28448     }
28449
28450   if (code != ERROR_MARK)
28451     cp_lexer_consume_token (parser->lexer);
28452   else
28453     {
28454       bool saved_colon_corrects_to_scope_p;
28455       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28456       parser->colon_corrects_to_scope_p = false;
28457       id = cp_parser_id_expression (parser, /*template_p=*/false,
28458                                     /*check_dependency_p=*/true,
28459                                     /*template_p=*/NULL,
28460                                     /*declarator_p=*/false,
28461                                     /*optional_p=*/false);
28462       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28463       if (identifier_p (id))
28464         {
28465           const char *p = IDENTIFIER_POINTER (id);
28466
28467           if (strcmp (p, "min") == 0)
28468             code = MIN_EXPR;
28469           else if (strcmp (p, "max") == 0)
28470             code = MAX_EXPR;
28471           else if (id == ansi_opname (PLUS_EXPR))
28472             code = PLUS_EXPR;
28473           else if (id == ansi_opname (MULT_EXPR))
28474             code = MULT_EXPR;
28475           else if (id == ansi_opname (MINUS_EXPR))
28476             code = MINUS_EXPR;
28477           else if (id == ansi_opname (BIT_AND_EXPR))
28478             code = BIT_AND_EXPR;
28479           else if (id == ansi_opname (BIT_IOR_EXPR))
28480             code = BIT_IOR_EXPR;
28481           else if (id == ansi_opname (BIT_XOR_EXPR))
28482             code = BIT_XOR_EXPR;
28483           else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28484             code = TRUTH_ANDIF_EXPR;
28485           else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28486             code = TRUTH_ORIF_EXPR;
28487           id = omp_reduction_id (code, id, NULL_TREE);
28488           tree scope = parser->scope;
28489           if (scope)
28490             id = build_qualified_name (NULL_TREE, scope, id, false);
28491           parser->scope = NULL_TREE;
28492           parser->qualifying_scope = NULL_TREE;
28493           parser->object_scope = NULL_TREE;
28494         }
28495       else
28496         {
28497           error ("invalid reduction-identifier");
28498          resync_fail:
28499           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28500                                                  /*or_comma=*/false,
28501                                                  /*consume_paren=*/true);
28502           return list;
28503         }
28504     }
28505
28506   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28507     goto resync_fail;
28508
28509   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28510                                           NULL);
28511   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28512     {
28513       OMP_CLAUSE_REDUCTION_CODE (c) = code;
28514       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28515     }
28516
28517   return nlist;
28518 }
28519
28520 /* OpenMP 2.5:
28521    schedule ( schedule-kind )
28522    schedule ( schedule-kind , expression )
28523
28524    schedule-kind:
28525      static | dynamic | guided | runtime | auto  */
28526
28527 static tree
28528 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28529 {
28530   tree c, t;
28531
28532   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28533     return list;
28534
28535   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28536
28537   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28538     {
28539       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28540       const char *p = IDENTIFIER_POINTER (id);
28541
28542       switch (p[0])
28543         {
28544         case 'd':
28545           if (strcmp ("dynamic", p) != 0)
28546             goto invalid_kind;
28547           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28548           break;
28549
28550         case 'g':
28551           if (strcmp ("guided", p) != 0)
28552             goto invalid_kind;
28553           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28554           break;
28555
28556         case 'r':
28557           if (strcmp ("runtime", p) != 0)
28558             goto invalid_kind;
28559           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28560           break;
28561
28562         default:
28563           goto invalid_kind;
28564         }
28565     }
28566   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28567     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28568   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28569     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28570   else
28571     goto invalid_kind;
28572   cp_lexer_consume_token (parser->lexer);
28573
28574   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28575     {
28576       cp_token *token;
28577       cp_lexer_consume_token (parser->lexer);
28578
28579       token = cp_lexer_peek_token (parser->lexer);
28580       t = cp_parser_assignment_expression (parser);
28581
28582       if (t == error_mark_node)
28583         goto resync_fail;
28584       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28585         error_at (token->location, "schedule %<runtime%> does not take "
28586                   "a %<chunk_size%> parameter");
28587       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28588         error_at (token->location, "schedule %<auto%> does not take "
28589                   "a %<chunk_size%> parameter");
28590       else
28591         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28592
28593       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28594         goto resync_fail;
28595     }
28596   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28597     goto resync_fail;
28598
28599   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28600   OMP_CLAUSE_CHAIN (c) = list;
28601   return c;
28602
28603  invalid_kind:
28604   cp_parser_error (parser, "invalid schedule kind");
28605  resync_fail:
28606   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28607                                          /*or_comma=*/false,
28608                                          /*consume_paren=*/true);
28609   return list;
28610 }
28611
28612 /* OpenMP 3.0:
28613    untied */
28614
28615 static tree
28616 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28617                              tree list, location_t location)
28618 {
28619   tree c;
28620
28621   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28622
28623   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28624   OMP_CLAUSE_CHAIN (c) = list;
28625   return c;
28626 }
28627
28628 /* OpenMP 4.0:
28629    inbranch
28630    notinbranch */
28631
28632 static tree
28633 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28634                              tree list, location_t location)
28635 {
28636   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28637   tree c = build_omp_clause (location, code);
28638   OMP_CLAUSE_CHAIN (c) = list;
28639   return c;
28640 }
28641
28642 /* OpenMP 4.0:
28643    parallel
28644    for
28645    sections
28646    taskgroup */
28647
28648 static tree
28649 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28650                                  enum omp_clause_code code,
28651                                  tree list, location_t location)
28652 {
28653   tree c = build_omp_clause (location, code);
28654   OMP_CLAUSE_CHAIN (c) = list;
28655   return c;
28656 }
28657
28658 /* OpenMP 4.0:
28659    num_teams ( expression ) */
28660
28661 static tree
28662 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28663                                 location_t location)
28664 {
28665   tree t, c;
28666
28667   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28668     return list;
28669
28670   t = cp_parser_expression (parser);
28671
28672   if (t == error_mark_node
28673       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28674     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28675                                            /*or_comma=*/false,
28676                                            /*consume_paren=*/true);
28677
28678   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28679                              "num_teams", location);
28680
28681   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28682   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28683   OMP_CLAUSE_CHAIN (c) = list;
28684
28685   return c;
28686 }
28687
28688 /* OpenMP 4.0:
28689    thread_limit ( expression ) */
28690
28691 static tree
28692 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28693                                    location_t location)
28694 {
28695   tree t, c;
28696
28697   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28698     return list;
28699
28700   t = cp_parser_expression (parser);
28701
28702   if (t == error_mark_node
28703       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28704     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28705                                            /*or_comma=*/false,
28706                                            /*consume_paren=*/true);
28707
28708   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28709                              "thread_limit", location);
28710
28711   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28712   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28713   OMP_CLAUSE_CHAIN (c) = list;
28714
28715   return c;
28716 }
28717
28718 /* OpenMP 4.0:
28719    aligned ( variable-list )
28720    aligned ( variable-list : constant-expression )  */
28721
28722 static tree
28723 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28724 {
28725   tree nlist, c, alignment = NULL_TREE;
28726   bool colon;
28727
28728   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28729     return list;
28730
28731   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28732                                           &colon);
28733
28734   if (colon)
28735     {
28736       alignment = cp_parser_constant_expression (parser);
28737
28738       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28739         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28740                                                /*or_comma=*/false,
28741                                                /*consume_paren=*/true);
28742
28743       if (alignment == error_mark_node)
28744         alignment = NULL_TREE;
28745     }
28746
28747   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28748     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28749
28750   return nlist;
28751 }
28752
28753 /* OpenMP 4.0:
28754    linear ( variable-list )
28755    linear ( variable-list : expression )  */
28756
28757 static tree
28758 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
28759                              bool is_cilk_simd_fn)
28760 {
28761   tree nlist, c, step = integer_one_node;
28762   bool colon;
28763
28764   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28765     return list;
28766
28767   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28768                                           &colon);
28769
28770   if (colon)
28771     {
28772       step = cp_parser_expression (parser);
28773
28774       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28775         {
28776           sorry ("using parameters for %<linear%> step is not supported yet");
28777           step = integer_one_node;
28778         }
28779       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28780         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28781                                                /*or_comma=*/false,
28782                                                /*consume_paren=*/true);
28783
28784       if (step == error_mark_node)
28785         return list;
28786     }
28787
28788   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28789     OMP_CLAUSE_LINEAR_STEP (c) = step;
28790
28791   return nlist;
28792 }
28793
28794 /* OpenMP 4.0:
28795    safelen ( constant-expression )  */
28796
28797 static tree
28798 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28799                               location_t location)
28800 {
28801   tree t, c;
28802
28803   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28804     return list;
28805
28806   t = cp_parser_constant_expression (parser);
28807
28808   if (t == error_mark_node
28809       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28810     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28811                                            /*or_comma=*/false,
28812                                            /*consume_paren=*/true);
28813
28814   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28815
28816   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28817   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28818   OMP_CLAUSE_CHAIN (c) = list;
28819
28820   return c;
28821 }
28822
28823 /* OpenMP 4.0:
28824    simdlen ( constant-expression )  */
28825
28826 static tree
28827 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28828                               location_t location)
28829 {
28830   tree t, c;
28831
28832   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28833     return list;
28834
28835   t = cp_parser_constant_expression (parser);
28836
28837   if (t == error_mark_node
28838       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28839     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28840                                            /*or_comma=*/false,
28841                                            /*consume_paren=*/true);
28842
28843   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28844
28845   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28846   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28847   OMP_CLAUSE_CHAIN (c) = list;
28848
28849   return c;
28850 }
28851
28852 /* OpenMP 4.0:
28853    depend ( depend-kind : variable-list )
28854
28855    depend-kind:
28856      in | out | inout  */
28857
28858 static tree
28859 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28860 {
28861   tree nlist, c;
28862   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28863
28864   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28865     return list;
28866
28867   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28868     {
28869       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28870       const char *p = IDENTIFIER_POINTER (id);
28871
28872       if (strcmp ("in", p) == 0)
28873         kind = OMP_CLAUSE_DEPEND_IN;
28874       else if (strcmp ("inout", p) == 0)
28875         kind = OMP_CLAUSE_DEPEND_INOUT;
28876       else if (strcmp ("out", p) == 0)
28877         kind = OMP_CLAUSE_DEPEND_OUT;
28878       else
28879         goto invalid_kind;
28880     }
28881   else
28882     goto invalid_kind;
28883
28884   cp_lexer_consume_token (parser->lexer);
28885   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28886     goto resync_fail;
28887
28888   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28889                                           NULL);
28890
28891   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28892     OMP_CLAUSE_DEPEND_KIND (c) = kind;
28893
28894   return nlist;
28895
28896  invalid_kind:
28897   cp_parser_error (parser, "invalid depend kind");
28898  resync_fail:
28899   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28900                                          /*or_comma=*/false,
28901                                          /*consume_paren=*/true);
28902   return list;
28903 }
28904
28905 /* OpenMP 4.0:
28906    map ( map-kind : variable-list )
28907    map ( variable-list )
28908
28909    map-kind:
28910      alloc | to | from | tofrom  */
28911
28912 static tree
28913 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28914 {
28915   tree nlist, c;
28916   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
28917
28918   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28919     return list;
28920
28921   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28922       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28923     {
28924       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28925       const char *p = IDENTIFIER_POINTER (id);
28926
28927       if (strcmp ("alloc", p) == 0)
28928         kind = GOMP_MAP_ALLOC;
28929       else if (strcmp ("to", p) == 0)
28930         kind = GOMP_MAP_TO;
28931       else if (strcmp ("from", p) == 0)
28932         kind = GOMP_MAP_FROM;
28933       else if (strcmp ("tofrom", p) == 0)
28934         kind = GOMP_MAP_TOFROM;
28935       else
28936         {
28937           cp_parser_error (parser, "invalid map kind");
28938           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28939                                                  /*or_comma=*/false,
28940                                                  /*consume_paren=*/true);
28941           return list;
28942         }
28943       cp_lexer_consume_token (parser->lexer);
28944       cp_lexer_consume_token (parser->lexer);
28945     }
28946
28947   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28948                                           NULL);
28949
28950   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28951     OMP_CLAUSE_SET_MAP_KIND (c, kind);
28952
28953   return nlist;
28954 }
28955
28956 /* OpenMP 4.0:
28957    device ( expression ) */
28958
28959 static tree
28960 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28961                              location_t location)
28962 {
28963   tree t, c;
28964
28965   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28966     return list;
28967
28968   t = cp_parser_expression (parser);
28969
28970   if (t == error_mark_node
28971       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28972     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28973                                            /*or_comma=*/false,
28974                                            /*consume_paren=*/true);
28975
28976   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28977                              "device", location);
28978
28979   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28980   OMP_CLAUSE_DEVICE_ID (c) = t;
28981   OMP_CLAUSE_CHAIN (c) = list;
28982
28983   return c;
28984 }
28985
28986 /* OpenMP 4.0:
28987    dist_schedule ( static )
28988    dist_schedule ( static , expression )  */
28989
28990 static tree
28991 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28992                                     location_t location)
28993 {
28994   tree c, t;
28995
28996   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28997     return list;
28998
28999   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29000
29001   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29002     goto invalid_kind;
29003   cp_lexer_consume_token (parser->lexer);
29004
29005   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29006     {
29007       cp_lexer_consume_token (parser->lexer);
29008
29009       t = cp_parser_assignment_expression (parser);
29010
29011       if (t == error_mark_node)
29012         goto resync_fail;
29013       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29014
29015       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29016         goto resync_fail;
29017     }
29018   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29019     goto resync_fail;
29020
29021   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29022                              location);
29023   OMP_CLAUSE_CHAIN (c) = list;
29024   return c;
29025
29026  invalid_kind:
29027   cp_parser_error (parser, "invalid dist_schedule kind");
29028  resync_fail:
29029   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29030                                          /*or_comma=*/false,
29031                                          /*consume_paren=*/true);
29032   return list;
29033 }
29034
29035 /* OpenMP 4.0:
29036    proc_bind ( proc-bind-kind )
29037
29038    proc-bind-kind:
29039      master | close | spread  */
29040
29041 static tree
29042 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29043                                 location_t location)
29044 {
29045   tree c;
29046   enum omp_clause_proc_bind_kind kind;
29047
29048   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29049     return list;
29050
29051   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29052     {
29053       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29054       const char *p = IDENTIFIER_POINTER (id);
29055
29056       if (strcmp ("master", p) == 0)
29057         kind = OMP_CLAUSE_PROC_BIND_MASTER;
29058       else if (strcmp ("close", p) == 0)
29059         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29060       else if (strcmp ("spread", p) == 0)
29061         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29062       else
29063         goto invalid_kind;
29064     }
29065   else
29066     goto invalid_kind;
29067
29068   cp_lexer_consume_token (parser->lexer);
29069   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29070     goto resync_fail;
29071
29072   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29073   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29074                              location);
29075   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29076   OMP_CLAUSE_CHAIN (c) = list;
29077   return c;
29078
29079  invalid_kind:
29080   cp_parser_error (parser, "invalid depend kind");
29081  resync_fail:
29082   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29083                                          /*or_comma=*/false,
29084                                          /*consume_paren=*/true);
29085   return list;
29086 }
29087
29088 /* OpenACC:
29089    async [( int-expr )] */
29090
29091 static tree
29092 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29093 {
29094   tree c, t;
29095   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29096
29097   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29098
29099   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29100     {
29101       cp_lexer_consume_token (parser->lexer);
29102
29103       t = cp_parser_expression (parser);
29104       if (t == error_mark_node
29105           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29106         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29107                                                 /*or_comma=*/false,
29108                                                 /*consume_paren=*/true);
29109     }
29110
29111   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29112
29113   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29114   OMP_CLAUSE_ASYNC_EXPR (c) = t;
29115   OMP_CLAUSE_CHAIN (c) = list;
29116   list = c;
29117
29118   return list;
29119 }
29120
29121 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
29122    is a bitmask in MASK.  Return the list of clauses found.  */
29123
29124 static tree
29125 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29126                            const char *where, cp_token *pragma_tok,
29127                            bool finish_p = true)
29128 {
29129   tree clauses = NULL;
29130   bool first = true;
29131
29132   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29133     {
29134       location_t here;
29135       pragma_omp_clause c_kind;
29136       const char *c_name;
29137       tree prev = clauses;
29138
29139       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29140         cp_lexer_consume_token (parser->lexer);
29141
29142       here = cp_lexer_peek_token (parser->lexer)->location;
29143       c_kind = cp_parser_omp_clause_name (parser);
29144
29145       switch (c_kind)
29146         {
29147         case PRAGMA_OACC_CLAUSE_ASYNC:
29148           clauses = cp_parser_oacc_clause_async (parser, clauses);
29149           c_name = "async";
29150           break;
29151         case PRAGMA_OACC_CLAUSE_COLLAPSE:
29152           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29153           c_name = "collapse";
29154           break;
29155         case PRAGMA_OACC_CLAUSE_COPY:
29156           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29157           c_name = "copy";
29158           break;
29159         case PRAGMA_OACC_CLAUSE_COPYIN:
29160           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29161           c_name = "copyin";
29162           break;
29163         case PRAGMA_OACC_CLAUSE_COPYOUT:
29164           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29165           c_name = "copyout";
29166           break;
29167         case PRAGMA_OACC_CLAUSE_CREATE:
29168           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29169           c_name = "create";
29170           break;
29171         case PRAGMA_OACC_CLAUSE_DELETE:
29172           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29173           c_name = "delete";
29174           break;
29175         case PRAGMA_OACC_CLAUSE_DEVICE:
29176           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29177           c_name = "device";
29178           break;
29179         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29180           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29181           c_name = "deviceptr";
29182           break;
29183         case PRAGMA_OACC_CLAUSE_HOST:
29184           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29185           c_name = "host";
29186           break;
29187         case PRAGMA_OACC_CLAUSE_IF:
29188           clauses = cp_parser_omp_clause_if (parser, clauses, here);
29189           c_name = "if";
29190           break;
29191         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29192           clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29193           c_name = "num_gangs";
29194           break;
29195         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29196           clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29197           c_name = "num_workers";
29198           break;
29199         case PRAGMA_OACC_CLAUSE_PRESENT:
29200           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29201           c_name = "present";
29202           break;
29203         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29204           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29205           c_name = "present_or_copy";
29206           break;
29207         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29208           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29209           c_name = "present_or_copyin";
29210           break;
29211         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29212           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29213           c_name = "present_or_copyout";
29214           break;
29215         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29216           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29217           c_name = "present_or_create";
29218           break;
29219         case PRAGMA_OACC_CLAUSE_REDUCTION:
29220           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29221           c_name = "reduction";
29222           break;
29223         case PRAGMA_OACC_CLAUSE_SELF:
29224           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29225           c_name = "self";
29226           break;
29227         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29228           clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29229           c_name = "vector_length";
29230           break;
29231         case PRAGMA_OACC_CLAUSE_WAIT:
29232           clauses = cp_parser_oacc_clause_wait (parser, clauses);
29233           c_name = "wait";
29234           break;
29235         default:
29236           cp_parser_error (parser, "expected %<#pragma acc%> clause");
29237           goto saw_error;
29238         }
29239
29240       first = false;
29241
29242       if (((mask >> c_kind) & 1) == 0)
29243         {
29244           /* Remove the invalid clause(s) from the list to avoid
29245              confusing the rest of the compiler.  */
29246           clauses = prev;
29247           error_at (here, "%qs is not valid for %qs", c_name, where);
29248         }
29249     }
29250
29251  saw_error:
29252   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29253
29254   if (finish_p)
29255     return finish_omp_clauses (clauses);
29256
29257   return clauses;
29258 }
29259
29260 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
29261    is a bitmask in MASK.  Return the list of clauses found; the result
29262    of clause default goes in *pdefault.  */
29263
29264 static tree
29265 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29266                            const char *where, cp_token *pragma_tok,
29267                            bool finish_p = true)
29268 {
29269   tree clauses = NULL;
29270   bool first = true;
29271   cp_token *token = NULL;
29272   bool cilk_simd_fn = false;
29273
29274   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29275     {
29276       pragma_omp_clause c_kind;
29277       const char *c_name;
29278       tree prev = clauses;
29279
29280       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29281         cp_lexer_consume_token (parser->lexer);
29282
29283       token = cp_lexer_peek_token (parser->lexer);
29284       c_kind = cp_parser_omp_clause_name (parser);
29285
29286       switch (c_kind)
29287         {
29288         case PRAGMA_OMP_CLAUSE_COLLAPSE:
29289           clauses = cp_parser_omp_clause_collapse (parser, clauses,
29290                                                    token->location);
29291           c_name = "collapse";
29292           break;
29293         case PRAGMA_OMP_CLAUSE_COPYIN:
29294           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29295           c_name = "copyin";
29296           break;
29297         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29298           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29299                                             clauses);
29300           c_name = "copyprivate";
29301           break;
29302         case PRAGMA_OMP_CLAUSE_DEFAULT:
29303           clauses = cp_parser_omp_clause_default (parser, clauses,
29304                                                   token->location);
29305           c_name = "default";
29306           break;
29307         case PRAGMA_OMP_CLAUSE_FINAL:
29308           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29309           c_name = "final";
29310           break;
29311         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29312           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29313                                             clauses);
29314           c_name = "firstprivate";
29315           break;
29316         case PRAGMA_OMP_CLAUSE_IF:
29317           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29318           c_name = "if";
29319           break;
29320         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29321           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29322                                             clauses);
29323           c_name = "lastprivate";
29324           break;
29325         case PRAGMA_OMP_CLAUSE_MERGEABLE:
29326           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29327                                                     token->location);
29328           c_name = "mergeable";
29329           break;
29330         case PRAGMA_OMP_CLAUSE_NOWAIT:
29331           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29332           c_name = "nowait";
29333           break;
29334         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29335           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29336                                                       token->location);
29337           c_name = "num_threads";
29338           break;
29339         case PRAGMA_OMP_CLAUSE_ORDERED:
29340           clauses = cp_parser_omp_clause_ordered (parser, clauses,
29341                                                   token->location);
29342           c_name = "ordered";
29343           break;
29344         case PRAGMA_OMP_CLAUSE_PRIVATE:
29345           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29346                                             clauses);
29347           c_name = "private";
29348           break;
29349         case PRAGMA_OMP_CLAUSE_REDUCTION:
29350           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29351           c_name = "reduction";
29352           break;
29353         case PRAGMA_OMP_CLAUSE_SCHEDULE:
29354           clauses = cp_parser_omp_clause_schedule (parser, clauses,
29355                                                    token->location);
29356           c_name = "schedule";
29357           break;
29358         case PRAGMA_OMP_CLAUSE_SHARED:
29359           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29360                                             clauses);
29361           c_name = "shared";
29362           break;
29363         case PRAGMA_OMP_CLAUSE_UNTIED:
29364           clauses = cp_parser_omp_clause_untied (parser, clauses,
29365                                                  token->location);
29366           c_name = "untied";
29367           break;
29368         case PRAGMA_OMP_CLAUSE_INBRANCH:
29369         case PRAGMA_CILK_CLAUSE_MASK:
29370           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29371                                                  clauses, token->location);
29372           c_name = "inbranch";
29373           break;
29374         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29375         case PRAGMA_CILK_CLAUSE_NOMASK:
29376           clauses = cp_parser_omp_clause_branch (parser,
29377                                                  OMP_CLAUSE_NOTINBRANCH,
29378                                                  clauses, token->location);
29379           c_name = "notinbranch";
29380           break;
29381         case PRAGMA_OMP_CLAUSE_PARALLEL:
29382           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29383                                                      clauses, token->location);
29384           c_name = "parallel";
29385           if (!first)
29386             {
29387              clause_not_first:
29388               error_at (token->location, "%qs must be the first clause of %qs",
29389                         c_name, where);
29390               clauses = prev;
29391             }
29392           break;
29393         case PRAGMA_OMP_CLAUSE_FOR:
29394           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29395                                                      clauses, token->location);
29396           c_name = "for";
29397           if (!first)
29398             goto clause_not_first;
29399           break;
29400         case PRAGMA_OMP_CLAUSE_SECTIONS:
29401           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29402                                                      clauses, token->location);
29403           c_name = "sections";
29404           if (!first)
29405             goto clause_not_first;
29406           break;
29407         case PRAGMA_OMP_CLAUSE_TASKGROUP:
29408           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29409                                                      clauses, token->location);
29410           c_name = "taskgroup";
29411           if (!first)
29412             goto clause_not_first;
29413           break;
29414         case PRAGMA_OMP_CLAUSE_TO:
29415           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29416                                             clauses);
29417           c_name = "to";
29418           break;
29419         case PRAGMA_OMP_CLAUSE_FROM:
29420           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29421                                             clauses);
29422           c_name = "from";
29423           break;
29424         case PRAGMA_OMP_CLAUSE_UNIFORM:
29425           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29426                                             clauses);
29427           c_name = "uniform";
29428           break;
29429         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29430           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29431                                                     token->location);
29432           c_name = "num_teams";
29433           break;
29434         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29435           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29436                                                        token->location);
29437           c_name = "thread_limit";
29438           break;
29439         case PRAGMA_OMP_CLAUSE_ALIGNED:
29440           clauses = cp_parser_omp_clause_aligned (parser, clauses);
29441           c_name = "aligned";
29442           break;
29443         case PRAGMA_OMP_CLAUSE_LINEAR:
29444           if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29445             cilk_simd_fn = true;
29446           clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29447           c_name = "linear";
29448           break;
29449         case PRAGMA_OMP_CLAUSE_DEPEND:
29450           clauses = cp_parser_omp_clause_depend (parser, clauses);
29451           c_name = "depend";
29452           break;
29453         case PRAGMA_OMP_CLAUSE_MAP:
29454           clauses = cp_parser_omp_clause_map (parser, clauses);
29455           c_name = "map";
29456           break;
29457         case PRAGMA_OMP_CLAUSE_DEVICE:
29458           clauses = cp_parser_omp_clause_device (parser, clauses,
29459                                                  token->location);
29460           c_name = "device";
29461           break;
29462         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29463           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29464                                                         token->location);
29465           c_name = "dist_schedule";
29466           break;
29467         case PRAGMA_OMP_CLAUSE_PROC_BIND:
29468           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29469                                                     token->location);
29470           c_name = "proc_bind";
29471           break;
29472         case PRAGMA_OMP_CLAUSE_SAFELEN:
29473           clauses = cp_parser_omp_clause_safelen (parser, clauses,
29474                                                   token->location);
29475           c_name = "safelen";
29476           break;
29477         case PRAGMA_OMP_CLAUSE_SIMDLEN:
29478           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29479                                                   token->location);
29480           c_name = "simdlen";
29481           break;
29482         case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29483           clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29484           c_name = "simdlen";
29485           break;
29486         default:
29487           cp_parser_error (parser, "expected %<#pragma omp%> clause");
29488           goto saw_error;
29489         }
29490
29491       first = false;
29492
29493       if (((mask >> c_kind) & 1) == 0)
29494         {
29495           /* Remove the invalid clause(s) from the list to avoid
29496              confusing the rest of the compiler.  */
29497           clauses = prev;
29498           error_at (token->location, "%qs is not valid for %qs", c_name, where);
29499         }
29500     }
29501  saw_error:
29502   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29503      no reason to skip to the end.  */
29504   if (!(flag_cilkplus && pragma_tok == NULL))
29505     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29506   if (finish_p)
29507     return finish_omp_clauses (clauses);
29508   return clauses;
29509 }
29510
29511 /* OpenMP 2.5:
29512    structured-block:
29513      statement
29514
29515    In practice, we're also interested in adding the statement to an
29516    outer node.  So it is convenient if we work around the fact that
29517    cp_parser_statement calls add_stmt.  */
29518
29519 static unsigned
29520 cp_parser_begin_omp_structured_block (cp_parser *parser)
29521 {
29522   unsigned save = parser->in_statement;
29523
29524   /* Only move the values to IN_OMP_BLOCK if they weren't false.
29525      This preserves the "not within loop or switch" style error messages
29526      for nonsense cases like
29527         void foo() {
29528         #pragma omp single
29529           break;
29530         }
29531   */
29532   if (parser->in_statement)
29533     parser->in_statement = IN_OMP_BLOCK;
29534
29535   return save;
29536 }
29537
29538 static void
29539 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29540 {
29541   parser->in_statement = save;
29542 }
29543
29544 static tree
29545 cp_parser_omp_structured_block (cp_parser *parser)
29546 {
29547   tree stmt = begin_omp_structured_block ();
29548   unsigned int save = cp_parser_begin_omp_structured_block (parser);
29549
29550   cp_parser_statement (parser, NULL_TREE, false, NULL);
29551
29552   cp_parser_end_omp_structured_block (parser, save);
29553   return finish_omp_structured_block (stmt);
29554 }
29555
29556 /* OpenMP 2.5:
29557    # pragma omp atomic new-line
29558      expression-stmt
29559
29560    expression-stmt:
29561      x binop= expr | x++ | ++x | x-- | --x
29562    binop:
29563      +, *, -, /, &, ^, |, <<, >>
29564
29565   where x is an lvalue expression with scalar type.
29566
29567    OpenMP 3.1:
29568    # pragma omp atomic new-line
29569      update-stmt
29570
29571    # pragma omp atomic read new-line
29572      read-stmt
29573
29574    # pragma omp atomic write new-line
29575      write-stmt
29576
29577    # pragma omp atomic update new-line
29578      update-stmt
29579
29580    # pragma omp atomic capture new-line
29581      capture-stmt
29582
29583    # pragma omp atomic capture new-line
29584      capture-block
29585
29586    read-stmt:
29587      v = x
29588    write-stmt:
29589      x = expr
29590    update-stmt:
29591      expression-stmt | x = x binop expr
29592    capture-stmt:
29593      v = expression-stmt
29594    capture-block:
29595      { v = x; update-stmt; } | { update-stmt; v = x; }
29596
29597    OpenMP 4.0:
29598    update-stmt:
29599      expression-stmt | x = x binop expr | x = expr binop x
29600    capture-stmt:
29601      v = update-stmt
29602    capture-block:
29603      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29604
29605   where x and v are lvalue expressions with scalar type.  */
29606
29607 static void
29608 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29609 {
29610   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29611   tree rhs1 = NULL_TREE, orig_lhs;
29612   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29613   bool structured_block = false;
29614   bool seq_cst = false;
29615
29616   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29617     {
29618       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29619       const char *p = IDENTIFIER_POINTER (id);
29620
29621       if (!strcmp (p, "seq_cst"))
29622         {
29623           seq_cst = true;
29624           cp_lexer_consume_token (parser->lexer);
29625           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29626               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29627             cp_lexer_consume_token (parser->lexer);
29628         }
29629     }
29630   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29631     {
29632       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29633       const char *p = IDENTIFIER_POINTER (id);
29634
29635       if (!strcmp (p, "read"))
29636         code = OMP_ATOMIC_READ;
29637       else if (!strcmp (p, "write"))
29638         code = NOP_EXPR;
29639       else if (!strcmp (p, "update"))
29640         code = OMP_ATOMIC;
29641       else if (!strcmp (p, "capture"))
29642         code = OMP_ATOMIC_CAPTURE_NEW;
29643       else
29644         p = NULL;
29645       if (p)
29646         cp_lexer_consume_token (parser->lexer);
29647     }
29648   if (!seq_cst)
29649     {
29650       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29651           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29652         cp_lexer_consume_token (parser->lexer);
29653
29654       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29655         {
29656           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29657           const char *p = IDENTIFIER_POINTER (id);
29658
29659           if (!strcmp (p, "seq_cst"))
29660             {
29661               seq_cst = true;
29662               cp_lexer_consume_token (parser->lexer);
29663             }
29664         }
29665     }
29666   cp_parser_require_pragma_eol (parser, pragma_tok);
29667
29668   switch (code)
29669     {
29670     case OMP_ATOMIC_READ:
29671     case NOP_EXPR: /* atomic write */
29672       v = cp_parser_unary_expression (parser);
29673       if (v == error_mark_node)
29674         goto saw_error;
29675       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29676         goto saw_error;
29677       if (code == NOP_EXPR)
29678         lhs = cp_parser_expression (parser);
29679       else
29680         lhs = cp_parser_unary_expression (parser);
29681       if (lhs == error_mark_node)
29682         goto saw_error;
29683       if (code == NOP_EXPR)
29684         {
29685           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29686              opcode.  */
29687           code = OMP_ATOMIC;
29688           rhs = lhs;
29689           lhs = v;
29690           v = NULL_TREE;
29691         }
29692       goto done;
29693     case OMP_ATOMIC_CAPTURE_NEW:
29694       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29695         {
29696           cp_lexer_consume_token (parser->lexer);
29697           structured_block = true;
29698         }
29699       else
29700         {
29701           v = cp_parser_unary_expression (parser);
29702           if (v == error_mark_node)
29703             goto saw_error;
29704           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29705             goto saw_error;
29706         }
29707     default:
29708       break;
29709     }
29710
29711 restart:
29712   lhs = cp_parser_unary_expression (parser);
29713   orig_lhs = lhs;
29714   switch (TREE_CODE (lhs))
29715     {
29716     case ERROR_MARK:
29717       goto saw_error;
29718
29719     case POSTINCREMENT_EXPR:
29720       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29721         code = OMP_ATOMIC_CAPTURE_OLD;
29722       /* FALLTHROUGH */
29723     case PREINCREMENT_EXPR:
29724       lhs = TREE_OPERAND (lhs, 0);
29725       opcode = PLUS_EXPR;
29726       rhs = integer_one_node;
29727       break;
29728
29729     case POSTDECREMENT_EXPR:
29730       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29731         code = OMP_ATOMIC_CAPTURE_OLD;
29732       /* FALLTHROUGH */
29733     case PREDECREMENT_EXPR:
29734       lhs = TREE_OPERAND (lhs, 0);
29735       opcode = MINUS_EXPR;
29736       rhs = integer_one_node;
29737       break;
29738
29739     case COMPOUND_EXPR:
29740       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29741          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29742          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29743          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29744          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29745                                              (TREE_OPERAND (lhs, 1), 0), 0)))
29746             == BOOLEAN_TYPE)
29747        /* Undo effects of boolean_increment for post {in,de}crement.  */
29748        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29749       /* FALLTHRU */
29750     case MODIFY_EXPR:
29751       if (TREE_CODE (lhs) == MODIFY_EXPR
29752          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29753         {
29754           /* Undo effects of boolean_increment.  */
29755           if (integer_onep (TREE_OPERAND (lhs, 1)))
29756             {
29757               /* This is pre or post increment.  */
29758               rhs = TREE_OPERAND (lhs, 1);
29759               lhs = TREE_OPERAND (lhs, 0);
29760               opcode = NOP_EXPR;
29761               if (code == OMP_ATOMIC_CAPTURE_NEW
29762                   && !structured_block
29763                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29764                 code = OMP_ATOMIC_CAPTURE_OLD;
29765               break;
29766             }
29767         }
29768       /* FALLTHRU */
29769     default:
29770       switch (cp_lexer_peek_token (parser->lexer)->type)
29771         {
29772         case CPP_MULT_EQ:
29773           opcode = MULT_EXPR;
29774           break;
29775         case CPP_DIV_EQ:
29776           opcode = TRUNC_DIV_EXPR;
29777           break;
29778         case CPP_PLUS_EQ:
29779           opcode = PLUS_EXPR;
29780           break;
29781         case CPP_MINUS_EQ:
29782           opcode = MINUS_EXPR;
29783           break;
29784         case CPP_LSHIFT_EQ:
29785           opcode = LSHIFT_EXPR;
29786           break;
29787         case CPP_RSHIFT_EQ:
29788           opcode = RSHIFT_EXPR;
29789           break;
29790         case CPP_AND_EQ:
29791           opcode = BIT_AND_EXPR;
29792           break;
29793         case CPP_OR_EQ:
29794           opcode = BIT_IOR_EXPR;
29795           break;
29796         case CPP_XOR_EQ:
29797           opcode = BIT_XOR_EXPR;
29798           break;
29799         case CPP_EQ:
29800           enum cp_parser_prec oprec;
29801           cp_token *token;
29802           cp_lexer_consume_token (parser->lexer);
29803           cp_parser_parse_tentatively (parser);
29804           rhs1 = cp_parser_simple_cast_expression (parser);
29805           if (rhs1 == error_mark_node)
29806             {
29807               cp_parser_abort_tentative_parse (parser);
29808               cp_parser_simple_cast_expression (parser);
29809               goto saw_error;
29810             }
29811           token = cp_lexer_peek_token (parser->lexer);
29812           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29813             {
29814               cp_parser_abort_tentative_parse (parser);
29815               cp_parser_parse_tentatively (parser);
29816               rhs = cp_parser_binary_expression (parser, false, true,
29817                                                  PREC_NOT_OPERATOR, NULL);
29818               if (rhs == error_mark_node)
29819                 {
29820                   cp_parser_abort_tentative_parse (parser);
29821                   cp_parser_binary_expression (parser, false, true,
29822                                                PREC_NOT_OPERATOR, NULL);
29823                   goto saw_error;
29824                 }
29825               switch (TREE_CODE (rhs))
29826                 {
29827                 case MULT_EXPR:
29828                 case TRUNC_DIV_EXPR:
29829                 case RDIV_EXPR:
29830                 case PLUS_EXPR:
29831                 case MINUS_EXPR:
29832                 case LSHIFT_EXPR:
29833                 case RSHIFT_EXPR:
29834                 case BIT_AND_EXPR:
29835                 case BIT_IOR_EXPR:
29836                 case BIT_XOR_EXPR:
29837                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29838                     {
29839                       if (cp_parser_parse_definitely (parser))
29840                         {
29841                           opcode = TREE_CODE (rhs);
29842                           rhs1 = TREE_OPERAND (rhs, 0);
29843                           rhs = TREE_OPERAND (rhs, 1);
29844                           goto stmt_done;
29845                         }
29846                       else
29847                         goto saw_error;
29848                     }
29849                   break;
29850                 default:
29851                   break;
29852                 }
29853               cp_parser_abort_tentative_parse (parser);
29854               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29855                 {
29856                   rhs = cp_parser_expression (parser);
29857                   if (rhs == error_mark_node)
29858                     goto saw_error;
29859                   opcode = NOP_EXPR;
29860                   rhs1 = NULL_TREE;
29861                   goto stmt_done;
29862                 }
29863               cp_parser_error (parser,
29864                                "invalid form of %<#pragma omp atomic%>");
29865               goto saw_error;
29866             }
29867           if (!cp_parser_parse_definitely (parser))
29868             goto saw_error;
29869           switch (token->type)
29870             {
29871             case CPP_SEMICOLON:
29872               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29873                 {
29874                   code = OMP_ATOMIC_CAPTURE_OLD;
29875                   v = lhs;
29876                   lhs = NULL_TREE;
29877                   lhs1 = rhs1;
29878                   rhs1 = NULL_TREE;
29879                   cp_lexer_consume_token (parser->lexer);
29880                   goto restart;
29881                 }
29882               else if (structured_block)
29883                 {
29884                   opcode = NOP_EXPR;
29885                   rhs = rhs1;
29886                   rhs1 = NULL_TREE;
29887                   goto stmt_done;
29888                 }
29889               cp_parser_error (parser,
29890                                "invalid form of %<#pragma omp atomic%>");
29891               goto saw_error;
29892             case CPP_MULT:
29893               opcode = MULT_EXPR;
29894               break;
29895             case CPP_DIV:
29896               opcode = TRUNC_DIV_EXPR;
29897               break;
29898             case CPP_PLUS:
29899               opcode = PLUS_EXPR;
29900               break;
29901             case CPP_MINUS:
29902               opcode = MINUS_EXPR;
29903               break;
29904             case CPP_LSHIFT:
29905               opcode = LSHIFT_EXPR;
29906               break;
29907             case CPP_RSHIFT:
29908               opcode = RSHIFT_EXPR;
29909               break;
29910             case CPP_AND:
29911               opcode = BIT_AND_EXPR;
29912               break;
29913             case CPP_OR:
29914               opcode = BIT_IOR_EXPR;
29915               break;
29916             case CPP_XOR:
29917               opcode = BIT_XOR_EXPR;
29918               break;
29919             default:
29920               cp_parser_error (parser,
29921                                "invalid operator for %<#pragma omp atomic%>");
29922               goto saw_error;
29923             }
29924           oprec = TOKEN_PRECEDENCE (token);
29925           gcc_assert (oprec != PREC_NOT_OPERATOR);
29926           if (commutative_tree_code (opcode))
29927             oprec = (enum cp_parser_prec) (oprec - 1);
29928           cp_lexer_consume_token (parser->lexer);
29929           rhs = cp_parser_binary_expression (parser, false, false,
29930                                              oprec, NULL);
29931           if (rhs == error_mark_node)
29932             goto saw_error;
29933           goto stmt_done;
29934           /* FALLTHROUGH */
29935         default:
29936           cp_parser_error (parser,
29937                            "invalid operator for %<#pragma omp atomic%>");
29938           goto saw_error;
29939         }
29940       cp_lexer_consume_token (parser->lexer);
29941
29942       rhs = cp_parser_expression (parser);
29943       if (rhs == error_mark_node)
29944         goto saw_error;
29945       break;
29946     }
29947 stmt_done:
29948   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29949     {
29950       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29951         goto saw_error;
29952       v = cp_parser_unary_expression (parser);
29953       if (v == error_mark_node)
29954         goto saw_error;
29955       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29956         goto saw_error;
29957       lhs1 = cp_parser_unary_expression (parser);
29958       if (lhs1 == error_mark_node)
29959         goto saw_error;
29960     }
29961   if (structured_block)
29962     {
29963       cp_parser_consume_semicolon_at_end_of_statement (parser);
29964       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29965     }
29966 done:
29967   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29968   if (!structured_block)
29969     cp_parser_consume_semicolon_at_end_of_statement (parser);
29970   return;
29971
29972  saw_error:
29973   cp_parser_skip_to_end_of_block_or_statement (parser);
29974   if (structured_block)
29975     {
29976       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29977         cp_lexer_consume_token (parser->lexer);
29978       else if (code == OMP_ATOMIC_CAPTURE_NEW)
29979         {
29980           cp_parser_skip_to_end_of_block_or_statement (parser);
29981           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29982             cp_lexer_consume_token (parser->lexer);
29983         }
29984     }
29985 }
29986
29987
29988 /* OpenMP 2.5:
29989    # pragma omp barrier new-line  */
29990
29991 static void
29992 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29993 {
29994   cp_parser_require_pragma_eol (parser, pragma_tok);
29995   finish_omp_barrier ();
29996 }
29997
29998 /* OpenMP 2.5:
29999    # pragma omp critical [(name)] new-line
30000      structured-block  */
30001
30002 static tree
30003 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30004 {
30005   tree stmt, name = NULL;
30006
30007   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30008     {
30009       cp_lexer_consume_token (parser->lexer);
30010
30011       name = cp_parser_identifier (parser);
30012
30013       if (name == error_mark_node
30014           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30015         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30016                                                /*or_comma=*/false,
30017                                                /*consume_paren=*/true);
30018       if (name == error_mark_node)
30019         name = NULL;
30020     }
30021   cp_parser_require_pragma_eol (parser, pragma_tok);
30022
30023   stmt = cp_parser_omp_structured_block (parser);
30024   return c_finish_omp_critical (input_location, stmt, name);
30025 }
30026
30027 /* OpenMP 2.5:
30028    # pragma omp flush flush-vars[opt] new-line
30029
30030    flush-vars:
30031      ( variable-list ) */
30032
30033 static void
30034 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30035 {
30036   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30037     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30038   cp_parser_require_pragma_eol (parser, pragma_tok);
30039
30040   finish_omp_flush ();
30041 }
30042
30043 /* Helper function, to parse omp for increment expression.  */
30044
30045 static tree
30046 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30047 {
30048   tree cond = cp_parser_binary_expression (parser, false, true,
30049                                            PREC_NOT_OPERATOR, NULL);
30050   if (cond == error_mark_node
30051       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30052     {
30053       cp_parser_skip_to_end_of_statement (parser);
30054       return error_mark_node;
30055     }
30056
30057   switch (TREE_CODE (cond))
30058     {
30059     case GT_EXPR:
30060     case GE_EXPR:
30061     case LT_EXPR:
30062     case LE_EXPR:
30063       break;
30064     case NE_EXPR:
30065       if (code == CILK_SIMD || code == CILK_FOR)
30066         break;
30067       /* Fall through: OpenMP disallows NE_EXPR.  */
30068     default:
30069       return error_mark_node;
30070     }
30071
30072   /* If decl is an iterator, preserve LHS and RHS of the relational
30073      expr until finish_omp_for.  */
30074   if (decl
30075       && (type_dependent_expression_p (decl)
30076           || CLASS_TYPE_P (TREE_TYPE (decl))))
30077     return cond;
30078
30079   return build_x_binary_op (input_location, TREE_CODE (cond),
30080                             TREE_OPERAND (cond, 0), ERROR_MARK,
30081                             TREE_OPERAND (cond, 1), ERROR_MARK,
30082                             /*overload=*/NULL, tf_warning_or_error);
30083 }
30084
30085 /* Helper function, to parse omp for increment expression.  */
30086
30087 static tree
30088 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30089 {
30090   cp_token *token = cp_lexer_peek_token (parser->lexer);
30091   enum tree_code op;
30092   tree lhs, rhs;
30093   cp_id_kind idk;
30094   bool decl_first;
30095
30096   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30097     {
30098       op = (token->type == CPP_PLUS_PLUS
30099             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30100       cp_lexer_consume_token (parser->lexer);
30101       lhs = cp_parser_simple_cast_expression (parser);
30102       if (lhs != decl)
30103         return error_mark_node;
30104       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30105     }
30106
30107   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30108   if (lhs != decl)
30109     return error_mark_node;
30110
30111   token = cp_lexer_peek_token (parser->lexer);
30112   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30113     {
30114       op = (token->type == CPP_PLUS_PLUS
30115             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30116       cp_lexer_consume_token (parser->lexer);
30117       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30118     }
30119
30120   op = cp_parser_assignment_operator_opt (parser);
30121   if (op == ERROR_MARK)
30122     return error_mark_node;
30123
30124   if (op != NOP_EXPR)
30125     {
30126       rhs = cp_parser_assignment_expression (parser);
30127       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30128       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30129     }
30130
30131   lhs = cp_parser_binary_expression (parser, false, false,
30132                                      PREC_ADDITIVE_EXPRESSION, NULL);
30133   token = cp_lexer_peek_token (parser->lexer);
30134   decl_first = lhs == decl;
30135   if (decl_first)
30136     lhs = NULL_TREE;
30137   if (token->type != CPP_PLUS
30138       && token->type != CPP_MINUS)
30139     return error_mark_node;
30140
30141   do
30142     {
30143       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30144       cp_lexer_consume_token (parser->lexer);
30145       rhs = cp_parser_binary_expression (parser, false, false,
30146                                          PREC_ADDITIVE_EXPRESSION, NULL);
30147       token = cp_lexer_peek_token (parser->lexer);
30148       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30149         {
30150           if (lhs == NULL_TREE)
30151             {
30152               if (op == PLUS_EXPR)
30153                 lhs = rhs;
30154               else
30155                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30156                                         tf_warning_or_error);
30157             }
30158           else
30159             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30160                                      ERROR_MARK, NULL, tf_warning_or_error);
30161         }
30162     }
30163   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30164
30165   if (!decl_first)
30166     {
30167       if (rhs != decl || op == MINUS_EXPR)
30168         return error_mark_node;
30169       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30170     }
30171   else
30172     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30173
30174   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30175 }
30176
30177 /* Parse the initialization statement of either an OpenMP for loop or
30178    a Cilk Plus for loop.
30179
30180    Return true if the resulting construct should have an
30181    OMP_CLAUSE_PRIVATE added to it.  */
30182
30183 static bool
30184 cp_parser_omp_for_loop_init (cp_parser *parser,
30185                              enum tree_code code,
30186                              tree &this_pre_body,
30187                              vec<tree, va_gc> *for_block,
30188                              tree &init,
30189                              tree &decl,
30190                              tree &real_decl)
30191 {
30192   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30193     return false;
30194
30195   bool add_private_clause = false;
30196
30197   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30198
30199      init-expr:
30200      var = lb
30201      integer-type var = lb
30202      random-access-iterator-type var = lb
30203      pointer-type var = lb
30204   */
30205   cp_decl_specifier_seq type_specifiers;
30206
30207   /* First, try to parse as an initialized declaration.  See
30208      cp_parser_condition, from whence the bulk of this is copied.  */
30209
30210   cp_parser_parse_tentatively (parser);
30211   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30212                                 /*is_trailing_return=*/false,
30213                                 &type_specifiers);
30214   if (cp_parser_parse_definitely (parser))
30215     {
30216       /* If parsing a type specifier seq succeeded, then this
30217          MUST be a initialized declaration.  */
30218       tree asm_specification, attributes;
30219       cp_declarator *declarator;
30220
30221       declarator = cp_parser_declarator (parser,
30222                                          CP_PARSER_DECLARATOR_NAMED,
30223                                          /*ctor_dtor_or_conv_p=*/NULL,
30224                                          /*parenthesized_p=*/NULL,
30225                                          /*member_p=*/false,
30226                                          /*friend_p=*/false);
30227       attributes = cp_parser_attributes_opt (parser);
30228       asm_specification = cp_parser_asm_specification_opt (parser);
30229
30230       if (declarator == cp_error_declarator) 
30231         cp_parser_skip_to_end_of_statement (parser);
30232
30233       else 
30234         {
30235           tree pushed_scope, auto_node;
30236
30237           decl = start_decl (declarator, &type_specifiers,
30238                              SD_INITIALIZED, attributes,
30239                              /*prefix_attributes=*/NULL_TREE,
30240                              &pushed_scope);
30241
30242           auto_node = type_uses_auto (TREE_TYPE (decl));
30243           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30244             {
30245               if (cp_lexer_next_token_is (parser->lexer, 
30246                                           CPP_OPEN_PAREN))
30247                 {
30248                   if (code != CILK_SIMD && code != CILK_FOR)
30249                     error ("parenthesized initialization is not allowed in "
30250                            "OpenMP %<for%> loop");
30251                   else
30252                     error ("parenthesized initialization is "
30253                            "not allowed in for-loop");
30254                 }
30255               else
30256                 /* Trigger an error.  */
30257                 cp_parser_require (parser, CPP_EQ, RT_EQ);
30258
30259               init = error_mark_node;
30260               cp_parser_skip_to_end_of_statement (parser);
30261             }
30262           else if (CLASS_TYPE_P (TREE_TYPE (decl))
30263                    || type_dependent_expression_p (decl)
30264                    || auto_node)
30265             {
30266               bool is_direct_init, is_non_constant_init;
30267
30268               init = cp_parser_initializer (parser,
30269                                             &is_direct_init,
30270                                             &is_non_constant_init);
30271
30272               if (auto_node)
30273                 {
30274                   TREE_TYPE (decl)
30275                     = do_auto_deduction (TREE_TYPE (decl), init,
30276                                          auto_node);
30277
30278                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
30279                       && !type_dependent_expression_p (decl))
30280                     goto non_class;
30281                 }
30282                       
30283               cp_finish_decl (decl, init, !is_non_constant_init,
30284                               asm_specification,
30285                               LOOKUP_ONLYCONVERTING);
30286               if (CLASS_TYPE_P (TREE_TYPE (decl)))
30287                 {
30288                   vec_safe_push (for_block, this_pre_body);
30289                   init = NULL_TREE;
30290                 }
30291               else
30292                 init = pop_stmt_list (this_pre_body);
30293               this_pre_body = NULL_TREE;
30294             }
30295           else
30296             {
30297               /* Consume '='.  */
30298               cp_lexer_consume_token (parser->lexer);
30299               init = cp_parser_assignment_expression (parser);
30300
30301             non_class:
30302               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30303                 init = error_mark_node;
30304               else
30305                 cp_finish_decl (decl, NULL_TREE,
30306                                 /*init_const_expr_p=*/false,
30307                                 asm_specification,
30308                                 LOOKUP_ONLYCONVERTING);
30309             }
30310
30311           if (pushed_scope)
30312             pop_scope (pushed_scope);
30313         }
30314     }
30315   else 
30316     {
30317       cp_id_kind idk;
30318       /* If parsing a type specifier sequence failed, then
30319          this MUST be a simple expression.  */
30320       if (code == CILK_FOR)
30321         error ("%<_Cilk_for%> allows expression instead of declaration only "
30322                "in C, not in C++");
30323       cp_parser_parse_tentatively (parser);
30324       decl = cp_parser_primary_expression (parser, false, false,
30325                                            false, &idk);
30326       if (!cp_parser_error_occurred (parser)
30327           && decl
30328           && DECL_P (decl)
30329           && CLASS_TYPE_P (TREE_TYPE (decl)))
30330         {
30331           tree rhs;
30332
30333           cp_parser_parse_definitely (parser);
30334           cp_parser_require (parser, CPP_EQ, RT_EQ);
30335           rhs = cp_parser_assignment_expression (parser);
30336           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30337                                                  decl, NOP_EXPR,
30338                                                  rhs,
30339                                                  tf_warning_or_error));
30340           add_private_clause = true;
30341         }
30342       else
30343         {
30344           decl = NULL;
30345           cp_parser_abort_tentative_parse (parser);
30346           init = cp_parser_expression (parser);
30347           if (init)
30348             {
30349               if (TREE_CODE (init) == MODIFY_EXPR
30350                   || TREE_CODE (init) == MODOP_EXPR)
30351                 real_decl = TREE_OPERAND (init, 0);
30352             }
30353         }
30354     }
30355   return add_private_clause;
30356 }
30357
30358 /* Parse the restricted form of the for statement allowed by OpenMP.  */
30359
30360 static tree
30361 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30362                         tree *cclauses)
30363 {
30364   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30365   tree real_decl, initv, condv, incrv, declv;
30366   tree this_pre_body, cl;
30367   location_t loc_first;
30368   bool collapse_err = false;
30369   int i, collapse = 1, nbraces = 0;
30370   vec<tree, va_gc> *for_block = make_tree_vector ();
30371
30372   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30373     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30374       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30375
30376   gcc_assert (collapse >= 1);
30377
30378   declv = make_tree_vec (collapse);
30379   initv = make_tree_vec (collapse);
30380   condv = make_tree_vec (collapse);
30381   incrv = make_tree_vec (collapse);
30382
30383   loc_first = cp_lexer_peek_token (parser->lexer)->location;
30384
30385   for (i = 0; i < collapse; i++)
30386     {
30387       int bracecount = 0;
30388       bool add_private_clause = false;
30389       location_t loc;
30390
30391       if (code != CILK_FOR
30392           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30393         {
30394           cp_parser_error (parser, "for statement expected");
30395           return NULL;
30396         }
30397       if (code == CILK_FOR
30398           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30399         {
30400           cp_parser_error (parser, "_Cilk_for statement expected");
30401           return NULL;
30402         }
30403       loc = cp_lexer_consume_token (parser->lexer)->location;
30404
30405       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30406         return NULL;
30407
30408       init = decl = real_decl = NULL;
30409       this_pre_body = push_stmt_list ();
30410
30411       add_private_clause
30412         |= cp_parser_omp_for_loop_init (parser, code,
30413                                         this_pre_body, for_block,
30414                                         init, decl, real_decl);
30415
30416       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30417       if (this_pre_body)
30418         {
30419           this_pre_body = pop_stmt_list (this_pre_body);
30420           if (pre_body)
30421             {
30422               tree t = pre_body;
30423               pre_body = push_stmt_list ();
30424               add_stmt (t);
30425               add_stmt (this_pre_body);
30426               pre_body = pop_stmt_list (pre_body);
30427             }
30428           else
30429             pre_body = this_pre_body;
30430         }
30431
30432       if (decl)
30433         real_decl = decl;
30434       if (cclauses != NULL
30435           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30436           && real_decl != NULL_TREE)
30437         {
30438           tree *c;
30439           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30440             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30441                 && OMP_CLAUSE_DECL (*c) == real_decl)
30442               {
30443                 error_at (loc, "iteration variable %qD"
30444                           " should not be firstprivate", real_decl);
30445                 *c = OMP_CLAUSE_CHAIN (*c);
30446               }
30447             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30448                      && OMP_CLAUSE_DECL (*c) == real_decl)
30449               {
30450                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
30451                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
30452                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
30453                 OMP_CLAUSE_DECL (l) = real_decl;
30454                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
30455                 if (code == OMP_SIMD)
30456                   {
30457                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30458                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30459                   }
30460                 else
30461                   {
30462                     OMP_CLAUSE_CHAIN (l) = clauses;
30463                     clauses = l;
30464                   }
30465                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
30466                 CP_OMP_CLAUSE_INFO (*c) = NULL;
30467                 add_private_clause = false;
30468               }
30469             else
30470               {
30471                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30472                     && OMP_CLAUSE_DECL (*c) == real_decl)
30473                   add_private_clause = false;
30474                 c = &OMP_CLAUSE_CHAIN (*c);
30475               }
30476         }
30477
30478       if (add_private_clause)
30479         {
30480           tree c;
30481           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30482             {
30483               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30484                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30485                   && OMP_CLAUSE_DECL (c) == decl)
30486                 break;
30487               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30488                        && OMP_CLAUSE_DECL (c) == decl)
30489                 error_at (loc, "iteration variable %qD "
30490                           "should not be firstprivate",
30491                           decl);
30492               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30493                        && OMP_CLAUSE_DECL (c) == decl)
30494                 error_at (loc, "iteration variable %qD should not be reduction",
30495                           decl);
30496             }
30497           if (c == NULL)
30498             {
30499               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30500               OMP_CLAUSE_DECL (c) = decl;
30501               c = finish_omp_clauses (c);
30502               if (c)
30503                 {
30504                   OMP_CLAUSE_CHAIN (c) = clauses;
30505                   clauses = c;
30506                 }
30507             }
30508         }
30509
30510       cond = NULL;
30511       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30512         cond = cp_parser_omp_for_cond (parser, decl, code);
30513       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30514
30515       incr = NULL;
30516       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30517         {
30518           /* If decl is an iterator, preserve the operator on decl
30519              until finish_omp_for.  */
30520           if (real_decl
30521               && ((processing_template_decl
30522                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30523                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30524             incr = cp_parser_omp_for_incr (parser, real_decl);
30525           else
30526             incr = cp_parser_expression (parser);
30527           if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30528             SET_EXPR_LOCATION (incr, input_location);
30529         }
30530
30531       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30532         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30533                                                /*or_comma=*/false,
30534                                                /*consume_paren=*/true);
30535
30536       TREE_VEC_ELT (declv, i) = decl;
30537       TREE_VEC_ELT (initv, i) = init;
30538       TREE_VEC_ELT (condv, i) = cond;
30539       TREE_VEC_ELT (incrv, i) = incr;
30540
30541       if (i == collapse - 1)
30542         break;
30543
30544       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30545          in between the collapsed for loops to be still considered perfectly
30546          nested.  Hopefully the final version clarifies this.
30547          For now handle (multiple) {'s and empty statements.  */
30548       cp_parser_parse_tentatively (parser);
30549       do
30550         {
30551           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30552             break;
30553           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30554             {
30555               cp_lexer_consume_token (parser->lexer);
30556               bracecount++;
30557             }
30558           else if (bracecount
30559                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30560             cp_lexer_consume_token (parser->lexer);
30561           else
30562             {
30563               loc = cp_lexer_peek_token (parser->lexer)->location;
30564               error_at (loc, "not enough collapsed for loops");
30565               collapse_err = true;
30566               cp_parser_abort_tentative_parse (parser);
30567               declv = NULL_TREE;
30568               break;
30569             }
30570         }
30571       while (1);
30572
30573       if (declv)
30574         {
30575           cp_parser_parse_definitely (parser);
30576           nbraces += bracecount;
30577         }
30578     }
30579
30580   /* Note that we saved the original contents of this flag when we entered
30581      the structured block, and so we don't need to re-save it here.  */
30582   if (code == CILK_SIMD || code == CILK_FOR)
30583     parser->in_statement = IN_CILK_SIMD_FOR;
30584   else
30585     parser->in_statement = IN_OMP_FOR;
30586
30587   /* Note that the grammar doesn't call for a structured block here,
30588      though the loop as a whole is a structured block.  */
30589   body = push_stmt_list ();
30590   cp_parser_statement (parser, NULL_TREE, false, NULL);
30591   body = pop_stmt_list (body);
30592
30593   if (declv == NULL_TREE)
30594     ret = NULL_TREE;
30595   else
30596     ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30597                           pre_body, clauses);
30598
30599   while (nbraces)
30600     {
30601       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30602         {
30603           cp_lexer_consume_token (parser->lexer);
30604           nbraces--;
30605         }
30606       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30607         cp_lexer_consume_token (parser->lexer);
30608       else
30609         {
30610           if (!collapse_err)
30611             {
30612               error_at (cp_lexer_peek_token (parser->lexer)->location,
30613                         "collapsed loops not perfectly nested");
30614             }
30615           collapse_err = true;
30616           cp_parser_statement_seq_opt (parser, NULL);
30617           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30618             break;
30619         }
30620     }
30621
30622   while (!for_block->is_empty ())
30623     add_stmt (pop_stmt_list (for_block->pop ()));
30624   release_tree_vector (for_block);
30625
30626   return ret;
30627 }
30628
30629 /* Helper function for OpenMP parsing, split clauses and call
30630    finish_omp_clauses on each of the set of clauses afterwards.  */
30631
30632 static void
30633 cp_omp_split_clauses (location_t loc, enum tree_code code,
30634                       omp_clause_mask mask, tree clauses, tree *cclauses)
30635 {
30636   int i;
30637   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30638   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30639     if (cclauses[i])
30640       cclauses[i] = finish_omp_clauses (cclauses[i]);
30641 }
30642
30643 /* OpenMP 4.0:
30644    #pragma omp simd simd-clause[optseq] new-line
30645      for-loop  */
30646
30647 #define OMP_SIMD_CLAUSE_MASK                                    \
30648         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
30649         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
30650         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
30651         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30652         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30653         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30654         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30655
30656 static tree
30657 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30658                     char *p_name, omp_clause_mask mask, tree *cclauses)
30659 {
30660   tree clauses, sb, ret;
30661   unsigned int save;
30662   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30663
30664   strcat (p_name, " simd");
30665   mask |= OMP_SIMD_CLAUSE_MASK;
30666   mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30667
30668   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30669                                        cclauses == NULL);
30670   if (cclauses)
30671     {
30672       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30673       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30674     }
30675
30676   sb = begin_omp_structured_block ();
30677   save = cp_parser_begin_omp_structured_block (parser);
30678
30679   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30680
30681   cp_parser_end_omp_structured_block (parser, save);
30682   add_stmt (finish_omp_structured_block (sb));
30683
30684   return ret;
30685 }
30686
30687 /* OpenMP 2.5:
30688    #pragma omp for for-clause[optseq] new-line
30689      for-loop
30690
30691    OpenMP 4.0:
30692    #pragma omp for simd for-simd-clause[optseq] new-line
30693      for-loop  */
30694
30695 #define OMP_FOR_CLAUSE_MASK                                     \
30696         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30697         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30698         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30699         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30700         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
30701         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
30702         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
30703         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30704
30705 static tree
30706 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30707                    char *p_name, omp_clause_mask mask, tree *cclauses)
30708 {
30709   tree clauses, sb, ret;
30710   unsigned int save;
30711   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30712
30713   strcat (p_name, " for");
30714   mask |= OMP_FOR_CLAUSE_MASK;
30715   if (cclauses)
30716     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30717
30718   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30719     {
30720       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30721       const char *p = IDENTIFIER_POINTER (id);
30722
30723       if (strcmp (p, "simd") == 0)
30724         {
30725           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30726           if (cclauses == NULL)
30727             cclauses = cclauses_buf;
30728
30729           cp_lexer_consume_token (parser->lexer);
30730           if (!flag_openmp)  /* flag_openmp_simd  */
30731             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30732                                        cclauses);
30733           sb = begin_omp_structured_block ();
30734           save = cp_parser_begin_omp_structured_block (parser);
30735           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30736                                     cclauses);
30737           cp_parser_end_omp_structured_block (parser, save);
30738           tree body = finish_omp_structured_block (sb);
30739           if (ret == NULL)
30740             return ret;
30741           ret = make_node (OMP_FOR);
30742           TREE_TYPE (ret) = void_type_node;
30743           OMP_FOR_BODY (ret) = body;
30744           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30745           SET_EXPR_LOCATION (ret, loc);
30746           add_stmt (ret);
30747           return ret;
30748         }
30749     }
30750   if (!flag_openmp)  /* flag_openmp_simd  */
30751     {
30752       cp_parser_require_pragma_eol (parser, pragma_tok);
30753       return NULL_TREE;
30754     }
30755
30756   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30757                                        cclauses == NULL);
30758   if (cclauses)
30759     {
30760       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30761       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30762     }
30763
30764   sb = begin_omp_structured_block ();
30765   save = cp_parser_begin_omp_structured_block (parser);
30766
30767   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30768
30769   cp_parser_end_omp_structured_block (parser, save);
30770   add_stmt (finish_omp_structured_block (sb));
30771
30772   return ret;
30773 }
30774
30775 /* OpenMP 2.5:
30776    # pragma omp master new-line
30777      structured-block  */
30778
30779 static tree
30780 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30781 {
30782   cp_parser_require_pragma_eol (parser, pragma_tok);
30783   return c_finish_omp_master (input_location,
30784                               cp_parser_omp_structured_block (parser));
30785 }
30786
30787 /* OpenMP 2.5:
30788    # pragma omp ordered new-line
30789      structured-block  */
30790
30791 static tree
30792 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30793 {
30794   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30795   cp_parser_require_pragma_eol (parser, pragma_tok);
30796   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30797 }
30798
30799 /* OpenMP 2.5:
30800
30801    section-scope:
30802      { section-sequence }
30803
30804    section-sequence:
30805      section-directive[opt] structured-block
30806      section-sequence section-directive structured-block  */
30807
30808 static tree
30809 cp_parser_omp_sections_scope (cp_parser *parser)
30810 {
30811   tree stmt, substmt;
30812   bool error_suppress = false;
30813   cp_token *tok;
30814
30815   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30816     return NULL_TREE;
30817
30818   stmt = push_stmt_list ();
30819
30820   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30821     {
30822       substmt = cp_parser_omp_structured_block (parser);
30823       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30824       add_stmt (substmt);
30825     }
30826
30827   while (1)
30828     {
30829       tok = cp_lexer_peek_token (parser->lexer);
30830       if (tok->type == CPP_CLOSE_BRACE)
30831         break;
30832       if (tok->type == CPP_EOF)
30833         break;
30834
30835       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30836         {
30837           cp_lexer_consume_token (parser->lexer);
30838           cp_parser_require_pragma_eol (parser, tok);
30839           error_suppress = false;
30840         }
30841       else if (!error_suppress)
30842         {
30843           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30844           error_suppress = true;
30845         }
30846
30847       substmt = cp_parser_omp_structured_block (parser);
30848       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30849       add_stmt (substmt);
30850     }
30851   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30852
30853   substmt = pop_stmt_list (stmt);
30854
30855   stmt = make_node (OMP_SECTIONS);
30856   TREE_TYPE (stmt) = void_type_node;
30857   OMP_SECTIONS_BODY (stmt) = substmt;
30858
30859   add_stmt (stmt);
30860   return stmt;
30861 }
30862
30863 /* OpenMP 2.5:
30864    # pragma omp sections sections-clause[optseq] newline
30865      sections-scope  */
30866
30867 #define OMP_SECTIONS_CLAUSE_MASK                                \
30868         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30869         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30870         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30871         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30872         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30873
30874 static tree
30875 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30876                         char *p_name, omp_clause_mask mask, tree *cclauses)
30877 {
30878   tree clauses, ret;
30879   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30880
30881   strcat (p_name, " sections");
30882   mask |= OMP_SECTIONS_CLAUSE_MASK;
30883   if (cclauses)
30884     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30885
30886   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30887                                        cclauses == NULL);
30888   if (cclauses)
30889     {
30890       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30891       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30892     }
30893
30894   ret = cp_parser_omp_sections_scope (parser);
30895   if (ret)
30896     OMP_SECTIONS_CLAUSES (ret) = clauses;
30897
30898   return ret;
30899 }
30900
30901 /* OpenMP 2.5:
30902    # pragma omp parallel parallel-clause[optseq] new-line
30903      structured-block
30904    # pragma omp parallel for parallel-for-clause[optseq] new-line
30905      structured-block
30906    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30907      structured-block
30908
30909    OpenMP 4.0:
30910    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30911      structured-block */
30912
30913 #define OMP_PARALLEL_CLAUSE_MASK                                \
30914         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
30915         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30916         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30917         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
30918         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
30919         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
30920         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30921         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
30922         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30923
30924 static tree
30925 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30926                         char *p_name, omp_clause_mask mask, tree *cclauses)
30927 {
30928   tree stmt, clauses, block;
30929   unsigned int save;
30930   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30931
30932   strcat (p_name, " parallel");
30933   mask |= OMP_PARALLEL_CLAUSE_MASK;
30934
30935   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30936     {
30937       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30938       if (cclauses == NULL)
30939         cclauses = cclauses_buf;
30940
30941       cp_lexer_consume_token (parser->lexer);
30942       if (!flag_openmp)  /* flag_openmp_simd  */
30943         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30944       block = begin_omp_parallel ();
30945       save = cp_parser_begin_omp_structured_block (parser);
30946       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30947       cp_parser_end_omp_structured_block (parser, save);
30948       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30949                                   block);
30950       if (ret == NULL_TREE)
30951         return ret;
30952       OMP_PARALLEL_COMBINED (stmt) = 1;
30953       return stmt;
30954     }
30955   else if (cclauses)
30956     {
30957       error_at (loc, "expected %<for%> after %qs", p_name);
30958       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30959       return NULL_TREE;
30960     }
30961   else if (!flag_openmp)  /* flag_openmp_simd  */
30962     {
30963       cp_parser_require_pragma_eol (parser, pragma_tok);
30964       return NULL_TREE;
30965     }
30966   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30967     {
30968       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30969       const char *p = IDENTIFIER_POINTER (id);
30970       if (strcmp (p, "sections") == 0)
30971         {
30972           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30973           cclauses = cclauses_buf;
30974
30975           cp_lexer_consume_token (parser->lexer);
30976           block = begin_omp_parallel ();
30977           save = cp_parser_begin_omp_structured_block (parser);
30978           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30979           cp_parser_end_omp_structured_block (parser, save);
30980           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30981                                       block);
30982           OMP_PARALLEL_COMBINED (stmt) = 1;
30983           return stmt;
30984         }
30985     }
30986
30987   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30988
30989   block = begin_omp_parallel ();
30990   save = cp_parser_begin_omp_structured_block (parser);
30991   cp_parser_statement (parser, NULL_TREE, false, NULL);
30992   cp_parser_end_omp_structured_block (parser, save);
30993   stmt = finish_omp_parallel (clauses, block);
30994   return stmt;
30995 }
30996
30997 /* OpenMP 2.5:
30998    # pragma omp single single-clause[optseq] new-line
30999      structured-block  */
31000
31001 #define OMP_SINGLE_CLAUSE_MASK                                  \
31002         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31003         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31004         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
31005         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31006
31007 static tree
31008 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31009 {
31010   tree stmt = make_node (OMP_SINGLE);
31011   TREE_TYPE (stmt) = void_type_node;
31012
31013   OMP_SINGLE_CLAUSES (stmt)
31014     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31015                                  "#pragma omp single", pragma_tok);
31016   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31017
31018   return add_stmt (stmt);
31019 }
31020
31021 /* OpenMP 3.0:
31022    # pragma omp task task-clause[optseq] new-line
31023      structured-block  */
31024
31025 #define OMP_TASK_CLAUSE_MASK                                    \
31026         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
31027         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
31028         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
31029         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31030         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31031         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31032         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
31033         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
31034         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31035
31036 static tree
31037 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31038 {
31039   tree clauses, block;
31040   unsigned int save;
31041
31042   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31043                                        "#pragma omp task", pragma_tok);
31044   block = begin_omp_task ();
31045   save = cp_parser_begin_omp_structured_block (parser);
31046   cp_parser_statement (parser, NULL_TREE, false, NULL);
31047   cp_parser_end_omp_structured_block (parser, save);
31048   return finish_omp_task (clauses, block);
31049 }
31050
31051 /* OpenMP 3.0:
31052    # pragma omp taskwait new-line  */
31053
31054 static void
31055 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31056 {
31057   cp_parser_require_pragma_eol (parser, pragma_tok);
31058   finish_omp_taskwait ();
31059 }
31060
31061 /* OpenMP 3.1:
31062    # pragma omp taskyield new-line  */
31063
31064 static void
31065 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31066 {
31067   cp_parser_require_pragma_eol (parser, pragma_tok);
31068   finish_omp_taskyield ();
31069 }
31070
31071 /* OpenMP 4.0:
31072    # pragma omp taskgroup new-line
31073      structured-block  */
31074
31075 static tree
31076 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31077 {
31078   cp_parser_require_pragma_eol (parser, pragma_tok);
31079   return c_finish_omp_taskgroup (input_location,
31080                                  cp_parser_omp_structured_block (parser));
31081 }
31082
31083
31084 /* OpenMP 2.5:
31085    # pragma omp threadprivate (variable-list) */
31086
31087 static void
31088 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31089 {
31090   tree vars;
31091
31092   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31093   cp_parser_require_pragma_eol (parser, pragma_tok);
31094
31095   finish_omp_threadprivate (vars);
31096 }
31097
31098 /* OpenMP 4.0:
31099    # pragma omp cancel cancel-clause[optseq] new-line  */
31100
31101 #define OMP_CANCEL_CLAUSE_MASK                                  \
31102         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31103         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31104         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31105         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
31106         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31107
31108 static void
31109 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31110 {
31111   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31112                                             "#pragma omp cancel", pragma_tok);
31113   finish_omp_cancel (clauses);
31114 }
31115
31116 /* OpenMP 4.0:
31117    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
31118
31119 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
31120         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31121         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31122         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31123         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31124
31125 static void
31126 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31127 {
31128   tree clauses;
31129   bool point_seen = false;
31130
31131   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31132     {
31133       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31134       const char *p = IDENTIFIER_POINTER (id);
31135
31136       if (strcmp (p, "point") == 0)
31137         {
31138           cp_lexer_consume_token (parser->lexer);
31139           point_seen = true;
31140         }
31141     }
31142   if (!point_seen)
31143     {
31144       cp_parser_error (parser, "expected %<point%>");
31145       cp_parser_require_pragma_eol (parser, pragma_tok);
31146       return;
31147     }
31148
31149   clauses = cp_parser_omp_all_clauses (parser,
31150                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
31151                                        "#pragma omp cancellation point",
31152                                        pragma_tok);
31153   finish_omp_cancellation_point (clauses);
31154 }
31155
31156 /* OpenMP 4.0:
31157    #pragma omp distribute distribute-clause[optseq] new-line
31158      for-loop  */
31159
31160 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
31161         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31162         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31163         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31164         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31165
31166 static tree
31167 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31168                           char *p_name, omp_clause_mask mask, tree *cclauses)
31169 {
31170   tree clauses, sb, ret;
31171   unsigned int save;
31172   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31173
31174   strcat (p_name, " distribute");
31175   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31176
31177   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31178     {
31179       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31180       const char *p = IDENTIFIER_POINTER (id);
31181       bool simd = false;
31182       bool parallel = false;
31183
31184       if (strcmp (p, "simd") == 0)
31185         simd = true;
31186       else
31187         parallel = strcmp (p, "parallel") == 0;
31188       if (parallel || simd)
31189         {
31190           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31191           if (cclauses == NULL)
31192             cclauses = cclauses_buf;
31193           cp_lexer_consume_token (parser->lexer);
31194           if (!flag_openmp)  /* flag_openmp_simd  */
31195             {
31196               if (simd)
31197                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31198                                            cclauses);
31199               else
31200                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31201                                                cclauses);
31202             }
31203           sb = begin_omp_structured_block ();
31204           save = cp_parser_begin_omp_structured_block (parser);
31205           if (simd)
31206             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31207                                       cclauses);
31208           else
31209             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31210                                           cclauses);
31211           cp_parser_end_omp_structured_block (parser, save);
31212           tree body = finish_omp_structured_block (sb);
31213           if (ret == NULL)
31214             return ret;
31215           ret = make_node (OMP_DISTRIBUTE);
31216           TREE_TYPE (ret) = void_type_node;
31217           OMP_FOR_BODY (ret) = body;
31218           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31219           SET_EXPR_LOCATION (ret, loc);
31220           add_stmt (ret);
31221           return ret;
31222         }
31223     }
31224   if (!flag_openmp)  /* flag_openmp_simd  */
31225     {
31226       cp_parser_require_pragma_eol (parser, pragma_tok);
31227       return NULL_TREE;
31228     }
31229
31230   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31231                                        cclauses == NULL);
31232   if (cclauses)
31233     {
31234       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31235       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31236     }
31237
31238   sb = begin_omp_structured_block ();
31239   save = cp_parser_begin_omp_structured_block (parser);
31240
31241   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31242
31243   cp_parser_end_omp_structured_block (parser, save);
31244   add_stmt (finish_omp_structured_block (sb));
31245
31246   return ret;
31247 }
31248
31249 /* OpenMP 4.0:
31250    # pragma omp teams teams-clause[optseq] new-line
31251      structured-block  */
31252
31253 #define OMP_TEAMS_CLAUSE_MASK                                   \
31254         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31255         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31256         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31257         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
31258         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
31259         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31260         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31261
31262 static tree
31263 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31264                      char *p_name, omp_clause_mask mask, tree *cclauses)
31265 {
31266   tree clauses, sb, ret;
31267   unsigned int save;
31268   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31269
31270   strcat (p_name, " teams");
31271   mask |= OMP_TEAMS_CLAUSE_MASK;
31272
31273   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31274     {
31275       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31276       const char *p = IDENTIFIER_POINTER (id);
31277       if (strcmp (p, "distribute") == 0)
31278         {
31279           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31280           if (cclauses == NULL)
31281             cclauses = cclauses_buf;
31282
31283           cp_lexer_consume_token (parser->lexer);
31284           if (!flag_openmp)  /* flag_openmp_simd  */
31285             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31286                                              cclauses);
31287           sb = begin_omp_structured_block ();
31288           save = cp_parser_begin_omp_structured_block (parser);
31289           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31290                                           cclauses);
31291           cp_parser_end_omp_structured_block (parser, save);
31292           tree body = finish_omp_structured_block (sb);
31293           if (ret == NULL)
31294             return ret;
31295           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31296           ret = make_node (OMP_TEAMS);
31297           TREE_TYPE (ret) = void_type_node;
31298           OMP_TEAMS_CLAUSES (ret) = clauses;
31299           OMP_TEAMS_BODY (ret) = body;
31300           return add_stmt (ret);
31301         }
31302     }
31303   if (!flag_openmp)  /* flag_openmp_simd  */
31304     {
31305       cp_parser_require_pragma_eol (parser, pragma_tok);
31306       return NULL_TREE;
31307     }
31308
31309   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31310                                        cclauses == NULL);
31311   if (cclauses)
31312     {
31313       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31314       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31315     }
31316
31317   tree stmt = make_node (OMP_TEAMS);
31318   TREE_TYPE (stmt) = void_type_node;
31319   OMP_TEAMS_CLAUSES (stmt) = clauses;
31320   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31321
31322   return add_stmt (stmt);
31323 }
31324
31325 /* OpenMP 4.0:
31326    # pragma omp target data target-data-clause[optseq] new-line
31327      structured-block  */
31328
31329 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
31330         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31331         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31332         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31333
31334 static tree
31335 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31336 {
31337   tree stmt = make_node (OMP_TARGET_DATA);
31338   TREE_TYPE (stmt) = void_type_node;
31339
31340   OMP_TARGET_DATA_CLAUSES (stmt)
31341     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31342                                  "#pragma omp target data", pragma_tok);
31343   keep_next_level (true);
31344   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31345
31346   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31347   return add_stmt (stmt);
31348 }
31349
31350 /* OpenMP 4.0:
31351    # pragma omp target update target-update-clause[optseq] new-line */
31352
31353 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
31354         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
31355         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
31356         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31357         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31358
31359 static bool
31360 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31361                              enum pragma_context context)
31362 {
31363   if (context == pragma_stmt)
31364     {
31365       error_at (pragma_tok->location,
31366                 "%<#pragma omp target update%> may only be "
31367                 "used in compound statements");
31368       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31369       return false;
31370     }
31371
31372   tree clauses
31373     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31374                                  "#pragma omp target update", pragma_tok);
31375   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31376       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31377     {
31378       error_at (pragma_tok->location,
31379                 "%<#pragma omp target update must contain at least one "
31380                 "%<from%> or %<to%> clauses");
31381       return false;
31382     }
31383
31384   tree stmt = make_node (OMP_TARGET_UPDATE);
31385   TREE_TYPE (stmt) = void_type_node;
31386   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31387   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31388   add_stmt (stmt);
31389   return false;
31390 }
31391
31392 /* OpenMP 4.0:
31393    # pragma omp target target-clause[optseq] new-line
31394      structured-block  */
31395
31396 #define OMP_TARGET_CLAUSE_MASK                                  \
31397         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31398         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31399         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31400
31401 static bool
31402 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31403                       enum pragma_context context)
31404 {
31405   if (context != pragma_stmt && context != pragma_compound)
31406     {
31407       cp_parser_error (parser, "expected declaration specifiers");
31408       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31409       return false;
31410     }
31411
31412   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31413     {
31414       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31415       const char *p = IDENTIFIER_POINTER (id);
31416
31417       if (strcmp (p, "teams") == 0)
31418         {
31419           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31420           char p_name[sizeof ("#pragma omp target teams distribute "
31421                               "parallel for simd")];
31422
31423           cp_lexer_consume_token (parser->lexer);
31424           strcpy (p_name, "#pragma omp target");
31425           if (!flag_openmp)  /* flag_openmp_simd  */
31426             {
31427               tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31428                                                OMP_TARGET_CLAUSE_MASK,
31429                                                cclauses);
31430               return stmt != NULL_TREE;
31431             }
31432           keep_next_level (true);
31433           tree sb = begin_omp_structured_block ();
31434           unsigned save = cp_parser_begin_omp_structured_block (parser);
31435           tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31436                                           OMP_TARGET_CLAUSE_MASK, cclauses);
31437           cp_parser_end_omp_structured_block (parser, save);
31438           tree body = finish_omp_structured_block (sb);
31439           if (ret == NULL_TREE)
31440             return false;
31441           tree stmt = make_node (OMP_TARGET);
31442           TREE_TYPE (stmt) = void_type_node;
31443           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31444           OMP_TARGET_BODY (stmt) = body;
31445           add_stmt (stmt);
31446           return true;
31447         }
31448       else if (!flag_openmp)  /* flag_openmp_simd  */
31449         {
31450           cp_parser_require_pragma_eol (parser, pragma_tok);
31451           return false;
31452         }
31453       else if (strcmp (p, "data") == 0)
31454         {
31455           cp_lexer_consume_token (parser->lexer);
31456           cp_parser_omp_target_data (parser, pragma_tok);
31457           return true;
31458         }
31459       else if (strcmp (p, "update") == 0)
31460         {
31461           cp_lexer_consume_token (parser->lexer);
31462           return cp_parser_omp_target_update (parser, pragma_tok, context);
31463         }
31464     }
31465
31466   tree stmt = make_node (OMP_TARGET);
31467   TREE_TYPE (stmt) = void_type_node;
31468
31469   OMP_TARGET_CLAUSES (stmt)
31470     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31471                                  "#pragma omp target", pragma_tok);
31472   keep_next_level (true);
31473   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31474
31475   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31476   add_stmt (stmt);
31477   return true;
31478 }
31479
31480 /* OpenACC 2.0:
31481    # pragma acc cache (variable-list) new-line
31482 */
31483
31484 static tree
31485 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31486 {
31487   tree stmt, clauses;
31488
31489   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31490   clauses = finish_omp_clauses (clauses);
31491
31492   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31493
31494   stmt = make_node (OACC_CACHE);
31495   TREE_TYPE (stmt) = void_type_node;
31496   OACC_CACHE_CLAUSES (stmt) = clauses;
31497   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31498   add_stmt (stmt);
31499
31500   return stmt;
31501 }
31502
31503 /* OpenACC 2.0:
31504    # pragma acc data oacc-data-clause[optseq] new-line
31505      structured-block  */
31506
31507 #define OACC_DATA_CLAUSE_MASK                                           \
31508         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31509         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31510         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31511         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31512         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31513         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31514         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31515         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31516         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31517         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31518         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31519
31520 static tree
31521 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31522 {
31523   tree stmt, clauses, block;
31524   unsigned int save;
31525
31526   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31527                                         "#pragma acc data", pragma_tok);
31528
31529   block = begin_omp_parallel ();
31530   save = cp_parser_begin_omp_structured_block (parser);
31531   cp_parser_statement (parser, NULL_TREE, false, NULL);
31532   cp_parser_end_omp_structured_block (parser, save);
31533   stmt = finish_oacc_data (clauses, block);
31534   return stmt;
31535 }
31536
31537 /* OpenACC 2.0:
31538    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31539
31540    or
31541
31542    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31543
31544    LOC is the location of the #pragma token.
31545 */
31546
31547 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
31548         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31549         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31550         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31551         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31552         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31553         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31554         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31555
31556 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
31557         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31558         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31559         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31560         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
31561         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31562
31563 static tree
31564 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31565                                 bool enter)
31566 {
31567   tree stmt, clauses;
31568
31569   if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31570      || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31571     {
31572       cp_parser_error (parser, enter
31573                        ? "expected %<data%> in %<#pragma acc enter data%>"
31574                        : "expected %<data%> in %<#pragma acc exit data%>");
31575       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31576       return NULL_TREE;
31577     }
31578
31579   const char *p =
31580     IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31581   if (strcmp (p, "data") != 0)
31582     {
31583       cp_parser_error (parser, "invalid pragma");
31584       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31585       return NULL_TREE;
31586     }
31587
31588   cp_lexer_consume_token (parser->lexer);
31589
31590   if (enter)
31591     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31592                                          "#pragma acc enter data", pragma_tok);
31593   else
31594     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31595                                          "#pragma acc exit data", pragma_tok);
31596
31597   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31598     {
31599       error_at (pragma_tok->location,
31600                 "%<#pragma acc enter data%> has no data movement clause");
31601       return NULL_TREE;
31602     }
31603
31604   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31605   TREE_TYPE (stmt) = void_type_node;
31606   if (enter)
31607     OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
31608   else
31609     OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
31610   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31611   add_stmt (stmt);
31612   return stmt;
31613 }
31614
31615 /* OpenACC 2.0:
31616    # pragma acc kernels oacc-kernels-clause[optseq] new-line
31617      structured-block  */
31618
31619 #define OACC_KERNELS_CLAUSE_MASK                                        \
31620         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31621         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31622         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31623         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31624         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31625         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31626         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31627         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31628         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31629         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31630         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31631         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31632         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31633
31634 static tree
31635 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31636 {
31637   tree stmt, clauses, block;
31638   unsigned int save;
31639
31640   clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31641                                         "#pragma acc kernels", pragma_tok);
31642
31643   block = begin_omp_parallel ();
31644   save = cp_parser_begin_omp_structured_block (parser);
31645   cp_parser_statement (parser, NULL_TREE, false, NULL);
31646   cp_parser_end_omp_structured_block (parser, save);
31647   stmt = finish_oacc_kernels (clauses, block);
31648   return stmt;
31649 }
31650
31651 /* OpenACC 2.0:
31652    # pragma acc loop oacc-loop-clause[optseq] new-line
31653      structured-block  */
31654
31655 #define OACC_LOOP_CLAUSE_MASK                                           \
31656         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
31657         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31658
31659 static tree
31660 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31661 {
31662   tree stmt, clauses, block;
31663   int save;
31664
31665   clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31666                                         "#pragma acc loop", pragma_tok);
31667
31668   block = begin_omp_structured_block ();
31669   save = cp_parser_begin_omp_structured_block (parser);
31670   stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31671   cp_parser_end_omp_structured_block (parser, save);
31672   add_stmt (finish_omp_structured_block (block));
31673   return stmt;
31674 }
31675
31676 /* OpenACC 2.0:
31677    # pragma acc parallel oacc-parallel-clause[optseq] new-line
31678      structured-block  */
31679
31680 #define OACC_PARALLEL_CLAUSE_MASK                                       \
31681         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31682         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31683         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31684         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31685         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31686         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31687         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31688         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
31689         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
31690         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31691         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31692         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31693         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31694         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31695         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
31696         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
31697         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31698
31699 static tree
31700 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31701 {
31702   tree stmt, clauses, block;
31703   unsigned int save;
31704
31705   clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31706                                          "#pragma acc parallel", pragma_tok);
31707
31708   block = begin_omp_parallel ();
31709   save = cp_parser_begin_omp_structured_block (parser);
31710   cp_parser_statement (parser, NULL_TREE, false, NULL);
31711   cp_parser_end_omp_structured_block (parser, save);
31712   stmt = finish_oacc_parallel (clauses, block);
31713   return stmt;
31714 }
31715
31716 /* OpenACC 2.0:
31717    # pragma acc update oacc-update-clause[optseq] new-line
31718 */
31719
31720 #define OACC_UPDATE_CLAUSE_MASK                                         \
31721         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31722         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
31723         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
31724         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31725         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
31726         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31727
31728 static tree
31729 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31730 {
31731   tree stmt, clauses;
31732
31733   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31734                                          "#pragma acc update", pragma_tok);
31735
31736   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31737     {
31738       error_at (pragma_tok->location,
31739                 "%<#pragma acc update%> must contain at least one "
31740                 "%<device%> or %<host/self%> clause");
31741       return NULL_TREE;
31742     }
31743
31744   stmt = make_node (OACC_UPDATE);
31745   TREE_TYPE (stmt) = void_type_node;
31746   OACC_UPDATE_CLAUSES (stmt) = clauses;
31747   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31748   add_stmt (stmt);
31749   return stmt;
31750 }
31751
31752 /* OpenACC 2.0:
31753    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31754
31755    LOC is the location of the #pragma token.
31756 */
31757
31758 #define OACC_WAIT_CLAUSE_MASK                                   \
31759         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31760
31761 static tree
31762 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31763 {
31764   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31765   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31766
31767   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31768     list = cp_parser_oacc_wait_list (parser, loc, list);
31769
31770   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31771                                         "#pragma acc wait", pragma_tok);
31772
31773   stmt = c_finish_oacc_wait (loc, list, clauses);
31774
31775   return stmt;
31776 }
31777
31778 /* OpenMP 4.0:
31779    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
31780
31781 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
31782         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
31783         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
31784         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
31785         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
31786         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
31787         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31788
31789 static void
31790 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31791                             enum pragma_context context)
31792 {
31793   bool first_p = parser->omp_declare_simd == NULL;
31794   cp_omp_declare_simd_data data;
31795   if (first_p)
31796     {
31797       data.error_seen = false;
31798       data.fndecl_seen = false;
31799       data.tokens = vNULL;
31800       parser->omp_declare_simd = &data;
31801     }
31802   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31803          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31804     cp_lexer_consume_token (parser->lexer);
31805   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31806     parser->omp_declare_simd->error_seen = true;
31807   cp_parser_require_pragma_eol (parser, pragma_tok);
31808   struct cp_token_cache *cp
31809     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31810   parser->omp_declare_simd->tokens.safe_push (cp);
31811   if (first_p)
31812     {
31813       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31814         cp_parser_pragma (parser, context);
31815       switch (context)
31816         {
31817         case pragma_external:
31818           cp_parser_declaration (parser);
31819           break;
31820         case pragma_member:
31821           cp_parser_member_declaration (parser);
31822           break;
31823         case pragma_objc_icode:
31824           cp_parser_block_declaration (parser, /*statement_p=*/false);
31825           break;
31826         default:
31827           cp_parser_declaration_statement (parser);
31828           break;
31829         }
31830       if (parser->omp_declare_simd
31831           && !parser->omp_declare_simd->error_seen
31832           && !parser->omp_declare_simd->fndecl_seen)
31833         error_at (pragma_tok->location,
31834                   "%<#pragma omp declare simd%> not immediately followed by "
31835                   "function declaration or definition");
31836       data.tokens.release ();
31837       parser->omp_declare_simd = NULL;
31838     }
31839 }
31840
31841 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.  
31842    This function is modelled similar to the late parsing of omp declare 
31843    simd.  */
31844
31845 static tree
31846 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31847 {
31848   struct cp_token_cache *ce;
31849   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31850   int ii = 0;
31851
31852   if (parser->omp_declare_simd != NULL)
31853     {
31854       error ("%<#pragma omp declare simd%> cannot be used in the same function"
31855              " marked as a Cilk Plus SIMD-enabled function");
31856       XDELETE (parser->cilk_simd_fn_info);
31857       parser->cilk_simd_fn_info = NULL;
31858       return attrs;
31859     }
31860   if (!info->error_seen && info->fndecl_seen)
31861     {
31862       error ("vector attribute not immediately followed by a single function"
31863              " declaration or definition");
31864       info->error_seen = true;
31865     }
31866   if (info->error_seen)
31867     return attrs;
31868
31869   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31870     {
31871       tree c, cl;
31872
31873       cp_parser_push_lexer_for_tokens (parser, ce);
31874       parser->lexer->in_pragma = true;
31875       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31876                                       "SIMD-enabled functions attribute", 
31877                                       NULL);
31878       cp_parser_pop_lexer (parser);
31879       if (cl)
31880         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31881
31882       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31883       TREE_CHAIN (c) = attrs;
31884       attrs = c;
31885
31886       c = build_tree_list (get_identifier ("omp declare simd"), cl);
31887       TREE_CHAIN (c) = attrs;
31888       if (processing_template_decl)
31889         ATTR_IS_DEPENDENT (c) = 1;
31890       attrs = c;
31891     }
31892   info->fndecl_seen = true;
31893   XDELETE (parser->cilk_simd_fn_info);
31894   parser->cilk_simd_fn_info = NULL;
31895   return attrs;
31896 }
31897
31898 /* Finalize #pragma omp declare simd clauses after direct declarator has
31899    been parsed, and put that into "omp declare simd" attribute.  */
31900
31901 static tree
31902 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31903 {
31904   struct cp_token_cache *ce;
31905   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
31906   int i;
31907
31908   if (!data->error_seen && data->fndecl_seen)
31909     {
31910       error ("%<#pragma omp declare simd%> not immediately followed by "
31911              "a single function declaration or definition");
31912       data->error_seen = true;
31913       return attrs;
31914     }
31915   if (data->error_seen)
31916     return attrs;
31917
31918   FOR_EACH_VEC_ELT (data->tokens, i, ce)
31919     {
31920       tree c, cl;
31921
31922       cp_parser_push_lexer_for_tokens (parser, ce);
31923       parser->lexer->in_pragma = true;
31924       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
31925       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
31926       cp_lexer_consume_token (parser->lexer);
31927       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
31928                                       "#pragma omp declare simd", pragma_tok);
31929       cp_parser_pop_lexer (parser);
31930       if (cl)
31931         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31932       c = build_tree_list (get_identifier ("omp declare simd"), cl);
31933       TREE_CHAIN (c) = attrs;
31934       if (processing_template_decl)
31935         ATTR_IS_DEPENDENT (c) = 1;
31936       attrs = c;
31937     }
31938
31939   data->fndecl_seen = true;
31940   return attrs;
31941 }
31942
31943
31944 /* OpenMP 4.0:
31945    # pragma omp declare target new-line
31946    declarations and definitions
31947    # pragma omp end declare target new-line  */
31948
31949 static void
31950 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
31951 {
31952   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31953   scope_chain->omp_declare_target_attribute++;
31954 }
31955
31956 static void
31957 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
31958 {
31959   const char *p = "";
31960   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31961     {
31962       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31963       p = IDENTIFIER_POINTER (id);
31964     }
31965   if (strcmp (p, "declare") == 0)
31966     {
31967       cp_lexer_consume_token (parser->lexer);
31968       p = "";
31969       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31970         {
31971           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31972           p = IDENTIFIER_POINTER (id);
31973         }
31974       if (strcmp (p, "target") == 0)
31975         cp_lexer_consume_token (parser->lexer);
31976       else
31977         {
31978           cp_parser_error (parser, "expected %<target%>");
31979           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31980           return;
31981         }
31982     }
31983   else
31984     {
31985       cp_parser_error (parser, "expected %<declare%>");
31986       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31987       return;
31988     }
31989   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31990   if (!scope_chain->omp_declare_target_attribute)
31991     error_at (pragma_tok->location,
31992               "%<#pragma omp end declare target%> without corresponding "
31993               "%<#pragma omp declare target%>");
31994   else
31995     scope_chain->omp_declare_target_attribute--;
31996 }
31997
31998 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
31999    expression and optional initializer clause of
32000    #pragma omp declare reduction.  We store the expression(s) as
32001    either 3, 6 or 7 special statements inside of the artificial function's
32002    body.  The first two statements are DECL_EXPRs for the artificial
32003    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32004    expression that uses those variables.
32005    If there was any INITIALIZER clause, this is followed by further statements,
32006    the fourth and fifth statements are DECL_EXPRs for the artificial
32007    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
32008    constructor variant (first token after open paren is not omp_priv),
32009    then the sixth statement is a statement with the function call expression
32010    that uses the OMP_PRIV and optionally OMP_ORIG variable.
32011    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32012    to initialize the OMP_PRIV artificial variable and there is seventh
32013    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
32014
32015 static bool
32016 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32017 {
32018   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32019   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32020   type = TREE_TYPE (type);
32021   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32022   DECL_ARTIFICIAL (omp_out) = 1;
32023   pushdecl (omp_out);
32024   add_decl_expr (omp_out);
32025   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32026   DECL_ARTIFICIAL (omp_in) = 1;
32027   pushdecl (omp_in);
32028   add_decl_expr (omp_in);
32029   tree combiner;
32030   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32031
32032   keep_next_level (true);
32033   tree block = begin_omp_structured_block ();
32034   combiner = cp_parser_expression (parser);
32035   finish_expr_stmt (combiner);
32036   block = finish_omp_structured_block (block);
32037   add_stmt (block);
32038
32039   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32040     return false;
32041
32042   const char *p = "";
32043   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32044     {
32045       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32046       p = IDENTIFIER_POINTER (id);
32047     }
32048
32049   if (strcmp (p, "initializer") == 0)
32050     {
32051       cp_lexer_consume_token (parser->lexer);
32052       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32053         return false;
32054
32055       p = "";
32056       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32057         {
32058           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32059           p = IDENTIFIER_POINTER (id);
32060         }
32061
32062       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32063       DECL_ARTIFICIAL (omp_priv) = 1;
32064       pushdecl (omp_priv);
32065       add_decl_expr (omp_priv);
32066       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32067       DECL_ARTIFICIAL (omp_orig) = 1;
32068       pushdecl (omp_orig);
32069       add_decl_expr (omp_orig);
32070
32071       keep_next_level (true);
32072       block = begin_omp_structured_block ();
32073
32074       bool ctor = false;
32075       if (strcmp (p, "omp_priv") == 0)
32076         {
32077           bool is_direct_init, is_non_constant_init;
32078           ctor = true;
32079           cp_lexer_consume_token (parser->lexer);
32080           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
32081           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32082               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32083                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32084                      == CPP_CLOSE_PAREN
32085                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32086                      == CPP_CLOSE_PAREN))
32087             {
32088               finish_omp_structured_block (block);
32089               error ("invalid initializer clause");
32090               return false;
32091             }
32092           initializer = cp_parser_initializer (parser, &is_direct_init,
32093                                                &is_non_constant_init);
32094           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32095                           NULL_TREE, LOOKUP_ONLYCONVERTING);
32096         }
32097       else
32098         {
32099           cp_parser_parse_tentatively (parser);
32100           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32101                                                   /*check_dependency_p=*/true,
32102                                                   /*template_p=*/NULL,
32103                                                   /*declarator_p=*/false,
32104                                                   /*optional_p=*/false);
32105           vec<tree, va_gc> *args;
32106           if (fn_name == error_mark_node
32107               || cp_parser_error_occurred (parser)
32108               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32109               || ((args = cp_parser_parenthesized_expression_list
32110                                 (parser, non_attr, /*cast_p=*/false,
32111                                  /*allow_expansion_p=*/true,
32112                                  /*non_constant_p=*/NULL)),
32113                   cp_parser_error_occurred (parser)))
32114             {
32115               finish_omp_structured_block (block);
32116               cp_parser_abort_tentative_parse (parser);
32117               cp_parser_error (parser, "expected id-expression (arguments)");
32118               return false;
32119             }
32120           unsigned int i;
32121           tree arg;
32122           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32123             if (arg == omp_priv
32124                 || (TREE_CODE (arg) == ADDR_EXPR
32125                     && TREE_OPERAND (arg, 0) == omp_priv))
32126               break;
32127           cp_parser_abort_tentative_parse (parser);
32128           if (arg == NULL_TREE)
32129             error ("one of the initializer call arguments should be %<omp_priv%>"
32130                    " or %<&omp_priv%>");
32131           initializer = cp_parser_postfix_expression (parser, false, false, false,
32132                                                       false, NULL);
32133           finish_expr_stmt (initializer);
32134         }
32135
32136       block = finish_omp_structured_block (block);
32137       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32138       add_stmt (block);
32139
32140       if (ctor)
32141         add_decl_expr (omp_orig);
32142
32143       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32144         return false;
32145     }
32146
32147   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32148     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32149
32150   return true;
32151 }
32152
32153 /* OpenMP 4.0
32154    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32155       initializer-clause[opt] new-line
32156
32157    initializer-clause:
32158       initializer (omp_priv initializer)
32159       initializer (function-name (argument-list))  */
32160
32161 static void
32162 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32163                                  enum pragma_context)
32164 {
32165   auto_vec<tree> types;
32166   enum tree_code reduc_code = ERROR_MARK;
32167   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32168   unsigned int i;
32169   cp_token *first_token;
32170   cp_token_cache *cp;
32171   int errs;
32172   void *p;
32173     
32174   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
32175   p = obstack_alloc (&declarator_obstack, 0);
32176
32177   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32178     goto fail;
32179
32180   switch (cp_lexer_peek_token (parser->lexer)->type)
32181     {
32182     case CPP_PLUS:
32183       reduc_code = PLUS_EXPR;
32184       break;
32185     case CPP_MULT:
32186       reduc_code = MULT_EXPR;
32187       break;
32188     case CPP_MINUS:
32189       reduc_code = MINUS_EXPR;
32190       break;
32191     case CPP_AND:
32192       reduc_code = BIT_AND_EXPR;
32193       break;
32194     case CPP_XOR:
32195       reduc_code = BIT_XOR_EXPR;
32196       break;
32197     case CPP_OR:
32198       reduc_code = BIT_IOR_EXPR;
32199       break;
32200     case CPP_AND_AND:
32201       reduc_code = TRUTH_ANDIF_EXPR;
32202       break;
32203     case CPP_OR_OR:
32204       reduc_code = TRUTH_ORIF_EXPR;
32205       break;
32206     case CPP_NAME:
32207       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32208       break;
32209     default:
32210       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32211                                "%<|%>, %<&&%>, %<||%> or identifier");
32212       goto fail;
32213     }
32214
32215   if (reduc_code != ERROR_MARK)
32216     cp_lexer_consume_token (parser->lexer);
32217
32218   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32219   if (reduc_id == error_mark_node)
32220     goto fail;
32221
32222   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32223     goto fail;
32224
32225   /* Types may not be defined in declare reduction type list.  */
32226   const char *saved_message;
32227   saved_message = parser->type_definition_forbidden_message;
32228   parser->type_definition_forbidden_message
32229     = G_("types may not be defined in declare reduction type list");
32230   bool saved_colon_corrects_to_scope_p;
32231   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32232   parser->colon_corrects_to_scope_p = false;
32233   bool saved_colon_doesnt_start_class_def_p;
32234   saved_colon_doesnt_start_class_def_p
32235     = parser->colon_doesnt_start_class_def_p;
32236   parser->colon_doesnt_start_class_def_p = true;
32237
32238   while (true)
32239     {
32240       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32241       type = cp_parser_type_id (parser);
32242       if (type == error_mark_node)
32243         ;
32244       else if (ARITHMETIC_TYPE_P (type)
32245                && (orig_reduc_id == NULL_TREE
32246                    || (TREE_CODE (type) != COMPLEX_TYPE
32247                        && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32248                                    "min") == 0
32249                            || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32250                                       "max") == 0))))
32251         error_at (loc, "predeclared arithmetic type %qT in "
32252                        "%<#pragma omp declare reduction%>", type);
32253       else if (TREE_CODE (type) == FUNCTION_TYPE
32254                || TREE_CODE (type) == METHOD_TYPE
32255                || TREE_CODE (type) == ARRAY_TYPE)
32256         error_at (loc, "function or array type %qT in "
32257                        "%<#pragma omp declare reduction%>", type);
32258       else if (TREE_CODE (type) == REFERENCE_TYPE)
32259         error_at (loc, "reference type %qT in "
32260                        "%<#pragma omp declare reduction%>", type);
32261       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32262         error_at (loc, "const, volatile or __restrict qualified type %qT in "
32263                        "%<#pragma omp declare reduction%>", type);
32264       else
32265         types.safe_push (type);
32266
32267       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32268         cp_lexer_consume_token (parser->lexer);
32269       else
32270         break;
32271     }
32272
32273   /* Restore the saved message.  */
32274   parser->type_definition_forbidden_message = saved_message;
32275   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32276   parser->colon_doesnt_start_class_def_p
32277     = saved_colon_doesnt_start_class_def_p;
32278
32279   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32280       || types.is_empty ())
32281     {
32282      fail:
32283       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32284       goto done;
32285     }
32286
32287   first_token = cp_lexer_peek_token (parser->lexer);
32288   cp = NULL;
32289   errs = errorcount;
32290   FOR_EACH_VEC_ELT (types, i, type)
32291     {
32292       tree fntype
32293         = build_function_type_list (void_type_node,
32294                                     cp_build_reference_type (type, false),
32295                                     NULL_TREE);
32296       tree this_reduc_id = reduc_id;
32297       if (!dependent_type_p (type))
32298         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32299       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32300       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32301       DECL_ARTIFICIAL (fndecl) = 1;
32302       DECL_EXTERNAL (fndecl) = 1;
32303       DECL_DECLARED_INLINE_P (fndecl) = 1;
32304       DECL_IGNORED_P (fndecl) = 1;
32305       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32306       DECL_ATTRIBUTES (fndecl)
32307         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32308                      DECL_ATTRIBUTES (fndecl));
32309       if (processing_template_decl)
32310         fndecl = push_template_decl (fndecl);
32311       bool block_scope = false;
32312       tree block = NULL_TREE;
32313       if (current_function_decl)
32314         {
32315           block_scope = true;
32316           DECL_CONTEXT (fndecl) = global_namespace;
32317           if (!processing_template_decl)
32318             pushdecl (fndecl);
32319         }
32320       else if (current_class_type)
32321         {
32322           if (cp == NULL)
32323             {
32324               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32325                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32326                 cp_lexer_consume_token (parser->lexer);
32327               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32328                 goto fail;
32329               cp = cp_token_cache_new (first_token,
32330                                        cp_lexer_peek_nth_token (parser->lexer,
32331                                                                 2));
32332             }
32333           DECL_STATIC_FUNCTION_P (fndecl) = 1;
32334           finish_member_declaration (fndecl);
32335           DECL_PENDING_INLINE_INFO (fndecl) = cp;
32336           DECL_PENDING_INLINE_P (fndecl) = 1;
32337           vec_safe_push (unparsed_funs_with_definitions, fndecl);
32338           continue;
32339         }
32340       else
32341         {
32342           DECL_CONTEXT (fndecl) = current_namespace;
32343           pushdecl (fndecl);
32344         }
32345       if (!block_scope)
32346         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32347       else
32348         block = begin_omp_structured_block ();
32349       if (cp)
32350         {
32351           cp_parser_push_lexer_for_tokens (parser, cp);
32352           parser->lexer->in_pragma = true;
32353         }
32354       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32355         {
32356           if (!block_scope)
32357             finish_function (0);
32358           else
32359             DECL_CONTEXT (fndecl) = current_function_decl;
32360           if (cp)
32361             cp_parser_pop_lexer (parser);
32362           goto fail;
32363         }
32364       if (cp)
32365         cp_parser_pop_lexer (parser);
32366       if (!block_scope)
32367         finish_function (0);
32368       else
32369         {
32370           DECL_CONTEXT (fndecl) = current_function_decl;
32371           block = finish_omp_structured_block (block);
32372           if (TREE_CODE (block) == BIND_EXPR)
32373             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32374           else if (TREE_CODE (block) == STATEMENT_LIST)
32375             DECL_SAVED_TREE (fndecl) = block;
32376           if (processing_template_decl)
32377             add_decl_expr (fndecl);
32378         }
32379       cp_check_omp_declare_reduction (fndecl);
32380       if (cp == NULL && types.length () > 1)
32381         cp = cp_token_cache_new (first_token,
32382                                  cp_lexer_peek_nth_token (parser->lexer, 2));
32383       if (errs != errorcount)
32384         break;
32385     }
32386
32387   cp_parser_require_pragma_eol (parser, pragma_tok);
32388
32389  done:
32390   /* Free any declarators allocated.  */
32391   obstack_free (&declarator_obstack, p);
32392 }
32393
32394 /* OpenMP 4.0
32395    #pragma omp declare simd declare-simd-clauses[optseq] new-line
32396    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32397       initializer-clause[opt] new-line
32398    #pragma omp declare target new-line  */
32399
32400 static void
32401 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32402                        enum pragma_context context)
32403 {
32404   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32405     {
32406       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32407       const char *p = IDENTIFIER_POINTER (id);
32408
32409       if (strcmp (p, "simd") == 0)
32410         {
32411           cp_lexer_consume_token (parser->lexer);
32412           cp_parser_omp_declare_simd (parser, pragma_tok,
32413                                       context);
32414           return;
32415         }
32416       cp_ensure_no_omp_declare_simd (parser);
32417       if (strcmp (p, "reduction") == 0)
32418         {
32419           cp_lexer_consume_token (parser->lexer);
32420           cp_parser_omp_declare_reduction (parser, pragma_tok,
32421                                            context);
32422           return;
32423         }
32424       if (!flag_openmp)  /* flag_openmp_simd  */
32425         {
32426           cp_parser_require_pragma_eol (parser, pragma_tok);
32427           return;
32428         }
32429       if (strcmp (p, "target") == 0)
32430         {
32431           cp_lexer_consume_token (parser->lexer);
32432           cp_parser_omp_declare_target (parser, pragma_tok);
32433           return;
32434         }
32435     }
32436   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32437                            "or %<target%>");
32438   cp_parser_require_pragma_eol (parser, pragma_tok);
32439 }
32440
32441 /* Main entry point to OpenMP statement pragmas.  */
32442
32443 static void
32444 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32445 {
32446   tree stmt;
32447   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32448   omp_clause_mask mask (0);
32449
32450   switch (pragma_tok->pragma_kind)
32451     {
32452     case PRAGMA_OACC_CACHE:
32453       stmt = cp_parser_oacc_cache (parser, pragma_tok);
32454       break;
32455     case PRAGMA_OACC_DATA:
32456       stmt = cp_parser_oacc_data (parser, pragma_tok);
32457       break;
32458     case PRAGMA_OACC_ENTER_DATA:
32459       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32460       break;
32461     case PRAGMA_OACC_EXIT_DATA:
32462       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32463       break;
32464     case PRAGMA_OACC_KERNELS:
32465       stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32466       break;
32467     case PRAGMA_OACC_LOOP:
32468       stmt = cp_parser_oacc_loop (parser, pragma_tok);
32469       break;
32470     case PRAGMA_OACC_PARALLEL:
32471       stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32472       break;
32473     case PRAGMA_OACC_UPDATE:
32474       stmt = cp_parser_oacc_update (parser, pragma_tok);
32475       break;
32476     case PRAGMA_OACC_WAIT:
32477       stmt = cp_parser_oacc_wait (parser, pragma_tok);
32478       break;
32479     case PRAGMA_OMP_ATOMIC:
32480       cp_parser_omp_atomic (parser, pragma_tok);
32481       return;
32482     case PRAGMA_OMP_CRITICAL:
32483       stmt = cp_parser_omp_critical (parser, pragma_tok);
32484       break;
32485     case PRAGMA_OMP_DISTRIBUTE:
32486       strcpy (p_name, "#pragma omp");
32487       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32488       break;
32489     case PRAGMA_OMP_FOR:
32490       strcpy (p_name, "#pragma omp");
32491       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32492       break;
32493     case PRAGMA_OMP_MASTER:
32494       stmt = cp_parser_omp_master (parser, pragma_tok);
32495       break;
32496     case PRAGMA_OMP_ORDERED:
32497       stmt = cp_parser_omp_ordered (parser, pragma_tok);
32498       break;
32499     case PRAGMA_OMP_PARALLEL:
32500       strcpy (p_name, "#pragma omp");
32501       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32502       break;
32503     case PRAGMA_OMP_SECTIONS:
32504       strcpy (p_name, "#pragma omp");
32505       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32506       break;
32507     case PRAGMA_OMP_SIMD:
32508       strcpy (p_name, "#pragma omp");
32509       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32510       break;
32511     case PRAGMA_OMP_SINGLE:
32512       stmt = cp_parser_omp_single (parser, pragma_tok);
32513       break;
32514     case PRAGMA_OMP_TASK:
32515       stmt = cp_parser_omp_task (parser, pragma_tok);
32516       break;
32517     case PRAGMA_OMP_TASKGROUP:
32518       stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32519       break;
32520     case PRAGMA_OMP_TEAMS:
32521       strcpy (p_name, "#pragma omp");
32522       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32523       break;
32524     default:
32525       gcc_unreachable ();
32526     }
32527
32528   if (stmt)
32529     SET_EXPR_LOCATION (stmt, pragma_tok->location);
32530 }
32531 \f
32532 /* Transactional Memory parsing routines.  */
32533
32534 /* Parse a transaction attribute.
32535
32536    txn-attribute:
32537         attribute
32538         [ [ identifier ] ]
32539
32540    ??? Simplify this when C++0x bracket attributes are
32541    implemented properly.  */
32542
32543 static tree
32544 cp_parser_txn_attribute_opt (cp_parser *parser)
32545 {
32546   cp_token *token;
32547   tree attr_name, attr = NULL;
32548
32549   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32550     return cp_parser_attributes_opt (parser);
32551
32552   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32553     return NULL_TREE;
32554   cp_lexer_consume_token (parser->lexer);
32555   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32556     goto error1;
32557
32558   token = cp_lexer_peek_token (parser->lexer);
32559   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32560     {
32561       token = cp_lexer_consume_token (parser->lexer);
32562
32563       attr_name = (token->type == CPP_KEYWORD
32564                    /* For keywords, use the canonical spelling,
32565                       not the parsed identifier.  */
32566                    ? ridpointers[(int) token->keyword]
32567                    : token->u.value);
32568       attr = build_tree_list (attr_name, NULL_TREE);
32569     }
32570   else
32571     cp_parser_error (parser, "expected identifier");
32572
32573   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32574  error1:
32575   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32576   return attr;
32577 }
32578
32579 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32580
32581    transaction-statement:
32582      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32583        compound-statement
32584      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32585 */
32586
32587 static tree
32588 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32589 {
32590   unsigned char old_in = parser->in_transaction;
32591   unsigned char this_in = 1, new_in;
32592   cp_token *token;
32593   tree stmt, attrs, noex;
32594
32595   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32596       || keyword == RID_TRANSACTION_RELAXED);
32597   token = cp_parser_require_keyword (parser, keyword,
32598       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32599           : RT_TRANSACTION_RELAXED));
32600   gcc_assert (token != NULL);
32601
32602   if (keyword == RID_TRANSACTION_RELAXED)
32603     this_in |= TM_STMT_ATTR_RELAXED;
32604   else
32605     {
32606       attrs = cp_parser_txn_attribute_opt (parser);
32607       if (attrs)
32608         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32609     }
32610
32611   /* Parse a noexcept specification.  */
32612   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32613
32614   /* Keep track if we're in the lexical scope of an outer transaction.  */
32615   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32616
32617   stmt = begin_transaction_stmt (token->location, NULL, this_in);
32618
32619   parser->in_transaction = new_in;
32620   cp_parser_compound_statement (parser, NULL, false, false);
32621   parser->in_transaction = old_in;
32622
32623   finish_transaction_stmt (stmt, NULL, this_in, noex);
32624
32625   return stmt;
32626 }
32627
32628 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32629
32630    transaction-expression:
32631      __transaction_atomic txn-noexcept-spec[opt] ( expression )
32632      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32633 */
32634
32635 static tree
32636 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32637 {
32638   unsigned char old_in = parser->in_transaction;
32639   unsigned char this_in = 1;
32640   cp_token *token;
32641   tree expr, noex;
32642   bool noex_expr;
32643
32644   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32645       || keyword == RID_TRANSACTION_RELAXED);
32646
32647   if (!flag_tm)
32648     error (keyword == RID_TRANSACTION_RELAXED
32649            ? G_("%<__transaction_relaxed%> without transactional memory "
32650                 "support enabled")
32651            : G_("%<__transaction_atomic%> without transactional memory "
32652                 "support enabled"));
32653
32654   token = cp_parser_require_keyword (parser, keyword,
32655       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32656           : RT_TRANSACTION_RELAXED));
32657   gcc_assert (token != NULL);
32658
32659   if (keyword == RID_TRANSACTION_RELAXED)
32660     this_in |= TM_STMT_ATTR_RELAXED;
32661
32662   /* Set this early.  This might mean that we allow transaction_cancel in
32663      an expression that we find out later actually has to be a constexpr.
32664      However, we expect that cxx_constant_value will be able to deal with
32665      this; also, if the noexcept has no constexpr, then what we parse next
32666      really is a transaction's body.  */
32667   parser->in_transaction = this_in;
32668
32669   /* Parse a noexcept specification.  */
32670   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32671                                                true);
32672
32673   if (!noex || !noex_expr
32674       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32675     {
32676       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32677
32678       expr = cp_parser_expression (parser);
32679       expr = finish_parenthesized_expr (expr);
32680
32681       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32682     }
32683   else
32684     {
32685       /* The only expression that is available got parsed for the noexcept
32686          already.  noexcept is true then.  */
32687       expr = noex;
32688       noex = boolean_true_node;
32689     }
32690
32691   expr = build_transaction_expr (token->location, expr, this_in, noex);
32692   parser->in_transaction = old_in;
32693
32694   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32695     return error_mark_node;
32696
32697   return (flag_tm ? expr : error_mark_node);
32698 }
32699
32700 /* Parse a function-transaction-block.
32701
32702    function-transaction-block:
32703      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32704          function-body
32705      __transaction_atomic txn-attribute[opt] function-try-block
32706      __transaction_relaxed ctor-initializer[opt] function-body
32707      __transaction_relaxed function-try-block
32708 */
32709
32710 static bool
32711 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32712 {
32713   unsigned char old_in = parser->in_transaction;
32714   unsigned char new_in = 1;
32715   tree compound_stmt, stmt, attrs;
32716   bool ctor_initializer_p;
32717   cp_token *token;
32718
32719   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32720       || keyword == RID_TRANSACTION_RELAXED);
32721   token = cp_parser_require_keyword (parser, keyword,
32722       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32723           : RT_TRANSACTION_RELAXED));
32724   gcc_assert (token != NULL);
32725
32726   if (keyword == RID_TRANSACTION_RELAXED)
32727     new_in |= TM_STMT_ATTR_RELAXED;
32728   else
32729     {
32730       attrs = cp_parser_txn_attribute_opt (parser);
32731       if (attrs)
32732         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32733     }
32734
32735   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32736
32737   parser->in_transaction = new_in;
32738
32739   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32740     ctor_initializer_p = cp_parser_function_try_block (parser);
32741   else
32742     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32743       (parser, /*in_function_try_block=*/false);
32744
32745   parser->in_transaction = old_in;
32746
32747   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32748
32749   return ctor_initializer_p;
32750 }
32751
32752 /* Parse a __transaction_cancel statement.
32753
32754    cancel-statement:
32755      __transaction_cancel txn-attribute[opt] ;
32756      __transaction_cancel txn-attribute[opt] throw-expression ;
32757
32758    ??? Cancel and throw is not yet implemented.  */
32759
32760 static tree
32761 cp_parser_transaction_cancel (cp_parser *parser)
32762 {
32763   cp_token *token;
32764   bool is_outer = false;
32765   tree stmt, attrs;
32766
32767   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32768                                      RT_TRANSACTION_CANCEL);
32769   gcc_assert (token != NULL);
32770
32771   attrs = cp_parser_txn_attribute_opt (parser);
32772   if (attrs)
32773     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32774
32775   /* ??? Parse cancel-and-throw here.  */
32776
32777   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32778
32779   if (!flag_tm)
32780     {
32781       error_at (token->location, "%<__transaction_cancel%> without "
32782                 "transactional memory support enabled");
32783       return error_mark_node;
32784     }
32785   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32786     {
32787       error_at (token->location, "%<__transaction_cancel%> within a "
32788                 "%<__transaction_relaxed%>");
32789       return error_mark_node;
32790     }
32791   else if (is_outer)
32792     {
32793       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32794           && !is_tm_may_cancel_outer (current_function_decl))
32795         {
32796           error_at (token->location, "outer %<__transaction_cancel%> not "
32797                     "within outer %<__transaction_atomic%>");
32798           error_at (token->location,
32799                     "  or a %<transaction_may_cancel_outer%> function");
32800           return error_mark_node;
32801         }
32802     }
32803   else if (parser->in_transaction == 0)
32804     {
32805       error_at (token->location, "%<__transaction_cancel%> not within "
32806                 "%<__transaction_atomic%>");
32807       return error_mark_node;
32808     }
32809
32810   stmt = build_tm_abort_call (token->location, is_outer);
32811   add_stmt (stmt);
32812
32813   return stmt;
32814 }
32815 \f
32816 /* The parser.  */
32817
32818 static GTY (()) cp_parser *the_parser;
32819
32820 \f
32821 /* Special handling for the first token or line in the file.  The first
32822    thing in the file might be #pragma GCC pch_preprocess, which loads a
32823    PCH file, which is a GC collection point.  So we need to handle this
32824    first pragma without benefit of an existing lexer structure.
32825
32826    Always returns one token to the caller in *FIRST_TOKEN.  This is
32827    either the true first token of the file, or the first token after
32828    the initial pragma.  */
32829
32830 static void
32831 cp_parser_initial_pragma (cp_token *first_token)
32832 {
32833   tree name = NULL;
32834
32835   cp_lexer_get_preprocessor_token (NULL, first_token);
32836   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32837     return;
32838
32839   cp_lexer_get_preprocessor_token (NULL, first_token);
32840   if (first_token->type == CPP_STRING)
32841     {
32842       name = first_token->u.value;
32843
32844       cp_lexer_get_preprocessor_token (NULL, first_token);
32845       if (first_token->type != CPP_PRAGMA_EOL)
32846         error_at (first_token->location,
32847                   "junk at end of %<#pragma GCC pch_preprocess%>");
32848     }
32849   else
32850     error_at (first_token->location, "expected string literal");
32851
32852   /* Skip to the end of the pragma.  */
32853   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32854     cp_lexer_get_preprocessor_token (NULL, first_token);
32855
32856   /* Now actually load the PCH file.  */
32857   if (name)
32858     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32859
32860   /* Read one more token to return to our caller.  We have to do this
32861      after reading the PCH file in, since its pointers have to be
32862      live.  */
32863   cp_lexer_get_preprocessor_token (NULL, first_token);
32864 }
32865
32866 /* Parses the grainsize pragma for the _Cilk_for statement.
32867    Syntax:
32868    #pragma cilk grainsize = <VALUE>.  */
32869
32870 static void
32871 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32872 {
32873   if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32874     {
32875       tree exp = cp_parser_binary_expression (parser, false, false,
32876                                               PREC_NOT_OPERATOR, NULL);
32877       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32878       if (!exp || exp == error_mark_node)
32879         {
32880           error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32881           return;
32882         }
32883
32884       /* Make sure the next token is _Cilk_for, it is invalid otherwise.  */
32885       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32886         cp_parser_cilk_for (parser, exp);
32887       else
32888         warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32889                     "%<#pragma cilk grainsize%> is not followed by "
32890                     "%<_Cilk_for%>");
32891       return;
32892     }
32893   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32894 }
32895
32896 /* Normal parsing of a pragma token.  Here we can (and must) use the
32897    regular lexer.  */
32898
32899 static bool
32900 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32901 {
32902   cp_token *pragma_tok;
32903   unsigned int id;
32904
32905   pragma_tok = cp_lexer_consume_token (parser->lexer);
32906   gcc_assert (pragma_tok->type == CPP_PRAGMA);
32907   parser->lexer->in_pragma = true;
32908
32909   id = pragma_tok->pragma_kind;
32910   if (id != PRAGMA_OMP_DECLARE_REDUCTION)
32911     cp_ensure_no_omp_declare_simd (parser);
32912   switch (id)
32913     {
32914     case PRAGMA_GCC_PCH_PREPROCESS:
32915       error_at (pragma_tok->location,
32916                 "%<#pragma GCC pch_preprocess%> must be first");
32917       break;
32918
32919     case PRAGMA_OMP_BARRIER:
32920       switch (context)
32921         {
32922         case pragma_compound:
32923           cp_parser_omp_barrier (parser, pragma_tok);
32924           return false;
32925         case pragma_stmt:
32926           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
32927                     "used in compound statements");
32928           break;
32929         default:
32930           goto bad_stmt;
32931         }
32932       break;
32933
32934     case PRAGMA_OMP_FLUSH:
32935       switch (context)
32936         {
32937         case pragma_compound:
32938           cp_parser_omp_flush (parser, pragma_tok);
32939           return false;
32940         case pragma_stmt:
32941           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
32942                     "used in compound statements");
32943           break;
32944         default:
32945           goto bad_stmt;
32946         }
32947       break;
32948
32949     case PRAGMA_OMP_TASKWAIT:
32950       switch (context)
32951         {
32952         case pragma_compound:
32953           cp_parser_omp_taskwait (parser, pragma_tok);
32954           return false;
32955         case pragma_stmt:
32956           error_at (pragma_tok->location,
32957                     "%<#pragma omp taskwait%> may only be "
32958                     "used in compound statements");
32959           break;
32960         default:
32961           goto bad_stmt;
32962         }
32963       break;
32964
32965     case PRAGMA_OMP_TASKYIELD:
32966       switch (context)
32967         {
32968         case pragma_compound:
32969           cp_parser_omp_taskyield (parser, pragma_tok);
32970           return false;
32971         case pragma_stmt:
32972           error_at (pragma_tok->location,
32973                     "%<#pragma omp taskyield%> may only be "
32974                     "used in compound statements");
32975           break;
32976         default:
32977           goto bad_stmt;
32978         }
32979       break;
32980
32981     case PRAGMA_OMP_CANCEL:
32982       switch (context)
32983         {
32984         case pragma_compound:
32985           cp_parser_omp_cancel (parser, pragma_tok);
32986           return false;
32987         case pragma_stmt:
32988           error_at (pragma_tok->location,
32989                     "%<#pragma omp cancel%> may only be "
32990                     "used in compound statements");
32991           break;
32992         default:
32993           goto bad_stmt;
32994         }
32995       break;
32996
32997     case PRAGMA_OMP_CANCELLATION_POINT:
32998       switch (context)
32999         {
33000         case pragma_compound:
33001           cp_parser_omp_cancellation_point (parser, pragma_tok);
33002           return false;
33003         case pragma_stmt:
33004           error_at (pragma_tok->location,
33005                     "%<#pragma omp cancellation point%> may only be "
33006                     "used in compound statements");
33007           break;
33008         default:
33009           goto bad_stmt;
33010         }
33011       break;
33012
33013     case PRAGMA_OMP_THREADPRIVATE:
33014       cp_parser_omp_threadprivate (parser, pragma_tok);
33015       return false;
33016
33017     case PRAGMA_OMP_DECLARE_REDUCTION:
33018       cp_parser_omp_declare (parser, pragma_tok, context);
33019       return false;
33020
33021     case PRAGMA_OACC_CACHE:
33022     case PRAGMA_OACC_DATA:
33023     case PRAGMA_OACC_ENTER_DATA:
33024     case PRAGMA_OACC_EXIT_DATA:
33025     case PRAGMA_OACC_KERNELS:
33026     case PRAGMA_OACC_PARALLEL:
33027     case PRAGMA_OACC_LOOP:
33028     case PRAGMA_OACC_UPDATE:
33029     case PRAGMA_OACC_WAIT:
33030     case PRAGMA_OMP_ATOMIC:
33031     case PRAGMA_OMP_CRITICAL:
33032     case PRAGMA_OMP_DISTRIBUTE:
33033     case PRAGMA_OMP_FOR:
33034     case PRAGMA_OMP_MASTER:
33035     case PRAGMA_OMP_ORDERED:
33036     case PRAGMA_OMP_PARALLEL:
33037     case PRAGMA_OMP_SECTIONS:
33038     case PRAGMA_OMP_SIMD:
33039     case PRAGMA_OMP_SINGLE:
33040     case PRAGMA_OMP_TASK:
33041     case PRAGMA_OMP_TASKGROUP:
33042     case PRAGMA_OMP_TEAMS:
33043       if (context != pragma_stmt && context != pragma_compound)
33044         goto bad_stmt;
33045       cp_parser_omp_construct (parser, pragma_tok);
33046       return true;
33047
33048     case PRAGMA_OMP_TARGET:
33049       return cp_parser_omp_target (parser, pragma_tok, context);
33050
33051     case PRAGMA_OMP_END_DECLARE_TARGET:
33052       cp_parser_omp_end_declare_target (parser, pragma_tok);
33053       return false;
33054
33055     case PRAGMA_OMP_SECTION:
33056       error_at (pragma_tok->location, 
33057                 "%<#pragma omp section%> may only be used in "
33058                 "%<#pragma omp sections%> construct");
33059       break;
33060
33061     case PRAGMA_IVDEP:
33062       {
33063         cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33064         cp_token *tok;
33065         tok = cp_lexer_peek_token (the_parser->lexer);
33066         if (tok->type != CPP_KEYWORD
33067             || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33068                 && tok->keyword != RID_DO))
33069           {
33070             cp_parser_error (parser, "for, while or do statement expected");
33071             return false;
33072           }
33073         cp_parser_iteration_statement (parser, true);
33074         return true;
33075       }
33076
33077     case PRAGMA_CILK_SIMD:
33078       if (context == pragma_external)
33079         {
33080           error_at (pragma_tok->location,
33081                     "%<#pragma simd%> must be inside a function");
33082           break;
33083         }
33084       cp_parser_cilk_simd (parser, pragma_tok);
33085       return true;
33086
33087     case PRAGMA_CILK_GRAINSIZE:
33088       if (context == pragma_external)
33089         {
33090           error_at (pragma_tok->location,
33091                     "%<#pragma cilk grainsize%> must be inside a function");
33092           break;
33093         }
33094
33095       /* Ignore the pragma if Cilk Plus is not enabled.  */
33096       if (flag_cilkplus)
33097         {
33098           cp_parser_cilk_grainsize (parser, pragma_tok);
33099           return true;
33100         }
33101       else
33102         {
33103           error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33104                     "%<#pragma cilk grainsize%>");
33105           break;
33106         }
33107
33108     default:
33109       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33110       c_invoke_pragma_handler (id);
33111       break;
33112
33113     bad_stmt:
33114       cp_parser_error (parser, "expected declaration specifiers");
33115       break;
33116     }
33117
33118   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33119   return false;
33120 }
33121
33122 /* The interface the pragma parsers have to the lexer.  */
33123
33124 enum cpp_ttype
33125 pragma_lex (tree *value)
33126 {
33127   cp_token *tok;
33128   enum cpp_ttype ret;
33129
33130   tok = cp_lexer_peek_token (the_parser->lexer);
33131
33132   ret = tok->type;
33133   *value = tok->u.value;
33134
33135   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33136     ret = CPP_EOF;
33137   else if (ret == CPP_STRING)
33138     *value = cp_parser_string_literal (the_parser, false, false);
33139   else
33140     {
33141       cp_lexer_consume_token (the_parser->lexer);
33142       if (ret == CPP_KEYWORD)
33143         ret = CPP_NAME;
33144     }
33145
33146   return ret;
33147 }
33148
33149 \f
33150 /* External interface.  */
33151
33152 /* Parse one entire translation unit.  */
33153
33154 void
33155 c_parse_file (void)
33156 {
33157   static bool already_called = false;
33158
33159   if (already_called)
33160     fatal_error (input_location,
33161                  "inter-module optimizations not implemented for C++");
33162   already_called = true;
33163
33164   the_parser = cp_parser_new ();
33165   push_deferring_access_checks (flag_access_control
33166                                 ? dk_no_deferred : dk_no_check);
33167   cp_parser_translation_unit (the_parser);
33168   the_parser = NULL;
33169 }
33170
33171 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's 
33172    vectorlength clause:
33173    Syntax:
33174    vectorlength ( constant-expression )  */
33175
33176 static tree
33177 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33178                                   bool is_simd_fn)
33179 {
33180   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33181   tree expr;
33182   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33183      safelen clause.  Thus, vectorlength is represented as OMP 4.0
33184      safelen.  For SIMD-enabled function it is represented by OMP 4.0
33185      simdlen.  */
33186   if (!is_simd_fn)
33187     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", 
33188                                loc);
33189   else
33190     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33191                                loc);
33192
33193   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33194     return error_mark_node;
33195
33196   expr = cp_parser_constant_expression (parser);
33197   expr = maybe_constant_value (expr);
33198
33199   /* If expr == error_mark_node, then don't emit any errors nor
33200      create a clause.  if any of the above functions returns
33201      error mark node then they would have emitted an error message.  */
33202   if (expr == error_mark_node)
33203     ;
33204   else if (!TREE_TYPE (expr)
33205            || !TREE_CONSTANT (expr)
33206            || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33207     error_at (loc, "vectorlength must be an integer constant");
33208   else if (TREE_CONSTANT (expr)
33209            && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33210     error_at (loc, "vectorlength must be a power of 2");
33211   else 
33212     {
33213       tree c;
33214       if (!is_simd_fn)
33215         { 
33216           c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN); 
33217           OMP_CLAUSE_SAFELEN_EXPR (c) = expr; 
33218           OMP_CLAUSE_CHAIN (c) = clauses; 
33219           clauses = c;
33220         }
33221       else
33222         {
33223           c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33224           OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33225           OMP_CLAUSE_CHAIN (c) = clauses;
33226           clauses = c;
33227         }
33228     }
33229
33230   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33231     return error_mark_node;
33232   return clauses;
33233 }
33234
33235 /* Handles the Cilk Plus #pragma simd linear clause.
33236    Syntax:
33237    linear ( simd-linear-variable-list )
33238
33239    simd-linear-variable-list:
33240      simd-linear-variable
33241      simd-linear-variable-list , simd-linear-variable
33242
33243    simd-linear-variable:
33244      id-expression
33245      id-expression : simd-linear-step
33246
33247    simd-linear-step:
33248    conditional-expression */
33249
33250 static tree
33251 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33252 {
33253   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33254
33255   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33256     return clauses;
33257   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33258     {
33259       cp_parser_error (parser, "expected identifier");
33260       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33261       return error_mark_node;
33262     }
33263
33264   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33265   parser->colon_corrects_to_scope_p = false;
33266   while (1)
33267     {
33268       cp_token *token = cp_lexer_peek_token (parser->lexer);
33269       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33270         {
33271           cp_parser_error (parser, "expected variable-name");
33272           clauses = error_mark_node;
33273           break;
33274         }
33275
33276       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33277                                                false, false);
33278       tree decl = cp_parser_lookup_name_simple (parser, var_name,
33279                                                 token->location);
33280       if (decl == error_mark_node)
33281         {
33282           cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33283                                        token->location);
33284           clauses = error_mark_node;
33285         }
33286       else
33287         {
33288           tree e = NULL_TREE;
33289           tree step_size = integer_one_node;
33290
33291           /* If present, parse the linear step.  Otherwise, assume the default
33292              value of 1.  */
33293           if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33294             {
33295               cp_lexer_consume_token (parser->lexer);
33296
33297               e = cp_parser_assignment_expression (parser);
33298               e = maybe_constant_value (e);
33299
33300               if (e == error_mark_node)
33301                 {
33302                   /* If an error has occurred,  then the whole pragma is
33303                      considered ill-formed.  Thus, no reason to keep
33304                      parsing.  */
33305                   clauses = error_mark_node;
33306                   break;
33307                 }
33308               else if (type_dependent_expression_p (e)
33309                        || value_dependent_expression_p (e)
33310                        || (TREE_TYPE (e)
33311                            && INTEGRAL_TYPE_P (TREE_TYPE (e))
33312                            && (TREE_CONSTANT (e)
33313                                || DECL_P (e))))
33314                 step_size = e;
33315               else
33316                 cp_parser_error (parser,
33317                                  "step size must be an integer constant "
33318                                  "expression or an integer variable");
33319             }
33320
33321           /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
33322           tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33323           OMP_CLAUSE_DECL (l) = decl;
33324           OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33325           OMP_CLAUSE_CHAIN (l) = clauses;
33326           clauses = l;
33327         }
33328       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33329         cp_lexer_consume_token (parser->lexer);
33330       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33331         break;
33332       else
33333         {
33334           error_at (cp_lexer_peek_token (parser->lexer)->location,
33335                     "expected %<,%> or %<)%> after %qE", decl);
33336           clauses = error_mark_node;
33337           break;
33338         }
33339     }
33340   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33341   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33342   return clauses;
33343 }
33344
33345 /* Returns the name of the next clause.  If the clause is not
33346    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33347    token is not consumed.  Otherwise, the appropriate enum from the
33348    pragma_simd_clause is returned and the token is consumed.  */
33349
33350 static pragma_omp_clause
33351 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33352 {
33353   pragma_omp_clause clause_type;
33354   cp_token *token = cp_lexer_peek_token (parser->lexer);
33355
33356   if (token->keyword == RID_PRIVATE)
33357     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33358   else if (!token->u.value || token->type != CPP_NAME)
33359     return PRAGMA_CILK_CLAUSE_NONE;
33360   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33361     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33362   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33363     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33364   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33365     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33366   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33367     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33368   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33369     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33370   else
33371     return PRAGMA_CILK_CLAUSE_NONE;
33372
33373   cp_lexer_consume_token (parser->lexer);
33374   return clause_type;
33375 }
33376
33377 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
33378
33379 static tree
33380 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33381 {
33382   tree clauses = NULL_TREE;
33383
33384   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33385          && clauses != error_mark_node)
33386     {
33387       pragma_omp_clause c_kind;
33388       c_kind = cp_parser_cilk_simd_clause_name (parser);
33389       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33390         clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33391       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33392         clauses = cp_parser_cilk_simd_linear (parser, clauses);
33393       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33394         /* Use the OpenMP 4.0 equivalent function.  */
33395         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33396       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33397         /* Use the OpenMP 4.0 equivalent function.  */
33398         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33399                                           clauses);
33400       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33401         /* Use the OMP 4.0 equivalent function.  */
33402         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33403                                           clauses);
33404       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33405         /* Use the OMP 4.0 equivalent function.  */
33406         clauses = cp_parser_omp_clause_reduction (parser, clauses);
33407       else
33408         {
33409           clauses = error_mark_node;
33410           cp_parser_error (parser, "expected %<#pragma simd%> clause");
33411           break;
33412         }
33413     }
33414
33415   cp_parser_skip_to_pragma_eol (parser, pragma_token);
33416
33417   if (clauses == error_mark_node)
33418     return error_mark_node;
33419   else
33420     return c_finish_cilk_clauses (clauses);
33421 }
33422
33423 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
33424
33425 static void
33426 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33427 {
33428   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33429
33430   if (clauses == error_mark_node)
33431     return;
33432
33433   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33434     {
33435       error_at (cp_lexer_peek_token (parser->lexer)->location,
33436                 "for statement expected");
33437       return;
33438     }
33439
33440   tree sb = begin_omp_structured_block ();
33441   int save = cp_parser_begin_omp_structured_block (parser);
33442   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33443   if (ret)
33444     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33445   cp_parser_end_omp_structured_block (parser, save);
33446   add_stmt (finish_omp_structured_block (sb));
33447 }
33448
33449 /* Main entry-point for parsing Cilk Plus _Cilk_for
33450    loops.  The return value is error_mark_node
33451    when errors happen and CILK_FOR tree on success.  */
33452
33453 static tree
33454 cp_parser_cilk_for (cp_parser *parser, tree grain)
33455 {
33456   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33457     gcc_unreachable ();
33458
33459   tree sb = begin_omp_structured_block ();
33460   int save = cp_parser_begin_omp_structured_block (parser);
33461
33462   tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33463   OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33464   OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33465   clauses = finish_omp_clauses (clauses);
33466
33467   tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33468   if (ret)
33469     cpp_validate_cilk_plus_loop (ret);
33470   else
33471     ret = error_mark_node;
33472
33473   cp_parser_end_omp_structured_block (parser, save);
33474   add_stmt (finish_omp_structured_block (sb));
33475   return ret;
33476 }
33477
33478 /* Create an identifier for a generic parameter type (a synthesized
33479    template parameter implied by `auto' or a concept identifier). */
33480
33481 static GTY(()) int generic_parm_count;
33482 static tree
33483 make_generic_type_name ()
33484 {
33485   char buf[32];
33486   sprintf (buf, "auto:%d", ++generic_parm_count);
33487   return get_identifier (buf);
33488 }
33489
33490 /* Predicate that behaves as is_auto_or_concept but matches the parent
33491    node of the generic type rather than the generic type itself.  This
33492    allows for type transformation in add_implicit_template_parms.  */
33493
33494 static inline bool
33495 tree_type_is_auto_or_concept (const_tree t)
33496 {
33497   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33498 }
33499
33500 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33501    (creating a new template parameter list if necessary).  Returns the newly
33502    created template type parm.  */
33503
33504 tree
33505 synthesize_implicit_template_parm  (cp_parser *parser)
33506 {
33507   gcc_assert (current_binding_level->kind == sk_function_parms);
33508
33509   /* We are either continuing a function template that already contains implicit
33510      template parameters, creating a new fully-implicit function template, or
33511      extending an existing explicit function template with implicit template
33512      parameters.  */
33513
33514   cp_binding_level *const entry_scope = current_binding_level;
33515
33516   bool become_template = false;
33517   cp_binding_level *parent_scope = 0;
33518
33519   if (parser->implicit_template_scope)
33520     {
33521       gcc_assert (parser->implicit_template_parms);
33522
33523       current_binding_level = parser->implicit_template_scope;
33524     }
33525   else
33526     {
33527       /* Roll back to the existing template parameter scope (in the case of
33528          extending an explicit function template) or introduce a new template
33529          parameter scope ahead of the function parameter scope (or class scope
33530          in the case of out-of-line member definitions).  The function scope is
33531          added back after template parameter synthesis below.  */
33532
33533       cp_binding_level *scope = entry_scope;
33534
33535       while (scope->kind == sk_function_parms)
33536         {
33537           parent_scope = scope;
33538           scope = scope->level_chain;
33539         }
33540       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33541         {
33542           /* If not defining a class, then any class scope is a scope level in
33543              an out-of-line member definition.  In this case simply wind back
33544              beyond the first such scope to inject the template parameter list.
33545              Otherwise wind back to the class being defined.  The latter can
33546              occur in class member friend declarations such as:
33547
33548                class A {
33549                  void foo (auto);
33550                };
33551                class B {
33552                  friend void A::foo (auto);
33553                };
33554
33555             The template parameter list synthesized for the friend declaration
33556             must be injected in the scope of 'B'.  This can also occur in
33557             erroneous cases such as:
33558
33559                struct A {
33560                  struct B {
33561                    void foo (auto);
33562                  };
33563                  void B::foo (auto) {}
33564                };
33565
33566             Here the attempted definition of 'B::foo' within 'A' is ill-formed
33567             but, nevertheless, the template parameter list synthesized for the
33568             declarator should be injected into the scope of 'A' as if the
33569             ill-formed template was specified explicitly.  */
33570
33571           while (scope->kind == sk_class && !scope->defining_class_p)
33572             {
33573               parent_scope = scope;
33574               scope = scope->level_chain;
33575             }
33576         }
33577
33578       current_binding_level = scope;
33579
33580       if (scope->kind != sk_template_parms
33581           || !function_being_declared_is_template_p (parser))
33582         {
33583           /* Introduce a new template parameter list for implicit template
33584              parameters.  */
33585
33586           become_template = true;
33587
33588           parser->implicit_template_scope
33589               = begin_scope (sk_template_parms, NULL);
33590
33591           ++processing_template_decl;
33592
33593           parser->fully_implicit_function_template_p = true;
33594           ++parser->num_template_parameter_lists;
33595         }
33596       else
33597         {
33598           /* Synthesize implicit template parameters at the end of the explicit
33599              template parameter list.  */
33600
33601           gcc_assert (current_template_parms);
33602
33603           parser->implicit_template_scope = scope;
33604
33605           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33606           parser->implicit_template_parms
33607             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33608         }
33609     }
33610
33611   /* Synthesize a new template parameter and track the current template
33612      parameter chain with implicit_template_parms.  */
33613
33614   tree synth_id = make_generic_type_name ();
33615   tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33616                                                     synth_id);
33617   tree new_parm
33618     = process_template_parm (parser->implicit_template_parms,
33619                              input_location,
33620                              build_tree_list (NULL_TREE, synth_tmpl_parm),
33621                              /*non_type=*/false,
33622                              /*param_pack=*/false);
33623
33624
33625   if (parser->implicit_template_parms)
33626     parser->implicit_template_parms
33627       = TREE_CHAIN (parser->implicit_template_parms);
33628   else
33629     parser->implicit_template_parms = new_parm;
33630
33631   tree new_type = TREE_TYPE (getdecls ());
33632
33633   /* If creating a fully implicit function template, start the new implicit
33634      template parameter list with this synthesized type, otherwise grow the
33635      current template parameter list.  */
33636
33637   if (become_template)
33638     {
33639       parent_scope->level_chain = current_binding_level;
33640
33641       tree new_parms = make_tree_vec (1);
33642       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33643       current_template_parms = tree_cons (size_int (processing_template_decl),
33644                                           new_parms, current_template_parms);
33645     }
33646   else
33647     {
33648       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33649       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33650       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33651       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33652     }
33653
33654   current_binding_level = entry_scope;
33655
33656   return new_type;
33657 }
33658
33659 /* Finish the declaration of a fully implicit function template.  Such a
33660    template has no explicit template parameter list so has not been through the
33661    normal template head and tail processing.  synthesize_implicit_template_parm
33662    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
33663    provided if the declaration is a class member such that its template
33664    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
33665    form is returned.  Otherwise NULL_TREE is returned. */
33666
33667 tree
33668 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33669 {
33670   gcc_assert (parser->fully_implicit_function_template_p);
33671
33672   if (member_decl_opt && member_decl_opt != error_mark_node
33673       && DECL_VIRTUAL_P (member_decl_opt))
33674     {
33675       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33676                 "implicit templates may not be %<virtual%>");
33677       DECL_VIRTUAL_P (member_decl_opt) = false;
33678     }
33679
33680   if (member_decl_opt)
33681     member_decl_opt = finish_member_template_decl (member_decl_opt);
33682   end_template_decl ();
33683
33684   parser->fully_implicit_function_template_p = false;
33685   --parser->num_template_parameter_lists;
33686
33687   return member_decl_opt;
33688 }
33689
33690 #include "gt-cp-parser.h"