Update gcc-50 to SVN version 239798 (gcc-5-branch)
[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_uncommitted_to_tentative_parse_p (parser))
4437           cp_parser_skip_to_end_of_statement (parser);
4438
4439         return expr;
4440       }
4441
4442     case CPP_OPEN_SQUARE:
4443       {
4444         if (c_dialect_objc ())
4445           {
4446             /* We might have an Objective-C++ message. */
4447             cp_parser_parse_tentatively (parser);
4448             tree msg = cp_parser_objc_message_expression (parser);
4449             /* If that works out, we're done ... */
4450             if (cp_parser_parse_definitely (parser))
4451               return msg;
4452             /* ... else, fall though to see if it's a lambda.  */
4453           }
4454         tree lam = cp_parser_lambda_expression (parser);
4455         /* Don't warn about a failed tentative parse.  */
4456         if (cp_parser_error_occurred (parser))
4457           return error_mark_node;
4458         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4459         return lam;
4460       }
4461
4462     case CPP_OBJC_STRING:
4463       if (c_dialect_objc ())
4464         /* We have an Objective-C++ string literal. */
4465         return cp_parser_objc_expression (parser);
4466       cp_parser_error (parser, "expected primary-expression");
4467       return error_mark_node;
4468
4469     case CPP_KEYWORD:
4470       switch (token->keyword)
4471         {
4472           /* These two are the boolean literals.  */
4473         case RID_TRUE:
4474           cp_lexer_consume_token (parser->lexer);
4475           return boolean_true_node;
4476         case RID_FALSE:
4477           cp_lexer_consume_token (parser->lexer);
4478           return boolean_false_node;
4479
4480           /* The `__null' literal.  */
4481         case RID_NULL:
4482           cp_lexer_consume_token (parser->lexer);
4483           return null_node;
4484
4485           /* The `nullptr' literal.  */
4486         case RID_NULLPTR:
4487           cp_lexer_consume_token (parser->lexer);
4488           return nullptr_node;
4489
4490           /* Recognize the `this' keyword.  */
4491         case RID_THIS:
4492           cp_lexer_consume_token (parser->lexer);
4493           if (parser->local_variables_forbidden_p)
4494             {
4495               error_at (token->location,
4496                         "%<this%> may not be used in this context");
4497               return error_mark_node;
4498             }
4499           /* Pointers cannot appear in constant-expressions.  */
4500           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4501             return error_mark_node;
4502           return finish_this_expr ();
4503
4504           /* The `operator' keyword can be the beginning of an
4505              id-expression.  */
4506         case RID_OPERATOR:
4507           goto id_expression;
4508
4509         case RID_FUNCTION_NAME:
4510         case RID_PRETTY_FUNCTION_NAME:
4511         case RID_C99_FUNCTION_NAME:
4512           {
4513             non_integral_constant name;
4514
4515             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4516                __func__ are the names of variables -- but they are
4517                treated specially.  Therefore, they are handled here,
4518                rather than relying on the generic id-expression logic
4519                below.  Grammatically, these names are id-expressions.
4520
4521                Consume the token.  */
4522             token = cp_lexer_consume_token (parser->lexer);
4523
4524             switch (token->keyword)
4525               {
4526               case RID_FUNCTION_NAME:
4527                 name = NIC_FUNC_NAME;
4528                 break;
4529               case RID_PRETTY_FUNCTION_NAME:
4530                 name = NIC_PRETTY_FUNC;
4531                 break;
4532               case RID_C99_FUNCTION_NAME:
4533                 name = NIC_C99_FUNC;
4534                 break;
4535               default:
4536                 gcc_unreachable ();
4537               }
4538
4539             if (cp_parser_non_integral_constant_expression (parser, name))
4540               return error_mark_node;
4541
4542             /* Look up the name.  */
4543             return finish_fname (token->u.value);
4544           }
4545
4546         case RID_VA_ARG:
4547           {
4548             tree expression;
4549             tree type;
4550             source_location type_location;
4551
4552             /* The `__builtin_va_arg' construct is used to handle
4553                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4554             cp_lexer_consume_token (parser->lexer);
4555             /* Look for the opening `('.  */
4556             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4557             /* Now, parse the assignment-expression.  */
4558             expression = cp_parser_assignment_expression (parser);
4559             /* Look for the `,'.  */
4560             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4561             type_location = cp_lexer_peek_token (parser->lexer)->location;
4562             /* Parse the type-id.  */
4563             type = cp_parser_type_id (parser);
4564             /* Look for the closing `)'.  */
4565             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4566             /* Using `va_arg' in a constant-expression is not
4567                allowed.  */
4568             if (cp_parser_non_integral_constant_expression (parser,
4569                                                             NIC_VA_ARG))
4570               return error_mark_node;
4571             return build_x_va_arg (type_location, expression, type);
4572           }
4573
4574         case RID_OFFSETOF:
4575           return cp_parser_builtin_offsetof (parser);
4576
4577         case RID_HAS_NOTHROW_ASSIGN:
4578         case RID_HAS_NOTHROW_CONSTRUCTOR:
4579         case RID_HAS_NOTHROW_COPY:        
4580         case RID_HAS_TRIVIAL_ASSIGN:
4581         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4582         case RID_HAS_TRIVIAL_COPY:        
4583         case RID_HAS_TRIVIAL_DESTRUCTOR:
4584         case RID_HAS_VIRTUAL_DESTRUCTOR:
4585         case RID_IS_ABSTRACT:
4586         case RID_IS_BASE_OF:
4587         case RID_IS_CLASS:
4588         case RID_IS_EMPTY:
4589         case RID_IS_ENUM:
4590         case RID_IS_FINAL:
4591         case RID_IS_LITERAL_TYPE:
4592         case RID_IS_POD:
4593         case RID_IS_POLYMORPHIC:
4594         case RID_IS_STD_LAYOUT:
4595         case RID_IS_TRIVIAL:
4596         case RID_IS_TRIVIALLY_ASSIGNABLE:
4597         case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
4598         case RID_IS_TRIVIALLY_COPYABLE:
4599         case RID_IS_UNION:
4600           return cp_parser_trait_expr (parser, token->keyword);
4601
4602         /* Objective-C++ expressions.  */
4603         case RID_AT_ENCODE:
4604         case RID_AT_PROTOCOL:
4605         case RID_AT_SELECTOR:
4606           return cp_parser_objc_expression (parser);
4607
4608         case RID_TEMPLATE:
4609           if (parser->in_function_body
4610               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4611                   == CPP_LESS))
4612             {
4613               error_at (token->location,
4614                         "a template declaration cannot appear at block scope");
4615               cp_parser_skip_to_end_of_block_or_statement (parser);
4616               return error_mark_node;
4617             }
4618         default:
4619           cp_parser_error (parser, "expected primary-expression");
4620           return error_mark_node;
4621         }
4622
4623       /* An id-expression can start with either an identifier, a
4624          `::' as the beginning of a qualified-id, or the "operator"
4625          keyword.  */
4626     case CPP_NAME:
4627     case CPP_SCOPE:
4628     case CPP_TEMPLATE_ID:
4629     case CPP_NESTED_NAME_SPECIFIER:
4630       {
4631         tree id_expression;
4632         tree decl;
4633         const char *error_msg;
4634         bool template_p;
4635         bool done;
4636         cp_token *id_expr_token;
4637
4638       id_expression:
4639         /* Parse the id-expression.  */
4640         id_expression
4641           = cp_parser_id_expression (parser,
4642                                      /*template_keyword_p=*/false,
4643                                      /*check_dependency_p=*/true,
4644                                      &template_p,
4645                                      /*declarator_p=*/false,
4646                                      /*optional_p=*/false);
4647         if (id_expression == error_mark_node)
4648           return error_mark_node;
4649         id_expr_token = token;
4650         token = cp_lexer_peek_token (parser->lexer);
4651         done = (token->type != CPP_OPEN_SQUARE
4652                 && token->type != CPP_OPEN_PAREN
4653                 && token->type != CPP_DOT
4654                 && token->type != CPP_DEREF
4655                 && token->type != CPP_PLUS_PLUS
4656                 && token->type != CPP_MINUS_MINUS);
4657         /* If we have a template-id, then no further lookup is
4658            required.  If the template-id was for a template-class, we
4659            will sometimes have a TYPE_DECL at this point.  */
4660         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4661                  || TREE_CODE (id_expression) == TYPE_DECL)
4662           decl = id_expression;
4663         /* Look up the name.  */
4664         else
4665           {
4666             tree ambiguous_decls;
4667
4668             /* If we already know that this lookup is ambiguous, then
4669                we've already issued an error message; there's no reason
4670                to check again.  */
4671             if (id_expr_token->type == CPP_NAME
4672                 && id_expr_token->error_reported)
4673               {
4674                 cp_parser_simulate_error (parser);
4675                 return error_mark_node;
4676               }
4677
4678             decl = cp_parser_lookup_name (parser, id_expression,
4679                                           none_type,
4680                                           template_p,
4681                                           /*is_namespace=*/false,
4682                                           /*check_dependency=*/true,
4683                                           &ambiguous_decls,
4684                                           id_expr_token->location);
4685             /* If the lookup was ambiguous, an error will already have
4686                been issued.  */
4687             if (ambiguous_decls)
4688               return error_mark_node;
4689
4690             /* In Objective-C++, we may have an Objective-C 2.0
4691                dot-syntax for classes here.  */
4692             if (c_dialect_objc ()
4693                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4694                 && TREE_CODE (decl) == TYPE_DECL
4695                 && objc_is_class_name (decl))
4696               {
4697                 tree component;
4698                 cp_lexer_consume_token (parser->lexer);
4699                 component = cp_parser_identifier (parser);
4700                 if (component == error_mark_node)
4701                   return error_mark_node;
4702
4703                 return objc_build_class_component_ref (id_expression, component);
4704               }
4705
4706             /* In Objective-C++, an instance variable (ivar) may be preferred
4707                to whatever cp_parser_lookup_name() found.  */
4708             decl = objc_lookup_ivar (decl, id_expression);
4709
4710             /* If name lookup gives us a SCOPE_REF, then the
4711                qualifying scope was dependent.  */
4712             if (TREE_CODE (decl) == SCOPE_REF)
4713               {
4714                 /* At this point, we do not know if DECL is a valid
4715                    integral constant expression.  We assume that it is
4716                    in fact such an expression, so that code like:
4717
4718                       template <int N> struct A {
4719                         int a[B<N>::i];
4720                       };
4721                      
4722                    is accepted.  At template-instantiation time, we
4723                    will check that B<N>::i is actually a constant.  */
4724                 return decl;
4725               }
4726             /* Check to see if DECL is a local variable in a context
4727                where that is forbidden.  */
4728             if (parser->local_variables_forbidden_p
4729                 && local_variable_p (decl))
4730               {
4731                 /* It might be that we only found DECL because we are
4732                    trying to be generous with pre-ISO scoping rules.
4733                    For example, consider:
4734
4735                      int i;
4736                      void g() {
4737                        for (int i = 0; i < 10; ++i) {}
4738                        extern void f(int j = i);
4739                      }
4740
4741                    Here, name look up will originally find the out
4742                    of scope `i'.  We need to issue a warning message,
4743                    but then use the global `i'.  */
4744                 decl = check_for_out_of_scope_variable (decl);
4745                 if (local_variable_p (decl))
4746                   {
4747                     error_at (id_expr_token->location,
4748                               "local variable %qD may not appear in this context",
4749                               decl);
4750                     return error_mark_node;
4751                   }
4752               }
4753           }
4754
4755         decl = (finish_id_expression
4756                 (id_expression, decl, parser->scope,
4757                  idk,
4758                  parser->integral_constant_expression_p,
4759                  parser->allow_non_integral_constant_expression_p,
4760                  &parser->non_integral_constant_expression_p,
4761                  template_p, done, address_p,
4762                  template_arg_p,
4763                  &error_msg,
4764                  id_expr_token->location));
4765         if (error_msg)
4766           cp_parser_error (parser, error_msg);
4767         return decl;
4768       }
4769
4770       /* Anything else is an error.  */
4771     default:
4772       cp_parser_error (parser, "expected primary-expression");
4773       return error_mark_node;
4774     }
4775 }
4776
4777 static inline tree
4778 cp_parser_primary_expression (cp_parser *parser,
4779                               bool address_p,
4780                               bool cast_p,
4781                               bool template_arg_p,
4782                               cp_id_kind *idk)
4783 {
4784   return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4785                                        /*decltype*/false, idk);
4786 }
4787
4788 /* Parse an id-expression.
4789
4790    id-expression:
4791      unqualified-id
4792      qualified-id
4793
4794    qualified-id:
4795      :: [opt] nested-name-specifier template [opt] unqualified-id
4796      :: identifier
4797      :: operator-function-id
4798      :: template-id
4799
4800    Return a representation of the unqualified portion of the
4801    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4802    a `::' or nested-name-specifier.
4803
4804    Often, if the id-expression was a qualified-id, the caller will
4805    want to make a SCOPE_REF to represent the qualified-id.  This
4806    function does not do this in order to avoid wastefully creating
4807    SCOPE_REFs when they are not required.
4808
4809    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4810    `template' keyword.
4811
4812    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4813    uninstantiated templates.
4814
4815    If *TEMPLATE_P is non-NULL, it is set to true iff the
4816    `template' keyword is used to explicitly indicate that the entity
4817    named is a template.
4818
4819    If DECLARATOR_P is true, the id-expression is appearing as part of
4820    a declarator, rather than as part of an expression.  */
4821
4822 static tree
4823 cp_parser_id_expression (cp_parser *parser,
4824                          bool template_keyword_p,
4825                          bool check_dependency_p,
4826                          bool *template_p,
4827                          bool declarator_p,
4828                          bool optional_p)
4829 {
4830   bool global_scope_p;
4831   bool nested_name_specifier_p;
4832
4833   /* Assume the `template' keyword was not used.  */
4834   if (template_p)
4835     *template_p = template_keyword_p;
4836
4837   /* Look for the optional `::' operator.  */
4838   global_scope_p
4839     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4840        != NULL_TREE);
4841   /* Look for the optional nested-name-specifier.  */
4842   nested_name_specifier_p
4843     = (cp_parser_nested_name_specifier_opt (parser,
4844                                             /*typename_keyword_p=*/false,
4845                                             check_dependency_p,
4846                                             /*type_p=*/false,
4847                                             declarator_p)
4848        != NULL_TREE);
4849   /* If there is a nested-name-specifier, then we are looking at
4850      the first qualified-id production.  */
4851   if (nested_name_specifier_p)
4852     {
4853       tree saved_scope;
4854       tree saved_object_scope;
4855       tree saved_qualifying_scope;
4856       tree unqualified_id;
4857       bool is_template;
4858
4859       /* See if the next token is the `template' keyword.  */
4860       if (!template_p)
4861         template_p = &is_template;
4862       *template_p = cp_parser_optional_template_keyword (parser);
4863       /* Name lookup we do during the processing of the
4864          unqualified-id might obliterate SCOPE.  */
4865       saved_scope = parser->scope;
4866       saved_object_scope = parser->object_scope;
4867       saved_qualifying_scope = parser->qualifying_scope;
4868       /* Process the final unqualified-id.  */
4869       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4870                                                  check_dependency_p,
4871                                                  declarator_p,
4872                                                  /*optional_p=*/false);
4873       /* Restore the SAVED_SCOPE for our caller.  */
4874       parser->scope = saved_scope;
4875       parser->object_scope = saved_object_scope;
4876       parser->qualifying_scope = saved_qualifying_scope;
4877
4878       return unqualified_id;
4879     }
4880   /* Otherwise, if we are in global scope, then we are looking at one
4881      of the other qualified-id productions.  */
4882   else if (global_scope_p)
4883     {
4884       cp_token *token;
4885       tree id;
4886
4887       /* Peek at the next token.  */
4888       token = cp_lexer_peek_token (parser->lexer);
4889
4890       /* If it's an identifier, and the next token is not a "<", then
4891          we can avoid the template-id case.  This is an optimization
4892          for this common case.  */
4893       if (token->type == CPP_NAME
4894           && !cp_parser_nth_token_starts_template_argument_list_p
4895                (parser, 2))
4896         return cp_parser_identifier (parser);
4897
4898       cp_parser_parse_tentatively (parser);
4899       /* Try a template-id.  */
4900       id = cp_parser_template_id (parser,
4901                                   /*template_keyword_p=*/false,
4902                                   /*check_dependency_p=*/true,
4903                                   none_type,
4904                                   declarator_p);
4905       /* If that worked, we're done.  */
4906       if (cp_parser_parse_definitely (parser))
4907         return id;
4908
4909       /* Peek at the next token.  (Changes in the token buffer may
4910          have invalidated the pointer obtained above.)  */
4911       token = cp_lexer_peek_token (parser->lexer);
4912
4913       switch (token->type)
4914         {
4915         case CPP_NAME:
4916           return cp_parser_identifier (parser);
4917
4918         case CPP_KEYWORD:
4919           if (token->keyword == RID_OPERATOR)
4920             return cp_parser_operator_function_id (parser);
4921           /* Fall through.  */
4922
4923         default:
4924           cp_parser_error (parser, "expected id-expression");
4925           return error_mark_node;
4926         }
4927     }
4928   else
4929     return cp_parser_unqualified_id (parser, template_keyword_p,
4930                                      /*check_dependency_p=*/true,
4931                                      declarator_p,
4932                                      optional_p);
4933 }
4934
4935 /* Parse an unqualified-id.
4936
4937    unqualified-id:
4938      identifier
4939      operator-function-id
4940      conversion-function-id
4941      ~ class-name
4942      template-id
4943
4944    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4945    keyword, in a construct like `A::template ...'.
4946
4947    Returns a representation of unqualified-id.  For the `identifier'
4948    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4949    production a BIT_NOT_EXPR is returned; the operand of the
4950    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4951    other productions, see the documentation accompanying the
4952    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4953    names are looked up in uninstantiated templates.  If DECLARATOR_P
4954    is true, the unqualified-id is appearing as part of a declarator,
4955    rather than as part of an expression.  */
4956
4957 static tree
4958 cp_parser_unqualified_id (cp_parser* parser,
4959                           bool template_keyword_p,
4960                           bool check_dependency_p,
4961                           bool declarator_p,
4962                           bool optional_p)
4963 {
4964   cp_token *token;
4965
4966   /* Peek at the next token.  */
4967   token = cp_lexer_peek_token (parser->lexer);
4968
4969   switch ((int) token->type)
4970     {
4971     case CPP_NAME:
4972       {
4973         tree id;
4974
4975         /* We don't know yet whether or not this will be a
4976            template-id.  */
4977         cp_parser_parse_tentatively (parser);
4978         /* Try a template-id.  */
4979         id = cp_parser_template_id (parser, template_keyword_p,
4980                                     check_dependency_p,
4981                                     none_type,
4982                                     declarator_p);
4983         /* If it worked, we're done.  */
4984         if (cp_parser_parse_definitely (parser))
4985           return id;
4986         /* Otherwise, it's an ordinary identifier.  */
4987         return cp_parser_identifier (parser);
4988       }
4989
4990     case CPP_TEMPLATE_ID:
4991       return cp_parser_template_id (parser, template_keyword_p,
4992                                     check_dependency_p,
4993                                     none_type,
4994                                     declarator_p);
4995
4996     case CPP_COMPL:
4997       {
4998         tree type_decl;
4999         tree qualifying_scope;
5000         tree object_scope;
5001         tree scope;
5002         bool done;
5003
5004         /* Consume the `~' token.  */
5005         cp_lexer_consume_token (parser->lexer);
5006         /* Parse the class-name.  The standard, as written, seems to
5007            say that:
5008
5009              template <typename T> struct S { ~S (); };
5010              template <typename T> S<T>::~S() {}
5011
5012            is invalid, since `~' must be followed by a class-name, but
5013            `S<T>' is dependent, and so not known to be a class.
5014            That's not right; we need to look in uninstantiated
5015            templates.  A further complication arises from:
5016
5017              template <typename T> void f(T t) {
5018                t.T::~T();
5019              }
5020
5021            Here, it is not possible to look up `T' in the scope of `T'
5022            itself.  We must look in both the current scope, and the
5023            scope of the containing complete expression.
5024
5025            Yet another issue is:
5026
5027              struct S {
5028                int S;
5029                ~S();
5030              };
5031
5032              S::~S() {}
5033
5034            The standard does not seem to say that the `S' in `~S'
5035            should refer to the type `S' and not the data member
5036            `S::S'.  */
5037
5038         /* DR 244 says that we look up the name after the "~" in the
5039            same scope as we looked up the qualifying name.  That idea
5040            isn't fully worked out; it's more complicated than that.  */
5041         scope = parser->scope;
5042         object_scope = parser->object_scope;
5043         qualifying_scope = parser->qualifying_scope;
5044
5045         /* Check for invalid scopes.  */
5046         if (scope == error_mark_node)
5047           {
5048             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5049               cp_lexer_consume_token (parser->lexer);
5050             return error_mark_node;
5051           }
5052         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5053           {
5054             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5055               error_at (token->location,
5056                         "scope %qT before %<~%> is not a class-name",
5057                         scope);
5058             cp_parser_simulate_error (parser);
5059             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5060               cp_lexer_consume_token (parser->lexer);
5061             return error_mark_node;
5062           }
5063         gcc_assert (!scope || TYPE_P (scope));
5064
5065         /* If the name is of the form "X::~X" it's OK even if X is a
5066            typedef.  */
5067         token = cp_lexer_peek_token (parser->lexer);
5068         if (scope
5069             && token->type == CPP_NAME
5070             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5071                 != CPP_LESS)
5072             && (token->u.value == TYPE_IDENTIFIER (scope)
5073                 || (CLASS_TYPE_P (scope)
5074                     && constructor_name_p (token->u.value, scope))))
5075           {
5076             cp_lexer_consume_token (parser->lexer);
5077             return build_nt (BIT_NOT_EXPR, scope);
5078           }
5079
5080         /* ~auto means the destructor of whatever the object is.  */
5081         if (cp_parser_is_keyword (token, RID_AUTO))
5082           {
5083             if (cxx_dialect < cxx14)
5084               pedwarn (input_location, 0,
5085                        "%<~auto%> only available with "
5086                        "-std=c++14 or -std=gnu++14");
5087             cp_lexer_consume_token (parser->lexer);
5088             return build_nt (BIT_NOT_EXPR, make_auto ());
5089           }
5090
5091         /* If there was an explicit qualification (S::~T), first look
5092            in the scope given by the qualification (i.e., S).
5093
5094            Note: in the calls to cp_parser_class_name below we pass
5095            typename_type so that lookup finds the injected-class-name
5096            rather than the constructor.  */
5097         done = false;
5098         type_decl = NULL_TREE;
5099         if (scope)
5100           {
5101             cp_parser_parse_tentatively (parser);
5102             type_decl = cp_parser_class_name (parser,
5103                                               /*typename_keyword_p=*/false,
5104                                               /*template_keyword_p=*/false,
5105                                               typename_type,
5106                                               /*check_dependency=*/false,
5107                                               /*class_head_p=*/false,
5108                                               declarator_p);
5109             if (cp_parser_parse_definitely (parser))
5110               done = true;
5111           }
5112         /* In "N::S::~S", look in "N" as well.  */
5113         if (!done && scope && qualifying_scope)
5114           {
5115             cp_parser_parse_tentatively (parser);
5116             parser->scope = qualifying_scope;
5117             parser->object_scope = NULL_TREE;
5118             parser->qualifying_scope = NULL_TREE;
5119             type_decl
5120               = cp_parser_class_name (parser,
5121                                       /*typename_keyword_p=*/false,
5122                                       /*template_keyword_p=*/false,
5123                                       typename_type,
5124                                       /*check_dependency=*/false,
5125                                       /*class_head_p=*/false,
5126                                       declarator_p);
5127             if (cp_parser_parse_definitely (parser))
5128               done = true;
5129           }
5130         /* In "p->S::~T", look in the scope given by "*p" as well.  */
5131         else if (!done && object_scope)
5132           {
5133             cp_parser_parse_tentatively (parser);
5134             parser->scope = object_scope;
5135             parser->object_scope = NULL_TREE;
5136             parser->qualifying_scope = NULL_TREE;
5137             type_decl
5138               = cp_parser_class_name (parser,
5139                                       /*typename_keyword_p=*/false,
5140                                       /*template_keyword_p=*/false,
5141                                       typename_type,
5142                                       /*check_dependency=*/false,
5143                                       /*class_head_p=*/false,
5144                                       declarator_p);
5145             if (cp_parser_parse_definitely (parser))
5146               done = true;
5147           }
5148         /* Look in the surrounding context.  */
5149         if (!done)
5150           {
5151             parser->scope = NULL_TREE;
5152             parser->object_scope = NULL_TREE;
5153             parser->qualifying_scope = NULL_TREE;
5154             if (processing_template_decl)
5155               cp_parser_parse_tentatively (parser);
5156             type_decl
5157               = cp_parser_class_name (parser,
5158                                       /*typename_keyword_p=*/false,
5159                                       /*template_keyword_p=*/false,
5160                                       typename_type,
5161                                       /*check_dependency=*/false,
5162                                       /*class_head_p=*/false,
5163                                       declarator_p);
5164             if (processing_template_decl
5165                 && ! cp_parser_parse_definitely (parser))
5166               {
5167                 /* We couldn't find a type with this name, so just accept
5168                    it and check for a match at instantiation time.  */
5169                 type_decl = cp_parser_identifier (parser);
5170                 if (type_decl != error_mark_node)
5171                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5172                 return type_decl;
5173               }
5174           }
5175         /* If an error occurred, assume that the name of the
5176            destructor is the same as the name of the qualifying
5177            class.  That allows us to keep parsing after running
5178            into ill-formed destructor names.  */
5179         if (type_decl == error_mark_node && scope)
5180           return build_nt (BIT_NOT_EXPR, scope);
5181         else if (type_decl == error_mark_node)
5182           return error_mark_node;
5183
5184         /* Check that destructor name and scope match.  */
5185         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5186           {
5187             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5188               error_at (token->location,
5189                         "declaration of %<~%T%> as member of %qT",
5190                         type_decl, scope);
5191             cp_parser_simulate_error (parser);
5192             return error_mark_node;
5193           }
5194
5195         /* [class.dtor]
5196
5197            A typedef-name that names a class shall not be used as the
5198            identifier in the declarator for a destructor declaration.  */
5199         if (declarator_p
5200             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5201             && !DECL_SELF_REFERENCE_P (type_decl)
5202             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5203           error_at (token->location,
5204                     "typedef-name %qD used as destructor declarator",
5205                     type_decl);
5206
5207         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5208       }
5209
5210     case CPP_KEYWORD:
5211       if (token->keyword == RID_OPERATOR)
5212         {
5213           tree id;
5214
5215           /* This could be a template-id, so we try that first.  */
5216           cp_parser_parse_tentatively (parser);
5217           /* Try a template-id.  */
5218           id = cp_parser_template_id (parser, template_keyword_p,
5219                                       /*check_dependency_p=*/true,
5220                                       none_type,
5221                                       declarator_p);
5222           /* If that worked, we're done.  */
5223           if (cp_parser_parse_definitely (parser))
5224             return id;
5225           /* We still don't know whether we're looking at an
5226              operator-function-id or a conversion-function-id.  */
5227           cp_parser_parse_tentatively (parser);
5228           /* Try an operator-function-id.  */
5229           id = cp_parser_operator_function_id (parser);
5230           /* If that didn't work, try a conversion-function-id.  */
5231           if (!cp_parser_parse_definitely (parser))
5232             id = cp_parser_conversion_function_id (parser);
5233           else if (UDLIT_OPER_P (id))
5234             {
5235               /* 17.6.3.3.5  */
5236               const char *name = UDLIT_OP_SUFFIX (id);
5237               if (name[0] != '_' && !in_system_header_at (input_location)
5238                   && declarator_p)
5239                 warning (0, "literal operator suffixes not preceded by %<_%>"
5240                             " are reserved for future standardization");
5241             }
5242
5243           return id;
5244         }
5245       /* Fall through.  */
5246
5247     default:
5248       if (optional_p)
5249         return NULL_TREE;
5250       cp_parser_error (parser, "expected unqualified-id");
5251       return error_mark_node;
5252     }
5253 }
5254
5255 /* Parse an (optional) nested-name-specifier.
5256
5257    nested-name-specifier: [C++98]
5258      class-or-namespace-name :: nested-name-specifier [opt]
5259      class-or-namespace-name :: template nested-name-specifier [opt]
5260
5261    nested-name-specifier: [C++0x]
5262      type-name ::
5263      namespace-name ::
5264      nested-name-specifier identifier ::
5265      nested-name-specifier template [opt] simple-template-id ::
5266
5267    PARSER->SCOPE should be set appropriately before this function is
5268    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5269    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
5270    in name lookups.
5271
5272    Sets PARSER->SCOPE to the class (TYPE) or namespace
5273    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5274    it unchanged if there is no nested-name-specifier.  Returns the new
5275    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5276
5277    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5278    part of a declaration and/or decl-specifier.  */
5279
5280 static tree
5281 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5282                                      bool typename_keyword_p,
5283                                      bool check_dependency_p,
5284                                      bool type_p,
5285                                      bool is_declaration)
5286 {
5287   bool success = false;
5288   cp_token_position start = 0;
5289   cp_token *token;
5290
5291   /* Remember where the nested-name-specifier starts.  */
5292   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5293     {
5294       start = cp_lexer_token_position (parser->lexer, false);
5295       push_deferring_access_checks (dk_deferred);
5296     }
5297
5298   while (true)
5299     {
5300       tree new_scope;
5301       tree old_scope;
5302       tree saved_qualifying_scope;
5303       bool template_keyword_p;
5304
5305       /* Spot cases that cannot be the beginning of a
5306          nested-name-specifier.  */
5307       token = cp_lexer_peek_token (parser->lexer);
5308
5309       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5310          the already parsed nested-name-specifier.  */
5311       if (token->type == CPP_NESTED_NAME_SPECIFIER)
5312         {
5313           /* Grab the nested-name-specifier and continue the loop.  */
5314           cp_parser_pre_parsed_nested_name_specifier (parser);
5315           /* If we originally encountered this nested-name-specifier
5316              with IS_DECLARATION set to false, we will not have
5317              resolved TYPENAME_TYPEs, so we must do so here.  */
5318           if (is_declaration
5319               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5320             {
5321               new_scope = resolve_typename_type (parser->scope,
5322                                                  /*only_current_p=*/false);
5323               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5324                 parser->scope = new_scope;
5325             }
5326           success = true;
5327           continue;
5328         }
5329
5330       /* Spot cases that cannot be the beginning of a
5331          nested-name-specifier.  On the second and subsequent times
5332          through the loop, we look for the `template' keyword.  */
5333       if (success && token->keyword == RID_TEMPLATE)
5334         ;
5335       /* A template-id can start a nested-name-specifier.  */
5336       else if (token->type == CPP_TEMPLATE_ID)
5337         ;
5338       /* DR 743: decltype can be used in a nested-name-specifier.  */
5339       else if (token_is_decltype (token))
5340         ;
5341       else
5342         {
5343           /* If the next token is not an identifier, then it is
5344              definitely not a type-name or namespace-name.  */
5345           if (token->type != CPP_NAME)
5346             break;
5347           /* If the following token is neither a `<' (to begin a
5348              template-id), nor a `::', then we are not looking at a
5349              nested-name-specifier.  */
5350           token = cp_lexer_peek_nth_token (parser->lexer, 2);
5351
5352           if (token->type == CPP_COLON
5353               && parser->colon_corrects_to_scope_p
5354               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5355             {
5356               error_at (token->location,
5357                         "found %<:%> in nested-name-specifier, expected %<::%>");
5358               token->type = CPP_SCOPE;
5359             }
5360
5361           if (token->type != CPP_SCOPE
5362               && !cp_parser_nth_token_starts_template_argument_list_p
5363                   (parser, 2))
5364             break;
5365         }
5366
5367       /* The nested-name-specifier is optional, so we parse
5368          tentatively.  */
5369       cp_parser_parse_tentatively (parser);
5370
5371       /* Look for the optional `template' keyword, if this isn't the
5372          first time through the loop.  */
5373       if (success)
5374         template_keyword_p = cp_parser_optional_template_keyword (parser);
5375       else
5376         template_keyword_p = false;
5377
5378       /* Save the old scope since the name lookup we are about to do
5379          might destroy it.  */
5380       old_scope = parser->scope;
5381       saved_qualifying_scope = parser->qualifying_scope;
5382       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5383          look up names in "X<T>::I" in order to determine that "Y" is
5384          a template.  So, if we have a typename at this point, we make
5385          an effort to look through it.  */
5386       if (is_declaration
5387           && !typename_keyword_p
5388           && parser->scope
5389           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5390         parser->scope = resolve_typename_type (parser->scope,
5391                                                /*only_current_p=*/false);
5392       /* Parse the qualifying entity.  */
5393       new_scope
5394         = cp_parser_qualifying_entity (parser,
5395                                        typename_keyword_p,
5396                                        template_keyword_p,
5397                                        check_dependency_p,
5398                                        type_p,
5399                                        is_declaration);
5400       /* Look for the `::' token.  */
5401       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5402
5403       /* If we found what we wanted, we keep going; otherwise, we're
5404          done.  */
5405       if (!cp_parser_parse_definitely (parser))
5406         {
5407           bool error_p = false;
5408
5409           /* Restore the OLD_SCOPE since it was valid before the
5410              failed attempt at finding the last
5411              class-or-namespace-name.  */
5412           parser->scope = old_scope;
5413           parser->qualifying_scope = saved_qualifying_scope;
5414
5415           /* If the next token is a decltype, and the one after that is a
5416              `::', then the decltype has failed to resolve to a class or
5417              enumeration type.  Give this error even when parsing
5418              tentatively since it can't possibly be valid--and we're going
5419              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5420              won't get another chance.*/
5421           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5422               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5423                   == CPP_SCOPE))
5424             {
5425               token = cp_lexer_consume_token (parser->lexer);
5426               error_at (token->location, "decltype evaluates to %qT, "
5427                         "which is not a class or enumeration type",
5428                         token->u.value);
5429               parser->scope = error_mark_node;
5430               error_p = true;
5431               /* As below.  */
5432               success = true;
5433               cp_lexer_consume_token (parser->lexer);
5434             }
5435
5436           if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5437               && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5438             {
5439               /* If we have a non-type template-id followed by ::, it can't
5440                  possibly be valid.  */
5441               token = cp_lexer_peek_token (parser->lexer);
5442               tree tid = token->u.tree_check_value->value;
5443               if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5444                   && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5445                 {
5446                   tree tmpl = NULL_TREE;
5447                   if (is_overloaded_fn (tid))
5448                     {
5449                       tree fns = get_fns (tid);
5450                       if (!OVL_CHAIN (fns))
5451                         tmpl = OVL_CURRENT (fns);
5452                       error_at (token->location, "function template-id %qD "
5453                                 "in nested-name-specifier", tid);
5454                     }
5455                   else
5456                     {
5457                       /* Variable template.  */
5458                       tmpl = TREE_OPERAND (tid, 0);
5459                       gcc_assert (variable_template_p (tmpl));
5460                       error_at (token->location, "variable template-id %qD "
5461                                 "in nested-name-specifier", tid);
5462                     }
5463                   if (tmpl)
5464                     inform (DECL_SOURCE_LOCATION (tmpl),
5465                             "%qD declared here", tmpl);
5466
5467                   parser->scope = error_mark_node;
5468                   error_p = true;
5469                   /* As below.  */
5470                   success = true;
5471                   cp_lexer_consume_token (parser->lexer);
5472                   cp_lexer_consume_token (parser->lexer);
5473                 }
5474             }
5475
5476           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5477             break;
5478           /* If the next token is an identifier, and the one after
5479              that is a `::', then any valid interpretation would have
5480              found a class-or-namespace-name.  */
5481           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5482                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5483                      == CPP_SCOPE)
5484                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5485                      != CPP_COMPL))
5486             {
5487               token = cp_lexer_consume_token (parser->lexer);
5488               if (!error_p)
5489                 {
5490                   if (!token->error_reported)
5491                     {
5492                       tree decl;
5493                       tree ambiguous_decls;
5494
5495                       decl = cp_parser_lookup_name (parser, token->u.value,
5496                                                     none_type,
5497                                                     /*is_template=*/false,
5498                                                     /*is_namespace=*/false,
5499                                                     /*check_dependency=*/true,
5500                                                     &ambiguous_decls,
5501                                                     token->location);
5502                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5503                         error_at (token->location,
5504                                   "%qD used without template parameters",
5505                                   decl);
5506                       else if (ambiguous_decls)
5507                         {
5508                           // cp_parser_lookup_name has the same diagnostic,
5509                           // thus make sure to emit it at most once.
5510                           if (cp_parser_uncommitted_to_tentative_parse_p
5511                               (parser))
5512                             {
5513                               error_at (token->location,
5514                                         "reference to %qD is ambiguous",
5515                                         token->u.value);
5516                               print_candidates (ambiguous_decls);
5517                             }
5518                           decl = error_mark_node;
5519                         }
5520                       else
5521                         {
5522                           if (cxx_dialect != cxx98)
5523                             cp_parser_name_lookup_error
5524                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5525                              token->location);
5526                           else
5527                             cp_parser_name_lookup_error
5528                             (parser, token->u.value, decl, NLE_CXX98,
5529                              token->location);
5530                         }
5531                     }
5532                   parser->scope = error_mark_node;
5533                   error_p = true;
5534                   /* Treat this as a successful nested-name-specifier
5535                      due to:
5536
5537                      [basic.lookup.qual]
5538
5539                      If the name found is not a class-name (clause
5540                      _class_) or namespace-name (_namespace.def_), the
5541                      program is ill-formed.  */
5542                   success = true;
5543                 }
5544               cp_lexer_consume_token (parser->lexer);
5545             }
5546           break;
5547         }
5548       /* We've found one valid nested-name-specifier.  */
5549       success = true;
5550       /* Name lookup always gives us a DECL.  */
5551       if (TREE_CODE (new_scope) == TYPE_DECL)
5552         new_scope = TREE_TYPE (new_scope);
5553       /* Uses of "template" must be followed by actual templates.  */
5554       if (template_keyword_p
5555           && !(CLASS_TYPE_P (new_scope)
5556                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5557                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5558                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5559           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5560                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5561                    == TEMPLATE_ID_EXPR)))
5562         permerror (input_location, TYPE_P (new_scope)
5563                    ? G_("%qT is not a template")
5564                    : G_("%qD is not a template"),
5565                    new_scope);
5566       /* If it is a class scope, try to complete it; we are about to
5567          be looking up names inside the class.  */
5568       if (TYPE_P (new_scope)
5569           /* Since checking types for dependency can be expensive,
5570              avoid doing it if the type is already complete.  */
5571           && !COMPLETE_TYPE_P (new_scope)
5572           /* Do not try to complete dependent types.  */
5573           && !dependent_type_p (new_scope))
5574         {
5575           new_scope = complete_type (new_scope);
5576           /* If it is a typedef to current class, use the current
5577              class instead, as the typedef won't have any names inside
5578              it yet.  */
5579           if (!COMPLETE_TYPE_P (new_scope)
5580               && currently_open_class (new_scope))
5581             new_scope = TYPE_MAIN_VARIANT (new_scope);
5582         }
5583       /* Make sure we look in the right scope the next time through
5584          the loop.  */
5585       parser->scope = new_scope;
5586     }
5587
5588   /* If parsing tentatively, replace the sequence of tokens that makes
5589      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5590      token.  That way, should we re-parse the token stream, we will
5591      not have to repeat the effort required to do the parse, nor will
5592      we issue duplicate error messages.  */
5593   if (success && start)
5594     {
5595       cp_token *token;
5596
5597       token = cp_lexer_token_at (parser->lexer, start);
5598       /* Reset the contents of the START token.  */
5599       token->type = CPP_NESTED_NAME_SPECIFIER;
5600       /* Retrieve any deferred checks.  Do not pop this access checks yet
5601          so the memory will not be reclaimed during token replacing below.  */
5602       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5603       token->u.tree_check_value->value = parser->scope;
5604       token->u.tree_check_value->checks = get_deferred_access_checks ();
5605       token->u.tree_check_value->qualifying_scope =
5606         parser->qualifying_scope;
5607       token->keyword = RID_MAX;
5608
5609       /* Purge all subsequent tokens.  */
5610       cp_lexer_purge_tokens_after (parser->lexer, start);
5611     }
5612
5613   if (start)
5614     pop_to_parent_deferring_access_checks ();
5615
5616   return success ? parser->scope : NULL_TREE;
5617 }
5618
5619 /* Parse a nested-name-specifier.  See
5620    cp_parser_nested_name_specifier_opt for details.  This function
5621    behaves identically, except that it will an issue an error if no
5622    nested-name-specifier is present.  */
5623
5624 static tree
5625 cp_parser_nested_name_specifier (cp_parser *parser,
5626                                  bool typename_keyword_p,
5627                                  bool check_dependency_p,
5628                                  bool type_p,
5629                                  bool is_declaration)
5630 {
5631   tree scope;
5632
5633   /* Look for the nested-name-specifier.  */
5634   scope = cp_parser_nested_name_specifier_opt (parser,
5635                                                typename_keyword_p,
5636                                                check_dependency_p,
5637                                                type_p,
5638                                                is_declaration);
5639   /* If it was not present, issue an error message.  */
5640   if (!scope)
5641     {
5642       cp_parser_error (parser, "expected nested-name-specifier");
5643       parser->scope = NULL_TREE;
5644     }
5645
5646   return scope;
5647 }
5648
5649 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5650    this is either a class-name or a namespace-name (which corresponds
5651    to the class-or-namespace-name production in the grammar). For
5652    C++0x, it can also be a type-name that refers to an enumeration
5653    type or a simple-template-id.
5654
5655    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5656    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5657    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5658    TYPE_P is TRUE iff the next name should be taken as a class-name,
5659    even the same name is declared to be another entity in the same
5660    scope.
5661
5662    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5663    specified by the class-or-namespace-name.  If neither is found the
5664    ERROR_MARK_NODE is returned.  */
5665
5666 static tree
5667 cp_parser_qualifying_entity (cp_parser *parser,
5668                              bool typename_keyword_p,
5669                              bool template_keyword_p,
5670                              bool check_dependency_p,
5671                              bool type_p,
5672                              bool is_declaration)
5673 {
5674   tree saved_scope;
5675   tree saved_qualifying_scope;
5676   tree saved_object_scope;
5677   tree scope;
5678   bool only_class_p;
5679   bool successful_parse_p;
5680
5681   /* DR 743: decltype can appear in a nested-name-specifier.  */
5682   if (cp_lexer_next_token_is_decltype (parser->lexer))
5683     {
5684       scope = cp_parser_decltype (parser);
5685       if (TREE_CODE (scope) != ENUMERAL_TYPE
5686           && !MAYBE_CLASS_TYPE_P (scope))
5687         {
5688           cp_parser_simulate_error (parser);
5689           return error_mark_node;
5690         }
5691       if (TYPE_NAME (scope))
5692         scope = TYPE_NAME (scope);
5693       return scope;
5694     }
5695
5696   /* Before we try to parse the class-name, we must save away the
5697      current PARSER->SCOPE since cp_parser_class_name will destroy
5698      it.  */
5699   saved_scope = parser->scope;
5700   saved_qualifying_scope = parser->qualifying_scope;
5701   saved_object_scope = parser->object_scope;
5702   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5703      there is no need to look for a namespace-name.  */
5704   only_class_p = template_keyword_p 
5705     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5706   if (!only_class_p)
5707     cp_parser_parse_tentatively (parser);
5708   scope = cp_parser_class_name (parser,
5709                                 typename_keyword_p,
5710                                 template_keyword_p,
5711                                 type_p ? class_type : none_type,
5712                                 check_dependency_p,
5713                                 /*class_head_p=*/false,
5714                                 is_declaration);
5715   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5716   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5717   if (!only_class_p 
5718       && cxx_dialect != cxx98
5719       && !successful_parse_p)
5720     {
5721       /* Restore the saved scope.  */
5722       parser->scope = saved_scope;
5723       parser->qualifying_scope = saved_qualifying_scope;
5724       parser->object_scope = saved_object_scope;
5725
5726       /* Parse tentatively.  */
5727       cp_parser_parse_tentatively (parser);
5728      
5729       /* Parse a type-name  */
5730       scope = cp_parser_type_name (parser);
5731
5732       /* "If the name found does not designate a namespace or a class,
5733          enumeration, or dependent type, the program is ill-formed."
5734
5735          We cover classes and dependent types above and namespaces below,
5736          so this code is only looking for enums.  */
5737       if (!scope || TREE_CODE (scope) != TYPE_DECL
5738           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5739         cp_parser_simulate_error (parser);
5740
5741       successful_parse_p = cp_parser_parse_definitely (parser);
5742     }
5743   /* If that didn't work, try for a namespace-name.  */
5744   if (!only_class_p && !successful_parse_p)
5745     {
5746       /* Restore the saved scope.  */
5747       parser->scope = saved_scope;
5748       parser->qualifying_scope = saved_qualifying_scope;
5749       parser->object_scope = saved_object_scope;
5750       /* If we are not looking at an identifier followed by the scope
5751          resolution operator, then this is not part of a
5752          nested-name-specifier.  (Note that this function is only used
5753          to parse the components of a nested-name-specifier.)  */
5754       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5755           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5756         return error_mark_node;
5757       scope = cp_parser_namespace_name (parser);
5758     }
5759
5760   return scope;
5761 }
5762
5763 /* Return true if we are looking at a compound-literal, false otherwise.  */
5764
5765 static bool
5766 cp_parser_compound_literal_p (cp_parser *parser)
5767 {
5768   /* Consume the `('.  */
5769   cp_lexer_consume_token (parser->lexer);
5770
5771   cp_lexer_save_tokens (parser->lexer);
5772
5773   /* Skip tokens until the next token is a closing parenthesis.
5774      If we find the closing `)', and the next token is a `{', then
5775      we are looking at a compound-literal.  */
5776   bool compound_literal_p
5777     = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5778                                               /*consume_paren=*/true)
5779        && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5780   
5781   /* Roll back the tokens we skipped.  */
5782   cp_lexer_rollback_tokens (parser->lexer);
5783
5784   return compound_literal_p;
5785 }
5786
5787 /* Parse a postfix-expression.
5788
5789    postfix-expression:
5790      primary-expression
5791      postfix-expression [ expression ]
5792      postfix-expression ( expression-list [opt] )
5793      simple-type-specifier ( expression-list [opt] )
5794      typename :: [opt] nested-name-specifier identifier
5795        ( expression-list [opt] )
5796      typename :: [opt] nested-name-specifier template [opt] template-id
5797        ( expression-list [opt] )
5798      postfix-expression . template [opt] id-expression
5799      postfix-expression -> template [opt] id-expression
5800      postfix-expression . pseudo-destructor-name
5801      postfix-expression -> pseudo-destructor-name
5802      postfix-expression ++
5803      postfix-expression --
5804      dynamic_cast < type-id > ( expression )
5805      static_cast < type-id > ( expression )
5806      reinterpret_cast < type-id > ( expression )
5807      const_cast < type-id > ( expression )
5808      typeid ( expression )
5809      typeid ( type-id )
5810
5811    GNU Extension:
5812
5813    postfix-expression:
5814      ( type-id ) { initializer-list , [opt] }
5815
5816    This extension is a GNU version of the C99 compound-literal
5817    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5818    but they are essentially the same concept.)
5819
5820    If ADDRESS_P is true, the postfix expression is the operand of the
5821    `&' operator.  CAST_P is true if this expression is the target of a
5822    cast.
5823
5824    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5825    class member access expressions [expr.ref].
5826
5827    Returns a representation of the expression.  */
5828
5829 static tree
5830 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5831                               bool member_access_only_p, bool decltype_p,
5832                               cp_id_kind * pidk_return)
5833 {
5834   cp_token *token;
5835   location_t loc;
5836   enum rid keyword;
5837   cp_id_kind idk = CP_ID_KIND_NONE;
5838   tree postfix_expression = NULL_TREE;
5839   bool is_member_access = false;
5840   int saved_in_statement = -1;
5841
5842   /* Peek at the next token.  */
5843   token = cp_lexer_peek_token (parser->lexer);
5844   loc = token->location;
5845   /* Some of the productions are determined by keywords.  */
5846   keyword = token->keyword;
5847   switch (keyword)
5848     {
5849     case RID_DYNCAST:
5850     case RID_STATCAST:
5851     case RID_REINTCAST:
5852     case RID_CONSTCAST:
5853       {
5854         tree type;
5855         tree expression;
5856         const char *saved_message;
5857         bool saved_in_type_id_in_expr_p;
5858
5859         /* All of these can be handled in the same way from the point
5860            of view of parsing.  Begin by consuming the token
5861            identifying the cast.  */
5862         cp_lexer_consume_token (parser->lexer);
5863
5864         /* New types cannot be defined in the cast.  */
5865         saved_message = parser->type_definition_forbidden_message;
5866         parser->type_definition_forbidden_message
5867           = G_("types may not be defined in casts");
5868
5869         /* Look for the opening `<'.  */
5870         cp_parser_require (parser, CPP_LESS, RT_LESS);
5871         /* Parse the type to which we are casting.  */
5872         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5873         parser->in_type_id_in_expr_p = true;
5874         type = cp_parser_type_id (parser);
5875         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5876         /* Look for the closing `>'.  */
5877         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5878         /* Restore the old message.  */
5879         parser->type_definition_forbidden_message = saved_message;
5880
5881         bool saved_greater_than_is_operator_p
5882           = parser->greater_than_is_operator_p;
5883         parser->greater_than_is_operator_p = true;
5884
5885         /* And the expression which is being cast.  */
5886         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5887         expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5888         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5889
5890         parser->greater_than_is_operator_p
5891           = saved_greater_than_is_operator_p;
5892
5893         /* Only type conversions to integral or enumeration types
5894            can be used in constant-expressions.  */
5895         if (!cast_valid_in_integral_constant_expression_p (type)
5896             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5897           return error_mark_node;
5898
5899         switch (keyword)
5900           {
5901           case RID_DYNCAST:
5902             postfix_expression
5903               = build_dynamic_cast (type, expression, tf_warning_or_error);
5904             break;
5905           case RID_STATCAST:
5906             postfix_expression
5907               = build_static_cast (type, expression, tf_warning_or_error);
5908             break;
5909           case RID_REINTCAST:
5910             postfix_expression
5911               = build_reinterpret_cast (type, expression, 
5912                                         tf_warning_or_error);
5913             break;
5914           case RID_CONSTCAST:
5915             postfix_expression
5916               = build_const_cast (type, expression, tf_warning_or_error);
5917             break;
5918           default:
5919             gcc_unreachable ();
5920           }
5921       }
5922       break;
5923
5924     case RID_TYPEID:
5925       {
5926         tree type;
5927         const char *saved_message;
5928         bool saved_in_type_id_in_expr_p;
5929
5930         /* Consume the `typeid' token.  */
5931         cp_lexer_consume_token (parser->lexer);
5932         /* Look for the `(' token.  */
5933         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5934         /* Types cannot be defined in a `typeid' expression.  */
5935         saved_message = parser->type_definition_forbidden_message;
5936         parser->type_definition_forbidden_message
5937           = G_("types may not be defined in a %<typeid%> expression");
5938         /* We can't be sure yet whether we're looking at a type-id or an
5939            expression.  */
5940         cp_parser_parse_tentatively (parser);
5941         /* Try a type-id first.  */
5942         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5943         parser->in_type_id_in_expr_p = true;
5944         type = cp_parser_type_id (parser);
5945         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5946         /* Look for the `)' token.  Otherwise, we can't be sure that
5947            we're not looking at an expression: consider `typeid (int
5948            (3))', for example.  */
5949         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5950         /* If all went well, simply lookup the type-id.  */
5951         if (cp_parser_parse_definitely (parser))
5952           postfix_expression = get_typeid (type, tf_warning_or_error);
5953         /* Otherwise, fall back to the expression variant.  */
5954         else
5955           {
5956             tree expression;
5957
5958             /* Look for an expression.  */
5959             expression = cp_parser_expression (parser, & idk);
5960             /* Compute its typeid.  */
5961             postfix_expression = build_typeid (expression, tf_warning_or_error);
5962             /* Look for the `)' token.  */
5963             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5964           }
5965         /* Restore the saved message.  */
5966         parser->type_definition_forbidden_message = saved_message;
5967         /* `typeid' may not appear in an integral constant expression.  */
5968         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5969           return error_mark_node;
5970       }
5971       break;
5972
5973     case RID_TYPENAME:
5974       {
5975         tree type;
5976         /* The syntax permitted here is the same permitted for an
5977            elaborated-type-specifier.  */
5978         type = cp_parser_elaborated_type_specifier (parser,
5979                                                     /*is_friend=*/false,
5980                                                     /*is_declaration=*/false);
5981         postfix_expression = cp_parser_functional_cast (parser, type);
5982       }
5983       break;
5984
5985     case RID_CILK_SPAWN:
5986       {
5987         cp_lexer_consume_token (parser->lexer);
5988         token = cp_lexer_peek_token (parser->lexer);
5989         if (token->type == CPP_SEMICOLON)
5990           {
5991             error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5992                       "an expression");
5993             postfix_expression = error_mark_node;
5994             break;
5995           }
5996         else if (!current_function_decl)
5997           {
5998             error_at (token->location, "%<_Cilk_spawn%> may only be used "
5999                       "inside a function");
6000             postfix_expression = error_mark_node;
6001             break;
6002           }
6003         else
6004           {
6005             /* Consecutive _Cilk_spawns are not allowed in a statement.  */
6006             saved_in_statement = parser->in_statement;
6007             parser->in_statement |= IN_CILK_SPAWN;
6008           }
6009         cfun->calls_cilk_spawn = 1;
6010         postfix_expression = 
6011           cp_parser_postfix_expression (parser, false, false, 
6012                                         false, false, &idk);
6013         if (!flag_cilkplus)
6014           {
6015             error_at (token->location, "-fcilkplus must be enabled to use"
6016                       " %<_Cilk_spawn%>");
6017             cfun->calls_cilk_spawn = 0;
6018           }
6019         else if (saved_in_statement & IN_CILK_SPAWN)
6020           {
6021             error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6022                       "are not permitted");
6023             postfix_expression = error_mark_node;
6024             cfun->calls_cilk_spawn = 0; 
6025           }
6026         else
6027           {
6028             postfix_expression = build_cilk_spawn (token->location, 
6029                                                    postfix_expression);
6030             if (postfix_expression != error_mark_node) 
6031               SET_EXPR_LOCATION (postfix_expression, input_location);
6032             parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6033           }
6034         break;
6035       }
6036
6037     case RID_BUILTIN_SHUFFLE:
6038       {
6039         vec<tree, va_gc> *vec;
6040         unsigned int i;
6041         tree p;
6042
6043         cp_lexer_consume_token (parser->lexer);
6044         vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6045                     /*cast_p=*/false, /*allow_expansion_p=*/true,
6046                     /*non_constant_p=*/NULL);
6047         if (vec == NULL)
6048           return error_mark_node;
6049
6050         FOR_EACH_VEC_ELT (*vec, i, p)
6051           mark_exp_read (p);
6052
6053         if (vec->length () == 2)
6054           return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6055                                          tf_warning_or_error);
6056         else if (vec->length () == 3)
6057           return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6058                                          tf_warning_or_error);
6059         else
6060         {
6061           error_at (loc, "wrong number of arguments to "
6062               "%<__builtin_shuffle%>");
6063           return error_mark_node;
6064         }
6065         break;
6066       }
6067
6068     default:
6069       {
6070         tree type;
6071
6072         /* If the next thing is a simple-type-specifier, we may be
6073            looking at a functional cast.  We could also be looking at
6074            an id-expression.  So, we try the functional cast, and if
6075            that doesn't work we fall back to the primary-expression.  */
6076         cp_parser_parse_tentatively (parser);
6077         /* Look for the simple-type-specifier.  */
6078         type = cp_parser_simple_type_specifier (parser,
6079                                                 /*decl_specs=*/NULL,
6080                                                 CP_PARSER_FLAGS_NONE);
6081         /* Parse the cast itself.  */
6082         if (!cp_parser_error_occurred (parser))
6083           postfix_expression
6084             = cp_parser_functional_cast (parser, type);
6085         /* If that worked, we're done.  */
6086         if (cp_parser_parse_definitely (parser))
6087           break;
6088
6089         /* If the functional-cast didn't work out, try a
6090            compound-literal.  */
6091         if (cp_parser_allow_gnu_extensions_p (parser)
6092             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6093           {
6094             tree initializer = NULL_TREE;
6095
6096             cp_parser_parse_tentatively (parser);
6097
6098             /* Avoid calling cp_parser_type_id pointlessly, see comment
6099                in cp_parser_cast_expression about c++/29234.  */
6100             if (!cp_parser_compound_literal_p (parser))
6101               cp_parser_simulate_error (parser);
6102             else
6103               {
6104                 /* Parse the type.  */
6105                 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6106                 parser->in_type_id_in_expr_p = true;
6107                 type = cp_parser_type_id (parser);
6108                 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6109                 /* Look for the `)'.  */
6110                 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6111               }
6112
6113             /* If things aren't going well, there's no need to
6114                keep going.  */
6115             if (!cp_parser_error_occurred (parser))
6116               {
6117                 bool non_constant_p;
6118                 /* Parse the brace-enclosed initializer list.  */
6119                 initializer = cp_parser_braced_list (parser,
6120                                                      &non_constant_p);
6121               }
6122             /* If that worked, we're definitely looking at a
6123                compound-literal expression.  */
6124             if (cp_parser_parse_definitely (parser))
6125               {
6126                 /* Warn the user that a compound literal is not
6127                    allowed in standard C++.  */
6128                 pedwarn (input_location, OPT_Wpedantic,
6129                          "ISO C++ forbids compound-literals");
6130                 /* For simplicity, we disallow compound literals in
6131                    constant-expressions.  We could
6132                    allow compound literals of integer type, whose
6133                    initializer was a constant, in constant
6134                    expressions.  Permitting that usage, as a further
6135                    extension, would not change the meaning of any
6136                    currently accepted programs.  (Of course, as
6137                    compound literals are not part of ISO C++, the
6138                    standard has nothing to say.)  */
6139                 if (cp_parser_non_integral_constant_expression (parser,
6140                                                                 NIC_NCC))
6141                   {
6142                     postfix_expression = error_mark_node;
6143                     break;
6144                   }
6145                 /* Form the representation of the compound-literal.  */
6146                 postfix_expression
6147                   = finish_compound_literal (type, initializer,
6148                                              tf_warning_or_error);
6149                 break;
6150               }
6151           }
6152
6153         /* It must be a primary-expression.  */
6154         postfix_expression
6155           = cp_parser_primary_expression (parser, address_p, cast_p,
6156                                           /*template_arg_p=*/false,
6157                                           decltype_p,
6158                                           &idk);
6159       }
6160       break;
6161     }
6162
6163   /* Note that we don't need to worry about calling build_cplus_new on a
6164      class-valued CALL_EXPR in decltype when it isn't the end of the
6165      postfix-expression; unary_complex_lvalue will take care of that for
6166      all these cases.  */
6167
6168   /* Keep looping until the postfix-expression is complete.  */
6169   while (true)
6170     {
6171       if (idk == CP_ID_KIND_UNQUALIFIED
6172           && identifier_p (postfix_expression)
6173           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6174         /* It is not a Koenig lookup function call.  */
6175         postfix_expression
6176           = unqualified_name_lookup_error (postfix_expression);
6177
6178       /* Peek at the next token.  */
6179       token = cp_lexer_peek_token (parser->lexer);
6180
6181       switch (token->type)
6182         {
6183         case CPP_OPEN_SQUARE:
6184           if (cp_next_tokens_can_be_std_attribute_p (parser))
6185             {
6186               cp_parser_error (parser,
6187                                "two consecutive %<[%> shall "
6188                                "only introduce an attribute");
6189               return error_mark_node;
6190             }
6191           postfix_expression
6192             = cp_parser_postfix_open_square_expression (parser,
6193                                                         postfix_expression,
6194                                                         false,
6195                                                         decltype_p);
6196           idk = CP_ID_KIND_NONE;
6197           is_member_access = false;
6198           break;
6199
6200         case CPP_OPEN_PAREN:
6201           /* postfix-expression ( expression-list [opt] ) */
6202           {
6203             bool koenig_p;
6204             bool is_builtin_constant_p;
6205             bool saved_integral_constant_expression_p = false;
6206             bool saved_non_integral_constant_expression_p = false;
6207             tsubst_flags_t complain = complain_flags (decltype_p);
6208             vec<tree, va_gc> *args;
6209
6210             is_member_access = false;
6211
6212             is_builtin_constant_p
6213               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6214             if (is_builtin_constant_p)
6215               {
6216                 /* The whole point of __builtin_constant_p is to allow
6217                    non-constant expressions to appear as arguments.  */
6218                 saved_integral_constant_expression_p
6219                   = parser->integral_constant_expression_p;
6220                 saved_non_integral_constant_expression_p
6221                   = parser->non_integral_constant_expression_p;
6222                 parser->integral_constant_expression_p = false;
6223               }
6224             args = (cp_parser_parenthesized_expression_list
6225                     (parser, non_attr,
6226                      /*cast_p=*/false, /*allow_expansion_p=*/true,
6227                      /*non_constant_p=*/NULL,
6228                      /*want_literal_zero_p=*/warn_memset_transposed_args));
6229             if (is_builtin_constant_p)
6230               {
6231                 parser->integral_constant_expression_p
6232                   = saved_integral_constant_expression_p;
6233                 parser->non_integral_constant_expression_p
6234                   = saved_non_integral_constant_expression_p;
6235               }
6236
6237             if (args == NULL)
6238               {
6239                 postfix_expression = error_mark_node;
6240                 break;
6241               }
6242
6243             /* Function calls are not permitted in
6244                constant-expressions.  */
6245             if (! builtin_valid_in_constant_expr_p (postfix_expression)
6246                 && cp_parser_non_integral_constant_expression (parser,
6247                                                                NIC_FUNC_CALL))
6248               {
6249                 postfix_expression = error_mark_node;
6250                 release_tree_vector (args);
6251                 break;
6252               }
6253
6254             koenig_p = false;
6255             if (idk == CP_ID_KIND_UNQUALIFIED
6256                 || idk == CP_ID_KIND_TEMPLATE_ID)
6257               {
6258                 if (identifier_p (postfix_expression))
6259                   {
6260                     if (!args->is_empty ())
6261                       {
6262                         koenig_p = true;
6263                         if (!any_type_dependent_arguments_p (args))
6264                           postfix_expression
6265                             = perform_koenig_lookup (postfix_expression, args,
6266                                                      complain);
6267                       }
6268                     else
6269                       postfix_expression
6270                         = unqualified_fn_lookup_error (postfix_expression);
6271                   }
6272                 /* We do not perform argument-dependent lookup if
6273                    normal lookup finds a non-function, in accordance
6274                    with the expected resolution of DR 218.  */
6275                 else if (!args->is_empty ()
6276                          && is_overloaded_fn (postfix_expression))
6277                   {
6278                     tree fn = get_first_fn (postfix_expression);
6279                     fn = STRIP_TEMPLATE (fn);
6280
6281                     /* Do not do argument dependent lookup if regular
6282                        lookup finds a member function or a block-scope
6283                        function declaration.  [basic.lookup.argdep]/3  */
6284                     if (!DECL_FUNCTION_MEMBER_P (fn)
6285                         && !DECL_LOCAL_FUNCTION_P (fn))
6286                       {
6287                         koenig_p = true;
6288                         if (!any_type_dependent_arguments_p (args))
6289                           postfix_expression
6290                             = perform_koenig_lookup (postfix_expression, args,
6291                                                      complain);
6292                       }
6293                   }
6294               }
6295
6296             if (warn_memset_transposed_args)
6297               {
6298                 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6299                     && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6300                     && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6301                     && vec_safe_length (args) == 3
6302                     && integer_zerop ((*args)[2])
6303                     && LITERAL_ZERO_P ((*args)[2])
6304                     && !(integer_zerop ((*args)[1])
6305                          && LITERAL_ZERO_P ((*args)[1])))
6306                   warning (OPT_Wmemset_transposed_args,
6307                            "%<memset%> used with constant zero length "
6308                            "parameter; this could be due to transposed "
6309                            "parameters");
6310
6311                 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6312                    to avoid leaking those into folder and middle-end.  */
6313                 unsigned int i;
6314                 tree arg;
6315                 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6316                   if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6317                     (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6318               }
6319
6320             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6321               {
6322                 tree instance = TREE_OPERAND (postfix_expression, 0);
6323                 tree fn = TREE_OPERAND (postfix_expression, 1);
6324
6325                 if (processing_template_decl
6326                     && (type_dependent_expression_p (instance)
6327                         || (!BASELINK_P (fn)
6328                             && TREE_CODE (fn) != FIELD_DECL)
6329                         || type_dependent_expression_p (fn)
6330                         || any_type_dependent_arguments_p (args)))
6331                   {
6332                     postfix_expression
6333                       = build_nt_call_vec (postfix_expression, args);
6334                     release_tree_vector (args);
6335                     break;
6336                   }
6337
6338                 if (BASELINK_P (fn))
6339                   {
6340                   postfix_expression
6341                     = (build_new_method_call
6342                        (instance, fn, &args, NULL_TREE,
6343                         (idk == CP_ID_KIND_QUALIFIED
6344                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6345                          : LOOKUP_NORMAL),
6346                         /*fn_p=*/NULL,
6347                         complain));
6348                   }
6349                 else
6350                   postfix_expression
6351                     = finish_call_expr (postfix_expression, &args,
6352                                         /*disallow_virtual=*/false,
6353                                         /*koenig_p=*/false,
6354                                         complain);
6355               }
6356             else if (TREE_CODE (postfix_expression) == OFFSET_REF
6357                      || TREE_CODE (postfix_expression) == MEMBER_REF
6358                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6359               postfix_expression = (build_offset_ref_call_from_tree
6360                                     (postfix_expression, &args,
6361                                      complain));
6362             else if (idk == CP_ID_KIND_QUALIFIED)
6363               /* A call to a static class member, or a namespace-scope
6364                  function.  */
6365               postfix_expression
6366                 = finish_call_expr (postfix_expression, &args,
6367                                     /*disallow_virtual=*/true,
6368                                     koenig_p,
6369                                     complain);
6370             else
6371               /* All other function calls.  */
6372               postfix_expression
6373                 = finish_call_expr (postfix_expression, &args,
6374                                     /*disallow_virtual=*/false,
6375                                     koenig_p,
6376                                     complain);
6377
6378             protected_set_expr_location (postfix_expression, token->location);
6379
6380             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
6381             idk = CP_ID_KIND_NONE;
6382
6383             release_tree_vector (args);
6384           }
6385           break;
6386
6387         case CPP_DOT:
6388         case CPP_DEREF:
6389           /* postfix-expression . template [opt] id-expression
6390              postfix-expression . pseudo-destructor-name
6391              postfix-expression -> template [opt] id-expression
6392              postfix-expression -> pseudo-destructor-name */
6393
6394           /* Consume the `.' or `->' operator.  */
6395           cp_lexer_consume_token (parser->lexer);
6396
6397           postfix_expression
6398             = cp_parser_postfix_dot_deref_expression (parser, token->type,
6399                                                       postfix_expression,
6400                                                       false, &idk, loc);
6401
6402           is_member_access = true;
6403           break;
6404
6405         case CPP_PLUS_PLUS:
6406           /* postfix-expression ++  */
6407           /* Consume the `++' token.  */
6408           cp_lexer_consume_token (parser->lexer);
6409           /* Generate a representation for the complete expression.  */
6410           postfix_expression
6411             = finish_increment_expr (postfix_expression,
6412                                      POSTINCREMENT_EXPR);
6413           /* Increments may not appear in constant-expressions.  */
6414           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6415             postfix_expression = error_mark_node;
6416           idk = CP_ID_KIND_NONE;
6417           is_member_access = false;
6418           break;
6419
6420         case CPP_MINUS_MINUS:
6421           /* postfix-expression -- */
6422           /* Consume the `--' token.  */
6423           cp_lexer_consume_token (parser->lexer);
6424           /* Generate a representation for the complete expression.  */
6425           postfix_expression
6426             = finish_increment_expr (postfix_expression,
6427                                      POSTDECREMENT_EXPR);
6428           /* Decrements may not appear in constant-expressions.  */
6429           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6430             postfix_expression = error_mark_node;
6431           idk = CP_ID_KIND_NONE;
6432           is_member_access = false;
6433           break;
6434
6435         default:
6436           if (pidk_return != NULL)
6437             * pidk_return = idk;
6438           if (member_access_only_p)
6439             return is_member_access? postfix_expression : error_mark_node;
6440           else
6441             return postfix_expression;
6442         }
6443     }
6444
6445   /* We should never get here.  */
6446   gcc_unreachable ();
6447   return error_mark_node;
6448 }
6449
6450 /* This function parses Cilk Plus array notations.  If a normal array expr. is
6451    parsed then the array index is passed back to the caller through *INIT_INDEX 
6452    and the function returns a NULL_TREE.  If array notation expr. is parsed, 
6453    then *INIT_INDEX is ignored by the caller and the function returns 
6454    a tree of type ARRAY_NOTATION_REF.  If some error occurred it returns 
6455    error_mark_node.  */
6456
6457 static tree
6458 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6459                           tree array_value)
6460 {
6461   cp_token *token = NULL;
6462   tree length_index, stride = NULL_TREE, value_tree, array_type;
6463   if (!array_value || array_value == error_mark_node)
6464     {
6465       cp_parser_skip_to_end_of_statement (parser);
6466       return error_mark_node;
6467     }
6468
6469   array_type = TREE_TYPE (array_value);
6470   
6471   bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6472   parser->colon_corrects_to_scope_p = false;
6473   token = cp_lexer_peek_token (parser->lexer);
6474   
6475   if (!token)
6476     {
6477       cp_parser_error (parser, "expected %<:%> or numeral");
6478       return error_mark_node;
6479     }
6480   else if (token->type == CPP_COLON)
6481     {
6482       /* Consume the ':'.  */
6483       cp_lexer_consume_token (parser->lexer);
6484       
6485       /* If we are here, then we have a case like this A[:].  */
6486       if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6487         {
6488           cp_parser_error (parser, "expected %<]%>");
6489           cp_parser_skip_to_end_of_statement (parser);
6490           return error_mark_node;
6491         }
6492       *init_index = NULL_TREE;
6493       stride = NULL_TREE;
6494       length_index = NULL_TREE;
6495     }
6496   else
6497     {
6498       /* If we are here, then there are three valid possibilities:
6499          1. ARRAY [ EXP ]
6500          2. ARRAY [ EXP : EXP ]
6501          3. ARRAY [ EXP : EXP : EXP ]  */
6502
6503       *init_index = cp_parser_expression (parser);      
6504       if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6505         {  
6506           /* This indicates that we have a normal array expression.  */
6507           parser->colon_corrects_to_scope_p = saved_colon_corrects;
6508           return NULL_TREE;
6509         }
6510       
6511       /* Consume the ':'.  */
6512       cp_lexer_consume_token (parser->lexer);
6513       length_index = cp_parser_expression (parser);
6514       if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6515         {
6516           cp_lexer_consume_token (parser->lexer);
6517           stride = cp_parser_expression (parser);
6518         }
6519     }
6520   parser->colon_corrects_to_scope_p = saved_colon_corrects;
6521
6522   if (*init_index == error_mark_node || length_index == error_mark_node
6523       || stride == error_mark_node || array_type == error_mark_node)
6524     {
6525       if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6526         cp_lexer_consume_token (parser->lexer);
6527       return error_mark_node;
6528     }
6529   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6530
6531   value_tree = build_array_notation_ref (loc, array_value, *init_index, 
6532                                          length_index, stride, array_type);
6533   return value_tree;
6534 }
6535
6536 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6537    by cp_parser_builtin_offsetof.  We're looking for
6538
6539      postfix-expression [ expression ]
6540      postfix-expression [ braced-init-list ] (C++11)
6541
6542    FOR_OFFSETOF is set if we're being called in that context, which
6543    changes how we deal with integer constant expressions.  */
6544
6545 static tree
6546 cp_parser_postfix_open_square_expression (cp_parser *parser,
6547                                           tree postfix_expression,
6548                                           bool for_offsetof,
6549                                           bool decltype_p)
6550 {
6551   tree index = NULL_TREE;
6552   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6553   bool saved_greater_than_is_operator_p;
6554
6555   /* Consume the `[' token.  */
6556   cp_lexer_consume_token (parser->lexer);
6557
6558   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6559   parser->greater_than_is_operator_p = true;
6560
6561   /* Parse the index expression.  */
6562   /* ??? For offsetof, there is a question of what to allow here.  If
6563      offsetof is not being used in an integral constant expression context,
6564      then we *could* get the right answer by computing the value at runtime.
6565      If we are in an integral constant expression context, then we might
6566      could accept any constant expression; hard to say without analysis.
6567      Rather than open the barn door too wide right away, allow only integer
6568      constant expressions here.  */
6569   if (for_offsetof)
6570     index = cp_parser_constant_expression (parser);
6571   else
6572     {
6573       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6574         {
6575           bool expr_nonconst_p;
6576           cp_lexer_set_source_position (parser->lexer);
6577           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6578           index = cp_parser_braced_list (parser, &expr_nonconst_p);
6579           if (flag_cilkplus
6580               && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6581             {
6582               error_at (cp_lexer_peek_token (parser->lexer)->location,
6583                         "braced list index is not allowed with array "
6584                         "notation");
6585               cp_parser_skip_to_end_of_statement (parser);
6586               return error_mark_node;
6587             }
6588         }
6589       else if (flag_cilkplus)
6590         {
6591           /* Here are have these two options:
6592              ARRAY[EXP : EXP]        - Array notation expr with default
6593              stride of 1.
6594              ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6595              stride.  */
6596           tree an_exp = cp_parser_array_notation (loc, parser, &index, 
6597                                                   postfix_expression);
6598           if (an_exp)
6599             return an_exp;
6600         }
6601       else
6602         index = cp_parser_expression (parser);
6603     }
6604
6605   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6606
6607   /* Look for the closing `]'.  */
6608   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6609
6610   /* Build the ARRAY_REF.  */
6611   postfix_expression = grok_array_decl (loc, postfix_expression,
6612                                         index, decltype_p);
6613
6614   /* When not doing offsetof, array references are not permitted in
6615      constant-expressions.  */
6616   if (!for_offsetof
6617       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6618     postfix_expression = error_mark_node;
6619
6620   return postfix_expression;
6621 }
6622
6623 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6624    by cp_parser_builtin_offsetof.  We're looking for
6625
6626      postfix-expression . template [opt] id-expression
6627      postfix-expression . pseudo-destructor-name
6628      postfix-expression -> template [opt] id-expression
6629      postfix-expression -> pseudo-destructor-name
6630
6631    FOR_OFFSETOF is set if we're being called in that context.  That sorta
6632    limits what of the above we'll actually accept, but nevermind.
6633    TOKEN_TYPE is the "." or "->" token, which will already have been
6634    removed from the stream.  */
6635
6636 static tree
6637 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6638                                         enum cpp_ttype token_type,
6639                                         tree postfix_expression,
6640                                         bool for_offsetof, cp_id_kind *idk,
6641                                         location_t location)
6642 {
6643   tree name;
6644   bool dependent_p;
6645   bool pseudo_destructor_p;
6646   tree scope = NULL_TREE;
6647
6648   /* If this is a `->' operator, dereference the pointer.  */
6649   if (token_type == CPP_DEREF)
6650     postfix_expression = build_x_arrow (location, postfix_expression,
6651                                         tf_warning_or_error);
6652   /* Check to see whether or not the expression is type-dependent.  */
6653   dependent_p = type_dependent_expression_p (postfix_expression);
6654   /* The identifier following the `->' or `.' is not qualified.  */
6655   parser->scope = NULL_TREE;
6656   parser->qualifying_scope = NULL_TREE;
6657   parser->object_scope = NULL_TREE;
6658   *idk = CP_ID_KIND_NONE;
6659
6660   /* Enter the scope corresponding to the type of the object
6661      given by the POSTFIX_EXPRESSION.  */
6662   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6663     {
6664       scope = TREE_TYPE (postfix_expression);
6665       /* According to the standard, no expression should ever have
6666          reference type.  Unfortunately, we do not currently match
6667          the standard in this respect in that our internal representation
6668          of an expression may have reference type even when the standard
6669          says it does not.  Therefore, we have to manually obtain the
6670          underlying type here.  */
6671       scope = non_reference (scope);
6672       /* The type of the POSTFIX_EXPRESSION must be complete.  */
6673       if (scope == unknown_type_node)
6674         {
6675           error_at (location, "%qE does not have class type",
6676                     postfix_expression);
6677           scope = NULL_TREE;
6678         }
6679       /* Unlike the object expression in other contexts, *this is not
6680          required to be of complete type for purposes of class member
6681          access (5.2.5) outside the member function body.  */
6682       else if (postfix_expression != current_class_ref
6683                && !(processing_template_decl && scope == current_class_type))
6684         scope = complete_type_or_else (scope, NULL_TREE);
6685       /* Let the name lookup machinery know that we are processing a
6686          class member access expression.  */
6687       parser->context->object_type = scope;
6688       /* If something went wrong, we want to be able to discern that case,
6689          as opposed to the case where there was no SCOPE due to the type
6690          of expression being dependent.  */
6691       if (!scope)
6692         scope = error_mark_node;
6693       /* If the SCOPE was erroneous, make the various semantic analysis
6694          functions exit quickly -- and without issuing additional error
6695          messages.  */
6696       if (scope == error_mark_node)
6697         postfix_expression = error_mark_node;
6698     }
6699
6700   /* Assume this expression is not a pseudo-destructor access.  */
6701   pseudo_destructor_p = false;
6702
6703   /* If the SCOPE is a scalar type, then, if this is a valid program,
6704      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
6705      is type dependent, it can be pseudo-destructor-name or something else.
6706      Try to parse it as pseudo-destructor-name first.  */
6707   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6708     {
6709       tree s;
6710       tree type;
6711
6712       cp_parser_parse_tentatively (parser);
6713       /* Parse the pseudo-destructor-name.  */
6714       s = NULL_TREE;
6715       cp_parser_pseudo_destructor_name (parser, postfix_expression,
6716                                         &s, &type);
6717       if (dependent_p
6718           && (cp_parser_error_occurred (parser)
6719               || !SCALAR_TYPE_P (type)))
6720         cp_parser_abort_tentative_parse (parser);
6721       else if (cp_parser_parse_definitely (parser))
6722         {
6723           pseudo_destructor_p = true;
6724           postfix_expression
6725             = finish_pseudo_destructor_expr (postfix_expression,
6726                                              s, type, location);
6727         }
6728     }
6729
6730   if (!pseudo_destructor_p)
6731     {
6732       /* If the SCOPE is not a scalar type, we are looking at an
6733          ordinary class member access expression, rather than a
6734          pseudo-destructor-name.  */
6735       bool template_p;
6736       cp_token *token = cp_lexer_peek_token (parser->lexer);
6737       /* Parse the id-expression.  */
6738       name = (cp_parser_id_expression
6739               (parser,
6740                cp_parser_optional_template_keyword (parser),
6741                /*check_dependency_p=*/true,
6742                &template_p,
6743                /*declarator_p=*/false,
6744                /*optional_p=*/false));
6745       /* In general, build a SCOPE_REF if the member name is qualified.
6746          However, if the name was not dependent and has already been
6747          resolved; there is no need to build the SCOPE_REF.  For example;
6748
6749              struct X { void f(); };
6750              template <typename T> void f(T* t) { t->X::f(); }
6751
6752          Even though "t" is dependent, "X::f" is not and has been resolved
6753          to a BASELINK; there is no need to include scope information.  */
6754
6755       /* But we do need to remember that there was an explicit scope for
6756          virtual function calls.  */
6757       if (parser->scope)
6758         *idk = CP_ID_KIND_QUALIFIED;
6759
6760       /* If the name is a template-id that names a type, we will get a
6761          TYPE_DECL here.  That is invalid code.  */
6762       if (TREE_CODE (name) == TYPE_DECL)
6763         {
6764           error_at (token->location, "invalid use of %qD", name);
6765           postfix_expression = error_mark_node;
6766         }
6767       else
6768         {
6769           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6770             {
6771               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6772                 {
6773                   error_at (token->location, "%<%D::%D%> is not a class member",
6774                             parser->scope, name);
6775                   postfix_expression = error_mark_node;
6776                 }
6777               else
6778                 name = build_qualified_name (/*type=*/NULL_TREE,
6779                                              parser->scope,
6780                                              name,
6781                                              template_p);
6782               parser->scope = NULL_TREE;
6783               parser->qualifying_scope = NULL_TREE;
6784               parser->object_scope = NULL_TREE;
6785             }
6786           if (parser->scope && name && BASELINK_P (name))
6787             adjust_result_of_qualified_name_lookup
6788               (name, parser->scope, scope);
6789           postfix_expression
6790             = finish_class_member_access_expr (postfix_expression, name,
6791                                                template_p, 
6792                                                tf_warning_or_error);
6793         }
6794     }
6795
6796   /* We no longer need to look up names in the scope of the object on
6797      the left-hand side of the `.' or `->' operator.  */
6798   parser->context->object_type = NULL_TREE;
6799
6800   /* Outside of offsetof, these operators may not appear in
6801      constant-expressions.  */
6802   if (!for_offsetof
6803       && (cp_parser_non_integral_constant_expression
6804           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6805     postfix_expression = error_mark_node;
6806
6807   return postfix_expression;
6808 }
6809
6810 /* Cache of LITERAL_ZERO_P constants.  */
6811
6812 static GTY(()) tree literal_zeros[itk_none];
6813
6814 /* Parse a parenthesized expression-list.
6815
6816    expression-list:
6817      assignment-expression
6818      expression-list, assignment-expression
6819
6820    attribute-list:
6821      expression-list
6822      identifier
6823      identifier, expression-list
6824
6825    CAST_P is true if this expression is the target of a cast.
6826
6827    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6828    argument pack.
6829
6830    Returns a vector of trees.  Each element is a representation of an
6831    assignment-expression.  NULL is returned if the ( and or ) are
6832    missing.  An empty, but allocated, vector is returned on no
6833    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6834    if we are parsing an attribute list for an attribute that wants a
6835    plain identifier argument, normal_attr for an attribute that wants
6836    an expression, or non_attr if we aren't parsing an attribute list.  If
6837    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6838    not all of the expressions in the list were constant.
6839    WANT_LITERAL_ZERO_P is true if the caller is interested in
6840    LITERAL_ZERO_P INTEGER_CSTs.  FIXME: once we don't fold everything
6841    immediately, this can be removed.  */
6842
6843 static vec<tree, va_gc> *
6844 cp_parser_parenthesized_expression_list (cp_parser* parser,
6845                                          int is_attribute_list,
6846                                          bool cast_p,
6847                                          bool allow_expansion_p,
6848                                          bool *non_constant_p,
6849                                          bool want_literal_zero_p)
6850 {
6851   vec<tree, va_gc> *expression_list;
6852   bool fold_expr_p = is_attribute_list != non_attr;
6853   tree identifier = NULL_TREE;
6854   bool saved_greater_than_is_operator_p;
6855
6856   /* Assume all the expressions will be constant.  */
6857   if (non_constant_p)
6858     *non_constant_p = false;
6859
6860   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6861     return NULL;
6862
6863   expression_list = make_tree_vector ();
6864
6865   /* Within a parenthesized expression, a `>' token is always
6866      the greater-than operator.  */
6867   saved_greater_than_is_operator_p
6868     = parser->greater_than_is_operator_p;
6869   parser->greater_than_is_operator_p = true;
6870
6871   /* Consume expressions until there are no more.  */
6872   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6873     while (true)
6874       {
6875         tree expr;
6876
6877         /* At the beginning of attribute lists, check to see if the
6878            next token is an identifier.  */
6879         if (is_attribute_list == id_attr
6880             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6881           {
6882             cp_token *token;
6883
6884             /* Consume the identifier.  */
6885             token = cp_lexer_consume_token (parser->lexer);
6886             /* Save the identifier.  */
6887             identifier = token->u.value;
6888           }
6889         else
6890           {
6891             bool expr_non_constant_p;
6892
6893             /* Parse the next assignment-expression.  */
6894             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6895               {
6896                 /* A braced-init-list.  */
6897                 cp_lexer_set_source_position (parser->lexer);
6898                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6899                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6900                 if (non_constant_p && expr_non_constant_p)
6901                   *non_constant_p = true;
6902               }
6903             else if (non_constant_p)
6904               {
6905                 expr = (cp_parser_constant_expression
6906                         (parser, /*allow_non_constant_p=*/true,
6907                          &expr_non_constant_p));
6908                 if (expr_non_constant_p)
6909                   *non_constant_p = true;
6910               }
6911             else
6912               {
6913                 expr = NULL_TREE;
6914                 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6915                 switch (tok->type)
6916                   {
6917                   case CPP_NUMBER:
6918                   case CPP_CHAR:
6919                   case CPP_WCHAR:
6920                   case CPP_CHAR16:
6921                   case CPP_CHAR32:
6922                     /* If a parameter is literal zero alone, remember it
6923                        for -Wmemset-transposed-args warning.  */
6924                     if (integer_zerop (tok->u.value)
6925                         && !TREE_OVERFLOW (tok->u.value)
6926                         && want_literal_zero_p
6927                         && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6928                             == CPP_COMMA
6929                             || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6930                                == CPP_CLOSE_PAREN))
6931                       {
6932                         unsigned int i;
6933                         for (i = 0; i < itk_none; ++i)
6934                           if (TREE_TYPE (tok->u.value) == integer_types[i])
6935                             break;
6936                         if (i < itk_none && literal_zeros[i])
6937                           expr = literal_zeros[i];
6938                         else
6939                           {
6940                             expr = copy_node (tok->u.value);
6941                             LITERAL_ZERO_P (expr) = 1;
6942                             if (i < itk_none)
6943                               literal_zeros[i] = expr;
6944                           }
6945                         /* Consume the 0 token (or '\0', 0LL etc.).  */
6946                         cp_lexer_consume_token (parser->lexer);
6947                       }
6948                     break;
6949                   default:
6950                     break;
6951                   }
6952                 if (expr == NULL_TREE)
6953                   expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
6954                                                           cast_p);
6955               }
6956
6957             if (fold_expr_p)
6958               expr = instantiate_non_dependent_expr (expr);
6959
6960             /* If we have an ellipsis, then this is an expression
6961                expansion.  */
6962             if (allow_expansion_p
6963                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6964               {
6965                 /* Consume the `...'.  */
6966                 cp_lexer_consume_token (parser->lexer);
6967
6968                 /* Build the argument pack.  */
6969                 expr = make_pack_expansion (expr);
6970               }
6971
6972              /* Add it to the list.  We add error_mark_node
6973                 expressions to the list, so that we can still tell if
6974                 the correct form for a parenthesized expression-list
6975                 is found. That gives better errors.  */
6976             vec_safe_push (expression_list, expr);
6977
6978             if (expr == error_mark_node)
6979               goto skip_comma;
6980           }
6981
6982         /* After the first item, attribute lists look the same as
6983            expression lists.  */
6984         is_attribute_list = non_attr;
6985
6986       get_comma:;
6987         /* If the next token isn't a `,', then we are done.  */
6988         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6989           break;
6990
6991         /* Otherwise, consume the `,' and keep going.  */
6992         cp_lexer_consume_token (parser->lexer);
6993       }
6994
6995   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6996     {
6997       int ending;
6998
6999     skip_comma:;
7000       /* We try and resync to an unnested comma, as that will give the
7001          user better diagnostics.  */
7002       ending = cp_parser_skip_to_closing_parenthesis (parser,
7003                                                       /*recovering=*/true,
7004                                                       /*or_comma=*/true,
7005                                                       /*consume_paren=*/true);
7006       if (ending < 0)
7007         goto get_comma;
7008       if (!ending)
7009         {
7010           parser->greater_than_is_operator_p
7011             = saved_greater_than_is_operator_p;
7012           return NULL;
7013         }
7014     }
7015
7016   parser->greater_than_is_operator_p
7017     = saved_greater_than_is_operator_p;
7018
7019   if (identifier)
7020     vec_safe_insert (expression_list, 0, identifier);
7021
7022   return expression_list;
7023 }
7024
7025 /* Parse a pseudo-destructor-name.
7026
7027    pseudo-destructor-name:
7028      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7029      :: [opt] nested-name-specifier template template-id :: ~ type-name
7030      :: [opt] nested-name-specifier [opt] ~ type-name
7031
7032    If either of the first two productions is used, sets *SCOPE to the
7033    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
7034    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
7035    or ERROR_MARK_NODE if the parse fails.  */
7036
7037 static void
7038 cp_parser_pseudo_destructor_name (cp_parser* parser,
7039                                   tree object,
7040                                   tree* scope,
7041                                   tree* type)
7042 {
7043   bool nested_name_specifier_p;
7044
7045   /* Handle ~auto.  */
7046   if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7047       && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7048       && !type_dependent_expression_p (object))
7049     {
7050       if (cxx_dialect < cxx14)
7051         pedwarn (input_location, 0,
7052                  "%<~auto%> only available with "
7053                  "-std=c++14 or -std=gnu++14");
7054       cp_lexer_consume_token (parser->lexer);
7055       cp_lexer_consume_token (parser->lexer);
7056       *scope = NULL_TREE;
7057       *type = TREE_TYPE (object);
7058       return;
7059     }
7060
7061   /* Assume that things will not work out.  */
7062   *type = error_mark_node;
7063
7064   /* Look for the optional `::' operator.  */
7065   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7066   /* Look for the optional nested-name-specifier.  */
7067   nested_name_specifier_p
7068     = (cp_parser_nested_name_specifier_opt (parser,
7069                                             /*typename_keyword_p=*/false,
7070                                             /*check_dependency_p=*/true,
7071                                             /*type_p=*/false,
7072                                             /*is_declaration=*/false)
7073        != NULL_TREE);
7074   /* Now, if we saw a nested-name-specifier, we might be doing the
7075      second production.  */
7076   if (nested_name_specifier_p
7077       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7078     {
7079       /* Consume the `template' keyword.  */
7080       cp_lexer_consume_token (parser->lexer);
7081       /* Parse the template-id.  */
7082       cp_parser_template_id (parser,
7083                              /*template_keyword_p=*/true,
7084                              /*check_dependency_p=*/false,
7085                              class_type,
7086                              /*is_declaration=*/true);
7087       /* Look for the `::' token.  */
7088       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7089     }
7090   /* If the next token is not a `~', then there might be some
7091      additional qualification.  */
7092   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7093     {
7094       /* At this point, we're looking for "type-name :: ~".  The type-name
7095          must not be a class-name, since this is a pseudo-destructor.  So,
7096          it must be either an enum-name, or a typedef-name -- both of which
7097          are just identifiers.  So, we peek ahead to check that the "::"
7098          and "~" tokens are present; if they are not, then we can avoid
7099          calling type_name.  */
7100       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7101           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7102           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7103         {
7104           cp_parser_error (parser, "non-scalar type");
7105           return;
7106         }
7107
7108       /* Look for the type-name.  */
7109       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7110       if (*scope == error_mark_node)
7111         return;
7112
7113       /* Look for the `::' token.  */
7114       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7115     }
7116   else
7117     *scope = NULL_TREE;
7118
7119   /* Look for the `~'.  */
7120   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7121
7122   /* Once we see the ~, this has to be a pseudo-destructor.  */
7123   if (!processing_template_decl && !cp_parser_error_occurred (parser))
7124     cp_parser_commit_to_topmost_tentative_parse (parser);
7125
7126   /* Look for the type-name again.  We are not responsible for
7127      checking that it matches the first type-name.  */
7128   *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7129 }
7130
7131 /* Parse a unary-expression.
7132
7133    unary-expression:
7134      postfix-expression
7135      ++ cast-expression
7136      -- cast-expression
7137      unary-operator cast-expression
7138      sizeof unary-expression
7139      sizeof ( type-id )
7140      alignof ( type-id )  [C++0x]
7141      new-expression
7142      delete-expression
7143
7144    GNU Extensions:
7145
7146    unary-expression:
7147      __extension__ cast-expression
7148      __alignof__ unary-expression
7149      __alignof__ ( type-id )
7150      alignof unary-expression  [C++0x]
7151      __real__ cast-expression
7152      __imag__ cast-expression
7153      && identifier
7154      sizeof ( type-id ) { initializer-list , [opt] }
7155      alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7156      __alignof__ ( type-id ) { initializer-list , [opt] }
7157
7158    ADDRESS_P is true iff the unary-expression is appearing as the
7159    operand of the `&' operator.   CAST_P is true if this expression is
7160    the target of a cast.
7161
7162    Returns a representation of the expression.  */
7163
7164 static tree
7165 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7166                             bool address_p, bool cast_p, bool decltype_p)
7167 {
7168   cp_token *token;
7169   enum tree_code unary_operator;
7170
7171   /* Peek at the next token.  */
7172   token = cp_lexer_peek_token (parser->lexer);
7173   /* Some keywords give away the kind of expression.  */
7174   if (token->type == CPP_KEYWORD)
7175     {
7176       enum rid keyword = token->keyword;
7177
7178       switch (keyword)
7179         {
7180         case RID_ALIGNOF:
7181         case RID_SIZEOF:
7182           {
7183             tree operand, ret;
7184             enum tree_code op;
7185             location_t first_loc;
7186
7187             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7188             /* Consume the token.  */
7189             cp_lexer_consume_token (parser->lexer);
7190             first_loc = cp_lexer_peek_token (parser->lexer)->location;
7191             /* Parse the operand.  */
7192             operand = cp_parser_sizeof_operand (parser, keyword);
7193
7194             if (TYPE_P (operand))
7195               ret = cxx_sizeof_or_alignof_type (operand, op, true);
7196             else
7197               {
7198                 /* ISO C++ defines alignof only with types, not with
7199                    expressions. So pedwarn if alignof is used with a non-
7200                    type expression. However, __alignof__ is ok.  */
7201                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7202                   pedwarn (token->location, OPT_Wpedantic,
7203                            "ISO C++ does not allow %<alignof%> "
7204                            "with a non-type");
7205
7206                 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7207               }
7208             /* For SIZEOF_EXPR, just issue diagnostics, but keep
7209                SIZEOF_EXPR with the original operand.  */
7210             if (op == SIZEOF_EXPR && ret != error_mark_node)
7211               {
7212                 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7213                   {
7214                     if (!processing_template_decl && TYPE_P (operand))
7215                       {
7216                         ret = build_min (SIZEOF_EXPR, size_type_node,
7217                                          build1 (NOP_EXPR, operand,
7218                                                  error_mark_node));
7219                         SIZEOF_EXPR_TYPE_P (ret) = 1;
7220                       }
7221                     else
7222                       ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7223                     TREE_SIDE_EFFECTS (ret) = 0;
7224                     TREE_READONLY (ret) = 1;
7225                   }
7226                 SET_EXPR_LOCATION (ret, first_loc);
7227               }
7228             return ret;
7229           }
7230
7231         case RID_NEW:
7232           return cp_parser_new_expression (parser);
7233
7234         case RID_DELETE:
7235           return cp_parser_delete_expression (parser);
7236
7237         case RID_EXTENSION:
7238           {
7239             /* The saved value of the PEDANTIC flag.  */
7240             int saved_pedantic;
7241             tree expr;
7242
7243             /* Save away the PEDANTIC flag.  */
7244             cp_parser_extension_opt (parser, &saved_pedantic);
7245             /* Parse the cast-expression.  */
7246             expr = cp_parser_simple_cast_expression (parser);
7247             /* Restore the PEDANTIC flag.  */
7248             pedantic = saved_pedantic;
7249
7250             return expr;
7251           }
7252
7253         case RID_REALPART:
7254         case RID_IMAGPART:
7255           {
7256             tree expression;
7257
7258             /* Consume the `__real__' or `__imag__' token.  */
7259             cp_lexer_consume_token (parser->lexer);
7260             /* Parse the cast-expression.  */
7261             expression = cp_parser_simple_cast_expression (parser);
7262             /* Create the complete representation.  */
7263             return build_x_unary_op (token->location,
7264                                      (keyword == RID_REALPART
7265                                       ? REALPART_EXPR : IMAGPART_EXPR),
7266                                      expression,
7267                                      tf_warning_or_error);
7268           }
7269           break;
7270
7271         case RID_TRANSACTION_ATOMIC:
7272         case RID_TRANSACTION_RELAXED:
7273           return cp_parser_transaction_expression (parser, keyword);
7274
7275         case RID_NOEXCEPT:
7276           {
7277             tree expr;
7278             const char *saved_message;
7279             bool saved_integral_constant_expression_p;
7280             bool saved_non_integral_constant_expression_p;
7281             bool saved_greater_than_is_operator_p;
7282
7283             cp_lexer_consume_token (parser->lexer);
7284             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7285
7286             saved_message = parser->type_definition_forbidden_message;
7287             parser->type_definition_forbidden_message
7288               = G_("types may not be defined in %<noexcept%> expressions");
7289
7290             saved_integral_constant_expression_p
7291               = parser->integral_constant_expression_p;
7292             saved_non_integral_constant_expression_p
7293               = parser->non_integral_constant_expression_p;
7294             parser->integral_constant_expression_p = false;
7295
7296             saved_greater_than_is_operator_p
7297               = parser->greater_than_is_operator_p;
7298             parser->greater_than_is_operator_p = true;
7299
7300             ++cp_unevaluated_operand;
7301             ++c_inhibit_evaluation_warnings;
7302             ++cp_noexcept_operand;
7303             expr = cp_parser_expression (parser);
7304             --cp_noexcept_operand;
7305             --c_inhibit_evaluation_warnings;
7306             --cp_unevaluated_operand;
7307
7308             parser->greater_than_is_operator_p
7309               = saved_greater_than_is_operator_p;
7310
7311             parser->integral_constant_expression_p
7312               = saved_integral_constant_expression_p;
7313             parser->non_integral_constant_expression_p
7314               = saved_non_integral_constant_expression_p;
7315
7316             parser->type_definition_forbidden_message = saved_message;
7317
7318             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7319             return finish_noexcept_expr (expr, tf_warning_or_error);
7320           }
7321
7322         default:
7323           break;
7324         }
7325     }
7326
7327   /* Look for the `:: new' and `:: delete', which also signal the
7328      beginning of a new-expression, or delete-expression,
7329      respectively.  If the next token is `::', then it might be one of
7330      these.  */
7331   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7332     {
7333       enum rid keyword;
7334
7335       /* See if the token after the `::' is one of the keywords in
7336          which we're interested.  */
7337       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7338       /* If it's `new', we have a new-expression.  */
7339       if (keyword == RID_NEW)
7340         return cp_parser_new_expression (parser);
7341       /* Similarly, for `delete'.  */
7342       else if (keyword == RID_DELETE)
7343         return cp_parser_delete_expression (parser);
7344     }
7345
7346   /* Look for a unary operator.  */
7347   unary_operator = cp_parser_unary_operator (token);
7348   /* The `++' and `--' operators can be handled similarly, even though
7349      they are not technically unary-operators in the grammar.  */
7350   if (unary_operator == ERROR_MARK)
7351     {
7352       if (token->type == CPP_PLUS_PLUS)
7353         unary_operator = PREINCREMENT_EXPR;
7354       else if (token->type == CPP_MINUS_MINUS)
7355         unary_operator = PREDECREMENT_EXPR;
7356       /* Handle the GNU address-of-label extension.  */
7357       else if (cp_parser_allow_gnu_extensions_p (parser)
7358                && token->type == CPP_AND_AND)
7359         {
7360           tree identifier;
7361           tree expression;
7362           location_t loc = token->location;
7363
7364           /* Consume the '&&' token.  */
7365           cp_lexer_consume_token (parser->lexer);
7366           /* Look for the identifier.  */
7367           identifier = cp_parser_identifier (parser);
7368           /* Create an expression representing the address.  */
7369           expression = finish_label_address_expr (identifier, loc);
7370           if (cp_parser_non_integral_constant_expression (parser,
7371                                                           NIC_ADDR_LABEL))
7372             expression = error_mark_node;
7373           return expression;
7374         }
7375     }
7376   if (unary_operator != ERROR_MARK)
7377     {
7378       tree cast_expression;
7379       tree expression = error_mark_node;
7380       non_integral_constant non_constant_p = NIC_NONE;
7381       location_t loc = token->location;
7382       tsubst_flags_t complain = complain_flags (decltype_p);
7383
7384       /* Consume the operator token.  */
7385       token = cp_lexer_consume_token (parser->lexer);
7386       /* Parse the cast-expression.  */
7387       cast_expression
7388         = cp_parser_cast_expression (parser,
7389                                      unary_operator == ADDR_EXPR,
7390                                      /*cast_p=*/false,
7391                                      /*decltype*/false,
7392                                      pidk);
7393       /* Now, build an appropriate representation.  */
7394       switch (unary_operator)
7395         {
7396         case INDIRECT_REF:
7397           non_constant_p = NIC_STAR;
7398           expression = build_x_indirect_ref (loc, cast_expression,
7399                                              RO_UNARY_STAR,
7400                                              complain);
7401           break;
7402
7403         case ADDR_EXPR:
7404            non_constant_p = NIC_ADDR;
7405           /* Fall through.  */
7406         case BIT_NOT_EXPR:
7407           expression = build_x_unary_op (loc, unary_operator,
7408                                          cast_expression,
7409                                          complain);
7410           break;
7411
7412         case PREINCREMENT_EXPR:
7413         case PREDECREMENT_EXPR:
7414           non_constant_p = unary_operator == PREINCREMENT_EXPR
7415                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7416           /* Fall through.  */
7417         case UNARY_PLUS_EXPR:
7418         case NEGATE_EXPR:
7419         case TRUTH_NOT_EXPR:
7420           expression = finish_unary_op_expr (loc, unary_operator,
7421                                              cast_expression, complain);
7422           break;
7423
7424         default:
7425           gcc_unreachable ();
7426         }
7427
7428       if (non_constant_p != NIC_NONE
7429           && cp_parser_non_integral_constant_expression (parser,
7430                                                          non_constant_p))
7431         expression = error_mark_node;
7432
7433       return expression;
7434     }
7435
7436   return cp_parser_postfix_expression (parser, address_p, cast_p,
7437                                        /*member_access_only_p=*/false,
7438                                        decltype_p,
7439                                        pidk);
7440 }
7441
7442 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
7443    unary-operator, the corresponding tree code is returned.  */
7444
7445 static enum tree_code
7446 cp_parser_unary_operator (cp_token* token)
7447 {
7448   switch (token->type)
7449     {
7450     case CPP_MULT:
7451       return INDIRECT_REF;
7452
7453     case CPP_AND:
7454       return ADDR_EXPR;
7455
7456     case CPP_PLUS:
7457       return UNARY_PLUS_EXPR;
7458
7459     case CPP_MINUS:
7460       return NEGATE_EXPR;
7461
7462     case CPP_NOT:
7463       return TRUTH_NOT_EXPR;
7464
7465     case CPP_COMPL:
7466       return BIT_NOT_EXPR;
7467
7468     default:
7469       return ERROR_MARK;
7470     }
7471 }
7472
7473 /* Parse a new-expression.
7474
7475    new-expression:
7476      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7477      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7478
7479    Returns a representation of the expression.  */
7480
7481 static tree
7482 cp_parser_new_expression (cp_parser* parser)
7483 {
7484   bool global_scope_p;
7485   vec<tree, va_gc> *placement;
7486   tree type;
7487   vec<tree, va_gc> *initializer;
7488   tree nelts = NULL_TREE;
7489   tree ret;
7490
7491   /* Look for the optional `::' operator.  */
7492   global_scope_p
7493     = (cp_parser_global_scope_opt (parser,
7494                                    /*current_scope_valid_p=*/false)
7495        != NULL_TREE);
7496   /* Look for the `new' operator.  */
7497   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7498   /* There's no easy way to tell a new-placement from the
7499      `( type-id )' construct.  */
7500   cp_parser_parse_tentatively (parser);
7501   /* Look for a new-placement.  */
7502   placement = cp_parser_new_placement (parser);
7503   /* If that didn't work out, there's no new-placement.  */
7504   if (!cp_parser_parse_definitely (parser))
7505     {
7506       if (placement != NULL)
7507         release_tree_vector (placement);
7508       placement = NULL;
7509     }
7510
7511   /* If the next token is a `(', then we have a parenthesized
7512      type-id.  */
7513   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7514     {
7515       cp_token *token;
7516       const char *saved_message = parser->type_definition_forbidden_message;
7517
7518       /* Consume the `('.  */
7519       cp_lexer_consume_token (parser->lexer);
7520
7521       /* Parse the type-id.  */
7522       parser->type_definition_forbidden_message
7523         = G_("types may not be defined in a new-expression");
7524       type = cp_parser_type_id (parser);
7525       parser->type_definition_forbidden_message = saved_message;
7526
7527       /* Look for the closing `)'.  */
7528       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7529       token = cp_lexer_peek_token (parser->lexer);
7530       /* There should not be a direct-new-declarator in this production,
7531          but GCC used to allowed this, so we check and emit a sensible error
7532          message for this case.  */
7533       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7534         {
7535           error_at (token->location,
7536                     "array bound forbidden after parenthesized type-id");
7537           inform (token->location, 
7538                   "try removing the parentheses around the type-id");
7539           cp_parser_direct_new_declarator (parser);
7540         }
7541     }
7542   /* Otherwise, there must be a new-type-id.  */
7543   else
7544     type = cp_parser_new_type_id (parser, &nelts);
7545
7546   /* If the next token is a `(' or '{', then we have a new-initializer.  */
7547   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7548       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7549     initializer = cp_parser_new_initializer (parser);
7550   else
7551     initializer = NULL;
7552
7553   /* A new-expression may not appear in an integral constant
7554      expression.  */
7555   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7556     ret = error_mark_node;
7557   else
7558     {
7559       /* Create a representation of the new-expression.  */
7560       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7561                        tf_warning_or_error);
7562     }
7563
7564   if (placement != NULL)
7565     release_tree_vector (placement);
7566   if (initializer != NULL)
7567     release_tree_vector (initializer);
7568
7569   return ret;
7570 }
7571
7572 /* Parse a new-placement.
7573
7574    new-placement:
7575      ( expression-list )
7576
7577    Returns the same representation as for an expression-list.  */
7578
7579 static vec<tree, va_gc> *
7580 cp_parser_new_placement (cp_parser* parser)
7581 {
7582   vec<tree, va_gc> *expression_list;
7583
7584   /* Parse the expression-list.  */
7585   expression_list = (cp_parser_parenthesized_expression_list
7586                      (parser, non_attr, /*cast_p=*/false,
7587                       /*allow_expansion_p=*/true,
7588                       /*non_constant_p=*/NULL));
7589
7590   return expression_list;
7591 }
7592
7593 /* Parse a new-type-id.
7594
7595    new-type-id:
7596      type-specifier-seq new-declarator [opt]
7597
7598    Returns the TYPE allocated.  If the new-type-id indicates an array
7599    type, *NELTS is set to the number of elements in the last array
7600    bound; the TYPE will not include the last array bound.  */
7601
7602 static tree
7603 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7604 {
7605   cp_decl_specifier_seq type_specifier_seq;
7606   cp_declarator *new_declarator;
7607   cp_declarator *declarator;
7608   cp_declarator *outer_declarator;
7609   const char *saved_message;
7610
7611   /* The type-specifier sequence must not contain type definitions.
7612      (It cannot contain declarations of new types either, but if they
7613      are not definitions we will catch that because they are not
7614      complete.)  */
7615   saved_message = parser->type_definition_forbidden_message;
7616   parser->type_definition_forbidden_message
7617     = G_("types may not be defined in a new-type-id");
7618   /* Parse the type-specifier-seq.  */
7619   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7620                                 /*is_trailing_return=*/false,
7621                                 &type_specifier_seq);
7622   /* Restore the old message.  */
7623   parser->type_definition_forbidden_message = saved_message;
7624
7625   if (type_specifier_seq.type == error_mark_node)
7626     return error_mark_node;
7627
7628   /* Parse the new-declarator.  */
7629   new_declarator = cp_parser_new_declarator_opt (parser);
7630
7631   /* Determine the number of elements in the last array dimension, if
7632      any.  */
7633   *nelts = NULL_TREE;
7634   /* Skip down to the last array dimension.  */
7635   declarator = new_declarator;
7636   outer_declarator = NULL;
7637   while (declarator && (declarator->kind == cdk_pointer
7638                         || declarator->kind == cdk_ptrmem))
7639     {
7640       outer_declarator = declarator;
7641       declarator = declarator->declarator;
7642     }
7643   while (declarator
7644          && declarator->kind == cdk_array
7645          && declarator->declarator
7646          && declarator->declarator->kind == cdk_array)
7647     {
7648       outer_declarator = declarator;
7649       declarator = declarator->declarator;
7650     }
7651
7652   if (declarator && declarator->kind == cdk_array)
7653     {
7654       *nelts = declarator->u.array.bounds;
7655       if (*nelts == error_mark_node)
7656         *nelts = integer_one_node;
7657
7658       if (outer_declarator)
7659         outer_declarator->declarator = declarator->declarator;
7660       else
7661         new_declarator = NULL;
7662     }
7663
7664   return groktypename (&type_specifier_seq, new_declarator, false);
7665 }
7666
7667 /* Parse an (optional) new-declarator.
7668
7669    new-declarator:
7670      ptr-operator new-declarator [opt]
7671      direct-new-declarator
7672
7673    Returns the declarator.  */
7674
7675 static cp_declarator *
7676 cp_parser_new_declarator_opt (cp_parser* parser)
7677 {
7678   enum tree_code code;
7679   tree type, std_attributes = NULL_TREE;
7680   cp_cv_quals cv_quals;  
7681
7682   /* We don't know if there's a ptr-operator next, or not.  */
7683   cp_parser_parse_tentatively (parser);
7684   /* Look for a ptr-operator.  */
7685   code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7686   /* If that worked, look for more new-declarators.  */
7687   if (cp_parser_parse_definitely (parser))
7688     {
7689       cp_declarator *declarator;
7690
7691       /* Parse another optional declarator.  */
7692       declarator = cp_parser_new_declarator_opt (parser);
7693
7694       declarator = cp_parser_make_indirect_declarator
7695         (code, type, cv_quals, declarator, std_attributes);
7696
7697       return declarator;
7698     }
7699
7700   /* If the next token is a `[', there is a direct-new-declarator.  */
7701   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7702     return cp_parser_direct_new_declarator (parser);
7703
7704   return NULL;
7705 }
7706
7707 /* Parse a direct-new-declarator.
7708
7709    direct-new-declarator:
7710      [ expression ]
7711      direct-new-declarator [constant-expression]
7712
7713    */
7714
7715 static cp_declarator *
7716 cp_parser_direct_new_declarator (cp_parser* parser)
7717 {
7718   cp_declarator *declarator = NULL;
7719
7720   while (true)
7721     {
7722       tree expression;
7723       cp_token *token;
7724
7725       /* Look for the opening `['.  */
7726       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7727
7728       token = cp_lexer_peek_token (parser->lexer);
7729       expression = cp_parser_expression (parser);
7730       /* The standard requires that the expression have integral
7731          type.  DR 74 adds enumeration types.  We believe that the
7732          real intent is that these expressions be handled like the
7733          expression in a `switch' condition, which also allows
7734          classes with a single conversion to integral or
7735          enumeration type.  */
7736       if (!processing_template_decl)
7737         {
7738           expression
7739             = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7740                                           expression,
7741                                           /*complain=*/true);
7742           if (!expression)
7743             {
7744               error_at (token->location,
7745                         "expression in new-declarator must have integral "
7746                         "or enumeration type");
7747               expression = error_mark_node;
7748             }
7749         }
7750
7751       /* Look for the closing `]'.  */
7752       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7753
7754       /* Add this bound to the declarator.  */
7755       declarator = make_array_declarator (declarator, expression);
7756
7757       /* If the next token is not a `[', then there are no more
7758          bounds.  */
7759       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7760         break;
7761     }
7762
7763   return declarator;
7764 }
7765
7766 /* Parse a new-initializer.
7767
7768    new-initializer:
7769      ( expression-list [opt] )
7770      braced-init-list
7771
7772    Returns a representation of the expression-list.  */
7773
7774 static vec<tree, va_gc> *
7775 cp_parser_new_initializer (cp_parser* parser)
7776 {
7777   vec<tree, va_gc> *expression_list;
7778
7779   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7780     {
7781       tree t;
7782       bool expr_non_constant_p;
7783       cp_lexer_set_source_position (parser->lexer);
7784       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7785       t = cp_parser_braced_list (parser, &expr_non_constant_p);
7786       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7787       expression_list = make_tree_vector_single (t);
7788     }
7789   else
7790     expression_list = (cp_parser_parenthesized_expression_list
7791                        (parser, non_attr, /*cast_p=*/false,
7792                         /*allow_expansion_p=*/true,
7793                         /*non_constant_p=*/NULL));
7794
7795   return expression_list;
7796 }
7797
7798 /* Parse a delete-expression.
7799
7800    delete-expression:
7801      :: [opt] delete cast-expression
7802      :: [opt] delete [ ] cast-expression
7803
7804    Returns a representation of the expression.  */
7805
7806 static tree
7807 cp_parser_delete_expression (cp_parser* parser)
7808 {
7809   bool global_scope_p;
7810   bool array_p;
7811   tree expression;
7812
7813   /* Look for the optional `::' operator.  */
7814   global_scope_p
7815     = (cp_parser_global_scope_opt (parser,
7816                                    /*current_scope_valid_p=*/false)
7817        != NULL_TREE);
7818   /* Look for the `delete' keyword.  */
7819   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7820   /* See if the array syntax is in use.  */
7821   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7822     {
7823       /* Consume the `[' token.  */
7824       cp_lexer_consume_token (parser->lexer);
7825       /* Look for the `]' token.  */
7826       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7827       /* Remember that this is the `[]' construct.  */
7828       array_p = true;
7829     }
7830   else
7831     array_p = false;
7832
7833   /* Parse the cast-expression.  */
7834   expression = cp_parser_simple_cast_expression (parser);
7835
7836   /* A delete-expression may not appear in an integral constant
7837      expression.  */
7838   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7839     return error_mark_node;
7840
7841   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7842                         tf_warning_or_error);
7843 }
7844
7845 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7846    neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7847    0 otherwise.  */
7848
7849 static int
7850 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7851 {
7852   cp_token *token = cp_lexer_peek_token (parser->lexer);
7853   switch (token->type)
7854     {
7855     case CPP_COMMA:
7856     case CPP_SEMICOLON:
7857     case CPP_QUERY:
7858     case CPP_COLON:
7859     case CPP_CLOSE_SQUARE:
7860     case CPP_CLOSE_PAREN:
7861     case CPP_CLOSE_BRACE:
7862     case CPP_OPEN_BRACE:
7863     case CPP_DOT:
7864     case CPP_DOT_STAR:
7865     case CPP_DEREF:
7866     case CPP_DEREF_STAR:
7867     case CPP_DIV:
7868     case CPP_MOD:
7869     case CPP_LSHIFT:
7870     case CPP_RSHIFT:
7871     case CPP_LESS:
7872     case CPP_GREATER:
7873     case CPP_LESS_EQ:
7874     case CPP_GREATER_EQ:
7875     case CPP_EQ_EQ:
7876     case CPP_NOT_EQ:
7877     case CPP_EQ:
7878     case CPP_MULT_EQ:
7879     case CPP_DIV_EQ:
7880     case CPP_MOD_EQ:
7881     case CPP_PLUS_EQ:
7882     case CPP_MINUS_EQ:
7883     case CPP_RSHIFT_EQ:
7884     case CPP_LSHIFT_EQ:
7885     case CPP_AND_EQ:
7886     case CPP_XOR_EQ:
7887     case CPP_OR_EQ:
7888     case CPP_XOR:
7889     case CPP_OR:
7890     case CPP_OR_OR:
7891     case CPP_EOF:
7892     case CPP_ELLIPSIS:
7893       return 0;
7894
7895     case CPP_OPEN_PAREN:
7896       /* In ((type ()) () the last () isn't a valid cast-expression,
7897          so the whole must be parsed as postfix-expression.  */
7898       return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7899              != CPP_CLOSE_PAREN;
7900
7901     case CPP_OPEN_SQUARE:
7902       /* '[' may start a primary-expression in obj-c++ and in C++11,
7903          as a lambda-expression, eg, '(void)[]{}'.  */
7904       if (cxx_dialect >= cxx11)
7905         return -1;
7906       return c_dialect_objc ();
7907
7908     case CPP_PLUS_PLUS:
7909     case CPP_MINUS_MINUS:
7910       /* '++' and '--' may or may not start a cast-expression:
7911
7912          struct T { void operator++(int); };
7913          void f() { (T())++; }
7914
7915          vs
7916
7917          int a;
7918          (int)++a;  */
7919       return -1;
7920
7921     default:
7922       return 1;
7923     }
7924 }
7925
7926 /* Parse a cast-expression.
7927
7928    cast-expression:
7929      unary-expression
7930      ( type-id ) cast-expression
7931
7932    ADDRESS_P is true iff the unary-expression is appearing as the
7933    operand of the `&' operator.   CAST_P is true if this expression is
7934    the target of a cast.
7935
7936    Returns a representation of the expression.  */
7937
7938 static tree
7939 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7940                            bool decltype_p, cp_id_kind * pidk)
7941 {
7942   /* If it's a `(', then we might be looking at a cast.  */
7943   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7944     {
7945       tree type = NULL_TREE;
7946       tree expr = NULL_TREE;
7947       int cast_expression = 0;
7948       const char *saved_message;
7949
7950       /* There's no way to know yet whether or not this is a cast.
7951          For example, `(int (3))' is a unary-expression, while `(int)
7952          3' is a cast.  So, we resort to parsing tentatively.  */
7953       cp_parser_parse_tentatively (parser);
7954       /* Types may not be defined in a cast.  */
7955       saved_message = parser->type_definition_forbidden_message;
7956       parser->type_definition_forbidden_message
7957         = G_("types may not be defined in casts");
7958       /* Consume the `('.  */
7959       cp_lexer_consume_token (parser->lexer);
7960       /* A very tricky bit is that `(struct S) { 3 }' is a
7961          compound-literal (which we permit in C++ as an extension).
7962          But, that construct is not a cast-expression -- it is a
7963          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7964          is legal; if the compound-literal were a cast-expression,
7965          you'd need an extra set of parentheses.)  But, if we parse
7966          the type-id, and it happens to be a class-specifier, then we
7967          will commit to the parse at that point, because we cannot
7968          undo the action that is done when creating a new class.  So,
7969          then we cannot back up and do a postfix-expression.
7970
7971          Another tricky case is the following (c++/29234):
7972
7973          struct S { void operator () (); };
7974
7975          void foo ()
7976          {
7977            ( S()() );
7978          }
7979
7980          As a type-id we parse the parenthesized S()() as a function
7981          returning a function, groktypename complains and we cannot
7982          back up in this case either.
7983
7984          Therefore, we scan ahead to the closing `)', and check to see
7985          if the tokens after the `)' can start a cast-expression.  Otherwise
7986          we are dealing with an unary-expression, a postfix-expression
7987          or something else.
7988
7989          Yet another tricky case, in C++11, is the following (c++/54891):
7990
7991          (void)[]{};
7992
7993          The issue is that usually, besides the case of lambda-expressions,
7994          the parenthesized type-id cannot be followed by '[', and, eg, we
7995          want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7996          Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7997          we don't commit, we try a cast-expression, then an unary-expression.
7998
7999          Save tokens so that we can put them back.  */
8000       cp_lexer_save_tokens (parser->lexer);
8001
8002       /* We may be looking at a cast-expression.  */
8003       if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8004                                                  /*consume_paren=*/true))
8005         cast_expression
8006           = cp_parser_tokens_start_cast_expression (parser);
8007
8008       /* Roll back the tokens we skipped.  */
8009       cp_lexer_rollback_tokens (parser->lexer);
8010       /* If we aren't looking at a cast-expression, simulate an error so
8011          that the call to cp_parser_error_occurred below returns true.  */
8012       if (!cast_expression)
8013         cp_parser_simulate_error (parser);
8014       else
8015         {
8016           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8017           parser->in_type_id_in_expr_p = true;
8018           /* Look for the type-id.  */
8019           type = cp_parser_type_id (parser);
8020           /* Look for the closing `)'.  */
8021           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8022           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8023         }
8024
8025       /* Restore the saved message.  */
8026       parser->type_definition_forbidden_message = saved_message;
8027
8028       /* At this point this can only be either a cast or a
8029          parenthesized ctor such as `(T ())' that looks like a cast to
8030          function returning T.  */
8031       if (!cp_parser_error_occurred (parser))
8032         {
8033           /* Only commit if the cast-expression doesn't start with
8034              '++', '--', or '[' in C++11.  */
8035           if (cast_expression > 0)
8036             cp_parser_commit_to_topmost_tentative_parse (parser);
8037
8038           expr = cp_parser_cast_expression (parser,
8039                                             /*address_p=*/false,
8040                                             /*cast_p=*/true,
8041                                             /*decltype_p=*/false,
8042                                             pidk);
8043
8044           if (cp_parser_parse_definitely (parser))
8045             {
8046               /* Warn about old-style casts, if so requested.  */
8047               if (warn_old_style_cast
8048                   && !in_system_header_at (input_location)
8049                   && !VOID_TYPE_P (type)
8050                   && current_lang_name != lang_name_c)
8051                 warning (OPT_Wold_style_cast, "use of old-style cast");
8052
8053               /* Only type conversions to integral or enumeration types
8054                  can be used in constant-expressions.  */
8055               if (!cast_valid_in_integral_constant_expression_p (type)
8056                   && cp_parser_non_integral_constant_expression (parser,
8057                                                                  NIC_CAST))
8058                 return error_mark_node;
8059
8060               /* Perform the cast.  */
8061               expr = build_c_cast (input_location, type, expr);
8062               return expr;
8063             }
8064         }
8065       else 
8066         cp_parser_abort_tentative_parse (parser);
8067     }
8068
8069   /* If we get here, then it's not a cast, so it must be a
8070      unary-expression.  */
8071   return cp_parser_unary_expression (parser, pidk, address_p,
8072                                      cast_p, decltype_p);
8073 }
8074
8075 /* Parse a binary expression of the general form:
8076
8077    pm-expression:
8078      cast-expression
8079      pm-expression .* cast-expression
8080      pm-expression ->* cast-expression
8081
8082    multiplicative-expression:
8083      pm-expression
8084      multiplicative-expression * pm-expression
8085      multiplicative-expression / pm-expression
8086      multiplicative-expression % pm-expression
8087
8088    additive-expression:
8089      multiplicative-expression
8090      additive-expression + multiplicative-expression
8091      additive-expression - multiplicative-expression
8092
8093    shift-expression:
8094      additive-expression
8095      shift-expression << additive-expression
8096      shift-expression >> additive-expression
8097
8098    relational-expression:
8099      shift-expression
8100      relational-expression < shift-expression
8101      relational-expression > shift-expression
8102      relational-expression <= shift-expression
8103      relational-expression >= shift-expression
8104
8105   GNU Extension:
8106
8107    relational-expression:
8108      relational-expression <? shift-expression
8109      relational-expression >? shift-expression
8110
8111    equality-expression:
8112      relational-expression
8113      equality-expression == relational-expression
8114      equality-expression != relational-expression
8115
8116    and-expression:
8117      equality-expression
8118      and-expression & equality-expression
8119
8120    exclusive-or-expression:
8121      and-expression
8122      exclusive-or-expression ^ and-expression
8123
8124    inclusive-or-expression:
8125      exclusive-or-expression
8126      inclusive-or-expression | exclusive-or-expression
8127
8128    logical-and-expression:
8129      inclusive-or-expression
8130      logical-and-expression && inclusive-or-expression
8131
8132    logical-or-expression:
8133      logical-and-expression
8134      logical-or-expression || logical-and-expression
8135
8136    All these are implemented with a single function like:
8137
8138    binary-expression:
8139      simple-cast-expression
8140      binary-expression <token> binary-expression
8141
8142    CAST_P is true if this expression is the target of a cast.
8143
8144    The binops_by_token map is used to get the tree codes for each <token> type.
8145    binary-expressions are associated according to a precedence table.  */
8146
8147 #define TOKEN_PRECEDENCE(token)                              \
8148 (((token->type == CPP_GREATER                                \
8149    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8150   && !parser->greater_than_is_operator_p)                    \
8151  ? PREC_NOT_OPERATOR                                         \
8152  : binops_by_token[token->type].prec)
8153
8154 static tree
8155 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8156                              bool no_toplevel_fold_p,
8157                              bool decltype_p,
8158                              enum cp_parser_prec prec,
8159                              cp_id_kind * pidk)
8160 {
8161   cp_parser_expression_stack stack;
8162   cp_parser_expression_stack_entry *sp = &stack[0];
8163   cp_parser_expression_stack_entry current;
8164   tree rhs;
8165   cp_token *token;
8166   enum tree_code rhs_type;
8167   enum cp_parser_prec new_prec, lookahead_prec;
8168   tree overload;
8169
8170   /* Parse the first expression.  */
8171   current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8172                       ? TRUTH_NOT_EXPR : ERROR_MARK);
8173   current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8174                                            cast_p, decltype_p, pidk);
8175   current.prec = prec;
8176
8177   if (cp_parser_error_occurred (parser))
8178     return error_mark_node;
8179
8180   for (;;)
8181     {
8182       /* Get an operator token.  */
8183       token = cp_lexer_peek_token (parser->lexer);
8184
8185       if (warn_cxx0x_compat
8186           && token->type == CPP_RSHIFT
8187           && !parser->greater_than_is_operator_p)
8188         {
8189           if (warning_at (token->location, OPT_Wc__0x_compat,
8190                           "%<>>%> operator is treated"
8191                           " as two right angle brackets in C++11"))
8192             inform (token->location,
8193                     "suggest parentheses around %<>>%> expression");
8194         }
8195
8196       new_prec = TOKEN_PRECEDENCE (token);
8197
8198       /* Popping an entry off the stack means we completed a subexpression:
8199          - either we found a token which is not an operator (`>' where it is not
8200            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8201            will happen repeatedly;
8202          - or, we found an operator which has lower priority.  This is the case
8203            where the recursive descent *ascends*, as in `3 * 4 + 5' after
8204            parsing `3 * 4'.  */
8205       if (new_prec <= current.prec)
8206         {
8207           if (sp == stack)
8208             break;
8209           else
8210             goto pop;
8211         }
8212
8213      get_rhs:
8214       current.tree_type = binops_by_token[token->type].tree_type;
8215       current.loc = token->location;
8216
8217       /* We used the operator token.  */
8218       cp_lexer_consume_token (parser->lexer);
8219
8220       /* For "false && x" or "true || x", x will never be executed;
8221          disable warnings while evaluating it.  */
8222       if (current.tree_type == TRUTH_ANDIF_EXPR)
8223         c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8224       else if (current.tree_type == TRUTH_ORIF_EXPR)
8225         c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8226
8227       /* Extract another operand.  It may be the RHS of this expression
8228          or the LHS of a new, higher priority expression.  */
8229       rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8230                   ? TRUTH_NOT_EXPR : ERROR_MARK);
8231       rhs = cp_parser_simple_cast_expression (parser);
8232
8233       /* Get another operator token.  Look up its precedence to avoid
8234          building a useless (immediately popped) stack entry for common
8235          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
8236       token = cp_lexer_peek_token (parser->lexer);
8237       lookahead_prec = TOKEN_PRECEDENCE (token);
8238       if (lookahead_prec > new_prec)
8239         {
8240           /* ... and prepare to parse the RHS of the new, higher priority
8241              expression.  Since precedence levels on the stack are
8242              monotonically increasing, we do not have to care about
8243              stack overflows.  */
8244           *sp = current;
8245           ++sp;
8246           current.lhs = rhs;
8247           current.lhs_type = rhs_type;
8248           current.prec = new_prec;
8249           new_prec = lookahead_prec;
8250           goto get_rhs;
8251
8252          pop:
8253           lookahead_prec = new_prec;
8254           /* If the stack is not empty, we have parsed into LHS the right side
8255              (`4' in the example above) of an expression we had suspended.
8256              We can use the information on the stack to recover the LHS (`3')
8257              from the stack together with the tree code (`MULT_EXPR'), and
8258              the precedence of the higher level subexpression
8259              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
8260              which will be used to actually build the additive expression.  */
8261           rhs = current.lhs;
8262           rhs_type = current.lhs_type;
8263           --sp;
8264           current = *sp;
8265         }
8266
8267       /* Undo the disabling of warnings done above.  */
8268       if (current.tree_type == TRUTH_ANDIF_EXPR)
8269         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8270       else if (current.tree_type == TRUTH_ORIF_EXPR)
8271         c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8272
8273       if (warn_logical_not_paren
8274           && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8275           && current.lhs_type == TRUTH_NOT_EXPR
8276           /* Avoid warning for !!x == y.  */
8277           && (TREE_CODE (current.lhs) != NE_EXPR
8278               || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8279           && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8280               || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8281                   /* Avoid warning for !b == y where b is boolean.  */
8282                   && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8283                       || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8284                           != BOOLEAN_TYPE))))
8285           /* Avoid warning for !!b == y where b is boolean.  */
8286           && (!DECL_P (current.lhs)
8287               || TREE_TYPE (current.lhs) == NULL_TREE
8288               || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8289         warn_logical_not_parentheses (current.loc, current.tree_type,
8290                                       maybe_constant_value (rhs));
8291
8292       overload = NULL;
8293       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8294          ERROR_MARK for everything that is not a binary expression.
8295          This makes warn_about_parentheses miss some warnings that
8296          involve unary operators.  For unary expressions we should
8297          pass the correct tree_code unless the unary expression was
8298          surrounded by parentheses.
8299       */
8300       if (no_toplevel_fold_p
8301           && lookahead_prec <= current.prec
8302           && sp == stack)
8303         current.lhs = build2 (current.tree_type,
8304                               TREE_CODE_CLASS (current.tree_type)
8305                               == tcc_comparison
8306                               ? boolean_type_node : TREE_TYPE (current.lhs),
8307                               current.lhs, rhs);
8308       else
8309         current.lhs = build_x_binary_op (current.loc, current.tree_type,
8310                                          current.lhs, current.lhs_type,
8311                                          rhs, rhs_type, &overload,
8312                                          complain_flags (decltype_p));
8313       current.lhs_type = current.tree_type;
8314       if (EXPR_P (current.lhs))
8315         SET_EXPR_LOCATION (current.lhs, current.loc);
8316
8317       /* If the binary operator required the use of an overloaded operator,
8318          then this expression cannot be an integral constant-expression.
8319          An overloaded operator can be used even if both operands are
8320          otherwise permissible in an integral constant-expression if at
8321          least one of the operands is of enumeration type.  */
8322
8323       if (overload
8324           && cp_parser_non_integral_constant_expression (parser,
8325                                                          NIC_OVERLOADED))
8326         return error_mark_node;
8327     }
8328
8329   return current.lhs;
8330 }
8331
8332 static tree
8333 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8334                              bool no_toplevel_fold_p,
8335                              enum cp_parser_prec prec,
8336                              cp_id_kind * pidk)
8337 {
8338   return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8339                                       /*decltype*/false, prec, pidk);
8340 }
8341
8342 /* Parse the `? expression : assignment-expression' part of a
8343    conditional-expression.  The LOGICAL_OR_EXPR is the
8344    logical-or-expression that started the conditional-expression.
8345    Returns a representation of the entire conditional-expression.
8346
8347    This routine is used by cp_parser_assignment_expression.
8348
8349      ? expression : assignment-expression
8350
8351    GNU Extensions:
8352
8353      ? : assignment-expression */
8354
8355 static tree
8356 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8357 {
8358   tree expr;
8359   tree assignment_expr;
8360   struct cp_token *token;
8361   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8362
8363   /* Consume the `?' token.  */
8364   cp_lexer_consume_token (parser->lexer);
8365   token = cp_lexer_peek_token (parser->lexer);
8366   if (cp_parser_allow_gnu_extensions_p (parser)
8367       && token->type == CPP_COLON)
8368     {
8369       pedwarn (token->location, OPT_Wpedantic, 
8370                "ISO C++ does not allow ?: with omitted middle operand");
8371       /* Implicit true clause.  */
8372       expr = NULL_TREE;
8373       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8374       warn_for_omitted_condop (token->location, logical_or_expr);
8375     }
8376   else
8377     {
8378       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8379       parser->colon_corrects_to_scope_p = false;
8380       /* Parse the expression.  */
8381       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8382       expr = cp_parser_expression (parser);
8383       c_inhibit_evaluation_warnings +=
8384         ((logical_or_expr == truthvalue_true_node)
8385          - (logical_or_expr == truthvalue_false_node));
8386       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8387     }
8388
8389   /* The next token should be a `:'.  */
8390   cp_parser_require (parser, CPP_COLON, RT_COLON);
8391   /* Parse the assignment-expression.  */
8392   assignment_expr = cp_parser_assignment_expression (parser);
8393   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8394
8395   /* Build the conditional-expression.  */
8396   return build_x_conditional_expr (loc, logical_or_expr,
8397                                    expr,
8398                                    assignment_expr,
8399                                    tf_warning_or_error);
8400 }
8401
8402 /* Parse an assignment-expression.
8403
8404    assignment-expression:
8405      conditional-expression
8406      logical-or-expression assignment-operator assignment_expression
8407      throw-expression
8408
8409    CAST_P is true if this expression is the target of a cast.
8410    DECLTYPE_P is true if this expression is the operand of decltype.
8411
8412    Returns a representation for the expression.  */
8413
8414 static tree
8415 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
8416                                  bool cast_p, bool decltype_p)
8417 {
8418   tree expr;
8419
8420   /* If the next token is the `throw' keyword, then we're looking at
8421      a throw-expression.  */
8422   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8423     expr = cp_parser_throw_expression (parser);
8424   /* Otherwise, it must be that we are looking at a
8425      logical-or-expression.  */
8426   else
8427     {
8428       /* Parse the binary expressions (logical-or-expression).  */
8429       expr = cp_parser_binary_expression (parser, cast_p, false,
8430                                           decltype_p,
8431                                           PREC_NOT_OPERATOR, pidk);
8432       /* If the next token is a `?' then we're actually looking at a
8433          conditional-expression.  */
8434       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8435         return cp_parser_question_colon_clause (parser, expr);
8436       else
8437         {
8438           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8439
8440           /* If it's an assignment-operator, we're using the second
8441              production.  */
8442           enum tree_code assignment_operator
8443             = cp_parser_assignment_operator_opt (parser);
8444           if (assignment_operator != ERROR_MARK)
8445             {
8446               bool non_constant_p;
8447               location_t saved_input_location;
8448
8449               /* Parse the right-hand side of the assignment.  */
8450               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8451
8452               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8453                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8454
8455               /* An assignment may not appear in a
8456                  constant-expression.  */
8457               if (cp_parser_non_integral_constant_expression (parser,
8458                                                               NIC_ASSIGNMENT))
8459                 return error_mark_node;
8460               /* Build the assignment expression.  Its default
8461                  location is the location of the '=' token.  */
8462               saved_input_location = input_location;
8463               input_location = loc;
8464               expr = build_x_modify_expr (loc, expr,
8465                                           assignment_operator,
8466                                           rhs,
8467                                           complain_flags (decltype_p));
8468               input_location = saved_input_location;
8469             }
8470         }
8471     }
8472
8473   return expr;
8474 }
8475
8476 /* Parse an (optional) assignment-operator.
8477
8478    assignment-operator: one of
8479      = *= /= %= += -= >>= <<= &= ^= |=
8480
8481    GNU Extension:
8482
8483    assignment-operator: one of
8484      <?= >?=
8485
8486    If the next token is an assignment operator, the corresponding tree
8487    code is returned, and the token is consumed.  For example, for
8488    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
8489    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
8490    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
8491    operator, ERROR_MARK is returned.  */
8492
8493 static enum tree_code
8494 cp_parser_assignment_operator_opt (cp_parser* parser)
8495 {
8496   enum tree_code op;
8497   cp_token *token;
8498
8499   /* Peek at the next token.  */
8500   token = cp_lexer_peek_token (parser->lexer);
8501
8502   switch (token->type)
8503     {
8504     case CPP_EQ:
8505       op = NOP_EXPR;
8506       break;
8507
8508     case CPP_MULT_EQ:
8509       op = MULT_EXPR;
8510       break;
8511
8512     case CPP_DIV_EQ:
8513       op = TRUNC_DIV_EXPR;
8514       break;
8515
8516     case CPP_MOD_EQ:
8517       op = TRUNC_MOD_EXPR;
8518       break;
8519
8520     case CPP_PLUS_EQ:
8521       op = PLUS_EXPR;
8522       break;
8523
8524     case CPP_MINUS_EQ:
8525       op = MINUS_EXPR;
8526       break;
8527
8528     case CPP_RSHIFT_EQ:
8529       op = RSHIFT_EXPR;
8530       break;
8531
8532     case CPP_LSHIFT_EQ:
8533       op = LSHIFT_EXPR;
8534       break;
8535
8536     case CPP_AND_EQ:
8537       op = BIT_AND_EXPR;
8538       break;
8539
8540     case CPP_XOR_EQ:
8541       op = BIT_XOR_EXPR;
8542       break;
8543
8544     case CPP_OR_EQ:
8545       op = BIT_IOR_EXPR;
8546       break;
8547
8548     default:
8549       /* Nothing else is an assignment operator.  */
8550       op = ERROR_MARK;
8551     }
8552
8553   /* If it was an assignment operator, consume it.  */
8554   if (op != ERROR_MARK)
8555     cp_lexer_consume_token (parser->lexer);
8556
8557   return op;
8558 }
8559
8560 /* Parse an expression.
8561
8562    expression:
8563      assignment-expression
8564      expression , assignment-expression
8565
8566    CAST_P is true if this expression is the target of a cast.
8567    DECLTYPE_P is true if this expression is the immediate operand of decltype,
8568      except possibly parenthesized or on the RHS of a comma (N3276).
8569
8570    Returns a representation of the expression.  */
8571
8572 static tree
8573 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8574                       bool cast_p, bool decltype_p)
8575 {
8576   tree expression = NULL_TREE;
8577   location_t loc = UNKNOWN_LOCATION;
8578
8579   while (true)
8580     {
8581       tree assignment_expression;
8582
8583       /* Parse the next assignment-expression.  */
8584       assignment_expression
8585         = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
8586
8587       /* We don't create a temporary for a call that is the immediate operand
8588          of decltype or on the RHS of a comma.  But when we see a comma, we
8589          need to create a temporary for a call on the LHS.  */
8590       if (decltype_p && !processing_template_decl
8591           && TREE_CODE (assignment_expression) == CALL_EXPR
8592           && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8593           && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8594         assignment_expression
8595           = build_cplus_new (TREE_TYPE (assignment_expression),
8596                              assignment_expression, tf_warning_or_error);
8597
8598       /* If this is the first assignment-expression, we can just
8599          save it away.  */
8600       if (!expression)
8601         expression = assignment_expression;
8602       else
8603         expression = build_x_compound_expr (loc, expression,
8604                                             assignment_expression,
8605                                             complain_flags (decltype_p));
8606       /* If the next token is not a comma, then we are done with the
8607          expression.  */
8608       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8609         break;
8610       /* Consume the `,'.  */
8611       loc = cp_lexer_peek_token (parser->lexer)->location;
8612       cp_lexer_consume_token (parser->lexer);
8613       /* A comma operator cannot appear in a constant-expression.  */
8614       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8615         expression = error_mark_node;
8616     }
8617
8618   return expression;
8619 }
8620
8621 /* Parse a constant-expression.
8622
8623    constant-expression:
8624      conditional-expression
8625
8626   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8627   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
8628   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
8629   is false, NON_CONSTANT_P should be NULL.  */
8630
8631 static tree
8632 cp_parser_constant_expression (cp_parser* parser,
8633                                bool allow_non_constant_p,
8634                                bool *non_constant_p)
8635 {
8636   bool saved_integral_constant_expression_p;
8637   bool saved_allow_non_integral_constant_expression_p;
8638   bool saved_non_integral_constant_expression_p;
8639   tree expression;
8640
8641   /* It might seem that we could simply parse the
8642      conditional-expression, and then check to see if it were
8643      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
8644      one that the compiler can figure out is constant, possibly after
8645      doing some simplifications or optimizations.  The standard has a
8646      precise definition of constant-expression, and we must honor
8647      that, even though it is somewhat more restrictive.
8648
8649      For example:
8650
8651        int i[(2, 3)];
8652
8653      is not a legal declaration, because `(2, 3)' is not a
8654      constant-expression.  The `,' operator is forbidden in a
8655      constant-expression.  However, GCC's constant-folding machinery
8656      will fold this operation to an INTEGER_CST for `3'.  */
8657
8658   /* Save the old settings.  */
8659   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8660   saved_allow_non_integral_constant_expression_p
8661     = parser->allow_non_integral_constant_expression_p;
8662   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8663   /* We are now parsing a constant-expression.  */
8664   parser->integral_constant_expression_p = true;
8665   parser->allow_non_integral_constant_expression_p
8666     = (allow_non_constant_p || cxx_dialect >= cxx11);
8667   parser->non_integral_constant_expression_p = false;
8668   /* Although the grammar says "conditional-expression", we parse an
8669      "assignment-expression", which also permits "throw-expression"
8670      and the use of assignment operators.  In the case that
8671      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8672      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
8673      actually essential that we look for an assignment-expression.
8674      For example, cp_parser_initializer_clauses uses this function to
8675      determine whether a particular assignment-expression is in fact
8676      constant.  */
8677   expression = cp_parser_assignment_expression (parser);
8678   /* Restore the old settings.  */
8679   parser->integral_constant_expression_p
8680     = saved_integral_constant_expression_p;
8681   parser->allow_non_integral_constant_expression_p
8682     = saved_allow_non_integral_constant_expression_p;
8683   if (cxx_dialect >= cxx11)
8684     {
8685       /* Require an rvalue constant expression here; that's what our
8686          callers expect.  Reference constant expressions are handled
8687          separately in e.g. cp_parser_template_argument.  */
8688       bool is_const = potential_rvalue_constant_expression (expression);
8689       parser->non_integral_constant_expression_p = !is_const;
8690       if (!is_const && !allow_non_constant_p)
8691         require_potential_rvalue_constant_expression (expression);
8692     }
8693   if (allow_non_constant_p)
8694     *non_constant_p = parser->non_integral_constant_expression_p;
8695   parser->non_integral_constant_expression_p
8696     = saved_non_integral_constant_expression_p;
8697
8698   return expression;
8699 }
8700
8701 /* Parse __builtin_offsetof.
8702
8703    offsetof-expression:
8704      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8705
8706    offsetof-member-designator:
8707      id-expression
8708      | offsetof-member-designator "." id-expression
8709      | offsetof-member-designator "[" expression "]"
8710      | offsetof-member-designator "->" id-expression  */
8711
8712 static tree
8713 cp_parser_builtin_offsetof (cp_parser *parser)
8714 {
8715   int save_ice_p, save_non_ice_p;
8716   tree type, expr;
8717   cp_id_kind dummy;
8718   cp_token *token;
8719
8720   /* We're about to accept non-integral-constant things, but will
8721      definitely yield an integral constant expression.  Save and
8722      restore these values around our local parsing.  */
8723   save_ice_p = parser->integral_constant_expression_p;
8724   save_non_ice_p = parser->non_integral_constant_expression_p;
8725
8726   /* Consume the "__builtin_offsetof" token.  */
8727   cp_lexer_consume_token (parser->lexer);
8728   /* Consume the opening `('.  */
8729   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8730   /* Parse the type-id.  */
8731   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8732   type = cp_parser_type_id (parser);
8733   /* Look for the `,'.  */
8734   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8735   token = cp_lexer_peek_token (parser->lexer);
8736
8737   /* Build the (type *)null that begins the traditional offsetof macro.  */
8738   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8739                             tf_warning_or_error);
8740
8741   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
8742   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8743                                                  true, &dummy, token->location);
8744   while (true)
8745     {
8746       token = cp_lexer_peek_token (parser->lexer);
8747       switch (token->type)
8748         {
8749         case CPP_OPEN_SQUARE:
8750           /* offsetof-member-designator "[" expression "]" */
8751           expr = cp_parser_postfix_open_square_expression (parser, expr,
8752                                                            true, false);
8753           break;
8754
8755         case CPP_DEREF:
8756           /* offsetof-member-designator "->" identifier */
8757           expr = grok_array_decl (token->location, expr,
8758                                   integer_zero_node, false);
8759           /* FALLTHRU */
8760
8761         case CPP_DOT:
8762           /* offsetof-member-designator "." identifier */
8763           cp_lexer_consume_token (parser->lexer);
8764           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8765                                                          expr, true, &dummy,
8766                                                          token->location);
8767           break;
8768
8769         case CPP_CLOSE_PAREN:
8770           /* Consume the ")" token.  */
8771           cp_lexer_consume_token (parser->lexer);
8772           goto success;
8773
8774         default:
8775           /* Error.  We know the following require will fail, but
8776              that gives the proper error message.  */
8777           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8778           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8779           expr = error_mark_node;
8780           goto failure;
8781         }
8782     }
8783
8784  success:
8785   expr = finish_offsetof (expr, loc);
8786
8787  failure:
8788   parser->integral_constant_expression_p = save_ice_p;
8789   parser->non_integral_constant_expression_p = save_non_ice_p;
8790
8791   return expr;
8792 }
8793
8794 /* Parse a trait expression.
8795
8796    Returns a representation of the expression, the underlying type
8797    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
8798
8799 static tree
8800 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8801 {
8802   cp_trait_kind kind;
8803   tree type1, type2 = NULL_TREE;
8804   bool binary = false;
8805   bool variadic = false;
8806
8807   switch (keyword)
8808     {
8809     case RID_HAS_NOTHROW_ASSIGN:
8810       kind = CPTK_HAS_NOTHROW_ASSIGN;
8811       break;
8812     case RID_HAS_NOTHROW_CONSTRUCTOR:
8813       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8814       break;
8815     case RID_HAS_NOTHROW_COPY:
8816       kind = CPTK_HAS_NOTHROW_COPY;
8817       break;
8818     case RID_HAS_TRIVIAL_ASSIGN:
8819       kind = CPTK_HAS_TRIVIAL_ASSIGN;
8820       break;
8821     case RID_HAS_TRIVIAL_CONSTRUCTOR:
8822       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8823       break;
8824     case RID_HAS_TRIVIAL_COPY:
8825       kind = CPTK_HAS_TRIVIAL_COPY;
8826       break;
8827     case RID_HAS_TRIVIAL_DESTRUCTOR:
8828       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8829       break;
8830     case RID_HAS_VIRTUAL_DESTRUCTOR:
8831       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8832       break;
8833     case RID_IS_ABSTRACT:
8834       kind = CPTK_IS_ABSTRACT;
8835       break;
8836     case RID_IS_BASE_OF:
8837       kind = CPTK_IS_BASE_OF;
8838       binary = true;
8839       break;
8840     case RID_IS_CLASS:
8841       kind = CPTK_IS_CLASS;
8842       break;
8843     case RID_IS_EMPTY:
8844       kind = CPTK_IS_EMPTY;
8845       break;
8846     case RID_IS_ENUM:
8847       kind = CPTK_IS_ENUM;
8848       break;
8849     case RID_IS_FINAL:
8850       kind = CPTK_IS_FINAL;
8851       break;
8852     case RID_IS_LITERAL_TYPE:
8853       kind = CPTK_IS_LITERAL_TYPE;
8854       break;
8855     case RID_IS_POD:
8856       kind = CPTK_IS_POD;
8857       break;
8858     case RID_IS_POLYMORPHIC:
8859       kind = CPTK_IS_POLYMORPHIC;
8860       break;
8861     case RID_IS_STD_LAYOUT:
8862       kind = CPTK_IS_STD_LAYOUT;
8863       break;
8864     case RID_IS_TRIVIAL:
8865       kind = CPTK_IS_TRIVIAL;
8866       break;
8867     case RID_IS_TRIVIALLY_ASSIGNABLE:
8868       kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
8869       binary = true;
8870       break;
8871     case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
8872       kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
8873       variadic = true;
8874       break;
8875     case RID_IS_TRIVIALLY_COPYABLE:
8876       kind = CPTK_IS_TRIVIALLY_COPYABLE;
8877       break;
8878     case RID_IS_UNION:
8879       kind = CPTK_IS_UNION;
8880       break;
8881     case RID_UNDERLYING_TYPE:
8882       kind = CPTK_UNDERLYING_TYPE;
8883       break;
8884     case RID_BASES:
8885       kind = CPTK_BASES;
8886       break;
8887     case RID_DIRECT_BASES:
8888       kind = CPTK_DIRECT_BASES;
8889       break;
8890     default:
8891       gcc_unreachable ();
8892     }
8893
8894   /* Consume the token.  */
8895   cp_lexer_consume_token (parser->lexer);
8896
8897   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8898
8899   type1 = cp_parser_type_id (parser);
8900
8901   if (type1 == error_mark_node)
8902     return error_mark_node;
8903
8904   if (binary)
8905     {
8906       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8907  
8908       type2 = cp_parser_type_id (parser);
8909
8910       if (type2 == error_mark_node)
8911         return error_mark_node;
8912     }
8913   else if (variadic)
8914     {
8915       while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8916         {
8917           cp_lexer_consume_token (parser->lexer);
8918           tree elt = cp_parser_type_id (parser);
8919           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8920             {
8921               cp_lexer_consume_token (parser->lexer);
8922               elt = make_pack_expansion (elt);
8923             }
8924           if (elt == error_mark_node)
8925             return error_mark_node;
8926           type2 = tree_cons (NULL_TREE, elt, type2);
8927         }
8928     }
8929
8930   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8931
8932   /* Complete the trait expression, which may mean either processing
8933      the trait expr now or saving it for template instantiation.  */
8934   switch(kind)
8935     {
8936     case CPTK_UNDERLYING_TYPE:
8937       return finish_underlying_type (type1);
8938     case CPTK_BASES:
8939       return finish_bases (type1, false);
8940     case CPTK_DIRECT_BASES:
8941       return finish_bases (type1, true);
8942     default:
8943       return finish_trait_expr (kind, type1, type2);
8944     }
8945 }
8946
8947 /* Lambdas that appear in variable initializer or default argument scope
8948    get that in their mangling, so we need to record it.  We might as well
8949    use the count for function and namespace scopes as well.  */
8950 static GTY(()) tree lambda_scope;
8951 static GTY(()) int lambda_count;
8952 typedef struct GTY(()) tree_int
8953 {
8954   tree t;
8955   int i;
8956 } tree_int;
8957 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8958
8959 static void
8960 start_lambda_scope (tree decl)
8961 {
8962   tree_int ti;
8963   gcc_assert (decl);
8964   /* Once we're inside a function, we ignore other scopes and just push
8965      the function again so that popping works properly.  */
8966   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8967     decl = current_function_decl;
8968   ti.t = lambda_scope;
8969   ti.i = lambda_count;
8970   vec_safe_push (lambda_scope_stack, ti);
8971   if (lambda_scope != decl)
8972     {
8973       /* Don't reset the count if we're still in the same function.  */
8974       lambda_scope = decl;
8975       lambda_count = 0;
8976     }
8977 }
8978
8979 static void
8980 record_lambda_scope (tree lambda)
8981 {
8982   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8983   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8984 }
8985
8986 static void
8987 finish_lambda_scope (void)
8988 {
8989   tree_int *p = &lambda_scope_stack->last ();
8990   if (lambda_scope != p->t)
8991     {
8992       lambda_scope = p->t;
8993       lambda_count = p->i;
8994     }
8995   lambda_scope_stack->pop ();
8996 }
8997
8998 /* Parse a lambda expression.
8999
9000    lambda-expression:
9001      lambda-introducer lambda-declarator [opt] compound-statement
9002
9003    Returns a representation of the expression.  */
9004
9005 static tree
9006 cp_parser_lambda_expression (cp_parser* parser)
9007 {
9008   tree lambda_expr = build_lambda_expr ();
9009   tree type;
9010   bool ok = true;
9011   cp_token *token = cp_lexer_peek_token (parser->lexer);
9012   cp_token_position start = 0;
9013
9014   LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9015
9016   if (cp_unevaluated_operand)
9017     {
9018       if (!token->error_reported)
9019         {
9020           error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9021                     "lambda-expression in unevaluated context");
9022           token->error_reported = true;
9023         }
9024       ok = false;
9025     }
9026   else if (parser->in_template_argument_list_p)
9027     {
9028       if (!token->error_reported)
9029         {
9030           error_at (token->location, "lambda-expression in template-argument");
9031           token->error_reported = true;
9032         }
9033       ok = false;
9034     }
9035
9036   /* We may be in the middle of deferred access check.  Disable
9037      it now.  */
9038   push_deferring_access_checks (dk_no_deferred);
9039
9040   cp_parser_lambda_introducer (parser, lambda_expr);
9041
9042   type = begin_lambda_type (lambda_expr);
9043   if (type == error_mark_node)
9044     return error_mark_node;
9045
9046   record_lambda_scope (lambda_expr);
9047
9048   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
9049   determine_visibility (TYPE_NAME (type));
9050
9051   /* Now that we've started the type, add the capture fields for any
9052      explicit captures.  */
9053   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9054
9055   {
9056     /* Inside the class, surrounding template-parameter-lists do not apply.  */
9057     unsigned int saved_num_template_parameter_lists
9058         = parser->num_template_parameter_lists;
9059     unsigned char in_statement = parser->in_statement;
9060     bool in_switch_statement_p = parser->in_switch_statement_p;
9061     bool fully_implicit_function_template_p
9062         = parser->fully_implicit_function_template_p;
9063     tree implicit_template_parms = parser->implicit_template_parms;
9064     cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9065     bool auto_is_implicit_function_template_parm_p
9066         = parser->auto_is_implicit_function_template_parm_p;
9067
9068     parser->num_template_parameter_lists = 0;
9069     parser->in_statement = 0;
9070     parser->in_switch_statement_p = false;
9071     parser->fully_implicit_function_template_p = false;
9072     parser->implicit_template_parms = 0;
9073     parser->implicit_template_scope = 0;
9074     parser->auto_is_implicit_function_template_parm_p = false;
9075
9076     /* By virtue of defining a local class, a lambda expression has access to
9077        the private variables of enclosing classes.  */
9078
9079     ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9080
9081     if (ok)
9082       {
9083         if (!cp_parser_error_occurred (parser)
9084             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9085             && cp_parser_start_tentative_firewall (parser))
9086           start = token;
9087         cp_parser_lambda_body (parser, lambda_expr);
9088       }
9089     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9090       {
9091         if (cp_parser_skip_to_closing_brace (parser))
9092           cp_lexer_consume_token (parser->lexer);
9093       }
9094
9095     /* The capture list was built up in reverse order; fix that now.  */
9096     LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9097       = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9098
9099     if (ok)
9100       maybe_add_lambda_conv_op (type);
9101
9102     type = finish_struct (type, /*attributes=*/NULL_TREE);
9103
9104     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9105     parser->in_statement = in_statement;
9106     parser->in_switch_statement_p = in_switch_statement_p;
9107     parser->fully_implicit_function_template_p
9108         = fully_implicit_function_template_p;
9109     parser->implicit_template_parms = implicit_template_parms;
9110     parser->implicit_template_scope = implicit_template_scope;
9111     parser->auto_is_implicit_function_template_parm_p
9112         = auto_is_implicit_function_template_parm_p;
9113   }
9114
9115   pop_deferring_access_checks ();
9116
9117   /* This field is only used during parsing of the lambda.  */
9118   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9119
9120   /* This lambda shouldn't have any proxies left at this point.  */
9121   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9122   /* And now that we're done, push proxies for an enclosing lambda.  */
9123   insert_pending_capture_proxies ();
9124
9125   if (ok)
9126     lambda_expr = build_lambda_object (lambda_expr);
9127   else
9128     lambda_expr = error_mark_node;
9129
9130   cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9131
9132   return lambda_expr;
9133 }
9134
9135 /* Parse the beginning of a lambda expression.
9136
9137    lambda-introducer:
9138      [ lambda-capture [opt] ]
9139
9140    LAMBDA_EXPR is the current representation of the lambda expression.  */
9141
9142 static void
9143 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9144 {
9145   /* Need commas after the first capture.  */
9146   bool first = true;
9147
9148   /* Eat the leading `['.  */
9149   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9150
9151   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
9152   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9153       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9154     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9155   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9156     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9157
9158   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9159     {
9160       cp_lexer_consume_token (parser->lexer);
9161       first = false;
9162     }
9163
9164   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9165     {
9166       cp_token* capture_token;
9167       tree capture_id;
9168       tree capture_init_expr;
9169       cp_id_kind idk = CP_ID_KIND_NONE;
9170       bool explicit_init_p = false;
9171
9172       enum capture_kind_type
9173       {
9174         BY_COPY,
9175         BY_REFERENCE
9176       };
9177       enum capture_kind_type capture_kind = BY_COPY;
9178
9179       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9180         {
9181           error ("expected end of capture-list");
9182           return;
9183         }
9184
9185       if (first)
9186         first = false;
9187       else
9188         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9189
9190       /* Possibly capture `this'.  */
9191       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9192         {
9193           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9194           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9195             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9196                      "with by-copy capture default");
9197           cp_lexer_consume_token (parser->lexer);
9198           add_capture (lambda_expr,
9199                        /*id=*/this_identifier,
9200                        /*initializer=*/finish_this_expr(),
9201                        /*by_reference_p=*/false,
9202                        explicit_init_p);
9203           continue;
9204         }
9205
9206       /* Remember whether we want to capture as a reference or not.  */
9207       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9208         {
9209           capture_kind = BY_REFERENCE;
9210           cp_lexer_consume_token (parser->lexer);
9211         }
9212
9213       /* Get the identifier.  */
9214       capture_token = cp_lexer_peek_token (parser->lexer);
9215       capture_id = cp_parser_identifier (parser);
9216
9217       if (capture_id == error_mark_node)
9218         /* Would be nice to have a cp_parser_skip_to_closing_x for general
9219            delimiters, but I modified this to stop on unnested ']' as well.  It
9220            was already changed to stop on unnested '}', so the
9221            "closing_parenthesis" name is no more misleading with my change.  */
9222         {
9223           cp_parser_skip_to_closing_parenthesis (parser,
9224                                                  /*recovering=*/true,
9225                                                  /*or_comma=*/true,
9226                                                  /*consume_paren=*/true);
9227           break;
9228         }
9229
9230       /* Find the initializer for this capture.  */
9231       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9232           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9233           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9234         {
9235           bool direct, non_constant;
9236           /* An explicit initializer exists.  */
9237           if (cxx_dialect < cxx14)
9238             pedwarn (input_location, 0,
9239                      "lambda capture initializers "
9240                      "only available with -std=c++14 or -std=gnu++14");
9241           capture_init_expr = cp_parser_initializer (parser, &direct,
9242                                                      &non_constant);
9243           explicit_init_p = true;
9244           if (capture_init_expr == NULL_TREE)
9245             {
9246               error ("empty initializer for lambda init-capture");
9247               capture_init_expr = error_mark_node;
9248             }
9249         }
9250       else
9251         {
9252           const char* error_msg;
9253
9254           /* Turn the identifier into an id-expression.  */
9255           capture_init_expr
9256             = cp_parser_lookup_name_simple (parser, capture_id,
9257                                             capture_token->location);
9258
9259           if (capture_init_expr == error_mark_node)
9260             {
9261               unqualified_name_lookup_error (capture_id);
9262               continue;
9263             }
9264           else if (DECL_P (capture_init_expr)
9265                    && (!VAR_P (capture_init_expr)
9266                        && TREE_CODE (capture_init_expr) != PARM_DECL))
9267             {
9268               error_at (capture_token->location,
9269                         "capture of non-variable %qD ",
9270                         capture_init_expr);
9271               inform (0, "%q+#D declared here", capture_init_expr);
9272               continue;
9273             }
9274           if (VAR_P (capture_init_expr)
9275               && decl_storage_duration (capture_init_expr) != dk_auto)
9276             {
9277               if (pedwarn (capture_token->location, 0, "capture of variable "
9278                            "%qD with non-automatic storage duration",
9279                            capture_init_expr))
9280                 inform (0, "%q+#D declared here", capture_init_expr);
9281               continue;
9282             }
9283
9284           capture_init_expr
9285             = finish_id_expression
9286                 (capture_id,
9287                  capture_init_expr,
9288                  parser->scope,
9289                  &idk,
9290                  /*integral_constant_expression_p=*/false,
9291                  /*allow_non_integral_constant_expression_p=*/false,
9292                  /*non_integral_constant_expression_p=*/NULL,
9293                  /*template_p=*/false,
9294                  /*done=*/true,
9295                  /*address_p=*/false,
9296                  /*template_arg_p=*/false,
9297                  &error_msg,
9298                  capture_token->location);
9299
9300           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9301             {
9302               cp_lexer_consume_token (parser->lexer);
9303               capture_init_expr = make_pack_expansion (capture_init_expr);
9304             }
9305           else
9306             check_for_bare_parameter_packs (capture_init_expr);
9307         }
9308
9309       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9310           && !explicit_init_p)
9311         {
9312           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9313               && capture_kind == BY_COPY)
9314             pedwarn (capture_token->location, 0, "explicit by-copy capture "
9315                      "of %qD redundant with by-copy capture default",
9316                      capture_id);
9317           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9318               && capture_kind == BY_REFERENCE)
9319             pedwarn (capture_token->location, 0, "explicit by-reference "
9320                      "capture of %qD redundant with by-reference capture "
9321                      "default", capture_id);
9322         }
9323
9324       add_capture (lambda_expr,
9325                    capture_id,
9326                    capture_init_expr,
9327                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
9328                    explicit_init_p);
9329     }
9330
9331   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9332 }
9333
9334 /* Parse the (optional) middle of a lambda expression.
9335
9336    lambda-declarator:
9337      < template-parameter-list [opt] >
9338      ( parameter-declaration-clause [opt] )
9339        attribute-specifier [opt]
9340        mutable [opt]
9341        exception-specification [opt]
9342        lambda-return-type-clause [opt]
9343
9344    LAMBDA_EXPR is the current representation of the lambda expression.  */
9345
9346 static bool
9347 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9348 {
9349   /* 5.1.1.4 of the standard says:
9350        If a lambda-expression does not include a lambda-declarator, it is as if
9351        the lambda-declarator were ().
9352      This means an empty parameter list, no attributes, and no exception
9353      specification.  */
9354   tree param_list = void_list_node;
9355   tree attributes = NULL_TREE;
9356   tree exception_spec = NULL_TREE;
9357   tree template_param_list = NULL_TREE;
9358
9359   /* The template-parameter-list is optional, but must begin with
9360      an opening angle if present.  */
9361   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9362     {
9363       if (cxx_dialect < cxx14)
9364         pedwarn (parser->lexer->next_token->location, 0,
9365                  "lambda templates are only available with "
9366                  "-std=c++14 or -std=gnu++14");
9367
9368       cp_lexer_consume_token (parser->lexer);
9369
9370       template_param_list = cp_parser_template_parameter_list (parser);
9371
9372       cp_parser_skip_to_end_of_template_parameter_list (parser);
9373
9374       /* We just processed one more parameter list.  */
9375       ++parser->num_template_parameter_lists;
9376     }
9377
9378   /* The parameter-declaration-clause is optional (unless
9379      template-parameter-list was given), but must begin with an
9380      opening parenthesis if present.  */
9381   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9382     {
9383       cp_lexer_consume_token (parser->lexer);
9384
9385       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9386
9387       /* Parse parameters.  */
9388       param_list = cp_parser_parameter_declaration_clause (parser);
9389
9390       /* Default arguments shall not be specified in the
9391          parameter-declaration-clause of a lambda-declarator.  */
9392       for (tree t = param_list; t; t = TREE_CHAIN (t))
9393         if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
9394           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9395                    "default argument specified for lambda parameter");
9396
9397       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9398
9399       attributes = cp_parser_attributes_opt (parser);
9400
9401       /* Parse optional `mutable' keyword.  */
9402       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9403         {
9404           cp_lexer_consume_token (parser->lexer);
9405           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9406         }
9407
9408       /* Parse optional exception specification.  */
9409       exception_spec = cp_parser_exception_specification_opt (parser);
9410
9411       /* Parse optional trailing return type.  */
9412       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9413         {
9414           cp_lexer_consume_token (parser->lexer);
9415           LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9416             = cp_parser_trailing_type_id (parser);
9417         }
9418
9419       /* The function parameters must be in scope all the way until after the
9420          trailing-return-type in case of decltype.  */
9421       pop_bindings_and_leave_scope ();
9422     }
9423   else if (template_param_list != NULL_TREE) // generate diagnostic
9424     cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9425
9426   /* Create the function call operator.
9427
9428      Messing with declarators like this is no uglier than building up the
9429      FUNCTION_DECL by hand, and this is less likely to get out of sync with
9430      other code.  */
9431   {
9432     cp_decl_specifier_seq return_type_specs;
9433     cp_declarator* declarator;
9434     tree fco;
9435     int quals;
9436     void *p;
9437
9438     clear_decl_specs (&return_type_specs);
9439     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9440       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9441     else
9442       /* Maybe we will deduce the return type later.  */
9443       return_type_specs.type = make_auto ();
9444
9445     p = obstack_alloc (&declarator_obstack, 0);
9446
9447     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9448                                      sfk_none);
9449
9450     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9451              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9452     declarator = make_call_declarator (declarator, param_list, quals,
9453                                        VIRT_SPEC_UNSPECIFIED,
9454                                        REF_QUAL_NONE,
9455                                        exception_spec,
9456                                        /*late_return_type=*/NULL_TREE);
9457     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9458
9459     fco = grokmethod (&return_type_specs,
9460                       declarator,
9461                       attributes);
9462     if (fco != error_mark_node)
9463       {
9464         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9465         DECL_ARTIFICIAL (fco) = 1;
9466         /* Give the object parameter a different name.  */
9467         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9468         if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9469           TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9470       }
9471     if (template_param_list)
9472       {
9473         fco = finish_member_template_decl (fco);
9474         finish_template_decl (template_param_list);
9475         --parser->num_template_parameter_lists;
9476       }
9477     else if (parser->fully_implicit_function_template_p)
9478       fco = finish_fully_implicit_template (parser, fco);
9479
9480     finish_member_declaration (fco);
9481
9482     obstack_free (&declarator_obstack, p);
9483
9484     return (fco != error_mark_node);
9485   }
9486 }
9487
9488 /* Parse the body of a lambda expression, which is simply
9489
9490    compound-statement
9491
9492    but which requires special handling.
9493    LAMBDA_EXPR is the current representation of the lambda expression.  */
9494
9495 static void
9496 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9497 {
9498   bool nested = (current_function_decl != NULL_TREE);
9499   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9500   if (nested)
9501     push_function_context ();
9502   else
9503     /* Still increment function_depth so that we don't GC in the
9504        middle of an expression.  */
9505     ++function_depth;
9506   /* Clear this in case we're in the middle of a default argument.  */
9507   parser->local_variables_forbidden_p = false;
9508
9509   /* Finish the function call operator
9510      - class_specifier
9511      + late_parsing_for_member
9512      + function_definition_after_declarator
9513      + ctor_initializer_opt_and_function_body  */
9514   {
9515     tree fco = lambda_function (lambda_expr);
9516     tree body;
9517     bool done = false;
9518     tree compound_stmt;
9519     tree cap;
9520
9521     /* Let the front end know that we are going to be defining this
9522        function.  */
9523     start_preparsed_function (fco,
9524                               NULL_TREE,
9525                               SF_PRE_PARSED | SF_INCLASS_INLINE);
9526
9527     start_lambda_scope (fco);
9528     body = begin_function_body ();
9529
9530     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9531       goto out;
9532
9533     /* Push the proxies for any explicit captures.  */
9534     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9535          cap = TREE_CHAIN (cap))
9536       build_capture_proxy (TREE_PURPOSE (cap));
9537
9538     compound_stmt = begin_compound_stmt (0);
9539
9540     /* 5.1.1.4 of the standard says:
9541          If a lambda-expression does not include a trailing-return-type, it
9542          is as if the trailing-return-type denotes the following type:
9543           * if the compound-statement is of the form
9544                { return attribute-specifier [opt] expression ; }
9545              the type of the returned expression after lvalue-to-rvalue
9546              conversion (_conv.lval_ 4.1), array-to-pointer conversion
9547              (_conv.array_ 4.2), and function-to-pointer conversion
9548              (_conv.func_ 4.3);
9549           * otherwise, void.  */
9550
9551     /* In a lambda that has neither a lambda-return-type-clause
9552        nor a deducible form, errors should be reported for return statements
9553        in the body.  Since we used void as the placeholder return type, parsing
9554        the body as usual will give such desired behavior.  */
9555     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9556         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9557         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9558       {
9559         tree expr = NULL_TREE;
9560         cp_id_kind idk = CP_ID_KIND_NONE;
9561
9562         /* Parse tentatively in case there's more after the initial return
9563            statement.  */
9564         cp_parser_parse_tentatively (parser);
9565
9566         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9567
9568         expr = cp_parser_expression (parser, &idk);
9569
9570         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9571         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9572
9573         if (cp_parser_parse_definitely (parser))
9574           {
9575             if (!processing_template_decl)
9576               apply_deduced_return_type (fco, lambda_return_type (expr));
9577
9578             /* Will get error here if type not deduced yet.  */
9579             finish_return_stmt (expr);
9580
9581             done = true;
9582           }
9583       }
9584
9585     if (!done)
9586       {
9587         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9588           cp_parser_label_declaration (parser);
9589         cp_parser_statement_seq_opt (parser, NULL_TREE);
9590         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9591       }
9592
9593     finish_compound_stmt (compound_stmt);
9594
9595   out:
9596     finish_function_body (body);
9597     finish_lambda_scope ();
9598
9599     /* Finish the function and generate code for it if necessary.  */
9600     tree fn = finish_function (/*inline*/2);
9601
9602     /* Only expand if the call op is not a template.  */
9603     if (!DECL_TEMPLATE_INFO (fco))
9604       expand_or_defer_fn (fn);
9605   }
9606
9607   parser->local_variables_forbidden_p = local_variables_forbidden_p;
9608   if (nested)
9609     pop_function_context();
9610   else
9611     --function_depth;
9612 }
9613
9614 /* Statements [gram.stmt.stmt]  */
9615
9616 /* Parse a statement.
9617
9618    statement:
9619      labeled-statement
9620      expression-statement
9621      compound-statement
9622      selection-statement
9623      iteration-statement
9624      jump-statement
9625      declaration-statement
9626      try-block
9627
9628   C++11:
9629
9630   statement:
9631     labeled-statement
9632     attribute-specifier-seq (opt) expression-statement
9633     attribute-specifier-seq (opt) compound-statement
9634     attribute-specifier-seq (opt) selection-statement
9635     attribute-specifier-seq (opt) iteration-statement
9636     attribute-specifier-seq (opt) jump-statement
9637     declaration-statement
9638     attribute-specifier-seq (opt) try-block
9639
9640   TM Extension:
9641
9642    statement:
9643      atomic-statement
9644
9645   IN_COMPOUND is true when the statement is nested inside a
9646   cp_parser_compound_statement; this matters for certain pragmas.
9647
9648   If IF_P is not NULL, *IF_P is set to indicate whether the statement
9649   is a (possibly labeled) if statement which is not enclosed in braces
9650   and has an else clause.  This is used to implement -Wparentheses.  */
9651
9652 static void
9653 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9654                      bool in_compound, bool *if_p)
9655 {
9656   tree statement, std_attrs = NULL_TREE;
9657   cp_token *token;
9658   location_t statement_location, attrs_location;
9659
9660  restart:
9661   if (if_p != NULL)
9662     *if_p = false;
9663   /* There is no statement yet.  */
9664   statement = NULL_TREE;
9665
9666   saved_token_sentinel saved_tokens (parser->lexer);
9667   attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9668   if (c_dialect_objc ())
9669     /* In obj-c++, seeing '[[' might be the either the beginning of
9670        c++11 attributes, or a nested objc-message-expression.  So
9671        let's parse the c++11 attributes tentatively.  */
9672     cp_parser_parse_tentatively (parser);
9673   std_attrs = cp_parser_std_attribute_spec_seq (parser);
9674   if (c_dialect_objc ())
9675     {
9676       if (!cp_parser_parse_definitely (parser))
9677         std_attrs = NULL_TREE;
9678     }
9679
9680   /* Peek at the next token.  */
9681   token = cp_lexer_peek_token (parser->lexer);
9682   /* Remember the location of the first token in the statement.  */
9683   statement_location = token->location;
9684   /* If this is a keyword, then that will often determine what kind of
9685      statement we have.  */
9686   if (token->type == CPP_KEYWORD)
9687     {
9688       enum rid keyword = token->keyword;
9689
9690       switch (keyword)
9691         {
9692         case RID_CASE:
9693         case RID_DEFAULT:
9694           /* Looks like a labeled-statement with a case label.
9695              Parse the label, and then use tail recursion to parse
9696              the statement.  */
9697           cp_parser_label_for_labeled_statement (parser, std_attrs);
9698           goto restart;
9699
9700         case RID_IF:
9701         case RID_SWITCH:
9702           statement = cp_parser_selection_statement (parser, if_p);
9703           break;
9704
9705         case RID_WHILE:
9706         case RID_DO:
9707         case RID_FOR:
9708           statement = cp_parser_iteration_statement (parser, false);
9709           break;
9710
9711         case RID_CILK_FOR:
9712           if (!flag_cilkplus)
9713             {
9714               error_at (cp_lexer_peek_token (parser->lexer)->location,
9715                         "-fcilkplus must be enabled to use %<_Cilk_for%>");
9716               cp_lexer_consume_token (parser->lexer);
9717               statement = error_mark_node;
9718             }
9719           else
9720             statement = cp_parser_cilk_for (parser, integer_zero_node);
9721           break;
9722
9723         case RID_BREAK:
9724         case RID_CONTINUE:
9725         case RID_RETURN:
9726         case RID_GOTO:
9727           statement = cp_parser_jump_statement (parser);
9728           break;
9729
9730         case RID_CILK_SYNC:
9731           cp_lexer_consume_token (parser->lexer);
9732           if (flag_cilkplus)
9733             {
9734               tree sync_expr = build_cilk_sync ();
9735               SET_EXPR_LOCATION (sync_expr,
9736                                  token->location);
9737               statement = finish_expr_stmt (sync_expr);
9738             }
9739           else
9740             {
9741               error_at (token->location, "-fcilkplus must be enabled to use"
9742                         " %<_Cilk_sync%>");
9743               statement = error_mark_node;
9744             }
9745           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9746           break;
9747
9748           /* Objective-C++ exception-handling constructs.  */
9749         case RID_AT_TRY:
9750         case RID_AT_CATCH:
9751         case RID_AT_FINALLY:
9752         case RID_AT_SYNCHRONIZED:
9753         case RID_AT_THROW:
9754           statement = cp_parser_objc_statement (parser);
9755           break;
9756
9757         case RID_TRY:
9758           statement = cp_parser_try_block (parser);
9759           break;
9760
9761         case RID_NAMESPACE:
9762           /* This must be a namespace alias definition.  */
9763           cp_parser_declaration_statement (parser);
9764           return;
9765           
9766         case RID_TRANSACTION_ATOMIC:
9767         case RID_TRANSACTION_RELAXED:
9768           statement = cp_parser_transaction (parser, keyword);
9769           break;
9770         case RID_TRANSACTION_CANCEL:
9771           statement = cp_parser_transaction_cancel (parser);
9772           break;
9773
9774         default:
9775           /* It might be a keyword like `int' that can start a
9776              declaration-statement.  */
9777           break;
9778         }
9779     }
9780   else if (token->type == CPP_NAME)
9781     {
9782       /* If the next token is a `:', then we are looking at a
9783          labeled-statement.  */
9784       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9785       if (token->type == CPP_COLON)
9786         {
9787           /* Looks like a labeled-statement with an ordinary label.
9788              Parse the label, and then use tail recursion to parse
9789              the statement.  */
9790
9791           cp_parser_label_for_labeled_statement (parser, std_attrs);
9792           goto restart;
9793         }
9794     }
9795   /* Anything that starts with a `{' must be a compound-statement.  */
9796   else if (token->type == CPP_OPEN_BRACE)
9797     statement = cp_parser_compound_statement (parser, NULL, false, false);
9798   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9799      a statement all its own.  */
9800   else if (token->type == CPP_PRAGMA)
9801     {
9802       /* Only certain OpenMP pragmas are attached to statements, and thus
9803          are considered statements themselves.  All others are not.  In
9804          the context of a compound, accept the pragma as a "statement" and
9805          return so that we can check for a close brace.  Otherwise we
9806          require a real statement and must go back and read one.  */
9807       if (in_compound)
9808         cp_parser_pragma (parser, pragma_compound);
9809       else if (!cp_parser_pragma (parser, pragma_stmt))
9810         goto restart;
9811       return;
9812     }
9813   else if (token->type == CPP_EOF)
9814     {
9815       cp_parser_error (parser, "expected statement");
9816       return;
9817     }
9818
9819   /* Everything else must be a declaration-statement or an
9820      expression-statement.  Try for the declaration-statement
9821      first, unless we are looking at a `;', in which case we know that
9822      we have an expression-statement.  */
9823   if (!statement)
9824     {
9825       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9826         {
9827           if (std_attrs != NULL_TREE)
9828             {
9829               /*  Attributes should be parsed as part of the the
9830                   declaration, so let's un-parse them.  */
9831               saved_tokens.rollback();
9832               std_attrs = NULL_TREE;
9833             }
9834
9835           cp_parser_parse_tentatively (parser);
9836           /* Try to parse the declaration-statement.  */
9837           cp_parser_declaration_statement (parser);
9838           /* If that worked, we're done.  */
9839           if (cp_parser_parse_definitely (parser))
9840             return;
9841         }
9842       /* Look for an expression-statement instead.  */
9843       statement = cp_parser_expression_statement (parser, in_statement_expr);
9844     }
9845
9846   /* Set the line number for the statement.  */
9847   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9848     SET_EXPR_LOCATION (statement, statement_location);
9849
9850   /* Note that for now, we don't do anything with c++11 statements
9851      parsed at this level.  */
9852   if (std_attrs != NULL_TREE)
9853     warning_at (attrs_location,
9854                 OPT_Wattributes,
9855                 "attributes at the beginning of statement are ignored");
9856 }
9857
9858 /* Parse the label for a labeled-statement, i.e.
9859
9860    identifier :
9861    case constant-expression :
9862    default :
9863
9864    GNU Extension:
9865    case constant-expression ... constant-expression : statement
9866
9867    When a label is parsed without errors, the label is added to the
9868    parse tree by the finish_* functions, so this function doesn't
9869    have to return the label.  */
9870
9871 static void
9872 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9873 {
9874   cp_token *token;
9875   tree label = NULL_TREE;
9876   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9877
9878   /* The next token should be an identifier.  */
9879   token = cp_lexer_peek_token (parser->lexer);
9880   if (token->type != CPP_NAME
9881       && token->type != CPP_KEYWORD)
9882     {
9883       cp_parser_error (parser, "expected labeled-statement");
9884       return;
9885     }
9886
9887   parser->colon_corrects_to_scope_p = false;
9888   switch (token->keyword)
9889     {
9890     case RID_CASE:
9891       {
9892         tree expr, expr_hi;
9893         cp_token *ellipsis;
9894
9895         /* Consume the `case' token.  */
9896         cp_lexer_consume_token (parser->lexer);
9897         /* Parse the constant-expression.  */
9898         expr = cp_parser_constant_expression (parser);
9899         if (check_for_bare_parameter_packs (expr))
9900           expr = error_mark_node;
9901
9902         ellipsis = cp_lexer_peek_token (parser->lexer);
9903         if (ellipsis->type == CPP_ELLIPSIS)
9904           {
9905             /* Consume the `...' token.  */
9906             cp_lexer_consume_token (parser->lexer);
9907             expr_hi = cp_parser_constant_expression (parser);
9908             if (check_for_bare_parameter_packs (expr_hi))
9909               expr_hi = error_mark_node;
9910
9911             /* We don't need to emit warnings here, as the common code
9912                will do this for us.  */
9913           }
9914         else
9915           expr_hi = NULL_TREE;
9916
9917         if (parser->in_switch_statement_p)
9918           finish_case_label (token->location, expr, expr_hi);
9919         else
9920           error_at (token->location,
9921                     "case label %qE not within a switch statement",
9922                     expr);
9923       }
9924       break;
9925
9926     case RID_DEFAULT:
9927       /* Consume the `default' token.  */
9928       cp_lexer_consume_token (parser->lexer);
9929
9930       if (parser->in_switch_statement_p)
9931         finish_case_label (token->location, NULL_TREE, NULL_TREE);
9932       else
9933         error_at (token->location, "case label not within a switch statement");
9934       break;
9935
9936     default:
9937       /* Anything else must be an ordinary label.  */
9938       label = finish_label_stmt (cp_parser_identifier (parser));
9939       break;
9940     }
9941
9942   /* Require the `:' token.  */
9943   cp_parser_require (parser, CPP_COLON, RT_COLON);
9944
9945   /* An ordinary label may optionally be followed by attributes.
9946      However, this is only permitted if the attributes are then
9947      followed by a semicolon.  This is because, for backward
9948      compatibility, when parsing
9949        lab: __attribute__ ((unused)) int i;
9950      we want the attribute to attach to "i", not "lab".  */
9951   if (label != NULL_TREE
9952       && cp_next_tokens_can_be_gnu_attribute_p (parser))
9953     {
9954       tree attrs;
9955       cp_parser_parse_tentatively (parser);
9956       attrs = cp_parser_gnu_attributes_opt (parser);
9957       if (attrs == NULL_TREE
9958           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9959         cp_parser_abort_tentative_parse (parser);
9960       else if (!cp_parser_parse_definitely (parser))
9961         ;
9962       else
9963         attributes = chainon (attributes, attrs);
9964     }
9965
9966   if (attributes != NULL_TREE)
9967     cplus_decl_attributes (&label, attributes, 0);
9968
9969   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9970 }
9971
9972 /* Parse an expression-statement.
9973
9974    expression-statement:
9975      expression [opt] ;
9976
9977    Returns the new EXPR_STMT -- or NULL_TREE if the expression
9978    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9979    indicates whether this expression-statement is part of an
9980    expression statement.  */
9981
9982 static tree
9983 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9984 {
9985   tree statement = NULL_TREE;
9986   cp_token *token = cp_lexer_peek_token (parser->lexer);
9987
9988   /* If the next token is a ';', then there is no expression
9989      statement.  */
9990   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9991     {
9992       statement = cp_parser_expression (parser);
9993       if (statement == error_mark_node
9994           && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9995         {
9996           cp_parser_skip_to_end_of_block_or_statement (parser);
9997           return error_mark_node;
9998         }
9999     }
10000
10001   /* Give a helpful message for "A<T>::type t;" and the like.  */
10002   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10003       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10004     {
10005       if (TREE_CODE (statement) == SCOPE_REF)
10006         error_at (token->location, "need %<typename%> before %qE because "
10007                   "%qT is a dependent scope",
10008                   statement, TREE_OPERAND (statement, 0));
10009       else if (is_overloaded_fn (statement)
10010                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10011         {
10012           /* A::A a; */
10013           tree fn = get_first_fn (statement);
10014           error_at (token->location,
10015                     "%<%T::%D%> names the constructor, not the type",
10016                     DECL_CONTEXT (fn), DECL_NAME (fn));
10017         }
10018     }
10019
10020   /* Consume the final `;'.  */
10021   cp_parser_consume_semicolon_at_end_of_statement (parser);
10022
10023   if (in_statement_expr
10024       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10025     /* This is the final expression statement of a statement
10026        expression.  */
10027     statement = finish_stmt_expr_expr (statement, in_statement_expr);
10028   else if (statement)
10029     statement = finish_expr_stmt (statement);
10030
10031   return statement;
10032 }
10033
10034 /* Parse a compound-statement.
10035
10036    compound-statement:
10037      { statement-seq [opt] }
10038
10039    GNU extension:
10040
10041    compound-statement:
10042      { label-declaration-seq [opt] statement-seq [opt] }
10043
10044    label-declaration-seq:
10045      label-declaration
10046      label-declaration-seq label-declaration
10047
10048    Returns a tree representing the statement.  */
10049
10050 static tree
10051 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10052                               bool in_try, bool function_body)
10053 {
10054   tree compound_stmt;
10055
10056   /* Consume the `{'.  */
10057   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10058     return error_mark_node;
10059   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10060       && !function_body && cxx_dialect < cxx14)
10061     pedwarn (input_location, OPT_Wpedantic,
10062              "compound-statement in constexpr function");
10063   /* Begin the compound-statement.  */
10064   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
10065   /* If the next keyword is `__label__' we have a label declaration.  */
10066   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10067     cp_parser_label_declaration (parser);
10068   /* Parse an (optional) statement-seq.  */
10069   cp_parser_statement_seq_opt (parser, in_statement_expr);
10070   /* Finish the compound-statement.  */
10071   finish_compound_stmt (compound_stmt);
10072   /* Consume the `}'.  */
10073   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10074
10075   return compound_stmt;
10076 }
10077
10078 /* Parse an (optional) statement-seq.
10079
10080    statement-seq:
10081      statement
10082      statement-seq [opt] statement  */
10083
10084 static void
10085 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10086 {
10087   /* Scan statements until there aren't any more.  */
10088   while (true)
10089     {
10090       cp_token *token = cp_lexer_peek_token (parser->lexer);
10091
10092       /* If we are looking at a `}', then we have run out of
10093          statements; the same is true if we have reached the end
10094          of file, or have stumbled upon a stray '@end'.  */
10095       if (token->type == CPP_CLOSE_BRACE
10096           || token->type == CPP_EOF
10097           || token->type == CPP_PRAGMA_EOL
10098           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10099         break;
10100       
10101       /* If we are in a compound statement and find 'else' then
10102          something went wrong.  */
10103       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10104         {
10105           if (parser->in_statement & IN_IF_STMT) 
10106             break;
10107           else
10108             {
10109               token = cp_lexer_consume_token (parser->lexer);
10110               error_at (token->location, "%<else%> without a previous %<if%>");
10111             }
10112         }
10113
10114       /* Parse the statement.  */
10115       cp_parser_statement (parser, in_statement_expr, true, NULL);
10116     }
10117 }
10118
10119 /* Parse a selection-statement.
10120
10121    selection-statement:
10122      if ( condition ) statement
10123      if ( condition ) statement else statement
10124      switch ( condition ) statement
10125
10126    Returns the new IF_STMT or SWITCH_STMT.
10127
10128    If IF_P is not NULL, *IF_P is set to indicate whether the statement
10129    is a (possibly labeled) if statement which is not enclosed in
10130    braces and has an else clause.  This is used to implement
10131    -Wparentheses.  */
10132
10133 static tree
10134 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
10135 {
10136   cp_token *token;
10137   enum rid keyword;
10138
10139   if (if_p != NULL)
10140     *if_p = false;
10141
10142   /* Peek at the next token.  */
10143   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10144
10145   /* See what kind of keyword it is.  */
10146   keyword = token->keyword;
10147   switch (keyword)
10148     {
10149     case RID_IF:
10150     case RID_SWITCH:
10151       {
10152         tree statement;
10153         tree condition;
10154
10155         /* Look for the `('.  */
10156         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10157           {
10158             cp_parser_skip_to_end_of_statement (parser);
10159             return error_mark_node;
10160           }
10161
10162         /* Begin the selection-statement.  */
10163         if (keyword == RID_IF)
10164           statement = begin_if_stmt ();
10165         else
10166           statement = begin_switch_stmt ();
10167
10168         /* Parse the condition.  */
10169         condition = cp_parser_condition (parser);
10170         /* Look for the `)'.  */
10171         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10172           cp_parser_skip_to_closing_parenthesis (parser, true, false,
10173                                                  /*consume_paren=*/true);
10174
10175         if (keyword == RID_IF)
10176           {
10177             bool nested_if;
10178             unsigned char in_statement;
10179
10180             /* Add the condition.  */
10181             finish_if_stmt_cond (condition, statement);
10182
10183             /* Parse the then-clause.  */
10184             in_statement = parser->in_statement;
10185             parser->in_statement |= IN_IF_STMT;
10186             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10187               {
10188                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10189                 add_stmt (build_empty_stmt (loc));
10190                 cp_lexer_consume_token (parser->lexer);
10191                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10192                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
10193                               "empty body in an %<if%> statement");
10194                 nested_if = false;
10195               }
10196             else
10197               cp_parser_implicitly_scoped_statement (parser, &nested_if);
10198             parser->in_statement = in_statement;
10199
10200             finish_then_clause (statement);
10201
10202             /* If the next token is `else', parse the else-clause.  */
10203             if (cp_lexer_next_token_is_keyword (parser->lexer,
10204                                                 RID_ELSE))
10205               {
10206                 /* Consume the `else' keyword.  */
10207                 cp_lexer_consume_token (parser->lexer);
10208                 begin_else_clause (statement);
10209                 /* Parse the else-clause.  */
10210                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10211                   {
10212                     location_t loc;
10213                     loc = cp_lexer_peek_token (parser->lexer)->location;
10214                     warning_at (loc,
10215                                 OPT_Wempty_body, "suggest braces around "
10216                                 "empty body in an %<else%> statement");
10217                     add_stmt (build_empty_stmt (loc));
10218                     cp_lexer_consume_token (parser->lexer);
10219                   }
10220                 else
10221                   cp_parser_implicitly_scoped_statement (parser, NULL);
10222
10223                 finish_else_clause (statement);
10224
10225                 /* If we are currently parsing a then-clause, then
10226                    IF_P will not be NULL.  We set it to true to
10227                    indicate that this if statement has an else clause.
10228                    This may trigger the Wparentheses warning below
10229                    when we get back up to the parent if statement.  */
10230                 if (if_p != NULL)
10231                   *if_p = true;
10232               }
10233             else
10234               {
10235                 /* This if statement does not have an else clause.  If
10236                    NESTED_IF is true, then the then-clause is an if
10237                    statement which does have an else clause.  We warn
10238                    about the potential ambiguity.  */
10239                 if (nested_if)
10240                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10241                               "suggest explicit braces to avoid ambiguous"
10242                               " %<else%>");
10243               }
10244
10245             /* Now we're all done with the if-statement.  */
10246             finish_if_stmt (statement);
10247           }
10248         else
10249           {
10250             bool in_switch_statement_p;
10251             unsigned char in_statement;
10252
10253             /* Add the condition.  */
10254             finish_switch_cond (condition, statement);
10255
10256             /* Parse the body of the switch-statement.  */
10257             in_switch_statement_p = parser->in_switch_statement_p;
10258             in_statement = parser->in_statement;
10259             parser->in_switch_statement_p = true;
10260             parser->in_statement |= IN_SWITCH_STMT;
10261             cp_parser_implicitly_scoped_statement (parser, NULL);
10262             parser->in_switch_statement_p = in_switch_statement_p;
10263             parser->in_statement = in_statement;
10264
10265             /* Now we're all done with the switch-statement.  */
10266             finish_switch_stmt (statement);
10267           }
10268
10269         return statement;
10270       }
10271       break;
10272
10273     default:
10274       cp_parser_error (parser, "expected selection-statement");
10275       return error_mark_node;
10276     }
10277 }
10278
10279 /* Parse a condition.
10280
10281    condition:
10282      expression
10283      type-specifier-seq declarator = initializer-clause
10284      type-specifier-seq declarator braced-init-list
10285
10286    GNU Extension:
10287
10288    condition:
10289      type-specifier-seq declarator asm-specification [opt]
10290        attributes [opt] = assignment-expression
10291
10292    Returns the expression that should be tested.  */
10293
10294 static tree
10295 cp_parser_condition (cp_parser* parser)
10296 {
10297   cp_decl_specifier_seq type_specifiers;
10298   const char *saved_message;
10299   int declares_class_or_enum;
10300
10301   /* Try the declaration first.  */
10302   cp_parser_parse_tentatively (parser);
10303   /* New types are not allowed in the type-specifier-seq for a
10304      condition.  */
10305   saved_message = parser->type_definition_forbidden_message;
10306   parser->type_definition_forbidden_message
10307     = G_("types may not be defined in conditions");
10308   /* Parse the type-specifier-seq.  */
10309   cp_parser_decl_specifier_seq (parser,
10310                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10311                                 &type_specifiers,
10312                                 &declares_class_or_enum);
10313   /* Restore the saved message.  */
10314   parser->type_definition_forbidden_message = saved_message;
10315   /* If all is well, we might be looking at a declaration.  */
10316   if (!cp_parser_error_occurred (parser))
10317     {
10318       tree decl;
10319       tree asm_specification;
10320       tree attributes;
10321       cp_declarator *declarator;
10322       tree initializer = NULL_TREE;
10323
10324       /* Parse the declarator.  */
10325       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10326                                          /*ctor_dtor_or_conv_p=*/NULL,
10327                                          /*parenthesized_p=*/NULL,
10328                                          /*member_p=*/false,
10329                                          /*friend_p=*/false);
10330       /* Parse the attributes.  */
10331       attributes = cp_parser_attributes_opt (parser);
10332       /* Parse the asm-specification.  */
10333       asm_specification = cp_parser_asm_specification_opt (parser);
10334       /* If the next token is not an `=' or '{', then we might still be
10335          looking at an expression.  For example:
10336
10337            if (A(a).x)
10338
10339          looks like a decl-specifier-seq and a declarator -- but then
10340          there is no `=', so this is an expression.  */
10341       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10342           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10343         cp_parser_simulate_error (parser);
10344         
10345       /* If we did see an `=' or '{', then we are looking at a declaration
10346          for sure.  */
10347       if (cp_parser_parse_definitely (parser))
10348         {
10349           tree pushed_scope;
10350           bool non_constant_p;
10351           bool flags = LOOKUP_ONLYCONVERTING;
10352
10353           /* Create the declaration.  */
10354           decl = start_decl (declarator, &type_specifiers,
10355                              /*initialized_p=*/true,
10356                              attributes, /*prefix_attributes=*/NULL_TREE,
10357                              &pushed_scope);
10358
10359           /* Parse the initializer.  */
10360           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10361             {
10362               initializer = cp_parser_braced_list (parser, &non_constant_p);
10363               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10364               flags = 0;
10365             }
10366           else
10367             {
10368               /* Consume the `='.  */
10369               cp_parser_require (parser, CPP_EQ, RT_EQ);
10370               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10371             }
10372           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10373             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10374
10375           /* Process the initializer.  */
10376           cp_finish_decl (decl,
10377                           initializer, !non_constant_p,
10378                           asm_specification,
10379                           flags);
10380
10381           if (pushed_scope)
10382             pop_scope (pushed_scope);
10383
10384           return convert_from_reference (decl);
10385         }
10386     }
10387   /* If we didn't even get past the declarator successfully, we are
10388      definitely not looking at a declaration.  */
10389   else
10390     cp_parser_abort_tentative_parse (parser);
10391
10392   /* Otherwise, we are looking at an expression.  */
10393   return cp_parser_expression (parser);
10394 }
10395
10396 /* Parses a for-statement or range-for-statement until the closing ')',
10397    not included. */
10398
10399 static tree
10400 cp_parser_for (cp_parser *parser, bool ivdep)
10401 {
10402   tree init, scope, decl;
10403   bool is_range_for;
10404
10405   /* Begin the for-statement.  */
10406   scope = begin_for_scope (&init);
10407
10408   /* Parse the initialization.  */
10409   is_range_for = cp_parser_for_init_statement (parser, &decl);
10410
10411   if (is_range_for)
10412     return cp_parser_range_for (parser, scope, init, decl, ivdep);
10413   else
10414     return cp_parser_c_for (parser, scope, init, ivdep);
10415 }
10416
10417 static tree
10418 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10419 {
10420   /* Normal for loop */
10421   tree condition = NULL_TREE;
10422   tree expression = NULL_TREE;
10423   tree stmt;
10424
10425   stmt = begin_for_stmt (scope, init);
10426   /* The for-init-statement has already been parsed in
10427      cp_parser_for_init_statement, so no work is needed here.  */
10428   finish_for_init_stmt (stmt);
10429
10430   /* If there's a condition, process it.  */
10431   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10432     condition = cp_parser_condition (parser);
10433   else if (ivdep)
10434     {
10435       cp_parser_error (parser, "missing loop condition in loop with "
10436                        "%<GCC ivdep%> pragma");
10437       condition = error_mark_node;
10438     }
10439   finish_for_cond (condition, stmt, ivdep);
10440   /* Look for the `;'.  */
10441   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10442
10443   /* If there's an expression, process it.  */
10444   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10445     expression = cp_parser_expression (parser);
10446   finish_for_expr (expression, stmt);
10447
10448   return stmt;
10449 }
10450
10451 /* Tries to parse a range-based for-statement:
10452
10453   range-based-for:
10454     decl-specifier-seq declarator : expression
10455
10456   The decl-specifier-seq declarator and the `:' are already parsed by
10457   cp_parser_for_init_statement. If processing_template_decl it returns a
10458   newly created RANGE_FOR_STMT; if not, it is converted to a
10459   regular FOR_STMT.  */
10460
10461 static tree
10462 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10463                      bool ivdep)
10464 {
10465   tree stmt, range_expr;
10466
10467   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10468     {
10469       bool expr_non_constant_p;
10470       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10471     }
10472   else
10473     range_expr = cp_parser_expression (parser);
10474
10475   /* If in template, STMT is converted to a normal for-statement
10476      at instantiation. If not, it is done just ahead. */
10477   if (processing_template_decl)
10478     {
10479       if (check_for_bare_parameter_packs (range_expr))
10480         range_expr = error_mark_node;
10481       stmt = begin_range_for_stmt (scope, init);
10482       if (ivdep)
10483         RANGE_FOR_IVDEP (stmt) = 1;
10484       finish_range_for_decl (stmt, range_decl, range_expr);
10485       if (!type_dependent_expression_p (range_expr)
10486           /* do_auto_deduction doesn't mess with template init-lists.  */
10487           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10488         do_range_for_auto_deduction (range_decl, range_expr);
10489     }
10490   else
10491     {
10492       stmt = begin_for_stmt (scope, init);
10493       stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10494     }
10495   return stmt;
10496 }
10497
10498 /* Subroutine of cp_convert_range_for: given the initializer expression,
10499    builds up the range temporary.  */
10500
10501 static tree
10502 build_range_temp (tree range_expr)
10503 {
10504   tree range_type, range_temp;
10505
10506   /* Find out the type deduced by the declaration
10507      `auto &&__range = range_expr'.  */
10508   range_type = cp_build_reference_type (make_auto (), true);
10509   range_type = do_auto_deduction (range_type, range_expr,
10510                                   type_uses_auto (range_type));
10511
10512   /* Create the __range variable.  */
10513   range_temp = build_decl (input_location, VAR_DECL,
10514                            get_identifier ("__for_range"), range_type);
10515   TREE_USED (range_temp) = 1;
10516   DECL_ARTIFICIAL (range_temp) = 1;
10517
10518   return range_temp;
10519 }
10520
10521 /* Used by cp_parser_range_for in template context: we aren't going to
10522    do a full conversion yet, but we still need to resolve auto in the
10523    type of the for-range-declaration if present.  This is basically
10524    a shortcut version of cp_convert_range_for.  */
10525
10526 static void
10527 do_range_for_auto_deduction (tree decl, tree range_expr)
10528 {
10529   tree auto_node = type_uses_auto (TREE_TYPE (decl));
10530   if (auto_node)
10531     {
10532       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10533       range_temp = convert_from_reference (build_range_temp (range_expr));
10534       iter_type = (cp_parser_perform_range_for_lookup
10535                    (range_temp, &begin_dummy, &end_dummy));
10536       if (iter_type)
10537         {
10538           iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10539                                   iter_type);
10540           iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10541                                             tf_warning_or_error);
10542           TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10543                                                 iter_decl, auto_node);
10544         }
10545     }
10546 }
10547
10548 /* Converts a range-based for-statement into a normal
10549    for-statement, as per the definition.
10550
10551       for (RANGE_DECL : RANGE_EXPR)
10552         BLOCK
10553
10554    should be equivalent to:
10555
10556       {
10557         auto &&__range = RANGE_EXPR;
10558         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10559               __begin != __end;
10560               ++__begin)
10561           {
10562               RANGE_DECL = *__begin;
10563               BLOCK
10564           }
10565       }
10566
10567    If RANGE_EXPR is an array:
10568         BEGIN_EXPR = __range
10569         END_EXPR = __range + ARRAY_SIZE(__range)
10570    Else if RANGE_EXPR has a member 'begin' or 'end':
10571         BEGIN_EXPR = __range.begin()
10572         END_EXPR = __range.end()
10573    Else:
10574         BEGIN_EXPR = begin(__range)
10575         END_EXPR = end(__range);
10576
10577    If __range has a member 'begin' but not 'end', or vice versa, we must
10578    still use the second alternative (it will surely fail, however).
10579    When calling begin()/end() in the third alternative we must use
10580    argument dependent lookup, but always considering 'std' as an associated
10581    namespace.  */
10582
10583 tree
10584 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10585                       bool ivdep)
10586 {
10587   tree begin, end;
10588   tree iter_type, begin_expr, end_expr;
10589   tree condition, expression;
10590
10591   if (range_decl == error_mark_node || range_expr == error_mark_node)
10592     /* If an error happened previously do nothing or else a lot of
10593        unhelpful errors would be issued.  */
10594     begin_expr = end_expr = iter_type = error_mark_node;
10595   else
10596     {
10597       tree range_temp;
10598
10599       if (TREE_CODE (range_expr) == VAR_DECL
10600           && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10601         /* Can't bind a reference to an array of runtime bound.  */
10602         range_temp = range_expr;
10603       else
10604         {
10605           range_temp = build_range_temp (range_expr);
10606           pushdecl (range_temp);
10607           cp_finish_decl (range_temp, range_expr,
10608                           /*is_constant_init*/false, NULL_TREE,
10609                           LOOKUP_ONLYCONVERTING);
10610           range_temp = convert_from_reference (range_temp);
10611         }
10612       iter_type = cp_parser_perform_range_for_lookup (range_temp,
10613                                                       &begin_expr, &end_expr);
10614     }
10615
10616   /* The new for initialization statement.  */
10617   begin = build_decl (input_location, VAR_DECL,
10618                       get_identifier ("__for_begin"), iter_type);
10619   TREE_USED (begin) = 1;
10620   DECL_ARTIFICIAL (begin) = 1;
10621   pushdecl (begin);
10622   cp_finish_decl (begin, begin_expr,
10623                   /*is_constant_init*/false, NULL_TREE,
10624                   LOOKUP_ONLYCONVERTING);
10625
10626   end = build_decl (input_location, VAR_DECL,
10627                     get_identifier ("__for_end"), iter_type);
10628   TREE_USED (end) = 1;
10629   DECL_ARTIFICIAL (end) = 1;
10630   pushdecl (end);
10631   cp_finish_decl (end, end_expr,
10632                   /*is_constant_init*/false, NULL_TREE,
10633                   LOOKUP_ONLYCONVERTING);
10634
10635   finish_for_init_stmt (statement);
10636
10637   /* The new for condition.  */
10638   condition = build_x_binary_op (input_location, NE_EXPR,
10639                                  begin, ERROR_MARK,
10640                                  end, ERROR_MARK,
10641                                  NULL, tf_warning_or_error);
10642   finish_for_cond (condition, statement, ivdep);
10643
10644   /* The new increment expression.  */
10645   expression = finish_unary_op_expr (input_location,
10646                                      PREINCREMENT_EXPR, begin,
10647                                      tf_warning_or_error);
10648   finish_for_expr (expression, statement);
10649
10650   /* The declaration is initialized with *__begin inside the loop body.  */
10651   cp_finish_decl (range_decl,
10652                   build_x_indirect_ref (input_location, begin, RO_NULL,
10653                                         tf_warning_or_error),
10654                   /*is_constant_init*/false, NULL_TREE,
10655                   LOOKUP_ONLYCONVERTING);
10656
10657   return statement;
10658 }
10659
10660 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10661    We need to solve both at the same time because the method used
10662    depends on the existence of members begin or end.
10663    Returns the type deduced for the iterator expression.  */
10664
10665 static tree
10666 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10667 {
10668   if (error_operand_p (range))
10669     {
10670       *begin = *end = error_mark_node;
10671       return error_mark_node;
10672     }
10673
10674   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10675     {
10676       error ("range-based %<for%> expression of type %qT "
10677              "has incomplete type", TREE_TYPE (range));
10678       *begin = *end = error_mark_node;
10679       return error_mark_node;
10680     }
10681   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10682     {
10683       /* If RANGE is an array, we will use pointer arithmetic.  */
10684       *begin = range;
10685       *end = build_binary_op (input_location, PLUS_EXPR,
10686                               range,
10687                               array_type_nelts_top (TREE_TYPE (range)),
10688                               0);
10689       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10690     }
10691   else
10692     {
10693       /* If it is not an array, we must do a bit of magic.  */
10694       tree id_begin, id_end;
10695       tree member_begin, member_end;
10696
10697       *begin = *end = error_mark_node;
10698
10699       id_begin = get_identifier ("begin");
10700       id_end = get_identifier ("end");
10701       member_begin = lookup_member (TREE_TYPE (range), id_begin,
10702                                     /*protect=*/2, /*want_type=*/false,
10703                                     tf_warning_or_error);
10704       member_end = lookup_member (TREE_TYPE (range), id_end,
10705                                   /*protect=*/2, /*want_type=*/false,
10706                                   tf_warning_or_error);
10707
10708       if (member_begin != NULL_TREE || member_end != NULL_TREE)
10709         {
10710           /* Use the member functions.  */
10711           if (member_begin != NULL_TREE)
10712             *begin = cp_parser_range_for_member_function (range, id_begin);
10713           else
10714             error ("range-based %<for%> expression of type %qT has an "
10715                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10716
10717           if (member_end != NULL_TREE)
10718             *end = cp_parser_range_for_member_function (range, id_end);
10719           else
10720             error ("range-based %<for%> expression of type %qT has a "
10721                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10722         }
10723       else
10724         {
10725           /* Use global functions with ADL.  */
10726           vec<tree, va_gc> *vec;
10727           vec = make_tree_vector ();
10728
10729           vec_safe_push (vec, range);
10730
10731           member_begin = perform_koenig_lookup (id_begin, vec,
10732                                                 tf_warning_or_error);
10733           *begin = finish_call_expr (member_begin, &vec, false, true,
10734                                      tf_warning_or_error);
10735           member_end = perform_koenig_lookup (id_end, vec,
10736                                               tf_warning_or_error);
10737           *end = finish_call_expr (member_end, &vec, false, true,
10738                                    tf_warning_or_error);
10739
10740           release_tree_vector (vec);
10741         }
10742
10743       /* Last common checks.  */
10744       if (*begin == error_mark_node || *end == error_mark_node)
10745         {
10746           /* If one of the expressions is an error do no more checks.  */
10747           *begin = *end = error_mark_node;
10748           return error_mark_node;
10749         }
10750       else if (type_dependent_expression_p (*begin)
10751                || type_dependent_expression_p (*end))
10752         /* Can happen, when, eg, in a template context, Koenig lookup
10753            can't resolve begin/end (c++/58503).  */
10754         return NULL_TREE;
10755       else
10756         {
10757           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10758           /* The unqualified type of the __begin and __end temporaries should
10759              be the same, as required by the multiple auto declaration.  */
10760           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10761             error ("inconsistent begin/end types in range-based %<for%> "
10762                    "statement: %qT and %qT",
10763                    TREE_TYPE (*begin), TREE_TYPE (*end));
10764           return iter_type;
10765         }
10766     }
10767 }
10768
10769 /* Helper function for cp_parser_perform_range_for_lookup.
10770    Builds a tree for RANGE.IDENTIFIER().  */
10771
10772 static tree
10773 cp_parser_range_for_member_function (tree range, tree identifier)
10774 {
10775   tree member, res;
10776   vec<tree, va_gc> *vec;
10777
10778   member = finish_class_member_access_expr (range, identifier,
10779                                             false, tf_warning_or_error);
10780   if (member == error_mark_node)
10781     return error_mark_node;
10782
10783   vec = make_tree_vector ();
10784   res = finish_call_expr (member, &vec,
10785                           /*disallow_virtual=*/false,
10786                           /*koenig_p=*/false,
10787                           tf_warning_or_error);
10788   release_tree_vector (vec);
10789   return res;
10790 }
10791
10792 /* Parse an iteration-statement.
10793
10794    iteration-statement:
10795      while ( condition ) statement
10796      do statement while ( expression ) ;
10797      for ( for-init-statement condition [opt] ; expression [opt] )
10798        statement
10799
10800    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
10801
10802 static tree
10803 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10804 {
10805   cp_token *token;
10806   enum rid keyword;
10807   tree statement;
10808   unsigned char in_statement;
10809
10810   /* Peek at the next token.  */
10811   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10812   if (!token)
10813     return error_mark_node;
10814
10815   /* Remember whether or not we are already within an iteration
10816      statement.  */
10817   in_statement = parser->in_statement;
10818
10819   /* See what kind of keyword it is.  */
10820   keyword = token->keyword;
10821   switch (keyword)
10822     {
10823     case RID_WHILE:
10824       {
10825         tree condition;
10826
10827         /* Begin the while-statement.  */
10828         statement = begin_while_stmt ();
10829         /* Look for the `('.  */
10830         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10831         /* Parse the condition.  */
10832         condition = cp_parser_condition (parser);
10833         finish_while_stmt_cond (condition, statement, ivdep);
10834         /* Look for the `)'.  */
10835         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10836         /* Parse the dependent statement.  */
10837         parser->in_statement = IN_ITERATION_STMT;
10838         cp_parser_already_scoped_statement (parser);
10839         parser->in_statement = in_statement;
10840         /* We're done with the while-statement.  */
10841         finish_while_stmt (statement);
10842       }
10843       break;
10844
10845     case RID_DO:
10846       {
10847         tree expression;
10848
10849         /* Begin the do-statement.  */
10850         statement = begin_do_stmt ();
10851         /* Parse the body of the do-statement.  */
10852         parser->in_statement = IN_ITERATION_STMT;
10853         cp_parser_implicitly_scoped_statement (parser, NULL);
10854         parser->in_statement = in_statement;
10855         finish_do_body (statement);
10856         /* Look for the `while' keyword.  */
10857         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10858         /* Look for the `('.  */
10859         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10860         /* Parse the expression.  */
10861         expression = cp_parser_expression (parser);
10862         /* We're done with the do-statement.  */
10863         finish_do_stmt (expression, statement, ivdep);
10864         /* Look for the `)'.  */
10865         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10866         /* Look for the `;'.  */
10867         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10868       }
10869       break;
10870
10871     case RID_FOR:
10872       {
10873         /* Look for the `('.  */
10874         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10875
10876         statement = cp_parser_for (parser, ivdep);
10877
10878         /* Look for the `)'.  */
10879         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10880
10881         /* Parse the body of the for-statement.  */
10882         parser->in_statement = IN_ITERATION_STMT;
10883         cp_parser_already_scoped_statement (parser);
10884         parser->in_statement = in_statement;
10885
10886         /* We're done with the for-statement.  */
10887         finish_for_stmt (statement);
10888       }
10889       break;
10890
10891     default:
10892       cp_parser_error (parser, "expected iteration-statement");
10893       statement = error_mark_node;
10894       break;
10895     }
10896
10897   return statement;
10898 }
10899
10900 /* Parse a for-init-statement or the declarator of a range-based-for.
10901    Returns true if a range-based-for declaration is seen.
10902
10903    for-init-statement:
10904      expression-statement
10905      simple-declaration  */
10906
10907 static bool
10908 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10909 {
10910   /* If the next token is a `;', then we have an empty
10911      expression-statement.  Grammatically, this is also a
10912      simple-declaration, but an invalid one, because it does not
10913      declare anything.  Therefore, if we did not handle this case
10914      specially, we would issue an error message about an invalid
10915      declaration.  */
10916   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10917     {
10918       bool is_range_for = false;
10919       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10920
10921       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10922           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10923         {
10924           /* N3994 -- for (id : init) ... */
10925           if (cxx_dialect < cxx1z)
10926             pedwarn (input_location, 0, "range-based for loop without a "
10927                      "type-specifier only available with "
10928                      "-std=c++1z or -std=gnu++1z");
10929           tree name = cp_parser_identifier (parser);
10930           tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10931           *decl = build_decl (input_location, VAR_DECL, name, type);
10932           pushdecl (*decl);
10933           cp_lexer_consume_token (parser->lexer);
10934           return true;
10935         }
10936
10937       /* A colon is used in range-based for.  */
10938       parser->colon_corrects_to_scope_p = false;
10939
10940       /* We're going to speculatively look for a declaration, falling back
10941          to an expression, if necessary.  */
10942       cp_parser_parse_tentatively (parser);
10943       /* Parse the declaration.  */
10944       cp_parser_simple_declaration (parser,
10945                                     /*function_definition_allowed_p=*/false,
10946                                     decl);
10947       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10948       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10949         {
10950           /* It is a range-for, consume the ':' */
10951           cp_lexer_consume_token (parser->lexer);
10952           is_range_for = true;
10953           if (cxx_dialect < cxx11)
10954             {
10955               pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10956                        "range-based %<for%> loops only available with "
10957                        "-std=c++11 or -std=gnu++11");
10958               *decl = error_mark_node;
10959             }
10960         }
10961       else
10962           /* The ';' is not consumed yet because we told
10963              cp_parser_simple_declaration not to.  */
10964           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10965
10966       if (cp_parser_parse_definitely (parser))
10967         return is_range_for;
10968       /* If the tentative parse failed, then we shall need to look for an
10969          expression-statement.  */
10970     }
10971   /* If we are here, it is an expression-statement.  */
10972   cp_parser_expression_statement (parser, NULL_TREE);
10973   return false;
10974 }
10975
10976 /* Parse a jump-statement.
10977
10978    jump-statement:
10979      break ;
10980      continue ;
10981      return expression [opt] ;
10982      return braced-init-list ;
10983      goto identifier ;
10984
10985    GNU extension:
10986
10987    jump-statement:
10988      goto * expression ;
10989
10990    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
10991
10992 static tree
10993 cp_parser_jump_statement (cp_parser* parser)
10994 {
10995   tree statement = error_mark_node;
10996   cp_token *token;
10997   enum rid keyword;
10998   unsigned char in_statement;
10999
11000   /* Peek at the next token.  */
11001   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11002   if (!token)
11003     return error_mark_node;
11004
11005   /* See what kind of keyword it is.  */
11006   keyword = token->keyword;
11007   switch (keyword)
11008     {
11009     case RID_BREAK:
11010       in_statement = parser->in_statement & ~IN_IF_STMT;      
11011       switch (in_statement)
11012         {
11013         case 0:
11014           error_at (token->location, "break statement not within loop or switch");
11015           break;
11016         default:
11017           gcc_assert ((in_statement & IN_SWITCH_STMT)
11018                       || in_statement == IN_ITERATION_STMT);
11019           statement = finish_break_stmt ();
11020           if (in_statement == IN_ITERATION_STMT)
11021             break_maybe_infinite_loop ();
11022           break;
11023         case IN_OMP_BLOCK:
11024           error_at (token->location, "invalid exit from OpenMP structured block");
11025           break;
11026         case IN_OMP_FOR:
11027           error_at (token->location, "break statement used with OpenMP for loop");
11028           break;
11029         case IN_CILK_SIMD_FOR:
11030           error_at (token->location, "break statement used with Cilk Plus for loop");
11031           break;
11032         }
11033       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11034       break;
11035
11036     case RID_CONTINUE:
11037       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11038         {
11039         case 0:
11040           error_at (token->location, "continue statement not within a loop");
11041           break;
11042         case IN_CILK_SIMD_FOR:
11043           error_at (token->location,
11044                     "continue statement within %<#pragma simd%> loop body");
11045           /* Fall through.  */
11046         case IN_ITERATION_STMT:
11047         case IN_OMP_FOR:
11048           statement = finish_continue_stmt ();
11049           break;
11050         case IN_OMP_BLOCK:
11051           error_at (token->location, "invalid exit from OpenMP structured block");
11052           break;
11053         default:
11054           gcc_unreachable ();
11055         }
11056       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11057       break;
11058
11059     case RID_RETURN:
11060       {
11061         tree expr;
11062         bool expr_non_constant_p;
11063
11064         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11065           {
11066             cp_lexer_set_source_position (parser->lexer);
11067             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11068             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11069           }
11070         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11071           expr = cp_parser_expression (parser);
11072         else
11073           /* If the next token is a `;', then there is no
11074              expression.  */
11075           expr = NULL_TREE;
11076         /* Build the return-statement.  */
11077         statement = finish_return_stmt (expr);
11078         /* Look for the final `;'.  */
11079         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11080       }
11081       break;
11082
11083     case RID_GOTO:
11084       if (parser->in_function_body
11085           && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11086         {
11087           error ("%<goto%> in %<constexpr%> function");
11088           cp_function_chain->invalid_constexpr = true;
11089         }
11090
11091       /* Create the goto-statement.  */
11092       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11093         {
11094           /* Issue a warning about this use of a GNU extension.  */
11095           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11096           /* Consume the '*' token.  */
11097           cp_lexer_consume_token (parser->lexer);
11098           /* Parse the dependent expression.  */
11099           finish_goto_stmt (cp_parser_expression (parser));
11100         }
11101       else
11102         finish_goto_stmt (cp_parser_identifier (parser));
11103       /* Look for the final `;'.  */
11104       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11105       break;
11106
11107     default:
11108       cp_parser_error (parser, "expected jump-statement");
11109       break;
11110     }
11111
11112   return statement;
11113 }
11114
11115 /* Parse a declaration-statement.
11116
11117    declaration-statement:
11118      block-declaration  */
11119
11120 static void
11121 cp_parser_declaration_statement (cp_parser* parser)
11122 {
11123   void *p;
11124
11125   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11126   p = obstack_alloc (&declarator_obstack, 0);
11127
11128  /* Parse the block-declaration.  */
11129   cp_parser_block_declaration (parser, /*statement_p=*/true);
11130
11131   /* Free any declarators allocated.  */
11132   obstack_free (&declarator_obstack, p);
11133 }
11134
11135 /* Some dependent statements (like `if (cond) statement'), are
11136    implicitly in their own scope.  In other words, if the statement is
11137    a single statement (as opposed to a compound-statement), it is
11138    none-the-less treated as if it were enclosed in braces.  Any
11139    declarations appearing in the dependent statement are out of scope
11140    after control passes that point.  This function parses a statement,
11141    but ensures that is in its own scope, even if it is not a
11142    compound-statement.
11143
11144    If IF_P is not NULL, *IF_P is set to indicate whether the statement
11145    is a (possibly labeled) if statement which is not enclosed in
11146    braces and has an else clause.  This is used to implement
11147    -Wparentheses.
11148
11149    Returns the new statement.  */
11150
11151 static tree
11152 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
11153 {
11154   tree statement;
11155
11156   if (if_p != NULL)
11157     *if_p = false;
11158
11159   /* Mark if () ; with a special NOP_EXPR.  */
11160   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11161     {
11162       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11163       cp_lexer_consume_token (parser->lexer);
11164       statement = add_stmt (build_empty_stmt (loc));
11165     }
11166   /* if a compound is opened, we simply parse the statement directly.  */
11167   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11168     statement = cp_parser_compound_statement (parser, NULL, false, false);
11169   /* If the token is not a `{', then we must take special action.  */
11170   else
11171     {
11172       /* Create a compound-statement.  */
11173       statement = begin_compound_stmt (0);
11174       /* Parse the dependent-statement.  */
11175       cp_parser_statement (parser, NULL_TREE, false, if_p);
11176       /* Finish the dummy compound-statement.  */
11177       finish_compound_stmt (statement);
11178     }
11179
11180   /* Return the statement.  */
11181   return statement;
11182 }
11183
11184 /* For some dependent statements (like `while (cond) statement'), we
11185    have already created a scope.  Therefore, even if the dependent
11186    statement is a compound-statement, we do not want to create another
11187    scope.  */
11188
11189 static void
11190 cp_parser_already_scoped_statement (cp_parser* parser)
11191 {
11192   /* If the token is a `{', then we must take special action.  */
11193   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11194     cp_parser_statement (parser, NULL_TREE, false, NULL);
11195   else
11196     {
11197       /* Avoid calling cp_parser_compound_statement, so that we
11198          don't create a new scope.  Do everything else by hand.  */
11199       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11200       /* If the next keyword is `__label__' we have a label declaration.  */
11201       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11202         cp_parser_label_declaration (parser);
11203       /* Parse an (optional) statement-seq.  */
11204       cp_parser_statement_seq_opt (parser, NULL_TREE);
11205       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11206     }
11207 }
11208
11209 /* Declarations [gram.dcl.dcl] */
11210
11211 /* Parse an optional declaration-sequence.
11212
11213    declaration-seq:
11214      declaration
11215      declaration-seq declaration  */
11216
11217 static void
11218 cp_parser_declaration_seq_opt (cp_parser* parser)
11219 {
11220   while (true)
11221     {
11222       cp_token *token;
11223
11224       token = cp_lexer_peek_token (parser->lexer);
11225
11226       if (token->type == CPP_CLOSE_BRACE
11227           || token->type == CPP_EOF
11228           || token->type == CPP_PRAGMA_EOL)
11229         break;
11230
11231       if (token->type == CPP_SEMICOLON)
11232         {
11233           /* A declaration consisting of a single semicolon is
11234              invalid.  Allow it unless we're being pedantic.  */
11235           cp_lexer_consume_token (parser->lexer);
11236           if (!in_system_header_at (input_location))
11237             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11238           continue;
11239         }
11240
11241       /* If we're entering or exiting a region that's implicitly
11242          extern "C", modify the lang context appropriately.  */
11243       if (!parser->implicit_extern_c && token->implicit_extern_c)
11244         {
11245           push_lang_context (lang_name_c);
11246           parser->implicit_extern_c = true;
11247         }
11248       else if (parser->implicit_extern_c && !token->implicit_extern_c)
11249         {
11250           pop_lang_context ();
11251           parser->implicit_extern_c = false;
11252         }
11253
11254       if (token->type == CPP_PRAGMA)
11255         {
11256           /* A top-level declaration can consist solely of a #pragma.
11257              A nested declaration cannot, so this is done here and not
11258              in cp_parser_declaration.  (A #pragma at block scope is
11259              handled in cp_parser_statement.)  */
11260           cp_parser_pragma (parser, pragma_external);
11261           continue;
11262         }
11263
11264       /* Parse the declaration itself.  */
11265       cp_parser_declaration (parser);
11266     }
11267 }
11268
11269 /* Parse a declaration.
11270
11271    declaration:
11272      block-declaration
11273      function-definition
11274      template-declaration
11275      explicit-instantiation
11276      explicit-specialization
11277      linkage-specification
11278      namespace-definition
11279
11280    GNU extension:
11281
11282    declaration:
11283       __extension__ declaration */
11284
11285 static void
11286 cp_parser_declaration (cp_parser* parser)
11287 {
11288   cp_token token1;
11289   cp_token token2;
11290   int saved_pedantic;
11291   void *p;
11292   tree attributes = NULL_TREE;
11293
11294   /* Check for the `__extension__' keyword.  */
11295   if (cp_parser_extension_opt (parser, &saved_pedantic))
11296     {
11297       /* Parse the qualified declaration.  */
11298       cp_parser_declaration (parser);
11299       /* Restore the PEDANTIC flag.  */
11300       pedantic = saved_pedantic;
11301
11302       return;
11303     }
11304
11305   /* Try to figure out what kind of declaration is present.  */
11306   token1 = *cp_lexer_peek_token (parser->lexer);
11307
11308   if (token1.type != CPP_EOF)
11309     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11310   else
11311     {
11312       token2.type = CPP_EOF;
11313       token2.keyword = RID_MAX;
11314     }
11315
11316   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
11317   p = obstack_alloc (&declarator_obstack, 0);
11318
11319   /* If the next token is `extern' and the following token is a string
11320      literal, then we have a linkage specification.  */
11321   if (token1.keyword == RID_EXTERN
11322       && cp_parser_is_pure_string_literal (&token2))
11323     cp_parser_linkage_specification (parser);
11324   /* If the next token is `template', then we have either a template
11325      declaration, an explicit instantiation, or an explicit
11326      specialization.  */
11327   else if (token1.keyword == RID_TEMPLATE)
11328     {
11329       /* `template <>' indicates a template specialization.  */
11330       if (token2.type == CPP_LESS
11331           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11332         cp_parser_explicit_specialization (parser);
11333       /* `template <' indicates a template declaration.  */
11334       else if (token2.type == CPP_LESS)
11335         cp_parser_template_declaration (parser, /*member_p=*/false);
11336       /* Anything else must be an explicit instantiation.  */
11337       else
11338         cp_parser_explicit_instantiation (parser);
11339     }
11340   /* If the next token is `export', then we have a template
11341      declaration.  */
11342   else if (token1.keyword == RID_EXPORT)
11343     cp_parser_template_declaration (parser, /*member_p=*/false);
11344   /* If the next token is `extern', 'static' or 'inline' and the one
11345      after that is `template', we have a GNU extended explicit
11346      instantiation directive.  */
11347   else if (cp_parser_allow_gnu_extensions_p (parser)
11348            && (token1.keyword == RID_EXTERN
11349                || token1.keyword == RID_STATIC
11350                || token1.keyword == RID_INLINE)
11351            && token2.keyword == RID_TEMPLATE)
11352     cp_parser_explicit_instantiation (parser);
11353   /* If the next token is `namespace', check for a named or unnamed
11354      namespace definition.  */
11355   else if (token1.keyword == RID_NAMESPACE
11356            && (/* A named namespace definition.  */
11357                (token2.type == CPP_NAME
11358                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11359                     != CPP_EQ))
11360                /* An unnamed namespace definition.  */
11361                || token2.type == CPP_OPEN_BRACE
11362                || token2.keyword == RID_ATTRIBUTE))
11363     cp_parser_namespace_definition (parser);
11364   /* An inline (associated) namespace definition.  */
11365   else if (token1.keyword == RID_INLINE
11366            && token2.keyword == RID_NAMESPACE)
11367     cp_parser_namespace_definition (parser);
11368   /* Objective-C++ declaration/definition.  */
11369   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11370     cp_parser_objc_declaration (parser, NULL_TREE);
11371   else if (c_dialect_objc ()
11372            && token1.keyword == RID_ATTRIBUTE
11373            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11374     cp_parser_objc_declaration (parser, attributes);
11375   /* We must have either a block declaration or a function
11376      definition.  */
11377   else
11378     /* Try to parse a block-declaration, or a function-definition.  */
11379     cp_parser_block_declaration (parser, /*statement_p=*/false);
11380
11381   /* Free any declarators allocated.  */
11382   obstack_free (&declarator_obstack, p);
11383 }
11384
11385 /* Parse a block-declaration.
11386
11387    block-declaration:
11388      simple-declaration
11389      asm-definition
11390      namespace-alias-definition
11391      using-declaration
11392      using-directive
11393
11394    GNU Extension:
11395
11396    block-declaration:
11397      __extension__ block-declaration
11398
11399    C++0x Extension:
11400
11401    block-declaration:
11402      static_assert-declaration
11403
11404    If STATEMENT_P is TRUE, then this block-declaration is occurring as
11405    part of a declaration-statement.  */
11406
11407 static void
11408 cp_parser_block_declaration (cp_parser *parser,
11409                              bool      statement_p)
11410 {
11411   cp_token *token1;
11412   int saved_pedantic;
11413
11414   /* Check for the `__extension__' keyword.  */
11415   if (cp_parser_extension_opt (parser, &saved_pedantic))
11416     {
11417       /* Parse the qualified declaration.  */
11418       cp_parser_block_declaration (parser, statement_p);
11419       /* Restore the PEDANTIC flag.  */
11420       pedantic = saved_pedantic;
11421
11422       return;
11423     }
11424
11425   /* Peek at the next token to figure out which kind of declaration is
11426      present.  */
11427   token1 = cp_lexer_peek_token (parser->lexer);
11428
11429   /* If the next keyword is `asm', we have an asm-definition.  */
11430   if (token1->keyword == RID_ASM)
11431     {
11432       if (statement_p)
11433         cp_parser_commit_to_tentative_parse (parser);
11434       cp_parser_asm_definition (parser);
11435     }
11436   /* If the next keyword is `namespace', we have a
11437      namespace-alias-definition.  */
11438   else if (token1->keyword == RID_NAMESPACE)
11439     cp_parser_namespace_alias_definition (parser);
11440   /* If the next keyword is `using', we have a
11441      using-declaration, a using-directive, or an alias-declaration.  */
11442   else if (token1->keyword == RID_USING)
11443     {
11444       cp_token *token2;
11445
11446       if (statement_p)
11447         cp_parser_commit_to_tentative_parse (parser);
11448       /* If the token after `using' is `namespace', then we have a
11449          using-directive.  */
11450       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11451       if (token2->keyword == RID_NAMESPACE)
11452         cp_parser_using_directive (parser);
11453       /* If the second token after 'using' is '=', then we have an
11454          alias-declaration.  */
11455       else if (cxx_dialect >= cxx11
11456                && token2->type == CPP_NAME
11457                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11458                    || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11459         cp_parser_alias_declaration (parser);
11460       /* Otherwise, it's a using-declaration.  */
11461       else
11462         cp_parser_using_declaration (parser,
11463                                      /*access_declaration_p=*/false);
11464     }
11465   /* If the next keyword is `__label__' we have a misplaced label
11466      declaration.  */
11467   else if (token1->keyword == RID_LABEL)
11468     {
11469       cp_lexer_consume_token (parser->lexer);
11470       error_at (token1->location, "%<__label__%> not at the beginning of a block");
11471       cp_parser_skip_to_end_of_statement (parser);
11472       /* If the next token is now a `;', consume it.  */
11473       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11474         cp_lexer_consume_token (parser->lexer);
11475     }
11476   /* If the next token is `static_assert' we have a static assertion.  */
11477   else if (token1->keyword == RID_STATIC_ASSERT)
11478     cp_parser_static_assert (parser, /*member_p=*/false);
11479   /* Anything else must be a simple-declaration.  */
11480   else
11481     cp_parser_simple_declaration (parser, !statement_p,
11482                                   /*maybe_range_for_decl*/NULL);
11483 }
11484
11485 /* Parse a simple-declaration.
11486
11487    simple-declaration:
11488      decl-specifier-seq [opt] init-declarator-list [opt] ;
11489
11490    init-declarator-list:
11491      init-declarator
11492      init-declarator-list , init-declarator
11493
11494    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11495    function-definition as a simple-declaration.
11496
11497    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11498    parsed declaration if it is an uninitialized single declarator not followed
11499    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11500    if present, will not be consumed.  */
11501
11502 static void
11503 cp_parser_simple_declaration (cp_parser* parser,
11504                               bool function_definition_allowed_p,
11505                               tree *maybe_range_for_decl)
11506 {
11507   cp_decl_specifier_seq decl_specifiers;
11508   int declares_class_or_enum;
11509   bool saw_declarator;
11510   location_t comma_loc = UNKNOWN_LOCATION;
11511   location_t init_loc = UNKNOWN_LOCATION;
11512
11513   if (maybe_range_for_decl)
11514     *maybe_range_for_decl = NULL_TREE;
11515
11516   /* Defer access checks until we know what is being declared; the
11517      checks for names appearing in the decl-specifier-seq should be
11518      done as if we were in the scope of the thing being declared.  */
11519   push_deferring_access_checks (dk_deferred);
11520
11521   /* Parse the decl-specifier-seq.  We have to keep track of whether
11522      or not the decl-specifier-seq declares a named class or
11523      enumeration type, since that is the only case in which the
11524      init-declarator-list is allowed to be empty.
11525
11526      [dcl.dcl]
11527
11528      In a simple-declaration, the optional init-declarator-list can be
11529      omitted only when declaring a class or enumeration, that is when
11530      the decl-specifier-seq contains either a class-specifier, an
11531      elaborated-type-specifier, or an enum-specifier.  */
11532   cp_parser_decl_specifier_seq (parser,
11533                                 CP_PARSER_FLAGS_OPTIONAL,
11534                                 &decl_specifiers,
11535                                 &declares_class_or_enum);
11536   /* We no longer need to defer access checks.  */
11537   stop_deferring_access_checks ();
11538
11539   /* In a block scope, a valid declaration must always have a
11540      decl-specifier-seq.  By not trying to parse declarators, we can
11541      resolve the declaration/expression ambiguity more quickly.  */
11542   if (!function_definition_allowed_p
11543       && !decl_specifiers.any_specifiers_p)
11544     {
11545       cp_parser_error (parser, "expected declaration");
11546       goto done;
11547     }
11548
11549   /* If the next two tokens are both identifiers, the code is
11550      erroneous. The usual cause of this situation is code like:
11551
11552        T t;
11553
11554      where "T" should name a type -- but does not.  */
11555   if (!decl_specifiers.any_type_specifiers_p
11556       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11557     {
11558       /* If parsing tentatively, we should commit; we really are
11559          looking at a declaration.  */
11560       cp_parser_commit_to_tentative_parse (parser);
11561       /* Give up.  */
11562       goto done;
11563     }
11564
11565   /* If we have seen at least one decl-specifier, and the next token
11566      is not a parenthesis, then we must be looking at a declaration.
11567      (After "int (" we might be looking at a functional cast.)  */
11568   if (decl_specifiers.any_specifiers_p
11569       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11570       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11571       && !cp_parser_error_occurred (parser))
11572     cp_parser_commit_to_tentative_parse (parser);
11573
11574   /* Keep going until we hit the `;' at the end of the simple
11575      declaration.  */
11576   saw_declarator = false;
11577   while (cp_lexer_next_token_is_not (parser->lexer,
11578                                      CPP_SEMICOLON))
11579     {
11580       cp_token *token;
11581       bool function_definition_p;
11582       tree decl;
11583
11584       if (saw_declarator)
11585         {
11586           /* If we are processing next declarator, comma is expected */
11587           token = cp_lexer_peek_token (parser->lexer);
11588           gcc_assert (token->type == CPP_COMMA);
11589           cp_lexer_consume_token (parser->lexer);
11590           if (maybe_range_for_decl)
11591             {
11592               *maybe_range_for_decl = error_mark_node;
11593               if (comma_loc == UNKNOWN_LOCATION)
11594                 comma_loc = token->location;
11595             }
11596         }
11597       else
11598         saw_declarator = true;
11599
11600       /* Parse the init-declarator.  */
11601       decl = cp_parser_init_declarator (parser, &decl_specifiers,
11602                                         /*checks=*/NULL,
11603                                         function_definition_allowed_p,
11604                                         /*member_p=*/false,
11605                                         declares_class_or_enum,
11606                                         &function_definition_p,
11607                                         maybe_range_for_decl,
11608                                         &init_loc);
11609       /* If an error occurred while parsing tentatively, exit quickly.
11610          (That usually happens when in the body of a function; each
11611          statement is treated as a declaration-statement until proven
11612          otherwise.)  */
11613       if (cp_parser_error_occurred (parser))
11614         goto done;
11615       /* Handle function definitions specially.  */
11616       if (function_definition_p)
11617         {
11618           /* If the next token is a `,', then we are probably
11619              processing something like:
11620
11621                void f() {}, *p;
11622
11623              which is erroneous.  */
11624           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11625             {
11626               cp_token *token = cp_lexer_peek_token (parser->lexer);
11627               error_at (token->location,
11628                         "mixing"
11629                         " declarations and function-definitions is forbidden");
11630             }
11631           /* Otherwise, we're done with the list of declarators.  */
11632           else
11633             {
11634               pop_deferring_access_checks ();
11635               return;
11636             }
11637         }
11638       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11639         *maybe_range_for_decl = decl;
11640       /* The next token should be either a `,' or a `;'.  */
11641       token = cp_lexer_peek_token (parser->lexer);
11642       /* If it's a `,', there are more declarators to come.  */
11643       if (token->type == CPP_COMMA)
11644         /* will be consumed next time around */;
11645       /* If it's a `;', we are done.  */
11646       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11647         break;
11648       /* Anything else is an error.  */
11649       else
11650         {
11651           /* If we have already issued an error message we don't need
11652              to issue another one.  */
11653           if (decl != error_mark_node
11654               || cp_parser_uncommitted_to_tentative_parse_p (parser))
11655             cp_parser_error (parser, "expected %<,%> or %<;%>");
11656           /* Skip tokens until we reach the end of the statement.  */
11657           cp_parser_skip_to_end_of_statement (parser);
11658           /* If the next token is now a `;', consume it.  */
11659           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11660             cp_lexer_consume_token (parser->lexer);
11661           goto done;
11662         }
11663       /* After the first time around, a function-definition is not
11664          allowed -- even if it was OK at first.  For example:
11665
11666            int i, f() {}
11667
11668          is not valid.  */
11669       function_definition_allowed_p = false;
11670     }
11671
11672   /* Issue an error message if no declarators are present, and the
11673      decl-specifier-seq does not itself declare a class or
11674      enumeration: [dcl.dcl]/3.  */
11675   if (!saw_declarator)
11676     {
11677       if (cp_parser_declares_only_class_p (parser))
11678         {
11679           if (!declares_class_or_enum
11680               && decl_specifiers.type
11681               && OVERLOAD_TYPE_P (decl_specifiers.type))
11682             /* Ensure an error is issued anyway when finish_decltype_type,
11683                called via cp_parser_decl_specifier_seq, returns a class or
11684                an enumeration (c++/51786).  */
11685             decl_specifiers.type = NULL_TREE;
11686           shadow_tag (&decl_specifiers);
11687         }
11688       /* Perform any deferred access checks.  */
11689       perform_deferred_access_checks (tf_warning_or_error);
11690     }
11691
11692   /* Consume the `;'.  */
11693   if (!maybe_range_for_decl)
11694     cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11695   else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11696     {
11697       if (init_loc != UNKNOWN_LOCATION)
11698         error_at (init_loc, "initializer in range-based %<for%> loop");
11699       if (comma_loc != UNKNOWN_LOCATION)
11700         error_at (comma_loc,
11701                   "multiple declarations in range-based %<for%> loop");
11702     }
11703
11704  done:
11705   pop_deferring_access_checks ();
11706 }
11707
11708 /* Parse a decl-specifier-seq.
11709
11710    decl-specifier-seq:
11711      decl-specifier-seq [opt] decl-specifier
11712      decl-specifier attribute-specifier-seq [opt] (C++11)
11713
11714    decl-specifier:
11715      storage-class-specifier
11716      type-specifier
11717      function-specifier
11718      friend
11719      typedef
11720
11721    GNU Extension:
11722
11723    decl-specifier:
11724      attributes
11725
11726    Set *DECL_SPECS to a representation of the decl-specifier-seq.
11727
11728    The parser flags FLAGS is used to control type-specifier parsing.
11729
11730    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11731    flags:
11732
11733      1: one of the decl-specifiers is an elaborated-type-specifier
11734         (i.e., a type declaration)
11735      2: one of the decl-specifiers is an enum-specifier or a
11736         class-specifier (i.e., a type definition)
11737
11738    */
11739
11740 static void
11741 cp_parser_decl_specifier_seq (cp_parser* parser,
11742                               cp_parser_flags flags,
11743                               cp_decl_specifier_seq *decl_specs,
11744                               int* declares_class_or_enum)
11745 {
11746   bool constructor_possible_p = !parser->in_declarator_p;
11747   bool found_decl_spec = false;
11748   cp_token *start_token = NULL;
11749   cp_decl_spec ds;
11750
11751   /* Clear DECL_SPECS.  */
11752   clear_decl_specs (decl_specs);
11753
11754   /* Assume no class or enumeration type is declared.  */
11755   *declares_class_or_enum = 0;
11756
11757   /* Keep reading specifiers until there are no more to read.  */
11758   while (true)
11759     {
11760       bool constructor_p;
11761       cp_token *token;
11762       ds = ds_last;
11763
11764       /* Peek at the next token.  */
11765       token = cp_lexer_peek_token (parser->lexer);
11766
11767       /* Save the first token of the decl spec list for error
11768          reporting.  */
11769       if (!start_token)
11770         start_token = token;
11771       /* Handle attributes.  */
11772       if (cp_next_tokens_can_be_attribute_p (parser))
11773         {
11774           /* Parse the attributes.  */
11775           tree attrs = cp_parser_attributes_opt (parser);
11776
11777           /* In a sequence of declaration specifiers, c++11 attributes
11778              appertain to the type that precede them. In that case
11779              [dcl.spec]/1 says:
11780
11781                  The attribute-specifier-seq affects the type only for
11782                  the declaration it appears in, not other declarations
11783                  involving the same type.
11784
11785              But for now let's force the user to position the
11786              attribute either at the beginning of the declaration or
11787              after the declarator-id, which would clearly mean that it
11788              applies to the declarator.  */
11789           if (cxx11_attribute_p (attrs))
11790             {
11791               if (!found_decl_spec)
11792                 /* The c++11 attribute is at the beginning of the
11793                    declaration.  It appertains to the entity being
11794                    declared.  */;
11795               else
11796                 {
11797                   if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11798                     {
11799                       /*  This is an attribute following a
11800                           class-specifier.  */
11801                       if (decl_specs->type_definition_p)
11802                         warn_misplaced_attr_for_class_type (token->location,
11803                                                             decl_specs->type);
11804                       attrs = NULL_TREE;
11805                     }
11806                   else
11807                     {
11808                       decl_specs->std_attributes
11809                         = chainon (decl_specs->std_attributes,
11810                                    attrs);
11811                       if (decl_specs->locations[ds_std_attribute] == 0)
11812                         decl_specs->locations[ds_std_attribute] = token->location;
11813                     }
11814                   continue;
11815                 }
11816             }
11817
11818             decl_specs->attributes
11819               = chainon (decl_specs->attributes,
11820                          attrs);
11821           if (decl_specs->locations[ds_attribute] == 0)
11822             decl_specs->locations[ds_attribute] = token->location;
11823           continue;
11824         }
11825       /* Assume we will find a decl-specifier keyword.  */
11826       found_decl_spec = true;
11827       /* If the next token is an appropriate keyword, we can simply
11828          add it to the list.  */
11829       switch (token->keyword)
11830         {
11831           /* decl-specifier:
11832                friend
11833                constexpr */
11834         case RID_FRIEND:
11835           if (!at_class_scope_p ())
11836             {
11837               error_at (token->location, "%<friend%> used outside of class");
11838               cp_lexer_purge_token (parser->lexer);
11839             }
11840           else
11841             {
11842               ds = ds_friend;
11843               /* Consume the token.  */
11844               cp_lexer_consume_token (parser->lexer);
11845             }
11846           break;
11847
11848         case RID_CONSTEXPR:
11849           ds = ds_constexpr;
11850           cp_lexer_consume_token (parser->lexer);
11851           break;
11852
11853           /* function-specifier:
11854                inline
11855                virtual
11856                explicit  */
11857         case RID_INLINE:
11858         case RID_VIRTUAL:
11859         case RID_EXPLICIT:
11860           cp_parser_function_specifier_opt (parser, decl_specs);
11861           break;
11862
11863           /* decl-specifier:
11864                typedef  */
11865         case RID_TYPEDEF:
11866           ds = ds_typedef;
11867           /* Consume the token.  */
11868           cp_lexer_consume_token (parser->lexer);
11869           /* A constructor declarator cannot appear in a typedef.  */
11870           constructor_possible_p = false;
11871           /* The "typedef" keyword can only occur in a declaration; we
11872              may as well commit at this point.  */
11873           cp_parser_commit_to_tentative_parse (parser);
11874
11875           if (decl_specs->storage_class != sc_none)
11876             decl_specs->conflicting_specifiers_p = true;
11877           break;
11878
11879           /* storage-class-specifier:
11880                auto
11881                register
11882                static
11883                extern
11884                mutable
11885
11886              GNU Extension:
11887                thread  */
11888         case RID_AUTO:
11889           if (cxx_dialect == cxx98) 
11890             {
11891               /* Consume the token.  */
11892               cp_lexer_consume_token (parser->lexer);
11893
11894               /* Complain about `auto' as a storage specifier, if
11895                  we're complaining about C++0x compatibility.  */
11896               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11897                           " changes meaning in C++11; please remove it");
11898
11899               /* Set the storage class anyway.  */
11900               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11901                                            token);
11902             }
11903           else
11904             /* C++0x auto type-specifier.  */
11905             found_decl_spec = false;
11906           break;
11907
11908         case RID_REGISTER:
11909         case RID_STATIC:
11910         case RID_EXTERN:
11911         case RID_MUTABLE:
11912           /* Consume the token.  */
11913           cp_lexer_consume_token (parser->lexer);
11914           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11915                                        token);
11916           break;
11917         case RID_THREAD:
11918           /* Consume the token.  */
11919           ds = ds_thread;
11920           cp_lexer_consume_token (parser->lexer);
11921           break;
11922
11923         default:
11924           /* We did not yet find a decl-specifier yet.  */
11925           found_decl_spec = false;
11926           break;
11927         }
11928
11929       if (found_decl_spec
11930           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11931           && token->keyword != RID_CONSTEXPR)
11932         error ("decl-specifier invalid in condition");
11933
11934       if (ds != ds_last)
11935         set_and_check_decl_spec_loc (decl_specs, ds, token);
11936
11937       /* Constructors are a special case.  The `S' in `S()' is not a
11938          decl-specifier; it is the beginning of the declarator.  */
11939       constructor_p
11940         = (!found_decl_spec
11941            && constructor_possible_p
11942            && (cp_parser_constructor_declarator_p
11943                (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11944
11945       /* If we don't have a DECL_SPEC yet, then we must be looking at
11946          a type-specifier.  */
11947       if (!found_decl_spec && !constructor_p)
11948         {
11949           int decl_spec_declares_class_or_enum;
11950           bool is_cv_qualifier;
11951           tree type_spec;
11952
11953           type_spec
11954             = cp_parser_type_specifier (parser, flags,
11955                                         decl_specs,
11956                                         /*is_declaration=*/true,
11957                                         &decl_spec_declares_class_or_enum,
11958                                         &is_cv_qualifier);
11959           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11960
11961           /* If this type-specifier referenced a user-defined type
11962              (a typedef, class-name, etc.), then we can't allow any
11963              more such type-specifiers henceforth.
11964
11965              [dcl.spec]
11966
11967              The longest sequence of decl-specifiers that could
11968              possibly be a type name is taken as the
11969              decl-specifier-seq of a declaration.  The sequence shall
11970              be self-consistent as described below.
11971
11972              [dcl.type]
11973
11974              As a general rule, at most one type-specifier is allowed
11975              in the complete decl-specifier-seq of a declaration.  The
11976              only exceptions are the following:
11977
11978              -- const or volatile can be combined with any other
11979                 type-specifier.
11980
11981              -- signed or unsigned can be combined with char, long,
11982                 short, or int.
11983
11984              -- ..
11985
11986              Example:
11987
11988                typedef char* Pc;
11989                void g (const int Pc);
11990
11991              Here, Pc is *not* part of the decl-specifier seq; it's
11992              the declarator.  Therefore, once we see a type-specifier
11993              (other than a cv-qualifier), we forbid any additional
11994              user-defined types.  We *do* still allow things like `int
11995              int' to be considered a decl-specifier-seq, and issue the
11996              error message later.  */
11997           if (type_spec && !is_cv_qualifier)
11998             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11999           /* A constructor declarator cannot follow a type-specifier.  */
12000           if (type_spec)
12001             {
12002               constructor_possible_p = false;
12003               found_decl_spec = true;
12004               if (!is_cv_qualifier)
12005                 decl_specs->any_type_specifiers_p = true;
12006             }
12007         }
12008
12009       /* If we still do not have a DECL_SPEC, then there are no more
12010          decl-specifiers.  */
12011       if (!found_decl_spec)
12012         break;
12013
12014       decl_specs->any_specifiers_p = true;
12015       /* After we see one decl-specifier, further decl-specifiers are
12016          always optional.  */
12017       flags |= CP_PARSER_FLAGS_OPTIONAL;
12018     }
12019
12020   /* Don't allow a friend specifier with a class definition.  */
12021   if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12022       && (*declares_class_or_enum & 2))
12023     error_at (decl_specs->locations[ds_friend],
12024               "class definition may not be declared a friend");
12025 }
12026
12027 /* Parse an (optional) storage-class-specifier.
12028
12029    storage-class-specifier:
12030      auto
12031      register
12032      static
12033      extern
12034      mutable
12035
12036    GNU Extension:
12037
12038    storage-class-specifier:
12039      thread
12040
12041    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
12042
12043 static tree
12044 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12045 {
12046   switch (cp_lexer_peek_token (parser->lexer)->keyword)
12047     {
12048     case RID_AUTO:
12049       if (cxx_dialect != cxx98)
12050         return NULL_TREE;
12051       /* Fall through for C++98.  */
12052
12053     case RID_REGISTER:
12054     case RID_STATIC:
12055     case RID_EXTERN:
12056     case RID_MUTABLE:
12057     case RID_THREAD:
12058       /* Consume the token.  */
12059       return cp_lexer_consume_token (parser->lexer)->u.value;
12060
12061     default:
12062       return NULL_TREE;
12063     }
12064 }
12065
12066 /* Parse an (optional) function-specifier.
12067
12068    function-specifier:
12069      inline
12070      virtual
12071      explicit
12072
12073    Returns an IDENTIFIER_NODE corresponding to the keyword used.
12074    Updates DECL_SPECS, if it is non-NULL.  */
12075
12076 static tree
12077 cp_parser_function_specifier_opt (cp_parser* parser,
12078                                   cp_decl_specifier_seq *decl_specs)
12079 {
12080   cp_token *token = cp_lexer_peek_token (parser->lexer);
12081   switch (token->keyword)
12082     {
12083     case RID_INLINE:
12084       set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12085       break;
12086
12087     case RID_VIRTUAL:
12088       /* 14.5.2.3 [temp.mem]
12089
12090          A member function template shall not be virtual.  */
12091       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12092         error_at (token->location, "templates may not be %<virtual%>");
12093       else
12094         set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12095       break;
12096
12097     case RID_EXPLICIT:
12098       set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12099       break;
12100
12101     default:
12102       return NULL_TREE;
12103     }
12104
12105   /* Consume the token.  */
12106   return cp_lexer_consume_token (parser->lexer)->u.value;
12107 }
12108
12109 /* Parse a linkage-specification.
12110
12111    linkage-specification:
12112      extern string-literal { declaration-seq [opt] }
12113      extern string-literal declaration  */
12114
12115 static void
12116 cp_parser_linkage_specification (cp_parser* parser)
12117 {
12118   tree linkage;
12119
12120   /* Look for the `extern' keyword.  */
12121   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12122
12123   /* Look for the string-literal.  */
12124   linkage = cp_parser_string_literal (parser, false, false);
12125
12126   /* Transform the literal into an identifier.  If the literal is a
12127      wide-character string, or contains embedded NULs, then we can't
12128      handle it as the user wants.  */
12129   if (strlen (TREE_STRING_POINTER (linkage))
12130       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12131     {
12132       cp_parser_error (parser, "invalid linkage-specification");
12133       /* Assume C++ linkage.  */
12134       linkage = lang_name_cplusplus;
12135     }
12136   else
12137     linkage = get_identifier (TREE_STRING_POINTER (linkage));
12138
12139   /* We're now using the new linkage.  */
12140   push_lang_context (linkage);
12141
12142   /* If the next token is a `{', then we're using the first
12143      production.  */
12144   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12145     {
12146       cp_ensure_no_omp_declare_simd (parser);
12147
12148       /* Consume the `{' token.  */
12149       cp_lexer_consume_token (parser->lexer);
12150       /* Parse the declarations.  */
12151       cp_parser_declaration_seq_opt (parser);
12152       /* Look for the closing `}'.  */
12153       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12154     }
12155   /* Otherwise, there's just one declaration.  */
12156   else
12157     {
12158       bool saved_in_unbraced_linkage_specification_p;
12159
12160       saved_in_unbraced_linkage_specification_p
12161         = parser->in_unbraced_linkage_specification_p;
12162       parser->in_unbraced_linkage_specification_p = true;
12163       cp_parser_declaration (parser);
12164       parser->in_unbraced_linkage_specification_p
12165         = saved_in_unbraced_linkage_specification_p;
12166     }
12167
12168   /* We're done with the linkage-specification.  */
12169   pop_lang_context ();
12170 }
12171
12172 /* Parse a static_assert-declaration.
12173
12174    static_assert-declaration:
12175      static_assert ( constant-expression , string-literal ) ; 
12176
12177    If MEMBER_P, this static_assert is a class member.  */
12178
12179 static void 
12180 cp_parser_static_assert(cp_parser *parser, bool member_p)
12181 {
12182   tree condition;
12183   tree message;
12184   cp_token *token;
12185   location_t saved_loc;
12186   bool dummy;
12187
12188   /* Peek at the `static_assert' token so we can keep track of exactly
12189      where the static assertion started.  */
12190   token = cp_lexer_peek_token (parser->lexer);
12191   saved_loc = token->location;
12192
12193   /* Look for the `static_assert' keyword.  */
12194   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
12195                                   RT_STATIC_ASSERT))
12196     return;
12197
12198   /*  We know we are in a static assertion; commit to any tentative
12199       parse.  */
12200   if (cp_parser_parsing_tentatively (parser))
12201     cp_parser_commit_to_tentative_parse (parser);
12202
12203   /* Parse the `(' starting the static assertion condition.  */
12204   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12205
12206   /* Parse the constant-expression.  Allow a non-constant expression
12207      here in order to give better diagnostics in finish_static_assert.  */
12208   condition = 
12209     cp_parser_constant_expression (parser,
12210                                    /*allow_non_constant_p=*/true,
12211                                    /*non_constant_p=*/&dummy);
12212
12213   /* Parse the separating `,'.  */
12214   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12215
12216   /* Parse the string-literal message.  */
12217   message = cp_parser_string_literal (parser, 
12218                                       /*translate=*/false,
12219                                       /*wide_ok=*/true);
12220
12221   /* A `)' completes the static assertion.  */
12222   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12223     cp_parser_skip_to_closing_parenthesis (parser, 
12224                                            /*recovering=*/true, 
12225                                            /*or_comma=*/false,
12226                                            /*consume_paren=*/true);
12227
12228   /* A semicolon terminates the declaration.  */
12229   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12230
12231   /* Complete the static assertion, which may mean either processing 
12232      the static assert now or saving it for template instantiation.  */
12233   finish_static_assert (condition, message, saved_loc, member_p);
12234 }
12235
12236 /* Parse the expression in decltype ( expression ).  */
12237
12238 static tree
12239 cp_parser_decltype_expr (cp_parser *parser,
12240                          bool &id_expression_or_member_access_p)
12241 {
12242   cp_token *id_expr_start_token;
12243   tree expr;
12244
12245   /* First, try parsing an id-expression.  */
12246   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12247   cp_parser_parse_tentatively (parser);
12248   expr = cp_parser_id_expression (parser,
12249                                   /*template_keyword_p=*/false,
12250                                   /*check_dependency_p=*/true,
12251                                   /*template_p=*/NULL,
12252                                   /*declarator_p=*/false,
12253                                   /*optional_p=*/false);
12254
12255   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12256     {
12257       bool non_integral_constant_expression_p = false;
12258       tree id_expression = expr;
12259       cp_id_kind idk;
12260       const char *error_msg;
12261
12262       if (identifier_p (expr))
12263         /* Lookup the name we got back from the id-expression.  */
12264         expr = cp_parser_lookup_name_simple (parser, expr,
12265                                              id_expr_start_token->location);
12266
12267       if (expr
12268           && expr != error_mark_node
12269           && TREE_CODE (expr) != TYPE_DECL
12270           && (TREE_CODE (expr) != BIT_NOT_EXPR
12271               || !TYPE_P (TREE_OPERAND (expr, 0)))
12272           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12273         {
12274           /* Complete lookup of the id-expression.  */
12275           expr = (finish_id_expression
12276                   (id_expression, expr, parser->scope, &idk,
12277                    /*integral_constant_expression_p=*/false,
12278                    /*allow_non_integral_constant_expression_p=*/true,
12279                    &non_integral_constant_expression_p,
12280                    /*template_p=*/false,
12281                    /*done=*/true,
12282                    /*address_p=*/false,
12283                    /*template_arg_p=*/false,
12284                    &error_msg,
12285                    id_expr_start_token->location));
12286
12287           if (expr == error_mark_node)
12288             /* We found an id-expression, but it was something that we
12289                should not have found. This is an error, not something
12290                we can recover from, so note that we found an
12291                id-expression and we'll recover as gracefully as
12292                possible.  */
12293             id_expression_or_member_access_p = true;
12294         }
12295
12296       if (expr 
12297           && expr != error_mark_node
12298           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12299         /* We have an id-expression.  */
12300         id_expression_or_member_access_p = true;
12301     }
12302
12303   if (!id_expression_or_member_access_p)
12304     {
12305       /* Abort the id-expression parse.  */
12306       cp_parser_abort_tentative_parse (parser);
12307
12308       /* Parsing tentatively, again.  */
12309       cp_parser_parse_tentatively (parser);
12310
12311       /* Parse a class member access.  */
12312       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12313                                            /*cast_p=*/false, /*decltype*/true,
12314                                            /*member_access_only_p=*/true, NULL);
12315
12316       if (expr 
12317           && expr != error_mark_node
12318           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12319         /* We have an id-expression.  */
12320         id_expression_or_member_access_p = true;
12321     }
12322
12323   if (id_expression_or_member_access_p)
12324     /* We have parsed the complete id-expression or member access.  */
12325     cp_parser_parse_definitely (parser);
12326   else
12327     {
12328       /* Abort our attempt to parse an id-expression or member access
12329          expression.  */
12330       cp_parser_abort_tentative_parse (parser);
12331
12332       /* Parse a full expression.  */
12333       expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12334                                    /*decltype_p=*/true);
12335     }
12336
12337   return expr;
12338 }
12339
12340 /* Parse a `decltype' type. Returns the type.
12341
12342    simple-type-specifier:
12343      decltype ( expression )
12344    C++14 proposal:
12345      decltype ( auto )  */
12346
12347 static tree
12348 cp_parser_decltype (cp_parser *parser)
12349 {
12350   tree expr;
12351   bool id_expression_or_member_access_p = false;
12352   const char *saved_message;
12353   bool saved_integral_constant_expression_p;
12354   bool saved_non_integral_constant_expression_p;
12355   bool saved_greater_than_is_operator_p;
12356   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12357
12358   if (start_token->type == CPP_DECLTYPE)
12359     {
12360       /* Already parsed.  */
12361       cp_lexer_consume_token (parser->lexer);
12362       return start_token->u.value;
12363     }
12364
12365   /* Look for the `decltype' token.  */
12366   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12367     return error_mark_node;
12368
12369   /* Parse the opening `('.  */
12370   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12371     return error_mark_node;
12372
12373   /* decltype (auto) */
12374   if (cxx_dialect >= cxx14
12375       && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12376     {
12377       cp_lexer_consume_token (parser->lexer);
12378       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12379         return error_mark_node;
12380       expr = make_decltype_auto ();
12381       AUTO_IS_DECLTYPE (expr) = true;
12382       goto rewrite;
12383     }
12384
12385   /* Types cannot be defined in a `decltype' expression.  Save away the
12386      old message.  */
12387   saved_message = parser->type_definition_forbidden_message;
12388
12389   /* And create the new one.  */
12390   parser->type_definition_forbidden_message
12391     = G_("types may not be defined in %<decltype%> expressions");
12392
12393   /* The restrictions on constant-expressions do not apply inside
12394      decltype expressions.  */
12395   saved_integral_constant_expression_p
12396     = parser->integral_constant_expression_p;
12397   saved_non_integral_constant_expression_p
12398     = parser->non_integral_constant_expression_p;
12399   parser->integral_constant_expression_p = false;
12400
12401   /* Within a parenthesized expression, a `>' token is always
12402      the greater-than operator.  */
12403   saved_greater_than_is_operator_p
12404     = parser->greater_than_is_operator_p;
12405   parser->greater_than_is_operator_p = true;
12406
12407   /* Do not actually evaluate the expression.  */
12408   ++cp_unevaluated_operand;
12409
12410   /* Do not warn about problems with the expression.  */
12411   ++c_inhibit_evaluation_warnings;
12412
12413   expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12414
12415   /* Go back to evaluating expressions.  */
12416   --cp_unevaluated_operand;
12417   --c_inhibit_evaluation_warnings;
12418
12419   /* The `>' token might be the end of a template-id or
12420      template-parameter-list now.  */
12421   parser->greater_than_is_operator_p
12422     = saved_greater_than_is_operator_p;
12423
12424   /* Restore the old message and the integral constant expression
12425      flags.  */
12426   parser->type_definition_forbidden_message = saved_message;
12427   parser->integral_constant_expression_p
12428     = saved_integral_constant_expression_p;
12429   parser->non_integral_constant_expression_p
12430     = saved_non_integral_constant_expression_p;
12431
12432   /* Parse to the closing `)'.  */
12433   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12434     {
12435       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12436                                              /*consume_paren=*/true);
12437       return error_mark_node;
12438     }
12439
12440   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12441                                tf_warning_or_error);
12442
12443  rewrite:
12444   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12445      it again.  */
12446   start_token->type = CPP_DECLTYPE;
12447   start_token->u.value = expr;
12448   start_token->keyword = RID_MAX;
12449   cp_lexer_purge_tokens_after (parser->lexer, start_token);
12450
12451   return expr;
12452 }
12453
12454 /* Special member functions [gram.special] */
12455
12456 /* Parse a conversion-function-id.
12457
12458    conversion-function-id:
12459      operator conversion-type-id
12460
12461    Returns an IDENTIFIER_NODE representing the operator.  */
12462
12463 static tree
12464 cp_parser_conversion_function_id (cp_parser* parser)
12465 {
12466   tree type;
12467   tree saved_scope;
12468   tree saved_qualifying_scope;
12469   tree saved_object_scope;
12470   tree pushed_scope = NULL_TREE;
12471
12472   /* Look for the `operator' token.  */
12473   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12474     return error_mark_node;
12475   /* When we parse the conversion-type-id, the current scope will be
12476      reset.  However, we need that information in able to look up the
12477      conversion function later, so we save it here.  */
12478   saved_scope = parser->scope;
12479   saved_qualifying_scope = parser->qualifying_scope;
12480   saved_object_scope = parser->object_scope;
12481   /* We must enter the scope of the class so that the names of
12482      entities declared within the class are available in the
12483      conversion-type-id.  For example, consider:
12484
12485        struct S {
12486          typedef int I;
12487          operator I();
12488        };
12489
12490        S::operator I() { ... }
12491
12492      In order to see that `I' is a type-name in the definition, we
12493      must be in the scope of `S'.  */
12494   if (saved_scope)
12495     pushed_scope = push_scope (saved_scope);
12496   /* Parse the conversion-type-id.  */
12497   type = cp_parser_conversion_type_id (parser);
12498   /* Leave the scope of the class, if any.  */
12499   if (pushed_scope)
12500     pop_scope (pushed_scope);
12501   /* Restore the saved scope.  */
12502   parser->scope = saved_scope;
12503   parser->qualifying_scope = saved_qualifying_scope;
12504   parser->object_scope = saved_object_scope;
12505   /* If the TYPE is invalid, indicate failure.  */
12506   if (type == error_mark_node)
12507     return error_mark_node;
12508   return mangle_conv_op_name_for_type (type);
12509 }
12510
12511 /* Parse a conversion-type-id:
12512
12513    conversion-type-id:
12514      type-specifier-seq conversion-declarator [opt]
12515
12516    Returns the TYPE specified.  */
12517
12518 static tree
12519 cp_parser_conversion_type_id (cp_parser* parser)
12520 {
12521   tree attributes;
12522   cp_decl_specifier_seq type_specifiers;
12523   cp_declarator *declarator;
12524   tree type_specified;
12525   const char *saved_message;
12526
12527   /* Parse the attributes.  */
12528   attributes = cp_parser_attributes_opt (parser);
12529
12530   saved_message = parser->type_definition_forbidden_message;
12531   parser->type_definition_forbidden_message
12532     = G_("types may not be defined in a conversion-type-id");
12533
12534   /* Parse the type-specifiers.  */
12535   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12536                                 /*is_trailing_return=*/false,
12537                                 &type_specifiers);
12538
12539   parser->type_definition_forbidden_message = saved_message;
12540
12541   /* If that didn't work, stop.  */
12542   if (type_specifiers.type == error_mark_node)
12543     return error_mark_node;
12544   /* Parse the conversion-declarator.  */
12545   declarator = cp_parser_conversion_declarator_opt (parser);
12546
12547   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
12548                                     /*initialized=*/0, &attributes);
12549   if (attributes)
12550     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12551
12552   /* Don't give this error when parsing tentatively.  This happens to
12553      work because we always parse this definitively once.  */
12554   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12555       && type_uses_auto (type_specified))
12556     {
12557       if (cxx_dialect < cxx14)
12558         {
12559           error ("invalid use of %<auto%> in conversion operator");
12560           return error_mark_node;
12561         }
12562       else if (template_parm_scope_p ())
12563         warning (0, "use of %<auto%> in member template "
12564                  "conversion operator can never be deduced");
12565     }
12566
12567   return type_specified;
12568 }
12569
12570 /* Parse an (optional) conversion-declarator.
12571
12572    conversion-declarator:
12573      ptr-operator conversion-declarator [opt]
12574
12575    */
12576
12577 static cp_declarator *
12578 cp_parser_conversion_declarator_opt (cp_parser* parser)
12579 {
12580   enum tree_code code;
12581   tree class_type, std_attributes = NULL_TREE;
12582   cp_cv_quals cv_quals;
12583
12584   /* We don't know if there's a ptr-operator next, or not.  */
12585   cp_parser_parse_tentatively (parser);
12586   /* Try the ptr-operator.  */
12587   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12588                                  &std_attributes);
12589   /* If it worked, look for more conversion-declarators.  */
12590   if (cp_parser_parse_definitely (parser))
12591     {
12592       cp_declarator *declarator;
12593
12594       /* Parse another optional declarator.  */
12595       declarator = cp_parser_conversion_declarator_opt (parser);
12596
12597       declarator = cp_parser_make_indirect_declarator
12598         (code, class_type, cv_quals, declarator, std_attributes);
12599
12600       return declarator;
12601    }
12602
12603   return NULL;
12604 }
12605
12606 /* Parse an (optional) ctor-initializer.
12607
12608    ctor-initializer:
12609      : mem-initializer-list
12610
12611    Returns TRUE iff the ctor-initializer was actually present.  */
12612
12613 static bool
12614 cp_parser_ctor_initializer_opt (cp_parser* parser)
12615 {
12616   /* If the next token is not a `:', then there is no
12617      ctor-initializer.  */
12618   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12619     {
12620       /* Do default initialization of any bases and members.  */
12621       if (DECL_CONSTRUCTOR_P (current_function_decl))
12622         finish_mem_initializers (NULL_TREE);
12623
12624       return false;
12625     }
12626
12627   /* Consume the `:' token.  */
12628   cp_lexer_consume_token (parser->lexer);
12629   /* And the mem-initializer-list.  */
12630   cp_parser_mem_initializer_list (parser);
12631
12632   return true;
12633 }
12634
12635 /* Parse a mem-initializer-list.
12636
12637    mem-initializer-list:
12638      mem-initializer ... [opt]
12639      mem-initializer ... [opt] , mem-initializer-list  */
12640
12641 static void
12642 cp_parser_mem_initializer_list (cp_parser* parser)
12643 {
12644   tree mem_initializer_list = NULL_TREE;
12645   tree target_ctor = error_mark_node;
12646   cp_token *token = cp_lexer_peek_token (parser->lexer);
12647
12648   /* Let the semantic analysis code know that we are starting the
12649      mem-initializer-list.  */
12650   if (!DECL_CONSTRUCTOR_P (current_function_decl))
12651     error_at (token->location,
12652               "only constructors take member initializers");
12653
12654   /* Loop through the list.  */
12655   while (true)
12656     {
12657       tree mem_initializer;
12658
12659       token = cp_lexer_peek_token (parser->lexer);
12660       /* Parse the mem-initializer.  */
12661       mem_initializer = cp_parser_mem_initializer (parser);
12662       /* If the next token is a `...', we're expanding member initializers. */
12663       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12664         {
12665           /* Consume the `...'. */
12666           cp_lexer_consume_token (parser->lexer);
12667
12668           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12669              can be expanded but members cannot. */
12670           if (mem_initializer != error_mark_node
12671               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12672             {
12673               error_at (token->location,
12674                         "cannot expand initializer for member %<%D%>",
12675                         TREE_PURPOSE (mem_initializer));
12676               mem_initializer = error_mark_node;
12677             }
12678
12679           /* Construct the pack expansion type. */
12680           if (mem_initializer != error_mark_node)
12681             mem_initializer = make_pack_expansion (mem_initializer);
12682         }
12683       if (target_ctor != error_mark_node
12684           && mem_initializer != error_mark_node)
12685         {
12686           error ("mem-initializer for %qD follows constructor delegation",
12687                  TREE_PURPOSE (mem_initializer));
12688           mem_initializer = error_mark_node;
12689         }
12690       /* Look for a target constructor. */
12691       if (mem_initializer != error_mark_node
12692           && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12693           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12694         {
12695           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12696           if (mem_initializer_list)
12697             {
12698               error ("constructor delegation follows mem-initializer for %qD",
12699                      TREE_PURPOSE (mem_initializer_list));
12700               mem_initializer = error_mark_node;
12701             }
12702           target_ctor = mem_initializer;
12703         }
12704       /* Add it to the list, unless it was erroneous.  */
12705       if (mem_initializer != error_mark_node)
12706         {
12707           TREE_CHAIN (mem_initializer) = mem_initializer_list;
12708           mem_initializer_list = mem_initializer;
12709         }
12710       /* If the next token is not a `,', we're done.  */
12711       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12712         break;
12713       /* Consume the `,' token.  */
12714       cp_lexer_consume_token (parser->lexer);
12715     }
12716
12717   /* Perform semantic analysis.  */
12718   if (DECL_CONSTRUCTOR_P (current_function_decl))
12719     finish_mem_initializers (mem_initializer_list);
12720 }
12721
12722 /* Parse a mem-initializer.
12723
12724    mem-initializer:
12725      mem-initializer-id ( expression-list [opt] )
12726      mem-initializer-id braced-init-list
12727
12728    GNU extension:
12729
12730    mem-initializer:
12731      ( expression-list [opt] )
12732
12733    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
12734    class) or FIELD_DECL (for a non-static data member) to initialize;
12735    the TREE_VALUE is the expression-list.  An empty initialization
12736    list is represented by void_list_node.  */
12737
12738 static tree
12739 cp_parser_mem_initializer (cp_parser* parser)
12740 {
12741   tree mem_initializer_id;
12742   tree expression_list;
12743   tree member;
12744   cp_token *token = cp_lexer_peek_token (parser->lexer);
12745
12746   /* Find out what is being initialized.  */
12747   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12748     {
12749       permerror (token->location,
12750                  "anachronistic old-style base class initializer");
12751       mem_initializer_id = NULL_TREE;
12752     }
12753   else
12754     {
12755       mem_initializer_id = cp_parser_mem_initializer_id (parser);
12756       if (mem_initializer_id == error_mark_node)
12757         return mem_initializer_id;
12758     }
12759   member = expand_member_init (mem_initializer_id);
12760   if (member && !DECL_P (member))
12761     in_base_initializer = 1;
12762
12763   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12764     {
12765       bool expr_non_constant_p;
12766       cp_lexer_set_source_position (parser->lexer);
12767       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12768       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12769       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12770       expression_list = build_tree_list (NULL_TREE, expression_list);
12771     }
12772   else
12773     {
12774       vec<tree, va_gc> *vec;
12775       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12776                                                      /*cast_p=*/false,
12777                                                      /*allow_expansion_p=*/true,
12778                                                      /*non_constant_p=*/NULL);
12779       if (vec == NULL)
12780         return error_mark_node;
12781       expression_list = build_tree_list_vec (vec);
12782       release_tree_vector (vec);
12783     }
12784
12785   if (expression_list == error_mark_node)
12786     return error_mark_node;
12787   if (!expression_list)
12788     expression_list = void_type_node;
12789
12790   in_base_initializer = 0;
12791
12792   return member ? build_tree_list (member, expression_list) : error_mark_node;
12793 }
12794
12795 /* Parse a mem-initializer-id.
12796
12797    mem-initializer-id:
12798      :: [opt] nested-name-specifier [opt] class-name
12799      identifier
12800
12801    Returns a TYPE indicating the class to be initializer for the first
12802    production.  Returns an IDENTIFIER_NODE indicating the data member
12803    to be initialized for the second production.  */
12804
12805 static tree
12806 cp_parser_mem_initializer_id (cp_parser* parser)
12807 {
12808   bool global_scope_p;
12809   bool nested_name_specifier_p;
12810   bool template_p = false;
12811   tree id;
12812
12813   cp_token *token = cp_lexer_peek_token (parser->lexer);
12814
12815   /* `typename' is not allowed in this context ([temp.res]).  */
12816   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12817     {
12818       error_at (token->location, 
12819                 "keyword %<typename%> not allowed in this context (a qualified "
12820                 "member initializer is implicitly a type)");
12821       cp_lexer_consume_token (parser->lexer);
12822     }
12823   /* Look for the optional `::' operator.  */
12824   global_scope_p
12825     = (cp_parser_global_scope_opt (parser,
12826                                    /*current_scope_valid_p=*/false)
12827        != NULL_TREE);
12828   /* Look for the optional nested-name-specifier.  The simplest way to
12829      implement:
12830
12831        [temp.res]
12832
12833        The keyword `typename' is not permitted in a base-specifier or
12834        mem-initializer; in these contexts a qualified name that
12835        depends on a template-parameter is implicitly assumed to be a
12836        type name.
12837
12838      is to assume that we have seen the `typename' keyword at this
12839      point.  */
12840   nested_name_specifier_p
12841     = (cp_parser_nested_name_specifier_opt (parser,
12842                                             /*typename_keyword_p=*/true,
12843                                             /*check_dependency_p=*/true,
12844                                             /*type_p=*/true,
12845                                             /*is_declaration=*/true)
12846        != NULL_TREE);
12847   if (nested_name_specifier_p)
12848     template_p = cp_parser_optional_template_keyword (parser);
12849   /* If there is a `::' operator or a nested-name-specifier, then we
12850      are definitely looking for a class-name.  */
12851   if (global_scope_p || nested_name_specifier_p)
12852     return cp_parser_class_name (parser,
12853                                  /*typename_keyword_p=*/true,
12854                                  /*template_keyword_p=*/template_p,
12855                                  typename_type,
12856                                  /*check_dependency_p=*/true,
12857                                  /*class_head_p=*/false,
12858                                  /*is_declaration=*/true);
12859   /* Otherwise, we could also be looking for an ordinary identifier.  */
12860   cp_parser_parse_tentatively (parser);
12861   /* Try a class-name.  */
12862   id = cp_parser_class_name (parser,
12863                              /*typename_keyword_p=*/true,
12864                              /*template_keyword_p=*/false,
12865                              none_type,
12866                              /*check_dependency_p=*/true,
12867                              /*class_head_p=*/false,
12868                              /*is_declaration=*/true);
12869   /* If we found one, we're done.  */
12870   if (cp_parser_parse_definitely (parser))
12871     return id;
12872   /* Otherwise, look for an ordinary identifier.  */
12873   return cp_parser_identifier (parser);
12874 }
12875
12876 /* Overloading [gram.over] */
12877
12878 /* Parse an operator-function-id.
12879
12880    operator-function-id:
12881      operator operator
12882
12883    Returns an IDENTIFIER_NODE for the operator which is a
12884    human-readable spelling of the identifier, e.g., `operator +'.  */
12885
12886 static tree
12887 cp_parser_operator_function_id (cp_parser* parser)
12888 {
12889   /* Look for the `operator' keyword.  */
12890   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12891     return error_mark_node;
12892   /* And then the name of the operator itself.  */
12893   return cp_parser_operator (parser);
12894 }
12895
12896 /* Return an identifier node for a user-defined literal operator.
12897    The suffix identifier is chained to the operator name identifier.  */
12898
12899 static tree
12900 cp_literal_operator_id (const char* name)
12901 {
12902   tree identifier;
12903   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12904                               + strlen (name) + 10);
12905   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12906   identifier = get_identifier (buffer);
12907
12908   return identifier;
12909 }
12910
12911 /* Parse an operator.
12912
12913    operator:
12914      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12915      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12916      || ++ -- , ->* -> () []
12917
12918    GNU Extensions:
12919
12920    operator:
12921      <? >? <?= >?=
12922
12923    Returns an IDENTIFIER_NODE for the operator which is a
12924    human-readable spelling of the identifier, e.g., `operator +'.  */
12925
12926 static tree
12927 cp_parser_operator (cp_parser* parser)
12928 {
12929   tree id = NULL_TREE;
12930   cp_token *token;
12931   bool utf8 = false;
12932
12933   /* Peek at the next token.  */
12934   token = cp_lexer_peek_token (parser->lexer);
12935   /* Figure out which operator we have.  */
12936   switch (token->type)
12937     {
12938     case CPP_KEYWORD:
12939       {
12940         enum tree_code op;
12941
12942         /* The keyword should be either `new' or `delete'.  */
12943         if (token->keyword == RID_NEW)
12944           op = NEW_EXPR;
12945         else if (token->keyword == RID_DELETE)
12946           op = DELETE_EXPR;
12947         else
12948           break;
12949
12950         /* Consume the `new' or `delete' token.  */
12951         cp_lexer_consume_token (parser->lexer);
12952
12953         /* Peek at the next token.  */
12954         token = cp_lexer_peek_token (parser->lexer);
12955         /* If it's a `[' token then this is the array variant of the
12956            operator.  */
12957         if (token->type == CPP_OPEN_SQUARE)
12958           {
12959             /* Consume the `[' token.  */
12960             cp_lexer_consume_token (parser->lexer);
12961             /* Look for the `]' token.  */
12962             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12963             id = ansi_opname (op == NEW_EXPR
12964                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12965           }
12966         /* Otherwise, we have the non-array variant.  */
12967         else
12968           id = ansi_opname (op);
12969
12970         return id;
12971       }
12972
12973     case CPP_PLUS:
12974       id = ansi_opname (PLUS_EXPR);
12975       break;
12976
12977     case CPP_MINUS:
12978       id = ansi_opname (MINUS_EXPR);
12979       break;
12980
12981     case CPP_MULT:
12982       id = ansi_opname (MULT_EXPR);
12983       break;
12984
12985     case CPP_DIV:
12986       id = ansi_opname (TRUNC_DIV_EXPR);
12987       break;
12988
12989     case CPP_MOD:
12990       id = ansi_opname (TRUNC_MOD_EXPR);
12991       break;
12992
12993     case CPP_XOR:
12994       id = ansi_opname (BIT_XOR_EXPR);
12995       break;
12996
12997     case CPP_AND:
12998       id = ansi_opname (BIT_AND_EXPR);
12999       break;
13000
13001     case CPP_OR:
13002       id = ansi_opname (BIT_IOR_EXPR);
13003       break;
13004
13005     case CPP_COMPL:
13006       id = ansi_opname (BIT_NOT_EXPR);
13007       break;
13008
13009     case CPP_NOT:
13010       id = ansi_opname (TRUTH_NOT_EXPR);
13011       break;
13012
13013     case CPP_EQ:
13014       id = ansi_assopname (NOP_EXPR);
13015       break;
13016
13017     case CPP_LESS:
13018       id = ansi_opname (LT_EXPR);
13019       break;
13020
13021     case CPP_GREATER:
13022       id = ansi_opname (GT_EXPR);
13023       break;
13024
13025     case CPP_PLUS_EQ:
13026       id = ansi_assopname (PLUS_EXPR);
13027       break;
13028
13029     case CPP_MINUS_EQ:
13030       id = ansi_assopname (MINUS_EXPR);
13031       break;
13032
13033     case CPP_MULT_EQ:
13034       id = ansi_assopname (MULT_EXPR);
13035       break;
13036
13037     case CPP_DIV_EQ:
13038       id = ansi_assopname (TRUNC_DIV_EXPR);
13039       break;
13040
13041     case CPP_MOD_EQ:
13042       id = ansi_assopname (TRUNC_MOD_EXPR);
13043       break;
13044
13045     case CPP_XOR_EQ:
13046       id = ansi_assopname (BIT_XOR_EXPR);
13047       break;
13048
13049     case CPP_AND_EQ:
13050       id = ansi_assopname (BIT_AND_EXPR);
13051       break;
13052
13053     case CPP_OR_EQ:
13054       id = ansi_assopname (BIT_IOR_EXPR);
13055       break;
13056
13057     case CPP_LSHIFT:
13058       id = ansi_opname (LSHIFT_EXPR);
13059       break;
13060
13061     case CPP_RSHIFT:
13062       id = ansi_opname (RSHIFT_EXPR);
13063       break;
13064
13065     case CPP_LSHIFT_EQ:
13066       id = ansi_assopname (LSHIFT_EXPR);
13067       break;
13068
13069     case CPP_RSHIFT_EQ:
13070       id = ansi_assopname (RSHIFT_EXPR);
13071       break;
13072
13073     case CPP_EQ_EQ:
13074       id = ansi_opname (EQ_EXPR);
13075       break;
13076
13077     case CPP_NOT_EQ:
13078       id = ansi_opname (NE_EXPR);
13079       break;
13080
13081     case CPP_LESS_EQ:
13082       id = ansi_opname (LE_EXPR);
13083       break;
13084
13085     case CPP_GREATER_EQ:
13086       id = ansi_opname (GE_EXPR);
13087       break;
13088
13089     case CPP_AND_AND:
13090       id = ansi_opname (TRUTH_ANDIF_EXPR);
13091       break;
13092
13093     case CPP_OR_OR:
13094       id = ansi_opname (TRUTH_ORIF_EXPR);
13095       break;
13096
13097     case CPP_PLUS_PLUS:
13098       id = ansi_opname (POSTINCREMENT_EXPR);
13099       break;
13100
13101     case CPP_MINUS_MINUS:
13102       id = ansi_opname (PREDECREMENT_EXPR);
13103       break;
13104
13105     case CPP_COMMA:
13106       id = ansi_opname (COMPOUND_EXPR);
13107       break;
13108
13109     case CPP_DEREF_STAR:
13110       id = ansi_opname (MEMBER_REF);
13111       break;
13112
13113     case CPP_DEREF:
13114       id = ansi_opname (COMPONENT_REF);
13115       break;
13116
13117     case CPP_OPEN_PAREN:
13118       /* Consume the `('.  */
13119       cp_lexer_consume_token (parser->lexer);
13120       /* Look for the matching `)'.  */
13121       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13122       return ansi_opname (CALL_EXPR);
13123
13124     case CPP_OPEN_SQUARE:
13125       /* Consume the `['.  */
13126       cp_lexer_consume_token (parser->lexer);
13127       /* Look for the matching `]'.  */
13128       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13129       return ansi_opname (ARRAY_REF);
13130
13131     case CPP_UTF8STRING:
13132     case CPP_UTF8STRING_USERDEF:
13133       utf8 = true;
13134     case CPP_STRING:
13135     case CPP_WSTRING:
13136     case CPP_STRING16:
13137     case CPP_STRING32:
13138     case CPP_STRING_USERDEF:
13139     case CPP_WSTRING_USERDEF:
13140     case CPP_STRING16_USERDEF:
13141     case CPP_STRING32_USERDEF:
13142       {
13143         tree str, string_tree;
13144         int sz, len;
13145
13146         if (cxx_dialect == cxx98)
13147           maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13148
13149         /* Consume the string.  */
13150         str = cp_parser_string_literal (parser, /*translate=*/true,
13151                                       /*wide_ok=*/true, /*lookup_udlit=*/false);
13152         if (str == error_mark_node)
13153           return error_mark_node;
13154         else if (TREE_CODE (str) == USERDEF_LITERAL)
13155           {
13156             string_tree = USERDEF_LITERAL_VALUE (str);
13157             id = USERDEF_LITERAL_SUFFIX_ID (str);
13158           }
13159         else
13160           {
13161             string_tree = str;
13162             /* Look for the suffix identifier.  */
13163             token = cp_lexer_peek_token (parser->lexer);
13164             if (token->type == CPP_NAME)
13165               id = cp_parser_identifier (parser);
13166             else if (token->type == CPP_KEYWORD)
13167               {
13168                 error ("unexpected keyword;"
13169                        " remove space between quotes and suffix identifier");
13170                 return error_mark_node;
13171               }
13172             else
13173               {
13174                 error ("expected suffix identifier");
13175                 return error_mark_node;
13176               }
13177           }
13178         sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
13179                                (TREE_TYPE (TREE_TYPE (string_tree))));
13180         len = TREE_STRING_LENGTH (string_tree) / sz - 1;
13181         if (len != 0)
13182           {
13183             error ("expected empty string after %<operator%> keyword");
13184             return error_mark_node;
13185           }
13186         if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
13187             != char_type_node)
13188           {
13189             error ("invalid encoding prefix in literal operator");
13190             return error_mark_node;
13191           }
13192         if (id != error_mark_node)
13193           {
13194             const char *name = IDENTIFIER_POINTER (id);
13195             id = cp_literal_operator_id (name);
13196           }
13197         return id;
13198       }
13199
13200     default:
13201       /* Anything else is an error.  */
13202       break;
13203     }
13204
13205   /* If we have selected an identifier, we need to consume the
13206      operator token.  */
13207   if (id)
13208     cp_lexer_consume_token (parser->lexer);
13209   /* Otherwise, no valid operator name was present.  */
13210   else
13211     {
13212       cp_parser_error (parser, "expected operator");
13213       id = error_mark_node;
13214     }
13215
13216   return id;
13217 }
13218
13219 /* Parse a template-declaration.
13220
13221    template-declaration:
13222      export [opt] template < template-parameter-list > declaration
13223
13224    If MEMBER_P is TRUE, this template-declaration occurs within a
13225    class-specifier.
13226
13227    The grammar rule given by the standard isn't correct.  What
13228    is really meant is:
13229
13230    template-declaration:
13231      export [opt] template-parameter-list-seq
13232        decl-specifier-seq [opt] init-declarator [opt] ;
13233      export [opt] template-parameter-list-seq
13234        function-definition
13235
13236    template-parameter-list-seq:
13237      template-parameter-list-seq [opt]
13238      template < template-parameter-list >  */
13239
13240 static void
13241 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13242 {
13243   /* Check for `export'.  */
13244   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13245     {
13246       /* Consume the `export' token.  */
13247       cp_lexer_consume_token (parser->lexer);
13248       /* Warn that we do not support `export'.  */
13249       warning (0, "keyword %<export%> not implemented, and will be ignored");
13250     }
13251
13252   cp_parser_template_declaration_after_export (parser, member_p);
13253 }
13254
13255 /* Parse a template-parameter-list.
13256
13257    template-parameter-list:
13258      template-parameter
13259      template-parameter-list , template-parameter
13260
13261    Returns a TREE_LIST.  Each node represents a template parameter.
13262    The nodes are connected via their TREE_CHAINs.  */
13263
13264 static tree
13265 cp_parser_template_parameter_list (cp_parser* parser)
13266 {
13267   tree parameter_list = NULL_TREE;
13268
13269   begin_template_parm_list ();
13270
13271   /* The loop below parses the template parms.  We first need to know
13272      the total number of template parms to be able to compute proper
13273      canonical types of each dependent type. So after the loop, when
13274      we know the total number of template parms,
13275      end_template_parm_list computes the proper canonical types and
13276      fixes up the dependent types accordingly.  */
13277   while (true)
13278     {
13279       tree parameter;
13280       bool is_non_type;
13281       bool is_parameter_pack;
13282       location_t parm_loc;
13283
13284       /* Parse the template-parameter.  */
13285       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13286       parameter = cp_parser_template_parameter (parser, 
13287                                                 &is_non_type,
13288                                                 &is_parameter_pack);
13289       /* Add it to the list.  */
13290       if (parameter != error_mark_node)
13291         parameter_list = process_template_parm (parameter_list,
13292                                                 parm_loc,
13293                                                 parameter,
13294                                                 is_non_type,
13295                                                 is_parameter_pack);
13296       else
13297        {
13298          tree err_parm = build_tree_list (parameter, parameter);
13299          parameter_list = chainon (parameter_list, err_parm);
13300        }
13301
13302       /* If the next token is not a `,', we're done.  */
13303       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13304         break;
13305       /* Otherwise, consume the `,' token.  */
13306       cp_lexer_consume_token (parser->lexer);
13307     }
13308
13309   return end_template_parm_list (parameter_list);
13310 }
13311
13312 /* Parse a template-parameter.
13313
13314    template-parameter:
13315      type-parameter
13316      parameter-declaration
13317
13318    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
13319    the parameter.  The TREE_PURPOSE is the default value, if any.
13320    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
13321    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
13322    set to true iff this parameter is a parameter pack. */
13323
13324 static tree
13325 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13326                               bool *is_parameter_pack)
13327 {
13328   cp_token *token;
13329   cp_parameter_declarator *parameter_declarator;
13330   cp_declarator *id_declarator;
13331   tree parm;
13332
13333   /* Assume it is a type parameter or a template parameter.  */
13334   *is_non_type = false;
13335   /* Assume it not a parameter pack. */
13336   *is_parameter_pack = false;
13337   /* Peek at the next token.  */
13338   token = cp_lexer_peek_token (parser->lexer);
13339   /* If it is `class' or `template', we have a type-parameter.  */
13340   if (token->keyword == RID_TEMPLATE)
13341     return cp_parser_type_parameter (parser, is_parameter_pack);
13342   /* If it is `class' or `typename' we do not know yet whether it is a
13343      type parameter or a non-type parameter.  Consider:
13344
13345        template <typename T, typename T::X X> ...
13346
13347      or:
13348
13349        template <class C, class D*> ...
13350
13351      Here, the first parameter is a type parameter, and the second is
13352      a non-type parameter.  We can tell by looking at the token after
13353      the identifier -- if it is a `,', `=', or `>' then we have a type
13354      parameter.  */
13355   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13356     {
13357       /* Peek at the token after `class' or `typename'.  */
13358       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13359       /* If it's an ellipsis, we have a template type parameter
13360          pack. */
13361       if (token->type == CPP_ELLIPSIS)
13362         return cp_parser_type_parameter (parser, is_parameter_pack);
13363       /* If it's an identifier, skip it.  */
13364       if (token->type == CPP_NAME)
13365         token = cp_lexer_peek_nth_token (parser->lexer, 3);
13366       /* Now, see if the token looks like the end of a template
13367          parameter.  */
13368       if (token->type == CPP_COMMA
13369           || token->type == CPP_EQ
13370           || token->type == CPP_GREATER)
13371         return cp_parser_type_parameter (parser, is_parameter_pack);
13372     }
13373
13374   /* Otherwise, it is a non-type parameter.
13375
13376      [temp.param]
13377
13378      When parsing a default template-argument for a non-type
13379      template-parameter, the first non-nested `>' is taken as the end
13380      of the template parameter-list rather than a greater-than
13381      operator.  */
13382   *is_non_type = true;
13383   parameter_declarator
13384      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13385                                         /*parenthesized_p=*/NULL);
13386
13387   if (!parameter_declarator)
13388     return error_mark_node;
13389
13390   /* If the parameter declaration is marked as a parameter pack, set
13391      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13392      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13393      grokdeclarator. */
13394   if (parameter_declarator->declarator
13395       && parameter_declarator->declarator->parameter_pack_p)
13396     {
13397       *is_parameter_pack = true;
13398       parameter_declarator->declarator->parameter_pack_p = false;
13399     }
13400
13401   if (parameter_declarator->default_argument)
13402     {
13403       /* Can happen in some cases of erroneous input (c++/34892).  */
13404       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13405         /* Consume the `...' for better error recovery.  */
13406         cp_lexer_consume_token (parser->lexer);
13407     }
13408   /* If the next token is an ellipsis, and we don't already have it
13409      marked as a parameter pack, then we have a parameter pack (that
13410      has no declarator).  */
13411   else if (!*is_parameter_pack
13412            && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13413            && (declarator_can_be_parameter_pack
13414                (parameter_declarator->declarator)))
13415     {
13416       /* Consume the `...'.  */
13417       cp_lexer_consume_token (parser->lexer);
13418       maybe_warn_variadic_templates ();
13419       
13420       *is_parameter_pack = true;
13421     }
13422   /* We might end up with a pack expansion as the type of the non-type
13423      template parameter, in which case this is a non-type template
13424      parameter pack.  */
13425   else if (parameter_declarator->decl_specifiers.type
13426            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13427     {
13428       *is_parameter_pack = true;
13429       parameter_declarator->decl_specifiers.type = 
13430         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13431     }
13432
13433   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13434     {
13435       /* Parameter packs cannot have default arguments.  However, a
13436          user may try to do so, so we'll parse them and give an
13437          appropriate diagnostic here.  */
13438
13439       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13440       
13441       /* Find the name of the parameter pack.  */     
13442       id_declarator = parameter_declarator->declarator;
13443       while (id_declarator && id_declarator->kind != cdk_id)
13444         id_declarator = id_declarator->declarator;
13445       
13446       if (id_declarator && id_declarator->kind == cdk_id)
13447         error_at (start_token->location,
13448                   "template parameter pack %qD cannot have a default argument",
13449                   id_declarator->u.id.unqualified_name);
13450       else
13451         error_at (start_token->location,
13452                   "template parameter pack cannot have a default argument");
13453       
13454       /* Parse the default argument, but throw away the result.  */
13455       cp_parser_default_argument (parser, /*template_parm_p=*/true);
13456     }
13457
13458   parm = grokdeclarator (parameter_declarator->declarator,
13459                          &parameter_declarator->decl_specifiers,
13460                          TPARM, /*initialized=*/0,
13461                          /*attrlist=*/NULL);
13462   if (parm == error_mark_node)
13463     return error_mark_node;
13464
13465   return build_tree_list (parameter_declarator->default_argument, parm);
13466 }
13467
13468 /* Parse a type-parameter.
13469
13470    type-parameter:
13471      class identifier [opt]
13472      class identifier [opt] = type-id
13473      typename identifier [opt]
13474      typename identifier [opt] = type-id
13475      template < template-parameter-list > class identifier [opt]
13476      template < template-parameter-list > class identifier [opt]
13477        = id-expression
13478
13479    GNU Extension (variadic templates):
13480
13481    type-parameter:
13482      class ... identifier [opt]
13483      typename ... identifier [opt]
13484
13485    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
13486    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
13487    the declaration of the parameter.
13488
13489    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13490
13491 static tree
13492 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13493 {
13494   cp_token *token;
13495   tree parameter;
13496
13497   /* Look for a keyword to tell us what kind of parameter this is.  */
13498   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13499   if (!token)
13500     return error_mark_node;
13501
13502   switch (token->keyword)
13503     {
13504     case RID_CLASS:
13505     case RID_TYPENAME:
13506       {
13507         tree identifier;
13508         tree default_argument;
13509
13510         /* If the next token is an ellipsis, we have a template
13511            argument pack. */
13512         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13513           {
13514             /* Consume the `...' token. */
13515             cp_lexer_consume_token (parser->lexer);
13516             maybe_warn_variadic_templates ();
13517
13518             *is_parameter_pack = true;
13519           }
13520
13521         /* If the next token is an identifier, then it names the
13522            parameter.  */
13523         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13524           identifier = cp_parser_identifier (parser);
13525         else
13526           identifier = NULL_TREE;
13527
13528         /* Create the parameter.  */
13529         parameter = finish_template_type_parm (class_type_node, identifier);
13530
13531         /* If the next token is an `=', we have a default argument.  */
13532         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13533           {
13534             /* Consume the `=' token.  */
13535             cp_lexer_consume_token (parser->lexer);
13536             /* Parse the default-argument.  */
13537             push_deferring_access_checks (dk_no_deferred);
13538             default_argument = cp_parser_type_id (parser);
13539
13540             /* Template parameter packs cannot have default
13541                arguments. */
13542             if (*is_parameter_pack)
13543               {
13544                 if (identifier)
13545                   error_at (token->location,
13546                             "template parameter pack %qD cannot have a "
13547                             "default argument", identifier);
13548                 else
13549                   error_at (token->location,
13550                             "template parameter packs cannot have "
13551                             "default arguments");
13552                 default_argument = NULL_TREE;
13553               }
13554             else if (check_for_bare_parameter_packs (default_argument))
13555               default_argument = error_mark_node;
13556             pop_deferring_access_checks ();
13557           }
13558         else
13559           default_argument = NULL_TREE;
13560
13561         /* Create the combined representation of the parameter and the
13562            default argument.  */
13563         parameter = build_tree_list (default_argument, parameter);
13564       }
13565       break;
13566
13567     case RID_TEMPLATE:
13568       {
13569         tree identifier;
13570         tree default_argument;
13571
13572         /* Look for the `<'.  */
13573         cp_parser_require (parser, CPP_LESS, RT_LESS);
13574         /* Parse the template-parameter-list.  */
13575         cp_parser_template_parameter_list (parser);
13576         /* Look for the `>'.  */
13577         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13578         /* Look for the `class' or 'typename' keywords.  */
13579         cp_parser_type_parameter_key (parser);
13580         /* If the next token is an ellipsis, we have a template
13581            argument pack. */
13582         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13583           {
13584             /* Consume the `...' token. */
13585             cp_lexer_consume_token (parser->lexer);
13586             maybe_warn_variadic_templates ();
13587
13588             *is_parameter_pack = true;
13589           }
13590         /* If the next token is an `=', then there is a
13591            default-argument.  If the next token is a `>', we are at
13592            the end of the parameter-list.  If the next token is a `,',
13593            then we are at the end of this parameter.  */
13594         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13595             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13596             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13597           {
13598             identifier = cp_parser_identifier (parser);
13599             /* Treat invalid names as if the parameter were nameless.  */
13600             if (identifier == error_mark_node)
13601               identifier = NULL_TREE;
13602           }
13603         else
13604           identifier = NULL_TREE;
13605
13606         /* Create the template parameter.  */
13607         parameter = finish_template_template_parm (class_type_node,
13608                                                    identifier);
13609
13610         /* If the next token is an `=', then there is a
13611            default-argument.  */
13612         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13613           {
13614             bool is_template;
13615
13616             /* Consume the `='.  */
13617             cp_lexer_consume_token (parser->lexer);
13618             /* Parse the id-expression.  */
13619             push_deferring_access_checks (dk_no_deferred);
13620             /* save token before parsing the id-expression, for error
13621                reporting */
13622             token = cp_lexer_peek_token (parser->lexer);
13623             default_argument
13624               = cp_parser_id_expression (parser,
13625                                          /*template_keyword_p=*/false,
13626                                          /*check_dependency_p=*/true,
13627                                          /*template_p=*/&is_template,
13628                                          /*declarator_p=*/false,
13629                                          /*optional_p=*/false);
13630             if (TREE_CODE (default_argument) == TYPE_DECL)
13631               /* If the id-expression was a template-id that refers to
13632                  a template-class, we already have the declaration here,
13633                  so no further lookup is needed.  */
13634                  ;
13635             else
13636               /* Look up the name.  */
13637               default_argument
13638                 = cp_parser_lookup_name (parser, default_argument,
13639                                          none_type,
13640                                          /*is_template=*/is_template,
13641                                          /*is_namespace=*/false,
13642                                          /*check_dependency=*/true,
13643                                          /*ambiguous_decls=*/NULL,
13644                                          token->location);
13645             /* See if the default argument is valid.  */
13646             default_argument
13647               = check_template_template_default_arg (default_argument);
13648
13649             /* Template parameter packs cannot have default
13650                arguments. */
13651             if (*is_parameter_pack)
13652               {
13653                 if (identifier)
13654                   error_at (token->location,
13655                             "template parameter pack %qD cannot "
13656                             "have a default argument",
13657                             identifier);
13658                 else
13659                   error_at (token->location, "template parameter packs cannot "
13660                             "have default arguments");
13661                 default_argument = NULL_TREE;
13662               }
13663             pop_deferring_access_checks ();
13664           }
13665         else
13666           default_argument = NULL_TREE;
13667
13668         /* Create the combined representation of the parameter and the
13669            default argument.  */
13670         parameter = build_tree_list (default_argument, parameter);
13671       }
13672       break;
13673
13674     default:
13675       gcc_unreachable ();
13676       break;
13677     }
13678
13679   return parameter;
13680 }
13681
13682 /* Parse a template-id.
13683
13684    template-id:
13685      template-name < template-argument-list [opt] >
13686
13687    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13688    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
13689    returned.  Otherwise, if the template-name names a function, or set
13690    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
13691    names a class, returns a TYPE_DECL for the specialization.
13692
13693    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13694    uninstantiated templates.  */
13695
13696 static tree
13697 cp_parser_template_id (cp_parser *parser,
13698                        bool template_keyword_p,
13699                        bool check_dependency_p,
13700                        enum tag_types tag_type,
13701                        bool is_declaration)
13702 {
13703   int i;
13704   tree templ;
13705   tree arguments;
13706   tree template_id;
13707   cp_token_position start_of_id = 0;
13708   deferred_access_check *chk;
13709   vec<deferred_access_check, va_gc> *access_check;
13710   cp_token *next_token = NULL, *next_token_2 = NULL;
13711   bool is_identifier;
13712
13713   /* If the next token corresponds to a template-id, there is no need
13714      to reparse it.  */
13715   next_token = cp_lexer_peek_token (parser->lexer);
13716   if (next_token->type == CPP_TEMPLATE_ID)
13717     {
13718       struct tree_check *check_value;
13719
13720       /* Get the stored value.  */
13721       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13722       /* Perform any access checks that were deferred.  */
13723       access_check = check_value->checks;
13724       if (access_check)
13725         {
13726           FOR_EACH_VEC_ELT (*access_check, i, chk)
13727             perform_or_defer_access_check (chk->binfo,
13728                                            chk->decl,
13729                                            chk->diag_decl,
13730                                            tf_warning_or_error);
13731         }
13732       /* Return the stored value.  */
13733       return check_value->value;
13734     }
13735
13736   /* Avoid performing name lookup if there is no possibility of
13737      finding a template-id.  */
13738   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13739       || (next_token->type == CPP_NAME
13740           && !cp_parser_nth_token_starts_template_argument_list_p
13741                (parser, 2)))
13742     {
13743       cp_parser_error (parser, "expected template-id");
13744       return error_mark_node;
13745     }
13746
13747   /* Remember where the template-id starts.  */
13748   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13749     start_of_id = cp_lexer_token_position (parser->lexer, false);
13750
13751   push_deferring_access_checks (dk_deferred);
13752
13753   /* Parse the template-name.  */
13754   is_identifier = false;
13755   templ = cp_parser_template_name (parser, template_keyword_p,
13756                                    check_dependency_p,
13757                                    is_declaration,
13758                                    tag_type,
13759                                    &is_identifier);
13760   if (templ == error_mark_node || is_identifier)
13761     {
13762       pop_deferring_access_checks ();
13763       return templ;
13764     }
13765
13766   /* If we find the sequence `[:' after a template-name, it's probably
13767      a digraph-typo for `< ::'. Substitute the tokens and check if we can
13768      parse correctly the argument list.  */
13769   next_token = cp_lexer_peek_token (parser->lexer);
13770   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13771   if (next_token->type == CPP_OPEN_SQUARE
13772       && next_token->flags & DIGRAPH
13773       && next_token_2->type == CPP_COLON
13774       && !(next_token_2->flags & PREV_WHITE))
13775     {
13776       cp_parser_parse_tentatively (parser);
13777       /* Change `:' into `::'.  */
13778       next_token_2->type = CPP_SCOPE;
13779       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13780          CPP_LESS.  */
13781       cp_lexer_consume_token (parser->lexer);
13782
13783       /* Parse the arguments.  */
13784       arguments = cp_parser_enclosed_template_argument_list (parser);
13785       if (!cp_parser_parse_definitely (parser))
13786         {
13787           /* If we couldn't parse an argument list, then we revert our changes
13788              and return simply an error. Maybe this is not a template-id
13789              after all.  */
13790           next_token_2->type = CPP_COLON;
13791           cp_parser_error (parser, "expected %<<%>");
13792           pop_deferring_access_checks ();
13793           return error_mark_node;
13794         }
13795       /* Otherwise, emit an error about the invalid digraph, but continue
13796          parsing because we got our argument list.  */
13797       if (permerror (next_token->location,
13798                      "%<<::%> cannot begin a template-argument list"))
13799         {
13800           static bool hint = false;
13801           inform (next_token->location,
13802                   "%<<:%> is an alternate spelling for %<[%>."
13803                   " Insert whitespace between %<<%> and %<::%>");
13804           if (!hint && !flag_permissive)
13805             {
13806               inform (next_token->location, "(if you use %<-fpermissive%> "
13807                       "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13808                       "accept your code)");
13809               hint = true;
13810             }
13811         }
13812     }
13813   else
13814     {
13815       /* Look for the `<' that starts the template-argument-list.  */
13816       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13817         {
13818           pop_deferring_access_checks ();
13819           return error_mark_node;
13820         }
13821       /* Parse the arguments.  */
13822       arguments = cp_parser_enclosed_template_argument_list (parser);
13823     }
13824
13825   /* Build a representation of the specialization.  */
13826   if (identifier_p (templ))
13827     template_id = build_min_nt_loc (next_token->location,
13828                                     TEMPLATE_ID_EXPR,
13829                                     templ, arguments);
13830   else if (DECL_TYPE_TEMPLATE_P (templ)
13831            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13832     {
13833       bool entering_scope;
13834       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13835          template (rather than some instantiation thereof) only if
13836          is not nested within some other construct.  For example, in
13837          "template <typename T> void f(T) { A<T>::", A<T> is just an
13838          instantiation of A.  */
13839       entering_scope = (template_parm_scope_p ()
13840                         && cp_lexer_next_token_is (parser->lexer,
13841                                                    CPP_SCOPE));
13842       template_id
13843         = finish_template_type (templ, arguments, entering_scope);
13844     }
13845   else if (variable_template_p (templ))
13846     {
13847       template_id = lookup_template_variable (templ, arguments);
13848     }
13849   else
13850     {
13851       /* If it's not a class-template or a template-template, it should be
13852          a function-template.  */
13853       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13854                    || TREE_CODE (templ) == OVERLOAD
13855                    || BASELINK_P (templ)));
13856
13857       template_id = lookup_template_function (templ, arguments);
13858     }
13859
13860   /* If parsing tentatively, replace the sequence of tokens that makes
13861      up the template-id with a CPP_TEMPLATE_ID token.  That way,
13862      should we re-parse the token stream, we will not have to repeat
13863      the effort required to do the parse, nor will we issue duplicate
13864      error messages about problems during instantiation of the
13865      template.  */
13866   if (start_of_id
13867       /* Don't do this if we had a parse error in a declarator; re-parsing
13868          might succeed if a name changes meaning (60361).  */
13869       && !(cp_parser_error_occurred (parser)
13870            && cp_parser_parsing_tentatively (parser)
13871            && parser->in_declarator_p))
13872     {
13873       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13874
13875       /* Reset the contents of the START_OF_ID token.  */
13876       token->type = CPP_TEMPLATE_ID;
13877       /* Retrieve any deferred checks.  Do not pop this access checks yet
13878          so the memory will not be reclaimed during token replacing below.  */
13879       token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13880       token->u.tree_check_value->value = template_id;
13881       token->u.tree_check_value->checks = get_deferred_access_checks ();
13882       token->keyword = RID_MAX;
13883
13884       /* Purge all subsequent tokens.  */
13885       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13886
13887       /* ??? Can we actually assume that, if template_id ==
13888          error_mark_node, we will have issued a diagnostic to the
13889          user, as opposed to simply marking the tentative parse as
13890          failed?  */
13891       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13892         error_at (token->location, "parse error in template argument list");
13893     }
13894
13895   pop_to_parent_deferring_access_checks ();
13896   return template_id;
13897 }
13898
13899 /* Parse a template-name.
13900
13901    template-name:
13902      identifier
13903
13904    The standard should actually say:
13905
13906    template-name:
13907      identifier
13908      operator-function-id
13909
13910    A defect report has been filed about this issue.
13911
13912    A conversion-function-id cannot be a template name because they cannot
13913    be part of a template-id. In fact, looking at this code:
13914
13915    a.operator K<int>()
13916
13917    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13918    It is impossible to call a templated conversion-function-id with an
13919    explicit argument list, since the only allowed template parameter is
13920    the type to which it is converting.
13921
13922    If TEMPLATE_KEYWORD_P is true, then we have just seen the
13923    `template' keyword, in a construction like:
13924
13925      T::template f<3>()
13926
13927    In that case `f' is taken to be a template-name, even though there
13928    is no way of knowing for sure.
13929
13930    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13931    name refers to a set of overloaded functions, at least one of which
13932    is a template, or an IDENTIFIER_NODE with the name of the template,
13933    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
13934    names are looked up inside uninstantiated templates.  */
13935
13936 static tree
13937 cp_parser_template_name (cp_parser* parser,
13938                          bool template_keyword_p,
13939                          bool check_dependency_p,
13940                          bool is_declaration,
13941                          enum tag_types tag_type,
13942                          bool *is_identifier)
13943 {
13944   tree identifier;
13945   tree decl;
13946   tree fns;
13947   cp_token *token = cp_lexer_peek_token (parser->lexer);
13948
13949   /* If the next token is `operator', then we have either an
13950      operator-function-id or a conversion-function-id.  */
13951   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13952     {
13953       /* We don't know whether we're looking at an
13954          operator-function-id or a conversion-function-id.  */
13955       cp_parser_parse_tentatively (parser);
13956       /* Try an operator-function-id.  */
13957       identifier = cp_parser_operator_function_id (parser);
13958       /* If that didn't work, try a conversion-function-id.  */
13959       if (!cp_parser_parse_definitely (parser))
13960         {
13961           cp_parser_error (parser, "expected template-name");
13962           return error_mark_node;
13963         }
13964     }
13965   /* Look for the identifier.  */
13966   else
13967     identifier = cp_parser_identifier (parser);
13968
13969   /* If we didn't find an identifier, we don't have a template-id.  */
13970   if (identifier == error_mark_node)
13971     return error_mark_node;
13972
13973   /* If the name immediately followed the `template' keyword, then it
13974      is a template-name.  However, if the next token is not `<', then
13975      we do not treat it as a template-name, since it is not being used
13976      as part of a template-id.  This enables us to handle constructs
13977      like:
13978
13979        template <typename T> struct S { S(); };
13980        template <typename T> S<T>::S();
13981
13982      correctly.  We would treat `S' as a template -- if it were `S<T>'
13983      -- but we do not if there is no `<'.  */
13984
13985   if (processing_template_decl
13986       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13987     {
13988       /* In a declaration, in a dependent context, we pretend that the
13989          "template" keyword was present in order to improve error
13990          recovery.  For example, given:
13991
13992            template <typename T> void f(T::X<int>);
13993
13994          we want to treat "X<int>" as a template-id.  */
13995       if (is_declaration
13996           && !template_keyword_p
13997           && parser->scope && TYPE_P (parser->scope)
13998           && check_dependency_p
13999           && dependent_scope_p (parser->scope)
14000           /* Do not do this for dtors (or ctors), since they never
14001              need the template keyword before their name.  */
14002           && !constructor_name_p (identifier, parser->scope))
14003         {
14004           cp_token_position start = 0;
14005
14006           /* Explain what went wrong.  */
14007           error_at (token->location, "non-template %qD used as template",
14008                     identifier);
14009           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
14010                   parser->scope, identifier);
14011           /* If parsing tentatively, find the location of the "<" token.  */
14012           if (cp_parser_simulate_error (parser))
14013             start = cp_lexer_token_position (parser->lexer, true);
14014           /* Parse the template arguments so that we can issue error
14015              messages about them.  */
14016           cp_lexer_consume_token (parser->lexer);
14017           cp_parser_enclosed_template_argument_list (parser);
14018           /* Skip tokens until we find a good place from which to
14019              continue parsing.  */
14020           cp_parser_skip_to_closing_parenthesis (parser,
14021                                                  /*recovering=*/true,
14022                                                  /*or_comma=*/true,
14023                                                  /*consume_paren=*/false);
14024           /* If parsing tentatively, permanently remove the
14025              template argument list.  That will prevent duplicate
14026              error messages from being issued about the missing
14027              "template" keyword.  */
14028           if (start)
14029             cp_lexer_purge_tokens_after (parser->lexer, start);
14030           if (is_identifier)
14031             *is_identifier = true;
14032           return identifier;
14033         }
14034
14035       /* If the "template" keyword is present, then there is generally
14036          no point in doing name-lookup, so we just return IDENTIFIER.
14037          But, if the qualifying scope is non-dependent then we can
14038          (and must) do name-lookup normally.  */
14039       if (template_keyword_p
14040           && (!parser->scope
14041               || (TYPE_P (parser->scope)
14042                   && dependent_type_p (parser->scope))))
14043         return identifier;
14044     }
14045
14046   /* Look up the name.  */
14047   decl = cp_parser_lookup_name (parser, identifier,
14048                                 tag_type,
14049                                 /*is_template=*/true,
14050                                 /*is_namespace=*/false,
14051                                 check_dependency_p,
14052                                 /*ambiguous_decls=*/NULL,
14053                                 token->location);
14054
14055   decl = strip_using_decl (decl);
14056
14057   /* If DECL is a template, then the name was a template-name.  */
14058   if (TREE_CODE (decl) == TEMPLATE_DECL)
14059     {
14060       if (TREE_DEPRECATED (decl)
14061           && deprecated_state != DEPRECATED_SUPPRESS)
14062         warn_deprecated_use (decl, NULL_TREE);
14063     }
14064   else
14065     {
14066       tree fn = NULL_TREE;
14067
14068       /* The standard does not explicitly indicate whether a name that
14069          names a set of overloaded declarations, some of which are
14070          templates, is a template-name.  However, such a name should
14071          be a template-name; otherwise, there is no way to form a
14072          template-id for the overloaded templates.  */
14073       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
14074       if (TREE_CODE (fns) == OVERLOAD)
14075         for (fn = fns; fn; fn = OVL_NEXT (fn))
14076           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
14077             break;
14078
14079       if (!fn)
14080         {
14081           /* The name does not name a template.  */
14082           cp_parser_error (parser, "expected template-name");
14083           return error_mark_node;
14084         }
14085     }
14086
14087   /* If DECL is dependent, and refers to a function, then just return
14088      its name; we will look it up again during template instantiation.  */
14089   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
14090     {
14091       tree scope = ovl_scope (decl);
14092       if (TYPE_P (scope) && dependent_type_p (scope))
14093         return identifier;
14094     }
14095
14096   return decl;
14097 }
14098
14099 /* Parse a template-argument-list.
14100
14101    template-argument-list:
14102      template-argument ... [opt]
14103      template-argument-list , template-argument ... [opt]
14104
14105    Returns a TREE_VEC containing the arguments.  */
14106
14107 static tree
14108 cp_parser_template_argument_list (cp_parser* parser)
14109 {
14110   tree fixed_args[10];
14111   unsigned n_args = 0;
14112   unsigned alloced = 10;
14113   tree *arg_ary = fixed_args;
14114   tree vec;
14115   bool saved_in_template_argument_list_p;
14116   bool saved_ice_p;
14117   bool saved_non_ice_p;
14118
14119   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
14120   parser->in_template_argument_list_p = true;
14121   /* Even if the template-id appears in an integral
14122      constant-expression, the contents of the argument list do
14123      not.  */
14124   saved_ice_p = parser->integral_constant_expression_p;
14125   parser->integral_constant_expression_p = false;
14126   saved_non_ice_p = parser->non_integral_constant_expression_p;
14127   parser->non_integral_constant_expression_p = false;
14128
14129   /* Parse the arguments.  */
14130   do
14131     {
14132       tree argument;
14133
14134       if (n_args)
14135         /* Consume the comma.  */
14136         cp_lexer_consume_token (parser->lexer);
14137
14138       /* Parse the template-argument.  */
14139       argument = cp_parser_template_argument (parser);
14140
14141       /* If the next token is an ellipsis, we're expanding a template
14142          argument pack. */
14143       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14144         {
14145           if (argument == error_mark_node)
14146             {
14147               cp_token *token = cp_lexer_peek_token (parser->lexer);
14148               error_at (token->location,
14149                         "expected parameter pack before %<...%>");
14150             }
14151           /* Consume the `...' token. */
14152           cp_lexer_consume_token (parser->lexer);
14153
14154           /* Make the argument into a TYPE_PACK_EXPANSION or
14155              EXPR_PACK_EXPANSION. */
14156           argument = make_pack_expansion (argument);
14157         }
14158
14159       if (n_args == alloced)
14160         {
14161           alloced *= 2;
14162
14163           if (arg_ary == fixed_args)
14164             {
14165               arg_ary = XNEWVEC (tree, alloced);
14166               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
14167             }
14168           else
14169             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
14170         }
14171       arg_ary[n_args++] = argument;
14172     }
14173   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14174
14175   vec = make_tree_vec (n_args);
14176
14177   while (n_args--)
14178     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
14179
14180   if (arg_ary != fixed_args)
14181     free (arg_ary);
14182   parser->non_integral_constant_expression_p = saved_non_ice_p;
14183   parser->integral_constant_expression_p = saved_ice_p;
14184   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
14185 #ifdef ENABLE_CHECKING
14186   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
14187 #endif
14188   return vec;
14189 }
14190
14191 /* Parse a template-argument.
14192
14193    template-argument:
14194      assignment-expression
14195      type-id
14196      id-expression
14197
14198    The representation is that of an assignment-expression, type-id, or
14199    id-expression -- except that the qualified id-expression is
14200    evaluated, so that the value returned is either a DECL or an
14201    OVERLOAD.
14202
14203    Although the standard says "assignment-expression", it forbids
14204    throw-expressions or assignments in the template argument.
14205    Therefore, we use "conditional-expression" instead.  */
14206
14207 static tree
14208 cp_parser_template_argument (cp_parser* parser)
14209 {
14210   tree argument;
14211   bool template_p;
14212   bool address_p;
14213   bool maybe_type_id = false;
14214   cp_token *token = NULL, *argument_start_token = NULL;
14215   location_t loc = 0;
14216   cp_id_kind idk;
14217
14218   /* There's really no way to know what we're looking at, so we just
14219      try each alternative in order.
14220
14221        [temp.arg]
14222
14223        In a template-argument, an ambiguity between a type-id and an
14224        expression is resolved to a type-id, regardless of the form of
14225        the corresponding template-parameter.
14226
14227      Therefore, we try a type-id first.  */
14228   cp_parser_parse_tentatively (parser);
14229   argument = cp_parser_template_type_arg (parser);
14230   /* If there was no error parsing the type-id but the next token is a
14231      '>>', our behavior depends on which dialect of C++ we're
14232      parsing. In C++98, we probably found a typo for '> >'. But there
14233      are type-id which are also valid expressions. For instance:
14234
14235      struct X { int operator >> (int); };
14236      template <int V> struct Foo {};
14237      Foo<X () >> 5> r;
14238
14239      Here 'X()' is a valid type-id of a function type, but the user just
14240      wanted to write the expression "X() >> 5". Thus, we remember that we
14241      found a valid type-id, but we still try to parse the argument as an
14242      expression to see what happens. 
14243
14244      In C++0x, the '>>' will be considered two separate '>'
14245      tokens.  */
14246   if (!cp_parser_error_occurred (parser)
14247       && cxx_dialect == cxx98
14248       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14249     {
14250       maybe_type_id = true;
14251       cp_parser_abort_tentative_parse (parser);
14252     }
14253   else
14254     {
14255       /* If the next token isn't a `,' or a `>', then this argument wasn't
14256       really finished. This means that the argument is not a valid
14257       type-id.  */
14258       if (!cp_parser_next_token_ends_template_argument_p (parser))
14259         cp_parser_error (parser, "expected template-argument");
14260       /* If that worked, we're done.  */
14261       if (cp_parser_parse_definitely (parser))
14262         return argument;
14263     }
14264   /* We're still not sure what the argument will be.  */
14265   cp_parser_parse_tentatively (parser);
14266   /* Try a template.  */
14267   argument_start_token = cp_lexer_peek_token (parser->lexer);
14268   argument = cp_parser_id_expression (parser,
14269                                       /*template_keyword_p=*/false,
14270                                       /*check_dependency_p=*/true,
14271                                       &template_p,
14272                                       /*declarator_p=*/false,
14273                                       /*optional_p=*/false);
14274   /* If the next token isn't a `,' or a `>', then this argument wasn't
14275      really finished.  */
14276   if (!cp_parser_next_token_ends_template_argument_p (parser))
14277     cp_parser_error (parser, "expected template-argument");
14278   if (!cp_parser_error_occurred (parser))
14279     {
14280       /* Figure out what is being referred to.  If the id-expression
14281          was for a class template specialization, then we will have a
14282          TYPE_DECL at this point.  There is no need to do name lookup
14283          at this point in that case.  */
14284       if (TREE_CODE (argument) != TYPE_DECL)
14285         argument = cp_parser_lookup_name (parser, argument,
14286                                           none_type,
14287                                           /*is_template=*/template_p,
14288                                           /*is_namespace=*/false,
14289                                           /*check_dependency=*/true,
14290                                           /*ambiguous_decls=*/NULL,
14291                                           argument_start_token->location);
14292       if (TREE_CODE (argument) != TEMPLATE_DECL
14293           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14294         cp_parser_error (parser, "expected template-name");
14295     }
14296   if (cp_parser_parse_definitely (parser))
14297     {
14298       if (TREE_DEPRECATED (argument))
14299         warn_deprecated_use (argument, NULL_TREE);
14300       return argument;
14301     }
14302   /* It must be a non-type argument.  There permitted cases are given
14303      in [temp.arg.nontype]:
14304
14305      -- an integral constant-expression of integral or enumeration
14306         type; or
14307
14308      -- the name of a non-type template-parameter; or
14309
14310      -- the name of an object or function with external linkage...
14311
14312      -- the address of an object or function with external linkage...
14313
14314      -- a pointer to member...  */
14315   /* Look for a non-type template parameter.  */
14316   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14317     {
14318       cp_parser_parse_tentatively (parser);
14319       argument = cp_parser_primary_expression (parser,
14320                                                /*address_p=*/false,
14321                                                /*cast_p=*/false,
14322                                                /*template_arg_p=*/true,
14323                                                &idk);
14324       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14325           || !cp_parser_next_token_ends_template_argument_p (parser))
14326         cp_parser_simulate_error (parser);
14327       if (cp_parser_parse_definitely (parser))
14328         return argument;
14329     }
14330
14331   /* If the next token is "&", the argument must be the address of an
14332      object or function with external linkage.  */
14333   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14334   if (address_p)
14335     {
14336       loc = cp_lexer_peek_token (parser->lexer)->location;
14337       cp_lexer_consume_token (parser->lexer);
14338     }
14339   /* See if we might have an id-expression.  */
14340   token = cp_lexer_peek_token (parser->lexer);
14341   if (token->type == CPP_NAME
14342       || token->keyword == RID_OPERATOR
14343       || token->type == CPP_SCOPE
14344       || token->type == CPP_TEMPLATE_ID
14345       || token->type == CPP_NESTED_NAME_SPECIFIER)
14346     {
14347       cp_parser_parse_tentatively (parser);
14348       argument = cp_parser_primary_expression (parser,
14349                                                address_p,
14350                                                /*cast_p=*/false,
14351                                                /*template_arg_p=*/true,
14352                                                &idk);
14353       if (cp_parser_error_occurred (parser)
14354           || !cp_parser_next_token_ends_template_argument_p (parser))
14355         cp_parser_abort_tentative_parse (parser);
14356       else
14357         {
14358           tree probe;
14359
14360           if (INDIRECT_REF_P (argument))
14361             {
14362               /* Strip the dereference temporarily.  */
14363               gcc_assert (REFERENCE_REF_P (argument));
14364               argument = TREE_OPERAND (argument, 0);
14365             }
14366
14367           /* If we're in a template, we represent a qualified-id referring
14368              to a static data member as a SCOPE_REF even if the scope isn't
14369              dependent so that we can check access control later.  */
14370           probe = argument;
14371           if (TREE_CODE (probe) == SCOPE_REF)
14372             probe = TREE_OPERAND (probe, 1);
14373           if (VAR_P (probe))
14374             {
14375               /* A variable without external linkage might still be a
14376                  valid constant-expression, so no error is issued here
14377                  if the external-linkage check fails.  */
14378               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14379                 cp_parser_simulate_error (parser);
14380             }
14381           else if (is_overloaded_fn (argument))
14382             /* All overloaded functions are allowed; if the external
14383                linkage test does not pass, an error will be issued
14384                later.  */
14385             ;
14386           else if (address_p
14387                    && (TREE_CODE (argument) == OFFSET_REF
14388                        || TREE_CODE (argument) == SCOPE_REF))
14389             /* A pointer-to-member.  */
14390             ;
14391           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14392             ;
14393           else
14394             cp_parser_simulate_error (parser);
14395
14396           if (cp_parser_parse_definitely (parser))
14397             {
14398               if (address_p)
14399                 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14400                                              tf_warning_or_error);
14401               else
14402                 argument = convert_from_reference (argument);
14403               return argument;
14404             }
14405         }
14406     }
14407   /* If the argument started with "&", there are no other valid
14408      alternatives at this point.  */
14409   if (address_p)
14410     {
14411       cp_parser_error (parser, "invalid non-type template argument");
14412       return error_mark_node;
14413     }
14414
14415   /* If the argument wasn't successfully parsed as a type-id followed
14416      by '>>', the argument can only be a constant expression now.
14417      Otherwise, we try parsing the constant-expression tentatively,
14418      because the argument could really be a type-id.  */
14419   if (maybe_type_id)
14420     cp_parser_parse_tentatively (parser);
14421   argument = cp_parser_constant_expression (parser);
14422
14423   if (!maybe_type_id)
14424     return argument;
14425   if (!cp_parser_next_token_ends_template_argument_p (parser))
14426     cp_parser_error (parser, "expected template-argument");
14427   if (cp_parser_parse_definitely (parser))
14428     return argument;
14429   /* We did our best to parse the argument as a non type-id, but that
14430      was the only alternative that matched (albeit with a '>' after
14431      it). We can assume it's just a typo from the user, and a
14432      diagnostic will then be issued.  */
14433   return cp_parser_template_type_arg (parser);
14434 }
14435
14436 /* Parse an explicit-instantiation.
14437
14438    explicit-instantiation:
14439      template declaration
14440
14441    Although the standard says `declaration', what it really means is:
14442
14443    explicit-instantiation:
14444      template decl-specifier-seq [opt] declarator [opt] ;
14445
14446    Things like `template int S<int>::i = 5, int S<double>::j;' are not
14447    supposed to be allowed.  A defect report has been filed about this
14448    issue.
14449
14450    GNU Extension:
14451
14452    explicit-instantiation:
14453      storage-class-specifier template
14454        decl-specifier-seq [opt] declarator [opt] ;
14455      function-specifier template
14456        decl-specifier-seq [opt] declarator [opt] ;  */
14457
14458 static void
14459 cp_parser_explicit_instantiation (cp_parser* parser)
14460 {
14461   int declares_class_or_enum;
14462   cp_decl_specifier_seq decl_specifiers;
14463   tree extension_specifier = NULL_TREE;
14464
14465   timevar_push (TV_TEMPLATE_INST);
14466
14467   /* Look for an (optional) storage-class-specifier or
14468      function-specifier.  */
14469   if (cp_parser_allow_gnu_extensions_p (parser))
14470     {
14471       extension_specifier
14472         = cp_parser_storage_class_specifier_opt (parser);
14473       if (!extension_specifier)
14474         extension_specifier
14475           = cp_parser_function_specifier_opt (parser,
14476                                               /*decl_specs=*/NULL);
14477     }
14478
14479   /* Look for the `template' keyword.  */
14480   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14481   /* Let the front end know that we are processing an explicit
14482      instantiation.  */
14483   begin_explicit_instantiation ();
14484   /* [temp.explicit] says that we are supposed to ignore access
14485      control while processing explicit instantiation directives.  */
14486   push_deferring_access_checks (dk_no_check);
14487   /* Parse a decl-specifier-seq.  */
14488   cp_parser_decl_specifier_seq (parser,
14489                                 CP_PARSER_FLAGS_OPTIONAL,
14490                                 &decl_specifiers,
14491                                 &declares_class_or_enum);
14492   /* If there was exactly one decl-specifier, and it declared a class,
14493      and there's no declarator, then we have an explicit type
14494      instantiation.  */
14495   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14496     {
14497       tree type;
14498
14499       type = check_tag_decl (&decl_specifiers,
14500                              /*explicit_type_instantiation_p=*/true);
14501       /* Turn access control back on for names used during
14502          template instantiation.  */
14503       pop_deferring_access_checks ();
14504       if (type)
14505         do_type_instantiation (type, extension_specifier,
14506                                /*complain=*/tf_error);
14507     }
14508   else
14509     {
14510       cp_declarator *declarator;
14511       tree decl;
14512
14513       /* Parse the declarator.  */
14514       declarator
14515         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14516                                 /*ctor_dtor_or_conv_p=*/NULL,
14517                                 /*parenthesized_p=*/NULL,
14518                                 /*member_p=*/false,
14519                                 /*friend_p=*/false);
14520       if (declares_class_or_enum & 2)
14521         cp_parser_check_for_definition_in_return_type (declarator,
14522                                                        decl_specifiers.type,
14523                                                        decl_specifiers.locations[ds_type_spec]);
14524       if (declarator != cp_error_declarator)
14525         {
14526           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14527             permerror (decl_specifiers.locations[ds_inline],
14528                        "explicit instantiation shall not use"
14529                        " %<inline%> specifier");
14530           if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14531             permerror (decl_specifiers.locations[ds_constexpr],
14532                        "explicit instantiation shall not use"
14533                        " %<constexpr%> specifier");
14534
14535           decl = grokdeclarator (declarator, &decl_specifiers,
14536                                  NORMAL, 0, &decl_specifiers.attributes);
14537           /* Turn access control back on for names used during
14538              template instantiation.  */
14539           pop_deferring_access_checks ();
14540           /* Do the explicit instantiation.  */
14541           do_decl_instantiation (decl, extension_specifier);
14542         }
14543       else
14544         {
14545           pop_deferring_access_checks ();
14546           /* Skip the body of the explicit instantiation.  */
14547           cp_parser_skip_to_end_of_statement (parser);
14548         }
14549     }
14550   /* We're done with the instantiation.  */
14551   end_explicit_instantiation ();
14552
14553   cp_parser_consume_semicolon_at_end_of_statement (parser);
14554
14555   timevar_pop (TV_TEMPLATE_INST);
14556 }
14557
14558 /* Parse an explicit-specialization.
14559
14560    explicit-specialization:
14561      template < > declaration
14562
14563    Although the standard says `declaration', what it really means is:
14564
14565    explicit-specialization:
14566      template <> decl-specifier [opt] init-declarator [opt] ;
14567      template <> function-definition
14568      template <> explicit-specialization
14569      template <> template-declaration  */
14570
14571 static void
14572 cp_parser_explicit_specialization (cp_parser* parser)
14573 {
14574   bool need_lang_pop;
14575   cp_token *token = cp_lexer_peek_token (parser->lexer);
14576
14577   /* Look for the `template' keyword.  */
14578   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14579   /* Look for the `<'.  */
14580   cp_parser_require (parser, CPP_LESS, RT_LESS);
14581   /* Look for the `>'.  */
14582   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14583   /* We have processed another parameter list.  */
14584   ++parser->num_template_parameter_lists;
14585   /* [temp]
14586
14587      A template ... explicit specialization ... shall not have C
14588      linkage.  */
14589   if (current_lang_name == lang_name_c)
14590     {
14591       error_at (token->location, "template specialization with C linkage");
14592       /* Give it C++ linkage to avoid confusing other parts of the
14593          front end.  */
14594       push_lang_context (lang_name_cplusplus);
14595       need_lang_pop = true;
14596     }
14597   else
14598     need_lang_pop = false;
14599   /* Let the front end know that we are beginning a specialization.  */
14600   if (!begin_specialization ())
14601     {
14602       end_specialization ();
14603       return;
14604     }
14605
14606   /* If the next keyword is `template', we need to figure out whether
14607      or not we're looking a template-declaration.  */
14608   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14609     {
14610       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14611           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14612         cp_parser_template_declaration_after_export (parser,
14613                                                      /*member_p=*/false);
14614       else
14615         cp_parser_explicit_specialization (parser);
14616     }
14617   else
14618     /* Parse the dependent declaration.  */
14619     cp_parser_single_declaration (parser,
14620                                   /*checks=*/NULL,
14621                                   /*member_p=*/false,
14622                                   /*explicit_specialization_p=*/true,
14623                                   /*friend_p=*/NULL);
14624   /* We're done with the specialization.  */
14625   end_specialization ();
14626   /* For the erroneous case of a template with C linkage, we pushed an
14627      implicit C++ linkage scope; exit that scope now.  */
14628   if (need_lang_pop)
14629     pop_lang_context ();
14630   /* We're done with this parameter list.  */
14631   --parser->num_template_parameter_lists;
14632 }
14633
14634 /* Parse a type-specifier.
14635
14636    type-specifier:
14637      simple-type-specifier
14638      class-specifier
14639      enum-specifier
14640      elaborated-type-specifier
14641      cv-qualifier
14642
14643    GNU Extension:
14644
14645    type-specifier:
14646      __complex__
14647
14648    Returns a representation of the type-specifier.  For a
14649    class-specifier, enum-specifier, or elaborated-type-specifier, a
14650    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14651
14652    The parser flags FLAGS is used to control type-specifier parsing.
14653
14654    If IS_DECLARATION is TRUE, then this type-specifier is appearing
14655    in a decl-specifier-seq.
14656
14657    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14658    class-specifier, enum-specifier, or elaborated-type-specifier, then
14659    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
14660    if a type is declared; 2 if it is defined.  Otherwise, it is set to
14661    zero.
14662
14663    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14664    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
14665    is set to FALSE.  */
14666
14667 static tree
14668 cp_parser_type_specifier (cp_parser* parser,
14669                           cp_parser_flags flags,
14670                           cp_decl_specifier_seq *decl_specs,
14671                           bool is_declaration,
14672                           int* declares_class_or_enum,
14673                           bool* is_cv_qualifier)
14674 {
14675   tree type_spec = NULL_TREE;
14676   cp_token *token;
14677   enum rid keyword;
14678   cp_decl_spec ds = ds_last;
14679
14680   /* Assume this type-specifier does not declare a new type.  */
14681   if (declares_class_or_enum)
14682     *declares_class_or_enum = 0;
14683   /* And that it does not specify a cv-qualifier.  */
14684   if (is_cv_qualifier)
14685     *is_cv_qualifier = false;
14686   /* Peek at the next token.  */
14687   token = cp_lexer_peek_token (parser->lexer);
14688
14689   /* If we're looking at a keyword, we can use that to guide the
14690      production we choose.  */
14691   keyword = token->keyword;
14692   switch (keyword)
14693     {
14694     case RID_ENUM:
14695       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14696         goto elaborated_type_specifier;
14697
14698       /* Look for the enum-specifier.  */
14699       type_spec = cp_parser_enum_specifier (parser);
14700       /* If that worked, we're done.  */
14701       if (type_spec)
14702         {
14703           if (declares_class_or_enum)
14704             *declares_class_or_enum = 2;
14705           if (decl_specs)
14706             cp_parser_set_decl_spec_type (decl_specs,
14707                                           type_spec,
14708                                           token,
14709                                           /*type_definition_p=*/true);
14710           return type_spec;
14711         }
14712       else
14713         goto elaborated_type_specifier;
14714
14715       /* Any of these indicate either a class-specifier, or an
14716          elaborated-type-specifier.  */
14717     case RID_CLASS:
14718     case RID_STRUCT:
14719     case RID_UNION:
14720       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14721         goto elaborated_type_specifier;
14722
14723       /* Parse tentatively so that we can back up if we don't find a
14724          class-specifier.  */
14725       cp_parser_parse_tentatively (parser);
14726       /* Look for the class-specifier.  */
14727       type_spec = cp_parser_class_specifier (parser);
14728       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14729       /* If that worked, we're done.  */
14730       if (cp_parser_parse_definitely (parser))
14731         {
14732           if (declares_class_or_enum)
14733             *declares_class_or_enum = 2;
14734           if (decl_specs)
14735             cp_parser_set_decl_spec_type (decl_specs,
14736                                           type_spec,
14737                                           token,
14738                                           /*type_definition_p=*/true);
14739           return type_spec;
14740         }
14741
14742       /* Fall through.  */
14743     elaborated_type_specifier:
14744       /* We're declaring (not defining) a class or enum.  */
14745       if (declares_class_or_enum)
14746         *declares_class_or_enum = 1;
14747
14748       /* Fall through.  */
14749     case RID_TYPENAME:
14750       /* Look for an elaborated-type-specifier.  */
14751       type_spec
14752         = (cp_parser_elaborated_type_specifier
14753            (parser,
14754             decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14755             is_declaration));
14756       if (decl_specs)
14757         cp_parser_set_decl_spec_type (decl_specs,
14758                                       type_spec,
14759                                       token,
14760                                       /*type_definition_p=*/false);
14761       return type_spec;
14762
14763     case RID_CONST:
14764       ds = ds_const;
14765       if (is_cv_qualifier)
14766         *is_cv_qualifier = true;
14767       break;
14768
14769     case RID_VOLATILE:
14770       ds = ds_volatile;
14771       if (is_cv_qualifier)
14772         *is_cv_qualifier = true;
14773       break;
14774
14775     case RID_RESTRICT:
14776       ds = ds_restrict;
14777       if (is_cv_qualifier)
14778         *is_cv_qualifier = true;
14779       break;
14780
14781     case RID_COMPLEX:
14782       /* The `__complex__' keyword is a GNU extension.  */
14783       ds = ds_complex;
14784       break;
14785
14786     default:
14787       break;
14788     }
14789
14790   /* Handle simple keywords.  */
14791   if (ds != ds_last)
14792     {
14793       if (decl_specs)
14794         {
14795           set_and_check_decl_spec_loc (decl_specs, ds, token);
14796           decl_specs->any_specifiers_p = true;
14797         }
14798       return cp_lexer_consume_token (parser->lexer)->u.value;
14799     }
14800
14801   /* If we do not already have a type-specifier, assume we are looking
14802      at a simple-type-specifier.  */
14803   type_spec = cp_parser_simple_type_specifier (parser,
14804                                                decl_specs,
14805                                                flags);
14806
14807   /* If we didn't find a type-specifier, and a type-specifier was not
14808      optional in this context, issue an error message.  */
14809   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14810     {
14811       cp_parser_error (parser, "expected type specifier");
14812       return error_mark_node;
14813     }
14814
14815   return type_spec;
14816 }
14817
14818 /* Parse a simple-type-specifier.
14819
14820    simple-type-specifier:
14821      :: [opt] nested-name-specifier [opt] type-name
14822      :: [opt] nested-name-specifier template template-id
14823      char
14824      wchar_t
14825      bool
14826      short
14827      int
14828      long
14829      signed
14830      unsigned
14831      float
14832      double
14833      void
14834
14835    C++0x Extension:
14836
14837    simple-type-specifier:
14838      auto
14839      decltype ( expression )   
14840      char16_t
14841      char32_t
14842      __underlying_type ( type-id )
14843
14844    GNU Extension:
14845
14846    simple-type-specifier:
14847      __int128
14848      __typeof__ unary-expression
14849      __typeof__ ( type-id )
14850      __typeof__ ( type-id ) { initializer-list , [opt] }
14851
14852    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
14853    appropriately updated.  */
14854
14855 static tree
14856 cp_parser_simple_type_specifier (cp_parser* parser,
14857                                  cp_decl_specifier_seq *decl_specs,
14858                                  cp_parser_flags flags)
14859 {
14860   tree type = NULL_TREE;
14861   cp_token *token;
14862   int idx;
14863
14864   /* Peek at the next token.  */
14865   token = cp_lexer_peek_token (parser->lexer);
14866
14867   /* If we're looking at a keyword, things are easy.  */
14868   switch (token->keyword)
14869     {
14870     case RID_CHAR:
14871       if (decl_specs)
14872         decl_specs->explicit_char_p = true;
14873       type = char_type_node;
14874       break;
14875     case RID_CHAR16:
14876       type = char16_type_node;
14877       break;
14878     case RID_CHAR32:
14879       type = char32_type_node;
14880       break;
14881     case RID_WCHAR:
14882       type = wchar_type_node;
14883       break;
14884     case RID_BOOL:
14885       type = boolean_type_node;
14886       break;
14887     case RID_SHORT:
14888       set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14889       type = short_integer_type_node;
14890       break;
14891     case RID_INT:
14892       if (decl_specs)
14893         decl_specs->explicit_int_p = true;
14894       type = integer_type_node;
14895       break;
14896     case RID_INT_N_0:
14897     case RID_INT_N_1:
14898     case RID_INT_N_2:
14899     case RID_INT_N_3:
14900       idx = token->keyword - RID_INT_N_0;
14901       if (! int_n_enabled_p [idx])
14902         break;
14903       if (decl_specs)
14904         {
14905           decl_specs->explicit_intN_p = true;
14906           decl_specs->int_n_idx = idx;
14907         }
14908       type = int_n_trees [idx].signed_type;
14909       break;
14910     case RID_LONG:
14911       if (decl_specs)
14912         set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14913       type = long_integer_type_node;
14914       break;
14915     case RID_SIGNED:
14916       set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14917       type = integer_type_node;
14918       break;
14919     case RID_UNSIGNED:
14920       set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14921       type = unsigned_type_node;
14922       break;
14923     case RID_FLOAT:
14924       type = float_type_node;
14925       break;
14926     case RID_DOUBLE:
14927       type = double_type_node;
14928       break;
14929     case RID_VOID:
14930       type = void_type_node;
14931       break;
14932
14933     case RID_AUTO:
14934       maybe_warn_cpp0x (CPP0X_AUTO);
14935       if (parser->auto_is_implicit_function_template_parm_p)
14936         {
14937           /* The 'auto' might be the placeholder return type for a function decl
14938              with trailing return type.  */
14939           bool have_trailing_return_fn_decl = false;
14940           if (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14941               == CPP_OPEN_PAREN)
14942             {
14943               cp_parser_parse_tentatively (parser);
14944               cp_lexer_consume_token (parser->lexer);
14945               cp_lexer_consume_token (parser->lexer);
14946               if (cp_parser_skip_to_closing_parenthesis (parser,
14947                                                          /*recovering*/false,
14948                                                          /*or_comma*/false,
14949                                                          /*consume_paren*/true))
14950                 have_trailing_return_fn_decl
14951                   = cp_lexer_next_token_is (parser->lexer, CPP_DEREF);
14952               cp_parser_abort_tentative_parse (parser);
14953             }
14954
14955           if (have_trailing_return_fn_decl)
14956             {
14957               type = make_auto ();
14958               break;
14959             }
14960
14961           if (cxx_dialect >= cxx14)
14962             type = synthesize_implicit_template_parm (parser);
14963           else
14964             type = error_mark_node;
14965
14966           if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14967             {
14968               if (cxx_dialect < cxx14)
14969                 error_at (token->location,
14970                          "use of %<auto%> in lambda parameter declaration "
14971                          "only available with "
14972                          "-std=c++14 or -std=gnu++14");
14973             }
14974           else if (cxx_dialect < cxx14)
14975             error_at (token->location,
14976                      "use of %<auto%> in parameter declaration "
14977                      "only available with "
14978                      "-std=c++14 or -std=gnu++14");
14979           else
14980             pedwarn (token->location, OPT_Wpedantic,
14981                      "ISO C++ forbids use of %<auto%> in parameter "
14982                      "declaration");
14983         }
14984       else
14985         type = make_auto ();
14986       break;
14987
14988     case RID_DECLTYPE:
14989       /* Since DR 743, decltype can either be a simple-type-specifier by
14990          itself or begin a nested-name-specifier.  Parsing it will replace
14991          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14992          handling below decide what to do.  */
14993       cp_parser_decltype (parser);
14994       cp_lexer_set_token_position (parser->lexer, token);
14995       break;
14996
14997     case RID_TYPEOF:
14998       /* Consume the `typeof' token.  */
14999       cp_lexer_consume_token (parser->lexer);
15000       /* Parse the operand to `typeof'.  */
15001       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
15002       /* If it is not already a TYPE, take its type.  */
15003       if (!TYPE_P (type))
15004         type = finish_typeof (type);
15005
15006       if (decl_specs)
15007         cp_parser_set_decl_spec_type (decl_specs, type,
15008                                       token,
15009                                       /*type_definition_p=*/false);
15010
15011       return type;
15012
15013     case RID_UNDERLYING_TYPE:
15014       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
15015       if (decl_specs)
15016         cp_parser_set_decl_spec_type (decl_specs, type,
15017                                       token,
15018                                       /*type_definition_p=*/false);
15019
15020       return type;
15021
15022     case RID_BASES:
15023     case RID_DIRECT_BASES:
15024       type = cp_parser_trait_expr (parser, token->keyword);
15025       if (decl_specs)
15026        cp_parser_set_decl_spec_type (decl_specs, type,
15027                                      token,
15028                                      /*type_definition_p=*/false);
15029       return type;
15030     default:
15031       break;
15032     }
15033
15034   /* If token is an already-parsed decltype not followed by ::,
15035      it's a simple-type-specifier.  */
15036   if (token->type == CPP_DECLTYPE
15037       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
15038     {
15039       type = token->u.value;
15040       if (decl_specs)
15041         {
15042           cp_parser_set_decl_spec_type (decl_specs, type,
15043                                         token,
15044                                         /*type_definition_p=*/false);
15045           /* Remember that we are handling a decltype in order to
15046              implement the resolution of DR 1510 when the argument
15047              isn't instantiation dependent.  */
15048           decl_specs->decltype_p = true;
15049         }
15050       cp_lexer_consume_token (parser->lexer);
15051       return type;
15052     }
15053
15054   /* If the type-specifier was for a built-in type, we're done.  */
15055   if (type)
15056     {
15057       /* Record the type.  */
15058       if (decl_specs
15059           && (token->keyword != RID_SIGNED
15060               && token->keyword != RID_UNSIGNED
15061               && token->keyword != RID_SHORT
15062               && token->keyword != RID_LONG))
15063         cp_parser_set_decl_spec_type (decl_specs,
15064                                       type,
15065                                       token,
15066                                       /*type_definition_p=*/false);
15067       if (decl_specs)
15068         decl_specs->any_specifiers_p = true;
15069
15070       /* Consume the token.  */
15071       cp_lexer_consume_token (parser->lexer);
15072
15073       if (type == error_mark_node)
15074         return error_mark_node;
15075
15076       /* There is no valid C++ program where a non-template type is
15077          followed by a "<".  That usually indicates that the user thought
15078          that the type was a template.  */
15079       cp_parser_check_for_invalid_template_id (parser, type, none_type,
15080                                                token->location);
15081
15082       return TYPE_NAME (type);
15083     }
15084
15085   /* The type-specifier must be a user-defined type.  */
15086   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
15087     {
15088       bool qualified_p;
15089       bool global_p;
15090
15091       /* Don't gobble tokens or issue error messages if this is an
15092          optional type-specifier.  */
15093       if (flags & CP_PARSER_FLAGS_OPTIONAL)
15094         cp_parser_parse_tentatively (parser);
15095
15096       /* Look for the optional `::' operator.  */
15097       global_p
15098         = (cp_parser_global_scope_opt (parser,
15099                                        /*current_scope_valid_p=*/false)
15100            != NULL_TREE);
15101       /* Look for the nested-name specifier.  */
15102       qualified_p
15103         = (cp_parser_nested_name_specifier_opt (parser,
15104                                                 /*typename_keyword_p=*/false,
15105                                                 /*check_dependency_p=*/true,
15106                                                 /*type_p=*/false,
15107                                                 /*is_declaration=*/false)
15108            != NULL_TREE);
15109       token = cp_lexer_peek_token (parser->lexer);
15110       /* If we have seen a nested-name-specifier, and the next token
15111          is `template', then we are using the template-id production.  */
15112       if (parser->scope
15113           && cp_parser_optional_template_keyword (parser))
15114         {
15115           /* Look for the template-id.  */
15116           type = cp_parser_template_id (parser,
15117                                         /*template_keyword_p=*/true,
15118                                         /*check_dependency_p=*/true,
15119                                         none_type,
15120                                         /*is_declaration=*/false);
15121           /* If the template-id did not name a type, we are out of
15122              luck.  */
15123           if (TREE_CODE (type) != TYPE_DECL)
15124             {
15125               cp_parser_error (parser, "expected template-id for type");
15126               type = NULL_TREE;
15127             }
15128         }
15129       /* Otherwise, look for a type-name.  */
15130       else
15131         type = cp_parser_type_name (parser);
15132       /* Keep track of all name-lookups performed in class scopes.  */
15133       if (type
15134           && !global_p
15135           && !qualified_p
15136           && TREE_CODE (type) == TYPE_DECL
15137           && identifier_p (DECL_NAME (type)))
15138         maybe_note_name_used_in_class (DECL_NAME (type), type);
15139       /* If it didn't work out, we don't have a TYPE.  */
15140       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
15141           && !cp_parser_parse_definitely (parser))
15142         type = NULL_TREE;
15143       if (type && decl_specs)
15144         cp_parser_set_decl_spec_type (decl_specs, type,
15145                                       token,
15146                                       /*type_definition_p=*/false);
15147     }
15148
15149   /* If we didn't get a type-name, issue an error message.  */
15150   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15151     {
15152       cp_parser_error (parser, "expected type-name");
15153       return error_mark_node;
15154     }
15155
15156   if (type && type != error_mark_node)
15157     {
15158       /* See if TYPE is an Objective-C type, and if so, parse and
15159          accept any protocol references following it.  Do this before
15160          the cp_parser_check_for_invalid_template_id() call, because
15161          Objective-C types can be followed by '<...>' which would
15162          enclose protocol names rather than template arguments, and so
15163          everything is fine.  */
15164       if (c_dialect_objc () && !parser->scope
15165           && (objc_is_id (type) || objc_is_class_name (type)))
15166         {
15167           tree protos = cp_parser_objc_protocol_refs_opt (parser);
15168           tree qual_type = objc_get_protocol_qualified_type (type, protos);
15169
15170           /* Clobber the "unqualified" type previously entered into
15171              DECL_SPECS with the new, improved protocol-qualified version.  */
15172           if (decl_specs)
15173             decl_specs->type = qual_type;
15174
15175           return qual_type;
15176         }
15177
15178       /* There is no valid C++ program where a non-template type is
15179          followed by a "<".  That usually indicates that the user
15180          thought that the type was a template.  */
15181       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
15182                                                none_type,
15183                                                token->location);
15184     }
15185
15186   return type;
15187 }
15188
15189 /* Parse a type-name.
15190
15191    type-name:
15192      class-name
15193      enum-name
15194      typedef-name
15195      simple-template-id [in c++0x]
15196
15197    enum-name:
15198      identifier
15199
15200    typedef-name:
15201      identifier
15202
15203    Returns a TYPE_DECL for the type.  */
15204
15205 static tree
15206 cp_parser_type_name (cp_parser* parser)
15207 {
15208   tree type_decl;
15209
15210   /* We can't know yet whether it is a class-name or not.  */
15211   cp_parser_parse_tentatively (parser);
15212   /* Try a class-name.  */
15213   type_decl = cp_parser_class_name (parser,
15214                                     /*typename_keyword_p=*/false,
15215                                     /*template_keyword_p=*/false,
15216                                     none_type,
15217                                     /*check_dependency_p=*/true,
15218                                     /*class_head_p=*/false,
15219                                     /*is_declaration=*/false);
15220   /* If it's not a class-name, keep looking.  */
15221   if (!cp_parser_parse_definitely (parser))
15222     {
15223       if (cxx_dialect < cxx11)
15224         /* It must be a typedef-name or an enum-name.  */
15225         return cp_parser_nonclass_name (parser);
15226
15227       cp_parser_parse_tentatively (parser);
15228       /* It is either a simple-template-id representing an
15229          instantiation of an alias template...  */
15230       type_decl = cp_parser_template_id (parser,
15231                                          /*template_keyword_p=*/false,
15232                                          /*check_dependency_p=*/true,
15233                                          none_type,
15234                                          /*is_declaration=*/false);
15235       /* Note that this must be an instantiation of an alias template
15236          because [temp.names]/6 says:
15237          
15238              A template-id that names an alias template specialization
15239              is a type-name.
15240
15241          Whereas [temp.names]/7 says:
15242          
15243              A simple-template-id that names a class template
15244              specialization is a class-name.  */
15245       if (type_decl != NULL_TREE
15246           && TREE_CODE (type_decl) == TYPE_DECL
15247           && TYPE_DECL_ALIAS_P (type_decl))
15248         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15249       else
15250         cp_parser_simulate_error (parser);
15251
15252       if (!cp_parser_parse_definitely (parser))
15253         /* ... Or a typedef-name or an enum-name.  */
15254         return cp_parser_nonclass_name (parser);
15255     }
15256
15257   return type_decl;
15258 }
15259
15260 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15261
15262    enum-name:
15263      identifier
15264
15265    typedef-name:
15266      identifier
15267
15268    Returns a TYPE_DECL for the type.  */
15269
15270 static tree
15271 cp_parser_nonclass_name (cp_parser* parser)
15272 {
15273   tree type_decl;
15274   tree identifier;
15275
15276   cp_token *token = cp_lexer_peek_token (parser->lexer);
15277   identifier = cp_parser_identifier (parser);
15278   if (identifier == error_mark_node)
15279     return error_mark_node;
15280
15281   /* Look up the type-name.  */
15282   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15283
15284   type_decl = strip_using_decl (type_decl);
15285   
15286   if (TREE_CODE (type_decl) != TYPE_DECL
15287       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15288     {
15289       /* See if this is an Objective-C type.  */
15290       tree protos = cp_parser_objc_protocol_refs_opt (parser);
15291       tree type = objc_get_protocol_qualified_type (identifier, protos);
15292       if (type)
15293         type_decl = TYPE_NAME (type);
15294     }
15295
15296   /* Issue an error if we did not find a type-name.  */
15297   if (TREE_CODE (type_decl) != TYPE_DECL
15298       /* In Objective-C, we have the complication that class names are
15299          normally type names and start declarations (eg, the
15300          "NSObject" in "NSObject *object;"), but can be used in an
15301          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15302          is an expression.  So, a classname followed by a dot is not a
15303          valid type-name.  */
15304       || (objc_is_class_name (TREE_TYPE (type_decl))
15305           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15306     {
15307       if (!cp_parser_simulate_error (parser))
15308         cp_parser_name_lookup_error (parser, identifier, type_decl,
15309                                      NLE_TYPE, token->location);
15310       return error_mark_node;
15311     }
15312   /* Remember that the name was used in the definition of the
15313      current class so that we can check later to see if the
15314      meaning would have been different after the class was
15315      entirely defined.  */
15316   else if (type_decl != error_mark_node
15317            && !parser->scope)
15318     maybe_note_name_used_in_class (identifier, type_decl);
15319   
15320   return type_decl;
15321 }
15322
15323 /* Parse an elaborated-type-specifier.  Note that the grammar given
15324    here incorporates the resolution to DR68.
15325
15326    elaborated-type-specifier:
15327      class-key :: [opt] nested-name-specifier [opt] identifier
15328      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15329      enum-key :: [opt] nested-name-specifier [opt] identifier
15330      typename :: [opt] nested-name-specifier identifier
15331      typename :: [opt] nested-name-specifier template [opt]
15332        template-id
15333
15334    GNU extension:
15335
15336    elaborated-type-specifier:
15337      class-key attributes :: [opt] nested-name-specifier [opt] identifier
15338      class-key attributes :: [opt] nested-name-specifier [opt]
15339                template [opt] template-id
15340      enum attributes :: [opt] nested-name-specifier [opt] identifier
15341
15342    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15343    declared `friend'.  If IS_DECLARATION is TRUE, then this
15344    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15345    something is being declared.
15346
15347    Returns the TYPE specified.  */
15348
15349 static tree
15350 cp_parser_elaborated_type_specifier (cp_parser* parser,
15351                                      bool is_friend,
15352                                      bool is_declaration)
15353 {
15354   enum tag_types tag_type;
15355   tree identifier;
15356   tree type = NULL_TREE;
15357   tree attributes = NULL_TREE;
15358   tree globalscope;
15359   cp_token *token = NULL;
15360
15361   /* See if we're looking at the `enum' keyword.  */
15362   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15363     {
15364       /* Consume the `enum' token.  */
15365       cp_lexer_consume_token (parser->lexer);
15366       /* Remember that it's an enumeration type.  */
15367       tag_type = enum_type;
15368       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15369          enums) is used here.  */
15370       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15371           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15372         {
15373             pedwarn (input_location, 0, "elaborated-type-specifier "
15374                       "for a scoped enum must not use the %<%D%> keyword",
15375                       cp_lexer_peek_token (parser->lexer)->u.value);
15376           /* Consume the `struct' or `class' and parse it anyway.  */
15377           cp_lexer_consume_token (parser->lexer);
15378         }
15379       /* Parse the attributes.  */
15380       attributes = cp_parser_attributes_opt (parser);
15381     }
15382   /* Or, it might be `typename'.  */
15383   else if (cp_lexer_next_token_is_keyword (parser->lexer,
15384                                            RID_TYPENAME))
15385     {
15386       /* Consume the `typename' token.  */
15387       cp_lexer_consume_token (parser->lexer);
15388       /* Remember that it's a `typename' type.  */
15389       tag_type = typename_type;
15390     }
15391   /* Otherwise it must be a class-key.  */
15392   else
15393     {
15394       tag_type = cp_parser_class_key (parser);
15395       if (tag_type == none_type)
15396         return error_mark_node;
15397       /* Parse the attributes.  */
15398       attributes = cp_parser_attributes_opt (parser);
15399     }
15400
15401   /* Look for the `::' operator.  */
15402   globalscope =  cp_parser_global_scope_opt (parser,
15403                                              /*current_scope_valid_p=*/false);
15404   /* Look for the nested-name-specifier.  */
15405   if (tag_type == typename_type && !globalscope)
15406     {
15407       if (!cp_parser_nested_name_specifier (parser,
15408                                            /*typename_keyword_p=*/true,
15409                                            /*check_dependency_p=*/true,
15410                                            /*type_p=*/true,
15411                                             is_declaration))
15412         return error_mark_node;
15413     }
15414   else
15415     /* Even though `typename' is not present, the proposed resolution
15416        to Core Issue 180 says that in `class A<T>::B', `B' should be
15417        considered a type-name, even if `A<T>' is dependent.  */
15418     cp_parser_nested_name_specifier_opt (parser,
15419                                          /*typename_keyword_p=*/true,
15420                                          /*check_dependency_p=*/true,
15421                                          /*type_p=*/true,
15422                                          is_declaration);
15423  /* For everything but enumeration types, consider a template-id.
15424     For an enumeration type, consider only a plain identifier.  */
15425   if (tag_type != enum_type)
15426     {
15427       bool template_p = false;
15428       tree decl;
15429
15430       /* Allow the `template' keyword.  */
15431       template_p = cp_parser_optional_template_keyword (parser);
15432       /* If we didn't see `template', we don't know if there's a
15433          template-id or not.  */
15434       if (!template_p)
15435         cp_parser_parse_tentatively (parser);
15436       /* Parse the template-id.  */
15437       token = cp_lexer_peek_token (parser->lexer);
15438       decl = cp_parser_template_id (parser, template_p,
15439                                     /*check_dependency_p=*/true,
15440                                     tag_type,
15441                                     is_declaration);
15442       /* If we didn't find a template-id, look for an ordinary
15443          identifier.  */
15444       if (!template_p && !cp_parser_parse_definitely (parser))
15445         ;
15446       /* We can get here when cp_parser_template_id, called by
15447          cp_parser_class_name with tag_type == none_type, succeeds
15448          and caches a BASELINK.  Then, when called again here,
15449          instead of failing and returning an error_mark_node
15450          returns it (see template/typename17.C in C++11).
15451          ??? Could we diagnose this earlier?  */
15452       else if (tag_type == typename_type && BASELINK_P (decl))
15453         {
15454           cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15455           type = error_mark_node;
15456         }
15457       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15458          in effect, then we must assume that, upon instantiation, the
15459          template will correspond to a class.  */
15460       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15461                && tag_type == typename_type)
15462         type = make_typename_type (parser->scope, decl,
15463                                    typename_type,
15464                                    /*complain=*/tf_error);
15465       /* If the `typename' keyword is in effect and DECL is not a type
15466          decl, then type is non existent.   */
15467       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15468         ; 
15469       else if (TREE_CODE (decl) == TYPE_DECL)
15470         type = check_elaborated_type_specifier (tag_type, decl,
15471                                                 /*allow_template_p=*/true);
15472       else if (decl == error_mark_node)
15473         type = error_mark_node; 
15474     }
15475
15476   if (!type)
15477     {
15478       token = cp_lexer_peek_token (parser->lexer);
15479       identifier = cp_parser_identifier (parser);
15480
15481       if (identifier == error_mark_node)
15482         {
15483           parser->scope = NULL_TREE;
15484           return error_mark_node;
15485         }
15486
15487       /* For a `typename', we needn't call xref_tag.  */
15488       if (tag_type == typename_type
15489           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15490         return cp_parser_make_typename_type (parser, identifier,
15491                                              token->location);
15492
15493       /* Template parameter lists apply only if we are not within a
15494          function parameter list.  */
15495       bool template_parm_lists_apply
15496           = parser->num_template_parameter_lists;
15497       if (template_parm_lists_apply)
15498         for (cp_binding_level *s = current_binding_level;
15499              s && s->kind != sk_template_parms;
15500              s = s->level_chain)
15501           if (s->kind == sk_function_parms)
15502             template_parm_lists_apply = false;
15503
15504       /* Look up a qualified name in the usual way.  */
15505       if (parser->scope)
15506         {
15507           tree decl;
15508           tree ambiguous_decls;
15509
15510           decl = cp_parser_lookup_name (parser, identifier,
15511                                         tag_type,
15512                                         /*is_template=*/false,
15513                                         /*is_namespace=*/false,
15514                                         /*check_dependency=*/true,
15515                                         &ambiguous_decls,
15516                                         token->location);
15517
15518           /* If the lookup was ambiguous, an error will already have been
15519              issued.  */
15520           if (ambiguous_decls)
15521             return error_mark_node;
15522
15523           /* If we are parsing friend declaration, DECL may be a
15524              TEMPLATE_DECL tree node here.  However, we need to check
15525              whether this TEMPLATE_DECL results in valid code.  Consider
15526              the following example:
15527
15528                namespace N {
15529                  template <class T> class C {};
15530                }
15531                class X {
15532                  template <class T> friend class N::C; // #1, valid code
15533                };
15534                template <class T> class Y {
15535                  friend class N::C;                    // #2, invalid code
15536                };
15537
15538              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15539              name lookup of `N::C'.  We see that friend declaration must
15540              be template for the code to be valid.  Note that
15541              processing_template_decl does not work here since it is
15542              always 1 for the above two cases.  */
15543
15544           decl = (cp_parser_maybe_treat_template_as_class
15545                   (decl, /*tag_name_p=*/is_friend
15546                          && template_parm_lists_apply));
15547
15548           if (TREE_CODE (decl) != TYPE_DECL)
15549             {
15550               cp_parser_diagnose_invalid_type_name (parser,
15551                                                     identifier,
15552                                                     token->location);
15553               return error_mark_node;
15554             }
15555
15556           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15557             {
15558               bool allow_template = (template_parm_lists_apply
15559                                      || DECL_SELF_REFERENCE_P (decl));
15560               type = check_elaborated_type_specifier (tag_type, decl,
15561                                                       allow_template);
15562
15563               if (type == error_mark_node)
15564                 return error_mark_node;
15565             }
15566
15567           /* Forward declarations of nested types, such as
15568
15569                class C1::C2;
15570                class C1::C2::C3;
15571
15572              are invalid unless all components preceding the final '::'
15573              are complete.  If all enclosing types are complete, these
15574              declarations become merely pointless.
15575
15576              Invalid forward declarations of nested types are errors
15577              caught elsewhere in parsing.  Those that are pointless arrive
15578              here.  */
15579
15580           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15581               && !is_friend && !processing_explicit_instantiation)
15582             warning (0, "declaration %qD does not declare anything", decl);
15583
15584           type = TREE_TYPE (decl);
15585         }
15586       else
15587         {
15588           /* An elaborated-type-specifier sometimes introduces a new type and
15589              sometimes names an existing type.  Normally, the rule is that it
15590              introduces a new type only if there is not an existing type of
15591              the same name already in scope.  For example, given:
15592
15593                struct S {};
15594                void f() { struct S s; }
15595
15596              the `struct S' in the body of `f' is the same `struct S' as in
15597              the global scope; the existing definition is used.  However, if
15598              there were no global declaration, this would introduce a new
15599              local class named `S'.
15600
15601              An exception to this rule applies to the following code:
15602
15603                namespace N { struct S; }
15604
15605              Here, the elaborated-type-specifier names a new type
15606              unconditionally; even if there is already an `S' in the
15607              containing scope this declaration names a new type.
15608              This exception only applies if the elaborated-type-specifier
15609              forms the complete declaration:
15610
15611                [class.name]
15612
15613                A declaration consisting solely of `class-key identifier ;' is
15614                either a redeclaration of the name in the current scope or a
15615                forward declaration of the identifier as a class name.  It
15616                introduces the name into the current scope.
15617
15618              We are in this situation precisely when the next token is a `;'.
15619
15620              An exception to the exception is that a `friend' declaration does
15621              *not* name a new type; i.e., given:
15622
15623                struct S { friend struct T; };
15624
15625              `T' is not a new type in the scope of `S'.
15626
15627              Also, `new struct S' or `sizeof (struct S)' never results in the
15628              definition of a new type; a new type can only be declared in a
15629              declaration context.  */
15630
15631           tag_scope ts;
15632           bool template_p;
15633
15634           if (is_friend)
15635             /* Friends have special name lookup rules.  */
15636             ts = ts_within_enclosing_non_class;
15637           else if (is_declaration
15638                    && cp_lexer_next_token_is (parser->lexer,
15639                                               CPP_SEMICOLON))
15640             /* This is a `class-key identifier ;' */
15641             ts = ts_current;
15642           else
15643             ts = ts_global;
15644
15645           template_p =
15646             (template_parm_lists_apply
15647              && (cp_parser_next_token_starts_class_definition_p (parser)
15648                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15649           /* An unqualified name was used to reference this type, so
15650              there were no qualifying templates.  */
15651           if (template_parm_lists_apply
15652               && !cp_parser_check_template_parameters (parser,
15653                                                        /*num_templates=*/0,
15654                                                        token->location,
15655                                                        /*declarator=*/NULL))
15656             return error_mark_node;
15657           type = xref_tag (tag_type, identifier, ts, template_p);
15658         }
15659     }
15660
15661   if (type == error_mark_node)
15662     return error_mark_node;
15663
15664   /* Allow attributes on forward declarations of classes.  */
15665   if (attributes)
15666     {
15667       if (TREE_CODE (type) == TYPENAME_TYPE)
15668         warning (OPT_Wattributes,
15669                  "attributes ignored on uninstantiated type");
15670       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15671                && ! processing_explicit_instantiation)
15672         warning (OPT_Wattributes,
15673                  "attributes ignored on template instantiation");
15674       else if (is_declaration && cp_parser_declares_only_class_p (parser))
15675         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15676       else
15677         warning (OPT_Wattributes,
15678                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15679     }
15680
15681   if (tag_type != enum_type)
15682     {
15683       /* Indicate whether this class was declared as a `class' or as a
15684          `struct'.  */
15685       if (CLASS_TYPE_P (type))
15686         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15687       cp_parser_check_class_key (tag_type, type);
15688     }
15689
15690   /* A "<" cannot follow an elaborated type specifier.  If that
15691      happens, the user was probably trying to form a template-id.  */
15692   cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15693                                            token->location);
15694
15695   return type;
15696 }
15697
15698 /* Parse an enum-specifier.
15699
15700    enum-specifier:
15701      enum-head { enumerator-list [opt] }
15702      enum-head { enumerator-list , } [C++0x]
15703
15704    enum-head:
15705      enum-key identifier [opt] enum-base [opt]
15706      enum-key nested-name-specifier identifier enum-base [opt]
15707
15708    enum-key:
15709      enum
15710      enum class   [C++0x]
15711      enum struct  [C++0x]
15712
15713    enum-base:   [C++0x]
15714      : type-specifier-seq
15715
15716    opaque-enum-specifier:
15717      enum-key identifier enum-base [opt] ;
15718
15719    GNU Extensions:
15720      enum-key attributes[opt] identifier [opt] enum-base [opt] 
15721        { enumerator-list [opt] }attributes[opt]
15722      enum-key attributes[opt] identifier [opt] enum-base [opt]
15723        { enumerator-list, }attributes[opt] [C++0x]
15724
15725    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15726    if the token stream isn't an enum-specifier after all.  */
15727
15728 static tree
15729 cp_parser_enum_specifier (cp_parser* parser)
15730 {
15731   tree identifier;
15732   tree type = NULL_TREE;
15733   tree prev_scope;
15734   tree nested_name_specifier = NULL_TREE;
15735   tree attributes;
15736   bool scoped_enum_p = false;
15737   bool has_underlying_type = false;
15738   bool nested_being_defined = false;
15739   bool new_value_list = false;
15740   bool is_new_type = false;
15741   bool is_anonymous = false;
15742   tree underlying_type = NULL_TREE;
15743   cp_token *type_start_token = NULL;
15744   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15745
15746   parser->colon_corrects_to_scope_p = false;
15747
15748   /* Parse tentatively so that we can back up if we don't find a
15749      enum-specifier.  */
15750   cp_parser_parse_tentatively (parser);
15751
15752   /* Caller guarantees that the current token is 'enum', an identifier
15753      possibly follows, and the token after that is an opening brace.
15754      If we don't have an identifier, fabricate an anonymous name for
15755      the enumeration being defined.  */
15756   cp_lexer_consume_token (parser->lexer);
15757
15758   /* Parse the "class" or "struct", which indicates a scoped
15759      enumeration type in C++0x.  */
15760   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15761       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15762     {
15763       if (cxx_dialect < cxx11)
15764         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15765
15766       /* Consume the `struct' or `class' token.  */
15767       cp_lexer_consume_token (parser->lexer);
15768
15769       scoped_enum_p = true;
15770     }
15771
15772   attributes = cp_parser_attributes_opt (parser);
15773
15774   /* Clear the qualification.  */
15775   parser->scope = NULL_TREE;
15776   parser->qualifying_scope = NULL_TREE;
15777   parser->object_scope = NULL_TREE;
15778
15779   /* Figure out in what scope the declaration is being placed.  */
15780   prev_scope = current_scope ();
15781
15782   type_start_token = cp_lexer_peek_token (parser->lexer);
15783
15784   push_deferring_access_checks (dk_no_check);
15785   nested_name_specifier
15786       = cp_parser_nested_name_specifier_opt (parser,
15787                                              /*typename_keyword_p=*/true,
15788                                              /*check_dependency_p=*/false,
15789                                              /*type_p=*/false,
15790                                              /*is_declaration=*/false);
15791
15792   if (nested_name_specifier)
15793     {
15794       tree name;
15795
15796       identifier = cp_parser_identifier (parser);
15797       name =  cp_parser_lookup_name (parser, identifier,
15798                                      enum_type,
15799                                      /*is_template=*/false,
15800                                      /*is_namespace=*/false,
15801                                      /*check_dependency=*/true,
15802                                      /*ambiguous_decls=*/NULL,
15803                                      input_location);
15804       if (name && name != error_mark_node)
15805         {
15806           type = TREE_TYPE (name);
15807           if (TREE_CODE (type) == TYPENAME_TYPE)
15808             {
15809               /* Are template enums allowed in ISO? */
15810               if (template_parm_scope_p ())
15811                 pedwarn (type_start_token->location, OPT_Wpedantic,
15812                          "%qD is an enumeration template", name);
15813               /* ignore a typename reference, for it will be solved by name
15814                  in start_enum.  */
15815               type = NULL_TREE;
15816             }
15817         }
15818       else if (nested_name_specifier == error_mark_node)
15819         /* We already issued an error.  */;
15820       else
15821         error_at (type_start_token->location,
15822                   "%qD is not an enumerator-name", identifier);
15823     }
15824   else
15825     {
15826       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15827         identifier = cp_parser_identifier (parser);
15828       else
15829         {
15830           identifier = make_anon_name ();
15831           is_anonymous = true;
15832           if (scoped_enum_p)
15833             error_at (type_start_token->location,
15834                       "anonymous scoped enum is not allowed");
15835         }
15836     }
15837   pop_deferring_access_checks ();
15838
15839   /* Check for the `:' that denotes a specified underlying type in C++0x.
15840      Note that a ':' could also indicate a bitfield width, however.  */
15841   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15842     {
15843       cp_decl_specifier_seq type_specifiers;
15844
15845       /* Consume the `:'.  */
15846       cp_lexer_consume_token (parser->lexer);
15847
15848       /* Parse the type-specifier-seq.  */
15849       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15850                                     /*is_trailing_return=*/false,
15851                                     &type_specifiers);
15852
15853       /* At this point this is surely not elaborated type specifier.  */
15854       if (!cp_parser_parse_definitely (parser))
15855         return NULL_TREE;
15856
15857       if (cxx_dialect < cxx11)
15858         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15859
15860       has_underlying_type = true;
15861
15862       /* If that didn't work, stop.  */
15863       if (type_specifiers.type != error_mark_node)
15864         {
15865           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15866                                             /*initialized=*/0, NULL);
15867           if (underlying_type == error_mark_node
15868               || check_for_bare_parameter_packs (underlying_type))
15869             underlying_type = NULL_TREE;
15870         }
15871     }
15872
15873   /* Look for the `{' but don't consume it yet.  */
15874   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15875     {
15876       if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15877         {
15878           cp_parser_error (parser, "expected %<{%>");
15879           if (has_underlying_type)
15880             {
15881               type = NULL_TREE;
15882               goto out;
15883             }
15884         }
15885       /* An opaque-enum-specifier must have a ';' here.  */
15886       if ((scoped_enum_p || underlying_type)
15887           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15888         {
15889           cp_parser_error (parser, "expected %<;%> or %<{%>");
15890           if (has_underlying_type)
15891             {
15892               type = NULL_TREE;
15893               goto out;
15894             }
15895         }
15896     }
15897
15898   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15899     return NULL_TREE;
15900
15901   if (nested_name_specifier)
15902     {
15903       if (CLASS_TYPE_P (nested_name_specifier))
15904         {
15905           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15906           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15907           push_scope (nested_name_specifier);
15908         }
15909       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15910         {
15911           push_nested_namespace (nested_name_specifier);
15912         }
15913     }
15914
15915   /* Issue an error message if type-definitions are forbidden here.  */
15916   if (!cp_parser_check_type_definition (parser))
15917     type = error_mark_node;
15918   else
15919     /* Create the new type.  We do this before consuming the opening
15920        brace so the enum will be recorded as being on the line of its
15921        tag (or the 'enum' keyword, if there is no tag).  */
15922     type = start_enum (identifier, type, underlying_type,
15923                        scoped_enum_p, &is_new_type);
15924
15925   /* If the next token is not '{' it is an opaque-enum-specifier or an
15926      elaborated-type-specifier.  */
15927   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15928     {
15929       timevar_push (TV_PARSE_ENUM);
15930       if (nested_name_specifier
15931           && nested_name_specifier != error_mark_node)
15932         {
15933           /* The following catches invalid code such as:
15934              enum class S<int>::E { A, B, C }; */
15935           if (!processing_specialization
15936               && CLASS_TYPE_P (nested_name_specifier)
15937               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15938             error_at (type_start_token->location, "cannot add an enumerator "
15939                       "list to a template instantiation");
15940
15941           if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15942             {
15943               error_at (type_start_token->location,
15944                         "%<%T::%E%> has not been declared",
15945                         TYPE_CONTEXT (nested_name_specifier),
15946                         nested_name_specifier);
15947               type = error_mark_node;
15948             }
15949           /* If that scope does not contain the scope in which the
15950              class was originally declared, the program is invalid.  */
15951           else if (prev_scope && !is_ancestor (prev_scope,
15952                                                nested_name_specifier))
15953             {
15954               if (at_namespace_scope_p ())
15955                 error_at (type_start_token->location,
15956                           "declaration of %qD in namespace %qD which does not "
15957                           "enclose %qD",
15958                           type, prev_scope, nested_name_specifier);
15959               else
15960                 error_at (type_start_token->location,
15961                           "declaration of %qD in %qD which does not "
15962                           "enclose %qD",
15963                           type, prev_scope, nested_name_specifier);
15964               type = error_mark_node;
15965             }
15966         }
15967
15968       if (scoped_enum_p)
15969         begin_scope (sk_scoped_enum, type);
15970
15971       /* Consume the opening brace.  */
15972       cp_lexer_consume_token (parser->lexer);
15973
15974       if (type == error_mark_node)
15975         ; /* Nothing to add */
15976       else if (OPAQUE_ENUM_P (type)
15977                || (cxx_dialect > cxx98 && processing_specialization))
15978         {
15979           new_value_list = true;
15980           SET_OPAQUE_ENUM_P (type, false);
15981           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15982         }
15983       else
15984         {
15985           error_at (type_start_token->location,
15986                     "multiple definition of %q#T", type);
15987           inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15988                   "previous definition here");
15989           type = error_mark_node;
15990         }
15991
15992       if (type == error_mark_node)
15993         cp_parser_skip_to_end_of_block_or_statement (parser);
15994       /* If the next token is not '}', then there are some enumerators.  */
15995       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15996         {
15997           if (is_anonymous && !scoped_enum_p)
15998             pedwarn (type_start_token->location, OPT_Wpedantic,
15999                      "ISO C++ forbids empty anonymous enum");
16000         }
16001       else
16002         cp_parser_enumerator_list (parser, type);
16003
16004       /* Consume the final '}'.  */
16005       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16006
16007       if (scoped_enum_p)
16008         finish_scope ();
16009       timevar_pop (TV_PARSE_ENUM);
16010     }
16011   else
16012     {
16013       /* If a ';' follows, then it is an opaque-enum-specifier
16014         and additional restrictions apply.  */
16015       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16016         {
16017           if (is_anonymous)
16018             error_at (type_start_token->location,
16019                       "opaque-enum-specifier without name");
16020           else if (nested_name_specifier)
16021             error_at (type_start_token->location,
16022                       "opaque-enum-specifier must use a simple identifier");
16023         }
16024     }
16025
16026   /* Look for trailing attributes to apply to this enumeration, and
16027      apply them if appropriate.  */
16028   if (cp_parser_allow_gnu_extensions_p (parser))
16029     {
16030       tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
16031       trailing_attr = chainon (trailing_attr, attributes);
16032       cplus_decl_attributes (&type,
16033                              trailing_attr,
16034                              (int) ATTR_FLAG_TYPE_IN_PLACE);
16035     }
16036
16037   /* Finish up the enumeration.  */
16038   if (type != error_mark_node)
16039     {
16040       if (new_value_list)
16041         finish_enum_value_list (type);
16042       if (is_new_type)
16043         finish_enum (type);
16044     }
16045
16046   if (nested_name_specifier)
16047     {
16048       if (CLASS_TYPE_P (nested_name_specifier))
16049         {
16050           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
16051           pop_scope (nested_name_specifier);
16052         }
16053       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
16054         {
16055           pop_nested_namespace (nested_name_specifier);
16056         }
16057     }
16058  out:
16059   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
16060   return type;
16061 }
16062
16063 /* Parse an enumerator-list.  The enumerators all have the indicated
16064    TYPE.
16065
16066    enumerator-list:
16067      enumerator-definition
16068      enumerator-list , enumerator-definition  */
16069
16070 static void
16071 cp_parser_enumerator_list (cp_parser* parser, tree type)
16072 {
16073   while (true)
16074     {
16075       /* Parse an enumerator-definition.  */
16076       cp_parser_enumerator_definition (parser, type);
16077
16078       /* If the next token is not a ',', we've reached the end of
16079          the list.  */
16080       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16081         break;
16082       /* Otherwise, consume the `,' and keep going.  */
16083       cp_lexer_consume_token (parser->lexer);
16084       /* If the next token is a `}', there is a trailing comma.  */
16085       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16086         {
16087           if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
16088             pedwarn (input_location, OPT_Wpedantic,
16089                      "comma at end of enumerator list");
16090           break;
16091         }
16092     }
16093 }
16094
16095 /* Parse an enumerator-definition.  The enumerator has the indicated
16096    TYPE.
16097
16098    enumerator-definition:
16099      enumerator
16100      enumerator = constant-expression
16101
16102    enumerator:
16103      identifier  */
16104
16105 static void
16106 cp_parser_enumerator_definition (cp_parser* parser, tree type)
16107 {
16108   tree identifier;
16109   tree value;
16110   location_t loc;
16111
16112   /* Save the input location because we are interested in the location
16113      of the identifier and not the location of the explicit value.  */
16114   loc = cp_lexer_peek_token (parser->lexer)->location;
16115
16116   /* Look for the identifier.  */
16117   identifier = cp_parser_identifier (parser);
16118   if (identifier == error_mark_node)
16119     return;
16120
16121   /* If the next token is an '=', then there is an explicit value.  */
16122   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16123     {
16124       /* Consume the `=' token.  */
16125       cp_lexer_consume_token (parser->lexer);
16126       /* Parse the value.  */
16127       value = cp_parser_constant_expression (parser);
16128     }
16129   else
16130     value = NULL_TREE;
16131
16132   /* If we are processing a template, make sure the initializer of the
16133      enumerator doesn't contain any bare template parameter pack.  */
16134   if (check_for_bare_parameter_packs (value))
16135     value = error_mark_node;
16136
16137   /* Create the enumerator.  */
16138   build_enumerator (identifier, value, type, loc);
16139 }
16140
16141 /* Parse a namespace-name.
16142
16143    namespace-name:
16144      original-namespace-name
16145      namespace-alias
16146
16147    Returns the NAMESPACE_DECL for the namespace.  */
16148
16149 static tree
16150 cp_parser_namespace_name (cp_parser* parser)
16151 {
16152   tree identifier;
16153   tree namespace_decl;
16154
16155   cp_token *token = cp_lexer_peek_token (parser->lexer);
16156
16157   /* Get the name of the namespace.  */
16158   identifier = cp_parser_identifier (parser);
16159   if (identifier == error_mark_node)
16160     return error_mark_node;
16161
16162   /* Look up the identifier in the currently active scope.  Look only
16163      for namespaces, due to:
16164
16165        [basic.lookup.udir]
16166
16167        When looking up a namespace-name in a using-directive or alias
16168        definition, only namespace names are considered.
16169
16170      And:
16171
16172        [basic.lookup.qual]
16173
16174        During the lookup of a name preceding the :: scope resolution
16175        operator, object, function, and enumerator names are ignored.
16176
16177      (Note that cp_parser_qualifying_entity only calls this
16178      function if the token after the name is the scope resolution
16179      operator.)  */
16180   namespace_decl = cp_parser_lookup_name (parser, identifier,
16181                                           none_type,
16182                                           /*is_template=*/false,
16183                                           /*is_namespace=*/true,
16184                                           /*check_dependency=*/true,
16185                                           /*ambiguous_decls=*/NULL,
16186                                           token->location);
16187   /* If it's not a namespace, issue an error.  */
16188   if (namespace_decl == error_mark_node
16189       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
16190     {
16191       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16192         error_at (token->location, "%qD is not a namespace-name", identifier);
16193       cp_parser_error (parser, "expected namespace-name");
16194       namespace_decl = error_mark_node;
16195     }
16196
16197   return namespace_decl;
16198 }
16199
16200 /* Parse a namespace-definition.
16201
16202    namespace-definition:
16203      named-namespace-definition
16204      unnamed-namespace-definition
16205
16206    named-namespace-definition:
16207      original-namespace-definition
16208      extension-namespace-definition
16209
16210    original-namespace-definition:
16211      namespace identifier { namespace-body }
16212
16213    extension-namespace-definition:
16214      namespace original-namespace-name { namespace-body }
16215
16216    unnamed-namespace-definition:
16217      namespace { namespace-body } */
16218
16219 static void
16220 cp_parser_namespace_definition (cp_parser* parser)
16221 {
16222   tree identifier, attribs;
16223   bool has_visibility;
16224   bool is_inline;
16225
16226   cp_ensure_no_omp_declare_simd (parser);
16227   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
16228     {
16229       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
16230       is_inline = true;
16231       cp_lexer_consume_token (parser->lexer);
16232     }
16233   else
16234     is_inline = false;
16235
16236   /* Look for the `namespace' keyword.  */
16237   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16238
16239   /* Get the name of the namespace.  We do not attempt to distinguish
16240      between an original-namespace-definition and an
16241      extension-namespace-definition at this point.  The semantic
16242      analysis routines are responsible for that.  */
16243   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16244     identifier = cp_parser_identifier (parser);
16245   else
16246     identifier = NULL_TREE;
16247
16248   /* Parse any specified attributes.  */
16249   attribs = cp_parser_attributes_opt (parser);
16250
16251   /* Look for the `{' to start the namespace.  */
16252   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16253   /* Start the namespace.  */
16254   push_namespace (identifier);
16255
16256   /* "inline namespace" is equivalent to a stub namespace definition
16257      followed by a strong using directive.  */
16258   if (is_inline)
16259     {
16260       tree name_space = current_namespace;
16261       /* Set up namespace association.  */
16262       DECL_NAMESPACE_ASSOCIATIONS (name_space)
16263         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16264                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
16265       /* Import the contents of the inline namespace.  */
16266       pop_namespace ();
16267       do_using_directive (name_space);
16268       push_namespace (identifier);
16269     }
16270
16271   has_visibility = handle_namespace_attrs (current_namespace, attribs);
16272
16273   /* Parse the body of the namespace.  */
16274   cp_parser_namespace_body (parser);
16275
16276   if (has_visibility)
16277     pop_visibility (1);
16278
16279   /* Finish the namespace.  */
16280   pop_namespace ();
16281   /* Look for the final `}'.  */
16282   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16283 }
16284
16285 /* Parse a namespace-body.
16286
16287    namespace-body:
16288      declaration-seq [opt]  */
16289
16290 static void
16291 cp_parser_namespace_body (cp_parser* parser)
16292 {
16293   cp_parser_declaration_seq_opt (parser);
16294 }
16295
16296 /* Parse a namespace-alias-definition.
16297
16298    namespace-alias-definition:
16299      namespace identifier = qualified-namespace-specifier ;  */
16300
16301 static void
16302 cp_parser_namespace_alias_definition (cp_parser* parser)
16303 {
16304   tree identifier;
16305   tree namespace_specifier;
16306
16307   cp_token *token = cp_lexer_peek_token (parser->lexer);
16308
16309   /* Look for the `namespace' keyword.  */
16310   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16311   /* Look for the identifier.  */
16312   identifier = cp_parser_identifier (parser);
16313   if (identifier == error_mark_node)
16314     return;
16315   /* Look for the `=' token.  */
16316   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16317       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
16318     {
16319       error_at (token->location, "%<namespace%> definition is not allowed here");
16320       /* Skip the definition.  */
16321       cp_lexer_consume_token (parser->lexer);
16322       if (cp_parser_skip_to_closing_brace (parser))
16323         cp_lexer_consume_token (parser->lexer);
16324       return;
16325     }
16326   cp_parser_require (parser, CPP_EQ, RT_EQ);
16327   /* Look for the qualified-namespace-specifier.  */
16328   namespace_specifier
16329     = cp_parser_qualified_namespace_specifier (parser);
16330   /* Look for the `;' token.  */
16331   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16332
16333   /* Register the alias in the symbol table.  */
16334   do_namespace_alias (identifier, namespace_specifier);
16335 }
16336
16337 /* Parse a qualified-namespace-specifier.
16338
16339    qualified-namespace-specifier:
16340      :: [opt] nested-name-specifier [opt] namespace-name
16341
16342    Returns a NAMESPACE_DECL corresponding to the specified
16343    namespace.  */
16344
16345 static tree
16346 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16347 {
16348   /* Look for the optional `::'.  */
16349   cp_parser_global_scope_opt (parser,
16350                               /*current_scope_valid_p=*/false);
16351
16352   /* Look for the optional nested-name-specifier.  */
16353   cp_parser_nested_name_specifier_opt (parser,
16354                                        /*typename_keyword_p=*/false,
16355                                        /*check_dependency_p=*/true,
16356                                        /*type_p=*/false,
16357                                        /*is_declaration=*/true);
16358
16359   return cp_parser_namespace_name (parser);
16360 }
16361
16362 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16363    access declaration.
16364
16365    using-declaration:
16366      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16367      using :: unqualified-id ;  
16368
16369    access-declaration:
16370      qualified-id ;  
16371
16372    */
16373
16374 static bool
16375 cp_parser_using_declaration (cp_parser* parser, 
16376                              bool access_declaration_p)
16377 {
16378   cp_token *token;
16379   bool typename_p = false;
16380   bool global_scope_p;
16381   tree decl;
16382   tree identifier;
16383   tree qscope;
16384   int oldcount = errorcount;
16385   cp_token *diag_token = NULL;
16386
16387   if (access_declaration_p)
16388     {
16389       diag_token = cp_lexer_peek_token (parser->lexer);
16390       cp_parser_parse_tentatively (parser);
16391     }
16392   else
16393     {
16394       /* Look for the `using' keyword.  */
16395       cp_parser_require_keyword (parser, RID_USING, RT_USING);
16396       
16397       /* Peek at the next token.  */
16398       token = cp_lexer_peek_token (parser->lexer);
16399       /* See if it's `typename'.  */
16400       if (token->keyword == RID_TYPENAME)
16401         {
16402           /* Remember that we've seen it.  */
16403           typename_p = true;
16404           /* Consume the `typename' token.  */
16405           cp_lexer_consume_token (parser->lexer);
16406         }
16407     }
16408
16409   /* Look for the optional global scope qualification.  */
16410   global_scope_p
16411     = (cp_parser_global_scope_opt (parser,
16412                                    /*current_scope_valid_p=*/false)
16413        != NULL_TREE);
16414
16415   /* If we saw `typename', or didn't see `::', then there must be a
16416      nested-name-specifier present.  */
16417   if (typename_p || !global_scope_p)
16418     {
16419       qscope = cp_parser_nested_name_specifier (parser, typename_p,
16420                                                 /*check_dependency_p=*/true,
16421                                                 /*type_p=*/false,
16422                                                 /*is_declaration=*/true);
16423       if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16424         {
16425           cp_parser_skip_to_end_of_block_or_statement (parser);
16426           return false;
16427         }
16428     }
16429   /* Otherwise, we could be in either of the two productions.  In that
16430      case, treat the nested-name-specifier as optional.  */
16431   else
16432     qscope = cp_parser_nested_name_specifier_opt (parser,
16433                                                   /*typename_keyword_p=*/false,
16434                                                   /*check_dependency_p=*/true,
16435                                                   /*type_p=*/false,
16436                                                   /*is_declaration=*/true);
16437   if (!qscope)
16438     qscope = global_namespace;
16439   else if (UNSCOPED_ENUM_P (qscope))
16440     qscope = CP_TYPE_CONTEXT (qscope);
16441
16442   if (access_declaration_p && cp_parser_error_occurred (parser))
16443     /* Something has already gone wrong; there's no need to parse
16444        further.  Since an error has occurred, the return value of
16445        cp_parser_parse_definitely will be false, as required.  */
16446     return cp_parser_parse_definitely (parser);
16447
16448   token = cp_lexer_peek_token (parser->lexer);
16449   /* Parse the unqualified-id.  */
16450   identifier = cp_parser_unqualified_id (parser,
16451                                          /*template_keyword_p=*/false,
16452                                          /*check_dependency_p=*/true,
16453                                          /*declarator_p=*/true,
16454                                          /*optional_p=*/false);
16455
16456   if (access_declaration_p)
16457     {
16458       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16459         cp_parser_simulate_error (parser);
16460       if (!cp_parser_parse_definitely (parser))
16461         return false;
16462     }
16463
16464   /* The function we call to handle a using-declaration is different
16465      depending on what scope we are in.  */
16466   if (qscope == error_mark_node || identifier == error_mark_node)
16467     ;
16468   else if (!identifier_p (identifier)
16469            && TREE_CODE (identifier) != BIT_NOT_EXPR)
16470     /* [namespace.udecl]
16471
16472        A using declaration shall not name a template-id.  */
16473     error_at (token->location,
16474               "a template-id may not appear in a using-declaration");
16475   else
16476     {
16477       if (at_class_scope_p ())
16478         {
16479           /* Create the USING_DECL.  */
16480           decl = do_class_using_decl (parser->scope, identifier);
16481
16482           if (decl && typename_p)
16483             USING_DECL_TYPENAME_P (decl) = 1;
16484
16485           if (check_for_bare_parameter_packs (decl))
16486             {
16487               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16488               return false;
16489             }
16490           else
16491             /* Add it to the list of members in this class.  */
16492             finish_member_declaration (decl);
16493         }
16494       else
16495         {
16496           decl = cp_parser_lookup_name_simple (parser,
16497                                                identifier,
16498                                                token->location);
16499           if (decl == error_mark_node)
16500             cp_parser_name_lookup_error (parser, identifier,
16501                                          decl, NLE_NULL,
16502                                          token->location);
16503           else if (check_for_bare_parameter_packs (decl))
16504             {
16505               cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16506               return false;
16507             }
16508           else if (!at_namespace_scope_p ())
16509             do_local_using_decl (decl, qscope, identifier);
16510           else
16511             do_toplevel_using_decl (decl, qscope, identifier);
16512         }
16513     }
16514
16515   /* Look for the final `;'.  */
16516   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16517
16518   if (access_declaration_p && errorcount == oldcount)
16519     warning_at (diag_token->location, OPT_Wdeprecated,
16520                 "access declarations are deprecated "
16521                 "in favour of using-declarations; "
16522                 "suggestion: add the %<using%> keyword");
16523
16524   return true;
16525 }
16526
16527 /* Parse an alias-declaration.
16528
16529    alias-declaration:
16530      using identifier attribute-specifier-seq [opt] = type-id  */
16531
16532 static tree
16533 cp_parser_alias_declaration (cp_parser* parser)
16534 {
16535   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16536   location_t id_location;
16537   cp_declarator *declarator;
16538   cp_decl_specifier_seq decl_specs;
16539   bool member_p;
16540   const char *saved_message = NULL;
16541
16542   /* Look for the `using' keyword.  */
16543   cp_token *using_token
16544     = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16545   if (using_token == NULL)
16546     return error_mark_node;
16547
16548   id_location = cp_lexer_peek_token (parser->lexer)->location;
16549   id = cp_parser_identifier (parser);
16550   if (id == error_mark_node)
16551     return error_mark_node;
16552
16553   cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16554   attributes = cp_parser_attributes_opt (parser);
16555   if (attributes == error_mark_node)
16556     return error_mark_node;
16557
16558   cp_parser_require (parser, CPP_EQ, RT_EQ);
16559
16560   if (cp_parser_error_occurred (parser))
16561     return error_mark_node;
16562
16563   cp_parser_commit_to_tentative_parse (parser);
16564
16565   /* Now we are going to parse the type-id of the declaration.  */
16566
16567   /*
16568     [dcl.type]/3 says:
16569
16570         "A type-specifier-seq shall not define a class or enumeration
16571          unless it appears in the type-id of an alias-declaration (7.1.3) that
16572          is not the declaration of a template-declaration."
16573
16574     In other words, if we currently are in an alias template, the
16575     type-id should not define a type.
16576
16577     So let's set parser->type_definition_forbidden_message in that
16578     case; cp_parser_check_type_definition (called by
16579     cp_parser_class_specifier) will then emit an error if a type is
16580     defined in the type-id.  */
16581   if (parser->num_template_parameter_lists)
16582     {
16583       saved_message = parser->type_definition_forbidden_message;
16584       parser->type_definition_forbidden_message =
16585         G_("types may not be defined in alias template declarations");
16586     }
16587
16588   type = cp_parser_type_id (parser);
16589
16590   /* Restore the error message if need be.  */
16591   if (parser->num_template_parameter_lists)
16592     parser->type_definition_forbidden_message = saved_message;
16593
16594   if (type == error_mark_node
16595       || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16596     {
16597       cp_parser_skip_to_end_of_block_or_statement (parser);
16598       return error_mark_node;
16599     }
16600
16601   /* A typedef-name can also be introduced by an alias-declaration. The
16602      identifier following the using keyword becomes a typedef-name. It has
16603      the same semantics as if it were introduced by the typedef
16604      specifier. In particular, it does not define a new type and it shall
16605      not appear in the type-id.  */
16606
16607   clear_decl_specs (&decl_specs);
16608   decl_specs.type = type;
16609   if (attributes != NULL_TREE)
16610     {
16611       decl_specs.attributes = attributes;
16612       set_and_check_decl_spec_loc (&decl_specs,
16613                                    ds_attribute,
16614                                    attrs_token);
16615     }
16616   set_and_check_decl_spec_loc (&decl_specs,
16617                                ds_typedef,
16618                                using_token);
16619   set_and_check_decl_spec_loc (&decl_specs,
16620                                ds_alias,
16621                                using_token);
16622
16623   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16624   declarator->id_loc = id_location;
16625
16626   member_p = at_class_scope_p ();
16627   if (member_p)
16628     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16629                       NULL_TREE, attributes);
16630   else
16631     decl = start_decl (declarator, &decl_specs, 0,
16632                        attributes, NULL_TREE, &pushed_scope);
16633   if (decl == error_mark_node)
16634     return decl;
16635
16636   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16637
16638   if (pushed_scope)
16639     pop_scope (pushed_scope);
16640
16641   /* If decl is a template, return its TEMPLATE_DECL so that it gets
16642      added into the symbol table; otherwise, return the TYPE_DECL.  */
16643   if (DECL_LANG_SPECIFIC (decl)
16644       && DECL_TEMPLATE_INFO (decl)
16645       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16646     {
16647       decl = DECL_TI_TEMPLATE (decl);
16648       if (member_p)
16649         check_member_template (decl);
16650     }
16651
16652   return decl;
16653 }
16654
16655 /* Parse a using-directive.
16656
16657    using-directive:
16658      using namespace :: [opt] nested-name-specifier [opt]
16659        namespace-name ;  */
16660
16661 static void
16662 cp_parser_using_directive (cp_parser* parser)
16663 {
16664   tree namespace_decl;
16665   tree attribs;
16666
16667   /* Look for the `using' keyword.  */
16668   cp_parser_require_keyword (parser, RID_USING, RT_USING);
16669   /* And the `namespace' keyword.  */
16670   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16671   /* Look for the optional `::' operator.  */
16672   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16673   /* And the optional nested-name-specifier.  */
16674   cp_parser_nested_name_specifier_opt (parser,
16675                                        /*typename_keyword_p=*/false,
16676                                        /*check_dependency_p=*/true,
16677                                        /*type_p=*/false,
16678                                        /*is_declaration=*/true);
16679   /* Get the namespace being used.  */
16680   namespace_decl = cp_parser_namespace_name (parser);
16681   /* And any specified attributes.  */
16682   attribs = cp_parser_attributes_opt (parser);
16683   /* Update the symbol table.  */
16684   parse_using_directive (namespace_decl, attribs);
16685   /* Look for the final `;'.  */
16686   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16687 }
16688
16689 /* Parse an asm-definition.
16690
16691    asm-definition:
16692      asm ( string-literal ) ;
16693
16694    GNU Extension:
16695
16696    asm-definition:
16697      asm volatile [opt] ( string-literal ) ;
16698      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16699      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16700                           : asm-operand-list [opt] ) ;
16701      asm volatile [opt] ( string-literal : asm-operand-list [opt]
16702                           : asm-operand-list [opt]
16703                           : asm-clobber-list [opt] ) ;
16704      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16705                                : asm-clobber-list [opt]
16706                                : asm-goto-list ) ;  */
16707
16708 static void
16709 cp_parser_asm_definition (cp_parser* parser)
16710 {
16711   tree string;
16712   tree outputs = NULL_TREE;
16713   tree inputs = NULL_TREE;
16714   tree clobbers = NULL_TREE;
16715   tree labels = NULL_TREE;
16716   tree asm_stmt;
16717   bool volatile_p = false;
16718   bool extended_p = false;
16719   bool invalid_inputs_p = false;
16720   bool invalid_outputs_p = false;
16721   bool goto_p = false;
16722   required_token missing = RT_NONE;
16723
16724   /* Look for the `asm' keyword.  */
16725   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16726
16727   if (parser->in_function_body
16728       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
16729     {
16730       error ("%<asm%> in %<constexpr%> function");
16731       cp_function_chain->invalid_constexpr = true;
16732     }
16733
16734   /* See if the next token is `volatile'.  */
16735   if (cp_parser_allow_gnu_extensions_p (parser)
16736       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16737     {
16738       /* Remember that we saw the `volatile' keyword.  */
16739       volatile_p = true;
16740       /* Consume the token.  */
16741       cp_lexer_consume_token (parser->lexer);
16742     }
16743   if (cp_parser_allow_gnu_extensions_p (parser)
16744       && parser->in_function_body
16745       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16746     {
16747       /* Remember that we saw the `goto' keyword.  */
16748       goto_p = true;
16749       /* Consume the token.  */
16750       cp_lexer_consume_token (parser->lexer);
16751     }
16752   /* Look for the opening `('.  */
16753   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16754     return;
16755   /* Look for the string.  */
16756   string = cp_parser_string_literal (parser, false, false);
16757   if (string == error_mark_node)
16758     {
16759       cp_parser_skip_to_closing_parenthesis (parser, true, false,
16760                                              /*consume_paren=*/true);
16761       return;
16762     }
16763
16764   /* If we're allowing GNU extensions, check for the extended assembly
16765      syntax.  Unfortunately, the `:' tokens need not be separated by
16766      a space in C, and so, for compatibility, we tolerate that here
16767      too.  Doing that means that we have to treat the `::' operator as
16768      two `:' tokens.  */
16769   if (cp_parser_allow_gnu_extensions_p (parser)
16770       && parser->in_function_body
16771       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16772           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16773     {
16774       bool inputs_p = false;
16775       bool clobbers_p = false;
16776       bool labels_p = false;
16777
16778       /* The extended syntax was used.  */
16779       extended_p = true;
16780
16781       /* Look for outputs.  */
16782       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16783         {
16784           /* Consume the `:'.  */
16785           cp_lexer_consume_token (parser->lexer);
16786           /* Parse the output-operands.  */
16787           if (cp_lexer_next_token_is_not (parser->lexer,
16788                                           CPP_COLON)
16789               && cp_lexer_next_token_is_not (parser->lexer,
16790                                              CPP_SCOPE)
16791               && cp_lexer_next_token_is_not (parser->lexer,
16792                                              CPP_CLOSE_PAREN)
16793               && !goto_p)
16794             outputs = cp_parser_asm_operand_list (parser);
16795
16796             if (outputs == error_mark_node)
16797               invalid_outputs_p = true;
16798         }
16799       /* If the next token is `::', there are no outputs, and the
16800          next token is the beginning of the inputs.  */
16801       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16802         /* The inputs are coming next.  */
16803         inputs_p = true;
16804
16805       /* Look for inputs.  */
16806       if (inputs_p
16807           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16808         {
16809           /* Consume the `:' or `::'.  */
16810           cp_lexer_consume_token (parser->lexer);
16811           /* Parse the output-operands.  */
16812           if (cp_lexer_next_token_is_not (parser->lexer,
16813                                           CPP_COLON)
16814               && cp_lexer_next_token_is_not (parser->lexer,
16815                                              CPP_SCOPE)
16816               && cp_lexer_next_token_is_not (parser->lexer,
16817                                              CPP_CLOSE_PAREN))
16818             inputs = cp_parser_asm_operand_list (parser);
16819
16820             if (inputs == error_mark_node)
16821               invalid_inputs_p = true;
16822         }
16823       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16824         /* The clobbers are coming next.  */
16825         clobbers_p = true;
16826
16827       /* Look for clobbers.  */
16828       if (clobbers_p
16829           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16830         {
16831           clobbers_p = true;
16832           /* Consume the `:' or `::'.  */
16833           cp_lexer_consume_token (parser->lexer);
16834           /* Parse the clobbers.  */
16835           if (cp_lexer_next_token_is_not (parser->lexer,
16836                                           CPP_COLON)
16837               && cp_lexer_next_token_is_not (parser->lexer,
16838                                              CPP_CLOSE_PAREN))
16839             clobbers = cp_parser_asm_clobber_list (parser);
16840         }
16841       else if (goto_p
16842                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16843         /* The labels are coming next.  */
16844         labels_p = true;
16845
16846       /* Look for labels.  */
16847       if (labels_p
16848           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16849         {
16850           labels_p = true;
16851           /* Consume the `:' or `::'.  */
16852           cp_lexer_consume_token (parser->lexer);
16853           /* Parse the labels.  */
16854           labels = cp_parser_asm_label_list (parser);
16855         }
16856
16857       if (goto_p && !labels_p)
16858         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16859     }
16860   else if (goto_p)
16861     missing = RT_COLON_SCOPE;
16862
16863   /* Look for the closing `)'.  */
16864   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16865                           missing ? missing : RT_CLOSE_PAREN))
16866     cp_parser_skip_to_closing_parenthesis (parser, true, false,
16867                                            /*consume_paren=*/true);
16868   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16869
16870   if (!invalid_inputs_p && !invalid_outputs_p)
16871     {
16872       /* Create the ASM_EXPR.  */
16873       if (parser->in_function_body)
16874         {
16875           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16876                                       inputs, clobbers, labels);
16877           /* If the extended syntax was not used, mark the ASM_EXPR.  */
16878           if (!extended_p)
16879             {
16880               tree temp = asm_stmt;
16881               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16882                 temp = TREE_OPERAND (temp, 0);
16883
16884               ASM_INPUT_P (temp) = 1;
16885             }
16886         }
16887       else
16888         symtab->finalize_toplevel_asm (string);
16889     }
16890 }
16891
16892 /* Declarators [gram.dcl.decl] */
16893
16894 /* Parse an init-declarator.
16895
16896    init-declarator:
16897      declarator initializer [opt]
16898
16899    GNU Extension:
16900
16901    init-declarator:
16902      declarator asm-specification [opt] attributes [opt] initializer [opt]
16903
16904    function-definition:
16905      decl-specifier-seq [opt] declarator ctor-initializer [opt]
16906        function-body
16907      decl-specifier-seq [opt] declarator function-try-block
16908
16909    GNU Extension:
16910
16911    function-definition:
16912      __extension__ function-definition
16913
16914    TM Extension:
16915
16916    function-definition:
16917      decl-specifier-seq [opt] declarator function-transaction-block
16918
16919    The DECL_SPECIFIERS apply to this declarator.  Returns a
16920    representation of the entity declared.  If MEMBER_P is TRUE, then
16921    this declarator appears in a class scope.  The new DECL created by
16922    this declarator is returned.
16923
16924    The CHECKS are access checks that should be performed once we know
16925    what entity is being declared (and, therefore, what classes have
16926    befriended it).
16927
16928    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16929    for a function-definition here as well.  If the declarator is a
16930    declarator for a function-definition, *FUNCTION_DEFINITION_P will
16931    be TRUE upon return.  By that point, the function-definition will
16932    have been completely parsed.
16933
16934    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16935    is FALSE.
16936
16937    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16938    parsed declaration if it is an uninitialized single declarator not followed
16939    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16940    if present, will not be consumed.  If returned, this declarator will be
16941    created with SD_INITIALIZED but will not call cp_finish_decl.
16942
16943    If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
16944    and there is an initializer, the pointed location_t is set to the
16945    location of the '=' or `(', or '{' in C++11 token introducing the
16946    initializer.  */
16947
16948 static tree
16949 cp_parser_init_declarator (cp_parser* parser,
16950                            cp_decl_specifier_seq *decl_specifiers,
16951                            vec<deferred_access_check, va_gc> *checks,
16952                            bool function_definition_allowed_p,
16953                            bool member_p,
16954                            int declares_class_or_enum,
16955                            bool* function_definition_p,
16956                            tree* maybe_range_for_decl,
16957                            location_t* init_loc)
16958 {
16959   cp_token *token = NULL, *asm_spec_start_token = NULL,
16960            *attributes_start_token = NULL;
16961   cp_declarator *declarator;
16962   tree prefix_attributes;
16963   tree attributes = NULL;
16964   tree asm_specification;
16965   tree initializer;
16966   tree decl = NULL_TREE;
16967   tree scope;
16968   int is_initialized;
16969   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
16970      initialized with "= ..", CPP_OPEN_PAREN if initialized with
16971      "(...)".  */
16972   enum cpp_ttype initialization_kind;
16973   bool is_direct_init = false;
16974   bool is_non_constant_init;
16975   int ctor_dtor_or_conv_p;
16976   bool friend_p = cp_parser_friend_p (decl_specifiers);
16977   tree pushed_scope = NULL_TREE;
16978   bool range_for_decl_p = false;
16979   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16980   location_t tmp_init_loc = UNKNOWN_LOCATION;
16981
16982   /* Gather the attributes that were provided with the
16983      decl-specifiers.  */
16984   prefix_attributes = decl_specifiers->attributes;
16985
16986   /* Assume that this is not the declarator for a function
16987      definition.  */
16988   if (function_definition_p)
16989     *function_definition_p = false;
16990
16991   /* Default arguments are only permitted for function parameters.  */
16992   if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16993     parser->default_arg_ok_p = false;
16994
16995   /* Defer access checks while parsing the declarator; we cannot know
16996      what names are accessible until we know what is being
16997      declared.  */
16998   resume_deferring_access_checks ();
16999
17000   /* Parse the declarator.  */
17001   token = cp_lexer_peek_token (parser->lexer);
17002   declarator
17003     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17004                             &ctor_dtor_or_conv_p,
17005                             /*parenthesized_p=*/NULL,
17006                             member_p, friend_p);
17007   /* Gather up the deferred checks.  */
17008   stop_deferring_access_checks ();
17009
17010   parser->default_arg_ok_p = saved_default_arg_ok_p;
17011
17012   /* If the DECLARATOR was erroneous, there's no need to go
17013      further.  */
17014   if (declarator == cp_error_declarator)
17015     return error_mark_node;
17016
17017   /* Check that the number of template-parameter-lists is OK.  */
17018   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
17019                                                        token->location))
17020     return error_mark_node;
17021
17022   if (declares_class_or_enum & 2)
17023     cp_parser_check_for_definition_in_return_type (declarator,
17024                                                    decl_specifiers->type,
17025                                                    decl_specifiers->locations[ds_type_spec]);
17026
17027   /* Figure out what scope the entity declared by the DECLARATOR is
17028      located in.  `grokdeclarator' sometimes changes the scope, so
17029      we compute it now.  */
17030   scope = get_scope_of_declarator (declarator);
17031
17032   /* Perform any lookups in the declared type which were thought to be
17033      dependent, but are not in the scope of the declarator.  */
17034   decl_specifiers->type
17035     = maybe_update_decl_type (decl_specifiers->type, scope);
17036
17037   /* If we're allowing GNU extensions, look for an
17038      asm-specification.  */
17039   if (cp_parser_allow_gnu_extensions_p (parser))
17040     {
17041       /* Look for an asm-specification.  */
17042       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
17043       asm_specification = cp_parser_asm_specification_opt (parser);
17044     }
17045   else
17046     asm_specification = NULL_TREE;
17047
17048   /* Look for attributes.  */
17049   attributes_start_token = cp_lexer_peek_token (parser->lexer);
17050   attributes = cp_parser_attributes_opt (parser);
17051
17052   /* Peek at the next token.  */
17053   token = cp_lexer_peek_token (parser->lexer);
17054
17055   bool bogus_implicit_tmpl = false;
17056
17057   if (function_declarator_p (declarator))
17058     {
17059       /* Check to see if the token indicates the start of a
17060          function-definition.  */
17061       if (cp_parser_token_starts_function_definition_p (token))
17062         {
17063           if (!function_definition_allowed_p)
17064             {
17065               /* If a function-definition should not appear here, issue an
17066                  error message.  */
17067               cp_parser_error (parser,
17068                                "a function-definition is not allowed here");
17069               return error_mark_node;
17070             }
17071
17072           location_t func_brace_location
17073             = cp_lexer_peek_token (parser->lexer)->location;
17074
17075           /* Neither attributes nor an asm-specification are allowed
17076              on a function-definition.  */
17077           if (asm_specification)
17078             error_at (asm_spec_start_token->location,
17079                       "an asm-specification is not allowed "
17080                       "on a function-definition");
17081           if (attributes)
17082             error_at (attributes_start_token->location,
17083                       "attributes are not allowed "
17084                       "on a function-definition");
17085           /* This is a function-definition.  */
17086           *function_definition_p = true;
17087
17088           /* Parse the function definition.  */
17089           if (member_p)
17090             decl = cp_parser_save_member_function_body (parser,
17091                                                         decl_specifiers,
17092                                                         declarator,
17093                                                         prefix_attributes);
17094           else
17095             decl =
17096               (cp_parser_function_definition_from_specifiers_and_declarator
17097                (parser, decl_specifiers, prefix_attributes, declarator));
17098
17099           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
17100             {
17101               /* This is where the prologue starts...  */
17102               DECL_STRUCT_FUNCTION (decl)->function_start_locus
17103                 = func_brace_location;
17104             }
17105
17106           return decl;
17107         }
17108     }
17109   else if (parser->fully_implicit_function_template_p)
17110     {
17111       /* A non-template declaration involving a function parameter list
17112          containing an implicit template parameter will be made into a
17113          template.  If the resulting declaration is not going to be an
17114          actual function then finish the template scope here to prevent it.
17115          An error message will be issued once we have a decl to talk about.
17116
17117          FIXME probably we should do type deduction rather than create an
17118          implicit template, but the standard currently doesn't allow it. */
17119       bogus_implicit_tmpl = true;
17120       finish_fully_implicit_template (parser, NULL_TREE);
17121     }
17122
17123   /* [dcl.dcl]
17124
17125      Only in function declarations for constructors, destructors, and
17126      type conversions can the decl-specifier-seq be omitted.
17127
17128      We explicitly postpone this check past the point where we handle
17129      function-definitions because we tolerate function-definitions
17130      that are missing their return types in some modes.  */
17131   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
17132     {
17133       cp_parser_error (parser,
17134                        "expected constructor, destructor, or type conversion");
17135       return error_mark_node;
17136     }
17137
17138   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
17139   if (token->type == CPP_EQ
17140       || token->type == CPP_OPEN_PAREN
17141       || token->type == CPP_OPEN_BRACE)
17142     {
17143       is_initialized = SD_INITIALIZED;
17144       initialization_kind = token->type;
17145       if (maybe_range_for_decl)
17146         *maybe_range_for_decl = error_mark_node;
17147       tmp_init_loc = token->location;
17148       if (init_loc && *init_loc == UNKNOWN_LOCATION)
17149         *init_loc = tmp_init_loc;
17150
17151       if (token->type == CPP_EQ
17152           && function_declarator_p (declarator))
17153         {
17154           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
17155           if (t2->keyword == RID_DEFAULT)
17156             is_initialized = SD_DEFAULTED;
17157           else if (t2->keyword == RID_DELETE)
17158             is_initialized = SD_DELETED;
17159         }
17160     }
17161   else
17162     {
17163       /* If the init-declarator isn't initialized and isn't followed by a
17164          `,' or `;', it's not a valid init-declarator.  */
17165       if (token->type != CPP_COMMA
17166           && token->type != CPP_SEMICOLON)
17167         {
17168           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
17169             range_for_decl_p = true;
17170           else
17171             {
17172               if (!maybe_range_for_decl)
17173                 cp_parser_error (parser, "expected initializer");
17174               return error_mark_node;
17175             }
17176         }
17177       is_initialized = SD_UNINITIALIZED;
17178       initialization_kind = CPP_EOF;
17179     }
17180
17181   /* Because start_decl has side-effects, we should only call it if we
17182      know we're going ahead.  By this point, we know that we cannot
17183      possibly be looking at any other construct.  */
17184   cp_parser_commit_to_tentative_parse (parser);
17185
17186   /* Enter the newly declared entry in the symbol table.  If we're
17187      processing a declaration in a class-specifier, we wait until
17188      after processing the initializer.  */
17189   if (!member_p)
17190     {
17191       if (parser->in_unbraced_linkage_specification_p)
17192         decl_specifiers->storage_class = sc_extern;
17193       decl = start_decl (declarator, decl_specifiers,
17194                          range_for_decl_p? SD_INITIALIZED : is_initialized,
17195                          attributes, prefix_attributes, &pushed_scope);
17196       cp_finalize_omp_declare_simd (parser, decl);
17197       /* Adjust location of decl if declarator->id_loc is more appropriate:
17198          set, and decl wasn't merged with another decl, in which case its
17199          location would be different from input_location, and more accurate.  */
17200       if (DECL_P (decl)
17201           && declarator->id_loc != UNKNOWN_LOCATION
17202           && DECL_SOURCE_LOCATION (decl) == input_location)
17203         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
17204     }
17205   else if (scope)
17206     /* Enter the SCOPE.  That way unqualified names appearing in the
17207        initializer will be looked up in SCOPE.  */
17208     pushed_scope = push_scope (scope);
17209
17210   /* Perform deferred access control checks, now that we know in which
17211      SCOPE the declared entity resides.  */
17212   if (!member_p && decl)
17213     {
17214       tree saved_current_function_decl = NULL_TREE;
17215
17216       /* If the entity being declared is a function, pretend that we
17217          are in its scope.  If it is a `friend', it may have access to
17218          things that would not otherwise be accessible.  */
17219       if (TREE_CODE (decl) == FUNCTION_DECL)
17220         {
17221           saved_current_function_decl = current_function_decl;
17222           current_function_decl = decl;
17223         }
17224
17225       /* Perform access checks for template parameters.  */
17226       cp_parser_perform_template_parameter_access_checks (checks);
17227
17228       /* Perform the access control checks for the declarator and the
17229          decl-specifiers.  */
17230       perform_deferred_access_checks (tf_warning_or_error);
17231
17232       /* Restore the saved value.  */
17233       if (TREE_CODE (decl) == FUNCTION_DECL)
17234         current_function_decl = saved_current_function_decl;
17235     }
17236
17237   /* Parse the initializer.  */
17238   initializer = NULL_TREE;
17239   is_direct_init = false;
17240   is_non_constant_init = true;
17241   if (is_initialized)
17242     {
17243       if (function_declarator_p (declarator))
17244         {
17245            if (initialization_kind == CPP_EQ)
17246              initializer = cp_parser_pure_specifier (parser);
17247            else
17248              {
17249                /* If the declaration was erroneous, we don't really
17250                   know what the user intended, so just silently
17251                   consume the initializer.  */
17252                if (decl != error_mark_node)
17253                  error_at (tmp_init_loc, "initializer provided for function");
17254                cp_parser_skip_to_closing_parenthesis (parser,
17255                                                       /*recovering=*/true,
17256                                                       /*or_comma=*/false,
17257                                                       /*consume_paren=*/true);
17258              }
17259         }
17260       else
17261         {
17262           /* We want to record the extra mangling scope for in-class
17263              initializers of class members and initializers of static data
17264              member templates.  The former involves deferring
17265              parsing of the initializer until end of class as with default
17266              arguments.  So right here we only handle the latter.  */
17267           if (!member_p && processing_template_decl)
17268             start_lambda_scope (decl);
17269           initializer = cp_parser_initializer (parser,
17270                                                &is_direct_init,
17271                                                &is_non_constant_init);
17272           if (!member_p && processing_template_decl)
17273             finish_lambda_scope ();
17274           if (initializer == error_mark_node)
17275             cp_parser_skip_to_end_of_statement (parser);
17276         }
17277     }
17278
17279   /* The old parser allows attributes to appear after a parenthesized
17280      initializer.  Mark Mitchell proposed removing this functionality
17281      on the GCC mailing lists on 2002-08-13.  This parser accepts the
17282      attributes -- but ignores them.  */
17283   if (cp_parser_allow_gnu_extensions_p (parser)
17284       && initialization_kind == CPP_OPEN_PAREN)
17285     if (cp_parser_attributes_opt (parser))
17286       warning (OPT_Wattributes,
17287                "attributes after parenthesized initializer ignored");
17288
17289   /* And now complain about a non-function implicit template.  */
17290   if (bogus_implicit_tmpl)
17291     error_at (DECL_SOURCE_LOCATION (decl),
17292               "non-function %qD declared as implicit template", decl);
17293
17294   /* For an in-class declaration, use `grokfield' to create the
17295      declaration.  */
17296   if (member_p)
17297     {
17298       if (pushed_scope)
17299         {
17300           pop_scope (pushed_scope);
17301           pushed_scope = NULL_TREE;
17302         }
17303       decl = grokfield (declarator, decl_specifiers,
17304                         initializer, !is_non_constant_init,
17305                         /*asmspec=*/NULL_TREE,
17306                         chainon (attributes, prefix_attributes));
17307       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17308         cp_parser_save_default_args (parser, decl);
17309       cp_finalize_omp_declare_simd (parser, decl);
17310     }
17311
17312   /* Finish processing the declaration.  But, skip member
17313      declarations.  */
17314   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17315     {
17316       cp_finish_decl (decl,
17317                       initializer, !is_non_constant_init,
17318                       asm_specification,
17319                       /* If the initializer is in parentheses, then this is
17320                          a direct-initialization, which means that an
17321                          `explicit' constructor is OK.  Otherwise, an
17322                          `explicit' constructor cannot be used.  */
17323                       ((is_direct_init || !is_initialized)
17324                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17325     }
17326   else if ((cxx_dialect != cxx98) && friend_p
17327            && decl && TREE_CODE (decl) == FUNCTION_DECL)
17328     /* Core issue #226 (C++0x only): A default template-argument
17329        shall not be specified in a friend class template
17330        declaration. */
17331     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true, 
17332                              /*is_partial=*/false, /*is_friend_decl=*/1);
17333
17334   if (!friend_p && pushed_scope)
17335     pop_scope (pushed_scope);
17336
17337   if (function_declarator_p (declarator)
17338       && parser->fully_implicit_function_template_p)
17339     {
17340       if (member_p)
17341         decl = finish_fully_implicit_template (parser, decl);
17342       else
17343         finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17344     }
17345
17346   return decl;
17347 }
17348
17349 /* Parse a declarator.
17350
17351    declarator:
17352      direct-declarator
17353      ptr-operator declarator
17354
17355    abstract-declarator:
17356      ptr-operator abstract-declarator [opt]
17357      direct-abstract-declarator
17358
17359    GNU Extensions:
17360
17361    declarator:
17362      attributes [opt] direct-declarator
17363      attributes [opt] ptr-operator declarator
17364
17365    abstract-declarator:
17366      attributes [opt] ptr-operator abstract-declarator [opt]
17367      attributes [opt] direct-abstract-declarator
17368
17369    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17370    detect constructor, destructor or conversion operators. It is set
17371    to -1 if the declarator is a name, and +1 if it is a
17372    function. Otherwise it is set to zero. Usually you just want to
17373    test for >0, but internally the negative value is used.
17374
17375    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17376    a decl-specifier-seq unless it declares a constructor, destructor,
17377    or conversion.  It might seem that we could check this condition in
17378    semantic analysis, rather than parsing, but that makes it difficult
17379    to handle something like `f()'.  We want to notice that there are
17380    no decl-specifiers, and therefore realize that this is an
17381    expression, not a declaration.)
17382
17383    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17384    the declarator is a direct-declarator of the form "(...)".
17385
17386    MEMBER_P is true iff this declarator is a member-declarator.
17387
17388    FRIEND_P is true iff this declarator is a friend.  */
17389
17390 static cp_declarator *
17391 cp_parser_declarator (cp_parser* parser,
17392                       cp_parser_declarator_kind dcl_kind,
17393                       int* ctor_dtor_or_conv_p,
17394                       bool* parenthesized_p,
17395                       bool member_p, bool friend_p)
17396 {
17397   cp_declarator *declarator;
17398   enum tree_code code;
17399   cp_cv_quals cv_quals;
17400   tree class_type;
17401   tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17402
17403   /* Assume this is not a constructor, destructor, or type-conversion
17404      operator.  */
17405   if (ctor_dtor_or_conv_p)
17406     *ctor_dtor_or_conv_p = 0;
17407
17408   if (cp_parser_allow_gnu_extensions_p (parser))
17409     gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17410
17411   /* Check for the ptr-operator production.  */
17412   cp_parser_parse_tentatively (parser);
17413   /* Parse the ptr-operator.  */
17414   code = cp_parser_ptr_operator (parser,
17415                                  &class_type,
17416                                  &cv_quals,
17417                                  &std_attributes);
17418
17419   /* If that worked, then we have a ptr-operator.  */
17420   if (cp_parser_parse_definitely (parser))
17421     {
17422       /* If a ptr-operator was found, then this declarator was not
17423          parenthesized.  */
17424       if (parenthesized_p)
17425         *parenthesized_p = true;
17426       /* The dependent declarator is optional if we are parsing an
17427          abstract-declarator.  */
17428       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17429         cp_parser_parse_tentatively (parser);
17430
17431       /* Parse the dependent declarator.  */
17432       declarator = cp_parser_declarator (parser, dcl_kind,
17433                                          /*ctor_dtor_or_conv_p=*/NULL,
17434                                          /*parenthesized_p=*/NULL,
17435                                          /*member_p=*/false,
17436                                          friend_p);
17437
17438       /* If we are parsing an abstract-declarator, we must handle the
17439          case where the dependent declarator is absent.  */
17440       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17441           && !cp_parser_parse_definitely (parser))
17442         declarator = NULL;
17443
17444       declarator = cp_parser_make_indirect_declarator
17445         (code, class_type, cv_quals, declarator, std_attributes);
17446     }
17447   /* Everything else is a direct-declarator.  */
17448   else
17449     {
17450       if (parenthesized_p)
17451         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17452                                                    CPP_OPEN_PAREN);
17453       declarator = cp_parser_direct_declarator (parser, dcl_kind,
17454                                                 ctor_dtor_or_conv_p,
17455                                                 member_p, friend_p);
17456     }
17457
17458   if (gnu_attributes && declarator && declarator != cp_error_declarator)
17459     declarator->attributes = gnu_attributes;
17460   return declarator;
17461 }
17462
17463 /* Parse a direct-declarator or direct-abstract-declarator.
17464
17465    direct-declarator:
17466      declarator-id
17467      direct-declarator ( parameter-declaration-clause )
17468        cv-qualifier-seq [opt]
17469        ref-qualifier [opt]
17470        exception-specification [opt]
17471      direct-declarator [ constant-expression [opt] ]
17472      ( declarator )
17473
17474    direct-abstract-declarator:
17475      direct-abstract-declarator [opt]
17476        ( parameter-declaration-clause )
17477        cv-qualifier-seq [opt]
17478        ref-qualifier [opt]
17479        exception-specification [opt]
17480      direct-abstract-declarator [opt] [ constant-expression [opt] ]
17481      ( abstract-declarator )
17482
17483    Returns a representation of the declarator.  DCL_KIND is
17484    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17485    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
17486    we are parsing a direct-declarator.  It is
17487    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17488    of ambiguity we prefer an abstract declarator, as per
17489    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17490    as for cp_parser_declarator.  */
17491
17492 static cp_declarator *
17493 cp_parser_direct_declarator (cp_parser* parser,
17494                              cp_parser_declarator_kind dcl_kind,
17495                              int* ctor_dtor_or_conv_p,
17496                              bool member_p, bool friend_p)
17497 {
17498   cp_token *token;
17499   cp_declarator *declarator = NULL;
17500   tree scope = NULL_TREE;
17501   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17502   bool saved_in_declarator_p = parser->in_declarator_p;
17503   bool first = true;
17504   tree pushed_scope = NULL_TREE;
17505
17506   while (true)
17507     {
17508       /* Peek at the next token.  */
17509       token = cp_lexer_peek_token (parser->lexer);
17510       if (token->type == CPP_OPEN_PAREN)
17511         {
17512           /* This is either a parameter-declaration-clause, or a
17513              parenthesized declarator. When we know we are parsing a
17514              named declarator, it must be a parenthesized declarator
17515              if FIRST is true. For instance, `(int)' is a
17516              parameter-declaration-clause, with an omitted
17517              direct-abstract-declarator. But `((*))', is a
17518              parenthesized abstract declarator. Finally, when T is a
17519              template parameter `(T)' is a
17520              parameter-declaration-clause, and not a parenthesized
17521              named declarator.
17522
17523              We first try and parse a parameter-declaration-clause,
17524              and then try a nested declarator (if FIRST is true).
17525
17526              It is not an error for it not to be a
17527              parameter-declaration-clause, even when FIRST is
17528              false. Consider,
17529
17530                int i (int);
17531                int i (3);
17532
17533              The first is the declaration of a function while the
17534              second is the definition of a variable, including its
17535              initializer.
17536
17537              Having seen only the parenthesis, we cannot know which of
17538              these two alternatives should be selected.  Even more
17539              complex are examples like:
17540
17541                int i (int (a));
17542                int i (int (3));
17543
17544              The former is a function-declaration; the latter is a
17545              variable initialization.
17546
17547              Thus again, we try a parameter-declaration-clause, and if
17548              that fails, we back out and return.  */
17549
17550           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17551             {
17552               tree params;
17553               bool is_declarator = false;
17554
17555               /* In a member-declarator, the only valid interpretation
17556                  of a parenthesis is the start of a
17557                  parameter-declaration-clause.  (It is invalid to
17558                  initialize a static data member with a parenthesized
17559                  initializer; only the "=" form of initialization is
17560                  permitted.)  */
17561               if (!member_p)
17562                 cp_parser_parse_tentatively (parser);
17563
17564               /* Consume the `('.  */
17565               cp_lexer_consume_token (parser->lexer);
17566               if (first)
17567                 {
17568                   /* If this is going to be an abstract declarator, we're
17569                      in a declarator and we can't have default args.  */
17570                   parser->default_arg_ok_p = false;
17571                   parser->in_declarator_p = true;
17572                 }
17573
17574               begin_scope (sk_function_parms, NULL_TREE);
17575
17576               /* Parse the parameter-declaration-clause.  */
17577               params = cp_parser_parameter_declaration_clause (parser);
17578
17579               /* Consume the `)'.  */
17580               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17581
17582               /* If all went well, parse the cv-qualifier-seq,
17583                  ref-qualifier and the exception-specification.  */
17584               if (member_p || cp_parser_parse_definitely (parser))
17585                 {
17586                   cp_cv_quals cv_quals;
17587                   cp_virt_specifiers virt_specifiers;
17588                   cp_ref_qualifier ref_qual;
17589                   tree exception_specification;
17590                   tree late_return;
17591                   tree attrs;
17592                   bool memfn = (member_p || (pushed_scope
17593                                              && CLASS_TYPE_P (pushed_scope)));
17594
17595                   is_declarator = true;
17596
17597                   if (ctor_dtor_or_conv_p)
17598                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17599                   first = false;
17600
17601                   /* Parse the cv-qualifier-seq.  */
17602                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17603                   /* Parse the ref-qualifier. */
17604                   ref_qual = cp_parser_ref_qualifier_opt (parser);
17605                   /* And the exception-specification.  */
17606                   exception_specification
17607                     = cp_parser_exception_specification_opt (parser);
17608
17609                   attrs = cp_parser_std_attribute_spec_seq (parser);
17610
17611                   /* In here, we handle cases where attribute is used after
17612                      the function declaration.  For example:
17613                      void func (int x) __attribute__((vector(..)));  */
17614                   if (flag_cilkplus
17615                       && cp_next_tokens_can_be_gnu_attribute_p (parser))
17616                     {
17617                       cp_parser_parse_tentatively (parser);
17618                       tree attr = cp_parser_gnu_attributes_opt (parser);
17619                       if (cp_lexer_next_token_is_not (parser->lexer,
17620                                                       CPP_SEMICOLON)
17621                           && cp_lexer_next_token_is_not (parser->lexer,
17622                                                          CPP_OPEN_BRACE))
17623                         cp_parser_abort_tentative_parse (parser);
17624                       else if (!cp_parser_parse_definitely (parser))
17625                         ;
17626                       else
17627                         attrs = chainon (attr, attrs);
17628                     }
17629                   late_return = (cp_parser_late_return_type_opt
17630                                  (parser, declarator,
17631                                   memfn ? cv_quals : -1));
17632
17633
17634                   /* Parse the virt-specifier-seq.  */
17635                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17636
17637                   /* Create the function-declarator.  */
17638                   declarator = make_call_declarator (declarator,
17639                                                      params,
17640                                                      cv_quals,
17641                                                      virt_specifiers,
17642                                                      ref_qual,
17643                                                      exception_specification,
17644                                                      late_return);
17645                   declarator->std_attributes = attrs;
17646                   /* Any subsequent parameter lists are to do with
17647                      return type, so are not those of the declared
17648                      function.  */
17649                   parser->default_arg_ok_p = false;
17650                 }
17651
17652               /* Remove the function parms from scope.  */
17653               pop_bindings_and_leave_scope ();
17654
17655               if (is_declarator)
17656                 /* Repeat the main loop.  */
17657                 continue;
17658             }
17659
17660           /* If this is the first, we can try a parenthesized
17661              declarator.  */
17662           if (first)
17663             {
17664               bool saved_in_type_id_in_expr_p;
17665
17666               parser->default_arg_ok_p = saved_default_arg_ok_p;
17667               parser->in_declarator_p = saved_in_declarator_p;
17668
17669               /* Consume the `('.  */
17670               cp_lexer_consume_token (parser->lexer);
17671               /* Parse the nested declarator.  */
17672               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17673               parser->in_type_id_in_expr_p = true;
17674               declarator
17675                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17676                                         /*parenthesized_p=*/NULL,
17677                                         member_p, friend_p);
17678               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17679               first = false;
17680               /* Expect a `)'.  */
17681               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17682                 declarator = cp_error_declarator;
17683               if (declarator == cp_error_declarator)
17684                 break;
17685
17686               goto handle_declarator;
17687             }
17688           /* Otherwise, we must be done.  */
17689           else
17690             break;
17691         }
17692       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17693                && token->type == CPP_OPEN_SQUARE
17694                && !cp_next_tokens_can_be_attribute_p (parser))
17695         {
17696           /* Parse an array-declarator.  */
17697           tree bounds, attrs;
17698
17699           if (ctor_dtor_or_conv_p)
17700             *ctor_dtor_or_conv_p = 0;
17701
17702           first = false;
17703           parser->default_arg_ok_p = false;
17704           parser->in_declarator_p = true;
17705           /* Consume the `['.  */
17706           cp_lexer_consume_token (parser->lexer);
17707           /* Peek at the next token.  */
17708           token = cp_lexer_peek_token (parser->lexer);
17709           /* If the next token is `]', then there is no
17710              constant-expression.  */
17711           if (token->type != CPP_CLOSE_SQUARE)
17712             {
17713               bool non_constant_p;
17714               bounds
17715                 = cp_parser_constant_expression (parser,
17716                                                  /*allow_non_constant=*/true,
17717                                                  &non_constant_p);
17718               if (!non_constant_p)
17719                 /* OK */;
17720               else if (error_operand_p (bounds))
17721                 /* Already gave an error.  */;
17722               else if (!parser->in_function_body
17723                        || current_binding_level->kind == sk_function_parms)
17724                 {
17725                   /* Normally, the array bound must be an integral constant
17726                      expression.  However, as an extension, we allow VLAs
17727                      in function scopes as long as they aren't part of a
17728                      parameter declaration.  */
17729                   cp_parser_error (parser,
17730                                    "array bound is not an integer constant");
17731                   bounds = error_mark_node;
17732                 }
17733               else if (processing_template_decl
17734                        && !type_dependent_expression_p (bounds))
17735                 {
17736                   /* Remember this wasn't a constant-expression.  */
17737                   bounds = build_nop (TREE_TYPE (bounds), bounds);
17738                   TREE_SIDE_EFFECTS (bounds) = 1;
17739                 }
17740             }
17741           else
17742             bounds = NULL_TREE;
17743           /* Look for the closing `]'.  */
17744           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17745             {
17746               declarator = cp_error_declarator;
17747               break;
17748             }
17749
17750           attrs = cp_parser_std_attribute_spec_seq (parser);
17751           declarator = make_array_declarator (declarator, bounds);
17752           declarator->std_attributes = attrs;
17753         }
17754       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17755         {
17756           {
17757             tree qualifying_scope;
17758             tree unqualified_name;
17759             tree attrs;
17760             special_function_kind sfk;
17761             bool abstract_ok;
17762             bool pack_expansion_p = false;
17763             cp_token *declarator_id_start_token;
17764
17765             /* Parse a declarator-id */
17766             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17767             if (abstract_ok)
17768               {
17769                 cp_parser_parse_tentatively (parser);
17770
17771                 /* If we see an ellipsis, we should be looking at a
17772                    parameter pack. */
17773                 if (token->type == CPP_ELLIPSIS)
17774                   {
17775                     /* Consume the `...' */
17776                     cp_lexer_consume_token (parser->lexer);
17777
17778                     pack_expansion_p = true;
17779                   }
17780               }
17781
17782             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17783             unqualified_name
17784               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17785             qualifying_scope = parser->scope;
17786             if (abstract_ok)
17787               {
17788                 bool okay = false;
17789
17790                 if (!unqualified_name && pack_expansion_p)
17791                   {
17792                     /* Check whether an error occurred. */
17793                     okay = !cp_parser_error_occurred (parser);
17794
17795                     /* We already consumed the ellipsis to mark a
17796                        parameter pack, but we have no way to report it,
17797                        so abort the tentative parse. We will be exiting
17798                        immediately anyway. */
17799                     cp_parser_abort_tentative_parse (parser);
17800                   }
17801                 else
17802                   okay = cp_parser_parse_definitely (parser);
17803
17804                 if (!okay)
17805                   unqualified_name = error_mark_node;
17806                 else if (unqualified_name
17807                          && (qualifying_scope
17808                              || (!identifier_p (unqualified_name))))
17809                   {
17810                     cp_parser_error (parser, "expected unqualified-id");
17811                     unqualified_name = error_mark_node;
17812                   }
17813               }
17814
17815             if (!unqualified_name)
17816               return NULL;
17817             if (unqualified_name == error_mark_node)
17818               {
17819                 declarator = cp_error_declarator;
17820                 pack_expansion_p = false;
17821                 declarator->parameter_pack_p = false;
17822                 break;
17823               }
17824
17825             attrs = cp_parser_std_attribute_spec_seq (parser);
17826
17827             if (qualifying_scope && at_namespace_scope_p ()
17828                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17829               {
17830                 /* In the declaration of a member of a template class
17831                    outside of the class itself, the SCOPE will sometimes
17832                    be a TYPENAME_TYPE.  For example, given:
17833
17834                    template <typename T>
17835                    int S<T>::R::i = 3;
17836
17837                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
17838                    this context, we must resolve S<T>::R to an ordinary
17839                    type, rather than a typename type.
17840
17841                    The reason we normally avoid resolving TYPENAME_TYPEs
17842                    is that a specialization of `S' might render
17843                    `S<T>::R' not a type.  However, if `S' is
17844                    specialized, then this `i' will not be used, so there
17845                    is no harm in resolving the types here.  */
17846                 tree type;
17847
17848                 /* Resolve the TYPENAME_TYPE.  */
17849                 type = resolve_typename_type (qualifying_scope,
17850                                               /*only_current_p=*/false);
17851                 /* If that failed, the declarator is invalid.  */
17852                 if (TREE_CODE (type) == TYPENAME_TYPE)
17853                   {
17854                     if (typedef_variant_p (type))
17855                       error_at (declarator_id_start_token->location,
17856                                 "cannot define member of dependent typedef "
17857                                 "%qT", type);
17858                     else
17859                       error_at (declarator_id_start_token->location,
17860                                 "%<%T::%E%> is not a type",
17861                                 TYPE_CONTEXT (qualifying_scope),
17862                                 TYPE_IDENTIFIER (qualifying_scope));
17863                   }
17864                 qualifying_scope = type;
17865               }
17866
17867             sfk = sfk_none;
17868
17869             if (unqualified_name)
17870               {
17871                 tree class_type;
17872
17873                 if (qualifying_scope
17874                     && CLASS_TYPE_P (qualifying_scope))
17875                   class_type = qualifying_scope;
17876                 else
17877                   class_type = current_class_type;
17878
17879                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17880                   {
17881                     tree name_type = TREE_TYPE (unqualified_name);
17882                     if (class_type && same_type_p (name_type, class_type))
17883                       {
17884                         if (qualifying_scope
17885                             && CLASSTYPE_USE_TEMPLATE (name_type))
17886                           {
17887                             error_at (declarator_id_start_token->location,
17888                                       "invalid use of constructor as a template");
17889                             inform (declarator_id_start_token->location,
17890                                     "use %<%T::%D%> instead of %<%T::%D%> to "
17891                                     "name the constructor in a qualified name",
17892                                     class_type,
17893                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17894                                     class_type, name_type);
17895                             declarator = cp_error_declarator;
17896                             break;
17897                           }
17898                         else
17899                           unqualified_name = constructor_name (class_type);
17900                       }
17901                     else
17902                       {
17903                         /* We do not attempt to print the declarator
17904                            here because we do not have enough
17905                            information about its original syntactic
17906                            form.  */
17907                         cp_parser_error (parser, "invalid declarator");
17908                         declarator = cp_error_declarator;
17909                         break;
17910                       }
17911                   }
17912
17913                 if (class_type)
17914                   {
17915                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17916                       sfk = sfk_destructor;
17917                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17918                       sfk = sfk_conversion;
17919                     else if (/* There's no way to declare a constructor
17920                                 for an anonymous type, even if the type
17921                                 got a name for linkage purposes.  */
17922                              !TYPE_WAS_ANONYMOUS (class_type)
17923                              /* Handle correctly (c++/19200):
17924
17925                                 struct S {
17926                                   struct T{};
17927                                   friend void S(T);
17928                                 };
17929
17930                                 and also:
17931
17932                                 namespace N {
17933                                   void S();
17934                                 }
17935
17936                                 struct S {
17937                                   friend void N::S();
17938                                 };  */
17939                              && !(friend_p
17940                                   && class_type != qualifying_scope)
17941                              && constructor_name_p (unqualified_name,
17942                                                     class_type))
17943                       {
17944                         unqualified_name = constructor_name (class_type);
17945                         sfk = sfk_constructor;
17946                       }
17947                     else if (is_overloaded_fn (unqualified_name)
17948                              && DECL_CONSTRUCTOR_P (get_first_fn
17949                                                     (unqualified_name)))
17950                       sfk = sfk_constructor;
17951
17952                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
17953                       *ctor_dtor_or_conv_p = -1;
17954                   }
17955               }
17956             declarator = make_id_declarator (qualifying_scope,
17957                                              unqualified_name,
17958                                              sfk);
17959             declarator->std_attributes = attrs;
17960             declarator->id_loc = token->location;
17961             declarator->parameter_pack_p = pack_expansion_p;
17962
17963             if (pack_expansion_p)
17964               maybe_warn_variadic_templates ();
17965           }
17966
17967         handle_declarator:;
17968           scope = get_scope_of_declarator (declarator);
17969           if (scope)
17970             {
17971               /* Any names that appear after the declarator-id for a
17972                  member are looked up in the containing scope.  */
17973               if (at_function_scope_p ())
17974                 {
17975                   /* But declarations with qualified-ids can't appear in a
17976                      function.  */
17977                   cp_parser_error (parser, "qualified-id in declaration");
17978                   declarator = cp_error_declarator;
17979                   break;
17980                 }
17981               pushed_scope = push_scope (scope);
17982             }
17983           parser->in_declarator_p = true;
17984           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17985               || (declarator && declarator->kind == cdk_id))
17986             /* Default args are only allowed on function
17987                declarations.  */
17988             parser->default_arg_ok_p = saved_default_arg_ok_p;
17989           else
17990             parser->default_arg_ok_p = false;
17991
17992           first = false;
17993         }
17994       /* We're done.  */
17995       else
17996         break;
17997     }
17998
17999   /* For an abstract declarator, we might wind up with nothing at this
18000      point.  That's an error; the declarator is not optional.  */
18001   if (!declarator)
18002     cp_parser_error (parser, "expected declarator");
18003
18004   /* If we entered a scope, we must exit it now.  */
18005   if (pushed_scope)
18006     pop_scope (pushed_scope);
18007
18008   parser->default_arg_ok_p = saved_default_arg_ok_p;
18009   parser->in_declarator_p = saved_in_declarator_p;
18010
18011   return declarator;
18012 }
18013
18014 /* Parse a ptr-operator.
18015
18016    ptr-operator:
18017      * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18018      * cv-qualifier-seq [opt]
18019      &
18020      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
18021      nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
18022
18023    GNU Extension:
18024
18025    ptr-operator:
18026      & cv-qualifier-seq [opt]
18027
18028    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
18029    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
18030    an rvalue reference. In the case of a pointer-to-member, *TYPE is
18031    filled in with the TYPE containing the member.  *CV_QUALS is
18032    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
18033    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
18034    Note that the tree codes returned by this function have nothing
18035    to do with the types of trees that will be eventually be created
18036    to represent the pointer or reference type being parsed. They are
18037    just constants with suggestive names. */
18038 static enum tree_code
18039 cp_parser_ptr_operator (cp_parser* parser,
18040                         tree* type,
18041                         cp_cv_quals *cv_quals,
18042                         tree *attributes)
18043 {
18044   enum tree_code code = ERROR_MARK;
18045   cp_token *token;
18046   tree attrs = NULL_TREE;
18047
18048   /* Assume that it's not a pointer-to-member.  */
18049   *type = NULL_TREE;
18050   /* And that there are no cv-qualifiers.  */
18051   *cv_quals = TYPE_UNQUALIFIED;
18052
18053   /* Peek at the next token.  */
18054   token = cp_lexer_peek_token (parser->lexer);
18055
18056   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
18057   if (token->type == CPP_MULT)
18058     code = INDIRECT_REF;
18059   else if (token->type == CPP_AND)
18060     code = ADDR_EXPR;
18061   else if ((cxx_dialect != cxx98) &&
18062            token->type == CPP_AND_AND) /* C++0x only */
18063     code = NON_LVALUE_EXPR;
18064
18065   if (code != ERROR_MARK)
18066     {
18067       /* Consume the `*', `&' or `&&'.  */
18068       cp_lexer_consume_token (parser->lexer);
18069
18070       /* A `*' can be followed by a cv-qualifier-seq, and so can a
18071          `&', if we are allowing GNU extensions.  (The only qualifier
18072          that can legally appear after `&' is `restrict', but that is
18073          enforced during semantic analysis.  */
18074       if (code == INDIRECT_REF
18075           || cp_parser_allow_gnu_extensions_p (parser))
18076         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18077
18078       attrs = cp_parser_std_attribute_spec_seq (parser);
18079       if (attributes != NULL)
18080         *attributes = attrs;
18081     }
18082   else
18083     {
18084       /* Try the pointer-to-member case.  */
18085       cp_parser_parse_tentatively (parser);
18086       /* Look for the optional `::' operator.  */
18087       cp_parser_global_scope_opt (parser,
18088                                   /*current_scope_valid_p=*/false);
18089       /* Look for the nested-name specifier.  */
18090       token = cp_lexer_peek_token (parser->lexer);
18091       cp_parser_nested_name_specifier (parser,
18092                                        /*typename_keyword_p=*/false,
18093                                        /*check_dependency_p=*/true,
18094                                        /*type_p=*/false,
18095                                        /*is_declaration=*/false);
18096       /* If we found it, and the next token is a `*', then we are
18097          indeed looking at a pointer-to-member operator.  */
18098       if (!cp_parser_error_occurred (parser)
18099           && cp_parser_require (parser, CPP_MULT, RT_MULT))
18100         {
18101           /* Indicate that the `*' operator was used.  */
18102           code = INDIRECT_REF;
18103
18104           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
18105             error_at (token->location, "%qD is a namespace", parser->scope);
18106           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
18107             error_at (token->location, "cannot form pointer to member of "
18108                       "non-class %q#T", parser->scope);
18109           else
18110             {
18111               /* The type of which the member is a member is given by the
18112                  current SCOPE.  */
18113               *type = parser->scope;
18114               /* The next name will not be qualified.  */
18115               parser->scope = NULL_TREE;
18116               parser->qualifying_scope = NULL_TREE;
18117               parser->object_scope = NULL_TREE;
18118               /* Look for optional c++11 attributes.  */
18119               attrs = cp_parser_std_attribute_spec_seq (parser);
18120               if (attributes != NULL)
18121                 *attributes = attrs;
18122               /* Look for the optional cv-qualifier-seq.  */
18123               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18124             }
18125         }
18126       /* If that didn't work we don't have a ptr-operator.  */
18127       if (!cp_parser_parse_definitely (parser))
18128         cp_parser_error (parser, "expected ptr-operator");
18129     }
18130
18131   return code;
18132 }
18133
18134 /* Parse an (optional) cv-qualifier-seq.
18135
18136    cv-qualifier-seq:
18137      cv-qualifier cv-qualifier-seq [opt]
18138
18139    cv-qualifier:
18140      const
18141      volatile
18142
18143    GNU Extension:
18144
18145    cv-qualifier:
18146      __restrict__
18147
18148    Returns a bitmask representing the cv-qualifiers.  */
18149
18150 static cp_cv_quals
18151 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
18152 {
18153   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
18154
18155   while (true)
18156     {
18157       cp_token *token;
18158       cp_cv_quals cv_qualifier;
18159
18160       /* Peek at the next token.  */
18161       token = cp_lexer_peek_token (parser->lexer);
18162       /* See if it's a cv-qualifier.  */
18163       switch (token->keyword)
18164         {
18165         case RID_CONST:
18166           cv_qualifier = TYPE_QUAL_CONST;
18167           break;
18168
18169         case RID_VOLATILE:
18170           cv_qualifier = TYPE_QUAL_VOLATILE;
18171           break;
18172
18173         case RID_RESTRICT:
18174           cv_qualifier = TYPE_QUAL_RESTRICT;
18175           break;
18176
18177         default:
18178           cv_qualifier = TYPE_UNQUALIFIED;
18179           break;
18180         }
18181
18182       if (!cv_qualifier)
18183         break;
18184
18185       if (cv_quals & cv_qualifier)
18186         {
18187           error_at (token->location, "duplicate cv-qualifier");
18188           cp_lexer_purge_token (parser->lexer);
18189         }
18190       else
18191         {
18192           cp_lexer_consume_token (parser->lexer);
18193           cv_quals |= cv_qualifier;
18194         }
18195     }
18196
18197   return cv_quals;
18198 }
18199
18200 /* Parse an (optional) ref-qualifier
18201
18202    ref-qualifier:
18203      &
18204      &&
18205
18206    Returns cp_ref_qualifier representing ref-qualifier. */
18207
18208 static cp_ref_qualifier
18209 cp_parser_ref_qualifier_opt (cp_parser* parser)
18210 {
18211   cp_ref_qualifier ref_qual = REF_QUAL_NONE;
18212
18213   /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532).  */
18214   if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
18215     return ref_qual;
18216
18217   while (true)
18218     {
18219       cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
18220       cp_token *token = cp_lexer_peek_token (parser->lexer);
18221
18222       switch (token->type)
18223         {
18224         case CPP_AND:
18225           curr_ref_qual = REF_QUAL_LVALUE;
18226           break;
18227
18228         case CPP_AND_AND:
18229           curr_ref_qual = REF_QUAL_RVALUE;
18230           break;
18231
18232         default:
18233           curr_ref_qual = REF_QUAL_NONE;
18234           break;
18235         }
18236
18237       if (!curr_ref_qual)
18238         break;
18239       else if (ref_qual)
18240         {
18241           error_at (token->location, "multiple ref-qualifiers");
18242           cp_lexer_purge_token (parser->lexer);
18243         }
18244       else
18245         {
18246           ref_qual = curr_ref_qual;
18247           cp_lexer_consume_token (parser->lexer);
18248         }
18249     }
18250
18251   return ref_qual;
18252 }
18253
18254 /* Parse an (optional) virt-specifier-seq.
18255
18256    virt-specifier-seq:
18257      virt-specifier virt-specifier-seq [opt]
18258
18259    virt-specifier:
18260      override
18261      final
18262
18263    Returns a bitmask representing the virt-specifiers.  */
18264
18265 static cp_virt_specifiers
18266 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18267 {
18268   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18269
18270   while (true)
18271     {
18272       cp_token *token;
18273       cp_virt_specifiers virt_specifier;
18274
18275       /* Peek at the next token.  */
18276       token = cp_lexer_peek_token (parser->lexer);
18277       /* See if it's a virt-specifier-qualifier.  */
18278       if (token->type != CPP_NAME)
18279         break;
18280       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18281         {
18282           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18283           virt_specifier = VIRT_SPEC_OVERRIDE;
18284         }
18285       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18286         {
18287           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18288           virt_specifier = VIRT_SPEC_FINAL;
18289         }
18290       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18291         {
18292           virt_specifier = VIRT_SPEC_FINAL;
18293         }
18294       else
18295         break;
18296
18297       if (virt_specifiers & virt_specifier)
18298         {
18299           error_at (token->location, "duplicate virt-specifier");
18300           cp_lexer_purge_token (parser->lexer);
18301         }
18302       else
18303         {
18304           cp_lexer_consume_token (parser->lexer);
18305           virt_specifiers |= virt_specifier;
18306         }
18307     }
18308   return virt_specifiers;
18309 }
18310
18311 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18312    is in scope even though it isn't real.  */
18313
18314 void
18315 inject_this_parameter (tree ctype, cp_cv_quals quals)
18316 {
18317   tree this_parm;
18318
18319   if (current_class_ptr)
18320     {
18321       /* We don't clear this between NSDMIs.  Is it already what we want?  */
18322       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18323       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18324           && cp_type_quals (type) == quals)
18325         return;
18326     }
18327
18328   this_parm = build_this_parm (ctype, quals);
18329   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
18330   current_class_ptr = NULL_TREE;
18331   current_class_ref
18332     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18333   current_class_ptr = this_parm;
18334 }
18335
18336 /* Return true iff our current scope is a non-static data member
18337    initializer.  */
18338
18339 bool
18340 parsing_nsdmi (void)
18341 {
18342   /* We recognize NSDMI context by the context-less 'this' pointer set up
18343      by the function above.  */
18344   if (current_class_ptr
18345       && TREE_CODE (current_class_ptr) == PARM_DECL
18346       && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18347     return true;
18348   return false;
18349 }
18350
18351 /* Parse a late-specified return type, if any.  This is not a separate
18352    non-terminal, but part of a function declarator, which looks like
18353
18354    -> trailing-type-specifier-seq abstract-declarator(opt)
18355
18356    Returns the type indicated by the type-id.
18357
18358    In addition to this this parses any queued up omp declare simd
18359    clauses and Cilk Plus SIMD-enabled function's vector attributes.
18360
18361    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18362    function.  */
18363
18364 static tree
18365 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18366                                 cp_cv_quals quals)
18367 {
18368   cp_token *token;
18369   tree type = NULL_TREE;
18370   bool declare_simd_p = (parser->omp_declare_simd
18371                          && declarator
18372                          && declarator->kind == cdk_id);
18373
18374   bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info 
18375                                 && declarator && declarator->kind == cdk_id);
18376   
18377   /* Peek at the next token.  */
18378   token = cp_lexer_peek_token (parser->lexer);
18379   /* A late-specified return type is indicated by an initial '->'. */
18380   if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18381     return NULL_TREE;
18382
18383   tree save_ccp = current_class_ptr;
18384   tree save_ccr = current_class_ref;
18385   if (quals >= 0)
18386     {
18387       /* DR 1207: 'this' is in scope in the trailing return type.  */
18388       inject_this_parameter (current_class_type, quals);
18389     }
18390
18391   if (token->type == CPP_DEREF)
18392     {
18393       /* Consume the ->.  */
18394       cp_lexer_consume_token (parser->lexer);
18395
18396       type = cp_parser_trailing_type_id (parser);
18397     }
18398
18399   if (cilk_simd_fn_vector_p)
18400     declarator->std_attributes
18401       = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18402                                                   declarator->std_attributes);
18403   if (declare_simd_p)
18404     declarator->std_attributes
18405       = cp_parser_late_parsing_omp_declare_simd (parser,
18406                                                  declarator->std_attributes);
18407
18408   if (quals >= 0)
18409     {
18410       current_class_ptr = save_ccp;
18411       current_class_ref = save_ccr;
18412     }
18413
18414   return type;
18415 }
18416
18417 /* Parse a declarator-id.
18418
18419    declarator-id:
18420      id-expression
18421      :: [opt] nested-name-specifier [opt] type-name
18422
18423    In the `id-expression' case, the value returned is as for
18424    cp_parser_id_expression if the id-expression was an unqualified-id.
18425    If the id-expression was a qualified-id, then a SCOPE_REF is
18426    returned.  The first operand is the scope (either a NAMESPACE_DECL
18427    or TREE_TYPE), but the second is still just a representation of an
18428    unqualified-id.  */
18429
18430 static tree
18431 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18432 {
18433   tree id;
18434   /* The expression must be an id-expression.  Assume that qualified
18435      names are the names of types so that:
18436
18437        template <class T>
18438        int S<T>::R::i = 3;
18439
18440      will work; we must treat `S<T>::R' as the name of a type.
18441      Similarly, assume that qualified names are templates, where
18442      required, so that:
18443
18444        template <class T>
18445        int S<T>::R<T>::i = 3;
18446
18447      will work, too.  */
18448   id = cp_parser_id_expression (parser,
18449                                 /*template_keyword_p=*/false,
18450                                 /*check_dependency_p=*/false,
18451                                 /*template_p=*/NULL,
18452                                 /*declarator_p=*/true,
18453                                 optional_p);
18454   if (id && BASELINK_P (id))
18455     id = BASELINK_FUNCTIONS (id);
18456   return id;
18457 }
18458
18459 /* Parse a type-id.
18460
18461    type-id:
18462      type-specifier-seq abstract-declarator [opt]
18463
18464    Returns the TYPE specified.  */
18465
18466 static tree
18467 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18468                      bool is_trailing_return)
18469 {
18470   cp_decl_specifier_seq type_specifier_seq;
18471   cp_declarator *abstract_declarator;
18472
18473   /* Parse the type-specifier-seq.  */
18474   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18475                                 is_trailing_return,
18476                                 &type_specifier_seq);
18477   if (type_specifier_seq.type == error_mark_node)
18478     return error_mark_node;
18479
18480   /* There might or might not be an abstract declarator.  */
18481   cp_parser_parse_tentatively (parser);
18482   /* Look for the declarator.  */
18483   abstract_declarator
18484     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18485                             /*parenthesized_p=*/NULL,
18486                             /*member_p=*/false,
18487                             /*friend_p=*/false);
18488   /* Check to see if there really was a declarator.  */
18489   if (!cp_parser_parse_definitely (parser))
18490     abstract_declarator = NULL;
18491
18492   if (type_specifier_seq.type
18493       /* None of the valid uses of 'auto' in C++14 involve the type-id
18494          nonterminal, but it is valid in a trailing-return-type.  */
18495       && !(cxx_dialect >= cxx14 && is_trailing_return)
18496       && type_uses_auto (type_specifier_seq.type))
18497     {
18498       /* A type-id with type 'auto' is only ok if the abstract declarator
18499          is a function declarator with a late-specified return type.  */
18500       if (abstract_declarator
18501           && abstract_declarator->kind == cdk_function
18502           && abstract_declarator->u.function.late_return_type)
18503         /* OK */;
18504       else
18505         {
18506           error ("invalid use of %<auto%>");
18507           return error_mark_node;
18508         }
18509     }
18510   
18511   return groktypename (&type_specifier_seq, abstract_declarator,
18512                        is_template_arg);
18513 }
18514
18515 static tree cp_parser_type_id (cp_parser *parser)
18516 {
18517   return cp_parser_type_id_1 (parser, false, false);
18518 }
18519
18520 static tree cp_parser_template_type_arg (cp_parser *parser)
18521 {
18522   tree r;
18523   const char *saved_message = parser->type_definition_forbidden_message;
18524   parser->type_definition_forbidden_message
18525     = G_("types may not be defined in template arguments");
18526   r = cp_parser_type_id_1 (parser, true, false);
18527   parser->type_definition_forbidden_message = saved_message;
18528   if (cxx_dialect >= cxx14 && type_uses_auto (r))
18529     {
18530       error ("invalid use of %<auto%> in template argument");
18531       r = error_mark_node;
18532     }
18533   return r;
18534 }
18535
18536 static tree cp_parser_trailing_type_id (cp_parser *parser)
18537 {
18538   return cp_parser_type_id_1 (parser, false, true);
18539 }
18540
18541 /* Parse a type-specifier-seq.
18542
18543    type-specifier-seq:
18544      type-specifier type-specifier-seq [opt]
18545
18546    GNU extension:
18547
18548    type-specifier-seq:
18549      attributes type-specifier-seq [opt]
18550
18551    If IS_DECLARATION is true, we are at the start of a "condition" or
18552    exception-declaration, so we might be followed by a declarator-id.
18553
18554    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18555    i.e. we've just seen "->".
18556
18557    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
18558
18559 static void
18560 cp_parser_type_specifier_seq (cp_parser* parser,
18561                               bool is_declaration,
18562                               bool is_trailing_return,
18563                               cp_decl_specifier_seq *type_specifier_seq)
18564 {
18565   bool seen_type_specifier = false;
18566   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18567   cp_token *start_token = NULL;
18568
18569   /* Clear the TYPE_SPECIFIER_SEQ.  */
18570   clear_decl_specs (type_specifier_seq);
18571
18572   /* In the context of a trailing return type, enum E { } is an
18573      elaborated-type-specifier followed by a function-body, not an
18574      enum-specifier.  */
18575   if (is_trailing_return)
18576     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18577
18578   /* Parse the type-specifiers and attributes.  */
18579   while (true)
18580     {
18581       tree type_specifier;
18582       bool is_cv_qualifier;
18583
18584       /* Check for attributes first.  */
18585       if (cp_next_tokens_can_be_attribute_p (parser))
18586         {
18587           type_specifier_seq->attributes =
18588             chainon (type_specifier_seq->attributes,
18589                      cp_parser_attributes_opt (parser));
18590           continue;
18591         }
18592
18593       /* record the token of the beginning of the type specifier seq,
18594          for error reporting purposes*/
18595      if (!start_token)
18596        start_token = cp_lexer_peek_token (parser->lexer);
18597
18598       /* Look for the type-specifier.  */
18599       type_specifier = cp_parser_type_specifier (parser,
18600                                                  flags,
18601                                                  type_specifier_seq,
18602                                                  /*is_declaration=*/false,
18603                                                  NULL,
18604                                                  &is_cv_qualifier);
18605       if (!type_specifier)
18606         {
18607           /* If the first type-specifier could not be found, this is not a
18608              type-specifier-seq at all.  */
18609           if (!seen_type_specifier)
18610             {
18611               /* Set in_declarator_p to avoid skipping to the semicolon.  */
18612               int in_decl = parser->in_declarator_p;
18613               parser->in_declarator_p = true;
18614
18615               if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18616                   || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18617                 cp_parser_error (parser, "expected type-specifier");
18618
18619               parser->in_declarator_p = in_decl;
18620
18621               type_specifier_seq->type = error_mark_node;
18622               return;
18623             }
18624           /* If subsequent type-specifiers could not be found, the
18625              type-specifier-seq is complete.  */
18626           break;
18627         }
18628
18629       seen_type_specifier = true;
18630       /* The standard says that a condition can be:
18631
18632             type-specifier-seq declarator = assignment-expression
18633
18634          However, given:
18635
18636            struct S {};
18637            if (int S = ...)
18638
18639          we should treat the "S" as a declarator, not as a
18640          type-specifier.  The standard doesn't say that explicitly for
18641          type-specifier-seq, but it does say that for
18642          decl-specifier-seq in an ordinary declaration.  Perhaps it
18643          would be clearer just to allow a decl-specifier-seq here, and
18644          then add a semantic restriction that if any decl-specifiers
18645          that are not type-specifiers appear, the program is invalid.  */
18646       if (is_declaration && !is_cv_qualifier)
18647         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18648     }
18649 }
18650
18651 /* Return whether the function currently being declared has an associated
18652    template parameter list.  */
18653
18654 static bool
18655 function_being_declared_is_template_p (cp_parser* parser)
18656 {
18657   if (!current_template_parms || processing_template_parmlist)
18658     return false;
18659
18660   if (parser->implicit_template_scope)
18661     return true;
18662
18663   if (at_class_scope_p ()
18664       && TYPE_BEING_DEFINED (current_class_type))
18665     return parser->num_template_parameter_lists != 0;
18666
18667   return ((int) parser->num_template_parameter_lists > template_class_depth
18668           (current_class_type));
18669 }
18670
18671 /* Parse a parameter-declaration-clause.
18672
18673    parameter-declaration-clause:
18674      parameter-declaration-list [opt] ... [opt]
18675      parameter-declaration-list , ...
18676
18677    Returns a representation for the parameter declarations.  A return
18678    value of NULL indicates a parameter-declaration-clause consisting
18679    only of an ellipsis.  */
18680
18681 static tree
18682 cp_parser_parameter_declaration_clause (cp_parser* parser)
18683 {
18684   tree parameters;
18685   cp_token *token;
18686   bool ellipsis_p;
18687   bool is_error;
18688
18689   struct cleanup {
18690     cp_parser* parser;
18691     int auto_is_implicit_function_template_parm_p;
18692     ~cleanup() {
18693       parser->auto_is_implicit_function_template_parm_p
18694         = auto_is_implicit_function_template_parm_p;
18695     }
18696   } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18697
18698   (void) cleanup;
18699
18700   if (!processing_specialization
18701       && !processing_template_parmlist
18702       && !processing_explicit_instantiation)
18703     if (!current_function_decl
18704         || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18705       parser->auto_is_implicit_function_template_parm_p = true;
18706
18707   /* Peek at the next token.  */
18708   token = cp_lexer_peek_token (parser->lexer);
18709   /* Check for trivial parameter-declaration-clauses.  */
18710   if (token->type == CPP_ELLIPSIS)
18711     {
18712       /* Consume the `...' token.  */
18713       cp_lexer_consume_token (parser->lexer);
18714       return NULL_TREE;
18715     }
18716   else if (token->type == CPP_CLOSE_PAREN)
18717     /* There are no parameters.  */
18718     {
18719 #ifndef NO_IMPLICIT_EXTERN_C
18720       if (in_system_header_at (input_location)
18721           && current_class_type == NULL
18722           && current_lang_name == lang_name_c)
18723         return NULL_TREE;
18724       else
18725 #endif
18726         return void_list_node;
18727     }
18728   /* Check for `(void)', too, which is a special case.  */
18729   else if (token->keyword == RID_VOID
18730            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18731                == CPP_CLOSE_PAREN))
18732     {
18733       /* Consume the `void' token.  */
18734       cp_lexer_consume_token (parser->lexer);
18735       /* There are no parameters.  */
18736       return void_list_node;
18737     }
18738
18739   /* Parse the parameter-declaration-list.  */
18740   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18741   /* If a parse error occurred while parsing the
18742      parameter-declaration-list, then the entire
18743      parameter-declaration-clause is erroneous.  */
18744   if (is_error)
18745     return NULL;
18746
18747   /* Peek at the next token.  */
18748   token = cp_lexer_peek_token (parser->lexer);
18749   /* If it's a `,', the clause should terminate with an ellipsis.  */
18750   if (token->type == CPP_COMMA)
18751     {
18752       /* Consume the `,'.  */
18753       cp_lexer_consume_token (parser->lexer);
18754       /* Expect an ellipsis.  */
18755       ellipsis_p
18756         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18757     }
18758   /* It might also be `...' if the optional trailing `,' was
18759      omitted.  */
18760   else if (token->type == CPP_ELLIPSIS)
18761     {
18762       /* Consume the `...' token.  */
18763       cp_lexer_consume_token (parser->lexer);
18764       /* And remember that we saw it.  */
18765       ellipsis_p = true;
18766     }
18767   else
18768     ellipsis_p = false;
18769
18770   /* Finish the parameter list.  */
18771   if (!ellipsis_p)
18772     parameters = chainon (parameters, void_list_node);
18773
18774   return parameters;
18775 }
18776
18777 /* Parse a parameter-declaration-list.
18778
18779    parameter-declaration-list:
18780      parameter-declaration
18781      parameter-declaration-list , parameter-declaration
18782
18783    Returns a representation of the parameter-declaration-list, as for
18784    cp_parser_parameter_declaration_clause.  However, the
18785    `void_list_node' is never appended to the list.  Upon return,
18786    *IS_ERROR will be true iff an error occurred.  */
18787
18788 static tree
18789 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18790 {
18791   tree parameters = NULL_TREE;
18792   tree *tail = &parameters;
18793   bool saved_in_unbraced_linkage_specification_p;
18794   int index = 0;
18795
18796   /* Assume all will go well.  */
18797   *is_error = false;
18798   /* The special considerations that apply to a function within an
18799      unbraced linkage specifications do not apply to the parameters
18800      to the function.  */
18801   saved_in_unbraced_linkage_specification_p
18802     = parser->in_unbraced_linkage_specification_p;
18803   parser->in_unbraced_linkage_specification_p = false;
18804
18805   /* Look for more parameters.  */
18806   while (true)
18807     {
18808       cp_parameter_declarator *parameter;
18809       tree decl = error_mark_node;
18810       bool parenthesized_p = false;
18811       int template_parm_idx = (function_being_declared_is_template_p (parser)?
18812                                TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18813                                                 (current_template_parms)) : 0);
18814
18815       /* Parse the parameter.  */
18816       parameter
18817         = cp_parser_parameter_declaration (parser,
18818                                            /*template_parm_p=*/false,
18819                                            &parenthesized_p);
18820
18821       /* We don't know yet if the enclosing context is deprecated, so wait
18822          and warn in grokparms if appropriate.  */
18823       deprecated_state = DEPRECATED_SUPPRESS;
18824
18825       if (parameter)
18826         {
18827           /* If a function parameter pack was specified and an implicit template
18828              parameter was introduced during cp_parser_parameter_declaration,
18829              change any implicit parameters introduced into packs.  */
18830           if (parser->implicit_template_parms
18831               && parameter->declarator
18832               && parameter->declarator->parameter_pack_p)
18833             {
18834               int latest_template_parm_idx = TREE_VEC_LENGTH
18835                 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18836
18837               if (latest_template_parm_idx != template_parm_idx)
18838                 parameter->decl_specifiers.type = convert_generic_types_to_packs
18839                   (parameter->decl_specifiers.type,
18840                    template_parm_idx, latest_template_parm_idx);
18841             }
18842
18843           decl = grokdeclarator (parameter->declarator,
18844                                  &parameter->decl_specifiers,
18845                                  PARM,
18846                                  parameter->default_argument != NULL_TREE,
18847                                  &parameter->decl_specifiers.attributes);
18848         }
18849
18850       deprecated_state = DEPRECATED_NORMAL;
18851
18852       /* If a parse error occurred parsing the parameter declaration,
18853          then the entire parameter-declaration-list is erroneous.  */
18854       if (decl == error_mark_node)
18855         {
18856           *is_error = true;
18857           parameters = error_mark_node;
18858           break;
18859         }
18860
18861       if (parameter->decl_specifiers.attributes)
18862         cplus_decl_attributes (&decl,
18863                                parameter->decl_specifiers.attributes,
18864                                0);
18865       if (DECL_NAME (decl))
18866         decl = pushdecl (decl);
18867
18868       if (decl != error_mark_node)
18869         {
18870           retrofit_lang_decl (decl);
18871           DECL_PARM_INDEX (decl) = ++index;
18872           DECL_PARM_LEVEL (decl) = function_parm_depth ();
18873         }
18874
18875       /* Add the new parameter to the list.  */
18876       *tail = build_tree_list (parameter->default_argument, decl);
18877       tail = &TREE_CHAIN (*tail);
18878
18879       /* Peek at the next token.  */
18880       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18881           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18882           /* These are for Objective-C++ */
18883           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18884           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18885         /* The parameter-declaration-list is complete.  */
18886         break;
18887       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18888         {
18889           cp_token *token;
18890
18891           /* Peek at the next token.  */
18892           token = cp_lexer_peek_nth_token (parser->lexer, 2);
18893           /* If it's an ellipsis, then the list is complete.  */
18894           if (token->type == CPP_ELLIPSIS)
18895             break;
18896           /* Otherwise, there must be more parameters.  Consume the
18897              `,'.  */
18898           cp_lexer_consume_token (parser->lexer);
18899           /* When parsing something like:
18900
18901                 int i(float f, double d)
18902
18903              we can tell after seeing the declaration for "f" that we
18904              are not looking at an initialization of a variable "i",
18905              but rather at the declaration of a function "i".
18906
18907              Due to the fact that the parsing of template arguments
18908              (as specified to a template-id) requires backtracking we
18909              cannot use this technique when inside a template argument
18910              list.  */
18911           if (!parser->in_template_argument_list_p
18912               && !parser->in_type_id_in_expr_p
18913               && cp_parser_uncommitted_to_tentative_parse_p (parser)
18914               /* However, a parameter-declaration of the form
18915                  "float(f)" (which is a valid declaration of a
18916                  parameter "f") can also be interpreted as an
18917                  expression (the conversion of "f" to "float").  */
18918               && !parenthesized_p)
18919             cp_parser_commit_to_tentative_parse (parser);
18920         }
18921       else
18922         {
18923           cp_parser_error (parser, "expected %<,%> or %<...%>");
18924           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18925             cp_parser_skip_to_closing_parenthesis (parser,
18926                                                    /*recovering=*/true,
18927                                                    /*or_comma=*/false,
18928                                                    /*consume_paren=*/false);
18929           break;
18930         }
18931     }
18932
18933   parser->in_unbraced_linkage_specification_p
18934     = saved_in_unbraced_linkage_specification_p;
18935
18936   /* Reset implicit_template_scope if we are about to leave the function
18937      parameter list that introduced it.  Note that for out-of-line member
18938      definitions, there will be one or more class scopes before we get to
18939      the template parameter scope.  */
18940
18941   if (cp_binding_level *its = parser->implicit_template_scope)
18942     if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18943       {
18944         while (maybe_its->kind == sk_class)
18945           maybe_its = maybe_its->level_chain;
18946         if (maybe_its == its)
18947           {
18948             parser->implicit_template_parms = 0;
18949             parser->implicit_template_scope = 0;
18950           }
18951       }
18952
18953   return parameters;
18954 }
18955
18956 /* Parse a parameter declaration.
18957
18958    parameter-declaration:
18959      decl-specifier-seq ... [opt] declarator
18960      decl-specifier-seq declarator = assignment-expression
18961      decl-specifier-seq ... [opt] abstract-declarator [opt]
18962      decl-specifier-seq abstract-declarator [opt] = assignment-expression
18963
18964    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18965    declares a template parameter.  (In that case, a non-nested `>'
18966    token encountered during the parsing of the assignment-expression
18967    is not interpreted as a greater-than operator.)
18968
18969    Returns a representation of the parameter, or NULL if an error
18970    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18971    true iff the declarator is of the form "(p)".  */
18972
18973 static cp_parameter_declarator *
18974 cp_parser_parameter_declaration (cp_parser *parser,
18975                                  bool template_parm_p,
18976                                  bool *parenthesized_p)
18977 {
18978   int declares_class_or_enum;
18979   cp_decl_specifier_seq decl_specifiers;
18980   cp_declarator *declarator;
18981   tree default_argument;
18982   cp_token *token = NULL, *declarator_token_start = NULL;
18983   const char *saved_message;
18984
18985   /* In a template parameter, `>' is not an operator.
18986
18987      [temp.param]
18988
18989      When parsing a default template-argument for a non-type
18990      template-parameter, the first non-nested `>' is taken as the end
18991      of the template parameter-list rather than a greater-than
18992      operator.  */
18993
18994   /* Type definitions may not appear in parameter types.  */
18995   saved_message = parser->type_definition_forbidden_message;
18996   parser->type_definition_forbidden_message
18997     = G_("types may not be defined in parameter types");
18998
18999   /* Parse the declaration-specifiers.  */
19000   cp_parser_decl_specifier_seq (parser,
19001                                 CP_PARSER_FLAGS_NONE,
19002                                 &decl_specifiers,
19003                                 &declares_class_or_enum);
19004
19005   /* Complain about missing 'typename' or other invalid type names.  */
19006   if (!decl_specifiers.any_type_specifiers_p
19007       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19008     decl_specifiers.type = error_mark_node;
19009
19010   /* If an error occurred, there's no reason to attempt to parse the
19011      rest of the declaration.  */
19012   if (cp_parser_error_occurred (parser))
19013     {
19014       parser->type_definition_forbidden_message = saved_message;
19015       return NULL;
19016     }
19017
19018   /* Peek at the next token.  */
19019   token = cp_lexer_peek_token (parser->lexer);
19020
19021   /* If the next token is a `)', `,', `=', `>', or `...', then there
19022      is no declarator. However, when variadic templates are enabled,
19023      there may be a declarator following `...'.  */
19024   if (token->type == CPP_CLOSE_PAREN
19025       || token->type == CPP_COMMA
19026       || token->type == CPP_EQ
19027       || token->type == CPP_GREATER)
19028     {
19029       declarator = NULL;
19030       if (parenthesized_p)
19031         *parenthesized_p = false;
19032     }
19033   /* Otherwise, there should be a declarator.  */
19034   else
19035     {
19036       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19037       parser->default_arg_ok_p = false;
19038
19039       /* After seeing a decl-specifier-seq, if the next token is not a
19040          "(", there is no possibility that the code is a valid
19041          expression.  Therefore, if parsing tentatively, we commit at
19042          this point.  */
19043       if (!parser->in_template_argument_list_p
19044           /* In an expression context, having seen:
19045
19046                (int((char ...
19047
19048              we cannot be sure whether we are looking at a
19049              function-type (taking a "char" as a parameter) or a cast
19050              of some object of type "char" to "int".  */
19051           && !parser->in_type_id_in_expr_p
19052           && cp_parser_uncommitted_to_tentative_parse_p (parser)
19053           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19054           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
19055         cp_parser_commit_to_tentative_parse (parser);
19056       /* Parse the declarator.  */
19057       declarator_token_start = token;
19058       declarator = cp_parser_declarator (parser,
19059                                          CP_PARSER_DECLARATOR_EITHER,
19060                                          /*ctor_dtor_or_conv_p=*/NULL,
19061                                          parenthesized_p,
19062                                          /*member_p=*/false,
19063                                          /*friend_p=*/false);
19064       parser->default_arg_ok_p = saved_default_arg_ok_p;
19065       /* After the declarator, allow more attributes.  */
19066       decl_specifiers.attributes
19067         = chainon (decl_specifiers.attributes,
19068                    cp_parser_attributes_opt (parser));
19069     }
19070
19071   /* If the next token is an ellipsis, and we have not seen a
19072      declarator name, and the type of the declarator contains parameter
19073      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
19074      a parameter pack expansion expression. Otherwise, leave the
19075      ellipsis for a C-style variadic function. */
19076   token = cp_lexer_peek_token (parser->lexer);
19077   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19078     {
19079       tree type = decl_specifiers.type;
19080
19081       if (type && DECL_P (type))
19082         type = TREE_TYPE (type);
19083
19084       if (type
19085           && TREE_CODE (type) != TYPE_PACK_EXPANSION
19086           && declarator_can_be_parameter_pack (declarator)
19087           && (!declarator || !declarator->parameter_pack_p)
19088           && uses_parameter_packs (type))
19089         {
19090           /* Consume the `...'. */
19091           cp_lexer_consume_token (parser->lexer);
19092           maybe_warn_variadic_templates ();
19093           
19094           /* Build a pack expansion type */
19095           if (declarator)
19096             declarator->parameter_pack_p = true;
19097           else
19098             decl_specifiers.type = make_pack_expansion (type);
19099         }
19100     }
19101
19102   /* The restriction on defining new types applies only to the type
19103      of the parameter, not to the default argument.  */
19104   parser->type_definition_forbidden_message = saved_message;
19105
19106   /* If the next token is `=', then process a default argument.  */
19107   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19108     {
19109       token = cp_lexer_peek_token (parser->lexer);
19110       /* If we are defining a class, then the tokens that make up the
19111          default argument must be saved and processed later.  */
19112       if (!template_parm_p && at_class_scope_p ()
19113           && TYPE_BEING_DEFINED (current_class_type)
19114           && !LAMBDA_TYPE_P (current_class_type))
19115         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
19116       /* Outside of a class definition, we can just parse the
19117          assignment-expression.  */
19118       else
19119         default_argument
19120           = cp_parser_default_argument (parser, template_parm_p);
19121
19122       if (!parser->default_arg_ok_p)
19123         {
19124           if (flag_permissive)
19125             warning (0, "deprecated use of default argument for parameter of non-function");
19126           else
19127             {
19128               error_at (token->location,
19129                         "default arguments are only "
19130                         "permitted for function parameters");
19131               default_argument = NULL_TREE;
19132             }
19133         }
19134       else if ((declarator && declarator->parameter_pack_p)
19135                || (decl_specifiers.type
19136                    && PACK_EXPANSION_P (decl_specifiers.type)))
19137         {
19138           /* Find the name of the parameter pack.  */     
19139           cp_declarator *id_declarator = declarator;
19140           while (id_declarator && id_declarator->kind != cdk_id)
19141             id_declarator = id_declarator->declarator;
19142           
19143           if (id_declarator && id_declarator->kind == cdk_id)
19144             error_at (declarator_token_start->location,
19145                       template_parm_p
19146                       ? G_("template parameter pack %qD "
19147                            "cannot have a default argument")
19148                       : G_("parameter pack %qD cannot have "
19149                            "a default argument"),
19150                       id_declarator->u.id.unqualified_name);
19151           else
19152             error_at (declarator_token_start->location,
19153                       template_parm_p
19154                       ? G_("template parameter pack cannot have "
19155                            "a default argument")
19156                       : G_("parameter pack cannot have a "
19157                            "default argument"));
19158
19159           default_argument = NULL_TREE;
19160         }
19161     }
19162   else
19163     default_argument = NULL_TREE;
19164
19165   return make_parameter_declarator (&decl_specifiers,
19166                                     declarator,
19167                                     default_argument);
19168 }
19169
19170 /* Parse a default argument and return it.
19171
19172    TEMPLATE_PARM_P is true if this is a default argument for a
19173    non-type template parameter.  */
19174 static tree
19175 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
19176 {
19177   tree default_argument = NULL_TREE;
19178   bool saved_greater_than_is_operator_p;
19179   bool saved_local_variables_forbidden_p;
19180   bool non_constant_p, is_direct_init;
19181
19182   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
19183      set correctly.  */
19184   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
19185   parser->greater_than_is_operator_p = !template_parm_p;
19186   /* Local variable names (and the `this' keyword) may not
19187      appear in a default argument.  */
19188   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19189   parser->local_variables_forbidden_p = true;
19190   /* Parse the assignment-expression.  */
19191   if (template_parm_p)
19192     push_deferring_access_checks (dk_no_deferred);
19193   tree saved_class_ptr = NULL_TREE;
19194   tree saved_class_ref = NULL_TREE;
19195   /* The "this" pointer is not valid in a default argument.  */
19196   if (cfun)
19197     {
19198       saved_class_ptr = current_class_ptr;
19199       cp_function_chain->x_current_class_ptr = NULL_TREE;
19200       saved_class_ref = current_class_ref;
19201       cp_function_chain->x_current_class_ref = NULL_TREE;
19202     }
19203   default_argument
19204     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
19205   /* Restore the "this" pointer.  */
19206   if (cfun)
19207     {
19208       cp_function_chain->x_current_class_ptr = saved_class_ptr;
19209       cp_function_chain->x_current_class_ref = saved_class_ref;
19210     }
19211   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
19212     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19213   if (template_parm_p)
19214     pop_deferring_access_checks ();
19215   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
19216   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19217
19218   return default_argument;
19219 }
19220
19221 /* Parse a function-body.
19222
19223    function-body:
19224      compound_statement  */
19225
19226 static void
19227 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
19228 {
19229   cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
19230 }
19231
19232 /* Parse a ctor-initializer-opt followed by a function-body.  Return
19233    true if a ctor-initializer was present.  When IN_FUNCTION_TRY_BLOCK
19234    is true we are parsing a function-try-block.  */
19235
19236 static bool
19237 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
19238                                                   bool in_function_try_block)
19239 {
19240   tree body, list;
19241   bool ctor_initializer_p;
19242   const bool check_body_p =
19243      DECL_CONSTRUCTOR_P (current_function_decl)
19244      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
19245   tree last = NULL;
19246
19247   /* Begin the function body.  */
19248   body = begin_function_body ();
19249   /* Parse the optional ctor-initializer.  */
19250   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
19251
19252   /* If we're parsing a constexpr constructor definition, we need
19253      to check that the constructor body is indeed empty.  However,
19254      before we get to cp_parser_function_body lot of junk has been
19255      generated, so we can't just check that we have an empty block.
19256      Rather we take a snapshot of the outermost block, and check whether
19257      cp_parser_function_body changed its state.  */
19258   if (check_body_p)
19259     {
19260       list = cur_stmt_list;
19261       if (STATEMENT_LIST_TAIL (list))
19262         last = STATEMENT_LIST_TAIL (list)->stmt;
19263     }
19264   /* Parse the function-body.  */
19265   cp_parser_function_body (parser, in_function_try_block);
19266   if (check_body_p)
19267     check_constexpr_ctor_body (last, list, /*complain=*/true);
19268   /* Finish the function body.  */
19269   finish_function_body (body);
19270
19271   return ctor_initializer_p;
19272 }
19273
19274 /* Parse an initializer.
19275
19276    initializer:
19277      = initializer-clause
19278      ( expression-list )
19279
19280    Returns an expression representing the initializer.  If no
19281    initializer is present, NULL_TREE is returned.
19282
19283    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19284    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
19285    set to TRUE if there is no initializer present.  If there is an
19286    initializer, and it is not a constant-expression, *NON_CONSTANT_P
19287    is set to true; otherwise it is set to false.  */
19288
19289 static tree
19290 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19291                        bool* non_constant_p)
19292 {
19293   cp_token *token;
19294   tree init;
19295
19296   /* Peek at the next token.  */
19297   token = cp_lexer_peek_token (parser->lexer);
19298
19299   /* Let our caller know whether or not this initializer was
19300      parenthesized.  */
19301   *is_direct_init = (token->type != CPP_EQ);
19302   /* Assume that the initializer is constant.  */
19303   *non_constant_p = false;
19304
19305   if (token->type == CPP_EQ)
19306     {
19307       /* Consume the `='.  */
19308       cp_lexer_consume_token (parser->lexer);
19309       /* Parse the initializer-clause.  */
19310       init = cp_parser_initializer_clause (parser, non_constant_p);
19311     }
19312   else if (token->type == CPP_OPEN_PAREN)
19313     {
19314       vec<tree, va_gc> *vec;
19315       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19316                                                      /*cast_p=*/false,
19317                                                      /*allow_expansion_p=*/true,
19318                                                      non_constant_p);
19319       if (vec == NULL)
19320         return error_mark_node;
19321       init = build_tree_list_vec (vec);
19322       release_tree_vector (vec);
19323     }
19324   else if (token->type == CPP_OPEN_BRACE)
19325     {
19326       cp_lexer_set_source_position (parser->lexer);
19327       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19328       init = cp_parser_braced_list (parser, non_constant_p);
19329       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19330     }
19331   else
19332     {
19333       /* Anything else is an error.  */
19334       cp_parser_error (parser, "expected initializer");
19335       init = error_mark_node;
19336     }
19337
19338   return init;
19339 }
19340
19341 /* Parse an initializer-clause.
19342
19343    initializer-clause:
19344      assignment-expression
19345      braced-init-list
19346
19347    Returns an expression representing the initializer.
19348
19349    If the `assignment-expression' production is used the value
19350    returned is simply a representation for the expression.
19351
19352    Otherwise, calls cp_parser_braced_list.  */
19353
19354 static tree
19355 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19356 {
19357   tree initializer;
19358
19359   /* Assume the expression is constant.  */
19360   *non_constant_p = false;
19361
19362   /* If it is not a `{', then we are looking at an
19363      assignment-expression.  */
19364   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19365     {
19366       initializer
19367         = cp_parser_constant_expression (parser,
19368                                         /*allow_non_constant_p=*/true,
19369                                         non_constant_p);
19370     }
19371   else
19372     initializer = cp_parser_braced_list (parser, non_constant_p);
19373
19374   return initializer;
19375 }
19376
19377 /* Parse a brace-enclosed initializer list.
19378
19379    braced-init-list:
19380      { initializer-list , [opt] }
19381      { }
19382
19383    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
19384    the elements of the initializer-list (or NULL, if the last
19385    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
19386    NULL_TREE.  There is no way to detect whether or not the optional
19387    trailing `,' was provided.  NON_CONSTANT_P is as for
19388    cp_parser_initializer.  */     
19389
19390 static tree
19391 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19392 {
19393   tree initializer;
19394
19395   /* Consume the `{' token.  */
19396   cp_lexer_consume_token (parser->lexer);
19397   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
19398   initializer = make_node (CONSTRUCTOR);
19399   /* If it's not a `}', then there is a non-trivial initializer.  */
19400   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19401     {
19402       /* Parse the initializer list.  */
19403       CONSTRUCTOR_ELTS (initializer)
19404         = cp_parser_initializer_list (parser, non_constant_p);
19405       /* A trailing `,' token is allowed.  */
19406       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19407         cp_lexer_consume_token (parser->lexer);
19408     }
19409   else
19410     *non_constant_p = false;
19411   /* Now, there should be a trailing `}'.  */
19412   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19413   TREE_TYPE (initializer) = init_list_type_node;
19414   return initializer;
19415 }
19416
19417 /* Consume tokens up to, and including, the next non-nested closing `]'.
19418    Returns true iff we found a closing `]'.  */
19419
19420 static bool
19421 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
19422 {
19423   unsigned square_depth = 0;
19424
19425   while (true)
19426     {
19427       cp_token * token = cp_lexer_peek_token (parser->lexer);
19428
19429       switch (token->type)
19430         {
19431         case CPP_EOF:
19432         case CPP_PRAGMA_EOL:
19433           /* If we've run out of tokens, then there is no closing `]'.  */
19434           return false;
19435
19436         case CPP_OPEN_SQUARE:
19437           ++square_depth;
19438           break;
19439
19440         case CPP_CLOSE_SQUARE:
19441           if (!square_depth--)
19442             {
19443               cp_lexer_consume_token (parser->lexer);
19444               return true;
19445             }
19446           break;
19447
19448         default:
19449           break;
19450         }
19451
19452       /* Consume the token.  */
19453       cp_lexer_consume_token (parser->lexer);
19454     }
19455 }
19456
19457 /* Return true if we are looking at an array-designator, false otherwise.  */
19458
19459 static bool
19460 cp_parser_array_designator_p (cp_parser *parser)
19461 {
19462   /* Consume the `['.  */
19463   cp_lexer_consume_token (parser->lexer);
19464
19465   cp_lexer_save_tokens (parser->lexer);
19466
19467   /* Skip tokens until the next token is a closing square bracket.
19468      If we find the closing `]', and the next token is a `=', then
19469      we are looking at an array designator.  */
19470   bool array_designator_p
19471     = (cp_parser_skip_to_closing_square_bracket (parser)
19472        && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
19473   
19474   /* Roll back the tokens we skipped.  */
19475   cp_lexer_rollback_tokens (parser->lexer);
19476
19477   return array_designator_p;
19478 }
19479
19480 /* Parse an initializer-list.
19481
19482    initializer-list:
19483      initializer-clause ... [opt]
19484      initializer-list , initializer-clause ... [opt]
19485
19486    GNU Extension:
19487
19488    initializer-list:
19489      designation initializer-clause ...[opt]
19490      initializer-list , designation initializer-clause ...[opt]
19491
19492    designation:
19493      . identifier =
19494      identifier :
19495      [ constant-expression ] =
19496
19497    Returns a vec of constructor_elt.  The VALUE of each elt is an expression
19498    for the initializer.  If the INDEX of the elt is non-NULL, it is the
19499    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
19500    as for cp_parser_initializer.  */
19501
19502 static vec<constructor_elt, va_gc> *
19503 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19504 {
19505   vec<constructor_elt, va_gc> *v = NULL;
19506
19507   /* Assume all of the expressions are constant.  */
19508   *non_constant_p = false;
19509
19510   /* Parse the rest of the list.  */
19511   while (true)
19512     {
19513       cp_token *token;
19514       tree designator;
19515       tree initializer;
19516       bool clause_non_constant_p;
19517
19518       /* If the next token is an identifier and the following one is a
19519          colon, we are looking at the GNU designated-initializer
19520          syntax.  */
19521       if (cp_parser_allow_gnu_extensions_p (parser)
19522           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19523           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19524         {
19525           /* Warn the user that they are using an extension.  */
19526           pedwarn (input_location, OPT_Wpedantic, 
19527                    "ISO C++ does not allow designated initializers");
19528           /* Consume the identifier.  */
19529           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19530           /* Consume the `:'.  */
19531           cp_lexer_consume_token (parser->lexer);
19532         }
19533       /* Also handle the C99 syntax, '. id ='.  */
19534       else if (cp_parser_allow_gnu_extensions_p (parser)
19535                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19536                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19537                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19538         {
19539           /* Warn the user that they are using an extension.  */
19540           pedwarn (input_location, OPT_Wpedantic,
19541                    "ISO C++ does not allow C99 designated initializers");
19542           /* Consume the `.'.  */
19543           cp_lexer_consume_token (parser->lexer);
19544           /* Consume the identifier.  */
19545           designator = cp_lexer_consume_token (parser->lexer)->u.value;
19546           /* Consume the `='.  */
19547           cp_lexer_consume_token (parser->lexer);
19548         }
19549       /* Also handle C99 array designators, '[ const ] ='.  */
19550       else if (cp_parser_allow_gnu_extensions_p (parser)
19551                && !c_dialect_objc ()
19552                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19553         {
19554           /* In C++11, [ could start a lambda-introducer.  */
19555           bool non_const = false;
19556
19557           cp_parser_parse_tentatively (parser);
19558
19559           if (!cp_parser_array_designator_p (parser))
19560             {
19561               cp_parser_simulate_error (parser);
19562               designator = NULL_TREE;
19563             }
19564           else
19565             {
19566               designator = cp_parser_constant_expression (parser, true,
19567                                                           &non_const);
19568               cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19569               cp_parser_require (parser, CPP_EQ, RT_EQ);
19570             }
19571
19572           if (!cp_parser_parse_definitely (parser))
19573             designator = NULL_TREE;
19574           else if (non_const)
19575             require_potential_rvalue_constant_expression (designator);
19576         }
19577       else
19578         designator = NULL_TREE;
19579
19580       /* Parse the initializer.  */
19581       initializer = cp_parser_initializer_clause (parser,
19582                                                   &clause_non_constant_p);
19583       /* If any clause is non-constant, so is the entire initializer.  */
19584       if (clause_non_constant_p)
19585         *non_constant_p = true;
19586
19587       /* If we have an ellipsis, this is an initializer pack
19588          expansion.  */
19589       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19590         {
19591           /* Consume the `...'.  */
19592           cp_lexer_consume_token (parser->lexer);
19593
19594           /* Turn the initializer into an initializer expansion.  */
19595           initializer = make_pack_expansion (initializer);
19596         }
19597
19598       /* Add it to the vector.  */
19599       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19600
19601       /* If the next token is not a comma, we have reached the end of
19602          the list.  */
19603       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19604         break;
19605
19606       /* Peek at the next token.  */
19607       token = cp_lexer_peek_nth_token (parser->lexer, 2);
19608       /* If the next token is a `}', then we're still done.  An
19609          initializer-clause can have a trailing `,' after the
19610          initializer-list and before the closing `}'.  */
19611       if (token->type == CPP_CLOSE_BRACE)
19612         break;
19613
19614       /* Consume the `,' token.  */
19615       cp_lexer_consume_token (parser->lexer);
19616     }
19617
19618   return v;
19619 }
19620
19621 /* Classes [gram.class] */
19622
19623 /* Parse a class-name.
19624
19625    class-name:
19626      identifier
19627      template-id
19628
19629    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19630    to indicate that names looked up in dependent types should be
19631    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
19632    keyword has been used to indicate that the name that appears next
19633    is a template.  TAG_TYPE indicates the explicit tag given before
19634    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
19635    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
19636    is the class being defined in a class-head.
19637
19638    Returns the TYPE_DECL representing the class.  */
19639
19640 static tree
19641 cp_parser_class_name (cp_parser *parser,
19642                       bool typename_keyword_p,
19643                       bool template_keyword_p,
19644                       enum tag_types tag_type,
19645                       bool check_dependency_p,
19646                       bool class_head_p,
19647                       bool is_declaration)
19648 {
19649   tree decl;
19650   tree scope;
19651   bool typename_p;
19652   cp_token *token;
19653   tree identifier = NULL_TREE;
19654
19655   /* All class-names start with an identifier.  */
19656   token = cp_lexer_peek_token (parser->lexer);
19657   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19658     {
19659       cp_parser_error (parser, "expected class-name");
19660       return error_mark_node;
19661     }
19662
19663   /* PARSER->SCOPE can be cleared when parsing the template-arguments
19664      to a template-id, so we save it here.  */
19665   scope = parser->scope;
19666   if (scope == error_mark_node)
19667     return error_mark_node;
19668
19669   /* Any name names a type if we're following the `typename' keyword
19670      in a qualified name where the enclosing scope is type-dependent.  */
19671   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19672                 && dependent_type_p (scope));
19673   /* Handle the common case (an identifier, but not a template-id)
19674      efficiently.  */
19675   if (token->type == CPP_NAME
19676       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19677     {
19678       cp_token *identifier_token;
19679       bool ambiguous_p;
19680
19681       /* Look for the identifier.  */
19682       identifier_token = cp_lexer_peek_token (parser->lexer);
19683       ambiguous_p = identifier_token->error_reported;
19684       identifier = cp_parser_identifier (parser);
19685       /* If the next token isn't an identifier, we are certainly not
19686          looking at a class-name.  */
19687       if (identifier == error_mark_node)
19688         decl = error_mark_node;
19689       /* If we know this is a type-name, there's no need to look it
19690          up.  */
19691       else if (typename_p)
19692         decl = identifier;
19693       else
19694         {
19695           tree ambiguous_decls;
19696           /* If we already know that this lookup is ambiguous, then
19697              we've already issued an error message; there's no reason
19698              to check again.  */
19699           if (ambiguous_p)
19700             {
19701               cp_parser_simulate_error (parser);
19702               return error_mark_node;
19703             }
19704           /* If the next token is a `::', then the name must be a type
19705              name.
19706
19707              [basic.lookup.qual]
19708
19709              During the lookup for a name preceding the :: scope
19710              resolution operator, object, function, and enumerator
19711              names are ignored.  */
19712           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19713             tag_type = typename_type;
19714           /* Look up the name.  */
19715           decl = cp_parser_lookup_name (parser, identifier,
19716                                         tag_type,
19717                                         /*is_template=*/false,
19718                                         /*is_namespace=*/false,
19719                                         check_dependency_p,
19720                                         &ambiguous_decls,
19721                                         identifier_token->location);
19722           if (ambiguous_decls)
19723             {
19724               if (cp_parser_parsing_tentatively (parser))
19725                 cp_parser_simulate_error (parser);
19726               return error_mark_node;
19727             }
19728         }
19729     }
19730   else
19731     {
19732       /* Try a template-id.  */
19733       decl = cp_parser_template_id (parser, template_keyword_p,
19734                                     check_dependency_p,
19735                                     tag_type,
19736                                     is_declaration);
19737       if (decl == error_mark_node)
19738         return error_mark_node;
19739     }
19740
19741   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19742
19743   /* If this is a typename, create a TYPENAME_TYPE.  */
19744   if (typename_p && decl != error_mark_node)
19745     {
19746       decl = make_typename_type (scope, decl, typename_type,
19747                                  /*complain=*/tf_error);
19748       if (decl != error_mark_node)
19749         decl = TYPE_NAME (decl);
19750     }
19751
19752   decl = strip_using_decl (decl);
19753
19754   /* Check to see that it is really the name of a class.  */
19755   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19756       && identifier_p (TREE_OPERAND (decl, 0))
19757       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19758     /* Situations like this:
19759
19760          template <typename T> struct A {
19761            typename T::template X<int>::I i;
19762          };
19763
19764        are problematic.  Is `T::template X<int>' a class-name?  The
19765        standard does not seem to be definitive, but there is no other
19766        valid interpretation of the following `::'.  Therefore, those
19767        names are considered class-names.  */
19768     {
19769       decl = make_typename_type (scope, decl, tag_type, tf_error);
19770       if (decl != error_mark_node)
19771         decl = TYPE_NAME (decl);
19772     }
19773   else if (TREE_CODE (decl) != TYPE_DECL
19774            || TREE_TYPE (decl) == error_mark_node
19775            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19776            /* In Objective-C 2.0, a classname followed by '.' starts a
19777               dot-syntax expression, and it's not a type-name.  */
19778            || (c_dialect_objc ()
19779                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
19780                && objc_is_class_name (decl)))
19781     decl = error_mark_node;
19782
19783   if (decl == error_mark_node)
19784     cp_parser_error (parser, "expected class-name");
19785   else if (identifier && !parser->scope)
19786     maybe_note_name_used_in_class (identifier, decl);
19787
19788   return decl;
19789 }
19790
19791 /* Parse a class-specifier.
19792
19793    class-specifier:
19794      class-head { member-specification [opt] }
19795
19796    Returns the TREE_TYPE representing the class.  */
19797
19798 static tree
19799 cp_parser_class_specifier_1 (cp_parser* parser)
19800 {
19801   tree type;
19802   tree attributes = NULL_TREE;
19803   bool nested_name_specifier_p;
19804   unsigned saved_num_template_parameter_lists;
19805   bool saved_in_function_body;
19806   unsigned char in_statement;
19807   bool in_switch_statement_p;
19808   bool saved_in_unbraced_linkage_specification_p;
19809   tree old_scope = NULL_TREE;
19810   tree scope = NULL_TREE;
19811   cp_token *closing_brace;
19812
19813   push_deferring_access_checks (dk_no_deferred);
19814
19815   /* Parse the class-head.  */
19816   type = cp_parser_class_head (parser,
19817                                &nested_name_specifier_p);
19818   /* If the class-head was a semantic disaster, skip the entire body
19819      of the class.  */
19820   if (!type)
19821     {
19822       cp_parser_skip_to_end_of_block_or_statement (parser);
19823       pop_deferring_access_checks ();
19824       return error_mark_node;
19825     }
19826
19827   /* Look for the `{'.  */
19828   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19829     {
19830       pop_deferring_access_checks ();
19831       return error_mark_node;
19832     }
19833
19834   cp_ensure_no_omp_declare_simd (parser);
19835
19836   /* Issue an error message if type-definitions are forbidden here.  */
19837   cp_parser_check_type_definition (parser);
19838   /* Remember that we are defining one more class.  */
19839   ++parser->num_classes_being_defined;
19840   /* Inside the class, surrounding template-parameter-lists do not
19841      apply.  */
19842   saved_num_template_parameter_lists
19843     = parser->num_template_parameter_lists;
19844   parser->num_template_parameter_lists = 0;
19845   /* We are not in a function body.  */
19846   saved_in_function_body = parser->in_function_body;
19847   parser->in_function_body = false;
19848   /* Or in a loop.  */
19849   in_statement = parser->in_statement;
19850   parser->in_statement = 0;
19851   /* Or in a switch.  */
19852   in_switch_statement_p = parser->in_switch_statement_p;
19853   parser->in_switch_statement_p = false;
19854   /* We are not immediately inside an extern "lang" block.  */
19855   saved_in_unbraced_linkage_specification_p
19856     = parser->in_unbraced_linkage_specification_p;
19857   parser->in_unbraced_linkage_specification_p = false;
19858
19859   /* Start the class.  */
19860   if (nested_name_specifier_p)
19861     {
19862       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19863       old_scope = push_inner_scope (scope);
19864     }
19865   type = begin_class_definition (type);
19866
19867   if (type == error_mark_node)
19868     /* If the type is erroneous, skip the entire body of the class.  */
19869     cp_parser_skip_to_closing_brace (parser);
19870   else
19871     /* Parse the member-specification.  */
19872     cp_parser_member_specification_opt (parser);
19873
19874   /* Look for the trailing `}'.  */
19875   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19876   /* Look for trailing attributes to apply to this class.  */
19877   if (cp_parser_allow_gnu_extensions_p (parser))
19878     attributes = cp_parser_gnu_attributes_opt (parser);
19879   if (type != error_mark_node)
19880     type = finish_struct (type, attributes);
19881   if (nested_name_specifier_p)
19882     pop_inner_scope (old_scope, scope);
19883
19884   /* We've finished a type definition.  Check for the common syntax
19885      error of forgetting a semicolon after the definition.  We need to
19886      be careful, as we can't just check for not-a-semicolon and be done
19887      with it; the user might have typed:
19888
19889      class X { } c = ...;
19890      class X { } *p = ...;
19891
19892      and so forth.  Instead, enumerate all the possible tokens that
19893      might follow this production; if we don't see one of them, then
19894      complain and silently insert the semicolon.  */
19895   {
19896     cp_token *token = cp_lexer_peek_token (parser->lexer);
19897     bool want_semicolon = true;
19898
19899     if (cp_next_tokens_can_be_std_attribute_p (parser))
19900       /* Don't try to parse c++11 attributes here.  As per the
19901          grammar, that should be a task for
19902          cp_parser_decl_specifier_seq.  */
19903       want_semicolon = false;
19904
19905     switch (token->type)
19906       {
19907       case CPP_NAME:
19908       case CPP_SEMICOLON:
19909       case CPP_MULT:
19910       case CPP_AND:
19911       case CPP_OPEN_PAREN:
19912       case CPP_CLOSE_PAREN:
19913       case CPP_COMMA:
19914         want_semicolon = false;
19915         break;
19916
19917         /* While it's legal for type qualifiers and storage class
19918            specifiers to follow type definitions in the grammar, only
19919            compiler testsuites contain code like that.  Assume that if
19920            we see such code, then what we're really seeing is a case
19921            like:
19922
19923            class X { }
19924            const <type> var = ...;
19925
19926            or
19927
19928            class Y { }
19929            static <type> func (...) ...
19930
19931            i.e. the qualifier or specifier applies to the next
19932            declaration.  To do so, however, we need to look ahead one
19933            more token to see if *that* token is a type specifier.
19934
19935            This code could be improved to handle:
19936
19937            class Z { }
19938            static const <type> var = ...;  */
19939       case CPP_KEYWORD:
19940         if (keyword_is_decl_specifier (token->keyword))
19941           {
19942             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19943
19944             /* Handling user-defined types here would be nice, but very
19945                tricky.  */
19946             want_semicolon
19947               = (lookahead->type == CPP_KEYWORD
19948                  && keyword_begins_type_specifier (lookahead->keyword));
19949           }
19950         break;
19951       default:
19952         break;
19953       }
19954
19955     /* If we don't have a type, then something is very wrong and we
19956        shouldn't try to do anything clever.  Likewise for not seeing the
19957        closing brace.  */
19958     if (closing_brace && TYPE_P (type) && want_semicolon)
19959       {
19960         cp_token_position prev
19961           = cp_lexer_previous_token_position (parser->lexer);
19962         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19963         location_t loc = prev_token->location;
19964
19965         if (CLASSTYPE_DECLARED_CLASS (type))
19966           error_at (loc, "expected %<;%> after class definition");
19967         else if (TREE_CODE (type) == RECORD_TYPE)
19968           error_at (loc, "expected %<;%> after struct definition");
19969         else if (TREE_CODE (type) == UNION_TYPE)
19970           error_at (loc, "expected %<;%> after union definition");
19971         else
19972           gcc_unreachable ();
19973
19974         /* Unget one token and smash it to look as though we encountered
19975            a semicolon in the input stream.  */
19976         cp_lexer_set_token_position (parser->lexer, prev);
19977         token = cp_lexer_peek_token (parser->lexer);
19978         token->type = CPP_SEMICOLON;
19979         token->keyword = RID_MAX;
19980       }
19981   }
19982
19983   /* If this class is not itself within the scope of another class,
19984      then we need to parse the bodies of all of the queued function
19985      definitions.  Note that the queued functions defined in a class
19986      are not always processed immediately following the
19987      class-specifier for that class.  Consider:
19988
19989        struct A {
19990          struct B { void f() { sizeof (A); } };
19991        };
19992
19993      If `f' were processed before the processing of `A' were
19994      completed, there would be no way to compute the size of `A'.
19995      Note that the nesting we are interested in here is lexical --
19996      not the semantic nesting given by TYPE_CONTEXT.  In particular,
19997      for:
19998
19999        struct A { struct B; };
20000        struct A::B { void f() { } };
20001
20002      there is no need to delay the parsing of `A::B::f'.  */
20003   if (--parser->num_classes_being_defined == 0)
20004     {
20005       tree decl;
20006       tree class_type = NULL_TREE;
20007       tree pushed_scope = NULL_TREE;
20008       unsigned ix;
20009       cp_default_arg_entry *e;
20010       tree save_ccp, save_ccr;
20011
20012       /* In a first pass, parse default arguments to the functions.
20013          Then, in a second pass, parse the bodies of the functions.
20014          This two-phased approach handles cases like:
20015
20016             struct S {
20017               void f() { g(); }
20018               void g(int i = 3);
20019             };
20020
20021          */
20022       FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
20023         {
20024           decl = e->decl;
20025           /* If there are default arguments that have not yet been processed,
20026              take care of them now.  */
20027           if (class_type != e->class_type)
20028             {
20029               if (pushed_scope)
20030                 pop_scope (pushed_scope);
20031               class_type = e->class_type;
20032               pushed_scope = push_scope (class_type);
20033             }
20034           /* Make sure that any template parameters are in scope.  */
20035           maybe_begin_member_template_processing (decl);
20036           /* Parse the default argument expressions.  */
20037           cp_parser_late_parsing_default_args (parser, decl);
20038           /* Remove any template parameters from the symbol table.  */
20039           maybe_end_member_template_processing ();
20040         }
20041       vec_safe_truncate (unparsed_funs_with_default_args, 0);
20042       /* Now parse any NSDMIs.  */
20043       save_ccp = current_class_ptr;
20044       save_ccr = current_class_ref;
20045       FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
20046         {
20047           if (class_type != DECL_CONTEXT (decl))
20048             {
20049               if (pushed_scope)
20050                 pop_scope (pushed_scope);
20051               class_type = DECL_CONTEXT (decl);
20052               pushed_scope = push_scope (class_type);
20053             }
20054           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
20055           cp_parser_late_parsing_nsdmi (parser, decl);
20056         }
20057       vec_safe_truncate (unparsed_nsdmis, 0);
20058       current_class_ptr = save_ccp;
20059       current_class_ref = save_ccr;
20060       if (pushed_scope)
20061         pop_scope (pushed_scope);
20062
20063       /* Now do some post-NSDMI bookkeeping.  */
20064       FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
20065         after_nsdmi_defaulted_late_checks (class_type);
20066       vec_safe_truncate (unparsed_classes, 0);
20067       after_nsdmi_defaulted_late_checks (type);
20068
20069       /* Now parse the body of the functions.  */
20070       if (flag_openmp)
20071         {
20072           /* OpenMP UDRs need to be parsed before all other functions.  */
20073           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20074             if (DECL_OMP_DECLARE_REDUCTION_P (decl))
20075               cp_parser_late_parsing_for_member (parser, decl);
20076           FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20077             if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
20078               cp_parser_late_parsing_for_member (parser, decl);
20079         }
20080       else
20081         FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
20082           cp_parser_late_parsing_for_member (parser, decl);
20083       vec_safe_truncate (unparsed_funs_with_definitions, 0);
20084     }
20085   else
20086     vec_safe_push (unparsed_classes, type);
20087
20088   /* Put back any saved access checks.  */
20089   pop_deferring_access_checks ();
20090
20091   /* Restore saved state.  */
20092   parser->in_switch_statement_p = in_switch_statement_p;
20093   parser->in_statement = in_statement;
20094   parser->in_function_body = saved_in_function_body;
20095   parser->num_template_parameter_lists
20096     = saved_num_template_parameter_lists;
20097   parser->in_unbraced_linkage_specification_p
20098     = saved_in_unbraced_linkage_specification_p;
20099
20100   return type;
20101 }
20102
20103 static tree
20104 cp_parser_class_specifier (cp_parser* parser)
20105 {
20106   tree ret;
20107   timevar_push (TV_PARSE_STRUCT);
20108   ret = cp_parser_class_specifier_1 (parser);
20109   timevar_pop (TV_PARSE_STRUCT);
20110   return ret;
20111 }
20112
20113 /* Parse a class-head.
20114
20115    class-head:
20116      class-key identifier [opt] base-clause [opt]
20117      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
20118      class-key nested-name-specifier [opt] template-id
20119        base-clause [opt]
20120
20121    class-virt-specifier:
20122      final
20123
20124    GNU Extensions:
20125      class-key attributes identifier [opt] base-clause [opt]
20126      class-key attributes nested-name-specifier identifier base-clause [opt]
20127      class-key attributes nested-name-specifier [opt] template-id
20128        base-clause [opt]
20129
20130    Upon return BASES is initialized to the list of base classes (or
20131    NULL, if there are none) in the same form returned by
20132    cp_parser_base_clause.
20133
20134    Returns the TYPE of the indicated class.  Sets
20135    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
20136    involving a nested-name-specifier was used, and FALSE otherwise.
20137
20138    Returns error_mark_node if this is not a class-head.
20139
20140    Returns NULL_TREE if the class-head is syntactically valid, but
20141    semantically invalid in a way that means we should skip the entire
20142    body of the class.  */
20143
20144 static tree
20145 cp_parser_class_head (cp_parser* parser,
20146                       bool* nested_name_specifier_p)
20147 {
20148   tree nested_name_specifier;
20149   enum tag_types class_key;
20150   tree id = NULL_TREE;
20151   tree type = NULL_TREE;
20152   tree attributes;
20153   tree bases;
20154   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20155   bool template_id_p = false;
20156   bool qualified_p = false;
20157   bool invalid_nested_name_p = false;
20158   bool invalid_explicit_specialization_p = false;
20159   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20160   tree pushed_scope = NULL_TREE;
20161   unsigned num_templates;
20162   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
20163   /* Assume no nested-name-specifier will be present.  */
20164   *nested_name_specifier_p = false;
20165   /* Assume no template parameter lists will be used in defining the
20166      type.  */
20167   num_templates = 0;
20168   parser->colon_corrects_to_scope_p = false;
20169
20170   /* Look for the class-key.  */
20171   class_key = cp_parser_class_key (parser);
20172   if (class_key == none_type)
20173     return error_mark_node;
20174
20175   /* Parse the attributes.  */
20176   attributes = cp_parser_attributes_opt (parser);
20177
20178   /* If the next token is `::', that is invalid -- but sometimes
20179      people do try to write:
20180
20181        struct ::S {};
20182
20183      Handle this gracefully by accepting the extra qualifier, and then
20184      issuing an error about it later if this really is a
20185      class-head.  If it turns out just to be an elaborated type
20186      specifier, remain silent.  */
20187   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
20188     qualified_p = true;
20189
20190   push_deferring_access_checks (dk_no_check);
20191
20192   /* Determine the name of the class.  Begin by looking for an
20193      optional nested-name-specifier.  */
20194   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
20195   nested_name_specifier
20196     = cp_parser_nested_name_specifier_opt (parser,
20197                                            /*typename_keyword_p=*/false,
20198                                            /*check_dependency_p=*/false,
20199                                            /*type_p=*/true,
20200                                            /*is_declaration=*/false);
20201   /* If there was a nested-name-specifier, then there *must* be an
20202      identifier.  */
20203   if (nested_name_specifier)
20204     {
20205       type_start_token = cp_lexer_peek_token (parser->lexer);
20206       /* Although the grammar says `identifier', it really means
20207          `class-name' or `template-name'.  You are only allowed to
20208          define a class that has already been declared with this
20209          syntax.
20210
20211          The proposed resolution for Core Issue 180 says that wherever
20212          you see `class T::X' you should treat `X' as a type-name.
20213
20214          It is OK to define an inaccessible class; for example:
20215
20216            class A { class B; };
20217            class A::B {};
20218
20219          We do not know if we will see a class-name, or a
20220          template-name.  We look for a class-name first, in case the
20221          class-name is a template-id; if we looked for the
20222          template-name first we would stop after the template-name.  */
20223       cp_parser_parse_tentatively (parser);
20224       type = cp_parser_class_name (parser,
20225                                    /*typename_keyword_p=*/false,
20226                                    /*template_keyword_p=*/false,
20227                                    class_type,
20228                                    /*check_dependency_p=*/false,
20229                                    /*class_head_p=*/true,
20230                                    /*is_declaration=*/false);
20231       /* If that didn't work, ignore the nested-name-specifier.  */
20232       if (!cp_parser_parse_definitely (parser))
20233         {
20234           invalid_nested_name_p = true;
20235           type_start_token = cp_lexer_peek_token (parser->lexer);
20236           id = cp_parser_identifier (parser);
20237           if (id == error_mark_node)
20238             id = NULL_TREE;
20239         }
20240       /* If we could not find a corresponding TYPE, treat this
20241          declaration like an unqualified declaration.  */
20242       if (type == error_mark_node)
20243         nested_name_specifier = NULL_TREE;
20244       /* Otherwise, count the number of templates used in TYPE and its
20245          containing scopes.  */
20246       else
20247         {
20248           tree scope;
20249
20250           for (scope = TREE_TYPE (type);
20251                scope && TREE_CODE (scope) != NAMESPACE_DECL;
20252                scope = get_containing_scope (scope))
20253             if (TYPE_P (scope)
20254                 && CLASS_TYPE_P (scope)
20255                 && CLASSTYPE_TEMPLATE_INFO (scope)
20256                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
20257                 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
20258                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
20259               ++num_templates;
20260         }
20261     }
20262   /* Otherwise, the identifier is optional.  */
20263   else
20264     {
20265       /* We don't know whether what comes next is a template-id,
20266          an identifier, or nothing at all.  */
20267       cp_parser_parse_tentatively (parser);
20268       /* Check for a template-id.  */
20269       type_start_token = cp_lexer_peek_token (parser->lexer);
20270       id = cp_parser_template_id (parser,
20271                                   /*template_keyword_p=*/false,
20272                                   /*check_dependency_p=*/true,
20273                                   class_key,
20274                                   /*is_declaration=*/true);
20275       /* If that didn't work, it could still be an identifier.  */
20276       if (!cp_parser_parse_definitely (parser))
20277         {
20278           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20279             {
20280               type_start_token = cp_lexer_peek_token (parser->lexer);
20281               id = cp_parser_identifier (parser);
20282             }
20283           else
20284             id = NULL_TREE;
20285         }
20286       else
20287         {
20288           template_id_p = true;
20289           ++num_templates;
20290         }
20291     }
20292
20293   pop_deferring_access_checks ();
20294
20295   if (id)
20296     {
20297       cp_parser_check_for_invalid_template_id (parser, id,
20298                                                class_key,
20299                                                type_start_token->location);
20300     }
20301   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20302
20303   /* If it's not a `:' or a `{' then we can't really be looking at a
20304      class-head, since a class-head only appears as part of a
20305      class-specifier.  We have to detect this situation before calling
20306      xref_tag, since that has irreversible side-effects.  */
20307   if (!cp_parser_next_token_starts_class_definition_p (parser))
20308     {
20309       cp_parser_error (parser, "expected %<{%> or %<:%>");
20310       type = error_mark_node;
20311       goto out;
20312     }
20313
20314   /* At this point, we're going ahead with the class-specifier, even
20315      if some other problem occurs.  */
20316   cp_parser_commit_to_tentative_parse (parser);
20317   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
20318     {
20319       cp_parser_error (parser,
20320                        "cannot specify %<override%> for a class");
20321       type = error_mark_node;
20322       goto out;
20323     }
20324   /* Issue the error about the overly-qualified name now.  */
20325   if (qualified_p)
20326     {
20327       cp_parser_error (parser,
20328                        "global qualification of class name is invalid");
20329       type = error_mark_node;
20330       goto out;
20331     }
20332   else if (invalid_nested_name_p)
20333     {
20334       cp_parser_error (parser,
20335                        "qualified name does not name a class");
20336       type = error_mark_node;
20337       goto out;
20338     }
20339   else if (nested_name_specifier)
20340     {
20341       tree scope;
20342
20343       /* Reject typedef-names in class heads.  */
20344       if (!DECL_IMPLICIT_TYPEDEF_P (type))
20345         {
20346           error_at (type_start_token->location,
20347                     "invalid class name in declaration of %qD",
20348                     type);
20349           type = NULL_TREE;
20350           goto done;
20351         }
20352
20353       /* Figure out in what scope the declaration is being placed.  */
20354       scope = current_scope ();
20355       /* If that scope does not contain the scope in which the
20356          class was originally declared, the program is invalid.  */
20357       if (scope && !is_ancestor (scope, nested_name_specifier))
20358         {
20359           if (at_namespace_scope_p ())
20360             error_at (type_start_token->location,
20361                       "declaration of %qD in namespace %qD which does not "
20362                       "enclose %qD",
20363                       type, scope, nested_name_specifier);
20364           else
20365             error_at (type_start_token->location,
20366                       "declaration of %qD in %qD which does not enclose %qD",
20367                       type, scope, nested_name_specifier);
20368           type = NULL_TREE;
20369           goto done;
20370         }
20371       /* [dcl.meaning]
20372
20373          A declarator-id shall not be qualified except for the
20374          definition of a ... nested class outside of its class
20375          ... [or] the definition or explicit instantiation of a
20376          class member of a namespace outside of its namespace.  */
20377       if (scope == nested_name_specifier)
20378         {
20379           permerror (nested_name_specifier_token_start->location,
20380                      "extra qualification not allowed");
20381           nested_name_specifier = NULL_TREE;
20382           num_templates = 0;
20383         }
20384     }
20385   /* An explicit-specialization must be preceded by "template <>".  If
20386      it is not, try to recover gracefully.  */
20387   if (at_namespace_scope_p ()
20388       && parser->num_template_parameter_lists == 0
20389       && template_id_p)
20390     {
20391       error_at (type_start_token->location,
20392                 "an explicit specialization must be preceded by %<template <>%>");
20393       invalid_explicit_specialization_p = true;
20394       /* Take the same action that would have been taken by
20395          cp_parser_explicit_specialization.  */
20396       ++parser->num_template_parameter_lists;
20397       begin_specialization ();
20398     }
20399   /* There must be no "return" statements between this point and the
20400      end of this function; set "type "to the correct return value and
20401      use "goto done;" to return.  */
20402   /* Make sure that the right number of template parameters were
20403      present.  */
20404   if (!cp_parser_check_template_parameters (parser, num_templates,
20405                                             type_start_token->location,
20406                                             /*declarator=*/NULL))
20407     {
20408       /* If something went wrong, there is no point in even trying to
20409          process the class-definition.  */
20410       type = NULL_TREE;
20411       goto done;
20412     }
20413
20414   /* Look up the type.  */
20415   if (template_id_p)
20416     {
20417       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20418           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20419               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20420         {
20421           error_at (type_start_token->location,
20422                     "function template %qD redeclared as a class template", id);
20423           type = error_mark_node;
20424         }
20425       else
20426         {
20427           type = TREE_TYPE (id);
20428           type = maybe_process_partial_specialization (type);
20429         }
20430       if (nested_name_specifier)
20431         pushed_scope = push_scope (nested_name_specifier);
20432     }
20433   else if (nested_name_specifier)
20434     {
20435       tree class_type;
20436
20437       /* Given:
20438
20439             template <typename T> struct S { struct T };
20440             template <typename T> struct S<T>::T { };
20441
20442          we will get a TYPENAME_TYPE when processing the definition of
20443          `S::T'.  We need to resolve it to the actual type before we
20444          try to define it.  */
20445       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20446         {
20447           class_type = resolve_typename_type (TREE_TYPE (type),
20448                                               /*only_current_p=*/false);
20449           if (TREE_CODE (class_type) != TYPENAME_TYPE)
20450             type = TYPE_NAME (class_type);
20451           else
20452             {
20453               cp_parser_error (parser, "could not resolve typename type");
20454               type = error_mark_node;
20455             }
20456         }
20457
20458       if (maybe_process_partial_specialization (TREE_TYPE (type))
20459           == error_mark_node)
20460         {
20461           type = NULL_TREE;
20462           goto done;
20463         }
20464
20465       class_type = current_class_type;
20466       /* Enter the scope indicated by the nested-name-specifier.  */
20467       pushed_scope = push_scope (nested_name_specifier);
20468       /* Get the canonical version of this type.  */
20469       type = TYPE_MAIN_DECL (TREE_TYPE (type));
20470       /* Call push_template_decl if it seems like we should be defining a
20471          template either from the template headers or the type we're
20472          defining, so that we diagnose both extra and missing headers.  */
20473       if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20474            || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
20475           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20476         {
20477           type = push_template_decl (type);
20478           if (type == error_mark_node)
20479             {
20480               type = NULL_TREE;
20481               goto done;
20482             }
20483         }
20484
20485       type = TREE_TYPE (type);
20486       *nested_name_specifier_p = true;
20487     }
20488   else      /* The name is not a nested name.  */
20489     {
20490       /* If the class was unnamed, create a dummy name.  */
20491       if (!id)
20492         id = make_anon_name ();
20493       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20494                        parser->num_template_parameter_lists);
20495     }
20496
20497   /* Indicate whether this class was declared as a `class' or as a
20498      `struct'.  */
20499   if (TREE_CODE (type) == RECORD_TYPE)
20500     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20501   cp_parser_check_class_key (class_key, type);
20502
20503   /* If this type was already complete, and we see another definition,
20504      that's an error.  */
20505   if (type != error_mark_node && COMPLETE_TYPE_P (type))
20506     {
20507       error_at (type_start_token->location, "redefinition of %q#T",
20508                 type);
20509       error_at (type_start_token->location, "previous definition of %q+#T",
20510                 type);
20511       type = NULL_TREE;
20512       goto done;
20513     }
20514   else if (type == error_mark_node)
20515     type = NULL_TREE;
20516
20517   if (type)
20518     {
20519       /* Apply attributes now, before any use of the class as a template
20520          argument in its base list.  */
20521       cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20522       fixup_attribute_variants (type);
20523     }
20524
20525   /* We will have entered the scope containing the class; the names of
20526      base classes should be looked up in that context.  For example:
20527
20528        struct A { struct B {}; struct C; };
20529        struct A::C : B {};
20530
20531      is valid.  */
20532
20533   /* Get the list of base-classes, if there is one.  */
20534   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20535     {
20536       /* PR59482: enter the class scope so that base-specifiers are looked
20537          up correctly.  */
20538       if (type)
20539         pushclass (type);
20540       bases = cp_parser_base_clause (parser);
20541       /* PR59482: get out of the previously pushed class scope so that the
20542          subsequent pops pop the right thing.  */
20543       if (type)
20544         popclass ();
20545     }
20546   else
20547     bases = NULL_TREE;
20548
20549   /* If we're really defining a class, process the base classes.
20550      If they're invalid, fail.  */
20551   if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20552       && !xref_basetypes (type, bases))
20553     type = NULL_TREE;
20554
20555  done:
20556   /* Leave the scope given by the nested-name-specifier.  We will
20557      enter the class scope itself while processing the members.  */
20558   if (pushed_scope)
20559     pop_scope (pushed_scope);
20560
20561   if (invalid_explicit_specialization_p)
20562     {
20563       end_specialization ();
20564       --parser->num_template_parameter_lists;
20565     }
20566
20567   if (type)
20568     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20569   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20570     CLASSTYPE_FINAL (type) = 1;
20571  out:
20572   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20573   return type;
20574 }
20575
20576 /* Parse a class-key.
20577
20578    class-key:
20579      class
20580      struct
20581      union
20582
20583    Returns the kind of class-key specified, or none_type to indicate
20584    error.  */
20585
20586 static enum tag_types
20587 cp_parser_class_key (cp_parser* parser)
20588 {
20589   cp_token *token;
20590   enum tag_types tag_type;
20591
20592   /* Look for the class-key.  */
20593   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20594   if (!token)
20595     return none_type;
20596
20597   /* Check to see if the TOKEN is a class-key.  */
20598   tag_type = cp_parser_token_is_class_key (token);
20599   if (!tag_type)
20600     cp_parser_error (parser, "expected class-key");
20601   return tag_type;
20602 }
20603
20604 /* Parse a type-parameter-key.
20605
20606    type-parameter-key:
20607      class
20608      typename
20609  */
20610
20611 static void
20612 cp_parser_type_parameter_key (cp_parser* parser)
20613 {
20614   /* Look for the type-parameter-key.  */
20615   enum tag_types tag_type = none_type;
20616   cp_token *token = cp_lexer_peek_token (parser->lexer);
20617   if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20618     {
20619       cp_lexer_consume_token (parser->lexer);
20620       if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20621         /* typename is not allowed in a template template parameter
20622            by the standard until C++1Z.  */
20623         pedwarn (token->location, OPT_Wpedantic, 
20624                  "ISO C++ forbids typename key in template template parameter;"
20625                  " use -std=c++1z or -std=gnu++1z");
20626     }
20627   else
20628     cp_parser_error (parser, "expected %<class%> or %<typename%>");
20629
20630   return;
20631 }
20632
20633 /* Parse an (optional) member-specification.
20634
20635    member-specification:
20636      member-declaration member-specification [opt]
20637      access-specifier : member-specification [opt]  */
20638
20639 static void
20640 cp_parser_member_specification_opt (cp_parser* parser)
20641 {
20642   while (true)
20643     {
20644       cp_token *token;
20645       enum rid keyword;
20646
20647       /* Peek at the next token.  */
20648       token = cp_lexer_peek_token (parser->lexer);
20649       /* If it's a `}', or EOF then we've seen all the members.  */
20650       if (token->type == CPP_CLOSE_BRACE
20651           || token->type == CPP_EOF
20652           || token->type == CPP_PRAGMA_EOL)
20653         break;
20654
20655       /* See if this token is a keyword.  */
20656       keyword = token->keyword;
20657       switch (keyword)
20658         {
20659         case RID_PUBLIC:
20660         case RID_PROTECTED:
20661         case RID_PRIVATE:
20662           /* Consume the access-specifier.  */
20663           cp_lexer_consume_token (parser->lexer);
20664           /* Remember which access-specifier is active.  */
20665           current_access_specifier = token->u.value;
20666           /* Look for the `:'.  */
20667           cp_parser_require (parser, CPP_COLON, RT_COLON);
20668           break;
20669
20670         default:
20671           /* Accept #pragmas at class scope.  */
20672           if (token->type == CPP_PRAGMA)
20673             {
20674               cp_parser_pragma (parser, pragma_member);
20675               break;
20676             }
20677
20678           /* Otherwise, the next construction must be a
20679              member-declaration.  */
20680           cp_parser_member_declaration (parser);
20681         }
20682     }
20683 }
20684
20685 /* Parse a member-declaration.
20686
20687    member-declaration:
20688      decl-specifier-seq [opt] member-declarator-list [opt] ;
20689      function-definition ; [opt]
20690      :: [opt] nested-name-specifier template [opt] unqualified-id ;
20691      using-declaration
20692      template-declaration
20693      alias-declaration
20694
20695    member-declarator-list:
20696      member-declarator
20697      member-declarator-list , member-declarator
20698
20699    member-declarator:
20700      declarator pure-specifier [opt]
20701      declarator constant-initializer [opt]
20702      identifier [opt] : constant-expression
20703
20704    GNU Extensions:
20705
20706    member-declaration:
20707      __extension__ member-declaration
20708
20709    member-declarator:
20710      declarator attributes [opt] pure-specifier [opt]
20711      declarator attributes [opt] constant-initializer [opt]
20712      identifier [opt] attributes [opt] : constant-expression  
20713
20714    C++0x Extensions:
20715
20716    member-declaration:
20717      static_assert-declaration  */
20718
20719 static void
20720 cp_parser_member_declaration (cp_parser* parser)
20721 {
20722   cp_decl_specifier_seq decl_specifiers;
20723   tree prefix_attributes;
20724   tree decl;
20725   int declares_class_or_enum;
20726   bool friend_p;
20727   cp_token *token = NULL;
20728   cp_token *decl_spec_token_start = NULL;
20729   cp_token *initializer_token_start = NULL;
20730   int saved_pedantic;
20731   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20732
20733   /* Check for the `__extension__' keyword.  */
20734   if (cp_parser_extension_opt (parser, &saved_pedantic))
20735     {
20736       /* Recurse.  */
20737       cp_parser_member_declaration (parser);
20738       /* Restore the old value of the PEDANTIC flag.  */
20739       pedantic = saved_pedantic;
20740
20741       return;
20742     }
20743
20744   /* Check for a template-declaration.  */
20745   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20746     {
20747       /* An explicit specialization here is an error condition, and we
20748          expect the specialization handler to detect and report this.  */
20749       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20750           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20751         cp_parser_explicit_specialization (parser);
20752       else
20753         cp_parser_template_declaration (parser, /*member_p=*/true);
20754
20755       return;
20756     }
20757
20758   /* Check for a using-declaration.  */
20759   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20760     {
20761       if (cxx_dialect < cxx11)
20762         {
20763           /* Parse the using-declaration.  */
20764           cp_parser_using_declaration (parser,
20765                                        /*access_declaration_p=*/false);
20766           return;
20767         }
20768       else
20769         {
20770           tree decl;
20771           bool alias_decl_expected;
20772           cp_parser_parse_tentatively (parser);
20773           decl = cp_parser_alias_declaration (parser);
20774           /* Note that if we actually see the '=' token after the
20775              identifier, cp_parser_alias_declaration commits the
20776              tentative parse.  In that case, we really expects an
20777              alias-declaration.  Otherwise, we expect a using
20778              declaration.  */
20779           alias_decl_expected =
20780             !cp_parser_uncommitted_to_tentative_parse_p (parser);
20781           cp_parser_parse_definitely (parser);
20782
20783           if (alias_decl_expected)
20784             finish_member_declaration (decl);
20785           else
20786             cp_parser_using_declaration (parser,
20787                                          /*access_declaration_p=*/false);
20788           return;
20789         }
20790     }
20791
20792   /* Check for @defs.  */
20793   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20794     {
20795       tree ivar, member;
20796       tree ivar_chains = cp_parser_objc_defs_expression (parser);
20797       ivar = ivar_chains;
20798       while (ivar)
20799         {
20800           member = ivar;
20801           ivar = TREE_CHAIN (member);
20802           TREE_CHAIN (member) = NULL_TREE;
20803           finish_member_declaration (member);
20804         }
20805       return;
20806     }
20807
20808   /* If the next token is `static_assert' we have a static assertion.  */
20809   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20810     {
20811       cp_parser_static_assert (parser, /*member_p=*/true);
20812       return;
20813     }
20814
20815   parser->colon_corrects_to_scope_p = false;
20816
20817   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20818       goto out;
20819
20820   /* Parse the decl-specifier-seq.  */
20821   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20822   cp_parser_decl_specifier_seq (parser,
20823                                 CP_PARSER_FLAGS_OPTIONAL,
20824                                 &decl_specifiers,
20825                                 &declares_class_or_enum);
20826   /* Check for an invalid type-name.  */
20827   if (!decl_specifiers.any_type_specifiers_p
20828       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20829     goto out;
20830   /* If there is no declarator, then the decl-specifier-seq should
20831      specify a type.  */
20832   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20833     {
20834       /* If there was no decl-specifier-seq, and the next token is a
20835          `;', then we have something like:
20836
20837            struct S { ; };
20838
20839          [class.mem]
20840
20841          Each member-declaration shall declare at least one member
20842          name of the class.  */
20843       if (!decl_specifiers.any_specifiers_p)
20844         {
20845           cp_token *token = cp_lexer_peek_token (parser->lexer);
20846           if (!in_system_header_at (token->location))
20847             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20848         }
20849       else
20850         {
20851           tree type;
20852
20853           /* See if this declaration is a friend.  */
20854           friend_p = cp_parser_friend_p (&decl_specifiers);
20855           /* If there were decl-specifiers, check to see if there was
20856              a class-declaration.  */
20857           type = check_tag_decl (&decl_specifiers,
20858                                  /*explicit_type_instantiation_p=*/false);
20859           /* Nested classes have already been added to the class, but
20860              a `friend' needs to be explicitly registered.  */
20861           if (friend_p)
20862             {
20863               /* If the `friend' keyword was present, the friend must
20864                  be introduced with a class-key.  */
20865                if (!declares_class_or_enum && cxx_dialect < cxx11)
20866                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20867                           "in C++03 a class-key must be used "
20868                           "when declaring a friend");
20869                /* In this case:
20870
20871                     template <typename T> struct A {
20872                       friend struct A<T>::B;
20873                     };
20874
20875                   A<T>::B will be represented by a TYPENAME_TYPE, and
20876                   therefore not recognized by check_tag_decl.  */
20877                if (!type)
20878                  {
20879                    type = decl_specifiers.type;
20880                    if (type && TREE_CODE (type) == TYPE_DECL)
20881                      type = TREE_TYPE (type);
20882                  }
20883                if (!type || !TYPE_P (type))
20884                  error_at (decl_spec_token_start->location,
20885                            "friend declaration does not name a class or "
20886                            "function");
20887                else
20888                  make_friend_class (current_class_type, type,
20889                                     /*complain=*/true);
20890             }
20891           /* If there is no TYPE, an error message will already have
20892              been issued.  */
20893           else if (!type || type == error_mark_node)
20894             ;
20895           /* An anonymous aggregate has to be handled specially; such
20896              a declaration really declares a data member (with a
20897              particular type), as opposed to a nested class.  */
20898           else if (ANON_AGGR_TYPE_P (type))
20899             {
20900               /* C++11 9.5/6.  */
20901               if (decl_specifiers.storage_class != sc_none)
20902                 error_at (decl_spec_token_start->location,
20903                           "a storage class on an anonymous aggregate "
20904                           "in class scope is not allowed");
20905
20906               /* Remove constructors and such from TYPE, now that we
20907                  know it is an anonymous aggregate.  */
20908               fixup_anonymous_aggr (type);
20909               /* And make the corresponding data member.  */
20910               decl = build_decl (decl_spec_token_start->location,
20911                                  FIELD_DECL, NULL_TREE, type);
20912               /* Add it to the class.  */
20913               finish_member_declaration (decl);
20914             }
20915           else
20916             cp_parser_check_access_in_redeclaration
20917                                               (TYPE_NAME (type),
20918                                                decl_spec_token_start->location);
20919         }
20920     }
20921   else
20922     {
20923       bool assume_semicolon = false;
20924
20925       /* Clear attributes from the decl_specifiers but keep them
20926          around as prefix attributes that apply them to the entity
20927          being declared.  */
20928       prefix_attributes = decl_specifiers.attributes;
20929       decl_specifiers.attributes = NULL_TREE;
20930
20931       /* See if these declarations will be friends.  */
20932       friend_p = cp_parser_friend_p (&decl_specifiers);
20933
20934       /* Keep going until we hit the `;' at the end of the
20935          declaration.  */
20936       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20937         {
20938           tree attributes = NULL_TREE;
20939           tree first_attribute;
20940
20941           /* Peek at the next token.  */
20942           token = cp_lexer_peek_token (parser->lexer);
20943
20944           /* Check for a bitfield declaration.  */
20945           if (token->type == CPP_COLON
20946               || (token->type == CPP_NAME
20947                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20948                   == CPP_COLON))
20949             {
20950               tree identifier;
20951               tree width;
20952
20953               /* Get the name of the bitfield.  Note that we cannot just
20954                  check TOKEN here because it may have been invalidated by
20955                  the call to cp_lexer_peek_nth_token above.  */
20956               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20957                 identifier = cp_parser_identifier (parser);
20958               else
20959                 identifier = NULL_TREE;
20960
20961               /* Consume the `:' token.  */
20962               cp_lexer_consume_token (parser->lexer);
20963               /* Get the width of the bitfield.  */
20964               width
20965                 = cp_parser_constant_expression (parser);
20966
20967               /* Look for attributes that apply to the bitfield.  */
20968               attributes = cp_parser_attributes_opt (parser);
20969               /* Remember which attributes are prefix attributes and
20970                  which are not.  */
20971               first_attribute = attributes;
20972               /* Combine the attributes.  */
20973               attributes = chainon (prefix_attributes, attributes);
20974
20975               /* Create the bitfield declaration.  */
20976               decl = grokbitfield (identifier
20977                                    ? make_id_declarator (NULL_TREE,
20978                                                          identifier,
20979                                                          sfk_none)
20980                                    : NULL,
20981                                    &decl_specifiers,
20982                                    width,
20983                                    attributes);
20984             }
20985           else
20986             {
20987               cp_declarator *declarator;
20988               tree initializer;
20989               tree asm_specification;
20990               int ctor_dtor_or_conv_p;
20991
20992               /* Parse the declarator.  */
20993               declarator
20994                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20995                                         &ctor_dtor_or_conv_p,
20996                                         /*parenthesized_p=*/NULL,
20997                                         /*member_p=*/true,
20998                                         friend_p);
20999
21000               /* If something went wrong parsing the declarator, make sure
21001                  that we at least consume some tokens.  */
21002               if (declarator == cp_error_declarator)
21003                 {
21004                   /* Skip to the end of the statement.  */
21005                   cp_parser_skip_to_end_of_statement (parser);
21006                   /* If the next token is not a semicolon, that is
21007                      probably because we just skipped over the body of
21008                      a function.  So, we consume a semicolon if
21009                      present, but do not issue an error message if it
21010                      is not present.  */
21011                   if (cp_lexer_next_token_is (parser->lexer,
21012                                               CPP_SEMICOLON))
21013                     cp_lexer_consume_token (parser->lexer);
21014                   goto out;
21015                 }
21016
21017               if (declares_class_or_enum & 2)
21018                 cp_parser_check_for_definition_in_return_type
21019                                             (declarator, decl_specifiers.type,
21020                                              decl_specifiers.locations[ds_type_spec]);
21021
21022               /* Look for an asm-specification.  */
21023               asm_specification = cp_parser_asm_specification_opt (parser);
21024               /* Look for attributes that apply to the declaration.  */
21025               attributes = cp_parser_attributes_opt (parser);
21026               /* Remember which attributes are prefix attributes and
21027                  which are not.  */
21028               first_attribute = attributes;
21029               /* Combine the attributes.  */
21030               attributes = chainon (prefix_attributes, attributes);
21031
21032               /* If it's an `=', then we have a constant-initializer or a
21033                  pure-specifier.  It is not correct to parse the
21034                  initializer before registering the member declaration
21035                  since the member declaration should be in scope while
21036                  its initializer is processed.  However, the rest of the
21037                  front end does not yet provide an interface that allows
21038                  us to handle this correctly.  */
21039               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21040                 {
21041                   /* In [class.mem]:
21042
21043                      A pure-specifier shall be used only in the declaration of
21044                      a virtual function.
21045
21046                      A member-declarator can contain a constant-initializer
21047                      only if it declares a static member of integral or
21048                      enumeration type.
21049
21050                      Therefore, if the DECLARATOR is for a function, we look
21051                      for a pure-specifier; otherwise, we look for a
21052                      constant-initializer.  When we call `grokfield', it will
21053                      perform more stringent semantics checks.  */
21054                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
21055                   if (function_declarator_p (declarator)
21056                       || (decl_specifiers.type
21057                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
21058                           && declarator->kind == cdk_id
21059                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
21060                               == FUNCTION_TYPE)))
21061                     initializer = cp_parser_pure_specifier (parser);
21062                   else if (decl_specifiers.storage_class != sc_static)
21063                     initializer = cp_parser_save_nsdmi (parser);
21064                   else if (cxx_dialect >= cxx11)
21065                     {
21066                       bool nonconst;
21067                       /* Don't require a constant rvalue in C++11, since we
21068                          might want a reference constant.  We'll enforce
21069                          constancy later.  */
21070                       cp_lexer_consume_token (parser->lexer);
21071                       /* Parse the initializer.  */
21072                       initializer = cp_parser_initializer_clause (parser,
21073                                                                   &nonconst);
21074                     }
21075                   else
21076                     /* Parse the initializer.  */
21077                     initializer = cp_parser_constant_initializer (parser);
21078                 }
21079               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
21080                        && !function_declarator_p (declarator))
21081                 {
21082                   bool x;
21083                   if (decl_specifiers.storage_class != sc_static)
21084                     initializer = cp_parser_save_nsdmi (parser);
21085                   else
21086                     initializer = cp_parser_initializer (parser, &x, &x);
21087                 }
21088               /* Otherwise, there is no initializer.  */
21089               else
21090                 initializer = NULL_TREE;
21091
21092               /* See if we are probably looking at a function
21093                  definition.  We are certainly not looking at a
21094                  member-declarator.  Calling `grokfield' has
21095                  side-effects, so we must not do it unless we are sure
21096                  that we are looking at a member-declarator.  */
21097               if (cp_parser_token_starts_function_definition_p
21098                   (cp_lexer_peek_token (parser->lexer)))
21099                 {
21100                   /* The grammar does not allow a pure-specifier to be
21101                      used when a member function is defined.  (It is
21102                      possible that this fact is an oversight in the
21103                      standard, since a pure function may be defined
21104                      outside of the class-specifier.  */
21105                   if (initializer && initializer_token_start)
21106                     error_at (initializer_token_start->location,
21107                               "pure-specifier on function-definition");
21108                   decl = cp_parser_save_member_function_body (parser,
21109                                                               &decl_specifiers,
21110                                                               declarator,
21111                                                               attributes);
21112                   if (parser->fully_implicit_function_template_p)
21113                     decl = finish_fully_implicit_template (parser, decl);
21114                   /* If the member was not a friend, declare it here.  */
21115                   if (!friend_p)
21116                     finish_member_declaration (decl);
21117                   /* Peek at the next token.  */
21118                   token = cp_lexer_peek_token (parser->lexer);
21119                   /* If the next token is a semicolon, consume it.  */
21120                   if (token->type == CPP_SEMICOLON)
21121                     cp_lexer_consume_token (parser->lexer);
21122                   goto out;
21123                 }
21124               else
21125                 if (declarator->kind == cdk_function)
21126                   declarator->id_loc = token->location;
21127               /* Create the declaration.  */
21128               decl = grokfield (declarator, &decl_specifiers,
21129                                 initializer, /*init_const_expr_p=*/true,
21130                                 asm_specification, attributes);
21131               if (parser->fully_implicit_function_template_p)
21132                 {
21133                   if (friend_p)
21134                     finish_fully_implicit_template (parser, 0);
21135                   else
21136                     decl = finish_fully_implicit_template (parser, decl);
21137                 }
21138             }
21139
21140           cp_finalize_omp_declare_simd (parser, decl);
21141
21142           /* Reset PREFIX_ATTRIBUTES.  */
21143           while (attributes && TREE_CHAIN (attributes) != first_attribute)
21144             attributes = TREE_CHAIN (attributes);
21145           if (attributes)
21146             TREE_CHAIN (attributes) = NULL_TREE;
21147
21148           /* If there is any qualification still in effect, clear it
21149              now; we will be starting fresh with the next declarator.  */
21150           parser->scope = NULL_TREE;
21151           parser->qualifying_scope = NULL_TREE;
21152           parser->object_scope = NULL_TREE;
21153           /* If it's a `,', then there are more declarators.  */
21154           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21155             {
21156               cp_lexer_consume_token (parser->lexer);
21157               if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21158                 {
21159                   cp_token *token = cp_lexer_previous_token (parser->lexer);
21160                   error_at (token->location,
21161                             "stray %<,%> at end of member declaration");
21162                 }
21163             }
21164           /* If the next token isn't a `;', then we have a parse error.  */
21165           else if (cp_lexer_next_token_is_not (parser->lexer,
21166                                                CPP_SEMICOLON))
21167             {
21168               /* The next token might be a ways away from where the
21169                  actual semicolon is missing.  Find the previous token
21170                  and use that for our error position.  */
21171               cp_token *token = cp_lexer_previous_token (parser->lexer);
21172               error_at (token->location,
21173                         "expected %<;%> at end of member declaration");
21174
21175               /* Assume that the user meant to provide a semicolon.  If
21176                  we were to cp_parser_skip_to_end_of_statement, we might
21177                  skip to a semicolon inside a member function definition
21178                  and issue nonsensical error messages.  */
21179               assume_semicolon = true;
21180             }
21181
21182           if (decl)
21183             {
21184               /* Add DECL to the list of members.  */
21185               if (!friend_p
21186                   /* Explicitly include, eg, NSDMIs, for better error
21187                      recovery (c++/58650).  */
21188                   || !DECL_DECLARES_FUNCTION_P (decl))
21189                 finish_member_declaration (decl);
21190
21191               if (TREE_CODE (decl) == FUNCTION_DECL)
21192                 cp_parser_save_default_args (parser, decl);
21193               else if (TREE_CODE (decl) == FIELD_DECL
21194                        && !DECL_C_BIT_FIELD (decl)
21195                        && DECL_INITIAL (decl))
21196                 /* Add DECL to the queue of NSDMI to be parsed later.  */
21197                 vec_safe_push (unparsed_nsdmis, decl);
21198             }
21199
21200           if (assume_semicolon)
21201             goto out;
21202         }
21203     }
21204
21205   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21206  out:
21207   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
21208 }
21209
21210 /* Parse a pure-specifier.
21211
21212    pure-specifier:
21213      = 0
21214
21215    Returns INTEGER_ZERO_NODE if a pure specifier is found.
21216    Otherwise, ERROR_MARK_NODE is returned.  */
21217
21218 static tree
21219 cp_parser_pure_specifier (cp_parser* parser)
21220 {
21221   cp_token *token;
21222
21223   /* Look for the `=' token.  */
21224   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21225     return error_mark_node;
21226   /* Look for the `0' token.  */
21227   token = cp_lexer_peek_token (parser->lexer);
21228
21229   if (token->type == CPP_EOF
21230       || token->type == CPP_PRAGMA_EOL)
21231     return error_mark_node;
21232
21233   cp_lexer_consume_token (parser->lexer);
21234
21235   /* Accept = default or = delete in c++0x mode.  */
21236   if (token->keyword == RID_DEFAULT
21237       || token->keyword == RID_DELETE)
21238     {
21239       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
21240       return token->u.value;
21241     }
21242
21243   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
21244   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
21245     {
21246       cp_parser_error (parser,
21247                        "invalid pure specifier (only %<= 0%> is allowed)");
21248       cp_parser_skip_to_end_of_statement (parser);
21249       return error_mark_node;
21250     }
21251   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
21252     {
21253       error_at (token->location, "templates may not be %<virtual%>");
21254       return error_mark_node;
21255     }
21256
21257   return integer_zero_node;
21258 }
21259
21260 /* Parse a constant-initializer.
21261
21262    constant-initializer:
21263      = constant-expression
21264
21265    Returns a representation of the constant-expression.  */
21266
21267 static tree
21268 cp_parser_constant_initializer (cp_parser* parser)
21269 {
21270   /* Look for the `=' token.  */
21271   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
21272     return error_mark_node;
21273
21274   /* It is invalid to write:
21275
21276        struct S { static const int i = { 7 }; };
21277
21278      */
21279   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21280     {
21281       cp_parser_error (parser,
21282                        "a brace-enclosed initializer is not allowed here");
21283       /* Consume the opening brace.  */
21284       cp_lexer_consume_token (parser->lexer);
21285       /* Skip the initializer.  */
21286       cp_parser_skip_to_closing_brace (parser);
21287       /* Look for the trailing `}'.  */
21288       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21289
21290       return error_mark_node;
21291     }
21292
21293   return cp_parser_constant_expression (parser);
21294 }
21295
21296 /* Derived classes [gram.class.derived] */
21297
21298 /* Parse a base-clause.
21299
21300    base-clause:
21301      : base-specifier-list
21302
21303    base-specifier-list:
21304      base-specifier ... [opt]
21305      base-specifier-list , base-specifier ... [opt]
21306
21307    Returns a TREE_LIST representing the base-classes, in the order in
21308    which they were declared.  The representation of each node is as
21309    described by cp_parser_base_specifier.
21310
21311    In the case that no bases are specified, this function will return
21312    NULL_TREE, not ERROR_MARK_NODE.  */
21313
21314 static tree
21315 cp_parser_base_clause (cp_parser* parser)
21316 {
21317   tree bases = NULL_TREE;
21318
21319   /* Look for the `:' that begins the list.  */
21320   cp_parser_require (parser, CPP_COLON, RT_COLON);
21321
21322   /* Scan the base-specifier-list.  */
21323   while (true)
21324     {
21325       cp_token *token;
21326       tree base;
21327       bool pack_expansion_p = false;
21328
21329       /* Look for the base-specifier.  */
21330       base = cp_parser_base_specifier (parser);
21331       /* Look for the (optional) ellipsis. */
21332       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21333         {
21334           /* Consume the `...'. */
21335           cp_lexer_consume_token (parser->lexer);
21336
21337           pack_expansion_p = true;
21338         }
21339
21340       /* Add BASE to the front of the list.  */
21341       if (base && base != error_mark_node)
21342         {
21343           if (pack_expansion_p)
21344             /* Make this a pack expansion type. */
21345             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21346
21347           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21348             {
21349               TREE_CHAIN (base) = bases;
21350               bases = base;
21351             }
21352         }
21353       /* Peek at the next token.  */
21354       token = cp_lexer_peek_token (parser->lexer);
21355       /* If it's not a comma, then the list is complete.  */
21356       if (token->type != CPP_COMMA)
21357         break;
21358       /* Consume the `,'.  */
21359       cp_lexer_consume_token (parser->lexer);
21360     }
21361
21362   /* PARSER->SCOPE may still be non-NULL at this point, if the last
21363      base class had a qualified name.  However, the next name that
21364      appears is certainly not qualified.  */
21365   parser->scope = NULL_TREE;
21366   parser->qualifying_scope = NULL_TREE;
21367   parser->object_scope = NULL_TREE;
21368
21369   return nreverse (bases);
21370 }
21371
21372 /* Parse a base-specifier.
21373
21374    base-specifier:
21375      :: [opt] nested-name-specifier [opt] class-name
21376      virtual access-specifier [opt] :: [opt] nested-name-specifier
21377        [opt] class-name
21378      access-specifier virtual [opt] :: [opt] nested-name-specifier
21379        [opt] class-name
21380
21381    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
21382    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21383    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
21384    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
21385
21386 static tree
21387 cp_parser_base_specifier (cp_parser* parser)
21388 {
21389   cp_token *token;
21390   bool done = false;
21391   bool virtual_p = false;
21392   bool duplicate_virtual_error_issued_p = false;
21393   bool duplicate_access_error_issued_p = false;
21394   bool class_scope_p, template_p;
21395   tree access = access_default_node;
21396   tree type;
21397
21398   /* Process the optional `virtual' and `access-specifier'.  */
21399   while (!done)
21400     {
21401       /* Peek at the next token.  */
21402       token = cp_lexer_peek_token (parser->lexer);
21403       /* Process `virtual'.  */
21404       switch (token->keyword)
21405         {
21406         case RID_VIRTUAL:
21407           /* If `virtual' appears more than once, issue an error.  */
21408           if (virtual_p && !duplicate_virtual_error_issued_p)
21409             {
21410               cp_parser_error (parser,
21411                                "%<virtual%> specified more than once in base-specified");
21412               duplicate_virtual_error_issued_p = true;
21413             }
21414
21415           virtual_p = true;
21416
21417           /* Consume the `virtual' token.  */
21418           cp_lexer_consume_token (parser->lexer);
21419
21420           break;
21421
21422         case RID_PUBLIC:
21423         case RID_PROTECTED:
21424         case RID_PRIVATE:
21425           /* If more than one access specifier appears, issue an
21426              error.  */
21427           if (access != access_default_node
21428               && !duplicate_access_error_issued_p)
21429             {
21430               cp_parser_error (parser,
21431                                "more than one access specifier in base-specified");
21432               duplicate_access_error_issued_p = true;
21433             }
21434
21435           access = ridpointers[(int) token->keyword];
21436
21437           /* Consume the access-specifier.  */
21438           cp_lexer_consume_token (parser->lexer);
21439
21440           break;
21441
21442         default:
21443           done = true;
21444           break;
21445         }
21446     }
21447   /* It is not uncommon to see programs mechanically, erroneously, use
21448      the 'typename' keyword to denote (dependent) qualified types
21449      as base classes.  */
21450   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21451     {
21452       token = cp_lexer_peek_token (parser->lexer);
21453       if (!processing_template_decl)
21454         error_at (token->location,
21455                   "keyword %<typename%> not allowed outside of templates");
21456       else
21457         error_at (token->location,
21458                   "keyword %<typename%> not allowed in this context "
21459                   "(the base class is implicitly a type)");
21460       cp_lexer_consume_token (parser->lexer);
21461     }
21462
21463   /* Look for the optional `::' operator.  */
21464   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21465   /* Look for the nested-name-specifier.  The simplest way to
21466      implement:
21467
21468        [temp.res]
21469
21470        The keyword `typename' is not permitted in a base-specifier or
21471        mem-initializer; in these contexts a qualified name that
21472        depends on a template-parameter is implicitly assumed to be a
21473        type name.
21474
21475      is to pretend that we have seen the `typename' keyword at this
21476      point.  */
21477   cp_parser_nested_name_specifier_opt (parser,
21478                                        /*typename_keyword_p=*/true,
21479                                        /*check_dependency_p=*/true,
21480                                        typename_type,
21481                                        /*is_declaration=*/true);
21482   /* If the base class is given by a qualified name, assume that names
21483      we see are type names or templates, as appropriate.  */
21484   class_scope_p = (parser->scope && TYPE_P (parser->scope));
21485   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21486
21487   if (!parser->scope
21488       && cp_lexer_next_token_is_decltype (parser->lexer))
21489     /* DR 950 allows decltype as a base-specifier.  */
21490     type = cp_parser_decltype (parser);
21491   else
21492     {
21493       /* Otherwise, look for the class-name.  */
21494       type = cp_parser_class_name (parser,
21495                                    class_scope_p,
21496                                    template_p,
21497                                    typename_type,
21498                                    /*check_dependency_p=*/true,
21499                                    /*class_head_p=*/false,
21500                                    /*is_declaration=*/true);
21501       type = TREE_TYPE (type);
21502     }
21503
21504   if (type == error_mark_node)
21505     return error_mark_node;
21506
21507   return finish_base_specifier (type, access, virtual_p);
21508 }
21509
21510 /* Exception handling [gram.exception] */
21511
21512 /* Parse an (optional) noexcept-specification.
21513
21514    noexcept-specification:
21515      noexcept ( constant-expression ) [opt]
21516
21517    If no noexcept-specification is present, returns NULL_TREE.
21518    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21519    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21520    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
21521    Otherwise, returns a noexcept specification unless RETURN_COND is true,
21522    in which case a boolean condition is returned instead.  */
21523
21524 static tree
21525 cp_parser_noexcept_specification_opt (cp_parser* parser,
21526                                       bool require_constexpr,
21527                                       bool* consumed_expr,
21528                                       bool return_cond)
21529 {
21530   cp_token *token;
21531   const char *saved_message;
21532
21533   /* Peek at the next token.  */
21534   token = cp_lexer_peek_token (parser->lexer);
21535
21536   /* Is it a noexcept-specification?  */
21537   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21538     {
21539       tree expr;
21540       cp_lexer_consume_token (parser->lexer);
21541
21542       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21543         {
21544           cp_lexer_consume_token (parser->lexer);
21545
21546           if (require_constexpr)
21547             {
21548               /* Types may not be defined in an exception-specification.  */
21549               saved_message = parser->type_definition_forbidden_message;
21550               parser->type_definition_forbidden_message
21551               = G_("types may not be defined in an exception-specification");
21552
21553               expr = cp_parser_constant_expression (parser);
21554
21555               /* Restore the saved message.  */
21556               parser->type_definition_forbidden_message = saved_message;
21557             }
21558           else
21559             {
21560               expr = cp_parser_expression (parser);
21561               *consumed_expr = true;
21562             }
21563
21564           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21565         }
21566       else
21567         {
21568           expr = boolean_true_node;
21569           if (!require_constexpr)
21570             *consumed_expr = false;
21571         }
21572
21573       /* We cannot build a noexcept-spec right away because this will check
21574          that expr is a constexpr.  */
21575       if (!return_cond)
21576         return build_noexcept_spec (expr, tf_warning_or_error);
21577       else
21578         return expr;
21579     }
21580   else
21581     return NULL_TREE;
21582 }
21583
21584 /* Parse an (optional) exception-specification.
21585
21586    exception-specification:
21587      throw ( type-id-list [opt] )
21588
21589    Returns a TREE_LIST representing the exception-specification.  The
21590    TREE_VALUE of each node is a type.  */
21591
21592 static tree
21593 cp_parser_exception_specification_opt (cp_parser* parser)
21594 {
21595   cp_token *token;
21596   tree type_id_list;
21597   const char *saved_message;
21598
21599   /* Peek at the next token.  */
21600   token = cp_lexer_peek_token (parser->lexer);
21601
21602   /* Is it a noexcept-specification?  */
21603   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21604                                                       false);
21605   if (type_id_list != NULL_TREE)
21606     return type_id_list;
21607
21608   /* If it's not `throw', then there's no exception-specification.  */
21609   if (!cp_parser_is_keyword (token, RID_THROW))
21610     return NULL_TREE;
21611
21612 #if 0
21613   /* Enable this once a lot of code has transitioned to noexcept?  */
21614   if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21615     warning (OPT_Wdeprecated, "dynamic exception specifications are "
21616              "deprecated in C++0x; use %<noexcept%> instead");
21617 #endif
21618
21619   /* Consume the `throw'.  */
21620   cp_lexer_consume_token (parser->lexer);
21621
21622   /* Look for the `('.  */
21623   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21624
21625   /* Peek at the next token.  */
21626   token = cp_lexer_peek_token (parser->lexer);
21627   /* If it's not a `)', then there is a type-id-list.  */
21628   if (token->type != CPP_CLOSE_PAREN)
21629     {
21630       /* Types may not be defined in an exception-specification.  */
21631       saved_message = parser->type_definition_forbidden_message;
21632       parser->type_definition_forbidden_message
21633         = G_("types may not be defined in an exception-specification");
21634       /* Parse the type-id-list.  */
21635       type_id_list = cp_parser_type_id_list (parser);
21636       /* Restore the saved message.  */
21637       parser->type_definition_forbidden_message = saved_message;
21638     }
21639   else
21640     type_id_list = empty_except_spec;
21641
21642   /* Look for the `)'.  */
21643   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21644
21645   return type_id_list;
21646 }
21647
21648 /* Parse an (optional) type-id-list.
21649
21650    type-id-list:
21651      type-id ... [opt]
21652      type-id-list , type-id ... [opt]
21653
21654    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
21655    in the order that the types were presented.  */
21656
21657 static tree
21658 cp_parser_type_id_list (cp_parser* parser)
21659 {
21660   tree types = NULL_TREE;
21661
21662   while (true)
21663     {
21664       cp_token *token;
21665       tree type;
21666
21667       /* Get the next type-id.  */
21668       type = cp_parser_type_id (parser);
21669       /* Parse the optional ellipsis. */
21670       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21671         {
21672           /* Consume the `...'. */
21673           cp_lexer_consume_token (parser->lexer);
21674
21675           /* Turn the type into a pack expansion expression. */
21676           type = make_pack_expansion (type);
21677         }
21678       /* Add it to the list.  */
21679       types = add_exception_specifier (types, type, /*complain=*/1);
21680       /* Peek at the next token.  */
21681       token = cp_lexer_peek_token (parser->lexer);
21682       /* If it is not a `,', we are done.  */
21683       if (token->type != CPP_COMMA)
21684         break;
21685       /* Consume the `,'.  */
21686       cp_lexer_consume_token (parser->lexer);
21687     }
21688
21689   return nreverse (types);
21690 }
21691
21692 /* Parse a try-block.
21693
21694    try-block:
21695      try compound-statement handler-seq  */
21696
21697 static tree
21698 cp_parser_try_block (cp_parser* parser)
21699 {
21700   tree try_block;
21701
21702   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21703   if (parser->in_function_body
21704       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
21705     error ("%<try%> in %<constexpr%> function");
21706
21707   try_block = begin_try_block ();
21708   cp_parser_compound_statement (parser, NULL, true, false);
21709   finish_try_block (try_block);
21710   cp_parser_handler_seq (parser);
21711   finish_handler_sequence (try_block);
21712
21713   return try_block;
21714 }
21715
21716 /* Parse a function-try-block.
21717
21718    function-try-block:
21719      try ctor-initializer [opt] function-body handler-seq  */
21720
21721 static bool
21722 cp_parser_function_try_block (cp_parser* parser)
21723 {
21724   tree compound_stmt;
21725   tree try_block;
21726   bool ctor_initializer_p;
21727
21728   /* Look for the `try' keyword.  */
21729   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21730     return false;
21731   /* Let the rest of the front end know where we are.  */
21732   try_block = begin_function_try_block (&compound_stmt);
21733   /* Parse the function-body.  */
21734   ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21735     (parser, /*in_function_try_block=*/true);
21736   /* We're done with the `try' part.  */
21737   finish_function_try_block (try_block);
21738   /* Parse the handlers.  */
21739   cp_parser_handler_seq (parser);
21740   /* We're done with the handlers.  */
21741   finish_function_handler_sequence (try_block, compound_stmt);
21742
21743   return ctor_initializer_p;
21744 }
21745
21746 /* Parse a handler-seq.
21747
21748    handler-seq:
21749      handler handler-seq [opt]  */
21750
21751 static void
21752 cp_parser_handler_seq (cp_parser* parser)
21753 {
21754   while (true)
21755     {
21756       cp_token *token;
21757
21758       /* Parse the handler.  */
21759       cp_parser_handler (parser);
21760       /* Peek at the next token.  */
21761       token = cp_lexer_peek_token (parser->lexer);
21762       /* If it's not `catch' then there are no more handlers.  */
21763       if (!cp_parser_is_keyword (token, RID_CATCH))
21764         break;
21765     }
21766 }
21767
21768 /* Parse a handler.
21769
21770    handler:
21771      catch ( exception-declaration ) compound-statement  */
21772
21773 static void
21774 cp_parser_handler (cp_parser* parser)
21775 {
21776   tree handler;
21777   tree declaration;
21778
21779   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21780   handler = begin_handler ();
21781   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21782   declaration = cp_parser_exception_declaration (parser);
21783   finish_handler_parms (declaration, handler);
21784   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21785   cp_parser_compound_statement (parser, NULL, false, false);
21786   finish_handler (handler);
21787 }
21788
21789 /* Parse an exception-declaration.
21790
21791    exception-declaration:
21792      type-specifier-seq declarator
21793      type-specifier-seq abstract-declarator
21794      type-specifier-seq
21795      ...
21796
21797    Returns a VAR_DECL for the declaration, or NULL_TREE if the
21798    ellipsis variant is used.  */
21799
21800 static tree
21801 cp_parser_exception_declaration (cp_parser* parser)
21802 {
21803   cp_decl_specifier_seq type_specifiers;
21804   cp_declarator *declarator;
21805   const char *saved_message;
21806
21807   /* If it's an ellipsis, it's easy to handle.  */
21808   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21809     {
21810       /* Consume the `...' token.  */
21811       cp_lexer_consume_token (parser->lexer);
21812       return NULL_TREE;
21813     }
21814
21815   /* Types may not be defined in exception-declarations.  */
21816   saved_message = parser->type_definition_forbidden_message;
21817   parser->type_definition_forbidden_message
21818     = G_("types may not be defined in exception-declarations");
21819
21820   /* Parse the type-specifier-seq.  */
21821   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21822                                 /*is_trailing_return=*/false,
21823                                 &type_specifiers);
21824   /* If it's a `)', then there is no declarator.  */
21825   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21826     declarator = NULL;
21827   else
21828     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21829                                        /*ctor_dtor_or_conv_p=*/NULL,
21830                                        /*parenthesized_p=*/NULL,
21831                                        /*member_p=*/false,
21832                                        /*friend_p=*/false);
21833
21834   /* Restore the saved message.  */
21835   parser->type_definition_forbidden_message = saved_message;
21836
21837   if (!type_specifiers.any_specifiers_p)
21838     return error_mark_node;
21839
21840   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21841 }
21842
21843 /* Parse a throw-expression.
21844
21845    throw-expression:
21846      throw assignment-expression [opt]
21847
21848    Returns a THROW_EXPR representing the throw-expression.  */
21849
21850 static tree
21851 cp_parser_throw_expression (cp_parser* parser)
21852 {
21853   tree expression;
21854   cp_token* token;
21855
21856   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21857   token = cp_lexer_peek_token (parser->lexer);
21858   /* Figure out whether or not there is an assignment-expression
21859      following the "throw" keyword.  */
21860   if (token->type == CPP_COMMA
21861       || token->type == CPP_SEMICOLON
21862       || token->type == CPP_CLOSE_PAREN
21863       || token->type == CPP_CLOSE_SQUARE
21864       || token->type == CPP_CLOSE_BRACE
21865       || token->type == CPP_COLON)
21866     expression = NULL_TREE;
21867   else
21868     expression = cp_parser_assignment_expression (parser);
21869
21870   return build_throw (expression);
21871 }
21872
21873 /* GNU Extensions */
21874
21875 /* Parse an (optional) asm-specification.
21876
21877    asm-specification:
21878      asm ( string-literal )
21879
21880    If the asm-specification is present, returns a STRING_CST
21881    corresponding to the string-literal.  Otherwise, returns
21882    NULL_TREE.  */
21883
21884 static tree
21885 cp_parser_asm_specification_opt (cp_parser* parser)
21886 {
21887   cp_token *token;
21888   tree asm_specification;
21889
21890   /* Peek at the next token.  */
21891   token = cp_lexer_peek_token (parser->lexer);
21892   /* If the next token isn't the `asm' keyword, then there's no
21893      asm-specification.  */
21894   if (!cp_parser_is_keyword (token, RID_ASM))
21895     return NULL_TREE;
21896
21897   /* Consume the `asm' token.  */
21898   cp_lexer_consume_token (parser->lexer);
21899   /* Look for the `('.  */
21900   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21901
21902   /* Look for the string-literal.  */
21903   asm_specification = cp_parser_string_literal (parser, false, false);
21904
21905   /* Look for the `)'.  */
21906   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21907
21908   return asm_specification;
21909 }
21910
21911 /* Parse an asm-operand-list.
21912
21913    asm-operand-list:
21914      asm-operand
21915      asm-operand-list , asm-operand
21916
21917    asm-operand:
21918      string-literal ( expression )
21919      [ string-literal ] string-literal ( expression )
21920
21921    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
21922    each node is the expression.  The TREE_PURPOSE is itself a
21923    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21924    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21925    is a STRING_CST for the string literal before the parenthesis. Returns
21926    ERROR_MARK_NODE if any of the operands are invalid.  */
21927
21928 static tree
21929 cp_parser_asm_operand_list (cp_parser* parser)
21930 {
21931   tree asm_operands = NULL_TREE;
21932   bool invalid_operands = false;
21933
21934   while (true)
21935     {
21936       tree string_literal;
21937       tree expression;
21938       tree name;
21939
21940       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21941         {
21942           /* Consume the `[' token.  */
21943           cp_lexer_consume_token (parser->lexer);
21944           /* Read the operand name.  */
21945           name = cp_parser_identifier (parser);
21946           if (name != error_mark_node)
21947             name = build_string (IDENTIFIER_LENGTH (name),
21948                                  IDENTIFIER_POINTER (name));
21949           /* Look for the closing `]'.  */
21950           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21951         }
21952       else
21953         name = NULL_TREE;
21954       /* Look for the string-literal.  */
21955       string_literal = cp_parser_string_literal (parser, false, false);
21956
21957       /* Look for the `('.  */
21958       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21959       /* Parse the expression.  */
21960       expression = cp_parser_expression (parser);
21961       /* Look for the `)'.  */
21962       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21963
21964       if (name == error_mark_node 
21965           || string_literal == error_mark_node 
21966           || expression == error_mark_node)
21967         invalid_operands = true;
21968
21969       /* Add this operand to the list.  */
21970       asm_operands = tree_cons (build_tree_list (name, string_literal),
21971                                 expression,
21972                                 asm_operands);
21973       /* If the next token is not a `,', there are no more
21974          operands.  */
21975       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21976         break;
21977       /* Consume the `,'.  */
21978       cp_lexer_consume_token (parser->lexer);
21979     }
21980
21981   return invalid_operands ? error_mark_node : nreverse (asm_operands);
21982 }
21983
21984 /* Parse an asm-clobber-list.
21985
21986    asm-clobber-list:
21987      string-literal
21988      asm-clobber-list , string-literal
21989
21990    Returns a TREE_LIST, indicating the clobbers in the order that they
21991    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
21992
21993 static tree
21994 cp_parser_asm_clobber_list (cp_parser* parser)
21995 {
21996   tree clobbers = NULL_TREE;
21997
21998   while (true)
21999     {
22000       tree string_literal;
22001
22002       /* Look for the string literal.  */
22003       string_literal = cp_parser_string_literal (parser, false, false);
22004       /* Add it to the list.  */
22005       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
22006       /* If the next token is not a `,', then the list is
22007          complete.  */
22008       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22009         break;
22010       /* Consume the `,' token.  */
22011       cp_lexer_consume_token (parser->lexer);
22012     }
22013
22014   return clobbers;
22015 }
22016
22017 /* Parse an asm-label-list.
22018
22019    asm-label-list:
22020      identifier
22021      asm-label-list , identifier
22022
22023    Returns a TREE_LIST, indicating the labels in the order that they
22024    appeared.  The TREE_VALUE of each node is a label.  */
22025
22026 static tree
22027 cp_parser_asm_label_list (cp_parser* parser)
22028 {
22029   tree labels = NULL_TREE;
22030
22031   while (true)
22032     {
22033       tree identifier, label, name;
22034
22035       /* Look for the identifier.  */
22036       identifier = cp_parser_identifier (parser);
22037       if (!error_operand_p (identifier))
22038         {
22039           label = lookup_label (identifier);
22040           if (TREE_CODE (label) == LABEL_DECL)
22041             {
22042               TREE_USED (label) = 1;
22043               check_goto (label);
22044               name = build_string (IDENTIFIER_LENGTH (identifier),
22045                                    IDENTIFIER_POINTER (identifier));
22046               labels = tree_cons (name, label, labels);
22047             }
22048         }
22049       /* If the next token is not a `,', then the list is
22050          complete.  */
22051       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22052         break;
22053       /* Consume the `,' token.  */
22054       cp_lexer_consume_token (parser->lexer);
22055     }
22056
22057   return nreverse (labels);
22058 }
22059
22060 /* Return TRUE iff the next tokens in the stream are possibly the
22061    beginning of a GNU extension attribute. */
22062
22063 static bool
22064 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
22065 {
22066   return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
22067 }
22068
22069 /* Return TRUE iff the next tokens in the stream are possibly the
22070    beginning of a standard C++-11 attribute specifier.  */
22071
22072 static bool
22073 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
22074 {
22075   return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
22076 }
22077
22078 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22079    beginning of a standard C++-11 attribute specifier.  */
22080
22081 static bool
22082 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
22083 {
22084   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22085
22086   return (cxx_dialect >= cxx11
22087           && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
22088               || (token->type == CPP_OPEN_SQUARE
22089                   && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
22090                   && token->type == CPP_OPEN_SQUARE)));
22091 }
22092
22093 /* Return TRUE iff the next Nth tokens in the stream are possibly the
22094    beginning of a GNU extension attribute.  */
22095
22096 static bool
22097 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
22098 {
22099   cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
22100
22101   return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
22102 }
22103
22104 /* Return true iff the next tokens can be the beginning of either a
22105    GNU attribute list, or a standard C++11 attribute sequence.  */
22106
22107 static bool
22108 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
22109 {
22110   return (cp_next_tokens_can_be_gnu_attribute_p (parser)
22111           || cp_next_tokens_can_be_std_attribute_p (parser));
22112 }
22113
22114 /* Return true iff the next Nth tokens can be the beginning of either
22115    a GNU attribute list, or a standard C++11 attribute sequence.  */
22116
22117 static bool
22118 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
22119 {
22120   return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
22121           || cp_nth_tokens_can_be_std_attribute_p (parser, n));
22122 }
22123
22124 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
22125    of GNU attributes, or return NULL.  */
22126
22127 static tree
22128 cp_parser_attributes_opt (cp_parser *parser)
22129 {
22130   if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22131       return cp_parser_gnu_attributes_opt (parser);
22132   return cp_parser_std_attribute_spec_seq (parser);
22133 }
22134
22135 #define CILK_SIMD_FN_CLAUSE_MASK                                  \
22136         ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH)   \
22137          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR)       \
22138          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM)      \
22139          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK)         \
22140          | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
22141
22142 /* Parses the Cilk Plus SIMD-enabled function's attribute.  Syntax:
22143    vector [(<clauses>)]  */
22144
22145 static void
22146 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
22147 {  
22148   bool first_p = parser->cilk_simd_fn_info == NULL;
22149   cp_token *token = v_token;
22150   if (first_p)
22151     {
22152       parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
22153       parser->cilk_simd_fn_info->error_seen = false;
22154       parser->cilk_simd_fn_info->fndecl_seen = false;
22155       parser->cilk_simd_fn_info->tokens = vNULL;
22156     }
22157   int paren_scope = 0;
22158   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22159     {
22160       cp_lexer_consume_token (parser->lexer);
22161       v_token = cp_lexer_peek_token (parser->lexer);
22162       paren_scope++;
22163     }
22164   while (paren_scope > 0)
22165     {
22166       token = cp_lexer_peek_token (parser->lexer);
22167       if (token->type == CPP_OPEN_PAREN)
22168         paren_scope++;
22169       else if (token->type == CPP_CLOSE_PAREN)
22170         paren_scope--;
22171       /* Do not push the last ')'  */
22172       if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
22173         cp_lexer_consume_token (parser->lexer);
22174     }
22175
22176   token->type = CPP_PRAGMA_EOL;
22177   parser->lexer->next_token = token;
22178   cp_lexer_consume_token (parser->lexer);
22179
22180   struct cp_token_cache *cp
22181     = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
22182   parser->cilk_simd_fn_info->tokens.safe_push (cp);
22183 }
22184
22185 /* Parse an (optional) series of attributes.
22186
22187    attributes:
22188      attributes attribute
22189
22190    attribute:
22191      __attribute__ (( attribute-list [opt] ))
22192
22193    The return value is as for cp_parser_gnu_attribute_list.  */
22194
22195 static tree
22196 cp_parser_gnu_attributes_opt (cp_parser* parser)
22197 {
22198   tree attributes = NULL_TREE;
22199
22200   while (true)
22201     {
22202       cp_token *token;
22203       tree attribute_list;
22204       bool ok = true;
22205
22206       /* Peek at the next token.  */
22207       token = cp_lexer_peek_token (parser->lexer);
22208       /* If it's not `__attribute__', then we're done.  */
22209       if (token->keyword != RID_ATTRIBUTE)
22210         break;
22211
22212       /* Consume the `__attribute__' keyword.  */
22213       cp_lexer_consume_token (parser->lexer);
22214       /* Look for the two `(' tokens.  */
22215       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22216       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22217
22218       /* Peek at the next token.  */
22219       token = cp_lexer_peek_token (parser->lexer);
22220       if (token->type != CPP_CLOSE_PAREN)
22221         /* Parse the attribute-list.  */
22222         attribute_list = cp_parser_gnu_attribute_list (parser);
22223       else
22224         /* If the next token is a `)', then there is no attribute
22225            list.  */
22226         attribute_list = NULL;
22227
22228       /* Look for the two `)' tokens.  */
22229       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22230         ok = false;
22231       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
22232         ok = false;
22233       if (!ok)
22234         cp_parser_skip_to_end_of_statement (parser);
22235
22236       /* Add these new attributes to the list.  */
22237       attributes = chainon (attributes, attribute_list);
22238     }
22239
22240   return attributes;
22241 }
22242
22243 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
22244    "__vector" or "__vector__."  */
22245
22246 static inline bool
22247 is_cilkplus_vector_p (tree name)
22248
22249   if (flag_cilkplus && is_attribute_p ("vector", name)) 
22250     return true;
22251   return false;
22252 }
22253
22254 /* Parse a GNU attribute-list.
22255
22256    attribute-list:
22257      attribute
22258      attribute-list , attribute
22259
22260    attribute:
22261      identifier
22262      identifier ( identifier )
22263      identifier ( identifier , expression-list )
22264      identifier ( expression-list )
22265
22266    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
22267    to an attribute.  The TREE_PURPOSE of each node is the identifier
22268    indicating which attribute is in use.  The TREE_VALUE represents
22269    the arguments, if any.  */
22270
22271 static tree
22272 cp_parser_gnu_attribute_list (cp_parser* parser)
22273 {
22274   tree attribute_list = NULL_TREE;
22275   bool save_translate_strings_p = parser->translate_strings_p;
22276
22277   parser->translate_strings_p = false;
22278   while (true)
22279     {
22280       cp_token *token;
22281       tree identifier;
22282       tree attribute;
22283
22284       /* Look for the identifier.  We also allow keywords here; for
22285          example `__attribute__ ((const))' is legal.  */
22286       token = cp_lexer_peek_token (parser->lexer);
22287       if (token->type == CPP_NAME
22288           || token->type == CPP_KEYWORD)
22289         {
22290           tree arguments = NULL_TREE;
22291
22292           /* Consume the token, but save it since we need it for the
22293              SIMD enabled function parsing.  */
22294           cp_token *id_token = cp_lexer_consume_token (parser->lexer);
22295
22296           /* Save away the identifier that indicates which attribute
22297              this is.  */
22298           identifier = (token->type == CPP_KEYWORD) 
22299             /* For keywords, use the canonical spelling, not the
22300                parsed identifier.  */
22301             ? ridpointers[(int) token->keyword]
22302             : id_token->u.value;
22303           
22304           attribute = build_tree_list (identifier, NULL_TREE);
22305
22306           /* Peek at the next token.  */
22307           token = cp_lexer_peek_token (parser->lexer);
22308           /* If it's an `(', then parse the attribute arguments.  */
22309           if (token->type == CPP_OPEN_PAREN)
22310             {
22311               vec<tree, va_gc> *vec;
22312               int attr_flag = (attribute_takes_identifier_p (identifier)
22313                                ? id_attr : normal_attr);
22314               if (is_cilkplus_vector_p (identifier))
22315                 {
22316                   cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22317                   continue;
22318                 }
22319               else
22320                 vec = cp_parser_parenthesized_expression_list 
22321                   (parser, attr_flag, /*cast_p=*/false, 
22322                    /*allow_expansion_p=*/false, 
22323                    /*non_constant_p=*/NULL);
22324               if (vec == NULL)
22325                 arguments = error_mark_node;
22326               else
22327                 {
22328                   arguments = build_tree_list_vec (vec);
22329                   release_tree_vector (vec);
22330                 }
22331               /* Save the arguments away.  */
22332               TREE_VALUE (attribute) = arguments;
22333             }
22334           else if (is_cilkplus_vector_p (identifier))
22335             {
22336               cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22337               continue;
22338             }
22339
22340           if (arguments != error_mark_node)
22341             {
22342               /* Add this attribute to the list.  */
22343               TREE_CHAIN (attribute) = attribute_list;
22344               attribute_list = attribute;
22345             }
22346
22347           token = cp_lexer_peek_token (parser->lexer);
22348         }
22349       /* Now, look for more attributes.  If the next token isn't a
22350          `,', we're done.  */
22351       if (token->type != CPP_COMMA)
22352         break;
22353
22354       /* Consume the comma and keep going.  */
22355       cp_lexer_consume_token (parser->lexer);
22356     }
22357   parser->translate_strings_p = save_translate_strings_p;
22358
22359   /* We built up the list in reverse order.  */
22360   return nreverse (attribute_list);
22361 }
22362
22363 /*  Parse a standard C++11 attribute.
22364
22365     The returned representation is a TREE_LIST which TREE_PURPOSE is
22366     the scoped name of the attribute, and the TREE_VALUE is its
22367     arguments list.
22368
22369     Note that the scoped name of the attribute is itself a TREE_LIST
22370     which TREE_PURPOSE is the namespace of the attribute, and
22371     TREE_VALUE its name.  This is unlike a GNU attribute -- as parsed
22372     by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22373     and which TREE_PURPOSE is directly the attribute name.
22374
22375     Clients of the attribute code should use get_attribute_namespace
22376     and get_attribute_name to get the actual namespace and name of
22377     attributes, regardless of their being GNU or C++11 attributes.
22378
22379     attribute:
22380       attribute-token attribute-argument-clause [opt]
22381
22382     attribute-token:
22383       identifier
22384       attribute-scoped-token
22385
22386     attribute-scoped-token:
22387       attribute-namespace :: identifier
22388
22389     attribute-namespace:
22390       identifier
22391
22392     attribute-argument-clause:
22393       ( balanced-token-seq )
22394
22395     balanced-token-seq:
22396       balanced-token [opt]
22397       balanced-token-seq balanced-token
22398
22399     balanced-token:
22400       ( balanced-token-seq )
22401       [ balanced-token-seq ]
22402       { balanced-token-seq }.  */
22403
22404 static tree
22405 cp_parser_std_attribute (cp_parser *parser)
22406 {
22407   tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22408   cp_token *token;
22409
22410   /* First, parse name of the the attribute, a.k.a
22411      attribute-token.  */
22412
22413   token = cp_lexer_peek_token (parser->lexer);
22414   if (token->type == CPP_NAME)
22415     attr_id = token->u.value;
22416   else if (token->type == CPP_KEYWORD)
22417     attr_id = ridpointers[(int) token->keyword];
22418   else if (token->flags & NAMED_OP)
22419     attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22420
22421   if (attr_id == NULL_TREE)
22422     return NULL_TREE;
22423
22424   cp_lexer_consume_token (parser->lexer);
22425
22426   token = cp_lexer_peek_token (parser->lexer);
22427   if (token->type == CPP_SCOPE)
22428     {
22429       /* We are seeing a scoped attribute token.  */
22430
22431       cp_lexer_consume_token (parser->lexer);
22432       attr_ns = attr_id;
22433
22434       token = cp_lexer_consume_token (parser->lexer);
22435       if (token->type == CPP_NAME)
22436         attr_id = token->u.value;
22437       else if (token->type == CPP_KEYWORD)
22438         attr_id = ridpointers[(int) token->keyword];
22439       else
22440         {
22441           error_at (token->location,
22442                     "expected an identifier for the attribute name");
22443           return error_mark_node;
22444         }
22445       attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22446                                    NULL_TREE);
22447       token = cp_lexer_peek_token (parser->lexer);
22448     }
22449   else
22450     {
22451       attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22452                                    NULL_TREE);
22453       /* C++11 noreturn attribute is equivalent to GNU's.  */
22454       if (is_attribute_p ("noreturn", attr_id))
22455         TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22456       /* C++14 deprecated attribute is equivalent to GNU's.  */
22457       else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
22458         {
22459           if (cxx_dialect == cxx11)
22460             pedwarn (token->location, OPT_Wpedantic,
22461                      "%<deprecated%> is a C++14 feature;"
22462                      " use %<gnu::deprecated%>");
22463           TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22464         }
22465     }
22466
22467   /* Now parse the optional argument clause of the attribute.  */
22468
22469   if (token->type != CPP_OPEN_PAREN)
22470     return attribute;
22471
22472   {
22473     vec<tree, va_gc> *vec;
22474     int attr_flag = normal_attr;
22475
22476     if (attr_ns == get_identifier ("gnu")
22477         && attribute_takes_identifier_p (attr_id))
22478       /* A GNU attribute that takes an identifier in parameter.  */
22479       attr_flag = id_attr;
22480
22481     vec = cp_parser_parenthesized_expression_list
22482       (parser, attr_flag, /*cast_p=*/false,
22483        /*allow_expansion_p=*/true,
22484        /*non_constant_p=*/NULL);
22485     if (vec == NULL)
22486       arguments = error_mark_node;
22487     else
22488       {
22489         arguments = build_tree_list_vec (vec);
22490         release_tree_vector (vec);
22491       }
22492
22493     if (arguments == error_mark_node)
22494       attribute = error_mark_node;
22495     else
22496       TREE_VALUE (attribute) = arguments;
22497   }
22498
22499   return attribute;
22500 }
22501
22502 /* Parse a list of standard C++-11 attributes.
22503
22504    attribute-list:
22505      attribute [opt]
22506      attribute-list , attribute[opt]
22507      attribute ...
22508      attribute-list , attribute ...
22509 */
22510
22511 static tree
22512 cp_parser_std_attribute_list (cp_parser *parser)
22513 {
22514   tree attributes = NULL_TREE, attribute = NULL_TREE;
22515   cp_token *token = NULL;
22516
22517   while (true)
22518     {
22519       attribute = cp_parser_std_attribute (parser);
22520       if (attribute == error_mark_node)
22521         break;
22522       if (attribute != NULL_TREE)
22523         {
22524           TREE_CHAIN (attribute) = attributes;
22525           attributes = attribute;
22526         }
22527       token = cp_lexer_peek_token (parser->lexer);
22528       if (token->type == CPP_ELLIPSIS)
22529         {
22530           cp_lexer_consume_token (parser->lexer);
22531           TREE_VALUE (attribute)
22532             = make_pack_expansion (TREE_VALUE (attribute));
22533           token = cp_lexer_peek_token (parser->lexer);
22534         }
22535       if (token->type != CPP_COMMA)
22536         break;
22537       cp_lexer_consume_token (parser->lexer);
22538     }
22539   attributes = nreverse (attributes);
22540   return attributes;
22541 }
22542
22543 /* Parse a standard C++-11 attribute specifier.
22544
22545    attribute-specifier:
22546      [ [ attribute-list ] ]
22547      alignment-specifier
22548
22549    alignment-specifier:
22550      alignas ( type-id ... [opt] )
22551      alignas ( alignment-expression ... [opt] ).  */
22552
22553 static tree
22554 cp_parser_std_attribute_spec (cp_parser *parser)
22555 {
22556   tree attributes = NULL_TREE;
22557   cp_token *token = cp_lexer_peek_token (parser->lexer);
22558
22559   if (token->type == CPP_OPEN_SQUARE
22560       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22561     {
22562       cp_lexer_consume_token (parser->lexer);
22563       cp_lexer_consume_token (parser->lexer);
22564
22565       attributes = cp_parser_std_attribute_list (parser);
22566
22567       if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22568           || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22569         cp_parser_skip_to_end_of_statement (parser);
22570       else
22571         /* Warn about parsing c++11 attribute in non-c++1 mode, only
22572            when we are sure that we have actually parsed them.  */
22573         maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22574     }
22575   else
22576     {
22577       tree alignas_expr;
22578
22579       /* Look for an alignment-specifier.  */
22580
22581       token = cp_lexer_peek_token (parser->lexer);
22582
22583       if (token->type != CPP_KEYWORD
22584           || token->keyword != RID_ALIGNAS)
22585         return NULL_TREE;
22586
22587       cp_lexer_consume_token (parser->lexer);
22588       maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22589
22590       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22591         {
22592           cp_parser_error (parser, "expected %<(%>");
22593           return error_mark_node;
22594         }
22595
22596       cp_parser_parse_tentatively (parser);
22597       alignas_expr = cp_parser_type_id (parser);
22598
22599       if (!cp_parser_parse_definitely (parser))
22600         {
22601           gcc_assert (alignas_expr == error_mark_node
22602                       || alignas_expr == NULL_TREE);
22603
22604           alignas_expr =
22605             cp_parser_assignment_expression (parser);
22606           if (alignas_expr == error_mark_node)
22607             cp_parser_skip_to_end_of_statement (parser);
22608           if (alignas_expr == NULL_TREE
22609               || alignas_expr == error_mark_node)
22610             return alignas_expr;
22611         }
22612
22613       alignas_expr = cxx_alignas_expr (alignas_expr);
22614       alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
22615
22616       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22617         {
22618           cp_lexer_consume_token (parser->lexer);
22619           alignas_expr = make_pack_expansion (alignas_expr);
22620         }
22621
22622       if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22623         {
22624           cp_parser_error (parser, "expected %<)%>");
22625           return error_mark_node;
22626         }
22627
22628       /* Build the C++-11 representation of an 'aligned'
22629          attribute.  */
22630       attributes =
22631         build_tree_list (build_tree_list (get_identifier ("gnu"),
22632                                           get_identifier ("aligned")),
22633                          alignas_expr);
22634     }
22635
22636   return attributes;
22637 }
22638
22639 /* Parse a standard C++-11 attribute-specifier-seq.
22640
22641    attribute-specifier-seq:
22642      attribute-specifier-seq [opt] attribute-specifier
22643  */
22644
22645 static tree
22646 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22647 {
22648   tree attr_specs = NULL_TREE;
22649   tree attr_last = NULL_TREE;
22650
22651   while (true)
22652     {
22653       tree attr_spec = cp_parser_std_attribute_spec (parser);
22654       if (attr_spec == NULL_TREE)
22655         break;
22656       if (attr_spec == error_mark_node)
22657         return error_mark_node;
22658
22659       if (attr_last)
22660         TREE_CHAIN (attr_last) = attr_spec;
22661       else
22662         attr_specs = attr_last = attr_spec;
22663       attr_last = tree_last (attr_last);
22664     }
22665
22666   return attr_specs;
22667 }
22668
22669 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
22670    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
22671    current value of the PEDANTIC flag, regardless of whether or not
22672    the `__extension__' keyword is present.  The caller is responsible
22673    for restoring the value of the PEDANTIC flag.  */
22674
22675 static bool
22676 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22677 {
22678   /* Save the old value of the PEDANTIC flag.  */
22679   *saved_pedantic = pedantic;
22680
22681   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22682     {
22683       /* Consume the `__extension__' token.  */
22684       cp_lexer_consume_token (parser->lexer);
22685       /* We're not being pedantic while the `__extension__' keyword is
22686          in effect.  */
22687       pedantic = 0;
22688
22689       return true;
22690     }
22691
22692   return false;
22693 }
22694
22695 /* Parse a label declaration.
22696
22697    label-declaration:
22698      __label__ label-declarator-seq ;
22699
22700    label-declarator-seq:
22701      identifier , label-declarator-seq
22702      identifier  */
22703
22704 static void
22705 cp_parser_label_declaration (cp_parser* parser)
22706 {
22707   /* Look for the `__label__' keyword.  */
22708   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22709
22710   while (true)
22711     {
22712       tree identifier;
22713
22714       /* Look for an identifier.  */
22715       identifier = cp_parser_identifier (parser);
22716       /* If we failed, stop.  */
22717       if (identifier == error_mark_node)
22718         break;
22719       /* Declare it as a label.  */
22720       finish_label_decl (identifier);
22721       /* If the next token is a `;', stop.  */
22722       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22723         break;
22724       /* Look for the `,' separating the label declarations.  */
22725       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22726     }
22727
22728   /* Look for the final `;'.  */
22729   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22730 }
22731
22732 /* Support Functions */
22733
22734 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22735    NAME should have one of the representations used for an
22736    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22737    is returned.  If PARSER->SCOPE is a dependent type, then a
22738    SCOPE_REF is returned.
22739
22740    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22741    returned; the name was already resolved when the TEMPLATE_ID_EXPR
22742    was formed.  Abstractly, such entities should not be passed to this
22743    function, because they do not need to be looked up, but it is
22744    simpler to check for this special case here, rather than at the
22745    call-sites.
22746
22747    In cases not explicitly covered above, this function returns a
22748    DECL, OVERLOAD, or baselink representing the result of the lookup.
22749    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22750    is returned.
22751
22752    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22753    (e.g., "struct") that was used.  In that case bindings that do not
22754    refer to types are ignored.
22755
22756    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22757    ignored.
22758
22759    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22760    are ignored.
22761
22762    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22763    types.
22764
22765    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22766    TREE_LIST of candidates if name-lookup results in an ambiguity, and
22767    NULL_TREE otherwise.  */
22768
22769 static tree
22770 cp_parser_lookup_name (cp_parser *parser, tree name,
22771                        enum tag_types tag_type,
22772                        bool is_template,
22773                        bool is_namespace,
22774                        bool check_dependency,
22775                        tree *ambiguous_decls,
22776                        location_t name_location)
22777 {
22778   tree decl;
22779   tree object_type = parser->context->object_type;
22780
22781   /* Assume that the lookup will be unambiguous.  */
22782   if (ambiguous_decls)
22783     *ambiguous_decls = NULL_TREE;
22784
22785   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22786      no longer valid.  Note that if we are parsing tentatively, and
22787      the parse fails, OBJECT_TYPE will be automatically restored.  */
22788   parser->context->object_type = NULL_TREE;
22789
22790   if (name == error_mark_node)
22791     return error_mark_node;
22792
22793   /* A template-id has already been resolved; there is no lookup to
22794      do.  */
22795   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22796     return name;
22797   if (BASELINK_P (name))
22798     {
22799       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22800                   == TEMPLATE_ID_EXPR);
22801       return name;
22802     }
22803
22804   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
22805      it should already have been checked to make sure that the name
22806      used matches the type being destroyed.  */
22807   if (TREE_CODE (name) == BIT_NOT_EXPR)
22808     {
22809       tree type;
22810
22811       /* Figure out to which type this destructor applies.  */
22812       if (parser->scope)
22813         type = parser->scope;
22814       else if (object_type)
22815         type = object_type;
22816       else
22817         type = current_class_type;
22818       /* If that's not a class type, there is no destructor.  */
22819       if (!type || !CLASS_TYPE_P (type))
22820         return error_mark_node;
22821       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22822         lazily_declare_fn (sfk_destructor, type);
22823       if (!CLASSTYPE_DESTRUCTORS (type))
22824           return error_mark_node;
22825       /* If it was a class type, return the destructor.  */
22826       return CLASSTYPE_DESTRUCTORS (type);
22827     }
22828
22829   /* By this point, the NAME should be an ordinary identifier.  If
22830      the id-expression was a qualified name, the qualifying scope is
22831      stored in PARSER->SCOPE at this point.  */
22832   gcc_assert (identifier_p (name));
22833
22834   /* Perform the lookup.  */
22835   if (parser->scope)
22836     {
22837       bool dependent_p;
22838
22839       if (parser->scope == error_mark_node)
22840         return error_mark_node;
22841
22842       /* If the SCOPE is dependent, the lookup must be deferred until
22843          the template is instantiated -- unless we are explicitly
22844          looking up names in uninstantiated templates.  Even then, we
22845          cannot look up the name if the scope is not a class type; it
22846          might, for example, be a template type parameter.  */
22847       dependent_p = (TYPE_P (parser->scope)
22848                      && dependent_scope_p (parser->scope));
22849       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22850           && dependent_p)
22851         /* Defer lookup.  */
22852         decl = error_mark_node;
22853       else
22854         {
22855           tree pushed_scope = NULL_TREE;
22856
22857           /* If PARSER->SCOPE is a dependent type, then it must be a
22858              class type, and we must not be checking dependencies;
22859              otherwise, we would have processed this lookup above.  So
22860              that PARSER->SCOPE is not considered a dependent base by
22861              lookup_member, we must enter the scope here.  */
22862           if (dependent_p)
22863             pushed_scope = push_scope (parser->scope);
22864
22865           /* If the PARSER->SCOPE is a template specialization, it
22866              may be instantiated during name lookup.  In that case,
22867              errors may be issued.  Even if we rollback the current
22868              tentative parse, those errors are valid.  */
22869           decl = lookup_qualified_name (parser->scope, name,
22870                                         tag_type != none_type,
22871                                         /*complain=*/true);
22872
22873           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22874              lookup result and the nested-name-specifier nominates a class C:
22875                * if the name specified after the nested-name-specifier, when
22876                looked up in C, is the injected-class-name of C (Clause 9), or
22877                * if the name specified after the nested-name-specifier is the
22878                same as the identifier or the simple-template-id's template-
22879                name in the last component of the nested-name-specifier,
22880              the name is instead considered to name the constructor of
22881              class C. [ Note: for example, the constructor is not an
22882              acceptable lookup result in an elaborated-type-specifier so
22883              the constructor would not be used in place of the
22884              injected-class-name. --end note ] Such a constructor name
22885              shall be used only in the declarator-id of a declaration that
22886              names a constructor or in a using-declaration.  */
22887           if (tag_type == none_type
22888               && DECL_SELF_REFERENCE_P (decl)
22889               && same_type_p (DECL_CONTEXT (decl), parser->scope))
22890             decl = lookup_qualified_name (parser->scope, ctor_identifier,
22891                                           tag_type != none_type,
22892                                           /*complain=*/true);
22893
22894           /* If we have a single function from a using decl, pull it out.  */
22895           if (TREE_CODE (decl) == OVERLOAD
22896               && !really_overloaded_fn (decl))
22897             decl = OVL_FUNCTION (decl);
22898
22899           if (pushed_scope)
22900             pop_scope (pushed_scope);
22901         }
22902
22903       /* If the scope is a dependent type and either we deferred lookup or
22904          we did lookup but didn't find the name, rememeber the name.  */
22905       if (decl == error_mark_node && TYPE_P (parser->scope)
22906           && dependent_type_p (parser->scope))
22907         {
22908           if (tag_type)
22909             {
22910               tree type;
22911
22912               /* The resolution to Core Issue 180 says that `struct
22913                  A::B' should be considered a type-name, even if `A'
22914                  is dependent.  */
22915               type = make_typename_type (parser->scope, name, tag_type,
22916                                          /*complain=*/tf_error);
22917               if (type != error_mark_node)
22918                 decl = TYPE_NAME (type);
22919             }
22920           else if (is_template
22921                    && (cp_parser_next_token_ends_template_argument_p (parser)
22922                        || cp_lexer_next_token_is (parser->lexer,
22923                                                   CPP_CLOSE_PAREN)))
22924             decl = make_unbound_class_template (parser->scope,
22925                                                 name, NULL_TREE,
22926                                                 /*complain=*/tf_error);
22927           else
22928             decl = build_qualified_name (/*type=*/NULL_TREE,
22929                                          parser->scope, name,
22930                                          is_template);
22931         }
22932       parser->qualifying_scope = parser->scope;
22933       parser->object_scope = NULL_TREE;
22934     }
22935   else if (object_type)
22936     {
22937       /* Look up the name in the scope of the OBJECT_TYPE, unless the
22938          OBJECT_TYPE is not a class.  */
22939       if (CLASS_TYPE_P (object_type))
22940         /* If the OBJECT_TYPE is a template specialization, it may
22941            be instantiated during name lookup.  In that case, errors
22942            may be issued.  Even if we rollback the current tentative
22943            parse, those errors are valid.  */
22944         decl = lookup_member (object_type,
22945                               name,
22946                               /*protect=*/0,
22947                               tag_type != none_type,
22948                               tf_warning_or_error);
22949       else
22950         decl = NULL_TREE;
22951
22952       if (!decl)
22953         /* Look it up in the enclosing context.  */
22954         decl = lookup_name_real (name, tag_type != none_type,
22955                                  /*nonclass=*/0,
22956                                  /*block_p=*/true, is_namespace, 0);
22957       parser->object_scope = object_type;
22958       parser->qualifying_scope = NULL_TREE;
22959     }
22960   else
22961     {
22962       decl = lookup_name_real (name, tag_type != none_type,
22963                                /*nonclass=*/0,
22964                                /*block_p=*/true, is_namespace, 0);
22965       parser->qualifying_scope = NULL_TREE;
22966       parser->object_scope = NULL_TREE;
22967     }
22968
22969   /* If the lookup failed, let our caller know.  */
22970   if (!decl || decl == error_mark_node)
22971     return error_mark_node;
22972
22973   /* Pull out the template from an injected-class-name (or multiple).  */
22974   if (is_template)
22975     decl = maybe_get_template_decl_from_type_decl (decl);
22976
22977   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
22978   if (TREE_CODE (decl) == TREE_LIST)
22979     {
22980       if (ambiguous_decls)
22981         *ambiguous_decls = decl;
22982       /* The error message we have to print is too complicated for
22983          cp_parser_error, so we incorporate its actions directly.  */
22984       if (!cp_parser_simulate_error (parser))
22985         {
22986           error_at (name_location, "reference to %qD is ambiguous",
22987                     name);
22988           print_candidates (decl);
22989         }
22990       return error_mark_node;
22991     }
22992
22993   gcc_assert (DECL_P (decl)
22994               || TREE_CODE (decl) == OVERLOAD
22995               || TREE_CODE (decl) == SCOPE_REF
22996               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22997               || BASELINK_P (decl));
22998
22999   /* If we have resolved the name of a member declaration, check to
23000      see if the declaration is accessible.  When the name resolves to
23001      set of overloaded functions, accessibility is checked when
23002      overload resolution is done.
23003
23004      During an explicit instantiation, access is not checked at all,
23005      as per [temp.explicit].  */
23006   if (DECL_P (decl))
23007     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
23008
23009   maybe_record_typedef_use (decl);
23010
23011   return decl;
23012 }
23013
23014 /* Like cp_parser_lookup_name, but for use in the typical case where
23015    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
23016    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
23017
23018 static tree
23019 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
23020 {
23021   return cp_parser_lookup_name (parser, name,
23022                                 none_type,
23023                                 /*is_template=*/false,
23024                                 /*is_namespace=*/false,
23025                                 /*check_dependency=*/true,
23026                                 /*ambiguous_decls=*/NULL,
23027                                 location);
23028 }
23029
23030 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
23031    the current context, return the TYPE_DECL.  If TAG_NAME_P is
23032    true, the DECL indicates the class being defined in a class-head,
23033    or declared in an elaborated-type-specifier.
23034
23035    Otherwise, return DECL.  */
23036
23037 static tree
23038 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
23039 {
23040   /* If the TEMPLATE_DECL is being declared as part of a class-head,
23041      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
23042
23043        struct A {
23044          template <typename T> struct B;
23045        };
23046
23047        template <typename T> struct A::B {};
23048
23049      Similarly, in an elaborated-type-specifier:
23050
23051        namespace N { struct X{}; }
23052
23053        struct A {
23054          template <typename T> friend struct N::X;
23055        };
23056
23057      However, if the DECL refers to a class type, and we are in
23058      the scope of the class, then the name lookup automatically
23059      finds the TYPE_DECL created by build_self_reference rather
23060      than a TEMPLATE_DECL.  For example, in:
23061
23062        template <class T> struct S {
23063          S s;
23064        };
23065
23066      there is no need to handle such case.  */
23067
23068   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
23069     return DECL_TEMPLATE_RESULT (decl);
23070
23071   return decl;
23072 }
23073
23074 /* If too many, or too few, template-parameter lists apply to the
23075    declarator, issue an error message.  Returns TRUE if all went well,
23076    and FALSE otherwise.  */
23077
23078 static bool
23079 cp_parser_check_declarator_template_parameters (cp_parser* parser,
23080                                                 cp_declarator *declarator,
23081                                                 location_t declarator_location)
23082 {
23083   switch (declarator->kind)
23084     {
23085     case cdk_id:
23086       {
23087         unsigned num_templates = 0;
23088         tree scope = declarator->u.id.qualifying_scope;
23089
23090         if (scope)
23091           num_templates = num_template_headers_for_class (scope);
23092         else if (TREE_CODE (declarator->u.id.unqualified_name)
23093                  == TEMPLATE_ID_EXPR)
23094           /* If the DECLARATOR has the form `X<y>' then it uses one
23095              additional level of template parameters.  */
23096           ++num_templates;
23097
23098         return cp_parser_check_template_parameters 
23099           (parser, num_templates, declarator_location, declarator);
23100       }
23101
23102     case cdk_function:
23103     case cdk_array:
23104     case cdk_pointer:
23105     case cdk_reference:
23106     case cdk_ptrmem:
23107       return (cp_parser_check_declarator_template_parameters
23108               (parser, declarator->declarator, declarator_location));
23109
23110     case cdk_error:
23111       return true;
23112
23113     default:
23114       gcc_unreachable ();
23115     }
23116   return false;
23117 }
23118
23119 /* NUM_TEMPLATES were used in the current declaration.  If that is
23120    invalid, return FALSE and issue an error messages.  Otherwise,
23121    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
23122    declarator and we can print more accurate diagnostics.  */
23123
23124 static bool
23125 cp_parser_check_template_parameters (cp_parser* parser,
23126                                      unsigned num_templates,
23127                                      location_t location,
23128                                      cp_declarator *declarator)
23129 {
23130   /* If there are the same number of template classes and parameter
23131      lists, that's OK.  */
23132   if (parser->num_template_parameter_lists == num_templates)
23133     return true;
23134   /* If there are more, but only one more, then we are referring to a
23135      member template.  That's OK too.  */
23136   if (parser->num_template_parameter_lists == num_templates + 1)
23137     return true;
23138   /* If there are more template classes than parameter lists, we have
23139      something like:
23140
23141        template <class T> void S<T>::R<T>::f ();  */
23142   if (parser->num_template_parameter_lists < num_templates)
23143     {
23144       if (declarator && !current_function_decl)
23145         error_at (location, "specializing member %<%T::%E%> "
23146                   "requires %<template<>%> syntax", 
23147                   declarator->u.id.qualifying_scope,
23148                   declarator->u.id.unqualified_name);
23149       else if (declarator)
23150         error_at (location, "invalid declaration of %<%T::%E%>",
23151                   declarator->u.id.qualifying_scope,
23152                   declarator->u.id.unqualified_name);
23153       else 
23154         error_at (location, "too few template-parameter-lists");
23155       return false;
23156     }
23157   /* Otherwise, there are too many template parameter lists.  We have
23158      something like:
23159
23160      template <class T> template <class U> void S::f();  */
23161   error_at (location, "too many template-parameter-lists");
23162   return false;
23163 }
23164
23165 /* Parse an optional `::' token indicating that the following name is
23166    from the global namespace.  If so, PARSER->SCOPE is set to the
23167    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
23168    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
23169    Returns the new value of PARSER->SCOPE, if the `::' token is
23170    present, and NULL_TREE otherwise.  */
23171
23172 static tree
23173 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
23174 {
23175   cp_token *token;
23176
23177   /* Peek at the next token.  */
23178   token = cp_lexer_peek_token (parser->lexer);
23179   /* If we're looking at a `::' token then we're starting from the
23180      global namespace, not our current location.  */
23181   if (token->type == CPP_SCOPE)
23182     {
23183       /* Consume the `::' token.  */
23184       cp_lexer_consume_token (parser->lexer);
23185       /* Set the SCOPE so that we know where to start the lookup.  */
23186       parser->scope = global_namespace;
23187       parser->qualifying_scope = global_namespace;
23188       parser->object_scope = NULL_TREE;
23189
23190       return parser->scope;
23191     }
23192   else if (!current_scope_valid_p)
23193     {
23194       parser->scope = NULL_TREE;
23195       parser->qualifying_scope = NULL_TREE;
23196       parser->object_scope = NULL_TREE;
23197     }
23198
23199   return NULL_TREE;
23200 }
23201
23202 /* Returns TRUE if the upcoming token sequence is the start of a
23203    constructor declarator.  If FRIEND_P is true, the declarator is
23204    preceded by the `friend' specifier.  */
23205
23206 static bool
23207 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
23208 {
23209   bool constructor_p;
23210   bool outside_class_specifier_p;
23211   tree nested_name_specifier;
23212   cp_token *next_token;
23213
23214   /* The common case is that this is not a constructor declarator, so
23215      try to avoid doing lots of work if at all possible.  It's not
23216      valid declare a constructor at function scope.  */
23217   if (parser->in_function_body)
23218     return false;
23219   /* And only certain tokens can begin a constructor declarator.  */
23220   next_token = cp_lexer_peek_token (parser->lexer);
23221   if (next_token->type != CPP_NAME
23222       && next_token->type != CPP_SCOPE
23223       && next_token->type != CPP_NESTED_NAME_SPECIFIER
23224       && next_token->type != CPP_TEMPLATE_ID)
23225     return false;
23226
23227   /* Parse tentatively; we are going to roll back all of the tokens
23228      consumed here.  */
23229   cp_parser_parse_tentatively (parser);
23230   /* Assume that we are looking at a constructor declarator.  */
23231   constructor_p = true;
23232
23233   /* Look for the optional `::' operator.  */
23234   cp_parser_global_scope_opt (parser,
23235                               /*current_scope_valid_p=*/false);
23236   /* Look for the nested-name-specifier.  */
23237   nested_name_specifier
23238     = (cp_parser_nested_name_specifier_opt (parser,
23239                                             /*typename_keyword_p=*/false,
23240                                             /*check_dependency_p=*/false,
23241                                             /*type_p=*/false,
23242                                             /*is_declaration=*/false));
23243
23244   outside_class_specifier_p = (!at_class_scope_p ()
23245                                || !TYPE_BEING_DEFINED (current_class_type)
23246                                || friend_p);
23247
23248   /* Outside of a class-specifier, there must be a
23249      nested-name-specifier.  */
23250   if (!nested_name_specifier && outside_class_specifier_p)
23251     constructor_p = false;
23252   else if (nested_name_specifier == error_mark_node)
23253     constructor_p = false;
23254
23255   /* If we have a class scope, this is easy; DR 147 says that S::S always
23256      names the constructor, and no other qualified name could.  */
23257   if (constructor_p && nested_name_specifier
23258       && CLASS_TYPE_P (nested_name_specifier))
23259     {
23260       tree id = cp_parser_unqualified_id (parser,
23261                                           /*template_keyword_p=*/false,
23262                                           /*check_dependency_p=*/false,
23263                                           /*declarator_p=*/true,
23264                                           /*optional_p=*/false);
23265       if (is_overloaded_fn (id))
23266         id = DECL_NAME (get_first_fn (id));
23267       if (!constructor_name_p (id, nested_name_specifier))
23268         constructor_p = false;
23269     }
23270   /* If we still think that this might be a constructor-declarator,
23271      look for a class-name.  */
23272   else if (constructor_p)
23273     {
23274       /* If we have:
23275
23276            template <typename T> struct S {
23277              S();
23278            };
23279
23280          we must recognize that the nested `S' names a class.  */
23281       tree type_decl;
23282       type_decl = cp_parser_class_name (parser,
23283                                         /*typename_keyword_p=*/false,
23284                                         /*template_keyword_p=*/false,
23285                                         none_type,
23286                                         /*check_dependency_p=*/false,
23287                                         /*class_head_p=*/false,
23288                                         /*is_declaration=*/false);
23289       /* If there was no class-name, then this is not a constructor.
23290          Otherwise, if we are in a class-specifier and we aren't
23291          handling a friend declaration, check that its type matches
23292          current_class_type (c++/38313).  Note: error_mark_node
23293          is left alone for error recovery purposes.  */
23294       constructor_p = (!cp_parser_error_occurred (parser)
23295                        && (outside_class_specifier_p
23296                            || type_decl == error_mark_node
23297                            || same_type_p (current_class_type,
23298                                            TREE_TYPE (type_decl))));
23299
23300       /* If we're still considering a constructor, we have to see a `(',
23301          to begin the parameter-declaration-clause, followed by either a
23302          `)', an `...', or a decl-specifier.  We need to check for a
23303          type-specifier to avoid being fooled into thinking that:
23304
23305            S (f) (int);
23306
23307          is a constructor.  (It is actually a function named `f' that
23308          takes one parameter (of type `int') and returns a value of type
23309          `S'.  */
23310       if (constructor_p
23311           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23312         constructor_p = false;
23313
23314       if (constructor_p
23315           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
23316           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
23317           /* A parameter declaration begins with a decl-specifier,
23318              which is either the "attribute" keyword, a storage class
23319              specifier, or (usually) a type-specifier.  */
23320           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
23321         {
23322           tree type;
23323           tree pushed_scope = NULL_TREE;
23324           unsigned saved_num_template_parameter_lists;
23325
23326           /* Names appearing in the type-specifier should be looked up
23327              in the scope of the class.  */
23328           if (current_class_type)
23329             type = NULL_TREE;
23330           else
23331             {
23332               type = TREE_TYPE (type_decl);
23333               if (TREE_CODE (type) == TYPENAME_TYPE)
23334                 {
23335                   type = resolve_typename_type (type,
23336                                                 /*only_current_p=*/false);
23337                   if (TREE_CODE (type) == TYPENAME_TYPE)
23338                     {
23339                       cp_parser_abort_tentative_parse (parser);
23340                       return false;
23341                     }
23342                 }
23343               pushed_scope = push_scope (type);
23344             }
23345
23346           /* Inside the constructor parameter list, surrounding
23347              template-parameter-lists do not apply.  */
23348           saved_num_template_parameter_lists
23349             = parser->num_template_parameter_lists;
23350           parser->num_template_parameter_lists = 0;
23351
23352           /* Look for the type-specifier.  */
23353           cp_parser_type_specifier (parser,
23354                                     CP_PARSER_FLAGS_NONE,
23355                                     /*decl_specs=*/NULL,
23356                                     /*is_declarator=*/true,
23357                                     /*declares_class_or_enum=*/NULL,
23358                                     /*is_cv_qualifier=*/NULL);
23359
23360           parser->num_template_parameter_lists
23361             = saved_num_template_parameter_lists;
23362
23363           /* Leave the scope of the class.  */
23364           if (pushed_scope)
23365             pop_scope (pushed_scope);
23366
23367           constructor_p = !cp_parser_error_occurred (parser);
23368         }
23369     }
23370
23371   /* We did not really want to consume any tokens.  */
23372   cp_parser_abort_tentative_parse (parser);
23373
23374   return constructor_p;
23375 }
23376
23377 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23378    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
23379    they must be performed once we are in the scope of the function.
23380
23381    Returns the function defined.  */
23382
23383 static tree
23384 cp_parser_function_definition_from_specifiers_and_declarator
23385   (cp_parser* parser,
23386    cp_decl_specifier_seq *decl_specifiers,
23387    tree attributes,
23388    const cp_declarator *declarator)
23389 {
23390   tree fn;
23391   bool success_p;
23392
23393   /* Begin the function-definition.  */
23394   success_p = start_function (decl_specifiers, declarator, attributes);
23395
23396   /* The things we're about to see are not directly qualified by any
23397      template headers we've seen thus far.  */
23398   reset_specialization ();
23399
23400   /* If there were names looked up in the decl-specifier-seq that we
23401      did not check, check them now.  We must wait until we are in the
23402      scope of the function to perform the checks, since the function
23403      might be a friend.  */
23404   perform_deferred_access_checks (tf_warning_or_error);
23405
23406   if (success_p)
23407     {
23408       cp_finalize_omp_declare_simd (parser, current_function_decl);
23409       parser->omp_declare_simd = NULL;
23410     }
23411
23412   if (!success_p)
23413     {
23414       /* Skip the entire function.  */
23415       cp_parser_skip_to_end_of_block_or_statement (parser);
23416       fn = error_mark_node;
23417     }
23418   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23419     {
23420       /* Seen already, skip it.  An error message has already been output.  */
23421       cp_parser_skip_to_end_of_block_or_statement (parser);
23422       fn = current_function_decl;
23423       current_function_decl = NULL_TREE;
23424       /* If this is a function from a class, pop the nested class.  */
23425       if (current_class_name)
23426         pop_nested_class ();
23427     }
23428   else
23429     {
23430       timevar_id_t tv;
23431       if (DECL_DECLARED_INLINE_P (current_function_decl))
23432         tv = TV_PARSE_INLINE;
23433       else
23434         tv = TV_PARSE_FUNC;
23435       timevar_push (tv);
23436       fn = cp_parser_function_definition_after_declarator (parser,
23437                                                          /*inline_p=*/false);
23438       timevar_pop (tv);
23439     }
23440
23441   return fn;
23442 }
23443
23444 /* Parse the part of a function-definition that follows the
23445    declarator.  INLINE_P is TRUE iff this function is an inline
23446    function defined within a class-specifier.
23447
23448    Returns the function defined.  */
23449
23450 static tree
23451 cp_parser_function_definition_after_declarator (cp_parser* parser,
23452                                                 bool inline_p)
23453 {
23454   tree fn;
23455   bool ctor_initializer_p = false;
23456   bool saved_in_unbraced_linkage_specification_p;
23457   bool saved_in_function_body;
23458   unsigned saved_num_template_parameter_lists;
23459   cp_token *token;
23460   bool fully_implicit_function_template_p
23461     = parser->fully_implicit_function_template_p;
23462   parser->fully_implicit_function_template_p = false;
23463   tree implicit_template_parms
23464     = parser->implicit_template_parms;
23465   parser->implicit_template_parms = 0;
23466   cp_binding_level* implicit_template_scope
23467     = parser->implicit_template_scope;
23468   parser->implicit_template_scope = 0;
23469
23470   saved_in_function_body = parser->in_function_body;
23471   parser->in_function_body = true;
23472   /* If the next token is `return', then the code may be trying to
23473      make use of the "named return value" extension that G++ used to
23474      support.  */
23475   token = cp_lexer_peek_token (parser->lexer);
23476   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23477     {
23478       /* Consume the `return' keyword.  */
23479       cp_lexer_consume_token (parser->lexer);
23480       /* Look for the identifier that indicates what value is to be
23481          returned.  */
23482       cp_parser_identifier (parser);
23483       /* Issue an error message.  */
23484       error_at (token->location,
23485                 "named return values are no longer supported");
23486       /* Skip tokens until we reach the start of the function body.  */
23487       while (true)
23488         {
23489           cp_token *token = cp_lexer_peek_token (parser->lexer);
23490           if (token->type == CPP_OPEN_BRACE
23491               || token->type == CPP_EOF
23492               || token->type == CPP_PRAGMA_EOL)
23493             break;
23494           cp_lexer_consume_token (parser->lexer);
23495         }
23496     }
23497   /* The `extern' in `extern "C" void f () { ... }' does not apply to
23498      anything declared inside `f'.  */
23499   saved_in_unbraced_linkage_specification_p
23500     = parser->in_unbraced_linkage_specification_p;
23501   parser->in_unbraced_linkage_specification_p = false;
23502   /* Inside the function, surrounding template-parameter-lists do not
23503      apply.  */
23504   saved_num_template_parameter_lists
23505     = parser->num_template_parameter_lists;
23506   parser->num_template_parameter_lists = 0;
23507
23508   start_lambda_scope (current_function_decl);
23509
23510   /* If the next token is `try', `__transaction_atomic', or
23511      `__transaction_relaxed`, then we are looking at either function-try-block
23512      or function-transaction-block.  Note that all of these include the
23513      function-body.  */
23514   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23515     ctor_initializer_p = cp_parser_function_transaction (parser,
23516         RID_TRANSACTION_ATOMIC);
23517   else if (cp_lexer_next_token_is_keyword (parser->lexer,
23518       RID_TRANSACTION_RELAXED))
23519     ctor_initializer_p = cp_parser_function_transaction (parser,
23520         RID_TRANSACTION_RELAXED);
23521   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23522     ctor_initializer_p = cp_parser_function_try_block (parser);
23523   else
23524     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23525       (parser, /*in_function_try_block=*/false);
23526
23527   finish_lambda_scope ();
23528
23529   /* Finish the function.  */
23530   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23531                         (inline_p ? 2 : 0));
23532   /* Generate code for it, if necessary.  */
23533   expand_or_defer_fn (fn);
23534   /* Restore the saved values.  */
23535   parser->in_unbraced_linkage_specification_p
23536     = saved_in_unbraced_linkage_specification_p;
23537   parser->num_template_parameter_lists
23538     = saved_num_template_parameter_lists;
23539   parser->in_function_body = saved_in_function_body;
23540
23541   parser->fully_implicit_function_template_p
23542     = fully_implicit_function_template_p;
23543   parser->implicit_template_parms
23544     = implicit_template_parms;
23545   parser->implicit_template_scope
23546     = implicit_template_scope;
23547
23548   if (parser->fully_implicit_function_template_p)
23549     finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23550
23551   return fn;
23552 }
23553
23554 /* Parse a template-declaration, assuming that the `export' (and
23555    `extern') keywords, if present, has already been scanned.  MEMBER_P
23556    is as for cp_parser_template_declaration.  */
23557
23558 static void
23559 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23560 {
23561   tree decl = NULL_TREE;
23562   vec<deferred_access_check, va_gc> *checks;
23563   tree parameter_list;
23564   bool friend_p = false;
23565   bool need_lang_pop;
23566   cp_token *token;
23567
23568   /* Look for the `template' keyword.  */
23569   token = cp_lexer_peek_token (parser->lexer);
23570   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23571     return;
23572
23573   /* And the `<'.  */
23574   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23575     return;
23576   if (at_class_scope_p () && current_function_decl)
23577     {
23578       /* 14.5.2.2 [temp.mem]
23579
23580          A local class shall not have member templates.  */
23581       error_at (token->location,
23582                 "invalid declaration of member template in local class");
23583       cp_parser_skip_to_end_of_block_or_statement (parser);
23584       return;
23585     }
23586   /* [temp]
23587
23588      A template ... shall not have C linkage.  */
23589   if (current_lang_name == lang_name_c)
23590     {
23591       error_at (token->location, "template with C linkage");
23592       /* Give it C++ linkage to avoid confusing other parts of the
23593          front end.  */
23594       push_lang_context (lang_name_cplusplus);
23595       need_lang_pop = true;
23596     }
23597   else
23598     need_lang_pop = false;
23599
23600   /* We cannot perform access checks on the template parameter
23601      declarations until we know what is being declared, just as we
23602      cannot check the decl-specifier list.  */
23603   push_deferring_access_checks (dk_deferred);
23604
23605   /* If the next token is `>', then we have an invalid
23606      specialization.  Rather than complain about an invalid template
23607      parameter, issue an error message here.  */
23608   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23609     {
23610       cp_parser_error (parser, "invalid explicit specialization");
23611       begin_specialization ();
23612       parameter_list = NULL_TREE;
23613     }
23614   else
23615     {
23616       /* Parse the template parameters.  */
23617       parameter_list = cp_parser_template_parameter_list (parser);
23618     }
23619
23620   /* Get the deferred access checks from the parameter list.  These
23621      will be checked once we know what is being declared, as for a
23622      member template the checks must be performed in the scope of the
23623      class containing the member.  */
23624   checks = get_deferred_access_checks ();
23625
23626   /* Look for the `>'.  */
23627   cp_parser_skip_to_end_of_template_parameter_list (parser);
23628   /* We just processed one more parameter list.  */
23629   ++parser->num_template_parameter_lists;
23630   /* If the next token is `template', there are more template
23631      parameters.  */
23632   if (cp_lexer_next_token_is_keyword (parser->lexer,
23633                                       RID_TEMPLATE))
23634     cp_parser_template_declaration_after_export (parser, member_p);
23635   else if (cxx_dialect >= cxx11
23636            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23637     decl = cp_parser_alias_declaration (parser);
23638   else
23639     {
23640       /* There are no access checks when parsing a template, as we do not
23641          know if a specialization will be a friend.  */
23642       push_deferring_access_checks (dk_no_check);
23643       token = cp_lexer_peek_token (parser->lexer);
23644       decl = cp_parser_single_declaration (parser,
23645                                            checks,
23646                                            member_p,
23647                                            /*explicit_specialization_p=*/false,
23648                                            &friend_p);
23649       pop_deferring_access_checks ();
23650
23651       /* If this is a member template declaration, let the front
23652          end know.  */
23653       if (member_p && !friend_p && decl)
23654         {
23655           if (TREE_CODE (decl) == TYPE_DECL)
23656             cp_parser_check_access_in_redeclaration (decl, token->location);
23657
23658           decl = finish_member_template_decl (decl);
23659         }
23660       else if (friend_p && decl
23661                && DECL_DECLARES_TYPE_P (decl))
23662         make_friend_class (current_class_type, TREE_TYPE (decl),
23663                            /*complain=*/true);
23664     }
23665   /* We are done with the current parameter list.  */
23666   --parser->num_template_parameter_lists;
23667
23668   pop_deferring_access_checks ();
23669
23670   /* Finish up.  */
23671   finish_template_decl (parameter_list);
23672
23673   /* Check the template arguments for a literal operator template.  */
23674   if (decl
23675       && DECL_DECLARES_FUNCTION_P (decl)
23676       && UDLIT_OPER_P (DECL_NAME (decl)))
23677     {
23678       bool ok = true;
23679       if (parameter_list == NULL_TREE)
23680         ok = false;
23681       else
23682         {
23683           int num_parms = TREE_VEC_LENGTH (parameter_list);
23684           if (num_parms == 1)
23685             {
23686               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23687               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23688               if (TREE_TYPE (parm) != char_type_node
23689                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23690                 ok = false;
23691             }
23692           else if (num_parms == 2 && cxx_dialect >= cxx14)
23693             {
23694               tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23695               tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23696               tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23697               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23698               if (TREE_TYPE (parm) != TREE_TYPE (type)
23699                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23700                 ok = false;
23701             }
23702           else
23703             ok = false;
23704         }
23705       if (!ok)
23706         {
23707           if (cxx_dialect >= cxx14)
23708             error ("literal operator template %qD has invalid parameter list."
23709                    "  Expected non-type template argument pack <char...>"
23710                    " or <typename CharT, CharT...>",
23711                    decl);
23712           else
23713             error ("literal operator template %qD has invalid parameter list."
23714                    "  Expected non-type template argument pack <char...>",
23715                    decl);
23716         }
23717     }
23718   /* Register member declarations.  */
23719   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23720     finish_member_declaration (decl);
23721   /* For the erroneous case of a template with C linkage, we pushed an
23722      implicit C++ linkage scope; exit that scope now.  */
23723   if (need_lang_pop)
23724     pop_lang_context ();
23725   /* If DECL is a function template, we must return to parse it later.
23726      (Even though there is no definition, there might be default
23727      arguments that need handling.)  */
23728   if (member_p && decl
23729       && DECL_DECLARES_FUNCTION_P (decl))
23730     vec_safe_push (unparsed_funs_with_definitions, decl);
23731 }
23732
23733 /* Perform the deferred access checks from a template-parameter-list.
23734    CHECKS is a TREE_LIST of access checks, as returned by
23735    get_deferred_access_checks.  */
23736
23737 static void
23738 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23739 {
23740   ++processing_template_parmlist;
23741   perform_access_checks (checks, tf_warning_or_error);
23742   --processing_template_parmlist;
23743 }
23744
23745 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23746    `function-definition' sequence that follows a template header.
23747    If MEMBER_P is true, this declaration appears in a class scope.
23748
23749    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
23750    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
23751
23752 static tree
23753 cp_parser_single_declaration (cp_parser* parser,
23754                               vec<deferred_access_check, va_gc> *checks,
23755                               bool member_p,
23756                               bool explicit_specialization_p,
23757                               bool* friend_p)
23758 {
23759   int declares_class_or_enum;
23760   tree decl = NULL_TREE;
23761   cp_decl_specifier_seq decl_specifiers;
23762   bool function_definition_p = false;
23763   cp_token *decl_spec_token_start;
23764
23765   /* This function is only used when processing a template
23766      declaration.  */
23767   gcc_assert (innermost_scope_kind () == sk_template_parms
23768               || innermost_scope_kind () == sk_template_spec);
23769
23770   /* Defer access checks until we know what is being declared.  */
23771   push_deferring_access_checks (dk_deferred);
23772
23773   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23774      alternative.  */
23775   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23776   cp_parser_decl_specifier_seq (parser,
23777                                 CP_PARSER_FLAGS_OPTIONAL,
23778                                 &decl_specifiers,
23779                                 &declares_class_or_enum);
23780   if (friend_p)
23781     *friend_p = cp_parser_friend_p (&decl_specifiers);
23782
23783   /* There are no template typedefs.  */
23784   if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23785     {
23786       error_at (decl_spec_token_start->location,
23787                 "template declaration of %<typedef%>");
23788       decl = error_mark_node;
23789     }
23790
23791   /* Gather up the access checks that occurred the
23792      decl-specifier-seq.  */
23793   stop_deferring_access_checks ();
23794
23795   /* Check for the declaration of a template class.  */
23796   if (declares_class_or_enum)
23797     {
23798       if (cp_parser_declares_only_class_p (parser))
23799         {
23800           decl = shadow_tag (&decl_specifiers);
23801
23802           /* In this case:
23803
23804                struct C {
23805                  friend template <typename T> struct A<T>::B;
23806                };
23807
23808              A<T>::B will be represented by a TYPENAME_TYPE, and
23809              therefore not recognized by shadow_tag.  */
23810           if (friend_p && *friend_p
23811               && !decl
23812               && decl_specifiers.type
23813               && TYPE_P (decl_specifiers.type))
23814             decl = decl_specifiers.type;
23815
23816           if (decl && decl != error_mark_node)
23817             decl = TYPE_NAME (decl);
23818           else
23819             decl = error_mark_node;
23820
23821           /* Perform access checks for template parameters.  */
23822           cp_parser_perform_template_parameter_access_checks (checks);
23823         }
23824     }
23825
23826   /* Complain about missing 'typename' or other invalid type names.  */
23827   if (!decl_specifiers.any_type_specifiers_p
23828       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23829     {
23830       /* cp_parser_parse_and_diagnose_invalid_type_name calls
23831          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23832          the rest of this declaration.  */
23833       decl = error_mark_node;
23834       goto out;
23835     }
23836
23837   /* If it's not a template class, try for a template function.  If
23838      the next token is a `;', then this declaration does not declare
23839      anything.  But, if there were errors in the decl-specifiers, then
23840      the error might well have come from an attempted class-specifier.
23841      In that case, there's no need to warn about a missing declarator.  */
23842   if (!decl
23843       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23844           || decl_specifiers.type != error_mark_node))
23845     {
23846       decl = cp_parser_init_declarator (parser,
23847                                         &decl_specifiers,
23848                                         checks,
23849                                         /*function_definition_allowed_p=*/true,
23850                                         member_p,
23851                                         declares_class_or_enum,
23852                                         &function_definition_p,
23853                                         NULL, NULL);
23854
23855     /* 7.1.1-1 [dcl.stc]
23856
23857        A storage-class-specifier shall not be specified in an explicit
23858        specialization...  */
23859     if (decl
23860         && explicit_specialization_p
23861         && decl_specifiers.storage_class != sc_none)
23862       {
23863         error_at (decl_spec_token_start->location,
23864                   "explicit template specialization cannot have a storage class");
23865         decl = error_mark_node;
23866       }
23867
23868     if (decl && VAR_P (decl))
23869       check_template_variable (decl);
23870     }
23871
23872   /* Look for a trailing `;' after the declaration.  */
23873   if (!function_definition_p
23874       && (decl == error_mark_node
23875           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23876     cp_parser_skip_to_end_of_block_or_statement (parser);
23877
23878  out:
23879   pop_deferring_access_checks ();
23880
23881   /* Clear any current qualification; whatever comes next is the start
23882      of something new.  */
23883   parser->scope = NULL_TREE;
23884   parser->qualifying_scope = NULL_TREE;
23885   parser->object_scope = NULL_TREE;
23886
23887   return decl;
23888 }
23889
23890 /* Parse a cast-expression that is not the operand of a unary "&".  */
23891
23892 static tree
23893 cp_parser_simple_cast_expression (cp_parser *parser)
23894 {
23895   return cp_parser_cast_expression (parser, /*address_p=*/false,
23896                                     /*cast_p=*/false, /*decltype*/false, NULL);
23897 }
23898
23899 /* Parse a functional cast to TYPE.  Returns an expression
23900    representing the cast.  */
23901
23902 static tree
23903 cp_parser_functional_cast (cp_parser* parser, tree type)
23904 {
23905   vec<tree, va_gc> *vec;
23906   tree expression_list;
23907   tree cast;
23908   bool nonconst_p;
23909
23910   if (!type)
23911     type = error_mark_node;
23912
23913   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23914     {
23915       cp_lexer_set_source_position (parser->lexer);
23916       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23917       expression_list = cp_parser_braced_list (parser, &nonconst_p);
23918       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23919       if (TREE_CODE (type) == TYPE_DECL)
23920         type = TREE_TYPE (type);
23921       return finish_compound_literal (type, expression_list,
23922                                       tf_warning_or_error);
23923     }
23924
23925
23926   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23927                                                  /*cast_p=*/true,
23928                                                  /*allow_expansion_p=*/true,
23929                                                  /*non_constant_p=*/NULL);
23930   if (vec == NULL)
23931     expression_list = error_mark_node;
23932   else
23933     {
23934       expression_list = build_tree_list_vec (vec);
23935       release_tree_vector (vec);
23936     }
23937
23938   cast = build_functional_cast (type, expression_list,
23939                                 tf_warning_or_error);
23940   /* [expr.const]/1: In an integral constant expression "only type
23941      conversions to integral or enumeration type can be used".  */
23942   if (TREE_CODE (type) == TYPE_DECL)
23943     type = TREE_TYPE (type);
23944   if (cast != error_mark_node
23945       && !cast_valid_in_integral_constant_expression_p (type)
23946       && cp_parser_non_integral_constant_expression (parser,
23947                                                      NIC_CONSTRUCTOR))
23948     return error_mark_node;
23949   return cast;
23950 }
23951
23952 /* Save the tokens that make up the body of a member function defined
23953    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
23954    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
23955    specifiers applied to the declaration.  Returns the FUNCTION_DECL
23956    for the member function.  */
23957
23958 static tree
23959 cp_parser_save_member_function_body (cp_parser* parser,
23960                                      cp_decl_specifier_seq *decl_specifiers,
23961                                      cp_declarator *declarator,
23962                                      tree attributes)
23963 {
23964   cp_token *first;
23965   cp_token *last;
23966   tree fn;
23967   bool function_try_block = false;
23968
23969   /* Create the FUNCTION_DECL.  */
23970   fn = grokmethod (decl_specifiers, declarator, attributes);
23971   cp_finalize_omp_declare_simd (parser, fn);
23972   /* If something went badly wrong, bail out now.  */
23973   if (fn == error_mark_node)
23974     {
23975       /* If there's a function-body, skip it.  */
23976       if (cp_parser_token_starts_function_definition_p
23977           (cp_lexer_peek_token (parser->lexer)))
23978         cp_parser_skip_to_end_of_block_or_statement (parser);
23979       return error_mark_node;
23980     }
23981
23982   /* Remember it, if there default args to post process.  */
23983   cp_parser_save_default_args (parser, fn);
23984
23985   /* Save away the tokens that make up the body of the
23986      function.  */
23987   first = parser->lexer->next_token;
23988
23989   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
23990     cp_lexer_consume_token (parser->lexer);
23991   else if (cp_lexer_next_token_is_keyword (parser->lexer,
23992                                            RID_TRANSACTION_ATOMIC))
23993     {
23994       cp_lexer_consume_token (parser->lexer);
23995       /* Match cp_parser_txn_attribute_opt [[ identifier ]].  */
23996       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
23997           && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
23998           && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
23999               || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
24000           && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
24001           && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
24002         {
24003           cp_lexer_consume_token (parser->lexer);
24004           cp_lexer_consume_token (parser->lexer);
24005           cp_lexer_consume_token (parser->lexer);
24006           cp_lexer_consume_token (parser->lexer);
24007           cp_lexer_consume_token (parser->lexer);
24008         }
24009       else
24010         while (cp_next_tokens_can_be_gnu_attribute_p (parser)
24011                && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
24012           {
24013             cp_lexer_consume_token (parser->lexer);
24014             if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24015               break;
24016           }
24017     }
24018
24019   /* Handle function try blocks.  */
24020   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
24021     {
24022       cp_lexer_consume_token (parser->lexer);
24023       function_try_block = true;
24024     }
24025   /* We can have braced-init-list mem-initializers before the fn body.  */
24026   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24027     {
24028       cp_lexer_consume_token (parser->lexer);
24029       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
24030         {
24031           /* cache_group will stop after an un-nested { } pair, too.  */
24032           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
24033             break;
24034
24035           /* variadic mem-inits have ... after the ')'.  */
24036           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24037             cp_lexer_consume_token (parser->lexer);
24038         }
24039     }
24040   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24041   /* Handle function try blocks.  */
24042   if (function_try_block)
24043     while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
24044       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24045   last = parser->lexer->next_token;
24046
24047   /* Save away the inline definition; we will process it when the
24048      class is complete.  */
24049   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
24050   DECL_PENDING_INLINE_P (fn) = 1;
24051
24052   /* We need to know that this was defined in the class, so that
24053      friend templates are handled correctly.  */
24054   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
24055
24056   /* Add FN to the queue of functions to be parsed later.  */
24057   vec_safe_push (unparsed_funs_with_definitions, fn);
24058
24059   return fn;
24060 }
24061
24062 /* Save the tokens that make up the in-class initializer for a non-static
24063    data member.  Returns a DEFAULT_ARG.  */
24064
24065 static tree
24066 cp_parser_save_nsdmi (cp_parser* parser)
24067 {
24068   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
24069 }
24070
24071 /* Parse a template-argument-list, as well as the trailing ">" (but
24072    not the opening "<").  See cp_parser_template_argument_list for the
24073    return value.  */
24074
24075 static tree
24076 cp_parser_enclosed_template_argument_list (cp_parser* parser)
24077 {
24078   tree arguments;
24079   tree saved_scope;
24080   tree saved_qualifying_scope;
24081   tree saved_object_scope;
24082   bool saved_greater_than_is_operator_p;
24083   int saved_unevaluated_operand;
24084   int saved_inhibit_evaluation_warnings;
24085
24086   /* [temp.names]
24087
24088      When parsing a template-id, the first non-nested `>' is taken as
24089      the end of the template-argument-list rather than a greater-than
24090      operator.  */
24091   saved_greater_than_is_operator_p
24092     = parser->greater_than_is_operator_p;
24093   parser->greater_than_is_operator_p = false;
24094   /* Parsing the argument list may modify SCOPE, so we save it
24095      here.  */
24096   saved_scope = parser->scope;
24097   saved_qualifying_scope = parser->qualifying_scope;
24098   saved_object_scope = parser->object_scope;
24099   /* We need to evaluate the template arguments, even though this
24100      template-id may be nested within a "sizeof".  */
24101   saved_unevaluated_operand = cp_unevaluated_operand;
24102   cp_unevaluated_operand = 0;
24103   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24104   c_inhibit_evaluation_warnings = 0;
24105   /* Parse the template-argument-list itself.  */
24106   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
24107       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24108     arguments = NULL_TREE;
24109   else
24110     arguments = cp_parser_template_argument_list (parser);
24111   /* Look for the `>' that ends the template-argument-list. If we find
24112      a '>>' instead, it's probably just a typo.  */
24113   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
24114     {
24115       if (cxx_dialect != cxx98)
24116         {
24117           /* In C++0x, a `>>' in a template argument list or cast
24118              expression is considered to be two separate `>'
24119              tokens. So, change the current token to a `>', but don't
24120              consume it: it will be consumed later when the outer
24121              template argument list (or cast expression) is parsed.
24122              Note that this replacement of `>' for `>>' is necessary
24123              even if we are parsing tentatively: in the tentative
24124              case, after calling
24125              cp_parser_enclosed_template_argument_list we will always
24126              throw away all of the template arguments and the first
24127              closing `>', either because the template argument list
24128              was erroneous or because we are replacing those tokens
24129              with a CPP_TEMPLATE_ID token.  The second `>' (which will
24130              not have been thrown away) is needed either to close an
24131              outer template argument list or to complete a new-style
24132              cast.  */
24133           cp_token *token = cp_lexer_peek_token (parser->lexer);
24134           token->type = CPP_GREATER;
24135         }
24136       else if (!saved_greater_than_is_operator_p)
24137         {
24138           /* If we're in a nested template argument list, the '>>' has
24139             to be a typo for '> >'. We emit the error message, but we
24140             continue parsing and we push a '>' as next token, so that
24141             the argument list will be parsed correctly.  Note that the
24142             global source location is still on the token before the
24143             '>>', so we need to say explicitly where we want it.  */
24144           cp_token *token = cp_lexer_peek_token (parser->lexer);
24145           error_at (token->location, "%<>>%> should be %<> >%> "
24146                     "within a nested template argument list");
24147
24148           token->type = CPP_GREATER;
24149         }
24150       else
24151         {
24152           /* If this is not a nested template argument list, the '>>'
24153             is a typo for '>'. Emit an error message and continue.
24154             Same deal about the token location, but here we can get it
24155             right by consuming the '>>' before issuing the diagnostic.  */
24156           cp_token *token = cp_lexer_consume_token (parser->lexer);
24157           error_at (token->location,
24158                     "spurious %<>>%>, use %<>%> to terminate "
24159                     "a template argument list");
24160         }
24161     }
24162   else
24163     cp_parser_skip_to_end_of_template_parameter_list (parser);
24164   /* The `>' token might be a greater-than operator again now.  */
24165   parser->greater_than_is_operator_p
24166     = saved_greater_than_is_operator_p;
24167   /* Restore the SAVED_SCOPE.  */
24168   parser->scope = saved_scope;
24169   parser->qualifying_scope = saved_qualifying_scope;
24170   parser->object_scope = saved_object_scope;
24171   cp_unevaluated_operand = saved_unevaluated_operand;
24172   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24173
24174   return arguments;
24175 }
24176
24177 /* MEMBER_FUNCTION is a member function, or a friend.  If default
24178    arguments, or the body of the function have not yet been parsed,
24179    parse them now.  */
24180
24181 static void
24182 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
24183 {
24184   timevar_push (TV_PARSE_INMETH);
24185   /* If this member is a template, get the underlying
24186      FUNCTION_DECL.  */
24187   if (DECL_FUNCTION_TEMPLATE_P (member_function))
24188     member_function = DECL_TEMPLATE_RESULT (member_function);
24189
24190   /* There should not be any class definitions in progress at this
24191      point; the bodies of members are only parsed outside of all class
24192      definitions.  */
24193   gcc_assert (parser->num_classes_being_defined == 0);
24194   /* While we're parsing the member functions we might encounter more
24195      classes.  We want to handle them right away, but we don't want
24196      them getting mixed up with functions that are currently in the
24197      queue.  */
24198   push_unparsed_function_queues (parser);
24199
24200   /* Make sure that any template parameters are in scope.  */
24201   maybe_begin_member_template_processing (member_function);
24202
24203   /* If the body of the function has not yet been parsed, parse it
24204      now.  */
24205   if (DECL_PENDING_INLINE_P (member_function))
24206     {
24207       tree function_scope;
24208       cp_token_cache *tokens;
24209
24210       /* The function is no longer pending; we are processing it.  */
24211       tokens = DECL_PENDING_INLINE_INFO (member_function);
24212       DECL_PENDING_INLINE_INFO (member_function) = NULL;
24213       DECL_PENDING_INLINE_P (member_function) = 0;
24214
24215       /* If this is a local class, enter the scope of the containing
24216          function.  */
24217       function_scope = current_function_decl;
24218       if (function_scope)
24219         push_function_context ();
24220
24221       /* Push the body of the function onto the lexer stack.  */
24222       cp_parser_push_lexer_for_tokens (parser, tokens);
24223
24224       /* Let the front end know that we going to be defining this
24225          function.  */
24226       start_preparsed_function (member_function, NULL_TREE,
24227                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
24228
24229       /* Don't do access checking if it is a templated function.  */
24230       if (processing_template_decl)
24231         push_deferring_access_checks (dk_no_check);
24232
24233       /* #pragma omp declare reduction needs special parsing.  */
24234       if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
24235         {
24236           parser->lexer->in_pragma = true;
24237           cp_parser_omp_declare_reduction_exprs (member_function, parser);
24238           finish_function (/*inline*/2);
24239           cp_check_omp_declare_reduction (member_function);
24240         }
24241       else
24242         /* Now, parse the body of the function.  */
24243         cp_parser_function_definition_after_declarator (parser,
24244                                                         /*inline_p=*/true);
24245
24246       if (processing_template_decl)
24247         pop_deferring_access_checks ();
24248
24249       /* Leave the scope of the containing function.  */
24250       if (function_scope)
24251         pop_function_context ();
24252       cp_parser_pop_lexer (parser);
24253     }
24254
24255   /* Remove any template parameters from the symbol table.  */
24256   maybe_end_member_template_processing ();
24257
24258   /* Restore the queue.  */
24259   pop_unparsed_function_queues (parser);
24260   timevar_pop (TV_PARSE_INMETH);
24261 }
24262
24263 /* If DECL contains any default args, remember it on the unparsed
24264    functions queue.  */
24265
24266 static void
24267 cp_parser_save_default_args (cp_parser* parser, tree decl)
24268 {
24269   tree probe;
24270
24271   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
24272        probe;
24273        probe = TREE_CHAIN (probe))
24274     if (TREE_PURPOSE (probe))
24275       {
24276         cp_default_arg_entry entry = {current_class_type, decl};
24277         vec_safe_push (unparsed_funs_with_default_args, entry);
24278         break;
24279       }
24280 }
24281
24282 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
24283    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
24284    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
24285    from the parameter-type-list.  */
24286
24287 static tree
24288 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
24289                                       tree default_arg, tree parmtype)
24290 {
24291   cp_token_cache *tokens;
24292   tree parsed_arg;
24293   bool dummy;
24294
24295   if (default_arg == error_mark_node)
24296     return error_mark_node;
24297
24298   /* Push the saved tokens for the default argument onto the parser's
24299      lexer stack.  */
24300   tokens = DEFARG_TOKENS (default_arg);
24301   cp_parser_push_lexer_for_tokens (parser, tokens);
24302
24303   start_lambda_scope (decl);
24304
24305   /* Parse the default argument.  */
24306   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
24307   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
24308     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
24309
24310   finish_lambda_scope ();
24311
24312   if (parsed_arg == error_mark_node)
24313     cp_parser_skip_to_end_of_statement (parser);
24314
24315   if (!processing_template_decl)
24316     {
24317       /* In a non-template class, check conversions now.  In a template,
24318          we'll wait and instantiate these as needed.  */
24319       if (TREE_CODE (decl) == PARM_DECL)
24320         parsed_arg = check_default_argument (parmtype, parsed_arg,
24321                                              tf_warning_or_error);
24322       else
24323         parsed_arg = digest_nsdmi_init (decl, parsed_arg);
24324     }
24325
24326   /* If the token stream has not been completely used up, then
24327      there was extra junk after the end of the default
24328      argument.  */
24329   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24330     {
24331       if (TREE_CODE (decl) == PARM_DECL)
24332         cp_parser_error (parser, "expected %<,%>");
24333       else
24334         cp_parser_error (parser, "expected %<;%>");
24335     }
24336
24337   /* Revert to the main lexer.  */
24338   cp_parser_pop_lexer (parser);
24339
24340   return parsed_arg;
24341 }
24342
24343 /* FIELD is a non-static data member with an initializer which we saved for
24344    later; parse it now.  */
24345
24346 static void
24347 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
24348 {
24349   tree def;
24350
24351   maybe_begin_member_template_processing (field);
24352
24353   push_unparsed_function_queues (parser);
24354   def = cp_parser_late_parse_one_default_arg (parser, field,
24355                                               DECL_INITIAL (field),
24356                                               NULL_TREE);
24357   pop_unparsed_function_queues (parser);
24358
24359   maybe_end_member_template_processing ();
24360
24361   DECL_INITIAL (field) = def;
24362 }
24363
24364 /* FN is a FUNCTION_DECL which may contains a parameter with an
24365    unparsed DEFAULT_ARG.  Parse the default args now.  This function
24366    assumes that the current scope is the scope in which the default
24367    argument should be processed.  */
24368
24369 static void
24370 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
24371 {
24372   bool saved_local_variables_forbidden_p;
24373   tree parm, parmdecl;
24374
24375   /* While we're parsing the default args, we might (due to the
24376      statement expression extension) encounter more classes.  We want
24377      to handle them right away, but we don't want them getting mixed
24378      up with default args that are currently in the queue.  */
24379   push_unparsed_function_queues (parser);
24380
24381   /* Local variable names (and the `this' keyword) may not appear
24382      in a default argument.  */
24383   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
24384   parser->local_variables_forbidden_p = true;
24385
24386   push_defarg_context (fn);
24387
24388   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24389          parmdecl = DECL_ARGUMENTS (fn);
24390        parm && parm != void_list_node;
24391        parm = TREE_CHAIN (parm),
24392          parmdecl = DECL_CHAIN (parmdecl))
24393     {
24394       tree default_arg = TREE_PURPOSE (parm);
24395       tree parsed_arg;
24396       vec<tree, va_gc> *insts;
24397       tree copy;
24398       unsigned ix;
24399
24400       if (!default_arg)
24401         continue;
24402
24403       if (TREE_CODE (default_arg) != DEFAULT_ARG)
24404         /* This can happen for a friend declaration for a function
24405            already declared with default arguments.  */
24406         continue;
24407
24408       parsed_arg
24409         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24410                                                 default_arg,
24411                                                 TREE_VALUE (parm));
24412       if (parsed_arg == error_mark_node)
24413         {
24414           continue;
24415         }
24416
24417       TREE_PURPOSE (parm) = parsed_arg;
24418
24419       /* Update any instantiations we've already created.  */
24420       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24421            vec_safe_iterate (insts, ix, &copy); ix++)
24422         TREE_PURPOSE (copy) = parsed_arg;
24423     }
24424
24425   pop_defarg_context ();
24426
24427   /* Make sure no default arg is missing.  */
24428   check_default_args (fn);
24429
24430   /* Restore the state of local_variables_forbidden_p.  */
24431   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24432
24433   /* Restore the queue.  */
24434   pop_unparsed_function_queues (parser);
24435 }
24436
24437 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24438
24439      sizeof ... ( identifier )
24440
24441    where the 'sizeof' token has already been consumed.  */
24442
24443 static tree
24444 cp_parser_sizeof_pack (cp_parser *parser)
24445 {
24446   /* Consume the `...'.  */
24447   cp_lexer_consume_token (parser->lexer);
24448   maybe_warn_variadic_templates ();
24449
24450   bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24451   if (paren)
24452     cp_lexer_consume_token (parser->lexer);
24453   else
24454     permerror (cp_lexer_peek_token (parser->lexer)->location,
24455                "%<sizeof...%> argument must be surrounded by parentheses");
24456
24457   cp_token *token = cp_lexer_peek_token (parser->lexer);
24458   tree name = cp_parser_identifier (parser);
24459   if (name == error_mark_node)
24460     return error_mark_node;
24461   /* The name is not qualified.  */
24462   parser->scope = NULL_TREE;
24463   parser->qualifying_scope = NULL_TREE;
24464   parser->object_scope = NULL_TREE;
24465   tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24466   if (expr == error_mark_node)
24467     cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24468                                  token->location);
24469   if (TREE_CODE (expr) == TYPE_DECL)
24470     expr = TREE_TYPE (expr);
24471   else if (TREE_CODE (expr) == CONST_DECL)
24472     expr = DECL_INITIAL (expr);
24473   expr = make_pack_expansion (expr);
24474   PACK_EXPANSION_SIZEOF_P (expr) = true;
24475
24476   if (paren)
24477     cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24478
24479   return expr;
24480 }
24481
24482 /* Parse the operand of `sizeof' (or a similar operator).  Returns
24483    either a TYPE or an expression, depending on the form of the
24484    input.  The KEYWORD indicates which kind of expression we have
24485    encountered.  */
24486
24487 static tree
24488 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24489 {
24490   tree expr = NULL_TREE;
24491   const char *saved_message;
24492   char *tmp;
24493   bool saved_integral_constant_expression_p;
24494   bool saved_non_integral_constant_expression_p;
24495
24496   /* If it's a `...', then we are computing the length of a parameter
24497      pack.  */
24498   if (keyword == RID_SIZEOF
24499       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24500     return cp_parser_sizeof_pack (parser);
24501
24502   /* Types cannot be defined in a `sizeof' expression.  Save away the
24503      old message.  */
24504   saved_message = parser->type_definition_forbidden_message;
24505   /* And create the new one.  */
24506   tmp = concat ("types may not be defined in %<",
24507                 IDENTIFIER_POINTER (ridpointers[keyword]),
24508                 "%> expressions", NULL);
24509   parser->type_definition_forbidden_message = tmp;
24510
24511   /* The restrictions on constant-expressions do not apply inside
24512      sizeof expressions.  */
24513   saved_integral_constant_expression_p
24514     = parser->integral_constant_expression_p;
24515   saved_non_integral_constant_expression_p
24516     = parser->non_integral_constant_expression_p;
24517   parser->integral_constant_expression_p = false;
24518
24519   /* Do not actually evaluate the expression.  */
24520   ++cp_unevaluated_operand;
24521   ++c_inhibit_evaluation_warnings;
24522   /* If it's a `(', then we might be looking at the type-id
24523      construction.  */
24524   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24525     {
24526       tree type = NULL_TREE;
24527
24528       /* We can't be sure yet whether we're looking at a type-id or an
24529          expression.  */
24530       cp_parser_parse_tentatively (parser);
24531       /* Note: as a GNU Extension, compound literals are considered
24532          postfix-expressions as they are in C99, so they are valid
24533          arguments to sizeof.  See comment in cp_parser_cast_expression
24534          for details.  */
24535       if (cp_parser_compound_literal_p (parser))
24536         cp_parser_simulate_error (parser);
24537       else
24538         {
24539           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24540           parser->in_type_id_in_expr_p = true;
24541           /* Look for the type-id.  */
24542           type = cp_parser_type_id (parser);
24543           /* Look for the closing `)'.  */
24544           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24545           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24546         }
24547
24548       /* If all went well, then we're done.  */
24549       if (cp_parser_parse_definitely (parser))
24550         {
24551           cp_decl_specifier_seq decl_specs;
24552
24553           /* Build a trivial decl-specifier-seq.  */
24554           clear_decl_specs (&decl_specs);
24555           decl_specs.type = type;
24556
24557           /* Call grokdeclarator to figure out what type this is.  */
24558           expr = grokdeclarator (NULL,
24559                                  &decl_specs,
24560                                  TYPENAME,
24561                                  /*initialized=*/0,
24562                                  /*attrlist=*/NULL);
24563         }
24564     }
24565
24566   /* If the type-id production did not work out, then we must be
24567      looking at the unary-expression production.  */
24568   if (!expr)
24569     expr = cp_parser_unary_expression (parser);
24570
24571   /* Go back to evaluating expressions.  */
24572   --cp_unevaluated_operand;
24573   --c_inhibit_evaluation_warnings;
24574
24575   /* Free the message we created.  */
24576   free (tmp);
24577   /* And restore the old one.  */
24578   parser->type_definition_forbidden_message = saved_message;
24579   parser->integral_constant_expression_p
24580     = saved_integral_constant_expression_p;
24581   parser->non_integral_constant_expression_p
24582     = saved_non_integral_constant_expression_p;
24583
24584   return expr;
24585 }
24586
24587 /* If the current declaration has no declarator, return true.  */
24588
24589 static bool
24590 cp_parser_declares_only_class_p (cp_parser *parser)
24591 {
24592   /* If the next token is a `;' or a `,' then there is no
24593      declarator.  */
24594   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24595           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24596 }
24597
24598 /* Update the DECL_SPECS to reflect the storage class indicated by
24599    KEYWORD.  */
24600
24601 static void
24602 cp_parser_set_storage_class (cp_parser *parser,
24603                              cp_decl_specifier_seq *decl_specs,
24604                              enum rid keyword,
24605                              cp_token *token)
24606 {
24607   cp_storage_class storage_class;
24608
24609   if (parser->in_unbraced_linkage_specification_p)
24610     {
24611       error_at (token->location, "invalid use of %qD in linkage specification",
24612                 ridpointers[keyword]);
24613       return;
24614     }
24615   else if (decl_specs->storage_class != sc_none)
24616     {
24617       decl_specs->conflicting_specifiers_p = true;
24618       return;
24619     }
24620
24621   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24622       && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24623       && decl_specs->gnu_thread_keyword_p)
24624     {
24625       pedwarn (decl_specs->locations[ds_thread], 0,
24626                 "%<__thread%> before %qD", ridpointers[keyword]);
24627     }
24628
24629   switch (keyword)
24630     {
24631     case RID_AUTO:
24632       storage_class = sc_auto;
24633       break;
24634     case RID_REGISTER:
24635       storage_class = sc_register;
24636       break;
24637     case RID_STATIC:
24638       storage_class = sc_static;
24639       break;
24640     case RID_EXTERN:
24641       storage_class = sc_extern;
24642       break;
24643     case RID_MUTABLE:
24644       storage_class = sc_mutable;
24645       break;
24646     default:
24647       gcc_unreachable ();
24648     }
24649   decl_specs->storage_class = storage_class;
24650   set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24651
24652   /* A storage class specifier cannot be applied alongside a typedef 
24653      specifier. If there is a typedef specifier present then set 
24654      conflicting_specifiers_p which will trigger an error later
24655      on in grokdeclarator. */
24656   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24657     decl_specs->conflicting_specifiers_p = true;
24658 }
24659
24660 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
24661    is true, the type is a class or enum definition.  */
24662
24663 static void
24664 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24665                               tree type_spec,
24666                               cp_token *token,
24667                               bool type_definition_p)
24668 {
24669   decl_specs->any_specifiers_p = true;
24670
24671   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24672      (with, for example, in "typedef int wchar_t;") we remember that
24673      this is what happened.  In system headers, we ignore these
24674      declarations so that G++ can work with system headers that are not
24675      C++-safe.  */
24676   if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24677       && !type_definition_p
24678       && (type_spec == boolean_type_node
24679           || type_spec == char16_type_node
24680           || type_spec == char32_type_node
24681           || type_spec == wchar_type_node)
24682       && (decl_specs->type
24683           || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24684           || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24685           || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24686           || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24687     {
24688       decl_specs->redefined_builtin_type = type_spec;
24689       set_and_check_decl_spec_loc (decl_specs,
24690                                    ds_redefined_builtin_type_spec,
24691                                    token);
24692       if (!decl_specs->type)
24693         {
24694           decl_specs->type = type_spec;
24695           decl_specs->type_definition_p = false;
24696           set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24697         }
24698     }
24699   else if (decl_specs->type)
24700     decl_specs->multiple_types_p = true;
24701   else
24702     {
24703       decl_specs->type = type_spec;
24704       decl_specs->type_definition_p = type_definition_p;
24705       decl_specs->redefined_builtin_type = NULL_TREE;
24706       set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24707     }
24708 }
24709
24710 /* True iff TOKEN is the GNU keyword __thread.  */
24711
24712 static bool
24713 token_is__thread (cp_token *token)
24714 {
24715   gcc_assert (token->keyword == RID_THREAD);
24716   return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24717 }
24718
24719 /* Set the location for a declarator specifier and check if it is
24720    duplicated.
24721
24722    DECL_SPECS is the sequence of declarator specifiers onto which to
24723    set the location.
24724
24725    DS is the single declarator specifier to set which location  is to
24726    be set onto the existing sequence of declarators.
24727
24728    LOCATION is the location for the declarator specifier to
24729    consider.  */
24730
24731 static void
24732 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24733                              cp_decl_spec ds, cp_token *token)
24734 {
24735   gcc_assert (ds < ds_last);
24736
24737   if (decl_specs == NULL)
24738     return;
24739
24740   source_location location = token->location;
24741
24742   if (decl_specs->locations[ds] == 0)
24743     {
24744       decl_specs->locations[ds] = location;
24745       if (ds == ds_thread)
24746         decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24747     }
24748   else
24749     {
24750       if (ds == ds_long)
24751         {
24752           if (decl_specs->locations[ds_long_long] != 0)
24753             error_at (location,
24754                       "%<long long long%> is too long for GCC");
24755           else
24756             {
24757               decl_specs->locations[ds_long_long] = location;
24758               pedwarn_cxx98 (location,
24759                              OPT_Wlong_long, 
24760                              "ISO C++ 1998 does not support %<long long%>");
24761             }
24762         }
24763       else if (ds == ds_thread)
24764         {
24765           bool gnu = token_is__thread (token);
24766           if (gnu != decl_specs->gnu_thread_keyword_p)
24767             error_at (location,
24768                       "both %<__thread%> and %<thread_local%> specified");
24769           else
24770             error_at (location, "duplicate %qD", token->u.value);
24771         }
24772       else
24773         {
24774           static const char *const decl_spec_names[] = {
24775             "signed",
24776             "unsigned",
24777             "short",
24778             "long",
24779             "const",
24780             "volatile",
24781             "restrict",
24782             "inline",
24783             "virtual",
24784             "explicit",
24785             "friend",
24786             "typedef",
24787             "using",
24788             "constexpr",
24789             "__complex"
24790           };
24791           error_at (location,
24792                     "duplicate %qs", decl_spec_names[ds]);
24793         }
24794     }
24795 }
24796
24797 /* Return true iff the declarator specifier DS is present in the
24798    sequence of declarator specifiers DECL_SPECS.  */
24799
24800 bool
24801 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24802                           cp_decl_spec ds)
24803 {
24804   gcc_assert (ds < ds_last);
24805
24806   if (decl_specs == NULL)
24807     return false;
24808
24809   return decl_specs->locations[ds] != 0;
24810 }
24811
24812 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24813    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
24814
24815 static bool
24816 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24817 {
24818   return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24819 }
24820
24821 /* Issue an error message indicating that TOKEN_DESC was expected.
24822    If KEYWORD is true, it indicated this function is called by
24823    cp_parser_require_keword and the required token can only be
24824    a indicated keyword. */
24825
24826 static void
24827 cp_parser_required_error (cp_parser *parser,
24828                           required_token token_desc,
24829                           bool keyword)
24830 {
24831   switch (token_desc)
24832     {
24833       case RT_NEW:
24834         cp_parser_error (parser, "expected %<new%>");
24835         return;
24836       case RT_DELETE:
24837         cp_parser_error (parser, "expected %<delete%>");
24838         return;
24839       case RT_RETURN:
24840         cp_parser_error (parser, "expected %<return%>");
24841         return;
24842       case RT_WHILE:
24843         cp_parser_error (parser, "expected %<while%>");
24844         return;
24845       case RT_EXTERN:
24846         cp_parser_error (parser, "expected %<extern%>");
24847         return;
24848       case RT_STATIC_ASSERT:
24849         cp_parser_error (parser, "expected %<static_assert%>");
24850         return;
24851       case RT_DECLTYPE:
24852         cp_parser_error (parser, "expected %<decltype%>");
24853         return;
24854       case RT_OPERATOR:
24855         cp_parser_error (parser, "expected %<operator%>");
24856         return;
24857       case RT_CLASS:
24858         cp_parser_error (parser, "expected %<class%>");
24859         return;
24860       case RT_TEMPLATE:
24861         cp_parser_error (parser, "expected %<template%>");
24862         return;
24863       case RT_NAMESPACE:
24864         cp_parser_error (parser, "expected %<namespace%>");
24865         return;
24866       case RT_USING:
24867         cp_parser_error (parser, "expected %<using%>");
24868         return;
24869       case RT_ASM:
24870         cp_parser_error (parser, "expected %<asm%>");
24871         return;
24872       case RT_TRY:
24873         cp_parser_error (parser, "expected %<try%>");
24874         return;
24875       case RT_CATCH:
24876         cp_parser_error (parser, "expected %<catch%>");
24877         return;
24878       case RT_THROW:
24879         cp_parser_error (parser, "expected %<throw%>");
24880         return;
24881       case RT_LABEL:
24882         cp_parser_error (parser, "expected %<__label__%>");
24883         return;
24884       case RT_AT_TRY:
24885         cp_parser_error (parser, "expected %<@try%>");
24886         return;
24887       case RT_AT_SYNCHRONIZED:
24888         cp_parser_error (parser, "expected %<@synchronized%>");
24889         return;
24890       case RT_AT_THROW:
24891         cp_parser_error (parser, "expected %<@throw%>");
24892         return;
24893       case RT_TRANSACTION_ATOMIC:
24894         cp_parser_error (parser, "expected %<__transaction_atomic%>");
24895         return;
24896       case RT_TRANSACTION_RELAXED:
24897         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24898         return;
24899       default:
24900         break;
24901     }
24902   if (!keyword)
24903     {
24904       switch (token_desc)
24905         {
24906           case RT_SEMICOLON:
24907             cp_parser_error (parser, "expected %<;%>");
24908             return;
24909           case RT_OPEN_PAREN:
24910             cp_parser_error (parser, "expected %<(%>");
24911             return;
24912           case RT_CLOSE_BRACE:
24913             cp_parser_error (parser, "expected %<}%>");
24914             return;
24915           case RT_OPEN_BRACE:
24916             cp_parser_error (parser, "expected %<{%>");
24917             return;
24918           case RT_CLOSE_SQUARE:
24919             cp_parser_error (parser, "expected %<]%>");
24920             return;
24921           case RT_OPEN_SQUARE:
24922             cp_parser_error (parser, "expected %<[%>");
24923             return;
24924           case RT_COMMA:
24925             cp_parser_error (parser, "expected %<,%>");
24926             return;
24927           case RT_SCOPE:
24928             cp_parser_error (parser, "expected %<::%>");
24929             return;
24930           case RT_LESS:
24931             cp_parser_error (parser, "expected %<<%>");
24932             return;
24933           case RT_GREATER:
24934             cp_parser_error (parser, "expected %<>%>");
24935             return;
24936           case RT_EQ:
24937             cp_parser_error (parser, "expected %<=%>");
24938             return;
24939           case RT_ELLIPSIS:
24940             cp_parser_error (parser, "expected %<...%>");
24941             return;
24942           case RT_MULT:
24943             cp_parser_error (parser, "expected %<*%>");
24944             return;
24945           case RT_COMPL:
24946             cp_parser_error (parser, "expected %<~%>");
24947             return;
24948           case RT_COLON:
24949             cp_parser_error (parser, "expected %<:%>");
24950             return;
24951           case RT_COLON_SCOPE:
24952             cp_parser_error (parser, "expected %<:%> or %<::%>");
24953             return;
24954           case RT_CLOSE_PAREN:
24955             cp_parser_error (parser, "expected %<)%>");
24956             return;
24957           case RT_COMMA_CLOSE_PAREN:
24958             cp_parser_error (parser, "expected %<,%> or %<)%>");
24959             return;
24960           case RT_PRAGMA_EOL:
24961             cp_parser_error (parser, "expected end of line");
24962             return;
24963           case RT_NAME:
24964             cp_parser_error (parser, "expected identifier");
24965             return;
24966           case RT_SELECT:
24967             cp_parser_error (parser, "expected selection-statement");
24968             return;
24969           case RT_INTERATION:
24970             cp_parser_error (parser, "expected iteration-statement");
24971             return;
24972           case RT_JUMP:
24973             cp_parser_error (parser, "expected jump-statement");
24974             return;
24975           case RT_CLASS_KEY:
24976             cp_parser_error (parser, "expected class-key");
24977             return;
24978           case RT_CLASS_TYPENAME_TEMPLATE:
24979             cp_parser_error (parser,
24980                  "expected %<class%>, %<typename%>, or %<template%>");
24981             return;
24982           default:
24983             gcc_unreachable ();
24984         }
24985     }
24986   else
24987     gcc_unreachable ();
24988 }
24989
24990
24991
24992 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
24993    issue an error message indicating that TOKEN_DESC was expected.
24994
24995    Returns the token consumed, if the token had the appropriate type.
24996    Otherwise, returns NULL.  */
24997
24998 static cp_token *
24999 cp_parser_require (cp_parser* parser,
25000                    enum cpp_ttype type,
25001                    required_token token_desc)
25002 {
25003   if (cp_lexer_next_token_is (parser->lexer, type))
25004     return cp_lexer_consume_token (parser->lexer);
25005   else
25006     {
25007       /* Output the MESSAGE -- unless we're parsing tentatively.  */
25008       if (!cp_parser_simulate_error (parser))
25009         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
25010       return NULL;
25011     }
25012 }
25013
25014 /* An error message is produced if the next token is not '>'.
25015    All further tokens are skipped until the desired token is
25016    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
25017
25018 static void
25019 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
25020 {
25021   /* Current level of '< ... >'.  */
25022   unsigned level = 0;
25023   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
25024   unsigned nesting_depth = 0;
25025
25026   /* Are we ready, yet?  If not, issue error message.  */
25027   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
25028     return;
25029
25030   /* Skip tokens until the desired token is found.  */
25031   while (true)
25032     {
25033       /* Peek at the next token.  */
25034       switch (cp_lexer_peek_token (parser->lexer)->type)
25035         {
25036         case CPP_LESS:
25037           if (!nesting_depth)
25038             ++level;
25039           break;
25040
25041         case CPP_RSHIFT:
25042           if (cxx_dialect == cxx98)
25043             /* C++0x views the `>>' operator as two `>' tokens, but
25044                C++98 does not. */
25045             break;
25046           else if (!nesting_depth && level-- == 0)
25047             {
25048               /* We've hit a `>>' where the first `>' closes the
25049                  template argument list, and the second `>' is
25050                  spurious.  Just consume the `>>' and stop; we've
25051                  already produced at least one error.  */
25052               cp_lexer_consume_token (parser->lexer);
25053               return;
25054             }
25055           /* Fall through for C++0x, so we handle the second `>' in
25056              the `>>'.  */
25057
25058         case CPP_GREATER:
25059           if (!nesting_depth && level-- == 0)
25060             {
25061               /* We've reached the token we want, consume it and stop.  */
25062               cp_lexer_consume_token (parser->lexer);
25063               return;
25064             }
25065           break;
25066
25067         case CPP_OPEN_PAREN:
25068         case CPP_OPEN_SQUARE:
25069           ++nesting_depth;
25070           break;
25071
25072         case CPP_CLOSE_PAREN:
25073         case CPP_CLOSE_SQUARE:
25074           if (nesting_depth-- == 0)
25075             return;
25076           break;
25077
25078         case CPP_EOF:
25079         case CPP_PRAGMA_EOL:
25080         case CPP_SEMICOLON:
25081         case CPP_OPEN_BRACE:
25082         case CPP_CLOSE_BRACE:
25083           /* The '>' was probably forgotten, don't look further.  */
25084           return;
25085
25086         default:
25087           break;
25088         }
25089
25090       /* Consume this token.  */
25091       cp_lexer_consume_token (parser->lexer);
25092     }
25093 }
25094
25095 /* If the next token is the indicated keyword, consume it.  Otherwise,
25096    issue an error message indicating that TOKEN_DESC was expected.
25097
25098    Returns the token consumed, if the token had the appropriate type.
25099    Otherwise, returns NULL.  */
25100
25101 static cp_token *
25102 cp_parser_require_keyword (cp_parser* parser,
25103                            enum rid keyword,
25104                            required_token token_desc)
25105 {
25106   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
25107
25108   if (token && token->keyword != keyword)
25109     {
25110       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
25111       return NULL;
25112     }
25113
25114   return token;
25115 }
25116
25117 /* Returns TRUE iff TOKEN is a token that can begin the body of a
25118    function-definition.  */
25119
25120 static bool
25121 cp_parser_token_starts_function_definition_p (cp_token* token)
25122 {
25123   return (/* An ordinary function-body begins with an `{'.  */
25124           token->type == CPP_OPEN_BRACE
25125           /* A ctor-initializer begins with a `:'.  */
25126           || token->type == CPP_COLON
25127           /* A function-try-block begins with `try'.  */
25128           || token->keyword == RID_TRY
25129           /* A function-transaction-block begins with `__transaction_atomic'
25130              or `__transaction_relaxed'.  */
25131           || token->keyword == RID_TRANSACTION_ATOMIC
25132           || token->keyword == RID_TRANSACTION_RELAXED
25133           /* The named return value extension begins with `return'.  */
25134           || token->keyword == RID_RETURN);
25135 }
25136
25137 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
25138    definition.  */
25139
25140 static bool
25141 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
25142 {
25143   cp_token *token;
25144
25145   token = cp_lexer_peek_token (parser->lexer);
25146   return (token->type == CPP_OPEN_BRACE
25147           || (token->type == CPP_COLON
25148               && !parser->colon_doesnt_start_class_def_p));
25149 }
25150
25151 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
25152    C++0x) ending a template-argument.  */
25153
25154 static bool
25155 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
25156 {
25157   cp_token *token;
25158
25159   token = cp_lexer_peek_token (parser->lexer);
25160   return (token->type == CPP_COMMA 
25161           || token->type == CPP_GREATER
25162           || token->type == CPP_ELLIPSIS
25163           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
25164 }
25165
25166 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
25167    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
25168
25169 static bool
25170 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
25171                                                      size_t n)
25172 {
25173   cp_token *token;
25174
25175   token = cp_lexer_peek_nth_token (parser->lexer, n);
25176   if (token->type == CPP_LESS)
25177     return true;
25178   /* Check for the sequence `<::' in the original code. It would be lexed as
25179      `[:', where `[' is a digraph, and there is no whitespace before
25180      `:'.  */
25181   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
25182     {
25183       cp_token *token2;
25184       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
25185       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
25186         return true;
25187     }
25188   return false;
25189 }
25190
25191 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
25192    or none_type otherwise.  */
25193
25194 static enum tag_types
25195 cp_parser_token_is_class_key (cp_token* token)
25196 {
25197   switch (token->keyword)
25198     {
25199     case RID_CLASS:
25200       return class_type;
25201     case RID_STRUCT:
25202       return record_type;
25203     case RID_UNION:
25204       return union_type;
25205
25206     default:
25207       return none_type;
25208     }
25209 }
25210
25211 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
25212    or none_type otherwise or if the token is null.  */
25213
25214 static enum tag_types
25215 cp_parser_token_is_type_parameter_key (cp_token* token)
25216 {
25217   if (!token)
25218     return none_type;
25219
25220   switch (token->keyword)
25221     {
25222     case RID_CLASS:
25223       return class_type;
25224     case RID_TYPENAME:
25225       return typename_type;
25226
25227     default:
25228       return none_type;
25229     }
25230 }
25231
25232 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
25233
25234 static void
25235 cp_parser_check_class_key (enum tag_types class_key, tree type)
25236 {
25237   if (type == error_mark_node)
25238     return;
25239   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
25240     {
25241       if (permerror (input_location, "%qs tag used in naming %q#T",
25242                      class_key == union_type ? "union"
25243                      : class_key == record_type ? "struct" : "class",
25244                      type))
25245         inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
25246                 "%q#T was previously declared here", type);
25247     }
25248 }
25249
25250 /* Issue an error message if DECL is redeclared with different
25251    access than its original declaration [class.access.spec/3].
25252    This applies to nested classes and nested class templates.
25253    [class.mem/1].  */
25254
25255 static void
25256 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
25257 {
25258   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
25259     return;
25260
25261   if ((TREE_PRIVATE (decl)
25262        != (current_access_specifier == access_private_node))
25263       || (TREE_PROTECTED (decl)
25264           != (current_access_specifier == access_protected_node)))
25265     error_at (location, "%qD redeclared with different access", decl);
25266 }
25267
25268 /* Look for the `template' keyword, as a syntactic disambiguator.
25269    Return TRUE iff it is present, in which case it will be
25270    consumed.  */
25271
25272 static bool
25273 cp_parser_optional_template_keyword (cp_parser *parser)
25274 {
25275   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25276     {
25277       /* In C++98 the `template' keyword can only be used within templates;
25278          outside templates the parser can always figure out what is a
25279          template and what is not.  In C++11,  per the resolution of DR 468,
25280          `template' is allowed in cases where it is not strictly necessary.  */
25281       if (!processing_template_decl
25282           && pedantic && cxx_dialect == cxx98)
25283         {
25284           cp_token *token = cp_lexer_peek_token (parser->lexer);
25285           pedwarn (token->location, OPT_Wpedantic,
25286                    "in C++98 %<template%> (as a disambiguator) is only "
25287                    "allowed within templates");
25288           /* If this part of the token stream is rescanned, the same
25289              error message would be generated.  So, we purge the token
25290              from the stream.  */
25291           cp_lexer_purge_token (parser->lexer);
25292           return false;
25293         }
25294       else
25295         {
25296           /* Consume the `template' keyword.  */
25297           cp_lexer_consume_token (parser->lexer);
25298           return true;
25299         }
25300     }
25301   return false;
25302 }
25303
25304 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
25305    set PARSER->SCOPE, and perform other related actions.  */
25306
25307 static void
25308 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
25309 {
25310   int i;
25311   struct tree_check *check_value;
25312   deferred_access_check *chk;
25313   vec<deferred_access_check, va_gc> *checks;
25314
25315   /* Get the stored value.  */
25316   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
25317   /* Perform any access checks that were deferred.  */
25318   checks = check_value->checks;
25319   if (checks)
25320     {
25321       FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
25322         perform_or_defer_access_check (chk->binfo,
25323                                        chk->decl,
25324                                        chk->diag_decl, tf_warning_or_error);
25325     }
25326   /* Set the scope from the stored value.  */
25327   parser->scope = check_value->value;
25328   parser->qualifying_scope = check_value->qualifying_scope;
25329   parser->object_scope = NULL_TREE;
25330 }
25331
25332 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
25333    encounter the end of a block before what we were looking for.  */
25334
25335 static bool
25336 cp_parser_cache_group (cp_parser *parser,
25337                        enum cpp_ttype end,
25338                        unsigned depth)
25339 {
25340   while (true)
25341     {
25342       cp_token *token = cp_lexer_peek_token (parser->lexer);
25343
25344       /* Abort a parenthesized expression if we encounter a semicolon.  */
25345       if ((end == CPP_CLOSE_PAREN || depth == 0)
25346           && token->type == CPP_SEMICOLON)
25347         return true;
25348       /* If we've reached the end of the file, stop.  */
25349       if (token->type == CPP_EOF
25350           || (end != CPP_PRAGMA_EOL
25351               && token->type == CPP_PRAGMA_EOL))
25352         return true;
25353       if (token->type == CPP_CLOSE_BRACE && depth == 0)
25354         /* We've hit the end of an enclosing block, so there's been some
25355            kind of syntax error.  */
25356         return true;
25357
25358       /* Consume the token.  */
25359       cp_lexer_consume_token (parser->lexer);
25360       /* See if it starts a new group.  */
25361       if (token->type == CPP_OPEN_BRACE)
25362         {
25363           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
25364           /* In theory this should probably check end == '}', but
25365              cp_parser_save_member_function_body needs it to exit
25366              after either '}' or ')' when called with ')'.  */
25367           if (depth == 0)
25368             return false;
25369         }
25370       else if (token->type == CPP_OPEN_PAREN)
25371         {
25372           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
25373           if (depth == 0 && end == CPP_CLOSE_PAREN)
25374             return false;
25375         }
25376       else if (token->type == CPP_PRAGMA)
25377         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
25378       else if (token->type == end)
25379         return false;
25380     }
25381 }
25382
25383 /* Like above, for caching a default argument or NSDMI.  Both of these are
25384    terminated by a non-nested comma, but it can be unclear whether or not a
25385    comma is nested in a template argument list unless we do more parsing.
25386    In order to handle this ambiguity, when we encounter a ',' after a '<'
25387    we try to parse what follows as a parameter-declaration-list (in the
25388    case of a default argument) or a member-declarator (in the case of an
25389    NSDMI).  If that succeeds, then we stop caching.  */
25390
25391 static tree
25392 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25393 {
25394   unsigned depth = 0;
25395   int maybe_template_id = 0;
25396   cp_token *first_token;
25397   cp_token *token;
25398   tree default_argument;
25399
25400   /* Add tokens until we have processed the entire default
25401      argument.  We add the range [first_token, token).  */
25402   first_token = cp_lexer_peek_token (parser->lexer);
25403   if (first_token->type == CPP_OPEN_BRACE)
25404     {
25405       /* For list-initialization, this is straightforward.  */
25406       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25407       token = cp_lexer_peek_token (parser->lexer);
25408     }
25409   else while (true)
25410     {
25411       bool done = false;
25412
25413       /* Peek at the next token.  */
25414       token = cp_lexer_peek_token (parser->lexer);
25415       /* What we do depends on what token we have.  */
25416       switch (token->type)
25417         {
25418           /* In valid code, a default argument must be
25419              immediately followed by a `,' `)', or `...'.  */
25420         case CPP_COMMA:
25421           if (depth == 0 && maybe_template_id)
25422             {
25423               /* If we've seen a '<', we might be in a
25424                  template-argument-list.  Until Core issue 325 is
25425                  resolved, we don't know how this situation ought
25426                  to be handled, so try to DTRT.  We check whether
25427                  what comes after the comma is a valid parameter
25428                  declaration list.  If it is, then the comma ends
25429                  the default argument; otherwise the default
25430                  argument continues.  */
25431               bool error = false;
25432
25433               /* Set ITALP so cp_parser_parameter_declaration_list
25434                  doesn't decide to commit to this parse.  */
25435               bool saved_italp = parser->in_template_argument_list_p;
25436               parser->in_template_argument_list_p = true;
25437
25438               cp_parser_parse_tentatively (parser);
25439               cp_lexer_consume_token (parser->lexer);
25440
25441               if (nsdmi)
25442                 {
25443                   int ctor_dtor_or_conv_p;
25444                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25445                                         &ctor_dtor_or_conv_p,
25446                                         /*parenthesized_p=*/NULL,
25447                                         /*member_p=*/true,
25448                                         /*friend_p=*/false);
25449                 }
25450               else
25451                 {
25452                   begin_scope (sk_function_parms, NULL_TREE);
25453                   cp_parser_parameter_declaration_list (parser, &error);
25454                   pop_bindings_and_leave_scope ();
25455                 }
25456               if (!cp_parser_error_occurred (parser) && !error)
25457                 done = true;
25458               cp_parser_abort_tentative_parse (parser);
25459
25460               parser->in_template_argument_list_p = saved_italp;
25461               break;
25462             }
25463         case CPP_CLOSE_PAREN:
25464         case CPP_ELLIPSIS:
25465           /* If we run into a non-nested `;', `}', or `]',
25466              then the code is invalid -- but the default
25467              argument is certainly over.  */
25468         case CPP_SEMICOLON:
25469         case CPP_CLOSE_BRACE:
25470         case CPP_CLOSE_SQUARE:
25471           if (depth == 0
25472               /* Handle correctly int n = sizeof ... ( p );  */
25473               && token->type != CPP_ELLIPSIS)
25474             done = true;
25475           /* Update DEPTH, if necessary.  */
25476           else if (token->type == CPP_CLOSE_PAREN
25477                    || token->type == CPP_CLOSE_BRACE
25478                    || token->type == CPP_CLOSE_SQUARE)
25479             --depth;
25480           break;
25481
25482         case CPP_OPEN_PAREN:
25483         case CPP_OPEN_SQUARE:
25484         case CPP_OPEN_BRACE:
25485           ++depth;
25486           break;
25487
25488         case CPP_LESS:
25489           if (depth == 0)
25490             /* This might be the comparison operator, or it might
25491                start a template argument list.  */
25492             ++maybe_template_id;
25493           break;
25494
25495         case CPP_RSHIFT:
25496           if (cxx_dialect == cxx98)
25497             break;
25498           /* Fall through for C++0x, which treats the `>>'
25499              operator like two `>' tokens in certain
25500              cases.  */
25501
25502         case CPP_GREATER:
25503           if (depth == 0)
25504             {
25505               /* This might be an operator, or it might close a
25506                  template argument list.  But if a previous '<'
25507                  started a template argument list, this will have
25508                  closed it, so we can't be in one anymore.  */
25509               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25510               if (maybe_template_id < 0)
25511                 maybe_template_id = 0;
25512             }
25513           break;
25514
25515           /* If we run out of tokens, issue an error message.  */
25516         case CPP_EOF:
25517         case CPP_PRAGMA_EOL:
25518           error_at (token->location, "file ends in default argument");
25519           done = true;
25520           break;
25521
25522         case CPP_NAME:
25523         case CPP_SCOPE:
25524           /* In these cases, we should look for template-ids.
25525              For example, if the default argument is
25526              `X<int, double>()', we need to do name lookup to
25527              figure out whether or not `X' is a template; if
25528              so, the `,' does not end the default argument.
25529
25530              That is not yet done.  */
25531           break;
25532
25533         default:
25534           break;
25535         }
25536
25537       /* If we've reached the end, stop.  */
25538       if (done)
25539         break;
25540
25541       /* Add the token to the token block.  */
25542       token = cp_lexer_consume_token (parser->lexer);
25543     }
25544
25545   /* Create a DEFAULT_ARG to represent the unparsed default
25546      argument.  */
25547   default_argument = make_node (DEFAULT_ARG);
25548   DEFARG_TOKENS (default_argument)
25549     = cp_token_cache_new (first_token, token);
25550   DEFARG_INSTANTIATIONS (default_argument) = NULL;
25551
25552   return default_argument;
25553 }
25554
25555 /* Begin parsing tentatively.  We always save tokens while parsing
25556    tentatively so that if the tentative parsing fails we can restore the
25557    tokens.  */
25558
25559 static void
25560 cp_parser_parse_tentatively (cp_parser* parser)
25561 {
25562   /* Enter a new parsing context.  */
25563   parser->context = cp_parser_context_new (parser->context);
25564   /* Begin saving tokens.  */
25565   cp_lexer_save_tokens (parser->lexer);
25566   /* In order to avoid repetitive access control error messages,
25567      access checks are queued up until we are no longer parsing
25568      tentatively.  */
25569   push_deferring_access_checks (dk_deferred);
25570 }
25571
25572 /* Commit to the currently active tentative parse.  */
25573
25574 static void
25575 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25576 {
25577   cp_parser_context *context;
25578   cp_lexer *lexer;
25579
25580   /* Mark all of the levels as committed.  */
25581   lexer = parser->lexer;
25582   for (context = parser->context; context->next; context = context->next)
25583     {
25584       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25585         break;
25586       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25587       while (!cp_lexer_saving_tokens (lexer))
25588         lexer = lexer->next;
25589       cp_lexer_commit_tokens (lexer);
25590     }
25591 }
25592
25593 /* Commit to the topmost currently active tentative parse.
25594
25595    Note that this function shouldn't be called when there are
25596    irreversible side-effects while in a tentative state.  For
25597    example, we shouldn't create a permanent entry in the symbol
25598    table, or issue an error message that might not apply if the
25599    tentative parse is aborted.  */
25600
25601 static void
25602 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25603 {
25604   cp_parser_context *context = parser->context;
25605   cp_lexer *lexer = parser->lexer;
25606
25607   if (context)
25608     {
25609       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25610         return;
25611       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25612
25613       while (!cp_lexer_saving_tokens (lexer))
25614         lexer = lexer->next;
25615       cp_lexer_commit_tokens (lexer);
25616     }
25617 }
25618
25619 /* Abort the currently active tentative parse.  All consumed tokens
25620    will be rolled back, and no diagnostics will be issued.  */
25621
25622 static void
25623 cp_parser_abort_tentative_parse (cp_parser* parser)
25624 {
25625   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25626               || errorcount > 0);
25627   cp_parser_simulate_error (parser);
25628   /* Now, pretend that we want to see if the construct was
25629      successfully parsed.  */
25630   cp_parser_parse_definitely (parser);
25631 }
25632
25633 /* Stop parsing tentatively.  If a parse error has occurred, restore the
25634    token stream.  Otherwise, commit to the tokens we have consumed.
25635    Returns true if no error occurred; false otherwise.  */
25636
25637 static bool
25638 cp_parser_parse_definitely (cp_parser* parser)
25639 {
25640   bool error_occurred;
25641   cp_parser_context *context;
25642
25643   /* Remember whether or not an error occurred, since we are about to
25644      destroy that information.  */
25645   error_occurred = cp_parser_error_occurred (parser);
25646   /* Remove the topmost context from the stack.  */
25647   context = parser->context;
25648   parser->context = context->next;
25649   /* If no parse errors occurred, commit to the tentative parse.  */
25650   if (!error_occurred)
25651     {
25652       /* Commit to the tokens read tentatively, unless that was
25653          already done.  */
25654       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25655         cp_lexer_commit_tokens (parser->lexer);
25656
25657       pop_to_parent_deferring_access_checks ();
25658     }
25659   /* Otherwise, if errors occurred, roll back our state so that things
25660      are just as they were before we began the tentative parse.  */
25661   else
25662     {
25663       cp_lexer_rollback_tokens (parser->lexer);
25664       pop_deferring_access_checks ();
25665     }
25666   /* Add the context to the front of the free list.  */
25667   context->next = cp_parser_context_free_list;
25668   cp_parser_context_free_list = context;
25669
25670   return !error_occurred;
25671 }
25672
25673 /* Returns true if we are parsing tentatively and are not committed to
25674    this tentative parse.  */
25675
25676 static bool
25677 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25678 {
25679   return (cp_parser_parsing_tentatively (parser)
25680           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25681 }
25682
25683 /* Returns nonzero iff an error has occurred during the most recent
25684    tentative parse.  */
25685
25686 static bool
25687 cp_parser_error_occurred (cp_parser* parser)
25688 {
25689   return (cp_parser_parsing_tentatively (parser)
25690           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25691 }
25692
25693 /* Returns nonzero if GNU extensions are allowed.  */
25694
25695 static bool
25696 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25697 {
25698   return parser->allow_gnu_extensions_p;
25699 }
25700 \f
25701 /* Objective-C++ Productions */
25702
25703
25704 /* Parse an Objective-C expression, which feeds into a primary-expression
25705    above.
25706
25707    objc-expression:
25708      objc-message-expression
25709      objc-string-literal
25710      objc-encode-expression
25711      objc-protocol-expression
25712      objc-selector-expression
25713
25714   Returns a tree representation of the expression.  */
25715
25716 static tree
25717 cp_parser_objc_expression (cp_parser* parser)
25718 {
25719   /* Try to figure out what kind of declaration is present.  */
25720   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25721
25722   switch (kwd->type)
25723     {
25724     case CPP_OPEN_SQUARE:
25725       return cp_parser_objc_message_expression (parser);
25726
25727     case CPP_OBJC_STRING:
25728       kwd = cp_lexer_consume_token (parser->lexer);
25729       return objc_build_string_object (kwd->u.value);
25730
25731     case CPP_KEYWORD:
25732       switch (kwd->keyword)
25733         {
25734         case RID_AT_ENCODE:
25735           return cp_parser_objc_encode_expression (parser);
25736
25737         case RID_AT_PROTOCOL:
25738           return cp_parser_objc_protocol_expression (parser);
25739
25740         case RID_AT_SELECTOR:
25741           return cp_parser_objc_selector_expression (parser);
25742
25743         default:
25744           break;
25745         }
25746     default:
25747       error_at (kwd->location,
25748                 "misplaced %<@%D%> Objective-C++ construct",
25749                 kwd->u.value);
25750       cp_parser_skip_to_end_of_block_or_statement (parser);
25751     }
25752
25753   return error_mark_node;
25754 }
25755
25756 /* Parse an Objective-C message expression.
25757
25758    objc-message-expression:
25759      [ objc-message-receiver objc-message-args ]
25760
25761    Returns a representation of an Objective-C message.  */
25762
25763 static tree
25764 cp_parser_objc_message_expression (cp_parser* parser)
25765 {
25766   tree receiver, messageargs;
25767
25768   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
25769   receiver = cp_parser_objc_message_receiver (parser);
25770   messageargs = cp_parser_objc_message_args (parser);
25771   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25772
25773   return objc_build_message_expr (receiver, messageargs);
25774 }
25775
25776 /* Parse an objc-message-receiver.
25777
25778    objc-message-receiver:
25779      expression
25780      simple-type-specifier
25781
25782   Returns a representation of the type or expression.  */
25783
25784 static tree
25785 cp_parser_objc_message_receiver (cp_parser* parser)
25786 {
25787   tree rcv;
25788
25789   /* An Objective-C message receiver may be either (1) a type
25790      or (2) an expression.  */
25791   cp_parser_parse_tentatively (parser);
25792   rcv = cp_parser_expression (parser);
25793
25794   /* If that worked out, fine.  */
25795   if (cp_parser_parse_definitely (parser))
25796     return rcv;
25797
25798   cp_parser_parse_tentatively (parser);
25799   rcv = cp_parser_simple_type_specifier (parser,
25800                                          /*decl_specs=*/NULL,
25801                                          CP_PARSER_FLAGS_NONE);
25802
25803   if (cp_parser_parse_definitely (parser))
25804     return objc_get_class_reference (rcv);
25805   
25806   cp_parser_error (parser, "objective-c++ message receiver expected");
25807   return error_mark_node;
25808 }
25809
25810 /* Parse the arguments and selectors comprising an Objective-C message.
25811
25812    objc-message-args:
25813      objc-selector
25814      objc-selector-args
25815      objc-selector-args , objc-comma-args
25816
25817    objc-selector-args:
25818      objc-selector [opt] : assignment-expression
25819      objc-selector-args objc-selector [opt] : assignment-expression
25820
25821    objc-comma-args:
25822      assignment-expression
25823      objc-comma-args , assignment-expression
25824
25825    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25826    selector arguments and TREE_VALUE containing a list of comma
25827    arguments.  */
25828
25829 static tree
25830 cp_parser_objc_message_args (cp_parser* parser)
25831 {
25832   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25833   bool maybe_unary_selector_p = true;
25834   cp_token *token = cp_lexer_peek_token (parser->lexer);
25835
25836   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25837     {
25838       tree selector = NULL_TREE, arg;
25839
25840       if (token->type != CPP_COLON)
25841         selector = cp_parser_objc_selector (parser);
25842
25843       /* Detect if we have a unary selector.  */
25844       if (maybe_unary_selector_p
25845           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25846         return build_tree_list (selector, NULL_TREE);
25847
25848       maybe_unary_selector_p = false;
25849       cp_parser_require (parser, CPP_COLON, RT_COLON);
25850       arg = cp_parser_assignment_expression (parser);
25851
25852       sel_args
25853         = chainon (sel_args,
25854                    build_tree_list (selector, arg));
25855
25856       token = cp_lexer_peek_token (parser->lexer);
25857     }
25858
25859   /* Handle non-selector arguments, if any. */
25860   while (token->type == CPP_COMMA)
25861     {
25862       tree arg;
25863
25864       cp_lexer_consume_token (parser->lexer);
25865       arg = cp_parser_assignment_expression (parser);
25866
25867       addl_args
25868         = chainon (addl_args,
25869                    build_tree_list (NULL_TREE, arg));
25870
25871       token = cp_lexer_peek_token (parser->lexer);
25872     }
25873
25874   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25875     {
25876       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25877       return build_tree_list (error_mark_node, error_mark_node);
25878     }
25879
25880   return build_tree_list (sel_args, addl_args);
25881 }
25882
25883 /* Parse an Objective-C encode expression.
25884
25885    objc-encode-expression:
25886      @encode objc-typename
25887
25888    Returns an encoded representation of the type argument.  */
25889
25890 static tree
25891 cp_parser_objc_encode_expression (cp_parser* parser)
25892 {
25893   tree type;
25894   cp_token *token;
25895
25896   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
25897   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25898   token = cp_lexer_peek_token (parser->lexer);
25899   type = complete_type (cp_parser_type_id (parser));
25900   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25901
25902   if (!type)
25903     {
25904       error_at (token->location, 
25905                 "%<@encode%> must specify a type as an argument");
25906       return error_mark_node;
25907     }
25908
25909   /* This happens if we find @encode(T) (where T is a template
25910      typename or something dependent on a template typename) when
25911      parsing a template.  In that case, we can't compile it
25912      immediately, but we rather create an AT_ENCODE_EXPR which will
25913      need to be instantiated when the template is used.
25914   */
25915   if (dependent_type_p (type))
25916     {
25917       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25918       TREE_READONLY (value) = 1;
25919       return value;
25920     }
25921
25922   return objc_build_encode_expr (type);
25923 }
25924
25925 /* Parse an Objective-C @defs expression.  */
25926
25927 static tree
25928 cp_parser_objc_defs_expression (cp_parser *parser)
25929 {
25930   tree name;
25931
25932   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
25933   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25934   name = cp_parser_identifier (parser);
25935   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25936
25937   return objc_get_class_ivars (name);
25938 }
25939
25940 /* Parse an Objective-C protocol expression.
25941
25942   objc-protocol-expression:
25943     @protocol ( identifier )
25944
25945   Returns a representation of the protocol expression.  */
25946
25947 static tree
25948 cp_parser_objc_protocol_expression (cp_parser* parser)
25949 {
25950   tree proto;
25951
25952   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
25953   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25954   proto = cp_parser_identifier (parser);
25955   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25956
25957   return objc_build_protocol_expr (proto);
25958 }
25959
25960 /* Parse an Objective-C selector expression.
25961
25962    objc-selector-expression:
25963      @selector ( objc-method-signature )
25964
25965    objc-method-signature:
25966      objc-selector
25967      objc-selector-seq
25968
25969    objc-selector-seq:
25970      objc-selector :
25971      objc-selector-seq objc-selector :
25972
25973   Returns a representation of the method selector.  */
25974
25975 static tree
25976 cp_parser_objc_selector_expression (cp_parser* parser)
25977 {
25978   tree sel_seq = NULL_TREE;
25979   bool maybe_unary_selector_p = true;
25980   cp_token *token;
25981   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25982
25983   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
25984   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25985   token = cp_lexer_peek_token (parser->lexer);
25986
25987   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25988          || token->type == CPP_SCOPE)
25989     {
25990       tree selector = NULL_TREE;
25991
25992       if (token->type != CPP_COLON
25993           || token->type == CPP_SCOPE)
25994         selector = cp_parser_objc_selector (parser);
25995
25996       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25997           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25998         {
25999           /* Detect if we have a unary selector.  */
26000           if (maybe_unary_selector_p)
26001             {
26002               sel_seq = selector;
26003               goto finish_selector;
26004             }
26005           else
26006             {
26007               cp_parser_error (parser, "expected %<:%>");
26008             }
26009         }
26010       maybe_unary_selector_p = false;
26011       token = cp_lexer_consume_token (parser->lexer);
26012
26013       if (token->type == CPP_SCOPE)
26014         {
26015           sel_seq
26016             = chainon (sel_seq,
26017                        build_tree_list (selector, NULL_TREE));
26018           sel_seq
26019             = chainon (sel_seq,
26020                        build_tree_list (NULL_TREE, NULL_TREE));
26021         }
26022       else
26023         sel_seq
26024           = chainon (sel_seq,
26025                      build_tree_list (selector, NULL_TREE));
26026
26027       token = cp_lexer_peek_token (parser->lexer);
26028     }
26029
26030  finish_selector:
26031   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26032
26033   return objc_build_selector_expr (loc, sel_seq);
26034 }
26035
26036 /* Parse a list of identifiers.
26037
26038    objc-identifier-list:
26039      identifier
26040      objc-identifier-list , identifier
26041
26042    Returns a TREE_LIST of identifier nodes.  */
26043
26044 static tree
26045 cp_parser_objc_identifier_list (cp_parser* parser)
26046 {
26047   tree identifier;
26048   tree list;
26049   cp_token *sep;
26050
26051   identifier = cp_parser_identifier (parser);
26052   if (identifier == error_mark_node)
26053     return error_mark_node;      
26054
26055   list = build_tree_list (NULL_TREE, identifier);
26056   sep = cp_lexer_peek_token (parser->lexer);
26057
26058   while (sep->type == CPP_COMMA)
26059     {
26060       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26061       identifier = cp_parser_identifier (parser);
26062       if (identifier == error_mark_node)
26063         return list;
26064
26065       list = chainon (list, build_tree_list (NULL_TREE,
26066                                              identifier));
26067       sep = cp_lexer_peek_token (parser->lexer);
26068     }
26069   
26070   return list;
26071 }
26072
26073 /* Parse an Objective-C alias declaration.
26074
26075    objc-alias-declaration:
26076      @compatibility_alias identifier identifier ;
26077
26078    This function registers the alias mapping with the Objective-C front end.
26079    It returns nothing.  */
26080
26081 static void
26082 cp_parser_objc_alias_declaration (cp_parser* parser)
26083 {
26084   tree alias, orig;
26085
26086   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
26087   alias = cp_parser_identifier (parser);
26088   orig = cp_parser_identifier (parser);
26089   objc_declare_alias (alias, orig);
26090   cp_parser_consume_semicolon_at_end_of_statement (parser);
26091 }
26092
26093 /* Parse an Objective-C class forward-declaration.
26094
26095    objc-class-declaration:
26096      @class objc-identifier-list ;
26097
26098    The function registers the forward declarations with the Objective-C
26099    front end.  It returns nothing.  */
26100
26101 static void
26102 cp_parser_objc_class_declaration (cp_parser* parser)
26103 {
26104   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
26105   while (true)
26106     {
26107       tree id;
26108       
26109       id = cp_parser_identifier (parser);
26110       if (id == error_mark_node)
26111         break;
26112       
26113       objc_declare_class (id);
26114
26115       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26116         cp_lexer_consume_token (parser->lexer);
26117       else
26118         break;
26119     }
26120   cp_parser_consume_semicolon_at_end_of_statement (parser);
26121 }
26122
26123 /* Parse a list of Objective-C protocol references.
26124
26125    objc-protocol-refs-opt:
26126      objc-protocol-refs [opt]
26127
26128    objc-protocol-refs:
26129      < objc-identifier-list >
26130
26131    Returns a TREE_LIST of identifiers, if any.  */
26132
26133 static tree
26134 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
26135 {
26136   tree protorefs = NULL_TREE;
26137
26138   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
26139     {
26140       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
26141       protorefs = cp_parser_objc_identifier_list (parser);
26142       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
26143     }
26144
26145   return protorefs;
26146 }
26147
26148 /* Parse a Objective-C visibility specification.  */
26149
26150 static void
26151 cp_parser_objc_visibility_spec (cp_parser* parser)
26152 {
26153   cp_token *vis = cp_lexer_peek_token (parser->lexer);
26154
26155   switch (vis->keyword)
26156     {
26157     case RID_AT_PRIVATE:
26158       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
26159       break;
26160     case RID_AT_PROTECTED:
26161       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
26162       break;
26163     case RID_AT_PUBLIC:
26164       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
26165       break;
26166     case RID_AT_PACKAGE:
26167       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
26168       break;
26169     default:
26170       return;
26171     }
26172
26173   /* Eat '@private'/'@protected'/'@public'.  */
26174   cp_lexer_consume_token (parser->lexer);
26175 }
26176
26177 /* Parse an Objective-C method type.  Return 'true' if it is a class
26178    (+) method, and 'false' if it is an instance (-) method.  */
26179
26180 static inline bool
26181 cp_parser_objc_method_type (cp_parser* parser)
26182 {
26183   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
26184     return true;
26185   else
26186     return false;
26187 }
26188
26189 /* Parse an Objective-C protocol qualifier.  */
26190
26191 static tree
26192 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
26193 {
26194   tree quals = NULL_TREE, node;
26195   cp_token *token = cp_lexer_peek_token (parser->lexer);
26196
26197   node = token->u.value;
26198
26199   while (node && identifier_p (node)
26200          && (node == ridpointers [(int) RID_IN]
26201              || node == ridpointers [(int) RID_OUT]
26202              || node == ridpointers [(int) RID_INOUT]
26203              || node == ridpointers [(int) RID_BYCOPY]
26204              || node == ridpointers [(int) RID_BYREF]
26205              || node == ridpointers [(int) RID_ONEWAY]))
26206     {
26207       quals = tree_cons (NULL_TREE, node, quals);
26208       cp_lexer_consume_token (parser->lexer);
26209       token = cp_lexer_peek_token (parser->lexer);
26210       node = token->u.value;
26211     }
26212
26213   return quals;
26214 }
26215
26216 /* Parse an Objective-C typename.  */
26217
26218 static tree
26219 cp_parser_objc_typename (cp_parser* parser)
26220 {
26221   tree type_name = NULL_TREE;
26222
26223   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26224     {
26225       tree proto_quals, cp_type = NULL_TREE;
26226
26227       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26228       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
26229
26230       /* An ObjC type name may consist of just protocol qualifiers, in which
26231          case the type shall default to 'id'.  */
26232       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26233         {
26234           cp_type = cp_parser_type_id (parser);
26235           
26236           /* If the type could not be parsed, an error has already
26237              been produced.  For error recovery, behave as if it had
26238              not been specified, which will use the default type
26239              'id'.  */
26240           if (cp_type == error_mark_node)
26241             {
26242               cp_type = NULL_TREE;
26243               /* We need to skip to the closing parenthesis as
26244                  cp_parser_type_id() does not seem to do it for
26245                  us.  */
26246               cp_parser_skip_to_closing_parenthesis (parser,
26247                                                      /*recovering=*/true,
26248                                                      /*or_comma=*/false,
26249                                                      /*consume_paren=*/false);
26250             }
26251         }
26252
26253       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26254       type_name = build_tree_list (proto_quals, cp_type);
26255     }
26256
26257   return type_name;
26258 }
26259
26260 /* Check to see if TYPE refers to an Objective-C selector name.  */
26261
26262 static bool
26263 cp_parser_objc_selector_p (enum cpp_ttype type)
26264 {
26265   return (type == CPP_NAME || type == CPP_KEYWORD
26266           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
26267           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
26268           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
26269           || type == CPP_XOR || type == CPP_XOR_EQ);
26270 }
26271
26272 /* Parse an Objective-C selector.  */
26273
26274 static tree
26275 cp_parser_objc_selector (cp_parser* parser)
26276 {
26277   cp_token *token = cp_lexer_consume_token (parser->lexer);
26278
26279   if (!cp_parser_objc_selector_p (token->type))
26280     {
26281       error_at (token->location, "invalid Objective-C++ selector name");
26282       return error_mark_node;
26283     }
26284
26285   /* C++ operator names are allowed to appear in ObjC selectors.  */
26286   switch (token->type)
26287     {
26288     case CPP_AND_AND: return get_identifier ("and");
26289     case CPP_AND_EQ: return get_identifier ("and_eq");
26290     case CPP_AND: return get_identifier ("bitand");
26291     case CPP_OR: return get_identifier ("bitor");
26292     case CPP_COMPL: return get_identifier ("compl");
26293     case CPP_NOT: return get_identifier ("not");
26294     case CPP_NOT_EQ: return get_identifier ("not_eq");
26295     case CPP_OR_OR: return get_identifier ("or");
26296     case CPP_OR_EQ: return get_identifier ("or_eq");
26297     case CPP_XOR: return get_identifier ("xor");
26298     case CPP_XOR_EQ: return get_identifier ("xor_eq");
26299     default: return token->u.value;
26300     }
26301 }
26302
26303 /* Parse an Objective-C params list.  */
26304
26305 static tree
26306 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
26307 {
26308   tree params = NULL_TREE;
26309   bool maybe_unary_selector_p = true;
26310   cp_token *token = cp_lexer_peek_token (parser->lexer);
26311
26312   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
26313     {
26314       tree selector = NULL_TREE, type_name, identifier;
26315       tree parm_attr = NULL_TREE;
26316
26317       if (token->keyword == RID_ATTRIBUTE)
26318         break;
26319
26320       if (token->type != CPP_COLON)
26321         selector = cp_parser_objc_selector (parser);
26322
26323       /* Detect if we have a unary selector.  */
26324       if (maybe_unary_selector_p
26325           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26326         {
26327           params = selector; /* Might be followed by attributes.  */
26328           break;
26329         }
26330
26331       maybe_unary_selector_p = false;
26332       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26333         {
26334           /* Something went quite wrong.  There should be a colon
26335              here, but there is not.  Stop parsing parameters.  */
26336           break;
26337         }
26338       type_name = cp_parser_objc_typename (parser);
26339       /* New ObjC allows attributes on parameters too.  */
26340       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26341         parm_attr = cp_parser_attributes_opt (parser);
26342       identifier = cp_parser_identifier (parser);
26343
26344       params
26345         = chainon (params,
26346                    objc_build_keyword_decl (selector,
26347                                             type_name,
26348                                             identifier,
26349                                             parm_attr));
26350
26351       token = cp_lexer_peek_token (parser->lexer);
26352     }
26353
26354   if (params == NULL_TREE)
26355     {
26356       cp_parser_error (parser, "objective-c++ method declaration is expected");
26357       return error_mark_node;
26358     }
26359
26360   /* We allow tail attributes for the method.  */
26361   if (token->keyword == RID_ATTRIBUTE)
26362     {
26363       *attributes = cp_parser_attributes_opt (parser);
26364       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26365           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26366         return params;
26367       cp_parser_error (parser, 
26368                        "method attributes must be specified at the end");
26369       return error_mark_node;
26370     }
26371
26372   if (params == NULL_TREE)
26373     {
26374       cp_parser_error (parser, "objective-c++ method declaration is expected");
26375       return error_mark_node;
26376     }
26377   return params;
26378 }
26379
26380 /* Parse the non-keyword Objective-C params.  */
26381
26382 static tree
26383 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
26384                                        tree* attributes)
26385 {
26386   tree params = make_node (TREE_LIST);
26387   cp_token *token = cp_lexer_peek_token (parser->lexer);
26388   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
26389
26390   while (token->type == CPP_COMMA)
26391     {
26392       cp_parameter_declarator *parmdecl;
26393       tree parm;
26394
26395       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26396       token = cp_lexer_peek_token (parser->lexer);
26397
26398       if (token->type == CPP_ELLIPSIS)
26399         {
26400           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
26401           *ellipsisp = true;
26402           token = cp_lexer_peek_token (parser->lexer);
26403           break;
26404         }
26405
26406       /* TODO: parse attributes for tail parameters.  */
26407       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26408       parm = grokdeclarator (parmdecl->declarator,
26409                              &parmdecl->decl_specifiers,
26410                              PARM, /*initialized=*/0,
26411                              /*attrlist=*/NULL);
26412
26413       chainon (params, build_tree_list (NULL_TREE, parm));
26414       token = cp_lexer_peek_token (parser->lexer);
26415     }
26416
26417   /* We allow tail attributes for the method.  */
26418   if (token->keyword == RID_ATTRIBUTE)
26419     {
26420       if (*attributes == NULL_TREE)
26421         {
26422           *attributes = cp_parser_attributes_opt (parser);
26423           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26424               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26425             return params;
26426         }
26427       else        
26428         /* We have an error, but parse the attributes, so that we can 
26429            carry on.  */
26430         *attributes = cp_parser_attributes_opt (parser);
26431
26432       cp_parser_error (parser, 
26433                        "method attributes must be specified at the end");
26434       return error_mark_node;
26435     }
26436
26437   return params;
26438 }
26439
26440 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
26441
26442 static void
26443 cp_parser_objc_interstitial_code (cp_parser* parser)
26444 {
26445   cp_token *token = cp_lexer_peek_token (parser->lexer);
26446
26447   /* If the next token is `extern' and the following token is a string
26448      literal, then we have a linkage specification.  */
26449   if (token->keyword == RID_EXTERN
26450       && cp_parser_is_pure_string_literal
26451          (cp_lexer_peek_nth_token (parser->lexer, 2)))
26452     cp_parser_linkage_specification (parser);
26453   /* Handle #pragma, if any.  */
26454   else if (token->type == CPP_PRAGMA)
26455     cp_parser_pragma (parser, pragma_objc_icode);
26456   /* Allow stray semicolons.  */
26457   else if (token->type == CPP_SEMICOLON)
26458     cp_lexer_consume_token (parser->lexer);
26459   /* Mark methods as optional or required, when building protocols.  */
26460   else if (token->keyword == RID_AT_OPTIONAL)
26461     {
26462       cp_lexer_consume_token (parser->lexer);
26463       objc_set_method_opt (true);
26464     }
26465   else if (token->keyword == RID_AT_REQUIRED)
26466     {
26467       cp_lexer_consume_token (parser->lexer);
26468       objc_set_method_opt (false);
26469     }
26470   else if (token->keyword == RID_NAMESPACE)
26471     cp_parser_namespace_definition (parser);
26472   /* Other stray characters must generate errors.  */
26473   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26474     {
26475       cp_lexer_consume_token (parser->lexer);
26476       error ("stray %qs between Objective-C++ methods",
26477              token->type == CPP_OPEN_BRACE ? "{" : "}");
26478     }
26479   /* Finally, try to parse a block-declaration, or a function-definition.  */
26480   else
26481     cp_parser_block_declaration (parser, /*statement_p=*/false);
26482 }
26483
26484 /* Parse a method signature.  */
26485
26486 static tree
26487 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26488 {
26489   tree rettype, kwdparms, optparms;
26490   bool ellipsis = false;
26491   bool is_class_method;
26492
26493   is_class_method = cp_parser_objc_method_type (parser);
26494   rettype = cp_parser_objc_typename (parser);
26495   *attributes = NULL_TREE;
26496   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26497   if (kwdparms == error_mark_node)
26498     return error_mark_node;
26499   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26500   if (optparms == error_mark_node)
26501     return error_mark_node;
26502
26503   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26504 }
26505
26506 static bool
26507 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26508 {
26509   tree tattr;  
26510   cp_lexer_save_tokens (parser->lexer);
26511   tattr = cp_parser_attributes_opt (parser);
26512   gcc_assert (tattr) ;
26513   
26514   /* If the attributes are followed by a method introducer, this is not allowed.
26515      Dump the attributes and flag the situation.  */
26516   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26517       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26518     return true;
26519
26520   /* Otherwise, the attributes introduce some interstitial code, possibly so
26521      rewind to allow that check.  */
26522   cp_lexer_rollback_tokens (parser->lexer);
26523   return false;  
26524 }
26525
26526 /* Parse an Objective-C method prototype list.  */
26527
26528 static void
26529 cp_parser_objc_method_prototype_list (cp_parser* parser)
26530 {
26531   cp_token *token = cp_lexer_peek_token (parser->lexer);
26532
26533   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26534     {
26535       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26536         {
26537           tree attributes, sig;
26538           bool is_class_method;
26539           if (token->type == CPP_PLUS)
26540             is_class_method = true;
26541           else
26542             is_class_method = false;
26543           sig = cp_parser_objc_method_signature (parser, &attributes);
26544           if (sig == error_mark_node)
26545             {
26546               cp_parser_skip_to_end_of_block_or_statement (parser);
26547               token = cp_lexer_peek_token (parser->lexer);
26548               continue;
26549             }
26550           objc_add_method_declaration (is_class_method, sig, attributes);
26551           cp_parser_consume_semicolon_at_end_of_statement (parser);
26552         }
26553       else if (token->keyword == RID_AT_PROPERTY)
26554         cp_parser_objc_at_property_declaration (parser);
26555       else if (token->keyword == RID_ATTRIBUTE 
26556                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26557         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
26558                     OPT_Wattributes, 
26559                     "prefix attributes are ignored for methods");
26560       else
26561         /* Allow for interspersed non-ObjC++ code.  */
26562         cp_parser_objc_interstitial_code (parser);
26563
26564       token = cp_lexer_peek_token (parser->lexer);
26565     }
26566
26567   if (token->type != CPP_EOF)
26568     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26569   else
26570     cp_parser_error (parser, "expected %<@end%>");
26571
26572   objc_finish_interface ();
26573 }
26574
26575 /* Parse an Objective-C method definition list.  */
26576
26577 static void
26578 cp_parser_objc_method_definition_list (cp_parser* parser)
26579 {
26580   cp_token *token = cp_lexer_peek_token (parser->lexer);
26581
26582   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26583     {
26584       tree meth;
26585
26586       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26587         {
26588           cp_token *ptk;
26589           tree sig, attribute;
26590           bool is_class_method;
26591           if (token->type == CPP_PLUS)
26592             is_class_method = true;
26593           else
26594             is_class_method = false;
26595           push_deferring_access_checks (dk_deferred);
26596           sig = cp_parser_objc_method_signature (parser, &attribute);
26597           if (sig == error_mark_node)
26598             {
26599               cp_parser_skip_to_end_of_block_or_statement (parser);
26600               token = cp_lexer_peek_token (parser->lexer);
26601               continue;
26602             }
26603           objc_start_method_definition (is_class_method, sig, attribute,
26604                                         NULL_TREE);
26605
26606           /* For historical reasons, we accept an optional semicolon.  */
26607           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26608             cp_lexer_consume_token (parser->lexer);
26609
26610           ptk = cp_lexer_peek_token (parser->lexer);
26611           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
26612                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26613             {
26614               perform_deferred_access_checks (tf_warning_or_error);
26615               stop_deferring_access_checks ();
26616               meth = cp_parser_function_definition_after_declarator (parser,
26617                                                                      false);
26618               pop_deferring_access_checks ();
26619               objc_finish_method_definition (meth);
26620             }
26621         }
26622       /* The following case will be removed once @synthesize is
26623          completely implemented.  */
26624       else if (token->keyword == RID_AT_PROPERTY)
26625         cp_parser_objc_at_property_declaration (parser);
26626       else if (token->keyword == RID_AT_SYNTHESIZE)
26627         cp_parser_objc_at_synthesize_declaration (parser);
26628       else if (token->keyword == RID_AT_DYNAMIC)
26629         cp_parser_objc_at_dynamic_declaration (parser);
26630       else if (token->keyword == RID_ATTRIBUTE 
26631                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26632         warning_at (token->location, OPT_Wattributes,
26633                     "prefix attributes are ignored for methods");
26634       else
26635         /* Allow for interspersed non-ObjC++ code.  */
26636         cp_parser_objc_interstitial_code (parser);
26637
26638       token = cp_lexer_peek_token (parser->lexer);
26639     }
26640
26641   if (token->type != CPP_EOF)
26642     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26643   else
26644     cp_parser_error (parser, "expected %<@end%>");
26645
26646   objc_finish_implementation ();
26647 }
26648
26649 /* Parse Objective-C ivars.  */
26650
26651 static void
26652 cp_parser_objc_class_ivars (cp_parser* parser)
26653 {
26654   cp_token *token = cp_lexer_peek_token (parser->lexer);
26655
26656   if (token->type != CPP_OPEN_BRACE)
26657     return;     /* No ivars specified.  */
26658
26659   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
26660   token = cp_lexer_peek_token (parser->lexer);
26661
26662   while (token->type != CPP_CLOSE_BRACE 
26663         && token->keyword != RID_AT_END && token->type != CPP_EOF)
26664     {
26665       cp_decl_specifier_seq declspecs;
26666       int decl_class_or_enum_p;
26667       tree prefix_attributes;
26668
26669       cp_parser_objc_visibility_spec (parser);
26670
26671       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26672         break;
26673
26674       cp_parser_decl_specifier_seq (parser,
26675                                     CP_PARSER_FLAGS_OPTIONAL,
26676                                     &declspecs,
26677                                     &decl_class_or_enum_p);
26678
26679       /* auto, register, static, extern, mutable.  */
26680       if (declspecs.storage_class != sc_none)
26681         {
26682           cp_parser_error (parser, "invalid type for instance variable");         
26683           declspecs.storage_class = sc_none;
26684         }
26685
26686       /* thread_local.  */
26687       if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26688         {
26689           cp_parser_error (parser, "invalid type for instance variable");
26690           declspecs.locations[ds_thread] = 0;
26691         }
26692       
26693       /* typedef.  */
26694       if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26695         {
26696           cp_parser_error (parser, "invalid type for instance variable");
26697           declspecs.locations[ds_typedef] = 0;
26698         }
26699
26700       prefix_attributes = declspecs.attributes;
26701       declspecs.attributes = NULL_TREE;
26702
26703       /* Keep going until we hit the `;' at the end of the
26704          declaration.  */
26705       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26706         {
26707           tree width = NULL_TREE, attributes, first_attribute, decl;
26708           cp_declarator *declarator = NULL;
26709           int ctor_dtor_or_conv_p;
26710
26711           /* Check for a (possibly unnamed) bitfield declaration.  */
26712           token = cp_lexer_peek_token (parser->lexer);
26713           if (token->type == CPP_COLON)
26714             goto eat_colon;
26715
26716           if (token->type == CPP_NAME
26717               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26718                   == CPP_COLON))
26719             {
26720               /* Get the name of the bitfield.  */
26721               declarator = make_id_declarator (NULL_TREE,
26722                                                cp_parser_identifier (parser),
26723                                                sfk_none);
26724
26725              eat_colon:
26726               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26727               /* Get the width of the bitfield.  */
26728               width
26729                 = cp_parser_constant_expression (parser);
26730             }
26731           else
26732             {
26733               /* Parse the declarator.  */
26734               declarator
26735                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26736                                         &ctor_dtor_or_conv_p,
26737                                         /*parenthesized_p=*/NULL,
26738                                         /*member_p=*/false,
26739                                         /*friend_p=*/false);
26740             }
26741
26742           /* Look for attributes that apply to the ivar.  */
26743           attributes = cp_parser_attributes_opt (parser);
26744           /* Remember which attributes are prefix attributes and
26745              which are not.  */
26746           first_attribute = attributes;
26747           /* Combine the attributes.  */
26748           attributes = chainon (prefix_attributes, attributes);
26749
26750           if (width)
26751               /* Create the bitfield declaration.  */
26752               decl = grokbitfield (declarator, &declspecs,
26753                                    width,
26754                                    attributes);
26755           else
26756             decl = grokfield (declarator, &declspecs,
26757                               NULL_TREE, /*init_const_expr_p=*/false,
26758                               NULL_TREE, attributes);
26759
26760           /* Add the instance variable.  */
26761           if (decl != error_mark_node && decl != NULL_TREE)
26762             objc_add_instance_variable (decl);
26763
26764           /* Reset PREFIX_ATTRIBUTES.  */
26765           while (attributes && TREE_CHAIN (attributes) != first_attribute)
26766             attributes = TREE_CHAIN (attributes);
26767           if (attributes)
26768             TREE_CHAIN (attributes) = NULL_TREE;
26769
26770           token = cp_lexer_peek_token (parser->lexer);
26771
26772           if (token->type == CPP_COMMA)
26773             {
26774               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
26775               continue;
26776             }
26777           break;
26778         }
26779
26780       cp_parser_consume_semicolon_at_end_of_statement (parser);
26781       token = cp_lexer_peek_token (parser->lexer);
26782     }
26783
26784   if (token->keyword == RID_AT_END)
26785     cp_parser_error (parser, "expected %<}%>");
26786
26787   /* Do not consume the RID_AT_END, so it will be read again as terminating
26788      the @interface of @implementation.  */ 
26789   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26790     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
26791     
26792   /* For historical reasons, we accept an optional semicolon.  */
26793   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26794     cp_lexer_consume_token (parser->lexer);
26795 }
26796
26797 /* Parse an Objective-C protocol declaration.  */
26798
26799 static void
26800 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26801 {
26802   tree proto, protorefs;
26803   cp_token *tok;
26804
26805   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
26806   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26807     {
26808       tok = cp_lexer_peek_token (parser->lexer);
26809       error_at (tok->location, "identifier expected after %<@protocol%>");
26810       cp_parser_consume_semicolon_at_end_of_statement (parser);
26811       return;
26812     }
26813
26814   /* See if we have a forward declaration or a definition.  */
26815   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26816
26817   /* Try a forward declaration first.  */
26818   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26819     {
26820       while (true)
26821         {
26822           tree id;
26823           
26824           id = cp_parser_identifier (parser);
26825           if (id == error_mark_node)
26826             break;
26827           
26828           objc_declare_protocol (id, attributes);
26829           
26830           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26831             cp_lexer_consume_token (parser->lexer);
26832           else
26833             break;
26834         }
26835       cp_parser_consume_semicolon_at_end_of_statement (parser);
26836     }
26837
26838   /* Ok, we got a full-fledged definition (or at least should).  */
26839   else
26840     {
26841       proto = cp_parser_identifier (parser);
26842       protorefs = cp_parser_objc_protocol_refs_opt (parser);
26843       objc_start_protocol (proto, protorefs, attributes);
26844       cp_parser_objc_method_prototype_list (parser);
26845     }
26846 }
26847
26848 /* Parse an Objective-C superclass or category.  */
26849
26850 static void
26851 cp_parser_objc_superclass_or_category (cp_parser *parser, 
26852                                        bool iface_p,
26853                                        tree *super,
26854                                        tree *categ, bool *is_class_extension)
26855 {
26856   cp_token *next = cp_lexer_peek_token (parser->lexer);
26857
26858   *super = *categ = NULL_TREE;
26859   *is_class_extension = false;
26860   if (next->type == CPP_COLON)
26861     {
26862       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
26863       *super = cp_parser_identifier (parser);
26864     }
26865   else if (next->type == CPP_OPEN_PAREN)
26866     {
26867       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
26868
26869       /* If there is no category name, and this is an @interface, we
26870          have a class extension.  */
26871       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26872         {
26873           *categ = NULL_TREE;
26874           *is_class_extension = true;
26875         }
26876       else
26877         *categ = cp_parser_identifier (parser);
26878
26879       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26880     }
26881 }
26882
26883 /* Parse an Objective-C class interface.  */
26884
26885 static void
26886 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26887 {
26888   tree name, super, categ, protos;
26889   bool is_class_extension;
26890
26891   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
26892   name = cp_parser_identifier (parser);
26893   if (name == error_mark_node)
26894     {
26895       /* It's hard to recover because even if valid @interface stuff
26896          is to follow, we can't compile it (or validate it) if we
26897          don't even know which class it refers to.  Let's assume this
26898          was a stray '@interface' token in the stream and skip it.
26899       */
26900       return;
26901     }
26902   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26903                                          &is_class_extension);
26904   protos = cp_parser_objc_protocol_refs_opt (parser);
26905
26906   /* We have either a class or a category on our hands.  */
26907   if (categ || is_class_extension)
26908     objc_start_category_interface (name, categ, protos, attributes);
26909   else
26910     {
26911       objc_start_class_interface (name, super, protos, attributes);
26912       /* Handle instance variable declarations, if any.  */
26913       cp_parser_objc_class_ivars (parser);
26914       objc_continue_interface ();
26915     }
26916
26917   cp_parser_objc_method_prototype_list (parser);
26918 }
26919
26920 /* Parse an Objective-C class implementation.  */
26921
26922 static void
26923 cp_parser_objc_class_implementation (cp_parser* parser)
26924 {
26925   tree name, super, categ;
26926   bool is_class_extension;
26927
26928   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
26929   name = cp_parser_identifier (parser);
26930   if (name == error_mark_node)
26931     {
26932       /* It's hard to recover because even if valid @implementation
26933          stuff is to follow, we can't compile it (or validate it) if
26934          we don't even know which class it refers to.  Let's assume
26935          this was a stray '@implementation' token in the stream and
26936          skip it.
26937       */
26938       return;
26939     }
26940   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26941                                          &is_class_extension);
26942
26943   /* We have either a class or a category on our hands.  */
26944   if (categ)
26945     objc_start_category_implementation (name, categ);
26946   else
26947     {
26948       objc_start_class_implementation (name, super);
26949       /* Handle instance variable declarations, if any.  */
26950       cp_parser_objc_class_ivars (parser);
26951       objc_continue_implementation ();
26952     }
26953
26954   cp_parser_objc_method_definition_list (parser);
26955 }
26956
26957 /* Consume the @end token and finish off the implementation.  */
26958
26959 static void
26960 cp_parser_objc_end_implementation (cp_parser* parser)
26961 {
26962   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
26963   objc_finish_implementation ();
26964 }
26965
26966 /* Parse an Objective-C declaration.  */
26967
26968 static void
26969 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26970 {
26971   /* Try to figure out what kind of declaration is present.  */
26972   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26973
26974   if (attributes)
26975     switch (kwd->keyword)
26976       {
26977         case RID_AT_ALIAS:
26978         case RID_AT_CLASS:
26979         case RID_AT_END:
26980           error_at (kwd->location, "attributes may not be specified before"
26981                     " the %<@%D%> Objective-C++ keyword",
26982                     kwd->u.value);
26983           attributes = NULL;
26984           break;
26985         case RID_AT_IMPLEMENTATION:
26986           warning_at (kwd->location, OPT_Wattributes,
26987                       "prefix attributes are ignored before %<@%D%>",
26988                       kwd->u.value);
26989           attributes = NULL;
26990         default:
26991           break;
26992       }
26993
26994   switch (kwd->keyword)
26995     {
26996     case RID_AT_ALIAS:
26997       cp_parser_objc_alias_declaration (parser);
26998       break;
26999     case RID_AT_CLASS:
27000       cp_parser_objc_class_declaration (parser);
27001       break;
27002     case RID_AT_PROTOCOL:
27003       cp_parser_objc_protocol_declaration (parser, attributes);
27004       break;
27005     case RID_AT_INTERFACE:
27006       cp_parser_objc_class_interface (parser, attributes);
27007       break;
27008     case RID_AT_IMPLEMENTATION:
27009       cp_parser_objc_class_implementation (parser);
27010       break;
27011     case RID_AT_END:
27012       cp_parser_objc_end_implementation (parser);
27013       break;
27014     default:
27015       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27016                 kwd->u.value);
27017       cp_parser_skip_to_end_of_block_or_statement (parser);
27018     }
27019 }
27020
27021 /* Parse an Objective-C try-catch-finally statement.
27022
27023    objc-try-catch-finally-stmt:
27024      @try compound-statement objc-catch-clause-seq [opt]
27025        objc-finally-clause [opt]
27026
27027    objc-catch-clause-seq:
27028      objc-catch-clause objc-catch-clause-seq [opt]
27029
27030    objc-catch-clause:
27031      @catch ( objc-exception-declaration ) compound-statement
27032
27033    objc-finally-clause:
27034      @finally compound-statement
27035
27036    objc-exception-declaration:
27037      parameter-declaration
27038      '...'
27039
27040    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
27041
27042    Returns NULL_TREE.
27043
27044    PS: This function is identical to c_parser_objc_try_catch_finally_statement
27045    for C.  Keep them in sync.  */   
27046
27047 static tree
27048 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
27049 {
27050   location_t location;
27051   tree stmt;
27052
27053   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
27054   location = cp_lexer_peek_token (parser->lexer)->location;
27055   objc_maybe_warn_exceptions (location);
27056   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
27057      node, lest it get absorbed into the surrounding block.  */
27058   stmt = push_stmt_list ();
27059   cp_parser_compound_statement (parser, NULL, false, false);
27060   objc_begin_try_stmt (location, pop_stmt_list (stmt));
27061
27062   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
27063     {
27064       cp_parameter_declarator *parm;
27065       tree parameter_declaration = error_mark_node;
27066       bool seen_open_paren = false;
27067
27068       cp_lexer_consume_token (parser->lexer);
27069       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27070         seen_open_paren = true;
27071       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27072         {
27073           /* We have "@catch (...)" (where the '...' are literally
27074              what is in the code).  Skip the '...'.
27075              parameter_declaration is set to NULL_TREE, and
27076              objc_being_catch_clauses() knows that that means
27077              '...'.  */
27078           cp_lexer_consume_token (parser->lexer);
27079           parameter_declaration = NULL_TREE;
27080         }
27081       else
27082         {
27083           /* We have "@catch (NSException *exception)" or something
27084              like that.  Parse the parameter declaration.  */
27085           parm = cp_parser_parameter_declaration (parser, false, NULL);
27086           if (parm == NULL)
27087             parameter_declaration = error_mark_node;
27088           else
27089             parameter_declaration = grokdeclarator (parm->declarator,
27090                                                     &parm->decl_specifiers,
27091                                                     PARM, /*initialized=*/0,
27092                                                     /*attrlist=*/NULL);
27093         }
27094       if (seen_open_paren)
27095         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27096       else
27097         {
27098           /* If there was no open parenthesis, we are recovering from
27099              an error, and we are trying to figure out what mistake
27100              the user has made.  */
27101
27102           /* If there is an immediate closing parenthesis, the user
27103              probably forgot the opening one (ie, they typed "@catch
27104              NSException *e)".  Parse the closing parenthesis and keep
27105              going.  */
27106           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
27107             cp_lexer_consume_token (parser->lexer);
27108           
27109           /* If these is no immediate closing parenthesis, the user
27110              probably doesn't know that parenthesis are required at
27111              all (ie, they typed "@catch NSException *e").  So, just
27112              forget about the closing parenthesis and keep going.  */
27113         }
27114       objc_begin_catch_clause (parameter_declaration);
27115       cp_parser_compound_statement (parser, NULL, false, false);
27116       objc_finish_catch_clause ();
27117     }
27118   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
27119     {
27120       cp_lexer_consume_token (parser->lexer);
27121       location = cp_lexer_peek_token (parser->lexer)->location;
27122       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
27123          node, lest it get absorbed into the surrounding block.  */
27124       stmt = push_stmt_list ();
27125       cp_parser_compound_statement (parser, NULL, false, false);
27126       objc_build_finally_clause (location, pop_stmt_list (stmt));
27127     }
27128
27129   return objc_finish_try_stmt ();
27130 }
27131
27132 /* Parse an Objective-C synchronized statement.
27133
27134    objc-synchronized-stmt:
27135      @synchronized ( expression ) compound-statement
27136
27137    Returns NULL_TREE.  */
27138
27139 static tree
27140 cp_parser_objc_synchronized_statement (cp_parser *parser)
27141 {
27142   location_t location;
27143   tree lock, stmt;
27144
27145   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
27146
27147   location = cp_lexer_peek_token (parser->lexer)->location;
27148   objc_maybe_warn_exceptions (location);
27149   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27150   lock = cp_parser_expression (parser);
27151   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27152
27153   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
27154      node, lest it get absorbed into the surrounding block.  */
27155   stmt = push_stmt_list ();
27156   cp_parser_compound_statement (parser, NULL, false, false);
27157
27158   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
27159 }
27160
27161 /* Parse an Objective-C throw statement.
27162
27163    objc-throw-stmt:
27164      @throw assignment-expression [opt] ;
27165
27166    Returns a constructed '@throw' statement.  */
27167
27168 static tree
27169 cp_parser_objc_throw_statement (cp_parser *parser)
27170 {
27171   tree expr = NULL_TREE;
27172   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27173
27174   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
27175
27176   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27177     expr = cp_parser_expression (parser);
27178
27179   cp_parser_consume_semicolon_at_end_of_statement (parser);
27180
27181   return objc_build_throw_stmt (loc, expr);
27182 }
27183
27184 /* Parse an Objective-C statement.  */
27185
27186 static tree
27187 cp_parser_objc_statement (cp_parser * parser)
27188 {
27189   /* Try to figure out what kind of declaration is present.  */
27190   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27191
27192   switch (kwd->keyword)
27193     {
27194     case RID_AT_TRY:
27195       return cp_parser_objc_try_catch_finally_statement (parser);
27196     case RID_AT_SYNCHRONIZED:
27197       return cp_parser_objc_synchronized_statement (parser);
27198     case RID_AT_THROW:
27199       return cp_parser_objc_throw_statement (parser);
27200     default:
27201       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
27202                kwd->u.value);
27203       cp_parser_skip_to_end_of_block_or_statement (parser);
27204     }
27205
27206   return error_mark_node;
27207 }
27208
27209 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
27210    look ahead to see if an objc keyword follows the attributes.  This
27211    is to detect the use of prefix attributes on ObjC @interface and 
27212    @protocol.  */
27213
27214 static bool
27215 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
27216 {
27217   cp_lexer_save_tokens (parser->lexer);
27218   *attrib = cp_parser_attributes_opt (parser);
27219   gcc_assert (*attrib);
27220   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
27221     {
27222       cp_lexer_commit_tokens (parser->lexer);
27223       return true;
27224     }
27225   cp_lexer_rollback_tokens (parser->lexer);
27226   return false;  
27227 }
27228
27229 /* This routine is a minimal replacement for
27230    c_parser_struct_declaration () used when parsing the list of
27231    types/names or ObjC++ properties.  For example, when parsing the
27232    code
27233
27234    @property (readonly) int a, b, c;
27235
27236    this function is responsible for parsing "int a, int b, int c" and
27237    returning the declarations as CHAIN of DECLs.
27238
27239    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
27240    similar parsing.  */
27241 static tree
27242 cp_parser_objc_struct_declaration (cp_parser *parser)
27243 {
27244   tree decls = NULL_TREE;
27245   cp_decl_specifier_seq declspecs;
27246   int decl_class_or_enum_p;
27247   tree prefix_attributes;
27248
27249   cp_parser_decl_specifier_seq (parser,
27250                                 CP_PARSER_FLAGS_NONE,
27251                                 &declspecs,
27252                                 &decl_class_or_enum_p);
27253
27254   if (declspecs.type == error_mark_node)
27255     return error_mark_node;
27256
27257   /* auto, register, static, extern, mutable.  */
27258   if (declspecs.storage_class != sc_none)
27259     {
27260       cp_parser_error (parser, "invalid type for property");
27261       declspecs.storage_class = sc_none;
27262     }
27263   
27264   /* thread_local.  */
27265   if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
27266     {
27267       cp_parser_error (parser, "invalid type for property");
27268       declspecs.locations[ds_thread] = 0;
27269     }
27270   
27271   /* typedef.  */
27272   if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
27273     {
27274       cp_parser_error (parser, "invalid type for property");
27275       declspecs.locations[ds_typedef] = 0;
27276     }
27277
27278   prefix_attributes = declspecs.attributes;
27279   declspecs.attributes = NULL_TREE;
27280
27281   /* Keep going until we hit the `;' at the end of the declaration. */
27282   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27283     {
27284       tree attributes, first_attribute, decl;
27285       cp_declarator *declarator;
27286       cp_token *token;
27287
27288       /* Parse the declarator.  */
27289       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27290                                          NULL, NULL, false, false);
27291
27292       /* Look for attributes that apply to the ivar.  */
27293       attributes = cp_parser_attributes_opt (parser);
27294       /* Remember which attributes are prefix attributes and
27295          which are not.  */
27296       first_attribute = attributes;
27297       /* Combine the attributes.  */
27298       attributes = chainon (prefix_attributes, attributes);
27299       
27300       decl = grokfield (declarator, &declspecs,
27301                         NULL_TREE, /*init_const_expr_p=*/false,
27302                         NULL_TREE, attributes);
27303
27304       if (decl == error_mark_node || decl == NULL_TREE)
27305         return error_mark_node;
27306       
27307       /* Reset PREFIX_ATTRIBUTES.  */
27308       while (attributes && TREE_CHAIN (attributes) != first_attribute)
27309         attributes = TREE_CHAIN (attributes);
27310       if (attributes)
27311         TREE_CHAIN (attributes) = NULL_TREE;
27312
27313       DECL_CHAIN (decl) = decls;
27314       decls = decl;
27315
27316       token = cp_lexer_peek_token (parser->lexer);
27317       if (token->type == CPP_COMMA)
27318         {
27319           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
27320           continue;
27321         }
27322       else
27323         break;
27324     }
27325   return decls;
27326 }
27327
27328 /* Parse an Objective-C @property declaration.  The syntax is:
27329
27330    objc-property-declaration:
27331      '@property' objc-property-attributes[opt] struct-declaration ;
27332
27333    objc-property-attributes:
27334     '(' objc-property-attribute-list ')'
27335
27336    objc-property-attribute-list:
27337      objc-property-attribute
27338      objc-property-attribute-list, objc-property-attribute
27339
27340    objc-property-attribute
27341      'getter' = identifier
27342      'setter' = identifier
27343      'readonly'
27344      'readwrite'
27345      'assign'
27346      'retain'
27347      'copy'
27348      'nonatomic'
27349
27350   For example:
27351     @property NSString *name;
27352     @property (readonly) id object;
27353     @property (retain, nonatomic, getter=getTheName) id name;
27354     @property int a, b, c;
27355
27356    PS: This function is identical to
27357    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
27358 static void 
27359 cp_parser_objc_at_property_declaration (cp_parser *parser)
27360 {
27361   /* The following variables hold the attributes of the properties as
27362      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
27363      seen.  When we see an attribute, we set them to 'true' (if they
27364      are boolean properties) or to the identifier (if they have an
27365      argument, ie, for getter and setter).  Note that here we only
27366      parse the list of attributes, check the syntax and accumulate the
27367      attributes that we find.  objc_add_property_declaration() will
27368      then process the information.  */
27369   bool property_assign = false;
27370   bool property_copy = false;
27371   tree property_getter_ident = NULL_TREE;
27372   bool property_nonatomic = false;
27373   bool property_readonly = false;
27374   bool property_readwrite = false;
27375   bool property_retain = false;
27376   tree property_setter_ident = NULL_TREE;
27377
27378   /* 'properties' is the list of properties that we read.  Usually a
27379      single one, but maybe more (eg, in "@property int a, b, c;" there
27380      are three).  */
27381   tree properties;
27382   location_t loc;
27383
27384   loc = cp_lexer_peek_token (parser->lexer)->location;
27385
27386   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
27387
27388   /* Parse the optional attribute list...  */
27389   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27390     {
27391       /* Eat the '('.  */
27392       cp_lexer_consume_token (parser->lexer);
27393
27394       while (true)
27395         {
27396           bool syntax_error = false;
27397           cp_token *token = cp_lexer_peek_token (parser->lexer);
27398           enum rid keyword;
27399
27400           if (token->type != CPP_NAME)
27401             {
27402               cp_parser_error (parser, "expected identifier");
27403               break;
27404             }
27405           keyword = C_RID_CODE (token->u.value);
27406           cp_lexer_consume_token (parser->lexer);
27407           switch (keyword)
27408             {
27409             case RID_ASSIGN:    property_assign = true;    break;
27410             case RID_COPY:      property_copy = true;      break;
27411             case RID_NONATOMIC: property_nonatomic = true; break;
27412             case RID_READONLY:  property_readonly = true;  break;
27413             case RID_READWRITE: property_readwrite = true; break;
27414             case RID_RETAIN:    property_retain = true;    break;
27415
27416             case RID_GETTER:
27417             case RID_SETTER:
27418               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27419                 {
27420                   if (keyword == RID_GETTER)
27421                     cp_parser_error (parser,
27422                                      "missing %<=%> (after %<getter%> attribute)");
27423                   else
27424                     cp_parser_error (parser,
27425                                      "missing %<=%> (after %<setter%> attribute)");
27426                   syntax_error = true;
27427                   break;
27428                 }
27429               cp_lexer_consume_token (parser->lexer); /* eat the = */
27430               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27431                 {
27432                   cp_parser_error (parser, "expected identifier");
27433                   syntax_error = true;
27434                   break;
27435                 }
27436               if (keyword == RID_SETTER)
27437                 {
27438                   if (property_setter_ident != NULL_TREE)
27439                     {
27440                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27441                       cp_lexer_consume_token (parser->lexer);
27442                     }
27443                   else
27444                     property_setter_ident = cp_parser_objc_selector (parser);
27445                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27446                     cp_parser_error (parser, "setter name must terminate with %<:%>");
27447                   else
27448                     cp_lexer_consume_token (parser->lexer);
27449                 }
27450               else
27451                 {
27452                   if (property_getter_ident != NULL_TREE)
27453                     {
27454                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27455                       cp_lexer_consume_token (parser->lexer);
27456                     }
27457                   else
27458                     property_getter_ident = cp_parser_objc_selector (parser);
27459                 }
27460               break;
27461             default:
27462               cp_parser_error (parser, "unknown property attribute");
27463               syntax_error = true;
27464               break;
27465             }
27466
27467           if (syntax_error)
27468             break;
27469
27470           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27471             cp_lexer_consume_token (parser->lexer);
27472           else
27473             break;
27474         }
27475
27476       /* FIXME: "@property (setter, assign);" will generate a spurious
27477          "error: expected â€˜)’ before â€˜,’ token".  This is because
27478          cp_parser_require, unlike the C counterpart, will produce an
27479          error even if we are in error recovery.  */
27480       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27481         {
27482           cp_parser_skip_to_closing_parenthesis (parser,
27483                                                  /*recovering=*/true,
27484                                                  /*or_comma=*/false,
27485                                                  /*consume_paren=*/true);
27486         }
27487     }
27488
27489   /* ... and the property declaration(s).  */
27490   properties = cp_parser_objc_struct_declaration (parser);
27491
27492   if (properties == error_mark_node)
27493     {
27494       cp_parser_skip_to_end_of_statement (parser);
27495       /* If the next token is now a `;', consume it.  */
27496       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27497         cp_lexer_consume_token (parser->lexer);
27498       return;
27499     }
27500
27501   if (properties == NULL_TREE)
27502     cp_parser_error (parser, "expected identifier");
27503   else
27504     {
27505       /* Comma-separated properties are chained together in
27506          reverse order; add them one by one.  */
27507       properties = nreverse (properties);
27508       
27509       for (; properties; properties = TREE_CHAIN (properties))
27510         objc_add_property_declaration (loc, copy_node (properties),
27511                                        property_readonly, property_readwrite,
27512                                        property_assign, property_retain,
27513                                        property_copy, property_nonatomic,
27514                                        property_getter_ident, property_setter_ident);
27515     }
27516   
27517   cp_parser_consume_semicolon_at_end_of_statement (parser);
27518 }
27519
27520 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
27521
27522    objc-synthesize-declaration:
27523      @synthesize objc-synthesize-identifier-list ;
27524
27525    objc-synthesize-identifier-list:
27526      objc-synthesize-identifier
27527      objc-synthesize-identifier-list, objc-synthesize-identifier
27528
27529    objc-synthesize-identifier
27530      identifier
27531      identifier = identifier
27532
27533   For example:
27534     @synthesize MyProperty;
27535     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27536
27537   PS: This function is identical to c_parser_objc_at_synthesize_declaration
27538   for C.  Keep them in sync.
27539 */
27540 static void 
27541 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27542 {
27543   tree list = NULL_TREE;
27544   location_t loc;
27545   loc = cp_lexer_peek_token (parser->lexer)->location;
27546
27547   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
27548   while (true)
27549     {
27550       tree property, ivar;
27551       property = cp_parser_identifier (parser);
27552       if (property == error_mark_node)
27553         {
27554           cp_parser_consume_semicolon_at_end_of_statement (parser);
27555           return;
27556         }
27557       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27558         {
27559           cp_lexer_consume_token (parser->lexer);
27560           ivar = cp_parser_identifier (parser);
27561           if (ivar == error_mark_node)
27562             {
27563               cp_parser_consume_semicolon_at_end_of_statement (parser);
27564               return;
27565             }
27566         }
27567       else
27568         ivar = NULL_TREE;
27569       list = chainon (list, build_tree_list (ivar, property));
27570       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27571         cp_lexer_consume_token (parser->lexer);
27572       else
27573         break;
27574     }
27575   cp_parser_consume_semicolon_at_end_of_statement (parser);
27576   objc_add_synthesize_declaration (loc, list);
27577 }
27578
27579 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
27580
27581    objc-dynamic-declaration:
27582      @dynamic identifier-list ;
27583
27584    For example:
27585      @dynamic MyProperty;
27586      @dynamic MyProperty, AnotherProperty;
27587
27588   PS: This function is identical to c_parser_objc_at_dynamic_declaration
27589   for C.  Keep them in sync.
27590 */
27591 static void 
27592 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27593 {
27594   tree list = NULL_TREE;
27595   location_t loc;
27596   loc = cp_lexer_peek_token (parser->lexer)->location;
27597
27598   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
27599   while (true)
27600     {
27601       tree property;
27602       property = cp_parser_identifier (parser);
27603       if (property == error_mark_node)
27604         {
27605           cp_parser_consume_semicolon_at_end_of_statement (parser);
27606           return;
27607         }
27608       list = chainon (list, build_tree_list (NULL, property));
27609       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27610         cp_lexer_consume_token (parser->lexer);
27611       else
27612         break;
27613     }
27614   cp_parser_consume_semicolon_at_end_of_statement (parser);
27615   objc_add_dynamic_declaration (loc, list);
27616 }
27617
27618 \f
27619 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines.  */
27620
27621 /* Returns name of the next clause.
27622    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27623    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
27624    returned and the token is consumed.  */
27625
27626 static pragma_omp_clause
27627 cp_parser_omp_clause_name (cp_parser *parser)
27628 {
27629   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27630
27631   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27632     result = PRAGMA_OMP_CLAUSE_IF;
27633   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27634     result = PRAGMA_OMP_CLAUSE_DEFAULT;
27635   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
27636     result = PRAGMA_OACC_CLAUSE_DELETE;
27637   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27638     result = PRAGMA_OMP_CLAUSE_PRIVATE;
27639   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27640     result = PRAGMA_OMP_CLAUSE_FOR;
27641   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27642     {
27643       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27644       const char *p = IDENTIFIER_POINTER (id);
27645
27646       switch (p[0])
27647         {
27648         case 'a':
27649           if (!strcmp ("aligned", p))
27650             result = PRAGMA_OMP_CLAUSE_ALIGNED;
27651           else if (!strcmp ("async", p))
27652             result = PRAGMA_OACC_CLAUSE_ASYNC;
27653           break;
27654         case 'c':
27655           if (!strcmp ("collapse", p))
27656             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27657           else if (!strcmp ("copy", p))
27658             result = PRAGMA_OACC_CLAUSE_COPY;
27659           else if (!strcmp ("copyin", p))
27660             result = PRAGMA_OMP_CLAUSE_COPYIN;
27661           else if (!strcmp ("copyout", p))
27662             result = PRAGMA_OACC_CLAUSE_COPYOUT;
27663           else if (!strcmp ("copyprivate", p))
27664             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27665           else if (!strcmp ("create", p))
27666             result = PRAGMA_OACC_CLAUSE_CREATE;
27667           break;
27668         case 'd':
27669           if (!strcmp ("depend", p))
27670             result = PRAGMA_OMP_CLAUSE_DEPEND;
27671           else if (!strcmp ("device", p))
27672             result = PRAGMA_OMP_CLAUSE_DEVICE;
27673           else if (!strcmp ("deviceptr", p))
27674             result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
27675           else if (!strcmp ("dist_schedule", p))
27676             result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27677           break;
27678         case 'f':
27679           if (!strcmp ("final", p))
27680             result = PRAGMA_OMP_CLAUSE_FINAL;
27681           else if (!strcmp ("firstprivate", p))
27682             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27683           else if (!strcmp ("from", p))
27684             result = PRAGMA_OMP_CLAUSE_FROM;
27685           break;
27686         case 'h':
27687           if (!strcmp ("host", p))
27688             result = PRAGMA_OACC_CLAUSE_HOST;
27689           break;
27690         case 'i':
27691           if (!strcmp ("inbranch", p))
27692             result = PRAGMA_OMP_CLAUSE_INBRANCH;
27693           break;
27694         case 'l':
27695           if (!strcmp ("lastprivate", p))
27696             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27697           else if (!strcmp ("linear", p))
27698             result = PRAGMA_OMP_CLAUSE_LINEAR;
27699           break;
27700         case 'm':
27701           if (!strcmp ("map", p))
27702             result = PRAGMA_OMP_CLAUSE_MAP;
27703           else if (!strcmp ("mergeable", p))
27704             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27705           else if (flag_cilkplus && !strcmp ("mask", p))
27706             result = PRAGMA_CILK_CLAUSE_MASK;
27707           break;
27708         case 'n':
27709           if (!strcmp ("notinbranch", p))
27710             result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27711           else if (!strcmp ("nowait", p))
27712             result = PRAGMA_OMP_CLAUSE_NOWAIT;
27713           else if (flag_cilkplus && !strcmp ("nomask", p))
27714             result = PRAGMA_CILK_CLAUSE_NOMASK;
27715           else if (!strcmp ("num_gangs", p))
27716             result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
27717           else if (!strcmp ("num_teams", p))
27718             result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27719           else if (!strcmp ("num_threads", p))
27720             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27721           else if (!strcmp ("num_workers", p))
27722             result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
27723           break;
27724         case 'o':
27725           if (!strcmp ("ordered", p))
27726             result = PRAGMA_OMP_CLAUSE_ORDERED;
27727           break;
27728         case 'p':
27729           if (!strcmp ("parallel", p))
27730             result = PRAGMA_OMP_CLAUSE_PARALLEL;
27731           else if (!strcmp ("present", p))
27732             result = PRAGMA_OACC_CLAUSE_PRESENT;
27733           else if (!strcmp ("present_or_copy", p)
27734                    || !strcmp ("pcopy", p))
27735             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
27736           else if (!strcmp ("present_or_copyin", p)
27737                    || !strcmp ("pcopyin", p))
27738             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
27739           else if (!strcmp ("present_or_copyout", p)
27740                    || !strcmp ("pcopyout", p))
27741             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
27742           else if (!strcmp ("present_or_create", p)
27743                    || !strcmp ("pcreate", p))
27744             result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
27745           else if (!strcmp ("proc_bind", p))
27746             result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27747           break;
27748         case 'r':
27749           if (!strcmp ("reduction", p))
27750             result = PRAGMA_OMP_CLAUSE_REDUCTION;
27751           break;
27752         case 's':
27753           if (!strcmp ("safelen", p))
27754             result = PRAGMA_OMP_CLAUSE_SAFELEN;
27755           else if (!strcmp ("schedule", p))
27756             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27757           else if (!strcmp ("sections", p))
27758             result = PRAGMA_OMP_CLAUSE_SECTIONS;
27759           else if (!strcmp ("self", p))
27760             result = PRAGMA_OACC_CLAUSE_SELF;
27761           else if (!strcmp ("shared", p))
27762             result = PRAGMA_OMP_CLAUSE_SHARED;
27763           else if (!strcmp ("simdlen", p))
27764             result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27765           break;
27766         case 't':
27767           if (!strcmp ("taskgroup", p))
27768             result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27769           else if (!strcmp ("thread_limit", p))
27770             result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27771           else if (!strcmp ("to", p))
27772             result = PRAGMA_OMP_CLAUSE_TO;
27773           break;
27774         case 'u':
27775           if (!strcmp ("uniform", p))
27776             result = PRAGMA_OMP_CLAUSE_UNIFORM;
27777           else if (!strcmp ("untied", p))
27778             result = PRAGMA_OMP_CLAUSE_UNTIED;
27779           break;
27780         case 'v':
27781           if (!strcmp ("vector_length", p))
27782             result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
27783           else if (flag_cilkplus && !strcmp ("vectorlength", p))
27784             result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27785           break;
27786         case 'w':
27787           if (!strcmp ("wait", p))
27788             result = PRAGMA_OACC_CLAUSE_WAIT;
27789           break;
27790         }
27791     }
27792
27793   if (result != PRAGMA_OMP_CLAUSE_NONE)
27794     cp_lexer_consume_token (parser->lexer);
27795
27796   return result;
27797 }
27798
27799 /* Validate that a clause of the given type does not already exist.  */
27800
27801 static void
27802 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27803                            const char *name, location_t location)
27804 {
27805   tree c;
27806
27807   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27808     if (OMP_CLAUSE_CODE (c) == code)
27809       {
27810         error_at (location, "too many %qs clauses", name);
27811         break;
27812       }
27813 }
27814
27815 /* OpenMP 2.5:
27816    variable-list:
27817      identifier
27818      variable-list , identifier
27819
27820    In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27821    colon).  An opening parenthesis will have been consumed by the caller.
27822
27823    If KIND is nonzero, create the appropriate node and install the decl
27824    in OMP_CLAUSE_DECL and add the node to the head of the list.
27825
27826    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27827    return the list created.
27828
27829    COLON can be NULL if only closing parenthesis should end the list,
27830    or pointer to bool which will receive false if the list is terminated
27831    by closing parenthesis or true if the list is terminated by colon.  */
27832
27833 static tree
27834 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27835                                 tree list, bool *colon)
27836 {
27837   cp_token *token;
27838   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27839   if (colon)
27840     {
27841       parser->colon_corrects_to_scope_p = false;
27842       *colon = false;
27843     }
27844   while (1)
27845     {
27846       tree name, decl;
27847
27848       token = cp_lexer_peek_token (parser->lexer);
27849       name = cp_parser_id_expression (parser, /*template_p=*/false,
27850                                       /*check_dependency_p=*/true,
27851                                       /*template_p=*/NULL,
27852                                       /*declarator_p=*/false,
27853                                       /*optional_p=*/false);
27854       if (name == error_mark_node)
27855         goto skip_comma;
27856
27857       decl = cp_parser_lookup_name_simple (parser, name, token->location);
27858       if (decl == error_mark_node)
27859         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27860                                      token->location);
27861       else if (kind != 0)
27862         {
27863           switch (kind)
27864             {
27865             case OMP_CLAUSE__CACHE_:
27866               if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
27867                 {
27868                   error_at (token->location, "expected %<[%>");
27869                   decl = error_mark_node;
27870                   break;
27871                 }
27872               /* FALL THROUGH.  */
27873             case OMP_CLAUSE_MAP:
27874             case OMP_CLAUSE_FROM:
27875             case OMP_CLAUSE_TO:
27876             case OMP_CLAUSE_DEPEND:
27877               while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27878                 {
27879                   tree low_bound = NULL_TREE, length = NULL_TREE;
27880
27881                   parser->colon_corrects_to_scope_p = false;
27882                   cp_lexer_consume_token (parser->lexer);
27883                   if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27884                     low_bound = cp_parser_expression (parser);
27885                   if (!colon)
27886                     parser->colon_corrects_to_scope_p
27887                       = saved_colon_corrects_to_scope_p;
27888                   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27889                     length = integer_one_node;
27890                   else
27891                     {
27892                       /* Look for `:'.  */
27893                       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27894                         goto skip_comma;
27895                       if (!cp_lexer_next_token_is (parser->lexer,
27896                                                    CPP_CLOSE_SQUARE))
27897                         length = cp_parser_expression (parser);
27898                     }
27899                   /* Look for the closing `]'.  */
27900                   if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27901                                           RT_CLOSE_SQUARE))
27902                     goto skip_comma;
27903
27904                   if (kind == OMP_CLAUSE__CACHE_)
27905                     {
27906                       if (TREE_CODE (low_bound) != INTEGER_CST
27907                           && !TREE_READONLY (low_bound))
27908                         {
27909                           error_at (token->location,
27910                                         "%qD is not a constant", low_bound);
27911                           decl = error_mark_node;
27912                         }
27913
27914                       if (TREE_CODE (length) != INTEGER_CST
27915                           && !TREE_READONLY (length))
27916                         {
27917                           error_at (token->location,
27918                                         "%qD is not a constant", length);
27919                           decl = error_mark_node;
27920                         }
27921                     }
27922
27923                   decl = tree_cons (low_bound, length, decl);
27924                 }
27925               break;
27926             default:
27927               break;
27928             }
27929
27930           tree u = build_omp_clause (token->location, kind);
27931           OMP_CLAUSE_DECL (u) = decl;
27932           OMP_CLAUSE_CHAIN (u) = list;
27933           list = u;
27934         }
27935       else
27936         list = tree_cons (decl, NULL_TREE, list);
27937
27938     get_comma:
27939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27940         break;
27941       cp_lexer_consume_token (parser->lexer);
27942     }
27943
27944   if (colon)
27945     parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27946
27947   if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27948     {
27949       *colon = true;
27950       cp_parser_require (parser, CPP_COLON, RT_COLON);
27951       return list;
27952     }
27953
27954   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27955     {
27956       int ending;
27957
27958       /* Try to resync to an unnested comma.  Copied from
27959          cp_parser_parenthesized_expression_list.  */
27960     skip_comma:
27961       if (colon)
27962         parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27963       ending = cp_parser_skip_to_closing_parenthesis (parser,
27964                                                       /*recovering=*/true,
27965                                                       /*or_comma=*/true,
27966                                                       /*consume_paren=*/true);
27967       if (ending < 0)
27968         goto get_comma;
27969     }
27970
27971   return list;
27972 }
27973
27974 /* Similarly, but expect leading and trailing parenthesis.  This is a very
27975    common case for omp clauses.  */
27976
27977 static tree
27978 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27979 {
27980   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27981     return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27982   return list;
27983 }
27984
27985 /* OpenACC 2.0:
27986    copy ( variable-list )
27987    copyin ( variable-list )
27988    copyout ( variable-list )
27989    create ( variable-list )
27990    delete ( variable-list )
27991    present ( variable-list )
27992    present_or_copy ( variable-list )
27993      pcopy ( variable-list )
27994    present_or_copyin ( variable-list )
27995      pcopyin ( variable-list )
27996    present_or_copyout ( variable-list )
27997      pcopyout ( variable-list )
27998    present_or_create ( variable-list )
27999      pcreate ( variable-list ) */
28000
28001 static tree
28002 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
28003                             tree list)
28004 {
28005   enum gomp_map_kind kind;
28006   switch (c_kind)
28007     {
28008     case PRAGMA_OACC_CLAUSE_COPY:
28009       kind = GOMP_MAP_FORCE_TOFROM;
28010       break;
28011     case PRAGMA_OACC_CLAUSE_COPYIN:
28012       kind = GOMP_MAP_FORCE_TO;
28013       break;
28014     case PRAGMA_OACC_CLAUSE_COPYOUT:
28015       kind = GOMP_MAP_FORCE_FROM;
28016       break;
28017     case PRAGMA_OACC_CLAUSE_CREATE:
28018       kind = GOMP_MAP_FORCE_ALLOC;
28019       break;
28020     case PRAGMA_OACC_CLAUSE_DELETE:
28021       kind = GOMP_MAP_FORCE_DEALLOC;
28022       break;
28023     case PRAGMA_OACC_CLAUSE_DEVICE:
28024       kind = GOMP_MAP_FORCE_TO;
28025       break;
28026     case PRAGMA_OACC_CLAUSE_HOST:
28027     case PRAGMA_OACC_CLAUSE_SELF:
28028       kind = GOMP_MAP_FORCE_FROM;
28029       break;
28030     case PRAGMA_OACC_CLAUSE_PRESENT:
28031       kind = GOMP_MAP_FORCE_PRESENT;
28032       break;
28033     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
28034       kind = GOMP_MAP_TOFROM;
28035       break;
28036     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
28037       kind = GOMP_MAP_TO;
28038       break;
28039     case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
28040       kind = GOMP_MAP_FROM;
28041       break;
28042     case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
28043       kind = GOMP_MAP_ALLOC;
28044       break;
28045     default:
28046       gcc_unreachable ();
28047     }
28048   tree nl, c;
28049   nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
28050
28051   for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
28052     OMP_CLAUSE_SET_MAP_KIND (c, kind);
28053
28054   return nl;
28055 }
28056
28057 /* OpenACC 2.0:
28058    deviceptr ( variable-list ) */
28059
28060 static tree
28061 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
28062 {
28063   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28064   tree vars, t;
28065
28066   /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
28067      cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
28068      variable-list must only allow for pointer variables.  */
28069   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28070   for (t = vars; t; t = TREE_CHAIN (t))
28071     {
28072       tree v = TREE_PURPOSE (t);
28073
28074       /* FIXME diagnostics: Ideally we should keep individual
28075          locations for all the variables in the var list to make the
28076          following errors more precise.  Perhaps
28077          c_parser_omp_var_list_parens should construct a list of
28078          locations to go along with the var list.  */
28079
28080       if (TREE_CODE (v) != VAR_DECL)
28081         error_at (loc, "%qD is not a variable", v);
28082       else if (TREE_TYPE (v) == error_mark_node)
28083         ;
28084       else if (!POINTER_TYPE_P (TREE_TYPE (v)))
28085         error_at (loc, "%qD is not a pointer variable", v);
28086
28087       tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
28088       OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
28089       OMP_CLAUSE_DECL (u) = v;
28090       OMP_CLAUSE_CHAIN (u) = list;
28091       list = u;
28092     }
28093
28094   return list;
28095 }
28096
28097 /* OpenACC:
28098    vector_length ( expression ) */
28099
28100 static tree
28101 cp_parser_oacc_clause_vector_length (cp_parser *parser, tree list)
28102 {
28103   tree t, c;
28104   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28105   bool error = false;
28106
28107   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28108     return list;
28109
28110   t = cp_parser_condition (parser);
28111   if (t == error_mark_node || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
28112     {
28113       error_at (location, "expected positive integer expression");
28114       error = true;
28115     }
28116
28117   if (error || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28118     {
28119       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28120                                            /*or_comma=*/false,
28121                                            /*consume_paren=*/true);
28122       return list;
28123     }
28124
28125   check_no_duplicate_clause (list, OMP_CLAUSE_VECTOR_LENGTH, "vector_length",
28126                              location);
28127
28128   c = build_omp_clause (location, OMP_CLAUSE_VECTOR_LENGTH);
28129   OMP_CLAUSE_VECTOR_LENGTH_EXPR (c) = t;
28130   OMP_CLAUSE_CHAIN (c) = list;
28131   list = c;
28132
28133   return list;
28134 }
28135
28136 /* OpenACC 2.0
28137    Parse wait clause or directive parameters.  */
28138
28139 static tree
28140 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
28141 {
28142   vec<tree, va_gc> *args;
28143   tree t, args_tree;
28144
28145   args = cp_parser_parenthesized_expression_list (parser, non_attr,
28146                                                   /*cast_p=*/false,
28147                                                   /*allow_expansion_p=*/true,
28148                                                   /*non_constant_p=*/NULL);
28149
28150   if (args == NULL || args->length () == 0)
28151     {
28152       cp_parser_error (parser, "expected integer expression before ')'");
28153       if (args != NULL)
28154         release_tree_vector (args);
28155       return list;
28156     }
28157
28158   args_tree = build_tree_list_vec (args);
28159
28160   release_tree_vector (args);
28161
28162   for (t = args_tree; t; t = TREE_CHAIN (t))
28163     {
28164       tree targ = TREE_VALUE (t);
28165
28166       if (targ != error_mark_node)
28167         {
28168           if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
28169             error ("%<wait%> expression must be integral");
28170           else
28171             {
28172               tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
28173
28174               mark_rvalue_use (targ);
28175               OMP_CLAUSE_DECL (c) = targ;
28176               OMP_CLAUSE_CHAIN (c) = list;
28177               list = c;
28178             }
28179         }
28180     }
28181
28182   return list;
28183 }
28184
28185 /* OpenACC:
28186    wait ( int-expr-list ) */
28187
28188 static tree
28189 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
28190 {
28191   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28192
28193   if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
28194     return list;
28195
28196   list = cp_parser_oacc_wait_list (parser, location, list);
28197
28198   return list;
28199 }
28200
28201 /* OpenMP 3.0:
28202    collapse ( constant-expression ) */
28203
28204 static tree
28205 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
28206 {
28207   tree c, num;
28208   location_t loc;
28209   HOST_WIDE_INT n;
28210
28211   loc = cp_lexer_peek_token (parser->lexer)->location;
28212   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28213     return list;
28214
28215   num = cp_parser_constant_expression (parser);
28216
28217   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28218     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28219                                            /*or_comma=*/false,
28220                                            /*consume_paren=*/true);
28221
28222   if (num == error_mark_node)
28223     return list;
28224   num = fold_non_dependent_expr (num);
28225   if (!tree_fits_shwi_p (num)
28226       || !INTEGRAL_TYPE_P (TREE_TYPE (num))
28227       || (n = tree_to_shwi (num)) <= 0
28228       || (int) n != n)
28229     {
28230       error_at (loc, "collapse argument needs positive constant integer expression");
28231       return list;
28232     }
28233
28234   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
28235   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
28236   OMP_CLAUSE_CHAIN (c) = list;
28237   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
28238
28239   return c;
28240 }
28241
28242 /* OpenMP 2.5:
28243    default ( shared | none ) */
28244
28245 static tree
28246 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
28247 {
28248   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
28249   tree c;
28250
28251   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28252     return list;
28253   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28254     {
28255       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28256       const char *p = IDENTIFIER_POINTER (id);
28257
28258       switch (p[0])
28259         {
28260         case 'n':
28261           if (strcmp ("none", p) != 0)
28262             goto invalid_kind;
28263           kind = OMP_CLAUSE_DEFAULT_NONE;
28264           break;
28265
28266         case 's':
28267           if (strcmp ("shared", p) != 0)
28268             goto invalid_kind;
28269           kind = OMP_CLAUSE_DEFAULT_SHARED;
28270           break;
28271
28272         default:
28273           goto invalid_kind;
28274         }
28275
28276       cp_lexer_consume_token (parser->lexer);
28277     }
28278   else
28279     {
28280     invalid_kind:
28281       cp_parser_error (parser, "expected %<none%> or %<shared%>");
28282     }
28283
28284   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28285     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28286                                            /*or_comma=*/false,
28287                                            /*consume_paren=*/true);
28288
28289   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
28290     return list;
28291
28292   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
28293   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
28294   OMP_CLAUSE_CHAIN (c) = list;
28295   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
28296
28297   return c;
28298 }
28299
28300 /* OpenMP 3.1:
28301    final ( expression ) */
28302
28303 static tree
28304 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
28305 {
28306   tree t, c;
28307
28308   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28309     return list;
28310
28311   t = cp_parser_condition (parser);
28312
28313   if (t == error_mark_node
28314       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28315     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28316                                            /*or_comma=*/false,
28317                                            /*consume_paren=*/true);
28318
28319   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
28320
28321   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
28322   OMP_CLAUSE_FINAL_EXPR (c) = t;
28323   OMP_CLAUSE_CHAIN (c) = list;
28324
28325   return c;
28326 }
28327
28328 /* OpenMP 2.5:
28329    if ( expression ) */
28330
28331 static tree
28332 cp_parser_omp_clause_if (cp_parser *parser, tree list, 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_condition (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_IF, "if", location);
28348
28349   c = build_omp_clause (location, OMP_CLAUSE_IF);
28350   OMP_CLAUSE_IF_EXPR (c) = t;
28351   OMP_CLAUSE_CHAIN (c) = list;
28352
28353   return c;
28354 }
28355
28356 /* OpenMP 3.1:
28357    mergeable */
28358
28359 static tree
28360 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
28361                                 tree list, location_t location)
28362 {
28363   tree c;
28364
28365   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
28366                              location);
28367
28368   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
28369   OMP_CLAUSE_CHAIN (c) = list;
28370   return c;
28371 }
28372
28373 /* OpenMP 2.5:
28374    nowait */
28375
28376 static tree
28377 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
28378                              tree list, location_t location)
28379 {
28380   tree c;
28381
28382   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
28383
28384   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
28385   OMP_CLAUSE_CHAIN (c) = list;
28386   return c;
28387 }
28388
28389 /* OpenACC:
28390    num_gangs ( expression ) */
28391
28392 static tree
28393 cp_parser_omp_clause_num_gangs (cp_parser *parser, tree list)
28394 {
28395   tree t, c;
28396   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28397
28398   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28399     return list;
28400
28401   t = cp_parser_condition (parser);
28402
28403   if (t == error_mark_node
28404       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28405     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28406                                            /*or_comma=*/false,
28407                                            /*consume_paren=*/true);
28408
28409   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28410     {
28411       error_at (location, "expected positive integer expression");
28412       return list;
28413     }
28414
28415   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_GANGS, "num_gangs", location);
28416
28417   c = build_omp_clause (location, OMP_CLAUSE_NUM_GANGS);
28418   OMP_CLAUSE_NUM_GANGS_EXPR (c) = t;
28419   OMP_CLAUSE_CHAIN (c) = list;
28420   list = c;
28421
28422   return list;
28423 }
28424
28425 /* OpenMP 2.5:
28426    num_threads ( expression ) */
28427
28428 static tree
28429 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
28430                                   location_t location)
28431 {
28432   tree t, c;
28433
28434   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28435     return list;
28436
28437   t = cp_parser_expression (parser);
28438
28439   if (t == error_mark_node
28440       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28441     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28442                                            /*or_comma=*/false,
28443                                            /*consume_paren=*/true);
28444
28445   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
28446                              "num_threads", location);
28447
28448   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
28449   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
28450   OMP_CLAUSE_CHAIN (c) = list;
28451
28452   return c;
28453 }
28454
28455 /* OpenACC:
28456    num_workers ( expression ) */
28457
28458 static tree
28459 cp_parser_omp_clause_num_workers (cp_parser *parser, tree list)
28460 {
28461   tree t, c;
28462   location_t location = cp_lexer_peek_token (parser->lexer)->location;
28463
28464   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28465     return list;
28466
28467   t = cp_parser_condition (parser);
28468
28469   if (t == error_mark_node
28470       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28471     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28472                                            /*or_comma=*/false,
28473                                            /*consume_paren=*/true);
28474
28475   if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
28476     {
28477       error_at (location, "expected positive integer expression");
28478       return list;
28479     }
28480
28481   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_WORKERS, "num_gangs",
28482                                                                 location);
28483
28484   c = build_omp_clause (location, OMP_CLAUSE_NUM_WORKERS);
28485   OMP_CLAUSE_NUM_WORKERS_EXPR (c) = t;
28486   OMP_CLAUSE_CHAIN (c) = list;
28487   list = c;
28488
28489   return list;
28490 }
28491
28492 /* OpenMP 2.5:
28493    ordered */
28494
28495 static tree
28496 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
28497                               tree list, location_t location)
28498 {
28499   tree c;
28500
28501   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
28502                              "ordered", location);
28503
28504   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
28505   OMP_CLAUSE_CHAIN (c) = list;
28506   return c;
28507 }
28508
28509 /* OpenMP 2.5:
28510    reduction ( reduction-operator : variable-list )
28511
28512    reduction-operator:
28513      One of: + * - & ^ | && ||
28514
28515    OpenMP 3.1:
28516
28517    reduction-operator:
28518      One of: + * - & ^ | && || min max
28519
28520    OpenMP 4.0:
28521
28522    reduction-operator:
28523      One of: + * - & ^ | && ||
28524      id-expression  */
28525
28526 static tree
28527 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
28528 {
28529   enum tree_code code = ERROR_MARK;
28530   tree nlist, c, id = NULL_TREE;
28531
28532   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28533     return list;
28534
28535   switch (cp_lexer_peek_token (parser->lexer)->type)
28536     {
28537     case CPP_PLUS: code = PLUS_EXPR; break;
28538     case CPP_MULT: code = MULT_EXPR; break;
28539     case CPP_MINUS: code = MINUS_EXPR; break;
28540     case CPP_AND: code = BIT_AND_EXPR; break;
28541     case CPP_XOR: code = BIT_XOR_EXPR; break;
28542     case CPP_OR: code = BIT_IOR_EXPR; break;
28543     case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
28544     case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
28545     default: break;
28546     }
28547
28548   if (code != ERROR_MARK)
28549     cp_lexer_consume_token (parser->lexer);
28550   else
28551     {
28552       bool saved_colon_corrects_to_scope_p;
28553       saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
28554       parser->colon_corrects_to_scope_p = false;
28555       id = cp_parser_id_expression (parser, /*template_p=*/false,
28556                                     /*check_dependency_p=*/true,
28557                                     /*template_p=*/NULL,
28558                                     /*declarator_p=*/false,
28559                                     /*optional_p=*/false);
28560       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28561       if (identifier_p (id))
28562         {
28563           const char *p = IDENTIFIER_POINTER (id);
28564
28565           if (strcmp (p, "min") == 0)
28566             code = MIN_EXPR;
28567           else if (strcmp (p, "max") == 0)
28568             code = MAX_EXPR;
28569           else if (id == ansi_opname (PLUS_EXPR))
28570             code = PLUS_EXPR;
28571           else if (id == ansi_opname (MULT_EXPR))
28572             code = MULT_EXPR;
28573           else if (id == ansi_opname (MINUS_EXPR))
28574             code = MINUS_EXPR;
28575           else if (id == ansi_opname (BIT_AND_EXPR))
28576             code = BIT_AND_EXPR;
28577           else if (id == ansi_opname (BIT_IOR_EXPR))
28578             code = BIT_IOR_EXPR;
28579           else if (id == ansi_opname (BIT_XOR_EXPR))
28580             code = BIT_XOR_EXPR;
28581           else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
28582             code = TRUTH_ANDIF_EXPR;
28583           else if (id == ansi_opname (TRUTH_ORIF_EXPR))
28584             code = TRUTH_ORIF_EXPR;
28585           id = omp_reduction_id (code, id, NULL_TREE);
28586           tree scope = parser->scope;
28587           if (scope)
28588             id = build_qualified_name (NULL_TREE, scope, id, false);
28589           parser->scope = NULL_TREE;
28590           parser->qualifying_scope = NULL_TREE;
28591           parser->object_scope = NULL_TREE;
28592         }
28593       else
28594         {
28595           error ("invalid reduction-identifier");
28596          resync_fail:
28597           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28598                                                  /*or_comma=*/false,
28599                                                  /*consume_paren=*/true);
28600           return list;
28601         }
28602     }
28603
28604   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28605     goto resync_fail;
28606
28607   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
28608                                           NULL);
28609   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28610     {
28611       OMP_CLAUSE_REDUCTION_CODE (c) = code;
28612       OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
28613     }
28614
28615   return nlist;
28616 }
28617
28618 /* OpenMP 2.5:
28619    schedule ( schedule-kind )
28620    schedule ( schedule-kind , expression )
28621
28622    schedule-kind:
28623      static | dynamic | guided | runtime | auto  */
28624
28625 static tree
28626 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
28627 {
28628   tree c, t;
28629
28630   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28631     return list;
28632
28633   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
28634
28635   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28636     {
28637       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28638       const char *p = IDENTIFIER_POINTER (id);
28639
28640       switch (p[0])
28641         {
28642         case 'd':
28643           if (strcmp ("dynamic", p) != 0)
28644             goto invalid_kind;
28645           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
28646           break;
28647
28648         case 'g':
28649           if (strcmp ("guided", p) != 0)
28650             goto invalid_kind;
28651           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
28652           break;
28653
28654         case 'r':
28655           if (strcmp ("runtime", p) != 0)
28656             goto invalid_kind;
28657           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
28658           break;
28659
28660         default:
28661           goto invalid_kind;
28662         }
28663     }
28664   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28665     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
28666   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
28667     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
28668   else
28669     goto invalid_kind;
28670   cp_lexer_consume_token (parser->lexer);
28671
28672   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28673     {
28674       cp_token *token;
28675       cp_lexer_consume_token (parser->lexer);
28676
28677       token = cp_lexer_peek_token (parser->lexer);
28678       t = cp_parser_assignment_expression (parser);
28679
28680       if (t == error_mark_node)
28681         goto resync_fail;
28682       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
28683         error_at (token->location, "schedule %<runtime%> does not take "
28684                   "a %<chunk_size%> parameter");
28685       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
28686         error_at (token->location, "schedule %<auto%> does not take "
28687                   "a %<chunk_size%> parameter");
28688       else
28689         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
28690
28691       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28692         goto resync_fail;
28693     }
28694   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28695     goto resync_fail;
28696
28697   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
28698   OMP_CLAUSE_CHAIN (c) = list;
28699   return c;
28700
28701  invalid_kind:
28702   cp_parser_error (parser, "invalid schedule kind");
28703  resync_fail:
28704   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28705                                          /*or_comma=*/false,
28706                                          /*consume_paren=*/true);
28707   return list;
28708 }
28709
28710 /* OpenMP 3.0:
28711    untied */
28712
28713 static tree
28714 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
28715                              tree list, location_t location)
28716 {
28717   tree c;
28718
28719   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
28720
28721   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
28722   OMP_CLAUSE_CHAIN (c) = list;
28723   return c;
28724 }
28725
28726 /* OpenMP 4.0:
28727    inbranch
28728    notinbranch */
28729
28730 static tree
28731 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
28732                              tree list, location_t location)
28733 {
28734   check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
28735   tree c = build_omp_clause (location, code);
28736   OMP_CLAUSE_CHAIN (c) = list;
28737   return c;
28738 }
28739
28740 /* OpenMP 4.0:
28741    parallel
28742    for
28743    sections
28744    taskgroup */
28745
28746 static tree
28747 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
28748                                  enum omp_clause_code code,
28749                                  tree list, location_t location)
28750 {
28751   tree c = build_omp_clause (location, code);
28752   OMP_CLAUSE_CHAIN (c) = list;
28753   return c;
28754 }
28755
28756 /* OpenMP 4.0:
28757    num_teams ( expression ) */
28758
28759 static tree
28760 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28761                                 location_t location)
28762 {
28763   tree t, c;
28764
28765   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28766     return list;
28767
28768   t = cp_parser_expression (parser);
28769
28770   if (t == error_mark_node
28771       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28772     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28773                                            /*or_comma=*/false,
28774                                            /*consume_paren=*/true);
28775
28776   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28777                              "num_teams", location);
28778
28779   c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28780   OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28781   OMP_CLAUSE_CHAIN (c) = list;
28782
28783   return c;
28784 }
28785
28786 /* OpenMP 4.0:
28787    thread_limit ( expression ) */
28788
28789 static tree
28790 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28791                                    location_t location)
28792 {
28793   tree t, c;
28794
28795   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28796     return list;
28797
28798   t = cp_parser_expression (parser);
28799
28800   if (t == error_mark_node
28801       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28802     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28803                                            /*or_comma=*/false,
28804                                            /*consume_paren=*/true);
28805
28806   check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28807                              "thread_limit", location);
28808
28809   c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28810   OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28811   OMP_CLAUSE_CHAIN (c) = list;
28812
28813   return c;
28814 }
28815
28816 /* OpenMP 4.0:
28817    aligned ( variable-list )
28818    aligned ( variable-list : constant-expression )  */
28819
28820 static tree
28821 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28822 {
28823   tree nlist, c, alignment = NULL_TREE;
28824   bool colon;
28825
28826   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28827     return list;
28828
28829   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28830                                           &colon);
28831
28832   if (colon)
28833     {
28834       alignment = cp_parser_constant_expression (parser);
28835
28836       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28837         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28838                                                /*or_comma=*/false,
28839                                                /*consume_paren=*/true);
28840
28841       if (alignment == error_mark_node)
28842         alignment = NULL_TREE;
28843     }
28844
28845   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28846     OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28847
28848   return nlist;
28849 }
28850
28851 /* OpenMP 4.0:
28852    linear ( variable-list )
28853    linear ( variable-list : expression )  */
28854
28855 static tree
28856 cp_parser_omp_clause_linear (cp_parser *parser, tree list, 
28857                              bool is_cilk_simd_fn)
28858 {
28859   tree nlist, c, step = integer_one_node;
28860   bool colon;
28861
28862   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28863     return list;
28864
28865   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28866                                           &colon);
28867
28868   if (colon)
28869     {
28870       step = cp_parser_expression (parser);
28871
28872       if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28873         {
28874           sorry ("using parameters for %<linear%> step is not supported yet");
28875           step = integer_one_node;
28876         }
28877       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28878         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28879                                                /*or_comma=*/false,
28880                                                /*consume_paren=*/true);
28881
28882       if (step == error_mark_node)
28883         return list;
28884     }
28885
28886   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28887     OMP_CLAUSE_LINEAR_STEP (c) = step;
28888
28889   return nlist;
28890 }
28891
28892 /* OpenMP 4.0:
28893    safelen ( constant-expression )  */
28894
28895 static tree
28896 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28897                               location_t location)
28898 {
28899   tree t, c;
28900
28901   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28902     return list;
28903
28904   t = cp_parser_constant_expression (parser);
28905
28906   if (t == error_mark_node
28907       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28908     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28909                                            /*or_comma=*/false,
28910                                            /*consume_paren=*/true);
28911
28912   check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28913
28914   c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28915   OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28916   OMP_CLAUSE_CHAIN (c) = list;
28917
28918   return c;
28919 }
28920
28921 /* OpenMP 4.0:
28922    simdlen ( constant-expression )  */
28923
28924 static tree
28925 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28926                               location_t location)
28927 {
28928   tree t, c;
28929
28930   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28931     return list;
28932
28933   t = cp_parser_constant_expression (parser);
28934
28935   if (t == error_mark_node
28936       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28937     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28938                                            /*or_comma=*/false,
28939                                            /*consume_paren=*/true);
28940
28941   check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28942
28943   c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28944   OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28945   OMP_CLAUSE_CHAIN (c) = list;
28946
28947   return c;
28948 }
28949
28950 /* OpenMP 4.0:
28951    depend ( depend-kind : variable-list )
28952
28953    depend-kind:
28954      in | out | inout  */
28955
28956 static tree
28957 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28958 {
28959   tree nlist, c;
28960   enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28961
28962   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28963     return list;
28964
28965   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28966     {
28967       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28968       const char *p = IDENTIFIER_POINTER (id);
28969
28970       if (strcmp ("in", p) == 0)
28971         kind = OMP_CLAUSE_DEPEND_IN;
28972       else if (strcmp ("inout", p) == 0)
28973         kind = OMP_CLAUSE_DEPEND_INOUT;
28974       else if (strcmp ("out", p) == 0)
28975         kind = OMP_CLAUSE_DEPEND_OUT;
28976       else
28977         goto invalid_kind;
28978     }
28979   else
28980     goto invalid_kind;
28981
28982   cp_lexer_consume_token (parser->lexer);
28983   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28984     goto resync_fail;
28985
28986   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28987                                           NULL);
28988
28989   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28990     OMP_CLAUSE_DEPEND_KIND (c) = kind;
28991
28992   return nlist;
28993
28994  invalid_kind:
28995   cp_parser_error (parser, "invalid depend kind");
28996  resync_fail:
28997   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28998                                          /*or_comma=*/false,
28999                                          /*consume_paren=*/true);
29000   return list;
29001 }
29002
29003 /* OpenMP 4.0:
29004    map ( map-kind : variable-list )
29005    map ( variable-list )
29006
29007    map-kind:
29008      alloc | to | from | tofrom  */
29009
29010 static tree
29011 cp_parser_omp_clause_map (cp_parser *parser, tree list)
29012 {
29013   tree nlist, c;
29014   enum gomp_map_kind kind = GOMP_MAP_TOFROM;
29015
29016   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29017     return list;
29018
29019   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
29020       && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
29021     {
29022       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29023       const char *p = IDENTIFIER_POINTER (id);
29024
29025       if (strcmp ("alloc", p) == 0)
29026         kind = GOMP_MAP_ALLOC;
29027       else if (strcmp ("to", p) == 0)
29028         kind = GOMP_MAP_TO;
29029       else if (strcmp ("from", p) == 0)
29030         kind = GOMP_MAP_FROM;
29031       else if (strcmp ("tofrom", p) == 0)
29032         kind = GOMP_MAP_TOFROM;
29033       else
29034         {
29035           cp_parser_error (parser, "invalid map kind");
29036           cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29037                                                  /*or_comma=*/false,
29038                                                  /*consume_paren=*/true);
29039           return list;
29040         }
29041       cp_lexer_consume_token (parser->lexer);
29042       cp_lexer_consume_token (parser->lexer);
29043     }
29044
29045   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
29046                                           NULL);
29047
29048   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
29049     OMP_CLAUSE_SET_MAP_KIND (c, kind);
29050
29051   return nlist;
29052 }
29053
29054 /* OpenMP 4.0:
29055    device ( expression ) */
29056
29057 static tree
29058 cp_parser_omp_clause_device (cp_parser *parser, tree list,
29059                              location_t location)
29060 {
29061   tree t, c;
29062
29063   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29064     return list;
29065
29066   t = cp_parser_expression (parser);
29067
29068   if (t == error_mark_node
29069       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29070     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29071                                            /*or_comma=*/false,
29072                                            /*consume_paren=*/true);
29073
29074   check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
29075                              "device", location);
29076
29077   c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
29078   OMP_CLAUSE_DEVICE_ID (c) = t;
29079   OMP_CLAUSE_CHAIN (c) = list;
29080
29081   return c;
29082 }
29083
29084 /* OpenMP 4.0:
29085    dist_schedule ( static )
29086    dist_schedule ( static , expression )  */
29087
29088 static tree
29089 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
29090                                     location_t location)
29091 {
29092   tree c, t;
29093
29094   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29095     return list;
29096
29097   c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
29098
29099   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
29100     goto invalid_kind;
29101   cp_lexer_consume_token (parser->lexer);
29102
29103   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29104     {
29105       cp_lexer_consume_token (parser->lexer);
29106
29107       t = cp_parser_assignment_expression (parser);
29108
29109       if (t == error_mark_node)
29110         goto resync_fail;
29111       OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
29112
29113       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29114         goto resync_fail;
29115     }
29116   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29117     goto resync_fail;
29118
29119   check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
29120                              location);
29121   OMP_CLAUSE_CHAIN (c) = list;
29122   return c;
29123
29124  invalid_kind:
29125   cp_parser_error (parser, "invalid dist_schedule kind");
29126  resync_fail:
29127   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29128                                          /*or_comma=*/false,
29129                                          /*consume_paren=*/true);
29130   return list;
29131 }
29132
29133 /* OpenMP 4.0:
29134    proc_bind ( proc-bind-kind )
29135
29136    proc-bind-kind:
29137      master | close | spread  */
29138
29139 static tree
29140 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
29141                                 location_t location)
29142 {
29143   tree c;
29144   enum omp_clause_proc_bind_kind kind;
29145
29146   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29147     return list;
29148
29149   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29150     {
29151       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29152       const char *p = IDENTIFIER_POINTER (id);
29153
29154       if (strcmp ("master", p) == 0)
29155         kind = OMP_CLAUSE_PROC_BIND_MASTER;
29156       else if (strcmp ("close", p) == 0)
29157         kind = OMP_CLAUSE_PROC_BIND_CLOSE;
29158       else if (strcmp ("spread", p) == 0)
29159         kind = OMP_CLAUSE_PROC_BIND_SPREAD;
29160       else
29161         goto invalid_kind;
29162     }
29163   else
29164     goto invalid_kind;
29165
29166   cp_lexer_consume_token (parser->lexer);
29167   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
29168     goto resync_fail;
29169
29170   c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
29171   check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
29172                              location);
29173   OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
29174   OMP_CLAUSE_CHAIN (c) = list;
29175   return c;
29176
29177  invalid_kind:
29178   cp_parser_error (parser, "invalid depend kind");
29179  resync_fail:
29180   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29181                                          /*or_comma=*/false,
29182                                          /*consume_paren=*/true);
29183   return list;
29184 }
29185
29186 /* OpenACC:
29187    async [( int-expr )] */
29188
29189 static tree
29190 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
29191 {
29192   tree c, t;
29193   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29194
29195   t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
29196
29197   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29198     {
29199       cp_lexer_consume_token (parser->lexer);
29200
29201       t = cp_parser_expression (parser);
29202       if (t == error_mark_node
29203           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29204         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29205                                                 /*or_comma=*/false,
29206                                                 /*consume_paren=*/true);
29207     }
29208
29209   check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
29210
29211   c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
29212   OMP_CLAUSE_ASYNC_EXPR (c) = t;
29213   OMP_CLAUSE_CHAIN (c) = list;
29214   list = c;
29215
29216   return list;
29217 }
29218
29219 /* Parse all OpenACC clauses.  The set clauses allowed by the directive
29220    is a bitmask in MASK.  Return the list of clauses found.  */
29221
29222 static tree
29223 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
29224                            const char *where, cp_token *pragma_tok,
29225                            bool finish_p = true)
29226 {
29227   tree clauses = NULL;
29228   bool first = true;
29229
29230   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29231     {
29232       location_t here;
29233       pragma_omp_clause c_kind;
29234       const char *c_name;
29235       tree prev = clauses;
29236
29237       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29238         cp_lexer_consume_token (parser->lexer);
29239
29240       here = cp_lexer_peek_token (parser->lexer)->location;
29241       c_kind = cp_parser_omp_clause_name (parser);
29242
29243       switch (c_kind)
29244         {
29245         case PRAGMA_OACC_CLAUSE_ASYNC:
29246           clauses = cp_parser_oacc_clause_async (parser, clauses);
29247           c_name = "async";
29248           break;
29249         case PRAGMA_OACC_CLAUSE_COLLAPSE:
29250           clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
29251           c_name = "collapse";
29252           break;
29253         case PRAGMA_OACC_CLAUSE_COPY:
29254           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29255           c_name = "copy";
29256           break;
29257         case PRAGMA_OACC_CLAUSE_COPYIN:
29258           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29259           c_name = "copyin";
29260           break;
29261         case PRAGMA_OACC_CLAUSE_COPYOUT:
29262           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29263           c_name = "copyout";
29264           break;
29265         case PRAGMA_OACC_CLAUSE_CREATE:
29266           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29267           c_name = "create";
29268           break;
29269         case PRAGMA_OACC_CLAUSE_DELETE:
29270           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29271           c_name = "delete";
29272           break;
29273         case PRAGMA_OACC_CLAUSE_DEVICE:
29274           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29275           c_name = "device";
29276           break;
29277         case PRAGMA_OACC_CLAUSE_DEVICEPTR:
29278           clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
29279           c_name = "deviceptr";
29280           break;
29281         case PRAGMA_OACC_CLAUSE_HOST:
29282           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29283           c_name = "host";
29284           break;
29285         case PRAGMA_OACC_CLAUSE_IF:
29286           clauses = cp_parser_omp_clause_if (parser, clauses, here);
29287           c_name = "if";
29288           break;
29289         case PRAGMA_OACC_CLAUSE_NUM_GANGS:
29290           clauses = cp_parser_omp_clause_num_gangs (parser, clauses);
29291           c_name = "num_gangs";
29292           break;
29293         case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
29294           clauses = cp_parser_omp_clause_num_workers (parser, clauses);
29295           c_name = "num_workers";
29296           break;
29297         case PRAGMA_OACC_CLAUSE_PRESENT:
29298           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29299           c_name = "present";
29300           break;
29301         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
29302           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29303           c_name = "present_or_copy";
29304           break;
29305         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
29306           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29307           c_name = "present_or_copyin";
29308           break;
29309         case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
29310           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29311           c_name = "present_or_copyout";
29312           break;
29313         case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
29314           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29315           c_name = "present_or_create";
29316           break;
29317         case PRAGMA_OACC_CLAUSE_REDUCTION:
29318           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29319           c_name = "reduction";
29320           break;
29321         case PRAGMA_OACC_CLAUSE_SELF:
29322           clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
29323           c_name = "self";
29324           break;
29325         case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
29326           clauses = cp_parser_oacc_clause_vector_length (parser, clauses);
29327           c_name = "vector_length";
29328           break;
29329         case PRAGMA_OACC_CLAUSE_WAIT:
29330           clauses = cp_parser_oacc_clause_wait (parser, clauses);
29331           c_name = "wait";
29332           break;
29333         default:
29334           cp_parser_error (parser, "expected %<#pragma acc%> clause");
29335           goto saw_error;
29336         }
29337
29338       first = false;
29339
29340       if (((mask >> c_kind) & 1) == 0)
29341         {
29342           /* Remove the invalid clause(s) from the list to avoid
29343              confusing the rest of the compiler.  */
29344           clauses = prev;
29345           error_at (here, "%qs is not valid for %qs", c_name, where);
29346         }
29347     }
29348
29349  saw_error:
29350   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29351
29352   if (finish_p)
29353     return finish_omp_clauses (clauses);
29354
29355   return clauses;
29356 }
29357
29358 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
29359    is a bitmask in MASK.  Return the list of clauses found; the result
29360    of clause default goes in *pdefault.  */
29361
29362 static tree
29363 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
29364                            const char *where, cp_token *pragma_tok,
29365                            bool finish_p = true)
29366 {
29367   tree clauses = NULL;
29368   bool first = true;
29369   cp_token *token = NULL;
29370   bool cilk_simd_fn = false;
29371
29372   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29373     {
29374       pragma_omp_clause c_kind;
29375       const char *c_name;
29376       tree prev = clauses;
29377
29378       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29379         cp_lexer_consume_token (parser->lexer);
29380
29381       token = cp_lexer_peek_token (parser->lexer);
29382       c_kind = cp_parser_omp_clause_name (parser);
29383
29384       switch (c_kind)
29385         {
29386         case PRAGMA_OMP_CLAUSE_COLLAPSE:
29387           clauses = cp_parser_omp_clause_collapse (parser, clauses,
29388                                                    token->location);
29389           c_name = "collapse";
29390           break;
29391         case PRAGMA_OMP_CLAUSE_COPYIN:
29392           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
29393           c_name = "copyin";
29394           break;
29395         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
29396           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
29397                                             clauses);
29398           c_name = "copyprivate";
29399           break;
29400         case PRAGMA_OMP_CLAUSE_DEFAULT:
29401           clauses = cp_parser_omp_clause_default (parser, clauses,
29402                                                   token->location);
29403           c_name = "default";
29404           break;
29405         case PRAGMA_OMP_CLAUSE_FINAL:
29406           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
29407           c_name = "final";
29408           break;
29409         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
29410           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
29411                                             clauses);
29412           c_name = "firstprivate";
29413           break;
29414         case PRAGMA_OMP_CLAUSE_IF:
29415           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
29416           c_name = "if";
29417           break;
29418         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
29419           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
29420                                             clauses);
29421           c_name = "lastprivate";
29422           break;
29423         case PRAGMA_OMP_CLAUSE_MERGEABLE:
29424           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
29425                                                     token->location);
29426           c_name = "mergeable";
29427           break;
29428         case PRAGMA_OMP_CLAUSE_NOWAIT:
29429           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
29430           c_name = "nowait";
29431           break;
29432         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
29433           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
29434                                                       token->location);
29435           c_name = "num_threads";
29436           break;
29437         case PRAGMA_OMP_CLAUSE_ORDERED:
29438           clauses = cp_parser_omp_clause_ordered (parser, clauses,
29439                                                   token->location);
29440           c_name = "ordered";
29441           break;
29442         case PRAGMA_OMP_CLAUSE_PRIVATE:
29443           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
29444                                             clauses);
29445           c_name = "private";
29446           break;
29447         case PRAGMA_OMP_CLAUSE_REDUCTION:
29448           clauses = cp_parser_omp_clause_reduction (parser, clauses);
29449           c_name = "reduction";
29450           break;
29451         case PRAGMA_OMP_CLAUSE_SCHEDULE:
29452           clauses = cp_parser_omp_clause_schedule (parser, clauses,
29453                                                    token->location);
29454           c_name = "schedule";
29455           break;
29456         case PRAGMA_OMP_CLAUSE_SHARED:
29457           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
29458                                             clauses);
29459           c_name = "shared";
29460           break;
29461         case PRAGMA_OMP_CLAUSE_UNTIED:
29462           clauses = cp_parser_omp_clause_untied (parser, clauses,
29463                                                  token->location);
29464           c_name = "untied";
29465           break;
29466         case PRAGMA_OMP_CLAUSE_INBRANCH:
29467         case PRAGMA_CILK_CLAUSE_MASK:
29468           clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
29469                                                  clauses, token->location);
29470           c_name = "inbranch";
29471           break;
29472         case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
29473         case PRAGMA_CILK_CLAUSE_NOMASK:
29474           clauses = cp_parser_omp_clause_branch (parser,
29475                                                  OMP_CLAUSE_NOTINBRANCH,
29476                                                  clauses, token->location);
29477           c_name = "notinbranch";
29478           break;
29479         case PRAGMA_OMP_CLAUSE_PARALLEL:
29480           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
29481                                                      clauses, token->location);
29482           c_name = "parallel";
29483           if (!first)
29484             {
29485              clause_not_first:
29486               error_at (token->location, "%qs must be the first clause of %qs",
29487                         c_name, where);
29488               clauses = prev;
29489             }
29490           break;
29491         case PRAGMA_OMP_CLAUSE_FOR:
29492           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
29493                                                      clauses, token->location);
29494           c_name = "for";
29495           if (!first)
29496             goto clause_not_first;
29497           break;
29498         case PRAGMA_OMP_CLAUSE_SECTIONS:
29499           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
29500                                                      clauses, token->location);
29501           c_name = "sections";
29502           if (!first)
29503             goto clause_not_first;
29504           break;
29505         case PRAGMA_OMP_CLAUSE_TASKGROUP:
29506           clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
29507                                                      clauses, token->location);
29508           c_name = "taskgroup";
29509           if (!first)
29510             goto clause_not_first;
29511           break;
29512         case PRAGMA_OMP_CLAUSE_TO:
29513           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
29514                                             clauses);
29515           c_name = "to";
29516           break;
29517         case PRAGMA_OMP_CLAUSE_FROM:
29518           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
29519                                             clauses);
29520           c_name = "from";
29521           break;
29522         case PRAGMA_OMP_CLAUSE_UNIFORM:
29523           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
29524                                             clauses);
29525           c_name = "uniform";
29526           break;
29527         case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
29528           clauses = cp_parser_omp_clause_num_teams (parser, clauses,
29529                                                     token->location);
29530           c_name = "num_teams";
29531           break;
29532         case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
29533           clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
29534                                                        token->location);
29535           c_name = "thread_limit";
29536           break;
29537         case PRAGMA_OMP_CLAUSE_ALIGNED:
29538           clauses = cp_parser_omp_clause_aligned (parser, clauses);
29539           c_name = "aligned";
29540           break;
29541         case PRAGMA_OMP_CLAUSE_LINEAR:
29542           if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
29543             cilk_simd_fn = true;
29544           clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
29545           c_name = "linear";
29546           break;
29547         case PRAGMA_OMP_CLAUSE_DEPEND:
29548           clauses = cp_parser_omp_clause_depend (parser, clauses);
29549           c_name = "depend";
29550           break;
29551         case PRAGMA_OMP_CLAUSE_MAP:
29552           clauses = cp_parser_omp_clause_map (parser, clauses);
29553           c_name = "map";
29554           break;
29555         case PRAGMA_OMP_CLAUSE_DEVICE:
29556           clauses = cp_parser_omp_clause_device (parser, clauses,
29557                                                  token->location);
29558           c_name = "device";
29559           break;
29560         case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
29561           clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
29562                                                         token->location);
29563           c_name = "dist_schedule";
29564           break;
29565         case PRAGMA_OMP_CLAUSE_PROC_BIND:
29566           clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
29567                                                     token->location);
29568           c_name = "proc_bind";
29569           break;
29570         case PRAGMA_OMP_CLAUSE_SAFELEN:
29571           clauses = cp_parser_omp_clause_safelen (parser, clauses,
29572                                                   token->location);
29573           c_name = "safelen";
29574           break;
29575         case PRAGMA_OMP_CLAUSE_SIMDLEN:
29576           clauses = cp_parser_omp_clause_simdlen (parser, clauses,
29577                                                   token->location);
29578           c_name = "simdlen";
29579           break;
29580         case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
29581           clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
29582           c_name = "simdlen";
29583           break;
29584         default:
29585           cp_parser_error (parser, "expected %<#pragma omp%> clause");
29586           goto saw_error;
29587         }
29588
29589       first = false;
29590
29591       if (((mask >> c_kind) & 1) == 0)
29592         {
29593           /* Remove the invalid clause(s) from the list to avoid
29594              confusing the rest of the compiler.  */
29595           clauses = prev;
29596           error_at (token->location, "%qs is not valid for %qs", c_name, where);
29597         }
29598     }
29599  saw_error:
29600   /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
29601      no reason to skip to the end.  */
29602   if (!(flag_cilkplus && pragma_tok == NULL))
29603     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29604   if (finish_p)
29605     return finish_omp_clauses (clauses);
29606   return clauses;
29607 }
29608
29609 /* OpenMP 2.5:
29610    structured-block:
29611      statement
29612
29613    In practice, we're also interested in adding the statement to an
29614    outer node.  So it is convenient if we work around the fact that
29615    cp_parser_statement calls add_stmt.  */
29616
29617 static unsigned
29618 cp_parser_begin_omp_structured_block (cp_parser *parser)
29619 {
29620   unsigned save = parser->in_statement;
29621
29622   /* Only move the values to IN_OMP_BLOCK if they weren't false.
29623      This preserves the "not within loop or switch" style error messages
29624      for nonsense cases like
29625         void foo() {
29626         #pragma omp single
29627           break;
29628         }
29629   */
29630   if (parser->in_statement)
29631     parser->in_statement = IN_OMP_BLOCK;
29632
29633   return save;
29634 }
29635
29636 static void
29637 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
29638 {
29639   parser->in_statement = save;
29640 }
29641
29642 static tree
29643 cp_parser_omp_structured_block (cp_parser *parser)
29644 {
29645   tree stmt = begin_omp_structured_block ();
29646   unsigned int save = cp_parser_begin_omp_structured_block (parser);
29647
29648   cp_parser_statement (parser, NULL_TREE, false, NULL);
29649
29650   cp_parser_end_omp_structured_block (parser, save);
29651   return finish_omp_structured_block (stmt);
29652 }
29653
29654 /* OpenMP 2.5:
29655    # pragma omp atomic new-line
29656      expression-stmt
29657
29658    expression-stmt:
29659      x binop= expr | x++ | ++x | x-- | --x
29660    binop:
29661      +, *, -, /, &, ^, |, <<, >>
29662
29663   where x is an lvalue expression with scalar type.
29664
29665    OpenMP 3.1:
29666    # pragma omp atomic new-line
29667      update-stmt
29668
29669    # pragma omp atomic read new-line
29670      read-stmt
29671
29672    # pragma omp atomic write new-line
29673      write-stmt
29674
29675    # pragma omp atomic update new-line
29676      update-stmt
29677
29678    # pragma omp atomic capture new-line
29679      capture-stmt
29680
29681    # pragma omp atomic capture new-line
29682      capture-block
29683
29684    read-stmt:
29685      v = x
29686    write-stmt:
29687      x = expr
29688    update-stmt:
29689      expression-stmt | x = x binop expr
29690    capture-stmt:
29691      v = expression-stmt
29692    capture-block:
29693      { v = x; update-stmt; } | { update-stmt; v = x; }
29694
29695    OpenMP 4.0:
29696    update-stmt:
29697      expression-stmt | x = x binop expr | x = expr binop x
29698    capture-stmt:
29699      v = update-stmt
29700    capture-block:
29701      { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
29702
29703   where x and v are lvalue expressions with scalar type.  */
29704
29705 static void
29706 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
29707 {
29708   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
29709   tree rhs1 = NULL_TREE, orig_lhs;
29710   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
29711   bool structured_block = false;
29712   bool seq_cst = false;
29713
29714   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29715     {
29716       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29717       const char *p = IDENTIFIER_POINTER (id);
29718
29719       if (!strcmp (p, "seq_cst"))
29720         {
29721           seq_cst = true;
29722           cp_lexer_consume_token (parser->lexer);
29723           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29724               && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29725             cp_lexer_consume_token (parser->lexer);
29726         }
29727     }
29728   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29729     {
29730       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29731       const char *p = IDENTIFIER_POINTER (id);
29732
29733       if (!strcmp (p, "read"))
29734         code = OMP_ATOMIC_READ;
29735       else if (!strcmp (p, "write"))
29736         code = NOP_EXPR;
29737       else if (!strcmp (p, "update"))
29738         code = OMP_ATOMIC;
29739       else if (!strcmp (p, "capture"))
29740         code = OMP_ATOMIC_CAPTURE_NEW;
29741       else
29742         p = NULL;
29743       if (p)
29744         cp_lexer_consume_token (parser->lexer);
29745     }
29746   if (!seq_cst)
29747     {
29748       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
29749           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
29750         cp_lexer_consume_token (parser->lexer);
29751
29752       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29753         {
29754           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29755           const char *p = IDENTIFIER_POINTER (id);
29756
29757           if (!strcmp (p, "seq_cst"))
29758             {
29759               seq_cst = true;
29760               cp_lexer_consume_token (parser->lexer);
29761             }
29762         }
29763     }
29764   cp_parser_require_pragma_eol (parser, pragma_tok);
29765
29766   switch (code)
29767     {
29768     case OMP_ATOMIC_READ:
29769     case NOP_EXPR: /* atomic write */
29770       v = cp_parser_unary_expression (parser);
29771       if (v == error_mark_node)
29772         goto saw_error;
29773       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29774         goto saw_error;
29775       if (code == NOP_EXPR)
29776         lhs = cp_parser_expression (parser);
29777       else
29778         lhs = cp_parser_unary_expression (parser);
29779       if (lhs == error_mark_node)
29780         goto saw_error;
29781       if (code == NOP_EXPR)
29782         {
29783           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
29784              opcode.  */
29785           code = OMP_ATOMIC;
29786           rhs = lhs;
29787           lhs = v;
29788           v = NULL_TREE;
29789         }
29790       goto done;
29791     case OMP_ATOMIC_CAPTURE_NEW:
29792       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29793         {
29794           cp_lexer_consume_token (parser->lexer);
29795           structured_block = true;
29796         }
29797       else
29798         {
29799           v = cp_parser_unary_expression (parser);
29800           if (v == error_mark_node)
29801             goto saw_error;
29802           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29803             goto saw_error;
29804         }
29805     default:
29806       break;
29807     }
29808
29809 restart:
29810   lhs = cp_parser_unary_expression (parser);
29811   orig_lhs = lhs;
29812   switch (TREE_CODE (lhs))
29813     {
29814     case ERROR_MARK:
29815       goto saw_error;
29816
29817     case POSTINCREMENT_EXPR:
29818       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29819         code = OMP_ATOMIC_CAPTURE_OLD;
29820       /* FALLTHROUGH */
29821     case PREINCREMENT_EXPR:
29822       lhs = TREE_OPERAND (lhs, 0);
29823       opcode = PLUS_EXPR;
29824       rhs = integer_one_node;
29825       break;
29826
29827     case POSTDECREMENT_EXPR:
29828       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
29829         code = OMP_ATOMIC_CAPTURE_OLD;
29830       /* FALLTHROUGH */
29831     case PREDECREMENT_EXPR:
29832       lhs = TREE_OPERAND (lhs, 0);
29833       opcode = MINUS_EXPR;
29834       rhs = integer_one_node;
29835       break;
29836
29837     case COMPOUND_EXPR:
29838       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
29839          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
29840          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
29841          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
29842          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
29843                                              (TREE_OPERAND (lhs, 1), 0), 0)))
29844             == BOOLEAN_TYPE)
29845        /* Undo effects of boolean_increment for post {in,de}crement.  */
29846        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
29847       /* FALLTHRU */
29848     case MODIFY_EXPR:
29849       if (TREE_CODE (lhs) == MODIFY_EXPR
29850          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
29851         {
29852           /* Undo effects of boolean_increment.  */
29853           if (integer_onep (TREE_OPERAND (lhs, 1)))
29854             {
29855               /* This is pre or post increment.  */
29856               rhs = TREE_OPERAND (lhs, 1);
29857               lhs = TREE_OPERAND (lhs, 0);
29858               opcode = NOP_EXPR;
29859               if (code == OMP_ATOMIC_CAPTURE_NEW
29860                   && !structured_block
29861                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
29862                 code = OMP_ATOMIC_CAPTURE_OLD;
29863               break;
29864             }
29865         }
29866       /* FALLTHRU */
29867     default:
29868       switch (cp_lexer_peek_token (parser->lexer)->type)
29869         {
29870         case CPP_MULT_EQ:
29871           opcode = MULT_EXPR;
29872           break;
29873         case CPP_DIV_EQ:
29874           opcode = TRUNC_DIV_EXPR;
29875           break;
29876         case CPP_PLUS_EQ:
29877           opcode = PLUS_EXPR;
29878           break;
29879         case CPP_MINUS_EQ:
29880           opcode = MINUS_EXPR;
29881           break;
29882         case CPP_LSHIFT_EQ:
29883           opcode = LSHIFT_EXPR;
29884           break;
29885         case CPP_RSHIFT_EQ:
29886           opcode = RSHIFT_EXPR;
29887           break;
29888         case CPP_AND_EQ:
29889           opcode = BIT_AND_EXPR;
29890           break;
29891         case CPP_OR_EQ:
29892           opcode = BIT_IOR_EXPR;
29893           break;
29894         case CPP_XOR_EQ:
29895           opcode = BIT_XOR_EXPR;
29896           break;
29897         case CPP_EQ:
29898           enum cp_parser_prec oprec;
29899           cp_token *token;
29900           cp_lexer_consume_token (parser->lexer);
29901           cp_parser_parse_tentatively (parser);
29902           rhs1 = cp_parser_simple_cast_expression (parser);
29903           if (rhs1 == error_mark_node)
29904             {
29905               cp_parser_abort_tentative_parse (parser);
29906               cp_parser_simple_cast_expression (parser);
29907               goto saw_error;
29908             }
29909           token = cp_lexer_peek_token (parser->lexer);
29910           if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
29911             {
29912               cp_parser_abort_tentative_parse (parser);
29913               cp_parser_parse_tentatively (parser);
29914               rhs = cp_parser_binary_expression (parser, false, true,
29915                                                  PREC_NOT_OPERATOR, NULL);
29916               if (rhs == error_mark_node)
29917                 {
29918                   cp_parser_abort_tentative_parse (parser);
29919                   cp_parser_binary_expression (parser, false, true,
29920                                                PREC_NOT_OPERATOR, NULL);
29921                   goto saw_error;
29922                 }
29923               switch (TREE_CODE (rhs))
29924                 {
29925                 case MULT_EXPR:
29926                 case TRUNC_DIV_EXPR:
29927                 case RDIV_EXPR:
29928                 case PLUS_EXPR:
29929                 case MINUS_EXPR:
29930                 case LSHIFT_EXPR:
29931                 case RSHIFT_EXPR:
29932                 case BIT_AND_EXPR:
29933                 case BIT_IOR_EXPR:
29934                 case BIT_XOR_EXPR:
29935                   if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29936                     {
29937                       if (cp_parser_parse_definitely (parser))
29938                         {
29939                           opcode = TREE_CODE (rhs);
29940                           rhs1 = TREE_OPERAND (rhs, 0);
29941                           rhs = TREE_OPERAND (rhs, 1);
29942                           goto stmt_done;
29943                         }
29944                       else
29945                         goto saw_error;
29946                     }
29947                   break;
29948                 default:
29949                   break;
29950                 }
29951               cp_parser_abort_tentative_parse (parser);
29952               if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29953                 {
29954                   rhs = cp_parser_expression (parser);
29955                   if (rhs == error_mark_node)
29956                     goto saw_error;
29957                   opcode = NOP_EXPR;
29958                   rhs1 = NULL_TREE;
29959                   goto stmt_done;
29960                 }
29961               cp_parser_error (parser,
29962                                "invalid form of %<#pragma omp atomic%>");
29963               goto saw_error;
29964             }
29965           if (!cp_parser_parse_definitely (parser))
29966             goto saw_error;
29967           switch (token->type)
29968             {
29969             case CPP_SEMICOLON:
29970               if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29971                 {
29972                   code = OMP_ATOMIC_CAPTURE_OLD;
29973                   v = lhs;
29974                   lhs = NULL_TREE;
29975                   lhs1 = rhs1;
29976                   rhs1 = NULL_TREE;
29977                   cp_lexer_consume_token (parser->lexer);
29978                   goto restart;
29979                 }
29980               else if (structured_block)
29981                 {
29982                   opcode = NOP_EXPR;
29983                   rhs = rhs1;
29984                   rhs1 = NULL_TREE;
29985                   goto stmt_done;
29986                 }
29987               cp_parser_error (parser,
29988                                "invalid form of %<#pragma omp atomic%>");
29989               goto saw_error;
29990             case CPP_MULT:
29991               opcode = MULT_EXPR;
29992               break;
29993             case CPP_DIV:
29994               opcode = TRUNC_DIV_EXPR;
29995               break;
29996             case CPP_PLUS:
29997               opcode = PLUS_EXPR;
29998               break;
29999             case CPP_MINUS:
30000               opcode = MINUS_EXPR;
30001               break;
30002             case CPP_LSHIFT:
30003               opcode = LSHIFT_EXPR;
30004               break;
30005             case CPP_RSHIFT:
30006               opcode = RSHIFT_EXPR;
30007               break;
30008             case CPP_AND:
30009               opcode = BIT_AND_EXPR;
30010               break;
30011             case CPP_OR:
30012               opcode = BIT_IOR_EXPR;
30013               break;
30014             case CPP_XOR:
30015               opcode = BIT_XOR_EXPR;
30016               break;
30017             default:
30018               cp_parser_error (parser,
30019                                "invalid operator for %<#pragma omp atomic%>");
30020               goto saw_error;
30021             }
30022           oprec = TOKEN_PRECEDENCE (token);
30023           gcc_assert (oprec != PREC_NOT_OPERATOR);
30024           if (commutative_tree_code (opcode))
30025             oprec = (enum cp_parser_prec) (oprec - 1);
30026           cp_lexer_consume_token (parser->lexer);
30027           rhs = cp_parser_binary_expression (parser, false, false,
30028                                              oprec, NULL);
30029           if (rhs == error_mark_node)
30030             goto saw_error;
30031           goto stmt_done;
30032           /* FALLTHROUGH */
30033         default:
30034           cp_parser_error (parser,
30035                            "invalid operator for %<#pragma omp atomic%>");
30036           goto saw_error;
30037         }
30038       cp_lexer_consume_token (parser->lexer);
30039
30040       rhs = cp_parser_expression (parser);
30041       if (rhs == error_mark_node)
30042         goto saw_error;
30043       break;
30044     }
30045 stmt_done:
30046   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
30047     {
30048       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
30049         goto saw_error;
30050       v = cp_parser_unary_expression (parser);
30051       if (v == error_mark_node)
30052         goto saw_error;
30053       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30054         goto saw_error;
30055       lhs1 = cp_parser_unary_expression (parser);
30056       if (lhs1 == error_mark_node)
30057         goto saw_error;
30058     }
30059   if (structured_block)
30060     {
30061       cp_parser_consume_semicolon_at_end_of_statement (parser);
30062       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30063     }
30064 done:
30065   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
30066   if (!structured_block)
30067     cp_parser_consume_semicolon_at_end_of_statement (parser);
30068   return;
30069
30070  saw_error:
30071   cp_parser_skip_to_end_of_block_or_statement (parser);
30072   if (structured_block)
30073     {
30074       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30075         cp_lexer_consume_token (parser->lexer);
30076       else if (code == OMP_ATOMIC_CAPTURE_NEW)
30077         {
30078           cp_parser_skip_to_end_of_block_or_statement (parser);
30079           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30080             cp_lexer_consume_token (parser->lexer);
30081         }
30082     }
30083 }
30084
30085
30086 /* OpenMP 2.5:
30087    # pragma omp barrier new-line  */
30088
30089 static void
30090 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
30091 {
30092   cp_parser_require_pragma_eol (parser, pragma_tok);
30093   finish_omp_barrier ();
30094 }
30095
30096 /* OpenMP 2.5:
30097    # pragma omp critical [(name)] new-line
30098      structured-block  */
30099
30100 static tree
30101 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
30102 {
30103   tree stmt, name = NULL;
30104
30105   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30106     {
30107       cp_lexer_consume_token (parser->lexer);
30108
30109       name = cp_parser_identifier (parser);
30110
30111       if (name == error_mark_node
30112           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30113         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30114                                                /*or_comma=*/false,
30115                                                /*consume_paren=*/true);
30116       if (name == error_mark_node)
30117         name = NULL;
30118     }
30119   cp_parser_require_pragma_eol (parser, pragma_tok);
30120
30121   stmt = cp_parser_omp_structured_block (parser);
30122   return c_finish_omp_critical (input_location, stmt, name);
30123 }
30124
30125 /* OpenMP 2.5:
30126    # pragma omp flush flush-vars[opt] new-line
30127
30128    flush-vars:
30129      ( variable-list ) */
30130
30131 static void
30132 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
30133 {
30134   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30135     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30136   cp_parser_require_pragma_eol (parser, pragma_tok);
30137
30138   finish_omp_flush ();
30139 }
30140
30141 /* Helper function, to parse omp for increment expression.  */
30142
30143 static tree
30144 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
30145 {
30146   tree cond = cp_parser_binary_expression (parser, false, true,
30147                                            PREC_NOT_OPERATOR, NULL);
30148   if (cond == error_mark_node
30149       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30150     {
30151       cp_parser_skip_to_end_of_statement (parser);
30152       return error_mark_node;
30153     }
30154
30155   switch (TREE_CODE (cond))
30156     {
30157     case GT_EXPR:
30158     case GE_EXPR:
30159     case LT_EXPR:
30160     case LE_EXPR:
30161       break;
30162     case NE_EXPR:
30163       if (code == CILK_SIMD || code == CILK_FOR)
30164         break;
30165       /* Fall through: OpenMP disallows NE_EXPR.  */
30166     default:
30167       return error_mark_node;
30168     }
30169
30170   /* If decl is an iterator, preserve LHS and RHS of the relational
30171      expr until finish_omp_for.  */
30172   if (decl
30173       && (type_dependent_expression_p (decl)
30174           || CLASS_TYPE_P (TREE_TYPE (decl))))
30175     return cond;
30176
30177   return build_x_binary_op (input_location, TREE_CODE (cond),
30178                             TREE_OPERAND (cond, 0), ERROR_MARK,
30179                             TREE_OPERAND (cond, 1), ERROR_MARK,
30180                             /*overload=*/NULL, tf_warning_or_error);
30181 }
30182
30183 /* Helper function, to parse omp for increment expression.  */
30184
30185 static tree
30186 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
30187 {
30188   cp_token *token = cp_lexer_peek_token (parser->lexer);
30189   enum tree_code op;
30190   tree lhs, rhs;
30191   cp_id_kind idk;
30192   bool decl_first;
30193
30194   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30195     {
30196       op = (token->type == CPP_PLUS_PLUS
30197             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
30198       cp_lexer_consume_token (parser->lexer);
30199       lhs = cp_parser_simple_cast_expression (parser);
30200       if (lhs != decl)
30201         return error_mark_node;
30202       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30203     }
30204
30205   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
30206   if (lhs != decl)
30207     return error_mark_node;
30208
30209   token = cp_lexer_peek_token (parser->lexer);
30210   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
30211     {
30212       op = (token->type == CPP_PLUS_PLUS
30213             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
30214       cp_lexer_consume_token (parser->lexer);
30215       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
30216     }
30217
30218   op = cp_parser_assignment_operator_opt (parser);
30219   if (op == ERROR_MARK)
30220     return error_mark_node;
30221
30222   if (op != NOP_EXPR)
30223     {
30224       rhs = cp_parser_assignment_expression (parser);
30225       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
30226       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30227     }
30228
30229   lhs = cp_parser_binary_expression (parser, false, false,
30230                                      PREC_ADDITIVE_EXPRESSION, NULL);
30231   token = cp_lexer_peek_token (parser->lexer);
30232   decl_first = lhs == decl;
30233   if (decl_first)
30234     lhs = NULL_TREE;
30235   if (token->type != CPP_PLUS
30236       && token->type != CPP_MINUS)
30237     return error_mark_node;
30238
30239   do
30240     {
30241       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
30242       cp_lexer_consume_token (parser->lexer);
30243       rhs = cp_parser_binary_expression (parser, false, false,
30244                                          PREC_ADDITIVE_EXPRESSION, NULL);
30245       token = cp_lexer_peek_token (parser->lexer);
30246       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
30247         {
30248           if (lhs == NULL_TREE)
30249             {
30250               if (op == PLUS_EXPR)
30251                 lhs = rhs;
30252               else
30253                 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
30254                                         tf_warning_or_error);
30255             }
30256           else
30257             lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
30258                                      ERROR_MARK, NULL, tf_warning_or_error);
30259         }
30260     }
30261   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
30262
30263   if (!decl_first)
30264     {
30265       if (rhs != decl || op == MINUS_EXPR)
30266         return error_mark_node;
30267       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
30268     }
30269   else
30270     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
30271
30272   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
30273 }
30274
30275 /* Parse the initialization statement of either an OpenMP for loop or
30276    a Cilk Plus for loop.
30277
30278    Return true if the resulting construct should have an
30279    OMP_CLAUSE_PRIVATE added to it.  */
30280
30281 static bool
30282 cp_parser_omp_for_loop_init (cp_parser *parser,
30283                              enum tree_code code,
30284                              tree &this_pre_body,
30285                              vec<tree, va_gc> *for_block,
30286                              tree &init,
30287                              tree &decl,
30288                              tree &real_decl)
30289 {
30290   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30291     return false;
30292
30293   bool add_private_clause = false;
30294
30295   /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
30296
30297      init-expr:
30298      var = lb
30299      integer-type var = lb
30300      random-access-iterator-type var = lb
30301      pointer-type var = lb
30302   */
30303   cp_decl_specifier_seq type_specifiers;
30304
30305   /* First, try to parse as an initialized declaration.  See
30306      cp_parser_condition, from whence the bulk of this is copied.  */
30307
30308   cp_parser_parse_tentatively (parser);
30309   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
30310                                 /*is_trailing_return=*/false,
30311                                 &type_specifiers);
30312   if (cp_parser_parse_definitely (parser))
30313     {
30314       /* If parsing a type specifier seq succeeded, then this
30315          MUST be a initialized declaration.  */
30316       tree asm_specification, attributes;
30317       cp_declarator *declarator;
30318
30319       declarator = cp_parser_declarator (parser,
30320                                          CP_PARSER_DECLARATOR_NAMED,
30321                                          /*ctor_dtor_or_conv_p=*/NULL,
30322                                          /*parenthesized_p=*/NULL,
30323                                          /*member_p=*/false,
30324                                          /*friend_p=*/false);
30325       attributes = cp_parser_attributes_opt (parser);
30326       asm_specification = cp_parser_asm_specification_opt (parser);
30327
30328       if (declarator == cp_error_declarator) 
30329         cp_parser_skip_to_end_of_statement (parser);
30330
30331       else 
30332         {
30333           tree pushed_scope, auto_node;
30334
30335           decl = start_decl (declarator, &type_specifiers,
30336                              SD_INITIALIZED, attributes,
30337                              /*prefix_attributes=*/NULL_TREE,
30338                              &pushed_scope);
30339
30340           auto_node = type_uses_auto (TREE_TYPE (decl));
30341           if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30342             {
30343               if (cp_lexer_next_token_is (parser->lexer, 
30344                                           CPP_OPEN_PAREN))
30345                 {
30346                   if (code != CILK_SIMD && code != CILK_FOR)
30347                     error ("parenthesized initialization is not allowed in "
30348                            "OpenMP %<for%> loop");
30349                   else
30350                     error ("parenthesized initialization is "
30351                            "not allowed in for-loop");
30352                 }
30353               else
30354                 /* Trigger an error.  */
30355                 cp_parser_require (parser, CPP_EQ, RT_EQ);
30356
30357               init = error_mark_node;
30358               cp_parser_skip_to_end_of_statement (parser);
30359             }
30360           else if (CLASS_TYPE_P (TREE_TYPE (decl))
30361                    || type_dependent_expression_p (decl)
30362                    || auto_node)
30363             {
30364               bool is_direct_init, is_non_constant_init;
30365
30366               init = cp_parser_initializer (parser,
30367                                             &is_direct_init,
30368                                             &is_non_constant_init);
30369
30370               if (auto_node)
30371                 {
30372                   TREE_TYPE (decl)
30373                     = do_auto_deduction (TREE_TYPE (decl), init,
30374                                          auto_node);
30375
30376                   if (!CLASS_TYPE_P (TREE_TYPE (decl))
30377                       && !type_dependent_expression_p (decl))
30378                     goto non_class;
30379                 }
30380                       
30381               cp_finish_decl (decl, init, !is_non_constant_init,
30382                               asm_specification,
30383                               LOOKUP_ONLYCONVERTING);
30384               if (CLASS_TYPE_P (TREE_TYPE (decl)))
30385                 {
30386                   vec_safe_push (for_block, this_pre_body);
30387                   init = NULL_TREE;
30388                 }
30389               else
30390                 init = pop_stmt_list (this_pre_body);
30391               this_pre_body = NULL_TREE;
30392             }
30393           else
30394             {
30395               /* Consume '='.  */
30396               cp_lexer_consume_token (parser->lexer);
30397               init = cp_parser_assignment_expression (parser);
30398
30399             non_class:
30400               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
30401                 init = error_mark_node;
30402               else
30403                 cp_finish_decl (decl, NULL_TREE,
30404                                 /*init_const_expr_p=*/false,
30405                                 asm_specification,
30406                                 LOOKUP_ONLYCONVERTING);
30407             }
30408
30409           if (pushed_scope)
30410             pop_scope (pushed_scope);
30411         }
30412     }
30413   else 
30414     {
30415       cp_id_kind idk;
30416       /* If parsing a type specifier sequence failed, then
30417          this MUST be a simple expression.  */
30418       if (code == CILK_FOR)
30419         error ("%<_Cilk_for%> allows expression instead of declaration only "
30420                "in C, not in C++");
30421       cp_parser_parse_tentatively (parser);
30422       decl = cp_parser_primary_expression (parser, false, false,
30423                                            false, &idk);
30424       if (!cp_parser_error_occurred (parser)
30425           && decl
30426           && DECL_P (decl)
30427           && CLASS_TYPE_P (TREE_TYPE (decl)))
30428         {
30429           tree rhs;
30430
30431           cp_parser_parse_definitely (parser);
30432           cp_parser_require (parser, CPP_EQ, RT_EQ);
30433           rhs = cp_parser_assignment_expression (parser);
30434           finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
30435                                                  decl, NOP_EXPR,
30436                                                  rhs,
30437                                                  tf_warning_or_error));
30438           add_private_clause = true;
30439         }
30440       else
30441         {
30442           decl = NULL;
30443           cp_parser_abort_tentative_parse (parser);
30444           init = cp_parser_expression (parser);
30445           if (init)
30446             {
30447               if (TREE_CODE (init) == MODIFY_EXPR
30448                   || TREE_CODE (init) == MODOP_EXPR)
30449                 real_decl = TREE_OPERAND (init, 0);
30450             }
30451         }
30452     }
30453   return add_private_clause;
30454 }
30455
30456 /* Parse the restricted form of the for statement allowed by OpenMP.  */
30457
30458 static tree
30459 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
30460                         tree *cclauses)
30461 {
30462   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
30463   tree real_decl, initv, condv, incrv, declv;
30464   tree this_pre_body, cl;
30465   location_t loc_first;
30466   bool collapse_err = false;
30467   int i, collapse = 1, nbraces = 0;
30468   vec<tree, va_gc> *for_block = make_tree_vector ();
30469
30470   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
30471     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
30472       collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
30473
30474   gcc_assert (collapse >= 1);
30475
30476   declv = make_tree_vec (collapse);
30477   initv = make_tree_vec (collapse);
30478   condv = make_tree_vec (collapse);
30479   incrv = make_tree_vec (collapse);
30480
30481   loc_first = cp_lexer_peek_token (parser->lexer)->location;
30482
30483   for (i = 0; i < collapse; i++)
30484     {
30485       int bracecount = 0;
30486       bool add_private_clause = false;
30487       location_t loc;
30488
30489       if (code != CILK_FOR
30490           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30491         {
30492           cp_parser_error (parser, "for statement expected");
30493           return NULL;
30494         }
30495       if (code == CILK_FOR
30496           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
30497         {
30498           cp_parser_error (parser, "_Cilk_for statement expected");
30499           return NULL;
30500         }
30501       loc = cp_lexer_consume_token (parser->lexer)->location;
30502
30503       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30504         return NULL;
30505
30506       init = decl = real_decl = NULL;
30507       this_pre_body = push_stmt_list ();
30508
30509       add_private_clause
30510         |= cp_parser_omp_for_loop_init (parser, code,
30511                                         this_pre_body, for_block,
30512                                         init, decl, real_decl);
30513
30514       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30515       if (this_pre_body)
30516         {
30517           this_pre_body = pop_stmt_list (this_pre_body);
30518           if (pre_body)
30519             {
30520               tree t = pre_body;
30521               pre_body = push_stmt_list ();
30522               add_stmt (t);
30523               add_stmt (this_pre_body);
30524               pre_body = pop_stmt_list (pre_body);
30525             }
30526           else
30527             pre_body = this_pre_body;
30528         }
30529
30530       if (decl)
30531         real_decl = decl;
30532       if (cclauses != NULL
30533           && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
30534           && real_decl != NULL_TREE)
30535         {
30536           tree *c;
30537           for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
30538             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
30539                 && OMP_CLAUSE_DECL (*c) == real_decl)
30540               {
30541                 error_at (loc, "iteration variable %qD"
30542                           " should not be firstprivate", real_decl);
30543                 *c = OMP_CLAUSE_CHAIN (*c);
30544               }
30545             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
30546                      && OMP_CLAUSE_DECL (*c) == real_decl)
30547               {
30548                 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES.  */
30549                 tree l = *c;
30550                 *c = OMP_CLAUSE_CHAIN (*c);
30551                 if (code == OMP_SIMD)
30552                   {
30553                     OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30554                     cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
30555                   }
30556                 else
30557                   {
30558                     OMP_CLAUSE_CHAIN (l) = clauses;
30559                     clauses = l;
30560                   }
30561                 add_private_clause = false;
30562               }
30563             else
30564               {
30565                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
30566                     && OMP_CLAUSE_DECL (*c) == real_decl)
30567                   add_private_clause = false;
30568                 c = &OMP_CLAUSE_CHAIN (*c);
30569               }
30570         }
30571
30572       if (add_private_clause)
30573         {
30574           tree c;
30575           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30576             {
30577               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
30578                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
30579                   && OMP_CLAUSE_DECL (c) == decl)
30580                 break;
30581               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
30582                        && OMP_CLAUSE_DECL (c) == decl)
30583                 error_at (loc, "iteration variable %qD "
30584                           "should not be firstprivate",
30585                           decl);
30586               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
30587                        && OMP_CLAUSE_DECL (c) == decl)
30588                 error_at (loc, "iteration variable %qD should not be reduction",
30589                           decl);
30590             }
30591           if (c == NULL)
30592             {
30593               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
30594               OMP_CLAUSE_DECL (c) = decl;
30595               c = finish_omp_clauses (c);
30596               if (c)
30597                 {
30598                   OMP_CLAUSE_CHAIN (c) = clauses;
30599                   clauses = c;
30600                 }
30601             }
30602         }
30603
30604       cond = NULL;
30605       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30606         cond = cp_parser_omp_for_cond (parser, decl, code);
30607       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30608
30609       incr = NULL;
30610       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30611         {
30612           /* If decl is an iterator, preserve the operator on decl
30613              until finish_omp_for.  */
30614           if (real_decl
30615               && ((processing_template_decl
30616                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
30617                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
30618             incr = cp_parser_omp_for_incr (parser, real_decl);
30619           else
30620             incr = cp_parser_expression (parser);
30621           if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
30622             SET_EXPR_LOCATION (incr, input_location);
30623         }
30624
30625       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30626         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30627                                                /*or_comma=*/false,
30628                                                /*consume_paren=*/true);
30629
30630       TREE_VEC_ELT (declv, i) = decl;
30631       TREE_VEC_ELT (initv, i) = init;
30632       TREE_VEC_ELT (condv, i) = cond;
30633       TREE_VEC_ELT (incrv, i) = incr;
30634
30635       if (i == collapse - 1)
30636         break;
30637
30638       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
30639          in between the collapsed for loops to be still considered perfectly
30640          nested.  Hopefully the final version clarifies this.
30641          For now handle (multiple) {'s and empty statements.  */
30642       cp_parser_parse_tentatively (parser);
30643       do
30644         {
30645           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30646             break;
30647           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30648             {
30649               cp_lexer_consume_token (parser->lexer);
30650               bracecount++;
30651             }
30652           else if (bracecount
30653                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30654             cp_lexer_consume_token (parser->lexer);
30655           else
30656             {
30657               loc = cp_lexer_peek_token (parser->lexer)->location;
30658               error_at (loc, "not enough collapsed for loops");
30659               collapse_err = true;
30660               cp_parser_abort_tentative_parse (parser);
30661               declv = NULL_TREE;
30662               break;
30663             }
30664         }
30665       while (1);
30666
30667       if (declv)
30668         {
30669           cp_parser_parse_definitely (parser);
30670           nbraces += bracecount;
30671         }
30672     }
30673
30674   /* Note that we saved the original contents of this flag when we entered
30675      the structured block, and so we don't need to re-save it here.  */
30676   if (code == CILK_SIMD || code == CILK_FOR)
30677     parser->in_statement = IN_CILK_SIMD_FOR;
30678   else
30679     parser->in_statement = IN_OMP_FOR;
30680
30681   /* Note that the grammar doesn't call for a structured block here,
30682      though the loop as a whole is a structured block.  */
30683   body = push_stmt_list ();
30684   cp_parser_statement (parser, NULL_TREE, false, NULL);
30685   body = pop_stmt_list (body);
30686
30687   if (declv == NULL_TREE)
30688     ret = NULL_TREE;
30689   else
30690     ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
30691                           pre_body, clauses);
30692
30693   while (nbraces)
30694     {
30695       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30696         {
30697           cp_lexer_consume_token (parser->lexer);
30698           nbraces--;
30699         }
30700       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30701         cp_lexer_consume_token (parser->lexer);
30702       else
30703         {
30704           if (!collapse_err)
30705             {
30706               error_at (cp_lexer_peek_token (parser->lexer)->location,
30707                         "collapsed loops not perfectly nested");
30708             }
30709           collapse_err = true;
30710           cp_parser_statement_seq_opt (parser, NULL);
30711           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
30712             break;
30713         }
30714     }
30715
30716   while (!for_block->is_empty ())
30717     add_stmt (pop_stmt_list (for_block->pop ()));
30718   release_tree_vector (for_block);
30719
30720   return ret;
30721 }
30722
30723 /* Helper function for OpenMP parsing, split clauses and call
30724    finish_omp_clauses on each of the set of clauses afterwards.  */
30725
30726 static void
30727 cp_omp_split_clauses (location_t loc, enum tree_code code,
30728                       omp_clause_mask mask, tree clauses, tree *cclauses)
30729 {
30730   int i;
30731   c_omp_split_clauses (loc, code, mask, clauses, cclauses);
30732   for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
30733     if (cclauses[i])
30734       cclauses[i] = finish_omp_clauses (cclauses[i]);
30735 }
30736
30737 /* OpenMP 4.0:
30738    #pragma omp simd simd-clause[optseq] new-line
30739      for-loop  */
30740
30741 #define OMP_SIMD_CLAUSE_MASK                                    \
30742         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN)      \
30743         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
30744         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
30745         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30746         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30747         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30748         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30749
30750 static tree
30751 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
30752                     char *p_name, omp_clause_mask mask, tree *cclauses)
30753 {
30754   tree clauses, sb, ret;
30755   unsigned int save;
30756   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30757
30758   strcat (p_name, " simd");
30759   mask |= OMP_SIMD_CLAUSE_MASK;
30760   mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
30761
30762   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30763                                        cclauses == NULL);
30764   if (cclauses)
30765     {
30766       cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
30767       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
30768     }
30769
30770   sb = begin_omp_structured_block ();
30771   save = cp_parser_begin_omp_structured_block (parser);
30772
30773   ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
30774
30775   cp_parser_end_omp_structured_block (parser, save);
30776   add_stmt (finish_omp_structured_block (sb));
30777
30778   return ret;
30779 }
30780
30781 /* OpenMP 2.5:
30782    #pragma omp for for-clause[optseq] new-line
30783      for-loop
30784
30785    OpenMP 4.0:
30786    #pragma omp for simd for-simd-clause[optseq] new-line
30787      for-loop  */
30788
30789 #define OMP_FOR_CLAUSE_MASK                                     \
30790         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30791         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30792         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30793         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30794         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED)      \
30795         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE)     \
30796         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT)       \
30797         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30798
30799 static tree
30800 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
30801                    char *p_name, omp_clause_mask mask, tree *cclauses)
30802 {
30803   tree clauses, sb, ret;
30804   unsigned int save;
30805   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30806
30807   strcat (p_name, " for");
30808   mask |= OMP_FOR_CLAUSE_MASK;
30809   if (cclauses)
30810     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30811
30812   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30813     {
30814       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30815       const char *p = IDENTIFIER_POINTER (id);
30816
30817       if (strcmp (p, "simd") == 0)
30818         {
30819           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30820           if (cclauses == NULL)
30821             cclauses = cclauses_buf;
30822
30823           cp_lexer_consume_token (parser->lexer);
30824           if (!flag_openmp)  /* flag_openmp_simd  */
30825             return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30826                                        cclauses);
30827           sb = begin_omp_structured_block ();
30828           save = cp_parser_begin_omp_structured_block (parser);
30829           ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30830                                     cclauses);
30831           cp_parser_end_omp_structured_block (parser, save);
30832           tree body = finish_omp_structured_block (sb);
30833           if (ret == NULL)
30834             return ret;
30835           ret = make_node (OMP_FOR);
30836           TREE_TYPE (ret) = void_type_node;
30837           OMP_FOR_BODY (ret) = body;
30838           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30839           SET_EXPR_LOCATION (ret, loc);
30840           add_stmt (ret);
30841           return ret;
30842         }
30843     }
30844   if (!flag_openmp)  /* flag_openmp_simd  */
30845     {
30846       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30847       return NULL_TREE;
30848     }
30849
30850   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30851                                        cclauses == NULL);
30852   if (cclauses)
30853     {
30854       cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
30855       clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
30856     }
30857
30858   sb = begin_omp_structured_block ();
30859   save = cp_parser_begin_omp_structured_block (parser);
30860
30861   ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
30862
30863   cp_parser_end_omp_structured_block (parser, save);
30864   add_stmt (finish_omp_structured_block (sb));
30865
30866   return ret;
30867 }
30868
30869 /* OpenMP 2.5:
30870    # pragma omp master new-line
30871      structured-block  */
30872
30873 static tree
30874 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
30875 {
30876   cp_parser_require_pragma_eol (parser, pragma_tok);
30877   return c_finish_omp_master (input_location,
30878                               cp_parser_omp_structured_block (parser));
30879 }
30880
30881 /* OpenMP 2.5:
30882    # pragma omp ordered new-line
30883      structured-block  */
30884
30885 static tree
30886 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
30887 {
30888   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30889   cp_parser_require_pragma_eol (parser, pragma_tok);
30890   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
30891 }
30892
30893 /* OpenMP 2.5:
30894
30895    section-scope:
30896      { section-sequence }
30897
30898    section-sequence:
30899      section-directive[opt] structured-block
30900      section-sequence section-directive structured-block  */
30901
30902 static tree
30903 cp_parser_omp_sections_scope (cp_parser *parser)
30904 {
30905   tree stmt, substmt;
30906   bool error_suppress = false;
30907   cp_token *tok;
30908
30909   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
30910     return NULL_TREE;
30911
30912   stmt = push_stmt_list ();
30913
30914   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30915     {
30916       substmt = cp_parser_omp_structured_block (parser);
30917       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30918       add_stmt (substmt);
30919     }
30920
30921   while (1)
30922     {
30923       tok = cp_lexer_peek_token (parser->lexer);
30924       if (tok->type == CPP_CLOSE_BRACE)
30925         break;
30926       if (tok->type == CPP_EOF)
30927         break;
30928
30929       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30930         {
30931           cp_lexer_consume_token (parser->lexer);
30932           cp_parser_require_pragma_eol (parser, tok);
30933           error_suppress = false;
30934         }
30935       else if (!error_suppress)
30936         {
30937           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30938           error_suppress = true;
30939         }
30940
30941       substmt = cp_parser_omp_structured_block (parser);
30942       substmt = build1 (OMP_SECTION, void_type_node, substmt);
30943       add_stmt (substmt);
30944     }
30945   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30946
30947   substmt = pop_stmt_list (stmt);
30948
30949   stmt = make_node (OMP_SECTIONS);
30950   TREE_TYPE (stmt) = void_type_node;
30951   OMP_SECTIONS_BODY (stmt) = substmt;
30952
30953   add_stmt (stmt);
30954   return stmt;
30955 }
30956
30957 /* OpenMP 2.5:
30958    # pragma omp sections sections-clause[optseq] newline
30959      sections-scope  */
30960
30961 #define OMP_SECTIONS_CLAUSE_MASK                                \
30962         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
30963         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30964         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE)  \
30965         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
30966         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30967
30968 static tree
30969 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30970                         char *p_name, omp_clause_mask mask, tree *cclauses)
30971 {
30972   tree clauses, ret;
30973   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30974
30975   strcat (p_name, " sections");
30976   mask |= OMP_SECTIONS_CLAUSE_MASK;
30977   if (cclauses)
30978     mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30979
30980   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30981                                        cclauses == NULL);
30982   if (cclauses)
30983     {
30984       cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30985       clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30986     }
30987
30988   ret = cp_parser_omp_sections_scope (parser);
30989   if (ret)
30990     OMP_SECTIONS_CLAUSES (ret) = clauses;
30991
30992   return ret;
30993 }
30994
30995 /* OpenMP 2.5:
30996    # pragma omp parallel parallel-clause[optseq] new-line
30997      structured-block
30998    # pragma omp parallel for parallel-for-clause[optseq] new-line
30999      structured-block
31000    # pragma omp parallel sections parallel-sections-clause[optseq] new-line
31001      structured-block
31002
31003    OpenMP 4.0:
31004    # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
31005      structured-block */
31006
31007 #define OMP_PARALLEL_CLAUSE_MASK                                \
31008         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
31009         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31010         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31011         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
31012         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31013         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN)       \
31014         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
31015         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)  \
31016         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
31017
31018 static tree
31019 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
31020                         char *p_name, omp_clause_mask mask, tree *cclauses)
31021 {
31022   tree stmt, clauses, block;
31023   unsigned int save;
31024   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31025
31026   strcat (p_name, " parallel");
31027   mask |= OMP_PARALLEL_CLAUSE_MASK;
31028
31029   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31030     {
31031       tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31032       if (cclauses == NULL)
31033         cclauses = cclauses_buf;
31034
31035       cp_lexer_consume_token (parser->lexer);
31036       if (!flag_openmp)  /* flag_openmp_simd  */
31037         return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31038       block = begin_omp_parallel ();
31039       save = cp_parser_begin_omp_structured_block (parser);
31040       tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
31041       cp_parser_end_omp_structured_block (parser, save);
31042       stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31043                                   block);
31044       if (ret == NULL_TREE)
31045         return ret;
31046       OMP_PARALLEL_COMBINED (stmt) = 1;
31047       return stmt;
31048     }
31049   else if (cclauses)
31050     {
31051       error_at (loc, "expected %<for%> after %qs", p_name);
31052       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31053       return NULL_TREE;
31054     }
31055   else if (!flag_openmp)  /* flag_openmp_simd  */
31056     {
31057       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31058       return NULL_TREE;
31059     }
31060   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31061     {
31062       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31063       const char *p = IDENTIFIER_POINTER (id);
31064       if (strcmp (p, "sections") == 0)
31065         {
31066           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31067           cclauses = cclauses_buf;
31068
31069           cp_lexer_consume_token (parser->lexer);
31070           block = begin_omp_parallel ();
31071           save = cp_parser_begin_omp_structured_block (parser);
31072           cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
31073           cp_parser_end_omp_structured_block (parser, save);
31074           stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
31075                                       block);
31076           OMP_PARALLEL_COMBINED (stmt) = 1;
31077           return stmt;
31078         }
31079     }
31080
31081   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
31082
31083   block = begin_omp_parallel ();
31084   save = cp_parser_begin_omp_structured_block (parser);
31085   cp_parser_statement (parser, NULL_TREE, false, NULL);
31086   cp_parser_end_omp_structured_block (parser, save);
31087   stmt = finish_omp_parallel (clauses, block);
31088   return stmt;
31089 }
31090
31091 /* OpenMP 2.5:
31092    # pragma omp single single-clause[optseq] new-line
31093      structured-block  */
31094
31095 #define OMP_SINGLE_CLAUSE_MASK                                  \
31096         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31097         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31098         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE)  \
31099         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
31100
31101 static tree
31102 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
31103 {
31104   tree stmt = make_node (OMP_SINGLE);
31105   TREE_TYPE (stmt) = void_type_node;
31106
31107   OMP_SINGLE_CLAUSES (stmt)
31108     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
31109                                  "#pragma omp single", pragma_tok);
31110   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
31111
31112   return add_stmt (stmt);
31113 }
31114
31115 /* OpenMP 3.0:
31116    # pragma omp task task-clause[optseq] new-line
31117      structured-block  */
31118
31119 #define OMP_TASK_CLAUSE_MASK                                    \
31120         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF)           \
31121         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED)       \
31122         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT)      \
31123         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31124         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31125         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31126         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL)        \
31127         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE)    \
31128         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
31129
31130 static tree
31131 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
31132 {
31133   tree clauses, block;
31134   unsigned int save;
31135
31136   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
31137                                        "#pragma omp task", pragma_tok);
31138   block = begin_omp_task ();
31139   save = cp_parser_begin_omp_structured_block (parser);
31140   cp_parser_statement (parser, NULL_TREE, false, NULL);
31141   cp_parser_end_omp_structured_block (parser, save);
31142   return finish_omp_task (clauses, block);
31143 }
31144
31145 /* OpenMP 3.0:
31146    # pragma omp taskwait new-line  */
31147
31148 static void
31149 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
31150 {
31151   cp_parser_require_pragma_eol (parser, pragma_tok);
31152   finish_omp_taskwait ();
31153 }
31154
31155 /* OpenMP 3.1:
31156    # pragma omp taskyield new-line  */
31157
31158 static void
31159 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
31160 {
31161   cp_parser_require_pragma_eol (parser, pragma_tok);
31162   finish_omp_taskyield ();
31163 }
31164
31165 /* OpenMP 4.0:
31166    # pragma omp taskgroup new-line
31167      structured-block  */
31168
31169 static tree
31170 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
31171 {
31172   cp_parser_require_pragma_eol (parser, pragma_tok);
31173   return c_finish_omp_taskgroup (input_location,
31174                                  cp_parser_omp_structured_block (parser));
31175 }
31176
31177
31178 /* OpenMP 2.5:
31179    # pragma omp threadprivate (variable-list) */
31180
31181 static void
31182 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
31183 {
31184   tree vars;
31185
31186   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31187   cp_parser_require_pragma_eol (parser, pragma_tok);
31188
31189   finish_omp_threadprivate (vars);
31190 }
31191
31192 /* OpenMP 4.0:
31193    # pragma omp cancel cancel-clause[optseq] new-line  */
31194
31195 #define OMP_CANCEL_CLAUSE_MASK                                  \
31196         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31197         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31198         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31199         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP)    \
31200         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31201
31202 static void
31203 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
31204 {
31205   tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
31206                                             "#pragma omp cancel", pragma_tok);
31207   finish_omp_cancel (clauses);
31208 }
31209
31210 /* OpenMP 4.0:
31211    # pragma omp cancellation point cancelpt-clause[optseq] new-line  */
31212
31213 #define OMP_CANCELLATION_POINT_CLAUSE_MASK                      \
31214         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL)     \
31215         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR)          \
31216         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS)     \
31217         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
31218
31219 static void
31220 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
31221 {
31222   tree clauses;
31223   bool point_seen = false;
31224
31225   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31226     {
31227       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31228       const char *p = IDENTIFIER_POINTER (id);
31229
31230       if (strcmp (p, "point") == 0)
31231         {
31232           cp_lexer_consume_token (parser->lexer);
31233           point_seen = true;
31234         }
31235     }
31236   if (!point_seen)
31237     {
31238       cp_parser_error (parser, "expected %<point%>");
31239       cp_parser_require_pragma_eol (parser, pragma_tok);
31240       return;
31241     }
31242
31243   clauses = cp_parser_omp_all_clauses (parser,
31244                                        OMP_CANCELLATION_POINT_CLAUSE_MASK,
31245                                        "#pragma omp cancellation point",
31246                                        pragma_tok);
31247   finish_omp_cancellation_point (clauses);
31248 }
31249
31250 /* OpenMP 4.0:
31251    #pragma omp distribute distribute-clause[optseq] new-line
31252      for-loop  */
31253
31254 #define OMP_DISTRIBUTE_CLAUSE_MASK                              \
31255         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31256         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31257         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
31258         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
31259
31260 static tree
31261 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
31262                           char *p_name, omp_clause_mask mask, tree *cclauses)
31263 {
31264   tree clauses, sb, ret;
31265   unsigned int save;
31266   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31267
31268   strcat (p_name, " distribute");
31269   mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
31270
31271   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31272     {
31273       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31274       const char *p = IDENTIFIER_POINTER (id);
31275       bool simd = false;
31276       bool parallel = false;
31277
31278       if (strcmp (p, "simd") == 0)
31279         simd = true;
31280       else
31281         parallel = strcmp (p, "parallel") == 0;
31282       if (parallel || simd)
31283         {
31284           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31285           if (cclauses == NULL)
31286             cclauses = cclauses_buf;
31287           cp_lexer_consume_token (parser->lexer);
31288           if (!flag_openmp)  /* flag_openmp_simd  */
31289             {
31290               if (simd)
31291                 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31292                                            cclauses);
31293               else
31294                 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31295                                                cclauses);
31296             }
31297           sb = begin_omp_structured_block ();
31298           save = cp_parser_begin_omp_structured_block (parser);
31299           if (simd)
31300             ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
31301                                       cclauses);
31302           else
31303             ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
31304                                           cclauses);
31305           cp_parser_end_omp_structured_block (parser, save);
31306           tree body = finish_omp_structured_block (sb);
31307           if (ret == NULL)
31308             return ret;
31309           ret = make_node (OMP_DISTRIBUTE);
31310           TREE_TYPE (ret) = void_type_node;
31311           OMP_FOR_BODY (ret) = body;
31312           OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31313           SET_EXPR_LOCATION (ret, loc);
31314           add_stmt (ret);
31315           return ret;
31316         }
31317     }
31318   if (!flag_openmp)  /* flag_openmp_simd  */
31319     {
31320       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31321       return NULL_TREE;
31322     }
31323
31324   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31325                                        cclauses == NULL);
31326   if (cclauses)
31327     {
31328       cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
31329       clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
31330     }
31331
31332   sb = begin_omp_structured_block ();
31333   save = cp_parser_begin_omp_structured_block (parser);
31334
31335   ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
31336
31337   cp_parser_end_omp_structured_block (parser, save);
31338   add_stmt (finish_omp_structured_block (sb));
31339
31340   return ret;
31341 }
31342
31343 /* OpenMP 4.0:
31344    # pragma omp teams teams-clause[optseq] new-line
31345      structured-block  */
31346
31347 #define OMP_TEAMS_CLAUSE_MASK                                   \
31348         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE)      \
31349         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
31350         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED)       \
31351         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION)    \
31352         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS)    \
31353         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
31354         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
31355
31356 static tree
31357 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
31358                      char *p_name, omp_clause_mask mask, tree *cclauses)
31359 {
31360   tree clauses, sb, ret;
31361   unsigned int save;
31362   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31363
31364   strcat (p_name, " teams");
31365   mask |= OMP_TEAMS_CLAUSE_MASK;
31366
31367   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31368     {
31369       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31370       const char *p = IDENTIFIER_POINTER (id);
31371       if (strcmp (p, "distribute") == 0)
31372         {
31373           tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
31374           if (cclauses == NULL)
31375             cclauses = cclauses_buf;
31376
31377           cp_lexer_consume_token (parser->lexer);
31378           if (!flag_openmp)  /* flag_openmp_simd  */
31379             return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31380                                              cclauses);
31381           sb = begin_omp_structured_block ();
31382           save = cp_parser_begin_omp_structured_block (parser);
31383           ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
31384                                           cclauses);
31385           cp_parser_end_omp_structured_block (parser, save);
31386           tree body = finish_omp_structured_block (sb);
31387           if (ret == NULL)
31388             return ret;
31389           clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31390           ret = make_node (OMP_TEAMS);
31391           TREE_TYPE (ret) = void_type_node;
31392           OMP_TEAMS_CLAUSES (ret) = clauses;
31393           OMP_TEAMS_BODY (ret) = body;
31394           OMP_TEAMS_COMBINED (ret) = 1;
31395           return add_stmt (ret);
31396         }
31397     }
31398   if (!flag_openmp)  /* flag_openmp_simd  */
31399     {
31400       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31401       return NULL_TREE;
31402     }
31403
31404   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
31405                                        cclauses == NULL);
31406   if (cclauses)
31407     {
31408       cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
31409       clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
31410     }
31411
31412   tree stmt = make_node (OMP_TEAMS);
31413   TREE_TYPE (stmt) = void_type_node;
31414   OMP_TEAMS_CLAUSES (stmt) = clauses;
31415   OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
31416
31417   return add_stmt (stmt);
31418 }
31419
31420 /* OpenMP 4.0:
31421    # pragma omp target data target-data-clause[optseq] new-line
31422      structured-block  */
31423
31424 #define OMP_TARGET_DATA_CLAUSE_MASK                             \
31425         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31426         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31427         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31428
31429 static tree
31430 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
31431 {
31432   tree stmt = make_node (OMP_TARGET_DATA);
31433   TREE_TYPE (stmt) = void_type_node;
31434
31435   OMP_TARGET_DATA_CLAUSES (stmt)
31436     = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
31437                                  "#pragma omp target data", pragma_tok);
31438   keep_next_level (true);
31439   OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
31440
31441   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31442   return add_stmt (stmt);
31443 }
31444
31445 /* OpenMP 4.0:
31446    # pragma omp target update target-update-clause[optseq] new-line */
31447
31448 #define OMP_TARGET_UPDATE_CLAUSE_MASK                           \
31449         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM)         \
31450         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO)           \
31451         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31452         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31453
31454 static bool
31455 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
31456                              enum pragma_context context)
31457 {
31458   if (context == pragma_stmt)
31459     {
31460       error_at (pragma_tok->location,
31461                 "%<#pragma omp target update%> may only be "
31462                 "used in compound statements");
31463       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31464       return false;
31465     }
31466
31467   tree clauses
31468     = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
31469                                  "#pragma omp target update", pragma_tok);
31470   if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
31471       && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
31472     {
31473       error_at (pragma_tok->location,
31474                 "%<#pragma omp target update%> must contain at least one "
31475                 "%<from%> or %<to%> clauses");
31476       return false;
31477     }
31478
31479   tree stmt = make_node (OMP_TARGET_UPDATE);
31480   TREE_TYPE (stmt) = void_type_node;
31481   OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
31482   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31483   add_stmt (stmt);
31484   return false;
31485 }
31486
31487 /* OpenMP 4.0:
31488    # pragma omp target target-clause[optseq] new-line
31489      structured-block  */
31490
31491 #define OMP_TARGET_CLAUSE_MASK                                  \
31492         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE)       \
31493         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)          \
31494         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
31495
31496 static bool
31497 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
31498                       enum pragma_context context)
31499 {
31500   if (context != pragma_stmt && context != pragma_compound)
31501     {
31502       cp_parser_error (parser, "expected declaration specifiers");
31503       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31504       return false;
31505     }
31506
31507   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31508     {
31509       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31510       const char *p = IDENTIFIER_POINTER (id);
31511
31512       if (strcmp (p, "teams") == 0)
31513         {
31514           tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
31515           char p_name[sizeof ("#pragma omp target teams distribute "
31516                               "parallel for simd")];
31517
31518           cp_lexer_consume_token (parser->lexer);
31519           strcpy (p_name, "#pragma omp target");
31520           if (!flag_openmp)  /* flag_openmp_simd  */
31521             {
31522               tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
31523                                                OMP_TARGET_CLAUSE_MASK,
31524                                                cclauses);
31525               return stmt != NULL_TREE;
31526             }
31527           keep_next_level (true);
31528           tree sb = begin_omp_structured_block ();
31529           unsigned save = cp_parser_begin_omp_structured_block (parser);
31530           tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
31531                                           OMP_TARGET_CLAUSE_MASK, cclauses);
31532           cp_parser_end_omp_structured_block (parser, save);
31533           tree body = finish_omp_structured_block (sb);
31534           if (ret == NULL_TREE)
31535             return false;
31536           tree stmt = make_node (OMP_TARGET);
31537           TREE_TYPE (stmt) = void_type_node;
31538           OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
31539           OMP_TARGET_BODY (stmt) = body;
31540           add_stmt (stmt);
31541           return true;
31542         }
31543       else if (!flag_openmp)  /* flag_openmp_simd  */
31544         {
31545           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31546           return false;
31547         }
31548       else if (strcmp (p, "data") == 0)
31549         {
31550           cp_lexer_consume_token (parser->lexer);
31551           cp_parser_omp_target_data (parser, pragma_tok);
31552           return true;
31553         }
31554       else if (strcmp (p, "update") == 0)
31555         {
31556           cp_lexer_consume_token (parser->lexer);
31557           return cp_parser_omp_target_update (parser, pragma_tok, context);
31558         }
31559     }
31560
31561   tree stmt = make_node (OMP_TARGET);
31562   TREE_TYPE (stmt) = void_type_node;
31563
31564   OMP_TARGET_CLAUSES (stmt)
31565     = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
31566                                  "#pragma omp target", pragma_tok);
31567   keep_next_level (true);
31568   OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
31569
31570   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31571   add_stmt (stmt);
31572   return true;
31573 }
31574
31575 /* OpenACC 2.0:
31576    # pragma acc cache (variable-list) new-line
31577 */
31578
31579 static tree
31580 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
31581 {
31582   tree stmt, clauses;
31583
31584   clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
31585   clauses = finish_omp_clauses (clauses);
31586
31587   cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
31588
31589   stmt = make_node (OACC_CACHE);
31590   TREE_TYPE (stmt) = void_type_node;
31591   OACC_CACHE_CLAUSES (stmt) = clauses;
31592   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31593   add_stmt (stmt);
31594
31595   return stmt;
31596 }
31597
31598 /* OpenACC 2.0:
31599    # pragma acc data oacc-data-clause[optseq] new-line
31600      structured-block  */
31601
31602 #define OACC_DATA_CLAUSE_MASK                                           \
31603         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31604         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31605         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31606         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31607         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31608         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31609         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31610         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31611         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31612         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31613         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
31614
31615 static tree
31616 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok)
31617 {
31618   tree stmt, clauses, block;
31619   unsigned int save;
31620
31621   clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
31622                                         "#pragma acc data", pragma_tok);
31623
31624   block = begin_omp_parallel ();
31625   save = cp_parser_begin_omp_structured_block (parser);
31626   cp_parser_statement (parser, NULL_TREE, false, NULL);
31627   cp_parser_end_omp_structured_block (parser, save);
31628   stmt = finish_oacc_data (clauses, block);
31629   return stmt;
31630 }
31631
31632 /* OpenACC 2.0:
31633    # pragma acc enter data oacc-enter-data-clause[optseq] new-line
31634
31635    or
31636
31637    # pragma acc exit data oacc-exit-data-clause[optseq] new-line
31638
31639    LOC is the location of the #pragma token.
31640 */
31641
31642 #define OACC_ENTER_DATA_CLAUSE_MASK                                     \
31643         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31644         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31645         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31646         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31647         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31648         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31649         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31650
31651 #define OACC_EXIT_DATA_CLAUSE_MASK                                      \
31652         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31653         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31654         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31655         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE)              \
31656         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
31657
31658 static tree
31659 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
31660                                 bool enter)
31661 {
31662   tree stmt, clauses;
31663
31664   if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
31665      || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31666     {
31667       cp_parser_error (parser, enter
31668                        ? "expected %<data%> in %<#pragma acc enter data%>"
31669                        : "expected %<data%> in %<#pragma acc exit data%>");
31670       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31671       return NULL_TREE;
31672     }
31673
31674   const char *p =
31675     IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
31676   if (strcmp (p, "data") != 0)
31677     {
31678       cp_parser_error (parser, "invalid pragma");
31679       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31680       return NULL_TREE;
31681     }
31682
31683   cp_lexer_consume_token (parser->lexer);
31684
31685   if (enter)
31686     clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
31687                                          "#pragma acc enter data", pragma_tok);
31688   else
31689     clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
31690                                          "#pragma acc exit data", pragma_tok);
31691
31692   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31693     {
31694       error_at (pragma_tok->location,
31695                 "%<#pragma acc enter data%> has no data movement clause");
31696       return NULL_TREE;
31697     }
31698
31699   stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
31700   TREE_TYPE (stmt) = void_type_node;
31701   if (enter)
31702     OACC_ENTER_DATA_CLAUSES (stmt) = clauses;
31703   else
31704     OACC_EXIT_DATA_CLAUSES (stmt) = clauses;
31705   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31706   add_stmt (stmt);
31707   return stmt;
31708 }
31709
31710 /* OpenACC 2.0:
31711    # pragma acc kernels oacc-kernels-clause[optseq] new-line
31712      structured-block  */
31713
31714 #define OACC_KERNELS_CLAUSE_MASK                                        \
31715         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31716         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31717         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31718         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31719         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31720         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31721         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31722         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31723         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31724         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31725         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31726         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31727         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31728
31729 static tree
31730 cp_parser_oacc_kernels (cp_parser *parser, cp_token *pragma_tok)
31731 {
31732   tree stmt, clauses, block;
31733   unsigned int save;
31734
31735   clauses = cp_parser_oacc_all_clauses (parser, OACC_KERNELS_CLAUSE_MASK,
31736                                         "#pragma acc kernels", pragma_tok);
31737
31738   block = begin_omp_parallel ();
31739   save = cp_parser_begin_omp_structured_block (parser);
31740   cp_parser_statement (parser, NULL_TREE, false, NULL);
31741   cp_parser_end_omp_structured_block (parser, save);
31742   stmt = finish_oacc_kernels (clauses, block);
31743   return stmt;
31744 }
31745
31746 /* OpenACC 2.0:
31747    # pragma acc loop oacc-loop-clause[optseq] new-line
31748      structured-block  */
31749
31750 #define OACC_LOOP_CLAUSE_MASK                                           \
31751         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE)            \
31752         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION))
31753
31754 static tree
31755 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok)
31756 {
31757   tree stmt, clauses, block;
31758   int save;
31759
31760   clauses = cp_parser_oacc_all_clauses (parser, OACC_LOOP_CLAUSE_MASK,
31761                                         "#pragma acc loop", pragma_tok);
31762
31763   block = begin_omp_structured_block ();
31764   save = cp_parser_begin_omp_structured_block (parser);
31765   stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL);
31766   cp_parser_end_omp_structured_block (parser, save);
31767   add_stmt (finish_omp_structured_block (block));
31768   return stmt;
31769 }
31770
31771 /* OpenACC 2.0:
31772    # pragma acc parallel oacc-parallel-clause[optseq] new-line
31773      structured-block  */
31774
31775 #define OACC_PARALLEL_CLAUSE_MASK                                       \
31776         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31777         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY)                \
31778         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN)              \
31779         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT)             \
31780         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE)              \
31781         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR)           \
31782         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31783         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS)           \
31784         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS)         \
31785         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT)             \
31786         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY)     \
31787         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN)   \
31788         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT)  \
31789         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE)   \
31790         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION)           \
31791         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH)       \
31792         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31793
31794 static tree
31795 cp_parser_oacc_parallel (cp_parser *parser, cp_token *pragma_tok)
31796 {
31797   tree stmt, clauses, block;
31798   unsigned int save;
31799
31800   clauses = cp_parser_oacc_all_clauses (parser, OACC_PARALLEL_CLAUSE_MASK,
31801                                          "#pragma acc parallel", pragma_tok);
31802
31803   block = begin_omp_parallel ();
31804   save = cp_parser_begin_omp_structured_block (parser);
31805   cp_parser_statement (parser, NULL_TREE, false, NULL);
31806   cp_parser_end_omp_structured_block (parser, save);
31807   stmt = finish_oacc_parallel (clauses, block);
31808   return stmt;
31809 }
31810
31811 /* OpenACC 2.0:
31812    # pragma acc update oacc-update-clause[optseq] new-line
31813 */
31814
31815 #define OACC_UPDATE_CLAUSE_MASK                                         \
31816         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC)               \
31817         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE)              \
31818         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)                \
31819         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF)                  \
31820         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF)                \
31821         | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
31822
31823 static tree
31824 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
31825 {
31826   tree stmt, clauses;
31827
31828   clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
31829                                          "#pragma acc update", pragma_tok);
31830
31831   if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
31832     {
31833       error_at (pragma_tok->location,
31834                 "%<#pragma acc update%> must contain at least one "
31835                 "%<device%> or %<host/self%> clause");
31836       return NULL_TREE;
31837     }
31838
31839   stmt = make_node (OACC_UPDATE);
31840   TREE_TYPE (stmt) = void_type_node;
31841   OACC_UPDATE_CLAUSES (stmt) = clauses;
31842   SET_EXPR_LOCATION (stmt, pragma_tok->location);
31843   add_stmt (stmt);
31844   return stmt;
31845 }
31846
31847 /* OpenACC 2.0:
31848    # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
31849
31850    LOC is the location of the #pragma token.
31851 */
31852
31853 #define OACC_WAIT_CLAUSE_MASK                                   \
31854         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
31855
31856 static tree
31857 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
31858 {
31859   tree clauses, list = NULL_TREE, stmt = NULL_TREE;
31860   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31861
31862   if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31863     list = cp_parser_oacc_wait_list (parser, loc, list);
31864
31865   clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
31866                                         "#pragma acc wait", pragma_tok);
31867
31868   stmt = c_finish_oacc_wait (loc, list, clauses);
31869
31870   return stmt;
31871 }
31872
31873 /* OpenMP 4.0:
31874    # pragma omp declare simd declare-simd-clauses[optseq] new-line  */
31875
31876 #define OMP_DECLARE_SIMD_CLAUSE_MASK                            \
31877         ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN)      \
31878         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR)       \
31879         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED)      \
31880         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)      \
31881         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH)     \
31882         | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
31883
31884 static void
31885 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
31886                             enum pragma_context context)
31887 {
31888   bool first_p = parser->omp_declare_simd == NULL;
31889   cp_omp_declare_simd_data data;
31890   if (first_p)
31891     {
31892       data.error_seen = false;
31893       data.fndecl_seen = false;
31894       data.tokens = vNULL;
31895       parser->omp_declare_simd = &data;
31896     }
31897   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31898          && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31899     cp_lexer_consume_token (parser->lexer);
31900   if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31901     parser->omp_declare_simd->error_seen = true;
31902   cp_parser_require_pragma_eol (parser, pragma_tok);
31903   struct cp_token_cache *cp
31904     = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
31905   parser->omp_declare_simd->tokens.safe_push (cp);
31906   if (first_p)
31907     {
31908       while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
31909         cp_parser_pragma (parser, context);
31910       switch (context)
31911         {
31912         case pragma_external:
31913           cp_parser_declaration (parser);
31914           break;
31915         case pragma_member:
31916           cp_parser_member_declaration (parser);
31917           break;
31918         case pragma_objc_icode:
31919           cp_parser_block_declaration (parser, /*statement_p=*/false);
31920           break;
31921         default:
31922           cp_parser_declaration_statement (parser);
31923           break;
31924         }
31925       if (parser->omp_declare_simd
31926           && !parser->omp_declare_simd->error_seen
31927           && !parser->omp_declare_simd->fndecl_seen)
31928         error_at (pragma_tok->location,
31929                   "%<#pragma omp declare simd%> not immediately followed by "
31930                   "function declaration or definition");
31931       data.tokens.release ();
31932       parser->omp_declare_simd = NULL;
31933     }
31934 }
31935
31936 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.  
31937    This function is modelled similar to the late parsing of omp declare 
31938    simd.  */
31939
31940 static tree
31941 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
31942 {
31943   struct cp_token_cache *ce;
31944   cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
31945   int ii = 0;
31946
31947   if (parser->omp_declare_simd != NULL)
31948     {
31949       error ("%<#pragma omp declare simd%> cannot be used in the same function"
31950              " marked as a Cilk Plus SIMD-enabled function");
31951       XDELETE (parser->cilk_simd_fn_info);
31952       parser->cilk_simd_fn_info = NULL;
31953       return attrs;
31954     }
31955   if (!info->error_seen && info->fndecl_seen)
31956     {
31957       error ("vector attribute not immediately followed by a single function"
31958              " declaration or definition");
31959       info->error_seen = true;
31960     }
31961   if (info->error_seen)
31962     return attrs;
31963
31964   FOR_EACH_VEC_ELT (info->tokens, ii, ce)
31965     {
31966       tree c, cl;
31967
31968       cp_parser_push_lexer_for_tokens (parser, ce);
31969       parser->lexer->in_pragma = true;
31970       cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
31971                                       "SIMD-enabled functions attribute", 
31972                                       NULL);
31973       cp_parser_pop_lexer (parser);
31974       if (cl)
31975         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
31976
31977       c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
31978       TREE_CHAIN (c) = attrs;
31979       attrs = c;
31980
31981       c = build_tree_list (get_identifier ("omp declare simd"), cl);
31982       TREE_CHAIN (c) = attrs;
31983       if (processing_template_decl)
31984         ATTR_IS_DEPENDENT (c) = 1;
31985       attrs = c;
31986     }
31987   info->fndecl_seen = true;
31988   XDELETE (parser->cilk_simd_fn_info);
31989   parser->cilk_simd_fn_info = NULL;
31990   return attrs;
31991 }
31992
31993 /* Finalize #pragma omp declare simd clauses after direct declarator has
31994    been parsed, and put that into "omp declare simd" attribute.  */
31995
31996 static tree
31997 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
31998 {
31999   struct cp_token_cache *ce;
32000   cp_omp_declare_simd_data *data = parser->omp_declare_simd;
32001   int i;
32002
32003   if (!data->error_seen && data->fndecl_seen)
32004     {
32005       error ("%<#pragma omp declare simd%> not immediately followed by "
32006              "a single function declaration or definition");
32007       data->error_seen = true;
32008       return attrs;
32009     }
32010   if (data->error_seen)
32011     return attrs;
32012
32013   FOR_EACH_VEC_ELT (data->tokens, i, ce)
32014     {
32015       tree c, cl;
32016
32017       cp_parser_push_lexer_for_tokens (parser, ce);
32018       parser->lexer->in_pragma = true;
32019       gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
32020       cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
32021       cp_lexer_consume_token (parser->lexer);
32022       cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
32023                                       "#pragma omp declare simd", pragma_tok);
32024       cp_parser_pop_lexer (parser);
32025       if (cl)
32026         cl = tree_cons (NULL_TREE, cl, NULL_TREE);
32027       c = build_tree_list (get_identifier ("omp declare simd"), cl);
32028       TREE_CHAIN (c) = attrs;
32029       if (processing_template_decl)
32030         ATTR_IS_DEPENDENT (c) = 1;
32031       attrs = c;
32032     }
32033
32034   data->fndecl_seen = true;
32035   return attrs;
32036 }
32037
32038
32039 /* OpenMP 4.0:
32040    # pragma omp declare target new-line
32041    declarations and definitions
32042    # pragma omp end declare target new-line  */
32043
32044 static void
32045 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
32046 {
32047   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32048   scope_chain->omp_declare_target_attribute++;
32049 }
32050
32051 static void
32052 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
32053 {
32054   const char *p = "";
32055   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32056     {
32057       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32058       p = IDENTIFIER_POINTER (id);
32059     }
32060   if (strcmp (p, "declare") == 0)
32061     {
32062       cp_lexer_consume_token (parser->lexer);
32063       p = "";
32064       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32065         {
32066           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32067           p = IDENTIFIER_POINTER (id);
32068         }
32069       if (strcmp (p, "target") == 0)
32070         cp_lexer_consume_token (parser->lexer);
32071       else
32072         {
32073           cp_parser_error (parser, "expected %<target%>");
32074           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32075           return;
32076         }
32077     }
32078   else
32079     {
32080       cp_parser_error (parser, "expected %<declare%>");
32081       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32082       return;
32083     }
32084   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32085   if (!scope_chain->omp_declare_target_attribute)
32086     error_at (pragma_tok->location,
32087               "%<#pragma omp end declare target%> without corresponding "
32088               "%<#pragma omp declare target%>");
32089   else
32090     scope_chain->omp_declare_target_attribute--;
32091 }
32092
32093 /* Helper function of cp_parser_omp_declare_reduction.  Parse the combiner
32094    expression and optional initializer clause of
32095    #pragma omp declare reduction.  We store the expression(s) as
32096    either 3, 6 or 7 special statements inside of the artificial function's
32097    body.  The first two statements are DECL_EXPRs for the artificial
32098    OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
32099    expression that uses those variables.
32100    If there was any INITIALIZER clause, this is followed by further statements,
32101    the fourth and fifth statements are DECL_EXPRs for the artificial
32102    OMP_PRIV resp. OMP_ORIG variables.  If the INITIALIZER clause wasn't the
32103    constructor variant (first token after open paren is not omp_priv),
32104    then the sixth statement is a statement with the function call expression
32105    that uses the OMP_PRIV and optionally OMP_ORIG variable.
32106    Otherwise, the sixth statement is whatever statement cp_finish_decl emits
32107    to initialize the OMP_PRIV artificial variable and there is seventh
32108    statement, a DECL_EXPR of the OMP_PRIV statement again.  */
32109
32110 static bool
32111 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
32112 {
32113   tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
32114   gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
32115   type = TREE_TYPE (type);
32116   tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
32117   DECL_ARTIFICIAL (omp_out) = 1;
32118   pushdecl (omp_out);
32119   add_decl_expr (omp_out);
32120   tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
32121   DECL_ARTIFICIAL (omp_in) = 1;
32122   pushdecl (omp_in);
32123   add_decl_expr (omp_in);
32124   tree combiner;
32125   tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
32126
32127   keep_next_level (true);
32128   tree block = begin_omp_structured_block ();
32129   combiner = cp_parser_expression (parser);
32130   finish_expr_stmt (combiner);
32131   block = finish_omp_structured_block (block);
32132   add_stmt (block);
32133
32134   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32135     return false;
32136
32137   const char *p = "";
32138   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32139     {
32140       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32141       p = IDENTIFIER_POINTER (id);
32142     }
32143
32144   if (strcmp (p, "initializer") == 0)
32145     {
32146       cp_lexer_consume_token (parser->lexer);
32147       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32148         return false;
32149
32150       p = "";
32151       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32152         {
32153           tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32154           p = IDENTIFIER_POINTER (id);
32155         }
32156
32157       omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
32158       DECL_ARTIFICIAL (omp_priv) = 1;
32159       pushdecl (omp_priv);
32160       add_decl_expr (omp_priv);
32161       omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
32162       DECL_ARTIFICIAL (omp_orig) = 1;
32163       pushdecl (omp_orig);
32164       add_decl_expr (omp_orig);
32165
32166       keep_next_level (true);
32167       block = begin_omp_structured_block ();
32168
32169       bool ctor = false;
32170       if (strcmp (p, "omp_priv") == 0)
32171         {
32172           bool is_direct_init, is_non_constant_init;
32173           ctor = true;
32174           cp_lexer_consume_token (parser->lexer);
32175           /* Reject initializer (omp_priv) and initializer (omp_priv ()).  */
32176           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
32177               || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32178                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
32179                      == CPP_CLOSE_PAREN
32180                   && cp_lexer_peek_nth_token (parser->lexer, 3)->type
32181                      == CPP_CLOSE_PAREN))
32182             {
32183               finish_omp_structured_block (block);
32184               error ("invalid initializer clause");
32185               return false;
32186             }
32187           initializer = cp_parser_initializer (parser, &is_direct_init,
32188                                                &is_non_constant_init);
32189           cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
32190                           NULL_TREE, LOOKUP_ONLYCONVERTING);
32191         }
32192       else
32193         {
32194           cp_parser_parse_tentatively (parser);
32195           tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
32196                                                   /*check_dependency_p=*/true,
32197                                                   /*template_p=*/NULL,
32198                                                   /*declarator_p=*/false,
32199                                                   /*optional_p=*/false);
32200           vec<tree, va_gc> *args;
32201           if (fn_name == error_mark_node
32202               || cp_parser_error_occurred (parser)
32203               || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
32204               || ((args = cp_parser_parenthesized_expression_list
32205                                 (parser, non_attr, /*cast_p=*/false,
32206                                  /*allow_expansion_p=*/true,
32207                                  /*non_constant_p=*/NULL)),
32208                   cp_parser_error_occurred (parser)))
32209             {
32210               finish_omp_structured_block (block);
32211               cp_parser_abort_tentative_parse (parser);
32212               cp_parser_error (parser, "expected id-expression (arguments)");
32213               return false;
32214             }
32215           unsigned int i;
32216           tree arg;
32217           FOR_EACH_VEC_SAFE_ELT (args, i, arg)
32218             if (arg == omp_priv
32219                 || (TREE_CODE (arg) == ADDR_EXPR
32220                     && TREE_OPERAND (arg, 0) == omp_priv))
32221               break;
32222           cp_parser_abort_tentative_parse (parser);
32223           if (arg == NULL_TREE)
32224             error ("one of the initializer call arguments should be %<omp_priv%>"
32225                    " or %<&omp_priv%>");
32226           initializer = cp_parser_postfix_expression (parser, false, false, false,
32227                                                       false, NULL);
32228           finish_expr_stmt (initializer);
32229         }
32230
32231       block = finish_omp_structured_block (block);
32232       cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
32233       add_stmt (block);
32234
32235       if (ctor)
32236         add_decl_expr (omp_orig);
32237
32238       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32239         return false;
32240     }
32241
32242   if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
32243     cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
32244
32245   return true;
32246 }
32247
32248 /* OpenMP 4.0
32249    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32250       initializer-clause[opt] new-line
32251
32252    initializer-clause:
32253       initializer (omp_priv initializer)
32254       initializer (function-name (argument-list))  */
32255
32256 static void
32257 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
32258                                  enum pragma_context)
32259 {
32260   auto_vec<tree> types;
32261   enum tree_code reduc_code = ERROR_MARK;
32262   tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
32263   unsigned int i;
32264   cp_token *first_token;
32265   cp_token_cache *cp;
32266   int errs;
32267   void *p;
32268     
32269   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
32270   p = obstack_alloc (&declarator_obstack, 0);
32271
32272   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32273     goto fail;
32274
32275   switch (cp_lexer_peek_token (parser->lexer)->type)
32276     {
32277     case CPP_PLUS:
32278       reduc_code = PLUS_EXPR;
32279       break;
32280     case CPP_MULT:
32281       reduc_code = MULT_EXPR;
32282       break;
32283     case CPP_MINUS:
32284       reduc_code = MINUS_EXPR;
32285       break;
32286     case CPP_AND:
32287       reduc_code = BIT_AND_EXPR;
32288       break;
32289     case CPP_XOR:
32290       reduc_code = BIT_XOR_EXPR;
32291       break;
32292     case CPP_OR:
32293       reduc_code = BIT_IOR_EXPR;
32294       break;
32295     case CPP_AND_AND:
32296       reduc_code = TRUTH_ANDIF_EXPR;
32297       break;
32298     case CPP_OR_OR:
32299       reduc_code = TRUTH_ORIF_EXPR;
32300       break;
32301     case CPP_NAME:
32302       reduc_id = orig_reduc_id = cp_parser_identifier (parser);
32303       break;
32304     default:
32305       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
32306                                "%<|%>, %<&&%>, %<||%> or identifier");
32307       goto fail;
32308     }
32309
32310   if (reduc_code != ERROR_MARK)
32311     cp_lexer_consume_token (parser->lexer);
32312
32313   reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
32314   if (reduc_id == error_mark_node)
32315     goto fail;
32316
32317   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32318     goto fail;
32319
32320   /* Types may not be defined in declare reduction type list.  */
32321   const char *saved_message;
32322   saved_message = parser->type_definition_forbidden_message;
32323   parser->type_definition_forbidden_message
32324     = G_("types may not be defined in declare reduction type list");
32325   bool saved_colon_corrects_to_scope_p;
32326   saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32327   parser->colon_corrects_to_scope_p = false;
32328   bool saved_colon_doesnt_start_class_def_p;
32329   saved_colon_doesnt_start_class_def_p
32330     = parser->colon_doesnt_start_class_def_p;
32331   parser->colon_doesnt_start_class_def_p = true;
32332
32333   while (true)
32334     {
32335       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32336       type = cp_parser_type_id (parser);
32337       if (type == error_mark_node)
32338         ;
32339       else if (ARITHMETIC_TYPE_P (type)
32340                && (orig_reduc_id == NULL_TREE
32341                    || (TREE_CODE (type) != COMPLEX_TYPE
32342                        && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32343                                    "min") == 0
32344                            || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
32345                                       "max") == 0))))
32346         error_at (loc, "predeclared arithmetic type %qT in "
32347                        "%<#pragma omp declare reduction%>", type);
32348       else if (TREE_CODE (type) == FUNCTION_TYPE
32349                || TREE_CODE (type) == METHOD_TYPE
32350                || TREE_CODE (type) == ARRAY_TYPE)
32351         error_at (loc, "function or array type %qT in "
32352                        "%<#pragma omp declare reduction%>", type);
32353       else if (TREE_CODE (type) == REFERENCE_TYPE)
32354         error_at (loc, "reference type %qT in "
32355                        "%<#pragma omp declare reduction%>", type);
32356       else if (TYPE_QUALS_NO_ADDR_SPACE (type))
32357         error_at (loc, "const, volatile or __restrict qualified type %qT in "
32358                        "%<#pragma omp declare reduction%>", type);
32359       else
32360         types.safe_push (type);
32361
32362       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32363         cp_lexer_consume_token (parser->lexer);
32364       else
32365         break;
32366     }
32367
32368   /* Restore the saved message.  */
32369   parser->type_definition_forbidden_message = saved_message;
32370   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32371   parser->colon_doesnt_start_class_def_p
32372     = saved_colon_doesnt_start_class_def_p;
32373
32374   if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
32375       || types.is_empty ())
32376     {
32377      fail:
32378       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32379       goto done;
32380     }
32381
32382   first_token = cp_lexer_peek_token (parser->lexer);
32383   cp = NULL;
32384   errs = errorcount;
32385   FOR_EACH_VEC_ELT (types, i, type)
32386     {
32387       tree fntype
32388         = build_function_type_list (void_type_node,
32389                                     cp_build_reference_type (type, false),
32390                                     NULL_TREE);
32391       tree this_reduc_id = reduc_id;
32392       if (!dependent_type_p (type))
32393         this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
32394       tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
32395       DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
32396       DECL_ARTIFICIAL (fndecl) = 1;
32397       DECL_EXTERNAL (fndecl) = 1;
32398       DECL_DECLARED_INLINE_P (fndecl) = 1;
32399       DECL_IGNORED_P (fndecl) = 1;
32400       DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
32401       SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
32402       DECL_ATTRIBUTES (fndecl)
32403         = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
32404                      DECL_ATTRIBUTES (fndecl));
32405       if (processing_template_decl)
32406         fndecl = push_template_decl (fndecl);
32407       bool block_scope = false;
32408       tree block = NULL_TREE;
32409       if (current_function_decl)
32410         {
32411           block_scope = true;
32412           DECL_CONTEXT (fndecl) = global_namespace;
32413           if (!processing_template_decl)
32414             pushdecl (fndecl);
32415         }
32416       else if (current_class_type)
32417         {
32418           if (cp == NULL)
32419             {
32420               while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32421                      && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
32422                 cp_lexer_consume_token (parser->lexer);
32423               if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32424                 goto fail;
32425               cp = cp_token_cache_new (first_token,
32426                                        cp_lexer_peek_nth_token (parser->lexer,
32427                                                                 2));
32428             }
32429           DECL_STATIC_FUNCTION_P (fndecl) = 1;
32430           finish_member_declaration (fndecl);
32431           DECL_PENDING_INLINE_INFO (fndecl) = cp;
32432           DECL_PENDING_INLINE_P (fndecl) = 1;
32433           vec_safe_push (unparsed_funs_with_definitions, fndecl);
32434           continue;
32435         }
32436       else
32437         {
32438           DECL_CONTEXT (fndecl) = current_namespace;
32439           pushdecl (fndecl);
32440         }
32441       if (!block_scope)
32442         start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
32443       else
32444         block = begin_omp_structured_block ();
32445       if (cp)
32446         {
32447           cp_parser_push_lexer_for_tokens (parser, cp);
32448           parser->lexer->in_pragma = true;
32449         }
32450       if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
32451         {
32452           if (!block_scope)
32453             finish_function (0);
32454           else
32455             DECL_CONTEXT (fndecl) = current_function_decl;
32456           if (cp)
32457             cp_parser_pop_lexer (parser);
32458           goto fail;
32459         }
32460       if (cp)
32461         cp_parser_pop_lexer (parser);
32462       if (!block_scope)
32463         finish_function (0);
32464       else
32465         {
32466           DECL_CONTEXT (fndecl) = current_function_decl;
32467           block = finish_omp_structured_block (block);
32468           if (TREE_CODE (block) == BIND_EXPR)
32469             DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
32470           else if (TREE_CODE (block) == STATEMENT_LIST)
32471             DECL_SAVED_TREE (fndecl) = block;
32472           if (processing_template_decl)
32473             add_decl_expr (fndecl);
32474         }
32475       cp_check_omp_declare_reduction (fndecl);
32476       if (cp == NULL && types.length () > 1)
32477         cp = cp_token_cache_new (first_token,
32478                                  cp_lexer_peek_nth_token (parser->lexer, 2));
32479       if (errs != errorcount)
32480         break;
32481     }
32482
32483   cp_parser_require_pragma_eol (parser, pragma_tok);
32484
32485  done:
32486   /* Free any declarators allocated.  */
32487   obstack_free (&declarator_obstack, p);
32488 }
32489
32490 /* OpenMP 4.0
32491    #pragma omp declare simd declare-simd-clauses[optseq] new-line
32492    #pragma omp declare reduction (reduction-id : typename-list : expression) \
32493       initializer-clause[opt] new-line
32494    #pragma omp declare target new-line  */
32495
32496 static void
32497 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
32498                        enum pragma_context context)
32499 {
32500   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32501     {
32502       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32503       const char *p = IDENTIFIER_POINTER (id);
32504
32505       if (strcmp (p, "simd") == 0)
32506         {
32507           cp_lexer_consume_token (parser->lexer);
32508           cp_parser_omp_declare_simd (parser, pragma_tok,
32509                                       context);
32510           return;
32511         }
32512       cp_ensure_no_omp_declare_simd (parser);
32513       if (strcmp (p, "reduction") == 0)
32514         {
32515           cp_lexer_consume_token (parser->lexer);
32516           cp_parser_omp_declare_reduction (parser, pragma_tok,
32517                                            context);
32518           return;
32519         }
32520       if (!flag_openmp)  /* flag_openmp_simd  */
32521         {
32522           cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32523           return;
32524         }
32525       if (strcmp (p, "target") == 0)
32526         {
32527           cp_lexer_consume_token (parser->lexer);
32528           cp_parser_omp_declare_target (parser, pragma_tok);
32529           return;
32530         }
32531     }
32532   cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
32533                            "or %<target%>");
32534   cp_parser_require_pragma_eol (parser, pragma_tok);
32535 }
32536
32537 /* Main entry point to OpenMP statement pragmas.  */
32538
32539 static void
32540 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
32541 {
32542   tree stmt;
32543   char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
32544   omp_clause_mask mask (0);
32545
32546   switch (pragma_tok->pragma_kind)
32547     {
32548     case PRAGMA_OACC_CACHE:
32549       stmt = cp_parser_oacc_cache (parser, pragma_tok);
32550       break;
32551     case PRAGMA_OACC_DATA:
32552       stmt = cp_parser_oacc_data (parser, pragma_tok);
32553       break;
32554     case PRAGMA_OACC_ENTER_DATA:
32555       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
32556       break;
32557     case PRAGMA_OACC_EXIT_DATA:
32558       stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
32559       break;
32560     case PRAGMA_OACC_KERNELS:
32561       stmt = cp_parser_oacc_kernels (parser, pragma_tok);
32562       break;
32563     case PRAGMA_OACC_LOOP:
32564       stmt = cp_parser_oacc_loop (parser, pragma_tok);
32565       break;
32566     case PRAGMA_OACC_PARALLEL:
32567       stmt = cp_parser_oacc_parallel (parser, pragma_tok);
32568       break;
32569     case PRAGMA_OACC_UPDATE:
32570       stmt = cp_parser_oacc_update (parser, pragma_tok);
32571       break;
32572     case PRAGMA_OACC_WAIT:
32573       stmt = cp_parser_oacc_wait (parser, pragma_tok);
32574       break;
32575     case PRAGMA_OMP_ATOMIC:
32576       cp_parser_omp_atomic (parser, pragma_tok);
32577       return;
32578     case PRAGMA_OMP_CRITICAL:
32579       stmt = cp_parser_omp_critical (parser, pragma_tok);
32580       break;
32581     case PRAGMA_OMP_DISTRIBUTE:
32582       strcpy (p_name, "#pragma omp");
32583       stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
32584       break;
32585     case PRAGMA_OMP_FOR:
32586       strcpy (p_name, "#pragma omp");
32587       stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
32588       break;
32589     case PRAGMA_OMP_MASTER:
32590       stmt = cp_parser_omp_master (parser, pragma_tok);
32591       break;
32592     case PRAGMA_OMP_ORDERED:
32593       stmt = cp_parser_omp_ordered (parser, pragma_tok);
32594       break;
32595     case PRAGMA_OMP_PARALLEL:
32596       strcpy (p_name, "#pragma omp");
32597       stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
32598       break;
32599     case PRAGMA_OMP_SECTIONS:
32600       strcpy (p_name, "#pragma omp");
32601       stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
32602       break;
32603     case PRAGMA_OMP_SIMD:
32604       strcpy (p_name, "#pragma omp");
32605       stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
32606       break;
32607     case PRAGMA_OMP_SINGLE:
32608       stmt = cp_parser_omp_single (parser, pragma_tok);
32609       break;
32610     case PRAGMA_OMP_TASK:
32611       stmt = cp_parser_omp_task (parser, pragma_tok);
32612       break;
32613     case PRAGMA_OMP_TASKGROUP:
32614       stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
32615       break;
32616     case PRAGMA_OMP_TEAMS:
32617       strcpy (p_name, "#pragma omp");
32618       stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
32619       break;
32620     default:
32621       gcc_unreachable ();
32622     }
32623
32624   if (stmt)
32625     SET_EXPR_LOCATION (stmt, pragma_tok->location);
32626 }
32627 \f
32628 /* Transactional Memory parsing routines.  */
32629
32630 /* Parse a transaction attribute.
32631
32632    txn-attribute:
32633         attribute
32634         [ [ identifier ] ]
32635
32636    ??? Simplify this when C++0x bracket attributes are
32637    implemented properly.  */
32638
32639 static tree
32640 cp_parser_txn_attribute_opt (cp_parser *parser)
32641 {
32642   cp_token *token;
32643   tree attr_name, attr = NULL;
32644
32645   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
32646     return cp_parser_attributes_opt (parser);
32647
32648   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
32649     return NULL_TREE;
32650   cp_lexer_consume_token (parser->lexer);
32651   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
32652     goto error1;
32653
32654   token = cp_lexer_peek_token (parser->lexer);
32655   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
32656     {
32657       token = cp_lexer_consume_token (parser->lexer);
32658
32659       attr_name = (token->type == CPP_KEYWORD
32660                    /* For keywords, use the canonical spelling,
32661                       not the parsed identifier.  */
32662                    ? ridpointers[(int) token->keyword]
32663                    : token->u.value);
32664       attr = build_tree_list (attr_name, NULL_TREE);
32665     }
32666   else
32667     cp_parser_error (parser, "expected identifier");
32668
32669   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32670  error1:
32671   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
32672   return attr;
32673 }
32674
32675 /* Parse a __transaction_atomic or __transaction_relaxed statement.
32676
32677    transaction-statement:
32678      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
32679        compound-statement
32680      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
32681 */
32682
32683 static tree
32684 cp_parser_transaction (cp_parser *parser, enum rid keyword)
32685 {
32686   unsigned char old_in = parser->in_transaction;
32687   unsigned char this_in = 1, new_in;
32688   cp_token *token;
32689   tree stmt, attrs, noex;
32690
32691   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32692       || keyword == RID_TRANSACTION_RELAXED);
32693   token = cp_parser_require_keyword (parser, keyword,
32694       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32695           : RT_TRANSACTION_RELAXED));
32696   gcc_assert (token != NULL);
32697
32698   if (keyword == RID_TRANSACTION_RELAXED)
32699     this_in |= TM_STMT_ATTR_RELAXED;
32700   else
32701     {
32702       attrs = cp_parser_txn_attribute_opt (parser);
32703       if (attrs)
32704         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32705     }
32706
32707   /* Parse a noexcept specification.  */
32708   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
32709
32710   /* Keep track if we're in the lexical scope of an outer transaction.  */
32711   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
32712
32713   stmt = begin_transaction_stmt (token->location, NULL, this_in);
32714
32715   parser->in_transaction = new_in;
32716   cp_parser_compound_statement (parser, NULL, false, false);
32717   parser->in_transaction = old_in;
32718
32719   finish_transaction_stmt (stmt, NULL, this_in, noex);
32720
32721   return stmt;
32722 }
32723
32724 /* Parse a __transaction_atomic or __transaction_relaxed expression.
32725
32726    transaction-expression:
32727      __transaction_atomic txn-noexcept-spec[opt] ( expression )
32728      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
32729 */
32730
32731 static tree
32732 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
32733 {
32734   unsigned char old_in = parser->in_transaction;
32735   unsigned char this_in = 1;
32736   cp_token *token;
32737   tree expr, noex;
32738   bool noex_expr;
32739
32740   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32741       || keyword == RID_TRANSACTION_RELAXED);
32742
32743   if (!flag_tm)
32744     error (keyword == RID_TRANSACTION_RELAXED
32745            ? G_("%<__transaction_relaxed%> without transactional memory "
32746                 "support enabled")
32747            : G_("%<__transaction_atomic%> without transactional memory "
32748                 "support enabled"));
32749
32750   token = cp_parser_require_keyword (parser, keyword,
32751       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32752           : RT_TRANSACTION_RELAXED));
32753   gcc_assert (token != NULL);
32754
32755   if (keyword == RID_TRANSACTION_RELAXED)
32756     this_in |= TM_STMT_ATTR_RELAXED;
32757
32758   /* Set this early.  This might mean that we allow transaction_cancel in
32759      an expression that we find out later actually has to be a constexpr.
32760      However, we expect that cxx_constant_value will be able to deal with
32761      this; also, if the noexcept has no constexpr, then what we parse next
32762      really is a transaction's body.  */
32763   parser->in_transaction = this_in;
32764
32765   /* Parse a noexcept specification.  */
32766   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
32767                                                true);
32768
32769   if (!noex || !noex_expr
32770       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32771     {
32772       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
32773
32774       expr = cp_parser_expression (parser);
32775       expr = finish_parenthesized_expr (expr);
32776
32777       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
32778     }
32779   else
32780     {
32781       /* The only expression that is available got parsed for the noexcept
32782          already.  noexcept is true then.  */
32783       expr = noex;
32784       noex = boolean_true_node;
32785     }
32786
32787   expr = build_transaction_expr (token->location, expr, this_in, noex);
32788   parser->in_transaction = old_in;
32789
32790   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
32791     return error_mark_node;
32792
32793   return (flag_tm ? expr : error_mark_node);
32794 }
32795
32796 /* Parse a function-transaction-block.
32797
32798    function-transaction-block:
32799      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
32800          function-body
32801      __transaction_atomic txn-attribute[opt] function-try-block
32802      __transaction_relaxed ctor-initializer[opt] function-body
32803      __transaction_relaxed function-try-block
32804 */
32805
32806 static bool
32807 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
32808 {
32809   unsigned char old_in = parser->in_transaction;
32810   unsigned char new_in = 1;
32811   tree compound_stmt, stmt, attrs;
32812   bool ctor_initializer_p;
32813   cp_token *token;
32814
32815   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
32816       || keyword == RID_TRANSACTION_RELAXED);
32817   token = cp_parser_require_keyword (parser, keyword,
32818       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
32819           : RT_TRANSACTION_RELAXED));
32820   gcc_assert (token != NULL);
32821
32822   if (keyword == RID_TRANSACTION_RELAXED)
32823     new_in |= TM_STMT_ATTR_RELAXED;
32824   else
32825     {
32826       attrs = cp_parser_txn_attribute_opt (parser);
32827       if (attrs)
32828         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
32829     }
32830
32831   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
32832
32833   parser->in_transaction = new_in;
32834
32835   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32836     ctor_initializer_p = cp_parser_function_try_block (parser);
32837   else
32838     ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
32839       (parser, /*in_function_try_block=*/false);
32840
32841   parser->in_transaction = old_in;
32842
32843   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
32844
32845   return ctor_initializer_p;
32846 }
32847
32848 /* Parse a __transaction_cancel statement.
32849
32850    cancel-statement:
32851      __transaction_cancel txn-attribute[opt] ;
32852      __transaction_cancel txn-attribute[opt] throw-expression ;
32853
32854    ??? Cancel and throw is not yet implemented.  */
32855
32856 static tree
32857 cp_parser_transaction_cancel (cp_parser *parser)
32858 {
32859   cp_token *token;
32860   bool is_outer = false;
32861   tree stmt, attrs;
32862
32863   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
32864                                      RT_TRANSACTION_CANCEL);
32865   gcc_assert (token != NULL);
32866
32867   attrs = cp_parser_txn_attribute_opt (parser);
32868   if (attrs)
32869     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
32870
32871   /* ??? Parse cancel-and-throw here.  */
32872
32873   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
32874
32875   if (!flag_tm)
32876     {
32877       error_at (token->location, "%<__transaction_cancel%> without "
32878                 "transactional memory support enabled");
32879       return error_mark_node;
32880     }
32881   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
32882     {
32883       error_at (token->location, "%<__transaction_cancel%> within a "
32884                 "%<__transaction_relaxed%>");
32885       return error_mark_node;
32886     }
32887   else if (is_outer)
32888     {
32889       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
32890           && !is_tm_may_cancel_outer (current_function_decl))
32891         {
32892           error_at (token->location, "outer %<__transaction_cancel%> not "
32893                     "within outer %<__transaction_atomic%>");
32894           error_at (token->location,
32895                     "  or a %<transaction_may_cancel_outer%> function");
32896           return error_mark_node;
32897         }
32898     }
32899   else if (parser->in_transaction == 0)
32900     {
32901       error_at (token->location, "%<__transaction_cancel%> not within "
32902                 "%<__transaction_atomic%>");
32903       return error_mark_node;
32904     }
32905
32906   stmt = build_tm_abort_call (token->location, is_outer);
32907   add_stmt (stmt);
32908
32909   return stmt;
32910 }
32911 \f
32912 /* The parser.  */
32913
32914 static GTY (()) cp_parser *the_parser;
32915
32916 \f
32917 /* Special handling for the first token or line in the file.  The first
32918    thing in the file might be #pragma GCC pch_preprocess, which loads a
32919    PCH file, which is a GC collection point.  So we need to handle this
32920    first pragma without benefit of an existing lexer structure.
32921
32922    Always returns one token to the caller in *FIRST_TOKEN.  This is
32923    either the true first token of the file, or the first token after
32924    the initial pragma.  */
32925
32926 static void
32927 cp_parser_initial_pragma (cp_token *first_token)
32928 {
32929   tree name = NULL;
32930
32931   cp_lexer_get_preprocessor_token (NULL, first_token);
32932   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
32933     return;
32934
32935   cp_lexer_get_preprocessor_token (NULL, first_token);
32936   if (first_token->type == CPP_STRING)
32937     {
32938       name = first_token->u.value;
32939
32940       cp_lexer_get_preprocessor_token (NULL, first_token);
32941       if (first_token->type != CPP_PRAGMA_EOL)
32942         error_at (first_token->location,
32943                   "junk at end of %<#pragma GCC pch_preprocess%>");
32944     }
32945   else
32946     error_at (first_token->location, "expected string literal");
32947
32948   /* Skip to the end of the pragma.  */
32949   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
32950     cp_lexer_get_preprocessor_token (NULL, first_token);
32951
32952   /* Now actually load the PCH file.  */
32953   if (name)
32954     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
32955
32956   /* Read one more token to return to our caller.  We have to do this
32957      after reading the PCH file in, since its pointers have to be
32958      live.  */
32959   cp_lexer_get_preprocessor_token (NULL, first_token);
32960 }
32961
32962 /* Parses the grainsize pragma for the _Cilk_for statement.
32963    Syntax:
32964    #pragma cilk grainsize = <VALUE>.  */
32965
32966 static void
32967 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
32968 {
32969   if (cp_parser_require (parser, CPP_EQ, RT_EQ))
32970     {
32971       tree exp = cp_parser_binary_expression (parser, false, false,
32972                                               PREC_NOT_OPERATOR, NULL);
32973       cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32974       if (!exp || exp == error_mark_node)
32975         {
32976           error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
32977           return;
32978         }
32979
32980       /* Make sure the next token is _Cilk_for, it is invalid otherwise.  */
32981       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
32982         cp_parser_cilk_for (parser, exp);
32983       else
32984         warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
32985                     "%<#pragma cilk grainsize%> is not followed by "
32986                     "%<_Cilk_for%>");
32987       return;
32988     }
32989   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32990 }
32991
32992 /* Normal parsing of a pragma token.  Here we can (and must) use the
32993    regular lexer.  */
32994
32995 static bool
32996 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
32997 {
32998   cp_token *pragma_tok;
32999   unsigned int id;
33000
33001   pragma_tok = cp_lexer_consume_token (parser->lexer);
33002   gcc_assert (pragma_tok->type == CPP_PRAGMA);
33003   parser->lexer->in_pragma = true;
33004
33005   id = pragma_tok->pragma_kind;
33006   if (id != PRAGMA_OMP_DECLARE_REDUCTION)
33007     cp_ensure_no_omp_declare_simd (parser);
33008   switch (id)
33009     {
33010     case PRAGMA_GCC_PCH_PREPROCESS:
33011       error_at (pragma_tok->location,
33012                 "%<#pragma GCC pch_preprocess%> must be first");
33013       break;
33014
33015     case PRAGMA_OMP_BARRIER:
33016       switch (context)
33017         {
33018         case pragma_compound:
33019           cp_parser_omp_barrier (parser, pragma_tok);
33020           return false;
33021         case pragma_stmt:
33022           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
33023                     "used in compound statements");
33024           break;
33025         default:
33026           goto bad_stmt;
33027         }
33028       break;
33029
33030     case PRAGMA_OMP_FLUSH:
33031       switch (context)
33032         {
33033         case pragma_compound:
33034           cp_parser_omp_flush (parser, pragma_tok);
33035           return false;
33036         case pragma_stmt:
33037           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
33038                     "used in compound statements");
33039           break;
33040         default:
33041           goto bad_stmt;
33042         }
33043       break;
33044
33045     case PRAGMA_OMP_TASKWAIT:
33046       switch (context)
33047         {
33048         case pragma_compound:
33049           cp_parser_omp_taskwait (parser, pragma_tok);
33050           return false;
33051         case pragma_stmt:
33052           error_at (pragma_tok->location,
33053                     "%<#pragma omp taskwait%> may only be "
33054                     "used in compound statements");
33055           break;
33056         default:
33057           goto bad_stmt;
33058         }
33059       break;
33060
33061     case PRAGMA_OMP_TASKYIELD:
33062       switch (context)
33063         {
33064         case pragma_compound:
33065           cp_parser_omp_taskyield (parser, pragma_tok);
33066           return false;
33067         case pragma_stmt:
33068           error_at (pragma_tok->location,
33069                     "%<#pragma omp taskyield%> may only be "
33070                     "used in compound statements");
33071           break;
33072         default:
33073           goto bad_stmt;
33074         }
33075       break;
33076
33077     case PRAGMA_OMP_CANCEL:
33078       switch (context)
33079         {
33080         case pragma_compound:
33081           cp_parser_omp_cancel (parser, pragma_tok);
33082           return false;
33083         case pragma_stmt:
33084           error_at (pragma_tok->location,
33085                     "%<#pragma omp cancel%> may only be "
33086                     "used in compound statements");
33087           break;
33088         default:
33089           goto bad_stmt;
33090         }
33091       break;
33092
33093     case PRAGMA_OMP_CANCELLATION_POINT:
33094       switch (context)
33095         {
33096         case pragma_compound:
33097           cp_parser_omp_cancellation_point (parser, pragma_tok);
33098           return false;
33099         case pragma_stmt:
33100           error_at (pragma_tok->location,
33101                     "%<#pragma omp cancellation point%> may only be "
33102                     "used in compound statements");
33103           break;
33104         default:
33105           goto bad_stmt;
33106         }
33107       break;
33108
33109     case PRAGMA_OMP_THREADPRIVATE:
33110       cp_parser_omp_threadprivate (parser, pragma_tok);
33111       return false;
33112
33113     case PRAGMA_OMP_DECLARE_REDUCTION:
33114       cp_parser_omp_declare (parser, pragma_tok, context);
33115       return false;
33116
33117     case PRAGMA_OACC_CACHE:
33118     case PRAGMA_OACC_DATA:
33119     case PRAGMA_OACC_ENTER_DATA:
33120     case PRAGMA_OACC_EXIT_DATA:
33121     case PRAGMA_OACC_KERNELS:
33122     case PRAGMA_OACC_PARALLEL:
33123     case PRAGMA_OACC_LOOP:
33124     case PRAGMA_OACC_UPDATE:
33125     case PRAGMA_OACC_WAIT:
33126     case PRAGMA_OMP_ATOMIC:
33127     case PRAGMA_OMP_CRITICAL:
33128     case PRAGMA_OMP_DISTRIBUTE:
33129     case PRAGMA_OMP_FOR:
33130     case PRAGMA_OMP_MASTER:
33131     case PRAGMA_OMP_ORDERED:
33132     case PRAGMA_OMP_PARALLEL:
33133     case PRAGMA_OMP_SECTIONS:
33134     case PRAGMA_OMP_SIMD:
33135     case PRAGMA_OMP_SINGLE:
33136     case PRAGMA_OMP_TASK:
33137     case PRAGMA_OMP_TASKGROUP:
33138     case PRAGMA_OMP_TEAMS:
33139       if (context != pragma_stmt && context != pragma_compound)
33140         goto bad_stmt;
33141       cp_parser_omp_construct (parser, pragma_tok);
33142       return true;
33143
33144     case PRAGMA_OMP_TARGET:
33145       return cp_parser_omp_target (parser, pragma_tok, context);
33146
33147     case PRAGMA_OMP_END_DECLARE_TARGET:
33148       cp_parser_omp_end_declare_target (parser, pragma_tok);
33149       return false;
33150
33151     case PRAGMA_OMP_SECTION:
33152       error_at (pragma_tok->location, 
33153                 "%<#pragma omp section%> may only be used in "
33154                 "%<#pragma omp sections%> construct");
33155       break;
33156
33157     case PRAGMA_IVDEP:
33158       {
33159         if (context == pragma_external)
33160           {
33161             error_at (pragma_tok->location,
33162                       "%<#pragma GCC ivdep%> must be inside a function");
33163             break;
33164           }
33165         cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33166         cp_token *tok;
33167         tok = cp_lexer_peek_token (the_parser->lexer);
33168         if (tok->type != CPP_KEYWORD
33169             || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
33170                 && tok->keyword != RID_DO))
33171           {
33172             cp_parser_error (parser, "for, while or do statement expected");
33173             return false;
33174           }
33175         cp_parser_iteration_statement (parser, true);
33176         return true;
33177       }
33178
33179     case PRAGMA_CILK_SIMD:
33180       if (context == pragma_external)
33181         {
33182           error_at (pragma_tok->location,
33183                     "%<#pragma simd%> must be inside a function");
33184           break;
33185         }
33186       cp_parser_cilk_simd (parser, pragma_tok);
33187       return true;
33188
33189     case PRAGMA_CILK_GRAINSIZE:
33190       if (context == pragma_external)
33191         {
33192           error_at (pragma_tok->location,
33193                     "%<#pragma cilk grainsize%> must be inside a function");
33194           break;
33195         }
33196
33197       /* Ignore the pragma if Cilk Plus is not enabled.  */
33198       if (flag_cilkplus)
33199         {
33200           cp_parser_cilk_grainsize (parser, pragma_tok);
33201           return true;
33202         }
33203       else
33204         {
33205           error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
33206                     "%<#pragma cilk grainsize%>");
33207           break;
33208         }
33209
33210     default:
33211       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
33212       c_invoke_pragma_handler (id);
33213       break;
33214
33215     bad_stmt:
33216       cp_parser_error (parser, "expected declaration specifiers");
33217       break;
33218     }
33219
33220   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33221   return false;
33222 }
33223
33224 /* The interface the pragma parsers have to the lexer.  */
33225
33226 enum cpp_ttype
33227 pragma_lex (tree *value)
33228 {
33229   cp_token *tok;
33230   enum cpp_ttype ret;
33231
33232   tok = cp_lexer_peek_token (the_parser->lexer);
33233
33234   ret = tok->type;
33235   *value = tok->u.value;
33236
33237   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
33238     ret = CPP_EOF;
33239   else if (ret == CPP_STRING)
33240     *value = cp_parser_string_literal (the_parser, false, false);
33241   else
33242     {
33243       cp_lexer_consume_token (the_parser->lexer);
33244       if (ret == CPP_KEYWORD)
33245         ret = CPP_NAME;
33246     }
33247
33248   return ret;
33249 }
33250
33251 \f
33252 /* External interface.  */
33253
33254 /* Parse one entire translation unit.  */
33255
33256 void
33257 c_parse_file (void)
33258 {
33259   static bool already_called = false;
33260
33261   if (already_called)
33262     fatal_error (input_location,
33263                  "inter-module optimizations not implemented for C++");
33264   already_called = true;
33265
33266   the_parser = cp_parser_new ();
33267   push_deferring_access_checks (flag_access_control
33268                                 ? dk_no_deferred : dk_no_check);
33269   cp_parser_translation_unit (the_parser);
33270   the_parser = NULL;
33271 }
33272
33273 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's 
33274    vectorlength clause:
33275    Syntax:
33276    vectorlength ( constant-expression )  */
33277
33278 static tree
33279 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
33280                                   bool is_simd_fn)
33281 {
33282   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33283   tree expr;
33284   /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
33285      safelen clause.  Thus, vectorlength is represented as OMP 4.0
33286      safelen.  For SIMD-enabled function it is represented by OMP 4.0
33287      simdlen.  */
33288   if (!is_simd_fn)
33289     check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength", 
33290                                loc);
33291   else
33292     check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
33293                                loc);
33294
33295   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33296     return error_mark_node;
33297
33298   expr = cp_parser_constant_expression (parser);
33299   expr = maybe_constant_value (expr);
33300
33301   /* If expr == error_mark_node, then don't emit any errors nor
33302      create a clause.  if any of the above functions returns
33303      error mark node then they would have emitted an error message.  */
33304   if (expr == error_mark_node)
33305     ;
33306   else if (!TREE_TYPE (expr)
33307            || !TREE_CONSTANT (expr)
33308            || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
33309     error_at (loc, "vectorlength must be an integer constant");
33310   else if (TREE_CONSTANT (expr)
33311            && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
33312     error_at (loc, "vectorlength must be a power of 2");
33313   else 
33314     {
33315       tree c;
33316       if (!is_simd_fn)
33317         { 
33318           c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN); 
33319           OMP_CLAUSE_SAFELEN_EXPR (c) = expr; 
33320           OMP_CLAUSE_CHAIN (c) = clauses; 
33321           clauses = c;
33322         }
33323       else
33324         {
33325           c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
33326           OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
33327           OMP_CLAUSE_CHAIN (c) = clauses;
33328           clauses = c;
33329         }
33330     }
33331
33332   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33333     return error_mark_node;
33334   return clauses;
33335 }
33336
33337 /* Handles the Cilk Plus #pragma simd linear clause.
33338    Syntax:
33339    linear ( simd-linear-variable-list )
33340
33341    simd-linear-variable-list:
33342      simd-linear-variable
33343      simd-linear-variable-list , simd-linear-variable
33344
33345    simd-linear-variable:
33346      id-expression
33347      id-expression : simd-linear-step
33348
33349    simd-linear-step:
33350    conditional-expression */
33351
33352 static tree
33353 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
33354 {
33355   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33356
33357   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33358     return clauses;
33359   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33360     {
33361       cp_parser_error (parser, "expected identifier");
33362       cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33363       return error_mark_node;
33364     }
33365
33366   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33367   parser->colon_corrects_to_scope_p = false;
33368   while (1)
33369     {
33370       cp_token *token = cp_lexer_peek_token (parser->lexer);
33371       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33372         {
33373           cp_parser_error (parser, "expected variable-name");
33374           clauses = error_mark_node;
33375           break;
33376         }
33377
33378       tree var_name = cp_parser_id_expression (parser, false, true, NULL,
33379                                                false, false);
33380       tree decl = cp_parser_lookup_name_simple (parser, var_name,
33381                                                 token->location);
33382       if (decl == error_mark_node)
33383         {
33384           cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
33385                                        token->location);
33386           clauses = error_mark_node;
33387         }
33388       else
33389         {
33390           tree e = NULL_TREE;
33391           tree step_size = integer_one_node;
33392
33393           /* If present, parse the linear step.  Otherwise, assume the default
33394              value of 1.  */
33395           if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
33396             {
33397               cp_lexer_consume_token (parser->lexer);
33398
33399               e = cp_parser_assignment_expression (parser);
33400               e = maybe_constant_value (e);
33401
33402               if (e == error_mark_node)
33403                 {
33404                   /* If an error has occurred,  then the whole pragma is
33405                      considered ill-formed.  Thus, no reason to keep
33406                      parsing.  */
33407                   clauses = error_mark_node;
33408                   break;
33409                 }
33410               else if (type_dependent_expression_p (e)
33411                        || value_dependent_expression_p (e)
33412                        || (TREE_TYPE (e)
33413                            && INTEGRAL_TYPE_P (TREE_TYPE (e))
33414                            && (TREE_CONSTANT (e)
33415                                || DECL_P (e))))
33416                 step_size = e;
33417               else
33418                 cp_parser_error (parser,
33419                                  "step size must be an integer constant "
33420                                  "expression or an integer variable");
33421             }
33422
33423           /* Use the OMP_CLAUSE_LINEAR,  which has the same semantics.  */
33424           tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33425           OMP_CLAUSE_DECL (l) = decl;
33426           OMP_CLAUSE_LINEAR_STEP (l) = step_size;
33427           OMP_CLAUSE_CHAIN (l) = clauses;
33428           clauses = l;
33429         }
33430       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33431         cp_lexer_consume_token (parser->lexer);
33432       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33433         break;
33434       else
33435         {
33436           error_at (cp_lexer_peek_token (parser->lexer)->location,
33437                     "expected %<,%> or %<)%> after %qE", decl);
33438           clauses = error_mark_node;
33439           break;
33440         }
33441     }
33442   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33443   cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
33444   return clauses;
33445 }
33446
33447 /* Returns the name of the next clause.  If the clause is not
33448    recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
33449    token is not consumed.  Otherwise, the appropriate enum from the
33450    pragma_simd_clause is returned and the token is consumed.  */
33451
33452 static pragma_omp_clause
33453 cp_parser_cilk_simd_clause_name (cp_parser *parser)
33454 {
33455   pragma_omp_clause clause_type;
33456   cp_token *token = cp_lexer_peek_token (parser->lexer);
33457
33458   if (token->keyword == RID_PRIVATE)
33459     clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
33460   else if (!token->u.value || token->type != CPP_NAME)
33461     return PRAGMA_CILK_CLAUSE_NONE;
33462   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
33463     clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
33464   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
33465     clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
33466   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
33467     clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
33468   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
33469     clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
33470   else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
33471     clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
33472   else
33473     return PRAGMA_CILK_CLAUSE_NONE;
33474
33475   cp_lexer_consume_token (parser->lexer);
33476   return clause_type;
33477 }
33478
33479 /* Parses all the #pragma simd clauses.  Returns a list of clauses found.  */
33480
33481 static tree
33482 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
33483 {
33484   tree clauses = NULL_TREE;
33485
33486   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
33487          && clauses != error_mark_node)
33488     {
33489       pragma_omp_clause c_kind;
33490       c_kind = cp_parser_cilk_simd_clause_name (parser);
33491       if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
33492         clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
33493       else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
33494         clauses = cp_parser_cilk_simd_linear (parser, clauses);
33495       else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
33496         /* Use the OpenMP 4.0 equivalent function.  */
33497         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
33498       else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
33499         /* Use the OpenMP 4.0 equivalent function.  */
33500         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33501                                           clauses);
33502       else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
33503         /* Use the OMP 4.0 equivalent function.  */
33504         clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33505                                           clauses);
33506       else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
33507         /* Use the OMP 4.0 equivalent function.  */
33508         clauses = cp_parser_omp_clause_reduction (parser, clauses);
33509       else
33510         {
33511           clauses = error_mark_node;
33512           cp_parser_error (parser, "expected %<#pragma simd%> clause");
33513           break;
33514         }
33515     }
33516
33517   cp_parser_skip_to_pragma_eol (parser, pragma_token);
33518
33519   if (clauses == error_mark_node)
33520     return error_mark_node;
33521   else
33522     return c_finish_cilk_clauses (clauses);
33523 }
33524
33525 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops.  */
33526
33527 static void
33528 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
33529 {
33530   tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
33531
33532   if (clauses == error_mark_node)
33533     return;
33534
33535   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
33536     {
33537       error_at (cp_lexer_peek_token (parser->lexer)->location,
33538                 "for statement expected");
33539       return;
33540     }
33541
33542   tree sb = begin_omp_structured_block ();
33543   int save = cp_parser_begin_omp_structured_block (parser);
33544   tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
33545   if (ret)
33546     cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
33547   cp_parser_end_omp_structured_block (parser, save);
33548   add_stmt (finish_omp_structured_block (sb));
33549 }
33550
33551 /* Main entry-point for parsing Cilk Plus _Cilk_for
33552    loops.  The return value is error_mark_node
33553    when errors happen and CILK_FOR tree on success.  */
33554
33555 static tree
33556 cp_parser_cilk_for (cp_parser *parser, tree grain)
33557 {
33558   if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
33559     gcc_unreachable ();
33560
33561   tree sb = begin_omp_structured_block ();
33562   int save = cp_parser_begin_omp_structured_block (parser);
33563
33564   tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
33565   OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
33566   OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
33567   clauses = finish_omp_clauses (clauses);
33568
33569   tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
33570   if (ret)
33571     cpp_validate_cilk_plus_loop (ret);
33572   else
33573     ret = error_mark_node;
33574
33575   cp_parser_end_omp_structured_block (parser, save);
33576   add_stmt (finish_omp_structured_block (sb));
33577   return ret;
33578 }
33579
33580 /* Create an identifier for a generic parameter type (a synthesized
33581    template parameter implied by `auto' or a concept identifier). */
33582
33583 static GTY(()) int generic_parm_count;
33584 static tree
33585 make_generic_type_name ()
33586 {
33587   char buf[32];
33588   sprintf (buf, "auto:%d", ++generic_parm_count);
33589   return get_identifier (buf);
33590 }
33591
33592 /* Predicate that behaves as is_auto_or_concept but matches the parent
33593    node of the generic type rather than the generic type itself.  This
33594    allows for type transformation in add_implicit_template_parms.  */
33595
33596 static inline bool
33597 tree_type_is_auto_or_concept (const_tree t)
33598 {
33599   return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
33600 }
33601
33602 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
33603    (creating a new template parameter list if necessary).  Returns the newly
33604    created template type parm.  */
33605
33606 tree
33607 synthesize_implicit_template_parm  (cp_parser *parser)
33608 {
33609   gcc_assert (current_binding_level->kind == sk_function_parms);
33610
33611   /* We are either continuing a function template that already contains implicit
33612      template parameters, creating a new fully-implicit function template, or
33613      extending an existing explicit function template with implicit template
33614      parameters.  */
33615
33616   cp_binding_level *const entry_scope = current_binding_level;
33617
33618   bool become_template = false;
33619   cp_binding_level *parent_scope = 0;
33620
33621   if (parser->implicit_template_scope)
33622     {
33623       gcc_assert (parser->implicit_template_parms);
33624
33625       current_binding_level = parser->implicit_template_scope;
33626     }
33627   else
33628     {
33629       /* Roll back to the existing template parameter scope (in the case of
33630          extending an explicit function template) or introduce a new template
33631          parameter scope ahead of the function parameter scope (or class scope
33632          in the case of out-of-line member definitions).  The function scope is
33633          added back after template parameter synthesis below.  */
33634
33635       cp_binding_level *scope = entry_scope;
33636
33637       while (scope->kind == sk_function_parms)
33638         {
33639           parent_scope = scope;
33640           scope = scope->level_chain;
33641         }
33642       if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
33643         {
33644           /* If not defining a class, then any class scope is a scope level in
33645              an out-of-line member definition.  In this case simply wind back
33646              beyond the first such scope to inject the template parameter list.
33647              Otherwise wind back to the class being defined.  The latter can
33648              occur in class member friend declarations such as:
33649
33650                class A {
33651                  void foo (auto);
33652                };
33653                class B {
33654                  friend void A::foo (auto);
33655                };
33656
33657             The template parameter list synthesized for the friend declaration
33658             must be injected in the scope of 'B'.  This can also occur in
33659             erroneous cases such as:
33660
33661                struct A {
33662                  struct B {
33663                    void foo (auto);
33664                  };
33665                  void B::foo (auto) {}
33666                };
33667
33668             Here the attempted definition of 'B::foo' within 'A' is ill-formed
33669             but, nevertheless, the template parameter list synthesized for the
33670             declarator should be injected into the scope of 'A' as if the
33671             ill-formed template was specified explicitly.  */
33672
33673           while (scope->kind == sk_class && !scope->defining_class_p)
33674             {
33675               parent_scope = scope;
33676               scope = scope->level_chain;
33677             }
33678         }
33679
33680       current_binding_level = scope;
33681
33682       if (scope->kind != sk_template_parms
33683           || !function_being_declared_is_template_p (parser))
33684         {
33685           /* Introduce a new template parameter list for implicit template
33686              parameters.  */
33687
33688           become_template = true;
33689
33690           parser->implicit_template_scope
33691               = begin_scope (sk_template_parms, NULL);
33692
33693           ++processing_template_decl;
33694
33695           parser->fully_implicit_function_template_p = true;
33696           ++parser->num_template_parameter_lists;
33697         }
33698       else
33699         {
33700           /* Synthesize implicit template parameters at the end of the explicit
33701              template parameter list.  */
33702
33703           gcc_assert (current_template_parms);
33704
33705           parser->implicit_template_scope = scope;
33706
33707           tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33708           parser->implicit_template_parms
33709             = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
33710         }
33711     }
33712
33713   /* Synthesize a new template parameter and track the current template
33714      parameter chain with implicit_template_parms.  */
33715
33716   tree synth_id = make_generic_type_name ();
33717   tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
33718                                                     synth_id);
33719   tree new_parm
33720     = process_template_parm (parser->implicit_template_parms,
33721                              input_location,
33722                              build_tree_list (NULL_TREE, synth_tmpl_parm),
33723                              /*non_type=*/false,
33724                              /*param_pack=*/false);
33725
33726
33727   if (parser->implicit_template_parms)
33728     parser->implicit_template_parms
33729       = TREE_CHAIN (parser->implicit_template_parms);
33730   else
33731     parser->implicit_template_parms = new_parm;
33732
33733   tree new_type = TREE_TYPE (getdecls ());
33734
33735   /* If creating a fully implicit function template, start the new implicit
33736      template parameter list with this synthesized type, otherwise grow the
33737      current template parameter list.  */
33738
33739   if (become_template)
33740     {
33741       parent_scope->level_chain = current_binding_level;
33742
33743       tree new_parms = make_tree_vec (1);
33744       TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
33745       current_template_parms = tree_cons (size_int (processing_template_decl),
33746                                           new_parms, current_template_parms);
33747     }
33748   else
33749     {
33750       tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
33751       int new_parm_idx = TREE_VEC_LENGTH (new_parms);
33752       new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
33753       TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
33754     }
33755
33756   current_binding_level = entry_scope;
33757
33758   return new_type;
33759 }
33760
33761 /* Finish the declaration of a fully implicit function template.  Such a
33762    template has no explicit template parameter list so has not been through the
33763    normal template head and tail processing.  synthesize_implicit_template_parm
33764    tries to do the head; this tries to do the tail.  MEMBER_DECL_OPT should be
33765    provided if the declaration is a class member such that its template
33766    declaration can be completed.  If MEMBER_DECL_OPT is provided the finished
33767    form is returned.  Otherwise NULL_TREE is returned. */
33768
33769 tree
33770 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
33771 {
33772   gcc_assert (parser->fully_implicit_function_template_p);
33773
33774   if (member_decl_opt && member_decl_opt != error_mark_node
33775       && DECL_VIRTUAL_P (member_decl_opt))
33776     {
33777       error_at (DECL_SOURCE_LOCATION (member_decl_opt),
33778                 "implicit templates may not be %<virtual%>");
33779       DECL_VIRTUAL_P (member_decl_opt) = false;
33780     }
33781
33782   if (member_decl_opt)
33783     member_decl_opt = finish_member_template_decl (member_decl_opt);
33784   end_template_decl ();
33785
33786   parser->fully_implicit_function_template_p = false;
33787   --parser->num_template_parameter_lists;
33788
33789   return member_decl_opt;
33790 }
33791
33792 #include "gt-cp-parser.h"